[llvm] [VPlan] Introduce VPlanConstantFolder (PR #125365)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 7 13:52:03 PDT 2025


================
@@ -1076,15 +1078,39 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
       TypeInfo.inferScalarType(R.getOperand(1)) ==
           TypeInfo.inferScalarType(R.getVPSingleValue()))
     return R.getVPSingleValue()->replaceAllUsesWith(R.getOperand(1));
+
+  // Constant folding. TODO: cast and cmp.
+  VPConstantFolder Folder(DL);
+  VPlan *Plan = R.getParent()->getPlan();
+  if (match(&R, m_BinaryAnd(m_VPValue(X), m_VPValue(Y))))
+    if (Value *V = Folder.foldAnd(X, Y))
+      R.getVPSingleValue()->replaceAllUsesWith(Plan->getOrAddLiveIn(V));
+  if (match(&R, m_BinaryOr(m_VPValue(X), m_VPValue(Y))))
+    if (Value *V = Folder.foldOr(X, Y))
+      R.getVPSingleValue()->replaceAllUsesWith(Plan->getOrAddLiveIn(V));
+  if (match(&R, m_Not(m_VPValue(X))))
+    if (Value *V = Folder.foldNot(X))
+      R.getVPSingleValue()->replaceAllUsesWith(Plan->getOrAddLiveIn(V));
+  if (match(&R, m_LogicalAnd(m_VPValue(X), m_VPValue(Y))))
+    if (Value *V = Folder.foldLogicalAnd(X, Y))
+      R.getVPSingleValue()->replaceAllUsesWith(Plan->getOrAddLiveIn(V));
+  if (match(&R, m_Select(m_VPValue(X), m_VPValue(Y), m_VPValue(Z))))
+    if (Value *V = Folder.foldSelect(X, Y, Z))
+      R.getVPSingleValue()->replaceAllUsesWith(Plan->getOrAddLiveIn(V));
+  if (match(&R, m_GetElementPtr(m_VPValue(X), m_VPValue(Y))))
+    if (Value *V = Folder.foldPtrAdd(
+            X, Y, cast<VPRecipeWithIRFlags>(R).getGEPNoWrapFlags()))
+      R.getVPSingleValue()->replaceAllUsesWith(Plan->getOrAddLiveIn(V));
----------------
fhahn wrote:

I think it would need something like a type switch to cast to specific recipe types that have `getOpcode` and operands can be accessed by `operands()`.

https://github.com/llvm/llvm-project/pull/125365


More information about the llvm-commits mailing list