34 cl::desc(
"Verify Call Frame Information instructions"),
39class CFIInstrInserterImpl {
49 if (
unsigned ErrorNum =
verify(MF))
51 " in/out CFI information errors.");
53 bool insertedCFI = insertCFIInstrs(MF);
60 class CSRSavedLocation {
62 enum Kind { Invalid, Register, CFAOffset };
78 Loc.K = Kind::CFAOffset;
83 static CSRSavedLocation createRegister(
unsigned Reg) {
85 Loc.K = Kind::Register;
90 bool isValid()
const {
return K != Kind::Invalid; }
92 unsigned getRegister()
const {
93 assert(K == Kind::Register);
98 assert(K == Kind::CFAOffset);
109 return getRegister() ==
RHS.getRegister();
110 case Kind::CFAOffset:
116 return !(*
this ==
RHS);
118 void dump(raw_ostream &OS)
const {
124 OS <<
"In Dwarf register: " <<
Reg;
126 case Kind::CFAOffset:
127 OS <<
"At CFA offset: " <<
Offset;
134 MachineBasicBlock *MBB;
136 int64_t IncomingCFAOffset = -1;
138 int64_t OutgoingCFAOffset = -1;
140 unsigned IncomingCFARegister = 0;
142 unsigned OutgoingCFARegister = 0;
144 BitVector IncomingCSRSaved;
146 BitVector OutgoingCSRSaved;
149 bool Processed =
false;
154 std::vector<MBBCFAInfo> MBBVector;
157 SmallDenseMap<unsigned, CSRSavedLocation, 16> CSRLocMap;
165 void calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo);
168 void updateSuccCFAInfo(MBBCFAInfo &MBBInfo);
178 int64_t getCorrectCFAOffset(MachineBasicBlock *
MBB) {
182 void reportCFAError(
const MBBCFAInfo &Pred,
const MBBCFAInfo &Succ);
183 void reportCSRError(
const MBBCFAInfo &Pred,
const MBBCFAInfo &Succ);
195 CFIInstrInserterLegacy() : MachineFunctionPass(ID) {}
197 void getAnalysisUsage(AnalysisUsage &AU)
const override {
203 return CFIInstrInserterImpl().run(MF);
208char CFIInstrInserterLegacy::ID = 0;
210 "Check CFA info and insert CFI instructions if needed",
false,
213 return new CFIInstrInserterLegacy();
219 CFIInstrInserterImpl().run(MF);
233 unsigned DwarfInitialRegister =
TRI.getDwarfRegNum(InitialRegister,
true);
234 unsigned NumRegs =
TRI.getNumSupportedRegs(MF);
240 MBBInfo.IncomingCFAOffset = InitialOffset;
241 MBBInfo.OutgoingCFAOffset = InitialOffset;
242 MBBInfo.IncomingCFARegister = DwarfInitialRegister;
243 MBBInfo.OutgoingCFARegister = DwarfInitialRegister;
244 MBBInfo.IncomingCSRSaved.resize(NumRegs);
245 MBBInfo.OutgoingCSRSaved.resize(NumRegs);
253 updateSuccCFAInfo(
MBBVector[MF.front().getNumber()]);
256void CFIInstrInserterImpl::calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo) {
258 int64_t SetOffset = MBBInfo.IncomingCFAOffset;
260 unsigned SetRegister = MBBInfo.IncomingCFARegister;
264 unsigned NumRegs =
TRI.getNumSupportedRegs(*MF);
265 BitVector CSRSaved(NumRegs), CSRRestored(NumRegs);
268 int RememberState = 0;
273 if (
MI.isCFIInstruction()) {
274 std::optional<unsigned> CSRReg;
275 std::optional<int64_t> CSROffset;
276 unsigned CFIIndex =
MI.getOperand(0).getCFIIndex();
308 "Support for cfi_llvm_def_aspace_cfa not implemented! Value of CFA "
309 "may be incorrect!\n");
318 if (RememberState != 1)
321 "Support for cfi_remember_state not implemented! Value of CFA "
322 "may be incorrect!\n");
329 if (RememberState != 0)
332 "Support for cfi_restore_state not implemented! Value of CFA may "
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;
358 CSRLoc = CSRSavedLocation::createRegister(*CSRReg);
360 CSRLoc = CSRSavedLocation::createCFAOffset(*CSROffset);
361 if (CSRLoc.isValid()) {
363 if (!Inserted && It->second != CSRLoc)
365 "Different saved locations for the same CSR");
372 if (RememberState != 0)
375 "Support for cfi_remember_state not implemented! Value of CFA may be "
379 MBBInfo.Processed =
true;
382 MBBInfo.OutgoingCFAOffset = SetOffset;
387 MBBInfo.OutgoingCSRSaved, MBBInfo.IncomingCSRSaved, CSRSaved,
391void CFIInstrInserterImpl::updateSuccCFAInfo(MBBCFAInfo &MBBInfo) {
393 Stack.push_back(MBBInfo.MBB);
398 calculateOutgoingCFAInfo(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(Succ);
408 }
while (!
Stack.empty());
414 bool InsertedCFIInstr =
false;
419 if (
MBB.
getNumber() == MF.front().getNumber())
continue;
422 auto MBBI = MBBInfo.MBB->begin();
429 if ((PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset &&
430 PrevMBBInfo->OutgoingCFARegister != MBBInfo.IncomingCFARegister) ||
437 nullptr, MBBInfo.IncomingCFARegister, getCorrectCFAOffset(&
MBB)));
440 InsertedCFIInstr =
true;
441 }
else if (PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset) {
446 nullptr, getCorrectCFAOffset(&
MBB)));
449 InsertedCFIInstr =
true;
450 }
else if (PrevMBBInfo->OutgoingCFARegister !=
451 MBBInfo.IncomingCFARegister) {
454 nullptr, MBBInfo.IncomingCFARegister));
457 InsertedCFIInstr =
true;
461 MF.getSubtarget().getFrameLowering()->emitCalleeSavedFrameMovesFullCFA(
463 InsertedCFIInstr =
true;
464 PrevMBBInfo = &MBBInfo;
469 PrevMBBInfo->OutgoingCSRSaved, MBBInfo.IncomingCSRSaved);
475 InsertedCFIInstr =
true;
479 MBBInfo.IncomingCSRSaved, PrevMBBInfo->OutgoingCSRSaved);
482 assert(it != CSRLocMap.
end() &&
"Reg should have an entry in CSRLocMap");
484 CSRSavedLocation RO = it->second;
486 case CSRSavedLocation::CFAOffset: {
487 CFIIndex = MF.addFrameInst(
491 case CSRSavedLocation::Register: {
492 CFIIndex = MF.addFrameInst(
501 InsertedCFIInstr =
true;
504 PrevMBBInfo = &MBBInfo;
506 return InsertedCFIInstr;
509void CFIInstrInserterImpl::reportCFAError(
const MBBCFAInfo &Pred,
510 const MBBCFAInfo &Succ) {
511 errs() <<
"*** Inconsistent CFA register and/or offset between pred and succ "
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";
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())
534 errs() <<
"Succ: " << Succ.MBB->getName() <<
" #" << Succ.MBB->getNumber()
535 <<
" incoming CSR Saved: ";
536 for (
int Reg : Succ.IncomingCSRSaved.set_bits())
542 unsigned ErrorNum = 0;
544 const MBBCFAInfo &CurrMBBInfo =
MBBVector[CurrMBB->getNumber()];
546 const MBBCFAInfo &SuccMBBInfo =
MBBVector[Succ->getNumber()];
549 if (SuccMBBInfo.IncomingCFAOffset != CurrMBBInfo.OutgoingCFAOffset ||
550 SuccMBBInfo.IncomingCFARegister != CurrMBBInfo.OutgoingCFARegister) {
553 if (SuccMBBInfo.MBB->succ_empty() && !SuccMBBInfo.MBB->isReturnBlock())
555 reportCFAError(CurrMBBInfo, SuccMBBInfo);
560 if (SuccMBBInfo.IncomingCSRSaved != CurrMBBInfo.OutgoingCSRSaved) {
561 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
#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_>.
static MachineInstr * SetRegister(MachineInstr &I, Register *TLSBaseAddrReg)
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)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
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
@ OpLLVMVectorRegisterMask
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.
iterator_range< succ_iterator > successors()
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
Representation of each machine instruction.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Wrapper class representing virtual and physical registers.
Represents a location in source code.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
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.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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)
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
LLVM_ABI void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
LLVM_ABI FunctionPass * createCFIInstrInserterLegacy()
Creates CFI Instruction Inserter pass.
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
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)