================
@@ -20,17 +21,117 @@ using namespace cir;
// Helpers
//===----------------------------------------------------------------------===//
+static constexpr unsigned MaxLookupDepth = 6;
+
mlir::Value CIRBasicAliasAnalysis::getUnderlyingObject(mlir::Value val) {
LDBG() << "Getting underlying object for: " << val;
- // TODO: Walk through cir.ptr_stride, cir.cast, cir.get_member, etc.
- // to find the root allocation (cir.alloca, cir.global_addr, function args).
- LDBG() << "Not yet implemented";
+ for (unsigned depth = 0; depth < MaxLookupDepth; ++depth) {
+ mlir::Operation *defOp = val.getDefiningOp();
+ if (!defOp) {
+ LDBG() << "No defining operation, stopping";
+ break; // Block argument (e.g. function parameter) — stop here.
+ }
+
+ // Bitcast and address-space casts don't change the underlying object.
+ // array_to_ptrdecay produces an element pointer to the same storage as
+ // the array pointer, so strip through it too.
+ if (auto castOp = mlir::dyn_cast<cir::CastOp>(defOp)) {
+ if (castOp.isAllocaPreservingCast() ||
+ castOp.getKind() == cir::CastKind::array_to_ptrdecay) {
+ LDBG() << "Walking past cast operation";
+ val = castOp.getSrc();
+ continue;
+ }
+ LDBG() << "Opaque cast operation, stopping";
+ break;
+ }
+
+ // Pointer stride: only strip through when we can prove the access stays
----------------
bcardosolopes wrote:
Nothing here checks bounds, it handles a constant zero stride and gives up,
same for "unverifiable bounds" below. `ptr_stride_inbounds_strips_to_alloca`
has the same problem, it expects MayAlias on every `elem` line, so it doesn't
strip either. Maybe this should be a TODO?
Also `PtrStrideOp` and `GetElementOp` run the same constant-zero test in two
different shapes. One helper would keep them from drifting.
https://github.com/llvm/llvm-project/pull/215683
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits