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/Passes.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Target/TargetRegisterInfo.h"
35 #include "llvm/Target/TargetFrameInfo.h"
39 STATISTIC(NumAllocations, "Number of frame indices allocated into local block");
40 STATISTIC(NumBaseRegisters, "Number of virtual frame base registers allocated");
41 STATISTIC(NumReplacements, "Number of frame indices references replaced");
44 class LocalStackSlotPass: public MachineFunctionPass {
45 void calculateFrameObjectOffsets(MachineFunction &Fn);
47 void insertFrameReferenceRegisters(MachineFunction &Fn);
49 static char ID; // Pass identification, replacement for typeid
50 explicit LocalStackSlotPass() : MachineFunctionPass(ID) { }
51 bool runOnMachineFunction(MachineFunction &MF);
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
55 MachineFunctionPass::getAnalysisUsage(AU);
57 const char *getPassName() const {
58 return "Local Stack Slot Allocation";
63 } // end anonymous namespace
65 char LocalStackSlotPass::ID = 0;
67 FunctionPass *llvm::createLocalStackSlotAllocationPass() {
68 return new LocalStackSlotPass();
71 bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) {
72 // Lay out the local blob.
73 calculateFrameObjectOffsets(MF);
75 // Insert virtual base registers to resolve frame index references.
76 insertFrameReferenceRegisters(MF);
80 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
82 AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx, int64_t &Offset,
84 unsigned Align = MFI->getObjectAlignment(FrameIdx);
86 // If the alignment of this object is greater than that of the stack, then
87 // increase the stack alignment to match.
88 MaxAlign = std::max(MaxAlign, Align);
90 // Adjust to alignment boundary.
91 Offset = (Offset + Align - 1) / Align * Align;
93 DEBUG(dbgs() << "Allocate FI(" << FrameIdx << ") to local offset "
95 MFI->mapLocalFrameObject(FrameIdx, Offset);
96 Offset += MFI->getObjectSize(FrameIdx);
101 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
102 /// abstract stack objects.
104 void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
105 // Loop over all of the stack objects, assigning sequential addresses...
106 MachineFrameInfo *MFI = Fn.getFrameInfo();
108 unsigned MaxAlign = 0;
110 // Make sure that the stack protector comes before the local variables on the
112 SmallSet<int, 16> LargeStackObjs;
113 if (MFI->getStackProtectorIndex() >= 0) {
114 AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), Offset, MaxAlign);
116 // Assign large stack objects first.
117 for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
118 if (MFI->isDeadObjectIndex(i))
120 if (MFI->getStackProtectorIndex() == (int)i)
122 if (!MFI->MayNeedStackProtector(i))
125 AdjustStackOffset(MFI, i, Offset, MaxAlign);
126 LargeStackObjs.insert(i);
130 // Then assign frame offsets to stack objects that are not used to spill
131 // callee saved registers.
132 for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
133 if (MFI->isDeadObjectIndex(i))
135 if (MFI->getStackProtectorIndex() == (int)i)
137 if (LargeStackObjs.count(i))
140 AdjustStackOffset(MFI, i, Offset, MaxAlign);
143 // Remember how big this blob of stack space is
144 MFI->setLocalFrameSize(Offset);
145 MFI->setLocalFrameMaxAlign(MaxAlign);
148 void LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) {
149 // Scan the function's instructions looking for frame index references.
150 // For each, ask the target if it wants a virtual base register for it
151 // based on what we can tell it about where the local will end up in the
152 // stack frame. If it wants one, re-use a suitable one we've previously
153 // allocated, or if there isn't one that fits the bill, allocate a new one
154 // and ask the target to create a defining instruction for it.
156 MachineFrameInfo *MFI = Fn.getFrameInfo();
157 const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
159 for (MachineFunction::iterator BB = Fn.begin(),
160 E = Fn.end(); BB != E; ++BB) {
161 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
162 MachineInstr *MI = I;
163 // For now, allocate the base register(s) within the basic block
164 // where they're used, and don't try to keep them around outside
165 // of that. It may be beneficial to try sharing them more broadly
166 // than that, but the increased register pressure makes that a
167 // tricky thing to balance. Investigate if re-materializing these
169 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
170 // Consider replacing all frame index operands that reference
171 // an object allocated in the local block.
172 if (MI->getOperand(i).isFI() &&
173 MFI->isObjectPreAllocated(MI->getOperand(i).getIndex())) {
174 DEBUG(dbgs() << "Considering: " << *MI);
175 if (TRI->needsFrameBaseReg(MI, i)) {
176 DEBUG(dbgs() << " Replacing FI in: " << *MI);
177 // FIXME: Make sure any new base reg is aligned reasonably. TBD
178 // what "reasonably" really means. Conservatively, can just
179 // use the alignment of the local block.
181 // If we have a suitable base register available, use it; otherwise
183 // FIXME: For the moment, just always create a new one.