[llvm] [SelectionDAG] Fold constant min/max vector reductions (PR #209190)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 14 02:35:05 PDT 2026


================
@@ -7479,6 +7483,28 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
   return V;
 }
 
+static std::optional<APInt> getIntegerIdentity(unsigned Opcode,
+                                               unsigned BitWidth) {
+  switch (Opcode) {
+  default:
+    return std::nullopt;
+  case ISD::ADD:
+  case ISD::OR:
+  case ISD::XOR:
+  case ISD::UMAX:
+    return APInt::getZero(BitWidth);
+  case ISD::MUL:
+    return APInt(BitWidth, 1);
+  case ISD::AND:
+  case ISD::UMIN:
+    return APInt::getAllOnes(BitWidth);
+  case ISD::SMAX:
+    return APInt::getSignedMinValue(BitWidth);
+  case ISD::SMIN:
+    return APInt::getSignedMaxValue(BitWidth);
----------------
lukel97 wrote:

I think this should be placed beside `SelectionDAG::getIdentityElement`, and getIdentityElement should be updated to use it. It should also probably just assert `llvm_unreachable` in the default case because APInt doesn't really have a "null" value, unlike SDValue

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


More information about the llvm-commits mailing list