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_OBJECT_TYPE_CHECK_ELIMINATION_H 17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_OBJECT_TYPE_CHECK_ELIMINATION_H 18 19 #include "optimizer/ir/graph.h" 20 #include "compiler_logger.h" 21 #include "optimizer/pass.h" 22 #include "deoptimize_elimination.h" 23 #include "optimizer/ir/graph_visitor.h" 24 25 namespace ark::compiler { 26 // NOLINTNEXTLINE(fuchsia-multiple-inheritance) 27 class ObjectTypeCheckElimination : public Optimization, public GraphVisitor { 28 using GraphVisitor::GraphVisitor; 29 using Optimization::Optimization; 30 31 public: ObjectTypeCheckElimination(Graph * graph)32 explicit ObjectTypeCheckElimination(Graph *graph) 33 : Optimization(graph), checksMustThrow_(graph->GetLocalAllocator()->Adapter()) 34 { 35 } 36 37 NO_MOVE_SEMANTIC(ObjectTypeCheckElimination); 38 NO_COPY_SEMANTIC(ObjectTypeCheckElimination); 39 ~ObjectTypeCheckElimination() override = default; 40 GetBlocksToVisit()41 const ArenaVector<BasicBlock *> &GetBlocksToVisit() const override 42 { 43 return GetGraph()->GetBlocksRPO(); 44 } 45 46 bool RunImpl() override; 47 GetPassName()48 const char *GetPassName() const override 49 { 50 return "ObjectTypeCheckElimination"; 51 } 52 IsApplied()53 bool IsApplied() const 54 { 55 return isApplied_; 56 } 57 58 void InvalidateAnalyses() override; 59 60 /** 61 * This function try to replace IsInstance with a constant. 62 * If input of IsInstance is Nullptr then it replaced by zero constant. 63 */ 64 static bool TryEliminateIsInstance(Inst *inst); 65 66 enum CheckCastEliminateType { REDUNDANT, MUST_THROW, INVALID }; 67 68 static CheckCastEliminateType TryEliminateCheckCast(Inst *inst); 69 70 static void VisitIsInstance(GraphVisitor *visitor, Inst *inst); 71 static void VisitCheckCast(GraphVisitor *visitor, Inst *inst); 72 73 #include "optimizer/ir/visitor.inc" 74 private: PushNewCheckMustThrow(Inst * inst)75 void PushNewCheckMustThrow(Inst *inst) 76 { 77 checksMustThrow_.push_back(inst); 78 } 79 80 void ReplaceCheckMustThrowByUnconditionalDeoptimize(); 81 82 static bool IsMember(Inst *inst, uint32_t typeId, Inst *refUser); 83 static bool IsSuccessfulIsInstance(IsInstanceInst *isInstance, uint32_t typeId, Inst *refUser); 84 SetApplied()85 void SetApplied() 86 { 87 isApplied_ = true; 88 } 89 90 private: 91 bool isApplied_ {false}; 92 InstVector checksMustThrow_; 93 }; 94 } // namespace ark::compiler 95 96 #endif // COMPILER_OPTIMIZER_OPTIMIZATIONS_OBJECT_TYPE_CHECK_ELIMINATION_H 97