blob: b65ac34dacdf88251ad2434940edfc2a1b32e7ae [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"
12#include "llvm/Instructions.h"
Chris Lattnerf3523592009-10-27 17:08:31 +000013#include "llvm/LLVMContext.h"
Nick Lewycky06e59e72010-03-13 19:58:26 +000014#include "llvm/ADT/SmallPtrSet.h"
15#include "llvm/ADT/STLExtras.h"
Nick Lewycky4dd68242009-09-27 21:39:46 +000016
17using namespace llvm;
18
Nick Lewycky06e59e72010-03-13 19:58:26 +000019class CloneInstruction : public ::testing::Test {
20protected:
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
Nick Lewycky4dd68242009-09-27 21:39:46 +000046 LLVMContext context;
Nick Lewycky06e59e72010-03-13 19:58:26 +000047 Value *V;
48};
49
50TEST_F(CloneInstruction, OverflowBits) {
51 V = new Argument(Type::getInt32Ty(context));
Nick Lewycky4dd68242009-09-27 21:39:46 +000052
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
Nick Lewycky06e59e72010-03-13 19:58:26 +000057 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();
Nick Lewycky4dd68242009-09-27 21:39:46 +000069
70 Add->setHasNoUnsignedWrap();
71 Sub->setHasNoUnsignedWrap();
72 Mul->setHasNoUnsignedWrap();
73
Nick Lewycky06e59e72010-03-13 19:58:26 +000074 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();
Nick Lewycky4dd68242009-09-27 21:39:46 +000086
87 Add->setHasNoSignedWrap();
88 Sub->setHasNoSignedWrap();
89 Mul->setHasNoSignedWrap();
90
Nick Lewycky06e59e72010-03-13 19:58:26 +000091 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();
Nick Lewycky4dd68242009-09-27 21:39:46 +0000103
104 Add->setHasNoUnsignedWrap(false);
105 Sub->setHasNoUnsignedWrap(false);
106 Mul->setHasNoUnsignedWrap(false);
107
Nick Lewycky06e59e72010-03-13 19:58:26 +0000108 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());
Nick Lewycky4dd68242009-09-27 21:39:46 +0000118}
119
Nick Lewycky06e59e72010-03-13 19:58:26 +0000120TEST_F(CloneInstruction, Inbounds) {
121 V = new Argument(Type::getInt32PtrTy(context));
122
Nick Lewycky4dd68242009-09-27 21:39:46 +0000123 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());
Nick Lewycky06e59e72010-03-13 19:58:26 +0000127 EXPECT_FALSE(this->clone(GEP)->isInBounds());
Nick Lewycky4dd68242009-09-27 21:39:46 +0000128
129 GEP->setIsInBounds();
Nick Lewycky06e59e72010-03-13 19:58:26 +0000130 EXPECT_TRUE(this->clone(GEP)->isInBounds());
Nick Lewycky4dd68242009-09-27 21:39:46 +0000131}
132
Nick Lewycky06e59e72010-03-13 19:58:26 +0000133TEST_F(CloneInstruction, Exact) {
134 V = new Argument(Type::getInt32Ty(context));
Nick Lewycky4dd68242009-09-27 21:39:46 +0000135
136 BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V);
Nick Lewycky06e59e72010-03-13 19:58:26 +0000137 EXPECT_FALSE(this->clone(SDiv)->isExact());
Nick Lewycky4dd68242009-09-27 21:39:46 +0000138
139 SDiv->setIsExact(true);
Nick Lewycky06e59e72010-03-13 19:58:26 +0000140 EXPECT_TRUE(this->clone(SDiv)->isExact());
Nick Lewycky4dd68242009-09-27 21:39:46 +0000141}