summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2010-01-01 18:34:40 +0000
committerChris Lattner <sabre@nondot.org>2010-01-01 18:34:40 +0000
commit25c87e9cf9ee7ea73affd103a3d3fbac62384bfa (patch)
tree0caa5214fa38d59aa887be4104a0498d4b2f0b37
parent4f2486353aa83c1472582fabfd33bf3b3d5a19a7 (diff)
downloadbcm5719-llvm-25c87e9cf9ee7ea73affd103a3d3fbac62384bfa.tar.gz
bcm5719-llvm-25c87e9cf9ee7ea73affd103a3d3fbac62384bfa.zip
implement the transform requested in PR5284
llvm-svn: 92398
-rw-r--r--llvm/lib/Transforms/Scalar/InstructionCombining.cpp13
-rw-r--r--llvm/test/Transforms/InstCombine/bswap-fold.ll31
2 files changed, 37 insertions, 7 deletions
diff --git a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp
index 516d72ea899..c6a8df44c8b 100644
--- a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -10139,6 +10139,19 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(1)))
if (Operand->getIntrinsicID() == Intrinsic::bswap)
return ReplaceInstUsesWith(CI, Operand->getOperand(1));
+
+ // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
+ if (TruncInst *TI = dyn_cast<TruncInst>(II->getOperand(1))) {
+ if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(TI->getOperand(0)))
+ if (Operand->getIntrinsicID() == Intrinsic::bswap) {
+ unsigned C = Operand->getType()->getPrimitiveSizeInBits() -
+ TI->getType()->getPrimitiveSizeInBits();
+ Value *CV = ConstantInt::get(Operand->getType(), C);
+ Value *V = Builder->CreateLShr(Operand->getOperand(1), CV);
+ return new TruncInst(V, TI->getType());
+ }
+ }
+
break;
case Intrinsic::powi:
if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getOperand(2))) {
diff --git a/llvm/test/Transforms/InstCombine/bswap-fold.ll b/llvm/test/Transforms/InstCombine/bswap-fold.ll
index 3e56951c607..034c70e400c 100644
--- a/llvm/test/Transforms/InstCombine/bswap-fold.ll
+++ b/llvm/test/Transforms/InstCombine/bswap-fold.ll
@@ -1,23 +1,22 @@
-; RUN: opt < %s -instcombine -S | grep ret | count 6
; RUN: opt < %s -instcombine -S | not grep call.*bswap
define i1 @test1(i16 %tmp2) {
- %tmp10 = call i16 @llvm.bswap.i16( i16 %tmp2 ) ; <i16> [#uses=1]
- %tmp = icmp eq i16 %tmp10, 1 ; <i1> [#uses=1]
+ %tmp10 = call i16 @llvm.bswap.i16( i16 %tmp2 )
+ %tmp = icmp eq i16 %tmp10, 1
ret i1 %tmp
}
define i1 @test2(i32 %tmp) {
- %tmp34 = tail call i32 @llvm.bswap.i32( i32 %tmp ) ; <i32> [#uses=1]
- %tmp.upgrd.1 = icmp eq i32 %tmp34, 1 ; <i1> [#uses=1]
+ %tmp34 = tail call i32 @llvm.bswap.i32( i32 %tmp )
+ %tmp.upgrd.1 = icmp eq i32 %tmp34, 1
ret i1 %tmp.upgrd.1
}
declare i32 @llvm.bswap.i32(i32)
define i1 @test3(i64 %tmp) {
- %tmp34 = tail call i64 @llvm.bswap.i64( i64 %tmp ) ; <i64> [#uses=1]
- %tmp.upgrd.2 = icmp eq i64 %tmp34, 1 ; <i1> [#uses=1]
+ %tmp34 = tail call i64 @llvm.bswap.i64( i64 %tmp )
+ %tmp.upgrd.2 = icmp eq i64 %tmp34, 1
ret i1 %tmp.upgrd.2
}
@@ -50,3 +49,21 @@ entry:
ret i32 %tmp4
}
+; PR5284
+declare i64 @llvm.bswap.i64(i64)
+declare i32 @llvm.bswap.i32(i32)
+declare i16 @llvm.bswap.i16(i16)
+
+define i16 @test7(i32 %A) {
+ %B = tail call i32 @llvm.bswap.i32(i32 %A) nounwind
+ %C = trunc i32 %B to i16
+ %D = tail call i16 @llvm.bswap.i16(i16 %C) nounwind
+ ret i16 %D
+}
+
+define i16 @test8(i64 %A) {
+ %B = tail call i64 @llvm.bswap.i64(i64 %A) nounwind
+ %C = trunc i64 %B to i16
+ %D = tail call i16 @llvm.bswap.i16(i16 %C) nounwind
+ ret i16 %D
+}
OpenPOWER on IntegriCloud