https://gcc.gnu.org/g:785a4b3e6071366e99655a8001aef04da3e1064e
commit r17-6233-g785a4b3e6071366e99655a8001aef04da3e1064e Author: Andrea Pinski <[email protected]> Date: Mon Oct 12 12:12:06 2026 -0700 ranger-op: Add op1_op2_relation for pointer_diff Pointer diff is like a signed minus which means we can add an op1_op2_relation which allows for relationships to be infered based on `a - b` values. For an example we can infer that `a < b` when `a - b` is negative. Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: * range-op-ptr.cc (class operator_pointer_diff): Add op1_op2_relation. (operator_pointer_diff::op1_op2_relation): New method. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp-cmp-sub-ptr-1.c: New test. Signed-off-by: Andrea Pinski <[email protected]> Diff: --- gcc/range-op-ptr.cc | 38 +++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/vrp-cmp-sub-ptr-1.c | 29 +++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/gcc/range-op-ptr.cc b/gcc/range-op-ptr.cc index e2c0273489bb..d58769b59d42 100644 --- a/gcc/range-op-ptr.cc +++ b/gcc/range-op-ptr.cc @@ -469,6 +469,7 @@ class operator_pointer_diff : public range_operator using range_operator::fold_range; using range_operator::update_bitmask; using range_operator::op1_op2_relation_effect; + using range_operator::op1_op2_relation; virtual bool fold_range (irange &r, tree type, const prange &op1, const prange &op2, @@ -478,6 +479,9 @@ class operator_pointer_diff : public range_operator const prange &op1_range, const prange &op2_range, relation_kind rel) const final override; + virtual relation_kind op1_op2_relation (const irange &lhs, + const prange &rhs1, + const prange &rhs2) const final override; void update_bitmask (irange &r, const prange &lh, const prange &rh) const final override { update_known_bitmask (r, POINTER_DIFF_EXPR, lh, rh); } @@ -518,6 +522,40 @@ operator_pointer_diff::op1_op2_relation_effect (irange &lhs_range, tree type, return minus_op1_op2_relation_effect (lhs_range, type, op1, op2, rel); } + +// Check if the LHS range indicates a relation between OP1 and OP2. +// OP1 - OP2 == 0 implies OP1 == OP2 even with wrapping arithmetic. +// For signed types, when both OP1 and OP2 are both positive, infer +// a relationship if there were it is overflow free. + +relation_kind +operator_pointer_diff::op1_op2_relation (const irange &lhs, const prange &rhs1, + const prange &) const +{ + if (lhs.undefined_p ()) + return VREL_UNDEFINED; + + if (lhs.zero_p ()) + return VREL_EQ; + + bool contains_z = lhs.contains_zero_p (); + // When a - b does not overflow and signed, + // we can figure out the relationship between + // a and b if the result is non-negative or non-positive. + if (TYPE_OVERFLOW_UNDEFINED (rhs1.type ())) + { + if (lhs.nonnegative_p ()) + return contains_z ? VREL_GE : VREL_GT; + if (lhs.nonpositive_p ()) + return contains_z ? VREL_LE : VREL_LT; + } + + if (!contains_z) + return VREL_NE; + + return VREL_VARYING; +} + bool operator_identity::fold_range (prange &r, tree type ATTRIBUTE_UNUSED, const prange &lh ATTRIBUTE_UNUSED, diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp-cmp-sub-ptr-1.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp-cmp-sub-ptr-1.c new file mode 100644 index 000000000000..b3341798bc1a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp-cmp-sub-ptr-1.c @@ -0,0 +1,29 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int f(int *a, int *b) +{ + __PTRDIFF_TYPE__ t = a - b; + if (t < 0) + return a < b; + return 0; +} + +int g(); + +int f1(int *a, int *b) +{ + __PTRDIFF_TYPE__ t = a - b; + if (t < 0) + { + if (a < b) + return g(); + } + return 0; +} + +/* { dg-final { scan-tree-dump-times " < " 2 "optimized" } } */ +/* Only 1 if left for f1 */ +/* { dg-final { scan-tree-dump-times "if " 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-not " != " "optimized" } } */ +/* { dg-final { scan-tree-dump-not " == " "optimized" } } */
