summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Target/X86/X86FrameLowering.cpp
diff options
context:
space:
mode:
authorPetar Jovanovic <petar.jovanovic@imgtec.com>2017-06-28 10:21:17 +0000
committerPetar Jovanovic <petar.jovanovic@imgtec.com>2017-06-28 10:21:17 +0000
commit7b3a38ec306c49861869dc9d2a2bd99af82b3280 (patch)
tree91bea90da1471b702f66e72f4ea3baf16e3a5329 /llvm/lib/Target/X86/X86FrameLowering.cpp
parent77b5536e4e7612c2a47e1f56253a0fc84f1beef9 (diff)
downloadbcm5719-llvm-7b3a38ec306c49861869dc9d2a2bd99af82b3280.tar.gz
bcm5719-llvm-7b3a38ec306c49861869dc9d2a2bd99af82b3280.zip
[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
Diffstat (limited to 'llvm/lib/Target/X86/X86FrameLowering.cpp')
-rw-r--r--llvm/lib/Target/X86/X86FrameLowering.cpp119
1 files changed, 116 insertions, 3 deletions
diff --git a/llvm/lib/Target/X86/X86FrameLowering.cpp b/llvm/lib/Target/X86/X86FrameLowering.cpp
index e3aa227702b..feac493aa90 100644
--- a/llvm/lib/Target/X86/X86FrameLowering.cpp
+++ b/llvm/lib/Target/X86/X86FrameLowering.cpp
@@ -958,7 +958,8 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
? 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 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
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 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
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 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
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 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
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 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
// 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 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF,
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 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF,
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 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF,
} 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 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF,
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 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF,
emitSPUpdate(MBB, MBBI, Offset, /*InEpilogue=*/true);
}
}
+
+ if (InsertedCFI)
+ MBB.updateCFIInfoSucc();
}
int X86FrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
@@ -2364,6 +2428,19 @@ void X86FrameLowering::adjustForSegmentedStacks(
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 @@ void X86FrameLowering::adjustForHiPEPrologue(
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 @@ eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
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 @@ eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
// 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 @@ eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
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 @@ MachineBasicBlock::iterator X86FrameLowering::restoreWin32EHStackPointers(
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 {
OpenPOWER on IntegriCloud