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 "loop_peeling.h"
17
18 #include <llvm/Analysis/LoopInfo.h>
19 #include <llvm/Analysis/MemoryBuiltins.h>
20 #include <llvm/IR/Dominators.h>
21 #include <llvm/IR/Operator.h>
22 #include <llvm/Support/KnownBits.h>
23 #include <llvm/Transforms/Utils/LoopPeel.h>
24
25 #include "transforms/builtins.h"
26 #include "transforms/transform_utils.h"
27
28 #define DEBUG_TYPE "ark-loop-peeling"
29
30 // Peel only loops with GVNable builtins
31 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects)
32 static llvm::cl::opt<bool> g_gvnOnly("ark-loop-peeling-gvn-only", llvm::cl::Hidden, llvm::cl::init(false));
33
34 namespace ark::llvmbackend::passes {
35
run(llvm::Loop & loop,llvm::LoopAnalysisManager & analysisManager,llvm::LoopStandardAnalysisResults & loopStandardAnalysisResults,llvm::LPMUpdater & lpmUpdater)36 llvm::PreservedAnalyses ArkLoopPeeling::run(llvm::Loop &loop,
37 [[maybe_unused]] llvm::LoopAnalysisManager &analysisManager,
38 llvm::LoopStandardAnalysisResults &loopStandardAnalysisResults,
39 [[maybe_unused]] llvm::LPMUpdater &lpmUpdater)
40 {
41 if (llvm::canPeel(&loop)) {
42 if (!g_gvnOnly || ContainsGvnBuiltin(&loop)) {
43 [[maybe_unused]] bool result =
44 llvm::peelLoop(&loop, 1U, &loopStandardAnalysisResults.LI, &loopStandardAnalysisResults.SE,
45 loopStandardAnalysisResults.DT, nullptr, true);
46 // Always returns true, false is unexpected
47 ASSERT(result);
48 return llvm::PreservedAnalyses::none();
49 }
50 }
51 return llvm::PreservedAnalyses::all();
52 }
53
ContainsGvnBuiltin(llvm::Loop * loop)54 bool ArkLoopPeeling::ContainsGvnBuiltin(llvm::Loop *loop)
55 {
56 ASSERT(loop->getHeader() != nullptr);
57 auto module = loop->getHeader()->getModule();
58 std::array builtins = {llvmbackend::builtins::LoadClass(module), llvmbackend::builtins::LoadInitClass(module),
59 llvmbackend::builtins::LoadString(module), llvmbackend::builtins::ResolveVirtual(module)};
60
61 for (auto &block : loop->blocks()) {
62 for (auto &inst : *block) {
63 auto call = llvm::dyn_cast<llvm::CallInst>(&inst);
64 if (call == nullptr) {
65 continue;
66 }
67 auto function = call->getCalledFunction();
68 if (std::find(builtins.cbegin(), builtins.cend(), function) != builtins.cend()) {
69 return true;
70 }
71 }
72 }
73 return false;
74 }
75
ShouldInsert(const ark::llvmbackend::LLVMCompilerOptions * options)76 bool ArkLoopPeeling::ShouldInsert([[maybe_unused]] const ark::llvmbackend::LLVMCompilerOptions *options)
77 {
78 return true;
79 }
80 } // namespace ark::llvmbackend::passes
81