Skip to main content
gamedev.net
🔒 Locked

Optimization Tip

Started by neocron 2 replies 650+ views
Original Post
neocron
neocron
Not a question. Just a little tip I thought I''d share. Using MSVC (pro or ent editions): You can selectively switch on/off the assume-no-pointer-aliasing to help the compiler during its optimization of loops and register allocations to get faster code:
    
#pragma optimize( "a", on )

void gloop()
{
   // Use those non aliased pointers

}

#pragma optimize( "a", off )
    
...of course you can tell the compiler to assume no pointer aliasing for your entire project by issuing /Oa or /Ow. HOWEVER you''d better be damn sure there really ARE no aliased pointers in your project or you''re in for some fun debugging!! Hope this helps squeeze a few ms off your inner loops ;-) Karen
baskuenen
baskuenen
Sounds nice Karen
But I don't know what "assume-no-pointer-aliasing" means?

What does the compiler optimize, and what exactly are the risks Karen is talking about? Can I enable this for my whole application?

Sorry for this "less intelligent" question



Edited by - baskuenen on September 23, 2000 12:41:05 AM
Stoffel
Stoffel
Look up "/Oa" in your compiler docs (if you run MSVC). I looked it up myself and honestly can''t make heads or tails of it.

[Stoffel shoots a baffled look into the audience]

Here''s the copy/paste:

/Oa, /Ow (Assume No Aliasing, Assume Aliasing Across Function Calls)
Home | Overview | How Do I | Compiler Options

Feature Only in Professional and Enterprise Editions Code optimization is supported only in Visual C++ Professional and Enterprise Editions. For more information, see Visual C++ Editions.

Aliasing can slow your application by reducing both register storage of variables and loop optimizations.

The Assume No Aliasing (/Oa) option tells the compiler that your program does not use aliasing. An alias is a name that refers to a memory location that is already referred to by a different name.

The Assume Aliasing Across Function Calls (/Ow) option tells the compiler that no aliasing occurs within function bodies but might occur across function calls. After each function call, pointer variables must be reloaded from memory.

To find these options in the development environment, click Settings on the Project menu. Then click the C/C++ tab, and click Optimizations in the Category box. Under Optimizations, click Customize.

Rules for using /Oa and /Ow
The following rules must be followed for any variable not declared as volatile, or else /Oa and /Ow are ignored. In these rules, a variable is referenced if it is on either side of an assignment or if a function uses it in an argument:

No pointer references a variable that is used directly.


No variable is used directly if a pointer to the variable is being used.


No variable is used directly if the variable’s address is taken within a function.


No pointer is used to access a memory location if another pointer is used to modify the same memory location.
Aliasing bugs most frequently show up as corrupted data. If variables are assigned seemingly random values, compile the program with Disable (/Od). If the program works when compiled with the /Od option, do not use /Oa or /Ow.

The following code fragment could have an aliasing problem:

i = -100;
while( i < 0 )
{
i += x + y;
*p = i;
}
Without /Oa or /Ow, the compiler must assume that x or y could be modified by the assignment to *p and cannot assume that x + y is constant for each loop iteration. If you specify /Oa or /Ow, the compiler assumes that modifying *p cannot affect either x or y and x + y can be removed from the loop.

Note You can disable optimizations around code that uses aliasing (for individual functions) by using #pragma optimize with the a or w option.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.