ValueTest.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //===- llvm/unittest/IR/ValueTest.cpp - Value unit tests ------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/IR/Value.h"
  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/Support/SourceMgr.h"
  15. #include "gtest/gtest.h"
  16. using namespace llvm;
  17. namespace {
  18. TEST(ValueTest, UsedInBasicBlock) {
  19. LLVMContext C;
  20. const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
  21. "bb0:\n"
  22. " %y1 = add i32 %y, 1\n"
  23. " %y2 = add i32 %y, 1\n"
  24. " %y3 = add i32 %y, 1\n"
  25. " %y4 = add i32 %y, 1\n"
  26. " %y5 = add i32 %y, 1\n"
  27. " %y6 = add i32 %y, 1\n"
  28. " %y7 = add i32 %y, 1\n"
  29. " %y8 = add i32 %x, 1\n"
  30. " ret void\n"
  31. "}\n";
  32. SMDiagnostic Err;
  33. std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
  34. Function *F = M->getFunction("f");
  35. EXPECT_FALSE(F->isUsedInBasicBlock(&F->front()));
  36. EXPECT_TRUE(std::next(F->arg_begin())->isUsedInBasicBlock(&F->front()));
  37. EXPECT_TRUE(F->arg_begin()->isUsedInBasicBlock(&F->front()));
  38. }
  39. TEST(GlobalTest, CreateAddressSpace) {
  40. LLVMContext Ctx;
  41. std::unique_ptr<Module> M(new Module("TestModule", Ctx));
  42. Type *Int8Ty = Type::getInt8Ty(Ctx);
  43. Type *Int32Ty = Type::getInt32Ty(Ctx);
  44. GlobalVariable *Dummy0
  45. = new GlobalVariable(*M,
  46. Int32Ty,
  47. true,
  48. GlobalValue::ExternalLinkage,
  49. Constant::getAllOnesValue(Int32Ty),
  50. "dummy",
  51. nullptr,
  52. GlobalVariable::NotThreadLocal,
  53. 1);
  54. EXPECT_TRUE(Value::MaximumAlignment == 536870912U);
  55. Dummy0->setAlignment(Align(536870912));
  56. EXPECT_EQ(Dummy0->getAlignment(), 536870912U);
  57. // Make sure the address space isn't dropped when returning this.
  58. Constant *Dummy1 = M->getOrInsertGlobal("dummy", Int32Ty);
  59. EXPECT_EQ(Dummy0, Dummy1);
  60. EXPECT_EQ(1u, Dummy1->getType()->getPointerAddressSpace());
  61. // This one requires a bitcast, but the address space must also stay the same.
  62. GlobalVariable *DummyCast0
  63. = new GlobalVariable(*M,
  64. Int32Ty,
  65. true,
  66. GlobalValue::ExternalLinkage,
  67. Constant::getAllOnesValue(Int32Ty),
  68. "dummy_cast",
  69. nullptr,
  70. GlobalVariable::NotThreadLocal,
  71. 1);
  72. // Make sure the address space isn't dropped when returning this.
  73. Constant *DummyCast1 = M->getOrInsertGlobal("dummy_cast", Int8Ty);
  74. EXPECT_EQ(1u, DummyCast1->getType()->getPointerAddressSpace());
  75. EXPECT_NE(DummyCast0, DummyCast1) << *DummyCast1;
  76. }
  77. #ifdef GTEST_HAS_DEATH_TEST
  78. #ifndef NDEBUG
  79. TEST(GlobalTest, AlignDeath) {
  80. LLVMContext Ctx;
  81. std::unique_ptr<Module> M(new Module("TestModule", Ctx));
  82. Type *Int32Ty = Type::getInt32Ty(Ctx);
  83. GlobalVariable *Var =
  84. new GlobalVariable(*M, Int32Ty, true, GlobalValue::ExternalLinkage,
  85. Constant::getAllOnesValue(Int32Ty), "var", nullptr,
  86. GlobalVariable::NotThreadLocal, 1);
  87. EXPECT_DEATH(Var->setAlignment(Align(1073741824U)),
  88. "Alignment is greater than MaximumAlignment");
  89. }
  90. #endif
  91. #endif
  92. TEST(ValueTest, printSlots) {
  93. // Check that Value::print() and Value::printAsOperand() work with and
  94. // without a slot tracker.
  95. LLVMContext C;
  96. const char *ModuleString = "@g0 = external global %500\n"
  97. "@g1 = external global %900\n"
  98. "\n"
  99. "%900 = type { i32, i32 }\n"
  100. "%500 = type { i32 }\n"
  101. "\n"
  102. "define void @f(i32 %x, i32 %y) {\n"
  103. "entry:\n"
  104. " %0 = add i32 %y, 1\n"
  105. " %1 = add i32 %y, 1\n"
  106. " ret void\n"
  107. "}\n";
  108. SMDiagnostic Err;
  109. std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
  110. Function *F = M->getFunction("f");
  111. ASSERT_TRUE(F);
  112. ASSERT_FALSE(F->empty());
  113. BasicBlock &BB = F->getEntryBlock();
  114. ASSERT_EQ(3u, BB.size());
  115. Instruction *I0 = &*BB.begin();
  116. ASSERT_TRUE(I0);
  117. Instruction *I1 = &*++BB.begin();
  118. ASSERT_TRUE(I1);
  119. GlobalVariable *G0 = M->getGlobalVariable("g0");
  120. ASSERT_TRUE(G0);
  121. GlobalVariable *G1 = M->getGlobalVariable("g1");
  122. ASSERT_TRUE(G1);
  123. ModuleSlotTracker MST(M.get());
  124. #define CHECK_PRINT(INST, STR) \
  125. do { \
  126. { \
  127. std::string S; \
  128. raw_string_ostream OS(S); \
  129. INST->print(OS); \
  130. EXPECT_EQ(STR, OS.str()); \
  131. } \
  132. { \
  133. std::string S; \
  134. raw_string_ostream OS(S); \
  135. INST->print(OS, MST); \
  136. EXPECT_EQ(STR, OS.str()); \
  137. } \
  138. } while (false)
  139. CHECK_PRINT(I0, " %0 = add i32 %y, 1");
  140. CHECK_PRINT(I1, " %1 = add i32 %y, 1");
  141. #undef CHECK_PRINT
  142. #define CHECK_PRINT_AS_OPERAND(INST, TYPE, STR) \
  143. do { \
  144. { \
  145. std::string S; \
  146. raw_string_ostream OS(S); \
  147. INST->printAsOperand(OS, TYPE); \
  148. EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
  149. } \
  150. { \
  151. std::string S; \
  152. raw_string_ostream OS(S); \
  153. INST->printAsOperand(OS, TYPE, MST); \
  154. EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
  155. } \
  156. } while (false)
  157. CHECK_PRINT_AS_OPERAND(I0, false, "%0");
  158. CHECK_PRINT_AS_OPERAND(I1, false, "%1");
  159. CHECK_PRINT_AS_OPERAND(I0, true, "i32 %0");
  160. CHECK_PRINT_AS_OPERAND(I1, true, "i32 %1");
  161. CHECK_PRINT_AS_OPERAND(G0, true, "%0* @g0");
  162. CHECK_PRINT_AS_OPERAND(G1, true, "%1* @g1");
  163. #undef CHECK_PRINT_AS_OPERAND
  164. }
  165. TEST(ValueTest, getLocalSlots) {
  166. // Verify that the getLocalSlot method returns the correct slot numbers.
  167. LLVMContext C;
  168. const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
  169. "entry:\n"
  170. " %0 = add i32 %y, 1\n"
  171. " %1 = add i32 %y, 1\n"
  172. " br label %2\n"
  173. "\n"
  174. " ret void\n"
  175. "}\n";
  176. SMDiagnostic Err;
  177. std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
  178. Function *F = M->getFunction("f");
  179. ASSERT_TRUE(F);
  180. ASSERT_FALSE(F->empty());
  181. BasicBlock &EntryBB = F->getEntryBlock();
  182. ASSERT_EQ(3u, EntryBB.size());
  183. BasicBlock *BB2 = &*++F->begin();
  184. ASSERT_TRUE(BB2);
  185. Instruction *I0 = &*EntryBB.begin();
  186. ASSERT_TRUE(I0);
  187. Instruction *I1 = &*++EntryBB.begin();
  188. ASSERT_TRUE(I1);
  189. ModuleSlotTracker MST(M.get());
  190. MST.incorporateFunction(*F);
  191. EXPECT_EQ(MST.getLocalSlot(I0), 0);
  192. EXPECT_EQ(MST.getLocalSlot(I1), 1);
  193. EXPECT_EQ(MST.getLocalSlot(&EntryBB), -1);
  194. EXPECT_EQ(MST.getLocalSlot(BB2), 2);
  195. }
  196. #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
  197. TEST(ValueTest, getLocalSlotDeath) {
  198. LLVMContext C;
  199. const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
  200. "entry:\n"
  201. " %0 = add i32 %y, 1\n"
  202. " %1 = add i32 %y, 1\n"
  203. " br label %2\n"
  204. "\n"
  205. " ret void\n"
  206. "}\n";
  207. SMDiagnostic Err;
  208. std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
  209. Function *F = M->getFunction("f");
  210. ASSERT_TRUE(F);
  211. ASSERT_FALSE(F->empty());
  212. BasicBlock *BB2 = &*++F->begin();
  213. ASSERT_TRUE(BB2);
  214. ModuleSlotTracker MST(M.get());
  215. EXPECT_DEATH(MST.getLocalSlot(BB2), "No function incorporated");
  216. }
  217. #endif
  218. } // end anonymous namespace