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
81
votes
3 answers
Why would code actively try to prevent tail-call optimization?
The title of the question might be a bit strange, but the thing is that, as far as I know, there is nothing that speaks against tail call optimization at all. However, while browsing open source projects, I already came across a few functions that…

JustSid
- 25,168
- 7
- 79
- 97
80
votes
7 answers
Is it possible to tell the branch predictor how likely it is to follow the branch?
Just to make it clear, I'm not going for any sort of portability here, so any solutions that will tie me to a certain box is fine.
Basically, I have an if statement that will 99% of the time evaluate to true, and am trying to eke out every last…

Andy Shulman
- 1,895
- 3
- 23
- 32
79
votes
2 answers
What is supercompilation?
Short and sweet: I've seen several sources talking about "supercompilation". But I have yet to find one single document anywhere on the face of the Internet which describes what this is. Presumably because it seems simple enough to whoever that it…

MathematicalOrchid
- 61,854
- 19
- 123
- 220
79
votes
4 answers
Why is -march=native used so rarely?
With most C/C++ compilers, there's a flag passable to the compiler, -march=native, which tells the compiler to tune generated code for the micro-architecture and ISA extensions of the host CPU. Even if it doesn't go by the same name, there's…

lcmylin
- 2,552
- 2
- 19
- 31
75
votes
5 answers
What kind of optimization does const offer in C/C++?
I know that where possible you should use the const keyword when passing parameters around by reference or by pointer for readability reasons. Is there any optimizations that the compiler can do if I specify that an argument is constant?
There could…

UnTraDe
- 3,747
- 10
- 36
- 60
75
votes
22 answers
Can compiler optimization introduce bugs?
Today I had a discussion with a friend of mine and we debated for a couple of hours about "compiler optimization".
I defended the point that sometimes, a compiler optimization might introduce bugs or at least, undesired behavior.
My friend totally…

ereOn
- 53,676
- 39
- 161
- 238
74
votes
3 answers
Compiler stops optimizing unused string away when adding characters
I am curious why the following piece of code:
#include
int main()
{
std::string a = "ABCDEFGHIJKLMNO";
}
when compiled with -O3 yields the following code:
main: # @main
xor eax, eax
ret
(I…

Ferenc Deak
- 34,348
- 17
- 99
- 167
74
votes
5 answers
Why is "while (i++ < n) {}" significantly slower than "while (++i < n) {}"
Apparently on my Windows 8 laptop with HotSpot JDK 1.7.0_45 (with all compiler/VM options set to default), the below loop
final int n = Integer.MAX_VALUE;
int i = 0;
while (++i < n) {
}
is at least 2 orders of magnitude faster (~10 ms vs. ~5000 ms)…

sikan
- 821
- 1
- 6
- 7
71
votes
4 answers
Why is memcmp(a, b, 4) only sometimes optimized to a uint32 comparison?
Given this code:
#include
int equal4(const char* a, const char* b)
{
return memcmp(a, b, 4) == 0;
}
int less4(const char* a, const char* b)
{
return memcmp(a, b, 4) < 0;
}
GCC 7 on x86_64 introduced an optimization for the…

John Zwinck
- 239,568
- 38
- 324
- 436
71
votes
2 answers
Why is std::fill(0) slower than std::fill(1)?
I have observed on a system that std::fill on a large std::vector was significantly and consistently slower when setting a constant value 0 compared to a constant value 1 or a dynamic value:
5.8 GiB/s vs 7.5 GiB/s
However, the results are…

Zulan
- 21,896
- 6
- 49
- 109
70
votes
2 answers
Why does using the ternary operator to return a string generate considerably different code from returning in an equivalent if/else block?
I was playing with the Compiler Explorer and I stumbled upon an interesting behavior with the ternary operator when using something like this:
std::string get_string(bool b)
{
return b ? "Hello" : "Stack-overflow";
}
The compiler generated code…

Marius T
- 783
- 5
- 10
70
votes
5 answers
Is this a JVM bug or "expected behavior"?
I noticed some unexpected behavior (unexpected relative to my personal expectations), and I'm wondering if something if there is a bug in the JVM or if perhaps this is a fringe case where I don't understand some of the details of what exactly is…

Michael McGowan
- 6,528
- 8
- 42
- 70
70
votes
7 answers
Is it possible to implement bitwise operators using integer arithmetic?
I am facing a rather peculiar problem. I am working on a compiler for an architecture that doesn't support bitwise operations. However, it handles signed 16-bit integer arithmetics and I was wondering if it would be possible to implement bitwise…

Statement
- 3,888
- 3
- 36
- 45
69
votes
8 answers
Why can't GCC optimize the logical / bitwise AND pair in "x && (x & 4242)" to "x & 4242"?
Here are two functions which I claim do exactly the same thing:
bool fast(int x)
{
return x & 4242;
}
bool slow(int x)
{
return x && (x & 4242);
}
Logically they do the same thing, and just to be 100% sure I wrote a test that ran all four…

John Zwinck
- 239,568
- 38
- 324
- 436
67
votes
8 answers
De Morgan's Law optimization with overloaded operators
Every programmer should know that:
(De Morgan's Laws)
Under some circumstances, in order to optimize the program, it may happen that compiler modifies (!p && !q) to (!(p || q)).
The two expressions are equivalent, and it makes no difference…

Alex
- 886
- 1
- 9
- 18