Home | History | Annotate | Download | only in IR
      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 
     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   Module *M = ParseAssemblyString(ModuleString, nullptr, 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 
     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   // 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());
     67 
     68 
     69   // This one requires a bitcast, but the address space must also stay the same.
     70   GlobalVariable *DummyCast0
     71     = new GlobalVariable(*M,
     72                          Int32Ty,
     73                          true,
     74                          GlobalValue::ExternalLinkage,
     75                          Constant::getAllOnesValue(Int32Ty),
     76                          "dummy_cast",
     77                          nullptr,
     78                          GlobalVariable::NotThreadLocal,
     79                          1);
     80 
     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;
     85 }
     86 } // end anonymous namespace
     87