effect of goto on C++ compiler optimization

后端 未结 4 2317
执笔经年
执笔经年 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:53

    I was wondering, from a purely compiler optimzation prespective, the result that goto's have on the compiler's optimizer? Does it make code faster, slower, or generally no change in performance compared to using temporaries / flags.

    Why do you care? Your primary concern should be getting your code generator to create the correct code. Efficiency is of much less importance than correctness. Your question should be "Will my use of gotos make my generated code more likely or less likely to be correct?"

    Look at the code generated by lex/yacc or flex/bison. That code is chock full of gotos. There's a good reason for that. lex and yacc implement finite state machines. Since the machine goes to another state at state transitions, the goto is arguably the most natural tool for such transitions.

    There is a simple way to eliminate those gotos in many cases by using a while loop around a switch statement. This is structured code. Per Douglas Jones (Jones D. W., How (not) to code a finite state machine, SIGPLAN Not. 23, 8 (Aug. 1988), 19-22.), this is the worst way to encode a FSM. He argues that a goto-based scheme is better.

    He also argues that there is an even better approach, which is convert your FSM to a control flow diagram using graph theory techniques. That's not always easy. It is an NP hard problem. That's why you still see a lot of FSMs, particularly auto-generated FSMs, implemented as either a loop around a switch or with state transitions implemented via gotos.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-18 21:54

    I agree heartily with David Hammen's answer, but I only have one point to add.

    When people are taught about compilers, they are taught about all the wonderful optimizations that compilers can do.

    They are not taught that the actual value of this depends on who the user is.

    If the code you are writing (or generating) and compiling contains very few function calls and could itself consume a large fraction of some other program's time, then yes, compiler optimization matters.

    If the code being generated contains function calls, or if for some other reason the program counter spends a small fraction of its time in the generated code, it's not worth worrying about. Why? Because even if that code could be so aggressively optimized that it took zero time, it would save no more than that small fraction, and there are probably much bigger performance issues, that the compiler can't fix, that are happy to be evading your attention.

    0 讨论(0)
  • 2021-02-18 21:55

    How do you think that loops are represented, at the assembly level ?

    Using jump instructions to labels...

    Many compilers will actually use jumps even in their Intermediate Representation:

    int loop(int* i) {
      int result = 0;
      while(*i) {
        result += *i;
      }
      return result;
    }
    
    int jump(int* i) {
      int result = 0;
      while (true) {
        if (not *i) { goto end; }
        result += *i;
      }
    
    end:
      return result;
    }
    

    Yields in LLVM:

    define i32 @_Z4loopPi(i32* nocapture %i) nounwind uwtable readonly {
      %1 = load i32* %i, align 4, !tbaa !0
      %2 = icmp eq i32 %1, 0
      br i1 %2, label %3, label %.lr.ph..lr.ph.split_crit_edge
    
    .lr.ph..lr.ph.split_crit_edge:                    ; preds = %.lr.ph..lr.ph.split_crit_edge, %0
      br label %.lr.ph..lr.ph.split_crit_edge
    
    ; <label>:3                                       ; preds = %0
      ret i32 0
    }
    
    define i32 @_Z4jumpPi(i32* nocapture %i) nounwind uwtable readonly {
      %1 = load i32* %i, align 4, !tbaa !0
      %2 = icmp eq i32 %1, 0
      br i1 %2, label %3, label %.lr.ph..lr.ph.split_crit_edge
    
    .lr.ph..lr.ph.split_crit_edge:                    ; preds = %.lr.ph..lr.ph.split_crit_edge, %0
      br label %.lr.ph..lr.ph.split_crit_edge
    
    ; <label>:3                                       ; preds = %0
      ret i32 0
    }
    

    Where br is the branch instruction (a conditional jump).

    All optimizations are performed on this structure. So, goto is the bread and butter of optimizers.

    0 讨论(0)
提交回复
热议问题