<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 --- - DCE optimization lost after manual inlining"
   href="https://llvm.org/bugs/show_bug.cgi?id=31703">31703</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>DCE optimization lost after manual inlining
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>3.9
          </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>skvadrik@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, 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=17870" name="attach_17870" title="a.c, b.c">attachment 17870</a> <a href="attachment.cgi?id=17870&action=edit" title="a.c, b.c">[details]</a></span>
a.c, b.c

Consider the following two snippets, a.c and b.c (in attach):

// a.c:
static void f(const char *s)
{
    for (; *s++ == '0'; );
}
int main(int argc, char **argv)
{
    const char *s0 = argv[1];
    for (int x = 0; x < 1000000000; ++x) f(s0);
    return 0;
}

// b.c is the same as a.c with 'f' inlined:
int main(int argc, char **argv)
{
    const char *s0 = argv[1];
    for (int x = 0; x < 1000000000; ++x) {
        for (const char *s = s0; *s++ == '0'; );
    }
    return 0;
}

Clang manages to optimize a.c to just 'return 0':

$ clang++-3.9 -c -O2 a.c -oa.o && objdump -d a.o
clang-3.9: warning: treating 'c' input as 'c++' when in C++ mode, this behavior
is deprecated

a.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <main>:
   0:   31 c0                   xor    %eax,%eax
   2:   c3                      retq 

However, after manual inlining of 'f' this optimization fails:

$ clang++-3.9 -c -O2 b.c -ob.o && objdump -d b.o
clang-3.9: warning: treating 'c' input as 'c++' when in C++ mode, this behavior
is deprecated

b.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <main>:
   0:   48 8b 46 08             mov    0x8(%rsi),%rax
   4:   31 c9                   xor    %ecx,%ecx
   6:   66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
   d:   00 00 00 
  10:   48 89 c2                mov    %rax,%rdx
  13:   66 66 66 66 2e 0f 1f    data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
  1a:   84 00 00 00 00 00 
  20:   80 3a 30                cmpb   $0x30,(%rdx)
  23:   48 8d 52 01             lea    0x1(%rdx),%rdx
  27:   74 f7                   je     20 <main+0x20>
  29:   48 89 c2                mov    %rax,%rdx
  2c:   0f 1f 40 00             nopl   0x0(%rax)
  30:   80 3a 30                cmpb   $0x30,(%rdx)
  33:   48 8d 52 01             lea    0x1(%rdx),%rdx
  37:   74 f7                   je     30 <main+0x30>
  39:   48 89 c2                mov    %rax,%rdx
  3c:   0f 1f 40 00             nopl   0x0(%rax)
  40:   80 3a 30                cmpb   $0x30,(%rdx)
  43:   48 8d 52 01             lea    0x1(%rdx),%rdx
  47:   74 f7                   je     40 <main+0x40>
  49:   48 89 c2                mov    %rax,%rdx
  4c:   0f 1f 40 00             nopl   0x0(%rax)
  50:   80 3a 30                cmpb   $0x30,(%rdx)
  53:   48 8d 52 01             lea    0x1(%rdx),%rdx
  57:   74 f7                   je     50 <main+0x50>
  59:   48 89 c2                mov    %rax,%rdx
  5c:   0f 1f 40 00             nopl   0x0(%rax)
  60:   80 3a 30                cmpb   $0x30,(%rdx)
  63:   48 8d 52 01             lea    0x1(%rdx),%rdx
  67:   74 f7                   je     60 <main+0x60>
  69:   83 c1 05                add    $0x5,%ecx
  6c:   81 f9 00 ca 9a 3b       cmp    $0x3b9aca00,%ecx
  72:   75 9c                   jne    10 <main+0x10>
  74:   31 c0                   xor    %eax,%eax
  76:   c3                      retq</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>