Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / unittests / Transforms / Vectorize / VPlanHCFGTest.cpp
blob5381fa461e9c992bfb9a216ca61bd7ea26b4a45b
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 VPlanTestBase {};
22 TEST_F(VPlanHCFGTest, testBuildHCFGInnerLoop) {
23 const char *ModuleString =
24 "define void @f(i32* %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, i32* %A, i64 %indvars.iv\n"
30 " %l1 = load i32, i32* %arr.idx, align 4\n"
31 " %res = add i32 %l1, 10\n"
32 " store i32 %res, i32* %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 = Entry->getSingleSuccessor()->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->getVPValueOrAddLiveIn(&*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"]
105 node [shape=rect, fontname=Courier, fontsize=30]
106 edge [fontname=Courier, fontsize=30]
107 compound=true
108 N0 [label =
109 "ph:\l" +
110 " EMIT vp\<%1\> = EXPAND SCEV (-1 + %N)\l" +
111 "No successors\l"
113 N1 [label =
114 "vector.ph:\l" +
115 "Successor(s): for.body\l"
117 N1 -> N2 [ label="" lhead=cluster_N3]
118 subgraph cluster_N3 {
119 fontname=Courier
120 label="\<x1\> for.body"
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 "for.end:\l" +
137 "No successors\l"
141 EXPECT_EQ(ExpectedStr, FullDump);
142 #endif
143 TargetLibraryInfoImpl TLII(Triple(M.getTargetTriple()));
144 TargetLibraryInfo TLI(TLII);
145 VPlanTransforms::VPInstructionsToVPRecipes(
146 Plan, [](PHINode *P) { return nullptr; }, *SE, TLI);
149 TEST_F(VPlanHCFGTest, testVPInstructionToVPRecipesInner) {
150 const char *ModuleString =
151 "define void @f(i32* %A, i64 %N) {\n"
152 "entry:\n"
153 " br label %for.body\n"
154 "for.body:\n"
155 " %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]\n"
156 " %arr.idx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv\n"
157 " %l1 = load i32, i32* %arr.idx, align 4\n"
158 " %res = add i32 %l1, 10\n"
159 " store i32 %res, i32* %arr.idx, align 4\n"
160 " %indvars.iv.next = add i64 %indvars.iv, 1\n"
161 " %exitcond = icmp ne i64 %indvars.iv.next, %N\n"
162 " br i1 %exitcond, label %for.body, label %for.end\n"
163 "for.end:\n"
164 " ret void\n"
165 "}\n";
167 Module &M = parseModule(ModuleString);
169 Function *F = M.getFunction("f");
170 BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
171 auto Plan = buildHCFG(LoopHeader);
173 TargetLibraryInfoImpl TLII(Triple(M.getTargetTriple()));
174 TargetLibraryInfo TLI(TLII);
175 VPlanTransforms::VPInstructionsToVPRecipes(
176 Plan, [](PHINode *P) { return nullptr; }, *SE, TLI);
178 VPBlockBase *Entry = Plan->getEntry()->getEntryBasicBlock();
179 EXPECT_NE(nullptr, Entry->getSingleSuccessor());
180 EXPECT_EQ(0u, Entry->getNumPredecessors());
181 EXPECT_EQ(1u, Entry->getNumSuccessors());
183 // Check that the region following the preheader is a single basic-block
184 // region (loop).
185 VPBasicBlock *VecBB = Entry->getSingleSuccessor()->getEntryBasicBlock();
186 EXPECT_EQ(8u, VecBB->size());
187 EXPECT_EQ(0u, VecBB->getNumPredecessors());
188 EXPECT_EQ(0u, VecBB->getNumSuccessors());
189 EXPECT_EQ(VecBB->getParent()->getEntryBasicBlock(), VecBB);
190 EXPECT_EQ(VecBB->getParent()->getExitingBasicBlock(), VecBB);
192 auto Iter = VecBB->begin();
193 EXPECT_NE(nullptr, dyn_cast<VPWidenPHIRecipe>(&*Iter++));
194 EXPECT_NE(nullptr, dyn_cast<VPWidenGEPRecipe>(&*Iter++));
195 EXPECT_NE(nullptr, dyn_cast<VPWidenMemoryInstructionRecipe>(&*Iter++));
196 EXPECT_NE(nullptr, dyn_cast<VPWidenRecipe>(&*Iter++));
197 EXPECT_NE(nullptr, dyn_cast<VPWidenMemoryInstructionRecipe>(&*Iter++));
198 EXPECT_NE(nullptr, dyn_cast<VPWidenRecipe>(&*Iter++));
199 EXPECT_NE(nullptr, dyn_cast<VPWidenRecipe>(&*Iter++));
200 EXPECT_NE(nullptr, dyn_cast<VPInstruction>(&*Iter++));
201 EXPECT_EQ(VecBB->end(), Iter);
204 } // namespace
205 } // namespace llvm