4911ace2fc89c4dcbc76210994f9af7f9bce0978
[platform/core/ml/nnfw.git] / runtime / onert / core / src / compiler / pass / ConstantInsertionPass.h
1 /*
2  * Copyright (c) 2019 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 #ifndef __ONERT_COMPILER_PASS_CONSTANT_INSERTION_PASS_H__
18 #define __ONERT_COMPILER_PASS_CONSTANT_INSERTION_PASS_H__
19
20 #include <compiler/PermuteFactor.h>
21 #include <ir/Index.h>
22 #include "LoweredOperationPass.h"
23 #include <unordered_map>
24 #include <utility>
25
26 namespace onert
27 {
28 namespace compiler
29 {
30 namespace pass
31 {
32
33 class ConstantInsertionPass : public LoweredOperationPass
34 {
35 public:
36   using LoweredOperationPass::LoweredOperationPass;
37
38 public:
39   std::string id() final { return "ConstantInsertionPass"; }
40
41 public:
42   void callback(const ir::OperationIndex &index, ir::Operation &node) final;
43
44 private:
45   struct ReplaceKey
46   {
47     ir::OperandIndex index;
48     PermuteFactor factor;
49
50     bool operator==(const ReplaceKey &other) const
51     {
52       return index == other.index && factor == other.factor;
53     }
54   };
55
56   /**
57    * @brief Structure that provides hash function of ReplaceKey
58    */
59   struct KeyHasher
60   {
61     std::size_t operator()(const ReplaceKey &key) const noexcept
62     {
63       using std::hash;
64       return hash<ir::OperandIndex>()(key.index) ^ (hash<PermuteFactor>()(key.factor) << 1);
65     }
66   };
67
68   std::unordered_map<ReplaceKey, ir::OperandIndex, KeyHasher> _replace_operands_map;
69 };
70
71 } // namespace pass
72 } // namespace compiler
73 } // namespace onert
74
75 #endif // __ONERT_COMPILER_PASS_CONSTANT_INSERTION_PASS_H__