ValueTest.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #include "llvm/AsmParser/Parser.h"
  10. #include "llvm/IR/Function.h"
  11. #include "llvm/IR/LLVMContext.h"
  12. #include "llvm/IR/Module.h"
  13. #include "llvm/IR/ModuleSlotTracker.h"
  14. #include "llvm/IR/Value.h"
  15. #include "llvm/Support/SourceMgr.h"
  16. #include "gtest/gtest.h"
  17. using namespace llvm;
  18. namespace {
  19. TEST(ValueTest, UsedInBasicBlock) {
  20. LLVMContext C;
  21. const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
  22. "bb0:\n"
  23. " %y1 = add i32 %y, 1\n"
  24. " %y2 = add i32 %y, 1\n"
  25. " %y3 = add i32 %y, 1\n"
  26. " %y4 = add i32 %y, 1\n"
  27. " %y5 = add i32 %y, 1\n"
  28. " %y6 = add i32 %y, 1\n"
  29. " %y7 = add i32 %y, 1\n"
  30. " %y8 = add i32 %x, 1\n"
  31. " ret void\n"
  32. "}\n";
  33. SMDiagnostic Err;
  34. std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
  35. Function *F = M->getFunction("f");
  36. EXPECT_FALSE(F->isUsedInBasicBlock(F->begin()));
  37. EXPECT_TRUE((++F->arg_begin())->isUsedInBasicBlock(F->begin()));
  38. EXPECT_TRUE(F->arg_begin()->isUsedInBasicBlock(F->begin()));
  39. }
  40. TEST(GlobalTest, CreateAddressSpace) {
  41. LLVMContext &Ctx = getGlobalContext();
  42. std::unique_ptr<Module> M(new Module("TestModule", Ctx));
  43. Type *Int8Ty = Type::getInt8Ty(Ctx);
  44. Type *Int32Ty = Type::getInt32Ty(Ctx);
  45. GlobalVariable *Dummy0
  46. = new GlobalVariable(*M,
  47. Int32Ty,
  48. true,
  49. GlobalValue::ExternalLinkage,
  50. Constant::getAllOnesValue(Int32Ty),
  51. "dummy",
  52. nullptr,
  53. GlobalVariable::NotThreadLocal,
  54. 1);
  55. EXPECT_TRUE(Value::MaximumAlignment == 536870912U);
  56. Dummy0->setAlignment(536870912U);
  57. EXPECT_EQ(Dummy0->getAlignment(), 536870912U);
  58. // Make sure the address space isn't dropped when returning this.
  59. Constant *Dummy1 = M->getOrInsertGlobal("dummy", Int32Ty);
  60. EXPECT_EQ(Dummy0, Dummy1);
  61. EXPECT_EQ(1u, Dummy1->getType()->getPointerAddressSpace());
  62. // This one requires a bitcast, but the address space must also stay the same.
  63. GlobalVariable *DummyCast0
  64. = new GlobalVariable(*M,
  65. Int32Ty,
  66. true,
  67. GlobalValue::ExternalLinkage,
  68. Constant::getAllOnesValue(Int32Ty),
  69. "dummy_cast",
  70. nullptr,
  71. GlobalVariable::NotThreadLocal,
  72. 1);
  73. // Make sure the address space isn't dropped when returning this.
  74. Constant *DummyCast1 = M->getOrInsertGlobal("dummy_cast", Int8Ty);
  75. EXPECT_EQ(1u, DummyCast1->getType()->getPointerAddressSpace());
  76. EXPECT_NE(DummyCast0, DummyCast1) << *DummyCast1;
  77. }
  78. #ifdef GTEST_HAS_DEATH_TEST
  79. #ifndef NDEBUG
  80. TEST(GlobalTest, AlignDeath) {
  81. LLVMContext &Ctx = getGlobalContext();
  82. std::unique_ptr<Module> M(new Module("TestModule", Ctx));
  83. Type *Int32Ty = Type::getInt32Ty(Ctx);
  84. GlobalVariable *Var =
  85. new GlobalVariable(*M, Int32Ty, true, GlobalValue::ExternalLinkage,
  86. Constant::getAllOnesValue(Int32Ty), "var", nullptr,
  87. GlobalVariable::NotThreadLocal, 1);
  88. EXPECT_DEATH(Var->setAlignment(536870913U), "Alignment is not a power of 2");
  89. EXPECT_DEATH(Var->setAlignment(1073741824U),
  90. "Alignment is greater than MaximumAlignment");
  91. }
  92. #endif
  93. #endif
  94. TEST(ValueTest, printSlots) {
  95. // Check that Value::print() and Value::printAsOperand() work with and
  96. // without a slot tracker.
  97. LLVMContext C;
  98. const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
  99. "entry:\n"
  100. " %0 = add i32 %y, 1\n"
  101. " %1 = add i32 %y, 1\n"
  102. " ret void\n"
  103. "}\n";
  104. SMDiagnostic Err;
  105. std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
  106. Function *F = M->getFunction("f");
  107. ASSERT_TRUE(F);
  108. ASSERT_FALSE(F->empty());
  109. BasicBlock &BB = F->getEntryBlock();
  110. ASSERT_EQ(3u, BB.size());
  111. Instruction *I0 = BB.begin();
  112. ASSERT_TRUE(I0);
  113. Instruction *I1 = ++BB.begin();
  114. ASSERT_TRUE(I1);
  115. ModuleSlotTracker MST(M.get());
  116. #define CHECK_PRINT(INST, STR) \
  117. do { \
  118. { \
  119. std::string S; \
  120. raw_string_ostream OS(S); \
  121. INST->print(OS); \
  122. EXPECT_EQ(STR, OS.str()); \
  123. } \
  124. { \
  125. std::string S; \
  126. raw_string_ostream OS(S); \
  127. INST->print(OS, MST); \
  128. EXPECT_EQ(STR, OS.str()); \
  129. } \
  130. } while (false)
  131. CHECK_PRINT(I0, " %0 = add i32 %y, 1");
  132. CHECK_PRINT(I1, " %1 = add i32 %y, 1");
  133. #undef CHECK_PRINT
  134. #define CHECK_PRINT_AS_OPERAND(INST, TYPE, STR) \
  135. do { \
  136. { \
  137. std::string S; \
  138. raw_string_ostream OS(S); \
  139. INST->printAsOperand(OS, TYPE); \
  140. EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
  141. } \
  142. { \
  143. std::string S; \
  144. raw_string_ostream OS(S); \
  145. INST->printAsOperand(OS, TYPE, MST); \
  146. EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
  147. } \
  148. } while (false)
  149. CHECK_PRINT_AS_OPERAND(I0, false, "%0");
  150. CHECK_PRINT_AS_OPERAND(I1, false, "%1");
  151. CHECK_PRINT_AS_OPERAND(I0, true, "i32 %0");
  152. CHECK_PRINT_AS_OPERAND(I1, true, "i32 %1");
  153. #undef CHECK_PRINT_AS_OPERAND
  154. }
  155. } // end anonymous namespace