1 //===------------- llvm/unittest/CodeGen/InstrRefLDVTest.cpp --------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/CodeGen/MachineModuleInfo.h"
10 #include "llvm/CodeGen/TargetLowering.h"
11 #include "llvm/CodeGen/TargetSubtargetInfo.h"
12 #include "llvm/IR/DIBuilder.h"
13 #include "llvm/IR/DebugInfoMetadata.h"
14 #include "llvm/IR/IRBuilder.h"
15 #include "llvm/MC/TargetRegistry.h"
16 #include "llvm/Support/TargetSelect.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Target/TargetOptions.h"
19 
20 #include "../lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h"
21 
22 #include "gtest/gtest.h"
23 
24 using namespace llvm;
25 using namespace LiveDebugValues;
26 
27 // Include helper functions to ease the manipulation of MachineFunctions
28 #include "MFCommon.inc"
29 
30 class InstrRefLDVTest : public testing::Test {
31 public:
32   LLVMContext Ctx;
33   Module Mod;
34   std::unique_ptr<MachineFunction> MF;
35   DICompileUnit *OurCU;
36   DIFile *OurFile;
37   DISubprogram *OurFunc;
38   DILexicalBlock *OurBlock, *AnotherBlock;
39   DISubprogram *ToInlineFunc;
40   DILexicalBlock *ToInlineBlock;
41 
42   DebugLoc OutermostLoc, InBlockLoc, NotNestedBlockLoc, InlinedLoc;
43 
44   MachineBasicBlock *MBB1, *MBB2, *MBB3, *MBB4;
45 
46   InstrRefLDVTest() : Ctx(), Mod("beehives", Ctx) {
47     // Boilerplate that creates a MachineFunction and associated blocks.
48     MF = createMachineFunction(Ctx, Mod);
49     llvm::Function &F = const_cast<llvm::Function &>(MF->getFunction());
50     auto BB1 = BasicBlock::Create(Ctx, "a", &F);
51     auto BB2 = BasicBlock::Create(Ctx, "b", &F);
52     auto BB3 = BasicBlock::Create(Ctx, "c", &F);
53     auto BB4 = BasicBlock::Create(Ctx, "d", &F);
54     IRBuilder<> IRB1(BB1), IRB2(BB2), IRB3(BB3), IRB4(BB4);
55     IRB1.CreateBr(BB2);
56     IRB2.CreateBr(BB3);
57     IRB3.CreateBr(BB4);
58     IRB4.CreateRetVoid();
59     MBB1 = MF->CreateMachineBasicBlock(BB1);
60     MF->insert(MF->end(), MBB1);
61     MBB2 = MF->CreateMachineBasicBlock(BB2);
62     MF->insert(MF->end(), MBB2);
63     MBB3 = MF->CreateMachineBasicBlock(BB3);
64     MF->insert(MF->end(), MBB3);
65     MBB4 = MF->CreateMachineBasicBlock(BB4);
66     MF->insert(MF->end(), MBB4);
67     MBB1->addSuccessor(MBB2);
68     MBB1->addSuccessor(MBB3);
69     MBB2->addSuccessor(MBB4);
70     MBB3->addSuccessor(MBB4);
71 
72     // Create metadata: CU, subprogram, some blocks and an inline function
73     // scope.
74     DIBuilder DIB(Mod);
75     OurFile = DIB.createFile("xyzzy.c", "/cave");
76     OurCU =
77         DIB.createCompileUnit(dwarf::DW_LANG_C99, OurFile, "nou", false, "", 0);
78     auto OurSubT = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
79     OurFunc =
80         DIB.createFunction(OurCU, "bees", "", OurFile, 1, OurSubT, 1,
81                            DINode::FlagZero, DISubprogram::SPFlagDefinition);
82     F.setSubprogram(OurFunc);
83     OurBlock = DIB.createLexicalBlock(OurFunc, OurFile, 2, 3);
84     AnotherBlock = DIB.createLexicalBlock(OurFunc, OurFile, 2, 6);
85     ToInlineFunc =
86         DIB.createFunction(OurFile, "shoes", "", OurFile, 10, OurSubT, 10,
87                            DINode::FlagZero, DISubprogram::SPFlagDefinition);
88 
89     // Make some nested scopes.
90     OutermostLoc = DILocation::get(Ctx, 3, 1, OurFunc);
91     InBlockLoc = DILocation::get(Ctx, 4, 1, OurBlock);
92     InlinedLoc = DILocation::get(Ctx, 10, 1, ToInlineFunc, InBlockLoc.get());
93 
94     // Make a scope that isn't nested within the others.
95     NotNestedBlockLoc = DILocation::get(Ctx, 4, 1, AnotherBlock);
96 
97     DIB.finalize();
98   }
99 };
100