Index: lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCompares.cpp +++ lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -1966,6 +1966,17 @@ return new ICmpInst(Pred, Builder->CreateTrunc(X, TruncTy), NewC); } + // When the shift is nuw and pred is >u or <=u, comparison only really happens + // in the pre-shifted bits. + if (Shl->hasNoUnsignedWrap() && + (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) { + Type *CTy = IntegerType::get(Cmp.getContext(), C->getBitWidth()); + if (X->getType()->isVectorTy()) + CTy = VectorType::get(CTy, X->getType()->getVectorNumElements()); + Value *NewC = ConstantInt::get(CTy, C->lshr(*ShiftAmt)); + return new ICmpInst(Pred, X, NewC); + } + return nullptr; } Index: test/Transforms/InstCombine/icmp-shl-zext-simplify.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/icmp-shl-zext-simplify.ll @@ -0,0 +1,44 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt %s -instcombine -S | FileCheck %s + +define i1 @icmp_ugt_32(i64) { +; CHECK-LABEL: @icmp_ugt_32( +; CHECK-NEXT: [[D:%.*]] = icmp ne i64 %0, 0 +; CHECK-NEXT: ret i1 [[D]] +; + %c = shl nuw i64 %0, 32 + %d = icmp ugt i64 %c, 4294967295 + ret i1 %d +} + +define i1 @icmp_ule_64(i128) { +; CHECK-LABEL: @icmp_ule_64( +; CHECK-NEXT: [[TMP2:%.*]] = trunc i128 %0 to i64 +; CHECK-NEXT: [[D:%.*]] = icmp eq i64 [[TMP2]], 0 +; CHECK-NEXT: ret i1 [[D]] +; + %c = shl nuw i128 %0, 64 + %d = icmp ule i128 %c, 18446744073709551615 + ret i1 %d +} + +define i1 @icmp_ugt_16(i64) { +; CHECK-LABEL: @icmp_ugt_16( +; CHECK-NEXT: [[D:%.*]] = icmp ugt i64 %0, 15 +; CHECK-NEXT: ret i1 [[D]] +; + %c = shl nuw i64 %0, 16 + %d = icmp ugt i64 %c, 1048575 ; 0x0f_ffff + ret i1 %d +} + +define <2 x i1> @icmp_ule_i64x2(<2 x i64>) { +; CHECK-LABEL: @icmp_ule_i64x2( +; CHECK-NEXT: [[TMP2:%.*]] = trunc <2 x i64> %0 to <2 x i48> +; CHECK-NEXT: [[D:%.*]] = icmp eq <2 x i48> [[TMP2]], zeroinitializer +; CHECK-NEXT: ret <2 x i1> [[D]] +; + %c = shl nuw <2 x i64> %0, + %d = icmp ule <2 x i64> %c, + ret <2 x i1> %d +}