2010-08-14 00:15:52 +00:00
|
|
|
//===- LocalStackSlotAllocation.cpp - Pre-allocate locals to stack slots --===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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 04:58:58 +00:00
|
|
|
// estimates they are likely to be out of range of stack pointer and frame
|
2010-08-14 00:15:52 +00:00
|
|
|
// pointer relative addressing.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2013-12-19 03:17:11 +00:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2013-12-19 03:17:11 +00:00
|
|
|
#include "llvm/CodeGen/StackProtector.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2010-08-14 00:15:52 +00:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-01-10 12:39:04 +00:00
|
|
|
#include "llvm/Target/TargetFrameLowering.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2010-08-14 00:15:52 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:02:50 +00:00
|
|
|
#define DEBUG_TYPE "localstackalloc"
|
|
|
|
|
2010-08-17 18:13:53 +00: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 00:15:52 +00:00
|
|
|
|
|
|
|
namespace {
|
2010-08-31 17:58:19 +00:00
|
|
|
class FrameRef {
|
|
|
|
MachineBasicBlock::iterator MI; // Instr referencing the frame
|
|
|
|
int64_t LocalOffset; // Local offset of the frame idx referenced
|
2013-04-30 20:04:37 +00:00
|
|
|
int FrameIdx; // The frame index
|
2010-08-31 17:58:19 +00:00
|
|
|
public:
|
2013-04-30 20:04:37 +00:00
|
|
|
FrameRef(MachineBasicBlock::iterator I, int64_t Offset, int Idx) :
|
|
|
|
MI(I), LocalOffset(Offset), FrameIdx(Idx) {}
|
2010-08-31 17:58:19 +00:00
|
|
|
bool operator<(const FrameRef &RHS) const {
|
|
|
|
return LocalOffset < RHS.LocalOffset;
|
|
|
|
}
|
2013-04-30 20:04:37 +00:00
|
|
|
MachineBasicBlock::iterator getMachineInstr() const { return MI; }
|
|
|
|
int64_t getLocalOffset() const { return LocalOffset; }
|
|
|
|
int getFrameIndex() const { return FrameIdx; }
|
2010-08-31 17:58:19 +00:00
|
|
|
};
|
|
|
|
|
2010-08-14 00:15:52 +00:00
|
|
|
class LocalStackSlotPass: public MachineFunctionPass {
|
2010-08-18 22:44:49 +00:00
|
|
|
SmallVector<int64_t,16> LocalOffsets;
|
2013-12-19 03:17:11 +00:00
|
|
|
/// StackObjSet - A set of stack object indexes
|
|
|
|
typedef SmallSetVector<int, 8> StackObjSet;
|
2010-08-17 18:13:53 +00:00
|
|
|
|
2010-08-18 22:44:49 +00:00
|
|
|
void AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx, int64_t &Offset,
|
2010-08-23 20:40:38 +00:00
|
|
|
bool StackGrowsDown, unsigned &MaxAlign);
|
2013-12-19 03:17:11 +00:00
|
|
|
void AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
|
|
|
|
SmallSet<int, 16> &ProtectedObjs,
|
|
|
|
MachineFrameInfo *MFI, bool StackGrowsDown,
|
|
|
|
int64_t &Offset, unsigned &MaxAlign);
|
2010-08-18 22:44:49 +00:00
|
|
|
void calculateFrameObjectOffsets(MachineFunction &Fn);
|
2010-08-20 16:48:30 +00:00
|
|
|
bool insertFrameReferenceRegisters(MachineFunction &Fn);
|
2010-08-14 00:15:52 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2013-12-19 03:17:11 +00:00
|
|
|
explicit LocalStackSlotPass() : MachineFunctionPass(ID) {
|
|
|
|
initializeLocalStackSlotPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2014-03-07 09:26:03 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2010-08-14 00:15:52 +00:00
|
|
|
|
2014-03-07 09:26:03 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-08-14 00:15:52 +00:00
|
|
|
AU.setPreservesCFG();
|
2013-12-19 03:17:11 +00:00
|
|
|
AU.addRequired<StackProtector>();
|
2010-08-14 00:15:52 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char LocalStackSlotPass::ID = 0;
|
2012-02-08 21:23:13 +00:00
|
|
|
char &llvm::LocalStackSlotAllocationID = LocalStackSlotPass::ID;
|
2013-12-19 03:17:11 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(LocalStackSlotPass, "localstackalloc",
|
|
|
|
"Local Stack Slot Allocation", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(StackProtector)
|
|
|
|
INITIALIZE_PASS_END(LocalStackSlotPass, "localstackalloc",
|
|
|
|
"Local Stack Slot Allocation", false, false)
|
|
|
|
|
2010-08-14 00:15:52 +00:00
|
|
|
|
|
|
|
bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) {
|
2010-08-18 22:44:49 +00:00
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
2010-08-24 19:05:43 +00:00
|
|
|
const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
|
2010-08-18 22:44:49 +00:00
|
|
|
unsigned LocalObjectCount = MFI->getObjectIndexEnd();
|
|
|
|
|
2010-08-24 19:05:43 +00:00
|
|
|
// If the target doesn't want/need this pass, or if there are no locals
|
|
|
|
// to consider, early exit.
|
|
|
|
if (!TRI->requiresVirtualBaseRegisters(MF) || LocalObjectCount == 0)
|
2010-08-18 22:44:49 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Make sure we have enough space to store the local offsets.
|
|
|
|
LocalOffsets.resize(MFI->getObjectIndexEnd());
|
|
|
|
|
2010-08-17 18:13:53 +00:00
|
|
|
// Lay out the local blob.
|
2010-08-14 00:15:52 +00:00
|
|
|
calculateFrameObjectOffsets(MF);
|
2010-08-17 18:13:53 +00:00
|
|
|
|
|
|
|
// Insert virtual base registers to resolve frame index references.
|
2010-08-20 16:48:30 +00:00
|
|
|
bool UsedBaseRegs = insertFrameReferenceRegisters(MF);
|
2010-08-18 22:44:49 +00:00
|
|
|
|
2010-08-19 02:47:08 +00: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.
|
2010-08-20 16:48:30 +00:00
|
|
|
MFI->setUseLocalStackAllocationBlock(UsedBaseRegs);
|
2010-08-19 02:47:08 +00:00
|
|
|
|
2010-08-14 00:15:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AdjustStackOffset - Helper function used to adjust the stack frame offset.
|
2010-08-18 22:44:49 +00:00
|
|
|
void LocalStackSlotPass::AdjustStackOffset(MachineFrameInfo *MFI,
|
|
|
|
int FrameIdx, int64_t &Offset,
|
2010-08-23 20:40:38 +00:00
|
|
|
bool StackGrowsDown,
|
2010-08-18 22:44:49 +00:00
|
|
|
unsigned &MaxAlign) {
|
2010-08-23 20:40:38 +00:00
|
|
|
// If the stack grows down, add the object size to find the lowest address.
|
|
|
|
if (StackGrowsDown)
|
|
|
|
Offset += MFI->getObjectSize(FrameIdx);
|
|
|
|
|
2010-08-14 00:15:52 +00:00
|
|
|
unsigned Align = MFI->getObjectAlignment(FrameIdx);
|
|
|
|
|
|
|
|
// If the alignment of this object is greater than that of the stack, then
|
|
|
|
// increase the stack alignment to match.
|
|
|
|
MaxAlign = std::max(MaxAlign, Align);
|
|
|
|
|
|
|
|
// Adjust to alignment boundary.
|
|
|
|
Offset = (Offset + Align - 1) / Align * Align;
|
|
|
|
|
2010-08-23 20:40:38 +00:00
|
|
|
int64_t LocalOffset = StackGrowsDown ? -Offset : Offset;
|
2010-08-14 00:15:52 +00:00
|
|
|
DEBUG(dbgs() << "Allocate FI(" << FrameIdx << ") to local offset "
|
2010-08-23 20:40:38 +00:00
|
|
|
<< LocalOffset << "\n");
|
2010-08-18 22:44:49 +00:00
|
|
|
// Keep the offset available for base register allocation
|
2010-08-23 20:40:38 +00:00
|
|
|
LocalOffsets[FrameIdx] = LocalOffset;
|
2010-08-18 22:44:49 +00:00
|
|
|
// And tell MFI about it for PEI to use later
|
2010-08-23 20:40:38 +00:00
|
|
|
MFI->mapLocalFrameObject(FrameIdx, LocalOffset);
|
|
|
|
|
|
|
|
if (!StackGrowsDown)
|
|
|
|
Offset += MFI->getObjectSize(FrameIdx);
|
2010-08-14 00:15:52 +00:00
|
|
|
|
|
|
|
++NumAllocations;
|
|
|
|
}
|
|
|
|
|
2013-12-19 03:17:11 +00:00
|
|
|
/// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
|
|
|
|
/// those required to be close to the Stack Protector) to stack offsets.
|
|
|
|
void LocalStackSlotPass::AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
|
|
|
|
SmallSet<int, 16> &ProtectedObjs,
|
|
|
|
MachineFrameInfo *MFI,
|
|
|
|
bool StackGrowsDown, int64_t &Offset,
|
|
|
|
unsigned &MaxAlign) {
|
|
|
|
|
|
|
|
for (StackObjSet::const_iterator I = UnassignedObjs.begin(),
|
|
|
|
E = UnassignedObjs.end(); I != E; ++I) {
|
|
|
|
int i = *I;
|
|
|
|
AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign);
|
|
|
|
ProtectedObjs.insert(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-14 00:15:52 +00: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...
|
|
|
|
MachineFrameInfo *MFI = Fn.getFrameInfo();
|
2011-01-10 12:39:04 +00:00
|
|
|
const TargetFrameLowering &TFI = *Fn.getTarget().getFrameLowering();
|
2010-08-23 20:40:38 +00:00
|
|
|
bool StackGrowsDown =
|
2011-01-10 12:39:04 +00:00
|
|
|
TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
|
2010-08-14 00:15:52 +00:00
|
|
|
int64_t Offset = 0;
|
2010-08-16 22:30:41 +00:00
|
|
|
unsigned MaxAlign = 0;
|
2013-12-19 03:17:11 +00:00
|
|
|
StackProtector *SP = &getAnalysis<StackProtector>();
|
2010-08-14 00:15:52 +00:00
|
|
|
|
|
|
|
// Make sure that the stack protector comes before the local variables on the
|
|
|
|
// stack.
|
2013-12-19 03:17:11 +00:00
|
|
|
SmallSet<int, 16> ProtectedObjs;
|
2010-08-14 00:15:52 +00:00
|
|
|
if (MFI->getStackProtectorIndex() >= 0) {
|
2013-12-19 03:17:11 +00:00
|
|
|
StackObjSet LargeArrayObjs;
|
2014-02-01 01:36:16 +00:00
|
|
|
StackObjSet SmallArrayObjs;
|
|
|
|
StackObjSet AddrOfObjs;
|
|
|
|
|
2010-08-23 20:40:38 +00:00
|
|
|
AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), Offset,
|
|
|
|
StackGrowsDown, MaxAlign);
|
2010-08-14 00:15:52 +00:00
|
|
|
|
|
|
|
// Assign large stack objects first.
|
|
|
|
for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
|
|
|
|
if (MFI->isDeadObjectIndex(i))
|
|
|
|
continue;
|
|
|
|
if (MFI->getStackProtectorIndex() == (int)i)
|
|
|
|
continue;
|
|
|
|
|
2013-12-19 03:17:11 +00:00
|
|
|
switch (SP->getSSPLayout(MFI->getObjectAllocation(i))) {
|
|
|
|
case StackProtector::SSPLK_None:
|
2014-02-01 01:36:16 +00:00
|
|
|
continue;
|
2013-12-19 03:17:11 +00:00
|
|
|
case StackProtector::SSPLK_SmallArray:
|
2014-02-01 01:36:16 +00:00
|
|
|
SmallArrayObjs.insert(i);
|
|
|
|
continue;
|
2013-12-19 03:17:11 +00:00
|
|
|
case StackProtector::SSPLK_AddrOf:
|
2014-02-01 01:36:16 +00:00
|
|
|
AddrOfObjs.insert(i);
|
2013-12-19 03:17:11 +00:00
|
|
|
continue;
|
|
|
|
case StackProtector::SSPLK_LargeArray:
|
|
|
|
LargeArrayObjs.insert(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unexpected SSPLayoutKind.");
|
2010-08-14 00:15:52 +00:00
|
|
|
}
|
2013-12-19 03:17:11 +00:00
|
|
|
|
|
|
|
AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
|
|
|
|
Offset, MaxAlign);
|
2014-02-01 01:36:16 +00:00
|
|
|
AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
|
|
|
|
Offset, MaxAlign);
|
|
|
|
AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
|
|
|
|
Offset, MaxAlign);
|
2010-08-14 00:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then assign frame offsets to stack objects that are not used to spill
|
|
|
|
// callee saved registers.
|
|
|
|
for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
|
|
|
|
if (MFI->isDeadObjectIndex(i))
|
|
|
|
continue;
|
|
|
|
if (MFI->getStackProtectorIndex() == (int)i)
|
|
|
|
continue;
|
2013-12-19 03:17:11 +00:00
|
|
|
if (ProtectedObjs.count(i))
|
2010-08-14 00:15:52 +00:00
|
|
|
continue;
|
|
|
|
|
2010-08-23 20:40:38 +00:00
|
|
|
AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign);
|
2010-08-14 00:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remember how big this blob of stack space is
|
2010-08-16 18:06:15 +00:00
|
|
|
MFI->setLocalFrameSize(Offset);
|
2010-08-16 22:30:41 +00:00
|
|
|
MFI->setLocalFrameMaxAlign(MaxAlign);
|
2010-08-14 00:15:52 +00:00
|
|
|
}
|
2010-08-17 18:13:53 +00:00
|
|
|
|
2010-08-18 17:57:37 +00:00
|
|
|
static inline bool
|
2013-04-30 20:04:37 +00:00
|
|
|
lookupCandidateBaseReg(int64_t BaseOffset,
|
2010-08-23 20:40:38 +00:00
|
|
|
int64_t FrameSizeAdjust,
|
2010-08-18 22:44:49 +00:00
|
|
|
int64_t LocalFrameOffset,
|
2010-08-18 17:57:37 +00:00
|
|
|
const MachineInstr *MI,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
2013-04-30 20:04:37 +00: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;
|
|
|
|
return TRI->isFrameOffsetLegal(MI, Offset);
|
2010-08-18 17:57:37 +00:00
|
|
|
}
|
|
|
|
|
2010-08-20 16:48:30 +00:00
|
|
|
bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) {
|
2010-08-17 18:13:53 +00: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-20 16:48:30 +00:00
|
|
|
bool UsedBaseReg = false;
|
2010-08-17 18:13:53 +00:00
|
|
|
|
|
|
|
MachineFrameInfo *MFI = Fn.getFrameInfo();
|
|
|
|
const TargetRegisterInfo *TRI = Fn.getTarget().getRegisterInfo();
|
2011-01-10 12:39:04 +00:00
|
|
|
const TargetFrameLowering &TFI = *Fn.getTarget().getFrameLowering();
|
2010-08-20 20:25:31 +00:00
|
|
|
bool StackGrowsDown =
|
2011-01-10 12:39:04 +00:00
|
|
|
TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
|
2010-08-17 18:13:53 +00:00
|
|
|
|
2010-08-31 17:58:19 +00: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-18 22:44:49 +00:00
|
|
|
|
2010-08-31 17:58:19 +00:00
|
|
|
for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
|
2010-08-17 18:13:53 +00:00
|
|
|
for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
|
|
|
|
MachineInstr *MI = I;
|
2010-12-17 23:09:14 +00:00
|
|
|
|
2013-11-29 06:35:30 +00:00
|
|
|
// Debug value, stackmap and patchpoint instructions can't be out of
|
|
|
|
// range, so they don't need any updates.
|
|
|
|
if (MI->isDebugValue() ||
|
|
|
|
MI->getOpcode() == TargetOpcode::STACKMAP ||
|
|
|
|
MI->getOpcode() == TargetOpcode::PATCHPOINT)
|
2010-08-17 22:41:55 +00:00
|
|
|
continue;
|
2010-12-17 23:09:14 +00:00
|
|
|
|
2010-08-17 18:13:53 +00: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.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
// Consider replacing all frame index operands that reference
|
|
|
|
// an object allocated in the local block.
|
2010-08-17 22:41:55 +00:00
|
|
|
if (MI->getOperand(i).isFI()) {
|
|
|
|
// Don't try this with values not in the local block.
|
2010-08-31 17:58:19 +00:00
|
|
|
if (!MFI->isObjectPreAllocated(MI->getOperand(i).getIndex()))
|
|
|
|
break;
|
2013-04-30 20:04:37 +00:00
|
|
|
int Idx = MI->getOperand(i).getIndex();
|
|
|
|
int64_t LocalOffset = LocalOffsets[Idx];
|
|
|
|
if (!TRI->needsFrameBaseReg(MI, LocalOffset))
|
|
|
|
break;
|
2010-08-31 17:58:19 +00:00
|
|
|
FrameReferenceInsns.
|
2013-04-30 20:04:37 +00:00
|
|
|
push_back(FrameRef(MI, LocalOffset, Idx));
|
2010-08-31 17:58:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-17 23:09:14 +00:00
|
|
|
|
2010-08-31 17:58:19 +00:00
|
|
|
// Sort the frame references by local offset
|
|
|
|
array_pod_sort(FrameReferenceInsns.begin(), FrameReferenceInsns.end());
|
|
|
|
|
2010-12-17 23:09:14 +00:00
|
|
|
MachineBasicBlock *Entry = Fn.begin();
|
2010-08-31 17:58:19 +00:00
|
|
|
|
2013-04-30 20:04:37 +00:00
|
|
|
unsigned BaseReg = 0;
|
|
|
|
int64_t BaseOffset = 0;
|
|
|
|
|
2010-12-17 23:09:14 +00:00
|
|
|
// Loop through the frame references and allocate for them as necessary.
|
2010-08-31 17:58:19 +00:00
|
|
|
for (int ref = 0, e = FrameReferenceInsns.size(); ref < e ; ++ref) {
|
2013-04-30 20:04:37 +00:00
|
|
|
FrameRef &FR = FrameReferenceInsns[ref];
|
|
|
|
MachineBasicBlock::iterator I = FR.getMachineInstr();
|
2010-08-31 17:58:19 +00:00
|
|
|
MachineInstr *MI = I;
|
2013-04-30 20:04:37 +00:00
|
|
|
int64_t LocalOffset = FR.getLocalOffset();
|
|
|
|
int FrameIdx = FR.getFrameIndex();
|
|
|
|
assert(MFI->isObjectPreAllocated(FrameIdx) &&
|
|
|
|
"Only pre-allocated locals expected!");
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Considering: " << *MI);
|
|
|
|
|
|
|
|
unsigned idx = 0;
|
|
|
|
for (unsigned f = MI->getNumOperands(); idx != f; ++idx) {
|
|
|
|
if (!MI->getOperand(idx).isFI())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (FrameIdx == I->getOperand(idx).getIndex())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(idx < MI->getNumOperands() && "Cannot find FI operand");
|
|
|
|
|
|
|
|
int64_t Offset = 0;
|
|
|
|
int64_t FrameSizeAdjust = StackGrowsDown ? MFI->getLocalFrameSize() : 0;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << " Replacing FI in: " << *MI);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (UsedBaseReg && lookupCandidateBaseReg(BaseOffset, FrameSizeAdjust,
|
|
|
|
LocalOffset, MI, TRI)) {
|
|
|
|
DEBUG(dbgs() << " Reusing base register " << BaseReg << "\n");
|
|
|
|
// We found a register to reuse.
|
|
|
|
Offset = FrameSizeAdjust + LocalOffset - BaseOffset;
|
|
|
|
} else {
|
|
|
|
// No previously defined register was in range, so create a // new one.
|
|
|
|
|
|
|
|
int64_t InstrOffset = TRI->getFrameIndexInstrOffset(MI, idx);
|
|
|
|
|
|
|
|
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 13:34:21 +00:00
|
|
|
if (ref + 1 >= e ||
|
|
|
|
!lookupCandidateBaseReg(
|
|
|
|
BaseOffset, FrameSizeAdjust,
|
|
|
|
FrameReferenceInsns[ref + 1].getLocalOffset(),
|
|
|
|
FrameReferenceInsns[ref + 1].getMachineInstr(), TRI)) {
|
2013-04-30 20:04:37 +00:00
|
|
|
BaseOffset = PrevBaseOffset;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MachineFunction *MF = MI->getParent()->getParent();
|
|
|
|
const TargetRegisterClass *RC = TRI->getPointerRegClass(*MF);
|
|
|
|
BaseReg = Fn.getRegInfo().createVirtualRegister(RC);
|
|
|
|
|
|
|
|
DEBUG(dbgs() << " Materializing base register " << BaseReg <<
|
|
|
|
" at frame local offset " << LocalOffset + InstrOffset << "\n");
|
|
|
|
|
|
|
|
// Tell the target to insert the instruction to initialize
|
|
|
|
// the base register.
|
|
|
|
// MachineBasicBlock::iterator InsertionPt = Entry->begin();
|
|
|
|
TRI->materializeFrameBaseRegister(Entry, BaseReg, FrameIdx,
|
|
|
|
InstrOffset);
|
|
|
|
|
|
|
|
// 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-17 18:13:53 +00:00
|
|
|
}
|
2013-04-30 20:04:37 +00: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.
|
2014-04-02 19:28:18 +00:00
|
|
|
TRI->resolveFrameIndex(*I, BaseReg, Offset);
|
2013-04-30 20:04:37 +00:00
|
|
|
DEBUG(dbgs() << "Resolved: " << *MI);
|
|
|
|
|
|
|
|
++NumReplacements;
|
2010-08-17 18:13:53 +00:00
|
|
|
}
|
2013-04-30 20:04:37 +00:00
|
|
|
|
2010-08-20 16:48:30 +00:00
|
|
|
return UsedBaseReg;
|
2010-08-17 18:13:53 +00:00
|
|
|
}
|