<html>
    <head>
      <base href="https://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 --- - missed loop invariant optimization and compare optimization"
   href="https://llvm.org/bugs/show_bug.cgi?id=30906">30906</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>missed loop invariant optimization and compare optimization
          </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>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>carrot@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Compile the following code with options -m64 -O2


struct C {
  inline bool bar(const C& c) const;
  unsigned int f;
};

bool C::bar(const C& c) const {
  return (f <= c.f) &&            // if comment out, llvm can optimize foo
      ((f ^ c.f) >> f == 0);
}

bool foo(int k, C& a, C& b) {
    int r = 1;
    for (int i = 0; i < k; ++i) {
      r += a.bar(b);
    }
    return r != 0;
}

llvm generates complex instructions to compute the loop of function foo.
Actually a.bar(b) is loop invariant, the loop can be simplified to 
r += k * a.bar(b)
And a.bar(b) returns bool value, it is 0 or 1, so the final result of r is
always greater than 0, and function foo should always return 1.

If I simplify the condition in C::bar a little by commenting out the first
line, then llvm can do the expected optimization and return 1 directly.

On the other hand, even if a.bar(b) is not loop invariant, the final value of r
is still >= 1, foo can still returns 1 directly.</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>