121
|
1 //===-- MachineFrameInfo.cpp ---------------------------------------------===//
|
|
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 /// \file Implements MachineFrameInfo that manages the stack frame.
|
|
11 //
|
|
12 //===----------------------------------------------------------------------===//
|
|
13
|
|
14 #include "llvm/CodeGen/MachineFrameInfo.h"
|
|
15
|
|
16 #include "llvm/ADT/BitVector.h"
|
|
17 #include "llvm/CodeGen/MachineFunction.h"
|
|
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
|
134
|
19 #include "llvm/CodeGen/TargetFrameLowering.h"
|
|
20 #include "llvm/CodeGen/TargetInstrInfo.h"
|
|
21 #include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
22 #include "llvm/CodeGen/TargetSubtargetInfo.h"
|
121
|
23 #include "llvm/Support/Debug.h"
|
|
24 #include "llvm/Support/raw_ostream.h"
|
|
25 #include <cassert>
|
|
26
|
|
27 #define DEBUG_TYPE "codegen"
|
|
28
|
|
29 using namespace llvm;
|
|
30
|
|
31 void MachineFrameInfo::ensureMaxAlignment(unsigned Align) {
|
|
32 if (!StackRealignable)
|
|
33 assert(Align <= StackAlignment &&
|
|
34 "For targets without stack realignment, Align is out of limit!");
|
|
35 if (MaxAlignment < Align) MaxAlignment = Align;
|
|
36 }
|
|
37
|
|
38 /// Clamp the alignment if requested and emit a warning.
|
|
39 static inline unsigned clampStackAlignment(bool ShouldClamp, unsigned Align,
|
|
40 unsigned StackAlign) {
|
|
41 if (!ShouldClamp || Align <= StackAlign)
|
|
42 return Align;
|
|
43 DEBUG(dbgs() << "Warning: requested alignment " << Align
|
|
44 << " exceeds the stack alignment " << StackAlign
|
|
45 << " when stack realignment is off" << '\n');
|
|
46 return StackAlign;
|
|
47 }
|
|
48
|
|
49 int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment,
|
134
|
50 bool IsSpillSlot,
|
|
51 const AllocaInst *Alloca,
|
|
52 uint8_t StackID) {
|
121
|
53 assert(Size != 0 && "Cannot allocate zero size stack objects!");
|
|
54 Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
|
134
|
55 Objects.push_back(StackObject(Size, Alignment, 0, false, IsSpillSlot, Alloca,
|
|
56 !IsSpillSlot, StackID));
|
121
|
57 int Index = (int)Objects.size() - NumFixedObjects - 1;
|
|
58 assert(Index >= 0 && "Bad frame index!");
|
|
59 ensureMaxAlignment(Alignment);
|
|
60 return Index;
|
|
61 }
|
|
62
|
|
63 int MachineFrameInfo::CreateSpillStackObject(uint64_t Size,
|
|
64 unsigned Alignment) {
|
|
65 Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
|
|
66 CreateStackObject(Size, Alignment, true);
|
|
67 int Index = (int)Objects.size() - NumFixedObjects - 1;
|
|
68 ensureMaxAlignment(Alignment);
|
|
69 return Index;
|
|
70 }
|
|
71
|
|
72 int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment,
|
|
73 const AllocaInst *Alloca) {
|
|
74 HasVarSizedObjects = true;
|
|
75 Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
|
|
76 Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
|
|
77 ensureMaxAlignment(Alignment);
|
|
78 return (int)Objects.size()-NumFixedObjects-1;
|
|
79 }
|
|
80
|
|
81 int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
|
134
|
82 bool IsImmutable, bool IsAliased) {
|
121
|
83 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
|
|
84 // The alignment of the frame index can be determined from its offset from
|
|
85 // the incoming frame position. If the frame object is at offset 32 and
|
|
86 // the stack is guaranteed to be 16-byte aligned, then we know that the
|
|
87 // object is 16-byte aligned. Note that unlike the non-fixed case, if the
|
|
88 // stack needs realignment, we can't assume that the stack will in fact be
|
|
89 // aligned.
|
134
|
90 unsigned Alignment = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
|
|
91 Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
|
|
92 Objects.insert(Objects.begin(),
|
|
93 StackObject(Size, Alignment, SPOffset, IsImmutable,
|
|
94 /*isSpillSlot=*/false, /*Alloca=*/nullptr,
|
|
95 IsAliased));
|
121
|
96 return -++NumFixedObjects;
|
|
97 }
|
|
98
|
|
99 int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
|
|
100 int64_t SPOffset,
|
134
|
101 bool IsImmutable) {
|
|
102 unsigned Alignment = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
|
|
103 Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
|
|
104 Objects.insert(Objects.begin(),
|
|
105 StackObject(Size, Alignment, SPOffset, IsImmutable,
|
|
106 /*IsSpillSlot=*/true, /*Alloca=*/nullptr,
|
|
107 /*IsAliased=*/false));
|
121
|
108 return -++NumFixedObjects;
|
|
109 }
|
|
110
|
|
111 BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const {
|
|
112 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
|
|
113 BitVector BV(TRI->getNumRegs());
|
|
114
|
|
115 // Before CSI is calculated, no registers are considered pristine. They can be
|
|
116 // freely used and PEI will make sure they are saved.
|
|
117 if (!isCalleeSavedInfoValid())
|
|
118 return BV;
|
|
119
|
|
120 const MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
121 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR;
|
|
122 ++CSR)
|
|
123 BV.set(*CSR);
|
|
124
|
|
125 // Saved CSRs are not pristine.
|
|
126 for (auto &I : getCalleeSavedInfo())
|
|
127 for (MCSubRegIterator S(I.getReg(), TRI, true); S.isValid(); ++S)
|
|
128 BV.reset(*S);
|
|
129
|
|
130 return BV;
|
|
131 }
|
|
132
|
|
133 unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
|
|
134 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
|
|
135 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
|
|
136 unsigned MaxAlign = getMaxAlignment();
|
|
137 int Offset = 0;
|
|
138
|
|
139 // This code is very, very similar to PEI::calculateFrameObjectOffsets().
|
|
140 // It really should be refactored to share code. Until then, changes
|
|
141 // should keep in mind that there's tight coupling between the two.
|
|
142
|
|
143 for (int i = getObjectIndexBegin(); i != 0; ++i) {
|
|
144 int FixedOff = -getObjectOffset(i);
|
|
145 if (FixedOff > Offset) Offset = FixedOff;
|
|
146 }
|
|
147 for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
|
|
148 if (isDeadObjectIndex(i))
|
|
149 continue;
|
|
150 Offset += getObjectSize(i);
|
|
151 unsigned Align = getObjectAlignment(i);
|
|
152 // Adjust to alignment boundary
|
|
153 Offset = (Offset+Align-1)/Align*Align;
|
|
154
|
|
155 MaxAlign = std::max(Align, MaxAlign);
|
|
156 }
|
|
157
|
|
158 if (adjustsStack() && TFI->hasReservedCallFrame(MF))
|
|
159 Offset += getMaxCallFrameSize();
|
|
160
|
|
161 // Round up the size to a multiple of the alignment. If the function has
|
|
162 // any calls or alloca's, align to the target's StackAlignment value to
|
|
163 // ensure that the callee's frame or the alloca data is suitably aligned;
|
|
164 // otherwise, for leaf functions, align to the TransientStackAlignment
|
|
165 // value.
|
|
166 unsigned StackAlign;
|
|
167 if (adjustsStack() || hasVarSizedObjects() ||
|
|
168 (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
|
|
169 StackAlign = TFI->getStackAlignment();
|
|
170 else
|
|
171 StackAlign = TFI->getTransientStackAlignment();
|
|
172
|
|
173 // If the frame pointer is eliminated, all frame offsets will be relative to
|
|
174 // SP not FP. Align to MaxAlign so this works.
|
|
175 StackAlign = std::max(StackAlign, MaxAlign);
|
|
176 unsigned AlignMask = StackAlign - 1;
|
|
177 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
|
|
178
|
|
179 return (unsigned)Offset;
|
|
180 }
|
|
181
|
|
182 void MachineFrameInfo::computeMaxCallFrameSize(const MachineFunction &MF) {
|
|
183 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
|
184 unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
|
|
185 unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
|
|
186 assert(FrameSetupOpcode != ~0u && FrameDestroyOpcode != ~0u &&
|
|
187 "Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");
|
|
188
|
|
189 MaxCallFrameSize = 0;
|
|
190 for (const MachineBasicBlock &MBB : MF) {
|
|
191 for (const MachineInstr &MI : MBB) {
|
|
192 unsigned Opcode = MI.getOpcode();
|
|
193 if (Opcode == FrameSetupOpcode || Opcode == FrameDestroyOpcode) {
|
|
194 unsigned Size = TII.getFrameSize(MI);
|
|
195 MaxCallFrameSize = std::max(MaxCallFrameSize, Size);
|
|
196 AdjustsStack = true;
|
|
197 } else if (MI.isInlineAsm()) {
|
|
198 // Some inline asm's need a stack frame, as indicated by operand 1.
|
|
199 unsigned ExtraInfo = MI.getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
|
|
200 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
|
|
201 AdjustsStack = true;
|
|
202 }
|
|
203 }
|
|
204 }
|
|
205 }
|
|
206
|
|
207 void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
|
|
208 if (Objects.empty()) return;
|
|
209
|
|
210 const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
|
|
211 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
|
|
212
|
|
213 OS << "Frame Objects:\n";
|
|
214
|
|
215 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
|
|
216 const StackObject &SO = Objects[i];
|
|
217 OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
|
|
218
|
|
219 if (SO.StackID != 0)
|
|
220 OS << "id=" << SO.StackID << ' ';
|
|
221
|
|
222 if (SO.Size == ~0ULL) {
|
|
223 OS << "dead\n";
|
|
224 continue;
|
|
225 }
|
|
226 if (SO.Size == 0)
|
|
227 OS << "variable sized";
|
|
228 else
|
|
229 OS << "size=" << SO.Size;
|
|
230 OS << ", align=" << SO.Alignment;
|
|
231
|
|
232 if (i < NumFixedObjects)
|
|
233 OS << ", fixed";
|
|
234 if (i < NumFixedObjects || SO.SPOffset != -1) {
|
|
235 int64_t Off = SO.SPOffset - ValOffset;
|
|
236 OS << ", at location [SP";
|
|
237 if (Off > 0)
|
|
238 OS << "+" << Off;
|
|
239 else if (Off < 0)
|
|
240 OS << Off;
|
|
241 OS << "]";
|
|
242 }
|
|
243 OS << "\n";
|
|
244 }
|
|
245 }
|
|
246
|
|
247 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
|
248 LLVM_DUMP_METHOD void MachineFrameInfo::dump(const MachineFunction &MF) const {
|
|
249 print(MF, dbgs());
|
|
250 }
|
|
251 #endif
|