InstructionsTest.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. //===- llvm/unittest/IR/InstructionsTest.cpp - Instructions 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/IR/Instructions.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/Analysis/ValueTracking.h"
  12. #include "llvm/IR/BasicBlock.h"
  13. #include "llvm/IR/Constants.h"
  14. #include "llvm/IR/DataLayout.h"
  15. #include "llvm/IR/DerivedTypes.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/IRBuilder.h"
  18. #include "llvm/IR/LLVMContext.h"
  19. #include "llvm/IR/MDBuilder.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/IR/Operator.h"
  22. #include "gtest/gtest.h"
  23. #include <memory>
  24. namespace llvm {
  25. namespace {
  26. TEST(InstructionsTest, ReturnInst) {
  27. LLVMContext &C(getGlobalContext());
  28. // test for PR6589
  29. const ReturnInst* r0 = ReturnInst::Create(C);
  30. EXPECT_EQ(r0->getNumOperands(), 0U);
  31. EXPECT_EQ(r0->op_begin(), r0->op_end());
  32. IntegerType* Int1 = IntegerType::get(C, 1);
  33. Constant* One = ConstantInt::get(Int1, 1, true);
  34. const ReturnInst* r1 = ReturnInst::Create(C, One);
  35. EXPECT_EQ(1U, r1->getNumOperands());
  36. User::const_op_iterator b(r1->op_begin());
  37. EXPECT_NE(r1->op_end(), b);
  38. EXPECT_EQ(One, *b);
  39. EXPECT_EQ(One, r1->getOperand(0));
  40. ++b;
  41. EXPECT_EQ(r1->op_end(), b);
  42. // clean up
  43. delete r0;
  44. delete r1;
  45. }
  46. // Test fixture that provides a module and a single function within it. Useful
  47. // for tests that need to refer to the function in some way.
  48. class ModuleWithFunctionTest : public testing::Test {
  49. protected:
  50. ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) {
  51. FArgTypes.push_back(Type::getInt8Ty(Ctx));
  52. FArgTypes.push_back(Type::getInt32Ty(Ctx));
  53. FArgTypes.push_back(Type::getInt64Ty(Ctx));
  54. FunctionType *FTy =
  55. FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
  56. F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
  57. }
  58. LLVMContext Ctx;
  59. std::unique_ptr<Module> M;
  60. SmallVector<Type *, 3> FArgTypes;
  61. Function *F;
  62. };
  63. TEST_F(ModuleWithFunctionTest, CallInst) {
  64. Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
  65. ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
  66. ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
  67. std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
  68. // Make sure iteration over a call's arguments works as expected.
  69. unsigned Idx = 0;
  70. for (Value *Arg : Call->arg_operands()) {
  71. EXPECT_EQ(FArgTypes[Idx], Arg->getType());
  72. EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
  73. Idx++;
  74. }
  75. }
  76. TEST_F(ModuleWithFunctionTest, InvokeInst) {
  77. BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
  78. BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F);
  79. Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
  80. ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
  81. ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
  82. std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args));
  83. // Make sure iteration over invoke's arguments works as expected.
  84. unsigned Idx = 0;
  85. for (Value *Arg : Invoke->arg_operands()) {
  86. EXPECT_EQ(FArgTypes[Idx], Arg->getType());
  87. EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType());
  88. Idx++;
  89. }
  90. }
  91. TEST(InstructionsTest, BranchInst) {
  92. LLVMContext &C(getGlobalContext());
  93. // Make a BasicBlocks
  94. BasicBlock* bb0 = BasicBlock::Create(C);
  95. BasicBlock* bb1 = BasicBlock::Create(C);
  96. // Mandatory BranchInst
  97. const BranchInst* b0 = BranchInst::Create(bb0);
  98. EXPECT_TRUE(b0->isUnconditional());
  99. EXPECT_FALSE(b0->isConditional());
  100. EXPECT_EQ(1U, b0->getNumSuccessors());
  101. // check num operands
  102. EXPECT_EQ(1U, b0->getNumOperands());
  103. EXPECT_NE(b0->op_begin(), b0->op_end());
  104. EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
  105. EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
  106. IntegerType* Int1 = IntegerType::get(C, 1);
  107. Constant* One = ConstantInt::get(Int1, 1, true);
  108. // Conditional BranchInst
  109. BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
  110. EXPECT_FALSE(b1->isUnconditional());
  111. EXPECT_TRUE(b1->isConditional());
  112. EXPECT_EQ(2U, b1->getNumSuccessors());
  113. // check num operands
  114. EXPECT_EQ(3U, b1->getNumOperands());
  115. User::const_op_iterator b(b1->op_begin());
  116. // check COND
  117. EXPECT_NE(b, b1->op_end());
  118. EXPECT_EQ(One, *b);
  119. EXPECT_EQ(One, b1->getOperand(0));
  120. EXPECT_EQ(One, b1->getCondition());
  121. ++b;
  122. // check ELSE
  123. EXPECT_EQ(bb1, *b);
  124. EXPECT_EQ(bb1, b1->getOperand(1));
  125. EXPECT_EQ(bb1, b1->getSuccessor(1));
  126. ++b;
  127. // check THEN
  128. EXPECT_EQ(bb0, *b);
  129. EXPECT_EQ(bb0, b1->getOperand(2));
  130. EXPECT_EQ(bb0, b1->getSuccessor(0));
  131. ++b;
  132. EXPECT_EQ(b1->op_end(), b);
  133. // clean up
  134. delete b0;
  135. delete b1;
  136. delete bb0;
  137. delete bb1;
  138. }
  139. TEST(InstructionsTest, CastInst) {
  140. LLVMContext &C(getGlobalContext());
  141. Type *Int8Ty = Type::getInt8Ty(C);
  142. Type *Int16Ty = Type::getInt16Ty(C);
  143. Type *Int32Ty = Type::getInt32Ty(C);
  144. Type *Int64Ty = Type::getInt64Ty(C);
  145. Type *V8x8Ty = VectorType::get(Int8Ty, 8);
  146. Type *V8x64Ty = VectorType::get(Int64Ty, 8);
  147. Type *X86MMXTy = Type::getX86_MMXTy(C);
  148. Type *HalfTy = Type::getHalfTy(C);
  149. Type *FloatTy = Type::getFloatTy(C);
  150. Type *DoubleTy = Type::getDoubleTy(C);
  151. Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
  152. Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
  153. Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
  154. Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
  155. Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
  156. Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
  157. Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
  158. Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
  159. Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
  160. Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
  161. Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
  162. Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
  163. Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
  164. Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
  165. const Constant* c8 = Constant::getNullValue(V8x8Ty);
  166. const Constant* c64 = Constant::getNullValue(V8x64Ty);
  167. const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
  168. EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
  169. EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
  170. EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
  171. EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
  172. EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
  173. EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
  174. EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
  175. EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
  176. EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
  177. EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
  178. EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
  179. EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
  180. // Check address space casts are rejected since we don't know the sizes here
  181. EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
  182. EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
  183. EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
  184. EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
  185. EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
  186. EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
  187. EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
  188. V2Int32PtrAS1Ty,
  189. true));
  190. // Test mismatched number of elements for pointers
  191. EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
  192. EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
  193. EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
  194. EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
  195. EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
  196. EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
  197. EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
  198. EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
  199. EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
  200. EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
  201. EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
  202. EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
  203. EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
  204. EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
  205. EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
  206. EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
  207. EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
  208. EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
  209. EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
  210. EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
  211. EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
  212. EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
  213. EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
  214. EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
  215. Constant::getNullValue(V4Int32PtrTy),
  216. V2Int32PtrTy));
  217. EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
  218. Constant::getNullValue(V2Int32PtrTy),
  219. V4Int32PtrTy));
  220. EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
  221. Constant::getNullValue(V4Int32PtrAS1Ty),
  222. V2Int32PtrTy));
  223. EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
  224. Constant::getNullValue(V2Int32PtrTy),
  225. V4Int32PtrAS1Ty));
  226. // Check that assertion is not hit when creating a cast with a vector of
  227. // pointers
  228. // First form
  229. BasicBlock *BB = BasicBlock::Create(C);
  230. Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
  231. CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
  232. // Second form
  233. CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
  234. }
  235. TEST(InstructionsTest, VectorGep) {
  236. LLVMContext &C(getGlobalContext());
  237. // Type Definitions
  238. Type *I8Ty = IntegerType::get(C, 8);
  239. Type *I32Ty = IntegerType::get(C, 32);
  240. PointerType *Ptri8Ty = PointerType::get(I8Ty, 0);
  241. PointerType *Ptri32Ty = PointerType::get(I32Ty, 0);
  242. VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
  243. VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
  244. // Test different aspects of the vector-of-pointers type
  245. // and GEPs which use this type.
  246. ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
  247. ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
  248. std::vector<Constant*> ConstVa(2, Ci32a);
  249. std::vector<Constant*> ConstVb(2, Ci32b);
  250. Constant *C2xi32a = ConstantVector::get(ConstVa);
  251. Constant *C2xi32b = ConstantVector::get(ConstVb);
  252. CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
  253. CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
  254. ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
  255. ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
  256. EXPECT_NE(ICmp0, ICmp1); // suppress warning.
  257. BasicBlock* BB0 = BasicBlock::Create(C);
  258. // Test InsertAtEnd ICmpInst constructor.
  259. ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
  260. EXPECT_NE(ICmp0, ICmp2); // suppress warning.
  261. GetElementPtrInst *Gep0 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32a);
  262. GetElementPtrInst *Gep1 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32b);
  263. GetElementPtrInst *Gep2 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32a);
  264. GetElementPtrInst *Gep3 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32b);
  265. CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
  266. CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
  267. CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
  268. CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
  269. Value *S0 = BTC0->stripPointerCasts();
  270. Value *S1 = BTC1->stripPointerCasts();
  271. Value *S2 = BTC2->stripPointerCasts();
  272. Value *S3 = BTC3->stripPointerCasts();
  273. EXPECT_NE(S0, Gep0);
  274. EXPECT_NE(S1, Gep1);
  275. EXPECT_NE(S2, Gep2);
  276. EXPECT_NE(S3, Gep3);
  277. int64_t Offset;
  278. DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
  279. "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
  280. ":128:128-n8:16:32:64-S128");
  281. // Make sure we don't crash
  282. GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
  283. GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
  284. GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
  285. GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
  286. // Gep of Geps
  287. GetElementPtrInst *GepII0 = GetElementPtrInst::Create(I32Ty, Gep0, C2xi32b);
  288. GetElementPtrInst *GepII1 = GetElementPtrInst::Create(I32Ty, Gep1, C2xi32a);
  289. GetElementPtrInst *GepII2 = GetElementPtrInst::Create(I32Ty, Gep2, C2xi32b);
  290. GetElementPtrInst *GepII3 = GetElementPtrInst::Create(I32Ty, Gep3, C2xi32a);
  291. EXPECT_EQ(GepII0->getNumIndices(), 1u);
  292. EXPECT_EQ(GepII1->getNumIndices(), 1u);
  293. EXPECT_EQ(GepII2->getNumIndices(), 1u);
  294. EXPECT_EQ(GepII3->getNumIndices(), 1u);
  295. EXPECT_FALSE(GepII0->hasAllZeroIndices());
  296. EXPECT_FALSE(GepII1->hasAllZeroIndices());
  297. EXPECT_FALSE(GepII2->hasAllZeroIndices());
  298. EXPECT_FALSE(GepII3->hasAllZeroIndices());
  299. delete GepII0;
  300. delete GepII1;
  301. delete GepII2;
  302. delete GepII3;
  303. delete BTC0;
  304. delete BTC1;
  305. delete BTC2;
  306. delete BTC3;
  307. delete Gep0;
  308. delete Gep1;
  309. delete Gep2;
  310. delete Gep3;
  311. ICmp2->eraseFromParent();
  312. delete BB0;
  313. delete ICmp0;
  314. delete ICmp1;
  315. delete PtrVecA;
  316. delete PtrVecB;
  317. }
  318. TEST(InstructionsTest, FPMathOperator) {
  319. LLVMContext &Context = getGlobalContext();
  320. IRBuilder<> Builder(Context);
  321. MDBuilder MDHelper(Context);
  322. Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
  323. MDNode *MD1 = MDHelper.createFPMath(1.0);
  324. Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
  325. EXPECT_TRUE(isa<FPMathOperator>(V1));
  326. FPMathOperator *O1 = cast<FPMathOperator>(V1);
  327. EXPECT_EQ(O1->getFPAccuracy(), 1.0);
  328. delete V1;
  329. delete I;
  330. }
  331. TEST(InstructionsTest, isEliminableCastPair) {
  332. LLVMContext &C(getGlobalContext());
  333. Type* Int16Ty = Type::getInt16Ty(C);
  334. Type* Int32Ty = Type::getInt32Ty(C);
  335. Type* Int64Ty = Type::getInt64Ty(C);
  336. Type* Int64PtrTy = Type::getInt64PtrTy(C);
  337. // Source and destination pointers have same size -> bitcast.
  338. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
  339. CastInst::IntToPtr,
  340. Int64PtrTy, Int64Ty, Int64PtrTy,
  341. Int32Ty, nullptr, Int32Ty),
  342. CastInst::BitCast);
  343. // Source and destination have unknown sizes, but the same address space and
  344. // the intermediate int is the maximum pointer size -> bitcast
  345. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
  346. CastInst::IntToPtr,
  347. Int64PtrTy, Int64Ty, Int64PtrTy,
  348. nullptr, nullptr, nullptr),
  349. CastInst::BitCast);
  350. // Source and destination have unknown sizes, but the same address space and
  351. // the intermediate int is not the maximum pointer size -> nothing
  352. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
  353. CastInst::IntToPtr,
  354. Int64PtrTy, Int32Ty, Int64PtrTy,
  355. nullptr, nullptr, nullptr),
  356. 0U);
  357. // Middle pointer big enough -> bitcast.
  358. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
  359. CastInst::PtrToInt,
  360. Int64Ty, Int64PtrTy, Int64Ty,
  361. nullptr, Int64Ty, nullptr),
  362. CastInst::BitCast);
  363. // Middle pointer too small -> fail.
  364. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
  365. CastInst::PtrToInt,
  366. Int64Ty, Int64PtrTy, Int64Ty,
  367. nullptr, Int32Ty, nullptr),
  368. 0U);
  369. // Test that we don't eliminate bitcasts between different address spaces,
  370. // or if we don't have available pointer size information.
  371. DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
  372. "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
  373. "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
  374. Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
  375. Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
  376. IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
  377. IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
  378. // Cannot simplify inttoptr, addrspacecast
  379. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
  380. CastInst::AddrSpaceCast,
  381. Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
  382. nullptr, Int16SizePtr, Int64SizePtr),
  383. 0U);
  384. // Cannot simplify addrspacecast, ptrtoint
  385. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
  386. CastInst::PtrToInt,
  387. Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
  388. Int64SizePtr, Int16SizePtr, nullptr),
  389. 0U);
  390. // Pass since the bitcast address spaces are the same
  391. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
  392. CastInst::BitCast,
  393. Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
  394. nullptr, nullptr, nullptr),
  395. CastInst::IntToPtr);
  396. }
  397. TEST(InstructionsTest, CloneCall) {
  398. LLVMContext &C(getGlobalContext());
  399. Type *Int32Ty = Type::getInt32Ty(C);
  400. Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty};
  401. Type *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false);
  402. Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
  403. Value *Args[] = {
  404. ConstantInt::get(Int32Ty, 1),
  405. ConstantInt::get(Int32Ty, 2),
  406. ConstantInt::get(Int32Ty, 3)
  407. };
  408. std::unique_ptr<CallInst> Call(CallInst::Create(Callee, Args, "result"));
  409. // Test cloning the tail call kind.
  410. CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail,
  411. CallInst::TCK_MustTail};
  412. for (CallInst::TailCallKind TCK : Kinds) {
  413. Call->setTailCallKind(TCK);
  414. std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
  415. EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
  416. }
  417. Call->setTailCallKind(CallInst::TCK_None);
  418. // Test cloning an attribute.
  419. {
  420. AttrBuilder AB;
  421. AB.addAttribute(Attribute::ReadOnly);
  422. Call->setAttributes(AttributeSet::get(C, AttributeSet::FunctionIndex, AB));
  423. std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
  424. EXPECT_TRUE(Clone->onlyReadsMemory());
  425. }
  426. }
  427. } // end anonymous namespace
  428. } // end namespace llvm