Several performance enhancements and cleanups from Chris.

Simplification of LiveIntervals::Interval::overlaps() and addition of
examples to overlaps() and liveAt() to make them clearer.

llvm-svn: 11028
This commit is contained in:
Alkis Evlogimenos 2004-01-31 04:56:07 +00:00
parent 729ea9e1d9
commit f2fb0fb486
2 changed files with 115 additions and 115 deletions
llvm
include/llvm/CodeGen
lib/CodeGen

View File

@ -13,8 +13,8 @@
// for register v if there is no instruction with number j' > j such // for register v if there is no instruction with number j' > j such
// that v is live at j' abd there is no instruction with number i' < i // that v is live at j' abd there is no instruction with number i' < i
// such that v is live at i'. In this implementation intervals can // such that v is live at i'. In this implementation intervals can
// have holes, i.e. an interval might look like [1,20], [50,65], // have holes, i.e. an interval might look like [1,20), [50,65),
// [1000,1001] // [1000,1001)
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -22,11 +22,7 @@
#define LLVM_CODEGEN_LIVEINTERVALS_H #define LLVM_CODEGEN_LIVEINTERVALS_H
#include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include <iostream>
#include <list> #include <list>
#include <map>
#include <vector>
namespace llvm { namespace llvm {
@ -113,6 +109,10 @@ namespace llvm {
public: public:
virtual void getAnalysisUsage(AnalysisUsage &AU) const; virtual void getAnalysisUsage(AnalysisUsage &AU) const;
virtual void releaseMemory();
/// runOnMachineFunction - pass entry point
virtual bool runOnMachineFunction(MachineFunction&);
Intervals& getIntervals() { return intervals_; } Intervals& getIntervals() { return intervals_; }
@ -134,9 +134,6 @@ namespace llvm {
unsigned rep(unsigned reg); unsigned rep(unsigned reg);
private: private:
/// runOnMachineFunction - pass entry point
bool runOnMachineFunction(MachineFunction&);
/// computeIntervals - compute live intervals /// computeIntervals - compute live intervals
void computeIntervals(); void computeIntervals();
@ -164,7 +161,10 @@ namespace llvm {
bool overlapsAliases(const Interval& lhs, const Interval& rhs) const; bool overlapsAliases(const Interval& lhs, const Interval& rhs) const;
unsigned getInstructionIndex(MachineInstr* instr) const; unsigned getInstructionIndex(MachineInstr* instr) const {
assert(mi2iMap_.count(instr) && "instruction not assigned a number");
return mi2iMap_.find(instr)->second;
}
void printRegName(unsigned reg) const; void printRegName(unsigned reg) const;
}; };

View File

@ -17,24 +17,19 @@
#define DEBUG_TYPE "liveintervals" #define DEBUG_TYPE "liveintervals"
#include "llvm/CodeGen/LiveIntervals.h" #include "llvm/CodeGen/LiveIntervals.h"
#include "llvm/Function.h"
#include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/LoopInfo.h"
#include "llvm/CodeGen/LiveVariables.h" #include "llvm/CodeGen/LiveVariables.h"
#include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h" #include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/SSARegMap.h" #include "llvm/CodeGen/SSARegMap.h"
#include "llvm/Target/MRegisterInfo.h" #include "llvm/Target/MRegisterInfo.h"
#include "llvm/Target/TargetInstrInfo.h" #include "llvm/Target/TargetInstrInfo.h"
#include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetRegInfo.h"
#include "llvm/Support/CFG.h" #include "llvm/Support/CFG.h"
#include "Support/CommandLine.h" #include "Support/CommandLine.h"
#include "Support/Debug.h" #include "Support/Debug.h"
#include "Support/DepthFirstIterator.h"
#include "Support/Statistic.h" #include "Support/Statistic.h"
#include <cmath>
#include <iostream> #include <iostream>
#include <limits> #include <limits>
@ -45,6 +40,7 @@ namespace {
"Live Interval Analysis"); "Live Interval Analysis");
Statistic<> numIntervals("liveintervals", "Number of intervals"); Statistic<> numIntervals("liveintervals", "Number of intervals");
Statistic<> numJoined ("liveintervals", "Number of intervals joined");
cl::opt<bool> cl::opt<bool>
join("join-liveintervals", join("join-liveintervals",
@ -63,6 +59,16 @@ void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const
MachineFunctionPass::getAnalysisUsage(AU); MachineFunctionPass::getAnalysisUsage(AU);
} }
void LiveIntervals::releaseMemory()
{
mbbi2mbbMap_.clear();
mi2iMap_.clear();
r2iMap_.clear();
r2iMap_.clear();
r2rMap_.clear();
intervals_.clear();
}
/// runOnMachineFunction - Register allocate the whole function /// runOnMachineFunction - Register allocate the whole function
/// ///
bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) { bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
@ -71,19 +77,13 @@ bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
tm_ = &fn.getTarget(); tm_ = &fn.getTarget();
mri_ = tm_->getRegisterInfo(); mri_ = tm_->getRegisterInfo();
lv_ = &getAnalysis<LiveVariables>(); lv_ = &getAnalysis<LiveVariables>();
mbbi2mbbMap_.clear();
mi2iMap_.clear();
r2iMap_.clear();
r2iMap_.clear();
r2rMap_.clear();
intervals_.clear();
// number MachineInstrs // number MachineInstrs
unsigned miIndex = 0; unsigned miIndex = 0;
for (MachineFunction::iterator mbb = mf_->begin(), mbbEnd = mf_->end(); for (MachineFunction::iterator mbb = mf_->begin(), mbbEnd = mf_->end();
mbb != mbbEnd; ++mbb) { mbb != mbbEnd; ++mbb) {
const std::pair<MachineBasicBlock*, unsigned>& entry = const std::pair<MachineBasicBlock*, unsigned>& entry =
lv_->getMachineBasicBlockInfo(&*mbb); lv_->getMachineBasicBlockInfo(mbb);
bool inserted = mbbi2mbbMap_.insert(std::make_pair(entry.second, bool inserted = mbbi2mbbMap_.insert(std::make_pair(entry.second,
entry.first)).second; entry.first)).second;
assert(inserted && "multiple index -> MachineBasicBlock"); assert(inserted && "multiple index -> MachineBasicBlock");
@ -107,20 +107,22 @@ bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
const MachineBasicBlock* mbb = mbbi; const MachineBasicBlock* mbb = mbbi;
unsigned loopDepth = loopInfo.getLoopDepth(mbb->getBasicBlock()); unsigned loopDepth = loopInfo.getLoopDepth(mbb->getBasicBlock());
for (MachineBasicBlock::const_iterator mii = mbb->begin(), if (loopDepth) {
mie = mbb->end(); mii != mie; ++mii) { for (MachineBasicBlock::const_iterator mii = mbb->begin(),
MachineInstr* mi = *mii; mie = mbb->end(); mii != mie; ++mii) {
MachineInstr* mi = *mii;
for (int i = mi->getNumOperands() - 1; i >= 0; --i) { for (int i = mi->getNumOperands() - 1; i >= 0; --i) {
MachineOperand& mop = mi->getOperand(i); MachineOperand& mop = mi->getOperand(i);
if (!mop.isVirtualRegister()) if (!mop.isVirtualRegister())
continue; continue;
unsigned reg = mop.getAllocatedRegNum(); unsigned reg = mop.getAllocatedRegNum();
Reg2IntervalMap::iterator r2iit = r2iMap_.find(reg); Reg2IntervalMap::iterator r2iit = r2iMap_.find(reg);
assert(r2iit != r2iMap_.end()); assert(r2iit != r2iMap_.end());
r2iit->second->weight += pow(10.0F, loopDepth); r2iit->second->weight += pow(10.0F, loopDepth);
}
} }
} }
} }
@ -152,31 +154,27 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock* mbb,
LiveVariables::VarInfo& vi = lv_->getVarInfo(reg); LiveVariables::VarInfo& vi = lv_->getVarInfo(reg);
Interval* interval = 0; Interval* interval = 0;
Reg2IntervalMap::iterator r2iit = r2iMap_.find(reg); Reg2IntervalMap::iterator r2iit = r2iMap_.lower_bound(reg);
if (r2iit == r2iMap_.end()) { if (r2iit == r2iMap_.end() || r2iit->first != reg) {
// add new interval // add new interval
intervals_.push_back(Interval(reg)); intervals_.push_back(Interval(reg));
// update interval index for this register // update interval index for this register
bool inserted = r2iMap_.insert(r2iit, std::make_pair(reg, --intervals_.end()));
r2iMap_.insert(std::make_pair(reg, --intervals_.end())).second;
assert(inserted);
interval = &intervals_.back(); interval = &intervals_.back();
} }
else { else {
interval = &*r2iit->second; interval = &*r2iit->second;
} }
for (MbbIndex2MbbMap::iterator // iterate over all of the blocks that the variable is completely
it = mbbi2mbbMap_.begin(), itEnd = mbbi2mbbMap_.end(); // live in, adding them to the live interval
it != itEnd; ++it) { for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
unsigned liveBlockIndex = it->first; if (vi.AliveBlocks[i]) {
MachineBasicBlock* liveBlock = it->second; MachineBasicBlock* mbb = lv_->getIndexMachineBasicBlock(i);
if (liveBlockIndex < vi.AliveBlocks.size() && if (!mbb->empty()) {
vi.AliveBlocks[liveBlockIndex] && interval->addRange(getInstructionIndex(mbb->front()),
!liveBlock->empty()) { getInstructionIndex(mbb->back()));
unsigned start = getInstructionIndex(liveBlock->front()); }
unsigned end = getInstructionIndex(liveBlock->back()) + 1;
interval->addRange(start, end);
} }
} }
@ -207,62 +205,49 @@ void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock* mbb,
DEBUG(std::cerr << "\t\tregister: "; printRegName(reg)); DEBUG(std::cerr << "\t\tregister: "; printRegName(reg));
unsigned start = getInstructionIndex(*mi); unsigned start = getInstructionIndex(*mi);
unsigned end = start; unsigned end = start + 1;
// register can be dead by the instruction defining it but it can // register can be dead by the instruction defining it but it can
// only be killed by subsequent instructions // only be killed by subsequent instructions
for (LiveVariables::killed_iterator for (LiveVariables::killed_iterator
ki = lv_->dead_begin(*mi), ki = lv_->dead_begin(*mi),
ke = lv_->dead_end(*mi); ke = lv_->dead_end(*mi);
ki != ke; ++ki) { ki != ke; ++ki) {
if (reg == ki->second) { if (reg == ki->second) {
end = getInstructionIndex(ki->first) + 1;
DEBUG(std::cerr << " dead\n"); DEBUG(std::cerr << " dead\n");
goto exit; goto exit;
} }
} }
++mi;
for (MachineBasicBlock::iterator e = mbb->end(); mi != e; ++mi) { {
for (LiveVariables::killed_iterator MachineBasicBlock::iterator e = mbb->end();
ki = lv_->dead_begin(*mi), do {
ke = lv_->dead_end(*mi); ++mi;
ki != ke; ++ki) { for (LiveVariables::killed_iterator
if (reg == ki->second) { ki = lv_->killed_begin(*mi),
end = getInstructionIndex(ki->first) + 1; ke = lv_->killed_end(*mi);
DEBUG(std::cerr << " dead\n"); ki != ke; ++ki) {
goto exit; if (reg == ki->second) {
DEBUG(std::cerr << " killed\n");
goto exit;
}
} }
} ++end;
} while (mi != e);
for (LiveVariables::killed_iterator
ki = lv_->killed_begin(*mi),
ke = lv_->killed_end(*mi);
ki != ke; ++ki) {
if (reg == ki->second) {
end = getInstructionIndex(ki->first) + 1;
DEBUG(std::cerr << " killed\n");
goto exit;
}
}
} }
exit: exit:
assert(start < end && "did not find end of interval?"); assert(start < end && "did not find end of interval?");
Reg2IntervalMap::iterator r2iit = r2iMap_.find(reg); Reg2IntervalMap::iterator r2iit = r2iMap_.lower_bound(reg);
if (r2iit != r2iMap_.end()) { if (r2iit != r2iMap_.end() && r2iit->first == reg) {
Interval& interval = *r2iit->second; r2iit->second->addRange(start, end);
interval.addRange(start, end);
} }
else { else {
intervals_.push_back(Interval(reg)); intervals_.push_back(Interval(reg));
Interval& interval = intervals_.back();
// update interval index for this register // update interval index for this register
bool inserted = r2iMap_.insert(r2iit, std::make_pair(reg, --intervals_.end()));
r2iMap_.insert(std::make_pair(reg, --intervals_.end())).second; intervals_.back().addRange(start, end);
assert(inserted);
interval.addRange(start, end);
} }
} }
@ -282,16 +267,9 @@ void LiveIntervals::handleRegisterDef(MachineBasicBlock* mbb,
} }
} }
unsigned LiveIntervals::getInstructionIndex(MachineInstr* instr) const
{
assert(mi2iMap_.find(instr) != mi2iMap_.end() &&
"instruction not assigned a number");
return mi2iMap_.find(instr)->second;
}
/// computeIntervals - computes the live intervals for virtual /// computeIntervals - computes the live intervals for virtual
/// registers. for some ordering of the machine instructions [1,N] a /// registers. for some ordering of the machine instructions [1,N) a
/// live interval is an interval [i, j] where 1 <= i <= j <= N for /// live interval is an interval [i, j) where 1 <= i <= j < N for
/// which a variable is live /// which a variable is live
void LiveIntervals::computeIntervals() void LiveIntervals::computeIntervals()
{ {
@ -318,13 +296,9 @@ void LiveIntervals::computeIntervals()
// handle explicit defs // handle explicit defs
for (int i = instr->getNumOperands() - 1; i >= 0; --i) { for (int i = instr->getNumOperands() - 1; i >= 0; --i) {
// handle register defs - build intervals
MachineOperand& mop = instr->getOperand(i); MachineOperand& mop = instr->getOperand(i);
if (mop.isRegister() && mop.isDef())
if (!mop.isRegister())
continue;
// handle defs - build intervals
if (mop.isDef())
handleRegisterDef(mbb, mi, mop.getAllocatedRegNum()); handleRegisterDef(mbb, mi, mop.getAllocatedRegNum());
} }
} }
@ -424,6 +398,7 @@ void LiveIntervals::joinIntervals()
intervals_.erase(dstInt); intervals_.erase(dstInt);
} }
} }
++numJoined;
} }
} }
} }
@ -434,7 +409,7 @@ void LiveIntervals::joinIntervals()
DEBUG(for (Reg2RegMap::const_iterator i = r2rMap_.begin(), DEBUG(for (Reg2RegMap::const_iterator i = r2rMap_.begin(),
e = r2rMap_.end(); i != e; ++i) e = r2rMap_.end(); i != e; ++i)
std::cerr << i->first << " -> " << i->second << '\n';); std::cerr << i->first << " -> " << i->second << '\n';);
} }
bool LiveIntervals::overlapsAliases(const Interval& lhs, bool LiveIntervals::overlapsAliases(const Interval& lhs,
@ -461,6 +436,15 @@ LiveIntervals::Interval::Interval(unsigned r)
} }
// This example is provided becaues liveAt() is non-obvious:
//
// this = [1,2), liveAt(1) will return false. The idea is that the
// variable is defined in 1 and not live after definition. So it was
// dead to begin with (defined but never used).
//
// this = [1,3), liveAt(2) will return false. The variable is used at
// 2 but 2 is the last use so the variable's allocated register is
// available for reuse.
bool LiveIntervals::Interval::liveAt(unsigned index) const bool LiveIntervals::Interval::liveAt(unsigned index) const
{ {
Ranges::const_iterator r = ranges.begin(); Ranges::const_iterator r = ranges.begin();
@ -472,13 +456,38 @@ bool LiveIntervals::Interval::liveAt(unsigned index) const
return false; return false;
} }
// This example is provided because overlaps() is non-obvious:
//
// 0: A = ...
// 1: B = ...
// 2: C = A + B ;; last use of A
//
// The live intervals should look like:
//
// A = [0, 3)
// B = [1, x)
// C = [2, y)
//
// A->overlaps(C) should return false since we want to be able to join
// A and C.
bool LiveIntervals::Interval::overlaps(const Interval& other) const bool LiveIntervals::Interval::overlaps(const Interval& other) const
{ {
Ranges::const_iterator i = ranges.begin(); Ranges::const_iterator i = ranges.begin();
Ranges::const_iterator ie = ranges.end();
Ranges::const_iterator j = other.ranges.begin(); Ranges::const_iterator j = other.ranges.begin();
Ranges::const_iterator je = other.ranges.end();
while (i != ie && j != je) {
if (i->first == j->first) {
return true;
}
else {
if (i->first > j->first) {
swap(i, j);
swap(ie, je);
}
assert(i->first < j->first);
while (i != ranges.end() && j != other.ranges.end()) {
if (i->first < j->first) {
if ((i->second - 1) > j->first) { if ((i->second - 1) > j->first) {
return true; return true;
} }
@ -486,17 +495,6 @@ bool LiveIntervals::Interval::overlaps(const Interval& other) const
++i; ++i;
} }
} }
else if (j->first < i->first) {
if ((j->second - 1) > i->first) {
return true;
}
else {
++j;
}
}
else {
return true;
}
} }
return false; return false;
@ -504,6 +502,7 @@ bool LiveIntervals::Interval::overlaps(const Interval& other) const
void LiveIntervals::Interval::addRange(unsigned start, unsigned end) void LiveIntervals::Interval::addRange(unsigned start, unsigned end)
{ {
assert(start < end && "Invalid range to add!");
DEBUG(std::cerr << "\t\t\tadding range: [" << start <<','<< end << ") -> "); DEBUG(std::cerr << "\t\t\tadding range: [" << start <<','<< end << ") -> ");
//assert(start < end && "invalid range?"); //assert(start < end && "invalid range?");
Range range = std::make_pair(start, end); Range range = std::make_pair(start, end);
@ -548,12 +547,13 @@ LiveIntervals::Interval::mergeRangesForward(Ranges::iterator it)
LiveIntervals::Interval::Ranges::iterator LiveIntervals::Interval::Ranges::iterator
LiveIntervals::Interval::mergeRangesBackward(Ranges::iterator it) LiveIntervals::Interval::mergeRangesBackward(Ranges::iterator it)
{ {
for (Ranges::iterator prev = it - 1; while (it != ranges.begin()) {
it != ranges.begin() && it->first <= prev->second; ) { Ranges::iterator prev = it - 1;
if (it->first > prev->second) break;
it->first = std::min(it->first, prev->first); it->first = std::min(it->first, prev->first);
it->second = std::max(it->second, prev->second); it->second = std::max(it->second, prev->second);
it = ranges.erase(prev); it = ranges.erase(prev);
prev = it - 1;
} }
return it; return it;