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

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon May 20 05:34:16 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) &&
----------------
arsenm wrote:

I'm not sure treating this as the minimum makes sense in the shift case. The RHS type is only an accessory to the main type. It would probably make more sense to stop treating shift as a uniform binary operator, and check the preferred shift amount type for the main type 

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


More information about the llvm-commits mailing list