33 cl::desc(
"Verify Call Frame Information instructions"),
46 void getAnalysisUsage(AnalysisUsage &AU)
const override {
51 bool runOnMachineFunction(MachineFunction &MF)
override {
59 if (
unsigned ErrorNum =
verify(MF))
61 " in/out CFI information errors.");
63 bool insertedCFI = insertCFIInstrs(MF);
70 MachineBasicBlock *MBB;
72 int64_t IncomingCFAOffset = -1;
74 int64_t OutgoingCFAOffset = -1;
76 unsigned IncomingCFARegister = 0;
78 unsigned OutgoingCFARegister = 0;
80 BitVector IncomingCSRSaved;
82 BitVector OutgoingCSRSaved;
85 bool Processed =
false;
88#define INVALID_REG UINT_MAX
89#define INVALID_OFFSET INT_MAX
91 class CSRSavedLocation {
105 CSRSavedLocation() {}
108 CSRSavedLocation
Loc;
109 Loc.K = Kind::CFAOffset;
114 static CSRSavedLocation createRegister(
unsigned Reg) {
115 CSRSavedLocation
Loc;
116 Loc.K = Kind::Register;
121 bool isValid()
const {
return K != Kind::Invalid; }
123 unsigned getRegister()
const {
124 assert(K == Kind::Register);
129 assert(K == Kind::CFAOffset);
140 return getRegister() ==
RHS.getRegister();
141 case Kind::CFAOffset:
147 return !(*
this ==
RHS);
153 std::vector<MBBCFAInfo> MBBVector;
156 SmallDenseMap<unsigned, CSRSavedLocation, 16> CSRLocMap;
160 void calculateCFAInfo(MachineFunction &MF);
164 void calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo);
167 void updateSuccCFAInfo(MBBCFAInfo &MBBInfo);
173 bool insertCFIInstrs(MachineFunction &MF);
177 int64_t getCorrectCFAOffset(MachineBasicBlock *
MBB) {
181 void reportCFAError(
const MBBCFAInfo &Pred,
const MBBCFAInfo &Succ);
182 void reportCSRError(
const MBBCFAInfo &Pred,
const MBBCFAInfo &Succ);
187 unsigned verify(MachineFunction &MF);
191char CFIInstrInserter::ID = 0;
193 "Check CFA info and insert CFI instructions if needed",
false,
207 unsigned DwarfInitialRegister =
TRI.getDwarfRegNum(InitialRegister,
true);
208 unsigned NumRegs =
TRI.getNumSupportedRegs(MF);
211 for (MachineBasicBlock &
MBB : MF) {
214 MBBInfo.IncomingCFAOffset = InitialOffset;
215 MBBInfo.OutgoingCFAOffset = InitialOffset;
216 MBBInfo.IncomingCFARegister = DwarfInitialRegister;
217 MBBInfo.OutgoingCFARegister = DwarfInitialRegister;
218 MBBInfo.IncomingCSRSaved.resize(NumRegs);
219 MBBInfo.OutgoingCSRSaved.resize(NumRegs);
227 updateSuccCFAInfo(
MBBVector[MF.front().getNumber()]);
230void CFIInstrInserter::calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo) {
232 int64_t SetOffset = MBBInfo.IncomingCFAOffset;
234 unsigned SetRegister = MBBInfo.IncomingCFARegister;
235 MachineFunction *MF = MBBInfo.MBB->getParent();
238 unsigned NumRegs =
TRI.getNumSupportedRegs(*MF);
239 BitVector CSRSaved(NumRegs), CSRRestored(NumRegs);
242 int RememberState = 0;
246 for (MachineInstr &
MI : *MBBInfo.MBB) {
247 if (
MI.isCFIInstruction()) {
248 std::optional<unsigned> CSRReg;
249 std::optional<int64_t> CSROffset;
250 unsigned CFIIndex =
MI.getOperand(0).getCFIIndex();
251 const MCCFIInstruction &CFI = Instrs[CFIIndex];
282 "Support for cfi_llvm_def_aspace_cfa not implemented! Value of CFA "
283 "may be incorrect!\n");
292 if (RememberState != 1)
295 "Support for cfi_remember_state not implemented! Value of CFA "
296 "may be incorrect!\n");
303 if (RememberState != 0)
306 "Support for cfi_restore_state not implemented! Value of CFA may "
322 assert((!CSRReg.has_value() || !CSROffset.has_value()) &&
323 "A register can only be at an offset from CFA or in another "
324 "register, but not both!");
325 CSRSavedLocation CSRLoc;
327 CSRLoc = CSRSavedLocation::createRegister(*CSRReg);
329 CSRLoc = CSRSavedLocation::createCFAOffset(*CSROffset);
330 if (CSRLoc.isValid()) {
332 if (!Inserted && It->second != CSRLoc)
334 "Different saved locations for the same CSR");
341 if (RememberState != 0)
344 "Support for cfi_remember_state not implemented! Value of CFA may be "
348 MBBInfo.Processed =
true;
351 MBBInfo.OutgoingCFAOffset = SetOffset;
352 MBBInfo.OutgoingCFARegister = SetRegister;
356 MBBInfo.OutgoingCSRSaved, MBBInfo.IncomingCSRSaved, CSRSaved,
360void CFIInstrInserter::updateSuccCFAInfo(MBBCFAInfo &MBBInfo) {
361 SmallVector<MachineBasicBlock *, 4>
Stack;
362 Stack.push_back(MBBInfo.MBB);
365 MachineBasicBlock *Current =
Stack.pop_back_val();
367 calculateOutgoingCFAInfo(CurrentInfo);
368 for (
auto *Succ : CurrentInfo.MBB->successors()) {
369 MBBCFAInfo &SuccInfo =
MBBVector[Succ->getNumber()];
370 if (!SuccInfo.Processed) {
371 SuccInfo.IncomingCFAOffset = CurrentInfo.OutgoingCFAOffset;
372 SuccInfo.IncomingCFARegister = CurrentInfo.OutgoingCFARegister;
373 SuccInfo.IncomingCSRSaved = CurrentInfo.OutgoingCSRSaved;
374 Stack.push_back(Succ);
377 }
while (!
Stack.empty());
380bool CFIInstrInserter::insertCFIInstrs(MachineFunction &MF) {
383 bool InsertedCFIInstr =
false;
385 BitVector SetDifference;
386 for (MachineBasicBlock &
MBB : MF) {
388 if (
MBB.
getNumber() == MF.front().getNumber())
continue;
391 auto MBBI = MBBInfo.MBB->begin();
398 if ((PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset &&
399 PrevMBBInfo->OutgoingCFARegister != MBBInfo.IncomingCFARegister) ||
406 nullptr, MBBInfo.IncomingCFARegister, getCorrectCFAOffset(&
MBB)));
409 InsertedCFIInstr =
true;
410 }
else if (PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset) {
415 nullptr, getCorrectCFAOffset(&
MBB)));
418 InsertedCFIInstr =
true;
419 }
else if (PrevMBBInfo->OutgoingCFARegister !=
420 MBBInfo.IncomingCFARegister) {
423 nullptr, MBBInfo.IncomingCFARegister));
426 InsertedCFIInstr =
true;
430 MF.getSubtarget().getFrameLowering()->emitCalleeSavedFrameMovesFullCFA(
432 InsertedCFIInstr =
true;
433 PrevMBBInfo = &MBBInfo;
438 PrevMBBInfo->OutgoingCSRSaved, MBBInfo.IncomingCSRSaved);
444 InsertedCFIInstr =
true;
448 MBBInfo.IncomingCSRSaved, PrevMBBInfo->OutgoingCSRSaved);
450 auto it = CSRLocMap.find(
Reg);
451 assert(it != CSRLocMap.end() &&
"Reg should have an entry in CSRLocMap");
453 CSRSavedLocation RO = it->second;
455 case CSRSavedLocation::CFAOffset: {
456 CFIIndex = MF.addFrameInst(
460 case CSRSavedLocation::Register: {
461 CFIIndex = MF.addFrameInst(
470 InsertedCFIInstr =
true;
473 PrevMBBInfo = &MBBInfo;
475 return InsertedCFIInstr;
478void CFIInstrInserter::reportCFAError(
const MBBCFAInfo &Pred,
479 const MBBCFAInfo &Succ) {
480 errs() <<
"*** Inconsistent CFA register and/or offset between pred and succ "
482 errs() <<
"Pred: " << Pred.MBB->getName() <<
" #" << Pred.MBB->getNumber()
483 <<
" in " << Pred.MBB->getParent()->getName()
484 <<
" outgoing CFA Reg:" << Pred.OutgoingCFARegister <<
"\n";
485 errs() <<
"Pred: " << Pred.MBB->getName() <<
" #" << Pred.MBB->getNumber()
486 <<
" in " << Pred.MBB->getParent()->getName()
487 <<
" outgoing CFA Offset:" << Pred.OutgoingCFAOffset <<
"\n";
488 errs() <<
"Succ: " << Succ.MBB->getName() <<
" #" << Succ.MBB->getNumber()
489 <<
" incoming CFA Reg:" << Succ.IncomingCFARegister <<
"\n";
490 errs() <<
"Succ: " << Succ.MBB->getName() <<
" #" << Succ.MBB->getNumber()
491 <<
" incoming CFA Offset:" << Succ.IncomingCFAOffset <<
"\n";
494void CFIInstrInserter::reportCSRError(
const MBBCFAInfo &Pred,
495 const MBBCFAInfo &Succ) {
496 errs() <<
"*** Inconsistent CSR Saved between pred and succ in function "
497 << Pred.MBB->getParent()->getName() <<
" ***\n";
498 errs() <<
"Pred: " << Pred.MBB->getName() <<
" #" << Pred.MBB->getNumber()
499 <<
" outgoing CSR Saved: ";
500 for (
int Reg : Pred.OutgoingCSRSaved.set_bits())
503 errs() <<
"Succ: " << Succ.MBB->getName() <<
" #" << Succ.MBB->getNumber()
504 <<
" incoming CSR Saved: ";
505 for (
int Reg : Succ.IncomingCSRSaved.set_bits())
510unsigned CFIInstrInserter::verify(MachineFunction &MF) {
511 unsigned ErrorNum = 0;
513 const MBBCFAInfo &CurrMBBInfo =
MBBVector[CurrMBB->getNumber()];
514 for (MachineBasicBlock *Succ : CurrMBB->successors()) {
515 const MBBCFAInfo &SuccMBBInfo =
MBBVector[Succ->getNumber()];
518 if (SuccMBBInfo.IncomingCFAOffset != CurrMBBInfo.OutgoingCFAOffset ||
519 SuccMBBInfo.IncomingCFARegister != CurrMBBInfo.OutgoingCFARegister) {
522 if (SuccMBBInfo.MBB->succ_empty() && !SuccMBBInfo.MBB->isReturnBlock())
524 reportCFAError(CurrMBBInfo, SuccMBBInfo);
529 if (SuccMBBInfo.IncomingCSRSaved != CurrMBBInfo.OutgoingCSRSaved) {
530 reportCSRError(CurrMBBInfo, SuccMBBInfo);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static cl::opt< bool > VerifyCFI("verify-cfiinstrs", cl::desc("Verify Call Frame Information instructions"), cl::init(false), cl::Hidden)
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
const HexagonInstrInfo * TII
Register const TargetRegisterInfo * TRI
Promote Memory to Register
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
SmallVector< MachineBasicBlock *, 4 > MBBVector
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
void setPreservesAll()
Set by analyses that do not transform their input at all.
iterator_range< const_set_bits_iterator > set_bits() const
static BitVector & apply(F &&f, BitVector &Out, BitVector const &Arg, ArgTys const &...Args)
FunctionPass class - This class is used to implement most global optimizations.
static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_def_cfa_register modifies a rule for computing CFA.
static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_restore says that the rule for Register is now the same as it was at the beginning of the functi...
unsigned getRegister2() const
unsigned getRegister() const
static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1, unsigned Register2, SMLoc Loc={})
.cfi_register Previous value of Register1 is saved in register Register2.
static MCCFIInstruction cfiDefCfa(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it.
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_offset Previous value of Register is saved at offset Offset from CFA.
OpType getOperation() const
static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa_offset modifies a rule for computing CFA.
int64_t getOffset() const
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
bool isBeginSection() const
Returns true if this block begins any section.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const std::vector< MCCFIInstruction > & getFrameInstructions() const
Returns a reference to a list of cfi instructions in the function's prologue.
bool needsFrameMoves() const
True if this function needs frame moves for debug or exceptions.
MCContext & getContext() const
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
const MachineBasicBlock & front() const
const MachineInstrBuilder & addCFIIndex(unsigned CFIIndex) const
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Wrapper class representing virtual and physical registers.
virtual Register getInitialCFARegister(const MachineFunction &MF) const
Return initial CFA register value i.e.
virtual int getInitialCFAOffset(const MachineFunction &MF) const
Return initial CFA offset value i.e.
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool operator!=(uint64_t V1, const APInt &V2)
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
LLVM_ABI void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
LLVM_ABI void initializeCFIInstrInserterPass(PassRegistry &)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
MCCFIInstruction createCFAOffset(const TargetRegisterInfo &MRI, unsigned Reg, const StackOffset &OffsetFromDefCFA, std::optional< int64_t > IncomingVGOffsetFromDefCFA)
iterator_range< df_iterator< T > > depth_first(const T &G)
LLVM_ABI FunctionPass * createCFIInstrInserter()
Creates CFI Instruction Inserter pass.