1 //===- LocalStackSlotAllocation.cpp - Pre-allocate locals to stack slots --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
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"
22 #include "llvm/LLVMContext.h"
23 #include "llvm/Module.h"
24 #include "llvm/Pass.h"
25 #include "llvm/ADT/SmallSet.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineFunctionPass.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
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"
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");
45 class LocalStackSlotPass: public MachineFunctionPass {
46 SmallVector<int64_t,16> LocalOffsets;
48 void AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx, int64_t &Offset,
50 void calculateFrameObjectOffsets(MachineFunction &Fn);
51 void insertFrameReferenceRegisters(MachineFunction &Fn);
53 static char ID; // Pass identification, replacement for typeid
54 explicit LocalStackSlotPass() : MachineFunctionPass(ID) { }
55 bool runOnMachineFunction(MachineFunction &MF);
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 MachineFunctionPass::getAnalysisUsage(AU);
61 const char *getPassName() const {
62 return "Local Stack Slot Allocation";
67 } // end anonymous namespace
69 char LocalStackSlotPass::ID = 0;
71 FunctionPass *llvm::createLocalStackSlotAllocationPass() {
72 return new LocalStackSlotPass();
75 bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) {
76 MachineFrameInfo *MFI = MF.getFrameInfo();
77 unsigned LocalObjectCount = MFI->getObjectIndexEnd();
79 // Early exit if there are no locals to consider
80 if (!LocalObjectCount)
83 // Make sure we have enough space to store the local offsets.
84 LocalOffsets.resize(MFI->getObjectIndexEnd());
86 // Lay out the local blob.
87 calculateFrameObjectOffsets(MF);
89 // Insert virtual base registers to resolve frame index references.
90 insertFrameReferenceRegisters(MF);
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.
97 MFI->setUseLocalStackAllocationBlock(NumBaseRegisters > 0);
102 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
103 void LocalStackSlotPass::AdjustStackOffset(MachineFrameInfo *MFI,
104 int FrameIdx, int64_t &Offset,
105 unsigned &MaxAlign) {
106 unsigned Align = MFI->getObjectAlignment(FrameIdx);
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);
112 // Adjust to alignment boundary.
113 Offset = (Offset + Align - 1) / Align * Align;
115 DEBUG(dbgs() << "Allocate FI(" << FrameIdx << ") to local offset "
117 // Keep the offset available for base register allocation
118 LocalOffsets[FrameIdx] = Offset;
119 // And tell MFI about it for PEI to use later
120 MFI->mapLocalFrameObject(FrameIdx, Offset);
121 Offset += MFI->getObjectSize(FrameIdx);
126 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
127 /// abstract stack objects.
129 void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
130 // Loop over all of the stack objects, assigning sequential addresses...
131 MachineFrameInfo *MFI = Fn.getFrameInfo();
133 unsigned MaxAlign = 0;
135 // Make sure that the stack protector comes before the local variables on the
137 SmallSet<int, 16> LargeStackObjs;
138 if (MFI->getStackProtectorIndex() >= 0) {
139 AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), Offset, MaxAlign);
141 // Assign large stack objects first.
142 for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
143 if (MFI->isDeadObjectIndex(i))
145 if (MFI->getStackProtectorIndex() == (int)i)
147 if (!MFI->MayNeedStackProtector(i))
150 AdjustStackOffset(MFI, i, Offset, MaxAlign);
151 LargeStackObjs.insert(i);
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))
160 if (MFI->getStackProtectorIndex() == (int)i)
162 if (LargeStackObjs.count(i))
165 AdjustStackOffset(MFI, i, Offset, MaxAlign);
168 // Remember how big this blob of stack space is
169 MFI->setLocalFrameSize(Offset);
170 MFI->setLocalFrameMaxAlign(MaxAlign);
174 lookupCandidateBaseReg(const SmallVector<std::pair<unsigned, int64_t>, 8> &Regs,
175 std::pair<unsigned, int64_t> &RegOffset,
176 int64_t LocalFrameOffset,
177 const MachineInstr *MI,
178 const TargetRegisterInfo *TRI) {
179 unsigned e = Regs.size();
180 for (unsigned i = 0; i < e; ++i) {
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;
185 if (TRI->isBaseRegInRange(MI, RegOffset.first, Offset))
191 void LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) {
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.
199 MachineFrameInfo *MFI = Fn.getFrameInfo();
200 const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
202 for (MachineFunction::iterator BB = Fn.begin(),
203 E = Fn.end(); BB != E; ++BB) {
204 // A base register definition is a register+offset pair.
205 SmallVector<std::pair<unsigned, int64_t>, 8> BaseRegisters;
207 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
208 MachineInstr *MI = I;
209 // Debug value instructions can't be out of range, so they don't need
211 // FIXME: When we extend this stuff to handle functions with both
212 // VLAs and dynamic realignment, we should update the debug values
213 // to reference the new base pointer when possible.
214 if (MI->isDebugValue())
217 // For now, allocate the base register(s) within the basic block
218 // where they're used, and don't try to keep them around outside
219 // of that. It may be beneficial to try sharing them more broadly
220 // than that, but the increased register pressure makes that a
221 // tricky thing to balance. Investigate if re-materializing these
223 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
224 // Consider replacing all frame index operands that reference
225 // an object allocated in the local block.
226 if (MI->getOperand(i).isFI()) {
227 int FrameIdx = MI->getOperand(i).getIndex();
228 // Don't try this with values not in the local block.
229 if (!MFI->isObjectPreAllocated(FrameIdx))
232 DEBUG(dbgs() << "Considering: " << *MI);
233 if (TRI->needsFrameBaseReg(MI, i)) {
234 unsigned BaseReg = 0;
237 DEBUG(dbgs() << " Replacing FI in: " << *MI);
239 // If we have a suitable base register available, use it; otherwise
242 std::pair<unsigned, int64_t> RegOffset;
243 if (lookupCandidateBaseReg(BaseRegisters, RegOffset,
244 LocalOffsets[FrameIdx], MI, TRI)) {
245 DEBUG(dbgs() << " Reusing base register " <<
246 RegOffset.first << "\n");
247 // We found a register to reuse.
248 BaseReg = RegOffset.first;
249 Offset = LocalOffsets[FrameIdx] - RegOffset.second;
251 // No previously defined register was in range, so create a
253 const TargetRegisterClass *RC = TRI->getPointerRegClass();
254 BaseReg = Fn.getRegInfo().createVirtualRegister(RC);
256 // Tell the target to insert the instruction to initialize
257 // the base register.
258 TRI->materializeFrameBaseRegister(I, BaseReg, FrameIdx);
260 BaseRegisters.push_back(std::pair<unsigned, int64_t>(BaseReg,
264 assert(BaseReg != 0 && "Unable to allocate virtual base register!");
266 // Modify the instruction to use the new base register rather
267 // than the frame index operand.
268 TRI->resolveFrameIndex(I, BaseReg, Offset);