Index: llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp =================================================================== --- llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp +++ llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp @@ -5248,6 +5248,10 @@ LLT ShTy = MRI.getType(Z); unsigned BW = Ty.getScalarSizeInBits(); + + if (!isPowerOf2_32(BW)) + return UnableToLegalize; + const bool IsFSHL = MI.getOpcode() == TargetOpcode::G_FSHL; unsigned RevOpcode = IsFSHL ? TargetOpcode::G_FSHR : TargetOpcode::G_FSHL; @@ -5346,9 +5350,16 @@ bool IsFSHL = MI.getOpcode() == TargetOpcode::G_FSHL; unsigned RevOpcode = IsFSHL ? TargetOpcode::G_FSHR : TargetOpcode::G_FSHL; + + // TODO: Use smarter heuristic that accounts for vector legalization. if (LI.getAction({RevOpcode, {Ty, ShTy}}).Action == Lower) return lowerFunnelShiftAsShifts(MI); - return lowerFunnelShiftWithInverse(MI); + + // This only works for powers of 2, fallback to shifts if it fails. + LegalizerHelper::LegalizeResult Result = lowerFunnelShiftWithInverse(MI); + if (Result == UnableToLegalize) + return lowerFunnelShiftAsShifts(MI); + return Result; } // Expand s32 = G_UITOFP s64 using bit operations to an IEEE float