blob: 604f5145b1a0f77a0b46ae5d55efed982f0b3105 [file] [log] [blame]
Matthias Braune4e14ae2017-04-26 23:36:58 +00001//===-- MachineFrameInfo.cpp ---------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Matthias Braune4e14ae2017-04-26 23:36:58 +00006//
7//===----------------------------------------------------------------------===//
8//
9/// \file Implements MachineFrameInfo that manages the stack frame.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/MachineFrameInfo.h"
14
15#include "llvm/ADT/BitVector.h"
16#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000018#include "llvm/CodeGen/TargetFrameLowering.h"
19#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000020#include "llvm/CodeGen/TargetRegisterInfo.h"
21#include "llvm/CodeGen/TargetSubtargetInfo.h"
Nico Weber432a3882018-04-30 14:59:11 +000022#include "llvm/Config/llvm-config.h"
Matthias Braune4e14ae2017-04-26 23:36:58 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/raw_ostream.h"
Matthias Braune4e14ae2017-04-26 23:36:58 +000025#include <cassert>
26
27#define DEBUG_TYPE "codegen"
28
29using namespace llvm;
30
Guillaume Chatelet18f805a2019-09-27 12:54:21 +000031void MachineFrameInfo::ensureMaxAlignment(Align Alignment) {
Matthias Braune4e14ae2017-04-26 23:36:58 +000032 if (!StackRealignable)
Guillaume Chatelet18f805a2019-09-27 12:54:21 +000033 assert(Alignment <= StackAlignment &&
34 "For targets without stack realignment, Alignment is out of limit!");
35 if (MaxAlignment < Alignment)
36 MaxAlignment = Alignment;
Matthias Braune4e14ae2017-04-26 23:36:58 +000037}
38
39/// Clamp the alignment if requested and emit a warning.
Guillaume Chatelet18f805a2019-09-27 12:54:21 +000040static inline Align clampStackAlignment(bool ShouldClamp, Align Alignment,
41 Align StackAlignment) {
42 if (!ShouldClamp || Alignment <= StackAlignment)
43 return Alignment;
44 LLVM_DEBUG(dbgs() << "Warning: requested alignment " << Alignment.value()
45 << " exceeds the stack alignment " << StackAlignment.value()
Nicola Zaghend34e60c2018-05-14 12:53:11 +000046 << " when stack realignment is off" << '\n');
Guillaume Chatelet18f805a2019-09-27 12:54:21 +000047 return StackAlignment;
Matthias Braune4e14ae2017-04-26 23:36:58 +000048}
49
Guillaume Chatelet18f805a2019-09-27 12:54:21 +000050int MachineFrameInfo::CreateStackObject(uint64_t Size, Align Alignment,
Matthias Braun7afbfd02017-12-05 01:18:15 +000051 bool IsSpillSlot,
52 const AllocaInst *Alloca,
53 uint8_t StackID) {
Matthias Braune4e14ae2017-04-26 23:36:58 +000054 assert(Size != 0 && "Cannot allocate zero size stack objects!");
55 Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
Matthias Braun7afbfd02017-12-05 01:18:15 +000056 Objects.push_back(StackObject(Size, Alignment, 0, false, IsSpillSlot, Alloca,
57 !IsSpillSlot, StackID));
Matthias Braune4e14ae2017-04-26 23:36:58 +000058 int Index = (int)Objects.size() - NumFixedObjects - 1;
59 assert(Index >= 0 && "Bad frame index!");
Sander de Smalen7f23e0a2019-04-02 09:46:52 +000060 if (StackID == 0)
61 ensureMaxAlignment(Alignment);
Matthias Braune4e14ae2017-04-26 23:36:58 +000062 return Index;
63}
64
Guillaume Chatelet18f805a2019-09-27 12:54:21 +000065int MachineFrameInfo::CreateSpillStackObject(uint64_t Size, Align Alignment) {
Matthias Braune4e14ae2017-04-26 23:36:58 +000066 Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
67 CreateStackObject(Size, Alignment, true);
68 int Index = (int)Objects.size() - NumFixedObjects - 1;
69 ensureMaxAlignment(Alignment);
70 return Index;
71}
72
Guillaume Chatelet18f805a2019-09-27 12:54:21 +000073int MachineFrameInfo::CreateVariableSizedObject(Align Alignment,
Matthias Braune4e14ae2017-04-26 23:36:58 +000074 const AllocaInst *Alloca) {
75 HasVarSizedObjects = true;
76 Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
77 Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
78 ensureMaxAlignment(Alignment);
79 return (int)Objects.size()-NumFixedObjects-1;
80}
81
82int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
Matthias Braun7afbfd02017-12-05 01:18:15 +000083 bool IsImmutable, bool IsAliased) {
Matthias Braune4e14ae2017-04-26 23:36:58 +000084 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
85 // The alignment of the frame index can be determined from its offset from
86 // the incoming frame position. If the frame object is at offset 32 and
87 // the stack is guaranteed to be 16-byte aligned, then we know that the
88 // object is 16-byte aligned. Note that unlike the non-fixed case, if the
89 // stack needs realignment, we can't assume that the stack will in fact be
90 // aligned.
Guillaume Chatelet18f805a2019-09-27 12:54:21 +000091 Align Alignment =
92 commonAlignment(ForcedRealign ? Align::None() : StackAlignment, SPOffset);
Matthias Braun7afbfd02017-12-05 01:18:15 +000093 Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
94 Objects.insert(Objects.begin(),
95 StackObject(Size, Alignment, SPOffset, IsImmutable,
Rui Ueyama49a3ad22019-07-16 04:46:31 +000096 /*IsSpillSlot=*/false, /*Alloca=*/nullptr,
Matthias Braun7afbfd02017-12-05 01:18:15 +000097 IsAliased));
Matthias Braune4e14ae2017-04-26 23:36:58 +000098 return -++NumFixedObjects;
99}
100
101int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
102 int64_t SPOffset,
Matthias Braun7afbfd02017-12-05 01:18:15 +0000103 bool IsImmutable) {
Guillaume Chatelet18f805a2019-09-27 12:54:21 +0000104 Align Alignment =
105 commonAlignment(ForcedRealign ? Align::None() : StackAlignment, SPOffset);
Matthias Braun7afbfd02017-12-05 01:18:15 +0000106 Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
107 Objects.insert(Objects.begin(),
108 StackObject(Size, Alignment, SPOffset, IsImmutable,
109 /*IsSpillSlot=*/true, /*Alloca=*/nullptr,
110 /*IsAliased=*/false));
Matthias Braune4e14ae2017-04-26 23:36:58 +0000111 return -++NumFixedObjects;
112}
113
114BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const {
115 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
116 BitVector BV(TRI->getNumRegs());
117
118 // Before CSI is calculated, no registers are considered pristine. They can be
119 // freely used and PEI will make sure they are saved.
120 if (!isCalleeSavedInfoValid())
121 return BV;
122
123 const MachineRegisterInfo &MRI = MF.getRegInfo();
124 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR;
125 ++CSR)
126 BV.set(*CSR);
127
128 // Saved CSRs are not pristine.
129 for (auto &I : getCalleeSavedInfo())
130 for (MCSubRegIterator S(I.getReg(), TRI, true); S.isValid(); ++S)
131 BV.reset(*S);
132
133 return BV;
134}
135
136unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
137 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
138 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
139 unsigned MaxAlign = getMaxAlignment();
140 int Offset = 0;
141
142 // This code is very, very similar to PEI::calculateFrameObjectOffsets().
143 // It really should be refactored to share code. Until then, changes
144 // should keep in mind that there's tight coupling between the two.
145
146 for (int i = getObjectIndexBegin(); i != 0; ++i) {
Sander de Smalen7f23e0a2019-04-02 09:46:52 +0000147 // Only estimate stack size of default stack.
Sander de Smalen5d6ee762019-06-17 09:13:29 +0000148 if (getStackID(i) != TargetStackID::Default)
Sander de Smalen7f23e0a2019-04-02 09:46:52 +0000149 continue;
Matthias Braune4e14ae2017-04-26 23:36:58 +0000150 int FixedOff = -getObjectOffset(i);
151 if (FixedOff > Offset) Offset = FixedOff;
152 }
153 for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
Sander de Smalen7f23e0a2019-04-02 09:46:52 +0000154 // Only estimate stack size of live objects on default stack.
Sander de Smalen5d6ee762019-06-17 09:13:29 +0000155 if (isDeadObjectIndex(i) || getStackID(i) != TargetStackID::Default)
Matthias Braune4e14ae2017-04-26 23:36:58 +0000156 continue;
157 Offset += getObjectSize(i);
158 unsigned Align = getObjectAlignment(i);
159 // Adjust to alignment boundary
160 Offset = (Offset+Align-1)/Align*Align;
161
162 MaxAlign = std::max(Align, MaxAlign);
163 }
164
165 if (adjustsStack() && TFI->hasReservedCallFrame(MF))
166 Offset += getMaxCallFrameSize();
167
168 // Round up the size to a multiple of the alignment. If the function has
169 // any calls or alloca's, align to the target's StackAlignment value to
170 // ensure that the callee's frame or the alloca data is suitably aligned;
171 // otherwise, for leaf functions, align to the TransientStackAlignment
172 // value.
173 unsigned StackAlign;
174 if (adjustsStack() || hasVarSizedObjects() ||
175 (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
176 StackAlign = TFI->getStackAlignment();
177 else
178 StackAlign = TFI->getTransientStackAlignment();
179
180 // If the frame pointer is eliminated, all frame offsets will be relative to
181 // SP not FP. Align to MaxAlign so this works.
182 StackAlign = std::max(StackAlign, MaxAlign);
183 unsigned AlignMask = StackAlign - 1;
184 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
185
186 return (unsigned)Offset;
187}
188
Matthias Braun4682ac62017-05-05 22:04:05 +0000189void MachineFrameInfo::computeMaxCallFrameSize(const MachineFunction &MF) {
190 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
191 unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
192 unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
193 assert(FrameSetupOpcode != ~0u && FrameDestroyOpcode != ~0u &&
194 "Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");
195
196 MaxCallFrameSize = 0;
197 for (const MachineBasicBlock &MBB : MF) {
198 for (const MachineInstr &MI : MBB) {
199 unsigned Opcode = MI.getOpcode();
200 if (Opcode == FrameSetupOpcode || Opcode == FrameDestroyOpcode) {
201 unsigned Size = TII.getFrameSize(MI);
202 MaxCallFrameSize = std::max(MaxCallFrameSize, Size);
203 AdjustsStack = true;
204 } else if (MI.isInlineAsm()) {
205 // Some inline asm's need a stack frame, as indicated by operand 1.
206 unsigned ExtraInfo = MI.getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
207 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
208 AdjustsStack = true;
209 }
210 }
211 }
212}
213
Matthias Braune4e14ae2017-04-26 23:36:58 +0000214void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
215 if (Objects.empty()) return;
216
217 const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
218 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
219
220 OS << "Frame Objects:\n";
221
222 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
223 const StackObject &SO = Objects[i];
224 OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
Matt Arsenaultdb782732017-07-20 21:03:45 +0000225
226 if (SO.StackID != 0)
Matt Arsenault97b6b1b2018-04-09 21:04:30 +0000227 OS << "id=" << static_cast<unsigned>(SO.StackID) << ' ';
Matt Arsenaultdb782732017-07-20 21:03:45 +0000228
Matthias Braune4e14ae2017-04-26 23:36:58 +0000229 if (SO.Size == ~0ULL) {
230 OS << "dead\n";
231 continue;
232 }
233 if (SO.Size == 0)
234 OS << "variable sized";
235 else
236 OS << "size=" << SO.Size;
Guillaume Chatelet1c18a9c2019-08-21 14:29:30 +0000237 OS << ", align=" << SO.Alignment.value();
Matthias Braune4e14ae2017-04-26 23:36:58 +0000238
239 if (i < NumFixedObjects)
240 OS << ", fixed";
241 if (i < NumFixedObjects || SO.SPOffset != -1) {
242 int64_t Off = SO.SPOffset - ValOffset;
243 OS << ", at location [SP";
244 if (Off > 0)
245 OS << "+" << Off;
246 else if (Off < 0)
247 OS << Off;
248 OS << "]";
249 }
250 OS << "\n";
251 }
252}
253
Aaron Ballman615eb472017-10-15 14:32:27 +0000254#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braune4e14ae2017-04-26 23:36:58 +0000255LLVM_DUMP_METHOD void MachineFrameInfo::dump(const MachineFunction &MF) const {
256 print(MF, dbgs());
257}
258#endif