Revert "[lldb][test] Remove compiler version check and use regex" (#124101)
[llvm-project.git] / llvm / unittests / Transforms / Vectorize / VPlanHCFGTest.cpp
blob19c2483d34ed1752cda9218cd6ab7c04a7470541
1 //===- llvm/unittest/Transforms/Vectorize/VPlanHCFGTest.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 //===----------------------------------------------------------------------===//
9 #include "../lib/Transforms/Vectorize/VPlan.h"
10 #include "../lib/Transforms/Vectorize/VPlanTransforms.h"
11 #include "VPlanTestBase.h"
12 #include "llvm/Analysis/TargetLibraryInfo.h"
13 #include "llvm/TargetParser/Triple.h"
14 #include "gtest/gtest.h"
15 #include <string>
17 namespace llvm {
18 namespace {
20 class VPlanHCFGTest : public VPlanTestIRBase {};
22 TEST_F(VPlanHCFGTest, testBuildHCFGInnerLoop) {
23 const char *ModuleString =
24 "define void @f(ptr %A, i64 %N) {\n"
25 "entry:\n"
26 " br label %for.body\n"
27 "for.body:\n"
28 " %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]\n"
29 " %arr.idx = getelementptr inbounds i32, ptr %A, i64 %indvars.iv\n"
30 " %l1 = load i32, ptr %arr.idx, align 4\n"
31 " %res = add i32 %l1, 10\n"
32 " store i32 %res, ptr %arr.idx, align 4\n"
33 " %indvars.iv.next = add i64 %indvars.iv, 1\n"
34 " %exitcond = icmp ne i64 %indvars.iv.next, %N\n"
35 " br i1 %exitcond, label %for.body, label %for.end\n"
36 "for.end:\n"
37 " ret void\n"
38 "}\n";
40 Module &M = parseModule(ModuleString);
42 Function *F = M.getFunction("f");
43 BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
44 auto Plan = buildHCFG(LoopHeader);
46 VPBasicBlock *Entry = Plan->getEntry()->getEntryBasicBlock();
47 EXPECT_NE(nullptr, Entry->getSingleSuccessor());
48 EXPECT_EQ(0u, Entry->getNumPredecessors());
49 EXPECT_EQ(1u, Entry->getNumSuccessors());
51 // Check that the region following the preheader is a single basic-block
52 // region (loop).
53 VPBasicBlock *VecBB = Plan->getVectorLoopRegion()->getEntryBasicBlock();
54 EXPECT_EQ(8u, VecBB->size());
55 EXPECT_EQ(0u, VecBB->getNumPredecessors());
56 EXPECT_EQ(0u, VecBB->getNumSuccessors());
57 EXPECT_EQ(VecBB->getParent()->getEntryBasicBlock(), VecBB);
58 EXPECT_EQ(VecBB->getParent()->getExitingBasicBlock(), VecBB);
59 EXPECT_EQ(&*Plan, VecBB->getPlan());
61 auto Iter = VecBB->begin();
62 VPWidenPHIRecipe *Phi = dyn_cast<VPWidenPHIRecipe>(&*Iter++);
63 EXPECT_NE(nullptr, Phi);
65 VPInstruction *Idx = dyn_cast<VPInstruction>(&*Iter++);
66 EXPECT_EQ(Instruction::GetElementPtr, Idx->getOpcode());
67 EXPECT_EQ(2u, Idx->getNumOperands());
68 EXPECT_EQ(Phi, Idx->getOperand(1));
70 VPInstruction *Load = dyn_cast<VPInstruction>(&*Iter++);
71 EXPECT_EQ(Instruction::Load, Load->getOpcode());
72 EXPECT_EQ(1u, Load->getNumOperands());
73 EXPECT_EQ(Idx, Load->getOperand(0));
75 VPInstruction *Add = dyn_cast<VPInstruction>(&*Iter++);
76 EXPECT_EQ(Instruction::Add, Add->getOpcode());
77 EXPECT_EQ(2u, Add->getNumOperands());
78 EXPECT_EQ(Load, Add->getOperand(0));
80 VPInstruction *Store = dyn_cast<VPInstruction>(&*Iter++);
81 EXPECT_EQ(Instruction::Store, Store->getOpcode());
82 EXPECT_EQ(2u, Store->getNumOperands());
83 EXPECT_EQ(Add, Store->getOperand(0));
84 EXPECT_EQ(Idx, Store->getOperand(1));
86 VPInstruction *IndvarAdd = dyn_cast<VPInstruction>(&*Iter++);
87 EXPECT_EQ(Instruction::Add, IndvarAdd->getOpcode());
88 EXPECT_EQ(2u, IndvarAdd->getNumOperands());
89 EXPECT_EQ(Phi, IndvarAdd->getOperand(0));
91 VPInstruction *ICmp = dyn_cast<VPInstruction>(&*Iter++);
92 EXPECT_EQ(Instruction::ICmp, ICmp->getOpcode());
93 EXPECT_EQ(2u, ICmp->getNumOperands());
94 EXPECT_EQ(IndvarAdd, ICmp->getOperand(0));
96 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
97 // Add an external value to check we do not print the list of external values,
98 // as this is not required with the new printing.
99 Plan->getOrAddLiveIn(&*F->arg_begin());
100 std::string FullDump;
101 raw_string_ostream OS(FullDump);
102 Plan->printDOT(OS);
103 const char *ExpectedStr = R"(digraph VPlan {
104 graph [labelloc=t, fontsize=30; label="Vectorization Plan\n for UF\>=1\nLive-in vp\<%0\> = vector-trip-count\nLive-in ir\<%N\> = original trip-count\n"]
105 node [shape=rect, fontname=Courier, fontsize=30]
106 edge [fontname=Courier, fontsize=30]
107 compound=true
108 N0 [label =
109 "ir-bb\<entry\>:\l" +
110 "Successor(s): vector.ph\l"
112 N0 -> N1 [ label=""]
113 N1 [label =
114 "vector.ph:\l" +
115 "Successor(s): vector loop\l"
117 N1 -> N2 [ label="" lhead=cluster_N3]
118 subgraph cluster_N3 {
119 fontname=Courier
120 label="\<x1\> vector loop"
121 N2 [label =
122 "vector.body:\l" +
123 " WIDEN-PHI ir\<%indvars.iv\> = phi ir\<0\>, ir\<%indvars.iv.next\>\l" +
124 " EMIT ir\<%arr.idx\> = getelementptr ir\<%A\>, ir\<%indvars.iv\>\l" +
125 " EMIT ir\<%l1\> = load ir\<%arr.idx\>\l" +
126 " EMIT ir\<%res\> = add ir\<%l1\>, ir\<10\>\l" +
127 " EMIT store ir\<%res\>, ir\<%arr.idx\>\l" +
128 " EMIT ir\<%indvars.iv.next\> = add ir\<%indvars.iv\>, ir\<1\>\l" +
129 " EMIT ir\<%exitcond\> = icmp ir\<%indvars.iv.next\>, ir\<%N\>\l" +
130 " EMIT branch-on-cond ir\<%exitcond\>\l" +
131 "No successors\l"
134 N2 -> N4 [ label="" ltail=cluster_N3]
135 N4 [label =
136 "middle.block:\l" +
137 " EMIT vp\<%cmp.n\> = icmp eq ir\<%N\>, vp\<%0\>\l" +
138 " EMIT branch-on-cond vp\<%cmp.n\>\l" +
139 "Successor(s): ir-bb\<for.end\>, scalar.ph\l"
141 N4 -> N5 [ label="T"]
142 N4 -> N6 [ label="F"]
143 N5 [label =
144 "ir-bb\<for.end\>:\l" +
145 "No successors\l"
147 N6 [label =
148 "scalar.ph:\l" +
149 "Successor(s): ir-bb\<for.body\>\l"
151 N6 -> N7 [ label=""]
152 N7 [label =
153 "ir-bb\<for.body\>:\l" +
154 " IR %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]\l" +
155 " IR %arr.idx = getelementptr inbounds i32, ptr %A, i64 %indvars.iv\l" +
156 " IR %l1 = load i32, ptr %arr.idx, align 4\l" +
157 " IR %res = add i32 %l1, 10\l" +
158 " IR store i32 %res, ptr %arr.idx, align 4\l" +
159 " IR %indvars.iv.next = add i64 %indvars.iv, 1\l" +
160 " IR %exitcond = icmp ne i64 %indvars.iv.next, %N\l" +
161 "No successors\l"
165 EXPECT_EQ(ExpectedStr, FullDump);
166 #endif
167 TargetLibraryInfoImpl TLII(Triple(M.getTargetTriple()));
168 TargetLibraryInfo TLI(TLII);
169 VPlanTransforms::VPInstructionsToVPRecipes(
170 Plan, [](PHINode *P) { return nullptr; }, *SE, TLI);
173 TEST_F(VPlanHCFGTest, testVPInstructionToVPRecipesInner) {
174 const char *ModuleString =
175 "define void @f(ptr %A, i64 %N) {\n"
176 "entry:\n"
177 " br label %for.body\n"
178 "for.body:\n"
179 " %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]\n"
180 " %arr.idx = getelementptr inbounds i32, ptr %A, i64 %indvars.iv\n"
181 " %l1 = load i32, ptr %arr.idx, align 4\n"
182 " %res = add i32 %l1, 10\n"
183 " store i32 %res, ptr %arr.idx, align 4\n"
184 " %indvars.iv.next = add i64 %indvars.iv, 1\n"
185 " %exitcond = icmp ne i64 %indvars.iv.next, %N\n"
186 " br i1 %exitcond, label %for.body, label %for.end\n"
187 "for.end:\n"
188 " ret void\n"
189 "}\n";
191 Module &M = parseModule(ModuleString);
193 Function *F = M.getFunction("f");
194 BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
195 auto Plan = buildHCFG(LoopHeader);
197 TargetLibraryInfoImpl TLII(Triple(M.getTargetTriple()));
198 TargetLibraryInfo TLI(TLII);
199 VPlanTransforms::VPInstructionsToVPRecipes(
200 Plan, [](PHINode *P) { return nullptr; }, *SE, TLI);
202 VPBlockBase *Entry = Plan->getEntry()->getEntryBasicBlock();
203 EXPECT_NE(nullptr, Entry->getSingleSuccessor());
204 EXPECT_EQ(0u, Entry->getNumPredecessors());
205 EXPECT_EQ(1u, Entry->getNumSuccessors());
207 // Check that the region following the preheader is a single basic-block
208 // region (loop).
209 VPBasicBlock *VecBB = Plan->getVectorLoopRegion()->getEntryBasicBlock();
210 EXPECT_EQ(8u, VecBB->size());
211 EXPECT_EQ(0u, VecBB->getNumPredecessors());
212 EXPECT_EQ(0u, VecBB->getNumSuccessors());
213 EXPECT_EQ(VecBB->getParent()->getEntryBasicBlock(), VecBB);
214 EXPECT_EQ(VecBB->getParent()->getExitingBasicBlock(), VecBB);
216 auto Iter = VecBB->begin();
217 EXPECT_NE(nullptr, dyn_cast<VPWidenPHIRecipe>(&*Iter++));
218 EXPECT_NE(nullptr, dyn_cast<VPWidenGEPRecipe>(&*Iter++));
219 EXPECT_NE(nullptr, dyn_cast<VPWidenMemoryRecipe>(&*Iter++));
220 EXPECT_NE(nullptr, dyn_cast<VPWidenRecipe>(&*Iter++));
221 EXPECT_NE(nullptr, dyn_cast<VPWidenMemoryRecipe>(&*Iter++));
222 EXPECT_NE(nullptr, dyn_cast<VPWidenRecipe>(&*Iter++));
223 EXPECT_NE(nullptr, dyn_cast<VPWidenRecipe>(&*Iter++));
224 EXPECT_NE(nullptr, dyn_cast<VPInstruction>(&*Iter++));
225 EXPECT_EQ(VecBB->end(), Iter);
228 } // namespace
229 } // namespace llvm