llvm/flang/lib/semantics/canonicalize-do.cc
Tim Keith 251e0196e4 [flang] Simplify Semantics::Perform
`Semantics::Perform` is mostly a series of calls followed by a check
for fatal errors. There is more error checking logic than real code.

To make it clearer, change each of the phases it calls to return true
on success so that `Perform` can just call them one after the other.

Original-commit: flang-compiler/f18@a218cac788
Reviewed-on: https://github.com/flang-compiler/f18/pull/317
2019-03-06 17:07:25 -08:00

98 lines
3.7 KiB
C++

// Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "canonicalize-do.h"
#include "../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)}) {
std::visit(
common::visitors{
[](auto &) {},
[&](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.has_value() &&
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);
Statement<NonLabelDoStmt> nonLabelDoStmt{std::optional<Label>{},
NonLabelDoStmt{std::make_tuple(std::optional<Name>{},
std::move(std::get<std::optional<LoopControl>>(
std::get<Statement<common::Indirection<LabelDoStmt>>>(
std::get<ExecutableConstruct>(doLoop->u).u)
.statement.value()
.t)))}};
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::optional<Name>{}}})};
stack.pop_back();
} while (!stack.empty() && stack.back().label == currentLabel);
i = --next;
}
}
};
bool CanonicalizeDo(Program &program) {
CanonicalizationOfDoLoops canonicalizationOfDoLoops;
Walk(program, canonicalizationOfDoLoops);
return true;
}
}