1 /*
2 * Copyright (c) 2023-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 #include "optimizer/analysis/liveness_use_table.h"
17
18 namespace ark::compiler {
UseTable(ArenaAllocator * allocator)19 UseTable::UseTable(ArenaAllocator *allocator) : table_(allocator->Adapter()), allocator_(allocator) {}
20
AddUseOnFixedLocation(const Inst * inst,Location location,LifeNumber ln)21 void UseTable::AddUseOnFixedLocation(const Inst *inst, Location location, LifeNumber ln)
22 {
23 auto res = table_.try_emplace(inst, allocator_->Adapter());
24 auto &uses = res.first->second;
25 ASSERT(location.IsRegisterValid());
26 uses[ln] = location.GetValue();
27 }
28
HasUseOnFixedLocation(const Inst * inst,LifeNumber ln) const29 bool UseTable::HasUseOnFixedLocation(const Inst *inst, LifeNumber ln) const
30 {
31 auto it = table_.find(inst);
32 if (it == table_.end()) {
33 return false;
34 }
35 const auto &uses = it->second;
36 return uses.count(ln) > 0;
37 }
38
GetNextUseOnFixedLocation(const Inst * inst,LifeNumber ln) const39 Register UseTable::GetNextUseOnFixedLocation(const Inst *inst, LifeNumber ln) const
40 {
41 auto it = table_.find(inst);
42 if (it == table_.end()) {
43 return GetInvalidReg();
44 }
45 const auto &uses = it->second;
46 auto usesIt = uses.lower_bound(ln);
47 return usesIt == uses.end() ? GetInvalidReg() : usesIt->second;
48 }
49
Dump(std::ostream & out,Arch arch) const50 void UseTable::Dump(std::ostream &out, Arch arch) const
51 {
52 out << "UseTable" << std::endl;
53 for (auto [inst, uses] : table_) {
54 out << "Inst v" << inst->GetId() << ": ";
55 auto sep = "";
56 for (auto [ln, r] : uses) {
57 out << sep << "{" << std::to_string(ln) << ", " << Location::MakeRegister(r, inst->GetType()).ToString(arch)
58 << "}";
59 sep = ", ";
60 }
61 out << std::endl;
62 }
63 }
64 } // namespace ark::compiler