|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 | 13 | #include "InstCombineInternal.h"
|
| 14 | +#include "llvm/ADT/APInt.h" |
14 | 15 | #include "llvm/ADT/APSInt.h"
|
15 | 16 | #include "llvm/ADT/ScopeExit.h"
|
16 | 17 | #include "llvm/ADT/SetVector.h"
|
|
23 | 24 | #include "llvm/Analysis/VectorUtils.h"
|
24 | 25 | #include "llvm/IR/ConstantRange.h"
|
25 | 26 | #include "llvm/IR/DataLayout.h"
|
| 27 | +#include "llvm/IR/Instructions.h" |
26 | 28 | #include "llvm/IR/IntrinsicInst.h"
|
27 | 29 | #include "llvm/IR/PatternMatch.h"
|
28 | 30 | #include "llvm/Support/KnownBits.h"
|
@@ -7103,6 +7105,34 @@ Instruction *InstCombinerImpl::foldICmpCommutative(ICmpInst::Predicate Pred,
|
7103 | 7105 | if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this))
|
7104 | 7106 | return replaceInstUsesWith(CxtI, V);
|
7105 | 7107 |
|
| 7108 | + // Folding (X / Y) < X => X > 0 for some constant Y other than 0 or 1 |
| 7109 | + { |
| 7110 | + const APInt *Divisor; |
| 7111 | + Value *Dividend; |
| 7112 | + if (match(Op0, m_UDiv(m_Value(Dividend), m_APInt(Divisor))) && |
| 7113 | + Op1 == Dividend && Divisor->ugt(1)) { |
| 7114 | + return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Dividend, |
| 7115 | + Constant::getNullValue(Dividend->getType())); |
| 7116 | + } |
| 7117 | + |
| 7118 | + if (match(Op0, m_SDiv(m_Value(Dividend), m_APInt(Divisor))) && |
| 7119 | + Op1 == Dividend && Divisor->ugt(1) && !ICmpInst::isUnsigned(Pred)) { |
| 7120 | + return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Dividend, |
| 7121 | + Constant::getNullValue(Dividend->getType())); |
| 7122 | + } |
| 7123 | + } |
| 7124 | + |
| 7125 | + // Another case of this fold is (X >> Y) < X => X > 0 if Y != 0 |
| 7126 | + { |
| 7127 | + const APInt *Shift; |
| 7128 | + Value *V; |
| 7129 | + if (match(Op0, m_LShr(m_Value(V), m_APInt(Shift))) && Op1 == V && |
| 7130 | + Shift->ugt(0) && !ICmpInst::isUnsigned(Pred)) { |
| 7131 | + return new ICmpInst(ICmpInst::getInversePredicate(Pred), V, |
| 7132 | + Constant::getNullValue(V->getType())); |
| 7133 | + } |
| 7134 | + } |
| 7135 | + |
7106 | 7136 | return nullptr;
|
7107 | 7137 | }
|
7108 | 7138 |
|
|
0 commit comments