33 cl::desc(
"Verify Call Frame Information instructions"),
59 if (
unsigned ErrorNum =
verify(MF))
61 " in/out CFI information errors.");
63 bool insertedCFI = insertCFIInstrs(MF);
72 int IncomingCFAOffset = -1;
74 int OutgoingCFAOffset = -1;
76 unsigned IncomingCFARegister = 0;
78 unsigned OutgoingCFARegister = 0;
81 bool Processed =
false;
94 void calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo);
97 void updateSuccCFAInfo(MBBCFAInfo &MBBInfo);
108 return -MBBVector[MBB->
getNumber()].IncomingCFAOffset;
111 void report(
const MBBCFAInfo &Pred,
const MBBCFAInfo &Succ);
122 "Check CFA info and insert CFI instructions if needed",
false,
133 unsigned InitialRegister =
140 MBBInfo.IncomingCFAOffset = InitialOffset;
141 MBBInfo.OutgoingCFAOffset = InitialOffset;
142 MBBInfo.IncomingCFARegister = InitialRegister;
143 MBBInfo.OutgoingCFARegister = InitialRegister;
152 if (
MBBVector[MBB.getNumber()].Processed)
continue;
153 updateSuccCFAInfo(
MBBVector[MBB.getNumber()]);
157 void CFIInstrInserter::calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo) {
159 int SetOffset = MBBInfo.IncomingCFAOffset;
161 unsigned SetRegister = MBBInfo.IncomingCFARegister;
162 const std::vector<MCCFIInstruction> &Instrs =
163 MBBInfo.MBB->getParent()->getFrameInstructions();
188 "Support for cfi_remember_state not implemented! Value of CFA " 189 "may be incorrect!\n");
196 "Support for cfi_restore_state not implemented! Value of CFA may " 216 MBBInfo.Processed =
true;
219 MBBInfo.OutgoingCFAOffset = SetOffset;
220 MBBInfo.OutgoingCFARegister = SetRegister;
223 void CFIInstrInserter::updateSuccCFAInfo(MBBCFAInfo &MBBInfo) {
230 if (CurrentInfo.Processed)
233 calculateOutgoingCFAInfo(CurrentInfo);
234 for (
auto *Succ : CurrentInfo.MBB->successors()) {
235 MBBCFAInfo &SuccInfo =
MBBVector[Succ->getNumber()];
236 if (!SuccInfo.Processed) {
237 SuccInfo.IncomingCFAOffset = CurrentInfo.OutgoingCFAOffset;
238 SuccInfo.IncomingCFARegister = CurrentInfo.OutgoingCFARegister;
242 }
while (!Stack.
empty());
248 bool InsertedCFIInstr =
false;
252 if (MBB.getNumber() == MF.front().getNumber())
continue;
254 const MBBCFAInfo &MBBInfo =
MBBVector[MBB.getNumber()];
255 auto MBBI = MBBInfo.MBB->begin();
256 DebugLoc DL = MBBInfo.MBB->findDebugLoc(MBBI);
258 if (PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset) {
262 if (PrevMBBInfo->OutgoingCFARegister != MBBInfo.IncomingCFARegister) {
264 nullptr, MBBInfo.IncomingCFARegister, getCorrectCFAOffset(&MBB)));
265 BuildMI(*MBBInfo.MBB, MBBI, DL, TII->
get(TargetOpcode::CFI_INSTRUCTION))
266 .addCFIIndex(CFIIndex);
273 nullptr, getCorrectCFAOffset(&MBB)));
274 BuildMI(*MBBInfo.MBB, MBBI, DL, TII->
get(TargetOpcode::CFI_INSTRUCTION))
275 .addCFIIndex(CFIIndex);
277 InsertedCFIInstr =
true;
281 }
else if (PrevMBBInfo->OutgoingCFARegister !=
282 MBBInfo.IncomingCFARegister) {
285 nullptr, MBBInfo.IncomingCFARegister));
286 BuildMI(*MBBInfo.MBB, MBBI, DL, TII->
get(TargetOpcode::CFI_INSTRUCTION))
287 .addCFIIndex(CFIIndex);
288 InsertedCFIInstr =
true;
290 PrevMBBInfo = &MBBInfo;
292 return InsertedCFIInstr;
295 void CFIInstrInserter::report(
const MBBCFAInfo &Pred,
const MBBCFAInfo &Succ) {
296 errs() <<
"*** Inconsistent CFA register and/or offset between pred and succ " 298 errs() <<
"Pred: " << Pred.MBB->getName() <<
" #" << Pred.MBB->getNumber()
299 <<
" in " << Pred.MBB->getParent()->getName()
300 <<
" outgoing CFA Reg:" << Pred.OutgoingCFARegister <<
"\n";
301 errs() <<
"Pred: " << Pred.MBB->getName() <<
" #" << Pred.MBB->getNumber()
302 <<
" in " << Pred.MBB->getParent()->getName()
303 <<
" outgoing CFA Offset:" << Pred.OutgoingCFAOffset <<
"\n";
304 errs() <<
"Succ: " << Succ.MBB->getName() <<
" #" << Succ.MBB->getNumber()
305 <<
" incoming CFA Reg:" << Succ.IncomingCFARegister <<
"\n";
306 errs() <<
"Succ: " << Succ.MBB->getName() <<
" #" << Succ.MBB->getNumber()
307 <<
" incoming CFA Offset:" << Succ.IncomingCFAOffset <<
"\n";
311 unsigned ErrorNum = 0;
313 const MBBCFAInfo &CurrMBBInfo =
MBBVector[CurrMBB->getNumber()];
315 const MBBCFAInfo &SuccMBBInfo =
MBBVector[Succ->getNumber()];
318 if (SuccMBBInfo.IncomingCFAOffset != CurrMBBInfo.OutgoingCFAOffset ||
319 SuccMBBInfo.IncomingCFARegister != CurrMBBInfo.OutgoingCFARegister) {
322 if (SuccMBBInfo.MBB->succ_empty() && !SuccMBBInfo.MBB->isReturnBlock())
324 report(CurrMBBInfo, SuccMBBInfo);
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
bool isCFIInstruction() const
void push_back(const T &Elt)
static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset)
.cfi_def_cfa_offset modifies a rule for computing CFA.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
const HexagonInstrInfo * TII
unsigned getCFIIndex() const
SmallVector< MachineBasicBlock *, 4 > MBBVector
static cl::opt< bool > VerifyCFI("verify-cfiinstrs", cl::desc("Verify Call Frame Information instructions"), cl::init(false), cl::Hidden)
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
virtual const TargetInstrInfo * getInstrInfo() const
TargetInstrInfo - Interface to description of machine instruction set.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register, int Offset)
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register)
.cfi_def_cfa_register modifies a rule for computing CFA.
Represent the analysis usage information of a pass.
OpType getOperation() const
FunctionPass class - This class is used to implement most global optimizations.
void initializeCFIInstrInserterPass(PassRegistry &)
const MachineBasicBlock & front() const
bool verify(const TargetRegisterInfo &TRI) const
Check that information hold by this instance make sense for the given TRI.
unsigned getRegister() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_NODISCARD T pop_back_val()
void setPreservesAll()
Set by analyses that do not transform their input at all.
Representation of each machine instruction.
LLVM_NODISCARD bool empty() const
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
INITIALIZE_PASS(CFIInstrInserter, "cfi-instr-inserter", "Check CFA info and insert CFI instructions if needed", false, false) FunctionPass *llvm
virtual const TargetFrameLowering * getFrameLowering() const
iterator_range< df_iterator< T > > depth_first(const T &G)
const MachineOperand & getOperand(unsigned i) const
FunctionPass * createCFIInstrInserter()
Creates CFI Instruction Inserter pass.
bool needsFrameMoves() const
True if this function needs frame moves for debug or exceptions.
virtual int getInitialCFAOffset(const MachineFunction &MF) const
Return initial CFA offset value i.e.
virtual unsigned getInitialCFARegister(const MachineFunction &MF) const
Return initial CFA register value i.e.