1 //===-- MachineFrameInfo.cpp ---------------------------------------------===//
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 /// \file Implements MachineFrameInfo that manages the stack frame.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MachineFrameInfo.h"
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/TargetInstrInfo.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
24 #include "llvm/Target/TargetSubtargetInfo.h"
27 #define DEBUG_TYPE "codegen"
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;
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)
43 DEBUG(dbgs() << "Warning: requested alignment " << Align
44 << " exceeds the stack alignment " << StackAlign
45 << " when stack realignment is off" << '\n');
49 int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment,
50 bool isSS, const AllocaInst *Alloca) {
51 assert(Size != 0 && "Cannot allocate zero size stack objects!");
52 Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
53 Objects.push_back(StackObject(Size, Alignment, 0, false, isSS, Alloca,
55 int Index = (int)Objects.size() - NumFixedObjects - 1;
56 assert(Index >= 0 && "Bad frame index!");
57 ensureMaxAlignment(Alignment);
61 int MachineFrameInfo::CreateSpillStackObject(uint64_t Size,
63 Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
64 CreateStackObject(Size, Alignment, true);
65 int Index = (int)Objects.size() - NumFixedObjects - 1;
66 ensureMaxAlignment(Alignment);
70 int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment,
71 const AllocaInst *Alloca) {
72 HasVarSizedObjects = true;
73 Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
74 Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
75 ensureMaxAlignment(Alignment);
76 return (int)Objects.size()-NumFixedObjects-1;
79 int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
80 bool Immutable, bool isAliased) {
81 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
82 // The alignment of the frame index can be determined from its offset from
83 // the incoming frame position. If the frame object is at offset 32 and
84 // the stack is guaranteed to be 16-byte aligned, then we know that the
85 // object is 16-byte aligned. Note that unlike the non-fixed case, if the
86 // stack needs realignment, we can't assume that the stack will in fact be
88 unsigned Align = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
89 Align = clampStackAlignment(!StackRealignable, Align, StackAlignment);
90 Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable,
92 /*Alloca*/ nullptr, isAliased));
93 return -++NumFixedObjects;
96 int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
99 unsigned Align = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
100 Align = clampStackAlignment(!StackRealignable, Align, StackAlignment);
101 Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable,
104 /*isAliased*/ false));
105 return -++NumFixedObjects;
108 BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const {
109 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
110 BitVector BV(TRI->getNumRegs());
112 // Before CSI is calculated, no registers are considered pristine. They can be
113 // freely used and PEI will make sure they are saved.
114 if (!isCalleeSavedInfoValid())
117 const MachineRegisterInfo &MRI = MF.getRegInfo();
118 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR;
122 // Saved CSRs are not pristine.
123 for (auto &I : getCalleeSavedInfo())
124 for (MCSubRegIterator S(I.getReg(), TRI, true); S.isValid(); ++S)
130 unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
131 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
132 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
133 unsigned MaxAlign = getMaxAlignment();
136 // This code is very, very similar to PEI::calculateFrameObjectOffsets().
137 // It really should be refactored to share code. Until then, changes
138 // should keep in mind that there's tight coupling between the two.
140 for (int i = getObjectIndexBegin(); i != 0; ++i) {
141 int FixedOff = -getObjectOffset(i);
142 if (FixedOff > Offset) Offset = FixedOff;
144 for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
145 if (isDeadObjectIndex(i))
147 Offset += getObjectSize(i);
148 unsigned Align = getObjectAlignment(i);
149 // Adjust to alignment boundary
150 Offset = (Offset+Align-1)/Align*Align;
152 MaxAlign = std::max(Align, MaxAlign);
155 if (adjustsStack() && TFI->hasReservedCallFrame(MF))
156 Offset += getMaxCallFrameSize();
158 // Round up the size to a multiple of the alignment. If the function has
159 // any calls or alloca's, align to the target's StackAlignment value to
160 // ensure that the callee's frame or the alloca data is suitably aligned;
161 // otherwise, for leaf functions, align to the TransientStackAlignment
164 if (adjustsStack() || hasVarSizedObjects() ||
165 (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
166 StackAlign = TFI->getStackAlignment();
168 StackAlign = TFI->getTransientStackAlignment();
170 // If the frame pointer is eliminated, all frame offsets will be relative to
171 // SP not FP. Align to MaxAlign so this works.
172 StackAlign = std::max(StackAlign, MaxAlign);
173 unsigned AlignMask = StackAlign - 1;
174 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
176 return (unsigned)Offset;
179 void MachineFrameInfo::computeMaxCallFrameSize(const MachineFunction &MF) {
180 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
181 unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
182 unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
183 assert(FrameSetupOpcode != ~0u && FrameDestroyOpcode != ~0u &&
184 "Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");
186 MaxCallFrameSize = 0;
187 for (const MachineBasicBlock &MBB : MF) {
188 for (const MachineInstr &MI : MBB) {
189 unsigned Opcode = MI.getOpcode();
190 if (Opcode == FrameSetupOpcode || Opcode == FrameDestroyOpcode) {
191 unsigned Size = TII.getFrameSize(MI);
192 MaxCallFrameSize = std::max(MaxCallFrameSize, Size);
194 } else if (MI.isInlineAsm()) {
195 // Some inline asm's need a stack frame, as indicated by operand 1.
196 unsigned ExtraInfo = MI.getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
197 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
204 void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
205 if (Objects.empty()) return;
207 const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
208 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
210 OS << "Frame Objects:\n";
212 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
213 const StackObject &SO = Objects[i];
214 OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
215 if (SO.Size == ~0ULL) {
220 OS << "variable sized";
222 OS << "size=" << SO.Size;
223 OS << ", align=" << SO.Alignment;
225 if (i < NumFixedObjects)
227 if (i < NumFixedObjects || SO.SPOffset != -1) {
228 int64_t Off = SO.SPOffset - ValOffset;
229 OS << ", at location [SP";
240 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
241 LLVM_DUMP_METHOD void MachineFrameInfo::dump(const MachineFunction &MF) const {