1 /*
2  * Copyright (c) 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 LIBARK_DEFECT_SCAN_AUX_INCLUDE_GRAPH_H
17 #define LIBARK_DEFECT_SCAN_AUX_INCLUDE_GRAPH_H
18 
19 #include <string>
20 #include <vector>
21 #include "compiler/optimizer/ir/basicblock.h"
22 #include "compiler/optimizer/ir/graph.h"
23 #include "inst_type.h"
24 
25 namespace panda::defect_scan_aux {
26 class BasicBlock;
27 class Graph;
28 
29 // a wrapper class for compiler::Inst
30 class Inst {
31 public:
Inst(const compiler::Inst *inst)32     explicit Inst(const compiler::Inst *inst) : inst_(inst)
33     {
34         type_ = GetInstType(inst_);
35     }
36     ~Inst() = default;
37 
38     bool operator==(const Inst &inst) const;
39     bool operator!=(const Inst &inst) const;
40     InstType GetType() const;
41     bool IsInstStLexVar() const;
42     bool IsInstLdLexVar() const;
43     bool IsInstStGlobal() const;
44     bool IsInstLdGlobal() const;
45     uint16_t GetArgIndex() const;
46     uint32_t GetPc() const;
47     uint32_t GetInstId() const;
48     BasicBlock GetBasicBlock() const;
49     Graph GetGraph() const;
50     std::vector<Inst> GetInputInsts() const;
51     std::vector<Inst> GetUserInsts() const;
52     std::vector<uint32_t> GetImms() const;
53 
54 private:
55     InstType GetInstType(const compiler::Inst *inst);
56 
57     const compiler::Inst *inst_ {nullptr};
58     InstType type_ {InstType::INVALID_TYPE};
59 };
60 
61 // a wrapper class for compiler::BasicBlock
62 class BasicBlock {
63 public:
BasicBlock(const compiler::BasicBlock *bb)64     explicit BasicBlock(const compiler::BasicBlock *bb) : bb_(bb) {}
65     ~BasicBlock() = default;
66 
67     bool operator==(const BasicBlock &bb) const;
68     bool operator!=(const BasicBlock &bb) const;
69     Graph GetGraph() const;
70     std::vector<BasicBlock> GetPredBlocks() const;
71     std::vector<BasicBlock> GetSuccBlocks() const;
72     std::vector<Inst> GetInstList() const;
73 
74 private:
75     const compiler::BasicBlock *bb_ {nullptr};
76 };
77 
78 // a wrapper class for compiler::Graph
79 class Graph {
80 public:
81     using InstVisitor = std::function<void(const Inst &)>;
Graph(const compiler::Graph *graph)82     explicit Graph(const compiler::Graph *graph) : graph_(graph) {}
83     ~Graph() = default;
84 
85     BasicBlock GetStartBasicBlock() const;
86     BasicBlock GetEndBasicBlock() const;
87     std::vector<BasicBlock> GetBasicBlockList() const;
88     void VisitAllInstructions(const InstVisitor visitor) const;
89 
90 private:
91     const compiler::Graph *graph_ {nullptr};
92 };
93 }  // namespace panda::defect_scan_aux
94 
95 #endif  // LIBARK_DEFECT_SCAN_AUX_INCLUDE_GRAPH_H