Lines Matching +full:block +full:- +full:offset

1 //===------ CFIInstrInserter.cpp - Insert additional CFI instructions -----===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
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
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
18 //===----------------------------------------------------------------------===//
31 static cl::opt<bool> VerifyCFI("verify-cfiinstrs",
70 /// Value of cfa offset valid at basic block entry.
71 int64_t IncomingCFAOffset = -1;
72 /// Value of cfa offset valid at basic block exit.
73 int64_t OutgoingCFAOffset = -1;
74 /// Value of cfa register valid at basic block entry.
76 /// Value of cfa register valid at basic block exit.
78 /// Set of callee saved registers saved at basic block entry.
80 /// Set of callee saved registers saved at basic block exit.
82 /// If in/out cfa offset and register values for this block have already
92 : Reg(R), Offset(O) {}
94 std::optional<int> Offset;
97 /// Contains cfa offset and register values valid at entry and exit of basic
104 /// Calculate cfa offset and register values valid at entry and exit for all
107 /// Calculate cfa offset and register values valid at basic block exit by
108 /// checking the block for CFI instructions. Block's incoming CFA info remains
111 /// Update in/out cfa offset and register values for successors of the basic
112 /// block.
115 /// Check if incoming CFA information of a basic block matches outgoing CFA
116 /// information of the previous block. If it doesn't, insert CFI instruction
117 /// at the beginning of the block that corrects the CFA calculation rule for
118 /// that block.
120 /// Return the cfa offset value that should be set at the beginning of a MBB
122 /// set absolute offset.
124 return MBBVector[MBB->getNumber()].IncomingCFAOffset;
129 /// Go through each MBB in a function and check that outgoing offset and
130 /// register of its predecessors match incoming offset and register of that
131 /// MBB, as well as that incoming offset and register of its successors match
132 /// outgoing offset and register of the MBB.
138 INITIALIZE_PASS(CFIInstrInserter, "cfi-instr-inserter",
145 // Initial CFA offset value i.e. the one valid at the beginning of the
148 MF.getSubtarget().getFrameLowering()->getInitialCFAOffset(MF);
152 MF.getSubtarget().getFrameLowering()->getInitialCFARegister(MF);
170 // on the assumption that the first block in the function is the entry block
171 // i.e. that it has initial cfa offset and register values as incoming CFA
177 // Outgoing cfa offset set by the block.
179 // Outgoing cfa register set by the block.
181 MachineFunction *MF = MBBInfo.MBB->getParent();
182 const std::vector<MCCFIInstruction> &Instrs = MF->getFrameInstructions();
183 const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo();
187 // Determine cfa offset and register set by the block.
215 CSROffset = CFI.getOffset() - SetOffset;
259 } else if (It->second.Reg != CSRReg || It->second.Offset != CSROffset) {
285 MBBCFAInfo &CurrentInfo = MBBVector[Current->getNumber()];
287 for (auto *Succ : CurrentInfo.MBB->successors()) {
288 MBBCFAInfo &SuccInfo = MBBVector[Succ->getNumber()];
310 auto MBBI = MBBInfo.MBB->begin();
311 DebugLoc DL = MBBInfo.MBB->findDebugLoc(MBBI);
317 if ((PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset &&
318 PrevMBBInfo->OutgoingCFARegister != MBBInfo.IncomingCFARegister) ||
320 // If both outgoing offset and register of a previous block don't match
321 // incoming offset and register of this block, or if this block begins a
322 // section, add a def_cfa instruction with the correct offset and
323 // register for this block.
326 BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
329 } else if (PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset) {
330 // If outgoing offset of a previous block doesn't match incoming offset
331 // of this block, add a def_cfa_offset instruction with the correct
332 // offset for this block.
335 BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
338 } else if (PrevMBBInfo->OutgoingCFARegister !=
343 BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
349 MF.getSubtarget().getFrameLowering()->emitCalleeSavedFrameMovesFullCFA(
357 PrevMBBInfo->OutgoingCSRSaved, MBBInfo.IncomingCSRSaved);
361 BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
367 MBBInfo.IncomingCSRSaved, PrevMBBInfo->OutgoingCSRSaved);
372 CSRSavedLocation RO = it->second;
373 if (!RO.Reg && RO.Offset) {
375 MCCFIInstruction::createOffset(nullptr, Reg, *RO.Offset));
376 } else if (RO.Reg && !RO.Offset) {
380 llvm_unreachable("RO.Reg and RO.Offset cannot both be valid/invalid");
382 BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
394 errs() << "*** Inconsistent CFA register and/or offset between pred and succ "
396 errs() << "Pred: " << Pred.MBB->getName() << " #" << Pred.MBB->getNumber()
397 << " in " << Pred.MBB->getParent()->getName()
399 errs() << "Pred: " << Pred.MBB->getName() << " #" << Pred.MBB->getNumber()
400 << " in " << Pred.MBB->getParent()->getName()
401 << " outgoing CFA Offset:" << Pred.OutgoingCFAOffset << "\n";
402 errs() << "Succ: " << Succ.MBB->getName() << " #" << Succ.MBB->getNumber()
404 errs() << "Succ: " << Succ.MBB->getName() << " #" << Succ.MBB->getNumber()
405 << " incoming CFA Offset:" << Succ.IncomingCFAOffset << "\n";
411 << Pred.MBB->getParent()->getName() << " ***\n";
412 errs() << "Pred: " << Pred.MBB->getName() << " #" << Pred.MBB->getNumber()
417 errs() << "Succ: " << Succ.MBB->getName() << " #" << Succ.MBB->getNumber()
427 const MBBCFAInfo &CurrMBBInfo = MBBVector[CurrMBB->getNumber()];
428 for (MachineBasicBlock *Succ : CurrMBB->successors()) {
429 const MBBCFAInfo &SuccMBBInfo = MBBVector[Succ->getNumber()];
430 // Check that incoming offset and register values of successors match the
431 // outgoing offset and register values of CurrMBB
436 if (SuccMBBInfo.MBB->succ_empty() && !SuccMBBInfo.MBB->isReturnBlock())