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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
| //===- ValueMapper.cpp - Unit tests for ValueMapper -----------------------===//
//
// 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/Transforms/Utils/ValueMapper.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
TEST(ValueMapperTest, mapMDNode) {
LLVMContext Context;
auto *U = MDTuple::get(Context, None);
// The node should be unchanged.
ValueToValueMapTy VM;
EXPECT_EQ(U, ValueMapper(VM).mapMDNode(*U));
}
TEST(ValueMapperTest, mapMDNodeCycle) {
LLVMContext Context;
MDNode *U0;
MDNode *U1;
{
Metadata *Ops[] = {nullptr};
auto T = MDTuple::getTemporary(Context, Ops);
Ops[0] = T.get();
U0 = MDTuple::get(Context, Ops);
T->replaceOperandWith(0, U0);
U1 = MDNode::replaceWithUniqued(std::move(T));
U0->resolveCycles();
}
EXPECT_TRUE(U0->isResolved());
EXPECT_TRUE(U0->isUniqued());
EXPECT_TRUE(U1->isResolved());
EXPECT_TRUE(U1->isUniqued());
EXPECT_EQ(U1, U0->getOperand(0));
EXPECT_EQ(U0, U1->getOperand(0));
// Cycles shouldn't be duplicated.
{
ValueToValueMapTy VM;
EXPECT_EQ(U0, ValueMapper(VM).mapMDNode(*U0));
EXPECT_EQ(U1, ValueMapper(VM).mapMDNode(*U1));
}
// Check the other order.
{
ValueToValueMapTy VM;
EXPECT_EQ(U1, ValueMapper(VM).mapMDNode(*U1));
EXPECT_EQ(U0, ValueMapper(VM).mapMDNode(*U0));
}
}
TEST(ValueMapperTest, mapMDNodeDuplicatedCycle) {
LLVMContext Context;
auto *PtrTy = Type::getInt8Ty(Context)->getPointerTo();
std::unique_ptr<GlobalVariable> G0 = std::make_unique<GlobalVariable>(
PtrTy, false, GlobalValue::ExternalLinkage, nullptr, "G0");
std::unique_ptr<GlobalVariable> G1 = std::make_unique<GlobalVariable>(
PtrTy, false, GlobalValue::ExternalLinkage, nullptr, "G1");
// Create a cycle that references G0.
MDNode *N0; // !0 = !{!1}
MDNode *N1; // !1 = !{!0, i8* @G0}
{
auto T0 = MDTuple::getTemporary(Context, nullptr);
Metadata *Ops1[] = {T0.get(), ConstantAsMetadata::get(G0.get())};
N1 = MDTuple::get(Context, Ops1);
T0->replaceOperandWith(0, N1);
N0 = MDNode::replaceWithUniqued(std::move(T0));
}
// Resolve N0 and N1.
ASSERT_FALSE(N0->isResolved());
ASSERT_FALSE(N1->isResolved());
N0->resolveCycles();
ASSERT_TRUE(N0->isResolved());
ASSERT_TRUE(N1->isResolved());
// Seed the value map to map G0 to G1 and map the nodes. The output should
// have new nodes that reference G1 (instead of G0).
ValueToValueMapTy VM;
VM[G0.get()] = G1.get();
MDNode *MappedN0 = ValueMapper(VM).mapMDNode(*N0);
MDNode *MappedN1 = ValueMapper(VM).mapMDNode(*N1);
EXPECT_NE(N0, MappedN0);
EXPECT_NE(N1, MappedN1);
EXPECT_EQ(ConstantAsMetadata::get(G1.get()), MappedN1->getOperand(1));
// Check that the output nodes are resolved.
EXPECT_TRUE(MappedN0->isResolved());
EXPECT_TRUE(MappedN1->isResolved());
}
TEST(ValueMapperTest, mapMDNodeUnresolved) {
LLVMContext Context;
TempMDTuple T = MDTuple::getTemporary(Context, None);
ValueToValueMapTy VM;
EXPECT_EQ(T.get(), ValueMapper(VM, RF_NoModuleLevelChanges).mapMDNode(*T));
}
TEST(ValueMapperTest, mapMDNodeDistinct) {
LLVMContext Context;
auto *D = MDTuple::getDistinct(Context, None);
{
// The node should be cloned.
ValueToValueMapTy VM;
EXPECT_NE(D, ValueMapper(VM).mapMDNode(*D));
}
{
// The node should be moved.
ValueToValueMapTy VM;
EXPECT_EQ(D, ValueMapper(VM, RF_MoveDistinctMDs).mapMDNode(*D));
}
}
TEST(ValueMapperTest, mapMDNodeDistinctOperands) {
LLVMContext Context;
Metadata *Old = MDTuple::getDistinct(Context, None);
auto *D = MDTuple::getDistinct(Context, Old);
ASSERT_EQ(Old, D->getOperand(0));
Metadata *New = MDTuple::getDistinct(Context, None);
ValueToValueMapTy VM;
VM.MD()[Old].reset(New);
// Make sure operands are updated.
EXPECT_EQ(D, ValueMapper(VM, RF_MoveDistinctMDs).mapMDNode(*D));
EXPECT_EQ(New, D->getOperand(0));
}
TEST(ValueMapperTest, mapMDNodeSeeded) {
LLVMContext Context;
auto *D = MDTuple::getDistinct(Context, None);
// The node should be moved.
ValueToValueMapTy VM;
EXPECT_EQ(None, VM.getMappedMD(D));
VM.MD().insert(std::make_pair(D, TrackingMDRef(D)));
EXPECT_EQ(D, *VM.getMappedMD(D));
EXPECT_EQ(D, ValueMapper(VM).mapMDNode(*D));
}
TEST(ValueMapperTest, mapMDNodeSeededWithNull) {
LLVMContext Context;
auto *D = MDTuple::getDistinct(Context, None);
// The node should be moved.
ValueToValueMapTy VM;
EXPECT_EQ(None, VM.getMappedMD(D));
VM.MD().insert(std::make_pair(D, TrackingMDRef()));
EXPECT_EQ(nullptr, *VM.getMappedMD(D));
EXPECT_EQ(nullptr, ValueMapper(VM).mapMDNode(*D));
}
TEST(ValueMapperTest, mapMetadataNullMapGlobalWithIgnoreMissingLocals) {
LLVMContext C;
FunctionType *FTy =
FunctionType::get(Type::getVoidTy(C), Type::getInt8Ty(C), false);
std::unique_ptr<Function> F(
Function::Create(FTy, GlobalValue::ExternalLinkage, "F"));
ValueToValueMapTy VM;
RemapFlags Flags = RF_IgnoreMissingLocals | RF_NullMapMissingGlobalValues;
EXPECT_EQ(nullptr, ValueMapper(VM, Flags).mapValue(*F));
}
TEST(ValueMapperTest, mapMetadataMDString) {
LLVMContext C;
auto *S1 = MDString::get(C, "S1");
ValueToValueMapTy VM;
// Make sure S1 maps to itself, but isn't memoized.
EXPECT_EQ(S1, ValueMapper(VM).mapMetadata(*S1));
EXPECT_EQ(None, VM.getMappedMD(S1));
// We still expect VM.MD() to be respected.
auto *S2 = MDString::get(C, "S2");
VM.MD()[S1].reset(S2);
EXPECT_EQ(S2, ValueMapper(VM).mapMetadata(*S1));
}
TEST(ValueMapperTest, mapMetadataGetMappedMD) {
LLVMContext C;
auto *N0 = MDTuple::get(C, None);
auto *N1 = MDTuple::get(C, N0);
// Make sure hasMD and getMappedMD work correctly.
ValueToValueMapTy VM;
EXPECT_FALSE(VM.hasMD());
EXPECT_EQ(N0, ValueMapper(VM).mapMetadata(*N0));
EXPECT_EQ(N1, ValueMapper(VM).mapMetadata(*N1));
EXPECT_TRUE(VM.hasMD());
ASSERT_NE(None, VM.getMappedMD(N0));
ASSERT_NE(None, VM.getMappedMD(N1));
EXPECT_EQ(N0, *VM.getMappedMD(N0));
EXPECT_EQ(N1, *VM.getMappedMD(N1));
}
TEST(ValueMapperTest, mapMetadataNoModuleLevelChanges) {
LLVMContext C;
auto *N0 = MDTuple::get(C, None);
auto *N1 = MDTuple::get(C, N0);
// Nothing should be memoized when RF_NoModuleLevelChanges.
ValueToValueMapTy VM;
EXPECT_FALSE(VM.hasMD());
EXPECT_EQ(N0, ValueMapper(VM, RF_NoModuleLevelChanges).mapMetadata(*N0));
EXPECT_EQ(N1, ValueMapper(VM, RF_NoModuleLevelChanges).mapMetadata(*N1));
EXPECT_FALSE(VM.hasMD());
EXPECT_EQ(None, VM.getMappedMD(N0));
EXPECT_EQ(None, VM.getMappedMD(N1));
}
TEST(ValueMapperTest, mapMetadataConstantAsMetadata) {
LLVMContext C;
FunctionType *FTy =
FunctionType::get(Type::getVoidTy(C), Type::getInt8Ty(C), false);
std::unique_ptr<Function> F(
Function::Create(FTy, GlobalValue::ExternalLinkage, "F"));
auto *CAM = ConstantAsMetadata::get(F.get());
{
// ConstantAsMetadata shouldn't be memoized.
ValueToValueMapTy VM;
EXPECT_EQ(CAM, ValueMapper(VM).mapMetadata(*CAM));
EXPECT_FALSE(VM.MD().count(CAM));
EXPECT_EQ(CAM, ValueMapper(VM, RF_IgnoreMissingLocals).mapMetadata(*CAM));
EXPECT_FALSE(VM.MD().count(CAM));
// But it should respect a mapping that gets seeded.
auto *N = MDTuple::get(C, None);
VM.MD()[CAM].reset(N);
EXPECT_EQ(N, ValueMapper(VM).mapMetadata(*CAM));
EXPECT_EQ(N, ValueMapper(VM, RF_IgnoreMissingLocals).mapMetadata(*CAM));
}
std::unique_ptr<Function> F2(
Function::Create(FTy, GlobalValue::ExternalLinkage, "F2"));
ValueToValueMapTy VM;
VM[F.get()] = F2.get();
auto *F2MD = ValueMapper(VM).mapMetadata(*CAM);
EXPECT_FALSE(VM.MD().count(CAM));
EXPECT_TRUE(F2MD);
EXPECT_EQ(F2.get(), cast<ConstantAsMetadata>(F2MD)->getValue());
}
#ifdef GTEST_HAS_DEATH_TEST
#ifndef NDEBUG
TEST(ValueMapperTest, mapMetadataLocalAsMetadata) {
LLVMContext C;
FunctionType *FTy =
FunctionType::get(Type::getVoidTy(C), Type::getInt8Ty(C), false);
std::unique_ptr<Function> F(
Function::Create(FTy, GlobalValue::ExternalLinkage, "F"));
Argument &A = *F->arg_begin();
// mapMetadata doesn't support LocalAsMetadata. The only valid container for
// LocalAsMetadata is a MetadataAsValue instance, so use it directly.
auto *LAM = LocalAsMetadata::get(&A);
ValueToValueMapTy VM;
EXPECT_DEATH(ValueMapper(VM).mapMetadata(*LAM), "Unexpected local metadata");
EXPECT_DEATH(ValueMapper(VM, RF_IgnoreMissingLocals).mapMetadata(*LAM),
"Unexpected local metadata");
}
#endif
#endif
TEST(ValueMapperTest, mapValueLocalAsMetadata) {
LLVMContext C;
FunctionType *FTy =
FunctionType::get(Type::getVoidTy(C), Type::getInt8Ty(C), false);
std::unique_ptr<Function> F(
Function::Create(FTy, GlobalValue::ExternalLinkage, "F"));
Argument &A = *F->arg_begin();
auto *LAM = LocalAsMetadata::get(&A);
auto *MAV = MetadataAsValue::get(C, LAM);
// The principled answer to a LocalAsMetadata of an unmapped SSA value would
// be to return nullptr (regardless of RF_IgnoreMissingLocals).
//
// However, algorithms that use RemapInstruction assume that each instruction
// only references SSA values from previous instructions. Arguments of
// such as "metadata i32 %x" don't currently successfully maintain that
// property. To keep RemapInstruction from crashing we need a non-null
// return here, but we also shouldn't reference the unmapped local. Use
// "metadata !{}".
auto *N0 = MDTuple::get(C, None);
auto *N0AV = MetadataAsValue::get(C, N0);
ValueToValueMapTy VM;
EXPECT_EQ(N0AV, ValueMapper(VM).mapValue(*MAV));
EXPECT_EQ(nullptr, ValueMapper(VM, RF_IgnoreMissingLocals).mapValue(*MAV));
EXPECT_FALSE(VM.count(MAV));
EXPECT_FALSE(VM.count(&A));
EXPECT_EQ(None, VM.getMappedMD(LAM));
VM[MAV] = MAV;
EXPECT_EQ(MAV, ValueMapper(VM).mapValue(*MAV));
EXPECT_EQ(MAV, ValueMapper(VM, RF_IgnoreMissingLocals).mapValue(*MAV));
EXPECT_TRUE(VM.count(MAV));
EXPECT_FALSE(VM.count(&A));
VM[MAV] = &A;
EXPECT_EQ(&A, ValueMapper(VM).mapValue(*MAV));
EXPECT_EQ(&A, ValueMapper(VM, RF_IgnoreMissingLocals).mapValue(*MAV));
EXPECT_TRUE(VM.count(MAV));
EXPECT_FALSE(VM.count(&A));
}
TEST(ValueMapperTest, mapValueLocalAsMetadataToConstant) {
LLVMContext Context;
auto *Int8 = Type::getInt8Ty(Context);
FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Int8, false);
std::unique_ptr<Function> F(
Function::Create(FTy, GlobalValue::ExternalLinkage, "F"));
// Map a local value to a constant.
Argument &A = *F->arg_begin();
Constant &C = *ConstantInt::get(Int8, 42);
ValueToValueMapTy VM;
VM[&A] = &C;
// Look up the metadata-as-value wrapper. Don't crash.
auto *MDA = MetadataAsValue::get(Context, ValueAsMetadata::get(&A));
auto *MDC = MetadataAsValue::get(Context, ValueAsMetadata::get(&C));
EXPECT_TRUE(isa<LocalAsMetadata>(MDA->getMetadata()));
EXPECT_TRUE(isa<ConstantAsMetadata>(MDC->getMetadata()));
EXPECT_EQ(&C, ValueMapper(VM).mapValue(A));
EXPECT_EQ(MDC, ValueMapper(VM).mapValue(*MDA));
}
} // end namespace
|