<div dir="ltr">Hello,<div><br></div><div>If I run clang on the following code:</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">void func(unsigned n) {<br>  for (unsigned long x = 1; x < n; ++x)<br>    dummy(x);<br>}</blockquote><div><br></div><div>I get the following llvm ir:</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">define void @func(i32 %n) {<br>entry:<br>  %conv = zext i32 %n to i64<br>  %cmp5 = icmp ugt i32 %n, 1<br>  br i1 %cmp5, label %for.body, label %for.cond.cleanup<br>for.cond.cleanup:                                 ; preds = %for.body, %entry<br>  ret void<br>for.body:                                         ; preds = %entry, %for.body<br>  %x.06 = phi i64 [ %inc, %for.body ], [ 1, %entry ]<br>  tail call void @dummy(i64 %x.06) #2<br>  %inc = add nuw nsw i64 %x.06, 1<br>  %exitcond = icmp eq i64 %inc, %conv<br>  br i1 %exitcond, label %for.cond.cleanup, label %for.body<br>}</blockquote></div><div><br></div><div>Over which, SCEV will provide the following analysis:</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Printing analysis 'Scalar Evolution Analysis' for function 'func':<br>Classifying expressions for: @func<br>  %conv = zext i32 %n to i64<br>  -->  (zext i32 %n to i64) U: [0,4294967296) S: [0,4294967296)<br>  %x.06 = phi i64 [ %inc, %for.body ], [ 1, %entry ]<br>  -->  {1,+,1}<nuw><nsw><%for.body> U: [1,-9223372036854775808) S: [1,-9223372036854775808)<span style="white-space:pre">          </span>Exits: (-1 + (zext i32 %n to i64))<span style="white-space:pre">           </span>LoopDispositions: { %for.body: Computable }<br>  %inc = add nuw nsw i64 %x.06, 1<br>  -->  {2,+,1}<nuw><%for.body> U: [2,0) S: [2,0)<span style="white-space:pre">           </span>Exits: (zext i32 %n to i64)<span style="white-space:pre">          </span>LoopDispositions: { %for.body: Computable }<br>Determining loop execution counts for: @func<br>Loop %for.body: backedge-taken count is (-2 + (zext i32 %n to i64))<nsw><br>Loop %for.body: max backedge-taken count is -2<br>Loop %for.body: Predicated backedge-taken count is (-2 + (zext i32 %n to i64))<nsw><br> Predicates:<br>Loop %for.body: Trip multiple is 1</blockquote></div><div><br></div><div>Now, I was surprised by the max backedge-taken count being -2, and I suspect it is due to the backedge-taken count being marked as <nsw> instead of <nuw>.</div><div><br></div><div>Is that on purpose, is that a bug, or is my analysis incorrect? I am not sure where to fix that issue.</div><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><b>Alexandre Isoard</b><br></div></div></div></div>