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/IR/Module.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "gtest/gtest.h"
25 static std::unique_ptr
<Module
> parseIR(LLVMContext
&C
, const char *IR
) {
27 std::unique_ptr
<Module
> Mod
= parseAssemblyString(IR
, Err
, C
);
29 Err
.print("LoopRotationUtilsTest", errs());
33 /// This test contains multi-deopt-exits pattern that might allow loop rotation
34 /// to trigger multiple times if multiple rotations are enabled.
35 /// At least one rotation should be performed, no matter what loop rotation settings are.
36 TEST(LoopRotate
, MultiDeoptExit
) {
39 std::unique_ptr
<Module
> M
= parseIR(
42 declare i32 @llvm.experimental.deoptimize.i32(...)
44 define i32 @test(ptr nonnull %a, i64 %x) {
49 %idx = phi i64 [ 0, %entry ], [ %idx.next, %for.tail ]
50 %sum = phi i32 [ 0, %entry ], [ %sum.next, %for.tail ]
51 %a.idx = getelementptr inbounds i32, ptr %a, i64 %idx
52 %val.a.idx = load i32, ptr %a.idx, align 4
53 %zero.check = icmp eq i32 %val.a.idx, 0
54 br i1 %zero.check, label %deopt.exit, label %for.cond2
57 %for.check = icmp ult i64 %idx, %x
58 br i1 %for.check, label %for.body, label %return
64 %sum.next = add i32 %sum, %val.a.idx
65 %idx.next = add nuw nsw i64 %idx, 1
72 %deopt.val = call i32(...) @llvm.experimental.deoptimize.i32() [ "deopt
"(i32 %val.a.idx) ]
77 auto *F
= M
->getFunction("test");
80 AssumptionCache
AC(*F
);
81 TargetTransformInfo
TTI(M
->getDataLayout());
82 TargetLibraryInfoImpl TLII
;
83 TargetLibraryInfo
TLI(TLII
);
84 ScalarEvolution
SE(*F
, TLI
, AC
, DT
, LI
);
85 SimplifyQuery
SQ(M
->getDataLayout());
87 Loop
*L
= *LI
.begin();
89 bool ret
= LoopRotation(L
, &LI
, &TTI
,
96 /// Checking a special case of multi-deopt exit loop that can not perform
97 /// required amount of rotations due to the desired header containing
98 /// non-duplicatable code.
99 /// Similar to MultiDeoptExit test this one should do at least one rotation and
100 /// pass no matter what loop rotation settings are.
101 TEST(LoopRotate
, MultiDeoptExit_Nondup
) {
104 std::unique_ptr
<Module
> M
= parseIR(
107 ; Rotation should be done once, attempted twice.
108 ; Second time fails due to non-duplicatable header.
110 declare i32 @llvm.experimental.deoptimize.i32(...)
112 declare void @nondup()
114 define i32 @test_nondup(ptr nonnull %a, i64 %x) {
119 %idx = phi i64 [ 0, %entry ], [ %idx.next, %for.tail ]
120 %sum = phi i32 [ 0, %entry ], [ %sum.next, %for.tail ]
121 %a.idx = getelementptr inbounds i32, ptr %a, i64 %idx
122 %val.a.idx = load i32, ptr %a.idx, align 4
123 %zero.check = icmp eq i32 %val.a.idx, 0
124 br i1 %zero.check, label %deopt.exit, label %for.cond2
127 call void @nondup() noduplicate
128 %for.check = icmp ult i64 %idx, %x
129 br i1 %for.check, label %for.body, label %return
135 %sum.next = add i32 %sum, %val.a.idx
136 %idx.next = add nuw nsw i64 %idx, 1
143 %deopt.val = call i32(...) @llvm.experimental.deoptimize.i32() [ "deopt
"(i32 %val.a.idx) ]
148 auto *F
= M
->getFunction("test_nondup");
149 DominatorTree
DT(*F
);
151 AssumptionCache
AC(*F
);
152 TargetTransformInfo
TTI(M
->getDataLayout());
153 TargetLibraryInfoImpl TLII
;
154 TargetLibraryInfo
TLI(TLII
);
155 ScalarEvolution
SE(*F
, TLI
, AC
, DT
, LI
);
156 SimplifyQuery
SQ(M
->getDataLayout());
158 Loop
*L
= *LI
.begin();
160 bool ret
= LoopRotation(L
, &LI
, &TTI
,
163 SQ
, true, -1, false);
164 /// LoopRotation should properly report "true" as we still perform the first rotation
165 /// so we do change the IR.