PhiValuesTest.cpp
7.21 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//===- PhiValuesTest.cpp - PhiValues unit tests ---------------------------===//
//
// 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 "llvm/Analysis/PhiValues.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "gtest/gtest.h"
using namespace llvm;
TEST(PhiValuesTest, SimplePhi) {
LLVMContext C;
Module M("PhiValuesTest", C);
Type *VoidTy = Type::getVoidTy(C);
Type *I1Ty = Type::getInt1Ty(C);
Type *I32Ty = Type::getInt32Ty(C);
Type *I32PtrTy = Type::getInt32PtrTy(C);
// Create a function with phis that do not have other phis as incoming values
Function *F = Function::Create(FunctionType::get(VoidTy, false),
Function::ExternalLinkage, "f", M);
BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
BasicBlock *If = BasicBlock::Create(C, "if", F);
BasicBlock *Else = BasicBlock::Create(C, "else", F);
BasicBlock *Then = BasicBlock::Create(C, "then", F);
BranchInst::Create(If, Else, UndefValue::get(I1Ty), Entry);
BranchInst::Create(Then, If);
BranchInst::Create(Then, Else);
Value *Val1 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val1", Entry);
Value *Val2 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val2", Entry);
Value *Val3 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val3", Entry);
Value *Val4 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val4", Entry);
PHINode *Phi1 = PHINode::Create(I32Ty, 2, "phi1", Then);
Phi1->addIncoming(Val1, If);
Phi1->addIncoming(Val2, Else);
PHINode *Phi2 = PHINode::Create(I32Ty, 2, "phi2", Then);
Phi2->addIncoming(Val1, If);
Phi2->addIncoming(Val3, Else);
PhiValues PV(*F);
PhiValues::ValueSet Vals;
// Check that simple usage works
Vals = PV.getValuesForPhi(Phi1);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val1));
EXPECT_TRUE(Vals.count(Val2));
Vals = PV.getValuesForPhi(Phi2);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val1));
EXPECT_TRUE(Vals.count(Val3));
// Check that values are updated when one value is replaced with another
Val1->replaceAllUsesWith(Val4);
PV.invalidateValue(Val1);
Vals = PV.getValuesForPhi(Phi1);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val4));
EXPECT_TRUE(Vals.count(Val2));
Vals = PV.getValuesForPhi(Phi2);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val4));
EXPECT_TRUE(Vals.count(Val3));
// Check that setting in incoming value directly updates the values
Phi1->setIncomingValue(0, Val1);
PV.invalidateValue(Phi1);
Vals = PV.getValuesForPhi(Phi1);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val1));
EXPECT_TRUE(Vals.count(Val2));
}
TEST(PhiValuesTest, DependentPhi) {
LLVMContext C;
Module M("PhiValuesTest", C);
Type *VoidTy = Type::getVoidTy(C);
Type *I1Ty = Type::getInt1Ty(C);
Type *I32Ty = Type::getInt32Ty(C);
Type *I32PtrTy = Type::getInt32PtrTy(C);
// Create a function with a phi that has another phi as an incoming value
Function *F = Function::Create(FunctionType::get(VoidTy, false),
Function::ExternalLinkage, "f", M);
BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
BasicBlock *If1 = BasicBlock::Create(C, "if1", F);
BasicBlock *Else1 = BasicBlock::Create(C, "else1", F);
BasicBlock *Then = BasicBlock::Create(C, "then", F);
BasicBlock *If2 = BasicBlock::Create(C, "if2", F);
BasicBlock *Else2 = BasicBlock::Create(C, "else2", F);
BasicBlock *End = BasicBlock::Create(C, "then", F);
BranchInst::Create(If1, Else1, UndefValue::get(I1Ty), Entry);
BranchInst::Create(Then, If1);
BranchInst::Create(Then, Else1);
BranchInst::Create(If2, Else2, UndefValue::get(I1Ty), Then);
BranchInst::Create(End, If2);
BranchInst::Create(End, Else2);
Value *Val1 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val1", Entry);
Value *Val2 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val2", Entry);
Value *Val3 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val3", Entry);
Value *Val4 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val4", Entry);
PHINode *Phi1 = PHINode::Create(I32Ty, 2, "phi1", Then);
Phi1->addIncoming(Val1, If1);
Phi1->addIncoming(Val2, Else1);
PHINode *Phi2 = PHINode::Create(I32Ty, 2, "phi2", Then);
Phi2->addIncoming(Val2, If1);
Phi2->addIncoming(Val3, Else1);
PHINode *Phi3 = PHINode::Create(I32Ty, 2, "phi3", End);
Phi3->addIncoming(Phi1, If2);
Phi3->addIncoming(Val3, Else2);
PhiValues PV(*F);
PhiValues::ValueSet Vals;
// Check that simple usage works
Vals = PV.getValuesForPhi(Phi1);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val1));
EXPECT_TRUE(Vals.count(Val2));
Vals = PV.getValuesForPhi(Phi2);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val2));
EXPECT_TRUE(Vals.count(Val3));
Vals = PV.getValuesForPhi(Phi3);
EXPECT_EQ(Vals.size(), 3u);
EXPECT_TRUE(Vals.count(Val1));
EXPECT_TRUE(Vals.count(Val2));
EXPECT_TRUE(Vals.count(Val3));
// Check that changing an incoming value in the dependent phi changes the depending phi
Phi1->setIncomingValue(0, Val4);
PV.invalidateValue(Phi1);
Vals = PV.getValuesForPhi(Phi1);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val4));
EXPECT_TRUE(Vals.count(Val2));
Vals = PV.getValuesForPhi(Phi2);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val2));
EXPECT_TRUE(Vals.count(Val3));
Vals = PV.getValuesForPhi(Phi3);
EXPECT_EQ(Vals.size(), 3u);
EXPECT_TRUE(Vals.count(Val4));
EXPECT_TRUE(Vals.count(Val2));
EXPECT_TRUE(Vals.count(Val3));
// Check that replacing an incoming phi with a value works
Phi3->setIncomingValue(0, Val1);
PV.invalidateValue(Phi3);
Vals = PV.getValuesForPhi(Phi1);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val4));
EXPECT_TRUE(Vals.count(Val2));
Vals = PV.getValuesForPhi(Phi2);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val2));
EXPECT_TRUE(Vals.count(Val3));
Vals = PV.getValuesForPhi(Phi3);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val1));
EXPECT_TRUE(Vals.count(Val3));
// Check that adding a phi as an incoming value works
Phi3->setIncomingValue(1, Phi2);
PV.invalidateValue(Phi3);
Vals = PV.getValuesForPhi(Phi1);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val4));
EXPECT_TRUE(Vals.count(Val2));
Vals = PV.getValuesForPhi(Phi2);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val2));
EXPECT_TRUE(Vals.count(Val3));
Vals = PV.getValuesForPhi(Phi3);
EXPECT_EQ(Vals.size(), 3u);
EXPECT_TRUE(Vals.count(Val1));
EXPECT_TRUE(Vals.count(Val2));
EXPECT_TRUE(Vals.count(Val3));
// Check that replacing an incoming phi then deleting it works
Phi3->setIncomingValue(1, Val2);
PV.invalidateValue(Phi2);
Phi2->eraseFromParent();
PV.invalidateValue(Phi3);
Vals = PV.getValuesForPhi(Phi1);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val4));
EXPECT_TRUE(Vals.count(Val2));
Vals = PV.getValuesForPhi(Phi3);
EXPECT_EQ(Vals.size(), 2u);
EXPECT_TRUE(Vals.count(Val1));
EXPECT_TRUE(Vals.count(Val2));
}