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