1e580952dSDimitry Andric //===- LocalStackSlotAllocation.cpp - Pre-allocate locals to stack slots --===// 2e580952dSDimitry Andric // 3e580952dSDimitry Andric // The LLVM Compiler Infrastructure 4e580952dSDimitry Andric // 5e580952dSDimitry Andric // This file is distributed under the University of Illinois Open Source 6e580952dSDimitry Andric // License. See LICENSE.TXT for details. 7e580952dSDimitry Andric // 8e580952dSDimitry Andric //===----------------------------------------------------------------------===// 9e580952dSDimitry Andric // 10e580952dSDimitry Andric // This pass assigns local frame indices to stack slots relative to one another 11e580952dSDimitry Andric // and allocates additional base registers to access them when the target 122754fe60SDimitry Andric // estimates they are likely to be out of range of stack pointer and frame 13e580952dSDimitry Andric // pointer relative addressing. 14e580952dSDimitry Andric // 15e580952dSDimitry Andric //===----------------------------------------------------------------------===// 16e580952dSDimitry Andric 17139f7f9bSDimitry Andric #include "llvm/CodeGen/Passes.h" 18139f7f9bSDimitry Andric #include "llvm/ADT/STLExtras.h" 1991bc56edSDimitry Andric #include "llvm/ADT/SetVector.h" 20e580952dSDimitry Andric #include "llvm/ADT/SmallSet.h" 21e580952dSDimitry Andric #include "llvm/ADT/Statistic.h" 22e580952dSDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 23e580952dSDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 24e580952dSDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 25e580952dSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 2691bc56edSDimitry Andric #include "llvm/CodeGen/StackProtector.h" 27139f7f9bSDimitry Andric #include "llvm/IR/Constants.h" 28139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h" 29139f7f9bSDimitry Andric #include "llvm/IR/Instructions.h" 30139f7f9bSDimitry Andric #include "llvm/IR/Intrinsics.h" 31139f7f9bSDimitry Andric #include "llvm/IR/LLVMContext.h" 32139f7f9bSDimitry Andric #include "llvm/IR/Module.h" 33139f7f9bSDimitry Andric #include "llvm/Pass.h" 34e580952dSDimitry Andric #include "llvm/Support/Debug.h" 35e580952dSDimitry Andric #include "llvm/Support/ErrorHandling.h" 36e580952dSDimitry Andric #include "llvm/Support/raw_ostream.h" 372754fe60SDimitry Andric #include "llvm/Target/TargetFrameLowering.h" 38139f7f9bSDimitry Andric #include "llvm/Target/TargetRegisterInfo.h" 3939d628a0SDimitry Andric #include "llvm/Target/TargetSubtargetInfo.h" 40e580952dSDimitry Andric 41e580952dSDimitry Andric using namespace llvm; 42e580952dSDimitry Andric 4391bc56edSDimitry Andric #define DEBUG_TYPE "localstackalloc" 4491bc56edSDimitry Andric 45e580952dSDimitry Andric STATISTIC(NumAllocations, "Number of frame indices allocated into local block"); 46e580952dSDimitry Andric STATISTIC(NumBaseRegisters, "Number of virtual frame base registers allocated"); 47e580952dSDimitry Andric STATISTIC(NumReplacements, "Number of frame indices references replaced"); 48e580952dSDimitry Andric 49e580952dSDimitry Andric namespace { 50e580952dSDimitry Andric class FrameRef { 51e580952dSDimitry Andric MachineBasicBlock::iterator MI; // Instr referencing the frame 52e580952dSDimitry Andric int64_t LocalOffset; // Local offset of the frame idx referenced 53284c1978SDimitry Andric int FrameIdx; // The frame index 54e580952dSDimitry Andric public: 55284c1978SDimitry Andric FrameRef(MachineBasicBlock::iterator I, int64_t Offset, int Idx) : 56284c1978SDimitry Andric MI(I), LocalOffset(Offset), FrameIdx(Idx) {} 57e580952dSDimitry Andric bool operator<(const FrameRef &RHS) const { 58e580952dSDimitry Andric return LocalOffset < RHS.LocalOffset; 59e580952dSDimitry Andric } 60284c1978SDimitry Andric MachineBasicBlock::iterator getMachineInstr() const { return MI; } 61284c1978SDimitry Andric int64_t getLocalOffset() const { return LocalOffset; } 62284c1978SDimitry Andric int getFrameIndex() const { return FrameIdx; } 63e580952dSDimitry Andric }; 64e580952dSDimitry Andric 65e580952dSDimitry Andric class LocalStackSlotPass: public MachineFunctionPass { 66e580952dSDimitry Andric SmallVector<int64_t,16> LocalOffsets; 6791bc56edSDimitry Andric /// StackObjSet - A set of stack object indexes 6891bc56edSDimitry Andric typedef SmallSetVector<int, 8> StackObjSet; 69e580952dSDimitry Andric 70e580952dSDimitry Andric void AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx, int64_t &Offset, 71e580952dSDimitry Andric bool StackGrowsDown, unsigned &MaxAlign); 7291bc56edSDimitry Andric void AssignProtectedObjSet(const StackObjSet &UnassignedObjs, 7391bc56edSDimitry Andric SmallSet<int, 16> &ProtectedObjs, 7491bc56edSDimitry Andric MachineFrameInfo *MFI, bool StackGrowsDown, 7591bc56edSDimitry Andric int64_t &Offset, unsigned &MaxAlign); 76e580952dSDimitry Andric void calculateFrameObjectOffsets(MachineFunction &Fn); 77e580952dSDimitry Andric bool insertFrameReferenceRegisters(MachineFunction &Fn); 78e580952dSDimitry Andric public: 79e580952dSDimitry Andric static char ID; // Pass identification, replacement for typeid 8091bc56edSDimitry Andric explicit LocalStackSlotPass() : MachineFunctionPass(ID) { 8191bc56edSDimitry Andric initializeLocalStackSlotPassPass(*PassRegistry::getPassRegistry()); 8291bc56edSDimitry Andric } 8391bc56edSDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 84e580952dSDimitry Andric 8591bc56edSDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 86e580952dSDimitry Andric AU.setPreservesCFG(); 8791bc56edSDimitry Andric AU.addRequired<StackProtector>(); 88e580952dSDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 89e580952dSDimitry Andric } 90e580952dSDimitry Andric 91e580952dSDimitry Andric private: 92e580952dSDimitry Andric }; 93e580952dSDimitry Andric } // end anonymous namespace 94e580952dSDimitry Andric 95e580952dSDimitry Andric char LocalStackSlotPass::ID = 0; 96dff0c46cSDimitry Andric char &llvm::LocalStackSlotAllocationID = LocalStackSlotPass::ID; 9791bc56edSDimitry Andric INITIALIZE_PASS_BEGIN(LocalStackSlotPass, "localstackalloc", 98dff0c46cSDimitry Andric "Local Stack Slot Allocation", false, false) 9991bc56edSDimitry Andric INITIALIZE_PASS_DEPENDENCY(StackProtector) 10091bc56edSDimitry Andric INITIALIZE_PASS_END(LocalStackSlotPass, "localstackalloc", 10191bc56edSDimitry Andric "Local Stack Slot Allocation", false, false) 10291bc56edSDimitry Andric 103e580952dSDimitry Andric 104e580952dSDimitry Andric bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) { 105e580952dSDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 10639d628a0SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 107e580952dSDimitry Andric unsigned LocalObjectCount = MFI->getObjectIndexEnd(); 108e580952dSDimitry Andric 109e580952dSDimitry Andric // If the target doesn't want/need this pass, or if there are no locals 110e580952dSDimitry Andric // to consider, early exit. 111e580952dSDimitry Andric if (!TRI->requiresVirtualBaseRegisters(MF) || LocalObjectCount == 0) 112e580952dSDimitry Andric return true; 113e580952dSDimitry Andric 114e580952dSDimitry Andric // Make sure we have enough space to store the local offsets. 115e580952dSDimitry Andric LocalOffsets.resize(MFI->getObjectIndexEnd()); 116e580952dSDimitry Andric 117e580952dSDimitry Andric // Lay out the local blob. 118e580952dSDimitry Andric calculateFrameObjectOffsets(MF); 119e580952dSDimitry Andric 120e580952dSDimitry Andric // Insert virtual base registers to resolve frame index references. 121e580952dSDimitry Andric bool UsedBaseRegs = insertFrameReferenceRegisters(MF); 122e580952dSDimitry Andric 123e580952dSDimitry Andric // Tell MFI whether any base registers were allocated. PEI will only 124e580952dSDimitry Andric // want to use the local block allocations from this pass if there were any. 125e580952dSDimitry Andric // Otherwise, PEI can do a bit better job of getting the alignment right 126e580952dSDimitry Andric // without a hole at the start since it knows the alignment of the stack 127e580952dSDimitry Andric // at the start of local allocation, and this pass doesn't. 128e580952dSDimitry Andric MFI->setUseLocalStackAllocationBlock(UsedBaseRegs); 129e580952dSDimitry Andric 130e580952dSDimitry Andric return true; 131e580952dSDimitry Andric } 132e580952dSDimitry Andric 133e580952dSDimitry Andric /// AdjustStackOffset - Helper function used to adjust the stack frame offset. 134e580952dSDimitry Andric void LocalStackSlotPass::AdjustStackOffset(MachineFrameInfo *MFI, 135e580952dSDimitry Andric int FrameIdx, int64_t &Offset, 136e580952dSDimitry Andric bool StackGrowsDown, 137e580952dSDimitry Andric unsigned &MaxAlign) { 138e580952dSDimitry Andric // If the stack grows down, add the object size to find the lowest address. 139e580952dSDimitry Andric if (StackGrowsDown) 140e580952dSDimitry Andric Offset += MFI->getObjectSize(FrameIdx); 141e580952dSDimitry Andric 142e580952dSDimitry Andric unsigned Align = MFI->getObjectAlignment(FrameIdx); 143e580952dSDimitry Andric 144e580952dSDimitry Andric // If the alignment of this object is greater than that of the stack, then 145e580952dSDimitry Andric // increase the stack alignment to match. 146e580952dSDimitry Andric MaxAlign = std::max(MaxAlign, Align); 147e580952dSDimitry Andric 148e580952dSDimitry Andric // Adjust to alignment boundary. 149e580952dSDimitry Andric Offset = (Offset + Align - 1) / Align * Align; 150e580952dSDimitry Andric 151e580952dSDimitry Andric int64_t LocalOffset = StackGrowsDown ? -Offset : Offset; 152e580952dSDimitry Andric DEBUG(dbgs() << "Allocate FI(" << FrameIdx << ") to local offset " 153e580952dSDimitry Andric << LocalOffset << "\n"); 154e580952dSDimitry Andric // Keep the offset available for base register allocation 155e580952dSDimitry Andric LocalOffsets[FrameIdx] = LocalOffset; 156e580952dSDimitry Andric // And tell MFI about it for PEI to use later 157e580952dSDimitry Andric MFI->mapLocalFrameObject(FrameIdx, LocalOffset); 158e580952dSDimitry Andric 159e580952dSDimitry Andric if (!StackGrowsDown) 160e580952dSDimitry Andric Offset += MFI->getObjectSize(FrameIdx); 161e580952dSDimitry Andric 162e580952dSDimitry Andric ++NumAllocations; 163e580952dSDimitry Andric } 164e580952dSDimitry Andric 16591bc56edSDimitry Andric /// AssignProtectedObjSet - Helper function to assign large stack objects (i.e., 16691bc56edSDimitry Andric /// those required to be close to the Stack Protector) to stack offsets. 16791bc56edSDimitry Andric void LocalStackSlotPass::AssignProtectedObjSet(const StackObjSet &UnassignedObjs, 16891bc56edSDimitry Andric SmallSet<int, 16> &ProtectedObjs, 16991bc56edSDimitry Andric MachineFrameInfo *MFI, 17091bc56edSDimitry Andric bool StackGrowsDown, int64_t &Offset, 17191bc56edSDimitry Andric unsigned &MaxAlign) { 17291bc56edSDimitry Andric 17391bc56edSDimitry Andric for (StackObjSet::const_iterator I = UnassignedObjs.begin(), 17491bc56edSDimitry Andric E = UnassignedObjs.end(); I != E; ++I) { 17591bc56edSDimitry Andric int i = *I; 17691bc56edSDimitry Andric AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign); 17791bc56edSDimitry Andric ProtectedObjs.insert(i); 17891bc56edSDimitry Andric } 17991bc56edSDimitry Andric } 18091bc56edSDimitry Andric 181e580952dSDimitry Andric /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the 182e580952dSDimitry Andric /// abstract stack objects. 183e580952dSDimitry Andric /// 184e580952dSDimitry Andric void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) { 185e580952dSDimitry Andric // Loop over all of the stack objects, assigning sequential addresses... 186e580952dSDimitry Andric MachineFrameInfo *MFI = Fn.getFrameInfo(); 18739d628a0SDimitry Andric const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering(); 188e580952dSDimitry Andric bool StackGrowsDown = 1892754fe60SDimitry Andric TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown; 190e580952dSDimitry Andric int64_t Offset = 0; 191e580952dSDimitry Andric unsigned MaxAlign = 0; 19291bc56edSDimitry Andric StackProtector *SP = &getAnalysis<StackProtector>(); 193e580952dSDimitry Andric 194e580952dSDimitry Andric // Make sure that the stack protector comes before the local variables on the 195e580952dSDimitry Andric // stack. 19691bc56edSDimitry Andric SmallSet<int, 16> ProtectedObjs; 197e580952dSDimitry Andric if (MFI->getStackProtectorIndex() >= 0) { 19891bc56edSDimitry Andric StackObjSet LargeArrayObjs; 19991bc56edSDimitry Andric StackObjSet SmallArrayObjs; 20091bc56edSDimitry Andric StackObjSet AddrOfObjs; 20191bc56edSDimitry Andric 202e580952dSDimitry Andric AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), Offset, 203e580952dSDimitry Andric StackGrowsDown, MaxAlign); 204e580952dSDimitry Andric 205e580952dSDimitry Andric // Assign large stack objects first. 206e580952dSDimitry Andric for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { 207e580952dSDimitry Andric if (MFI->isDeadObjectIndex(i)) 208e580952dSDimitry Andric continue; 209e580952dSDimitry Andric if (MFI->getStackProtectorIndex() == (int)i) 210e580952dSDimitry Andric continue; 211e580952dSDimitry Andric 21291bc56edSDimitry Andric switch (SP->getSSPLayout(MFI->getObjectAllocation(i))) { 21391bc56edSDimitry Andric case StackProtector::SSPLK_None: 21491bc56edSDimitry Andric continue; 21591bc56edSDimitry Andric case StackProtector::SSPLK_SmallArray: 21691bc56edSDimitry Andric SmallArrayObjs.insert(i); 21791bc56edSDimitry Andric continue; 21891bc56edSDimitry Andric case StackProtector::SSPLK_AddrOf: 21991bc56edSDimitry Andric AddrOfObjs.insert(i); 22091bc56edSDimitry Andric continue; 22191bc56edSDimitry Andric case StackProtector::SSPLK_LargeArray: 22291bc56edSDimitry Andric LargeArrayObjs.insert(i); 22391bc56edSDimitry Andric continue; 224e580952dSDimitry Andric } 22591bc56edSDimitry Andric llvm_unreachable("Unexpected SSPLayoutKind."); 22691bc56edSDimitry Andric } 22791bc56edSDimitry Andric 22891bc56edSDimitry Andric AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown, 22991bc56edSDimitry Andric Offset, MaxAlign); 23091bc56edSDimitry Andric AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown, 23191bc56edSDimitry Andric Offset, MaxAlign); 23291bc56edSDimitry Andric AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown, 23391bc56edSDimitry Andric Offset, MaxAlign); 234e580952dSDimitry Andric } 235e580952dSDimitry Andric 236e580952dSDimitry Andric // Then assign frame offsets to stack objects that are not used to spill 237e580952dSDimitry Andric // callee saved registers. 238e580952dSDimitry Andric for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { 239e580952dSDimitry Andric if (MFI->isDeadObjectIndex(i)) 240e580952dSDimitry Andric continue; 241e580952dSDimitry Andric if (MFI->getStackProtectorIndex() == (int)i) 242e580952dSDimitry Andric continue; 24391bc56edSDimitry Andric if (ProtectedObjs.count(i)) 244e580952dSDimitry Andric continue; 245e580952dSDimitry Andric 246e580952dSDimitry Andric AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign); 247e580952dSDimitry Andric } 248e580952dSDimitry Andric 249e580952dSDimitry Andric // Remember how big this blob of stack space is 250e580952dSDimitry Andric MFI->setLocalFrameSize(Offset); 251e580952dSDimitry Andric MFI->setLocalFrameMaxAlign(MaxAlign); 252e580952dSDimitry Andric } 253e580952dSDimitry Andric 254e580952dSDimitry Andric static inline bool 255ff0cc061SDimitry Andric lookupCandidateBaseReg(unsigned BaseReg, 256ff0cc061SDimitry Andric int64_t BaseOffset, 257e580952dSDimitry Andric int64_t FrameSizeAdjust, 258e580952dSDimitry Andric int64_t LocalFrameOffset, 259e580952dSDimitry Andric const MachineInstr *MI, 260e580952dSDimitry Andric const TargetRegisterInfo *TRI) { 261e580952dSDimitry Andric // Check if the relative offset from the where the base register references 262e580952dSDimitry Andric // to the target address is in range for the instruction. 263284c1978SDimitry Andric int64_t Offset = FrameSizeAdjust + LocalFrameOffset - BaseOffset; 264ff0cc061SDimitry Andric return TRI->isFrameOffsetLegal(MI, BaseReg, Offset); 265e580952dSDimitry Andric } 266e580952dSDimitry Andric 267e580952dSDimitry Andric bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) { 268e580952dSDimitry Andric // Scan the function's instructions looking for frame index references. 269e580952dSDimitry Andric // For each, ask the target if it wants a virtual base register for it 270e580952dSDimitry Andric // based on what we can tell it about where the local will end up in the 271e580952dSDimitry Andric // stack frame. If it wants one, re-use a suitable one we've previously 272e580952dSDimitry Andric // allocated, or if there isn't one that fits the bill, allocate a new one 273e580952dSDimitry Andric // and ask the target to create a defining instruction for it. 274e580952dSDimitry Andric bool UsedBaseReg = false; 275e580952dSDimitry Andric 276e580952dSDimitry Andric MachineFrameInfo *MFI = Fn.getFrameInfo(); 27739d628a0SDimitry Andric const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo(); 27839d628a0SDimitry Andric const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering(); 279e580952dSDimitry Andric bool StackGrowsDown = 2802754fe60SDimitry Andric TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown; 281e580952dSDimitry Andric 282e580952dSDimitry Andric // Collect all of the instructions in the block that reference 283e580952dSDimitry Andric // a frame index. Also store the frame index referenced to ease later 284e580952dSDimitry Andric // lookup. (For any insn that has more than one FI reference, we arbitrarily 285e580952dSDimitry Andric // choose the first one). 286e580952dSDimitry Andric SmallVector<FrameRef, 64> FrameReferenceInsns; 2872754fe60SDimitry Andric 288e580952dSDimitry Andric for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { 289e580952dSDimitry Andric for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { 290e580952dSDimitry Andric MachineInstr *MI = I; 2912754fe60SDimitry Andric 29291bc56edSDimitry Andric // Debug value, stackmap and patchpoint instructions can't be out of 29391bc56edSDimitry Andric // range, so they don't need any updates. 29491bc56edSDimitry Andric if (MI->isDebugValue() || 29539d628a0SDimitry Andric MI->getOpcode() == TargetOpcode::STATEPOINT || 29691bc56edSDimitry Andric MI->getOpcode() == TargetOpcode::STACKMAP || 29791bc56edSDimitry Andric MI->getOpcode() == TargetOpcode::PATCHPOINT) 298e580952dSDimitry Andric continue; 2992754fe60SDimitry Andric 300e580952dSDimitry Andric // For now, allocate the base register(s) within the basic block 301e580952dSDimitry Andric // where they're used, and don't try to keep them around outside 302e580952dSDimitry Andric // of that. It may be beneficial to try sharing them more broadly 303e580952dSDimitry Andric // than that, but the increased register pressure makes that a 304e580952dSDimitry Andric // tricky thing to balance. Investigate if re-materializing these 305e580952dSDimitry Andric // becomes an issue. 306e580952dSDimitry Andric for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 307e580952dSDimitry Andric // Consider replacing all frame index operands that reference 308e580952dSDimitry Andric // an object allocated in the local block. 309e580952dSDimitry Andric if (MI->getOperand(i).isFI()) { 310e580952dSDimitry Andric // Don't try this with values not in the local block. 311e580952dSDimitry Andric if (!MFI->isObjectPreAllocated(MI->getOperand(i).getIndex())) 312e580952dSDimitry Andric break; 313284c1978SDimitry Andric int Idx = MI->getOperand(i).getIndex(); 314284c1978SDimitry Andric int64_t LocalOffset = LocalOffsets[Idx]; 315284c1978SDimitry Andric if (!TRI->needsFrameBaseReg(MI, LocalOffset)) 316284c1978SDimitry Andric break; 317e580952dSDimitry Andric FrameReferenceInsns. 318284c1978SDimitry Andric push_back(FrameRef(MI, LocalOffset, Idx)); 319e580952dSDimitry Andric break; 320e580952dSDimitry Andric } 321e580952dSDimitry Andric } 322e580952dSDimitry Andric } 323e580952dSDimitry Andric } 3242754fe60SDimitry Andric 325e580952dSDimitry Andric // Sort the frame references by local offset 326e580952dSDimitry Andric array_pod_sort(FrameReferenceInsns.begin(), FrameReferenceInsns.end()); 327e580952dSDimitry Andric 3282754fe60SDimitry Andric MachineBasicBlock *Entry = Fn.begin(); 329e580952dSDimitry Andric 330284c1978SDimitry Andric unsigned BaseReg = 0; 331284c1978SDimitry Andric int64_t BaseOffset = 0; 332284c1978SDimitry Andric 3332754fe60SDimitry Andric // Loop through the frame references and allocate for them as necessary. 334e580952dSDimitry Andric for (int ref = 0, e = FrameReferenceInsns.size(); ref < e ; ++ref) { 335284c1978SDimitry Andric FrameRef &FR = FrameReferenceInsns[ref]; 336284c1978SDimitry Andric MachineBasicBlock::iterator I = FR.getMachineInstr(); 337e580952dSDimitry Andric MachineInstr *MI = I; 338284c1978SDimitry Andric int64_t LocalOffset = FR.getLocalOffset(); 339284c1978SDimitry Andric int FrameIdx = FR.getFrameIndex(); 340e580952dSDimitry Andric assert(MFI->isObjectPreAllocated(FrameIdx) && 341e580952dSDimitry Andric "Only pre-allocated locals expected!"); 342e580952dSDimitry Andric 343e580952dSDimitry Andric DEBUG(dbgs() << "Considering: " << *MI); 344284c1978SDimitry Andric 345284c1978SDimitry Andric unsigned idx = 0; 346284c1978SDimitry Andric for (unsigned f = MI->getNumOperands(); idx != f; ++idx) { 347284c1978SDimitry Andric if (!MI->getOperand(idx).isFI()) 348284c1978SDimitry Andric continue; 349284c1978SDimitry Andric 350284c1978SDimitry Andric if (FrameIdx == I->getOperand(idx).getIndex()) 351284c1978SDimitry Andric break; 352284c1978SDimitry Andric } 353284c1978SDimitry Andric 354284c1978SDimitry Andric assert(idx < MI->getNumOperands() && "Cannot find FI operand"); 355284c1978SDimitry Andric 356e580952dSDimitry Andric int64_t Offset = 0; 357284c1978SDimitry Andric int64_t FrameSizeAdjust = StackGrowsDown ? MFI->getLocalFrameSize() : 0; 358e580952dSDimitry Andric 359e580952dSDimitry Andric DEBUG(dbgs() << " Replacing FI in: " << *MI); 360e580952dSDimitry Andric 361e580952dSDimitry Andric // If we have a suitable base register available, use it; otherwise 362e580952dSDimitry Andric // create a new one. Note that any offset encoded in the 363e580952dSDimitry Andric // instruction itself will be taken into account by the target, 364e580952dSDimitry Andric // so we don't have to adjust for it here when reusing a base 365e580952dSDimitry Andric // register. 366ff0cc061SDimitry Andric if (UsedBaseReg && lookupCandidateBaseReg(BaseReg, BaseOffset, 367ff0cc061SDimitry Andric FrameSizeAdjust, LocalOffset, MI, 368ff0cc061SDimitry Andric TRI)) { 369284c1978SDimitry Andric DEBUG(dbgs() << " Reusing base register " << BaseReg << "\n"); 370e580952dSDimitry Andric // We found a register to reuse. 371284c1978SDimitry Andric Offset = FrameSizeAdjust + LocalOffset - BaseOffset; 372e580952dSDimitry Andric } else { 373284c1978SDimitry Andric // No previously defined register was in range, so create a // new one. 374284c1978SDimitry Andric 375e580952dSDimitry Andric int64_t InstrOffset = TRI->getFrameIndexInstrOffset(MI, idx); 376284c1978SDimitry Andric 377284c1978SDimitry Andric int64_t PrevBaseOffset = BaseOffset; 378284c1978SDimitry Andric BaseOffset = FrameSizeAdjust + LocalOffset + InstrOffset; 379284c1978SDimitry Andric 380284c1978SDimitry Andric // We'd like to avoid creating single-use virtual base registers. 381284c1978SDimitry Andric // Because the FrameRefs are in sorted order, and we've already 382284c1978SDimitry Andric // processed all FrameRefs before this one, just check whether or not 383284c1978SDimitry Andric // the next FrameRef will be able to reuse this new register. If not, 384284c1978SDimitry Andric // then don't bother creating it. 38591bc56edSDimitry Andric if (ref + 1 >= e || 38691bc56edSDimitry Andric !lookupCandidateBaseReg( 387ff0cc061SDimitry Andric BaseReg, BaseOffset, FrameSizeAdjust, 38891bc56edSDimitry Andric FrameReferenceInsns[ref + 1].getLocalOffset(), 38991bc56edSDimitry Andric FrameReferenceInsns[ref + 1].getMachineInstr(), TRI)) { 390284c1978SDimitry Andric BaseOffset = PrevBaseOffset; 391284c1978SDimitry Andric continue; 392284c1978SDimitry Andric } 393284c1978SDimitry Andric 3947ae0e2c9SDimitry Andric const MachineFunction *MF = MI->getParent()->getParent(); 3957ae0e2c9SDimitry Andric const TargetRegisterClass *RC = TRI->getPointerRegClass(*MF); 396e580952dSDimitry Andric BaseReg = Fn.getRegInfo().createVirtualRegister(RC); 397e580952dSDimitry Andric 398e580952dSDimitry Andric DEBUG(dbgs() << " Materializing base register " << BaseReg << 399284c1978SDimitry Andric " at frame local offset " << LocalOffset + InstrOffset << "\n"); 4002754fe60SDimitry Andric 401e580952dSDimitry Andric // Tell the target to insert the instruction to initialize 402e580952dSDimitry Andric // the base register. 4032754fe60SDimitry Andric // MachineBasicBlock::iterator InsertionPt = Entry->begin(); 4042754fe60SDimitry Andric TRI->materializeFrameBaseRegister(Entry, BaseReg, FrameIdx, 4052754fe60SDimitry Andric InstrOffset); 406e580952dSDimitry Andric 407e580952dSDimitry Andric // The base register already includes any offset specified 408e580952dSDimitry Andric // by the instruction, so account for that so it doesn't get 409e580952dSDimitry Andric // applied twice. 410e580952dSDimitry Andric Offset = -InstrOffset; 411e580952dSDimitry Andric 412e580952dSDimitry Andric ++NumBaseRegisters; 413e580952dSDimitry Andric UsedBaseReg = true; 414e580952dSDimitry Andric } 415e580952dSDimitry Andric assert(BaseReg != 0 && "Unable to allocate virtual base register!"); 416e580952dSDimitry Andric 417e580952dSDimitry Andric // Modify the instruction to use the new base register rather 418e580952dSDimitry Andric // than the frame index operand. 41991bc56edSDimitry Andric TRI->resolveFrameIndex(*I, BaseReg, Offset); 420e580952dSDimitry Andric DEBUG(dbgs() << "Resolved: " << *MI); 421e580952dSDimitry Andric 422e580952dSDimitry Andric ++NumReplacements; 423e580952dSDimitry Andric } 424284c1978SDimitry Andric 425e580952dSDimitry Andric return UsedBaseReg; 426e580952dSDimitry Andric } 427