收录日期:2019/04/18 21:09:18 时间:2010-06-22 20:37:01 标签:optimization,visual-c++,visual-studio-2005,compiler-optimization

Documentation for __assume says "The most common use of __assume is with the default case of a switch statement, as shown in the following example.".

  • Is there any other case where __assume can lead to a more efficient (or even a different) code?
  • When inside of if / else , is compiler automatically "assuming" what is already know because of if condition?

I was unable to find any non-trivial example which would show any of above - I hope someone else could.

Consider the following code, compiled with the /Ox switch:

if (1) {
  printf("live code\n");
} else {
  printf("dead code\n");
}

The optimizer will optimize away the else. Now consider:

int x = 1;
if (x == 1) {
  printf("live code\n");
} else {
  printf("dead code\n");
}

The optimizer will once again optimize away the else. Also consider:

int x = 1;
__assume(x != 1);
if (x == 1) {
  printf("live code\n");
} else {
  printf("dead code\n");
}

The optimizer will optimize away the if this time -- incorrectly so.

To test, build a test program in Release mode (with the /Ox and /Zi options) and look at the generated assembly (Alt+8 in Visual Studio.)

Now consider the above if/else condition being tested in an inline method. In certain contexts, the programmer may know that the inline method is called with a particular value and the optimizer may not have realized this fact. Using __assume at the caller level in the manner illustrated above, just before the inlined method is called, can theoretically help the optimizer.

From Optimization Best Practices:

__assume has been in Visual C++ for multiple releases, but it has become much more usable in Visual C++ 2005. With __assume, a developer can tell the compiler to make assumptions about the value of some variable.

For example __assume(a < 5); tells the optimizer that at that line of code the variable a is less than 5. Again this is a promise to the compiler. If a is actually 6 at this point in the program then the behavior of the program after the compiler has optimized may not be what you would expect. __assume is most useful prior to switch statements and/or conditional expressions.

There are some limitations to __assume. First, like __restrict, it is only a suggestion, so the compiler is free to ignore it. Also, __assume currently works only with variable inequalities against constants. It does not propagate symbolic inequalities, for example, assume(a < b).

Some example of __assume leading to a faster code other than u201Cno defaultu201D in switch? Delegation, some example of code? How object delegate to other Is there some way to assume @Nullable as default? (using FindBugs or any other free tool) Can we assume default array values in Java? for example, assume that an int array is set to all zeros? Example of a buffer overflow leading to a security leak Is there a reason to explicitly code a default constructor when there are no other constructors? Any weird purpose of switch / default in this code? In a class with no virtual methods or superclass, is it safe to assume (address of first member variable) == this? Is there a faster way to copy a file other than File.Copy Should I switch to a language other than Java when programming for web? Is CPYTOIMPF to qtemp faster than to other library? How to set up Drupal default front page (view) to open in other language than default ASP .NET create other Session when switch to http://example.com or http://www.example.com Javascript String concatenation faster than this example? C# How to maximize chance that particular section of code will have no context switch? What's an example of a simple C function which is faster implemented in inline assembly? Is just setting a value faster than the checking of existance and only setting if doesn't exist - like in this Android / Java code? Find the longest path in a directed cyclic graph from a source s to a destination f. Assume no positive weight cycles exists