• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "propagate_lenarray.h"
17 
18 #include <llvm/Analysis/LoopInfo.h>
19 #include <llvm/Analysis/ScalarEvolution.h>
20 #include <llvm/Analysis/ScalarEvolutionExpressions.h>
21 #include <llvm/Analysis/MemoryBuiltins.h>
22 #include <llvm/Analysis/ValueTracking.h>
23 #include <llvm/IR/Dominators.h>
24 #include <llvm/IR/Instructions.h>
25 #include <llvm/IR/InstIterator.h>
26 #include <llvm/IR/Operator.h>
27 #include <llvm/Support/KnownBits.h>
28 #include <llvm/Transforms/Utils/LoopPeel.h>
29 
30 #include "transforms/builtins.h"
31 
32 #define DEBUG_TYPE "propagate-lenarray"
33 
34 namespace ark::llvmbackend::passes {
35 
ShouldInsert(const ark::llvmbackend::LLVMCompilerOptions * options)36 bool PropagateLenArray::ShouldInsert([[maybe_unused]] const ark::llvmbackend::LLVMCompilerOptions *options)
37 {
38     return true;
39 }
40 
run(llvm::Function & function,llvm::FunctionAnalysisManager & analysisManager)41 llvm::PreservedAnalyses PropagateLenArray::run(llvm::Function &function,
42                                                [[maybe_unused]] llvm::FunctionAnalysisManager &analysisManager)
43 {
44     LLVM_DEBUG(llvm::dbgs() << "Running on '" << function.getName() << "'\n");
45 
46     llvm::DenseMap<llvm::CallInst *, llvm::ConstantInt *> replace {};
47     for (auto &inst : llvm::instructions(function)) {
48         auto call = llvm::dyn_cast<llvm::CallInst>(&inst);
49         if (call == nullptr || call->getCalledFunction() != llvmbackend::builtins::LenArray(function.getParent())) {
50             continue;
51         }
52 
53         auto maybeAlloc = llvm::dyn_cast<llvm::CallInst>(call->getArgOperand(0));
54         // Matching AllocateArrayTlab*: first arg is klass, second is size
55         // NOTE(zdenis): Attribute for size
56         // NOTE(zdenis): Support MultiArray
57         if (maybeAlloc != nullptr && maybeAlloc->arg_size() == 2U && llvm::isAllocLikeFn(maybeAlloc, nullptr)) {
58             auto maybeSize = llvm::dyn_cast<llvm::ConstantInt>(maybeAlloc->getArgOperand(1U));
59             if (maybeSize != nullptr) {
60                 replace.insert({call, maybeSize});
61             }
62         }
63     }
64 
65     bool changed = false;
66     for (auto &[lenArray, size] : replace) {
67         LLVM_DEBUG(llvm::dbgs() << "Replacing " << *lenArray << " with " << *size << "\n");
68         auto cast = llvm::CastInst::Create(llvm::CastInst::Trunc, size,
69                                            llvmbackend::builtins::LenArray(function.getParent())->getReturnType());
70         cast->insertBefore(lenArray);
71         lenArray->replaceAllUsesWith(cast);
72         changed = true;
73     }
74 
75     return changed ? llvm::PreservedAnalyses::none() : llvm::PreservedAnalyses::all();
76 }
77 }  // namespace ark::llvmbackend::passes
78