Can a C ++ compiler optimize code when working with pointers?

With these two questions as a background ( first and second ), I wondered how many optimizations the C ++ compiler can perform when working with pointers? More specifically, I'm interested in how smart the compiler is, when it optimizes the code that it can detect, it will never run.

(Some may indicate that this is a fraud of this question , what it can be, but they did not completely answer this specific part. I decided to start a new question that concerns only this problem.)

(I am not an expert in C ++, so I may be mistaken in the following statements, but I will still give him a chance). The C ++ compiler can optimize code snippets that it recognizes, never execute or never exit (e.g. loops). Here is an example:


void test() {
    bool escape = false;

    while ( !escape ); // Will never be exited

    // Do something useful after having escaped
}

The compiler will most likely know that the loop will never be output, since the code never changes the value escape, so the loop will end. This makes the cycle useless.

Now, if we changed the variable to a pointer, would the compiler optimize the loop? Let's say the code looks like this:


void test( bool* escape ) {
    while ( *escape ); // Will this be executed?

    // Do something useful after having escaped
}

, , volatile , ?. , , , , ++, ? , , escape, ? ? , , . , ?

+3
5

(while ( !escape );) label: goto label; (, , ).

(while ( *escape );) , * escape true false , . , , * escape , :

 bool b = *escape;
 label:   if (b) goto label;

volatile '* escape' .

+9

, . " " . . , . , .

, , volatile. SO, , volatile .

+2

, , .

" 1.9". , " ". ( " ", .)

1.9 (6): " - volatile -". , , , .

, , () , , , 2, , , , , , ( Goedel). - , -, .

+2

, ++ . - , . , , . , , .

. , * , , . , . , , , , , * escape .

, , , , . - , .

+1

, . , , , , , , .

, , , . , lfsr, ( ). ( ) , , , , ldr r0, # 0x12345, 0x12345 , , . .

, , . , , , , , , , - , , - , , , - . - :

  ldr r0,[r1]
compare:
  cmp r0,#0
  bne compare

, . ( ), , , "" . , "" ( , , , ).

, - :

void test( bool* escape ) {
    while ( *escape );
}

pretest() {
    bool escape = false;
    test(&escape);
}

, ( , , ). , while (* escape); . - (), - test() , . test() . , .

, , while, - , . - while . , .

, , , .

+1

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


All Articles