ConstantsTest.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. //===- llvm/unittest/IR/ConstantsTest.cpp - Constants 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/Constants.h"
  9. #include "llvm-c/Core.h"
  10. #include "llvm/AsmParser/Parser.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. LLVMContext Context;
  22. IntegerType *Int1 = IntegerType::get(Context, 1);
  23. Constant* One = ConstantInt::get(Int1, 1, true);
  24. Constant* Zero = ConstantInt::get(Int1, 0);
  25. Constant* NegOne = ConstantInt::get(Int1, static_cast<uint64_t>(-1), true);
  26. EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1));
  27. Constant* Undef = UndefValue::get(Int1);
  28. // Input: @b = constant i1 add(i1 1 , i1 1)
  29. // Output: @b = constant i1 false
  30. EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One));
  31. // @c = constant i1 add(i1 -1, i1 1)
  32. // @c = constant i1 false
  33. EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One));
  34. // @d = constant i1 add(i1 -1, i1 -1)
  35. // @d = constant i1 false
  36. EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne));
  37. // @e = constant i1 sub(i1 -1, i1 1)
  38. // @e = constant i1 false
  39. EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One));
  40. // @f = constant i1 sub(i1 1 , i1 -1)
  41. // @f = constant i1 false
  42. EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne));
  43. // @g = constant i1 sub(i1 1 , i1 1)
  44. // @g = constant i1 false
  45. EXPECT_EQ(Zero, ConstantExpr::getSub(One, One));
  46. // @h = constant i1 shl(i1 1 , i1 1) ; undefined
  47. // @h = constant i1 undef
  48. EXPECT_EQ(Undef, ConstantExpr::getShl(One, One));
  49. // @i = constant i1 shl(i1 1 , i1 0)
  50. // @i = constant i1 true
  51. EXPECT_EQ(One, ConstantExpr::getShl(One, Zero));
  52. // @j = constant i1 lshr(i1 1, i1 1) ; undefined
  53. // @j = constant i1 undef
  54. EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One));
  55. // @m = constant i1 ashr(i1 1, i1 1) ; undefined
  56. // @m = constant i1 undef
  57. EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One));
  58. // @n = constant i1 mul(i1 -1, i1 1)
  59. // @n = constant i1 true
  60. EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One));
  61. // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
  62. // @o = constant i1 true
  63. EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One));
  64. // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
  65. // @p = constant i1 true
  66. EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne));
  67. // @q = constant i1 udiv(i1 -1, i1 1)
  68. // @q = constant i1 true
  69. EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One));
  70. // @r = constant i1 udiv(i1 1, i1 -1)
  71. // @r = constant i1 true
  72. EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne));
  73. // @s = constant i1 srem(i1 -1, i1 1) ; overflow
  74. // @s = constant i1 false
  75. EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One));
  76. // @t = constant i1 urem(i1 -1, i1 1)
  77. // @t = constant i1 false
  78. EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One));
  79. // @u = constant i1 srem(i1 1, i1 -1) ; overflow
  80. // @u = constant i1 false
  81. EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne));
  82. }
  83. TEST(ConstantsTest, IntSigns) {
  84. LLVMContext Context;
  85. IntegerType *Int8Ty = Type::getInt8Ty(Context);
  86. EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue());
  87. EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue());
  88. EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue());
  89. EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue());
  90. EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue());
  91. EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue());
  92. // Overflow is handled by truncation.
  93. EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
  94. }
  95. TEST(ConstantsTest, FP128Test) {
  96. LLVMContext Context;
  97. Type *FP128Ty = Type::getFP128Ty(Context);
  98. IntegerType *Int128Ty = Type::getIntNTy(Context, 128);
  99. Constant *Zero128 = Constant::getNullValue(Int128Ty);
  100. Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty);
  101. EXPECT_TRUE(isa<ConstantFP>(X));
  102. }
  103. TEST(ConstantsTest, PointerCast) {
  104. LLVMContext C;
  105. Type *Int8PtrTy = Type::getInt8PtrTy(C);
  106. Type *Int32PtrTy = Type::getInt32PtrTy(C);
  107. Type *Int64Ty = Type::getInt64Ty(C);
  108. VectorType *Int8PtrVecTy = VectorType::get(Int8PtrTy, 4);
  109. VectorType *Int32PtrVecTy = VectorType::get(Int32PtrTy, 4);
  110. VectorType *Int64VecTy = VectorType::get(Int64Ty, 4);
  111. // ptrtoint i8* to i64
  112. EXPECT_EQ(Constant::getNullValue(Int64Ty),
  113. ConstantExpr::getPointerCast(
  114. Constant::getNullValue(Int8PtrTy), Int64Ty));
  115. // bitcast i8* to i32*
  116. EXPECT_EQ(Constant::getNullValue(Int32PtrTy),
  117. ConstantExpr::getPointerCast(
  118. Constant::getNullValue(Int8PtrTy), Int32PtrTy));
  119. // ptrtoint <4 x i8*> to <4 x i64>
  120. EXPECT_EQ(Constant::getNullValue(Int64VecTy),
  121. ConstantExpr::getPointerCast(
  122. Constant::getNullValue(Int8PtrVecTy), Int64VecTy));
  123. // bitcast <4 x i8*> to <4 x i32*>
  124. EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy),
  125. ConstantExpr::getPointerCast(
  126. Constant::getNullValue(Int8PtrVecTy), Int32PtrVecTy));
  127. Type *Int32Ptr1Ty = Type::getInt32PtrTy(C, 1);
  128. ConstantInt *K = ConstantInt::get(Type::getInt64Ty(C), 1234);
  129. // Make sure that addrspacecast of inttoptr is not folded away.
  130. EXPECT_NE(K,
  131. ConstantExpr::getAddrSpaceCast(
  132. ConstantExpr::getIntToPtr(K, Int32PtrTy), Int32Ptr1Ty));
  133. EXPECT_NE(K,
  134. ConstantExpr::getAddrSpaceCast(
  135. ConstantExpr::getIntToPtr(K, Int32Ptr1Ty), Int32PtrTy));
  136. Constant *NullInt32Ptr0 = Constant::getNullValue(Int32PtrTy);
  137. Constant *NullInt32Ptr1 = Constant::getNullValue(Int32Ptr1Ty);
  138. // Make sure that addrspacecast of null is not folded away.
  139. EXPECT_NE(Constant::getNullValue(Int32PtrTy),
  140. ConstantExpr::getAddrSpaceCast(NullInt32Ptr0, Int32Ptr1Ty));
  141. EXPECT_NE(Constant::getNullValue(Int32Ptr1Ty),
  142. ConstantExpr::getAddrSpaceCast(NullInt32Ptr1, Int32PtrTy));
  143. }
  144. #define CHECK(x, y) \
  145. { \
  146. std::string __s; \
  147. raw_string_ostream __o(__s); \
  148. Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction(); \
  149. __I->print(__o); \
  150. __I->deleteValue(); \
  151. __o.flush(); \
  152. EXPECT_EQ(std::string(" <badref> = " y), __s); \
  153. }
  154. TEST(ConstantsTest, AsInstructionsTest) {
  155. LLVMContext Context;
  156. std::unique_ptr<Module> M(new Module("MyModule", Context));
  157. Type *Int64Ty = Type::getInt64Ty(Context);
  158. Type *Int32Ty = Type::getInt32Ty(Context);
  159. Type *Int16Ty = Type::getInt16Ty(Context);
  160. Type *Int1Ty = Type::getInt1Ty(Context);
  161. Type *FloatTy = Type::getFloatTy(Context);
  162. Type *DoubleTy = Type::getDoubleTy(Context);
  163. Constant *Global = M->getOrInsertGlobal("dummy",
  164. PointerType::getUnqual(Int32Ty));
  165. Constant *Global2 = M->getOrInsertGlobal("dummy2",
  166. PointerType::getUnqual(Int32Ty));
  167. Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty);
  168. Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy);
  169. Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy);
  170. Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty);
  171. Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty);
  172. Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy);
  173. Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2));
  174. Constant *One = ConstantInt::get(Int32Ty, 1);
  175. Constant *Two = ConstantInt::get(Int64Ty, 2);
  176. Constant *Big = ConstantInt::get(Context, APInt{256, uint64_t(-1), true});
  177. Constant *Elt = ConstantInt::get(Int16Ty, 2015);
  178. Constant *Undef16 = UndefValue::get(Int16Ty);
  179. Constant *Undef64 = UndefValue::get(Int64Ty);
  180. Constant *UndefV16 = UndefValue::get(P6->getType());
  181. #define P0STR "ptrtoint (i32** @dummy to i32)"
  182. #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
  183. #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)"
  184. #define P3STR "ptrtoint (i32** @dummy to i1)"
  185. #define P4STR "ptrtoint (i32** @dummy2 to i32)"
  186. #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)"
  187. #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)"
  188. CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR);
  189. CHECK(ConstantExpr::getFNeg(P1), "fneg float " P1STR);
  190. CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1");
  191. CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR);
  192. CHECK(ConstantExpr::getAdd(P0, P0, false, true), "add nsw i32 " P0STR ", "
  193. P0STR);
  194. CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", "
  195. P0STR);
  196. CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR);
  197. CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR);
  198. CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR);
  199. CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR);
  200. CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR);
  201. CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR);
  202. CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR);
  203. CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR);
  204. CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR);
  205. CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR);
  206. CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR);
  207. CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR);
  208. CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR);
  209. CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR);
  210. CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR);
  211. CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR);
  212. CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", "
  213. P0STR);
  214. CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR);
  215. CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR);
  216. CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR);
  217. CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR);
  218. CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64");
  219. CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64");
  220. CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR
  221. " to float");
  222. CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR
  223. " to double");
  224. CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR);
  225. CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR
  226. ", i32 " P4STR);
  227. CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR
  228. ", " P4STR);
  229. CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float "
  230. P1STR ", " P5STR);
  231. std::vector<Constant*> V;
  232. V.push_back(One);
  233. // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
  234. // not a normal one!
  235. //CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
  236. // "getelementptr i32*, i32** @dummy, i32 1");
  237. CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty),
  238. Global, V),
  239. "getelementptr inbounds i32*, i32** @dummy, i32 1");
  240. CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> "
  241. P6STR ", i32 1");
  242. EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Two));
  243. EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Big));
  244. EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Undef64));
  245. EXPECT_EQ(Elt, ConstantExpr::getExtractElement(
  246. ConstantExpr::getInsertElement(P6, Elt, One), One));
  247. EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Two));
  248. EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Big));
  249. EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Undef64));
  250. }
  251. #ifdef GTEST_HAS_DEATH_TEST
  252. #ifndef NDEBUG
  253. TEST(ConstantsTest, ReplaceWithConstantTest) {
  254. LLVMContext Context;
  255. std::unique_ptr<Module> M(new Module("MyModule", Context));
  256. Type *Int32Ty = Type::getInt32Ty(Context);
  257. Constant *One = ConstantInt::get(Int32Ty, 1);
  258. Constant *Global =
  259. M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty));
  260. Constant *GEP = ConstantExpr::getGetElementPtr(
  261. PointerType::getUnqual(Int32Ty), Global, One);
  262. EXPECT_DEATH(Global->replaceAllUsesWith(GEP),
  263. "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
  264. }
  265. #endif
  266. #endif
  267. #undef CHECK
  268. TEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
  269. LLVMContext Context;
  270. std::unique_ptr<Module> M(new Module("MyModule", Context));
  271. Type *IntTy = Type::getInt8Ty(Context);
  272. ArrayType *ArrayTy = ArrayType::get(IntTy, 2);
  273. Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0),
  274. ConstantInt::get(IntTy, 1)};
  275. Constant *A01 = ConstantArray::get(ArrayTy, A01Vals);
  276. Constant *Global = new GlobalVariable(*M, IntTy, false,
  277. GlobalValue::ExternalLinkage, nullptr);
  278. Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy);
  279. Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt};
  280. Constant *A0G = ConstantArray::get(ArrayTy, A0GVals);
  281. ASSERT_NE(A01, A0G);
  282. GlobalVariable *RefArray =
  283. new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
  284. ASSERT_EQ(A0G, RefArray->getInitializer());
  285. GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
  286. ASSERT_EQ(A01, RefArray->getInitializer());
  287. }
  288. TEST(ConstantsTest, ConstantExprReplaceWithConstant) {
  289. LLVMContext Context;
  290. std::unique_ptr<Module> M(new Module("MyModule", Context));
  291. Type *IntTy = Type::getInt8Ty(Context);
  292. Constant *G1 = new GlobalVariable(*M, IntTy, false,
  293. GlobalValue::ExternalLinkage, nullptr);
  294. Constant *G2 = new GlobalVariable(*M, IntTy, false,
  295. GlobalValue::ExternalLinkage, nullptr);
  296. ASSERT_NE(G1, G2);
  297. Constant *Int1 = ConstantExpr::getPtrToInt(G1, IntTy);
  298. Constant *Int2 = ConstantExpr::getPtrToInt(G2, IntTy);
  299. ASSERT_NE(Int1, Int2);
  300. GlobalVariable *Ref =
  301. new GlobalVariable(*M, IntTy, false, GlobalValue::ExternalLinkage, Int1);
  302. ASSERT_EQ(Int1, Ref->getInitializer());
  303. G1->replaceAllUsesWith(G2);
  304. ASSERT_EQ(Int2, Ref->getInitializer());
  305. }
  306. TEST(ConstantsTest, GEPReplaceWithConstant) {
  307. LLVMContext Context;
  308. std::unique_ptr<Module> M(new Module("MyModule", Context));
  309. Type *IntTy = Type::getInt32Ty(Context);
  310. Type *PtrTy = PointerType::get(IntTy, 0);
  311. auto *C1 = ConstantInt::get(IntTy, 1);
  312. auto *Placeholder = new GlobalVariable(
  313. *M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr);
  314. auto *GEP = ConstantExpr::getGetElementPtr(IntTy, Placeholder, C1);
  315. ASSERT_EQ(GEP->getOperand(0), Placeholder);
  316. auto *Ref =
  317. new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, GEP);
  318. ASSERT_EQ(GEP, Ref->getInitializer());
  319. auto *Global = new GlobalVariable(*M, PtrTy, false,
  320. GlobalValue::ExternalLinkage, nullptr);
  321. auto *Alias = GlobalAlias::create(IntTy, 0, GlobalValue::ExternalLinkage,
  322. "alias", Global, M.get());
  323. Placeholder->replaceAllUsesWith(Alias);
  324. ASSERT_EQ(GEP, Ref->getInitializer());
  325. ASSERT_EQ(GEP->getOperand(0), Alias);
  326. }
  327. TEST(ConstantsTest, AliasCAPI) {
  328. LLVMContext Context;
  329. SMDiagnostic Error;
  330. std::unique_ptr<Module> M =
  331. parseAssemblyString("@g = global i32 42", Error, Context);
  332. GlobalVariable *G = M->getGlobalVariable("g");
  333. Type *I16Ty = Type::getInt16Ty(Context);
  334. Type *I16PTy = PointerType::get(I16Ty, 0);
  335. Constant *Aliasee = ConstantExpr::getBitCast(G, I16PTy);
  336. LLVMValueRef AliasRef =
  337. LLVMAddAlias(wrap(M.get()), wrap(I16PTy), wrap(Aliasee), "a");
  338. ASSERT_EQ(unwrap<GlobalAlias>(AliasRef)->getAliasee(), Aliasee);
  339. }
  340. static std::string getNameOfType(Type *T) {
  341. std::string S;
  342. raw_string_ostream RSOS(S);
  343. T->print(RSOS);
  344. return S;
  345. }
  346. TEST(ConstantsTest, BuildConstantDataArrays) {
  347. LLVMContext Context;
  348. std::unique_ptr<Module> M(new Module("MyModule", Context));
  349. for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
  350. Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
  351. ArrayType *ArrayTy = ArrayType::get(T, 2);
  352. Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
  353. Constant *CDV = ConstantArray::get(ArrayTy, Vals);
  354. ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr)
  355. << " T = " << getNameOfType(T);
  356. }
  357. for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context),
  358. Type::getDoubleTy(Context)}) {
  359. ArrayType *ArrayTy = ArrayType::get(T, 2);
  360. Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
  361. Constant *CDV = ConstantArray::get(ArrayTy, Vals);
  362. ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr)
  363. << " T = " << getNameOfType(T);
  364. }
  365. }
  366. TEST(ConstantsTest, BuildConstantDataVectors) {
  367. LLVMContext Context;
  368. std::unique_ptr<Module> M(new Module("MyModule", Context));
  369. for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
  370. Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
  371. Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
  372. Constant *CDV = ConstantVector::get(Vals);
  373. ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr)
  374. << " T = " << getNameOfType(T);
  375. }
  376. for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context),
  377. Type::getDoubleTy(Context)}) {
  378. Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
  379. Constant *CDV = ConstantVector::get(Vals);
  380. ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr)
  381. << " T = " << getNameOfType(T);
  382. }
  383. }
  384. TEST(ConstantsTest, BitcastToGEP) {
  385. LLVMContext Context;
  386. std::unique_ptr<Module> M(new Module("MyModule", Context));
  387. auto *i32 = Type::getInt32Ty(Context);
  388. auto *U = StructType::create(Context, "Unsized");
  389. Type *EltTys[] = {i32, U};
  390. auto *S = StructType::create(EltTys);
  391. auto *G = new GlobalVariable(*M, S, false,
  392. GlobalValue::ExternalLinkage, nullptr);
  393. auto *PtrTy = PointerType::get(i32, 0);
  394. auto *C = ConstantExpr::getBitCast(G, PtrTy);
  395. ASSERT_EQ(cast<ConstantExpr>(C)->getOpcode(), Instruction::BitCast);
  396. }
  397. bool foldFuncPtrAndConstToNull(LLVMContext &Context, Module *TheModule,
  398. uint64_t AndValue,
  399. MaybeAlign FunctionAlign = llvm::None) {
  400. Type *VoidType(Type::getVoidTy(Context));
  401. FunctionType *FuncType(FunctionType::get(VoidType, false));
  402. Function *Func(Function::Create(
  403. FuncType, GlobalValue::ExternalLinkage, "", TheModule));
  404. if (FunctionAlign)
  405. Func->setAlignment(*FunctionAlign);
  406. IntegerType *ConstantIntType(Type::getInt32Ty(Context));
  407. ConstantInt *TheConstant(ConstantInt::get(ConstantIntType, AndValue));
  408. Constant *TheConstantExpr(
  409. ConstantExpr::getPtrToInt(Func, ConstantIntType));
  410. bool result = ConstantExpr::get(Instruction::And, TheConstantExpr,
  411. TheConstant)->isNullValue();
  412. if (!TheModule) {
  413. // If the Module exists then it will delete the Function.
  414. delete Func;
  415. }
  416. return result;
  417. }
  418. TEST(ConstantsTest, FoldFunctionPtrAlignUnknownAnd2) {
  419. LLVMContext Context;
  420. Module TheModule("TestModule", Context);
  421. // When the DataLayout doesn't specify a function pointer alignment we
  422. // assume in this case that it is 4 byte aligned. This is a bug but we can't
  423. // fix it directly because it causes a code size regression on X86.
  424. // FIXME: This test should be changed once existing targets have
  425. // appropriate defaults. See associated FIXME in ConstantFoldBinaryInstruction
  426. ASSERT_TRUE(foldFuncPtrAndConstToNull(Context, &TheModule, 2));
  427. }
  428. TEST(ConstantsTest, DontFoldFunctionPtrAlignUnknownAnd4) {
  429. LLVMContext Context;
  430. Module TheModule("TestModule", Context);
  431. ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 4));
  432. }
  433. TEST(ConstantsTest, FoldFunctionPtrAlign4) {
  434. LLVMContext Context;
  435. Module TheModule("TestModule", Context);
  436. const char* AlignmentStrings[] = { "Fi32", "Fn32" };
  437. for (unsigned AndValue = 1; AndValue <= 2; ++AndValue) {
  438. for (const char *AlignmentString : AlignmentStrings) {
  439. TheModule.setDataLayout(AlignmentString);
  440. ASSERT_TRUE(foldFuncPtrAndConstToNull(Context, &TheModule, AndValue));
  441. }
  442. }
  443. }
  444. TEST(ConstantsTest, DontFoldFunctionPtrAlign1) {
  445. LLVMContext Context;
  446. Module TheModule("TestModule", Context);
  447. const char* AlignmentStrings[] = { "Fi8", "Fn8" };
  448. for (const char* AlignmentString : AlignmentStrings) {
  449. TheModule.setDataLayout(AlignmentString);
  450. ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 2));
  451. }
  452. }
  453. TEST(ConstantsTest, FoldFunctionAlign4PtrAlignMultiple) {
  454. LLVMContext Context;
  455. Module TheModule("TestModule", Context);
  456. TheModule.setDataLayout("Fn8");
  457. ASSERT_TRUE(foldFuncPtrAndConstToNull(Context, &TheModule, 2, Align(4)));
  458. }
  459. TEST(ConstantsTest, DontFoldFunctionAlign4PtrAlignIndependent) {
  460. LLVMContext Context;
  461. Module TheModule("TestModule", Context);
  462. TheModule.setDataLayout("Fi8");
  463. ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 2, Align(4)));
  464. }
  465. TEST(ConstantsTest, DontFoldFunctionPtrIfNoModule) {
  466. LLVMContext Context;
  467. // Even though the function is explicitly 4 byte aligned, in the absence of a
  468. // DataLayout we can't assume that the function pointer is aligned.
  469. ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, nullptr, 2, Align(4)));
  470. }
  471. TEST(ConstantsTest, FoldGlobalVariablePtr) {
  472. LLVMContext Context;
  473. IntegerType *IntType(Type::getInt32Ty(Context));
  474. std::unique_ptr<GlobalVariable> Global(
  475. new GlobalVariable(IntType, true, GlobalValue::ExternalLinkage));
  476. Global->setAlignment(Align(4));
  477. ConstantInt *TheConstant(ConstantInt::get(IntType, 2));
  478. Constant *TheConstantExpr(
  479. ConstantExpr::getPtrToInt(Global.get(), IntType));
  480. ASSERT_TRUE(ConstantExpr::get( \
  481. Instruction::And, TheConstantExpr, TheConstant)->isNullValue());
  482. }
  483. } // end anonymous namespace
  484. } // end namespace llvm