xref: /llvm-project/llvm/unittests/CodeGen/MachineBasicBlockTest.cpp (revision a23f9846163956b74ab578bc972415c015022d10)
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, 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("foo.c", "/bar");
42   DICompileUnit *OurCU =
43       DIB.createCompileUnit(dwarf::DW_LANG_C99, OurFile, "", false, "", 0);
44   auto OurSubT =
45       DIB.createSubroutineType(DIB.getOrCreateTypeArray(std::nullopt));
46   DISubprogram *OurFunc =
47       DIB.createFunction(OurCU, "bees", "", OurFile, 1, OurSubT, 1,
48                          DINode::FlagZero, DISubprogram::SPFlagDefinition);
49 
50   DebugLoc DL0;
51   DebugLoc DL1 = DILocation::get(Ctx, 1, 0, OurFunc);
52   DebugLoc DL2 = DILocation::get(Ctx, 2, 0, OurFunc);
53   DebugLoc DL3 = DILocation::get(Ctx, 3, 0, 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   // FIXME: This would crash (see https://reviews.llvm.org/D150577).
60   //EXPECT_EQ(DL0, MBB.rfindDebugLoc(MBB.instr_rbegin()));
61   //EXPECT_EQ(DL0, MBB.rfindDebugLoc(MBB.instr_rend()));
62 
63   EXPECT_EQ(DL0, MBB.findPrevDebugLoc(MBB.instr_begin()));
64   EXPECT_EQ(DL0, MBB.findPrevDebugLoc(MBB.instr_end()));
65 
66   EXPECT_EQ(DL0, MBB.rfindPrevDebugLoc(MBB.instr_rbegin()));
67   EXPECT_EQ(DL0, MBB.rfindPrevDebugLoc(MBB.instr_rend()));
68 
69   // Insert two MIs with DebugLoc DL1 and DL3.
70   // Also add a DBG_VALUE with a different DebugLoc in between.
71   MCInstrDesc COPY = {TargetOpcode::COPY, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
72   MCInstrDesc DBG = {TargetOpcode::DBG_VALUE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
73   auto MI3 = MF->CreateMachineInstr(COPY, DL3);
74   MBB.insert(MBB.begin(), MI3);
75   auto MI2 = MF->CreateMachineInstr(DBG, DL2);
76   MBB.insert(MBB.begin(), MI2);
77   auto MI1 = MF->CreateMachineInstr(COPY, DL1);
78   MBB.insert(MBB.begin(), MI1);
79 
80   // Test using two MIs with a debug instruction in between.
81   EXPECT_EQ(DL1, MBB.findDebugLoc(MBB.instr_begin()));
82   EXPECT_EQ(DL1, MBB.findDebugLoc(MI1));
83   EXPECT_EQ(DL3, MBB.findDebugLoc(MI2));
84   EXPECT_EQ(DL3, MBB.findDebugLoc(MI3));
85   EXPECT_EQ(DL0, MBB.findDebugLoc(MBB.instr_end()));
86 
87   // FIXME: This would crash (see https://reviews.llvm.org/D150577).
88   //EXPECT_EQ(DL1, MBB.rfindDebugLoc(MBB.instr_rend()));
89   EXPECT_EQ(DL1, MBB.rfindDebugLoc(MI1));
90   EXPECT_EQ(DL3, MBB.rfindDebugLoc(MI2));
91   EXPECT_EQ(DL3, MBB.rfindDebugLoc(MI3));
92   EXPECT_EQ(DL3, MBB.rfindDebugLoc(MBB.instr_rbegin()));
93 
94   EXPECT_EQ(DL0, MBB.findPrevDebugLoc(MBB.instr_begin()));
95   EXPECT_EQ(DL0, MBB.findPrevDebugLoc(MI1));
96   EXPECT_EQ(DL1, MBB.findPrevDebugLoc(MI2));
97   EXPECT_EQ(DL1, MBB.findPrevDebugLoc(MI3));
98   EXPECT_EQ(DL3, MBB.findPrevDebugLoc(MBB.instr_end()));
99 
100   EXPECT_EQ(DL0, MBB.rfindPrevDebugLoc(MBB.instr_rend()));
101   EXPECT_EQ(DL0, MBB.rfindPrevDebugLoc(MI1));
102   EXPECT_EQ(DL1, MBB.rfindPrevDebugLoc(MI2));
103   EXPECT_EQ(DL1, MBB.rfindPrevDebugLoc(MI3));
104   EXPECT_EQ(DL1, MBB.rfindPrevDebugLoc(MBB.instr_rbegin()));
105 }
106 
107 } // end namespace
108