[IndVarSimplify] Wisely choose sext or zext when widening IV.
This is to fix the performance regression caused by two earlier patches: D18777 and D18867. (both were reverted).
The problem was, with those patches, some IV sexts are converted to zexts. IndVarSimplify marks the IV as signed or unsigned based on its cast users which comes first. If the IV's first cast user is a zext, it will be marked as unsigned. However, marking the IV as unsigned will cause the IV to be zero-extended during widening, if the IV also has sext users (e.g. a sext of IV for GEP), the sext cannot be removed and trunc is introduced. In general, we want to remove as many s/zexts as possible during widening.
The idea of this patch is to mark the IV as singed even if its first cast user is a zext, when
- This IV is known to be non-negative
- The first cast user, zext, has a GEP user. (GEP's offsets are treated as signed values)
Please name the name so that the intent of the bool is obvious (perhaps WasSignExtended?). Or (even better) use something other than a bool as the domain (perhaps an enum?). Also, clang-format.