Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 1 | //===- LocalStackSlotAllocation.cpp - Pre-allocate locals to stack slots --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass assigns local frame indices to stack slots relative to one another |
| 11 | // and allocates additional base registers to access them when the target |
| 12 | // estimates the are likely to be out of range of stack pointer and frame |
| 13 | // pointer relative addressing. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #define DEBUG_TYPE "localstackalloc" |
| 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Intrinsics.h" |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 22 | #include "llvm/LLVMContext.h" |
| 23 | #include "llvm/Module.h" |
| 24 | #include "llvm/Pass.h" |
| 25 | #include "llvm/ADT/SmallSet.h" |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Statistic.h" |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 28 | #include "llvm/CodeGen/MachineFunction.h" |
| 29 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/Passes.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/Support/ErrorHandling.h" |
| 34 | #include "llvm/Support/raw_ostream.h" |
| 35 | #include "llvm/Target/TargetRegisterInfo.h" |
| 36 | #include "llvm/Target/TargetFrameInfo.h" |
| 37 | |
| 38 | using namespace llvm; |
| 39 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 40 | STATISTIC(NumAllocations, "Number of frame indices allocated into local block"); |
| 41 | STATISTIC(NumBaseRegisters, "Number of virtual frame base registers allocated"); |
| 42 | STATISTIC(NumReplacements, "Number of frame indices references replaced"); |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 43 | |
| 44 | namespace { |
| 45 | class LocalStackSlotPass: public MachineFunctionPass { |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 46 | void calculateFrameObjectOffsets(MachineFunction &Fn); |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 47 | |
| 48 | void insertFrameReferenceRegisters(MachineFunction &Fn); |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 49 | public: |
| 50 | static char ID; // Pass identification, replacement for typeid |
| 51 | explicit LocalStackSlotPass() : MachineFunctionPass(ID) { } |
| 52 | bool runOnMachineFunction(MachineFunction &MF); |
| 53 | |
| 54 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 55 | AU.setPreservesCFG(); |
| 56 | MachineFunctionPass::getAnalysisUsage(AU); |
| 57 | } |
| 58 | const char *getPassName() const { |
| 59 | return "Local Stack Slot Allocation"; |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | }; |
| 64 | } // end anonymous namespace |
| 65 | |
| 66 | char LocalStackSlotPass::ID = 0; |
| 67 | |
| 68 | FunctionPass *llvm::createLocalStackSlotAllocationPass() { |
| 69 | return new LocalStackSlotPass(); |
| 70 | } |
| 71 | |
| 72 | bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) { |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 73 | // Lay out the local blob. |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 74 | calculateFrameObjectOffsets(MF); |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 75 | |
| 76 | // Insert virtual base registers to resolve frame index references. |
| 77 | insertFrameReferenceRegisters(MF); |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 78 | return true; |
| 79 | } |
| 80 | |
| 81 | /// AdjustStackOffset - Helper function used to adjust the stack frame offset. |
| 82 | static inline void |
| 83 | AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx, int64_t &Offset, |
| 84 | unsigned &MaxAlign) { |
| 85 | unsigned Align = MFI->getObjectAlignment(FrameIdx); |
| 86 | |
| 87 | // If the alignment of this object is greater than that of the stack, then |
| 88 | // increase the stack alignment to match. |
| 89 | MaxAlign = std::max(MaxAlign, Align); |
| 90 | |
| 91 | // Adjust to alignment boundary. |
| 92 | Offset = (Offset + Align - 1) / Align * Align; |
| 93 | |
| 94 | DEBUG(dbgs() << "Allocate FI(" << FrameIdx << ") to local offset " |
| 95 | << Offset << "\n"); |
| 96 | MFI->mapLocalFrameObject(FrameIdx, Offset); |
| 97 | Offset += MFI->getObjectSize(FrameIdx); |
| 98 | |
| 99 | ++NumAllocations; |
| 100 | } |
| 101 | |
| 102 | /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the |
| 103 | /// abstract stack objects. |
| 104 | /// |
| 105 | void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) { |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 106 | // Loop over all of the stack objects, assigning sequential addresses... |
| 107 | MachineFrameInfo *MFI = Fn.getFrameInfo(); |
| 108 | int64_t Offset = 0; |
Jim Grosbach | 4861ed6 | 2010-08-16 22:30:41 +0000 | [diff] [blame] | 109 | unsigned MaxAlign = 0; |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 110 | |
| 111 | // Make sure that the stack protector comes before the local variables on the |
| 112 | // stack. |
| 113 | SmallSet<int, 16> LargeStackObjs; |
| 114 | if (MFI->getStackProtectorIndex() >= 0) { |
| 115 | AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), Offset, MaxAlign); |
| 116 | |
| 117 | // Assign large stack objects first. |
| 118 | for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { |
| 119 | if (MFI->isDeadObjectIndex(i)) |
| 120 | continue; |
| 121 | if (MFI->getStackProtectorIndex() == (int)i) |
| 122 | continue; |
| 123 | if (!MFI->MayNeedStackProtector(i)) |
| 124 | continue; |
| 125 | |
| 126 | AdjustStackOffset(MFI, i, Offset, MaxAlign); |
| 127 | LargeStackObjs.insert(i); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Then assign frame offsets to stack objects that are not used to spill |
| 132 | // callee saved registers. |
| 133 | for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { |
| 134 | if (MFI->isDeadObjectIndex(i)) |
| 135 | continue; |
| 136 | if (MFI->getStackProtectorIndex() == (int)i) |
| 137 | continue; |
| 138 | if (LargeStackObjs.count(i)) |
| 139 | continue; |
| 140 | |
| 141 | AdjustStackOffset(MFI, i, Offset, MaxAlign); |
| 142 | } |
| 143 | |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 144 | // Remember how big this blob of stack space is |
Jim Grosbach | 6324934 | 2010-08-16 18:06:15 +0000 | [diff] [blame] | 145 | MFI->setLocalFrameSize(Offset); |
Jim Grosbach | 4861ed6 | 2010-08-16 22:30:41 +0000 | [diff] [blame] | 146 | MFI->setLocalFrameMaxAlign(MaxAlign); |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 147 | } |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 148 | |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame^] | 149 | static inline bool |
| 150 | lookupCandidateBaseReg(const SmallVector<std::pair<unsigned, int64_t>, 8> &Regs, |
| 151 | std::pair<unsigned, int64_t> &RegOffset, |
| 152 | const MachineInstr *MI, |
| 153 | const TargetRegisterInfo *TRI) { |
| 154 | unsigned e = Regs.size(); |
| 155 | for (unsigned i = 0; i < e; ++i) { |
| 156 | RegOffset = Regs[i]; |
| 157 | if (TRI->isBaseRegInRange(MI, RegOffset.first, RegOffset.second)) |
| 158 | return true; |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 163 | void LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) { |
| 164 | // Scan the function's instructions looking for frame index references. |
| 165 | // For each, ask the target if it wants a virtual base register for it |
| 166 | // based on what we can tell it about where the local will end up in the |
| 167 | // stack frame. If it wants one, re-use a suitable one we've previously |
| 168 | // allocated, or if there isn't one that fits the bill, allocate a new one |
| 169 | // and ask the target to create a defining instruction for it. |
| 170 | |
| 171 | MachineFrameInfo *MFI = Fn.getFrameInfo(); |
| 172 | const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo(); |
| 173 | |
| 174 | for (MachineFunction::iterator BB = Fn.begin(), |
| 175 | E = Fn.end(); BB != E; ++BB) { |
| 176 | for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { |
| 177 | MachineInstr *MI = I; |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 178 | // Debug value instructions can't be out of range, so they don't need |
| 179 | // any updates. |
| 180 | // FIXME: When we extend this stuff to handle functions with both |
| 181 | // VLAs and dynamic realignment, we should update the debug values |
| 182 | // to reference the new base pointer when possible. |
| 183 | if (MI->isDebugValue()) |
| 184 | continue; |
| 185 | |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame^] | 186 | // A base register definition is a register+offset pair. |
| 187 | SmallVector<std::pair<unsigned, int64_t>, 8> BaseRegisters; |
| 188 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 189 | // For now, allocate the base register(s) within the basic block |
| 190 | // where they're used, and don't try to keep them around outside |
| 191 | // of that. It may be beneficial to try sharing them more broadly |
| 192 | // than that, but the increased register pressure makes that a |
| 193 | // tricky thing to balance. Investigate if re-materializing these |
| 194 | // becomes an issue. |
| 195 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 196 | // Consider replacing all frame index operands that reference |
| 197 | // an object allocated in the local block. |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 198 | if (MI->getOperand(i).isFI()) { |
| 199 | int FrameIdx = MI->getOperand(i).getIndex(); |
| 200 | // Don't try this with values not in the local block. |
| 201 | if (!MFI->isObjectPreAllocated(FrameIdx)) |
| 202 | continue; |
| 203 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 204 | DEBUG(dbgs() << "Considering: " << *MI); |
| 205 | if (TRI->needsFrameBaseReg(MI, i)) { |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame^] | 206 | unsigned BaseReg = 0; |
| 207 | unsigned Offset = 0; |
| 208 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 209 | DEBUG(dbgs() << " Replacing FI in: " << *MI); |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 210 | |
Jim Grosbach | 8ae231a | 2010-08-17 20:21:30 +0000 | [diff] [blame] | 211 | // If we have a suitable base register available, use it; otherwise |
| 212 | // create a new one. |
Jim Grosbach | 8ae231a | 2010-08-17 20:21:30 +0000 | [diff] [blame] | 213 | |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame^] | 214 | std::pair<unsigned, int64_t> RegOffset; |
| 215 | if (lookupCandidateBaseReg(BaseRegisters, RegOffset, MI, TRI)) { |
| 216 | // We found a register to reuse. |
| 217 | BaseReg = RegOffset.first; |
| 218 | Offset = RegOffset.second; |
| 219 | } else { |
| 220 | // No previously defined register was in range, so create a |
| 221 | // new one. |
| 222 | const TargetRegisterClass *RC = TRI->getPointerRegClass(); |
| 223 | BaseReg = Fn.getRegInfo().createVirtualRegister(RC); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 224 | |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame^] | 225 | // Tell the target to insert the instruction to initialize |
| 226 | // the base register. |
| 227 | TRI->materializeFrameBaseRegister(I, BaseReg, FrameIdx); |
| 228 | |
| 229 | BaseRegisters.push_back(std::pair<unsigned, int64_t>(BaseReg, |
| 230 | Offset)); |
| 231 | ++NumBaseRegisters; |
| 232 | } |
| 233 | assert(BaseReg != 0 && "Unable to allocate virtual base register!"); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 234 | |
| 235 | // Modify the instruction to use the new base register rather |
| 236 | // than the frame index operand. |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame^] | 237 | TRI->resolveFrameIndex(I, BaseReg, Offset); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 238 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 239 | ++NumReplacements; |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |