implement the transform requested in PR5284
authorChris Lattner <sabre@nondot.org>
Fri, 1 Jan 2010 18:34:40 +0000 (18:34 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 1 Jan 2010 18:34:40 +0000 (18:34 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92398 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp
test/Transforms/InstCombine/bswap-fold.ll

index 516d72ea89967160c2efbc00a1032988ac60e81f..c6a8df44c8b977ac96a59c1b277671bbb44ead0c 100644 (file)
@@ -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))) {
index 3e56951c6077cae0bca403d9a2316ab2d29fde03..034c70e400cee15c86b29c9b01fd5c39cf5730e1 100644 (file)
@@ -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
+}