Skip to content

Commit f4bfa4e

Browse files
committed
VT: teach isImpliedCondOperands about samesign
isImpliedCondICmps() and its callers in ValueTracking can greatly benefit from being taught about samesign. As a first step, teach one caller, namely isImpliedCondOperands(). Very minimal changes are required for this, as CmpPredicate::getMatching() does most of the work.
1 parent 6529c3d commit f4bfa4e

File tree

2 files changed

+17
-35
lines changed

2 files changed

+17
-35
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9223,8 +9223,8 @@ static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
92239223
/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
92249224
/// ALHS ARHS" is true. Otherwise, return std::nullopt.
92259225
static std::optional<bool>
9226-
isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS,
9227-
const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9226+
isImpliedCondOperands(CmpPredicate Pred, const Value *ALHS, const Value *ARHS,
9227+
const Value *BLHS, const Value *BRHS) {
92289228
switch (Pred) {
92299229
default:
92309230
return std::nullopt;
@@ -9293,36 +9293,34 @@ static std::optional<bool> isImpliedCondCommonOperandWithCR(
92939293
/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
92949294
/// is true. Return false if LHS implies RHS is false. Otherwise, return
92959295
/// std::nullopt if we can't infer anything.
9296-
static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
9297-
CmpInst::Predicate RPred,
9298-
const Value *R0, const Value *R1,
9299-
const DataLayout &DL,
9300-
bool LHSIsTrue) {
9296+
static std::optional<bool>
9297+
isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0,
9298+
const Value *R1, const DataLayout &DL, bool LHSIsTrue) {
93019299
Value *L0 = LHS->getOperand(0);
93029300
Value *L1 = LHS->getOperand(1);
93039301

93049302
// The rest of the logic assumes the LHS condition is true. If that's not the
93059303
// case, invert the predicate to make it so.
9306-
CmpInst::Predicate LPred =
9307-
LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
9304+
CmpPredicate LPred =
9305+
LHSIsTrue ? LHS->getCmpPredicate() : LHS->getInverseCmpPredicate();
93089306

93099307
// We can have non-canonical operands, so try to normalize any common operand
93109308
// to L0/R0.
93119309
if (L0 == R1) {
93129310
std::swap(R0, R1);
9313-
RPred = ICmpInst::getSwappedPredicate(RPred);
9311+
RPred = ICmpInst::getSwappedCmpPredicate(RPred);
93149312
}
93159313
if (R0 == L1) {
93169314
std::swap(L0, L1);
9317-
LPred = ICmpInst::getSwappedPredicate(LPred);
9315+
LPred = ICmpInst::getSwappedCmpPredicate(LPred);
93189316
}
93199317
if (L1 == R1) {
93209318
// If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
93219319
if (L0 != R0 || match(L0, m_ImmConstant())) {
93229320
std::swap(L0, L1);
9323-
LPred = ICmpInst::getSwappedPredicate(LPred);
9321+
LPred = ICmpInst::getSwappedCmpPredicate(LPred);
93249322
std::swap(R0, R1);
9325-
RPred = ICmpInst::getSwappedPredicate(RPred);
9323+
RPred = ICmpInst::getSwappedCmpPredicate(RPred);
93269324
}
93279325
}
93289326

@@ -9381,8 +9379,8 @@ static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
93819379
match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
93829380
return CmpPredicate::getMatching(LPred, RPred).has_value();
93839381

9384-
if (LPred == RPred)
9385-
return isImpliedCondOperands(LPred, L0, L1, R0, R1);
9382+
if (auto P = CmpPredicate::getMatching(LPred, RPred))
9383+
return isImpliedCondOperands(*P, L0, L1, R0, R1);
93869384

93879385
return std::nullopt;
93889386
}

llvm/test/Analysis/ValueTracking/implied-condition-samesign.ll

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
define i1 @incr_sle(i32 %i, i32 %len) {
55
; CHECK-LABEL: define i1 @incr_sle(
66
; CHECK-SAME: i32 [[I:%.*]], i32 [[LEN:%.*]]) {
7-
; CHECK-NEXT: [[I_INCR:%.*]] = add nuw nsw i32 [[I]], 1
8-
; CHECK-NEXT: [[I_GT_LEN:%.*]] = icmp samesign ugt i32 [[I]], [[LEN]]
9-
; CHECK-NEXT: [[I_INCR_SGT_LEN:%.*]] = icmp sgt i32 [[I_INCR]], [[LEN]]
10-
; CHECK-NEXT: [[RES:%.*]] = icmp sle i1 [[I_INCR_SGT_LEN]], [[I_GT_LEN]]
11-
; CHECK-NEXT: ret i1 [[RES]]
7+
; CHECK-NEXT: ret i1 true
128
;
139
%i.incr = add nsw nuw i32 %i, 1
1410
%i.gt.len = icmp samesign ugt i32 %i, %len
@@ -20,11 +16,7 @@ define i1 @incr_sle(i32 %i, i32 %len) {
2016
define i1 @incr_sge(i32 %i, i32 %len) {
2117
; CHECK-LABEL: define i1 @incr_sge(
2218
; CHECK-SAME: i32 [[I:%.*]], i32 [[LEN:%.*]]) {
23-
; CHECK-NEXT: [[I_INCR:%.*]] = add nuw nsw i32 [[I]], 1
24-
; CHECK-NEXT: [[I_LT_LEN:%.*]] = icmp samesign ult i32 [[I]], [[LEN]]
25-
; CHECK-NEXT: [[I_INCR_SLT_LEN:%.*]] = icmp slt i32 [[I_INCR]], [[LEN]]
26-
; CHECK-NEXT: [[RES:%.*]] = icmp sge i1 [[I_INCR_SLT_LEN]], [[I_LT_LEN]]
27-
; CHECK-NEXT: ret i1 [[RES]]
19+
; CHECK-NEXT: ret i1 true
2820
;
2921
%i.incr = add nsw nuw i32 %i, 1
3022
%i.lt.len = icmp samesign ult i32 %i, %len
@@ -36,11 +28,7 @@ define i1 @incr_sge(i32 %i, i32 %len) {
3628
define i1 @incr_ule(i32 %i, i32 %len) {
3729
; CHECK-LABEL: define i1 @incr_ule(
3830
; CHECK-SAME: i32 [[I:%.*]], i32 [[LEN:%.*]]) {
39-
; CHECK-NEXT: [[I_INCR:%.*]] = add nuw nsw i32 [[I]], 1
40-
; CHECK-NEXT: [[I_GT_LEN:%.*]] = icmp samesign ugt i32 [[I]], [[LEN]]
41-
; CHECK-NEXT: [[I_INCR_SGT_LEN:%.*]] = icmp sgt i32 [[I_INCR]], [[LEN]]
42-
; CHECK-NEXT: [[RES:%.*]] = icmp ule i1 [[I_GT_LEN]], [[I_INCR_SGT_LEN]]
43-
; CHECK-NEXT: ret i1 [[RES]]
31+
; CHECK-NEXT: ret i1 true
4432
;
4533
%i.incr = add nsw nuw i32 %i, 1
4634
%i.gt.len = icmp samesign ugt i32 %i, %len
@@ -52,11 +40,7 @@ define i1 @incr_ule(i32 %i, i32 %len) {
5240
define i1 @incr_uge(i32 %i, i32 %len) {
5341
; CHECK-LABEL: define i1 @incr_uge(
5442
; CHECK-SAME: i32 [[I:%.*]], i32 [[LEN:%.*]]) {
55-
; CHECK-NEXT: [[I_INCR:%.*]] = add nuw nsw i32 [[I]], 1
56-
; CHECK-NEXT: [[I_LT_LEN:%.*]] = icmp samesign ult i32 [[I]], [[LEN]]
57-
; CHECK-NEXT: [[I_INCR_SLT_LEN:%.*]] = icmp slt i32 [[I_INCR]], [[LEN]]
58-
; CHECK-NEXT: [[RES:%.*]] = icmp uge i1 [[I_LT_LEN]], [[I_INCR_SLT_LEN]]
59-
; CHECK-NEXT: ret i1 [[RES]]
43+
; CHECK-NEXT: ret i1 true
6044
;
6145
%i.incr = add nsw nuw i32 %i, 1
6246
%i.lt.len = icmp samesign ult i32 %i, %len

0 commit comments

Comments
 (0)