[clang] [CIR] Add a points-to analysis skeleton (PR #244356)
Henrich Lauko via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 03:49:05 PST 2026
================
@@ -0,0 +1,269 @@
+//===- CIRPointsToAnalysis.cpp - Points-to analysis for CIR ---------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/CIR/Dialect/Analysis/CIRPointsToAnalysis.h"
+#include "mlir/IR/Remarks.h"
+#include "clang/CIR/Dialect/IR/CIRDialect.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/TypeSwitch.h"
+#include "llvm/Support/DebugLog.h"
+
+#include <algorithm>
+
+#define DEBUG_TYPE "cir-points-to-analysis"
+
+using namespace cir;
+
+/// The name used to print `object`: the name of the cir.alloca allocating it.
+static llvm::StringRef getObjectName(mlir::Value object) {
+ llvm::StringRef name = object.getDefiningOp<cir::AllocaOp>().getName();
+ return name.empty() ? "<unnamed>" : name;
+}
+
+/// The options of every remark emitted for the analysis of `root`.
+static mlir::remark::RemarkOpts getRemarkOpts(llvm::StringRef name,
+ mlir::Operation *root,
+ llvm::StringRef subCategory) {
+ mlir::remark::RemarkOpts opts = mlir::remark::RemarkOpts::name(name)
+ .category("points-to")
+ .subCategory(subCategory);
+ if (auto func = mlir::dyn_cast<cir::FuncOp>(root))
+ opts = opts.function(func.getSymName());
+ return opts;
+}
+
+//===----------------------------------------------------------------------===//
+// PointsToSet
+//===----------------------------------------------------------------------===//
+
+bool PointsToSet::merge(const PointsToSet &other) {
+ assert(!sizeLimitHit && "merging into a set that hit the size limit");
+ bool changed = objects.set_union(other.objects);
+ if (other.unknown)
+ changed |= insertUnknown();
+ return changed;
+}
+
+void PointsToSet::replaceWithUnknown() {
+ objects.clear();
+ unknown = true;
+ sizeLimitHit = true;
+}
+
+void PointsToSet::print(llvm::raw_ostream &os) const {
+ llvm::SmallVector<llvm::StringRef> names =
+ llvm::map_to_vector(objects, getObjectName);
+ llvm::sort(names);
+ if (unknown)
+ names.push_back("unknown");
+ os << '[';
+ llvm::interleaveComma(names, os);
+ os << ']';
+}
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+void PointsToSet::dump() const {
+ print(llvm::errs());
+ llvm::errs() << '\n';
+}
+#endif
+
+//===----------------------------------------------------------------------===//
+// PointsToAnalysis
+//===----------------------------------------------------------------------===//
+
+PointsToAnalysis::PointsToAnalysis(mlir::Operation *op,
+ const PointsToLimits &limits)
+ : root(op), limits(limits) {
+ unknownSet.insertUnknown();
+ compute(op);
+}
+
+void PointsToAnalysis::compute(mlir::Operation *op) {
+ // Create the sets of the values the analysis computes before solving, so a
+ // value never changes from "not analyzed", which reads as "unknown", to a set
+ // with fewer members. Each cir.alloca result points to the object it
+ // allocates, each cir.load result starts out pointing to nothing, and a null
+ // pointer constant points to nothing.
+ uint64_t numOperations = 0;
+ mlir::WalkResult tooLarge = op->walk([&](mlir::Operation *nested) {
+ if (nested == op)
----------------
xlauko wrote:
Both walks start at `op` and then skip it with `if (nested == op)` on every callback. Walking `op`'s regions directly would remove the check from both lambdas.
https://github.com/llvm/llvm-project/pull/244356
More information about the cfe-commits
mailing list