2020-02-25 23:11:52 +08:00
|
|
|
//===-- lib/Semantics/canonicalize-do.cpp ---------------------------------===//
|
2018-09-20 05:26:02 +08:00
|
|
|
//
|
2019-12-21 04:52:07 +08:00
|
|
|
// 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
|
2018-09-20 05:26:02 +08:00
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-09-20 05:26:02 +08:00
|
|
|
|
|
|
|
#include "canonicalize-do.h"
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Parser/parse-tree-visitor.h"
|
2018-09-20 05:26:02 +08:00
|
|
|
|
2018-09-22 02:12:02 +08:00
|
|
|
namespace Fortran::parser {
|
2018-09-20 05:26:02 +08:00
|
|
|
|
|
|
|
class CanonicalizationOfDoLoops {
|
2018-10-06 04:59:43 +08:00
|
|
|
struct LabelInfo {
|
|
|
|
Block::iterator iter;
|
|
|
|
Label label;
|
|
|
|
};
|
|
|
|
|
2018-09-20 05:26:02 +08:00
|
|
|
public:
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename T> bool Pre(T &) { return true; }
|
|
|
|
template <typename T> void Post(T &) {}
|
2018-10-06 04:59:43 +08:00
|
|
|
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)}) {
|
|
|
|
std::visit(
|
2018-11-30 01:27:34 +08:00
|
|
|
common::visitors{
|
|
|
|
[](auto &) {},
|
2019-07-10 18:53:25 +08:00
|
|
|
// Labels on end-stmt of constructs are accepted by f18 as an
|
|
|
|
// extension.
|
2019-07-10 01:40:14 +08:00
|
|
|
[&](common::Indirection<AssociateConstruct> &associate) {
|
2020-03-29 12:00:16 +08:00
|
|
|
CanonicalizeIfMatch(block, stack, i,
|
|
|
|
std::get<Statement<EndAssociateStmt>>(associate.value().t));
|
2019-07-10 01:40:14 +08:00
|
|
|
},
|
|
|
|
[&](common::Indirection<BlockConstruct> &blockConstruct) {
|
2020-03-29 12:00:16 +08:00
|
|
|
CanonicalizeIfMatch(block, stack, i,
|
|
|
|
std::get<Statement<EndBlockStmt>>(blockConstruct.value().t));
|
2019-07-10 01:40:14 +08:00
|
|
|
},
|
|
|
|
[&](common::Indirection<ChangeTeamConstruct> &changeTeam) {
|
2020-03-29 12:00:16 +08:00
|
|
|
CanonicalizeIfMatch(block, stack, i,
|
|
|
|
std::get<Statement<EndChangeTeamStmt>>(changeTeam.value().t));
|
2019-07-10 01:40:14 +08:00
|
|
|
},
|
|
|
|
[&](common::Indirection<CriticalConstruct> &critical) {
|
2020-03-29 12:00:16 +08:00
|
|
|
CanonicalizeIfMatch(block, stack, i,
|
|
|
|
std::get<Statement<EndCriticalStmt>>(critical.value().t));
|
2019-07-09 00:19:07 +08:00
|
|
|
},
|
|
|
|
[&](common::Indirection<DoConstruct> &doConstruct) {
|
2020-03-29 12:00:16 +08:00
|
|
|
CanonicalizeIfMatch(block, stack, i,
|
|
|
|
std::get<Statement<EndDoStmt>>(doConstruct.value().t));
|
2019-07-09 00:19:07 +08:00
|
|
|
},
|
2019-07-10 01:40:14 +08:00
|
|
|
[&](common::Indirection<IfConstruct> &ifConstruct) {
|
2020-03-29 12:00:16 +08:00
|
|
|
CanonicalizeIfMatch(block, stack, i,
|
|
|
|
std::get<Statement<EndIfStmt>>(ifConstruct.value().t));
|
2019-07-10 01:40:14 +08:00
|
|
|
},
|
2019-07-09 00:19:07 +08:00
|
|
|
[&](common::Indirection<CaseConstruct> &caseConstruct) {
|
2020-03-29 12:00:16 +08:00
|
|
|
CanonicalizeIfMatch(block, stack, i,
|
|
|
|
std::get<Statement<EndSelectStmt>>(caseConstruct.value().t));
|
2019-07-09 00:19:07 +08:00
|
|
|
},
|
|
|
|
[&](common::Indirection<SelectRankConstruct> &selectRank) {
|
2020-03-29 12:00:16 +08:00
|
|
|
CanonicalizeIfMatch(block, stack, i,
|
|
|
|
std::get<Statement<EndSelectStmt>>(selectRank.value().t));
|
2019-07-09 00:19:07 +08:00
|
|
|
},
|
|
|
|
[&](common::Indirection<SelectTypeConstruct> &selectType) {
|
2020-03-29 12:00:16 +08:00
|
|
|
CanonicalizeIfMatch(block, stack, i,
|
|
|
|
std::get<Statement<EndSelectStmt>>(selectType.value().t));
|
2019-07-09 00:19:07 +08:00
|
|
|
},
|
2019-07-10 01:40:14 +08:00
|
|
|
[&](common::Indirection<ForallConstruct> &forall) {
|
2020-03-29 12:00:16 +08:00
|
|
|
CanonicalizeIfMatch(block, stack, i,
|
|
|
|
std::get<Statement<EndForallStmt>>(forall.value().t));
|
2019-07-10 01:40:14 +08:00
|
|
|
},
|
|
|
|
[&](common::Indirection<WhereConstruct> &where) {
|
|
|
|
CanonicalizeIfMatch(block, stack, i,
|
|
|
|
std::get<Statement<EndWhereStmt>>(where.value().t));
|
|
|
|
},
|
2018-10-06 04:59:43 +08:00
|
|
|
[&](Statement<common::Indirection<LabelDoStmt>> &labelDoStmt) {
|
2020-03-29 12:00:16 +08:00
|
|
|
auto &label{std::get<Label>(labelDoStmt.statement.value().t)};
|
|
|
|
stack.push_back(LabelInfo{i, label});
|
2018-10-06 04:59:43 +08:00
|
|
|
},
|
|
|
|
[&](Statement<common::Indirection<EndDoStmt>> &endDoStmt) {
|
2020-03-29 12:00:16 +08:00
|
|
|
CanonicalizeIfMatch(block, stack, i, endDoStmt);
|
2018-10-06 04:59:43 +08:00
|
|
|
},
|
|
|
|
[&](Statement<ActionStmt> &actionStmt) {
|
2020-03-29 12:00:16 +08:00
|
|
|
CanonicalizeIfMatch(block, stack, i, actionStmt);
|
2018-11-30 01:27:34 +08:00
|
|
|
},
|
|
|
|
},
|
2018-10-06 04:59:43 +08:00
|
|
|
executableConstruct->u);
|
2018-09-21 23:22:41 +08:00
|
|
|
}
|
2018-09-20 05:26:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename T>
|
2018-10-06 04:59:43 +08:00
|
|
|
void CanonicalizeIfMatch(Block &originalBlock, std::vector<LabelInfo> &stack,
|
|
|
|
Block::iterator &i, Statement<T> &statement) {
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!stack.empty() && statement.label &&
|
2018-10-06 04:59:43 +08:00
|
|
|
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{}};
|
2018-10-05 00:25:58 +08:00
|
|
|
}
|
2018-10-06 04:59:43 +08:00
|
|
|
auto next{++i};
|
|
|
|
do {
|
|
|
|
Block block;
|
|
|
|
auto doLoop{stack.back().iter};
|
2018-10-11 07:52:06 +08:00
|
|
|
auto originalSource{
|
|
|
|
std::get<Statement<common::Indirection<LabelDoStmt>>>(
|
|
|
|
std::get<ExecutableConstruct>(doLoop->u).u)
|
|
|
|
.source};
|
2018-10-06 04:59:43 +08:00
|
|
|
block.splice(block.begin(), originalBlock, ++stack.back().iter, next);
|
2019-07-10 23:44:05 +08:00
|
|
|
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))}};
|
2018-10-11 07:52:06 +08:00
|
|
|
nonLabelDoStmt.source = originalSource;
|
|
|
|
std::get<ExecutableConstruct>(doLoop->u).u =
|
|
|
|
common::Indirection<DoConstruct>{
|
|
|
|
std::make_tuple(std::move(nonLabelDoStmt), std::move(block),
|
2019-07-10 23:44:05 +08:00
|
|
|
Statement<EndDoStmt>{
|
|
|
|
std::optional<Label>{}, EndDoStmt{std::move(name)}})};
|
2018-10-06 04:59:43 +08:00
|
|
|
stack.pop_back();
|
|
|
|
} while (!stack.empty() && stack.back().label == currentLabel);
|
|
|
|
i = --next;
|
|
|
|
}
|
2018-09-20 05:26:02 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-07 09:07:25 +08:00
|
|
|
bool CanonicalizeDo(Program &program) {
|
2018-10-06 04:59:43 +08:00
|
|
|
CanonicalizationOfDoLoops canonicalizationOfDoLoops;
|
2018-09-22 02:12:02 +08:00
|
|
|
Walk(program, canonicalizationOfDoLoops);
|
2019-03-07 09:07:25 +08:00
|
|
|
return true;
|
2018-09-20 05:26:02 +08:00
|
|
|
}
|
2019-03-07 09:07:25 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::parser
|