Control flow graph
A control flow graph (CFG) is an abstract data structure used in compilers. It is an abstract representation of a procedure or 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 a block. Directed edges are used to represent jumps in the control flow. There are two specially designated blocks: the entry block, through which control enters into the flow graph, and the exit block, through which all control flow leaves.
The CFG is essential to several compiler optimizations based on global dataflow analysis (def-use chaining, use-def chaining).
A CFG is a static representation of the program, and represents all alternatives of control flow. So, for example, both arms of an
Reachability is another graph property useful in optimization. If a block/subgraph is not connected from the subgraph containing the entry block, that block is unreachable during any execution, and so is dead code; it can be safely removed. If the exit block is unreachable from the entry block, it indicates an infinite loop (not all infinite loops are detectable, of course. See Halting problem). Again, dead code and some infinite loops are possible even if the programmer didn't explicitly code that way: optimizations like constant propagation and constant folding followed by jump threading[?] could collapse multiple basic blocks into one, cause edges to be removed from a CFG, etc., thus possibly disconnecting parts of the graph.
See also: CFG, compiler optimization
IF statement are represented in the CFG. A cycle in a CFG may imply that there is a loop in the code (specifically, a cycle caused by a back edge[?] to a dominator). This allows a compiler to detect non-syntactic loops, such as those created with the GOTO statement. Even if a programmer avoids GOTOs, passes like tail recursion optimization could introduce non-syntactic loops.
Definitions