Questions tagged [compiler-optimization]

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).

3117 questions
55
votes
2 answers

SIMD instructions lowering CPU frequency

I read this article. It talked about why AVX-512 instruction: Intel’s latest processors have advanced instructions (AVX-512) that may cause the core, or maybe the rest of the CPU to run slower because of how much power they use. I think on…
HCSF
  • 2,387
  • 1
  • 14
  • 40
52
votes
6 answers

Why is gcc allowed to speculatively load from a struct?

Example Showing the gcc Optimization and User Code that May Fault The function 'foo' in the snippet below will load only one of the struct members A or B; well at least that is the intention of the unoptimized code. typedef struct { int A; int…
zr.
  • 7,528
  • 11
  • 50
  • 84
51
votes
3 answers

Why is Clang optimizing this code out?

The purpose of the code is to find the total number of 32-bit floating point bit patterns which represent values between 0 and 1. It seems to me this should work, but for some reason the assembly output from Clang is basically the equivalent of…
Chris_F
  • 4,991
  • 5
  • 33
  • 63
49
votes
14 answers

Using Assembly Language in C/C++

I remember reading somewhere that to really optimize & speed up certain section of the code, programmers write that section in Assembly language. My questions are - Is this practice still done? and How does one do this? Isn't writing in Assembly…
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
49
votes
1 answer

JIT not optimizing loop that involves Integer.MAX_VALUE

While writing an answer to another question, I noticed a strange border case for JIT optimization. The following program is not a "Microbenchmark" and not intended to reliably measure an execution time (as pointed out in the answers to the other…
Marco13
  • 53,703
  • 9
  • 80
  • 159
49
votes
5 answers

GCC: Difference between -O3 and -Os

I am quite familiar with GCC -O3 flag, but how it differs from -Os, in which situation we should prefer one over other?
Saqlain
  • 17,490
  • 4
  • 27
  • 33
48
votes
6 answers

Why doesn't GCC optimize out deletion of null pointers in C++?

Consider a simple program: int main() { int* ptr = nullptr; delete ptr; } With GCC (7.2), there is a call instruction regarding to operator delete in the resulting program. With Clang and Intel compilers, there are no such instructions, the…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
48
votes
2 answers

Is this a compiler optimisation bug, or an undefined behaviour?

We have an annoying bug I can't explain around this piece of code: unsigned char bitmap[K_BITMAP_SIZE] = {0} ; SetBit(bitmap, K_18); // Sets the bit #18 to 1 for(size_t i = 0; i < K_END; ++i) { if(TestBit(bitmap, i)) // true for 18 { …
47
votes
2 answers

Unexpected result when C++ store element into std::vector from return value of function

When the function involves reallocation, I found some compilers may save the address before the function call. It leads the return value stored in the invalid address. There is an example to explain behavior in above description. #include…
Morris Yang
  • 482
  • 3
  • 10
47
votes
1 answer

gcc optimization flag -O3 makes code slower than -O2

I find this topic Why is it faster to process a sorted array than an unsorted array? . And try to run this code. And I find strange behavior. If I compile this code with -O3 optimization flag it takes 2.98605 sec to run. If I compile with -O2 it…
Mike Minaev
  • 1,912
  • 4
  • 23
  • 33
46
votes
3 answers

Getting an optimization report from GCC

I would like to know if there is an option I can use with GCC to get a detailed report on the optimization actually chosen and performed by the compiler. This is possible with the Intel C compiler using the -opt-report. I do not want to look at the…
bkm
  • 973
  • 7
  • 13
46
votes
14 answers

Can different optimization levels lead to functionally different code?

I am curious about the liberties that a compiler has when optimizing. Let's limit this question to GCC and C/C++ (any version, any flavour of standard): Is it possible to write code which behaves differently depending on which optimization level it…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
46
votes
4 answers

Does the C# compiler treat a lambda expression as a public or private method?

Internally, the compiler should be translating lambda expressions to methods. In that case, would these methods be private or public (or something else) and is it possible to change that?
Alex Sk
  • 555
  • 4
  • 10
46
votes
9 answers

Why doesn't a compiler optimize floating-point *2 into an exponent increment?

I've often noticed gcc converting multiplications into shifts in the executable. Something similar might happen when multiplying an int and a float. For example, 2 * f, might simply increment the exponent of f by 1, saving some cycles. Do the…
user1095108
  • 14,119
  • 9
  • 58
  • 116
45
votes
1 answer

"xor eax, ebp" being used in C++ compiler output

I just tried compiling a couple of C++ snippets on VS2010 and analyzed the executables on IDA Pro. Something I noticed is that there most of them have something like the following at the start(shortly after a call to __security_check_cookie) xor…