blob: 3e410256414458ba6dea0899fdfd325be623d09e [file] [log] [blame]
Florian Hahn8af01572017-09-28 11:09:22 +00001//===- ValueLatticeTest.cpp - ScalarEvolution unit 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/Analysis/ValueLattice.h"
11#include "llvm/ADT/SmallVector.h"
12#include "llvm/IR/ConstantRange.h"
13#include "llvm/IR/Constants.h"
14#include "llvm/IR/IRBuilder.h"
15#include "llvm/IR/LLVMContext.h"
16#include "llvm/IR/Module.h"
17#include "gtest/gtest.h"
18
19namespace llvm {
20namespace {
21
22// We use this fixture to ensure that we clean up ScalarEvolution before
23// deleting the PassManager.
24class ValueLatticeTest : public testing::Test {
25protected:
26 LLVMContext Context;
27 Module M;
28
29 ValueLatticeTest() : M("", Context) {}
30};
31
32TEST_F(ValueLatticeTest, ValueLatticeGetters) {
33 auto I32Ty = IntegerType::get(Context, 32);
34 auto *C1 = ConstantInt::get(I32Ty, 1);
35
36 EXPECT_TRUE(ValueLatticeElement::get(C1).isConstantRange());
37 EXPECT_TRUE(
38 ValueLatticeElement::getRange({C1->getValue()}).isConstantRange());
39 EXPECT_TRUE(ValueLatticeElement::getOverdefined().isOverdefined());
40
41 auto FloatTy = Type::getFloatTy(Context);
42 auto *C2 = ConstantFP::get(FloatTy, 1.1);
43 EXPECT_TRUE(ValueLatticeElement::get(C2).isConstant());
44 EXPECT_TRUE(ValueLatticeElement::getNot(C2).isNotConstant());
45}
46
47TEST_F(ValueLatticeTest, MergeIn) {
48 auto I32Ty = IntegerType::get(Context, 32);
49 auto *C1 = ConstantInt::get(I32Ty, 1);
50
51 // Merge to lattice values with equal integer constant.
52 auto LV1 = ValueLatticeElement::get(C1);
53 LV1.mergeIn(ValueLatticeElement::get(C1), M.getDataLayout());
54 EXPECT_TRUE(LV1.isConstantRange());
55 EXPECT_EQ(LV1.asConstantInteger().getValue().getLimitedValue(), 1U);
56
57 // Merge LV1 with different integer constant.
58 LV1.mergeIn(ValueLatticeElement::get(ConstantInt::get(I32Ty, 99)),
59 M.getDataLayout());
60 EXPECT_TRUE(LV1.isConstantRange());
61 EXPECT_EQ(LV1.getConstantRange().getLower().getLimitedValue(), 1U);
62 EXPECT_EQ(LV1.getConstantRange().getUpper().getLimitedValue(), 100U);
63
64 // Merge LV1 in undefined value.
65 ValueLatticeElement LV2;
66 LV2.mergeIn(LV1, M.getDataLayout());
67 EXPECT_TRUE(LV1.isConstantRange());
68 EXPECT_EQ(LV1.getConstantRange().getLower().getLimitedValue(), 1U);
69 EXPECT_EQ(LV1.getConstantRange().getUpper().getLimitedValue(), 100U);
70 EXPECT_TRUE(LV2.isConstantRange());
71 EXPECT_EQ(LV2.getConstantRange().getLower().getLimitedValue(), 1U);
72 EXPECT_EQ(LV2.getConstantRange().getUpper().getLimitedValue(), 100U);
73
74 // Merge with overdefined.
75 LV1.mergeIn(ValueLatticeElement::getOverdefined(), M.getDataLayout());
76 EXPECT_TRUE(LV1.isOverdefined());
77}
78
79TEST_F(ValueLatticeTest, satisfiesPredicateIntegers) {
80 auto I32Ty = IntegerType::get(Context, 32);
81 auto *C1 = ConstantInt::get(I32Ty, 1);
82 auto LV1 = ValueLatticeElement::get(C1);
83
84 // Check satisfiesPredicate for equal integer constants.
85 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::ICMP_EQ, LV1));
86 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::ICMP_SGE, LV1));
87 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::ICMP_SLE, LV1));
88 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::ICMP_NE, LV1));
89 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::ICMP_SLT, LV1));
90 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::ICMP_SGT, LV1));
91
92 auto LV2 =
93 ValueLatticeElement::getRange({APInt(32, 10, true), APInt(32, 20, true)});
94 // Check satisfiesPredicate with distinct integer ranges.
95 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::ICMP_SLT, LV2));
96 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::ICMP_SLE, LV2));
97 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::ICMP_NE, LV2));
98 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::ICMP_EQ, LV2));
99 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::ICMP_SGE, LV2));
100 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::ICMP_SGT, LV2));
101
102 auto LV3 =
103 ValueLatticeElement::getRange({APInt(32, 15, true), APInt(32, 19, true)});
104 // Check satisfiesPredicate with a subset integer ranges.
105 EXPECT_FALSE(LV2.satisfiesPredicate(CmpInst::ICMP_SLT, LV3));
106 EXPECT_FALSE(LV2.satisfiesPredicate(CmpInst::ICMP_SLE, LV3));
107 EXPECT_FALSE(LV2.satisfiesPredicate(CmpInst::ICMP_NE, LV3));
108 EXPECT_FALSE(LV2.satisfiesPredicate(CmpInst::ICMP_EQ, LV3));
109 EXPECT_FALSE(LV2.satisfiesPredicate(CmpInst::ICMP_SGE, LV3));
110 EXPECT_FALSE(LV2.satisfiesPredicate(CmpInst::ICMP_SGT, LV3));
111
112 auto LV4 =
113 ValueLatticeElement::getRange({APInt(32, 15, true), APInt(32, 25, true)});
114 // Check satisfiesPredicate with overlapping integer ranges.
115 EXPECT_FALSE(LV3.satisfiesPredicate(CmpInst::ICMP_SLT, LV4));
116 EXPECT_FALSE(LV3.satisfiesPredicate(CmpInst::ICMP_SLE, LV4));
117 EXPECT_FALSE(LV3.satisfiesPredicate(CmpInst::ICMP_NE, LV4));
118 EXPECT_FALSE(LV3.satisfiesPredicate(CmpInst::ICMP_EQ, LV4));
119 EXPECT_FALSE(LV3.satisfiesPredicate(CmpInst::ICMP_SGE, LV4));
120 EXPECT_FALSE(LV3.satisfiesPredicate(CmpInst::ICMP_SGT, LV4));
121}
122
123TEST_F(ValueLatticeTest, satisfiesPredicateFloat) {
124 auto FloatTy = IntegerType::getFloatTy(Context);
125 auto *C1 = ConstantFP::get(FloatTy, 1.0);
126 auto LV1 = ValueLatticeElement::get(C1);
127 auto LV2 = ValueLatticeElement::get(C1);
128
129 // Check satisfiesPredicate for equal floating point constants.
130 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::FCMP_OEQ, LV2));
131 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OGE, LV2));
132 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OLE, LV2));
133 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_ONE, LV2));
134 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OLT, LV2));
135 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OGT, LV2));
136
137 LV1.mergeIn(ValueLatticeElement::get(ConstantFP::get(FloatTy, 2.2)),
138 M.getDataLayout());
139 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OEQ, LV2));
140 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OGE, LV2));
141 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OLE, LV2));
142 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_ONE, LV2));
143 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OLT, LV2));
144 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OGT, LV2));
145}
146
147} // end anonymous namespace
148} // end namespace llvm