2004-07-23 17:56:30 +00:00
|
|
|
//===-- LiveIntervalAnalysis.h - Live Interval Analysis ---------*- C++ -*-===//
|
2003-11-20 03:32:25 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-07-19 02:13:59 +00:00
|
|
|
// This file implements the LiveInterval analysis pass. Given some numbering of
|
|
|
|
// each the machine instructions (in this implemention depth-first order) an
|
|
|
|
// interval [i, j) is said to be a live interval 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 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), [1000,1001).
|
2003-11-20 03:32:25 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-07-23 17:56:30 +00:00
|
|
|
#ifndef LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
|
|
|
|
#define LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
|
2003-11-20 03:32:25 +00:00
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2005-09-21 04:18:25 +00:00
|
|
|
#include "llvm/CodeGen/LiveInterval.h"
|
2007-02-15 05:59:24 +00:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2007-04-17 20:32:26 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2007-02-01 05:32:05 +00:00
|
|
|
#include "llvm/ADT/IndexedMap.h"
|
2003-11-20 03:32:25 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
class LiveVariables;
|
|
|
|
class MRegisterInfo;
|
Allow the live interval analysis pass to be a bit more aggressive about
numbering values in live ranges for physical registers.
The alpha backend currently generates code that looks like this:
vreg = preg
...
preg = vreg
use preg
...
preg = vreg
use preg
etc. Because vreg contains the value of preg coming in, each of the
copies back into preg contain that initial value as well.
In the case of the Alpha, this allows this testcase:
void "foo"(int %blah) {
store int 5, int *%MyVar
store int 12, int* %MyVar2
ret void
}
to compile to:
foo:
ldgp $29, 0($27)
ldiq $0,5
stl $0,MyVar
ldiq $0,12
stl $0,MyVar2
ret $31,($26),1
instead of:
foo:
ldgp $29, 0($27)
bis $29,$29,$0
ldiq $1,5
bis $0,$0,$29
stl $1,MyVar
ldiq $1,12
bis $0,$0,$29
stl $1,MyVar2
ret $31,($26),1
This does not seem to have any noticable effect on X86 code.
This fixes PR535.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@20536 91177308-0d34-0410-b5e6-96231b3b80d8
2005-03-09 23:05:19 +00:00
|
|
|
class TargetInstrInfo;
|
2007-04-17 20:32:26 +00:00
|
|
|
class TargetRegisterClass;
|
2004-08-04 09:46:26 +00:00
|
|
|
class VirtRegMap;
|
|
|
|
|
|
|
|
class LiveIntervals : public MachineFunctionPass {
|
|
|
|
MachineFunction* mf_;
|
|
|
|
const TargetMachine* tm_;
|
|
|
|
const MRegisterInfo* mri_;
|
Allow the live interval analysis pass to be a bit more aggressive about
numbering values in live ranges for physical registers.
The alpha backend currently generates code that looks like this:
vreg = preg
...
preg = vreg
use preg
...
preg = vreg
use preg
etc. Because vreg contains the value of preg coming in, each of the
copies back into preg contain that initial value as well.
In the case of the Alpha, this allows this testcase:
void "foo"(int %blah) {
store int 5, int *%MyVar
store int 12, int* %MyVar2
ret void
}
to compile to:
foo:
ldgp $29, 0($27)
ldiq $0,5
stl $0,MyVar
ldiq $0,12
stl $0,MyVar2
ret $31,($26),1
instead of:
foo:
ldgp $29, 0($27)
bis $29,$29,$0
ldiq $1,5
bis $0,$0,$29
stl $1,MyVar
ldiq $1,12
bis $0,$0,$29
stl $1,MyVar2
ret $31,($26),1
This does not seem to have any noticable effect on X86 code.
This fixes PR535.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@20536 91177308-0d34-0410-b5e6-96231b3b80d8
2005-03-09 23:05:19 +00:00
|
|
|
const TargetInstrInfo* tii_;
|
2004-08-04 09:46:26 +00:00
|
|
|
LiveVariables* lv_;
|
|
|
|
|
2006-09-15 03:57:23 +00:00
|
|
|
/// MBB2IdxMap - The index of the first instruction in the specified basic
|
|
|
|
/// block.
|
|
|
|
std::vector<unsigned> MBB2IdxMap;
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
|
|
|
|
Mi2IndexMap mi2iMap_;
|
|
|
|
|
|
|
|
typedef std::vector<MachineInstr*> Index2MiMap;
|
|
|
|
Index2MiMap i2miMap_;
|
|
|
|
|
|
|
|
typedef std::map<unsigned, LiveInterval> Reg2IntervalMap;
|
|
|
|
Reg2IntervalMap r2iMap_;
|
|
|
|
|
2007-02-01 05:32:05 +00:00
|
|
|
typedef IndexedMap<unsigned> Reg2RegMap;
|
2004-08-04 09:46:26 +00:00
|
|
|
Reg2RegMap r2rMap_;
|
|
|
|
|
2007-02-15 05:59:24 +00:00
|
|
|
BitVector allocatableRegs_;
|
2007-04-17 20:32:26 +00:00
|
|
|
DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs_;
|
2004-08-26 22:22:38 +00:00
|
|
|
|
2007-03-01 02:03:03 +00:00
|
|
|
/// JoinedLIs - Keep track which register intervals have been coalesced
|
|
|
|
/// with other intervals.
|
|
|
|
BitVector JoinedLIs;
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
public:
|
2006-09-02 05:32:53 +00:00
|
|
|
struct CopyRec {
|
|
|
|
MachineInstr *MI;
|
|
|
|
unsigned SrcReg, DstReg;
|
|
|
|
};
|
|
|
|
CopyRec getCopyRec(MachineInstr *MI, unsigned SrcReg, unsigned DstReg) {
|
|
|
|
CopyRec R;
|
|
|
|
R.MI = MI;
|
|
|
|
R.SrcReg = SrcReg;
|
|
|
|
R.DstReg = DstReg;
|
|
|
|
return R;
|
|
|
|
}
|
2006-08-24 22:43:55 +00:00
|
|
|
struct InstrSlots {
|
2004-08-04 09:46:26 +00:00
|
|
|
enum {
|
|
|
|
LOAD = 0,
|
|
|
|
USE = 1,
|
|
|
|
DEF = 2,
|
|
|
|
STORE = 3,
|
2006-02-22 16:23:43 +00:00
|
|
|
NUM = 4
|
2004-08-04 09:46:26 +00:00
|
|
|
};
|
2003-11-20 03:32:25 +00:00
|
|
|
};
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
static unsigned getBaseIndex(unsigned index) {
|
|
|
|
return index - (index % InstrSlots::NUM);
|
|
|
|
}
|
|
|
|
static unsigned getBoundaryIndex(unsigned index) {
|
|
|
|
return getBaseIndex(index + InstrSlots::NUM - 1);
|
|
|
|
}
|
|
|
|
static unsigned getLoadIndex(unsigned index) {
|
|
|
|
return getBaseIndex(index) + InstrSlots::LOAD;
|
|
|
|
}
|
|
|
|
static unsigned getUseIndex(unsigned index) {
|
|
|
|
return getBaseIndex(index) + InstrSlots::USE;
|
|
|
|
}
|
|
|
|
static unsigned getDefIndex(unsigned index) {
|
|
|
|
return getBaseIndex(index) + InstrSlots::DEF;
|
|
|
|
}
|
|
|
|
static unsigned getStoreIndex(unsigned index) {
|
|
|
|
return getBaseIndex(index) + InstrSlots::STORE;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef Reg2IntervalMap::iterator iterator;
|
2004-09-30 15:59:17 +00:00
|
|
|
typedef Reg2IntervalMap::const_iterator const_iterator;
|
|
|
|
const_iterator begin() const { return r2iMap_.begin(); }
|
|
|
|
const_iterator end() const { return r2iMap_.end(); }
|
2004-08-04 09:46:26 +00:00
|
|
|
iterator begin() { return r2iMap_.begin(); }
|
|
|
|
iterator end() { return r2iMap_.end(); }
|
|
|
|
unsigned getNumIntervals() const { return r2iMap_.size(); }
|
|
|
|
|
|
|
|
LiveInterval &getInterval(unsigned reg) {
|
|
|
|
Reg2IntervalMap::iterator I = r2iMap_.find(reg);
|
|
|
|
assert(I != r2iMap_.end() && "Interval does not exist for register");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const LiveInterval &getInterval(unsigned reg) const {
|
|
|
|
Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
|
|
|
|
assert(I != r2iMap_.end() && "Interval does not exist for register");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2007-02-19 21:49:54 +00:00
|
|
|
bool hasInterval(unsigned reg) const {
|
2007-03-01 02:03:03 +00:00
|
|
|
return r2iMap_.count(reg);
|
2007-02-19 21:49:54 +00:00
|
|
|
}
|
|
|
|
|
2006-09-15 03:57:23 +00:00
|
|
|
/// getMBBStartIdx - Return the base index of the first instruction in the
|
|
|
|
/// specified MachineBasicBlock.
|
|
|
|
unsigned getMBBStartIdx(MachineBasicBlock *MBB) const {
|
|
|
|
return getMBBStartIdx(MBB->getNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned getMBBStartIdx(unsigned MBBNo) const {
|
|
|
|
assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
|
|
|
|
return MBB2IdxMap[MBBNo];
|
|
|
|
}
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
/// getInstructionIndex - returns the base index of instr
|
|
|
|
unsigned getInstructionIndex(MachineInstr* instr) const {
|
|
|
|
Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
|
|
|
|
assert(it != mi2iMap_.end() && "Invalid instruction!");
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getInstructionFromIndex - given an index in any slot of an
|
|
|
|
/// instruction return a pointer the instruction
|
|
|
|
MachineInstr* getInstructionFromIndex(unsigned index) const {
|
|
|
|
index /= InstrSlots::NUM; // convert index to vector index
|
|
|
|
assert(index < i2miMap_.size() &&
|
|
|
|
"index does not correspond to an instruction");
|
|
|
|
return i2miMap_[index];
|
|
|
|
}
|
2006-09-15 03:57:23 +00:00
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
|
|
|
|
VirtRegMap& vrm,
|
|
|
|
int slot);
|
|
|
|
|
2006-11-16 02:41:50 +00:00
|
|
|
/// CreateNewLiveInterval - Create a new live interval with the given live
|
|
|
|
/// ranges. The new live interval will have an infinite spill weight.
|
|
|
|
LiveInterval &CreateNewLiveInterval(const LiveInterval *LI,
|
|
|
|
const std::vector<LiveRange> &LRs);
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
virtual void releaseMemory();
|
|
|
|
|
|
|
|
/// runOnMachineFunction - pass entry point
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction&);
|
|
|
|
|
2004-09-30 15:59:17 +00:00
|
|
|
/// print - Implement the dump method.
|
2004-12-07 04:03:45 +00:00
|
|
|
virtual void print(std::ostream &O, const Module* = 0) const;
|
2006-12-17 05:15:13 +00:00
|
|
|
void print(std::ostream *O, const Module* M = 0) const {
|
|
|
|
if (O) print(*O, M);
|
|
|
|
}
|
2004-09-30 15:59:17 +00:00
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
private:
|
2007-02-22 23:03:39 +00:00
|
|
|
/// isRemoved - returns true if the specified machine instr has been
|
|
|
|
/// removed.
|
|
|
|
bool isRemoved(MachineInstr* instr) const {
|
2007-02-22 23:52:23 +00:00
|
|
|
return !mi2iMap_.count(instr);
|
2007-02-22 23:03:39 +00:00
|
|
|
}
|
|
|
|
|
2006-08-24 22:43:55 +00:00
|
|
|
/// RemoveMachineInstrFromMaps - This marks the specified machine instr as
|
|
|
|
/// deleted.
|
|
|
|
void RemoveMachineInstrFromMaps(MachineInstr *MI) {
|
|
|
|
// remove index -> MachineInstr and
|
|
|
|
// MachineInstr -> index mappings
|
|
|
|
Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
|
|
|
|
if (mi2i != mi2iMap_.end()) {
|
|
|
|
i2miMap_[mi2i->second/InstrSlots::NUM] = 0;
|
|
|
|
mi2iMap_.erase(mi2i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-15 03:57:23 +00:00
|
|
|
/// computeIntervals - Compute live intervals.
|
2006-09-14 06:42:17 +00:00
|
|
|
void computeIntervals();
|
2004-08-04 09:46:26 +00:00
|
|
|
|
|
|
|
/// joinIntervals - join compatible live intervals
|
|
|
|
void joinIntervals();
|
|
|
|
|
2006-09-02 05:32:53 +00:00
|
|
|
/// CopyCoallesceInMBB - Coallsece copies in the specified MBB, putting
|
|
|
|
/// copies that cannot yet be coallesced into the "TryAgain" list.
|
|
|
|
void CopyCoallesceInMBB(MachineBasicBlock *MBB,
|
2007-04-18 02:30:19 +00:00
|
|
|
std::vector<CopyRec> *TryAgain, bool PhysOnly = false);
|
2007-02-19 21:49:54 +00:00
|
|
|
|
2006-08-24 22:43:55 +00:00
|
|
|
/// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
|
|
|
|
/// which are the src/dst of the copy instruction CopyMI. This returns true
|
|
|
|
/// if the copy was successfully coallesced away, or if it is never possible
|
|
|
|
/// to coallesce these this copy, due to register constraints. It returns
|
|
|
|
/// false if it is not currently possible to coallesce this interval, but
|
|
|
|
/// it may be possible if other things get coallesced.
|
2007-04-17 20:32:26 +00:00
|
|
|
bool JoinCopy(MachineInstr *CopyMI, unsigned SrcReg, unsigned DstReg,
|
|
|
|
bool PhysOnly = false);
|
2006-08-24 22:43:55 +00:00
|
|
|
|
2006-08-29 23:18:15 +00:00
|
|
|
/// JoinIntervals - Attempt to join these two intervals. On failure, this
|
|
|
|
/// returns false. Otherwise, if one of the intervals being joined is a
|
|
|
|
/// physreg, this method always canonicalizes DestInt to be it. The output
|
|
|
|
/// "SrcInt" will not have been modified, so we can use this information
|
|
|
|
/// below to update aliases.
|
|
|
|
bool JoinIntervals(LiveInterval &LHS, LiveInterval &RHS);
|
|
|
|
|
2006-09-02 22:27:29 +00:00
|
|
|
/// SimpleJoin - Attempt to join the specified interval into this one. The
|
2006-09-02 05:26:01 +00:00
|
|
|
/// caller of this method must guarantee that the RHS only contains a single
|
|
|
|
/// value number and that the RHS is not defined by a copy from this
|
|
|
|
/// interval. This returns false if the intervals are not joinable, or it
|
|
|
|
/// joins them and returns true.
|
|
|
|
bool SimpleJoin(LiveInterval &LHS, LiveInterval &RHS);
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
/// handleRegisterDef - update intervals for a register def
|
|
|
|
/// (calls handlePhysicalRegisterDef and
|
|
|
|
/// handleVirtualRegisterDef)
|
2006-09-03 08:07:11 +00:00
|
|
|
void handleRegisterDef(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock::iterator MI, unsigned MIIdx,
|
2006-01-29 07:59:37 +00:00
|
|
|
unsigned reg);
|
2004-08-04 09:46:26 +00:00
|
|
|
|
|
|
|
/// handleVirtualRegisterDef - update intervals for a virtual
|
|
|
|
/// register def
|
2006-09-03 08:07:11 +00:00
|
|
|
void handleVirtualRegisterDef(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
unsigned MIIdx,
|
2004-08-04 09:46:26 +00:00
|
|
|
LiveInterval& interval);
|
|
|
|
|
Allow the live interval analysis pass to be a bit more aggressive about
numbering values in live ranges for physical registers.
The alpha backend currently generates code that looks like this:
vreg = preg
...
preg = vreg
use preg
...
preg = vreg
use preg
etc. Because vreg contains the value of preg coming in, each of the
copies back into preg contain that initial value as well.
In the case of the Alpha, this allows this testcase:
void "foo"(int %blah) {
store int 5, int *%MyVar
store int 12, int* %MyVar2
ret void
}
to compile to:
foo:
ldgp $29, 0($27)
ldiq $0,5
stl $0,MyVar
ldiq $0,12
stl $0,MyVar2
ret $31,($26),1
instead of:
foo:
ldgp $29, 0($27)
bis $29,$29,$0
ldiq $1,5
bis $0,$0,$29
stl $1,MyVar
ldiq $1,12
bis $0,$0,$29
stl $1,MyVar2
ret $31,($26),1
This does not seem to have any noticable effect on X86 code.
This fixes PR535.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@20536 91177308-0d34-0410-b5e6-96231b3b80d8
2005-03-09 23:05:19 +00:00
|
|
|
/// handlePhysicalRegisterDef - update intervals for a physical register
|
2006-08-24 22:43:55 +00:00
|
|
|
/// def.
|
2004-08-04 09:46:26 +00:00
|
|
|
void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
|
|
|
|
MachineBasicBlock::iterator mi,
|
2006-09-03 08:07:11 +00:00
|
|
|
unsigned MIIdx,
|
2006-08-31 05:54:43 +00:00
|
|
|
LiveInterval &interval,
|
|
|
|
unsigned SrcReg);
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2007-02-19 21:49:54 +00:00
|
|
|
/// handleLiveInRegister - Create interval for a livein register.
|
2007-02-21 22:41:17 +00:00
|
|
|
void handleLiveInRegister(MachineBasicBlock* mbb,
|
|
|
|
unsigned MIIdx,
|
2007-04-25 07:30:23 +00:00
|
|
|
LiveInterval &interval, bool isAlias = false);
|
2007-02-19 21:49:54 +00:00
|
|
|
|
2006-05-12 06:06:34 +00:00
|
|
|
/// Return true if the two specified registers belong to different
|
|
|
|
/// register classes. The registers may be either phys or virt regs.
|
|
|
|
bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2006-08-24 22:43:55 +00:00
|
|
|
bool AdjustCopiesBackFrom(LiveInterval &IntA, LiveInterval &IntB,
|
2006-08-29 23:18:15 +00:00
|
|
|
MachineInstr *CopyMI);
|
2005-10-21 15:49:28 +00:00
|
|
|
|
2007-02-26 21:37:37 +00:00
|
|
|
/// lastRegisterUse - Returns the last use of the specific register between
|
|
|
|
/// cycles Start and End. It also returns the use operand by reference. It
|
|
|
|
/// returns NULL if there are no uses.
|
|
|
|
MachineInstr *lastRegisterUse(unsigned Reg, unsigned Start, unsigned End,
|
|
|
|
MachineOperand *&MOU);
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2007-04-02 18:49:18 +00:00
|
|
|
/// findDefOperand - Returns the MachineOperand that is a def of the specific
|
|
|
|
/// register. It returns NULL if the def is not found.
|
|
|
|
MachineOperand *findDefOperand(MachineInstr *MI, unsigned Reg);
|
|
|
|
|
2007-03-01 02:03:03 +00:00
|
|
|
/// unsetRegisterKill - Unset IsKill property of all uses of the specific
|
2007-02-22 23:03:39 +00:00
|
|
|
/// register of the specific instruction.
|
|
|
|
void unsetRegisterKill(MachineInstr *MI, unsigned Reg);
|
|
|
|
|
2007-03-01 02:03:03 +00:00
|
|
|
/// hasRegisterDef - True if the instruction defines the specific register.
|
|
|
|
///
|
|
|
|
bool hasRegisterDef(MachineInstr *MI, unsigned Reg);
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
static LiveInterval createInterval(unsigned Reg);
|
|
|
|
|
2007-02-19 21:49:54 +00:00
|
|
|
void removeInterval(unsigned Reg) {
|
|
|
|
r2iMap_.erase(Reg);
|
|
|
|
}
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
LiveInterval &getOrCreateInterval(unsigned reg) {
|
|
|
|
Reg2IntervalMap::iterator I = r2iMap_.find(reg);
|
|
|
|
if (I == r2iMap_.end())
|
|
|
|
I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// rep - returns the representative of this register
|
2004-09-08 03:01:50 +00:00
|
|
|
unsigned rep(unsigned Reg) {
|
|
|
|
unsigned Rep = r2rMap_[Reg];
|
|
|
|
if (Rep)
|
|
|
|
return r2rMap_[Reg] = rep(Rep);
|
|
|
|
return Reg;
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void printRegName(unsigned reg) const;
|
|
|
|
};
|
|
|
|
|
2003-11-20 03:32:25 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|