ConstantRangeTest.cpp revision 8211becc98131d3ce1813cd97413240bc0afad00
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" 1132e1eef631fb275c9db580d80116d1110c3ae5eeChris Lattner#include "llvm/Support/raw_os_ostream.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()); 37a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Full.isWrappedSet()); 38a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(Full.contains(APInt(16, 0x0))); 39a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(Full.contains(APInt(16, 0x9))); 40a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(Full.contains(APInt(16, 0xa))); 41a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(Full.contains(APInt(16, 0xaa9))); 42a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(Full.contains(APInt(16, 0xaaa))); 43a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 44a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Empty.isFullSet()); 45a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(Empty.isEmptySet()); 46a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Empty.isWrappedSet()); 47a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Empty.contains(APInt(16, 0x0))); 48a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Empty.contains(APInt(16, 0x9))); 49a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Empty.contains(APInt(16, 0xa))); 50a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Empty.contains(APInt(16, 0xaa9))); 51a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Empty.contains(APInt(16, 0xaaa))); 52a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 53a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(One.isFullSet()); 54a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(One.isEmptySet()); 55a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(One.isWrappedSet()); 56a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(One.contains(APInt(16, 0x0))); 57a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(One.contains(APInt(16, 0x9))); 58a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(One.contains(APInt(16, 0xa))); 59a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(One.contains(APInt(16, 0xaa9))); 60a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(One.contains(APInt(16, 0xaaa))); 61a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 62a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Some.isFullSet()); 63a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Some.isEmptySet()); 64a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Some.isWrappedSet()); 65a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Some.contains(APInt(16, 0x0))); 66a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Some.contains(APInt(16, 0x9))); 67a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(Some.contains(APInt(16, 0xa))); 68a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(Some.contains(APInt(16, 0xaa9))); 69a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Some.contains(APInt(16, 0xaaa))); 70a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 71a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Wrap.isFullSet()); 72a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Wrap.isEmptySet()); 73a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(Wrap.isWrappedSet()); 74a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(Wrap.contains(APInt(16, 0x0))); 75a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(Wrap.contains(APInt(16, 0x9))); 76a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Wrap.contains(APInt(16, 0xa))); 77a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Wrap.contains(APInt(16, 0xaa9))); 78a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(Wrap.contains(APInt(16, 0xaaa))); 7944b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 80a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 8144b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, Equality) { 828211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full, Full); 838211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty, Empty); 848211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One, One); 858211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some, Some); 868211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Wrap, Wrap); 878211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_NE(Full, Empty); 888211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_NE(Full, One); 898211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_NE(Full, Some); 908211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_NE(Full, Wrap); 918211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_NE(Empty, One); 928211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_NE(Empty, Some); 938211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_NE(Empty, Wrap); 948211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_NE(One, Some); 958211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_NE(One, Wrap); 968211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_NE(Some, Wrap); 9744b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 98a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 9944b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, SingleElement) { 100a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Full.getSingleElement(), static_cast<APInt *>(NULL)); 101a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Empty.getSingleElement(), static_cast<APInt *>(NULL)); 102a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(*One.getSingleElement(), APInt(16, 0xa)); 103a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Some.getSingleElement(), static_cast<APInt *>(NULL)); 104a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Wrap.getSingleElement(), static_cast<APInt *>(NULL)); 105a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 106a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Full.isSingleElement()); 107a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Empty.isSingleElement()); 108a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(One.isSingleElement()); 109a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Some.isSingleElement()); 110a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_FALSE(Wrap.isSingleElement()); 11144b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 112a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 11344b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, GetSetSize) { 114a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Full.getSetSize(), APInt(16, 0)); 115a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Empty.getSetSize(), APInt(16, 0)); 116a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(One.getSetSize(), APInt(16, 1)); 117a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Some.getSetSize(), APInt(16, 0xaa0)); 118a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Wrap.getSetSize(), APInt(16, 0x10000 - 0xaa0)); 11944b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 120a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 12144b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, GetMinsAndMaxes) { 122a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Full.getUnsignedMax(), APInt(16, UINT16_MAX)); 123a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(One.getUnsignedMax(), APInt(16, 0xa)); 124a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Some.getUnsignedMax(), APInt(16, 0xaa9)); 125a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Wrap.getUnsignedMax(), APInt(16, UINT16_MAX)); 126a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 127a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Full.getUnsignedMin(), APInt(16, 0)); 128a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(One.getUnsignedMin(), APInt(16, 0xa)); 129a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Some.getUnsignedMin(), APInt(16, 0xa)); 130a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Wrap.getUnsignedMin(), APInt(16, 0)); 131a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 132a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Full.getSignedMax(), APInt(16, INT16_MAX)); 133a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(One.getSignedMax(), APInt(16, 0xa)); 134a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Some.getSignedMax(), APInt(16, 0xaa9)); 135a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Wrap.getSignedMax(), APInt(16, INT16_MAX)); 136a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 137b714c08eb8342d4818732b0248710158714fadd5Ryan Flynn EXPECT_EQ(Full.getSignedMin(), APInt(16, (uint64_t)INT16_MIN)); 138a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(One.getSignedMin(), APInt(16, 0xa)); 139a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_EQ(Some.getSignedMin(), APInt(16, 0xa)); 140b714c08eb8342d4818732b0248710158714fadd5Ryan Flynn EXPECT_EQ(Wrap.getSignedMin(), APInt(16, (uint64_t)INT16_MIN)); 141780905e9f9f29419de56b47253f6db84bc659f42Nick Lewycky 142780905e9f9f29419de56b47253f6db84bc659f42Nick Lewycky // Found by Klee 143780905e9f9f29419de56b47253f6db84bc659f42Nick Lewycky EXPECT_EQ(ConstantRange(APInt(4, 7), APInt(4, 0)).getSignedMax(), 144780905e9f9f29419de56b47253f6db84bc659f42Nick Lewycky APInt(4, 7)); 14544b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 146a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 14744b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, Trunc) { 148a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange TFull = Full.truncate(10); 149a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange TEmpty = Empty.truncate(10); 150a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange TOne = One.truncate(10); 151a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange TSome = Some.truncate(10); 152a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange TWrap = Wrap.truncate(10); 153a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(TFull.isFullSet()); 154a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(TEmpty.isEmptySet()); 1558211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(TOne, ConstantRange(APInt(One.getLower()).trunc(10), 156a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman APInt(One.getUpper()).trunc(10))); 157a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(TSome.isFullSet()); 15844b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 159a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 16044b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, ZExt) { 161a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange ZFull = Full.zeroExtend(20); 162a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange ZEmpty = Empty.zeroExtend(20); 163a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange ZOne = One.zeroExtend(20); 164a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange ZSome = Some.zeroExtend(20); 165a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange ZWrap = Wrap.zeroExtend(20); 1668211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(ZFull, ConstantRange(APInt(20, 0), APInt(20, 0x10000))); 167a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(ZEmpty.isEmptySet()); 1688211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(ZOne, ConstantRange(APInt(One.getLower()).zext(20), 1698211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt(One.getUpper()).zext(20))); 1708211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(ZSome, ConstantRange(APInt(Some.getLower()).zext(20), 1718211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt(Some.getUpper()).zext(20))); 1728211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(ZWrap, ConstantRange(APInt(Wrap.getLower()).zext(20), 1738211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt(Wrap.getUpper()).zext(20))); 17444b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 175a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 17644b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, SExt) { 177a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange SFull = Full.signExtend(20); 178a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange SEmpty = Empty.signExtend(20); 179a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange SOne = One.signExtend(20); 180a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange SSome = Some.signExtend(20); 181a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman ConstantRange SWrap = Wrap.signExtend(20); 1828211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(SFull, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true), 1838211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt(20, INT16_MAX + 1, true))); 184a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman EXPECT_TRUE(SEmpty.isEmptySet()); 1858211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(SOne, ConstantRange(APInt(One.getLower()).sext(20), 1868211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt(One.getUpper()).sext(20))); 1878211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(SSome, ConstantRange(APInt(Some.getLower()).sext(20), 1888211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt(Some.getUpper()).sext(20))); 1898211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(SWrap, ConstantRange(APInt(Wrap.getLower()).sext(20), 1908211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt(Wrap.getUpper()).sext(20))); 19144b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 19244b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky 19344b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, IntersectWith) { 1948211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.intersectWith(Full), Empty); 1958211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.intersectWith(Empty), Empty); 1968211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.intersectWith(One), Empty); 1978211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.intersectWith(Some), Empty); 1988211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.intersectWith(Wrap), Empty); 1998211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.intersectWith(Full), Full); 2008211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.intersectWith(Some), Some); 2018211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.intersectWith(One), One); 2028211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.intersectWith(One), One); 2038211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.intersectWith(Some), Some); 2048211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.intersectWith(Wrap), Empty); 2058211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One.intersectWith(Wrap), Empty); 2068211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One.intersectWith(Wrap), Wrap.intersectWith(One)); 207a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 2083a4a884c1618d94202ee714ea5c899cd80d1c536Nick Lewycky // Klee generated testcase from PR4545. 2093a4a884c1618d94202ee714ea5c899cd80d1c536Nick Lewycky // The intersection of i16 [4, 2) and [6, 5) is disjoint, looking like 2103a4a884c1618d94202ee714ea5c899cd80d1c536Nick Lewycky // 01..4.6789ABCDEF where the dots represent values not in the intersection. 2113a4a884c1618d94202ee714ea5c899cd80d1c536Nick Lewycky ConstantRange LHS(APInt(16, 4), APInt(16, 2)); 2123a4a884c1618d94202ee714ea5c899cd80d1c536Nick Lewycky ConstantRange RHS(APInt(16, 6), APInt(16, 5)); 2138142ce568d40545582e2e43a9e0aa57cd67d6da2Chris Lattner EXPECT_TRUE(LHS.intersectWith(RHS) == LHS); 21444b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 215a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 21644b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, UnionWith) { 2178211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Wrap.unionWith(One), 2188211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb))); 2198211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One.unionWith(Wrap), Wrap.unionWith(One)); 2208211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.unionWith(Empty), Empty); 2218211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.unionWith(Full), Full); 2228211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.unionWith(Wrap), Full); 2237e7dc45eb15d3739564f2a63ab1c468c57d94ff8Nick Lewycky 2247e7dc45eb15d3739564f2a63ab1c468c57d94ff8Nick Lewycky // PR4545 2258211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(ConstantRange(APInt(16, 14), APInt(16, 1)).unionWith( 2268211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky ConstantRange(APInt(16, 0), APInt(16, 8))), 2278211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky ConstantRange(APInt(16, 14), APInt(16, 8))); 2288211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(ConstantRange(APInt(16, 6), APInt(16, 4)).unionWith( 2298211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky ConstantRange(APInt(16, 4), APInt(16, 0))), 2308142ce568d40545582e2e43a9e0aa57cd67d6da2Chris Lattner ConstantRange(16)); 2318211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 0)).unionWith( 2328211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky ConstantRange(APInt(16, 2), APInt(16, 1))), 2338142ce568d40545582e2e43a9e0aa57cd67d6da2Chris Lattner ConstantRange(16)); 23444b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 235a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 23644b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, SubtractAPInt) { 2378211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.subtract(APInt(16, 4)), Full); 2388211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty); 2398211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.subtract(APInt(16, 4)), 2408211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6))); 2418211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Wrap.subtract(APInt(16, 4)), 2428211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6))); 2438211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One.subtract(APInt(16, 4)), 2448211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky ConstantRange(APInt(16, 0x6))); 24544b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 246a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 24744b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, Add) { 2488211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.add(APInt(16, 4)), Full); 2498211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.add(Full), Full); 2508211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.add(Empty), Empty); 2518211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.add(One), Full); 2528211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.add(Some), Full); 2538211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.add(Wrap), Full); 2548211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.add(Empty), Empty); 2558211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.add(One), Empty); 2568211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.add(Some), Empty); 2578211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.add(Wrap), Empty); 2588211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.add(APInt(16, 4)), Empty); 2598211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.add(APInt(16, 4)), 2608142ce568d40545582e2e43a9e0aa57cd67d6da2Chris Lattner ConstantRange(APInt(16, 0xe), APInt(16, 0xaae))); 2618211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Wrap.add(APInt(16, 4)), 2628142ce568d40545582e2e43a9e0aa57cd67d6da2Chris Lattner ConstantRange(APInt(16, 0xaae), APInt(16, 0xe))); 2638211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One.add(APInt(16, 4)), 2648142ce568d40545582e2e43a9e0aa57cd67d6da2Chris Lattner ConstantRange(APInt(16, 0xe))); 26544b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 266a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 26744b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, Multiply) { 2688211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.multiply(Full), Full); 2698211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.multiply(Empty), Empty); 2708211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.multiply(One), Full); 2718211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.multiply(Some), Full); 2728211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.multiply(Wrap), Full); 2738211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.multiply(Empty), Empty); 2748211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.multiply(One), Empty); 2758211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.multiply(Some), Empty); 2768211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.multiply(Wrap), Empty); 2778211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa), 2788211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt(16, 0xa*0xa + 1))); 2798211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa), 2808211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt(16, 0xa*0xaa9 + 1))); 2818211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One.multiply(Wrap), Full); 2828211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.multiply(Some), Full); 2838211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.multiply(Wrap), Full); 2848211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Wrap.multiply(Wrap), Full); 285f1db120d0494ec55d9265cea7dab22e80dcae10cNick Lewycky 286f1db120d0494ec55d9265cea7dab22e80dcae10cNick Lewycky // http://llvm.org/PR4545 2878211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply( 2888211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky ConstantRange(APInt(4, 6), APInt(4, 2))), 2898211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky ConstantRange(4, /*isFullSet=*/true)); 29044b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 29144b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky 29244b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, UMax) { 2938211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.umax(Full), Full); 2948211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.umax(Empty), Empty); 2958211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0))); 2968211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.umax(Wrap), Full); 2978211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0))); 2988211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.umax(Empty), Empty); 2998211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.umax(Some), Empty); 3008211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.umax(Wrap), Empty); 3018211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.umax(One), Empty); 3028211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.umax(Some), Some); 3038211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.umax(Wrap), ConstantRange(APInt(16, 0xa), APInt(16, 0))); 3048211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.umax(One), Some); 30544b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky // TODO: ConstantRange is currently over-conservative here. 3068211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Wrap.umax(Wrap), Full); 3078211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Wrap.umax(One), ConstantRange(APInt(16, 0xa), APInt(16, 0))); 3088211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One.umax(One), One); 30944b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 310a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 31144b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, SMax) { 3128211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.smax(Full), Full); 3138211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.smax(Empty), Empty); 3148211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.smax(Some), ConstantRange(APInt(16, 0xa), 3158211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt::getSignedMinValue(16))); 3168211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.smax(Wrap), Full); 3178211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.smax(One), ConstantRange(APInt(16, 0xa), 3188211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt::getSignedMinValue(16))); 3198211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.smax(Empty), Empty); 3208211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.smax(Some), Empty); 3218211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.smax(Wrap), Empty); 3228211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.smax(One), Empty); 3238211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.smax(Some), Some); 3248211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa), 3258211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt(16, (uint64_t)INT16_MIN))); 3268211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.smax(One), Some); 3278211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa), 3288211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt(16, (uint64_t)INT16_MIN))); 3298211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One.smax(One), One); 33044b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick Lewycky} 331a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 33244b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5Nick LewyckyTEST_F(ConstantRangeTest, UDiv) { 3338211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.udiv(Full), Full); 3348211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.udiv(Empty), Empty); 3358211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.udiv(One), ConstantRange(APInt(16, 0), 3368211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt(16, 0xffff / 0xa + 1))); 3378211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.udiv(Some), ConstantRange(APInt(16, 0), 3388211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky APInt(16, 0xffff / 0xa + 1))); 3398211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Full.udiv(Wrap), Full); 3408211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.udiv(Empty), Empty); 3418211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.udiv(One), Empty); 3428211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.udiv(Some), Empty); 3438211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Empty.udiv(Wrap), Empty); 3448211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One.udiv(One), ConstantRange(APInt(16, 1))); 3458211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 2))); 3468211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(One.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb))); 3478211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 0x111))); 3488211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Some.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa))); 3498211becc98131d3ce1813cd97413240bc0afad00Nick Lewycky EXPECT_EQ(Wrap.udiv(Wrap), Full); 350a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman} 351a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman 352a3755d8d366fdd7a2415b93ac0253f8e677c9dfdDan Gohman} // anonymous namespace 353