1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
//===-- lib/Semantics/canonicalize-do.cpp ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "canonicalize-do.h"
#include "flang/Parser/parse-tree-visitor.h"
namespace Fortran::parser {
class CanonicalizationOfDoLoops {
struct LabelInfo {
Block::iterator iter;
Label label;
};
public:
template <typename T> bool Pre(T &) { return true; }
template <typename T> void Post(T &) {}
void Post(Block &block) {
std::vector<LabelInfo> stack;
for (auto i{block.begin()}, end{block.end()}; i != end; ++i) {
if (auto *executableConstruct{std::get_if<ExecutableConstruct>(&i->u)}) {
common::visit(
common::visitors{
[](auto &) {},
// Labels on end-stmt of constructs are accepted by f18 as an
// extension.
[&](common::Indirection<AssociateConstruct> &associate) {
CanonicalizeIfMatch(block, stack, i,
std::get<Statement<EndAssociateStmt>>(
associate.value().t));
},
[&](common::Indirection<BlockConstruct> &blockConstruct) {
CanonicalizeIfMatch(block, stack, i,
std::get<Statement<EndBlockStmt>>(
blockConstruct.value().t));
},
[&](common::Indirection<ChangeTeamConstruct> &changeTeam) {
CanonicalizeIfMatch(block, stack, i,
std::get<Statement<EndChangeTeamStmt>>(
changeTeam.value().t));
},
[&](common::Indirection<CriticalConstruct> &critical) {
CanonicalizeIfMatch(block, stack, i,
std::get<Statement<EndCriticalStmt>>(critical.value().t));
},
[&](common::Indirection<DoConstruct> &doConstruct) {
CanonicalizeIfMatch(block, stack, i,
std::get<Statement<EndDoStmt>>(doConstruct.value().t));
},
[&](common::Indirection<IfConstruct> &ifConstruct) {
CanonicalizeIfMatch(block, stack, i,
std::get<Statement<EndIfStmt>>(ifConstruct.value().t));
},
[&](common::Indirection<CaseConstruct> &caseConstruct) {
CanonicalizeIfMatch(block, stack, i,
std::get<Statement<EndSelectStmt>>(
caseConstruct.value().t));
},
[&](common::Indirection<SelectRankConstruct> &selectRank) {
CanonicalizeIfMatch(block, stack, i,
std::get<Statement<EndSelectStmt>>(selectRank.value().t));
},
[&](common::Indirection<SelectTypeConstruct> &selectType) {
CanonicalizeIfMatch(block, stack, i,
std::get<Statement<EndSelectStmt>>(selectType.value().t));
},
[&](common::Indirection<ForallConstruct> &forall) {
CanonicalizeIfMatch(block, stack, i,
std::get<Statement<EndForallStmt>>(forall.value().t));
},
[&](common::Indirection<WhereConstruct> &where) {
CanonicalizeIfMatch(block, stack, i,
std::get<Statement<EndWhereStmt>>(where.value().t));
},
[&](Statement<common::Indirection<LabelDoStmt>> &labelDoStmt) {
auto &label{std::get<Label>(labelDoStmt.statement.value().t)};
stack.push_back(LabelInfo{i, label});
},
[&](Statement<common::Indirection<EndDoStmt>> &endDoStmt) {
CanonicalizeIfMatch(block, stack, i, endDoStmt);
},
[&](Statement<ActionStmt> &actionStmt) {
CanonicalizeIfMatch(block, stack, i, actionStmt);
},
},
executableConstruct->u);
}
}
}
private:
template <typename T>
void CanonicalizeIfMatch(Block &originalBlock, std::vector<LabelInfo> &stack,
Block::iterator &i, Statement<T> &statement) {
if (!stack.empty() && statement.label &&
stack.back().label == *statement.label) {
auto currentLabel{stack.back().label};
if constexpr (std::is_same_v<T, common::Indirection<EndDoStmt>>) {
std::get<ExecutableConstruct>(i->u).u = Statement<ActionStmt>{
std::optional<Label>{currentLabel}, ContinueStmt{}};
}
auto next{++i};
do {
Block block;
auto doLoop{stack.back().iter};
auto originalSource{
std::get<Statement<common::Indirection<LabelDoStmt>>>(
std::get<ExecutableConstruct>(doLoop->u).u)
.source};
block.splice(block.begin(), originalBlock, ++stack.back().iter, next);
auto &labelDo{std::get<Statement<common::Indirection<LabelDoStmt>>>(
std::get<ExecutableConstruct>(doLoop->u).u)};
auto &loopControl{
std::get<std::optional<LoopControl>>(labelDo.statement.value().t)};
auto &name{std::get<std::optional<Name>>(labelDo.statement.value().t)};
Statement<NonLabelDoStmt> nonLabelDoStmt{std::move(labelDo.label),
NonLabelDoStmt{
std::make_tuple(common::Clone(name), std::move(loopControl))}};
nonLabelDoStmt.source = originalSource;
std::get<ExecutableConstruct>(doLoop->u).u =
common::Indirection<DoConstruct>{
std::make_tuple(std::move(nonLabelDoStmt), std::move(block),
Statement<EndDoStmt>{
std::optional<Label>{}, EndDoStmt{std::move(name)}})};
stack.pop_back();
} while (!stack.empty() && stack.back().label == currentLabel);
i = --next;
}
}
};
bool CanonicalizeDo(Program &program) {
CanonicalizationOfDoLoops canonicalizationOfDoLoops;
Walk(program, canonicalizationOfDoLoops);
return true;
}
} // namespace Fortran::parser
|