diff options
| author | Roman Lebedev <lebedev.ri@gmail.com> | 2018-09-13 20:33:12 +0000 |
|---|---|---|
| committer | Roman Lebedev <lebedev.ri@gmail.com> | 2018-09-13 20:33:12 +0000 |
| commit | 6dc87004fab4b34076e6f0f8505316e40b9ea2fb (patch) | |
| tree | 006a78021884c24eae7db013688b12e3786d5d46 /llvm/lib/Transforms | |
| parent | d565b0f017e216aa5693484249628d03e98e8d27 (diff) | |
| download | bcm5719-llvm-6dc87004fab4b34076e6f0f8505316e40b9ea2fb.tar.gz bcm5719-llvm-6dc87004fab4b34076e6f0f8505316e40b9ea2fb.zip | |
[InstCombine] Inefficient pattern for high-bits checking 2 (PR38708)
Summary:
It is sometimes important to check that some newly-computed value
is non-negative and only n bits wide (where n is a variable.)
There are many ways to check that:
https://godbolt.org/z/o4RB8D
The last variant seems best?
(I'm sure there are some other variations i haven't thought of..)
More complicated, canonical pattern:
https://rise4fun.com/Alive/uhA
We do need to have two `switch()`'es like this,
to not mismatch the swappable predicates.
https://bugs.llvm.org/show_bug.cgi?id=38708
Reviewers: spatel, craig.topper, RKSimon
Reviewed By: spatel
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D52001
llvm-svn: 342173
Diffstat (limited to 'llvm/lib/Transforms')
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 55 |
1 files changed, 36 insertions, 19 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index d0673d952d0..a295f65f387 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -4624,34 +4624,51 @@ static Instruction *canonicalizeICmpBool(ICmpInst &I, } // Transform pattern like: -// (1 << Y) u<= X -// (1 << Y) u> X +// (1 << Y) u<= X or ~(-1 << Y) u< X +// (1 << Y) u> X or ~(-1 << Y) u>= X // Into: // (X l>> Y) != 0 // (X l>> Y) == 0 static Instruction *foldICmpWithHighBitMask(ICmpInst &Cmp, InstCombiner::BuilderTy &Builder) { - ICmpInst::Predicate Pred; + ICmpInst::Predicate Pred, NewPred; Value *X, *Y; - if (!match(&Cmp, - m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) - return nullptr; + if (match(&Cmp, + m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) { + // We want X to be the icmp's second operand, so swap predicate if it isn't. + if (Cmp.getOperand(0) == X) + Pred = Cmp.getSwappedPredicate(); - // We want X to be the icmp's second operand, so swap predicate if it is not. - if (Cmp.getOperand(0) == X) - Pred = Cmp.getSwappedPredicate(); + switch (Pred) { + case ICmpInst::ICMP_ULE: + NewPred = ICmpInst::ICMP_NE; + break; + case ICmpInst::ICMP_UGT: + NewPred = ICmpInst::ICMP_EQ; + break; + default: + return nullptr; + } + } else if (match(&Cmp, + m_c_ICmp(Pred, + m_OneUse(m_Not(m_Shl(m_AllOnes(), m_Value(Y)))), + m_Value(X)))) { + // We want X to be the icmp's second operand, so swap predicate if it isn't. + if (Cmp.getOperand(0) == X) + Pred = Cmp.getSwappedPredicate(); - ICmpInst::Predicate NewPred; - switch (Pred) { - case ICmpInst::ICMP_ULE: - NewPred = ICmpInst::ICMP_NE; - break; - case ICmpInst::ICMP_UGT: - NewPred = ICmpInst::ICMP_EQ; - break; - default: + switch (Pred) { + case ICmpInst::ICMP_ULT: + NewPred = ICmpInst::ICMP_NE; + break; + case ICmpInst::ICMP_UGE: + NewPred = ICmpInst::ICMP_EQ; + break; + default: + return nullptr; + } + } else return nullptr; - } Value *NewX = Builder.CreateLShr(X, Y, X->getName() + ".highbits"); Constant *Zero = Constant::getNullValue(NewX->getType()); |

