From e33d41315240541aa50bd61e434af8d8998b4f92 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 1 Jan 2010 18:34:40 +0000 Subject: [PATCH] implement the transform requested in PR5284 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92398 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/InstructionCombining.cpp | 13 ++++++++ test/Transforms/InstCombine/bswap-fold.ll | 31 ++++++++++++++----- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 516d72ea899..c6a8df44c8b 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -10139,6 +10139,19 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) { if (IntrinsicInst *Operand = dyn_cast(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(II->getOperand(1))) { + if (IntrinsicInst *Operand = dyn_cast(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(II->getOperand(2))) { diff --git a/test/Transforms/InstCombine/bswap-fold.ll b/test/Transforms/InstCombine/bswap-fold.ll index 3e56951c607..034c70e400c 100644 --- a/test/Transforms/InstCombine/bswap-fold.ll +++ b/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 ) ; [#uses=1] - %tmp = icmp eq i16 %tmp10, 1 ; [#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 ) ; [#uses=1] - %tmp.upgrd.1 = icmp eq i32 %tmp34, 1 ; [#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 ) ; [#uses=1] - %tmp.upgrd.2 = icmp eq i64 %tmp34, 1 ; [#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 +} -- 2.34.1