effect of goto on C++ compiler optimization

后端 未结 4 2318
执笔经年
执笔经年 2021-02-18 21:26

What are the performance benefits or penalties of using goto with a modern C++ compiler?

I am writing a C++ code generator and use of

4条回答
  •  温柔的废话
    2021-02-18 21:54

    The part of a compiler that would be affected works with a flow graph. The syntax you use to create a particular flow graph will normally be irrelevant -- if you create something like a while loop using a goto instead of an actual while statement, it's not going to affect the optimizer at all (by that point, the syntax that produced the flow graph will be long gone).

    It is possible, however, to produce a flow graph with gotos that couldn't be produced by any normal flow control statements (loops, switch, etc.) In such a case, you may produce an irreducible flow graph, and when/if you do, that will often limit the ability of the compiler to optimize the code.

    In other words, if (for example) you took code that was written with normal for, while, switch, etc., and converted it to use goto in every case, but retained the same structure, almost any reasonably modern compiler would probably produce essentially identical code either way. If, however, you use gotos to produce the mess of spaghetti like much of the FORTRAN I had to look at decades ago, then the compiler probably won't be able to do much with it.

提交回复
热议问题