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 
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"
17 using namespace llvm;
18 
19 namespace {
20 
TEST(ValueTest,UsedInBasicBlock)21 TEST(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;
37   std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
38 
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 
TEST(GlobalTest,CreateAddressSpace)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);
51 
52   GlobalVariable *Dummy0
53     = new GlobalVariable(*M,
54                          Int32Ty,
55                          true,
56                          GlobalValue::ExternalLinkage,
57                          Constant::getAllOnesValue(Int32Ty),
58                          "dummy",
59                          nullptr,
60                          GlobalVariable::NotThreadLocal,
61                          1);
62 
63   EXPECT_TRUE(Value::MaximumAlignment == 536870912U);
64   Dummy0->setAlignment(536870912U);
65   EXPECT_EQ(Dummy0->getAlignment(), 536870912U);
66 
67   // 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",
81                          nullptr,
82                          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 }
90 
91 #ifdef GTEST_HAS_DEATH_TEST
92 #ifndef NDEBUG
TEST(GlobalTest,AlignDeath)93 TEST(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 
109 } // end anonymous namespace
110