38#ifndef LLVM_ADT_GENERICUNIFORMITYIMPL_H
39#define LLVM_ADT_GENERICUNIFORMITYIMPL_H
50#define DEBUG_TYPE "uniformity"
54template <
typename Range>
auto unique(Range &&R) {
86 using BlockT =
typename ContextT::BlockT;
91 using CycleT =
typename CycleInfoT::CycleT;
106 POIndex[&BB] = m_order.
size();
109 <<
"): " << Context.print(&BB) <<
"\n");
111 ReducibleCycleHeaders.
insert(&BB);
116 return POIndex.
lookup(BB);
120 return ReducibleCycleHeaders.
contains(BB);
127 const ContextT &Context;
137template <
typename>
class DivergencePropagator;
259 using BlockT =
typename ContextT::BlockT;
266 using CycleT =
typename CycleInfoT::CycleT;
316 CachedControlDivDescs;
327 using BlockT =
typename ContextT::BlockT;
331 using UseT =
typename ContextT::UseT;
336 using CycleT =
typename CycleInfoT::CycleT;
340 typename SyncDependenceAnalysisT::DivergenceDescriptor;
380 if (
I.isTerminator()) {
443 void taintAndPushAllDefs(
const BlockT &JoinBlock);
447 void taintAndPushPhiNodes(
const BlockT &JoinBlock);
452 void propagateCycleExitDivergence(
const BlockT &DivExit,
456 void analyzeCycleExitDivergence(
const CycleT &DefCycle);
469 bool isTemporalDivergent(
const BlockT &ObservingBlock,
473template <
typename ImplT>
481 using BlockT =
typename ContextT::BlockT;
487 using CycleT =
typename CycleInfoT::CycleT;
492 typename SyncDependenceAnalysisT::DivergenceDescriptor;
508 std::unique_ptr<DivergenceDescriptorT>
DivDesc;
518 Out <<
"Propagator::BlockLabels {\n";
519 for (
int BlockIdx = (
int)
CyclePOT.size() - 1; BlockIdx >= 0; --BlockIdx) {
522 Out <<
Context.print(
Block) <<
"(" << BlockIdx <<
") : ";
526 Out <<
Context.print(Label) <<
"\n";
538 <<
"\tpushed label: " <<
Context.print(&PushedLabel)
540 <<
"\told label: " <<
Context.print(OldLabel) <<
"\n");
543 if (OldLabel == &PushedLabel)
546 if (OldLabel != &SuccBlock) {
547 auto SuccIdx =
CyclePOT.getIndex(&SuccBlock);
576 DivDesc->CycleDivBlocks.insert(&ExitBlock);
588 DivDesc->JoinDivBlocks.insert(&SuccBlock);
602 const BlockT *FloorLabel =
nullptr;
608 if (DivTermCycle && !DivTermCycle->contains(SuccBlock)) {
612 DivDesc->CycleDivBlocks.insert(SuccBlock);
614 <<
Context.print(SuccBlock) <<
"\n");
616 auto SuccIdx =
CyclePOT.getIndex(SuccBlock);
618 FloorIdx = std::min<int>(FloorIdx, SuccIdx);
623 if (BlockIdx == -1 || BlockIdx < FloorIdx)
629 if (BlockIdx == DivTermIdx) {
636 << BlockIdx <<
"\n");
641 bool CausedJoin =
false;
642 int LoweredFloorIdx = FloorIdx;
662 const auto *BlockCycle =
CI.getCycle(
Block);
668 if (
const auto *BlockCycle = getReducibleParent(
Block)) {
670 BlockCycle->getExitBlocks(BlockCycleExits);
671 for (
auto *BlockCycleExit : BlockCycleExits) {
674 std::min<int>(LoweredFloorIdx,
CyclePOT.getIndex(BlockCycleExit));
678 CausedJoin |=
visitEdge(*SuccBlock, *Label);
680 std::min<int>(LoweredFloorIdx,
CyclePOT.getIndex(SuccBlock));
687 FloorIdx = LoweredFloorIdx;
688 }
else if (FloorLabel != Label) {
691 FloorIdx = LoweredFloorIdx;
712 for (
const auto *Exit : Exits) {
715 DivDesc->CycleDivBlocks.insert(Exit);
726template <
typename ContextT>
730template <
typename ContextT>
733 : CyclePO(
Context), DT(DT), CI(CI) {
737template <
typename ContextT>
742 return EmptyDivergenceDesc;
746 auto ItCached = CachedControlDivDescs.find(DivTermBlock);
747 if (ItCached != CachedControlDivDescs.end())
748 return *ItCached->second;
758 for (
const auto *BB :
Blocks) {
759 Out << LS << CI.getSSAContext().
print(BB);
766 dbgs() <<
"\nResult (" << CI.getSSAContext().print(DivTermBlock)
767 <<
"):\n JoinDivBlocks: " << printBlockSet(DivDesc->JoinDivBlocks)
768 <<
" CycleDivBlocks: " << printBlockSet(DivDesc->CycleDivBlocks)
773 CachedControlDivDescs.try_emplace(DivTermBlock, std::move(DivDesc));
774 assert(ItInserted.second);
775 return *ItInserted.first->second;
778template <
typename ContextT>
781 if (isAlwaysUniform(
I))
784 if (
I.isTerminator()) {
785 Marked = DivergentTermBlocks.insert(
I.getParent()).second;
788 <<
Context.print(
I.getParent()) <<
"\n");
791 Marked = markDefsDivergent(
I);
795 Worklist.push_back(&
I);
798template <
typename ContextT>
801 if (DivergentValues.insert(Val).second) {
808template <
typename ContextT>
811 UniformOverrides.insert(&Instr);
827template <
typename ContextT>
829 const CycleT &DefCycle) {
831 DefCycle.getExitBlocks(Exits);
832 for (
auto *Exit : Exits) {
833 for (
auto &Phi : Exit->phis()) {
834 if (usesValueFromCycle(Phi, DefCycle)) {
840 for (
auto *BB : DefCycle.blocks()) {
842 [&](BlockT *Exit) {
return DT.dominates(BB, Exit); }))
844 for (
auto &II : *BB) {
845 propagateTemporalDivergence(II, DefCycle);
850template <
typename ContextT>
851void GenericUniformityAnalysisImpl<ContextT>::propagateCycleExitDivergence(
852 const BlockT &DivExit,
const CycleT &InnerDivCycle) {
855 auto *DivCycle = &InnerDivCycle;
856 auto *OuterDivCycle = DivCycle;
857 auto *ExitLevelCycle = CI.getCycle(&DivExit);
858 const unsigned CycleExitDepth =
859 ExitLevelCycle ? ExitLevelCycle->getDepth() : 0;
862 while (DivCycle && DivCycle->getDepth() > CycleExitDepth) {
864 <<
Context.print(DivCycle->getHeader()) <<
"\n");
865 OuterDivCycle = DivCycle;
866 DivCycle = DivCycle->getParentCycle();
869 <<
Context.print(OuterDivCycle->getHeader()) <<
"\n");
871 if (!DivergentExitCycles.insert(OuterDivCycle).second)
876 for (
const auto *
C : AssumedDivergent) {
877 if (
C->contains(OuterDivCycle))
881 analyzeCycleExitDivergence(*OuterDivCycle);
884template <
typename ContextT>
885void GenericUniformityAnalysisImpl<ContextT>::taintAndPushAllDefs(
888 for (
const auto &
I :
instrs(BB)) {
892 if (
I.isTerminator())
900template <
typename ContextT>
901void GenericUniformityAnalysisImpl<ContextT>::taintAndPushPhiNodes(
902 const BlockT &JoinBlock) {
905 for (
const auto &Phi : JoinBlock.phis()) {
913 if (ContextT::isConstantOrUndefValuePhi(Phi))
922template <
typename CycleT>
926 [Candidate](CycleT *
C) {
return C->contains(Candidate); }))
937template <
typename CycleT,
typename BlockT>
939 const BlockT *DivTermBlock,
940 const BlockT *JoinBlock) {
947 const auto *OriginalCycle =
Cycle;
949 while (Parent && !Parent->contains(DivTermBlock)) {
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>
1033bool GenericUniformityAnalysisImpl<ContextT>::isTemporalDivergent(
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>
1143template <
typename ContextT>
1145 bool haveDivergentArgs =
false;
1150 if (DivergentValues.empty() && DivergentTermBlocks.empty() &&
1151 DivergentExitCycles.empty()) {
1152 OS <<
"ALL VALUES UNIFORM\n";
1156 for (
const auto &entry : DivergentValues) {
1159 if (!haveDivergentArgs) {
1160 OS <<
"DIVERGENT ARGUMENTS:\n";
1161 haveDivergentArgs =
true;
1163 OS <<
" DIVERGENT: " <<
Context.print(entry) <<
'\n';
1167 if (!AssumedDivergent.empty()) {
1168 OS <<
"CYCLES ASSSUMED DIVERGENT:\n";
1169 for (
const CycleT *cycle : AssumedDivergent) {
1170 OS <<
" " << cycle->print(
Context) <<
'\n';
1174 if (!DivergentExitCycles.empty()) {
1175 OS <<
"CYCLES WITH DIVERGENT EXIT:\n";
1176 for (
const CycleT *cycle : DivergentExitCycles) {
1177 OS <<
" " << cycle->print(
Context) <<
'\n';
1184 OS <<
"DEFINITIONS\n";
1187 for (
auto value : defs) {
1188 if (isDivergent(
value))
1189 OS <<
" DIVERGENT: ";
1195 OS <<
"TERMINATORS\n";
1198 bool divergentTerminators = hasDivergentTerminator(
block);
1199 for (
auto *
T : terms) {
1200 if (divergentTerminators)
1201 OS <<
" DIVERGENT: ";
1207 OS <<
"END BLOCK\n";
1211template <
typename ContextT>
1213 return DA->hasDivergence();
1216template <
typename ContextT>
1217const typename ContextT::FunctionT &
1219 return DA->getFunction();
1223template <
typename ContextT>
1225 return DA->isDivergent(V);
1228template <
typename ContextT>
1230 return DA->isDivergent(*
I);
1233template <
typename ContextT>
1235 return DA->isDivergentUse(U);
1238template <
typename ContextT>
1240 return DA->hasDivergentTerminator(
B);
1244template <
typename ContextT>
1249template <
typename ContextT>
1254 while (!Stack.empty()) {
1255 auto *NextBB = Stack.back();
1256 if (Finalized.
count(NextBB)) {
1260 LLVM_DEBUG(
dbgs() <<
" visiting " << CI.getSSAContext().print(NextBB)
1262 auto *NestedCycle = CI.getCycle(NextBB);
1265 while (NestedCycle->getParentCycle() !=
Cycle)
1266 NestedCycle = NestedCycle->getParentCycle();
1268 SmallVector<BlockT *, 3> NestedExits;
1269 NestedCycle->getExitBlocks(NestedExits);
1270 bool PushedNodes =
false;
1271 for (
auto *NestedExitBB : NestedExits) {
1273 << CI.getSSAContext().print(NestedExitBB) <<
"\n");
1276 if (Finalized.
count(NestedExitBB))
1279 Stack.push_back(NestedExitBB);
1281 << CI.getSSAContext().print(NestedExitBB) <<
"\n");
1286 computeCyclePO(CI, NestedCycle, Finalized);
1293 bool PushedNodes =
false;
1296 << CI.getSSAContext().print(SuccBB) <<
"\n");
1299 if (Finalized.
count(SuccBB))
1302 Stack.push_back(SuccBB);
1303 LLVM_DEBUG(
dbgs() <<
" pushed succ: " << CI.getSSAContext().print(SuccBB)
1309 << CI.getSSAContext().print(NextBB) <<
"\n");
1311 Finalized.
insert(NextBB);
1312 appendBlock(*NextBB);
1318template <
typename ContextT>
1319void ModifiedPostOrder<ContextT>::computeCyclePO(
1320 const CycleInfoT &CI,
const CycleT *
Cycle,
1321 SmallPtrSetImpl<const BlockT *> &Finalized) {
1323 SmallVector<const BlockT *>
Stack;
1327 << CI.getSSAContext().print(CycleHeader) <<
"\n");
1328 assert(!Finalized.count(CycleHeader));
1329 Finalized.insert(CycleHeader);
1333 << CI.getSSAContext().print(CycleHeader) <<
"\n");
1338 LLVM_DEBUG(
dbgs() <<
" examine succ: " << CI.getSSAContext().print(BB)
1342 if (BB == CycleHeader)
1344 if (!Finalized.count(BB)) {
1345 LLVM_DEBUG(
dbgs() <<
" pushed succ: " << CI.getSSAContext().print(BB)
1347 Stack.push_back(BB);
1352 computeStackPO(Stack, CI,
Cycle, Finalized);
1358template <
typename ContextT>
1362 auto *
F = CI.getFunction();
1364 Stack.push_back(&
F->front());
1365 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.
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...
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