[PATCH] D150110: [TargetLowering]: Fix unnecessary call to `computeKnownBits`

Dhruv Chawla via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 8 04:24:04 PDT 2023


0xdc03 created this revision.
Herald added subscribers: foad, hiraditya.
Herald added a project: All.
0xdc03 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

In the `SimplifyDemandedBits` function, there is a fallthrough to the
default case in the case of `ISD::ADD`, `ISD::MUL` and `ISD::SUB`. This
leads to a call to `computeKnownBits` which is unnecessary as the
calls to `SimplifyDemandedBits` in the cases themselves handle the
calculation of the known bits. This information is discarded through the
`Known2` variables.

By keeping this information around and calling
`KnownBits::mul` or `KnownBits::computeForAddSub` directly, the
unnecessary computation can be avoided. For now, the NSW bit is not
passed through to `KnownBits` as this is something that
`computeKnownBits` does not handle either. This requires updating
`computeForAddCarry` to handle the flag as well.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150110

Files:
  llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp


Index: llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -2592,9 +2592,10 @@
     SDNodeFlags Flags = Op.getNode()->getFlags();
     unsigned DemandedBitsLZ = DemandedBits.countl_zero();
     APInt LoMask = APInt::getLowBitsSet(BitWidth, BitWidth - DemandedBitsLZ);
-    if (SimplifyDemandedBits(Op0, LoMask, DemandedElts, Known2, TLO,
+    KnownBits KnownOp0, KnownOp1;
+    if (SimplifyDemandedBits(Op0, LoMask, DemandedElts, KnownOp0, TLO,
                              Depth + 1) ||
-        SimplifyDemandedBits(Op1, LoMask, DemandedElts, Known2, TLO,
+        SimplifyDemandedBits(Op1, LoMask, DemandedElts, KnownOp1, TLO,
                              Depth + 1) ||
         // See if the operation should be performed at a smaller bit width.
         ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO)) {
@@ -2691,7 +2692,16 @@
       }
     }
 
-    [[fallthrough]];
+    if (Op.getOpcode() == ISD::MUL) {
+      Known = KnownBits::mul(KnownOp0, KnownOp1);
+    } else { // Op.getOpcode() is either ISD::ADD or ISD::SUB.
+      // TODO: Update `computeForAddCarry` to handle the NSW flag as well so
+      //       that `Flags.hasNoSignedWrap()` can be passed through here
+      //       instead of false.
+      Known = KnownBits::computeForAddSub(Op.getOpcode() == ISD::ADD,
+              false, KnownOp0, KnownOp1);
+    }
+    break;
   }
   default:
     // We also ask the target about intrinsics (which could be specific to it).


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150110.520327.patch
Type: text/x-patch
Size: 1622 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230508/baa15b9e/attachment.bin>


More information about the llvm-commits mailing list