Type.h doesn't need to #include LLVMContext.h
[oota-llvm.git] / unittests / Transforms / Utils / Cloning.cpp
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"
13 #include "llvm/LLVMContext.h"
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
25   EXPECT_FALSE(Add->clone()->hasNoUnsignedWrap());
26   EXPECT_FALSE(Add->clone()->hasNoSignedWrap());
27   EXPECT_FALSE(Sub->clone()->hasNoUnsignedWrap());
28   EXPECT_FALSE(Sub->clone()->hasNoSignedWrap());
29   EXPECT_FALSE(Mul->clone()->hasNoUnsignedWrap());
30   EXPECT_FALSE(Mul->clone()->hasNoSignedWrap());
31
32   Add->setHasNoUnsignedWrap();
33   Sub->setHasNoUnsignedWrap();
34   Mul->setHasNoUnsignedWrap();
35
36   EXPECT_TRUE(Add->clone()->hasNoUnsignedWrap());
37   EXPECT_FALSE(Add->clone()->hasNoSignedWrap());
38   EXPECT_TRUE(Sub->clone()->hasNoUnsignedWrap());
39   EXPECT_FALSE(Sub->clone()->hasNoSignedWrap());
40   EXPECT_TRUE(Mul->clone()->hasNoUnsignedWrap());
41   EXPECT_FALSE(Mul->clone()->hasNoSignedWrap());
42
43   Add->setHasNoSignedWrap();
44   Sub->setHasNoSignedWrap();
45   Mul->setHasNoSignedWrap();
46
47   EXPECT_TRUE(Add->clone()->hasNoUnsignedWrap());
48   EXPECT_TRUE(Add->clone()->hasNoSignedWrap());
49   EXPECT_TRUE(Sub->clone()->hasNoUnsignedWrap());
50   EXPECT_TRUE(Sub->clone()->hasNoSignedWrap());
51   EXPECT_TRUE(Mul->clone()->hasNoUnsignedWrap());
52   EXPECT_TRUE(Mul->clone()->hasNoSignedWrap());
53
54   Add->setHasNoUnsignedWrap(false);
55   Sub->setHasNoUnsignedWrap(false);
56   Mul->setHasNoUnsignedWrap(false);
57
58   EXPECT_FALSE(Add->clone()->hasNoUnsignedWrap());
59   EXPECT_TRUE(Add->clone()->hasNoSignedWrap());
60   EXPECT_FALSE(Sub->clone()->hasNoUnsignedWrap());
61   EXPECT_TRUE(Sub->clone()->hasNoSignedWrap());
62   EXPECT_FALSE(Mul->clone()->hasNoUnsignedWrap());
63   EXPECT_TRUE(Mul->clone()->hasNoSignedWrap());
64 }
65
66 TEST(CloneInstruction, Inbounds) {
67   LLVMContext context;
68   Value *V = new Argument(Type::getInt32PtrTy(context));
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());
73   EXPECT_FALSE(GEP->clone()->isInBounds());
74
75   GEP->setIsInBounds();
76   EXPECT_TRUE(GEP->clone()->isInBounds());
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);
84   EXPECT_FALSE(SDiv->clone()->isExact());
85
86   SDiv->setIsExact(true);
87   EXPECT_TRUE(SDiv->clone()->isExact());
88 }