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 | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 46 | SmallVector<int64_t,16> LocalOffsets; |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 47 | |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 48 | void AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx, int64_t &Offset, |
| 49 | unsigned &MaxAlign); |
| 50 | void calculateFrameObjectOffsets(MachineFunction &Fn); |
Jim Grosbach | 2d16f5b | 2010-08-20 16:48:30 +0000 | [diff] [blame] | 51 | bool insertFrameReferenceRegisters(MachineFunction &Fn); |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 52 | public: |
| 53 | static char ID; // Pass identification, replacement for typeid |
| 54 | explicit LocalStackSlotPass() : MachineFunctionPass(ID) { } |
| 55 | bool runOnMachineFunction(MachineFunction &MF); |
| 56 | |
| 57 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 58 | AU.setPreservesCFG(); |
| 59 | MachineFunctionPass::getAnalysisUsage(AU); |
| 60 | } |
| 61 | const char *getPassName() const { |
| 62 | return "Local Stack Slot Allocation"; |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | }; |
| 67 | } // end anonymous namespace |
| 68 | |
| 69 | char LocalStackSlotPass::ID = 0; |
| 70 | |
| 71 | FunctionPass *llvm::createLocalStackSlotAllocationPass() { |
| 72 | return new LocalStackSlotPass(); |
| 73 | } |
| 74 | |
| 75 | bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) { |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 76 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 77 | unsigned LocalObjectCount = MFI->getObjectIndexEnd(); |
| 78 | |
| 79 | // Early exit if there are no locals to consider |
| 80 | if (!LocalObjectCount) |
| 81 | return true; |
| 82 | |
| 83 | // Make sure we have enough space to store the local offsets. |
| 84 | LocalOffsets.resize(MFI->getObjectIndexEnd()); |
| 85 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 86 | // Lay out the local blob. |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 87 | calculateFrameObjectOffsets(MF); |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 88 | |
| 89 | // Insert virtual base registers to resolve frame index references. |
Jim Grosbach | 2d16f5b | 2010-08-20 16:48:30 +0000 | [diff] [blame] | 90 | bool UsedBaseRegs = insertFrameReferenceRegisters(MF); |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 91 | |
Jim Grosbach | a0fc005 | 2010-08-19 02:47:08 +0000 | [diff] [blame] | 92 | // Tell MFI whether any base registers were allocated. PEI will only |
| 93 | // want to use the local block allocations from this pass if there were any. |
| 94 | // Otherwise, PEI can do a bit better job of getting the alignment right |
| 95 | // without a hole at the start since it knows the alignment of the stack |
| 96 | // at the start of local allocation, and this pass doesn't. |
Jim Grosbach | 2d16f5b | 2010-08-20 16:48:30 +0000 | [diff] [blame] | 97 | MFI->setUseLocalStackAllocationBlock(UsedBaseRegs); |
Jim Grosbach | a0fc005 | 2010-08-19 02:47:08 +0000 | [diff] [blame] | 98 | |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
| 102 | /// AdjustStackOffset - Helper function used to adjust the stack frame offset. |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 103 | void LocalStackSlotPass::AdjustStackOffset(MachineFrameInfo *MFI, |
| 104 | int FrameIdx, int64_t &Offset, |
| 105 | unsigned &MaxAlign) { |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 106 | unsigned Align = MFI->getObjectAlignment(FrameIdx); |
| 107 | |
| 108 | // If the alignment of this object is greater than that of the stack, then |
| 109 | // increase the stack alignment to match. |
| 110 | MaxAlign = std::max(MaxAlign, Align); |
| 111 | |
| 112 | // Adjust to alignment boundary. |
| 113 | Offset = (Offset + Align - 1) / Align * Align; |
| 114 | |
| 115 | DEBUG(dbgs() << "Allocate FI(" << FrameIdx << ") to local offset " |
| 116 | << Offset << "\n"); |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 117 | // Keep the offset available for base register allocation |
| 118 | LocalOffsets[FrameIdx] = Offset; |
| 119 | // And tell MFI about it for PEI to use later |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 120 | MFI->mapLocalFrameObject(FrameIdx, Offset); |
| 121 | Offset += MFI->getObjectSize(FrameIdx); |
| 122 | |
| 123 | ++NumAllocations; |
| 124 | } |
| 125 | |
| 126 | /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the |
| 127 | /// abstract stack objects. |
| 128 | /// |
| 129 | void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) { |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 130 | // Loop over all of the stack objects, assigning sequential addresses... |
| 131 | MachineFrameInfo *MFI = Fn.getFrameInfo(); |
| 132 | int64_t Offset = 0; |
Jim Grosbach | 4861ed6 | 2010-08-16 22:30:41 +0000 | [diff] [blame] | 133 | unsigned MaxAlign = 0; |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 134 | |
| 135 | // Make sure that the stack protector comes before the local variables on the |
| 136 | // stack. |
| 137 | SmallSet<int, 16> LargeStackObjs; |
| 138 | if (MFI->getStackProtectorIndex() >= 0) { |
| 139 | AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), Offset, MaxAlign); |
| 140 | |
| 141 | // Assign large stack objects first. |
| 142 | for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { |
| 143 | if (MFI->isDeadObjectIndex(i)) |
| 144 | continue; |
| 145 | if (MFI->getStackProtectorIndex() == (int)i) |
| 146 | continue; |
| 147 | if (!MFI->MayNeedStackProtector(i)) |
| 148 | continue; |
| 149 | |
| 150 | AdjustStackOffset(MFI, i, Offset, MaxAlign); |
| 151 | LargeStackObjs.insert(i); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // Then assign frame offsets to stack objects that are not used to spill |
| 156 | // callee saved registers. |
| 157 | for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { |
| 158 | if (MFI->isDeadObjectIndex(i)) |
| 159 | continue; |
| 160 | if (MFI->getStackProtectorIndex() == (int)i) |
| 161 | continue; |
| 162 | if (LargeStackObjs.count(i)) |
| 163 | continue; |
| 164 | |
| 165 | AdjustStackOffset(MFI, i, Offset, MaxAlign); |
| 166 | } |
| 167 | |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 168 | // Remember how big this blob of stack space is |
Jim Grosbach | 6324934 | 2010-08-16 18:06:15 +0000 | [diff] [blame] | 169 | MFI->setLocalFrameSize(Offset); |
Jim Grosbach | 4861ed6 | 2010-08-16 22:30:41 +0000 | [diff] [blame] | 170 | MFI->setLocalFrameMaxAlign(MaxAlign); |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 171 | } |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 172 | |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 173 | static inline bool |
| 174 | lookupCandidateBaseReg(const SmallVector<std::pair<unsigned, int64_t>, 8> &Regs, |
| 175 | std::pair<unsigned, int64_t> &RegOffset, |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 176 | int64_t LocalFrameOffset, |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 177 | const MachineInstr *MI, |
| 178 | const TargetRegisterInfo *TRI) { |
| 179 | unsigned e = Regs.size(); |
| 180 | for (unsigned i = 0; i < e; ++i) { |
| 181 | RegOffset = Regs[i]; |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 182 | // Check if the relative offset from the where the base register references |
| 183 | // to the target address is in range for the instruction. |
| 184 | int64_t Offset = LocalFrameOffset - RegOffset.second; |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 185 | if (TRI->isFrameOffsetLegal(MI, Offset)) |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 186 | return true; |
| 187 | } |
| 188 | return false; |
| 189 | } |
| 190 | |
Jim Grosbach | 2d16f5b | 2010-08-20 16:48:30 +0000 | [diff] [blame] | 191 | bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) { |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 192 | // Scan the function's instructions looking for frame index references. |
| 193 | // For each, ask the target if it wants a virtual base register for it |
| 194 | // based on what we can tell it about where the local will end up in the |
| 195 | // stack frame. If it wants one, re-use a suitable one we've previously |
| 196 | // allocated, or if there isn't one that fits the bill, allocate a new one |
| 197 | // and ask the target to create a defining instruction for it. |
Jim Grosbach | 2d16f5b | 2010-08-20 16:48:30 +0000 | [diff] [blame] | 198 | bool UsedBaseReg = false; |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 199 | |
| 200 | MachineFrameInfo *MFI = Fn.getFrameInfo(); |
| 201 | const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo(); |
| 202 | |
| 203 | for (MachineFunction::iterator BB = Fn.begin(), |
| 204 | E = Fn.end(); BB != E; ++BB) { |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 205 | // A base register definition is a register+offset pair. |
| 206 | SmallVector<std::pair<unsigned, int64_t>, 8> BaseRegisters; |
| 207 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 208 | for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { |
| 209 | MachineInstr *MI = I; |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 210 | // Debug value instructions can't be out of range, so they don't need |
| 211 | // any updates. |
| 212 | // FIXME: When we extend this stuff to handle functions with both |
| 213 | // VLAs and dynamic realignment, we should update the debug values |
| 214 | // to reference the new base pointer when possible. |
| 215 | if (MI->isDebugValue()) |
| 216 | continue; |
| 217 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 218 | // For now, allocate the base register(s) within the basic block |
| 219 | // where they're used, and don't try to keep them around outside |
| 220 | // of that. It may be beneficial to try sharing them more broadly |
| 221 | // than that, but the increased register pressure makes that a |
| 222 | // tricky thing to balance. Investigate if re-materializing these |
| 223 | // becomes an issue. |
| 224 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 225 | // Consider replacing all frame index operands that reference |
| 226 | // an object allocated in the local block. |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 227 | if (MI->getOperand(i).isFI()) { |
| 228 | int FrameIdx = MI->getOperand(i).getIndex(); |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 229 | |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 230 | // Don't try this with values not in the local block. |
| 231 | if (!MFI->isObjectPreAllocated(FrameIdx)) |
| 232 | continue; |
| 233 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 234 | DEBUG(dbgs() << "Considering: " << *MI); |
| 235 | if (TRI->needsFrameBaseReg(MI, i)) { |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 236 | unsigned BaseReg = 0; |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 237 | int64_t Offset = 0; |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 238 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 239 | DEBUG(dbgs() << " Replacing FI in: " << *MI); |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 240 | |
Jim Grosbach | 8ae231a | 2010-08-17 20:21:30 +0000 | [diff] [blame] | 241 | // If we have a suitable base register available, use it; otherwise |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 242 | // create a new one. Note that any offset encoded in the |
| 243 | // instruction itself will be taken into account by the target, |
| 244 | // so we don't have to adjust for it here when reusing a base |
| 245 | // register. |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 246 | std::pair<unsigned, int64_t> RegOffset; |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 247 | if (lookupCandidateBaseReg(BaseRegisters, RegOffset, |
| 248 | LocalOffsets[FrameIdx], MI, TRI)) { |
Jim Grosbach | cd799ce | 2010-08-18 23:14:02 +0000 | [diff] [blame] | 249 | DEBUG(dbgs() << " Reusing base register " << |
| 250 | RegOffset.first << "\n"); |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 251 | // We found a register to reuse. |
| 252 | BaseReg = RegOffset.first; |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 253 | Offset = LocalOffsets[FrameIdx] - RegOffset.second; |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 254 | } else { |
| 255 | // No previously defined register was in range, so create a |
| 256 | // new one. |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 257 | int64_t InstrOffset = TRI->getFrameIndexInstrOffset(MI, i); |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 258 | const TargetRegisterClass *RC = TRI->getPointerRegClass(); |
| 259 | BaseReg = Fn.getRegInfo().createVirtualRegister(RC); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 260 | |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 261 | DEBUG(dbgs() << " Materializing base register " << BaseReg << |
| 262 | " at frame local offset " << |
| 263 | LocalOffsets[FrameIdx] + InstrOffset << "\n"); |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 264 | // Tell the target to insert the instruction to initialize |
| 265 | // the base register. |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 266 | TRI->materializeFrameBaseRegister(I, BaseReg, FrameIdx, |
| 267 | InstrOffset); |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 268 | |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 269 | // The base register already includes any offset specified |
| 270 | // by the instruction, so account for that so it doesn't get |
| 271 | // applied twice. |
| 272 | Offset = -InstrOffset; |
| 273 | |
| 274 | BaseRegisters.push_back( |
| 275 | std::pair<unsigned, int64_t>(BaseReg, |
| 276 | LocalOffsets[FrameIdx] + InstrOffset)); |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 277 | ++NumBaseRegisters; |
Jim Grosbach | 2d16f5b | 2010-08-20 16:48:30 +0000 | [diff] [blame] | 278 | UsedBaseReg = true; |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 279 | } |
| 280 | assert(BaseReg != 0 && "Unable to allocate virtual base register!"); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 281 | |
| 282 | // Modify the instruction to use the new base register rather |
| 283 | // than the frame index operand. |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 284 | TRI->resolveFrameIndex(I, BaseReg, Offset); |
Jim Grosbach | ab6bdec | 2010-08-20 19:04:43 +0000 | [diff] [blame^] | 285 | DEBUG(dbgs() << "Resolved: " << *MI); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 286 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 287 | ++NumReplacements; |
| 288 | } |
| 289 | |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
Jim Grosbach | 2d16f5b | 2010-08-20 16:48:30 +0000 | [diff] [blame] | 294 | return UsedBaseReg; |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 295 | } |