[llvm] r369519 - [InstCombine] narrow icmp with extended operands of different widths
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 21 04:56:09 PDT 2019
Author: spatel
Date: Wed Aug 21 04:56:08 2019
New Revision: 369519
URL: http://llvm.org/viewvc/llvm-project?rev=369519&view=rev
Log:
[InstCombine] narrow icmp with extended operands of different widths
An intermediate extend is used to widen the narrow operand to the width of
the other (wider) operand. At that point, we have the same logic as the
existing transform that was restricted to folds of equal width zext/sext.
This mostly solves PR42700:
https://bugs.llvm.org/show_bug.cgi?id=42700
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/trunk/test/Transforms/InstCombine/2004-11-27-SetCCForCastLargerAndConstant.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=369519&r1=369518&r2=369519&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Wed Aug 21 04:56:08 2019
@@ -4026,7 +4026,8 @@ Instruction *InstCombiner::foldICmpEqual
return nullptr;
}
-static Instruction *foldICmpWithZextOrSext(ICmpInst &ICmp) {
+static Instruction *foldICmpWithZextOrSext(ICmpInst &ICmp,
+ InstCombiner::BuilderTy &Builder) {
assert(isa<CastInst>(ICmp.getOperand(0)) && "Expected cast for operand 0");
auto *CastOp0 = cast<CastInst>(ICmp.getOperand(0));
Value *X;
@@ -4038,15 +4039,25 @@ static Instruction *foldICmpWithZextOrSe
if (auto *CastOp1 = dyn_cast<CastInst>(ICmp.getOperand(1))) {
// If the signedness of the two casts doesn't agree (i.e. one is a sext
// and the other is a zext), then we can't handle this.
+ // TODO: This is too strict. We can handle some predicates (equality?).
if (CastOp0->getOpcode() != CastOp1->getOpcode())
return nullptr;
// Not an extension from the same type?
- // TODO: Handle this by extending the narrower operand to the type of
- // the wider operand.
Value *Y = CastOp1->getOperand(0);
- if (X->getType() != Y->getType())
- return nullptr;
+ Type *XTy = X->getType(), *YTy = Y->getType();
+ if (XTy != YTy) {
+ // One of the casts must have one use because we are creating a new cast.
+ if (!CastOp0->hasOneUse() && !CastOp1->hasOneUse())
+ return nullptr;
+ // Extend the narrower operand to the type of the wider operand.
+ if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits())
+ X = Builder.CreateCast(CastOp0->getOpcode(), X, YTy);
+ else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits())
+ Y = Builder.CreateCast(CastOp0->getOpcode(), Y, XTy);
+ else
+ return nullptr;
+ }
// (zext X) == (zext Y) --> X == Y
// (sext X) == (sext Y) --> X == Y
@@ -4148,7 +4159,7 @@ Instruction *InstCombiner::foldICmpWithC
return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
}
- return foldICmpWithZextOrSext(ICmp);
+ return foldICmpWithZextOrSext(ICmp, Builder);
}
static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS) {
Modified: llvm/trunk/test/Transforms/InstCombine/2004-11-27-SetCCForCastLargerAndConstant.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/2004-11-27-SetCCForCastLargerAndConstant.ll?rev=369519&r1=369518&r2=369519&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/2004-11-27-SetCCForCastLargerAndConstant.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/2004-11-27-SetCCForCastLargerAndConstant.ll Wed Aug 21 04:56:08 2019
@@ -269,9 +269,8 @@ define i1 @gt_unsigned_to_small_negative
define i1 @different_size_zext_zext_ugt(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_zext_zext_ugt(
-; CHECK-NEXT: [[ZX:%.*]] = zext i7 [[X:%.*]] to i25
-; CHECK-NEXT: [[ZY:%.*]] = zext i4 [[Y:%.*]] to i25
-; CHECK-NEXT: [[R:%.*]] = icmp ugt i25 [[ZX]], [[ZY]]
+; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[Y:%.*]] to i7
+; CHECK-NEXT: [[R:%.*]] = icmp ult i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%zx = zext i7 %x to i25
@@ -282,9 +281,8 @@ define i1 @different_size_zext_zext_ugt(
define <2 x i1> @different_size_zext_zext_ugt_commute(<2 x i4> %x, <2 x i7> %y) {
; CHECK-LABEL: @different_size_zext_zext_ugt_commute(
-; CHECK-NEXT: [[ZX:%.*]] = zext <2 x i4> [[X:%.*]] to <2 x i25>
-; CHECK-NEXT: [[ZY:%.*]] = zext <2 x i7> [[Y:%.*]] to <2 x i25>
-; CHECK-NEXT: [[R:%.*]] = icmp ugt <2 x i25> [[ZX]], [[ZY]]
+; CHECK-NEXT: [[TMP1:%.*]] = zext <2 x i4> [[X:%.*]] to <2 x i7>
+; CHECK-NEXT: [[R:%.*]] = icmp ugt <2 x i7> [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret <2 x i1> [[R]]
;
%zx = zext <2 x i4> %x to <2 x i25>
@@ -295,9 +293,8 @@ define <2 x i1> @different_size_zext_zex
define i1 @different_size_zext_zext_ult(i4 %x, i7 %y) {
; CHECK-LABEL: @different_size_zext_zext_ult(
-; CHECK-NEXT: [[ZX:%.*]] = zext i4 [[X:%.*]] to i25
-; CHECK-NEXT: [[ZY:%.*]] = zext i7 [[Y:%.*]] to i25
-; CHECK-NEXT: [[R:%.*]] = icmp ult i25 [[ZX]], [[ZY]]
+; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[X:%.*]] to i7
+; CHECK-NEXT: [[R:%.*]] = icmp ult i7 [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%zx = zext i4 %x to i25
@@ -308,9 +305,8 @@ define i1 @different_size_zext_zext_ult(
define i1 @different_size_zext_zext_eq(i4 %x, i7 %y) {
; CHECK-LABEL: @different_size_zext_zext_eq(
-; CHECK-NEXT: [[ZX:%.*]] = zext i4 [[X:%.*]] to i25
-; CHECK-NEXT: [[ZY:%.*]] = zext i7 [[Y:%.*]] to i25
-; CHECK-NEXT: [[R:%.*]] = icmp eq i25 [[ZX]], [[ZY]]
+; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[X:%.*]] to i7
+; CHECK-NEXT: [[R:%.*]] = icmp eq i7 [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%zx = zext i4 %x to i25
@@ -321,9 +317,8 @@ define i1 @different_size_zext_zext_eq(i
define i1 @different_size_zext_zext_ne_commute(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_zext_zext_ne_commute(
-; CHECK-NEXT: [[ZX:%.*]] = zext i7 [[X:%.*]] to i25
-; CHECK-NEXT: [[ZY:%.*]] = zext i4 [[Y:%.*]] to i25
-; CHECK-NEXT: [[R:%.*]] = icmp ne i25 [[ZX]], [[ZY]]
+; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[Y:%.*]] to i7
+; CHECK-NEXT: [[R:%.*]] = icmp ne i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%zx = zext i7 %x to i25
@@ -334,9 +329,8 @@ define i1 @different_size_zext_zext_ne_c
define i1 @different_size_zext_zext_slt(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_zext_zext_slt(
-; CHECK-NEXT: [[ZX:%.*]] = zext i7 [[X:%.*]] to i25
-; CHECK-NEXT: [[ZY:%.*]] = zext i4 [[Y:%.*]] to i25
-; CHECK-NEXT: [[R:%.*]] = icmp ult i25 [[ZX]], [[ZY]]
+; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[Y:%.*]] to i7
+; CHECK-NEXT: [[R:%.*]] = icmp ugt i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%zx = zext i7 %x to i25
@@ -347,9 +341,8 @@ define i1 @different_size_zext_zext_slt(
define i1 @different_size_zext_zext_sgt(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_zext_zext_sgt(
-; CHECK-NEXT: [[ZX:%.*]] = zext i7 [[X:%.*]] to i25
-; CHECK-NEXT: [[ZY:%.*]] = zext i4 [[Y:%.*]] to i25
-; CHECK-NEXT: [[R:%.*]] = icmp ugt i25 [[ZX]], [[ZY]]
+; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[Y:%.*]] to i7
+; CHECK-NEXT: [[R:%.*]] = icmp ult i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%zx = zext i7 %x to i25
@@ -360,9 +353,8 @@ define i1 @different_size_zext_zext_sgt(
define i1 @different_size_sext_sext_sgt(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_sext_sgt(
-; CHECK-NEXT: [[SX:%.*]] = sext i7 [[X:%.*]] to i25
-; CHECK-NEXT: [[SY:%.*]] = sext i4 [[Y:%.*]] to i25
-; CHECK-NEXT: [[R:%.*]] = icmp sgt i25 [[SX]], [[SY]]
+; CHECK-NEXT: [[TMP1:%.*]] = sext i4 [[Y:%.*]] to i7
+; CHECK-NEXT: [[R:%.*]] = icmp slt i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
@@ -373,9 +365,8 @@ define i1 @different_size_sext_sext_sgt(
define i1 @different_size_sext_sext_sle(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_sext_sle(
-; CHECK-NEXT: [[SX:%.*]] = sext i7 [[X:%.*]] to i25
-; CHECK-NEXT: [[SY:%.*]] = sext i4 [[Y:%.*]] to i25
-; CHECK-NEXT: [[R:%.*]] = icmp sle i25 [[SX]], [[SY]]
+; CHECK-NEXT: [[TMP1:%.*]] = sext i4 [[Y:%.*]] to i7
+; CHECK-NEXT: [[R:%.*]] = icmp sge i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
@@ -386,9 +377,8 @@ define i1 @different_size_sext_sext_sle(
define i1 @different_size_sext_sext_eq(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_sext_eq(
-; CHECK-NEXT: [[SX:%.*]] = sext i7 [[X:%.*]] to i25
-; CHECK-NEXT: [[SY:%.*]] = sext i4 [[Y:%.*]] to i25
-; CHECK-NEXT: [[R:%.*]] = icmp eq i25 [[SX]], [[SY]]
+; CHECK-NEXT: [[TMP1:%.*]] = sext i4 [[Y:%.*]] to i7
+; CHECK-NEXT: [[R:%.*]] = icmp eq i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
@@ -399,9 +389,8 @@ define i1 @different_size_sext_sext_eq(i
define i1 @different_size_sext_sext_ule(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_sext_ule(
-; CHECK-NEXT: [[SX:%.*]] = sext i7 [[X:%.*]] to i25
-; CHECK-NEXT: [[SY:%.*]] = sext i4 [[Y:%.*]] to i25
-; CHECK-NEXT: [[R:%.*]] = icmp ule i25 [[SX]], [[SY]]
+; CHECK-NEXT: [[TMP1:%.*]] = sext i4 [[Y:%.*]] to i7
+; CHECK-NEXT: [[R:%.*]] = icmp uge i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
@@ -410,6 +399,8 @@ define i1 @different_size_sext_sext_ule(
ret i1 %r
}
+; TODO: This can be reduced.
+
define i1 @different_size_sext_zext_ne(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_zext_ne(
; CHECK-NEXT: [[SX:%.*]] = sext i7 [[X:%.*]] to i25
@@ -427,10 +418,10 @@ declare void @use(i25)
define i1 @different_size_sext_sext_ule_extra_use1(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_sext_ule_extra_use1(
-; CHECK-NEXT: [[SX:%.*]] = sext i7 [[X:%.*]] to i25
; CHECK-NEXT: [[SY:%.*]] = sext i4 [[Y:%.*]] to i25
; CHECK-NEXT: call void @use(i25 [[SY]])
-; CHECK-NEXT: [[R:%.*]] = icmp ule i25 [[SX]], [[SY]]
+; CHECK-NEXT: [[TMP1:%.*]] = sext i4 [[Y]] to i7
+; CHECK-NEXT: [[R:%.*]] = icmp uge i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
@@ -444,8 +435,8 @@ define i1 @different_size_sext_sext_ule_
; CHECK-LABEL: @different_size_sext_sext_ule_extra_use2(
; CHECK-NEXT: [[SX:%.*]] = sext i7 [[X:%.*]] to i25
; CHECK-NEXT: call void @use(i25 [[SX]])
-; CHECK-NEXT: [[SY:%.*]] = sext i4 [[Y:%.*]] to i25
-; CHECK-NEXT: [[R:%.*]] = icmp ule i25 [[SX]], [[SY]]
+; CHECK-NEXT: [[TMP1:%.*]] = sext i4 [[Y:%.*]] to i7
+; CHECK-NEXT: [[R:%.*]] = icmp uge i7 [[TMP1]], [[X]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
@@ -455,6 +446,8 @@ define i1 @different_size_sext_sext_ule_
ret i1 %r
}
+; Negative test - extra uses on both casts is too much.
+
define i1 @different_size_sext_sext_ule_extra_use3(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_sext_ule_extra_use3(
; CHECK-NEXT: [[SX:%.*]] = sext i7 [[X:%.*]] to i25
More information about the llvm-commits
mailing list