14ba319b5SDimitry Andric //===------ CFIInstrInserter.cpp - Insert additional CFI instructions -----===//
24ba319b5SDimitry Andric //
34ba319b5SDimitry Andric //                     The LLVM Compiler Infrastructure
44ba319b5SDimitry Andric //
54ba319b5SDimitry Andric // This file is distributed under the University of Illinois Open Source
64ba319b5SDimitry Andric // License. See LICENSE.TXT for details.
74ba319b5SDimitry Andric //
84ba319b5SDimitry Andric //===----------------------------------------------------------------------===//
94ba319b5SDimitry Andric //
104ba319b5SDimitry Andric /// \file This pass verifies incoming and outgoing CFA information of basic
114ba319b5SDimitry Andric /// blocks. CFA information is information about offset and register set by CFI
124ba319b5SDimitry Andric /// directives, valid at the start and end of a basic block. This pass checks
134ba319b5SDimitry Andric /// that outgoing information of predecessors matches incoming information of
144ba319b5SDimitry Andric /// their successors. Then it checks if blocks have correct CFA calculation rule
154ba319b5SDimitry Andric /// set and inserts additional CFI instruction at their beginnings if they
164ba319b5SDimitry Andric /// don't. CFI instructions are inserted if basic blocks have incorrect offset
174ba319b5SDimitry Andric /// or register set by previous blocks, as a result of a non-linear layout of
184ba319b5SDimitry Andric /// blocks in a function.
194ba319b5SDimitry Andric //===----------------------------------------------------------------------===//
204ba319b5SDimitry Andric 
214ba319b5SDimitry Andric #include "llvm/ADT/DepthFirstIterator.h"
224ba319b5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
234ba319b5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
244ba319b5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
254ba319b5SDimitry Andric #include "llvm/CodeGen/Passes.h"
264ba319b5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
274ba319b5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
284ba319b5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
294ba319b5SDimitry Andric #include "llvm/Target/TargetMachine.h"
304ba319b5SDimitry Andric using namespace llvm;
314ba319b5SDimitry Andric 
324ba319b5SDimitry Andric static cl::opt<bool> VerifyCFI("verify-cfiinstrs",
334ba319b5SDimitry Andric     cl::desc("Verify Call Frame Information instructions"),
344ba319b5SDimitry Andric     cl::init(false),
354ba319b5SDimitry Andric     cl::Hidden);
364ba319b5SDimitry Andric 
374ba319b5SDimitry Andric namespace {
384ba319b5SDimitry Andric class CFIInstrInserter : public MachineFunctionPass {
394ba319b5SDimitry Andric  public:
404ba319b5SDimitry Andric   static char ID;
414ba319b5SDimitry Andric 
CFIInstrInserter()424ba319b5SDimitry Andric   CFIInstrInserter() : MachineFunctionPass(ID) {
434ba319b5SDimitry Andric     initializeCFIInstrInserterPass(*PassRegistry::getPassRegistry());
444ba319b5SDimitry Andric   }
454ba319b5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const464ba319b5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
474ba319b5SDimitry Andric     AU.setPreservesAll();
484ba319b5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
494ba319b5SDimitry Andric   }
504ba319b5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)514ba319b5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override {
524ba319b5SDimitry Andric     if (!MF.getMMI().hasDebugInfo() &&
534ba319b5SDimitry Andric         !MF.getFunction().needsUnwindTableEntry())
544ba319b5SDimitry Andric       return false;
554ba319b5SDimitry Andric 
564ba319b5SDimitry Andric     MBBVector.resize(MF.getNumBlockIDs());
574ba319b5SDimitry Andric     calculateCFAInfo(MF);
584ba319b5SDimitry Andric 
594ba319b5SDimitry Andric     if (VerifyCFI) {
604ba319b5SDimitry Andric       if (unsigned ErrorNum = verify(MF))
614ba319b5SDimitry Andric         report_fatal_error("Found " + Twine(ErrorNum) +
624ba319b5SDimitry Andric                            " in/out CFI information errors.");
634ba319b5SDimitry Andric     }
644ba319b5SDimitry Andric     bool insertedCFI = insertCFIInstrs(MF);
654ba319b5SDimitry Andric     MBBVector.clear();
664ba319b5SDimitry Andric     return insertedCFI;
674ba319b5SDimitry Andric   }
684ba319b5SDimitry Andric 
694ba319b5SDimitry Andric  private:
704ba319b5SDimitry Andric   struct MBBCFAInfo {
714ba319b5SDimitry Andric     MachineBasicBlock *MBB;
724ba319b5SDimitry Andric     /// Value of cfa offset valid at basic block entry.
734ba319b5SDimitry Andric     int IncomingCFAOffset = -1;
744ba319b5SDimitry Andric     /// Value of cfa offset valid at basic block exit.
754ba319b5SDimitry Andric     int OutgoingCFAOffset = -1;
764ba319b5SDimitry Andric     /// Value of cfa register valid at basic block entry.
774ba319b5SDimitry Andric     unsigned IncomingCFARegister = 0;
784ba319b5SDimitry Andric     /// Value of cfa register valid at basic block exit.
794ba319b5SDimitry Andric     unsigned OutgoingCFARegister = 0;
804ba319b5SDimitry Andric     /// If in/out cfa offset and register values for this block have already
814ba319b5SDimitry Andric     /// been set or not.
824ba319b5SDimitry Andric     bool Processed = false;
834ba319b5SDimitry Andric   };
844ba319b5SDimitry Andric 
854ba319b5SDimitry Andric   /// Contains cfa offset and register values valid at entry and exit of basic
864ba319b5SDimitry Andric   /// blocks.
874ba319b5SDimitry Andric   std::vector<MBBCFAInfo> MBBVector;
884ba319b5SDimitry Andric 
894ba319b5SDimitry Andric   /// Calculate cfa offset and register values valid at entry and exit for all
904ba319b5SDimitry Andric   /// basic blocks in a function.
914ba319b5SDimitry Andric   void calculateCFAInfo(MachineFunction &MF);
924ba319b5SDimitry Andric   /// Calculate cfa offset and register values valid at basic block exit by
934ba319b5SDimitry Andric   /// checking the block for CFI instructions. Block's incoming CFA info remains
944ba319b5SDimitry Andric   /// the same.
954ba319b5SDimitry Andric   void calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo);
964ba319b5SDimitry Andric   /// Update in/out cfa offset and register values for successors of the basic
974ba319b5SDimitry Andric   /// block.
984ba319b5SDimitry Andric   void updateSuccCFAInfo(MBBCFAInfo &MBBInfo);
994ba319b5SDimitry Andric 
1004ba319b5SDimitry Andric   /// Check if incoming CFA information of a basic block matches outgoing CFA
1014ba319b5SDimitry Andric   /// information of the previous block. If it doesn't, insert CFI instruction
1024ba319b5SDimitry Andric   /// at the beginning of the block that corrects the CFA calculation rule for
1034ba319b5SDimitry Andric   /// that block.
1044ba319b5SDimitry Andric   bool insertCFIInstrs(MachineFunction &MF);
1054ba319b5SDimitry Andric   /// Return the cfa offset value that should be set at the beginning of a MBB
1064ba319b5SDimitry Andric   /// if needed. The negated value is needed when creating CFI instructions that
1074ba319b5SDimitry Andric   /// set absolute offset.
getCorrectCFAOffset(MachineBasicBlock * MBB)1084ba319b5SDimitry Andric   int getCorrectCFAOffset(MachineBasicBlock *MBB) {
1094ba319b5SDimitry Andric     return -MBBVector[MBB->getNumber()].IncomingCFAOffset;
1104ba319b5SDimitry Andric   }
1114ba319b5SDimitry Andric 
1124ba319b5SDimitry Andric   void report(const MBBCFAInfo &Pred, const MBBCFAInfo &Succ);
1134ba319b5SDimitry Andric   /// Go through each MBB in a function and check that outgoing offset and
1144ba319b5SDimitry Andric   /// register of its predecessors match incoming offset and register of that
1154ba319b5SDimitry Andric   /// MBB, as well as that incoming offset and register of its successors match
1164ba319b5SDimitry Andric   /// outgoing offset and register of the MBB.
1174ba319b5SDimitry Andric   unsigned verify(MachineFunction &MF);
1184ba319b5SDimitry Andric };
1194ba319b5SDimitry Andric }  // namespace
1204ba319b5SDimitry Andric 
1214ba319b5SDimitry Andric char CFIInstrInserter::ID = 0;
1224ba319b5SDimitry Andric INITIALIZE_PASS(CFIInstrInserter, "cfi-instr-inserter",
1234ba319b5SDimitry Andric                 "Check CFA info and insert CFI instructions if needed", false,
1244ba319b5SDimitry Andric                 false)
createCFIInstrInserter()1254ba319b5SDimitry Andric FunctionPass *llvm::createCFIInstrInserter() { return new CFIInstrInserter(); }
1264ba319b5SDimitry Andric 
calculateCFAInfo(MachineFunction & MF)1274ba319b5SDimitry Andric void CFIInstrInserter::calculateCFAInfo(MachineFunction &MF) {
1284ba319b5SDimitry Andric   // Initial CFA offset value i.e. the one valid at the beginning of the
1294ba319b5SDimitry Andric   // function.
1304ba319b5SDimitry Andric   int InitialOffset =
1314ba319b5SDimitry Andric       MF.getSubtarget().getFrameLowering()->getInitialCFAOffset(MF);
1324ba319b5SDimitry Andric   // Initial CFA register value i.e. the one valid at the beginning of the
1334ba319b5SDimitry Andric   // function.
1344ba319b5SDimitry Andric   unsigned InitialRegister =
1354ba319b5SDimitry Andric       MF.getSubtarget().getFrameLowering()->getInitialCFARegister(MF);
1364ba319b5SDimitry Andric 
1374ba319b5SDimitry Andric   // Initialize MBBMap.
1384ba319b5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
1394ba319b5SDimitry Andric     MBBCFAInfo MBBInfo;
1404ba319b5SDimitry Andric     MBBInfo.MBB = &MBB;
1414ba319b5SDimitry Andric     MBBInfo.IncomingCFAOffset = InitialOffset;
1424ba319b5SDimitry Andric     MBBInfo.OutgoingCFAOffset = InitialOffset;
1434ba319b5SDimitry Andric     MBBInfo.IncomingCFARegister = InitialRegister;
1444ba319b5SDimitry Andric     MBBInfo.OutgoingCFARegister = InitialRegister;
1454ba319b5SDimitry Andric     MBBVector[MBB.getNumber()] = MBBInfo;
1464ba319b5SDimitry Andric   }
1474ba319b5SDimitry Andric 
1484ba319b5SDimitry Andric   // Set in/out cfa info for all blocks in the function. This traversal is based
1494ba319b5SDimitry Andric   // on the assumption that the first block in the function is the entry block
1504ba319b5SDimitry Andric   // i.e. that it has initial cfa offset and register values as incoming CFA
1514ba319b5SDimitry Andric   // information.
1524ba319b5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
1534ba319b5SDimitry Andric     if (MBBVector[MBB.getNumber()].Processed) continue;
1544ba319b5SDimitry Andric     updateSuccCFAInfo(MBBVector[MBB.getNumber()]);
1554ba319b5SDimitry Andric   }
1564ba319b5SDimitry Andric }
1574ba319b5SDimitry Andric 
calculateOutgoingCFAInfo(MBBCFAInfo & MBBInfo)1584ba319b5SDimitry Andric void CFIInstrInserter::calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo) {
1594ba319b5SDimitry Andric   // Outgoing cfa offset set by the block.
1604ba319b5SDimitry Andric   int SetOffset = MBBInfo.IncomingCFAOffset;
1614ba319b5SDimitry Andric   // Outgoing cfa register set by the block.
1624ba319b5SDimitry Andric   unsigned SetRegister = MBBInfo.IncomingCFARegister;
1634ba319b5SDimitry Andric   const std::vector<MCCFIInstruction> &Instrs =
1644ba319b5SDimitry Andric       MBBInfo.MBB->getParent()->getFrameInstructions();
1654ba319b5SDimitry Andric 
1664ba319b5SDimitry Andric   // Determine cfa offset and register set by the block.
1674ba319b5SDimitry Andric   for (MachineInstr &MI : *MBBInfo.MBB) {
1684ba319b5SDimitry Andric     if (MI.isCFIInstruction()) {
1694ba319b5SDimitry Andric       unsigned CFIIndex = MI.getOperand(0).getCFIIndex();
1704ba319b5SDimitry Andric       const MCCFIInstruction &CFI = Instrs[CFIIndex];
1714ba319b5SDimitry Andric       switch (CFI.getOperation()) {
1724ba319b5SDimitry Andric       case MCCFIInstruction::OpDefCfaRegister:
1734ba319b5SDimitry Andric         SetRegister = CFI.getRegister();
1744ba319b5SDimitry Andric         break;
1754ba319b5SDimitry Andric       case MCCFIInstruction::OpDefCfaOffset:
1764ba319b5SDimitry Andric         SetOffset = CFI.getOffset();
1774ba319b5SDimitry Andric         break;
1784ba319b5SDimitry Andric       case MCCFIInstruction::OpAdjustCfaOffset:
1794ba319b5SDimitry Andric         SetOffset += CFI.getOffset();
1804ba319b5SDimitry Andric         break;
1814ba319b5SDimitry Andric       case MCCFIInstruction::OpDefCfa:
1824ba319b5SDimitry Andric         SetRegister = CFI.getRegister();
1834ba319b5SDimitry Andric         SetOffset = CFI.getOffset();
1844ba319b5SDimitry Andric         break;
1854ba319b5SDimitry Andric       case MCCFIInstruction::OpRememberState:
1864ba319b5SDimitry Andric         // TODO: Add support for handling cfi_remember_state.
1874ba319b5SDimitry Andric #ifndef NDEBUG
1884ba319b5SDimitry Andric         report_fatal_error(
1894ba319b5SDimitry Andric             "Support for cfi_remember_state not implemented! Value of CFA "
1904ba319b5SDimitry Andric             "may be incorrect!\n");
1914ba319b5SDimitry Andric #endif
1924ba319b5SDimitry Andric         break;
1934ba319b5SDimitry Andric       case MCCFIInstruction::OpRestoreState:
1944ba319b5SDimitry Andric         // TODO: Add support for handling cfi_restore_state.
1954ba319b5SDimitry Andric #ifndef NDEBUG
1964ba319b5SDimitry Andric         report_fatal_error(
1974ba319b5SDimitry Andric             "Support for cfi_restore_state not implemented! Value of CFA may "
1984ba319b5SDimitry Andric             "be incorrect!\n");
1994ba319b5SDimitry Andric #endif
2004ba319b5SDimitry Andric         break;
2014ba319b5SDimitry Andric       // Other CFI directives do not affect CFA value.
2024ba319b5SDimitry Andric       case MCCFIInstruction::OpSameValue:
2034ba319b5SDimitry Andric       case MCCFIInstruction::OpOffset:
2044ba319b5SDimitry Andric       case MCCFIInstruction::OpRelOffset:
2054ba319b5SDimitry Andric       case MCCFIInstruction::OpEscape:
2064ba319b5SDimitry Andric       case MCCFIInstruction::OpRestore:
2074ba319b5SDimitry Andric       case MCCFIInstruction::OpUndefined:
2084ba319b5SDimitry Andric       case MCCFIInstruction::OpRegister:
2094ba319b5SDimitry Andric       case MCCFIInstruction::OpWindowSave:
210*b5893f02SDimitry Andric       case MCCFIInstruction::OpNegateRAState:
2114ba319b5SDimitry Andric       case MCCFIInstruction::OpGnuArgsSize:
2124ba319b5SDimitry Andric         break;
2134ba319b5SDimitry Andric       }
2144ba319b5SDimitry Andric     }
2154ba319b5SDimitry Andric   }
2164ba319b5SDimitry Andric 
2174ba319b5SDimitry Andric   MBBInfo.Processed = true;
2184ba319b5SDimitry Andric 
2194ba319b5SDimitry Andric   // Update outgoing CFA info.
2204ba319b5SDimitry Andric   MBBInfo.OutgoingCFAOffset = SetOffset;
2214ba319b5SDimitry Andric   MBBInfo.OutgoingCFARegister = SetRegister;
2224ba319b5SDimitry Andric }
2234ba319b5SDimitry Andric 
updateSuccCFAInfo(MBBCFAInfo & MBBInfo)2244ba319b5SDimitry Andric void CFIInstrInserter::updateSuccCFAInfo(MBBCFAInfo &MBBInfo) {
2254ba319b5SDimitry Andric   SmallVector<MachineBasicBlock *, 4> Stack;
2264ba319b5SDimitry Andric   Stack.push_back(MBBInfo.MBB);
2274ba319b5SDimitry Andric 
2284ba319b5SDimitry Andric   do {
2294ba319b5SDimitry Andric     MachineBasicBlock *Current = Stack.pop_back_val();
2304ba319b5SDimitry Andric     MBBCFAInfo &CurrentInfo = MBBVector[Current->getNumber()];
2314ba319b5SDimitry Andric     if (CurrentInfo.Processed)
2324ba319b5SDimitry Andric       continue;
2334ba319b5SDimitry Andric 
2344ba319b5SDimitry Andric     calculateOutgoingCFAInfo(CurrentInfo);
2354ba319b5SDimitry Andric     for (auto *Succ : CurrentInfo.MBB->successors()) {
2364ba319b5SDimitry Andric       MBBCFAInfo &SuccInfo = MBBVector[Succ->getNumber()];
2374ba319b5SDimitry Andric       if (!SuccInfo.Processed) {
2384ba319b5SDimitry Andric         SuccInfo.IncomingCFAOffset = CurrentInfo.OutgoingCFAOffset;
2394ba319b5SDimitry Andric         SuccInfo.IncomingCFARegister = CurrentInfo.OutgoingCFARegister;
2404ba319b5SDimitry Andric         Stack.push_back(Succ);
2414ba319b5SDimitry Andric       }
2424ba319b5SDimitry Andric     }
2434ba319b5SDimitry Andric   } while (!Stack.empty());
2444ba319b5SDimitry Andric }
2454ba319b5SDimitry Andric 
insertCFIInstrs(MachineFunction & MF)2464ba319b5SDimitry Andric bool CFIInstrInserter::insertCFIInstrs(MachineFunction &MF) {
2474ba319b5SDimitry Andric   const MBBCFAInfo *PrevMBBInfo = &MBBVector[MF.front().getNumber()];
2484ba319b5SDimitry Andric   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
2494ba319b5SDimitry Andric   bool InsertedCFIInstr = false;
2504ba319b5SDimitry Andric 
2514ba319b5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
2524ba319b5SDimitry Andric     // Skip the first MBB in a function
2534ba319b5SDimitry Andric     if (MBB.getNumber() == MF.front().getNumber()) continue;
2544ba319b5SDimitry Andric 
2554ba319b5SDimitry Andric     const MBBCFAInfo &MBBInfo = MBBVector[MBB.getNumber()];
2564ba319b5SDimitry Andric     auto MBBI = MBBInfo.MBB->begin();
2574ba319b5SDimitry Andric     DebugLoc DL = MBBInfo.MBB->findDebugLoc(MBBI);
2584ba319b5SDimitry Andric 
2594ba319b5SDimitry Andric     if (PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset) {
2604ba319b5SDimitry Andric       // If both outgoing offset and register of a previous block don't match
2614ba319b5SDimitry Andric       // incoming offset and register of this block, add a def_cfa instruction
2624ba319b5SDimitry Andric       // with the correct offset and register for this block.
2634ba319b5SDimitry Andric       if (PrevMBBInfo->OutgoingCFARegister != MBBInfo.IncomingCFARegister) {
2644ba319b5SDimitry Andric         unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
2654ba319b5SDimitry Andric             nullptr, MBBInfo.IncomingCFARegister, getCorrectCFAOffset(&MBB)));
2664ba319b5SDimitry Andric         BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
2674ba319b5SDimitry Andric             .addCFIIndex(CFIIndex);
2684ba319b5SDimitry Andric         // If outgoing offset of a previous block doesn't match incoming offset
2694ba319b5SDimitry Andric         // of this block, add a def_cfa_offset instruction with the correct
2704ba319b5SDimitry Andric         // offset for this block.
2714ba319b5SDimitry Andric       } else {
2724ba319b5SDimitry Andric         unsigned CFIIndex =
2734ba319b5SDimitry Andric             MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(
2744ba319b5SDimitry Andric                 nullptr, getCorrectCFAOffset(&MBB)));
2754ba319b5SDimitry Andric         BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
2764ba319b5SDimitry Andric             .addCFIIndex(CFIIndex);
2774ba319b5SDimitry Andric       }
2784ba319b5SDimitry Andric       InsertedCFIInstr = true;
2794ba319b5SDimitry Andric       // If outgoing register of a previous block doesn't match incoming
2804ba319b5SDimitry Andric       // register of this block, add a def_cfa_register instruction with the
2814ba319b5SDimitry Andric       // correct register for this block.
2824ba319b5SDimitry Andric     } else if (PrevMBBInfo->OutgoingCFARegister !=
2834ba319b5SDimitry Andric                MBBInfo.IncomingCFARegister) {
2844ba319b5SDimitry Andric       unsigned CFIIndex =
2854ba319b5SDimitry Andric           MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
2864ba319b5SDimitry Andric               nullptr, MBBInfo.IncomingCFARegister));
2874ba319b5SDimitry Andric       BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
2884ba319b5SDimitry Andric           .addCFIIndex(CFIIndex);
2894ba319b5SDimitry Andric       InsertedCFIInstr = true;
2904ba319b5SDimitry Andric     }
2914ba319b5SDimitry Andric     PrevMBBInfo = &MBBInfo;
2924ba319b5SDimitry Andric   }
2934ba319b5SDimitry Andric   return InsertedCFIInstr;
2944ba319b5SDimitry Andric }
2954ba319b5SDimitry Andric 
report(const MBBCFAInfo & Pred,const MBBCFAInfo & Succ)2964ba319b5SDimitry Andric void CFIInstrInserter::report(const MBBCFAInfo &Pred, const MBBCFAInfo &Succ) {
2974ba319b5SDimitry Andric   errs() << "*** Inconsistent CFA register and/or offset between pred and succ "
2984ba319b5SDimitry Andric             "***\n";
2994ba319b5SDimitry Andric   errs() << "Pred: " << Pred.MBB->getName() << " #" << Pred.MBB->getNumber()
3004ba319b5SDimitry Andric          << " in " << Pred.MBB->getParent()->getName()
3014ba319b5SDimitry Andric          << " outgoing CFA Reg:" << Pred.OutgoingCFARegister << "\n";
3024ba319b5SDimitry Andric   errs() << "Pred: " << Pred.MBB->getName() << " #" << Pred.MBB->getNumber()
3034ba319b5SDimitry Andric          << " in " << Pred.MBB->getParent()->getName()
3044ba319b5SDimitry Andric          << " outgoing CFA Offset:" << Pred.OutgoingCFAOffset << "\n";
3054ba319b5SDimitry Andric   errs() << "Succ: " << Succ.MBB->getName() << " #" << Succ.MBB->getNumber()
3064ba319b5SDimitry Andric          << " incoming CFA Reg:" << Succ.IncomingCFARegister << "\n";
3074ba319b5SDimitry Andric   errs() << "Succ: " << Succ.MBB->getName() << " #" << Succ.MBB->getNumber()
3084ba319b5SDimitry Andric          << " incoming CFA Offset:" << Succ.IncomingCFAOffset << "\n";
3094ba319b5SDimitry Andric }
3104ba319b5SDimitry Andric 
verify(MachineFunction & MF)3114ba319b5SDimitry Andric unsigned CFIInstrInserter::verify(MachineFunction &MF) {
3124ba319b5SDimitry Andric   unsigned ErrorNum = 0;
3134ba319b5SDimitry Andric   for (auto *CurrMBB : depth_first(&MF)) {
3144ba319b5SDimitry Andric     const MBBCFAInfo &CurrMBBInfo = MBBVector[CurrMBB->getNumber()];
3154ba319b5SDimitry Andric     for (MachineBasicBlock *Succ : CurrMBB->successors()) {
3164ba319b5SDimitry Andric       const MBBCFAInfo &SuccMBBInfo = MBBVector[Succ->getNumber()];
3174ba319b5SDimitry Andric       // Check that incoming offset and register values of successors match the
3184ba319b5SDimitry Andric       // outgoing offset and register values of CurrMBB
3194ba319b5SDimitry Andric       if (SuccMBBInfo.IncomingCFAOffset != CurrMBBInfo.OutgoingCFAOffset ||
3204ba319b5SDimitry Andric           SuccMBBInfo.IncomingCFARegister != CurrMBBInfo.OutgoingCFARegister) {
321*b5893f02SDimitry Andric         // Inconsistent offsets/registers are ok for 'noreturn' blocks because
322*b5893f02SDimitry Andric         // we don't generate epilogues inside such blocks.
323*b5893f02SDimitry Andric         if (SuccMBBInfo.MBB->succ_empty() && !SuccMBBInfo.MBB->isReturnBlock())
324*b5893f02SDimitry Andric           continue;
3254ba319b5SDimitry Andric         report(CurrMBBInfo, SuccMBBInfo);
3264ba319b5SDimitry Andric         ErrorNum++;
3274ba319b5SDimitry Andric       }
3284ba319b5SDimitry Andric     }
3294ba319b5SDimitry Andric   }
3304ba319b5SDimitry Andric   return ErrorNum;
3314ba319b5SDimitry Andric }
332