2009-05-18 19:03:16 +00:00
|
|
|
//===-- llvm/CodeGen/Spiller.cpp - Spiller -------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "spiller"
|
|
|
|
|
|
|
|
#include "Spiller.h"
|
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2012-04-02 22:44:18 +00:00
|
|
|
#include "llvm/CodeGen/LiveRangeEdit.h"
|
2010-10-26 00:11:33 +00:00
|
|
|
#include "llvm/CodeGen/LiveStackAnalysis.h"
|
2009-08-22 20:54:03 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2009-05-18 19:03:16 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2010-07-10 22:42:59 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2010-07-20 23:50:15 +00:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2009-05-18 19:03:16 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2012-11-28 19:13:06 +00:00
|
|
|
#include "llvm/CodeGen/VirtRegMap.h"
|
2009-11-19 04:15:33 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2009-05-18 19:03:16 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2010-06-25 22:53:05 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-22 20:54:03 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2009-05-18 19:03:16 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2009-11-19 04:15:33 +00:00
|
|
|
namespace {
|
2011-11-12 23:29:02 +00:00
|
|
|
enum SpillerName { trivial, inline_ };
|
2009-11-19 04:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static cl::opt<SpillerName>
|
|
|
|
spillerOpt("spiller",
|
|
|
|
cl::desc("Spiller to use: (default: standard)"),
|
|
|
|
cl::Prefix,
|
2009-12-09 05:39:12 +00:00
|
|
|
cl::values(clEnumVal(trivial, "trivial spiller"),
|
2010-06-30 00:24:51 +00:00
|
|
|
clEnumValN(inline_, "inline", "inline spiller"),
|
2009-11-19 04:15:33 +00:00
|
|
|
clEnumValEnd),
|
2011-11-12 23:29:02 +00:00
|
|
|
cl::init(trivial));
|
2009-11-19 04:15:33 +00:00
|
|
|
|
2009-12-09 05:39:12 +00:00
|
|
|
// Spiller virtual destructor implementation.
|
2009-05-18 19:03:16 +00:00
|
|
|
Spiller::~Spiller() {}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-06-02 16:53:25 +00:00
|
|
|
/// Utility class for spillers.
|
|
|
|
class SpillerBase : public Spiller {
|
|
|
|
protected:
|
2010-07-20 23:50:15 +00:00
|
|
|
MachineFunctionPass *pass;
|
2009-06-02 16:53:25 +00:00
|
|
|
MachineFunction *mf;
|
2010-07-20 23:50:15 +00:00
|
|
|
VirtRegMap *vrm;
|
2009-06-02 16:53:25 +00:00
|
|
|
LiveIntervals *lis;
|
|
|
|
MachineFrameInfo *mfi;
|
|
|
|
MachineRegisterInfo *mri;
|
|
|
|
const TargetInstrInfo *tii;
|
2010-05-06 19:06:44 +00:00
|
|
|
const TargetRegisterInfo *tri;
|
2010-07-06 18:35:20 +00:00
|
|
|
|
|
|
|
/// Construct a spiller base.
|
2010-07-20 23:50:15 +00:00
|
|
|
SpillerBase(MachineFunctionPass &pass, MachineFunction &mf, VirtRegMap &vrm)
|
|
|
|
: pass(&pass), mf(&mf), vrm(&vrm)
|
2009-05-18 19:03:16 +00:00
|
|
|
{
|
2010-07-20 23:50:15 +00:00
|
|
|
lis = &pass.getAnalysis<LiveIntervals>();
|
|
|
|
mfi = mf.getFrameInfo();
|
|
|
|
mri = &mf.getRegInfo();
|
|
|
|
tii = mf.getTarget().getInstrInfo();
|
|
|
|
tri = mf.getTarget().getRegisterInfo();
|
2009-05-18 19:03:16 +00:00
|
|
|
}
|
|
|
|
|
2009-06-02 16:53:25 +00:00
|
|
|
/// Add spill ranges for every use/def of the live interval, inserting loads
|
2009-11-18 20:31:20 +00:00
|
|
|
/// immediately before each use, and stores after each def. No folding or
|
|
|
|
/// remat is attempted.
|
2012-02-28 22:07:24 +00:00
|
|
|
void trivialSpillEverywhere(LiveRangeEdit& LRE) {
|
|
|
|
LiveInterval* li = &LRE.getParent();
|
|
|
|
|
2010-01-05 01:25:55 +00:00
|
|
|
DEBUG(dbgs() << "Spilling everywhere " << *li << "\n");
|
2009-05-18 19:03:16 +00:00
|
|
|
|
2013-11-13 00:15:44 +00:00
|
|
|
assert(li->weight != llvm::huge_valf &&
|
2009-05-18 19:03:16 +00:00
|
|
|
"Attempting to spill already spilled value.");
|
|
|
|
|
2011-01-09 21:17:37 +00:00
|
|
|
assert(!TargetRegisterInfo::isStackSlot(li->reg) &&
|
2009-05-18 19:03:16 +00:00
|
|
|
"Trying to spill a stack slot.");
|
|
|
|
|
2010-01-05 01:25:55 +00:00
|
|
|
DEBUG(dbgs() << "Trivial spill everywhere of reg" << li->reg << "\n");
|
2009-06-24 20:46:24 +00:00
|
|
|
|
2009-05-18 19:03:16 +00:00
|
|
|
const TargetRegisterClass *trc = mri->getRegClass(li->reg);
|
|
|
|
unsigned ss = vrm->assignVirt2StackSlot(li->reg);
|
|
|
|
|
2009-11-18 20:31:20 +00:00
|
|
|
// Iterate over reg uses/defs.
|
2014-03-13 23:12:04 +00:00
|
|
|
for (MachineRegisterInfo::reg_instr_iterator
|
|
|
|
regItr = mri->reg_instr_begin(li->reg);
|
|
|
|
regItr != mri->reg_instr_end();) {
|
2009-05-18 19:03:16 +00:00
|
|
|
|
2009-11-18 20:31:20 +00:00
|
|
|
// Grab the use/def instr.
|
2009-05-18 19:03:16 +00:00
|
|
|
MachineInstr *mi = &*regItr;
|
2009-06-24 20:46:24 +00:00
|
|
|
|
2010-01-05 01:25:55 +00:00
|
|
|
DEBUG(dbgs() << " Processing " << *mi);
|
2009-06-24 20:46:24 +00:00
|
|
|
|
2009-11-18 20:31:20 +00:00
|
|
|
// Step regItr to the next use/def instr.
|
2014-03-13 23:12:04 +00:00
|
|
|
++regItr;
|
2010-07-06 18:35:20 +00:00
|
|
|
|
2009-11-18 20:31:20 +00:00
|
|
|
// Collect uses & defs for this instr.
|
2009-05-18 19:03:16 +00:00
|
|
|
SmallVector<unsigned, 2> indices;
|
|
|
|
bool hasUse = false;
|
|
|
|
bool hasDef = false;
|
|
|
|
for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
|
|
|
|
MachineOperand &op = mi->getOperand(i);
|
|
|
|
if (!op.isReg() || op.getReg() != li->reg)
|
|
|
|
continue;
|
|
|
|
hasUse |= mi->getOperand(i).isUse();
|
|
|
|
hasDef |= mi->getOperand(i).isDef();
|
|
|
|
indices.push_back(i);
|
|
|
|
}
|
|
|
|
|
2013-08-14 23:50:16 +00:00
|
|
|
// Create a new virtual register for the load and/or store.
|
|
|
|
unsigned NewVReg = LRE.create();
|
2010-07-06 18:35:20 +00:00
|
|
|
|
2009-11-18 20:31:20 +00:00
|
|
|
// Update the reg operands & kill flags.
|
2009-05-18 19:03:16 +00:00
|
|
|
for (unsigned i = 0; i < indices.size(); ++i) {
|
2009-11-18 20:31:20 +00:00
|
|
|
unsigned mopIdx = indices[i];
|
|
|
|
MachineOperand &mop = mi->getOperand(mopIdx);
|
2013-08-14 23:50:16 +00:00
|
|
|
mop.setReg(NewVReg);
|
2009-11-18 20:31:20 +00:00
|
|
|
if (mop.isUse() && !mi->isRegTiedToDefOperand(mopIdx)) {
|
|
|
|
mop.setIsKill(true);
|
2009-05-18 19:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-02 16:53:25 +00:00
|
|
|
assert(hasUse || hasDef);
|
|
|
|
|
2009-11-18 20:31:20 +00:00
|
|
|
// Insert reload if necessary.
|
|
|
|
MachineBasicBlock::iterator miItr(mi);
|
2009-05-18 19:03:16 +00:00
|
|
|
if (hasUse) {
|
2013-08-14 23:50:16 +00:00
|
|
|
MachineInstrSpan MIS(miItr);
|
|
|
|
|
|
|
|
tii->loadRegFromStackSlot(*mi->getParent(), miItr, NewVReg, ss, trc,
|
2010-05-06 19:06:44 +00:00
|
|
|
tri);
|
2013-08-14 23:50:16 +00:00
|
|
|
lis->InsertMachineInstrRangeInMaps(MIS.begin(), miItr);
|
2009-05-18 19:03:16 +00:00
|
|
|
}
|
|
|
|
|
2009-11-18 20:31:20 +00:00
|
|
|
// Insert store if necessary.
|
2009-05-18 19:03:16 +00:00
|
|
|
if (hasDef) {
|
2013-08-14 23:50:16 +00:00
|
|
|
MachineInstrSpan MIS(miItr);
|
|
|
|
|
2014-03-02 12:27:27 +00:00
|
|
|
tii->storeRegToStackSlot(*mi->getParent(), std::next(miItr), NewVReg,
|
2010-05-06 19:06:44 +00:00
|
|
|
true, ss, trc, tri);
|
2014-03-02 12:27:27 +00:00
|
|
|
lis->InsertMachineInstrRangeInMaps(std::next(miItr), MIS.end());
|
2009-05-18 19:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-02 16:53:25 +00:00
|
|
|
};
|
2009-05-18 19:03:16 +00:00
|
|
|
|
2010-04-07 22:44:07 +00:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace {
|
2009-05-18 19:03:16 +00:00
|
|
|
|
2009-06-02 16:53:25 +00:00
|
|
|
/// Spills any live range using the spill-everywhere method with no attempt at
|
|
|
|
/// folding.
|
|
|
|
class TrivialSpiller : public SpillerBase {
|
|
|
|
public:
|
2009-06-19 02:17:53 +00:00
|
|
|
|
2010-07-20 23:50:15 +00:00
|
|
|
TrivialSpiller(MachineFunctionPass &pass, MachineFunction &mf,
|
|
|
|
VirtRegMap &vrm)
|
|
|
|
: SpillerBase(pass, mf, vrm) {}
|
2009-05-18 19:03:16 +00:00
|
|
|
|
2014-03-07 09:26:03 +00:00
|
|
|
void spill(LiveRangeEdit &LRE) override {
|
2009-11-19 04:15:33 +00:00
|
|
|
// Ignore spillIs - we don't use it.
|
2012-02-28 22:07:24 +00:00
|
|
|
trivialSpillEverywhere(LRE);
|
2009-05-18 19:03:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-04-07 22:44:07 +00:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-12-20 02:50:00 +00:00
|
|
|
void Spiller::anchor() { }
|
|
|
|
|
2010-07-20 23:50:15 +00:00
|
|
|
llvm::Spiller* llvm::createSpiller(MachineFunctionPass &pass,
|
|
|
|
MachineFunction &mf,
|
|
|
|
VirtRegMap &vrm) {
|
2009-11-19 04:15:33 +00:00
|
|
|
switch (spillerOpt) {
|
2010-07-20 23:50:15 +00:00
|
|
|
case trivial: return new TrivialSpiller(pass, mf, vrm);
|
|
|
|
case inline_: return createInlineSpiller(pass, mf, vrm);
|
2009-11-19 04:15:33 +00:00
|
|
|
}
|
2012-01-10 18:08:01 +00:00
|
|
|
llvm_unreachable("Invalid spiller optimization");
|
2009-05-18 19:03:16 +00:00
|
|
|
}
|