1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
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 
16 #ifndef COMPILER_OPTIMIZER_OPTIMIZATIONS_MOVE_CONSTANTS_H
17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_MOVE_CONSTANTS_H
18 
19 #include "optimizer/pass.h"
20 #include "optimizer/ir/basicblock.h"
21 #include "compiler_options.h"
22 
23 namespace panda::compiler {
24 
25 class MoveConstants : public Optimization {
26 public:
27     explicit MoveConstants(Graph *graph);
28 
29     NO_MOVE_SEMANTIC(MoveConstants);
30     NO_COPY_SEMANTIC(MoveConstants);
31     ~MoveConstants() override = default;
32 
33     bool RunImpl() override;
34     bool IsEnable() const override
35     {
36         return options.IsCompilerMoveConstants();
37     }
38 
39     const char *GetPassName() const override
40     {
41         return "MoveConstants";
42     }
43 
44 private:
45     ArenaUnorderedMap<uint32_t, ArenaVector<BasicBlock *>> user_dominators_cache_;
46     ArenaVector<const ArenaVector<BasicBlock *> *> user_dominating_blocks_;
47     int moved_constants_counter_;
48 
49     void MoveFromStartBlock(Inst *inst);
50     void GetUsersDominatingBlocks(const Inst *inst);
51     BasicBlock *FindCommonDominator();
52     const ArenaVector<BasicBlock *> *GetDominators(const User &user);
53 };
54 
55 }  // namespace panda::compiler
56 
57 #endif  // COMPILER_OPTIMIZER_OPTIMIZATIONS_MOVE_CONSTANTS_H
58