xref: /llvm-project/flang/docs/ControlFlowGraph.md (revision b7ff03206d668cd5a620a9d4e1b22ea112ed56e3)
1932aae77SSourabh Singh Tomar<!--===- docs/ControlFlowGraph.md
2932aae77SSourabh Singh Tomar
3932aae77SSourabh Singh Tomar   Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4932aae77SSourabh Singh Tomar   See https://llvm.org/LICENSE.txt for license information.
5932aae77SSourabh Singh Tomar   SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6932aae77SSourabh Singh Tomar
7932aae77SSourabh Singh Tomar-->
8932aae77SSourabh Singh Tomar
9271a7bb1SRichard Barton# Control Flow Graph
10271a7bb1SRichard Barton
11*b7ff0320Scor3ntin```{contents}
12*b7ff0320Scor3ntin---
13*b7ff0320Scor3ntinlocal:
14*b7ff0320Scor3ntin---
15271a7bb1SRichard Barton```
16271a7bb1SRichard Barton
17eaff2004Ssameeran joshi## Concept
18eaff2004Ssameeran joshiAfter a Fortran subprogram has been parsed, its names resolved, and all its
19eaff2004Ssameeran joshisemantic constraints successfully checked, the parse tree of its
20eaff2004Ssameeran joshiexecutable part is translated into another abstract representation,
21eaff2004Ssameeran joshinamely the _control flow graph_ described in this note.
22eaff2004Ssameeran joshi
23eaff2004Ssameeran joshiThis second representation of the subprogram's executable part is
24eaff2004Ssameeran joshisuitable for analysis and incremental modification as the subprogram
25eaff2004Ssameeran joshiis readied for code generation.
26eaff2004Ssameeran joshiMany high-level Fortran features are implemented by rewriting portions
27eaff2004Ssameeran joshiof a subprogram's control flow graph in place.
28eaff2004Ssameeran joshi
29eaff2004Ssameeran joshi### Control Flow Graph
30eaff2004Ssameeran joshiA _control flow graph_ is a collection of simple (_i.e.,_ "non-extended")
31eaff2004Ssameeran joshibasic _blocks_ that comprise straight-line sequences of _actions_ with a
32eaff2004Ssameeran joshisingle entry point and a single exit point, and a collection of
33eaff2004Ssameeran joshidirected flow _edges_ (or _arcs_) denoting all possible transitions of
34eaff2004Ssameeran joshicontrol flow that may take place during execution from the end of
35eaff2004Ssameeran joshione basic block to the beginning of another (or itself).
36eaff2004Ssameeran joshi
37eaff2004Ssameeran joshiA block that has multiple distinct successors in the flow of control
38eaff2004Ssameeran joshimust end with an action that selects its successor.
39eaff2004Ssameeran joshi
40eaff2004Ssameeran joshiThe sequence of actions that constitutes a basic block may
41eaff2004Ssameeran joshiinclude references to user and library procedures.
42eaff2004Ssameeran joshiSubprogram calls with implicit control flow afterwards, namely
43eaff2004Ssameeran joshialternate returns and `END=`/`ERR=` labels on input/output,
44eaff2004Ssameeran joshiwill be lowered in translation to a representation that materializes
45eaff2004Ssameeran joshithat control flow into something similar to a computed `GO TO` or
46eaff2004Ssameeran joshiC language `switch` statement.
47eaff2004Ssameeran joshi
48eaff2004Ssameeran joshiFor convenience in optimization and to simplify the implementation of
49eaff2004Ssameeran joshidata flow confluence functions, we may choose to maintain the
50eaff2004Ssameeran joshiproperty that each flow arc is the sole outbound arc emanating from
51eaff2004Ssameeran joshiits originating block, the sole inbound arc arriving at its destination,
52eaff2004Ssameeran joshior both.
53eaff2004Ssameeran joshiEmpty blocks would inserted to "split" arcs when necessary to maintain this
54eaff2004Ssameeran joshiinvariant property.
55eaff2004Ssameeran joshi
56eaff2004Ssameeran joshiFortran subprograms (other than internal subprograms) can have multiple
57eaff2004Ssameeran joshientry points by using the obsolescent `ENTRY` statement.
58eaff2004Ssameeran joshiWe will implement such subprograms by constructing a union
59eaff2004Ssameeran joshiof their dummy argument lists and using it as part of the definition
60eaff2004Ssameeran joshiof a new subroutine or function that can be called by each of
61eaff2004Ssameeran joshithe entry points, which are then all converted into wrapper routines that
62eaff2004Ssameeran joshipass a selector value as an additional argument to drive a `switch` on entry
63eaff2004Ssameeran joshito the new subprogram.
64eaff2004Ssameeran joshi
65eaff2004Ssameeran joshiThis transformation ensures that every subprogram's control
66eaff2004Ssameeran joshiflow graph has a well-defined `START` node.
67eaff2004Ssameeran joshi
68eaff2004Ssameeran joshiStatement labels can be used in Fortran on any statement, but only
69eaff2004Ssameeran joshithe labels that decorate legal destinations of `GO TO` statements
70eaff2004Ssameeran joshineed to be implemented in the control flow graph.
71eaff2004Ssameeran joshiSpecifically, non-executable statements like `DATA`, `NAMELIST`, and
72eaff2004Ssameeran joshi`FORMAT` statements will be extracted into data initialization
73eaff2004Ssameeran joshirecords before or during the construction of the control flow
74eaff2004Ssameeran joshigraph, and will survive only as synonyms for `CONTINUE`.
75eaff2004Ssameeran joshi
76eaff2004Ssameeran joshiNests of multiple labeled `DO` loops that terminate on the same
77eaff2004Ssameeran joshilabel will be have that label rewritten so that `GO TO` within
78eaff2004Ssameeran joshithe loop nest will arrive at the copy that most closely nests
79eaff2004Ssameeran joshithe context.
80eaff2004Ssameeran joshiThe Fortran standard does not require us to do this, but XLF
81eaff2004Ssameeran joshi(at least) works this way.
82eaff2004Ssameeran joshi
83eaff2004Ssameeran joshi### Expressions and Statements (Operations and Actions)
84eaff2004Ssameeran joshiExpressions are trees, not DAGs, of intrinsic operations,
85eaff2004Ssameeran joshiresolved function references, constant literals, and
86eaff2004Ssameeran joshidata designators.
87eaff2004Ssameeran joshi
88eaff2004Ssameeran joshiExpression nodes are represented in the compiler in a type-safe manner.
89eaff2004Ssameeran joshiThere is a distinct class or class template for every category of
90eaff2004Ssameeran joshiintrinsic type, templatized over its supported kind type parameter values.
91eaff2004Ssameeran joshi
92eaff2004Ssameeran joshiOperands are storage-owning indirections to other instances
93eaff2004Ssameeran joshiof `Expression`, instances of constant values, and to representations
94eaff2004Ssameeran joshiof data and function references.
95eaff2004Ssameeran joshiThese indirections are not nullable apart from the situation in which
96eaff2004Ssameeran joshithe operands of an expression are being removed for use elsewhere before
97eaff2004Ssameeran joshithe expression is destructed.
98eaff2004Ssameeran joshi
99eaff2004Ssameeran joshiThe ranks and the extents of the shapes of the results of expressions
100eaff2004Ssameeran joshiare explicit for constant arrays and recoverable by analysis otherwise.
101eaff2004Ssameeran joshi
102eaff2004Ssameeran joshiParenthesized subexpressions are scrupulously preserved in accordance with
103eaff2004Ssameeran joshithe Fortran standard.
104eaff2004Ssameeran joshi
105eaff2004Ssameeran joshiThe expression tree is meant to be a representation that is
106eaff2004Ssameeran joshias equally well suited for use in the symbol table (e.g., for
107eaff2004Ssameeran joshia bound of an explicit shape array) as it is for an action
108eaff2004Ssameeran joshiin a basic block of the control flow graph (e.g., the right
109eaff2004Ssameeran joshihand side of an assignment statement).
110eaff2004Ssameeran joshi
111eaff2004Ssameeran joshiEach basic block comprises a linear sequence of _actions_.
112eaff2004Ssameeran joshiThese are represented as a doubly-linked list so that insertion
113eaff2004Ssameeran joshiand deletion can be done in constant time.
114eaff2004Ssameeran joshi
115eaff2004Ssameeran joshiOnly the last action in a basic block can represent a change
116eaff2004Ssameeran joshito the flow of control.
117eaff2004Ssameeran joshi
118eaff2004Ssameeran joshi### Scope Transitions
119eaff2004Ssameeran joshiSome of the various scopes of the symbol table are visible in the control flow
120eaff2004Ssameeran joshigraph as `SCOPE ENTRY` and `SCOPE EXIT` actions.
121eaff2004Ssameeran joshi`SCOPE ENTRY` actions are unique for their corresponding scopes,
122eaff2004Ssameeran joshiwhile `SCOPE EXIT` actions need not be so.
123eaff2004Ssameeran joshiIt must be the case that
124eaff2004Ssameeran joshiany flow of control within the subprogram will enter only scopes that are
125eaff2004Ssameeran joshinot yet active, and exit only the most recently entered scope that has not
126eaff2004Ssameeran joshiyet been deactivated; i.e., when modeled by a push-down stack that is
127eaff2004Ssameeran joshipushed by each traversal of a `SCOPE ENTRY` action,
128eaff2004Ssameeran joshithe entries of the stack are always distinct, only the scope at
129eaff2004Ssameeran joshithe top of the stack is ever popped by `SCOPE EXIT`, and the stack is empty
130eaff2004Ssameeran joshiwhen the subprogram terminates.
131eaff2004Ssameeran joshiFurther, any references to resolved symbols must be to symbols whose scopes
132eaff2004Ssameeran joshiare active.
133eaff2004Ssameeran joshi
134eaff2004Ssameeran joshiThe `DEALLOCATE` actions and calls to `FINAL` procedures implied by scoped
135eaff2004Ssameeran joshilifetimes will be explicit in the sequence of actions in the control flow
136eaff2004Ssameeran joshigraph.
137eaff2004Ssameeran joshi
138eaff2004Ssameeran joshiParallel regions might be partially represented by scopes, or by explicit
139eaff2004Ssameeran joshioperations similar to the scope entry and exit operations.
140eaff2004Ssameeran joshi
141eaff2004Ssameeran joshi### Data Flow Representation
142eaff2004Ssameeran joshiThe subprogram text will be in static single assignment form by the time the
143eaff2004Ssameeran joshisubprogram arrives at the bridge to the LLVM IR builder.
144eaff2004Ssameeran joshiMerge points are actions at the heads of basic blocks whose operands
145eaff2004Ssameeran joshiare definition points; definition points are actions at the ends of
146eaff2004Ssameeran joshibasic blocks whose operands are expression trees (which may refer to
147eaff2004Ssameeran joshimerge points).
148eaff2004Ssameeran joshi
149eaff2004Ssameeran joshi### Rewriting Transformations
150eaff2004Ssameeran joshi
151eaff2004Ssameeran joshi#### I/O
152eaff2004Ssameeran joshi#### Dynamic allocation
153eaff2004Ssameeran joshi#### Array constructors
154eaff2004Ssameeran joshi
155eaff2004Ssameeran joshi#### Derived type initialization, deallocation, and finalization
156eaff2004Ssameeran joshiThe machinery behind the complicated semantics of Fortran's derived types
157eaff2004Ssameeran joshiand `ALLOCATABLE` objects will be implemented in large part by the run time
158eaff2004Ssameeran joshisupport library.
159eaff2004Ssameeran joshi
160eaff2004Ssameeran joshi#### Actual argument temporaries
161eaff2004Ssameeran joshi#### Array assignments, `WHERE`, and `FORALL`
162eaff2004Ssameeran joshi
163eaff2004Ssameeran joshiArray operations have shape.
164eaff2004Ssameeran joshi
165eaff2004Ssameeran joshi`WHERE` masks have shape.
166eaff2004Ssameeran joshiTheir effects on array operations are by means of explicit `MASK` operands that
167eaff2004Ssameeran joshiare part of array assignment operations.
168eaff2004Ssameeran joshi
169eaff2004Ssameeran joshi#### Intrinsic function and subroutine calls
170