Control-flow graph
A control flow graph (CFG) is used in compilers. It is an abstract representation of a program maintained internally by a compiler. Each node in the graph represents a basic block, i.e. a straight-line piece of code without any jumps or jump targets; jump targets start a block, and jumps end of a block. Directed edges are used to represent jumps in the control flow.
A CFG is a static representation of the program, and represents all alternatives of control flow. So, for example, both arms of an if
statement are represented in the CFG. A cycle in a CFG implies that there is a loop in the code. This allows a compiler to detect non-syntactic loops (loops created with the goto
statement).
The CFG is essential to several compiler optimizations, especially those using relaxation (def-use chaining, use-def chaining).