xref: /llvm-project/flang/lib/Lower/PFTBuilder.cpp (revision 518e6f12f37cc47be99c6d218bf07c0191c66de2)
1 //===-- PFTBuilder.cpp ----------------------------------------------------===//
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 #include "flang/Lower/PFTBuilder.h"
10 #include "flang/Lower/IntervalSet.h"
11 #include "flang/Lower/Support/Utils.h"
12 #include "flang/Parser/dump-parse-tree.h"
13 #include "flang/Parser/parse-tree-visitor.h"
14 #include "flang/Semantics/semantics.h"
15 #include "flang/Semantics/tools.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/IntervalMap.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/Debug.h"
20 
21 #define DEBUG_TYPE "flang-pft"
22 
23 static llvm::cl::opt<bool> clDisableStructuredFir(
24     "no-structured-fir", llvm::cl::desc("disable generation of structured FIR"),
25     llvm::cl::init(false), llvm::cl::Hidden);
26 
27 using namespace Fortran;
28 
29 namespace {
30 /// Helpers to unveil parser node inside Fortran::parser::Statement<>,
31 /// Fortran::parser::UnlabeledStatement, and Fortran::common::Indirection<>
32 template <typename A>
33 struct RemoveIndirectionHelper {
34   using Type = A;
35 };
36 template <typename A>
37 struct RemoveIndirectionHelper<common::Indirection<A>> {
38   using Type = A;
39 };
40 
41 template <typename A>
42 struct UnwrapStmt {
43   static constexpr bool isStmt{false};
44 };
45 template <typename A>
46 struct UnwrapStmt<parser::Statement<A>> {
47   static constexpr bool isStmt{true};
48   using Type = typename RemoveIndirectionHelper<A>::Type;
49   constexpr UnwrapStmt(const parser::Statement<A> &a)
50       : unwrapped{removeIndirection(a.statement)}, position{a.source},
51         label{a.label} {}
52   const Type &unwrapped;
53   parser::CharBlock position;
54   std::optional<parser::Label> label;
55 };
56 template <typename A>
57 struct UnwrapStmt<parser::UnlabeledStatement<A>> {
58   static constexpr bool isStmt{true};
59   using Type = typename RemoveIndirectionHelper<A>::Type;
60   constexpr UnwrapStmt(const parser::UnlabeledStatement<A> &a)
61       : unwrapped{removeIndirection(a.statement)}, position{a.source} {}
62   const Type &unwrapped;
63   parser::CharBlock position;
64   std::optional<parser::Label> label;
65 };
66 
67 void dumpScope(const semantics::Scope *scope, int depth = -1);
68 
69 /// The instantiation of a parse tree visitor (Pre and Post) is extremely
70 /// expensive in terms of compile and link time.  So one goal here is to
71 /// limit the bridge to one such instantiation.
72 class PFTBuilder {
73 public:
74   PFTBuilder(const semantics::SemanticsContext &semanticsContext)
75       : pgm{std::make_unique<lower::pft::Program>(
76             semanticsContext.GetCommonBlocks())},
77         semanticsContext{semanticsContext} {
78     lower::pft::PftNode pftRoot{*pgm.get()};
79     pftParentStack.push_back(pftRoot);
80   }
81 
82   /// Get the result
83   std::unique_ptr<lower::pft::Program> result() { return std::move(pgm); }
84 
85   template <typename A>
86   constexpr bool Pre(const A &a) {
87     if constexpr (lower::pft::isFunctionLike<A>) {
88       return enterFunction(a, semanticsContext);
89     } else if constexpr (lower::pft::isConstruct<A> ||
90                          lower::pft::isDirective<A>) {
91       return enterConstructOrDirective(a);
92     } else if constexpr (UnwrapStmt<A>::isStmt) {
93       using T = typename UnwrapStmt<A>::Type;
94       // Node "a" being visited has one of the following types:
95       // Statement<T>, Statement<Indirection<T>>, UnlabeledStatement<T>,
96       // or UnlabeledStatement<Indirection<T>>
97       auto stmt{UnwrapStmt<A>(a)};
98       if constexpr (lower::pft::isConstructStmt<T> ||
99                     lower::pft::isOtherStmt<T>) {
100         addEvaluation(lower::pft::Evaluation{
101             stmt.unwrapped, pftParentStack.back(), stmt.position, stmt.label});
102         return false;
103       } else if constexpr (std::is_same_v<T, parser::ActionStmt>) {
104         return std::visit(
105             common::visitors{
106                 [&](const common::Indirection<parser::IfStmt> &x) {
107                   convertIfStmt(x.value(), stmt.position, stmt.label);
108                   return false;
109                 },
110                 [&](const auto &x) {
111                   addEvaluation(lower::pft::Evaluation{
112                       removeIndirection(x), pftParentStack.back(),
113                       stmt.position, stmt.label});
114                   return true;
115                 },
116             },
117             stmt.unwrapped.u);
118       }
119     }
120     return true;
121   }
122 
123   /// Convert an IfStmt into an IfConstruct, retaining the IfStmt as the
124   /// first statement of the construct.
125   void convertIfStmt(const parser::IfStmt &ifStmt, parser::CharBlock position,
126                      std::optional<parser::Label> label) {
127     // Generate a skeleton IfConstruct parse node.  Its components are never
128     // referenced.  The actual components are available via the IfConstruct
129     // evaluation's nested evaluationList, with the ifStmt in the position of
130     // the otherwise normal IfThenStmt.  Caution: All other PFT nodes reference
131     // front end generated parse nodes; this is an exceptional case.
132     static const auto ifConstruct = parser::IfConstruct{
133         parser::Statement<parser::IfThenStmt>{
134             std::nullopt,
135             parser::IfThenStmt{
136                 std::optional<parser::Name>{},
137                 parser::ScalarLogicalExpr{parser::LogicalExpr{parser::Expr{
138                     parser::LiteralConstant{parser::LogicalLiteralConstant{
139                         false, std::optional<parser::KindParam>{}}}}}}}},
140         parser::Block{}, std::list<parser::IfConstruct::ElseIfBlock>{},
141         std::optional<parser::IfConstruct::ElseBlock>{},
142         parser::Statement<parser::EndIfStmt>{std::nullopt,
143                                              parser::EndIfStmt{std::nullopt}}};
144     enterConstructOrDirective(ifConstruct);
145     addEvaluation(
146         lower::pft::Evaluation{ifStmt, pftParentStack.back(), position, label});
147     Pre(std::get<parser::UnlabeledStatement<parser::ActionStmt>>(ifStmt.t));
148     static const auto endIfStmt = parser::EndIfStmt{std::nullopt};
149     addEvaluation(
150         lower::pft::Evaluation{endIfStmt, pftParentStack.back(), {}, {}});
151     exitConstructOrDirective();
152   }
153 
154   template <typename A>
155   constexpr void Post(const A &) {
156     if constexpr (lower::pft::isFunctionLike<A>) {
157       exitFunction();
158     } else if constexpr (lower::pft::isConstruct<A> ||
159                          lower::pft::isDirective<A>) {
160       exitConstructOrDirective();
161     }
162   }
163 
164   // Module like
165   bool Pre(const parser::Module &node) { return enterModule(node); }
166   bool Pre(const parser::Submodule &node) { return enterModule(node); }
167 
168   void Post(const parser::Module &) { exitModule(); }
169   void Post(const parser::Submodule &) { exitModule(); }
170 
171   // Block data
172   bool Pre(const parser::BlockData &node) {
173     addUnit(lower::pft::BlockDataUnit{node, pftParentStack.back(),
174                                       semanticsContext});
175     return false;
176   }
177 
178   // Get rid of production wrapper
179   bool Pre(const parser::Statement<parser::ForallAssignmentStmt> &statement) {
180     addEvaluation(std::visit(
181         [&](const auto &x) {
182           return lower::pft::Evaluation{x, pftParentStack.back(),
183                                         statement.source, statement.label};
184         },
185         statement.statement.u));
186     return false;
187   }
188   bool Pre(const parser::WhereBodyConstruct &whereBody) {
189     return std::visit(
190         common::visitors{
191             [&](const parser::Statement<parser::AssignmentStmt> &stmt) {
192               // Not caught as other AssignmentStmt because it is not
193               // wrapped in a parser::ActionStmt.
194               addEvaluation(lower::pft::Evaluation{stmt.statement,
195                                                    pftParentStack.back(),
196                                                    stmt.source, stmt.label});
197               return false;
198             },
199             [&](const auto &) { return true; },
200         },
201         whereBody.u);
202   }
203 
204   // CompilerDirective have special handling in case they are top level
205   // directives (i.e. they do not belong to a ProgramUnit).
206   bool Pre(const parser::CompilerDirective &directive) {
207     assert(pftParentStack.size() > 0 &&
208            "At least the Program must be a parent");
209     if (pftParentStack.back().isA<lower::pft::Program>()) {
210       addUnit(
211           lower::pft::CompilerDirectiveUnit(directive, pftParentStack.back()));
212       return false;
213     }
214     return enterConstructOrDirective(directive);
215   }
216 
217 private:
218   /// Initialize a new module-like unit and make it the builder's focus.
219   template <typename A>
220   bool enterModule(const A &mod) {
221     Fortran::lower::pft::ModuleLikeUnit &unit =
222         addUnit(lower::pft::ModuleLikeUnit{mod, pftParentStack.back()});
223     functionList = &unit.nestedFunctions;
224     pushEvaluationList(&unit.evaluationList);
225     pftParentStack.emplace_back(unit);
226     LLVM_DEBUG(dumpScope(&unit.getScope()));
227     return true;
228   }
229 
230   void exitModule() {
231     if (!evaluationListStack.empty())
232       popEvaluationList();
233     pftParentStack.pop_back();
234     resetFunctionState();
235   }
236 
237   /// Add the end statement Evaluation of a sub/program to the PFT.
238   /// There may be intervening internal subprogram definitions between
239   /// prior statements and this end statement.
240   void endFunctionBody() {
241     if (evaluationListStack.empty())
242       return;
243     auto evaluationList = evaluationListStack.back();
244     if (evaluationList->empty() || !evaluationList->back().isEndStmt()) {
245       const auto &endStmt =
246           pftParentStack.back().get<lower::pft::FunctionLikeUnit>().endStmt;
247       endStmt.visit(common::visitors{
248           [&](const parser::Statement<parser::EndProgramStmt> &s) {
249             addEvaluation(lower::pft::Evaluation{
250                 s.statement, pftParentStack.back(), s.source, s.label});
251           },
252           [&](const parser::Statement<parser::EndFunctionStmt> &s) {
253             addEvaluation(lower::pft::Evaluation{
254                 s.statement, pftParentStack.back(), s.source, s.label});
255           },
256           [&](const parser::Statement<parser::EndSubroutineStmt> &s) {
257             addEvaluation(lower::pft::Evaluation{
258                 s.statement, pftParentStack.back(), s.source, s.label});
259           },
260           [&](const parser::Statement<parser::EndMpSubprogramStmt> &s) {
261             addEvaluation(lower::pft::Evaluation{
262                 s.statement, pftParentStack.back(), s.source, s.label});
263           },
264           [&](const auto &s) {
265             llvm::report_fatal_error("missing end statement or unexpected "
266                                      "begin statement reference");
267           },
268       });
269     }
270     lastLexicalEvaluation = nullptr;
271   }
272 
273   /// Pop the ModuleLikeUnit evaluationList when entering the first module
274   /// procedure.
275   void cleanModuleEvaluationList() {
276     if (evaluationListStack.empty())
277       return;
278     if (pftParentStack.back().isA<lower::pft::ModuleLikeUnit>())
279       popEvaluationList();
280   }
281 
282   /// Initialize a new function-like unit and make it the builder's focus.
283   template <typename A>
284   bool enterFunction(const A &func,
285                      const semantics::SemanticsContext &semanticsContext) {
286     cleanModuleEvaluationList();
287     endFunctionBody(); // enclosing host subprogram body, if any
288     Fortran::lower::pft::FunctionLikeUnit &unit =
289         addFunction(lower::pft::FunctionLikeUnit{func, pftParentStack.back(),
290                                                  semanticsContext});
291     labelEvaluationMap = &unit.labelEvaluationMap;
292     assignSymbolLabelMap = &unit.assignSymbolLabelMap;
293     functionList = &unit.nestedFunctions;
294     pushEvaluationList(&unit.evaluationList);
295     pftParentStack.emplace_back(unit);
296     LLVM_DEBUG(dumpScope(&unit.getScope()));
297     return true;
298   }
299 
300   void exitFunction() {
301     rewriteIfGotos();
302     endFunctionBody();
303     analyzeBranches(nullptr, *evaluationListStack.back()); // add branch links
304     processEntryPoints();
305     popEvaluationList();
306     labelEvaluationMap = nullptr;
307     assignSymbolLabelMap = nullptr;
308     pftParentStack.pop_back();
309     resetFunctionState();
310   }
311 
312   /// Initialize a new construct or directive and make it the builder's focus.
313   template <typename A>
314   bool enterConstructOrDirective(const A &constructOrDirective) {
315     Fortran::lower::pft::Evaluation &eval = addEvaluation(
316         lower::pft::Evaluation{constructOrDirective, pftParentStack.back()});
317     eval.evaluationList.reset(new lower::pft::EvaluationList);
318     pushEvaluationList(eval.evaluationList.get());
319     pftParentStack.emplace_back(eval);
320     constructAndDirectiveStack.emplace_back(&eval);
321     return true;
322   }
323 
324   void exitConstructOrDirective() {
325     rewriteIfGotos();
326     auto *eval = constructAndDirectiveStack.back();
327     if (eval->isExecutableDirective()) {
328       // A construct at the end of an (unstructured) OpenACC or OpenMP
329       // construct region must have an exit target inside the region.
330       Fortran::lower::pft::EvaluationList &evaluationList =
331           *eval->evaluationList;
332       if (!evaluationList.empty() && evaluationList.back().isConstruct()) {
333         static const parser::ContinueStmt exitTarget{};
334         addEvaluation(
335             lower::pft::Evaluation{exitTarget, pftParentStack.back(), {}, {}});
336       }
337     }
338     popEvaluationList();
339     pftParentStack.pop_back();
340     constructAndDirectiveStack.pop_back();
341   }
342 
343   /// Reset function state to that of an enclosing host function.
344   void resetFunctionState() {
345     if (!pftParentStack.empty()) {
346       pftParentStack.back().visit(common::visitors{
347           [&](lower::pft::FunctionLikeUnit &p) {
348             functionList = &p.nestedFunctions;
349             labelEvaluationMap = &p.labelEvaluationMap;
350             assignSymbolLabelMap = &p.assignSymbolLabelMap;
351           },
352           [&](lower::pft::ModuleLikeUnit &p) {
353             functionList = &p.nestedFunctions;
354           },
355           [&](auto &) { functionList = nullptr; },
356       });
357     }
358   }
359 
360   template <typename A>
361   A &addUnit(A &&unit) {
362     pgm->getUnits().emplace_back(std::move(unit));
363     return std::get<A>(pgm->getUnits().back());
364   }
365 
366   template <typename A>
367   A &addFunction(A &&func) {
368     if (functionList) {
369       functionList->emplace_back(std::move(func));
370       return functionList->back();
371     }
372     return addUnit(std::move(func));
373   }
374 
375   // ActionStmt has a couple of non-conforming cases, explicitly handled here.
376   // The other cases use an Indirection, which are discarded in the PFT.
377   lower::pft::Evaluation
378   makeEvaluationAction(const parser::ActionStmt &statement,
379                        parser::CharBlock position,
380                        std::optional<parser::Label> label) {
381     return std::visit(
382         common::visitors{
383             [&](const auto &x) {
384               return lower::pft::Evaluation{
385                   removeIndirection(x), pftParentStack.back(), position, label};
386             },
387         },
388         statement.u);
389   }
390 
391   /// Append an Evaluation to the end of the current list.
392   lower::pft::Evaluation &addEvaluation(lower::pft::Evaluation &&eval) {
393     assert(functionList && "not in a function");
394     assert(!evaluationListStack.empty() && "empty evaluation list stack");
395     if (!constructAndDirectiveStack.empty())
396       eval.parentConstruct = constructAndDirectiveStack.back();
397     auto &entryPointList = eval.getOwningProcedure()->entryPointList;
398     evaluationListStack.back()->emplace_back(std::move(eval));
399     lower::pft::Evaluation *p = &evaluationListStack.back()->back();
400     if (p->isActionStmt() || p->isConstructStmt() || p->isEndStmt() ||
401         p->isExecutableDirective()) {
402       if (lastLexicalEvaluation) {
403         lastLexicalEvaluation->lexicalSuccessor = p;
404         p->printIndex = lastLexicalEvaluation->printIndex + 1;
405       } else {
406         p->printIndex = 1;
407       }
408       lastLexicalEvaluation = p;
409       for (std::size_t entryIndex = entryPointList.size() - 1;
410            entryIndex && !entryPointList[entryIndex].second->lexicalSuccessor;
411            --entryIndex)
412         // Link to the entry's first executable statement.
413         entryPointList[entryIndex].second->lexicalSuccessor = p;
414     } else if (const auto *entryStmt = p->getIf<parser::EntryStmt>()) {
415       const semantics::Symbol *sym =
416           std::get<parser::Name>(entryStmt->t).symbol;
417       if (auto *details = sym->detailsIf<semantics::GenericDetails>())
418         sym = details->specific();
419       assert(sym->has<semantics::SubprogramDetails>() &&
420              "entry must be a subprogram");
421       entryPointList.push_back(std::pair{sym, p});
422     }
423     if (p->label.has_value())
424       labelEvaluationMap->try_emplace(*p->label, p);
425     return evaluationListStack.back()->back();
426   }
427 
428   /// push a new list on the stack of Evaluation lists
429   void pushEvaluationList(lower::pft::EvaluationList *evaluationList) {
430     assert(functionList && "not in a function");
431     assert(evaluationList && evaluationList->empty() &&
432            "evaluation list isn't correct");
433     evaluationListStack.emplace_back(evaluationList);
434   }
435 
436   /// pop the current list and return to the last Evaluation list
437   void popEvaluationList() {
438     assert(functionList && "not in a function");
439     evaluationListStack.pop_back();
440   }
441 
442   /// Rewrite IfConstructs containing a GotoStmt or CycleStmt to eliminate an
443   /// unstructured branch and a trivial basic block.  The pre-branch-analysis
444   /// code:
445   ///
446   ///       <<IfConstruct>>
447   ///         1 If[Then]Stmt: if(cond) goto L
448   ///         2 GotoStmt: goto L
449   ///         3 EndIfStmt
450   ///       <<End IfConstruct>>
451   ///       4 Statement: ...
452   ///       5 Statement: ...
453   ///       6 Statement: L ...
454   ///
455   /// becomes:
456   ///
457   ///       <<IfConstruct>>
458   ///         1 If[Then]Stmt [negate]: if(cond) goto L
459   ///         4 Statement: ...
460   ///         5 Statement: ...
461   ///         3 EndIfStmt
462   ///       <<End IfConstruct>>
463   ///       6 Statement: L ...
464   ///
465   /// The If[Then]Stmt condition is implicitly negated.  It is not modified
466   /// in the PFT.  It must be negated when generating FIR.  The GotoStmt or
467   /// CycleStmt is deleted.
468   ///
469   /// The transformation is only valid for forward branch targets at the same
470   /// construct nesting level as the IfConstruct.  The result must not violate
471   /// construct nesting requirements or contain an EntryStmt.  The result
472   /// is subject to normal un/structured code classification analysis.  The
473   /// result is allowed to violate the F18 Clause 11.1.2.1 prohibition on
474   /// transfer of control into the interior of a construct block, as that does
475   /// not compromise correct code generation.  When two transformation
476   /// candidates overlap, at least one must be disallowed.  In such cases,
477   /// the current heuristic favors simple code generation, which happens to
478   /// favor later candidates over earlier candidates.  That choice is probably
479   /// not significant, but could be changed.
480   ///
481   void rewriteIfGotos() {
482     auto &evaluationList = *evaluationListStack.back();
483     if (!evaluationList.size())
484       return;
485     struct T {
486       lower::pft::EvaluationList::iterator ifConstructIt;
487       parser::Label ifTargetLabel;
488       bool isCycleStmt = false;
489     };
490     llvm::SmallVector<T> ifCandidateStack;
491     const auto *doStmt =
492         evaluationList.begin()->getIf<parser::NonLabelDoStmt>();
493     std::string doName = doStmt ? getConstructName(*doStmt) : std::string{};
494     for (auto it = evaluationList.begin(), end = evaluationList.end();
495          it != end; ++it) {
496       auto &eval = *it;
497       if (eval.isA<parser::EntryStmt>() || eval.isIntermediateConstructStmt()) {
498         ifCandidateStack.clear();
499         continue;
500       }
501       auto firstStmt = [](lower::pft::Evaluation *e) {
502         return e->isConstruct() ? &*e->evaluationList->begin() : e;
503       };
504       const Fortran::lower::pft::Evaluation &targetEval = *firstStmt(&eval);
505       bool targetEvalIsEndDoStmt = targetEval.isA<parser::EndDoStmt>();
506       auto branchTargetMatch = [&]() {
507         if (const parser::Label targetLabel =
508                 ifCandidateStack.back().ifTargetLabel)
509           if (targetLabel == *targetEval.label)
510             return true; // goto target match
511         if (targetEvalIsEndDoStmt && ifCandidateStack.back().isCycleStmt)
512           return true; // cycle target match
513         return false;
514       };
515       if (targetEval.label || targetEvalIsEndDoStmt) {
516         while (!ifCandidateStack.empty() && branchTargetMatch()) {
517           lower::pft::EvaluationList::iterator ifConstructIt =
518               ifCandidateStack.back().ifConstructIt;
519           lower::pft::EvaluationList::iterator successorIt =
520               std::next(ifConstructIt);
521           if (successorIt != it) {
522             Fortran::lower::pft::EvaluationList &ifBodyList =
523                 *ifConstructIt->evaluationList;
524             lower::pft::EvaluationList::iterator branchStmtIt =
525                 std::next(ifBodyList.begin());
526             assert((branchStmtIt->isA<parser::GotoStmt>() ||
527                     branchStmtIt->isA<parser::CycleStmt>()) &&
528                    "expected goto or cycle statement");
529             ifBodyList.erase(branchStmtIt);
530             lower::pft::Evaluation &ifStmt = *ifBodyList.begin();
531             ifStmt.negateCondition = true;
532             ifStmt.lexicalSuccessor = firstStmt(&*successorIt);
533             lower::pft::EvaluationList::iterator endIfStmtIt =
534                 std::prev(ifBodyList.end());
535             std::prev(it)->lexicalSuccessor = &*endIfStmtIt;
536             endIfStmtIt->lexicalSuccessor = firstStmt(&*it);
537             ifBodyList.splice(endIfStmtIt, evaluationList, successorIt, it);
538             for (; successorIt != endIfStmtIt; ++successorIt)
539               successorIt->parentConstruct = &*ifConstructIt;
540           }
541           ifCandidateStack.pop_back();
542         }
543       }
544       if (eval.isA<parser::IfConstruct>() && eval.evaluationList->size() == 3) {
545         const auto bodyEval = std::next(eval.evaluationList->begin());
546         if (const auto *gotoStmt = bodyEval->getIf<parser::GotoStmt>()) {
547           ifCandidateStack.push_back({it, gotoStmt->v});
548         } else if (doStmt) {
549           if (const auto *cycleStmt = bodyEval->getIf<parser::CycleStmt>()) {
550             std::string cycleName = getConstructName(*cycleStmt);
551             if (cycleName.empty() || cycleName == doName)
552               // This candidate will match doStmt's EndDoStmt.
553               ifCandidateStack.push_back({it, {}, true});
554           }
555         }
556       }
557     }
558   }
559 
560   /// Mark IO statement ERR, EOR, and END specifier branch targets.
561   /// Mark an IO statement with an assigned format as unstructured.
562   template <typename A>
563   void analyzeIoBranches(lower::pft::Evaluation &eval, const A &stmt) {
564     auto analyzeFormatSpec = [&](const parser::Format &format) {
565       if (const auto *expr = std::get_if<parser::Expr>(&format.u)) {
566         if (semantics::ExprHasTypeCategory(*semantics::GetExpr(*expr),
567                                            common::TypeCategory::Integer))
568           eval.isUnstructured = true;
569       }
570     };
571     auto analyzeSpecs{[&](const auto &specList) {
572       for (const auto &spec : specList) {
573         std::visit(
574             Fortran::common::visitors{
575                 [&](const Fortran::parser::Format &format) {
576                   analyzeFormatSpec(format);
577                 },
578                 [&](const auto &label) {
579                   using LabelNodes =
580                       std::tuple<parser::ErrLabel, parser::EorLabel,
581                                  parser::EndLabel>;
582                   if constexpr (common::HasMember<decltype(label), LabelNodes>)
583                     markBranchTarget(eval, label.v);
584                 }},
585             spec.u);
586       }
587     }};
588 
589     using OtherIOStmts =
590         std::tuple<parser::BackspaceStmt, parser::CloseStmt,
591                    parser::EndfileStmt, parser::FlushStmt, parser::OpenStmt,
592                    parser::RewindStmt, parser::WaitStmt>;
593 
594     if constexpr (std::is_same_v<A, parser::ReadStmt> ||
595                   std::is_same_v<A, parser::WriteStmt>) {
596       if (stmt.format)
597         analyzeFormatSpec(*stmt.format);
598       analyzeSpecs(stmt.controls);
599     } else if constexpr (std::is_same_v<A, parser::PrintStmt>) {
600       analyzeFormatSpec(std::get<parser::Format>(stmt.t));
601     } else if constexpr (std::is_same_v<A, parser::InquireStmt>) {
602       if (const auto *specList =
603               std::get_if<std::list<parser::InquireSpec>>(&stmt.u))
604         analyzeSpecs(*specList);
605     } else if constexpr (common::HasMember<A, OtherIOStmts>) {
606       analyzeSpecs(stmt.v);
607     } else {
608       // Always crash if this is instantiated
609       static_assert(!std::is_same_v<A, parser::ReadStmt>,
610                     "Unexpected IO statement");
611     }
612   }
613 
614   /// Set the exit of a construct, possibly from multiple enclosing constructs.
615   void setConstructExit(lower::pft::Evaluation &eval) {
616     eval.constructExit = &eval.evaluationList->back().nonNopSuccessor();
617   }
618 
619   /// Mark the target of a branch as a new block.
620   void markBranchTarget(lower::pft::Evaluation &sourceEvaluation,
621                         lower::pft::Evaluation &targetEvaluation) {
622     sourceEvaluation.isUnstructured = true;
623     if (!sourceEvaluation.controlSuccessor)
624       sourceEvaluation.controlSuccessor = &targetEvaluation;
625     targetEvaluation.isNewBlock = true;
626     // If this is a branch into the body of a construct (usually illegal,
627     // but allowed in some legacy cases), then the targetEvaluation and its
628     // ancestors must be marked as unstructured.
629     lower::pft::Evaluation *sourceConstruct = sourceEvaluation.parentConstruct;
630     lower::pft::Evaluation *targetConstruct = targetEvaluation.parentConstruct;
631     if (targetConstruct &&
632         &targetConstruct->getFirstNestedEvaluation() == &targetEvaluation)
633       // A branch to an initial constructStmt is a branch to the construct.
634       targetConstruct = targetConstruct->parentConstruct;
635     if (targetConstruct) {
636       while (sourceConstruct && sourceConstruct != targetConstruct)
637         sourceConstruct = sourceConstruct->parentConstruct;
638       if (sourceConstruct != targetConstruct) // branch into a construct body
639         for (lower::pft::Evaluation *eval = &targetEvaluation; eval;
640              eval = eval->parentConstruct) {
641           eval->isUnstructured = true;
642           // If the branch is a backward branch into an already analyzed
643           // DO or IF construct, mark the construct exit as a new block.
644           // For a forward branch, the isUnstructured flag will cause this
645           // to be done when the construct is analyzed.
646           if (eval->constructExit && (eval->isA<parser::DoConstruct>() ||
647                                       eval->isA<parser::IfConstruct>()))
648             eval->constructExit->isNewBlock = true;
649         }
650     }
651   }
652   void markBranchTarget(lower::pft::Evaluation &sourceEvaluation,
653                         parser::Label label) {
654     assert(label && "missing branch target label");
655     lower::pft::Evaluation *targetEvaluation{
656         labelEvaluationMap->find(label)->second};
657     assert(targetEvaluation && "missing branch target evaluation");
658     markBranchTarget(sourceEvaluation, *targetEvaluation);
659   }
660 
661   /// Mark the successor of an Evaluation as a new block.
662   void markSuccessorAsNewBlock(lower::pft::Evaluation &eval) {
663     eval.nonNopSuccessor().isNewBlock = true;
664   }
665 
666   template <typename A>
667   inline std::string getConstructName(const A &stmt) {
668     using MaybeConstructNameWrapper =
669         std::tuple<parser::BlockStmt, parser::CycleStmt, parser::ElseStmt,
670                    parser::ElsewhereStmt, parser::EndAssociateStmt,
671                    parser::EndBlockStmt, parser::EndCriticalStmt,
672                    parser::EndDoStmt, parser::EndForallStmt, parser::EndIfStmt,
673                    parser::EndSelectStmt, parser::EndWhereStmt,
674                    parser::ExitStmt>;
675     if constexpr (common::HasMember<A, MaybeConstructNameWrapper>) {
676       if (stmt.v)
677         return stmt.v->ToString();
678     }
679 
680     using MaybeConstructNameInTuple = std::tuple<
681         parser::AssociateStmt, parser::CaseStmt, parser::ChangeTeamStmt,
682         parser::CriticalStmt, parser::ElseIfStmt, parser::EndChangeTeamStmt,
683         parser::ForallConstructStmt, parser::IfThenStmt, parser::LabelDoStmt,
684         parser::MaskedElsewhereStmt, parser::NonLabelDoStmt,
685         parser::SelectCaseStmt, parser::SelectRankCaseStmt,
686         parser::TypeGuardStmt, parser::WhereConstructStmt>;
687     if constexpr (common::HasMember<A, MaybeConstructNameInTuple>) {
688       if (auto name = std::get<std::optional<parser::Name>>(stmt.t))
689         return name->ToString();
690     }
691 
692     // These statements have multiple std::optional<parser::Name> elements.
693     if constexpr (std::is_same_v<A, parser::SelectRankStmt> ||
694                   std::is_same_v<A, parser::SelectTypeStmt>) {
695       if (auto name = std::get<0>(stmt.t))
696         return name->ToString();
697     }
698 
699     return {};
700   }
701 
702   /// \p parentConstruct can be null if this statement is at the highest
703   /// level of a program.
704   template <typename A>
705   void insertConstructName(const A &stmt,
706                            lower::pft::Evaluation *parentConstruct) {
707     std::string name = getConstructName(stmt);
708     if (!name.empty())
709       constructNameMap[name] = parentConstruct;
710   }
711 
712   /// Insert branch links for a list of Evaluations.
713   /// \p parentConstruct can be null if the evaluationList contains the
714   /// top-level statements of a program.
715   void analyzeBranches(lower::pft::Evaluation *parentConstruct,
716                        std::list<lower::pft::Evaluation> &evaluationList) {
717     lower::pft::Evaluation *lastConstructStmtEvaluation{};
718     for (auto &eval : evaluationList) {
719       eval.visit(common::visitors{
720           // Action statements (except IO statements)
721           [&](const parser::CallStmt &s) {
722             // Look for alternate return specifiers.
723             const auto &args =
724                 std::get<std::list<parser::ActualArgSpec>>(s.v.t);
725             for (const auto &arg : args) {
726               const auto &actual = std::get<parser::ActualArg>(arg.t);
727               if (const auto *altReturn =
728                       std::get_if<parser::AltReturnSpec>(&actual.u))
729                 markBranchTarget(eval, altReturn->v);
730             }
731           },
732           [&](const parser::CycleStmt &s) {
733             std::string name = getConstructName(s);
734             lower::pft::Evaluation *construct{name.empty()
735                                                   ? doConstructStack.back()
736                                                   : constructNameMap[name]};
737             assert(construct && "missing CYCLE construct");
738             markBranchTarget(eval, construct->evaluationList->back());
739           },
740           [&](const parser::ExitStmt &s) {
741             std::string name = getConstructName(s);
742             lower::pft::Evaluation *construct{name.empty()
743                                                   ? doConstructStack.back()
744                                                   : constructNameMap[name]};
745             assert(construct && "missing EXIT construct");
746             markBranchTarget(eval, *construct->constructExit);
747           },
748           [&](const parser::FailImageStmt &) {
749             eval.isUnstructured = true;
750             if (eval.lexicalSuccessor->lexicalSuccessor)
751               markSuccessorAsNewBlock(eval);
752           },
753           [&](const parser::GotoStmt &s) { markBranchTarget(eval, s.v); },
754           [&](const parser::IfStmt &) {
755             eval.lexicalSuccessor->isNewBlock = true;
756             lastConstructStmtEvaluation = &eval;
757           },
758           [&](const parser::ReturnStmt &) {
759             eval.isUnstructured = true;
760             if (eval.lexicalSuccessor->lexicalSuccessor)
761               markSuccessorAsNewBlock(eval);
762           },
763           [&](const parser::StopStmt &) {
764             eval.isUnstructured = true;
765             if (eval.lexicalSuccessor->lexicalSuccessor)
766               markSuccessorAsNewBlock(eval);
767           },
768           [&](const parser::ComputedGotoStmt &s) {
769             for (auto &label : std::get<std::list<parser::Label>>(s.t))
770               markBranchTarget(eval, label);
771           },
772           [&](const parser::ArithmeticIfStmt &s) {
773             markBranchTarget(eval, std::get<1>(s.t));
774             markBranchTarget(eval, std::get<2>(s.t));
775             markBranchTarget(eval, std::get<3>(s.t));
776           },
777           [&](const parser::AssignStmt &s) { // legacy label assignment
778             auto &label = std::get<parser::Label>(s.t);
779             const auto *sym = std::get<parser::Name>(s.t).symbol;
780             assert(sym && "missing AssignStmt symbol");
781             lower::pft::Evaluation *target{
782                 labelEvaluationMap->find(label)->second};
783             assert(target && "missing branch target evaluation");
784             if (!target->isA<parser::FormatStmt>())
785               target->isNewBlock = true;
786             auto iter = assignSymbolLabelMap->find(*sym);
787             if (iter == assignSymbolLabelMap->end()) {
788               lower::pft::LabelSet labelSet{};
789               labelSet.insert(label);
790               assignSymbolLabelMap->try_emplace(*sym, labelSet);
791             } else {
792               iter->second.insert(label);
793             }
794           },
795           [&](const parser::AssignedGotoStmt &) {
796             // Although this statement is a branch, it doesn't have any
797             // explicit control successors.  So the code at the end of the
798             // loop won't mark the successor.  Do that here.
799             eval.isUnstructured = true;
800             markSuccessorAsNewBlock(eval);
801           },
802 
803           // The first executable statement after an EntryStmt is a new block.
804           [&](const parser::EntryStmt &) {
805             eval.lexicalSuccessor->isNewBlock = true;
806           },
807 
808           // Construct statements
809           [&](const parser::AssociateStmt &s) {
810             insertConstructName(s, parentConstruct);
811           },
812           [&](const parser::BlockStmt &s) {
813             insertConstructName(s, parentConstruct);
814           },
815           [&](const parser::SelectCaseStmt &s) {
816             insertConstructName(s, parentConstruct);
817             lastConstructStmtEvaluation = &eval;
818           },
819           [&](const parser::CaseStmt &) {
820             eval.isNewBlock = true;
821             lastConstructStmtEvaluation->controlSuccessor = &eval;
822             lastConstructStmtEvaluation = &eval;
823           },
824           [&](const parser::EndSelectStmt &) {
825             eval.nonNopSuccessor().isNewBlock = true;
826             lastConstructStmtEvaluation = nullptr;
827           },
828           [&](const parser::ChangeTeamStmt &s) {
829             insertConstructName(s, parentConstruct);
830           },
831           [&](const parser::CriticalStmt &s) {
832             insertConstructName(s, parentConstruct);
833           },
834           [&](const parser::NonLabelDoStmt &s) {
835             insertConstructName(s, parentConstruct);
836             doConstructStack.push_back(parentConstruct);
837             const auto &loopControl =
838                 std::get<std::optional<parser::LoopControl>>(s.t);
839             if (!loopControl.has_value()) {
840               eval.isUnstructured = true; // infinite loop
841               return;
842             }
843             eval.nonNopSuccessor().isNewBlock = true;
844             eval.controlSuccessor = &evaluationList.back();
845             if (const auto *bounds =
846                     std::get_if<parser::LoopControl::Bounds>(&loopControl->u)) {
847               if (bounds->name.thing.symbol->GetType()->IsNumeric(
848                       common::TypeCategory::Real))
849                 eval.isUnstructured = true; // real-valued loop control
850             } else if (std::get_if<parser::ScalarLogicalExpr>(
851                            &loopControl->u)) {
852               eval.isUnstructured = true; // while loop
853             }
854           },
855           [&](const parser::EndDoStmt &) {
856             lower::pft::Evaluation &doEval = evaluationList.front();
857             eval.controlSuccessor = &doEval;
858             doConstructStack.pop_back();
859             if (parentConstruct->lowerAsStructured())
860               return;
861             // The loop is unstructured, which wasn't known for all cases when
862             // visiting the NonLabelDoStmt.
863             parentConstruct->constructExit->isNewBlock = true;
864             const auto &doStmt = *doEval.getIf<parser::NonLabelDoStmt>();
865             const auto &loopControl =
866                 std::get<std::optional<parser::LoopControl>>(doStmt.t);
867             if (!loopControl.has_value())
868               return; // infinite loop
869             if (const auto *concurrent =
870                     std::get_if<parser::LoopControl::Concurrent>(
871                         &loopControl->u)) {
872               // If there is a mask, the EndDoStmt starts a new block.
873               const auto &header =
874                   std::get<parser::ConcurrentHeader>(concurrent->t);
875               eval.isNewBlock |=
876                   std::get<std::optional<parser::ScalarLogicalExpr>>(header.t)
877                       .has_value();
878             }
879           },
880           [&](const parser::IfThenStmt &s) {
881             insertConstructName(s, parentConstruct);
882             eval.lexicalSuccessor->isNewBlock = true;
883             lastConstructStmtEvaluation = &eval;
884           },
885           [&](const parser::ElseIfStmt &) {
886             eval.isNewBlock = true;
887             eval.lexicalSuccessor->isNewBlock = true;
888             lastConstructStmtEvaluation->controlSuccessor = &eval;
889             lastConstructStmtEvaluation = &eval;
890           },
891           [&](const parser::ElseStmt &) {
892             eval.isNewBlock = true;
893             lastConstructStmtEvaluation->controlSuccessor = &eval;
894             lastConstructStmtEvaluation = nullptr;
895           },
896           [&](const parser::EndIfStmt &) {
897             if (parentConstruct->lowerAsUnstructured())
898               parentConstruct->constructExit->isNewBlock = true;
899             if (lastConstructStmtEvaluation) {
900               lastConstructStmtEvaluation->controlSuccessor =
901                   parentConstruct->constructExit;
902               lastConstructStmtEvaluation = nullptr;
903             }
904           },
905           [&](const parser::SelectRankStmt &s) {
906             insertConstructName(s, parentConstruct);
907             lastConstructStmtEvaluation = &eval;
908           },
909           [&](const parser::SelectRankCaseStmt &) {
910             eval.isNewBlock = true;
911             lastConstructStmtEvaluation->controlSuccessor = &eval;
912             lastConstructStmtEvaluation = &eval;
913           },
914           [&](const parser::SelectTypeStmt &s) {
915             insertConstructName(s, parentConstruct);
916             lastConstructStmtEvaluation = &eval;
917           },
918           [&](const parser::TypeGuardStmt &) {
919             eval.isNewBlock = true;
920             lastConstructStmtEvaluation->controlSuccessor = &eval;
921             lastConstructStmtEvaluation = &eval;
922           },
923 
924           // Constructs - set (unstructured) construct exit targets
925           [&](const parser::AssociateConstruct &) { setConstructExit(eval); },
926           [&](const parser::BlockConstruct &) {
927             // EndBlockStmt may have code.
928             eval.constructExit = &eval.evaluationList->back();
929           },
930           [&](const parser::CaseConstruct &) {
931             setConstructExit(eval);
932             eval.isUnstructured = true;
933           },
934           [&](const parser::ChangeTeamConstruct &) {
935             // EndChangeTeamStmt may have code.
936             eval.constructExit = &eval.evaluationList->back();
937           },
938           [&](const parser::CriticalConstruct &) {
939             // EndCriticalStmt may have code.
940             eval.constructExit = &eval.evaluationList->back();
941           },
942           [&](const parser::DoConstruct &) { setConstructExit(eval); },
943           [&](const parser::ForallConstruct &) { setConstructExit(eval); },
944           [&](const parser::IfConstruct &) { setConstructExit(eval); },
945           [&](const parser::SelectRankConstruct &) {
946             setConstructExit(eval);
947             eval.isUnstructured = true;
948           },
949           [&](const parser::SelectTypeConstruct &) {
950             setConstructExit(eval);
951             eval.isUnstructured = true;
952           },
953           [&](const parser::WhereConstruct &) { setConstructExit(eval); },
954 
955           // Default - Common analysis for IO statements; otherwise nop.
956           [&](const auto &stmt) {
957             using A = std::decay_t<decltype(stmt)>;
958             using IoStmts = std::tuple<
959                 parser::BackspaceStmt, parser::CloseStmt, parser::EndfileStmt,
960                 parser::FlushStmt, parser::InquireStmt, parser::OpenStmt,
961                 parser::PrintStmt, parser::ReadStmt, parser::RewindStmt,
962                 parser::WaitStmt, parser::WriteStmt>;
963             if constexpr (common::HasMember<A, IoStmts>)
964               analyzeIoBranches(eval, stmt);
965           },
966       });
967 
968       // Analyze construct evaluations.
969       if (eval.evaluationList)
970         analyzeBranches(&eval, *eval.evaluationList);
971 
972       // Set the successor of the last statement in an IF or SELECT block.
973       if (!eval.controlSuccessor && eval.lexicalSuccessor &&
974           eval.lexicalSuccessor->isIntermediateConstructStmt()) {
975         eval.controlSuccessor = parentConstruct->constructExit;
976         eval.lexicalSuccessor->isNewBlock = true;
977       }
978 
979       // Propagate isUnstructured flag to enclosing construct.
980       if (parentConstruct && eval.isUnstructured)
981         parentConstruct->isUnstructured = true;
982 
983       // The successor of a branch starts a new block.
984       if (eval.controlSuccessor && eval.isActionStmt() &&
985           eval.lowerAsUnstructured())
986         markSuccessorAsNewBlock(eval);
987     }
988   }
989 
990   /// Do processing specific to subprograms with multiple entry points.
991   void processEntryPoints() {
992     lower::pft::Evaluation *initialEval = &evaluationListStack.back()->front();
993     lower::pft::FunctionLikeUnit *unit = initialEval->getOwningProcedure();
994     int entryCount = unit->entryPointList.size();
995     if (entryCount == 1)
996       return;
997 
998     // The first executable statement in the subprogram is preceded by a
999     // branch to the entry point, so it starts a new block.
1000     if (initialEval->hasNestedEvaluations())
1001       initialEval = &initialEval->getFirstNestedEvaluation();
1002     else if (initialEval->isA<Fortran::parser::EntryStmt>())
1003       initialEval = initialEval->lexicalSuccessor;
1004     initialEval->isNewBlock = true;
1005 
1006     // All function entry points share a single result container.
1007     // Find one of the largest results.
1008     for (int entryIndex = 0; entryIndex < entryCount; ++entryIndex) {
1009       unit->setActiveEntry(entryIndex);
1010       const auto &details =
1011           unit->getSubprogramSymbol().get<semantics::SubprogramDetails>();
1012       if (details.isFunction()) {
1013         const semantics::Symbol *resultSym = &details.result();
1014         assert(resultSym && "missing result symbol");
1015         if (!unit->primaryResult ||
1016             unit->primaryResult->size() < resultSym->size())
1017           unit->primaryResult = resultSym;
1018       }
1019     }
1020     unit->setActiveEntry(0);
1021   }
1022 
1023   std::unique_ptr<lower::pft::Program> pgm;
1024   std::vector<lower::pft::PftNode> pftParentStack;
1025   const semantics::SemanticsContext &semanticsContext;
1026 
1027   /// functionList points to the internal or module procedure function list
1028   /// of a FunctionLikeUnit or a ModuleLikeUnit.  It may be null.
1029   std::list<lower::pft::FunctionLikeUnit> *functionList{};
1030   std::vector<lower::pft::Evaluation *> constructAndDirectiveStack{};
1031   std::vector<lower::pft::Evaluation *> doConstructStack{};
1032   /// evaluationListStack is the current nested construct evaluationList state.
1033   std::vector<lower::pft::EvaluationList *> evaluationListStack{};
1034   llvm::DenseMap<parser::Label, lower::pft::Evaluation *> *labelEvaluationMap{};
1035   lower::pft::SymbolLabelMap *assignSymbolLabelMap{};
1036   std::map<std::string, lower::pft::Evaluation *> constructNameMap{};
1037   lower::pft::Evaluation *lastLexicalEvaluation{};
1038 };
1039 
1040 /// Dump all program scopes and symbols with addresses to disambiguate names.
1041 /// This is static, unchanging front end information, so dump it only once.
1042 void dumpScope(const semantics::Scope *scope, int depth) {
1043   static int initialVisitCounter = 0;
1044   if (depth < 0) {
1045     if (++initialVisitCounter != 1)
1046       return;
1047     while (!scope->IsGlobal())
1048       scope = &scope->parent();
1049     LLVM_DEBUG(llvm::dbgs() << "Full program scope information.\n"
1050                                "Addresses in angle brackets are scopes. "
1051                                "Unbracketed addresses are symbols.\n");
1052   }
1053   static const std::string white{"                                      ++"};
1054   std::string w = white.substr(0, depth * 2);
1055   if (depth >= 0) {
1056     LLVM_DEBUG(llvm::dbgs() << w << "<" << scope << "> ");
1057     if (auto *sym{scope->symbol()}) {
1058       LLVM_DEBUG(llvm::dbgs() << sym << " " << *sym << "\n");
1059     } else {
1060       if (scope->IsIntrinsicModules()) {
1061         LLVM_DEBUG(llvm::dbgs() << "IntrinsicModules (no detail)\n");
1062         return;
1063       }
1064       LLVM_DEBUG(llvm::dbgs() << "[anonymous]\n");
1065     }
1066   }
1067   for (const auto &scp : scope->children())
1068     if (!scp.symbol())
1069       dumpScope(&scp, depth + 1);
1070   for (auto iter = scope->begin(); iter != scope->end(); ++iter) {
1071     common::Reference<semantics::Symbol> sym = iter->second;
1072     if (auto scp = sym->scope())
1073       dumpScope(scp, depth + 1);
1074     else
1075       LLVM_DEBUG(llvm::dbgs() << w + "  " << &*sym << "   " << *sym << "\n");
1076   }
1077 }
1078 
1079 class PFTDumper {
1080 public:
1081   void dumpPFT(llvm::raw_ostream &outputStream,
1082                const lower::pft::Program &pft) {
1083     for (auto &unit : pft.getUnits()) {
1084       std::visit(common::visitors{
1085                      [&](const lower::pft::BlockDataUnit &unit) {
1086                        outputStream << getNodeIndex(unit) << " ";
1087                        outputStream << "BlockData: ";
1088                        outputStream << "\nEnd BlockData\n\n";
1089                      },
1090                      [&](const lower::pft::FunctionLikeUnit &func) {
1091                        dumpFunctionLikeUnit(outputStream, func);
1092                      },
1093                      [&](const lower::pft::ModuleLikeUnit &unit) {
1094                        dumpModuleLikeUnit(outputStream, unit);
1095                      },
1096                      [&](const lower::pft::CompilerDirectiveUnit &unit) {
1097                        dumpCompilerDirectiveUnit(outputStream, unit);
1098                      },
1099                  },
1100                  unit);
1101     }
1102   }
1103 
1104   llvm::StringRef evaluationName(const lower::pft::Evaluation &eval) {
1105     return eval.visit([](const auto &parseTreeNode) {
1106       return parser::ParseTreeDumper::GetNodeName(parseTreeNode);
1107     });
1108   }
1109 
1110   void dumpEvaluation(llvm::raw_ostream &outputStream,
1111                       const lower::pft::Evaluation &eval,
1112                       const std::string &indentString, int indent = 1) {
1113     llvm::StringRef name = evaluationName(eval);
1114     llvm::StringRef newBlock = eval.isNewBlock ? "^" : "";
1115     llvm::StringRef bang = eval.isUnstructured ? "!" : "";
1116     outputStream << indentString;
1117     if (eval.printIndex)
1118       outputStream << eval.printIndex << ' ';
1119     if (eval.hasNestedEvaluations())
1120       outputStream << "<<" << newBlock << name << bang << ">>";
1121     else
1122       outputStream << newBlock << name << bang;
1123     if (eval.negateCondition)
1124       outputStream << " [negate]";
1125     if (eval.constructExit)
1126       outputStream << " -> " << eval.constructExit->printIndex;
1127     else if (eval.controlSuccessor)
1128       outputStream << " -> " << eval.controlSuccessor->printIndex;
1129     else if (eval.isA<parser::EntryStmt>() && eval.lexicalSuccessor)
1130       outputStream << " -> " << eval.lexicalSuccessor->printIndex;
1131     if (!eval.position.empty())
1132       outputStream << ": " << eval.position.ToString();
1133     else if (auto *dir = eval.getIf<Fortran::parser::CompilerDirective>())
1134       outputStream << ": !" << dir->source.ToString();
1135     outputStream << '\n';
1136     if (eval.hasNestedEvaluations()) {
1137       dumpEvaluationList(outputStream, *eval.evaluationList, indent + 1);
1138       outputStream << indentString << "<<End " << name << bang << ">>\n";
1139     }
1140   }
1141 
1142   void dumpEvaluation(llvm::raw_ostream &ostream,
1143                       const lower::pft::Evaluation &eval) {
1144     dumpEvaluation(ostream, eval, "");
1145   }
1146 
1147   void dumpEvaluationList(llvm::raw_ostream &outputStream,
1148                           const lower::pft::EvaluationList &evaluationList,
1149                           int indent = 1) {
1150     static const auto white = "                                      ++"s;
1151     auto indentString = white.substr(0, indent * 2);
1152     for (const lower::pft::Evaluation &eval : evaluationList)
1153       dumpEvaluation(outputStream, eval, indentString, indent);
1154   }
1155 
1156   void
1157   dumpFunctionLikeUnit(llvm::raw_ostream &outputStream,
1158                        const lower::pft::FunctionLikeUnit &functionLikeUnit) {
1159     outputStream << getNodeIndex(functionLikeUnit) << " ";
1160     llvm::StringRef unitKind;
1161     llvm::StringRef name;
1162     llvm::StringRef header;
1163     if (functionLikeUnit.beginStmt) {
1164       functionLikeUnit.beginStmt->visit(common::visitors{
1165           [&](const parser::Statement<parser::ProgramStmt> &stmt) {
1166             unitKind = "Program";
1167             name = toStringRef(stmt.statement.v.source);
1168           },
1169           [&](const parser::Statement<parser::FunctionStmt> &stmt) {
1170             unitKind = "Function";
1171             name = toStringRef(std::get<parser::Name>(stmt.statement.t).source);
1172             header = toStringRef(stmt.source);
1173           },
1174           [&](const parser::Statement<parser::SubroutineStmt> &stmt) {
1175             unitKind = "Subroutine";
1176             name = toStringRef(std::get<parser::Name>(stmt.statement.t).source);
1177             header = toStringRef(stmt.source);
1178           },
1179           [&](const parser::Statement<parser::MpSubprogramStmt> &stmt) {
1180             unitKind = "MpSubprogram";
1181             name = toStringRef(stmt.statement.v.source);
1182             header = toStringRef(stmt.source);
1183           },
1184           [&](const auto &) { llvm_unreachable("not a valid begin stmt"); },
1185       });
1186     } else {
1187       unitKind = "Program";
1188       name = "<anonymous>";
1189     }
1190     outputStream << unitKind << ' ' << name;
1191     if (!header.empty())
1192       outputStream << ": " << header;
1193     outputStream << '\n';
1194     dumpEvaluationList(outputStream, functionLikeUnit.evaluationList);
1195     if (!functionLikeUnit.nestedFunctions.empty()) {
1196       outputStream << "\nContains\n";
1197       for (const lower::pft::FunctionLikeUnit &func :
1198            functionLikeUnit.nestedFunctions)
1199         dumpFunctionLikeUnit(outputStream, func);
1200       outputStream << "End Contains\n";
1201     }
1202     outputStream << "End " << unitKind << ' ' << name << "\n\n";
1203   }
1204 
1205   void dumpModuleLikeUnit(llvm::raw_ostream &outputStream,
1206                           const lower::pft::ModuleLikeUnit &moduleLikeUnit) {
1207     outputStream << getNodeIndex(moduleLikeUnit) << " ";
1208     llvm::StringRef unitKind;
1209     llvm::StringRef name;
1210     llvm::StringRef header;
1211     moduleLikeUnit.beginStmt.visit(common::visitors{
1212         [&](const parser::Statement<parser::ModuleStmt> &stmt) {
1213           unitKind = "Module";
1214           name = toStringRef(stmt.statement.v.source);
1215           header = toStringRef(stmt.source);
1216         },
1217         [&](const parser::Statement<parser::SubmoduleStmt> &stmt) {
1218           unitKind = "Submodule";
1219           name = toStringRef(std::get<parser::Name>(stmt.statement.t).source);
1220           header = toStringRef(stmt.source);
1221         },
1222         [&](const auto &) {
1223           llvm_unreachable("not a valid module begin stmt");
1224         },
1225     });
1226     outputStream << unitKind << ' ' << name << ": " << header << '\n';
1227     dumpEvaluationList(outputStream, moduleLikeUnit.evaluationList);
1228     outputStream << "Contains\n";
1229     for (const lower::pft::FunctionLikeUnit &func :
1230          moduleLikeUnit.nestedFunctions)
1231       dumpFunctionLikeUnit(outputStream, func);
1232     outputStream << "End Contains\nEnd " << unitKind << ' ' << name << "\n\n";
1233   }
1234 
1235   // Top level directives
1236   void dumpCompilerDirectiveUnit(
1237       llvm::raw_ostream &outputStream,
1238       const lower::pft::CompilerDirectiveUnit &directive) {
1239     outputStream << getNodeIndex(directive) << " ";
1240     outputStream << "CompilerDirective: !";
1241     outputStream << directive.get<Fortran::parser::CompilerDirective>()
1242                         .source.ToString();
1243     outputStream << "\nEnd CompilerDirective\n\n";
1244   }
1245 
1246   template <typename T>
1247   std::size_t getNodeIndex(const T &node) {
1248     auto addr = static_cast<const void *>(&node);
1249     auto it = nodeIndexes.find(addr);
1250     if (it != nodeIndexes.end())
1251       return it->second;
1252     nodeIndexes.try_emplace(addr, nextIndex);
1253     return nextIndex++;
1254   }
1255   std::size_t getNodeIndex(const lower::pft::Program &) { return 0; }
1256 
1257 private:
1258   llvm::DenseMap<const void *, std::size_t> nodeIndexes;
1259   std::size_t nextIndex{1}; // 0 is the root
1260 };
1261 
1262 } // namespace
1263 
1264 template <typename A, typename T>
1265 static lower::pft::FunctionLikeUnit::FunctionStatement
1266 getFunctionStmt(const T &func) {
1267   lower::pft::FunctionLikeUnit::FunctionStatement result{
1268       std::get<parser::Statement<A>>(func.t)};
1269   return result;
1270 }
1271 
1272 template <typename A, typename T>
1273 static lower::pft::ModuleLikeUnit::ModuleStatement getModuleStmt(const T &mod) {
1274   lower::pft::ModuleLikeUnit::ModuleStatement result{
1275       std::get<parser::Statement<A>>(mod.t)};
1276   return result;
1277 }
1278 
1279 template <typename A>
1280 static const semantics::Symbol *getSymbol(A &beginStmt) {
1281   const auto *symbol = beginStmt.visit(common::visitors{
1282       [](const parser::Statement<parser::ProgramStmt> &stmt)
1283           -> const semantics::Symbol * { return stmt.statement.v.symbol; },
1284       [](const parser::Statement<parser::FunctionStmt> &stmt)
1285           -> const semantics::Symbol * {
1286         return std::get<parser::Name>(stmt.statement.t).symbol;
1287       },
1288       [](const parser::Statement<parser::SubroutineStmt> &stmt)
1289           -> const semantics::Symbol * {
1290         return std::get<parser::Name>(stmt.statement.t).symbol;
1291       },
1292       [](const parser::Statement<parser::MpSubprogramStmt> &stmt)
1293           -> const semantics::Symbol * { return stmt.statement.v.symbol; },
1294       [](const parser::Statement<parser::ModuleStmt> &stmt)
1295           -> const semantics::Symbol * { return stmt.statement.v.symbol; },
1296       [](const parser::Statement<parser::SubmoduleStmt> &stmt)
1297           -> const semantics::Symbol * {
1298         return std::get<parser::Name>(stmt.statement.t).symbol;
1299       },
1300       [](const auto &) -> const semantics::Symbol * {
1301         llvm_unreachable("unknown FunctionLike or ModuleLike beginStmt");
1302         return nullptr;
1303       }});
1304   assert(symbol && "parser::Name must have resolved symbol");
1305   return symbol;
1306 }
1307 
1308 bool Fortran::lower::pft::Evaluation::lowerAsStructured() const {
1309   return !lowerAsUnstructured();
1310 }
1311 
1312 bool Fortran::lower::pft::Evaluation::lowerAsUnstructured() const {
1313   return isUnstructured || clDisableStructuredFir;
1314 }
1315 
1316 lower::pft::FunctionLikeUnit *
1317 Fortran::lower::pft::Evaluation::getOwningProcedure() const {
1318   return parent.visit(common::visitors{
1319       [](lower::pft::FunctionLikeUnit &c) { return &c; },
1320       [&](lower::pft::Evaluation &c) { return c.getOwningProcedure(); },
1321       [](auto &) -> lower::pft::FunctionLikeUnit * { return nullptr; },
1322   });
1323 }
1324 
1325 bool Fortran::lower::definedInCommonBlock(const semantics::Symbol &sym) {
1326   return semantics::FindCommonBlockContaining(sym);
1327 }
1328 
1329 /// Is the symbol `sym` a global?
1330 bool Fortran::lower::symbolIsGlobal(const semantics::Symbol &sym) {
1331   return semantics::IsSaved(sym) || lower::definedInCommonBlock(sym) ||
1332          semantics::IsNamedConstant(sym);
1333 }
1334 
1335 namespace {
1336 /// This helper class sorts the symbols in a scope such that a symbol will
1337 /// be placed after those it depends upon. Otherwise the sort is stable and
1338 /// preserves the order of the symbol table, which is sorted by name. This
1339 /// analysis may also be done for an individual symbol.
1340 struct SymbolDependenceAnalysis {
1341   explicit SymbolDependenceAnalysis(const semantics::Scope &scope) {
1342     analyzeEquivalenceSets(scope);
1343     for (const auto &iter : scope)
1344       analyze(iter.second.get());
1345     finalize();
1346   }
1347   explicit SymbolDependenceAnalysis(const semantics::Symbol &symbol) {
1348     analyzeEquivalenceSets(symbol.owner());
1349     analyze(symbol);
1350     finalize();
1351   }
1352   Fortran::lower::pft::VariableList getVariableList() {
1353     return std::move(layeredVarList[0]);
1354   }
1355 
1356 private:
1357   /// Analyze the equivalence sets defined in \p scope, plus the equivalence
1358   /// sets in host module, submodule, and procedure scopes that may define
1359   /// symbols referenced in \p scope. This analysis excludes equivalence sets
1360   /// involving common blocks, which are handled elsewhere.
1361   void analyzeEquivalenceSets(const semantics::Scope &scope) {
1362     // FIXME: When this function is called on the scope of an internal
1363     // procedure whose parent contains an EQUIVALENCE set and the internal
1364     // procedure uses variables from that EQUIVALENCE set, we end up creating
1365     // an AggregateStore for those variables unnecessarily.
1366 
1367     // A function defined in a [sub]module has no explicit USE of its ancestor
1368     // [sub]modules. Analyze those scopes here to accommodate references to
1369     // symbols in them.
1370     for (auto *scp = &scope.parent(); !scp->IsGlobal(); scp = &scp->parent())
1371       if (scp->kind() == Fortran::semantics::Scope::Kind::Module)
1372         analyzeLocalEquivalenceSets(*scp);
1373     // Analyze local, USEd, and host procedure scope equivalences.
1374     for (const auto &iter : scope) {
1375       const semantics::Symbol &ultimate = iter.second.get().GetUltimate();
1376       if (!skipSymbol(ultimate))
1377         analyzeLocalEquivalenceSets(ultimate.owner());
1378     }
1379     // Add all aggregate stores to the front of the variable list.
1380     adjustSize(1);
1381     // The copy in the loop matters, 'stores' will still be used.
1382     for (auto st : stores)
1383       layeredVarList[0].emplace_back(std::move(st));
1384   }
1385 
1386   /// Analyze the equivalence sets defined locally in \p scope that don't
1387   /// involve common blocks.
1388   void analyzeLocalEquivalenceSets(const semantics::Scope &scope) {
1389     if (scope.equivalenceSets().empty())
1390       return; // no equivalence sets to analyze
1391     if (analyzedScopes.find(&scope) != analyzedScopes.end())
1392       return; // equivalence sets already analyzed
1393 
1394     analyzedScopes.insert(&scope);
1395     std::list<std::list<semantics::SymbolRef>> aggregates =
1396         Fortran::semantics::GetStorageAssociations(scope);
1397     for (std::list<semantics::SymbolRef> aggregate : aggregates) {
1398       const Fortran::semantics::Symbol *aggregateSym = nullptr;
1399       bool isGlobal = false;
1400       const semantics::Symbol &first = *aggregate.front();
1401       // Exclude equivalence sets involving common blocks.
1402       // Those are handled in instantiateCommon.
1403       if (lower::definedInCommonBlock(first))
1404         continue;
1405       std::size_t start = first.offset();
1406       std::size_t end = first.offset() + first.size();
1407       const Fortran::semantics::Symbol *namingSym = nullptr;
1408       for (semantics::SymbolRef symRef : aggregate) {
1409         const semantics::Symbol &sym = *symRef;
1410         aliasSyms.insert(&sym);
1411         if (sym.test(Fortran::semantics::Symbol::Flag::CompilerCreated)) {
1412           aggregateSym = &sym;
1413         } else {
1414           isGlobal |= lower::symbolIsGlobal(sym);
1415           start = std::min(sym.offset(), start);
1416           end = std::max(sym.offset() + sym.size(), end);
1417           if (!namingSym || (sym.name() < namingSym->name()))
1418             namingSym = &sym;
1419         }
1420       }
1421       assert(namingSym && "must contain at least one user symbol");
1422       if (!aggregateSym) {
1423         stores.emplace_back(
1424             Fortran::lower::pft::Variable::Interval{start, end - start},
1425             *namingSym, isGlobal);
1426       } else {
1427         stores.emplace_back(*aggregateSym, *namingSym, isGlobal);
1428       }
1429     }
1430   }
1431 
1432   // Recursively visit each symbol to determine the height of its dependence on
1433   // other symbols.
1434   int analyze(const semantics::Symbol &sym) {
1435     auto done = seen.insert(&sym);
1436     if (!done.second)
1437       return 0;
1438     LLVM_DEBUG(llvm::dbgs() << "analyze symbol " << &sym << " in <"
1439                             << &sym.owner() << ">: " << sym << '\n');
1440     const bool isProcedurePointerOrDummy =
1441         semantics::IsProcedurePointer(sym) ||
1442         (semantics::IsProcedure(sym) && IsDummy(sym));
1443     // A procedure argument in a subprogram with multiple entry points might
1444     // need a layeredVarList entry to trigger creation of a symbol map entry
1445     // in some cases.  Non-dummy procedures don't.
1446     if (semantics::IsProcedure(sym) && !isProcedurePointerOrDummy)
1447       return 0;
1448     semantics::Symbol ultimate = sym.GetUltimate();
1449     if (const auto *details =
1450             ultimate.detailsIf<semantics::NamelistDetails>()) {
1451       // handle namelist group symbols
1452       for (const semantics::SymbolRef &s : details->objects())
1453         analyze(s);
1454       return 0;
1455     }
1456     if (!ultimate.has<semantics::ObjectEntityDetails>() &&
1457         !isProcedurePointerOrDummy)
1458       return 0;
1459 
1460     if (sym.has<semantics::DerivedTypeDetails>())
1461       llvm_unreachable("not yet implemented - derived type analysis");
1462 
1463     // Symbol must be something lowering will have to allocate.
1464     int depth = 0;
1465     // Analyze symbols appearing in object entity specification expressions.
1466     // This ensures these symbols will be instantiated before the current one.
1467     // This is not done for object entities that are host associated because
1468     // they must be instantiated from the value of the host symbols.
1469     // (The specification expressions should not be re-evaluated.)
1470     if (const auto *details = sym.detailsIf<semantics::ObjectEntityDetails>()) {
1471       const semantics::DeclTypeSpec *symTy = sym.GetType();
1472       assert(symTy && "symbol must have a type");
1473       // check CHARACTER's length
1474       if (symTy->category() == semantics::DeclTypeSpec::Character)
1475         if (auto e = symTy->characterTypeSpec().length().GetExplicit())
1476           for (const auto &s : evaluate::CollectSymbols(*e))
1477             depth = std::max(analyze(s) + 1, depth);
1478 
1479       auto doExplicit = [&](const auto &bound) {
1480         if (bound.isExplicit()) {
1481           semantics::SomeExpr e{*bound.GetExplicit()};
1482           for (const auto &s : evaluate::CollectSymbols(e))
1483             depth = std::max(analyze(s) + 1, depth);
1484         }
1485       };
1486       // Handle any symbols in array bound declarations.
1487       for (const semantics::ShapeSpec &subs : details->shape()) {
1488         doExplicit(subs.lbound());
1489         doExplicit(subs.ubound());
1490       }
1491       // Handle any symbols in coarray bound declarations.
1492       for (const semantics::ShapeSpec &subs : details->coshape()) {
1493         doExplicit(subs.lbound());
1494         doExplicit(subs.ubound());
1495       }
1496       // Handle any symbols in initialization expressions.
1497       if (auto e = details->init())
1498         for (const auto &s : evaluate::CollectSymbols(*e))
1499           depth = std::max(analyze(s) + 1, depth);
1500     }
1501     adjustSize(depth + 1);
1502     bool global = lower::symbolIsGlobal(sym);
1503     layeredVarList[depth].emplace_back(sym, global, depth);
1504     if (semantics::IsAllocatable(sym))
1505       layeredVarList[depth].back().setHeapAlloc();
1506     if (semantics::IsPointer(sym))
1507       layeredVarList[depth].back().setPointer();
1508     if (ultimate.attrs().test(semantics::Attr::TARGET))
1509       layeredVarList[depth].back().setTarget();
1510 
1511     // If there are alias sets, then link the participating variables to their
1512     // aggregate stores when constructing the new variable on the list.
1513     if (lower::pft::Variable::AggregateStore *store = findStoreIfAlias(sym))
1514       layeredVarList[depth].back().setAlias(store->getOffset());
1515     return depth;
1516   }
1517 
1518   /// Skip symbol in alias analysis.
1519   bool skipSymbol(const semantics::Symbol &sym) {
1520     // Common block equivalences are largely managed by the front end.
1521     // Compiler generated symbols ('.' names) cannot be equivalenced.
1522     // FIXME: Equivalence code generation may need to be revisited.
1523     return !sym.has<semantics::ObjectEntityDetails>() ||
1524            lower::definedInCommonBlock(sym) || sym.name()[0] == '.';
1525   }
1526 
1527   // Make sure the table is of appropriate size.
1528   void adjustSize(std::size_t size) {
1529     if (layeredVarList.size() < size)
1530       layeredVarList.resize(size);
1531   }
1532 
1533   Fortran::lower::pft::Variable::AggregateStore *
1534   findStoreIfAlias(const Fortran::evaluate::Symbol &sym) {
1535     const semantics::Symbol &ultimate = sym.GetUltimate();
1536     const semantics::Scope &scope = ultimate.owner();
1537     // Expect the total number of EQUIVALENCE sets to be small for a typical
1538     // Fortran program.
1539     if (aliasSyms.find(&ultimate) != aliasSyms.end()) {
1540       LLVM_DEBUG(llvm::dbgs() << "found aggregate containing " << &ultimate
1541                               << " " << ultimate.name() << " in <" << &scope
1542                               << "> " << scope.GetName() << '\n');
1543       std::size_t off = ultimate.offset();
1544       std::size_t symSize = ultimate.size();
1545       for (lower::pft::Variable::AggregateStore &v : stores) {
1546         if (&v.getOwningScope() == &scope) {
1547           auto intervalOff = std::get<0>(v.interval);
1548           auto intervalSize = std::get<1>(v.interval);
1549           if (off >= intervalOff && off < intervalOff + intervalSize)
1550             return &v;
1551           // Zero sized symbol in zero sized equivalence.
1552           if (off == intervalOff && symSize == 0)
1553             return &v;
1554         }
1555       }
1556       // clang-format off
1557       LLVM_DEBUG(
1558           llvm::dbgs() << "looking for " << off << "\n{\n";
1559           for (lower::pft::Variable::AggregateStore &v : stores) {
1560             llvm::dbgs() << " in scope: " << &v.getOwningScope() << "\n";
1561             llvm::dbgs() << "  i = [" << std::get<0>(v.interval) << ".."
1562                 << std::get<0>(v.interval) + std::get<1>(v.interval)
1563                 << "]\n";
1564           }
1565           llvm::dbgs() << "}\n");
1566       // clang-format on
1567       llvm_unreachable("the store must be present");
1568     }
1569     return nullptr;
1570   }
1571 
1572   /// Flatten the result VariableList.
1573   void finalize() {
1574     for (int i = 1, end = layeredVarList.size(); i < end; ++i)
1575       layeredVarList[0].insert(layeredVarList[0].end(),
1576                                layeredVarList[i].begin(),
1577                                layeredVarList[i].end());
1578   }
1579 
1580   llvm::SmallSet<const semantics::Symbol *, 32> seen;
1581   std::vector<Fortran::lower::pft::VariableList> layeredVarList;
1582   llvm::SmallSet<const semantics::Symbol *, 32> aliasSyms;
1583   /// Set of scopes that have been analyzed for aliases.
1584   llvm::SmallSet<const semantics::Scope *, 4> analyzedScopes;
1585   std::vector<Fortran::lower::pft::Variable::AggregateStore> stores;
1586 };
1587 } // namespace
1588 
1589 //===----------------------------------------------------------------------===//
1590 // FunctionLikeUnit implementation
1591 //===----------------------------------------------------------------------===//
1592 
1593 Fortran::lower::pft::FunctionLikeUnit::FunctionLikeUnit(
1594     const parser::MainProgram &func, const lower::pft::PftNode &parent,
1595     const semantics::SemanticsContext &semanticsContext)
1596     : ProgramUnit{func, parent}, endStmt{
1597                                      getFunctionStmt<parser::EndProgramStmt>(
1598                                          func)} {
1599   const auto &programStmt =
1600       std::get<std::optional<parser::Statement<parser::ProgramStmt>>>(func.t);
1601   if (programStmt.has_value()) {
1602     beginStmt = FunctionStatement(programStmt.value());
1603     const semantics::Symbol *symbol = getSymbol(*beginStmt);
1604     entryPointList[0].first = symbol;
1605     scope = symbol->scope();
1606   } else {
1607     scope = &semanticsContext.FindScope(
1608         std::get<parser::Statement<parser::EndProgramStmt>>(func.t).source);
1609   }
1610 }
1611 
1612 Fortran::lower::pft::FunctionLikeUnit::FunctionLikeUnit(
1613     const parser::FunctionSubprogram &func, const lower::pft::PftNode &parent,
1614     const semantics::SemanticsContext &)
1615     : ProgramUnit{func, parent},
1616       beginStmt{getFunctionStmt<parser::FunctionStmt>(func)},
1617       endStmt{getFunctionStmt<parser::EndFunctionStmt>(func)} {
1618   const semantics::Symbol *symbol = getSymbol(*beginStmt);
1619   entryPointList[0].first = symbol;
1620   scope = symbol->scope();
1621 }
1622 
1623 Fortran::lower::pft::FunctionLikeUnit::FunctionLikeUnit(
1624     const parser::SubroutineSubprogram &func, const lower::pft::PftNode &parent,
1625     const semantics::SemanticsContext &)
1626     : ProgramUnit{func, parent},
1627       beginStmt{getFunctionStmt<parser::SubroutineStmt>(func)},
1628       endStmt{getFunctionStmt<parser::EndSubroutineStmt>(func)} {
1629   const semantics::Symbol *symbol = getSymbol(*beginStmt);
1630   entryPointList[0].first = symbol;
1631   scope = symbol->scope();
1632 }
1633 
1634 Fortran::lower::pft::FunctionLikeUnit::FunctionLikeUnit(
1635     const parser::SeparateModuleSubprogram &func,
1636     const lower::pft::PftNode &parent, const semantics::SemanticsContext &)
1637     : ProgramUnit{func, parent},
1638       beginStmt{getFunctionStmt<parser::MpSubprogramStmt>(func)},
1639       endStmt{getFunctionStmt<parser::EndMpSubprogramStmt>(func)} {
1640   const semantics::Symbol *symbol = getSymbol(*beginStmt);
1641   entryPointList[0].first = symbol;
1642   scope = symbol->scope();
1643 }
1644 
1645 Fortran::lower::HostAssociations &
1646 Fortran::lower::pft::FunctionLikeUnit::parentHostAssoc() {
1647   if (auto *par = parent.getIf<FunctionLikeUnit>())
1648     return par->hostAssociations;
1649   llvm::report_fatal_error("parent is not a function");
1650 }
1651 
1652 bool Fortran::lower::pft::FunctionLikeUnit::parentHasHostAssoc() {
1653   if (auto *par = parent.getIf<FunctionLikeUnit>())
1654     return !par->hostAssociations.empty();
1655   return false;
1656 }
1657 
1658 parser::CharBlock
1659 Fortran::lower::pft::FunctionLikeUnit::getStartingSourceLoc() const {
1660   if (beginStmt)
1661     return stmtSourceLoc(*beginStmt);
1662   if (!evaluationList.empty())
1663     return evaluationList.front().position;
1664   return stmtSourceLoc(endStmt);
1665 }
1666 
1667 //===----------------------------------------------------------------------===//
1668 // ModuleLikeUnit implementation
1669 //===----------------------------------------------------------------------===//
1670 
1671 Fortran::lower::pft::ModuleLikeUnit::ModuleLikeUnit(
1672     const parser::Module &m, const lower::pft::PftNode &parent)
1673     : ProgramUnit{m, parent}, beginStmt{getModuleStmt<parser::ModuleStmt>(m)},
1674       endStmt{getModuleStmt<parser::EndModuleStmt>(m)} {}
1675 
1676 Fortran::lower::pft::ModuleLikeUnit::ModuleLikeUnit(
1677     const parser::Submodule &m, const lower::pft::PftNode &parent)
1678     : ProgramUnit{m, parent}, beginStmt{getModuleStmt<parser::SubmoduleStmt>(
1679                                   m)},
1680       endStmt{getModuleStmt<parser::EndSubmoduleStmt>(m)} {}
1681 
1682 parser::CharBlock
1683 Fortran::lower::pft::ModuleLikeUnit::getStartingSourceLoc() const {
1684   return stmtSourceLoc(beginStmt);
1685 }
1686 const Fortran::semantics::Scope &
1687 Fortran::lower::pft::ModuleLikeUnit::getScope() const {
1688   const Fortran::semantics::Symbol *symbol = getSymbol(beginStmt);
1689   assert(symbol && symbol->scope() &&
1690          "Module statement must have a symbol with a scope");
1691   return *symbol->scope();
1692 }
1693 
1694 //===----------------------------------------------------------------------===//
1695 // BlockDataUnit implementation
1696 //===----------------------------------------------------------------------===//
1697 
1698 Fortran::lower::pft::BlockDataUnit::BlockDataUnit(
1699     const parser::BlockData &bd, const lower::pft::PftNode &parent,
1700     const semantics::SemanticsContext &semanticsContext)
1701     : ProgramUnit{bd, parent},
1702       symTab{semanticsContext.FindScope(
1703           std::get<parser::Statement<parser::EndBlockDataStmt>>(bd.t).source)} {
1704 }
1705 
1706 std::unique_ptr<lower::pft::Program>
1707 Fortran::lower::createPFT(const parser::Program &root,
1708                           const semantics::SemanticsContext &semanticsContext) {
1709   PFTBuilder walker(semanticsContext);
1710   Walk(root, walker);
1711   return walker.result();
1712 }
1713 
1714 void Fortran::lower::dumpPFT(llvm::raw_ostream &outputStream,
1715                              const lower::pft::Program &pft) {
1716   PFTDumper{}.dumpPFT(outputStream, pft);
1717 }
1718 
1719 void Fortran::lower::pft::Program::dump() const {
1720   dumpPFT(llvm::errs(), *this);
1721 }
1722 
1723 void Fortran::lower::pft::Evaluation::dump() const {
1724   PFTDumper{}.dumpEvaluation(llvm::errs(), *this);
1725 }
1726 
1727 void Fortran::lower::pft::Variable::dump() const {
1728   if (auto *s = std::get_if<Nominal>(&var)) {
1729     llvm::errs() << s->symbol << " " << *s->symbol;
1730     llvm::errs() << " (depth: " << s->depth << ')';
1731     if (s->global)
1732       llvm::errs() << ", global";
1733     if (s->heapAlloc)
1734       llvm::errs() << ", allocatable";
1735     if (s->pointer)
1736       llvm::errs() << ", pointer";
1737     if (s->target)
1738       llvm::errs() << ", target";
1739     if (s->aliaser)
1740       llvm::errs() << ", equivalence(" << s->aliasOffset << ')';
1741   } else if (auto *s = std::get_if<AggregateStore>(&var)) {
1742     llvm::errs() << "interval[" << std::get<0>(s->interval) << ", "
1743                  << std::get<1>(s->interval) << "]:";
1744     llvm::errs() << " name: " << toStringRef(s->getNamingSymbol().name());
1745     if (s->isGlobal())
1746       llvm::errs() << ", global";
1747     if (s->initialValueSymbol)
1748       llvm::errs() << ", initial value: {" << *s->initialValueSymbol << "}";
1749   } else {
1750     llvm_unreachable("not a Variable");
1751   }
1752   llvm::errs() << '\n';
1753 }
1754 
1755 void Fortran::lower::pft::dump(Fortran::lower::pft::VariableList &variableList,
1756                                std::string s) {
1757   llvm::errs() << (s.empty() ? "VariableList" : s) << " " << &variableList
1758                << " size=" << variableList.size() << "\n";
1759   for (auto var : variableList) {
1760     llvm::errs() << "  ";
1761     var.dump();
1762   }
1763 }
1764 
1765 void Fortran::lower::pft::FunctionLikeUnit::dump() const {
1766   PFTDumper{}.dumpFunctionLikeUnit(llvm::errs(), *this);
1767 }
1768 
1769 void Fortran::lower::pft::ModuleLikeUnit::dump() const {
1770   PFTDumper{}.dumpModuleLikeUnit(llvm::errs(), *this);
1771 }
1772 
1773 /// The BlockDataUnit dump is just the associated symbol table.
1774 void Fortran::lower::pft::BlockDataUnit::dump() const {
1775   llvm::errs() << "block data {\n" << symTab << "\n}\n";
1776 }
1777 
1778 /// Find or create an ordered list of equivalences and variables in \p scope.
1779 /// The result is cached in \p map.
1780 const lower::pft::VariableList &
1781 lower::pft::getScopeVariableList(const semantics::Scope &scope,
1782                                  ScopeVariableListMap &map) {
1783   LLVM_DEBUG(llvm::dbgs() << "\ngetScopeVariableList of [sub]module scope <"
1784                           << &scope << "> " << scope.GetName() << "\n");
1785   auto iter = map.find(&scope);
1786   if (iter == map.end()) {
1787     SymbolDependenceAnalysis sda(scope);
1788     map.emplace(&scope, std::move(sda.getVariableList()));
1789     iter = map.find(&scope);
1790   }
1791   return iter->second;
1792 }
1793 
1794 /// Create an ordered list of equivalences and variables in \p scope.
1795 /// The result is not cached.
1796 lower::pft::VariableList
1797 lower::pft::getScopeVariableList(const semantics::Scope &scope) {
1798   LLVM_DEBUG(
1799       llvm::dbgs() << "\ngetScopeVariableList of [sub]program|block scope <"
1800                    << &scope << "> " << scope.GetName() << "\n");
1801   SymbolDependenceAnalysis sda(scope);
1802   return sda.getVariableList();
1803 }
1804 
1805 /// Create an ordered list of equivalences and variables that \p symbol
1806 /// depends on (no caching). Include \p symbol at the end of the list.
1807 lower::pft::VariableList
1808 lower::pft::getDependentVariableList(const semantics::Symbol &symbol) {
1809   LLVM_DEBUG(llvm::dbgs() << "\ngetDependentVariableList of " << &symbol
1810                           << " - " << symbol << "\n");
1811   SymbolDependenceAnalysis sda(symbol);
1812   return sda.getVariableList();
1813 }
1814 
1815 namespace {
1816 /// Helper class to find all the symbols referenced in a FunctionLikeUnit.
1817 /// It defines a parse tree visitor doing a deep visit in all nodes with
1818 /// symbols (including evaluate::Expr).
1819 struct SymbolVisitor {
1820   template <typename A>
1821   bool Pre(const A &x) {
1822     if constexpr (Fortran::parser::HasTypedExpr<A>::value)
1823       // Some parse tree Expr may legitimately be un-analyzed after semantics
1824       // (for instance PDT component initial value in the PDT definition body).
1825       if (const auto *expr = Fortran::semantics::GetExpr(nullptr, x))
1826         visitExpr(*expr);
1827     return true;
1828   }
1829 
1830   bool Pre(const Fortran::parser::Name &name) {
1831     if (const semantics::Symbol *symbol = name.symbol)
1832       visitSymbol(*symbol);
1833     return false;
1834   }
1835 
1836   template <typename T>
1837   void visitExpr(const Fortran::evaluate::Expr<T> &expr) {
1838     for (const semantics::Symbol &symbol :
1839          Fortran::evaluate::CollectSymbols(expr))
1840       visitSymbol(symbol);
1841   }
1842 
1843   void visitSymbol(const Fortran::semantics::Symbol &symbol) {
1844     callBack(symbol);
1845     // - Visit statement function body since it will be inlined in lowering.
1846     // - Visit function results specification expressions because allocations
1847     //   happens on the caller side.
1848     if (const auto *subprogramDetails =
1849             symbol.detailsIf<Fortran::semantics::SubprogramDetails>()) {
1850       if (const auto &maybeExpr = subprogramDetails->stmtFunction()) {
1851         visitExpr(*maybeExpr);
1852       } else {
1853         if (subprogramDetails->isFunction()) {
1854           // Visit result extents expressions that are explicit.
1855           const Fortran::semantics::Symbol &result =
1856               subprogramDetails->result();
1857           if (const auto *objectDetails =
1858                   result.detailsIf<Fortran::semantics::ObjectEntityDetails>())
1859             if (objectDetails->shape().IsExplicitShape())
1860               for (const Fortran::semantics::ShapeSpec &shapeSpec :
1861                    objectDetails->shape()) {
1862                 visitExpr(shapeSpec.lbound().GetExplicit().value());
1863                 visitExpr(shapeSpec.ubound().GetExplicit().value());
1864               }
1865         }
1866       }
1867     }
1868     if (Fortran::semantics::IsProcedure(symbol)) {
1869       if (auto dynamicType = Fortran::evaluate::DynamicType::From(symbol)) {
1870         // Visit result length specification expressions that are explicit.
1871         if (dynamicType->category() ==
1872             Fortran::common::TypeCategory::Character) {
1873           if (std::optional<Fortran::evaluate::ExtentExpr> length =
1874                   dynamicType->GetCharLength())
1875             visitExpr(*length);
1876         } else if (const Fortran::semantics::DerivedTypeSpec *derivedTypeSpec =
1877                        Fortran::evaluate::GetDerivedTypeSpec(dynamicType)) {
1878           for (const auto &[_, param] : derivedTypeSpec->parameters())
1879             if (const Fortran::semantics::MaybeIntExpr &expr =
1880                     param.GetExplicit())
1881               visitExpr(expr.value());
1882         }
1883       }
1884     }
1885   }
1886 
1887   template <typename A>
1888   constexpr void Post(const A &) {}
1889 
1890   const std::function<void(const Fortran::semantics::Symbol &)> &callBack;
1891 };
1892 } // namespace
1893 
1894 void Fortran::lower::pft::visitAllSymbols(
1895     const Fortran::lower::pft::FunctionLikeUnit &funit,
1896     const std::function<void(const Fortran::semantics::Symbol &)> callBack) {
1897   SymbolVisitor visitor{callBack};
1898   funit.visit([&](const auto &functionParserNode) {
1899     parser::Walk(functionParserNode, visitor);
1900   });
1901 }
1902 
1903 void Fortran::lower::pft::visitAllSymbols(
1904     const Fortran::lower::pft::Evaluation &eval,
1905     const std::function<void(const Fortran::semantics::Symbol &)> callBack) {
1906   SymbolVisitor visitor{callBack};
1907   eval.visit([&](const auto &functionParserNode) {
1908     parser::Walk(functionParserNode, visitor);
1909   });
1910 }
1911