Skip to content

Commit

Permalink
[analyzer][solver][NFC] Simplify function signatures
Browse files Browse the repository at this point in the history
Since RangeSet::Factory actually contains BasicValueFactory, we can
remove value factory from many function signatures inside the solver.

Differential Revision: https://reviews.llvm.org/D105005
  • Loading branch information
SavchenkoValeriy committed Jun 28, 2021
1 parent 6f3b775 commit 8474bb1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 71 deletions.
Expand Up @@ -213,6 +213,9 @@ class RangeSet {
/// where N = size(What)
RangeSet negate(RangeSet What);

/// Return associated value factory.
BasicValueFactory &getValueFactory() const { return ValueFactory; }

private:
/// Return a persistent version of the given container.
RangeSet makePersistent(ContainerType &&From);
Expand Down
127 changes: 56 additions & 71 deletions clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp
Expand Up @@ -549,14 +549,13 @@ class EquivalenceClass : public llvm::FoldingSetNode {
SymbolRef Sym);

/// Merge classes for the given symbols and return a new state.
LLVM_NODISCARD static inline ProgramStateRef
merge(BasicValueFactory &BV, RangeSet::Factory &F, ProgramStateRef State,
SymbolRef First, SymbolRef Second);
LLVM_NODISCARD static inline ProgramStateRef merge(RangeSet::Factory &F,
ProgramStateRef State,
SymbolRef First,
SymbolRef Second);
// Merge this class with the given class and return a new state.
LLVM_NODISCARD inline ProgramStateRef merge(BasicValueFactory &BV,
RangeSet::Factory &F,
ProgramStateRef State,
EquivalenceClass Other);
LLVM_NODISCARD inline ProgramStateRef
merge(RangeSet::Factory &F, ProgramStateRef State, EquivalenceClass Other);

/// Return a set of class members for the given state.
LLVM_NODISCARD inline SymbolSet getClassMembers(ProgramStateRef State) const;
Expand All @@ -567,15 +566,14 @@ class EquivalenceClass : public llvm::FoldingSetNode {
SymbolReaper &Reaper) const;

LLVM_NODISCARD static inline ProgramStateRef
markDisequal(BasicValueFactory &BV, RangeSet::Factory &F,
ProgramStateRef State, SymbolRef First, SymbolRef Second);
markDisequal(RangeSet::Factory &F, ProgramStateRef State, SymbolRef First,
SymbolRef Second);
LLVM_NODISCARD static inline ProgramStateRef
markDisequal(BasicValueFactory &BV, RangeSet::Factory &F,
ProgramStateRef State, EquivalenceClass First,
EquivalenceClass Second);
markDisequal(RangeSet::Factory &F, ProgramStateRef State,
EquivalenceClass First, EquivalenceClass Second);
LLVM_NODISCARD inline ProgramStateRef
markDisequal(BasicValueFactory &BV, RangeSet::Factory &F,
ProgramStateRef State, EquivalenceClass Other) const;
markDisequal(RangeSet::Factory &F, ProgramStateRef State,
EquivalenceClass Other) const;
LLVM_NODISCARD static inline ClassSet
getDisequalClasses(ProgramStateRef State, SymbolRef Sym);
LLVM_NODISCARD inline ClassSet
Expand Down Expand Up @@ -641,15 +639,13 @@ class EquivalenceClass : public llvm::FoldingSetNode {
}
static inline SymbolSet::Factory &getMembersFactory(ProgramStateRef State);

inline ProgramStateRef mergeImpl(BasicValueFactory &BV, RangeSet::Factory &F,
ProgramStateRef State, SymbolSet Members,
EquivalenceClass Other,
inline ProgramStateRef mergeImpl(RangeSet::Factory &F, ProgramStateRef State,
SymbolSet Members, EquivalenceClass Other,
SymbolSet OtherMembers);
static inline bool
addToDisequalityInfo(DisequalityMapTy &Info, ConstraintRangeTy &Constraints,
BasicValueFactory &BV, RangeSet::Factory &F,
ProgramStateRef State, EquivalenceClass First,
EquivalenceClass Second);
RangeSet::Factory &F, ProgramStateRef State,
EquivalenceClass First, EquivalenceClass Second);

/// This is a unique identifier of the class.
uintptr_t ID;
Expand Down Expand Up @@ -740,8 +736,7 @@ struct EqualityInfo {
//===----------------------------------------------------------------------===//

template <class SecondTy, class... RestTy>
LLVM_NODISCARD inline RangeSet intersect(BasicValueFactory &BV,
RangeSet::Factory &F, RangeSet Head,
LLVM_NODISCARD inline RangeSet intersect(RangeSet::Factory &F, RangeSet Head,
SecondTy Second, RestTy... Tail);

template <class... RangeTy> struct IntersectionTraits;
Expand All @@ -764,15 +759,14 @@ struct IntersectionTraits<OptionalOrPointer, TailTy...> {
};

template <class EndTy>
LLVM_NODISCARD inline EndTy intersect(BasicValueFactory &BV,
RangeSet::Factory &F, EndTy End) {
LLVM_NODISCARD inline EndTy intersect(RangeSet::Factory &F, EndTy End) {
// If the list contains only RangeSet or Optional<RangeSet>, simply return
// that range set.
return End;
}

LLVM_NODISCARD LLVM_ATTRIBUTE_UNUSED inline Optional<RangeSet>
intersect(BasicValueFactory &BV, RangeSet::Factory &F, const RangeSet *End) {
intersect(RangeSet::Factory &F, const RangeSet *End) {
// This is an extraneous conversion from a raw pointer into Optional<RangeSet>
if (End) {
return *End;
Expand All @@ -781,25 +775,23 @@ intersect(BasicValueFactory &BV, RangeSet::Factory &F, const RangeSet *End) {
}

template <class... RestTy>
LLVM_NODISCARD inline RangeSet intersect(BasicValueFactory &BV,
RangeSet::Factory &F, RangeSet Head,
LLVM_NODISCARD inline RangeSet intersect(RangeSet::Factory &F, RangeSet Head,
RangeSet Second, RestTy... Tail) {
// Here we call either the <RangeSet,RangeSet,...> or <RangeSet,...> version
// of the function and can be sure that the result is RangeSet.
return intersect(BV, F, F.intersect(Head, Second), Tail...);
return intersect(F, F.intersect(Head, Second), Tail...);
}

template <class SecondTy, class... RestTy>
LLVM_NODISCARD inline RangeSet intersect(BasicValueFactory &BV,
RangeSet::Factory &F, RangeSet Head,
LLVM_NODISCARD inline RangeSet intersect(RangeSet::Factory &F, RangeSet Head,
SecondTy Second, RestTy... Tail) {
if (Second) {
// Here we call the <RangeSet,RangeSet,...> version of the function...
return intersect(BV, F, Head, *Second, Tail...);
return intersect(F, Head, *Second, Tail...);
}
// ...and here it is either <RangeSet,RangeSet,...> or <RangeSet,...>, which
// means that the result is definitely RangeSet.
return intersect(BV, F, Head, Tail...);
return intersect(F, Head, Tail...);
}

/// Main generic intersect function.
Expand All @@ -824,12 +816,12 @@ LLVM_NODISCARD inline RangeSet intersect(BasicValueFactory &BV,
template <class HeadTy, class SecondTy, class... RestTy>
LLVM_NODISCARD inline
typename IntersectionTraits<HeadTy, SecondTy, RestTy...>::Type
intersect(BasicValueFactory &BV, RangeSet::Factory &F, HeadTy Head,
SecondTy Second, RestTy... Tail) {
intersect(RangeSet::Factory &F, HeadTy Head, SecondTy Second,
RestTy... Tail) {
if (Head) {
return intersect(BV, F, *Head, Second, Tail...);
return intersect(F, *Head, Second, Tail...);
}
return intersect(BV, F, Second, Tail...);
return intersect(F, Second, Tail...);
}

//===----------------------------------------------------------------------===//
Expand All @@ -845,9 +837,9 @@ class SymbolicRangeInferrer
: public SymExprVisitor<SymbolicRangeInferrer, RangeSet> {
public:
template <class SourceType>
static RangeSet inferRange(BasicValueFactory &BV, RangeSet::Factory &F,
ProgramStateRef State, SourceType Origin) {
SymbolicRangeInferrer Inferrer(BV, F, State);
static RangeSet inferRange(RangeSet::Factory &F, ProgramStateRef State,
SourceType Origin) {
SymbolicRangeInferrer Inferrer(F, State);
return Inferrer.infer(Origin);
}

Expand All @@ -872,9 +864,8 @@ class SymbolicRangeInferrer
}

private:
SymbolicRangeInferrer(BasicValueFactory &BV, RangeSet::Factory &F,
ProgramStateRef S)
: ValueFactory(BV), RangeFactory(F), State(S) {}
SymbolicRangeInferrer(RangeSet::Factory &F, ProgramStateRef S)
: ValueFactory(F.getValueFactory()), RangeFactory(F), State(S) {}

/// Infer range information from the given integer constant.
///
Expand All @@ -899,7 +890,7 @@ class SymbolicRangeInferrer

RangeSet infer(SymbolRef Sym) {
if (Optional<RangeSet> ConstraintBasedRange = intersect(
ValueFactory, RangeFactory, getConstraint(State, Sym),
RangeFactory, getConstraint(State, Sym),
// If Sym is a difference of symbols A - B, then maybe we have range
// set stored for B - A.
//
Expand Down Expand Up @@ -1536,12 +1527,12 @@ class RangeConstraintManager : public RangedConstraintManager {

ProgramStateRef trackDisequality(ProgramStateRef State, SymbolRef LHS,
SymbolRef RHS) {
return EquivalenceClass::markDisequal(getBasicVals(), F, State, LHS, RHS);
return EquivalenceClass::markDisequal(F, State, LHS, RHS);
}

ProgramStateRef trackEquality(ProgramStateRef State, SymbolRef LHS,
SymbolRef RHS) {
return EquivalenceClass::merge(getBasicVals(), F, State, LHS, RHS);
return EquivalenceClass::merge(F, State, LHS, RHS);
}

LLVM_NODISCARD ProgramStateRef setConstraint(ProgramStateRef State,
Expand Down Expand Up @@ -1674,19 +1665,17 @@ inline EquivalenceClass EquivalenceClass::find(ProgramStateRef State,
return Sym;
}

inline ProgramStateRef EquivalenceClass::merge(BasicValueFactory &BV,
RangeSet::Factory &F,
inline ProgramStateRef EquivalenceClass::merge(RangeSet::Factory &F,
ProgramStateRef State,
SymbolRef First,
SymbolRef Second) {
EquivalenceClass FirstClass = find(State, First);
EquivalenceClass SecondClass = find(State, Second);

return FirstClass.merge(BV, F, State, SecondClass);
return FirstClass.merge(F, State, SecondClass);
}

inline ProgramStateRef EquivalenceClass::merge(BasicValueFactory &BV,
RangeSet::Factory &F,
inline ProgramStateRef EquivalenceClass::merge(RangeSet::Factory &F,
ProgramStateRef State,
EquivalenceClass Other) {
// It is already the same class.
Expand Down Expand Up @@ -1714,15 +1703,14 @@ inline ProgramStateRef EquivalenceClass::merge(BasicValueFactory &BV,
// its members. Merging is not a trivial operation, so it's easier to
// merge the smaller class into the bigger one.
if (Members.getHeight() >= OtherMembers.getHeight()) {
return mergeImpl(BV, F, State, Members, Other, OtherMembers);
return mergeImpl(F, State, Members, Other, OtherMembers);
} else {
return Other.mergeImpl(BV, F, State, OtherMembers, *this, Members);
return Other.mergeImpl(F, State, OtherMembers, *this, Members);
}
}

inline ProgramStateRef
EquivalenceClass::mergeImpl(BasicValueFactory &ValueFactory,
RangeSet::Factory &RangeFactory,
EquivalenceClass::mergeImpl(RangeSet::Factory &RangeFactory,
ProgramStateRef State, SymbolSet MyMembers,
EquivalenceClass Other, SymbolSet OtherMembers) {
// Essentially what we try to recreate here is some kind of union-find
Expand All @@ -1745,7 +1733,7 @@ EquivalenceClass::mergeImpl(BasicValueFactory &ValueFactory,
// Intersection here makes perfect sense because both of these constraints
// must hold for the whole new class.
if (Optional<RangeSet> NewClassConstraint =
intersect(ValueFactory, RangeFactory, getConstraint(State, *this),
intersect(RangeFactory, getConstraint(State, *this),
getConstraint(State, Other))) {
// NOTE: Essentially, NewClassConstraint should NEVER be infeasible because
// range inferrer shouldn't generate ranges incompatible with
Expand Down Expand Up @@ -1858,25 +1846,22 @@ bool EquivalenceClass::isTriviallyDead(ProgramStateRef State,
return isTrivial(State) && Reaper.isDead(getRepresentativeSymbol());
}

inline ProgramStateRef EquivalenceClass::markDisequal(BasicValueFactory &VF,
RangeSet::Factory &RF,
inline ProgramStateRef EquivalenceClass::markDisequal(RangeSet::Factory &RF,
ProgramStateRef State,
SymbolRef First,
SymbolRef Second) {
return markDisequal(VF, RF, State, find(State, First), find(State, Second));
return markDisequal(RF, State, find(State, First), find(State, Second));
}

inline ProgramStateRef EquivalenceClass::markDisequal(BasicValueFactory &VF,
RangeSet::Factory &RF,
inline ProgramStateRef EquivalenceClass::markDisequal(RangeSet::Factory &RF,
ProgramStateRef State,
EquivalenceClass First,
EquivalenceClass Second) {
return First.markDisequal(VF, RF, State, Second);
return First.markDisequal(RF, State, Second);
}

inline ProgramStateRef
EquivalenceClass::markDisequal(BasicValueFactory &VF, RangeSet::Factory &RF,
ProgramStateRef State,
EquivalenceClass::markDisequal(RangeSet::Factory &RF, ProgramStateRef State,
EquivalenceClass Other) const {
// If we know that two classes are equal, we can only produce an infeasible
// state.
Expand All @@ -1889,9 +1874,9 @@ EquivalenceClass::markDisequal(BasicValueFactory &VF, RangeSet::Factory &RF,

// Disequality is a symmetric relation, so if we mark A as disequal to B,
// we should also mark B as disequalt to A.
if (!addToDisequalityInfo(DisequalityInfo, Constraints, VF, RF, State, *this,
if (!addToDisequalityInfo(DisequalityInfo, Constraints, RF, State, *this,
Other) ||
!addToDisequalityInfo(DisequalityInfo, Constraints, VF, RF, State, Other,
!addToDisequalityInfo(DisequalityInfo, Constraints, RF, State, Other,
*this))
return nullptr;

Expand All @@ -1906,8 +1891,8 @@ EquivalenceClass::markDisequal(BasicValueFactory &VF, RangeSet::Factory &RF,

inline bool EquivalenceClass::addToDisequalityInfo(
DisequalityMapTy &Info, ConstraintRangeTy &Constraints,
BasicValueFactory &VF, RangeSet::Factory &RF, ProgramStateRef State,
EquivalenceClass First, EquivalenceClass Second) {
RangeSet::Factory &RF, ProgramStateRef State, EquivalenceClass First,
EquivalenceClass Second) {

// 1. Get all of the required factories.
DisequalityMapTy::Factory &F = State->get_context<DisequalityMap>();
Expand All @@ -1930,7 +1915,7 @@ inline bool EquivalenceClass::addToDisequalityInfo(
if (const llvm::APSInt *Point = SecondConstraint->getConcreteValue()) {

RangeSet FirstConstraint = SymbolicRangeInferrer::inferRange(
VF, RF, State, First.getRepresentativeSymbol());
RF, State, First.getRepresentativeSymbol());

FirstConstraint = RF.deletePoint(FirstConstraint, *Point);

Expand Down Expand Up @@ -1985,7 +1970,7 @@ LLVM_NODISCARD ProgramStateRef EquivalenceClass::simplify(
// The simplified symbol should be the member of the original Class,
// however, it might be in another existing class at the moment. We
// have to merge these classes.
State = merge(SVB.getBasicValueFactory(), F, State, ClassOfSimplifiedSym);
State = merge(F, State, ClassOfSimplifiedSym);
if (!State)
return nullptr;
}
Expand Down Expand Up @@ -2274,12 +2259,12 @@ RangeConstraintManager::removeDeadBindings(ProgramStateRef State,

RangeSet RangeConstraintManager::getRange(ProgramStateRef State,
SymbolRef Sym) {
return SymbolicRangeInferrer::inferRange(getBasicVals(), F, State, Sym);
return SymbolicRangeInferrer::inferRange(F, State, Sym);
}

RangeSet RangeConstraintManager::getRange(ProgramStateRef State,
EquivalenceClass Class) {
return SymbolicRangeInferrer::inferRange(getBasicVals(), F, State, Class);
return SymbolicRangeInferrer::inferRange(F, State, Class);
}

//===------------------------------------------------------------------------===
Expand Down

0 comments on commit 8474bb1

Please sign in to comment.