21#define DEBUG_TYPE "reaching-defs-analysis"
39 OS <<
"Reaching definitions for for machine function: " << MF.
getName()
46 "Reaching Definitions Analysis",
false,
true)
61 MachineFunctionAnalysisManager::Invalidator &) {
65 return !PAC.preserved() &&
108 int DefFrameIndex = 0;
109 int SrcFrameIndex = 0;
110 if (
TII->isStoreToStackSlot(
MI, DefFrameIndex) ||
111 TII->isStackSlotCopy(
MI, DefFrameIndex, SrcFrameIndex))
112 return DefFrameIndex == FrameIndex;
118 assert(MBBNumber < MBBReachingDefs.numBlockIDs() &&
119 "Unexpected basic block number.");
120 MBBReachingDefs.startBasicBlock(MBBNumber, NumRegUnits);
127 if (LiveRegs.empty())
128 LiveRegs.assign(NumRegUnits, ReachingDefDefaultVal);
133 for (
MCRegUnit Unit : TRI->regunits(LI.PhysReg)) {
137 if (LiveRegs[Unit] != -1) {
139 MBBReachingDefs.append(MBBNumber, Unit, -1);
149 assert(
unsigned(pred->getNumber()) < MBBOutRegsInfos.size() &&
150 "Should have pre-allocated MBBInfos for all MBBs");
151 const LiveRegsDefInfo &Incoming = MBBOutRegsInfos[pred->getNumber()];
154 if (Incoming.empty())
158 for (
unsigned Unit = 0;
Unit != NumRegUnits; ++
Unit)
159 LiveRegs[Unit] = std::max(LiveRegs[Unit], Incoming[Unit]);
163 for (
unsigned Unit = 0;
Unit != NumRegUnits; ++
Unit)
164 if (LiveRegs[Unit] != ReachingDefDefaultVal)
165 MBBReachingDefs.append(MBBNumber, Unit, LiveRegs[Unit]);
169 assert(!LiveRegs.empty() &&
"Must enter basic block first.");
171 assert(MBBNumber < MBBOutRegsInfos.size() &&
172 "Unexpected basic block number.");
174 MBBOutRegsInfos[MBBNumber] = LiveRegs;
180 for (
int &OutLiveReg : MBBOutRegsInfos[MBBNumber])
181 if (OutLiveReg != ReachingDefDefaultVal)
182 OutLiveReg -= CurInstr;
187 assert(!
MI->isDebugInstr() &&
"Won't process debug instructions");
189 unsigned MBBNumber =
MI->getParent()->getNumber();
190 assert(MBBNumber < MBBReachingDefs.numBlockIDs() &&
191 "Unexpected basic block number.");
193 for (
auto &MO :
MI->operands()) {
198 MBBFrameObjsReachingDefs[{MBBNumber,
FrameIndex}].push_back(CurInstr);
202 for (
MCRegUnit Unit : TRI->regunits(MO.getReg().asMCReg())) {
208 if (LiveRegs[Unit] != CurInstr) {
209 LiveRegs[
Unit] = CurInstr;
210 MBBReachingDefs.append(MBBNumber, Unit, CurInstr);
214 InstIds[
MI] = CurInstr;
220 assert(MBBNumber < MBBReachingDefs.numBlockIDs() &&
221 "Unexpected basic block number.");
226 int NumInsts = std::distance(NonDbgInsts.begin(), NonDbgInsts.end());
231 assert(
unsigned(pred->getNumber()) < MBBOutRegsInfos.size() &&
232 "Should have pre-allocated MBBInfos for all MBBs");
233 const LiveRegsDefInfo &Incoming = MBBOutRegsInfos[pred->getNumber()];
235 if (Incoming.empty())
238 for (
unsigned Unit = 0;
Unit != NumRegUnits; ++
Unit) {
240 if (Def == ReachingDefDefaultVal)
243 auto Defs = MBBReachingDefs.defs(MBBNumber, Unit);
244 if (!Defs.empty() && Defs.front() < 0) {
245 if (Defs.front() >= Def)
249 MBBReachingDefs.replaceFront(MBBNumber, Unit, Def);
252 MBBReachingDefs.prepend(MBBNumber, Unit, Def);
257 if (MBBOutRegsInfos[MBBNumber][Unit] < Def - NumInsts)
258 MBBOutRegsInfos[MBBNumber][
Unit] =
Def - NumInsts;
263void ReachingDefInfo::processBasicBlock(
265 MachineBasicBlock *
MBB = TraversedMBB.
MBB;
267 << (!TraversedMBB.
IsDone ?
": incomplete\n"
268 :
": all preds known\n"));
272 reprocessBasicBlock(
MBB);
276 enterBasicBlock(
MBB);
277 for (MachineInstr &
MI :
280 leaveBasicBlock(
MBB);
288 LLVM_DEBUG(
dbgs() <<
"********** REACHING DEFINITION ANALYSIS **********\n");
294 OS <<
"RDA results for " << MF->getName() <<
"\n";
303 int FrameIndex = MO.getIndex();
305 }
else if (MO.isReg()) {
325 OS << Num <<
": " <<
MI <<
"\n";
326 InstToNumMap[&
MI] = Num;
339 MBBOutRegsInfos.clear();
340 MBBReachingDefs.clear();
341 MBBFrameObjsReachingDefs.clear();
353 NumRegUnits = TRI->getNumRegUnits();
354 NumStackObjects = MF->getFrameInfo().getNumObjects();
355 ObjectIndexBegin = MF->getFrameInfo().getObjectIndexBegin();
356 MBBReachingDefs.init(MF->getNumBlockIDs());
358 MBBOutRegsInfos.resize(MF->getNumBlockIDs());
360 TraversedMBBOrder = Traversal.
traverse(*MF);
366 processBasicBlock(TraversedMBB);
369 for (
unsigned MBBNumber = 0, NumBlockIDs = MF->getNumBlockIDs();
370 MBBNumber != NumBlockIDs; ++MBBNumber) {
371 for (
unsigned Unit = 0; Unit != NumRegUnits; ++Unit) {
372 int LastDef = ReachingDefDefaultVal;
373 for (
int Def : MBBReachingDefs.defs(MBBNumber, Unit)) {
374 assert(Def > LastDef &&
"Defs must be sorted and unique");
383 assert(InstIds.count(
MI) &&
"Unexpected machine instuction.");
384 int InstId = InstIds.lookup(
MI);
385 int DefRes = ReachingDefDefaultVal;
386 unsigned MBBNumber =
MI->getParent()->getNumber();
387 assert(MBBNumber < MBBReachingDefs.numBlockIDs() &&
388 "Unexpected basic block number.");
389 int LatestDef = ReachingDefDefaultVal;
393 int FrameIndex = Reg.stackSlotIndex();
394 auto Lookup = MBBFrameObjsReachingDefs.find({MBBNumber, FrameIndex});
395 if (
Lookup == MBBFrameObjsReachingDefs.end())
397 auto &Defs =
Lookup->second;
398 for (
int Def : Defs) {
403 LatestDef = std::max(LatestDef, DefRes);
407 for (
MCRegUnit Unit : TRI->regunits(Reg)) {
408 for (
int Def : MBBReachingDefs.defs(MBBNumber, Unit)) {
413 LatestDef = std::max(LatestDef, DefRes);
429 if (ParentA != ParentB)
437 assert(
static_cast<size_t>(
MBB->getNumber()) <
439 "Unexpected basic block number.");
440 assert(InstId <
static_cast<int>(
MBB->size()) &&
441 "Unexpected instruction id.");
446 for (
auto &
MI : *
MBB) {
447 auto F = InstIds.find(&
MI);
448 if (
F != InstIds.end() &&
F->second == InstId)
456 assert(InstIds.count(
MI) &&
"Unexpected machine instuction.");
465 InstSet &
Uses)
const {
468 while (++
MI !=
MBB->end()) {
469 if (
MI->isDebugInstr())
474 if (getReachingLocalMIDef(&*
MI, Reg) != Def)
477 for (
auto &MO :
MI->operands()) {
489 InstSet &
Uses)
const {
492 for (
auto &MO :
MI.operands()) {
500 auto Last =
MBB->getLastNonDebugInstr();
507 InstSet &
Uses)
const {
520 while (!ToVisit.
empty()) {
532 InstSet &Defs)
const {
538 for (
auto *
MBB :
MI->getParent()->predecessors())
543 InstSet &Defs)
const {
549 InstSet &Defs, BlockSet &VisitedBBs)
const {
555 LiveRegs.addLiveOuts(*
MBB);
556 if (Reg.isPhysical() && LiveRegs.available(Reg))
562 for (
auto *Pred :
MBB->predecessors())
570 if (LocalDef && InstIds.lookup(LocalDef) < InstIds.lookup(
MI))
587 unsigned Idx)
const {
588 assert(
MI->getOperand(Idx).isReg() &&
"Expected register operand");
601 LiveRegs.addLiveOuts(*
MBB);
604 if (!LiveRegs.available(Reg))
611 LiveRegs.stepBackward(
Last);
612 if (!LiveRegs.available(Reg))
613 return InstIds.lookup(&
Last) > InstIds.lookup(
MI);
620 auto Last =
MBB->getLastNonDebugInstr();
626 return Def == getReachingLocalMIDef(
MI, Reg);
635 LiveRegs.addLiveOuts(*
MBB);
636 if (Reg.isPhysical() && LiveRegs.available(Reg))
639 auto Last =
MBB->getLastNonDebugInstr();
645 for (
auto &MO :
Last->operands())
655 LiveRegs.addLiveOuts(*
MBB);
656 if (Reg.isPhysical() && LiveRegs.available(Reg))
659 auto Last =
MBB->getLastNonDebugInstr();
664 int FrameIndex = Reg.stackSlotIndex();
671 for (
auto &MO :
Last->operands())
675 return Def < 0 ? nullptr : getInstFromId(
MBB, Def);
679 return MI.mayLoadOrStore() ||
MI.mayRaiseFPException() ||
680 MI.hasUnmodeledSideEffects() ||
MI.isTerminator() ||
681 MI.isCall() ||
MI.isBarrier() ||
MI.isBranch() ||
MI.isReturn();
687template <
typename Iterator>
692 SmallSet<Register, 2> Defs;
706 for (
auto I = ++Iterator(From),
E = Iterator(To);
I !=
E; ++
I) {
709 for (
auto &MO :
I->operands())
710 if (MO.isReg() && MO.getReg() && Defs.
count(MO.getReg()))
720 for (
auto I = Iterator(From), E = From->
getParent()->
end();
I != E; ++
I)
722 return isSafeToMove<Iterator>(From, To);
732 return isSafeToMove<Iterator>(From, To);
760 for (
auto &MO :
MI->operands()) {
767 for (
auto *
I :
Uses) {
779 InstSet &Dead)
const {
785 unsigned LiveDefs = 0;
786 for (
auto &MO : Def->operands()) {
801 for (
auto &MO :
MI->operands()) {
805 if (
IsDead(Def, MO.getReg()))
819 if (
auto *Def = getReachingLocalMIDef(
MI, Reg)) {
832 for (
auto E =
MBB->end();
I != E; ++
I) {
835 for (
auto &MO :
I->operands())
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
ReachingDefInfo InstSet InstSet & Ignore
ReachingDefInfo InstSet & ToRemove
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
const HexagonInstrInfo * TII
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static bool isValidRegUseOf(const MachineOperand &MO, Register Reg, const TargetRegisterInfo *TRI)
static bool mayHaveSideEffects(MachineInstr &MI)
static bool isValidReg(const MachineOperand &MO)
static bool isFIDef(const MachineInstr &MI, int FrameIndex, const TargetInstrInfo *TII)
static bool isValidRegDef(const MachineOperand &MO)
static bool isValidRegDefOf(const MachineOperand &MO, Register Reg, const TargetRegisterInfo *TRI)
static bool isValidRegUse(const MachineOperand &MO)
Remove Loads Into Fake Uses
This file defines generic set operations that may be used on set's of different types,...
This file defines the SmallSet class.
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Represents analyses that only rely on functions' control flow.
A set of register units used to track register liveness.
This class provides the basic blocks traversal order used by passes like ReachingDefAnalysis and Exec...
TraversalOrder traverse(MachineFunction &MF)
unsigned numBlockIDs() const
An RAII based helper class to modify MachineFunctionProperties when running pass.
instr_iterator instr_begin()
iterator_range< livein_iterator > liveins() const
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
instr_iterator instr_end()
iterator_range< pred_iterator > predecessors()
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Properties which a MachineFunction may have at a given point in time.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
MachineFunctionProperties getRequiredProperties() const override
This class provides the reaching def analysis.
MachineInstr * getUniqueReachingMIDef(MachineInstr *MI, Register Reg) const
If a single MachineInstr creates the reaching definition, then return it.
bool isReachingDefLiveOut(MachineInstr *MI, Register Reg) const
Return whether the reaching def for MI also is live out of its parent block.
bool isSafeToMoveForwards(MachineInstr *From, MachineInstr *To) const
Return whether From can be moved forwards to just before To.
int getReachingDef(MachineInstr *MI, Register Reg) const
Provides the instruction id of the closest reaching def instruction of Reg that reaches MI,...
void run(MachineFunction &mf)
void getReachingLocalUses(MachineInstr *MI, Register Reg, InstSet &Uses) const
Provides the uses, in the same block as MI, of register that MI defines.
int getClearance(MachineInstr *MI, Register Reg) const
Provides the clearance - the number of instructions since the closest reaching def instuction of Reg ...
bool isRegDefinedAfter(MachineInstr *MI, Register Reg) const
Return whether the given register is defined after MI.
void init()
Initialize data structures.
void print(raw_ostream &OS)
bool hasLocalDefBefore(MachineInstr *MI, Register Reg) const
Provide whether the register has been defined in the same basic block as, and before,...
void reset()
Re-run the analysis.
void getGlobalUses(MachineInstr *MI, Register Reg, InstSet &Uses) const
Collect the users of the value stored in Reg, which is defined by MI.
MachineInstr * getMIOperand(MachineInstr *MI, unsigned Idx) const
If a single MachineInstr creates the reaching definition, for MIs operand at Idx, then return it.
void getLiveOuts(MachineBasicBlock *MBB, Register Reg, InstSet &Defs, BlockSet &VisitedBBs) const
Search MBB for a definition of Reg and insert it into Defs.
void traverse()
Traverse the machine function, mapping definitions.
bool isSafeToMoveBackwards(MachineInstr *From, MachineInstr *To) const
Return whether From can be moved backwards to just after To.
void collectKilledOperands(MachineInstr *MI, InstSet &Dead) const
Assuming MI is dead, recursively search the incoming operands which are killed by MI and collect thos...
bool hasSameReachingDef(MachineInstr *A, MachineInstr *B, Register Reg) const
Return whether A and B use the same def of Reg.
bool isRegUsedAfter(MachineInstr *MI, Register Reg) const
Return whether the given register is used after MI, whether it's a local use or a live out.
void getGlobalReachingDefs(MachineInstr *MI, Register Reg, InstSet &Defs) const
Collect all possible definitions of the value stored in Reg, which is used by MI.
bool isSafeToRemove(MachineInstr *MI, InstSet &ToRemove) const
Return whether removing this instruction will have no effect on the program, returning the redundant ...
MachineInstr * getLocalLiveOutMIDef(MachineBasicBlock *MBB, Register Reg) const
Return the local MI that produces the live out value for Reg, or nullptr for a non-live out or non-lo...
bool invalidate(MachineFunction &F, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
bool getLiveInUses(MachineBasicBlock *MBB, Register Reg, InstSet &Uses) const
For the given block, collect the instructions that use the live-in value of the provided register.
bool isSafeToDefRegAt(MachineInstr *MI, Register Reg) const
Return whether a MachineInstr could be inserted at MI and safely define the given register without af...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Wrapper class representing virtual and physical registers.
static Register index2StackSlot(int FI)
Convert a non-negative frame index to a stack slot register value.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
This class implements an extremely fast bulk output stream that can only output to a stream.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
NodeAddr< DefNode * > Def
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void initializeReachingDefInfoWrapperPassPass(PassRegistry &)
LLVM_ABI Printable printRegUnit(unsigned Unit, const TargetRegisterInfo *TRI)
Create Printable object to print register units on a raw_ostream.
bool set_is_subset(const S1Ty &S1, const S2Ty &S2)
set_is_subset(A, B) - Return true iff A in B
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned MCRegUnit
Register units are used to compute register aliasing.
auto instructionsWithoutDebug(IterT It, IterT End, bool SkipPseudoOp=true)
Construct a range iterator which begins at It and moves forwards until End is reached,...
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
MachineBasicBlock * MBB
The basic block.
bool IsDone
True if the block that is ready for its final round of processing.
bool PrimaryPass
True if this is the first time we process the basic block.