2013-01-07 23:35:46 +08:00
|
|
|
//===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
|
2010-03-16 17:55:46 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2012-12-04 18:23:08 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-03-27 04:41:15 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/MDBuilder.h"
|
2014-03-27 04:41:15 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2017-02-24 06:50:52 +08:00
|
|
|
#include "llvm/IR/NoFolder.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Operator.h"
|
2017-05-08 20:40:18 +08:00
|
|
|
#include "gmock/gmock-matchers.h"
|
2010-03-16 17:55:46 +08:00
|
|
|
#include "gtest/gtest.h"
|
2014-03-27 04:41:15 +08:00
|
|
|
#include <memory>
|
2010-03-16 17:55:46 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace {
|
|
|
|
|
2010-03-16 18:59:48 +08:00
|
|
|
TEST(InstructionsTest, ReturnInst) {
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext C;
|
2010-03-16 17:55:46 +08:00
|
|
|
|
2010-03-16 18:59:48 +08:00
|
|
|
// test for PR6589
|
2010-03-16 17:55:46 +08:00
|
|
|
const ReturnInst* r0 = ReturnInst::Create(C);
|
2010-03-16 23:26:09 +08:00
|
|
|
EXPECT_EQ(r0->getNumOperands(), 0U);
|
2010-03-16 18:59:48 +08:00
|
|
|
EXPECT_EQ(r0->op_begin(), r0->op_end());
|
2010-03-16 19:24:53 +08:00
|
|
|
|
2011-07-18 12:54:35 +08:00
|
|
|
IntegerType* Int1 = IntegerType::get(C, 1);
|
2010-03-16 19:24:53 +08:00
|
|
|
Constant* One = ConstantInt::get(Int1, 1, true);
|
|
|
|
const ReturnInst* r1 = ReturnInst::Create(C, One);
|
2011-08-28 03:23:22 +08:00
|
|
|
EXPECT_EQ(1U, r1->getNumOperands());
|
2010-03-16 19:24:53 +08:00
|
|
|
User::const_op_iterator b(r1->op_begin());
|
2011-08-28 03:23:22 +08:00
|
|
|
EXPECT_NE(r1->op_end(), b);
|
|
|
|
EXPECT_EQ(One, *b);
|
|
|
|
EXPECT_EQ(One, r1->getOperand(0));
|
2010-03-16 19:24:53 +08:00
|
|
|
++b;
|
2011-08-28 03:23:22 +08:00
|
|
|
EXPECT_EQ(r1->op_end(), b);
|
2010-03-16 20:32:03 +08:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
delete r0;
|
|
|
|
delete r1;
|
2010-03-16 17:55:46 +08:00
|
|
|
}
|
|
|
|
|
2014-03-27 05:46:24 +08:00
|
|
|
// Test fixture that provides a module and a single function within it. Useful
|
|
|
|
// for tests that need to refer to the function in some way.
|
|
|
|
class ModuleWithFunctionTest : public testing::Test {
|
|
|
|
protected:
|
2014-03-27 19:32:41 +08:00
|
|
|
ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) {
|
2014-03-27 19:38:28 +08:00
|
|
|
FArgTypes.push_back(Type::getInt8Ty(Ctx));
|
|
|
|
FArgTypes.push_back(Type::getInt32Ty(Ctx));
|
|
|
|
FArgTypes.push_back(Type::getInt64Ty(Ctx));
|
2014-03-27 05:46:24 +08:00
|
|
|
FunctionType *FTy =
|
|
|
|
FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
|
|
|
|
F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
|
|
|
|
}
|
2014-03-27 04:41:15 +08:00
|
|
|
|
2014-03-27 05:46:24 +08:00
|
|
|
LLVMContext Ctx;
|
|
|
|
std::unique_ptr<Module> M;
|
2014-03-27 19:33:11 +08:00
|
|
|
SmallVector<Type *, 3> FArgTypes;
|
2014-03-27 05:46:24 +08:00
|
|
|
Function *F;
|
|
|
|
};
|
2014-03-27 04:41:15 +08:00
|
|
|
|
2014-03-27 05:46:24 +08:00
|
|
|
TEST_F(ModuleWithFunctionTest, CallInst) {
|
|
|
|
Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
|
|
|
|
ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
|
|
|
|
ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
|
2014-03-27 05:11:34 +08:00
|
|
|
std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
|
2014-03-27 04:41:15 +08:00
|
|
|
|
|
|
|
// Make sure iteration over a call's arguments works as expected.
|
|
|
|
unsigned Idx = 0;
|
|
|
|
for (Value *Arg : Call->arg_operands()) {
|
2014-03-27 05:46:24 +08:00
|
|
|
EXPECT_EQ(FArgTypes[Idx], Arg->getType());
|
2014-03-27 04:41:15 +08:00
|
|
|
EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
|
|
|
|
Idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-27 05:46:24 +08:00
|
|
|
TEST_F(ModuleWithFunctionTest, InvokeInst) {
|
|
|
|
BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
|
|
|
|
BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F);
|
|
|
|
|
|
|
|
Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
|
|
|
|
ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
|
|
|
|
ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
|
|
|
|
std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args));
|
|
|
|
|
|
|
|
// Make sure iteration over invoke's arguments works as expected.
|
|
|
|
unsigned Idx = 0;
|
|
|
|
for (Value *Arg : Invoke->arg_operands()) {
|
|
|
|
EXPECT_EQ(FArgTypes[Idx], Arg->getType());
|
|
|
|
EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType());
|
|
|
|
Idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-16 23:26:09 +08:00
|
|
|
TEST(InstructionsTest, BranchInst) {
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext C;
|
2010-03-16 23:26:09 +08:00
|
|
|
|
|
|
|
// Make a BasicBlocks
|
|
|
|
BasicBlock* bb0 = BasicBlock::Create(C);
|
|
|
|
BasicBlock* bb1 = BasicBlock::Create(C);
|
|
|
|
|
|
|
|
// Mandatory BranchInst
|
|
|
|
const BranchInst* b0 = BranchInst::Create(bb0);
|
|
|
|
|
2010-03-16 23:53:58 +08:00
|
|
|
EXPECT_TRUE(b0->isUnconditional());
|
|
|
|
EXPECT_FALSE(b0->isConditional());
|
2011-08-28 03:23:22 +08:00
|
|
|
EXPECT_EQ(1U, b0->getNumSuccessors());
|
2010-03-16 23:53:58 +08:00
|
|
|
|
2010-03-16 23:26:09 +08:00
|
|
|
// check num operands
|
2011-08-28 03:23:22 +08:00
|
|
|
EXPECT_EQ(1U, b0->getNumOperands());
|
2010-03-16 23:26:09 +08:00
|
|
|
|
|
|
|
EXPECT_NE(b0->op_begin(), b0->op_end());
|
2014-03-02 20:27:27 +08:00
|
|
|
EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
|
2010-03-16 23:53:58 +08:00
|
|
|
|
2014-03-02 20:27:27 +08:00
|
|
|
EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
|
2010-03-16 23:26:09 +08:00
|
|
|
|
2011-07-18 12:54:35 +08:00
|
|
|
IntegerType* Int1 = IntegerType::get(C, 1);
|
2010-03-16 23:26:09 +08:00
|
|
|
Constant* One = ConstantInt::get(Int1, 1, true);
|
|
|
|
|
|
|
|
// Conditional BranchInst
|
|
|
|
BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
|
|
|
|
|
2010-03-16 23:53:58 +08:00
|
|
|
EXPECT_FALSE(b1->isUnconditional());
|
|
|
|
EXPECT_TRUE(b1->isConditional());
|
2011-08-28 03:23:22 +08:00
|
|
|
EXPECT_EQ(2U, b1->getNumSuccessors());
|
2010-03-16 23:53:58 +08:00
|
|
|
|
2010-03-16 23:26:09 +08:00
|
|
|
// check num operands
|
2011-08-28 03:23:22 +08:00
|
|
|
EXPECT_EQ(3U, b1->getNumOperands());
|
2010-03-16 23:26:09 +08:00
|
|
|
|
|
|
|
User::const_op_iterator b(b1->op_begin());
|
|
|
|
|
|
|
|
// check COND
|
|
|
|
EXPECT_NE(b, b1->op_end());
|
2011-08-28 03:23:22 +08:00
|
|
|
EXPECT_EQ(One, *b);
|
|
|
|
EXPECT_EQ(One, b1->getOperand(0));
|
|
|
|
EXPECT_EQ(One, b1->getCondition());
|
2010-03-16 23:26:09 +08:00
|
|
|
++b;
|
|
|
|
|
|
|
|
// check ELSE
|
2011-08-28 03:23:22 +08:00
|
|
|
EXPECT_EQ(bb1, *b);
|
|
|
|
EXPECT_EQ(bb1, b1->getOperand(1));
|
|
|
|
EXPECT_EQ(bb1, b1->getSuccessor(1));
|
2010-03-16 23:26:09 +08:00
|
|
|
++b;
|
|
|
|
|
|
|
|
// check THEN
|
2011-08-28 03:23:22 +08:00
|
|
|
EXPECT_EQ(bb0, *b);
|
|
|
|
EXPECT_EQ(bb0, b1->getOperand(2));
|
|
|
|
EXPECT_EQ(bb0, b1->getSuccessor(0));
|
2010-03-16 23:26:09 +08:00
|
|
|
++b;
|
|
|
|
|
2011-08-28 03:23:22 +08:00
|
|
|
EXPECT_EQ(b1->op_end(), b);
|
2010-03-16 23:26:09 +08:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
delete b0;
|
|
|
|
delete b1;
|
|
|
|
|
|
|
|
delete bb0;
|
|
|
|
delete bb1;
|
|
|
|
}
|
|
|
|
|
2011-04-01 11:34:54 +08:00
|
|
|
TEST(InstructionsTest, CastInst) {
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext C;
|
2011-04-01 11:34:54 +08:00
|
|
|
|
2013-07-31 04:45:05 +08:00
|
|
|
Type *Int8Ty = Type::getInt8Ty(C);
|
|
|
|
Type *Int16Ty = Type::getInt16Ty(C);
|
|
|
|
Type *Int32Ty = Type::getInt32Ty(C);
|
|
|
|
Type *Int64Ty = Type::getInt64Ty(C);
|
|
|
|
Type *V8x8Ty = VectorType::get(Int8Ty, 8);
|
|
|
|
Type *V8x64Ty = VectorType::get(Int64Ty, 8);
|
|
|
|
Type *X86MMXTy = Type::getX86_MMXTy(C);
|
|
|
|
|
|
|
|
Type *HalfTy = Type::getHalfTy(C);
|
|
|
|
Type *FloatTy = Type::getFloatTy(C);
|
|
|
|
Type *DoubleTy = Type::getDoubleTy(C);
|
|
|
|
|
|
|
|
Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
|
|
|
|
Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
|
|
|
|
Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
|
|
|
|
|
|
|
|
Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
|
|
|
|
Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
|
|
|
|
|
|
|
|
Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
|
|
|
|
Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
|
|
|
|
|
|
|
|
Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
|
|
|
|
Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
|
|
|
|
Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
|
|
|
|
Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
|
|
|
|
|
|
|
|
Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
|
|
|
|
Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
|
2014-01-23 03:21:33 +08:00
|
|
|
Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
|
2011-04-01 11:34:54 +08:00
|
|
|
|
2011-05-18 15:13:41 +08:00
|
|
|
const Constant* c8 = Constant::getNullValue(V8x8Ty);
|
|
|
|
const Constant* c64 = Constant::getNullValue(V8x64Ty);
|
|
|
|
|
2013-11-15 09:34:59 +08:00
|
|
|
const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
|
|
|
|
|
2013-07-31 06:02:14 +08:00
|
|
|
EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
|
|
|
|
EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
|
|
|
|
EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
|
|
|
|
EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
|
|
|
|
EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
|
2011-08-28 03:23:22 +08:00
|
|
|
EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
|
|
|
|
EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
|
2011-12-05 14:29:09 +08:00
|
|
|
|
2013-07-31 04:45:05 +08:00
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
|
|
|
|
|
|
|
|
// Check address space casts are rejected since we don't know the sizes here
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
|
|
|
|
EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
|
2013-11-15 09:34:59 +08:00
|
|
|
EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
|
|
|
|
EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
|
|
|
|
V2Int32PtrAS1Ty,
|
|
|
|
true));
|
2013-07-31 04:45:05 +08:00
|
|
|
|
|
|
|
// Test mismatched number of elements for pointers
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
|
|
|
|
|
|
|
|
EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
|
|
|
|
EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
|
|
|
|
EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
|
|
|
|
EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
|
|
|
|
EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
|
|
|
|
EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
|
|
|
|
EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
|
|
|
|
|
|
|
|
EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
|
|
|
|
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
|
|
|
|
EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
|
|
|
|
EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
|
|
|
|
EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
|
2013-07-31 08:17:33 +08:00
|
|
|
|
|
|
|
|
2014-01-23 03:21:33 +08:00
|
|
|
EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
|
|
|
|
Constant::getNullValue(V4Int32PtrTy),
|
|
|
|
V2Int32PtrTy));
|
|
|
|
EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
|
|
|
|
Constant::getNullValue(V2Int32PtrTy),
|
|
|
|
V4Int32PtrTy));
|
|
|
|
|
|
|
|
EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
|
|
|
|
Constant::getNullValue(V4Int32PtrAS1Ty),
|
|
|
|
V2Int32PtrTy));
|
|
|
|
EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
|
|
|
|
Constant::getNullValue(V2Int32PtrTy),
|
|
|
|
V4Int32PtrAS1Ty));
|
|
|
|
|
|
|
|
|
2013-07-31 08:17:33 +08:00
|
|
|
// Check that assertion is not hit when creating a cast with a vector of
|
|
|
|
// pointers
|
|
|
|
// First form
|
|
|
|
BasicBlock *BB = BasicBlock::Create(C);
|
|
|
|
Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
|
2016-04-15 05:59:01 +08:00
|
|
|
auto Inst1 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
|
2013-07-31 08:17:33 +08:00
|
|
|
|
|
|
|
// Second form
|
2016-04-15 05:59:01 +08:00
|
|
|
auto Inst2 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
|
|
|
|
|
|
|
|
delete Inst2;
|
|
|
|
Inst1->eraseFromParent();
|
|
|
|
delete BB;
|
2013-07-31 04:45:05 +08:00
|
|
|
}
|
2011-12-05 14:29:09 +08:00
|
|
|
|
|
|
|
TEST(InstructionsTest, VectorGep) {
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext C;
|
2011-12-05 14:29:09 +08:00
|
|
|
|
|
|
|
// Type Definitions
|
2015-03-15 05:40:10 +08:00
|
|
|
Type *I8Ty = IntegerType::get(C, 8);
|
|
|
|
Type *I32Ty = IntegerType::get(C, 32);
|
|
|
|
PointerType *Ptri8Ty = PointerType::get(I8Ty, 0);
|
|
|
|
PointerType *Ptri32Ty = PointerType::get(I32Ty, 0);
|
2011-12-05 14:29:09 +08:00
|
|
|
|
|
|
|
VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
|
|
|
|
VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
|
|
|
|
|
|
|
|
// Test different aspects of the vector-of-pointers type
|
|
|
|
// and GEPs which use this type.
|
|
|
|
ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
|
|
|
|
ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
|
|
|
|
std::vector<Constant*> ConstVa(2, Ci32a);
|
|
|
|
std::vector<Constant*> ConstVb(2, Ci32b);
|
|
|
|
Constant *C2xi32a = ConstantVector::get(ConstVa);
|
|
|
|
Constant *C2xi32b = ConstantVector::get(ConstVb);
|
|
|
|
|
|
|
|
CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
|
|
|
|
CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
|
|
|
|
|
|
|
|
ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
|
|
|
|
ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
|
|
|
|
EXPECT_NE(ICmp0, ICmp1); // suppress warning.
|
|
|
|
|
2013-01-16 22:38:50 +08:00
|
|
|
BasicBlock* BB0 = BasicBlock::Create(C);
|
|
|
|
// Test InsertAtEnd ICmpInst constructor.
|
|
|
|
ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
|
|
|
|
EXPECT_NE(ICmp0, ICmp2); // suppress warning.
|
|
|
|
|
2015-03-15 05:40:10 +08:00
|
|
|
GetElementPtrInst *Gep0 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32a);
|
|
|
|
GetElementPtrInst *Gep1 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32b);
|
|
|
|
GetElementPtrInst *Gep2 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32a);
|
|
|
|
GetElementPtrInst *Gep3 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32b);
|
2011-12-05 14:29:09 +08:00
|
|
|
|
|
|
|
CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
|
|
|
|
CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
|
|
|
|
CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
|
|
|
|
CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
|
|
|
|
|
|
|
|
Value *S0 = BTC0->stripPointerCasts();
|
|
|
|
Value *S1 = BTC1->stripPointerCasts();
|
|
|
|
Value *S2 = BTC2->stripPointerCasts();
|
|
|
|
Value *S3 = BTC3->stripPointerCasts();
|
|
|
|
|
|
|
|
EXPECT_NE(S0, Gep0);
|
|
|
|
EXPECT_NE(S1, Gep1);
|
|
|
|
EXPECT_NE(S2, Gep2);
|
|
|
|
EXPECT_NE(S3, Gep3);
|
|
|
|
|
|
|
|
int64_t Offset;
|
2012-10-09 00:39:34 +08:00
|
|
|
DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
|
2013-12-14 02:56:34 +08:00
|
|
|
"2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
|
2011-12-05 14:29:09 +08:00
|
|
|
":128:128-n8:16:32:64-S128");
|
|
|
|
// Make sure we don't crash
|
2015-03-10 10:37:25 +08:00
|
|
|
GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
|
|
|
|
GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
|
|
|
|
GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
|
|
|
|
GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
|
2011-12-05 14:29:09 +08:00
|
|
|
|
|
|
|
// Gep of Geps
|
2015-03-15 05:40:10 +08:00
|
|
|
GetElementPtrInst *GepII0 = GetElementPtrInst::Create(I32Ty, Gep0, C2xi32b);
|
|
|
|
GetElementPtrInst *GepII1 = GetElementPtrInst::Create(I32Ty, Gep1, C2xi32a);
|
|
|
|
GetElementPtrInst *GepII2 = GetElementPtrInst::Create(I32Ty, Gep2, C2xi32b);
|
|
|
|
GetElementPtrInst *GepII3 = GetElementPtrInst::Create(I32Ty, Gep3, C2xi32a);
|
2011-12-05 14:29:09 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(GepII0->getNumIndices(), 1u);
|
|
|
|
EXPECT_EQ(GepII1->getNumIndices(), 1u);
|
|
|
|
EXPECT_EQ(GepII2->getNumIndices(), 1u);
|
|
|
|
EXPECT_EQ(GepII3->getNumIndices(), 1u);
|
|
|
|
|
|
|
|
EXPECT_FALSE(GepII0->hasAllZeroIndices());
|
|
|
|
EXPECT_FALSE(GepII1->hasAllZeroIndices());
|
|
|
|
EXPECT_FALSE(GepII2->hasAllZeroIndices());
|
|
|
|
EXPECT_FALSE(GepII3->hasAllZeroIndices());
|
|
|
|
|
|
|
|
delete GepII0;
|
|
|
|
delete GepII1;
|
|
|
|
delete GepII2;
|
|
|
|
delete GepII3;
|
|
|
|
|
|
|
|
delete BTC0;
|
|
|
|
delete BTC1;
|
|
|
|
delete BTC2;
|
|
|
|
delete BTC3;
|
|
|
|
|
|
|
|
delete Gep0;
|
|
|
|
delete Gep1;
|
|
|
|
delete Gep2;
|
|
|
|
delete Gep3;
|
|
|
|
|
2013-01-16 22:38:50 +08:00
|
|
|
ICmp2->eraseFromParent();
|
|
|
|
delete BB0;
|
|
|
|
|
2011-12-05 14:29:09 +08:00
|
|
|
delete ICmp0;
|
|
|
|
delete ICmp1;
|
|
|
|
delete PtrVecA;
|
|
|
|
delete PtrVecB;
|
|
|
|
}
|
|
|
|
|
2012-04-17 00:28:59 +08:00
|
|
|
TEST(InstructionsTest, FPMathOperator) {
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext Context;
|
2012-04-17 00:28:59 +08:00
|
|
|
IRBuilder<> Builder(Context);
|
|
|
|
MDBuilder MDHelper(Context);
|
|
|
|
Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
|
|
|
|
MDNode *MD1 = MDHelper.createFPMath(1.0);
|
|
|
|
Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
|
|
|
|
EXPECT_TRUE(isa<FPMathOperator>(V1));
|
|
|
|
FPMathOperator *O1 = cast<FPMathOperator>(V1);
|
|
|
|
EXPECT_EQ(O1->getFPAccuracy(), 1.0);
|
|
|
|
delete V1;
|
|
|
|
delete I;
|
|
|
|
}
|
|
|
|
|
2012-10-31 00:03:32 +08:00
|
|
|
|
|
|
|
TEST(InstructionsTest, isEliminableCastPair) {
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext C;
|
2012-10-31 00:03:32 +08:00
|
|
|
|
2013-07-31 06:27:10 +08:00
|
|
|
Type* Int16Ty = Type::getInt16Ty(C);
|
2012-10-31 00:03:32 +08:00
|
|
|
Type* Int32Ty = Type::getInt32Ty(C);
|
|
|
|
Type* Int64Ty = Type::getInt64Ty(C);
|
|
|
|
Type* Int64PtrTy = Type::getInt64PtrTy(C);
|
|
|
|
|
|
|
|
// Source and destination pointers have same size -> bitcast.
|
|
|
|
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
|
|
|
|
CastInst::IntToPtr,
|
|
|
|
Int64PtrTy, Int64Ty, Int64PtrTy,
|
2014-06-09 06:29:17 +08:00
|
|
|
Int32Ty, nullptr, Int32Ty),
|
2012-10-31 00:03:32 +08:00
|
|
|
CastInst::BitCast);
|
|
|
|
|
2013-07-31 06:27:10 +08:00
|
|
|
// Source and destination have unknown sizes, but the same address space and
|
|
|
|
// the intermediate int is the maximum pointer size -> bitcast
|
2012-10-31 00:03:32 +08:00
|
|
|
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
|
|
|
|
CastInst::IntToPtr,
|
|
|
|
Int64PtrTy, Int64Ty, Int64PtrTy,
|
2014-06-09 06:29:17 +08:00
|
|
|
nullptr, nullptr, nullptr),
|
2013-07-31 06:27:10 +08:00
|
|
|
CastInst::BitCast);
|
|
|
|
|
|
|
|
// Source and destination have unknown sizes, but the same address space and
|
|
|
|
// the intermediate int is not the maximum pointer size -> nothing
|
|
|
|
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
|
|
|
|
CastInst::IntToPtr,
|
|
|
|
Int64PtrTy, Int32Ty, Int64PtrTy,
|
2014-06-09 06:29:17 +08:00
|
|
|
nullptr, nullptr, nullptr),
|
2012-10-31 00:03:32 +08:00
|
|
|
0U);
|
|
|
|
|
|
|
|
// Middle pointer big enough -> bitcast.
|
|
|
|
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
|
|
|
|
CastInst::PtrToInt,
|
|
|
|
Int64Ty, Int64PtrTy, Int64Ty,
|
2014-06-09 06:29:17 +08:00
|
|
|
nullptr, Int64Ty, nullptr),
|
2012-10-31 00:03:32 +08:00
|
|
|
CastInst::BitCast);
|
|
|
|
|
|
|
|
// Middle pointer too small -> fail.
|
|
|
|
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
|
|
|
|
CastInst::PtrToInt,
|
|
|
|
Int64Ty, Int64PtrTy, Int64Ty,
|
2014-06-09 06:29:17 +08:00
|
|
|
nullptr, Int32Ty, nullptr),
|
2012-10-31 00:03:32 +08:00
|
|
|
0U);
|
2013-07-31 06:27:10 +08:00
|
|
|
|
|
|
|
// Test that we don't eliminate bitcasts between different address spaces,
|
|
|
|
// or if we don't have available pointer size information.
|
|
|
|
DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
|
|
|
|
"-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
|
2013-12-14 02:56:34 +08:00
|
|
|
"-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
|
2013-07-31 06:27:10 +08:00
|
|
|
|
|
|
|
Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
|
|
|
|
Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
|
|
|
|
|
|
|
|
IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
|
|
|
|
IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
|
|
|
|
|
2013-11-15 09:34:59 +08:00
|
|
|
// Cannot simplify inttoptr, addrspacecast
|
2013-07-31 06:27:10 +08:00
|
|
|
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
|
2013-11-15 09:34:59 +08:00
|
|
|
CastInst::AddrSpaceCast,
|
2013-07-31 06:27:10 +08:00
|
|
|
Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
|
2014-06-09 06:29:17 +08:00
|
|
|
nullptr, Int16SizePtr, Int64SizePtr),
|
2013-07-31 06:27:10 +08:00
|
|
|
0U);
|
|
|
|
|
2013-11-15 09:34:59 +08:00
|
|
|
// Cannot simplify addrspacecast, ptrtoint
|
|
|
|
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
|
|
|
|
CastInst::PtrToInt,
|
|
|
|
Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
|
2014-06-09 06:29:17 +08:00
|
|
|
Int64SizePtr, Int16SizePtr, nullptr),
|
2013-07-31 06:27:10 +08:00
|
|
|
0U);
|
|
|
|
|
|
|
|
// Pass since the bitcast address spaces are the same
|
|
|
|
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
|
|
|
|
CastInst::BitCast,
|
|
|
|
Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
|
2014-06-09 06:29:17 +08:00
|
|
|
nullptr, nullptr, nullptr),
|
2013-07-31 06:27:10 +08:00
|
|
|
CastInst::IntToPtr);
|
|
|
|
|
2012-10-31 00:03:32 +08:00
|
|
|
}
|
|
|
|
|
2014-05-07 04:08:20 +08:00
|
|
|
TEST(InstructionsTest, CloneCall) {
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext C;
|
2014-05-07 04:08:20 +08:00
|
|
|
Type *Int32Ty = Type::getInt32Ty(C);
|
|
|
|
Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty};
|
|
|
|
Type *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false);
|
|
|
|
Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
|
|
|
|
Value *Args[] = {
|
|
|
|
ConstantInt::get(Int32Ty, 1),
|
|
|
|
ConstantInt::get(Int32Ty, 2),
|
|
|
|
ConstantInt::get(Int32Ty, 3)
|
|
|
|
};
|
|
|
|
std::unique_ptr<CallInst> Call(CallInst::Create(Callee, Args, "result"));
|
|
|
|
|
|
|
|
// Test cloning the tail call kind.
|
|
|
|
CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail,
|
|
|
|
CallInst::TCK_MustTail};
|
|
|
|
for (CallInst::TailCallKind TCK : Kinds) {
|
|
|
|
Call->setTailCallKind(TCK);
|
|
|
|
std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
|
|
|
|
EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
|
|
|
|
}
|
|
|
|
Call->setTailCallKind(CallInst::TCK_None);
|
|
|
|
|
|
|
|
// Test cloning an attribute.
|
|
|
|
{
|
|
|
|
AttrBuilder AB;
|
|
|
|
AB.addAttribute(Attribute::ReadOnly);
|
Rename AttributeSet to AttributeList
Summary:
This class is a list of AttributeSetNodes corresponding the function
prototype of a call or function declaration. This class used to be
called ParamAttrListPtr, then AttrListPtr, then AttributeSet. It is
typically accessed by parameter and return value index, so
"AttributeList" seems like a more intuitive name.
Rename AttributeSetImpl to AttributeListImpl to follow suit.
It's useful to rename this class so that we can rename AttributeSetNode
to AttributeSet later. AttributeSet is the set of attributes that apply
to a single function, argument, or return value.
Reviewers: sanjoy, javed.absar, chandlerc, pete
Reviewed By: pete
Subscribers: pete, jholewinski, arsenm, dschuff, mehdi_amini, jfb, nhaehnle, sbc100, void, llvm-commits
Differential Revision: https://reviews.llvm.org/D31102
llvm-svn: 298393
2017-03-22 00:57:19 +08:00
|
|
|
Call->setAttributes(
|
|
|
|
AttributeList::get(C, AttributeList::FunctionIndex, AB));
|
2014-05-07 04:08:20 +08:00
|
|
|
std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
|
|
|
|
EXPECT_TRUE(Clone->onlyReadsMemory());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 14:21:42 +08:00
|
|
|
TEST(InstructionsTest, AlterCallBundles) {
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext C;
|
2016-01-14 14:21:42 +08:00
|
|
|
Type *Int32Ty = Type::getInt32Ty(C);
|
|
|
|
Type *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
|
|
|
|
Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
|
|
|
|
Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
|
|
|
|
OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
|
|
|
|
std::unique_ptr<CallInst> Call(
|
|
|
|
CallInst::Create(Callee, Args, OldBundle, "result"));
|
|
|
|
Call->setTailCallKind(CallInst::TailCallKind::TCK_NoTail);
|
|
|
|
AttrBuilder AB;
|
|
|
|
AB.addAttribute(Attribute::Cold);
|
Rename AttributeSet to AttributeList
Summary:
This class is a list of AttributeSetNodes corresponding the function
prototype of a call or function declaration. This class used to be
called ParamAttrListPtr, then AttrListPtr, then AttributeSet. It is
typically accessed by parameter and return value index, so
"AttributeList" seems like a more intuitive name.
Rename AttributeSetImpl to AttributeListImpl to follow suit.
It's useful to rename this class so that we can rename AttributeSetNode
to AttributeSet later. AttributeSet is the set of attributes that apply
to a single function, argument, or return value.
Reviewers: sanjoy, javed.absar, chandlerc, pete
Reviewed By: pete
Subscribers: pete, jholewinski, arsenm, dschuff, mehdi_amini, jfb, nhaehnle, sbc100, void, llvm-commits
Differential Revision: https://reviews.llvm.org/D31102
llvm-svn: 298393
2017-03-22 00:57:19 +08:00
|
|
|
Call->setAttributes(AttributeList::get(C, AttributeList::FunctionIndex, AB));
|
2016-01-14 14:21:42 +08:00
|
|
|
Call->setDebugLoc(DebugLoc(MDNode::get(C, None)));
|
|
|
|
|
|
|
|
OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
|
|
|
|
std::unique_ptr<CallInst> Clone(CallInst::Create(Call.get(), NewBundle));
|
|
|
|
EXPECT_EQ(Call->getNumArgOperands(), Clone->getNumArgOperands());
|
|
|
|
EXPECT_EQ(Call->getArgOperand(0), Clone->getArgOperand(0));
|
|
|
|
EXPECT_EQ(Call->getCallingConv(), Clone->getCallingConv());
|
|
|
|
EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
|
|
|
|
EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
|
|
|
|
EXPECT_EQ(Call->getDebugLoc(), Clone->getDebugLoc());
|
2016-01-14 14:30:19 +08:00
|
|
|
EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
|
2016-01-14 14:21:42 +08:00
|
|
|
EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
|
|
|
|
}
|
2013-07-31 06:27:10 +08:00
|
|
|
|
2016-01-14 14:21:42 +08:00
|
|
|
TEST(InstructionsTest, AlterInvokeBundles) {
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext C;
|
2016-01-14 14:21:42 +08:00
|
|
|
Type *Int32Ty = Type::getInt32Ty(C);
|
|
|
|
Type *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
|
|
|
|
Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
|
|
|
|
Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
|
2016-01-15 23:08:36 +08:00
|
|
|
std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C));
|
|
|
|
std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C));
|
2016-01-14 14:21:42 +08:00
|
|
|
OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
|
2016-01-15 23:08:36 +08:00
|
|
|
std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(
|
|
|
|
Callee, NormalDest.get(), UnwindDest.get(), Args, OldBundle, "result"));
|
2016-01-14 14:21:42 +08:00
|
|
|
AttrBuilder AB;
|
|
|
|
AB.addAttribute(Attribute::Cold);
|
Rename AttributeSet to AttributeList
Summary:
This class is a list of AttributeSetNodes corresponding the function
prototype of a call or function declaration. This class used to be
called ParamAttrListPtr, then AttrListPtr, then AttributeSet. It is
typically accessed by parameter and return value index, so
"AttributeList" seems like a more intuitive name.
Rename AttributeSetImpl to AttributeListImpl to follow suit.
It's useful to rename this class so that we can rename AttributeSetNode
to AttributeSet later. AttributeSet is the set of attributes that apply
to a single function, argument, or return value.
Reviewers: sanjoy, javed.absar, chandlerc, pete
Reviewed By: pete
Subscribers: pete, jholewinski, arsenm, dschuff, mehdi_amini, jfb, nhaehnle, sbc100, void, llvm-commits
Differential Revision: https://reviews.llvm.org/D31102
llvm-svn: 298393
2017-03-22 00:57:19 +08:00
|
|
|
Invoke->setAttributes(
|
|
|
|
AttributeList::get(C, AttributeList::FunctionIndex, AB));
|
2016-01-14 14:21:42 +08:00
|
|
|
Invoke->setDebugLoc(DebugLoc(MDNode::get(C, None)));
|
|
|
|
|
|
|
|
OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
|
2016-01-15 23:08:36 +08:00
|
|
|
std::unique_ptr<InvokeInst> Clone(
|
|
|
|
InvokeInst::Create(Invoke.get(), NewBundle));
|
2016-01-14 14:21:42 +08:00
|
|
|
EXPECT_EQ(Invoke->getNormalDest(), Clone->getNormalDest());
|
|
|
|
EXPECT_EQ(Invoke->getUnwindDest(), Clone->getUnwindDest());
|
|
|
|
EXPECT_EQ(Invoke->getNumArgOperands(), Clone->getNumArgOperands());
|
|
|
|
EXPECT_EQ(Invoke->getArgOperand(0), Clone->getArgOperand(0));
|
|
|
|
EXPECT_EQ(Invoke->getCallingConv(), Clone->getCallingConv());
|
|
|
|
EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
|
|
|
|
EXPECT_EQ(Invoke->getDebugLoc(), Clone->getDebugLoc());
|
2016-01-14 17:21:49 +08:00
|
|
|
EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
|
2016-01-14 14:21:42 +08:00
|
|
|
EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
|
|
|
|
}
|
2013-07-31 06:27:10 +08:00
|
|
|
|
2017-02-24 06:50:52 +08:00
|
|
|
TEST_F(ModuleWithFunctionTest, DropPoisonGeneratingFlags) {
|
|
|
|
auto *OnlyBB = BasicBlock::Create(Ctx, "bb", F);
|
|
|
|
auto *Arg0 = &*F->arg_begin();
|
|
|
|
|
|
|
|
IRBuilder<NoFolder> B(Ctx);
|
|
|
|
B.SetInsertPoint(OnlyBB);
|
|
|
|
|
|
|
|
{
|
|
|
|
auto *UI =
|
|
|
|
cast<Instruction>(B.CreateUDiv(Arg0, Arg0, "", /*isExact*/ true));
|
|
|
|
ASSERT_TRUE(UI->isExact());
|
|
|
|
UI->dropPoisonGeneratingFlags();
|
|
|
|
ASSERT_FALSE(UI->isExact());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto *ShrI =
|
|
|
|
cast<Instruction>(B.CreateLShr(Arg0, Arg0, "", /*isExact*/ true));
|
|
|
|
ASSERT_TRUE(ShrI->isExact());
|
|
|
|
ShrI->dropPoisonGeneratingFlags();
|
|
|
|
ASSERT_FALSE(ShrI->isExact());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto *AI = cast<Instruction>(
|
|
|
|
B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ false));
|
|
|
|
ASSERT_TRUE(AI->hasNoUnsignedWrap());
|
|
|
|
AI->dropPoisonGeneratingFlags();
|
|
|
|
ASSERT_FALSE(AI->hasNoUnsignedWrap());
|
|
|
|
ASSERT_FALSE(AI->hasNoSignedWrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto *SI = cast<Instruction>(
|
|
|
|
B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ false, /*HasNSW*/ true));
|
|
|
|
ASSERT_TRUE(SI->hasNoSignedWrap());
|
|
|
|
SI->dropPoisonGeneratingFlags();
|
|
|
|
ASSERT_FALSE(SI->hasNoUnsignedWrap());
|
|
|
|
ASSERT_FALSE(SI->hasNoSignedWrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto *ShlI = cast<Instruction>(
|
|
|
|
B.CreateShl(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ true));
|
|
|
|
ASSERT_TRUE(ShlI->hasNoSignedWrap());
|
|
|
|
ASSERT_TRUE(ShlI->hasNoUnsignedWrap());
|
|
|
|
ShlI->dropPoisonGeneratingFlags();
|
|
|
|
ASSERT_FALSE(ShlI->hasNoUnsignedWrap());
|
|
|
|
ASSERT_FALSE(ShlI->hasNoSignedWrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Value *GEPBase = Constant::getNullValue(B.getInt8PtrTy());
|
|
|
|
auto *GI = cast<GetElementPtrInst>(B.CreateInBoundsGEP(GEPBase, {Arg0}));
|
|
|
|
ASSERT_TRUE(GI->isInBounds());
|
|
|
|
GI->dropPoisonGeneratingFlags();
|
|
|
|
ASSERT_FALSE(GI->isInBounds());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 16:04:20 +08:00
|
|
|
TEST(InstructionsTest, GEPIndices) {
|
|
|
|
LLVMContext Context;
|
|
|
|
IRBuilder<NoFolder> Builder(Context);
|
|
|
|
Type *ElementTy = Builder.getInt8Ty();
|
|
|
|
Type *ArrTy = ArrayType::get(ArrayType::get(ElementTy, 64), 64);
|
|
|
|
Value *Indices[] = {
|
|
|
|
Builder.getInt32(0),
|
|
|
|
Builder.getInt32(13),
|
|
|
|
Builder.getInt32(42) };
|
|
|
|
|
|
|
|
Value *V = Builder.CreateGEP(ArrTy, UndefValue::get(PointerType::getUnqual(ArrTy)),
|
|
|
|
Indices);
|
|
|
|
ASSERT_TRUE(isa<GetElementPtrInst>(V));
|
|
|
|
|
|
|
|
auto *GEPI = cast<GetElementPtrInst>(V);
|
|
|
|
ASSERT_NE(GEPI->idx_begin(), GEPI->idx_end());
|
|
|
|
ASSERT_EQ(GEPI->idx_end(), std::next(GEPI->idx_begin(), 3));
|
|
|
|
EXPECT_EQ(Indices[0], GEPI->idx_begin()[0]);
|
|
|
|
EXPECT_EQ(Indices[1], GEPI->idx_begin()[1]);
|
|
|
|
EXPECT_EQ(Indices[2], GEPI->idx_begin()[2]);
|
|
|
|
EXPECT_EQ(GEPI->idx_begin(), GEPI->indices().begin());
|
|
|
|
EXPECT_EQ(GEPI->idx_end(), GEPI->indices().end());
|
|
|
|
|
|
|
|
const auto *CGEPI = GEPI;
|
|
|
|
ASSERT_NE(CGEPI->idx_begin(), CGEPI->idx_end());
|
|
|
|
ASSERT_EQ(CGEPI->idx_end(), std::next(CGEPI->idx_begin(), 3));
|
|
|
|
EXPECT_EQ(Indices[0], CGEPI->idx_begin()[0]);
|
|
|
|
EXPECT_EQ(Indices[1], CGEPI->idx_begin()[1]);
|
|
|
|
EXPECT_EQ(Indices[2], CGEPI->idx_begin()[2]);
|
|
|
|
EXPECT_EQ(CGEPI->idx_begin(), CGEPI->indices().begin());
|
|
|
|
EXPECT_EQ(CGEPI->idx_end(), CGEPI->indices().end());
|
|
|
|
|
|
|
|
delete GEPI;
|
|
|
|
}
|
|
|
|
|
2017-04-12 15:27:28 +08:00
|
|
|
TEST(InstructionsTest, SwitchInst) {
|
|
|
|
LLVMContext C;
|
|
|
|
|
|
|
|
std::unique_ptr<BasicBlock> BB1, BB2, BB3;
|
|
|
|
BB1.reset(BasicBlock::Create(C));
|
|
|
|
BB2.reset(BasicBlock::Create(C));
|
|
|
|
BB3.reset(BasicBlock::Create(C));
|
|
|
|
|
|
|
|
// We create block 0 after the others so that it gets destroyed first and
|
|
|
|
// clears the uses of the other basic blocks.
|
|
|
|
std::unique_ptr<BasicBlock> BB0(BasicBlock::Create(C));
|
|
|
|
|
|
|
|
auto *Int32Ty = Type::getInt32Ty(C);
|
|
|
|
|
|
|
|
SwitchInst *SI =
|
|
|
|
SwitchInst::Create(UndefValue::get(Int32Ty), BB0.get(), 3, BB0.get());
|
|
|
|
SI->addCase(ConstantInt::get(Int32Ty, 1), BB1.get());
|
|
|
|
SI->addCase(ConstantInt::get(Int32Ty, 2), BB2.get());
|
|
|
|
SI->addCase(ConstantInt::get(Int32Ty, 3), BB3.get());
|
|
|
|
|
|
|
|
auto CI = SI->case_begin();
|
|
|
|
ASSERT_NE(CI, SI->case_end());
|
|
|
|
EXPECT_EQ(1, CI->getCaseValue()->getSExtValue());
|
|
|
|
EXPECT_EQ(BB1.get(), CI->getCaseSuccessor());
|
|
|
|
EXPECT_EQ(2, (CI + 1)->getCaseValue()->getSExtValue());
|
|
|
|
EXPECT_EQ(BB2.get(), (CI + 1)->getCaseSuccessor());
|
|
|
|
EXPECT_EQ(3, (CI + 2)->getCaseValue()->getSExtValue());
|
|
|
|
EXPECT_EQ(BB3.get(), (CI + 2)->getCaseSuccessor());
|
|
|
|
EXPECT_EQ(CI + 1, std::next(CI));
|
|
|
|
EXPECT_EQ(CI + 2, std::next(CI, 2));
|
|
|
|
EXPECT_EQ(CI + 3, std::next(CI, 3));
|
|
|
|
EXPECT_EQ(SI->case_end(), CI + 3);
|
|
|
|
EXPECT_EQ(0, CI - CI);
|
|
|
|
EXPECT_EQ(1, (CI + 1) - CI);
|
|
|
|
EXPECT_EQ(2, (CI + 2) - CI);
|
|
|
|
EXPECT_EQ(3, SI->case_end() - CI);
|
|
|
|
EXPECT_EQ(3, std::distance(CI, SI->case_end()));
|
|
|
|
|
|
|
|
auto CCI = const_cast<const SwitchInst *>(SI)->case_begin();
|
|
|
|
SwitchInst::ConstCaseIt CCE = SI->case_end();
|
|
|
|
ASSERT_NE(CCI, SI->case_end());
|
|
|
|
EXPECT_EQ(1, CCI->getCaseValue()->getSExtValue());
|
|
|
|
EXPECT_EQ(BB1.get(), CCI->getCaseSuccessor());
|
|
|
|
EXPECT_EQ(2, (CCI + 1)->getCaseValue()->getSExtValue());
|
|
|
|
EXPECT_EQ(BB2.get(), (CCI + 1)->getCaseSuccessor());
|
|
|
|
EXPECT_EQ(3, (CCI + 2)->getCaseValue()->getSExtValue());
|
|
|
|
EXPECT_EQ(BB3.get(), (CCI + 2)->getCaseSuccessor());
|
|
|
|
EXPECT_EQ(CCI + 1, std::next(CCI));
|
|
|
|
EXPECT_EQ(CCI + 2, std::next(CCI, 2));
|
|
|
|
EXPECT_EQ(CCI + 3, std::next(CCI, 3));
|
|
|
|
EXPECT_EQ(CCE, CCI + 3);
|
|
|
|
EXPECT_EQ(0, CCI - CCI);
|
|
|
|
EXPECT_EQ(1, (CCI + 1) - CCI);
|
|
|
|
EXPECT_EQ(2, (CCI + 2) - CCI);
|
|
|
|
EXPECT_EQ(3, CCE - CCI);
|
|
|
|
EXPECT_EQ(3, std::distance(CCI, CCE));
|
|
|
|
|
|
|
|
// Make sure that the const iterator is compatible with a const auto ref.
|
|
|
|
const auto &Handle = *CCI;
|
|
|
|
EXPECT_EQ(1, Handle.getCaseValue()->getSExtValue());
|
|
|
|
EXPECT_EQ(BB1.get(), Handle.getCaseSuccessor());
|
|
|
|
}
|
|
|
|
|
2017-05-08 20:40:18 +08:00
|
|
|
TEST(InstructionsTest, CommuteShuffleMask) {
|
|
|
|
SmallVector<int, 16> Indices({-1, 0, 7});
|
|
|
|
ShuffleVectorInst::commuteShuffleMask(Indices, 4);
|
|
|
|
EXPECT_THAT(Indices, testing::ContainerEq(ArrayRef<int>({-1, 4, 3})));
|
|
|
|
}
|
|
|
|
|
2016-01-14 14:21:42 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
} // end namespace llvm
|