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 |
Bob Wilson | 67b067d | 2011-01-07 04:58:58 +0000 | [diff] [blame] | 12 | // estimates they are likely to be out of range of stack pointer and frame |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 13 | // pointer relative addressing. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #define DEBUG_TYPE "localstackalloc" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame^] | 18 | #include "llvm/CodeGen/Passes.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include "llvm/ADT/SmallSet.h" |
| 21 | #include "llvm/ADT/Statistic.h" |
| 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/MachineFunction.h" |
| 24 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 25 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 26 | #include "llvm/Constants.h" |
| 27 | #include "llvm/DerivedTypes.h" |
| 28 | #include "llvm/Instructions.h" |
| 29 | #include "llvm/Intrinsics.h" |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 30 | #include "llvm/LLVMContext.h" |
| 31 | #include "llvm/Module.h" |
| 32 | #include "llvm/Pass.h" |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
| 34 | #include "llvm/Support/ErrorHandling.h" |
| 35 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetFrameLowering.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame^] | 37 | #include "llvm/Target/TargetRegisterInfo.h" |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace llvm; |
| 40 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 41 | STATISTIC(NumAllocations, "Number of frame indices allocated into local block"); |
| 42 | STATISTIC(NumBaseRegisters, "Number of virtual frame base registers allocated"); |
| 43 | STATISTIC(NumReplacements, "Number of frame indices references replaced"); |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 44 | |
| 45 | namespace { |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 46 | class FrameRef { |
| 47 | MachineBasicBlock::iterator MI; // Instr referencing the frame |
| 48 | int64_t LocalOffset; // Local offset of the frame idx referenced |
| 49 | public: |
| 50 | FrameRef(MachineBasicBlock::iterator I, int64_t Offset) : |
| 51 | MI(I), LocalOffset(Offset) {} |
| 52 | bool operator<(const FrameRef &RHS) const { |
| 53 | return LocalOffset < RHS.LocalOffset; |
| 54 | } |
| 55 | MachineBasicBlock::iterator getMachineInstr() { return MI; } |
| 56 | }; |
| 57 | |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 58 | class LocalStackSlotPass: public MachineFunctionPass { |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 59 | SmallVector<int64_t,16> LocalOffsets; |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 60 | |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 61 | void AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx, int64_t &Offset, |
Jim Grosbach | 67ff81a | 2010-08-23 20:40:38 +0000 | [diff] [blame] | 62 | bool StackGrowsDown, unsigned &MaxAlign); |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 63 | void calculateFrameObjectOffsets(MachineFunction &Fn); |
Jim Grosbach | 2d16f5b | 2010-08-20 16:48:30 +0000 | [diff] [blame] | 64 | bool insertFrameReferenceRegisters(MachineFunction &Fn); |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 65 | public: |
| 66 | static char ID; // Pass identification, replacement for typeid |
| 67 | explicit LocalStackSlotPass() : MachineFunctionPass(ID) { } |
| 68 | bool runOnMachineFunction(MachineFunction &MF); |
| 69 | |
| 70 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 71 | AU.setPreservesCFG(); |
| 72 | MachineFunctionPass::getAnalysisUsage(AU); |
| 73 | } |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 74 | |
| 75 | private: |
| 76 | }; |
| 77 | } // end anonymous namespace |
| 78 | |
| 79 | char LocalStackSlotPass::ID = 0; |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 80 | char &llvm::LocalStackSlotAllocationID = LocalStackSlotPass::ID; |
| 81 | INITIALIZE_PASS(LocalStackSlotPass, "localstackalloc", |
| 82 | "Local Stack Slot Allocation", false, false) |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 83 | |
| 84 | bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) { |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 85 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Jim Grosbach | a273442 | 2010-08-24 19:05:43 +0000 | [diff] [blame] | 86 | const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo(); |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 87 | unsigned LocalObjectCount = MFI->getObjectIndexEnd(); |
| 88 | |
Jim Grosbach | a273442 | 2010-08-24 19:05:43 +0000 | [diff] [blame] | 89 | // If the target doesn't want/need this pass, or if there are no locals |
| 90 | // to consider, early exit. |
| 91 | if (!TRI->requiresVirtualBaseRegisters(MF) || LocalObjectCount == 0) |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 92 | return true; |
| 93 | |
| 94 | // Make sure we have enough space to store the local offsets. |
| 95 | LocalOffsets.resize(MFI->getObjectIndexEnd()); |
| 96 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 97 | // Lay out the local blob. |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 98 | calculateFrameObjectOffsets(MF); |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 99 | |
| 100 | // Insert virtual base registers to resolve frame index references. |
Jim Grosbach | 2d16f5b | 2010-08-20 16:48:30 +0000 | [diff] [blame] | 101 | bool UsedBaseRegs = insertFrameReferenceRegisters(MF); |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 102 | |
Jim Grosbach | a0fc005 | 2010-08-19 02:47:08 +0000 | [diff] [blame] | 103 | // Tell MFI whether any base registers were allocated. PEI will only |
| 104 | // want to use the local block allocations from this pass if there were any. |
| 105 | // Otherwise, PEI can do a bit better job of getting the alignment right |
| 106 | // without a hole at the start since it knows the alignment of the stack |
| 107 | // at the start of local allocation, and this pass doesn't. |
Jim Grosbach | 2d16f5b | 2010-08-20 16:48:30 +0000 | [diff] [blame] | 108 | MFI->setUseLocalStackAllocationBlock(UsedBaseRegs); |
Jim Grosbach | a0fc005 | 2010-08-19 02:47:08 +0000 | [diff] [blame] | 109 | |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | |
| 113 | /// AdjustStackOffset - Helper function used to adjust the stack frame offset. |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 114 | void LocalStackSlotPass::AdjustStackOffset(MachineFrameInfo *MFI, |
| 115 | int FrameIdx, int64_t &Offset, |
Jim Grosbach | 67ff81a | 2010-08-23 20:40:38 +0000 | [diff] [blame] | 116 | bool StackGrowsDown, |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 117 | unsigned &MaxAlign) { |
Jim Grosbach | 67ff81a | 2010-08-23 20:40:38 +0000 | [diff] [blame] | 118 | // If the stack grows down, add the object size to find the lowest address. |
| 119 | if (StackGrowsDown) |
| 120 | Offset += MFI->getObjectSize(FrameIdx); |
| 121 | |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 122 | unsigned Align = MFI->getObjectAlignment(FrameIdx); |
| 123 | |
| 124 | // If the alignment of this object is greater than that of the stack, then |
| 125 | // increase the stack alignment to match. |
| 126 | MaxAlign = std::max(MaxAlign, Align); |
| 127 | |
| 128 | // Adjust to alignment boundary. |
| 129 | Offset = (Offset + Align - 1) / Align * Align; |
| 130 | |
Jim Grosbach | 67ff81a | 2010-08-23 20:40:38 +0000 | [diff] [blame] | 131 | int64_t LocalOffset = StackGrowsDown ? -Offset : Offset; |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 132 | DEBUG(dbgs() << "Allocate FI(" << FrameIdx << ") to local offset " |
Jim Grosbach | 67ff81a | 2010-08-23 20:40:38 +0000 | [diff] [blame] | 133 | << LocalOffset << "\n"); |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 134 | // Keep the offset available for base register allocation |
Jim Grosbach | 67ff81a | 2010-08-23 20:40:38 +0000 | [diff] [blame] | 135 | LocalOffsets[FrameIdx] = LocalOffset; |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 136 | // And tell MFI about it for PEI to use later |
Jim Grosbach | 67ff81a | 2010-08-23 20:40:38 +0000 | [diff] [blame] | 137 | MFI->mapLocalFrameObject(FrameIdx, LocalOffset); |
| 138 | |
| 139 | if (!StackGrowsDown) |
| 140 | Offset += MFI->getObjectSize(FrameIdx); |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 141 | |
| 142 | ++NumAllocations; |
| 143 | } |
| 144 | |
| 145 | /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the |
| 146 | /// abstract stack objects. |
| 147 | /// |
| 148 | void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) { |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 149 | // Loop over all of the stack objects, assigning sequential addresses... |
| 150 | MachineFrameInfo *MFI = Fn.getFrameInfo(); |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 151 | const TargetFrameLowering &TFI = *Fn.getTarget().getFrameLowering(); |
Jim Grosbach | 67ff81a | 2010-08-23 20:40:38 +0000 | [diff] [blame] | 152 | bool StackGrowsDown = |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 153 | TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown; |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 154 | int64_t Offset = 0; |
Jim Grosbach | 4861ed6 | 2010-08-16 22:30:41 +0000 | [diff] [blame] | 155 | unsigned MaxAlign = 0; |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 156 | |
| 157 | // Make sure that the stack protector comes before the local variables on the |
| 158 | // stack. |
| 159 | SmallSet<int, 16> LargeStackObjs; |
| 160 | if (MFI->getStackProtectorIndex() >= 0) { |
Jim Grosbach | 67ff81a | 2010-08-23 20:40:38 +0000 | [diff] [blame] | 161 | AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), Offset, |
| 162 | StackGrowsDown, MaxAlign); |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 163 | |
| 164 | // Assign large stack objects first. |
| 165 | for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { |
| 166 | if (MFI->isDeadObjectIndex(i)) |
| 167 | continue; |
| 168 | if (MFI->getStackProtectorIndex() == (int)i) |
| 169 | continue; |
| 170 | if (!MFI->MayNeedStackProtector(i)) |
| 171 | continue; |
| 172 | |
Jim Grosbach | 67ff81a | 2010-08-23 20:40:38 +0000 | [diff] [blame] | 173 | AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign); |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 174 | LargeStackObjs.insert(i); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Then assign frame offsets to stack objects that are not used to spill |
| 179 | // callee saved registers. |
| 180 | for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { |
| 181 | if (MFI->isDeadObjectIndex(i)) |
| 182 | continue; |
| 183 | if (MFI->getStackProtectorIndex() == (int)i) |
| 184 | continue; |
| 185 | if (LargeStackObjs.count(i)) |
| 186 | continue; |
| 187 | |
Jim Grosbach | 67ff81a | 2010-08-23 20:40:38 +0000 | [diff] [blame] | 188 | AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign); |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 191 | // Remember how big this blob of stack space is |
Jim Grosbach | 6324934 | 2010-08-16 18:06:15 +0000 | [diff] [blame] | 192 | MFI->setLocalFrameSize(Offset); |
Jim Grosbach | 4861ed6 | 2010-08-16 22:30:41 +0000 | [diff] [blame] | 193 | MFI->setLocalFrameMaxAlign(MaxAlign); |
Jim Grosbach | 3d72367 | 2010-08-14 00:15:52 +0000 | [diff] [blame] | 194 | } |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 195 | |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 196 | static inline bool |
| 197 | lookupCandidateBaseReg(const SmallVector<std::pair<unsigned, int64_t>, 8> &Regs, |
| 198 | std::pair<unsigned, int64_t> &RegOffset, |
Jim Grosbach | 67ff81a | 2010-08-23 20:40:38 +0000 | [diff] [blame] | 199 | int64_t FrameSizeAdjust, |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 200 | int64_t LocalFrameOffset, |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 201 | const MachineInstr *MI, |
| 202 | const TargetRegisterInfo *TRI) { |
| 203 | unsigned e = Regs.size(); |
| 204 | for (unsigned i = 0; i < e; ++i) { |
| 205 | RegOffset = Regs[i]; |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 206 | // Check if the relative offset from the where the base register references |
| 207 | // to the target address is in range for the instruction. |
Jim Grosbach | 67ff81a | 2010-08-23 20:40:38 +0000 | [diff] [blame] | 208 | int64_t Offset = FrameSizeAdjust + LocalFrameOffset - RegOffset.second; |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 209 | if (TRI->isFrameOffsetLegal(MI, Offset)) |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | return false; |
| 213 | } |
| 214 | |
Jim Grosbach | 2d16f5b | 2010-08-20 16:48:30 +0000 | [diff] [blame] | 215 | bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) { |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 216 | // Scan the function's instructions looking for frame index references. |
| 217 | // For each, ask the target if it wants a virtual base register for it |
| 218 | // based on what we can tell it about where the local will end up in the |
| 219 | // stack frame. If it wants one, re-use a suitable one we've previously |
| 220 | // allocated, or if there isn't one that fits the bill, allocate a new one |
| 221 | // and ask the target to create a defining instruction for it. |
Jim Grosbach | 2d16f5b | 2010-08-20 16:48:30 +0000 | [diff] [blame] | 222 | bool UsedBaseReg = false; |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 223 | |
| 224 | MachineFrameInfo *MFI = Fn.getFrameInfo(); |
| 225 | const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo(); |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 226 | const TargetFrameLowering &TFI = *Fn.getTarget().getFrameLowering(); |
Jim Grosbach | 4c207c2 | 2010-08-20 20:25:31 +0000 | [diff] [blame] | 227 | bool StackGrowsDown = |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 228 | TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown; |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 229 | |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 230 | // Collect all of the instructions in the block that reference |
| 231 | // a frame index. Also store the frame index referenced to ease later |
| 232 | // lookup. (For any insn that has more than one FI reference, we arbitrarily |
| 233 | // choose the first one). |
| 234 | SmallVector<FrameRef, 64> FrameReferenceInsns; |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 235 | |
Bill Wendling | 976ef86 | 2010-12-17 23:09:14 +0000 | [diff] [blame] | 236 | // A base register definition is a register + offset pair. |
| 237 | SmallVector<std::pair<unsigned, int64_t>, 8> BaseRegisters; |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 238 | |
| 239 | for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 240 | for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { |
| 241 | MachineInstr *MI = I; |
Bill Wendling | 976ef86 | 2010-12-17 23:09:14 +0000 | [diff] [blame] | 242 | |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 243 | // Debug value instructions can't be out of range, so they don't need |
| 244 | // any updates. |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 245 | if (MI->isDebugValue()) |
| 246 | continue; |
Bill Wendling | 976ef86 | 2010-12-17 23:09:14 +0000 | [diff] [blame] | 247 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 248 | // For now, allocate the base register(s) within the basic block |
| 249 | // where they're used, and don't try to keep them around outside |
| 250 | // of that. It may be beneficial to try sharing them more broadly |
| 251 | // than that, but the increased register pressure makes that a |
| 252 | // tricky thing to balance. Investigate if re-materializing these |
| 253 | // becomes an issue. |
| 254 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 255 | // Consider replacing all frame index operands that reference |
| 256 | // an object allocated in the local block. |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 257 | if (MI->getOperand(i).isFI()) { |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 258 | // Don't try this with values not in the local block. |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 259 | if (!MFI->isObjectPreAllocated(MI->getOperand(i).getIndex())) |
| 260 | break; |
| 261 | FrameReferenceInsns. |
| 262 | push_back(FrameRef(MI, LocalOffsets[MI->getOperand(i).getIndex()])); |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
Bill Wendling | 976ef86 | 2010-12-17 23:09:14 +0000 | [diff] [blame] | 268 | |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 269 | // Sort the frame references by local offset |
| 270 | array_pod_sort(FrameReferenceInsns.begin(), FrameReferenceInsns.end()); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 271 | |
Bill Wendling | 976ef86 | 2010-12-17 23:09:14 +0000 | [diff] [blame] | 272 | MachineBasicBlock *Entry = Fn.begin(); |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 273 | |
Bill Wendling | 976ef86 | 2010-12-17 23:09:14 +0000 | [diff] [blame] | 274 | // Loop through the frame references and allocate for them as necessary. |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 275 | for (int ref = 0, e = FrameReferenceInsns.size(); ref < e ; ++ref) { |
| 276 | MachineBasicBlock::iterator I = |
| 277 | FrameReferenceInsns[ref].getMachineInstr(); |
| 278 | MachineInstr *MI = I; |
| 279 | for (unsigned idx = 0, e = MI->getNumOperands(); idx != e; ++idx) { |
| 280 | // Consider replacing all frame index operands that reference |
| 281 | // an object allocated in the local block. |
| 282 | if (MI->getOperand(idx).isFI()) { |
| 283 | int FrameIdx = MI->getOperand(idx).getIndex(); |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 284 | |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 285 | assert(MFI->isObjectPreAllocated(FrameIdx) && |
| 286 | "Only pre-allocated locals expected!"); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 287 | |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 288 | DEBUG(dbgs() << "Considering: " << *MI); |
| 289 | if (TRI->needsFrameBaseReg(MI, LocalOffsets[FrameIdx])) { |
| 290 | unsigned BaseReg = 0; |
| 291 | int64_t Offset = 0; |
| 292 | int64_t FrameSizeAdjust = |
| 293 | StackGrowsDown ? MFI->getLocalFrameSize() : 0; |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 294 | |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 295 | DEBUG(dbgs() << " Replacing FI in: " << *MI); |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 296 | |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 297 | // If we have a suitable base register available, use it; otherwise |
| 298 | // create a new one. Note that any offset encoded in the |
| 299 | // instruction itself will be taken into account by the target, |
| 300 | // so we don't have to adjust for it here when reusing a base |
| 301 | // register. |
| 302 | std::pair<unsigned, int64_t> RegOffset; |
| 303 | if (lookupCandidateBaseReg(BaseRegisters, RegOffset, |
| 304 | FrameSizeAdjust, |
| 305 | LocalOffsets[FrameIdx], |
| 306 | MI, TRI)) { |
| 307 | DEBUG(dbgs() << " Reusing base register " << |
| 308 | RegOffset.first << "\n"); |
| 309 | // We found a register to reuse. |
| 310 | BaseReg = RegOffset.first; |
| 311 | Offset = FrameSizeAdjust + LocalOffsets[FrameIdx] - |
| 312 | RegOffset.second; |
| 313 | } else { |
| 314 | // No previously defined register was in range, so create a |
| 315 | // new one. |
| 316 | int64_t InstrOffset = TRI->getFrameIndexInstrOffset(MI, idx); |
Jakob Stoklund Olesen | 397fc48 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 317 | const MachineFunction *MF = MI->getParent()->getParent(); |
| 318 | const TargetRegisterClass *RC = TRI->getPointerRegClass(*MF); |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 319 | BaseReg = Fn.getRegInfo().createVirtualRegister(RC); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 320 | |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 321 | DEBUG(dbgs() << " Materializing base register " << BaseReg << |
| 322 | " at frame local offset " << |
| 323 | LocalOffsets[FrameIdx] + InstrOffset << "\n"); |
Bill Wendling | 976ef86 | 2010-12-17 23:09:14 +0000 | [diff] [blame] | 324 | |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 325 | // Tell the target to insert the instruction to initialize |
| 326 | // the base register. |
Bill Wendling | 976ef86 | 2010-12-17 23:09:14 +0000 | [diff] [blame] | 327 | // MachineBasicBlock::iterator InsertionPt = Entry->begin(); |
| 328 | TRI->materializeFrameBaseRegister(Entry, BaseReg, FrameIdx, |
| 329 | InstrOffset); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 330 | |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 331 | // The base register already includes any offset specified |
| 332 | // by the instruction, so account for that so it doesn't get |
| 333 | // applied twice. |
| 334 | Offset = -InstrOffset; |
| 335 | |
| 336 | int64_t BaseOffset = FrameSizeAdjust + LocalOffsets[FrameIdx] + |
| 337 | InstrOffset; |
| 338 | BaseRegisters.push_back( |
| 339 | std::pair<unsigned, int64_t>(BaseReg, BaseOffset)); |
| 340 | ++NumBaseRegisters; |
| 341 | UsedBaseReg = true; |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 342 | } |
Jim Grosbach | 864d22e | 2010-08-31 17:58:19 +0000 | [diff] [blame] | 343 | assert(BaseReg != 0 && "Unable to allocate virtual base register!"); |
| 344 | |
| 345 | // Modify the instruction to use the new base register rather |
| 346 | // than the frame index operand. |
| 347 | TRI->resolveFrameIndex(I, BaseReg, Offset); |
| 348 | DEBUG(dbgs() << "Resolved: " << *MI); |
| 349 | |
| 350 | ++NumReplacements; |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
Jim Grosbach | 2d16f5b | 2010-08-20 16:48:30 +0000 | [diff] [blame] | 355 | return UsedBaseReg; |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 356 | } |