From f3dda13bd2fd08252ff5aaf84974a28d7c721f57 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Tue, 25 Oct 2016 20:11:47 +0000 Subject: [PATCH] [InstCombine] Ensure that truncated int types are legal. Fixes the FIXMEs in D25952 and rL285075. Patch by bryant! Differential Revision: https://reviews.llvm.org/D25955 llvm-svn: 285108 --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 6 ++---- llvm/test/Transforms/InstCombine/icmp-shl-nuw.ll | 9 ++++----- llvm/test/Transforms/InstCombine/icmp.ll | 6 +++--- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 9a31de4f8aae..652d2c7bef42 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -1950,9 +1950,6 @@ Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp, And, Constant::getNullValue(And->getType())); } - // FIXME: This transform can create illegal types. Use the DataLayout to - // decide when to try this? - // Transform (icmp pred iM (shl iM %v, N), C) // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N)) // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N. @@ -1960,7 +1957,8 @@ Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp, // free on the target. It has the additional benefit of comparing to a // smaller constant, which will be target friendly. unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1); - if (Shl->hasOneUse() && Amt != 0 && C->countTrailingZeros() >= Amt) { + if (Shl->hasOneUse() && Amt != 0 && C->countTrailingZeros() >= Amt && + DL.isLegalInteger(TypeBits - Amt)) { Type *TruncTy = IntegerType::get(Cmp.getContext(), TypeBits - Amt); if (X->getType()->isVectorTy()) TruncTy = VectorType::get(TruncTy, X->getType()->getVectorNumElements()); diff --git a/llvm/test/Transforms/InstCombine/icmp-shl-nuw.ll b/llvm/test/Transforms/InstCombine/icmp-shl-nuw.ll index d5a1b87565a4..4ded0a4d93b0 100644 --- a/llvm/test/Transforms/InstCombine/icmp-shl-nuw.ll +++ b/llvm/test/Transforms/InstCombine/icmp-shl-nuw.ll @@ -14,8 +14,8 @@ define i1 @icmp_ugt_32(i64) { 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: [[C:%.*]] = shl nuw i128 %0, 64 +; CHECK-NEXT: [[D:%.*]] = icmp ult i128 [[C]], 18446744073709551616 ; CHECK-NEXT: ret i1 [[D]] ; %c = shl nuw i128 %0, 64 @@ -34,11 +34,10 @@ define i1 @icmp_ugt_16(i64) { ret i1 %d } -; FIXME: InstCombine ought not to emit the potentially illegal i48. define <2 x i1> @icmp_ule_16x2(<2 x i64>) { ; CHECK-LABEL: @icmp_ule_16x2( -; CHECK-NEXT: [[TMP2:%.*]] = trunc <2 x i64> %0 to <2 x i48> -; CHECK-NEXT: [[D:%.*]] = icmp eq <2 x i48> [[TMP2]], zeroinitializer +; CHECK-NEXT: [[C:%.*]] = shl nuw <2 x i64> %0, +; CHECK-NEXT: [[D:%.*]] = icmp ult <2 x i64> [[C]], ; CHECK-NEXT: ret <2 x i1> [[D]] ; %c = shl nuw <2 x i64> %0, diff --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll index 57e02a47e55e..099aaca5f7d6 100644 --- a/llvm/test/Transforms/InstCombine/icmp.ll +++ b/llvm/test/Transforms/InstCombine/icmp.ll @@ -1208,12 +1208,12 @@ define i1 @icmp_shl16(i32 %x) { ret i1 %cmp } -; FIXME: We shouldn't be creating illegal types like i15 in InstCombine. +; D25952: Don't create illegal types like i15 in InstCombine define i1 @icmp_shl17(i32 %x) { ; CHECK-LABEL: @icmp_shl17( -; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 %x to i15 -; CHECK-NEXT: [[CMP:%.*]] = icmp slt i15 [[TMP1]], 18 +; CHECK-NEXT: [[SHL:%.*]] = shl i32 %x, 17 +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[SHL]], 2359296 ; CHECK-NEXT: ret i1 [[CMP]] ; %shl = shl i32 %x, 17