1//===------ CFIInstrInserter.cpp - Insert additional CFI instructions -----===//
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/// \file This pass verifies incoming and outgoing CFA information of basic
10/// blocks. CFA information is information about offset and register set by CFI
11/// directives, valid at the start and end of a basic block. This pass checks
12/// that outgoing information of predecessors matches incoming information of
13/// their successors. Then it checks if blocks have correct CFA calculation rule
14/// set and inserts additional CFI instruction at their beginnings if they
15/// don't. CFI instructions are inserted if basic blocks have incorrect offset
16/// or register set by previous blocks, as a result of a non-linear layout of
17/// blocks in a function.
18//===----------------------------------------------------------------------===//
19
20#include "llvm/CodeGen/CFIInstrInserter.h"
21#include "llvm/ADT/DepthFirstIterator.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/CodeGen/TargetFrameLowering.h"
26#include "llvm/CodeGen/TargetInstrInfo.h"
27#include "llvm/CodeGen/TargetSubtargetInfo.h"
28#include "llvm/InitializePasses.h"
29#include "llvm/MC/MCContext.h"
30#include "llvm/MC/MCDwarf.h"
31using namespace llvm;
32
33static cl::opt<bool> VerifyCFI("verify-cfiinstrs",
34 cl::desc("Verify Call Frame Information instructions"),
35 cl::init(Val: false),
36 cl::Hidden);
37
38namespace {
39class CFIInstrInserterImpl {
40public:
41 bool run(MachineFunction &MF) {
42 if (!MF.needsFrameMoves())
43 return false;
44
45 MBBVector.resize(new_size: MF.getNumBlockIDs());
46 calculateCFAInfo(MF);
47
48 if (VerifyCFI) {
49 if (unsigned ErrorNum = verify(MF))
50 report_fatal_error(reason: "Found " + Twine(ErrorNum) +
51 " in/out CFI information errors.");
52 }
53 bool insertedCFI = insertCFIInstrs(MF);
54 MBBVector.clear();
55 return insertedCFI;
56 }
57
58private:
59 /// contains the location where CSR register is saved.
60 class CSRSavedLocation {
61 public:
62 enum Kind { Invalid, Register, CFAOffset };
63 Kind K = Invalid;
64
65 private:
66 union {
67 // Dwarf register number
68 unsigned Reg;
69 // CFA offset
70 int64_t Offset;
71 };
72
73 public:
74 CSRSavedLocation() {}
75
76 static CSRSavedLocation createCFAOffset(int64_t Offset) {
77 CSRSavedLocation Loc;
78 Loc.K = Kind::CFAOffset;
79 Loc.Offset = Offset;
80 return Loc;
81 }
82
83 static CSRSavedLocation createRegister(unsigned Reg) {
84 CSRSavedLocation Loc;
85 Loc.K = Kind::Register;
86 Loc.Reg = Reg;
87 return Loc;
88 }
89
90 bool isValid() const { return K != Kind::Invalid; }
91
92 unsigned getRegister() const {
93 assert(K == Kind::Register);
94 return Reg;
95 }
96
97 int64_t getOffset() const {
98 assert(K == Kind::CFAOffset);
99 return Offset;
100 }
101
102 bool operator==(const CSRSavedLocation &RHS) const {
103 if (K != RHS.K)
104 return false;
105 switch (K) {
106 case Kind::Invalid:
107 return true;
108 case Kind::Register:
109 return getRegister() == RHS.getRegister();
110 case Kind::CFAOffset:
111 return getOffset() == RHS.getOffset();
112 }
113 llvm_unreachable("Unknown CSRSavedLocation Kind!");
114 }
115 bool operator!=(const CSRSavedLocation &RHS) const {
116 return !(*this == RHS);
117 }
118 void dump(raw_ostream &OS) const {
119 switch (K) {
120 case Kind::Invalid:
121 OS << "Invalid";
122 break;
123 case Kind::Register:
124 OS << "In Dwarf register: " << Reg;
125 break;
126 case Kind::CFAOffset:
127 OS << "At CFA offset: " << Offset;
128 break;
129 }
130 }
131 };
132
133 struct MBBCFAInfo {
134 MachineBasicBlock *MBB;
135 /// Value of cfa offset valid at basic block entry.
136 int64_t IncomingCFAOffset = -1;
137 /// Value of cfa offset valid at basic block exit.
138 int64_t OutgoingCFAOffset = -1;
139 /// Value of cfa register valid at basic block entry.
140 unsigned IncomingCFARegister = 0;
141 /// Value of cfa register valid at basic block exit.
142 unsigned OutgoingCFARegister = 0;
143 /// Set of callee saved registers saved at basic block entry.
144 BitVector IncomingCSRSaved;
145 /// Set of callee saved registers saved at basic block exit.
146 BitVector OutgoingCSRSaved;
147 /// If in/out cfa offset and register values for this block have already
148 /// been set or not.
149 bool Processed = false;
150 };
151
152 /// Contains cfa offset and register values valid at entry and exit of basic
153 /// blocks.
154 std::vector<MBBCFAInfo> MBBVector;
155
156 /// Map the callee save registers to the locations where they are saved.
157 SmallDenseMap<unsigned, CSRSavedLocation, 16> CSRLocMap;
158
159 /// Calculate cfa offset and register values valid at entry and exit for all
160 /// basic blocks in a function.
161 void calculateCFAInfo(MachineFunction &MF);
162 /// Calculate cfa offset and register values valid at basic block exit by
163 /// checking the block for CFI instructions. Block's incoming CFA info remains
164 /// the same.
165 void calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo);
166 /// Update in/out cfa offset and register values for successors of the basic
167 /// block.
168 void updateSuccCFAInfo(MBBCFAInfo &MBBInfo);
169
170 /// Check if incoming CFA information of a basic block matches outgoing CFA
171 /// information of the previous block. If it doesn't, insert CFI instruction
172 /// at the beginning of the block that corrects the CFA calculation rule for
173 /// that block.
174 bool insertCFIInstrs(MachineFunction &MF);
175 /// Return the cfa offset value that should be set at the beginning of a MBB
176 /// if needed. The negated value is needed when creating CFI instructions that
177 /// set absolute offset.
178 int64_t getCorrectCFAOffset(MachineBasicBlock *MBB) {
179 return MBBVector[MBB->getNumber()].IncomingCFAOffset;
180 }
181
182 void reportCFAError(const MBBCFAInfo &Pred, const MBBCFAInfo &Succ);
183 void reportCSRError(const MBBCFAInfo &Pred, const MBBCFAInfo &Succ);
184 /// Go through each MBB in a function and check that outgoing offset and
185 /// register of its predecessors match incoming offset and register of that
186 /// MBB, as well as that incoming offset and register of its successors match
187 /// outgoing offset and register of the MBB.
188 unsigned verify(MachineFunction &MF);
189};
190
191class CFIInstrInserterLegacy : public MachineFunctionPass {
192public:
193 static char ID;
194
195 CFIInstrInserterLegacy() : MachineFunctionPass(ID) {}
196
197 void getAnalysisUsage(AnalysisUsage &AU) const override {
198 AU.setPreservesAll();
199 MachineFunctionPass::getAnalysisUsage(AU);
200 }
201
202 bool runOnMachineFunction(MachineFunction &MF) override {
203 return CFIInstrInserterImpl().run(MF);
204 }
205};
206} // namespace
207
208char CFIInstrInserterLegacy::ID = 0;
209INITIALIZE_PASS(CFIInstrInserterLegacy, "cfi-instr-inserter",
210 "Check CFA info and insert CFI instructions if needed", false,
211 false)
212FunctionPass *llvm::createCFIInstrInserterLegacy() {
213 return new CFIInstrInserterLegacy();
214}
215
216PreservedAnalyses
217CFIInstrInserterPass::run(MachineFunction &MF,
218 MachineFunctionAnalysisManager &MFAM) {
219 CFIInstrInserterImpl().run(MF);
220 return PreservedAnalyses::all();
221}
222
223void CFIInstrInserterImpl::calculateCFAInfo(MachineFunction &MF) {
224 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
225 // Initial CFA offset value i.e. the one valid at the beginning of the
226 // function.
227 int InitialOffset =
228 MF.getSubtarget().getFrameLowering()->getInitialCFAOffset(MF);
229 // Initial CFA register value i.e. the one valid at the beginning of the
230 // function.
231 Register InitialRegister =
232 MF.getSubtarget().getFrameLowering()->getInitialCFARegister(MF);
233 unsigned DwarfInitialRegister = TRI.getDwarfRegNum(Reg: InitialRegister, isEH: true);
234 unsigned NumRegs = TRI.getNumSupportedRegs(MF);
235
236 // Initialize MBBMap.
237 for (MachineBasicBlock &MBB : MF) {
238 MBBCFAInfo &MBBInfo = MBBVector[MBB.getNumber()];
239 MBBInfo.MBB = &MBB;
240 MBBInfo.IncomingCFAOffset = InitialOffset;
241 MBBInfo.OutgoingCFAOffset = InitialOffset;
242 MBBInfo.IncomingCFARegister = DwarfInitialRegister;
243 MBBInfo.OutgoingCFARegister = DwarfInitialRegister;
244 MBBInfo.IncomingCSRSaved.resize(N: NumRegs);
245 MBBInfo.OutgoingCSRSaved.resize(N: NumRegs);
246 }
247 CSRLocMap.clear();
248
249 // Set in/out cfa info for all blocks in the function. This traversal is based
250 // on the assumption that the first block in the function is the entry block
251 // i.e. that it has initial cfa offset and register values as incoming CFA
252 // information.
253 updateSuccCFAInfo(MBBInfo&: MBBVector[MF.front().getNumber()]);
254}
255
256void CFIInstrInserterImpl::calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo) {
257 // Outgoing cfa offset set by the block.
258 int64_t SetOffset = MBBInfo.IncomingCFAOffset;
259 // Outgoing cfa register set by the block.
260 unsigned SetRegister = MBBInfo.IncomingCFARegister;
261 MachineFunction *MF = MBBInfo.MBB->getParent();
262 const std::vector<MCCFIInstruction> &Instrs = MF->getFrameInstructions();
263 const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo();
264 unsigned NumRegs = TRI.getNumSupportedRegs(*MF);
265 BitVector CSRSaved(NumRegs), CSRRestored(NumRegs);
266
267#ifndef NDEBUG
268 int RememberState = 0;
269#endif
270
271 // Determine cfa offset and register set by the block.
272 for (MachineInstr &MI : *MBBInfo.MBB) {
273 if (MI.isCFIInstruction()) {
274 std::optional<unsigned> CSRReg;
275 std::optional<int64_t> CSROffset;
276 unsigned CFIIndex = MI.getOperand(i: 0).getCFIIndex();
277 const MCCFIInstruction &CFI = Instrs[CFIIndex];
278 switch (CFI.getOperation()) {
279 case MCCFIInstruction::OpDefCfaRegister:
280 SetRegister = CFI.getRegister();
281 break;
282 case MCCFIInstruction::OpDefCfaOffset:
283 SetOffset = CFI.getOffset();
284 break;
285 case MCCFIInstruction::OpAdjustCfaOffset:
286 SetOffset += CFI.getOffset();
287 break;
288 case MCCFIInstruction::OpDefCfa:
289 SetRegister = CFI.getRegister();
290 SetOffset = CFI.getOffset();
291 break;
292 case MCCFIInstruction::OpOffset:
293 CSROffset = CFI.getOffset();
294 break;
295 case MCCFIInstruction::OpRegister:
296 CSRReg = CFI.getRegister2();
297 break;
298 case MCCFIInstruction::OpRelOffset:
299 CSROffset = CFI.getOffset() - SetOffset;
300 break;
301 case MCCFIInstruction::OpRestore:
302 CSRRestored.set(CFI.getRegister());
303 break;
304 case MCCFIInstruction::OpLLVMDefAspaceCfa:
305 // TODO: Add support for handling cfi_def_aspace_cfa.
306#ifndef NDEBUG
307 report_fatal_error(
308 "Support for cfi_llvm_def_aspace_cfa not implemented! Value of CFA "
309 "may be incorrect!\n");
310#endif
311 break;
312 case MCCFIInstruction::OpRememberState:
313 // TODO: Add support for handling cfi_remember_state.
314#ifndef NDEBUG
315 // Currently we need cfi_remember_state and cfi_restore_state to be in
316 // the same BB, so it will not impact outgoing CFA.
317 ++RememberState;
318 if (RememberState != 1)
319 MF->getContext().reportError(
320 SMLoc(),
321 "Support for cfi_remember_state not implemented! Value of CFA "
322 "may be incorrect!\n");
323#endif
324 break;
325 case MCCFIInstruction::OpRestoreState:
326 // TODO: Add support for handling cfi_restore_state.
327#ifndef NDEBUG
328 --RememberState;
329 if (RememberState != 0)
330 MF->getContext().reportError(
331 SMLoc(),
332 "Support for cfi_restore_state not implemented! Value of CFA may "
333 "be incorrect!\n");
334#endif
335 break;
336 // Other CFI directives do not affect CFA value.
337 case MCCFIInstruction::OpUndefined:
338 case MCCFIInstruction::OpSameValue:
339 case MCCFIInstruction::OpEscape:
340 case MCCFIInstruction::OpWindowSave:
341 case MCCFIInstruction::OpNegateRAState:
342 case MCCFIInstruction::OpNegateRAStateWithPC:
343 case MCCFIInstruction::OpLLVMSetRAState:
344 case MCCFIInstruction::OpGnuArgsSize:
345 case MCCFIInstruction::OpLLVMRegisterPair:
346 case MCCFIInstruction::OpLLVMVectorRegisters:
347 case MCCFIInstruction::OpLLVMVectorOffset:
348 case MCCFIInstruction::OpLLVMVectorRegisterMask:
349 case MCCFIInstruction::OpLabel:
350 case MCCFIInstruction::OpValOffset:
351 break;
352 }
353 assert((!CSRReg.has_value() || !CSROffset.has_value()) &&
354 "A register can only be at an offset from CFA or in another "
355 "register, but not both!");
356 CSRSavedLocation CSRLoc;
357 if (CSRReg)
358 CSRLoc = CSRSavedLocation::createRegister(Reg: *CSRReg);
359 else if (CSROffset)
360 CSRLoc = CSRSavedLocation::createCFAOffset(Offset: *CSROffset);
361 if (CSRLoc.isValid()) {
362 auto [It, Inserted] = CSRLocMap.insert(KV: {CFI.getRegister(), CSRLoc});
363 if (!Inserted && It->second != CSRLoc)
364 reportFatalInternalError(
365 reason: "Different saved locations for the same CSR");
366 CSRSaved.set(CFI.getRegister());
367 }
368 }
369 }
370
371#ifndef NDEBUG
372 if (RememberState != 0)
373 MF->getContext().reportError(
374 SMLoc(),
375 "Support for cfi_remember_state not implemented! Value of CFA may be "
376 "incorrect!\n");
377#endif
378
379 MBBInfo.Processed = true;
380
381 // Update outgoing CFA info.
382 MBBInfo.OutgoingCFAOffset = SetOffset;
383 MBBInfo.OutgoingCFARegister = SetRegister;
384
385 // Update outgoing CSR info.
386 BitVector::apply(f: [](auto x, auto y, auto z) { return (x | y) & ~z; },
387 Out&: MBBInfo.OutgoingCSRSaved, Arg: MBBInfo.IncomingCSRSaved, Args: CSRSaved,
388 Args: CSRRestored);
389}
390
391void CFIInstrInserterImpl::updateSuccCFAInfo(MBBCFAInfo &MBBInfo) {
392 SmallVector<MachineBasicBlock *, 4> Stack;
393 Stack.push_back(Elt: MBBInfo.MBB);
394
395 do {
396 MachineBasicBlock *Current = Stack.pop_back_val();
397 MBBCFAInfo &CurrentInfo = MBBVector[Current->getNumber()];
398 calculateOutgoingCFAInfo(MBBInfo&: CurrentInfo);
399 for (auto *Succ : CurrentInfo.MBB->successors()) {
400 MBBCFAInfo &SuccInfo = MBBVector[Succ->getNumber()];
401 if (!SuccInfo.Processed) {
402 SuccInfo.IncomingCFAOffset = CurrentInfo.OutgoingCFAOffset;
403 SuccInfo.IncomingCFARegister = CurrentInfo.OutgoingCFARegister;
404 SuccInfo.IncomingCSRSaved = CurrentInfo.OutgoingCSRSaved;
405 Stack.push_back(Elt: Succ);
406 }
407 }
408 } while (!Stack.empty());
409}
410
411bool CFIInstrInserterImpl::insertCFIInstrs(MachineFunction &MF) {
412 const MBBCFAInfo *PrevMBBInfo = &MBBVector[MF.front().getNumber()];
413 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
414 bool InsertedCFIInstr = false;
415
416 BitVector SetDifference;
417 for (MachineBasicBlock &MBB : MF) {
418 // Skip the first MBB in a function
419 if (MBB.getNumber() == MF.front().getNumber()) continue;
420
421 const MBBCFAInfo &MBBInfo = MBBVector[MBB.getNumber()];
422 auto MBBI = MBBInfo.MBB->begin();
423 DebugLoc DL = MBBInfo.MBB->findDebugLoc(MBBI);
424
425 // If the current MBB will be placed in a unique section, a full DefCfa
426 // must be emitted.
427 const bool ForceFullCFA = MBB.isBeginSection();
428
429 if ((PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset &&
430 PrevMBBInfo->OutgoingCFARegister != MBBInfo.IncomingCFARegister) ||
431 ForceFullCFA) {
432 // If both outgoing offset and register of a previous block don't match
433 // incoming offset and register of this block, or if this block begins a
434 // section, add a def_cfa instruction with the correct offset and
435 // register for this block.
436 unsigned CFIIndex = MF.addFrameInst(Inst: MCCFIInstruction::cfiDefCfa(
437 L: nullptr, Register: MBBInfo.IncomingCFARegister, Offset: getCorrectCFAOffset(MBB: &MBB)));
438 BuildMI(BB&: *MBBInfo.MBB, I: MBBI, MIMD: DL, MCID: TII->get(Opcode: TargetOpcode::CFI_INSTRUCTION))
439 .addCFIIndex(CFIIndex);
440 InsertedCFIInstr = true;
441 } else if (PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset) {
442 // If outgoing offset of a previous block doesn't match incoming offset
443 // of this block, add a def_cfa_offset instruction with the correct
444 // offset for this block.
445 unsigned CFIIndex = MF.addFrameInst(Inst: MCCFIInstruction::cfiDefCfaOffset(
446 L: nullptr, Offset: getCorrectCFAOffset(MBB: &MBB)));
447 BuildMI(BB&: *MBBInfo.MBB, I: MBBI, MIMD: DL, MCID: TII->get(Opcode: TargetOpcode::CFI_INSTRUCTION))
448 .addCFIIndex(CFIIndex);
449 InsertedCFIInstr = true;
450 } else if (PrevMBBInfo->OutgoingCFARegister !=
451 MBBInfo.IncomingCFARegister) {
452 unsigned CFIIndex =
453 MF.addFrameInst(Inst: MCCFIInstruction::createDefCfaRegister(
454 L: nullptr, Register: MBBInfo.IncomingCFARegister));
455 BuildMI(BB&: *MBBInfo.MBB, I: MBBI, MIMD: DL, MCID: TII->get(Opcode: TargetOpcode::CFI_INSTRUCTION))
456 .addCFIIndex(CFIIndex);
457 InsertedCFIInstr = true;
458 }
459
460 if (ForceFullCFA) {
461 MF.getSubtarget().getFrameLowering()->emitCalleeSavedFrameMovesFullCFA(
462 MBB&: *MBBInfo.MBB, MBBI);
463 InsertedCFIInstr = true;
464 PrevMBBInfo = &MBBInfo;
465 continue;
466 }
467
468 BitVector::apply(f: [](auto x, auto y) { return x & ~y; }, Out&: SetDifference,
469 Arg: PrevMBBInfo->OutgoingCSRSaved, Args: MBBInfo.IncomingCSRSaved);
470 for (int Reg : SetDifference.set_bits()) {
471 unsigned CFIIndex =
472 MF.addFrameInst(Inst: MCCFIInstruction::createRestore(L: nullptr, Register: Reg));
473 BuildMI(BB&: *MBBInfo.MBB, I: MBBI, MIMD: DL, MCID: TII->get(Opcode: TargetOpcode::CFI_INSTRUCTION))
474 .addCFIIndex(CFIIndex);
475 InsertedCFIInstr = true;
476 }
477
478 BitVector::apply(f: [](auto x, auto y) { return x & ~y; }, Out&: SetDifference,
479 Arg: MBBInfo.IncomingCSRSaved, Args: PrevMBBInfo->OutgoingCSRSaved);
480 for (int Reg : SetDifference.set_bits()) {
481 auto it = CSRLocMap.find(Val: Reg);
482 assert(it != CSRLocMap.end() && "Reg should have an entry in CSRLocMap");
483 unsigned CFIIndex;
484 CSRSavedLocation RO = it->second;
485 switch (RO.K) {
486 case CSRSavedLocation::CFAOffset: {
487 CFIIndex = MF.addFrameInst(
488 Inst: MCCFIInstruction::createOffset(L: nullptr, Register: Reg, Offset: RO.getOffset()));
489 break;
490 }
491 case CSRSavedLocation::Register: {
492 CFIIndex = MF.addFrameInst(
493 Inst: MCCFIInstruction::createRegister(L: nullptr, Register1: Reg, Register2: RO.getRegister()));
494 break;
495 }
496 default:
497 llvm_unreachable("Invalid CSRSavedLocation!");
498 }
499 BuildMI(BB&: *MBBInfo.MBB, I: MBBI, MIMD: DL, MCID: TII->get(Opcode: TargetOpcode::CFI_INSTRUCTION))
500 .addCFIIndex(CFIIndex);
501 InsertedCFIInstr = true;
502 }
503
504 PrevMBBInfo = &MBBInfo;
505 }
506 return InsertedCFIInstr;
507}
508
509void CFIInstrInserterImpl::reportCFAError(const MBBCFAInfo &Pred,
510 const MBBCFAInfo &Succ) {
511 errs() << "*** Inconsistent CFA register and/or offset between pred and succ "
512 "***\n";
513 errs() << "Pred: " << Pred.MBB->getName() << " #" << Pred.MBB->getNumber()
514 << " in " << Pred.MBB->getParent()->getName()
515 << " outgoing CFA Reg:" << Pred.OutgoingCFARegister << "\n";
516 errs() << "Pred: " << Pred.MBB->getName() << " #" << Pred.MBB->getNumber()
517 << " in " << Pred.MBB->getParent()->getName()
518 << " outgoing CFA Offset:" << Pred.OutgoingCFAOffset << "\n";
519 errs() << "Succ: " << Succ.MBB->getName() << " #" << Succ.MBB->getNumber()
520 << " incoming CFA Reg:" << Succ.IncomingCFARegister << "\n";
521 errs() << "Succ: " << Succ.MBB->getName() << " #" << Succ.MBB->getNumber()
522 << " incoming CFA Offset:" << Succ.IncomingCFAOffset << "\n";
523}
524
525void CFIInstrInserterImpl::reportCSRError(const MBBCFAInfo &Pred,
526 const MBBCFAInfo &Succ) {
527 errs() << "*** Inconsistent CSR Saved between pred and succ in function "
528 << Pred.MBB->getParent()->getName() << " ***\n";
529 errs() << "Pred: " << Pred.MBB->getName() << " #" << Pred.MBB->getNumber()
530 << " outgoing CSR Saved: ";
531 for (int Reg : Pred.OutgoingCSRSaved.set_bits())
532 errs() << Reg << " ";
533 errs() << "\n";
534 errs() << "Succ: " << Succ.MBB->getName() << " #" << Succ.MBB->getNumber()
535 << " incoming CSR Saved: ";
536 for (int Reg : Succ.IncomingCSRSaved.set_bits())
537 errs() << Reg << " ";
538 errs() << "\n";
539}
540
541unsigned CFIInstrInserterImpl::verify(MachineFunction &MF) {
542 unsigned ErrorNum = 0;
543 for (auto *CurrMBB : depth_first(G: &MF)) {
544 const MBBCFAInfo &CurrMBBInfo = MBBVector[CurrMBB->getNumber()];
545 for (MachineBasicBlock *Succ : CurrMBB->successors()) {
546 const MBBCFAInfo &SuccMBBInfo = MBBVector[Succ->getNumber()];
547 // Check that incoming offset and register values of successors match the
548 // outgoing offset and register values of CurrMBB
549 if (SuccMBBInfo.IncomingCFAOffset != CurrMBBInfo.OutgoingCFAOffset ||
550 SuccMBBInfo.IncomingCFARegister != CurrMBBInfo.OutgoingCFARegister) {
551 // Inconsistent offsets/registers are ok for 'noreturn' blocks because
552 // we don't generate epilogues inside such blocks.
553 if (SuccMBBInfo.MBB->succ_empty() && !SuccMBBInfo.MBB->isReturnBlock())
554 continue;
555 reportCFAError(Pred: CurrMBBInfo, Succ: SuccMBBInfo);
556 ErrorNum++;
557 }
558 // Check that IncomingCSRSaved of every successor matches the
559 // OutgoingCSRSaved of CurrMBB
560 if (SuccMBBInfo.IncomingCSRSaved != CurrMBBInfo.OutgoingCSRSaved) {
561 reportCSRError(Pred: CurrMBBInfo, Succ: SuccMBBInfo);
562 ErrorNum++;
563 }
564 }
565 }
566 return ErrorNum;
567}
568