31 const DependencyGraph &DAG) {
32 auto Skip = [&DAG](
auto OpIt) {
34 return I ==
nullptr || DAG.getNode(
I) ==
nullptr;
36 while (OpIt != OpItE && Skip(OpIt))
44 assert(OpIt != OpItE &&
"Can't dereference end iterator!");
53 "Cant' dereference end iterator!");
60 assert(OpIt != OpItE &&
"Already at end!");
63 OpIt = PredIterator::skipBadIt(OpIt, OpItE, *DAG);
71 OpIt = PredIterator::skipBadIt(OpIt, OpItE, *DAG);
81 assert(DAG ==
Other.DAG &&
"Iterators of different DAGs!");
82 assert(N ==
Other.N &&
"Iterators of different nodes!");
83 return OpIt ==
Other.OpIt && MemIt ==
Other.MemIt;
91 return I ==
nullptr || DAG.
getNode(
I) ==
nullptr;
93 while (UserIt != UserItE && Skip(UserIt))
101 assert(UserIt != UserItE &&
"Can't dereference end iterator!");
106 if (UserIt != UserItE)
110 "Cant' dereference end iterator!");
117 assert(UserIt != UserItE &&
"Already at end!");
120 UserIt = SuccIterator::skipOutOfScope(UserIt, UserItE, *DAG);
125 if (UserIt != UserItE) {
128 UserIt = SuccIterator::skipOutOfScope(UserIt, UserItE, *DAG);
138 assert(DAG ==
Other.DAG &&
"Iterators of different DAGs!");
139 assert(N ==
Other.N &&
"Iterators of different nodes!");
140 return UserIt ==
Other.UserIt && MemIt ==
Other.MemIt;
144 if (this->SB !=
nullptr)
145 this->SB->eraseFromBundle(
this);
152 SB->eraseFromBundle(
this);
165 static constexpr unsigned Indent = 4;
166 for (
auto *Pred : MemPreds)
167 OS.
indent(Indent) <<
"<-" << *Pred->getInstruction() <<
"\n";
179 I =
I->getNextNode();
192 I =
I->getPrevNode();
205 if (TopMemN ==
nullptr)
208 assert(BotMemN !=
nullptr &&
"TopMemN should be null too!");
213DependencyGraph::DependencyType
218 return DependencyType::ReadAfterWrite;
220 return DependencyType::WriteAfterWrite;
223 return DependencyType::WriteAfterRead;
226 return DependencyType::Control;
228 return DependencyType::Control;
231 return DependencyType::Other;
232 return DependencyType::None;
238 return !LI->isUnordered();
240 return !
SI->isUnordered();
245 bool Is = IsOrdered(
I);
247 "An ordered instruction must be a MemDepCandidate!");
252 DependencyType DepType) {
253 std::optional<MemoryLocation> DstLocOpt =
259 "Expected a mem instr");
266 case DependencyType::ReadAfterWrite:
267 case DependencyType::WriteAfterWrite:
269 case DependencyType::WriteAfterRead:
277 DependencyType RoughDepType = getRoughDepType(SrcI, DstI);
278 switch (RoughDepType) {
279 case DependencyType::ReadAfterWrite:
280 case DependencyType::WriteAfterWrite:
281 case DependencyType::WriteAfterRead:
282 return alias(SrcI, DstI, RoughDepType);
283 case DependencyType::Control:
289 case DependencyType::Other:
291 case DependencyType::None:
297void DependencyGraph::scanAndAddDeps(
MemDGNode &DstN,
300 "DstN is the mem dep destination, so it must be mem");
306 if (hasDep(SrcI, DstI))
307 DstN.addMemPred(&SrcN);
311void DependencyGraph::setDefUseUnscheduledSuccs(
321 unsigned CntUnschedPreds = 0;
327 if (OpI->getParent() !=
I.getParent())
329 if (!NewInterval.contains(OpI))
334 OpN->incrUnscheduledSuccs();
335 if (!OpN->scheduled())
342 bool NewIsAbove = DAGInterval.empty() || NewInterval.comesBefore(DAGInterval);
343 const auto &TopInterval = NewIsAbove ? NewInterval : DAGInterval;
344 const auto &BotInterval = NewIsAbove ? DAGInterval : NewInterval;
358 if (BotN->scheduled())
360 unsigned CntUnscheduledPreds = 0;
361 for (
Value *
Op : BotI.operands()) {
368 if (!TopInterval.contains(OpI))
370 if (!OpN->scheduled()) {
371 OpN->incrUnscheduledSuccs();
372 ++CntUnscheduledPreds;
375 *BotN->UnscheduledPreds += CntUnscheduledPreds;
387 MemN->setPrevNode(LastMemN);
392 if (!DAGInterval.empty()) {
393 bool NewIsAbove = NewInterval.comesBefore(DAGInterval);
394 const auto &TopInterval = NewIsAbove ? NewInterval : DAGInterval;
395 const auto &BotInterval = NewIsAbove ? DAGInterval : NewInterval;
400 assert((LinkTopN ==
nullptr || LinkBotN ==
nullptr ||
401 LinkTopN->comesBefore(LinkBotN)) &&
403 if (LinkTopN !=
nullptr && LinkBotN !=
nullptr) {
404 LinkTopN->setNextNode(LinkBotN);
409 auto UnionIntvl = DAGInterval.getUnionInterval(NewInterval);
414 if (ChainTopN !=
nullptr && ChainBotN !=
nullptr) {
415 for (
auto *
N = ChainTopN->getNextNode(), *LastN = ChainTopN;
N !=
nullptr;
416 LastN =
N,
N =
N->getNextNode()) {
417 assert(
N == LastN->getNextNode() &&
"Bad chain!");
418 assert(
N->getPrevNode() == LastN &&
"Bad chain!");
424 setDefUseUnscheduledSuccs(NewInterval);
430 for (
auto *PrevI = IncludingN ?
I :
I->getPrevNode(); PrevI !=
nullptr;
431 PrevI = PrevI->getPrevNode()) {
433 if (PrevN ==
nullptr)
436 if (PrevMemN !=
nullptr && PrevMemN != SkipN)
445 for (
auto *NextI = IncludingN ?
I :
I->getNextNode(); NextI !=
nullptr;
446 NextI = NextI->getNextNode()) {
448 if (NextN ==
nullptr)
451 if (NextMemN !=
nullptr && NextMemN != SkipN)
457void DependencyGraph::notifyCreateInstr(
Instruction *
I) {
462 if (!(DAGInterval.contains(
I) || DAGInterval.touches(
I)))
465 DAGInterval = DAGInterval.getUnionInterval({
I,
I});
470 if (MemN !=
nullptr) {
471 if (
auto *PrevMemN = getMemDGNodeBefore(MemN,
false)) {
472 PrevMemN->NextMemN = MemN;
473 MemN->PrevMemN = PrevMemN;
475 if (
auto *NextMemN = getMemDGNodeAfter(MemN,
false)) {
476 NextMemN->PrevMemN = MemN;
477 MemN->NextMemN = NextMemN;
482 if (DAGInterval.top()->comesBefore(
I)) {
485 scanAndAddDeps(*MemN, SrcInterval);
488 if (
I->comesBefore(DAGInterval.bottom())) {
497void DependencyGraph::notifyMoveInstr(
Instruction *
I,
const BBIterator &To) {
503 assert(!(To != BB->end() && &*To ==
I->getNextNode()) &&
504 !(To == BB->end() && std::next(
I->getIterator()) == BB->end()) &&
505 "Should not have been called if destination is same as origin.");
509 assert(To.getNodeParent() ==
I->getParent() &&
510 "TODO: We don't support movement across BBs!");
512 (To == std::next(DAGInterval.bottom()->getIterator()) ||
513 (To != BB->end() && std::next(To) == DAGInterval.top()->getIterator()) ||
514 (To != BB->end() && DAGInterval.contains(&*To))) &&
515 "TODO: To should be either within the DAGInterval or right "
519 auto OrigDAGInterval = DAGInterval;
522 DAGInterval.notifyMoveInstr(
I, To);
535 MemN->detachFromChain();
550 if (To == BB->end() ||
551 To == std::next(OrigDAGInterval.bottom()->getIterator())) {
556 getMemDGNodeBefore(InsertAfterN,
true, MemN));
561 getMemDGNodeBefore(BeforeToN,
false, MemN));
563 getMemDGNodeAfter(BeforeToN,
true, MemN));
577 auto *PrevMemN = getMemDGNodeBefore(MemN,
false);
578 auto *NextMemN = getMemDGNodeAfter(MemN,
false);
579 if (PrevMemN !=
nullptr)
580 PrevMemN->NextMemN = NextMemN;
581 if (NextMemN !=
nullptr)
582 NextMemN->PrevMemN = PrevMemN;
585 while (!MemN->memPreds().empty()) {
586 auto *PredN = *MemN->memPreds().begin();
587 MemN->removeMemPred(PredN);
589 while (!MemN->memSuccs().empty()) {
590 auto *SuccN = *MemN->memSuccs().begin();
591 SuccN->removeMemPred(MemN);
596 if (!
N->scheduled()) {
597 for (
auto *PredN :
N->preds(*
this))
598 if (!PredN->scheduled())
599 PredN->decrUnscheduledSuccs();
600 for (
auto *SuccN :
N->succs(*
this))
602 SuccN->decrUnscheduledPreds();
606 InstrToNodeMap.erase(
I);
609void DependencyGraph::notifySetUse(
const Use &U,
Value *NewSrc) {
618 if (UserI ==
nullptr)
621 if (UserN ==
nullptr)
625 if (UserN->scheduled())
630 if (
auto *CurrSrcN =
getNode(CurrSrcI)) {
632 if (!CurrSrcN->scheduled()) {
634 CurrSrcN->decrUnscheduledSuccs();
636 UserN->decrUnscheduledPreds();
641 if (
auto *NewSrcN =
getNode(NewSrcI)) {
643 if (!NewSrcN->scheduled()) {
645 NewSrcN->incrUnscheduledSuccs();
647 UserN->incrUnscheduledPreds();
659 auto NewInterval = Union.getSingleDiff(DAGInterval);
660 if (NewInterval.empty())
663 createNewNodes(NewInterval);
679 if (!DstRange.empty()) {
682 scanAndAddDeps(DstN, SrcRange);
687 if (MemDAGInterval.empty()) {
688 FullScan(NewInterval);
705 else if (DAGInterval.bottom()->comesBefore(NewInterval.top())) {
707 auto SrcRangeFull = MemDAGInterval.getUnionInterval(DstRange);
711 scanAndAddDeps(DstN, SrcRange);
715 else if (NewInterval.bottom()->comesBefore(DAGInterval.top())) {
729 FullScan(NewInterval);
745 auto DstRangeOld = MemDAGInterval;
748 scanAndAddDeps(DstN, SrcRange);
761 Nodes.
reserve(InstrToNodeMap.size());
762 for (
const auto &Pair : InstrToNodeMap)
768 for (
auto *
N : Nodes)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Represent a constant reference to an array (0 or more elements consecutively in memory),...
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.
A DependencyGraph Node that points to an Instruction and contains memory dependency edges.
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...
void setSchedBundle(SchedBundle &SB)
SchedBundle * SB
The scheduler bundle that this node belongs to.
bool Scheduled
This is true if this node has been scheduled.
std::optional< unsigned > UnscheduledSuccs
The number of unscheduled successors.
static bool isMemDepNodeCandidate(Instruction *I)
\Returns true if I is a memory dependency candidate instruction.
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)
std::optional< unsigned > UnscheduledPreds
LLVM_DUMP_METHOD void dump() const
DGNode * getNode(Instruction *I) const
DGNode * getNodeOrNull(Instruction *I) const
Like getNode() but returns nullptr if I is nullptr.
void print(raw_ostream &OS) const
DGNode * getOrCreateNode(Instruction *I)
LLVM_ABI Interval< Instruction > extend(ArrayRef< Instruction * > Instrs)
Build/extend the dependency graph such that it includes Instrs.
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)