2010-06-29 23:58:39 +00: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 20:39:23 +00:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
2010-06-29 23:58:39 +00:00
|
|
|
#include "Spiller.h"
|
2010-10-14 23:49:52 +00:00
|
|
|
#include "LiveRangeEdit.h"
|
2010-06-29 23:58:39 +00:00
|
|
|
#include "VirtRegMap.h"
|
2010-11-10 23:55:56 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2010-06-29 23:58:39 +00:00
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2010-10-26 00:11:35 +00:00
|
|
|
#include "llvm/CodeGen/LiveStackAnalysis.h"
|
2010-06-29 23:58:39 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2010-10-28 20:34:47 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-06-29 23:58:39 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-10-28 20:34:47 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
VerifySpills("verify-spills", cl::desc("Verify after each spill/split"));
|
|
|
|
|
2010-06-29 23:58:39 +00:00
|
|
|
namespace {
|
|
|
|
class InlineSpiller : public Spiller {
|
2010-08-06 18:47:06 +00:00
|
|
|
MachineFunctionPass &pass_;
|
2010-06-29 23:58:39 +00:00
|
|
|
MachineFunction &mf_;
|
|
|
|
LiveIntervals &lis_;
|
2010-10-26 00:11:35 +00:00
|
|
|
LiveStacks &lss_;
|
2010-11-10 23:55:56 +00:00
|
|
|
AliasAnalysis *aa_;
|
2010-06-29 23:58:39 +00:00
|
|
|
VirtRegMap &vrm_;
|
|
|
|
MachineFrameInfo &mfi_;
|
|
|
|
MachineRegisterInfo &mri_;
|
|
|
|
const TargetInstrInfo &tii_;
|
|
|
|
const TargetRegisterInfo &tri_;
|
2010-06-30 23:03:52 +00:00
|
|
|
const BitVector reserved_;
|
|
|
|
|
|
|
|
// Variables that are valid during spill(), but used by multiple methods.
|
2010-10-14 23:49:52 +00:00
|
|
|
LiveRangeEdit *edit_;
|
2010-06-30 23:03:52 +00:00
|
|
|
const TargetRegisterClass *rc_;
|
|
|
|
int stackSlot_;
|
2010-06-29 23:58:39 +00:00
|
|
|
|
2010-10-20 22:00:51 +00:00
|
|
|
// Values that failed to remat at some point.
|
2010-07-02 17:44:57 +00:00
|
|
|
SmallPtrSet<VNInfo*, 8> usedValues_;
|
|
|
|
|
2010-06-29 23:58:39 +00:00
|
|
|
~InlineSpiller() {}
|
|
|
|
|
|
|
|
public:
|
2010-07-20 23:50:15 +00:00
|
|
|
InlineSpiller(MachineFunctionPass &pass,
|
|
|
|
MachineFunction &mf,
|
|
|
|
VirtRegMap &vrm)
|
2010-08-06 18:47:06 +00:00
|
|
|
: pass_(pass),
|
|
|
|
mf_(mf),
|
2010-07-20 23:50:15 +00:00
|
|
|
lis_(pass.getAnalysis<LiveIntervals>()),
|
2010-10-26 00:11:35 +00:00
|
|
|
lss_(pass.getAnalysis<LiveStacks>()),
|
2010-11-10 23:55:56 +00:00
|
|
|
aa_(&pass.getAnalysis<AliasAnalysis>()),
|
2010-07-20 23:50:15 +00:00
|
|
|
vrm_(vrm),
|
|
|
|
mfi_(*mf.getFrameInfo()),
|
|
|
|
mri_(mf.getRegInfo()),
|
|
|
|
tii_(*mf.getTarget().getInstrInfo()),
|
|
|
|
tri_(*mf.getTarget().getRegisterInfo()),
|
2010-12-10 22:54:40 +00:00
|
|
|
reserved_(tri_.getReservedRegs(mf_)) {}
|
2010-06-29 23:58:39 +00:00
|
|
|
|
|
|
|
void spill(LiveInterval *li,
|
2010-08-13 22:56:53 +00:00
|
|
|
SmallVectorImpl<LiveInterval*> &newIntervals,
|
2010-11-10 19:18:47 +00:00
|
|
|
const SmallVectorImpl<LiveInterval*> &spillIs);
|
2010-07-02 17:44:57 +00:00
|
|
|
|
2010-10-14 23:49:52 +00:00
|
|
|
void spill(LiveRangeEdit &);
|
|
|
|
|
2010-07-02 17:44:57 +00:00
|
|
|
private:
|
|
|
|
bool reMaterializeFor(MachineBasicBlock::iterator MI);
|
|
|
|
void reMaterializeAll();
|
|
|
|
|
2010-08-04 22:35:11 +00:00
|
|
|
bool coalesceStackAccess(MachineInstr *MI);
|
2010-07-01 00:13:04 +00:00
|
|
|
bool foldMemoryOperand(MachineBasicBlock::iterator MI,
|
2010-12-18 03:04:14 +00:00
|
|
|
const SmallVectorImpl<unsigned> &Ops,
|
|
|
|
MachineInstr *LoadMI = 0);
|
2010-06-30 23:03:52 +00:00
|
|
|
void insertReload(LiveInterval &NewLI, MachineBasicBlock::iterator MI);
|
|
|
|
void insertSpill(LiveInterval &NewLI, MachineBasicBlock::iterator MI);
|
2010-06-29 23:58:39 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
2010-07-20 23:50:15 +00:00
|
|
|
Spiller *createInlineSpiller(MachineFunctionPass &pass,
|
|
|
|
MachineFunction &mf,
|
|
|
|
VirtRegMap &vrm) {
|
2010-10-29 00:40:55 +00:00
|
|
|
if (VerifySpills)
|
2010-12-18 00:06:56 +00:00
|
|
|
mf.verify(&pass, "When creating inline spiller");
|
2010-07-20 23:50:15 +00:00
|
|
|
return new InlineSpiller(pass, mf, vrm);
|
2010-06-29 23:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-22 23:01:49 +00:00
|
|
|
/// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
|
2010-07-02 17:44:57 +00:00
|
|
|
bool InlineSpiller::reMaterializeFor(MachineBasicBlock::iterator MI) {
|
2010-06-30 23:03:52 +00:00
|
|
|
SlotIndex UseIdx = lis_.getInstructionIndex(MI).getUseIndex();
|
2010-10-14 23:49:52 +00:00
|
|
|
VNInfo *OrigVNI = edit_->getParent().getVNInfoAt(UseIdx);
|
2010-10-20 22:00:51 +00:00
|
|
|
|
2010-07-02 17:44:57 +00:00
|
|
|
if (!OrigVNI) {
|
|
|
|
DEBUG(dbgs() << "\tadding <undef> flags: ");
|
2010-06-30 23:03:52 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
2010-10-14 23:49:52 +00:00
|
|
|
if (MO.isReg() && MO.isUse() && MO.getReg() == edit_->getReg())
|
2010-06-30 23:03:52 +00:00
|
|
|
MO.setIsUndef();
|
|
|
|
}
|
2010-07-02 17:44:57 +00:00
|
|
|
DEBUG(dbgs() << UseIdx << '\t' << *MI);
|
2010-06-30 23:03:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-20 22:00:51 +00:00
|
|
|
|
2010-11-10 01:05:12 +00:00
|
|
|
LiveRangeEdit::Remat RM(OrigVNI);
|
|
|
|
if (!edit_->canRematerializeAt(RM, UseIdx, false, lis_)) {
|
2010-07-02 17:44:57 +00:00
|
|
|
usedValues_.insert(OrigVNI);
|
|
|
|
DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
|
2010-06-30 23:03:52 +00:00
|
|
|
return false;
|
2010-07-02 17:44:57 +00:00
|
|
|
}
|
2010-06-30 23:03:52 +00:00
|
|
|
|
2010-10-20 22:00:51 +00:00
|
|
|
// If the instruction also writes edit_->getReg(), it had better not require
|
|
|
|
// the same register for uses and defs.
|
2010-07-02 17:44:57 +00:00
|
|
|
bool Reads, Writes;
|
|
|
|
SmallVector<unsigned, 8> Ops;
|
2010-10-14 23:49:52 +00:00
|
|
|
tie(Reads, Writes) = MI->readsWritesVirtualRegister(edit_->getReg(), &Ops);
|
2010-07-02 17:44:57 +00: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-06-30 23:03:52 +00:00
|
|
|
return false;
|
2010-07-02 17:44:57 +00:00
|
|
|
}
|
2010-06-30 23:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-18 03:04:14 +00:00
|
|
|
// Before rematerializing into a register for a single instruction, try to
|
|
|
|
// fold a load into the instruction. That avoids allocating a new register.
|
|
|
|
if (RM.OrigMI->getDesc().canFoldAsLoad() &&
|
|
|
|
foldMemoryOperand(MI, Ops, RM.OrigMI)) {
|
|
|
|
edit_->markRematerialized(RM.ParentVNI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-02 17:44:57 +00:00
|
|
|
// Alocate a new register for the remat.
|
2010-10-14 23:49:52 +00:00
|
|
|
LiveInterval &NewLI = edit_->create(mri_, lis_, vrm_);
|
2010-07-02 17:44:57 +00:00
|
|
|
NewLI.markNotSpillable();
|
|
|
|
|
2011-02-09 00:25:36 +00:00
|
|
|
// Rematting for a copy: Set allocation hint to be the destination register.
|
|
|
|
if (MI->isCopy())
|
|
|
|
mri_.setRegAllocationHint(NewLI.reg, 0, MI->getOperand(0).getReg());
|
|
|
|
|
2010-07-02 17:44:57 +00:00
|
|
|
// Finally we can rematerialize OrigMI before MI.
|
2010-10-20 22:00:51 +00:00
|
|
|
SlotIndex DefIdx = edit_->rematerializeAt(*MI->getParent(), MI, NewLI.reg, RM,
|
|
|
|
lis_, tii_, tri_);
|
2011-02-08 19:33:55 +00:00
|
|
|
DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
|
|
|
|
<< *lis_.getInstructionFromIndex(DefIdx));
|
2010-07-02 17:44:57 +00:00
|
|
|
|
|
|
|
// Replace operands
|
|
|
|
for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(Ops[i]);
|
2010-10-14 23:49:52 +00:00
|
|
|
if (MO.isReg() && MO.isUse() && MO.getReg() == edit_->getReg()) {
|
|
|
|
MO.setReg(NewLI.reg);
|
2010-07-02 17:44:57 +00:00
|
|
|
MO.setIsKill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG(dbgs() << "\t " << UseIdx << '\t' << *MI);
|
|
|
|
|
2010-09-25 12:04:16 +00:00
|
|
|
VNInfo *DefVNI = NewLI.getNextValue(DefIdx, 0, lis_.getVNInfoAllocator());
|
2010-06-30 23:03:52 +00:00
|
|
|
NewLI.addRange(LiveRange(DefIdx, UseIdx.getDefIndex(), DefVNI));
|
2010-07-02 17:44:57 +00:00
|
|
|
DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
|
2010-06-30 23:03:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-14 23:49:52 +00:00
|
|
|
/// reMaterializeAll - Try to rematerialize as many uses as possible,
|
2010-07-02 17:44:57 +00: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-11-10 23:55:56 +00:00
|
|
|
if (!edit_->anyRematerializable(lis_, tii_, aa_))
|
2010-07-02 17:44:57 +00:00
|
|
|
return;
|
|
|
|
|
2010-10-20 22:00:51 +00:00
|
|
|
usedValues_.clear();
|
|
|
|
|
2010-10-14 23:49:52 +00:00
|
|
|
// Try to remat before all uses of edit_->getReg().
|
2010-07-02 17:44:57 +00:00
|
|
|
bool anyRemat = false;
|
|
|
|
for (MachineRegisterInfo::use_nodbg_iterator
|
2010-10-14 23:49:52 +00:00
|
|
|
RI = mri_.use_nodbg_begin(edit_->getReg());
|
2010-07-02 17:44:57 +00:00
|
|
|
MachineInstr *MI = RI.skipInstruction();)
|
|
|
|
anyRemat |= reMaterializeFor(MI);
|
|
|
|
|
|
|
|
if (!anyRemat)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Remove any values that were completely rematted.
|
|
|
|
bool anyRemoved = false;
|
2010-10-20 22:00:51 +00:00
|
|
|
for (LiveInterval::vni_iterator I = edit_->getParent().vni_begin(),
|
|
|
|
E = edit_->getParent().vni_end(); I != E; ++I) {
|
2010-07-02 17:44:57 +00:00
|
|
|
VNInfo *VNI = *I;
|
2010-10-20 22:00:51 +00:00
|
|
|
if (VNI->hasPHIKill() || !edit_->didRematerialize(VNI) ||
|
|
|
|
usedValues_.count(VNI))
|
2010-07-02 17:44:57 +00: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 03:37:09 +00:00
|
|
|
VNI->def = SlotIndex();
|
2010-07-02 17:44:57 +00:00
|
|
|
anyRemoved = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!anyRemoved)
|
|
|
|
return;
|
|
|
|
|
2010-10-14 23:49:52 +00: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 19:54:40 +00:00
|
|
|
MachineInstr *MI = RI.skipInstruction();) {
|
|
|
|
if (!MI->isDebugValue())
|
2010-07-02 17:44:57 +00:00
|
|
|
continue;
|
2010-10-14 23:49:52 +00:00
|
|
|
// Try to preserve the debug value if parent is live immediately after it.
|
2010-07-02 19:54:40 +00:00
|
|
|
MachineBasicBlock::iterator NextMI = MI;
|
|
|
|
++NextMI;
|
|
|
|
if (NextMI != MI->getParent()->end() && !lis_.isNotInMIMap(NextMI)) {
|
2010-10-14 23:49:52 +00:00
|
|
|
SlotIndex Idx = lis_.getInstructionIndex(NextMI);
|
|
|
|
VNInfo *VNI = edit_->getParent().getVNInfoAt(Idx);
|
2010-08-10 20:45:07 +00:00
|
|
|
if (VNI && (VNI->hasPHIKill() || usedValues_.count(VNI)))
|
2010-07-02 19:54:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DEBUG(dbgs() << "Removing debug info due to remat:" << "\t" << *MI);
|
|
|
|
MI->eraseFromParent();
|
2010-07-02 17:44:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-04 22:35:11 +00: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-14 23:49:52 +00:00
|
|
|
if (reg != edit_->getReg() || FI != stackSlot_)
|
2010-08-04 22:35:11 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Coalescing stack access: " << *MI);
|
|
|
|
lis_.RemoveMachineInstrFromMaps(MI);
|
|
|
|
MI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-01 00:13:04 +00:00
|
|
|
/// foldMemoryOperand - Try folding stack slot references in Ops into MI.
|
2010-12-18 03:04:14 +00:00
|
|
|
/// @param MI Instruction using or defining the current register.
|
2010-12-18 03:28:32 +00:00
|
|
|
/// @param Ops Operand indices from readsWritesVirtualRegister().
|
2010-12-18 03:04:14 +00:00
|
|
|
/// @param LoadMI Load instruction to use instead of stack slot when non-null.
|
|
|
|
/// @return True on success, and MI will be erased.
|
2010-07-01 00:13:04 +00:00
|
|
|
bool InlineSpiller::foldMemoryOperand(MachineBasicBlock::iterator MI,
|
2010-12-18 03:04:14 +00:00
|
|
|
const SmallVectorImpl<unsigned> &Ops,
|
|
|
|
MachineInstr *LoadMI) {
|
2010-07-01 00:13:04 +00:00
|
|
|
// 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;
|
2011-02-08 19:33:55 +00:00
|
|
|
// We cannot fold a load instruction into a def.
|
|
|
|
if (LoadMI && MO.isDef())
|
|
|
|
return false;
|
2010-07-01 00:13:04 +00:00
|
|
|
// Tied use operands should not be passed to foldMemoryOperand.
|
|
|
|
if (!MI->isRegTiedToDefOperand(Idx))
|
|
|
|
FoldOps.push_back(Idx);
|
|
|
|
}
|
|
|
|
|
2010-12-18 03:04:14 +00:00
|
|
|
MachineInstr *FoldMI =
|
|
|
|
LoadMI ? tii_.foldMemoryOperand(MI, FoldOps, LoadMI)
|
|
|
|
: tii_.foldMemoryOperand(MI, FoldOps, stackSlot_);
|
2010-07-01 00:13:04 +00:00
|
|
|
if (!FoldMI)
|
|
|
|
return false;
|
|
|
|
lis_.ReplaceMachineInstrInMaps(MI, FoldMI);
|
2010-12-18 03:04:14 +00:00
|
|
|
if (!LoadMI)
|
|
|
|
vrm_.addSpillSlotUse(stackSlot_, FoldMI);
|
2010-07-09 17:29:08 +00:00
|
|
|
MI->eraseFromParent();
|
2010-07-01 00:13:04 +00:00
|
|
|
DEBUG(dbgs() << "\tfolded: " << *FoldMI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-30 23:03:52 +00: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 12:04:16 +00:00
|
|
|
VNInfo *LoadVNI = NewLI.getNextValue(LoadIdx, 0,
|
2010-06-30 23:03:52 +00: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();
|
2010-11-15 20:55:49 +00:00
|
|
|
|
|
|
|
// Get the defined value. It could be an early clobber so keep the def index.
|
2010-06-30 23:03:52 +00:00
|
|
|
SlotIndex Idx = lis_.getInstructionIndex(MI).getDefIndex();
|
2010-11-15 20:55:49 +00:00
|
|
|
VNInfo *VNI = edit_->getParent().getVNInfoAt(Idx);
|
|
|
|
assert(VNI && VNI->def.getDefIndex() == Idx && "Inconsistent VNInfo");
|
|
|
|
Idx = VNI->def;
|
|
|
|
|
2010-06-30 23:03:52 +00:00
|
|
|
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 12:04:16 +00:00
|
|
|
VNInfo *StoreVNI = NewLI.getNextValue(Idx, 0, lis_.getVNInfoAllocator());
|
2010-06-30 23:03:52 +00:00
|
|
|
NewLI.addRange(LiveRange(Idx, StoreIdx, StoreVNI));
|
|
|
|
}
|
|
|
|
|
2010-06-29 23:58:39 +00:00
|
|
|
void InlineSpiller::spill(LiveInterval *li,
|
2010-08-13 22:56:53 +00:00
|
|
|
SmallVectorImpl<LiveInterval*> &newIntervals,
|
2010-11-10 19:18:47 +00:00
|
|
|
const SmallVectorImpl<LiveInterval*> &spillIs) {
|
2011-03-07 22:42:16 +00:00
|
|
|
LiveRangeEdit edit(*li, newIntervals, &spillIs);
|
2010-10-14 23:49:52 +00:00
|
|
|
spill(edit);
|
2010-10-28 20:34:47 +00:00
|
|
|
if (VerifySpills)
|
2010-12-18 00:06:56 +00:00
|
|
|
mf_.verify(&pass_, "After inline spill");
|
2010-10-14 23:49:52 +00:00
|
|
|
}
|
2010-06-29 23:58:39 +00:00
|
|
|
|
2010-10-14 23:49:52 +00:00
|
|
|
void InlineSpiller::spill(LiveRangeEdit &edit) {
|
|
|
|
edit_ = &edit;
|
2011-01-09 21:17:37 +00:00
|
|
|
assert(!TargetRegisterInfo::isStackSlot(edit.getReg())
|
|
|
|
&& "Trying to spill a stack slot.");
|
2010-10-30 01:26:09 +00:00
|
|
|
DEBUG(dbgs() << "Inline spilling "
|
|
|
|
<< mri_.getRegClass(edit.getReg())->getName()
|
2011-02-24 01:07:55 +00:00
|
|
|
<< ':' << edit.getParent() << "\nFrom original "
|
|
|
|
<< PrintReg(vrm_.getOriginal(edit.getReg())) << '\n');
|
2010-10-14 23:49:52 +00:00
|
|
|
assert(edit.getParent().isSpillable() &&
|
|
|
|
"Attempting to spill already spilled value.");
|
2010-06-29 23:58:39 +00:00
|
|
|
|
2010-07-02 17:44:57 +00:00
|
|
|
reMaterializeAll();
|
|
|
|
|
|
|
|
// Remat may handle everything.
|
2010-10-14 23:49:52 +00:00
|
|
|
if (edit_->getParent().empty())
|
2010-07-02 17:44:57 +00:00
|
|
|
return;
|
|
|
|
|
2010-10-14 23:49:52 +00:00
|
|
|
rc_ = mri_.getRegClass(edit.getReg());
|
2011-02-24 01:07:55 +00:00
|
|
|
|
|
|
|
// Share a stack slot among all descendants of Orig.
|
|
|
|
unsigned Orig = vrm_.getOriginal(edit.getReg());
|
|
|
|
stackSlot_ = vrm_.getStackSlot(Orig);
|
|
|
|
if (stackSlot_ == VirtRegMap::NO_STACK_SLOT)
|
|
|
|
stackSlot_ = vrm_.assignVirt2StackSlot(Orig);
|
|
|
|
|
|
|
|
if (Orig != edit.getReg())
|
|
|
|
vrm_.assignVirt2StackSlot(edit.getReg(), stackSlot_);
|
2010-07-02 17:44:57 +00:00
|
|
|
|
2010-10-26 00:11:35 +00:00
|
|
|
// Update LiveStacks now that we are committed to spilling.
|
|
|
|
LiveInterval &stacklvr = lss_.getOrCreateInterval(stackSlot_, rc_);
|
2011-02-24 01:07:55 +00:00
|
|
|
if (!stacklvr.hasAtLeastOneValue())
|
|
|
|
stacklvr.getNextValue(SlotIndex(), 0, lss_.getVNInfoAllocator());
|
2010-10-26 00:11:35 +00:00
|
|
|
stacklvr.MergeRangesInAsValue(edit_->getParent(), stacklvr.getValNumInfo(0));
|
|
|
|
|
2010-06-30 23:03:52 +00:00
|
|
|
// Iterate over instructions using register.
|
2010-10-14 23:49:52 +00:00
|
|
|
for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(edit.getReg());
|
2010-06-29 23:58:39 +00:00
|
|
|
MachineInstr *MI = RI.skipInstruction();) {
|
|
|
|
|
2010-07-02 19:54:40 +00: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-04 22:35:11 +00:00
|
|
|
// Stack slot accesses may coalesce away.
|
|
|
|
if (coalesceStackAccess(MI))
|
|
|
|
continue;
|
|
|
|
|
2010-06-29 23:58:39 +00:00
|
|
|
// Analyze instruction.
|
|
|
|
bool Reads, Writes;
|
|
|
|
SmallVector<unsigned, 8> Ops;
|
2010-10-14 23:49:52 +00:00
|
|
|
tie(Reads, Writes) = MI->readsWritesVirtualRegister(edit.getReg(), &Ops);
|
2010-06-29 23:58:39 +00:00
|
|
|
|
2010-07-02 17:44:57 +00:00
|
|
|
// Attempt to fold memory ops.
|
|
|
|
if (foldMemoryOperand(MI, Ops))
|
|
|
|
continue;
|
|
|
|
|
2010-06-29 23:58:39 +00:00
|
|
|
// Allocate interval around instruction.
|
|
|
|
// FIXME: Infer regclass from instruction alone.
|
2010-10-14 23:49:52 +00:00
|
|
|
LiveInterval &NewLI = edit.create(mri_, lis_, vrm_);
|
2010-06-29 23:58:39 +00:00
|
|
|
NewLI.markNotSpillable();
|
|
|
|
|
2010-07-02 17:44:57 +00:00
|
|
|
if (Reads)
|
2010-06-30 23:03:52 +00:00
|
|
|
insertReload(NewLI, MI);
|
2010-06-29 23:58:39 +00: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-14 23:49:52 +00:00
|
|
|
MO.setReg(NewLI.reg);
|
2010-06-29 23:58:39 +00: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-06-30 23:03:52 +00:00
|
|
|
if (Writes && hasLiveDef)
|
|
|
|
insertSpill(NewLI, MI);
|
2010-06-29 23:58:39 +00:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
|
|
|
|
}
|
|
|
|
}
|