When will the trivial (code that has no effect) code gets removed in compilation process?

volatile int num = 0;
num = num + 10;

The above C++ Code seems to produce following code in intel assembly:

mov DWORD PTR [rbp-4], 0
mov eax, DWORD PTR [rbp-4]
add eax, 10
mov DWORD PTR [rbp-4], eax

If I change C++ code to

volatile int num = 0;
num = num + 0;

why will not compiler produce assembly code as:

mov DWORD PTR [rbp-4], 0
mov eax, DWORD PTR [rbp-4]
add eax, 0
mov DWORD PTR [rbp-4], eax

At which part of compilation process does this kind trivial code gets removed. Is there any compiler flag which will make GCC compiler to not do these kind of optimizations.

0 Comment

NO COMMENTS

LEAVE A REPLY

Captcha image