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 EXPECT_TRUE(Value::MaximumAlignment
== 4294967296ULL);
65 Dummy0
->setAlignment(Align(4294967296ULL));
66 EXPECT_EQ(Dummy0
->getAlignment(), 4294967296ULL);
68 // Make sure the address space isn't dropped when returning this.
69 Constant
*Dummy1
= M
->getOrInsertGlobal("dummy", Int32Ty
);
70 EXPECT_EQ(Dummy0
, Dummy1
);
71 EXPECT_EQ(1u, Dummy1
->getType()->getPointerAddressSpace());
74 // This one requires a bitcast, but the address space must also stay the same.
75 GlobalVariable
*DummyCast0
76 = new GlobalVariable(*M
,
79 GlobalValue::ExternalLinkage
,
80 Constant::getAllOnesValue(Int32Ty
),
83 GlobalVariable::NotThreadLocal
,
86 // Make sure the address space isn't dropped when returning this.
87 Constant
*DummyCast1
= M
->getOrInsertGlobal("dummy_cast", Int8Ty
);
88 EXPECT_EQ(DummyCast0
, DummyCast1
);
89 EXPECT_EQ(1u, DummyCast1
->getType()->getPointerAddressSpace());
92 #ifdef GTEST_HAS_DEATH_TEST
95 TEST(GlobalTest
, AlignDeath
) {
97 std::unique_ptr
<Module
> M(new Module("TestModule", Ctx
));
98 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
100 new GlobalVariable(*M
, Int32Ty
, true, GlobalValue::ExternalLinkage
,
101 Constant::getAllOnesValue(Int32Ty
), "var", nullptr,
102 GlobalVariable::NotThreadLocal
, 1);
104 EXPECT_DEATH(Var
->setAlignment(Align(8589934592ULL)),
105 "Alignment is greater than MaximumAlignment");
110 TEST(ValueTest
, printSlots
) {
111 // Check that Value::print() and Value::printAsOperand() work with and
112 // without a slot tracker.
115 const char *ModuleString
= "@g0 = external global %500\n"
116 "@g1 = external global %900\n"
118 "%900 = type { i32, i32 }\n"
119 "%500 = type { i32 }\n"
121 "define void @f(i32 %x, i32 %y) {\n"
123 " %0 = add i32 %y, 1\n"
124 " %1 = add i32 %y, 1\n"
128 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
130 Function
*F
= M
->getFunction("f");
132 ASSERT_FALSE(F
->empty());
133 BasicBlock
&BB
= F
->getEntryBlock();
134 ASSERT_EQ(3u, BB
.size());
136 Instruction
*I0
= &*BB
.begin();
138 Instruction
*I1
= &*++BB
.begin();
141 GlobalVariable
*G0
= M
->getGlobalVariable("g0");
143 GlobalVariable
*G1
= M
->getGlobalVariable("g1");
146 ModuleSlotTracker
MST(M
.get());
148 #define CHECK_PRINT(INST, STR) \
152 raw_string_ostream OS(S); \
154 EXPECT_EQ(STR, OS.str()); \
158 raw_string_ostream OS(S); \
159 INST->print(OS, MST); \
160 EXPECT_EQ(STR, OS.str()); \
163 CHECK_PRINT(I0
, " %0 = add i32 %y, 1");
164 CHECK_PRINT(I1
, " %1 = add i32 %y, 1");
167 #define CHECK_PRINT_AS_OPERAND(INST, TYPE, STR) \
171 raw_string_ostream OS(S); \
172 INST->printAsOperand(OS, TYPE); \
173 EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
177 raw_string_ostream OS(S); \
178 INST->printAsOperand(OS, TYPE, MST); \
179 EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
182 CHECK_PRINT_AS_OPERAND(I0
, false, "%0");
183 CHECK_PRINT_AS_OPERAND(I1
, false, "%1");
184 CHECK_PRINT_AS_OPERAND(I0
, true, "i32 %0");
185 CHECK_PRINT_AS_OPERAND(I1
, true, "i32 %1");
186 CHECK_PRINT_AS_OPERAND(G0
, true, "ptr @g0");
187 CHECK_PRINT_AS_OPERAND(G1
, true, "ptr @g1");
188 #undef CHECK_PRINT_AS_OPERAND
191 TEST(ValueTest
, getLocalSlots
) {
192 // Verify that the getLocalSlot method returns the correct slot numbers.
194 const char *ModuleString
= "define void @f(i32 %x, i32 %y) {\n"
196 " %0 = add i32 %y, 1\n"
197 " %1 = add i32 %y, 1\n"
203 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
205 Function
*F
= M
->getFunction("f");
207 ASSERT_FALSE(F
->empty());
208 BasicBlock
&EntryBB
= F
->getEntryBlock();
209 ASSERT_EQ(3u, EntryBB
.size());
210 BasicBlock
*BB2
= &*++F
->begin();
213 Instruction
*I0
= &*EntryBB
.begin();
215 Instruction
*I1
= &*++EntryBB
.begin();
218 ModuleSlotTracker
MST(M
.get());
219 MST
.incorporateFunction(*F
);
220 EXPECT_EQ(MST
.getLocalSlot(I0
), 0);
221 EXPECT_EQ(MST
.getLocalSlot(I1
), 1);
222 EXPECT_EQ(MST
.getLocalSlot(&EntryBB
), -1);
223 EXPECT_EQ(MST
.getLocalSlot(BB2
), 2);
226 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
227 TEST(ValueTest
, getLocalSlotDeath
) {
229 const char *ModuleString
= "define void @f(i32 %x, i32 %y) {\n"
231 " %0 = add i32 %y, 1\n"
232 " %1 = add i32 %y, 1\n"
238 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
240 Function
*F
= M
->getFunction("f");
242 ASSERT_FALSE(F
->empty());
243 BasicBlock
*BB2
= &*++F
->begin();
246 ModuleSlotTracker
MST(M
.get());
247 EXPECT_DEATH(MST
.getLocalSlot(BB2
), "No function incorporated");
251 TEST(ValueTest
, replaceUsesOutsideBlock
) {
252 // Check that Value::replaceUsesOutsideBlock(New, BB) replaces uses outside
253 // BB, including dbg.* uses of MetadataAsValue(ValueAsMetadata(this)).
255 define i32 @f() !dbg !6 {
257 %a = add i32 0, 1, !dbg !15
258 %b = add i32 0, 2, !dbg !15
259 %c = add i32 %a, 2, !dbg !15
260 call void @llvm.dbg.value(metadata i32 %a, metadata !9, metadata !DIExpression()), !dbg !15
261 br label %exit, !dbg !15
264 call void @llvm.dbg.value(metadata i32 %a, metadata !11, metadata !DIExpression()), !dbg !16
268 declare void @llvm.dbg.value(metadata, metadata, metadata)
271 !llvm.module.flags = !{!5}
273 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify
", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
274 !1 = !DIFile(filename: "test
.ll
", directory: "/")
276 !5 = !{i32 2, !"Debug Info Version
", i32 3}
277 !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)
278 !7 = !DISubroutineType(types: !2)
280 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
281 !10 = !DIBasicType(name: "ty32
", size: 32, encoding: DW_ATE_signed)
282 !11 = !DILocalVariable(name: "2", scope: !6, file: !1, line: 2, type: !12)
283 !12 = !DIBasicType(name: "ty64
", size: 64, encoding: DW_ATE_signed)
284 !15 = !DILocation(line: 1, column: 1, scope: !6)
285 !16 = !DILocation(line: 5, column: 1, scope: !6)
289 std::unique_ptr
<Module
> M
= parseAssemblyString(IR
, Err
, Ctx
);
291 Err
.print("ValueTest", errs());
293 auto GetNext
= [](auto *I
) { return &*++I
->getIterator(); };
295 Function
*F
= M
->getFunction("f");
297 BasicBlock
*Entry
= &F
->front();
298 Instruction
*A
= &Entry
->front();
299 Instruction
*B
= GetNext(A
);
300 Instruction
*C
= GetNext(B
);
301 auto *EntryDbg
= cast
<DbgValueInst
>(GetNext(C
));
303 BasicBlock
*Exit
= GetNext(Entry
);
304 auto *ExitDbg
= cast
<DbgValueInst
>(&Exit
->front());
305 Instruction
*Ret
= GetNext(ExitDbg
);
307 A
->replaceUsesOutsideBlock(B
, Entry
);
308 // These users are in Entry so shouldn't be changed.
309 ASSERT_TRUE(C
->getOperand(0) == cast
<Value
>(A
));
310 ASSERT_TRUE(EntryDbg
->getValue(0) == cast
<Value
>(A
));
311 // These users are outside Entry so should be changed.
312 ASSERT_TRUE(ExitDbg
->getValue(0) == cast
<Value
>(B
));
313 ASSERT_TRUE(Ret
->getOperand(0) == cast
<Value
>(B
));
315 } // end anonymous namespace