VPlanPredicatorTest.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //===- llvm/unittests/Transforms/Vectorize/VPlanPredicatorTest.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. #include "../lib/Transforms/Vectorize/VPlanPredicator.h"
  10. #include "VPlanTestBase.h"
  11. #include "gtest/gtest.h"
  12. namespace llvm {
  13. namespace {
  14. class VPlanPredicatorTest : public VPlanTestBase {};
  15. TEST_F(VPlanPredicatorTest, BasicPredicatorTest) {
  16. const char *ModuleString =
  17. "@arr = common global [8 x [8 x i64]] "
  18. "zeroinitializer, align 16\n"
  19. "@arr2 = common global [8 x [8 x i64]] "
  20. "zeroinitializer, align 16\n"
  21. "@arr3 = common global [8 x [8 x i64]] "
  22. "zeroinitializer, align 16\n"
  23. "define void @f(i64 %n1) {\n"
  24. "entry:\n"
  25. " br label %for.cond1.preheader\n"
  26. "for.cond1.preheader: \n"
  27. " %i1.029 = phi i64 [ 0, %entry ], [ %inc14, %for.inc13 ]\n"
  28. " br label %for.body3\n"
  29. "for.body3: \n"
  30. " %i2.028 = phi i64 [ 0, %for.cond1.preheader ], [ %inc, %for.inc ]\n"
  31. " %arrayidx4 = getelementptr inbounds [8 x [8 x i64]], [8 x [8 x i64]]* "
  32. "@arr, i64 0, i64 %i2.028, i64 %i1.029\n"
  33. " %0 = load i64, i64* %arrayidx4, align 8\n"
  34. " %cmp5 = icmp ugt i64 %0, 10\n"
  35. " br i1 %cmp5, label %if.then, label %for.inc\n"
  36. "if.then: \n"
  37. " %arrayidx7 = getelementptr inbounds [8 x [8 x i64]], [8 x [8 x i64]]* "
  38. "@arr2, i64 0, i64 %i2.028, i64 %i1.029\n"
  39. " %1 = load i64, i64* %arrayidx7, align 8\n"
  40. " %cmp8 = icmp ugt i64 %1, 100\n"
  41. " br i1 %cmp8, label %if.then9, label %for.inc\n"
  42. "if.then9: \n"
  43. " %add = add nuw nsw i64 %i2.028, %i1.029\n"
  44. " %arrayidx11 = getelementptr inbounds [8 x [8 x i64]], [8 x [8 x "
  45. "i64]]* @arr3, i64 0, i64 %i2.028, i64 %i1.029\n"
  46. " store i64 %add, i64* %arrayidx11, align 8\n"
  47. " br label %for.inc\n"
  48. "for.inc: \n"
  49. " %inc = add nuw nsw i64 %i2.028, 1\n"
  50. " %exitcond = icmp eq i64 %inc, 8\n"
  51. " br i1 %exitcond, label %for.inc13, label %for.body3\n"
  52. "for.inc13: \n"
  53. " %inc14 = add nuw nsw i64 %i1.029, 1\n"
  54. " %exitcond30 = icmp eq i64 %inc14, 8\n"
  55. " br i1 %exitcond30, label %for.end15, label %for.cond1.preheader\n"
  56. "for.end15: \n"
  57. " ret void\n"
  58. "}\n";
  59. Module &M = parseModule(ModuleString);
  60. Function *F = M.getFunction("f");
  61. BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
  62. auto Plan = buildHCFG(LoopHeader);
  63. VPRegionBlock *TopRegion = cast<VPRegionBlock>(Plan->getEntry());
  64. VPBlockBase *PH = TopRegion->getEntry();
  65. VPBlockBase *H = PH->getSingleSuccessor();
  66. VPBlockBase *InnerLoopH = H->getSingleSuccessor();
  67. VPBlockBase *OuterIf = InnerLoopH->getSuccessors()[0];
  68. VPBlockBase *InnerLoopLatch = InnerLoopH->getSuccessors()[1];
  69. VPBlockBase *InnerIf = OuterIf->getSuccessors()[0];
  70. VPValue *CBV1 = InnerLoopH->getCondBit();
  71. VPValue *CBV2 = OuterIf->getCondBit();
  72. // Apply predication.
  73. VPlanPredicator VPP(*Plan);
  74. VPP.predicate();
  75. VPBlockBase *InnerLoopLinSucc = InnerLoopH->getSingleSuccessor();
  76. VPBlockBase *OuterIfLinSucc = OuterIf->getSingleSuccessor();
  77. VPBlockBase *InnerIfLinSucc = InnerIf->getSingleSuccessor();
  78. VPValue *OuterIfPred = OuterIf->getPredicate();
  79. VPInstruction *InnerAnd =
  80. cast<VPInstruction>(InnerIf->getEntryBasicBlock()->begin());
  81. VPValue *InnerIfPred = InnerIf->getPredicate();
  82. // Test block predicates
  83. EXPECT_NE(nullptr, CBV1);
  84. EXPECT_NE(nullptr, CBV2);
  85. EXPECT_NE(nullptr, InnerAnd);
  86. EXPECT_EQ(CBV1, OuterIfPred);
  87. EXPECT_EQ(InnerAnd->getOpcode(), Instruction::And);
  88. EXPECT_EQ(InnerAnd->getOperand(0), CBV1);
  89. EXPECT_EQ(InnerAnd->getOperand(1), CBV2);
  90. EXPECT_EQ(InnerIfPred, InnerAnd);
  91. // Test Linearization
  92. EXPECT_EQ(InnerLoopLinSucc, OuterIf);
  93. EXPECT_EQ(OuterIfLinSucc, InnerIf);
  94. EXPECT_EQ(InnerIfLinSucc, InnerLoopLatch);
  95. }
  96. // Test generation of Not and Or during predication.
  97. TEST_F(VPlanPredicatorTest, PredicatorNegOrTest) {
  98. const char *ModuleString =
  99. "@arr = common global [100 x [100 x i32]] zeroinitializer, align 16\n"
  100. "@arr2 = common global [100 x [100 x i32]] zeroinitializer, align 16\n"
  101. "@arr3 = common global [100 x [100 x i32]] zeroinitializer, align 16\n"
  102. "define void @foo() {\n"
  103. "entry:\n"
  104. " br label %for.cond1.preheader\n"
  105. "for.cond1.preheader: \n"
  106. " %indvars.iv42 = phi i64 [ 0, %entry ], [ %indvars.iv.next43, "
  107. "%for.inc22 ]\n"
  108. " br label %for.body3\n"
  109. "for.body3: \n"
  110. " %indvars.iv = phi i64 [ 0, %for.cond1.preheader ], [ "
  111. "%indvars.iv.next, %if.end21 ]\n"
  112. " %arrayidx5 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 "
  113. "x i32]]* @arr, i64 0, i64 %indvars.iv, i64 %indvars.iv42\n"
  114. " %0 = load i32, i32* %arrayidx5, align 4\n"
  115. " %cmp6 = icmp slt i32 %0, 100\n"
  116. " br i1 %cmp6, label %if.then, label %if.end21\n"
  117. "if.then: \n"
  118. " %cmp7 = icmp sgt i32 %0, 10\n"
  119. " br i1 %cmp7, label %if.then8, label %if.else\n"
  120. "if.then8: \n"
  121. " %add = add nsw i32 %0, 10\n"
  122. " %arrayidx12 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 "
  123. "x i32]]* @arr2, i64 0, i64 %indvars.iv, i64 %indvars.iv42\n"
  124. " store i32 %add, i32* %arrayidx12, align 4\n"
  125. " br label %if.end\n"
  126. "if.else: \n"
  127. " %sub = add nsw i32 %0, -10\n"
  128. " %arrayidx16 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 "
  129. "x i32]]* @arr3, i64 0, i64 %indvars.iv, i64 %indvars.iv42\n"
  130. " store i32 %sub, i32* %arrayidx16, align 4\n"
  131. " br label %if.end\n"
  132. "if.end: \n"
  133. " store i32 222, i32* %arrayidx5, align 4\n"
  134. " br label %if.end21\n"
  135. "if.end21: \n"
  136. " %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\n"
  137. " %exitcond = icmp eq i64 %indvars.iv.next, 100\n"
  138. " br i1 %exitcond, label %for.inc22, label %for.body3\n"
  139. "for.inc22: \n"
  140. " %indvars.iv.next43 = add nuw nsw i64 %indvars.iv42, 1\n"
  141. " %exitcond44 = icmp eq i64 %indvars.iv.next43, 100\n"
  142. " br i1 %exitcond44, label %for.end24, label %for.cond1.preheader\n"
  143. "for.end24: \n"
  144. " ret void\n"
  145. "}\n";
  146. Module &M = parseModule(ModuleString);
  147. Function *F = M.getFunction("foo");
  148. BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
  149. auto Plan = buildHCFG(LoopHeader);
  150. VPRegionBlock *TopRegion = cast<VPRegionBlock>(Plan->getEntry());
  151. VPBlockBase *PH = TopRegion->getEntry();
  152. VPBlockBase *H = PH->getSingleSuccessor();
  153. VPBlockBase *OuterIfCmpBlk = H->getSingleSuccessor();
  154. VPBlockBase *InnerIfCmpBlk = OuterIfCmpBlk->getSuccessors()[0];
  155. VPBlockBase *InnerIfTSucc = InnerIfCmpBlk->getSuccessors()[0];
  156. VPBlockBase *InnerIfFSucc = InnerIfCmpBlk->getSuccessors()[1];
  157. VPBlockBase *TSuccSucc = InnerIfTSucc->getSingleSuccessor();
  158. VPBlockBase *FSuccSucc = InnerIfFSucc->getSingleSuccessor();
  159. VPValue *OuterCBV = OuterIfCmpBlk->getCondBit();
  160. VPValue *InnerCBV = InnerIfCmpBlk->getCondBit();
  161. // Apply predication.
  162. VPlanPredicator VPP(*Plan);
  163. VPP.predicate();
  164. VPInstruction *And =
  165. cast<VPInstruction>(InnerIfTSucc->getEntryBasicBlock()->begin());
  166. VPInstruction *Not =
  167. cast<VPInstruction>(InnerIfFSucc->getEntryBasicBlock()->begin());
  168. VPInstruction *NotAnd = cast<VPInstruction>(
  169. &*std::next(InnerIfFSucc->getEntryBasicBlock()->begin(), 1));
  170. VPInstruction *Or =
  171. cast<VPInstruction>(TSuccSucc->getEntryBasicBlock()->begin());
  172. // Test block predicates
  173. EXPECT_NE(nullptr, OuterCBV);
  174. EXPECT_NE(nullptr, InnerCBV);
  175. EXPECT_NE(nullptr, And);
  176. EXPECT_NE(nullptr, Not);
  177. EXPECT_NE(nullptr, NotAnd);
  178. EXPECT_EQ(And->getOpcode(), Instruction::And);
  179. EXPECT_EQ(NotAnd->getOpcode(), Instruction::And);
  180. EXPECT_EQ(Not->getOpcode(), VPInstruction::Not);
  181. EXPECT_EQ(And->getOperand(0), OuterCBV);
  182. EXPECT_EQ(And->getOperand(1), InnerCBV);
  183. EXPECT_EQ(Not->getOperand(0), InnerCBV);
  184. EXPECT_EQ(NotAnd->getOperand(0), OuterCBV);
  185. EXPECT_EQ(NotAnd->getOperand(1), Not);
  186. EXPECT_EQ(InnerIfTSucc->getPredicate(), And);
  187. EXPECT_EQ(InnerIfFSucc->getPredicate(), NotAnd);
  188. EXPECT_EQ(TSuccSucc, FSuccSucc);
  189. EXPECT_EQ(Or->getOpcode(), Instruction::Or);
  190. EXPECT_EQ(TSuccSucc->getPredicate(), Or);
  191. // Test operands of the Or - account for differences in predecessor block
  192. // ordering.
  193. VPInstruction *OrOp0Inst = cast<VPInstruction>(Or->getOperand(0));
  194. VPInstruction *OrOp1Inst = cast<VPInstruction>(Or->getOperand(1));
  195. bool ValidOrOperands = false;
  196. if (((OrOp0Inst == And) && (OrOp1Inst == NotAnd)) ||
  197. ((OrOp0Inst == NotAnd) && (OrOp1Inst == And)))
  198. ValidOrOperands = true;
  199. EXPECT_TRUE(ValidOrOperands);
  200. }
  201. } // namespace
  202. } // namespace llvm