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/SourceMgr.h"
17 #include "gtest/gtest.h"
22 TEST(ValueTest
, UsedInBasicBlock
) {
25 const char *ModuleString
= "define void @f(i32 %x, i32 %y) {\n"
27 " %y1 = add i32 %y, 1\n"
28 " %y2 = add i32 %y, 1\n"
29 " %y3 = add i32 %y, 1\n"
30 " %y4 = add i32 %y, 1\n"
31 " %y5 = add i32 %y, 1\n"
32 " %y6 = add i32 %y, 1\n"
33 " %y7 = add i32 %y, 1\n"
34 " %y8 = add i32 %x, 1\n"
38 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
40 Function
*F
= M
->getFunction("f");
42 EXPECT_FALSE(F
->isUsedInBasicBlock(&F
->front()));
43 EXPECT_TRUE(std::next(F
->arg_begin())->isUsedInBasicBlock(&F
->front()));
44 EXPECT_TRUE(F
->arg_begin()->isUsedInBasicBlock(&F
->front()));
47 TEST(GlobalTest
, CreateAddressSpace
) {
49 std::unique_ptr
<Module
> M(new Module("TestModule", Ctx
));
50 Type
*Int8Ty
= Type::getInt8Ty(Ctx
);
51 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
53 GlobalVariable
*Dummy0
54 = new GlobalVariable(*M
,
57 GlobalValue::ExternalLinkage
,
58 Constant::getAllOnesValue(Int32Ty
),
61 GlobalVariable::NotThreadLocal
,
64 const Align
kMaxAlignment(Value::MaximumAlignment
);
65 EXPECT_TRUE(kMaxAlignment
.value() == 4294967296ULL);
66 Dummy0
->setAlignment(kMaxAlignment
);
67 EXPECT_TRUE(Dummy0
->getAlign());
68 EXPECT_EQ(*Dummy0
->getAlign(), kMaxAlignment
);
70 // Make sure the address space isn't dropped when returning this.
71 Constant
*Dummy1
= M
->getOrInsertGlobal("dummy", Int32Ty
);
72 EXPECT_EQ(Dummy0
, Dummy1
);
73 EXPECT_EQ(1u, Dummy1
->getType()->getPointerAddressSpace());
76 // This one requires a bitcast, but the address space must also stay the same.
77 GlobalVariable
*DummyCast0
78 = new GlobalVariable(*M
,
81 GlobalValue::ExternalLinkage
,
82 Constant::getAllOnesValue(Int32Ty
),
85 GlobalVariable::NotThreadLocal
,
88 // Make sure the address space isn't dropped when returning this.
89 Constant
*DummyCast1
= M
->getOrInsertGlobal("dummy_cast", Int8Ty
);
90 EXPECT_EQ(DummyCast0
, DummyCast1
);
91 EXPECT_EQ(1u, DummyCast1
->getType()->getPointerAddressSpace());
94 #ifdef GTEST_HAS_DEATH_TEST
97 TEST(GlobalTest
, AlignDeath
) {
99 std::unique_ptr
<Module
> M(new Module("TestModule", Ctx
));
100 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
101 GlobalVariable
*Var
=
102 new GlobalVariable(*M
, Int32Ty
, true, GlobalValue::ExternalLinkage
,
103 Constant::getAllOnesValue(Int32Ty
), "var", nullptr,
104 GlobalVariable::NotThreadLocal
, 1);
106 EXPECT_DEATH(Var
->setAlignment(Align(8589934592ULL)),
107 "Alignment is greater than MaximumAlignment");
112 TEST(ValueTest
, printSlots
) {
113 // Check that Value::print() and Value::printAsOperand() work with and
114 // without a slot tracker.
117 const char *ModuleString
= "@g0 = external global %500\n"
118 "@g1 = external global %900\n"
120 "%900 = type { i32, i32 }\n"
121 "%500 = type { i32 }\n"
123 "define void @f(i32 %x, i32 %y) {\n"
125 " %0 = add i32 %y, 1\n"
126 " %1 = add i32 %y, 1\n"
130 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
132 Function
*F
= M
->getFunction("f");
134 ASSERT_FALSE(F
->empty());
135 BasicBlock
&BB
= F
->getEntryBlock();
136 ASSERT_EQ(3u, BB
.size());
138 Instruction
*I0
= &*BB
.begin();
140 Instruction
*I1
= &*++BB
.begin();
143 GlobalVariable
*G0
= M
->getGlobalVariable("g0");
145 GlobalVariable
*G1
= M
->getGlobalVariable("g1");
148 ModuleSlotTracker
MST(M
.get());
150 #define CHECK_PRINT(INST, STR) \
154 raw_string_ostream OS(S); \
156 EXPECT_EQ(STR, OS.str()); \
160 raw_string_ostream OS(S); \
161 INST->print(OS, MST); \
162 EXPECT_EQ(STR, OS.str()); \
165 CHECK_PRINT(I0
, " %0 = add i32 %y, 1");
166 CHECK_PRINT(I1
, " %1 = add i32 %y, 1");
169 #define CHECK_PRINT_AS_OPERAND(INST, TYPE, STR) \
173 raw_string_ostream OS(S); \
174 INST->printAsOperand(OS, TYPE); \
175 EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
179 raw_string_ostream OS(S); \
180 INST->printAsOperand(OS, TYPE, MST); \
181 EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
184 CHECK_PRINT_AS_OPERAND(I0
, false, "%0");
185 CHECK_PRINT_AS_OPERAND(I1
, false, "%1");
186 CHECK_PRINT_AS_OPERAND(I0
, true, "i32 %0");
187 CHECK_PRINT_AS_OPERAND(I1
, true, "i32 %1");
188 CHECK_PRINT_AS_OPERAND(G0
, true, "ptr @g0");
189 CHECK_PRINT_AS_OPERAND(G1
, true, "ptr @g1");
190 #undef CHECK_PRINT_AS_OPERAND
193 TEST(ValueTest
, getLocalSlots
) {
194 // Verify that the getLocalSlot method returns the correct slot numbers.
196 const char *ModuleString
= "define void @f(i32 %x, i32 %y) {\n"
198 " %0 = add i32 %y, 1\n"
199 " %1 = add i32 %y, 1\n"
205 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
207 Function
*F
= M
->getFunction("f");
209 ASSERT_FALSE(F
->empty());
210 BasicBlock
&EntryBB
= F
->getEntryBlock();
211 ASSERT_EQ(3u, EntryBB
.size());
212 BasicBlock
*BB2
= &*++F
->begin();
215 Instruction
*I0
= &*EntryBB
.begin();
217 Instruction
*I1
= &*++EntryBB
.begin();
220 ModuleSlotTracker
MST(M
.get());
221 MST
.incorporateFunction(*F
);
222 EXPECT_EQ(MST
.getLocalSlot(I0
), 0);
223 EXPECT_EQ(MST
.getLocalSlot(I1
), 1);
224 EXPECT_EQ(MST
.getLocalSlot(&EntryBB
), -1);
225 EXPECT_EQ(MST
.getLocalSlot(BB2
), 2);
228 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
229 TEST(ValueTest
, getLocalSlotDeath
) {
231 const char *ModuleString
= "define void @f(i32 %x, i32 %y) {\n"
233 " %0 = add i32 %y, 1\n"
234 " %1 = add i32 %y, 1\n"
240 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
242 Function
*F
= M
->getFunction("f");
244 ASSERT_FALSE(F
->empty());
245 BasicBlock
*BB2
= &*++F
->begin();
248 ModuleSlotTracker
MST(M
.get());
249 EXPECT_DEATH(MST
.getLocalSlot(BB2
), "No function incorporated");
253 TEST(ValueTest
, replaceUsesOutsideBlock
) {
254 // Check that Value::replaceUsesOutsideBlock(New, BB) replaces uses outside
255 // BB, including dbg.* uses of MetadataAsValue(ValueAsMetadata(this)).
257 define i32 @f() !dbg !6 {
259 %a = add i32 0, 1, !dbg !15
260 %b = add i32 0, 2, !dbg !15
261 %c = add i32 %a, 2, !dbg !15
262 call void @llvm.dbg.value(metadata i32 %a, metadata !9, metadata !DIExpression()), !dbg !15
263 br label %exit, !dbg !15
266 call void @llvm.dbg.value(metadata i32 %a, metadata !11, metadata !DIExpression()), !dbg !16
270 declare void @llvm.dbg.value(metadata, metadata, metadata)
273 !llvm.module.flags = !{!5}
275 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify
", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
276 !1 = !DIFile(filename: "test
.ll
", directory: "/")
278 !5 = !{i32 2, !"Debug Info Version
", i32 3}
279 !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)
280 !7 = !DISubroutineType(types: !2)
282 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
283 !10 = !DIBasicType(name: "ty32
", size: 32, encoding: DW_ATE_signed)
284 !11 = !DILocalVariable(name: "2", scope: !6, file: !1, line: 2, type: !12)
285 !12 = !DIBasicType(name: "ty64
", size: 64, encoding: DW_ATE_signed)
286 !15 = !DILocation(line: 1, column: 1, scope: !6)
287 !16 = !DILocation(line: 5, column: 1, scope: !6)
291 std::unique_ptr
<Module
> M
= parseAssemblyString(IR
, Err
, Ctx
);
293 Err
.print("ValueTest", errs());
295 auto GetNext
= [](auto *I
) { return &*++I
->getIterator(); };
297 Function
*F
= M
->getFunction("f");
299 BasicBlock
*Entry
= &F
->front();
300 Instruction
*A
= &Entry
->front();
301 Instruction
*B
= GetNext(A
);
302 Instruction
*C
= GetNext(B
);
303 auto *EntryDbg
= cast
<DbgValueInst
>(GetNext(C
));
305 BasicBlock
*Exit
= GetNext(Entry
);
306 auto *ExitDbg
= cast
<DbgValueInst
>(&Exit
->front());
307 Instruction
*Ret
= GetNext(ExitDbg
);
309 A
->replaceUsesOutsideBlock(B
, Entry
);
310 // These users are in Entry so shouldn't be changed.
311 ASSERT_TRUE(C
->getOperand(0) == cast
<Value
>(A
));
312 ASSERT_TRUE(EntryDbg
->getValue(0) == cast
<Value
>(A
));
313 // These users are outside Entry so should be changed.
314 ASSERT_TRUE(ExitDbg
->getValue(0) == cast
<Value
>(B
));
315 ASSERT_TRUE(Ret
->getOperand(0) == cast
<Value
>(B
));
317 } // end anonymous namespace