14#define DEBUG_TYPE "hexagon_live_vars"
32 "Hexagon Live Variable Analysis",
false,
false)
37class HexagonLiveVariablesImpl {
143 unsigned getNumRegs()
const {
return NumRegs; }
147 void clearDistanceMap() { DistanceMap.
clear(); }
162 HLV(
std::make_unique<HexagonLiveVariablesImpl>()) {
182 HLV->runOnMachineFunction(MF, MDT, MPDT);
186 return HLV->updateLocalLiveness(Fn);
191 HLV->constructUseDef(
MBB);
192 return HLV->updateLocalLiveness(
MBB, updateBundle);
198 assert(MIDelta->getParent() == To);
200 return HLV->incrementalUpdate(MIDelta, From, To);
209 HLV->addNewMI(
MI,
MBB);
213 HLV->constructUseDef(
MBB);
220 HLVComplete = !HLV->runOnMachineFunction(Fn, MDT, MPDT);
225 unsigned Reg)
const {
227 auto It = HLV->MBBLiveOuts.find(
MBB);
228 if (It == HLV->MBBLiveOuts.end())
230 if (Reg >= It->second.size())
232 return It->second[Reg];
238 auto It = HLV->MBBLiveOuts.find(
MBB);
239 if (It == HLV->MBBLiveOuts.end())
255 if (MIBegin == MIEnd)
260 if (MII->isBundle() || MII->isDebugInstr())
262 if (ExceptionsList && ExceptionsList->
contains(&*MII))
264 auto It = HLV->MIUseDefs.find(&*MII);
265 assert(It != HLV->MIUseDefs.end());
267 if (It->second.first[*AI]) {
271 }
while (MII != MIBegin);
284 if (MIBegin == MIEnd)
289 if (MII->isBundle() || MII->isDebugInstr())
291 auto It = HLV->MIUseDefs.find(&*MII);
292 assert(It != HLV->MIUseDefs.end());
294 if (It->second.second[*AI]) {
298 }
while (MII != MIBegin);
306 assert(
MI &&
"Invalid machine instruction");
307 assert(
MBB &&
"Invalid machine basic block");
308 auto It = HLV->MIUseDefs.find(
MI);
309 assert(It != HLV->MIUseDefs.end() &&
"Missing MI use/def information");
312 lit !=
MBB->livein_end(); ++lit) {
319 return MBBLiveIns.
anyCommon(It->second.second);
328 unsigned BufferPerMBB)
const {
329 assert(HLV->DistanceMap.find(From) != HLV->DistanceMap.end());
330 assert(HLV->DistanceMap.find(To) != HLV->DistanceMap.end());
331 unsigned FromSize = HLV->DistanceMap[From];
336 unsigned S = BufferPerMBB;
337 bool ToFirst =
false;
342 else if (
MBB == To) {
355 S += HLV->DistanceMap[
MBB] + BufferPerMBB;
359 S += FromSize + HLV->DistanceMap[To] + 2 * BufferPerMBB;
364 HLV->clearDistanceMap();
365 HLV->generateDistanceMap(Fn);
371bool HexagonLiveVariablesImpl::runOnMachineFunction(
380 TRI = ST.getRegisterInfo();
381 QII = ST.getInstrInfo();
383 NumRegs =
TRI->getNumRegs();
389 LLVM_DEBUG(
dbgs() <<
"\nNumber of registers in Hexagon is:" << NumRegs);
391 PhysRegDef.resize(NumRegs);
392 PhysRegUse.resize(NumRegs);
396 constructUseDef(&*
MBBI);
398 updateGlobalLiveness(Fn);
403 std::fill(PhysRegDef.begin(), PhysRegDef.end(), (
MachineInstr *)0);
404 std::fill(PhysRegUse.begin(), PhysRegUse.end(), (
MachineInstr *)0);
407 std::pair<BitVector, BitVector> &UseDef = MBBUseDefs[
MBB];
415 Defs.
resize(NumRegs,
false);
416 LiveOuts.
resize(NumRegs,
false);
423 unsigned MBBInsSize = 0;
428 MBBInsSize += QII->getSize(*
MI);
430 if (
MI->isBundle() ||
MI->isDebugInstr())
435 MIUseDef.first.resize(NumRegs);
436 MIUseDef.second.resize(NumRegs);
437 MIUseDef.first.reset();
438 MIUseDef.second.reset();
444 unsigned NumOperandsToProcess =
MI->getNumOperands();
445 for (
unsigned i = 0; i != NumOperandsToProcess; ++i) {
449 if (!QII->isPredicated(*
MI))
460 MIUseDef.first.set(
Reg);
463 if (!QII->isPredicated(*
MI) && !
MI->isKill()) {
469 MIUseDef.second.set(
Reg);
473 for (
unsigned i = 0, e = UseRegs.
size(); i != e; ++i)
474 handlePhysRegUse(UseRegs[i],
MI,
Uses);
476 for (
unsigned i = 0, e = DefRegs.
size(); i != e; ++i)
477 handlePhysRegDef(DefRegs[i],
MI, Defs);
479 DistanceMap[
MBB] = MBBInsSize;
482void HexagonLiveVariablesImpl::handlePhysRegUse(
MachineOperand *MO,
490 if (PhysRegDef[*SupI])
495 bool subRegDefined =
false;
498 if (PhysRegDef[*SubI])
499 subRegDefined =
true;
505 if (undefSubRegs.
empty()) {
506 if (!subRegDefined) {
510 PhysRegUse[*SubI] =
MI;
517 for (
unsigned i = 0; i < undefSubRegs.
size(); ++i) {
519 PhysRegUse[undefSubRegs[i]] =
MI;
520 Uses.set(undefSubRegs[i]);
525void HexagonLiveVariablesImpl::handlePhysRegDef(
MachineOperand *MO,
528 auto SetRegDef = [&](
unsigned Reg) ->
void {
529 PhysRegDef[
Reg] =
MI;
531 if (PhysRegUse[*AI]) {
543 for (
unsigned R = 1, NR =
TRI->getNumRegs(); R != NR; ++R)
566 while (!WorkStack.
empty()) {
568 BlockState &WState = State[W->getNumber()];
573 if (W->succ_empty() || WState.SuccQueued) {
576 WState.SuccQueued =
true;
580 WState.SuccQueued =
true;
592 dbgs() <<
"gatherBlocksDF: {";
595 B != BE; ++
B) { dbgs() <<
" BB#" << (*B)->getNumber(); }
dbgs()
599bool HexagonLiveVariablesImpl::updateGlobalLiveness(
MachineFunction &Fn) {
604 for (++
I;
I !=
E; ++
I) {
605 std::vector<MachineBasicBlock::RegisterMaskPair> OldLiveIn(
606 I->livein_begin(),
I->livein_end());
607 for (
unsigned i = 0; i < OldLiveIn.size(); ++i)
608 I->removeLiveIn(OldLiveIn[i].PhysReg);
619 B = BlocksDepthFirst.begin(),
620 BE = BlocksDepthFirst.end();
622 Repeat |= updateGlobalLiveness(*
B, Defs, LiveIns);
627 Changed |= updateLocalLiveness(Fn);
633 assert(
X &&
"Invalid start block");
634 assert(
Y &&
"Invalid end block");
641 BlocksDepthFirst.end();
643 for (
B = BlocksDepthFirst.begin(); (
B != BE); ++
B) {
654 Repeat |= updateGlobalLiveness(*
B, Defs, LiveIns);
656 B = BlocksDepthFirst.begin();
671 auto LiveOutIt = MBBLiveOuts.find(
MBB);
672 if (LiveOutIt == MBBLiveOuts.end())
673 LiveOutIt = MBBLiveOuts.insert({
MBB,
BitVector(NumRegs)}).first;
682 if (!LiveOuts[(*LI).PhysReg]) {
684 LiveOuts.
set((*LI).PhysReg);
691 Changed |= updateLiveIns(
MBB, LiveIns, LiveOuts);
702 const std::pair<BitVector, BitVector> &UseDefs = MBBUseDefs[
MBB];
706 LiveIns.
reset(UseDefs.second);
707 LiveIns |= UseDefs.first;
726 unsigned R = (*I).PhysReg;
736bool HexagonLiveVariablesImpl::updateLocalLiveness(
MachineFunction &Fn) {
739 updateLocalLiveness(&*
B,
false);
749 updateLiveOuts(
MBB, LiveOut);
756 MII != MIREnd; ++MII) {
759 if (
MI->isBundle()) {
764 if (
MI->isDebugInstr())
768 for (
unsigned i = 0; i <
MI->getNumOperands(); ++i) {
774 if (!QII->isPredicated(*
MI) && !
MI->isKill()) {
781 if (!QII->isPredicated(*
MI))
787 auto RemoveDef = [&](
unsigned Reg,
bool Implicit) ->
void {
793 for (
auto *UseOp : UseRegs)
794 if (UseOp->isImplicit() &&
TRI->regsOverlap(*
SI, UseOp->getReg()))
795 Used.set(UseOp->getReg());
799 for (
unsigned i = 0; i < DefRegs.
size(); ++i) {
804 for (
unsigned R = 1, NR =
TRI->getNumRegs(); R != NR; ++R)
810 for (
unsigned i = UseRegs.
size(); i > 0;) {
812 unsigned UseReg = UseRegs[i]->getReg();
819 if (Killed && !UseRegs[i]->
isDebug())
820 UseRegs[i]->setIsKill(
true);
825 MII != BundleHeads.
end(); ++MII) {
827 assert(
MI &&
"Invalid bundle head");
828 assert(
MI->isBundle() &&
"Expected a bundle head instruction");
829 assert(
MI->getParent() ==
MBB &&
"Bundle head not in expected block");
832 for (++BS; BS != BE; ++BS)
834 BS->unbundleFromPred();
836 BS =
MI->getIterator();
838 bool memShufDisabled = QII->getBundleNoShuf(*
MI);
839 MI->eraseFromParent();
843 QII->setBundleNoShuf(BundleMII);
849bool HexagonLiveVariablesImpl::incrementalUpdate(MICInstIterType MIDelta,
855 constructUseDef(From);
860 updateGlobalLiveness(From, To);
862 updateLocalLiveness(From,
true);
863 updateLocalLiveness(To,
true);
868 if (MIUseDef == MIUseDefs.end())
870 const BitVector &Defs = MIUseDef->second.second;
884 constructUseDef(
MBB);
886 updateGlobalLiveness(
MBB,
MBB);
893 constructUseDef(
MBB);
894 updateGlobalLiveness(
MBB,
MBB);
897void HexagonLiveVariablesImpl::generateDistanceMap(
const MachineFunction &Fn) {
898 assert(DistanceMap.empty() &&
"DistanceMap not empty, first clear!");
902 unsigned MBBInsSize = 0;
907 MBBInsSize += QII->getSize(*
MI);
909 DistanceMap[
MBB] = MBBInsSize;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator MBBI
Function Alias Analysis false
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static Register UseReg(const MachineOperand &MO)
static void UpdateBundle(MachineInstr *BundleHead)
static void gatherBlocksDF(MachineFunction &Fn, SmallVectorImpl< MachineBasicBlock * > *Blocks)
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Remove Loads Into Fake Uses
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
bool anyCommon(const BitVector &RHS) const
Test if any common bits are set.
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
MachineBasicBlock::const_instr_iterator MICInstIterType
void recalculate(MachineFunction &MF)
recalculate - recalculates the liveness from scratch.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void constructUseDef(MachineBasicBlock *MBB)
Constructs use-defs of MBB by analyzing each MachineOperand.
unsigned getDistanceBetween(const MachineBasicBlock *From, const MachineBasicBlock *To, unsigned BufferPerMBB=HEXAGON_INSTR_SIZE) const
Returns the linear distance (as per layout) of MI from the Function.
bool isUsedWithin(MICInstIterType MIBegin, MICInstIterType MIEnd, unsigned Reg, MICInstIterType &Use, SmallPtrSet< MachineInstr *, 2 > *ExceptionsList=nullptr) const
bool incrementalUpdate(MICInstIterType MIDelta, MachineBasicBlock *From, MachineBasicBlock *To)
incrementalUpdate - update the liveness when MIDelta is moved from From to To.
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
bool updateLocalLiveness(MachineFunction &Fn)
updateLocalLiveness - update only kill flags of operands.
const BitVector & getLiveOuts(const MachineBasicBlock *MBB) const
bool isDefLiveIn(const MachineInstr *MI, const MachineBasicBlock *MBB) const
MIUseDef_t & getMIUseDefs()
void regenerateDistanceMap(const MachineFunction &Fn)
MBBUseDef_t & getMBBUseDefs()
void addNewMBB(MachineBasicBlock *MBB)
addNewMBB - inform the LiveVariable Analysis that new MBB has been added.
bool isLiveOut(const MachineBasicBlock *MBB, unsigned Reg) const
bool isDefinedWithin(MICInstIterType MIBegin, MICInstIterType MIEnd, unsigned Reg, MICInstIterType &Def) const
void addNewMI(MachineInstr *MI, MachineBasicBlock *MBB)
MCRegAliasIterator enumerates all registers aliasing Reg.
MCSubRegIterator enumerates all sub-registers of Reg.
bool isValid() const
Returns true if this iterator is not yet at the end.
MCSuperRegIterator enumerates all super-registers of Reg.
bool isValid() const
Returns true if this iterator is not yet at the end.
livein_iterator livein_end() const
instr_iterator instr_begin()
reverse_instr_iterator instr_rbegin()
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
succ_iterator succ_begin()
bool livein_empty() const
LLVM_ABI void removeLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Remove the specified register from the live in set.
LiveInVector::const_iterator livein_iterator
LLVM_ABI livein_iterator livein_begin() const
SmallVectorImpl< MachineBasicBlock * >::iterator succ_iterator
reverse_instr_iterator instr_rend()
Instructions::iterator instr_iterator
instr_iterator instr_end()
Instructions::const_iterator const_instr_iterator
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
Instructions::reverse_iterator reverse_instr_iterator
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineFunctionPass(char &ID)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
BasicBlockListType::iterator iterator
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them.
const MachineBasicBlock & front() const
BasicBlockListType::const_iterator const_iterator
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
void setIsDead(bool Val=true)
void setIsKill(bool Val=true)
Register getReg() const
getReg - Returns the register number.
static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
MachinePostDominatorTree - an analysis pass wrapper for DominatorTree used to compute the post-domina...
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
typename SuperClass::iterator iterator
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
A Use represents the edge between a Value definition and its users.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void finalizeBundle(MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
finalizeBundle - Finalize a machine instruction bundle which includes a sequence of instructions star...
@ Implicit
Not emitted register (e.g. carry, or temporary result).
char & HexagonLiveVariablesID
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
MachineBasicBlock::instr_iterator getBundleEnd(MachineBasicBlock::instr_iterator I)
Returns an iterator pointing beyond the bundle containing I.
DenseMap< MachineBasicBlock *, UseDef_t > MBBUseDef_t
void initializeHexagonLiveVariablesPass(PassRegistry &)
std::pair< BitVector, BitVector > UseDef_t
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
DenseMap< const MachineInstr *, UseDef_t > MIUseDef_t
Implement std::hash so that hash_code can be used in STL containers.