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/Constant.h"
13 #include "llvm/Instructions.h"
14 #include "llvm/LLVMContext.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/ADT/STLExtras.h"
20 class CloneInstruction : public ::testing::Test {
22 virtual void SetUp() {
28 Value *V2 = V1->clone();
35 DeleteContainerPointers(Clones);
38 virtual void TearDown() {
40 DeleteContainerPointers(Orig);
44 SmallPtrSet<Value *, 4> Orig; // Erase on exit
45 SmallPtrSet<Value *, 4> Clones; // Erase in eraseClones
51 TEST_F(CloneInstruction, OverflowBits) {
52 V = new Argument(Type::getInt32Ty(context));
54 BinaryOperator *Add = BinaryOperator::Create(Instruction::Add, V, V);
55 BinaryOperator *Sub = BinaryOperator::Create(Instruction::Sub, V, V);
56 BinaryOperator *Mul = BinaryOperator::Create(Instruction::Mul, V, V);
58 BinaryOperator *AddClone = this->clone(Add);
59 BinaryOperator *SubClone = this->clone(Sub);
60 BinaryOperator *MulClone = this->clone(Mul);
62 EXPECT_FALSE(AddClone->hasNoUnsignedWrap());
63 EXPECT_FALSE(AddClone->hasNoSignedWrap());
64 EXPECT_FALSE(SubClone->hasNoUnsignedWrap());
65 EXPECT_FALSE(SubClone->hasNoSignedWrap());
66 EXPECT_FALSE(MulClone->hasNoUnsignedWrap());
67 EXPECT_FALSE(MulClone->hasNoSignedWrap());
71 Add->setHasNoUnsignedWrap();
72 Sub->setHasNoUnsignedWrap();
73 Mul->setHasNoUnsignedWrap();
75 AddClone = this->clone(Add);
76 SubClone = this->clone(Sub);
77 MulClone = this->clone(Mul);
79 EXPECT_TRUE(AddClone->hasNoUnsignedWrap());
80 EXPECT_FALSE(AddClone->hasNoSignedWrap());
81 EXPECT_TRUE(SubClone->hasNoUnsignedWrap());
82 EXPECT_FALSE(SubClone->hasNoSignedWrap());
83 EXPECT_TRUE(MulClone->hasNoUnsignedWrap());
84 EXPECT_FALSE(MulClone->hasNoSignedWrap());
88 Add->setHasNoSignedWrap();
89 Sub->setHasNoSignedWrap();
90 Mul->setHasNoSignedWrap();
92 AddClone = this->clone(Add);
93 SubClone = this->clone(Sub);
94 MulClone = this->clone(Mul);
96 EXPECT_TRUE(AddClone->hasNoUnsignedWrap());
97 EXPECT_TRUE(AddClone->hasNoSignedWrap());
98 EXPECT_TRUE(SubClone->hasNoUnsignedWrap());
99 EXPECT_TRUE(SubClone->hasNoSignedWrap());
100 EXPECT_TRUE(MulClone->hasNoUnsignedWrap());
101 EXPECT_TRUE(MulClone->hasNoSignedWrap());
105 Add->setHasNoUnsignedWrap(false);
106 Sub->setHasNoUnsignedWrap(false);
107 Mul->setHasNoUnsignedWrap(false);
109 AddClone = this->clone(Add);
110 SubClone = this->clone(Sub);
111 MulClone = this->clone(Mul);
113 EXPECT_FALSE(AddClone->hasNoUnsignedWrap());
114 EXPECT_TRUE(AddClone->hasNoSignedWrap());
115 EXPECT_FALSE(SubClone->hasNoUnsignedWrap());
116 EXPECT_TRUE(SubClone->hasNoSignedWrap());
117 EXPECT_FALSE(MulClone->hasNoUnsignedWrap());
118 EXPECT_TRUE(MulClone->hasNoSignedWrap());
121 TEST_F(CloneInstruction, Inbounds) {
122 V = new Argument(Type::getInt32PtrTy(context));
124 Constant *Z = Constant::getNullValue(Type::getInt32Ty(context));
125 std::vector<Value *> ops;
127 GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops);
128 EXPECT_FALSE(this->clone(GEP)->isInBounds());
130 GEP->setIsInBounds();
131 EXPECT_TRUE(this->clone(GEP)->isInBounds());
134 TEST_F(CloneInstruction, Exact) {
135 V = new Argument(Type::getInt32Ty(context));
137 BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V);
138 EXPECT_FALSE(this->clone(SDiv)->isExact());
140 SDiv->setIsExact(true);
141 EXPECT_TRUE(this->clone(SDiv)->isExact());