[llvm-bugs] [Bug 30906] New: missed loop invariant optimization and compare optimization

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Nov 3 14:40:46 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=30906

            Bug ID: 30906
           Summary: missed loop invariant optimization and compare
                    optimization
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: carrot at google.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20161103/f2020f66/attachment-0001.html>


More information about the llvm-bugs mailing list