blob: d1deb8f8c00eab82c8c1fd85a74610caaeb070e7 [file] [log] [blame]
Ben Clayton75cd5922021-07-02 21:15:44 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "src/transform/fold_trivial_single_use_lets.h"
16
17#include "src/program_builder.h"
18#include "src/sem/block_statement.h"
19#include "src/sem/function.h"
20#include "src/sem/statement.h"
21#include "src/sem/variable.h"
22#include "src/utils/scoped_assignment.h"
23
24TINT_INSTANTIATE_TYPEINFO(tint::transform::FoldTrivialSingleUseLets);
25
26namespace tint {
27namespace transform {
28namespace {
29
30ast::VariableDeclStatement* AsTrivialLetDecl(ast::Statement* stmt) {
31 auto* var_decl = stmt->As<ast::VariableDeclStatement>();
32 if (!var_decl) {
33 return nullptr;
34 }
35 auto* var = var_decl->variable();
36 if (!var->is_const()) {
37 return nullptr;
38 }
39 auto* ctor = var->constructor();
40 if (!IsAnyOf<ast::IdentifierExpression, ast::ScalarConstructorExpression>(
41 ctor)) {
42 return nullptr;
43 }
44 return var_decl;
45}
46
47} // namespace
48
49FoldTrivialSingleUseLets::FoldTrivialSingleUseLets() = default;
50
51FoldTrivialSingleUseLets::~FoldTrivialSingleUseLets() = default;
52
53void FoldTrivialSingleUseLets::Run(CloneContext& ctx,
54 const DataMap&,
55 DataMap&) {
56 for (auto* node : ctx.src->ASTNodes().Objects()) {
57 if (auto* block = node->As<ast::BlockStatement>()) {
58 auto& stmts = block->statements();
59 for (size_t stmt_idx = 0; stmt_idx < stmts.size(); stmt_idx++) {
60 auto* stmt = stmts[stmt_idx];
61 if (auto* let_decl = AsTrivialLetDecl(stmt)) {
62 auto* let = let_decl->variable();
63 auto* sem_let = ctx.src->Sem().Get(let);
64 auto& users = sem_let->Users();
65 if (users.size() != 1) {
66 continue; // Does not have a single user.
67 }
68
69 auto* user = users[0];
70 auto* user_stmt = user->Stmt()->Declaration();
71
72 for (size_t i = stmt_idx; i < stmts.size(); i++) {
73 if (user_stmt == stmts[i]) {
74 auto* user_expr = user->Declaration();
75 ctx.Remove(stmts, let_decl);
76 ctx.Replace(user_expr, ctx.Clone(let->constructor()));
77 }
78 if (!AsTrivialLetDecl(stmts[i])) {
79 // Stop if we hit a statement that isn't the single use of the
80 // let, and isn't a let itself.
81 break;
82 }
83 }
84 }
85 }
86 }
87 }
88
89 ctx.Clone();
90}
91
92} // namespace transform
93} // namespace tint