1 //===- ValueLatticeTest.cpp - ScalarEvolution unit tests --------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/ValueLattice.h"
10 #include "llvm/ADT/SmallVector.h"
11 #include "llvm/IR/ConstantRange.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/IRBuilder.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "gtest/gtest.h"
21 // We use this fixture to ensure that we clean up ScalarEvolution before
22 // deleting the PassManager.
23 class ValueLatticeTest
: public testing::Test
{
28 ValueLatticeTest() : M("", Context
) {}
31 TEST_F(ValueLatticeTest
, ValueLatticeGetters
) {
32 auto I32Ty
= IntegerType::get(Context
, 32);
33 auto *C1
= ConstantInt::get(I32Ty
, 1);
35 EXPECT_TRUE(ValueLatticeElement::get(C1
).isConstantRange());
37 ValueLatticeElement::getRange({C1
->getValue()}).isConstantRange());
38 EXPECT_TRUE(ValueLatticeElement::getOverdefined().isOverdefined());
40 auto FloatTy
= Type::getFloatTy(Context
);
41 auto *C2
= ConstantFP::get(FloatTy
, 1.1);
42 EXPECT_TRUE(ValueLatticeElement::get(C2
).isConstant());
43 EXPECT_TRUE(ValueLatticeElement::getNot(C2
).isNotConstant());
46 TEST_F(ValueLatticeTest
, MergeIn
) {
47 auto I32Ty
= IntegerType::get(Context
, 32);
48 auto *C1
= ConstantInt::get(I32Ty
, 1);
50 // Merge to lattice values with equal integer constant.
51 auto LV1
= ValueLatticeElement::get(C1
);
52 EXPECT_FALSE(LV1
.mergeIn(ValueLatticeElement::get(C1
), M
.getDataLayout()));
53 EXPECT_TRUE(LV1
.isConstantRange());
54 EXPECT_EQ(LV1
.asConstantInteger().getValue().getLimitedValue(), 1U);
56 // Merge LV1 with different integer constant.
57 EXPECT_TRUE(LV1
.mergeIn(ValueLatticeElement::get(ConstantInt::get(I32Ty
, 99)),
59 EXPECT_TRUE(LV1
.isConstantRange());
60 EXPECT_EQ(LV1
.getConstantRange().getLower().getLimitedValue(), 1U);
61 EXPECT_EQ(LV1
.getConstantRange().getUpper().getLimitedValue(), 100U);
63 // Merge constant range with same constant range.
64 EXPECT_FALSE(LV1
.mergeIn(LV1
, M
.getDataLayout()));
65 EXPECT_TRUE(LV1
.isConstantRange());
66 EXPECT_EQ(LV1
.getConstantRange().getLower().getLimitedValue(), 1U);
67 EXPECT_EQ(LV1
.getConstantRange().getUpper().getLimitedValue(), 100U);
69 // Merge LV1 in undefined value.
70 ValueLatticeElement LV2
;
71 EXPECT_TRUE(LV2
.mergeIn(LV1
, M
.getDataLayout()));
72 EXPECT_TRUE(LV1
.isConstantRange());
73 EXPECT_EQ(LV1
.getConstantRange().getLower().getLimitedValue(), 1U);
74 EXPECT_EQ(LV1
.getConstantRange().getUpper().getLimitedValue(), 100U);
75 EXPECT_TRUE(LV2
.isConstantRange());
76 EXPECT_EQ(LV2
.getConstantRange().getLower().getLimitedValue(), 1U);
77 EXPECT_EQ(LV2
.getConstantRange().getUpper().getLimitedValue(), 100U);
79 // Merge LV1 with overdefined.
81 LV1
.mergeIn(ValueLatticeElement::getOverdefined(), M
.getDataLayout()));
82 EXPECT_TRUE(LV1
.isOverdefined());
84 // Merge overdefined with overdefined.
86 LV1
.mergeIn(ValueLatticeElement::getOverdefined(), M
.getDataLayout()));
87 EXPECT_TRUE(LV1
.isOverdefined());
90 TEST_F(ValueLatticeTest
, getCompareIntegers
) {
91 auto *I32Ty
= IntegerType::get(Context
, 32);
92 auto *I1Ty
= IntegerType::get(Context
, 1);
93 auto *C1
= ConstantInt::get(I32Ty
, 1);
94 auto LV1
= ValueLatticeElement::get(C1
);
96 // Check getCompare for equal integer constants.
97 EXPECT_TRUE(LV1
.getCompare(CmpInst::ICMP_EQ
, I1Ty
, LV1
)->isOneValue());
98 EXPECT_TRUE(LV1
.getCompare(CmpInst::ICMP_SGE
, I1Ty
, LV1
)->isOneValue());
99 EXPECT_TRUE(LV1
.getCompare(CmpInst::ICMP_SLE
, I1Ty
, LV1
)->isOneValue());
100 EXPECT_TRUE(LV1
.getCompare(CmpInst::ICMP_NE
, I1Ty
, LV1
)->isZeroValue());
101 EXPECT_TRUE(LV1
.getCompare(CmpInst::ICMP_SLT
, I1Ty
, LV1
)->isZeroValue());
102 EXPECT_TRUE(LV1
.getCompare(CmpInst::ICMP_SGT
, I1Ty
, LV1
)->isZeroValue());
105 ValueLatticeElement::getRange({APInt(32, 10, true), APInt(32, 20, true)});
106 // Check getCompare with distinct integer ranges.
107 EXPECT_TRUE(LV1
.getCompare(CmpInst::ICMP_SLT
, I1Ty
, LV2
)->isOneValue());
108 EXPECT_TRUE(LV1
.getCompare(CmpInst::ICMP_SLE
, I1Ty
, LV2
)->isOneValue());
109 EXPECT_TRUE(LV1
.getCompare(CmpInst::ICMP_NE
, I1Ty
, LV2
)->isOneValue());
110 EXPECT_TRUE(LV1
.getCompare(CmpInst::ICMP_EQ
, I1Ty
, LV2
)->isZeroValue());
111 EXPECT_TRUE(LV1
.getCompare(CmpInst::ICMP_SGE
, I1Ty
, LV2
)->isZeroValue());
112 EXPECT_TRUE(LV1
.getCompare(CmpInst::ICMP_SGT
, I1Ty
, LV2
)->isZeroValue());
115 ValueLatticeElement::getRange({APInt(32, 15, true), APInt(32, 19, true)});
116 // Check getCompare with a subset integer ranges.
117 EXPECT_EQ(LV2
.getCompare(CmpInst::ICMP_SLT
, I1Ty
, LV3
), nullptr);
118 EXPECT_EQ(LV2
.getCompare(CmpInst::ICMP_SLE
, I1Ty
, LV3
), nullptr);
119 EXPECT_EQ(LV2
.getCompare(CmpInst::ICMP_NE
, I1Ty
, LV3
), nullptr);
120 EXPECT_EQ(LV2
.getCompare(CmpInst::ICMP_EQ
, I1Ty
, LV3
), nullptr);
121 EXPECT_EQ(LV2
.getCompare(CmpInst::ICMP_SGE
, I1Ty
, LV3
), nullptr);
122 EXPECT_EQ(LV2
.getCompare(CmpInst::ICMP_SGT
, I1Ty
, LV3
), nullptr);
125 ValueLatticeElement::getRange({APInt(32, 15, true), APInt(32, 25, true)});
126 // Check getCompare with overlapping integer ranges.
127 EXPECT_EQ(LV3
.getCompare(CmpInst::ICMP_SLT
, I1Ty
, LV4
), nullptr);
128 EXPECT_EQ(LV3
.getCompare(CmpInst::ICMP_SLE
, I1Ty
, LV4
), nullptr);
129 EXPECT_EQ(LV3
.getCompare(CmpInst::ICMP_NE
, I1Ty
, LV4
), nullptr);
130 EXPECT_EQ(LV3
.getCompare(CmpInst::ICMP_EQ
, I1Ty
, LV4
), nullptr);
131 EXPECT_EQ(LV3
.getCompare(CmpInst::ICMP_SGE
, I1Ty
, LV4
), nullptr);
132 EXPECT_EQ(LV3
.getCompare(CmpInst::ICMP_SGT
, I1Ty
, LV4
), nullptr);
135 TEST_F(ValueLatticeTest
, getCompareFloat
) {
136 auto *FloatTy
= IntegerType::getFloatTy(Context
);
137 auto *I1Ty
= IntegerType::get(Context
, 1);
138 auto *C1
= ConstantFP::get(FloatTy
, 1.0);
139 auto LV1
= ValueLatticeElement::get(C1
);
140 auto LV2
= ValueLatticeElement::get(C1
);
142 // Check getCompare for equal floating point constants.
143 EXPECT_TRUE(LV1
.getCompare(CmpInst::FCMP_OEQ
, I1Ty
, LV2
)->isOneValue());
144 EXPECT_TRUE(LV1
.getCompare(CmpInst::FCMP_OGE
, I1Ty
, LV2
)->isOneValue());
145 EXPECT_TRUE(LV1
.getCompare(CmpInst::FCMP_OLE
, I1Ty
, LV2
)->isOneValue());
146 EXPECT_TRUE(LV1
.getCompare(CmpInst::FCMP_ONE
, I1Ty
, LV2
)->isZeroValue());
147 EXPECT_TRUE(LV1
.getCompare(CmpInst::FCMP_OLT
, I1Ty
, LV2
)->isZeroValue());
148 EXPECT_TRUE(LV1
.getCompare(CmpInst::FCMP_OGT
, I1Ty
, LV2
)->isZeroValue());
151 LV1
.mergeIn(ValueLatticeElement::get(ConstantFP::get(FloatTy
, 2.2)),
153 EXPECT_EQ(LV1
.getCompare(CmpInst::FCMP_OEQ
, I1Ty
, LV2
), nullptr);
154 EXPECT_EQ(LV1
.getCompare(CmpInst::FCMP_OGE
, I1Ty
, LV2
), nullptr);
155 EXPECT_EQ(LV1
.getCompare(CmpInst::FCMP_OLE
, I1Ty
, LV2
), nullptr);
156 EXPECT_EQ(LV1
.getCompare(CmpInst::FCMP_ONE
, I1Ty
, LV2
), nullptr);
157 EXPECT_EQ(LV1
.getCompare(CmpInst::FCMP_OLT
, I1Ty
, LV2
), nullptr);
158 EXPECT_EQ(LV1
.getCompare(CmpInst::FCMP_OGT
, I1Ty
, LV2
), nullptr);
161 TEST_F(ValueLatticeTest
, getCompareUndef
) {
162 auto *I32Ty
= IntegerType::get(Context
, 32);
163 auto *I1Ty
= IntegerType::get(Context
, 1);
165 auto LV1
= ValueLatticeElement::get(UndefValue::get(I32Ty
));
167 ValueLatticeElement::getRange({APInt(32, 10, true), APInt(32, 20, true)});
168 EXPECT_TRUE(isa
<UndefValue
>(LV1
.getCompare(CmpInst::ICMP_SLT
, I1Ty
, LV2
)));
169 EXPECT_TRUE(isa
<UndefValue
>(LV1
.getCompare(CmpInst::ICMP_SLE
, I1Ty
, LV2
)));
170 EXPECT_TRUE(isa
<UndefValue
>(LV1
.getCompare(CmpInst::ICMP_NE
, I1Ty
, LV2
)));
171 EXPECT_TRUE(isa
<UndefValue
>(LV1
.getCompare(CmpInst::ICMP_EQ
, I1Ty
, LV2
)));
172 EXPECT_TRUE(isa
<UndefValue
>(LV1
.getCompare(CmpInst::ICMP_SGE
, I1Ty
, LV2
)));
173 EXPECT_TRUE(isa
<UndefValue
>(LV1
.getCompare(CmpInst::ICMP_SGT
, I1Ty
, LV2
)));
175 auto *FloatTy
= IntegerType::getFloatTy(Context
);
176 auto LV3
= ValueLatticeElement::get(ConstantFP::get(FloatTy
, 1.0));
177 EXPECT_TRUE(isa
<UndefValue
>(LV1
.getCompare(CmpInst::FCMP_OEQ
, I1Ty
, LV3
)));
178 EXPECT_TRUE(isa
<UndefValue
>(LV1
.getCompare(CmpInst::FCMP_OGE
, I1Ty
, LV3
)));
179 EXPECT_TRUE(isa
<UndefValue
>(LV1
.getCompare(CmpInst::FCMP_OLE
, I1Ty
, LV3
)));
180 EXPECT_TRUE(isa
<UndefValue
>(LV1
.getCompare(CmpInst::FCMP_ONE
, I1Ty
, LV3
)));
181 EXPECT_TRUE(isa
<UndefValue
>(LV1
.getCompare(CmpInst::FCMP_OLT
, I1Ty
, LV3
)));
182 EXPECT_TRUE(isa
<UndefValue
>(LV1
.getCompare(CmpInst::FCMP_OGT
, I1Ty
, LV3
)));
185 } // end anonymous namespace
186 } // end namespace llvm