Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the compiler able to optimize spaghetti code?

Tags:

c++

c

gcc

I am writing a compiler that generates C++ code at the end, I can't use while\for or any other normal loop so I translate it to goto\if and assigments\call 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: ;
}

This is really painful to watch, but can GCC compiler compile and optimize code like the one above as if it consisted of normal loops and etc?

like image 430
KugBuBu Avatar asked Dec 26 '22 03:12

KugBuBu


1 Answers

Compilers optimize program control flow with flow graphs analysis, using a goto instead of an if branch at that level is practically equivalent from a compiler's standpoint.

A caveat to keep in mind: since gotos can jump around practically everywhere in your function, if your generator generates irreducible control flow graphs, that might definitely affect the compiler's optimization capabilities.

like image 199
Marco A. Avatar answered Dec 27 '22 18:12

Marco A.