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/CodeGenTargetMachineImpl.h" 11 #include "llvm/CodeGen/MachineFunction.h" 12 #include "llvm/CodeGen/MachineInstr.h" 13 #include "llvm/CodeGen/MachineInstrBuilder.h" 14 #include "llvm/CodeGen/MachineModuleInfo.h" 15 #include "llvm/CodeGen/TargetFrameLowering.h" 16 #include "llvm/CodeGen/TargetInstrInfo.h" 17 #include "llvm/CodeGen/TargetLowering.h" 18 #include "llvm/CodeGen/TargetSubtargetInfo.h" 19 #include "llvm/IR/DIBuilder.h" 20 #include "llvm/IR/DebugInfoMetadata.h" 21 #include "llvm/IR/IRBuilder.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/MC/TargetRegistry.h" 24 #include "gmock/gmock.h" 25 #include "gtest/gtest.h" 26 27 using namespace llvm; 28 29 namespace { 30 // Include helper functions to ease the manipulation of MachineFunctions. 31 #include "MFCommon.inc" 32 33 TEST(FindDebugLocTest, DifferentIterators) { 34 LLVMContext Ctx; 35 Module Mod("Module", Ctx); 36 auto MF = createMachineFunction(Ctx, Mod); 37 auto &MBB = *MF->CreateMachineBasicBlock(); 38 39 // Create metadata: CU, subprogram, some blocks and an inline function 40 // scope. 41 DIBuilder DIB(Mod); 42 DIFile *OurFile = DIB.createFile("foo.c", "/bar"); 43 DICompileUnit *OurCU = 44 DIB.createCompileUnit(dwarf::DW_LANG_C99, OurFile, "", false, "", 0); 45 auto OurSubT = DIB.createSubroutineType(DIB.getOrCreateTypeArray({})); 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 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 = {TargetOpcode::COPY, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 71 MCInstrDesc DBG = {TargetOpcode::DBG_VALUE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 72 auto MI3 = MF->CreateMachineInstr(COPY, DL3); 73 MBB.insert(MBB.begin(), MI3); 74 auto MI2 = MF->CreateMachineInstr(DBG, DL2); 75 MBB.insert(MBB.begin(), MI2); 76 auto MI1 = MF->CreateMachineInstr(COPY, DL1); 77 MBB.insert(MBB.begin(), 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