[llvm-bugs] [Bug 31217] New: Infinite Empty Loops are incorrectly optimized out (C)

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Nov 30 10:31:03 PST 2016


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

            Bug ID: 31217
           Summary: Infinite Empty Loops are incorrectly optimized out (C)
           Product: clang
           Version: 3.8
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: npmazzuca at gmail.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

```C
void foo() { while (1) { } }

void create_null() {
        foo();

        int i = 0;
        while (i < 100) { i += 1; }
}

__attribute__((noreturn))
void use_null() {
        __builtin_unreachable();
}


int main() {
        create_null();
        use_null();
}
```

This crashes under optimization level 2; this is invalid behavior under the C11
standard:

```
An iteration statement whose controlling expression is not a constant
expression, [note 156] that performs no  input/output  operations,
does  not  access  volatile  objects,  and  performs  no synchronization or
atomic operations in its body, controlling expression, or (in the case of
a for statement) its expression-3, may be   assumed   by   the
implementation to terminate. [note 157]

156: An omitted controlling expression is replaced by a nonzero constant,
     which is a constant expression.
157: This  is  intended  to  allow  compiler  transformations  such  as
     removal  of  empty  loops  even  when termination cannot be proven. 
```

Note "whose controlling expression is not a constant expression" - in `while
(1) { }`, `1` is a constant expression, and thus the loop may not be removed.

-- 
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/20161130/83a6d106/attachment.html>


More information about the llvm-bugs mailing list