<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 --- - Infinite loop with any optimization if control reaches end of non-void function" href="https://urldefense.proofpoint.com/v2/url?u=https-3A__llvm.org_bugs_show-5Fbug.cgi-3Fid-3D24080&d=AwMBaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=pF93YEPyB-J_PERP4DUZOJDzFVX5ZQ57vQk33wu0vio&m=LrglzSYsw6BhOyndYc607y-OAimvdg7CBqaH7iCwW5c&s=ECidtJdCvX4oROiRoZFOWrzgLGC7Q_xu39kzd0orvgg&e=">24080</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Infinite loop with any optimization if control reaches end of non-void function
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.4
          </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>C++
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>mkoval@cs.cmu.edu
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=14572" name="attach_14572" title="test.cpp">attachment 14572</a> <a href="attachment.cgi?id=14572&action=edit" title="test.cpp">[details]</a></span>
test.cpp

Overview:

Enabling any form of optimization (-O1, -O2, or -Os) causes a 'for' loop to
enter an infinite loop if control reaches the end of non-void function. The
loop terminates correctly, but the program crashes with 'SIGILL, Illegal
instruction', when compiled with -O0.


Steps to Reproduce:

Compile the attached program with 'clang++ -O1 -o test test.cpp' and run it
with './test'.


Actual Results (with -O1, -O2, or -Os):

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 12
// ...


Actual Results (with -O0):

i = 0
i = 1
i = 2
i = 3
Illegal instruction


Expected results:

i = 0
i = 1
i = 2
i = 3


Build Date & Platform:

I am using the latest version of Clang available in the Ubuntu Apt repository
for Ubuntu 14.04:

Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4)
Target: x86_64-pc-linux-gnu
Thread model: posix


Additional Builds and Platforms:

I was not able to replicate this behavior on OS X with the version of Clang
shipped with X Code:

Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix


Additional Information:

Adding additional statements in 'f' causes even more strange behavior. For
example, modifying the function as follows produces different behavior:

bool f()
{
  std::cout << "b" << std::endl;
  for (size_t i = 0; i < 4; ++i) {
    std::cout << "i = " << i << std::endl;
  }
  std::cout << "c" << std::endl;
}

With -O1 the program produces the following output in an infinite loop:

b
i = 0
i = 1
i = 2
i = 3
c

With -Os or -O2 it crashes with:

b
i = 0
i = 1
i = 2
i = 3
c
terminate called after throwing an instance of 'std::bad_cast'
  what():  std::bad_cast
Aborted</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>