38#ifndef LLVM_ADT_GENERICUNIFORMITYIMPL_H
39#define LLVM_ADT_GENERICUNIFORMITYIMPL_H
50#define DEBUG_TYPE "uniformity"
56template <
typename Range>
auto unique(Range &&R) {
88 using BlockT =
typename ContextT::BlockT;
108 POIndex[&BB] = m_order.
size();
111 <<
"): " << Context.print(&BB) <<
"\n");
113 ReducibleCycleHeaders.
insert(&BB);
118 return POIndex.
lookup(BB);
122 return ReducibleCycleHeaders.
contains(BB);
129 const ContextT &Context;
261 using BlockT =
typename ContextT::BlockT;
318 CachedControlDivDescs;
329 using BlockT =
typename ContextT::BlockT;
333 using UseT =
typename ContextT::UseT;
342 typename SyncDependenceAnalysisT::DivergenceDescriptor;
383 if (
I.isTerminator()) {
446 void taintAndPushAllDefs(
const BlockT &JoinBlock);
450 void taintAndPushPhiNodes(
const BlockT &JoinBlock);
455 void propagateCycleExitDivergence(
const BlockT &DivExit,
459 void analyzeCycleExitDivergence(
const CycleT &DefCycle);
472 bool isTemporalDivergent(
const BlockT &ObservingBlock,
476template <
typename ImplT>
484 using BlockT =
typename ContextT::BlockT;
495 typename SyncDependenceAnalysisT::DivergenceDescriptor;
511 std::unique_ptr<DivergenceDescriptorT>
DivDesc;
521 Out <<
"Propagator::BlockLabels {\n";
522 for (
int BlockIdx = (
int)
CyclePOT.size() - 1; BlockIdx >= 0; --BlockIdx) {
525 Out <<
Context.print(
Block) <<
"(" << BlockIdx <<
") : ";
529 Out <<
Context.print(Label) <<
"\n";
541 <<
"\tpushed label: " <<
Context.print(&PushedLabel)
543 <<
"\told label: " <<
Context.print(OldLabel) <<
"\n");
546 if (OldLabel == &PushedLabel)
549 if (OldLabel != &SuccBlock) {
550 auto SuccIdx =
CyclePOT.getIndex(&SuccBlock);
579 DivDesc->CycleDivBlocks.insert(&ExitBlock);
591 DivDesc->JoinDivBlocks.insert(&SuccBlock);
605 const BlockT *FloorLabel =
nullptr;
611 if (DivTermCycle && !DivTermCycle->contains(SuccBlock)) {
615 DivDesc->CycleDivBlocks.insert(SuccBlock);
617 <<
Context.print(SuccBlock) <<
"\n");
619 auto SuccIdx =
CyclePOT.getIndex(SuccBlock);
621 FloorIdx = std::min<int>(FloorIdx, SuccIdx);
626 if (BlockIdx == -1 || BlockIdx < FloorIdx)
632 if (BlockIdx == DivTermIdx) {
639 << BlockIdx <<
"\n");
644 bool CausedJoin =
false;
645 int LoweredFloorIdx = FloorIdx;
671 if (
const auto *BlockCycle = getReducibleParent(
Block)) {
673 BlockCycle->getExitBlocks(BlockCycleExits);
674 for (
auto *BlockCycleExit : BlockCycleExits) {
677 std::min<int>(LoweredFloorIdx,
CyclePOT.getIndex(BlockCycleExit));
681 CausedJoin |=
visitEdge(*SuccBlock, *Label);
683 std::min<int>(LoweredFloorIdx,
CyclePOT.getIndex(SuccBlock));
690 FloorIdx = LoweredFloorIdx;
691 }
else if (FloorLabel != Label) {
694 FloorIdx = LoweredFloorIdx;
715 for (
const auto *Exit : Exits) {
718 DivDesc->CycleDivBlocks.insert(Exit);
729template <
typename ContextT>
733template <
typename ContextT>
736 : CyclePO(
Context), DT(DT), CI(CI) {
740template <
typename ContextT>
745 return EmptyDivergenceDesc;
749 auto ItCached = CachedControlDivDescs.find(DivTermBlock);
750 if (ItCached != CachedControlDivDescs.end())
751 return *ItCached->second;
761 for (
const auto *BB :
Blocks) {
762 Out << LS << CI.getSSAContext().
print(BB);
769 dbgs() <<
"\nResult (" << CI.getSSAContext().print(DivTermBlock)
770 <<
"):\n JoinDivBlocks: " << printBlockSet(DivDesc->JoinDivBlocks)
771 <<
" CycleDivBlocks: " << printBlockSet(DivDesc->CycleDivBlocks)
776 CachedControlDivDescs.try_emplace(DivTermBlock, std::move(DivDesc));
777 assert(ItInserted.second);
778 return *ItInserted.first->second;
781template <
typename ContextT>
784 if (isAlwaysUniform(
I))
787 if (
I.isTerminator()) {
788 Marked = DivergentTermBlocks.insert(
I.getParent()).second;
791 <<
Context.print(
I.getParent()) <<
"\n");
794 Marked = markDefsDivergent(
I);
798 Worklist.push_back(&
I);
801template <
typename ContextT>
804 if (DivergentValues.insert(Val).second) {
811template <
typename ContextT>
814 UniformOverrides.insert(&Instr);
830template <
typename ContextT>
832 const CycleT &DefCycle) {
834 DefCycle.getExitBlocks(Exits);
835 for (
auto *Exit : Exits) {
836 for (
auto &Phi : Exit->phis()) {
837 if (usesValueFromCycle(Phi, DefCycle)) {
843 for (
auto *BB : DefCycle.blocks()) {
845 [&](BlockT *Exit) {
return DT.dominates(BB, Exit); }))
847 for (
auto &II : *BB) {
848 propagateTemporalDivergence(II, DefCycle);
853template <
typename ContextT>
855 const BlockT &DivExit,
const CycleT &InnerDivCycle) {
858 auto *DivCycle = &InnerDivCycle;
859 auto *OuterDivCycle = DivCycle;
860 auto *ExitLevelCycle = CI.getCycle(&DivExit);
861 const unsigned CycleExitDepth =
862 ExitLevelCycle ? ExitLevelCycle->getDepth() : 0;
865 while (DivCycle && DivCycle->getDepth() > CycleExitDepth) {
867 <<
Context.print(DivCycle->getHeader()) <<
"\n");
868 OuterDivCycle = DivCycle;
869 DivCycle = DivCycle->getParentCycle();
872 <<
Context.print(OuterDivCycle->getHeader()) <<
"\n");
874 if (!DivergentExitCycles.insert(OuterDivCycle).second)
879 for (
const auto *
C : AssumedDivergent) {
880 if (
C->contains(OuterDivCycle))
884 analyzeCycleExitDivergence(*OuterDivCycle);
887template <
typename ContextT>
891 for (
const auto &
I :
instrs(BB)) {
895 if (
I.isTerminator())
903template <
typename ContextT>
905 const BlockT &JoinBlock) {
908 for (
const auto &Phi : JoinBlock.phis()) {
916 if (ContextT::isConstantOrUndefValuePhi(Phi))
925template <
typename CycleT>
929 [Candidate](CycleT *
C) {
return C->contains(Candidate); }))
940template <
typename CycleT,
typename BlockT>
942 const BlockT *DivTermBlock,
943 const BlockT *JoinBlock) {
956 while (Parent && !Parent->contains(DivTermBlock)) {
960 assert(!Parent->isReducible());
965 LLVM_DEBUG(
dbgs() <<
"cycle made divergent by external branch\n");
973template <
typename ContextT,
typename CycleT,
typename BlockT,
974 typename DominatorTreeT>
977 const BlockT *JoinBlock,
const DominatorTreeT &DT,
980 <<
"for internal branch " <<
Context.print(DivTermBlock)
982 if (DT.properlyDominates(DivTermBlock, JoinBlock))
997 <<
" does not dominate join\n");
1000 while (Parent && !DT.properlyDominates(Parent->getHeader(), JoinBlock)) {
1002 <<
" does not dominate join\n");
1007 LLVM_DEBUG(
dbgs() <<
" cycle made divergent by internal branch\n");
1011template <
typename ContextT,
typename CycleT,
typename BlockT,
1012 typename DominatorTreeT>
1013static const CycleT *
1015 const BlockT *JoinBlock,
const DominatorTreeT &DT,
1016 ContextT &Context) {
1032template <
typename ContextT>
1034 const BlockT &ObservingBlock,
const InstructionT &Def)
const {
1035 const BlockT *DefBlock = Def.getParent();
1036 for (
const CycleT *
Cycle = CI.getCycle(DefBlock);
1039 if (DivergentExitCycles.contains(
Cycle)) {
1046template <
typename ContextT>
1049 const auto *DivTermBlock = Term.getParent();
1050 DivergentTermBlocks.insert(DivTermBlock);
1055 if (!DT.isReachableFromEntry(DivTermBlock))
1058 const auto &DivDesc = SDA.getJoinBlocks(DivTermBlock);
1062 for (
const auto *JoinBlock : DivDesc.JoinDivBlocks) {
1063 const auto *
Cycle = CI.getCycle(JoinBlock);
1072 taintAndPushPhiNodes(*JoinBlock);
1078 return A->getDepth() >
B->getDepth();
1086 for (
auto *
C : DivCycles) {
1090 for (
const BlockT *BB :
C->blocks()) {
1091 taintAndPushAllDefs(*BB);
1095 const auto *BranchCycle = CI.getCycle(DivTermBlock);
1096 assert(DivDesc.CycleDivBlocks.empty() || BranchCycle);
1097 for (
const auto *DivExitBlock : DivDesc.CycleDivBlocks) {
1098 propagateCycleExitDivergence(*DivExitBlock, *BranchCycle);
1102template <
typename ContextT>
1105 auto DivValuesCopy = DivergentValues;
1106 for (
const auto DivVal : DivValuesCopy) {
1107 assert(isDivergent(DivVal) &&
"Worklist invariant violated!");
1113 while (!Worklist.empty()) {
1115 Worklist.pop_back();
1119 if (
I->isTerminator()) {
1120 analyzeControlDivergence(*
I);
1125 assert(isDivergent(*
I) &&
"Worklist invariant violated!");
1130template <
typename ContextT>
1133 return UniformOverrides.contains(&Instr);
1136template <
typename ContextT>
1141 DA.reset(
new ImplT{Func, DT, CI,
TTI});
1146template <
typename ContextT>
1148 bool haveDivergentArgs =
false;
1153 if (DivergentValues.empty() && DivergentTermBlocks.empty() &&
1154 DivergentExitCycles.empty()) {
1155 OS <<
"ALL VALUES UNIFORM\n";
1159 for (
const auto &
entry : DivergentValues) {
1162 if (!haveDivergentArgs) {
1163 OS <<
"DIVERGENT ARGUMENTS:\n";
1164 haveDivergentArgs =
true;
1170 if (!AssumedDivergent.empty()) {
1171 OS <<
"CYCLES ASSSUMED DIVERGENT:\n";
1172 for (
const CycleT *cycle : AssumedDivergent) {
1173 OS <<
" " << cycle->print(
Context) <<
'\n';
1177 if (!DivergentExitCycles.empty()) {
1178 OS <<
"CYCLES WITH DIVERGENT EXIT:\n";
1179 for (
const CycleT *cycle : DivergentExitCycles) {
1180 OS <<
" " << cycle->print(
Context) <<
'\n';
1187 OS <<
"DEFINITIONS\n";
1190 for (
auto value : defs) {
1191 if (isDivergent(
value))
1192 OS <<
" DIVERGENT: ";
1198 OS <<
"TERMINATORS\n";
1201 bool divergentTerminators = hasDivergentTerminator(
block);
1202 for (
auto *
T : terms) {
1203 if (divergentTerminators)
1204 OS <<
" DIVERGENT: ";
1210 OS <<
"END BLOCK\n";
1214template <
typename ContextT>
1216 return DA->hasDivergence();
1220template <
typename ContextT>
1222 return DA->isDivergent(V);
1225template <
typename ContextT>
1227 return DA->isDivergent(*
I);
1230template <
typename ContextT>
1232 return DA->isDivergentUse(U);
1235template <
typename ContextT>
1237 return DA->hasDivergentTerminator(
B);
1241template <
typename ContextT>
1246template <
typename ContextT>
1251 while (!Stack.empty()) {
1252 auto *NextBB = Stack.back();
1253 if (Finalized.
count(NextBB)) {
1257 LLVM_DEBUG(
dbgs() <<
" visiting " << CI.getSSAContext().print(NextBB)
1259 auto *NestedCycle = CI.getCycle(NextBB);
1262 while (NestedCycle->getParentCycle() !=
Cycle)
1263 NestedCycle = NestedCycle->getParentCycle();
1266 NestedCycle->getExitBlocks(NestedExits);
1267 bool PushedNodes =
false;
1268 for (
auto *NestedExitBB : NestedExits) {
1270 << CI.getSSAContext().print(NestedExitBB) <<
"\n");
1273 if (Finalized.
count(NestedExitBB))
1276 Stack.push_back(NestedExitBB);
1278 << CI.getSSAContext().print(NestedExitBB) <<
"\n");
1283 computeCyclePO(CI, NestedCycle, Finalized);
1290 bool PushedNodes =
false;
1293 << CI.getSSAContext().print(SuccBB) <<
"\n");
1296 if (Finalized.
count(SuccBB))
1299 Stack.push_back(SuccBB);
1300 LLVM_DEBUG(
dbgs() <<
" pushed succ: " << CI.getSSAContext().print(SuccBB)
1306 << CI.getSSAContext().print(NextBB) <<
"\n");
1308 Finalized.
insert(NextBB);
1309 appendBlock(*NextBB);
1315template <
typename ContextT>
1317 const CycleInfoT &CI,
const CycleT *
Cycle,
1324 << CI.getSSAContext().print(CycleHeader) <<
"\n");
1326 Finalized.
insert(CycleHeader);
1330 << CI.getSSAContext().print(CycleHeader) <<
"\n");
1335 LLVM_DEBUG(
dbgs() <<
" examine succ: " << CI.getSSAContext().print(BB)
1339 if (BB == CycleHeader)
1341 if (!Finalized.
count(BB)) {
1342 LLVM_DEBUG(
dbgs() <<
" pushed succ: " << CI.getSSAContext().print(BB)
1344 Stack.push_back(BB);
1349 computeStackPO(Stack, CI,
Cycle, Finalized);
1355template <
typename ContextT>
1362 computeStackPO(Stack, CI,
nullptr, Finalized);
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Given that RA is a live value
DenseMap< Block *, BlockRelaxAux > Blocks
print Instructions which execute on loop entry
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallPtrSet class.
This file defines the SparseBitVector class.
unify loop Fixup each natural loop to have a single exit block
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Compute divergence starting with a divergent branch.
typename SyncDependenceAnalysisT::BlockLabelMap BlockLabelMapT
const ModifiedPO & CyclePOT
GenericSyncDependenceAnalysis< ContextT > SyncDependenceAnalysisT
typename ContextT::DominatorTreeT DominatorTreeT
bool computeJoin(const BlockT &SuccBlock, const BlockT &PushedLabel)
const BlockT & DivTermBlock
std::unique_ptr< DivergenceDescriptorT > DivDesc
void printDefs(raw_ostream &Out)
typename ContextT::FunctionT FunctionT
const DominatorTreeT & DT
ModifiedPostOrder< ContextT > ModifiedPO
std::unique_ptr< DivergenceDescriptorT > computeJoinPoints()
BlockLabelMapT & BlockLabels
SparseBitVector FreshLabels
bool visitCycleExitEdge(const BlockT &ExitBlock, const BlockT &Label)
typename ContextT::ValueRefT ValueRefT
typename ContextT::BlockT BlockT
typename SyncDependenceAnalysisT::DivergenceDescriptor DivergenceDescriptorT
typename CycleInfoT::CycleT CycleT
DivergencePropagator(const ModifiedPO &CyclePOT, const DominatorTreeT &DT, const CycleInfoT &CI, const BlockT &DivTermBlock)
bool visitEdge(const BlockT &SuccBlock, const BlockT &Label)
Cycle information for a function.
FunctionT * getFunction() const
GenericCycle< ContextT > CycleT
CycleT * getCycle(const BlockT *Block) const
Find the innermost cycle containing a given block.
A possibly irreducible generalization of a Loop.
BlockT * getHeader() const
bool isReducible() const
Whether the cycle is a natural loop.
void getExitBlocks(SmallVectorImpl< BlockT * > &TmpStorage) const
Return all of the successor blocks of this cycle.
bool contains(const BlockT *Block) const
Return whether Block is contained in the cycle.
const GenericCycle * getParentCycle() const
Locate join blocks for disjoint paths starting at a divergent branch.
GenericSyncDependenceAnalysis(const ContextT &Context, const DominatorTreeT &DT, const CycleInfoT &CI)
ModifiedPostOrder< ContextT > ModifiedPO
typename ContextT::DominatorTreeT DominatorTreeT
typename ContextT::FunctionT FunctionT
typename ContextT::InstructionT InstructionT
typename ContextT::BlockT BlockT
typename ContextT::ValueRefT ValueRefT
typename CycleInfoT::CycleT CycleT
const DivergenceDescriptor & getJoinBlocks(const BlockT *DivTermBlock)
Computes divergent join points and cycle exits caused by branch divergence in Term.
Construct a specially modified post-order traversal of cycles.
typename ContextT::FunctionT FunctionT
const BlockT * operator[](size_t idx) const
typename CycleInfoT::CycleT CycleT
bool isReducibleCycleHeader(const BlockT *BB) const
ModifiedPostOrder(const ContextT &C)
unsigned count(BlockT *BB) const
void compute(const CycleInfoT &CI)
Generically compute the modified post order.
GenericCycleInfo< ContextT > CycleInfoT
void appendBlock(const BlockT &BB, bool isReducibleCycleHeader=false)
unsigned getIndex(const BlockT *BB) const
typename std::vector< BlockT * >::const_iterator const_iterator
typename ContextT::DominatorTreeT DominatorTreeT
typename ContextT::BlockT BlockT
Simple wrapper around std::function<void(raw_ostream&)>.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
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.
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 push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class implements an extremely fast bulk output stream that can only output to a stream.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
static const CycleT * getIntDivCycle(const CycleT *Cycle, const BlockT *DivTermBlock, const BlockT *JoinBlock, const DominatorTreeT &DT, ContextT &Context)
Return the outermost cycle made divergent by branch inside it.
static const CycleT * getExtDivCycle(const CycleT *Cycle, const BlockT *DivTermBlock, const BlockT *JoinBlock)
Return the outermost cycle made divergent by branch outside it.
constexpr auto adl_begin(RangeT &&range) -> decltype(adl_detail::begin_impl(std::forward< RangeT >(range)))
Returns the begin iterator to range using std::begin and function found through Argument-Dependent Lo...
auto successors(const MachineBasicBlock *BB)
static bool insertIfNotContained(SmallVector< CycleT * > &Cycles, CycleT *Candidate)
Add Candidate to Cycles if it is not already contained in Cycles.
constexpr auto adl_end(RangeT &&range) -> decltype(adl_detail::end_impl(std::forward< RangeT >(range)))
Returns the end iterator to range using std::end and functions found through Argument-Dependent Looku...
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
void sort(IteratorTy Start, IteratorTy End)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
auto instrs(const MachineBasicBlock &BB)
unsigned succ_size(const MachineBasicBlock *BB)
static const CycleT * getOutermostDivergentCycle(const CycleT *Cycle, const BlockT *DivTermBlock, const BlockT *JoinBlock, const DominatorTreeT &DT, ContextT &Context)
Information discovered by the sync dependence analysis for each divergent branch.
ConstBlockSet CycleDivBlocks
ConstBlockSet JoinDivBlocks
BlockLabelMap BlockLabels