2010-08-14 08:15:52 +08:00
|
|
|
//===- LocalStackSlotAllocation.cpp - Pre-allocate locals to stack slots --===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-08-14 08:15:52 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass assigns local frame indices to stack slots relative to one another
|
|
|
|
// and allocates additional base registers to access them when the target
|
2011-01-07 12:58:58 +08:00
|
|
|
// estimates they are likely to be out of range of stack pointer and frame
|
2010-08-14 08:15:52 +08:00
|
|
|
// pointer relative addressing.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-12-19 11:17:11 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2017-10-11 06:33:29 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2017-10-11 06:33:29 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2017-10-11 06:33:29 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetFrameLowering.h"
|
|
|
|
#include "llvm/CodeGen/TargetOpcodes.h"
|
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2010-08-14 08:15:52 +08:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-10-11 06:33:29 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <tuple>
|
2010-08-14 08:15:52 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "localstackalloc"
|
|
|
|
|
2010-08-18 02:13:53 +08:00
|
|
|
STATISTIC(NumAllocations, "Number of frame indices allocated into local block");
|
|
|
|
STATISTIC(NumBaseRegisters, "Number of virtual frame base registers allocated");
|
|
|
|
STATISTIC(NumReplacements, "Number of frame indices references replaced");
|
2010-08-14 08:15:52 +08:00
|
|
|
|
|
|
|
namespace {
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2010-09-01 01:58:19 +08:00
|
|
|
class FrameRef {
|
|
|
|
MachineBasicBlock::iterator MI; // Instr referencing the frame
|
|
|
|
int64_t LocalOffset; // Local offset of the frame idx referenced
|
2013-05-01 04:04:37 +08:00
|
|
|
int FrameIdx; // The frame index
|
2016-10-26 22:53:50 +08:00
|
|
|
|
|
|
|
// Order reference instruction appears in program. Used to ensure
|
|
|
|
// deterministic order when multiple instructions may reference the same
|
|
|
|
// location.
|
|
|
|
unsigned Order;
|
|
|
|
|
2010-09-01 01:58:19 +08:00
|
|
|
public:
|
2016-10-26 22:53:50 +08:00
|
|
|
FrameRef(MachineInstr *I, int64_t Offset, int Idx, unsigned Ord) :
|
|
|
|
MI(I), LocalOffset(Offset), FrameIdx(Idx), Order(Ord) {}
|
|
|
|
|
2010-09-01 01:58:19 +08:00
|
|
|
bool operator<(const FrameRef &RHS) const {
|
2016-10-26 22:53:50 +08:00
|
|
|
return std::tie(LocalOffset, FrameIdx, Order) <
|
|
|
|
std::tie(RHS.LocalOffset, RHS.FrameIdx, RHS.Order);
|
2010-09-01 01:58:19 +08:00
|
|
|
}
|
2016-10-26 22:53:50 +08:00
|
|
|
|
2013-05-01 04:04:37 +08:00
|
|
|
MachineBasicBlock::iterator getMachineInstr() const { return MI; }
|
|
|
|
int64_t getLocalOffset() const { return LocalOffset; }
|
|
|
|
int getFrameIndex() const { return FrameIdx; }
|
2010-09-01 01:58:19 +08:00
|
|
|
};
|
|
|
|
|
2010-08-14 08:15:52 +08:00
|
|
|
class LocalStackSlotPass: public MachineFunctionPass {
|
2017-10-11 06:33:29 +08:00
|
|
|
SmallVector<int64_t, 16> LocalOffsets;
|
|
|
|
|
2013-12-19 11:17:11 +08:00
|
|
|
/// StackObjSet - A set of stack object indexes
|
2017-10-11 06:33:29 +08:00
|
|
|
using StackObjSet = SmallSetVector<int, 8>;
|
2010-08-18 02:13:53 +08:00
|
|
|
|
2016-07-29 02:40:00 +08:00
|
|
|
void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx, int64_t &Offset,
|
[Alignment][NFC] Transition to MachineFrameInfo::getObjectAlign()
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: arsenm, sdardis, nemanjai, jvesely, nhaehnle, hiraditya, kbarton, jrtc27, atanasyan, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77215
2020-04-01 21:49:04 +08:00
|
|
|
bool StackGrowsDown, Align &MaxAlign);
|
2013-12-19 11:17:11 +08:00
|
|
|
void AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
|
|
|
|
SmallSet<int, 16> &ProtectedObjs,
|
2016-07-29 02:40:00 +08:00
|
|
|
MachineFrameInfo &MFI, bool StackGrowsDown,
|
[Alignment][NFC] Transition to MachineFrameInfo::getObjectAlign()
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: arsenm, sdardis, nemanjai, jvesely, nhaehnle, hiraditya, kbarton, jrtc27, atanasyan, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77215
2020-04-01 21:49:04 +08:00
|
|
|
int64_t &Offset, Align &MaxAlign);
|
2010-08-19 06:44:49 +08:00
|
|
|
void calculateFrameObjectOffsets(MachineFunction &Fn);
|
2010-08-21 00:48:30 +08:00
|
|
|
bool insertFrameReferenceRegisters(MachineFunction &Fn);
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2010-08-14 08:15:52 +08:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2016-10-26 22:53:50 +08:00
|
|
|
explicit LocalStackSlotPass() : MachineFunctionPass(ID) {
|
2013-12-19 11:17:11 +08:00
|
|
|
initializeLocalStackSlotPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2010-08-14 08:15:52 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-08-14 08:15:52 +08:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
};
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2010-08-14 08:15:52 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char LocalStackSlotPass::ID = 0;
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2012-02-09 05:23:13 +08:00
|
|
|
char &llvm::LocalStackSlotAllocationID = LocalStackSlotPass::ID;
|
2018-07-13 08:08:38 +08:00
|
|
|
INITIALIZE_PASS(LocalStackSlotPass, DEBUG_TYPE,
|
|
|
|
"Local Stack Slot Allocation", false, false)
|
2013-12-19 11:17:11 +08:00
|
|
|
|
2010-08-14 08:15:52 +08:00
|
|
|
bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) {
|
2016-07-29 02:40:00 +08:00
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
|
2016-07-29 02:40:00 +08:00
|
|
|
unsigned LocalObjectCount = MFI.getObjectIndexEnd();
|
2010-08-19 06:44:49 +08:00
|
|
|
|
2010-08-25 03:05:43 +08:00
|
|
|
// If the target doesn't want/need this pass, or if there are no locals
|
|
|
|
// to consider, early exit.
|
2020-09-11 00:08:41 +08:00
|
|
|
if (LocalObjectCount == 0 || !TRI->requiresVirtualBaseRegisters(MF))
|
2022-02-16 18:58:54 +08:00
|
|
|
return false;
|
2010-08-19 06:44:49 +08:00
|
|
|
|
|
|
|
// Make sure we have enough space to store the local offsets.
|
2016-07-29 02:40:00 +08:00
|
|
|
LocalOffsets.resize(MFI.getObjectIndexEnd());
|
2010-08-19 06:44:49 +08:00
|
|
|
|
2010-08-18 02:13:53 +08:00
|
|
|
// Lay out the local blob.
|
2010-08-14 08:15:52 +08:00
|
|
|
calculateFrameObjectOffsets(MF);
|
2010-08-18 02:13:53 +08:00
|
|
|
|
|
|
|
// Insert virtual base registers to resolve frame index references.
|
2010-08-21 00:48:30 +08:00
|
|
|
bool UsedBaseRegs = insertFrameReferenceRegisters(MF);
|
2010-08-19 06:44:49 +08:00
|
|
|
|
2010-08-19 10:47:08 +08:00
|
|
|
// Tell MFI whether any base registers were allocated. PEI will only
|
|
|
|
// want to use the local block allocations from this pass if there were any.
|
|
|
|
// Otherwise, PEI can do a bit better job of getting the alignment right
|
|
|
|
// without a hole at the start since it knows the alignment of the stack
|
|
|
|
// at the start of local allocation, and this pass doesn't.
|
2016-07-29 02:40:00 +08:00
|
|
|
MFI.setUseLocalStackAllocationBlock(UsedBaseRegs);
|
2010-08-19 10:47:08 +08:00
|
|
|
|
2010-08-14 08:15:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AdjustStackOffset - Helper function used to adjust the stack frame offset.
|
[Alignment][NFC] Transition to MachineFrameInfo::getObjectAlign()
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: arsenm, sdardis, nemanjai, jvesely, nhaehnle, hiraditya, kbarton, jrtc27, atanasyan, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77215
2020-04-01 21:49:04 +08:00
|
|
|
void LocalStackSlotPass::AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
|
|
|
|
int64_t &Offset, bool StackGrowsDown,
|
|
|
|
Align &MaxAlign) {
|
2010-08-24 04:40:38 +08:00
|
|
|
// If the stack grows down, add the object size to find the lowest address.
|
|
|
|
if (StackGrowsDown)
|
2016-07-29 02:40:00 +08:00
|
|
|
Offset += MFI.getObjectSize(FrameIdx);
|
2010-08-24 04:40:38 +08:00
|
|
|
|
[Alignment][NFC] Transition to MachineFrameInfo::getObjectAlign()
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: arsenm, sdardis, nemanjai, jvesely, nhaehnle, hiraditya, kbarton, jrtc27, atanasyan, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77215
2020-04-01 21:49:04 +08:00
|
|
|
Align Alignment = MFI.getObjectAlign(FrameIdx);
|
2010-08-14 08:15:52 +08:00
|
|
|
|
|
|
|
// If the alignment of this object is greater than that of the stack, then
|
|
|
|
// increase the stack alignment to match.
|
[Alignment][NFC] Transition to MachineFrameInfo::getObjectAlign()
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: arsenm, sdardis, nemanjai, jvesely, nhaehnle, hiraditya, kbarton, jrtc27, atanasyan, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77215
2020-04-01 21:49:04 +08:00
|
|
|
MaxAlign = std::max(MaxAlign, Alignment);
|
2010-08-14 08:15:52 +08:00
|
|
|
|
|
|
|
// Adjust to alignment boundary.
|
[Alignment][NFC] Transition to MachineFrameInfo::getObjectAlign()
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: arsenm, sdardis, nemanjai, jvesely, nhaehnle, hiraditya, kbarton, jrtc27, atanasyan, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77215
2020-04-01 21:49:04 +08:00
|
|
|
Offset = alignTo(Offset, Alignment);
|
2010-08-14 08:15:52 +08:00
|
|
|
|
2010-08-24 04:40:38 +08:00
|
|
|
int64_t LocalOffset = StackGrowsDown ? -Offset : Offset;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Allocate FI(" << FrameIdx << ") to local offset "
|
|
|
|
<< LocalOffset << "\n");
|
2010-08-19 06:44:49 +08:00
|
|
|
// Keep the offset available for base register allocation
|
2010-08-24 04:40:38 +08:00
|
|
|
LocalOffsets[FrameIdx] = LocalOffset;
|
2010-08-19 06:44:49 +08:00
|
|
|
// And tell MFI about it for PEI to use later
|
2016-07-29 02:40:00 +08:00
|
|
|
MFI.mapLocalFrameObject(FrameIdx, LocalOffset);
|
2010-08-24 04:40:38 +08:00
|
|
|
|
|
|
|
if (!StackGrowsDown)
|
2016-07-29 02:40:00 +08:00
|
|
|
Offset += MFI.getObjectSize(FrameIdx);
|
2010-08-14 08:15:52 +08:00
|
|
|
|
|
|
|
++NumAllocations;
|
|
|
|
}
|
|
|
|
|
2013-12-19 11:17:11 +08:00
|
|
|
/// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
|
|
|
|
/// those required to be close to the Stack Protector) to stack offsets.
|
[Alignment][NFC] Transition to MachineFrameInfo::getObjectAlign()
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: arsenm, sdardis, nemanjai, jvesely, nhaehnle, hiraditya, kbarton, jrtc27, atanasyan, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77215
2020-04-01 21:49:04 +08:00
|
|
|
void LocalStackSlotPass::AssignProtectedObjSet(
|
|
|
|
const StackObjSet &UnassignedObjs, SmallSet<int, 16> &ProtectedObjs,
|
|
|
|
MachineFrameInfo &MFI, bool StackGrowsDown, int64_t &Offset,
|
|
|
|
Align &MaxAlign) {
|
2021-02-14 12:41:39 +08:00
|
|
|
for (int i : UnassignedObjs) {
|
2013-12-19 11:17:11 +08:00
|
|
|
AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign);
|
|
|
|
ProtectedObjs.insert(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-14 08:15:52 +08:00
|
|
|
/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
|
|
|
|
/// abstract stack objects.
|
|
|
|
void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
|
|
|
|
// Loop over all of the stack objects, assigning sequential addresses...
|
2016-07-29 02:40:00 +08:00
|
|
|
MachineFrameInfo &MFI = Fn.getFrameInfo();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
|
2010-08-24 04:40:38 +08:00
|
|
|
bool StackGrowsDown =
|
2011-01-10 20:39:04 +08:00
|
|
|
TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
|
2010-08-14 08:15:52 +08:00
|
|
|
int64_t Offset = 0;
|
[Alignment][NFC] Transition to MachineFrameInfo::getObjectAlign()
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: arsenm, sdardis, nemanjai, jvesely, nhaehnle, hiraditya, kbarton, jrtc27, atanasyan, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77215
2020-04-01 21:49:04 +08:00
|
|
|
Align MaxAlign;
|
2010-08-14 08:15:52 +08:00
|
|
|
|
|
|
|
// Make sure that the stack protector comes before the local variables on the
|
|
|
|
// stack.
|
2013-12-19 11:17:11 +08:00
|
|
|
SmallSet<int, 16> ProtectedObjs;
|
2019-07-18 04:46:09 +08:00
|
|
|
if (MFI.hasStackProtectorIndex()) {
|
|
|
|
int StackProtectorFI = MFI.getStackProtectorIndex();
|
2019-07-18 04:46:19 +08:00
|
|
|
|
|
|
|
// We need to make sure we didn't pre-allocate the stack protector when
|
|
|
|
// doing this.
|
|
|
|
// If we already have a stack protector, this will re-assign it to a slot
|
|
|
|
// that is **not** covering the protected objects.
|
|
|
|
assert(!MFI.isObjectPreAllocated(StackProtectorFI) &&
|
|
|
|
"Stack protector pre-allocated in LocalStackSlotAllocation");
|
|
|
|
|
2013-12-19 11:17:11 +08:00
|
|
|
StackObjSet LargeArrayObjs;
|
2014-02-01 09:36:16 +08:00
|
|
|
StackObjSet SmallArrayObjs;
|
|
|
|
StackObjSet AddrOfObjs;
|
|
|
|
|
2021-12-14 19:11:41 +08:00
|
|
|
// Only place the stack protector in the local stack area if the target
|
|
|
|
// allows it.
|
|
|
|
if (TFI.isStackIdSafeForLocalArea(MFI.getStackID(StackProtectorFI)))
|
|
|
|
AdjustStackOffset(MFI, StackProtectorFI, Offset, StackGrowsDown,
|
|
|
|
MaxAlign);
|
2010-08-14 08:15:52 +08:00
|
|
|
|
|
|
|
// Assign large stack objects first.
|
2016-07-29 02:40:00 +08:00
|
|
|
for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
|
|
|
|
if (MFI.isDeadObjectIndex(i))
|
2010-08-14 08:15:52 +08:00
|
|
|
continue;
|
2019-07-18 04:46:09 +08:00
|
|
|
if (StackProtectorFI == (int)i)
|
2010-08-14 08:15:52 +08:00
|
|
|
continue;
|
2020-07-14 23:20:00 +08:00
|
|
|
if (!TFI.isStackIdSafeForLocalArea(MFI.getStackID(i)))
|
|
|
|
continue;
|
2010-08-14 08:15:52 +08:00
|
|
|
|
2018-07-13 08:08:38 +08:00
|
|
|
switch (MFI.getObjectSSPLayout(i)) {
|
|
|
|
case MachineFrameInfo::SSPLK_None:
|
2014-02-01 09:36:16 +08:00
|
|
|
continue;
|
2018-07-13 08:08:38 +08:00
|
|
|
case MachineFrameInfo::SSPLK_SmallArray:
|
2014-02-01 09:36:16 +08:00
|
|
|
SmallArrayObjs.insert(i);
|
|
|
|
continue;
|
2018-07-13 08:08:38 +08:00
|
|
|
case MachineFrameInfo::SSPLK_AddrOf:
|
2014-02-01 09:36:16 +08:00
|
|
|
AddrOfObjs.insert(i);
|
2013-12-19 11:17:11 +08:00
|
|
|
continue;
|
2018-07-13 08:08:38 +08:00
|
|
|
case MachineFrameInfo::SSPLK_LargeArray:
|
2013-12-19 11:17:11 +08:00
|
|
|
LargeArrayObjs.insert(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unexpected SSPLayoutKind.");
|
2010-08-14 08:15:52 +08:00
|
|
|
}
|
2013-12-19 11:17:11 +08:00
|
|
|
|
|
|
|
AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
|
|
|
|
Offset, MaxAlign);
|
2014-02-01 09:36:16 +08:00
|
|
|
AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
|
|
|
|
Offset, MaxAlign);
|
|
|
|
AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
|
|
|
|
Offset, MaxAlign);
|
2010-08-14 08:15:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then assign frame offsets to stack objects that are not used to spill
|
|
|
|
// callee saved registers.
|
2016-07-29 02:40:00 +08:00
|
|
|
for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
|
|
|
|
if (MFI.isDeadObjectIndex(i))
|
2010-08-14 08:15:52 +08:00
|
|
|
continue;
|
2016-07-29 02:40:00 +08:00
|
|
|
if (MFI.getStackProtectorIndex() == (int)i)
|
2010-08-14 08:15:52 +08:00
|
|
|
continue;
|
2013-12-19 11:17:11 +08:00
|
|
|
if (ProtectedObjs.count(i))
|
2010-08-14 08:15:52 +08:00
|
|
|
continue;
|
2020-07-14 23:20:00 +08:00
|
|
|
if (!TFI.isStackIdSafeForLocalArea(MFI.getStackID(i)))
|
|
|
|
continue;
|
2010-08-14 08:15:52 +08:00
|
|
|
|
2010-08-24 04:40:38 +08:00
|
|
|
AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign);
|
2010-08-14 08:15:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remember how big this blob of stack space is
|
2016-07-29 02:40:00 +08:00
|
|
|
MFI.setLocalFrameSize(Offset);
|
[Alignment][NFC] Transition to MachineFrameInfo::getObjectAlign()
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: arsenm, sdardis, nemanjai, jvesely, nhaehnle, hiraditya, kbarton, jrtc27, atanasyan, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77215
2020-04-01 21:49:04 +08:00
|
|
|
MFI.setLocalFrameMaxAlign(MaxAlign);
|
2010-08-14 08:15:52 +08:00
|
|
|
}
|
2010-08-18 02:13:53 +08:00
|
|
|
|
2010-08-19 01:57:37 +08:00
|
|
|
static inline bool
|
2015-03-21 01:20:07 +08:00
|
|
|
lookupCandidateBaseReg(unsigned BaseReg,
|
|
|
|
int64_t BaseOffset,
|
2010-08-24 04:40:38 +08:00
|
|
|
int64_t FrameSizeAdjust,
|
2010-08-19 06:44:49 +08:00
|
|
|
int64_t LocalFrameOffset,
|
2016-07-01 07:39:46 +08:00
|
|
|
const MachineInstr &MI,
|
2010-08-19 01:57:37 +08:00
|
|
|
const TargetRegisterInfo *TRI) {
|
2013-05-01 04:04:37 +08:00
|
|
|
// Check if the relative offset from the where the base register references
|
|
|
|
// to the target address is in range for the instruction.
|
|
|
|
int64_t Offset = FrameSizeAdjust + LocalFrameOffset - BaseOffset;
|
2016-07-01 07:39:46 +08:00
|
|
|
return TRI->isFrameOffsetLegal(&MI, BaseReg, Offset);
|
2010-08-19 01:57:37 +08:00
|
|
|
}
|
|
|
|
|
2010-08-21 00:48:30 +08:00
|
|
|
bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) {
|
2010-08-18 02:13:53 +08:00
|
|
|
// Scan the function's instructions looking for frame index references.
|
|
|
|
// For each, ask the target if it wants a virtual base register for it
|
|
|
|
// based on what we can tell it about where the local will end up in the
|
|
|
|
// stack frame. If it wants one, re-use a suitable one we've previously
|
|
|
|
// allocated, or if there isn't one that fits the bill, allocate a new one
|
|
|
|
// and ask the target to create a defining instruction for it.
|
2010-08-21 00:48:30 +08:00
|
|
|
bool UsedBaseReg = false;
|
2010-08-18 02:13:53 +08:00
|
|
|
|
2016-07-29 02:40:00 +08:00
|
|
|
MachineFrameInfo &MFI = Fn.getFrameInfo();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo();
|
|
|
|
const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
|
2010-08-21 04:25:31 +08:00
|
|
|
bool StackGrowsDown =
|
2011-01-10 20:39:04 +08:00
|
|
|
TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
|
2010-08-18 02:13:53 +08:00
|
|
|
|
2010-09-01 01:58:19 +08:00
|
|
|
// Collect all of the instructions in the block that reference
|
|
|
|
// a frame index. Also store the frame index referenced to ease later
|
|
|
|
// lookup. (For any insn that has more than one FI reference, we arbitrarily
|
|
|
|
// choose the first one).
|
|
|
|
SmallVector<FrameRef, 64> FrameReferenceInsns;
|
2010-08-19 06:44:49 +08:00
|
|
|
|
2016-10-26 22:53:50 +08:00
|
|
|
unsigned Order = 0;
|
|
|
|
|
2016-07-01 07:39:46 +08:00
|
|
|
for (MachineBasicBlock &BB : Fn) {
|
|
|
|
for (MachineInstr &MI : BB) {
|
2013-11-29 14:35:30 +08:00
|
|
|
// Debug value, stackmap and patchpoint instructions can't be out of
|
|
|
|
// range, so they don't need any updates.
|
2018-05-09 10:42:00 +08:00
|
|
|
if (MI.isDebugInstr() || MI.getOpcode() == TargetOpcode::STATEPOINT ||
|
2016-07-01 07:39:46 +08:00
|
|
|
MI.getOpcode() == TargetOpcode::STACKMAP ||
|
|
|
|
MI.getOpcode() == TargetOpcode::PATCHPOINT)
|
2010-08-18 06:41:55 +08:00
|
|
|
continue;
|
2010-12-18 07:09:14 +08:00
|
|
|
|
2010-08-18 02:13:53 +08:00
|
|
|
// For now, allocate the base register(s) within the basic block
|
|
|
|
// where they're used, and don't try to keep them around outside
|
|
|
|
// of that. It may be beneficial to try sharing them more broadly
|
|
|
|
// than that, but the increased register pressure makes that a
|
|
|
|
// tricky thing to balance. Investigate if re-materializing these
|
|
|
|
// becomes an issue.
|
2021-11-26 00:55:16 +08:00
|
|
|
for (const MachineOperand &MO : MI.operands()) {
|
2010-08-18 02:13:53 +08:00
|
|
|
// Consider replacing all frame index operands that reference
|
|
|
|
// an object allocated in the local block.
|
2021-11-26 00:55:16 +08:00
|
|
|
if (MO.isFI()) {
|
2010-08-18 06:41:55 +08:00
|
|
|
// Don't try this with values not in the local block.
|
2021-11-26 00:55:16 +08:00
|
|
|
if (!MFI.isObjectPreAllocated(MO.getIndex()))
|
2010-09-01 01:58:19 +08:00
|
|
|
break;
|
2021-11-26 00:55:16 +08:00
|
|
|
int Idx = MO.getIndex();
|
2013-05-01 04:04:37 +08:00
|
|
|
int64_t LocalOffset = LocalOffsets[Idx];
|
2016-07-01 07:39:46 +08:00
|
|
|
if (!TRI->needsFrameBaseReg(&MI, LocalOffset))
|
2013-05-01 04:04:37 +08:00
|
|
|
break;
|
2016-10-26 22:53:50 +08:00
|
|
|
FrameReferenceInsns.push_back(FrameRef(&MI, LocalOffset, Idx, Order++));
|
2010-09-01 01:58:19 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-18 07:09:14 +08:00
|
|
|
|
2016-10-18 08:11:19 +08:00
|
|
|
// Sort the frame references by local offset.
|
|
|
|
// Use frame index as a tie-breaker in case MI's have the same offset.
|
llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)
Summary: The convenience wrapper in STLExtras is available since rL342102.
Reviewers: dblaikie, javed.absar, JDevlieghere, andreadb
Subscribers: MatzeB, sanjoy, arsenm, dschuff, mehdi_amini, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, eraman, aheejin, kbarton, JDevlieghere, javed.absar, gbedwell, jrtc27, mgrang, atanasyan, steven_wu, george.burgess.iv, dexonsmith, kristina, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D52573
llvm-svn: 343163
2018-09-27 10:13:45 +08:00
|
|
|
llvm::sort(FrameReferenceInsns);
|
2010-09-01 01:58:19 +08:00
|
|
|
|
2015-10-10 03:13:58 +08:00
|
|
|
MachineBasicBlock *Entry = &Fn.front();
|
2010-09-01 01:58:19 +08:00
|
|
|
|
2013-05-01 04:04:37 +08:00
|
|
|
unsigned BaseReg = 0;
|
|
|
|
int64_t BaseOffset = 0;
|
|
|
|
|
2010-12-18 07:09:14 +08:00
|
|
|
// Loop through the frame references and allocate for them as necessary.
|
2010-09-01 01:58:19 +08:00
|
|
|
for (int ref = 0, e = FrameReferenceInsns.size(); ref < e ; ++ref) {
|
2013-05-01 04:04:37 +08:00
|
|
|
FrameRef &FR = FrameReferenceInsns[ref];
|
2016-07-01 07:39:46 +08:00
|
|
|
MachineInstr &MI = *FR.getMachineInstr();
|
2013-05-01 04:04:37 +08:00
|
|
|
int64_t LocalOffset = FR.getLocalOffset();
|
|
|
|
int FrameIdx = FR.getFrameIndex();
|
2016-07-29 02:40:00 +08:00
|
|
|
assert(MFI.isObjectPreAllocated(FrameIdx) &&
|
2013-05-01 04:04:37 +08:00
|
|
|
"Only pre-allocated locals expected!");
|
|
|
|
|
2019-07-26 06:23:48 +08:00
|
|
|
// We need to keep the references to the stack protector slot through frame
|
|
|
|
// index operands so that it gets resolved by PEI rather than this pass.
|
|
|
|
// This avoids accesses to the stack protector though virtual base
|
|
|
|
// registers, and forces PEI to address it using fp/sp/bp.
|
|
|
|
if (MFI.hasStackProtectorIndex() &&
|
|
|
|
FrameIdx == MFI.getStackProtectorIndex())
|
|
|
|
continue;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Considering: " << MI);
|
2013-05-01 04:04:37 +08:00
|
|
|
|
|
|
|
unsigned idx = 0;
|
2016-07-01 07:39:46 +08:00
|
|
|
for (unsigned f = MI.getNumOperands(); idx != f; ++idx) {
|
|
|
|
if (!MI.getOperand(idx).isFI())
|
2013-05-01 04:04:37 +08:00
|
|
|
continue;
|
|
|
|
|
2016-07-01 07:39:46 +08:00
|
|
|
if (FrameIdx == MI.getOperand(idx).getIndex())
|
2013-05-01 04:04:37 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-07-01 07:39:46 +08:00
|
|
|
assert(idx < MI.getNumOperands() && "Cannot find FI operand");
|
2013-05-01 04:04:37 +08:00
|
|
|
|
|
|
|
int64_t Offset = 0;
|
2016-07-29 02:40:00 +08:00
|
|
|
int64_t FrameSizeAdjust = StackGrowsDown ? MFI.getLocalFrameSize() : 0;
|
2013-05-01 04:04:37 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " Replacing FI in: " << MI);
|
2013-05-01 04:04:37 +08:00
|
|
|
|
|
|
|
// If we have a suitable base register available, use it; otherwise
|
|
|
|
// create a new one. Note that any offset encoded in the
|
|
|
|
// instruction itself will be taken into account by the target,
|
|
|
|
// so we don't have to adjust for it here when reusing a base
|
|
|
|
// register.
|
2016-07-01 07:39:46 +08:00
|
|
|
if (UsedBaseReg &&
|
|
|
|
lookupCandidateBaseReg(BaseReg, BaseOffset, FrameSizeAdjust,
|
|
|
|
LocalOffset, MI, TRI)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " Reusing base register " << BaseReg << "\n");
|
2013-05-01 04:04:37 +08:00
|
|
|
// We found a register to reuse.
|
|
|
|
Offset = FrameSizeAdjust + LocalOffset - BaseOffset;
|
|
|
|
} else {
|
2016-07-01 07:39:46 +08:00
|
|
|
// No previously defined register was in range, so create a new one.
|
|
|
|
int64_t InstrOffset = TRI->getFrameIndexInstrOffset(&MI, idx);
|
2013-05-01 04:04:37 +08:00
|
|
|
|
|
|
|
int64_t PrevBaseOffset = BaseOffset;
|
|
|
|
BaseOffset = FrameSizeAdjust + LocalOffset + InstrOffset;
|
|
|
|
|
|
|
|
// We'd like to avoid creating single-use virtual base registers.
|
|
|
|
// Because the FrameRefs are in sorted order, and we've already
|
|
|
|
// processed all FrameRefs before this one, just check whether or not
|
|
|
|
// the next FrameRef will be able to reuse this new register. If not,
|
|
|
|
// then don't bother creating it.
|
2014-02-23 21:34:21 +08:00
|
|
|
if (ref + 1 >= e ||
|
|
|
|
!lookupCandidateBaseReg(
|
2015-03-21 01:20:07 +08:00
|
|
|
BaseReg, BaseOffset, FrameSizeAdjust,
|
2014-02-23 21:34:21 +08:00
|
|
|
FrameReferenceInsns[ref + 1].getLocalOffset(),
|
2016-07-01 07:39:46 +08:00
|
|
|
*FrameReferenceInsns[ref + 1].getMachineInstr(), TRI)) {
|
2013-05-01 04:04:37 +08:00
|
|
|
BaseOffset = PrevBaseOffset;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-11 07:50:49 +08:00
|
|
|
const MachineFunction *MF = MI.getMF();
|
2013-05-01 04:04:37 +08:00
|
|
|
const TargetRegisterClass *RC = TRI->getPointerRegClass(*MF);
|
|
|
|
BaseReg = Fn.getRegInfo().createVirtualRegister(RC);
|
|
|
|
|
2021-01-23 07:02:36 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " Materializing base register"
|
2018-05-14 20:53:11 +08:00
|
|
|
<< " at frame local offset "
|
2021-01-23 07:02:36 +08:00
|
|
|
<< LocalOffset + InstrOffset);
|
2013-05-01 04:04:37 +08:00
|
|
|
|
|
|
|
// Tell the target to insert the instruction to initialize
|
|
|
|
// the base register.
|
|
|
|
// MachineBasicBlock::iterator InsertionPt = Entry->begin();
|
2021-01-23 07:02:36 +08:00
|
|
|
BaseReg = TRI->materializeFrameBaseRegister(Entry, FrameIdx, InstrOffset);
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << " into " << printReg(BaseReg, TRI) << '\n');
|
2013-05-01 04:04:37 +08:00
|
|
|
|
|
|
|
// The base register already includes any offset specified
|
|
|
|
// by the instruction, so account for that so it doesn't get
|
|
|
|
// applied twice.
|
|
|
|
Offset = -InstrOffset;
|
|
|
|
|
|
|
|
++NumBaseRegisters;
|
|
|
|
UsedBaseReg = true;
|
2010-08-18 02:13:53 +08:00
|
|
|
}
|
2013-05-01 04:04:37 +08:00
|
|
|
assert(BaseReg != 0 && "Unable to allocate virtual base register!");
|
|
|
|
|
|
|
|
// Modify the instruction to use the new base register rather
|
|
|
|
// than the frame index operand.
|
2016-07-01 07:39:46 +08:00
|
|
|
TRI->resolveFrameIndex(MI, BaseReg, Offset);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Resolved: " << MI);
|
2013-05-01 04:04:37 +08:00
|
|
|
|
|
|
|
++NumReplacements;
|
2010-08-18 02:13:53 +08:00
|
|
|
}
|
2013-05-01 04:04:37 +08:00
|
|
|
|
2010-08-21 00:48:30 +08:00
|
|
|
return UsedBaseReg;
|
2010-08-18 02:13:53 +08:00
|
|
|
}
|