blob: 3aa796c750a1f40553d073ee9abe11aa5c880105 [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"
Chris Lattnerf3523592009-10-27 17:08:31 +000013#include "llvm/LLVMContext.h"
Nick Lewycky4dd68242009-09-27 21:39:46 +000014
15using namespace llvm;
16
17TEST(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
66TEST(CloneInstruction, Inbounds) {
67 LLVMContext context;
Duncan Sandsac53a0b2009-10-06 15:40:36 +000068 Value *V = new Argument(Type::getInt32PtrTy(context));
Nick Lewycky4dd68242009-09-27 21:39:46 +000069 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
79TEST(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}