Skip to content

Commit f9a2bec

Browse files
committed
InstSimplify: teach simplifyICmpWithConstant about samesign
We have chosen to change ConstantRange::makeAllowedICmpRegion to respect samesign information, noting that ConstantRange::makeExactICmpRegion should not be modified.
1 parent 193c879 commit f9a2bec

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

llvm/include/llvm/IR/ConstantRange.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434
#include "llvm/ADT/APInt.h"
3535
#include "llvm/IR/InstrTypes.h"
3636
#include "llvm/IR/Instruction.h"
37-
#include "llvm/Support/Compiler.h"
3837
#include <cstdint>
3938

4039
namespace llvm {
4140

4241
class MDNode;
4342
class raw_ostream;
4443
struct KnownBits;
44+
class CmpPredicate;
4545

4646
/// This class represents a range of values.
4747
class [[nodiscard]] ConstantRange {
@@ -103,6 +103,11 @@ class [[nodiscard]] ConstantRange {
103103
static ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred,
104104
const ConstantRange &Other);
105105

106+
/// Calls makeAllowedICmpRegion with a CmpPredicate, yielding an asymmetric
107+
/// range when samesign information is present.
108+
static ConstantRange makeAsymmetricICmpRegion(CmpPredicate Pred,
109+
const ConstantRange &Other);
110+
106111
/// Produce the largest range such that all values in the returned range
107112
/// satisfy the given predicate with all values contained within Other.
108113
/// Formally, this returns a subset of

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3012,7 +3012,7 @@ static Value *simplifyICmpWithConstant(CmpPredicate Pred, Value *LHS,
30123012
}
30133013

30143014
// Rule out tautological comparisons (eg., ult 0 or uge 0).
3015-
ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C);
3015+
ConstantRange RHS_CR = ConstantRange::makeAsymmetricICmpRegion(Pred, *C);
30163016
if (RHS_CR.isEmptySet())
30173017
return ConstantInt::getFalse(ITy);
30183018
if (RHS_CR.isFullSet())

llvm/lib/IR/ConstantRange.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred,
145145
}
146146
}
147147

148+
ConstantRange ConstantRange::makeAsymmetricICmpRegion(CmpPredicate Pred,
149+
const ConstantRange &CR) {
150+
if (Pred.hasSameSign() && ICmpInst::isRelational(Pred))
151+
return makeAllowedICmpRegion(Pred, CR).unionWith(makeAllowedICmpRegion(
152+
ICmpInst::getFlippedSignednessPredicate(Pred), CR));
153+
return makeAllowedICmpRegion(Pred, CR);
154+
}
155+
148156
ConstantRange ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
149157
const ConstantRange &CR) {
150158
// Follows from De-Morgan's laws:

llvm/test/Analysis/ValueTracking/constant-ranges.ll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ define i1 @srem_posC_okay1(i8 %x) {
180180

181181
define i1 @srem_posC_okay1_samesign(i8 %x) {
182182
; CHECK-LABEL: @srem_posC_okay1_samesign(
183-
; CHECK-NEXT: ret i1 false
183+
; CHECK-NEXT: ret i1 true
184184
;
185185
%val = srem i8 34, %x
186186
%r = icmp samesign uge i8 %val, -3
@@ -198,9 +198,7 @@ define i1 @srem_negC_okay0(i8 %x) {
198198

199199
define i1 @srem_negC_okay0_samesign(i8 %x) {
200200
; CHECK-LABEL: @srem_negC_okay0_samesign(
201-
; CHECK-NEXT: [[VAL:%.*]] = srem i8 -34, [[X:%.*]]
202-
; CHECK-NEXT: [[R:%.*]] = icmp samesign ule i8 [[VAL]], 0
203-
; CHECK-NEXT: ret i1 [[R]]
201+
; CHECK-NEXT: ret i1 true
204202
;
205203
%val = srem i8 -34, %x
206204
%r = icmp samesign ule i8 %val, 0
@@ -218,9 +216,7 @@ define i1 @srem_negC_okay1(i8 %x) {
218216

219217
define i1 @srem_negC_okay1_samesign(i8 %x) {
220218
; CHECK-LABEL: @srem_negC_okay1_samesign(
221-
; CHECK-NEXT: [[VAL:%.*]] = srem i8 -34, [[X:%.*]]
222-
; CHECK-NEXT: [[R:%.*]] = icmp samesign uge i8 [[VAL]], -34
223-
; CHECK-NEXT: ret i1 [[R]]
219+
; CHECK-NEXT: ret i1 true
224220
;
225221
%val = srem i8 -34, %x
226222
%r = icmp samesign uge i8 %val, -34

0 commit comments

Comments
 (0)