44#ifndef LLVM_ADT_GENERICUNIFORMITYIMPL_H
45#define LLVM_ADT_GENERICUNIFORMITYIMPL_H
57#define DEBUG_TYPE "uniformity"
89 using BlockT =
typename ContextT::BlockT;
94 using CycleT =
typename CycleInfoT::CycleT;
109 POIndex[&BB] = m_order.
size();
112 <<
"): " << Context.print(&BB) <<
"\n");
114 ReducibleCycleHeaders.
insert(&BB);
119 return POIndex.
lookup(BB);
123 return ReducibleCycleHeaders.
contains(BB);
130 const ContextT &Context;
140template <
typename>
class DivergencePropagator;
262 using BlockT =
typename ContextT::BlockT;
269 using CycleT =
typename CycleInfoT::CycleT;
319 CachedControlDivDescs;
330 using BlockT =
typename ContextT::BlockT;
334 using UseT =
typename ContextT::UseT;
339 using CycleT =
typename CycleInfoT::CycleT;
343 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;
490 using CycleT =
typename CycleInfoT::CycleT;
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;
665 const auto *BlockCycle =
CI.getCycle(
Block);
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>
854void GenericUniformityAnalysisImpl<ContextT>::propagateCycleExitDivergence(
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>
888void GenericUniformityAnalysisImpl<ContextT>::taintAndPushAllDefs(
891 for (
const auto &
I :
instrs(BB)) {
895 if (
I.isTerminator())
903template <
typename ContextT>
904void GenericUniformityAnalysisImpl<ContextT>::taintAndPushPhiNodes(
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) {
950 const auto *OriginalCycle =
Cycle;
952 while (Parent && !Parent->contains(DivTermBlock)) {
968 LLVM_DEBUG(
dbgs() <<
"cycle made divergent by external branch\n");
976template <
typename ContextT,
typename CycleT,
typename BlockT,
977 typename DominatorTreeT>
980 const BlockT *JoinBlock,
const DominatorTreeT &DT,
983 <<
" for internal branch " <<
Context.print(DivTermBlock)
985 if (DT.properlyDominates(DivTermBlock, JoinBlock))
1000 <<
" does not dominate join\n");
1003 while (Parent && !DT.properlyDominates(Parent->getHeader(), JoinBlock)) {
1005 <<
" does not dominate join\n");
1010 LLVM_DEBUG(
dbgs() <<
" cycle made divergent by internal branch\n");
1014template <
typename ContextT,
typename CycleT,
typename BlockT,
1015 typename DominatorTreeT>
1016static const CycleT *
1018 const BlockT *JoinBlock,
const DominatorTreeT &DT,
1019 ContextT &Context) {
1035template <
typename ContextT>
1036bool GenericUniformityAnalysisImpl<ContextT>::isTemporalDivergent(
1037 const BlockT &ObservingBlock,
const InstructionT &Def)
const {
1038 const BlockT *DefBlock = Def.getParent();
1039 for (
const CycleT *
Cycle = CI.getCycle(DefBlock);
1042 if (DivergentExitCycles.contains(
Cycle)) {
1049template <
typename ContextT>
1052 const auto *DivTermBlock = Term.getParent();
1053 DivergentTermBlocks.insert(DivTermBlock);
1058 if (!DT.isReachableFromEntry(DivTermBlock))
1061 const auto &DivDesc = SDA.getJoinBlocks(DivTermBlock);
1065 for (
const auto *JoinBlock : DivDesc.JoinDivBlocks) {
1066 const auto *
Cycle = CI.getCycle(JoinBlock);
1075 taintAndPushPhiNodes(*JoinBlock);
1081 return A->getDepth() >
B->getDepth();
1089 for (
auto *
C : DivCycles) {
1093 for (
const BlockT *BB :
C->blocks()) {
1094 taintAndPushAllDefs(*BB);
1098 const auto *BranchCycle = CI.getCycle(DivTermBlock);
1099 assert(DivDesc.CycleDivBlocks.empty() || BranchCycle);
1100 for (
const auto *DivExitBlock : DivDesc.CycleDivBlocks) {
1101 propagateCycleExitDivergence(*DivExitBlock, *BranchCycle);
1105template <
typename ContextT>
1108 auto DivValuesCopy = DivergentValues;
1109 for (
const auto DivVal : DivValuesCopy) {
1110 assert(isDivergent(DivVal) &&
"Worklist invariant violated!");
1116 while (!Worklist.empty()) {
1118 Worklist.pop_back();
1122 if (
I->isTerminator()) {
1123 analyzeControlDivergence(*
I);
1128 assert(isDivergent(*
I) &&
"Worklist invariant violated!");
1133template <
typename ContextT>
1136 return UniformOverrides.contains(&Instr);
1139template <
typename ContextT>
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;
1166 OS <<
" DIVERGENT: " <<
Context.print(entry) <<
'\n';
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();
1219template <
typename ContextT>
1220const typename ContextT::FunctionT &
1222 return DA->getFunction();
1226template <
typename ContextT>
1228 return DA->isDivergent(V);
1231template <
typename ContextT>
1233 return DA->isDivergent(*
I);
1236template <
typename ContextT>
1238 return DA->isDivergentUse(U);
1241template <
typename ContextT>
1243 return DA->hasDivergentTerminator(
B);
1247template <
typename ContextT>
1252template <
typename ContextT>
1257 while (!Stack.empty()) {
1258 auto *NextBB = Stack.back();
1259 if (Finalized.
count(NextBB)) {
1263 LLVM_DEBUG(
dbgs() <<
" visiting " << CI.getSSAContext().print(NextBB)
1265 auto *NestedCycle = CI.getCycle(NextBB);
1268 while (NestedCycle->getParentCycle() !=
Cycle)
1269 NestedCycle = NestedCycle->getParentCycle();
1271 SmallVector<BlockT *, 3> NestedExits;
1272 NestedCycle->getExitBlocks(NestedExits);
1273 bool PushedNodes =
false;
1274 for (
auto *NestedExitBB : NestedExits) {
1276 << CI.getSSAContext().print(NestedExitBB) <<
"\n");
1279 if (Finalized.
count(NestedExitBB))
1282 Stack.push_back(NestedExitBB);
1284 << CI.getSSAContext().print(NestedExitBB) <<
"\n");
1289 computeCyclePO(CI, NestedCycle, Finalized);
1296 bool PushedNodes =
false;
1299 << CI.getSSAContext().print(SuccBB) <<
"\n");
1302 if (Finalized.
count(SuccBB))
1305 Stack.push_back(SuccBB);
1306 LLVM_DEBUG(
dbgs() <<
" pushed succ: " << CI.getSSAContext().print(SuccBB)
1312 << CI.getSSAContext().print(NextBB) <<
"\n");
1314 Finalized.
insert(NextBB);
1315 appendBlock(*NextBB);
1321template <
typename ContextT>
1322void ModifiedPostOrder<ContextT>::computeCyclePO(
1323 const CycleInfoT &CI,
const CycleT *
Cycle,
1324 SmallPtrSetImpl<const BlockT *> &Finalized) {
1326 SmallVector<const BlockT *>
Stack;
1330 << CI.getSSAContext().print(CycleHeader) <<
"\n");
1331 assert(!Finalized.count(CycleHeader));
1332 Finalized.insert(CycleHeader);
1336 << CI.getSSAContext().print(CycleHeader) <<
"\n");
1341 LLVM_DEBUG(
dbgs() <<
" examine succ: " << CI.getSSAContext().print(BB)
1345 if (BB == CycleHeader)
1347 if (!Finalized.count(BB)) {
1348 LLVM_DEBUG(
dbgs() <<
" pushed succ: " << CI.getSSAContext().print(BB)
1350 Stack.push_back(BB);
1355 computeStackPO(Stack, CI,
Cycle, Finalized);
1361template <
typename ContextT>
1365 auto *
F = CI.getFunction();
1367 Stack.push_back(&
F->front());
1368 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
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
GenericCycleInfo< ContextT > CycleInfoT
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.
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
GenericCycleInfo< ContextT > CycleInfoT
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.
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.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
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