blob: 844254ee32f2d85e4d9f902cec347168b91d35f1 [file] [log] [blame]
Florian Hahn8af01572017-09-28 11:09:22 +00001//===- ValueLatticeTest.cpp - ScalarEvolution unit tests --------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Florian Hahn8af01572017-09-28 11:09:22 +00006//
7//===----------------------------------------------------------------------===//
8
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"
17
18namespace llvm {
19namespace {
20
21// We use this fixture to ensure that we clean up ScalarEvolution before
22// deleting the PassManager.
23class ValueLatticeTest : public testing::Test {
24protected:
25 LLVMContext Context;
26 Module M;
27
28 ValueLatticeTest() : M("", Context) {}
29};
30
31TEST_F(ValueLatticeTest, ValueLatticeGetters) {
32 auto I32Ty = IntegerType::get(Context, 32);
33 auto *C1 = ConstantInt::get(I32Ty, 1);
34
35 EXPECT_TRUE(ValueLatticeElement::get(C1).isConstantRange());
36 EXPECT_TRUE(
37 ValueLatticeElement::getRange({C1->getValue()}).isConstantRange());
38 EXPECT_TRUE(ValueLatticeElement::getOverdefined().isOverdefined());
39
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());
44}
45
46TEST_F(ValueLatticeTest, MergeIn) {
47 auto I32Ty = IntegerType::get(Context, 32);
48 auto *C1 = ConstantInt::get(I32Ty, 1);
49
50 // Merge to lattice values with equal integer constant.
51 auto LV1 = ValueLatticeElement::get(C1);
Florian Hahnf6814132018-06-27 12:57:51 +000052 EXPECT_FALSE(LV1.mergeIn(ValueLatticeElement::get(C1), M.getDataLayout()));
Florian Hahn8af01572017-09-28 11:09:22 +000053 EXPECT_TRUE(LV1.isConstantRange());
54 EXPECT_EQ(LV1.asConstantInteger().getValue().getLimitedValue(), 1U);
55
56 // Merge LV1 with different integer constant.
Florian Hahnf6814132018-06-27 12:57:51 +000057 EXPECT_TRUE(LV1.mergeIn(ValueLatticeElement::get(ConstantInt::get(I32Ty, 99)),
58 M.getDataLayout()));
59 EXPECT_TRUE(LV1.isConstantRange());
60 EXPECT_EQ(LV1.getConstantRange().getLower().getLimitedValue(), 1U);
61 EXPECT_EQ(LV1.getConstantRange().getUpper().getLimitedValue(), 100U);
62
63 // Merge constant range with same constant range.
64 EXPECT_FALSE(LV1.mergeIn(LV1, M.getDataLayout()));
Florian Hahn8af01572017-09-28 11:09:22 +000065 EXPECT_TRUE(LV1.isConstantRange());
66 EXPECT_EQ(LV1.getConstantRange().getLower().getLimitedValue(), 1U);
67 EXPECT_EQ(LV1.getConstantRange().getUpper().getLimitedValue(), 100U);
68
69 // Merge LV1 in undefined value.
70 ValueLatticeElement LV2;
Florian Hahnf6814132018-06-27 12:57:51 +000071 EXPECT_TRUE(LV2.mergeIn(LV1, M.getDataLayout()));
Florian Hahn8af01572017-09-28 11:09:22 +000072 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);
78
Florian Hahnf6814132018-06-27 12:57:51 +000079 // Merge LV1 with overdefined.
80 EXPECT_TRUE(
81 LV1.mergeIn(ValueLatticeElement::getOverdefined(), M.getDataLayout()));
82 EXPECT_TRUE(LV1.isOverdefined());
83
84 // Merge overdefined with overdefined.
85 EXPECT_FALSE(
86 LV1.mergeIn(ValueLatticeElement::getOverdefined(), M.getDataLayout()));
Florian Hahn8af01572017-09-28 11:09:22 +000087 EXPECT_TRUE(LV1.isOverdefined());
88}
89
Florian Hahn0b7c6422018-03-05 17:33:50 +000090TEST_F(ValueLatticeTest, getCompareIntegers) {
91 auto *I32Ty = IntegerType::get(Context, 32);
92 auto *I1Ty = IntegerType::get(Context, 1);
Florian Hahn8af01572017-09-28 11:09:22 +000093 auto *C1 = ConstantInt::get(I32Ty, 1);
94 auto LV1 = ValueLatticeElement::get(C1);
95
Florian Hahn0b7c6422018-03-05 17:33:50 +000096 // 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());
Florian Hahn8af01572017-09-28 11:09:22 +0000103
104 auto LV2 =
105 ValueLatticeElement::getRange({APInt(32, 10, true), APInt(32, 20, true)});
Florian Hahn0b7c6422018-03-05 17:33:50 +0000106 // 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());
Florian Hahn8af01572017-09-28 11:09:22 +0000113
114 auto LV3 =
115 ValueLatticeElement::getRange({APInt(32, 15, true), APInt(32, 19, true)});
Florian Hahn0b7c6422018-03-05 17:33:50 +0000116 // 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);
Florian Hahn8af01572017-09-28 11:09:22 +0000123
124 auto LV4 =
125 ValueLatticeElement::getRange({APInt(32, 15, true), APInt(32, 25, true)});
Florian Hahn0b7c6422018-03-05 17:33:50 +0000126 // 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);
Florian Hahn8af01572017-09-28 11:09:22 +0000133}
134
Florian Hahn0b7c6422018-03-05 17:33:50 +0000135TEST_F(ValueLatticeTest, getCompareFloat) {
136 auto *FloatTy = IntegerType::getFloatTy(Context);
137 auto *I1Ty = IntegerType::get(Context, 1);
Florian Hahn8af01572017-09-28 11:09:22 +0000138 auto *C1 = ConstantFP::get(FloatTy, 1.0);
139 auto LV1 = ValueLatticeElement::get(C1);
140 auto LV2 = ValueLatticeElement::get(C1);
141
Florian Hahn0b7c6422018-03-05 17:33:50 +0000142 // 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());
Florian Hahn8af01572017-09-28 11:09:22 +0000149
Florian Hahnf6814132018-06-27 12:57:51 +0000150 EXPECT_TRUE(
151 LV1.mergeIn(ValueLatticeElement::get(ConstantFP::get(FloatTy, 2.2)),
152 M.getDataLayout()));
Florian Hahn0b7c6422018-03-05 17:33:50 +0000153 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);
159}
160
161TEST_F(ValueLatticeTest, getCompareUndef) {
162 auto *I32Ty = IntegerType::get(Context, 32);
163 auto *I1Ty = IntegerType::get(Context, 1);
164
165 auto LV1 = ValueLatticeElement::get(UndefValue::get(I32Ty));
166 auto LV2 =
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)));
174
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)));
Florian Hahn8af01572017-09-28 11:09:22 +0000183}
184
185} // end anonymous namespace
186} // end namespace llvm