Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 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" |
Chris Lattner | f352359 | 2009-10-27 17:08:31 +0000 | [diff] [blame] | 13 | #include "llvm/LLVMContext.h" |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallPtrSet.h" |
| 15 | #include "llvm/ADT/STLExtras.h" |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace llvm; |
| 18 | |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 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 | |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 46 | LLVMContext context; |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 47 | Value *V; |
| 48 | }; |
| 49 | |
| 50 | TEST_F(CloneInstruction, OverflowBits) { |
| 51 | V = new Argument(Type::getInt32Ty(context)); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 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 | |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 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(); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 69 | |
| 70 | Add->setHasNoUnsignedWrap(); |
| 71 | Sub->setHasNoUnsignedWrap(); |
| 72 | Mul->setHasNoUnsignedWrap(); |
| 73 | |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 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(); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 86 | |
| 87 | Add->setHasNoSignedWrap(); |
| 88 | Sub->setHasNoSignedWrap(); |
| 89 | Mul->setHasNoSignedWrap(); |
| 90 | |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 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(); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 103 | |
| 104 | Add->setHasNoUnsignedWrap(false); |
| 105 | Sub->setHasNoUnsignedWrap(false); |
| 106 | Mul->setHasNoUnsignedWrap(false); |
| 107 | |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 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()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 120 | TEST_F(CloneInstruction, Inbounds) { |
| 121 | V = new Argument(Type::getInt32PtrTy(context)); |
| 122 | |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 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()); |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 127 | EXPECT_FALSE(this->clone(GEP)->isInBounds()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 128 | |
| 129 | GEP->setIsInBounds(); |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 130 | EXPECT_TRUE(this->clone(GEP)->isInBounds()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 133 | TEST_F(CloneInstruction, Exact) { |
| 134 | V = new Argument(Type::getInt32Ty(context)); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 135 | |
| 136 | BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V); |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 137 | EXPECT_FALSE(this->clone(SDiv)->isExact()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 138 | |
| 139 | SDiv->setIsExact(true); |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 140 | EXPECT_TRUE(this->clone(SDiv)->isExact()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 141 | } |