1 /* 2 * Copyright (c) 2021-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 COMPILER_OPTIMIZER_OPTIMIZATIONS_PHI_RESOLVER_H 17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_PHI_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 ark::compiler { 24 25 class RegAllocResolver { 26 public: RegAllocResolver(Graph * graph)27 explicit RegAllocResolver(Graph *graph) 28 : graph_(graph), 29 liveness_(&graph->GetAnalysis<LivenessAnalyzer>()), 30 inputLocations_(graph->GetLocalAllocator()->Adapter()) 31 { 32 } 33 NO_MOVE_SEMANTIC(RegAllocResolver); 34 NO_COPY_SEMANTIC(RegAllocResolver); 35 ~RegAllocResolver() = default; 36 37 void Resolve(); 38 PANDA_PUBLIC_API bool ResolveCatchPhis(); 39 40 private: 41 /* 42 * Methods to set instructions source/dest registers 43 */ 44 void ResolveInputs(Inst *inst); 45 void ResolveOutput(Inst *inst); 46 /* 47 * Save states should capture location of the variable at the time when its user is executed. 48 * If single SaveStateInst is reused by multiple users and some life intervals corresponding to 49 * SaveStateInst's inputs were split between these users then SaveStateInst should be copied in 50 * order to capture correct locations for each of its users. 51 */ 52 void ResolveSaveState(Inst *inst); 53 54 void AddCatchPhiMoves(Inst *inst); 55 56 Inst *SqueezeCatchPhiInputs(CatchPhiInst *catchPhi); 57 CanStoreToAccumulator(const Inst * inst)58 bool CanStoreToAccumulator(const Inst *inst) const 59 { 60 return graph_->IsBytecodeOptimizer() && inst->GetDstReg() == GetAccReg(); 61 } CanReadFromAccumulator(const Inst * inst,size_t inputNumber)62 bool CanReadFromAccumulator(const Inst *inst, size_t inputNumber) const 63 { 64 return graph_->IsBytecodeOptimizer() && inst->GetSrcReg(inputNumber) == GetAccReg(); 65 } 66 67 void PropagateCallerMasks(SaveStateInst *saveState); 68 69 void FillSaveStateRootsMask(SaveStateInst *saveState, Inst *user, SaveStateInst *targetSs); 70 71 void AddMoveToFixedLocation(Inst *inst, Location inputLocation, size_t inputNum); 72 AddLocationToRoots(Location location,SaveStateInst * saveState,const Graph * graph)73 static inline void AddLocationToRoots(Location location, SaveStateInst *saveState, const Graph *graph) 74 { 75 if (location.IsFixedRegister()) { 76 saveState->GetRootsRegsMask().set(location.GetValue()); 77 } else if (location.IsStack()) { 78 saveState->GetRootsStackMask()->SetBit(location.GetValue()); 79 } else { 80 ASSERT(location.IsStackParameter()); 81 auto slotOffset = graph->GetStackSlotsCount(); 82 saveState->GetRootsStackMask()->SetBit(location.GetValue() + slotOffset); 83 } 84 } 85 GetGraph()86 Graph *GetGraph() const 87 { 88 return graph_; 89 } 90 91 private: 92 Graph *graph_; 93 LivenessAnalyzer *liveness_; 94 ArenaVector<Location> inputLocations_; 95 }; 96 97 } // namespace ark::compiler 98 #endif // COMPILER_OPTIMIZER_OPTIMIZATIONS_PHI_RESOLVER_H 99