[llvm] [VPlan] Add vputils::reconstructSSA (PR #212209)
Luke Lau via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 27 21:14:43 PDT 2026
================
@@ -1143,3 +1143,41 @@ void vputils::detail::pullOutPermutationsImpl(
}
}
}
+
+/// Implements the algorithm described in "Simple and Efficient Construction of
+/// Static Single Assignment Form" by Braun et al.
+static VPValue *reconstructSSAImpl(VPBasicBlock *VPBB,
+ DenseMap<VPBasicBlock *, VPValue *> &Defs) {
+ if (VPValue *Def = Defs.lookup(VPBB))
+ return Def;
+ assert(VPBB->getNumPredecessors() && "Not all paths have def");
+
+ if (VPBlockBase *Pred = VPBB->getSinglePredecessor())
+ return reconstructSSAImpl(cast<VPBasicBlock>(Pred), Defs);
+
+ // Multiple predecessors, create a join.
+ Type *Ty = Defs.begin()->second->getScalarType();
+ auto *Phi = new VPPhi({}, {}, DebugLoc::getUnknown(), "", Ty);
+ VPBB->insert(Phi, VPBB->getFirstNonPhi());
+ Defs[VPBB] = Phi;
+ for (auto *Pred : VPBB->predecessors())
+ Phi->addIncoming(reconstructSSAImpl(cast<VPBasicBlock>(Pred), Defs));
+
+ // Fold away trivial phis.
+ // TODO: Remove phi users which have become trivial too.
+ if (all_equal(Phi->incoming_values())) {
+ VPValue *Common = Phi->getIncomingValue(0);
+ Phi->replaceAllUsesWith(Common);
+ Phi->eraseFromParent();
+ Defs[VPBB] = Common;
+ return Common;
+ }
+
+ return Phi;
+}
+
+VPValue *vputils::reconstructSSA(DenseMap<VPBasicBlock *, VPValue *> Defs,
+ VPBasicBlock *VPBB) {
+ assert(!Defs.empty() && "Defs shouldn't be empty");
----------------
lukel97 wrote:
I could imagine future users of reconstructSSA might dynamically construct Defs, so it might be valid for it to end up passing just one def at runtime
https://github.com/llvm/llvm-project/pull/212209
More information about the llvm-commits
mailing list