<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 --- - constexpr code unnecessarily generated and called"
   href="http://llvm.org/bugs/show_bug.cgi?id=15968">15968</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>constexpr code unnecessarily generated and called
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </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++11
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>hhinnant@apple.com
          </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=10497" name="attach_10497" title="date.cpp">attachment 10497</a> <a href="attachment.cgi?id=10497&action=edit" title="date.cpp">[details]</a></span>
date.cpp

I've been working on a constexpr date class that validates its input.  If all
of the input is compile-time information, the validation should happen at
compile time.

The enclosed file ends with:

int
main()
{
    constexpr ymd_date ymd(year(2013), month(5), day(11));
    return ymd.year();
}

Using tip-of-trunk clang, and -O3, the generated assembly for main is:


    .section    __TEXT,__text,regular,pure_instructions
    .globl    _main
    .align    4, 0x90
_main:                                  ## @main
    .cfi_startproc
## BB#0:                                ## %entry
    pushq    %rbp
Ltmp2:
    .cfi_def_cfa_offset 16
Ltmp3:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp4:
    .cfi_def_cfa_register %rbp
    movl    $2013, %edi             ## imm = 0x7DD
    movl    $5, %esi
    movl    $11, %edx
    callq    __ZN8ymd_date16check_invariantsE4year5month3day
    movl    $2013, %eax             ## imm = 0x7DD
    popq    %rbp
    ret
    .cfi_endproc

However the compiler should be able to deduce that the call to
__ZN8ymd_date16check_invariantsE4year5month3day is unnecessary because:

1.  It is constexpr.
2.  It is called in the process of constructing a constexpr.
3.  It compiled.

Thus it can't possibly have any run time impact.  What I'm looking for is:

    .section    __TEXT,__text,regular,pure_instructions
    .globl    _main
    .align    4, 0x90
_main:                                  ## @main
    .cfi_startproc
## BB#0:                                ## %entry
    pushq    %rbp
Ltmp2:
    .cfi_def_cfa_offset 16
Ltmp3:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp4:
    .cfi_def_cfa_register %rbp
    movl    $2013, %eax             ## imm = 0x7DD
    popq    %rbp
    ret
    .cfi_endproc

Which is *all* that gets generated if I change the enclosure to:

constexpr ymd_date ymd(year(2013), month(5), day(11));

int
main()
{
    return ymd.year();
}

In the example the programmer can quickly loose track of what input needs to be
validated and which doesn't (because it is compile-time information).  And this
seems to me to be one of the things that constexpr could do well.</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>