<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Unable to detect loop entry guard with ScalarEvolution"
   href="http://llvm.org/bugs/show_bug.cgi?id=15205">15205</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Unable to detect loop entry guard with ScalarEvolution
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Global Analyses
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>michele.scandale@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Using ScalarEvolution in custom pass run on the IR at the end of all standard
optimization, in the following case ScalarEvolution do not recognize that the
loop has an entry guard related to the initial value of its induction variable.

The C test case:
/////////////////////////////////////
extern void x(int);

int foo_int(int a, int b) {
  int i;
  for (i = 0; i < b/2; ++i) {
    x(i);
  }

  return 0;
}
/////////////////////////////////////

the correspondent optimized IR is:

///////////////////////////////////////////////////////////////////////////////
define i32 @foo_int(i32 %a, i32 %b) nounwind uwtable {
entry:
  %div = sdiv i32 %b, 2
  %cmp3 = icmp sgt i32 %b, 1
  br i1 %cmp3, label %for.body, label %for.end

for.body:                                         ; preds = %entry, %for.body
  %i.04 = phi i32 [ %inc, %for.body ], [ 0, %entry ]
  tail call void @x(i32 %i.04) nounwind
  %inc = add nsw i32 %i.04, 1
  %cmp = icmp slt i32 %inc, %div
  br i1 %cmp, label %for.body, label %for.end

for.end:                                          ; preds = %for.body, %entry
  ret i32 0
}
///////////////////////////////////////////////////////////////////////////////

>From the IR is possible to see that the condition in the entry block is

  %cmp3 = icmp sgt i32 %b, 1

but starting from the loop IV it should be expected something like

  %cmp3 = icmp slt i32 0, %div

and this is verified looking at the instcombine debug trace:
////////////////////////////////////////////////
IC: Visiting:   %cmp3 = icmp slt i32 0, %div
IC: Old =   %cmp3 = icmp sgt i32 %div, 0
    New =   <badref> = icmp sge i32 %b, 2
IC: ADD:   %cmp3 = icmp sge i32 %b, 2
IC: ERASE   %0 = icmp sgt i32 %div, 0
IC: ADD:   %div = sdiv i32 %b, 2
IC: Visiting:   %div = sdiv i32 %b, 2
IC: Visiting:   %cmp3 = icmp sge i32 %b, 2
IC: Old =   %cmp3 = icmp sge i32 %b, 2
    New =   <badref> = icmp sgt i32 %b, 1
IC: ADD:   %cmp3 = icmp sgt i32 %b, 1
IC: ERASE   %0 = icmp sge i32 %b, 2
////////////////////////////////////////////////

After InstCombine pass ScalarEvolution is no more able to detect the entry
guard of the loop.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>