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 BYTECODE_OPTIMIZER_MODULE_CONSTANT_ANALYZER_H
17 #define BYTECODE_OPTIMIZER_MODULE_CONSTANT_ANALYZER_H
18 
19 #include <cstring>
20 #include "bytecode_optimizer/ir_interface.h"
21 #include "compiler/optimizer/pass.h"
22 #include "compiler/optimizer/ir/graph.h"
23 #include "compiler/optimizer/ir/graph_visitor.h"
24 #include "libpandabase/utils/arena_containers.h"
25 
26 namespace panda::bytecodeopt {
27 
28 using compiler::BasicBlock;
29 using compiler::Inst;
30 using compiler::Opcode;
31 using compiler::RuntimeInterface;
32 
33 class ConstantValue;
34 
35 using ModuleConstantAnalysisResult = std::unordered_map<uint32_t, ConstantValue *>;
36 
37 /*
38  * ModuleConstantAnalyzer is used to scan the IR of the given function, and
39  * analysis the initial value of all module constants (represented in
40  * BytecodeAnalysisResult for now, extracts from bytecode later after new
41  * stconstmodulevar instruction is enabled)
42  */
43 class ModuleConstantAnalyzer : public compiler::Analysis, public compiler::GraphVisitor {
44 public:
45     explicit ModuleConstantAnalyzer(compiler::Graph *graph,
46                                     const std::unordered_set<uint32_t> &const_local_export_slots,
47                                     ModuleConstantAnalysisResult &analysis_result,
48                                     const BytecodeOptIrInterface *iface);
49 
50     NO_MOVE_SEMANTIC(ModuleConstantAnalyzer);
51     NO_COPY_SEMANTIC(ModuleConstantAnalyzer);
52     ~ModuleConstantAnalyzer() override = default;
53 
54     const char *GetPassName() const override
55     {
56         return "ModuleConstantAnalysis";
57     }
58 
59     bool RunImpl() override;
60 
61     const ArenaVector<BasicBlock *> &GetBlocksToVisit() const override
62     {
63         return GetGraph()->GetBlocksRPO();
64     }
65 
66 protected:
67     static void VisitIntrinsic(GraphVisitor *v, Inst *inst);
68 
69 #include "compiler/optimizer/ir/visitor.inc"
70 
71 private:
72     ConstantValue *GetInstConstValue(Inst *inst);
73     ConstantValue *GetConstantInstConstValue(compiler::ConstantInst *inst);
74     ConstantValue *GetIntrinsicInstConstValue(compiler::IntrinsicInst *inst);
75     ConstantValue *GetLoadStringInstConstValue(compiler::LoadFromPool *inst);
76     bool IsConstModuleVar(uint32_t slot);
77     void RecordModuleConstValue(uint32_t slot, ConstantValue *value);
78 
79     const std::unordered_set<uint32_t> &const_local_export_slots_;
80     ModuleConstantAnalysisResult &analysis_result_;
81     const BytecodeOptIrInterface *ir_interface_;
82 };
83 
84 }  // namespace panda::bytecodeopt
85 
86 #endif  // BYTECODE_OPTIMIZER_MODULE_CONSTANT_ANALYZER_H