Index: lib/IR/Value.cpp =================================================================== --- lib/IR/Value.cpp +++ lib/IR/Value.cpp @@ -773,6 +773,8 @@ void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); assert(Old != New && "Changing value into itself!"); + assert(Old->getType() == New->getType() && + "replaceAllUses of value with new value of different type!"); // Get the linked list base, which is guaranteed to exist since the // HasValueHandle flag is set. Index: lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCalls.cpp +++ lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -1286,7 +1286,7 @@ if (!Caller->use_empty() && // void -> non-void is handled specially !NewRetTy->isVoidTy()) - return false; // Cannot transform this return value. + return false; // Cannot transform this return value. } if (!CallerPAL.isEmpty() && !Caller->use_empty()) { @@ -1505,8 +1505,14 @@ if (!Caller->use_empty()) ReplaceInstUsesWith(*Caller, NV); - else if (Caller->hasValueHandle()) - ValueHandleBase::ValueIsRAUWd(Caller, NV); + else if (Caller->hasValueHandle()) { + if (OldRetTy == NV->getType()) + ValueHandleBase::ValueIsRAUWd(Caller, NV); + else + // We cannot call ValueIsRAUWd with a different type, and the + // actual tracked value will disappear. + ValueHandleBase::ValueIsDeleted(Caller); + } EraseInstFromFunction(*Caller); return true; Index: test/DebugInfo/voidMDNode.ll =================================================================== --- /dev/null +++ test/DebugInfo/voidMDNode.ll @@ -0,0 +1,63 @@ +; RUN: opt -instcombine < %s | FileCheck %s + +; IR generated from (the obvisouly wrong): +; void foo(void) {} +; void baz() { +; int deadvar = ((int (*)(void))&foo)(); +; } +; and optimized with just -mem2reg + +define void @foo() #0 { +entry: + ret void, !dbg !17 +} + +; Function Attrs: nounwind ssp uwtable +define void @baz() #0 { +entry: + %call = call i32 bitcast (void ()* @foo to i32 ()*)(), !dbg !18 +; Check that when instcombine removes the const bitcast in the call +; instruction, it doesn't RAUW the %call value with the replacement +; instruction that is of void type. Metadata referenceing a void operand +; would be rendered as . +; CHECK-NOT: + call void @llvm.dbg.value(metadata !{i32 %call}, i64 0, metadata !19, metadata !20), !dbg !21 + ret void, !dbg !22 +} + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.value(metadata, i64, metadata, metadata) #1 + +attributes #0 = { nounwind ssp uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!14, !15} +!llvm.ident = !{!16} + +!0 = metadata !{metadata !"0x11\0012\00clang version 3.6.0 \000\00\000\00\001", metadata !1, metadata !2, metadata !3, metadata !8, metadata !2, metadata !2} ; [ DW_TAG_compile_unit ] [/tmp/voidmetadata.c] [DW_LANG_C99] +!1 = metadata !{metadata !"voidmetadata.c", metadata !"/tmp"} +!2 = metadata !{} +!3 = metadata !{metadata !4} +!4 = metadata !{metadata !"0xf\00\000\0064\0064\000\000", null, null, metadata !5} ; [ DW_TAG_pointer_type ] [line 0, size 64, align 64, offset 0] [from ] +!5 = metadata !{metadata !"0x15\00\000\000\000\000\000\000", null, null, null, metadata !6, null, null, null} ; [ DW_TAG_subroutine_type ] [line 0, size 0, align 0, offset 0] [from ] +!6 = metadata !{metadata !7} +!7 = metadata !{metadata !"0x24\00int\000\0032\0032\000\000\005", null, null} ; [ DW_TAG_base_type ] [int] [line 0, size 32, align 32, offset 0, enc DW_ATE_signed] +!8 = metadata !{metadata !9, metadata !13} +!9 = metadata !{metadata !"0x2e\00foo\00foo\00\002\000\001\000\000\00256\000\002", metadata !1, metadata !10, metadata !11, null, void ()* @foo, null, null, metadata !2} ; [ DW_TAG_subprogram ] [line 2] [def] [foo] +!10 = metadata !{metadata !"0x29", metadata !1} ; [ DW_TAG_file_type ] [/tmp/voidmetadata.c] +!11 = metadata !{metadata !"0x15\00\000\000\000\000\000\000", null, null, null, metadata !12, null, null, null} ; [ DW_TAG_subroutine_type ] [line 0, size 0, align 0, offset 0] [from ] +!12 = metadata !{null} +!13 = metadata !{metadata !"0x2e\00baz\00baz\00\004\000\001\000\000\000\000\004", metadata !1, metadata !10, metadata !11, null, void ()* @baz, null, null, metadata !2} ; [ DW_TAG_subprogram ] [line 4] [def] [baz] +!14 = metadata !{i32 2, metadata !"Dwarf Version", i32 2} +!15 = metadata !{i32 2, metadata !"Debug Info Version", i32 2} +!16 = metadata !{metadata !"clang version 3.6.0 "} +!17 = metadata !{i32 2, i32 17, metadata !9, null} +!18 = metadata !{i32 5, i32 16, metadata !13, null} +!19 = metadata !{metadata !"0x100\00deadvar\005\000", metadata !13, metadata !10, metadata !7} ; [ DW_TAG_auto_variable ] [deadvar] [line 5] +!20 = metadata !{metadata !"0x102"} ; [ DW_TAG_expression ] +!21 = metadata !{i32 5, i32 6, metadata !13, null} +!22 = metadata !{i32 6, i32 1, metadata !13, null}