2009-09-27 23:39:46 +02:00
|
|
|
//===- Cloning.cpp - Unit tests for the Cloner ----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "llvm/Argument.h"
|
2011-04-11 11:35:34 +02:00
|
|
|
#include "llvm/Constant.h"
|
2009-09-27 23:39:46 +02:00
|
|
|
#include "llvm/Instructions.h"
|
2009-10-27 18:08:31 +01:00
|
|
|
#include "llvm/LLVMContext.h"
|
2010-03-13 20:58:26 +01:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2009-09-27 23:39:46 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-03-13 20:58:26 +01:00
|
|
|
class CloneInstruction : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
virtual void SetUp() {
|
|
|
|
V = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T *clone(T *V1) {
|
|
|
|
Value *V2 = V1->clone();
|
|
|
|
Orig.insert(V1);
|
|
|
|
Clones.insert(V2);
|
|
|
|
return cast<T>(V2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void eraseClones() {
|
|
|
|
DeleteContainerPointers(Clones);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void TearDown() {
|
|
|
|
eraseClones();
|
|
|
|
DeleteContainerPointers(Orig);
|
|
|
|
delete V;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallPtrSet<Value *, 4> Orig; // Erase on exit
|
|
|
|
SmallPtrSet<Value *, 4> Clones; // Erase in eraseClones
|
|
|
|
|
2009-09-27 23:39:46 +02:00
|
|
|
LLVMContext context;
|
2010-03-13 20:58:26 +01:00
|
|
|
Value *V;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(CloneInstruction, OverflowBits) {
|
|
|
|
V = new Argument(Type::getInt32Ty(context));
|
2009-09-27 23:39:46 +02:00
|
|
|
|
|
|
|
BinaryOperator *Add = BinaryOperator::Create(Instruction::Add, V, V);
|
|
|
|
BinaryOperator *Sub = BinaryOperator::Create(Instruction::Sub, V, V);
|
|
|
|
BinaryOperator *Mul = BinaryOperator::Create(Instruction::Mul, V, V);
|
|
|
|
|
2010-03-13 20:58:26 +01:00
|
|
|
BinaryOperator *AddClone = this->clone(Add);
|
|
|
|
BinaryOperator *SubClone = this->clone(Sub);
|
|
|
|
BinaryOperator *MulClone = this->clone(Mul);
|
|
|
|
|
|
|
|
EXPECT_FALSE(AddClone->hasNoUnsignedWrap());
|
|
|
|
EXPECT_FALSE(AddClone->hasNoSignedWrap());
|
|
|
|
EXPECT_FALSE(SubClone->hasNoUnsignedWrap());
|
|
|
|
EXPECT_FALSE(SubClone->hasNoSignedWrap());
|
|
|
|
EXPECT_FALSE(MulClone->hasNoUnsignedWrap());
|
|
|
|
EXPECT_FALSE(MulClone->hasNoSignedWrap());
|
|
|
|
|
|
|
|
eraseClones();
|
2009-09-27 23:39:46 +02:00
|
|
|
|
|
|
|
Add->setHasNoUnsignedWrap();
|
|
|
|
Sub->setHasNoUnsignedWrap();
|
|
|
|
Mul->setHasNoUnsignedWrap();
|
|
|
|
|
2010-03-13 20:58:26 +01:00
|
|
|
AddClone = this->clone(Add);
|
|
|
|
SubClone = this->clone(Sub);
|
|
|
|
MulClone = this->clone(Mul);
|
|
|
|
|
|
|
|
EXPECT_TRUE(AddClone->hasNoUnsignedWrap());
|
|
|
|
EXPECT_FALSE(AddClone->hasNoSignedWrap());
|
|
|
|
EXPECT_TRUE(SubClone->hasNoUnsignedWrap());
|
|
|
|
EXPECT_FALSE(SubClone->hasNoSignedWrap());
|
|
|
|
EXPECT_TRUE(MulClone->hasNoUnsignedWrap());
|
|
|
|
EXPECT_FALSE(MulClone->hasNoSignedWrap());
|
|
|
|
|
|
|
|
eraseClones();
|
2009-09-27 23:39:46 +02:00
|
|
|
|
|
|
|
Add->setHasNoSignedWrap();
|
|
|
|
Sub->setHasNoSignedWrap();
|
|
|
|
Mul->setHasNoSignedWrap();
|
|
|
|
|
2010-03-13 20:58:26 +01:00
|
|
|
AddClone = this->clone(Add);
|
|
|
|
SubClone = this->clone(Sub);
|
|
|
|
MulClone = this->clone(Mul);
|
|
|
|
|
|
|
|
EXPECT_TRUE(AddClone->hasNoUnsignedWrap());
|
|
|
|
EXPECT_TRUE(AddClone->hasNoSignedWrap());
|
|
|
|
EXPECT_TRUE(SubClone->hasNoUnsignedWrap());
|
|
|
|
EXPECT_TRUE(SubClone->hasNoSignedWrap());
|
|
|
|
EXPECT_TRUE(MulClone->hasNoUnsignedWrap());
|
|
|
|
EXPECT_TRUE(MulClone->hasNoSignedWrap());
|
|
|
|
|
|
|
|
eraseClones();
|
2009-09-27 23:39:46 +02:00
|
|
|
|
|
|
|
Add->setHasNoUnsignedWrap(false);
|
|
|
|
Sub->setHasNoUnsignedWrap(false);
|
|
|
|
Mul->setHasNoUnsignedWrap(false);
|
|
|
|
|
2010-03-13 20:58:26 +01:00
|
|
|
AddClone = this->clone(Add);
|
|
|
|
SubClone = this->clone(Sub);
|
|
|
|
MulClone = this->clone(Mul);
|
|
|
|
|
|
|
|
EXPECT_FALSE(AddClone->hasNoUnsignedWrap());
|
|
|
|
EXPECT_TRUE(AddClone->hasNoSignedWrap());
|
|
|
|
EXPECT_FALSE(SubClone->hasNoUnsignedWrap());
|
|
|
|
EXPECT_TRUE(SubClone->hasNoSignedWrap());
|
|
|
|
EXPECT_FALSE(MulClone->hasNoUnsignedWrap());
|
|
|
|
EXPECT_TRUE(MulClone->hasNoSignedWrap());
|
2009-09-27 23:39:46 +02:00
|
|
|
}
|
|
|
|
|
2010-03-13 20:58:26 +01:00
|
|
|
TEST_F(CloneInstruction, Inbounds) {
|
|
|
|
V = new Argument(Type::getInt32PtrTy(context));
|
|
|
|
|
2009-09-27 23:39:46 +02:00
|
|
|
Constant *Z = Constant::getNullValue(Type::getInt32Ty(context));
|
|
|
|
std::vector<Value *> ops;
|
|
|
|
ops.push_back(Z);
|
|
|
|
GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops.begin(), ops.end());
|
2010-03-13 20:58:26 +01:00
|
|
|
EXPECT_FALSE(this->clone(GEP)->isInBounds());
|
2009-09-27 23:39:46 +02:00
|
|
|
|
|
|
|
GEP->setIsInBounds();
|
2010-03-13 20:58:26 +01:00
|
|
|
EXPECT_TRUE(this->clone(GEP)->isInBounds());
|
2009-09-27 23:39:46 +02:00
|
|
|
}
|
|
|
|
|
2010-03-13 20:58:26 +01:00
|
|
|
TEST_F(CloneInstruction, Exact) {
|
|
|
|
V = new Argument(Type::getInt32Ty(context));
|
2009-09-27 23:39:46 +02:00
|
|
|
|
|
|
|
BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V);
|
2010-03-13 20:58:26 +01:00
|
|
|
EXPECT_FALSE(this->clone(SDiv)->isExact());
|
2009-09-27 23:39:46 +02:00
|
|
|
|
|
|
|
SDiv->setIsExact(true);
|
2010-03-13 20:58:26 +01:00
|
|
|
EXPECT_TRUE(this->clone(SDiv)->isExact());
|
2009-09-27 23:39:46 +02:00
|
|
|
}
|