[llvm] [PredicateInfo] Support existing `PredicateType` by adding `PredicatePHI` when needing introduction of phi nodes (PR #151132)
Rajveer Singh Bharadwaj via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 30 06:21:12 PDT 2025
https://github.com/Rajveer100 updated https://github.com/llvm/llvm-project/pull/151132
>From fa9177b3ad6d576ef4639ff6e4a77f7c00690d46 Mon Sep 17 00:00:00 2001
From: Rajveer <rajveer.developer at icloud.com>
Date: Tue, 29 Jul 2025 17:21:36 +0530
Subject: [PATCH] [PredicateInfo] Support existing `PredicateType` by adding
`PredicatePHI` when needing introduction of phi nodes
Resolves #150606
Currently `ssa.copy` is used mostly for straight line code, i.e, without
joins or uses of phi nodes. With this, passes would be able to pick up the
relevant info and further optimize the IR.
---
llvm/include/llvm/Transforms/Utils/PredicateInfo.h | 12 +++++++++++-
llvm/lib/Transforms/Utils/PredicateInfo.cpp | 12 ++++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/llvm/include/llvm/Transforms/Utils/PredicateInfo.h b/llvm/include/llvm/Transforms/Utils/PredicateInfo.h
index c243e236901d5..92fd6b5b28e5c 100644
--- a/llvm/include/llvm/Transforms/Utils/PredicateInfo.h
+++ b/llvm/include/llvm/Transforms/Utils/PredicateInfo.h
@@ -67,7 +67,7 @@ class Value;
class IntrinsicInst;
class raw_ostream;
-enum PredicateType { PT_Branch, PT_Assume, PT_Switch };
+enum PredicateType { PT_Branch, PT_Assume, PT_Switch, PT_PHI };
/// Constraint for a predicate of the form "cmp Pred Op, OtherOp", where Op
/// is the value the constraint applies to (the ssa.copy result).
@@ -171,6 +171,16 @@ class PredicateSwitch : public PredicateWithEdge {
}
};
+class PredicatePHI : public PredicateBase {
+public:
+ BasicBlock *PHIBlock;
+ SmallVector<std::pair<BasicBlock *, PredicateBase *>, 4> IncomingPredicates;
+
+ PredicatePHI(Value *Op, BasicBlock *PHIBB)
+ : PredicateBase(PT_PHI, Op, nullptr), PHIBlock(PHIBB) {}
+ static bool classof(const PredicateBase *PB) { return PB->Type == PT_PHI; }
+};
+
/// Encapsulates PredicateInfo, including all data associated with memory
/// accesses.
class PredicateInfo {
diff --git a/llvm/lib/Transforms/Utils/PredicateInfo.cpp b/llvm/lib/Transforms/Utils/PredicateInfo.cpp
index de9deab28750f..ac802aa063953 100644
--- a/llvm/lib/Transforms/Utils/PredicateInfo.cpp
+++ b/llvm/lib/Transforms/Utils/PredicateInfo.cpp
@@ -15,6 +15,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Analysis/AssumptionCache.h"
+#include "llvm/Analysis/IteratedDominanceFrontier.h"
#include "llvm/IR/AssemblyAnnotationWriter.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
@@ -213,6 +214,8 @@ class PredicateInfoBuilder {
// whether it returned a valid result.
DenseMap<Value *, unsigned int> ValueInfoNums;
+ DenseMap<BasicBlock *, SmallVector<Value *, 4>> PHICandidates;
+
BumpPtrAllocator &Allocator;
ValueInfo &getOrCreateValueInfo(Value *);
@@ -478,6 +481,9 @@ void PredicateInfoBuilder::buildPredicateInfo() {
if (DT.isReachableFromEntry(II->getParent()))
processAssume(II, II->getParent(), OpsToRename);
}
+
+ // ...
+
// Now rename all our operations.
renameUses(OpsToRename);
}
@@ -773,6 +779,8 @@ std::optional<PredicateConstraint> PredicateBase::getConstraint() const {
}
return {{CmpInst::ICMP_EQ, cast<PredicateSwitch>(this)->CaseValue}};
+ case PT_PHI:
+ return cast<PredicatePHI>(this)->getConstraint();
}
llvm_unreachable("Unknown predicate type");
}
@@ -838,6 +846,10 @@ class PredicateInfoAnnotatedWriter : public AssemblyAnnotationWriter {
} else if (const auto *PA = dyn_cast<PredicateAssume>(PI)) {
OS << "; assume predicate info {"
<< " Comparison:" << *PA->Condition;
+ } else if (const auto *PP = dyn_cast<PredicatePHI>(PI)) {
+ OS << "; phi predicate info { PHIBlock: ";
+ PP->PHIBlock->printAsOperand(OS);
+ OS << " IncomingEdges: " << PP->IncomingPredicates.size();
}
OS << ", RenamedOp: ";
PI->RenamedOp->printAsOperand(OS, false);
More information about the llvm-commits
mailing list