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 llvm.experimental.deoptimize calls
31 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects)
32 static llvm::cl::opt<bool> g_deoptimizeOnly("ark-loop-peeling-deoptimize-only", llvm::cl::Hidden, llvm::cl::init(true));
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_deoptimizeOnly || ContainsDeoptimize(&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
ContainsDeoptimize(llvm::Loop * loop)54 bool ArkLoopPeeling::ContainsDeoptimize(llvm::Loop *loop)
55 {
56 ASSERT(loop->getHeader() != nullptr);
57 for (auto &block : loop->blocks()) {
58 for (auto &inst : *block) {
59 auto call = llvm::dyn_cast<llvm::CallInst>(&inst);
60 if (call == nullptr) {
61 continue;
62 }
63 if (call->getIntrinsicID() == llvm::Intrinsic::experimental_deoptimize) {
64 return true;
65 }
66 }
67 }
68 return false;
69 }
70
ShouldInsert(const ark::llvmbackend::LLVMCompilerOptions * options)71 bool ArkLoopPeeling::ShouldInsert([[maybe_unused]] const ark::llvmbackend::LLVMCompilerOptions *options)
72 {
73 return true;
74 }
75 } // namespace ark::llvmbackend::passes
76