compiler-optimization

When and why would I use -fno-elide-constructors?

强颜欢笑 提交于 2019-12-04 12:13:16
I'm learning C++ and I came across the -fno-elide-constructors , below I have included the description from the man page. -fno-elide-constructors The C++ standard allows an implementation to omit creating a temporary which is only used to initialize another object of the same type. Specifying this option disables that optimization, and forces G++ to call the copy constructor in all cases. So with this option I am able disable this particular type of compiler optimization. I have a program that creates 2 objects and adds them together and prints when each of the function is called using using

Is IL generated by expression trees optimized?

帅比萌擦擦* 提交于 2019-12-04 11:41:24
问题 Ok this is merely curiosity, serves no real world help. I know that with expression trees you can generate MSIL on the fly just like the regular C# compiler does. Since compiler can decide optimizations, I'm tempted to ask what is the case with IL generated during Expression.Compile() . Basically two questions: Since at compile time the compiler can produce different (may be slightly) IL in debug mode and release mode , is there ever a difference in the IL generated by compiling an expression

Examples of CLR compiler optimizations

て烟熏妆下的殇ゞ 提交于 2019-12-04 10:59:20
I'm doing a presentation in few months about .Net performance and optimization, I wanted to provide some samples of unnecessary optimization, things that will be done by the compiler anyways. where can I find some explanation on what optimizations the compiler is actually capable of maybe some before and after code? check out these links C# Compiler Optimizations compiler optimization msdn Also checkout this book on MSIL 1. Microsoft Intermediate Language: Comparison Between C# and VB.NET / Niranjan Kumar What I think would be even better than examples of "things that will be done by the

Suppress JIT optimization on module load (managed only)

北战南征 提交于 2019-12-04 10:41:42
问题 If I run a release build in VS but WITH debugger attached. So I can set breakpoints and investigate the optimized code disassembly. Usually, in order to see all optimizations I need to run WITHOUT a debugger attached and detach to the running proccess. Does unselecting the "Suppress JIT optimization on module load (managed only)" switch in Visual Studio is sufficient to bring the same result? By 'same result' I mean: same (optimized) machine instructions as by starting without debugger

Are compilers allowed to optimize-out exception throws?

懵懂的女人 提交于 2019-12-04 10:26:23
问题 We have been discussing this topic today at work, and none of us could come up with a definitive answer about that question. Consider the following situation: int foo() { int err; err = some_call(1); if (err != 0) return err; err = some_call(2); if (err != 0) return err; err = some_call(3); if (err != 0) return err; err = some_call(4); if (err != 0) return err; bar(); return err; } There is a lot of code repetition. Obviously, this could be factorized with a macro, sadly not with a template

Why is gcc allowed to speculatively load from a struct?

牧云@^-^@ 提交于 2019-12-04 10:10:00
问题 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 B; } Pair; int foo(const Pair *P, int c) { int x; if (c) x = P->A; else x = P->B; return c/102 + x; } Here is what gcc -O3 gives: mov eax, esi mov edx, -1600085855 test esi, esi mov ecx, DWORD PTR [rdi+4] <-- ***load P->B** cmovne ecx, DWORD PTR [rdi] <

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

巧了我就是萌 提交于 2019-12-04 09:50:44
问题 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 compilers, perhaps if one requests them to do so (e.g. via -ffast-math ), in general, do it? Are compilers generally smart enough to do this, or do I need to do this myself using the scalb*() or ldexp()/frexp() function family? 回答1: For example, 2 * f,

What does the compiler do in assembly when optimizing code? ie -O2 flag

蹲街弑〆低调 提交于 2019-12-04 08:34:09
问题 So when you add an optimization flag when compiling your C++, it runs faster, but how does this work? Could someone explain what really goes on in the assembly? 回答1: It means you're making the compiler do extra work / analysis at compile time, so you can reap the rewards of a few extra precious cpu cycles at runtime. Might be best to explain with an example. Consider a loop like this: const int n = 5; for (int i = 0; i < n; ++i) cout << "bleh" << endl; If you compile this without

How to deal with branch prediction when using a switch case in CPU emulation

佐手、 提交于 2019-12-04 08:20:37
问题 I recently read the question here Why is it faster to process a sorted array than an unsorted array? and found the answer to be absolutely fascinating and it has completely changed my outlook on programming when dealing with branches that are based on Data. I currently have a fairly basic, but fully functioning interpreted Intel 8080 Emulator written in C, the heart of the operation is a 256 long switch-case table for handling each opcode. My initial thought was this would obviously be the

How exactly does gcc do optimizations?

可紊 提交于 2019-12-04 08:17:11
In order to know how exactly the gcc do the optimization, I have written two program compiling with -O2, but there is some difference of the assembly code. In my programs, I want to output "hello" in the loop, and add some delay between each output. These two programs are only for illustrating my question, and I know I can using volatile or asm in program 1 to achieve my purpose. Program 1 #include <stdio.h> int main(int argc, char **argv) { unsigned long i = 0; while (1) { if (++i > 0x1fffffffUL) { printf("hello\n"); i = 0; } } } Compile with -O2, the assembly code is: Disassembly of section