1 //===- Local.cpp - Unit tests for Local -----------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/Utils/Local.h"
10 #include "llvm/Analysis/DomTreeUpdater.h"
11 #include "llvm/Analysis/PostDominators.h"
12 #include "llvm/AsmParser/Parser.h"
13 #include "llvm/IR/BasicBlock.h"
14 #include "llvm/IR/DIBuilder.h"
15 #include "llvm/IR/IRBuilder.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/IntrinsicInst.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Verifier.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "gtest/gtest.h"
25 TEST(Local
, RecursivelyDeleteDeadPHINodes
) {
28 IRBuilder
<> builder(C
);
31 BasicBlock
*bb0
= BasicBlock::Create(C
);
32 BasicBlock
*bb1
= BasicBlock::Create(C
);
34 builder
.SetInsertPoint(bb0
);
35 PHINode
*phi
= builder
.CreatePHI(Type::getInt32Ty(C
), 2);
36 BranchInst
*br0
= builder
.CreateCondBr(builder
.getTrue(), bb0
, bb1
);
38 builder
.SetInsertPoint(bb1
);
39 BranchInst
*br1
= builder
.CreateBr(bb0
);
41 phi
->addIncoming(phi
, bb0
);
42 phi
->addIncoming(phi
, bb1
);
44 // The PHI will be removed
45 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi
));
47 // Make sure the blocks only contain the branches
48 EXPECT_EQ(&bb0
->front(), br0
);
49 EXPECT_EQ(&bb1
->front(), br1
);
51 builder
.SetInsertPoint(bb0
);
52 phi
= builder
.CreatePHI(Type::getInt32Ty(C
), 0);
54 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi
));
56 builder
.SetInsertPoint(bb0
);
57 phi
= builder
.CreatePHI(Type::getInt32Ty(C
), 0);
58 builder
.CreateAdd(phi
, phi
);
60 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi
));
62 bb0
->dropAllReferences();
63 bb1
->dropAllReferences();
68 TEST(Local
, RemoveDuplicatePHINodes
) {
72 std::unique_ptr
<Function
> F(
73 Function::Create(FunctionType::get(B
.getVoidTy(), false),
74 GlobalValue::ExternalLinkage
, "F"));
75 BasicBlock
*Entry(BasicBlock::Create(C
, "", F
.get()));
76 BasicBlock
*BB(BasicBlock::Create(C
, "", F
.get()));
77 BranchInst::Create(BB
, Entry
);
81 AssertingVH
<PHINode
> P1
= B
.CreatePHI(Type::getInt32Ty(C
), 2);
82 P1
->addIncoming(B
.getInt32(42), Entry
);
84 PHINode
*P2
= B
.CreatePHI(Type::getInt32Ty(C
), 2);
85 P2
->addIncoming(B
.getInt32(42), Entry
);
87 AssertingVH
<PHINode
> P3
= B
.CreatePHI(Type::getInt32Ty(C
), 2);
88 P3
->addIncoming(B
.getInt32(42), Entry
);
89 P3
->addIncoming(B
.getInt32(23), BB
);
91 PHINode
*P4
= B
.CreatePHI(Type::getInt32Ty(C
), 2);
92 P4
->addIncoming(B
.getInt32(42), Entry
);
93 P4
->addIncoming(B
.getInt32(23), BB
);
95 P1
->addIncoming(P3
, BB
);
96 P2
->addIncoming(P4
, BB
);
97 BranchInst::Create(BB
, BB
);
99 // Verify that we can eliminate PHIs that become duplicates after chaning PHIs
101 EXPECT_TRUE(EliminateDuplicatePHINodes(BB
));
102 EXPECT_EQ(3U, BB
->size());
105 static std::unique_ptr
<Module
> parseIR(LLVMContext
&C
, const char *IR
) {
107 std::unique_ptr
<Module
> Mod
= parseAssemblyString(IR
, Err
, C
);
109 Err
.print("UtilsTests", errs());
113 TEST(Local
, ReplaceDbgDeclare
) {
116 // Original C source to get debug info for a local variable:
117 // void f() { int x; }
118 std::unique_ptr
<Module
> M
= parseIR(C
,
120 define void @f() !dbg !8 {
122 %x = alloca i32, align 4
123 call void @llvm.dbg.declare(metadata i32* %x, metadata !11, metadata !DIExpression()), !dbg !13
124 call void @llvm.dbg.declare(metadata i32* %x, metadata !11, metadata !DIExpression()), !dbg !13
127 declare void @llvm.dbg.declare(metadata, metadata, metadata)
129 !llvm.module.flags = !{!3, !4}
130 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version
6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
131 !1 = !DIFile(filename: "t2
.c
", directory: "foo
")
133 !3 = !{i32 2, !"Dwarf Version
", i32 4}
134 !4 = !{i32 2, !"Debug Info Version
", i32 3}
135 !8 = distinct !DISubprogram(name: "f
", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2)
136 !9 = !DISubroutineType(types: !10)
138 !11 = !DILocalVariable(name: "x
", scope: !8, file: !1, line: 2, type: !12)
139 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
140 !13 = !DILocation(line: 2, column: 7, scope: !8)
141 !14 = !DILocation(line: 3, column: 1, scope: !8)
143 auto *GV
= M
->getNamedValue("f");
145 auto *F
= dyn_cast
<Function
>(GV
);
147 Instruction
*Inst
= &F
->front().front();
148 auto *AI
= dyn_cast
<AllocaInst
>(Inst
);
150 Inst
= Inst
->getNextNode()->getNextNode();
152 auto *DII
= dyn_cast
<DbgDeclareInst
>(Inst
);
154 Value
*NewBase
= Constant::getNullValue(Type::getInt32PtrTy(C
));
156 replaceDbgDeclare(AI
, NewBase
, DII
, DIB
, DIExpression::NoDeref
, 0,
157 DIExpression::NoDeref
);
159 // There should be exactly two dbg.declares.
161 for (const Instruction
&I
: F
->front())
162 if (isa
<DbgDeclareInst
>(I
))
164 EXPECT_EQ(2, Declares
);
167 /// Build the dominator tree for the function and run the Test.
168 static void runWithDomTree(
169 Module
&M
, StringRef FuncName
,
170 function_ref
<void(Function
&F
, DominatorTree
*DT
)> Test
) {
171 auto *F
= M
.getFunction(FuncName
);
172 ASSERT_NE(F
, nullptr) << "Could not find " << FuncName
;
173 // Compute the dominator tree for the function.
174 DominatorTree
DT(*F
);
178 TEST(Local
, MergeBasicBlockIntoOnlyPred
) {
180 std::unique_ptr
<Module
> M
;
181 auto resetIR
= [&]() {
184 define i32 @f(i8* %str) {
187 bb2.i: ; preds = %bb4.i, %entry
188 br i1 false, label %bb4.i, label %base2flt.exit204
189 bb4.i: ; preds = %bb2.i
190 br i1 false, label %base2flt.exit204, label %bb2.i
191 bb10.i196.bb7.i197_crit_edge: ; No predecessors!
193 bb7.i197: ; preds = %bb10.i196.bb7.i197_crit_edge
194 %.reg2mem.0 = phi i32 [ %.reg2mem.0, %bb10.i196.bb7.i197_crit_edge ]
195 br i1 undef, label %base2flt.exit204, label %base2flt.exit204
196 base2flt.exit204: ; preds = %bb7.i197, %bb7.i197, %bb2.i, %bb4.i
202 auto resetIRReplaceEntry
= [&]() {
208 bb2.i: ; preds = %entry
214 auto Test
= [&](Function
&F
, DomTreeUpdater
&DTU
) {
215 for (Function::iterator I
= F
.begin(), E
= F
.end(); I
!= E
;) {
216 BasicBlock
*BB
= &*I
++;
217 BasicBlock
*SinglePred
= BB
->getSinglePredecessor();
218 if (!SinglePred
|| SinglePred
== BB
|| BB
->hasAddressTaken())
220 BranchInst
*Term
= dyn_cast
<BranchInst
>(SinglePred
->getTerminator());
221 if (Term
&& !Term
->isConditional())
222 MergeBasicBlockIntoOnlyPred(BB
, &DTU
);
224 if (DTU
.hasDomTree()) {
225 EXPECT_TRUE(DTU
.getDomTree().verify());
227 if (DTU
.hasPostDomTree()) {
228 EXPECT_TRUE(DTU
.getPostDomTree().verify());
232 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with
235 runWithDomTree(*M
, "f", [&](Function
&F
, DominatorTree
*DT
) {
236 PostDominatorTree PDT
= PostDominatorTree(F
);
237 DomTreeUpdater
DTU(*DT
, PDT
, DomTreeUpdater::UpdateStrategy::Eager
);
241 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with
244 runWithDomTree(*M
, "f", [&](Function
&F
, DominatorTree
*DT
) {
245 DomTreeUpdater
DTU(*DT
, DomTreeUpdater::UpdateStrategy::Eager
);
249 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with
252 runWithDomTree(*M
, "f", [&](Function
&F
, DominatorTree
*DT
) {
253 PostDominatorTree PDT
= PostDominatorTree(F
);
254 DomTreeUpdater
DTU(PDT
, DomTreeUpdater::UpdateStrategy::Eager
);
258 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with
261 runWithDomTree(*M
, "f", [&](Function
&F
, DominatorTree
*DT
) {
262 PostDominatorTree PDT
= PostDominatorTree(F
);
263 DomTreeUpdater
DTU(*DT
, PDT
, DomTreeUpdater::UpdateStrategy::Lazy
);
267 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with
270 runWithDomTree(*M
, "f", [&](Function
&F
, DominatorTree
*DT
) {
271 PostDominatorTree PDT
= PostDominatorTree(F
);
272 DomTreeUpdater
DTU(PDT
, DomTreeUpdater::UpdateStrategy::Lazy
);
276 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with DT.
278 runWithDomTree(*M
, "f", [&](Function
&F
, DominatorTree
*DT
) {
279 DomTreeUpdater
DTU(*DT
, DomTreeUpdater::UpdateStrategy::Lazy
);
283 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with
285 resetIRReplaceEntry();
286 runWithDomTree(*M
, "f", [&](Function
&F
, DominatorTree
*DT
) {
287 PostDominatorTree PDT
= PostDominatorTree(F
);
288 DomTreeUpdater
DTU(*DT
, PDT
, DomTreeUpdater::UpdateStrategy::Eager
);
292 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with
294 resetIRReplaceEntry();
295 runWithDomTree(*M
, "f", [&](Function
&F
, DominatorTree
*DT
) {
296 DomTreeUpdater
DTU(*DT
, DomTreeUpdater::UpdateStrategy::Eager
);
300 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with
302 resetIRReplaceEntry();
303 runWithDomTree(*M
, "f", [&](Function
&F
, DominatorTree
*DT
) {
304 PostDominatorTree PDT
= PostDominatorTree(F
);
305 DomTreeUpdater
DTU(PDT
, DomTreeUpdater::UpdateStrategy::Eager
);
309 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with
311 resetIRReplaceEntry();
312 runWithDomTree(*M
, "f", [&](Function
&F
, DominatorTree
*DT
) {
313 PostDominatorTree PDT
= PostDominatorTree(F
);
314 DomTreeUpdater
DTU(*DT
, PDT
, DomTreeUpdater::UpdateStrategy::Lazy
);
318 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with
320 resetIRReplaceEntry();
321 runWithDomTree(*M
, "f", [&](Function
&F
, DominatorTree
*DT
) {
322 PostDominatorTree PDT
= PostDominatorTree(F
);
323 DomTreeUpdater
DTU(PDT
, DomTreeUpdater::UpdateStrategy::Lazy
);
327 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with DT.
328 resetIRReplaceEntry();
329 runWithDomTree(*M
, "f", [&](Function
&F
, DominatorTree
*DT
) {
330 DomTreeUpdater
DTU(*DT
, DomTreeUpdater::UpdateStrategy::Lazy
);
335 TEST(Local
, ConstantFoldTerminator
) {
338 std::unique_ptr
<Module
> M
= parseIR(C
,
340 define void @br_same_dest() {
342 br i1 false, label %bb0, label %bb0
347 define void @br_different_dest() {
349 br i1 true, label %bb0, label %bb1
358 define void @switch_2_different_dest() {
360 switch i32 0, label %default [ i32 0, label %bb0 ]
366 define void @switch_2_different_dest_default() {
368 switch i32 1, label %default [ i32 0, label %bb0 ]
374 define void @switch_3_different_dest() {
376 switch i32 0, label %default [ i32 0, label %bb0
386 define void @switch_variable_2_default_dest(i32 %arg) {
388 switch i32 %arg, label %default [ i32 0, label %default ]
393 define void @switch_constant_2_default_dest() {
395 switch i32 1, label %default [ i32 0, label %default ]
400 define void @switch_constant_3_repeated_dest() {
402 switch i32 0, label %default [ i32 0, label %bb0
410 define void @indirectbr() {
412 indirectbr i8* blockaddress(@indirectbr, %bb0), [label %bb0, label %bb1]
419 define void @indirectbr_repeated() {
421 indirectbr i8* blockaddress(@indirectbr_repeated, %bb0), [label %bb0, label %bb0]
426 define void @indirectbr_unreachable() {
428 indirectbr i8* blockaddress(@indirectbr_unreachable, %bb0), [label %bb1]
436 auto CFAllTerminatorsEager
= [&](Function
&F
, DominatorTree
*DT
) {
437 PostDominatorTree PDT
= PostDominatorTree(F
);
438 DomTreeUpdater
DTU(*DT
, PDT
, DomTreeUpdater::UpdateStrategy::Eager
);
439 for (Function::iterator I
= F
.begin(), E
= F
.end(); I
!= E
;) {
440 BasicBlock
*BB
= &*I
++;
441 ConstantFoldTerminator(BB
, true, nullptr, &DTU
);
444 EXPECT_TRUE(DTU
.getDomTree().verify());
445 EXPECT_TRUE(DTU
.getPostDomTree().verify());
448 auto CFAllTerminatorsLazy
= [&](Function
&F
, DominatorTree
*DT
) {
449 PostDominatorTree PDT
= PostDominatorTree(F
);
450 DomTreeUpdater
DTU(*DT
, PDT
, DomTreeUpdater::UpdateStrategy::Lazy
);
451 for (Function::iterator I
= F
.begin(), E
= F
.end(); I
!= E
;) {
452 BasicBlock
*BB
= &*I
++;
453 ConstantFoldTerminator(BB
, true, nullptr, &DTU
);
456 EXPECT_TRUE(DTU
.getDomTree().verify());
457 EXPECT_TRUE(DTU
.getPostDomTree().verify());
460 // Test ConstantFoldTerminator under Eager UpdateStrategy.
461 runWithDomTree(*M
, "br_same_dest", CFAllTerminatorsEager
);
462 runWithDomTree(*M
, "br_different_dest", CFAllTerminatorsEager
);
463 runWithDomTree(*M
, "switch_2_different_dest", CFAllTerminatorsEager
);
464 runWithDomTree(*M
, "switch_2_different_dest_default", CFAllTerminatorsEager
);
465 runWithDomTree(*M
, "switch_3_different_dest", CFAllTerminatorsEager
);
466 runWithDomTree(*M
, "switch_variable_2_default_dest", CFAllTerminatorsEager
);
467 runWithDomTree(*M
, "switch_constant_2_default_dest", CFAllTerminatorsEager
);
468 runWithDomTree(*M
, "switch_constant_3_repeated_dest", CFAllTerminatorsEager
);
469 runWithDomTree(*M
, "indirectbr", CFAllTerminatorsEager
);
470 runWithDomTree(*M
, "indirectbr_repeated", CFAllTerminatorsEager
);
471 runWithDomTree(*M
, "indirectbr_unreachable", CFAllTerminatorsEager
);
473 // Test ConstantFoldTerminator under Lazy UpdateStrategy.
474 runWithDomTree(*M
, "br_same_dest", CFAllTerminatorsLazy
);
475 runWithDomTree(*M
, "br_different_dest", CFAllTerminatorsLazy
);
476 runWithDomTree(*M
, "switch_2_different_dest", CFAllTerminatorsLazy
);
477 runWithDomTree(*M
, "switch_2_different_dest_default", CFAllTerminatorsLazy
);
478 runWithDomTree(*M
, "switch_3_different_dest", CFAllTerminatorsLazy
);
479 runWithDomTree(*M
, "switch_variable_2_default_dest", CFAllTerminatorsLazy
);
480 runWithDomTree(*M
, "switch_constant_2_default_dest", CFAllTerminatorsLazy
);
481 runWithDomTree(*M
, "switch_constant_3_repeated_dest", CFAllTerminatorsLazy
);
482 runWithDomTree(*M
, "indirectbr", CFAllTerminatorsLazy
);
483 runWithDomTree(*M
, "indirectbr_repeated", CFAllTerminatorsLazy
);
484 runWithDomTree(*M
, "indirectbr_unreachable", CFAllTerminatorsLazy
);
487 struct SalvageDebugInfoTest
: ::testing::Test
{
489 std::unique_ptr
<Module
> M
;
490 Function
*F
= nullptr;
495 define void @f() !dbg !8 {
499 call void @llvm.dbg.value(metadata i32 %x, metadata !11, metadata !DIExpression()), !dbg !13
500 call void @llvm.dbg.value(metadata i32 %y, metadata !11, metadata !DIExpression()), !dbg !13
503 declare void @llvm.dbg.value(metadata, metadata, metadata)
505 !llvm.module.flags = !{!3, !4}
506 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version
6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
507 !1 = !DIFile(filename: "t2
.c
", directory: "foo
")
509 !3 = !{i32 2, !"Dwarf Version
", i32 4}
510 !4 = !{i32 2, !"Debug Info Version
", i32 3}
511 !8 = distinct !DISubprogram(name: "f
", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2)
512 !9 = !DISubroutineType(types: !10)
514 !11 = !DILocalVariable(name: "x
", scope: !8, file: !1, line: 2, type: !12)
515 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
516 !13 = !DILocation(line: 2, column: 7, scope: !8)
517 !14 = !DILocation(line: 3, column: 1, scope: !8)
520 auto *GV
= M
->getNamedValue("f");
522 F
= dyn_cast
<Function
>(GV
);
526 bool doesDebugValueDescribeX(const DbgValueInst
&DI
) {
527 const auto &CI
= *cast
<ConstantInt
>(DI
.getValue());
529 return DI
.getExpression()->getElements().equals(
530 {dwarf::DW_OP_plus_uconst
, 1, dwarf::DW_OP_stack_value
});
531 else if (CI
.isOneValue())
532 return DI
.getExpression()->getElements().empty();
536 bool doesDebugValueDescribeY(const DbgValueInst
&DI
) {
537 const auto &CI
= *cast
<ConstantInt
>(DI
.getValue());
539 return DI
.getExpression()->getElements().equals(
540 {dwarf::DW_OP_plus_uconst
, 1, dwarf::DW_OP_plus_uconst
, 2,
541 dwarf::DW_OP_stack_value
});
542 else if (CI
.isOneValue())
543 return DI
.getExpression()->getElements().equals(
544 {dwarf::DW_OP_plus_uconst
, 2, dwarf::DW_OP_stack_value
});
548 void verifyDebugValuesAreSalvaged() {
549 // Check that the debug values for %x and %y are preserved.
552 for (const Instruction
&I
: F
->front()) {
553 auto DI
= dyn_cast
<DbgValueInst
>(&I
);
555 // The function should only contain debug values and a terminator.
556 ASSERT_TRUE(I
.isTerminator());
559 EXPECT_EQ(DI
->getVariable()->getName(), "x");
560 FoundX
|= doesDebugValueDescribeX(*DI
);
561 FoundY
|= doesDebugValueDescribeY(*DI
);
568 TEST_F(SalvageDebugInfoTest
, RecursiveInstDeletion
) {
569 Instruction
*Inst
= &F
->front().front();
570 Inst
= Inst
->getNextNode(); // Get %y = add ...
572 bool Deleted
= RecursivelyDeleteTriviallyDeadInstructions(Inst
);
573 ASSERT_TRUE(Deleted
);
574 verifyDebugValuesAreSalvaged();
577 TEST_F(SalvageDebugInfoTest
, RecursiveBlockSimplification
) {
578 BasicBlock
*BB
= &F
->front();
580 bool Deleted
= SimplifyInstructionsInBlock(BB
);
581 ASSERT_TRUE(Deleted
);
582 verifyDebugValuesAreSalvaged();
585 TEST(Local
, ChangeToUnreachable
) {
588 std::unique_ptr
<Module
> M
= parseIR(Ctx
,
590 define internal void @foo() !dbg !6 {
596 !llvm.debugify = !{!3, !4}
597 !llvm.module.flags = !{!5}
599 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify
", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
600 !1 = !DIFile(filename: "test
.ll
", directory: "/")
604 !5 = !{i32 2, !"Debug Info Version
", i32 3}
605 !6 = distinct !DISubprogram(name: "foo
", linkageName: "foo
", scope: null, file: !1, line: 1, type: !7, isLocal: true, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !0, retainedNodes: !2)
606 !7 = !DISubroutineType(types: !2)
607 !8 = !DILocation(line: 1, column: 1, scope: !6)
610 bool BrokenDebugInfo
= true;
611 verifyModule(*M
, &errs(), &BrokenDebugInfo
);
612 ASSERT_FALSE(BrokenDebugInfo
);
614 Function
&F
= *cast
<Function
>(M
->getNamedValue("foo"));
616 BasicBlock
&BB
= F
.front();
617 Instruction
&A
= BB
.front();
618 DebugLoc DLA
= A
.getDebugLoc();
620 ASSERT_TRUE(isa
<ReturnInst
>(&A
));
621 // One instruction should be affected.
622 EXPECT_EQ(changeToUnreachable(&A
, /*UseLLVMTrap*/false), 1U);
624 Instruction
&B
= BB
.front();
626 // There should be an uncreachable instruction.
627 ASSERT_TRUE(isa
<UnreachableInst
>(&B
));
629 DebugLoc DLB
= B
.getDebugLoc();
633 TEST(Local
, ReplaceAllDbgUsesWith
) {
634 using namespace llvm::dwarf
;
638 // Note: The datalayout simulates Darwin/x86_64.
639 std::unique_ptr
<Module
> M
= parseIR(Ctx
,
641 target datalayout = "e
-m
:o
-i63
:64-f80
:128-n8
:16:32:64-S128
"
643 declare i32 @escape(i32)
645 define void @f() !dbg !6 {
647 %a = add i32 0, 1, !dbg !15
648 call void @llvm.dbg.value(metadata i32 %a, metadata !9, metadata !DIExpression()), !dbg !15
650 %b = add i64 0, 1, !dbg !16
651 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression()), !dbg !16
652 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul)), !dbg !16
653 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_stack_value)), !dbg !16
654 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 8)), !dbg !16
655 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_LLVM_fragment, 0, 8)), !dbg !16
656 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8)), !dbg !16
658 %c = inttoptr i64 0 to i64*, !dbg !17
659 call void @llvm.dbg.declare(metadata i64* %c, metadata !13, metadata !DIExpression()), !dbg !17
661 %d = inttoptr i64 0 to i32*, !dbg !18
662 call void @llvm.dbg.addr(metadata i32* %d, metadata !20, metadata !DIExpression()), !dbg !18
664 %e = add <2 x i16> zeroinitializer, zeroinitializer
665 call void @llvm.dbg.value(metadata <2 x i16> %e, metadata !14, metadata !DIExpression()), !dbg !18
667 %f = call i32 @escape(i32 0)
668 call void @llvm.dbg.value(metadata i32 %f, metadata !9, metadata !DIExpression()), !dbg !15
670 %barrier = call i32 @escape(i32 0)
672 %g = call i32 @escape(i32 %f)
673 call void @llvm.dbg.value(metadata i32 %g, metadata !9, metadata !DIExpression()), !dbg !15
678 declare void @llvm.dbg.addr(metadata, metadata, metadata)
679 declare void @llvm.dbg.declare(metadata, metadata, metadata)
680 declare void @llvm.dbg.value(metadata, metadata, metadata)
683 !llvm.module.flags = !{!5}
685 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify
", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
686 !1 = !DIFile(filename: "/Users
/vsk
/Desktop
/foo
.ll
", directory: "/")
688 !5 = !{i32 2, !"Debug Info Version
", i32 3}
689 !6 = distinct !DISubprogram(name: "f
", linkageName: "f
", scope: null, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !0, retainedNodes: !8)
690 !7 = !DISubroutineType(types: !2)
691 !8 = !{!9, !11, !13, !14}
692 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
693 !10 = !DIBasicType(name: "ty32
", size: 32, encoding: DW_ATE_signed)
694 !11 = !DILocalVariable(name: "2", scope: !6, file: !1, line: 2, type: !12)
695 !12 = !DIBasicType(name: "ty64
", size: 64, encoding: DW_ATE_signed)
696 !13 = !DILocalVariable(name: "3", scope: !6, file: !1, line: 3, type: !12)
697 !14 = !DILocalVariable(name: "4", scope: !6, file: !1, line: 4, type: !10)
698 !15 = !DILocation(line: 1, column: 1, scope: !6)
699 !16 = !DILocation(line: 2, column: 1, scope: !6)
700 !17 = !DILocation(line: 3, column: 1, scope: !6)
701 !18 = !DILocation(line: 4, column: 1, scope: !6)
702 !19 = !DILocation(line: 5, column: 1, scope: !6)
703 !20 = !DILocalVariable(name: "5", scope: !6, file: !1, line: 5, type: !10)
706 bool BrokenDebugInfo
= true;
707 verifyModule(*M
, &errs(), &BrokenDebugInfo
);
708 ASSERT_FALSE(BrokenDebugInfo
);
710 Function
&F
= *cast
<Function
>(M
->getNamedValue("f"));
713 BasicBlock
&BB
= F
.front();
714 Instruction
&A
= BB
.front();
715 Instruction
&B
= *A
.getNextNonDebugInstruction();
716 Instruction
&C
= *B
.getNextNonDebugInstruction();
717 Instruction
&D
= *C
.getNextNonDebugInstruction();
718 Instruction
&E
= *D
.getNextNonDebugInstruction();
719 Instruction
&F_
= *E
.getNextNonDebugInstruction();
720 Instruction
&Barrier
= *F_
.getNextNonDebugInstruction();
721 Instruction
&G
= *Barrier
.getNextNonDebugInstruction();
723 // Simulate i32 <-> i64* conversion. Expect no updates: the datalayout says
724 // pointers are 64 bits, so the conversion would be lossy.
725 EXPECT_FALSE(replaceAllDbgUsesWith(A
, C
, C
, DT
));
726 EXPECT_FALSE(replaceAllDbgUsesWith(C
, A
, A
, DT
));
728 // Simulate i32 <-> <2 x i16> conversion. This is unsupported.
729 EXPECT_FALSE(replaceAllDbgUsesWith(E
, A
, A
, DT
));
730 EXPECT_FALSE(replaceAllDbgUsesWith(A
, E
, E
, DT
));
732 // Simulate i32* <-> i64* conversion.
733 EXPECT_TRUE(replaceAllDbgUsesWith(D
, C
, C
, DT
));
735 SmallVector
<DbgVariableIntrinsic
*, 2> CDbgVals
;
736 findDbgUsers(CDbgVals
, &C
);
737 EXPECT_EQ(2U, CDbgVals
.size());
738 EXPECT_TRUE(any_of(CDbgVals
, [](DbgVariableIntrinsic
*DII
) {
739 return isa
<DbgAddrIntrinsic
>(DII
);
741 EXPECT_TRUE(any_of(CDbgVals
, [](DbgVariableIntrinsic
*DII
) {
742 return isa
<DbgDeclareInst
>(DII
);
745 EXPECT_TRUE(replaceAllDbgUsesWith(C
, D
, D
, DT
));
747 SmallVector
<DbgVariableIntrinsic
*, 2> DDbgVals
;
748 findDbgUsers(DDbgVals
, &D
);
749 EXPECT_EQ(2U, DDbgVals
.size());
750 EXPECT_TRUE(any_of(DDbgVals
, [](DbgVariableIntrinsic
*DII
) {
751 return isa
<DbgAddrIntrinsic
>(DII
);
753 EXPECT_TRUE(any_of(DDbgVals
, [](DbgVariableIntrinsic
*DII
) {
754 return isa
<DbgDeclareInst
>(DII
);
757 // Introduce a use-before-def. Check that the dbg.value for %a is salvaged.
758 EXPECT_TRUE(replaceAllDbgUsesWith(A
, F_
, F_
, DT
));
760 auto *ADbgVal
= cast
<DbgValueInst
>(A
.getNextNode());
761 EXPECT_EQ(ConstantInt::get(A
.getType(), 0), ADbgVal
->getVariableLocation());
763 // Introduce a use-before-def. Check that the dbg.values for %f are deleted.
764 EXPECT_TRUE(replaceAllDbgUsesWith(F_
, G
, G
, DT
));
766 SmallVector
<DbgValueInst
*, 1> FDbgVals
;
767 findDbgValues(FDbgVals
, &F
);
768 EXPECT_EQ(0U, FDbgVals
.size());
770 // Simulate i32 -> i64 conversion to test sign-extension. Here are some
771 // interesting cases to handle:
772 // 1) debug user has empty DIExpression
773 // 2) debug user has non-empty, non-stack-value'd DIExpression
774 // 3) debug user has non-empty, stack-value'd DIExpression
775 // 4-6) like (1-3), but with a fragment
776 EXPECT_TRUE(replaceAllDbgUsesWith(B
, A
, A
, DT
));
778 SmallVector
<DbgValueInst
*, 8> ADbgVals
;
779 findDbgValues(ADbgVals
, &A
);
780 EXPECT_EQ(6U, ADbgVals
.size());
782 // Check that %a has a dbg.value with a DIExpression matching \p Ops.
783 auto hasADbgVal
= [&](ArrayRef
<uint64_t> Ops
) {
784 return any_of(ADbgVals
, [&](DbgValueInst
*DVI
) {
785 assert(DVI
->getVariable()->getName() == "2");
786 return DVI
->getExpression()->getElements() == Ops
;
790 // Case 1: The original expr is empty, so no deref is needed.
791 EXPECT_TRUE(hasADbgVal({DW_OP_dup
, DW_OP_constu
, 31, DW_OP_shr
, DW_OP_lit0
,
792 DW_OP_not
, DW_OP_mul
, DW_OP_or
, DW_OP_stack_value
}));
794 // Case 2: Perform an address calculation with the original expr, deref it,
795 // then sign-extend the result.
796 EXPECT_TRUE(hasADbgVal({DW_OP_lit0
, DW_OP_mul
, DW_OP_deref
, DW_OP_dup
,
797 DW_OP_constu
, 31, DW_OP_shr
, DW_OP_lit0
, DW_OP_not
,
798 DW_OP_mul
, DW_OP_or
, DW_OP_stack_value
}));
800 // Case 3: Insert the sign-extension logic before the DW_OP_stack_value.
801 EXPECT_TRUE(hasADbgVal({DW_OP_lit0
, DW_OP_mul
, DW_OP_dup
, DW_OP_constu
, 31,
802 DW_OP_shr
, DW_OP_lit0
, DW_OP_not
, DW_OP_mul
, DW_OP_or
,
803 DW_OP_stack_value
}));
805 // Cases 4-6: Just like cases 1-3, but preserve the fragment at the end.
806 EXPECT_TRUE(hasADbgVal({DW_OP_dup
, DW_OP_constu
, 31, DW_OP_shr
, DW_OP_lit0
,
807 DW_OP_not
, DW_OP_mul
, DW_OP_or
, DW_OP_stack_value
,
808 DW_OP_LLVM_fragment
, 0, 8}));
810 hasADbgVal({DW_OP_lit0
, DW_OP_mul
, DW_OP_deref
, DW_OP_dup
, DW_OP_constu
,
811 31, DW_OP_shr
, DW_OP_lit0
, DW_OP_not
, DW_OP_mul
, DW_OP_or
,
812 DW_OP_stack_value
, DW_OP_LLVM_fragment
, 0, 8}));
813 EXPECT_TRUE(hasADbgVal({DW_OP_lit0
, DW_OP_mul
, DW_OP_dup
, DW_OP_constu
, 31,
814 DW_OP_shr
, DW_OP_lit0
, DW_OP_not
, DW_OP_mul
, DW_OP_or
,
815 DW_OP_stack_value
, DW_OP_LLVM_fragment
, 0, 8}));
817 verifyModule(*M
, &errs(), &BrokenDebugInfo
);
818 ASSERT_FALSE(BrokenDebugInfo
);
821 TEST(Local
, RemoveUnreachableBlocks
) {
824 std::unique_ptr
<Module
> M
= parseIR(C
,
826 define void @br_simple() {
835 define void @br_self_loop() {
839 br i1 true, label %bb1, label %bb0
841 br i1 true, label %bb0, label %bb2
846 define void @br_constant() {
850 br i1 true, label %bb1, label %bb2
852 br i1 true, label %bb0, label %bb2
857 define void @br_loop() {
869 auto runEager
= [&](Function
&F
, DominatorTree
*DT
) {
870 PostDominatorTree PDT
= PostDominatorTree(F
);
871 DomTreeUpdater
DTU(*DT
, PDT
, DomTreeUpdater::UpdateStrategy::Eager
);
872 removeUnreachableBlocks(F
, nullptr, &DTU
);
873 EXPECT_TRUE(DTU
.getDomTree().verify());
874 EXPECT_TRUE(DTU
.getPostDomTree().verify());
877 auto runLazy
= [&](Function
&F
, DominatorTree
*DT
) {
878 PostDominatorTree PDT
= PostDominatorTree(F
);
879 DomTreeUpdater
DTU(*DT
, PDT
, DomTreeUpdater::UpdateStrategy::Lazy
);
880 removeUnreachableBlocks(F
, nullptr, &DTU
);
881 EXPECT_TRUE(DTU
.getDomTree().verify());
882 EXPECT_TRUE(DTU
.getPostDomTree().verify());
885 // Test removeUnreachableBlocks under Eager UpdateStrategy.
886 runWithDomTree(*M
, "br_simple", runEager
);
887 runWithDomTree(*M
, "br_self_loop", runEager
);
888 runWithDomTree(*M
, "br_constant", runEager
);
889 runWithDomTree(*M
, "br_loop", runEager
);
891 // Test removeUnreachableBlocks under Lazy UpdateStrategy.
892 runWithDomTree(*M
, "br_simple", runLazy
);
893 runWithDomTree(*M
, "br_self_loop", runLazy
);
894 runWithDomTree(*M
, "br_constant", runLazy
);
895 runWithDomTree(*M
, "br_loop", runLazy
);
907 auto checkRUBlocksRetVal
= [&](Function
&F
, DominatorTree
*DT
) {
908 DomTreeUpdater
DTU(DT
, DomTreeUpdater::UpdateStrategy::Lazy
);
909 EXPECT_TRUE(removeUnreachableBlocks(F
, nullptr, &DTU
));
910 EXPECT_FALSE(removeUnreachableBlocks(F
, nullptr, &DTU
));
911 EXPECT_TRUE(DTU
.getDomTree().verify());
914 runWithDomTree(*M
, "f", checkRUBlocksRetVal
);