2010-06-30 01:58:39 +02:00
|
|
|
//===-------- InlineSpiller.cpp - Insert spills and restores inline -------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The inline spiller modifies the machine function directly instead of
|
|
|
|
// inserting spills and restores in VirtRegMap.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-03 21:39:23 +01:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
2010-06-30 01:58:39 +02:00
|
|
|
#include "Spiller.h"
|
2010-10-15 01:49:52 +02:00
|
|
|
#include "LiveRangeEdit.h"
|
2010-07-20 17:41:07 +02:00
|
|
|
#include "SplitKit.h"
|
2010-06-30 01:58:39 +02:00
|
|
|
#include "VirtRegMap.h"
|
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2010-10-26 02:11:35 +02:00
|
|
|
#include "llvm/CodeGen/LiveStackAnalysis.h"
|
2010-10-28 22:34:50 +02:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
2010-06-30 01:58:39 +02:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2010-07-19 20:41:20 +02:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2010-06-30 01:58:39 +02:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2010-10-28 22:34:47 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-06-30 01:58:39 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-10-28 22:34:47 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
VerifySpills("verify-spills", cl::desc("Verify after each spill/split"));
|
|
|
|
|
2010-11-04 01:32:32 +01:00
|
|
|
static cl::opt<bool>
|
|
|
|
ExtraSpillerSplits("extra-spiller-splits",
|
|
|
|
cl::desc("Enable additional splitting during splitting"));
|
|
|
|
|
2010-06-30 01:58:39 +02:00
|
|
|
namespace {
|
|
|
|
class InlineSpiller : public Spiller {
|
2010-08-06 20:47:06 +02:00
|
|
|
MachineFunctionPass &pass_;
|
2010-06-30 01:58:39 +02:00
|
|
|
MachineFunction &mf_;
|
|
|
|
LiveIntervals &lis_;
|
2010-10-26 02:11:35 +02:00
|
|
|
LiveStacks &lss_;
|
2010-10-28 22:34:50 +02:00
|
|
|
MachineDominatorTree &mdt_;
|
2010-07-19 20:41:20 +02:00
|
|
|
MachineLoopInfo &loops_;
|
2010-06-30 01:58:39 +02:00
|
|
|
VirtRegMap &vrm_;
|
|
|
|
MachineFrameInfo &mfi_;
|
|
|
|
MachineRegisterInfo &mri_;
|
|
|
|
const TargetInstrInfo &tii_;
|
|
|
|
const TargetRegisterInfo &tri_;
|
2010-07-01 01:03:52 +02:00
|
|
|
const BitVector reserved_;
|
|
|
|
|
2010-07-20 17:41:07 +02:00
|
|
|
SplitAnalysis splitAnalysis_;
|
|
|
|
|
2010-07-01 01:03:52 +02:00
|
|
|
// Variables that are valid during spill(), but used by multiple methods.
|
2010-10-15 01:49:52 +02:00
|
|
|
LiveRangeEdit *edit_;
|
2010-07-01 01:03:52 +02:00
|
|
|
const TargetRegisterClass *rc_;
|
|
|
|
int stackSlot_;
|
2010-06-30 01:58:39 +02:00
|
|
|
|
2010-10-21 00:00:51 +02:00
|
|
|
// Values that failed to remat at some point.
|
2010-07-02 19:44:57 +02:00
|
|
|
SmallPtrSet<VNInfo*, 8> usedValues_;
|
|
|
|
|
2010-06-30 01:58:39 +02:00
|
|
|
~InlineSpiller() {}
|
|
|
|
|
|
|
|
public:
|
2010-07-21 01:50:15 +02:00
|
|
|
InlineSpiller(MachineFunctionPass &pass,
|
|
|
|
MachineFunction &mf,
|
|
|
|
VirtRegMap &vrm)
|
2010-08-06 20:47:06 +02:00
|
|
|
: pass_(pass),
|
|
|
|
mf_(mf),
|
2010-07-21 01:50:15 +02:00
|
|
|
lis_(pass.getAnalysis<LiveIntervals>()),
|
2010-10-26 02:11:35 +02:00
|
|
|
lss_(pass.getAnalysis<LiveStacks>()),
|
2010-10-28 22:34:50 +02:00
|
|
|
mdt_(pass.getAnalysis<MachineDominatorTree>()),
|
2010-07-21 01:50:15 +02:00
|
|
|
loops_(pass.getAnalysis<MachineLoopInfo>()),
|
|
|
|
vrm_(vrm),
|
|
|
|
mfi_(*mf.getFrameInfo()),
|
|
|
|
mri_(mf.getRegInfo()),
|
|
|
|
tii_(*mf.getTarget().getInstrInfo()),
|
|
|
|
tri_(*mf.getTarget().getRegisterInfo()),
|
2010-07-20 17:41:07 +02:00
|
|
|
reserved_(tri_.getReservedRegs(mf_)),
|
2010-07-21 01:50:15 +02:00
|
|
|
splitAnalysis_(mf, lis_, loops_) {}
|
2010-06-30 01:58:39 +02:00
|
|
|
|
|
|
|
void spill(LiveInterval *li,
|
2010-08-14 00:56:53 +02:00
|
|
|
SmallVectorImpl<LiveInterval*> &newIntervals,
|
|
|
|
SmallVectorImpl<LiveInterval*> &spillIs);
|
2010-07-02 19:44:57 +02:00
|
|
|
|
2010-10-15 01:49:52 +02:00
|
|
|
void spill(LiveRangeEdit &);
|
|
|
|
|
2010-07-02 19:44:57 +02:00
|
|
|
private:
|
2010-07-20 17:41:07 +02:00
|
|
|
bool split();
|
|
|
|
|
2010-07-02 19:44:57 +02:00
|
|
|
bool reMaterializeFor(MachineBasicBlock::iterator MI);
|
|
|
|
void reMaterializeAll();
|
|
|
|
|
2010-08-05 00:35:11 +02:00
|
|
|
bool coalesceStackAccess(MachineInstr *MI);
|
2010-07-01 02:13:04 +02:00
|
|
|
bool foldMemoryOperand(MachineBasicBlock::iterator MI,
|
|
|
|
const SmallVectorImpl<unsigned> &Ops);
|
2010-07-01 01:03:52 +02:00
|
|
|
void insertReload(LiveInterval &NewLI, MachineBasicBlock::iterator MI);
|
|
|
|
void insertSpill(LiveInterval &NewLI, MachineBasicBlock::iterator MI);
|
2010-06-30 01:58:39 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
2010-07-21 01:50:15 +02:00
|
|
|
Spiller *createInlineSpiller(MachineFunctionPass &pass,
|
|
|
|
MachineFunction &mf,
|
|
|
|
VirtRegMap &vrm) {
|
2010-10-29 02:40:55 +02:00
|
|
|
if (VerifySpills)
|
|
|
|
mf.verify(&pass);
|
2010-07-21 01:50:15 +02:00
|
|
|
return new InlineSpiller(pass, mf, vrm);
|
2010-06-30 01:58:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-20 17:41:07 +02:00
|
|
|
/// split - try splitting the current interval into pieces that may allocate
|
|
|
|
/// separately. Return true if successful.
|
|
|
|
bool InlineSpiller::split() {
|
2010-10-15 01:49:52 +02:00
|
|
|
splitAnalysis_.analyze(&edit_->getParent());
|
2010-07-20 17:41:07 +02:00
|
|
|
|
2010-10-06 00:19:33 +02:00
|
|
|
// Try splitting around loops.
|
2010-11-04 01:32:32 +01:00
|
|
|
if (ExtraSpillerSplits) {
|
|
|
|
const MachineLoop *loop = splitAnalysis_.getBestSplitLoop();
|
|
|
|
if (loop) {
|
|
|
|
SplitEditor(splitAnalysis_, lis_, vrm_, mdt_, *edit_)
|
|
|
|
.splitAroundLoop(loop);
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-20 17:41:07 +02:00
|
|
|
}
|
2010-08-12 19:07:14 +02:00
|
|
|
|
|
|
|
// Try splitting into single block intervals.
|
|
|
|
SplitAnalysis::BlockPtrSet blocks;
|
|
|
|
if (splitAnalysis_.getMultiUseBlocks(blocks)) {
|
2010-10-28 22:34:50 +02:00
|
|
|
SplitEditor(splitAnalysis_, lis_, vrm_, mdt_, *edit_)
|
2010-10-06 00:19:33 +02:00
|
|
|
.splitSingleBlocks(blocks);
|
|
|
|
return true;
|
2010-08-12 19:07:14 +02:00
|
|
|
}
|
|
|
|
|
2010-08-13 23:18:48 +02:00
|
|
|
// Try splitting inside a basic block.
|
2010-11-04 01:32:32 +01:00
|
|
|
if (ExtraSpillerSplits) {
|
|
|
|
const MachineBasicBlock *MBB = splitAnalysis_.getBlockForInsideSplit();
|
|
|
|
if (MBB){
|
|
|
|
SplitEditor(splitAnalysis_, lis_, vrm_, mdt_, *edit_)
|
|
|
|
.splitInsideBlock(MBB);
|
|
|
|
return true;
|
|
|
|
}
|
2010-08-13 23:18:48 +02:00
|
|
|
}
|
|
|
|
|
2010-07-20 17:41:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-15 01:49:52 +02:00
|
|
|
/// reMaterializeFor - Attempt to rematerialize edit_->getReg() before MI instead of
|
2010-07-01 01:03:52 +02:00
|
|
|
/// reloading it.
|
2010-07-02 19:44:57 +02:00
|
|
|
bool InlineSpiller::reMaterializeFor(MachineBasicBlock::iterator MI) {
|
2010-07-01 01:03:52 +02:00
|
|
|
SlotIndex UseIdx = lis_.getInstructionIndex(MI).getUseIndex();
|
2010-10-15 01:49:52 +02:00
|
|
|
VNInfo *OrigVNI = edit_->getParent().getVNInfoAt(UseIdx);
|
2010-10-21 00:00:51 +02:00
|
|
|
|
2010-07-02 19:44:57 +02:00
|
|
|
if (!OrigVNI) {
|
|
|
|
DEBUG(dbgs() << "\tadding <undef> flags: ");
|
2010-07-01 01:03:52 +02:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
2010-10-15 01:49:52 +02:00
|
|
|
if (MO.isReg() && MO.isUse() && MO.getReg() == edit_->getReg())
|
2010-07-01 01:03:52 +02:00
|
|
|
MO.setIsUndef();
|
|
|
|
}
|
2010-07-02 19:44:57 +02:00
|
|
|
DEBUG(dbgs() << UseIdx << '\t' << *MI);
|
2010-07-01 01:03:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-21 00:00:51 +02:00
|
|
|
|
|
|
|
LiveRangeEdit::Remat RM = edit_->canRematerializeAt(OrigVNI, UseIdx, false,
|
|
|
|
lis_);
|
|
|
|
if (!RM) {
|
2010-07-02 19:44:57 +02:00
|
|
|
usedValues_.insert(OrigVNI);
|
|
|
|
DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
|
2010-07-01 01:03:52 +02:00
|
|
|
return false;
|
2010-07-02 19:44:57 +02:00
|
|
|
}
|
2010-07-01 01:03:52 +02:00
|
|
|
|
2010-10-21 00:00:51 +02:00
|
|
|
// If the instruction also writes edit_->getReg(), it had better not require
|
|
|
|
// the same register for uses and defs.
|
2010-07-02 19:44:57 +02:00
|
|
|
bool Reads, Writes;
|
|
|
|
SmallVector<unsigned, 8> Ops;
|
2010-10-15 01:49:52 +02:00
|
|
|
tie(Reads, Writes) = MI->readsWritesVirtualRegister(edit_->getReg(), &Ops);
|
2010-07-02 19:44:57 +02:00
|
|
|
if (Writes) {
|
|
|
|
for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(Ops[i]);
|
|
|
|
if (MO.isUse() ? MI->isRegTiedToDefOperand(Ops[i]) : MO.getSubReg()) {
|
|
|
|
usedValues_.insert(OrigVNI);
|
|
|
|
DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI);
|
2010-07-01 01:03:52 +02:00
|
|
|
return false;
|
2010-07-02 19:44:57 +02:00
|
|
|
}
|
2010-07-01 01:03:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-02 19:44:57 +02:00
|
|
|
// Alocate a new register for the remat.
|
2010-10-15 01:49:52 +02:00
|
|
|
LiveInterval &NewLI = edit_->create(mri_, lis_, vrm_);
|
2010-07-02 19:44:57 +02:00
|
|
|
NewLI.markNotSpillable();
|
|
|
|
|
|
|
|
// Finally we can rematerialize OrigMI before MI.
|
2010-10-21 00:00:51 +02:00
|
|
|
SlotIndex DefIdx = edit_->rematerializeAt(*MI->getParent(), MI, NewLI.reg, RM,
|
|
|
|
lis_, tii_, tri_);
|
|
|
|
DEBUG(dbgs() << "\tremat: " << DefIdx << '\n');
|
2010-07-02 19:44:57 +02:00
|
|
|
|
|
|
|
// Replace operands
|
|
|
|
for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(Ops[i]);
|
2010-10-15 01:49:52 +02:00
|
|
|
if (MO.isReg() && MO.isUse() && MO.getReg() == edit_->getReg()) {
|
|
|
|
MO.setReg(NewLI.reg);
|
2010-07-02 19:44:57 +02:00
|
|
|
MO.setIsKill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG(dbgs() << "\t " << UseIdx << '\t' << *MI);
|
|
|
|
|
2010-09-25 14:04:16 +02:00
|
|
|
VNInfo *DefVNI = NewLI.getNextValue(DefIdx, 0, lis_.getVNInfoAllocator());
|
2010-07-01 01:03:52 +02:00
|
|
|
NewLI.addRange(LiveRange(DefIdx, UseIdx.getDefIndex(), DefVNI));
|
2010-07-02 19:44:57 +02:00
|
|
|
DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
|
2010-07-01 01:03:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-15 01:49:52 +02:00
|
|
|
/// reMaterializeAll - Try to rematerialize as many uses as possible,
|
2010-07-02 19:44:57 +02:00
|
|
|
/// and trim the live ranges after.
|
|
|
|
void InlineSpiller::reMaterializeAll() {
|
|
|
|
// Do a quick scan of the interval values to find if any are remattable.
|
2010-10-21 00:00:51 +02:00
|
|
|
if (!edit_->anyRematerializable(lis_, tii_, 0))
|
2010-07-02 19:44:57 +02:00
|
|
|
return;
|
|
|
|
|
2010-10-21 00:00:51 +02:00
|
|
|
usedValues_.clear();
|
|
|
|
|
2010-10-15 01:49:52 +02:00
|
|
|
// Try to remat before all uses of edit_->getReg().
|
2010-07-02 19:44:57 +02:00
|
|
|
bool anyRemat = false;
|
|
|
|
for (MachineRegisterInfo::use_nodbg_iterator
|
2010-10-15 01:49:52 +02:00
|
|
|
RI = mri_.use_nodbg_begin(edit_->getReg());
|
2010-07-02 19:44:57 +02:00
|
|
|
MachineInstr *MI = RI.skipInstruction();)
|
|
|
|
anyRemat |= reMaterializeFor(MI);
|
|
|
|
|
|
|
|
if (!anyRemat)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Remove any values that were completely rematted.
|
|
|
|
bool anyRemoved = false;
|
2010-10-21 00:00:51 +02:00
|
|
|
for (LiveInterval::vni_iterator I = edit_->getParent().vni_begin(),
|
|
|
|
E = edit_->getParent().vni_end(); I != E; ++I) {
|
2010-07-02 19:44:57 +02:00
|
|
|
VNInfo *VNI = *I;
|
2010-10-21 00:00:51 +02:00
|
|
|
if (VNI->hasPHIKill() || !edit_->didRematerialize(VNI) ||
|
|
|
|
usedValues_.count(VNI))
|
2010-07-02 19:44:57 +02:00
|
|
|
continue;
|
|
|
|
MachineInstr *DefMI = lis_.getInstructionFromIndex(VNI->def);
|
|
|
|
DEBUG(dbgs() << "\tremoving dead def: " << VNI->def << '\t' << *DefMI);
|
|
|
|
lis_.RemoveMachineInstrFromMaps(DefMI);
|
|
|
|
vrm_.RemoveMachineInstrFromMaps(DefMI);
|
|
|
|
DefMI->eraseFromParent();
|
2010-09-26 05:37:09 +02:00
|
|
|
VNI->def = SlotIndex();
|
2010-07-02 19:44:57 +02:00
|
|
|
anyRemoved = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!anyRemoved)
|
|
|
|
return;
|
|
|
|
|
2010-10-15 01:49:52 +02:00
|
|
|
// Removing values may cause debug uses where parent is not live.
|
|
|
|
for (MachineRegisterInfo::use_iterator RI = mri_.use_begin(edit_->getReg());
|
2010-07-02 21:54:40 +02:00
|
|
|
MachineInstr *MI = RI.skipInstruction();) {
|
|
|
|
if (!MI->isDebugValue())
|
2010-07-02 19:44:57 +02:00
|
|
|
continue;
|
2010-10-15 01:49:52 +02:00
|
|
|
// Try to preserve the debug value if parent is live immediately after it.
|
2010-07-02 21:54:40 +02:00
|
|
|
MachineBasicBlock::iterator NextMI = MI;
|
|
|
|
++NextMI;
|
|
|
|
if (NextMI != MI->getParent()->end() && !lis_.isNotInMIMap(NextMI)) {
|
2010-10-15 01:49:52 +02:00
|
|
|
SlotIndex Idx = lis_.getInstructionIndex(NextMI);
|
|
|
|
VNInfo *VNI = edit_->getParent().getVNInfoAt(Idx);
|
2010-08-10 22:45:07 +02:00
|
|
|
if (VNI && (VNI->hasPHIKill() || usedValues_.count(VNI)))
|
2010-07-02 21:54:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DEBUG(dbgs() << "Removing debug info due to remat:" << "\t" << *MI);
|
|
|
|
MI->eraseFromParent();
|
2010-07-02 19:44:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-05 00:35:11 +02:00
|
|
|
/// If MI is a load or store of stackSlot_, it can be removed.
|
|
|
|
bool InlineSpiller::coalesceStackAccess(MachineInstr *MI) {
|
|
|
|
int FI = 0;
|
|
|
|
unsigned reg;
|
|
|
|
if (!(reg = tii_.isLoadFromStackSlot(MI, FI)) &&
|
|
|
|
!(reg = tii_.isStoreToStackSlot(MI, FI)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We have a stack access. Is it the right register and slot?
|
2010-10-15 01:49:52 +02:00
|
|
|
if (reg != edit_->getReg() || FI != stackSlot_)
|
2010-08-05 00:35:11 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Coalescing stack access: " << *MI);
|
|
|
|
lis_.RemoveMachineInstrFromMaps(MI);
|
|
|
|
MI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-01 02:13:04 +02:00
|
|
|
/// foldMemoryOperand - Try folding stack slot references in Ops into MI.
|
|
|
|
/// Return true on success, and MI will be erased.
|
|
|
|
bool InlineSpiller::foldMemoryOperand(MachineBasicBlock::iterator MI,
|
|
|
|
const SmallVectorImpl<unsigned> &Ops) {
|
|
|
|
// TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
|
|
|
|
// operands.
|
|
|
|
SmallVector<unsigned, 8> FoldOps;
|
|
|
|
for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
|
|
|
|
unsigned Idx = Ops[i];
|
|
|
|
MachineOperand &MO = MI->getOperand(Idx);
|
|
|
|
if (MO.isImplicit())
|
|
|
|
continue;
|
|
|
|
// FIXME: Teach targets to deal with subregs.
|
|
|
|
if (MO.getSubReg())
|
|
|
|
return false;
|
|
|
|
// Tied use operands should not be passed to foldMemoryOperand.
|
|
|
|
if (!MI->isRegTiedToDefOperand(Idx))
|
|
|
|
FoldOps.push_back(Idx);
|
|
|
|
}
|
|
|
|
|
2010-07-09 19:29:08 +02:00
|
|
|
MachineInstr *FoldMI = tii_.foldMemoryOperand(MI, FoldOps, stackSlot_);
|
2010-07-01 02:13:04 +02:00
|
|
|
if (!FoldMI)
|
|
|
|
return false;
|
|
|
|
lis_.ReplaceMachineInstrInMaps(MI, FoldMI);
|
|
|
|
vrm_.addSpillSlotUse(stackSlot_, FoldMI);
|
2010-07-09 19:29:08 +02:00
|
|
|
MI->eraseFromParent();
|
2010-07-01 02:13:04 +02:00
|
|
|
DEBUG(dbgs() << "\tfolded: " << *FoldMI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-01 01:03:52 +02:00
|
|
|
/// insertReload - Insert a reload of NewLI.reg before MI.
|
|
|
|
void InlineSpiller::insertReload(LiveInterval &NewLI,
|
|
|
|
MachineBasicBlock::iterator MI) {
|
|
|
|
MachineBasicBlock &MBB = *MI->getParent();
|
|
|
|
SlotIndex Idx = lis_.getInstructionIndex(MI).getDefIndex();
|
|
|
|
tii_.loadRegFromStackSlot(MBB, MI, NewLI.reg, stackSlot_, rc_, &tri_);
|
|
|
|
--MI; // Point to load instruction.
|
|
|
|
SlotIndex LoadIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
|
|
|
|
vrm_.addSpillSlotUse(stackSlot_, MI);
|
|
|
|
DEBUG(dbgs() << "\treload: " << LoadIdx << '\t' << *MI);
|
2010-09-25 14:04:16 +02:00
|
|
|
VNInfo *LoadVNI = NewLI.getNextValue(LoadIdx, 0,
|
2010-07-01 01:03:52 +02:00
|
|
|
lis_.getVNInfoAllocator());
|
|
|
|
NewLI.addRange(LiveRange(LoadIdx, Idx, LoadVNI));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// insertSpill - Insert a spill of NewLI.reg after MI.
|
|
|
|
void InlineSpiller::insertSpill(LiveInterval &NewLI,
|
|
|
|
MachineBasicBlock::iterator MI) {
|
|
|
|
MachineBasicBlock &MBB = *MI->getParent();
|
|
|
|
SlotIndex Idx = lis_.getInstructionIndex(MI).getDefIndex();
|
|
|
|
tii_.storeRegToStackSlot(MBB, ++MI, NewLI.reg, true, stackSlot_, rc_, &tri_);
|
|
|
|
--MI; // Point to store instruction.
|
|
|
|
SlotIndex StoreIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
|
|
|
|
vrm_.addSpillSlotUse(stackSlot_, MI);
|
|
|
|
DEBUG(dbgs() << "\tspilled: " << StoreIdx << '\t' << *MI);
|
2010-09-25 14:04:16 +02:00
|
|
|
VNInfo *StoreVNI = NewLI.getNextValue(Idx, 0, lis_.getVNInfoAllocator());
|
2010-07-01 01:03:52 +02:00
|
|
|
NewLI.addRange(LiveRange(Idx, StoreIdx, StoreVNI));
|
|
|
|
}
|
|
|
|
|
2010-06-30 01:58:39 +02:00
|
|
|
void InlineSpiller::spill(LiveInterval *li,
|
2010-08-14 00:56:53 +02:00
|
|
|
SmallVectorImpl<LiveInterval*> &newIntervals,
|
|
|
|
SmallVectorImpl<LiveInterval*> &spillIs) {
|
2010-10-15 01:49:52 +02:00
|
|
|
LiveRangeEdit edit(*li, newIntervals, spillIs);
|
|
|
|
spill(edit);
|
2010-10-28 22:34:47 +02:00
|
|
|
if (VerifySpills)
|
|
|
|
mf_.verify(&pass_);
|
2010-10-15 01:49:52 +02:00
|
|
|
}
|
2010-06-30 01:58:39 +02:00
|
|
|
|
2010-10-15 01:49:52 +02:00
|
|
|
void InlineSpiller::spill(LiveRangeEdit &edit) {
|
|
|
|
edit_ = &edit;
|
2010-10-30 03:26:09 +02:00
|
|
|
assert(!edit.getParent().isStackSlot() && "Trying to spill a stack slot.");
|
|
|
|
DEBUG(dbgs() << "Inline spilling "
|
|
|
|
<< mri_.getRegClass(edit.getReg())->getName()
|
|
|
|
<< ':' << edit.getParent() << "\n");
|
2010-10-15 01:49:52 +02:00
|
|
|
assert(edit.getParent().isSpillable() &&
|
|
|
|
"Attempting to spill already spilled value.");
|
2010-06-30 01:58:39 +02:00
|
|
|
|
2010-07-20 17:41:07 +02:00
|
|
|
if (split())
|
|
|
|
return;
|
|
|
|
|
2010-07-02 19:44:57 +02:00
|
|
|
reMaterializeAll();
|
|
|
|
|
|
|
|
// Remat may handle everything.
|
2010-10-15 01:49:52 +02:00
|
|
|
if (edit_->getParent().empty())
|
2010-07-02 19:44:57 +02:00
|
|
|
return;
|
|
|
|
|
2010-10-15 01:49:52 +02:00
|
|
|
rc_ = mri_.getRegClass(edit.getReg());
|
2010-11-01 20:49:57 +01:00
|
|
|
stackSlot_ = vrm_.assignVirt2StackSlot(edit_->getReg());
|
2010-07-02 19:44:57 +02:00
|
|
|
|
2010-10-26 02:11:35 +02:00
|
|
|
// Update LiveStacks now that we are committed to spilling.
|
|
|
|
LiveInterval &stacklvr = lss_.getOrCreateInterval(stackSlot_, rc_);
|
2010-11-01 20:49:57 +01:00
|
|
|
assert(stacklvr.empty() && "Just created stack slot not empty");
|
|
|
|
stacklvr.getNextValue(SlotIndex(), 0, lss_.getVNInfoAllocator());
|
2010-10-26 02:11:35 +02:00
|
|
|
stacklvr.MergeRangesInAsValue(edit_->getParent(), stacklvr.getValNumInfo(0));
|
|
|
|
|
2010-07-01 01:03:52 +02:00
|
|
|
// Iterate over instructions using register.
|
2010-10-15 01:49:52 +02:00
|
|
|
for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(edit.getReg());
|
2010-06-30 01:58:39 +02:00
|
|
|
MachineInstr *MI = RI.skipInstruction();) {
|
|
|
|
|
2010-07-02 21:54:40 +02:00
|
|
|
// Debug values are not allowed to affect codegen.
|
|
|
|
if (MI->isDebugValue()) {
|
|
|
|
// Modify DBG_VALUE now that the value is in a spill slot.
|
|
|
|
uint64_t Offset = MI->getOperand(1).getImm();
|
|
|
|
const MDNode *MDPtr = MI->getOperand(2).getMetadata();
|
|
|
|
DebugLoc DL = MI->getDebugLoc();
|
|
|
|
if (MachineInstr *NewDV = tii_.emitFrameIndexDebugValue(mf_, stackSlot_,
|
|
|
|
Offset, MDPtr, DL)) {
|
|
|
|
DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
MBB->insert(MBB->erase(MI), NewDV);
|
|
|
|
} else {
|
|
|
|
DEBUG(dbgs() << "Removing debug info due to spill:" << "\t" << *MI);
|
|
|
|
MI->eraseFromParent();
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-08-05 00:35:11 +02:00
|
|
|
// Stack slot accesses may coalesce away.
|
|
|
|
if (coalesceStackAccess(MI))
|
|
|
|
continue;
|
|
|
|
|
2010-06-30 01:58:39 +02:00
|
|
|
// Analyze instruction.
|
|
|
|
bool Reads, Writes;
|
|
|
|
SmallVector<unsigned, 8> Ops;
|
2010-10-15 01:49:52 +02:00
|
|
|
tie(Reads, Writes) = MI->readsWritesVirtualRegister(edit.getReg(), &Ops);
|
2010-06-30 01:58:39 +02:00
|
|
|
|
2010-07-02 19:44:57 +02:00
|
|
|
// Attempt to fold memory ops.
|
|
|
|
if (foldMemoryOperand(MI, Ops))
|
|
|
|
continue;
|
|
|
|
|
2010-06-30 01:58:39 +02:00
|
|
|
// Allocate interval around instruction.
|
|
|
|
// FIXME: Infer regclass from instruction alone.
|
2010-10-15 01:49:52 +02:00
|
|
|
LiveInterval &NewLI = edit.create(mri_, lis_, vrm_);
|
2010-06-30 01:58:39 +02:00
|
|
|
NewLI.markNotSpillable();
|
|
|
|
|
2010-07-02 19:44:57 +02:00
|
|
|
if (Reads)
|
2010-07-01 01:03:52 +02:00
|
|
|
insertReload(NewLI, MI);
|
2010-06-30 01:58:39 +02:00
|
|
|
|
|
|
|
// Rewrite instruction operands.
|
|
|
|
bool hasLiveDef = false;
|
|
|
|
for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(Ops[i]);
|
2010-10-15 01:49:52 +02:00
|
|
|
MO.setReg(NewLI.reg);
|
2010-06-30 01:58:39 +02:00
|
|
|
if (MO.isUse()) {
|
|
|
|
if (!MI->isRegTiedToDefOperand(Ops[i]))
|
|
|
|
MO.setIsKill();
|
|
|
|
} else {
|
|
|
|
if (!MO.isDead())
|
|
|
|
hasLiveDef = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Use a second vreg if instruction has no tied ops.
|
2010-07-01 01:03:52 +02:00
|
|
|
if (Writes && hasLiveDef)
|
|
|
|
insertSpill(NewLI, MI);
|
2010-06-30 01:58:39 +02:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
|
|
|
|
}
|
|
|
|
}
|