LLVM API Documentation

LocalStackSlotAllocation.cpp
Go to the documentation of this file.
00001 //===- LocalStackSlotAllocation.cpp - Pre-allocate locals to stack slots --===//
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 assigns local frame indices to stack slots relative to one another
00011 // and allocates additional base registers to access them when the target
00012 // estimates they are likely to be out of range of stack pointer and frame
00013 // pointer relative addressing.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #include "llvm/CodeGen/Passes.h"
00018 #include "llvm/ADT/STLExtras.h"
00019 #include "llvm/ADT/SetVector.h"
00020 #include "llvm/ADT/SmallSet.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include "llvm/CodeGen/MachineFrameInfo.h"
00023 #include "llvm/CodeGen/MachineFunction.h"
00024 #include "llvm/CodeGen/MachineFunctionPass.h"
00025 #include "llvm/CodeGen/MachineRegisterInfo.h"
00026 #include "llvm/CodeGen/StackProtector.h"
00027 #include "llvm/IR/Constants.h"
00028 #include "llvm/IR/DerivedTypes.h"
00029 #include "llvm/IR/Instructions.h"
00030 #include "llvm/IR/Intrinsics.h"
00031 #include "llvm/IR/LLVMContext.h"
00032 #include "llvm/IR/Module.h"
00033 #include "llvm/Pass.h"
00034 #include "llvm/Support/Debug.h"
00035 #include "llvm/Support/ErrorHandling.h"
00036 #include "llvm/Support/raw_ostream.h"
00037 #include "llvm/Target/TargetFrameLowering.h"
00038 #include "llvm/Target/TargetRegisterInfo.h"
00039 #include "llvm/Target/TargetSubtargetInfo.h"
00040 
00041 using namespace llvm;
00042 
00043 #define DEBUG_TYPE "localstackalloc"
00044 
00045 STATISTIC(NumAllocations, "Number of frame indices allocated into local block");
00046 STATISTIC(NumBaseRegisters, "Number of virtual frame base registers allocated");
00047 STATISTIC(NumReplacements, "Number of frame indices references replaced");
00048 
00049 namespace {
00050   class FrameRef {
00051     MachineBasicBlock::iterator MI; // Instr referencing the frame
00052     int64_t LocalOffset;            // Local offset of the frame idx referenced
00053     int FrameIdx;                   // The frame index
00054   public:
00055     FrameRef(MachineBasicBlock::iterator I, int64_t Offset, int Idx) :
00056       MI(I), LocalOffset(Offset), FrameIdx(Idx) {}
00057     bool operator<(const FrameRef &RHS) const {
00058       return LocalOffset < RHS.LocalOffset;
00059     }
00060     MachineBasicBlock::iterator getMachineInstr() const { return MI; }
00061     int64_t getLocalOffset() const { return LocalOffset; }
00062     int getFrameIndex() const { return FrameIdx; }
00063   };
00064 
00065   class LocalStackSlotPass: public MachineFunctionPass {
00066     SmallVector<int64_t,16> LocalOffsets;
00067     /// StackObjSet - A set of stack object indexes
00068     typedef SmallSetVector<int, 8> StackObjSet;
00069 
00070     void AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx, int64_t &Offset,
00071                            bool StackGrowsDown, unsigned &MaxAlign);
00072     void AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
00073                                SmallSet<int, 16> &ProtectedObjs,
00074                                MachineFrameInfo *MFI, bool StackGrowsDown,
00075                                int64_t &Offset, unsigned &MaxAlign);
00076     void calculateFrameObjectOffsets(MachineFunction &Fn);
00077     bool insertFrameReferenceRegisters(MachineFunction &Fn);
00078   public:
00079     static char ID; // Pass identification, replacement for typeid
00080     explicit LocalStackSlotPass() : MachineFunctionPass(ID) { 
00081       initializeLocalStackSlotPassPass(*PassRegistry::getPassRegistry());
00082     }
00083     bool runOnMachineFunction(MachineFunction &MF) override;
00084 
00085     void getAnalysisUsage(AnalysisUsage &AU) const override {
00086       AU.setPreservesCFG();
00087       AU.addRequired<StackProtector>();
00088       MachineFunctionPass::getAnalysisUsage(AU);
00089     }
00090 
00091   private:
00092   };
00093 } // end anonymous namespace
00094 
00095 char LocalStackSlotPass::ID = 0;
00096 char &llvm::LocalStackSlotAllocationID = LocalStackSlotPass::ID;
00097 INITIALIZE_PASS_BEGIN(LocalStackSlotPass, "localstackalloc",
00098                       "Local Stack Slot Allocation", false, false)
00099 INITIALIZE_PASS_DEPENDENCY(StackProtector)
00100 INITIALIZE_PASS_END(LocalStackSlotPass, "localstackalloc",
00101                     "Local Stack Slot Allocation", false, false)
00102 
00103 
00104 bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) {
00105   MachineFrameInfo *MFI = MF.getFrameInfo();
00106   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
00107   unsigned LocalObjectCount = MFI->getObjectIndexEnd();
00108 
00109   // If the target doesn't want/need this pass, or if there are no locals
00110   // to consider, early exit.
00111   if (!TRI->requiresVirtualBaseRegisters(MF) || LocalObjectCount == 0)
00112     return true;
00113 
00114   // Make sure we have enough space to store the local offsets.
00115   LocalOffsets.resize(MFI->getObjectIndexEnd());
00116 
00117   // Lay out the local blob.
00118   calculateFrameObjectOffsets(MF);
00119 
00120   // Insert virtual base registers to resolve frame index references.
00121   bool UsedBaseRegs = insertFrameReferenceRegisters(MF);
00122 
00123   // Tell MFI whether any base registers were allocated. PEI will only
00124   // want to use the local block allocations from this pass if there were any.
00125   // Otherwise, PEI can do a bit better job of getting the alignment right
00126   // without a hole at the start since it knows the alignment of the stack
00127   // at the start of local allocation, and this pass doesn't.
00128   MFI->setUseLocalStackAllocationBlock(UsedBaseRegs);
00129 
00130   return true;
00131 }
00132 
00133 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
00134 void LocalStackSlotPass::AdjustStackOffset(MachineFrameInfo *MFI,
00135                                            int FrameIdx, int64_t &Offset,
00136                                            bool StackGrowsDown,
00137                                            unsigned &MaxAlign) {
00138   // If the stack grows down, add the object size to find the lowest address.
00139   if (StackGrowsDown)
00140     Offset += MFI->getObjectSize(FrameIdx);
00141 
00142   unsigned Align = MFI->getObjectAlignment(FrameIdx);
00143 
00144   // If the alignment of this object is greater than that of the stack, then
00145   // increase the stack alignment to match.
00146   MaxAlign = std::max(MaxAlign, Align);
00147 
00148   // Adjust to alignment boundary.
00149   Offset = (Offset + Align - 1) / Align * Align;
00150 
00151   int64_t LocalOffset = StackGrowsDown ? -Offset : Offset;
00152   DEBUG(dbgs() << "Allocate FI(" << FrameIdx << ") to local offset "
00153         << LocalOffset << "\n");
00154   // Keep the offset available for base register allocation
00155   LocalOffsets[FrameIdx] = LocalOffset;
00156   // And tell MFI about it for PEI to use later
00157   MFI->mapLocalFrameObject(FrameIdx, LocalOffset);
00158 
00159   if (!StackGrowsDown)
00160     Offset += MFI->getObjectSize(FrameIdx);
00161 
00162   ++NumAllocations;
00163 }
00164 
00165 /// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
00166 /// those required to be close to the Stack Protector) to stack offsets.
00167 void LocalStackSlotPass::AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
00168                                            SmallSet<int, 16> &ProtectedObjs,
00169                                            MachineFrameInfo *MFI,
00170                                            bool StackGrowsDown, int64_t &Offset,
00171                                            unsigned &MaxAlign) {
00172 
00173   for (StackObjSet::const_iterator I = UnassignedObjs.begin(),
00174         E = UnassignedObjs.end(); I != E; ++I) {
00175     int i = *I;
00176     AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign);
00177     ProtectedObjs.insert(i);
00178   }
00179 }
00180 
00181 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
00182 /// abstract stack objects.
00183 ///
00184 void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
00185   // Loop over all of the stack objects, assigning sequential addresses...
00186   MachineFrameInfo *MFI = Fn.getFrameInfo();
00187   const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
00188   bool StackGrowsDown =
00189     TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
00190   int64_t Offset = 0;
00191   unsigned MaxAlign = 0;
00192   StackProtector *SP = &getAnalysis<StackProtector>();
00193 
00194   // Make sure that the stack protector comes before the local variables on the
00195   // stack.
00196   SmallSet<int, 16> ProtectedObjs;
00197   if (MFI->getStackProtectorIndex() >= 0) {
00198     StackObjSet LargeArrayObjs;
00199     StackObjSet SmallArrayObjs;
00200     StackObjSet AddrOfObjs;
00201 
00202     AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), Offset,
00203                       StackGrowsDown, MaxAlign);
00204 
00205     // Assign large stack objects first.
00206     for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
00207       if (MFI->isDeadObjectIndex(i))
00208         continue;
00209       if (MFI->getStackProtectorIndex() == (int)i)
00210         continue;
00211 
00212       switch (SP->getSSPLayout(MFI->getObjectAllocation(i))) {
00213       case StackProtector::SSPLK_None:
00214         continue;
00215       case StackProtector::SSPLK_SmallArray:
00216         SmallArrayObjs.insert(i);
00217         continue;
00218       case StackProtector::SSPLK_AddrOf:
00219         AddrOfObjs.insert(i);
00220         continue;
00221       case StackProtector::SSPLK_LargeArray:
00222         LargeArrayObjs.insert(i);
00223         continue;
00224       }
00225       llvm_unreachable("Unexpected SSPLayoutKind.");
00226     }
00227 
00228     AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
00229                           Offset, MaxAlign);
00230     AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
00231                           Offset, MaxAlign);
00232     AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
00233                           Offset, MaxAlign);
00234   }
00235 
00236   // Then assign frame offsets to stack objects that are not used to spill
00237   // callee saved registers.
00238   for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
00239     if (MFI->isDeadObjectIndex(i))
00240       continue;
00241     if (MFI->getStackProtectorIndex() == (int)i)
00242       continue;
00243     if (ProtectedObjs.count(i))
00244       continue;
00245 
00246     AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign);
00247   }
00248 
00249   // Remember how big this blob of stack space is
00250   MFI->setLocalFrameSize(Offset);
00251   MFI->setLocalFrameMaxAlign(MaxAlign);
00252 }
00253 
00254 static inline bool
00255 lookupCandidateBaseReg(int64_t BaseOffset,
00256                        int64_t FrameSizeAdjust,
00257                        int64_t LocalFrameOffset,
00258                        const MachineInstr *MI,
00259                        const TargetRegisterInfo *TRI) {
00260   // Check if the relative offset from the where the base register references
00261   // to the target address is in range for the instruction.
00262   int64_t Offset = FrameSizeAdjust + LocalFrameOffset - BaseOffset;
00263   return TRI->isFrameOffsetLegal(MI, Offset);
00264 }
00265 
00266 bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) {
00267   // Scan the function's instructions looking for frame index references.
00268   // For each, ask the target if it wants a virtual base register for it
00269   // based on what we can tell it about where the local will end up in the
00270   // stack frame. If it wants one, re-use a suitable one we've previously
00271   // allocated, or if there isn't one that fits the bill, allocate a new one
00272   // and ask the target to create a defining instruction for it.
00273   bool UsedBaseReg = false;
00274 
00275   MachineFrameInfo *MFI = Fn.getFrameInfo();
00276   const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo();
00277   const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
00278   bool StackGrowsDown =
00279     TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
00280 
00281   // Collect all of the instructions in the block that reference
00282   // a frame index. Also store the frame index referenced to ease later
00283   // lookup. (For any insn that has more than one FI reference, we arbitrarily
00284   // choose the first one).
00285   SmallVector<FrameRef, 64> FrameReferenceInsns;
00286 
00287   for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
00288     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
00289       MachineInstr *MI = I;
00290 
00291       // Debug value, stackmap and patchpoint instructions can't be out of
00292       // range, so they don't need any updates.
00293       if (MI->isDebugValue() ||
00294           MI->getOpcode() == TargetOpcode::STACKMAP ||
00295           MI->getOpcode() == TargetOpcode::PATCHPOINT)
00296         continue;
00297 
00298       // For now, allocate the base register(s) within the basic block
00299       // where they're used, and don't try to keep them around outside
00300       // of that. It may be beneficial to try sharing them more broadly
00301       // than that, but the increased register pressure makes that a
00302       // tricky thing to balance. Investigate if re-materializing these
00303       // becomes an issue.
00304       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00305         // Consider replacing all frame index operands that reference
00306         // an object allocated in the local block.
00307         if (MI->getOperand(i).isFI()) {
00308           // Don't try this with values not in the local block.
00309           if (!MFI->isObjectPreAllocated(MI->getOperand(i).getIndex()))
00310             break;
00311           int Idx = MI->getOperand(i).getIndex();
00312           int64_t LocalOffset = LocalOffsets[Idx];
00313           if (!TRI->needsFrameBaseReg(MI, LocalOffset))
00314             break;
00315           FrameReferenceInsns.
00316             push_back(FrameRef(MI, LocalOffset, Idx));
00317           break;
00318         }
00319       }
00320     }
00321   }
00322 
00323   // Sort the frame references by local offset
00324   array_pod_sort(FrameReferenceInsns.begin(), FrameReferenceInsns.end());
00325 
00326   MachineBasicBlock *Entry = Fn.begin();
00327 
00328   unsigned BaseReg = 0;
00329   int64_t BaseOffset = 0;
00330 
00331   // Loop through the frame references and allocate for them as necessary.
00332   for (int ref = 0, e = FrameReferenceInsns.size(); ref < e ; ++ref) {
00333     FrameRef &FR = FrameReferenceInsns[ref];
00334     MachineBasicBlock::iterator I = FR.getMachineInstr();
00335     MachineInstr *MI = I;
00336     int64_t LocalOffset = FR.getLocalOffset();
00337     int FrameIdx = FR.getFrameIndex();
00338     assert(MFI->isObjectPreAllocated(FrameIdx) &&
00339            "Only pre-allocated locals expected!");
00340 
00341     DEBUG(dbgs() << "Considering: " << *MI);
00342 
00343     unsigned idx = 0;
00344     for (unsigned f = MI->getNumOperands(); idx != f; ++idx) {
00345       if (!MI->getOperand(idx).isFI())
00346         continue;
00347 
00348       if (FrameIdx == I->getOperand(idx).getIndex())
00349         break;
00350     }
00351 
00352     assert(idx < MI->getNumOperands() && "Cannot find FI operand");
00353 
00354     int64_t Offset = 0;
00355     int64_t FrameSizeAdjust = StackGrowsDown ? MFI->getLocalFrameSize() : 0;
00356 
00357     DEBUG(dbgs() << "  Replacing FI in: " << *MI);
00358 
00359     // If we have a suitable base register available, use it; otherwise
00360     // create a new one. Note that any offset encoded in the
00361     // instruction itself will be taken into account by the target,
00362     // so we don't have to adjust for it here when reusing a base
00363     // register.
00364     if (UsedBaseReg && lookupCandidateBaseReg(BaseOffset, FrameSizeAdjust,
00365                                               LocalOffset, MI, TRI)) {
00366       DEBUG(dbgs() << "  Reusing base register " << BaseReg << "\n");
00367       // We found a register to reuse.
00368       Offset = FrameSizeAdjust + LocalOffset - BaseOffset;
00369     } else {
00370       // No previously defined register was in range, so create a // new one.
00371  
00372       int64_t InstrOffset = TRI->getFrameIndexInstrOffset(MI, idx);
00373 
00374       int64_t PrevBaseOffset = BaseOffset;
00375       BaseOffset = FrameSizeAdjust + LocalOffset + InstrOffset;
00376 
00377       // We'd like to avoid creating single-use virtual base registers.
00378       // Because the FrameRefs are in sorted order, and we've already
00379       // processed all FrameRefs before this one, just check whether or not
00380       // the next FrameRef will be able to reuse this new register. If not,
00381       // then don't bother creating it.
00382       if (ref + 1 >= e ||
00383           !lookupCandidateBaseReg(
00384               BaseOffset, FrameSizeAdjust,
00385               FrameReferenceInsns[ref + 1].getLocalOffset(),
00386               FrameReferenceInsns[ref + 1].getMachineInstr(), TRI)) {
00387         BaseOffset = PrevBaseOffset;
00388         continue;
00389       }
00390 
00391       const MachineFunction *MF = MI->getParent()->getParent();
00392       const TargetRegisterClass *RC = TRI->getPointerRegClass(*MF);
00393       BaseReg = Fn.getRegInfo().createVirtualRegister(RC);
00394 
00395       DEBUG(dbgs() << "  Materializing base register " << BaseReg <<
00396             " at frame local offset " << LocalOffset + InstrOffset << "\n");
00397 
00398       // Tell the target to insert the instruction to initialize
00399       // the base register.
00400       //            MachineBasicBlock::iterator InsertionPt = Entry->begin();
00401       TRI->materializeFrameBaseRegister(Entry, BaseReg, FrameIdx,
00402                                         InstrOffset);
00403 
00404       // The base register already includes any offset specified
00405       // by the instruction, so account for that so it doesn't get
00406       // applied twice.
00407       Offset = -InstrOffset;
00408 
00409       ++NumBaseRegisters;
00410       UsedBaseReg = true;
00411     }
00412     assert(BaseReg != 0 && "Unable to allocate virtual base register!");
00413 
00414     // Modify the instruction to use the new base register rather
00415     // than the frame index operand.
00416     TRI->resolveFrameIndex(*I, BaseReg, Offset);
00417     DEBUG(dbgs() << "Resolved: " << *MI);
00418 
00419     ++NumReplacements;
00420   }
00421 
00422   return UsedBaseReg;
00423 }