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