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" |
Jay Foad | 562b84b | 2011-04-11 09:35:34 +0000 | [diff] [blame] | 12 | #include "llvm/Constant.h" |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 13 | #include "llvm/Instructions.h" |
Chris Lattner | f352359 | 2009-10-27 17:08:31 +0000 | [diff] [blame] | 14 | #include "llvm/LLVMContext.h" |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallPtrSet.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame^] | 20 | namespace { |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 21 | class CloneInstruction : public ::testing::Test { |
| 22 | protected: |
| 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 Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 48 | LLVMContext context; |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 49 | Value *V; |
| 50 | }; |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame^] | 51 | } |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 52 | |
| 53 | TEST_F(CloneInstruction, OverflowBits) { |
| 54 | V = new Argument(Type::getInt32Ty(context)); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 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 | |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 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(); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 72 | |
| 73 | Add->setHasNoUnsignedWrap(); |
| 74 | Sub->setHasNoUnsignedWrap(); |
| 75 | Mul->setHasNoUnsignedWrap(); |
| 76 | |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 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(); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 89 | |
| 90 | Add->setHasNoSignedWrap(); |
| 91 | Sub->setHasNoSignedWrap(); |
| 92 | Mul->setHasNoSignedWrap(); |
| 93 | |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 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(); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 106 | |
| 107 | Add->setHasNoUnsignedWrap(false); |
| 108 | Sub->setHasNoUnsignedWrap(false); |
| 109 | Mul->setHasNoUnsignedWrap(false); |
| 110 | |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 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()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 123 | TEST_F(CloneInstruction, Inbounds) { |
| 124 | V = new Argument(Type::getInt32PtrTy(context)); |
| 125 | |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 126 | Constant *Z = Constant::getNullValue(Type::getInt32Ty(context)); |
| 127 | std::vector<Value *> ops; |
| 128 | ops.push_back(Z); |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 129 | GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops); |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 130 | EXPECT_FALSE(this->clone(GEP)->isInBounds()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 131 | |
| 132 | GEP->setIsInBounds(); |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 133 | EXPECT_TRUE(this->clone(GEP)->isInBounds()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 136 | TEST_F(CloneInstruction, Exact) { |
| 137 | V = new Argument(Type::getInt32Ty(context)); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 138 | |
| 139 | BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V); |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 140 | EXPECT_FALSE(this->clone(SDiv)->isExact()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 141 | |
| 142 | SDiv->setIsExact(true); |
Nick Lewycky | 06e59e7 | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 143 | EXPECT_TRUE(this->clone(SDiv)->isExact()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 144 | } |