clang 24.0.0git
CIRPointsToAnalysis.h
Go to the documentation of this file.
1//===- CIRPointsToAnalysis.h - Points-to analysis for CIR -------*- 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//
9// This file defines PointsToAnalysis, which computes the set of objects each
10// value in an operation may point to.
11//
12// The analysis is inclusion based and flow insensitive: a pointer stored into
13// a memory location at any point may be loaded back from it at any point.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef CLANG_CIR_DIALECT_ANALYSIS_CIRPOINTSTOANALYSIS_H
18#define CLANG_CIR_DIALECT_ANALYSIS_CIRPOINTSTOANALYSIS_H
19
20#include "mlir/IR/Operation.h"
21#include "mlir/IR/Value.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/MapVector.h"
25#include "llvm/ADT/SetVector.h"
26#include "llvm/Support/Compiler.h"
27#include "llvm/Support/raw_ostream.h"
28
29#include <utility>
30
31namespace cir {
32
33/// The objects a value may point to.
34///
35/// An object is identified by the value that allocates it, which for now is
36/// always the result of a cir.alloca. A set may also contain "unknown", which
37/// stands for any object the analysis does not track: memory reached through a
38/// function argument or a global, memory written by code the analysis cannot
39/// see, and any tracked object whose address has escaped.
41public:
42 /// The tracked objects in the set.
44 return objects.getArrayRef();
45 }
46
47 /// Whether the set contains "unknown". "unknown" may stand for any object
48 /// whose address escaped, including tracked ones, so a set that contains it
49 /// may point to any object.
50 bool containsUnknown() const { return unknown; }
51
52 /// Print the set as the sorted names of the cir.alloca operations of its
53 /// objects, followed by "unknown" if the set contains it, e.g. "[a, b]".
54 void print(llvm::raw_ostream &os) const;
55 LLVM_DUMP_METHOD void dump() const;
56
57private:
58 friend class PointsToAnalysis;
59
60 /// Whether the set grew past the size limit of the analysis and was replaced
61 /// by "unknown". Such a set no longer names the objects it held.
62 bool hitSizeLimit() const { return sizeLimitHit; }
63
64 /// Add `object` to the set. Return true if the set changed.
65 bool insert(mlir::Value object) { return objects.insert(object); }
66
67 /// Add "unknown" to the set. Return true if the set changed.
68 bool insertUnknown() { return !std::exchange(unknown, true); }
69
70 /// Add every member of `other` to the set. Return true if the set changed.
71 bool merge(const PointsToSet &other);
72
73 /// Remove the tracked objects and keep only "unknown". The set stays this way
74 /// from then on.
75 void replaceWithUnknown();
76
77 llvm::SmallSetVector<mlir::Value, 4> objects;
78 bool unknown = false;
79 bool sizeLimitHit = false;
80};
81
82inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
83 const PointsToSet &set) {
84 set.print(os);
85 return os;
86}
87
88/// Limits on the work done by PointsToAnalysis. When the operation is too
89/// large, or the sets take too long to compute, the analysis gives up, and
90/// every value points to "unknown".
92 /// The largest number of operations the analysis is computed for.
93 unsigned maxOperations = 5000;
94
95 /// The work allowed to compute the sets, counted as the number of times an
96 /// operation is visited. It is `workPerOperation` for each operation, and at
97 /// least `minWork`.
98 unsigned workPerOperation = 32;
99 unsigned minWork = 100000;
100
101 /// The largest number of objects a set holds. A set that grows past it is
102 /// replaced by "unknown", and its objects escape.
103 unsigned maxSetSize = 100;
104};
105
106/// Computes the points-to set of every value in an operation.
107///
108/// The analysis looks at the operations nested in the operation it is built
109/// for and tracks the addresses of cir.alloca objects through cir.store and
110/// cir.load. Comparing a tracked address or testing it for null doesn't let
111/// code reach the object, but any other use of a tracked address is treated
112/// as an escape: the object may then be read or written by code the analysis
113/// does not model, so its memory is assumed to hold "unknown", and everything
114/// stored in it escapes too. Values the analysis does not compute a set for,
115/// such as function arguments or the results of operations it does not model,
116/// point to "unknown". A null pointer constant points to no object.
117///
118/// The sets describe the IR at the time the analysis is built and are not
119/// updated when it changes.
121public:
122 explicit PointsToAnalysis(mlir::Operation *op,
123 const PointsToLimits &limits = PointsToLimits());
126
127 /// Return the objects `value` may point to. Values that were not analyzed
128 /// point to "unknown".
129 const PointsToSet &getPointsTo(mlir::Value value) const;
130
131 /// Report the results as optimization remarks in the "points-to" category,
132 /// when remarks are enabled for it: the set of every loaded pointer as an
133 /// analysis remark, and what made the analysis lose precision as missed
134 /// remarks: the objects that escaped, the sets that were replaced by
135 /// "unknown", and the operation or work limit, if the analysis gave up on
136 /// one. `subCategory` names the client, if any.
137 /// The analysis doesn't emit remarks by itself.
138 void emitRemarks(llvm::StringRef subCategory = {}) const;
139
140private:
141 /// Compute the sets of the values in `op`.
142 void compute(mlir::Operation *op);
143
144 /// Apply the effect of `op` on the points-to sets. Return true if any set
145 /// changed.
146 bool visit(mlir::Operation *op);
147
148 /// Add the members of `from` to `to`, replacing `to` by "unknown" if it grows
149 /// past the size limit. `op` is the operation being visited. Return true if
150 /// anything changed.
151 bool addTo(PointsToSet &to, const PointsToSet &from, mlir::Operation *op);
152
153 /// Record that `objects` may be accessed by code the analysis cannot see,
154 /// because of `op`. Return true if anything changed.
155 bool markEscaped(llvm::ArrayRef<mlir::Value> objects, mlir::Operation *op);
156
157 /// Drop every set, so that every value points to "unknown".
158 void dropAllSets();
159
160 /// The operation the analysis is built for.
161 mlir::Operation *root;
162
163 PointsToLimits limits;
164
165 /// The objects each value may point to.
166 llvm::DenseMap<mlir::Value, PointsToSet> valuePointsTo;
167
168 /// The objects the memory of each tracked object may point to.
169 llvm::DenseMap<mlir::Value, PointsToSet> memoryPointsTo;
170
171 /// Tracked objects whose address escaped, and the operation that first made
172 /// each of them escape.
173 llvm::MapVector<mlir::Value, mlir::Operation *> escaped;
174
175 /// The operations that made a set grow past the size limit, and the number of
176 /// objects the set held.
178
179 /// Whether the analysis gave up because the operation has more than
180 /// `maxOperations` operations.
181 bool operationLimitHit = false;
182
183 /// The work limit, in operation visits, if the analysis gave up on it, and 0
184 /// otherwise.
185 uint64_t workLimitHit = 0;
186
187 /// The set returned for values that were not analyzed.
188 PointsToSet unknownSet;
189};
190
191} // namespace cir
192
193#endif // CLANG_CIR_DIALECT_ANALYSIS_CIRPOINTSTOANALYSIS_H
const PointsToSet & getPointsTo(mlir::Value value) const
Return the objects value may point to.
PointsToAnalysis(mlir::Operation *op, const PointsToLimits &limits=PointsToLimits())
PointsToAnalysis(const PointsToAnalysis &)=delete
void emitRemarks(llvm::StringRef subCategory={}) const
Report the results as optimization remarks in the "points-to" category, when remarks are enabled for ...
PointsToAnalysis & operator=(const PointsToAnalysis &)=delete
The objects a value may point to.
friend class PointsToAnalysis
bool containsUnknown() const
Whether the set contains "unknown".
LLVM_DUMP_METHOD void dump() const
llvm::ArrayRef< mlir::Value > getObjects() const
The tracked objects in the set.
void print(llvm::raw_ostream &os) const
Print the set as the sorted names of the cir.alloca operations of its objects, followed by "unknown" ...
llvm::raw_ostream & operator<<(llvm::raw_ostream &os, const PointsToSet &set)
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
Limits on the work done by PointsToAnalysis.
unsigned maxOperations
The largest number of operations the analysis is computed for.
unsigned maxSetSize
The largest number of objects a set holds.
unsigned workPerOperation
The work allowed to compute the sets, counted as the number of times an operation is visited.