blob: 8bb802fa167517c65e477abc2f6e7090343fcac9 [file] [log] [blame]
Jim Grosbach3d723672010-08-14 00:15:52 +00001//===- 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 Grosbach3d723672010-08-14 00:15:52 +000022#include "llvm/LLVMContext.h"
23#include "llvm/Module.h"
24#include "llvm/Pass.h"
25#include "llvm/ADT/SmallSet.h"
Jim Grosbach8708ead2010-08-17 18:13:53 +000026#include "llvm/ADT/Statistic.h"
Jim Grosbach3d723672010-08-14 00:15:52 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineFunctionPass.h"
Jim Grosbachdc140c62010-08-17 22:41:55 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Jim Grosbach3d723672010-08-14 00:15:52 +000031#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
38using namespace llvm;
39
Jim Grosbach8708ead2010-08-17 18:13:53 +000040STATISTIC(NumAllocations, "Number of frame indices allocated into local block");
41STATISTIC(NumBaseRegisters, "Number of virtual frame base registers allocated");
42STATISTIC(NumReplacements, "Number of frame indices references replaced");
Jim Grosbach3d723672010-08-14 00:15:52 +000043
44namespace {
45 class LocalStackSlotPass: public MachineFunctionPass {
Jim Grosbach2b1e2022010-08-18 22:44:49 +000046 SmallVector<int64_t,16> LocalOffsets;
Jim Grosbach8708ead2010-08-17 18:13:53 +000047
Jim Grosbach2b1e2022010-08-18 22:44:49 +000048 void AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx, int64_t &Offset,
Jim Grosbach67ff81a2010-08-23 20:40:38 +000049 bool StackGrowsDown, unsigned &MaxAlign);
Jim Grosbach2b1e2022010-08-18 22:44:49 +000050 void calculateFrameObjectOffsets(MachineFunction &Fn);
Jim Grosbach2d16f5b2010-08-20 16:48:30 +000051 bool insertFrameReferenceRegisters(MachineFunction &Fn);
Jim Grosbach3d723672010-08-14 00:15:52 +000052 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
69char LocalStackSlotPass::ID = 0;
70
71FunctionPass *llvm::createLocalStackSlotAllocationPass() {
72 return new LocalStackSlotPass();
73}
74
75bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) {
Jim Grosbach2b1e2022010-08-18 22:44:49 +000076 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbacha2734422010-08-24 19:05:43 +000077 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
Jim Grosbach2b1e2022010-08-18 22:44:49 +000078 unsigned LocalObjectCount = MFI->getObjectIndexEnd();
79
Jim Grosbacha2734422010-08-24 19:05:43 +000080 // If the target doesn't want/need this pass, or if there are no locals
81 // to consider, early exit.
82 if (!TRI->requiresVirtualBaseRegisters(MF) || LocalObjectCount == 0)
Jim Grosbach2b1e2022010-08-18 22:44:49 +000083 return true;
84
85 // Make sure we have enough space to store the local offsets.
86 LocalOffsets.resize(MFI->getObjectIndexEnd());
87
Jim Grosbach8708ead2010-08-17 18:13:53 +000088 // Lay out the local blob.
Jim Grosbach3d723672010-08-14 00:15:52 +000089 calculateFrameObjectOffsets(MF);
Jim Grosbach8708ead2010-08-17 18:13:53 +000090
91 // Insert virtual base registers to resolve frame index references.
Jim Grosbach2d16f5b2010-08-20 16:48:30 +000092 bool UsedBaseRegs = insertFrameReferenceRegisters(MF);
Jim Grosbach2b1e2022010-08-18 22:44:49 +000093
Jim Grosbacha0fc0052010-08-19 02:47:08 +000094 // Tell MFI whether any base registers were allocated. PEI will only
95 // want to use the local block allocations from this pass if there were any.
96 // Otherwise, PEI can do a bit better job of getting the alignment right
97 // without a hole at the start since it knows the alignment of the stack
98 // at the start of local allocation, and this pass doesn't.
Jim Grosbach2d16f5b2010-08-20 16:48:30 +000099 MFI->setUseLocalStackAllocationBlock(UsedBaseRegs);
Jim Grosbacha0fc0052010-08-19 02:47:08 +0000100
Jim Grosbach3d723672010-08-14 00:15:52 +0000101 return true;
102}
103
104/// AdjustStackOffset - Helper function used to adjust the stack frame offset.
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000105void LocalStackSlotPass::AdjustStackOffset(MachineFrameInfo *MFI,
106 int FrameIdx, int64_t &Offset,
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000107 bool StackGrowsDown,
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000108 unsigned &MaxAlign) {
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000109 // If the stack grows down, add the object size to find the lowest address.
110 if (StackGrowsDown)
111 Offset += MFI->getObjectSize(FrameIdx);
112
Jim Grosbach3d723672010-08-14 00:15:52 +0000113 unsigned Align = MFI->getObjectAlignment(FrameIdx);
114
115 // If the alignment of this object is greater than that of the stack, then
116 // increase the stack alignment to match.
117 MaxAlign = std::max(MaxAlign, Align);
118
119 // Adjust to alignment boundary.
120 Offset = (Offset + Align - 1) / Align * Align;
121
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000122 int64_t LocalOffset = StackGrowsDown ? -Offset : Offset;
Jim Grosbach3d723672010-08-14 00:15:52 +0000123 DEBUG(dbgs() << "Allocate FI(" << FrameIdx << ") to local offset "
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000124 << LocalOffset << "\n");
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000125 // Keep the offset available for base register allocation
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000126 LocalOffsets[FrameIdx] = LocalOffset;
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000127 // And tell MFI about it for PEI to use later
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000128 MFI->mapLocalFrameObject(FrameIdx, LocalOffset);
129
130 if (!StackGrowsDown)
131 Offset += MFI->getObjectSize(FrameIdx);
Jim Grosbach3d723672010-08-14 00:15:52 +0000132
133 ++NumAllocations;
134}
135
136/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
137/// abstract stack objects.
138///
139void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
Jim Grosbach3d723672010-08-14 00:15:52 +0000140 // Loop over all of the stack objects, assigning sequential addresses...
141 MachineFrameInfo *MFI = Fn.getFrameInfo();
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000142 const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
143 bool StackGrowsDown =
144 TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
Jim Grosbach3d723672010-08-14 00:15:52 +0000145 int64_t Offset = 0;
Jim Grosbach4861ed62010-08-16 22:30:41 +0000146 unsigned MaxAlign = 0;
Jim Grosbach3d723672010-08-14 00:15:52 +0000147
148 // Make sure that the stack protector comes before the local variables on the
149 // stack.
150 SmallSet<int, 16> LargeStackObjs;
151 if (MFI->getStackProtectorIndex() >= 0) {
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000152 AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), Offset,
153 StackGrowsDown, MaxAlign);
Jim Grosbach3d723672010-08-14 00:15:52 +0000154
155 // Assign large stack objects first.
156 for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
157 if (MFI->isDeadObjectIndex(i))
158 continue;
159 if (MFI->getStackProtectorIndex() == (int)i)
160 continue;
161 if (!MFI->MayNeedStackProtector(i))
162 continue;
163
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000164 AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign);
Jim Grosbach3d723672010-08-14 00:15:52 +0000165 LargeStackObjs.insert(i);
166 }
167 }
168
169 // Then assign frame offsets to stack objects that are not used to spill
170 // callee saved registers.
171 for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
172 if (MFI->isDeadObjectIndex(i))
173 continue;
174 if (MFI->getStackProtectorIndex() == (int)i)
175 continue;
176 if (LargeStackObjs.count(i))
177 continue;
178
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000179 AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign);
Jim Grosbach3d723672010-08-14 00:15:52 +0000180 }
181
Jim Grosbach3d723672010-08-14 00:15:52 +0000182 // Remember how big this blob of stack space is
Jim Grosbach63249342010-08-16 18:06:15 +0000183 MFI->setLocalFrameSize(Offset);
Jim Grosbach4861ed62010-08-16 22:30:41 +0000184 MFI->setLocalFrameMaxAlign(MaxAlign);
Jim Grosbach3d723672010-08-14 00:15:52 +0000185}
Jim Grosbach8708ead2010-08-17 18:13:53 +0000186
Jim Grosbach74d803a2010-08-18 17:57:37 +0000187static inline bool
188lookupCandidateBaseReg(const SmallVector<std::pair<unsigned, int64_t>, 8> &Regs,
189 std::pair<unsigned, int64_t> &RegOffset,
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000190 int64_t FrameSizeAdjust,
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000191 int64_t LocalFrameOffset,
Jim Grosbach74d803a2010-08-18 17:57:37 +0000192 const MachineInstr *MI,
193 const TargetRegisterInfo *TRI) {
194 unsigned e = Regs.size();
195 for (unsigned i = 0; i < e; ++i) {
196 RegOffset = Regs[i];
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000197 // Check if the relative offset from the where the base register references
198 // to the target address is in range for the instruction.
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000199 int64_t Offset = FrameSizeAdjust + LocalFrameOffset - RegOffset.second;
Jim Grosbache2f55692010-08-19 23:52:25 +0000200 if (TRI->isFrameOffsetLegal(MI, Offset))
Jim Grosbach74d803a2010-08-18 17:57:37 +0000201 return true;
202 }
203 return false;
204}
205
Jim Grosbach2d16f5b2010-08-20 16:48:30 +0000206bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) {
Jim Grosbach8708ead2010-08-17 18:13:53 +0000207 // Scan the function's instructions looking for frame index references.
208 // For each, ask the target if it wants a virtual base register for it
209 // based on what we can tell it about where the local will end up in the
210 // stack frame. If it wants one, re-use a suitable one we've previously
211 // allocated, or if there isn't one that fits the bill, allocate a new one
212 // and ask the target to create a defining instruction for it.
Jim Grosbach2d16f5b2010-08-20 16:48:30 +0000213 bool UsedBaseReg = false;
Jim Grosbach8708ead2010-08-17 18:13:53 +0000214
215 MachineFrameInfo *MFI = Fn.getFrameInfo();
216 const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
Jim Grosbach4c207c22010-08-20 20:25:31 +0000217 const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
218 bool StackGrowsDown =
219 TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
Jim Grosbach8708ead2010-08-17 18:13:53 +0000220
221 for (MachineFunction::iterator BB = Fn.begin(),
222 E = Fn.end(); BB != E; ++BB) {
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000223 // A base register definition is a register+offset pair.
224 SmallVector<std::pair<unsigned, int64_t>, 8> BaseRegisters;
225
Jim Grosbach8708ead2010-08-17 18:13:53 +0000226 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
227 MachineInstr *MI = I;
Jim Grosbachdc140c62010-08-17 22:41:55 +0000228 // Debug value instructions can't be out of range, so they don't need
229 // any updates.
230 // FIXME: When we extend this stuff to handle functions with both
231 // VLAs and dynamic realignment, we should update the debug values
232 // to reference the new base pointer when possible.
233 if (MI->isDebugValue())
234 continue;
235
Jim Grosbach8708ead2010-08-17 18:13:53 +0000236 // For now, allocate the base register(s) within the basic block
237 // where they're used, and don't try to keep them around outside
238 // of that. It may be beneficial to try sharing them more broadly
239 // than that, but the increased register pressure makes that a
240 // tricky thing to balance. Investigate if re-materializing these
241 // becomes an issue.
242 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
243 // Consider replacing all frame index operands that reference
244 // an object allocated in the local block.
Jim Grosbachdc140c62010-08-17 22:41:55 +0000245 if (MI->getOperand(i).isFI()) {
246 int FrameIdx = MI->getOperand(i).getIndex();
Jim Grosbache2f55692010-08-19 23:52:25 +0000247
Jim Grosbachdc140c62010-08-17 22:41:55 +0000248 // Don't try this with values not in the local block.
249 if (!MFI->isObjectPreAllocated(FrameIdx))
250 continue;
251
Jim Grosbach8708ead2010-08-17 18:13:53 +0000252 DEBUG(dbgs() << "Considering: " << *MI);
Jim Grosbach31973802010-08-24 21:19:33 +0000253 if (TRI->needsFrameBaseReg(MI, LocalOffsets[FrameIdx])) {
Jim Grosbach74d803a2010-08-18 17:57:37 +0000254 unsigned BaseReg = 0;
Jim Grosbache2f55692010-08-19 23:52:25 +0000255 int64_t Offset = 0;
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000256 int64_t FrameSizeAdjust = StackGrowsDown ? MFI->getLocalFrameSize()
257 : 0;
Jim Grosbach74d803a2010-08-18 17:57:37 +0000258
Jim Grosbach8708ead2010-08-17 18:13:53 +0000259 DEBUG(dbgs() << " Replacing FI in: " << *MI);
Jim Grosbach8708ead2010-08-17 18:13:53 +0000260
Jim Grosbach8ae231a2010-08-17 20:21:30 +0000261 // If we have a suitable base register available, use it; otherwise
Jim Grosbache2f55692010-08-19 23:52:25 +0000262 // create a new one. Note that any offset encoded in the
263 // instruction itself will be taken into account by the target,
264 // so we don't have to adjust for it here when reusing a base
265 // register.
Jim Grosbach74d803a2010-08-18 17:57:37 +0000266 std::pair<unsigned, int64_t> RegOffset;
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000267 if (lookupCandidateBaseReg(BaseRegisters, RegOffset,
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000268 FrameSizeAdjust,
Jim Grosbach4c207c22010-08-20 20:25:31 +0000269 LocalOffsets[FrameIdx],
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000270 MI, TRI)) {
Jim Grosbachcd799ce2010-08-18 23:14:02 +0000271 DEBUG(dbgs() << " Reusing base register " <<
272 RegOffset.first << "\n");
Jim Grosbach74d803a2010-08-18 17:57:37 +0000273 // We found a register to reuse.
274 BaseReg = RegOffset.first;
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000275 Offset = FrameSizeAdjust + LocalOffsets[FrameIdx] -
276 RegOffset.second;
Jim Grosbach74d803a2010-08-18 17:57:37 +0000277 } else {
278 // No previously defined register was in range, so create a
279 // new one.
Jim Grosbache2f55692010-08-19 23:52:25 +0000280 int64_t InstrOffset = TRI->getFrameIndexInstrOffset(MI, i);
Jim Grosbach74d803a2010-08-18 17:57:37 +0000281 const TargetRegisterClass *RC = TRI->getPointerRegClass();
282 BaseReg = Fn.getRegInfo().createVirtualRegister(RC);
Jim Grosbachdc140c62010-08-17 22:41:55 +0000283
Jim Grosbache2f55692010-08-19 23:52:25 +0000284 DEBUG(dbgs() << " Materializing base register " << BaseReg <<
285 " at frame local offset " <<
286 LocalOffsets[FrameIdx] + InstrOffset << "\n");
Jim Grosbach74d803a2010-08-18 17:57:37 +0000287 // Tell the target to insert the instruction to initialize
288 // the base register.
Jim Grosbache2f55692010-08-19 23:52:25 +0000289 TRI->materializeFrameBaseRegister(I, BaseReg, FrameIdx,
290 InstrOffset);
Jim Grosbach74d803a2010-08-18 17:57:37 +0000291
Jim Grosbache2f55692010-08-19 23:52:25 +0000292 // The base register already includes any offset specified
293 // by the instruction, so account for that so it doesn't get
294 // applied twice.
295 Offset = -InstrOffset;
296
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000297 int64_t BaseOffset = FrameSizeAdjust + LocalOffsets[FrameIdx] +
298 InstrOffset;
Jim Grosbache2f55692010-08-19 23:52:25 +0000299 BaseRegisters.push_back(
Jim Grosbach67ff81a2010-08-23 20:40:38 +0000300 std::pair<unsigned, int64_t>(BaseReg, BaseOffset));
Jim Grosbach74d803a2010-08-18 17:57:37 +0000301 ++NumBaseRegisters;
Jim Grosbach2d16f5b2010-08-20 16:48:30 +0000302 UsedBaseReg = true;
Jim Grosbach74d803a2010-08-18 17:57:37 +0000303 }
304 assert(BaseReg != 0 && "Unable to allocate virtual base register!");
Jim Grosbachdc140c62010-08-17 22:41:55 +0000305
306 // Modify the instruction to use the new base register rather
307 // than the frame index operand.
Jim Grosbach74d803a2010-08-18 17:57:37 +0000308 TRI->resolveFrameIndex(I, BaseReg, Offset);
Jim Grosbachab6bdec2010-08-20 19:04:43 +0000309 DEBUG(dbgs() << "Resolved: " << *MI);
Jim Grosbachdc140c62010-08-17 22:41:55 +0000310
Jim Grosbach8708ead2010-08-17 18:13:53 +0000311 ++NumReplacements;
312 }
Jim Grosbach8708ead2010-08-17 18:13:53 +0000313 }
314 }
315 }
316 }
Jim Grosbach2d16f5b2010-08-20 16:48:30 +0000317 return UsedBaseReg;
Jim Grosbach8708ead2010-08-17 18:13:53 +0000318}