clang 20.0.0git
Arena.h
Go to the documentation of this file.
1//===-- Arena.h -------------------------------*- C++ -------------------*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8#ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE__ARENA_H
9#define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE__ARENA_H
10
14#include "llvm/ADT/StringRef.h"
15#include <vector>
16
17namespace clang::dataflow {
18
19/// The Arena owns the objects that model data within an analysis.
20/// For example, `Value`, `StorageLocation`, `Atom`, and `Formula`.
21class Arena {
22public:
24 : True(Formula::create(Alloc, Formula::Literal, {}, 1)),
25 False(Formula::create(Alloc, Formula::Literal, {}, 0)) {}
26 Arena(const Arena &) = delete;
27 Arena &operator=(const Arena &) = delete;
28
29 /// Creates a `T` (some subclass of `StorageLocation`), forwarding `args` to
30 /// the constructor, and returns a reference to it.
31 ///
32 /// The `Arena` takes ownership of the created object. The object will be
33 /// destroyed when the `Arena` is destroyed.
34 template <typename T, typename... Args>
35 std::enable_if_t<std::is_base_of<StorageLocation, T>::value, T &>
36 create(Args &&...args) {
37 // Note: If allocation of individual `StorageLocation`s turns out to be
38 // costly, consider creating specializations of `create<T>` for commonly
39 // used `StorageLocation` subclasses and make them use a `BumpPtrAllocator`.
40 return *cast<T>(
41 Locs.emplace_back(std::make_unique<T>(std::forward<Args>(args)...))
42 .get());
43 }
44
45 /// Creates a `T` (some subclass of `Value`), forwarding `args` to the
46 /// constructor, and returns a reference to it.
47 ///
48 /// The `Arena` takes ownership of the created object. The object will be
49 /// destroyed when the `Arena` is destroyed.
50 template <typename T, typename... Args>
51 std::enable_if_t<std::is_base_of<Value, T>::value, T &>
52 create(Args &&...args) {
53 // Note: If allocation of individual `Value`s turns out to be costly,
54 // consider creating specializations of `create<T>` for commonly used
55 // `Value` subclasses and make them use a `BumpPtrAllocator`.
56 return *cast<T>(
57 Vals.emplace_back(std::make_unique<T>(std::forward<Args>(args)...))
58 .get());
59 }
60
61 /// Creates a BoolValue wrapping a particular formula.
62 ///
63 /// Passing in the same formula will result in the same BoolValue.
64 /// FIXME: Interning BoolValues but not other Values is inconsistent.
65 /// Decide whether we want Value interning or not.
67
68 /// Creates a fresh atom and wraps in in an AtomicBoolValue.
69 /// FIXME: For now, identical-address AtomicBoolValue <=> identical atom.
70 /// Stop relying on pointer identity and remove this guarantee.
72 return cast<AtomicBoolValue>(makeBoolValue(makeAtomRef(makeAtom())));
73 }
74
75 /// Creates a fresh Top boolean value.
77 // No need for deduplicating: there's no way to create aliasing Tops.
78 return create<TopBoolValue>(makeAtomRef(makeAtom()));
79 }
80
81 /// Returns a symbolic integer value that models an integer literal equal to
82 /// `Value`. These literals are the same every time.
83 /// Integer literals are not typed; the type is determined by the `Expr` that
84 /// an integer literal is associated with.
85 IntegerValue &makeIntLiteral(llvm::APInt Value);
86
87 // Factories for boolean formulas.
88 // Formulas are interned: passing the same arguments return the same result.
89 // For commutative operations like And/Or, interning ignores order.
90 // Simplifications are applied: makeOr(X, X) => X, etc.
91
92 /// Returns a formula for the conjunction of `LHS` and `RHS`.
93 const Formula &makeAnd(const Formula &LHS, const Formula &RHS);
94
95 /// Returns a formula for the disjunction of `LHS` and `RHS`.
96 const Formula &makeOr(const Formula &LHS, const Formula &RHS);
97
98 /// Returns a formula for the negation of `Val`.
99 const Formula &makeNot(const Formula &Val);
100
101 /// Returns a formula for `LHS => RHS`.
102 const Formula &makeImplies(const Formula &LHS, const Formula &RHS);
103
104 /// Returns a formula for `LHS <=> RHS`.
105 const Formula &makeEquals(const Formula &LHS, const Formula &RHS);
106
107 /// Returns a formula for the variable A.
108 const Formula &makeAtomRef(Atom A);
109
110 /// Returns a formula for a literal true/false.
111 const Formula &makeLiteral(bool Value) { return Value ? True : False; }
112
113 // Parses a formula from its textual representation.
114 // This may refer to atoms that were not produced by makeAtom() yet!
116
117 /// Returns a new atomic boolean variable, distinct from any other.
118 Atom makeAtom() { return static_cast<Atom>(NextAtom++); };
119
120 /// Creates a fresh flow condition and returns a token that identifies it. The
121 /// token can be used to perform various operations on the flow condition such
122 /// as adding constraints to it, forking it, joining it with another flow
123 /// condition, or checking implications.
125
126private:
127 llvm::BumpPtrAllocator Alloc;
128
129 // Storage for the state of a program.
130 std::vector<std::unique_ptr<StorageLocation>> Locs;
131 std::vector<std::unique_ptr<Value>> Vals;
132
133 // Indices that are used to avoid recreating the same integer literals and
134 // composite boolean values.
135 llvm::DenseMap<llvm::APInt, IntegerValue *> IntegerLiterals;
136 using FormulaPair = std::pair<const Formula *, const Formula *>;
137 llvm::DenseMap<FormulaPair, const Formula *> Ands;
138 llvm::DenseMap<FormulaPair, const Formula *> Ors;
139 llvm::DenseMap<const Formula *, const Formula *> Nots;
140 llvm::DenseMap<FormulaPair, const Formula *> Implies;
141 llvm::DenseMap<FormulaPair, const Formula *> Equals;
142 llvm::DenseMap<Atom, const Formula *> AtomRefs;
143
144 llvm::DenseMap<const Formula *, BoolValue *> FormulaValues;
145 unsigned NextAtom = 0;
146
147 const Formula &True, &False;
148};
149
150} // namespace clang::dataflow
151
152#endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE__ARENA_H
The Arena owns the objects that model data within an analysis.
Definition: Arena.h:21
const Formula & makeEquals(const Formula &LHS, const Formula &RHS)
Returns a formula for LHS <=> RHS.
Definition: Arena.cpp:91
const Formula & makeAtomRef(Atom A)
Returns a formula for the variable A.
Definition: Arena.cpp:34
Atom makeAtom()
Returns a new atomic boolean variable, distinct from any other.
Definition: Arena.h:118
IntegerValue & makeIntLiteral(llvm::APInt Value)
Returns a symbolic integer value that models an integer literal equal to Value.
Definition: Arena.cpp:104
const Formula & makeNot(const Formula &Val)
Returns a formula for the negation of Val.
Definition: Arena.cpp:67
std::enable_if_t< std::is_base_of< Value, T >::value, T & > create(Args &&...args)
Creates a T (some subclass of Value), forwarding args to the constructor, and returns a reference to ...
Definition: Arena.h:52
Atom makeFlowConditionToken()
Creates a fresh flow condition and returns a token that identifies it.
Definition: Arena.h:124
TopBoolValue & makeTopValue()
Creates a fresh Top boolean value.
Definition: Arena.h:76
const Formula & makeOr(const Formula &LHS, const Formula &RHS)
Returns a formula for the disjunction of LHS and RHS.
Definition: Arena.cpp:54
BoolValue & makeBoolValue(const Formula &)
Creates a BoolValue wrapping a particular formula.
Definition: Arena.cpp:112
const Formula & makeAnd(const Formula &LHS, const Formula &RHS)
Returns a formula for the conjunction of LHS and RHS.
Definition: Arena.cpp:41
AtomicBoolValue & makeAtomValue()
Creates a fresh atom and wraps in in an AtomicBoolValue.
Definition: Arena.h:71
const Formula & makeImplies(const Formula &LHS, const Formula &RHS)
Returns a formula for LHS => RHS.
Definition: Arena.cpp:78
Arena(const Arena &)=delete
const Formula & makeLiteral(bool Value)
Returns a formula for a literal true/false.
Definition: Arena.h:111
llvm::Expected< const Formula & > parseFormula(llvm::StringRef)
Definition: Arena.cpp:202
std::enable_if_t< std::is_base_of< StorageLocation, T >::value, T & > create(Args &&...args)
Creates a T (some subclass of StorageLocation), forwarding args to the constructor,...
Definition: Arena.h:36
Arena & operator=(const Arena &)=delete
Models an atomic boolean.
Definition: Value.h:133
Models a boolean.
Definition: Value.h:94
@ Literal
Constant true or false.
Definition: Formula.h:56
static const Formula & create(llvm::BumpPtrAllocator &Alloc, Kind K, ArrayRef< const Formula * > Operands, unsigned Value=0)
Definition: Formula.cpp:20
Models an integer.
Definition: Value.h:160
A TopBoolValue represents a boolean that is explicitly unconstrained.
Definition: Value.h:116
Base class for all values computed by abstract interpretation.
Definition: Value.h:33
Dataflow Directional Tag Classes.
Definition: AdornedCFG.h:29
Atom
Identifies an atomic boolean variable such as "V1".
Definition: Formula.h:34
uint32_t Literal
Literals are represented as positive integers.
Definition: CNFFormula.h:35
const FunctionProtoType * T