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