================
@@ -1167,11 +1167,42 @@ class CIRCleanupScopeOpFlattening
     collectExitsInCleanup(cleanupBodyRegion, /*ignoreBreak=*/false);
   }
 
+  // Whether the slot \p alloca read by \p loadOp may change before the end of
+  // \p cleanupRegion: its address escapes, or a store or lifetime marker on it
+  // runs after the load, in the load's block or in the cleanup.
+  static bool slotMayChangeBeforeCleanupEnd(cir::AllocaOp alloca,
+                                            cir::LoadOp loadOp,
+                                            mlir::Region &cleanupRegion) {
+    mlir::Block *loadBlock = loadOp->getBlock();
+    return llvm::any_of(alloca->getUsers(), [&](mlir::Operation *user) {
+      // Reads never change the slot.
+      if (mlir::isa<cir::LoadOp>(user))
+        return false;
+
+      // Storing the address, or using it in anything but a store to the slot
+      // or a lifetime marker, lets it escape.
+      if (auto storeOp = mlir::dyn_cast<cir::StoreOp>(user)) {
+        if (storeOp.getValue() == alloca.getResult())
+          return true;
+      } else if (!mlir::isa<cir::LifetimeStartOp, cir::LifetimeEndOp>(user)) {
+        return true;
----------------
bcardosolopes wrote:

(This `return true` is for uses that let the address escape. Lifetime markers 
skip that line and get the "does it run after the load" check instead.)

Only a lifetime.end in the cleanup, or a marker after the load in its block, 
blocks. In those cases sinking would be wrong because the sunk load would read 
x after its lifetime ends, so the load has to stay ahead of the cleanup. The 
cost is the __ret_operand_tmp store/reload around the cleanup, which 
mem2reg/SROA remove. I'd rather leave it to the optimizer!

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

Reply via email to