================
@@ -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) {
----------------
bcardosolopes wrote:

IIUC, your note on `isAllocaPreservingCast` says the opposite (CIROps.td:335): 
decay "changes the scope of the memory object, so we exclude it here". 
Stripping it here is what gets you `arr#0 <-> ptr#0: MustAlias`, whole array 
against first element.

Which one is right? If the strip is fine for AA the note wants updating, and 
this probably belongs behind a second predicate on `CastOp` rather than an `||` 
at the use site.

https://github.com/llvm/llvm-project/pull/215683
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to