compiler-optimization

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

落花浮王杯 提交于 2020-04-07 11:19:09
问题 The C11 standard appears to imply that iteration statements with constant controlling expressions should not be optimized out. I'm taking my advice from this answer, which specifically quotes section 6.8.5 from the draft standard: An iteration statement whose controlling expression is not a constant expression ... may be assumed by the implementation to terminate. In that answer it mentions that a loop like while(1) ; should not be subject to optimization. So...why does Clang/LLVM optimize

G++ optimization beyond -O3/-Ofast

被刻印的时光 ゝ 提交于 2020-04-07 10:46:31
问题 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, we want to run the program on several systems probably for some months. Therefore we are really interested in pushing the optimization to the limits. All systems will run Debian/Linux on relatively new hardware (Intel i5 or i7). The Question What

G++ optimization beyond -O3/-Ofast

允我心安 提交于 2020-04-07 10:46:11
问题 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, we want to run the program on several systems probably for some months. Therefore we are really interested in pushing the optimization to the limits. All systems will run Debian/Linux on relatively new hardware (Intel i5 or i7). The Question What

Is the Null-Conditional Operators thread-safety save from compiler optimisations? [duplicate]

浪子不回头ぞ 提交于 2020-03-28 06:53:10
问题 This question already has answers here : Events and multithreading once again (2 answers) C# Events and Thread Safety (15 answers) Is C#'s null-conditional delegate invocation thread safe? (3 answers) Is C# 6 ?. (Elvis op) thread safe? If so, how? (2 answers) c# ?. conditional operator (3 answers) Closed 13 days ago . The documentation says that PropertyChanged?.Invoke(…) is thread safe, because it is equivalent to this: var handler = this.PropertyChanged; if (handler != null) { handler(…); }

How optimized is the C# compiler? [duplicate]

流过昼夜 提交于 2020-03-24 09:57:10
问题 This question already has answers here : MS C# compiler and non-optimized code (3 answers) Closed 4 years ago . The IL code (generated with https://dotnetfiddle.net) of this piece of code: public class Program { public static void Main() { int i = 10; if (i < 4) Console.WriteLine("Hello World"); } } contains ldstr "Hello World". Shouldn't the compiler know that Console.WriteLine never gets executed? The IL code of this: public class Program { public static void Main() { if (10 < 4) Console

Compiler changes printf to puts

对着背影说爱祢 提交于 2020-02-15 10:00:30
问题 Consider the following code: #include <stdio.h> void foo() { printf("Hello world\n"); } void bar() { printf("Hello world"); } The assembly produced by both these two functions is: .LC0: .string "Hello world" foo(): mov edi, OFFSET FLAT:.LC0 jmp puts bar(): mov edi, OFFSET FLAT:.LC0 xor eax, eax jmp printf Now I know the difference between puts and printf, but I find this quite interesting that gcc is able to introspect the const char* and figure out whether to call printf or puts. Another

How does noexcept in C++ change the assembly?

自作多情 提交于 2020-02-02 11:18:17
问题 How does noexcept in C++ change the assembly? I tried a bit with small functions, in godbolt, but the assembly did not change. float pi() //noexcept // no difference { return 3.14; } int main(){ float b{0}; b = pi(); return 0; } I am looking for a minimal working example, where I can see a change in the assembly due to noexcept . 回答1: Pretty simple examples can be constructed that involve destructors directly rather than introspection on noexcept status: void a(int); void b() noexcept; void c

How does noexcept in C++ change the assembly?

余生长醉 提交于 2020-02-02 11:18:04
问题 How does noexcept in C++ change the assembly? I tried a bit with small functions, in godbolt, but the assembly did not change. float pi() //noexcept // no difference { return 3.14; } int main(){ float b{0}; b = pi(); return 0; } I am looking for a minimal working example, where I can see a change in the assembly due to noexcept . 回答1: Pretty simple examples can be constructed that involve destructors directly rather than introspection on noexcept status: void a(int); void b() noexcept; void c

Volatile and compiler optimization

耗尽温柔 提交于 2020-01-29 14:25:26
问题 Is it OK to say that 'volatile' keyword makes no difference if the compiler optimization is turned off i.e (gcc -o0 ....)? I had made some sample 'C' program and seeing the difference between volatile and non-volatile in the generated assembly code only when the compiler optimization is turned on i.e ((gcc -o1 ....). 回答1: No, there is no basis for making such a statement. volatile has specific semantics that are spelled out in the standard. You are asserting that gcc -O0 always generates code

Volatile and compiler optimization

你离开我真会死。 提交于 2020-01-29 14:25:07
问题 Is it OK to say that 'volatile' keyword makes no difference if the compiler optimization is turned off i.e (gcc -o0 ....)? I had made some sample 'C' program and seeing the difference between volatile and non-volatile in the generated assembly code only when the compiler optimization is turned on i.e ((gcc -o1 ....). 回答1: No, there is no basis for making such a statement. volatile has specific semantics that are spelled out in the standard. You are asserting that gcc -O0 always generates code