blob: 4dd0c2c36cf0ac8a2a91c0b1562d7c17e28ec7a5 [file] [log] [blame]
Benjamin Kramereee73f52013-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 Carruth9aca9182014-01-07 12:34:26 +000010#include "llvm/AsmParser/Parser.h"
Benjamin Kramereee73f52013-04-12 08:33:11 +000011#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"
17using namespace llvm;
18
19namespace {
20
21TEST(ValueTest, UsedInBasicBlock) {
22 LLVMContext C;
23
24 const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
25 "bb0:\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"
34 " ret void\n"
35 "}\n";
36 SMDiagnostic Err;
Rafael Espindola11c07d72014-08-19 16:58:54 +000037 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
Benjamin Kramereee73f52013-04-12 08:33:11 +000038
39 Function *F = M->getFunction("f");
40
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()));
44}
45
Matt Arsenault27e783e2013-09-30 21:23:03 +000046TEST(GlobalTest, CreateAddressSpace) {
47 LLVMContext &Ctx = getGlobalContext();
Ahmed Charles56440fd2014-03-06 05:51:42 +000048 std::unique_ptr<Module> M(new Module("TestModule", Ctx));
Matt Arsenault27e783e2013-09-30 21:23:03 +000049 Type *Int8Ty = Type::getInt8Ty(Ctx);
50 Type *Int32Ty = Type::getInt32Ty(Ctx);
51
52 GlobalVariable *Dummy0
53 = new GlobalVariable(*M,
54 Int32Ty,
55 true,
56 GlobalValue::ExternalLinkage,
57 Constant::getAllOnesValue(Int32Ty),
58 "dummy",
Craig Topper66f09ad2014-06-08 22:29:17 +000059 nullptr,
Matt Arsenault27e783e2013-09-30 21:23:03 +000060 GlobalVariable::NotThreadLocal,
61 1);
62
Rafael Espindolaa492fe92014-10-23 14:45:19 +000063 EXPECT_TRUE(Value::MaximumAlignment == 536870912U);
64 Dummy0->setAlignment(536870912U);
65 EXPECT_EQ(Dummy0->getAlignment(), 536870912U);
66
Matt Arsenault27e783e2013-09-30 21:23:03 +000067 // Make sure the address space isn't dropped when returning this.
68 Constant *Dummy1 = M->getOrInsertGlobal("dummy", Int32Ty);
69 EXPECT_EQ(Dummy0, Dummy1);
70 EXPECT_EQ(1u, Dummy1->getType()->getPointerAddressSpace());
71
72
73 // This one requires a bitcast, but the address space must also stay the same.
74 GlobalVariable *DummyCast0
75 = new GlobalVariable(*M,
76 Int32Ty,
77 true,
78 GlobalValue::ExternalLinkage,
79 Constant::getAllOnesValue(Int32Ty),
80 "dummy_cast",
Craig Topper66f09ad2014-06-08 22:29:17 +000081 nullptr,
Matt Arsenault27e783e2013-09-30 21:23:03 +000082 GlobalVariable::NotThreadLocal,
83 1);
84
85 // Make sure the address space isn't dropped when returning this.
86 Constant *DummyCast1 = M->getOrInsertGlobal("dummy_cast", Int8Ty);
87 EXPECT_EQ(1u, DummyCast1->getType()->getPointerAddressSpace());
88 EXPECT_NE(DummyCast0, DummyCast1) << *DummyCast1;
89}
Rafael Espindolaa492fe92014-10-23 14:45:19 +000090
91#ifdef GTEST_HAS_DEATH_TEST
92#ifndef NDEBUG
93TEST(GlobalTest, AlignDeath) {
94 LLVMContext &Ctx = getGlobalContext();
95 std::unique_ptr<Module> M(new Module("TestModule", Ctx));
96 Type *Int32Ty = Type::getInt32Ty(Ctx);
97 GlobalVariable *Var =
98 new GlobalVariable(*M, Int32Ty, true, GlobalValue::ExternalLinkage,
99 Constant::getAllOnesValue(Int32Ty), "var", nullptr,
100 GlobalVariable::NotThreadLocal, 1);
101
102 EXPECT_DEATH(Var->setAlignment(536870913U), "Alignment is not a power of 2");
103 EXPECT_DEATH(Var->setAlignment(1073741824U),
104 "Alignment is greater than MaximumAlignment");
105}
106#endif
107#endif
108
Benjamin Kramereee73f52013-04-12 08:33:11 +0000109} // end anonymous namespace