1 | //===- MachineBasicBlockTest.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/MachineBasicBlock.h" |
10 | #include "llvm/CodeGen/MachineFunction.h" |
11 | #include "llvm/CodeGen/MachineInstr.h" |
12 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
13 | #include "llvm/CodeGen/MachineModuleInfo.h" |
14 | #include "llvm/CodeGen/TargetFrameLowering.h" |
15 | #include "llvm/CodeGen/TargetInstrInfo.h" |
16 | #include "llvm/CodeGen/TargetLowering.h" |
17 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
18 | #include "llvm/IR/DIBuilder.h" |
19 | #include "llvm/IR/DebugInfoMetadata.h" |
20 | #include "llvm/IR/IRBuilder.h" |
21 | #include "llvm/MC/TargetRegistry.h" |
22 | #include "llvm/Target/TargetMachine.h" |
23 | #include "gmock/gmock.h" |
24 | #include "gtest/gtest.h" |
25 | |
26 | using namespace llvm; |
27 | |
28 | namespace { |
29 | // Include helper functions to ease the manipulation of MachineFunctions. |
30 | #include "MFCommon.inc" |
31 | |
32 | TEST(FindDebugLocTest, DifferentIterators) { |
33 | LLVMContext Ctx; |
34 | Module Mod("Module" , Ctx); |
35 | auto MF = createMachineFunction(Ctx, M&: Mod); |
36 | auto &MBB = *MF->CreateMachineBasicBlock(); |
37 | |
38 | // Create metadata: CU, subprogram, some blocks and an inline function |
39 | // scope. |
40 | DIBuilder DIB(Mod); |
41 | DIFile *OurFile = DIB.createFile(Filename: "foo.c" , Directory: "/bar" ); |
42 | DICompileUnit *OurCU = |
43 | DIB.createCompileUnit(Lang: dwarf::DW_LANG_C99, File: OurFile, Producer: "" , isOptimized: false, Flags: "" , RV: 0); |
44 | auto OurSubT = |
45 | DIB.createSubroutineType(ParameterTypes: DIB.getOrCreateTypeArray(Elements: std::nullopt)); |
46 | DISubprogram *OurFunc = |
47 | DIB.createFunction(Scope: OurCU, Name: "bees" , LinkageName: "" , File: OurFile, LineNo: 1, Ty: OurSubT, ScopeLine: 1, |
48 | Flags: DINode::FlagZero, SPFlags: DISubprogram::SPFlagDefinition); |
49 | |
50 | DebugLoc DL0; |
51 | DebugLoc DL1 = DILocation::get(Context&: Ctx, Line: 1, Column: 0, Scope: OurFunc); |
52 | DebugLoc DL2 = DILocation::get(Context&: Ctx, Line: 2, Column: 0, Scope: OurFunc); |
53 | DebugLoc DL3 = DILocation::get(Context&: Ctx, Line: 3, Column: 0, Scope: OurFunc); |
54 | |
55 | // Test using and empty MBB. |
56 | EXPECT_EQ(DL0, MBB.findDebugLoc(MBB.instr_begin())); |
57 | EXPECT_EQ(DL0, MBB.findDebugLoc(MBB.instr_end())); |
58 | |
59 | EXPECT_EQ(DL0, MBB.rfindDebugLoc(MBB.instr_rbegin())); |
60 | EXPECT_EQ(DL0, MBB.rfindDebugLoc(MBB.instr_rend())); |
61 | |
62 | EXPECT_EQ(DL0, MBB.findPrevDebugLoc(MBB.instr_begin())); |
63 | EXPECT_EQ(DL0, MBB.findPrevDebugLoc(MBB.instr_end())); |
64 | |
65 | EXPECT_EQ(DL0, MBB.rfindPrevDebugLoc(MBB.instr_rbegin())); |
66 | EXPECT_EQ(DL0, MBB.rfindPrevDebugLoc(MBB.instr_rend())); |
67 | |
68 | // Insert two MIs with DebugLoc DL1 and DL3. |
69 | // Also add a DBG_VALUE with a different DebugLoc in between. |
70 | MCInstrDesc COPY = {.Opcode: TargetOpcode::COPY, .NumOperands: 0, .NumDefs: 0, .Size: 0, .SchedClass: 0, .NumImplicitUses: 0, .NumImplicitDefs: 0, .ImplicitOffset: 0, .OpInfoOffset: 0, .Flags: 0, .TSFlags: 0}; |
71 | MCInstrDesc DBG = {.Opcode: TargetOpcode::DBG_VALUE, .NumOperands: 0, .NumDefs: 0, .Size: 0, .SchedClass: 0, .NumImplicitUses: 0, .NumImplicitDefs: 0, .ImplicitOffset: 0, .OpInfoOffset: 0, .Flags: 0, .TSFlags: 0}; |
72 | auto MI3 = MF->CreateMachineInstr(MCID: COPY, DL: DL3); |
73 | MBB.insert(I: MBB.begin(), MI: MI3); |
74 | auto MI2 = MF->CreateMachineInstr(MCID: DBG, DL: DL2); |
75 | MBB.insert(I: MBB.begin(), MI: MI2); |
76 | auto MI1 = MF->CreateMachineInstr(MCID: COPY, DL: DL1); |
77 | MBB.insert(I: MBB.begin(), MI: MI1); |
78 | |
79 | // Test using two MIs with a debug instruction in between. |
80 | EXPECT_EQ(DL1, MBB.findDebugLoc(MBB.instr_begin())); |
81 | EXPECT_EQ(DL1, MBB.findDebugLoc(MI1)); |
82 | EXPECT_EQ(DL3, MBB.findDebugLoc(MI2)); |
83 | EXPECT_EQ(DL3, MBB.findDebugLoc(MI3)); |
84 | EXPECT_EQ(DL0, MBB.findDebugLoc(MBB.instr_end())); |
85 | |
86 | EXPECT_EQ(DL1, MBB.rfindDebugLoc(MBB.instr_rend())); |
87 | EXPECT_EQ(DL1, MBB.rfindDebugLoc(MI1)); |
88 | EXPECT_EQ(DL3, MBB.rfindDebugLoc(MI2)); |
89 | EXPECT_EQ(DL3, MBB.rfindDebugLoc(MI3)); |
90 | EXPECT_EQ(DL3, MBB.rfindDebugLoc(MBB.instr_rbegin())); |
91 | |
92 | EXPECT_EQ(DL0, MBB.findPrevDebugLoc(MBB.instr_begin())); |
93 | EXPECT_EQ(DL0, MBB.findPrevDebugLoc(MI1)); |
94 | EXPECT_EQ(DL1, MBB.findPrevDebugLoc(MI2)); |
95 | EXPECT_EQ(DL1, MBB.findPrevDebugLoc(MI3)); |
96 | EXPECT_EQ(DL3, MBB.findPrevDebugLoc(MBB.instr_end())); |
97 | |
98 | EXPECT_EQ(DL0, MBB.rfindPrevDebugLoc(MBB.instr_rend())); |
99 | EXPECT_EQ(DL0, MBB.rfindPrevDebugLoc(MI1)); |
100 | EXPECT_EQ(DL1, MBB.rfindPrevDebugLoc(MI2)); |
101 | EXPECT_EQ(DL1, MBB.rfindPrevDebugLoc(MI3)); |
102 | EXPECT_EQ(DL1, MBB.rfindPrevDebugLoc(MBB.instr_rbegin())); |
103 | |
104 | // Finalize DIBuilder to avoid memory leaks. |
105 | DIB.finalize(); |
106 | } |
107 | |
108 | } // end namespace |
109 | |