On Fri, Nov 27, 2015 at 12:44 PM, Bin Cheng <bin.ch...@arm.com> wrote: > Hi, > This patch is to fix PR68529. In my previous scev/niter overflow patches, I > only computed no-overflow information for control iv in simple loops with > LT_EXPR as exit condition code. This bug is about loop with NE_EXPR as exit > condition code. Given below example: > > #include <stdio.h> > #include <stdlib.h> > > int main(){ > char c[10000]={}; > unsigned int nchar=9999; > > while(nchar--!=0){ > c[nchar]='A'; > } > > printf("%s\n",c); > return 0; > } > nchar used as an index to array 'c' doesn't overflow during loop iterations. > Thus &c[nchar] acts as a scev. GCC now fails to do that. With this patch, > this issue is fixed. > > Furthermore, the computation of no-overflow information could be improved by > using TREE_OVERFLOW_UNDEFINED semantic of signed type for C/C++. I didn't > do that because: > 1) I doubt how useful it could be because I have already changed scev to use > the semantic whenever possible. It doesn't need loop niter analysis' help. > 2) To do that, I need to expose chrec_convert_aggressive information out of > scev in function simple_iv, because that function could corrupt > TREE_OVERFLOW_UNDEFINED semantic assumption. This isn't appropriate for > Stage3. > > Bootstrap and test on x86_64 and x86. I don't expect any issue on aarch64 > either. Is it OK?
+ if (integer_onep (e) + && (integer_onep (s) + || (TREE_CODE (c) == INTEGER_CST + && TREE_CODE (s) == INTEGER_CST + && wi::mod_trunc (c, s, TYPE_SIGN (type)) == 0))) the only thing I'm looking at here is the modulo sign. Considering we're looking at the sign bit of the step to normalize 'c' and 's' what happens for for (unsigned int i = 0; i != 1000; --i) ? I suppose we get s == 1 and c == -1000U and you'll say the control IV doesn't wrap. Similar for i -= 2 where even when we use a signed modulo (singed)-1000U % 2 is still 0. So I think you need to remember whether we consider the step to be negative and compare iv->base and final as well. Bonus points for a wrong-code testcase with the above. I'd also like to see a testcase exercising step != 1. Thanks, Richard. > 2015-11-27 Bin Cheng <bin.ch...@arm.com> > > PR tree-optimization/68529 > * tree-ssa-loop-niter.c (number_of_iterations_ne): Add new param. > Compute no-overflow information for control iv. > (number_of_iterations_lt, number_of_iterations_le): Add new param. > (number_of_iterations_cond): Pass new argument to above functions. > > 2015-11-27 Bin Cheng <bin.ch...@arm.com> > > PR tree-optimization/68529 > * gcc.dg/tree-ssa/pr68529-1.c: New test. > * gcc.dg/tree-ssa/pr68529-2.c: New test. > * gcc.dg/tree-ssa/pr68529-3.c: New test. >