[llvm] [DAG] Constant fold ISD::FSHL/FSHR nodes (PR #154480)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 22 03:42:29 PDT 2025


================
@@ -7175,6 +7175,49 @@ SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL,
     }
   }
 
+  // Handle fshl/fshr special cases.
+  if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
+    auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
+    auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
+    auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
+
+    if (C1 && C2 && C3) {
+      if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
+        return SDValue();
+      const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
+                  &V3 = C3->getAPIntValue();
+
+      APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
+                                            : APIntOps::fshr(V1, V2, V3);
+
+      SDValue Folded = getConstant(FoldedVal, DL, VT);
+      assert((!Folded || !VT.isVector()) &&
+             "Can't fold vectors ops with scalar operands");
+      return Folded;
----------------
RKSimon wrote:

The assert seems redundant as I can't see how getConstant can fail or VT could not be a scalar integer.
```
return getConstant(FoldedVal, DL, VT);
```

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


More information about the llvm-commits mailing list