• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "fixup_poisons.h"
17 
18 #include <llvm/IR/Constants.h>
19 
20 #include "transforms/gc_utils.h"
21 
22 #define DEBUG_TYPE "fixup-poisons"
23 
24 using ark::llvmbackend::gc_utils::IsGcRefType;
25 
26 namespace ark::llvmbackend::passes {
27 
28 FixupPoisons::FixupPoisons() = default;
29 
FixupInstructionOperands(llvm::Instruction & instruction)30 bool FixupPoisons::FixupInstructionOperands(llvm::Instruction &instruction)
31 {
32     bool changed = false;
33     for (auto operand : instruction.operand_values()) {
34         auto poison = llvm::dyn_cast<llvm::PoisonValue>(operand);
35         if (poison == nullptr) {
36             continue;
37         }
38         if (IsGcRefType(poison->getType())) {
39             LLVM_DEBUG(llvm::dbgs() << "Replacing poison in inst:\n");
40             LLVM_DEBUG(instruction.print(llvm::dbgs()));
41             LLVM_DEBUG(llvm::dbgs() << "with null pointer");
42 
43             auto replacement = llvm::Constant::getNullValue(poison->getType());
44             poison->replaceAllUsesWith(replacement);
45             changed = true;
46         }
47     }
48     return changed;
49 }
50 
run(llvm::Function & function,llvm::FunctionAnalysisManager & analysisManager)51 llvm::PreservedAnalyses FixupPoisons::run(llvm::Function &function,
52                                           [[maybe_unused]] llvm::FunctionAnalysisManager &analysisManager)
53 {
54     bool changed = false;
55 
56     for (auto &basicBlock : function) {
57         for (auto &instruction : basicBlock) {
58             changed |= FixupInstructionOperands(instruction);
59         }
60     }
61 
62     return changed ? llvm::PreservedAnalyses::none() : llvm::PreservedAnalyses::all();
63 }
64 
65 }  // namespace ark::llvmbackend::passes
66