summaryrefslogtreecommitdiff
path: root/unittests/Transforms/Utils/Cloning.cpp
blob: 7c93f6f5834dc3756e0f79f587f37a0ee0562d1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//===- Cloning.cpp - Unit tests for the Cloner ----------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "gtest/gtest.h"
#include "llvm/Argument.h"
#include "llvm/Instructions.h"

using namespace llvm;

TEST(CloneInstruction, OverflowBits) {
  LLVMContext context;
  Value *V = new Argument(Type::getInt32Ty(context));

  BinaryOperator *Add = BinaryOperator::Create(Instruction::Add, V, V);
  BinaryOperator *Sub = BinaryOperator::Create(Instruction::Sub, V, V);
  BinaryOperator *Mul = BinaryOperator::Create(Instruction::Mul, V, V);

  EXPECT_FALSE(Add->clone()->hasNoUnsignedWrap());
  EXPECT_FALSE(Add->clone()->hasNoSignedWrap());
  EXPECT_FALSE(Sub->clone()->hasNoUnsignedWrap());
  EXPECT_FALSE(Sub->clone()->hasNoSignedWrap());
  EXPECT_FALSE(Mul->clone()->hasNoUnsignedWrap());
  EXPECT_FALSE(Mul->clone()->hasNoSignedWrap());

  Add->setHasNoUnsignedWrap();
  Sub->setHasNoUnsignedWrap();
  Mul->setHasNoUnsignedWrap();

  EXPECT_TRUE(Add->clone()->hasNoUnsignedWrap());
  EXPECT_FALSE(Add->clone()->hasNoSignedWrap());
  EXPECT_TRUE(Sub->clone()->hasNoUnsignedWrap());
  EXPECT_FALSE(Sub->clone()->hasNoSignedWrap());
  EXPECT_TRUE(Mul->clone()->hasNoUnsignedWrap());
  EXPECT_FALSE(Mul->clone()->hasNoSignedWrap());

  Add->setHasNoSignedWrap();
  Sub->setHasNoSignedWrap();
  Mul->setHasNoSignedWrap();

  EXPECT_TRUE(Add->clone()->hasNoUnsignedWrap());
  EXPECT_TRUE(Add->clone()->hasNoSignedWrap());
  EXPECT_TRUE(Sub->clone()->hasNoUnsignedWrap());
  EXPECT_TRUE(Sub->clone()->hasNoSignedWrap());
  EXPECT_TRUE(Mul->clone()->hasNoUnsignedWrap());
  EXPECT_TRUE(Mul->clone()->hasNoSignedWrap());

  Add->setHasNoUnsignedWrap(false);
  Sub->setHasNoUnsignedWrap(false);
  Mul->setHasNoUnsignedWrap(false);

  EXPECT_FALSE(Add->clone()->hasNoUnsignedWrap());
  EXPECT_TRUE(Add->clone()->hasNoSignedWrap());
  EXPECT_FALSE(Sub->clone()->hasNoUnsignedWrap());
  EXPECT_TRUE(Sub->clone()->hasNoSignedWrap());
  EXPECT_FALSE(Mul->clone()->hasNoUnsignedWrap());
  EXPECT_TRUE(Mul->clone()->hasNoSignedWrap());
}

TEST(CloneInstruction, Inbounds) {
  LLVMContext context;
  Value *V = new Argument(Type::getInt32Ty(context)->getPointerTo());
  Constant *Z = Constant::getNullValue(Type::getInt32Ty(context));
  std::vector<Value *> ops;
  ops.push_back(Z);
  GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops.begin(), ops.end());
  EXPECT_FALSE(GEP->clone()->isInBounds());

  GEP->setIsInBounds();
  EXPECT_TRUE(GEP->clone()->isInBounds());
}

TEST(CloneInstruction, Exact) {
  LLVMContext context;
  Value *V = new Argument(Type::getInt32Ty(context));

  BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V);
  EXPECT_FALSE(SDiv->clone()->isExact());

  SDiv->setIsExact(true);
  EXPECT_TRUE(SDiv->clone()->isExact());
}