Dear all,
<div><br></div><div>SCEV currently has a weakness in the loop trip count computation code when it comes to loops whose bounds are determined by a <= comparison.</div><div><br></div><div>Take the example from the attached test case:</div>


<div><br></div><div><div><font face="courier new, monospace">int Proc_8 (int arg1)</font></div><div><font face="courier new, monospace">{</font></div><div><font face="courier new, monospace">  int result = 0;</font></div>


<div><font face="courier new, monospace">  for (int i = arg1; i <= arg1 + 5; ++i)</font></div><div><font face="courier new, monospace">    result++;</font></div><div><font face="courier new, monospace">   </font></div>


<div><font face="courier new, monospace">  return result;</font></div><div><font face="courier new, monospace">}</font></div></div><div><br></div><div>GCC is able to determine the trip count exactly and translates this whole function into a single instruction (+ return) on PPC.</div>


<div><br></div><div>LLVM fails on two fronts:</div><div>- First of all, it can't deal with the <= comparison because the code to handle this isn't there. It was added in an earlier version of SCEV years ago but then reverted alongside a lot of other (buggy) code. The attached patch fixes this.</div>


<div>- Secondly, even once the patch has been applied, LLVM can only derive a smax() expression for the trip count of this loop. While this is a lot better already, it's still not an exact number. I get the impression that this has something to do with the 'nsw' flag on the original addition not being propagated through the SCEV analysis, but I'm not entirely sure here.</div>


<div><br></div><div>The patch passes all regression tests and the nightly test suite. I do have one worry about the line marked with a 'FIXME', though. Is it necessary to check for overflow here? Can this be done using the same method as in ScalarEvolution::getBECount, i.e. zero-extend to larger type, add, compare results?</div>


<div><br></div><div>Thanks for your feedback!</div><div>Tobias</div>