<html>
    <head>
      <base href="http://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 --- - [X86] Call to __tls_get_addr makes the function non-leaf after it was determined to be a leaf"
   href="http://llvm.org/bugs/show_bug.cgi?id=20243">20243</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[X86] Call to __tls_get_addr makes the function non-leaf after it was determined to be a leaf
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </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>dario.domizioli@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>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=12754" name="attach_12754" title="Test case source code (see description for command lines)">attachment 12754</a> <a href="attachment.cgi?id=12754&action=edit" title="Test case source code (see description for command lines)">[details]</a></span>
Test case source code (see description for command lines)

Consider a simple example which accesses the address of a thread-local
variable:

// ===== ===== tls_non_leaf.cpp ===== =====
struct SomeStruct {
    int x;
};

static thread_local SomeStruct ts = {0};

SomeStruct* foo(SomeStruct *t) {
    return (t == 0) ? &ts : t;
}
// ===== ===== tls_non_leaf.cpp ===== =====

In PIC mode on X86, the access to &ts is lowered to a call to __tls_get_addr.

However, the lowering of such access to such call is performed under
X86AsmPrinter::EmitInstruction (which calls lowerTlsAddr), well after the
function was analysed to determine whether it contained any function calls
(i.e. whether it was a leaf or not).
This can have issues with features like -momit-leaf-frame-pointer, because the
function is not actually a leaf but it was incorrectly considered a leaf.

To reproduce, compile the source to assembly this way:

clang -O2 -fpic -fno-omit-frame-pointer -momit-leaf-frame-pointer -S -std=c++11
-o omit.s tls_non_leaf.cpp

And then this way:

clang -O2 -fpic -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -S
-std=c++11 -o no-omit.s tls_non_leaf.cpp

In theory, since the function is not really a leaf, the different options for
the leaf frame pointer should not make a difference. However, there is a
difference in the generated code because the function is mistakenly recognized
as a leaf. 

My opinion is that the expansion to __tls_get_addr happens too late, it should
happen at instruction selection stage rather than in the AsmPrinter.

I have tested this on x86_64-unknown-linux with trunk at revision 212522.
However I believe this problem affects all x86_64 triples that support TLS.</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>