• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_ANALYSIS_LIVE_IN_H
17 #define COMPILER_ANALYSIS_LIVE_IN_H
18 
19 #include "optimizer/ir/basicblock.h"
20 #include "optimizer/ir/graph.h"
21 #include "typed_ref_set.h"
22 
23 namespace ark::compiler {
24 
25 class LiveInAnalysis {
26 public:
LiveInAnalysis(Graph * graph)27     explicit LiveInAnalysis(Graph *graph)
28         : graph_(graph),
29           liveIn_(graph_->GetLocalAllocator()->Adapter()),
30           allInsts_(graph_->GetLocalAllocator()->Adapter())
31     {
32     }
33     ~LiveInAnalysis() = default;
34     NO_COPY_SEMANTIC(LiveInAnalysis);
35     NO_MOVE_SEMANTIC(LiveInAnalysis);
36 
37     static bool HasAllocs(Graph *graph);
38 
39     // Compute set of ref-typed instructions live at the beginning of each
40     // basic block. If `checkAllocs` is true, return false if graph don't contain NewObject instructions.
41     bool Run(bool checkAllocs);
42 
43     template <typename Callback>
VisitAlive(const BasicBlock * block,Callback && cb)44     void VisitAlive(const BasicBlock *block, Callback &&cb)
45     {
46         auto &liveIns = liveIn_[block->GetId()];
47         VisitBitVector(liveIns, [this, &cb](size_t liveIdx) {
48             ASSERT(liveIdx < allInsts_.size());
49             ASSERT(allInsts_[liveIdx] != nullptr);
50             cb(allInsts_[liveIdx]);
51         });
52     }
53 
54     bool IsAlive(const BasicBlock *block, const Inst *inst) const;
55 
IsAllocInst(const Inst * inst)56     static bool IsAllocInst(const Inst *inst)
57     {
58         return inst->GetOpcode() == Opcode::NewObject || inst->GetOpcode() == Opcode::NewArray;
59     }
60 
61 private:
62     Graph *graph_;
63     // Live ins evaluation requires union of successor's live ins,
64     // bit vector's union works faster than set's union.
65     ArenaVector<ArenaBitVector> liveIn_;
66     ArenaVector<Inst *> allInsts_;
67 
AddInst(Inst * inst)68     void AddInst(Inst *inst)
69     {
70         if (inst->GetId() >= allInsts_.size()) {
71             allInsts_.resize(inst->GetId() + 1);
72         }
73         allInsts_[inst->GetId()] = inst;
74     }
75 
76     void ProcessBlock(BasicBlock *block);
77 };
78 
79 }  // namespace ark::compiler
80 
81 #endif  // COMPILER_ANALYSIS_LIVE_IN_H
82