2004-07-23 17:56:30 +00:00
|
|
|
//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
|
2003-11-20 03:32:25 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2003-11-20 03:32:25 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the LiveInterval analysis pass which is used
|
|
|
|
// by the Linear Scan Register allocator. This pass linearizes the
|
|
|
|
// basic blocks of the function in DFS order and uses the
|
|
|
|
// LiveVariables pass to conservatively compute live intervals for
|
|
|
|
// each virtual and physical register.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-09-21 04:19:09 +00:00
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "LiveRangeCalc.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2008-07-25 00:02:30 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2003-11-20 03:32:25 +00:00
|
|
|
#include "llvm/CodeGen/LiveVariables.h"
|
2013-12-14 00:53:32 +00:00
|
|
|
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
|
2012-06-05 22:02:15 +00:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
2003-11-20 03:32:25 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2007-12-31 04:13:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2003-11-20 03:32:25 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2012-11-28 19:13:06 +00:00
|
|
|
#include "llvm/CodeGen/VirtRegMap.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Value.h"
|
2013-06-17 19:00:36 +00:00
|
|
|
#include "llvm/Support/BlockFrequency.h"
|
2012-07-27 20:58:46 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 13:10:19 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2014-08-04 21:25:23 +00:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2004-09-03 18:19:51 +00:00
|
|
|
#include <algorithm>
|
2006-12-02 02:22:01 +00:00
|
|
|
#include <cmath>
|
2012-12-03 16:50:05 +00:00
|
|
|
#include <limits>
|
2003-11-20 03:32:25 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:02:50 +00:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char LiveIntervals::ID = 0;
|
2012-08-03 22:12:54 +00:00
|
|
|
char &llvm::LiveIntervalsID = LiveIntervals::ID;
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(LiveIntervals, "liveintervals",
|
|
|
|
"Live Interval Analysis", false, false)
|
2012-02-10 04:10:36 +00:00
|
|
|
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LiveVariables)
|
2012-02-10 04:10:36 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
|
|
|
|
INITIALIZE_PASS_END(LiveIntervals, "liveintervals",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Live Interval Analysis", false, false)
|
2003-11-20 03:32:25 +00:00
|
|
|
|
2013-06-21 18:33:23 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
static cl::opt<bool> EnablePrecomputePhysRegs(
|
|
|
|
"precompute-phys-liveness", cl::Hidden,
|
|
|
|
cl::desc("Eagerly compute live intervals for all physreg units."));
|
|
|
|
#else
|
|
|
|
static bool EnablePrecomputePhysRegs = false;
|
|
|
|
#endif // NDEBUG
|
|
|
|
|
2006-08-24 22:43:55 +00:00
|
|
|
void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
|
2009-07-31 23:37:33 +00:00
|
|
|
AU.setPreservesCFG();
|
2008-07-25 00:02:30 +00:00
|
|
|
AU.addRequired<AliasAnalysis>();
|
|
|
|
AU.addPreserved<AliasAnalysis>();
|
2013-02-09 00:04:07 +00:00
|
|
|
// LiveVariables isn't really required by this analysis, it is only required
|
|
|
|
// here to make sure it is live during TwoAddressInstructionPass and
|
|
|
|
// PHIElimination. This is temporary.
|
2004-08-04 09:46:26 +00:00
|
|
|
AU.addRequired<LiveVariables>();
|
2010-08-17 21:00:37 +00:00
|
|
|
AU.addPreserved<LiveVariables>();
|
2012-02-13 20:44:42 +00:00
|
|
|
AU.addPreservedID(MachineLoopInfoID);
|
2012-06-20 23:31:34 +00:00
|
|
|
AU.addRequiredTransitiveID(MachineDominatorsID);
|
2008-01-04 20:54:55 +00:00
|
|
|
AU.addPreservedID(MachineDominatorsID);
|
2009-11-03 23:52:08 +00:00
|
|
|
AU.addPreserved<SlotIndexes>();
|
|
|
|
AU.addRequiredTransitive<SlotIndexes>();
|
2004-08-04 09:46:26 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
2003-11-20 03:32:25 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 22:02:15 +00:00
|
|
|
LiveIntervals::LiveIntervals() : MachineFunctionPass(ID),
|
2014-04-14 00:51:57 +00:00
|
|
|
DomTree(nullptr), LRCalc(nullptr) {
|
2012-06-05 22:02:15 +00:00
|
|
|
initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
LiveIntervals::~LiveIntervals() {
|
|
|
|
delete LRCalc;
|
|
|
|
}
|
|
|
|
|
2006-08-24 22:43:55 +00:00
|
|
|
void LiveIntervals::releaseMemory() {
|
2008-08-13 21:49:13 +00:00
|
|
|
// Free the live intervals themselves.
|
2012-06-22 20:37:52 +00:00
|
|
|
for (unsigned i = 0, e = VirtRegIntervals.size(); i != e; ++i)
|
|
|
|
delete VirtRegIntervals[TargetRegisterInfo::index2VirtReg(i)];
|
|
|
|
VirtRegIntervals.clear();
|
2012-02-08 17:33:45 +00:00
|
|
|
RegMaskSlots.clear();
|
|
|
|
RegMaskBits.clear();
|
2012-02-10 01:26:29 +00:00
|
|
|
RegMaskBlocks.clear();
|
2009-07-09 03:57:02 +00:00
|
|
|
|
2013-10-10 21:29:02 +00:00
|
|
|
for (unsigned i = 0, e = RegUnitRanges.size(); i != e; ++i)
|
|
|
|
delete RegUnitRanges[i];
|
|
|
|
RegUnitRanges.clear();
|
2012-06-05 22:02:15 +00:00
|
|
|
|
2010-06-26 11:30:59 +00:00
|
|
|
// Release VNInfo memory regions, VNInfo objects don't need to be dtor'd.
|
|
|
|
VNInfoAllocator.Reset();
|
2006-05-11 07:29:24 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 17:28:46 +00:00
|
|
|
/// runOnMachineFunction - calculates LiveIntervals
|
2008-05-28 20:54:50 +00:00
|
|
|
///
|
|
|
|
bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
|
2012-06-04 22:39:14 +00:00
|
|
|
MF = &fn;
|
|
|
|
MRI = &MF->getRegInfo();
|
2014-10-14 06:26:53 +00:00
|
|
|
TRI = MF->getSubtarget().getRegisterInfo();
|
|
|
|
TII = MF->getSubtarget().getInstrInfo();
|
2012-06-04 22:39:14 +00:00
|
|
|
AA = &getAnalysis<AliasAnalysis>();
|
|
|
|
Indexes = &getAnalysis<SlotIndexes>();
|
2012-06-20 23:31:34 +00:00
|
|
|
DomTree = &getAnalysis<MachineDominatorTree>();
|
|
|
|
if (!LRCalc)
|
2012-06-05 22:02:15 +00:00
|
|
|
LRCalc = new LiveRangeCalc();
|
2003-11-20 03:32:25 +00:00
|
|
|
|
2012-07-27 20:58:46 +00:00
|
|
|
// Allocate space for all virtual registers.
|
|
|
|
VirtRegIntervals.resize(MRI->getNumVirtRegs());
|
|
|
|
|
2013-02-09 00:04:07 +00:00
|
|
|
computeVirtRegs();
|
|
|
|
computeRegMasks();
|
2012-06-20 23:31:34 +00:00
|
|
|
computeLiveInRegUnits();
|
2012-06-05 22:02:15 +00:00
|
|
|
|
2013-06-21 18:33:23 +00:00
|
|
|
if (EnablePrecomputePhysRegs) {
|
|
|
|
// For stress testing, precompute live ranges of all physical register
|
|
|
|
// units, including reserved registers.
|
|
|
|
for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
|
|
|
|
getRegUnit(i);
|
|
|
|
}
|
2004-09-30 15:59:17 +00:00
|
|
|
DEBUG(dump());
|
2004-08-04 09:46:26 +00:00
|
|
|
return true;
|
2003-11-20 03:32:25 +00:00
|
|
|
}
|
|
|
|
|
2004-09-30 15:59:17 +00:00
|
|
|
/// print - Implement the dump method.
|
2009-08-23 06:03:38 +00:00
|
|
|
void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
|
2009-08-23 03:41:05 +00:00
|
|
|
OS << "********** INTERVALS **********\n";
|
2012-02-14 23:46:21 +00:00
|
|
|
|
2012-06-05 22:02:15 +00:00
|
|
|
// Dump the regunits.
|
2013-10-10 21:29:02 +00:00
|
|
|
for (unsigned i = 0, e = RegUnitRanges.size(); i != e; ++i)
|
|
|
|
if (LiveRange *LR = RegUnitRanges[i])
|
2013-10-10 21:29:05 +00:00
|
|
|
OS << PrintRegUnit(i, TRI) << ' ' << *LR << '\n';
|
2012-06-05 22:02:15 +00:00
|
|
|
|
2012-02-14 23:46:21 +00:00
|
|
|
// Dump the virtregs.
|
2012-06-22 20:37:52 +00:00
|
|
|
for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
|
|
|
|
if (hasInterval(Reg))
|
2013-10-10 21:29:05 +00:00
|
|
|
OS << getInterval(Reg) << '\n';
|
2012-06-22 20:37:52 +00:00
|
|
|
}
|
2004-09-30 15:59:17 +00:00
|
|
|
|
2012-11-09 19:18:49 +00:00
|
|
|
OS << "RegMasks:";
|
|
|
|
for (unsigned i = 0, e = RegMaskSlots.size(); i != e; ++i)
|
|
|
|
OS << ' ' << RegMaskSlots[i];
|
|
|
|
OS << '\n';
|
|
|
|
|
2009-09-14 21:33:42 +00:00
|
|
|
printInstrs(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LiveIntervals::printInstrs(raw_ostream &OS) const {
|
2009-08-23 03:41:05 +00:00
|
|
|
OS << "********** MACHINEINSTRS **********\n";
|
2012-06-04 22:39:14 +00:00
|
|
|
MF->print(OS, Indexes);
|
2004-09-30 15:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-09-11 22:23:19 +00:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2009-09-14 21:33:42 +00:00
|
|
|
void LiveIntervals::dumpInstrs() const {
|
2010-01-04 22:49:02 +00:00
|
|
|
printInstrs(dbgs());
|
2009-09-14 21:33:42 +00:00
|
|
|
}
|
2012-09-06 19:06:06 +00:00
|
|
|
#endif
|
2009-09-14 21:33:42 +00:00
|
|
|
|
2008-08-13 21:49:13 +00:00
|
|
|
LiveInterval* LiveIntervals::createInterval(unsigned reg) {
|
2013-11-13 00:15:44 +00:00
|
|
|
float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
|
|
|
|
llvm::huge_valf : 0.0F;
|
2008-08-13 21:49:13 +00:00
|
|
|
return new LiveInterval(reg, Weight);
|
2004-04-09 18:07:57 +00:00
|
|
|
}
|
2007-11-12 06:35:08 +00:00
|
|
|
|
2012-06-05 22:02:15 +00:00
|
|
|
|
2012-07-27 20:58:46 +00:00
|
|
|
/// computeVirtRegInterval - Compute the live interval of a virtual register,
|
|
|
|
/// based on defs and uses.
|
2013-10-10 21:28:57 +00:00
|
|
|
void LiveIntervals::computeVirtRegInterval(LiveInterval &LI) {
|
2012-07-27 20:58:46 +00:00
|
|
|
assert(LRCalc && "LRCalc not initialized.");
|
2013-10-10 21:28:57 +00:00
|
|
|
assert(LI.empty() && "Should only compute empty intervals.");
|
2012-07-27 20:58:46 +00:00
|
|
|
LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator());
|
|
|
|
LRCalc->createDeadDefs(LI);
|
|
|
|
LRCalc->extendToUses(LI);
|
2014-06-03 22:42:10 +00:00
|
|
|
computeDeadValues(&LI, LI, nullptr, nullptr);
|
2012-07-27 20:58:46 +00:00
|
|
|
}
|
|
|
|
|
2012-07-27 21:56:39 +00:00
|
|
|
void LiveIntervals::computeVirtRegs() {
|
|
|
|
for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
|
|
|
|
if (MRI->reg_nodbg_empty(Reg))
|
|
|
|
continue;
|
2013-08-14 23:50:16 +00:00
|
|
|
createAndComputeVirtRegInterval(Reg);
|
2012-07-27 21:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LiveIntervals::computeRegMasks() {
|
|
|
|
RegMaskBlocks.resize(MF->getNumBlockIDs());
|
|
|
|
|
|
|
|
// Find all instructions with regmask operands.
|
|
|
|
for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end();
|
|
|
|
MBBI != E; ++MBBI) {
|
|
|
|
MachineBasicBlock *MBB = MBBI;
|
|
|
|
std::pair<unsigned, unsigned> &RMB = RegMaskBlocks[MBB->getNumber()];
|
|
|
|
RMB.first = RegMaskSlots.size();
|
|
|
|
for (MachineBasicBlock::iterator MI = MBB->begin(), ME = MBB->end();
|
|
|
|
MI != ME; ++MI)
|
|
|
|
for (MIOperands MO(MI); MO.isValid(); ++MO) {
|
|
|
|
if (!MO->isRegMask())
|
|
|
|
continue;
|
|
|
|
RegMaskSlots.push_back(Indexes->getInstructionIndex(MI).getRegSlot());
|
|
|
|
RegMaskBits.push_back(MO->getRegMask());
|
|
|
|
}
|
|
|
|
// Compute the number of register mask instructions in this block.
|
2012-09-10 21:26:47 +00:00
|
|
|
RMB.second = RegMaskSlots.size() - RMB.first;
|
2012-07-27 21:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-27 20:58:46 +00:00
|
|
|
|
2012-06-05 22:02:15 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Register Unit Liveness
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Fixed interference typically comes from ABI boundaries: Function arguments
|
|
|
|
// and return values are passed in fixed registers, and so are exception
|
|
|
|
// pointers entering landing pads. Certain instructions require values to be
|
|
|
|
// present in specific registers. That is also represented through fixed
|
|
|
|
// interference.
|
|
|
|
//
|
|
|
|
|
2013-10-10 21:29:02 +00:00
|
|
|
/// computeRegUnitInterval - Compute the live range of a register unit, based
|
|
|
|
/// on the uses and defs of aliasing registers. The range should be empty,
|
2012-06-05 22:02:15 +00:00
|
|
|
/// or contain only dead phi-defs from ABI blocks.
|
2013-10-10 21:29:02 +00:00
|
|
|
void LiveIntervals::computeRegUnitRange(LiveRange &LR, unsigned Unit) {
|
2012-06-05 22:02:15 +00:00
|
|
|
assert(LRCalc && "LRCalc not initialized.");
|
|
|
|
LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator());
|
|
|
|
|
|
|
|
// The physregs aliasing Unit are the roots and their super-registers.
|
|
|
|
// Create all values as dead defs before extending to uses. Note that roots
|
|
|
|
// may share super-registers. That's OK because createDeadDefs() is
|
|
|
|
// idempotent. It is very rare for a register unit to have multiple roots, so
|
|
|
|
// uniquing super-registers is probably not worthwhile.
|
|
|
|
for (MCRegUnitRootIterator Roots(Unit, TRI); Roots.isValid(); ++Roots) {
|
2013-05-22 22:36:55 +00:00
|
|
|
for (MCSuperRegIterator Supers(*Roots, TRI, /*IncludeSelf=*/true);
|
|
|
|
Supers.isValid(); ++Supers) {
|
2012-06-05 22:02:15 +00:00
|
|
|
if (!MRI->reg_empty(*Supers))
|
2013-10-10 21:29:02 +00:00
|
|
|
LRCalc->createDeadDefs(LR, *Supers);
|
2012-06-05 22:02:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-10 21:29:02 +00:00
|
|
|
// Now extend LR to reach all uses.
|
2012-06-05 22:02:15 +00:00
|
|
|
// Ignore uses of reserved registers. We only track defs of those.
|
|
|
|
for (MCRegUnitRootIterator Roots(Unit, TRI); Roots.isValid(); ++Roots) {
|
2013-05-22 22:36:55 +00:00
|
|
|
for (MCSuperRegIterator Supers(*Roots, TRI, /*IncludeSelf=*/true);
|
|
|
|
Supers.isValid(); ++Supers) {
|
2012-06-05 22:02:15 +00:00
|
|
|
unsigned Reg = *Supers;
|
2012-10-15 22:14:34 +00:00
|
|
|
if (!MRI->isReserved(Reg) && !MRI->reg_empty(Reg))
|
2013-10-10 21:29:02 +00:00
|
|
|
LRCalc->extendToUses(LR, Reg);
|
2012-06-05 22:02:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// computeLiveInRegUnits - Precompute the live ranges of any register units
|
|
|
|
/// that are live-in to an ABI block somewhere. Register values can appear
|
|
|
|
/// without a corresponding def when entering the entry block or a landing pad.
|
|
|
|
///
|
|
|
|
void LiveIntervals::computeLiveInRegUnits() {
|
2013-10-10 21:29:02 +00:00
|
|
|
RegUnitRanges.resize(TRI->getNumRegUnits());
|
2012-06-05 22:02:15 +00:00
|
|
|
DEBUG(dbgs() << "Computing live-in reg-units in ABI blocks.\n");
|
|
|
|
|
2013-10-10 21:29:02 +00:00
|
|
|
// Keep track of the live range sets allocated.
|
|
|
|
SmallVector<unsigned, 8> NewRanges;
|
2012-06-05 22:02:15 +00:00
|
|
|
|
|
|
|
// Check all basic blocks for live-ins.
|
|
|
|
for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
|
|
|
|
MFI != MFE; ++MFI) {
|
|
|
|
const MachineBasicBlock *MBB = MFI;
|
|
|
|
|
|
|
|
// We only care about ABI blocks: Entry + landing pads.
|
|
|
|
if ((MFI != MF->begin() && !MBB->isLandingPad()) || MBB->livein_empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Create phi-defs at Begin for all live-in registers.
|
|
|
|
SlotIndex Begin = Indexes->getMBBStartIdx(MBB);
|
|
|
|
DEBUG(dbgs() << Begin << "\tBB#" << MBB->getNumber());
|
|
|
|
for (MachineBasicBlock::livein_iterator LII = MBB->livein_begin(),
|
|
|
|
LIE = MBB->livein_end(); LII != LIE; ++LII) {
|
|
|
|
for (MCRegUnitIterator Units(*LII, TRI); Units.isValid(); ++Units) {
|
|
|
|
unsigned Unit = *Units;
|
2013-10-10 21:29:02 +00:00
|
|
|
LiveRange *LR = RegUnitRanges[Unit];
|
|
|
|
if (!LR) {
|
|
|
|
LR = RegUnitRanges[Unit] = new LiveRange();
|
|
|
|
NewRanges.push_back(Unit);
|
2012-06-05 22:02:15 +00:00
|
|
|
}
|
2013-10-10 21:29:02 +00:00
|
|
|
VNInfo *VNI = LR->createDeadDef(Begin, getVNInfoAllocator());
|
2012-06-05 23:00:03 +00:00
|
|
|
(void)VNI;
|
2012-06-05 22:02:15 +00:00
|
|
|
DEBUG(dbgs() << ' ' << PrintRegUnit(Unit, TRI) << '#' << VNI->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG(dbgs() << '\n');
|
|
|
|
}
|
2013-10-10 21:29:02 +00:00
|
|
|
DEBUG(dbgs() << "Created " << NewRanges.size() << " new intervals.\n");
|
2012-06-05 22:02:15 +00:00
|
|
|
|
2013-10-10 21:29:02 +00:00
|
|
|
// Compute the 'normal' part of the ranges.
|
|
|
|
for (unsigned i = 0, e = NewRanges.size(); i != e; ++i) {
|
|
|
|
unsigned Unit = NewRanges[i];
|
|
|
|
computeRegUnitRange(*RegUnitRanges[Unit], Unit);
|
|
|
|
}
|
2012-06-05 22:02:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-08 00:03:05 +00:00
|
|
|
/// shrinkToUses - After removing some uses of a register, shrink its live
|
|
|
|
/// range to just the remaining uses. This method does not compute reaching
|
|
|
|
/// defs for new uses, and it doesn't remove dead defs.
|
2011-03-17 20:37:07 +00:00
|
|
|
bool LiveIntervals::shrinkToUses(LiveInterval *li,
|
2011-03-07 23:29:10 +00:00
|
|
|
SmallVectorImpl<MachineInstr*> *dead) {
|
2011-02-08 00:03:05 +00:00
|
|
|
DEBUG(dbgs() << "Shrink: " << *li << '\n');
|
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(li->reg)
|
2012-01-03 20:05:57 +00:00
|
|
|
&& "Can only shrink virtual registers");
|
2011-02-08 00:03:05 +00:00
|
|
|
// Find all the values used, including PHI kills.
|
|
|
|
SmallVector<std::pair<SlotIndex, VNInfo*>, 16> WorkList;
|
|
|
|
|
2011-09-15 15:24:16 +00:00
|
|
|
// Blocks that have already been added to WorkList as live-out.
|
|
|
|
SmallPtrSet<MachineBasicBlock*, 16> LiveOut;
|
|
|
|
|
2011-02-08 00:03:05 +00:00
|
|
|
// Visit all instructions reading li->reg.
|
2014-03-13 06:02:25 +00:00
|
|
|
for (MachineRegisterInfo::reg_instr_iterator
|
|
|
|
I = MRI->reg_instr_begin(li->reg), E = MRI->reg_instr_end();
|
|
|
|
I != E; ) {
|
|
|
|
MachineInstr *UseMI = &*(I++);
|
2011-02-08 00:03:05 +00:00
|
|
|
if (UseMI->isDebugValue() || !UseMI->readsVirtualRegister(li->reg))
|
|
|
|
continue;
|
2011-11-13 23:53:25 +00:00
|
|
|
SlotIndex Idx = getInstructionIndex(UseMI).getRegSlot();
|
2013-10-10 21:28:52 +00:00
|
|
|
LiveQueryResult LRQ = li->Query(Idx);
|
2012-05-20 02:54:52 +00:00
|
|
|
VNInfo *VNI = LRQ.valueIn();
|
2011-03-18 03:06:04 +00:00
|
|
|
if (!VNI) {
|
|
|
|
// This shouldn't happen: readsVirtualRegister returns true, but there is
|
|
|
|
// no live value. It is likely caused by a target getting <undef> flags
|
|
|
|
// wrong.
|
|
|
|
DEBUG(dbgs() << Idx << '\t' << *UseMI
|
|
|
|
<< "Warning: Instr claims to read non-existent value in "
|
|
|
|
<< *li << '\n');
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-14 18:45:38 +00:00
|
|
|
// Special case: An early-clobber tied operand reads and writes the
|
2012-05-20 02:54:52 +00:00
|
|
|
// register one slot early.
|
|
|
|
if (VNInfo *DefVNI = LRQ.valueDefined())
|
|
|
|
Idx = DefVNI->def;
|
|
|
|
|
2011-02-08 00:03:05 +00:00
|
|
|
WorkList.push_back(std::make_pair(Idx, VNI));
|
|
|
|
}
|
|
|
|
|
2013-10-10 21:28:47 +00:00
|
|
|
// Create new live ranges with only minimal live segments per def.
|
|
|
|
LiveRange NewLR;
|
2011-02-08 00:03:05 +00:00
|
|
|
for (LiveInterval::vni_iterator I = li->vni_begin(), E = li->vni_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
VNInfo *VNI = *I;
|
|
|
|
if (VNI->isUnused())
|
|
|
|
continue;
|
2013-10-10 21:28:47 +00:00
|
|
|
NewLR.addSegment(LiveRange::Segment(VNI->def, VNI->def.getDeadSlot(), VNI));
|
2011-02-08 00:03:05 +00:00
|
|
|
}
|
|
|
|
|
2011-03-02 00:33:03 +00:00
|
|
|
// Keep track of the PHIs that are in use.
|
|
|
|
SmallPtrSet<VNInfo*, 8> UsedPHIs;
|
|
|
|
|
2011-02-08 00:03:05 +00:00
|
|
|
// Extend intervals to reach all uses in WorkList.
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
SlotIndex Idx = WorkList.back().first;
|
|
|
|
VNInfo *VNI = WorkList.back().second;
|
|
|
|
WorkList.pop_back();
|
2011-11-13 23:53:25 +00:00
|
|
|
const MachineBasicBlock *MBB = getMBBFromIndex(Idx.getPrevSlot());
|
2011-02-08 00:03:05 +00:00
|
|
|
SlotIndex BlockStart = getMBBStartIdx(MBB);
|
2011-03-02 00:33:03 +00:00
|
|
|
|
|
|
|
// Extend the live range for VNI to be live at Idx.
|
2013-10-10 21:28:47 +00:00
|
|
|
if (VNInfo *ExtVNI = NewLR.extendInBlock(BlockStart, Idx)) {
|
2011-03-02 01:43:30 +00:00
|
|
|
(void)ExtVNI;
|
2011-03-02 00:33:03 +00:00
|
|
|
assert(ExtVNI == VNI && "Unexpected existing value number");
|
|
|
|
// Is this a PHIDef we haven't seen before?
|
2011-03-03 00:20:51 +00:00
|
|
|
if (!VNI->isPHIDef() || VNI->def != BlockStart || !UsedPHIs.insert(VNI))
|
2011-03-02 00:33:03 +00:00
|
|
|
continue;
|
|
|
|
// The PHI is live, make sure the predecessors are live-out.
|
|
|
|
for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
|
|
|
|
PE = MBB->pred_end(); PI != PE; ++PI) {
|
2011-09-15 15:24:16 +00:00
|
|
|
if (!LiveOut.insert(*PI))
|
|
|
|
continue;
|
2011-11-13 23:53:25 +00:00
|
|
|
SlotIndex Stop = getMBBEndIdx(*PI);
|
2011-03-02 00:33:03 +00:00
|
|
|
// A predecessor is not required to have a live-out value for a PHI.
|
2011-11-13 23:53:25 +00:00
|
|
|
if (VNInfo *PVNI = li->getVNInfoBefore(Stop))
|
2011-03-02 00:33:03 +00:00
|
|
|
WorkList.push_back(std::make_pair(Stop, PVNI));
|
2011-02-08 00:03:05 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// VNI is live-in to MBB.
|
|
|
|
DEBUG(dbgs() << " live-in at " << BlockStart << '\n');
|
2013-10-10 21:28:47 +00:00
|
|
|
NewLR.addSegment(LiveRange::Segment(BlockStart, Idx, VNI));
|
2011-02-08 00:03:05 +00:00
|
|
|
|
|
|
|
// Make sure VNI is live-out from the predecessors.
|
|
|
|
for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
|
|
|
|
PE = MBB->pred_end(); PI != PE; ++PI) {
|
2011-09-15 15:24:16 +00:00
|
|
|
if (!LiveOut.insert(*PI))
|
|
|
|
continue;
|
2011-11-13 23:53:25 +00:00
|
|
|
SlotIndex Stop = getMBBEndIdx(*PI);
|
|
|
|
assert(li->getVNInfoBefore(Stop) == VNI &&
|
|
|
|
"Wrong value out of predecessor");
|
2011-02-08 00:03:05 +00:00
|
|
|
WorkList.push_back(std::make_pair(Stop, VNI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle dead values.
|
2011-03-17 20:37:07 +00:00
|
|
|
bool CanSeparate = false;
|
2014-06-03 22:42:10 +00:00
|
|
|
computeDeadValues(li, NewLR, &CanSeparate, dead);
|
|
|
|
|
|
|
|
// Move the trimmed segments back.
|
|
|
|
li->segments.swap(NewLR.segments);
|
|
|
|
DEBUG(dbgs() << "Shrunk: " << *li << '\n');
|
|
|
|
return CanSeparate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LiveIntervals::computeDeadValues(LiveInterval *li,
|
|
|
|
LiveRange &LR,
|
|
|
|
bool *CanSeparate,
|
|
|
|
SmallVectorImpl<MachineInstr*> *dead) {
|
2011-02-08 00:03:05 +00:00
|
|
|
for (LiveInterval::vni_iterator I = li->vni_begin(), E = li->vni_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
VNInfo *VNI = *I;
|
|
|
|
if (VNI->isUnused())
|
|
|
|
continue;
|
2014-06-03 22:42:10 +00:00
|
|
|
LiveRange::iterator LRI = LR.FindSegmentContaining(VNI->def);
|
|
|
|
assert(LRI != LR.end() && "Missing segment for PHI");
|
2013-10-10 21:28:47 +00:00
|
|
|
if (LRI->end != VNI->def.getDeadSlot())
|
2011-02-08 00:03:05 +00:00
|
|
|
continue;
|
2011-03-02 00:33:01 +00:00
|
|
|
if (VNI->isPHIDef()) {
|
2011-02-08 00:03:05 +00:00
|
|
|
// This is a dead PHI. Remove it.
|
2012-08-03 20:59:32 +00:00
|
|
|
VNI->markUnused();
|
2014-06-03 22:42:10 +00:00
|
|
|
LR.removeSegment(LRI->start, LRI->end);
|
2011-03-17 20:37:07 +00:00
|
|
|
DEBUG(dbgs() << "Dead PHI at " << VNI->def << " may separate interval\n");
|
2014-06-03 22:42:10 +00:00
|
|
|
if (CanSeparate)
|
|
|
|
*CanSeparate = true;
|
2011-02-08 00:03:05 +00:00
|
|
|
} else {
|
|
|
|
// This is a dead def. Make sure the instruction knows.
|
|
|
|
MachineInstr *MI = getInstructionFromIndex(VNI->def);
|
|
|
|
assert(MI && "No instruction defining live value");
|
2012-06-04 22:39:14 +00:00
|
|
|
MI->addRegisterDead(li->reg, TRI);
|
2011-03-07 23:29:10 +00:00
|
|
|
if (dead && MI->allDefsAreDead()) {
|
2011-03-16 22:56:08 +00:00
|
|
|
DEBUG(dbgs() << "All defs dead: " << VNI->def << '\t' << *MI);
|
2011-03-07 23:29:10 +00:00
|
|
|
dead->push_back(MI);
|
|
|
|
}
|
2011-02-08 00:03:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-10 21:28:57 +00:00
|
|
|
void LiveIntervals::extendToIndices(LiveRange &LR,
|
2012-09-17 23:03:25 +00:00
|
|
|
ArrayRef<SlotIndex> Indices) {
|
|
|
|
assert(LRCalc && "LRCalc not initialized.");
|
|
|
|
LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator());
|
|
|
|
for (unsigned i = 0, e = Indices.size(); i != e; ++i)
|
2013-10-10 21:28:57 +00:00
|
|
|
LRCalc->extend(LR, Indices[i]);
|
2012-09-17 23:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LiveIntervals::pruneValue(LiveInterval *LI, SlotIndex Kill,
|
|
|
|
SmallVectorImpl<SlotIndex> *EndPoints) {
|
2013-10-10 21:28:52 +00:00
|
|
|
LiveQueryResult LRQ = LI->Query(Kill);
|
2012-09-17 23:03:25 +00:00
|
|
|
VNInfo *VNI = LRQ.valueOut();
|
|
|
|
if (!VNI)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill);
|
|
|
|
SlotIndex MBBStart, MBBEnd;
|
2014-03-02 13:30:33 +00:00
|
|
|
std::tie(MBBStart, MBBEnd) = Indexes->getMBBRange(KillMBB);
|
2012-09-17 23:03:25 +00:00
|
|
|
|
|
|
|
// If VNI isn't live out from KillMBB, the value is trivially pruned.
|
|
|
|
if (LRQ.endPoint() < MBBEnd) {
|
2013-10-10 21:28:43 +00:00
|
|
|
LI->removeSegment(Kill, LRQ.endPoint());
|
2012-09-17 23:03:25 +00:00
|
|
|
if (EndPoints) EndPoints->push_back(LRQ.endPoint());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// VNI is live out of KillMBB.
|
2013-10-10 21:28:43 +00:00
|
|
|
LI->removeSegment(Kill, MBBEnd);
|
2012-09-17 23:03:25 +00:00
|
|
|
if (EndPoints) EndPoints->push_back(MBBEnd);
|
|
|
|
|
2012-10-13 16:15:31 +00:00
|
|
|
// Find all blocks that are reachable from KillMBB without leaving VNI's live
|
|
|
|
// range. It is possible that KillMBB itself is reachable, so start a DFS
|
|
|
|
// from each successor.
|
|
|
|
typedef SmallPtrSet<MachineBasicBlock*, 9> VisitedTy;
|
|
|
|
VisitedTy Visited;
|
|
|
|
for (MachineBasicBlock::succ_iterator
|
|
|
|
SuccI = KillMBB->succ_begin(), SuccE = KillMBB->succ_end();
|
|
|
|
SuccI != SuccE; ++SuccI) {
|
|
|
|
for (df_ext_iterator<MachineBasicBlock*, VisitedTy>
|
|
|
|
I = df_ext_begin(*SuccI, Visited), E = df_ext_end(*SuccI, Visited);
|
|
|
|
I != E;) {
|
|
|
|
MachineBasicBlock *MBB = *I;
|
|
|
|
|
|
|
|
// Check if VNI is live in to MBB.
|
2014-03-02 13:30:33 +00:00
|
|
|
std::tie(MBBStart, MBBEnd) = Indexes->getMBBRange(MBB);
|
2013-10-10 21:28:52 +00:00
|
|
|
LiveQueryResult LRQ = LI->Query(MBBStart);
|
2012-10-13 16:15:31 +00:00
|
|
|
if (LRQ.valueIn() != VNI) {
|
2013-10-10 21:28:43 +00:00
|
|
|
// This block isn't part of the VNI segment. Prune the search.
|
2012-10-13 16:15:31 +00:00
|
|
|
I.skipChildren();
|
|
|
|
continue;
|
|
|
|
}
|
2012-09-17 23:03:25 +00:00
|
|
|
|
2012-10-13 16:15:31 +00:00
|
|
|
// Prune the search if VNI is killed in MBB.
|
|
|
|
if (LRQ.endPoint() < MBBEnd) {
|
2013-10-10 21:28:43 +00:00
|
|
|
LI->removeSegment(MBBStart, LRQ.endPoint());
|
2012-10-13 16:15:31 +00:00
|
|
|
if (EndPoints) EndPoints->push_back(LRQ.endPoint());
|
|
|
|
I.skipChildren();
|
|
|
|
continue;
|
|
|
|
}
|
2012-09-17 23:03:25 +00:00
|
|
|
|
2012-10-13 16:15:31 +00:00
|
|
|
// VNI is live through MBB.
|
2013-10-10 21:28:43 +00:00
|
|
|
LI->removeSegment(MBBStart, MBBEnd);
|
2012-10-13 16:15:31 +00:00
|
|
|
if (EndPoints) EndPoints->push_back(MBBEnd);
|
|
|
|
++I;
|
2012-09-17 23:03:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-08 00:03:05 +00:00
|
|
|
|
2007-11-12 06:35:08 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Register allocator hooks.
|
|
|
|
//
|
|
|
|
|
2012-09-06 18:15:18 +00:00
|
|
|
void LiveIntervals::addKillFlags(const VirtRegMap *VRM) {
|
|
|
|
// Keep track of regunit ranges.
|
2013-10-10 21:29:02 +00:00
|
|
|
SmallVector<std::pair<LiveRange*, LiveRange::iterator>, 8> RU;
|
2012-09-06 18:15:18 +00:00
|
|
|
|
2012-06-20 23:23:59 +00:00
|
|
|
for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
|
2012-06-04 22:39:14 +00:00
|
|
|
if (MRI->reg_nodbg_empty(Reg))
|
2011-02-08 21:13:03 +00:00
|
|
|
continue;
|
2012-06-20 23:23:59 +00:00
|
|
|
LiveInterval *LI = &getInterval(Reg);
|
2012-09-06 18:15:18 +00:00
|
|
|
if (LI->empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Find the regunit intervals for the assigned register. They may overlap
|
|
|
|
// the virtual register live range, cancelling any kills.
|
|
|
|
RU.clear();
|
|
|
|
for (MCRegUnitIterator Units(VRM->getPhys(Reg), TRI); Units.isValid();
|
|
|
|
++Units) {
|
2013-10-10 21:29:02 +00:00
|
|
|
LiveRange &RURanges = getRegUnit(*Units);
|
|
|
|
if (RURanges.empty())
|
2012-09-06 18:15:18 +00:00
|
|
|
continue;
|
2013-10-10 21:29:02 +00:00
|
|
|
RU.push_back(std::make_pair(&RURanges, RURanges.find(LI->begin()->end)));
|
2012-09-06 18:15:18 +00:00
|
|
|
}
|
2011-02-08 21:13:03 +00:00
|
|
|
|
2013-10-10 21:28:43 +00:00
|
|
|
// Every instruction that kills Reg corresponds to a segment range end
|
|
|
|
// point.
|
2011-02-08 21:13:03 +00:00
|
|
|
for (LiveInterval::iterator RI = LI->begin(), RE = LI->end(); RI != RE;
|
|
|
|
++RI) {
|
2011-11-13 20:45:27 +00:00
|
|
|
// A block index indicates an MBB edge.
|
|
|
|
if (RI->end.isBlock())
|
2011-02-08 21:13:03 +00:00
|
|
|
continue;
|
|
|
|
MachineInstr *MI = getInstructionFromIndex(RI->end);
|
|
|
|
if (!MI)
|
|
|
|
continue;
|
2012-09-06 18:15:18 +00:00
|
|
|
|
2013-10-04 16:52:58 +00:00
|
|
|
// Check if any of the regunits are live beyond the end of RI. That could
|
2012-09-06 18:15:18 +00:00
|
|
|
// happen when a physreg is defined as a copy of a virtreg:
|
|
|
|
//
|
|
|
|
// %EAX = COPY %vreg5
|
|
|
|
// FOO %vreg5 <--- MI, cancel kill because %EAX is live.
|
|
|
|
// BAR %EAX<kill>
|
|
|
|
//
|
|
|
|
// There should be no kill flag on FOO when %vreg5 is rewritten as %EAX.
|
|
|
|
bool CancelKill = false;
|
|
|
|
for (unsigned u = 0, e = RU.size(); u != e; ++u) {
|
2013-10-10 21:29:02 +00:00
|
|
|
LiveRange &RRanges = *RU[u].first;
|
|
|
|
LiveRange::iterator &I = RU[u].second;
|
|
|
|
if (I == RRanges.end())
|
2012-09-06 18:15:18 +00:00
|
|
|
continue;
|
2013-10-10 21:29:02 +00:00
|
|
|
I = RRanges.advanceTo(I, RI->end);
|
|
|
|
if (I == RRanges.end() || I->start >= RI->end)
|
2012-09-06 18:15:18 +00:00
|
|
|
continue;
|
|
|
|
// I is overlapping RI.
|
|
|
|
CancelKill = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (CancelKill)
|
2014-04-14 00:51:57 +00:00
|
|
|
MI->clearRegisterKills(Reg, nullptr);
|
2012-09-06 18:15:18 +00:00
|
|
|
else
|
2014-04-14 00:51:57 +00:00
|
|
|
MI->addRegisterKilled(Reg, nullptr);
|
2011-02-08 21:13:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-10 01:23:55 +00:00
|
|
|
MachineBasicBlock*
|
|
|
|
LiveIntervals::intervalIsInOneMBB(const LiveInterval &LI) const {
|
|
|
|
// A local live range must be fully contained inside the block, meaning it is
|
|
|
|
// defined and killed at instructions, not at block boundaries. It is not
|
|
|
|
// live in or or out of any block.
|
|
|
|
//
|
|
|
|
// It is technically possible to have a PHI-defined live range identical to a
|
|
|
|
// single block, but we are going to return false in that case.
|
|
|
|
|
|
|
|
SlotIndex Start = LI.beginIndex();
|
|
|
|
if (Start.isBlock())
|
2014-04-14 00:51:57 +00:00
|
|
|
return nullptr;
|
2012-02-10 01:23:55 +00:00
|
|
|
|
|
|
|
SlotIndex Stop = LI.endIndex();
|
|
|
|
if (Stop.isBlock())
|
2014-04-14 00:51:57 +00:00
|
|
|
return nullptr;
|
2012-02-10 01:23:55 +00:00
|
|
|
|
|
|
|
// getMBBFromIndex doesn't need to search the MBB table when both indexes
|
|
|
|
// belong to proper instructions.
|
2012-06-04 22:39:14 +00:00
|
|
|
MachineBasicBlock *MBB1 = Indexes->getMBBFromIndex(Start);
|
|
|
|
MachineBasicBlock *MBB2 = Indexes->getMBBFromIndex(Stop);
|
2014-04-14 00:51:57 +00:00
|
|
|
return MBB1 == MBB2 ? MBB1 : nullptr;
|
Live interval splitting:
When a live interval is being spilled, rather than creating short, non-spillable
intervals for every def / use, split the interval at BB boundaries. That is, for
every BB where the live interval is defined or used, create a new interval that
covers all the defs and uses in the BB.
This is designed to eliminate one common problem: multiple reloads of the same
value in a single basic block. Note, it does *not* decrease the number of spills
since no copies are inserted so the split intervals are *connected* through
spill and reloads (or rematerialization). The newly created intervals can be
spilled again, in that case, since it does not span multiple basic blocks, it's
spilled in the usual manner. However, it can reuse the same stack slot as the
previously split interval.
This is currently controlled by -split-intervals-at-bb.
llvm-svn: 44198
2007-11-17 00:40:40 +00:00
|
|
|
}
|
|
|
|
|
2012-08-03 20:10:24 +00:00
|
|
|
bool
|
|
|
|
LiveIntervals::hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const {
|
|
|
|
for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const VNInfo *PHI = *I;
|
|
|
|
if (PHI->isUnused() || !PHI->isPHIDef())
|
|
|
|
continue;
|
|
|
|
const MachineBasicBlock *PHIMBB = getMBBFromIndex(PHI->def);
|
|
|
|
// Conservatively return true instead of scanning huge predecessor lists.
|
|
|
|
if (PHIMBB->pred_size() > 100)
|
|
|
|
return true;
|
|
|
|
for (MachineBasicBlock::const_pred_iterator
|
|
|
|
PI = PHIMBB->pred_begin(), PE = PHIMBB->pred_end(); PI != PE; ++PI)
|
|
|
|
if (VNI == LI.getVNInfoBefore(Indexes->getMBBEndIdx(*PI)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-01 20:59:38 +00:00
|
|
|
float
|
2013-12-14 00:53:32 +00:00
|
|
|
LiveIntervals::getSpillWeight(bool isDef, bool isUse,
|
|
|
|
const MachineBlockFrequencyInfo *MBFI,
|
|
|
|
const MachineInstr *MI) {
|
|
|
|
BlockFrequency Freq = MBFI->getBlockFreq(MI->getParent());
|
2013-12-14 02:37:38 +00:00
|
|
|
const float Scale = 1.0f / MBFI->getEntryFreq();
|
2013-12-14 00:53:32 +00:00
|
|
|
return (isDef + isUse) * (Freq.getFrequency() * Scale);
|
2010-03-01 20:59:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 21:28:47 +00:00
|
|
|
LiveRange::Segment
|
2013-10-10 21:28:43 +00:00
|
|
|
LiveIntervals::addSegmentToEndOfBlock(unsigned reg, MachineInstr* startInst) {
|
2013-08-14 23:50:16 +00:00
|
|
|
LiveInterval& Interval = createEmptyInterval(reg);
|
2008-06-05 17:15:43 +00:00
|
|
|
VNInfo* VN = Interval.getNextValue(
|
2011-11-13 20:45:27 +00:00
|
|
|
SlotIndex(getInstructionIndex(startInst).getRegSlot()),
|
2012-02-04 05:20:49 +00:00
|
|
|
getVNInfoAllocator());
|
2013-10-10 21:28:47 +00:00
|
|
|
LiveRange::Segment S(
|
2011-11-13 20:45:27 +00:00
|
|
|
SlotIndex(getInstructionIndex(startInst).getRegSlot()),
|
2009-12-22 00:11:50 +00:00
|
|
|
getMBBEndIdx(startInst->getParent()), VN);
|
2013-10-10 21:28:43 +00:00
|
|
|
Interval.addSegment(S);
|
2010-08-12 20:01:23 +00:00
|
|
|
|
2013-10-10 21:28:43 +00:00
|
|
|
return S;
|
2008-06-05 17:15:43 +00:00
|
|
|
}
|
2012-02-08 17:33:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Register mask functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
bool LiveIntervals::checkRegMaskInterference(LiveInterval &LI,
|
|
|
|
BitVector &UsableRegs) {
|
|
|
|
if (LI.empty())
|
|
|
|
return false;
|
2012-02-10 01:31:31 +00:00
|
|
|
LiveInterval::iterator LiveI = LI.begin(), LiveE = LI.end();
|
|
|
|
|
|
|
|
// Use a smaller arrays for local live ranges.
|
|
|
|
ArrayRef<SlotIndex> Slots;
|
|
|
|
ArrayRef<const uint32_t*> Bits;
|
|
|
|
if (MachineBasicBlock *MBB = intervalIsInOneMBB(LI)) {
|
|
|
|
Slots = getRegMaskSlotsInBlock(MBB->getNumber());
|
|
|
|
Bits = getRegMaskBitsInBlock(MBB->getNumber());
|
|
|
|
} else {
|
|
|
|
Slots = getRegMaskSlots();
|
|
|
|
Bits = getRegMaskBits();
|
|
|
|
}
|
2012-02-08 17:33:45 +00:00
|
|
|
|
|
|
|
// We are going to enumerate all the register mask slots contained in LI.
|
|
|
|
// Start with a binary search of RegMaskSlots to find a starting point.
|
|
|
|
ArrayRef<SlotIndex>::iterator SlotI =
|
|
|
|
std::lower_bound(Slots.begin(), Slots.end(), LiveI->start);
|
|
|
|
ArrayRef<SlotIndex>::iterator SlotE = Slots.end();
|
|
|
|
|
|
|
|
// No slots in range, LI begins after the last call.
|
|
|
|
if (SlotI == SlotE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Found = false;
|
|
|
|
for (;;) {
|
|
|
|
assert(*SlotI >= LiveI->start);
|
|
|
|
// Loop over all slots overlapping this segment.
|
|
|
|
while (*SlotI < LiveI->end) {
|
|
|
|
// *SlotI overlaps LI. Collect mask bits.
|
|
|
|
if (!Found) {
|
|
|
|
// This is the first overlap. Initialize UsableRegs to all ones.
|
|
|
|
UsableRegs.clear();
|
2012-06-04 22:39:14 +00:00
|
|
|
UsableRegs.resize(TRI->getNumRegs(), true);
|
2012-02-08 17:33:45 +00:00
|
|
|
Found = true;
|
|
|
|
}
|
|
|
|
// Remove usable registers clobbered by this mask.
|
2012-02-10 01:31:31 +00:00
|
|
|
UsableRegs.clearBitsNotInMask(Bits[SlotI-Slots.begin()]);
|
2012-02-08 17:33:45 +00:00
|
|
|
if (++SlotI == SlotE)
|
|
|
|
return Found;
|
|
|
|
}
|
|
|
|
// *SlotI is beyond the current LI segment.
|
|
|
|
LiveI = LI.advanceTo(LiveI, *SlotI);
|
|
|
|
if (LiveI == LiveE)
|
|
|
|
return Found;
|
|
|
|
// Advance SlotI until it overlaps.
|
|
|
|
while (*SlotI < LiveI->start)
|
|
|
|
if (++SlotI == SlotE)
|
|
|
|
return Found;
|
|
|
|
}
|
|
|
|
}
|
2012-02-17 18:44:18 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IntervalUpdate class.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-02-21 00:00:36 +00:00
|
|
|
// HMEditor is a toolkit used by handleMove to trim or extend live intervals.
|
2012-02-17 18:44:18 +00:00
|
|
|
class LiveIntervals::HMEditor {
|
|
|
|
private:
|
2012-02-17 23:43:40 +00:00
|
|
|
LiveIntervals& LIS;
|
|
|
|
const MachineRegisterInfo& MRI;
|
|
|
|
const TargetRegisterInfo& TRI;
|
2012-10-12 21:31:57 +00:00
|
|
|
SlotIndex OldIdx;
|
2012-02-17 23:43:40 +00:00
|
|
|
SlotIndex NewIdx;
|
2013-10-10 21:29:02 +00:00
|
|
|
SmallPtrSet<LiveRange*, 8> Updated;
|
2012-10-16 00:22:51 +00:00
|
|
|
bool UpdateFlags;
|
2012-02-19 07:13:05 +00:00
|
|
|
|
2012-02-17 18:44:18 +00:00
|
|
|
public:
|
2012-02-17 23:43:40 +00:00
|
|
|
HMEditor(LiveIntervals& LIS, const MachineRegisterInfo& MRI,
|
2012-10-12 21:31:57 +00:00
|
|
|
const TargetRegisterInfo& TRI,
|
2012-10-16 00:22:51 +00:00
|
|
|
SlotIndex OldIdx, SlotIndex NewIdx, bool UpdateFlags)
|
|
|
|
: LIS(LIS), MRI(MRI), TRI(TRI), OldIdx(OldIdx), NewIdx(NewIdx),
|
|
|
|
UpdateFlags(UpdateFlags) {}
|
|
|
|
|
|
|
|
// FIXME: UpdateFlags is a workaround that creates live intervals for all
|
|
|
|
// physregs, even those that aren't needed for regalloc, in order to update
|
|
|
|
// kill flags. This is wasteful. Eventually, LiveVariables will strip all kill
|
|
|
|
// flags, and postRA passes will use a live register utility instead.
|
2013-10-10 21:29:02 +00:00
|
|
|
LiveRange *getRegUnitLI(unsigned Unit) {
|
2012-10-16 00:22:51 +00:00
|
|
|
if (UpdateFlags)
|
|
|
|
return &LIS.getRegUnit(Unit);
|
|
|
|
return LIS.getCachedRegUnit(Unit);
|
|
|
|
}
|
2012-10-12 21:31:57 +00:00
|
|
|
|
|
|
|
/// Update all live ranges touched by MI, assuming a move from OldIdx to
|
|
|
|
/// NewIdx.
|
|
|
|
void updateAllRanges(MachineInstr *MI) {
|
|
|
|
DEBUG(dbgs() << "handleMove " << OldIdx << " -> " << NewIdx << ": " << *MI);
|
|
|
|
bool hasRegMask = false;
|
|
|
|
for (MIOperands MO(MI); MO.isValid(); ++MO) {
|
|
|
|
if (MO->isRegMask())
|
|
|
|
hasRegMask = true;
|
|
|
|
if (!MO->isReg())
|
2012-02-21 22:29:38 +00:00
|
|
|
continue;
|
2012-10-12 21:31:57 +00:00
|
|
|
// Aggressively clear all kill flags.
|
|
|
|
// They are reinserted by VirtRegRewriter.
|
|
|
|
if (MO->isUse())
|
|
|
|
MO->setIsKill(false);
|
2012-02-19 07:13:05 +00:00
|
|
|
|
2012-10-12 21:31:57 +00:00
|
|
|
unsigned Reg = MO->getReg();
|
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg)) {
|
2013-10-10 21:29:02 +00:00
|
|
|
LiveInterval &LI = LIS.getInterval(Reg);
|
|
|
|
updateRange(LI, Reg);
|
2012-10-12 21:31:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-02-21 22:29:38 +00:00
|
|
|
|
2012-10-12 21:31:57 +00:00
|
|
|
// For physregs, only update the regunits that actually have a
|
|
|
|
// precomputed live range.
|
|
|
|
for (MCRegUnitIterator Units(Reg, &TRI); Units.isValid(); ++Units)
|
2013-10-10 21:29:02 +00:00
|
|
|
if (LiveRange *LR = getRegUnitLI(*Units))
|
|
|
|
updateRange(*LR, *Units);
|
2012-10-12 21:31:57 +00:00
|
|
|
}
|
|
|
|
if (hasRegMask)
|
|
|
|
updateRegMaskSlots();
|
2012-02-19 07:13:05 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 03:00:30 +00:00
|
|
|
private:
|
2012-10-12 21:31:57 +00:00
|
|
|
/// Update a single live range, assuming an instruction has been moved from
|
|
|
|
/// OldIdx to NewIdx.
|
2013-10-10 21:29:02 +00:00
|
|
|
void updateRange(LiveRange &LR, unsigned Reg) {
|
|
|
|
if (!Updated.insert(&LR))
|
2012-10-12 21:31:57 +00:00
|
|
|
return;
|
|
|
|
DEBUG({
|
|
|
|
dbgs() << " ";
|
2013-10-10 21:29:02 +00:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg))
|
|
|
|
dbgs() << PrintReg(Reg);
|
2012-10-12 21:31:57 +00:00
|
|
|
else
|
2013-10-10 21:29:02 +00:00
|
|
|
dbgs() << PrintRegUnit(Reg, &TRI);
|
|
|
|
dbgs() << ":\t" << LR << '\n';
|
2012-10-12 21:31:57 +00:00
|
|
|
});
|
|
|
|
if (SlotIndex::isEarlierInstr(OldIdx, NewIdx))
|
2013-10-10 21:29:02 +00:00
|
|
|
handleMoveDown(LR);
|
2012-10-12 21:31:57 +00:00
|
|
|
else
|
2013-10-10 21:29:02 +00:00
|
|
|
handleMoveUp(LR, Reg);
|
|
|
|
DEBUG(dbgs() << " -->\t" << LR << '\n');
|
|
|
|
LR.verify();
|
2012-10-12 21:31:57 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 21:29:02 +00:00
|
|
|
/// Update LR to reflect an instruction has been moved downwards from OldIdx
|
2012-10-12 21:31:57 +00:00
|
|
|
/// to NewIdx.
|
|
|
|
///
|
|
|
|
/// 1. Live def at OldIdx:
|
|
|
|
/// Move def to NewIdx, assert endpoint after NewIdx.
|
|
|
|
///
|
|
|
|
/// 2. Live def at OldIdx, killed at NewIdx:
|
|
|
|
/// Change to dead def at NewIdx.
|
|
|
|
/// (Happens when bundling def+kill together).
|
|
|
|
///
|
|
|
|
/// 3. Dead def at OldIdx:
|
|
|
|
/// Move def to NewIdx, possibly across another live value.
|
|
|
|
///
|
|
|
|
/// 4. Def at OldIdx AND at NewIdx:
|
2013-10-10 21:28:43 +00:00
|
|
|
/// Remove segment [OldIdx;NewIdx) and value defined at OldIdx.
|
2012-10-12 21:31:57 +00:00
|
|
|
/// (Happens when bundling multiple defs together).
|
|
|
|
///
|
|
|
|
/// 5. Value read at OldIdx, killed before NewIdx:
|
|
|
|
/// Extend kill to NewIdx.
|
|
|
|
///
|
2013-10-10 21:29:02 +00:00
|
|
|
void handleMoveDown(LiveRange &LR) {
|
2012-10-12 21:31:57 +00:00
|
|
|
// First look for a kill at OldIdx.
|
2013-10-10 21:29:02 +00:00
|
|
|
LiveRange::iterator I = LR.find(OldIdx.getBaseIndex());
|
|
|
|
LiveRange::iterator E = LR.end();
|
|
|
|
// Is LR even live at OldIdx?
|
2012-10-12 21:31:57 +00:00
|
|
|
if (I == E || SlotIndex::isEarlierInstr(OldIdx, I->start))
|
|
|
|
return;
|
2012-02-19 03:00:30 +00:00
|
|
|
|
2012-10-12 21:31:57 +00:00
|
|
|
// Handle a live-in value.
|
|
|
|
if (!SlotIndex::isSameInstr(I->start, OldIdx)) {
|
|
|
|
bool isKill = SlotIndex::isSameInstr(OldIdx, I->end);
|
|
|
|
// If the live-in value already extends to NewIdx, there is nothing to do.
|
|
|
|
if (!SlotIndex::isEarlierInstr(I->end, NewIdx))
|
2012-02-19 03:00:30 +00:00
|
|
|
return;
|
2012-10-12 21:31:57 +00:00
|
|
|
// Aggressively remove all kill flags from the old kill point.
|
|
|
|
// Kill flags shouldn't be used while live intervals exist, they will be
|
|
|
|
// reinserted by VirtRegRewriter.
|
|
|
|
if (MachineInstr *KillMI = LIS.getInstructionFromIndex(I->end))
|
|
|
|
for (MIBundleOperands MO(KillMI); MO.isValid(); ++MO)
|
|
|
|
if (MO->isReg() && MO->isUse())
|
|
|
|
MO->setIsKill(false);
|
2013-10-10 21:29:02 +00:00
|
|
|
// Adjust I->end to reach NewIdx. This may temporarily make LR invalid by
|
2012-10-12 21:31:57 +00:00
|
|
|
// overlapping ranges. Case 5 above.
|
|
|
|
I->end = NewIdx.getRegSlot(I->end.isEarlyClobber());
|
|
|
|
// If this was a kill, there may also be a def. Otherwise we're done.
|
|
|
|
if (!isKill)
|
2012-02-19 03:00:30 +00:00
|
|
|
return;
|
2012-10-12 21:31:57 +00:00
|
|
|
++I;
|
2012-06-19 23:50:18 +00:00
|
|
|
}
|
2012-02-19 03:00:30 +00:00
|
|
|
|
2012-10-12 21:31:57 +00:00
|
|
|
// Check for a def at OldIdx.
|
|
|
|
if (I == E || !SlotIndex::isSameInstr(OldIdx, I->start))
|
|
|
|
return;
|
|
|
|
// We have a def at OldIdx.
|
|
|
|
VNInfo *DefVNI = I->valno;
|
|
|
|
assert(DefVNI->def == I->start && "Inconsistent def");
|
|
|
|
DefVNI->def = NewIdx.getRegSlot(I->start.isEarlyClobber());
|
|
|
|
// If the defined value extends beyond NewIdx, just move the def down.
|
|
|
|
// This is case 1 above.
|
|
|
|
if (SlotIndex::isEarlierInstr(NewIdx, I->end)) {
|
|
|
|
I->start = DefVNI->def;
|
|
|
|
return;
|
2012-06-19 23:50:18 +00:00
|
|
|
}
|
2012-10-12 21:31:57 +00:00
|
|
|
// The remaining possibilities are now:
|
|
|
|
// 2. Live def at OldIdx, killed at NewIdx: isSameInstr(I->end, NewIdx).
|
|
|
|
// 3. Dead def at OldIdx: I->end = OldIdx.getDeadSlot().
|
|
|
|
// In either case, it is possible that there is an existing def at NewIdx.
|
|
|
|
assert((I->end == OldIdx.getDeadSlot() ||
|
|
|
|
SlotIndex::isSameInstr(I->end, NewIdx)) &&
|
|
|
|
"Cannot move def below kill");
|
2013-10-10 21:29:02 +00:00
|
|
|
LiveRange::iterator NewI = LR.advanceTo(I, NewIdx.getRegSlot());
|
2012-10-12 21:31:57 +00:00
|
|
|
if (NewI != E && SlotIndex::isSameInstr(NewI->start, NewIdx)) {
|
|
|
|
// There is an existing def at NewIdx, case 4 above. The def at OldIdx is
|
|
|
|
// coalesced into that value.
|
|
|
|
assert(NewI->valno != DefVNI && "Multiple defs of value?");
|
2013-10-10 21:29:02 +00:00
|
|
|
LR.removeValNo(DefVNI);
|
2012-10-12 21:31:57 +00:00
|
|
|
return;
|
2012-02-17 18:44:18 +00:00
|
|
|
}
|
2012-10-12 21:31:57 +00:00
|
|
|
// There was no existing def at NewIdx. Turn *I into a dead def at NewIdx.
|
2013-10-10 21:29:02 +00:00
|
|
|
// If the def at OldIdx was dead, we allow it to be moved across other LR
|
2012-10-12 21:31:57 +00:00
|
|
|
// values. The new range should be placed immediately before NewI, move any
|
|
|
|
// intermediate ranges up.
|
|
|
|
assert(NewI != I && "Inconsistent iterators");
|
2014-03-02 12:27:27 +00:00
|
|
|
std::copy(std::next(I), NewI, I);
|
|
|
|
*std::prev(NewI)
|
2013-10-10 21:28:47 +00:00
|
|
|
= LiveRange::Segment(DefVNI->def, NewIdx.getDeadSlot(), DefVNI);
|
2012-02-17 18:44:18 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 21:29:02 +00:00
|
|
|
/// Update LR to reflect an instruction has been moved upwards from OldIdx
|
2012-10-12 21:31:57 +00:00
|
|
|
/// to NewIdx.
|
|
|
|
///
|
|
|
|
/// 1. Live def at OldIdx:
|
|
|
|
/// Hoist def to NewIdx.
|
|
|
|
///
|
|
|
|
/// 2. Dead def at OldIdx:
|
|
|
|
/// Hoist def+end to NewIdx, possibly move across other values.
|
|
|
|
///
|
|
|
|
/// 3. Dead def at OldIdx AND existing def at NewIdx:
|
|
|
|
/// Remove value defined at OldIdx, coalescing it with existing value.
|
|
|
|
///
|
|
|
|
/// 4. Live def at OldIdx AND existing def at NewIdx:
|
|
|
|
/// Remove value defined at NewIdx, hoist OldIdx def to NewIdx.
|
|
|
|
/// (Happens when bundling multiple defs together).
|
|
|
|
///
|
|
|
|
/// 5. Value killed at OldIdx:
|
|
|
|
/// Hoist kill to NewIdx, then scan for last kill between NewIdx and
|
|
|
|
/// OldIdx.
|
|
|
|
///
|
2013-10-10 21:29:02 +00:00
|
|
|
void handleMoveUp(LiveRange &LR, unsigned Reg) {
|
2012-10-12 21:31:57 +00:00
|
|
|
// First look for a kill at OldIdx.
|
2013-10-10 21:29:02 +00:00
|
|
|
LiveRange::iterator I = LR.find(OldIdx.getBaseIndex());
|
|
|
|
LiveRange::iterator E = LR.end();
|
|
|
|
// Is LR even live at OldIdx?
|
2012-10-12 21:31:57 +00:00
|
|
|
if (I == E || SlotIndex::isEarlierInstr(OldIdx, I->start))
|
|
|
|
return;
|
2012-02-19 07:13:05 +00:00
|
|
|
|
2012-10-12 21:31:57 +00:00
|
|
|
// Handle a live-in value.
|
|
|
|
if (!SlotIndex::isSameInstr(I->start, OldIdx)) {
|
|
|
|
// If the live-in value isn't killed here, there is nothing to do.
|
|
|
|
if (!SlotIndex::isSameInstr(OldIdx, I->end))
|
|
|
|
return;
|
|
|
|
// Adjust I->end to end at NewIdx. If we are hoisting a kill above
|
|
|
|
// another use, we need to search for that use. Case 5 above.
|
|
|
|
I->end = NewIdx.getRegSlot(I->end.isEarlyClobber());
|
|
|
|
++I;
|
|
|
|
// If OldIdx also defines a value, there couldn't have been another use.
|
|
|
|
if (I == E || !SlotIndex::isSameInstr(I->start, OldIdx)) {
|
|
|
|
// No def, search for the new kill.
|
|
|
|
// This can never be an early clobber kill since there is no def.
|
2014-03-02 12:27:27 +00:00
|
|
|
std::prev(I)->end = findLastUseBefore(Reg).getRegSlot();
|
2012-10-12 21:31:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-02-19 07:13:05 +00:00
|
|
|
}
|
|
|
|
|
2012-10-12 21:31:57 +00:00
|
|
|
// Now deal with the def at OldIdx.
|
|
|
|
assert(I != E && SlotIndex::isSameInstr(I->start, OldIdx) && "No def?");
|
|
|
|
VNInfo *DefVNI = I->valno;
|
|
|
|
assert(DefVNI->def == I->start && "Inconsistent def");
|
|
|
|
DefVNI->def = NewIdx.getRegSlot(I->start.isEarlyClobber());
|
|
|
|
|
|
|
|
// Check for an existing def at NewIdx.
|
2013-10-10 21:29:02 +00:00
|
|
|
LiveRange::iterator NewI = LR.find(NewIdx.getRegSlot());
|
2012-10-12 21:31:57 +00:00
|
|
|
if (SlotIndex::isSameInstr(NewI->start, NewIdx)) {
|
|
|
|
assert(NewI->valno != DefVNI && "Same value defined more than once?");
|
|
|
|
// There is an existing def at NewIdx.
|
|
|
|
if (I->end.isDead()) {
|
|
|
|
// Case 3: Remove the dead def at OldIdx.
|
2013-10-10 21:29:02 +00:00
|
|
|
LR.removeValNo(DefVNI);
|
2012-10-12 21:31:57 +00:00
|
|
|
return;
|
2012-02-19 07:13:05 +00:00
|
|
|
}
|
2012-10-12 21:31:57 +00:00
|
|
|
// Case 4: Replace def at NewIdx with live def at OldIdx.
|
|
|
|
I->start = DefVNI->def;
|
2013-10-10 21:29:02 +00:00
|
|
|
LR.removeValNo(NewI->valno);
|
2012-10-12 21:31:57 +00:00
|
|
|
return;
|
2012-02-19 07:13:05 +00:00
|
|
|
}
|
|
|
|
|
2012-10-12 21:31:57 +00:00
|
|
|
// There is no existing def at NewIdx. Hoist DefVNI.
|
|
|
|
if (!I->end.isDead()) {
|
|
|
|
// Leave the end point of a live def.
|
|
|
|
I->start = DefVNI->def;
|
|
|
|
return;
|
2012-02-19 07:13:05 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 21:29:02 +00:00
|
|
|
// DefVNI is a dead def. It may have been moved across other values in LR,
|
2012-10-12 21:31:57 +00:00
|
|
|
// so move I up to NewI. Slide [NewI;I) down one position.
|
2014-03-02 12:27:27 +00:00
|
|
|
std::copy_backward(NewI, I, std::next(I));
|
2013-10-10 21:28:47 +00:00
|
|
|
*NewI = LiveRange::Segment(DefVNI->def, NewIdx.getDeadSlot(), DefVNI);
|
2012-02-19 07:13:05 +00:00
|
|
|
}
|
|
|
|
|
2012-10-12 21:31:57 +00:00
|
|
|
void updateRegMaskSlots() {
|
2012-02-19 03:00:30 +00:00
|
|
|
SmallVectorImpl<SlotIndex>::iterator RI =
|
|
|
|
std::lower_bound(LIS.RegMaskSlots.begin(), LIS.RegMaskSlots.end(),
|
|
|
|
OldIdx);
|
2012-11-09 19:18:49 +00:00
|
|
|
assert(RI != LIS.RegMaskSlots.end() && *RI == OldIdx.getRegSlot() &&
|
|
|
|
"No RegMask at OldIdx.");
|
|
|
|
*RI = NewIdx.getRegSlot();
|
|
|
|
assert((RI == LIS.RegMaskSlots.begin() ||
|
2014-03-02 12:27:27 +00:00
|
|
|
SlotIndex::isEarlierInstr(*std::prev(RI), *RI)) &&
|
|
|
|
"Cannot move regmask instruction above another call");
|
|
|
|
assert((std::next(RI) == LIS.RegMaskSlots.end() ||
|
|
|
|
SlotIndex::isEarlierInstr(*RI, *std::next(RI))) &&
|
|
|
|
"Cannot move regmask instruction below another call");
|
2012-02-17 18:44:18 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 03:00:30 +00:00
|
|
|
// Return the last use of reg between NewIdx and OldIdx.
|
2012-10-12 21:31:57 +00:00
|
|
|
SlotIndex findLastUseBefore(unsigned Reg) {
|
2012-09-12 06:56:16 +00:00
|
|
|
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg)) {
|
2013-03-08 18:08:57 +00:00
|
|
|
SlotIndex LastUse = NewIdx;
|
2014-03-13 06:02:25 +00:00
|
|
|
for (MachineRegisterInfo::use_instr_nodbg_iterator
|
|
|
|
UI = MRI.use_instr_nodbg_begin(Reg),
|
|
|
|
UE = MRI.use_instr_nodbg_end();
|
|
|
|
UI != UE; ++UI) {
|
2012-09-12 06:56:16 +00:00
|
|
|
const MachineInstr* MI = &*UI;
|
|
|
|
SlotIndex InstSlot = LIS.getSlotIndexes()->getInstructionIndex(MI);
|
|
|
|
if (InstSlot > LastUse && InstSlot < OldIdx)
|
|
|
|
LastUse = InstSlot;
|
|
|
|
}
|
2013-03-08 18:08:57 +00:00
|
|
|
return LastUse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a regunit interval, so scanning the use list could be very
|
|
|
|
// expensive. Scan upwards from OldIdx instead.
|
|
|
|
assert(NewIdx < OldIdx && "Expected upwards move");
|
|
|
|
SlotIndexes *Indexes = LIS.getSlotIndexes();
|
|
|
|
MachineBasicBlock *MBB = Indexes->getMBBFromIndex(NewIdx);
|
|
|
|
|
|
|
|
// OldIdx may not correspond to an instruction any longer, so set MII to
|
|
|
|
// point to the next instruction after OldIdx, or MBB->end().
|
|
|
|
MachineBasicBlock::iterator MII = MBB->end();
|
|
|
|
if (MachineInstr *MI = Indexes->getInstructionFromIndex(
|
|
|
|
Indexes->getNextNonNullIndex(OldIdx)))
|
|
|
|
if (MI->getParent() == MBB)
|
|
|
|
MII = MI;
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator Begin = MBB->begin();
|
|
|
|
while (MII != Begin) {
|
|
|
|
if ((--MII)->isDebugValue())
|
|
|
|
continue;
|
|
|
|
SlotIndex Idx = Indexes->getInstructionIndex(MII);
|
|
|
|
|
|
|
|
// Stop searching when NewIdx is reached.
|
|
|
|
if (!SlotIndex::isEarlierInstr(NewIdx, Idx))
|
|
|
|
return NewIdx;
|
|
|
|
|
|
|
|
// Check if MII uses Reg.
|
|
|
|
for (MIBundleOperands MO(MII); MO.isValid(); ++MO)
|
|
|
|
if (MO->isReg() &&
|
|
|
|
TargetRegisterInfo::isPhysicalRegister(MO->getReg()) &&
|
|
|
|
TRI.hasRegUnit(MO->getReg(), Reg))
|
|
|
|
return Idx;
|
2012-02-17 18:44:18 +00:00
|
|
|
}
|
2013-03-08 18:08:57 +00:00
|
|
|
// Didn't reach NewIdx. It must be the first instruction in the block.
|
|
|
|
return NewIdx;
|
2012-02-17 18:44:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-16 00:22:51 +00:00
|
|
|
void LiveIntervals::handleMove(MachineInstr* MI, bool UpdateFlags) {
|
2012-10-12 21:31:57 +00:00
|
|
|
assert(!MI->isBundled() && "Can't handle bundled instructions yet.");
|
2012-06-04 22:39:14 +00:00
|
|
|
SlotIndex OldIndex = Indexes->getInstructionIndex(MI);
|
|
|
|
Indexes->removeMachineInstrFromMaps(MI);
|
2012-10-12 21:31:57 +00:00
|
|
|
SlotIndex NewIndex = Indexes->insertMachineInstrInMaps(MI);
|
2012-02-17 23:43:40 +00:00
|
|
|
assert(getMBBStartIdx(MI->getParent()) <= OldIndex &&
|
|
|
|
OldIndex < getMBBEndIdx(MI->getParent()) &&
|
2012-02-17 18:44:18 +00:00
|
|
|
"Cannot handle moves across basic block boundaries.");
|
|
|
|
|
2012-10-16 00:22:51 +00:00
|
|
|
HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags);
|
2012-10-12 21:31:57 +00:00
|
|
|
HME.updateAllRanges(MI);
|
2012-02-21 22:29:38 +00:00
|
|
|
}
|
|
|
|
|
2012-06-19 22:50:53 +00:00
|
|
|
void LiveIntervals::handleMoveIntoBundle(MachineInstr* MI,
|
2012-10-16 00:22:51 +00:00
|
|
|
MachineInstr* BundleStart,
|
|
|
|
bool UpdateFlags) {
|
2012-10-12 21:31:57 +00:00
|
|
|
SlotIndex OldIndex = Indexes->getInstructionIndex(MI);
|
2012-06-04 22:39:14 +00:00
|
|
|
SlotIndex NewIndex = Indexes->getInstructionIndex(BundleStart);
|
2012-10-16 00:22:51 +00:00
|
|
|
HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags);
|
2012-10-12 21:31:57 +00:00
|
|
|
HME.updateAllRanges(MI);
|
2012-02-17 18:44:18 +00:00
|
|
|
}
|
2013-02-17 00:10:44 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
LiveIntervals::repairIntervalsInRange(MachineBasicBlock *MBB,
|
2013-02-17 11:09:00 +00:00
|
|
|
MachineBasicBlock::iterator Begin,
|
|
|
|
MachineBasicBlock::iterator End,
|
2013-02-17 03:48:23 +00:00
|
|
|
ArrayRef<unsigned> OrigRegs) {
|
2013-02-20 22:10:00 +00:00
|
|
|
// Find anchor points, which are at the beginning/end of blocks or at
|
|
|
|
// instructions that already have indexes.
|
|
|
|
while (Begin != MBB->begin() && !Indexes->hasIndex(Begin))
|
|
|
|
--Begin;
|
|
|
|
while (End != MBB->end() && !Indexes->hasIndex(End))
|
|
|
|
++End;
|
|
|
|
|
2013-02-20 06:46:48 +00:00
|
|
|
SlotIndex endIdx;
|
|
|
|
if (End == MBB->end())
|
|
|
|
endIdx = getMBBEndIdx(MBB).getPrevSlot();
|
2013-02-17 11:09:00 +00:00
|
|
|
else
|
2013-02-20 06:46:48 +00:00
|
|
|
endIdx = getInstructionIndex(End);
|
2013-02-17 11:09:00 +00:00
|
|
|
|
2013-02-20 06:46:41 +00:00
|
|
|
Indexes->repairIndexesInRange(MBB, Begin, End);
|
|
|
|
|
2013-02-20 06:46:48 +00:00
|
|
|
for (MachineBasicBlock::iterator I = End; I != Begin;) {
|
|
|
|
--I;
|
|
|
|
MachineInstr *MI = I;
|
2013-02-23 10:25:25 +00:00
|
|
|
if (MI->isDebugValue())
|
|
|
|
continue;
|
2013-02-20 06:46:48 +00:00
|
|
|
for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
|
|
|
|
MOE = MI->operands_end(); MOI != MOE; ++MOI) {
|
|
|
|
if (MOI->isReg() &&
|
|
|
|
TargetRegisterInfo::isVirtualRegister(MOI->getReg()) &&
|
|
|
|
!hasInterval(MOI->getReg())) {
|
2013-08-14 23:50:16 +00:00
|
|
|
createAndComputeVirtRegInterval(MOI->getReg());
|
2013-02-20 06:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-17 00:10:44 +00:00
|
|
|
for (unsigned i = 0, e = OrigRegs.size(); i != e; ++i) {
|
|
|
|
unsigned Reg = OrigRegs[i];
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LiveInterval &LI = getInterval(Reg);
|
2013-02-20 22:09:57 +00:00
|
|
|
// FIXME: Should we support undefs that gain defs?
|
|
|
|
if (!LI.hasAtLeastOneValue())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LiveInterval::iterator LII = LI.find(endIdx);
|
|
|
|
SlotIndex lastUseIdx;
|
|
|
|
if (LII != LI.end() && LII->start < endIdx)
|
|
|
|
lastUseIdx = LII->end;
|
|
|
|
else
|
|
|
|
--LII;
|
2013-02-20 06:46:48 +00:00
|
|
|
|
2013-02-17 11:09:00 +00:00
|
|
|
for (MachineBasicBlock::iterator I = End; I != Begin;) {
|
|
|
|
--I;
|
|
|
|
MachineInstr *MI = I;
|
2013-02-23 10:25:25 +00:00
|
|
|
if (MI->isDebugValue())
|
|
|
|
continue;
|
2013-02-17 00:10:44 +00:00
|
|
|
|
2013-02-23 10:25:25 +00:00
|
|
|
SlotIndex instrIdx = getInstructionIndex(MI);
|
2013-02-20 22:09:57 +00:00
|
|
|
bool isStartValid = getInstructionFromIndex(LII->start);
|
|
|
|
bool isEndValid = getInstructionFromIndex(LII->end);
|
|
|
|
|
|
|
|
// FIXME: This doesn't currently handle early-clobber or multiple removed
|
|
|
|
// defs inside of the region to repair.
|
2013-02-17 00:10:44 +00:00
|
|
|
for (MachineInstr::mop_iterator OI = MI->operands_begin(),
|
|
|
|
OE = MI->operands_end(); OI != OE; ++OI) {
|
|
|
|
const MachineOperand &MO = *OI;
|
|
|
|
if (!MO.isReg() || MO.getReg() != Reg)
|
|
|
|
continue;
|
|
|
|
|
2013-02-20 06:46:48 +00:00
|
|
|
if (MO.isDef()) {
|
2013-02-20 22:09:57 +00:00
|
|
|
if (!isStartValid) {
|
|
|
|
if (LII->end.isDead()) {
|
|
|
|
SlotIndex prevStart;
|
|
|
|
if (LII != LI.begin())
|
2014-03-02 12:27:27 +00:00
|
|
|
prevStart = std::prev(LII)->start;
|
2013-02-20 22:09:57 +00:00
|
|
|
|
2013-10-10 21:28:43 +00:00
|
|
|
// FIXME: This could be more efficient if there was a
|
|
|
|
// removeSegment method that returned an iterator.
|
|
|
|
LI.removeSegment(*LII, true);
|
2013-02-20 22:09:57 +00:00
|
|
|
if (prevStart.isValid())
|
|
|
|
LII = LI.find(prevStart);
|
|
|
|
else
|
|
|
|
LII = LI.begin();
|
|
|
|
} else {
|
|
|
|
LII->start = instrIdx.getRegSlot();
|
|
|
|
LII->valno->def = instrIdx.getRegSlot();
|
|
|
|
if (MO.getSubReg() && !MO.isUndef())
|
|
|
|
lastUseIdx = instrIdx.getRegSlot();
|
|
|
|
else
|
|
|
|
lastUseIdx = SlotIndex();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lastUseIdx.isValid()) {
|
|
|
|
VNInfo *VNI = LI.getNextValue(instrIdx.getRegSlot(),
|
|
|
|
VNInfoAllocator);
|
2013-10-10 21:28:47 +00:00
|
|
|
LiveRange::Segment S(instrIdx.getRegSlot(),
|
|
|
|
instrIdx.getDeadSlot(), VNI);
|
2013-10-10 21:28:43 +00:00
|
|
|
LII = LI.addSegment(S);
|
2013-02-20 06:46:48 +00:00
|
|
|
} else if (LII->start != instrIdx.getRegSlot()) {
|
2013-02-20 22:09:57 +00:00
|
|
|
VNInfo *VNI = LI.getNextValue(instrIdx.getRegSlot(),
|
|
|
|
VNInfoAllocator);
|
2013-10-10 21:28:47 +00:00
|
|
|
LiveRange::Segment S(instrIdx.getRegSlot(), lastUseIdx, VNI);
|
2013-10-10 21:28:43 +00:00
|
|
|
LII = LI.addSegment(S);
|
2013-02-20 06:46:48 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 22:09:57 +00:00
|
|
|
if (MO.getSubReg() && !MO.isUndef())
|
|
|
|
lastUseIdx = instrIdx.getRegSlot();
|
|
|
|
else
|
|
|
|
lastUseIdx = SlotIndex();
|
|
|
|
} else if (MO.isUse()) {
|
|
|
|
// FIXME: This should probably be handled outside of this branch,
|
|
|
|
// either as part of the def case (for defs inside of the region) or
|
|
|
|
// after the loop over the region.
|
|
|
|
if (!isEndValid && !LII->end.isBlock())
|
2013-02-20 06:46:48 +00:00
|
|
|
LII->end = instrIdx.getRegSlot();
|
2013-02-20 22:09:57 +00:00
|
|
|
if (!lastUseIdx.isValid())
|
|
|
|
lastUseIdx = instrIdx.getRegSlot();
|
2013-02-17 00:10:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|