Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit c4f70d4

Browse files
author
James Molloy
committed
Add a function computeRegisterLiveness() to MachineBasicBlock. This uses analyzePhysReg() from r163694 to heuristically try and determine the liveness state of a physical register upon arrival at a particular instruction in a block.
The search for liveness is clipped to a specific number of instructions around the target MachineInstr, in order to avoid degenerating into an O(N^2) algorithm. It tries to use various clues about how instructions around (both before and after) a given MachineInstr use that register, to determine its state at the MachineInstr. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@163695 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent b17cf29 commit c4f70d4

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

include/llvm/CodeGen/MachineBasicBlock.h

+22
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,28 @@ class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
547547
return findDebugLoc(MBBI.getInstrIterator());
548548
}
549549

550+
/// Possible outcome of a register liveness query to computeRegisterLiveness()
551+
enum LivenessQueryResult {
552+
LQR_Live, ///< Register is known to be live.
553+
LQR_OverlappingLive, ///< Register itself is not live, but some overlapping
554+
///< register is.
555+
LQR_Dead, ///< Register is known to be dead.
556+
LQR_Unknown ///< Register liveness not decidable from local
557+
///< neighborhood.
558+
};
559+
560+
/// computeRegisterLiveness - Return whether (physical) register \c Reg
561+
/// has been <def>ined and not <kill>ed as of just before \c MI.
562+
///
563+
/// Search is localised to a neighborhood of
564+
/// \c Neighborhood instructions before (searching for defs or kills) and
565+
/// Neighborhood instructions after (searching just for defs) MI.
566+
///
567+
/// \c Reg must be a physical register.
568+
LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
569+
unsigned Reg, MachineInstr *MI,
570+
unsigned Neighborhood=10);
571+
550572
// Debugging methods.
551573
void dump() const;
552574
void print(raw_ostream &OS, SlotIndexes* = 0) const;

lib/CodeGen/MachineBasicBlock.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,80 @@ getWeightIterator(MachineBasicBlock::const_succ_iterator I) const {
971971
return Weights.begin() + index;
972972
}
973973

974+
/// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed
975+
/// as of just before "MI".
976+
///
977+
/// Search is localised to a neighborhood of
978+
/// Neighborhood instructions before (searching for defs or kills) and N
979+
/// instructions after (searching just for defs) MI.
980+
MachineBasicBlock::LivenessQueryResult
981+
MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI,
982+
unsigned Reg, MachineInstr *MI,
983+
unsigned Neighborhood) {
984+
985+
unsigned N = Neighborhood;
986+
MachineBasicBlock *MBB = MI->getParent();
987+
988+
// Start by searching backwards from MI, looking for kills, reads or defs.
989+
990+
MachineBasicBlock::iterator I(MI);
991+
// If this is the first insn in the block, don't search backwards.
992+
if (I != MBB->begin()) {
993+
do {
994+
--I;
995+
996+
MachineOperandIteratorBase::PhysRegInfo Analysis =
997+
MIOperands(I).analyzePhysReg(Reg, TRI);
998+
999+
if (Analysis.Kills)
1000+
// Register killed, so isn't live.
1001+
return LQR_Dead;
1002+
1003+
else if (Analysis.DefinesOverlap || Analysis.ReadsOverlap)
1004+
// Defined or read without a previous kill - live.
1005+
return (Analysis.Defines || Analysis.Reads) ?
1006+
LQR_Live : LQR_OverlappingLive;
1007+
1008+
} while (I != MBB->begin() && --N > 0);
1009+
}
1010+
1011+
// Did we get to the start of the block?
1012+
if (I == MBB->begin()) {
1013+
// If so, the register's state is definitely defined by the live-in state.
1014+
for (MCRegAliasIterator RAI(Reg, TRI, /*IncludeSelf=*/true);
1015+
RAI.isValid(); ++RAI) {
1016+
if (MBB->isLiveIn(*RAI))
1017+
return (*RAI == Reg) ? LQR_Live : LQR_OverlappingLive;
1018+
}
1019+
1020+
return LQR_Dead;
1021+
}
1022+
1023+
N = Neighborhood;
1024+
1025+
// Try searching forwards from MI, looking for reads or defs.
1026+
I = MachineBasicBlock::iterator(MI);
1027+
// If this is the last insn in the block, don't search forwards.
1028+
if (I != MBB->end()) {
1029+
for (++I; I != MBB->end() && N > 0; ++I, --N) {
1030+
MachineOperandIteratorBase::PhysRegInfo Analysis =
1031+
MIOperands(I).analyzePhysReg(Reg, TRI);
1032+
1033+
if (Analysis.ReadsOverlap)
1034+
// Used, therefore must have been live.
1035+
return (Analysis.Reads) ?
1036+
LQR_Live : LQR_OverlappingLive;
1037+
1038+
else if (Analysis.DefinesOverlap)
1039+
// Defined (but not read) therefore cannot have been live.
1040+
return LQR_Dead;
1041+
}
1042+
}
1043+
1044+
// At this point we have no idea of the liveness of the register.
1045+
return LQR_Unknown;
1046+
}
1047+
9741048
void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB,
9751049
bool t) {
9761050
OS << "BB#" << MBB->getNumber();

0 commit comments

Comments
 (0)