/3rd_party/llvm/unittests/VMCore/InstructionsTest.cpp

https://code.google.com/p/softart/ · C++ · 247 lines · 173 code · 50 blank · 24 comment · 0 complexity · 37191298d982e20caaec0ef91d7a9282 MD5 · raw file

  1. //===- llvm/unittest/VMCore/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/BasicBlock.h"
  10. #include "llvm/Constants.h"
  11. #include "llvm/DerivedTypes.h"
  12. #include "llvm/IRBuilder.h"
  13. #include "llvm/Instructions.h"
  14. #include "llvm/LLVMContext.h"
  15. #include "llvm/MDBuilder.h"
  16. #include "llvm/Operator.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/Analysis/ValueTracking.h"
  19. #include "llvm/Target/TargetData.h"
  20. #include "gtest/gtest.h"
  21. namespace llvm {
  22. namespace {
  23. TEST(InstructionsTest, ReturnInst) {
  24. LLVMContext &C(getGlobalContext());
  25. // test for PR6589
  26. const ReturnInst* r0 = ReturnInst::Create(C);
  27. EXPECT_EQ(r0->getNumOperands(), 0U);
  28. EXPECT_EQ(r0->op_begin(), r0->op_end());
  29. IntegerType* Int1 = IntegerType::get(C, 1);
  30. Constant* One = ConstantInt::get(Int1, 1, true);
  31. const ReturnInst* r1 = ReturnInst::Create(C, One);
  32. EXPECT_EQ(1U, r1->getNumOperands());
  33. User::const_op_iterator b(r1->op_begin());
  34. EXPECT_NE(r1->op_end(), b);
  35. EXPECT_EQ(One, *b);
  36. EXPECT_EQ(One, r1->getOperand(0));
  37. ++b;
  38. EXPECT_EQ(r1->op_end(), b);
  39. // clean up
  40. delete r0;
  41. delete r1;
  42. }
  43. TEST(InstructionsTest, BranchInst) {
  44. LLVMContext &C(getGlobalContext());
  45. // Make a BasicBlocks
  46. BasicBlock* bb0 = BasicBlock::Create(C);
  47. BasicBlock* bb1 = BasicBlock::Create(C);
  48. // Mandatory BranchInst
  49. const BranchInst* b0 = BranchInst::Create(bb0);
  50. EXPECT_TRUE(b0->isUnconditional());
  51. EXPECT_FALSE(b0->isConditional());
  52. EXPECT_EQ(1U, b0->getNumSuccessors());
  53. // check num operands
  54. EXPECT_EQ(1U, b0->getNumOperands());
  55. EXPECT_NE(b0->op_begin(), b0->op_end());
  56. EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
  57. EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
  58. IntegerType* Int1 = IntegerType::get(C, 1);
  59. Constant* One = ConstantInt::get(Int1, 1, true);
  60. // Conditional BranchInst
  61. BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
  62. EXPECT_FALSE(b1->isUnconditional());
  63. EXPECT_TRUE(b1->isConditional());
  64. EXPECT_EQ(2U, b1->getNumSuccessors());
  65. // check num operands
  66. EXPECT_EQ(3U, b1->getNumOperands());
  67. User::const_op_iterator b(b1->op_begin());
  68. // check COND
  69. EXPECT_NE(b, b1->op_end());
  70. EXPECT_EQ(One, *b);
  71. EXPECT_EQ(One, b1->getOperand(0));
  72. EXPECT_EQ(One, b1->getCondition());
  73. ++b;
  74. // check ELSE
  75. EXPECT_EQ(bb1, *b);
  76. EXPECT_EQ(bb1, b1->getOperand(1));
  77. EXPECT_EQ(bb1, b1->getSuccessor(1));
  78. ++b;
  79. // check THEN
  80. EXPECT_EQ(bb0, *b);
  81. EXPECT_EQ(bb0, b1->getOperand(2));
  82. EXPECT_EQ(bb0, b1->getSuccessor(0));
  83. ++b;
  84. EXPECT_EQ(b1->op_end(), b);
  85. // clean up
  86. delete b0;
  87. delete b1;
  88. delete bb0;
  89. delete bb1;
  90. }
  91. TEST(InstructionsTest, CastInst) {
  92. LLVMContext &C(getGlobalContext());
  93. Type* Int8Ty = Type::getInt8Ty(C);
  94. Type* Int64Ty = Type::getInt64Ty(C);
  95. Type* V8x8Ty = VectorType::get(Int8Ty, 8);
  96. Type* V8x64Ty = VectorType::get(Int64Ty, 8);
  97. Type* X86MMXTy = Type::getX86_MMXTy(C);
  98. const Constant* c8 = Constant::getNullValue(V8x8Ty);
  99. const Constant* c64 = Constant::getNullValue(V8x64Ty);
  100. EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
  101. EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
  102. EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
  103. EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
  104. EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
  105. EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
  106. EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
  107. }
  108. TEST(InstructionsTest, VectorGep) {
  109. LLVMContext &C(getGlobalContext());
  110. // Type Definitions
  111. PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0);
  112. PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 8), 0);
  113. VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
  114. VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
  115. // Test different aspects of the vector-of-pointers type
  116. // and GEPs which use this type.
  117. ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
  118. ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
  119. std::vector<Constant*> ConstVa(2, Ci32a);
  120. std::vector<Constant*> ConstVb(2, Ci32b);
  121. Constant *C2xi32a = ConstantVector::get(ConstVa);
  122. Constant *C2xi32b = ConstantVector::get(ConstVb);
  123. CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
  124. CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
  125. ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
  126. ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
  127. EXPECT_NE(ICmp0, ICmp1); // suppress warning.
  128. GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a);
  129. GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b);
  130. GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a);
  131. GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b);
  132. CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
  133. CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
  134. CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
  135. CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
  136. Value *S0 = BTC0->stripPointerCasts();
  137. Value *S1 = BTC1->stripPointerCasts();
  138. Value *S2 = BTC2->stripPointerCasts();
  139. Value *S3 = BTC3->stripPointerCasts();
  140. EXPECT_NE(S0, Gep0);
  141. EXPECT_NE(S1, Gep1);
  142. EXPECT_NE(S2, Gep2);
  143. EXPECT_NE(S3, Gep3);
  144. int64_t Offset;
  145. TargetData TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
  146. "2:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80"
  147. ":128:128-n8:16:32:64-S128");
  148. // Make sure we don't crash
  149. GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
  150. GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
  151. GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
  152. GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
  153. // Gep of Geps
  154. GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b);
  155. GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a);
  156. GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b);
  157. GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a);
  158. EXPECT_EQ(GepII0->getNumIndices(), 1u);
  159. EXPECT_EQ(GepII1->getNumIndices(), 1u);
  160. EXPECT_EQ(GepII2->getNumIndices(), 1u);
  161. EXPECT_EQ(GepII3->getNumIndices(), 1u);
  162. EXPECT_FALSE(GepII0->hasAllZeroIndices());
  163. EXPECT_FALSE(GepII1->hasAllZeroIndices());
  164. EXPECT_FALSE(GepII2->hasAllZeroIndices());
  165. EXPECT_FALSE(GepII3->hasAllZeroIndices());
  166. delete GepII0;
  167. delete GepII1;
  168. delete GepII2;
  169. delete GepII3;
  170. delete BTC0;
  171. delete BTC1;
  172. delete BTC2;
  173. delete BTC3;
  174. delete Gep0;
  175. delete Gep1;
  176. delete Gep2;
  177. delete Gep3;
  178. delete ICmp0;
  179. delete ICmp1;
  180. delete PtrVecA;
  181. delete PtrVecB;
  182. }
  183. TEST(InstructionsTest, FPMathOperator) {
  184. LLVMContext &Context = getGlobalContext();
  185. IRBuilder<> Builder(Context);
  186. MDBuilder MDHelper(Context);
  187. Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
  188. MDNode *MD1 = MDHelper.createFPMath(1.0);
  189. Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
  190. EXPECT_TRUE(isa<FPMathOperator>(V1));
  191. FPMathOperator *O1 = cast<FPMathOperator>(V1);
  192. EXPECT_EQ(O1->getFPAccuracy(), 1.0);
  193. delete V1;
  194. delete I;
  195. }
  196. } // end anonymous namespace
  197. } // end namespace llvm