Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 647e081

Browse files
committed
[InstCombine] Merge DebugLoc when speculatively hoisting store instruction
Summary: Along with https://reviews.llvm.org/D27804, debug locations need to be merged when hoisting store instructions as well. Not sure if just dropping debug locations would make more sense for this case, but as the branch instruction will have at least different discriminator with the hoisted store instruction, I think there will be no difference in practice. Reviewers: aprantl, andreadb, danielcdh Reviewed By: aprantl Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D29062 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@293372 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 0439ed6 commit 647e081

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ static bool HoistThenElseCodeToIf(BranchInst *BI,
12801280
if (!isa<CallInst>(I1))
12811281
I1->setDebugLoc(
12821282
DILocation::getMergedLocation(I1->getDebugLoc(), I2->getDebugLoc()));
1283-
1283+
12841284
I2->eraseFromParent();
12851285
Changed = true;
12861286

@@ -1546,7 +1546,7 @@ static bool sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) {
15461546
}))
15471547
return false;
15481548
}
1549-
1549+
15501550
// We don't need to do any more checking here; canSinkLastInstruction should
15511551
// have done it all for us.
15521552
SmallVector<Value*, 4> NewOperands;
@@ -1653,7 +1653,7 @@ namespace {
16531653
bool isValid() const {
16541654
return !Fail;
16551655
}
1656-
1656+
16571657
void operator -- () {
16581658
if (Fail)
16591659
return;
@@ -1737,7 +1737,7 @@ static bool SinkThenElseCodeToEnd(BranchInst *BI1) {
17371737
}
17381738
if (UnconditionalPreds.size() < 2)
17391739
return false;
1740-
1740+
17411741
bool Changed = false;
17421742
// We take a two-step approach to tail sinking. First we scan from the end of
17431743
// each block upwards in lockstep. If the n'th instruction from the end of each
@@ -1767,7 +1767,7 @@ static bool SinkThenElseCodeToEnd(BranchInst *BI1) {
17671767
unsigned NumPHIInsts = NumPHIdValues / UnconditionalPreds.size();
17681768
if ((NumPHIdValues % UnconditionalPreds.size()) != 0)
17691769
NumPHIInsts++;
1770-
1770+
17711771
return NumPHIInsts <= 1;
17721772
};
17731773

@@ -1790,7 +1790,7 @@ static bool SinkThenElseCodeToEnd(BranchInst *BI1) {
17901790
}
17911791
if (!Profitable)
17921792
return false;
1793-
1793+
17941794
DEBUG(dbgs() << "SINK: Splitting edge\n");
17951795
// We have a conditional edge and we're going to sink some instructions.
17961796
// Insert a new block postdominating all blocks we're going to sink from.
@@ -1800,7 +1800,7 @@ static bool SinkThenElseCodeToEnd(BranchInst *BI1) {
18001800
return false;
18011801
Changed = true;
18021802
}
1803-
1803+
18041804
// Now that we've analyzed all potential sinking candidates, perform the
18051805
// actual sink. We iteratively sink the last non-terminator of the source
18061806
// blocks into their common successor unless doing so would require too
@@ -1826,7 +1826,7 @@ static bool SinkThenElseCodeToEnd(BranchInst *BI1) {
18261826
DEBUG(dbgs() << "SINK: stopping here, too many PHIs would be created!\n");
18271827
break;
18281828
}
1829-
1829+
18301830
if (!sinkLastInstruction(UnconditionalPreds))
18311831
return Changed;
18321832
NumSinkCommons++;
@@ -2078,6 +2078,9 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,
20782078
Value *S = Builder.CreateSelect(
20792079
BrCond, TrueV, FalseV, TrueV->getName() + "." + FalseV->getName(), BI);
20802080
SpeculatedStore->setOperand(0, S);
2081+
SpeculatedStore->setDebugLoc(
2082+
DILocation::getMergedLocation(
2083+
BI->getDebugLoc(), SpeculatedStore->getDebugLoc()));
20812084
}
20822085

20832086
// Metadata can be dependent on the condition we are hoisting above.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
; RUN: opt < %s -simplifycfg -S | FileCheck %s
2+
3+
; Check if the debug info for hoisted store for "ret = 0" is removed
4+
;
5+
; int foo(int x) {
6+
; int ret = 1;
7+
; if (x)
8+
; ret = 0;
9+
; return ret;
10+
; }
11+
;
12+
; CHECK: store i32 1,{{.+}}!dbg ![[DLOC1:[0-9]+]]
13+
; CHECK: icmp ne {{.+}}!dbg ![[DLOC2:[0-9]+]]
14+
; CHECK: [[VREG:%[^ ]+]] = select
15+
; CHECK: store i32 [[VREG]]
16+
; CHECK-NOT: !dbg
17+
; CHECK-SAME: {{$}}
18+
; CHECK: ret {{.+}}!dbg ![[DLOC3:[0-9]+]]
19+
; CHECK: ![[DLOC1]] = !DILocation(line: 2
20+
; CHECK: ![[DLOC2]] = !DILocation(line: 3
21+
; CHECK: ![[DLOC3]] = !DILocation(line: 5
22+
23+
target triple = "x86_64-unknown-linux-gnu"
24+
25+
; Function Attrs: noinline nounwind uwtable
26+
define i32 @foo(i32) !dbg !6 {
27+
%2 = alloca i32, align 4
28+
%3 = alloca i32, align 4
29+
store i32 %0, i32* %2, align 4
30+
store i32 1, i32* %3, align 4, !dbg !14
31+
%4 = load i32, i32* %2, align 4, !dbg !15
32+
%5 = icmp ne i32 %4, 0, !dbg !15
33+
br i1 %5, label %6, label %7, !dbg !17
34+
35+
; <label>:6: ; preds = %1
36+
store i32 0, i32* %3, align 4, !dbg !18
37+
br label %7, !dbg !19
38+
39+
; <label>:7: ; preds = %6, %1
40+
%8 = load i32, i32* %3, align 4, !dbg !20
41+
ret i32 %8, !dbg !21
42+
}
43+
44+
!llvm.dbg.cu = !{!0}
45+
!llvm.module.flags = !{!3, !4}
46+
47+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1)
48+
!1 = !DIFile(filename: "foo.c", directory: "b/")
49+
!2 = !{}
50+
!3 = !{i32 2, !"Dwarf Version", i32 4}
51+
!4 = !{i32 2, !"Debug Info Version", i32 3}
52+
!5 = !{}
53+
!6 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)
54+
!7 = !DISubroutineType(types: !8)
55+
!8 = !{!9, !9}
56+
!9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
57+
!10 = !DILocalVariable(name: "x", arg: 1, scope: !6, file: !1, line: 1, type: !9)
58+
!11 = !DIExpression()
59+
!12 = !DILocation(line: 1, column: 13, scope: !6)
60+
!13 = !DILocalVariable(name: "ret", scope: !6, file: !1, line: 2, type: !9)
61+
!14 = !DILocation(line: 2, column: 7, scope: !6)
62+
!15 = !DILocation(line: 3, column: 7, scope: !16)
63+
!16 = distinct !DILexicalBlock(scope: !6, file: !1, line: 3, column: 7)
64+
!17 = !DILocation(line: 3, column: 7, scope: !6)
65+
!18 = !DILocation(line: 4, column: 9, scope: !16)
66+
!19 = !DILocation(line: 4, column: 5, scope: !16)
67+
!20 = !DILocation(line: 5, column: 10, scope: !6)
68+
!21 = !DILocation(line: 5, column: 3, scope: !6)

0 commit comments

Comments
 (0)