Skip to content

Commit fa9177b

Browse files
committed
[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.
1 parent ece7a72 commit fa9177b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

llvm/include/llvm/Transforms/Utils/PredicateInfo.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Value;
6767
class IntrinsicInst;
6868
class raw_ostream;
6969

70-
enum PredicateType { PT_Branch, PT_Assume, PT_Switch };
70+
enum PredicateType { PT_Branch, PT_Assume, PT_Switch, PT_PHI };
7171

7272
/// Constraint for a predicate of the form "cmp Pred Op, OtherOp", where Op
7373
/// is the value the constraint applies to (the ssa.copy result).
@@ -171,6 +171,16 @@ class PredicateSwitch : public PredicateWithEdge {
171171
}
172172
};
173173

174+
class PredicatePHI : public PredicateBase {
175+
public:
176+
BasicBlock *PHIBlock;
177+
SmallVector<std::pair<BasicBlock *, PredicateBase *>, 4> IncomingPredicates;
178+
179+
PredicatePHI(Value *Op, BasicBlock *PHIBB)
180+
: PredicateBase(PT_PHI, Op, nullptr), PHIBlock(PHIBB) {}
181+
static bool classof(const PredicateBase *PB) { return PB->Type == PT_PHI; }
182+
};
183+
174184
/// Encapsulates PredicateInfo, including all data associated with memory
175185
/// accesses.
176186
class PredicateInfo {

llvm/lib/Transforms/Utils/PredicateInfo.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ADT/STLExtras.h"
1616
#include "llvm/ADT/SmallPtrSet.h"
1717
#include "llvm/Analysis/AssumptionCache.h"
18+
#include "llvm/Analysis/IteratedDominanceFrontier.h"
1819
#include "llvm/IR/AssemblyAnnotationWriter.h"
1920
#include "llvm/IR/Dominators.h"
2021
#include "llvm/IR/IRBuilder.h"
@@ -213,6 +214,8 @@ class PredicateInfoBuilder {
213214
// whether it returned a valid result.
214215
DenseMap<Value *, unsigned int> ValueInfoNums;
215216

217+
DenseMap<BasicBlock *, SmallVector<Value *, 4>> PHICandidates;
218+
216219
BumpPtrAllocator &Allocator;
217220

218221
ValueInfo &getOrCreateValueInfo(Value *);
@@ -478,6 +481,9 @@ void PredicateInfoBuilder::buildPredicateInfo() {
478481
if (DT.isReachableFromEntry(II->getParent()))
479482
processAssume(II, II->getParent(), OpsToRename);
480483
}
484+
485+
// ...
486+
481487
// Now rename all our operations.
482488
renameUses(OpsToRename);
483489
}
@@ -773,6 +779,8 @@ std::optional<PredicateConstraint> PredicateBase::getConstraint() const {
773779
}
774780

775781
return {{CmpInst::ICMP_EQ, cast<PredicateSwitch>(this)->CaseValue}};
782+
case PT_PHI:
783+
return cast<PredicatePHI>(this)->getConstraint();
776784
}
777785
llvm_unreachable("Unknown predicate type");
778786
}
@@ -838,6 +846,10 @@ class PredicateInfoAnnotatedWriter : public AssemblyAnnotationWriter {
838846
} else if (const auto *PA = dyn_cast<PredicateAssume>(PI)) {
839847
OS << "; assume predicate info {"
840848
<< " Comparison:" << *PA->Condition;
849+
} else if (const auto *PP = dyn_cast<PredicatePHI>(PI)) {
850+
OS << "; phi predicate info { PHIBlock: ";
851+
PP->PHIBlock->printAsOperand(OS);
852+
OS << " IncomingEdges: " << PP->IncomingPredicates.size();
841853
}
842854
OS << ", RenamedOp: ";
843855
PI->RenamedOp->printAsOperand(OS, false);

0 commit comments

Comments
 (0)