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 m_order.empty(); }
102 size_t size()
const {
return m_order.size(); }
111 POIndex[&BB] = m_order.size();
112 m_order.push_back(&BB);
114 <<
"): " << Context.print(&BB) <<
"\n");
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 if (
I.isTerminator()) {
403 if (ContextT::isAlwaysUniform(V))
481 void taintAndPushAllDefs(
const BlockT &JoinBlock);
485 void taintAndPushPhiNodes(
const BlockT &JoinBlock);
490 void propagateCycleExitDivergence(
const BlockT &DivExit,
494 void analyzeCycleExitDivergence(
const CycleT &DefCycle);
507 bool isTemporalDivergent(
const BlockT &ObservingBlock,
511template <
typename ImplT>
519 using BlockT =
typename ContextT::BlockT;
525 using CycleT =
typename CycleInfoT::CycleT;
530 typename SyncDependenceAnalysisT::DivergenceDescriptor;
546 std::unique_ptr<DivergenceDescriptorT>
DivDesc;
556 Out <<
"Propagator::BlockLabels {\n";
557 for (
int BlockIdx = (
int)
CyclePOT.size() - 1; BlockIdx >= 0; --BlockIdx) {
560 Out <<
Context.print(
Block) <<
"(" << BlockIdx <<
") : ";
564 Out <<
Context.print(Label) <<
"\n";
576 <<
"\tpushed label: " <<
Context.print(&PushedLabel)
578 <<
"\told label: " <<
Context.print(OldLabel) <<
"\n");
581 if (OldLabel == &PushedLabel)
584 if (OldLabel != &SuccBlock) {
585 auto SuccIdx =
CyclePOT.getIndex(&SuccBlock);
614 DivDesc->CycleDivBlocks.insert(&ExitBlock);
626 DivDesc->JoinDivBlocks.insert(&SuccBlock);
647 if (
C->isReducible())
649 while (
const CycleT *
P =
C->getParentCycle()) {
650 if (
P->isReducible())
661 if (DivTermCycle && !DivTermCycle->contains(SuccBlock)) {
665 DivDesc->CycleDivBlocks.insert(SuccBlock);
667 <<
Context.print(SuccBlock) <<
"\n");
686 (!IrreducibleAncestor || !IrreducibleAncestor->contains(
Block)))
692 if (BlockIdx == DivTermIdx) {
698 << BlockIdx <<
"\n");
720 const auto *BlockCycle =
CI.getCycle(
Block);
723 BlockCycle->getExitBlocks(BlockCycleExits);
724 bool BranchIsInside = BlockCycle->contains(&
DivTermBlock);
725 for (
auto *BlockCycleExit : BlockCycleExits) {
744 if (
Cycle->isReducible()) {
750 Cycle->getExitBlocks(Exits);
751 auto *Header =
Cycle->getHeader();
753 for (
const auto *Exit : Exits) {
756 DivDesc->CycleDivBlocks.insert(Exit);
767template <
typename ContextT>
770 : CyclePO(Context), DT(DT), CI(CI) {
774template <
typename ContextT>
779 return EmptyDivergenceDesc;
783 auto ItCached = CachedControlDivDescs.find(DivTermBlock);
784 if (ItCached != CachedControlDivDescs.end())
785 return *ItCached->second;
795 for (
const auto *BB : Blocks) {
796 Out << LS << CI.getSSAContext().print(BB);
803 dbgs() <<
"\nResult (" << CI.getSSAContext().print(DivTermBlock)
804 <<
"):\n JoinDivBlocks: " << printBlockSet(DivDesc->JoinDivBlocks)
805 <<
" CycleDivBlocks: " << printBlockSet(DivDesc->CycleDivBlocks)
810 CachedControlDivDescs.try_emplace(DivTermBlock, std::move(DivDesc));
811 assert(ItInserted.second);
812 return *ItInserted.first->second;
815template <
typename ContextT>
828 if (
I.isTerminator()) {
832 <<
Context.print(
I.getParent()) <<
"\n");
842template <
typename ContextT>
852template <
typename ContextT>
855 UniformOverrides.insert(&Instr);
858template <
typename ContextT>
877template <
typename ContextT>
878void GenericUniformityAnalysisImpl<ContextT>::analyzeCycleExitDivergence(
879 const CycleT &DefCycle) {
881 DefCycle.getExitBlocks(Exits);
882 for (
auto *Exit : Exits) {
883 for (
auto &Phi : Exit->phis()) {
884 if (usesValueFromCycle(Phi, DefCycle)) {
890 for (
auto *BB : DefCycle.blocks()) {
892 [&](BlockT *Exit) {
return DT.dominates(BB, Exit); }))
894 for (
auto &
II : *BB) {
895 propagateTemporalDivergence(
II, DefCycle);
900template <
typename ContextT>
901void GenericUniformityAnalysisImpl<ContextT>::propagateCycleExitDivergence(
902 const BlockT &DivExit,
const CycleT &InnerDivCycle) {
905 auto *DivCycle = &InnerDivCycle;
906 auto *OuterDivCycle = DivCycle;
907 auto *ExitLevelCycle = CI.getCycle(&DivExit);
908 const unsigned CycleExitDepth =
909 ExitLevelCycle ? ExitLevelCycle->getDepth() : 0;
912 while (DivCycle && DivCycle->getDepth() > CycleExitDepth) {
914 <<
Context.print(DivCycle->getHeader()) <<
"\n");
915 OuterDivCycle = DivCycle;
916 DivCycle = DivCycle->getParentCycle();
919 <<
Context.print(OuterDivCycle->getHeader()) <<
"\n");
921 if (!DivergentExitCycles.insert(OuterDivCycle).second)
926 for (
const auto *
C : AssumedDivergent) {
927 if (
C->contains(OuterDivCycle))
931 analyzeCycleExitDivergence(*OuterDivCycle);
934template <
typename ContextT>
935void GenericUniformityAnalysisImpl<ContextT>::taintAndPushAllDefs(
938 for (
const auto &
I :
instrs(BB)) {
942 if (
I.isTerminator())
950template <
typename ContextT>
951void GenericUniformityAnalysisImpl<ContextT>::taintAndPushPhiNodes(
952 const BlockT &JoinBlock) {
955 for (
const auto &Phi : JoinBlock.phis()) {
963 if (ContextT::isConstantOrUndefValuePhi(Phi))
972template <
typename CycleT>
976 [Candidate](CycleT *
C) {
return C->contains(Candidate); }))
978 Cycles.push_back(Candidate);
987template <
typename CycleT,
typename BlockT>
989 const BlockT *DivTermBlock,
990 const BlockT *JoinBlock) {
994 if (
Cycle->contains(DivTermBlock))
997 const auto *OriginalCycle =
Cycle;
998 const auto *Parent =
Cycle->getParentCycle();
999 while (Parent && !Parent->contains(DivTermBlock)) {
1001 Parent =
Cycle->getParentCycle();
1007 (void)OriginalCycle;
1010 if (
Cycle->isReducible()) {
1015 LLVM_DEBUG(
dbgs() <<
"cycle made divergent by external branch\n");
1023template <
typename ContextT,
typename CycleT,
typename BlockT,
1024 typename DominatorTreeT>
1025static const CycleT *
1027 const BlockT *JoinBlock,
const DominatorTreeT &DT,
1028 ContextT &Context) {
1030 <<
" for internal branch " << Context.print(DivTermBlock)
1032 if (DT.properlyDominates(DivTermBlock, JoinBlock))
1037 while (
Cycle && !
Cycle->contains(DivTermBlock)) {
1043 if (DT.properlyDominates(
Cycle->getHeader(), JoinBlock))
1047 <<
" does not dominate join\n");
1049 const auto *Parent =
Cycle->getParentCycle();
1050 while (Parent && !DT.properlyDominates(Parent->getHeader(), JoinBlock)) {
1051 LLVM_DEBUG(
dbgs() <<
" header " << Context.print(Parent->getHeader())
1052 <<
" does not dominate join\n");
1054 Parent = Parent->getParentCycle();
1057 LLVM_DEBUG(
dbgs() <<
" cycle made divergent by internal branch\n");
1061template <
typename ContextT,
typename CycleT,
typename BlockT,
1062 typename DominatorTreeT>
1063static const CycleT *
1065 const BlockT *JoinBlock,
const DominatorTreeT &DT,
1066 ContextT &Context) {
1082template <
typename ContextT>
1083bool GenericUniformityAnalysisImpl<ContextT>::isTemporalDivergent(
1084 const BlockT &ObservingBlock,
const InstructionT &Def)
const {
1085 const BlockT *DefBlock = Def.getParent();
1086 for (
const CycleT *
Cycle = CI.getCycle(DefBlock);
1089 if (DivergentExitCycles.contains(
Cycle)) {
1096template <
typename ContextT>
1099 const auto *DivTermBlock = Term.getParent();
1105 if (!DT.isReachableFromEntry(DivTermBlock))
1108 const auto &DivDesc = SDA.getJoinBlocks(DivTermBlock);
1112 for (
const auto *JoinBlock : DivDesc.JoinDivBlocks) {
1113 const auto *
Cycle =
CI.getCycle(JoinBlock);
1122 taintAndPushPhiNodes(*JoinBlock);
1128 return A->getDepth() >
B->getDepth();
1136 for (
auto *
C : DivCycles) {
1140 for (
const BlockT *BB :
C->blocks()) {
1141 taintAndPushAllDefs(*BB);
1145 const auto *BranchCycle =
CI.getCycle(DivTermBlock);
1146 assert(DivDesc.CycleDivBlocks.empty() || BranchCycle);
1147 for (
const auto *DivExitBlock : DivDesc.CycleDivBlocks) {
1148 propagateCycleExitDivergence(*DivExitBlock, *BranchCycle);
1152template <
typename ContextT>
1164 if (
I->isTerminator()) {
1175template <
typename ContextT>
1182template <
typename ContextT>
1185 return UniformOverrides.contains(&Instr);
1188template <
typename ContextT>
1194template <
typename ContextT>
1198 DA.reset(
new ImplT{DT, CI,
TTI});
1201template <
typename ContextT>
1206 constexpr bool IsMIR = std::is_same<InstructionT, MachineInstr>::value;
1207 std::string NewLine = IsMIR ?
"" :
"\n";
1209 bool FoundDivergence =
false;
1213 if (!AssumedDivergent.empty()) {
1214 FoundDivergence =
true;
1215 OS <<
"CYCLES ASSUMED DIVERGENT:\n";
1216 for (
const CycleT *cycle : AssumedDivergent) {
1217 OS <<
" " << cycle->print(
Context) <<
'\n';
1221 if (!DivergentExitCycles.empty()) {
1222 FoundDivergence =
true;
1223 OS <<
"CYCLES WITH DIVERGENT EXIT:\n";
1224 for (
const CycleT *cycle : DivergentExitCycles) {
1225 OS <<
" " << cycle->print(
Context) <<
'\n';
1230 FoundDivergence =
true;
1231 OS <<
"\nTEMPORAL DIVERGENCE LIST:\n";
1234 OS <<
"Value :" <<
Context.print(Val) << NewLine
1235 <<
"Used by :" <<
Context.print(UseInst) << NewLine
1236 <<
"Outside cycle :" <<
Cycle->print(
Context) <<
"\n\n";
1243 OS <<
"DEFINITIONS\n";
1246 for (
auto value : defs) {
1248 FoundDivergence =
true;
1249 OS <<
" DIVERGENT: ";
1253 OS <<
Context.print(value) << NewLine;
1256 OS <<
"TERMINATORS\n";
1260 if (divergentTerminators)
1261 FoundDivergence =
true;
1262 for (
auto *
T : terms) {
1263 if (divergentTerminators)
1264 OS <<
" DIVERGENT: ";
1270 OS <<
"END BLOCK\n";
1273 if (!FoundDivergence)
1274 OS <<
"ALL VALUES UNIFORM\n";
1277template <
typename ContextT>
1281 return make_range(DA->TemporalDivergenceList.begin(),
1282 DA->TemporalDivergenceList.end());
1285template <
typename ContextT>
1286const typename ContextT::FunctionT &
1288 return DA->getFunction();
1292template <
typename ContextT>
1294 return DA->isDivergent(V);
1297template <
typename ContextT>
1299 return DA->isDivergent(*
I);
1302template <
typename ContextT>
1304 return DA->isDivergentUse(U);
1307template <
typename ContextT>
1309 return DA->hasDivergentTerminator(
B);
1313template <
typename ContextT>
1318template <
typename ContextT>
1319void llvm::ModifiedPostOrder<ContextT>::computeStackPO(
1323 while (!Stack.empty()) {
1324 auto *NextBB = Stack.back();
1325 if (Finalized.
count(NextBB)) {
1329 LLVM_DEBUG(
dbgs() <<
" visiting " << CI.getSSAContext().print(NextBB)
1331 auto *NestedCycle = CI.getCycle(NextBB);
1334 while (NestedCycle->getParentCycle() !=
Cycle)
1335 NestedCycle = NestedCycle->getParentCycle();
1338 NestedCycle->getExitBlocks(NestedExits);
1339 bool PushedNodes =
false;
1340 for (
auto *NestedExitBB : NestedExits) {
1342 << CI.getSSAContext().print(NestedExitBB) <<
"\n");
1345 if (Finalized.
count(NestedExitBB))
1348 Stack.push_back(NestedExitBB);
1350 << CI.getSSAContext().print(NestedExitBB) <<
"\n");
1355 computeCyclePO(CI, NestedCycle, Finalized);
1362 bool PushedNodes =
false;
1365 << CI.getSSAContext().print(SuccBB) <<
"\n");
1368 if (Finalized.
count(SuccBB))
1371 Stack.push_back(SuccBB);
1372 LLVM_DEBUG(
dbgs() <<
" pushed succ: " << CI.getSSAContext().print(SuccBB)
1378 << CI.getSSAContext().print(NextBB) <<
"\n");
1380 Finalized.
insert(NextBB);
1381 appendBlock(*NextBB);
1387template <
typename ContextT>
1388void ModifiedPostOrder<ContextT>::computeCyclePO(
1389 const CycleInfoT &CI,
const CycleT *
Cycle,
1393 auto *CycleHeader =
Cycle->getHeader();
1396 << CI.getSSAContext().print(CycleHeader) <<
"\n");
1397 assert(!Finalized.count(CycleHeader));
1398 Finalized.insert(CycleHeader);
1402 << CI.getSSAContext().print(CycleHeader) <<
"\n");
1403 appendBlock(*CycleHeader,
Cycle->isReducible());
1407 LLVM_DEBUG(
dbgs() <<
" examine succ: " << CI.getSSAContext().print(BB)
1409 if (!
Cycle->contains(BB))
1411 if (BB == CycleHeader)
1413 if (!Finalized.count(BB)) {
1414 LLVM_DEBUG(
dbgs() <<
" pushed succ: " << CI.getSSAContext().print(BB)
1416 Stack.push_back(BB);
1421 computeStackPO(Stack, CI,
Cycle, Finalized);
1427template <
typename ContextT>
1431 auto *
F = CI.getFunction();
1433 Stack.push_back(&
F->front());
1434 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.
unify loop Fixup each natural loop to have a single exit block
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
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.
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.
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.
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.
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.
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...
auto instrs(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