ConstantsTest.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //===- llvm/unittest/IR/ConstantsTest.cpp - Constants 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/Constants.h"
  11. #include "llvm/IR/DerivedTypes.h"
  12. #include "llvm/IR/InstrTypes.h"
  13. #include "llvm/IR/Instruction.h"
  14. #include "llvm/IR/LLVMContext.h"
  15. #include "llvm/IR/Module.h"
  16. #include "llvm/Support/SourceMgr.h"
  17. #include "gtest/gtest.h"
  18. namespace llvm {
  19. namespace {
  20. TEST(ConstantsTest, Integer_i1) {
  21. IntegerType* Int1 = IntegerType::get(getGlobalContext(), 1);
  22. Constant* One = ConstantInt::get(Int1, 1, true);
  23. Constant* Zero = ConstantInt::get(Int1, 0);
  24. Constant* NegOne = ConstantInt::get(Int1, static_cast<uint64_t>(-1), true);
  25. EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1));
  26. Constant* Undef = UndefValue::get(Int1);
  27. // Input: @b = constant i1 add(i1 1 , i1 1)
  28. // Output: @b = constant i1 false
  29. EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One));
  30. // @c = constant i1 add(i1 -1, i1 1)
  31. // @c = constant i1 false
  32. EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One));
  33. // @d = constant i1 add(i1 -1, i1 -1)
  34. // @d = constant i1 false
  35. EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne));
  36. // @e = constant i1 sub(i1 -1, i1 1)
  37. // @e = constant i1 false
  38. EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One));
  39. // @f = constant i1 sub(i1 1 , i1 -1)
  40. // @f = constant i1 false
  41. EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne));
  42. // @g = constant i1 sub(i1 1 , i1 1)
  43. // @g = constant i1 false
  44. EXPECT_EQ(Zero, ConstantExpr::getSub(One, One));
  45. // @h = constant i1 shl(i1 1 , i1 1) ; undefined
  46. // @h = constant i1 undef
  47. EXPECT_EQ(Undef, ConstantExpr::getShl(One, One));
  48. // @i = constant i1 shl(i1 1 , i1 0)
  49. // @i = constant i1 true
  50. EXPECT_EQ(One, ConstantExpr::getShl(One, Zero));
  51. // @j = constant i1 lshr(i1 1, i1 1) ; undefined
  52. // @j = constant i1 undef
  53. EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One));
  54. // @m = constant i1 ashr(i1 1, i1 1) ; undefined
  55. // @m = constant i1 undef
  56. EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One));
  57. // @n = constant i1 mul(i1 -1, i1 1)
  58. // @n = constant i1 true
  59. EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One));
  60. // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
  61. // @o = constant i1 true
  62. EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One));
  63. // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
  64. // @p = constant i1 true
  65. EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne));
  66. // @q = constant i1 udiv(i1 -1, i1 1)
  67. // @q = constant i1 true
  68. EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One));
  69. // @r = constant i1 udiv(i1 1, i1 -1)
  70. // @r = constant i1 true
  71. EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne));
  72. // @s = constant i1 srem(i1 -1, i1 1) ; overflow
  73. // @s = constant i1 false
  74. EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One));
  75. // @t = constant i1 urem(i1 -1, i1 1)
  76. // @t = constant i1 false
  77. EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One));
  78. // @u = constant i1 srem(i1 1, i1 -1) ; overflow
  79. // @u = constant i1 false
  80. EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne));
  81. }
  82. TEST(ConstantsTest, IntSigns) {
  83. IntegerType* Int8Ty = Type::getInt8Ty(getGlobalContext());
  84. EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue());
  85. EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue());
  86. EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue());
  87. EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue());
  88. EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue());
  89. EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue());
  90. // Overflow is handled by truncation.
  91. EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
  92. }
  93. TEST(ConstantsTest, FP128Test) {
  94. Type *FP128Ty = Type::getFP128Ty(getGlobalContext());
  95. IntegerType *Int128Ty = Type::getIntNTy(getGlobalContext(), 128);
  96. Constant *Zero128 = Constant::getNullValue(Int128Ty);
  97. Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty);
  98. EXPECT_TRUE(isa<ConstantFP>(X));
  99. }
  100. TEST(ConstantsTest, PointerCast) {
  101. LLVMContext &C(getGlobalContext());
  102. Type *Int8PtrTy = Type::getInt8PtrTy(C);
  103. Type *Int32PtrTy = Type::getInt32PtrTy(C);
  104. Type *Int64Ty = Type::getInt64Ty(C);
  105. VectorType *Int8PtrVecTy = VectorType::get(Int8PtrTy, 4);
  106. VectorType *Int32PtrVecTy = VectorType::get(Int32PtrTy, 4);
  107. VectorType *Int64VecTy = VectorType::get(Int64Ty, 4);
  108. // ptrtoint i8* to i64
  109. EXPECT_EQ(Constant::getNullValue(Int64Ty),
  110. ConstantExpr::getPointerCast(
  111. Constant::getNullValue(Int8PtrTy), Int64Ty));
  112. // bitcast i8* to i32*
  113. EXPECT_EQ(Constant::getNullValue(Int32PtrTy),
  114. ConstantExpr::getPointerCast(
  115. Constant::getNullValue(Int8PtrTy), Int32PtrTy));
  116. // ptrtoint <4 x i8*> to <4 x i64>
  117. EXPECT_EQ(Constant::getNullValue(Int64VecTy),
  118. ConstantExpr::getPointerCast(
  119. Constant::getNullValue(Int8PtrVecTy), Int64VecTy));
  120. // bitcast <4 x i8*> to <4 x i32*>
  121. EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy),
  122. ConstantExpr::getPointerCast(
  123. Constant::getNullValue(Int8PtrVecTy), Int32PtrVecTy));
  124. }
  125. #define CHECK(x, y) { \
  126. std::string __s; \
  127. raw_string_ostream __o(__s); \
  128. Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction(); \
  129. __I->print(__o); \
  130. delete __I; \
  131. __o.flush(); \
  132. EXPECT_EQ(std::string(" <badref> = " y), __s); \
  133. }
  134. TEST(ConstantsTest, AsInstructionsTest) {
  135. std::unique_ptr<Module> M(new Module("MyModule", getGlobalContext()));
  136. Type *Int64Ty = Type::getInt64Ty(getGlobalContext());
  137. Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
  138. Type *Int16Ty = Type::getInt16Ty(getGlobalContext());
  139. Type *Int1Ty = Type::getInt1Ty(getGlobalContext());
  140. Type *FloatTy = Type::getFloatTy(getGlobalContext());
  141. Type *DoubleTy = Type::getDoubleTy(getGlobalContext());
  142. Constant *Global = M->getOrInsertGlobal("dummy",
  143. PointerType::getUnqual(Int32Ty));
  144. Constant *Global2 = M->getOrInsertGlobal("dummy2",
  145. PointerType::getUnqual(Int32Ty));
  146. Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty);
  147. Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy);
  148. Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy);
  149. Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty);
  150. Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty);
  151. Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy);
  152. Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2));
  153. Constant *One = ConstantInt::get(Int32Ty, 1);
  154. Constant *Two = ConstantInt::get(Int64Ty, 2);
  155. Constant *Big = ConstantInt::get(getGlobalContext(),
  156. APInt{256, uint64_t(-1), true});
  157. Constant *Elt = ConstantInt::get(Int16Ty, 2015);
  158. Constant *Undef16 = UndefValue::get(Int16Ty);
  159. Constant *Undef64 = UndefValue::get(Int64Ty);
  160. Constant *UndefV16 = UndefValue::get(P6->getType());
  161. #define P0STR "ptrtoint (i32** @dummy to i32)"
  162. #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
  163. #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)"
  164. #define P3STR "ptrtoint (i32** @dummy to i1)"
  165. #define P4STR "ptrtoint (i32** @dummy2 to i32)"
  166. #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)"
  167. #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)"
  168. CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR);
  169. CHECK(ConstantExpr::getFNeg(P1), "fsub float -0.000000e+00, " P1STR);
  170. CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1");
  171. CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR);
  172. CHECK(ConstantExpr::getAdd(P0, P0, false, true), "add nsw i32 " P0STR ", "
  173. P0STR);
  174. CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", "
  175. P0STR);
  176. CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR);
  177. CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR);
  178. CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR);
  179. CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR);
  180. CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR);
  181. CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR);
  182. CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR);
  183. CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR);
  184. CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR);
  185. CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR);
  186. CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR);
  187. CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR);
  188. CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR);
  189. CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR);
  190. CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR);
  191. CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR);
  192. CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", "
  193. P0STR);
  194. CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR);
  195. CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR);
  196. CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR);
  197. CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR);
  198. CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64");
  199. CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64");
  200. CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR
  201. " to float");
  202. CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR
  203. " to double");
  204. CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR);
  205. CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR
  206. ", i32 " P4STR);
  207. CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR
  208. ", " P4STR);
  209. CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float "
  210. P1STR ", " P5STR);
  211. std::vector<Constant*> V;
  212. V.push_back(One);
  213. // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
  214. // not a normal one!
  215. //CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
  216. // "getelementptr i32*, i32** @dummy, i32 1");
  217. CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty),
  218. Global, V),
  219. "getelementptr inbounds i32*, i32** @dummy, i32 1");
  220. CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> "
  221. P6STR ", i32 1");
  222. EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Two));
  223. EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Big));
  224. EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Undef64));
  225. EXPECT_EQ(Elt, ConstantExpr::getExtractElement(
  226. ConstantExpr::getInsertElement(P6, Elt, One), One));
  227. EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Two));
  228. EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Big));
  229. EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Undef64));
  230. }
  231. #ifdef GTEST_HAS_DEATH_TEST
  232. #ifndef NDEBUG
  233. TEST(ConstantsTest, ReplaceWithConstantTest) {
  234. std::unique_ptr<Module> M(new Module("MyModule", getGlobalContext()));
  235. Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
  236. Constant *One = ConstantInt::get(Int32Ty, 1);
  237. Constant *Global =
  238. M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty));
  239. Constant *GEP = ConstantExpr::getGetElementPtr(
  240. PointerType::getUnqual(Int32Ty), Global, One);
  241. EXPECT_DEATH(Global->replaceAllUsesWith(GEP),
  242. "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
  243. }
  244. #endif
  245. #endif
  246. #undef CHECK
  247. TEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
  248. LLVMContext Context;
  249. std::unique_ptr<Module> M(new Module("MyModule", Context));
  250. Type *IntTy = Type::getInt8Ty(Context);
  251. ArrayType *ArrayTy = ArrayType::get(IntTy, 2);
  252. Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0),
  253. ConstantInt::get(IntTy, 1)};
  254. Constant *A01 = ConstantArray::get(ArrayTy, A01Vals);
  255. Constant *Global = new GlobalVariable(*M, IntTy, false,
  256. GlobalValue::ExternalLinkage, nullptr);
  257. Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy);
  258. Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt};
  259. Constant *A0G = ConstantArray::get(ArrayTy, A0GVals);
  260. ASSERT_NE(A01, A0G);
  261. GlobalVariable *RefArray =
  262. new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
  263. ASSERT_EQ(A0G, RefArray->getInitializer());
  264. GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
  265. ASSERT_EQ(A01, RefArray->getInitializer());
  266. }
  267. TEST(ConstantsTest, ConstantExprReplaceWithConstant) {
  268. LLVMContext Context;
  269. std::unique_ptr<Module> M(new Module("MyModule", Context));
  270. Type *IntTy = Type::getInt8Ty(Context);
  271. Constant *G1 = new GlobalVariable(*M, IntTy, false,
  272. GlobalValue::ExternalLinkage, nullptr);
  273. Constant *G2 = new GlobalVariable(*M, IntTy, false,
  274. GlobalValue::ExternalLinkage, nullptr);
  275. ASSERT_NE(G1, G2);
  276. Constant *Int1 = ConstantExpr::getPtrToInt(G1, IntTy);
  277. Constant *Int2 = ConstantExpr::getPtrToInt(G2, IntTy);
  278. ASSERT_NE(Int1, Int2);
  279. GlobalVariable *Ref =
  280. new GlobalVariable(*M, IntTy, false, GlobalValue::ExternalLinkage, Int1);
  281. ASSERT_EQ(Int1, Ref->getInitializer());
  282. G1->replaceAllUsesWith(G2);
  283. ASSERT_EQ(Int2, Ref->getInitializer());
  284. }
  285. TEST(ConstantsTest, GEPReplaceWithConstant) {
  286. LLVMContext Context;
  287. std::unique_ptr<Module> M(new Module("MyModule", Context));
  288. Type *IntTy = Type::getInt32Ty(Context);
  289. auto *PtrTy = PointerType::get(IntTy, 0);
  290. auto *C1 = ConstantInt::get(IntTy, 1);
  291. auto *Placeholder = new GlobalVariable(
  292. *M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr);
  293. auto *GEP = ConstantExpr::getGetElementPtr(IntTy, Placeholder, C1);
  294. ASSERT_EQ(GEP->getOperand(0), Placeholder);
  295. auto *Ref =
  296. new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, GEP);
  297. ASSERT_EQ(GEP, Ref->getInitializer());
  298. auto *Global = new GlobalVariable(*M, PtrTy, false,
  299. GlobalValue::ExternalLinkage, nullptr);
  300. auto *Alias = GlobalAlias::create(PtrTy, GlobalValue::ExternalLinkage,
  301. "alias", Global, M.get());
  302. Placeholder->replaceAllUsesWith(Alias);
  303. ASSERT_EQ(GEP, Ref->getInitializer());
  304. ASSERT_EQ(GEP->getOperand(0), Alias);
  305. }
  306. TEST(ConstantsTest, AliasCAPI) {
  307. LLVMContext Context;
  308. SMDiagnostic Error;
  309. std::unique_ptr<Module> M =
  310. parseAssemblyString("@g = global i32 42", Error, Context);
  311. GlobalVariable *G = M->getGlobalVariable("g");
  312. Type *I16Ty = Type::getInt16Ty(Context);
  313. Type *I16PTy = PointerType::get(I16Ty, 0);
  314. Constant *Aliasee = ConstantExpr::getBitCast(G, I16PTy);
  315. LLVMValueRef AliasRef =
  316. LLVMAddAlias(wrap(M.get()), wrap(I16PTy), wrap(Aliasee), "a");
  317. ASSERT_EQ(unwrap<GlobalAlias>(AliasRef)->getAliasee(), Aliasee);
  318. }
  319. } // end anonymous namespace
  320. } // end namespace llvm