• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "types_analysis.h"
17 #include "optimizer/ir/inst.h"
18 
19 namespace ark::compiler {
RunImpl()20 bool TypesAnalysis::RunImpl()
21 {
22     marker_ = GetGraph()->NewMarker();
23     VisitGraph();
24     GetGraph()->EraseMarker(marker_);
25     return true;
26 }
27 
MarkedPhiRec(PhiInst * phi,AnyBaseType type)28 void TypesAnalysis::MarkedPhiRec(PhiInst *phi, AnyBaseType type)
29 {
30     if (phi->SetMarker(marker_)) {
31         auto phiType = phi->GetAnyType();
32         // Phi has 2 inputs or users with different types
33         if (phiType != type) {
34             phi->SetAssumedAnyType(AnyBaseType::UNDEFINED_TYPE);
35             return;
36         }
37         return;
38     }
39     phi->SetAssumedAnyType(type);
40     for (auto &user : phi->GetUsers()) {
41         auto userInst = user.GetInst();
42         if (userInst->GetOpcode() == Opcode::Phi) {
43             MarkedPhiRec(userInst->CastToPhi(), type);
44         }
45     }
46 }
47 
VisitCastValueToAnyType(GraphVisitor * v,Inst * inst)48 void TypesAnalysis::VisitCastValueToAnyType(GraphVisitor *v, Inst *inst)
49 {
50     auto self = static_cast<TypesAnalysis *>(v);
51     auto type = inst->CastToCastValueToAnyType()->GetAnyType();
52     ASSERT(type != AnyBaseType::UNDEFINED_TYPE);
53     for (auto &user : inst->GetUsers()) {
54         auto userInst = user.GetInst();
55         if (userInst->GetOpcode() == Opcode::Phi) {
56             self->MarkedPhiRec(userInst->CastToPhi(), type);
57         }
58     }
59 }
60 
VisitAnyTypeCheck(GraphVisitor * v,Inst * inst)61 void TypesAnalysis::VisitAnyTypeCheck(GraphVisitor *v, Inst *inst)
62 {
63     auto self = static_cast<TypesAnalysis *>(v);
64     auto type = inst->CastToAnyTypeCheck()->GetAnyType();
65     if (type == AnyBaseType::UNDEFINED_TYPE) {
66         return;
67     }
68     auto input = inst->GetInput(0).GetInst();
69     if (input->GetOpcode() == Opcode::Phi) {
70         self->MarkedPhiRec(input->CastToPhi(), type);
71     }
72 }
73 }  // namespace ark::compiler
74