The as-if rule
De cppreference.com
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Permet à tout et toutes les transformations de code qui ne changent pas le comportement observable du programme
Original:
Allows any and all code transformations that do not change the observable behavior of the program
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Sommaire |
[modifier] Explication
Le compilateur C + + est autorisé à effectuer toutes les modifications apportées au programme, à condition que ce qui suit reste vrai:
Original:
The C++ compiler is permitted to perform any changes to the program as long as the following remains true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- Accès (lectures et écritures) aux objets volatiles se produire dans le même ordre que écrit .Original:Accesses (reads and writes) to the volatile objects occur in the same order as written.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- À la fin du programme, les données écrites sur tous les fichiers, c'est exactement comme si le programme a été exécuté tel qu'il est écrit .Original:At program termination, the data written to all files is exactly as if the program was executed as written.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Toutes les opérations d'entrée et de sortie se produire dans le même ordre et avec le même contenu que si le programme a été exécuté tel qu'il est écrit .Original:All input and output operations occur in the same order and with the same content as if the program was executed as written.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifier] Notes
Parce que le compilateur est (généralement) pas d'analyser le code d'une bibliothèque externe pour déterminer si elle fonctionne ou ne fonctionne pas accès I / O ou volatile, bibliothèque tierce appelle aussi ne sont pas affectés par l'optimisation. Toutefois, les appels de la bibliothèque standard peut être remplacé par d'autres appels, éliminés, ou ajouté au programme lors de l'optimisation .
Original:
Because the compiler is (usually) unable to analyze the code of an external library to determine whether it does or does not perform I/O or volatile access, third-party library calls also aren't affected by optimization. However, standard library calls may be replaced by other calls, eliminated, or added to the program during optimization.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Programmes avec un comportement indéfini, par exemple grâce à l'accès à un tableau hors limites, la modification d'un objet const, Afin d'évaluation violations, etc, sont exempts de la règle, si: ils changent souvent de comportement observable lorsque recompilé avec différents paramètres d'optimisation. Par exemple, si un test de dépassement d'entier signé repose sur le résultat de ce trop-plein, par exemple if(n+1 < n) abort();, it is removed entirely by some compilers parce débordements de signe est un comportement indéfini et l'optimiseur est libre d'assumer cela n'arrive jamais et que le test est redondant .
Original:
Programs with undefined behavior, e.g. due to access to an array out of bounds, modification of a const object, Afin d'évaluation violations, etc, are free from the as-if rule: they often change observable behavior when recompiled with different optimization settings. For example, if a test for signed integer overflow relies on the result of that overflow, e.g. if(n+1 < n) abort();, it is removed entirely by some compilers because débordements de signe est un comportement indéfini and the optimizer is free to assume it never happens and the test is redundant.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Copier élision est le seul bien défini exception à la règle que, si .
Original:
Copier élision is the only well-defined exception from the as-if rule.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifier] Exemple
int& preinc(int& n) { return ++n; } int add(int n, int m) { return n+m; } // volatile input to prevent constant folding volatile int input = 7; // volatile output to make the result a visible side-effect volatile int result; int main() { int n = input; // using built-in operators would invoke undefined behavior // int m = ++n + ++n; // but using functions makes sure the code executes as-if // the functions were not overlapped int m = add(preinc(n), preinc(n)); result = m; }
Résultat :
# full code of the main() function as produced by the GCC compiler # x86 (Intel) platform: movl input(%rip), %eax # eax = input leal 3(%rax,%rax), %eax # eax = 3 + eax + eax movl %eax, result(%rip) # result = eax xorl %eax, %eax # eax = 0 (the return value of main()) ret # PowerPC (IBM) platform: lwz 9,LC..1(2) li 3,0 # r3 = 0 (the return value of main()) lwz 11,0(9) # r11 = input; slwi 11,11,1 # r11 = r11 << 1; addi 0,11,3 # r0 = r11 + 3; stw 0,4(9) # result = r0; blr # Sparc (Sun) platform: sethi %hi(result), %g2 sethi %hi(input), %g1 mov 0, %o0 # o0 = 0 (the return value of main) ld [%g1+%lo(input)], %g1 # g1 = input add %g1, %g1, %g1 # g1 = g1 + g1 add %g1, 3, %g1 # g1 = 3 + g1 st %g1, [%g2+%lo(result)] # result = g1 jmp %o7+8 nop # in all cases, the side effects of preinc() were eliminated, and the # entire main() function was reduced to the equivalent of result = 2*input + 3;