[llvm-dev] clang emits calls to consexpr function. (original) (raw)

Hans Wennborg via llvm-dev llvm-dev at lists.llvm.org
Tue Feb 5 05:08:07 PST 2019


On Tue, Feb 5, 2019 at 1:41 PM kamlesh kumar via llvm-dev <llvm-dev at lists.llvm.org> wrote:

Hi Devs, consider below testcase $cat test.cpp constexpr int product() { return 10*20; } int main() { const int x = product(); return 0; }

$./clang test.cpp -std=c++11 -S $./clang -v clang version 9.0.0 Target: x8664-unknown-linux-gnu $cat test.s main: .cfistartproc # %bb.0: pushq %rbp .cfidefcfaoffset 16 .cfioffset %rbp, -16 movq %rsp, %rbp .cfidefcfaregister %rbp subq $16, %rsp movl $0, -4(%rbp) callq Z7productv //here you can see the calls to product function xorl %ecx, %ecx movl %eax, -8(%rbp) movl %ecx, %eax addq $16, %rsp popq %rbp .cfidefcfa %rsp, 8 retq while g++ do not emits calls to constexpr function $g++ test.cpp -std=c++11 $cat test.s main: .LFB1: .cfistartproc pushq %rbp .cfidefcfaoffset 16 .cfioffset 6, -16 movq %rsp, %rbp .cfidefcfaregister 6 movl $200, -4(%rbp) movl $0, %eax popq %rbp .cfidefcfa 7, 8 ret .cfiendproc is this bug in clang compiler?

It's not a bug. constexpr just means the value can be used as a constant, it doesn't mean the compiler has to compute it as a constant. If you turn on some optimization, the call will get inlined and constant folded: https://gcc.godbolt.org/z/M806yO



More information about the llvm-dev mailing list