Home | History | Annotate | Download | only in IR
      1 //===- 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/IR/ConstantRange.h"
     11 #include "llvm/IR/Instructions.h"
     12 #include "llvm/IR/Operator.h"
     13 #include "gtest/gtest.h"
     14 
     15 using namespace llvm;
     16 
     17 namespace {
     18 
     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 };
     27 
     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) {
     35   EXPECT_TRUE(Full.isFullSet());
     36   EXPECT_FALSE(Full.isEmptySet());
     37   EXPECT_TRUE(Full.inverse().isEmptySet());
     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());
     47   EXPECT_TRUE(Empty.inverse().isFullSet());
     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)));
     63   EXPECT_FALSE(One.inverse().contains(APInt(16, 0xa)));
     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)));
     82 }
     83 
     84 TEST_F(ConstantRangeTest, Equality) {
     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);
    100 }
    101 
    102 TEST_F(ConstantRangeTest, SingleElement) {
    103   EXPECT_EQ(Full.getSingleElement(), static_cast<APInt *>(nullptr));
    104   EXPECT_EQ(Empty.getSingleElement(), static_cast<APInt *>(nullptr));
    105   EXPECT_EQ(*One.getSingleElement(), APInt(16, 0xa));
    106   EXPECT_EQ(Some.getSingleElement(), static_cast<APInt *>(nullptr));
    107   EXPECT_EQ(Wrap.getSingleElement(), static_cast<APInt *>(nullptr));
    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());
    114 }
    115 
    116 TEST_F(ConstantRangeTest, GetSetSize) {
    117   EXPECT_EQ(Full.getSetSize(), APInt(17, 65536));
    118   EXPECT_EQ(Empty.getSetSize(), APInt(17, 0));
    119   EXPECT_EQ(One.getSetSize(), APInt(17, 1));
    120   EXPECT_EQ(Some.getSetSize(), APInt(17, 0xaa0));
    121 
    122   ConstantRange Wrap(APInt(4, 7), APInt(4, 3));
    123   ConstantRange Wrap2(APInt(4, 8), APInt(4, 7));
    124   EXPECT_EQ(Wrap.getSetSize(), APInt(5, 12));
    125   EXPECT_EQ(Wrap2.getSetSize(), APInt(5, 15));
    126 }
    127 
    128 TEST_F(ConstantRangeTest, GetMinsAndMaxes) {
    129   EXPECT_EQ(Full.getUnsignedMax(), APInt(16, UINT16_MAX));
    130   EXPECT_EQ(One.getUnsignedMax(), APInt(16, 0xa));
    131   EXPECT_EQ(Some.getUnsignedMax(), APInt(16, 0xaa9));
    132   EXPECT_EQ(Wrap.getUnsignedMax(), APInt(16, UINT16_MAX));
    133 
    134   EXPECT_EQ(Full.getUnsignedMin(), APInt(16, 0));
    135   EXPECT_EQ(One.getUnsignedMin(), APInt(16, 0xa));
    136   EXPECT_EQ(Some.getUnsignedMin(), APInt(16, 0xa));
    137   EXPECT_EQ(Wrap.getUnsignedMin(), APInt(16, 0));
    138 
    139   EXPECT_EQ(Full.getSignedMax(), APInt(16, INT16_MAX));
    140   EXPECT_EQ(One.getSignedMax(), APInt(16, 0xa));
    141   EXPECT_EQ(Some.getSignedMax(), APInt(16, 0xaa9));
    142   EXPECT_EQ(Wrap.getSignedMax(), APInt(16, INT16_MAX));
    143 
    144   EXPECT_EQ(Full.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
    145   EXPECT_EQ(One.getSignedMin(), APInt(16, 0xa));
    146   EXPECT_EQ(Some.getSignedMin(), APInt(16, 0xa));
    147   EXPECT_EQ(Wrap.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
    148 
    149   // Found by Klee
    150   EXPECT_EQ(ConstantRange(APInt(4, 7), APInt(4, 0)).getSignedMax(),
    151             APInt(4, 7));
    152 }
    153 
    154 TEST_F(ConstantRangeTest, SignWrapped) {
    155   EXPECT_TRUE(Full.isSignWrappedSet());
    156   EXPECT_FALSE(Empty.isSignWrappedSet());
    157   EXPECT_FALSE(One.isSignWrappedSet());
    158   EXPECT_FALSE(Some.isSignWrappedSet());
    159   EXPECT_TRUE(Wrap.isSignWrappedSet());
    160 
    161   EXPECT_FALSE(ConstantRange(APInt(8, 127), APInt(8, 128)).isSignWrappedSet());
    162   EXPECT_TRUE(ConstantRange(APInt(8, 127), APInt(8, 129)).isSignWrappedSet());
    163   EXPECT_FALSE(ConstantRange(APInt(8, 128), APInt(8, 129)).isSignWrappedSet());
    164   EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 9)).isSignWrappedSet());
    165   EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 250)).isSignWrappedSet());
    166   EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 10)).isSignWrappedSet());
    167   EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 251)).isSignWrappedSet());
    168 }
    169 
    170 TEST_F(ConstantRangeTest, Trunc) {
    171   ConstantRange TFull = Full.truncate(10);
    172   ConstantRange TEmpty = Empty.truncate(10);
    173   ConstantRange TOne = One.truncate(10);
    174   ConstantRange TSome = Some.truncate(10);
    175   ConstantRange TWrap = Wrap.truncate(10);
    176   EXPECT_TRUE(TFull.isFullSet());
    177   EXPECT_TRUE(TEmpty.isEmptySet());
    178   EXPECT_EQ(TOne, ConstantRange(One.getLower().trunc(10),
    179                                 One.getUpper().trunc(10)));
    180   EXPECT_TRUE(TSome.isFullSet());
    181 }
    182 
    183 TEST_F(ConstantRangeTest, ZExt) {
    184   ConstantRange ZFull = Full.zeroExtend(20);
    185   ConstantRange ZEmpty = Empty.zeroExtend(20);
    186   ConstantRange ZOne = One.zeroExtend(20);
    187   ConstantRange ZSome = Some.zeroExtend(20);
    188   ConstantRange ZWrap = Wrap.zeroExtend(20);
    189   EXPECT_EQ(ZFull, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
    190   EXPECT_TRUE(ZEmpty.isEmptySet());
    191   EXPECT_EQ(ZOne, ConstantRange(One.getLower().zext(20),
    192                                 One.getUpper().zext(20)));
    193   EXPECT_EQ(ZSome, ConstantRange(Some.getLower().zext(20),
    194                                  Some.getUpper().zext(20)));
    195   EXPECT_EQ(ZWrap, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
    196 
    197   // zext([5, 0), 3->7) = [5, 8)
    198   ConstantRange FiveZero(APInt(3, 5), APInt(3, 0));
    199   EXPECT_EQ(FiveZero.zeroExtend(7), ConstantRange(APInt(7, 5), APInt(7, 8)));
    200 }
    201 
    202 TEST_F(ConstantRangeTest, SExt) {
    203   ConstantRange SFull = Full.signExtend(20);
    204   ConstantRange SEmpty = Empty.signExtend(20);
    205   ConstantRange SOne = One.signExtend(20);
    206   ConstantRange SSome = Some.signExtend(20);
    207   ConstantRange SWrap = Wrap.signExtend(20);
    208   EXPECT_EQ(SFull, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
    209                                  APInt(20, INT16_MAX + 1, true)));
    210   EXPECT_TRUE(SEmpty.isEmptySet());
    211   EXPECT_EQ(SOne, ConstantRange(One.getLower().sext(20),
    212                                 One.getUpper().sext(20)));
    213   EXPECT_EQ(SSome, ConstantRange(Some.getLower().sext(20),
    214                                  Some.getUpper().sext(20)));
    215   EXPECT_EQ(SWrap, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
    216                                  APInt(20, INT16_MAX + 1, true)));
    217 
    218   EXPECT_EQ(ConstantRange(APInt(8, 120), APInt(8, 140)).signExtend(16),
    219             ConstantRange(APInt(16, -128), APInt(16, 128)));
    220 
    221   EXPECT_EQ(ConstantRange(APInt(16, 0x0200), APInt(16, 0x8000)).signExtend(19),
    222             ConstantRange(APInt(19, 0x0200), APInt(19, 0x8000)));
    223 }
    224 
    225 TEST_F(ConstantRangeTest, IntersectWith) {
    226   EXPECT_EQ(Empty.intersectWith(Full), Empty);
    227   EXPECT_EQ(Empty.intersectWith(Empty), Empty);
    228   EXPECT_EQ(Empty.intersectWith(One), Empty);
    229   EXPECT_EQ(Empty.intersectWith(Some), Empty);
    230   EXPECT_EQ(Empty.intersectWith(Wrap), Empty);
    231   EXPECT_EQ(Full.intersectWith(Full), Full);
    232   EXPECT_EQ(Some.intersectWith(Some), Some);
    233   EXPECT_EQ(Some.intersectWith(One), One);
    234   EXPECT_EQ(Full.intersectWith(One), One);
    235   EXPECT_EQ(Full.intersectWith(Some), Some);
    236   EXPECT_EQ(Some.intersectWith(Wrap), Empty);
    237   EXPECT_EQ(One.intersectWith(Wrap), Empty);
    238   EXPECT_EQ(One.intersectWith(Wrap), Wrap.intersectWith(One));
    239 
    240   // Klee generated testcase from PR4545.
    241   // The intersection of i16 [4, 2) and [6, 5) is disjoint, looking like
    242   // 01..4.6789ABCDEF where the dots represent values not in the intersection.
    243   ConstantRange LHS(APInt(16, 4), APInt(16, 2));
    244   ConstantRange RHS(APInt(16, 6), APInt(16, 5));
    245   EXPECT_TRUE(LHS.intersectWith(RHS) == LHS);
    246 
    247   // previous bug: intersection of [min, 3) and [2, max) should be 2
    248   LHS = ConstantRange(APInt(32, -2147483646), APInt(32, 3));
    249   RHS = ConstantRange(APInt(32, 2), APInt(32, 2147483646));
    250   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2)));
    251 
    252   // [2, 0) /\ [4, 3) = [2, 0)
    253   LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
    254   RHS = ConstantRange(APInt(32, 4), APInt(32, 3));
    255   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2), APInt(32, 0)));
    256 
    257   // [2, 0) /\ [4, 2) = [4, 0)
    258   LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
    259   RHS = ConstantRange(APInt(32, 4), APInt(32, 2));
    260   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 0)));
    261 
    262   // [4, 2) /\ [5, 1) = [5, 1)
    263   LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
    264   RHS = ConstantRange(APInt(32, 5), APInt(32, 1));
    265   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 5), APInt(32, 1)));
    266 
    267   // [2, 0) /\ [7, 4) = [7, 4)
    268   LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
    269   RHS = ConstantRange(APInt(32, 7), APInt(32, 4));
    270   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 7), APInt(32, 4)));
    271 
    272   // [4, 2) /\ [1, 0) = [1, 0)
    273   LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
    274   RHS = ConstantRange(APInt(32, 1), APInt(32, 0));
    275   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 2)));
    276 
    277   // [15, 0) /\ [7, 6) = [15, 0)
    278   LHS = ConstantRange(APInt(32, 15), APInt(32, 0));
    279   RHS = ConstantRange(APInt(32, 7), APInt(32, 6));
    280   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 15), APInt(32, 0)));
    281 }
    282 
    283 TEST_F(ConstantRangeTest, UnionWith) {
    284   EXPECT_EQ(Wrap.unionWith(One),
    285             ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb)));
    286   EXPECT_EQ(One.unionWith(Wrap), Wrap.unionWith(One));
    287   EXPECT_EQ(Empty.unionWith(Empty), Empty);
    288   EXPECT_EQ(Full.unionWith(Full), Full);
    289   EXPECT_EQ(Some.unionWith(Wrap), Full);
    290 
    291   // PR4545
    292   EXPECT_EQ(ConstantRange(APInt(16, 14), APInt(16, 1)).unionWith(
    293                                     ConstantRange(APInt(16, 0), APInt(16, 8))),
    294             ConstantRange(APInt(16, 14), APInt(16, 8)));
    295   EXPECT_EQ(ConstantRange(APInt(16, 6), APInt(16, 4)).unionWith(
    296                                     ConstantRange(APInt(16, 4), APInt(16, 0))),
    297               ConstantRange(16));
    298   EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 0)).unionWith(
    299                                     ConstantRange(APInt(16, 2), APInt(16, 1))),
    300               ConstantRange(16));
    301 }
    302 
    303 TEST_F(ConstantRangeTest, SetDifference) {
    304   EXPECT_EQ(Full.difference(Empty), Full);
    305   EXPECT_EQ(Full.difference(Full), Empty);
    306   EXPECT_EQ(Empty.difference(Empty), Empty);
    307   EXPECT_EQ(Empty.difference(Full), Empty);
    308 
    309   ConstantRange A(APInt(16, 3), APInt(16, 7));
    310   ConstantRange B(APInt(16, 5), APInt(16, 9));
    311   ConstantRange C(APInt(16, 3), APInt(16, 5));
    312   ConstantRange D(APInt(16, 7), APInt(16, 9));
    313   ConstantRange E(APInt(16, 5), APInt(16, 4));
    314   ConstantRange F(APInt(16, 7), APInt(16, 3));
    315   EXPECT_EQ(A.difference(B), C);
    316   EXPECT_EQ(B.difference(A), D);
    317   EXPECT_EQ(E.difference(A), F);
    318 }
    319 
    320 TEST_F(ConstantRangeTest, SubtractAPInt) {
    321   EXPECT_EQ(Full.subtract(APInt(16, 4)), Full);
    322   EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty);
    323   EXPECT_EQ(Some.subtract(APInt(16, 4)),
    324             ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
    325   EXPECT_EQ(Wrap.subtract(APInt(16, 4)),
    326             ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
    327   EXPECT_EQ(One.subtract(APInt(16, 4)),
    328             ConstantRange(APInt(16, 0x6)));
    329 }
    330 
    331 TEST_F(ConstantRangeTest, Add) {
    332   EXPECT_EQ(Full.add(APInt(16, 4)), Full);
    333   EXPECT_EQ(Full.add(Full), Full);
    334   EXPECT_EQ(Full.add(Empty), Empty);
    335   EXPECT_EQ(Full.add(One), Full);
    336   EXPECT_EQ(Full.add(Some), Full);
    337   EXPECT_EQ(Full.add(Wrap), Full);
    338   EXPECT_EQ(Empty.add(Empty), Empty);
    339   EXPECT_EQ(Empty.add(One), Empty);
    340   EXPECT_EQ(Empty.add(Some), Empty);
    341   EXPECT_EQ(Empty.add(Wrap), Empty);
    342   EXPECT_EQ(Empty.add(APInt(16, 4)), Empty);
    343   EXPECT_EQ(Some.add(APInt(16, 4)),
    344             ConstantRange(APInt(16, 0xe), APInt(16, 0xaae)));
    345   EXPECT_EQ(Wrap.add(APInt(16, 4)),
    346             ConstantRange(APInt(16, 0xaae), APInt(16, 0xe)));
    347   EXPECT_EQ(One.add(APInt(16, 4)),
    348             ConstantRange(APInt(16, 0xe)));
    349 }
    350 
    351 TEST_F(ConstantRangeTest, Sub) {
    352   EXPECT_EQ(Full.sub(APInt(16, 4)), Full);
    353   EXPECT_EQ(Full.sub(Full), Full);
    354   EXPECT_EQ(Full.sub(Empty), Empty);
    355   EXPECT_EQ(Full.sub(One), Full);
    356   EXPECT_EQ(Full.sub(Some), Full);
    357   EXPECT_EQ(Full.sub(Wrap), Full);
    358   EXPECT_EQ(Empty.sub(Empty), Empty);
    359   EXPECT_EQ(Empty.sub(One), Empty);
    360   EXPECT_EQ(Empty.sub(Some), Empty);
    361   EXPECT_EQ(Empty.sub(Wrap), Empty);
    362   EXPECT_EQ(Empty.sub(APInt(16, 4)), Empty);
    363   EXPECT_EQ(Some.sub(APInt(16, 4)),
    364             ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
    365   EXPECT_EQ(Some.sub(Some),
    366             ConstantRange(APInt(16, 0xf561), APInt(16, 0xaa0)));
    367   EXPECT_EQ(Wrap.sub(APInt(16, 4)),
    368             ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
    369   EXPECT_EQ(One.sub(APInt(16, 4)),
    370             ConstantRange(APInt(16, 0x6)));
    371 }
    372 
    373 TEST_F(ConstantRangeTest, Multiply) {
    374   EXPECT_EQ(Full.multiply(Full), Full);
    375   EXPECT_EQ(Full.multiply(Empty), Empty);
    376   EXPECT_EQ(Full.multiply(One), Full);
    377   EXPECT_EQ(Full.multiply(Some), Full);
    378   EXPECT_EQ(Full.multiply(Wrap), Full);
    379   EXPECT_EQ(Empty.multiply(Empty), Empty);
    380   EXPECT_EQ(Empty.multiply(One), Empty);
    381   EXPECT_EQ(Empty.multiply(Some), Empty);
    382   EXPECT_EQ(Empty.multiply(Wrap), Empty);
    383   EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa),
    384                                              APInt(16, 0xa*0xa + 1)));
    385   EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa),
    386                                               APInt(16, 0xa*0xaa9 + 1)));
    387   EXPECT_EQ(One.multiply(Wrap), Full);
    388   EXPECT_EQ(Some.multiply(Some), Full);
    389   EXPECT_EQ(Some.multiply(Wrap), Full);
    390   EXPECT_EQ(Wrap.multiply(Wrap), Full);
    391 
    392   ConstantRange Zero(APInt(16, 0));
    393   EXPECT_EQ(Zero.multiply(Full), Zero);
    394   EXPECT_EQ(Zero.multiply(Some), Zero);
    395   EXPECT_EQ(Zero.multiply(Wrap), Zero);
    396   EXPECT_EQ(Full.multiply(Zero), Zero);
    397   EXPECT_EQ(Some.multiply(Zero), Zero);
    398   EXPECT_EQ(Wrap.multiply(Zero), Zero);
    399 
    400   // http://llvm.org/PR4545
    401   EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply(
    402                 ConstantRange(APInt(4, 6), APInt(4, 2))),
    403             ConstantRange(4, /*isFullSet=*/true));
    404 
    405   EXPECT_EQ(ConstantRange(APInt(8, 254), APInt(8, 0)).multiply(
    406               ConstantRange(APInt(8, 252), APInt(8, 4))),
    407             ConstantRange(APInt(8, 250), APInt(8, 9)));
    408   EXPECT_EQ(ConstantRange(APInt(8, 254), APInt(8, 255)).multiply(
    409               ConstantRange(APInt(8, 2), APInt(8, 4))),
    410             ConstantRange(APInt(8, 250), APInt(8, 253)));
    411 }
    412 
    413 TEST_F(ConstantRangeTest, UMax) {
    414   EXPECT_EQ(Full.umax(Full), Full);
    415   EXPECT_EQ(Full.umax(Empty), Empty);
    416   EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
    417   EXPECT_EQ(Full.umax(Wrap), Full);
    418   EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
    419   EXPECT_EQ(Empty.umax(Empty), Empty);
    420   EXPECT_EQ(Empty.umax(Some), Empty);
    421   EXPECT_EQ(Empty.umax(Wrap), Empty);
    422   EXPECT_EQ(Empty.umax(One), Empty);
    423   EXPECT_EQ(Some.umax(Some), Some);
    424   EXPECT_EQ(Some.umax(Wrap), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
    425   EXPECT_EQ(Some.umax(One), Some);
    426   // TODO: ConstantRange is currently over-conservative here.
    427   EXPECT_EQ(Wrap.umax(Wrap), Full);
    428   EXPECT_EQ(Wrap.umax(One), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
    429   EXPECT_EQ(One.umax(One), One);
    430 }
    431 
    432 TEST_F(ConstantRangeTest, SMax) {
    433   EXPECT_EQ(Full.smax(Full), Full);
    434   EXPECT_EQ(Full.smax(Empty), Empty);
    435   EXPECT_EQ(Full.smax(Some), ConstantRange(APInt(16, 0xa),
    436                                            APInt::getSignedMinValue(16)));
    437   EXPECT_EQ(Full.smax(Wrap), Full);
    438   EXPECT_EQ(Full.smax(One), ConstantRange(APInt(16, 0xa),
    439                                           APInt::getSignedMinValue(16)));
    440   EXPECT_EQ(Empty.smax(Empty), Empty);
    441   EXPECT_EQ(Empty.smax(Some), Empty);
    442   EXPECT_EQ(Empty.smax(Wrap), Empty);
    443   EXPECT_EQ(Empty.smax(One), Empty);
    444   EXPECT_EQ(Some.smax(Some), Some);
    445   EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa),
    446                                            APInt(16, (uint64_t)INT16_MIN)));
    447   EXPECT_EQ(Some.smax(One), Some);
    448   EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa),
    449                                           APInt(16, (uint64_t)INT16_MIN)));
    450   EXPECT_EQ(One.smax(One), One);
    451 }
    452 
    453 TEST_F(ConstantRangeTest, UMin) {
    454   EXPECT_EQ(Full.umin(Full), Full);
    455   EXPECT_EQ(Full.umin(Empty), Empty);
    456   EXPECT_EQ(Full.umin(Some), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
    457   EXPECT_EQ(Full.umin(Wrap), Full);
    458   EXPECT_EQ(Empty.umin(Empty), Empty);
    459   EXPECT_EQ(Empty.umin(Some), Empty);
    460   EXPECT_EQ(Empty.umin(Wrap), Empty);
    461   EXPECT_EQ(Empty.umin(One), Empty);
    462   EXPECT_EQ(Some.umin(Some), Some);
    463   EXPECT_EQ(Some.umin(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
    464   EXPECT_EQ(Some.umin(One), One);
    465   // TODO: ConstantRange is currently over-conservative here.
    466   EXPECT_EQ(Wrap.umin(Wrap), Full);
    467   EXPECT_EQ(Wrap.umin(One), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
    468   EXPECT_EQ(One.umin(One), One);
    469 }
    470 
    471 TEST_F(ConstantRangeTest, SMin) {
    472   EXPECT_EQ(Full.smin(Full), Full);
    473   EXPECT_EQ(Full.smin(Empty), Empty);
    474   EXPECT_EQ(Full.smin(Some), ConstantRange(APInt(16, (uint64_t)INT16_MIN),
    475                                            APInt(16, 0xaaa)));
    476   EXPECT_EQ(Full.smin(Wrap), Full);
    477   EXPECT_EQ(Empty.smin(Empty), Empty);
    478   EXPECT_EQ(Empty.smin(Some), Empty);
    479   EXPECT_EQ(Empty.smin(Wrap), Empty);
    480   EXPECT_EQ(Empty.smin(One), Empty);
    481   EXPECT_EQ(Some.smin(Some), Some);
    482   EXPECT_EQ(Some.smin(Wrap), ConstantRange(APInt(16, (uint64_t)INT16_MIN),
    483                                            APInt(16, 0xaaa)));
    484   EXPECT_EQ(Some.smin(One), One);
    485   // TODO: ConstantRange is currently over-conservative here.
    486   EXPECT_EQ(Wrap.smin(Wrap), Full);
    487   EXPECT_EQ(Wrap.smin(One), ConstantRange(APInt(16, (uint64_t)INT16_MIN),
    488                                           APInt(16, 0xb)));
    489   EXPECT_EQ(One.smin(One), One);
    490 }
    491 
    492 TEST_F(ConstantRangeTest, UDiv) {
    493   EXPECT_EQ(Full.udiv(Full), Full);
    494   EXPECT_EQ(Full.udiv(Empty), Empty);
    495   EXPECT_EQ(Full.udiv(One), ConstantRange(APInt(16, 0),
    496                                           APInt(16, 0xffff / 0xa + 1)));
    497   EXPECT_EQ(Full.udiv(Some), ConstantRange(APInt(16, 0),
    498                                            APInt(16, 0xffff / 0xa + 1)));
    499   EXPECT_EQ(Full.udiv(Wrap), Full);
    500   EXPECT_EQ(Empty.udiv(Empty), Empty);
    501   EXPECT_EQ(Empty.udiv(One), Empty);
    502   EXPECT_EQ(Empty.udiv(Some), Empty);
    503   EXPECT_EQ(Empty.udiv(Wrap), Empty);
    504   EXPECT_EQ(One.udiv(One), ConstantRange(APInt(16, 1)));
    505   EXPECT_EQ(One.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 2)));
    506   EXPECT_EQ(One.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
    507   EXPECT_EQ(Some.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 0x111)));
    508   EXPECT_EQ(Some.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
    509   EXPECT_EQ(Wrap.udiv(Wrap), Full);
    510 }
    511 
    512 TEST_F(ConstantRangeTest, Shl) {
    513   EXPECT_EQ(Full.shl(Full), Full);
    514   EXPECT_EQ(Full.shl(Empty), Empty);
    515   EXPECT_EQ(Full.shl(One), Full);    // TODO: [0, (-1 << 0xa) + 1)
    516   EXPECT_EQ(Full.shl(Some), Full);   // TODO: [0, (-1 << 0xa) + 1)
    517   EXPECT_EQ(Full.shl(Wrap), Full);
    518   EXPECT_EQ(Empty.shl(Empty), Empty);
    519   EXPECT_EQ(Empty.shl(One), Empty);
    520   EXPECT_EQ(Empty.shl(Some), Empty);
    521   EXPECT_EQ(Empty.shl(Wrap), Empty);
    522   EXPECT_EQ(One.shl(One), ConstantRange(APInt(16, 0xa << 0xa),
    523                                         APInt(16, (0xa << 0xa) + 1)));
    524   EXPECT_EQ(One.shl(Some), Full);    // TODO: [0xa << 0xa, 0)
    525   EXPECT_EQ(One.shl(Wrap), Full);    // TODO: [0xa, 0xa << 14 + 1)
    526   EXPECT_EQ(Some.shl(Some), Full);   // TODO: [0xa << 0xa, 0xfc01)
    527   EXPECT_EQ(Some.shl(Wrap), Full);   // TODO: [0xa, 0x7ff << 0x5 + 1)
    528   EXPECT_EQ(Wrap.shl(Wrap), Full);
    529 }
    530 
    531 TEST_F(ConstantRangeTest, Lshr) {
    532   EXPECT_EQ(Full.lshr(Full), Full);
    533   EXPECT_EQ(Full.lshr(Empty), Empty);
    534   EXPECT_EQ(Full.lshr(One), ConstantRange(APInt(16, 0),
    535                                           APInt(16, (0xffff >> 0xa) + 1)));
    536   EXPECT_EQ(Full.lshr(Some), ConstantRange(APInt(16, 0),
    537                                            APInt(16, (0xffff >> 0xa) + 1)));
    538   EXPECT_EQ(Full.lshr(Wrap), Full);
    539   EXPECT_EQ(Empty.lshr(Empty), Empty);
    540   EXPECT_EQ(Empty.lshr(One), Empty);
    541   EXPECT_EQ(Empty.lshr(Some), Empty);
    542   EXPECT_EQ(Empty.lshr(Wrap), Empty);
    543   EXPECT_EQ(One.lshr(One), ConstantRange(APInt(16, 0)));
    544   EXPECT_EQ(One.lshr(Some), ConstantRange(APInt(16, 0)));
    545   EXPECT_EQ(One.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
    546   EXPECT_EQ(Some.lshr(Some), ConstantRange(APInt(16, 0),
    547                                            APInt(16, (0xaaa >> 0xa) + 1)));
    548   EXPECT_EQ(Some.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
    549   EXPECT_EQ(Wrap.lshr(Wrap), Full);
    550 }
    551 
    552 TEST(ConstantRange, MakeAllowedICmpRegion) {
    553   // PR8250
    554   ConstantRange SMax = ConstantRange(APInt::getSignedMaxValue(32));
    555   EXPECT_TRUE(ConstantRange::makeAllowedICmpRegion(ICmpInst::ICMP_SGT, SMax)
    556                   .isEmptySet());
    557 }
    558 
    559 TEST(ConstantRange, MakeSatisfyingICmpRegion) {
    560   ConstantRange LowHalf(APInt(8, 0), APInt(8, 128));
    561   ConstantRange HighHalf(APInt(8, 128), APInt(8, 0));
    562   ConstantRange EmptySet(8, /* isFullSet = */ false);
    563 
    564   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_NE, LowHalf),
    565             HighHalf);
    566 
    567   EXPECT_EQ(
    568       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_NE, HighHalf),
    569       LowHalf);
    570 
    571   EXPECT_TRUE(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_EQ,
    572                                                       HighHalf).isEmptySet());
    573 
    574   ConstantRange UnsignedSample(APInt(8, 5), APInt(8, 200));
    575 
    576   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_ULT,
    577                                                     UnsignedSample),
    578             ConstantRange(APInt(8, 0), APInt(8, 5)));
    579 
    580   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_ULE,
    581                                                     UnsignedSample),
    582             ConstantRange(APInt(8, 0), APInt(8, 6)));
    583 
    584   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_UGT,
    585                                                     UnsignedSample),
    586             ConstantRange(APInt(8, 200), APInt(8, 0)));
    587 
    588   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_UGE,
    589                                                     UnsignedSample),
    590             ConstantRange(APInt(8, 199), APInt(8, 0)));
    591 
    592   ConstantRange SignedSample(APInt(8, -5), APInt(8, 5));
    593 
    594   EXPECT_EQ(
    595       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SLT, SignedSample),
    596       ConstantRange(APInt(8, -128), APInt(8, -5)));
    597 
    598   EXPECT_EQ(
    599       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SLE, SignedSample),
    600       ConstantRange(APInt(8, -128), APInt(8, -4)));
    601 
    602   EXPECT_EQ(
    603       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SGT, SignedSample),
    604       ConstantRange(APInt(8, 5), APInt(8, -128)));
    605 
    606   EXPECT_EQ(
    607       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SGE, SignedSample),
    608       ConstantRange(APInt(8, 4), APInt(8, -128)));
    609 }
    610 
    611 TEST(ConstantRange, MakeGuaranteedNoWrapRegion) {
    612   const int IntMin4Bits = 8;
    613   const int IntMax4Bits = 7;
    614   typedef OverflowingBinaryOperator OBO;
    615 
    616   for (int Const : {0, -1, -2, 1, 2, IntMin4Bits, IntMax4Bits}) {
    617     APInt C(4, Const, true /* = isSigned */);
    618 
    619     auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
    620         Instruction::Add, C, OBO::NoUnsignedWrap);
    621 
    622     EXPECT_FALSE(NUWRegion.isEmptySet());
    623 
    624     auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
    625         Instruction::Add, C, OBO::NoSignedWrap);
    626 
    627     EXPECT_FALSE(NSWRegion.isEmptySet());
    628 
    629     auto NoWrapRegion = ConstantRange::makeGuaranteedNoWrapRegion(
    630         Instruction::Add, C, OBO::NoSignedWrap | OBO::NoUnsignedWrap);
    631 
    632     EXPECT_FALSE(NoWrapRegion.isEmptySet());
    633     EXPECT_TRUE(NUWRegion.intersectWith(NSWRegion).contains(NoWrapRegion));
    634 
    635     for (APInt I = NUWRegion.getLower(), E = NUWRegion.getUpper(); I != E;
    636          ++I) {
    637       bool Overflow = false;
    638       I.uadd_ov(C, Overflow);
    639       EXPECT_FALSE(Overflow);
    640     }
    641 
    642     for (APInt I = NSWRegion.getLower(), E = NSWRegion.getUpper(); I != E;
    643          ++I) {
    644       bool Overflow = false;
    645       I.sadd_ov(C, Overflow);
    646       EXPECT_FALSE(Overflow);
    647     }
    648 
    649     for (APInt I = NoWrapRegion.getLower(), E = NoWrapRegion.getUpper(); I != E;
    650          ++I) {
    651       bool Overflow = false;
    652 
    653       I.sadd_ov(C, Overflow);
    654       EXPECT_FALSE(Overflow);
    655 
    656       I.uadd_ov(C, Overflow);
    657       EXPECT_FALSE(Overflow);
    658     }
    659   }
    660 
    661   auto NSWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
    662       Instruction::Add, ConstantRange(32, /* isFullSet = */ true),
    663       OBO::NoSignedWrap);
    664   EXPECT_TRUE(NSWForAllValues.isSingleElement() &&
    665               NSWForAllValues.getSingleElement()->isMinValue());
    666 
    667   auto NUWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
    668       Instruction::Add, ConstantRange(32, /* isFullSet = */ true),
    669       OBO::NoUnsignedWrap);
    670   EXPECT_TRUE(NUWForAllValues.isSingleElement() &&
    671               NSWForAllValues.getSingleElement()->isMinValue());
    672 
    673   auto NUWAndNSWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
    674       Instruction::Add, ConstantRange(32, /* isFullSet = */ true),
    675       OBO::NoUnsignedWrap | OBO::NoSignedWrap);
    676   EXPECT_TRUE(NUWAndNSWForAllValues.isSingleElement() &&
    677               NSWForAllValues.getSingleElement()->isMinValue());
    678 
    679   ConstantRange OneToFive(APInt(32, 1), APInt(32, 6));
    680   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
    681                 Instruction::Add, OneToFive, OBO::NoSignedWrap),
    682             ConstantRange(APInt::getSignedMinValue(32),
    683                           APInt::getSignedMaxValue(32) - 4));
    684   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
    685                 Instruction::Add, OneToFive, OBO::NoUnsignedWrap),
    686             ConstantRange(APInt::getMinValue(32), APInt::getMinValue(32) - 5));
    687   EXPECT_EQ(
    688       ConstantRange::makeGuaranteedNoWrapRegion(
    689           Instruction::Add, OneToFive, OBO::NoUnsignedWrap | OBO::NoSignedWrap),
    690       ConstantRange(APInt::getMinValue(32), APInt::getSignedMaxValue(32) - 4));
    691 
    692   ConstantRange MinusFiveToMinusTwo(APInt(32, -5), APInt(32, -1));
    693   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
    694                 Instruction::Add, MinusFiveToMinusTwo, OBO::NoSignedWrap),
    695             ConstantRange(APInt::getSignedMinValue(32) + 5,
    696                           APInt::getSignedMinValue(32)));
    697   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
    698                 Instruction::Add, MinusFiveToMinusTwo, OBO::NoUnsignedWrap),
    699             ConstantRange(APInt(32, 0), APInt(32, 2)));
    700   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
    701                 Instruction::Add, MinusFiveToMinusTwo,
    702                 OBO::NoUnsignedWrap | OBO::NoSignedWrap),
    703             ConstantRange(APInt(32, 0), APInt(32, 2)));
    704 
    705   ConstantRange MinusOneToOne(APInt(32, -1), APInt(32, 2));
    706   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
    707                 Instruction::Add, MinusOneToOne, OBO::NoSignedWrap),
    708             ConstantRange(APInt::getSignedMinValue(32) + 1,
    709                           APInt::getSignedMinValue(32) - 1));
    710   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
    711                 Instruction::Add, MinusOneToOne, OBO::NoUnsignedWrap),
    712             ConstantRange(APInt(32, 0), APInt(32, 1)));
    713   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
    714                 Instruction::Add, MinusOneToOne,
    715                 OBO::NoUnsignedWrap | OBO::NoSignedWrap),
    716             ConstantRange(APInt(32, 0), APInt(32, 1)));
    717 }
    718 
    719 TEST(ConstantRange, GetEquivalentICmp) {
    720   APInt RHS;
    721   CmpInst::Predicate Pred;
    722 
    723   EXPECT_TRUE(ConstantRange(APInt::getMinValue(32), APInt(32, 100))
    724                   .getEquivalentICmp(Pred, RHS));
    725   EXPECT_EQ(Pred, CmpInst::ICMP_ULT);
    726   EXPECT_EQ(RHS, APInt(32, 100));
    727 
    728   EXPECT_TRUE(ConstantRange(APInt::getSignedMinValue(32), APInt(32, 100))
    729                   .getEquivalentICmp(Pred, RHS));
    730   EXPECT_EQ(Pred, CmpInst::ICMP_SLT);
    731   EXPECT_EQ(RHS, APInt(32, 100));
    732 
    733   EXPECT_TRUE(ConstantRange(APInt(32, 100), APInt::getMinValue(32))
    734                   .getEquivalentICmp(Pred, RHS));
    735   EXPECT_EQ(Pred, CmpInst::ICMP_UGE);
    736   EXPECT_EQ(RHS, APInt(32, 100));
    737 
    738   EXPECT_TRUE(ConstantRange(APInt(32, 100), APInt::getSignedMinValue(32))
    739                   .getEquivalentICmp(Pred, RHS));
    740   EXPECT_EQ(Pred, CmpInst::ICMP_SGE);
    741   EXPECT_EQ(RHS, APInt(32, 100));
    742 
    743   EXPECT_TRUE(
    744       ConstantRange(32, /*isFullSet=*/true).getEquivalentICmp(Pred, RHS));
    745   EXPECT_EQ(Pred, CmpInst::ICMP_UGE);
    746   EXPECT_EQ(RHS, APInt(32, 0));
    747 
    748   EXPECT_TRUE(
    749       ConstantRange(32, /*isFullSet=*/false).getEquivalentICmp(Pred, RHS));
    750   EXPECT_EQ(Pred, CmpInst::ICMP_ULT);
    751   EXPECT_EQ(RHS, APInt(32, 0));
    752 
    753   EXPECT_FALSE(ConstantRange(APInt(32, 100), APInt(32, 200))
    754                    .getEquivalentICmp(Pred, RHS));
    755 
    756   EXPECT_FALSE(ConstantRange(APInt::getSignedMinValue(32) - APInt(32, 100),
    757                              APInt::getSignedMinValue(32) + APInt(32, 100))
    758                    .getEquivalentICmp(Pred, RHS));
    759 
    760   EXPECT_FALSE(ConstantRange(APInt::getMinValue(32) - APInt(32, 100),
    761                              APInt::getMinValue(32) + APInt(32, 100))
    762                    .getEquivalentICmp(Pred, RHS));
    763 }
    764 
    765 }  // anonymous namespace
    766