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/ADT/Statistic.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/Module.h"
25 #include "llvm/Pass.h"
26 #include "llvm/ADT/SmallSet.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 processed");
42 class LocalStackSlotPass: public MachineFunctionPass {
43 void calculateFrameObjectOffsets(MachineFunction &Fn);
45 static char ID; // Pass identification, replacement for typeid
46 explicit LocalStackSlotPass() : MachineFunctionPass(ID) { }
47 bool runOnMachineFunction(MachineFunction &MF);
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
51 MachineFunctionPass::getAnalysisUsage(AU);
53 const char *getPassName() const {
54 return "Local Stack Slot Allocation";
59 } // end anonymous namespace
61 char LocalStackSlotPass::ID = 0;
63 FunctionPass *llvm::createLocalStackSlotAllocationPass() {
64 return new LocalStackSlotPass();
67 bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) {
68 calculateFrameObjectOffsets(MF);
72 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
74 AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx, int64_t &Offset,
76 unsigned Align = MFI->getObjectAlignment(FrameIdx);
78 // If the alignment of this object is greater than that of the stack, then
79 // increase the stack alignment to match.
80 MaxAlign = std::max(MaxAlign, Align);
82 // Adjust to alignment boundary.
83 Offset = (Offset + Align - 1) / Align * Align;
85 DEBUG(dbgs() << "Allocate FI(" << FrameIdx << ") to local offset "
87 MFI->mapLocalFrameObject(FrameIdx, Offset);
88 Offset += MFI->getObjectSize(FrameIdx);
93 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
94 /// abstract stack objects.
96 void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
97 // Loop over all of the stack objects, assigning sequential addresses...
98 MachineFrameInfo *MFI = Fn.getFrameInfo();
100 unsigned MaxAlign = 0;
102 // Make sure that the stack protector comes before the local variables on the
104 SmallSet<int, 16> LargeStackObjs;
105 if (MFI->getStackProtectorIndex() >= 0) {
106 AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), Offset, MaxAlign);
108 // Assign large stack objects first.
109 for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
110 if (MFI->isDeadObjectIndex(i))
112 if (MFI->getStackProtectorIndex() == (int)i)
114 if (!MFI->MayNeedStackProtector(i))
117 AdjustStackOffset(MFI, i, Offset, MaxAlign);
118 LargeStackObjs.insert(i);
122 // Then assign frame offsets to stack objects that are not used to spill
123 // callee saved registers.
124 for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
125 if (MFI->isDeadObjectIndex(i))
127 if (MFI->getStackProtectorIndex() == (int)i)
129 if (LargeStackObjs.count(i))
132 AdjustStackOffset(MFI, i, Offset, MaxAlign);
135 // Remember how big this blob of stack space is
136 MFI->setLocalFrameSize(Offset);
137 MFI->setLocalFrameMaxAlign(MaxAlign);