[Bug tree-optimization/94356] Missed optimisation: useless multiplication generated for pointer comparison
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94356 --- Comment #6 from Marc Glisse --- (In reply to Andrew Pinski from comment #5) > Hmm, the following is worse: That looks like a separate issue. We have fold_comparison for GENERIC, and match.pd has related patterns for integers, or for pointers with ==, but not for pointers with <. Strange, I thought I had added those, possibly together with pointer_diff since the behavior is similar.
[Bug tree-optimization/94356] Missed optimisation: useless multiplication generated for pointer comparison
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94356 Andrew Pinski changed: What|Removed |Added Severity|normal |enhancement --- Comment #5 from Andrew Pinski --- Hmm, the following is worse: typedef int t[10]; int f(t *p, long long o) { t *p1 = p+o; return p < p1; }
[Bug tree-optimization/94356] Missed optimisation: useless multiplication generated for pointer comparison
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94356 Richard Biener changed: What|Removed |Added Status|UNCONFIRMED |NEW Ever confirmed|0 |1 Last reconfirmed||2020-03-30 --- Comment #4 from Richard Biener --- I also remember tackling this in pointer_int_sum at some point (also with mixed results). Keep in mind the sizetype requirement for POINTER_PLUS_EXPR which IMHO should change to either ssizetype or [s]sizetype - Bin tried the change to ssizetype with again mixed results ;)
[Bug tree-optimization/94356] Missed optimisation: useless multiplication generated for pointer comparison
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94356 --- Comment #3 from Marc Glisse --- I tried https://gcc.gnu.org/pipermail/gcc-patches/2017-May/475037.html some time ago. Having a different type for the multiplication and the offsetting introduced a lot of NOPs and caused a few regressions (from my notes, pta-ptrarith-3.c and ssa-pre-8.c were just testsuite issues, but there were others). I filed a bug or 2 about those (PR 88926 at least). Then I tried to also make the offsets signed, to be consistent, but that's a much bigger piece and I ran out of time and motivation.
[Bug tree-optimization/94356] Missed optimisation: useless multiplication generated for pointer comparison
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94356 Jakub Jelinek changed: What|Removed |Added CC||jakub at gcc dot gnu.org --- Comment #2 from Jakub Jelinek --- Given struct S { char a[37]; }; int f1 (struct S *s, int a) { struct S *sa = s + a; return s < sa; } int f2 (struct S *s, int a, int b) { struct S *sa = s + a; return sa < s + b; } int f3 (struct S *s, int a) { struct S *sa = s + a; return s == sa; } int f4 (struct S *s, int a, int b) { struct S *sa = s + a; return sa == s + b; } f3 is optimized in: /* X + Y < Y is the same as X < 0 when there is no overflow. */ ... /* For equality, this is also true with wrapping overflow. */ ... (simplify (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0)) (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)) && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)) && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3 (op @1 { build_zero_cst (TREE_TYPE (@1)); } f4 is optimized too, but not exactly sure where, partly e.g. in /* For integral types with wrapping overflow and C odd fold x * C EQ/NE y * C into x EQ/NE y. */ (for cmp (eq ne) (simplify (cmp (mult @0 INTEGER_CST@1) (mult @2 @1)) (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)) && (TREE_INT_CST_LOW (@1) & 1) != 0) (cmp @0 @2 For f1/f2 we likely only optimize the ptr + x < ptr + y into x < y at RTL time, and in any case, the multiplications are done right now in sizetype which is unsigned type.
[Bug tree-optimization/94356] Missed optimisation: useless multiplication generated for pointer comparison
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94356 --- Comment #1 from Marc Glisse --- That's because internally we use an unsigned type for offsets (including for the multiplication). There have been tries to change that...