Compiler optimization involves adapting a compiler to reduce run-time or object size or both. This can be accomplished using compiler arguments (i.e. CFLAGS, LDFLAGS), compiler plugins (DEHYDRA for instance) or direct modifications to the compiler (such as modifying source code).
Questions tagged [compiler-optimization]
3117 questions
149
votes
1 answer
Bubble sort slower with -O3 than -O2 with GCC
I made a bubble sort implementation in C, and was testing its performance when I noticed that the -O3 flag made it run even slower than no flags at all! Meanwhile -O2 was making it run a lot faster as expected.
Without optimisations:
time ./sort…

anon
- 1,269
- 2
- 4
- 6
149
votes
5 answers
Why does the enhanced GCC 6 optimizer break practical C++ code?
GCC 6 has a new optimizer feature: It assumes that this is always not null and optimizes based on that.
Value range propagation now assumes that the this pointer of C++ member functions is non-null. This eliminates common null pointer checks but…

boot4life
- 4,966
- 7
- 25
- 47
138
votes
4 answers
Clang optimization levels
For gcc, the manual explains what -O3, -Os, etc. translate to in terms of specific optimisation arguments (-funswitch-loops, -fcompare-elim, etc.)
I'm looking for the same info for clang.
I've looked online and in man clang which only gives general…

Antoine
- 13,494
- 6
- 40
- 52
133
votes
3 answers
Using this pointer causes strange deoptimization in hot loop
I recently came across a strange deoptimization (or rather missed optimization opportunity).
Consider this function for efficient unpacking of arrays of 3-bit integers to 8-bit integers. It unpacks 16 ints in each loop iteration:
void…

gexicide
- 38,535
- 21
- 92
- 152
132
votes
7 answers
Why can't (or doesn't) the compiler optimize a predictable addition loop into a multiplication?
This is a question that came to mind while reading the brilliant answer by Mysticial to the question: why is it faster to process a sorted array than an unsorted array?
Context for the types involved:
const unsigned arraySize = 32768;
int…

jhabbott
- 18,461
- 9
- 58
- 95
113
votes
3 answers
How to remove "noise" from GCC/clang assembly output?
I want to inspect the assembly output of applying boost::variant in my code in order to see which intermediate calls are optimized away.
When I compile the following example (with GCC 5.3 using g++ -O3 -std=c++14 -S), it seems as if the compiler…

m.s.
- 16,063
- 7
- 53
- 88
105
votes
11 answers
Why can't C compilers rearrange struct members to eliminate alignment padding?
Possible Duplicate:
Why doesn't GCC optimize structs?
Why doesn't C++ make the structure tighter?
Consider the following example on a 32 bit x86 machine:
Due to alignment constraints, the following struct
struct s1 {
char a;
int b;
…

bisgardo
- 4,130
- 4
- 29
- 38
101
votes
7 answers
How to disable compiler optimizations in gcc?
I am trying to learn assembly language. I have searched and found how to disassemble a .c file but I think it produces some optimized version of the program. Is there any way so that I can see the exact assembly code which corresponds to my C file.

Neal
- 3,119
- 4
- 27
- 32
97
votes
11 answers
Efficiency of premature return in a function
This is a situation I encounter frequently as an inexperienced programmer and am wondering about particularly for an ambitious, speed-intensive project of mine I'm trying to optimize. For the major C-like languages (C, objC, C++, Java, C#, etc) and…

Philip Guin
- 1,452
- 15
- 21
94
votes
8 answers
G++ optimization beyond -O3/-Ofast
The Problem
We have a mid-sized program for a simulation task, that we need to optimize. We have already done our best optimizing the source to the limit of our programming skills, including profiling with Gprof and Valgrind.
When finally finished,…

Haatschii
- 9,021
- 10
- 58
- 95
89
votes
8 answers
Is there a reason why not to use link-time optimization (LTO)?
GCC, MSVC, LLVM, and probably other toolchains have support for link-time (whole program) optimization to allow optimization of calls among compilation units.
Is there a reason not to enable this option when compiling production software?

Honza
- 1,734
- 3
- 16
- 22
87
votes
3 answers
Why can't GCC generate an optimal operator== for a struct of two int32s?
A colleague showed me code that I thought wouldn't be necessary, but sure enough, it was. I would expect most compilers would see all three of these attempts at equality tests as equivalent:
#include
#include
struct Point {
…

Ben
- 9,184
- 1
- 43
- 56
84
votes
5 answers
Why doesn't 'd /= d' throw a division by zero exception when d == 0?
I don't quite understand why I don't get a division by zero exception:
int d = 0;
d /= d;
I expected to get a division by zero exception but instead d == 1.
Why doesn't d /= d throw a division by zero exception when d == 0?

Valerii Boldakov
- 1,751
- 9
- 19
84
votes
6 answers
How to turn off gcc compiler optimization to enable buffer overflow
I'm working on a homework problem that requires disabling compiler optimization protection for it to work. I'm using gcc 4.4.1 on ubuntu linux, but can't figure out which flags are are the right ones. I realize it's architecture dependant - my…

sa125
- 28,121
- 38
- 111
- 153
81
votes
2 answers
What is the difference between the /Ox and /O2 compiler options?
Microsoft's C++ compiler (cl.exe, as included with Visual Studio) offers several optimization switches. The difference between most of them seems self-explanatory, but it's not clear to me what the difference is between /O2 (which optimizes code for…

Cody Gray - on strike
- 239,200
- 50
- 490
- 574