blob: 90466b2505ecd40fa0fa83430c7f42218b162427 [file] [log] [blame]
Benjamin Kramer71c1b222013-04-12 08:33:11 +00001//===- llvm/unittest/IR/ValueTest.cpp - Value unit tests ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruth3c0d6072017-06-06 11:06:56 +000010#include "llvm/IR/Value.h"
Chandler Carruthbc65a8d2014-01-07 12:34:26 +000011#include "llvm/AsmParser/Parser.h"
Benjamin Kramer71c1b222013-04-12 08:33:11 +000012#include "llvm/IR/Function.h"
13#include "llvm/IR/LLVMContext.h"
14#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +000015#include "llvm/IR/ModuleSlotTracker.h"
Benjamin Kramer71c1b222013-04-12 08:33:11 +000016#include "llvm/Support/SourceMgr.h"
17#include "gtest/gtest.h"
18using namespace llvm;
19
20namespace {
21
22TEST(ValueTest, UsedInBasicBlock) {
23 LLVMContext C;
24
25 const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
26 "bb0:\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"
35 " ret void\n"
36 "}\n";
37 SMDiagnostic Err;
Rafael Espindola9b29ff92014-08-19 16:58:54 +000038 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
Benjamin Kramer71c1b222013-04-12 08:33:11 +000039
40 Function *F = M->getFunction("f");
41
Duncan P. N. Exon Smith70f8c202015-10-20 18:30:20 +000042 EXPECT_FALSE(F->isUsedInBasicBlock(&F->front()));
Reid Kleckner1c19be82017-03-16 22:59:15 +000043 EXPECT_TRUE(std::next(F->arg_begin())->isUsedInBasicBlock(&F->front()));
Duncan P. N. Exon Smith70f8c202015-10-20 18:30:20 +000044 EXPECT_TRUE(F->arg_begin()->isUsedInBasicBlock(&F->front()));
Benjamin Kramer71c1b222013-04-12 08:33:11 +000045}
46
Matt Arsenault2ebcd572013-09-30 21:23:03 +000047TEST(GlobalTest, CreateAddressSpace) {
Mehdi Amini8be77072016-04-14 21:59:01 +000048 LLVMContext Ctx;
Ahmed Charlesf4ccd112014-03-06 05:51:42 +000049 std::unique_ptr<Module> M(new Module("TestModule", Ctx));
Matt Arsenault2ebcd572013-09-30 21:23:03 +000050 Type *Int8Ty = Type::getInt8Ty(Ctx);
51 Type *Int32Ty = Type::getInt32Ty(Ctx);
52
53 GlobalVariable *Dummy0
54 = new GlobalVariable(*M,
55 Int32Ty,
56 true,
57 GlobalValue::ExternalLinkage,
58 Constant::getAllOnesValue(Int32Ty),
59 "dummy",
Craig Topperb1770412014-06-08 22:29:17 +000060 nullptr,
Matt Arsenault2ebcd572013-09-30 21:23:03 +000061 GlobalVariable::NotThreadLocal,
62 1);
63
Rafael Espindola08aeb162014-10-23 14:45:19 +000064 EXPECT_TRUE(Value::MaximumAlignment == 536870912U);
65 Dummy0->setAlignment(536870912U);
66 EXPECT_EQ(Dummy0->getAlignment(), 536870912U);
67
Matt Arsenault2ebcd572013-09-30 21:23:03 +000068 // 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());
72
73
74 // This one requires a bitcast, but the address space must also stay the same.
75 GlobalVariable *DummyCast0
76 = new GlobalVariable(*M,
77 Int32Ty,
78 true,
79 GlobalValue::ExternalLinkage,
80 Constant::getAllOnesValue(Int32Ty),
81 "dummy_cast",
Craig Topperb1770412014-06-08 22:29:17 +000082 nullptr,
Matt Arsenault2ebcd572013-09-30 21:23:03 +000083 GlobalVariable::NotThreadLocal,
84 1);
85
86 // Make sure the address space isn't dropped when returning this.
87 Constant *DummyCast1 = M->getOrInsertGlobal("dummy_cast", Int8Ty);
88 EXPECT_EQ(1u, DummyCast1->getType()->getPointerAddressSpace());
89 EXPECT_NE(DummyCast0, DummyCast1) << *DummyCast1;
90}
Rafael Espindola08aeb162014-10-23 14:45:19 +000091
92#ifdef GTEST_HAS_DEATH_TEST
93#ifndef NDEBUG
94TEST(GlobalTest, AlignDeath) {
Mehdi Amini8be77072016-04-14 21:59:01 +000095 LLVMContext Ctx;
Rafael Espindola08aeb162014-10-23 14:45:19 +000096 std::unique_ptr<Module> M(new Module("TestModule", Ctx));
97 Type *Int32Ty = Type::getInt32Ty(Ctx);
98 GlobalVariable *Var =
99 new GlobalVariable(*M, Int32Ty, true, GlobalValue::ExternalLinkage,
100 Constant::getAllOnesValue(Int32Ty), "var", nullptr,
101 GlobalVariable::NotThreadLocal, 1);
102
103 EXPECT_DEATH(Var->setAlignment(536870913U), "Alignment is not a power of 2");
104 EXPECT_DEATH(Var->setAlignment(1073741824U),
105 "Alignment is greater than MaximumAlignment");
106}
107#endif
108#endif
109
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +0000110TEST(ValueTest, printSlots) {
111 // Check that Value::print() and Value::printAsOperand() work with and
112 // without a slot tracker.
113 LLVMContext C;
114
Roman Tereshin238a3382018-03-22 21:29:07 +0000115 const char *ModuleString = "@g0 = external global %500\n"
116 "@g1 = external global %900\n"
117 "\n"
118 "%900 = type { i32, i32 }\n"
119 "%500 = type { i32 }\n"
120 "\n"
121 "define void @f(i32 %x, i32 %y) {\n"
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +0000122 "entry:\n"
123 " %0 = add i32 %y, 1\n"
124 " %1 = add i32 %y, 1\n"
125 " ret void\n"
126 "}\n";
127 SMDiagnostic Err;
128 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
129
130 Function *F = M->getFunction("f");
131 ASSERT_TRUE(F);
132 ASSERT_FALSE(F->empty());
133 BasicBlock &BB = F->getEntryBlock();
134 ASSERT_EQ(3u, BB.size());
135
Duncan P. N. Exon Smith70f8c202015-10-20 18:30:20 +0000136 Instruction *I0 = &*BB.begin();
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +0000137 ASSERT_TRUE(I0);
Duncan P. N. Exon Smith70f8c202015-10-20 18:30:20 +0000138 Instruction *I1 = &*++BB.begin();
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +0000139 ASSERT_TRUE(I1);
140
Roman Tereshin238a3382018-03-22 21:29:07 +0000141 GlobalVariable *G0 = M->getGlobalVariable("g0");
142 ASSERT_TRUE(G0);
143 GlobalVariable *G1 = M->getGlobalVariable("g1");
144 ASSERT_TRUE(G1);
145
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +0000146 ModuleSlotTracker MST(M.get());
147
148#define CHECK_PRINT(INST, STR) \
149 do { \
150 { \
151 std::string S; \
152 raw_string_ostream OS(S); \
153 INST->print(OS); \
154 EXPECT_EQ(STR, OS.str()); \
155 } \
156 { \
157 std::string S; \
158 raw_string_ostream OS(S); \
159 INST->print(OS, MST); \
160 EXPECT_EQ(STR, OS.str()); \
161 } \
162 } while (false)
163 CHECK_PRINT(I0, " %0 = add i32 %y, 1");
164 CHECK_PRINT(I1, " %1 = add i32 %y, 1");
165#undef CHECK_PRINT
166
167#define CHECK_PRINT_AS_OPERAND(INST, TYPE, STR) \
168 do { \
169 { \
170 std::string S; \
171 raw_string_ostream OS(S); \
172 INST->printAsOperand(OS, TYPE); \
173 EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
174 } \
175 { \
176 std::string S; \
177 raw_string_ostream OS(S); \
178 INST->printAsOperand(OS, TYPE, MST); \
179 EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
180 } \
181 } while (false)
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");
Roman Tereshin238a3382018-03-22 21:29:07 +0000186 CHECK_PRINT_AS_OPERAND(G0, true, "%0* @g0");
187 CHECK_PRINT_AS_OPERAND(G1, true, "%1* @g1");
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +0000188#undef CHECK_PRINT_AS_OPERAND
189}
190
Alex Lorenz9adb2122015-07-27 22:31:04 +0000191TEST(ValueTest, getLocalSlots) {
192 // Verify that the getLocalSlot method returns the correct slot numbers.
193 LLVMContext C;
194 const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
195 "entry:\n"
196 " %0 = add i32 %y, 1\n"
197 " %1 = add i32 %y, 1\n"
198 " br label %2\n"
199 "\n"
200 " ret void\n"
201 "}\n";
202 SMDiagnostic Err;
203 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
204
205 Function *F = M->getFunction("f");
206 ASSERT_TRUE(F);
207 ASSERT_FALSE(F->empty());
208 BasicBlock &EntryBB = F->getEntryBlock();
209 ASSERT_EQ(3u, EntryBB.size());
Duncan P. N. Exon Smith70f8c202015-10-20 18:30:20 +0000210 BasicBlock *BB2 = &*++F->begin();
Alex Lorenz9adb2122015-07-27 22:31:04 +0000211 ASSERT_TRUE(BB2);
212
Duncan P. N. Exon Smith70f8c202015-10-20 18:30:20 +0000213 Instruction *I0 = &*EntryBB.begin();
Alex Lorenz9adb2122015-07-27 22:31:04 +0000214 ASSERT_TRUE(I0);
Duncan P. N. Exon Smith70f8c202015-10-20 18:30:20 +0000215 Instruction *I1 = &*++EntryBB.begin();
Alex Lorenz9adb2122015-07-27 22:31:04 +0000216 ASSERT_TRUE(I1);
217
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);
224}
225
226#if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
227TEST(ValueTest, getLocalSlotDeath) {
228 LLVMContext C;
229 const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
230 "entry:\n"
231 " %0 = add i32 %y, 1\n"
232 " %1 = add i32 %y, 1\n"
233 " br label %2\n"
234 "\n"
235 " ret void\n"
236 "}\n";
237 SMDiagnostic Err;
238 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
239
240 Function *F = M->getFunction("f");
241 ASSERT_TRUE(F);
242 ASSERT_FALSE(F->empty());
Duncan P. N. Exon Smith70f8c202015-10-20 18:30:20 +0000243 BasicBlock *BB2 = &*++F->begin();
Alex Lorenz9adb2122015-07-27 22:31:04 +0000244 ASSERT_TRUE(BB2);
245
246 ModuleSlotTracker MST(M.get());
247 EXPECT_DEATH(MST.getLocalSlot(BB2), "No function incorporated");
248}
249#endif
250
Benjamin Kramer71c1b222013-04-12 08:33:11 +0000251} // end anonymous namespace