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 //===----------------------------------------------------------------------===//
8
9 #include "../lib/Transforms/Vectorize/VPlan.h"
10 #include "../lib/Transforms/Vectorize/VPlanTransforms.h"
11 #include "VPlanTestBase.h"
12 #include "gtest/gtest.h"
13 #include <string>
14
15 namespace llvm {
16 namespace {
17
18 class VPlanHCFGTest : public VPlanTestBase {};
19
TEST_F(VPlanHCFGTest,testBuildHCFGInnerLoop)20 TEST_F(VPlanHCFGTest, testBuildHCFGInnerLoop) {
21 const char *ModuleString =
22 "define void @f(i32* %A, i64 %N) {\n"
23 "entry:\n"
24 " br label %for.body\n"
25 "for.body:\n"
26 " %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]\n"
27 " %arr.idx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv\n"
28 " %l1 = load i32, i32* %arr.idx, align 4\n"
29 " %res = add i32 %l1, 10\n"
30 " store i32 %res, i32* %arr.idx, align 4\n"
31 " %indvars.iv.next = add i64 %indvars.iv, 1\n"
32 " %exitcond = icmp ne i64 %indvars.iv.next, %N\n"
33 " br i1 %exitcond, label %for.body, label %for.end\n"
34 "for.end:\n"
35 " ret void\n"
36 "}\n";
37
38 Module &M = parseModule(ModuleString);
39
40 Function *F = M.getFunction("f");
41 BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
42 auto Plan = buildHCFG(LoopHeader);
43
44 VPBasicBlock *Entry = Plan->getEntry()->getEntryBasicBlock();
45 EXPECT_NE(nullptr, Entry->getSingleSuccessor());
46 EXPECT_EQ(0u, Entry->getNumPredecessors());
47 EXPECT_EQ(1u, Entry->getNumSuccessors());
48 EXPECT_EQ(nullptr, Entry->getCondBit());
49
50 VPBasicBlock *VecBB = Entry->getSingleSuccessor()->getEntryBasicBlock();
51 EXPECT_EQ(7u, VecBB->size());
52 EXPECT_EQ(2u, VecBB->getNumPredecessors());
53 EXPECT_EQ(2u, VecBB->getNumSuccessors());
54 EXPECT_EQ(&*Plan, VecBB->getPlan());
55
56 auto Iter = VecBB->begin();
57 VPInstruction *Phi = dyn_cast<VPInstruction>(&*Iter++);
58 EXPECT_EQ(Instruction::PHI, Phi->getOpcode());
59
60 VPInstruction *Idx = dyn_cast<VPInstruction>(&*Iter++);
61 EXPECT_EQ(Instruction::GetElementPtr, Idx->getOpcode());
62 EXPECT_EQ(2u, Idx->getNumOperands());
63 EXPECT_EQ(Phi, Idx->getOperand(1));
64
65 VPInstruction *Load = dyn_cast<VPInstruction>(&*Iter++);
66 EXPECT_EQ(Instruction::Load, Load->getOpcode());
67 EXPECT_EQ(1u, Load->getNumOperands());
68 EXPECT_EQ(Idx, Load->getOperand(0));
69
70 VPInstruction *Add = dyn_cast<VPInstruction>(&*Iter++);
71 EXPECT_EQ(Instruction::Add, Add->getOpcode());
72 EXPECT_EQ(2u, Add->getNumOperands());
73 EXPECT_EQ(Load, Add->getOperand(0));
74
75 VPInstruction *Store = dyn_cast<VPInstruction>(&*Iter++);
76 EXPECT_EQ(Instruction::Store, Store->getOpcode());
77 EXPECT_EQ(2u, Store->getNumOperands());
78 EXPECT_EQ(Add, Store->getOperand(0));
79 EXPECT_EQ(Idx, Store->getOperand(1));
80
81 VPInstruction *IndvarAdd = dyn_cast<VPInstruction>(&*Iter++);
82 EXPECT_EQ(Instruction::Add, IndvarAdd->getOpcode());
83 EXPECT_EQ(2u, IndvarAdd->getNumOperands());
84 EXPECT_EQ(Phi, IndvarAdd->getOperand(0));
85
86 VPInstruction *ICmp = dyn_cast<VPInstruction>(&*Iter++);
87 EXPECT_EQ(Instruction::ICmp, ICmp->getOpcode());
88 EXPECT_EQ(2u, ICmp->getNumOperands());
89 EXPECT_EQ(IndvarAdd, ICmp->getOperand(0));
90 EXPECT_EQ(VecBB->getCondBit(), ICmp);
91
92 // Add an external value to check we do not print the list of external values,
93 // as this is not required with the new printing.
94 Plan->addVPValue(&*F->arg_begin());
95 std::string FullDump;
96 raw_string_ostream(FullDump) << *Plan;
97 const char *ExpectedStr = R"(digraph VPlan {
98 graph [labelloc=t, fontsize=30; label="Vectorization Plan"]
99 node [shape=rect, fontname=Courier, fontsize=30]
100 edge [fontname=Courier, fontsize=30]
101 compound=true
102 subgraph cluster_N0 {
103 fontname=Courier
104 label="\<x1\> TopRegion"
105 N1 [label =
106 "entry:\n"
107 ]
108 N1 -> N2 [ label=""]
109 N2 [label =
110 "for.body:\n" +
111 "EMIT ir<%indvars.iv> = phi ir<0> ir<%indvars.iv.next>\l" +
112 "EMIT ir<%arr.idx> = getelementptr ir<%A> ir<%indvars.iv>\l" +
113 "EMIT ir<%l1> = load ir<%arr.idx>\l" +
114 "EMIT ir<%res> = add ir<%l1> ir<10>\l" +
115 "EMIT store ir<%res> ir<%arr.idx>\l" +
116 "EMIT ir<%indvars.iv.next> = add ir<%indvars.iv> ir<1>\l" +
117 "EMIT ir<%exitcond> = icmp ir<%indvars.iv.next> ir<%N>\l" +
118 "CondBit: ir<%exitcond> (for.body)\l"
119 ]
120 N2 -> N2 [ label="T"]
121 N2 -> N3 [ label="F"]
122 N3 [label =
123 "for.end:\n" +
124 "EMIT ret\l"
125 ]
126 }
127 }
128 )";
129 EXPECT_EQ(ExpectedStr, FullDump);
130
131 LoopVectorizationLegality::InductionList Inductions;
132 SmallPtrSet<Instruction *, 1> DeadInstructions;
133 VPlanTransforms::VPInstructionsToVPRecipes(LI->getLoopFor(LoopHeader), Plan,
134 Inductions, DeadInstructions);
135 }
136
TEST_F(VPlanHCFGTest,testVPInstructionToVPRecipesInner)137 TEST_F(VPlanHCFGTest, testVPInstructionToVPRecipesInner) {
138 const char *ModuleString =
139 "define void @f(i32* %A, i64 %N) {\n"
140 "entry:\n"
141 " br label %for.body\n"
142 "for.body:\n"
143 " %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]\n"
144 " %arr.idx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv\n"
145 " %l1 = load i32, i32* %arr.idx, align 4\n"
146 " %res = add i32 %l1, 10\n"
147 " store i32 %res, i32* %arr.idx, align 4\n"
148 " %indvars.iv.next = add i64 %indvars.iv, 1\n"
149 " %exitcond = icmp ne i64 %indvars.iv.next, %N\n"
150 " br i1 %exitcond, label %for.body, label %for.end\n"
151 "for.end:\n"
152 " ret void\n"
153 "}\n";
154
155 Module &M = parseModule(ModuleString);
156
157 Function *F = M.getFunction("f");
158 BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
159 auto Plan = buildHCFG(LoopHeader);
160
161 LoopVectorizationLegality::InductionList Inductions;
162 SmallPtrSet<Instruction *, 1> DeadInstructions;
163 VPlanTransforms::VPInstructionsToVPRecipes(LI->getLoopFor(LoopHeader), Plan,
164 Inductions, DeadInstructions);
165
166 VPBlockBase *Entry = Plan->getEntry()->getEntryBasicBlock();
167 EXPECT_NE(nullptr, Entry->getSingleSuccessor());
168 EXPECT_EQ(0u, Entry->getNumPredecessors());
169 EXPECT_EQ(1u, Entry->getNumSuccessors());
170
171 VPBasicBlock *VecBB = Entry->getSingleSuccessor()->getEntryBasicBlock();
172 EXPECT_EQ(7u, VecBB->size());
173 EXPECT_EQ(2u, VecBB->getNumPredecessors());
174 EXPECT_EQ(2u, VecBB->getNumSuccessors());
175
176 auto Iter = VecBB->begin();
177 EXPECT_NE(nullptr, dyn_cast<VPWidenPHIRecipe>(&*Iter++));
178 EXPECT_NE(nullptr, dyn_cast<VPWidenGEPRecipe>(&*Iter++));
179 EXPECT_NE(nullptr, dyn_cast<VPWidenMemoryInstructionRecipe>(&*Iter++));
180 EXPECT_NE(nullptr, dyn_cast<VPWidenRecipe>(&*Iter++));
181 EXPECT_NE(nullptr, dyn_cast<VPWidenMemoryInstructionRecipe>(&*Iter++));
182 EXPECT_NE(nullptr, dyn_cast<VPWidenRecipe>(&*Iter++));
183 EXPECT_NE(nullptr, dyn_cast<VPWidenRecipe>(&*Iter++));
184 EXPECT_EQ(VecBB->end(), Iter);
185 }
186
187 } // namespace
188 } // namespace llvm
189