How to disable compiler optimizations in gcc?

The gcc option -O enables different levels of optimization. Use -O0 to disable them and use -S to output assembly. -O3 is the highest level of optimization. Starting with gcc 4.8 the optimization level -Og is available. It enables optimizations that do not interfere with debugging and is the recommended default for the standard edit-compile-debug … Read more

Efficiency of premature return in a function

There is no difference at all: =====> cat test_return.cpp extern void something(); extern void something2(); void test(bool b) { if(b) { something(); } else something2(); } =====> cat test_return2.cpp extern void something(); extern void something2(); void test(bool b) { if(b) { something(); return; } something2(); } =====> rm -f test_return.s test_return2.s =====> g++ -S test_return.cpp … Read more

Why can’t C compilers rearrange struct members to eliminate alignment padding? [duplicate]

There are multiple reasons why the C compiler cannot automatically reorder the fields: The C compiler doesn’t know whether the struct represents the memory structure of objects beyond the current compilation unit (for example: a foreign library, a file on disc, network data, CPU page tables, …). In such a case the binary structure of … Read more

How to remove “noise” from GCC/clang assembly output?

Stripping out the .cfi directives, unused labels, and comment lines is a solved problem: the scripts behind Matt Godbolt’s compiler explorer are open source on its github project. It can even do colour highlighting to match source lines to asm lines (using the debug info). You can set it up locally so you can feed … Read more

Using this pointer causes strange deoptimization in hot loop

Pointer aliasing seems to be the problem, ironically between this and this->target. The compiler is taking into account the rather obscene possibility that you initialized: this->target = &this In that case, writing to this->target[0] would alter the contents of this (and thus, this->target). The memory aliasing problem is not restricted to the above. In principle, … Read more

Clang optimization levels

I found this related question. To sum it up, to find out about compiler optimization passes: llvm-as < /dev/null | opt -O3 -disable-output -debug-pass=Arguments As pointed out in Geoff Nixon‘s answer (+1), clang additionally runs some higher level optimizations, which we can retrieve with: echo ‘int;’ | clang -xc -O3 – -o /dev/null -\#\#\# Documentation … Read more

How do I make an infinite empty loop that won’t be optimized away?

The C11 standard says this, 6.8.5/6: An iteration statement whose controlling expression is not a constant expression,156) that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation … Read more

Bubble sort slower with -O3 than -O2 with GCC

This is a regression in GCC11/12. GCC10 and earlier were doing separate dword loads, even if it merged for a qword store. It looks like GCC’s naïveté about store-forwarding stalls is hurting its auto-vectorization strategy here. See also Store forwarding by example for some practical benchmarks on Intel with hardware performance counters, and What are … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)