Index: lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- lib/Transforms/Utils/SimplifyCFG.cpp +++ lib/Transforms/Utils/SimplifyCFG.cpp @@ -1280,7 +1280,7 @@ if (!isa(I1)) I1->setDebugLoc( DILocation::getMergedLocation(I1->getDebugLoc(), I2->getDebugLoc())); - + I2->eraseFromParent(); Changed = true; @@ -1538,7 +1538,7 @@ })) return false; } - + // We don't need to do any more checking here; canSinkLastInstruction should // have done it all for us. SmallVector NewOperands; @@ -1645,7 +1645,7 @@ bool isValid() const { return !Fail; } - + void operator -- () { if (Fail) return; @@ -1729,7 +1729,7 @@ } if (UnconditionalPreds.size() < 2) return false; - + bool Changed = false; // We take a two-step approach to tail sinking. First we scan from the end of // each block upwards in lockstep. If the n'th instruction from the end of each @@ -1759,7 +1759,7 @@ unsigned NumPHIInsts = NumPHIdValues / UnconditionalPreds.size(); if ((NumPHIdValues % UnconditionalPreds.size()) != 0) NumPHIInsts++; - + return NumPHIInsts <= 1; }; @@ -1782,7 +1782,7 @@ } if (!Profitable) return false; - + DEBUG(dbgs() << "SINK: Splitting edge\n"); // We have a conditional edge and we're going to sink some instructions. // Insert a new block postdominating all blocks we're going to sink from. @@ -1792,7 +1792,7 @@ return false; Changed = true; } - + // Now that we've analyzed all potential sinking candidates, perform the // actual sink. We iteratively sink the last non-terminator of the source // blocks into their common successor unless doing so would require too @@ -1818,7 +1818,7 @@ DEBUG(dbgs() << "SINK: stopping here, too many PHIs would be created!\n"); break; } - + if (!sinkLastInstruction(UnconditionalPreds)) return Changed; NumSinkCommons++; @@ -2070,6 +2070,9 @@ Value *S = Builder.CreateSelect( BrCond, TrueV, FalseV, TrueV->getName() + "." + FalseV->getName(), BI); SpeculatedStore->setOperand(0, S); + SpeculatedStore->setDebugLoc( + DILocation::getMergedLocation( + BI->getDebugLoc(), SpeculatedStore->getDebugLoc())); } // Metadata can be dependent on the condition we are hoisting above. Index: test/Transforms/SimplifyCFG/remove-debug-2.ll =================================================================== --- /dev/null +++ test/Transforms/SimplifyCFG/remove-debug-2.ll @@ -0,0 +1,66 @@ +; RUN: opt < %s -simplifycfg -S | FileCheck %s + +; CHECK: line: 1 +; CHECK: line: 2 +; CHECK: line: 3 +; CHECK-NOT: line: 4 +; CHECK: line: 5 +; +; Checks if the debug info for hoisted "x = i" is removed +; int x; +; void bar(); +; void baz(); +; +; int foo(int x) { +; int ret = 1; +; if (x) +; ret = 0; +; return ret; +; } + +target triple = "x86_64-unknown-linux-gnu" + +; Function Attrs: noinline nounwind uwtable +define i32 @foo(i32) !dbg !6 { + %2 = alloca i32, align 4 + %3 = alloca i32, align 4 + store i32 %0, i32* %2, align 4 + store i32 1, i32* %3, align 4, !dbg !14 + %4 = load i32, i32* %2, align 4, !dbg !15 + %5 = icmp ne i32 %4, 0, !dbg !15 + br i1 %5, label %6, label %7, !dbg !17 + +;