[llvm] [InstCombine] Try the flipped strictness of predicate in `foldICmpShlConstant` (PR #92773)
via llvm-commits
llvm-commits at lists.llvm.org
Mon May 20 10:50:13 PDT 2024
================
@@ -2414,14 +2414,37 @@ Instruction *InstCombinerImpl::foldICmpShlConstant(ICmpInst &Cmp,
// free on the target. It has the additional benefit of comparing to a
// smaller constant that may be more target-friendly.
unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1);
- if (Shl->hasOneUse() && Amt != 0 && C.countr_zero() >= Amt &&
- DL.isLegalInteger(TypeBits - Amt)) {
- Type *TruncTy = IntegerType::get(Cmp.getContext(), TypeBits - Amt);
- if (auto *ShVTy = dyn_cast<VectorType>(ShType))
- TruncTy = VectorType::get(TruncTy, ShVTy->getElementCount());
- Constant *NewC =
- ConstantInt::get(TruncTy, C.ashr(*ShiftAmt).trunc(TypeBits - Amt));
- return new ICmpInst(Pred, Builder.CreateTrunc(X, TruncTy), NewC);
+ if (Shl->hasOneUse() && Amt != 0 && DL.isLegalInteger(TypeBits - Amt)) {
+ auto FoldICmpShlToICmpTrunc = [&](ICmpInst::Predicate Pred,
+ const APInt &C) -> Instruction * {
+ if (C.countr_zero() < Amt)
+ return nullptr;
+ Type *TruncTy = ShType->getWithNewBitWidth(TypeBits - Amt);
+ Constant *NewC =
+ ConstantInt::get(TruncTy, C.ashr(*ShiftAmt).trunc(TypeBits - Amt));
+ return new ICmpInst(
+ Pred, Builder.CreateTrunc(X, TruncTy, "", Shl->hasNoSignedWrap()),
----------------
goldsteinn wrote:
> > If you don't have `nsw` and have `nuw`, you can just use `lshr` and propagate `nuw`? If you have both you have to drop either `nuw` or `nsw`. https://alive2.llvm.org/ce/z/9tjTDb
>
> Yeah, I don't know which one is better when we have both flags :(
I guess knowledge about 1 extra bit vs more knowledge about 1 less bit. For my money id stick w/ `nuw`.
> BTW, as the motivation cases (see [dtcxzyw/llvm-opt-benchmark#616](https://github.com/dtcxzyw/llvm-opt-benchmark/pull/616)) have no flags on shl, I think it is ok to just keep it simple.
IMO its not a big increase in complexity, and since we regularly have missed-optimizations related to throwing away info, think its better to just handle right from the start.
https://github.com/llvm/llvm-project/pull/92773
More information about the llvm-commits
mailing list