Teach this test not to leak. Also, clean up all the cast<BinaryOperator> cruft.
[oota-llvm.git] / unittests / Transforms / Utils / Cloning.cpp
1 //===- Cloning.cpp - Unit tests for the Cloner ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "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"
16
17 using namespace llvm;
18
19 class CloneInstruction : public ::testing::Test {
20 protected:
21   virtual void SetUp() {
22     V = NULL;
23   }
24
25   template <typename T>
26   T *clone(T *V1) {
27     Value *V2 = V1->clone();
28     Orig.insert(V1);
29     Clones.insert(V2);
30     return cast<T>(V2);
31   }
32
33   void eraseClones() {
34     DeleteContainerPointers(Clones);
35   }
36
37   virtual void TearDown() {
38     eraseClones();
39     DeleteContainerPointers(Orig);
40     delete V;
41   }
42
43   SmallPtrSet<Value *, 4> Orig;   // Erase on exit
44   SmallPtrSet<Value *, 4> Clones; // Erase in eraseClones
45
46   LLVMContext context;
47   Value *V;
48 };
49
50 TEST_F(CloneInstruction, OverflowBits) {
51   V = new Argument(Type::getInt32Ty(context));
52
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);
56
57   BinaryOperator *AddClone = this->clone(Add);
58   BinaryOperator *SubClone = this->clone(Sub);
59   BinaryOperator *MulClone = this->clone(Mul);
60
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());
67
68   eraseClones();
69
70   Add->setHasNoUnsignedWrap();
71   Sub->setHasNoUnsignedWrap();
72   Mul->setHasNoUnsignedWrap();
73
74   AddClone = this->clone(Add);
75   SubClone = this->clone(Sub);
76   MulClone = this->clone(Mul);
77
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());
84
85   eraseClones();
86
87   Add->setHasNoSignedWrap();
88   Sub->setHasNoSignedWrap();
89   Mul->setHasNoSignedWrap();
90
91   AddClone = this->clone(Add);
92   SubClone = this->clone(Sub);
93   MulClone = this->clone(Mul);
94
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());
101
102   eraseClones();
103
104   Add->setHasNoUnsignedWrap(false);
105   Sub->setHasNoUnsignedWrap(false);
106   Mul->setHasNoUnsignedWrap(false);
107
108   AddClone = this->clone(Add);
109   SubClone = this->clone(Sub);
110   MulClone = this->clone(Mul);
111
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());
118 }
119
120 TEST_F(CloneInstruction, Inbounds) {
121   V = new Argument(Type::getInt32PtrTy(context));
122
123   Constant *Z = Constant::getNullValue(Type::getInt32Ty(context));
124   std::vector<Value *> ops;
125   ops.push_back(Z);
126   GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops.begin(), ops.end());
127   EXPECT_FALSE(this->clone(GEP)->isInBounds());
128
129   GEP->setIsInBounds();
130   EXPECT_TRUE(this->clone(GEP)->isInBounds());
131 }
132
133 TEST_F(CloneInstruction, Exact) {
134   V = new Argument(Type::getInt32Ty(context));
135
136   BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V);
137   EXPECT_FALSE(this->clone(SDiv)->isExact());
138
139   SDiv->setIsExact(true);
140   EXPECT_TRUE(this->clone(SDiv)->isExact());
141 }