1 //===- llvm/unittest/IR/ValueTest.cpp - Value unit tests ------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 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" 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; 38 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C); 39 40 Function *F = M->getFunction("f"); 41 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())); 45 } 46 47 TEST(GlobalTest, CreateAddressSpace) { 48 LLVMContext Ctx; 49 std::unique_ptr<Module> M(new Module("TestModule", Ctx)); 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", 60 nullptr, 61 GlobalVariable::NotThreadLocal, 62 1); 63 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); 69 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()); 74 75 76 // This one requires a bitcast, but the address space must also stay the same. 77 GlobalVariable *DummyCast0 78 = new GlobalVariable(*M, 79 Int32Ty, 80 true, 81 GlobalValue::ExternalLinkage, 82 Constant::getAllOnesValue(Int32Ty), 83 "dummy_cast", 84 nullptr, 85 GlobalVariable::NotThreadLocal, 86 1); 87 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()); 92 } 93 94 #ifdef GTEST_HAS_DEATH_TEST 95 #ifndef NDEBUG 96 97 TEST(GlobalTest, AlignDeath) { 98 LLVMContext Ctx; 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); 105 106 EXPECT_DEATH(Var->setAlignment(Align(8589934592ULL)), 107 "Alignment is greater than MaximumAlignment"); 108 } 109 #endif 110 #endif 111 112 TEST(ValueTest, printSlots) { 113 // Check that Value::print() and Value::printAsOperand() work with and 114 // without a slot tracker. 115 LLVMContext C; 116 117 const char *ModuleString = "@g0 = external global %500\n" 118 "@g1 = external global %900\n" 119 "\n" 120 "%900 = type { i32, i32 }\n" 121 "%500 = type { i32 }\n" 122 "\n" 123 "define void @f(i32 %x, i32 %y) {\n" 124 "entry:\n" 125 " %0 = add i32 %y, 1\n" 126 " %1 = add i32 %y, 1\n" 127 " ret void\n" 128 "}\n"; 129 SMDiagnostic Err; 130 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C); 131 132 Function *F = M->getFunction("f"); 133 ASSERT_TRUE(F); 134 ASSERT_FALSE(F->empty()); 135 BasicBlock &BB = F->getEntryBlock(); 136 ASSERT_EQ(3u, BB.size()); 137 138 Instruction *I0 = &*BB.begin(); 139 ASSERT_TRUE(I0); 140 Instruction *I1 = &*++BB.begin(); 141 ASSERT_TRUE(I1); 142 143 GlobalVariable *G0 = M->getGlobalVariable("g0"); 144 ASSERT_TRUE(G0); 145 GlobalVariable *G1 = M->getGlobalVariable("g1"); 146 ASSERT_TRUE(G1); 147 148 ModuleSlotTracker MST(M.get()); 149 150 #define CHECK_PRINT(INST, STR) \ 151 do { \ 152 { \ 153 std::string S; \ 154 raw_string_ostream OS(S); \ 155 INST->print(OS); \ 156 EXPECT_EQ(STR, OS.str()); \ 157 } \ 158 { \ 159 std::string S; \ 160 raw_string_ostream OS(S); \ 161 INST->print(OS, MST); \ 162 EXPECT_EQ(STR, OS.str()); \ 163 } \ 164 } while (false) 165 CHECK_PRINT(I0, " %0 = add i32 %y, 1"); 166 CHECK_PRINT(I1, " %1 = add i32 %y, 1"); 167 #undef CHECK_PRINT 168 169 #define CHECK_PRINT_AS_OPERAND(INST, TYPE, STR) \ 170 do { \ 171 { \ 172 std::string S; \ 173 raw_string_ostream OS(S); \ 174 INST->printAsOperand(OS, TYPE); \ 175 EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \ 176 } \ 177 { \ 178 std::string S; \ 179 raw_string_ostream OS(S); \ 180 INST->printAsOperand(OS, TYPE, MST); \ 181 EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \ 182 } \ 183 } while (false) 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 191 } 192 193 TEST(ValueTest, getLocalSlots) { 194 // Verify that the getLocalSlot method returns the correct slot numbers. 195 LLVMContext C; 196 const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n" 197 "entry:\n" 198 " %0 = add i32 %y, 1\n" 199 " %1 = add i32 %y, 1\n" 200 " br label %2\n" 201 "\n" 202 " ret void\n" 203 "}\n"; 204 SMDiagnostic Err; 205 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C); 206 207 Function *F = M->getFunction("f"); 208 ASSERT_TRUE(F); 209 ASSERT_FALSE(F->empty()); 210 BasicBlock &EntryBB = F->getEntryBlock(); 211 ASSERT_EQ(3u, EntryBB.size()); 212 BasicBlock *BB2 = &*++F->begin(); 213 ASSERT_TRUE(BB2); 214 215 Instruction *I0 = &*EntryBB.begin(); 216 ASSERT_TRUE(I0); 217 Instruction *I1 = &*++EntryBB.begin(); 218 ASSERT_TRUE(I1); 219 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); 226 } 227 228 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) 229 TEST(ValueTest, getLocalSlotDeath) { 230 LLVMContext C; 231 const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n" 232 "entry:\n" 233 " %0 = add i32 %y, 1\n" 234 " %1 = add i32 %y, 1\n" 235 " br label %2\n" 236 "\n" 237 " ret void\n" 238 "}\n"; 239 SMDiagnostic Err; 240 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C); 241 242 Function *F = M->getFunction("f"); 243 ASSERT_TRUE(F); 244 ASSERT_FALSE(F->empty()); 245 BasicBlock *BB2 = &*++F->begin(); 246 ASSERT_TRUE(BB2); 247 248 ModuleSlotTracker MST(M.get()); 249 EXPECT_DEATH(MST.getLocalSlot(BB2), "No function incorporated"); 250 } 251 #endif 252 253 TEST(ValueTest, replaceUsesOutsideBlock) { 254 // Check that Value::replaceUsesOutsideBlock(New, BB) replaces uses outside 255 // BB, including dbg.* uses of MetadataAsValue(ValueAsMetadata(this)). 256 const auto *IR = R"( 257 define i32 @f() !dbg !6 { 258 entry: 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 264 265 exit: 266 call void @llvm.dbg.value(metadata i32 %a, metadata !11, metadata !DIExpression()), !dbg !16 267 ret i32 %a, !dbg !16 268 } 269 270 declare void @llvm.dbg.value(metadata, metadata, metadata) 271 272 !llvm.dbg.cu = !{!0} 273 !llvm.module.flags = !{!5} 274 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: "/") 277 !2 = !{} 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) 281 !8 = !{!9, !11} 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) 288 )"; 289 LLVMContext Ctx; 290 SMDiagnostic Err; 291 std::unique_ptr<Module> M = parseAssemblyString(IR, Err, Ctx); 292 if (!M) 293 Err.print("ValueTest", errs()); 294 295 auto GetNext = [](auto *I) { return &*++I->getIterator(); }; 296 297 Function *F = M->getFunction("f"); 298 // Entry. 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)); 304 // Exit. 305 BasicBlock *Exit = GetNext(Entry); 306 auto *ExitDbg = cast<DbgValueInst>(&Exit->front()); 307 Instruction *Ret = GetNext(ExitDbg); 308 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)); 316 } 317 } // end anonymous namespace 318