1 //===- llvm/unittest/IR/ValueTest.cpp - Value unit tests ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/IR/Function.h"
12 #include "llvm/IR/LLVMContext.h"
13 #include "llvm/IR/Module.h"
14 #include "llvm/IR/Value.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "gtest/gtest.h"
21 TEST(ValueTest, UsedInBasicBlock) {
24 const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
26 " %y1 = add i32 %y, 1\n"
27 " %y2 = add i32 %y, 1\n"
28 " %y3 = add i32 %y, 1\n"
29 " %y4 = add i32 %y, 1\n"
30 " %y5 = add i32 %y, 1\n"
31 " %y6 = add i32 %y, 1\n"
32 " %y7 = add i32 %y, 1\n"
33 " %y8 = add i32 %x, 1\n"
37 Module *M = ParseAssemblyString(ModuleString, NULL, Err, C);
39 Function *F = M->getFunction("f");
41 EXPECT_FALSE(F->isUsedInBasicBlock(F->begin()));
42 EXPECT_TRUE((++F->arg_begin())->isUsedInBasicBlock(F->begin()));
43 EXPECT_TRUE(F->arg_begin()->isUsedInBasicBlock(F->begin()));
46 TEST(GlobalTest, CreateAddressSpace) {
47 LLVMContext &Ctx = getGlobalContext();
48 std::unique_ptr<Module> M(new Module("TestModule", Ctx));
49 Type *Int8Ty = Type::getInt8Ty(Ctx);
50 Type *Int32Ty = Type::getInt32Ty(Ctx);
52 GlobalVariable *Dummy0
53 = new GlobalVariable(*M,
56 GlobalValue::ExternalLinkage,
57 Constant::getAllOnesValue(Int32Ty),
60 GlobalVariable::NotThreadLocal,
63 // Make sure the address space isn't dropped when returning this.
64 Constant *Dummy1 = M->getOrInsertGlobal("dummy", Int32Ty);
65 EXPECT_EQ(Dummy0, Dummy1);
66 EXPECT_EQ(1u, Dummy1->getType()->getPointerAddressSpace());
69 // This one requires a bitcast, but the address space must also stay the same.
70 GlobalVariable *DummyCast0
71 = new GlobalVariable(*M,
74 GlobalValue::ExternalLinkage,
75 Constant::getAllOnesValue(Int32Ty),
78 GlobalVariable::NotThreadLocal,
81 // Make sure the address space isn't dropped when returning this.
82 Constant *DummyCast1 = M->getOrInsertGlobal("dummy_cast", Int8Ty);
83 EXPECT_EQ(1u, DummyCast1->getType()->getPointerAddressSpace());
84 EXPECT_NE(DummyCast0, DummyCast1) << *DummyCast1;
86 } // end anonymous namespace