<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">On 8/15/2018 12:21 PM, Alexandre Isoard
      via llvm-dev wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CANLM5Lc30_y9s-z=_aAuvG7kanBKnAUfXqUXp1qkhPoeUJbXFg@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
      <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>
      </div>
    </blockquote>
    <br>
    The backedge-taken count isn't nuw because nsw/nuw markings aren't
    flow-sensitive: there isn't any way to mark the trip count as nuw
    without marking every computation of `(long)n-2` as nuw.<br>
    <br>
    There's some code in ScalarEvolution::howFarToZero to try to refine
    the max backedge-taken count in some cases, but it isn't very
    general.  See <a class="moz-txt-link-freetext" href="https://reviews.llvm.org/D28536">https://reviews.llvm.org/D28536</a> .<br>
    <br>
    -Eli<br>
    <pre class="moz-signature" cols="72">-- 
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project</pre>
  </body>
</html>