diff options
author | David Bolvansky <david.bolvansky@gmail.com> | 2019-06-21 16:25:32 +0000 |
---|---|---|
committer | David Bolvansky <david.bolvansky@gmail.com> | 2019-06-21 16:25:32 +0000 |
commit | dbcdad51ff8263240ab236404a2c23bb2d6c3b2e (patch) | |
tree | c38c3a78c852d1566a4deecb83a0354b1f1aa428 /llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp | |
parent | 96e77ce626a55dbf631a189a2e36e6d1404a9544 (diff) | |
download | bcm5719-llvm-dbcdad51ff8263240ab236404a2c23bb2d6c3b2e.tar.gz bcm5719-llvm-dbcdad51ff8263240ab236404a2c23bb2d6c3b2e.zip |
[InstCombine] (1 << (C - x)) -> ((1 << C) >> x) if C is bitwidth - 1
Summary:
```
%a = sub i32 31, %x
%r = shl i32 1, %a
=>
%d = shl i32 1, 31
%r = lshr i32 %d, %x
Done: 1
Optimization is correct!
```
https://rise4fun.com/Alive/btZm
Reviewers: spatel, lebedev.ri, nikic
Reviewed By: lebedev.ri
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63652
llvm-svn: 364073
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp index 21f1ff56602..655f40a0de0 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp @@ -582,6 +582,8 @@ Instruction *InstCombiner::visitShl(BinaryOperator &I) { Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); Type *Ty = I.getType(); + unsigned BitWidth = Ty->getScalarSizeInBits(); + const APInt *ShAmtAPInt; if (match(Op1, m_APInt(ShAmtAPInt))) { unsigned ShAmt = ShAmtAPInt->getZExtValue(); @@ -670,6 +672,12 @@ Instruction *InstCombiner::visitShl(BinaryOperator &I) { return BinaryOperator::CreateMul(X, ConstantExpr::getShl(C2, C1)); } + // (1 << (C - x)) -> ((1 << C) >> x) if C is bitwidth - 1 + if (match(Op0, m_One()) && + match(Op1, m_Sub(m_SpecificInt(BitWidth - 1), m_Value(X)))) + return BinaryOperator::CreateLShr( + ConstantInt::get(Ty, APInt::getSignMask(BitWidth)), X); + return nullptr; } |