[InstCombine] Signed saturation patterns
[llvm-core.git] / unittests / Transforms / Utils / UnrollLoopTest.cpp
blobfba21a76867e5d59ac5b85baba472e232c55b8df
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 //===----------------------------------------------------------------------===//
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"
21 using namespace llvm;
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;
31 TEST(LoopUnrollRuntime, Latch) {
32 LLVMContext C;
34 std::unique_ptr<Module> M = parseIR(
36 R"(define i32 @test(i32* %a, i32* %b, i32* %c, i64 %n) {
37 entry:
38 br label %while.cond
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
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
56 while.end: ; preds = %while.cond
57 ret i32 0
58 })"
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);
69 Loop *L = *LI.begin();
71 bool PreserveLCSSA = L->isRecursivelyLCSSAForm(DT,LI);
73 bool ret = UnrollRuntimeLoopRemainder(L, 4, true, false, false, false, &LI,
74 &SE, &DT, &AC, PreserveLCSSA);
75 EXPECT_FALSE(ret);