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