[cfe-users] Why does clang not always produce constant value for same static constexpr

Christopher Williams via cfe-users cfe-users at lists.llvm.org
Wed Sep 25 12:37:39 PDT 2019


Given the code below, clang produces a constant value for test1, test2
and test4. Why doesn't it for test3?
This is more of a curious query, than a request for help, but if someone
does have the answer, I'd appreciate as much detail as possible.
||

    |staticconstexprintcount_x(constchar*str){intcount{};for(;*str
    !=0;++str){count +=*str
    =='x';}returncount;}#defineSTRx1"123456789x"#defineSTRx4STRx1STRx1STRx1STRx1#defineSTRx8STRx4STRx4#defineSTRx16STRx8STRx8inttest1(){returncount_x(STRx4);}inttest2(){returncount_x(STRx8);}inttest3(){returncount_x(STRx16);}inttest4(){constexprautok
    =count_x(STRx16);returnk;}|


    |test1():# @test1()mov eax,4ret test2():# @test2()mov eax,8ret
    test3():# @test3()xor eax,eax mov dl,49mov ecx,offset
    .L.str.2+1.LBB2_1:# =>This Inner Loop Header: Depth=1xor esi,esi cmp
    dl,120sete sil add eax,esi movzx edx,byte ptr [rcx]add rcx,1test
    dl,dl jne .LBB2_1 ret test4():# @test4()mov eax,16ret
    .L.str.2:.asciz
    "123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x123456789x"|

gcc does:

    |test1():mov eax,4ret test2():mov eax,8ret test3():mov eax,16ret
    test4():mov eax,16ret|

Compilation command lines used:

|clang++-Ofast-std=c++2a-S -o --c src/test.cpp |grep -Ev$'^\t+\\.'gcc9
-Ofast-std=c++2a-S -o --c src/test.cpp |grep -Ev$'^\t+\\.'|

Compiler Explorer: https://godbolt.org/z/V-3MEp






More information about the cfe-users mailing list