Katoptriss
Katoptriss

Reputation: 107

Disable GCC simplification of expressions known at compile time

Suppose I have the following C line:

int a = 4 + 7;

When compiling this line, GCC will always produce something like mov DWORD PTR [rbp-X], 0xb, as the result of this computation is constant and known at compile time. Is there a way to disable this behaviour to have a mov 4, add 7 instead?

A quick search led me to the -O0 or -OG flags, but they turn off other kinds of optimizations, such as the fact that functions should be aligned in memory.

Upvotes: 0

Views: 306

Answers (1)

Eric Postpischil
Eric Postpischil

Reputation: 224310

Is there a way to disable this behaviour to have a mov 4, add 7 instead?

No. Evaluation of elementary constant expressions is built into the compiler.

Upvotes: 4

Related Questions