You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Any expression that is assumed to evaluate to true.
25
+
*`expression`*\
26
+
For reachable code, any expression that is assumed to evaluate to **`true`**. Use `0` to indicate unreachable code to the optimizer.
27
27
28
28
## Remarks
29
29
30
-
The optimizer assumes that the condition represented by `expression` is true at the point where the keyword appears and remains true until `expression` is modified (for example, by assignment to a variable). Selective use of hints passed to the optimizer by **`__assume`** can improve optimization.
30
+
The optimizer assumes that the condition represented by `expression` is **`true`** at the point where the keyword appears and remains true until `expression` is modified (for example, by assignment to a variable). Selective use of hints passed to the optimizer by **`__assume`** can improve optimization.
31
31
32
-
If the **`__assume`** statement is written as a contradiction (an expression that always evaluates to false), it is always treated as `__assume(0)`. If your code isn’t behaving as expected, ensure that the `expression` you defined is valid and true, as described earlier. For more information about expected `__assume(0)`behavior, see the later remarks.
32
+
If the **`__assume`** statement is written as a contradiction (an expression that always evaluates to **`false`**), it's always treated as `__assume(0)`. If your code isn't behaving as expected, ensure that the `expression` you defined is valid and **`true`**, as described earlier. The `__assume(0)` statement is a special case. Use `__assume(0)`to indicate a code path that can't be reached.
33
33
34
34
> [!WARNING]
35
35
> A program must not contain an invalid **`__assume`** statement on a reachable path. If the compiler can reach an invalid **`__assume`** statement, the program might cause unpredictable and potentially dangerous behavior.
36
36
37
-
`__assume` is not a genuine intrinsic. It does not have to be declared as a function and it cannot be used in a `#pragma intrinsic` directive. Although no code is generated, the code generated by the optimizer is affected.
37
+
For compatibility with previous versions, **`_assume`** is a synonym for **`__assume`** unless compiler option [`/Za` (Disable language extensions)](../build/reference/za-ze-disable-language-extensions.md)is specified.
38
38
39
-
Use **`__assume`** in an [ASSERT](../c-runtime-library/reference/assert-asserte-assert-expr-macros.md) only when the assert is not recoverable. Do not use **`__assume`** in an assert for which you have subsequent error recovery code because the compiler might optimize away the error-handling code.
39
+
`__assume` isn't a genuine intrinsic. It doesn't have to be declared as a function and it can't be used in a `#pragma intrinsic` directive. Although no code is generated, the code generated by the optimizer is affected.
40
40
41
-
The `__assume(0)` statement is a special case. Use `__assume(0)` to indicate a code path that cannot be reached. The following example shows how to use `__assume(0)` to indicate that the default case of a switch statement cannot be reached. This shows the most typical use of `__assume(0)`.
42
-
43
-
For compatibility with previous versions, **`_assume`** is a synonym for **`__assume`** unless compiler option [/Za \(Disable language extensions)](../build/reference/za-ze-disable-language-extensions.md) is specified.
41
+
Use **`__assume`** in an [`ASSERT`](../c-runtime-library/reference/assert-asserte-assert-expr-macros.md) only when the assertion isn't recoverable. Don't use **`__assume`** in an assertion for which you have subsequent error recovery code because the compiler might optimize away the error-handling code.
44
42
45
43
## Requirements
46
44
47
-
|Intrinsic|Architecture|
48
-
|---------------|------------------|
49
-
|**`__assume`**|x86, ARM, x64, ARM64|
45
+
|Intrinsic|Architecture|
46
+
|--|--|
47
+
|**`__assume`**|x86, ARM, x64, ARM64, ARM64EC |
50
48
51
49
## Example
52
50
51
+
The following example shows how to use `__assume(0)` to indicate that the **`default`** case of a **`switch`** statement can't be reached. It's the most typical use of `__assume(0)`. Here, the programmer knows that the only possible inputs for `p` will be 1 or 2. If another value is passed in for `p`, the program becomes invalid and causes unpredictable behavior.
// cannot be reached. As so, it does not have to generate
78
-
// the extra code to check that 'p' has a value
79
-
// not represented by a case arm. This makes the switch
80
-
// run faster.
62
+
switch(p)
63
+
{
64
+
case 1:
65
+
func1(1);
66
+
break;
67
+
case 2:
68
+
func1(-1);
69
+
break;
70
+
default:
71
+
__assume(0);
72
+
// This tells the optimizer that the default
73
+
// cannot be reached. As so, it does not have to generate
74
+
// the extra code to check that 'p' has a value
75
+
// not represented by a case arm. This makes the switch
76
+
// run faster.
81
77
}
82
78
}
83
79
```
84
80
85
-
The use of `__assume(0)` tells the optimizer that the default case cannot be reached. The example demonstrates that the programmer knows that the only possible inputs for `p` will be 1 or 2. If another value is passed in for `p`, the program becomes invalid and causes unpredictable behavior.
81
+
As a result of the `__assume(0)` statement, the compiler doesn't generate code to test whether `p` has a value that isn't represented in a case statement.
86
82
87
-
As a result of the `__assume(0)` statement, the compiler does not generate code to test whether `p` has a value that is not represented in a case statement. For this to work, the `__assume(0)` statement must be the first statement in the body of the default case.
88
-
89
-
Because the compiler generates code based on **`__assume`**, that code might not run correctly if the expression inside the **`__assume`** statement is false at run time. If you are not sure that the expression will always be true at run time, you can use the `assert` function to protect the code.
83
+
If you aren't sure that the expression will always be **`true`** at runtime, you can use the `assert` function to protect the code. This macro definition wraps the **`__assume`** statement with a check:
Unfortunately, this use of `assert` prevents the compiler from performing the default-case optimization that was described earlier in this document. As an alternative, you can use a separate macro, as follows.
89
+
For the **`default`** case optimization to work, the `__assume(0)` statement must be the first statement in the body of the **`default`**case. Unfortunately, the `assert` in the `ASSUME` macro prevents the compiler from performing this optimization. As an alternative, you can use a separate macro, as shown here:
96
90
97
91
```C
98
92
#ifdef DEBUG
99
-
# define NODEFAULT ASSERT(0)
93
+
// This code is supposed to be unreachable, so assert
0 commit comments