@@ -104,6 +104,36 @@ IRTranslator::IRTranslator() : MachineFunctionPass(ID) {
104104 initializeIRTranslatorPass (*PassRegistry::getPassRegistry ());
105105}
106106
107+ #ifndef NDEBUG
108+ // / Verify that every instruction created has the same DILocation as the
109+ // / instruction being translated.
110+ class DILocationVerifier : MachineFunction::Delegate {
111+ MachineFunction &MF;
112+ const Instruction *CurrInst = nullptr ;
113+
114+ public:
115+ DILocationVerifier (MachineFunction &MF) : MF(MF) { MF.setDelegate (this ); }
116+ ~DILocationVerifier () { MF.resetDelegate (this ); }
117+
118+ const Instruction *getCurrentInst () const { return CurrInst; }
119+ void setCurrentInst (const Instruction *Inst) { CurrInst = Inst; }
120+
121+ void MF_HandleInsertion (const MachineInstr &MI) override {
122+ assert (getCurrentInst () && " Inserted instruction without a current MI" );
123+
124+ // Only print the check message if we're actually checking it.
125+ #ifndef NDEBUG
126+ LLVM_DEBUG (dbgs () << " Checking DILocation from " << *CurrInst
127+ << " was copied to " << MI);
128+ #endif
129+ assert (CurrInst->getDebugLoc () == MI.getDebugLoc () &&
130+ " Line info was not transferred to all instructions" );
131+ }
132+ void MF_HandleRemoval (const MachineInstr &MI) override {}
133+ };
134+ #endif // ifndef NDEBUG
135+
136+
107137void IRTranslator::getAnalysisUsage (AnalysisUsage &AU) const {
108138 AU.addRequired <StackProtector>();
109139 AU.addRequired <TargetPassConfig>();
@@ -1468,9 +1498,16 @@ bool IRTranslator::translateAtomicRMW(const User &U,
14681498}
14691499
14701500void IRTranslator::finishPendingPhis () {
1501+ #ifndef NDEBUG
1502+ DILocationVerifier Verifier (*MF);
1503+ #endif // ifndef NDEBUG
14711504 for (auto &Phi : PendingPHIs) {
14721505 const PHINode *PI = Phi.first ;
14731506 ArrayRef<MachineInstr *> ComponentPHIs = Phi.second ;
1507+ EntryBuilder.setDebugLoc (PI->getDebugLoc ());
1508+ #ifndef NDEBUG
1509+ Verifier.setCurrentInst (PI);
1510+ #endif // ifndef NDEBUG
14741511
14751512 // All MachineBasicBlocks exist, add them to the PHI. We assume IRTranslator
14761513 // won't create extra control flow here, otherwise we need to find the
@@ -1509,6 +1546,7 @@ bool IRTranslator::valueIsSplit(const Value &V,
15091546
15101547bool IRTranslator::translate (const Instruction &Inst) {
15111548 CurBuilder.setDebugLoc (Inst.getDebugLoc ());
1549+ EntryBuilder.setDebugLoc (Inst.getDebugLoc ());
15121550 switch (Inst.getOpcode ()) {
15131551#define HANDLE_INST (NUM, OPCODE, CLASS ) \
15141552 case Instruction::OPCODE: return translate##OPCODE (Inst, CurBuilder);
@@ -1684,31 +1722,39 @@ bool IRTranslator::runOnMachineFunction(MachineFunction &CurMF) {
16841722 }
16851723
16861724 // Need to visit defs before uses when translating instructions.
1687- ReversePostOrderTraversal<const Function *> RPOT (&F);
1688- for (const BasicBlock *BB : RPOT) {
1689- MachineBasicBlock &MBB = getMBB (*BB);
1690- // Set the insertion point of all the following translations to
1691- // the end of this basic block.
1692- CurBuilder.setMBB (MBB);
1693-
1694- for (const Instruction &Inst : *BB) {
1695- if (translate (Inst))
1696- continue ;
1697-
1698- OptimizationRemarkMissed R (" gisel-irtranslator" , " GISelFailure" ,
1699- Inst.getDebugLoc (), BB);
1700- R << " unable to translate instruction: " << ore::NV (" Opcode" , &Inst);
1701-
1702- if (ORE->allowExtraAnalysis (" gisel-irtranslator" )) {
1703- std::string InstStrStorage;
1704- raw_string_ostream InstStr (InstStrStorage);
1705- InstStr << Inst;
1725+ {
1726+ ReversePostOrderTraversal<const Function *> RPOT (&F);
1727+ #ifndef NDEBUG
1728+ DILocationVerifier Verifier (*MF);
1729+ #endif // ifndef NDEBUG
1730+ for (const BasicBlock *BB : RPOT) {
1731+ MachineBasicBlock &MBB = getMBB (*BB);
1732+ // Set the insertion point of all the following translations to
1733+ // the end of this basic block.
1734+ CurBuilder.setMBB (MBB);
1735+
1736+ for (const Instruction &Inst : *BB) {
1737+ #ifndef NDEBUG
1738+ Verifier.setCurrentInst (&Inst);
1739+ #endif // ifndef NDEBUG
1740+ if (translate (Inst))
1741+ continue ;
1742+
1743+ OptimizationRemarkMissed R (" gisel-irtranslator" , " GISelFailure" ,
1744+ Inst.getDebugLoc (), BB);
1745+ R << " unable to translate instruction: " << ore::NV (" Opcode" , &Inst);
1746+
1747+ if (ORE->allowExtraAnalysis (" gisel-irtranslator" )) {
1748+ std::string InstStrStorage;
1749+ raw_string_ostream InstStr (InstStrStorage);
1750+ InstStr << Inst;
1751+
1752+ R << " : '" << InstStr.str () << " '" ;
1753+ }
17061754
1707- R << " : '" << InstStr.str () << " '" ;
1755+ reportTranslationError (*MF, *TPC, *ORE, R);
1756+ return false ;
17081757 }
1709-
1710- reportTranslationError (*MF, *TPC, *ORE, R);
1711- return false ;
17121758 }
17131759 }
17141760
0 commit comments