InstructionsTest.cpp revision c6a4f5e819217e1e12c458aed8e7b122e23a3a58
1c779e96158cbac4c62df8e2053ab6a933eba5868Chandler Carruth//===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
25a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif//
35a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif//                     The LLVM Compiler Infrastructure
45a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif//
55a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif// This file is distributed under the University of Illinois Open Source
65a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif// License. See LICENSE.TXT for details.
75a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif//
85a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif//===----------------------------------------------------------------------===//
95a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif
100b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instructions.h"
115a88dda4be791426ab4d20a6a6c9c65d66614a27Chandler Carruth#include "llvm/ADT/STLExtras.h"
125a88dda4be791426ab4d20a6a6c9c65d66614a27Chandler Carruth#include "llvm/Analysis/ValueTracking.h"
130b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/BasicBlock.h"
140b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h"
150b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h"
160b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
1736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/Function.h"
180b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IRBuilder.h"
190b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/LLVMContext.h"
200b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/MDBuilder.h"
2136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/Module.h"
220b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Operator.h"
235a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif#include "gtest/gtest.h"
2436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include <memory>
255a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif
265a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greifnamespace llvm {
275a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greifnamespace {
285a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif
29138acfe353ed8b895de093a4c118b01093b6fbfbGabor GreifTEST(InstructionsTest, ReturnInst) {
305a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif  LLVMContext &C(getGlobalContext());
315a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif
32138acfe353ed8b895de093a4c118b01093b6fbfbGabor Greif  // test for PR6589
335a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif  const ReturnInst* r0 = ReturnInst::Create(C);
34d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  EXPECT_EQ(r0->getNumOperands(), 0U);
35138acfe353ed8b895de093a4c118b01093b6fbfbGabor Greif  EXPECT_EQ(r0->op_begin(), r0->op_end());
3622385eb8527f6bf8083ee85f85dc51e4d4928aaaGabor Greif
37db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  IntegerType* Int1 = IntegerType::get(C, 1);
3822385eb8527f6bf8083ee85f85dc51e4d4928aaaGabor Greif  Constant* One = ConstantInt::get(Int1, 1, true);
3922385eb8527f6bf8083ee85f85dc51e4d4928aaaGabor Greif  const ReturnInst* r1 = ReturnInst::Create(C, One);
40f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(1U, r1->getNumOperands());
4122385eb8527f6bf8083ee85f85dc51e4d4928aaaGabor Greif  User::const_op_iterator b(r1->op_begin());
42f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_NE(r1->op_end(), b);
43f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(One, *b);
44f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(One, r1->getOperand(0));
4522385eb8527f6bf8083ee85f85dc51e4d4928aaaGabor Greif  ++b;
46f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(r1->op_end(), b);
47642c066906488715220dd87c5b976c67bb8a303dGabor Greif
48642c066906488715220dd87c5b976c67bb8a303dGabor Greif  // clean up
49642c066906488715220dd87c5b976c67bb8a303dGabor Greif  delete r0;
50642c066906488715220dd87c5b976c67bb8a303dGabor Greif  delete r1;
515a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif}
525a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif
5336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// Test fixture that provides a module and a single function within it. Useful
5436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// for tests that need to refer to the function in some way.
5536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass ModuleWithFunctionTest : public testing::Test {
5636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesprotected:
5736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) {
5836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    FArgTypes.push_back(Type::getInt8Ty(Ctx));
5936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    FArgTypes.push_back(Type::getInt32Ty(Ctx));
6036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    FArgTypes.push_back(Type::getInt64Ty(Ctx));
6136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    FunctionType *FTy =
6236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
6336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
6436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
6536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
6636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  LLVMContext Ctx;
6736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::unique_ptr<Module> M;
6836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  SmallVector<Type *, 3> FArgTypes;
6936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Function *F;
7036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines};
7136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
7236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesTEST_F(ModuleWithFunctionTest, CallInst) {
7336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
7436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                   ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
7536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                   ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
7636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
7736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
7836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // Make sure iteration over a call's arguments works as expected.
7936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  unsigned Idx = 0;
8036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  for (Value *Arg : Call->arg_operands()) {
8136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    EXPECT_EQ(FArgTypes[Idx], Arg->getType());
8236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
8336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    Idx++;
8436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
8536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
8636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
8736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesTEST_F(ModuleWithFunctionTest, InvokeInst) {
8836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
8936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F);
9036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
9136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
9236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                   ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
9336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                   ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
9436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args));
9536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
9636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // Make sure iteration over invoke's arguments works as expected.
9736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  unsigned Idx = 0;
9836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  for (Value *Arg : Invoke->arg_operands()) {
9936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    EXPECT_EQ(FArgTypes[Idx], Arg->getType());
10036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType());
10136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    Idx++;
10236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
10336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
10436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
105d165e1a71193ebff49ede9663d1ed70050f04699Gabor GreifTEST(InstructionsTest, BranchInst) {
106d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  LLVMContext &C(getGlobalContext());
107d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
108d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  // Make a BasicBlocks
109d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  BasicBlock* bb0 = BasicBlock::Create(C);
110d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  BasicBlock* bb1 = BasicBlock::Create(C);
111d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
112d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  // Mandatory BranchInst
113d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  const BranchInst* b0 = BranchInst::Create(bb0);
114d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
115abf657f7e6dbce1bf8e02fce2d602f33d5fc5c2aGabor Greif  EXPECT_TRUE(b0->isUnconditional());
116abf657f7e6dbce1bf8e02fce2d602f33d5fc5c2aGabor Greif  EXPECT_FALSE(b0->isConditional());
117f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(1U, b0->getNumSuccessors());
118abf657f7e6dbce1bf8e02fce2d602f33d5fc5c2aGabor Greif
119d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  // check num operands
120f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(1U, b0->getNumOperands());
121d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
122d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  EXPECT_NE(b0->op_begin(), b0->op_end());
12336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
124abf657f7e6dbce1bf8e02fce2d602f33d5fc5c2aGabor Greif
12536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
126d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
127db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  IntegerType* Int1 = IntegerType::get(C, 1);
128d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  Constant* One = ConstantInt::get(Int1, 1, true);
129d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
130d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  // Conditional BranchInst
131d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
132d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
133abf657f7e6dbce1bf8e02fce2d602f33d5fc5c2aGabor Greif  EXPECT_FALSE(b1->isUnconditional());
134abf657f7e6dbce1bf8e02fce2d602f33d5fc5c2aGabor Greif  EXPECT_TRUE(b1->isConditional());
135f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(2U, b1->getNumSuccessors());
136abf657f7e6dbce1bf8e02fce2d602f33d5fc5c2aGabor Greif
137d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  // check num operands
138f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(3U, b1->getNumOperands());
139d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
140d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  User::const_op_iterator b(b1->op_begin());
141d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
142d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  // check COND
143d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  EXPECT_NE(b, b1->op_end());
144f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(One, *b);
145f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(One, b1->getOperand(0));
146f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(One, b1->getCondition());
147d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  ++b;
148d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
149d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  // check ELSE
150f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(bb1, *b);
151f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(bb1, b1->getOperand(1));
152f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(bb1, b1->getSuccessor(1));
153d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  ++b;
154d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
155d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  // check THEN
156f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(bb0, *b);
157f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(bb0, b1->getOperand(2));
158f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(bb0, b1->getSuccessor(0));
159d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  ++b;
160d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
161f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(b1->op_end(), b);
162d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
163d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  // clean up
164d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  delete b0;
165d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  delete b1;
166d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
167d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  delete bb0;
168d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif  delete bb1;
169d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif}
170d165e1a71193ebff49ede9663d1ed70050f04699Gabor Greif
171607946533d4eb781713b363605c4a241503dbe0eDuncan SandsTEST(InstructionsTest, CastInst) {
172607946533d4eb781713b363605c4a241503dbe0eDuncan Sands  LLVMContext &C(getGlobalContext());
173607946533d4eb781713b363605c4a241503dbe0eDuncan Sands
174f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *Int8Ty = Type::getInt8Ty(C);
175f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *Int16Ty = Type::getInt16Ty(C);
176f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *Int32Ty = Type::getInt32Ty(C);
177f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *Int64Ty = Type::getInt64Ty(C);
178f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *V8x8Ty = VectorType::get(Int8Ty, 8);
179f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *V8x64Ty = VectorType::get(Int64Ty, 8);
180f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *X86MMXTy = Type::getX86_MMXTy(C);
181f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault
182f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *HalfTy = Type::getHalfTy(C);
183f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *FloatTy = Type::getFloatTy(C);
184f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *DoubleTy = Type::getDoubleTy(C);
185f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault
186f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
187f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
188f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
189f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault
190f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
191f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
192f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault
193f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
194f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
195f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault
196f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
197f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
198f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
199f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
200f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault
201f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
202f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
20336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
204607946533d4eb781713b363605c4a241503dbe0eDuncan Sands
205117feba971a020653fef3d6d61a16345355b83fdDuncan Sands  const Constant* c8 = Constant::getNullValue(V8x8Ty);
206117feba971a020653fef3d6d61a16345355b83fdDuncan Sands  const Constant* c64 = Constant::getNullValue(V8x64Ty);
207117feba971a020653fef3d6d61a16345355b83fdDuncan Sands
20859d3ae6cdc4316ad338cd848251f33a236ccb36cMatt Arsenault  const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
20959d3ae6cdc4316ad338cd848251f33a236ccb36cMatt Arsenault
210485c7fd76b32a69c46782a715682ed8831b0873bMatt Arsenault  EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
211485c7fd76b32a69c46782a715682ed8831b0873bMatt Arsenault  EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
212485c7fd76b32a69c46782a715682ed8831b0873bMatt Arsenault  EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
213485c7fd76b32a69c46782a715682ed8831b0873bMatt Arsenault  EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
214485c7fd76b32a69c46782a715682ed8831b0873bMatt Arsenault  EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
215f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
216f5ec9b55e871d326b8917f203711529273e7fa54John McCall  EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
2171608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
218f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
219f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
220f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
221f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
222f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
223f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault
224f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  // Check address space casts are rejected since we don't know the sizes here
225f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
226f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
227f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
228f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
229f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
23059d3ae6cdc4316ad338cd848251f33a236ccb36cMatt Arsenault  EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
23159d3ae6cdc4316ad338cd848251f33a236ccb36cMatt Arsenault  EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
23259d3ae6cdc4316ad338cd848251f33a236ccb36cMatt Arsenault                                                             V2Int32PtrAS1Ty,
23359d3ae6cdc4316ad338cd848251f33a236ccb36cMatt Arsenault                                                             true));
234f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault
235f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  // Test mismatched number of elements for pointers
236f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
237f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
238f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
239f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
240f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
241f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault
242f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
243f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
244f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
245f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
246f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
247f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
248f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
249f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
250f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
251f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault
252f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
253f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
254f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
255f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault
256f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
257f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
258f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
259f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
260f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
261f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault  EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
2621bf0ec4e1642a532c0121de8ccc0878d6403c9d3Matt Arsenault
2631bf0ec4e1642a532c0121de8ccc0878d6403c9d3Matt Arsenault
26436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
26536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                     Constant::getNullValue(V4Int32PtrTy),
26636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                     V2Int32PtrTy));
26736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
26836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                     Constant::getNullValue(V2Int32PtrTy),
26936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                     V4Int32PtrTy));
27036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
27136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
27236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                     Constant::getNullValue(V4Int32PtrAS1Ty),
27336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                     V2Int32PtrTy));
27436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
27536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                     Constant::getNullValue(V2Int32PtrTy),
27636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                     V4Int32PtrAS1Ty));
27736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
27836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
2791bf0ec4e1642a532c0121de8ccc0878d6403c9d3Matt Arsenault  // Check that assertion is not hit when creating a cast with a vector of
2801bf0ec4e1642a532c0121de8ccc0878d6403c9d3Matt Arsenault  // pointers
2811bf0ec4e1642a532c0121de8ccc0878d6403c9d3Matt Arsenault  // First form
2821bf0ec4e1642a532c0121de8ccc0878d6403c9d3Matt Arsenault  BasicBlock *BB = BasicBlock::Create(C);
2831bf0ec4e1642a532c0121de8ccc0878d6403c9d3Matt Arsenault  Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
2841bf0ec4e1642a532c0121de8ccc0878d6403c9d3Matt Arsenault  CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
2851bf0ec4e1642a532c0121de8ccc0878d6403c9d3Matt Arsenault
2861bf0ec4e1642a532c0121de8ccc0878d6403c9d3Matt Arsenault  // Second form
2871bf0ec4e1642a532c0121de8ccc0878d6403c9d3Matt Arsenault  CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
288f34dc428fa577d6d5d71ab3a1f9765b4e5da5a4fMatt Arsenault}
2891608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
2901608769abeb1430dc34f31ffac0d9850f99ae36aNadav RotemTEST(InstructionsTest, VectorGep) {
2911608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  LLVMContext &C(getGlobalContext());
2921608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
2931608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  // Type Definitions
2941608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0);
29576bf61fe0754500c5c9d01fc440ff36c76ff61f5Matt Arsenault  PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 32), 0);
2961608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
2971608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
2981608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
2991608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3001608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  // Test different aspects of the vector-of-pointers type
3011608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  // and GEPs which use this type.
3021608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
3031608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
3041608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  std::vector<Constant*> ConstVa(2, Ci32a);
3051608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  std::vector<Constant*> ConstVb(2, Ci32b);
3061608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  Constant *C2xi32a = ConstantVector::get(ConstVa);
3071608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  Constant *C2xi32b = ConstantVector::get(ConstVb);
3081608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3091608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
3101608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
3111608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3121608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
3131608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
3141608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  EXPECT_NE(ICmp0, ICmp1); // suppress warning.
3151608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3164802b9d6dc7443985066f0381c0a2468f72f9b81Evgeniy Stepanov  BasicBlock* BB0 = BasicBlock::Create(C);
3174802b9d6dc7443985066f0381c0a2468f72f9b81Evgeniy Stepanov  // Test InsertAtEnd ICmpInst constructor.
3184802b9d6dc7443985066f0381c0a2468f72f9b81Evgeniy Stepanov  ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
3194802b9d6dc7443985066f0381c0a2468f72f9b81Evgeniy Stepanov  EXPECT_NE(ICmp0, ICmp2); // suppress warning.
3204802b9d6dc7443985066f0381c0a2468f72f9b81Evgeniy Stepanov
3211608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a);
3221608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b);
3231608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a);
3241608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b);
3251608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3261608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
3271608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
3281608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
3291608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
3301608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3311608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  Value *S0 = BTC0->stripPointerCasts();
3321608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  Value *S1 = BTC1->stripPointerCasts();
3331608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  Value *S2 = BTC2->stripPointerCasts();
3341608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  Value *S3 = BTC3->stripPointerCasts();
3351608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3361608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  EXPECT_NE(S0, Gep0);
3371608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  EXPECT_NE(S1, Gep1);
3381608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  EXPECT_NE(S2, Gep2);
3391608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  EXPECT_NE(S3, Gep3);
3401608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3411608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  int64_t Offset;
342791cfc211a9801002bfda6b3eb4de7e041f04f53Micah Villmow  DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
34336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
3441608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem                ":128:128-n8:16:32:64-S128");
3451608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  // Make sure we don't crash
346a070d2a0355c4993240b5206ebc1d517c151331dDan Gohman  GetPointerBaseWithConstantOffset(Gep0, Offset, &TD);
347a070d2a0355c4993240b5206ebc1d517c151331dDan Gohman  GetPointerBaseWithConstantOffset(Gep1, Offset, &TD);
348a070d2a0355c4993240b5206ebc1d517c151331dDan Gohman  GetPointerBaseWithConstantOffset(Gep2, Offset, &TD);
349a070d2a0355c4993240b5206ebc1d517c151331dDan Gohman  GetPointerBaseWithConstantOffset(Gep3, Offset, &TD);
3501608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3511608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  // Gep of Geps
3521608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b);
3531608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a);
3541608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b);
3551608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a);
3561608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3571608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  EXPECT_EQ(GepII0->getNumIndices(), 1u);
3581608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  EXPECT_EQ(GepII1->getNumIndices(), 1u);
3591608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  EXPECT_EQ(GepII2->getNumIndices(), 1u);
3601608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  EXPECT_EQ(GepII3->getNumIndices(), 1u);
3611608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3621608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  EXPECT_FALSE(GepII0->hasAllZeroIndices());
3631608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  EXPECT_FALSE(GepII1->hasAllZeroIndices());
3641608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  EXPECT_FALSE(GepII2->hasAllZeroIndices());
3651608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  EXPECT_FALSE(GepII3->hasAllZeroIndices());
3661608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3671608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete GepII0;
3681608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete GepII1;
3691608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete GepII2;
3701608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete GepII3;
3711608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3721608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete BTC0;
3731608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete BTC1;
3741608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete BTC2;
3751608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete BTC3;
3761608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3771608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete Gep0;
3781608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete Gep1;
3791608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete Gep2;
3801608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete Gep3;
3811608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3824802b9d6dc7443985066f0381c0a2468f72f9b81Evgeniy Stepanov  ICmp2->eraseFromParent();
3834802b9d6dc7443985066f0381c0a2468f72f9b81Evgeniy Stepanov  delete BB0;
3844802b9d6dc7443985066f0381c0a2468f72f9b81Evgeniy Stepanov
3851608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete ICmp0;
3861608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete ICmp1;
3871608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete PtrVecA;
3881608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem  delete PtrVecB;
3891608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem}
3901608769abeb1430dc34f31ffac0d9850f99ae36aNadav Rotem
3918883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan SandsTEST(InstructionsTest, FPMathOperator) {
3928883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan Sands  LLVMContext &Context = getGlobalContext();
3938883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan Sands  IRBuilder<> Builder(Context);
3948883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan Sands  MDBuilder MDHelper(Context);
3958883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan Sands  Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
3968883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan Sands  MDNode *MD1 = MDHelper.createFPMath(1.0);
3978883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan Sands  Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
3988883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan Sands  EXPECT_TRUE(isa<FPMathOperator>(V1));
3998883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan Sands  FPMathOperator *O1 = cast<FPMathOperator>(V1);
4008883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan Sands  EXPECT_EQ(O1->getFPAccuracy(), 1.0);
4018883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan Sands  delete V1;
4028883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan Sands  delete I;
4038883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan Sands}
4048883c43ddc13e5f92ba8dfe00f2116a153a570d5Duncan Sands
405446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands
406446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan SandsTEST(InstructionsTest, isEliminableCastPair) {
407446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands  LLVMContext &C(getGlobalContext());
408446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands
4093181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  Type* Int16Ty = Type::getInt16Ty(C);
410446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands  Type* Int32Ty = Type::getInt32Ty(C);
411446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands  Type* Int64Ty = Type::getInt64Ty(C);
412446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands  Type* Int64PtrTy = Type::getInt64PtrTy(C);
413446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands
414446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands  // Source and destination pointers have same size -> bitcast.
415446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands  EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
416446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands                                           CastInst::IntToPtr,
417446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands                                           Int64PtrTy, Int64Ty, Int64PtrTy,
418c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                                           Int32Ty, nullptr, Int32Ty),
419446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands            CastInst::BitCast);
420446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands
4213181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  // Source and destination have unknown sizes, but the same address space and
4223181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  // the intermediate int is the maximum pointer size -> bitcast
423446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands  EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
424446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands                                           CastInst::IntToPtr,
425446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands                                           Int64PtrTy, Int64Ty, Int64PtrTy,
426c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                                           nullptr, nullptr, nullptr),
4273181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault            CastInst::BitCast);
4283181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault
4293181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  // Source and destination have unknown sizes, but the same address space and
4303181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  // the intermediate int is not the maximum pointer size -> nothing
4313181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
4323181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault                                           CastInst::IntToPtr,
4333181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault                                           Int64PtrTy, Int32Ty, Int64PtrTy,
434c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                                           nullptr, nullptr, nullptr),
435446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands            0U);
436446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands
437446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands  // Middle pointer big enough -> bitcast.
438446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands  EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
439446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands                                           CastInst::PtrToInt,
440446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands                                           Int64Ty, Int64PtrTy, Int64Ty,
441c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                                           nullptr, Int64Ty, nullptr),
442446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands            CastInst::BitCast);
443446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands
444446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands  // Middle pointer too small -> fail.
445446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands  EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
446446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands                                           CastInst::PtrToInt,
447446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands                                           Int64Ty, Int64PtrTy, Int64Ty,
448c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                                           nullptr, Int32Ty, nullptr),
449446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands            0U);
4503181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault
4513181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  // Test that we don't eliminate bitcasts between different address spaces,
4523181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  // or if we don't have available pointer size information.
4533181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
4543181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault                "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
45536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
4563181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault
4573181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
4583181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
4593181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault
4603181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
4613181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
4623181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault
46359d3ae6cdc4316ad338cd848251f33a236ccb36cMatt Arsenault  // Cannot simplify inttoptr, addrspacecast
4643181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
46559d3ae6cdc4316ad338cd848251f33a236ccb36cMatt Arsenault                                           CastInst::AddrSpaceCast,
4663181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault                                           Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
467c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                                           nullptr, Int16SizePtr, Int64SizePtr),
4683181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault            0U);
4693181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault
47059d3ae6cdc4316ad338cd848251f33a236ccb36cMatt Arsenault  // Cannot simplify addrspacecast, ptrtoint
47159d3ae6cdc4316ad338cd848251f33a236ccb36cMatt Arsenault  EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
47259d3ae6cdc4316ad338cd848251f33a236ccb36cMatt Arsenault                                           CastInst::PtrToInt,
47359d3ae6cdc4316ad338cd848251f33a236ccb36cMatt Arsenault                                           Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
474c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                                           Int64SizePtr, Int16SizePtr, nullptr),
4753181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault            0U);
4763181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault
4773181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  // Pass since the bitcast address spaces are the same
4783181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault  EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
4793181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault                                           CastInst::BitCast,
4803181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault                                           Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
481c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                                           nullptr, nullptr, nullptr),
4823181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault            CastInst::IntToPtr);
4833181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault
484446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands}
485446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands
486dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen HinesTEST(InstructionsTest, CloneCall) {
487dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  LLVMContext &C(getGlobalContext());
488dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Type *Int32Ty = Type::getInt32Ty(C);
489dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty};
490dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Type *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false);
491dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
492dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Value *Args[] = {
493dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    ConstantInt::get(Int32Ty, 1),
494dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    ConstantInt::get(Int32Ty, 2),
495dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    ConstantInt::get(Int32Ty, 3)
496dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  };
497dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  std::unique_ptr<CallInst> Call(CallInst::Create(Callee, Args, "result"));
498dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
499dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // Test cloning the tail call kind.
500dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail,
501dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                    CallInst::TCK_MustTail};
502dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  for (CallInst::TailCallKind TCK : Kinds) {
503dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Call->setTailCallKind(TCK);
504dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
505dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
506dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
507dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Call->setTailCallKind(CallInst::TCK_None);
508dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
509dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // Test cloning an attribute.
510dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  {
511dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    AttrBuilder AB;
512dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    AB.addAttribute(Attribute::ReadOnly);
513dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Call->setAttributes(AttributeSet::get(C, AttributeSet::FunctionIndex, AB));
514dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
515dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    EXPECT_TRUE(Clone->onlyReadsMemory());
516dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
517dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
518dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
5195a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif}  // end anonymous namespace
5205a7069a80fee26490b201cb3d88eb124585bd6c8Gabor Greif}  // end namespace llvm
5213181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault
5223181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault
523