[indvars] Use fact loop must exit to canonicalize to unsigned conditions
authorPhilip Reames <listmail@philipreames.com>
Fri, 22 Oct 2021 17:24:27 +0000 (10:24 -0700)
committerPhilip Reames <listmail@philipreames.com>
Fri, 22 Oct 2021 17:31:36 +0000 (10:31 -0700)
commit412eb07edd4e83afeb1e727e91213e2cbd771f29
tree97d1c52279150a745a35213e08db7ef7efa9c34b
parentd9eebe3cd7832b69471b27e68f0ee6f1374ed759
[indvars] Use fact loop must exit to canonicalize to unsigned conditions

The logic in this patch is that if we find a comparison which would be unsigned except for when the loop is infinite, and we can prove that an infinite loop must be ill defined, we can still make the predicate unsigned.

The eventual goal (combined with a follow on patch) is to use the fact the loop exits to remove the zext (see tests) entirely.

A couple of points worth noting:
* We loose the ability to prove the loop unreachable by committing to the must exit interpretation. If instead, we later proved that rhs was definitely outside the range required for finiteness, we could have killed the loop entirely. (We don't currently implement this transform, but could in theory, do so.)
* simplifyAndExtend has a very limited list of users it walks. In particular, in the examples is stops at the zext and never visits the icmp. (Because we can't fold the zext to an addrec yet in SCEV.) Being willing to visit when we haven't simplified regresses multiple tests (seemingly because of less optimal results when computing trip counts).  D112170 explores fixing that, but - at least so far - appears to be too expensive compile time wise.

Differential Revision: https://reviews.llvm.org/D111836
llvm/include/llvm/Analysis/ScalarEvolution.h
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
llvm/test/Transforms/IndVarSimplify/finite-exit-comparisons.ll