30 const DependencyGraph &DAG) {
31 auto Skip = [&DAG](
auto OpIt) {
33 return I ==
nullptr || DAG.getNode(
I) ==
nullptr;
35 while (OpIt != OpItE && Skip(OpIt))
43 assert(OpIt != OpItE &&
"Can't dereference end iterator!");
52 "Cant' dereference end iterator!");
59 assert(OpIt != OpItE &&
"Already at end!");
62 OpIt = PredIterator::skipBadIt(OpIt, OpItE, *DAG);
70 OpIt = PredIterator::skipBadIt(OpIt, OpItE, *DAG);
80 assert(DAG ==
Other.DAG &&
"Iterators of different DAGs!");
81 assert(N ==
Other.N &&
"Iterators of different nodes!");
82 return OpIt ==
Other.OpIt && MemIt ==
Other.MemIt;
90 return I ==
nullptr || DAG.
getNode(
I) ==
nullptr;
92 while (UserIt != UserItE && Skip(UserIt))
100 assert(UserIt != UserItE &&
"Can't dereference end iterator!");
105 if (UserIt != UserItE)
109 "Cant' dereference end iterator!");
116 assert(UserIt != UserItE &&
"Already at end!");
119 UserIt = SuccIterator::skipOutOfScope(UserIt, UserItE, *DAG);
124 if (UserIt != UserItE) {
127 UserIt = SuccIterator::skipOutOfScope(UserIt, UserItE, *DAG);
137 assert(DAG ==
Other.DAG &&
"Iterators of different DAGs!");
138 assert(N ==
Other.N &&
"Iterators of different nodes!");
139 return UserIt ==
Other.UserIt && MemIt ==
Other.MemIt;
143 if (this->SB !=
nullptr)
144 this->SB->eraseFromBundle(
this);
151 SB->eraseFromBundle(
this);
162 unsigned CntUnschedDeps = 0;
164 if (!SuccN->scheduled())
170 unsigned CntUnschedDeps = 0;
172 if (!PredN->scheduled())
183 OS << *
I <<
" Unsched:";
195 static constexpr unsigned Indent = 4;
196 for (
auto *
Pred : MemPreds)
197 OS.
indent(Indent) <<
"<-" << *
Pred->getInstruction() <<
"\n";
198 for (
auto *Succ : MemSuccs)
199 OS.
indent(Indent) <<
"->" << *Succ->getInstruction() <<
"\n";
211 I =
I->getNextNode();
224 I =
I->getPrevNode();
237 if (TopMemN ==
nullptr)
240 assert(BotMemN !=
nullptr &&
"TopMemN should be null too!");
245DependencyGraph::DependencyType
250 return DependencyType::ReadAfterWrite;
252 return DependencyType::WriteAfterWrite;
255 return DependencyType::WriteAfterRead;
258 return DependencyType::Control;
260 return DependencyType::Control;
263 return DependencyType::Other;
264 return DependencyType::None;
270 return !LI->isUnordered();
272 return !
SI->isUnordered();
277 bool Is = IsOrdered(
I);
279 "An ordered instruction must be a MemDepCandidate!");
284 DependencyType DepType) {
285 std::optional<MemoryLocation> DstLocOpt =
291 "Expected a mem instr");
298 case DependencyType::ReadAfterWrite:
299 case DependencyType::WriteAfterWrite:
301 case DependencyType::WriteAfterRead:
309 DependencyType RoughDepType = getRoughDepType(SrcI, DstI);
310 switch (RoughDepType) {
311 case DependencyType::ReadAfterWrite:
312 case DependencyType::WriteAfterWrite:
313 case DependencyType::WriteAfterRead:
314 return alias(SrcI, DstI, RoughDepType);
315 case DependencyType::Control:
321 case DependencyType::Other:
323 case DependencyType::None:
329void DependencyGraph::scanAndAddDeps(
MemDGNode &DstN,
332 "DstN is the mem dep destination, so it must be mem");
338 if (hasDep(SrcI, DstI))
339 DstN.addMemPred(&SrcN, Dir);
343void DependencyGraph::setDefUseUnscheduledSuccs(
353 unsigned CntUnschedPreds = 0;
359 if (OpI->getParent() !=
I.getParent())
361 if (!NewInterval.contains(OpI))
367 OpN->incrUnscheduledDeps();
368 if (!OpN->scheduled())
376 bool NewIsAbove = DAGInterval.empty() || NewInterval.comesBefore(DAGInterval);
377 const auto &TopInterval = NewIsAbove ? NewInterval : DAGInterval;
378 const auto &BotInterval = NewIsAbove ? DAGInterval : NewInterval;
392 if (BotN->scheduled())
394 unsigned CntUnscheduledPreds = 0;
395 for (
Value *
Op : BotI.operands()) {
402 if (!TopInterval.contains(OpI))
404 if (!OpN->scheduled()) {
406 OpN->incrUnscheduledDeps();
407 ++CntUnscheduledPreds;
411 *BotN->UnscheduledDeps += CntUnscheduledPreds;
423 MemN->setPrevNode(LastMemN);
428 if (!DAGInterval.empty()) {
429 bool NewIsAbove = NewInterval.comesBefore(DAGInterval);
430 const auto &TopInterval = NewIsAbove ? NewInterval : DAGInterval;
431 const auto &BotInterval = NewIsAbove ? DAGInterval : NewInterval;
436 assert((LinkTopN ==
nullptr || LinkBotN ==
nullptr ||
437 LinkTopN->comesBefore(LinkBotN)) &&
439 if (LinkTopN !=
nullptr && LinkBotN !=
nullptr) {
440 LinkTopN->setNextNode(LinkBotN);
445 auto UnionIntvl = DAGInterval.getUnionInterval(NewInterval);
450 if (ChainTopN !=
nullptr && ChainBotN !=
nullptr) {
451 for (
auto *
N = ChainTopN->getNextNode(), *LastN = ChainTopN;
N !=
nullptr;
452 LastN =
N,
N =
N->getNextNode()) {
453 assert(
N == LastN->getNextNode() &&
"Bad chain!");
454 assert(
N->getPrevNode() == LastN &&
"Bad chain!");
460 setDefUseUnscheduledSuccs(NewInterval);
466 for (
auto *PrevI = IncludingN ?
I :
I->getPrevNode(); PrevI !=
nullptr;
467 PrevI = PrevI->getPrevNode()) {
469 if (PrevN ==
nullptr)
472 if (PrevMemN !=
nullptr && PrevMemN != SkipN)
481 for (
auto *NextI = IncludingN ?
I :
I->getNextNode(); NextI !=
nullptr;
482 NextI = NextI->getNextNode()) {
484 if (NextN ==
nullptr)
487 if (NextMemN !=
nullptr && NextMemN != SkipN)
493void DependencyGraph::notifyCreateInstr(
Instruction *
I) {
498 if (!(DAGInterval.contains(
I) || DAGInterval.touches(
I)))
501 DAGInterval = DAGInterval.getUnionInterval({
I,
I});
506 Sched->notifyCreateInstrBeforeDAGUpdates(
N);
509 if (
MemN !=
nullptr) {
510 if (
auto *PrevMemN = getMemDGNodeBefore(
MemN,
false)) {
511 PrevMemN->NextMemN =
MemN;
512 MemN->PrevMemN = PrevMemN;
514 if (
auto *NextMemN = getMemDGNodeAfter(
MemN,
false)) {
515 NextMemN->PrevMemN =
MemN;
516 MemN->NextMemN = NextMemN;
521 if (DAGInterval.top()->comesBefore(
I)) {
524 scanAndAddDeps(*
MemN, SrcInterval);
527 if (
I->comesBefore(DAGInterval.bottom())) {
539 if (!
N->scheduled()) {
549 if (!OpN->scheduled())
550 OpN->incrUnscheduledDeps();
552 for (User *U :
I->users()) {
559 if (!UN->scheduled())
560 N->incrUnscheduledDeps();
571 if (!OpN->scheduled())
572 N->incrUnscheduledDeps();
574 for (User *U :
I->users()) {
581 if (!UN->scheduled())
582 N->incrUnscheduledDeps();
588 if (Sched !=
nullptr) {
589 Sched->notifyCreateInstrAfterDAGUpdates(
N);
597void DependencyGraph::notifyMoveInstr(
Instruction *
I,
const BBIterator &To) {
606 assert(!(To != BB->end() && &*To ==
I->getNextNode()) &&
607 !(To == BB->end() && std::next(
I->getIterator()) == BB->end()) &&
608 "Should not have been called if destination is same as origin.");
612 assert(To.getNodeParent() ==
I->getParent() &&
613 "TODO: We don't support movement across BBs!");
615 (To == std::next(DAGInterval.bottom()->getIterator()) ||
616 (To != BB->end() && std::next(To) == DAGInterval.top()->getIterator()) ||
617 (To != BB->end() && DAGInterval.contains(&*To))) &&
618 "TODO: To should be either within the DAGInterval or right "
622 auto OrigDAGInterval = DAGInterval;
625 DAGInterval.notifyMoveInstr(
I, To);
638 MemN->detachFromChain();
653 if (To == BB->end() ||
654 To == std::next(OrigDAGInterval.bottom()->getIterator())) {
659 getMemDGNodeBefore(InsertAfterN,
true,
MemN));
664 getMemDGNodeBefore(BeforeToN,
false,
MemN));
666 getMemDGNodeAfter(BeforeToN,
true,
MemN));
690 auto *PrevMemN = getMemDGNodeBefore(
MemN,
false);
691 auto *NextMemN = getMemDGNodeAfter(
MemN,
false);
692 if (PrevMemN !=
nullptr)
693 PrevMemN->NextMemN = NextMemN;
694 if (NextMemN !=
nullptr)
695 NextMemN->PrevMemN = PrevMemN;
698 while (!
MemN->memPreds().empty()) {
699 auto *PredN = *
MemN->memPreds().begin();
700 MemN->removeMemPred(PredN, Dir);
702 while (!
MemN->memSuccs().empty()) {
703 auto *SuccN = *
MemN->memSuccs().begin();
704 SuccN->removeMemPred(
MemN, Dir);
709 if (!
N->scheduled()) {
717 if (PredN && !PredN->scheduled())
718 PredN->decrUnscheduledDeps();
721 for (User *U :
I->users()) {
725 if (SuccN && !SuccN->scheduled())
726 SuccN->decrUnscheduledDeps();
731 if (Sched !=
nullptr)
732 Sched->notifyEraseInstr(
I);
735 InstrToNodeMap.erase(
I);
738void DependencyGraph::notifySetUse(
const Use &U,
Value *NewSrc) {
749 if (UserI ==
nullptr)
752 if (UserN ==
nullptr)
756 if (UserN->scheduled())
761 if (
auto *CurrSrcN =
getNode(CurrSrcI)) {
763 if (!CurrSrcN->scheduled()) {
765 CurrSrcN->decrUnscheduledDeps();
767 UserN->decrUnscheduledDeps();
772 if (
auto *NewSrcN =
getNode(NewSrcI)) {
774 if (!NewSrcN->scheduled()) {
776 NewSrcN->incrUnscheduledDeps();
778 UserN->incrUnscheduledDeps();
790 auto NewInterval = Union.getSingleDiff(DAGInterval);
791 if (NewInterval.empty())
794 createNewNodes(NewInterval);
810 if (!DstRange.empty()) {
813 scanAndAddDeps(DstN, SrcRange);
818 if (MemDAGInterval.empty()) {
819 FullScan(NewInterval);
836 else if (DAGInterval.bottom()->comesBefore(NewInterval.top())) {
838 auto SrcRangeFull = MemDAGInterval.getUnionInterval(DstRange);
842 scanAndAddDeps(DstN, SrcRange);
846 else if (NewInterval.bottom()->comesBefore(DAGInterval.top())) {
860 FullScan(NewInterval);
876 auto DstRangeOld = MemDAGInterval;
879 scanAndAddDeps(DstN, SrcRange);
892 if (FromSuccN == ToN)
896 if (ToPredN == FromN)
901 for (
const auto &[I1, NPtr1] : InstrToNodeMap) {
902 NPtr1->verify(*
this);
903 for (
const auto &[I2, NPtr2] : InstrToNodeMap) {
904 if (Dependency(NPtr1.get(), NPtr2.get())) {
905 assert(I1->comesBefore(I2) &&
906 "Instruction ordering violates dependency!");
914 Nodes.
reserve(InstrToNodeMap.size());
915 for (
const auto &Pair : InstrToNodeMap)
921 for (
auto *
N : Nodes)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
bool empty() const
Check if the array is empty.
LLVM_ABI bool mayWriteToMemory() const LLVM_READONLY
Return true if this instruction may modify memory.
LLVM_ABI bool mayReadFromMemory() const LLVM_READONLY
Return true if this instruction may read memory.
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
This class implements an extremely fast bulk output stream that can only output to a stream.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
An ArrayRef of Values or Instructions that we can print/dump for debugging.
A DependencyGraph Node that points to an Instruction and contains memory dependency edges.
friend class DependencyGraph
virtual void print(raw_ostream &OS, bool PrintDeps=true) const
static bool isMemDepCandidate(Instruction *I)
We consider I as a Memory Dependency Candidate instruction if it reads/write memory or if it has side...
std::optional< unsigned > UnscheduledDeps
The number of unscheduled successors in the bottom-up scheduling direction (or predecessors in top-do...
DGNode(Instruction *I, DGNodeID ID)
void setSchedBundle(SchedBundle &SB)
void verify(DependencyGraph &DAG) const
iterator_range< iterator > preds(DependencyGraph &DAG) const
\Returns a range of DAG predecessors nodes.
SchedBundle * SB
The scheduler bundle that this node belongs to.
bool Scheduled
This is true if this node has been scheduled.
static bool isMemDepNodeCandidate(Instruction *I)
\Returns true if I is a memory dependency candidate instruction.
iterator_range< succ_iterator > succs(DependencyGraph &DAG) const
\Returns a range of DAG successor nodes.
static bool isFenceLike(Instruction *I)
\Returns true if I is fence like. It excludes non-mem intrinsics.
LLVM_DUMP_METHOD void dump() const
Instruction * getInstruction() const
static bool isStackSaveOrRestoreIntrinsic(Instruction *I)
bool empty() const
\Returns true if the DAG's state is clear. Used in assertions.
LLVM_DUMP_METHOD void dump() const
DGNode * getNode(Instruction *I) const
LLVM_ABI Interval< Instruction > extend(BndlRef< Instruction * > Instrs)
Build/extend the dependency graph such that it includes Instrs.
DGNode * getNodeOrNull(Instruction *I) const
Like getNode() but returns nullptr if I is nullptr.
void print(raw_ostream &OS) const
DGNode * getOrCreateNode(Instruction *I)
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
bool mayWriteToMemory() const
bool mayReadFromMemory() const
bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
bool isTerminator() const
static LLVM_ABI MemDGNode * getBotMemDGNode(const Interval< Instruction > &Intvl, const DependencyGraph &DAG)
Scans the instruction chain in Intvl bottom-up, returning the bottom-most MemDGNode,...
static LLVM_ABI MemDGNode * getTopMemDGNode(const Interval< Instruction > &Intvl, const DependencyGraph &DAG)
Scans the instruction chain in Intvl top-down, returning the top-most MemDGNode, or nullptr.
static LLVM_ABI Interval< MemDGNode > make(const Interval< Instruction > &Instrs, DependencyGraph &DAG)
Given Instrs it finds their closest mem nodes in the interval and returns the corresponding mem range...
A DependencyGraph Node for instructions that may read/write memory, or have some ordering constraints...
void print(raw_ostream &OS, bool PrintDeps=true) const override
LLVM_ABI value_type operator*()
LLVM_ABI PredIterator & operator++()
LLVM_ABI bool operator==(const PredIterator &Other) const
LLVM_ABI value_type operator*()
LLVM_ABI bool operator==(const SuccIterator &Other) const
LLVM_ABI SuccIterator & operator++()
Represents a Def-use/Use-def edge in SandboxIR.
static ModRefInfo aliasAnalysisGetModRefInfo(BatchAAResults &BatchAA, const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
Equivalent to BatchAA::getModRefInfo().
static std::optional< llvm::MemoryLocation > memoryLocationGetOrNone(const Instruction *I)
Equivalent to MemoryLocation::getOrNone(I).
A SandboxIR Value has users. This is the base class.
mapped_iterator< sandboxir::UserUseIterator, UseToUser > user_iterator
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static bool isOrdered(Instruction *I)
StringLiteral schedDirectionToStr(SchedDirection Dir)
template class LLVM_TEMPLATE_ABI Interval< MemDGNode >
BasicBlock(llvm::BasicBlock *BB, Context &SBCtx)
template class LLVM_TEMPLATE_ABI Interval< Instruction >
friend class Instruction
Iterator for Instructions in a `BasicBlock.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
auto dyn_cast_or_null(const Y &Val)
auto reverse(ContainerTy &&C)
bool isModSet(const ModRefInfo MRI)
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
@ ModRef
The access may reference and may modify the value stored in memory.
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
bool isRefSet(const ModRefInfo MRI)