1
$\begingroup$

Consider the following C code:

for(int i=0; i<100000; i++) {
    if (operation == 0) {
        a[i] *= 2;
    } else {
        a[i] += 1;
    }
}

Would a real compiler (GCC) be able to do this optimization? If so, what is it called/how is it found?

if (operation == 0) {    
    for(int i=0; i<100000; i++) {        
        a[i] *= 2;        
    }
} else {
    for(int i=0; i<100000; i++) {        
        a[i] += 1;        
    }
}
$\endgroup$

closed as off-topic by David Richerby, Raphael Oct 14 '18 at 13:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development or programming tools are off-topic here, but can be asked on Stack Overflow." – David Richerby, Raphael
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Why don't you try and see? You can look at the assembly code produced by the compiler. $\endgroup$ – Yuval Filmus Oct 13 '18 at 5:17
  • $\begingroup$ Looks like it does with -O3 but not by default $\endgroup$ – cannon Oct 13 '18 at 7:18
  • 1
    $\begingroup$ Voting to close as off-topic. Questions about the principles behind compilers are absolutely on-topic, here, but "Does this actual compiler do this thing?" isn't a question about computer science. $\endgroup$ – David Richerby Oct 14 '18 at 13:12

Browse other questions tagged or ask your own question.