WIP as I still need to do further testing and re-collecting &
applying the loop guards every time can be quite expensive in terms of
compile time. But I thought it would be good to share early, in case I
missed anything.
This patch improves handling of pointer comparisons involving
subtractions, by using knowledge that the involved variables are != 0
and signed positive.
It uses applyLoopGuards to add != 0 info to the involved expressions (by
re-writing them as umax expression. It also adds a new fold to move zext
into umax expressions and to move udiv expression inside subtraction, if
the operands guarantee a positive result.
The motivating code for this is eliminating range checks for code
like below from PR48965.
#include <assert.h> void foo(const int *y, unsigned y_len) { const int *y_end = y + y_len; unsigned c = 0; for (const int *y_iter = y; y_iter != y_end; y_iter++, c++) { assert(c < y_len); } assert(c == y_len); }
Proof for zext/umax fold: https://alive2.llvm.org/ce/z/t75T53
Proof for getUDivExpr extension:a https://alive2.llvm.org/ce/z/H_G2Q0
Proof for isKnownPredicateSubIdiom: https://alive2.llvm.org/ce/z/Gfe8mS
clang-tidy: warning: 'auto *SM' can be declared as 'const auto *SM' [llvm-qualified-auto]
not useful