[llvm-bugs] [Bug 37479] New: LoopIdiomRecognize can turn infinite loops into countable loop using ctlz

via llvm-bugs llvm-bugs at lists.llvm.org
Tue May 15 16:23:23 PDT 2018


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

            Bug ID: 37479
           Summary: LoopIdiomRecognize can turn infinite loops into
                    countable loop using ctlz
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: craig.topper at gmail.com
                CC: llvm-bugs at lists.llvm.org

On targets that support ctlz, loop idiom recognize can transform code like this

int foo(int x) {
  int cnt = 0;
  while (x) {
    x >>= 1;
    ++cnt;
  }
  return cnt;
}

Into just returning (32 - ctlz(x)). Or if the loop contains other code it will
create a loop that runs (32 - ctlz(x)) times.

But if x was negative the original code would have been an infinite loop since
the shift would just keep copying the sign bit preventing x from ever being 0.


This transform was added by D32605 and the motivating case cited there looks
pretty much like above with an additional "if (x < 0) x = -x;" before the loop.

Ideally I'd like to not lose the optimization. Are there any things we can
exploit to ensure this is safe for that motivating case.

-- 
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/20180515/bc441dfe/attachment.html>


More information about the llvm-bugs mailing list