http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53792

Giulio Eulisse <giulio.eulisse at cern dot ch> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |giulio.eulisse at cern dot
                   |                            |ch

--- Comment #6 from Giulio Eulisse <giulio.eulisse at cern dot ch> 2012-08-08 
11:17:58 UTC ---
A simpler testcase for the underlying problem is the following.

struct entry {                                                                  
  char const* label;
  int         value;
};

constexpr bool same(char const *x, char const *y) {
  return !*x && !*y     ? true                                                  
       : /* default */    (*x == *y && same(x+1, y+1));                         
}

constexpr int keyToValue(char const *label, entry const *entries) {             
  return !entries->label ? entries->value                         
       : same(entries->label, label) ? entries->value
       : /*default*/                   keyToValue(label, entries+1);            
}

constexpr entry foo[] = {{"Foo", 0}, {"Bar", 1}, {"FooBar", 2}, {0, -1}};

int
bar()
{
  /* constexpr */ int result = keyToValue("Foo", foo); // Uncomment constexpr
for optimized version.
  return result;
}

Without the constexpr the code is fully expanded as if the arguments were
generic:

0000000000000000 <_Z3barv>:
   0:   53                      push   %rbx
   1:   bf 00 00 00 00          mov    $0x0,%edi
   6:   bb 00 00 00 00          mov    $0x0,%ebx
   b:   eb 0f                   jmp    1c <_Z3barv+0x1c>
   d:   0f 1f 00                nopl   (%rax)
  10:   48 83 c3 10             add    $0x10,%rbx
  14:   48 8b 3b                mov    (%rbx),%rdi
  17:   48 85 ff                test   %rdi,%rdi
  1a:   74 1d                   je     39 <_Z3barv+0x39>
  1c:   80 3f 46                cmpb   $0x46,(%rdi)
  1f:   75 ef                   jne    10 <_Z3barv+0x10>
  21:   80 7f 01 6f             cmpb   $0x6f,0x1(%rdi)
  25:   75 e9                   jne    10 <_Z3barv+0x10>
  27:   48 83 c7 02             add    $0x2,%rdi
  2b:   be 00 00 00 00          mov    $0x0,%esi
  30:   e8 00 00 00 00          callq  35 <_Z3barv+0x35>
  35:   84 c0                   test   %al,%al
  37:   74 d7                   je     10 <_Z3barv+0x10>
  39:   8b 43 08                mov    0x8(%rbx),%eax
  3c:   5b                      pop    %rbx
  3d:   c3                      retq   

Disassembly of section .text._Z4samePKcS0_:

0000000000000000 <_Z4samePKcS0_>:
   0:   0f b6 17                movzbl (%rdi),%edx
   3:   84 d2                   test   %dl,%dl
   5:   75 09                   jne    10 <_Z4samePKcS0_+0x10>
   7:   80 3e 00                cmpb   $0x0,(%rsi)
   a:   0f 94 c0                sete   %al
   d:   c3                      retq   
   e:   66 90                   xchg   %ax,%ax
  10:   31 c0                   xor    %eax,%eax
  12:   3a 16                   cmp    (%rsi),%dl
  14:   74 0a                   je     20 <_Z4samePKcS0_+0x20>
  16:   c3                      retq   
  17:   66 0f 1f 84 00 00 00    nopw   0x0(%rax,%rax,1)
  1e:   00 00 
  20:   0f b6 47 01             movzbl 0x1(%rdi),%eax
  24:   84 c0                   test   %al,%al
  26:   75 10                   jne    38 <_Z4samePKcS0_+0x38>
  28:   80 7e 01 00             cmpb   $0x0,0x1(%rsi)
  2c:   b8 01 00 00 00          mov    $0x1,%eax
  31:   75 0a                   jne    3d <_Z4samePKcS0_+0x3d>
  33:   f3 c3                   repz retq 
  35:   0f 1f 00                nopl   (%rax)
  38:   3a 46 01                cmp    0x1(%rsi),%al
  3b:   74 0b                   je     48 <_Z4samePKcS0_+0x48>
  3d:   31 c0                   xor    %eax,%eax
  3f:   90                      nop
  40:   c3                      retq   
  41:   0f 1f 80 00 00 00 00    nopl   0x0(%rax)
  48:   48 83 ec 08             sub    $0x8,%rsp
  4c:   48 83 c6 02             add    $0x2,%rsi
  50:   48 83 c7 02             add    $0x2,%rdi
  54:   e8 00 00 00 00          callq  59 <_Z4samePKcS0_+0x59>
  59:   84 c0                   test   %al,%al
  5b:   74 10                   je     6d <_Z4samePKcS0_+0x6d>
  5d:   b8 01 00 00 00          mov    $0x1,%eax
  62:   66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)
  68:   48 83 c4 08             add    $0x8,%rsp
  6c:   c3                      retq   
  6d:   31 c0                   xor    %eax,%eax
  6f:   eb f7                   jmp    68 <_Z4samePKcS0_+0x68>

while with constexpr on results everything is optimized out:

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

Reply via email to