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(
383 TRI = ST.getRegisterInfo();
384 QII = ST.getInstrInfo();
386 NumRegs =
TRI->getNumRegs();
392 LLVM_DEBUG(
dbgs() <<
"\nNumber of registers in Hexagon is:" << NumRegs);
394 PhysRegDef.resize(NumRegs);
395 PhysRegUse.resize(NumRegs);
399 constructUseDef(&*
MBBI);
401 updateGlobalLiveness(Fn);
406 std::fill(PhysRegDef.begin(), PhysRegDef.end(), (
MachineInstr *)0);
407 std::fill(PhysRegUse.begin(), PhysRegUse.end(), (
MachineInstr *)0);
410 std::pair<BitVector, BitVector> &UseDef = MBBUseDefs[
MBB];
418 Defs.
resize(NumRegs,
false);
419 LiveOuts.
resize(NumRegs,
false);
426 unsigned MBBInsSize = 0;
431 MBBInsSize += QII->getSize(*
MI);
433 if (
MI->isBundle() ||
MI->isDebugInstr())
438 MIUseDef.first.resize(NumRegs);
439 MIUseDef.second.resize(NumRegs);
440 MIUseDef.first.reset();
441 MIUseDef.second.reset();
447 unsigned NumOperandsToProcess =
MI->getNumOperands();
448 for (
unsigned i = 0; i != NumOperandsToProcess; ++i) {
452 if (!QII->isPredicated(*
MI))
463 MIUseDef.first.set(
Reg);
466 if (!QII->isPredicated(*
MI) && !
MI->isKill()) {
472 MIUseDef.second.set(
Reg);
476 for (
unsigned i = 0, e = UseRegs.
size(); i != e; ++i)
477 handlePhysRegUse(UseRegs[i],
MI,
Uses);
479 for (
unsigned i = 0, e = DefRegs.
size(); i != e; ++i)
480 handlePhysRegDef(DefRegs[i],
MI, Defs);
482 DistanceMap[
MBB] = MBBInsSize;
485void HexagonLiveVariablesImpl::handlePhysRegUse(
MachineOperand *MO,
493 if (PhysRegDef[*SupI])
498 bool subRegDefined =
false;
501 if (PhysRegDef[*SubI])
502 subRegDefined =
true;
508 if (undefSubRegs.
empty()) {
509 if (!subRegDefined) {
513 PhysRegUse[*SubI] =
MI;
520 for (
unsigned i = 0; i < undefSubRegs.
size(); ++i) {
522 PhysRegUse[undefSubRegs[i]] =
MI;
523 Uses.set(undefSubRegs[i]);
528void HexagonLiveVariablesImpl::handlePhysRegDef(
MachineOperand *MO,
531 auto SetRegDef = [&](
unsigned Reg) ->
void {
532 PhysRegDef[
Reg] =
MI;
534 if (PhysRegUse[*AI]) {
546 for (
unsigned R = 1, NR =
TRI->getNumRegs(); R != NR; ++R)
569 while (!WorkStack.
empty()) {
571 BlockState &WState = State[W->getNumber()];
576 if (W->succ_empty() || WState.SuccQueued) {
579 WState.SuccQueued =
true;
583 WState.SuccQueued =
true;
595 dbgs() <<
"gatherBlocksDF: {";
598 B != BE; ++
B) { dbgs() <<
" BB#" << (*B)->getNumber(); }
dbgs()
602bool HexagonLiveVariablesImpl::updateGlobalLiveness(
MachineFunction &Fn) {
607 for (++
I;
I !=
E; ++
I) {
608 std::vector<MachineBasicBlock::RegisterMaskPair> OldLiveIn(
609 I->livein_begin(),
I->livein_end());
610 for (
unsigned i = 0; i < OldLiveIn.size(); ++i)
611 I->removeLiveIn(OldLiveIn[i].PhysReg);
622 B = BlocksDepthFirst.begin(),
623 BE = BlocksDepthFirst.end();
625 Repeat |= updateGlobalLiveness(*
B, Defs, LiveIns);
630 Changed |= updateLocalLiveness(Fn);
636 assert(
X &&
"Invalid start block");
637 assert(
Y &&
"Invalid end block");
644 BlocksDepthFirst.end();
646 for (
B = BlocksDepthFirst.begin(); (
B != BE); ++
B) {
657 Repeat |= updateGlobalLiveness(*
B, Defs, LiveIns);
659 B = BlocksDepthFirst.begin();
674 auto LiveOutIt = MBBLiveOuts.find(
MBB);
675 if (LiveOutIt == MBBLiveOuts.end())
676 LiveOutIt = MBBLiveOuts.insert({
MBB,
BitVector(NumRegs)}).first;
685 if (!LiveOuts[(*LI).PhysReg]) {
687 LiveOuts.
set((*LI).PhysReg);
694 Changed |= updateLiveIns(
MBB, LiveIns, LiveOuts);
705 const std::pair<BitVector, BitVector> &UseDefs = MBBUseDefs[
MBB];
709 LiveIns.
reset(UseDefs.second);
710 LiveIns |= UseDefs.first;
729 unsigned R = (*I).PhysReg;
739bool HexagonLiveVariablesImpl::updateLocalLiveness(
MachineFunction &Fn) {
742 updateLocalLiveness(&*
B,
false);
752 updateLiveOuts(
MBB, LiveOut);
759 MII != MIREnd; ++MII) {
762 if (
MI->isBundle()) {
767 if (
MI->isDebugInstr())
771 for (
unsigned i = 0; i <
MI->getNumOperands(); ++i) {
777 if (!QII->isPredicated(*
MI) && !
MI->isKill()) {
784 if (!QII->isPredicated(*
MI))
790 auto RemoveDef = [&](
unsigned Reg,
bool Implicit) ->
void {
796 for (
auto *UseOp : UseRegs)
797 if (UseOp->isImplicit() &&
TRI->regsOverlap(*
SI, UseOp->getReg()))
798 Used.set(UseOp->getReg());
802 for (
unsigned i = 0; i < DefRegs.
size(); ++i) {
807 for (
unsigned R = 1, NR =
TRI->getNumRegs(); R != NR; ++R)
813 for (
unsigned i = UseRegs.
size(); i > 0;) {
815 unsigned UseReg = UseRegs[i]->getReg();
822 if (Killed && !UseRegs[i]->
isDebug())
823 UseRegs[i]->setIsKill(
true);
828 MII != BundleHeads.
end(); ++MII) {
830 assert(
MI &&
"Invalid bundle head");
831 assert(
MI->isBundle() &&
"Expected a bundle head instruction");
832 assert(
MI->getParent() ==
MBB &&
"Bundle head not in expected block");
835 for (++BS; BS != BE; ++BS)
837 BS->unbundleFromPred();
839 BS =
MI->getIterator();
841 bool memShufDisabled = QII->getBundleNoShuf(*
MI);
842 MI->eraseFromParent();
846 QII->setBundleNoShuf(BundleMII);
852bool HexagonLiveVariablesImpl::incrementalUpdate(MICInstIterType MIDelta,
858 constructUseDef(From);
863 updateGlobalLiveness(From, To);
865 updateLocalLiveness(From,
true);
866 updateLocalLiveness(To,
true);
871 if (MIUseDef == MIUseDefs.end())
873 const BitVector &Defs = MIUseDef->second.second;
887 constructUseDef(
MBB);
889 updateGlobalLiveness(
MBB,
MBB);
896 constructUseDef(
MBB);
897 updateGlobalLiveness(
MBB,
MBB);
900void HexagonLiveVariablesImpl::generateDistanceMap(
const MachineFunction &Fn) {
901 assert(DistanceMap.empty() &&
"DistanceMap not empty, first clear!");
905 unsigned MBBInsSize = 0;
910 MBBInsSize += QII->getSize(*
MI);
912 DistanceMap[
MBB] = MBBInsSize;
unsigned const MachineRegisterInfo * MRI
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")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
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
std::enable_if_t< GraphHasNodeNumbers< T * >, void > updateBlockNumbers()
Update dominator tree after renumbering blocks.
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.