llvm-project
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"
19using namespace llvm;
20
21extern cl::opt<bool> UseNewDbgInfoFormat;
22
23namespace {
24
25TEST(ValueTest, UsedInBasicBlock) {
26LLVMContext C;
27
28const 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";
40SMDiagnostic Err;
41std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
42
43Function *F = M->getFunction("f");
44
45EXPECT_FALSE(F->isUsedInBasicBlock(&F->front()));
46EXPECT_TRUE(std::next(F->arg_begin())->isUsedInBasicBlock(&F->front()));
47EXPECT_TRUE(F->arg_begin()->isUsedInBasicBlock(&F->front()));
48}
49
50TEST(GlobalTest, CreateAddressSpace) {
51LLVMContext Ctx;
52std::unique_ptr<Module> M(new Module("TestModule", Ctx));
53Type *Int8Ty = Type::getInt8Ty(Ctx);
54Type *Int32Ty = Type::getInt32Ty(Ctx);
55
56GlobalVariable *Dummy0
57= new GlobalVariable(*M,
58Int32Ty,
59true,
60GlobalValue::ExternalLinkage,
61Constant::getAllOnesValue(Int32Ty),
62"dummy",
63nullptr,
64GlobalVariable::NotThreadLocal,
651);
66
67const Align kMaxAlignment(Value::MaximumAlignment);
68EXPECT_TRUE(kMaxAlignment.value() == 4294967296ULL);
69Dummy0->setAlignment(kMaxAlignment);
70EXPECT_TRUE(Dummy0->getAlign());
71EXPECT_EQ(*Dummy0->getAlign(), kMaxAlignment);
72
73// Make sure the address space isn't dropped when returning this.
74Constant *Dummy1 = M->getOrInsertGlobal("dummy", Int32Ty);
75EXPECT_EQ(Dummy0, Dummy1);
76EXPECT_EQ(1u, Dummy1->getType()->getPointerAddressSpace());
77
78
79// This one requires a bitcast, but the address space must also stay the same.
80GlobalVariable *DummyCast0
81= new GlobalVariable(*M,
82Int32Ty,
83true,
84GlobalValue::ExternalLinkage,
85Constant::getAllOnesValue(Int32Ty),
86"dummy_cast",
87nullptr,
88GlobalVariable::NotThreadLocal,
891);
90
91// Make sure the address space isn't dropped when returning this.
92Constant *DummyCast1 = M->getOrInsertGlobal("dummy_cast", Int8Ty);
93EXPECT_EQ(DummyCast0, DummyCast1);
94EXPECT_EQ(1u, DummyCast1->getType()->getPointerAddressSpace());
95}
96
97#ifdef GTEST_HAS_DEATH_TEST
98#ifndef NDEBUG
99
100TEST(GlobalTest, AlignDeath) {
101LLVMContext Ctx;
102std::unique_ptr<Module> M(new Module("TestModule", Ctx));
103Type *Int32Ty = Type::getInt32Ty(Ctx);
104GlobalVariable *Var =
105new GlobalVariable(*M, Int32Ty, true, GlobalValue::ExternalLinkage,
106Constant::getAllOnesValue(Int32Ty), "var", nullptr,
107GlobalVariable::NotThreadLocal, 1);
108
109EXPECT_DEATH(Var->setAlignment(Align(8589934592ULL)),
110"Alignment is greater than MaximumAlignment");
111}
112#endif
113#endif
114
115TEST(ValueTest, printSlots) {
116// Check that Value::print() and Value::printAsOperand() work with and
117// without a slot tracker.
118LLVMContext C;
119
120const 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";
132SMDiagnostic Err;
133std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
134
135Function *F = M->getFunction("f");
136ASSERT_TRUE(F);
137ASSERT_FALSE(F->empty());
138BasicBlock &BB = F->getEntryBlock();
139ASSERT_EQ(3u, BB.size());
140
141Instruction *I0 = &*BB.begin();
142ASSERT_TRUE(I0);
143Instruction *I1 = &*++BB.begin();
144ASSERT_TRUE(I1);
145
146GlobalVariable *G0 = M->getGlobalVariable("g0");
147ASSERT_TRUE(G0);
148GlobalVariable *G1 = M->getGlobalVariable("g1");
149ASSERT_TRUE(G1);
150
151ModuleSlotTracker MST(M.get());
152
153#define CHECK_PRINT(INST, STR) \
154do { \
155{ \
156std::string S; \
157raw_string_ostream OS(S); \
158INST->print(OS); \
159EXPECT_EQ(STR, OS.str()); \
160} \
161{ \
162std::string S; \
163raw_string_ostream OS(S); \
164INST->print(OS, MST); \
165EXPECT_EQ(STR, OS.str()); \
166} \
167} while (false)
168CHECK_PRINT(I0, " %0 = add i32 %y, 1");
169CHECK_PRINT(I1, " %1 = add i32 %y, 1");
170#undef CHECK_PRINT
171
172#define CHECK_PRINT_AS_OPERAND(INST, TYPE, STR) \
173do { \
174{ \
175std::string S; \
176raw_string_ostream OS(S); \
177INST->printAsOperand(OS, TYPE); \
178EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
179} \
180{ \
181std::string S; \
182raw_string_ostream OS(S); \
183INST->printAsOperand(OS, TYPE, MST); \
184EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
185} \
186} while (false)
187CHECK_PRINT_AS_OPERAND(I0, false, "%0");
188CHECK_PRINT_AS_OPERAND(I1, false, "%1");
189CHECK_PRINT_AS_OPERAND(I0, true, "i32 %0");
190CHECK_PRINT_AS_OPERAND(I1, true, "i32 %1");
191CHECK_PRINT_AS_OPERAND(G0, true, "ptr @g0");
192CHECK_PRINT_AS_OPERAND(G1, true, "ptr @g1");
193#undef CHECK_PRINT_AS_OPERAND
194}
195
196TEST(ValueTest, getLocalSlots) {
197// Verify that the getLocalSlot method returns the correct slot numbers.
198LLVMContext C;
199const 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";
207SMDiagnostic Err;
208std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
209
210Function *F = M->getFunction("f");
211ASSERT_TRUE(F);
212ASSERT_FALSE(F->empty());
213BasicBlock &EntryBB = F->getEntryBlock();
214ASSERT_EQ(3u, EntryBB.size());
215BasicBlock *BB2 = &*++F->begin();
216ASSERT_TRUE(BB2);
217
218Instruction *I0 = &*EntryBB.begin();
219ASSERT_TRUE(I0);
220Instruction *I1 = &*++EntryBB.begin();
221ASSERT_TRUE(I1);
222
223ModuleSlotTracker MST(M.get());
224MST.incorporateFunction(*F);
225EXPECT_EQ(MST.getLocalSlot(I0), 0);
226EXPECT_EQ(MST.getLocalSlot(I1), 1);
227EXPECT_EQ(MST.getLocalSlot(&EntryBB), -1);
228EXPECT_EQ(MST.getLocalSlot(BB2), 2);
229}
230
231#if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
232TEST(ValueTest, getLocalSlotDeath) {
233LLVMContext C;
234const 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";
242SMDiagnostic Err;
243std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
244
245Function *F = M->getFunction("f");
246ASSERT_TRUE(F);
247ASSERT_FALSE(F->empty());
248BasicBlock *BB2 = &*++F->begin();
249ASSERT_TRUE(BB2);
250
251ModuleSlotTracker MST(M.get());
252EXPECT_DEATH(MST.getLocalSlot(BB2), "No function incorporated");
253}
254#endif
255
256TEST(ValueTest, replaceUsesOutsideBlock) {
257// Check that Value::replaceUsesOutsideBlock(New, BB) replaces uses outside
258// BB, including dbg.* uses of MetadataAsValue(ValueAsMetadata(this)).
259bool OldDbgValueMode = UseNewDbgInfoFormat;
260UseNewDbgInfoFormat = false;
261const auto *IR = R"(
262define i32 @f() !dbg !6 {
263entry:
264%a = add i32 0, 1, !dbg !15
265%b = add i32 0, 2, !dbg !15
266%c = add i32 %a, 2, !dbg !15
267call void @llvm.dbg.value(metadata i32 %a, metadata !9, metadata !DIExpression()), !dbg !15
268br label %exit, !dbg !15
269
270exit:
271call void @llvm.dbg.value(metadata i32 %a, metadata !11, metadata !DIExpression()), !dbg !16
272ret i32 %a, !dbg !16
273}
274
275declare 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)";
294LLVMContext Ctx;
295SMDiagnostic Err;
296std::unique_ptr<Module> M = parseAssemblyString(IR, Err, Ctx);
297if (!M)
298Err.print("ValueTest", errs());
299
300auto GetNext = [](auto *I) { return &*++I->getIterator(); };
301
302Function *F = M->getFunction("f");
303// Entry.
304BasicBlock *Entry = &F->front();
305Instruction *A = &Entry->front();
306Instruction *B = GetNext(A);
307Instruction *C = GetNext(B);
308auto *EntryDbg = cast<DbgValueInst>(GetNext(C));
309// Exit.
310BasicBlock *Exit = GetNext(Entry);
311auto *ExitDbg = cast<DbgValueInst>(&Exit->front());
312Instruction *Ret = GetNext(ExitDbg);
313
314A->replaceUsesOutsideBlock(B, Entry);
315// These users are in Entry so shouldn't be changed.
316ASSERT_TRUE(C->getOperand(0) == cast<Value>(A));
317ASSERT_TRUE(EntryDbg->getValue(0) == cast<Value>(A));
318// These users are outside Entry so should be changed.
319ASSERT_TRUE(ExitDbg->getValue(0) == cast<Value>(B));
320ASSERT_TRUE(Ret->getOperand(0) == cast<Value>(B));
321UseNewDbgInfoFormat = OldDbgValueMode;
322}
323
324TEST(ValueTest, replaceUsesOutsideBlockDbgVariableRecord) {
325// Check that Value::replaceUsesOutsideBlock(New, BB) replaces uses outside
326// BB, including DbgVariableRecords.
327const auto *IR = R"(
328define i32 @f() !dbg !6 {
329entry:
330%a = add i32 0, 1, !dbg !15
331%b = add i32 0, 2, !dbg !15
332%c = add i32 %a, 2, !dbg !15
333call void @llvm.dbg.value(metadata i32 %a, metadata !9, metadata !DIExpression()), !dbg !15
334br label %exit, !dbg !15
335
336exit:
337call void @llvm.dbg.value(metadata i32 %a, metadata !11, metadata !DIExpression()), !dbg !16
338ret i32 %a, !dbg !16
339}
340
341declare 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)";
360LLVMContext Ctx;
361SMDiagnostic Err;
362std::unique_ptr<Module> M = parseAssemblyString(IR, Err, Ctx);
363if (!M)
364Err.print("ValueTest", errs());
365
366auto GetNext = [](auto *I) { return &*++I->getIterator(); };
367
368Function *F = M->getFunction("f");
369// Entry.
370BasicBlock *Entry = &F->front();
371Instruction *A = &Entry->front();
372Instruction *B = GetNext(A);
373Instruction *C = GetNext(B);
374Instruction *Branch = GetNext(C);
375// Exit.
376BasicBlock *Exit = GetNext(Entry);
377Instruction *Ret = &Exit->front();
378
379EXPECT_TRUE(Branch->hasDbgRecords());
380EXPECT_TRUE(Ret->hasDbgRecords());
381
382DbgVariableRecord *DVR1 =
383cast<DbgVariableRecord>(&*Branch->getDbgRecordRange().begin());
384DbgVariableRecord *DVR2 =
385cast<DbgVariableRecord>(&*Ret->getDbgRecordRange().begin());
386
387A->replaceUsesOutsideBlock(B, Entry);
388// These users are in Entry so shouldn't be changed.
389EXPECT_TRUE(DVR1->getVariableLocationOp(0) == cast<Value>(A));
390// These users are outside Entry so should be changed.
391EXPECT_TRUE(DVR2->getVariableLocationOp(0) == cast<Value>(B));
392}
393
394} // end anonymous namespace
395