How does a modern compiler know that an operation will not have side effects?

I read that compilers will be optimized for operations without side effects. I assume this is being done very conservatively, but how the compiler knows. Does he have a lookup table for the side effects of free operations, or does she do it otherwise?

+3
source share
4 answers

It performs static code analysis at compile time.

Remember that he knows that the code only performs local calculations and does not perform pointer math or does not use global variables or does not change data that is somehow passed by reference. Any functions that it calls should also not be collateral (repeat this).

+5
source

The programmer can also give the compiler some hints: see for example noalias .

+1
source

.

. C restrict, const volatile .

, , , , .

, - .

, , . , ; . . ( , , , ).

+1

(aka pure). . , Java , , . , , .

, , . ( , , sin C, errno .

, , , , - , , . mod-ref- (aka alias analysis pointer analysis, , escape-). :

  • (global x, y->f, *y, **y ..)
  • ,
  • (.. *x = 5;), ,
  • , .

, , .

Note that some languages, such as C ++, provide annotations for cleanliness (a function constnot to be confused with pure virtual functions), and even allow some additional functions, such as mutable. I was told that they are not particularly useful for optimization, although, but I did not write a C ++ compiler, so I do not know why. I would suggest that a separate compilation destroys their pleasure, perhaps.

+1
source

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


All Articles