• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Constant.h"
13 #include "llvm/Instructions.h"
14 #include "llvm/LLVMContext.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/ADT/STLExtras.h"
17 
18 using namespace llvm;
19 
20 namespace {
21 class CloneInstruction : public ::testing::Test {
22 protected:
SetUp()23   virtual void SetUp() {
24     V = NULL;
25   }
26 
27   template <typename T>
clone(T * V1)28   T *clone(T *V1) {
29     Value *V2 = V1->clone();
30     Orig.insert(V1);
31     Clones.insert(V2);
32     return cast<T>(V2);
33   }
34 
eraseClones()35   void eraseClones() {
36     DeleteContainerPointers(Clones);
37   }
38 
TearDown()39   virtual void TearDown() {
40     eraseClones();
41     DeleteContainerPointers(Orig);
42     delete V;
43   }
44 
45   SmallPtrSet<Value *, 4> Orig;   // Erase on exit
46   SmallPtrSet<Value *, 4> Clones; // Erase in eraseClones
47 
48   LLVMContext context;
49   Value *V;
50 };
51 }
52 
TEST_F(CloneInstruction,OverflowBits)53 TEST_F(CloneInstruction, OverflowBits) {
54   V = new Argument(Type::getInt32Ty(context));
55 
56   BinaryOperator *Add = BinaryOperator::Create(Instruction::Add, V, V);
57   BinaryOperator *Sub = BinaryOperator::Create(Instruction::Sub, V, V);
58   BinaryOperator *Mul = BinaryOperator::Create(Instruction::Mul, V, V);
59 
60   BinaryOperator *AddClone = this->clone(Add);
61   BinaryOperator *SubClone = this->clone(Sub);
62   BinaryOperator *MulClone = this->clone(Mul);
63 
64   EXPECT_FALSE(AddClone->hasNoUnsignedWrap());
65   EXPECT_FALSE(AddClone->hasNoSignedWrap());
66   EXPECT_FALSE(SubClone->hasNoUnsignedWrap());
67   EXPECT_FALSE(SubClone->hasNoSignedWrap());
68   EXPECT_FALSE(MulClone->hasNoUnsignedWrap());
69   EXPECT_FALSE(MulClone->hasNoSignedWrap());
70 
71   eraseClones();
72 
73   Add->setHasNoUnsignedWrap();
74   Sub->setHasNoUnsignedWrap();
75   Mul->setHasNoUnsignedWrap();
76 
77   AddClone = this->clone(Add);
78   SubClone = this->clone(Sub);
79   MulClone = this->clone(Mul);
80 
81   EXPECT_TRUE(AddClone->hasNoUnsignedWrap());
82   EXPECT_FALSE(AddClone->hasNoSignedWrap());
83   EXPECT_TRUE(SubClone->hasNoUnsignedWrap());
84   EXPECT_FALSE(SubClone->hasNoSignedWrap());
85   EXPECT_TRUE(MulClone->hasNoUnsignedWrap());
86   EXPECT_FALSE(MulClone->hasNoSignedWrap());
87 
88   eraseClones();
89 
90   Add->setHasNoSignedWrap();
91   Sub->setHasNoSignedWrap();
92   Mul->setHasNoSignedWrap();
93 
94   AddClone = this->clone(Add);
95   SubClone = this->clone(Sub);
96   MulClone = this->clone(Mul);
97 
98   EXPECT_TRUE(AddClone->hasNoUnsignedWrap());
99   EXPECT_TRUE(AddClone->hasNoSignedWrap());
100   EXPECT_TRUE(SubClone->hasNoUnsignedWrap());
101   EXPECT_TRUE(SubClone->hasNoSignedWrap());
102   EXPECT_TRUE(MulClone->hasNoUnsignedWrap());
103   EXPECT_TRUE(MulClone->hasNoSignedWrap());
104 
105   eraseClones();
106 
107   Add->setHasNoUnsignedWrap(false);
108   Sub->setHasNoUnsignedWrap(false);
109   Mul->setHasNoUnsignedWrap(false);
110 
111   AddClone = this->clone(Add);
112   SubClone = this->clone(Sub);
113   MulClone = this->clone(Mul);
114 
115   EXPECT_FALSE(AddClone->hasNoUnsignedWrap());
116   EXPECT_TRUE(AddClone->hasNoSignedWrap());
117   EXPECT_FALSE(SubClone->hasNoUnsignedWrap());
118   EXPECT_TRUE(SubClone->hasNoSignedWrap());
119   EXPECT_FALSE(MulClone->hasNoUnsignedWrap());
120   EXPECT_TRUE(MulClone->hasNoSignedWrap());
121 }
122 
TEST_F(CloneInstruction,Inbounds)123 TEST_F(CloneInstruction, Inbounds) {
124   V = new Argument(Type::getInt32PtrTy(context));
125 
126   Constant *Z = Constant::getNullValue(Type::getInt32Ty(context));
127   std::vector<Value *> ops;
128   ops.push_back(Z);
129   GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops);
130   EXPECT_FALSE(this->clone(GEP)->isInBounds());
131 
132   GEP->setIsInBounds();
133   EXPECT_TRUE(this->clone(GEP)->isInBounds());
134 }
135 
TEST_F(CloneInstruction,Exact)136 TEST_F(CloneInstruction, Exact) {
137   V = new Argument(Type::getInt32Ty(context));
138 
139   BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V);
140   EXPECT_FALSE(this->clone(SDiv)->isExact());
141 
142   SDiv->setIsExact(true);
143   EXPECT_TRUE(this->clone(SDiv)->isExact());
144 }
145