summaryrefslogtreecommitdiffstats
path: root/llvm
diff options
context:
space:
mode:
Diffstat (limited to 'llvm')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp27
-rw-r--r--llvm/test/Transforms/InstCombine/2011-05-28-swapmulsub.ll3
-rw-r--r--llvm/test/Transforms/InstCombine/cast.ll2
-rw-r--r--llvm/test/Transforms/InstCombine/icmp.ll6
-rw-r--r--llvm/test/Transforms/InstCombine/trunc.ll135
5 files changed, 163 insertions, 10 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index dddcd0183b3..75562eca390 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -566,11 +566,28 @@ Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
// Transform "trunc (and X, cst)" -> "and (trunc X), cst" so long as the dest
// type isn't non-native.
if (Src->hasOneUse() && isa<IntegerType>(SrcTy) &&
- ShouldChangeType(SrcTy, DestTy) &&
- match(Src, m_And(m_Value(A), m_ConstantInt(Cst)))) {
- Value *NewTrunc = Builder->CreateTrunc(A, DestTy, A->getName() + ".tr");
- return BinaryOperator::CreateAnd(NewTrunc,
- ConstantExpr::getTrunc(Cst, DestTy));
+ ShouldChangeType(SrcTy, DestTy)) {
+
+ // Transform "trunc (and X, cst)" -> "and (trunc X), cst" so long as the dest
+ // type isn't non-native.
+ if (match(Src, m_And(m_Value(A), m_ConstantInt(Cst)))) {
+ Value *NewTrunc = Builder->CreateTrunc(A, DestTy, A->getName() + ".tr");
+ return BinaryOperator::CreateAnd(NewTrunc,
+ ConstantExpr::getTrunc(Cst, DestTy));
+ }
+
+ // Transform "trunc (shl X, cst)" -> "shl (trunc X), cst" so long as the
+ // dest type isn't non-native and cst < dest size.
+ if (match(Src, m_Shl(m_Value(A), m_ConstantInt(Cst)))) {
+ const unsigned DestSize = DestTy->getPrimitiveSizeInBits();
+ if (Cst->getValue().ult(DestSize)) {
+ Value *NewTrunc = Builder->CreateTrunc(A, DestTy, A->getName() + ".tr");
+
+ return BinaryOperator::Create(
+ Instruction::Shl, NewTrunc,
+ ConstantInt::get(DestTy, Cst->getValue().trunc(DestSize)));
+ }
+ }
}
if (Instruction *I = foldVecTruncToExtElt(CI, *this, DL))
diff --git a/llvm/test/Transforms/InstCombine/2011-05-28-swapmulsub.ll b/llvm/test/Transforms/InstCombine/2011-05-28-swapmulsub.ll
index 639b64aacba..a304d0102c4 100644
--- a/llvm/test/Transforms/InstCombine/2011-05-28-swapmulsub.ll
+++ b/llvm/test/Transforms/InstCombine/2011-05-28-swapmulsub.ll
@@ -32,7 +32,8 @@ entry:
; CHECK-NOT: mul i32
%mul = mul i32 %sub, -4
; CHECK: sub i32
-; CHECK-NEXT: shl
+; CHECK-NEXT: trunc i32
+; CHECK-NEXT: shl i16
store i32 %mul, i32* %a, align 4
%tmp2 = load i32, i32* %a, align 4
%conv = trunc i32 %tmp2 to i16
diff --git a/llvm/test/Transforms/InstCombine/cast.ll b/llvm/test/Transforms/InstCombine/cast.ll
index 3bc79cef735..2a6ffddfecb 100644
--- a/llvm/test/Transforms/InstCombine/cast.ll
+++ b/llvm/test/Transforms/InstCombine/cast.ll
@@ -987,7 +987,7 @@ define double @test73(double *%p, i128 %i) {
%lo = shl nsw i128 %i, 3
%o = trunc i128 %lo to i64
%q = bitcast double* %p to i8*
- %pp = getelementptr inbounds i8, i8* %q, i64 %o
+ %pp = getelementptr i8, i8* %q, i64 %o
%r = bitcast i8* %pp to double*
%l = load double, double* %r
ret double %l
diff --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll
index 4575e1017ff..96cdc32d1e5 100644
--- a/llvm/test/Transforms/InstCombine/icmp.ll
+++ b/llvm/test/Transforms/InstCombine/icmp.ll
@@ -794,9 +794,9 @@ define i1 @test60_addrspacecast_smaller(i8* %foo, i16 %i, i64 %j) {
define i1 @test60_addrspacecast_larger(i8 addrspace(1)* %foo, i32 %i, i16 %j) {
; CHECK-LABEL: @test60_addrspacecast_larger(
-; CHECK-NEXT: [[GEP1_IDX:%.*]] = shl nuw i32 %i, 2
-; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[GEP1_IDX]] to i16
-; CHECK-NEXT: [[TMP2:%.*]] = icmp slt i16 [[TMP1]], %j
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 %i to i16
+; CHECK-NEXT: [[SHL:%.*]] = shl i16 [[TMP1]], 2
+; CHECK-NEXT: [[TMP2:%.*]] = icmp slt i16 [[SHL]], %j
; CHECK-NEXT: ret i1 [[TMP2]]
;
%bit = addrspacecast i8 addrspace(1)* %foo to i32 addrspace(2)*
diff --git a/llvm/test/Transforms/InstCombine/trunc.ll b/llvm/test/Transforms/InstCombine/trunc.ll
index 38f6b2804d6..3c101f65011 100644
--- a/llvm/test/Transforms/InstCombine/trunc.ll
+++ b/llvm/test/Transforms/InstCombine/trunc.ll
@@ -160,3 +160,138 @@ define i32 @trunc_bitcast3(<4 x i32> %v) {
; CHECK-NEXT: ret i32 %ext
}
+; CHECK-LABEL: @trunc_shl_31_i32_i64(
+; CHECK: %val.tr = trunc i64 %val to i32
+; CHECK-NEXT: shl i32 %val.tr, 31
+define i32 @trunc_shl_31_i32_i64(i64 %val) {
+ %shl = shl i64 %val, 31
+ %trunc = trunc i64 %shl to i32
+ ret i32 %trunc
+}
+
+; CHECK-LABEL: @trunc_shl_nsw_31_i32_i64(
+; CHECK: %val.tr = trunc i64 %val to i32
+; CHECK-NEXT: shl i32 %val.tr, 31
+define i32 @trunc_shl_nsw_31_i32_i64(i64 %val) {
+ %shl = shl nsw i64 %val, 31
+ %trunc = trunc i64 %shl to i32
+ ret i32 %trunc
+}
+
+; CHECK-LABEL: @trunc_shl_nuw_31_i32_i64(
+; CHECK: %val.tr = trunc i64 %val to i32
+; CHECK-NEXT: shl i32 %val.tr, 31
+define i32 @trunc_shl_nuw_31_i32_i64(i64 %val) {
+ %shl = shl nuw i64 %val, 31
+ %trunc = trunc i64 %shl to i32
+ ret i32 %trunc
+}
+
+; CHECK-LABEL: @trunc_shl_nsw_nuw_31_i32_i64(
+; CHECK: %val.tr = trunc i64 %val to i32
+; CHECK-NEXT: shl i32 %val.tr, 31
+define i32 @trunc_shl_nsw_nuw_31_i32_i64(i64 %val) {
+ %shl = shl nsw nuw i64 %val, 31
+ %trunc = trunc i64 %shl to i32
+ ret i32 %trunc
+}
+
+; CHECK-LABEL: @trunc_shl_15_i16_i64(
+; CHECK: %val.tr = trunc i64 %val to i16
+; CHECK-NEXT: shl i16 %val.tr, 15
+define i16 @trunc_shl_15_i16_i64(i64 %val) {
+ %shl = shl i64 %val, 15
+ %trunc = trunc i64 %shl to i16
+ ret i16 %trunc
+}
+
+; CHECK-LABEL: @trunc_shl_15_i16_i32(
+; CHECK: %val.tr = trunc i32 %val to i16
+; CHECK-NEXT: shl i16 %val.tr, 15
+define i16 @trunc_shl_15_i16_i32(i32 %val) {
+ %shl = shl i32 %val, 15
+ %trunc = trunc i32 %shl to i16
+ ret i16 %trunc
+}
+
+; CHECK-LABEL: @trunc_shl_7_i8_i64(
+; CHECK: %val.tr = trunc i64 %val to i8
+; CHECK-NEXT: shl i8 %val.tr, 7
+define i8 @trunc_shl_7_i8_i64(i64 %val) {
+ %shl = shl i64 %val, 7
+ %trunc = trunc i64 %shl to i8
+ ret i8 %trunc
+}
+
+; CHECK-LABEL: @trunc_shl_1_i2_i64(
+; CHECK: shl i64 %val, 1
+; CHECK-NEXT: trunc i64 %shl to i2
+define i2 @trunc_shl_1_i2_i64(i64 %val) {
+ %shl = shl i64 %val, 1
+ %trunc = trunc i64 %shl to i2
+ ret i2 %trunc
+}
+
+; CHECK-LABEL: @trunc_shl_1_i32_i64(
+; CHECK: %val.tr = trunc i64 %val to i32
+; CHECK-NEXT: shl i32 %val.tr, 1
+define i32 @trunc_shl_1_i32_i64(i64 %val) {
+ %shl = shl i64 %val, 1
+ %trunc = trunc i64 %shl to i32
+ ret i32 %trunc
+}
+
+; CHECK-LABEL: @trunc_shl_16_i32_i64(
+; CHECK: %val.tr = trunc i64 %val to i32
+; CHECK-NEXT: shl i32 %val.tr, 16
+define i32 @trunc_shl_16_i32_i64(i64 %val) {
+ %shl = shl i64 %val, 16
+ %trunc = trunc i64 %shl to i32
+ ret i32 %trunc
+}
+
+; CHECK-LABEL: @trunc_shl_33_i32_i64(
+; CHECK: ret i32 0
+define i32 @trunc_shl_33_i32_i64(i64 %val) {
+ %shl = shl i64 %val, 33
+ %trunc = trunc i64 %shl to i32
+ ret i32 %trunc
+}
+
+; CHECK-LABEL: @trunc_shl_32_i32_i64(
+; CHECK: ret i32 0
+define i32 @trunc_shl_32_i32_i64(i64 %val) {
+ %shl = shl i64 %val, 32
+ %trunc = trunc i64 %shl to i32
+ ret i32 %trunc
+}
+
+; TODO: Should be able to handle vectors
+; CHECK-LABEL: @trunc_shl_16_v2i32_v2i64(
+; CHECK: shl <2 x i64>
+define <2 x i32> @trunc_shl_16_v2i32_v2i64(<2 x i64> %val) {
+ %shl = shl <2 x i64> %val, <i64 16, i64 16>
+ %trunc = trunc <2 x i64> %shl to <2 x i32>
+ ret <2 x i32> %trunc
+}
+
+; CHECK-LABEL: @trunc_shl_nosplat_v2i32_v2i64(
+; CHECK: shl <2 x i64>
+define <2 x i32> @trunc_shl_nosplat_v2i32_v2i64(<2 x i64> %val) {
+ %shl = shl <2 x i64> %val, <i64 15, i64 16>
+ %trunc = trunc <2 x i64> %shl to <2 x i32>
+ ret <2 x i32> %trunc
+}
+
+; CHECK-LABEL: @trunc_shl_31_i32_i64_multi_use(
+; CHECK: shl i64 %val, 31
+; CHECK-NOT: shl i32
+; CHECK: trunc i64 %shl to i32
+; CHECK-NOT: shl i32
+define void @trunc_shl_31_i32_i64_multi_use(i64 %val, i32 addrspace(1)* %ptr0, i64 addrspace(1)* %ptr1) {
+ %shl = shl i64 %val, 31
+ %trunc = trunc i64 %shl to i32
+ store volatile i32 %trunc, i32 addrspace(1)* %ptr0
+ store volatile i64 %shl, i64 addrspace(1)* %ptr1
+ ret void
+}
OpenPOWER on IntegriCloud