44#ifndef LLVM_ADT_GENERICUNIFORMITYIMPL_H
45#define LLVM_ADT_GENERICUNIFORMITYIMPL_H
56#define DEBUG_TYPE "uniformity"
91 using BlockT =
typename ContextT::BlockT;
96 using CycleT =
typename CycleInfoT::CycleT;
101 bool empty()
const {
return Order.empty(); }
102 size_t size()
const {
return Order.size(); }
111 POIndex[&BB] = Order.size();
112 Order.push_back(&BB);
114 <<
"): " << Context.print(&BB) <<
"\n");
115 if (IsReducibleCycleHeader)
116 ReducibleCycleHeaders.insert(&BB);
120 assert(POIndex.count(BB));
121 return POIndex.lookup(BB);
125 return ReducibleCycleHeaders.contains(BB);
132 const ContextT &Context;
142template <
typename>
class DivergencePropagator;
264 using BlockT =
typename ContextT::BlockT;
271 using CycleT =
typename CycleInfoT::CycleT;
321 CachedControlDivDescs;
332 using BlockT =
typename ContextT::BlockT;
336 using UseT =
typename ContextT::UseT;
341 using CycleT =
typename CycleInfoT::CycleT;
345 typename SyncDependenceAnalysisT::DivergenceDescriptor;
349 std::tuple<ConstValueRefT, InstructionT *, const CycleT *>;
386 assert(
I.isTerminator() &&
"Expected a terminator instruction!");
401 if (ContextT::isAlwaysUniform(V))
479 void taintAndPushAllDefs(
const BlockT &JoinBlock);
483 void taintAndPushPhiNodes(
const BlockT &JoinBlock);
488 void propagateCycleExitDivergence(
const BlockT &DivExit,
492 void analyzeCycleExitDivergence(
const CycleT &DefCycle);
505 bool isTemporalDivergent(
const BlockT &ObservingBlock,
509template <
typename ImplT>
517 using BlockT =
typename ContextT::BlockT;
523 using CycleT =
typename CycleInfoT::CycleT;
528 typename SyncDependenceAnalysisT::DivergenceDescriptor;
544 std::unique_ptr<DivergenceDescriptorT>
DivDesc;
554 Out <<
"Propagator::BlockLabels {\n";
555 for (
int BlockIdx = (
int)
CyclePOT.size() - 1; BlockIdx >= 0; --BlockIdx) {
558 Out <<
Context.print(
Block) <<
"(" << BlockIdx <<
") : ";
562 Out <<
Context.print(Label) <<
"\n";
574 <<
"\tpushed label: " <<
Context.print(&PushedLabel)
576 <<
"\told label: " <<
Context.print(OldLabel) <<
"\n");
579 if (OldLabel == &PushedLabel)
582 if (OldLabel != &SuccBlock) {
583 auto SuccIdx =
CyclePOT.getIndex(&SuccBlock);
612 DivDesc->CycleDivBlocks.insert(&ExitBlock);
624 DivDesc->JoinDivBlocks.insert(&SuccBlock);
645 if (
C->isReducible())
647 while (
const CycleT *
P =
C->getParentCycle()) {
648 if (
P->isReducible())
659 if (DivTermCycle && !DivTermCycle->contains(SuccBlock)) {
663 DivDesc->CycleDivBlocks.insert(SuccBlock);
665 <<
Context.print(SuccBlock) <<
"\n");
684 (!IrreducibleAncestor || !IrreducibleAncestor->contains(
Block)))
690 if (BlockIdx == DivTermIdx) {
696 << BlockIdx <<
"\n");
718 const auto *BlockCycle =
CI.getCycle(
Block);
721 BlockCycle->getExitBlocks(BlockCycleExits);
722 bool BranchIsInside = BlockCycle->contains(&
DivTermBlock);
723 for (
auto *BlockCycleExit : BlockCycleExits) {
742 if (
Cycle->isReducible()) {
748 Cycle->getExitBlocks(Exits);
749 auto *Header =
Cycle->getHeader();
751 for (
const auto *Exit : Exits) {
754 DivDesc->CycleDivBlocks.insert(Exit);
765template <
typename ContextT>
768 : CyclePO(Context), DT(DT), CI(CI) {
772template <
typename ContextT>
777 return EmptyDivergenceDesc;
781 auto ItCached = CachedControlDivDescs.find(DivTermBlock);
782 if (ItCached != CachedControlDivDescs.end())
783 return *ItCached->second;
793 for (
const auto *BB : Blocks) {
794 Out << LS << CI.getSSAContext().print(BB);
801 dbgs() <<
"\nResult (" << CI.getSSAContext().print(DivTermBlock)
802 <<
"):\n JoinDivBlocks: " << PrintBlockSet(DivDesc->JoinDivBlocks)
803 <<
" CycleDivBlocks: " << PrintBlockSet(DivDesc->CycleDivBlocks)
808 CachedControlDivDescs.try_emplace(DivTermBlock, std::move(DivDesc));
809 assert(ItInserted.second);
810 return *ItInserted.first->second;
813template <
typename ContextT>
826 if (
I.isTerminator()) {
830 <<
Context.print(
I.getParent()) <<
"\n");
840template <
typename ContextT>
850template <
typename ContextT>
853 UniformOverrides.insert(&Instr);
856template <
typename ContextT>
875template <
typename ContextT>
876void GenericUniformityAnalysisImpl<ContextT>::analyzeCycleExitDivergence(
877 const CycleT &DefCycle) {
879 DefCycle.getExitBlocks(Exits);
880 for (
auto *Exit : Exits) {
881 for (
auto &Phi : Exit->phis()) {
882 if (usesValueFromCycle(Phi, DefCycle)) {
888 for (
auto *BB : DefCycle.blocks()) {
890 [&](BlockT *Exit) {
return DT.dominates(BB, Exit); }))
892 for (
auto &
II : *BB) {
893 propagateTemporalDivergence(
II, DefCycle);
898template <
typename ContextT>
899void GenericUniformityAnalysisImpl<ContextT>::propagateCycleExitDivergence(
900 const BlockT &DivExit,
const CycleT &InnerDivCycle) {
903 auto *DivCycle = &InnerDivCycle;
904 auto *OuterDivCycle = DivCycle;
905 auto *ExitLevelCycle = CI.getCycle(&DivExit);
906 const unsigned CycleExitDepth =
907 ExitLevelCycle ? ExitLevelCycle->getDepth() : 0;
910 while (DivCycle && DivCycle->getDepth() > CycleExitDepth) {
912 <<
Context.print(DivCycle->getHeader()) <<
"\n");
913 OuterDivCycle = DivCycle;
914 DivCycle = DivCycle->getParentCycle();
917 <<
Context.print(OuterDivCycle->getHeader()) <<
"\n");
919 if (!DivergentExitCycles.insert(OuterDivCycle).second)
924 for (
const auto *
C : AssumedDivergent) {
925 if (
C->contains(OuterDivCycle))
929 analyzeCycleExitDivergence(*OuterDivCycle);
932template <
typename ContextT>
933void GenericUniformityAnalysisImpl<ContextT>::taintAndPushAllDefs(
936 for (
const auto &
I :
instrs(BB)) {
940 if (
I.isTerminator())
948template <
typename ContextT>
949void GenericUniformityAnalysisImpl<ContextT>::taintAndPushPhiNodes(
950 const BlockT &JoinBlock) {
953 for (
const auto &Phi : JoinBlock.phis()) {
961 if (ContextT::isConstantOrUndefValuePhi(Phi))
970template <
typename CycleT>
973 [Candidate](CycleT *
C) {
return C->contains(Candidate); }))
975 Cycles.push_back(Candidate);
984template <
typename CycleT,
typename BlockT>
986 const BlockT *JoinBlock) {
990 if (
Cycle->contains(DivTermBlock))
993 const auto *OriginalCycle =
Cycle;
994 const auto *Parent =
Cycle->getParentCycle();
995 while (Parent && !Parent->contains(DivTermBlock)) {
997 Parent =
Cycle->getParentCycle();
1003 (void)OriginalCycle;
1006 if (
Cycle->isReducible()) {
1011 LLVM_DEBUG(
dbgs() <<
"cycle made divergent by external branch\n");
1019template <
typename ContextT,
typename CycleT,
typename BlockT,
1020 typename DominatorTreeT>
1022 const BlockT *JoinBlock,
const DominatorTreeT &DT,
1023 ContextT &Context) {
1025 <<
" for internal branch " << Context.print(DivTermBlock)
1027 if (DT.properlyDominates(DivTermBlock, JoinBlock))
1032 while (
Cycle && !
Cycle->contains(DivTermBlock)) {
1038 if (DT.properlyDominates(
Cycle->getHeader(), JoinBlock))
1042 <<
" does not dominate join\n");
1044 const auto *Parent =
Cycle->getParentCycle();
1045 while (Parent && !DT.properlyDominates(Parent->getHeader(), JoinBlock)) {
1046 LLVM_DEBUG(
dbgs() <<
" header " << Context.print(Parent->getHeader())
1047 <<
" does not dominate join\n");
1049 Parent = Parent->getParentCycle();
1052 LLVM_DEBUG(
dbgs() <<
" cycle made divergent by internal branch\n");
1056template <
typename ContextT,
typename CycleT,
typename BlockT,
1057 typename DominatorTreeT>
1060 const BlockT *JoinBlock,
const DominatorTreeT &DT,
1061 ContextT &Context) {
1077template <
typename ContextT>
1078bool GenericUniformityAnalysisImpl<ContextT>::isTemporalDivergent(
1079 const BlockT &ObservingBlock,
const InstructionT &Def)
const {
1080 const BlockT *DefBlock = Def.getParent();
1081 for (
const CycleT *
Cycle = CI.getCycle(DefBlock);
1084 if (DivergentExitCycles.contains(
Cycle)) {
1091template <
typename ContextT>
1094 const auto *DivTermBlock = Term.getParent();
1100 if (!DT.isReachableFromEntry(DivTermBlock))
1103 const auto &DivDesc = SDA.getJoinBlocks(DivTermBlock);
1107 for (
const auto *JoinBlock : DivDesc.JoinDivBlocks) {
1108 const auto *
Cycle =
CI.getCycle(JoinBlock);
1117 taintAndPushPhiNodes(*JoinBlock);
1123 return A->getDepth() >
B->getDepth();
1131 for (
auto *
C : DivCycles) {
1135 for (
const BlockT *BB :
C->blocks()) {
1136 taintAndPushAllDefs(*BB);
1140 const auto *BranchCycle =
CI.getCycle(DivTermBlock);
1141 assert(DivDesc.CycleDivBlocks.empty() || BranchCycle);
1142 for (
const auto *DivExitBlock : DivDesc.CycleDivBlocks) {
1143 propagateCycleExitDivergence(*DivExitBlock, *BranchCycle);
1147template <
typename ContextT>
1159 if (
I->isTerminator()) {
1170template <
typename ContextT>
1177template <
typename ContextT>
1180 return UniformOverrides.contains(&Instr);
1183template <
typename ContextT>
1189template <
typename ContextT>
1193 DA.reset(
new ImplT{DT, CI,
TTI});
1196template <
typename ContextT>
1201 constexpr bool IsMIR = std::is_same<InstructionT, MachineInstr>::value;
1202 std::string NewLine = IsMIR ?
"" :
"\n";
1204 bool FoundDivergence =
false;
1208 if (!AssumedDivergent.empty()) {
1209 FoundDivergence =
true;
1210 OS <<
"CYCLES ASSUMED DIVERGENT:\n";
1216 if (!DivergentExitCycles.empty()) {
1217 FoundDivergence =
true;
1218 OS <<
"CYCLES WITH DIVERGENT EXIT:\n";
1225 FoundDivergence =
true;
1226 OS <<
"\nTEMPORAL DIVERGENCE LIST:\n";
1229 OS <<
"Value :" <<
Context.print(Val) << NewLine
1230 <<
"Used by :" <<
Context.print(UseInst) << NewLine
1231 <<
"Outside cycle :" <<
Cycle->print(
Context) <<
"\n\n";
1238 OS <<
"DEFINITIONS\n";
1241 for (
auto Value : Defs) {
1243 FoundDivergence =
true;
1244 OS <<
" DIVERGENT: ";
1251 OS <<
"TERMINATORS\n";
1255 if (DivergentTerminators)
1256 FoundDivergence =
true;
1257 for (
auto *
T : Terms) {
1258 if (DivergentTerminators)
1259 OS <<
" DIVERGENT: ";
1265 OS <<
"END BLOCK\n";
1268 if (!FoundDivergence)
1269 OS <<
"ALL VALUES UNIFORM\n";
1272template <
typename ContextT>
1276 return make_range(DA->TemporalDivergenceList.begin(),
1277 DA->TemporalDivergenceList.end());
1280template <
typename ContextT>
1281const typename ContextT::FunctionT &
1283 return DA->getFunction();
1289template <
typename ContextT>
1291 return DA && DA->isDivergent(V);
1294template <
typename ContextT>
1297 assert(
I->isTerminator() &&
"Expected a terminator instruction!");
1298 return DA && DA->isDivergentTerminator(*
I);
1301template <
typename ContextT>
1303 return DA && DA->isDivergentUse(U);
1306template <
typename ContextT>
1308 return DA && DA->hasDivergentTerminator(
B);
1312template <
typename ContextT>
1315 Out <<
" Uniformity analysis not computed (no branch divergence).\n";
1321template <
typename ContextT>
1322void llvm::ModifiedPostOrder<ContextT>::computeStackPO(
1326 while (!Stack.empty()) {
1327 auto *NextBB = Stack.back();
1328 if (Finalized.
count(NextBB)) {
1332 LLVM_DEBUG(
dbgs() <<
" visiting " << CI.getSSAContext().print(NextBB)
1334 auto *NestedCycle = CI.getCycle(NextBB);
1337 while (NestedCycle->getParentCycle() !=
Cycle)
1338 NestedCycle = NestedCycle->getParentCycle();
1341 NestedCycle->getExitBlocks(NestedExits);
1342 bool PushedNodes =
false;
1343 for (
auto *NestedExitBB : NestedExits) {
1345 << CI.getSSAContext().print(NestedExitBB) <<
"\n");
1348 if (Finalized.
count(NestedExitBB))
1351 Stack.push_back(NestedExitBB);
1353 << CI.getSSAContext().print(NestedExitBB) <<
"\n");
1358 computeCyclePO(CI, NestedCycle, Finalized);
1365 bool PushedNodes =
false;
1368 << CI.getSSAContext().print(SuccBB) <<
"\n");
1371 if (Finalized.
count(SuccBB))
1374 Stack.push_back(SuccBB);
1375 LLVM_DEBUG(
dbgs() <<
" pushed succ: " << CI.getSSAContext().print(SuccBB)
1381 << CI.getSSAContext().print(NextBB) <<
"\n");
1383 Finalized.
insert(NextBB);
1384 appendBlock(*NextBB);
1390template <
typename ContextT>
1391void ModifiedPostOrder<ContextT>::computeCyclePO(
1392 const CycleInfoT &CI,
const CycleT *
Cycle,
1396 auto *CycleHeader =
Cycle->getHeader();
1399 << CI.getSSAContext().print(CycleHeader) <<
"\n");
1400 assert(!Finalized.count(CycleHeader));
1401 Finalized.insert(CycleHeader);
1405 << CI.getSSAContext().print(CycleHeader) <<
"\n");
1406 appendBlock(*CycleHeader,
Cycle->isReducible());
1410 LLVM_DEBUG(
dbgs() <<
" examine succ: " << CI.getSSAContext().print(BB)
1412 if (!
Cycle->contains(BB))
1414 if (BB == CycleHeader)
1416 if (!Finalized.count(BB)) {
1417 LLVM_DEBUG(
dbgs() <<
" pushed succ: " << CI.getSSAContext().print(BB)
1419 Stack.push_back(BB);
1424 computeStackPO(Stack, CI,
Cycle, Finalized);
1430template <
typename ContextT>
1434 auto *
F = CI.getFunction();
1436 Stack.push_back(&
F->front());
1437 computeStackPO(Stack, CI,
nullptr, Finalized);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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 defines the SmallPtrSet class.
This file defines the SparseBitVector class.
Implements a dense probed hash-table based set.
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 CycleInfoT::CycleT CycleT
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.
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
DivergencePropagator< ContextT > DivergencePropagatorT
DenseMap< const BlockT *, const BlockT * > BlockLabelMap
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
typename CycleInfoT::CycleT CycleT
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
typename CycleInfoT::CycleT CycleT
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.
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.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
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.
const CycleT * getOutermostDivergentCycle(const CycleT *Cycle, const BlockT *DivTermBlock, const BlockT *JoinBlock, const DominatorTreeT &DT, ContextT &Context)
void sort(IteratorTy Start, IteratorTy End)
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.
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 insertIfNotContained(SmallVector< CycleT * > &Cycles, CycleT *Candidate)
Add Candidate to Cycles if it is not already contained in Cycles.
const CycleT * getExtDivCycle(const CycleT *Cycle, const BlockT *DivTermBlock, const BlockT *JoinBlock)
Return the outermost cycle made divergent by branch outside it.
auto instrs(const MachineBasicBlock &BB)
Information discovered by the sync dependence analysis for each divergent branch.
ConstBlockSet CycleDivBlocks
ConstBlockSet JoinDivBlocks
BlockLabelMap BlockLabels