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>()) {
180 HLV->runOnMachineFunction(MF, MDT, MPDT);
184 return HLV->updateLocalLiveness(Fn);
189 HLV->constructUseDef(
MBB);
190 return HLV->updateLocalLiveness(
MBB, updateBundle);
196 assert(MIDelta->getParent() == To);
198 return HLV->incrementalUpdate(MIDelta, From, To);
207 HLV->addNewMI(
MI,
MBB);
211 HLV->constructUseDef(
MBB);
218 HLVComplete = !HLV->runOnMachineFunction(Fn, MDT, MPDT);
223 unsigned Reg)
const {
225 auto It = HLV->MBBLiveOuts.find(
MBB);
226 if (It == HLV->MBBLiveOuts.end())
228 if (Reg >= It->second.size())
230 return It->second[Reg];
236 auto It = HLV->MBBLiveOuts.find(
MBB);
237 if (It == HLV->MBBLiveOuts.end())
253 if (MIBegin == MIEnd)
258 if (MII->isBundle() || MII->isDebugInstr())
260 if (ExceptionsList && ExceptionsList->
contains(&*MII))
262 auto It = HLV->MIUseDefs.find(&*MII);
263 assert(It != HLV->MIUseDefs.end());
265 if (It->second.first[*AI]) {
269 }
while (MII != MIBegin);
282 if (MIBegin == MIEnd)
287 if (MII->isBundle() || MII->isDebugInstr())
289 auto It = HLV->MIUseDefs.find(&*MII);
290 assert(It != HLV->MIUseDefs.end());
292 if (It->second.second[*AI]) {
296 }
while (MII != MIBegin);
304 assert(
MI &&
"Invalid machine instruction");
305 assert(
MBB &&
"Invalid machine basic block");
306 auto It = HLV->MIUseDefs.find(
MI);
307 assert(It != HLV->MIUseDefs.end() &&
"Missing MI use/def information");
310 lit !=
MBB->livein_end(); ++lit) {
317 return MBBLiveIns.
anyCommon(It->second.second);
326 unsigned BufferPerMBB)
const {
327 assert(HLV->DistanceMap.find(From) != HLV->DistanceMap.end());
328 assert(HLV->DistanceMap.find(To) != HLV->DistanceMap.end());
329 unsigned FromSize = HLV->DistanceMap[From];
334 unsigned S = BufferPerMBB;
335 bool ToFirst =
false;
340 else if (
MBB == To) {
353 S += HLV->DistanceMap[
MBB] + BufferPerMBB;
357 S += FromSize + HLV->DistanceMap[To] + 2 * BufferPerMBB;
362 HLV->clearDistanceMap();
363 HLV->generateDistanceMap(Fn);
369bool HexagonLiveVariablesImpl::runOnMachineFunction(
378 TRI = ST.getRegisterInfo();
379 QII = ST.getInstrInfo();
381 NumRegs =
TRI->getNumRegs();
387 LLVM_DEBUG(
dbgs() <<
"\nNumber of registers in Hexagon is:" << NumRegs);
389 PhysRegDef.resize(NumRegs);
390 PhysRegUse.resize(NumRegs);
394 constructUseDef(&*
MBBI);
396 updateGlobalLiveness(Fn);
401 std::fill(PhysRegDef.begin(), PhysRegDef.end(), (
MachineInstr *)0);
402 std::fill(PhysRegUse.begin(), PhysRegUse.end(), (
MachineInstr *)0);
405 std::pair<BitVector, BitVector> &UseDef = MBBUseDefs[
MBB];
413 Defs.
resize(NumRegs,
false);
414 LiveOuts.
resize(NumRegs,
false);
421 unsigned MBBInsSize = 0;
426 MBBInsSize += QII->getSize(*
MI);
428 if (
MI->isBundle() ||
MI->isDebugInstr())
433 MIUseDef.first.resize(NumRegs);
434 MIUseDef.second.resize(NumRegs);
435 MIUseDef.first.reset();
436 MIUseDef.second.reset();
442 unsigned NumOperandsToProcess =
MI->getNumOperands();
443 for (
unsigned i = 0; i != NumOperandsToProcess; ++i) {
447 if (!QII->isPredicated(*
MI))
458 MIUseDef.first.set(
Reg);
461 if (!QII->isPredicated(*
MI) && !
MI->isKill()) {
467 MIUseDef.second.set(
Reg);
471 for (
unsigned i = 0, e = UseRegs.
size(); i != e; ++i)
472 handlePhysRegUse(UseRegs[i],
MI,
Uses);
474 for (
unsigned i = 0, e = DefRegs.
size(); i != e; ++i)
475 handlePhysRegDef(DefRegs[i],
MI, Defs);
477 DistanceMap[
MBB] = MBBInsSize;
480void HexagonLiveVariablesImpl::handlePhysRegUse(
MachineOperand *MO,
488 if (PhysRegDef[*SupI])
493 bool subRegDefined =
false;
496 if (PhysRegDef[*SubI])
497 subRegDefined =
true;
503 if (undefSubRegs.
empty()) {
504 if (!subRegDefined) {
508 PhysRegUse[*SubI] =
MI;
515 for (
unsigned i = 0; i < undefSubRegs.
size(); ++i) {
517 PhysRegUse[undefSubRegs[i]] =
MI;
518 Uses.set(undefSubRegs[i]);
523void HexagonLiveVariablesImpl::handlePhysRegDef(
MachineOperand *MO,
526 auto SetRegDef = [&](
unsigned Reg) ->
void {
527 PhysRegDef[
Reg] =
MI;
529 if (PhysRegUse[*AI]) {
541 for (
unsigned R = 1, NR =
TRI->getNumRegs(); R != NR; ++R)
564 while (!WorkStack.
empty()) {
566 BlockState &WState = State[W->getNumber()];
571 if (W->succ_empty() || WState.SuccQueued) {
574 WState.SuccQueued =
true;
578 WState.SuccQueued =
true;
590 dbgs() <<
"gatherBlocksDF: {";
593 B != BE; ++
B) { dbgs() <<
" BB#" << (*B)->getNumber(); }
dbgs()
597bool HexagonLiveVariablesImpl::updateGlobalLiveness(
MachineFunction &Fn) {
602 for (++
I;
I !=
E; ++
I) {
603 std::vector<MachineBasicBlock::RegisterMaskPair> OldLiveIn(
604 I->livein_begin(),
I->livein_end());
605 for (
unsigned i = 0; i < OldLiveIn.size(); ++i)
606 I->removeLiveIn(OldLiveIn[i].PhysReg);
617 B = BlocksDepthFirst.begin(),
618 BE = BlocksDepthFirst.end();
620 Repeat |= updateGlobalLiveness(*
B, Defs, LiveIns);
625 Changed |= updateLocalLiveness(Fn);
631 assert(
X &&
"Invalid start block");
632 assert(
Y &&
"Invalid end block");
639 BlocksDepthFirst.end();
641 for (
B = BlocksDepthFirst.begin(); (
B != BE); ++
B) {
652 Repeat |= updateGlobalLiveness(*
B, Defs, LiveIns);
654 B = BlocksDepthFirst.begin();
669 auto LiveOutIt = MBBLiveOuts.find(
MBB);
670 if (LiveOutIt == MBBLiveOuts.end())
671 LiveOutIt = MBBLiveOuts.insert({
MBB,
BitVector(NumRegs)}).first;
680 if (!LiveOuts[(*LI).PhysReg]) {
682 LiveOuts.
set((*LI).PhysReg);
689 Changed |= updateLiveIns(
MBB, LiveIns, LiveOuts);
700 const std::pair<BitVector, BitVector> &UseDefs = MBBUseDefs[
MBB];
704 LiveIns.
reset(UseDefs.second);
705 LiveIns |= UseDefs.first;
724 unsigned R = (*I).PhysReg;
734bool HexagonLiveVariablesImpl::updateLocalLiveness(
MachineFunction &Fn) {
737 updateLocalLiveness(&*
B,
false);
747 updateLiveOuts(
MBB, LiveOut);
754 MII != MIREnd; ++MII) {
757 if (
MI->isBundle()) {
762 if (
MI->isDebugInstr())
766 for (
unsigned i = 0; i <
MI->getNumOperands(); ++i) {
772 if (!QII->isPredicated(*
MI) && !
MI->isKill()) {
779 if (!QII->isPredicated(*
MI))
785 auto RemoveDef = [&](
unsigned Reg,
bool Implicit) ->
void {
791 for (
auto *UseOp : UseRegs)
792 if (UseOp->isImplicit() &&
TRI->regsOverlap(*
SI, UseOp->getReg()))
793 Used.set(UseOp->getReg());
797 for (
unsigned i = 0; i < DefRegs.
size(); ++i) {
802 for (
unsigned R = 1, NR =
TRI->getNumRegs(); R != NR; ++R)
808 for (
unsigned i = UseRegs.
size(); i > 0;) {
810 unsigned UseReg = UseRegs[i]->getReg();
817 if (Killed && !UseRegs[i]->
isDebug())
818 UseRegs[i]->setIsKill(
true);
823 MII != BundleHeads.
end(); ++MII) {
825 assert(
MI &&
"Invalid bundle head");
826 assert(
MI->isBundle() &&
"Expected a bundle head instruction");
827 assert(
MI->getParent() ==
MBB &&
"Bundle head not in expected block");
830 for (++BS; BS != BE; ++BS)
832 BS->unbundleFromPred();
834 BS =
MI->getIterator();
836 bool memShufDisabled = QII->getBundleNoShuf(*
MI);
837 MI->eraseFromParent();
841 QII->setBundleNoShuf(BundleMII);
847bool HexagonLiveVariablesImpl::incrementalUpdate(MICInstIterType MIDelta,
853 constructUseDef(From);
858 updateGlobalLiveness(From, To);
860 updateLocalLiveness(From,
true);
861 updateLocalLiveness(To,
true);
866 if (MIUseDef == MIUseDefs.end())
868 const BitVector &Defs = MIUseDef->second.second;
882 constructUseDef(
MBB);
884 updateGlobalLiveness(
MBB,
MBB);
891 constructUseDef(
MBB);
892 updateGlobalLiveness(
MBB,
MBB);
895void HexagonLiveVariablesImpl::generateDistanceMap(
const MachineFunction &Fn) {
896 assert(DistanceMap.empty() &&
"DistanceMap not empty, first clear!");
900 unsigned MBBInsSize = 0;
905 MBBInsSize += QII->getSize(*
MI);
907 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:
BitVector & reset()
Reset all bits in the bitvector.
int find_first() const
Returns the index of the first set bit, -1 if none of the bits are set.
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
bool anyCommon(const BitVector &RHS) const
Test if any common bits are set.
BitVector & set()
Set all bits in the bitvector.
int find_next(unsigned Prev) const
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.