• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_REGALLOC_REG_ALLOC_RESOLVER_H
17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_REGALLOC_REG_ALLOC_RESOLVER_H
18 
19 #include "compiler/optimizer/analysis/liveness_analyzer.h"
20 #include "reg_alloc_base.h"
21 #include "compiler/optimizer/ir/graph.h"
22 
23 namespace panda::compiler {
24 
25 class RegAllocResolver {
26 public:
RegAllocResolver(Graph * graph)27     explicit RegAllocResolver(Graph *graph) : graph_(graph), liveness_(&graph->GetAnalysis<LivenessAnalyzer>()) {}
28     NO_MOVE_SEMANTIC(RegAllocResolver);
29     NO_COPY_SEMANTIC(RegAllocResolver);
30     ~RegAllocResolver() = default;
31 
32     void Resolve();
33     bool ResolveCatchPhis();
34 
35 private:
36     /*
37      * Methods to set instructions source/dest registers
38      */
39     void ResolveInputs(Inst *inst);
40     void ResolveOutput(Inst *inst);
41     /*
42      * Save states should capture location of the variable at the time when its user is executed.
43      * If single SaveStateInst is reused by multiple users and some life intervals corresponding to
44      * SaveStateInst's inputs were split between these users then SaveStateInst should be copied in
45      * order to capture correct locations for each of its users.
46      */
47     void AddCatchPhiMoves(Inst *inst);
48 
49     Inst *SqueezeCatchPhiInputs(CatchPhiInst *catch_phi);
50 
CanStoreToAccumulator(const Inst * inst)51     bool CanStoreToAccumulator(const Inst *inst) const
52     {
53         return graph_->IsBytecodeOptimizer() && inst->GetDstReg() == ACC_REG_ID;
54     }
CanReadFromAccumulator(const Inst * inst,size_t input_number)55     bool CanReadFromAccumulator(const Inst *inst, size_t input_number) const
56     {
57         return graph_->IsBytecodeOptimizer() && inst->GetSrcReg(input_number) == ACC_REG_ID;
58     }
59 
60     void PropagateCallerMasks(SaveStateInst *save_state);
61 
62     void FillSaveStateRootsMask(SaveStateInst *save_state, Inst *user, SaveStateInst *target_ss);
63 
64     void AddMoveToFixedLocation(Inst *inst, Location input_location, size_t input_num);
65 
AddLocationToRoots(Location location,SaveStateInst * save_state,const Graph * graph)66     static inline void AddLocationToRoots(Location location, SaveStateInst *save_state, const Graph *graph)
67     {
68         if (location.IsFixedRegister()) {
69             save_state->GetRootsRegsMask().set(location.GetValue());
70         } else if (location.IsStack()) {
71             save_state->GetRootsStackMask()->SetBit(location.GetValue());
72         } else {
73             ASSERT(location.IsStackParameter());
74             auto slot_offset = graph->GetStackSlotsCount();
75             save_state->GetRootsStackMask()->SetBit(location.GetValue() + slot_offset);
76         }
77     }
78 
GetGraph()79     Graph *GetGraph() const
80     {
81         return graph_;
82     }
83 
84 private:
85     Graph *graph_;
86     LivenessAnalyzer *liveness_;
87 };
88 
89 }  // namespace panda::compiler
90 
91 #endif  // COMPILER_OPTIMIZER_OPTIMIZATIONS_REGALLOC_REG_ALLOC_RESOLVER_H
92