| title | /Og (Global Optimizations) | |||||||
|---|---|---|---|---|---|---|---|---|
| description | Describes the deprecated MSVC compiler option /Og, formerly used to enable global optimizations. | |||||||
| ms.date | 10/19/2021 | |||||||
| f1_keywords |
|
|||||||
| helpviewer_keywords |
|
|||||||
| ms.assetid | d10630cc-b9cf-4e97-bde3-8d7ee79e9435 |
Deprecated. Provides local and global optimizations, automatic-register allocation, and loop optimization. We recommend you use either /O1 (Minimize Size) or /O2 (Maximize Speed) instead.
/Og
/Og is deprecated. These optimizations are now enabled by default when any optimizations are enabled. For more information on optimizations, see /O1, /O2 (Minimize Size, Maximize Speed), or /Ox (Enable Most Speed Optimizations).
The following optimizations are available under /Og:
-
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
bandcdon't change between the three expressions, the compiler can assign the calculation ofb + cto a temporary variable, and use that variable forb + 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
registerkeyword is ignored by default, and causes a diagnostic under/std:c++17or later. -
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 + ydoesn't change in the loop body:i = -100; while( i < 0 ) { i += x + y; }
After optimization,
x + yis 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
__restrict,noalias, orrestrict.[!NOTE] You can enable or disable global optimization on a function-by-function basis using the
optimizepragma together with thegoption.
For related information, see /Oi (Generate intrinsic functions) and /Ox (Enable most speed optimizations).
-
Open the project's Property Pages dialog box. For details, see Set C++ compiler and build properties in Visual Studio.
-
Select the Configuration Properties > C/C++ > Command Line property page.
-
Enter the compiler option in the Additional Options box.
- See xref:Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool.AdditionalOptions%2A.