When can a variable be completely excluded?

Consider this code example:

int main(void) { volatile int a; static volatile int b; volatile int c; c = 20; static volatile int d; d = 30; volatile int e = 40; static volatile int f = 50; return 0; } 

Without volatile compiler can optimize all variables since they are never read.

I think that a and b can be optimized since they are not completely used, see unused mutable variable .

I think c and d cannot be deleted, since they are written, and writing to mutable variables should actually happen. e should be equivalent to c .

GCC does not optimize f , but also does not generate any commands to write to it. 50 is specified in the data section. LLVM (clang) completely removes f .

Are these statements true?

  • A mutable variable can be optimized if it is never available.
  • Initialization of a static or global variable is not considered access.
+6
source share
2 answers

Writes volatile variables (even automatic) as observable behavior.

C11 (N1570) 5.1.2.3/6:

Least requirements for the corresponding implementation:

- Access to volatile objects is evaluated strictly in accordance with the rules of an abstract machine.

- At the end of the program, all the data written to the files should be identical to the result, which is the execution of the program in accordance with abstract semantics.

- The dynamics of entry and exit of interactive devices should take place, as indicated in 7.21.3. The purpose of these requirements is that an unbuffered or string buffer appear as soon as possible to make sure that the prompts actually appear before the program is waiting for input.

This is the observed behavior of the program.

The question is, does initialization ( e , f ) perform as β€œaccess”? As Sander de Dycker pointed out, 6.7.3 says:

What constitutes access to an object that has a type with variable qualifications is determined by the implementation.

which means that before the compiler, you can optimize e and f , but this should be documented!

+8
source

Strictly speaking, any mutable variable whose access (read or write) cannot be optimized in accordance with the C standard. The standard says that access to the volatile object can have unknown side effects and that access to the volatile object must follow the rules of the abstract machine C (where all expressions are evaluated as given by their semantics).

From the mighty Standard (shock):

(C11, 6.7.3p7) "An object that has a mutable type can be modified in ways that are unknown to the implementation or other unknown side effects . Therefore, any expression referring to such an object is evaluated strictly in accordance with the rules of an abstract machine , as described in 5.1. 2.3. "

After that, even a simple variable initialization should be considered as access. Remember that the static specifier also initiates the initialization of the object (up to 0 ) and, thus, gets access.

Now, as you know, compilers behave differently with a mutable qualifier, and I think that many of them simply optimize most of the volatile objects of your sample program, except for those that have an explicit purpose ( = ).

+4
source

Source: https://habr.com/ru/post/980642/


All Articles