VPlanLoopInfoTest.cpp 2.8 KB

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