1e5dd7070Spatrick //== RangeConstraintManager.cpp - Manage range constraints.------*- C++ -*--==//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This file defines RangeConstraintManager, a class that tracks simple
10e5dd7070Spatrick // equality and inequality constraints on symbolic values of ProgramState.
11e5dd7070Spatrick //
12e5dd7070Spatrick //===----------------------------------------------------------------------===//
13e5dd7070Spatrick
14e5dd7070Spatrick #include "clang/Basic/JsonSupport.h"
15e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
16e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
17e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
18e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h"
19ec727ea7Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/SValVisitor.h"
20e5dd7070Spatrick #include "llvm/ADT/FoldingSet.h"
21e5dd7070Spatrick #include "llvm/ADT/ImmutableSet.h"
22a9ac8606Spatrick #include "llvm/ADT/STLExtras.h"
23a9ac8606Spatrick #include "llvm/ADT/SmallSet.h"
24*12c85518Srobert #include "llvm/ADT/StringExtras.h"
25a9ac8606Spatrick #include "llvm/Support/Compiler.h"
26e5dd7070Spatrick #include "llvm/Support/raw_ostream.h"
27a9ac8606Spatrick #include <algorithm>
28a9ac8606Spatrick #include <iterator>
29*12c85518Srobert #include <optional>
30e5dd7070Spatrick
31e5dd7070Spatrick using namespace clang;
32e5dd7070Spatrick using namespace ento;
33e5dd7070Spatrick
34ec727ea7Spatrick // This class can be extended with other tables which will help to reason
35ec727ea7Spatrick // about ranges more precisely.
36ec727ea7Spatrick class OperatorRelationsTable {
37ec727ea7Spatrick static_assert(BO_LT < BO_GT && BO_GT < BO_LE && BO_LE < BO_GE &&
38ec727ea7Spatrick BO_GE < BO_EQ && BO_EQ < BO_NE,
39ec727ea7Spatrick "This class relies on operators order. Rework it otherwise.");
40ec727ea7Spatrick
41ec727ea7Spatrick public:
42ec727ea7Spatrick enum TriStateKind {
43ec727ea7Spatrick False = 0,
44ec727ea7Spatrick True,
45ec727ea7Spatrick Unknown,
46ec727ea7Spatrick };
47ec727ea7Spatrick
48ec727ea7Spatrick private:
49ec727ea7Spatrick // CmpOpTable holds states which represent the corresponding range for
50ec727ea7Spatrick // branching an exploded graph. We can reason about the branch if there is
51ec727ea7Spatrick // a previously known fact of the existence of a comparison expression with
52ec727ea7Spatrick // operands used in the current expression.
53ec727ea7Spatrick // E.g. assuming (x < y) is true that means (x != y) is surely true.
54ec727ea7Spatrick // if (x previous_operation y) // < | != | >
55ec727ea7Spatrick // if (x operation y) // != | > | <
56ec727ea7Spatrick // tristate // True | Unknown | False
57ec727ea7Spatrick //
58ec727ea7Spatrick // CmpOpTable represents next:
59ec727ea7Spatrick // __|< |> |<=|>=|==|!=|UnknownX2|
60ec727ea7Spatrick // < |1 |0 |* |0 |0 |* |1 |
61ec727ea7Spatrick // > |0 |1 |0 |* |0 |* |1 |
62ec727ea7Spatrick // <=|1 |0 |1 |* |1 |* |0 |
63ec727ea7Spatrick // >=|0 |1 |* |1 |1 |* |0 |
64ec727ea7Spatrick // ==|0 |0 |* |* |1 |0 |1 |
65ec727ea7Spatrick // !=|1 |1 |* |* |0 |1 |0 |
66ec727ea7Spatrick //
67ec727ea7Spatrick // Columns stands for a previous operator.
68ec727ea7Spatrick // Rows stands for a current operator.
69ec727ea7Spatrick // Each row has exactly two `Unknown` cases.
70ec727ea7Spatrick // UnknownX2 means that both `Unknown` previous operators are met in code,
71ec727ea7Spatrick // and there is a special column for that, for example:
72ec727ea7Spatrick // if (x >= y)
73ec727ea7Spatrick // if (x != y)
74ec727ea7Spatrick // if (x <= y)
75ec727ea7Spatrick // False only
76ec727ea7Spatrick static constexpr size_t CmpOpCount = BO_NE - BO_LT + 1;
77ec727ea7Spatrick const TriStateKind CmpOpTable[CmpOpCount][CmpOpCount + 1] = {
78ec727ea7Spatrick // < > <= >= == != UnknownX2
79ec727ea7Spatrick {True, False, Unknown, False, False, Unknown, True}, // <
80ec727ea7Spatrick {False, True, False, Unknown, False, Unknown, True}, // >
81ec727ea7Spatrick {True, False, True, Unknown, True, Unknown, False}, // <=
82ec727ea7Spatrick {False, True, Unknown, True, True, Unknown, False}, // >=
83ec727ea7Spatrick {False, False, Unknown, Unknown, True, False, True}, // ==
84ec727ea7Spatrick {True, True, Unknown, Unknown, False, True, False}, // !=
85ec727ea7Spatrick };
86ec727ea7Spatrick
getIndexFromOp(BinaryOperatorKind OP)87ec727ea7Spatrick static size_t getIndexFromOp(BinaryOperatorKind OP) {
88ec727ea7Spatrick return static_cast<size_t>(OP - BO_LT);
89ec727ea7Spatrick }
90ec727ea7Spatrick
91ec727ea7Spatrick public:
getCmpOpCount() const92ec727ea7Spatrick constexpr size_t getCmpOpCount() const { return CmpOpCount; }
93ec727ea7Spatrick
getOpFromIndex(size_t Index)94ec727ea7Spatrick static BinaryOperatorKind getOpFromIndex(size_t Index) {
95ec727ea7Spatrick return static_cast<BinaryOperatorKind>(Index + BO_LT);
96ec727ea7Spatrick }
97ec727ea7Spatrick
getCmpOpState(BinaryOperatorKind CurrentOP,BinaryOperatorKind QueriedOP) const98ec727ea7Spatrick TriStateKind getCmpOpState(BinaryOperatorKind CurrentOP,
99ec727ea7Spatrick BinaryOperatorKind QueriedOP) const {
100ec727ea7Spatrick return CmpOpTable[getIndexFromOp(CurrentOP)][getIndexFromOp(QueriedOP)];
101ec727ea7Spatrick }
102ec727ea7Spatrick
getCmpOpStateForUnknownX2(BinaryOperatorKind CurrentOP) const103ec727ea7Spatrick TriStateKind getCmpOpStateForUnknownX2(BinaryOperatorKind CurrentOP) const {
104ec727ea7Spatrick return CmpOpTable[getIndexFromOp(CurrentOP)][CmpOpCount];
105ec727ea7Spatrick }
106ec727ea7Spatrick };
107a9ac8606Spatrick
108ec727ea7Spatrick //===----------------------------------------------------------------------===//
109ec727ea7Spatrick // RangeSet implementation
110ec727ea7Spatrick //===----------------------------------------------------------------------===//
111ec727ea7Spatrick
112a9ac8606Spatrick RangeSet::ContainerType RangeSet::Factory::EmptySet{};
113a9ac8606Spatrick
add(RangeSet LHS,RangeSet RHS)114*12c85518Srobert RangeSet RangeSet::Factory::add(RangeSet LHS, RangeSet RHS) {
115*12c85518Srobert ContainerType Result;
116*12c85518Srobert Result.reserve(LHS.size() + RHS.size());
117*12c85518Srobert std::merge(LHS.begin(), LHS.end(), RHS.begin(), RHS.end(),
118*12c85518Srobert std::back_inserter(Result));
119*12c85518Srobert return makePersistent(std::move(Result));
120*12c85518Srobert }
121*12c85518Srobert
add(RangeSet Original,Range Element)122a9ac8606Spatrick RangeSet RangeSet::Factory::add(RangeSet Original, Range Element) {
123a9ac8606Spatrick ContainerType Result;
124a9ac8606Spatrick Result.reserve(Original.size() + 1);
125a9ac8606Spatrick
126a9ac8606Spatrick const_iterator Lower = llvm::lower_bound(Original, Element);
127a9ac8606Spatrick Result.insert(Result.end(), Original.begin(), Lower);
128a9ac8606Spatrick Result.push_back(Element);
129a9ac8606Spatrick Result.insert(Result.end(), Lower, Original.end());
130a9ac8606Spatrick
131a9ac8606Spatrick return makePersistent(std::move(Result));
132e5dd7070Spatrick }
133e5dd7070Spatrick
add(RangeSet Original,const llvm::APSInt & Point)134a9ac8606Spatrick RangeSet RangeSet::Factory::add(RangeSet Original, const llvm::APSInt &Point) {
135a9ac8606Spatrick return add(Original, Range(Point));
136e5dd7070Spatrick }
137a9ac8606Spatrick
unite(RangeSet LHS,RangeSet RHS)138*12c85518Srobert RangeSet RangeSet::Factory::unite(RangeSet LHS, RangeSet RHS) {
139*12c85518Srobert ContainerType Result = unite(*LHS.Impl, *RHS.Impl);
140*12c85518Srobert return makePersistent(std::move(Result));
141*12c85518Srobert }
142*12c85518Srobert
unite(RangeSet Original,Range R)143*12c85518Srobert RangeSet RangeSet::Factory::unite(RangeSet Original, Range R) {
144*12c85518Srobert ContainerType Result;
145*12c85518Srobert Result.push_back(R);
146*12c85518Srobert Result = unite(*Original.Impl, Result);
147*12c85518Srobert return makePersistent(std::move(Result));
148*12c85518Srobert }
149*12c85518Srobert
unite(RangeSet Original,llvm::APSInt Point)150*12c85518Srobert RangeSet RangeSet::Factory::unite(RangeSet Original, llvm::APSInt Point) {
151*12c85518Srobert return unite(Original, Range(ValueFactory.getValue(Point)));
152*12c85518Srobert }
153*12c85518Srobert
unite(RangeSet Original,llvm::APSInt From,llvm::APSInt To)154*12c85518Srobert RangeSet RangeSet::Factory::unite(RangeSet Original, llvm::APSInt From,
155*12c85518Srobert llvm::APSInt To) {
156*12c85518Srobert return unite(Original,
157*12c85518Srobert Range(ValueFactory.getValue(From), ValueFactory.getValue(To)));
158*12c85518Srobert }
159*12c85518Srobert
160*12c85518Srobert template <typename T>
swapIterators(T & First,T & FirstEnd,T & Second,T & SecondEnd)161*12c85518Srobert void swapIterators(T &First, T &FirstEnd, T &Second, T &SecondEnd) {
162*12c85518Srobert std::swap(First, Second);
163*12c85518Srobert std::swap(FirstEnd, SecondEnd);
164*12c85518Srobert }
165*12c85518Srobert
unite(const ContainerType & LHS,const ContainerType & RHS)166*12c85518Srobert RangeSet::ContainerType RangeSet::Factory::unite(const ContainerType &LHS,
167*12c85518Srobert const ContainerType &RHS) {
168*12c85518Srobert if (LHS.empty())
169*12c85518Srobert return RHS;
170*12c85518Srobert if (RHS.empty())
171*12c85518Srobert return LHS;
172*12c85518Srobert
173*12c85518Srobert using llvm::APSInt;
174*12c85518Srobert using iterator = ContainerType::const_iterator;
175*12c85518Srobert
176*12c85518Srobert iterator First = LHS.begin();
177*12c85518Srobert iterator FirstEnd = LHS.end();
178*12c85518Srobert iterator Second = RHS.begin();
179*12c85518Srobert iterator SecondEnd = RHS.end();
180*12c85518Srobert APSIntType Ty = APSIntType(First->From());
181*12c85518Srobert const APSInt Min = Ty.getMinValue();
182*12c85518Srobert
183*12c85518Srobert // Handle a corner case first when both range sets start from MIN.
184*12c85518Srobert // This helps to avoid complicated conditions below. Specifically, this
185*12c85518Srobert // particular check for `MIN` is not needed in the loop below every time
186*12c85518Srobert // when we do `Second->From() - One` operation.
187*12c85518Srobert if (Min == First->From() && Min == Second->From()) {
188*12c85518Srobert if (First->To() > Second->To()) {
189*12c85518Srobert // [ First ]--->
190*12c85518Srobert // [ Second ]----->
191*12c85518Srobert // MIN^
192*12c85518Srobert // The Second range is entirely inside the First one.
193*12c85518Srobert
194*12c85518Srobert // Check if Second is the last in its RangeSet.
195*12c85518Srobert if (++Second == SecondEnd)
196*12c85518Srobert // [ First ]--[ First + 1 ]--->
197*12c85518Srobert // [ Second ]--------------------->
198*12c85518Srobert // MIN^
199*12c85518Srobert // The Union is equal to First's RangeSet.
200*12c85518Srobert return LHS;
201*12c85518Srobert } else {
202*12c85518Srobert // case 1: [ First ]----->
203*12c85518Srobert // case 2: [ First ]--->
204*12c85518Srobert // [ Second ]--->
205*12c85518Srobert // MIN^
206*12c85518Srobert // The First range is entirely inside or equal to the Second one.
207*12c85518Srobert
208*12c85518Srobert // Check if First is the last in its RangeSet.
209*12c85518Srobert if (++First == FirstEnd)
210*12c85518Srobert // [ First ]----------------------->
211*12c85518Srobert // [ Second ]--[ Second + 1 ]---->
212*12c85518Srobert // MIN^
213*12c85518Srobert // The Union is equal to Second's RangeSet.
214*12c85518Srobert return RHS;
215*12c85518Srobert }
216*12c85518Srobert }
217*12c85518Srobert
218*12c85518Srobert const APSInt One = Ty.getValue(1);
219*12c85518Srobert ContainerType Result;
220*12c85518Srobert
221*12c85518Srobert // This is called when there are no ranges left in one of the ranges.
222*12c85518Srobert // Append the rest of the ranges from another range set to the Result
223*12c85518Srobert // and return with that.
224*12c85518Srobert const auto AppendTheRest = [&Result](iterator I, iterator E) {
225*12c85518Srobert Result.append(I, E);
226*12c85518Srobert return Result;
227*12c85518Srobert };
228*12c85518Srobert
229*12c85518Srobert while (true) {
230*12c85518Srobert // We want to keep the following invariant at all times:
231*12c85518Srobert // ---[ First ------>
232*12c85518Srobert // -----[ Second --->
233*12c85518Srobert if (First->From() > Second->From())
234*12c85518Srobert swapIterators(First, FirstEnd, Second, SecondEnd);
235*12c85518Srobert
236*12c85518Srobert // The Union definitely starts with First->From().
237*12c85518Srobert // ----------[ First ------>
238*12c85518Srobert // ------------[ Second --->
239*12c85518Srobert // ----------[ Union ------>
240*12c85518Srobert // UnionStart^
241*12c85518Srobert const llvm::APSInt &UnionStart = First->From();
242*12c85518Srobert
243*12c85518Srobert // Loop where the invariant holds.
244*12c85518Srobert while (true) {
245*12c85518Srobert // Skip all enclosed ranges.
246*12c85518Srobert // ---[ First ]--->
247*12c85518Srobert // -----[ Second ]--[ Second + 1 ]--[ Second + N ]----->
248*12c85518Srobert while (First->To() >= Second->To()) {
249*12c85518Srobert // Check if Second is the last in its RangeSet.
250*12c85518Srobert if (++Second == SecondEnd) {
251*12c85518Srobert // Append the Union.
252*12c85518Srobert // ---[ Union ]--->
253*12c85518Srobert // -----[ Second ]----->
254*12c85518Srobert // --------[ First ]--->
255*12c85518Srobert // UnionEnd^
256*12c85518Srobert Result.emplace_back(UnionStart, First->To());
257*12c85518Srobert // ---[ Union ]----------------->
258*12c85518Srobert // --------------[ First + 1]--->
259*12c85518Srobert // Append all remaining ranges from the First's RangeSet.
260*12c85518Srobert return AppendTheRest(++First, FirstEnd);
261*12c85518Srobert }
262*12c85518Srobert }
263*12c85518Srobert
264*12c85518Srobert // Check if First and Second are disjoint. It means that we find
265*12c85518Srobert // the end of the Union. Exit the loop and append the Union.
266*12c85518Srobert // ---[ First ]=------------->
267*12c85518Srobert // ------------=[ Second ]--->
268*12c85518Srobert // ----MinusOne^
269*12c85518Srobert if (First->To() < Second->From() - One)
270*12c85518Srobert break;
271*12c85518Srobert
272*12c85518Srobert // First is entirely inside the Union. Go next.
273*12c85518Srobert // ---[ Union ----------->
274*12c85518Srobert // ---- [ First ]-------->
275*12c85518Srobert // -------[ Second ]----->
276*12c85518Srobert // Check if First is the last in its RangeSet.
277*12c85518Srobert if (++First == FirstEnd) {
278*12c85518Srobert // Append the Union.
279*12c85518Srobert // ---[ Union ]--->
280*12c85518Srobert // -----[ First ]------->
281*12c85518Srobert // --------[ Second ]--->
282*12c85518Srobert // UnionEnd^
283*12c85518Srobert Result.emplace_back(UnionStart, Second->To());
284*12c85518Srobert // ---[ Union ]------------------>
285*12c85518Srobert // --------------[ Second + 1]--->
286*12c85518Srobert // Append all remaining ranges from the Second's RangeSet.
287*12c85518Srobert return AppendTheRest(++Second, SecondEnd);
288*12c85518Srobert }
289*12c85518Srobert
290*12c85518Srobert // We know that we are at one of the two cases:
291*12c85518Srobert // case 1: --[ First ]--------->
292*12c85518Srobert // case 2: ----[ First ]------->
293*12c85518Srobert // --------[ Second ]---------->
294*12c85518Srobert // In both cases First starts after Second->From().
295*12c85518Srobert // Make sure that the loop invariant holds.
296*12c85518Srobert swapIterators(First, FirstEnd, Second, SecondEnd);
297*12c85518Srobert }
298*12c85518Srobert
299*12c85518Srobert // Here First and Second are disjoint.
300*12c85518Srobert // Append the Union.
301*12c85518Srobert // ---[ Union ]--------------->
302*12c85518Srobert // -----------------[ Second ]--->
303*12c85518Srobert // ------[ First ]--------------->
304*12c85518Srobert // UnionEnd^
305*12c85518Srobert Result.emplace_back(UnionStart, First->To());
306*12c85518Srobert
307*12c85518Srobert // Check if First is the last in its RangeSet.
308*12c85518Srobert if (++First == FirstEnd)
309*12c85518Srobert // ---[ Union ]--------------->
310*12c85518Srobert // --------------[ Second ]--->
311*12c85518Srobert // Append all remaining ranges from the Second's RangeSet.
312*12c85518Srobert return AppendTheRest(Second, SecondEnd);
313*12c85518Srobert }
314*12c85518Srobert
315*12c85518Srobert llvm_unreachable("Normally, we should not reach here");
316*12c85518Srobert }
317*12c85518Srobert
getRangeSet(Range From)318a9ac8606Spatrick RangeSet RangeSet::Factory::getRangeSet(Range From) {
319a9ac8606Spatrick ContainerType Result;
320a9ac8606Spatrick Result.push_back(From);
321a9ac8606Spatrick return makePersistent(std::move(Result));
322e5dd7070Spatrick }
323a9ac8606Spatrick
makePersistent(ContainerType && From)324a9ac8606Spatrick RangeSet RangeSet::Factory::makePersistent(ContainerType &&From) {
325a9ac8606Spatrick llvm::FoldingSetNodeID ID;
326a9ac8606Spatrick void *InsertPos;
327a9ac8606Spatrick
328a9ac8606Spatrick From.Profile(ID);
329a9ac8606Spatrick ContainerType *Result = Cache.FindNodeOrInsertPos(ID, InsertPos);
330a9ac8606Spatrick
331a9ac8606Spatrick if (!Result) {
332a9ac8606Spatrick // It is cheaper to fully construct the resulting range on stack
333a9ac8606Spatrick // and move it to the freshly allocated buffer if we don't have
334a9ac8606Spatrick // a set like this already.
335a9ac8606Spatrick Result = construct(std::move(From));
336a9ac8606Spatrick Cache.InsertNode(Result, InsertPos);
337a9ac8606Spatrick }
338a9ac8606Spatrick
339a9ac8606Spatrick return Result;
340a9ac8606Spatrick }
341a9ac8606Spatrick
construct(ContainerType && From)342a9ac8606Spatrick RangeSet::ContainerType *RangeSet::Factory::construct(ContainerType &&From) {
343a9ac8606Spatrick void *Buffer = Arena.Allocate();
344a9ac8606Spatrick return new (Buffer) ContainerType(std::move(From));
345a9ac8606Spatrick }
346a9ac8606Spatrick
getMinValue() const347e5dd7070Spatrick const llvm::APSInt &RangeSet::getMinValue() const {
348e5dd7070Spatrick assert(!isEmpty());
349ec727ea7Spatrick return begin()->From();
350ec727ea7Spatrick }
351ec727ea7Spatrick
getMaxValue() const352ec727ea7Spatrick const llvm::APSInt &RangeSet::getMaxValue() const {
353ec727ea7Spatrick assert(!isEmpty());
354a9ac8606Spatrick return std::prev(end())->To();
355ec727ea7Spatrick }
356a9ac8606Spatrick
isUnsigned() const357*12c85518Srobert bool clang::ento::RangeSet::isUnsigned() const {
358*12c85518Srobert assert(!isEmpty());
359*12c85518Srobert return begin()->From().isUnsigned();
360*12c85518Srobert }
361*12c85518Srobert
getBitWidth() const362*12c85518Srobert uint32_t clang::ento::RangeSet::getBitWidth() const {
363*12c85518Srobert assert(!isEmpty());
364*12c85518Srobert return begin()->From().getBitWidth();
365*12c85518Srobert }
366*12c85518Srobert
getAPSIntType() const367*12c85518Srobert APSIntType clang::ento::RangeSet::getAPSIntType() const {
368*12c85518Srobert assert(!isEmpty());
369*12c85518Srobert return APSIntType(begin()->From());
370*12c85518Srobert }
371*12c85518Srobert
containsImpl(llvm::APSInt & Point) const372a9ac8606Spatrick bool RangeSet::containsImpl(llvm::APSInt &Point) const {
373a9ac8606Spatrick if (isEmpty() || !pin(Point))
374a9ac8606Spatrick return false;
375a9ac8606Spatrick
376a9ac8606Spatrick Range Dummy(Point);
377a9ac8606Spatrick const_iterator It = llvm::upper_bound(*this, Dummy);
378a9ac8606Spatrick if (It == begin())
379a9ac8606Spatrick return false;
380a9ac8606Spatrick
381a9ac8606Spatrick return std::prev(It)->Includes(Point);
382a9ac8606Spatrick }
383a9ac8606Spatrick
pin(llvm::APSInt & Point) const384a9ac8606Spatrick bool RangeSet::pin(llvm::APSInt &Point) const {
385a9ac8606Spatrick APSIntType Type(getMinValue());
386a9ac8606Spatrick if (Type.testInRange(Point, true) != APSIntType::RTR_Within)
387a9ac8606Spatrick return false;
388a9ac8606Spatrick
389a9ac8606Spatrick Type.apply(Point);
390a9ac8606Spatrick return true;
391e5dd7070Spatrick }
392e5dd7070Spatrick
pin(llvm::APSInt & Lower,llvm::APSInt & Upper) const393e5dd7070Spatrick bool RangeSet::pin(llvm::APSInt &Lower, llvm::APSInt &Upper) const {
394e5dd7070Spatrick // This function has nine cases, the cartesian product of range-testing
395e5dd7070Spatrick // both the upper and lower bounds against the symbol's type.
396e5dd7070Spatrick // Each case requires a different pinning operation.
397e5dd7070Spatrick // The function returns false if the described range is entirely outside
398e5dd7070Spatrick // the range of values for the associated symbol.
399e5dd7070Spatrick APSIntType Type(getMinValue());
400e5dd7070Spatrick APSIntType::RangeTestResultKind LowerTest = Type.testInRange(Lower, true);
401e5dd7070Spatrick APSIntType::RangeTestResultKind UpperTest = Type.testInRange(Upper, true);
402e5dd7070Spatrick
403e5dd7070Spatrick switch (LowerTest) {
404e5dd7070Spatrick case APSIntType::RTR_Below:
405e5dd7070Spatrick switch (UpperTest) {
406e5dd7070Spatrick case APSIntType::RTR_Below:
407e5dd7070Spatrick // The entire range is outside the symbol's set of possible values.
408e5dd7070Spatrick // If this is a conventionally-ordered range, the state is infeasible.
409e5dd7070Spatrick if (Lower <= Upper)
410e5dd7070Spatrick return false;
411e5dd7070Spatrick
412e5dd7070Spatrick // However, if the range wraps around, it spans all possible values.
413e5dd7070Spatrick Lower = Type.getMinValue();
414e5dd7070Spatrick Upper = Type.getMaxValue();
415e5dd7070Spatrick break;
416e5dd7070Spatrick case APSIntType::RTR_Within:
417e5dd7070Spatrick // The range starts below what's possible but ends within it. Pin.
418e5dd7070Spatrick Lower = Type.getMinValue();
419e5dd7070Spatrick Type.apply(Upper);
420e5dd7070Spatrick break;
421e5dd7070Spatrick case APSIntType::RTR_Above:
422e5dd7070Spatrick // The range spans all possible values for the symbol. Pin.
423e5dd7070Spatrick Lower = Type.getMinValue();
424e5dd7070Spatrick Upper = Type.getMaxValue();
425e5dd7070Spatrick break;
426e5dd7070Spatrick }
427e5dd7070Spatrick break;
428e5dd7070Spatrick case APSIntType::RTR_Within:
429e5dd7070Spatrick switch (UpperTest) {
430e5dd7070Spatrick case APSIntType::RTR_Below:
431e5dd7070Spatrick // The range wraps around, but all lower values are not possible.
432e5dd7070Spatrick Type.apply(Lower);
433e5dd7070Spatrick Upper = Type.getMaxValue();
434e5dd7070Spatrick break;
435e5dd7070Spatrick case APSIntType::RTR_Within:
436e5dd7070Spatrick // The range may or may not wrap around, but both limits are valid.
437e5dd7070Spatrick Type.apply(Lower);
438e5dd7070Spatrick Type.apply(Upper);
439e5dd7070Spatrick break;
440e5dd7070Spatrick case APSIntType::RTR_Above:
441e5dd7070Spatrick // The range starts within what's possible but ends above it. Pin.
442e5dd7070Spatrick Type.apply(Lower);
443e5dd7070Spatrick Upper = Type.getMaxValue();
444e5dd7070Spatrick break;
445e5dd7070Spatrick }
446e5dd7070Spatrick break;
447e5dd7070Spatrick case APSIntType::RTR_Above:
448e5dd7070Spatrick switch (UpperTest) {
449e5dd7070Spatrick case APSIntType::RTR_Below:
450e5dd7070Spatrick // The range wraps but is outside the symbol's set of possible values.
451e5dd7070Spatrick return false;
452e5dd7070Spatrick case APSIntType::RTR_Within:
453e5dd7070Spatrick // The range starts above what's possible but ends within it (wrap).
454e5dd7070Spatrick Lower = Type.getMinValue();
455e5dd7070Spatrick Type.apply(Upper);
456e5dd7070Spatrick break;
457e5dd7070Spatrick case APSIntType::RTR_Above:
458e5dd7070Spatrick // The entire range is outside the symbol's set of possible values.
459e5dd7070Spatrick // If this is a conventionally-ordered range, the state is infeasible.
460e5dd7070Spatrick if (Lower <= Upper)
461e5dd7070Spatrick return false;
462e5dd7070Spatrick
463e5dd7070Spatrick // However, if the range wraps around, it spans all possible values.
464e5dd7070Spatrick Lower = Type.getMinValue();
465e5dd7070Spatrick Upper = Type.getMaxValue();
466e5dd7070Spatrick break;
467e5dd7070Spatrick }
468e5dd7070Spatrick break;
469e5dd7070Spatrick }
470e5dd7070Spatrick
471e5dd7070Spatrick return true;
472e5dd7070Spatrick }
473e5dd7070Spatrick
intersect(RangeSet What,llvm::APSInt Lower,llvm::APSInt Upper)474a9ac8606Spatrick RangeSet RangeSet::Factory::intersect(RangeSet What, llvm::APSInt Lower,
475a9ac8606Spatrick llvm::APSInt Upper) {
476a9ac8606Spatrick if (What.isEmpty() || !What.pin(Lower, Upper))
477a9ac8606Spatrick return getEmptySet();
478e5dd7070Spatrick
479a9ac8606Spatrick ContainerType DummyContainer;
480ec727ea7Spatrick
481a9ac8606Spatrick if (Lower <= Upper) {
482a9ac8606Spatrick // [Lower, Upper] is a regular range.
483ec727ea7Spatrick //
484a9ac8606Spatrick // Shortcut: check that there is even a possibility of the intersection
485a9ac8606Spatrick // by checking the two following situations:
486ec727ea7Spatrick //
487a9ac8606Spatrick // <---[ What ]---[------]------>
488a9ac8606Spatrick // Lower Upper
489a9ac8606Spatrick // -or-
490a9ac8606Spatrick // <----[------]----[ What ]---->
491a9ac8606Spatrick // Lower Upper
492a9ac8606Spatrick if (What.getMaxValue() < Lower || Upper < What.getMinValue())
493a9ac8606Spatrick return getEmptySet();
494a9ac8606Spatrick
495a9ac8606Spatrick DummyContainer.push_back(
496a9ac8606Spatrick Range(ValueFactory.getValue(Lower), ValueFactory.getValue(Upper)));
497a9ac8606Spatrick } else {
498a9ac8606Spatrick // [Lower, Upper] is an inverted range, i.e. [MIN, Upper] U [Lower, MAX]
499ec727ea7Spatrick //
500a9ac8606Spatrick // Shortcut: check that there is even a possibility of the intersection
501a9ac8606Spatrick // by checking the following situation:
502a9ac8606Spatrick //
503a9ac8606Spatrick // <------]---[ What ]---[------>
504a9ac8606Spatrick // Upper Lower
505a9ac8606Spatrick if (What.getMaxValue() < Lower && Upper < What.getMinValue())
506a9ac8606Spatrick return getEmptySet();
507e5dd7070Spatrick
508a9ac8606Spatrick DummyContainer.push_back(
509a9ac8606Spatrick Range(ValueFactory.getMinValue(Upper), ValueFactory.getValue(Upper)));
510a9ac8606Spatrick DummyContainer.push_back(
511a9ac8606Spatrick Range(ValueFactory.getValue(Lower), ValueFactory.getMaxValue(Lower)));
512a9ac8606Spatrick }
513ec727ea7Spatrick
514a9ac8606Spatrick return intersect(*What.Impl, DummyContainer);
515a9ac8606Spatrick }
516a9ac8606Spatrick
intersect(const RangeSet::ContainerType & LHS,const RangeSet::ContainerType & RHS)517a9ac8606Spatrick RangeSet RangeSet::Factory::intersect(const RangeSet::ContainerType &LHS,
518a9ac8606Spatrick const RangeSet::ContainerType &RHS) {
519a9ac8606Spatrick ContainerType Result;
520a9ac8606Spatrick Result.reserve(std::max(LHS.size(), RHS.size()));
521a9ac8606Spatrick
522a9ac8606Spatrick const_iterator First = LHS.begin(), Second = RHS.begin(),
523a9ac8606Spatrick FirstEnd = LHS.end(), SecondEnd = RHS.end();
524a9ac8606Spatrick
525a9ac8606Spatrick // If we ran out of ranges in one set, but not in the other,
526a9ac8606Spatrick // it means that those elements are definitely not in the
527a9ac8606Spatrick // intersection.
528a9ac8606Spatrick while (First != FirstEnd && Second != SecondEnd) {
529a9ac8606Spatrick // We want to keep the following invariant at all times:
530a9ac8606Spatrick //
531a9ac8606Spatrick // ----[ First ---------------------->
532a9ac8606Spatrick // --------[ Second ----------------->
533a9ac8606Spatrick if (Second->From() < First->From())
534*12c85518Srobert swapIterators(First, FirstEnd, Second, SecondEnd);
535a9ac8606Spatrick
536a9ac8606Spatrick // Loop where the invariant holds:
537a9ac8606Spatrick do {
538a9ac8606Spatrick // Check for the following situation:
539a9ac8606Spatrick //
540a9ac8606Spatrick // ----[ First ]--------------------->
541a9ac8606Spatrick // ---------------[ Second ]--------->
542a9ac8606Spatrick //
543a9ac8606Spatrick // which means that...
544a9ac8606Spatrick if (Second->From() > First->To()) {
545a9ac8606Spatrick // ...First is not in the intersection.
546a9ac8606Spatrick //
547a9ac8606Spatrick // We should move on to the next range after First and break out of the
548a9ac8606Spatrick // loop because the invariant might not be true.
549a9ac8606Spatrick ++First;
550a9ac8606Spatrick break;
551a9ac8606Spatrick }
552a9ac8606Spatrick
553a9ac8606Spatrick // We have a guaranteed intersection at this point!
554a9ac8606Spatrick // And this is the current situation:
555a9ac8606Spatrick //
556a9ac8606Spatrick // ----[ First ]----------------->
557a9ac8606Spatrick // -------[ Second ------------------>
558a9ac8606Spatrick //
559a9ac8606Spatrick // Additionally, it definitely starts with Second->From().
560a9ac8606Spatrick const llvm::APSInt &IntersectionStart = Second->From();
561a9ac8606Spatrick
562a9ac8606Spatrick // It is important to know which of the two ranges' ends
563a9ac8606Spatrick // is greater. That "longer" range might have some other
564a9ac8606Spatrick // intersections, while the "shorter" range might not.
565a9ac8606Spatrick if (Second->To() > First->To()) {
566a9ac8606Spatrick // Here we make a decision to keep First as the "longer"
567a9ac8606Spatrick // range.
568*12c85518Srobert swapIterators(First, FirstEnd, Second, SecondEnd);
569a9ac8606Spatrick }
570a9ac8606Spatrick
571a9ac8606Spatrick // At this point, we have the following situation:
572a9ac8606Spatrick //
573a9ac8606Spatrick // ---- First ]-------------------->
574a9ac8606Spatrick // ---- Second ]--[ Second+1 ---------->
575a9ac8606Spatrick //
576a9ac8606Spatrick // We don't know the relationship between First->From and
577a9ac8606Spatrick // Second->From and we don't know whether Second+1 intersects
578a9ac8606Spatrick // with First.
579a9ac8606Spatrick //
580a9ac8606Spatrick // However, we know that [IntersectionStart, Second->To] is
581a9ac8606Spatrick // a part of the intersection...
582a9ac8606Spatrick Result.push_back(Range(IntersectionStart, Second->To()));
583a9ac8606Spatrick ++Second;
584a9ac8606Spatrick // ...and that the invariant will hold for a valid Second+1
585a9ac8606Spatrick // because First->From <= Second->To < (Second+1)->From.
586a9ac8606Spatrick } while (Second != SecondEnd);
587a9ac8606Spatrick }
588a9ac8606Spatrick
589a9ac8606Spatrick if (Result.empty())
590a9ac8606Spatrick return getEmptySet();
591a9ac8606Spatrick
592a9ac8606Spatrick return makePersistent(std::move(Result));
593a9ac8606Spatrick }
594a9ac8606Spatrick
intersect(RangeSet LHS,RangeSet RHS)595a9ac8606Spatrick RangeSet RangeSet::Factory::intersect(RangeSet LHS, RangeSet RHS) {
596a9ac8606Spatrick // Shortcut: let's see if the intersection is even possible.
597a9ac8606Spatrick if (LHS.isEmpty() || RHS.isEmpty() || LHS.getMaxValue() < RHS.getMinValue() ||
598a9ac8606Spatrick RHS.getMaxValue() < LHS.getMinValue())
599a9ac8606Spatrick return getEmptySet();
600a9ac8606Spatrick
601a9ac8606Spatrick return intersect(*LHS.Impl, *RHS.Impl);
602a9ac8606Spatrick }
603a9ac8606Spatrick
intersect(RangeSet LHS,llvm::APSInt Point)604a9ac8606Spatrick RangeSet RangeSet::Factory::intersect(RangeSet LHS, llvm::APSInt Point) {
605a9ac8606Spatrick if (LHS.containsImpl(Point))
606a9ac8606Spatrick return getRangeSet(ValueFactory.getValue(Point));
607a9ac8606Spatrick
608a9ac8606Spatrick return getEmptySet();
609a9ac8606Spatrick }
610a9ac8606Spatrick
negate(RangeSet What)611a9ac8606Spatrick RangeSet RangeSet::Factory::negate(RangeSet What) {
612a9ac8606Spatrick if (What.isEmpty())
613a9ac8606Spatrick return getEmptySet();
614a9ac8606Spatrick
615a9ac8606Spatrick const llvm::APSInt SampleValue = What.getMinValue();
616a9ac8606Spatrick const llvm::APSInt &MIN = ValueFactory.getMinValue(SampleValue);
617a9ac8606Spatrick const llvm::APSInt &MAX = ValueFactory.getMaxValue(SampleValue);
618a9ac8606Spatrick
619a9ac8606Spatrick ContainerType Result;
620a9ac8606Spatrick Result.reserve(What.size() + (SampleValue == MIN));
621ec727ea7Spatrick
622ec727ea7Spatrick // Handle a special case for MIN value.
623a9ac8606Spatrick const_iterator It = What.begin();
624a9ac8606Spatrick const_iterator End = What.end();
625a9ac8606Spatrick
626a9ac8606Spatrick const llvm::APSInt &From = It->From();
627a9ac8606Spatrick const llvm::APSInt &To = It->To();
628a9ac8606Spatrick
629a9ac8606Spatrick if (From == MIN) {
630a9ac8606Spatrick // If the range [From, To] is [MIN, MAX], then result is also [MIN, MAX].
631a9ac8606Spatrick if (To == MAX) {
632a9ac8606Spatrick return What;
633a9ac8606Spatrick }
634a9ac8606Spatrick
635a9ac8606Spatrick const_iterator Last = std::prev(End);
636a9ac8606Spatrick
637a9ac8606Spatrick // Try to find and unite the following ranges:
638a9ac8606Spatrick // [MIN, MIN] & [MIN + 1, N] => [MIN, N].
639a9ac8606Spatrick if (Last->To() == MAX) {
640a9ac8606Spatrick // It means that in the original range we have ranges
641a9ac8606Spatrick // [MIN, A], ... , [B, MAX]
642a9ac8606Spatrick // And the result should be [MIN, -B], ..., [-A, MAX]
643a9ac8606Spatrick Result.emplace_back(MIN, ValueFactory.getValue(-Last->From()));
644a9ac8606Spatrick // We already negated Last, so we can skip it.
645a9ac8606Spatrick End = Last;
646ec727ea7Spatrick } else {
647a9ac8606Spatrick // Add a separate range for the lowest value.
648a9ac8606Spatrick Result.emplace_back(MIN, MIN);
649ec727ea7Spatrick }
650a9ac8606Spatrick
651a9ac8606Spatrick // Skip adding the second range in case when [From, To] are [MIN, MIN].
652a9ac8606Spatrick if (To != MIN) {
653a9ac8606Spatrick Result.emplace_back(ValueFactory.getValue(-To), MAX);
654ec727ea7Spatrick }
655a9ac8606Spatrick
656ec727ea7Spatrick // Skip the first range in the loop.
657a9ac8606Spatrick ++It;
658ec727ea7Spatrick }
659ec727ea7Spatrick
660ec727ea7Spatrick // Negate all other ranges.
661a9ac8606Spatrick for (; It != End; ++It) {
662ec727ea7Spatrick // Negate int values.
663a9ac8606Spatrick const llvm::APSInt &NewFrom = ValueFactory.getValue(-It->To());
664a9ac8606Spatrick const llvm::APSInt &NewTo = ValueFactory.getValue(-It->From());
665a9ac8606Spatrick
666ec727ea7Spatrick // Add a negated range.
667a9ac8606Spatrick Result.emplace_back(NewFrom, NewTo);
668e5dd7070Spatrick }
669ec727ea7Spatrick
670a9ac8606Spatrick llvm::sort(Result);
671a9ac8606Spatrick return makePersistent(std::move(Result));
672e5dd7070Spatrick }
673e5dd7070Spatrick
674*12c85518Srobert // Convert range set to the given integral type using truncation and promotion.
675*12c85518Srobert // This works similar to APSIntType::apply function but for the range set.
castTo(RangeSet What,APSIntType Ty)676*12c85518Srobert RangeSet RangeSet::Factory::castTo(RangeSet What, APSIntType Ty) {
677*12c85518Srobert // Set is empty or NOOP (aka cast to the same type).
678*12c85518Srobert if (What.isEmpty() || What.getAPSIntType() == Ty)
679*12c85518Srobert return What;
680*12c85518Srobert
681*12c85518Srobert const bool IsConversion = What.isUnsigned() != Ty.isUnsigned();
682*12c85518Srobert const bool IsTruncation = What.getBitWidth() > Ty.getBitWidth();
683*12c85518Srobert const bool IsPromotion = What.getBitWidth() < Ty.getBitWidth();
684*12c85518Srobert
685*12c85518Srobert if (IsTruncation)
686*12c85518Srobert return makePersistent(truncateTo(What, Ty));
687*12c85518Srobert
688*12c85518Srobert // Here we handle 2 cases:
689*12c85518Srobert // - IsConversion && !IsPromotion.
690*12c85518Srobert // In this case we handle changing a sign with same bitwidth: char -> uchar,
691*12c85518Srobert // uint -> int. Here we convert negatives to positives and positives which
692*12c85518Srobert // is out of range to negatives. We use convertTo function for that.
693*12c85518Srobert // - IsConversion && IsPromotion && !What.isUnsigned().
694*12c85518Srobert // In this case we handle changing a sign from signeds to unsigneds with
695*12c85518Srobert // higher bitwidth: char -> uint, int-> uint64. The point is that we also
696*12c85518Srobert // need convert negatives to positives and use convertTo function as well.
697*12c85518Srobert // For example, we don't need such a convertion when converting unsigned to
698*12c85518Srobert // signed with higher bitwidth, because all the values of unsigned is valid
699*12c85518Srobert // for the such signed.
700*12c85518Srobert if (IsConversion && (!IsPromotion || !What.isUnsigned()))
701*12c85518Srobert return makePersistent(convertTo(What, Ty));
702*12c85518Srobert
703*12c85518Srobert assert(IsPromotion && "Only promotion operation from unsigneds left.");
704*12c85518Srobert return makePersistent(promoteTo(What, Ty));
705*12c85518Srobert }
706*12c85518Srobert
castTo(RangeSet What,QualType T)707*12c85518Srobert RangeSet RangeSet::Factory::castTo(RangeSet What, QualType T) {
708*12c85518Srobert assert(T->isIntegralOrEnumerationType() && "T shall be an integral type.");
709*12c85518Srobert return castTo(What, ValueFactory.getAPSIntType(T));
710*12c85518Srobert }
711*12c85518Srobert
truncateTo(RangeSet What,APSIntType Ty)712*12c85518Srobert RangeSet::ContainerType RangeSet::Factory::truncateTo(RangeSet What,
713*12c85518Srobert APSIntType Ty) {
714*12c85518Srobert using llvm::APInt;
715*12c85518Srobert using llvm::APSInt;
716*12c85518Srobert ContainerType Result;
717*12c85518Srobert ContainerType Dummy;
718*12c85518Srobert // CastRangeSize is an amount of all possible values of cast type.
719*12c85518Srobert // Example: `char` has 256 values; `short` has 65536 values.
720*12c85518Srobert // But in fact we use `amount of values` - 1, because
721*12c85518Srobert // we can't keep `amount of values of UINT64` inside uint64_t.
722*12c85518Srobert // E.g. 256 is an amount of all possible values of `char` and we can't keep
723*12c85518Srobert // it inside `char`.
724*12c85518Srobert // And it's OK, it's enough to do correct calculations.
725*12c85518Srobert uint64_t CastRangeSize = APInt::getMaxValue(Ty.getBitWidth()).getZExtValue();
726*12c85518Srobert for (const Range &R : What) {
727*12c85518Srobert // Get bounds of the given range.
728*12c85518Srobert APSInt FromInt = R.From();
729*12c85518Srobert APSInt ToInt = R.To();
730*12c85518Srobert // CurrentRangeSize is an amount of all possible values of the current
731*12c85518Srobert // range minus one.
732*12c85518Srobert uint64_t CurrentRangeSize = (ToInt - FromInt).getZExtValue();
733*12c85518Srobert // This is an optimization for a specific case when this Range covers
734*12c85518Srobert // the whole range of the target type.
735*12c85518Srobert Dummy.clear();
736*12c85518Srobert if (CurrentRangeSize >= CastRangeSize) {
737*12c85518Srobert Dummy.emplace_back(ValueFactory.getMinValue(Ty),
738*12c85518Srobert ValueFactory.getMaxValue(Ty));
739*12c85518Srobert Result = std::move(Dummy);
740*12c85518Srobert break;
741*12c85518Srobert }
742*12c85518Srobert // Cast the bounds.
743*12c85518Srobert Ty.apply(FromInt);
744*12c85518Srobert Ty.apply(ToInt);
745*12c85518Srobert const APSInt &PersistentFrom = ValueFactory.getValue(FromInt);
746*12c85518Srobert const APSInt &PersistentTo = ValueFactory.getValue(ToInt);
747*12c85518Srobert if (FromInt > ToInt) {
748*12c85518Srobert Dummy.emplace_back(ValueFactory.getMinValue(Ty), PersistentTo);
749*12c85518Srobert Dummy.emplace_back(PersistentFrom, ValueFactory.getMaxValue(Ty));
750*12c85518Srobert } else
751*12c85518Srobert Dummy.emplace_back(PersistentFrom, PersistentTo);
752*12c85518Srobert // Every range retrieved after truncation potentialy has garbage values.
753*12c85518Srobert // So, we have to unite every next range with the previouses.
754*12c85518Srobert Result = unite(Result, Dummy);
755*12c85518Srobert }
756*12c85518Srobert
757*12c85518Srobert return Result;
758*12c85518Srobert }
759*12c85518Srobert
760*12c85518Srobert // Divide the convertion into two phases (presented as loops here).
761*12c85518Srobert // First phase(loop) works when casted values go in ascending order.
762*12c85518Srobert // E.g. char{1,3,5,127} -> uint{1,3,5,127}
763*12c85518Srobert // Interrupt the first phase and go to second one when casted values start
764*12c85518Srobert // go in descending order. That means that we crossed over the middle of
765*12c85518Srobert // the type value set (aka 0 for signeds and MAX/2+1 for unsigneds).
766*12c85518Srobert // For instance:
767*12c85518Srobert // 1: uchar{1,3,5,128,255} -> char{1,3,5,-128,-1}
768*12c85518Srobert // Here we put {1,3,5} to one array and {-128, -1} to another
769*12c85518Srobert // 2: char{-128,-127,-1,0,1,2} -> uchar{128,129,255,0,1,3}
770*12c85518Srobert // Here we put {128,129,255} to one array and {0,1,3} to another.
771*12c85518Srobert // After that we unite both arrays.
772*12c85518Srobert // NOTE: We don't just concatenate the arrays, because they may have
773*12c85518Srobert // adjacent ranges, e.g.:
774*12c85518Srobert // 1: char(-128, 127) -> uchar -> arr1(128, 255), arr2(0, 127) ->
775*12c85518Srobert // unite -> uchar(0, 255)
776*12c85518Srobert // 2: uchar(0, 1)U(254, 255) -> char -> arr1(0, 1), arr2(-2, -1) ->
777*12c85518Srobert // unite -> uchar(-2, 1)
convertTo(RangeSet What,APSIntType Ty)778*12c85518Srobert RangeSet::ContainerType RangeSet::Factory::convertTo(RangeSet What,
779*12c85518Srobert APSIntType Ty) {
780*12c85518Srobert using llvm::APInt;
781*12c85518Srobert using llvm::APSInt;
782*12c85518Srobert using Bounds = std::pair<const APSInt &, const APSInt &>;
783*12c85518Srobert ContainerType AscendArray;
784*12c85518Srobert ContainerType DescendArray;
785*12c85518Srobert auto CastRange = [Ty, &VF = ValueFactory](const Range &R) -> Bounds {
786*12c85518Srobert // Get bounds of the given range.
787*12c85518Srobert APSInt FromInt = R.From();
788*12c85518Srobert APSInt ToInt = R.To();
789*12c85518Srobert // Cast the bounds.
790*12c85518Srobert Ty.apply(FromInt);
791*12c85518Srobert Ty.apply(ToInt);
792*12c85518Srobert return {VF.getValue(FromInt), VF.getValue(ToInt)};
793*12c85518Srobert };
794*12c85518Srobert // Phase 1. Fill the first array.
795*12c85518Srobert APSInt LastConvertedInt = Ty.getMinValue();
796*12c85518Srobert const auto *It = What.begin();
797*12c85518Srobert const auto *E = What.end();
798*12c85518Srobert while (It != E) {
799*12c85518Srobert Bounds NewBounds = CastRange(*(It++));
800*12c85518Srobert // If values stop going acsending order, go to the second phase(loop).
801*12c85518Srobert if (NewBounds.first < LastConvertedInt) {
802*12c85518Srobert DescendArray.emplace_back(NewBounds.first, NewBounds.second);
803*12c85518Srobert break;
804*12c85518Srobert }
805*12c85518Srobert // If the range contains a midpoint, then split the range.
806*12c85518Srobert // E.g. char(-5, 5) -> uchar(251, 5)
807*12c85518Srobert // Here we shall add a range (251, 255) to the first array and (0, 5) to the
808*12c85518Srobert // second one.
809*12c85518Srobert if (NewBounds.first > NewBounds.second) {
810*12c85518Srobert DescendArray.emplace_back(ValueFactory.getMinValue(Ty), NewBounds.second);
811*12c85518Srobert AscendArray.emplace_back(NewBounds.first, ValueFactory.getMaxValue(Ty));
812*12c85518Srobert } else
813*12c85518Srobert // Values are going acsending order.
814*12c85518Srobert AscendArray.emplace_back(NewBounds.first, NewBounds.second);
815*12c85518Srobert LastConvertedInt = NewBounds.first;
816*12c85518Srobert }
817*12c85518Srobert // Phase 2. Fill the second array.
818*12c85518Srobert while (It != E) {
819*12c85518Srobert Bounds NewBounds = CastRange(*(It++));
820*12c85518Srobert DescendArray.emplace_back(NewBounds.first, NewBounds.second);
821*12c85518Srobert }
822*12c85518Srobert // Unite both arrays.
823*12c85518Srobert return unite(AscendArray, DescendArray);
824*12c85518Srobert }
825*12c85518Srobert
826*12c85518Srobert /// Promotion from unsigneds to signeds/unsigneds left.
promoteTo(RangeSet What,APSIntType Ty)827*12c85518Srobert RangeSet::ContainerType RangeSet::Factory::promoteTo(RangeSet What,
828*12c85518Srobert APSIntType Ty) {
829*12c85518Srobert ContainerType Result;
830*12c85518Srobert // We definitely know the size of the result set.
831*12c85518Srobert Result.reserve(What.size());
832*12c85518Srobert
833*12c85518Srobert // Each unsigned value fits every larger type without any changes,
834*12c85518Srobert // whether the larger type is signed or unsigned. So just promote and push
835*12c85518Srobert // back each range one by one.
836*12c85518Srobert for (const Range &R : What) {
837*12c85518Srobert // Get bounds of the given range.
838*12c85518Srobert llvm::APSInt FromInt = R.From();
839*12c85518Srobert llvm::APSInt ToInt = R.To();
840*12c85518Srobert // Cast the bounds.
841*12c85518Srobert Ty.apply(FromInt);
842*12c85518Srobert Ty.apply(ToInt);
843*12c85518Srobert Result.emplace_back(ValueFactory.getValue(FromInt),
844*12c85518Srobert ValueFactory.getValue(ToInt));
845*12c85518Srobert }
846*12c85518Srobert return Result;
847*12c85518Srobert }
848*12c85518Srobert
deletePoint(RangeSet From,const llvm::APSInt & Point)849a9ac8606Spatrick RangeSet RangeSet::Factory::deletePoint(RangeSet From,
850a9ac8606Spatrick const llvm::APSInt &Point) {
851a9ac8606Spatrick if (!From.contains(Point))
852a9ac8606Spatrick return From;
853a9ac8606Spatrick
854a9ac8606Spatrick llvm::APSInt Upper = Point;
855a9ac8606Spatrick llvm::APSInt Lower = Point;
856a9ac8606Spatrick
857a9ac8606Spatrick ++Upper;
858a9ac8606Spatrick --Lower;
859a9ac8606Spatrick
860a9ac8606Spatrick // Notice that the lower bound is greater than the upper bound.
861a9ac8606Spatrick return intersect(From, Upper, Lower);
862e5dd7070Spatrick }
863e5dd7070Spatrick
dump(raw_ostream & OS) const864*12c85518Srobert LLVM_DUMP_METHOD void Range::dump(raw_ostream &OS) const {
865a9ac8606Spatrick OS << '[' << toString(From(), 10) << ", " << toString(To(), 10) << ']';
866a9ac8606Spatrick }
dump() const867*12c85518Srobert LLVM_DUMP_METHOD void Range::dump() const { dump(llvm::errs()); }
868e5dd7070Spatrick
dump(raw_ostream & OS) const869*12c85518Srobert LLVM_DUMP_METHOD void RangeSet::dump(raw_ostream &OS) const {
870a9ac8606Spatrick OS << "{ ";
871a9ac8606Spatrick llvm::interleaveComma(*this, OS, [&OS](const Range &R) { R.dump(OS); });
872a9ac8606Spatrick OS << " }";
873e5dd7070Spatrick }
dump() const874*12c85518Srobert LLVM_DUMP_METHOD void RangeSet::dump() const { dump(llvm::errs()); }
875a9ac8606Spatrick
876a9ac8606Spatrick REGISTER_SET_FACTORY_WITH_PROGRAMSTATE(SymbolSet, SymbolRef)
877e5dd7070Spatrick
878e5dd7070Spatrick namespace {
879a9ac8606Spatrick class EquivalenceClass;
880a9ac8606Spatrick } // end anonymous namespace
881a9ac8606Spatrick
882a9ac8606Spatrick REGISTER_MAP_WITH_PROGRAMSTATE(ClassMap, SymbolRef, EquivalenceClass)
883a9ac8606Spatrick REGISTER_MAP_WITH_PROGRAMSTATE(ClassMembers, EquivalenceClass, SymbolSet)
884a9ac8606Spatrick REGISTER_MAP_WITH_PROGRAMSTATE(ConstraintRange, EquivalenceClass, RangeSet)
885a9ac8606Spatrick
886a9ac8606Spatrick REGISTER_SET_FACTORY_WITH_PROGRAMSTATE(ClassSet, EquivalenceClass)
887a9ac8606Spatrick REGISTER_MAP_WITH_PROGRAMSTATE(DisequalityMap, EquivalenceClass, ClassSet)
888a9ac8606Spatrick
889a9ac8606Spatrick namespace {
890a9ac8606Spatrick /// This class encapsulates a set of symbols equal to each other.
891a9ac8606Spatrick ///
892a9ac8606Spatrick /// The main idea of the approach requiring such classes is in narrowing
893a9ac8606Spatrick /// and sharing constraints between symbols within the class. Also we can
894a9ac8606Spatrick /// conclude that there is no practical need in storing constraints for
895a9ac8606Spatrick /// every member of the class separately.
896a9ac8606Spatrick ///
897a9ac8606Spatrick /// Main terminology:
898a9ac8606Spatrick ///
899a9ac8606Spatrick /// * "Equivalence class" is an object of this class, which can be efficiently
900a9ac8606Spatrick /// compared to other classes. It represents the whole class without
901a9ac8606Spatrick /// storing the actual in it. The members of the class however can be
902a9ac8606Spatrick /// retrieved from the state.
903a9ac8606Spatrick ///
904a9ac8606Spatrick /// * "Class members" are the symbols corresponding to the class. This means
905a9ac8606Spatrick /// that A == B for every member symbols A and B from the class. Members of
906a9ac8606Spatrick /// each class are stored in the state.
907a9ac8606Spatrick ///
908a9ac8606Spatrick /// * "Trivial class" is a class that has and ever had only one same symbol.
909a9ac8606Spatrick ///
910a9ac8606Spatrick /// * "Merge operation" merges two classes into one. It is the main operation
911a9ac8606Spatrick /// to produce non-trivial classes.
912a9ac8606Spatrick /// If, at some point, we can assume that two symbols from two distinct
913a9ac8606Spatrick /// classes are equal, we can merge these classes.
914a9ac8606Spatrick class EquivalenceClass : public llvm::FoldingSetNode {
915a9ac8606Spatrick public:
916a9ac8606Spatrick /// Find equivalence class for the given symbol in the given state.
917*12c85518Srobert [[nodiscard]] static inline EquivalenceClass find(ProgramStateRef State,
918a9ac8606Spatrick SymbolRef Sym);
919a9ac8606Spatrick
920a9ac8606Spatrick /// Merge classes for the given symbols and return a new state.
921*12c85518Srobert [[nodiscard]] static inline ProgramStateRef merge(RangeSet::Factory &F,
922a9ac8606Spatrick ProgramStateRef State,
923a9ac8606Spatrick SymbolRef First,
924a9ac8606Spatrick SymbolRef Second);
925a9ac8606Spatrick // Merge this class with the given class and return a new state.
926*12c85518Srobert [[nodiscard]] inline ProgramStateRef
927a9ac8606Spatrick merge(RangeSet::Factory &F, ProgramStateRef State, EquivalenceClass Other);
928a9ac8606Spatrick
929a9ac8606Spatrick /// Return a set of class members for the given state.
930*12c85518Srobert [[nodiscard]] inline SymbolSet getClassMembers(ProgramStateRef State) const;
931a9ac8606Spatrick
932a9ac8606Spatrick /// Return true if the current class is trivial in the given state.
933a9ac8606Spatrick /// A class is trivial if and only if there is not any member relations stored
934a9ac8606Spatrick /// to it in State/ClassMembers.
935a9ac8606Spatrick /// An equivalence class with one member might seem as it does not hold any
936a9ac8606Spatrick /// meaningful information, i.e. that is a tautology. However, during the
937a9ac8606Spatrick /// removal of dead symbols we do not remove classes with one member for
938a9ac8606Spatrick /// resource and performance reasons. Consequently, a class with one member is
939a9ac8606Spatrick /// not necessarily trivial. It could happen that we have a class with two
940a9ac8606Spatrick /// members and then during the removal of dead symbols we remove one of its
941a9ac8606Spatrick /// members. In this case, the class is still non-trivial (it still has the
942a9ac8606Spatrick /// mappings in ClassMembers), even though it has only one member.
943*12c85518Srobert [[nodiscard]] inline bool isTrivial(ProgramStateRef State) const;
944a9ac8606Spatrick
945a9ac8606Spatrick /// Return true if the current class is trivial and its only member is dead.
946*12c85518Srobert [[nodiscard]] inline bool isTriviallyDead(ProgramStateRef State,
947a9ac8606Spatrick SymbolReaper &Reaper) const;
948a9ac8606Spatrick
949*12c85518Srobert [[nodiscard]] static inline ProgramStateRef
950a9ac8606Spatrick markDisequal(RangeSet::Factory &F, ProgramStateRef State, SymbolRef First,
951a9ac8606Spatrick SymbolRef Second);
952*12c85518Srobert [[nodiscard]] static inline ProgramStateRef
953a9ac8606Spatrick markDisequal(RangeSet::Factory &F, ProgramStateRef State,
954a9ac8606Spatrick EquivalenceClass First, EquivalenceClass Second);
955*12c85518Srobert [[nodiscard]] inline ProgramStateRef
956a9ac8606Spatrick markDisequal(RangeSet::Factory &F, ProgramStateRef State,
957a9ac8606Spatrick EquivalenceClass Other) const;
958*12c85518Srobert [[nodiscard]] static inline ClassSet getDisequalClasses(ProgramStateRef State,
959*12c85518Srobert SymbolRef Sym);
960*12c85518Srobert [[nodiscard]] inline ClassSet getDisequalClasses(ProgramStateRef State) const;
961*12c85518Srobert [[nodiscard]] inline ClassSet
962a9ac8606Spatrick getDisequalClasses(DisequalityMapTy Map, ClassSet::Factory &Factory) const;
963a9ac8606Spatrick
964*12c85518Srobert [[nodiscard]] static inline std::optional<bool>
965*12c85518Srobert areEqual(ProgramStateRef State, EquivalenceClass First,
966a9ac8606Spatrick EquivalenceClass Second);
967*12c85518Srobert [[nodiscard]] static inline std::optional<bool>
968a9ac8606Spatrick areEqual(ProgramStateRef State, SymbolRef First, SymbolRef Second);
969a9ac8606Spatrick
970*12c85518Srobert /// Remove one member from the class.
971*12c85518Srobert [[nodiscard]] ProgramStateRef removeMember(ProgramStateRef State,
972*12c85518Srobert const SymbolRef Old);
973*12c85518Srobert
974a9ac8606Spatrick /// Iterate over all symbols and try to simplify them.
975*12c85518Srobert [[nodiscard]] static inline ProgramStateRef simplify(SValBuilder &SVB,
976a9ac8606Spatrick RangeSet::Factory &F,
977a9ac8606Spatrick ProgramStateRef State,
978a9ac8606Spatrick EquivalenceClass Class);
979a9ac8606Spatrick
980a9ac8606Spatrick void dumpToStream(ProgramStateRef State, raw_ostream &os) const;
dump(ProgramStateRef State) const981a9ac8606Spatrick LLVM_DUMP_METHOD void dump(ProgramStateRef State) const {
982a9ac8606Spatrick dumpToStream(State, llvm::errs());
983a9ac8606Spatrick }
984a9ac8606Spatrick
985a9ac8606Spatrick /// Check equivalence data for consistency.
986*12c85518Srobert [[nodiscard]] LLVM_ATTRIBUTE_UNUSED static bool
987a9ac8606Spatrick isClassDataConsistent(ProgramStateRef State);
988a9ac8606Spatrick
getType() const989*12c85518Srobert [[nodiscard]] QualType getType() const {
990a9ac8606Spatrick return getRepresentativeSymbol()->getType();
991a9ac8606Spatrick }
992a9ac8606Spatrick
993a9ac8606Spatrick EquivalenceClass() = delete;
994a9ac8606Spatrick EquivalenceClass(const EquivalenceClass &) = default;
995a9ac8606Spatrick EquivalenceClass &operator=(const EquivalenceClass &) = delete;
996a9ac8606Spatrick EquivalenceClass(EquivalenceClass &&) = default;
997a9ac8606Spatrick EquivalenceClass &operator=(EquivalenceClass &&) = delete;
998a9ac8606Spatrick
operator ==(const EquivalenceClass & Other) const999a9ac8606Spatrick bool operator==(const EquivalenceClass &Other) const {
1000a9ac8606Spatrick return ID == Other.ID;
1001a9ac8606Spatrick }
operator <(const EquivalenceClass & Other) const1002a9ac8606Spatrick bool operator<(const EquivalenceClass &Other) const { return ID < Other.ID; }
operator !=(const EquivalenceClass & Other) const1003a9ac8606Spatrick bool operator!=(const EquivalenceClass &Other) const {
1004a9ac8606Spatrick return !operator==(Other);
1005a9ac8606Spatrick }
1006a9ac8606Spatrick
Profile(llvm::FoldingSetNodeID & ID,uintptr_t CID)1007a9ac8606Spatrick static void Profile(llvm::FoldingSetNodeID &ID, uintptr_t CID) {
1008a9ac8606Spatrick ID.AddInteger(CID);
1009a9ac8606Spatrick }
1010a9ac8606Spatrick
Profile(llvm::FoldingSetNodeID & ID) const1011a9ac8606Spatrick void Profile(llvm::FoldingSetNodeID &ID) const { Profile(ID, this->ID); }
1012a9ac8606Spatrick
1013a9ac8606Spatrick private:
EquivalenceClass(SymbolRef Sym)1014a9ac8606Spatrick /* implicit */ EquivalenceClass(SymbolRef Sym)
1015a9ac8606Spatrick : ID(reinterpret_cast<uintptr_t>(Sym)) {}
1016a9ac8606Spatrick
1017a9ac8606Spatrick /// This function is intended to be used ONLY within the class.
1018a9ac8606Spatrick /// The fact that ID is a pointer to a symbol is an implementation detail
1019a9ac8606Spatrick /// and should stay that way.
1020a9ac8606Spatrick /// In the current implementation, we use it to retrieve the only member
1021a9ac8606Spatrick /// of the trivial class.
getRepresentativeSymbol() const1022a9ac8606Spatrick SymbolRef getRepresentativeSymbol() const {
1023a9ac8606Spatrick return reinterpret_cast<SymbolRef>(ID);
1024a9ac8606Spatrick }
1025a9ac8606Spatrick static inline SymbolSet::Factory &getMembersFactory(ProgramStateRef State);
1026a9ac8606Spatrick
1027a9ac8606Spatrick inline ProgramStateRef mergeImpl(RangeSet::Factory &F, ProgramStateRef State,
1028a9ac8606Spatrick SymbolSet Members, EquivalenceClass Other,
1029a9ac8606Spatrick SymbolSet OtherMembers);
1030*12c85518Srobert
1031a9ac8606Spatrick static inline bool
1032a9ac8606Spatrick addToDisequalityInfo(DisequalityMapTy &Info, ConstraintRangeTy &Constraints,
1033a9ac8606Spatrick RangeSet::Factory &F, ProgramStateRef State,
1034a9ac8606Spatrick EquivalenceClass First, EquivalenceClass Second);
1035a9ac8606Spatrick
1036a9ac8606Spatrick /// This is a unique identifier of the class.
1037a9ac8606Spatrick uintptr_t ID;
1038a9ac8606Spatrick };
1039a9ac8606Spatrick
1040a9ac8606Spatrick //===----------------------------------------------------------------------===//
1041a9ac8606Spatrick // Constraint functions
1042a9ac8606Spatrick //===----------------------------------------------------------------------===//
1043a9ac8606Spatrick
1044*12c85518Srobert [[nodiscard]] LLVM_ATTRIBUTE_UNUSED bool
areFeasible(ConstraintRangeTy Constraints)1045a9ac8606Spatrick areFeasible(ConstraintRangeTy Constraints) {
1046a9ac8606Spatrick return llvm::none_of(
1047a9ac8606Spatrick Constraints,
1048a9ac8606Spatrick [](const std::pair<EquivalenceClass, RangeSet> &ClassConstraint) {
1049a9ac8606Spatrick return ClassConstraint.second.isEmpty();
1050a9ac8606Spatrick });
1051a9ac8606Spatrick }
1052a9ac8606Spatrick
getConstraint(ProgramStateRef State,EquivalenceClass Class)1053*12c85518Srobert [[nodiscard]] inline const RangeSet *getConstraint(ProgramStateRef State,
1054a9ac8606Spatrick EquivalenceClass Class) {
1055a9ac8606Spatrick return State->get<ConstraintRange>(Class);
1056a9ac8606Spatrick }
1057a9ac8606Spatrick
getConstraint(ProgramStateRef State,SymbolRef Sym)1058*12c85518Srobert [[nodiscard]] inline const RangeSet *getConstraint(ProgramStateRef State,
1059a9ac8606Spatrick SymbolRef Sym) {
1060a9ac8606Spatrick return getConstraint(State, EquivalenceClass::find(State, Sym));
1061a9ac8606Spatrick }
1062a9ac8606Spatrick
setConstraint(ProgramStateRef State,EquivalenceClass Class,RangeSet Constraint)1063*12c85518Srobert [[nodiscard]] ProgramStateRef setConstraint(ProgramStateRef State,
1064a9ac8606Spatrick EquivalenceClass Class,
1065a9ac8606Spatrick RangeSet Constraint) {
1066a9ac8606Spatrick return State->set<ConstraintRange>(Class, Constraint);
1067a9ac8606Spatrick }
1068a9ac8606Spatrick
setConstraints(ProgramStateRef State,ConstraintRangeTy Constraints)1069*12c85518Srobert [[nodiscard]] ProgramStateRef setConstraints(ProgramStateRef State,
1070a9ac8606Spatrick ConstraintRangeTy Constraints) {
1071a9ac8606Spatrick return State->set<ConstraintRange>(Constraints);
1072a9ac8606Spatrick }
1073a9ac8606Spatrick
1074a9ac8606Spatrick //===----------------------------------------------------------------------===//
1075a9ac8606Spatrick // Equality/diseqiality abstraction
1076a9ac8606Spatrick //===----------------------------------------------------------------------===//
1077a9ac8606Spatrick
1078a9ac8606Spatrick /// A small helper function for detecting symbolic (dis)equality.
1079a9ac8606Spatrick ///
1080a9ac8606Spatrick /// Equality check can have different forms (like a == b or a - b) and this
1081a9ac8606Spatrick /// class encapsulates those away if the only thing the user wants to check -
1082a9ac8606Spatrick /// whether it's equality/diseqiality or not.
1083a9ac8606Spatrick ///
1084a9ac8606Spatrick /// \returns true if assuming this Sym to be true means equality of operands
1085a9ac8606Spatrick /// false if it means disequality of operands
1086a9ac8606Spatrick /// None otherwise
meansEquality(const SymSymExpr * Sym)1087*12c85518Srobert std::optional<bool> meansEquality(const SymSymExpr *Sym) {
1088a9ac8606Spatrick switch (Sym->getOpcode()) {
1089a9ac8606Spatrick case BO_Sub:
1090a9ac8606Spatrick // This case is: A - B != 0 -> disequality check.
1091a9ac8606Spatrick return false;
1092a9ac8606Spatrick case BO_EQ:
1093a9ac8606Spatrick // This case is: A == B != 0 -> equality check.
1094a9ac8606Spatrick return true;
1095a9ac8606Spatrick case BO_NE:
1096a9ac8606Spatrick // This case is: A != B != 0 -> diseqiality check.
1097a9ac8606Spatrick return false;
1098a9ac8606Spatrick default:
1099*12c85518Srobert return std::nullopt;
1100a9ac8606Spatrick }
1101a9ac8606Spatrick }
1102a9ac8606Spatrick
1103a9ac8606Spatrick //===----------------------------------------------------------------------===//
1104a9ac8606Spatrick // Intersection functions
1105a9ac8606Spatrick //===----------------------------------------------------------------------===//
1106a9ac8606Spatrick
1107a9ac8606Spatrick template <class SecondTy, class... RestTy>
1108*12c85518Srobert [[nodiscard]] inline RangeSet intersect(RangeSet::Factory &F, RangeSet Head,
1109a9ac8606Spatrick SecondTy Second, RestTy... Tail);
1110a9ac8606Spatrick
1111a9ac8606Spatrick template <class... RangeTy> struct IntersectionTraits;
1112a9ac8606Spatrick
1113a9ac8606Spatrick template <class... TailTy> struct IntersectionTraits<RangeSet, TailTy...> {
1114a9ac8606Spatrick // Found RangeSet, no need to check any further
1115a9ac8606Spatrick using Type = RangeSet;
1116a9ac8606Spatrick };
1117a9ac8606Spatrick
1118a9ac8606Spatrick template <> struct IntersectionTraits<> {
1119a9ac8606Spatrick // We ran out of types, and we didn't find any RangeSet, so the result should
1120a9ac8606Spatrick // be optional.
1121*12c85518Srobert using Type = std::optional<RangeSet>;
1122a9ac8606Spatrick };
1123a9ac8606Spatrick
1124a9ac8606Spatrick template <class OptionalOrPointer, class... TailTy>
1125a9ac8606Spatrick struct IntersectionTraits<OptionalOrPointer, TailTy...> {
1126a9ac8606Spatrick // If current type is Optional or a raw pointer, we should keep looking.
1127a9ac8606Spatrick using Type = typename IntersectionTraits<TailTy...>::Type;
1128a9ac8606Spatrick };
1129a9ac8606Spatrick
1130a9ac8606Spatrick template <class EndTy>
intersect(RangeSet::Factory & F,EndTy End)1131*12c85518Srobert [[nodiscard]] inline EndTy intersect(RangeSet::Factory &F, EndTy End) {
1132*12c85518Srobert // If the list contains only RangeSet or std::optional<RangeSet>, simply
1133*12c85518Srobert // return that range set.
1134a9ac8606Spatrick return End;
1135a9ac8606Spatrick }
1136a9ac8606Spatrick
1137*12c85518Srobert [[nodiscard]] LLVM_ATTRIBUTE_UNUSED inline std::optional<RangeSet>
intersect(RangeSet::Factory & F,const RangeSet * End)1138a9ac8606Spatrick intersect(RangeSet::Factory &F, const RangeSet *End) {
1139*12c85518Srobert // This is an extraneous conversion from a raw pointer into
1140*12c85518Srobert // std::optional<RangeSet>
1141a9ac8606Spatrick if (End) {
1142a9ac8606Spatrick return *End;
1143a9ac8606Spatrick }
1144*12c85518Srobert return std::nullopt;
1145a9ac8606Spatrick }
1146a9ac8606Spatrick
1147a9ac8606Spatrick template <class... RestTy>
intersect(RangeSet::Factory & F,RangeSet Head,RangeSet Second,RestTy...Tail)1148*12c85518Srobert [[nodiscard]] inline RangeSet intersect(RangeSet::Factory &F, RangeSet Head,
1149a9ac8606Spatrick RangeSet Second, RestTy... Tail) {
1150a9ac8606Spatrick // Here we call either the <RangeSet,RangeSet,...> or <RangeSet,...> version
1151a9ac8606Spatrick // of the function and can be sure that the result is RangeSet.
1152a9ac8606Spatrick return intersect(F, F.intersect(Head, Second), Tail...);
1153a9ac8606Spatrick }
1154a9ac8606Spatrick
1155a9ac8606Spatrick template <class SecondTy, class... RestTy>
intersect(RangeSet::Factory & F,RangeSet Head,SecondTy Second,RestTy...Tail)1156*12c85518Srobert [[nodiscard]] inline RangeSet intersect(RangeSet::Factory &F, RangeSet Head,
1157a9ac8606Spatrick SecondTy Second, RestTy... Tail) {
1158a9ac8606Spatrick if (Second) {
1159a9ac8606Spatrick // Here we call the <RangeSet,RangeSet,...> version of the function...
1160a9ac8606Spatrick return intersect(F, Head, *Second, Tail...);
1161a9ac8606Spatrick }
1162a9ac8606Spatrick // ...and here it is either <RangeSet,RangeSet,...> or <RangeSet,...>, which
1163a9ac8606Spatrick // means that the result is definitely RangeSet.
1164a9ac8606Spatrick return intersect(F, Head, Tail...);
1165a9ac8606Spatrick }
1166a9ac8606Spatrick
1167a9ac8606Spatrick /// Main generic intersect function.
1168a9ac8606Spatrick /// It intersects all of the given range sets. If some of the given arguments
1169*12c85518Srobert /// don't hold a range set (nullptr or std::nullopt), the function will skip
1170*12c85518Srobert /// them.
1171a9ac8606Spatrick ///
1172a9ac8606Spatrick /// Available representations for the arguments are:
1173a9ac8606Spatrick /// * RangeSet
1174*12c85518Srobert /// * std::optional<RangeSet>
1175a9ac8606Spatrick /// * RangeSet *
1176a9ac8606Spatrick /// Pointer to a RangeSet is automatically assumed to be nullable and will get
1177a9ac8606Spatrick /// checked as well as the optional version. If this behaviour is undesired,
1178a9ac8606Spatrick /// please dereference the pointer in the call.
1179a9ac8606Spatrick ///
1180a9ac8606Spatrick /// Return type depends on the arguments' types. If we can be sure in compile
1181a9ac8606Spatrick /// time that there will be a range set as a result, the returning type is
1182*12c85518Srobert /// simply RangeSet, in other cases we have to back off to
1183*12c85518Srobert /// std::optional<RangeSet>.
1184a9ac8606Spatrick ///
1185a9ac8606Spatrick /// Please, prefer optional range sets to raw pointers. If the last argument is
1186*12c85518Srobert /// a raw pointer and all previous arguments are std::nullopt, it will cost one
1187*12c85518Srobert /// additional check to convert RangeSet * into std::optional<RangeSet>.
1188a9ac8606Spatrick template <class HeadTy, class SecondTy, class... RestTy>
1189*12c85518Srobert [[nodiscard]] inline
1190a9ac8606Spatrick typename IntersectionTraits<HeadTy, SecondTy, RestTy...>::Type
intersect(RangeSet::Factory & F,HeadTy Head,SecondTy Second,RestTy...Tail)1191a9ac8606Spatrick intersect(RangeSet::Factory &F, HeadTy Head, SecondTy Second,
1192a9ac8606Spatrick RestTy... Tail) {
1193a9ac8606Spatrick if (Head) {
1194a9ac8606Spatrick return intersect(F, *Head, Second, Tail...);
1195a9ac8606Spatrick }
1196a9ac8606Spatrick return intersect(F, Second, Tail...);
1197a9ac8606Spatrick }
1198a9ac8606Spatrick
1199a9ac8606Spatrick //===----------------------------------------------------------------------===//
1200a9ac8606Spatrick // Symbolic reasoning logic
1201a9ac8606Spatrick //===----------------------------------------------------------------------===//
1202ec727ea7Spatrick
1203ec727ea7Spatrick /// A little component aggregating all of the reasoning we have about
1204ec727ea7Spatrick /// the ranges of symbolic expressions.
1205ec727ea7Spatrick ///
1206ec727ea7Spatrick /// Even when we don't know the exact values of the operands, we still
1207ec727ea7Spatrick /// can get a pretty good estimate of the result's range.
1208ec727ea7Spatrick class SymbolicRangeInferrer
1209ec727ea7Spatrick : public SymExprVisitor<SymbolicRangeInferrer, RangeSet> {
1210ec727ea7Spatrick public:
1211a9ac8606Spatrick template <class SourceType>
inferRange(RangeSet::Factory & F,ProgramStateRef State,SourceType Origin)1212a9ac8606Spatrick static RangeSet inferRange(RangeSet::Factory &F, ProgramStateRef State,
1213a9ac8606Spatrick SourceType Origin) {
1214a9ac8606Spatrick SymbolicRangeInferrer Inferrer(F, State);
1215a9ac8606Spatrick return Inferrer.infer(Origin);
1216ec727ea7Spatrick }
1217ec727ea7Spatrick
VisitSymExpr(SymbolRef Sym)1218ec727ea7Spatrick RangeSet VisitSymExpr(SymbolRef Sym) {
1219*12c85518Srobert if (std::optional<RangeSet> RS = getRangeForNegatedSym(Sym))
1220*12c85518Srobert return *RS;
1221*12c85518Srobert // If we've reached this line, the actual type of the symbolic
1222ec727ea7Spatrick // expression is not supported for advanced inference.
1223ec727ea7Spatrick // In this case, we simply backoff to the default "let's simply
1224ec727ea7Spatrick // infer the range from the expression's type".
1225ec727ea7Spatrick return infer(Sym->getType());
1226ec727ea7Spatrick }
1227ec727ea7Spatrick
VisitUnarySymExpr(const UnarySymExpr * USE)1228*12c85518Srobert RangeSet VisitUnarySymExpr(const UnarySymExpr *USE) {
1229*12c85518Srobert if (std::optional<RangeSet> RS = getRangeForNegatedUnarySym(USE))
1230*12c85518Srobert return *RS;
1231*12c85518Srobert return infer(USE->getType());
1232*12c85518Srobert }
1233*12c85518Srobert
VisitSymIntExpr(const SymIntExpr * Sym)1234ec727ea7Spatrick RangeSet VisitSymIntExpr(const SymIntExpr *Sym) {
1235ec727ea7Spatrick return VisitBinaryOperator(Sym);
1236ec727ea7Spatrick }
1237ec727ea7Spatrick
VisitIntSymExpr(const IntSymExpr * Sym)1238ec727ea7Spatrick RangeSet VisitIntSymExpr(const IntSymExpr *Sym) {
1239ec727ea7Spatrick return VisitBinaryOperator(Sym);
1240ec727ea7Spatrick }
1241ec727ea7Spatrick
VisitSymSymExpr(const SymSymExpr * SSE)1242*12c85518Srobert RangeSet VisitSymSymExpr(const SymSymExpr *SSE) {
1243a9ac8606Spatrick return intersect(
1244a9ac8606Spatrick RangeFactory,
1245*12c85518Srobert // If Sym is a difference of symbols A - B, then maybe we have range
1246*12c85518Srobert // set stored for B - A.
1247*12c85518Srobert //
1248*12c85518Srobert // If we have range set stored for both A - B and B - A then
1249*12c85518Srobert // calculate the effective range set by intersecting the range set
1250*12c85518Srobert // for A - B and the negated range set of B - A.
1251*12c85518Srobert getRangeForNegatedSymSym(SSE),
1252*12c85518Srobert // If Sym is a comparison expression (except <=>),
1253*12c85518Srobert // find any other comparisons with the same operands.
1254*12c85518Srobert // See function description.
1255*12c85518Srobert getRangeForComparisonSymbol(SSE),
1256a9ac8606Spatrick // If Sym is (dis)equality, we might have some information
1257a9ac8606Spatrick // on that in our equality classes data structure.
1258*12c85518Srobert getRangeForEqualities(SSE),
1259a9ac8606Spatrick // And we should always check what we can get from the operands.
1260*12c85518Srobert VisitBinaryOperator(SSE));
1261ec727ea7Spatrick }
1262ec727ea7Spatrick
1263ec727ea7Spatrick private:
SymbolicRangeInferrer(RangeSet::Factory & F,ProgramStateRef S)1264a9ac8606Spatrick SymbolicRangeInferrer(RangeSet::Factory &F, ProgramStateRef S)
1265a9ac8606Spatrick : ValueFactory(F.getValueFactory()), RangeFactory(F), State(S) {}
1266ec727ea7Spatrick
1267ec727ea7Spatrick /// Infer range information from the given integer constant.
1268ec727ea7Spatrick ///
1269ec727ea7Spatrick /// It's not a real "inference", but is here for operating with
1270ec727ea7Spatrick /// sub-expressions in a more polymorphic manner.
inferAs(const llvm::APSInt & Val,QualType)1271ec727ea7Spatrick RangeSet inferAs(const llvm::APSInt &Val, QualType) {
1272ec727ea7Spatrick return {RangeFactory, Val};
1273ec727ea7Spatrick }
1274ec727ea7Spatrick
1275ec727ea7Spatrick /// Infer range information from symbol in the context of the given type.
inferAs(SymbolRef Sym,QualType DestType)1276ec727ea7Spatrick RangeSet inferAs(SymbolRef Sym, QualType DestType) {
1277ec727ea7Spatrick QualType ActualType = Sym->getType();
1278ec727ea7Spatrick // Check that we can reason about the symbol at all.
1279ec727ea7Spatrick if (ActualType->isIntegralOrEnumerationType() ||
1280ec727ea7Spatrick Loc::isLocType(ActualType)) {
1281ec727ea7Spatrick return infer(Sym);
1282ec727ea7Spatrick }
1283ec727ea7Spatrick // Otherwise, let's simply infer from the destination type.
1284ec727ea7Spatrick // We couldn't figure out nothing else about that expression.
1285ec727ea7Spatrick return infer(DestType);
1286ec727ea7Spatrick }
1287ec727ea7Spatrick
infer(SymbolRef Sym)1288ec727ea7Spatrick RangeSet infer(SymbolRef Sym) {
1289*12c85518Srobert return intersect(RangeFactory,
1290*12c85518Srobert // Of course, we should take the constraint directly
1291*12c85518Srobert // associated with this symbol into consideration.
1292a9ac8606Spatrick getConstraint(State, Sym),
1293*12c85518Srobert // Apart from the Sym itself, we can infer quite a lot if
1294*12c85518Srobert // we look into subexpressions of Sym.
1295a9ac8606Spatrick Visit(Sym));
1296a9ac8606Spatrick }
1297ec727ea7Spatrick
infer(EquivalenceClass Class)1298a9ac8606Spatrick RangeSet infer(EquivalenceClass Class) {
1299a9ac8606Spatrick if (const RangeSet *AssociatedConstraint = getConstraint(State, Class))
1300a9ac8606Spatrick return *AssociatedConstraint;
1301a9ac8606Spatrick
1302a9ac8606Spatrick return infer(Class.getType());
1303ec727ea7Spatrick }
1304ec727ea7Spatrick
1305ec727ea7Spatrick /// Infer range information solely from the type.
infer(QualType T)1306ec727ea7Spatrick RangeSet infer(QualType T) {
1307ec727ea7Spatrick // Lazily generate a new RangeSet representing all possible values for the
1308ec727ea7Spatrick // given symbol type.
1309ec727ea7Spatrick RangeSet Result(RangeFactory, ValueFactory.getMinValue(T),
1310ec727ea7Spatrick ValueFactory.getMaxValue(T));
1311ec727ea7Spatrick
1312ec727ea7Spatrick // References are known to be non-zero.
1313ec727ea7Spatrick if (T->isReferenceType())
1314ec727ea7Spatrick return assumeNonZero(Result, T);
1315ec727ea7Spatrick
1316ec727ea7Spatrick return Result;
1317ec727ea7Spatrick }
1318ec727ea7Spatrick
1319ec727ea7Spatrick template <class BinarySymExprTy>
VisitBinaryOperator(const BinarySymExprTy * Sym)1320ec727ea7Spatrick RangeSet VisitBinaryOperator(const BinarySymExprTy *Sym) {
1321ec727ea7Spatrick // TODO #1: VisitBinaryOperator implementation might not make a good
1322ec727ea7Spatrick // use of the inferred ranges. In this case, we might be calculating
1323ec727ea7Spatrick // everything for nothing. This being said, we should introduce some
1324ec727ea7Spatrick // sort of laziness mechanism here.
1325ec727ea7Spatrick //
1326ec727ea7Spatrick // TODO #2: We didn't go into the nested expressions before, so it
1327ec727ea7Spatrick // might cause us spending much more time doing the inference.
1328ec727ea7Spatrick // This can be a problem for deeply nested expressions that are
1329ec727ea7Spatrick // involved in conditions and get tested continuously. We definitely
1330ec727ea7Spatrick // need to address this issue and introduce some sort of caching
1331ec727ea7Spatrick // in here.
1332ec727ea7Spatrick QualType ResultType = Sym->getType();
1333ec727ea7Spatrick return VisitBinaryOperator(inferAs(Sym->getLHS(), ResultType),
1334ec727ea7Spatrick Sym->getOpcode(),
1335ec727ea7Spatrick inferAs(Sym->getRHS(), ResultType), ResultType);
1336ec727ea7Spatrick }
1337ec727ea7Spatrick
1338ec727ea7Spatrick RangeSet VisitBinaryOperator(RangeSet LHS, BinaryOperator::Opcode Op,
1339*12c85518Srobert RangeSet RHS, QualType T);
1340ec727ea7Spatrick
1341ec727ea7Spatrick //===----------------------------------------------------------------------===//
1342ec727ea7Spatrick // Ranges and operators
1343ec727ea7Spatrick //===----------------------------------------------------------------------===//
1344ec727ea7Spatrick
1345ec727ea7Spatrick /// Return a rough approximation of the given range set.
1346ec727ea7Spatrick ///
1347ec727ea7Spatrick /// For the range set:
1348ec727ea7Spatrick /// { [x_0, y_0], [x_1, y_1], ... , [x_N, y_N] }
1349ec727ea7Spatrick /// it will return the range [x_0, y_N].
fillGaps(RangeSet Origin)1350ec727ea7Spatrick static Range fillGaps(RangeSet Origin) {
1351ec727ea7Spatrick assert(!Origin.isEmpty());
1352ec727ea7Spatrick return {Origin.getMinValue(), Origin.getMaxValue()};
1353ec727ea7Spatrick }
1354ec727ea7Spatrick
1355ec727ea7Spatrick /// Try to convert given range into the given type.
1356ec727ea7Spatrick ///
1357*12c85518Srobert /// It will return std::nullopt only when the trivial conversion is possible.
convert(const Range & Origin,APSIntType To)1358*12c85518Srobert std::optional<Range> convert(const Range &Origin, APSIntType To) {
1359ec727ea7Spatrick if (To.testInRange(Origin.From(), false) != APSIntType::RTR_Within ||
1360ec727ea7Spatrick To.testInRange(Origin.To(), false) != APSIntType::RTR_Within) {
1361*12c85518Srobert return std::nullopt;
1362ec727ea7Spatrick }
1363ec727ea7Spatrick return Range(ValueFactory.Convert(To, Origin.From()),
1364ec727ea7Spatrick ValueFactory.Convert(To, Origin.To()));
1365ec727ea7Spatrick }
1366ec727ea7Spatrick
1367ec727ea7Spatrick template <BinaryOperator::Opcode Op>
VisitBinaryOperator(RangeSet LHS,RangeSet RHS,QualType T)1368ec727ea7Spatrick RangeSet VisitBinaryOperator(RangeSet LHS, RangeSet RHS, QualType T) {
1369*12c85518Srobert assert(!LHS.isEmpty() && !RHS.isEmpty());
1370ec727ea7Spatrick
1371ec727ea7Spatrick Range CoarseLHS = fillGaps(LHS);
1372ec727ea7Spatrick Range CoarseRHS = fillGaps(RHS);
1373ec727ea7Spatrick
1374ec727ea7Spatrick APSIntType ResultType = ValueFactory.getAPSIntType(T);
1375ec727ea7Spatrick
1376ec727ea7Spatrick // We need to convert ranges to the resulting type, so we can compare values
1377ec727ea7Spatrick // and combine them in a meaningful (in terms of the given operation) way.
1378ec727ea7Spatrick auto ConvertedCoarseLHS = convert(CoarseLHS, ResultType);
1379ec727ea7Spatrick auto ConvertedCoarseRHS = convert(CoarseRHS, ResultType);
1380ec727ea7Spatrick
1381ec727ea7Spatrick // It is hard to reason about ranges when conversion changes
1382ec727ea7Spatrick // borders of the ranges.
1383ec727ea7Spatrick if (!ConvertedCoarseLHS || !ConvertedCoarseRHS) {
1384ec727ea7Spatrick return infer(T);
1385ec727ea7Spatrick }
1386ec727ea7Spatrick
1387ec727ea7Spatrick return VisitBinaryOperator<Op>(*ConvertedCoarseLHS, *ConvertedCoarseRHS, T);
1388ec727ea7Spatrick }
1389ec727ea7Spatrick
1390ec727ea7Spatrick template <BinaryOperator::Opcode Op>
VisitBinaryOperator(Range LHS,Range RHS,QualType T)1391ec727ea7Spatrick RangeSet VisitBinaryOperator(Range LHS, Range RHS, QualType T) {
1392ec727ea7Spatrick return infer(T);
1393ec727ea7Spatrick }
1394ec727ea7Spatrick
1395ec727ea7Spatrick /// Return a symmetrical range for the given range and type.
1396ec727ea7Spatrick ///
1397ec727ea7Spatrick /// If T is signed, return the smallest range [-x..x] that covers the original
1398ec727ea7Spatrick /// range, or [-min(T), max(T)] if the aforementioned symmetric range doesn't
1399ec727ea7Spatrick /// exist due to original range covering min(T)).
1400ec727ea7Spatrick ///
1401ec727ea7Spatrick /// If T is unsigned, return the smallest range [0..x] that covers the
1402ec727ea7Spatrick /// original range.
getSymmetricalRange(Range Origin,QualType T)1403ec727ea7Spatrick Range getSymmetricalRange(Range Origin, QualType T) {
1404ec727ea7Spatrick APSIntType RangeType = ValueFactory.getAPSIntType(T);
1405ec727ea7Spatrick
1406ec727ea7Spatrick if (RangeType.isUnsigned()) {
1407ec727ea7Spatrick return Range(ValueFactory.getMinValue(RangeType), Origin.To());
1408ec727ea7Spatrick }
1409ec727ea7Spatrick
1410ec727ea7Spatrick if (Origin.From().isMinSignedValue()) {
1411ec727ea7Spatrick // If mini is a minimal signed value, absolute value of it is greater
1412ec727ea7Spatrick // than the maximal signed value. In order to avoid these
1413ec727ea7Spatrick // complications, we simply return the whole range.
1414ec727ea7Spatrick return {ValueFactory.getMinValue(RangeType),
1415ec727ea7Spatrick ValueFactory.getMaxValue(RangeType)};
1416ec727ea7Spatrick }
1417ec727ea7Spatrick
1418ec727ea7Spatrick // At this point, we are sure that the type is signed and we can safely
1419ec727ea7Spatrick // use unary - operator.
1420ec727ea7Spatrick //
1421ec727ea7Spatrick // While calculating absolute maximum, we can use the following formula
1422ec727ea7Spatrick // because of these reasons:
1423ec727ea7Spatrick // * If From >= 0 then To >= From and To >= -From.
1424ec727ea7Spatrick // AbsMax == To == max(To, -From)
1425ec727ea7Spatrick // * If To <= 0 then -From >= -To and -From >= From.
1426ec727ea7Spatrick // AbsMax == -From == max(-From, To)
1427ec727ea7Spatrick // * Otherwise, From <= 0, To >= 0, and
1428ec727ea7Spatrick // AbsMax == max(abs(From), abs(To))
1429ec727ea7Spatrick llvm::APSInt AbsMax = std::max(-Origin.From(), Origin.To());
1430ec727ea7Spatrick
1431ec727ea7Spatrick // Intersection is guaranteed to be non-empty.
1432ec727ea7Spatrick return {ValueFactory.getValue(-AbsMax), ValueFactory.getValue(AbsMax)};
1433ec727ea7Spatrick }
1434ec727ea7Spatrick
1435ec727ea7Spatrick /// Return a range set subtracting zero from \p Domain.
assumeNonZero(RangeSet Domain,QualType T)1436ec727ea7Spatrick RangeSet assumeNonZero(RangeSet Domain, QualType T) {
1437ec727ea7Spatrick APSIntType IntType = ValueFactory.getAPSIntType(T);
1438a9ac8606Spatrick return RangeFactory.deletePoint(Domain, IntType.getZeroValue());
1439ec727ea7Spatrick }
1440ec727ea7Spatrick
1441*12c85518Srobert template <typename ProduceNegatedSymFunc>
getRangeForNegatedExpr(ProduceNegatedSymFunc F,QualType T)1442*12c85518Srobert std::optional<RangeSet> getRangeForNegatedExpr(ProduceNegatedSymFunc F,
1443*12c85518Srobert QualType T) {
1444*12c85518Srobert // Do not negate if the type cannot be meaningfully negated.
1445a9ac8606Spatrick if (!T->isUnsignedIntegerOrEnumerationType() &&
1446a9ac8606Spatrick !T->isSignedIntegerOrEnumerationType())
1447*12c85518Srobert return std::nullopt;
1448a9ac8606Spatrick
1449*12c85518Srobert if (SymbolRef NegatedSym = F())
1450*12c85518Srobert if (const RangeSet *NegatedRange = getConstraint(State, NegatedSym))
1451a9ac8606Spatrick return RangeFactory.negate(*NegatedRange);
1452*12c85518Srobert
1453*12c85518Srobert return std::nullopt;
1454ec727ea7Spatrick }
1455*12c85518Srobert
getRangeForNegatedUnarySym(const UnarySymExpr * USE)1456*12c85518Srobert std::optional<RangeSet> getRangeForNegatedUnarySym(const UnarySymExpr *USE) {
1457*12c85518Srobert // Just get the operand when we negate a symbol that is already negated.
1458*12c85518Srobert // -(-a) == a
1459*12c85518Srobert return getRangeForNegatedExpr(
1460*12c85518Srobert [USE]() -> SymbolRef {
1461*12c85518Srobert if (USE->getOpcode() == UO_Minus)
1462*12c85518Srobert return USE->getOperand();
1463*12c85518Srobert return nullptr;
1464*12c85518Srobert },
1465*12c85518Srobert USE->getType());
1466ec727ea7Spatrick }
1467*12c85518Srobert
getRangeForNegatedSymSym(const SymSymExpr * SSE)1468*12c85518Srobert std::optional<RangeSet> getRangeForNegatedSymSym(const SymSymExpr *SSE) {
1469*12c85518Srobert return getRangeForNegatedExpr(
1470*12c85518Srobert [SSE, State = this->State]() -> SymbolRef {
1471*12c85518Srobert if (SSE->getOpcode() == BO_Sub)
1472*12c85518Srobert return State->getSymbolManager().getSymSymExpr(
1473*12c85518Srobert SSE->getRHS(), BO_Sub, SSE->getLHS(), SSE->getType());
1474*12c85518Srobert return nullptr;
1475*12c85518Srobert },
1476*12c85518Srobert SSE->getType());
1477ec727ea7Spatrick }
1478*12c85518Srobert
getRangeForNegatedSym(SymbolRef Sym)1479*12c85518Srobert std::optional<RangeSet> getRangeForNegatedSym(SymbolRef Sym) {
1480*12c85518Srobert return getRangeForNegatedExpr(
1481*12c85518Srobert [Sym, State = this->State]() {
1482*12c85518Srobert return State->getSymbolManager().getUnarySymExpr(Sym, UO_Minus,
1483*12c85518Srobert Sym->getType());
1484*12c85518Srobert },
1485*12c85518Srobert Sym->getType());
1486ec727ea7Spatrick }
1487ec727ea7Spatrick
1488ec727ea7Spatrick // Returns ranges only for binary comparison operators (except <=>)
1489ec727ea7Spatrick // when left and right operands are symbolic values.
1490ec727ea7Spatrick // Finds any other comparisons with the same operands.
1491ec727ea7Spatrick // Then do logical calculations and refuse impossible branches.
1492ec727ea7Spatrick // E.g. (x < y) and (x > y) at the same time are impossible.
1493ec727ea7Spatrick // E.g. (x >= y) and (x != y) at the same time makes (x > y) true only.
1494ec727ea7Spatrick // E.g. (x == y) and (y == x) are just reversed but the same.
1495ec727ea7Spatrick // It covers all possible combinations (see CmpOpTable description).
1496ec727ea7Spatrick // Note that `x` and `y` can also stand for subexpressions,
1497ec727ea7Spatrick // not only for actual symbols.
getRangeForComparisonSymbol(const SymSymExpr * SSE)1498*12c85518Srobert std::optional<RangeSet> getRangeForComparisonSymbol(const SymSymExpr *SSE) {
1499*12c85518Srobert const BinaryOperatorKind CurrentOP = SSE->getOpcode();
1500ec727ea7Spatrick
1501ec727ea7Spatrick // We currently do not support <=> (C++20).
1502ec727ea7Spatrick if (!BinaryOperator::isComparisonOp(CurrentOP) || (CurrentOP == BO_Cmp))
1503*12c85518Srobert return std::nullopt;
1504ec727ea7Spatrick
1505ec727ea7Spatrick static const OperatorRelationsTable CmpOpTable{};
1506ec727ea7Spatrick
1507ec727ea7Spatrick const SymExpr *LHS = SSE->getLHS();
1508ec727ea7Spatrick const SymExpr *RHS = SSE->getRHS();
1509ec727ea7Spatrick QualType T = SSE->getType();
1510ec727ea7Spatrick
1511ec727ea7Spatrick SymbolManager &SymMgr = State->getSymbolManager();
1512ec727ea7Spatrick
1513*12c85518Srobert // We use this variable to store the last queried operator (`QueriedOP`)
1514*12c85518Srobert // for which the `getCmpOpState` returned with `Unknown`. If there are two
1515*12c85518Srobert // different OPs that returned `Unknown` then we have to query the special
1516*12c85518Srobert // `UnknownX2` column. We assume that `getCmpOpState(CurrentOP, CurrentOP)`
1517*12c85518Srobert // never returns `Unknown`, so `CurrentOP` is a good initial value.
1518*12c85518Srobert BinaryOperatorKind LastQueriedOpToUnknown = CurrentOP;
1519ec727ea7Spatrick
1520ec727ea7Spatrick // Loop goes through all of the columns exept the last one ('UnknownX2').
1521ec727ea7Spatrick // We treat `UnknownX2` column separately at the end of the loop body.
1522ec727ea7Spatrick for (size_t i = 0; i < CmpOpTable.getCmpOpCount(); ++i) {
1523ec727ea7Spatrick
1524ec727ea7Spatrick // Let's find an expression e.g. (x < y).
1525ec727ea7Spatrick BinaryOperatorKind QueriedOP = OperatorRelationsTable::getOpFromIndex(i);
1526ec727ea7Spatrick const SymSymExpr *SymSym = SymMgr.getSymSymExpr(LHS, QueriedOP, RHS, T);
1527a9ac8606Spatrick const RangeSet *QueriedRangeSet = getConstraint(State, SymSym);
1528ec727ea7Spatrick
1529ec727ea7Spatrick // If ranges were not previously found,
1530ec727ea7Spatrick // try to find a reversed expression (y > x).
1531ec727ea7Spatrick if (!QueriedRangeSet) {
1532ec727ea7Spatrick const BinaryOperatorKind ROP =
1533ec727ea7Spatrick BinaryOperator::reverseComparisonOp(QueriedOP);
1534ec727ea7Spatrick SymSym = SymMgr.getSymSymExpr(RHS, ROP, LHS, T);
1535a9ac8606Spatrick QueriedRangeSet = getConstraint(State, SymSym);
1536ec727ea7Spatrick }
1537ec727ea7Spatrick
1538ec727ea7Spatrick if (!QueriedRangeSet || QueriedRangeSet->isEmpty())
1539ec727ea7Spatrick continue;
1540ec727ea7Spatrick
1541ec727ea7Spatrick const llvm::APSInt *ConcreteValue = QueriedRangeSet->getConcreteValue();
1542ec727ea7Spatrick const bool isInFalseBranch =
1543ec727ea7Spatrick ConcreteValue ? (*ConcreteValue == 0) : false;
1544ec727ea7Spatrick
1545ec727ea7Spatrick // If it is a false branch, we shall be guided by opposite operator,
1546ec727ea7Spatrick // because the table is made assuming we are in the true branch.
1547ec727ea7Spatrick // E.g. when (x <= y) is false, then (x > y) is true.
1548ec727ea7Spatrick if (isInFalseBranch)
1549ec727ea7Spatrick QueriedOP = BinaryOperator::negateComparisonOp(QueriedOP);
1550ec727ea7Spatrick
1551ec727ea7Spatrick OperatorRelationsTable::TriStateKind BranchState =
1552ec727ea7Spatrick CmpOpTable.getCmpOpState(CurrentOP, QueriedOP);
1553ec727ea7Spatrick
1554ec727ea7Spatrick if (BranchState == OperatorRelationsTable::Unknown) {
1555*12c85518Srobert if (LastQueriedOpToUnknown != CurrentOP &&
1556*12c85518Srobert LastQueriedOpToUnknown != QueriedOP) {
1557*12c85518Srobert // If we got the Unknown state for both different operators.
1558ec727ea7Spatrick // if (x <= y) // assume true
1559ec727ea7Spatrick // if (x != y) // assume true
1560ec727ea7Spatrick // if (x < y) // would be also true
1561ec727ea7Spatrick // Get a state from `UnknownX2` column.
1562ec727ea7Spatrick BranchState = CmpOpTable.getCmpOpStateForUnknownX2(CurrentOP);
1563*12c85518Srobert } else {
1564*12c85518Srobert LastQueriedOpToUnknown = QueriedOP;
1565ec727ea7Spatrick continue;
1566ec727ea7Spatrick }
1567*12c85518Srobert }
1568ec727ea7Spatrick
1569a9ac8606Spatrick return (BranchState == OperatorRelationsTable::True) ? getTrueRange(T)
1570a9ac8606Spatrick : getFalseRange(T);
1571ec727ea7Spatrick }
1572ec727ea7Spatrick
1573*12c85518Srobert return std::nullopt;
1574a9ac8606Spatrick }
1575a9ac8606Spatrick
getRangeForEqualities(const SymSymExpr * Sym)1576*12c85518Srobert std::optional<RangeSet> getRangeForEqualities(const SymSymExpr *Sym) {
1577*12c85518Srobert std::optional<bool> Equality = meansEquality(Sym);
1578a9ac8606Spatrick
1579a9ac8606Spatrick if (!Equality)
1580*12c85518Srobert return std::nullopt;
1581a9ac8606Spatrick
1582*12c85518Srobert if (std::optional<bool> AreEqual =
1583a9ac8606Spatrick EquivalenceClass::areEqual(State, Sym->getLHS(), Sym->getRHS())) {
1584a9ac8606Spatrick // Here we cover two cases at once:
1585a9ac8606Spatrick // * if Sym is equality and its operands are known to be equal -> true
1586a9ac8606Spatrick // * if Sym is disequality and its operands are disequal -> true
1587a9ac8606Spatrick if (*AreEqual == *Equality) {
1588a9ac8606Spatrick return getTrueRange(Sym->getType());
1589a9ac8606Spatrick }
1590a9ac8606Spatrick // Opposite combinations result in false.
1591a9ac8606Spatrick return getFalseRange(Sym->getType());
1592a9ac8606Spatrick }
1593a9ac8606Spatrick
1594*12c85518Srobert return std::nullopt;
1595a9ac8606Spatrick }
1596a9ac8606Spatrick
getTrueRange(QualType T)1597a9ac8606Spatrick RangeSet getTrueRange(QualType T) {
1598a9ac8606Spatrick RangeSet TypeRange = infer(T);
1599a9ac8606Spatrick return assumeNonZero(TypeRange, T);
1600a9ac8606Spatrick }
1601a9ac8606Spatrick
getFalseRange(QualType T)1602a9ac8606Spatrick RangeSet getFalseRange(QualType T) {
1603a9ac8606Spatrick const llvm::APSInt &Zero = ValueFactory.getValue(0, T);
1604a9ac8606Spatrick return RangeSet(RangeFactory, Zero);
1605ec727ea7Spatrick }
1606ec727ea7Spatrick
1607ec727ea7Spatrick BasicValueFactory &ValueFactory;
1608ec727ea7Spatrick RangeSet::Factory &RangeFactory;
1609ec727ea7Spatrick ProgramStateRef State;
1610ec727ea7Spatrick };
1611ec727ea7Spatrick
1612a9ac8606Spatrick //===----------------------------------------------------------------------===//
1613a9ac8606Spatrick // Range-based reasoning about symbolic operations
1614a9ac8606Spatrick //===----------------------------------------------------------------------===//
1615a9ac8606Spatrick
1616ec727ea7Spatrick template <>
VisitBinaryOperator(RangeSet LHS,RangeSet RHS,QualType T)1617*12c85518Srobert RangeSet SymbolicRangeInferrer::VisitBinaryOperator<BO_NE>(RangeSet LHS,
1618*12c85518Srobert RangeSet RHS,
1619*12c85518Srobert QualType T) {
1620*12c85518Srobert assert(!LHS.isEmpty() && !RHS.isEmpty());
1621*12c85518Srobert
1622*12c85518Srobert if (LHS.getAPSIntType() == RHS.getAPSIntType()) {
1623*12c85518Srobert if (intersect(RangeFactory, LHS, RHS).isEmpty())
1624*12c85518Srobert return getTrueRange(T);
1625*12c85518Srobert
1626*12c85518Srobert } else {
1627*12c85518Srobert // We can only lose information if we are casting smaller signed type to
1628*12c85518Srobert // bigger unsigned type. For e.g.,
1629*12c85518Srobert // LHS (unsigned short): [2, USHRT_MAX]
1630*12c85518Srobert // RHS (signed short): [SHRT_MIN, 0]
1631*12c85518Srobert //
1632*12c85518Srobert // Casting RHS to LHS type will leave us with overlapping values
1633*12c85518Srobert // CastedRHS : [0, 0] U [SHRT_MAX + 1, USHRT_MAX]
1634*12c85518Srobert //
1635*12c85518Srobert // We can avoid this by checking if signed type's maximum value is lesser
1636*12c85518Srobert // than unsigned type's minimum value.
1637*12c85518Srobert
1638*12c85518Srobert // If both have different signs then only we can get more information.
1639*12c85518Srobert if (LHS.isUnsigned() != RHS.isUnsigned()) {
1640*12c85518Srobert if (LHS.isUnsigned() && (LHS.getBitWidth() >= RHS.getBitWidth())) {
1641*12c85518Srobert if (RHS.getMaxValue().isNegative() ||
1642*12c85518Srobert LHS.getAPSIntType().convert(RHS.getMaxValue()) < LHS.getMinValue())
1643*12c85518Srobert return getTrueRange(T);
1644*12c85518Srobert
1645*12c85518Srobert } else if (RHS.isUnsigned() && (LHS.getBitWidth() <= RHS.getBitWidth())) {
1646*12c85518Srobert if (LHS.getMaxValue().isNegative() ||
1647*12c85518Srobert RHS.getAPSIntType().convert(LHS.getMaxValue()) < RHS.getMinValue())
1648*12c85518Srobert return getTrueRange(T);
1649*12c85518Srobert }
1650*12c85518Srobert }
1651*12c85518Srobert
1652*12c85518Srobert // Both RangeSets should be casted to bigger unsigned type.
1653*12c85518Srobert APSIntType CastingType(std::max(LHS.getBitWidth(), RHS.getBitWidth()),
1654*12c85518Srobert LHS.isUnsigned() || RHS.isUnsigned());
1655*12c85518Srobert
1656*12c85518Srobert RangeSet CastedLHS = RangeFactory.castTo(LHS, CastingType);
1657*12c85518Srobert RangeSet CastedRHS = RangeFactory.castTo(RHS, CastingType);
1658*12c85518Srobert
1659*12c85518Srobert if (intersect(RangeFactory, CastedLHS, CastedRHS).isEmpty())
1660*12c85518Srobert return getTrueRange(T);
1661*12c85518Srobert }
1662*12c85518Srobert
1663*12c85518Srobert // In all other cases, the resulting range cannot be deduced.
1664*12c85518Srobert return infer(T);
1665*12c85518Srobert }
1666*12c85518Srobert
1667*12c85518Srobert template <>
VisitBinaryOperator(Range LHS,Range RHS,QualType T)1668ec727ea7Spatrick RangeSet SymbolicRangeInferrer::VisitBinaryOperator<BO_Or>(Range LHS, Range RHS,
1669ec727ea7Spatrick QualType T) {
1670ec727ea7Spatrick APSIntType ResultType = ValueFactory.getAPSIntType(T);
1671ec727ea7Spatrick llvm::APSInt Zero = ResultType.getZeroValue();
1672ec727ea7Spatrick
1673ec727ea7Spatrick bool IsLHSPositiveOrZero = LHS.From() >= Zero;
1674ec727ea7Spatrick bool IsRHSPositiveOrZero = RHS.From() >= Zero;
1675ec727ea7Spatrick
1676ec727ea7Spatrick bool IsLHSNegative = LHS.To() < Zero;
1677ec727ea7Spatrick bool IsRHSNegative = RHS.To() < Zero;
1678ec727ea7Spatrick
1679ec727ea7Spatrick // Check if both ranges have the same sign.
1680ec727ea7Spatrick if ((IsLHSPositiveOrZero && IsRHSPositiveOrZero) ||
1681ec727ea7Spatrick (IsLHSNegative && IsRHSNegative)) {
1682ec727ea7Spatrick // The result is definitely greater or equal than any of the operands.
1683ec727ea7Spatrick const llvm::APSInt &Min = std::max(LHS.From(), RHS.From());
1684ec727ea7Spatrick
1685ec727ea7Spatrick // We estimate maximal value for positives as the maximal value for the
1686ec727ea7Spatrick // given type. For negatives, we estimate it with -1 (e.g. 0x11111111).
1687ec727ea7Spatrick //
1688ec727ea7Spatrick // TODO: We basically, limit the resulting range from below, but don't do
1689ec727ea7Spatrick // anything with the upper bound.
1690ec727ea7Spatrick //
1691ec727ea7Spatrick // For positive operands, it can be done as follows: for the upper
1692ec727ea7Spatrick // bound of LHS and RHS we calculate the most significant bit set.
1693ec727ea7Spatrick // Let's call it the N-th bit. Then we can estimate the maximal
1694ec727ea7Spatrick // number to be 2^(N+1)-1, i.e. the number with all the bits up to
1695ec727ea7Spatrick // the N-th bit set.
1696ec727ea7Spatrick const llvm::APSInt &Max = IsLHSNegative
1697ec727ea7Spatrick ? ValueFactory.getValue(--Zero)
1698ec727ea7Spatrick : ValueFactory.getMaxValue(ResultType);
1699ec727ea7Spatrick
1700ec727ea7Spatrick return {RangeFactory, ValueFactory.getValue(Min), Max};
1701ec727ea7Spatrick }
1702ec727ea7Spatrick
1703ec727ea7Spatrick // Otherwise, let's check if at least one of the operands is negative.
1704ec727ea7Spatrick if (IsLHSNegative || IsRHSNegative) {
1705ec727ea7Spatrick // This means that the result is definitely negative as well.
1706ec727ea7Spatrick return {RangeFactory, ValueFactory.getMinValue(ResultType),
1707ec727ea7Spatrick ValueFactory.getValue(--Zero)};
1708ec727ea7Spatrick }
1709ec727ea7Spatrick
1710ec727ea7Spatrick RangeSet DefaultRange = infer(T);
1711ec727ea7Spatrick
1712ec727ea7Spatrick // It is pretty hard to reason about operands with different signs
1713ec727ea7Spatrick // (and especially with possibly different signs). We simply check if it
1714ec727ea7Spatrick // can be zero. In order to conclude that the result could not be zero,
1715ec727ea7Spatrick // at least one of the operands should be definitely not zero itself.
1716ec727ea7Spatrick if (!LHS.Includes(Zero) || !RHS.Includes(Zero)) {
1717ec727ea7Spatrick return assumeNonZero(DefaultRange, T);
1718ec727ea7Spatrick }
1719ec727ea7Spatrick
1720ec727ea7Spatrick // Nothing much else to do here.
1721ec727ea7Spatrick return DefaultRange;
1722ec727ea7Spatrick }
1723ec727ea7Spatrick
1724ec727ea7Spatrick template <>
VisitBinaryOperator(Range LHS,Range RHS,QualType T)1725ec727ea7Spatrick RangeSet SymbolicRangeInferrer::VisitBinaryOperator<BO_And>(Range LHS,
1726ec727ea7Spatrick Range RHS,
1727ec727ea7Spatrick QualType T) {
1728ec727ea7Spatrick APSIntType ResultType = ValueFactory.getAPSIntType(T);
1729ec727ea7Spatrick llvm::APSInt Zero = ResultType.getZeroValue();
1730ec727ea7Spatrick
1731ec727ea7Spatrick bool IsLHSPositiveOrZero = LHS.From() >= Zero;
1732ec727ea7Spatrick bool IsRHSPositiveOrZero = RHS.From() >= Zero;
1733ec727ea7Spatrick
1734ec727ea7Spatrick bool IsLHSNegative = LHS.To() < Zero;
1735ec727ea7Spatrick bool IsRHSNegative = RHS.To() < Zero;
1736ec727ea7Spatrick
1737ec727ea7Spatrick // Check if both ranges have the same sign.
1738ec727ea7Spatrick if ((IsLHSPositiveOrZero && IsRHSPositiveOrZero) ||
1739ec727ea7Spatrick (IsLHSNegative && IsRHSNegative)) {
1740ec727ea7Spatrick // The result is definitely less or equal than any of the operands.
1741ec727ea7Spatrick const llvm::APSInt &Max = std::min(LHS.To(), RHS.To());
1742ec727ea7Spatrick
1743ec727ea7Spatrick // We conservatively estimate lower bound to be the smallest positive
1744ec727ea7Spatrick // or negative value corresponding to the sign of the operands.
1745ec727ea7Spatrick const llvm::APSInt &Min = IsLHSNegative
1746ec727ea7Spatrick ? ValueFactory.getMinValue(ResultType)
1747ec727ea7Spatrick : ValueFactory.getValue(Zero);
1748ec727ea7Spatrick
1749ec727ea7Spatrick return {RangeFactory, Min, Max};
1750ec727ea7Spatrick }
1751ec727ea7Spatrick
1752ec727ea7Spatrick // Otherwise, let's check if at least one of the operands is positive.
1753ec727ea7Spatrick if (IsLHSPositiveOrZero || IsRHSPositiveOrZero) {
1754ec727ea7Spatrick // This makes result definitely positive.
1755ec727ea7Spatrick //
1756ec727ea7Spatrick // We can also reason about a maximal value by finding the maximal
1757ec727ea7Spatrick // value of the positive operand.
1758ec727ea7Spatrick const llvm::APSInt &Max = IsLHSPositiveOrZero ? LHS.To() : RHS.To();
1759ec727ea7Spatrick
1760ec727ea7Spatrick // The minimal value on the other hand is much harder to reason about.
1761ec727ea7Spatrick // The only thing we know for sure is that the result is positive.
1762ec727ea7Spatrick return {RangeFactory, ValueFactory.getValue(Zero),
1763ec727ea7Spatrick ValueFactory.getValue(Max)};
1764ec727ea7Spatrick }
1765ec727ea7Spatrick
1766ec727ea7Spatrick // Nothing much else to do here.
1767ec727ea7Spatrick return infer(T);
1768ec727ea7Spatrick }
1769ec727ea7Spatrick
1770ec727ea7Spatrick template <>
VisitBinaryOperator(Range LHS,Range RHS,QualType T)1771ec727ea7Spatrick RangeSet SymbolicRangeInferrer::VisitBinaryOperator<BO_Rem>(Range LHS,
1772ec727ea7Spatrick Range RHS,
1773ec727ea7Spatrick QualType T) {
1774ec727ea7Spatrick llvm::APSInt Zero = ValueFactory.getAPSIntType(T).getZeroValue();
1775ec727ea7Spatrick
1776ec727ea7Spatrick Range ConservativeRange = getSymmetricalRange(RHS, T);
1777ec727ea7Spatrick
1778ec727ea7Spatrick llvm::APSInt Max = ConservativeRange.To();
1779ec727ea7Spatrick llvm::APSInt Min = ConservativeRange.From();
1780ec727ea7Spatrick
1781ec727ea7Spatrick if (Max == Zero) {
1782ec727ea7Spatrick // It's an undefined behaviour to divide by 0 and it seems like we know
1783ec727ea7Spatrick // for sure that RHS is 0. Let's say that the resulting range is
1784ec727ea7Spatrick // simply infeasible for that matter.
1785ec727ea7Spatrick return RangeFactory.getEmptySet();
1786ec727ea7Spatrick }
1787ec727ea7Spatrick
1788ec727ea7Spatrick // At this point, our conservative range is closed. The result, however,
1789ec727ea7Spatrick // couldn't be greater than the RHS' maximal absolute value. Because of
1790ec727ea7Spatrick // this reason, we turn the range into open (or half-open in case of
1791ec727ea7Spatrick // unsigned integers).
1792ec727ea7Spatrick //
1793ec727ea7Spatrick // While we operate on integer values, an open interval (a, b) can be easily
1794ec727ea7Spatrick // represented by the closed interval [a + 1, b - 1]. And this is exactly
1795ec727ea7Spatrick // what we do next.
1796ec727ea7Spatrick //
1797ec727ea7Spatrick // If we are dealing with unsigned case, we shouldn't move the lower bound.
1798ec727ea7Spatrick if (Min.isSigned()) {
1799ec727ea7Spatrick ++Min;
1800ec727ea7Spatrick }
1801ec727ea7Spatrick --Max;
1802ec727ea7Spatrick
1803ec727ea7Spatrick bool IsLHSPositiveOrZero = LHS.From() >= Zero;
1804ec727ea7Spatrick bool IsRHSPositiveOrZero = RHS.From() >= Zero;
1805ec727ea7Spatrick
1806ec727ea7Spatrick // Remainder operator results with negative operands is implementation
1807ec727ea7Spatrick // defined. Positive cases are much easier to reason about though.
1808ec727ea7Spatrick if (IsLHSPositiveOrZero && IsRHSPositiveOrZero) {
1809ec727ea7Spatrick // If maximal value of LHS is less than maximal value of RHS,
1810ec727ea7Spatrick // the result won't get greater than LHS.To().
1811ec727ea7Spatrick Max = std::min(LHS.To(), Max);
1812ec727ea7Spatrick // We want to check if it is a situation similar to the following:
1813ec727ea7Spatrick //
1814ec727ea7Spatrick // <------------|---[ LHS ]--------[ RHS ]----->
1815ec727ea7Spatrick // -INF 0 +INF
1816ec727ea7Spatrick //
1817ec727ea7Spatrick // In this situation, we can conclude that (LHS / RHS) == 0 and
1818ec727ea7Spatrick // (LHS % RHS) == LHS.
1819ec727ea7Spatrick Min = LHS.To() < RHS.From() ? LHS.From() : Zero;
1820ec727ea7Spatrick }
1821ec727ea7Spatrick
1822ec727ea7Spatrick // Nevertheless, the symmetrical range for RHS is a conservative estimate
1823ec727ea7Spatrick // for any sign of either LHS, or RHS.
1824ec727ea7Spatrick return {RangeFactory, ValueFactory.getValue(Min), ValueFactory.getValue(Max)};
1825ec727ea7Spatrick }
1826ec727ea7Spatrick
VisitBinaryOperator(RangeSet LHS,BinaryOperator::Opcode Op,RangeSet RHS,QualType T)1827*12c85518Srobert RangeSet SymbolicRangeInferrer::VisitBinaryOperator(RangeSet LHS,
1828*12c85518Srobert BinaryOperator::Opcode Op,
1829*12c85518Srobert RangeSet RHS, QualType T) {
1830*12c85518Srobert // We should propagate information about unfeasbility of one of the
1831*12c85518Srobert // operands to the resulting range.
1832*12c85518Srobert if (LHS.isEmpty() || RHS.isEmpty()) {
1833*12c85518Srobert return RangeFactory.getEmptySet();
1834*12c85518Srobert }
1835*12c85518Srobert
1836*12c85518Srobert switch (Op) {
1837*12c85518Srobert case BO_NE:
1838*12c85518Srobert return VisitBinaryOperator<BO_NE>(LHS, RHS, T);
1839*12c85518Srobert case BO_Or:
1840*12c85518Srobert return VisitBinaryOperator<BO_Or>(LHS, RHS, T);
1841*12c85518Srobert case BO_And:
1842*12c85518Srobert return VisitBinaryOperator<BO_And>(LHS, RHS, T);
1843*12c85518Srobert case BO_Rem:
1844*12c85518Srobert return VisitBinaryOperator<BO_Rem>(LHS, RHS, T);
1845*12c85518Srobert default:
1846*12c85518Srobert return infer(T);
1847*12c85518Srobert }
1848*12c85518Srobert }
1849*12c85518Srobert
1850*12c85518Srobert //===----------------------------------------------------------------------===//
1851*12c85518Srobert // Constraint manager implementation details
1852*12c85518Srobert //===----------------------------------------------------------------------===//
1853*12c85518Srobert
1854*12c85518Srobert class RangeConstraintManager : public RangedConstraintManager {
1855*12c85518Srobert public:
RangeConstraintManager(ExprEngine * EE,SValBuilder & SVB)1856*12c85518Srobert RangeConstraintManager(ExprEngine *EE, SValBuilder &SVB)
1857*12c85518Srobert : RangedConstraintManager(EE, SVB), F(getBasicVals()) {}
1858*12c85518Srobert
1859*12c85518Srobert //===------------------------------------------------------------------===//
1860*12c85518Srobert // Implementation for interface from ConstraintManager.
1861*12c85518Srobert //===------------------------------------------------------------------===//
1862*12c85518Srobert
haveEqualConstraints(ProgramStateRef S1,ProgramStateRef S2) const1863*12c85518Srobert bool haveEqualConstraints(ProgramStateRef S1,
1864*12c85518Srobert ProgramStateRef S2) const override {
1865*12c85518Srobert // NOTE: ClassMembers are as simple as back pointers for ClassMap,
1866*12c85518Srobert // so comparing constraint ranges and class maps should be
1867*12c85518Srobert // sufficient.
1868*12c85518Srobert return S1->get<ConstraintRange>() == S2->get<ConstraintRange>() &&
1869*12c85518Srobert S1->get<ClassMap>() == S2->get<ClassMap>();
1870*12c85518Srobert }
1871*12c85518Srobert
1872*12c85518Srobert bool canReasonAbout(SVal X) const override;
1873*12c85518Srobert
1874*12c85518Srobert ConditionTruthVal checkNull(ProgramStateRef State, SymbolRef Sym) override;
1875*12c85518Srobert
1876*12c85518Srobert const llvm::APSInt *getSymVal(ProgramStateRef State,
1877*12c85518Srobert SymbolRef Sym) const override;
1878*12c85518Srobert
1879*12c85518Srobert ProgramStateRef removeDeadBindings(ProgramStateRef State,
1880*12c85518Srobert SymbolReaper &SymReaper) override;
1881*12c85518Srobert
1882*12c85518Srobert void printJson(raw_ostream &Out, ProgramStateRef State, const char *NL = "\n",
1883*12c85518Srobert unsigned int Space = 0, bool IsDot = false) const override;
1884*12c85518Srobert void printValue(raw_ostream &Out, ProgramStateRef State,
1885*12c85518Srobert SymbolRef Sym) override;
1886*12c85518Srobert void printConstraints(raw_ostream &Out, ProgramStateRef State,
1887*12c85518Srobert const char *NL = "\n", unsigned int Space = 0,
1888*12c85518Srobert bool IsDot = false) const;
1889*12c85518Srobert void printEquivalenceClasses(raw_ostream &Out, ProgramStateRef State,
1890*12c85518Srobert const char *NL = "\n", unsigned int Space = 0,
1891*12c85518Srobert bool IsDot = false) const;
1892*12c85518Srobert void printDisequalities(raw_ostream &Out, ProgramStateRef State,
1893*12c85518Srobert const char *NL = "\n", unsigned int Space = 0,
1894*12c85518Srobert bool IsDot = false) const;
1895*12c85518Srobert
1896*12c85518Srobert //===------------------------------------------------------------------===//
1897*12c85518Srobert // Implementation for interface from RangedConstraintManager.
1898*12c85518Srobert //===------------------------------------------------------------------===//
1899*12c85518Srobert
1900*12c85518Srobert ProgramStateRef assumeSymNE(ProgramStateRef State, SymbolRef Sym,
1901*12c85518Srobert const llvm::APSInt &V,
1902*12c85518Srobert const llvm::APSInt &Adjustment) override;
1903*12c85518Srobert
1904*12c85518Srobert ProgramStateRef assumeSymEQ(ProgramStateRef State, SymbolRef Sym,
1905*12c85518Srobert const llvm::APSInt &V,
1906*12c85518Srobert const llvm::APSInt &Adjustment) override;
1907*12c85518Srobert
1908*12c85518Srobert ProgramStateRef assumeSymLT(ProgramStateRef State, SymbolRef Sym,
1909*12c85518Srobert const llvm::APSInt &V,
1910*12c85518Srobert const llvm::APSInt &Adjustment) override;
1911*12c85518Srobert
1912*12c85518Srobert ProgramStateRef assumeSymGT(ProgramStateRef State, SymbolRef Sym,
1913*12c85518Srobert const llvm::APSInt &V,
1914*12c85518Srobert const llvm::APSInt &Adjustment) override;
1915*12c85518Srobert
1916*12c85518Srobert ProgramStateRef assumeSymLE(ProgramStateRef State, SymbolRef Sym,
1917*12c85518Srobert const llvm::APSInt &V,
1918*12c85518Srobert const llvm::APSInt &Adjustment) override;
1919*12c85518Srobert
1920*12c85518Srobert ProgramStateRef assumeSymGE(ProgramStateRef State, SymbolRef Sym,
1921*12c85518Srobert const llvm::APSInt &V,
1922*12c85518Srobert const llvm::APSInt &Adjustment) override;
1923*12c85518Srobert
1924*12c85518Srobert ProgramStateRef assumeSymWithinInclusiveRange(
1925*12c85518Srobert ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
1926*12c85518Srobert const llvm::APSInt &To, const llvm::APSInt &Adjustment) override;
1927*12c85518Srobert
1928*12c85518Srobert ProgramStateRef assumeSymOutsideInclusiveRange(
1929*12c85518Srobert ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
1930*12c85518Srobert const llvm::APSInt &To, const llvm::APSInt &Adjustment) override;
1931*12c85518Srobert
1932*12c85518Srobert private:
1933*12c85518Srobert RangeSet::Factory F;
1934*12c85518Srobert
1935*12c85518Srobert RangeSet getRange(ProgramStateRef State, SymbolRef Sym);
1936*12c85518Srobert RangeSet getRange(ProgramStateRef State, EquivalenceClass Class);
1937*12c85518Srobert ProgramStateRef setRange(ProgramStateRef State, SymbolRef Sym,
1938*12c85518Srobert RangeSet Range);
1939*12c85518Srobert ProgramStateRef setRange(ProgramStateRef State, EquivalenceClass Class,
1940*12c85518Srobert RangeSet Range);
1941*12c85518Srobert
1942*12c85518Srobert RangeSet getSymLTRange(ProgramStateRef St, SymbolRef Sym,
1943*12c85518Srobert const llvm::APSInt &Int,
1944*12c85518Srobert const llvm::APSInt &Adjustment);
1945*12c85518Srobert RangeSet getSymGTRange(ProgramStateRef St, SymbolRef Sym,
1946*12c85518Srobert const llvm::APSInt &Int,
1947*12c85518Srobert const llvm::APSInt &Adjustment);
1948*12c85518Srobert RangeSet getSymLERange(ProgramStateRef St, SymbolRef Sym,
1949*12c85518Srobert const llvm::APSInt &Int,
1950*12c85518Srobert const llvm::APSInt &Adjustment);
1951*12c85518Srobert RangeSet getSymLERange(llvm::function_ref<RangeSet()> RS,
1952*12c85518Srobert const llvm::APSInt &Int,
1953*12c85518Srobert const llvm::APSInt &Adjustment);
1954*12c85518Srobert RangeSet getSymGERange(ProgramStateRef St, SymbolRef Sym,
1955*12c85518Srobert const llvm::APSInt &Int,
1956*12c85518Srobert const llvm::APSInt &Adjustment);
1957*12c85518Srobert };
1958*12c85518Srobert
1959a9ac8606Spatrick //===----------------------------------------------------------------------===//
1960a9ac8606Spatrick // Constraint assignment logic
1961a9ac8606Spatrick //===----------------------------------------------------------------------===//
1962a9ac8606Spatrick
1963a9ac8606Spatrick /// ConstraintAssignorBase is a small utility class that unifies visitor
1964a9ac8606Spatrick /// for ranges with a visitor for constraints (rangeset/range/constant).
1965a9ac8606Spatrick ///
1966a9ac8606Spatrick /// It is designed to have one derived class, but generally it can have more.
1967a9ac8606Spatrick /// Derived class can control which types we handle by defining methods of the
1968a9ac8606Spatrick /// following form:
1969a9ac8606Spatrick ///
1970a9ac8606Spatrick /// bool handle${SYMBOL}To${CONSTRAINT}(const SYMBOL *Sym,
1971a9ac8606Spatrick /// CONSTRAINT Constraint);
1972a9ac8606Spatrick ///
1973a9ac8606Spatrick /// where SYMBOL is the type of the symbol (e.g. SymSymExpr, SymbolCast, etc.)
1974a9ac8606Spatrick /// CONSTRAINT is the type of constraint (RangeSet/Range/Const)
1975a9ac8606Spatrick /// return value signifies whether we should try other handle methods
1976a9ac8606Spatrick /// (i.e. false would mean to stop right after calling this method)
1977a9ac8606Spatrick template <class Derived> class ConstraintAssignorBase {
1978a9ac8606Spatrick public:
1979a9ac8606Spatrick using Const = const llvm::APSInt &;
1980a9ac8606Spatrick
1981a9ac8606Spatrick #define DISPATCH(CLASS) return assign##CLASS##Impl(cast<CLASS>(Sym), Constraint)
1982a9ac8606Spatrick
1983a9ac8606Spatrick #define ASSIGN(CLASS, TO, SYM, CONSTRAINT) \
1984a9ac8606Spatrick if (!static_cast<Derived *>(this)->assign##CLASS##To##TO(SYM, CONSTRAINT)) \
1985a9ac8606Spatrick return false
1986a9ac8606Spatrick
assign(SymbolRef Sym,RangeSet Constraint)1987a9ac8606Spatrick void assign(SymbolRef Sym, RangeSet Constraint) {
1988a9ac8606Spatrick assignImpl(Sym, Constraint);
1989a9ac8606Spatrick }
1990a9ac8606Spatrick
assignImpl(SymbolRef Sym,RangeSet Constraint)1991a9ac8606Spatrick bool assignImpl(SymbolRef Sym, RangeSet Constraint) {
1992a9ac8606Spatrick switch (Sym->getKind()) {
1993a9ac8606Spatrick #define SYMBOL(Id, Parent) \
1994a9ac8606Spatrick case SymExpr::Id##Kind: \
1995a9ac8606Spatrick DISPATCH(Id);
1996a9ac8606Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/Symbols.def"
1997a9ac8606Spatrick }
1998a9ac8606Spatrick llvm_unreachable("Unknown SymExpr kind!");
1999a9ac8606Spatrick }
2000a9ac8606Spatrick
2001a9ac8606Spatrick #define DEFAULT_ASSIGN(Id) \
2002a9ac8606Spatrick bool assign##Id##To##RangeSet(const Id *Sym, RangeSet Constraint) { \
2003a9ac8606Spatrick return true; \
2004a9ac8606Spatrick } \
2005a9ac8606Spatrick bool assign##Id##To##Range(const Id *Sym, Range Constraint) { return true; } \
2006a9ac8606Spatrick bool assign##Id##To##Const(const Id *Sym, Const Constraint) { return true; }
2007a9ac8606Spatrick
2008a9ac8606Spatrick // When we dispatch for constraint types, we first try to check
2009a9ac8606Spatrick // if the new constraint is the constant and try the corresponding
2010a9ac8606Spatrick // assignor methods. If it didn't interrupt, we can proceed to the
2011a9ac8606Spatrick // range, and finally to the range set.
2012a9ac8606Spatrick #define CONSTRAINT_DISPATCH(Id) \
2013a9ac8606Spatrick if (const llvm::APSInt *Const = Constraint.getConcreteValue()) { \
2014a9ac8606Spatrick ASSIGN(Id, Const, Sym, *Const); \
2015a9ac8606Spatrick } \
2016a9ac8606Spatrick if (Constraint.size() == 1) { \
2017a9ac8606Spatrick ASSIGN(Id, Range, Sym, *Constraint.begin()); \
2018a9ac8606Spatrick } \
2019a9ac8606Spatrick ASSIGN(Id, RangeSet, Sym, Constraint)
2020a9ac8606Spatrick
2021a9ac8606Spatrick // Our internal assign method first tries to call assignor methods for all
2022a9ac8606Spatrick // constraint types that apply. And if not interrupted, continues with its
2023a9ac8606Spatrick // parent class.
2024a9ac8606Spatrick #define SYMBOL(Id, Parent) \
2025a9ac8606Spatrick bool assign##Id##Impl(const Id *Sym, RangeSet Constraint) { \
2026a9ac8606Spatrick CONSTRAINT_DISPATCH(Id); \
2027a9ac8606Spatrick DISPATCH(Parent); \
2028a9ac8606Spatrick } \
2029a9ac8606Spatrick DEFAULT_ASSIGN(Id)
2030a9ac8606Spatrick #define ABSTRACT_SYMBOL(Id, Parent) SYMBOL(Id, Parent)
2031a9ac8606Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/Symbols.def"
2032a9ac8606Spatrick
2033a9ac8606Spatrick // Default implementations for the top class that doesn't have parents.
assignSymExprImpl(const SymExpr * Sym,RangeSet Constraint)2034a9ac8606Spatrick bool assignSymExprImpl(const SymExpr *Sym, RangeSet Constraint) {
2035a9ac8606Spatrick CONSTRAINT_DISPATCH(SymExpr);
2036a9ac8606Spatrick return true;
2037a9ac8606Spatrick }
2038a9ac8606Spatrick DEFAULT_ASSIGN(SymExpr);
2039a9ac8606Spatrick
2040a9ac8606Spatrick #undef DISPATCH
2041a9ac8606Spatrick #undef CONSTRAINT_DISPATCH
2042a9ac8606Spatrick #undef DEFAULT_ASSIGN
2043a9ac8606Spatrick #undef ASSIGN
2044a9ac8606Spatrick };
2045a9ac8606Spatrick
2046a9ac8606Spatrick /// A little component aggregating all of the reasoning we have about
2047a9ac8606Spatrick /// assigning new constraints to symbols.
2048a9ac8606Spatrick ///
2049a9ac8606Spatrick /// The main purpose of this class is to associate constraints to symbols,
2050a9ac8606Spatrick /// and impose additional constraints on other symbols, when we can imply
2051a9ac8606Spatrick /// them.
2052a9ac8606Spatrick ///
2053a9ac8606Spatrick /// It has a nice symmetry with SymbolicRangeInferrer. When the latter
2054a9ac8606Spatrick /// can provide more precise ranges by looking into the operands of the
2055a9ac8606Spatrick /// expression in question, ConstraintAssignor looks into the operands
2056a9ac8606Spatrick /// to see if we can imply more from the new constraint.
2057a9ac8606Spatrick class ConstraintAssignor : public ConstraintAssignorBase<ConstraintAssignor> {
2058a9ac8606Spatrick public:
2059a9ac8606Spatrick template <class ClassOrSymbol>
2060*12c85518Srobert [[nodiscard]] static ProgramStateRef
assign(ProgramStateRef State,SValBuilder & Builder,RangeSet::Factory & F,ClassOrSymbol CoS,RangeSet NewConstraint)2061a9ac8606Spatrick assign(ProgramStateRef State, SValBuilder &Builder, RangeSet::Factory &F,
2062a9ac8606Spatrick ClassOrSymbol CoS, RangeSet NewConstraint) {
2063a9ac8606Spatrick if (!State || NewConstraint.isEmpty())
2064a9ac8606Spatrick return nullptr;
2065a9ac8606Spatrick
2066a9ac8606Spatrick ConstraintAssignor Assignor{State, Builder, F};
2067a9ac8606Spatrick return Assignor.assign(CoS, NewConstraint);
2068a9ac8606Spatrick }
2069a9ac8606Spatrick
2070*12c85518Srobert /// Handle expressions like: a % b != 0.
2071*12c85518Srobert template <typename SymT>
handleRemainderOp(const SymT * Sym,RangeSet Constraint)2072*12c85518Srobert bool handleRemainderOp(const SymT *Sym, RangeSet Constraint) {
2073*12c85518Srobert if (Sym->getOpcode() != BO_Rem)
2074*12c85518Srobert return true;
2075*12c85518Srobert // a % b != 0 implies that a != 0.
2076*12c85518Srobert if (!Constraint.containsZero()) {
2077*12c85518Srobert SVal SymSVal = Builder.makeSymbolVal(Sym->getLHS());
2078*12c85518Srobert if (auto NonLocSymSVal = SymSVal.getAs<nonloc::SymbolVal>()) {
2079*12c85518Srobert State = State->assume(*NonLocSymSVal, true);
2080*12c85518Srobert if (!State)
2081*12c85518Srobert return false;
2082*12c85518Srobert }
2083*12c85518Srobert }
2084*12c85518Srobert return true;
2085*12c85518Srobert }
2086*12c85518Srobert
2087a9ac8606Spatrick inline bool assignSymExprToConst(const SymExpr *Sym, Const Constraint);
assignSymIntExprToRangeSet(const SymIntExpr * Sym,RangeSet Constraint)2088*12c85518Srobert inline bool assignSymIntExprToRangeSet(const SymIntExpr *Sym,
2089*12c85518Srobert RangeSet Constraint) {
2090*12c85518Srobert return handleRemainderOp(Sym, Constraint);
2091*12c85518Srobert }
2092a9ac8606Spatrick inline bool assignSymSymExprToRangeSet(const SymSymExpr *Sym,
2093a9ac8606Spatrick RangeSet Constraint);
2094a9ac8606Spatrick
2095a9ac8606Spatrick private:
ConstraintAssignor(ProgramStateRef State,SValBuilder & Builder,RangeSet::Factory & F)2096a9ac8606Spatrick ConstraintAssignor(ProgramStateRef State, SValBuilder &Builder,
2097a9ac8606Spatrick RangeSet::Factory &F)
2098a9ac8606Spatrick : State(State), Builder(Builder), RangeFactory(F) {}
2099a9ac8606Spatrick using Base = ConstraintAssignorBase<ConstraintAssignor>;
2100a9ac8606Spatrick
2101a9ac8606Spatrick /// Base method for handling new constraints for symbols.
assign(SymbolRef Sym,RangeSet NewConstraint)2102*12c85518Srobert [[nodiscard]] ProgramStateRef assign(SymbolRef Sym, RangeSet NewConstraint) {
2103a9ac8606Spatrick // All constraints are actually associated with equivalence classes, and
2104a9ac8606Spatrick // that's what we are going to do first.
2105a9ac8606Spatrick State = assign(EquivalenceClass::find(State, Sym), NewConstraint);
2106a9ac8606Spatrick if (!State)
2107a9ac8606Spatrick return nullptr;
2108a9ac8606Spatrick
2109a9ac8606Spatrick // And after that we can check what other things we can get from this
2110a9ac8606Spatrick // constraint.
2111a9ac8606Spatrick Base::assign(Sym, NewConstraint);
2112a9ac8606Spatrick return State;
2113a9ac8606Spatrick }
2114a9ac8606Spatrick
2115a9ac8606Spatrick /// Base method for handling new constraints for classes.
assign(EquivalenceClass Class,RangeSet NewConstraint)2116*12c85518Srobert [[nodiscard]] ProgramStateRef assign(EquivalenceClass Class,
2117a9ac8606Spatrick RangeSet NewConstraint) {
2118a9ac8606Spatrick // There is a chance that we might need to update constraints for the
2119a9ac8606Spatrick // classes that are known to be disequal to Class.
2120a9ac8606Spatrick //
2121a9ac8606Spatrick // In order for this to be even possible, the new constraint should
2122a9ac8606Spatrick // be simply a constant because we can't reason about range disequalities.
2123a9ac8606Spatrick if (const llvm::APSInt *Point = NewConstraint.getConcreteValue()) {
2124a9ac8606Spatrick
2125a9ac8606Spatrick ConstraintRangeTy Constraints = State->get<ConstraintRange>();
2126a9ac8606Spatrick ConstraintRangeTy::Factory &CF = State->get_context<ConstraintRange>();
2127a9ac8606Spatrick
2128a9ac8606Spatrick // Add new constraint.
2129a9ac8606Spatrick Constraints = CF.add(Constraints, Class, NewConstraint);
2130a9ac8606Spatrick
2131a9ac8606Spatrick for (EquivalenceClass DisequalClass : Class.getDisequalClasses(State)) {
2132a9ac8606Spatrick RangeSet UpdatedConstraint = SymbolicRangeInferrer::inferRange(
2133a9ac8606Spatrick RangeFactory, State, DisequalClass);
2134a9ac8606Spatrick
2135a9ac8606Spatrick UpdatedConstraint = RangeFactory.deletePoint(UpdatedConstraint, *Point);
2136a9ac8606Spatrick
2137a9ac8606Spatrick // If we end up with at least one of the disequal classes to be
2138a9ac8606Spatrick // constrained with an empty range-set, the state is infeasible.
2139a9ac8606Spatrick if (UpdatedConstraint.isEmpty())
2140a9ac8606Spatrick return nullptr;
2141a9ac8606Spatrick
2142a9ac8606Spatrick Constraints = CF.add(Constraints, DisequalClass, UpdatedConstraint);
2143a9ac8606Spatrick }
2144a9ac8606Spatrick assert(areFeasible(Constraints) && "Constraint manager shouldn't produce "
2145a9ac8606Spatrick "a state with infeasible constraints");
2146a9ac8606Spatrick
2147a9ac8606Spatrick return setConstraints(State, Constraints);
2148a9ac8606Spatrick }
2149a9ac8606Spatrick
2150a9ac8606Spatrick return setConstraint(State, Class, NewConstraint);
2151a9ac8606Spatrick }
2152a9ac8606Spatrick
trackDisequality(ProgramStateRef State,SymbolRef LHS,SymbolRef RHS)2153a9ac8606Spatrick ProgramStateRef trackDisequality(ProgramStateRef State, SymbolRef LHS,
2154a9ac8606Spatrick SymbolRef RHS) {
2155a9ac8606Spatrick return EquivalenceClass::markDisequal(RangeFactory, State, LHS, RHS);
2156a9ac8606Spatrick }
2157a9ac8606Spatrick
trackEquality(ProgramStateRef State,SymbolRef LHS,SymbolRef RHS)2158a9ac8606Spatrick ProgramStateRef trackEquality(ProgramStateRef State, SymbolRef LHS,
2159a9ac8606Spatrick SymbolRef RHS) {
2160a9ac8606Spatrick return EquivalenceClass::merge(RangeFactory, State, LHS, RHS);
2161a9ac8606Spatrick }
2162a9ac8606Spatrick
interpreteAsBool(RangeSet Constraint)2163*12c85518Srobert [[nodiscard]] std::optional<bool> interpreteAsBool(RangeSet Constraint) {
2164a9ac8606Spatrick assert(!Constraint.isEmpty() && "Empty ranges shouldn't get here");
2165a9ac8606Spatrick
2166a9ac8606Spatrick if (Constraint.getConcreteValue())
2167*12c85518Srobert return !Constraint.getConcreteValue()->isZero();
2168a9ac8606Spatrick
2169*12c85518Srobert if (!Constraint.containsZero())
2170a9ac8606Spatrick return true;
2171a9ac8606Spatrick
2172*12c85518Srobert return std::nullopt;
2173a9ac8606Spatrick }
2174a9ac8606Spatrick
2175a9ac8606Spatrick ProgramStateRef State;
2176a9ac8606Spatrick SValBuilder &Builder;
2177a9ac8606Spatrick RangeSet::Factory &RangeFactory;
2178a9ac8606Spatrick };
2179a9ac8606Spatrick
assignSymExprToConst(const SymExpr * Sym,const llvm::APSInt & Constraint)2180a9ac8606Spatrick bool ConstraintAssignor::assignSymExprToConst(const SymExpr *Sym,
2181a9ac8606Spatrick const llvm::APSInt &Constraint) {
2182a9ac8606Spatrick llvm::SmallSet<EquivalenceClass, 4> SimplifiedClasses;
2183a9ac8606Spatrick // Iterate over all equivalence classes and try to simplify them.
2184a9ac8606Spatrick ClassMembersTy Members = State->get<ClassMembers>();
2185a9ac8606Spatrick for (std::pair<EquivalenceClass, SymbolSet> ClassToSymbolSet : Members) {
2186a9ac8606Spatrick EquivalenceClass Class = ClassToSymbolSet.first;
2187a9ac8606Spatrick State = EquivalenceClass::simplify(Builder, RangeFactory, State, Class);
2188a9ac8606Spatrick if (!State)
2189a9ac8606Spatrick return false;
2190a9ac8606Spatrick SimplifiedClasses.insert(Class);
2191a9ac8606Spatrick }
2192a9ac8606Spatrick
2193a9ac8606Spatrick // Trivial equivalence classes (those that have only one symbol member) are
2194a9ac8606Spatrick // not stored in the State. Thus, we must skim through the constraints as
2195a9ac8606Spatrick // well. And we try to simplify symbols in the constraints.
2196a9ac8606Spatrick ConstraintRangeTy Constraints = State->get<ConstraintRange>();
2197a9ac8606Spatrick for (std::pair<EquivalenceClass, RangeSet> ClassConstraint : Constraints) {
2198a9ac8606Spatrick EquivalenceClass Class = ClassConstraint.first;
2199a9ac8606Spatrick if (SimplifiedClasses.count(Class)) // Already simplified.
2200a9ac8606Spatrick continue;
2201a9ac8606Spatrick State = EquivalenceClass::simplify(Builder, RangeFactory, State, Class);
2202a9ac8606Spatrick if (!State)
2203a9ac8606Spatrick return false;
2204a9ac8606Spatrick }
2205a9ac8606Spatrick
2206*12c85518Srobert // We may have trivial equivalence classes in the disequality info as
2207*12c85518Srobert // well, and we need to simplify them.
2208*12c85518Srobert DisequalityMapTy DisequalityInfo = State->get<DisequalityMap>();
2209*12c85518Srobert for (std::pair<EquivalenceClass, ClassSet> DisequalityEntry :
2210*12c85518Srobert DisequalityInfo) {
2211*12c85518Srobert EquivalenceClass Class = DisequalityEntry.first;
2212*12c85518Srobert ClassSet DisequalClasses = DisequalityEntry.second;
2213*12c85518Srobert State = EquivalenceClass::simplify(Builder, RangeFactory, State, Class);
2214*12c85518Srobert if (!State)
2215*12c85518Srobert return false;
2216*12c85518Srobert }
2217*12c85518Srobert
2218a9ac8606Spatrick return true;
2219a9ac8606Spatrick }
2220a9ac8606Spatrick
assignSymSymExprToRangeSet(const SymSymExpr * Sym,RangeSet Constraint)2221a9ac8606Spatrick bool ConstraintAssignor::assignSymSymExprToRangeSet(const SymSymExpr *Sym,
2222a9ac8606Spatrick RangeSet Constraint) {
2223*12c85518Srobert if (!handleRemainderOp(Sym, Constraint))
2224*12c85518Srobert return false;
2225*12c85518Srobert
2226*12c85518Srobert std::optional<bool> ConstraintAsBool = interpreteAsBool(Constraint);
2227a9ac8606Spatrick
2228a9ac8606Spatrick if (!ConstraintAsBool)
2229a9ac8606Spatrick return true;
2230a9ac8606Spatrick
2231*12c85518Srobert if (std::optional<bool> Equality = meansEquality(Sym)) {
2232a9ac8606Spatrick // Here we cover two cases:
2233a9ac8606Spatrick // * if Sym is equality and the new constraint is true -> Sym's operands
2234a9ac8606Spatrick // should be marked as equal
2235a9ac8606Spatrick // * if Sym is disequality and the new constraint is false -> Sym's
2236a9ac8606Spatrick // operands should be also marked as equal
2237a9ac8606Spatrick if (*Equality == *ConstraintAsBool) {
2238a9ac8606Spatrick State = trackEquality(State, Sym->getLHS(), Sym->getRHS());
2239a9ac8606Spatrick } else {
2240a9ac8606Spatrick // Other combinations leave as with disequal operands.
2241a9ac8606Spatrick State = trackDisequality(State, Sym->getLHS(), Sym->getRHS());
2242a9ac8606Spatrick }
2243a9ac8606Spatrick
2244a9ac8606Spatrick if (!State)
2245a9ac8606Spatrick return false;
2246a9ac8606Spatrick }
2247a9ac8606Spatrick
2248a9ac8606Spatrick return true;
2249a9ac8606Spatrick }
2250a9ac8606Spatrick
2251e5dd7070Spatrick } // end anonymous namespace
2252e5dd7070Spatrick
2253e5dd7070Spatrick std::unique_ptr<ConstraintManager>
CreateRangeConstraintManager(ProgramStateManager & StMgr,ExprEngine * Eng)2254ec727ea7Spatrick ento::CreateRangeConstraintManager(ProgramStateManager &StMgr,
2255ec727ea7Spatrick ExprEngine *Eng) {
2256e5dd7070Spatrick return std::make_unique<RangeConstraintManager>(Eng, StMgr.getSValBuilder());
2257e5dd7070Spatrick }
2258e5dd7070Spatrick
getConstraintMap(ProgramStateRef State)2259a9ac8606Spatrick ConstraintMap ento::getConstraintMap(ProgramStateRef State) {
2260a9ac8606Spatrick ConstraintMap::Factory &F = State->get_context<ConstraintMap>();
2261a9ac8606Spatrick ConstraintMap Result = F.getEmptyMap();
2262a9ac8606Spatrick
2263a9ac8606Spatrick ConstraintRangeTy Constraints = State->get<ConstraintRange>();
2264a9ac8606Spatrick for (std::pair<EquivalenceClass, RangeSet> ClassConstraint : Constraints) {
2265a9ac8606Spatrick EquivalenceClass Class = ClassConstraint.first;
2266a9ac8606Spatrick SymbolSet ClassMembers = Class.getClassMembers(State);
2267a9ac8606Spatrick assert(!ClassMembers.isEmpty() &&
2268a9ac8606Spatrick "Class must always have at least one member!");
2269a9ac8606Spatrick
2270a9ac8606Spatrick SymbolRef Representative = *ClassMembers.begin();
2271a9ac8606Spatrick Result = F.add(Result, Representative, ClassConstraint.second);
2272a9ac8606Spatrick }
2273a9ac8606Spatrick
2274a9ac8606Spatrick return Result;
2275a9ac8606Spatrick }
2276a9ac8606Spatrick
2277a9ac8606Spatrick //===----------------------------------------------------------------------===//
2278a9ac8606Spatrick // EqualityClass implementation details
2279a9ac8606Spatrick //===----------------------------------------------------------------------===//
2280a9ac8606Spatrick
dumpToStream(ProgramStateRef State,raw_ostream & os) const2281a9ac8606Spatrick LLVM_DUMP_METHOD void EquivalenceClass::dumpToStream(ProgramStateRef State,
2282a9ac8606Spatrick raw_ostream &os) const {
2283a9ac8606Spatrick SymbolSet ClassMembers = getClassMembers(State);
2284a9ac8606Spatrick for (const SymbolRef &MemberSym : ClassMembers) {
2285a9ac8606Spatrick MemberSym->dump();
2286a9ac8606Spatrick os << "\n";
2287a9ac8606Spatrick }
2288a9ac8606Spatrick }
2289a9ac8606Spatrick
find(ProgramStateRef State,SymbolRef Sym)2290a9ac8606Spatrick inline EquivalenceClass EquivalenceClass::find(ProgramStateRef State,
2291a9ac8606Spatrick SymbolRef Sym) {
2292a9ac8606Spatrick assert(State && "State should not be null");
2293a9ac8606Spatrick assert(Sym && "Symbol should not be null");
2294a9ac8606Spatrick // We store far from all Symbol -> Class mappings
2295a9ac8606Spatrick if (const EquivalenceClass *NontrivialClass = State->get<ClassMap>(Sym))
2296a9ac8606Spatrick return *NontrivialClass;
2297a9ac8606Spatrick
2298a9ac8606Spatrick // This is a trivial class of Sym.
2299a9ac8606Spatrick return Sym;
2300a9ac8606Spatrick }
2301a9ac8606Spatrick
merge(RangeSet::Factory & F,ProgramStateRef State,SymbolRef First,SymbolRef Second)2302a9ac8606Spatrick inline ProgramStateRef EquivalenceClass::merge(RangeSet::Factory &F,
2303a9ac8606Spatrick ProgramStateRef State,
2304a9ac8606Spatrick SymbolRef First,
2305a9ac8606Spatrick SymbolRef Second) {
2306a9ac8606Spatrick EquivalenceClass FirstClass = find(State, First);
2307a9ac8606Spatrick EquivalenceClass SecondClass = find(State, Second);
2308a9ac8606Spatrick
2309a9ac8606Spatrick return FirstClass.merge(F, State, SecondClass);
2310a9ac8606Spatrick }
2311a9ac8606Spatrick
merge(RangeSet::Factory & F,ProgramStateRef State,EquivalenceClass Other)2312a9ac8606Spatrick inline ProgramStateRef EquivalenceClass::merge(RangeSet::Factory &F,
2313a9ac8606Spatrick ProgramStateRef State,
2314a9ac8606Spatrick EquivalenceClass Other) {
2315a9ac8606Spatrick // It is already the same class.
2316a9ac8606Spatrick if (*this == Other)
2317a9ac8606Spatrick return State;
2318a9ac8606Spatrick
2319a9ac8606Spatrick // FIXME: As of now, we support only equivalence classes of the same type.
2320a9ac8606Spatrick // This limitation is connected to the lack of explicit casts in
2321a9ac8606Spatrick // our symbolic expression model.
2322a9ac8606Spatrick //
2323a9ac8606Spatrick // That means that for `int x` and `char y` we don't distinguish
2324a9ac8606Spatrick // between these two very different cases:
2325a9ac8606Spatrick // * `x == y`
2326a9ac8606Spatrick // * `(char)x == y`
2327a9ac8606Spatrick //
2328a9ac8606Spatrick // The moment we introduce symbolic casts, this restriction can be
2329a9ac8606Spatrick // lifted.
2330a9ac8606Spatrick if (getType() != Other.getType())
2331a9ac8606Spatrick return State;
2332a9ac8606Spatrick
2333a9ac8606Spatrick SymbolSet Members = getClassMembers(State);
2334a9ac8606Spatrick SymbolSet OtherMembers = Other.getClassMembers(State);
2335a9ac8606Spatrick
2336a9ac8606Spatrick // We estimate the size of the class by the height of tree containing
2337a9ac8606Spatrick // its members. Merging is not a trivial operation, so it's easier to
2338a9ac8606Spatrick // merge the smaller class into the bigger one.
2339a9ac8606Spatrick if (Members.getHeight() >= OtherMembers.getHeight()) {
2340a9ac8606Spatrick return mergeImpl(F, State, Members, Other, OtherMembers);
2341a9ac8606Spatrick } else {
2342a9ac8606Spatrick return Other.mergeImpl(F, State, OtherMembers, *this, Members);
2343a9ac8606Spatrick }
2344a9ac8606Spatrick }
2345a9ac8606Spatrick
2346a9ac8606Spatrick inline ProgramStateRef
mergeImpl(RangeSet::Factory & RangeFactory,ProgramStateRef State,SymbolSet MyMembers,EquivalenceClass Other,SymbolSet OtherMembers)2347a9ac8606Spatrick EquivalenceClass::mergeImpl(RangeSet::Factory &RangeFactory,
2348a9ac8606Spatrick ProgramStateRef State, SymbolSet MyMembers,
2349a9ac8606Spatrick EquivalenceClass Other, SymbolSet OtherMembers) {
2350a9ac8606Spatrick // Essentially what we try to recreate here is some kind of union-find
2351a9ac8606Spatrick // data structure. It does have certain limitations due to persistence
2352a9ac8606Spatrick // and the need to remove elements from classes.
2353a9ac8606Spatrick //
2354a9ac8606Spatrick // In this setting, EquialityClass object is the representative of the class
2355a9ac8606Spatrick // or the parent element. ClassMap is a mapping of class members to their
2356a9ac8606Spatrick // parent. Unlike the union-find structure, they all point directly to the
2357a9ac8606Spatrick // class representative because we don't have an opportunity to actually do
2358a9ac8606Spatrick // path compression when dealing with immutability. This means that we
2359a9ac8606Spatrick // compress paths every time we do merges. It also means that we lose
2360a9ac8606Spatrick // the main amortized complexity benefit from the original data structure.
2361a9ac8606Spatrick ConstraintRangeTy Constraints = State->get<ConstraintRange>();
2362a9ac8606Spatrick ConstraintRangeTy::Factory &CRF = State->get_context<ConstraintRange>();
2363a9ac8606Spatrick
2364a9ac8606Spatrick // 1. If the merged classes have any constraints associated with them, we
2365a9ac8606Spatrick // need to transfer them to the class we have left.
2366a9ac8606Spatrick //
2367a9ac8606Spatrick // Intersection here makes perfect sense because both of these constraints
2368a9ac8606Spatrick // must hold for the whole new class.
2369*12c85518Srobert if (std::optional<RangeSet> NewClassConstraint =
2370a9ac8606Spatrick intersect(RangeFactory, getConstraint(State, *this),
2371a9ac8606Spatrick getConstraint(State, Other))) {
2372a9ac8606Spatrick // NOTE: Essentially, NewClassConstraint should NEVER be infeasible because
2373a9ac8606Spatrick // range inferrer shouldn't generate ranges incompatible with
2374a9ac8606Spatrick // equivalence classes. However, at the moment, due to imperfections
2375a9ac8606Spatrick // in the solver, it is possible and the merge function can also
2376a9ac8606Spatrick // return infeasible states aka null states.
2377a9ac8606Spatrick if (NewClassConstraint->isEmpty())
2378a9ac8606Spatrick // Infeasible state
2379a9ac8606Spatrick return nullptr;
2380a9ac8606Spatrick
2381a9ac8606Spatrick // No need in tracking constraints of a now-dissolved class.
2382a9ac8606Spatrick Constraints = CRF.remove(Constraints, Other);
2383a9ac8606Spatrick // Assign new constraints for this class.
2384a9ac8606Spatrick Constraints = CRF.add(Constraints, *this, *NewClassConstraint);
2385a9ac8606Spatrick
2386a9ac8606Spatrick assert(areFeasible(Constraints) && "Constraint manager shouldn't produce "
2387a9ac8606Spatrick "a state with infeasible constraints");
2388a9ac8606Spatrick
2389a9ac8606Spatrick State = State->set<ConstraintRange>(Constraints);
2390a9ac8606Spatrick }
2391a9ac8606Spatrick
2392a9ac8606Spatrick // 2. Get ALL equivalence-related maps
2393a9ac8606Spatrick ClassMapTy Classes = State->get<ClassMap>();
2394a9ac8606Spatrick ClassMapTy::Factory &CMF = State->get_context<ClassMap>();
2395a9ac8606Spatrick
2396a9ac8606Spatrick ClassMembersTy Members = State->get<ClassMembers>();
2397a9ac8606Spatrick ClassMembersTy::Factory &MF = State->get_context<ClassMembers>();
2398a9ac8606Spatrick
2399a9ac8606Spatrick DisequalityMapTy DisequalityInfo = State->get<DisequalityMap>();
2400a9ac8606Spatrick DisequalityMapTy::Factory &DF = State->get_context<DisequalityMap>();
2401a9ac8606Spatrick
2402a9ac8606Spatrick ClassSet::Factory &CF = State->get_context<ClassSet>();
2403a9ac8606Spatrick SymbolSet::Factory &F = getMembersFactory(State);
2404a9ac8606Spatrick
2405a9ac8606Spatrick // 2. Merge members of the Other class into the current class.
2406a9ac8606Spatrick SymbolSet NewClassMembers = MyMembers;
2407a9ac8606Spatrick for (SymbolRef Sym : OtherMembers) {
2408a9ac8606Spatrick NewClassMembers = F.add(NewClassMembers, Sym);
2409a9ac8606Spatrick // *this is now the class for all these new symbols.
2410a9ac8606Spatrick Classes = CMF.add(Classes, Sym, *this);
2411a9ac8606Spatrick }
2412a9ac8606Spatrick
2413a9ac8606Spatrick // 3. Adjust member mapping.
2414a9ac8606Spatrick //
2415a9ac8606Spatrick // No need in tracking members of a now-dissolved class.
2416a9ac8606Spatrick Members = MF.remove(Members, Other);
2417a9ac8606Spatrick // Now only the current class is mapped to all the symbols.
2418a9ac8606Spatrick Members = MF.add(Members, *this, NewClassMembers);
2419a9ac8606Spatrick
2420a9ac8606Spatrick // 4. Update disequality relations
2421a9ac8606Spatrick ClassSet DisequalToOther = Other.getDisequalClasses(DisequalityInfo, CF);
2422a9ac8606Spatrick // We are about to merge two classes but they are already known to be
2423a9ac8606Spatrick // non-equal. This is a contradiction.
2424a9ac8606Spatrick if (DisequalToOther.contains(*this))
2425a9ac8606Spatrick return nullptr;
2426a9ac8606Spatrick
2427a9ac8606Spatrick if (!DisequalToOther.isEmpty()) {
2428a9ac8606Spatrick ClassSet DisequalToThis = getDisequalClasses(DisequalityInfo, CF);
2429a9ac8606Spatrick DisequalityInfo = DF.remove(DisequalityInfo, Other);
2430a9ac8606Spatrick
2431a9ac8606Spatrick for (EquivalenceClass DisequalClass : DisequalToOther) {
2432a9ac8606Spatrick DisequalToThis = CF.add(DisequalToThis, DisequalClass);
2433a9ac8606Spatrick
2434a9ac8606Spatrick // Disequality is a symmetric relation meaning that if
2435a9ac8606Spatrick // DisequalToOther not null then the set for DisequalClass is not
2436a9ac8606Spatrick // empty and has at least Other.
2437a9ac8606Spatrick ClassSet OriginalSetLinkedToOther =
2438a9ac8606Spatrick *DisequalityInfo.lookup(DisequalClass);
2439a9ac8606Spatrick
2440a9ac8606Spatrick // Other will be eliminated and we should replace it with the bigger
2441a9ac8606Spatrick // united class.
2442a9ac8606Spatrick ClassSet NewSet = CF.remove(OriginalSetLinkedToOther, Other);
2443a9ac8606Spatrick NewSet = CF.add(NewSet, *this);
2444a9ac8606Spatrick
2445a9ac8606Spatrick DisequalityInfo = DF.add(DisequalityInfo, DisequalClass, NewSet);
2446a9ac8606Spatrick }
2447a9ac8606Spatrick
2448a9ac8606Spatrick DisequalityInfo = DF.add(DisequalityInfo, *this, DisequalToThis);
2449a9ac8606Spatrick State = State->set<DisequalityMap>(DisequalityInfo);
2450a9ac8606Spatrick }
2451a9ac8606Spatrick
2452a9ac8606Spatrick // 5. Update the state
2453a9ac8606Spatrick State = State->set<ClassMap>(Classes);
2454a9ac8606Spatrick State = State->set<ClassMembers>(Members);
2455a9ac8606Spatrick
2456a9ac8606Spatrick return State;
2457a9ac8606Spatrick }
2458a9ac8606Spatrick
2459a9ac8606Spatrick inline SymbolSet::Factory &
getMembersFactory(ProgramStateRef State)2460a9ac8606Spatrick EquivalenceClass::getMembersFactory(ProgramStateRef State) {
2461a9ac8606Spatrick return State->get_context<SymbolSet>();
2462a9ac8606Spatrick }
2463a9ac8606Spatrick
getClassMembers(ProgramStateRef State) const2464a9ac8606Spatrick SymbolSet EquivalenceClass::getClassMembers(ProgramStateRef State) const {
2465a9ac8606Spatrick if (const SymbolSet *Members = State->get<ClassMembers>(*this))
2466a9ac8606Spatrick return *Members;
2467a9ac8606Spatrick
2468a9ac8606Spatrick // This class is trivial, so we need to construct a set
2469a9ac8606Spatrick // with just that one symbol from the class.
2470a9ac8606Spatrick SymbolSet::Factory &F = getMembersFactory(State);
2471a9ac8606Spatrick return F.add(F.getEmptySet(), getRepresentativeSymbol());
2472a9ac8606Spatrick }
2473a9ac8606Spatrick
isTrivial(ProgramStateRef State) const2474a9ac8606Spatrick bool EquivalenceClass::isTrivial(ProgramStateRef State) const {
2475a9ac8606Spatrick return State->get<ClassMembers>(*this) == nullptr;
2476a9ac8606Spatrick }
2477a9ac8606Spatrick
isTriviallyDead(ProgramStateRef State,SymbolReaper & Reaper) const2478a9ac8606Spatrick bool EquivalenceClass::isTriviallyDead(ProgramStateRef State,
2479a9ac8606Spatrick SymbolReaper &Reaper) const {
2480a9ac8606Spatrick return isTrivial(State) && Reaper.isDead(getRepresentativeSymbol());
2481a9ac8606Spatrick }
2482a9ac8606Spatrick
markDisequal(RangeSet::Factory & RF,ProgramStateRef State,SymbolRef First,SymbolRef Second)2483a9ac8606Spatrick inline ProgramStateRef EquivalenceClass::markDisequal(RangeSet::Factory &RF,
2484a9ac8606Spatrick ProgramStateRef State,
2485a9ac8606Spatrick SymbolRef First,
2486a9ac8606Spatrick SymbolRef Second) {
2487a9ac8606Spatrick return markDisequal(RF, State, find(State, First), find(State, Second));
2488a9ac8606Spatrick }
2489a9ac8606Spatrick
markDisequal(RangeSet::Factory & RF,ProgramStateRef State,EquivalenceClass First,EquivalenceClass Second)2490a9ac8606Spatrick inline ProgramStateRef EquivalenceClass::markDisequal(RangeSet::Factory &RF,
2491a9ac8606Spatrick ProgramStateRef State,
2492a9ac8606Spatrick EquivalenceClass First,
2493a9ac8606Spatrick EquivalenceClass Second) {
2494a9ac8606Spatrick return First.markDisequal(RF, State, Second);
2495a9ac8606Spatrick }
2496a9ac8606Spatrick
2497a9ac8606Spatrick inline ProgramStateRef
markDisequal(RangeSet::Factory & RF,ProgramStateRef State,EquivalenceClass Other) const2498a9ac8606Spatrick EquivalenceClass::markDisequal(RangeSet::Factory &RF, ProgramStateRef State,
2499a9ac8606Spatrick EquivalenceClass Other) const {
2500a9ac8606Spatrick // If we know that two classes are equal, we can only produce an infeasible
2501a9ac8606Spatrick // state.
2502a9ac8606Spatrick if (*this == Other) {
2503a9ac8606Spatrick return nullptr;
2504a9ac8606Spatrick }
2505a9ac8606Spatrick
2506a9ac8606Spatrick DisequalityMapTy DisequalityInfo = State->get<DisequalityMap>();
2507a9ac8606Spatrick ConstraintRangeTy Constraints = State->get<ConstraintRange>();
2508a9ac8606Spatrick
2509a9ac8606Spatrick // Disequality is a symmetric relation, so if we mark A as disequal to B,
2510a9ac8606Spatrick // we should also mark B as disequalt to A.
2511a9ac8606Spatrick if (!addToDisequalityInfo(DisequalityInfo, Constraints, RF, State, *this,
2512a9ac8606Spatrick Other) ||
2513a9ac8606Spatrick !addToDisequalityInfo(DisequalityInfo, Constraints, RF, State, Other,
2514a9ac8606Spatrick *this))
2515a9ac8606Spatrick return nullptr;
2516a9ac8606Spatrick
2517a9ac8606Spatrick assert(areFeasible(Constraints) && "Constraint manager shouldn't produce "
2518a9ac8606Spatrick "a state with infeasible constraints");
2519a9ac8606Spatrick
2520a9ac8606Spatrick State = State->set<DisequalityMap>(DisequalityInfo);
2521a9ac8606Spatrick State = State->set<ConstraintRange>(Constraints);
2522a9ac8606Spatrick
2523a9ac8606Spatrick return State;
2524a9ac8606Spatrick }
2525a9ac8606Spatrick
addToDisequalityInfo(DisequalityMapTy & Info,ConstraintRangeTy & Constraints,RangeSet::Factory & RF,ProgramStateRef State,EquivalenceClass First,EquivalenceClass Second)2526a9ac8606Spatrick inline bool EquivalenceClass::addToDisequalityInfo(
2527a9ac8606Spatrick DisequalityMapTy &Info, ConstraintRangeTy &Constraints,
2528a9ac8606Spatrick RangeSet::Factory &RF, ProgramStateRef State, EquivalenceClass First,
2529a9ac8606Spatrick EquivalenceClass Second) {
2530a9ac8606Spatrick
2531a9ac8606Spatrick // 1. Get all of the required factories.
2532a9ac8606Spatrick DisequalityMapTy::Factory &F = State->get_context<DisequalityMap>();
2533a9ac8606Spatrick ClassSet::Factory &CF = State->get_context<ClassSet>();
2534a9ac8606Spatrick ConstraintRangeTy::Factory &CRF = State->get_context<ConstraintRange>();
2535a9ac8606Spatrick
2536a9ac8606Spatrick // 2. Add Second to the set of classes disequal to First.
2537a9ac8606Spatrick const ClassSet *CurrentSet = Info.lookup(First);
2538a9ac8606Spatrick ClassSet NewSet = CurrentSet ? *CurrentSet : CF.getEmptySet();
2539a9ac8606Spatrick NewSet = CF.add(NewSet, Second);
2540a9ac8606Spatrick
2541a9ac8606Spatrick Info = F.add(Info, First, NewSet);
2542a9ac8606Spatrick
2543a9ac8606Spatrick // 3. If Second is known to be a constant, we can delete this point
2544a9ac8606Spatrick // from the constraint asociated with First.
2545a9ac8606Spatrick //
2546a9ac8606Spatrick // So, if Second == 10, it means that First != 10.
2547a9ac8606Spatrick // At the same time, the same logic does not apply to ranges.
2548a9ac8606Spatrick if (const RangeSet *SecondConstraint = Constraints.lookup(Second))
2549a9ac8606Spatrick if (const llvm::APSInt *Point = SecondConstraint->getConcreteValue()) {
2550a9ac8606Spatrick
2551a9ac8606Spatrick RangeSet FirstConstraint = SymbolicRangeInferrer::inferRange(
2552a9ac8606Spatrick RF, State, First.getRepresentativeSymbol());
2553a9ac8606Spatrick
2554a9ac8606Spatrick FirstConstraint = RF.deletePoint(FirstConstraint, *Point);
2555a9ac8606Spatrick
2556a9ac8606Spatrick // If the First class is about to be constrained with an empty
2557a9ac8606Spatrick // range-set, the state is infeasible.
2558a9ac8606Spatrick if (FirstConstraint.isEmpty())
2559a9ac8606Spatrick return false;
2560a9ac8606Spatrick
2561a9ac8606Spatrick Constraints = CRF.add(Constraints, First, FirstConstraint);
2562a9ac8606Spatrick }
2563a9ac8606Spatrick
2564a9ac8606Spatrick return true;
2565a9ac8606Spatrick }
2566a9ac8606Spatrick
areEqual(ProgramStateRef State,SymbolRef FirstSym,SymbolRef SecondSym)2567*12c85518Srobert inline std::optional<bool> EquivalenceClass::areEqual(ProgramStateRef State,
2568a9ac8606Spatrick SymbolRef FirstSym,
2569a9ac8606Spatrick SymbolRef SecondSym) {
2570a9ac8606Spatrick return EquivalenceClass::areEqual(State, find(State, FirstSym),
2571a9ac8606Spatrick find(State, SecondSym));
2572a9ac8606Spatrick }
2573a9ac8606Spatrick
areEqual(ProgramStateRef State,EquivalenceClass First,EquivalenceClass Second)2574*12c85518Srobert inline std::optional<bool> EquivalenceClass::areEqual(ProgramStateRef State,
2575a9ac8606Spatrick EquivalenceClass First,
2576a9ac8606Spatrick EquivalenceClass Second) {
2577a9ac8606Spatrick // The same equivalence class => symbols are equal.
2578a9ac8606Spatrick if (First == Second)
2579a9ac8606Spatrick return true;
2580a9ac8606Spatrick
2581a9ac8606Spatrick // Let's check if we know anything about these two classes being not equal to
2582a9ac8606Spatrick // each other.
2583a9ac8606Spatrick ClassSet DisequalToFirst = First.getDisequalClasses(State);
2584a9ac8606Spatrick if (DisequalToFirst.contains(Second))
2585a9ac8606Spatrick return false;
2586a9ac8606Spatrick
2587a9ac8606Spatrick // It is not clear.
2588*12c85518Srobert return std::nullopt;
2589*12c85518Srobert }
2590*12c85518Srobert
2591*12c85518Srobert [[nodiscard]] ProgramStateRef
removeMember(ProgramStateRef State,const SymbolRef Old)2592*12c85518Srobert EquivalenceClass::removeMember(ProgramStateRef State, const SymbolRef Old) {
2593*12c85518Srobert
2594*12c85518Srobert SymbolSet ClsMembers = getClassMembers(State);
2595*12c85518Srobert assert(ClsMembers.contains(Old));
2596*12c85518Srobert
2597*12c85518Srobert // Remove `Old`'s Class->Sym relation.
2598*12c85518Srobert SymbolSet::Factory &F = getMembersFactory(State);
2599*12c85518Srobert ClassMembersTy::Factory &EMFactory = State->get_context<ClassMembers>();
2600*12c85518Srobert ClsMembers = F.remove(ClsMembers, Old);
2601*12c85518Srobert // Ensure another precondition of the removeMember function (we can check
2602*12c85518Srobert // this only with isEmpty, thus we have to do the remove first).
2603*12c85518Srobert assert(!ClsMembers.isEmpty() &&
2604*12c85518Srobert "Class should have had at least two members before member removal");
2605*12c85518Srobert // Overwrite the existing members assigned to this class.
2606*12c85518Srobert ClassMembersTy ClassMembersMap = State->get<ClassMembers>();
2607*12c85518Srobert ClassMembersMap = EMFactory.add(ClassMembersMap, *this, ClsMembers);
2608*12c85518Srobert State = State->set<ClassMembers>(ClassMembersMap);
2609*12c85518Srobert
2610*12c85518Srobert // Remove `Old`'s Sym->Class relation.
2611*12c85518Srobert ClassMapTy Classes = State->get<ClassMap>();
2612*12c85518Srobert ClassMapTy::Factory &CMF = State->get_context<ClassMap>();
2613*12c85518Srobert Classes = CMF.remove(Classes, Old);
2614*12c85518Srobert State = State->set<ClassMap>(Classes);
2615*12c85518Srobert
2616*12c85518Srobert return State;
2617*12c85518Srobert }
2618*12c85518Srobert
2619*12c85518Srobert // Re-evaluate an SVal with top-level `State->assume` logic.
2620*12c85518Srobert [[nodiscard]] ProgramStateRef
reAssume(ProgramStateRef State,const RangeSet * Constraint,SVal TheValue)2621*12c85518Srobert reAssume(ProgramStateRef State, const RangeSet *Constraint, SVal TheValue) {
2622*12c85518Srobert if (!Constraint)
2623*12c85518Srobert return State;
2624*12c85518Srobert
2625*12c85518Srobert const auto DefinedVal = TheValue.castAs<DefinedSVal>();
2626*12c85518Srobert
2627*12c85518Srobert // If the SVal is 0, we can simply interpret that as `false`.
2628*12c85518Srobert if (Constraint->encodesFalseRange())
2629*12c85518Srobert return State->assume(DefinedVal, false);
2630*12c85518Srobert
2631*12c85518Srobert // If the constraint does not encode 0 then we can interpret that as `true`
2632*12c85518Srobert // AND as a Range(Set).
2633*12c85518Srobert if (Constraint->encodesTrueRange()) {
2634*12c85518Srobert State = State->assume(DefinedVal, true);
2635*12c85518Srobert if (!State)
2636*12c85518Srobert return nullptr;
2637*12c85518Srobert // Fall through, re-assume based on the range values as well.
2638*12c85518Srobert }
2639*12c85518Srobert // Overestimate the individual Ranges with the RangeSet' lowest and
2640*12c85518Srobert // highest values.
2641*12c85518Srobert return State->assumeInclusiveRange(DefinedVal, Constraint->getMinValue(),
2642*12c85518Srobert Constraint->getMaxValue(), true);
2643a9ac8606Spatrick }
2644a9ac8606Spatrick
2645a9ac8606Spatrick // Iterate over all symbols and try to simplify them. Once a symbol is
2646a9ac8606Spatrick // simplified then we check if we can merge the simplified symbol's equivalence
2647a9ac8606Spatrick // class to this class. This way, we simplify not just the symbols but the
2648a9ac8606Spatrick // classes as well: we strive to keep the number of the classes to be the
2649a9ac8606Spatrick // absolute minimum.
2650*12c85518Srobert [[nodiscard]] ProgramStateRef
simplify(SValBuilder & SVB,RangeSet::Factory & F,ProgramStateRef State,EquivalenceClass Class)2651a9ac8606Spatrick EquivalenceClass::simplify(SValBuilder &SVB, RangeSet::Factory &F,
2652a9ac8606Spatrick ProgramStateRef State, EquivalenceClass Class) {
2653a9ac8606Spatrick SymbolSet ClassMembers = Class.getClassMembers(State);
2654a9ac8606Spatrick for (const SymbolRef &MemberSym : ClassMembers) {
2655*12c85518Srobert
2656*12c85518Srobert const SVal SimplifiedMemberVal = simplifyToSVal(State, MemberSym);
2657*12c85518Srobert const SymbolRef SimplifiedMemberSym = SimplifiedMemberVal.getAsSymbol();
2658*12c85518Srobert
2659*12c85518Srobert // The symbol is collapsed to a constant, check if the current State is
2660*12c85518Srobert // still feasible.
2661*12c85518Srobert if (const auto CI = SimplifiedMemberVal.getAs<nonloc::ConcreteInt>()) {
2662*12c85518Srobert const llvm::APSInt &SV = CI->getValue();
2663*12c85518Srobert const RangeSet *ClassConstraint = getConstraint(State, Class);
2664*12c85518Srobert // We have found a contradiction.
2665*12c85518Srobert if (ClassConstraint && !ClassConstraint->contains(SV))
2666*12c85518Srobert return nullptr;
2667*12c85518Srobert }
2668*12c85518Srobert
2669a9ac8606Spatrick if (SimplifiedMemberSym && MemberSym != SimplifiedMemberSym) {
2670a9ac8606Spatrick // The simplified symbol should be the member of the original Class,
2671a9ac8606Spatrick // however, it might be in another existing class at the moment. We
2672a9ac8606Spatrick // have to merge these classes.
2673*12c85518Srobert ProgramStateRef OldState = State;
2674a9ac8606Spatrick State = merge(F, State, MemberSym, SimplifiedMemberSym);
2675a9ac8606Spatrick if (!State)
2676a9ac8606Spatrick return nullptr;
2677*12c85518Srobert // No state change, no merge happened actually.
2678*12c85518Srobert if (OldState == State)
2679*12c85518Srobert continue;
2680*12c85518Srobert
2681*12c85518Srobert // Be aware that `SimplifiedMemberSym` might refer to an already dead
2682*12c85518Srobert // symbol. In that case, the eqclass of that might not be the same as the
2683*12c85518Srobert // eqclass of `MemberSym`. This is because the dead symbols are not
2684*12c85518Srobert // preserved in the `ClassMap`, hence
2685*12c85518Srobert // `find(State, SimplifiedMemberSym)` will result in a trivial eqclass
2686*12c85518Srobert // compared to the eqclass of `MemberSym`.
2687*12c85518Srobert // These eqclasses should be the same if `SimplifiedMemberSym` is alive.
2688*12c85518Srobert // --> assert(find(State, MemberSym) == find(State, SimplifiedMemberSym))
2689*12c85518Srobert //
2690*12c85518Srobert // Note that `MemberSym` must be alive here since that is from the
2691*12c85518Srobert // `ClassMembers` where all the symbols are alive.
2692*12c85518Srobert
2693*12c85518Srobert // Remove the old and more complex symbol.
2694*12c85518Srobert State = find(State, MemberSym).removeMember(State, MemberSym);
2695*12c85518Srobert
2696*12c85518Srobert // Query the class constraint again b/c that may have changed during the
2697*12c85518Srobert // merge above.
2698*12c85518Srobert const RangeSet *ClassConstraint = getConstraint(State, Class);
2699*12c85518Srobert
2700*12c85518Srobert // Re-evaluate an SVal with top-level `State->assume`, this ignites
2701*12c85518Srobert // a RECURSIVE algorithm that will reach a FIXPOINT.
2702*12c85518Srobert //
2703*12c85518Srobert // About performance and complexity: Let us assume that in a State we
2704*12c85518Srobert // have N non-trivial equivalence classes and that all constraints and
2705*12c85518Srobert // disequality info is related to non-trivial classes. In the worst case,
2706*12c85518Srobert // we can simplify only one symbol of one class in each iteration. The
2707*12c85518Srobert // number of symbols in one class cannot grow b/c we replace the old
2708*12c85518Srobert // symbol with the simplified one. Also, the number of the equivalence
2709*12c85518Srobert // classes can decrease only, b/c the algorithm does a merge operation
2710*12c85518Srobert // optionally. We need N iterations in this case to reach the fixpoint.
2711*12c85518Srobert // Thus, the steps needed to be done in the worst case is proportional to
2712*12c85518Srobert // N*N.
2713*12c85518Srobert //
2714*12c85518Srobert // This worst case scenario can be extended to that case when we have
2715*12c85518Srobert // trivial classes in the constraints and in the disequality map. This
2716*12c85518Srobert // case can be reduced to the case with a State where there are only
2717*12c85518Srobert // non-trivial classes. This is because a merge operation on two trivial
2718*12c85518Srobert // classes results in one non-trivial class.
2719*12c85518Srobert State = reAssume(State, ClassConstraint, SimplifiedMemberVal);
2720*12c85518Srobert if (!State)
2721*12c85518Srobert return nullptr;
2722a9ac8606Spatrick }
2723a9ac8606Spatrick }
2724a9ac8606Spatrick return State;
2725a9ac8606Spatrick }
2726a9ac8606Spatrick
getDisequalClasses(ProgramStateRef State,SymbolRef Sym)2727a9ac8606Spatrick inline ClassSet EquivalenceClass::getDisequalClasses(ProgramStateRef State,
2728a9ac8606Spatrick SymbolRef Sym) {
2729a9ac8606Spatrick return find(State, Sym).getDisequalClasses(State);
2730a9ac8606Spatrick }
2731a9ac8606Spatrick
2732a9ac8606Spatrick inline ClassSet
getDisequalClasses(ProgramStateRef State) const2733a9ac8606Spatrick EquivalenceClass::getDisequalClasses(ProgramStateRef State) const {
2734a9ac8606Spatrick return getDisequalClasses(State->get<DisequalityMap>(),
2735a9ac8606Spatrick State->get_context<ClassSet>());
2736a9ac8606Spatrick }
2737a9ac8606Spatrick
2738a9ac8606Spatrick inline ClassSet
getDisequalClasses(DisequalityMapTy Map,ClassSet::Factory & Factory) const2739a9ac8606Spatrick EquivalenceClass::getDisequalClasses(DisequalityMapTy Map,
2740a9ac8606Spatrick ClassSet::Factory &Factory) const {
2741a9ac8606Spatrick if (const ClassSet *DisequalClasses = Map.lookup(*this))
2742a9ac8606Spatrick return *DisequalClasses;
2743a9ac8606Spatrick
2744a9ac8606Spatrick return Factory.getEmptySet();
2745a9ac8606Spatrick }
2746a9ac8606Spatrick
isClassDataConsistent(ProgramStateRef State)2747a9ac8606Spatrick bool EquivalenceClass::isClassDataConsistent(ProgramStateRef State) {
2748a9ac8606Spatrick ClassMembersTy Members = State->get<ClassMembers>();
2749a9ac8606Spatrick
2750a9ac8606Spatrick for (std::pair<EquivalenceClass, SymbolSet> ClassMembersPair : Members) {
2751a9ac8606Spatrick for (SymbolRef Member : ClassMembersPair.second) {
2752a9ac8606Spatrick // Every member of the class should have a mapping back to the class.
2753a9ac8606Spatrick if (find(State, Member) == ClassMembersPair.first) {
2754a9ac8606Spatrick continue;
2755a9ac8606Spatrick }
2756a9ac8606Spatrick
2757a9ac8606Spatrick return false;
2758a9ac8606Spatrick }
2759a9ac8606Spatrick }
2760a9ac8606Spatrick
2761a9ac8606Spatrick DisequalityMapTy Disequalities = State->get<DisequalityMap>();
2762a9ac8606Spatrick for (std::pair<EquivalenceClass, ClassSet> DisequalityInfo : Disequalities) {
2763a9ac8606Spatrick EquivalenceClass Class = DisequalityInfo.first;
2764a9ac8606Spatrick ClassSet DisequalClasses = DisequalityInfo.second;
2765a9ac8606Spatrick
2766a9ac8606Spatrick // There is no use in keeping empty sets in the map.
2767a9ac8606Spatrick if (DisequalClasses.isEmpty())
2768a9ac8606Spatrick return false;
2769a9ac8606Spatrick
2770a9ac8606Spatrick // Disequality is symmetrical, i.e. for every Class A and B that A != B,
2771a9ac8606Spatrick // B != A should also be true.
2772a9ac8606Spatrick for (EquivalenceClass DisequalClass : DisequalClasses) {
2773a9ac8606Spatrick const ClassSet *DisequalToDisequalClasses =
2774a9ac8606Spatrick Disequalities.lookup(DisequalClass);
2775a9ac8606Spatrick
2776a9ac8606Spatrick // It should be a set of at least one element: Class
2777a9ac8606Spatrick if (!DisequalToDisequalClasses ||
2778a9ac8606Spatrick !DisequalToDisequalClasses->contains(Class))
2779a9ac8606Spatrick return false;
2780a9ac8606Spatrick }
2781a9ac8606Spatrick }
2782a9ac8606Spatrick
2783a9ac8606Spatrick return true;
2784a9ac8606Spatrick }
2785a9ac8606Spatrick
2786a9ac8606Spatrick //===----------------------------------------------------------------------===//
2787a9ac8606Spatrick // RangeConstraintManager implementation
2788a9ac8606Spatrick //===----------------------------------------------------------------------===//
2789a9ac8606Spatrick
canReasonAbout(SVal X) const2790e5dd7070Spatrick bool RangeConstraintManager::canReasonAbout(SVal X) const {
2791*12c85518Srobert std::optional<nonloc::SymbolVal> SymVal = X.getAs<nonloc::SymbolVal>();
2792e5dd7070Spatrick if (SymVal && SymVal->isExpression()) {
2793e5dd7070Spatrick const SymExpr *SE = SymVal->getSymbol();
2794e5dd7070Spatrick
2795e5dd7070Spatrick if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
2796e5dd7070Spatrick switch (SIE->getOpcode()) {
2797e5dd7070Spatrick // We don't reason yet about bitwise-constraints on symbolic values.
2798e5dd7070Spatrick case BO_And:
2799e5dd7070Spatrick case BO_Or:
2800e5dd7070Spatrick case BO_Xor:
2801e5dd7070Spatrick return false;
2802e5dd7070Spatrick // We don't reason yet about these arithmetic constraints on
2803e5dd7070Spatrick // symbolic values.
2804e5dd7070Spatrick case BO_Mul:
2805e5dd7070Spatrick case BO_Div:
2806e5dd7070Spatrick case BO_Rem:
2807e5dd7070Spatrick case BO_Shl:
2808e5dd7070Spatrick case BO_Shr:
2809e5dd7070Spatrick return false;
2810e5dd7070Spatrick // All other cases.
2811e5dd7070Spatrick default:
2812e5dd7070Spatrick return true;
2813e5dd7070Spatrick }
2814e5dd7070Spatrick }
2815e5dd7070Spatrick
2816e5dd7070Spatrick if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
2817e5dd7070Spatrick // FIXME: Handle <=> here.
2818e5dd7070Spatrick if (BinaryOperator::isEqualityOp(SSE->getOpcode()) ||
2819e5dd7070Spatrick BinaryOperator::isRelationalOp(SSE->getOpcode())) {
2820e5dd7070Spatrick // We handle Loc <> Loc comparisons, but not (yet) NonLoc <> NonLoc.
2821e5dd7070Spatrick // We've recently started producing Loc <> NonLoc comparisons (that
2822e5dd7070Spatrick // result from casts of one of the operands between eg. intptr_t and
2823e5dd7070Spatrick // void *), but we can't reason about them yet.
2824e5dd7070Spatrick if (Loc::isLocType(SSE->getLHS()->getType())) {
2825e5dd7070Spatrick return Loc::isLocType(SSE->getRHS()->getType());
2826e5dd7070Spatrick }
2827e5dd7070Spatrick }
2828e5dd7070Spatrick }
2829e5dd7070Spatrick
2830e5dd7070Spatrick return false;
2831e5dd7070Spatrick }
2832e5dd7070Spatrick
2833e5dd7070Spatrick return true;
2834e5dd7070Spatrick }
2835e5dd7070Spatrick
checkNull(ProgramStateRef State,SymbolRef Sym)2836e5dd7070Spatrick ConditionTruthVal RangeConstraintManager::checkNull(ProgramStateRef State,
2837e5dd7070Spatrick SymbolRef Sym) {
2838a9ac8606Spatrick const RangeSet *Ranges = getConstraint(State, Sym);
2839e5dd7070Spatrick
2840e5dd7070Spatrick // If we don't have any information about this symbol, it's underconstrained.
2841e5dd7070Spatrick if (!Ranges)
2842e5dd7070Spatrick return ConditionTruthVal();
2843e5dd7070Spatrick
2844e5dd7070Spatrick // If we have a concrete value, see if it's zero.
2845e5dd7070Spatrick if (const llvm::APSInt *Value = Ranges->getConcreteValue())
2846e5dd7070Spatrick return *Value == 0;
2847e5dd7070Spatrick
2848e5dd7070Spatrick BasicValueFactory &BV = getBasicVals();
2849e5dd7070Spatrick APSIntType IntType = BV.getAPSIntType(Sym->getType());
2850e5dd7070Spatrick llvm::APSInt Zero = IntType.getZeroValue();
2851e5dd7070Spatrick
2852e5dd7070Spatrick // Check if zero is in the set of possible values.
2853a9ac8606Spatrick if (!Ranges->contains(Zero))
2854e5dd7070Spatrick return false;
2855e5dd7070Spatrick
2856e5dd7070Spatrick // Zero is a possible value, but it is not the /only/ possible value.
2857e5dd7070Spatrick return ConditionTruthVal();
2858e5dd7070Spatrick }
2859e5dd7070Spatrick
getSymVal(ProgramStateRef St,SymbolRef Sym) const2860e5dd7070Spatrick const llvm::APSInt *RangeConstraintManager::getSymVal(ProgramStateRef St,
2861e5dd7070Spatrick SymbolRef Sym) const {
2862a9ac8606Spatrick const RangeSet *T = getConstraint(St, Sym);
2863e5dd7070Spatrick return T ? T->getConcreteValue() : nullptr;
2864e5dd7070Spatrick }
2865e5dd7070Spatrick
2866a9ac8606Spatrick //===----------------------------------------------------------------------===//
2867a9ac8606Spatrick // Remove dead symbols from existing constraints
2868a9ac8606Spatrick //===----------------------------------------------------------------------===//
2869a9ac8606Spatrick
2870e5dd7070Spatrick /// Scan all symbols referenced by the constraints. If the symbol is not alive
2871e5dd7070Spatrick /// as marked in LSymbols, mark it as dead in DSymbols.
2872e5dd7070Spatrick ProgramStateRef
removeDeadBindings(ProgramStateRef State,SymbolReaper & SymReaper)2873e5dd7070Spatrick RangeConstraintManager::removeDeadBindings(ProgramStateRef State,
2874e5dd7070Spatrick SymbolReaper &SymReaper) {
2875a9ac8606Spatrick ClassMembersTy ClassMembersMap = State->get<ClassMembers>();
2876a9ac8606Spatrick ClassMembersTy NewClassMembersMap = ClassMembersMap;
2877a9ac8606Spatrick ClassMembersTy::Factory &EMFactory = State->get_context<ClassMembers>();
2878a9ac8606Spatrick SymbolSet::Factory &SetFactory = State->get_context<SymbolSet>();
2879e5dd7070Spatrick
2880a9ac8606Spatrick ConstraintRangeTy Constraints = State->get<ConstraintRange>();
2881a9ac8606Spatrick ConstraintRangeTy NewConstraints = Constraints;
2882a9ac8606Spatrick ConstraintRangeTy::Factory &ConstraintFactory =
2883a9ac8606Spatrick State->get_context<ConstraintRange>();
2884a9ac8606Spatrick
2885a9ac8606Spatrick ClassMapTy Map = State->get<ClassMap>();
2886a9ac8606Spatrick ClassMapTy NewMap = Map;
2887a9ac8606Spatrick ClassMapTy::Factory &ClassFactory = State->get_context<ClassMap>();
2888a9ac8606Spatrick
2889a9ac8606Spatrick DisequalityMapTy Disequalities = State->get<DisequalityMap>();
2890a9ac8606Spatrick DisequalityMapTy::Factory &DisequalityFactory =
2891a9ac8606Spatrick State->get_context<DisequalityMap>();
2892a9ac8606Spatrick ClassSet::Factory &ClassSetFactory = State->get_context<ClassSet>();
2893a9ac8606Spatrick
2894a9ac8606Spatrick bool ClassMapChanged = false;
2895a9ac8606Spatrick bool MembersMapChanged = false;
2896a9ac8606Spatrick bool ConstraintMapChanged = false;
2897a9ac8606Spatrick bool DisequalitiesChanged = false;
2898a9ac8606Spatrick
2899a9ac8606Spatrick auto removeDeadClass = [&](EquivalenceClass Class) {
2900a9ac8606Spatrick // Remove associated constraint ranges.
2901a9ac8606Spatrick Constraints = ConstraintFactory.remove(Constraints, Class);
2902a9ac8606Spatrick ConstraintMapChanged = true;
2903a9ac8606Spatrick
2904a9ac8606Spatrick // Update disequality information to not hold any information on the
2905a9ac8606Spatrick // removed class.
2906a9ac8606Spatrick ClassSet DisequalClasses =
2907a9ac8606Spatrick Class.getDisequalClasses(Disequalities, ClassSetFactory);
2908a9ac8606Spatrick if (!DisequalClasses.isEmpty()) {
2909a9ac8606Spatrick for (EquivalenceClass DisequalClass : DisequalClasses) {
2910a9ac8606Spatrick ClassSet DisequalToDisequalSet =
2911a9ac8606Spatrick DisequalClass.getDisequalClasses(Disequalities, ClassSetFactory);
2912a9ac8606Spatrick // DisequalToDisequalSet is guaranteed to be non-empty for consistent
2913a9ac8606Spatrick // disequality info.
2914a9ac8606Spatrick assert(!DisequalToDisequalSet.isEmpty());
2915a9ac8606Spatrick ClassSet NewSet = ClassSetFactory.remove(DisequalToDisequalSet, Class);
2916a9ac8606Spatrick
2917a9ac8606Spatrick // No need in keeping an empty set.
2918a9ac8606Spatrick if (NewSet.isEmpty()) {
2919a9ac8606Spatrick Disequalities =
2920a9ac8606Spatrick DisequalityFactory.remove(Disequalities, DisequalClass);
2921a9ac8606Spatrick } else {
2922a9ac8606Spatrick Disequalities =
2923a9ac8606Spatrick DisequalityFactory.add(Disequalities, DisequalClass, NewSet);
2924a9ac8606Spatrick }
2925a9ac8606Spatrick }
2926a9ac8606Spatrick // Remove the data for the class
2927a9ac8606Spatrick Disequalities = DisequalityFactory.remove(Disequalities, Class);
2928a9ac8606Spatrick DisequalitiesChanged = true;
2929a9ac8606Spatrick }
2930a9ac8606Spatrick };
2931a9ac8606Spatrick
2932a9ac8606Spatrick // 1. Let's see if dead symbols are trivial and have associated constraints.
2933a9ac8606Spatrick for (std::pair<EquivalenceClass, RangeSet> ClassConstraintPair :
2934a9ac8606Spatrick Constraints) {
2935a9ac8606Spatrick EquivalenceClass Class = ClassConstraintPair.first;
2936a9ac8606Spatrick if (Class.isTriviallyDead(State, SymReaper)) {
2937a9ac8606Spatrick // If this class is trivial, we can remove its constraints right away.
2938a9ac8606Spatrick removeDeadClass(Class);
2939a9ac8606Spatrick }
2940a9ac8606Spatrick }
2941a9ac8606Spatrick
2942a9ac8606Spatrick // 2. We don't need to track classes for dead symbols.
2943a9ac8606Spatrick for (std::pair<SymbolRef, EquivalenceClass> SymbolClassPair : Map) {
2944a9ac8606Spatrick SymbolRef Sym = SymbolClassPair.first;
2945a9ac8606Spatrick
2946e5dd7070Spatrick if (SymReaper.isDead(Sym)) {
2947a9ac8606Spatrick ClassMapChanged = true;
2948a9ac8606Spatrick NewMap = ClassFactory.remove(NewMap, Sym);
2949e5dd7070Spatrick }
2950e5dd7070Spatrick }
2951e5dd7070Spatrick
2952a9ac8606Spatrick // 3. Remove dead members from classes and remove dead non-trivial classes
2953a9ac8606Spatrick // and their constraints.
2954a9ac8606Spatrick for (std::pair<EquivalenceClass, SymbolSet> ClassMembersPair :
2955a9ac8606Spatrick ClassMembersMap) {
2956a9ac8606Spatrick EquivalenceClass Class = ClassMembersPair.first;
2957a9ac8606Spatrick SymbolSet LiveMembers = ClassMembersPair.second;
2958a9ac8606Spatrick bool MembersChanged = false;
2959a9ac8606Spatrick
2960a9ac8606Spatrick for (SymbolRef Member : ClassMembersPair.second) {
2961a9ac8606Spatrick if (SymReaper.isDead(Member)) {
2962a9ac8606Spatrick MembersChanged = true;
2963a9ac8606Spatrick LiveMembers = SetFactory.remove(LiveMembers, Member);
2964a9ac8606Spatrick }
2965a9ac8606Spatrick }
2966a9ac8606Spatrick
2967a9ac8606Spatrick // Check if the class changed.
2968a9ac8606Spatrick if (!MembersChanged)
2969a9ac8606Spatrick continue;
2970a9ac8606Spatrick
2971a9ac8606Spatrick MembersMapChanged = true;
2972a9ac8606Spatrick
2973a9ac8606Spatrick if (LiveMembers.isEmpty()) {
2974a9ac8606Spatrick // The class is dead now, we need to wipe it out of the members map...
2975a9ac8606Spatrick NewClassMembersMap = EMFactory.remove(NewClassMembersMap, Class);
2976a9ac8606Spatrick
2977a9ac8606Spatrick // ...and remove all of its constraints.
2978a9ac8606Spatrick removeDeadClass(Class);
2979a9ac8606Spatrick } else {
2980a9ac8606Spatrick // We need to change the members associated with the class.
2981a9ac8606Spatrick NewClassMembersMap =
2982a9ac8606Spatrick EMFactory.add(NewClassMembersMap, Class, LiveMembers);
2983a9ac8606Spatrick }
2984a9ac8606Spatrick }
2985a9ac8606Spatrick
2986a9ac8606Spatrick // 4. Update the state with new maps.
2987a9ac8606Spatrick //
2988a9ac8606Spatrick // Here we try to be humble and update a map only if it really changed.
2989a9ac8606Spatrick if (ClassMapChanged)
2990a9ac8606Spatrick State = State->set<ClassMap>(NewMap);
2991a9ac8606Spatrick
2992a9ac8606Spatrick if (MembersMapChanged)
2993a9ac8606Spatrick State = State->set<ClassMembers>(NewClassMembersMap);
2994a9ac8606Spatrick
2995a9ac8606Spatrick if (ConstraintMapChanged)
2996a9ac8606Spatrick State = State->set<ConstraintRange>(Constraints);
2997a9ac8606Spatrick
2998a9ac8606Spatrick if (DisequalitiesChanged)
2999a9ac8606Spatrick State = State->set<DisequalityMap>(Disequalities);
3000a9ac8606Spatrick
3001a9ac8606Spatrick assert(EquivalenceClass::isClassDataConsistent(State));
3002a9ac8606Spatrick
3003a9ac8606Spatrick return State;
3004e5dd7070Spatrick }
3005e5dd7070Spatrick
getRange(ProgramStateRef State,SymbolRef Sym)3006e5dd7070Spatrick RangeSet RangeConstraintManager::getRange(ProgramStateRef State,
3007e5dd7070Spatrick SymbolRef Sym) {
3008a9ac8606Spatrick return SymbolicRangeInferrer::inferRange(F, State, Sym);
3009a9ac8606Spatrick }
3010a9ac8606Spatrick
setRange(ProgramStateRef State,SymbolRef Sym,RangeSet Range)3011a9ac8606Spatrick ProgramStateRef RangeConstraintManager::setRange(ProgramStateRef State,
3012a9ac8606Spatrick SymbolRef Sym,
3013a9ac8606Spatrick RangeSet Range) {
3014a9ac8606Spatrick return ConstraintAssignor::assign(State, getSValBuilder(), F, Sym, Range);
3015e5dd7070Spatrick }
3016e5dd7070Spatrick
3017e5dd7070Spatrick //===------------------------------------------------------------------------===
3018e5dd7070Spatrick // assumeSymX methods: protected interface for RangeConstraintManager.
3019e5dd7070Spatrick //===------------------------------------------------------------------------===/
3020e5dd7070Spatrick
3021e5dd7070Spatrick // The syntax for ranges below is mathematical, using [x, y] for closed ranges
3022e5dd7070Spatrick // and (x, y) for open ranges. These ranges are modular, corresponding with
3023e5dd7070Spatrick // a common treatment of C integer overflow. This means that these methods
3024e5dd7070Spatrick // do not have to worry about overflow; RangeSet::Intersect can handle such a
3025e5dd7070Spatrick // "wraparound" range.
3026e5dd7070Spatrick // As an example, the range [UINT_MAX-1, 3) contains five values: UINT_MAX-1,
3027e5dd7070Spatrick // UINT_MAX, 0, 1, and 2.
3028e5dd7070Spatrick
3029e5dd7070Spatrick ProgramStateRef
assumeSymNE(ProgramStateRef St,SymbolRef Sym,const llvm::APSInt & Int,const llvm::APSInt & Adjustment)3030e5dd7070Spatrick RangeConstraintManager::assumeSymNE(ProgramStateRef St, SymbolRef Sym,
3031e5dd7070Spatrick const llvm::APSInt &Int,
3032e5dd7070Spatrick const llvm::APSInt &Adjustment) {
3033e5dd7070Spatrick // Before we do any real work, see if the value can even show up.
3034e5dd7070Spatrick APSIntType AdjustmentType(Adjustment);
3035e5dd7070Spatrick if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within)
3036e5dd7070Spatrick return St;
3037e5dd7070Spatrick
3038a9ac8606Spatrick llvm::APSInt Point = AdjustmentType.convert(Int) - Adjustment;
3039a9ac8606Spatrick RangeSet New = getRange(St, Sym);
3040a9ac8606Spatrick New = F.deletePoint(New, Point);
3041e5dd7070Spatrick
3042a9ac8606Spatrick return setRange(St, Sym, New);
3043e5dd7070Spatrick }
3044e5dd7070Spatrick
3045e5dd7070Spatrick ProgramStateRef
assumeSymEQ(ProgramStateRef St,SymbolRef Sym,const llvm::APSInt & Int,const llvm::APSInt & Adjustment)3046e5dd7070Spatrick RangeConstraintManager::assumeSymEQ(ProgramStateRef St, SymbolRef Sym,
3047e5dd7070Spatrick const llvm::APSInt &Int,
3048e5dd7070Spatrick const llvm::APSInt &Adjustment) {
3049e5dd7070Spatrick // Before we do any real work, see if the value can even show up.
3050e5dd7070Spatrick APSIntType AdjustmentType(Adjustment);
3051e5dd7070Spatrick if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within)
3052e5dd7070Spatrick return nullptr;
3053e5dd7070Spatrick
3054e5dd7070Spatrick // [Int-Adjustment, Int-Adjustment]
3055e5dd7070Spatrick llvm::APSInt AdjInt = AdjustmentType.convert(Int) - Adjustment;
3056a9ac8606Spatrick RangeSet New = getRange(St, Sym);
3057a9ac8606Spatrick New = F.intersect(New, AdjInt);
3058a9ac8606Spatrick
3059a9ac8606Spatrick return setRange(St, Sym, New);
3060e5dd7070Spatrick }
3061e5dd7070Spatrick
getSymLTRange(ProgramStateRef St,SymbolRef Sym,const llvm::APSInt & Int,const llvm::APSInt & Adjustment)3062e5dd7070Spatrick RangeSet RangeConstraintManager::getSymLTRange(ProgramStateRef St,
3063e5dd7070Spatrick SymbolRef Sym,
3064e5dd7070Spatrick const llvm::APSInt &Int,
3065e5dd7070Spatrick const llvm::APSInt &Adjustment) {
3066e5dd7070Spatrick // Before we do any real work, see if the value can even show up.
3067e5dd7070Spatrick APSIntType AdjustmentType(Adjustment);
3068e5dd7070Spatrick switch (AdjustmentType.testInRange(Int, true)) {
3069e5dd7070Spatrick case APSIntType::RTR_Below:
3070e5dd7070Spatrick return F.getEmptySet();
3071e5dd7070Spatrick case APSIntType::RTR_Within:
3072e5dd7070Spatrick break;
3073e5dd7070Spatrick case APSIntType::RTR_Above:
3074e5dd7070Spatrick return getRange(St, Sym);
3075e5dd7070Spatrick }
3076e5dd7070Spatrick
3077e5dd7070Spatrick // Special case for Int == Min. This is always false.
3078e5dd7070Spatrick llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
3079e5dd7070Spatrick llvm::APSInt Min = AdjustmentType.getMinValue();
3080e5dd7070Spatrick if (ComparisonVal == Min)
3081e5dd7070Spatrick return F.getEmptySet();
3082e5dd7070Spatrick
3083e5dd7070Spatrick llvm::APSInt Lower = Min - Adjustment;
3084e5dd7070Spatrick llvm::APSInt Upper = ComparisonVal - Adjustment;
3085e5dd7070Spatrick --Upper;
3086e5dd7070Spatrick
3087a9ac8606Spatrick RangeSet Result = getRange(St, Sym);
3088a9ac8606Spatrick return F.intersect(Result, Lower, Upper);
3089e5dd7070Spatrick }
3090e5dd7070Spatrick
3091e5dd7070Spatrick ProgramStateRef
assumeSymLT(ProgramStateRef St,SymbolRef Sym,const llvm::APSInt & Int,const llvm::APSInt & Adjustment)3092e5dd7070Spatrick RangeConstraintManager::assumeSymLT(ProgramStateRef St, SymbolRef Sym,
3093e5dd7070Spatrick const llvm::APSInt &Int,
3094e5dd7070Spatrick const llvm::APSInt &Adjustment) {
3095e5dd7070Spatrick RangeSet New = getSymLTRange(St, Sym, Int, Adjustment);
3096a9ac8606Spatrick return setRange(St, Sym, New);
3097e5dd7070Spatrick }
3098e5dd7070Spatrick
getSymGTRange(ProgramStateRef St,SymbolRef Sym,const llvm::APSInt & Int,const llvm::APSInt & Adjustment)3099e5dd7070Spatrick RangeSet RangeConstraintManager::getSymGTRange(ProgramStateRef St,
3100e5dd7070Spatrick SymbolRef Sym,
3101e5dd7070Spatrick const llvm::APSInt &Int,
3102e5dd7070Spatrick const llvm::APSInt &Adjustment) {
3103e5dd7070Spatrick // Before we do any real work, see if the value can even show up.
3104e5dd7070Spatrick APSIntType AdjustmentType(Adjustment);
3105e5dd7070Spatrick switch (AdjustmentType.testInRange(Int, true)) {
3106e5dd7070Spatrick case APSIntType::RTR_Below:
3107e5dd7070Spatrick return getRange(St, Sym);
3108e5dd7070Spatrick case APSIntType::RTR_Within:
3109e5dd7070Spatrick break;
3110e5dd7070Spatrick case APSIntType::RTR_Above:
3111e5dd7070Spatrick return F.getEmptySet();
3112e5dd7070Spatrick }
3113e5dd7070Spatrick
3114e5dd7070Spatrick // Special case for Int == Max. This is always false.
3115e5dd7070Spatrick llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
3116e5dd7070Spatrick llvm::APSInt Max = AdjustmentType.getMaxValue();
3117e5dd7070Spatrick if (ComparisonVal == Max)
3118e5dd7070Spatrick return F.getEmptySet();
3119e5dd7070Spatrick
3120e5dd7070Spatrick llvm::APSInt Lower = ComparisonVal - Adjustment;
3121e5dd7070Spatrick llvm::APSInt Upper = Max - Adjustment;
3122e5dd7070Spatrick ++Lower;
3123e5dd7070Spatrick
3124a9ac8606Spatrick RangeSet SymRange = getRange(St, Sym);
3125a9ac8606Spatrick return F.intersect(SymRange, Lower, Upper);
3126e5dd7070Spatrick }
3127e5dd7070Spatrick
3128e5dd7070Spatrick ProgramStateRef
assumeSymGT(ProgramStateRef St,SymbolRef Sym,const llvm::APSInt & Int,const llvm::APSInt & Adjustment)3129e5dd7070Spatrick RangeConstraintManager::assumeSymGT(ProgramStateRef St, SymbolRef Sym,
3130e5dd7070Spatrick const llvm::APSInt &Int,
3131e5dd7070Spatrick const llvm::APSInt &Adjustment) {
3132e5dd7070Spatrick RangeSet New = getSymGTRange(St, Sym, Int, Adjustment);
3133a9ac8606Spatrick return setRange(St, Sym, New);
3134e5dd7070Spatrick }
3135e5dd7070Spatrick
getSymGERange(ProgramStateRef St,SymbolRef Sym,const llvm::APSInt & Int,const llvm::APSInt & Adjustment)3136e5dd7070Spatrick RangeSet RangeConstraintManager::getSymGERange(ProgramStateRef St,
3137e5dd7070Spatrick SymbolRef Sym,
3138e5dd7070Spatrick const llvm::APSInt &Int,
3139e5dd7070Spatrick const llvm::APSInt &Adjustment) {
3140e5dd7070Spatrick // Before we do any real work, see if the value can even show up.
3141e5dd7070Spatrick APSIntType AdjustmentType(Adjustment);
3142e5dd7070Spatrick switch (AdjustmentType.testInRange(Int, true)) {
3143e5dd7070Spatrick case APSIntType::RTR_Below:
3144e5dd7070Spatrick return getRange(St, Sym);
3145e5dd7070Spatrick case APSIntType::RTR_Within:
3146e5dd7070Spatrick break;
3147e5dd7070Spatrick case APSIntType::RTR_Above:
3148e5dd7070Spatrick return F.getEmptySet();
3149e5dd7070Spatrick }
3150e5dd7070Spatrick
3151e5dd7070Spatrick // Special case for Int == Min. This is always feasible.
3152e5dd7070Spatrick llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
3153e5dd7070Spatrick llvm::APSInt Min = AdjustmentType.getMinValue();
3154e5dd7070Spatrick if (ComparisonVal == Min)
3155e5dd7070Spatrick return getRange(St, Sym);
3156e5dd7070Spatrick
3157e5dd7070Spatrick llvm::APSInt Max = AdjustmentType.getMaxValue();
3158e5dd7070Spatrick llvm::APSInt Lower = ComparisonVal - Adjustment;
3159e5dd7070Spatrick llvm::APSInt Upper = Max - Adjustment;
3160e5dd7070Spatrick
3161a9ac8606Spatrick RangeSet SymRange = getRange(St, Sym);
3162a9ac8606Spatrick return F.intersect(SymRange, Lower, Upper);
3163e5dd7070Spatrick }
3164e5dd7070Spatrick
3165e5dd7070Spatrick ProgramStateRef
assumeSymGE(ProgramStateRef St,SymbolRef Sym,const llvm::APSInt & Int,const llvm::APSInt & Adjustment)3166e5dd7070Spatrick RangeConstraintManager::assumeSymGE(ProgramStateRef St, SymbolRef Sym,
3167e5dd7070Spatrick const llvm::APSInt &Int,
3168e5dd7070Spatrick const llvm::APSInt &Adjustment) {
3169e5dd7070Spatrick RangeSet New = getSymGERange(St, Sym, Int, Adjustment);
3170a9ac8606Spatrick return setRange(St, Sym, New);
3171e5dd7070Spatrick }
3172e5dd7070Spatrick
3173a9ac8606Spatrick RangeSet
getSymLERange(llvm::function_ref<RangeSet ()> RS,const llvm::APSInt & Int,const llvm::APSInt & Adjustment)3174a9ac8606Spatrick RangeConstraintManager::getSymLERange(llvm::function_ref<RangeSet()> RS,
3175e5dd7070Spatrick const llvm::APSInt &Int,
3176e5dd7070Spatrick const llvm::APSInt &Adjustment) {
3177e5dd7070Spatrick // Before we do any real work, see if the value can even show up.
3178e5dd7070Spatrick APSIntType AdjustmentType(Adjustment);
3179e5dd7070Spatrick switch (AdjustmentType.testInRange(Int, true)) {
3180e5dd7070Spatrick case APSIntType::RTR_Below:
3181e5dd7070Spatrick return F.getEmptySet();
3182e5dd7070Spatrick case APSIntType::RTR_Within:
3183e5dd7070Spatrick break;
3184e5dd7070Spatrick case APSIntType::RTR_Above:
3185e5dd7070Spatrick return RS();
3186e5dd7070Spatrick }
3187e5dd7070Spatrick
3188e5dd7070Spatrick // Special case for Int == Max. This is always feasible.
3189e5dd7070Spatrick llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
3190e5dd7070Spatrick llvm::APSInt Max = AdjustmentType.getMaxValue();
3191e5dd7070Spatrick if (ComparisonVal == Max)
3192e5dd7070Spatrick return RS();
3193e5dd7070Spatrick
3194e5dd7070Spatrick llvm::APSInt Min = AdjustmentType.getMinValue();
3195e5dd7070Spatrick llvm::APSInt Lower = Min - Adjustment;
3196e5dd7070Spatrick llvm::APSInt Upper = ComparisonVal - Adjustment;
3197e5dd7070Spatrick
3198a9ac8606Spatrick RangeSet Default = RS();
3199a9ac8606Spatrick return F.intersect(Default, Lower, Upper);
3200e5dd7070Spatrick }
3201e5dd7070Spatrick
getSymLERange(ProgramStateRef St,SymbolRef Sym,const llvm::APSInt & Int,const llvm::APSInt & Adjustment)3202e5dd7070Spatrick RangeSet RangeConstraintManager::getSymLERange(ProgramStateRef St,
3203e5dd7070Spatrick SymbolRef Sym,
3204e5dd7070Spatrick const llvm::APSInt &Int,
3205e5dd7070Spatrick const llvm::APSInt &Adjustment) {
3206e5dd7070Spatrick return getSymLERange([&] { return getRange(St, Sym); }, Int, Adjustment);
3207e5dd7070Spatrick }
3208e5dd7070Spatrick
3209e5dd7070Spatrick ProgramStateRef
assumeSymLE(ProgramStateRef St,SymbolRef Sym,const llvm::APSInt & Int,const llvm::APSInt & Adjustment)3210e5dd7070Spatrick RangeConstraintManager::assumeSymLE(ProgramStateRef St, SymbolRef Sym,
3211e5dd7070Spatrick const llvm::APSInt &Int,
3212e5dd7070Spatrick const llvm::APSInt &Adjustment) {
3213e5dd7070Spatrick RangeSet New = getSymLERange(St, Sym, Int, Adjustment);
3214a9ac8606Spatrick return setRange(St, Sym, New);
3215e5dd7070Spatrick }
3216e5dd7070Spatrick
assumeSymWithinInclusiveRange(ProgramStateRef State,SymbolRef Sym,const llvm::APSInt & From,const llvm::APSInt & To,const llvm::APSInt & Adjustment)3217e5dd7070Spatrick ProgramStateRef RangeConstraintManager::assumeSymWithinInclusiveRange(
3218e5dd7070Spatrick ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
3219e5dd7070Spatrick const llvm::APSInt &To, const llvm::APSInt &Adjustment) {
3220e5dd7070Spatrick RangeSet New = getSymGERange(State, Sym, From, Adjustment);
3221e5dd7070Spatrick if (New.isEmpty())
3222e5dd7070Spatrick return nullptr;
3223e5dd7070Spatrick RangeSet Out = getSymLERange([&] { return New; }, To, Adjustment);
3224a9ac8606Spatrick return setRange(State, Sym, Out);
3225e5dd7070Spatrick }
3226e5dd7070Spatrick
assumeSymOutsideInclusiveRange(ProgramStateRef State,SymbolRef Sym,const llvm::APSInt & From,const llvm::APSInt & To,const llvm::APSInt & Adjustment)3227e5dd7070Spatrick ProgramStateRef RangeConstraintManager::assumeSymOutsideInclusiveRange(
3228e5dd7070Spatrick ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
3229e5dd7070Spatrick const llvm::APSInt &To, const llvm::APSInt &Adjustment) {
3230e5dd7070Spatrick RangeSet RangeLT = getSymLTRange(State, Sym, From, Adjustment);
3231e5dd7070Spatrick RangeSet RangeGT = getSymGTRange(State, Sym, To, Adjustment);
3232a9ac8606Spatrick RangeSet New(F.add(RangeLT, RangeGT));
3233a9ac8606Spatrick return setRange(State, Sym, New);
3234e5dd7070Spatrick }
3235e5dd7070Spatrick
3236e5dd7070Spatrick //===----------------------------------------------------------------------===//
3237e5dd7070Spatrick // Pretty-printing.
3238e5dd7070Spatrick //===----------------------------------------------------------------------===//
3239e5dd7070Spatrick
printJson(raw_ostream & Out,ProgramStateRef State,const char * NL,unsigned int Space,bool IsDot) const3240e5dd7070Spatrick void RangeConstraintManager::printJson(raw_ostream &Out, ProgramStateRef State,
3241e5dd7070Spatrick const char *NL, unsigned int Space,
3242e5dd7070Spatrick bool IsDot) const {
3243a9ac8606Spatrick printConstraints(Out, State, NL, Space, IsDot);
3244a9ac8606Spatrick printEquivalenceClasses(Out, State, NL, Space, IsDot);
3245a9ac8606Spatrick printDisequalities(Out, State, NL, Space, IsDot);
3246a9ac8606Spatrick }
3247a9ac8606Spatrick
printValue(raw_ostream & Out,ProgramStateRef State,SymbolRef Sym)3248*12c85518Srobert void RangeConstraintManager::printValue(raw_ostream &Out, ProgramStateRef State,
3249*12c85518Srobert SymbolRef Sym) {
3250*12c85518Srobert const RangeSet RS = getRange(State, Sym);
3251*12c85518Srobert Out << RS.getBitWidth() << (RS.isUnsigned() ? "u:" : "s:");
3252*12c85518Srobert RS.dump(Out);
3253*12c85518Srobert }
3254*12c85518Srobert
toString(const SymbolRef & Sym)3255a9ac8606Spatrick static std::string toString(const SymbolRef &Sym) {
3256a9ac8606Spatrick std::string S;
3257a9ac8606Spatrick llvm::raw_string_ostream O(S);
3258a9ac8606Spatrick Sym->dumpToStream(O);
3259a9ac8606Spatrick return O.str();
3260a9ac8606Spatrick }
3261a9ac8606Spatrick
printConstraints(raw_ostream & Out,ProgramStateRef State,const char * NL,unsigned int Space,bool IsDot) const3262a9ac8606Spatrick void RangeConstraintManager::printConstraints(raw_ostream &Out,
3263a9ac8606Spatrick ProgramStateRef State,
3264a9ac8606Spatrick const char *NL,
3265a9ac8606Spatrick unsigned int Space,
3266a9ac8606Spatrick bool IsDot) const {
3267e5dd7070Spatrick ConstraintRangeTy Constraints = State->get<ConstraintRange>();
3268e5dd7070Spatrick
3269e5dd7070Spatrick Indent(Out, Space, IsDot) << "\"constraints\": ";
3270e5dd7070Spatrick if (Constraints.isEmpty()) {
3271e5dd7070Spatrick Out << "null," << NL;
3272e5dd7070Spatrick return;
3273e5dd7070Spatrick }
3274e5dd7070Spatrick
3275a9ac8606Spatrick std::map<std::string, RangeSet> OrderedConstraints;
3276a9ac8606Spatrick for (std::pair<EquivalenceClass, RangeSet> P : Constraints) {
3277a9ac8606Spatrick SymbolSet ClassMembers = P.first.getClassMembers(State);
3278a9ac8606Spatrick for (const SymbolRef &ClassMember : ClassMembers) {
3279a9ac8606Spatrick bool insertion_took_place;
3280a9ac8606Spatrick std::tie(std::ignore, insertion_took_place) =
3281a9ac8606Spatrick OrderedConstraints.insert({toString(ClassMember), P.second});
3282a9ac8606Spatrick assert(insertion_took_place &&
3283a9ac8606Spatrick "two symbols should not have the same dump");
3284a9ac8606Spatrick }
3285a9ac8606Spatrick }
3286a9ac8606Spatrick
3287e5dd7070Spatrick ++Space;
3288e5dd7070Spatrick Out << '[' << NL;
3289a9ac8606Spatrick bool First = true;
3290a9ac8606Spatrick for (std::pair<std::string, RangeSet> P : OrderedConstraints) {
3291a9ac8606Spatrick if (First) {
3292a9ac8606Spatrick First = false;
3293a9ac8606Spatrick } else {
3294e5dd7070Spatrick Out << ',';
3295e5dd7070Spatrick Out << NL;
3296e5dd7070Spatrick }
3297a9ac8606Spatrick Indent(Out, Space, IsDot)
3298a9ac8606Spatrick << "{ \"symbol\": \"" << P.first << "\", \"range\": \"";
3299a9ac8606Spatrick P.second.dump(Out);
3300a9ac8606Spatrick Out << "\" }";
3301a9ac8606Spatrick }
3302a9ac8606Spatrick Out << NL;
3303a9ac8606Spatrick
3304a9ac8606Spatrick --Space;
3305a9ac8606Spatrick Indent(Out, Space, IsDot) << "]," << NL;
3306a9ac8606Spatrick }
3307a9ac8606Spatrick
toString(ProgramStateRef State,EquivalenceClass Class)3308a9ac8606Spatrick static std::string toString(ProgramStateRef State, EquivalenceClass Class) {
3309a9ac8606Spatrick SymbolSet ClassMembers = Class.getClassMembers(State);
3310a9ac8606Spatrick llvm::SmallVector<SymbolRef, 8> ClassMembersSorted(ClassMembers.begin(),
3311a9ac8606Spatrick ClassMembers.end());
3312a9ac8606Spatrick llvm::sort(ClassMembersSorted,
3313a9ac8606Spatrick [](const SymbolRef &LHS, const SymbolRef &RHS) {
3314a9ac8606Spatrick return toString(LHS) < toString(RHS);
3315a9ac8606Spatrick });
3316a9ac8606Spatrick
3317a9ac8606Spatrick bool FirstMember = true;
3318a9ac8606Spatrick
3319a9ac8606Spatrick std::string Str;
3320a9ac8606Spatrick llvm::raw_string_ostream Out(Str);
3321a9ac8606Spatrick Out << "[ ";
3322a9ac8606Spatrick for (SymbolRef ClassMember : ClassMembersSorted) {
3323a9ac8606Spatrick if (FirstMember)
3324a9ac8606Spatrick FirstMember = false;
3325a9ac8606Spatrick else
3326a9ac8606Spatrick Out << ", ";
3327a9ac8606Spatrick Out << "\"" << ClassMember << "\"";
3328a9ac8606Spatrick }
3329a9ac8606Spatrick Out << " ]";
3330a9ac8606Spatrick return Out.str();
3331a9ac8606Spatrick }
3332a9ac8606Spatrick
printEquivalenceClasses(raw_ostream & Out,ProgramStateRef State,const char * NL,unsigned int Space,bool IsDot) const3333a9ac8606Spatrick void RangeConstraintManager::printEquivalenceClasses(raw_ostream &Out,
3334a9ac8606Spatrick ProgramStateRef State,
3335a9ac8606Spatrick const char *NL,
3336a9ac8606Spatrick unsigned int Space,
3337a9ac8606Spatrick bool IsDot) const {
3338a9ac8606Spatrick ClassMembersTy Members = State->get<ClassMembers>();
3339a9ac8606Spatrick
3340a9ac8606Spatrick Indent(Out, Space, IsDot) << "\"equivalence_classes\": ";
3341a9ac8606Spatrick if (Members.isEmpty()) {
3342a9ac8606Spatrick Out << "null," << NL;
3343a9ac8606Spatrick return;
3344a9ac8606Spatrick }
3345a9ac8606Spatrick
3346a9ac8606Spatrick std::set<std::string> MembersStr;
3347a9ac8606Spatrick for (std::pair<EquivalenceClass, SymbolSet> ClassToSymbolSet : Members)
3348a9ac8606Spatrick MembersStr.insert(toString(State, ClassToSymbolSet.first));
3349a9ac8606Spatrick
3350a9ac8606Spatrick ++Space;
3351a9ac8606Spatrick Out << '[' << NL;
3352a9ac8606Spatrick bool FirstClass = true;
3353a9ac8606Spatrick for (const std::string &Str : MembersStr) {
3354a9ac8606Spatrick if (FirstClass) {
3355a9ac8606Spatrick FirstClass = false;
3356a9ac8606Spatrick } else {
3357a9ac8606Spatrick Out << ',';
3358a9ac8606Spatrick Out << NL;
3359a9ac8606Spatrick }
3360a9ac8606Spatrick Indent(Out, Space, IsDot);
3361a9ac8606Spatrick Out << Str;
3362a9ac8606Spatrick }
3363a9ac8606Spatrick Out << NL;
3364a9ac8606Spatrick
3365a9ac8606Spatrick --Space;
3366a9ac8606Spatrick Indent(Out, Space, IsDot) << "]," << NL;
3367a9ac8606Spatrick }
3368a9ac8606Spatrick
printDisequalities(raw_ostream & Out,ProgramStateRef State,const char * NL,unsigned int Space,bool IsDot) const3369a9ac8606Spatrick void RangeConstraintManager::printDisequalities(raw_ostream &Out,
3370a9ac8606Spatrick ProgramStateRef State,
3371a9ac8606Spatrick const char *NL,
3372a9ac8606Spatrick unsigned int Space,
3373a9ac8606Spatrick bool IsDot) const {
3374a9ac8606Spatrick DisequalityMapTy Disequalities = State->get<DisequalityMap>();
3375a9ac8606Spatrick
3376a9ac8606Spatrick Indent(Out, Space, IsDot) << "\"disequality_info\": ";
3377a9ac8606Spatrick if (Disequalities.isEmpty()) {
3378a9ac8606Spatrick Out << "null," << NL;
3379a9ac8606Spatrick return;
3380a9ac8606Spatrick }
3381a9ac8606Spatrick
3382a9ac8606Spatrick // Transform the disequality info to an ordered map of
3383a9ac8606Spatrick // [string -> (ordered set of strings)]
3384a9ac8606Spatrick using EqClassesStrTy = std::set<std::string>;
3385a9ac8606Spatrick using DisequalityInfoStrTy = std::map<std::string, EqClassesStrTy>;
3386a9ac8606Spatrick DisequalityInfoStrTy DisequalityInfoStr;
3387a9ac8606Spatrick for (std::pair<EquivalenceClass, ClassSet> ClassToDisEqSet : Disequalities) {
3388a9ac8606Spatrick EquivalenceClass Class = ClassToDisEqSet.first;
3389a9ac8606Spatrick ClassSet DisequalClasses = ClassToDisEqSet.second;
3390a9ac8606Spatrick EqClassesStrTy MembersStr;
3391a9ac8606Spatrick for (EquivalenceClass DisEqClass : DisequalClasses)
3392a9ac8606Spatrick MembersStr.insert(toString(State, DisEqClass));
3393a9ac8606Spatrick DisequalityInfoStr.insert({toString(State, Class), MembersStr});
3394a9ac8606Spatrick }
3395a9ac8606Spatrick
3396a9ac8606Spatrick ++Space;
3397a9ac8606Spatrick Out << '[' << NL;
3398a9ac8606Spatrick bool FirstClass = true;
3399a9ac8606Spatrick for (std::pair<std::string, EqClassesStrTy> ClassToDisEqSet :
3400a9ac8606Spatrick DisequalityInfoStr) {
3401a9ac8606Spatrick const std::string &Class = ClassToDisEqSet.first;
3402a9ac8606Spatrick if (FirstClass) {
3403a9ac8606Spatrick FirstClass = false;
3404a9ac8606Spatrick } else {
3405a9ac8606Spatrick Out << ',';
3406a9ac8606Spatrick Out << NL;
3407a9ac8606Spatrick }
3408a9ac8606Spatrick Indent(Out, Space, IsDot) << "{" << NL;
3409a9ac8606Spatrick unsigned int DisEqSpace = Space + 1;
3410a9ac8606Spatrick Indent(Out, DisEqSpace, IsDot) << "\"class\": ";
3411a9ac8606Spatrick Out << Class;
3412a9ac8606Spatrick const EqClassesStrTy &DisequalClasses = ClassToDisEqSet.second;
3413a9ac8606Spatrick if (!DisequalClasses.empty()) {
3414a9ac8606Spatrick Out << "," << NL;
3415a9ac8606Spatrick Indent(Out, DisEqSpace, IsDot) << "\"disequal_to\": [" << NL;
3416a9ac8606Spatrick unsigned int DisEqClassSpace = DisEqSpace + 1;
3417a9ac8606Spatrick Indent(Out, DisEqClassSpace, IsDot);
3418a9ac8606Spatrick bool FirstDisEqClass = true;
3419a9ac8606Spatrick for (const std::string &DisEqClass : DisequalClasses) {
3420a9ac8606Spatrick if (FirstDisEqClass) {
3421a9ac8606Spatrick FirstDisEqClass = false;
3422a9ac8606Spatrick } else {
3423a9ac8606Spatrick Out << ',' << NL;
3424a9ac8606Spatrick Indent(Out, DisEqClassSpace, IsDot);
3425a9ac8606Spatrick }
3426a9ac8606Spatrick Out << DisEqClass;
3427a9ac8606Spatrick }
3428a9ac8606Spatrick Out << "]" << NL;
3429a9ac8606Spatrick }
3430a9ac8606Spatrick Indent(Out, Space, IsDot) << "}";
3431a9ac8606Spatrick }
3432a9ac8606Spatrick Out << NL;
3433e5dd7070Spatrick
3434e5dd7070Spatrick --Space;
3435e5dd7070Spatrick Indent(Out, Space, IsDot) << "]," << NL;
3436e5dd7070Spatrick }
3437