llvm-project

Форк
0
/
ValueTest.cpp 
394 строки · 15.3 Кб
1
//===- llvm/unittest/IR/ValueTest.cpp - Value unit tests ------------------===//
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 "llvm/IR/Value.h"
10
#include "llvm/AsmParser/Parser.h"
11
#include "llvm/IR/Function.h"
12
#include "llvm/IR/IntrinsicInst.h"
13
#include "llvm/IR/LLVMContext.h"
14
#include "llvm/IR/Module.h"
15
#include "llvm/IR/ModuleSlotTracker.h"
16
#include "llvm/Support/CommandLine.h"
17
#include "llvm/Support/SourceMgr.h"
18
#include "gtest/gtest.h"
19
using namespace llvm;
20

21
extern cl::opt<bool> UseNewDbgInfoFormat;
22

23
namespace {
24

25
TEST(ValueTest, UsedInBasicBlock) {
26
  LLVMContext C;
27

28
  const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
29
                             "bb0:\n"
30
                             "  %y1 = add i32 %y, 1\n"
31
                             "  %y2 = add i32 %y, 1\n"
32
                             "  %y3 = add i32 %y, 1\n"
33
                             "  %y4 = add i32 %y, 1\n"
34
                             "  %y5 = add i32 %y, 1\n"
35
                             "  %y6 = add i32 %y, 1\n"
36
                             "  %y7 = add i32 %y, 1\n"
37
                             "  %y8 = add i32 %x, 1\n"
38
                             "  ret void\n"
39
                             "}\n";
40
  SMDiagnostic Err;
41
  std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
42

43
  Function *F = M->getFunction("f");
44

45
  EXPECT_FALSE(F->isUsedInBasicBlock(&F->front()));
46
  EXPECT_TRUE(std::next(F->arg_begin())->isUsedInBasicBlock(&F->front()));
47
  EXPECT_TRUE(F->arg_begin()->isUsedInBasicBlock(&F->front()));
48
}
49

50
TEST(GlobalTest, CreateAddressSpace) {
51
  LLVMContext Ctx;
52
  std::unique_ptr<Module> M(new Module("TestModule", Ctx));
53
  Type *Int8Ty = Type::getInt8Ty(Ctx);
54
  Type *Int32Ty = Type::getInt32Ty(Ctx);
55

56
  GlobalVariable *Dummy0
57
    = new GlobalVariable(*M,
58
                         Int32Ty,
59
                         true,
60
                         GlobalValue::ExternalLinkage,
61
                         Constant::getAllOnesValue(Int32Ty),
62
                         "dummy",
63
                         nullptr,
64
                         GlobalVariable::NotThreadLocal,
65
                         1);
66

67
  const Align kMaxAlignment(Value::MaximumAlignment);
68
  EXPECT_TRUE(kMaxAlignment.value() == 4294967296ULL);
69
  Dummy0->setAlignment(kMaxAlignment);
70
  EXPECT_TRUE(Dummy0->getAlign());
71
  EXPECT_EQ(*Dummy0->getAlign(), kMaxAlignment);
72

73
  // Make sure the address space isn't dropped when returning this.
74
  Constant *Dummy1 = M->getOrInsertGlobal("dummy", Int32Ty);
75
  EXPECT_EQ(Dummy0, Dummy1);
76
  EXPECT_EQ(1u, Dummy1->getType()->getPointerAddressSpace());
77

78

79
  // This one requires a bitcast, but the address space must also stay the same.
80
  GlobalVariable *DummyCast0
81
    = new GlobalVariable(*M,
82
                         Int32Ty,
83
                         true,
84
                         GlobalValue::ExternalLinkage,
85
                         Constant::getAllOnesValue(Int32Ty),
86
                         "dummy_cast",
87
                         nullptr,
88
                         GlobalVariable::NotThreadLocal,
89
                         1);
90

91
  // Make sure the address space isn't dropped when returning this.
92
  Constant *DummyCast1 = M->getOrInsertGlobal("dummy_cast", Int8Ty);
93
  EXPECT_EQ(DummyCast0, DummyCast1);
94
  EXPECT_EQ(1u, DummyCast1->getType()->getPointerAddressSpace());
95
}
96

97
#ifdef GTEST_HAS_DEATH_TEST
98
#ifndef NDEBUG
99

100
TEST(GlobalTest, AlignDeath) {
101
  LLVMContext Ctx;
102
  std::unique_ptr<Module> M(new Module("TestModule", Ctx));
103
  Type *Int32Ty = Type::getInt32Ty(Ctx);
104
  GlobalVariable *Var =
105
      new GlobalVariable(*M, Int32Ty, true, GlobalValue::ExternalLinkage,
106
                         Constant::getAllOnesValue(Int32Ty), "var", nullptr,
107
                         GlobalVariable::NotThreadLocal, 1);
108

109
  EXPECT_DEATH(Var->setAlignment(Align(8589934592ULL)),
110
               "Alignment is greater than MaximumAlignment");
111
}
112
#endif
113
#endif
114

115
TEST(ValueTest, printSlots) {
116
  // Check that Value::print() and Value::printAsOperand() work with and
117
  // without a slot tracker.
118
  LLVMContext C;
119

120
  const char *ModuleString = "@g0 = external global %500\n"
121
                             "@g1 = external global %900\n"
122
                             "\n"
123
                             "%900 = type { i32, i32 }\n"
124
                             "%500 = type { i32 }\n"
125
                             "\n"
126
                             "define void @f(i32 %x, i32 %y) {\n"
127
                             "entry:\n"
128
                             "  %0 = add i32 %y, 1\n"
129
                             "  %1 = add i32 %y, 1\n"
130
                             "  ret void\n"
131
                             "}\n";
132
  SMDiagnostic Err;
133
  std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
134

135
  Function *F = M->getFunction("f");
136
  ASSERT_TRUE(F);
137
  ASSERT_FALSE(F->empty());
138
  BasicBlock &BB = F->getEntryBlock();
139
  ASSERT_EQ(3u, BB.size());
140

141
  Instruction *I0 = &*BB.begin();
142
  ASSERT_TRUE(I0);
143
  Instruction *I1 = &*++BB.begin();
144
  ASSERT_TRUE(I1);
145

146
  GlobalVariable *G0 = M->getGlobalVariable("g0");
147
  ASSERT_TRUE(G0);
148
  GlobalVariable *G1 = M->getGlobalVariable("g1");
149
  ASSERT_TRUE(G1);
150

151
  ModuleSlotTracker MST(M.get());
152

153
#define CHECK_PRINT(INST, STR)                                                 \
154
  do {                                                                         \
155
    {                                                                          \
156
      std::string S;                                                           \
157
      raw_string_ostream OS(S);                                                \
158
      INST->print(OS);                                                         \
159
      EXPECT_EQ(STR, OS.str());                                                \
160
    }                                                                          \
161
    {                                                                          \
162
      std::string S;                                                           \
163
      raw_string_ostream OS(S);                                                \
164
      INST->print(OS, MST);                                                    \
165
      EXPECT_EQ(STR, OS.str());                                                \
166
    }                                                                          \
167
  } while (false)
168
  CHECK_PRINT(I0, "  %0 = add i32 %y, 1");
169
  CHECK_PRINT(I1, "  %1 = add i32 %y, 1");
170
#undef CHECK_PRINT
171

172
#define CHECK_PRINT_AS_OPERAND(INST, TYPE, STR)                                \
173
  do {                                                                         \
174
    {                                                                          \
175
      std::string S;                                                           \
176
      raw_string_ostream OS(S);                                                \
177
      INST->printAsOperand(OS, TYPE);                                          \
178
      EXPECT_EQ(StringRef(STR), StringRef(OS.str()));                          \
179
    }                                                                          \
180
    {                                                                          \
181
      std::string S;                                                           \
182
      raw_string_ostream OS(S);                                                \
183
      INST->printAsOperand(OS, TYPE, MST);                                     \
184
      EXPECT_EQ(StringRef(STR), StringRef(OS.str()));                          \
185
    }                                                                          \
186
  } while (false)
187
  CHECK_PRINT_AS_OPERAND(I0, false, "%0");
188
  CHECK_PRINT_AS_OPERAND(I1, false, "%1");
189
  CHECK_PRINT_AS_OPERAND(I0, true, "i32 %0");
190
  CHECK_PRINT_AS_OPERAND(I1, true, "i32 %1");
191
  CHECK_PRINT_AS_OPERAND(G0, true, "ptr @g0");
192
  CHECK_PRINT_AS_OPERAND(G1, true, "ptr @g1");
193
#undef CHECK_PRINT_AS_OPERAND
194
}
195

196
TEST(ValueTest, getLocalSlots) {
197
  // Verify that the getLocalSlot method returns the correct slot numbers.
198
  LLVMContext C;
199
  const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
200
                             "entry:\n"
201
                             "  %0 = add i32 %y, 1\n"
202
                             "  %1 = add i32 %y, 1\n"
203
                             "  br label %2\n"
204
                             "\n"
205
                             "  ret void\n"
206
                             "}\n";
207
  SMDiagnostic Err;
208
  std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
209

210
  Function *F = M->getFunction("f");
211
  ASSERT_TRUE(F);
212
  ASSERT_FALSE(F->empty());
213
  BasicBlock &EntryBB = F->getEntryBlock();
214
  ASSERT_EQ(3u, EntryBB.size());
215
  BasicBlock *BB2 = &*++F->begin();
216
  ASSERT_TRUE(BB2);
217

218
  Instruction *I0 = &*EntryBB.begin();
219
  ASSERT_TRUE(I0);
220
  Instruction *I1 = &*++EntryBB.begin();
221
  ASSERT_TRUE(I1);
222

223
  ModuleSlotTracker MST(M.get());
224
  MST.incorporateFunction(*F);
225
  EXPECT_EQ(MST.getLocalSlot(I0), 0);
226
  EXPECT_EQ(MST.getLocalSlot(I1), 1);
227
  EXPECT_EQ(MST.getLocalSlot(&EntryBB), -1);
228
  EXPECT_EQ(MST.getLocalSlot(BB2), 2);
229
}
230

231
#if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
232
TEST(ValueTest, getLocalSlotDeath) {
233
  LLVMContext C;
234
  const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
235
                             "entry:\n"
236
                             "  %0 = add i32 %y, 1\n"
237
                             "  %1 = add i32 %y, 1\n"
238
                             "  br label %2\n"
239
                             "\n"
240
                             "  ret void\n"
241
                             "}\n";
242
  SMDiagnostic Err;
243
  std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
244

245
  Function *F = M->getFunction("f");
246
  ASSERT_TRUE(F);
247
  ASSERT_FALSE(F->empty());
248
  BasicBlock *BB2 = &*++F->begin();
249
  ASSERT_TRUE(BB2);
250

251
  ModuleSlotTracker MST(M.get());
252
  EXPECT_DEATH(MST.getLocalSlot(BB2), "No function incorporated");
253
}
254
#endif
255

256
TEST(ValueTest, replaceUsesOutsideBlock) {
257
  // Check that Value::replaceUsesOutsideBlock(New, BB) replaces uses outside
258
  // BB, including dbg.* uses of MetadataAsValue(ValueAsMetadata(this)).
259
  bool OldDbgValueMode = UseNewDbgInfoFormat;
260
  UseNewDbgInfoFormat = false;
261
  const auto *IR = R"(
262
    define i32 @f() !dbg !6 {
263
    entry:
264
      %a = add i32 0, 1, !dbg !15
265
      %b = add i32 0, 2, !dbg !15
266
      %c = add i32 %a, 2, !dbg !15
267
      call void @llvm.dbg.value(metadata i32 %a, metadata !9, metadata !DIExpression()), !dbg !15
268
      br label %exit, !dbg !15
269

270
    exit:
271
      call void @llvm.dbg.value(metadata i32 %a, metadata !11, metadata !DIExpression()), !dbg !16
272
      ret i32 %a, !dbg !16
273
    }
274

275
    declare void @llvm.dbg.value(metadata, metadata, metadata)
276

277
    !llvm.dbg.cu = !{!0}
278
    !llvm.module.flags = !{!5}
279

280
    !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
281
    !1 = !DIFile(filename: "test.ll", directory: "/")
282
    !2 = !{}
283
    !5 = !{i32 2, !"Debug Info Version", i32 3}
284
    !6 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !0, retainedNodes: !8)
285
    !7 = !DISubroutineType(types: !2)
286
    !8 = !{!9, !11}
287
    !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
288
    !10 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_signed)
289
    !11 = !DILocalVariable(name: "2", scope: !6, file: !1, line: 2, type: !12)
290
    !12 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_signed)
291
    !15 = !DILocation(line: 1, column: 1, scope: !6)
292
    !16 = !DILocation(line: 5, column: 1, scope: !6)
293
  )";
294
  LLVMContext Ctx;
295
  SMDiagnostic Err;
296
  std::unique_ptr<Module> M = parseAssemblyString(IR, Err, Ctx);
297
  if (!M)
298
    Err.print("ValueTest", errs());
299

300
  auto GetNext = [](auto *I) { return &*++I->getIterator(); };
301

302
  Function *F = M->getFunction("f");
303
  // Entry.
304
  BasicBlock *Entry = &F->front();
305
  Instruction *A = &Entry->front();
306
  Instruction *B = GetNext(A);
307
  Instruction *C = GetNext(B);
308
  auto *EntryDbg = cast<DbgValueInst>(GetNext(C));
309
  // Exit.
310
  BasicBlock *Exit = GetNext(Entry);
311
  auto *ExitDbg = cast<DbgValueInst>(&Exit->front());
312
  Instruction *Ret = GetNext(ExitDbg);
313

314
  A->replaceUsesOutsideBlock(B, Entry);
315
  // These users are in Entry so shouldn't be changed.
316
  ASSERT_TRUE(C->getOperand(0) == cast<Value>(A));
317
  ASSERT_TRUE(EntryDbg->getValue(0) == cast<Value>(A));
318
  // These users are outside Entry so should be changed.
319
  ASSERT_TRUE(ExitDbg->getValue(0) == cast<Value>(B));
320
  ASSERT_TRUE(Ret->getOperand(0) == cast<Value>(B));
321
  UseNewDbgInfoFormat = OldDbgValueMode;
322
}
323

324
TEST(ValueTest, replaceUsesOutsideBlockDbgVariableRecord) {
325
  // Check that Value::replaceUsesOutsideBlock(New, BB) replaces uses outside
326
  // BB, including DbgVariableRecords.
327
  const auto *IR = R"(
328
    define i32 @f() !dbg !6 {
329
    entry:
330
      %a = add i32 0, 1, !dbg !15
331
      %b = add i32 0, 2, !dbg !15
332
      %c = add i32 %a, 2, !dbg !15
333
      call void @llvm.dbg.value(metadata i32 %a, metadata !9, metadata !DIExpression()), !dbg !15
334
      br label %exit, !dbg !15
335

336
    exit:
337
      call void @llvm.dbg.value(metadata i32 %a, metadata !11, metadata !DIExpression()), !dbg !16
338
      ret i32 %a, !dbg !16
339
    }
340

341
    declare void @llvm.dbg.value(metadata, metadata, metadata)
342

343
    !llvm.dbg.cu = !{!0}
344
    !llvm.module.flags = !{!5}
345

346
    !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
347
    !1 = !DIFile(filename: "test.ll", directory: "/")
348
    !2 = !{}
349
    !5 = !{i32 2, !"Debug Info Version", i32 3}
350
    !6 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !0, retainedNodes: !8)
351
    !7 = !DISubroutineType(types: !2)
352
    !8 = !{!9, !11}
353
    !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
354
    !10 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_signed)
355
    !11 = !DILocalVariable(name: "2", scope: !6, file: !1, line: 2, type: !12)
356
    !12 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_signed)
357
    !15 = !DILocation(line: 1, column: 1, scope: !6)
358
    !16 = !DILocation(line: 5, column: 1, scope: !6)
359
  )";
360
  LLVMContext Ctx;
361
  SMDiagnostic Err;
362
  std::unique_ptr<Module> M = parseAssemblyString(IR, Err, Ctx);
363
  if (!M)
364
    Err.print("ValueTest", errs());
365

366
  auto GetNext = [](auto *I) { return &*++I->getIterator(); };
367

368
  Function *F = M->getFunction("f");
369
  // Entry.
370
  BasicBlock *Entry = &F->front();
371
  Instruction *A = &Entry->front();
372
  Instruction *B = GetNext(A);
373
  Instruction *C = GetNext(B);
374
  Instruction *Branch = GetNext(C);
375
  // Exit.
376
  BasicBlock *Exit = GetNext(Entry);
377
  Instruction *Ret = &Exit->front();
378

379
  EXPECT_TRUE(Branch->hasDbgRecords());
380
  EXPECT_TRUE(Ret->hasDbgRecords());
381

382
  DbgVariableRecord *DVR1 =
383
      cast<DbgVariableRecord>(&*Branch->getDbgRecordRange().begin());
384
  DbgVariableRecord *DVR2 =
385
      cast<DbgVariableRecord>(&*Ret->getDbgRecordRange().begin());
386

387
  A->replaceUsesOutsideBlock(B, Entry);
388
  // These users are in Entry so shouldn't be changed.
389
  EXPECT_TRUE(DVR1->getVariableLocationOp(0) == cast<Value>(A));
390
  // These users are outside Entry so should be changed.
391
  EXPECT_TRUE(DVR2->getVariableLocationOp(0) == cast<Value>(B));
392
}
393

394
} // end anonymous namespace
395

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.