diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2021-11-06 21:56:53 +0100 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2021-11-06 21:59:45 +0100 |
commit | 9f0194be4570469ce830e6d581d21c75650562c7 (patch) | |
tree | d7dc95df5e9846490a242eaaaa001259e1e8fd11 /llvm/lib/IR/ConstantRange.cpp | |
parent | cefc01fa65a7ebcc10cbf3c3bb2278a6a122deaf (diff) | |
download | llvm-9f0194be4570469ce830e6d581d21c75650562c7.zip llvm-9f0194be4570469ce830e6d581d21c75650562c7.tar.gz llvm-9f0194be4570469ce830e6d581d21c75650562c7.tar.bz2 |
[ConstantRange] Add getEquivalentICmp() variant with offset (NFCI)
Add a variant of getEquivalentICmp() that produces an optional
offset. This allows us to create an equivalent icmp for all ranges.
Use this in the with.overflow folding code, which was doing this
adjustment separately -- this clarifies that the fold will indeed
always apply.
Diffstat (limited to 'llvm/lib/IR/ConstantRange.cpp')
-rw-r--r-- | llvm/lib/IR/ConstantRange.cpp | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp index d37c969..b01f5cf 100644 --- a/llvm/lib/IR/ConstantRange.cpp +++ b/llvm/lib/IR/ConstantRange.cpp @@ -183,38 +183,41 @@ CmpInst::Predicate ConstantRange::getEquivalentPredWithFlippedSignedness( return CmpInst::Predicate::BAD_ICMP_PREDICATE; } -bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred, - APInt &RHS) const { - bool Success = false; - +void ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred, + APInt &RHS, APInt &Offset) const { + Offset = APInt(getBitWidth(), 0); if (isFullSet() || isEmptySet()) { Pred = isEmptySet() ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE; RHS = APInt(getBitWidth(), 0); - Success = true; } else if (auto *OnlyElt = getSingleElement()) { Pred = CmpInst::ICMP_EQ; RHS = *OnlyElt; - Success = true; } else if (auto *OnlyMissingElt = getSingleMissingElement()) { Pred = CmpInst::ICMP_NE; RHS = *OnlyMissingElt; - Success = true; } else if (getLower().isMinSignedValue() || getLower().isMinValue()) { Pred = getLower().isMinSignedValue() ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT; RHS = getUpper(); - Success = true; } else if (getUpper().isMinSignedValue() || getUpper().isMinValue()) { Pred = getUpper().isMinSignedValue() ? CmpInst::ICMP_SGE : CmpInst::ICMP_UGE; RHS = getLower(); - Success = true; + } else { + Pred = CmpInst::ICMP_ULT; + RHS = getUpper() - getLower(); + Offset = -getLower(); } - assert((!Success || ConstantRange::makeExactICmpRegion(Pred, RHS) == *this) && + assert(ConstantRange::makeExactICmpRegion(Pred, RHS) == add(Offset) && "Bad result!"); +} - return Success; +bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred, + APInt &RHS) const { + APInt Offset; + getEquivalentICmp(Pred, RHS, Offset); + return Offset.isZero(); } bool ConstantRange::icmp(CmpInst::Predicate Pred, |