Can compiler optimization introduce bugs?

Compiler optimizations can introduce bugs or undesirable behaviour. That’s why you can turn them off. One example: a compiler can optimize the read/write access to a memory location, doing things like eliminating duplicate reads or duplicate writes, or re-ordering certain operations. If the memory location in question is only used by a single thread and … Read more

Is it possible to tell the branch predictor how likely it is to follow the branch?

Yes, but it will have no effect. Exceptions are older (obsolete) architectures pre Netburst, and even then it doesn’t do anything measurable. There is an “branch hint” opcode Intel introduced with the Netburst architecture, and a default static branch prediction for cold jumps (backward predicted taken, forward predicted non taken) on some older architectures. GCC … Read more

Why would code actively try to prevent tail-call optimization?

My guess here is that it’s to ensure that __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ is in the stack trace for debugging purposes. It has __attribute__((no inline)) which backs up this idea. If you notice, that function just goes and bounces to another function anyway, so it’s a form of trampoline which I can only think is there with such … Read more

Why doesn’t ‘d /= d’ throw a division by zero exception when d == 0?

C++ does not have a “Division by Zero” Exception to catch. The behavior you’re observing is the result of Compiler optimizations: The compiler assumes Undefined Behavior doesn’t happen Division by Zero in C++ is undefined behavior Therefore, code which can cause a Division by Zero is presumed to not do so. And, code which must … Read more

How to turn off gcc compiler optimization to enable buffer overflow

That’s a good problem. In order to solve that problem you will also have to disable ASLR otherwise the address of g() will be unpredictable. Disable ASLR: sudo bash -c ‘echo 0 > /proc/sys/kernel/randomize_va_space’ Disable canaries: gcc overflow.c -o overflow -fno-stack-protector After canaries and ASLR are disabled it should be a straight forward attack like … Read more

Is there a reason why not to use link-time optimization (LTO)?

I assume that by “production software” you mean software that you ship to the customers / goes into production. The answers at Why not always use compiler optimization? (kindly pointed out by Mankarse) mostly apply to situations in which you want to debug your code (so the software is still in the development phase — … Read more

Why can’t GCC generate an optimal operator== for a struct of two int32s?

If you “fix” the alignment, all give the same assembly language output (with GCC): struct alignas(std::int64_t) Point { std::int32_t x, y; }; Demo As a note, some correct/legal ways to do some stuff (as type punning) is to use memcpy, so having specific optimization (or be more aggressive) when using that function seems logical.

G++ optimization beyond -O3/-Ofast

Most of the answers suggest alternative solutions, such as different compilers or external libraries, which would most likely bring a lot of rewriting or integration work. I will try to stick to what the question is asking, and focus on what can be done with GCC alone, by activating compiler flags or doing minimal changes … Read more

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