45#ifndef LLVM_ADT_GENERICUNIFORMITYIMPL_H
46#define LLVM_ADT_GENERICUNIFORMITYIMPL_H
58#define DEBUG_TYPE "uniformity"
93 using BlockT =
typename ContextT::BlockT;
102 bool empty()
const {
return Order.empty(); }
103 size_t size()
const {
return Order.size(); }
110 return Num < POIndex.size() && POIndex[Num] != InvalidIndex;
116 POIndex[Num] = Order.size();
117 Order.push_back(&BB);
119 <<
"): " << Context.print(&BB) <<
"\n");
120 if (IsReducibleCycleHeader)
121 ReducibleCycleHeaders.insert(&BB);
126 assert(Num < POIndex.size() && POIndex[Num] != InvalidIndex);
131 return ReducibleCycleHeaders.contains(BB);
150template <
typename>
class DivergencePropagator;
272 using BlockT =
typename ContextT::BlockT;
317 CachedControlDivDescs;
328 using BlockT =
typename ContextT::BlockT;
332 using UseT =
typename ContextT::UseT;
340 typename SyncDependenceAnalysisT::DivergenceDescriptor;
343 std::tuple<ConstValueRefT, InstructionT *, CycleRef>;
382 assert(
I.isTerminator() &&
"Expected a terminator instruction!");
397 if (ContextT::isAlwaysUniform(
V))
478 void taintAndPushAllDefs(
const BlockT &JoinBlock);
482 void taintAndPushPhiNodes(
const BlockT &JoinBlock);
487 void propagateCycleExitDivergence(
const BlockT &DivExit,
CycleRef DivCycle);
490 void analyzeCycleExitDivergence(
CycleRef DefCycle);
502 bool isTemporalDivergent(
const BlockT &ObservingBlock,
506template <
typename ImplT>
514 using BlockT =
typename ContextT::BlockT;
524 typename SyncDependenceAnalysisT::DivergenceDescriptor;
539 std::unique_ptr<DivergenceDescriptorT>
DivDesc;
563 Out <<
"Propagator::BlockLabels {\n";
564 for (
int BlockIdx = (
int)
CyclePOT.size() - 1; BlockIdx >= 0; --BlockIdx) {
567 Out <<
Context.print(
Block) <<
"(" << BlockIdx <<
") : ";
571 Out <<
Context.print(Label) <<
"\n";
580 const auto *OldLabel =
label(&SuccBlock);
583 <<
"\tpushed label: " <<
Context.print(&PushedLabel)
585 <<
"\told label: " <<
Context.print(OldLabel) <<
"\n");
588 if (OldLabel == &PushedLabel)
591 if (OldLabel != &SuccBlock) {
592 auto SuccIdx =
CyclePOT.getIndex(&SuccBlock);
602 label(&SuccBlock) = &PushedLabel;
609 label(&SuccBlock) = &SuccBlock;
621 DivDesc->CycleDivBlocks.insert(&ExitBlock);
633 DivDesc->JoinDivBlocks.insert(&SuccBlock);
654 if (
CI.isReducible(
C))
657 if (
CI.isReducible(
P))
668 if (DivTermCycle && !
CI.contains(DivTermCycle, SuccBlock)) {
672 DivDesc->CycleDivBlocks.insert(SuccBlock);
674 <<
Context.print(SuccBlock) <<
"\n");
693 (!IrreducibleAncestor || !
CI.contains(IrreducibleAncestor,
Block)))
701 if (BlockIdx == DivTermIdx && !
CyclePOT.isReducibleCycleHeader(
Block)) {
702 LLVM_DEBUG(
dbgs() <<
"Skipping DivTermBlock, which is not a reducible "
708 << BlockIdx <<
"\n");
733 CI.getExitBlocks(BlockCycle, BlockCycleExits);
735 for (
auto *BlockCycleExit : BlockCycleExits) {
759 if (
CI.isReducible(
C)) {
765 CI.getExitBlocks(
C, Exits);
766 auto *Header =
CI.getHeader(
C);
767 auto *HeaderLabel =
label(Header);
768 for (
const auto *Exit : Exits) {
769 if (
label(Exit) != HeaderLabel) {
771 DivDesc->CycleDivBlocks.insert(Exit);
782template <
typename ContextT>
785 : CyclePO(
Context), DT(DT), CI(CI) {
789template <
typename ContextT>
794 return EmptyDivergenceDesc;
798 auto ItCached = CachedControlDivDescs.find(DivTermBlock);
799 if (ItCached != CachedControlDivDescs.end())
800 return *ItCached->second;
810 for (
const auto *BB : Blocks) {
811 Out << LS << CI.getSSAContext().print(BB);
818 dbgs() <<
"\nResult (" << CI.getSSAContext().print(DivTermBlock)
819 <<
"):\n JoinDivBlocks: " << PrintBlockSet(DivDesc->JoinDivBlocks)
820 <<
" CycleDivBlocks: " << PrintBlockSet(DivDesc->CycleDivBlocks)
825 CachedControlDivDescs.try_emplace(DivTermBlock, std::move(DivDesc));
826 assert(ItInserted.second);
827 return *ItInserted.first->second;
830template <
typename ContextT>
843 if (
I.isTerminator()) {
847 <<
Context.print(
I.getParent()) <<
"\n");
857template <
typename ContextT>
867template <
typename ContextT>
870 UniformOverrides.insert(&Instr);
873template <
typename ContextT>
892template <
typename ContextT>
893void GenericUniformityAnalysisImpl<ContextT>::analyzeCycleExitDivergence(
896 CI.getExitBlocks(DefCycle, Exits);
897 for (
auto *Exit : Exits) {
898 for (
auto &Phi : Exit->phis()) {
899 if (usesValueFromCycle(Phi, DefCycle)) {
905 for (
auto *BB : CI.getBlocks(DefCycle)) {
907 [&](BlockT *Exit) {
return DT.dominates(BB, Exit); }))
909 for (
auto &
II : *BB) {
910 propagateTemporalDivergence(
II, DefCycle);
915template <
typename ContextT>
916void GenericUniformityAnalysisImpl<ContextT>::propagateCycleExitDivergence(
917 const BlockT &DivExit,
CycleRef InnerDivCycle) {
924 while (DivCycle && !CI.contains(DivCycle, &DivExit)) {
926 <<
Context.print(CI.getHeader(DivCycle)) <<
"\n");
927 OuterDivCycle = DivCycle;
928 DivCycle = CI.getParentCycle(DivCycle);
931 <<
Context.print(CI.getHeader(OuterDivCycle)) <<
"\n");
933 if (!DivergentExitCycles.insert(OuterDivCycle))
938 for (
auto C : AssumedDivergent) {
939 if (CI.contains(
C, OuterDivCycle))
943 analyzeCycleExitDivergence(OuterDivCycle);
946template <
typename ContextT>
947void GenericUniformityAnalysisImpl<ContextT>::taintAndPushAllDefs(
950 for (
const auto &
I :
instrs(BB)) {
954 if (
I.isTerminator())
962template <
typename ContextT>
963void GenericUniformityAnalysisImpl<ContextT>::taintAndPushPhiNodes(
964 const BlockT &JoinBlock) {
967 for (
const auto &Phi : JoinBlock.phis()) {
975 if (ContextT::isConstantOrUndefValuePhi(Phi))
984template <
typename CycleInfoT>
988 [&](
CycleRef C) {
return CI.contains(
C, Candidate); }))
990 Cycles.push_back(Candidate);
999template <
typename CycleInfoT,
typename BlockT>
1001 const BlockT *DivTermBlock,
const BlockT *JoinBlock) {
1003 assert(CI.contains(Cycle, JoinBlock));
1005 if (CI.contains(Cycle, DivTermBlock))
1009 CycleRef Parent = CI.getParentCycle(Cycle);
1010 while (Parent && !CI.contains(Parent, DivTermBlock)) {
1012 Parent = CI.getParentCycle(Cycle);
1018 (void)OriginalCycle;
1019 assert(Cycle == OriginalCycle || !CI.isReducible(Cycle));
1021 if (CI.isReducible(Cycle)) {
1022 assert(CI.getHeader(Cycle) == JoinBlock);
1026 LLVM_DEBUG(
dbgs() <<
"cycle made divergent by external branch\n");
1034template <
typename ContextT,
typename CycleInfoT,
typename BlockT,
1035 typename DominatorTreeT>
1037 const BlockT *DivTermBlock,
const BlockT *JoinBlock,
1038 const DominatorTreeT &DT, ContextT &
Context) {
1040 <<
" for internal branch " <<
Context.print(DivTermBlock)
1042 if (DT.properlyDominates(DivTermBlock, JoinBlock))
1046 assert(Cycle && CI.contains(Cycle, JoinBlock));
1047 while (Cycle && !CI.contains(Cycle, DivTermBlock)) {
1048 Cycle = CI.getParentCycle(Cycle);
1050 if (!Cycle || CI.isReducible(Cycle))
1053 if (DT.properlyDominates(CI.getHeader(Cycle), JoinBlock))
1057 <<
" does not dominate join\n");
1059 CycleRef Parent = CI.getParentCycle(Cycle);
1060 while (Parent && !DT.properlyDominates(CI.getHeader(Parent), JoinBlock)) {
1062 <<
" does not dominate join\n");
1064 Parent = CI.getParentCycle(Parent);
1067 LLVM_DEBUG(
dbgs() <<
" cycle made divergent by internal branch\n");
1071template <
typename ContextT,
typename CycleInfoT,
typename BlockT,
1072 typename DominatorTreeT>
1075 const BlockT *DivTermBlock,
const BlockT *JoinBlock,
1076 const DominatorTreeT &DT, ContextT &
Context) {
1093template <
typename ContextT>
1094bool GenericUniformityAnalysisImpl<ContextT>::isTemporalDivergent(
1095 const BlockT &ObservingBlock,
const InstructionT &Def)
const {
1096 const BlockT *DefBlock = Def.getParent();
1097 for (
auto C = CI.getCycle(DefBlock);
C && !CI.contains(
C, &ObservingBlock);
1098 C = CI.getParentCycle(
C)) {
1099 if (DivergentExitCycles.contains(
C)) {
1106template <
typename ContextT>
1109 const auto *DivTermBlock = Term.getParent();
1115 if (!DT.isReachableFromEntry(DivTermBlock))
1118 const auto &DivDesc = SDA.getJoinBlocks(DivTermBlock);
1122 for (
const auto *JoinBlock : DivDesc.JoinDivBlocks) {
1127 CI,
C, DivTermBlock, JoinBlock, DT,
Context)) {
1132 taintAndPushPhiNodes(*JoinBlock);
1138 return CI.getDepth(
A) >
CI.getDepth(
B);
1146 for (
auto C : DivCycles) {
1150 for (
const BlockT *BB :
CI.getBlocks(
C)) {
1151 taintAndPushAllDefs(*BB);
1155 CycleRef BranchCycle =
CI.getCycle(DivTermBlock);
1156 assert(DivDesc.CycleDivBlocks.empty() || BranchCycle);
1157 for (
const auto *DivExitBlock : DivDesc.CycleDivBlocks) {
1158 propagateCycleExitDivergence(*DivExitBlock, BranchCycle);
1162template <
typename ContextT>
1174 if (
I->isTerminator()) {
1185template <
typename ContextT>
1191template <
typename ContextT>
1194 return UniformOverrides.contains(&Instr);
1197template <
typename ContextT>
1203template <
typename ContextT>
1207 DA.reset(
new ImplT{DT, CI,
TTI});
1210template <
typename ContextT>
1215 constexpr bool IsMIR = std::is_same<InstructionT, MachineInstr>::value;
1216 std::string NewLine = IsMIR ?
"" :
"\n";
1218 bool FoundDivergence =
false;
1222 if (!AssumedDivergent.empty()) {
1223 FoundDivergence =
true;
1224 OS <<
"CYCLES ASSUMED DIVERGENT:\n";
1225 for (
auto C : AssumedDivergent) {
1226 OS <<
" " <<
CI.print(
C) <<
'\n';
1230 if (!DivergentExitCycles.empty()) {
1231 FoundDivergence =
true;
1232 OS <<
"CYCLES WITH DIVERGENT EXIT:\n";
1233 for (
auto C : DivergentExitCycles) {
1234 OS <<
" " <<
CI.print(
C) <<
'\n';
1239 FoundDivergence =
true;
1240 OS <<
"\nTEMPORAL DIVERGENCE LIST:\n";
1243 OS <<
"Value :" <<
Context.print(Val) << NewLine
1244 <<
"Used by :" <<
Context.print(UseInst) << NewLine
1245 <<
"Outside cycle :" <<
CI.print(
C) <<
"\n\n";
1252 OS <<
"DEFINITIONS\n";
1255 for (
auto Value : Defs) {
1257 FoundDivergence =
true;
1258 OS <<
" DIVERGENT: ";
1265 OS <<
"TERMINATORS\n";
1269 if (DivergentTerminators)
1270 FoundDivergence =
true;
1271 for (
auto *
T : Terms) {
1272 if (DivergentTerminators)
1273 OS <<
" DIVERGENT: ";
1279 OS <<
"END BLOCK\n";
1282 if (!FoundDivergence)
1283 OS <<
"ALL VALUES UNIFORM\n";
1286template <
typename ContextT>
1290 return make_range(DA->TemporalDivergenceList.begin(),
1291 DA->TemporalDivergenceList.end());
1294template <
typename ContextT>
1295const typename ContextT::FunctionT &
1297 return DA->getFunction();
1300template <
typename ContextT>
1303 return DA->getCycleInfo();
1309template <
typename ContextT>
1311 return DA && DA->isDivergent(
V);
1314template <
typename ContextT>
1317 assert(
I->isTerminator() &&
"Expected a terminator instruction!");
1318 return DA && DA->isDivergentTerminator(*
I);
1321template <
typename ContextT>
1323 return DA && DA->isDivergentUse(U);
1326template <
typename ContextT>
1328 return DA && DA->hasDivergentTerminator(
B);
1331template <
typename ContextT>
1338template <
typename ContextT>
1341 Out <<
" Uniformity analysis not computed (no branch divergence).\n";
1347template <
typename ContextT>
1348void llvm::ModifiedPostOrder<ContextT>::computeStackPO(
1352 while (!Stack.empty()) {
1353 auto *NextBB = Stack.back();
1354 if (Finalized.
count(NextBB)) {
1358 LLVM_DEBUG(
dbgs() <<
" visiting " << CI.getSSAContext().print(NextBB)
1360 CycleRef NestedCycle = CI.getCycle(NextBB);
1361 if (
C != NestedCycle &&
1362 (!
C || (NestedCycle && CI.contains(
C, NestedCycle)))) {
1364 while (CI.getParentCycle(NestedCycle) !=
C)
1365 NestedCycle = CI.getParentCycle(NestedCycle);
1368 CI.getExitBlocks(NestedCycle, NestedExits);
1369 bool PushedNodes =
false;
1370 for (
auto *NestedExitBB : NestedExits) {
1372 << CI.getSSAContext().print(NestedExitBB) <<
"\n");
1373 if (
C && !CI.contains(
C, NestedExitBB))
1375 if (Finalized.
count(NestedExitBB))
1378 Stack.push_back(NestedExitBB);
1380 << CI.getSSAContext().print(NestedExitBB) <<
"\n");
1385 computeCyclePO(CI, NestedCycle, Finalized);
1392 bool PushedNodes =
false;
1395 << CI.getSSAContext().print(SuccBB) <<
"\n");
1396 if (
C && !CI.contains(
C, SuccBB))
1398 if (Finalized.
count(SuccBB))
1401 Stack.push_back(SuccBB);
1402 LLVM_DEBUG(
dbgs() <<
" pushed succ: " << CI.getSSAContext().print(SuccBB)
1408 << CI.getSSAContext().print(NextBB) <<
"\n");
1410 Finalized.
insert(NextBB);
1411 appendBlock(*NextBB);
1417template <
typename ContextT>
1418void ModifiedPostOrder<ContextT>::computeCyclePO(
1423 auto *CycleHeader = CI.getHeader(
C);
1426 << CI.getSSAContext().print(CycleHeader) <<
"\n");
1427 assert(!Finalized.count(CycleHeader));
1428 Finalized.insert(CycleHeader);
1432 << CI.getSSAContext().print(CycleHeader) <<
"\n");
1433 appendBlock(*CycleHeader, CI.isReducible(
C));
1437 LLVM_DEBUG(
dbgs() <<
" examine succ: " << CI.getSSAContext().print(BB)
1439 if (!CI.contains(
C, BB))
1441 if (BB == CycleHeader)
1443 if (!Finalized.count(BB)) {
1444 LLVM_DEBUG(
dbgs() <<
" pushed succ: " << CI.getSSAContext().print(BB)
1446 Stack.push_back(BB);
1451 computeStackPO(Stack, CI,
C, Finalized);
1457template <
typename ContextT>
1461 auto *
F = CI.getFunction();
1464 Stack.push_back(&
F->front());
1465 computeStackPO(Stack, CI,
CycleRef(), Finalized);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the DenseSet and SmallDenseSet classes.
uint64_t IntrinsicInst * II
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SparseBitVector class.
static const unsigned InvalidIndex
static Function * getFunction(FunctionType *Ty, const Twine &Name, Module *M)
Opaque handle to a cycle within a GenericCycleInfo that wraps the cycle's preorder index.
Implements a dense probed hash-table based set.
Compute divergence starting with a divergent branch.
const ModifiedPO & CyclePOT
GenericSyncDependenceAnalysis< ContextT > SyncDependenceAnalysisT
const BlockT *& label(const BlockT *BB)
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
SmallVector< const BlockT * > BlockLabels
const DominatorTreeT & DT
ModifiedPostOrder< ContextT > ModifiedPO
std::unique_ptr< DivergenceDescriptorT > computeJoinPoints()
SparseBitVector FreshLabels
bool visitCycleExitEdge(const BlockT &ExitBlock, const BlockT &Label)
typename ContextT::ValueRefT ValueRefT
typename ContextT::BlockT BlockT
DivergencePropagator(const ModifiedPO &CyclePOT, const DominatorTreeT &DT, const CycleInfoT &CI, const BlockT &DivTermBlock)
bool visitEdge(const BlockT &SuccBlock, const BlockT &Label)
typename SyncDependenceAnalysisT::DivergenceDescriptor DivergenceDescriptorT
Cycle information for a function.
Locate join blocks for disjoint paths starting at a divergent branch.
GenericSyncDependenceAnalysis(const ContextT &Context, const DominatorTreeT &DT, const CycleInfoT &CI)
ModifiedPostOrder< ContextT > ModifiedPO
DivergencePropagator< ContextT > DivergencePropagatorT
SmallPtrSet< const BlockT *, 4 > ConstBlockSet
typename ContextT::DominatorTreeT DominatorTreeT
GenericCycleInfo< ContextT > CycleInfoT
typename ContextT::FunctionT FunctionT
typename ContextT::InstructionT InstructionT
typename ContextT::BlockT BlockT
typename ContextT::ValueRefT ValueRefT
const DivergenceDescriptor & getJoinBlocks(const BlockT *DivTermBlock)
Computes divergent join points and cycle exits caused by branch divergence in Term.
A helper class to return the specified delimiter string after the first invocation of operator String...
Construct a specially modified post-order traversal of cycles.
typename ContextT::FunctionT FunctionT
bool isReducibleCycleHeader(const BlockT *BB) const
void appendBlock(const BlockT &BB, bool IsReducibleCycleHeader=false)
const BlockT * operator[](size_t Idx) const
ModifiedPostOrder(const ContextT &C)
unsigned count(BlockT *BB) const
void compute(const CycleInfoT &CI)
Generically compute the modified post order.
GenericCycleInfo< ContextT > CycleInfoT
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.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
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.
LLVM Value Representation.
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
This is an optimization pass for GlobalISel generic memory operations.
bool insertIfNotContained(const CycleInfoT &CI, SmallVector< CycleRef > &Cycles, CycleRef Candidate)
Add Candidate to Cycles if it is not already contained in Cycles.
CycleRef getExtDivCycle(const CycleInfoT &CI, CycleRef Cycle, const BlockT *DivTermBlock, const BlockT *JoinBlock)
Return the outermost cycle made divergent by branch outside it.
auto successors(const MachineBasicBlock *BB)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
CycleRef getOutermostDivergentCycle(const CycleInfoT &CI, CycleRef Cycle, const BlockT *DivTermBlock, const BlockT *JoinBlock, const DominatorTreeT &DT, ContextT &Context)
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
auto succ_size(const MachineBasicBlock *BB)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
auto instrs(const MachineBasicBlock &BB)
CycleRef getIntDivCycle(const CycleInfoT &CI, CycleRef Cycle, const BlockT *DivTermBlock, const BlockT *JoinBlock, const DominatorTreeT &DT, ContextT &Context)
Return the outermost cycle made divergent by branch inside it.
Information discovered by the sync dependence analysis for each divergent branch.
ConstBlockSet CycleDivBlocks
ConstBlockSet JoinDivBlocks