LLVM API Documentation

PrologEpilogInserter.cpp
Go to the documentation of this file.
00001 //===-- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function --===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass is responsible for finalizing the functions frame layout, saving
00011 // callee saved registers, and for emitting prolog & epilog code for the
00012 // function.
00013 //
00014 // This pass must be run after register allocation.  After this pass is
00015 // executed, it is illegal to construct MO_FrameIndex operands.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "PrologEpilogInserter.h"
00020 #include "llvm/ADT/IndexedMap.h"
00021 #include "llvm/ADT/STLExtras.h"
00022 #include "llvm/ADT/SetVector.h"
00023 #include "llvm/ADT/SmallSet.h"
00024 #include "llvm/ADT/Statistic.h"
00025 #include "llvm/CodeGen/MachineDominators.h"
00026 #include "llvm/CodeGen/MachineFrameInfo.h"
00027 #include "llvm/CodeGen/MachineInstr.h"
00028 #include "llvm/CodeGen/MachineLoopInfo.h"
00029 #include "llvm/CodeGen/MachineModuleInfo.h"
00030 #include "llvm/CodeGen/MachineRegisterInfo.h"
00031 #include "llvm/CodeGen/RegisterScavenging.h"
00032 #include "llvm/CodeGen/StackProtector.h"
00033 #include "llvm/IR/DiagnosticInfo.h"
00034 #include "llvm/IR/InlineAsm.h"
00035 #include "llvm/IR/LLVMContext.h"
00036 #include "llvm/Support/CommandLine.h"
00037 #include "llvm/Support/Compiler.h"
00038 #include "llvm/Support/Debug.h"
00039 #include "llvm/Support/raw_ostream.h"
00040 #include "llvm/Target/TargetFrameLowering.h"
00041 #include "llvm/Target/TargetInstrInfo.h"
00042 #include "llvm/Target/TargetMachine.h"
00043 #include "llvm/Target/TargetRegisterInfo.h"
00044 #include "llvm/Target/TargetSubtargetInfo.h"
00045 #include <climits>
00046 
00047 using namespace llvm;
00048 
00049 #define DEBUG_TYPE "pei"
00050 
00051 char PEI::ID = 0;
00052 char &llvm::PrologEpilogCodeInserterID = PEI::ID;
00053 
00054 static cl::opt<unsigned>
00055 WarnStackSize("warn-stack-size", cl::Hidden, cl::init((unsigned)-1),
00056               cl::desc("Warn for stack size bigger than the given"
00057                        " number"));
00058 
00059 INITIALIZE_PASS_BEGIN(PEI, "prologepilog",
00060                 "Prologue/Epilogue Insertion", false, false)
00061 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
00062 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
00063 INITIALIZE_PASS_DEPENDENCY(StackProtector)
00064 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
00065 INITIALIZE_PASS_END(PEI, "prologepilog",
00066                     "Prologue/Epilogue Insertion & Frame Finalization",
00067                     false, false)
00068 
00069 STATISTIC(NumScavengedRegs, "Number of frame index regs scavenged");
00070 STATISTIC(NumBytesStackSpace,
00071           "Number of bytes used for stack in all functions");
00072 
00073 void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
00074   AU.setPreservesCFG();
00075   AU.addPreserved<MachineLoopInfo>();
00076   AU.addPreserved<MachineDominatorTree>();
00077   AU.addRequired<StackProtector>();
00078   AU.addRequired<TargetPassConfig>();
00079   MachineFunctionPass::getAnalysisUsage(AU);
00080 }
00081 
00082 bool PEI::isReturnBlock(MachineBasicBlock* MBB) {
00083   return (MBB && !MBB->empty() && MBB->back().isReturn());
00084 }
00085 
00086 /// Compute the set of return blocks
00087 void PEI::calculateSets(MachineFunction &Fn) {
00088   // Sets used to compute spill, restore placement sets.
00089   const std::vector<CalleeSavedInfo> &CSI =
00090     Fn.getFrameInfo()->getCalleeSavedInfo();
00091 
00092   // If no CSRs used, we are done.
00093   if (CSI.empty())
00094     return;
00095 
00096   // Save refs to entry and return blocks.
00097   EntryBlock = Fn.begin();
00098   for (MachineFunction::iterator MBB = Fn.begin(), E = Fn.end();
00099        MBB != E; ++MBB)
00100     if (isReturnBlock(MBB))
00101       ReturnBlocks.push_back(MBB);
00102 
00103   return;
00104 }
00105 
00106 /// StackObjSet - A set of stack object indexes
00107 typedef SmallSetVector<int, 8> StackObjSet;
00108 
00109 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
00110 /// frame indexes with appropriate references.
00111 ///
00112 bool PEI::runOnMachineFunction(MachineFunction &Fn) {
00113   const Function* F = Fn.getFunction();
00114   const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo();
00115   const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
00116 
00117   assert(!Fn.getRegInfo().getNumVirtRegs() && "Regalloc must assign all vregs");
00118 
00119   RS = TRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : nullptr;
00120   FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(Fn);
00121 
00122   // Calculate the MaxCallFrameSize and AdjustsStack variables for the
00123   // function's frame information. Also eliminates call frame pseudo
00124   // instructions.
00125   calculateCallsInformation(Fn);
00126 
00127   // Allow the target machine to make some adjustments to the function
00128   // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
00129   TFI->processFunctionBeforeCalleeSavedScan(Fn, RS);
00130 
00131   // Scan the function for modified callee saved registers and insert spill code
00132   // for any callee saved registers that are modified.
00133   calculateCalleeSavedRegisters(Fn);
00134 
00135   // Determine placement of CSR spill/restore code:
00136   // place all spills in the entry block, all restores in return blocks.
00137   calculateSets(Fn);
00138 
00139   // Add the code to save and restore the callee saved registers
00140   if (!F->hasFnAttribute(Attribute::Naked))
00141     insertCSRSpillsAndRestores(Fn);
00142 
00143   // Allow the target machine to make final modifications to the function
00144   // before the frame layout is finalized.
00145   TFI->processFunctionBeforeFrameFinalized(Fn, RS);
00146 
00147   // Calculate actual frame offsets for all abstract stack objects...
00148   calculateFrameObjectOffsets(Fn);
00149 
00150   // Add prolog and epilog code to the function.  This function is required
00151   // to align the stack frame as necessary for any stack variables or
00152   // called functions.  Because of this, calculateCalleeSavedRegisters()
00153   // must be called before this function in order to set the AdjustsStack
00154   // and MaxCallFrameSize variables.
00155   if (!F->hasFnAttribute(Attribute::Naked))
00156     insertPrologEpilogCode(Fn);
00157 
00158   // Replace all MO_FrameIndex operands with physical register references
00159   // and actual offsets.
00160   //
00161   replaceFrameIndices(Fn);
00162 
00163   // If register scavenging is needed, as we've enabled doing it as a
00164   // post-pass, scavenge the virtual registers that frame index elimination
00165   // inserted.
00166   if (TRI->requiresRegisterScavenging(Fn) && FrameIndexVirtualScavenging)
00167     scavengeFrameVirtualRegs(Fn);
00168 
00169   // Clear any vregs created by virtual scavenging.
00170   Fn.getRegInfo().clearVirtRegs();
00171 
00172   // Warn on stack size when we exceeds the given limit.
00173   MachineFrameInfo *MFI = Fn.getFrameInfo();
00174   uint64_t StackSize = MFI->getStackSize();
00175   if (WarnStackSize.getNumOccurrences() > 0 && WarnStackSize < StackSize) {
00176     DiagnosticInfoStackSize DiagStackSize(*F, StackSize);
00177     F->getContext().diagnose(DiagStackSize);
00178   }
00179 
00180   delete RS;
00181   ReturnBlocks.clear();
00182   return true;
00183 }
00184 
00185 /// calculateCallsInformation - Calculate the MaxCallFrameSize and AdjustsStack
00186 /// variables for the function's frame information and eliminate call frame
00187 /// pseudo instructions.
00188 void PEI::calculateCallsInformation(MachineFunction &Fn) {
00189   const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo();
00190   const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
00191   MachineFrameInfo *MFI = Fn.getFrameInfo();
00192 
00193   unsigned MaxCallFrameSize = 0;
00194   bool AdjustsStack = MFI->adjustsStack();
00195 
00196   // Get the function call frame set-up and tear-down instruction opcode
00197   int FrameSetupOpcode   = TII.getCallFrameSetupOpcode();
00198   int FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
00199 
00200   // Early exit for targets which have no call frame setup/destroy pseudo
00201   // instructions.
00202   if (FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
00203     return;
00204 
00205   std::vector<MachineBasicBlock::iterator> FrameSDOps;
00206   for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
00207     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
00208       if (I->getOpcode() == FrameSetupOpcode ||
00209           I->getOpcode() == FrameDestroyOpcode) {
00210         assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
00211                " instructions should have a single immediate argument!");
00212         unsigned Size = I->getOperand(0).getImm();
00213         if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
00214         AdjustsStack = true;
00215         FrameSDOps.push_back(I);
00216       } else if (I->isInlineAsm()) {
00217         // Some inline asm's need a stack frame, as indicated by operand 1.
00218         unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
00219         if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
00220           AdjustsStack = true;
00221       }
00222 
00223   MFI->setAdjustsStack(AdjustsStack);
00224   MFI->setMaxCallFrameSize(MaxCallFrameSize);
00225 
00226   for (std::vector<MachineBasicBlock::iterator>::iterator
00227          i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
00228     MachineBasicBlock::iterator I = *i;
00229 
00230     // If call frames are not being included as part of the stack frame, and
00231     // the target doesn't indicate otherwise, remove the call frame pseudos
00232     // here. The sub/add sp instruction pairs are still inserted, but we don't
00233     // need to track the SP adjustment for frame index elimination.
00234     if (TFI->canSimplifyCallFramePseudos(Fn))
00235       TFI->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I);
00236   }
00237 }
00238 
00239 
00240 /// calculateCalleeSavedRegisters - Scan the function for modified callee saved
00241 /// registers.
00242 void PEI::calculateCalleeSavedRegisters(MachineFunction &F) {
00243   const TargetRegisterInfo *RegInfo = F.getSubtarget().getRegisterInfo();
00244   const TargetFrameLowering *TFI = F.getSubtarget().getFrameLowering();
00245   MachineFrameInfo *MFI = F.getFrameInfo();
00246 
00247   // Get the callee saved register list...
00248   const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&F);
00249 
00250   // These are used to keep track the callee-save area. Initialize them.
00251   MinCSFrameIndex = INT_MAX;
00252   MaxCSFrameIndex = 0;
00253 
00254   // Early exit for targets which have no callee saved registers.
00255   if (!CSRegs || CSRegs[0] == 0)
00256     return;
00257 
00258   // In Naked functions we aren't going to save any registers.
00259   if (F.getFunction()->hasFnAttribute(Attribute::Naked))
00260     return;
00261 
00262   std::vector<CalleeSavedInfo> CSI;
00263   for (unsigned i = 0; CSRegs[i]; ++i) {
00264     unsigned Reg = CSRegs[i];
00265     // Functions which call __builtin_unwind_init get all their registers saved.
00266     if (F.getRegInfo().isPhysRegUsed(Reg) || F.getMMI().callsUnwindInit()) {
00267       // If the reg is modified, save it!
00268       CSI.push_back(CalleeSavedInfo(Reg));
00269     }
00270   }
00271 
00272   if (!TFI->assignCalleeSavedSpillSlots(F, RegInfo, CSI)) {
00273     // If target doesn't implement this, use generic code.
00274 
00275     if (CSI.empty())
00276       return; // Early exit if no callee saved registers are modified!
00277 
00278     unsigned NumFixedSpillSlots;
00279     const TargetFrameLowering::SpillSlot *FixedSpillSlots =
00280         TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
00281 
00282     // Now that we know which registers need to be saved and restored, allocate
00283     // stack slots for them.
00284     for (std::vector<CalleeSavedInfo>::iterator I = CSI.begin(), E = CSI.end();
00285          I != E; ++I) {
00286       unsigned Reg = I->getReg();
00287       const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
00288 
00289       int FrameIdx;
00290       if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) {
00291         I->setFrameIdx(FrameIdx);
00292         continue;
00293       }
00294 
00295       // Check to see if this physreg must be spilled to a particular stack slot
00296       // on this target.
00297       const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
00298       while (FixedSlot != FixedSpillSlots + NumFixedSpillSlots &&
00299              FixedSlot->Reg != Reg)
00300         ++FixedSlot;
00301 
00302       if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
00303         // Nope, just spill it anywhere convenient.
00304         unsigned Align = RC->getAlignment();
00305         unsigned StackAlign = TFI->getStackAlignment();
00306 
00307         // We may not be able to satisfy the desired alignment specification of
00308         // the TargetRegisterClass if the stack alignment is smaller. Use the
00309         // min.
00310         Align = std::min(Align, StackAlign);
00311         FrameIdx = MFI->CreateStackObject(RC->getSize(), Align, true);
00312         if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
00313         if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
00314       } else {
00315         // Spill it to the stack where we must.
00316         FrameIdx =
00317             MFI->CreateFixedSpillStackObject(RC->getSize(), FixedSlot->Offset);
00318       }
00319 
00320       I->setFrameIdx(FrameIdx);
00321     }
00322   }
00323 
00324   MFI->setCalleeSavedInfo(CSI);
00325 }
00326 
00327 /// insertCSRSpillsAndRestores - Insert spill and restore code for
00328 /// callee saved registers used in the function.
00329 ///
00330 void PEI::insertCSRSpillsAndRestores(MachineFunction &Fn) {
00331   // Get callee saved register information.
00332   MachineFrameInfo *MFI = Fn.getFrameInfo();
00333   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
00334 
00335   MFI->setCalleeSavedInfoValid(true);
00336 
00337   // Early exit if no callee saved registers are modified!
00338   if (CSI.empty())
00339     return;
00340 
00341   const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo();
00342   const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
00343   const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo();
00344   MachineBasicBlock::iterator I;
00345 
00346   // Spill using target interface.
00347   I = EntryBlock->begin();
00348   if (!TFI->spillCalleeSavedRegisters(*EntryBlock, I, CSI, TRI)) {
00349     for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
00350       // Add the callee-saved register as live-in.
00351       // It's killed at the spill.
00352       EntryBlock->addLiveIn(CSI[i].getReg());
00353 
00354       // Insert the spill to the stack frame.
00355       unsigned Reg = CSI[i].getReg();
00356       const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
00357       TII.storeRegToStackSlot(*EntryBlock, I, Reg, true, CSI[i].getFrameIdx(),
00358                               RC, TRI);
00359     }
00360   }
00361 
00362   // Restore using target interface.
00363   for (unsigned ri = 0, re = ReturnBlocks.size(); ri != re; ++ri) {
00364     MachineBasicBlock *MBB = ReturnBlocks[ri];
00365     I = MBB->end();
00366     --I;
00367 
00368     // Skip over all terminator instructions, which are part of the return
00369     // sequence.
00370     MachineBasicBlock::iterator I2 = I;
00371     while (I2 != MBB->begin() && (--I2)->isTerminator())
00372       I = I2;
00373 
00374     bool AtStart = I == MBB->begin();
00375     MachineBasicBlock::iterator BeforeI = I;
00376     if (!AtStart)
00377       --BeforeI;
00378 
00379     // Restore all registers immediately before the return and any
00380     // terminators that precede it.
00381     if (!TFI->restoreCalleeSavedRegisters(*MBB, I, CSI, TRI)) {
00382       for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
00383         unsigned Reg = CSI[i].getReg();
00384         const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
00385         TII.loadRegFromStackSlot(*MBB, I, Reg, CSI[i].getFrameIdx(), RC, TRI);
00386         assert(I != MBB->begin() &&
00387                "loadRegFromStackSlot didn't insert any code!");
00388         // Insert in reverse order.  loadRegFromStackSlot can insert
00389         // multiple instructions.
00390         if (AtStart)
00391           I = MBB->begin();
00392         else {
00393           I = BeforeI;
00394           ++I;
00395         }
00396       }
00397     }
00398   }
00399 }
00400 
00401 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
00402 static inline void
00403 AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx,
00404                   bool StackGrowsDown, int64_t &Offset,
00405                   unsigned &MaxAlign) {
00406   // If the stack grows down, add the object size to find the lowest address.
00407   if (StackGrowsDown)
00408     Offset += MFI->getObjectSize(FrameIdx);
00409 
00410   unsigned Align = MFI->getObjectAlignment(FrameIdx);
00411 
00412   // If the alignment of this object is greater than that of the stack, then
00413   // increase the stack alignment to match.
00414   MaxAlign = std::max(MaxAlign, Align);
00415 
00416   // Adjust to alignment boundary.
00417   Offset = (Offset + Align - 1) / Align * Align;
00418 
00419   if (StackGrowsDown) {
00420     DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset << "]\n");
00421     MFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset
00422   } else {
00423     DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset << "]\n");
00424     MFI->setObjectOffset(FrameIdx, Offset);
00425     Offset += MFI->getObjectSize(FrameIdx);
00426   }
00427 }
00428 
00429 /// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
00430 /// those required to be close to the Stack Protector) to stack offsets.
00431 static void
00432 AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
00433                       SmallSet<int, 16> &ProtectedObjs,
00434                       MachineFrameInfo *MFI, bool StackGrowsDown,
00435                       int64_t &Offset, unsigned &MaxAlign) {
00436 
00437   for (StackObjSet::const_iterator I = UnassignedObjs.begin(),
00438         E = UnassignedObjs.end(); I != E; ++I) {
00439     int i = *I;
00440     AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
00441     ProtectedObjs.insert(i);
00442   }
00443 }
00444 
00445 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
00446 /// abstract stack objects.
00447 ///
00448 void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
00449   const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
00450   StackProtector *SP = &getAnalysis<StackProtector>();
00451 
00452   bool StackGrowsDown =
00453     TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
00454 
00455   // Loop over all of the stack objects, assigning sequential addresses...
00456   MachineFrameInfo *MFI = Fn.getFrameInfo();
00457 
00458   // Start at the beginning of the local area.
00459   // The Offset is the distance from the stack top in the direction
00460   // of stack growth -- so it's always nonnegative.
00461   int LocalAreaOffset = TFI.getOffsetOfLocalArea();
00462   if (StackGrowsDown)
00463     LocalAreaOffset = -LocalAreaOffset;
00464   assert(LocalAreaOffset >= 0
00465          && "Local area offset should be in direction of stack growth");
00466   int64_t Offset = LocalAreaOffset;
00467 
00468   // If there are fixed sized objects that are preallocated in the local area,
00469   // non-fixed objects can't be allocated right at the start of local area.
00470   // We currently don't support filling in holes in between fixed sized
00471   // objects, so we adjust 'Offset' to point to the end of last fixed sized
00472   // preallocated object.
00473   for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) {
00474     int64_t FixedOff;
00475     if (StackGrowsDown) {
00476       // The maximum distance from the stack pointer is at lower address of
00477       // the object -- which is given by offset. For down growing stack
00478       // the offset is negative, so we negate the offset to get the distance.
00479       FixedOff = -MFI->getObjectOffset(i);
00480     } else {
00481       // The maximum distance from the start pointer is at the upper
00482       // address of the object.
00483       FixedOff = MFI->getObjectOffset(i) + MFI->getObjectSize(i);
00484     }
00485     if (FixedOff > Offset) Offset = FixedOff;
00486   }
00487 
00488   // First assign frame offsets to stack objects that are used to spill
00489   // callee saved registers.
00490   if (StackGrowsDown) {
00491     for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
00492       // If the stack grows down, we need to add the size to find the lowest
00493       // address of the object.
00494       Offset += MFI->getObjectSize(i);
00495 
00496       unsigned Align = MFI->getObjectAlignment(i);
00497       // Adjust to alignment boundary
00498       Offset = (Offset+Align-1)/Align*Align;
00499 
00500       MFI->setObjectOffset(i, -Offset);        // Set the computed offset
00501     }
00502   } else {
00503     int MaxCSFI = MaxCSFrameIndex, MinCSFI = MinCSFrameIndex;
00504     for (int i = MaxCSFI; i >= MinCSFI ; --i) {
00505       unsigned Align = MFI->getObjectAlignment(i);
00506       // Adjust to alignment boundary
00507       Offset = (Offset+Align-1)/Align*Align;
00508 
00509       MFI->setObjectOffset(i, Offset);
00510       Offset += MFI->getObjectSize(i);
00511     }
00512   }
00513 
00514   unsigned MaxAlign = MFI->getMaxAlignment();
00515 
00516   // Make sure the special register scavenging spill slot is closest to the
00517   // incoming stack pointer if a frame pointer is required and is closer
00518   // to the incoming rather than the final stack pointer.
00519   const TargetRegisterInfo *RegInfo = Fn.getSubtarget().getRegisterInfo();
00520   bool EarlyScavengingSlots = (TFI.hasFP(Fn) &&
00521                                TFI.isFPCloseToIncomingSP() &&
00522                                RegInfo->useFPForScavengingIndex(Fn) &&
00523                                !RegInfo->needsStackRealignment(Fn));
00524   if (RS && EarlyScavengingSlots) {
00525     SmallVector<int, 2> SFIs;
00526     RS->getScavengingFrameIndices(SFIs);
00527     for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
00528            IE = SFIs.end(); I != IE; ++I)
00529       AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign);
00530   }
00531 
00532   // FIXME: Once this is working, then enable flag will change to a target
00533   // check for whether the frame is large enough to want to use virtual
00534   // frame index registers. Functions which don't want/need this optimization
00535   // will continue to use the existing code path.
00536   if (MFI->getUseLocalStackAllocationBlock()) {
00537     unsigned Align = MFI->getLocalFrameMaxAlign();
00538 
00539     // Adjust to alignment boundary.
00540     Offset = (Offset + Align - 1) / Align * Align;
00541 
00542     DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
00543 
00544     // Resolve offsets for objects in the local block.
00545     for (unsigned i = 0, e = MFI->getLocalFrameObjectCount(); i != e; ++i) {
00546       std::pair<int, int64_t> Entry = MFI->getLocalFrameObjectMap(i);
00547       int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
00548       DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" <<
00549             FIOffset << "]\n");
00550       MFI->setObjectOffset(Entry.first, FIOffset);
00551     }
00552     // Allocate the local block
00553     Offset += MFI->getLocalFrameSize();
00554 
00555     MaxAlign = std::max(Align, MaxAlign);
00556   }
00557 
00558   // Make sure that the stack protector comes before the local variables on the
00559   // stack.
00560   SmallSet<int, 16> ProtectedObjs;
00561   if (MFI->getStackProtectorIndex() >= 0) {
00562     StackObjSet LargeArrayObjs;
00563     StackObjSet SmallArrayObjs;
00564     StackObjSet AddrOfObjs;
00565 
00566     AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), StackGrowsDown,
00567                       Offset, MaxAlign);
00568 
00569     // Assign large stack objects first.
00570     for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
00571       if (MFI->isObjectPreAllocated(i) &&
00572           MFI->getUseLocalStackAllocationBlock())
00573         continue;
00574       if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
00575         continue;
00576       if (RS && RS->isScavengingFrameIndex((int)i))
00577         continue;
00578       if (MFI->isDeadObjectIndex(i))
00579         continue;
00580       if (MFI->getStackProtectorIndex() == (int)i)
00581         continue;
00582 
00583       switch (SP->getSSPLayout(MFI->getObjectAllocation(i))) {
00584       case StackProtector::SSPLK_None:
00585         continue;
00586       case StackProtector::SSPLK_SmallArray:
00587         SmallArrayObjs.insert(i);
00588         continue;
00589       case StackProtector::SSPLK_AddrOf:
00590         AddrOfObjs.insert(i);
00591         continue;
00592       case StackProtector::SSPLK_LargeArray:
00593         LargeArrayObjs.insert(i);
00594         continue;
00595       }
00596       llvm_unreachable("Unexpected SSPLayoutKind.");
00597     }
00598 
00599     AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
00600                           Offset, MaxAlign);
00601     AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
00602                           Offset, MaxAlign);
00603     AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
00604                           Offset, MaxAlign);
00605   }
00606 
00607   // Then assign frame offsets to stack objects that are not used to spill
00608   // callee saved registers.
00609   for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
00610     if (MFI->isObjectPreAllocated(i) &&
00611         MFI->getUseLocalStackAllocationBlock())
00612       continue;
00613     if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
00614       continue;
00615     if (RS && RS->isScavengingFrameIndex((int)i))
00616       continue;
00617     if (MFI->isDeadObjectIndex(i))
00618       continue;
00619     if (MFI->getStackProtectorIndex() == (int)i)
00620       continue;
00621     if (ProtectedObjs.count(i))
00622       continue;
00623 
00624     AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
00625   }
00626 
00627   // Make sure the special register scavenging spill slot is closest to the
00628   // stack pointer.
00629   if (RS && !EarlyScavengingSlots) {
00630     SmallVector<int, 2> SFIs;
00631     RS->getScavengingFrameIndices(SFIs);
00632     for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
00633            IE = SFIs.end(); I != IE; ++I)
00634       AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign);
00635   }
00636 
00637   if (!TFI.targetHandlesStackFrameRounding()) {
00638     // If we have reserved argument space for call sites in the function
00639     // immediately on entry to the current function, count it as part of the
00640     // overall stack size.
00641     if (MFI->adjustsStack() && TFI.hasReservedCallFrame(Fn))
00642       Offset += MFI->getMaxCallFrameSize();
00643 
00644     // Round up the size to a multiple of the alignment.  If the function has
00645     // any calls or alloca's, align to the target's StackAlignment value to
00646     // ensure that the callee's frame or the alloca data is suitably aligned;
00647     // otherwise, for leaf functions, align to the TransientStackAlignment
00648     // value.
00649     unsigned StackAlign;
00650     if (MFI->adjustsStack() || MFI->hasVarSizedObjects() ||
00651         (RegInfo->needsStackRealignment(Fn) && MFI->getObjectIndexEnd() != 0))
00652       StackAlign = TFI.getStackAlignment();
00653     else
00654       StackAlign = TFI.getTransientStackAlignment();
00655 
00656     // If the frame pointer is eliminated, all frame offsets will be relative to
00657     // SP not FP. Align to MaxAlign so this works.
00658     StackAlign = std::max(StackAlign, MaxAlign);
00659     unsigned AlignMask = StackAlign - 1;
00660     Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
00661   }
00662 
00663   // Update frame info to pretend that this is part of the stack...
00664   int64_t StackSize = Offset - LocalAreaOffset;
00665   MFI->setStackSize(StackSize);
00666   NumBytesStackSpace += StackSize;
00667 }
00668 
00669 /// insertPrologEpilogCode - Scan the function for modified callee saved
00670 /// registers, insert spill code for these callee saved registers, then add
00671 /// prolog and epilog code to the function.
00672 ///
00673 void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
00674   const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
00675 
00676   // Add prologue to the function...
00677   TFI.emitPrologue(Fn);
00678 
00679   // Add epilogue to restore the callee-save registers in each exiting block
00680   for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
00681     // If last instruction is a return instruction, add an epilogue
00682     if (!I->empty() && I->back().isReturn())
00683       TFI.emitEpilogue(Fn, *I);
00684   }
00685 
00686   // Emit additional code that is required to support segmented stacks, if
00687   // we've been asked for it.  This, when linked with a runtime with support
00688   // for segmented stacks (libgcc is one), will result in allocating stack
00689   // space in small chunks instead of one large contiguous block.
00690   if (Fn.shouldSplitStack())
00691     TFI.adjustForSegmentedStacks(Fn);
00692 
00693   // Emit additional code that is required to explicitly handle the stack in
00694   // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The
00695   // approach is rather similar to that of Segmented Stacks, but it uses a
00696   // different conditional check and another BIF for allocating more stack
00697   // space.
00698   if (Fn.getFunction()->getCallingConv() == CallingConv::HiPE)
00699     TFI.adjustForHiPEPrologue(Fn);
00700 }
00701 
00702 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
00703 /// register references and actual offsets.
00704 ///
00705 void PEI::replaceFrameIndices(MachineFunction &Fn) {
00706   if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
00707 
00708   // Store SPAdj at exit of a basic block.
00709   SmallVector<int, 8> SPState;
00710   SPState.resize(Fn.getNumBlockIDs());
00711   SmallPtrSet<MachineBasicBlock*, 8> Reachable;
00712 
00713   // Iterate over the reachable blocks in DFS order.
00714   for (auto DFI = df_ext_begin(&Fn, Reachable), DFE = df_ext_end(&Fn, Reachable);
00715        DFI != DFE; ++DFI) {
00716     int SPAdj = 0;
00717     // Check the exit state of the DFS stack predecessor.
00718     if (DFI.getPathLength() >= 2) {
00719       MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
00720       assert(Reachable.count(StackPred) &&
00721              "DFS stack predecessor is already visited.\n");
00722       SPAdj = SPState[StackPred->getNumber()];
00723     }
00724     MachineBasicBlock *BB = *DFI;
00725     replaceFrameIndices(BB, Fn, SPAdj);
00726     SPState[BB->getNumber()] = SPAdj;
00727   }
00728 
00729   // Handle the unreachable blocks.
00730   for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
00731     if (Reachable.count(BB))
00732       // Already handled in DFS traversal.
00733       continue;
00734     int SPAdj = 0;
00735     replaceFrameIndices(BB, Fn, SPAdj);
00736   }
00737 }
00738 
00739 void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &Fn,
00740                               int &SPAdj) {
00741   const TargetMachine &TM = Fn.getTarget();
00742   assert(TM.getSubtargetImpl()->getRegisterInfo() &&
00743          "TM::getRegisterInfo() must be implemented!");
00744   const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo();
00745   const TargetRegisterInfo &TRI = *TM.getSubtargetImpl()->getRegisterInfo();
00746   const TargetFrameLowering *TFI = TM.getSubtargetImpl()->getFrameLowering();
00747   bool StackGrowsDown =
00748     TFI->getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
00749   int FrameSetupOpcode   = TII.getCallFrameSetupOpcode();
00750   int FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
00751 
00752   if (RS && !FrameIndexVirtualScavenging) RS->enterBasicBlock(BB);
00753 
00754   for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
00755 
00756     if (I->getOpcode() == FrameSetupOpcode ||
00757         I->getOpcode() == FrameDestroyOpcode) {
00758       // Remember how much SP has been adjusted to create the call
00759       // frame.
00760       int Size = I->getOperand(0).getImm();
00761 
00762       if ((!StackGrowsDown && I->getOpcode() == FrameSetupOpcode) ||
00763           (StackGrowsDown && I->getOpcode() == FrameDestroyOpcode))
00764         Size = -Size;
00765 
00766       SPAdj += Size;
00767 
00768       MachineBasicBlock::iterator PrevI = BB->end();
00769       if (I != BB->begin()) PrevI = std::prev(I);
00770       TFI->eliminateCallFramePseudoInstr(Fn, *BB, I);
00771 
00772       // Visit the instructions created by eliminateCallFramePseudoInstr().
00773       if (PrevI == BB->end())
00774         I = BB->begin();     // The replaced instr was the first in the block.
00775       else
00776         I = std::next(PrevI);
00777       continue;
00778     }
00779 
00780     MachineInstr *MI = I;
00781     bool DoIncr = true;
00782     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00783       if (!MI->getOperand(i).isFI())
00784         continue;
00785 
00786       // Frame indicies in debug values are encoded in a target independent
00787       // way with simply the frame index and offset rather than any
00788       // target-specific addressing mode.
00789       if (MI->isDebugValue()) {
00790         assert(i == 0 && "Frame indicies can only appear as the first "
00791                          "operand of a DBG_VALUE machine instruction");
00792         unsigned Reg;
00793         MachineOperand &Offset = MI->getOperand(1);
00794         Offset.setImm(Offset.getImm() +
00795                       TFI->getFrameIndexReference(
00796                           Fn, MI->getOperand(0).getIndex(), Reg));
00797         MI->getOperand(0).ChangeToRegister(Reg, false /*isDef*/);
00798         continue;
00799       }
00800 
00801       // Some instructions (e.g. inline asm instructions) can have
00802       // multiple frame indices and/or cause eliminateFrameIndex
00803       // to insert more than one instruction. We need the register
00804       // scavenger to go through all of these instructions so that
00805       // it can update its register information. We keep the
00806       // iterator at the point before insertion so that we can
00807       // revisit them in full.
00808       bool AtBeginning = (I == BB->begin());
00809       if (!AtBeginning) --I;
00810 
00811       // If this instruction has a FrameIndex operand, we need to
00812       // use that target machine register info object to eliminate
00813       // it.
00814       TRI.eliminateFrameIndex(MI, SPAdj, i,
00815                               FrameIndexVirtualScavenging ?  nullptr : RS);
00816 
00817       // Reset the iterator if we were at the beginning of the BB.
00818       if (AtBeginning) {
00819         I = BB->begin();
00820         DoIncr = false;
00821       }
00822 
00823       MI = nullptr;
00824       break;
00825     }
00826 
00827     if (DoIncr && I != BB->end()) ++I;
00828 
00829     // Update register states.
00830     if (RS && !FrameIndexVirtualScavenging && MI) RS->forward(MI);
00831   }
00832 }
00833 
00834 /// scavengeFrameVirtualRegs - Replace all frame index virtual registers
00835 /// with physical registers. Use the register scavenger to find an
00836 /// appropriate register to use.
00837 ///
00838 /// FIXME: Iterating over the instruction stream is unnecessary. We can simply
00839 /// iterate over the vreg use list, which at this point only contains machine
00840 /// operands for which eliminateFrameIndex need a new scratch reg.
00841 void
00842 PEI::scavengeFrameVirtualRegs(MachineFunction &Fn) {
00843   // Run through the instructions and find any virtual registers.
00844   for (MachineFunction::iterator BB = Fn.begin(),
00845        E = Fn.end(); BB != E; ++BB) {
00846     RS->enterBasicBlock(BB);
00847 
00848     int SPAdj = 0;
00849 
00850     // The instruction stream may change in the loop, so check BB->end()
00851     // directly.
00852     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
00853       // We might end up here again with a NULL iterator if we scavenged a
00854       // register for which we inserted spill code for definition by what was
00855       // originally the first instruction in BB.
00856       if (I == MachineBasicBlock::iterator(nullptr))
00857         I = BB->begin();
00858 
00859       MachineInstr *MI = I;
00860       MachineBasicBlock::iterator J = std::next(I);
00861       MachineBasicBlock::iterator P =
00862                          I == BB->begin() ? MachineBasicBlock::iterator(nullptr)
00863                                           : std::prev(I);
00864 
00865       // RS should process this instruction before we might scavenge at this
00866       // location. This is because we might be replacing a virtual register
00867       // defined by this instruction, and if so, registers killed by this
00868       // instruction are available, and defined registers are not.
00869       RS->forward(I);
00870 
00871       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00872         if (MI->getOperand(i).isReg()) {
00873           MachineOperand &MO = MI->getOperand(i);
00874           unsigned Reg = MO.getReg();
00875           if (Reg == 0)
00876             continue;
00877           if (!TargetRegisterInfo::isVirtualRegister(Reg))
00878             continue;
00879 
00880           // When we first encounter a new virtual register, it
00881           // must be a definition.
00882           assert(MI->getOperand(i).isDef() &&
00883                  "frame index virtual missing def!");
00884           // Scavenge a new scratch register
00885           const TargetRegisterClass *RC = Fn.getRegInfo().getRegClass(Reg);
00886           unsigned ScratchReg = RS->scavengeRegister(RC, J, SPAdj);
00887 
00888           ++NumScavengedRegs;
00889 
00890           // Replace this reference to the virtual register with the
00891           // scratch register.
00892           assert (ScratchReg && "Missing scratch register!");
00893           MachineRegisterInfo &MRI = Fn.getRegInfo();
00894           Fn.getRegInfo().replaceRegWith(Reg, ScratchReg);
00895           
00896           // Make sure MRI now accounts this register as used.
00897           MRI.setPhysRegUsed(ScratchReg);
00898 
00899           // Because this instruction was processed by the RS before this
00900           // register was allocated, make sure that the RS now records the
00901           // register as being used.
00902           RS->setRegUsed(ScratchReg);
00903         }
00904       }
00905 
00906       // If the scavenger needed to use one of its spill slots, the
00907       // spill code will have been inserted in between I and J. This is a
00908       // problem because we need the spill code before I: Move I to just
00909       // prior to J.
00910       if (I != std::prev(J)) {
00911         BB->splice(J, BB, I);
00912 
00913         // Before we move I, we need to prepare the RS to visit I again.
00914         // Specifically, RS will assert if it sees uses of registers that
00915         // it believes are undefined. Because we have already processed
00916         // register kills in I, when it visits I again, it will believe that
00917         // those registers are undefined. To avoid this situation, unprocess
00918         // the instruction I.
00919         assert(RS->getCurrentPosition() == I &&
00920           "The register scavenger has an unexpected position");
00921         I = P;
00922         RS->unprocess(P);
00923       } else
00924         ++I;
00925     }
00926   }
00927 }