Skip to content

Commit 96e1320

Browse files
committed
[VPlan] Move properlyDominates to VPDominatorTree (NFCI).
This allows for easier re-use in additional places in the future. Also move code to VPlanAnalysis.cpp
1 parent 026210e commit 96e1320

File tree

4 files changed

+56
-48
lines changed

4 files changed

+56
-48
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#include "llvm/Support/Casting.h"
4141
#include "llvm/Support/CommandLine.h"
4242
#include "llvm/Support/Debug.h"
43-
#include "llvm/Support/GenericDomTreeConstruction.h"
4443
#include "llvm/Support/GraphWriter.h"
4544
#include "llvm/Support/raw_ostream.h"
4645
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -1422,8 +1421,6 @@ void VPlanIngredient::print(raw_ostream &O) const {
14221421

14231422
#endif
14241423

1425-
template void DomTreeBuilder::Calculate<VPDominatorTree>(VPDominatorTree &DT);
1426-
14271424
void VPValue::replaceAllUsesWith(VPValue *New) {
14281425
replaceUsesWithIf(New, [](VPUser &, unsigned) { return true; });
14291426
}

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#include "VPlanAnalysis.h"
1010
#include "VPlan.h"
1111
#include "VPlanCFG.h"
12+
#include "VPlanDominatorTree.h"
1213
#include "llvm/ADT/TypeSwitch.h"
1314
#include "llvm/IR/Instruction.h"
1415
#include "llvm/IR/PatternMatch.h"
16+
#include "llvm/Support/GenericDomTreeConstruction.h"
1517

1618
using namespace llvm;
1719

@@ -319,3 +321,46 @@ void llvm::collectEphemeralRecipesForVPlan(
319321
}
320322
}
321323
}
324+
325+
template void DomTreeBuilder::Calculate<DominatorTreeBase<VPBlockBase, false>>(
326+
DominatorTreeBase<VPBlockBase, false> &DT);
327+
328+
bool VPDominatorTree::properlyDominates(const VPRecipeBase *A,
329+
const VPRecipeBase *B) {
330+
if (A == B)
331+
return false;
332+
333+
auto LocalComesBefore = [](const VPRecipeBase *A, const VPRecipeBase *B) {
334+
for (auto &R : *A->getParent()) {
335+
if (&R == A)
336+
return true;
337+
if (&R == B)
338+
return false;
339+
}
340+
llvm_unreachable("recipe not found");
341+
};
342+
const VPBlockBase *ParentA = A->getParent();
343+
const VPBlockBase *ParentB = B->getParent();
344+
if (ParentA == ParentB)
345+
return LocalComesBefore(A, B);
346+
347+
#ifndef NDEBUG
348+
auto GetReplicateRegion = [](VPRecipeBase *R) -> VPRegionBlock * {
349+
auto *Region = dyn_cast_or_null<VPRegionBlock>(R->getParent()->getParent());
350+
if (Region && Region->isReplicator()) {
351+
assert(Region->getNumSuccessors() == 1 &&
352+
Region->getNumPredecessors() == 1 && "Expected SESE region!");
353+
assert(R->getParent()->size() == 1 &&
354+
"A recipe in an original replicator region must be the only "
355+
"recipe in its block");
356+
return Region;
357+
}
358+
return nullptr;
359+
};
360+
assert(!GetReplicateRegion(const_cast<VPRecipeBase *>(A)) &&
361+
"No replicate regions expected at this point");
362+
assert(!GetReplicateRegion(const_cast<VPRecipeBase *>(B)) &&
363+
"No replicate regions expected at this point");
364+
#endif
365+
return Base::properlyDominates(ParentA, ParentB);
366+
}

llvm/lib/Transforms/Vectorize/VPlanDominatorTree.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ template <> struct DomTreeNodeTraits<VPBlockBase> {
3232
static ParentPtr getParent(NodePtr B) { return B->getPlan(); }
3333
};
3434

35-
///
3635
/// Template specialization of the standard LLVM dominator tree utility for
3736
/// VPBlockBases.
38-
using VPDominatorTree = DomTreeBase<VPBlockBase>;
37+
class VPDominatorTree : public DominatorTreeBase<VPBlockBase, false> {
38+
using Base = DominatorTreeBase<VPBlockBase, false>;
39+
40+
public:
41+
VPDominatorTree() = default;
42+
43+
/// Returns true if \p A properly dominates \p B.
44+
bool properlyDominates(const VPRecipeBase *A, const VPRecipeBase *B);
45+
};
3946

4047
using VPDomTreeNode = DomTreeNodeBase<VPBlockBase>;
4148

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -716,47 +716,6 @@ void VPlanTransforms::optimizeForVFAndUF(VPlan &Plan, ElementCount BestVF,
716716
// 2. Replace vector loop region with VPBasicBlock.
717717
}
718718

719-
#ifndef NDEBUG
720-
static VPRegionBlock *GetReplicateRegion(VPRecipeBase *R) {
721-
auto *Region = dyn_cast_or_null<VPRegionBlock>(R->getParent()->getParent());
722-
if (Region && Region->isReplicator()) {
723-
assert(Region->getNumSuccessors() == 1 &&
724-
Region->getNumPredecessors() == 1 && "Expected SESE region!");
725-
assert(R->getParent()->size() == 1 &&
726-
"A recipe in an original replicator region must be the only "
727-
"recipe in its block");
728-
return Region;
729-
}
730-
return nullptr;
731-
}
732-
#endif
733-
734-
static bool properlyDominates(const VPRecipeBase *A, const VPRecipeBase *B,
735-
VPDominatorTree &VPDT) {
736-
if (A == B)
737-
return false;
738-
739-
auto LocalComesBefore = [](const VPRecipeBase *A, const VPRecipeBase *B) {
740-
for (auto &R : *A->getParent()) {
741-
if (&R == A)
742-
return true;
743-
if (&R == B)
744-
return false;
745-
}
746-
llvm_unreachable("recipe not found");
747-
};
748-
const VPBlockBase *ParentA = A->getParent();
749-
const VPBlockBase *ParentB = B->getParent();
750-
if (ParentA == ParentB)
751-
return LocalComesBefore(A, B);
752-
753-
assert(!GetReplicateRegion(const_cast<VPRecipeBase *>(A)) &&
754-
"No replicate regions expected at this point");
755-
assert(!GetReplicateRegion(const_cast<VPRecipeBase *>(B)) &&
756-
"No replicate regions expected at this point");
757-
return VPDT.properlyDominates(ParentA, ParentB);
758-
}
759-
760719
/// Sink users of \p FOR after the recipe defining the previous value \p
761720
/// Previous of the recurrence. \returns true if all users of \p FOR could be
762721
/// re-arranged as needed or false if it is not possible.
@@ -776,7 +735,7 @@ sinkRecurrenceUsersAfterPrevious(VPFirstOrderRecurrencePHIRecipe *FOR,
776735

777736
if (isa<VPHeaderPHIRecipe>(SinkCandidate) ||
778737
!Seen.insert(SinkCandidate).second ||
779-
properlyDominates(Previous, SinkCandidate, VPDT))
738+
VPDT.properlyDominates(Previous, SinkCandidate))
780739
return true;
781740

782741
if (SinkCandidate->mayHaveSideEffects())
@@ -803,7 +762,7 @@ sinkRecurrenceUsersAfterPrevious(VPFirstOrderRecurrencePHIRecipe *FOR,
803762
// Keep recipes to sink ordered by dominance so earlier instructions are
804763
// processed first.
805764
sort(WorkList, [&VPDT](const VPRecipeBase *A, const VPRecipeBase *B) {
806-
return properlyDominates(A, B, VPDT);
765+
return VPDT.properlyDominates(A, B);
807766
});
808767

809768
for (VPRecipeBase *SinkCandidate : WorkList) {

0 commit comments

Comments
 (0)