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 class CSRSavedLocation {
72 enum Kind { Invalid, Register, CFAOffset };
88 Loc.K = Kind::CFAOffset;
93 static CSRSavedLocation createRegister(
unsigned Reg) {
95 Loc.K = Kind::Register;
100 bool isValid()
const {
return K != Kind::Invalid; }
102 unsigned getRegister()
const {
103 assert(K == Kind::Register);
108 assert(K == Kind::CFAOffset);
119 return getRegister() ==
RHS.getRegister();
120 case Kind::CFAOffset:
126 return !(*
this ==
RHS);
128 void dump(raw_ostream &OS)
const {
134 OS <<
"In Dwarf register: " <<
Reg;
136 case Kind::CFAOffset:
137 OS <<
"At CFA offset: " <<
Offset;
144 MachineBasicBlock *MBB;
146 int64_t IncomingCFAOffset = -1;
148 int64_t OutgoingCFAOffset = -1;
150 unsigned IncomingCFARegister = 0;
152 unsigned OutgoingCFARegister = 0;
154 BitVector IncomingCSRSaved;
156 BitVector OutgoingCSRSaved;
159 bool Processed =
false;
164 std::vector<MBBCFAInfo> MBBVector;
167 SmallDenseMap<unsigned, CSRSavedLocation, 16> CSRLocMap;
171 void calculateCFAInfo(MachineFunction &MF);
175 void calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo);
178 void updateSuccCFAInfo(MBBCFAInfo &MBBInfo);
184 bool insertCFIInstrs(MachineFunction &MF);
188 int64_t getCorrectCFAOffset(MachineBasicBlock *
MBB) {
192 void reportCFAError(
const MBBCFAInfo &Pred,
const MBBCFAInfo &Succ);
193 void reportCSRError(
const MBBCFAInfo &Pred,
const MBBCFAInfo &Succ);
198 unsigned verify(MachineFunction &MF);
202char CFIInstrInserter::ID = 0;
204 "Check CFA info and insert CFI instructions if needed",
false,
218 unsigned DwarfInitialRegister =
TRI.getDwarfRegNum(InitialRegister,
true);
219 unsigned NumRegs =
TRI.getNumSupportedRegs(MF);
222 for (MachineBasicBlock &
MBB : MF) {
225 MBBInfo.IncomingCFAOffset = InitialOffset;
226 MBBInfo.OutgoingCFAOffset = InitialOffset;
227 MBBInfo.IncomingCFARegister = DwarfInitialRegister;
228 MBBInfo.OutgoingCFARegister = DwarfInitialRegister;
229 MBBInfo.IncomingCSRSaved.resize(NumRegs);
230 MBBInfo.OutgoingCSRSaved.resize(NumRegs);
238 updateSuccCFAInfo(
MBBVector[MF.front().getNumber()]);
241void CFIInstrInserter::calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo) {
243 int64_t SetOffset = MBBInfo.IncomingCFAOffset;
245 unsigned SetRegister = MBBInfo.IncomingCFARegister;
246 MachineFunction *MF = MBBInfo.MBB->getParent();
249 unsigned NumRegs =
TRI.getNumSupportedRegs(*MF);
250 BitVector CSRSaved(NumRegs), CSRRestored(NumRegs);
253 int RememberState = 0;
257 for (MachineInstr &
MI : *MBBInfo.MBB) {
258 if (
MI.isCFIInstruction()) {
259 std::optional<unsigned> CSRReg;
260 std::optional<int64_t> CSROffset;
261 unsigned CFIIndex =
MI.getOperand(0).getCFIIndex();
262 const MCCFIInstruction &CFI = Instrs[CFIIndex];
293 "Support for cfi_llvm_def_aspace_cfa not implemented! Value of CFA "
294 "may be incorrect!\n");
303 if (RememberState != 1)
306 "Support for cfi_remember_state not implemented! Value of CFA "
307 "may be incorrect!\n");
314 if (RememberState != 0)
317 "Support for cfi_restore_state not implemented! Value of CFA may "
333 assert((!CSRReg.has_value() || !CSROffset.has_value()) &&
334 "A register can only be at an offset from CFA or in another "
335 "register, but not both!");
336 CSRSavedLocation CSRLoc;
338 CSRLoc = CSRSavedLocation::createRegister(*CSRReg);
340 CSRLoc = CSRSavedLocation::createCFAOffset(*CSROffset);
341 if (CSRLoc.isValid()) {
343 if (!Inserted && It->second != CSRLoc)
345 "Different saved locations for the same CSR");
352 if (RememberState != 0)
355 "Support for cfi_remember_state not implemented! Value of CFA may be "
359 MBBInfo.Processed =
true;
362 MBBInfo.OutgoingCFAOffset = SetOffset;
363 MBBInfo.OutgoingCFARegister = SetRegister;
367 MBBInfo.OutgoingCSRSaved, MBBInfo.IncomingCSRSaved, CSRSaved,
371void CFIInstrInserter::updateSuccCFAInfo(MBBCFAInfo &MBBInfo) {
372 SmallVector<MachineBasicBlock *, 4>
Stack;
373 Stack.push_back(MBBInfo.MBB);
376 MachineBasicBlock *Current =
Stack.pop_back_val();
378 calculateOutgoingCFAInfo(CurrentInfo);
379 for (
auto *Succ : CurrentInfo.MBB->successors()) {
380 MBBCFAInfo &SuccInfo =
MBBVector[Succ->getNumber()];
381 if (!SuccInfo.Processed) {
382 SuccInfo.IncomingCFAOffset = CurrentInfo.OutgoingCFAOffset;
383 SuccInfo.IncomingCFARegister = CurrentInfo.OutgoingCFARegister;
384 SuccInfo.IncomingCSRSaved = CurrentInfo.OutgoingCSRSaved;
385 Stack.push_back(Succ);
388 }
while (!
Stack.empty());
391bool CFIInstrInserter::insertCFIInstrs(MachineFunction &MF) {
394 bool InsertedCFIInstr =
false;
396 BitVector SetDifference;
397 for (MachineBasicBlock &
MBB : MF) {
399 if (
MBB.
getNumber() == MF.front().getNumber())
continue;
402 auto MBBI = MBBInfo.MBB->begin();
409 if ((PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset &&
410 PrevMBBInfo->OutgoingCFARegister != MBBInfo.IncomingCFARegister) ||
417 nullptr, MBBInfo.IncomingCFARegister, getCorrectCFAOffset(&
MBB)));
420 InsertedCFIInstr =
true;
421 }
else if (PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset) {
426 nullptr, getCorrectCFAOffset(&
MBB)));
429 InsertedCFIInstr =
true;
430 }
else if (PrevMBBInfo->OutgoingCFARegister !=
431 MBBInfo.IncomingCFARegister) {
434 nullptr, MBBInfo.IncomingCFARegister));
437 InsertedCFIInstr =
true;
441 MF.getSubtarget().getFrameLowering()->emitCalleeSavedFrameMovesFullCFA(
443 InsertedCFIInstr =
true;
444 PrevMBBInfo = &MBBInfo;
449 PrevMBBInfo->OutgoingCSRSaved, MBBInfo.IncomingCSRSaved);
455 InsertedCFIInstr =
true;
459 MBBInfo.IncomingCSRSaved, PrevMBBInfo->OutgoingCSRSaved);
461 auto it = CSRLocMap.find(
Reg);
462 assert(it != CSRLocMap.end() &&
"Reg should have an entry in CSRLocMap");
464 CSRSavedLocation RO = it->second;
466 case CSRSavedLocation::CFAOffset: {
467 CFIIndex = MF.addFrameInst(
471 case CSRSavedLocation::Register: {
472 CFIIndex = MF.addFrameInst(
481 InsertedCFIInstr =
true;
484 PrevMBBInfo = &MBBInfo;
486 return InsertedCFIInstr;
489void CFIInstrInserter::reportCFAError(
const MBBCFAInfo &Pred,
490 const MBBCFAInfo &Succ) {
491 errs() <<
"*** Inconsistent CFA register and/or offset between pred and succ "
493 errs() <<
"Pred: " << Pred.MBB->getName() <<
" #" << Pred.MBB->getNumber()
494 <<
" in " << Pred.MBB->getParent()->getName()
495 <<
" outgoing CFA Reg:" << Pred.OutgoingCFARegister <<
"\n";
496 errs() <<
"Pred: " << Pred.MBB->getName() <<
" #" << Pred.MBB->getNumber()
497 <<
" in " << Pred.MBB->getParent()->getName()
498 <<
" outgoing CFA Offset:" << Pred.OutgoingCFAOffset <<
"\n";
499 errs() <<
"Succ: " << Succ.MBB->getName() <<
" #" << Succ.MBB->getNumber()
500 <<
" incoming CFA Reg:" << Succ.IncomingCFARegister <<
"\n";
501 errs() <<
"Succ: " << Succ.MBB->getName() <<
" #" << Succ.MBB->getNumber()
502 <<
" incoming CFA Offset:" << Succ.IncomingCFAOffset <<
"\n";
505void CFIInstrInserter::reportCSRError(
const MBBCFAInfo &Pred,
506 const MBBCFAInfo &Succ) {
507 errs() <<
"*** Inconsistent CSR Saved between pred and succ in function "
508 << Pred.MBB->getParent()->getName() <<
" ***\n";
509 errs() <<
"Pred: " << Pred.MBB->getName() <<
" #" << Pred.MBB->getNumber()
510 <<
" outgoing CSR Saved: ";
511 for (
int Reg : Pred.OutgoingCSRSaved.set_bits())
514 errs() <<
"Succ: " << Succ.MBB->getName() <<
" #" << Succ.MBB->getNumber()
515 <<
" incoming CSR Saved: ";
516 for (
int Reg : Succ.IncomingCSRSaved.set_bits())
521unsigned CFIInstrInserter::verify(MachineFunction &MF) {
522 unsigned ErrorNum = 0;
524 const MBBCFAInfo &CurrMBBInfo =
MBBVector[CurrMBB->getNumber()];
525 for (MachineBasicBlock *Succ : CurrMBB->successors()) {
526 const MBBCFAInfo &SuccMBBInfo =
MBBVector[Succ->getNumber()];
529 if (SuccMBBInfo.IncomingCFAOffset != CurrMBBInfo.OutgoingCFAOffset ||
530 SuccMBBInfo.IncomingCFARegister != CurrMBBInfo.OutgoingCFARegister) {
533 if (SuccMBBInfo.MBB->succ_empty() && !SuccMBBInfo.MBB->isReturnBlock())
535 reportCFAError(CurrMBBInfo, SuccMBBInfo);
540 if (SuccMBBInfo.IncomingCSRSaved != CurrMBBInfo.OutgoingCSRSaved) {
541 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...
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.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
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.