Can the compiler optimize spaghetti code?

I am writing a compiler that generates C ++ code at the end, I cannot use while\for or any other normal loop, so I will transfer it to goto\if and assign \ will cause lines like this:

 if (i<b) goto loop_959__again; loop_959__end: ; } { int inumber; int i; i=0; inumber=3; if (!(inumber<30)) goto loop_4482__end; loop_4482__again: float fnumber; _A1__main__increase(__owner); i++; inumber++; fnumber=3; loop_4482__step_begin: if (inumber<30) goto loop_4482__again; loop_4482__end: ; } 

It is very painful to watch, but can the GCC compiler compile and optimize code like the one above, as if it consisted of regular loops, etc.?

+4
source share
1 answer

Compilers optimize the control flow of the program with flow graph analysis, using goto instead of the if branch at this level is almost equivalent from the point of view of the compiler.

Beware of gotos in mind: since gotos can jump almost everywhere in your function, if your generator generates irreducible control flow graphs , it can definitely affect compiler optimization capabilities.

+10
source

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


All Articles