Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/ConstantRangeTest.cpp - ConstantRange tests --===// |
| 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 "llvm/Support/ConstantRange.h" |
Nick Lewycky | f2d7b7c | 2010-09-28 18:18:36 +0000 | [diff] [blame] | 11 | #include "llvm/Instructions.h" |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 12 | |
| 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | using namespace llvm; |
| 16 | |
| 17 | namespace { |
| 18 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 19 | class ConstantRangeTest : public ::testing::Test { |
| 20 | protected: |
| 21 | static ConstantRange Full; |
| 22 | static ConstantRange Empty; |
| 23 | static ConstantRange One; |
| 24 | static ConstantRange Some; |
| 25 | static ConstantRange Wrap; |
| 26 | }; |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 27 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 28 | ConstantRange ConstantRangeTest::Full(16); |
| 29 | ConstantRange ConstantRangeTest::Empty(16, false); |
| 30 | ConstantRange ConstantRangeTest::One(APInt(16, 0xa)); |
| 31 | ConstantRange ConstantRangeTest::Some(APInt(16, 0xa), APInt(16, 0xaaa)); |
| 32 | ConstantRange ConstantRangeTest::Wrap(APInt(16, 0xaaa), APInt(16, 0xa)); |
| 33 | |
| 34 | TEST_F(ConstantRangeTest, Basics) { |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 35 | EXPECT_TRUE(Full.isFullSet()); |
| 36 | EXPECT_FALSE(Full.isEmptySet()); |
Owen Anderson | 9773e45 | 2010-08-07 05:47:46 +0000 | [diff] [blame] | 37 | EXPECT_TRUE(Full.inverse().isEmptySet()); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 38 | EXPECT_FALSE(Full.isWrappedSet()); |
| 39 | EXPECT_TRUE(Full.contains(APInt(16, 0x0))); |
| 40 | EXPECT_TRUE(Full.contains(APInt(16, 0x9))); |
| 41 | EXPECT_TRUE(Full.contains(APInt(16, 0xa))); |
| 42 | EXPECT_TRUE(Full.contains(APInt(16, 0xaa9))); |
| 43 | EXPECT_TRUE(Full.contains(APInt(16, 0xaaa))); |
| 44 | |
| 45 | EXPECT_FALSE(Empty.isFullSet()); |
| 46 | EXPECT_TRUE(Empty.isEmptySet()); |
Owen Anderson | 9773e45 | 2010-08-07 05:47:46 +0000 | [diff] [blame] | 47 | EXPECT_TRUE(Empty.inverse().isFullSet()); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 48 | EXPECT_FALSE(Empty.isWrappedSet()); |
| 49 | EXPECT_FALSE(Empty.contains(APInt(16, 0x0))); |
| 50 | EXPECT_FALSE(Empty.contains(APInt(16, 0x9))); |
| 51 | EXPECT_FALSE(Empty.contains(APInt(16, 0xa))); |
| 52 | EXPECT_FALSE(Empty.contains(APInt(16, 0xaa9))); |
| 53 | EXPECT_FALSE(Empty.contains(APInt(16, 0xaaa))); |
| 54 | |
| 55 | EXPECT_FALSE(One.isFullSet()); |
| 56 | EXPECT_FALSE(One.isEmptySet()); |
| 57 | EXPECT_FALSE(One.isWrappedSet()); |
| 58 | EXPECT_FALSE(One.contains(APInt(16, 0x0))); |
| 59 | EXPECT_FALSE(One.contains(APInt(16, 0x9))); |
| 60 | EXPECT_TRUE(One.contains(APInt(16, 0xa))); |
| 61 | EXPECT_FALSE(One.contains(APInt(16, 0xaa9))); |
| 62 | EXPECT_FALSE(One.contains(APInt(16, 0xaaa))); |
Owen Anderson | 9773e45 | 2010-08-07 05:47:46 +0000 | [diff] [blame] | 63 | EXPECT_FALSE(One.inverse().contains(APInt(16, 0xa))); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 64 | |
| 65 | EXPECT_FALSE(Some.isFullSet()); |
| 66 | EXPECT_FALSE(Some.isEmptySet()); |
| 67 | EXPECT_FALSE(Some.isWrappedSet()); |
| 68 | EXPECT_FALSE(Some.contains(APInt(16, 0x0))); |
| 69 | EXPECT_FALSE(Some.contains(APInt(16, 0x9))); |
| 70 | EXPECT_TRUE(Some.contains(APInt(16, 0xa))); |
| 71 | EXPECT_TRUE(Some.contains(APInt(16, 0xaa9))); |
| 72 | EXPECT_FALSE(Some.contains(APInt(16, 0xaaa))); |
| 73 | |
| 74 | EXPECT_FALSE(Wrap.isFullSet()); |
| 75 | EXPECT_FALSE(Wrap.isEmptySet()); |
| 76 | EXPECT_TRUE(Wrap.isWrappedSet()); |
| 77 | EXPECT_TRUE(Wrap.contains(APInt(16, 0x0))); |
| 78 | EXPECT_TRUE(Wrap.contains(APInt(16, 0x9))); |
| 79 | EXPECT_FALSE(Wrap.contains(APInt(16, 0xa))); |
| 80 | EXPECT_FALSE(Wrap.contains(APInt(16, 0xaa9))); |
| 81 | EXPECT_TRUE(Wrap.contains(APInt(16, 0xaaa))); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 82 | } |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 83 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 84 | TEST_F(ConstantRangeTest, Equality) { |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 85 | EXPECT_EQ(Full, Full); |
| 86 | EXPECT_EQ(Empty, Empty); |
| 87 | EXPECT_EQ(One, One); |
| 88 | EXPECT_EQ(Some, Some); |
| 89 | EXPECT_EQ(Wrap, Wrap); |
| 90 | EXPECT_NE(Full, Empty); |
| 91 | EXPECT_NE(Full, One); |
| 92 | EXPECT_NE(Full, Some); |
| 93 | EXPECT_NE(Full, Wrap); |
| 94 | EXPECT_NE(Empty, One); |
| 95 | EXPECT_NE(Empty, Some); |
| 96 | EXPECT_NE(Empty, Wrap); |
| 97 | EXPECT_NE(One, Some); |
| 98 | EXPECT_NE(One, Wrap); |
| 99 | EXPECT_NE(Some, Wrap); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 100 | } |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 101 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 102 | TEST_F(ConstantRangeTest, SingleElement) { |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 103 | EXPECT_EQ(Full.getSingleElement(), static_cast<APInt *>(NULL)); |
| 104 | EXPECT_EQ(Empty.getSingleElement(), static_cast<APInt *>(NULL)); |
| 105 | EXPECT_EQ(*One.getSingleElement(), APInt(16, 0xa)); |
| 106 | EXPECT_EQ(Some.getSingleElement(), static_cast<APInt *>(NULL)); |
| 107 | EXPECT_EQ(Wrap.getSingleElement(), static_cast<APInt *>(NULL)); |
| 108 | |
| 109 | EXPECT_FALSE(Full.isSingleElement()); |
| 110 | EXPECT_FALSE(Empty.isSingleElement()); |
| 111 | EXPECT_TRUE(One.isSingleElement()); |
| 112 | EXPECT_FALSE(Some.isSingleElement()); |
| 113 | EXPECT_FALSE(Wrap.isSingleElement()); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 114 | } |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 115 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 116 | TEST_F(ConstantRangeTest, GetSetSize) { |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 117 | EXPECT_EQ(Full.getSetSize(), APInt(16, 0)); |
| 118 | EXPECT_EQ(Empty.getSetSize(), APInt(16, 0)); |
| 119 | EXPECT_EQ(One.getSetSize(), APInt(16, 1)); |
| 120 | EXPECT_EQ(Some.getSetSize(), APInt(16, 0xaa0)); |
| 121 | EXPECT_EQ(Wrap.getSetSize(), APInt(16, 0x10000 - 0xaa0)); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 122 | } |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 123 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 124 | TEST_F(ConstantRangeTest, GetMinsAndMaxes) { |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 125 | EXPECT_EQ(Full.getUnsignedMax(), APInt(16, UINT16_MAX)); |
| 126 | EXPECT_EQ(One.getUnsignedMax(), APInt(16, 0xa)); |
| 127 | EXPECT_EQ(Some.getUnsignedMax(), APInt(16, 0xaa9)); |
| 128 | EXPECT_EQ(Wrap.getUnsignedMax(), APInt(16, UINT16_MAX)); |
| 129 | |
| 130 | EXPECT_EQ(Full.getUnsignedMin(), APInt(16, 0)); |
| 131 | EXPECT_EQ(One.getUnsignedMin(), APInt(16, 0xa)); |
| 132 | EXPECT_EQ(Some.getUnsignedMin(), APInt(16, 0xa)); |
| 133 | EXPECT_EQ(Wrap.getUnsignedMin(), APInt(16, 0)); |
| 134 | |
| 135 | EXPECT_EQ(Full.getSignedMax(), APInt(16, INT16_MAX)); |
| 136 | EXPECT_EQ(One.getSignedMax(), APInt(16, 0xa)); |
| 137 | EXPECT_EQ(Some.getSignedMax(), APInt(16, 0xaa9)); |
| 138 | EXPECT_EQ(Wrap.getSignedMax(), APInt(16, INT16_MAX)); |
| 139 | |
Ryan Flynn | b714c08 | 2009-07-22 16:17:36 +0000 | [diff] [blame] | 140 | EXPECT_EQ(Full.getSignedMin(), APInt(16, (uint64_t)INT16_MIN)); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 141 | EXPECT_EQ(One.getSignedMin(), APInt(16, 0xa)); |
| 142 | EXPECT_EQ(Some.getSignedMin(), APInt(16, 0xa)); |
Ryan Flynn | b714c08 | 2009-07-22 16:17:36 +0000 | [diff] [blame] | 143 | EXPECT_EQ(Wrap.getSignedMin(), APInt(16, (uint64_t)INT16_MIN)); |
Nick Lewycky | 780905e | 2009-07-13 04:50:21 +0000 | [diff] [blame] | 144 | |
| 145 | // Found by Klee |
| 146 | EXPECT_EQ(ConstantRange(APInt(4, 7), APInt(4, 0)).getSignedMax(), |
| 147 | APInt(4, 7)); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 148 | } |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 149 | |
Nick Lewycky | 32cda11 | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 150 | TEST_F(ConstantRangeTest, SignWrapped) { |
| 151 | EXPECT_TRUE(Full.isSignWrappedSet()); |
| 152 | EXPECT_FALSE(Empty.isSignWrappedSet()); |
| 153 | EXPECT_FALSE(One.isSignWrappedSet()); |
| 154 | EXPECT_FALSE(Some.isSignWrappedSet()); |
| 155 | EXPECT_TRUE(Wrap.isSignWrappedSet()); |
| 156 | |
| 157 | EXPECT_FALSE(ConstantRange(APInt(8, 127), APInt(8, 128)).isSignWrappedSet()); |
| 158 | EXPECT_TRUE(ConstantRange(APInt(8, 127), APInt(8, 129)).isSignWrappedSet()); |
| 159 | EXPECT_FALSE(ConstantRange(APInt(8, 128), APInt(8, 129)).isSignWrappedSet()); |
| 160 | EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 9)).isSignWrappedSet()); |
| 161 | EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 250)).isSignWrappedSet()); |
| 162 | EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 10)).isSignWrappedSet()); |
| 163 | EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 251)).isSignWrappedSet()); |
| 164 | } |
| 165 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 166 | TEST_F(ConstantRangeTest, Trunc) { |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 167 | ConstantRange TFull = Full.truncate(10); |
| 168 | ConstantRange TEmpty = Empty.truncate(10); |
| 169 | ConstantRange TOne = One.truncate(10); |
| 170 | ConstantRange TSome = Some.truncate(10); |
| 171 | ConstantRange TWrap = Wrap.truncate(10); |
| 172 | EXPECT_TRUE(TFull.isFullSet()); |
| 173 | EXPECT_TRUE(TEmpty.isEmptySet()); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 174 | EXPECT_EQ(TOne, ConstantRange(One.getLower().trunc(10), |
| 175 | One.getUpper().trunc(10))); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 176 | EXPECT_TRUE(TSome.isFullSet()); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 177 | } |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 178 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 179 | TEST_F(ConstantRangeTest, ZExt) { |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 180 | ConstantRange ZFull = Full.zeroExtend(20); |
| 181 | ConstantRange ZEmpty = Empty.zeroExtend(20); |
| 182 | ConstantRange ZOne = One.zeroExtend(20); |
| 183 | ConstantRange ZSome = Some.zeroExtend(20); |
| 184 | ConstantRange ZWrap = Wrap.zeroExtend(20); |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 185 | EXPECT_EQ(ZFull, ConstantRange(APInt(20, 0), APInt(20, 0x10000))); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 186 | EXPECT_TRUE(ZEmpty.isEmptySet()); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 187 | EXPECT_EQ(ZOne, ConstantRange(One.getLower().zext(20), |
| 188 | One.getUpper().zext(20))); |
| 189 | EXPECT_EQ(ZSome, ConstantRange(Some.getLower().zext(20), |
| 190 | Some.getUpper().zext(20))); |
Nick Lewycky | 32cda11 | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 191 | EXPECT_EQ(ZWrap, ConstantRange(APInt(20, 0), APInt(20, 0x10000))); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 192 | } |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 193 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 194 | TEST_F(ConstantRangeTest, SExt) { |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 195 | ConstantRange SFull = Full.signExtend(20); |
| 196 | ConstantRange SEmpty = Empty.signExtend(20); |
| 197 | ConstantRange SOne = One.signExtend(20); |
| 198 | ConstantRange SSome = Some.signExtend(20); |
| 199 | ConstantRange SWrap = Wrap.signExtend(20); |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 200 | EXPECT_EQ(SFull, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true), |
| 201 | APInt(20, INT16_MAX + 1, true))); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 202 | EXPECT_TRUE(SEmpty.isEmptySet()); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 203 | EXPECT_EQ(SOne, ConstantRange(One.getLower().sext(20), |
| 204 | One.getUpper().sext(20))); |
| 205 | EXPECT_EQ(SSome, ConstantRange(Some.getLower().sext(20), |
| 206 | Some.getUpper().sext(20))); |
Nick Lewycky | 32cda11 | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 207 | EXPECT_EQ(SWrap, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true), |
| 208 | APInt(20, INT16_MAX + 1, true))); |
| 209 | |
| 210 | EXPECT_EQ(ConstantRange(APInt(8, 120), APInt(8, 140)).signExtend(16), |
| 211 | ConstantRange(APInt(16, -128), APInt(16, 128))); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | TEST_F(ConstantRangeTest, IntersectWith) { |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 215 | EXPECT_EQ(Empty.intersectWith(Full), Empty); |
| 216 | EXPECT_EQ(Empty.intersectWith(Empty), Empty); |
| 217 | EXPECT_EQ(Empty.intersectWith(One), Empty); |
| 218 | EXPECT_EQ(Empty.intersectWith(Some), Empty); |
| 219 | EXPECT_EQ(Empty.intersectWith(Wrap), Empty); |
| 220 | EXPECT_EQ(Full.intersectWith(Full), Full); |
| 221 | EXPECT_EQ(Some.intersectWith(Some), Some); |
| 222 | EXPECT_EQ(Some.intersectWith(One), One); |
| 223 | EXPECT_EQ(Full.intersectWith(One), One); |
| 224 | EXPECT_EQ(Full.intersectWith(Some), Some); |
| 225 | EXPECT_EQ(Some.intersectWith(Wrap), Empty); |
| 226 | EXPECT_EQ(One.intersectWith(Wrap), Empty); |
| 227 | EXPECT_EQ(One.intersectWith(Wrap), Wrap.intersectWith(One)); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 228 | |
Nick Lewycky | 3a4a884 | 2009-07-18 06:34:42 +0000 | [diff] [blame] | 229 | // Klee generated testcase from PR4545. |
| 230 | // The intersection of i16 [4, 2) and [6, 5) is disjoint, looking like |
| 231 | // 01..4.6789ABCDEF where the dots represent values not in the intersection. |
| 232 | ConstantRange LHS(APInt(16, 4), APInt(16, 2)); |
| 233 | ConstantRange RHS(APInt(16, 6), APInt(16, 5)); |
Chris Lattner | 8142ce5 | 2009-08-23 06:32:25 +0000 | [diff] [blame] | 234 | EXPECT_TRUE(LHS.intersectWith(RHS) == LHS); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 235 | } |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 236 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 237 | TEST_F(ConstantRangeTest, UnionWith) { |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 238 | EXPECT_EQ(Wrap.unionWith(One), |
| 239 | ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb))); |
| 240 | EXPECT_EQ(One.unionWith(Wrap), Wrap.unionWith(One)); |
| 241 | EXPECT_EQ(Empty.unionWith(Empty), Empty); |
| 242 | EXPECT_EQ(Full.unionWith(Full), Full); |
| 243 | EXPECT_EQ(Some.unionWith(Wrap), Full); |
Nick Lewycky | 7e7dc45 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 244 | |
| 245 | // PR4545 |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 246 | EXPECT_EQ(ConstantRange(APInt(16, 14), APInt(16, 1)).unionWith( |
| 247 | ConstantRange(APInt(16, 0), APInt(16, 8))), |
| 248 | ConstantRange(APInt(16, 14), APInt(16, 8))); |
| 249 | EXPECT_EQ(ConstantRange(APInt(16, 6), APInt(16, 4)).unionWith( |
| 250 | ConstantRange(APInt(16, 4), APInt(16, 0))), |
Chris Lattner | 8142ce5 | 2009-08-23 06:32:25 +0000 | [diff] [blame] | 251 | ConstantRange(16)); |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 252 | EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 0)).unionWith( |
| 253 | ConstantRange(APInt(16, 2), APInt(16, 1))), |
Chris Lattner | 8142ce5 | 2009-08-23 06:32:25 +0000 | [diff] [blame] | 254 | ConstantRange(16)); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 255 | } |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 256 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 257 | TEST_F(ConstantRangeTest, SubtractAPInt) { |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 258 | EXPECT_EQ(Full.subtract(APInt(16, 4)), Full); |
| 259 | EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty); |
| 260 | EXPECT_EQ(Some.subtract(APInt(16, 4)), |
| 261 | ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6))); |
| 262 | EXPECT_EQ(Wrap.subtract(APInt(16, 4)), |
| 263 | ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6))); |
| 264 | EXPECT_EQ(One.subtract(APInt(16, 4)), |
| 265 | ConstantRange(APInt(16, 0x6))); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 266 | } |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 267 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 268 | TEST_F(ConstantRangeTest, Add) { |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 269 | EXPECT_EQ(Full.add(APInt(16, 4)), Full); |
| 270 | EXPECT_EQ(Full.add(Full), Full); |
| 271 | EXPECT_EQ(Full.add(Empty), Empty); |
| 272 | EXPECT_EQ(Full.add(One), Full); |
| 273 | EXPECT_EQ(Full.add(Some), Full); |
| 274 | EXPECT_EQ(Full.add(Wrap), Full); |
| 275 | EXPECT_EQ(Empty.add(Empty), Empty); |
| 276 | EXPECT_EQ(Empty.add(One), Empty); |
| 277 | EXPECT_EQ(Empty.add(Some), Empty); |
| 278 | EXPECT_EQ(Empty.add(Wrap), Empty); |
| 279 | EXPECT_EQ(Empty.add(APInt(16, 4)), Empty); |
| 280 | EXPECT_EQ(Some.add(APInt(16, 4)), |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 281 | ConstantRange(APInt(16, 0xe), APInt(16, 0xaae))); |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 282 | EXPECT_EQ(Wrap.add(APInt(16, 4)), |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 283 | ConstantRange(APInt(16, 0xaae), APInt(16, 0xe))); |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 284 | EXPECT_EQ(One.add(APInt(16, 4)), |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 285 | ConstantRange(APInt(16, 0xe))); |
| 286 | } |
| 287 | |
| 288 | TEST_F(ConstantRangeTest, Sub) { |
| 289 | EXPECT_EQ(Full.sub(APInt(16, 4)), Full); |
| 290 | EXPECT_EQ(Full.sub(Full), Full); |
| 291 | EXPECT_EQ(Full.sub(Empty), Empty); |
| 292 | EXPECT_EQ(Full.sub(One), Full); |
| 293 | EXPECT_EQ(Full.sub(Some), Full); |
| 294 | EXPECT_EQ(Full.sub(Wrap), Full); |
| 295 | EXPECT_EQ(Empty.sub(Empty), Empty); |
| 296 | EXPECT_EQ(Empty.sub(One), Empty); |
| 297 | EXPECT_EQ(Empty.sub(Some), Empty); |
| 298 | EXPECT_EQ(Empty.sub(Wrap), Empty); |
| 299 | EXPECT_EQ(Empty.sub(APInt(16, 4)), Empty); |
| 300 | EXPECT_EQ(Some.sub(APInt(16, 4)), |
| 301 | ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6))); |
| 302 | EXPECT_EQ(Wrap.sub(APInt(16, 4)), |
| 303 | ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6))); |
| 304 | EXPECT_EQ(One.sub(APInt(16, 4)), |
| 305 | ConstantRange(APInt(16, 0x6))); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 306 | } |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 307 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 308 | TEST_F(ConstantRangeTest, Multiply) { |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 309 | EXPECT_EQ(Full.multiply(Full), Full); |
| 310 | EXPECT_EQ(Full.multiply(Empty), Empty); |
| 311 | EXPECT_EQ(Full.multiply(One), Full); |
| 312 | EXPECT_EQ(Full.multiply(Some), Full); |
| 313 | EXPECT_EQ(Full.multiply(Wrap), Full); |
| 314 | EXPECT_EQ(Empty.multiply(Empty), Empty); |
| 315 | EXPECT_EQ(Empty.multiply(One), Empty); |
| 316 | EXPECT_EQ(Empty.multiply(Some), Empty); |
| 317 | EXPECT_EQ(Empty.multiply(Wrap), Empty); |
| 318 | EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa), |
| 319 | APInt(16, 0xa*0xa + 1))); |
| 320 | EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa), |
| 321 | APInt(16, 0xa*0xaa9 + 1))); |
| 322 | EXPECT_EQ(One.multiply(Wrap), Full); |
| 323 | EXPECT_EQ(Some.multiply(Some), Full); |
| 324 | EXPECT_EQ(Some.multiply(Wrap), Full); |
| 325 | EXPECT_EQ(Wrap.multiply(Wrap), Full); |
Nick Lewycky | f1db120 | 2009-07-13 03:27:41 +0000 | [diff] [blame] | 326 | |
| 327 | // http://llvm.org/PR4545 |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 328 | EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply( |
| 329 | ConstantRange(APInt(4, 6), APInt(4, 2))), |
| 330 | ConstantRange(4, /*isFullSet=*/true)); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 331 | } |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 332 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 333 | TEST_F(ConstantRangeTest, UMax) { |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 334 | EXPECT_EQ(Full.umax(Full), Full); |
| 335 | EXPECT_EQ(Full.umax(Empty), Empty); |
| 336 | EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0))); |
| 337 | EXPECT_EQ(Full.umax(Wrap), Full); |
| 338 | EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0))); |
| 339 | EXPECT_EQ(Empty.umax(Empty), Empty); |
| 340 | EXPECT_EQ(Empty.umax(Some), Empty); |
| 341 | EXPECT_EQ(Empty.umax(Wrap), Empty); |
| 342 | EXPECT_EQ(Empty.umax(One), Empty); |
| 343 | EXPECT_EQ(Some.umax(Some), Some); |
| 344 | EXPECT_EQ(Some.umax(Wrap), ConstantRange(APInt(16, 0xa), APInt(16, 0))); |
| 345 | EXPECT_EQ(Some.umax(One), Some); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 346 | // TODO: ConstantRange is currently over-conservative here. |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 347 | EXPECT_EQ(Wrap.umax(Wrap), Full); |
| 348 | EXPECT_EQ(Wrap.umax(One), ConstantRange(APInt(16, 0xa), APInt(16, 0))); |
| 349 | EXPECT_EQ(One.umax(One), One); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | TEST_F(ConstantRangeTest, SMax) { |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 353 | EXPECT_EQ(Full.smax(Full), Full); |
| 354 | EXPECT_EQ(Full.smax(Empty), Empty); |
| 355 | EXPECT_EQ(Full.smax(Some), ConstantRange(APInt(16, 0xa), |
| 356 | APInt::getSignedMinValue(16))); |
| 357 | EXPECT_EQ(Full.smax(Wrap), Full); |
| 358 | EXPECT_EQ(Full.smax(One), ConstantRange(APInt(16, 0xa), |
| 359 | APInt::getSignedMinValue(16))); |
| 360 | EXPECT_EQ(Empty.smax(Empty), Empty); |
| 361 | EXPECT_EQ(Empty.smax(Some), Empty); |
| 362 | EXPECT_EQ(Empty.smax(Wrap), Empty); |
| 363 | EXPECT_EQ(Empty.smax(One), Empty); |
| 364 | EXPECT_EQ(Some.smax(Some), Some); |
| 365 | EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa), |
| 366 | APInt(16, (uint64_t)INT16_MIN))); |
| 367 | EXPECT_EQ(Some.smax(One), Some); |
| 368 | EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa), |
| 369 | APInt(16, (uint64_t)INT16_MIN))); |
| 370 | EXPECT_EQ(One.smax(One), One); |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 371 | } |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 372 | |
Nick Lewycky | 44b3e8d | 2009-07-11 18:43:20 +0000 | [diff] [blame] | 373 | TEST_F(ConstantRangeTest, UDiv) { |
Nick Lewycky | 8211bec | 2009-09-05 18:27:40 +0000 | [diff] [blame] | 374 | EXPECT_EQ(Full.udiv(Full), Full); |
| 375 | EXPECT_EQ(Full.udiv(Empty), Empty); |
| 376 | EXPECT_EQ(Full.udiv(One), ConstantRange(APInt(16, 0), |
| 377 | APInt(16, 0xffff / 0xa + 1))); |
| 378 | EXPECT_EQ(Full.udiv(Some), ConstantRange(APInt(16, 0), |
| 379 | APInt(16, 0xffff / 0xa + 1))); |
| 380 | EXPECT_EQ(Full.udiv(Wrap), Full); |
| 381 | EXPECT_EQ(Empty.udiv(Empty), Empty); |
| 382 | EXPECT_EQ(Empty.udiv(One), Empty); |
| 383 | EXPECT_EQ(Empty.udiv(Some), Empty); |
| 384 | EXPECT_EQ(Empty.udiv(Wrap), Empty); |
| 385 | EXPECT_EQ(One.udiv(One), ConstantRange(APInt(16, 1))); |
| 386 | EXPECT_EQ(One.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 2))); |
| 387 | EXPECT_EQ(One.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb))); |
| 388 | EXPECT_EQ(Some.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 0x111))); |
| 389 | EXPECT_EQ(Some.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa))); |
| 390 | EXPECT_EQ(Wrap.udiv(Wrap), Full); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 393 | TEST_F(ConstantRangeTest, Shl) { |
| 394 | EXPECT_EQ(Full.shl(Full), Full); |
| 395 | EXPECT_EQ(Full.shl(Empty), Empty); |
| 396 | EXPECT_EQ(Full.shl(One), Full); // TODO: [0, (-1 << 0xa) + 1) |
| 397 | EXPECT_EQ(Full.shl(Some), Full); // TODO: [0, (-1 << 0xa) + 1) |
| 398 | EXPECT_EQ(Full.shl(Wrap), Full); |
| 399 | EXPECT_EQ(Empty.shl(Empty), Empty); |
| 400 | EXPECT_EQ(Empty.shl(One), Empty); |
| 401 | EXPECT_EQ(Empty.shl(Some), Empty); |
| 402 | EXPECT_EQ(Empty.shl(Wrap), Empty); |
| 403 | EXPECT_EQ(One.shl(One), ConstantRange(APInt(16, 0xa << 0xa), |
| 404 | APInt(16, (0xa << 0xa) + 1))); |
| 405 | EXPECT_EQ(One.shl(Some), Full); // TODO: [0xa << 0xa, 0) |
| 406 | EXPECT_EQ(One.shl(Wrap), Full); // TODO: [0xa, 0xa << 14 + 1) |
| 407 | EXPECT_EQ(Some.shl(Some), Full); // TODO: [0xa << 0xa, 0xfc01) |
| 408 | EXPECT_EQ(Some.shl(Wrap), Full); // TODO: [0xa, 0x7ff << 0x5 + 1) |
| 409 | EXPECT_EQ(Wrap.shl(Wrap), Full); |
| 410 | } |
| 411 | |
| 412 | TEST_F(ConstantRangeTest, Lshr) { |
| 413 | EXPECT_EQ(Full.lshr(Full), Full); |
| 414 | EXPECT_EQ(Full.lshr(Empty), Empty); |
| 415 | EXPECT_EQ(Full.lshr(One), ConstantRange(APInt(16, 0), |
| 416 | APInt(16, (0xffff >> 0xa) + 1))); |
| 417 | EXPECT_EQ(Full.lshr(Some), ConstantRange(APInt(16, 0), |
| 418 | APInt(16, (0xffff >> 0xa) + 1))); |
| 419 | EXPECT_EQ(Full.lshr(Wrap), Full); |
| 420 | EXPECT_EQ(Empty.lshr(Empty), Empty); |
| 421 | EXPECT_EQ(Empty.lshr(One), Empty); |
| 422 | EXPECT_EQ(Empty.lshr(Some), Empty); |
| 423 | EXPECT_EQ(Empty.lshr(Wrap), Empty); |
| 424 | EXPECT_EQ(One.lshr(One), ConstantRange(APInt(16, 0))); |
| 425 | EXPECT_EQ(One.lshr(Some), ConstantRange(APInt(16, 0))); |
| 426 | EXPECT_EQ(One.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb))); |
| 427 | EXPECT_EQ(Some.lshr(Some), ConstantRange(APInt(16, 0), |
| 428 | APInt(16, (0xaaa >> 0xa) + 1))); |
| 429 | EXPECT_EQ(Some.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa))); |
| 430 | EXPECT_EQ(Wrap.lshr(Wrap), Full); |
| 431 | } |
| 432 | |
Nick Lewycky | f2d7b7c | 2010-09-28 18:18:36 +0000 | [diff] [blame] | 433 | TEST(ConstantRange, MakeICmpRegion) { |
| 434 | // PR8250 |
| 435 | ConstantRange SMax = ConstantRange(APInt::getSignedMaxValue(32)); |
| 436 | EXPECT_TRUE(ConstantRange::makeICmpRegion(ICmpInst::ICMP_SGT, |
| 437 | SMax).isEmptySet()); |
| 438 | } |
| 439 | |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 440 | } // anonymous namespace |