• 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 ResolveSaveState(Inst *inst);
48 
49     void AddCatchPhiMoves(Inst *inst);
50 
51     Inst *SqueezeCatchPhiInputs(CatchPhiInst *catch_phi);
52 
CanStoreToAccumulator(const Inst * inst)53     bool CanStoreToAccumulator(const Inst *inst) const
54     {
55         return graph_->IsBytecodeOptimizer() && inst->GetDstReg() == ACC_REG_ID;
56     }
CanReadFromAccumulator(const Inst * inst,size_t input_number)57     bool CanReadFromAccumulator(const Inst *inst, size_t input_number) const
58     {
59         return graph_->IsBytecodeOptimizer() && inst->GetSrcReg(input_number) == ACC_REG_ID;
60     }
61 
62     void PropagateCallerMasks(SaveStateInst *save_state);
63 
64     void FillSaveStateRootsMask(SaveStateInst *save_state, Inst *user, SaveStateInst *target_ss);
65 
66     void AddMoveToFixedLocation(Inst *inst, Location input_location, size_t input_num);
67 
AddLocationToRoots(Location location,SaveStateInst * save_state,const Graph * graph)68     static inline void AddLocationToRoots(Location location, SaveStateInst *save_state, const Graph *graph)
69     {
70         if (location.IsFixedRegister()) {
71             save_state->GetRootsRegsMask().set(location.GetValue());
72         } else if (location.IsStack()) {
73             save_state->GetRootsStackMask()->SetBit(location.GetValue());
74         } else {
75             ASSERT(location.IsStackParameter());
76             auto slot_offset = graph->GetStackSlotsCount();
77             save_state->GetRootsStackMask()->SetBit(location.GetValue() + slot_offset);
78         }
79     }
80 
GetGraph()81     Graph *GetGraph() const
82     {
83         return graph_;
84     }
85 
86 private:
87     Graph *graph_;
88     LivenessAnalyzer *liveness_;
89 };
90 
91 }  // namespace panda::compiler
92 #endif  // COMPILER_OPTIMIZER_OPTIMIZATIONS_REGALLOC_REG_ALLOC_RESOLVER_H