Why the compiler cannot (always?) optimize code that has GOTO? -


i've written pl/sql programs found goto helpful, jumping out of nested loops, etc.

i've been wondering, if it's bad idea use gotos in other languages. (for example c++.)

i found these questions:

goto still considered harmful?

effect of goto on c++ compiler optimization

there's answer saying: "a compiler typically cannot optimize code contains gotos."

and in other one: "it possible, however, produce flow graph gotos couldn't produced normal flow control statements (loops, switch, etc.)"

i've been told that's goto evil, no 1 told me why, except codes gotos horrible read. well, if no 1 learns, understands how use them because they're "evil", no 1 can read them.

what cases when compiler can , cannot optimize code contains goto? why?

big fat disclaimer:

i've been wondering, if it's bad idea use gotos in other languages. (for example c++.)

except 1 exception (c, mentioned below) coupled great deal of discipline, never idea. it's language-dependent, ask local guru, odds you'll find use case rather slim. err on side of caution. chances language has more idiomatic , clearer way whatever you're inclined use goto for.


it's not case of "cannot ever" or halting-problem-impossible "just impractical". sometimes, if hide loops manually writing equivalent gotos (what stupid thing do!), may work chance. @ other times, if control flow graph resembles spaghetti or otherwise weird/unusual, compiler may not manage optimizing anything.

there's time , thought put creating optimization passes, , time compiler can spend running optimization passes. it's more rewarding focus on used, well-understood control flow patterns built languages (while loop, loops known-at-start trip count, return, switch, chains of else if, etc.). when optimizations happen on ir only supports goto-esque control flow, these patterns lead predictable, recognized disguises , optimizations tuned those. second sentence quote talks about. if code jumps , forth in manner unlike loop, loop unrolling pass won't give time of day if it's clever (the bad kind of clever) way of writing "do 4 times".

until now, i've assumed goto 1 standard c — is, target label known statically , restricted honor other language constructs (e.g. jump within 1 function). there extensions allow jumping arbitrary, dynamically-chosen targets (e.g. "computed goto"). these harder analyse (and optimize), in same ballpark function pointers.

i've been told that's goto evil, no 1 told me why, except codes gotos horrible read. well, if no 1 learns, understands how use them because they're "evil", no 1 can read them.

then told have done bad job. isn't question it, i'll keep brief: 99% of control flow fits few categories warrant specialized control flow constructs. using single construct, goto, cases obscures intent, it's more code , takes longer understand code doing. can experience first hand: write reasonably complicated >100 line batch files (that awful ancient windows scripting language).

ah, 1 more thing:

i found goto helpful, jumping out of nested loops

that potentially fine use, languages provide clearer ways of doing that. alternatively, flatten loop (e.g. python's itertools.combinations) , break, or put nested loop separate function (good idea anyway in many cases) , return.

moreover, these are uses goto. example, error handling in c. these use cases conform simple pattern, 1 not built language, 1 has make without.


Comments