• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 BRANCH_ELIMINATOR_H
17 #define BRANCH_ELIMINATOR_H
18 
19 #include <limits>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include "helpers/visit_helper/visit_helper-inl.h"
25 #include "libabckit/include/c/abckit.h"
26 #include "libabckit/include/c/ir_core.h"
27 #include "libabckit/include/c/metadata_core.h"
28 #include "libabckit/include/c/isa/isa_dynamic.h"
29 
30 struct ConstantInfo {
31     std::string path;
32     std::string objName;    // Config
33     std::string fieldName;  // isDebug
34     bool fieldValue;        // field is constant true or false
35 };
36 
37 using ConstantInfoIndexType = uint32_t;
38 
39 using SuspectsType = std::unordered_map<AbckitCoreImportDescriptor *, std::vector<ConstantInfoIndexType>>;
40 
41 class BranchEliminator {
42 public:
43     BranchEliminator(enum AbckitApiVersion version, AbckitFile *file, std::vector<ConstantInfo> constants);
44 
45     void Run();
46 
47     constexpr static ConstantInfoIndexType INVALID_INDEX = std::numeric_limits<ConstantInfoIndexType>::max();
48 
49 private:
50     bool GetInstAsBool(AbckitInst *inst) const;
51     bool GetSuspects(AbckitCoreModule *mod, SuspectsType &suspects);
52     void EliminateBranchWithSuspect(AbckitCoreFunction *method, const SuspectsType &suspects);
53     ConstantInfoIndexType GetConstantInfoIndex(AbckitInst *inst, const SuspectsType &suspects) const;
54     void ReplaceUsers(AbckitInst *oldInst, AbckitInst *newInst);
55     bool ReplaceModuleVarByConstant(AbckitGraph *graph, const SuspectsType &suspects);
56     void ReplaceLdObjByNameWithBoolean(AbckitInst *ldObjByName, ConstantInfoIndexType constInfoIndex);
57     void DeleteUnreachableBranch(AbckitInst *ifInst) const;
58     bool LoweringConstants(AbckitGraph *graph);
59     bool RemoveUnusedInsts(AbckitGraph *graph);
60     bool CanBeRemoved(AbckitInst *inst);
61     bool IsEliminatableIfInst(AbckitInst *ifInst);
62 
63 private:
64     const AbckitApi *impl_ = nullptr;
65     const AbckitInspectApi *implI_ = nullptr;
66     const AbckitGraphApi *implG_ = nullptr;
67     const AbckitIsaApiDynamic *dynG_ = nullptr;
68     const AbckitModifyApi *implM_ = nullptr;
69     AbckitFile *file_ = nullptr;
70     VisitHelper vh_;
71 
72     std::vector<ConstantInfo> constants_;
73 };
74 
75 #endif /* BRANCH_ELIMINATOR_H */
76