/Og (Global Optimizations)
 Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

# /Og (Global Optimizations)

Visual Studio .NET 2003
```/Og
```

This option provides local and global optimizations, automatic-register allocation, and loop optimization.

• Local and global common subexpression elimination

In this optimization, the value of a common subexpression is calculated once. In the following example, if the values of `b` and `c` do not change between the three expressions, the compiler can assign the calculation of` b + c `to a temporary variable, and substitute the variable for `b + c`:

```a = b + c;
d = b + c;
e = b + c;
```

For local common subexpression optimization, the compiler examines short sections of code for common subexpressions. For global common subexpression optimization, the compiler searches entire functions for common subexpressions.

• Automatic register allocation

This optimization allows the compiler to store frequently used variables and subexpressions in registers; the register keyword is ignored.

• Loop optimization

This optimization removes invariant subexpressions from the body of a loop. An optimal loop contains only expressions whose values change through each execution of the loop. In the following example, the expression` x + y `does not change in the loop body:

```i = -100;
while( i < 0 )
{
i += x + y;
}
```

After optimization,` x + y `is calculated once rather than every time the loop is executed:

```i = -100;
t = x + y;
while( i < 0 )
{
i += t;
}
```

Loop optimization is much more effective when the compiler can assume no aliasing, which you set with Assume No Aliasing (/Oa) or Assume Aliasing Across Function Calls (/Ow).

Note   You can enable or disable global optimization on a function-by-function basis using the optimize pragma with the g option.

For related information, see Generate Intrinsic Functions (/Oi), Improve Float Consistency (/Op), and Full Optimization (/Ox).

To set this compiler option in the Visual Studio development environment

1. Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.
2. Click the C/C++ folder.
3. Click the Optimization property page.
4. Modify the Global Optimizations property.

To set this compiler option programmatically