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_INTRINSICS_LOWERING_H 17 #define LIBLLVMBACKEND_TRANSFORMS_PASSES_INTRINSICS_LOWERING_H 18 19 #include <unordered_map> 20 21 #include "llvm_ark_interface.h" 22 23 #include <llvm/IR/Intrinsics.h> 24 #include <llvm/IR/PassManager.h> 25 26 namespace ark::llvmbackend { 27 struct LLVMCompilerOptions; 28 } // namespace ark::llvmbackend 29 30 namespace llvm { 31 class CallInst; 32 class Instruction; 33 } // namespace llvm 34 35 namespace ark::llvmbackend::passes { 36 37 class IntrinsicsLowering : public llvm::PassInfoMixin<IntrinsicsLowering> { 38 public: 39 explicit IntrinsicsLowering(LLVMArkInterface *arkInterface = nullptr); 40 ShouldInsert(const ark::llvmbackend::LLVMCompilerOptions * options)41 static bool ShouldInsert([[maybe_unused]] const ark::llvmbackend::LLVMCompilerOptions *options) 42 { 43 return true; 44 } 45 46 static IntrinsicsLowering Create(LLVMArkInterface *arkInterface, 47 const ark::llvmbackend::LLVMCompilerOptions *options); 48 49 // NOLINTNEXTLINE(readability-identifier-naming) 50 llvm::PreservedAnalyses run(llvm::Function &function, llvm::FunctionAnalysisManager &analysisManager); 51 52 private: 53 bool ReplaceWithLLVMIntrinsic(llvm::CallInst *call, llvm::Intrinsic::ID intrinsicId); 54 55 void HandleMemCall(llvm::CallInst *call, llvm::FunctionCallee callee, 56 std::unordered_map<llvm::Instruction *, llvm::Instruction *> *instToReplaceWithInst); 57 bool HandleCall(llvm::CallInst *call, LLVMArkInterface::IntrinsicId intrinsicId, 58 std::unordered_map<llvm::Instruction *, llvm::Instruction *> *instToReplaceWithInst); 59 60 bool HandleFRem(llvm::Instruction *inst, LLVMArkInterface::IntrinsicId intrinsicId, 61 std::unordered_map<llvm::Instruction *, llvm::Instruction *> *instToReplaceWithInst); 62 63 private: 64 LLVMArkInterface *arkInterface_; 65 66 public: 67 static constexpr llvm::StringRef ARG_NAME = "intrinsics-lowering"; 68 }; 69 70 } // namespace ark::llvmbackend::passes 71 72 #endif // LIBLLVMBACKEND_TRANSFORMS_PASSES_INTRINSICS_LOWERING_H 73