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 | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace llvm; |
| 16 | |
| 17 | TEST(CloneInstruction, OverflowBits) { |
| 18 | LLVMContext context; |
| 19 | Value *V = new Argument(Type::getInt32Ty(context)); |
| 20 | |
| 21 | BinaryOperator *Add = BinaryOperator::Create(Instruction::Add, V, V); |
| 22 | BinaryOperator *Sub = BinaryOperator::Create(Instruction::Sub, V, V); |
| 23 | BinaryOperator *Mul = BinaryOperator::Create(Instruction::Mul, V, V); |
| 24 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame^] | 25 | EXPECT_FALSE(cast<BinaryOperator>(Add->clone())->hasNoUnsignedWrap()); |
| 26 | EXPECT_FALSE(cast<BinaryOperator>(Add->clone())->hasNoSignedWrap()); |
| 27 | EXPECT_FALSE(cast<BinaryOperator>(Sub->clone())->hasNoUnsignedWrap()); |
| 28 | EXPECT_FALSE(cast<BinaryOperator>(Sub->clone())->hasNoSignedWrap()); |
| 29 | EXPECT_FALSE(cast<BinaryOperator>(Mul->clone())->hasNoUnsignedWrap()); |
| 30 | EXPECT_FALSE(cast<BinaryOperator>(Mul->clone())->hasNoSignedWrap()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 31 | |
| 32 | Add->setHasNoUnsignedWrap(); |
| 33 | Sub->setHasNoUnsignedWrap(); |
| 34 | Mul->setHasNoUnsignedWrap(); |
| 35 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame^] | 36 | EXPECT_TRUE(cast<BinaryOperator>(Add->clone())->hasNoUnsignedWrap()); |
| 37 | EXPECT_FALSE(cast<BinaryOperator>(Add->clone())->hasNoSignedWrap()); |
| 38 | EXPECT_TRUE(cast<BinaryOperator>(Sub->clone())->hasNoUnsignedWrap()); |
| 39 | EXPECT_FALSE(cast<BinaryOperator>(Sub->clone())->hasNoSignedWrap()); |
| 40 | EXPECT_TRUE(cast<BinaryOperator>(Mul->clone())->hasNoUnsignedWrap()); |
| 41 | EXPECT_FALSE(cast<BinaryOperator>(Mul->clone())->hasNoSignedWrap()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 42 | |
| 43 | Add->setHasNoSignedWrap(); |
| 44 | Sub->setHasNoSignedWrap(); |
| 45 | Mul->setHasNoSignedWrap(); |
| 46 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame^] | 47 | EXPECT_TRUE(cast<BinaryOperator>(Add->clone())->hasNoUnsignedWrap()); |
| 48 | EXPECT_TRUE(cast<BinaryOperator>(Add->clone())->hasNoSignedWrap()); |
| 49 | EXPECT_TRUE(cast<BinaryOperator>(Sub->clone())->hasNoUnsignedWrap()); |
| 50 | EXPECT_TRUE(cast<BinaryOperator>(Sub->clone())->hasNoSignedWrap()); |
| 51 | EXPECT_TRUE(cast<BinaryOperator>(Mul->clone())->hasNoUnsignedWrap()); |
| 52 | EXPECT_TRUE(cast<BinaryOperator>(Mul->clone())->hasNoSignedWrap()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 53 | |
| 54 | Add->setHasNoUnsignedWrap(false); |
| 55 | Sub->setHasNoUnsignedWrap(false); |
| 56 | Mul->setHasNoUnsignedWrap(false); |
| 57 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame^] | 58 | EXPECT_FALSE(cast<BinaryOperator>(Add->clone())->hasNoUnsignedWrap()); |
| 59 | EXPECT_TRUE(cast<BinaryOperator>(Add->clone())->hasNoSignedWrap()); |
| 60 | EXPECT_FALSE(cast<BinaryOperator>(Sub->clone())->hasNoUnsignedWrap()); |
| 61 | EXPECT_TRUE(cast<BinaryOperator>(Sub->clone())->hasNoSignedWrap()); |
| 62 | EXPECT_FALSE(cast<BinaryOperator>(Mul->clone())->hasNoUnsignedWrap()); |
| 63 | EXPECT_TRUE(cast<BinaryOperator>(Mul->clone())->hasNoSignedWrap()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST(CloneInstruction, Inbounds) { |
| 67 | LLVMContext context; |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 68 | Value *V = new Argument(Type::getInt32PtrTy(context)); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 69 | Constant *Z = Constant::getNullValue(Type::getInt32Ty(context)); |
| 70 | std::vector<Value *> ops; |
| 71 | ops.push_back(Z); |
| 72 | GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops.begin(), ops.end()); |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame^] | 73 | EXPECT_FALSE(cast<GetElementPtrInst>(GEP->clone())->isInBounds()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 74 | |
| 75 | GEP->setIsInBounds(); |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame^] | 76 | EXPECT_TRUE(cast<GetElementPtrInst>(GEP->clone())->isInBounds()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | TEST(CloneInstruction, Exact) { |
| 80 | LLVMContext context; |
| 81 | Value *V = new Argument(Type::getInt32Ty(context)); |
| 82 | |
| 83 | BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V); |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame^] | 84 | EXPECT_FALSE(cast<BinaryOperator>(SDiv->clone())->isExact()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 85 | |
| 86 | SDiv->setIsExact(true); |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame^] | 87 | EXPECT_TRUE(cast<BinaryOperator>(SDiv->clone())->isExact()); |
Nick Lewycky | 4dd6824 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 88 | } |