1 //===- Cloning.cpp - Unit tests for the Cloner ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/Argument.h"
12 #include "llvm/Instructions.h"
13 #include "llvm/LLVMContext.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/STLExtras.h"
19 class CloneInstruction : public ::testing::Test {
21 virtual void SetUp() {
27 Value *V2 = V1->clone();
34 DeleteContainerPointers(Clones);
37 virtual void TearDown() {
39 DeleteContainerPointers(Orig);
43 SmallPtrSet<Value *, 4> Orig; // Erase on exit
44 SmallPtrSet<Value *, 4> Clones; // Erase in eraseClones
50 TEST_F(CloneInstruction, OverflowBits) {
51 V = new Argument(Type::getInt32Ty(context));
53 BinaryOperator *Add = BinaryOperator::Create(Instruction::Add, V, V);
54 BinaryOperator *Sub = BinaryOperator::Create(Instruction::Sub, V, V);
55 BinaryOperator *Mul = BinaryOperator::Create(Instruction::Mul, V, V);
57 BinaryOperator *AddClone = this->clone(Add);
58 BinaryOperator *SubClone = this->clone(Sub);
59 BinaryOperator *MulClone = this->clone(Mul);
61 EXPECT_FALSE(AddClone->hasNoUnsignedWrap());
62 EXPECT_FALSE(AddClone->hasNoSignedWrap());
63 EXPECT_FALSE(SubClone->hasNoUnsignedWrap());
64 EXPECT_FALSE(SubClone->hasNoSignedWrap());
65 EXPECT_FALSE(MulClone->hasNoUnsignedWrap());
66 EXPECT_FALSE(MulClone->hasNoSignedWrap());
70 Add->setHasNoUnsignedWrap();
71 Sub->setHasNoUnsignedWrap();
72 Mul->setHasNoUnsignedWrap();
74 AddClone = this->clone(Add);
75 SubClone = this->clone(Sub);
76 MulClone = this->clone(Mul);
78 EXPECT_TRUE(AddClone->hasNoUnsignedWrap());
79 EXPECT_FALSE(AddClone->hasNoSignedWrap());
80 EXPECT_TRUE(SubClone->hasNoUnsignedWrap());
81 EXPECT_FALSE(SubClone->hasNoSignedWrap());
82 EXPECT_TRUE(MulClone->hasNoUnsignedWrap());
83 EXPECT_FALSE(MulClone->hasNoSignedWrap());
87 Add->setHasNoSignedWrap();
88 Sub->setHasNoSignedWrap();
89 Mul->setHasNoSignedWrap();
91 AddClone = this->clone(Add);
92 SubClone = this->clone(Sub);
93 MulClone = this->clone(Mul);
95 EXPECT_TRUE(AddClone->hasNoUnsignedWrap());
96 EXPECT_TRUE(AddClone->hasNoSignedWrap());
97 EXPECT_TRUE(SubClone->hasNoUnsignedWrap());
98 EXPECT_TRUE(SubClone->hasNoSignedWrap());
99 EXPECT_TRUE(MulClone->hasNoUnsignedWrap());
100 EXPECT_TRUE(MulClone->hasNoSignedWrap());
104 Add->setHasNoUnsignedWrap(false);
105 Sub->setHasNoUnsignedWrap(false);
106 Mul->setHasNoUnsignedWrap(false);
108 AddClone = this->clone(Add);
109 SubClone = this->clone(Sub);
110 MulClone = this->clone(Mul);
112 EXPECT_FALSE(AddClone->hasNoUnsignedWrap());
113 EXPECT_TRUE(AddClone->hasNoSignedWrap());
114 EXPECT_FALSE(SubClone->hasNoUnsignedWrap());
115 EXPECT_TRUE(SubClone->hasNoSignedWrap());
116 EXPECT_FALSE(MulClone->hasNoUnsignedWrap());
117 EXPECT_TRUE(MulClone->hasNoSignedWrap());
120 TEST_F(CloneInstruction, Inbounds) {
121 V = new Argument(Type::getInt32PtrTy(context));
123 Constant *Z = Constant::getNullValue(Type::getInt32Ty(context));
124 std::vector<Value *> ops;
126 GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops.begin(), ops.end());
127 EXPECT_FALSE(this->clone(GEP)->isInBounds());
129 GEP->setIsInBounds();
130 EXPECT_TRUE(this->clone(GEP)->isInBounds());
133 TEST_F(CloneInstruction, Exact) {
134 V = new Argument(Type::getInt32Ty(context));
136 BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V);
137 EXPECT_FALSE(this->clone(SDiv)->isExact());
139 SDiv->setIsExact(true);
140 EXPECT_TRUE(this->clone(SDiv)->isExact());