Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / compiler / pass / UnusedOperandEliminationPass.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "Pass.h"
18
19 #include "UnusedOperandEliminationPass.h"
20 #include "ir/Index.h"
21 #include "util/Set.h"
22 #include "ir/Graph.h"
23
24 /**
25  * @file  UnusedOperandEliminationPass.cc
26  * @brief This file contains UnusedOperandEliminationPass class implementation
27  */
28
29 namespace onert
30 {
31 namespace compiler
32 {
33 namespace pass
34 {
35
36 void UnusedOperandEliminationPass::run()
37 {
38   util::Set<ir::OperandIndex> used;
39
40   _graph.operations().iterate([&](const ir::OperationIndex &, const ir::IOperation &node) {
41     for (auto &&ind : (node.getInputs() + node.getOutputs()) | ir::Remove::UNDEFINED)
42     {
43       used.add(ind);
44     }
45   });
46
47   // Graph's inputs/outputs are always considered as used
48   for (auto &&ind : (_graph.getInputs() + _graph.getOutputs()) | ir::Remove::UNDEFINED)
49   {
50     used.add(ind);
51   }
52
53   _graph.operands().iterate([&](const ir::OperandIndex &ind, const ir::Operand &) {
54     if (!used.contains(ind))
55     {
56       VERBOSE() << "Remove unused operand " << ind << std::endl;
57       _graph.operands().remove(ind);
58     }
59   });
60 }
61
62 } // namespace pass
63 } // namespace compiler
64 } // namespace onert