ConstantRangeTest.cpp revision 62d7afad8faf7a1fbbf3402f8e23ce4ece9ab108
1a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman//===- llvm/unittest/Support/ConstantRangeTest.cpp - ConstantRange tests --===//
2a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman//
3a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman//                     The LLVM Compiler Infrastructure
4a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman//
5a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman// This file is distributed under the University of Illinois Open Source
6a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman// License. See LICENSE.TXT for details.
7a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman//
8a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman//===----------------------------------------------------------------------===//
9a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
10a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman#include "llvm/Support/ConstantRange.h"
11f2d7b7c879548090beae51b21c4b363da7dbbaadNick Lewycky#include "llvm/Instructions.h"
12a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
13a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman#include "gtest/gtest.h"
14a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
15a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohmanusing namespace llvm;
16a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
17a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohmannamespace {
18a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
1944b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewyckyclass ConstantRangeTest : public ::testing::Test {
2044b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewyckyprotected:
2144b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky  static ConstantRange Full;
2244b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky  static ConstantRange Empty;
2344b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky  static ConstantRange One;
2444b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky  static ConstantRange Some;
2544b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky  static ConstantRange Wrap;
2644b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky};
27a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
2844b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyConstantRange ConstantRangeTest::Full(16);
2944b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyConstantRange ConstantRangeTest::Empty(16, false);
3044b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyConstantRange ConstantRangeTest::One(APInt(16, 0xa));
3144b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyConstantRange ConstantRangeTest::Some(APInt(16, 0xa), APInt(16, 0xaaa));
3244b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyConstantRange ConstantRangeTest::Wrap(APInt(16, 0xaaa), APInt(16, 0xa));
3344b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky
3444b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, Basics) {
35a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(Full.isFullSet());
36a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Full.isEmptySet());
379773e45a1e97f5098905bac26b8b8b7c844473f0Owen Anderson  EXPECT_TRUE(Full.inverse().isEmptySet());
38a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Full.isWrappedSet());
39a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(Full.contains(APInt(16, 0x0)));
40a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(Full.contains(APInt(16, 0x9)));
41a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(Full.contains(APInt(16, 0xa)));
42a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(Full.contains(APInt(16, 0xaa9)));
43a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(Full.contains(APInt(16, 0xaaa)));
44a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
45a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Empty.isFullSet());
46a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(Empty.isEmptySet());
479773e45a1e97f5098905bac26b8b8b7c844473f0Owen Anderson  EXPECT_TRUE(Empty.inverse().isFullSet());
48a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Empty.isWrappedSet());
49a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Empty.contains(APInt(16, 0x0)));
50a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Empty.contains(APInt(16, 0x9)));
51a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Empty.contains(APInt(16, 0xa)));
52a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Empty.contains(APInt(16, 0xaa9)));
53a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Empty.contains(APInt(16, 0xaaa)));
54a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
55a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(One.isFullSet());
56a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(One.isEmptySet());
57a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(One.isWrappedSet());
58a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(One.contains(APInt(16, 0x0)));
59a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(One.contains(APInt(16, 0x9)));
60a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(One.contains(APInt(16, 0xa)));
61a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(One.contains(APInt(16, 0xaa9)));
62a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(One.contains(APInt(16, 0xaaa)));
639773e45a1e97f5098905bac26b8b8b7c844473f0Owen Anderson  EXPECT_FALSE(One.inverse().contains(APInt(16, 0xa)));
64a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
65a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Some.isFullSet());
66a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Some.isEmptySet());
67a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Some.isWrappedSet());
68a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Some.contains(APInt(16, 0x0)));
69a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Some.contains(APInt(16, 0x9)));
70a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(Some.contains(APInt(16, 0xa)));
71a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(Some.contains(APInt(16, 0xaa9)));
72a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Some.contains(APInt(16, 0xaaa)));
73a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
74a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Wrap.isFullSet());
75a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Wrap.isEmptySet());
76a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(Wrap.isWrappedSet());
77a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(Wrap.contains(APInt(16, 0x0)));
78a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(Wrap.contains(APInt(16, 0x9)));
79a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Wrap.contains(APInt(16, 0xa)));
80a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Wrap.contains(APInt(16, 0xaa9)));
81a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(Wrap.contains(APInt(16, 0xaaa)));
8244b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
83a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
8444b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, Equality) {
858211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full, Full);
868211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty, Empty);
878211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One, One);
888211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some, Some);
898211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Wrap, Wrap);
908211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_NE(Full, Empty);
918211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_NE(Full, One);
928211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_NE(Full, Some);
938211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_NE(Full, Wrap);
948211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_NE(Empty, One);
958211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_NE(Empty, Some);
968211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_NE(Empty, Wrap);
978211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_NE(One, Some);
988211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_NE(One, Wrap);
998211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_NE(Some, Wrap);
10044b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
101a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
10244b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, SingleElement) {
103a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Full.getSingleElement(), static_cast<APInt *>(NULL));
104a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Empty.getSingleElement(), static_cast<APInt *>(NULL));
105a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(*One.getSingleElement(), APInt(16, 0xa));
106a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Some.getSingleElement(), static_cast<APInt *>(NULL));
107a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Wrap.getSingleElement(), static_cast<APInt *>(NULL));
108a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
109a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Full.isSingleElement());
110a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Empty.isSingleElement());
111a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(One.isSingleElement());
112a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Some.isSingleElement());
113a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_FALSE(Wrap.isSingleElement());
11444b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
115a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
11644b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, GetSetSize) {
117a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Full.getSetSize(), APInt(16, 0));
118a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Empty.getSetSize(), APInt(16, 0));
119a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(One.getSetSize(), APInt(16, 1));
120a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Some.getSetSize(), APInt(16, 0xaa0));
121a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Wrap.getSetSize(), APInt(16, 0x10000 - 0xaa0));
12244b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
123a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
12444b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, GetMinsAndMaxes) {
125a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Full.getUnsignedMax(), APInt(16, UINT16_MAX));
126a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(One.getUnsignedMax(), APInt(16, 0xa));
127a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Some.getUnsignedMax(), APInt(16, 0xaa9));
128a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Wrap.getUnsignedMax(), APInt(16, UINT16_MAX));
129a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
130a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Full.getUnsignedMin(), APInt(16, 0));
131a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(One.getUnsignedMin(), APInt(16, 0xa));
132a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Some.getUnsignedMin(), APInt(16, 0xa));
133a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Wrap.getUnsignedMin(), APInt(16, 0));
134a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
135a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Full.getSignedMax(), APInt(16, INT16_MAX));
136a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(One.getSignedMax(), APInt(16, 0xa));
137a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Some.getSignedMax(), APInt(16, 0xaa9));
138a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Wrap.getSignedMax(), APInt(16, INT16_MAX));
139a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
140b714c08eb8342d4818732b0248710158714fadd5Ryan Flynn  EXPECT_EQ(Full.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
141a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(One.getSignedMin(), APInt(16, 0xa));
142a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_EQ(Some.getSignedMin(), APInt(16, 0xa));
143b714c08eb8342d4818732b0248710158714fadd5Ryan Flynn  EXPECT_EQ(Wrap.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
144780905e9f9f29419de56b47253f6db84bc659f42Nick Lewycky
145780905e9f9f29419de56b47253f6db84bc659f42Nick Lewycky  // Found by Klee
146780905e9f9f29419de56b47253f6db84bc659f42Nick Lewycky  EXPECT_EQ(ConstantRange(APInt(4, 7), APInt(4, 0)).getSignedMax(),
147780905e9f9f29419de56b47253f6db84bc659f42Nick Lewycky            APInt(4, 7));
14844b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
149a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
15032cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick LewyckyTEST_F(ConstantRangeTest, SignWrapped) {
15132cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_TRUE(Full.isSignWrappedSet());
15232cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_FALSE(Empty.isSignWrappedSet());
15332cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_FALSE(One.isSignWrappedSet());
15432cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_FALSE(Some.isSignWrappedSet());
15532cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_TRUE(Wrap.isSignWrappedSet());
15632cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky
15732cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_FALSE(ConstantRange(APInt(8, 127), APInt(8, 128)).isSignWrappedSet());
15832cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_TRUE(ConstantRange(APInt(8, 127), APInt(8, 129)).isSignWrappedSet());
15932cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_FALSE(ConstantRange(APInt(8, 128), APInt(8, 129)).isSignWrappedSet());
16032cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 9)).isSignWrappedSet());
16132cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 250)).isSignWrappedSet());
16232cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 10)).isSignWrappedSet());
16332cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 251)).isSignWrappedSet());
16432cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky}
16532cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky
16644b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, Trunc) {
167a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange TFull = Full.truncate(10);
168a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange TEmpty = Empty.truncate(10);
169a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange TOne = One.truncate(10);
170a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange TSome = Some.truncate(10);
171a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange TWrap = Wrap.truncate(10);
172a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(TFull.isFullSet());
173a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(TEmpty.isEmptySet());
17440f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  EXPECT_EQ(TOne, ConstantRange(One.getLower().trunc(10),
17540f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad                                One.getUpper().trunc(10)));
176a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(TSome.isFullSet());
17744b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
178a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
17944b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, ZExt) {
180a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange ZFull = Full.zeroExtend(20);
181a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange ZEmpty = Empty.zeroExtend(20);
182a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange ZOne = One.zeroExtend(20);
183a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange ZSome = Some.zeroExtend(20);
184a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange ZWrap = Wrap.zeroExtend(20);
1858211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(ZFull, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
186a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(ZEmpty.isEmptySet());
18740f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  EXPECT_EQ(ZOne, ConstantRange(One.getLower().zext(20),
18840f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad                                One.getUpper().zext(20)));
18940f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  EXPECT_EQ(ZSome, ConstantRange(Some.getLower().zext(20),
19040f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad                                 Some.getUpper().zext(20)));
19132cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_EQ(ZWrap, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
19244b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
193a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
19444b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, SExt) {
195a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange SFull = Full.signExtend(20);
196a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange SEmpty = Empty.signExtend(20);
197a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange SOne = One.signExtend(20);
198a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange SSome = Some.signExtend(20);
199a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  ConstantRange SWrap = Wrap.signExtend(20);
2008211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(SFull, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
2018211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky                                 APInt(20, INT16_MAX + 1, true)));
202a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman  EXPECT_TRUE(SEmpty.isEmptySet());
20340f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  EXPECT_EQ(SOne, ConstantRange(One.getLower().sext(20),
20440f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad                                One.getUpper().sext(20)));
20540f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad  EXPECT_EQ(SSome, ConstantRange(Some.getLower().sext(20),
20640f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad                                 Some.getUpper().sext(20)));
20732cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_EQ(SWrap, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
20832cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky                                 APInt(20, INT16_MAX + 1, true)));
20932cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky
21032cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky  EXPECT_EQ(ConstantRange(APInt(8, 120), APInt(8, 140)).signExtend(16),
21132cda119ef63ae1f1ee4b60e1d9e4a5ea8e00604Nick Lewycky            ConstantRange(APInt(16, -128), APInt(16, 128)));
21244b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
21344b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky
21444b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, IntersectWith) {
2158211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.intersectWith(Full), Empty);
2168211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.intersectWith(Empty), Empty);
2178211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.intersectWith(One), Empty);
2188211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.intersectWith(Some), Empty);
2198211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.intersectWith(Wrap), Empty);
2208211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.intersectWith(Full), Full);
2218211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.intersectWith(Some), Some);
2228211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.intersectWith(One), One);
2238211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.intersectWith(One), One);
2248211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.intersectWith(Some), Some);
2258211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.intersectWith(Wrap), Empty);
2268211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One.intersectWith(Wrap), Empty);
2278211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One.intersectWith(Wrap), Wrap.intersectWith(One));
228a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
2293a4a884c1618d94202ee714ea5c899cd80d1c536Nick Lewycky  // Klee generated testcase from PR4545.
2303a4a884c1618d94202ee714ea5c899cd80d1c536Nick Lewycky  // The intersection of i16 [4, 2) and [6, 5) is disjoint, looking like
2313a4a884c1618d94202ee714ea5c899cd80d1c536Nick Lewycky  // 01..4.6789ABCDEF where the dots represent values not in the intersection.
2323a4a884c1618d94202ee714ea5c899cd80d1c536Nick Lewycky  ConstantRange LHS(APInt(16, 4), APInt(16, 2));
2333a4a884c1618d94202ee714ea5c899cd80d1c536Nick Lewycky  ConstantRange RHS(APInt(16, 6), APInt(16, 5));
2348142ce568d40545582e2e43a9e0aa57cd67d6da2Chris Lattner  EXPECT_TRUE(LHS.intersectWith(RHS) == LHS);
235fbb7a73631e3c23510abb3904e8ad38c87ff2a24Nuno Lopes
236fbb7a73631e3c23510abb3904e8ad38c87ff2a24Nuno Lopes  // previous bug: intersection of [min, 3) and [2, max) should be 2
237532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  LHS = ConstantRange(APInt(32, -2147483646), APInt(32, 3));
238532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  RHS = ConstantRange(APInt(32, 2), APInt(32, 2147483646));
239fbb7a73631e3c23510abb3904e8ad38c87ff2a24Nuno Lopes  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2)));
240532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes
241532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  // [2, 0) /\ [4, 3) = [2, 0)
242532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
243532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  RHS = ConstantRange(APInt(32, 4), APInt(32, 3));
244532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2), APInt(32, 0)));
245532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes
246532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  // [2, 0) /\ [4, 2) = [4, 0)
247532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
248532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  RHS = ConstantRange(APInt(32, 4), APInt(32, 2));
249532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 0)));
250532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes
251532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  // [4, 2) /\ [5, 1) = [5, 1)
252532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
253532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  RHS = ConstantRange(APInt(32, 5), APInt(32, 1));
254532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 5), APInt(32, 1)));
255532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes
256532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  // [2, 0) /\ [7, 4) = [7, 4)
257532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
258532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  RHS = ConstantRange(APInt(32, 7), APInt(32, 4));
259532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 7), APInt(32, 4)));
260532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes
261532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  // [4, 2) /\ [1, 0) = [1, 0)
262532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
263532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  RHS = ConstantRange(APInt(32, 1), APInt(32, 0));
264532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 2)));
265532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes
266532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  // [15, 0) /\ [7, 6) = [15, 0)
267532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  LHS = ConstantRange(APInt(32, 15), APInt(32, 0));
268532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  RHS = ConstantRange(APInt(32, 7), APInt(32, 6));
269532516a87bc57f21e6d99f49548e4c2adf835551Nuno Lopes  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 15), APInt(32, 0)));
27044b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
271a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
27244b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, UnionWith) {
2738211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Wrap.unionWith(One),
2748211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky            ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb)));
2758211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One.unionWith(Wrap), Wrap.unionWith(One));
2768211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.unionWith(Empty), Empty);
2778211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.unionWith(Full), Full);
2788211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.unionWith(Wrap), Full);
2797e7dc45eb15d3739564f2a63ab1c468c57d94ff8Nick Lewycky
2807e7dc45eb15d3739564f2a63ab1c468c57d94ff8Nick Lewycky  // PR4545
2818211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(ConstantRange(APInt(16, 14), APInt(16, 1)).unionWith(
2828211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky                                    ConstantRange(APInt(16, 0), APInt(16, 8))),
2838211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky            ConstantRange(APInt(16, 14), APInt(16, 8)));
2848211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(ConstantRange(APInt(16, 6), APInt(16, 4)).unionWith(
2858211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky                                    ConstantRange(APInt(16, 4), APInt(16, 0))),
2868142ce568d40545582e2e43a9e0aa57cd67d6da2Chris Lattner              ConstantRange(16));
2878211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 0)).unionWith(
2888211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky                                    ConstantRange(APInt(16, 2), APInt(16, 1))),
2898142ce568d40545582e2e43a9e0aa57cd67d6da2Chris Lattner              ConstantRange(16));
29044b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
291a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
29262d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno LopesTEST_F(ConstantRangeTest, SetDifference) {
29362d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes  EXPECT_EQ(Full.difference(Empty), Full);
29462d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes  EXPECT_EQ(Full.difference(Full), Empty);
29562d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes  EXPECT_EQ(Empty.difference(Empty), Empty);
29662d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes  EXPECT_EQ(Empty.difference(Full), Empty);
29762d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes
29862d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes  ConstantRange A(APInt(16, 3), APInt(16, 7));
29962d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes  ConstantRange B(APInt(16, 5), APInt(16, 9));
30062d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes  ConstantRange C(APInt(16, 3), APInt(16, 5));
30162d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes  ConstantRange D(APInt(16, 7), APInt(16, 9));
30262d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes  ConstantRange E(APInt(16, 5), APInt(16, 4));
30362d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes  ConstantRange F(APInt(16, 7), APInt(16, 3));
30462d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes  EXPECT_EQ(A.difference(B), C);
30562d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes  EXPECT_EQ(B.difference(A), D);
30662d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes  EXPECT_EQ(E.difference(A), F);
30762d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes}
30862d7afad8faf7a1fbbf3402f8e23ce4ece9ab108Nuno Lopes
30944b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, SubtractAPInt) {
3108211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.subtract(APInt(16, 4)), Full);
3118211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty);
3128211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.subtract(APInt(16, 4)),
3138211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky            ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
3148211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Wrap.subtract(APInt(16, 4)),
3158211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky            ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
3168211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One.subtract(APInt(16, 4)),
3178211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky            ConstantRange(APInt(16, 0x6)));
31844b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
319a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
32044b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, Add) {
3218211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.add(APInt(16, 4)), Full);
3228211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.add(Full), Full);
3238211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.add(Empty), Empty);
3248211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.add(One), Full);
3258211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.add(Some), Full);
3268211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.add(Wrap), Full);
3278211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.add(Empty), Empty);
3288211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.add(One), Empty);
3298211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.add(Some), Empty);
3308211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.add(Wrap), Empty);
3318211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.add(APInt(16, 4)), Empty);
3328211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.add(APInt(16, 4)),
3337f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky            ConstantRange(APInt(16, 0xe), APInt(16, 0xaae)));
3348211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Wrap.add(APInt(16, 4)),
3357f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky            ConstantRange(APInt(16, 0xaae), APInt(16, 0xe)));
3368211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One.add(APInt(16, 4)),
3377f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky            ConstantRange(APInt(16, 0xe)));
3387f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky}
3397f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky
3407f9ef4bb514d5a28637789d8f397dadd4344dc1bNick LewyckyTEST_F(ConstantRangeTest, Sub) {
3417f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.sub(APInt(16, 4)), Full);
3427f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.sub(Full), Full);
3437f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.sub(Empty), Empty);
3447f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.sub(One), Full);
3457f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.sub(Some), Full);
3467f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.sub(Wrap), Full);
3477f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Empty.sub(Empty), Empty);
3487f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Empty.sub(One), Empty);
3497f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Empty.sub(Some), Empty);
3507f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Empty.sub(Wrap), Empty);
3517f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Empty.sub(APInt(16, 4)), Empty);
3527f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Some.sub(APInt(16, 4)),
3537f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky            ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
354e6240e8b83e9bd60e622650e9e801ebc957172b4Nick Lewycky  EXPECT_EQ(Some.sub(Some),
355e6240e8b83e9bd60e622650e9e801ebc957172b4Nick Lewycky            ConstantRange(APInt(16, 0xf561), APInt(16, 0xaa0)));
3567f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Wrap.sub(APInt(16, 4)),
3577f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky            ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
3587f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(One.sub(APInt(16, 4)),
3597f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky            ConstantRange(APInt(16, 0x6)));
36044b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
361a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
36244b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, Multiply) {
3638211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.multiply(Full), Full);
3648211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.multiply(Empty), Empty);
3658211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.multiply(One), Full);
3668211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.multiply(Some), Full);
3678211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.multiply(Wrap), Full);
3688211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.multiply(Empty), Empty);
3698211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.multiply(One), Empty);
3708211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.multiply(Some), Empty);
3718211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.multiply(Wrap), Empty);
3728211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa),
3738211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky                                             APInt(16, 0xa*0xa + 1)));
3748211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa),
3758211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky                                              APInt(16, 0xa*0xaa9 + 1)));
3768211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One.multiply(Wrap), Full);
3778211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.multiply(Some), Full);
3788211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.multiply(Wrap), Full);
3798211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Wrap.multiply(Wrap), Full);
380f1db120d0494ec55d9265cea7dab22e80dcae10cNick Lewycky
381f1db120d0494ec55d9265cea7dab22e80dcae10cNick Lewycky  // http://llvm.org/PR4545
3828211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply(
3838211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky                ConstantRange(APInt(4, 6), APInt(4, 2))),
3848211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky            ConstantRange(4, /*isFullSet=*/true));
38544b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
38644b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky
38744b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, UMax) {
3888211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.umax(Full), Full);
3898211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.umax(Empty), Empty);
3908211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
3918211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.umax(Wrap), Full);
3928211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
3938211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.umax(Empty), Empty);
3948211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.umax(Some), Empty);
3958211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.umax(Wrap), Empty);
3968211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.umax(One), Empty);
3978211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.umax(Some), Some);
3988211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.umax(Wrap), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
3998211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.umax(One), Some);
40044b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky  // TODO: ConstantRange is currently over-conservative here.
4018211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Wrap.umax(Wrap), Full);
4028211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Wrap.umax(One), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
4038211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One.umax(One), One);
40444b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
405a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
40644b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, SMax) {
4078211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.smax(Full), Full);
4088211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.smax(Empty), Empty);
4098211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.smax(Some), ConstantRange(APInt(16, 0xa),
4108211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky                                           APInt::getSignedMinValue(16)));
4118211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.smax(Wrap), Full);
4128211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.smax(One), ConstantRange(APInt(16, 0xa),
4138211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky                                          APInt::getSignedMinValue(16)));
4148211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.smax(Empty), Empty);
4158211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.smax(Some), Empty);
4168211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.smax(Wrap), Empty);
4178211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.smax(One), Empty);
4188211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.smax(Some), Some);
4198211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa),
4208211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky                                           APInt(16, (uint64_t)INT16_MIN)));
4218211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.smax(One), Some);
4228211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa),
4238211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky                                          APInt(16, (uint64_t)INT16_MIN)));
4248211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One.smax(One), One);
42544b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky}
426a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
42744b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, UDiv) {
4288211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.udiv(Full), Full);
4298211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.udiv(Empty), Empty);
4308211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.udiv(One), ConstantRange(APInt(16, 0),
4318211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky                                          APInt(16, 0xffff / 0xa + 1)));
4328211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.udiv(Some), ConstantRange(APInt(16, 0),
4338211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky                                           APInt(16, 0xffff / 0xa + 1)));
4348211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Full.udiv(Wrap), Full);
4358211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.udiv(Empty), Empty);
4368211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.udiv(One), Empty);
4378211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.udiv(Some), Empty);
4388211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Empty.udiv(Wrap), Empty);
4398211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One.udiv(One), ConstantRange(APInt(16, 1)));
4408211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 2)));
4418211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(One.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
4428211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 0x111)));
4438211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Some.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
4448211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky  EXPECT_EQ(Wrap.udiv(Wrap), Full);
445a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman}
446a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman
4477f9ef4bb514d5a28637789d8f397dadd4344dc1bNick LewyckyTEST_F(ConstantRangeTest, Shl) {
4487f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.shl(Full), Full);
4497f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.shl(Empty), Empty);
4507f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.shl(One), Full);    // TODO: [0, (-1 << 0xa) + 1)
4517f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.shl(Some), Full);   // TODO: [0, (-1 << 0xa) + 1)
4527f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.shl(Wrap), Full);
4537f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Empty.shl(Empty), Empty);
4547f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Empty.shl(One), Empty);
4557f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Empty.shl(Some), Empty);
4567f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Empty.shl(Wrap), Empty);
4577f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(One.shl(One), ConstantRange(APInt(16, 0xa << 0xa),
4587f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky                                        APInt(16, (0xa << 0xa) + 1)));
4597f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(One.shl(Some), Full);    // TODO: [0xa << 0xa, 0)
4607f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(One.shl(Wrap), Full);    // TODO: [0xa, 0xa << 14 + 1)
4617f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Some.shl(Some), Full);   // TODO: [0xa << 0xa, 0xfc01)
4627f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Some.shl(Wrap), Full);   // TODO: [0xa, 0x7ff << 0x5 + 1)
4637f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Wrap.shl(Wrap), Full);
4647f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky}
4657f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky
4667f9ef4bb514d5a28637789d8f397dadd4344dc1bNick LewyckyTEST_F(ConstantRangeTest, Lshr) {
4677f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.lshr(Full), Full);
4687f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.lshr(Empty), Empty);
4697f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.lshr(One), ConstantRange(APInt(16, 0),
4707f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky                                          APInt(16, (0xffff >> 0xa) + 1)));
4717f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.lshr(Some), ConstantRange(APInt(16, 0),
4727f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky                                           APInt(16, (0xffff >> 0xa) + 1)));
4737f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Full.lshr(Wrap), Full);
4747f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Empty.lshr(Empty), Empty);
4757f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Empty.lshr(One), Empty);
4767f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Empty.lshr(Some), Empty);
4777f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Empty.lshr(Wrap), Empty);
4787f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(One.lshr(One), ConstantRange(APInt(16, 0)));
4797f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(One.lshr(Some), ConstantRange(APInt(16, 0)));
4807f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(One.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
4817f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Some.lshr(Some), ConstantRange(APInt(16, 0),
4827f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky                                           APInt(16, (0xaaa >> 0xa) + 1)));
4837f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Some.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
4847f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky  EXPECT_EQ(Wrap.lshr(Wrap), Full);
4857f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky}
4867f9ef4bb514d5a28637789d8f397dadd4344dc1bNick Lewycky
487f2d7b7c879548090beae51b21c4b363da7dbbaadNick LewyckyTEST(ConstantRange, MakeICmpRegion) {
488f2d7b7c879548090beae51b21c4b363da7dbbaadNick Lewycky  // PR8250
489f2d7b7c879548090beae51b21c4b363da7dbbaadNick Lewycky  ConstantRange SMax = ConstantRange(APInt::getSignedMaxValue(32));
490f2d7b7c879548090beae51b21c4b363da7dbbaadNick Lewycky  EXPECT_TRUE(ConstantRange::makeICmpRegion(ICmpInst::ICMP_SGT,
491f2d7b7c879548090beae51b21c4b363da7dbbaadNick Lewycky                                            SMax).isEmptySet());
492f2d7b7c879548090beae51b21c4b363da7dbbaadNick Lewycky}
493f2d7b7c879548090beae51b21c4b363da7dbbaadNick Lewycky
494a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman}  // anonymous namespace
495