<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 --- - llc generates wrong code for x86 with -march=atom"
   href="https://llvm.org/bugs/show_bug.cgi?id=26876">26876</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>llc generates wrong code for x86 with -march=atom
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.7
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>arne.hasselbring@googlemail.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>Created <span class=""><a href="attachment.cgi?id=16004" name="attach_16004" title="A minimal source code example that compiles to bad code">attachment 16004</a> <a href="attachment.cgi?id=16004&action=edit" title="A minimal source code example that compiles to bad code">[details]</a></span>
A minimal source code example that compiles to bad code

Compiling the attached file with the following command line gives a program
that crashes when returning from foo because of improper stack operations.

Compilation command line:
$ clang++ -std=c++14 -m32 -march=atom -o prog main.cpp

Executing prog results in
$ ./prog
Before foo()
Segmentation fault

This seems to happen if std::chrono::system_clock::now() is the last operation
in a function. It doesn't seem to matter whether the result is written into a
variable or not. Initially the problem occured when initializing the last
member variable of a class with now(). For this minimal example, I had to add
an __attribute__((noinline)) to foo() to get the error, but it is exactly the
same as in the original code.

I traced this down to an error in the generated assembly code. foo is compiled
to the following code:

 8048780:       55                      push   %ebp
 8048781:       89 e5                   mov    %esp,%ebp
 8048783:       8d 64 24 e8             lea    -0x18(%esp),%esp
 8048787:       8d 45 f8                lea    -0x8(%ebp),%eax
 804878a:       89 04 24                mov    %eax,(%esp)
 804878d:       e8 3e fe ff ff          call   80485d0
<_ZNSt6chrono3_V212system_clock3nowEv@plt>
 8048792:       8d 64 24 19             lea    0x19(%esp),%esp
 8048796:       5d                      pop    %ebp
 8048797:       c3                      ret    

You can see that in the beginning, 0x18 is subtracted from %esp and in the end,
0x19 is added. std::chrono::system_clock::now() adds another 4 bytes to %esp
(by returning with ret 0x04, I suspect this has to do with some ABI
callee-cleanup things), so in the end, the stack pointer is 5 bytes higher than
it should be. So it leads to a corrupted stack and the ret at 0x8048797 jumps
into some undefined place.
Compiling without -march=atom leads to a correct addition of 0x14 to %esp.

The clang/llvm is compiled by crosstool-ng, clang++ --version gives:
clang version 3.7.0 (<a href="https://github.com/llvm-mirror/clang.git">https://github.com/llvm-mirror/clang.git</a>
40b68b4c02b9d9e1e4138815747adf5589496240)
(<a href="https://github.com/llvm-mirror/llvm.git">https://github.com/llvm-mirror/llvm.git</a>
17611b180b77406671cf0819a452dd128ce7481c)</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>