blob: 17047e7ca15a5f8f3c0cd35b9b49ba6158fa5a7d [file] [log] [blame]
Nick Lewyckya475d662009-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 Lattner85c4ec22009-10-27 17:08:31 +000013#include "llvm/LLVMContext.h"
Nick Lewyckya475d662009-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
Devang Patel12236e12009-10-27 22:16:29 +000025 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 Lewyckya475d662009-09-27 21:39:46 +000031
32 Add->setHasNoUnsignedWrap();
33 Sub->setHasNoUnsignedWrap();
34 Mul->setHasNoUnsignedWrap();
35
Devang Patel12236e12009-10-27 22:16:29 +000036 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 Lewyckya475d662009-09-27 21:39:46 +000042
43 Add->setHasNoSignedWrap();
44 Sub->setHasNoSignedWrap();
45 Mul->setHasNoSignedWrap();
46
Devang Patel12236e12009-10-27 22:16:29 +000047 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 Lewyckya475d662009-09-27 21:39:46 +000053
54 Add->setHasNoUnsignedWrap(false);
55 Sub->setHasNoUnsignedWrap(false);
56 Mul->setHasNoUnsignedWrap(false);
57
Devang Patel12236e12009-10-27 22:16:29 +000058 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 Lewyckya475d662009-09-27 21:39:46 +000064}
65
66TEST(CloneInstruction, Inbounds) {
67 LLVMContext context;
Duncan Sandsf2519d62009-10-06 15:40:36 +000068 Value *V = new Argument(Type::getInt32PtrTy(context));
Nick Lewyckya475d662009-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());
Devang Patel12236e12009-10-27 22:16:29 +000073 EXPECT_FALSE(cast<GetElementPtrInst>(GEP->clone())->isInBounds());
Nick Lewyckya475d662009-09-27 21:39:46 +000074
75 GEP->setIsInBounds();
Devang Patel12236e12009-10-27 22:16:29 +000076 EXPECT_TRUE(cast<GetElementPtrInst>(GEP->clone())->isInBounds());
Nick Lewyckya475d662009-09-27 21:39:46 +000077}
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);
Devang Patel12236e12009-10-27 22:16:29 +000084 EXPECT_FALSE(cast<BinaryOperator>(SDiv->clone())->isExact());
Nick Lewyckya475d662009-09-27 21:39:46 +000085
86 SDiv->setIsExact(true);
Devang Patel12236e12009-10-27 22:16:29 +000087 EXPECT_TRUE(cast<BinaryOperator>(SDiv->clone())->isExact());
Nick Lewyckya475d662009-09-27 21:39:46 +000088}