Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

C compiler: inefficient code generation: sizeof with operand of variable length array type

pmor
Beginner
1,309 Views

Sample code:

int f(int n)
{
	int x[n];
	return sizeof x;
}

Invocation:

$ icc t0.c -std=c11 -pedantic -Wall -Wextra -c -O3
<nothing>

Expected generated code:

f:                                      # @f
        lea     eax, [4*rdi]
        ret

 (or similar)

Actual generated code:

f:
        push      rbp                                           #2.1
        mov       rbp, rsp                                      #2.1
        movsxd    rdi, edi                                      #2.1
        shl       rdi, 2                                        #3.6
        mov       rax, rdi                                      #3.6
        add       rax, 15                                       #3.6
        and       rax, -16                                      #3.6
        sub       rsp, rax                                      #3.6
        mov       rax, rsp                                      #3.6
        mov       rdx, rax                                      #4.16
        mov       rax, rdi                                      #4.16
        add       rax, 15                                       #4.16
        and       rax, -16                                      #4.16
        add       rsp, rax                                      #4.16
        mov       eax, edi                                      #4.2
        mov       rsp, rbp                                      #4.2
        pop       rbp                                           #4.2
        ret                                                     #4.2

 Version: icc 2021.5.0 on x86-64 on Linux

0 Kudos
3 Replies
MadhuK_Intel
Moderator
1,280 Views

Hi,

 

Thank you for posting in Intel communities.

 

We were able to reproduce your issue at our end with icc. Could you please try with Intel C++ Compiler 2022 (icx) compiler as it is generating the expected assembly code?

 

Not sure if you are aware of it, but the Intel C++ Classic compiler will enter "Legacy Product Support" mode signaling the end of regular updates. Please see the below article for more details.

 

https://www.intel.com/content/www/us/en/developer/articles/technical/adoption-of-llvm-complete-icx.h...

 

Thanks & Regards,

Madhu

 

0 Kudos
pmor
Beginner
1,242 Views

@MadhuK_Intel icx is OK:

f(int):                                  # 
        lea     eax, [4*rdi]
        ret
0 Kudos
MadhuK_Intel
Moderator
1,142 Views

Hi,


Glad to know that your issue is resolved. If you need any additional information, please post a new question as this thread will no longer be monitored by Intel.


Thanks and regards,

Madhu


0 Kudos
Reply