18 #define DEBUG_TYPE "regalloc"
47 "Live Interval Analysis",
false,
false)
58 cl::desc(
"Eagerly compute live intervals for all physreg units."));
81 DomTree(0), LRCalc(0) {
91 for (
unsigned i = 0, e = VirtRegIntervals.size(); i != e; ++i)
93 VirtRegIntervals.clear();
96 RegMaskBlocks.
clear();
98 for (
unsigned i = 0, e = RegUnitRanges.size(); i != e; ++i)
99 delete RegUnitRanges[i];
100 RegUnitRanges.clear();
103 VNInfoAllocator.
Reset();
114 AA = &getAnalysis<AliasAnalysis>();
115 Indexes = &getAnalysis<SlotIndexes>();
116 DomTree = &getAnalysis<MachineDominatorTree>();
125 computeLiveInRegUnits();
139 OS <<
"********** INTERVALS **********\n";
142 for (
unsigned i = 0, e = RegUnitRanges.size(); i != e; ++i)
154 for (
unsigned i = 0, e = RegMaskSlots.size(); i != e; ++i)
155 OS <<
' ' << RegMaskSlots[i];
161 void LiveIntervals::printInstrs(
raw_ostream &OS)
const {
162 OS <<
"********** MACHINEINSTRS **********\n";
163 MF->
print(OS, Indexes);
166 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
167 void LiveIntervals::dumpInstrs()
const {
172 LiveInterval* LiveIntervals::createInterval(
unsigned reg) {
182 assert(LRCalc &&
"LRCalc not initialized.");
183 assert(LI.
empty() &&
"Should only compute empty intervals.");
189 void LiveIntervals::computeVirtRegs() {
198 void LiveIntervals::computeRegMasks() {
205 std::pair<unsigned, unsigned> &RMB = RegMaskBlocks[MBB->
getNumber()];
206 RMB.first = RegMaskSlots.size();
210 if (!MO->isRegMask())
216 RMB.second = RegMaskSlots.size() - RMB.first;
234 void LiveIntervals::computeRegUnitRange(
LiveRange &LR,
unsigned Unit) {
235 assert(LRCalc &&
"LRCalc not initialized.");
256 unsigned Reg = *Supers;
268 void LiveIntervals::computeLiveInRegUnits() {
270 DEBUG(
dbgs() <<
"Computing live-in reg-units in ABI blocks.\n");
290 unsigned Unit = *Units;
293 LR = RegUnitRanges[Unit] =
new LiveRange();
303 DEBUG(
dbgs() <<
"Created " << NewRanges.
size() <<
" new intervals.\n");
306 for (
unsigned i = 0, e = NewRanges.
size(); i != e; ++i) {
307 unsigned Unit = NewRanges[i];
308 computeRegUnitRange(*RegUnitRanges[Unit], Unit);
318 DEBUG(
dbgs() <<
"Shrink: " << *li <<
'\n');
320 &&
"Can only shrink virtual registers");
340 <<
"Warning: Instr claims to read non-existent value in "
349 WorkList.
push_back(std::make_pair(Idx, VNI));
366 while (!WorkList.
empty()) {
376 assert(ExtVNI == VNI &&
"Unexpected existing value number");
382 PE = MBB->
pred_end(); PI != PE; ++PI) {
388 WorkList.
push_back(std::make_pair(Stop, PVNI));
394 DEBUG(
dbgs() <<
" live-in at " << BlockStart <<
'\n');
399 PE = MBB->
pred_end(); PI != PE; ++PI) {
404 "Wrong value out of predecessor");
405 WorkList.
push_back(std::make_pair(Stop, VNI));
410 bool CanSeparate =
false;
417 assert(LRI != NewLR.
end() &&
"Missing segment for PHI");
424 DEBUG(
dbgs() <<
"Dead PHI at " << VNI->
def <<
" may separate interval\n");
429 assert(MI &&
"No instruction defining live value");
432 DEBUG(
dbgs() <<
"All defs dead: " << VNI->
def <<
'\t' << *MI);
440 DEBUG(
dbgs() <<
"Shrunk: " << *li <<
'\n');
446 assert(LRCalc &&
"LRCalc not initialized.");
448 for (
unsigned i = 0, e = Indices.
size(); i != e; ++i)
449 LRCalc->
extend(LR, Indices[i]);
472 if (EndPoints) EndPoints->
push_back(MBBEnd);
481 SuccI != SuccE; ++SuccI) {
506 if (EndPoints) EndPoints->
push_back(MBBEnd);
534 if (RURanges.
empty())
544 if (RI->end.isBlock())
558 bool CancelKill =
false;
559 for (
unsigned u = 0, e = RU.
size(); u != e; ++u) {
562 if (I == RRanges.
end())
565 if (I == RRanges.
end() || I->start >= RI->end)
600 return MBB1 == MBB2 ? MBB1 : NULL;
667 std::lower_bound(Slots.
begin(), Slots.
end(), LiveI->start);
676 assert(*SlotI >= LiveI->start);
678 while (*SlotI < LiveI->
end) {
688 if (++SlotI == SlotE)
696 while (*SlotI < LiveI->start)
697 if (++SlotI == SlotE)
721 : LIS(LIS), MRI(MRI), TRI(TRI), OldIdx(OldIdx), NewIdx(NewIdx),
722 UpdateFlags(UpdateFlags) {}
730 return &LIS.getRegUnit(Unit);
731 return LIS.getCachedRegUnit(Unit);
737 DEBUG(
dbgs() <<
"handleMove " << OldIdx <<
" -> " << NewIdx <<
": " << *MI);
738 bool hasRegMask =
false;
747 MO->setIsKill(
false);
749 unsigned Reg = MO->getReg();
754 updateRange(LI, Reg);
762 updateRange(*LR, *Units);
765 updateRegMaskSlots();
772 if (!Updated.insert(&LR))
780 dbgs() <<
":\t" << LR <<
'\n';
785 handleMoveUp(LR, Reg);
827 if (
MachineInstr *KillMI = LIS.getInstructionFromIndex(I->end))
829 if (MO->isReg() && MO->isUse())
830 MO->setIsKill(
false);
833 I->end = NewIdx.getRegSlot(I->end.isEarlyClobber());
844 VNInfo *DefVNI = I->valno;
845 assert(DefVNI->
def == I->start &&
"Inconsistent def");
850 I->start = DefVNI->
def;
857 assert((I->end == OldIdx.getDeadSlot() ||
859 "Cannot move def below kill");
864 assert(NewI->valno != DefVNI &&
"Multiple defs of value?");
872 assert(NewI != I &&
"Inconsistent iterators");
898 void handleMoveUp(
LiveRange &LR,
unsigned Reg) {
913 I->end = NewIdx.getRegSlot(I->end.isEarlyClobber());
919 llvm::prior(I)->end = findLastUseBefore(Reg).getRegSlot();
926 VNInfo *DefVNI = I->valno;
927 assert(DefVNI->
def == I->start &&
"Inconsistent def");
933 assert(NewI->valno != DefVNI &&
"Same value defined more than once?");
935 if (I->end.isDead()) {
941 I->start = DefVNI->
def;
947 if (!I->end.isDead()) {
949 I->start = DefVNI->
def;
959 void updateRegMaskSlots() {
961 std::lower_bound(LIS.RegMaskSlots.begin(), LIS.RegMaskSlots.end(),
963 assert(RI != LIS.RegMaskSlots.end() && *RI == OldIdx.getRegSlot() &&
964 "No RegMask at OldIdx.");
965 *RI = NewIdx.getRegSlot();
966 assert((RI == LIS.RegMaskSlots.begin() ||
968 "Cannot move regmask instruction above another call");
969 assert((
llvm::next(RI) == LIS.RegMaskSlots.end() ||
971 "Cannot move regmask instruction below another call");
975 SlotIndex findLastUseBefore(
unsigned Reg) {
980 UI =
MRI.use_nodbg_begin(Reg),
981 UE =
MRI.use_nodbg_end();
982 UI != UE; UI.skipInstruction()) {
984 SlotIndex InstSlot = LIS.getSlotIndexes()->getInstructionIndex(MI);
985 if (InstSlot > LastUse && InstSlot < OldIdx)
993 assert(NewIdx < OldIdx &&
"Expected upwards move");
1006 while (MII != Begin) {
1007 if ((--MII)->isDebugValue())
1019 TRI.hasRegUnit(MO->getReg(),
Reg))
1028 assert(!MI->
isBundled() &&
"Can't handle bundled instructions yet.");
1034 "Cannot handle moves across basic block boundaries.");
1036 HMEditor HME(*
this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags);
1045 HMEditor HME(*
this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags);
1058 while (End != MBB->
end() && !Indexes->
hasIndex(End))
1062 if (End == MBB->
end())
1084 for (
unsigned i = 0, e = OrigRegs.
size(); i != e; ++i) {
1085 unsigned Reg = OrigRegs[i];
1096 if (LII != LI.
end() && LII->start < endIdx)
1097 lastUseIdx = LII->end;
1120 if (!isStartValid) {
1121 if (LII->end.isDead()) {
1123 if (LII != LI.
begin())
1130 LII = LI.
find(prevStart);
1150 }
else if (LII->start != instrIdx.
getRegSlot()) {
1161 }
else if (MO.
isUse()) {
1165 if (!isEndValid && !LII->end.isBlock())
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
static float getSpillWeight(bool isDef, bool isUse, BlockFrequency freq)
void push_back(const T &Elt)
const_iterator end(StringRef path)
Get end iterator over path.
mop_iterator operands_end()
bool isValid() const
Check if the iterator is at the end of the list.
AnalysisUsage & addPreserved()
ArrayRef< SlotIndex > getRegMaskSlots() const
static PassRegistry * getPassRegistry()
void extend(LiveRange &LR, SlotIndex Kill, unsigned PhysReg=0)
SlotIndex def
The index of the defining instruction.
void createDeadDefs(LiveRange &LR, unsigned Reg)
void updateAllRanges(MachineInstr *MI)
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
static unsigned index2VirtReg(unsigned Index)
The main container class for the LLVM Intermediate Representation.
bool addRegisterDead(unsigned Reg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
void print(raw_ostream &OS, SlotIndexes *=0) const
char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
std::vector< unsigned >::const_iterator livein_iterator
SlotIndex getInstructionIndex(const MachineInstr *instr) const
Returns the base index of the given instruction.
static bool isVirtualRegister(unsigned Reg)
iterator advanceTo(iterator I, SlotIndex Pos)
MachineBasicBlock * getMBBFromIndex(SlotIndex index) const
SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const
Return the last index in the given basic block.
bool readsVirtualRegister(unsigned Reg) const
INITIALIZE_PASS_BEGIN(LiveIntervals,"liveintervals","Live Interval Analysis", false, false) INITIALIZE_PASS_END(LiveIntervals
SlotIndex endPoint() const
virtual bool runOnMachineFunction(MachineFunction &)
runOnMachineFunction - pass entry point
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
void markUnused()
Mark this value as unused.
static uint64_t getEntryFrequency()
Returns the frequency of the entry block of the function.
bool checkRegMaskInterference(LiveInterval &LI, BitVector &UsableRegs)
void pruneValue(LiveInterval *LI, SlotIndex Kill, SmallVectorImpl< SlotIndex > *EndPoints)
LoopInfoBase< BlockT, LoopT > * LI
bool allDefsAreDead() const
static bool isEarlierInstr(SlotIndex A, SlotIndex B)
livein_iterator livein_begin() const
unsigned getNumVirtRegs() const
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
void clear()
clear - Clear all bits.
MachineBasicBlock * intervalIsInOneMBB(const LiveInterval &LI) const
char & MachineLoopInfoID
MachineLoopInfo - This pass is a loop analysis pass.
unsigned getNumBlockIDs() const
ArrayRef< SlotIndex > getRegMaskSlotsInBlock(unsigned MBBNum) const
ArrayRef< const uint32_t * > getRegMaskBits() const
VNInfo::Allocator & getVNInfoAllocator()
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
std::vector< MachineBasicBlock * >::iterator succ_iterator
bool isBlock() const
isBlock - Returns true if this is a block boundary slot.
ID
LLVM Calling Convention Representation.
SlotIndex getDeadSlot() const
Returns the dead def kill slot for the current instruction.
bool isUnused() const
Returns true if this value is unused.
SlotIndex getNextNonNullIndex(SlotIndex Index)
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
bool shrinkToUses(LiveInterval *li, SmallVectorImpl< MachineInstr * > *dead=0)
MCRegUnitRootIterator enumerates the root registers of a register unit.
iterator addSegment(Segment S)
bool livein_empty() const
AnalysisUsage & addPreservedID(const void *ID)
size_t size() const
size - Get the array size.
void clearRegisterKills(unsigned Reg, const TargetRegisterInfo *RegInfo)
SlotIndex getPrevSlot() const
const MachineBasicBlock * getParent() const
bool hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const
bool isDebugValue() const
void reset(const MachineFunction *MF, SlotIndexes *, MachineDominatorTree *, VNInfo::Allocator *)
bundle_iterator< MachineInstr, instr_iterator > iterator
void removeValNo(VNInfo *ValNo)
bool hasAtLeastOneValue() const
void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
SlotIndexes * getSlotIndexes() const
LiveQueryResult Query(SlotIndex Idx) const
void swap(SmallVectorImpl &RHS)
df_ext_iterator< T, SetTy > df_ext_end(const T &G, SetTy &S)
virtual void getAnalysisUsage(AnalysisUsage &AU) const
bool isReserved(unsigned PhysReg) const
livein_iterator livein_end() const
Live Interval static false cl::opt< bool > EnablePrecomputePhysRegs("precompute-phys-liveness", cl::Hidden, cl::desc("Eagerly compute live intervals for all physreg units."))
void extendToUses(LiveRange &LR, unsigned Reg)
df_ext_iterator< T, SetTy > df_ext_begin(const T &G, SetTy &S)
ItTy next(ItTy it, Dist n)
void initializeLiveIntervalsPass(PassRegistry &)
LiveInterval::Segment addSegmentToEndOfBlock(unsigned reg, MachineInstr *startInst)
VNInfo * valueDefined() const
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
HMEditor(LiveIntervals &LIS, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, SlotIndex OldIdx, SlotIndex NewIdx, bool UpdateFlags)
void verify() const
Walk the range and assert if any invariants fail to hold.
virtual void releaseMemory()
#define INITIALIZE_AG_DEPENDENCY(depName)
succ_iterator succ_begin()
void repairIntervalsInRange(MachineBasicBlock *MBB, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End, ArrayRef< unsigned > OrigRegs)
unsigned getSubReg() const
void handleMove(MachineInstr *MI, bool UpdateFlags=false)
pred_iterator pred_begin()
SlotIndex getInstructionIndex(const MachineInstr *MI) const
Returns the base index for the given instruction.
MachineInstr * getInstructionFromIndex(SlotIndex index) const
virtual void print(raw_ostream &O, const Module *=0) const
print - Implement the dump method.
std::vector< MachineBasicBlock * >::const_iterator const_pred_iterator
iterator find(SlotIndex Pos)
VNInfo * valueOut() const
unsigned id
The ID number of this value.
void removeSegment(SlotIndex Start, SlotIndex End, bool RemoveDeadValNo=false)
virtual const TargetInstrInfo * getInstrInfo() const
ArrayRef< const uint32_t * > getRegMaskBitsInBlock(unsigned MBBNum) const
static bool isSameInstr(SlotIndex A, SlotIndex B)
isSameInstr - Return true if A and B refer to the same instruction.
bool hasIndex(const MachineInstr *instr) const
MachineBasicBlock * getMBBFromIndex(SlotIndex index) const
Returns the basic block which the given index falls in.
LiveRange * getRegUnitLI(unsigned Unit)
LiveInterval & getInterval(unsigned Reg)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
SlotIndex getMBBEndIdx(unsigned Num) const
Returns the last index in the given basic block number.
void repairIndexesInRange(MachineBasicBlock *MBB, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End)
Repair indexes after adding and removing instructions.
LiveInterval & createEmptyInterval(unsigned Reg)
unsigned getNumRegUnits() const
Return the number of (native) register units in the target. Register units are numbered from 0 to get...
SlotIndex beginIndex() const
beginIndex - Return the lowest numbered slot covered.
static bool isPhysicalRegister(unsigned Reg)
bool isLandingPad() const
MachineRegisterInfo & getRegInfo()
void addKillFlags(const VirtRegMap *)
VNInfo * createDeadDef(SlotIndex Def, VNInfo::Allocator &VNInfoAllocator)
virtual void getAnalysisUsage(AnalysisUsage &AU) const
void handleMoveIntoBundle(MachineInstr *MI, MachineInstr *BundleStart, bool UpdateFlags=false)
SlotIndex endIndex() const
VNInfo * extendInBlock(SlotIndex StartIdx, SlotIndex Kill)
const TargetMachine & getTarget() const
virtual const TargetRegisterInfo * getRegisterInfo() const
AnalysisUsage & addRequiredTransitiveID(char &ID)
AnalysisUsage & addRequiredTransitive()
void removeMachineInstrFromMaps(MachineInstr *mi)
Remove the given machine instruction from the mapping.
SlotIndex getMBBStartIdx(unsigned Num) const
Returns the first index in the given basic block number.
bool hasInterval(unsigned Reg) const
bool reg_empty(unsigned RegNo) const
SlotIndex getRegSlot(bool EC=false) const
unsigned getReg() const
getReg - Returns the register number.
bool isValid() const
isValid - Returns true until all the operands have been visited.
VNInfo * getNextValue(SlotIndex def, VNInfo::Allocator &VNInfoAllocator)
mop_iterator operands_begin()
MachineInstr * getInstructionFromIndex(SlotIndex index) const
Returns the instruction associated with the given index.
BasicBlockListType::iterator iterator
ItTy prior(ItTy it, Dist n)
SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const
Return the first index in the given basic block.
bool addRegisterKilled(unsigned IncomingReg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
unsigned getPhys(unsigned virtReg) const
returns the physical register mapped to the specified virtual register
reg_iterator reg_begin(unsigned RegNo) const
const MCRegisterInfo & MRI
iterator FindSegmentContaining(SlotIndex Idx)
SlotIndex - An opaque wrapper around machine indexes.
SlotIndex insertMachineInstrInMaps(MachineInstr *mi, bool Late=false)
tier< T1, T2 > tie(T1 &f, T2 &s)
void extendToIndices(LiveRange &LR, ArrayRef< SlotIndex > Indices)
unsigned pred_size() const
bool reg_nodbg_empty(unsigned RegNo) const
LiveRange & getRegUnit(unsigned Unit)
VNInfo * getVNInfoBefore(SlotIndex Idx) const
LiveInterval & createAndComputeVirtRegInterval(unsigned Reg)
const std::pair< SlotIndex, SlotIndex > & getMBBRange(unsigned Num) const
Return the (start,end) range of the given basic block number.