1<!--===- docs/ControlFlowGraph.md 2 3 Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 See https://llvm.org/LICENSE.txt for license information. 5 SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 7--> 8 9# Control Flow Graph 10 11```eval_rst 12.. contents:: 13 :local: 14``` 15 16## Concept 17After a Fortran subprogram has been parsed, its names resolved, and all its 18semantic constraints successfully checked, the parse tree of its 19executable part is translated into another abstract representation, 20namely the _control flow graph_ described in this note. 21 22This second representation of the subprogram's executable part is 23suitable for analysis and incremental modification as the subprogram 24is readied for code generation. 25Many high-level Fortran features are implemented by rewriting portions 26of a subprogram's control flow graph in place. 27 28### Control Flow Graph 29A _control flow graph_ is a collection of simple (_i.e.,_ "non-extended") 30basic _blocks_ that comprise straight-line sequences of _actions_ with a 31single entry point and a single exit point, and a collection of 32directed flow _edges_ (or _arcs_) denoting all possible transitions of 33control flow that may take place during execution from the end of 34one basic block to the beginning of another (or itself). 35 36A block that has multiple distinct successors in the flow of control 37must end with an action that selects its successor. 38 39The sequence of actions that constitutes a basic block may 40include references to user and library procedures. 41Subprogram calls with implicit control flow afterwards, namely 42alternate returns and `END=`/`ERR=` labels on input/output, 43will be lowered in translation to a representation that materializes 44that control flow into something similar to a computed `GO TO` or 45C language `switch` statement. 46 47For convenience in optimization and to simplify the implementation of 48data flow confluence functions, we may choose to maintain the 49property that each flow arc is the sole outbound arc emanating from 50its originating block, the sole inbound arc arriving at its destination, 51or both. 52Empty blocks would inserted to "split" arcs when necessary to maintain this 53invariant property. 54 55Fortran subprograms (other than internal subprograms) can have multiple 56entry points by using the obsolescent `ENTRY` statement. 57We will implement such subprograms by constructing a union 58of their dummy argument lists and using it as part of the definition 59of a new subroutine or function that can be called by each of 60the entry points, which are then all converted into wrapper routines that 61pass a selector value as an additional argument to drive a `switch` on entry 62to the new subprogram. 63 64This transformation ensures that every subprogram's control 65flow graph has a well-defined `START` node. 66 67Statement labels can be used in Fortran on any statement, but only 68the labels that decorate legal destinations of `GO TO` statements 69need to be implemented in the control flow graph. 70Specifically, non-executable statements like `DATA`, `NAMELIST`, and 71`FORMAT` statements will be extracted into data initialization 72records before or during the construction of the control flow 73graph, and will survive only as synonyms for `CONTINUE`. 74 75Nests of multiple labeled `DO` loops that terminate on the same 76label will be have that label rewritten so that `GO TO` within 77the loop nest will arrive at the copy that most closely nests 78the context. 79The Fortran standard does not require us to do this, but XLF 80(at least) works this way. 81 82### Expressions and Statements (Operations and Actions) 83Expressions are trees, not DAGs, of intrinsic operations, 84resolved function references, constant literals, and 85data designators. 86 87Expression nodes are represented in the compiler in a type-safe manner. 88There is a distinct class or class template for every category of 89intrinsic type, templatized over its supported kind type parameter values. 90 91Operands are storage-owning indirections to other instances 92of `Expression`, instances of constant values, and to representations 93of data and function references. 94These indirections are not nullable apart from the situation in which 95the operands of an expression are being removed for use elsewhere before 96the expression is destructed. 97 98The ranks and the extents of the shapes of the results of expressions 99are explicit for constant arrays and recoverable by analysis otherwise. 100 101Parenthesized subexpressions are scrupulously preserved in accordance with 102the Fortran standard. 103 104The expression tree is meant to be a representation that is 105as equally well suited for use in the symbol table (e.g., for 106a bound of an explicit shape array) as it is for an action 107in a basic block of the control flow graph (e.g., the right 108hand side of an assignment statement). 109 110Each basic block comprises a linear sequence of _actions_. 111These are represented as a doubly-linked list so that insertion 112and deletion can be done in constant time. 113 114Only the last action in a basic block can represent a change 115to the flow of control. 116 117### Scope Transitions 118Some of the various scopes of the symbol table are visible in the control flow 119graph as `SCOPE ENTRY` and `SCOPE EXIT` actions. 120`SCOPE ENTRY` actions are unique for their corresponding scopes, 121while `SCOPE EXIT` actions need not be so. 122It must be the case that 123any flow of control within the subprogram will enter only scopes that are 124not yet active, and exit only the most recently entered scope that has not 125yet been deactivated; i.e., when modeled by a push-down stack that is 126pushed by each traversal of a `SCOPE ENTRY` action, 127the entries of the stack are always distinct, only the scope at 128the top of the stack is ever popped by `SCOPE EXIT`, and the stack is empty 129when the subprogram terminates. 130Further, any references to resolved symbols must be to symbols whose scopes 131are active. 132 133The `DEALLOCATE` actions and calls to `FINAL` procedures implied by scoped 134lifetimes will be explicit in the sequence of actions in the control flow 135graph. 136 137Parallel regions might be partially represented by scopes, or by explicit 138operations similar to the scope entry and exit operations. 139 140### Data Flow Representation 141The subprogram text will be in static single assignment form by the time the 142subprogram arrives at the bridge to the LLVM IR builder. 143Merge points are actions at the heads of basic blocks whose operands 144are definition points; definition points are actions at the ends of 145basic blocks whose operands are expression trees (which may refer to 146merge points). 147 148### Rewriting Transformations 149 150#### I/O 151#### Dynamic allocation 152#### Array constructors 153 154#### Derived type initialization, deallocation, and finalization 155The machinery behind the complicated semantics of Fortran's derived types 156and `ALLOCATABLE` objects will be implemented in large part by the run time 157support library. 158 159#### Actual argument temporaries 160#### Array assignments, `WHERE`, and `FORALL` 161 162Array operations have shape. 163 164`WHERE` masks have shape. 165Their effects on array operations are by means of explicit `MASK` operands that 166are part of array assignment operations. 167 168#### Intrinsic function and subroutine calls 169