1 //===- LoopRotationUtilsTest.cpp - Unit tests for LoopRotation utility ----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/Utils/LoopRotationUtils.h"
10 #include "llvm/Analysis/AssumptionCache.h"
11 #include "llvm/Analysis/InstructionSimplify.h"
12 #include "llvm/Analysis/LoopInfo.h"
13 #include "llvm/Analysis/ScalarEvolution.h"
14 #include "llvm/Analysis/TargetLibraryInfo.h"
15 #include "llvm/Analysis/TargetTransformInfo.h"
16 #include "llvm/AsmParser/Parser.h"
17 #include "llvm/IR/Dominators.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "gtest/gtest.h"
24 static std::unique_ptr
<Module
> parseIR(LLVMContext
&C
, const char *IR
) {
26 std::unique_ptr
<Module
> Mod
= parseAssemblyString(IR
, Err
, C
);
28 Err
.print("LoopRotationUtilsTest", errs());
32 /// This test contains multi-deopt-exits pattern that might allow loop rotation
33 /// to trigger multiple times if multiple rotations are enabled.
34 /// At least one rotation should be performed, no matter what loop rotation settings are.
35 TEST(LoopRotate
, MultiDeoptExit
) {
38 std::unique_ptr
<Module
> M
= parseIR(
41 declare i32 @llvm.experimental.deoptimize.i32(...)
43 define i32 @test(i32 * nonnull %a, i64 %x) {
48 %idx = phi i64 [ 0, %entry ], [ %idx.next, %for.tail ]
49 %sum = phi i32 [ 0, %entry ], [ %sum.next, %for.tail ]
50 %a.idx = getelementptr inbounds i32, i32 *%a, i64 %idx
51 %val.a.idx = load i32, i32* %a.idx, align 4
52 %zero.check = icmp eq i32 %val.a.idx, 0
53 br i1 %zero.check, label %deopt.exit, label %for.cond2
56 %for.check = icmp ult i64 %idx, %x
57 br i1 %for.check, label %for.body, label %return
63 %sum.next = add i32 %sum, %val.a.idx
64 %idx.next = add nuw nsw i64 %idx, 1
71 %deopt.val = call i32(...) @llvm.experimental.deoptimize.i32() [ "deopt
"(i32 %val.a.idx) ]
76 auto *F
= M
->getFunction("test");
79 AssumptionCache
AC(*F
);
80 TargetTransformInfo
TTI(M
->getDataLayout());
81 TargetLibraryInfoImpl TLII
;
82 TargetLibraryInfo
TLI(TLII
);
83 ScalarEvolution
SE(*F
, TLI
, AC
, DT
, LI
);
84 SimplifyQuery
SQ(M
->getDataLayout());
86 Loop
*L
= *LI
.begin();
88 bool ret
= LoopRotation(L
, &LI
, &TTI
,
95 /// Checking a special case of multi-deopt exit loop that can not perform
96 /// required amount of rotations due to the desired header containing
97 /// non-duplicatable code.
98 /// Similar to MultiDeoptExit test this one should do at least one rotation and
99 /// pass no matter what loop rotation settings are.
100 TEST(LoopRotate
, MultiDeoptExit_Nondup
) {
103 std::unique_ptr
<Module
> M
= parseIR(
106 ; Rotation should be done once, attempted twice.
107 ; Second time fails due to non-duplicatable header.
109 declare i32 @llvm.experimental.deoptimize.i32(...)
111 declare void @nondup()
113 define i32 @test_nondup(i32 * nonnull %a, i64 %x) {
118 %idx = phi i64 [ 0, %entry ], [ %idx.next, %for.tail ]
119 %sum = phi i32 [ 0, %entry ], [ %sum.next, %for.tail ]
120 %a.idx = getelementptr inbounds i32, i32 *%a, i64 %idx
121 %val.a.idx = load i32, i32* %a.idx, align 4
122 %zero.check = icmp eq i32 %val.a.idx, 0
123 br i1 %zero.check, label %deopt.exit, label %for.cond2
126 call void @nondup() noduplicate
127 %for.check = icmp ult i64 %idx, %x
128 br i1 %for.check, label %for.body, label %return
134 %sum.next = add i32 %sum, %val.a.idx
135 %idx.next = add nuw nsw i64 %idx, 1
142 %deopt.val = call i32(...) @llvm.experimental.deoptimize.i32() [ "deopt
"(i32 %val.a.idx) ]
147 auto *F
= M
->getFunction("test_nondup");
148 DominatorTree
DT(*F
);
150 AssumptionCache
AC(*F
);
151 TargetTransformInfo
TTI(M
->getDataLayout());
152 TargetLibraryInfoImpl TLII
;
153 TargetLibraryInfo
TLI(TLII
);
154 ScalarEvolution
SE(*F
, TLI
, AC
, DT
, LI
);
155 SimplifyQuery
SQ(M
->getDataLayout());
157 Loop
*L
= *LI
.begin();
159 bool ret
= LoopRotation(L
, &LI
, &TTI
,
162 SQ
, true, -1, false);
163 /// LoopRotation should properly report "true" as we still perform the first rotation
164 /// so we do change the IR.