1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
| //===- llvm/unittest/Transforms/Vectorize/VPlanHCFGTest.cpp ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "../lib/Transforms/Vectorize/VPlan.h"
#include "../lib/Transforms/Vectorize/VPlanHCFGTransforms.h"
#include "VPlanTestBase.h"
#include "gtest/gtest.h"
namespace llvm {
namespace {
class VPlanHCFGTest : public VPlanTestBase {};
TEST_F(VPlanHCFGTest, testBuildHCFGInnerLoop) {
const char *ModuleString =
"define void @f(i32* %A, i64 %N) {\n"
"entry:\n"
" br label %for.body\n"
"for.body:\n"
" %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]\n"
" %arr.idx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv\n"
" %l1 = load i32, i32* %arr.idx, align 4\n"
" %res = add i32 %l1, 10\n"
" store i32 %res, i32* %arr.idx, align 4\n"
" %indvars.iv.next = add i64 %indvars.iv, 1\n"
" %exitcond = icmp ne i64 %indvars.iv.next, %N\n"
" br i1 %exitcond, label %for.body, label %for.end\n"
"for.end:\n"
" ret void\n"
"}\n";
Module &M = parseModule(ModuleString);
Function *F = M.getFunction("f");
BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
auto Plan = buildHCFG(LoopHeader);
VPBasicBlock *Entry = Plan->getEntry()->getEntryBasicBlock();
EXPECT_NE(nullptr, Entry->getSingleSuccessor());
EXPECT_EQ(0u, Entry->getNumPredecessors());
EXPECT_EQ(1u, Entry->getNumSuccessors());
EXPECT_EQ(nullptr, Entry->getCondBit());
VPBasicBlock *VecBB = Entry->getSingleSuccessor()->getEntryBasicBlock();
EXPECT_EQ(7u, VecBB->size());
EXPECT_EQ(2u, VecBB->getNumPredecessors());
EXPECT_EQ(2u, VecBB->getNumSuccessors());
auto Iter = VecBB->begin();
VPInstruction *Phi = dyn_cast<VPInstruction>(&*Iter++);
EXPECT_EQ(Instruction::PHI, Phi->getOpcode());
VPInstruction *Idx = dyn_cast<VPInstruction>(&*Iter++);
EXPECT_EQ(Instruction::GetElementPtr, Idx->getOpcode());
EXPECT_EQ(2u, Idx->getNumOperands());
EXPECT_EQ(Phi, Idx->getOperand(1));
VPInstruction *Load = dyn_cast<VPInstruction>(&*Iter++);
EXPECT_EQ(Instruction::Load, Load->getOpcode());
EXPECT_EQ(1u, Load->getNumOperands());
EXPECT_EQ(Idx, Load->getOperand(0));
VPInstruction *Add = dyn_cast<VPInstruction>(&*Iter++);
EXPECT_EQ(Instruction::Add, Add->getOpcode());
EXPECT_EQ(2u, Add->getNumOperands());
EXPECT_EQ(Load, Add->getOperand(0));
VPInstruction *Store = dyn_cast<VPInstruction>(&*Iter++);
EXPECT_EQ(Instruction::Store, Store->getOpcode());
EXPECT_EQ(2u, Store->getNumOperands());
EXPECT_EQ(Add, Store->getOperand(0));
EXPECT_EQ(Idx, Store->getOperand(1));
VPInstruction *IndvarAdd = dyn_cast<VPInstruction>(&*Iter++);
EXPECT_EQ(Instruction::Add, IndvarAdd->getOpcode());
EXPECT_EQ(2u, IndvarAdd->getNumOperands());
EXPECT_EQ(Phi, IndvarAdd->getOperand(0));
VPInstruction *ICmp = dyn_cast<VPInstruction>(&*Iter++);
EXPECT_EQ(Instruction::ICmp, ICmp->getOpcode());
EXPECT_EQ(2u, ICmp->getNumOperands());
EXPECT_EQ(IndvarAdd, ICmp->getOperand(0));
EXPECT_EQ(VecBB->getCondBit(), ICmp);
LoopVectorizationLegality::InductionList Inductions;
SmallPtrSet<Instruction *, 1> DeadInstructions;
VPlanHCFGTransforms::VPInstructionsToVPRecipes(Plan, &Inductions,
DeadInstructions);
}
TEST_F(VPlanHCFGTest, testVPInstructionToVPRecipesInner) {
const char *ModuleString =
"define void @f(i32* %A, i64 %N) {\n"
"entry:\n"
" br label %for.body\n"
"for.body:\n"
" %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]\n"
" %arr.idx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv\n"
" %l1 = load i32, i32* %arr.idx, align 4\n"
" %res = add i32 %l1, 10\n"
" store i32 %res, i32* %arr.idx, align 4\n"
" %indvars.iv.next = add i64 %indvars.iv, 1\n"
" %exitcond = icmp ne i64 %indvars.iv.next, %N\n"
" br i1 %exitcond, label %for.body, label %for.end\n"
"for.end:\n"
" ret void\n"
"}\n";
Module &M = parseModule(ModuleString);
Function *F = M.getFunction("f");
BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
auto Plan = buildHCFG(LoopHeader);
LoopVectorizationLegality::InductionList Inductions;
SmallPtrSet<Instruction *, 1> DeadInstructions;
VPlanHCFGTransforms::VPInstructionsToVPRecipes(Plan, &Inductions,
DeadInstructions);
VPBlockBase *Entry = Plan->getEntry()->getEntryBasicBlock();
EXPECT_NE(nullptr, Entry->getSingleSuccessor());
EXPECT_EQ(0u, Entry->getNumPredecessors());
EXPECT_EQ(1u, Entry->getNumSuccessors());
VPBasicBlock *VecBB = Entry->getSingleSuccessor()->getEntryBasicBlock();
EXPECT_EQ(6u, VecBB->size());
EXPECT_EQ(2u, VecBB->getNumPredecessors());
EXPECT_EQ(2u, VecBB->getNumSuccessors());
auto Iter = VecBB->begin();
auto *Phi = dyn_cast<VPWidenPHIRecipe>(&*Iter++);
EXPECT_NE(nullptr, Phi);
auto *Idx = dyn_cast<VPWidenRecipe>(&*Iter++);
EXPECT_NE(nullptr, Idx);
auto *Load = dyn_cast<VPWidenMemoryInstructionRecipe>(&*Iter++);
EXPECT_NE(nullptr, Load);
auto *Add = dyn_cast<VPWidenRecipe>(&*Iter++);
EXPECT_NE(nullptr, Add);
auto *Store = dyn_cast<VPWidenMemoryInstructionRecipe>(&*Iter++);
EXPECT_NE(nullptr, Store);
auto *LastWiden = dyn_cast<VPWidenRecipe>(&*Iter++);
EXPECT_NE(nullptr, LastWiden);
EXPECT_EQ(VecBB->end(), Iter);
}
} // namespace
} // namespace llvm
|