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