From 1d584029aea9c458c8772b6bbaf3acdc10cbe702 Mon Sep 17 00:00:00 2001 From: Nadav Rotem Date: Fri, 1 Feb 2013 06:45:40 +0000 Subject: [PATCH] Optimize shift lefts of a constant by a value plus constant into a single shift. llvm-svn: 174152 --- .../lib/Transforms/InstCombine/InstCombineShifts.cpp | 6 ++++++ llvm/test/Transforms/InstCombine/shift.ll | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp index 8cf76e5..f9e94f2 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp @@ -709,6 +709,12 @@ Instruction *InstCombiner::visitShl(BinaryOperator &I) { match(I.getOperand(1), m_Constant(C2))) return BinaryOperator::CreateShl(ConstantExpr::getShl(C1, C2), A); + // shl (c1 , add(y , c2)) -> (shl (shl(c1, c2)), y) + if (match(I.getOperand(0), m_Constant(C1)) && + match(I.getOperand(1), m_Add(m_Value(A), m_Constant(C2)))) { + return BinaryOperator::CreateShl(ConstantExpr::getShl(C1, C2), A); + } + return 0; } diff --git a/llvm/test/Transforms/InstCombine/shift.ll b/llvm/test/Transforms/InstCombine/shift.ll index 41f8aa9..117a906 100644 --- a/llvm/test/Transforms/InstCombine/shift.ll +++ b/llvm/test/Transforms/InstCombine/shift.ll @@ -745,3 +745,23 @@ define i32 @test62(i32 %x) { ; CHECK: @test62 ; CHECK: ashr exact i32 %x, 3 } + +; CHECK: @test63 +; CHECK: shl <4 x i32> , %B +define <4 x i32> @test63(i32 %n) { +entry: + %K = insertelement <4 x i32> undef, i32 %n, i32 0 + %B = shufflevector <4 x i32> %K, <4 x i32> undef, <4 x i32> zeroinitializer + %A = add <4 x i32> %B, + %T = shl <4 x i32> , %A + ret <4 x i32> %T +} + +; CHECK: @test64 +; CHECK: shl i32 524288, %n +define i32 @test64(i32 %n) { +entry: + %A = add i32 %n, 19 + %T = shl i32 1 , %A + ret i32 %T +} -- 2.7.4