1 //===- llvm/unittests/Transforms/Vectorize/VPlanLoopInfoTest.cpp -----===//
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 "../lib/Transforms/Vectorize/VPlanLoopInfo.h"
10 #include "VPlanTestBase.h"
11 #include "gtest/gtest.h"
12 
13 namespace llvm {
14 namespace {
15 
16 class VPlanLoopInfo : public VPlanTestBase {};
17 
TEST_F(VPlanLoopInfo,BasicLoopInfoTest)18 TEST_F(VPlanLoopInfo, BasicLoopInfoTest) {
19   const char *ModuleString =
20       "define void @f(i32* %a, i32* %b, i32* %c, i32 %N, i32 %M, i32 %K) {\n"
21       "entry:\n"
22       "  br label %for.body\n"
23       "for.body:\n"
24       "  %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.inc ]\n"
25       "  br i1 true, label %if.then, label %if.else\n"
26       "if.then:\n"
27       "  br label %for.inc\n"
28       "if.else:\n"
29       "  br label %for.inc\n"
30       "for.inc:\n"
31       "  %iv.next = add nuw nsw i64 %iv, 1\n"
32       "  %exitcond = icmp eq i64 %iv.next, 300\n"
33       "  br i1 %exitcond, label %for.end, label %for.body\n"
34       "for.end:\n"
35       "  ret void\n"
36       "}\n";
37 
38   Module &M = parseModule(ModuleString);
39 
40   Function *F = M.getFunction("f");
41   BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
42   auto Plan = buildHCFG(LoopHeader);
43 
44   // Build VPlan domination tree and loop info analyses.
45   VPRegionBlock *TopRegion = cast<VPRegionBlock>(Plan->getEntry());
46   VPDominatorTree VPDT;
47   VPDT.recalculate(*TopRegion);
48   VPLoopInfo VPLI;
49   VPLI.analyze(VPDT);
50 
51   VPBlockBase *PH = TopRegion->getEntry();
52   VPBlockBase *H = PH->getSingleSuccessor();
53   VPBlockBase *IfThen = H->getSuccessors()[0];
54   VPBlockBase *IfElse = H->getSuccessors()[1];
55   VPBlockBase *Latch = IfThen->getSingleSuccessor();
56   VPBlockBase *Exit = Latch->getSuccessors()[0] != H
57                           ? Latch->getSuccessors()[0]
58                           : Latch->getSuccessors()[1];
59 
60   // Number of loops.
61   EXPECT_EQ(1, std::distance(VPLI.begin(), VPLI.end()));
62   VPLoop *VPLp = *VPLI.begin();
63 
64   // VPBBs contained in VPLoop.
65   EXPECT_FALSE(VPLp->contains(PH));
66   EXPECT_EQ(nullptr, VPLI.getLoopFor(PH));
67   EXPECT_TRUE(VPLp->contains(H));
68   EXPECT_EQ(VPLp, VPLI.getLoopFor(H));
69   EXPECT_TRUE(VPLp->contains(IfThen));
70   EXPECT_EQ(VPLp, VPLI.getLoopFor(IfThen));
71   EXPECT_TRUE(VPLp->contains(IfElse));
72   EXPECT_EQ(VPLp, VPLI.getLoopFor(IfElse));
73   EXPECT_TRUE(VPLp->contains(Latch));
74   EXPECT_EQ(VPLp, VPLI.getLoopFor(Latch));
75   EXPECT_FALSE(VPLp->contains(Exit));
76   EXPECT_EQ(nullptr, VPLI.getLoopFor(Exit));
77 
78   // VPLoop's parts.
79   EXPECT_EQ(PH, VPLp->getLoopPreheader());
80   EXPECT_EQ(H, VPLp->getHeader());
81   EXPECT_EQ(Latch, VPLp->getLoopLatch());
82   EXPECT_EQ(Latch, VPLp->getExitingBlock());
83   EXPECT_EQ(Exit, VPLp->getExitBlock());
84 }
85 } // namespace
86 } // namespace llvm
87