On Thu, Mar 02, 2023 at 12:00:18PM +0000, Ali Farzanrad wrote:
> Hi,
>
> Is it normal to have such behavior?
>
> $ cat loop.c
> int
> main(void)
> {
> for (;;)
> ;
> }
> $ clang -O1 -Wall -Wextra -S -o loop.c.s loop.c
> $ clang++ -O1 -Wall -Wextra -S -o loop.cxx.s loop.c
> clang++: warning: treating 'c' input as 'c++' when in C++ mode, this behavior
> is deprecated [-Wdeprecated]
> $ diff -U8 loop.c.s loop.cxx.s
> --- loop.c.s Thu Mar 2 11:55:02 2023
> +++ loop.cxx.s Thu Mar 2 11:55:08 2023
> @@ -5,20 +5,16 @@
> .type main,@function
> main: # @main
> .cfi_startproc
> # %bb.0:
> pushq %rbp
> .cfi_def_cfa_offset 16
> .cfi_offset %rbp, -16
> movq %rsp, %rbp
> - .cfi_def_cfa_register %rbp
> - .p2align 4, 0x90
> -.LBB0_1: # =>This Inner Loop Header: Depth=1
> - jmp .LBB0_1
> .Lfunc_end0:
> .size main, .Lfunc_end0-main
> .cfi_endproc
> # -- End function
> .section
> .text.__llvm_retpoline_r11,"axG",@progbits,__llvm_retpoline_r11,comdat
> .hidden __llvm_retpoline_r11 # -- Begin function
> __llvm_retpoline_r11
> .weak __llvm_retpoline_r11
> .p2align 4, 0xcc
>
>
> See that jmp instruction which is removed.
> Without that jmp instruction the main function might reach other codes!
>
See https://en.cppreference.com/w/cpp/language/for "Notes"
In C++, a for loop that has no observable behaviour can be removed.
Note that "observable behaviour" has a specific definition in C++.
-Otto