[X86] Correct dwarf unwind information in function epilogue
CFI instructions that set appropriate cfa offset and cfa register are now
inserted in emitEpilogue() in X86FrameLowering.
Majority of the changes in this patch:
1. Ensure that CFI instructions do not affect code generation.
2. Enable maintaining correct information about cfa offset and cfa register
in a function when basic blocks are reordered, merged, split, duplicated.
These changes are target independent and described below.
Changed CFI instructions so that they:
1. are duplicable
2. are not counted as instructions when tail duplicating or tail merging
3. can be compared as equal
Add information to each MachineBasicBlock about cfa offset and cfa register
that are valid at its entry and exit (incoming and outgoing CFI info). Add
support for updating this information when basic blocks are merged, split,
duplicated, created. Add a verification pass (CFIInfoVerifier) that checks
that outgoing cfa offset and register of predecessor blocks match incoming
values of their successors.
Incoming and outgoing CFI information is used by a late pass
(CFIInstrInserter) that corrects CFA calculation rule for a basic block if
needed. That means that additional CFI instructions get inserted at basic
block beginning to correct the rule for calculating CFA. Having CFI
instructions in function epilogue can cause incorrect CFA calculation rule
for some basic blocks. This can happen if, due to basic block reordering,
or the existence of multiple epilogue blocks, some of the blocks have wrong
cfa offset and register values set by the epilogue block above them.
Patch by Violeta Vukobrat.
Differential Revision: https://reviews.llvm.org/D18046
llvm-svn: 306529
diff --git a/llvm/lib/Target/X86/X86CallFrameOptimization.cpp b/llvm/lib/Target/X86/X86CallFrameOptimization.cpp
index 765af67..a7d157b 100644
--- a/llvm/lib/Target/X86/X86CallFrameOptimization.cpp
+++ b/llvm/lib/Target/X86/X86CallFrameOptimization.cpp
@@ -234,6 +234,12 @@
assert(isPowerOf2_32(SlotSize) && "Expect power of 2 stack slot size");
Log2SlotSize = Log2_32(SlotSize);
+ // Set initial incoming and outgoing cfa offset and register values for basic
+ // blocks. This is done here because this pass runs before PEI and can insert
+ // CFI instructions.
+ // TODO: Find a better solution to this problem.
+ TFL->initializeCFIInfo(MF);
+
if (skipFunction(*MF.getFunction()) || !isLegal(MF))
return false;
@@ -536,11 +542,13 @@
// For debugging, when using SP-based CFA, we need to adjust the CFA
// offset after each push.
// TODO: This is needed only if we require precise CFA.
- if (!TFL->hasFP(MF))
- TFL->BuildCFI(
- MBB, std::next(Push), DL,
- MCCFIInstruction::createAdjustCfaOffset(nullptr, SlotSize));
-
+ if (!TFL->hasFP(MF)) {
+ TFL->BuildCFI(MBB, std::next(Push), DL,
+ MCCFIInstruction::createAdjustCfaOffset(nullptr, SlotSize));
+ // Update the CFI information for MBB and it's successors.
+ MBB.updateCFIInfo(std::next(Push));
+ MBB.updateCFIInfoSucc();
+ }
MBB.erase(MOV);
}
diff --git a/llvm/lib/Target/X86/X86FrameLowering.cpp b/llvm/lib/Target/X86/X86FrameLowering.cpp
index e3aa227..feac493 100644
--- a/llvm/lib/Target/X86/X86FrameLowering.cpp
+++ b/llvm/lib/Target/X86/X86FrameLowering.cpp
@@ -958,7 +958,8 @@
? getX86SubSuperRegister(FramePtr, 64) : FramePtr;
unsigned BasePtr = TRI->getBaseRegister();
bool HasWinCFI = false;
-
+ bool InsertedCFI = false;
+
// Debug location must be unknown since the first debug location is used
// to determine the end of the prologue.
DebugLoc DL;
@@ -1093,6 +1094,9 @@
assert(StackSize);
BuildCFI(MBB, MBBI, DL,
MCCFIInstruction::createDefCfaOffset(nullptr, 2 * stackGrowth));
+ MBB.setDefOffset(true);
+ MBB.updateCFIInfo(std::prev(MBBI));
+ InsertedCFI = true;
// Change the rule for the FramePtr to be an "offset" rule.
unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true);
@@ -1121,6 +1125,9 @@
unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true);
BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaRegister(
nullptr, DwarfFramePtr));
+ MBB.setDefRegister(true);
+ MBB.updateCFIInfo(std::prev(MBBI));
+ InsertedCFI = true;
}
}
} else {
@@ -1152,6 +1159,9 @@
assert(StackSize);
BuildCFI(MBB, MBBI, DL,
MCCFIInstruction::createDefCfaOffset(nullptr, StackOffset));
+ MBB.setDefOffset(true);
+ MBB.updateCFIInfo(std::prev(MBBI));
+ InsertedCFI = true;
StackOffset += stackGrowth;
}
@@ -1417,6 +1427,9 @@
assert(StackSize);
BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaOffset(
nullptr, -StackSize + stackGrowth));
+ MBB.setDefOffset(true);
+ MBB.updateCFIInfo(std::prev(MBBI));
+ InsertedCFI = true;
}
// Emit DWARF info specifying the offsets of the callee-saved registers.
@@ -1438,6 +1451,9 @@
// At this point we know if the function has WinCFI or not.
MF.setHasWinCFI(HasWinCFI);
+
+ if (InsertedCFI)
+ MBB.updateCFIInfoSucc();
}
bool X86FrameLowering::canUseLEAForSPInEpilogue(
@@ -1548,6 +1564,12 @@
unsigned CSSize = X86FI->getCalleeSavedFrameSize();
uint64_t NumBytes = 0;
+ bool NeedsDwarfCFI = (MF.getMMI().hasDebugInfo() ||
+ MF.getFunction()->needsUnwindTableEntry()) &&
+ (!MF.getSubtarget<X86Subtarget>().isTargetDarwin() &&
+ !MF.getSubtarget<X86Subtarget>().isOSWindows());
+ bool InsertedCFI = false;
+
if (RetOpcode && *RetOpcode == X86::CATCHRET) {
// SEH shouldn't use catchret.
assert(!isAsynchronousEHPersonality(
@@ -1582,6 +1604,17 @@
BuildMI(MBB, MBBI, DL,
TII.get(Is64Bit ? X86::POP64r : X86::POP32r), MachineFramePtr)
.setMIFlag(MachineInstr::FrameDestroy);
+ if (NeedsDwarfCFI) {
+ unsigned DwarfStackPtr =
+ TRI->getDwarfRegNum(Is64Bit ? X86::RSP : X86::ESP, true);
+ BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfa(
+ nullptr, DwarfStackPtr, -SlotSize));
+ --MBBI;
+ MBB.setDefOffset(true);
+ MBB.setDefRegister(true);
+ MBB.updateCFIInfo(MBBI);
+ InsertedCFI = true;
+ }
} else {
NumBytes = StackSize - CSSize;
}
@@ -1666,6 +1699,14 @@
} else if (NumBytes) {
// Adjust stack pointer back: ESP += numbytes.
emitSPUpdate(MBB, MBBI, NumBytes, /*InEpilogue=*/true);
+ if (!hasFP(MF) && NeedsDwarfCFI) {
+ // Define the current CFA rule to use the provided offset.
+ BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaOffset(
+ nullptr, -CSSize - SlotSize));
+ MBB.setDefOffset(true);
+ MBB.updateCFIInfo(std::prev(MBBI));
+ InsertedCFI = true;
+ }
--MBBI;
}
@@ -1678,6 +1719,26 @@
if (NeedsWinCFI && MF.hasWinCFI())
BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_Epilogue));
+ if (!hasFP(MF) && NeedsDwarfCFI) {
+ MBBI = FirstCSPop;
+ int64_t Offset = -CSSize - SlotSize;
+ // Mark callee-saved pop instruction.
+ // Define the current CFA rule to use the provided offset.
+ while (MBBI != MBB.end()) {
+ MachineBasicBlock::iterator PI = MBBI;
+ unsigned Opc = PI->getOpcode();
+ ++MBBI;
+ if (Opc == X86::POP32r || Opc == X86::POP64r) {
+ Offset += SlotSize;
+ BuildCFI(MBB, MBBI, DL,
+ MCCFIInstruction::createDefCfaOffset(nullptr, Offset));
+ MBB.setDefOffset(true);
+ MBB.updateCFIInfo(std::prev(MBBI));
+ InsertedCFI = true;
+ }
+ }
+ }
+
if (!RetOpcode || !isTailCallOpcode(*RetOpcode)) {
// Add the return addr area delta back since we are not tail calling.
int Offset = -1 * X86FI->getTCReturnAddrDelta();
@@ -1690,6 +1751,9 @@
emitSPUpdate(MBB, MBBI, Offset, /*InEpilogue=*/true);
}
}
+
+ if (InsertedCFI)
+ MBB.updateCFIInfoSucc();
}
int X86FrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
@@ -2364,6 +2428,19 @@
checkMBB->addSuccessor(allocMBB);
checkMBB->addSuccessor(&PrologueMBB);
+ int InitialOffset = TRI->getSlotSize();
+ unsigned InitialRegister = TRI->getDwarfRegNum(StackPtr, true);
+ // Set CFI info for checkMBB.
+ checkMBB->setIncomingCFAOffset(InitialOffset);
+ checkMBB->setIncomingCFARegister(InitialRegister);
+ checkMBB->setOutgoingCFAOffset(InitialOffset);
+ checkMBB->setOutgoingCFARegister(InitialRegister);
+ // Set CFI info for allocMBB.
+ allocMBB->setIncomingCFAOffset(InitialOffset);
+ allocMBB->setIncomingCFARegister(InitialRegister);
+ allocMBB->setOutgoingCFAOffset(InitialOffset);
+ allocMBB->setOutgoingCFARegister(InitialRegister);
+
#ifdef EXPENSIVE_CHECKS
MF.verify();
#endif
@@ -2535,6 +2612,19 @@
stackCheckMBB->addSuccessor(incStackMBB, {1, 100});
incStackMBB->addSuccessor(&PrologueMBB, {99, 100});
incStackMBB->addSuccessor(incStackMBB, {1, 100});
+
+ int InitialOffset = TRI->getSlotSize();
+ unsigned InitialRegister = TRI->getDwarfRegNum(StackPtr, true);
+ // Set CFI info to stackCheckMBB.
+ stackCheckMBB->setIncomingCFAOffset(InitialOffset);
+ stackCheckMBB->setIncomingCFARegister(InitialRegister);
+ stackCheckMBB->setOutgoingCFAOffset(InitialOffset);
+ stackCheckMBB->setOutgoingCFARegister(InitialRegister);
+ // Set CFI info to incStackMBB.
+ incStackMBB->setIncomingCFAOffset(InitialOffset);
+ incStackMBB->setIncomingCFARegister(InitialRegister);
+ incStackMBB->setOutgoingCFAOffset(InitialOffset);
+ incStackMBB->setOutgoingCFARegister(InitialRegister);
}
#ifdef EXPENSIVE_CHECKS
MF.verify();
@@ -2640,6 +2730,7 @@
bool WindowsCFI = MF.getTarget().getMCAsmInfo()->usesWindowsCFI();
bool DwarfCFI = !WindowsCFI &&
(MMI.hasDebugInfo() || Fn->needsUnwindTableEntry());
+ bool InsertedCFI = false;
// If we have any exception handlers in this function, and we adjust
// the SP before calls, we may need to indicate this to the unwinder
@@ -2665,10 +2756,12 @@
// TODO: This is needed only if we require precise CFA.
// If this is a callee-pop calling convention, emit a CFA adjust for
// the amount the callee popped.
- if (isDestroy && InternalAmt && DwarfCFI && !hasFP(MF))
+ if (isDestroy && InternalAmt && DwarfCFI && !hasFP(MF)) {
BuildCFI(MBB, InsertPos, DL,
MCCFIInstruction::createAdjustCfaOffset(nullptr, -InternalAmt));
-
+ MBB.updateCFIInfo(std::prev(InsertPos));
+ InsertedCFI = true;
+ }
// Add Amount to SP to destroy a frame, or subtract to setup.
int64_t StackAdjustment = isDestroy ? Amount : -Amount;
int64_t CfaAdjustment = -StackAdjustment;
@@ -2702,9 +2795,13 @@
BuildCFI(MBB, InsertPos, DL,
MCCFIInstruction::createAdjustCfaOffset(nullptr,
CfaAdjustment));
+ MBB.updateCFIInfo(std::prev(InsertPos));
+ InsertedCFI = true;
}
}
+ if (InsertedCFI) MBB.updateCFIInfoSucc();
+
return I;
}
@@ -2826,6 +2923,22 @@
return MBBI;
}
+void X86FrameLowering::initializeCFIInfo(MachineFunction &MF) const {
+ int InitialOffset = TRI->getSlotSize();
+ unsigned InitialRegister = TRI->getDwarfRegNum(StackPtr, true);
+ // Initialize CFI info if it hasn't already been initialized.
+ for (auto &MBB : MF) {
+ if (MBB.getIncomingCFAOffset() == -1)
+ MBB.setIncomingCFAOffset(InitialOffset);
+ if (MBB.getOutgoingCFAOffset() == -1)
+ MBB.setOutgoingCFAOffset(InitialOffset);
+ if (MBB.getIncomingCFARegister() == 0)
+ MBB.setIncomingCFARegister(InitialRegister);
+ if (MBB.getOutgoingCFARegister() == 0)
+ MBB.setOutgoingCFARegister(InitialRegister);
+ }
+}
+
namespace {
// Struct used by orderFrameObjects to help sort the stack objects.
struct X86FrameSortingObject {
diff --git a/llvm/lib/Target/X86/X86FrameLowering.h b/llvm/lib/Target/X86/X86FrameLowering.h
index 7d214ca..60edf84 100644
--- a/llvm/lib/Target/X86/X86FrameLowering.h
+++ b/llvm/lib/Target/X86/X86FrameLowering.h
@@ -177,6 +177,8 @@
MachineBasicBlock::iterator MBBI,
const DebugLoc &DL, bool RestoreSP = false) const;
+ void initializeCFIInfo(MachineFunction &MF) const override;
+
private:
uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;