1 //===- llvm/unittest/IR/ValueTest.cpp - Value unit tests ------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
21 extern cl::opt
<bool> UseNewDbgInfoFormat
;
25 TEST(ValueTest
, UsedInBasicBlock
) {
28 const char *ModuleString
= "define void @f(i32 %x, i32 %y) {\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"
41 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
43 Function
*F
= M
->getFunction("f");
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()));
50 TEST(GlobalTest
, CreateAddressSpace
) {
52 std::unique_ptr
<Module
> M(new Module("TestModule", Ctx
));
53 Type
*Int8Ty
= Type::getInt8Ty(Ctx
);
54 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
56 GlobalVariable
*Dummy0
57 = new GlobalVariable(*M
,
60 GlobalValue::ExternalLinkage
,
61 Constant::getAllOnesValue(Int32Ty
),
64 GlobalVariable::NotThreadLocal
,
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
);
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());
79 // This one requires a bitcast, but the address space must also stay the same.
80 GlobalVariable
*DummyCast0
81 = new GlobalVariable(*M
,
84 GlobalValue::ExternalLinkage
,
85 Constant::getAllOnesValue(Int32Ty
),
88 GlobalVariable::NotThreadLocal
,
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());
97 #ifdef GTEST_HAS_DEATH_TEST
100 TEST(GlobalTest
, AlignDeath
) {
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);
109 EXPECT_DEATH(Var
->setAlignment(Align(8589934592ULL)),
110 "Alignment is greater than MaximumAlignment");
115 TEST(ValueTest
, printSlots
) {
116 // Check that Value::print() and Value::printAsOperand() work with and
117 // without a slot tracker.
120 const char *ModuleString
= "@g0 = external global %500\n"
121 "@g1 = external global %900\n"
123 "%900 = type { i32, i32 }\n"
124 "%500 = type { i32 }\n"
126 "define void @f(i32 %x, i32 %y) {\n"
128 " %0 = add i32 %y, 1\n"
129 " %1 = add i32 %y, 1\n"
133 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
135 Function
*F
= M
->getFunction("f");
137 ASSERT_FALSE(F
->empty());
138 BasicBlock
&BB
= F
->getEntryBlock();
139 ASSERT_EQ(3u, BB
.size());
141 Instruction
*I0
= &*BB
.begin();
143 Instruction
*I1
= &*++BB
.begin();
146 GlobalVariable
*G0
= M
->getGlobalVariable("g0");
148 GlobalVariable
*G1
= M
->getGlobalVariable("g1");
151 ModuleSlotTracker
MST(M
.get());
153 #define CHECK_PRINT(INST, STR) \
157 raw_string_ostream OS(S); \
159 EXPECT_EQ(STR, OS.str()); \
163 raw_string_ostream OS(S); \
164 INST->print(OS, MST); \
165 EXPECT_EQ(STR, OS.str()); \
168 CHECK_PRINT(I0
, " %0 = add i32 %y, 1");
169 CHECK_PRINT(I1
, " %1 = add i32 %y, 1");
172 #define CHECK_PRINT_AS_OPERAND(INST, TYPE, STR) \
176 raw_string_ostream OS(S); \
177 INST->printAsOperand(OS, TYPE); \
178 EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
182 raw_string_ostream OS(S); \
183 INST->printAsOperand(OS, TYPE, MST); \
184 EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
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
196 TEST(ValueTest
, getLocalSlots
) {
197 // Verify that the getLocalSlot method returns the correct slot numbers.
199 const char *ModuleString
= "define void @f(i32 %x, i32 %y) {\n"
201 " %0 = add i32 %y, 1\n"
202 " %1 = add i32 %y, 1\n"
208 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
210 Function
*F
= M
->getFunction("f");
212 ASSERT_FALSE(F
->empty());
213 BasicBlock
&EntryBB
= F
->getEntryBlock();
214 ASSERT_EQ(3u, EntryBB
.size());
215 BasicBlock
*BB2
= &*++F
->begin();
218 Instruction
*I0
= &*EntryBB
.begin();
220 Instruction
*I1
= &*++EntryBB
.begin();
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);
231 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
232 TEST(ValueTest
, getLocalSlotDeath
) {
234 const char *ModuleString
= "define void @f(i32 %x, i32 %y) {\n"
236 " %0 = add i32 %y, 1\n"
237 " %1 = add i32 %y, 1\n"
243 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
245 Function
*F
= M
->getFunction("f");
247 ASSERT_FALSE(F
->empty());
248 BasicBlock
*BB2
= &*++F
->begin();
251 ModuleSlotTracker
MST(M
.get());
252 EXPECT_DEATH(MST
.getLocalSlot(BB2
), "No function incorporated");
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;
262 define i32 @f() !dbg !6 {
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
271 call void @llvm.dbg.value(metadata i32 %a, metadata !11, metadata !DIExpression()), !dbg !16
275 declare void @llvm.dbg.value(metadata, metadata, metadata)
278 !llvm.module.flags = !{!5}
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: "/")
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)
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)
296 std::unique_ptr
<Module
> M
= parseAssemblyString(IR
, Err
, Ctx
);
298 Err
.print("ValueTest", errs());
300 auto GetNext
= [](auto *I
) { return &*++I
->getIterator(); };
302 Function
*F
= M
->getFunction("f");
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
));
310 BasicBlock
*Exit
= GetNext(Entry
);
311 auto *ExitDbg
= cast
<DbgValueInst
>(&Exit
->front());
312 Instruction
*Ret
= GetNext(ExitDbg
);
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
;
324 TEST(ValueTest
, replaceUsesOutsideBlockDbgVariableRecord
) {
325 // Check that Value::replaceUsesOutsideBlock(New, BB) replaces uses outside
326 // BB, including DbgVariableRecords.
328 define i32 @f() !dbg !6 {
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
337 call void @llvm.dbg.value(metadata i32 %a, metadata !11, metadata !DIExpression()), !dbg !16
341 declare void @llvm.dbg.value(metadata, metadata, metadata)
344 !llvm.module.flags = !{!5}
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: "/")
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)
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)
362 std::unique_ptr
<Module
> M
= parseAssemblyString(IR
, Err
, Ctx
);
364 Err
.print("ValueTest", errs());
366 auto GetNext
= [](auto *I
) { return &*++I
->getIterator(); };
368 Function
*F
= M
->getFunction("f");
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
);
376 BasicBlock
*Exit
= GetNext(Entry
);
377 Instruction
*Ret
= &Exit
->front();
379 EXPECT_TRUE(Branch
->hasDbgRecords());
380 EXPECT_TRUE(Ret
->hasDbgRecords());
382 DbgVariableRecord
*DVR1
=
383 cast
<DbgVariableRecord
>(&*Branch
->getDbgRecordRange().begin());
384 DbgVariableRecord
*DVR2
=
385 cast
<DbgVariableRecord
>(&*Ret
->getDbgRecordRange().begin());
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
));
394 } // end anonymous namespace