[llvm] [SDAG] Fix type checks in `ShrinkDemandedOp` to avoid creating invalid truncates (PR #92730)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Mon May 20 06:51:19 PDT 2024


================
@@ -596,10 +596,17 @@ bool TargetLowering::ShrinkDemandedOp(SDValue Op, unsigned BitWidth,
   // Op's type. For expedience, just check power-of-2 integer types.
   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
   unsigned DemandedSize = DemandedBits.getActiveBits();
+  // Types of LHS and RHS may differ before legalization (e.g., shl), so we
+  // need to check both.
+  unsigned MinWidth =
+      std::min(Op.getOperand(0).getValueType().getScalarSizeInBits(),
+               Op.getOperand(1).getValueType().getScalarSizeInBits());
   for (unsigned SmallVTBits = llvm::bit_ceil(DemandedSize);
-       SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
+       SmallVTBits < MinWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
     EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
-    if (TLI.isTruncateFree(VT, SmallVT) && TLI.isZExtFree(SmallVT, VT)) {
+    if (TLI.isTruncateFree(Op.getOperand(0).getValueType(), SmallVT) &&
+        TLI.isTruncateFree(Op.getOperand(1).getValueType(), SmallVT) &&
----------------
dtcxzyw wrote:

See https://github.com/llvm/llvm-project/pull/92753.

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


More information about the llvm-commits mailing list