• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- UnrollLoopTest.cpp - Unit tests for UnrollLoop ---------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Transforms/Utils/UnrollLoop.h"
10 #include "llvm/Analysis/AssumptionCache.h"
11 #include "llvm/Analysis/LoopInfo.h"
12 #include "llvm/Analysis/ScalarEvolution.h"
13 #include "llvm/Analysis/TargetLibraryInfo.h"
14 #include "llvm/AsmParser/Parser.h"
15 #include "llvm/IR/BasicBlock.h"
16 #include "llvm/IR/Dominators.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "gtest/gtest.h"
20 
21 using namespace llvm;
22 
parseIR(LLVMContext & C,const char * IR)23 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
24   SMDiagnostic Err;
25   std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
26   if (!Mod)
27     Err.print("UnrollLoopTests", errs());
28   return Mod;
29 }
30 
TEST(LoopUnrollRuntime,Latch)31 TEST(LoopUnrollRuntime, Latch) {
32   LLVMContext C;
33 
34   std::unique_ptr<Module> M = parseIR(
35     C,
36     R"(define i32 @test(i32* %a, i32* %b, i32* %c, i64 %n) {
37 entry:
38   br label %while.cond
39 
40 while.cond:                                       ; preds = %while.body, %entry
41   %i.0 = phi i64 [ 0, %entry ], [ %inc, %while.body ]
42   %cmp = icmp slt i64 %i.0, %n
43   br i1 %cmp, label %while.body, label %while.end
44 
45 while.body:                                       ; preds = %while.cond
46   %arrayidx = getelementptr inbounds i32, i32* %b, i64 %i.0
47   %0 = load i32, i32* %arrayidx
48   %arrayidx1 = getelementptr inbounds i32, i32* %c, i64 %i.0
49   %1 = load i32, i32* %arrayidx1
50   %mul = mul nsw i32 %0, %1
51   %arrayidx2 = getelementptr inbounds i32, i32* %a, i64 %i.0
52   store i32 %mul, i32* %arrayidx2
53   %inc = add nsw i64 %i.0, 1
54   br label %while.cond
55 
56 while.end:                                        ; preds = %while.cond
57   ret i32 0
58 })"
59     );
60 
61   auto *F = M->getFunction("test");
62   DominatorTree DT(*F);
63   LoopInfo LI(DT);
64   AssumptionCache AC(*F);
65   TargetLibraryInfoImpl TLII;
66   TargetLibraryInfo TLI(TLII);
67   ScalarEvolution SE(*F, TLI, AC, DT, LI);
68 
69   Loop *L = *LI.begin();
70 
71   bool PreserveLCSSA = L->isRecursivelyLCSSAForm(DT,LI);
72 
73   bool ret =
74       UnrollRuntimeLoopRemainder(L, 4, true, false, false, false, &LI, &SE, &DT,
75                                  &AC, /*TTI=*/nullptr, PreserveLCSSA);
76   EXPECT_FALSE(ret);
77 }
78