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 // This pass provides an optional shrink wrapping variant of prolog/epilog
00018 // insertion, enabled via --shrink-wrap. See ShrinkWrapping.cpp.
00019 //
00020 //===----------------------------------------------------------------------===//
00021 
00022 #include "PrologEpilogInserter.h"
00023 #include "llvm/CodeGen/MachineDominators.h"
00024 #include "llvm/CodeGen/MachineLoopInfo.h"
00025 #include "llvm/CodeGen/MachineInstr.h"
00026 #include "llvm/CodeGen/MachineFrameInfo.h"
00027 #include "llvm/CodeGen/MachineModuleInfo.h"
00028 #include "llvm/CodeGen/MachineRegisterInfo.h"
00029 #include "llvm/CodeGen/RegisterScavenging.h"
00030 #include "llvm/Target/TargetMachine.h"
00031 #include "llvm/Target/TargetRegisterInfo.h"
00032 #include "llvm/Target/TargetFrameInfo.h"
00033 #include "llvm/Target/TargetInstrInfo.h"
00034 #include "llvm/Support/Compiler.h"
00035 #include "llvm/ADT/STLExtras.h"
00036 #include <climits>
00037 
00038 using namespace llvm;
00039 
00040 char PEI::ID = 0;
00041 
00042 static RegisterPass<PEI>
00043 X("prologepilog", "Prologue/Epilogue Insertion");
00044 
00045 /// createPrologEpilogCodeInserter - This function returns a pass that inserts
00046 /// prolog and epilog code, and eliminates abstract frame references.
00047 ///
00048 FunctionPass *llvm::createPrologEpilogCodeInserter() { return new PEI(); }
00049 
00050 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
00051 /// frame indexes with appropriate references.
00052 ///
00053 bool PEI::runOnMachineFunction(MachineFunction &Fn) {
00054   const Function* F = Fn.getFunction();
00055   const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
00056   RS = TRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : NULL;
00057 
00058   // Get MachineModuleInfo so that we can track the construction of the
00059   // frame.
00060   if (MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>())
00061     Fn.getFrameInfo()->setMachineModuleInfo(MMI);
00062 
00063   // Calculate the MaxCallFrameSize and HasCalls variables for the function's
00064   // frame information. Also eliminates call frame pseudo instructions.
00065   calculateCallsInformation(Fn);
00066 
00067   // Allow the target machine to make some adjustments to the function
00068   // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
00069   TRI->processFunctionBeforeCalleeSavedScan(Fn, RS);
00070 
00071   // Scan the function for modified callee saved registers and insert spill code
00072   // for any callee saved registers that are modified.
00073   calculateCalleeSavedRegisters(Fn);
00074 
00075   // Determine placement of CSR spill/restore code:
00076   //  - with shrink wrapping, place spills and restores to tightly
00077   //    enclose regions in the Machine CFG of the function where
00078   //    they are used. Without shrink wrapping
00079   //  - default (no shrink wrapping), place all spills in the
00080   //    entry block, all restores in return blocks.
00081   placeCSRSpillsAndRestores(Fn);
00082 
00083   // Add the code to save and restore the callee saved registers
00084   if (!F->hasFnAttr(Attribute::Naked))
00085     insertCSRSpillsAndRestores(Fn);
00086 
00087   // Allow the target machine to make final modifications to the function
00088   // before the frame layout is finalized.
00089   TRI->processFunctionBeforeFrameFinalized(Fn);
00090 
00091   // Calculate actual frame offsets for all abstract stack objects...
00092   calculateFrameObjectOffsets(Fn);
00093 
00094   // Add prolog and epilog code to the function.  This function is required
00095   // to align the stack frame as necessary for any stack variables or
00096   // called functions.  Because of this, calculateCalleeSavedRegisters
00097   // must be called before this function in order to set the HasCalls
00098   // and MaxCallFrameSize variables.
00099   if (!F->hasFnAttr(Attribute::Naked))
00100     insertPrologEpilogCode(Fn);
00101 
00102   // Replace all MO_FrameIndex operands with physical register references
00103   // and actual offsets.
00104   //
00105   replaceFrameIndices(Fn);
00106 
00107   delete RS;
00108   clearAllSets();
00109   return true;
00110 }
00111 
00112 #if 0
00113 void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
00114   AU.setPreservesCFG();
00115   if (ShrinkWrapping || ShrinkWrapFunc != "") {
00116     AU.addRequired<MachineLoopInfo>();
00117     AU.addRequired<MachineDominatorTree>();
00118   }
00119   AU.addPreserved<MachineLoopInfo>();
00120   AU.addPreserved<MachineDominatorTree>();
00121   MachineFunctionPass::getAnalysisUsage(AU);
00122 }
00123 #endif
00124 
00125 /// calculateCallsInformation - Calculate the MaxCallFrameSize and HasCalls
00126 /// variables for the function's frame information and eliminate call frame
00127 /// pseudo instructions.
00128 void PEI::calculateCallsInformation(MachineFunction &Fn) {
00129   const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
00130 
00131   unsigned MaxCallFrameSize = 0;
00132   bool HasCalls = false;
00133 
00134   // Get the function call frame set-up and tear-down instruction opcode
00135   int FrameSetupOpcode   = RegInfo->getCallFrameSetupOpcode();
00136   int FrameDestroyOpcode = RegInfo->getCallFrameDestroyOpcode();
00137 
00138   // Early exit for targets which have no call frame setup/destroy pseudo
00139   // instructions.
00140   if (FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
00141     return;
00142 
00143   std::vector<MachineBasicBlock::iterator> FrameSDOps;
00144   for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
00145     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
00146       if (I->getOpcode() == FrameSetupOpcode ||
00147           I->getOpcode() == FrameDestroyOpcode) {
00148         assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
00149                " instructions should have a single immediate argument!");
00150         unsigned Size = I->getOperand(0).getImm();
00151         if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
00152         HasCalls = true;
00153         FrameSDOps.push_back(I);
00154       } else if (I->getOpcode() == TargetInstrInfo::INLINEASM) {
00155         // An InlineAsm might be a call; assume it is to get the stack frame
00156         // aligned correctly for calls.
00157         HasCalls = true;
00158       }
00159 
00160   MachineFrameInfo *FFI = Fn.getFrameInfo();
00161   FFI->setHasCalls(HasCalls);
00162   FFI->setMaxCallFrameSize(MaxCallFrameSize);
00163 
00164   for (std::vector<MachineBasicBlock::iterator>::iterator
00165          i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
00166     MachineBasicBlock::iterator I = *i;
00167 
00168     // If call frames are not being included as part of the stack frame, and
00169     // there is no dynamic allocation (therefore referencing frame slots off
00170     // sp), leave the pseudo ops alone. We'll eliminate them later.
00171     if (RegInfo->hasReservedCallFrame(Fn) || RegInfo->hasFP(Fn))
00172       RegInfo->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I);
00173   }
00174 }
00175 
00176 
00177 /// calculateCalleeSavedRegisters - Scan the function for modified callee saved
00178 /// registers.
00179 void PEI::calculateCalleeSavedRegisters(MachineFunction &Fn) {
00180   const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
00181   const TargetFrameInfo *TFI = Fn.getTarget().getFrameInfo();
00182   MachineFrameInfo *FFI = Fn.getFrameInfo();
00183 
00184   // Get the callee saved register list...
00185   const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(&Fn);
00186 
00187   // These are used to keep track the callee-save area. Initialize them.
00188   MinCSFrameIndex = INT_MAX;
00189   MaxCSFrameIndex = 0;
00190 
00191   // Early exit for targets which have no callee saved registers.
00192   if (CSRegs == 0 || CSRegs[0] == 0)
00193     return;
00194 
00195   // Figure out which *callee saved* registers are modified by the current
00196   // function, thus needing to be saved and restored in the prolog/epilog.
00197   const TargetRegisterClass * const *CSRegClasses =
00198     RegInfo->getCalleeSavedRegClasses(&Fn);
00199 
00200   std::vector<CalleeSavedInfo> CSI;
00201   for (unsigned i = 0; CSRegs[i]; ++i) {
00202     unsigned Reg = CSRegs[i];
00203     if (Fn.getRegInfo().isPhysRegUsed(Reg)) {
00204       // If the reg is modified, save it!
00205       CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
00206     } else {
00207       for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
00208            *AliasSet; ++AliasSet) {  // Check alias registers too.
00209         if (Fn.getRegInfo().isPhysRegUsed(*AliasSet)) {
00210           CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
00211           break;
00212         }
00213       }
00214     }
00215   }
00216 
00217   if (CSI.empty())
00218     return;   // Early exit if no callee saved registers are modified!
00219 
00220   unsigned NumFixedSpillSlots;
00221   const std::pair<unsigned,int> *FixedSpillSlots =
00222     TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
00223 
00224   // Now that we know which registers need to be saved and restored, allocate
00225   // stack slots for them.
00226   for (std::vector<CalleeSavedInfo>::iterator
00227          I = CSI.begin(), E = CSI.end(); I != E; ++I) {
00228     unsigned Reg = I->getReg();
00229     const TargetRegisterClass *RC = I->getRegClass();
00230 
00231     int FrameIdx;
00232     if (RegInfo->hasReservedSpillSlot(Fn, Reg, FrameIdx)) {
00233       I->setFrameIdx(FrameIdx);
00234       continue;
00235     }
00236 
00237     // Check to see if this physreg must be spilled to a particular stack slot
00238     // on this target.
00239     const std::pair<unsigned,int> *FixedSlot = FixedSpillSlots;
00240     while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
00241            FixedSlot->first != Reg)
00242       ++FixedSlot;
00243 
00244     if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
00245       // Nope, just spill it anywhere convenient.
00246       unsigned Align = RC->getAlignment();
00247       unsigned StackAlign = TFI->getStackAlignment();
00248 
00249       // We may not be able to satisfy the desired alignment specification of
00250       // the TargetRegisterClass if the stack alignment is smaller. Use the
00251       // min.
00252       Align = std::min(Align, StackAlign);
00253       FrameIdx = FFI->CreateStackObject(RC->getSize(), Align);
00254       if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
00255       if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
00256     } else {
00257       // Spill it to the stack where we must.
00258       FrameIdx = FFI->CreateFixedObject(RC->getSize(), FixedSlot->second);
00259     }
00260 
00261     I->setFrameIdx(FrameIdx);
00262   }
00263 
00264   FFI->setCalleeSavedInfo(CSI);
00265 }
00266 
00267 /// insertCSRSpillsAndRestores - Insert spill and restore code for
00268 /// callee saved registers used in the function, handling shrink wrapping.
00269 ///
00270 void PEI::insertCSRSpillsAndRestores(MachineFunction &Fn) {
00271   // Get callee saved register information.
00272   MachineFrameInfo *FFI = Fn.getFrameInfo();
00273   const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
00274 
00275   FFI->setCalleeSavedInfoValid(true);
00276 
00277   // Early exit if no callee saved registers are modified!
00278   if (CSI.empty())
00279     return;
00280 
00281   const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
00282   MachineBasicBlock::iterator I;
00283 
00284   if (! ShrinkWrapThisFunction) {
00285     // Spill using target interface.
00286     I = EntryBlock->begin();
00287     if (!TII.spillCalleeSavedRegisters(*EntryBlock, I, CSI)) {
00288       for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
00289         // Add the callee-saved register as live-in.
00290         // It's killed at the spill.
00291         EntryBlock->addLiveIn(CSI[i].getReg());
00292 
00293         // Insert the spill to the stack frame.
00294         TII.storeRegToStackSlot(*EntryBlock, I, CSI[i].getReg(), true,
00295                                 CSI[i].getFrameIdx(), CSI[i].getRegClass());
00296       }
00297     }
00298 
00299     // Restore using target interface.
00300     for (unsigned ri = 0, re = ReturnBlocks.size(); ri != re; ++ri) {
00301       MachineBasicBlock* MBB = ReturnBlocks[ri];
00302       I = MBB->end(); --I;
00303 
00304       // Skip over all terminator instructions, which are part of the return
00305       // sequence.
00306       MachineBasicBlock::iterator I2 = I;
00307       while (I2 != MBB->begin() && (--I2)->getDesc().isTerminator())
00308         I = I2;
00309 
00310       bool AtStart = I == MBB->begin();
00311       MachineBasicBlock::iterator BeforeI = I;
00312       if (!AtStart)
00313         --BeforeI;
00314 
00315       // Restore all registers immediately before the return and any
00316       // terminators that preceed it.
00317       if (!TII.restoreCalleeSavedRegisters(*MBB, I, CSI)) {
00318         for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
00319           TII.loadRegFromStackSlot(*MBB, I, CSI[i].getReg(),
00320                                    CSI[i].getFrameIdx(),
00321                                    CSI[i].getRegClass());
00322           assert(I != MBB->begin() &&
00323                  "loadRegFromStackSlot didn't insert any code!");
00324           // Insert in reverse order.  loadRegFromStackSlot can insert
00325           // multiple instructions.
00326           if (AtStart)
00327             I = MBB->begin();
00328           else {
00329             I = BeforeI;
00330             ++I;
00331           }
00332         }
00333       }
00334     }
00335     return;
00336   }
00337 
00338   // Insert spills.
00339   std::vector<CalleeSavedInfo> blockCSI;
00340   for (CSRegBlockMap::iterator BI = CSRSave.begin(),
00341          BE = CSRSave.end(); BI != BE; ++BI) {
00342     MachineBasicBlock* MBB = BI->first;
00343     CSRegSet save = BI->second;
00344 
00345     if (save.empty())
00346       continue;
00347 
00348     blockCSI.clear();
00349     for (CSRegSet::iterator RI = save.begin(),
00350            RE = save.end(); RI != RE; ++RI) {
00351       blockCSI.push_back(CSI[*RI]);
00352     }
00353     assert(blockCSI.size() > 0 &&
00354            "Could not collect callee saved register info");
00355 
00356     I = MBB->begin();
00357 
00358     // When shrink wrapping, use stack slot stores/loads.
00359     for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
00360       // Add the callee-saved register as live-in.
00361       // It's killed at the spill.
00362       MBB->addLiveIn(blockCSI[i].getReg());
00363 
00364       // Insert the spill to the stack frame.
00365       TII.storeRegToStackSlot(*MBB, I, blockCSI[i].getReg(),
00366                               true,
00367                               blockCSI[i].getFrameIdx(),
00368                               blockCSI[i].getRegClass());
00369     }
00370   }
00371 
00372   for (CSRegBlockMap::iterator BI = CSRRestore.begin(),
00373          BE = CSRRestore.end(); BI != BE; ++BI) {
00374     MachineBasicBlock* MBB = BI->first;
00375     CSRegSet restore = BI->second;
00376 
00377     if (restore.empty())
00378       continue;
00379 
00380     blockCSI.clear();
00381     for (CSRegSet::iterator RI = restore.begin(),
00382            RE = restore.end(); RI != RE; ++RI) {
00383       blockCSI.push_back(CSI[*RI]);
00384     }
00385     assert(blockCSI.size() > 0 &&
00386            "Could not find callee saved register info");
00387 
00388     // If MBB is empty and needs restores, insert at the _beginning_.
00389     if (MBB->empty()) {
00390       I = MBB->begin();
00391     } else {
00392       I = MBB->end();
00393       --I;
00394 
00395       // Skip over all terminator instructions, which are part of the
00396       // return sequence.
00397       if (! I->getDesc().isTerminator()) {
00398         ++I;
00399       } else {
00400         MachineBasicBlock::iterator I2 = I;
00401         while (I2 != MBB->begin() && (--I2)->getDesc().isTerminator())
00402           I = I2;
00403       }
00404     }
00405 
00406     bool AtStart = I == MBB->begin();
00407     MachineBasicBlock::iterator BeforeI = I;
00408     if (!AtStart)
00409       --BeforeI;
00410 
00411     // Restore all registers immediately before the return and any
00412     // terminators that preceed it.
00413     for (unsigned i = 0, e = blockCSI.size(); i != e; ++i) {
00414       TII.loadRegFromStackSlot(*MBB, I, blockCSI[i].getReg(),
00415                                blockCSI[i].getFrameIdx(),
00416                                blockCSI[i].getRegClass());
00417       assert(I != MBB->begin() &&
00418              "loadRegFromStackSlot didn't insert any code!");
00419       // Insert in reverse order.  loadRegFromStackSlot can insert
00420       // multiple instructions.
00421       if (AtStart)
00422         I = MBB->begin();
00423       else {
00424         I = BeforeI;
00425         ++I;
00426       }
00427     }
00428   }
00429 }
00430 
00431 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
00432 static inline void
00433 AdjustStackOffset(MachineFrameInfo *FFI, int FrameIdx,
00434                   bool StackGrowsDown, int64_t &Offset,
00435                   unsigned &MaxAlign) {
00436   // If stack grows down, we need to add size of find the lowest address of the
00437   // object.
00438   if (StackGrowsDown)
00439     Offset += FFI->getObjectSize(FrameIdx);
00440 
00441   unsigned Align = FFI->getObjectAlignment(FrameIdx);
00442 
00443   // If the alignment of this object is greater than that of the stack, then
00444   // increase the stack alignment to match.
00445   MaxAlign = std::max(MaxAlign, Align);
00446 
00447   // Adjust to alignment boundary.
00448   Offset = (Offset + Align - 1) / Align * Align;
00449 
00450   if (StackGrowsDown) {
00451     FFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset
00452   } else {
00453     FFI->setObjectOffset(FrameIdx, Offset);
00454     Offset += FFI->getObjectSize(FrameIdx);
00455   }
00456 }
00457 
00458 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
00459 /// abstract stack objects.
00460 ///
00461 void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
00462   const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
00463 
00464   bool StackGrowsDown =
00465     TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
00466 
00467   // Loop over all of the stack objects, assigning sequential addresses...
00468   MachineFrameInfo *FFI = Fn.getFrameInfo();
00469 
00470   unsigned MaxAlign = FFI->getMaxAlignment();
00471 
00472   // Start at the beginning of the local area.
00473   // The Offset is the distance from the stack top in the direction
00474   // of stack growth -- so it's always nonnegative.
00475   int64_t Offset = TFI.getOffsetOfLocalArea();
00476   if (StackGrowsDown)
00477     Offset = -Offset;
00478   assert(Offset >= 0
00479          && "Local area offset should be in direction of stack growth");
00480 
00481   // If there are fixed sized objects that are preallocated in the local area,
00482   // non-fixed objects can't be allocated right at the start of local area.
00483   // We currently don't support filling in holes in between fixed sized
00484   // objects, so we adjust 'Offset' to point to the end of last fixed sized
00485   // preallocated object.
00486   for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
00487     int64_t FixedOff;
00488     if (StackGrowsDown) {
00489       // The maximum distance from the stack pointer is at lower address of
00490       // the object -- which is given by offset. For down growing stack
00491       // the offset is negative, so we negate the offset to get the distance.
00492       FixedOff = -FFI->getObjectOffset(i);
00493     } else {
00494       // The maximum distance from the start pointer is at the upper
00495       // address of the object.
00496       FixedOff = FFI->getObjectOffset(i) + FFI->getObjectSize(i);
00497     }
00498     if (FixedOff > Offset) Offset = FixedOff;
00499   }
00500 
00501   // First assign frame offsets to stack objects that are used to spill
00502   // callee saved registers.
00503   if (StackGrowsDown) {
00504     for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
00505       // If stack grows down, we need to add size of find the lowest
00506       // address of the object.
00507       Offset += FFI->getObjectSize(i);
00508 
00509       unsigned Align = FFI->getObjectAlignment(i);
00510       // If the alignment of this object is greater than that of the stack,
00511       // then increase the stack alignment to match.
00512       MaxAlign = std::max(MaxAlign, Align);
00513       // Adjust to alignment boundary
00514       Offset = (Offset+Align-1)/Align*Align;
00515 
00516       FFI->setObjectOffset(i, -Offset);        // Set the computed offset
00517     }
00518   } else {
00519     int MaxCSFI = MaxCSFrameIndex, MinCSFI = MinCSFrameIndex;
00520     for (int i = MaxCSFI; i >= MinCSFI ; --i) {
00521       unsigned Align = FFI->getObjectAlignment(i);
00522       // If the alignment of this object is greater than that of the stack,
00523       // then increase the stack alignment to match.
00524       MaxAlign = std::max(MaxAlign, Align);
00525       // Adjust to alignment boundary
00526       Offset = (Offset+Align-1)/Align*Align;
00527 
00528       FFI->setObjectOffset(i, Offset);
00529       Offset += FFI->getObjectSize(i);
00530     }
00531   }
00532 
00533   // Make sure the special register scavenging spill slot is closest to the
00534   // frame pointer if a frame pointer is required.
00535   const TargetRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
00536   if (RS && RegInfo->hasFP(Fn)) {
00537     int SFI = RS->getScavengingFrameIndex();
00538     if (SFI >= 0)
00539       AdjustStackOffset(FFI, SFI, StackGrowsDown, Offset, MaxAlign);
00540   }
00541 
00542   // Make sure that the stack protector comes before the local variables on the
00543   // stack.
00544   if (FFI->getStackProtectorIndex() >= 0)
00545     AdjustStackOffset(FFI, FFI->getStackProtectorIndex(), StackGrowsDown,
00546                       Offset, MaxAlign);
00547 
00548   // Then assign frame offsets to stack objects that are not used to spill
00549   // callee saved registers.
00550   for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
00551     if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
00552       continue;
00553     if (RS && (int)i == RS->getScavengingFrameIndex())
00554       continue;
00555     if (FFI->isDeadObjectIndex(i))
00556       continue;
00557     if (FFI->getStackProtectorIndex() == (int)i)
00558       continue;
00559 
00560     AdjustStackOffset(FFI, i, StackGrowsDown, Offset, MaxAlign);
00561   }
00562 
00563   // Make sure the special register scavenging spill slot is closest to the
00564   // stack pointer.
00565   if (RS && !RegInfo->hasFP(Fn)) {
00566     int SFI = RS->getScavengingFrameIndex();
00567     if (SFI >= 0)
00568       AdjustStackOffset(FFI, SFI, StackGrowsDown, Offset, MaxAlign);
00569   }
00570 
00571   // Round up the size to a multiple of the alignment, but only if there are
00572   // calls or alloca's in the function.  This ensures that any calls to
00573   // subroutines have their stack frames suitable aligned.
00574   // Also do this if we need runtime alignment of the stack.  In this case
00575   // offsets will be relative to SP not FP; round up the stack size so this
00576   // works.
00577   if (!RegInfo->targetHandlesStackFrameRounding() &&
00578       (FFI->hasCalls() || FFI->hasVarSizedObjects() ||
00579        (RegInfo->needsStackRealignment(Fn) &&
00580         FFI->getObjectIndexEnd() != 0))) {
00581     // If we have reserved argument space for call sites in the function
00582     // immediately on entry to the current function, count it as part of the
00583     // overall stack size.
00584     if (RegInfo->hasReservedCallFrame(Fn))
00585       Offset += FFI->getMaxCallFrameSize();
00586 
00587     unsigned AlignMask = std::max(TFI.getStackAlignment(),MaxAlign) - 1;
00588     Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
00589   }
00590 
00591   // Update frame info to pretend that this is part of the stack...
00592   FFI->setStackSize(Offset+TFI.getOffsetOfLocalArea());
00593 
00594   // Remember the required stack alignment in case targets need it to perform
00595   // dynamic stack alignment.
00596   FFI->setMaxAlignment(MaxAlign);
00597 }
00598 
00599 
00600 /// insertPrologEpilogCode - Scan the function for modified callee saved
00601 /// registers, insert spill code for these callee saved registers, then add
00602 /// prolog and epilog code to the function.
00603 ///
00604 void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
00605   const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
00606 
00607   // Add prologue to the function...
00608   TRI->emitPrologue(Fn);
00609 
00610   // Add epilogue to restore the callee-save registers in each exiting block
00611   for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
00612     // If last instruction is a return instruction, add an epilogue
00613     if (!I->empty() && I->back().getDesc().isReturn())
00614       TRI->emitEpilogue(Fn, *I);
00615   }
00616 }
00617 
00618 
00619 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
00620 /// register references and actual offsets.
00621 ///
00622 void PEI::replaceFrameIndices(MachineFunction &Fn) {
00623   if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
00624 
00625   const TargetMachine &TM = Fn.getTarget();
00626   assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
00627   const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
00628   const TargetFrameInfo *TFI = TM.getFrameInfo();
00629   bool StackGrowsDown =
00630     TFI->getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
00631   int FrameSetupOpcode   = TRI.getCallFrameSetupOpcode();
00632   int FrameDestroyOpcode = TRI.getCallFrameDestroyOpcode();
00633 
00634   for (MachineFunction::iterator BB = Fn.begin(),
00635          E = Fn.end(); BB != E; ++BB) {
00636     int SPAdj = 0;  // SP offset due to call frame setup / destroy.
00637     if (RS) RS->enterBasicBlock(BB);
00638 
00639     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
00640       if (I->getOpcode() == TargetInstrInfo::DECLARE) {
00641         // Ignore it.
00642         ++I;
00643         continue;
00644       }
00645 
00646       if (I->getOpcode() == FrameSetupOpcode ||
00647           I->getOpcode() == FrameDestroyOpcode) {
00648         // Remember how much SP has been adjusted to create the call
00649         // frame.
00650         int Size = I->getOperand(0).getImm();
00651 
00652         if ((!StackGrowsDown && I->getOpcode() == FrameSetupOpcode) ||
00653             (StackGrowsDown && I->getOpcode() == FrameDestroyOpcode))
00654           Size = -Size;
00655 
00656         SPAdj += Size;
00657 
00658         MachineBasicBlock::iterator PrevI = BB->end();
00659         if (I != BB->begin()) PrevI = prior(I);
00660         TRI.eliminateCallFramePseudoInstr(Fn, *BB, I);
00661 
00662         // Visit the instructions created by eliminateCallFramePseudoInstr().
00663         if (PrevI == BB->end())
00664           I = BB->begin();     // The replaced instr was the first in the block.
00665         else
00666           I = next(PrevI);
00667         continue;
00668       }
00669 
00670       MachineInstr *MI = I;
00671       bool DoIncr = true;
00672       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
00673         if (MI->getOperand(i).isFI()) {
00674           // Some instructions (e.g. inline asm instructions) can have
00675           // multiple frame indices and/or cause eliminateFrameIndex
00676           // to insert more than one instruction. We need the register
00677           // scavenger to go through all of these instructions so that
00678           // it can update its register information. We keep the
00679           // iterator at the point before insertion so that we can
00680           // revisit them in full.
00681           bool AtBeginning = (I == BB->begin());
00682           if (!AtBeginning) --I;
00683 
00684           // If this instruction has a FrameIndex operand, we need to
00685           // use that target machine register info object to eliminate
00686           // it.
00687 
00688           TRI.eliminateFrameIndex(MI, SPAdj, RS);
00689 
00690           // Reset the iterator if we were at the beginning of the BB.
00691           if (AtBeginning) {
00692             I = BB->begin();
00693             DoIncr = false;
00694           }
00695 
00696           MI = 0;
00697           break;
00698         }
00699 
00700       if (DoIncr && I != BB->end()) ++I;
00701 
00702       // Update register states.
00703       if (RS && MI) RS->forward(MI);
00704     }
00705 
00706     assert(SPAdj == 0 && "Unbalanced call frame setup / destroy pairs?");
00707   }
00708 }
00709