blob: e95fc7f8cdf4a3acc25949c2ca179c174f78160a [file] [log] [blame]
Jim Grosbacha030fa52010-08-14 00:15:52 +00001//===- LocalStackSlotAllocation.cpp - Pre-allocate locals to stack slots --===//
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// 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
Bob Wilsond23b3d22011-01-07 04:58:58 +000012// estimates they are likely to be out of range of stack pointer and frame
Jim Grosbacha030fa52010-08-14 00:15:52 +000013// pointer relative addressing.
14//
15//===----------------------------------------------------------------------===//
16
Josh Magee22b8ba22013-12-19 03:17:11 +000017#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/SmallSet.h"
Eugene Zelenko149178d2017-10-10 22:33:29 +000019#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/Statistic.h"
Eugene Zelenko149178d2017-10-10 22:33:29 +000021#include "llvm/CodeGen/MachineBasicBlock.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko149178d2017-10-10 22:33:29 +000025#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Josh Magee22b8ba22013-12-19 03:17:11 +000028#include "llvm/CodeGen/StackProtector.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000029#include "llvm/CodeGen/TargetFrameLowering.h"
30#include "llvm/CodeGen/TargetOpcodes.h"
31#include "llvm/CodeGen/TargetRegisterInfo.h"
32#include "llvm/CodeGen/TargetSubtargetInfo.h"
Jim Grosbacha030fa52010-08-14 00:15:52 +000033#include "llvm/Pass.h"
Jim Grosbacha030fa52010-08-14 00:15:52 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/raw_ostream.h"
Eugene Zelenko149178d2017-10-10 22:33:29 +000037#include <algorithm>
38#include <cassert>
39#include <cstdint>
40#include <tuple>
Jim Grosbacha030fa52010-08-14 00:15:52 +000041
42using namespace llvm;
43
Chandler Carruth1b9dde02014-04-22 02:02:50 +000044#define DEBUG_TYPE "localstackalloc"
45
Jim Grosbachc252ee22010-08-17 18:13:53 +000046STATISTIC(NumAllocations, "Number of frame indices allocated into local block");
47STATISTIC(NumBaseRegisters, "Number of virtual frame base registers allocated");
48STATISTIC(NumReplacements, "Number of frame indices references replaced");
Jim Grosbacha030fa52010-08-14 00:15:52 +000049
50namespace {
Eugene Zelenko149178d2017-10-10 22:33:29 +000051
Jim Grosbach365e9312010-08-31 17:58:19 +000052 class FrameRef {
53 MachineBasicBlock::iterator MI; // Instr referencing the frame
54 int64_t LocalOffset; // Local offset of the frame idx referenced
Hal Finkel71532512013-04-30 20:04:37 +000055 int FrameIdx; // The frame index
Matt Arsenault8fac5012016-10-26 14:53:50 +000056
57 // Order reference instruction appears in program. Used to ensure
58 // deterministic order when multiple instructions may reference the same
59 // location.
60 unsigned Order;
61
Jim Grosbach365e9312010-08-31 17:58:19 +000062 public:
Matt Arsenault8fac5012016-10-26 14:53:50 +000063 FrameRef(MachineInstr *I, int64_t Offset, int Idx, unsigned Ord) :
64 MI(I), LocalOffset(Offset), FrameIdx(Idx), Order(Ord) {}
65
Jim Grosbach365e9312010-08-31 17:58:19 +000066 bool operator<(const FrameRef &RHS) const {
Matt Arsenault8fac5012016-10-26 14:53:50 +000067 return std::tie(LocalOffset, FrameIdx, Order) <
68 std::tie(RHS.LocalOffset, RHS.FrameIdx, RHS.Order);
Jim Grosbach365e9312010-08-31 17:58:19 +000069 }
Matt Arsenault8fac5012016-10-26 14:53:50 +000070
Hal Finkel71532512013-04-30 20:04:37 +000071 MachineBasicBlock::iterator getMachineInstr() const { return MI; }
72 int64_t getLocalOffset() const { return LocalOffset; }
73 int getFrameIndex() const { return FrameIdx; }
Jim Grosbach365e9312010-08-31 17:58:19 +000074 };
75
Jim Grosbacha030fa52010-08-14 00:15:52 +000076 class LocalStackSlotPass: public MachineFunctionPass {
Eugene Zelenko149178d2017-10-10 22:33:29 +000077 SmallVector<int64_t, 16> LocalOffsets;
78
Josh Magee22b8ba22013-12-19 03:17:11 +000079 /// StackObjSet - A set of stack object indexes
Eugene Zelenko149178d2017-10-10 22:33:29 +000080 using StackObjSet = SmallSetVector<int, 8>;
Jim Grosbachc252ee22010-08-17 18:13:53 +000081
Matthias Braun941a7052016-07-28 18:40:00 +000082 void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx, int64_t &Offset,
Jim Grosbach754f8e62010-08-23 20:40:38 +000083 bool StackGrowsDown, unsigned &MaxAlign);
Josh Magee22b8ba22013-12-19 03:17:11 +000084 void AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
85 SmallSet<int, 16> &ProtectedObjs,
Matthias Braun941a7052016-07-28 18:40:00 +000086 MachineFrameInfo &MFI, bool StackGrowsDown,
Josh Magee22b8ba22013-12-19 03:17:11 +000087 int64_t &Offset, unsigned &MaxAlign);
Jim Grosbachdbfc2ce2010-08-18 22:44:49 +000088 void calculateFrameObjectOffsets(MachineFunction &Fn);
Jim Grosbach06006912010-08-20 16:48:30 +000089 bool insertFrameReferenceRegisters(MachineFunction &Fn);
Eugene Zelenko149178d2017-10-10 22:33:29 +000090
Jim Grosbacha030fa52010-08-14 00:15:52 +000091 public:
92 static char ID; // Pass identification, replacement for typeid
Eugene Zelenko149178d2017-10-10 22:33:29 +000093
Matt Arsenault8fac5012016-10-26 14:53:50 +000094 explicit LocalStackSlotPass() : MachineFunctionPass(ID) {
Josh Magee22b8ba22013-12-19 03:17:11 +000095 initializeLocalStackSlotPassPass(*PassRegistry::getPassRegistry());
96 }
Eugene Zelenko149178d2017-10-10 22:33:29 +000097
Craig Topper4584cd52014-03-07 09:26:03 +000098 bool runOnMachineFunction(MachineFunction &MF) override;
Jim Grosbacha030fa52010-08-14 00:15:52 +000099
Craig Topper4584cd52014-03-07 09:26:03 +0000100 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jim Grosbacha030fa52010-08-14 00:15:52 +0000101 AU.setPreservesCFG();
Josh Magee22b8ba22013-12-19 03:17:11 +0000102 AU.addRequired<StackProtector>();
Jim Grosbacha030fa52010-08-14 00:15:52 +0000103 MachineFunctionPass::getAnalysisUsage(AU);
104 }
Jim Grosbacha030fa52010-08-14 00:15:52 +0000105 };
Eugene Zelenko149178d2017-10-10 22:33:29 +0000106
Jim Grosbacha030fa52010-08-14 00:15:52 +0000107} // end anonymous namespace
108
109char LocalStackSlotPass::ID = 0;
Eugene Zelenko149178d2017-10-10 22:33:29 +0000110
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000111char &llvm::LocalStackSlotAllocationID = LocalStackSlotPass::ID;
Eugene Zelenko149178d2017-10-10 22:33:29 +0000112
Matthias Braun1527baa2017-05-25 21:26:32 +0000113INITIALIZE_PASS_BEGIN(LocalStackSlotPass, DEBUG_TYPE,
Josh Magee22b8ba22013-12-19 03:17:11 +0000114 "Local Stack Slot Allocation", false, false)
115INITIALIZE_PASS_DEPENDENCY(StackProtector)
Matthias Braun1527baa2017-05-25 21:26:32 +0000116INITIALIZE_PASS_END(LocalStackSlotPass, DEBUG_TYPE,
Josh Magee22b8ba22013-12-19 03:17:11 +0000117 "Local Stack Slot Allocation", false, false)
118
Jim Grosbacha030fa52010-08-14 00:15:52 +0000119bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) {
Matthias Braun941a7052016-07-28 18:40:00 +0000120 MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000121 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000122 unsigned LocalObjectCount = MFI.getObjectIndexEnd();
Jim Grosbachdbfc2ce2010-08-18 22:44:49 +0000123
Jim Grosbachb77d67f2010-08-24 19:05:43 +0000124 // If the target doesn't want/need this pass, or if there are no locals
125 // to consider, early exit.
126 if (!TRI->requiresVirtualBaseRegisters(MF) || LocalObjectCount == 0)
Jim Grosbachdbfc2ce2010-08-18 22:44:49 +0000127 return true;
128
129 // Make sure we have enough space to store the local offsets.
Matthias Braun941a7052016-07-28 18:40:00 +0000130 LocalOffsets.resize(MFI.getObjectIndexEnd());
Jim Grosbachdbfc2ce2010-08-18 22:44:49 +0000131
Jim Grosbachc252ee22010-08-17 18:13:53 +0000132 // Lay out the local blob.
Jim Grosbacha030fa52010-08-14 00:15:52 +0000133 calculateFrameObjectOffsets(MF);
Jim Grosbachc252ee22010-08-17 18:13:53 +0000134
135 // Insert virtual base registers to resolve frame index references.
Jim Grosbach06006912010-08-20 16:48:30 +0000136 bool UsedBaseRegs = insertFrameReferenceRegisters(MF);
Jim Grosbachdbfc2ce2010-08-18 22:44:49 +0000137
Jim Grosbach743d7c82010-08-19 02:47:08 +0000138 // Tell MFI whether any base registers were allocated. PEI will only
139 // want to use the local block allocations from this pass if there were any.
140 // Otherwise, PEI can do a bit better job of getting the alignment right
141 // without a hole at the start since it knows the alignment of the stack
142 // at the start of local allocation, and this pass doesn't.
Matthias Braun941a7052016-07-28 18:40:00 +0000143 MFI.setUseLocalStackAllocationBlock(UsedBaseRegs);
Jim Grosbach743d7c82010-08-19 02:47:08 +0000144
Jim Grosbacha030fa52010-08-14 00:15:52 +0000145 return true;
146}
147
148/// AdjustStackOffset - Helper function used to adjust the stack frame offset.
Matthias Braun941a7052016-07-28 18:40:00 +0000149void LocalStackSlotPass::AdjustStackOffset(MachineFrameInfo &MFI,
Jim Grosbachdbfc2ce2010-08-18 22:44:49 +0000150 int FrameIdx, int64_t &Offset,
Jim Grosbach754f8e62010-08-23 20:40:38 +0000151 bool StackGrowsDown,
Jim Grosbachdbfc2ce2010-08-18 22:44:49 +0000152 unsigned &MaxAlign) {
Jim Grosbach754f8e62010-08-23 20:40:38 +0000153 // If the stack grows down, add the object size to find the lowest address.
154 if (StackGrowsDown)
Matthias Braun941a7052016-07-28 18:40:00 +0000155 Offset += MFI.getObjectSize(FrameIdx);
Jim Grosbach754f8e62010-08-23 20:40:38 +0000156
Matthias Braun941a7052016-07-28 18:40:00 +0000157 unsigned Align = MFI.getObjectAlignment(FrameIdx);
Jim Grosbacha030fa52010-08-14 00:15:52 +0000158
159 // If the alignment of this object is greater than that of the stack, then
160 // increase the stack alignment to match.
161 MaxAlign = std::max(MaxAlign, Align);
162
163 // Adjust to alignment boundary.
164 Offset = (Offset + Align - 1) / Align * Align;
165
Jim Grosbach754f8e62010-08-23 20:40:38 +0000166 int64_t LocalOffset = StackGrowsDown ? -Offset : Offset;
Jim Grosbacha030fa52010-08-14 00:15:52 +0000167 DEBUG(dbgs() << "Allocate FI(" << FrameIdx << ") to local offset "
Jim Grosbach754f8e62010-08-23 20:40:38 +0000168 << LocalOffset << "\n");
Jim Grosbachdbfc2ce2010-08-18 22:44:49 +0000169 // Keep the offset available for base register allocation
Jim Grosbach754f8e62010-08-23 20:40:38 +0000170 LocalOffsets[FrameIdx] = LocalOffset;
Jim Grosbachdbfc2ce2010-08-18 22:44:49 +0000171 // And tell MFI about it for PEI to use later
Matthias Braun941a7052016-07-28 18:40:00 +0000172 MFI.mapLocalFrameObject(FrameIdx, LocalOffset);
Jim Grosbach754f8e62010-08-23 20:40:38 +0000173
174 if (!StackGrowsDown)
Matthias Braun941a7052016-07-28 18:40:00 +0000175 Offset += MFI.getObjectSize(FrameIdx);
Jim Grosbacha030fa52010-08-14 00:15:52 +0000176
177 ++NumAllocations;
178}
179
Josh Magee22b8ba22013-12-19 03:17:11 +0000180/// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
181/// those required to be close to the Stack Protector) to stack offsets.
182void LocalStackSlotPass::AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
183 SmallSet<int, 16> &ProtectedObjs,
Matthias Braun941a7052016-07-28 18:40:00 +0000184 MachineFrameInfo &MFI,
Josh Magee22b8ba22013-12-19 03:17:11 +0000185 bool StackGrowsDown, int64_t &Offset,
186 unsigned &MaxAlign) {
Josh Magee22b8ba22013-12-19 03:17:11 +0000187 for (StackObjSet::const_iterator I = UnassignedObjs.begin(),
188 E = UnassignedObjs.end(); I != E; ++I) {
189 int i = *I;
190 AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign);
191 ProtectedObjs.insert(i);
192 }
193}
194
Jim Grosbacha030fa52010-08-14 00:15:52 +0000195/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
196/// abstract stack objects.
Jim Grosbacha030fa52010-08-14 00:15:52 +0000197void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
Jim Grosbacha030fa52010-08-14 00:15:52 +0000198 // Loop over all of the stack objects, assigning sequential addresses...
Matthias Braun941a7052016-07-28 18:40:00 +0000199 MachineFrameInfo &MFI = Fn.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000200 const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
Jim Grosbach754f8e62010-08-23 20:40:38 +0000201 bool StackGrowsDown =
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000202 TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
Jim Grosbacha030fa52010-08-14 00:15:52 +0000203 int64_t Offset = 0;
Jim Grosbach36d5ec32010-08-16 22:30:41 +0000204 unsigned MaxAlign = 0;
Josh Magee22b8ba22013-12-19 03:17:11 +0000205 StackProtector *SP = &getAnalysis<StackProtector>();
Jim Grosbacha030fa52010-08-14 00:15:52 +0000206
207 // Make sure that the stack protector comes before the local variables on the
208 // stack.
Josh Magee22b8ba22013-12-19 03:17:11 +0000209 SmallSet<int, 16> ProtectedObjs;
Matthias Braun941a7052016-07-28 18:40:00 +0000210 if (MFI.getStackProtectorIndex() >= 0) {
Josh Magee22b8ba22013-12-19 03:17:11 +0000211 StackObjSet LargeArrayObjs;
Josh Magee24c7f062014-02-01 01:36:16 +0000212 StackObjSet SmallArrayObjs;
213 StackObjSet AddrOfObjs;
214
Matthias Braun941a7052016-07-28 18:40:00 +0000215 AdjustStackOffset(MFI, MFI.getStackProtectorIndex(), Offset,
Jim Grosbach754f8e62010-08-23 20:40:38 +0000216 StackGrowsDown, MaxAlign);
Jim Grosbacha030fa52010-08-14 00:15:52 +0000217
218 // Assign large stack objects first.
Matthias Braun941a7052016-07-28 18:40:00 +0000219 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
220 if (MFI.isDeadObjectIndex(i))
Jim Grosbacha030fa52010-08-14 00:15:52 +0000221 continue;
Matthias Braun941a7052016-07-28 18:40:00 +0000222 if (MFI.getStackProtectorIndex() == (int)i)
Jim Grosbacha030fa52010-08-14 00:15:52 +0000223 continue;
Jim Grosbacha030fa52010-08-14 00:15:52 +0000224
Matthias Braun941a7052016-07-28 18:40:00 +0000225 switch (SP->getSSPLayout(MFI.getObjectAllocation(i))) {
Josh Magee22b8ba22013-12-19 03:17:11 +0000226 case StackProtector::SSPLK_None:
Josh Magee24c7f062014-02-01 01:36:16 +0000227 continue;
Josh Magee22b8ba22013-12-19 03:17:11 +0000228 case StackProtector::SSPLK_SmallArray:
Josh Magee24c7f062014-02-01 01:36:16 +0000229 SmallArrayObjs.insert(i);
230 continue;
Josh Magee22b8ba22013-12-19 03:17:11 +0000231 case StackProtector::SSPLK_AddrOf:
Josh Magee24c7f062014-02-01 01:36:16 +0000232 AddrOfObjs.insert(i);
Josh Magee22b8ba22013-12-19 03:17:11 +0000233 continue;
234 case StackProtector::SSPLK_LargeArray:
235 LargeArrayObjs.insert(i);
236 continue;
237 }
238 llvm_unreachable("Unexpected SSPLayoutKind.");
Jim Grosbacha030fa52010-08-14 00:15:52 +0000239 }
Josh Magee22b8ba22013-12-19 03:17:11 +0000240
241 AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
242 Offset, MaxAlign);
Josh Magee24c7f062014-02-01 01:36:16 +0000243 AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
244 Offset, MaxAlign);
245 AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
246 Offset, MaxAlign);
Jim Grosbacha030fa52010-08-14 00:15:52 +0000247 }
248
249 // Then assign frame offsets to stack objects that are not used to spill
250 // callee saved registers.
Matthias Braun941a7052016-07-28 18:40:00 +0000251 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
252 if (MFI.isDeadObjectIndex(i))
Jim Grosbacha030fa52010-08-14 00:15:52 +0000253 continue;
Matthias Braun941a7052016-07-28 18:40:00 +0000254 if (MFI.getStackProtectorIndex() == (int)i)
Jim Grosbacha030fa52010-08-14 00:15:52 +0000255 continue;
Josh Magee22b8ba22013-12-19 03:17:11 +0000256 if (ProtectedObjs.count(i))
Jim Grosbacha030fa52010-08-14 00:15:52 +0000257 continue;
258
Jim Grosbach754f8e62010-08-23 20:40:38 +0000259 AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign);
Jim Grosbacha030fa52010-08-14 00:15:52 +0000260 }
261
Jim Grosbacha030fa52010-08-14 00:15:52 +0000262 // Remember how big this blob of stack space is
Matthias Braun941a7052016-07-28 18:40:00 +0000263 MFI.setLocalFrameSize(Offset);
264 MFI.setLocalFrameMaxAlign(MaxAlign);
Jim Grosbacha030fa52010-08-14 00:15:52 +0000265}
Jim Grosbachc252ee22010-08-17 18:13:53 +0000266
Jim Grosbache0e9b302010-08-18 17:57:37 +0000267static inline bool
John Brawn1f26a472015-03-20 17:20:07 +0000268lookupCandidateBaseReg(unsigned BaseReg,
269 int64_t BaseOffset,
Jim Grosbach754f8e62010-08-23 20:40:38 +0000270 int64_t FrameSizeAdjust,
Jim Grosbachdbfc2ce2010-08-18 22:44:49 +0000271 int64_t LocalFrameOffset,
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000272 const MachineInstr &MI,
Jim Grosbache0e9b302010-08-18 17:57:37 +0000273 const TargetRegisterInfo *TRI) {
Hal Finkel71532512013-04-30 20:04:37 +0000274 // Check if the relative offset from the where the base register references
275 // to the target address is in range for the instruction.
276 int64_t Offset = FrameSizeAdjust + LocalFrameOffset - BaseOffset;
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000277 return TRI->isFrameOffsetLegal(&MI, BaseReg, Offset);
Jim Grosbache0e9b302010-08-18 17:57:37 +0000278}
279
Jim Grosbach06006912010-08-20 16:48:30 +0000280bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) {
Jim Grosbachc252ee22010-08-17 18:13:53 +0000281 // Scan the function's instructions looking for frame index references.
282 // For each, ask the target if it wants a virtual base register for it
283 // based on what we can tell it about where the local will end up in the
284 // stack frame. If it wants one, re-use a suitable one we've previously
285 // allocated, or if there isn't one that fits the bill, allocate a new one
286 // and ask the target to create a defining instruction for it.
Jim Grosbach06006912010-08-20 16:48:30 +0000287 bool UsedBaseReg = false;
Jim Grosbachc252ee22010-08-17 18:13:53 +0000288
Matthias Braun941a7052016-07-28 18:40:00 +0000289 MachineFrameInfo &MFI = Fn.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000290 const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo();
291 const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
Jim Grosbach7648a212010-08-20 20:25:31 +0000292 bool StackGrowsDown =
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000293 TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
Jim Grosbachc252ee22010-08-17 18:13:53 +0000294
Jim Grosbach365e9312010-08-31 17:58:19 +0000295 // Collect all of the instructions in the block that reference
296 // a frame index. Also store the frame index referenced to ease later
297 // lookup. (For any insn that has more than one FI reference, we arbitrarily
298 // choose the first one).
299 SmallVector<FrameRef, 64> FrameReferenceInsns;
Jim Grosbachdbfc2ce2010-08-18 22:44:49 +0000300
Matt Arsenault8fac5012016-10-26 14:53:50 +0000301 unsigned Order = 0;
302
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000303 for (MachineBasicBlock &BB : Fn) {
304 for (MachineInstr &MI : BB) {
Lang Hames7468daa2013-11-29 06:35:30 +0000305 // Debug value, stackmap and patchpoint instructions can't be out of
306 // range, so they don't need any updates.
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000307 if (MI.isDebugValue() || MI.getOpcode() == TargetOpcode::STATEPOINT ||
308 MI.getOpcode() == TargetOpcode::STACKMAP ||
309 MI.getOpcode() == TargetOpcode::PATCHPOINT)
Jim Grosbach3cf08662010-08-17 22:41:55 +0000310 continue;
Bill Wendling3fff1fd2010-12-17 23:09:14 +0000311
Jim Grosbachc252ee22010-08-17 18:13:53 +0000312 // For now, allocate the base register(s) within the basic block
313 // where they're used, and don't try to keep them around outside
314 // of that. It may be beneficial to try sharing them more broadly
315 // than that, but the increased register pressure makes that a
316 // tricky thing to balance. Investigate if re-materializing these
317 // becomes an issue.
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000318 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Jim Grosbachc252ee22010-08-17 18:13:53 +0000319 // Consider replacing all frame index operands that reference
320 // an object allocated in the local block.
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000321 if (MI.getOperand(i).isFI()) {
Jim Grosbach3cf08662010-08-17 22:41:55 +0000322 // Don't try this with values not in the local block.
Matthias Braun941a7052016-07-28 18:40:00 +0000323 if (!MFI.isObjectPreAllocated(MI.getOperand(i).getIndex()))
Jim Grosbach365e9312010-08-31 17:58:19 +0000324 break;
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000325 int Idx = MI.getOperand(i).getIndex();
Hal Finkel71532512013-04-30 20:04:37 +0000326 int64_t LocalOffset = LocalOffsets[Idx];
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000327 if (!TRI->needsFrameBaseReg(&MI, LocalOffset))
Hal Finkel71532512013-04-30 20:04:37 +0000328 break;
Matt Arsenault8fac5012016-10-26 14:53:50 +0000329 FrameReferenceInsns.push_back(FrameRef(&MI, LocalOffset, Idx, Order++));
Jim Grosbach365e9312010-08-31 17:58:19 +0000330 break;
331 }
332 }
333 }
334 }
Bill Wendling3fff1fd2010-12-17 23:09:14 +0000335
Mandeep Singh Grange82678a2016-10-18 00:11:19 +0000336 // Sort the frame references by local offset.
337 // Use frame index as a tie-breaker in case MI's have the same offset.
Mandeep Singh Grange92f0cf2018-04-06 18:08:42 +0000338 llvm::sort(FrameReferenceInsns.begin(), FrameReferenceInsns.end());
Jim Grosbach3cf08662010-08-17 22:41:55 +0000339
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000340 MachineBasicBlock *Entry = &Fn.front();
Jim Grosbache0e9b302010-08-18 17:57:37 +0000341
Hal Finkel71532512013-04-30 20:04:37 +0000342 unsigned BaseReg = 0;
343 int64_t BaseOffset = 0;
344
Bill Wendling3fff1fd2010-12-17 23:09:14 +0000345 // Loop through the frame references and allocate for them as necessary.
Jim Grosbach365e9312010-08-31 17:58:19 +0000346 for (int ref = 0, e = FrameReferenceInsns.size(); ref < e ; ++ref) {
Hal Finkel71532512013-04-30 20:04:37 +0000347 FrameRef &FR = FrameReferenceInsns[ref];
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000348 MachineInstr &MI = *FR.getMachineInstr();
Hal Finkel71532512013-04-30 20:04:37 +0000349 int64_t LocalOffset = FR.getLocalOffset();
350 int FrameIdx = FR.getFrameIndex();
Matthias Braun941a7052016-07-28 18:40:00 +0000351 assert(MFI.isObjectPreAllocated(FrameIdx) &&
Hal Finkel71532512013-04-30 20:04:37 +0000352 "Only pre-allocated locals expected!");
Jim Grosbachc252ee22010-08-17 18:13:53 +0000353
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000354 DEBUG(dbgs() << "Considering: " << MI);
Jim Grosbach3cf08662010-08-17 22:41:55 +0000355
Hal Finkel71532512013-04-30 20:04:37 +0000356 unsigned idx = 0;
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000357 for (unsigned f = MI.getNumOperands(); idx != f; ++idx) {
358 if (!MI.getOperand(idx).isFI())
Hal Finkel71532512013-04-30 20:04:37 +0000359 continue;
Jim Grosbache0e9b302010-08-18 17:57:37 +0000360
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000361 if (FrameIdx == MI.getOperand(idx).getIndex())
Hal Finkel71532512013-04-30 20:04:37 +0000362 break;
Jim Grosbachc252ee22010-08-17 18:13:53 +0000363 }
Hal Finkel71532512013-04-30 20:04:37 +0000364
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000365 assert(idx < MI.getNumOperands() && "Cannot find FI operand");
Hal Finkel71532512013-04-30 20:04:37 +0000366
367 int64_t Offset = 0;
Matthias Braun941a7052016-07-28 18:40:00 +0000368 int64_t FrameSizeAdjust = StackGrowsDown ? MFI.getLocalFrameSize() : 0;
Hal Finkel71532512013-04-30 20:04:37 +0000369
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000370 DEBUG(dbgs() << " Replacing FI in: " << MI);
Hal Finkel71532512013-04-30 20:04:37 +0000371
372 // If we have a suitable base register available, use it; otherwise
373 // create a new one. Note that any offset encoded in the
374 // instruction itself will be taken into account by the target,
375 // so we don't have to adjust for it here when reusing a base
376 // register.
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000377 if (UsedBaseReg &&
378 lookupCandidateBaseReg(BaseReg, BaseOffset, FrameSizeAdjust,
379 LocalOffset, MI, TRI)) {
Hal Finkel71532512013-04-30 20:04:37 +0000380 DEBUG(dbgs() << " Reusing base register " << BaseReg << "\n");
381 // We found a register to reuse.
382 Offset = FrameSizeAdjust + LocalOffset - BaseOffset;
383 } else {
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000384 // No previously defined register was in range, so create a new one.
385 int64_t InstrOffset = TRI->getFrameIndexInstrOffset(&MI, idx);
Hal Finkel71532512013-04-30 20:04:37 +0000386
387 int64_t PrevBaseOffset = BaseOffset;
388 BaseOffset = FrameSizeAdjust + LocalOffset + InstrOffset;
389
390 // We'd like to avoid creating single-use virtual base registers.
391 // Because the FrameRefs are in sorted order, and we've already
392 // processed all FrameRefs before this one, just check whether or not
393 // the next FrameRef will be able to reuse this new register. If not,
394 // then don't bother creating it.
Benjamin Kramerc24d19c2014-02-23 13:34:21 +0000395 if (ref + 1 >= e ||
396 !lookupCandidateBaseReg(
John Brawn1f26a472015-03-20 17:20:07 +0000397 BaseReg, BaseOffset, FrameSizeAdjust,
Benjamin Kramerc24d19c2014-02-23 13:34:21 +0000398 FrameReferenceInsns[ref + 1].getLocalOffset(),
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000399 *FrameReferenceInsns[ref + 1].getMachineInstr(), TRI)) {
Hal Finkel71532512013-04-30 20:04:37 +0000400 BaseOffset = PrevBaseOffset;
401 continue;
402 }
403
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000404 const MachineFunction *MF = MI.getMF();
Hal Finkel71532512013-04-30 20:04:37 +0000405 const TargetRegisterClass *RC = TRI->getPointerRegClass(*MF);
406 BaseReg = Fn.getRegInfo().createVirtualRegister(RC);
407
408 DEBUG(dbgs() << " Materializing base register " << BaseReg <<
409 " at frame local offset " << LocalOffset + InstrOffset << "\n");
410
411 // Tell the target to insert the instruction to initialize
412 // the base register.
413 // MachineBasicBlock::iterator InsertionPt = Entry->begin();
414 TRI->materializeFrameBaseRegister(Entry, BaseReg, FrameIdx,
415 InstrOffset);
416
417 // The base register already includes any offset specified
418 // by the instruction, so account for that so it doesn't get
419 // applied twice.
420 Offset = -InstrOffset;
421
422 ++NumBaseRegisters;
423 UsedBaseReg = true;
424 }
425 assert(BaseReg != 0 && "Unable to allocate virtual base register!");
426
427 // Modify the instruction to use the new base register rather
428 // than the frame index operand.
Duncan P. N. Exon Smithc73850c2016-06-30 23:39:46 +0000429 TRI->resolveFrameIndex(MI, BaseReg, Offset);
430 DEBUG(dbgs() << "Resolved: " << MI);
Hal Finkel71532512013-04-30 20:04:37 +0000431
432 ++NumReplacements;
Jim Grosbachc252ee22010-08-17 18:13:53 +0000433 }
Hal Finkel71532512013-04-30 20:04:37 +0000434
Jim Grosbach06006912010-08-20 16:48:30 +0000435 return UsedBaseReg;
Jim Grosbachc252ee22010-08-17 18:13:53 +0000436}