llvm.org GIT mirror llvm / 90cac21
ValueMapper: Use API from r266503 in unit tests, NFC I'm not in a hurry to migrate all the users, but the unit tests at least should use the new API. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266505 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 4 years ago
1 changed file(s) with 70 addition(s) and 70 deletion(s). Raw diff Collapse all Expand all
1818
1919 namespace {
2020
21 TEST(ValueMapperTest, MapMetadata) {
21 TEST(ValueMapperTest, mapMDNode) {
2222 LLVMContext Context;
2323 auto *U = MDTuple::get(Context, None);
2424
2525 // The node should be unchanged.
2626 ValueToValueMapTy VM;
27 EXPECT_EQ(U, MapMetadata(U, VM, RF_None));
28 }
29
30 TEST(ValueMapperTest, MapMetadataCycle) {
27 EXPECT_EQ(U, ValueMapper(VM).mapMDNode(*U));
28 }
29
30 TEST(ValueMapperTest, mapMDNodeCycle) {
3131 LLVMContext Context;
3232 MDNode *U0;
3333 MDNode *U1;
5151 // Cycles shouldn't be duplicated.
5252 {
5353 ValueToValueMapTy VM;
54 EXPECT_EQ(U0, MapMetadata(U0, VM, RF_None));
55 EXPECT_EQ(U1, MapMetadata(U1, VM, RF_None));
54 EXPECT_EQ(U0, ValueMapper(VM).mapMDNode(*U0));
55 EXPECT_EQ(U1, ValueMapper(VM).mapMDNode(*U1));
5656 }
5757
5858 // Check the other order.
5959 {
6060 ValueToValueMapTy VM;
61 EXPECT_EQ(U1, MapMetadata(U1, VM, RF_None));
62 EXPECT_EQ(U0, MapMetadata(U0, VM, RF_None));
63 }
64 }
65
66 TEST(ValueMapperTest, MapMetadataDuplicatedCycle) {
61 EXPECT_EQ(U1, ValueMapper(VM).mapMDNode(*U1));
62 EXPECT_EQ(U0, ValueMapper(VM).mapMDNode(*U0));
63 }
64 }
65
66 TEST(ValueMapperTest, mapMDNodeDuplicatedCycle) {
6767 LLVMContext Context;
6868 auto *PtrTy = Type::getInt8Ty(Context)->getPointerTo();
6969 std::unique_ptr G0 = llvm::make_unique(
9393 // have new nodes that reference G1 (instead of G0).
9494 ValueToValueMapTy VM;
9595 VM[G0.get()] = G1.get();
96 MDNode *MappedN0 = MapMetadata(N0, VM);
97 MDNode *MappedN1 = MapMetadata(N1, VM);
96 MDNode *MappedN0 = ValueMapper(VM).mapMDNode(*N0);
97 MDNode *MappedN1 = ValueMapper(VM).mapMDNode(*N1);
9898 EXPECT_NE(N0, MappedN0);
9999 EXPECT_NE(N1, MappedN1);
100100 EXPECT_EQ(ConstantAsMetadata::get(G1.get()), MappedN1->getOperand(1));
104104 EXPECT_TRUE(MappedN1->isResolved());
105105 }
106106
107 TEST(ValueMapperTest, MapMetadataUnresolved) {
107 TEST(ValueMapperTest, mapMDNodeUnresolved) {
108108 LLVMContext Context;
109109 TempMDTuple T = MDTuple::getTemporary(Context, None);
110110
111111 ValueToValueMapTy VM;
112 EXPECT_EQ(T.get(), MapMetadata(T.get(), VM, RF_NoModuleLevelChanges));
113 }
114
115 TEST(ValueMapperTest, MapMetadataDistinct) {
112 EXPECT_EQ(T.get(), ValueMapper(VM, RF_NoModuleLevelChanges).mapMDNode(*T));
113 }
114
115 TEST(ValueMapperTest, mapMDNodeDistinct) {
116116 LLVMContext Context;
117117 auto *D = MDTuple::getDistinct(Context, None);
118118
119119 {
120120 // The node should be cloned.
121121 ValueToValueMapTy VM;
122 EXPECT_NE(D, MapMetadata(D, VM, RF_None));
122 EXPECT_NE(D, ValueMapper(VM).mapMDNode(*D));
123123 }
124124 {
125125 // The node should be moved.
126126 ValueToValueMapTy VM;
127 EXPECT_EQ(D, MapMetadata(D, VM, RF_MoveDistinctMDs));
128 }
129 }
130
131 TEST(ValueMapperTest, MapMetadataDistinctOperands) {
127 EXPECT_EQ(D, ValueMapper(VM, RF_MoveDistinctMDs).mapMDNode(*D));
128 }
129 }
130
131 TEST(ValueMapperTest, mapMDNodeDistinctOperands) {
132132 LLVMContext Context;
133133 Metadata *Old = MDTuple::getDistinct(Context, None);
134134 auto *D = MDTuple::getDistinct(Context, Old);
139139 VM.MD()[Old].reset(New);
140140
141141 // Make sure operands are updated.
142 EXPECT_EQ(D, MapMetadata(D, VM, RF_MoveDistinctMDs));
142 EXPECT_EQ(D, ValueMapper(VM, RF_MoveDistinctMDs).mapMDNode(*D));
143143 EXPECT_EQ(New, D->getOperand(0));
144144 }
145145
146 TEST(ValueMapperTest, MapMetadataSeeded) {
146 TEST(ValueMapperTest, mapMDNodeSeeded) {
147147 LLVMContext Context;
148148 auto *D = MDTuple::getDistinct(Context, None);
149149
153153
154154 VM.MD().insert(std::make_pair(D, TrackingMDRef(D)));
155155 EXPECT_EQ(D, *VM.getMappedMD(D));
156 EXPECT_EQ(D, MapMetadata(D, VM, RF_None));
157 }
158
159 TEST(ValueMapperTest, MapMetadataSeededWithNull) {
156 EXPECT_EQ(D, ValueMapper(VM).mapMDNode(*D));
157 }
158
159 TEST(ValueMapperTest, mapMDNodeSeededWithNull) {
160160 LLVMContext Context;
161161 auto *D = MDTuple::getDistinct(Context, None);
162162
166166
167167 VM.MD().insert(std::make_pair(D, TrackingMDRef()));
168168 EXPECT_EQ(nullptr, *VM.getMappedMD(D));
169 EXPECT_EQ(nullptr, MapMetadata(D, VM, RF_None));
170 }
171
172 TEST(ValueMapperTest, MapMetadataNullMapGlobalWithIgnoreMissingLocals) {
169 EXPECT_EQ(nullptr, ValueMapper(VM).mapMDNode(*D));
170 }
171
172 TEST(ValueMapperTest, mapMetadataNullMapGlobalWithIgnoreMissingLocals) {
173173 LLVMContext C;
174174 FunctionType *FTy =
175175 FunctionType::get(Type::getVoidTy(C), Type::getInt8Ty(C), false);
178178
179179 ValueToValueMapTy VM;
180180 RemapFlags Flags = RF_IgnoreMissingLocals | RF_NullMapMissingGlobalValues;
181 EXPECT_EQ(nullptr, MapValue(F.get(), VM, Flags));
182 }
183
184 TEST(ValueMapperTest, MapMetadataMDString) {
181 EXPECT_EQ(nullptr, ValueMapper(VM, Flags).mapValue(*F));
182 }
183
184 TEST(ValueMapperTest, mapMetadataMDString) {
185185 LLVMContext C;
186186 auto *S1 = MDString::get(C, "S1");
187187 ValueToValueMapTy VM;
188188
189189 // Make sure S1 maps to itself, but isn't memoized.
190 EXPECT_EQ(S1, MapMetadata(S1, VM));
190 EXPECT_EQ(S1, ValueMapper(VM).mapMetadata(*S1));
191191 EXPECT_EQ(None, VM.getMappedMD(S1));
192192
193193 // We still expect VM.MD() to be respected.
194194 auto *S2 = MDString::get(C, "S2");
195195 VM.MD()[S1].reset(S2);
196 EXPECT_EQ(S2, MapMetadata(S1, VM));
197 }
198
199 TEST(ValueMapperTest, MapMetadataGetMappedMD) {
196 EXPECT_EQ(S2, ValueMapper(VM).mapMetadata(*S1));
197 }
198
199 TEST(ValueMapperTest, mapMetadataGetMappedMD) {
200200 LLVMContext C;
201201 auto *N0 = MDTuple::get(C, None);
202202 auto *N1 = MDTuple::get(C, N0);
204204 // Make sure hasMD and getMappedMD work correctly.
205205 ValueToValueMapTy VM;
206206 EXPECT_FALSE(VM.hasMD());
207 EXPECT_EQ(N0, MapMetadata(N0, VM));
208 EXPECT_EQ(N1, MapMetadata(N1, VM));
207 EXPECT_EQ(N0, ValueMapper(VM).mapMetadata(*N0));
208 EXPECT_EQ(N1, ValueMapper(VM).mapMetadata(*N1));
209209 EXPECT_TRUE(VM.hasMD());
210210 ASSERT_NE(None, VM.getMappedMD(N0));
211211 ASSERT_NE(None, VM.getMappedMD(N1));
213213 EXPECT_EQ(N1, *VM.getMappedMD(N1));
214214 }
215215
216 TEST(ValueMapperTest, MapMetadataNoModuleLevelChanges) {
216 TEST(ValueMapperTest, mapMetadataNoModuleLevelChanges) {
217217 LLVMContext C;
218218 auto *N0 = MDTuple::get(C, None);
219219 auto *N1 = MDTuple::get(C, N0);
221221 // Nothing should be memoized when RF_NoModuleLevelChanges.
222222 ValueToValueMapTy VM;
223223 EXPECT_FALSE(VM.hasMD());
224 EXPECT_EQ(N0, MapMetadata(N0, VM, RF_NoModuleLevelChanges));
225 EXPECT_EQ(N1, MapMetadata(N1, VM, RF_NoModuleLevelChanges));
224 EXPECT_EQ(N0, ValueMapper(VM, RF_NoModuleLevelChanges).mapMetadata(*N0));
225 EXPECT_EQ(N1, ValueMapper(VM, RF_NoModuleLevelChanges).mapMetadata(*N1));
226226 EXPECT_FALSE(VM.hasMD());
227227 EXPECT_EQ(None, VM.getMappedMD(N0));
228228 EXPECT_EQ(None, VM.getMappedMD(N1));
229229 }
230230
231 TEST(ValueMapperTest, MapMetadataConstantAsMetadata) {
231 TEST(ValueMapperTest, mapMetadataConstantAsMetadata) {
232232 LLVMContext C;
233233 FunctionType *FTy =
234234 FunctionType::get(Type::getVoidTy(C), Type::getInt8Ty(C), false);
238238 auto *CAM = ConstantAsMetadata::get(F.get());
239239 {
240240 ValueToValueMapTy VM;
241 EXPECT_EQ(CAM, MapMetadata(CAM, VM));
241 EXPECT_EQ(CAM, ValueMapper(VM).mapMetadata(*CAM));
242242 EXPECT_TRUE(VM.MD().count(CAM));
243243 VM.MD().erase(CAM);
244 EXPECT_EQ(CAM, MapMetadata(CAM, VM, RF_IgnoreMissingLocals));
244 EXPECT_EQ(CAM, ValueMapper(VM, RF_IgnoreMissingLocals).mapMetadata(*CAM));
245245 EXPECT_TRUE(VM.MD().count(CAM));
246246
247247 auto *N = MDTuple::get(C, None);
248248 VM.MD()[CAM].reset(N);
249 EXPECT_EQ(N, MapMetadata(CAM, VM));
250 EXPECT_EQ(N, MapMetadata(CAM, VM, RF_IgnoreMissingLocals));
249 EXPECT_EQ(N, ValueMapper(VM).mapMetadata(*CAM));
250 EXPECT_EQ(N, ValueMapper(VM, RF_IgnoreMissingLocals).mapMetadata(*CAM));
251251 }
252252
253253 std::unique_ptr F2(
254254 Function::Create(FTy, GlobalValue::ExternalLinkage, "F2"));
255255 ValueToValueMapTy VM;
256256 VM[F.get()] = F2.get();
257 auto *F2MD = MapMetadata(CAM, VM);
257 auto *F2MD = ValueMapper(VM).mapMetadata(*CAM);
258258 EXPECT_TRUE(VM.MD().count(CAM));
259259 EXPECT_TRUE(F2MD);
260260 EXPECT_EQ(F2.get(), cast(F2MD)->getValue());
262262
263263 #ifdef GTEST_HAS_DEATH_TEST
264264 #ifndef NDEBUG
265 TEST(ValueMapperTest, MapMetadataLocalAsMetadata) {
265 TEST(ValueMapperTest, mapMetadataLocalAsMetadata) {
266266 LLVMContext C;
267267 FunctionType *FTy =
268268 FunctionType::get(Type::getVoidTy(C), Type::getInt8Ty(C), false);
270270 Function::Create(FTy, GlobalValue::ExternalLinkage, "F"));
271271 Argument &A = *F->arg_begin();
272272
273 // MapMetadata doesn't support LocalAsMetadata. The only valid container for
273 // mapMetadata doesn't support LocalAsMetadata. The only valid container for
274274 // LocalAsMetadata is a MetadataAsValue instance, so use it directly.
275275 auto *LAM = LocalAsMetadata::get(&A);
276276 ValueToValueMapTy VM;
277 EXPECT_DEATH(MapMetadata(LAM, VM), "Unexpected local metadata");
278 EXPECT_DEATH(MapMetadata(LAM, VM, RF_IgnoreMissingLocals),
277 EXPECT_DEATH(ValueMapper(VM).mapMetadata(*LAM), "Unexpected local metadata");
278 EXPECT_DEATH(ValueMapper(VM, RF_IgnoreMissingLocals).mapMetadata(*LAM),
279279 "Unexpected local metadata");
280280 }
281281 #endif
282282 #endif
283283
284 TEST(ValueMapperTest, MapValueLocalAsMetadata) {
284 TEST(ValueMapperTest, mapValueLocalAsMetadata) {
285285 LLVMContext C;
286286 FunctionType *FTy =
287287 FunctionType::get(Type::getVoidTy(C), Type::getInt8Ty(C), false);
304304 auto *N0 = MDTuple::get(C, None);
305305 auto *N0AV = MetadataAsValue::get(C, N0);
306306 ValueToValueMapTy VM;
307 EXPECT_EQ(N0AV, MapValue(MAV, VM));
308 EXPECT_EQ(nullptr, MapValue(MAV, VM, RF_IgnoreMissingLocals));
307 EXPECT_EQ(N0AV, ValueMapper(VM).mapValue(*MAV));
308 EXPECT_EQ(nullptr, ValueMapper(VM, RF_IgnoreMissingLocals).mapValue(*MAV));
309309 EXPECT_FALSE(VM.count(MAV));
310310 EXPECT_FALSE(VM.count(&A));
311311 EXPECT_EQ(None, VM.getMappedMD(LAM));
312312
313313 VM[MAV] = MAV;
314 EXPECT_EQ(MAV, MapValue(MAV, VM));
315 EXPECT_EQ(MAV, MapValue(MAV, VM, RF_IgnoreMissingLocals));
314 EXPECT_EQ(MAV, ValueMapper(VM).mapValue(*MAV));
315 EXPECT_EQ(MAV, ValueMapper(VM, RF_IgnoreMissingLocals).mapValue(*MAV));
316316 EXPECT_TRUE(VM.count(MAV));
317317 EXPECT_FALSE(VM.count(&A));
318318
319319 VM[MAV] = &A;
320 EXPECT_EQ(&A, MapValue(MAV, VM));
321 EXPECT_EQ(&A, MapValue(MAV, VM, RF_IgnoreMissingLocals));
320 EXPECT_EQ(&A, ValueMapper(VM).mapValue(*MAV));
321 EXPECT_EQ(&A, ValueMapper(VM, RF_IgnoreMissingLocals).mapValue(*MAV));
322322 EXPECT_TRUE(VM.count(MAV));
323323 EXPECT_FALSE(VM.count(&A));
324324 }
325325
326 TEST(ValueMapperTest, MapValueLocalAsMetadataToConstant) {
326 TEST(ValueMapperTest, mapValueLocalAsMetadataToConstant) {
327327 LLVMContext Context;
328328 auto *Int8 = Type::getInt8Ty(Context);
329329 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Int8, false);
341341 auto *MDC = MetadataAsValue::get(Context, ValueAsMetadata::get(&C));
342342 EXPECT_TRUE(isa(MDA->getMetadata()));
343343 EXPECT_TRUE(isa(MDC->getMetadata()));
344 EXPECT_EQ(&C, MapValue(&A, VM));
345 EXPECT_EQ(MDC, MapValue(MDA, VM));
344 EXPECT_EQ(&C, ValueMapper(VM).mapValue(A));
345 EXPECT_EQ(MDC, ValueMapper(VM).mapValue(*MDA));
346346 }
347347
348348 } // end namespace