blob: cb9f8f2e01845dbf3266a16281971f0500bd5662 [file] [log] [blame]
Petar Jovanovice2bfcd62018-04-24 10:32:08 +00001//===------ CFIInstrInserter.cpp - Insert additional CFI instructions -----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Petar Jovanovice2bfcd62018-04-24 10:32:08 +00006//
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
Petar Jovanovic3ae0c0e2018-05-07 11:47:48 +000020#include "llvm/ADT/DepthFirstIterator.h"
Wei Mi68d23012020-02-13 09:23:27 -080021#include "llvm/ADT/Optional.h"
22#include "llvm/ADT/SetOperations.h"
Petar Jovanovice2bfcd62018-04-24 10:32:08 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/CodeGen/TargetFrameLowering.h"
28#include "llvm/CodeGen/TargetInstrInfo.h"
29#include "llvm/CodeGen/TargetSubtargetInfo.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080030#include "llvm/InitializePasses.h"
Petar Jovanovice2bfcd62018-04-24 10:32:08 +000031#include "llvm/Target/TargetMachine.h"
32using namespace llvm;
33
Petar Jovanoviccc491572018-05-07 14:09:33 +000034static cl::opt<bool> VerifyCFI("verify-cfiinstrs",
35 cl::desc("Verify Call Frame Information instructions"),
36 cl::init(false),
37 cl::Hidden);
38
Petar Jovanovice2bfcd62018-04-24 10:32:08 +000039namespace {
40class CFIInstrInserter : public MachineFunctionPass {
41 public:
42 static char ID;
43
44 CFIInstrInserter() : MachineFunctionPass(ID) {
45 initializeCFIInstrInserterPass(*PassRegistry::getPassRegistry());
46 }
47
48 void getAnalysisUsage(AnalysisUsage &AU) const override {
49 AU.setPreservesAll();
50 MachineFunctionPass::getAnalysisUsage(AU);
51 }
52
53 bool runOnMachineFunction(MachineFunction &MF) override {
David Candler92aa0c22019-10-31 08:55:57 +000054 if (!MF.needsFrameMoves())
Petar Jovanovice2bfcd62018-04-24 10:32:08 +000055 return false;
56
57 MBBVector.resize(MF.getNumBlockIDs());
58 calculateCFAInfo(MF);
Petar Jovanoviccc491572018-05-07 14:09:33 +000059
60 if (VerifyCFI) {
61 if (unsigned ErrorNum = verify(MF))
62 report_fatal_error("Found " + Twine(ErrorNum) +
63 " in/out CFI information errors.");
64 }
Petar Jovanovice2bfcd62018-04-24 10:32:08 +000065 bool insertedCFI = insertCFIInstrs(MF);
66 MBBVector.clear();
67 return insertedCFI;
68 }
69
70 private:
71 struct MBBCFAInfo {
72 MachineBasicBlock *MBB;
73 /// Value of cfa offset valid at basic block entry.
74 int IncomingCFAOffset = -1;
75 /// Value of cfa offset valid at basic block exit.
76 int OutgoingCFAOffset = -1;
77 /// Value of cfa register valid at basic block entry.
78 unsigned IncomingCFARegister = 0;
79 /// Value of cfa register valid at basic block exit.
80 unsigned OutgoingCFARegister = 0;
Wei Mi68d23012020-02-13 09:23:27 -080081 /// Set of callee saved registers saved at basic block entry.
82 BitVector IncomingCSRSaved;
83 /// Set of callee saved registers saved at basic block exit.
84 BitVector OutgoingCSRSaved;
Petar Jovanovice2bfcd62018-04-24 10:32:08 +000085 /// If in/out cfa offset and register values for this block have already
86 /// been set or not.
87 bool Processed = false;
88 };
89
Wei Mi68d23012020-02-13 09:23:27 -080090#define INVALID_REG UINT_MAX
91#define INVALID_OFFSET INT_MAX
92 /// contains the location where CSR register is saved.
93 struct CSRSavedLocation {
94 CSRSavedLocation(Optional<unsigned> R, Optional<int> O)
95 : Reg(R), Offset(O) {}
96 Optional<unsigned> Reg;
97 Optional<int> Offset;
98 };
99
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000100 /// Contains cfa offset and register values valid at entry and exit of basic
101 /// blocks.
102 std::vector<MBBCFAInfo> MBBVector;
103
Wei Mi68d23012020-02-13 09:23:27 -0800104 /// Map the callee save registers to the locations where they are saved.
105 SmallDenseMap<unsigned, CSRSavedLocation, 16> CSRLocMap;
106
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000107 /// Calculate cfa offset and register values valid at entry and exit for all
108 /// basic blocks in a function.
109 void calculateCFAInfo(MachineFunction &MF);
110 /// Calculate cfa offset and register values valid at basic block exit by
111 /// checking the block for CFI instructions. Block's incoming CFA info remains
112 /// the same.
113 void calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo);
114 /// Update in/out cfa offset and register values for successors of the basic
115 /// block.
116 void updateSuccCFAInfo(MBBCFAInfo &MBBInfo);
117
118 /// Check if incoming CFA information of a basic block matches outgoing CFA
119 /// information of the previous block. If it doesn't, insert CFI instruction
120 /// at the beginning of the block that corrects the CFA calculation rule for
121 /// that block.
122 bool insertCFIInstrs(MachineFunction &MF);
123 /// Return the cfa offset value that should be set at the beginning of a MBB
124 /// if needed. The negated value is needed when creating CFI instructions that
125 /// set absolute offset.
126 int getCorrectCFAOffset(MachineBasicBlock *MBB) {
Fangrui Song7e49dc62020-05-22 15:18:05 -0700127 return MBBVector[MBB->getNumber()].IncomingCFAOffset;
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000128 }
129
Wei Mi68d23012020-02-13 09:23:27 -0800130 void reportCFAError(const MBBCFAInfo &Pred, const MBBCFAInfo &Succ);
131 void reportCSRError(const MBBCFAInfo &Pred, const MBBCFAInfo &Succ);
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000132 /// Go through each MBB in a function and check that outgoing offset and
133 /// register of its predecessors match incoming offset and register of that
134 /// MBB, as well as that incoming offset and register of its successors match
135 /// outgoing offset and register of the MBB.
136 unsigned verify(MachineFunction &MF);
137};
138} // namespace
139
140char CFIInstrInserter::ID = 0;
141INITIALIZE_PASS(CFIInstrInserter, "cfi-instr-inserter",
142 "Check CFA info and insert CFI instructions if needed", false,
143 false)
144FunctionPass *llvm::createCFIInstrInserter() { return new CFIInstrInserter(); }
145
146void CFIInstrInserter::calculateCFAInfo(MachineFunction &MF) {
147 // Initial CFA offset value i.e. the one valid at the beginning of the
148 // function.
149 int InitialOffset =
150 MF.getSubtarget().getFrameLowering()->getInitialCFAOffset(MF);
151 // Initial CFA register value i.e. the one valid at the beginning of the
152 // function.
153 unsigned InitialRegister =
154 MF.getSubtarget().getFrameLowering()->getInitialCFARegister(MF);
Wei Mi68d23012020-02-13 09:23:27 -0800155 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
156 unsigned NumRegs = TRI.getNumRegs();
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000157
158 // Initialize MBBMap.
159 for (MachineBasicBlock &MBB : MF) {
160 MBBCFAInfo MBBInfo;
161 MBBInfo.MBB = &MBB;
162 MBBInfo.IncomingCFAOffset = InitialOffset;
163 MBBInfo.OutgoingCFAOffset = InitialOffset;
164 MBBInfo.IncomingCFARegister = InitialRegister;
165 MBBInfo.OutgoingCFARegister = InitialRegister;
Wei Mi68d23012020-02-13 09:23:27 -0800166 MBBInfo.IncomingCSRSaved.resize(NumRegs);
167 MBBInfo.OutgoingCSRSaved.resize(NumRegs);
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000168 MBBVector[MBB.getNumber()] = MBBInfo;
169 }
Wei Mi68d23012020-02-13 09:23:27 -0800170 CSRLocMap.clear();
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000171
172 // Set in/out cfa info for all blocks in the function. This traversal is based
173 // on the assumption that the first block in the function is the entry block
174 // i.e. that it has initial cfa offset and register values as incoming CFA
175 // information.
Fangrui Songde172ef2020-05-23 14:00:33 -0700176 updateSuccCFAInfo(MBBVector[MF.front().getNumber()]);
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000177}
178
179void CFIInstrInserter::calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo) {
180 // Outgoing cfa offset set by the block.
181 int SetOffset = MBBInfo.IncomingCFAOffset;
182 // Outgoing cfa register set by the block.
183 unsigned SetRegister = MBBInfo.IncomingCFARegister;
Wei Mi68d23012020-02-13 09:23:27 -0800184 MachineFunction *MF = MBBInfo.MBB->getParent();
185 const std::vector<MCCFIInstruction> &Instrs = MF->getFrameInstructions();
186 const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo();
187 unsigned NumRegs = TRI.getNumRegs();
188 BitVector CSRSaved(NumRegs), CSRRestored(NumRegs);
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000189
190 // Determine cfa offset and register set by the block.
191 for (MachineInstr &MI : *MBBInfo.MBB) {
192 if (MI.isCFIInstruction()) {
Wei Mi68d23012020-02-13 09:23:27 -0800193 Optional<unsigned> CSRReg;
194 Optional<int> CSROffset;
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000195 unsigned CFIIndex = MI.getOperand(0).getCFIIndex();
196 const MCCFIInstruction &CFI = Instrs[CFIIndex];
197 switch (CFI.getOperation()) {
198 case MCCFIInstruction::OpDefCfaRegister:
199 SetRegister = CFI.getRegister();
200 break;
201 case MCCFIInstruction::OpDefCfaOffset:
202 SetOffset = CFI.getOffset();
203 break;
204 case MCCFIInstruction::OpAdjustCfaOffset:
205 SetOffset += CFI.getOffset();
206 break;
207 case MCCFIInstruction::OpDefCfa:
208 SetRegister = CFI.getRegister();
209 SetOffset = CFI.getOffset();
210 break;
Wei Mi68d23012020-02-13 09:23:27 -0800211 case MCCFIInstruction::OpOffset:
212 CSROffset = CFI.getOffset();
213 break;
214 case MCCFIInstruction::OpRegister:
215 CSRReg = CFI.getRegister2();
216 break;
217 case MCCFIInstruction::OpRelOffset:
218 CSROffset = CFI.getOffset() - SetOffset;
219 break;
220 case MCCFIInstruction::OpRestore:
221 CSRRestored.set(CFI.getRegister());
222 break;
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000223 case MCCFIInstruction::OpRememberState:
224 // TODO: Add support for handling cfi_remember_state.
225#ifndef NDEBUG
226 report_fatal_error(
227 "Support for cfi_remember_state not implemented! Value of CFA "
228 "may be incorrect!\n");
229#endif
230 break;
231 case MCCFIInstruction::OpRestoreState:
232 // TODO: Add support for handling cfi_restore_state.
233#ifndef NDEBUG
234 report_fatal_error(
235 "Support for cfi_restore_state not implemented! Value of CFA may "
236 "be incorrect!\n");
237#endif
238 break;
239 // Other CFI directives do not affect CFA value.
Wei Mia0357262020-03-19 22:45:27 -0700240 case MCCFIInstruction::OpUndefined:
Wei Mi68d23012020-02-13 09:23:27 -0800241 case MCCFIInstruction::OpSameValue:
242 case MCCFIInstruction::OpEscape:
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000243 case MCCFIInstruction::OpWindowSave:
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000244 case MCCFIInstruction::OpNegateRAState:
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000245 case MCCFIInstruction::OpGnuArgsSize:
246 break;
247 }
Wei Mi68d23012020-02-13 09:23:27 -0800248 if (CSRReg || CSROffset) {
249 auto It = CSRLocMap.find(CFI.getRegister());
250 if (It == CSRLocMap.end()) {
251 CSRLocMap.insert(
252 {CFI.getRegister(), CSRSavedLocation(CSRReg, CSROffset)});
253 } else if (It->second.Reg != CSRReg || It->second.Offset != CSROffset) {
254 llvm_unreachable("Different saved locations for the same CSR");
255 }
256 CSRSaved.set(CFI.getRegister());
257 }
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000258 }
259 }
260
261 MBBInfo.Processed = true;
262
263 // Update outgoing CFA info.
264 MBBInfo.OutgoingCFAOffset = SetOffset;
265 MBBInfo.OutgoingCFARegister = SetRegister;
Wei Mi68d23012020-02-13 09:23:27 -0800266
267 // Update outgoing CSR info.
268 MBBInfo.OutgoingCSRSaved = MBBInfo.IncomingCSRSaved;
269 MBBInfo.OutgoingCSRSaved |= CSRSaved;
270 MBBInfo.OutgoingCSRSaved.reset(CSRRestored);
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000271}
272
273void CFIInstrInserter::updateSuccCFAInfo(MBBCFAInfo &MBBInfo) {
Sanjoy Das82105e22018-05-11 15:54:46 +0000274 SmallVector<MachineBasicBlock *, 4> Stack;
275 Stack.push_back(MBBInfo.MBB);
276
277 do {
278 MachineBasicBlock *Current = Stack.pop_back_val();
279 MBBCFAInfo &CurrentInfo = MBBVector[Current->getNumber()];
Sanjoy Das82105e22018-05-11 15:54:46 +0000280 calculateOutgoingCFAInfo(CurrentInfo);
281 for (auto *Succ : CurrentInfo.MBB->successors()) {
282 MBBCFAInfo &SuccInfo = MBBVector[Succ->getNumber()];
283 if (!SuccInfo.Processed) {
284 SuccInfo.IncomingCFAOffset = CurrentInfo.OutgoingCFAOffset;
285 SuccInfo.IncomingCFARegister = CurrentInfo.OutgoingCFARegister;
Wei Mi68d23012020-02-13 09:23:27 -0800286 SuccInfo.IncomingCSRSaved = CurrentInfo.OutgoingCSRSaved;
Sanjoy Das82105e22018-05-11 15:54:46 +0000287 Stack.push_back(Succ);
288 }
289 }
290 } while (!Stack.empty());
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000291}
292
293bool CFIInstrInserter::insertCFIInstrs(MachineFunction &MF) {
294 const MBBCFAInfo *PrevMBBInfo = &MBBVector[MF.front().getNumber()];
295 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
296 bool InsertedCFIInstr = false;
297
298 for (MachineBasicBlock &MBB : MF) {
299 // Skip the first MBB in a function
300 if (MBB.getNumber() == MF.front().getNumber()) continue;
301
302 const MBBCFAInfo &MBBInfo = MBBVector[MBB.getNumber()];
303 auto MBBI = MBBInfo.MBB->begin();
304 DebugLoc DL = MBBInfo.MBB->findDebugLoc(MBBI);
305
306 if (PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset) {
307 // If both outgoing offset and register of a previous block don't match
308 // incoming offset and register of this block, add a def_cfa instruction
309 // with the correct offset and register for this block.
310 if (PrevMBBInfo->OutgoingCFARegister != MBBInfo.IncomingCFARegister) {
Fangrui Song7e49dc62020-05-22 15:18:05 -0700311 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfa(
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000312 nullptr, MBBInfo.IncomingCFARegister, getCorrectCFAOffset(&MBB)));
313 BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
314 .addCFIIndex(CFIIndex);
315 // If outgoing offset of a previous block doesn't match incoming offset
316 // of this block, add a def_cfa_offset instruction with the correct
317 // offset for this block.
318 } else {
Fangrui Song0840d722020-05-22 15:51:24 -0700319 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(
320 nullptr, getCorrectCFAOffset(&MBB)));
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000321 BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
322 .addCFIIndex(CFIIndex);
323 }
324 InsertedCFIInstr = true;
325 // If outgoing register of a previous block doesn't match incoming
326 // register of this block, add a def_cfa_register instruction with the
327 // correct register for this block.
328 } else if (PrevMBBInfo->OutgoingCFARegister !=
329 MBBInfo.IncomingCFARegister) {
330 unsigned CFIIndex =
331 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
332 nullptr, MBBInfo.IncomingCFARegister));
333 BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
334 .addCFIIndex(CFIIndex);
335 InsertedCFIInstr = true;
336 }
Wei Mi68d23012020-02-13 09:23:27 -0800337
338 BitVector SetDifference = PrevMBBInfo->OutgoingCSRSaved;
339 SetDifference.reset(MBBInfo.IncomingCSRSaved);
340 for (int Reg : SetDifference.set_bits()) {
341 unsigned CFIIndex =
342 MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
343 BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
344 .addCFIIndex(CFIIndex);
345 InsertedCFIInstr = true;
346 }
347
348 SetDifference = MBBInfo.IncomingCSRSaved;
349 SetDifference.reset(PrevMBBInfo->OutgoingCSRSaved);
350 for (int Reg : SetDifference.set_bits()) {
351 auto it = CSRLocMap.find(Reg);
352 assert(it != CSRLocMap.end() && "Reg should have an entry in CSRLocMap");
353 unsigned CFIIndex;
354 CSRSavedLocation RO = it->second;
355 if (!RO.Reg && RO.Offset) {
356 CFIIndex = MF.addFrameInst(
357 MCCFIInstruction::createOffset(nullptr, Reg, *RO.Offset));
358 } else if (RO.Reg && !RO.Offset) {
359 CFIIndex = MF.addFrameInst(
360 MCCFIInstruction::createRegister(nullptr, Reg, *RO.Reg));
361 } else {
362 llvm_unreachable("RO.Reg and RO.Offset cannot both be valid/invalid");
363 }
364 BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
365 .addCFIIndex(CFIIndex);
366 InsertedCFIInstr = true;
367 }
368
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000369 PrevMBBInfo = &MBBInfo;
370 }
371 return InsertedCFIInstr;
372}
373
Wei Mi68d23012020-02-13 09:23:27 -0800374void CFIInstrInserter::reportCFAError(const MBBCFAInfo &Pred,
375 const MBBCFAInfo &Succ) {
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000376 errs() << "*** Inconsistent CFA register and/or offset between pred and succ "
377 "***\n";
Sanjoy Das82105e22018-05-11 15:54:46 +0000378 errs() << "Pred: " << Pred.MBB->getName() << " #" << Pred.MBB->getNumber()
379 << " in " << Pred.MBB->getParent()->getName()
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000380 << " outgoing CFA Reg:" << Pred.OutgoingCFARegister << "\n";
Sanjoy Das82105e22018-05-11 15:54:46 +0000381 errs() << "Pred: " << Pred.MBB->getName() << " #" << Pred.MBB->getNumber()
382 << " in " << Pred.MBB->getParent()->getName()
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000383 << " outgoing CFA Offset:" << Pred.OutgoingCFAOffset << "\n";
Sanjoy Das82105e22018-05-11 15:54:46 +0000384 errs() << "Succ: " << Succ.MBB->getName() << " #" << Succ.MBB->getNumber()
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000385 << " incoming CFA Reg:" << Succ.IncomingCFARegister << "\n";
Sanjoy Das82105e22018-05-11 15:54:46 +0000386 errs() << "Succ: " << Succ.MBB->getName() << " #" << Succ.MBB->getNumber()
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000387 << " incoming CFA Offset:" << Succ.IncomingCFAOffset << "\n";
388}
389
Wei Mi68d23012020-02-13 09:23:27 -0800390void CFIInstrInserter::reportCSRError(const MBBCFAInfo &Pred,
391 const MBBCFAInfo &Succ) {
392 errs() << "*** Inconsistent CSR Saved between pred and succ in function "
393 << Pred.MBB->getParent()->getName() << " ***\n";
394 errs() << "Pred: " << Pred.MBB->getName() << " #" << Pred.MBB->getNumber()
395 << " outgoing CSR Saved: ";
396 for (int Reg : Pred.OutgoingCSRSaved.set_bits())
397 errs() << Reg << " ";
398 errs() << "\n";
399 errs() << "Succ: " << Succ.MBB->getName() << " #" << Succ.MBB->getNumber()
400 << " incoming CSR Saved: ";
401 for (int Reg : Succ.IncomingCSRSaved.set_bits())
402 errs() << Reg << " ";
403 errs() << "\n";
404}
405
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000406unsigned CFIInstrInserter::verify(MachineFunction &MF) {
407 unsigned ErrorNum = 0;
Petar Jovanovic3ae0c0e2018-05-07 11:47:48 +0000408 for (auto *CurrMBB : depth_first(&MF)) {
409 const MBBCFAInfo &CurrMBBInfo = MBBVector[CurrMBB->getNumber()];
410 for (MachineBasicBlock *Succ : CurrMBB->successors()) {
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000411 const MBBCFAInfo &SuccMBBInfo = MBBVector[Succ->getNumber()];
412 // Check that incoming offset and register values of successors match the
413 // outgoing offset and register values of CurrMBB
414 if (SuccMBBInfo.IncomingCFAOffset != CurrMBBInfo.OutgoingCFAOffset ||
415 SuccMBBInfo.IncomingCFARegister != CurrMBBInfo.OutgoingCFARegister) {
Vladimir Stefanovic7e58ebf2018-08-30 17:31:38 +0000416 // Inconsistent offsets/registers are ok for 'noreturn' blocks because
417 // we don't generate epilogues inside such blocks.
418 if (SuccMBBInfo.MBB->succ_empty() && !SuccMBBInfo.MBB->isReturnBlock())
419 continue;
Wei Mi68d23012020-02-13 09:23:27 -0800420 reportCFAError(CurrMBBInfo, SuccMBBInfo);
421 ErrorNum++;
422 }
423 // Check that IncomingCSRSaved of every successor matches the
424 // OutgoingCSRSaved of CurrMBB
425 if (SuccMBBInfo.IncomingCSRSaved != CurrMBBInfo.OutgoingCSRSaved) {
426 reportCSRError(CurrMBBInfo, SuccMBBInfo);
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000427 ErrorNum++;
428 }
429 }
430 }
431 return ErrorNum;
432}