blob: 4243b2d39de7a229a7272798729adf6c09613faf [file] [log] [blame]
Nick Lewycky4dd68242009-09-27 21:39:46 +00001//===- 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"
Jay Foad562b84b2011-04-11 09:35:34 +000012#include "llvm/Constant.h"
Nick Lewycky4dd68242009-09-27 21:39:46 +000013#include "llvm/Instructions.h"
Chris Lattnerf3523592009-10-27 17:08:31 +000014#include "llvm/LLVMContext.h"
Nick Lewycky06e59e72010-03-13 19:58:26 +000015#include "llvm/ADT/SmallPtrSet.h"
16#include "llvm/ADT/STLExtras.h"
Nick Lewycky4dd68242009-09-27 21:39:46 +000017
18using namespace llvm;
19
David Blaikie2d24e2a2011-12-20 02:50:00 +000020namespace {
Nick Lewycky06e59e72010-03-13 19:58:26 +000021class CloneInstruction : public ::testing::Test {
22protected:
23 virtual void SetUp() {
24 V = NULL;
25 }
26
27 template <typename T>
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
35 void eraseClones() {
36 DeleteContainerPointers(Clones);
37 }
38
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
Nick Lewycky4dd68242009-09-27 21:39:46 +000048 LLVMContext context;
Nick Lewycky06e59e72010-03-13 19:58:26 +000049 Value *V;
50};
David Blaikie2d24e2a2011-12-20 02:50:00 +000051}
Nick Lewycky06e59e72010-03-13 19:58:26 +000052
53TEST_F(CloneInstruction, OverflowBits) {
54 V = new Argument(Type::getInt32Ty(context));
Nick Lewycky4dd68242009-09-27 21:39:46 +000055
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
Nick Lewycky06e59e72010-03-13 19:58:26 +000060 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();
Nick Lewycky4dd68242009-09-27 21:39:46 +000072
73 Add->setHasNoUnsignedWrap();
74 Sub->setHasNoUnsignedWrap();
75 Mul->setHasNoUnsignedWrap();
76
Nick Lewycky06e59e72010-03-13 19:58:26 +000077 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();
Nick Lewycky4dd68242009-09-27 21:39:46 +000089
90 Add->setHasNoSignedWrap();
91 Sub->setHasNoSignedWrap();
92 Mul->setHasNoSignedWrap();
93
Nick Lewycky06e59e72010-03-13 19:58:26 +000094 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();
Nick Lewycky4dd68242009-09-27 21:39:46 +0000106
107 Add->setHasNoUnsignedWrap(false);
108 Sub->setHasNoUnsignedWrap(false);
109 Mul->setHasNoUnsignedWrap(false);
110
Nick Lewycky06e59e72010-03-13 19:58:26 +0000111 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());
Nick Lewycky4dd68242009-09-27 21:39:46 +0000121}
122
Nick Lewycky06e59e72010-03-13 19:58:26 +0000123TEST_F(CloneInstruction, Inbounds) {
124 V = new Argument(Type::getInt32PtrTy(context));
125
Nick Lewycky4dd68242009-09-27 21:39:46 +0000126 Constant *Z = Constant::getNullValue(Type::getInt32Ty(context));
127 std::vector<Value *> ops;
128 ops.push_back(Z);
Jay Foada9203102011-07-25 09:48:08 +0000129 GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops);
Nick Lewycky06e59e72010-03-13 19:58:26 +0000130 EXPECT_FALSE(this->clone(GEP)->isInBounds());
Nick Lewycky4dd68242009-09-27 21:39:46 +0000131
132 GEP->setIsInBounds();
Nick Lewycky06e59e72010-03-13 19:58:26 +0000133 EXPECT_TRUE(this->clone(GEP)->isInBounds());
Nick Lewycky4dd68242009-09-27 21:39:46 +0000134}
135
Nick Lewycky06e59e72010-03-13 19:58:26 +0000136TEST_F(CloneInstruction, Exact) {
137 V = new Argument(Type::getInt32Ty(context));
Nick Lewycky4dd68242009-09-27 21:39:46 +0000138
139 BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V);
Nick Lewycky06e59e72010-03-13 19:58:26 +0000140 EXPECT_FALSE(this->clone(SDiv)->isExact());
Nick Lewycky4dd68242009-09-27 21:39:46 +0000141
142 SDiv->setIsExact(true);
Nick Lewycky06e59e72010-03-13 19:58:26 +0000143 EXPECT_TRUE(this->clone(SDiv)->isExact());
Nick Lewycky4dd68242009-09-27 21:39:46 +0000144}