[llvm] [DAGCombine] Propagate truncate to operands (PR #98666)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue May 6 06:17:20 PDT 2025


================
@@ -15869,6 +15878,28 @@ SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
     break;
   }
 
+  if (!LegalOperations || TLI.isOperationLegal(N0.getOpcode(), VT)) {
+    switch (N0.getOpcode()) {
+    case ISD::ADD:
+    case ISD::SUB:
+    case ISD::MUL:
+    case ISD::AND:
+    case ISD::OR:
+    case ISD::XOR:
+      if (!N0.hasOneUse() || !VT.isScalarInteger())
+        break;
+      if (!TLI.isNarrowingProfitable(N0.getNode(), SrcVT, VT))
+        break;
+      SDValue NarrowL = DAG.getNode(ISD::TRUNCATE, DL, VT, N0.getOperand(0));
+      SDValue NarrowR = DAG.getNode(ISD::TRUNCATE, DL, VT, N0.getOperand(1));
+      SDValue TruncatedOp =
+          DAG.getNode(N0.getOpcode(), DL, VT, NarrowL, NarrowR);
+      if (TLI.IsDesirableToPromoteOp(TruncatedOp, SrcVT))
+        break;
----------------
arsenm wrote:

Needing to speculatively create some nodes in case it's profitable is unfortunate, is there a way to avoid this

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


More information about the llvm-commits mailing list