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 #ifndef LIBLLVMBACKEND_TRANSFORMS_PASSES_INLINE_IR_REMOVE_UNUSED_FUNCTIONS_H 17 #define LIBLLVMBACKEND_TRANSFORMS_PASSES_INLINE_IR_REMOVE_UNUSED_FUNCTIONS_H 18 19 #include <llvm/ADT/DenseSet.h> 20 #include <llvm/IR/PassManager.h> 21 22 namespace ark::llvmbackend { 23 struct LLVMCompilerOptions; 24 } // namespace ark::llvmbackend 25 26 namespace llvm { 27 class Function; 28 class Module; 29 class Value; 30 } // namespace llvm 31 32 namespace ark::llvmbackend::passes { 33 34 /** 35 * Remove unused functions from the module. 36 * 37 * The function is unused if it is not used by any root function. 38 * A root function is a function without FUNCTION_MD_INLINE_MODULE metadata 39 */ 40 class RemoveUnusedFunctions : public llvm::PassInfoMixin<RemoveUnusedFunctions> { 41 public: 42 static bool ShouldInsert(const ark::llvmbackend::LLVMCompilerOptions *options); 43 44 // NOLINTNEXTLINE(readability-identifier-naming) 45 llvm::PreservedAnalyses run(llvm::Module &module, llvm::ModuleAnalysisManager &analysis_manager); 46 47 private: 48 void VisitValue(llvm::DenseSet<llvm::Function *> &usedFunctions, llvm::Value &value, 49 llvm::DenseSet<llvm::Value *> &seenValues); 50 51 public: 52 static constexpr llvm::StringRef ARG_NAME = "remove-unused-functions"; 53 }; 54 55 } // namespace ark::llvmbackend::passes 56 57 #endif // LIBLLVMBACKEND_TRANSFORMS_PASSES_INLINE_IR_REMOVE_UNUSED_FUNCTIONS_H 58