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

Commit d0ef044

Browse files
committed
Ignore debug info when making optimization decisions in SimplifyCFG.
Summary: Debug info should *not* affect code generation. This patch properly handles debug info to make sure the generated code are the same with or without debug info. Reviewers: davidxl, mzolotukhin, jmolloy Subscribers: aprantl, llvm-commits Differential Revision: https://reviews.llvm.org/D25286 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@284415 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 29dee96 commit d0ef044

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,8 +1485,14 @@ static bool sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) {
14851485
// canSinkLastInstruction returning true guarantees that every block has at
14861486
// least one non-terminator instruction.
14871487
SmallVector<Instruction*,4> Insts;
1488-
for (auto *BB : Blocks)
1489-
Insts.push_back(BB->getTerminator()->getPrevNode());
1488+
for (auto *BB : Blocks) {
1489+
Instruction *I = BB->getTerminator();
1490+
do {
1491+
I = I->getPrevNode();
1492+
} while (isa<DbgInfoIntrinsic>(I) && I != &BB->front());
1493+
if (!isa<DbgInfoIntrinsic>(I))
1494+
Insts.push_back(I);
1495+
}
14901496

14911497
// The only checking we need to do now is that all users of all instructions
14921498
// are the same PHI node. canSinkLastInstruction should have checked this but
@@ -1584,15 +1590,15 @@ namespace {
15841590
Fail = false;
15851591
Insts.clear();
15861592
for (auto *BB : Blocks) {
1587-
if (Instruction *Terminator = BB->getTerminator()) {
1588-
if (Instruction *LastNonTerminator = Terminator->getPrevNode()) {
1589-
Insts.push_back(LastNonTerminator);
1590-
continue;
1591-
}
1593+
Instruction *Inst = BB->getTerminator();
1594+
for (Inst = Inst->getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
1595+
Inst = Inst->getPrevNode();
1596+
if (!Inst) {
1597+
// Block wasn't big enough.
1598+
Fail = true;
1599+
return;
15921600
}
1593-
// Block wasn't big enough.
1594-
Fail = true;
1595-
return;
1601+
Insts.push_back(Inst);
15961602
}
15971603
}
15981604

@@ -1604,7 +1610,8 @@ namespace {
16041610
if (Fail)
16051611
return;
16061612
for (auto *&Inst : Insts) {
1607-
Inst = Inst->getPrevNode();
1613+
for (Inst = Inst->getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
1614+
Inst = Inst->getPrevNode();
16081615
// Already at beginning of block.
16091616
if (!Inst) {
16101617
Fail = true;

test/Transforms/SimplifyCFG/sink-common-code.ll

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ if.end:
340340
; CHECK-LABEL: test13
341341
; CHECK-DAG: select
342342
; CHECK-DAG: load volatile
343-
; CHECK: store volatile {{.*}}, !tbaa !0
343+
; CHECK: store volatile {{.*}}, !tbaa ![[TBAA:[0-9]]]
344344
; CHECK-NOT: load
345345
; CHECK-NOT: store
346346

@@ -384,13 +384,26 @@ if.else:
384384
%gepb = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 1
385385
%sv2 = load i32, i32* %gepb
386386
%cmp2 = icmp eq i32 %sv2, 57
387+
call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !9, metadata !DIExpression()), !dbg !11
387388
br label %if.end
388389

389390
if.end:
390391
%p = phi i1 [ %cmp1, %if.then ], [ %cmp2, %if.else ]
391392
ret i32 1
392393
}
393394

395+
declare void @llvm.dbg.value(metadata, i64, metadata, metadata)
396+
!llvm.module.flags = !{!5, !6}
397+
!llvm.dbg.cu = !{!7}
398+
399+
!5 = !{i32 2, !"Dwarf Version", i32 4}
400+
!6 = !{i32 2, !"Debug Info Version", i32 3}
401+
!7 = distinct !DICompileUnit(language: DW_LANG_C99, file: !10)
402+
!8 = distinct !DISubprogram(name: "foo", unit: !7)
403+
!9 = !DILocalVariable(name: "b", line: 1, arg: 2, scope: !8)
404+
!10 = !DIFile(filename: "a.c", directory: "a/b")
405+
!11 = !DILocation(line: 1, column: 14, scope: !8)
406+
394407
; CHECK-LABEL: test14
395408
; CHECK: getelementptr
396409
; CHECK: load
@@ -781,6 +794,6 @@ merge:
781794
; CHECK: right:
782795
; CHECK-NEXT: %val1 = call i32 @call_target() [ "deopt"(i32 20) ]
783796

784-
; CHECK: !0 = !{!1, !1, i64 0}
785-
; CHECK: !1 = !{!"float", !2}
786-
; CHECK: !2 = !{!"an example type tree"}
797+
; CHECK: ![[TBAA]] = !{![[TYPE:[0-9]]], ![[TYPE]], i64 0}
798+
; CHECK: ![[TYPE]] = !{!"float", ![[TEXT:[0-9]]]}
799+
; CHECK: ![[TEXT]] = !{!"an example type tree"}

0 commit comments

Comments
 (0)