Benjamin Kramer | eee73f5 | 2013-04-12 08:33:11 +0000 | [diff] [blame] | 1 | //===- 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 Carruth | 9aca918 | 2014-01-07 12:34:26 +0000 | [diff] [blame] | 10 | #include "llvm/AsmParser/Parser.h" |
Benjamin Kramer | eee73f5 | 2013-04-12 08:33:11 +0000 | [diff] [blame] | 11 | #include "llvm/IR/Function.h" |
| 12 | #include "llvm/IR/LLVMContext.h" |
| 13 | #include "llvm/IR/Module.h" |
Duncan P. N. Exon Smith | 1f8a99a | 2015-06-27 00:38:26 +0000 | [diff] [blame] | 14 | #include "llvm/IR/ModuleSlotTracker.h" |
Benjamin Kramer | eee73f5 | 2013-04-12 08:33:11 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Value.h" |
| 16 | #include "llvm/Support/SourceMgr.h" |
| 17 | #include "gtest/gtest.h" |
| 18 | using namespace llvm; |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | TEST(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 Espindola | 11c07d7 | 2014-08-19 16:58:54 +0000 | [diff] [blame] | 38 | std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C); |
Benjamin Kramer | eee73f5 | 2013-04-12 08:33:11 +0000 | [diff] [blame] | 39 | |
| 40 | Function *F = M->getFunction("f"); |
| 41 | |
| 42 | EXPECT_FALSE(F->isUsedInBasicBlock(F->begin())); |
| 43 | EXPECT_TRUE((++F->arg_begin())->isUsedInBasicBlock(F->begin())); |
| 44 | EXPECT_TRUE(F->arg_begin()->isUsedInBasicBlock(F->begin())); |
| 45 | } |
| 46 | |
Matt Arsenault | 27e783e | 2013-09-30 21:23:03 +0000 | [diff] [blame] | 47 | TEST(GlobalTest, CreateAddressSpace) { |
| 48 | LLVMContext &Ctx = getGlobalContext(); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 49 | std::unique_ptr<Module> M(new Module("TestModule", Ctx)); |
Matt Arsenault | 27e783e | 2013-09-30 21:23:03 +0000 | [diff] [blame] | 50 | 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 Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 60 | nullptr, |
Matt Arsenault | 27e783e | 2013-09-30 21:23:03 +0000 | [diff] [blame] | 61 | GlobalVariable::NotThreadLocal, |
| 62 | 1); |
| 63 | |
Rafael Espindola | a492fe9 | 2014-10-23 14:45:19 +0000 | [diff] [blame] | 64 | EXPECT_TRUE(Value::MaximumAlignment == 536870912U); |
| 65 | Dummy0->setAlignment(536870912U); |
| 66 | EXPECT_EQ(Dummy0->getAlignment(), 536870912U); |
| 67 | |
Matt Arsenault | 27e783e | 2013-09-30 21:23:03 +0000 | [diff] [blame] | 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()); |
| 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 Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 82 | nullptr, |
Matt Arsenault | 27e783e | 2013-09-30 21:23:03 +0000 | [diff] [blame] | 83 | 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 Espindola | a492fe9 | 2014-10-23 14:45:19 +0000 | [diff] [blame] | 91 | |
| 92 | #ifdef GTEST_HAS_DEATH_TEST |
| 93 | #ifndef NDEBUG |
| 94 | TEST(GlobalTest, AlignDeath) { |
| 95 | LLVMContext &Ctx = getGlobalContext(); |
| 96 | 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 Smith | 1f8a99a | 2015-06-27 00:38:26 +0000 | [diff] [blame] | 110 | TEST(ValueTest, printSlots) { |
| 111 | // Check that Value::print() and Value::printAsOperand() work with and |
| 112 | // without a slot tracker. |
| 113 | LLVMContext C; |
| 114 | |
| 115 | const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n" |
| 116 | "entry:\n" |
| 117 | " %0 = add i32 %y, 1\n" |
| 118 | " %1 = add i32 %y, 1\n" |
| 119 | " ret void\n" |
| 120 | "}\n"; |
| 121 | SMDiagnostic Err; |
| 122 | std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C); |
| 123 | |
| 124 | Function *F = M->getFunction("f"); |
| 125 | ASSERT_TRUE(F); |
| 126 | ASSERT_FALSE(F->empty()); |
| 127 | BasicBlock &BB = F->getEntryBlock(); |
| 128 | ASSERT_EQ(3u, BB.size()); |
| 129 | |
| 130 | Instruction *I0 = BB.begin(); |
| 131 | ASSERT_TRUE(I0); |
| 132 | Instruction *I1 = ++BB.begin(); |
| 133 | ASSERT_TRUE(I1); |
| 134 | |
| 135 | ModuleSlotTracker MST(M.get()); |
| 136 | |
| 137 | #define CHECK_PRINT(INST, STR) \ |
| 138 | do { \ |
| 139 | { \ |
| 140 | std::string S; \ |
| 141 | raw_string_ostream OS(S); \ |
| 142 | INST->print(OS); \ |
| 143 | EXPECT_EQ(STR, OS.str()); \ |
| 144 | } \ |
| 145 | { \ |
| 146 | std::string S; \ |
| 147 | raw_string_ostream OS(S); \ |
| 148 | INST->print(OS, MST); \ |
| 149 | EXPECT_EQ(STR, OS.str()); \ |
| 150 | } \ |
| 151 | } while (false) |
| 152 | CHECK_PRINT(I0, " %0 = add i32 %y, 1"); |
| 153 | CHECK_PRINT(I1, " %1 = add i32 %y, 1"); |
| 154 | #undef CHECK_PRINT |
| 155 | |
| 156 | #define CHECK_PRINT_AS_OPERAND(INST, TYPE, STR) \ |
| 157 | do { \ |
| 158 | { \ |
| 159 | std::string S; \ |
| 160 | raw_string_ostream OS(S); \ |
| 161 | INST->printAsOperand(OS, TYPE); \ |
| 162 | EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \ |
| 163 | } \ |
| 164 | { \ |
| 165 | std::string S; \ |
| 166 | raw_string_ostream OS(S); \ |
| 167 | INST->printAsOperand(OS, TYPE, MST); \ |
| 168 | EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \ |
| 169 | } \ |
| 170 | } while (false) |
| 171 | CHECK_PRINT_AS_OPERAND(I0, false, "%0"); |
| 172 | CHECK_PRINT_AS_OPERAND(I1, false, "%1"); |
| 173 | CHECK_PRINT_AS_OPERAND(I0, true, "i32 %0"); |
| 174 | CHECK_PRINT_AS_OPERAND(I1, true, "i32 %1"); |
| 175 | #undef CHECK_PRINT_AS_OPERAND |
| 176 | } |
| 177 | |
Alex Lorenz | 991a624 | 2015-07-27 22:31:04 +0000 | [diff] [blame^] | 178 | TEST(ValueTest, getLocalSlots) { |
| 179 | // Verify that the getLocalSlot method returns the correct slot numbers. |
| 180 | LLVMContext C; |
| 181 | const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n" |
| 182 | "entry:\n" |
| 183 | " %0 = add i32 %y, 1\n" |
| 184 | " %1 = add i32 %y, 1\n" |
| 185 | " br label %2\n" |
| 186 | "\n" |
| 187 | " ret void\n" |
| 188 | "}\n"; |
| 189 | SMDiagnostic Err; |
| 190 | std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C); |
| 191 | |
| 192 | Function *F = M->getFunction("f"); |
| 193 | ASSERT_TRUE(F); |
| 194 | ASSERT_FALSE(F->empty()); |
| 195 | BasicBlock &EntryBB = F->getEntryBlock(); |
| 196 | ASSERT_EQ(3u, EntryBB.size()); |
| 197 | BasicBlock *BB2 = ++F->begin(); |
| 198 | ASSERT_TRUE(BB2); |
| 199 | |
| 200 | Instruction *I0 = EntryBB.begin(); |
| 201 | ASSERT_TRUE(I0); |
| 202 | Instruction *I1 = ++EntryBB.begin(); |
| 203 | ASSERT_TRUE(I1); |
| 204 | |
| 205 | ModuleSlotTracker MST(M.get()); |
| 206 | MST.incorporateFunction(*F); |
| 207 | EXPECT_EQ(MST.getLocalSlot(I0), 0); |
| 208 | EXPECT_EQ(MST.getLocalSlot(I1), 1); |
| 209 | EXPECT_EQ(MST.getLocalSlot(&EntryBB), -1); |
| 210 | EXPECT_EQ(MST.getLocalSlot(BB2), 2); |
| 211 | } |
| 212 | |
| 213 | #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) |
| 214 | TEST(ValueTest, getLocalSlotDeath) { |
| 215 | LLVMContext C; |
| 216 | const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n" |
| 217 | "entry:\n" |
| 218 | " %0 = add i32 %y, 1\n" |
| 219 | " %1 = add i32 %y, 1\n" |
| 220 | " br label %2\n" |
| 221 | "\n" |
| 222 | " ret void\n" |
| 223 | "}\n"; |
| 224 | SMDiagnostic Err; |
| 225 | std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C); |
| 226 | |
| 227 | Function *F = M->getFunction("f"); |
| 228 | ASSERT_TRUE(F); |
| 229 | ASSERT_FALSE(F->empty()); |
| 230 | BasicBlock *BB2 = ++F->begin(); |
| 231 | ASSERT_TRUE(BB2); |
| 232 | |
| 233 | ModuleSlotTracker MST(M.get()); |
| 234 | EXPECT_DEATH(MST.getLocalSlot(BB2), "No function incorporated"); |
| 235 | } |
| 236 | #endif |
| 237 | |
Benjamin Kramer | eee73f5 | 2013-04-12 08:33:11 +0000 | [diff] [blame] | 238 | } // end anonymous namespace |