10#include "mlir/IR/Remarks.h"
12#include "llvm/ADT/STLExtras.h"
13#include "llvm/ADT/TypeSwitch.h"
14#include "llvm/Support/DebugLog.h"
18#define DEBUG_TYPE "cir-points-to-analysis"
24 llvm::StringRef name =
object.getDefiningOp<cir::AllocaOp>().getName();
25 return name.empty() ?
"<unnamed>" : name;
30 mlir::Operation *root,
31 llvm::StringRef subCategory) {
32 mlir::remark::RemarkOpts opts = mlir::remark::RemarkOpts::name(name)
33 .category(
"points-to")
34 .subCategory(subCategory);
35 if (
auto func = mlir::dyn_cast<cir::FuncOp>(root))
36 opts = opts.function(func.getSymName());
41static mlir::WalkResult
43 llvm::function_ref<mlir::WalkResult(mlir::Operation *)> callback) {
44 for (mlir::Region ®ion : op->getRegions())
45 if (region.walk(callback).wasInterrupted())
46 return mlir::WalkResult::interrupt();
47 return mlir::WalkResult::advance();
55 assert(!sizeLimitHit &&
"merging into a set that hit the size limit");
56 bool changed = objects.set_union(other.objects);
58 changed |= insertUnknown();
62void PointsToSet::replaceWithUnknown() {
73 names.push_back(
"unknown");
75 llvm::interleaveComma(names, os);
79#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
92 : root(op), limits(limits) {
93 unknownSet.insertUnknown();
97void PointsToAnalysis::compute(mlir::Operation *op) {
103 uint64_t numOperations = 0;
104 mlir::WalkResult tooLarge =
walkNested(op, [&](mlir::Operation *nested) {
106 return mlir::WalkResult::interrupt();
107 llvm::TypeSwitch<mlir::Operation *>(nested)
108 .Case([&](cir::AllocaOp alloca) {
109 valuePointsTo[alloca.getResult()].insert(alloca.getResult());
111 .Case([&](cir::LoadOp load) {
112 valuePointsTo.try_emplace(load.getResult());
114 .Case([&](cir::ConstantOp constant) {
115 if (constant.isNullPtr())
116 valuePointsTo.try_emplace(constant.getResult());
118 return mlir::WalkResult::advance();
120 if (tooLarge.wasInterrupted()) {
122 operationLimitHit =
true;
128 uint64_t budget = std::max<uint64_t>(
129 limits.minWork,
uint64_t(limits.workPerOperation) * numOperations);
131 unsigned iterations = 0;
135 mlir::WalkResult result =
walkNested(op, [&](mlir::Operation *nested) {
137 return mlir::WalkResult::interrupt();
138 changed |= visit(nested);
139 return mlir::WalkResult::advance();
141 if (result.wasInterrupted()) {
143 workLimitHit = budget;
148 LDBG() <<
"Solved in " << iterations <<
" iterations";
152 if (!root->getContext()->getRemarkEngine())
155 if (operationLimitHit)
156 mlir::remark::missed(root->getLoc(),
158 << mlir::remark::add(
"gave up: more than {0} operations",
159 limits.maxOperations);
161 mlir::remark::missed(root->getLoc(),
163 << mlir::remark::add(
"gave up: more than {0} operation visits",
165 for (
auto [
object, op] : escaped)
166 mlir::remark::missed(op->getLoc(),
168 << mlir::remark::add(
"the address of {0} escapes",
170 for (
auto [op, size] : sizeLimitHits)
171 mlir::remark::missed(op->getLoc(),
173 << mlir::remark::add(
174 "a set of {0} objects is replaced by unknown, the limit is {1}",
175 size, limits.maxSetSize);
177 root->walk([&](cir::LoadOp load) {
178 if (!mlir::isa<cir::PointerType>(load.getType()))
180 mlir::remark::analysis(load.getLoc(),
182 << mlir::remark::add(
"points to {0}",
getPointsTo(load.getResult()));
187 auto it = valuePointsTo.find(value);
188 if (it == valuePointsTo.end())
193void PointsToAnalysis::dropAllSets() {
196 valuePointsTo.clear();
197 memoryPointsTo.clear();
199 sizeLimitHits.clear();
203 mlir::Operation *op) {
207 if (to.hitSizeLimit())
210 bool changed = to.merge(from);
218 sizeLimitHits.emplace_back(op, objects.size());
219 to.replaceWithUnknown();
222 markEscaped(objects, op);
226bool PointsToAnalysis::markEscaped(llvm::ArrayRef<mlir::Value> objects,
227 mlir::Operation *op) {
228 bool changed =
false;
229 llvm::SmallVector<mlir::Value> worklist;
230 for (mlir::Value
object : objects)
231 if (escaped.insert({object, op}).second)
232 worklist.push_back(
object);
234 while (!worklist.empty()) {
235 mlir::Value
object = worklist.pop_back_val();
240 PointsToSet &memory = memoryPointsTo[object];
241 memory.insertUnknown();
243 if (escaped.insert({inner, op}).second)
244 worklist.push_back(inner);
249bool PointsToAnalysis::visit(mlir::Operation *op) {
253 auto escapeOperands = [&] {
254 bool changed =
false;
255 for (mlir::Value operand : op->getOperands())
256 changed |= markEscaped(
getPointsTo(operand).getObjects(), op);
260 return llvm::TypeSwitch<mlir::Operation *, bool>(op)
261 .Case([&](cir::StoreOp store) {
262 const PointsToSet &addr =
getPointsTo(store.getAddr());
263 const PointsToSet &value =
getPointsTo(store.getValue());
264 bool changed =
false;
265 for (mlir::Value
object : addr.
getObjects()) {
266 changed |= addTo(memoryPointsTo[
object], value, op);
267 if (escaped.contains(
object))
268 changed |= markEscaped(value.
getObjects(), op);
273 changed |= markEscaped(value.
getObjects(), op);
276 .Case([&](cir::LoadOp load) {
277 const PointsToSet &addr =
getPointsTo(load.getAddr());
279 for (mlir::Value
object : addr.
getObjects()) {
280 auto it = memoryPointsTo.find(
object);
281 if (it != memoryPointsTo.end())
282 loaded.merge(it->second);
285 loaded.insertUnknown();
286 return addTo(valuePointsTo[load.getResult()], loaded, op);
290 .Case([&](cir::CmpOp) {
return false; })
291 .Case([&](cir::CastOp cast) {
292 if (
cast.getKind() == cir::CastKind::ptr_to_bool)
294 return escapeOperands();
296 .
Default([&](mlir::Operation *) {
return escapeOperands(); });
static mlir::remark::RemarkOpts getRemarkOpts(llvm::StringRef name, mlir::Operation *root, llvm::StringRef subCategory)
The options of every remark emitted for the analysis of root.
static llvm::StringRef getObjectName(mlir::Value object)
The name used to print object: the name of the cir.alloca allocating it.
static mlir::WalkResult walkNested(mlir::Operation *op, llvm::function_ref< mlir::WalkResult(mlir::Operation *)> callback)
Walk the operations nested in op, without op itself.
const PointsToSet & getPointsTo(mlir::Value value) const
Return the objects value may point to.
PointsToAnalysis(mlir::Operation *op, const PointsToLimits &limits=PointsToLimits())
void emitRemarks(llvm::StringRef subCategory={}) const
Report the results as optimization remarks in the "points-to" category, when remarks are enabled for ...
The objects a value may point to.
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" ...
@ Default
Set to the current date and time.
U cast(CodeGen::Address addr)
__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.