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 #include "object_type_propagation.h"
17 #include "optimizer/ir/basicblock.h"
18 #include "optimizer/ir/inst.h"
19
20 namespace panda::compiler {
RunImpl()21 bool ObjectTypePropagation::RunImpl()
22 {
23 VisitGraph();
24 return true;
25 }
26
VisitNewObject(GraphVisitor * v,Inst * i)27 void ObjectTypePropagation::VisitNewObject(GraphVisitor *v, Inst *i)
28 {
29 auto self = static_cast<ObjectTypePropagation *>(v);
30 auto inst = i->CastToNewObject();
31 auto klass = self->GetGraph()->GetRuntime()->GetClass(inst->GetMethod(), inst->GetTypeId());
32 if (klass != nullptr) {
33 inst->SetObjectTypeInfo(ObjectTypeInfo(static_cast<ObjectTypeInfo::ClassType>(klass)));
34 }
35 }
36
VisitNewArray(GraphVisitor * v,Inst * i)37 void ObjectTypePropagation::VisitNewArray(GraphVisitor *v, Inst *i)
38 {
39 auto self = static_cast<ObjectTypePropagation *>(v);
40 auto inst = i->CastToNewArray();
41 auto klass = self->GetGraph()->GetRuntime()->GetClass(inst->GetMethod(), inst->GetTypeId());
42 if (klass != nullptr) {
43 inst->SetObjectTypeInfo(ObjectTypeInfo(static_cast<ObjectTypeInfo::ClassType>(klass)));
44 }
45 }
46
VisitLoadString(GraphVisitor * v,Inst * i)47 void ObjectTypePropagation::VisitLoadString(GraphVisitor *v, Inst *i)
48 {
49 auto self = static_cast<ObjectTypePropagation *>(v);
50 auto inst = i->CastToLoadString();
51 auto klass = self->GetGraph()->GetRuntime()->GetStringClass(inst->GetMethod());
52 if (klass != nullptr) {
53 inst->SetObjectTypeInfo(ObjectTypeInfo(static_cast<ObjectTypeInfo::ClassType>(klass)));
54 }
55 }
56
VisitLoadArray(GraphVisitor * v,Inst * i)57 void ObjectTypePropagation::VisitLoadArray([[maybe_unused]] GraphVisitor *v, [[maybe_unused]] Inst *i)
58 {
59 // LoadArray should be processed more carefully, because it may contain object of the derived class with own method
60 // implementation. We need to check all array stores and method calls between NewArray and LoadArray.
61 // TODO(mshertennikov): Support it.
62 }
63 } // namespace panda::compiler
64