[llvm-dev] clang emits calls to consexpr function.
Michiel Derhaeg via llvm-dev
llvm-dev at lists.llvm.org
Tue Feb 5 05:35:41 PST 2019
And if you define "x" as constexpr instead of const, you will force the
compiler to compute product at compile-time without opimizations
enabled.
On 05/02 14:08, Hans Wennborg via llvm-dev wrote:
> 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: x86_64-unknown-linux-gnu
> >
> > $cat test.s
> >
> > main:
> > .cfi_startproc
> > # %bb.0:
> > pushq %rbp
> > .cfi_def_cfa_offset 16
> > .cfi_offset %rbp, -16
> > movq %rsp, %rbp
> > .cfi_def_cfa_register %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
> > .cfi_def_cfa %rsp, 8
> > retq
> >
> >
> > while g++ do not emits calls to constexpr function
> > $g++ test.cpp -std=c++11
> > $cat test.s
> > main:
> > .LFB1:
> > .cfi_startproc
> > pushq %rbp
> > .cfi_def_cfa_offset 16
> > .cfi_offset 6, -16
> > movq %rsp, %rbp
> > .cfi_def_cfa_register 6
> > movl $200, -4(%rbp)
> > movl $0, %eax
> > popq %rbp
> > .cfi_def_cfa 7, 8
> > ret
> > .cfi_endproc
> >
> > 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
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
More information about the llvm-dev
mailing list