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_DANGLING_POINTERS_CHECKER_H 17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_DANGLING_POINTERS_CHECKER_H 18 19 #include "compiler/optimizer/pass.h" 20 #include "compiler/optimizer/ir/basicblock.h" 21 #include "compiler/compiler_options.h" 22 23 namespace ark::compiler { 24 class DanglingPointersChecker : public Analysis { 25 public: 26 explicit DanglingPointersChecker(Graph *graph); 27 28 NO_MOVE_SEMANTIC(DanglingPointersChecker); 29 NO_COPY_SEMANTIC(DanglingPointersChecker); 30 ~DanglingPointersChecker() override = default; 31 32 bool RunImpl() override; 33 GetPassName()34 const char *GetPassName() const override 35 { 36 return "DanglingPointersChecker"; 37 } 38 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects) 39 inline static std::unordered_map<Arch, std::unordered_map<std::string, size_t>> regmap_ { 40 // NOLINTNEXTLINE(readability-magic-numbers) 41 {Arch::AARCH64, {{"thread", 28}, {"frame", 23}, {"acc", 21}, {"acc_tag", 22}}}, 42 // NOLINTNEXTLINE(readability-magic-numbers) 43 {Arch::X86_64, {{"thread", 15}, {"frame", 5}, {"acc", 11}, {"acc_tag", 3}}}}; 44 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects) 45 inline static std::unordered_set<std::string> targetFuncs_ { 46 "CreateArrayByIdEntrypoint", "CreateObjectByClassInterpreter", "CreateMultiArrayRecEntrypoint", 47 "ResolveLiteralArrayByIdEntrypoint", "GetStaticFieldByIdEntrypoint", "GetCalleeMethodFromBytecodeId", 48 "ResolveTypeByIdEntrypoint", "CreateMultiDimensionalArrayById", "SafepointEntrypointInterp"}; 49 50 private: 51 ArenaVector<const Inst *> objectsUsers_; 52 ArenaSet<const BasicBlock *> checkedBlocks_; 53 ArenaVector<Inst *> phiInsts_; 54 ArenaSet<Inst *> objectInsts_; 55 Inst *accLivein_ {nullptr}; 56 Inst *accTagLivein_ {nullptr}; 57 Inst *frameLivein_ {nullptr}; 58 Inst *threadLivein_ {nullptr}; 59 Inst *lastAccDef_ {nullptr}; 60 Inst *lastAccTagDef_ {nullptr}; 61 Inst *lastFrameDef_ {nullptr}; 62 63 bool CheckAccSyncCallRuntime(); 64 void InitLiveIns(); 65 void UpdateLastAccAndFrameDef(Inst *inst); 66 void GetLastAccDefinition(CallInst *runtimeCallInst); 67 std::tuple<Inst *, Inst *> GetAccAndFrameDefs(Inst *inst); 68 std::tuple<Inst *, Inst *> GetPhiAccDef(); 69 std::tuple<Inst *, Inst *> GetAccDefFromInputs(Inst *inst); 70 71 Inst *GetPhiAccTagDef(); 72 bool IsAccTagDefInInputs(Inst *inst); 73 bool IsAccTagDef(Inst *inst); 74 bool IsFrameDef(Inst *inst); 75 bool IsAccPtr(Inst *inst); 76 bool IsAccTagPtr(Inst *inst); 77 bool IsSaveAcc(const Inst *inst); 78 bool CheckObjectInputs(Inst *inst); 79 bool CheckStoreAcc(CallInst *runtimeCallInst); 80 bool CheckStoreAccTag(CallInst *runtimeCallInst); 81 bool CheckAccUsers(CallInst *runtimeCallInst); 82 bool CheckObjectsUsers(CallInst *runtimeCallInst); 83 bool CheckUsers(CallInst *runtimeCallInst); 84 bool CheckSuccessors(BasicBlock *bb, bool prevRes); 85 }; 86 } // namespace ark::compiler 87 88 #endif 89