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

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 7 13:46:32 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));
----------------
artagnon wrote:

Just to clarify: what should I call tryToConstantFold with? `R->getOpcode()`, and is there some API for getting the ops in a generic fashion? I wasn't able to find this, which is why I used the recipe matchers.

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


More information about the llvm-commits mailing list