Author: Bruno Cardoso Lopes
Date: 2026-11-21T15:25:40-08:00
New Revision: 8742bd9d13bcc55eaff79dab0c825a8bbe53104e

URL: 
https://github.com/llvm/llvm-project/commit/8742bd9d13bcc55eaff79dab0c825a8bbe53104e
DIFF: 
https://github.com/llvm/llvm-project/commit/8742bd9d13bcc55eaff79dab0c825a8bbe53104e.diff

LOG: [CIR] Load return values before cleanups that can change them (#244202)

When FlattenCFG routes a `cir.return` through a cleanup, it sinks the
load feeding the return past the cleanup. If the cleanup stores to the
slot, ends its lifetime, or calls something holding its address, the
function returns the post-cleanup value.

Sink the load only when the slot provably can't change: its address does
not escape and nothing after the load or in the cleanup uses it.
Otherwise spill it through the existing return-operand temporary, as
volatile and atomic loads already are. Under NRVO, where return slot
escapes, the value is now read before the cleanups, which is also valid.

Fixing this issue unblocks more usage of mem2reg in CIR.

Added: 
    clang/test/CIR/Transforms/flatten-cleanup-scope-return-load.cir

Modified: 
    clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp 
b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
index f11af3f4c6467..d1936b9f01d18 100644
--- a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
@@ -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;
+      }
+
+      // A store or lifetime marker only matters if it runs after the load.
+      if (cleanupRegion.isAncestor(user->getParentRegion()))
+        return true;
+      mlir::Operation *userInBlock = loadBlock->findAncestorOpInBlock(*user);
+      return userInBlock && loadOp->isBeforeInBlock(userInBlock);
+    });
+  }
+
   // Check if an operand's defining op should be moved to the destination 
block.
   // We only sink constants and simple loads. Anything else should be saved
-  // to a temporary alloca and reloaded at the destination block.
+  // to a temporary alloca and reloaded at the destination block. This must be
+  // called before \p cleanupRegion is inlined.
   static bool shouldSinkReturnOperand(mlir::Value operand,
-                                      cir::ReturnOp returnOp) {
+                                      cir::ReturnOp returnOp,
+                                      mlir::Region &cleanupRegion) {
     // Block arguments can't be moved
     mlir::Operation *defOp = operand.getDefiningOp();
     if (!defOp)
@@ -1201,12 +1232,12 @@ class CIRCleanupScopeOpFlattening
       assert(funcOp && "Return op has no function parent?");
       mlir::Block &funcEntryBlock = funcOp.getBody().front();
 
-      // Check if it's an alloca in the function entry block
-      if (auto allocaOp =
-              mlir::dyn_cast_if_present<cir::AllocaOp>(ptr.getDefiningOp()))
-        return allocaOp->getBlock() == &funcEntryBlock;
-
-      return false;
+      // Check if it's an alloca in the function entry block that keeps its
+      // value until the cleanup is done.
+      auto allocaOp =
+          mlir::dyn_cast_if_present<cir::AllocaOp>(ptr.getDefiningOp());
+      return allocaOp && allocaOp->getBlock() == &funcEntryBlock &&
+             !slotMayChangeBeforeCleanupEnd(allocaOp, loadOp, cleanupRegion);
     }
 
     // Make sure we only fall through to here with constants.
@@ -1216,19 +1247,20 @@ class CIRCleanupScopeOpFlattening
 
   // For returns with operands in cleanup dispatch blocks, the operands may not
   // dominate the dispatch block. This function handles that by either sinking
-  // the operand's defining op to the dispatch block (for constants and simple
-  // loads) or by storing to a temporary alloca and reloading it.
-  void
-  getReturnOpOperands(cir::ReturnOp returnOp, mlir::Operation *exitOp,
-                      mlir::Location loc, mlir::PatternRewriter &rewriter,
-                      llvm::SmallVectorImpl<mlir::Value> &returnValues) const {
+  // the operand's defining op to the dispatch block (for the operands in
+  // \p sinkableOperands) or by storing to a temporary alloca and reloading it.
+  void getReturnOpOperands(
+      cir::ReturnOp returnOp, mlir::Operation *exitOp, mlir::Location loc,
+      const llvm::SmallPtrSetImpl<mlir::Operation *> &sinkableOperands,
+      mlir::PatternRewriter &rewriter,
+      llvm::SmallVectorImpl<mlir::Value> &returnValues) const {
     mlir::Block *destBlock = rewriter.getInsertionBlock();
     auto funcOp = exitOp->getParentOfType<cir::FuncOp>();
     assert(funcOp && "Return op has no function parent?");
     mlir::Block &funcEntryBlock = funcOp.getBody().front();
 
     for (mlir::Value operand : returnOp.getOperands()) {
-      if (shouldSinkReturnOperand(operand, returnOp)) {
+      if (sinkableOperands.contains(operand.getDefiningOp())) {
         // Sink the defining op to the dispatch block.
         mlir::Operation *defOp = operand.getDefiningOp();
         rewriter.moveOpBefore(defOp, destBlock, destBlock->end());
@@ -1279,10 +1311,10 @@ class CIRCleanupScopeOpFlattening
   // block. For return ops with operands, this handles the dominance issue by
   // either moving the operand's defining op to the dispatch block (if it's a
   // trivial use) or by storing to a temporary alloca and loading it.
-  mlir::LogicalResult
-  createExitTerminator(mlir::Operation *exitOp, mlir::Location loc,
-                       mlir::Block *continueBlock,
-                       mlir::PatternRewriter &rewriter) const {
+  mlir::LogicalResult createExitTerminator(
+      mlir::Operation *exitOp, mlir::Location loc, mlir::Block *continueBlock,
+      const llvm::SmallPtrSetImpl<mlir::Operation *> &sinkableOperands,
+      mlir::PatternRewriter &rewriter) const {
     return llvm::TypeSwitch<mlir::Operation *, mlir::LogicalResult>(exitOp)
         .Case<cir::YieldOp>([&](auto) {
           // Yield becomes a branch to continue block.
@@ -1318,7 +1350,8 @@ class CIRCleanupScopeOpFlattening
           // branching through the outer cleanup.
           if (returnOp.hasOperand()) {
             llvm::SmallVector<mlir::Value, 2> returnValues;
-            getReturnOpOperands(returnOp, exitOp, loc, rewriter, returnValues);
+            getReturnOpOperands(returnOp, exitOp, loc, sinkableOperands,
+                                rewriter, returnValues);
             cir::ReturnOp::create(rewriter, loc, returnValues);
           } else {
             cir::ReturnOp::create(rewriter, loc);
@@ -1536,6 +1569,21 @@ class CIRCleanupScopeOpFlattening
       destSlot = getOrCreateCleanupDestSlot(funcOp, rewriter, loc);
     }
 
+    // Pick the return operands to sink past the cleanup while the cleanup
+    // region is still intact.
+    llvm::SmallPtrSet<mlir::Operation *, 4> sinkableOperands;
+    if (emitNormalCleanup) {
+      for (const CleanupExit &exit : exits) {
+        auto returnOp = dyn_cast<cir::ReturnOp>(exit.exitOp);
+        if (!returnOp)
+          continue;
+        for (mlir::Value operand : returnOp.getOperands())
+          if (shouldSinkReturnOperand(operand, returnOp,
+                                      cleanupOp.getCleanupRegion()))
+            sinkableOperands.insert(operand.getDefiningOp());
+      }
+    }
+
     // Split the current block to create the insertion point.
     mlir::Block *currentBlock = rewriter.getInsertionBlock();
     mlir::Block *continueBlock =
@@ -1615,8 +1663,8 @@ class CIRCleanupScopeOpFlattening
           // Create a block for this destination.
           mlir::Block *destBlock = rewriter.createBlock(normalInsertPt);
           rewriter.setInsertionPointToEnd(destBlock);
-          result =
-              createExitTerminator(exit.exitOp, loc, continueBlock, rewriter);
+          result = createExitTerminator(exit.exitOp, loc, continueBlock,
+                                        sinkableOperands, rewriter);
 
           // Add to switch cases.
           caseValues.push_back(
@@ -1661,7 +1709,8 @@ class CIRCleanupScopeOpFlattening
         // block.
         rewriter.setInsertionPointToEnd(exitBlock);
         mlir::Operation *exitOp = exits[0].exitOp;
-        result = createExitTerminator(exitOp, loc, continueBlock, rewriter);
+        result = createExitTerminator(exitOp, loc, continueBlock,
+                                      sinkableOperands, rewriter);
 
         // Replace body exit with branch to cleanup entry.
         rewriter.setInsertionPoint(exitOp);

diff  --git a/clang/test/CIR/Transforms/flatten-cleanup-scope-return-load.cir 
b/clang/test/CIR/Transforms/flatten-cleanup-scope-return-load.cir
new file mode 100644
index 0000000000000..9391e28470767
--- /dev/null
+++ b/clang/test/CIR/Transforms/flatten-cleanup-scope-return-load.cir
@@ -0,0 +1,395 @@
+// RUN: cir-opt %s -cir-flatten-cfg -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s
+
+// A load feeding a cir.return is sunk past the cleanup only if the cleanup
+// cannot change the loaded slot. Otherwise it is read before the cleanup.
+
+!s32i = !cir.int<s, 32>
+
+cir.func private @may_write()
+cir.func private @take_address(!cir.ptr<!s32i>)
+
+// CHECK-LABEL: cir.func @store_in_cleanup(
+// CHECK:         %[[TMP:.*]] = cir.alloca "__ret_operand_tmp"
+// CHECK:         %[[X:.*]] = cir.alloca "x"
+// CHECK:         %[[VAL:.*]] = cir.load %[[X]]
+// CHECK-NEXT:    cir.store %[[VAL]], %[[TMP]]
+// CHECK-NEXT:    cir.br ^[[CLEANUP:bb[0-9]+]]
+// CHECK:       ^[[CLEANUP]]:
+// CHECK-NEXT:    %[[SEVEN:.*]] = cir.const #cir.int<7>
+// CHECK-NEXT:    cir.store %[[SEVEN]], %[[X]]
+// CHECK-NEXT:    cir.br ^[[EXIT:bb[0-9]+]]
+// CHECK:       ^[[EXIT]]:
+// CHECK-NEXT:    %[[RET:.*]] = cir.load %[[TMP]]
+// CHECK-NEXT:    cir.return %[[RET]] : !s32i
+cir.func @store_in_cleanup() -> !s32i {
+  %0 = cir.alloca "x" align(4) : !cir.ptr<!s32i>
+  cir.cleanup.scope {
+    %1 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    cir.return %1 : !s32i
+  } cleanup normal {
+    %2 = cir.const #cir.int<7> : !s32i
+    cir.store %2, %0 : !s32i, !cir.ptr<!s32i>
+    cir.yield
+  }
+  cir.trap
+}
+
+// CHECK-LABEL: cir.func @lifetime_end_in_cleanup(
+// CHECK:         %[[TMP:.*]] = cir.alloca "__ret_operand_tmp"
+// CHECK:         %[[X:.*]] = cir.alloca "x"
+// CHECK:         %[[VAL:.*]] = cir.load %[[X]]
+// CHECK-NEXT:    cir.store %[[VAL]], %[[TMP]]
+// CHECK-NEXT:    cir.br ^[[CLEANUP:bb[0-9]+]]
+// CHECK:       ^[[CLEANUP]]:
+// CHECK-NEXT:    cir.lifetime.end %[[X]]
+// CHECK-NEXT:    cir.br ^[[EXIT:bb[0-9]+]]
+// CHECK:       ^[[EXIT]]:
+// CHECK-NEXT:    %[[RET:.*]] = cir.load %[[TMP]]
+// CHECK-NEXT:    cir.return %[[RET]] : !s32i
+cir.func @lifetime_end_in_cleanup() -> !s32i {
+  %0 = cir.alloca "x" align(4) : !cir.ptr<!s32i>
+  cir.cleanup.scope {
+    %1 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    cir.return %1 : !s32i
+  } cleanup normal {
+    cir.lifetime.end %0 : !cir.ptr<!s32i>
+    cir.yield
+  }
+  cir.trap
+}
+
+// The address of x is passed to a call, so the opaque call in the cleanup may
+// write x.
+
+// CHECK-LABEL: cir.func @escaped_by_call(
+// CHECK:         %[[TMP:.*]] = cir.alloca "__ret_operand_tmp"
+// CHECK:         %[[X:.*]] = cir.alloca "x"
+// CHECK:         %[[VAL:.*]] = cir.load %[[X]]
+// CHECK-NEXT:    cir.store %[[VAL]], %[[TMP]]
+// CHECK-NEXT:    cir.br ^[[CLEANUP:bb[0-9]+]]
+// CHECK:       ^[[CLEANUP]]:
+// CHECK-NEXT:    cir.call @may_write()
+// CHECK-NEXT:    cir.br ^[[EXIT:bb[0-9]+]]
+// CHECK:       ^[[EXIT]]:
+// CHECK-NEXT:    %[[RET:.*]] = cir.load %[[TMP]]
+// CHECK-NEXT:    cir.return %[[RET]] : !s32i
+cir.func @escaped_by_call() -> !s32i {
+  %0 = cir.alloca "x" align(4) : !cir.ptr<!s32i>
+  cir.call @take_address(%0) : (!cir.ptr<!s32i>) -> ()
+  cir.cleanup.scope {
+    %1 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    cir.return %1 : !s32i
+  } cleanup normal {
+    cir.call @may_write() : () -> ()
+    cir.yield
+  }
+  cir.trap
+}
+
+// The address of x is stored through %arg0, so the opaque call in the cleanup
+// may write x through it.
+
+// CHECK-LABEL: cir.func @escaped_by_store(
+// CHECK:         %[[TMP:.*]] = cir.alloca "__ret_operand_tmp"
+// CHECK:         %[[X:.*]] = cir.alloca "x"
+// CHECK:         %[[VAL:.*]] = cir.load %[[X]]
+// CHECK-NEXT:    cir.store %[[VAL]], %[[TMP]]
+// CHECK-NEXT:    cir.br ^[[CLEANUP:bb[0-9]+]]
+// CHECK:       ^[[CLEANUP]]:
+// CHECK-NEXT:    cir.call @may_write()
+// CHECK-NEXT:    cir.br ^[[EXIT:bb[0-9]+]]
+// CHECK:       ^[[EXIT]]:
+// CHECK-NEXT:    %[[RET:.*]] = cir.load %[[TMP]]
+// CHECK-NEXT:    cir.return %[[RET]] : !s32i
+cir.func @escaped_by_store(%arg0: !cir.ptr<!cir.ptr<!s32i>>) -> !s32i {
+  %0 = cir.alloca "x" align(4) : !cir.ptr<!s32i>
+  cir.store %0, %arg0 : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
+  cir.cleanup.scope {
+    %1 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    cir.return %1 : !s32i
+  } cleanup normal {
+    cir.call @may_write() : () -> ()
+    cir.yield
+  }
+  cir.trap
+}
+
+// A store to x between the load and the return, as in `return x++`, must
+// still come after the load.
+
+// CHECK-LABEL: cir.func @store_before_return(
+// CHECK:         %[[TMP:.*]] = cir.alloca "__ret_operand_tmp"
+// CHECK:         %[[X:.*]] = cir.alloca "x"
+// CHECK:         %[[VAL:.*]] = cir.load %[[X]]
+// CHECK-NEXT:    %[[NINE:.*]] = cir.const #cir.int<9>
+// CHECK-NEXT:    cir.store %[[NINE]], %[[X]]
+// CHECK-NEXT:    cir.store %[[VAL]], %[[TMP]]
+// CHECK-NEXT:    cir.br ^[[CLEANUP:bb[0-9]+]]
+// CHECK:       ^[[CLEANUP]]:
+// CHECK-NEXT:    cir.br ^[[EXIT:bb[0-9]+]]
+// CHECK:       ^[[EXIT]]:
+// CHECK-NEXT:    %[[RET:.*]] = cir.load %[[TMP]]
+// CHECK-NEXT:    cir.return %[[RET]] : !s32i
+cir.func @store_before_return() -> !s32i {
+  %0 = cir.alloca "x" align(4) : !cir.ptr<!s32i>
+  cir.cleanup.scope {
+    %1 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    %2 = cir.const #cir.int<9> : !s32i
+    cir.store %2, %0 : !s32i, !cir.ptr<!s32i>
+    cir.return %1 : !s32i
+  } cleanup normal {
+    cir.yield
+  }
+  cir.trap
+}
+
+// x does not escape and is only stored before the cleanup scope, so the
+// opaque call in the cleanup cannot change it and the load is sunk.
+
+// CHECK-LABEL: cir.func @untouched_slot(
+// CHECK-NOT:     "__ret_operand_tmp"
+// CHECK:         %[[X:.*]] = cir.alloca "x"
+// CHECK:         cir.store %{{.*}}, %[[X]]
+// CHECK-NOT:     cir.load %[[X]]
+// CHECK:         cir.call @may_write()
+// CHECK-NEXT:    cir.br ^[[EXIT:bb[0-9]+]]
+// CHECK:       ^[[EXIT]]:
+// CHECK-NEXT:    %[[RET:.*]] = cir.load %[[X]]
+// CHECK-NEXT:    cir.return %[[RET]] : !s32i
+cir.func @untouched_slot() -> !s32i {
+  %0 = cir.alloca "x" align(4) : !cir.ptr<!s32i>
+  %1 = cir.const #cir.int<5> : !s32i
+  cir.store %1, %0 : !s32i, !cir.ptr<!s32i>
+  cir.cleanup.scope {
+    %2 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    cir.return %2 : !s32i
+  } cleanup normal {
+    cir.call @may_write() : () -> ()
+    cir.yield
+  }
+  cir.trap
+}
+
+// Reading x in the cleanup does not change it, so the load is sunk.
+
+// CHECK-LABEL: cir.func @read_in_cleanup(
+// CHECK-NOT:     "__ret_operand_tmp"
+// CHECK:         %[[X:.*]] = cir.alloca "x"
+// CHECK:         cir.br ^[[BODY:bb[0-9]+]]
+// CHECK:       ^[[BODY]]:
+// CHECK-NEXT:    cir.br ^[[CLEANUP:bb[0-9]+]]
+// CHECK:       ^[[CLEANUP]]:
+// CHECK-NEXT:    cir.load %[[X]]
+// CHECK-NEXT:    cir.br ^[[EXIT:bb[0-9]+]]
+// CHECK:       ^[[EXIT]]:
+// CHECK-NEXT:    %[[RET:.*]] = cir.load %[[X]]
+// CHECK-NEXT:    cir.return %[[RET]] : !s32i
+cir.func @read_in_cleanup() -> !s32i {
+  %0 = cir.alloca "x" align(4) : !cir.ptr<!s32i>
+  cir.cleanup.scope {
+    %1 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    cir.return %1 : !s32i
+  } cleanup normal {
+    %2 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    cir.yield
+  }
+  cir.trap
+}
+
+// A read in a region nested in the cleanup does not change x either.
+
+// CHECK-LABEL: cir.func @read_in_nested_cleanup_region(
+// CHECK-NOT:     "__ret_operand_tmp"
+// CHECK:         %[[X:.*]] = cir.alloca "x"
+// CHECK:         cir.brcond %{{.*}} ^[[THEN:bb[0-9]+]], ^[[JOIN:bb[0-9]+]]
+// CHECK:       ^[[THEN]]:
+// CHECK-NEXT:    cir.load %[[X]]
+// CHECK-NEXT:    cir.br ^[[JOIN]]
+// CHECK:       ^[[JOIN]]:
+// CHECK-NEXT:    cir.br ^[[EXIT:bb[0-9]+]]
+// CHECK:       ^[[EXIT]]:
+// CHECK-NEXT:    %[[RET:.*]] = cir.load %[[X]]
+// CHECK-NEXT:    cir.return %[[RET]] : !s32i
+cir.func @read_in_nested_cleanup_region(%arg0: !cir.bool) -> !s32i {
+  %0 = cir.alloca "x" align(4) : !cir.ptr<!s32i>
+  cir.cleanup.scope {
+    %1 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    cir.return %1 : !s32i
+  } cleanup normal {
+    cir.if %arg0 {
+      %2 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    }
+    cir.yield
+  }
+  cir.trap
+}
+
+// The read in the cleanup is fine, but the store after it is not: x is read
+// before the cleanup.
+
+// CHECK-LABEL: cir.func @read_then_store_in_cleanup(
+// CHECK:         %[[TMP:.*]] = cir.alloca "__ret_operand_tmp"
+// CHECK:         %[[X:.*]] = cir.alloca "x"
+// CHECK:         %[[VAL:.*]] = cir.load %[[X]]
+// CHECK-NEXT:    cir.store %[[VAL]], %[[TMP]]
+// CHECK-NEXT:    cir.br ^[[CLEANUP:bb[0-9]+]]
+// CHECK:       ^[[CLEANUP]]:
+// CHECK-NEXT:    cir.load %[[X]]
+// CHECK-NEXT:    %[[SEVEN:.*]] = cir.const #cir.int<7>
+// CHECK-NEXT:    cir.store %[[SEVEN]], %[[X]]
+// CHECK-NEXT:    cir.br ^[[EXIT:bb[0-9]+]]
+// CHECK:       ^[[EXIT]]:
+// CHECK-NEXT:    %[[RET:.*]] = cir.load %[[TMP]]
+// CHECK-NEXT:    cir.return %[[RET]] : !s32i
+cir.func @read_then_store_in_cleanup() -> !s32i {
+  %0 = cir.alloca "x" align(4) : !cir.ptr<!s32i>
+  cir.cleanup.scope {
+    %1 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    cir.return %1 : !s32i
+  } cleanup normal {
+    %2 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    %3 = cir.const #cir.int<7> : !s32i
+    cir.store %3, %0 : !s32i, !cir.ptr<!s32i>
+    cir.yield
+  }
+  cir.trap
+}
+
+// Each return is routed through the dispatch block. Only the return of x
+// needs its load before the cleanup.
+
+// CHECK-LABEL: cir.func @multi_exit(
+// CHECK:         %[[TMP:.*]] = cir.alloca "__ret_operand_tmp"
+// CHECK:         %[[DEST:.*]] = cir.alloca "__cleanup_dest_slot"
+// CHECK:         %[[X:.*]] = cir.alloca "x"
+// CHECK:         %[[Y:.*]] = cir.alloca "y"
+// CHECK:         cir.brcond %{{.*}} ^[[RET_X:bb[0-9]+]], ^[[RET_Y:bb[0-9]+]]
+// CHECK:       ^[[RET_X]]:
+// CHECK-NEXT:    %[[VAL:.*]] = cir.load %[[X]]
+// CHECK-NEXT:    cir.store %[[VAL]], %[[TMP]]
+// CHECK:         cir.store %{{.*}}, %[[DEST]]
+// CHECK-NEXT:    cir.br ^[[CLEANUP:bb[0-9]+]]
+// CHECK:       ^[[RET_Y]]:
+// CHECK-NOT:     cir.load %[[Y]]
+// CHECK:         cir.store %{{.*}}, %[[DEST]]
+// CHECK-NEXT:    cir.br ^[[CLEANUP]]
+// CHECK:       ^[[CLEANUP]]:
+// CHECK-NEXT:    %[[SEVEN:.*]] = cir.const #cir.int<7>
+// CHECK-NEXT:    cir.store %[[SEVEN]], %[[X]]
+// CHECK-NEXT:    cir.br ^[[DISPATCH:bb[0-9]+]]
+// CHECK:       ^[[DISPATCH]]:
+// CHECK:         cir.switch.flat
+// CHECK-NEXT:      0: ^[[DEST_X:bb[0-9]+]],
+// CHECK-NEXT:      1: ^[[DEST_Y:bb[0-9]+]]
+// CHECK:       ^[[DEST_X]]:
+// CHECK-NEXT:    %[[RET_X_VAL:.*]] = cir.load %[[TMP]]
+// CHECK-NEXT:    cir.return %[[RET_X_VAL]] : !s32i
+// CHECK:       ^[[DEST_Y]]:
+// CHECK-NEXT:    %[[RET_Y_VAL:.*]] = cir.load %[[Y]]
+// CHECK-NEXT:    cir.return %[[RET_Y_VAL]] : !s32i
+cir.func @multi_exit(%arg0: !cir.bool) -> !s32i {
+  %0 = cir.alloca "x" align(4) : !cir.ptr<!s32i>
+  %1 = cir.alloca "y" align(4) : !cir.ptr<!s32i>
+  cir.cleanup.scope {
+    cir.brcond %arg0 ^bb_x, ^bb_y
+  ^bb_x:
+    %2 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    cir.return %2 : !s32i
+  ^bb_y:
+    %3 = cir.load %1 : !cir.ptr<!s32i>, !s32i
+    cir.return %3 : !s32i
+  } cleanup normal {
+    %4 = cir.const #cir.int<7> : !s32i
+    cir.store %4, %0 : !s32i, !cir.ptr<!s32i>
+    cir.yield
+  }
+  cir.trap
+}
+
+// The inner cleanup leaves x alone, so the load is sunk past it, but it must
+// stay ahead of the outer cleanup that ends the lifetime of x.
+
+// CHECK-LABEL: cir.func @nested_outer_clobbers(
+// CHECK:         %[[TMP:.*]] = cir.alloca "__ret_operand_tmp"
+// CHECK:         %[[DEST:.*]] = cir.alloca "__cleanup_dest_slot"
+// CHECK:         %[[X:.*]] = cir.alloca "x"
+// CHECK-NOT:     cir.load %[[X]]
+// CHECK:         cir.call @may_write()
+// CHECK-NEXT:    cir.br ^[[INNER_EXIT:bb[0-9]+]]
+// CHECK:       ^[[INNER_EXIT]]:
+// CHECK-NEXT:    %[[VAL:.*]] = cir.load %[[X]]
+// CHECK-NEXT:    cir.store %[[VAL]], %[[TMP]]
+// CHECK-NEXT:    %[[RET_ID:.*]] = cir.const #cir.int<1>
+// CHECK-NEXT:    cir.store %[[RET_ID]], %[[DEST]]
+// CHECK-NEXT:    cir.br ^[[OUTER_CLEANUP:bb[0-9]+]]
+// CHECK:       ^[[OUTER_CLEANUP]]:
+// CHECK-NEXT:    cir.lifetime.end %[[X]]
+// CHECK:         cir.switch.flat
+// CHECK-NEXT:      0: ^{{bb[0-9]+}},
+// CHECK-NEXT:      1: ^[[OUTER_RET:bb[0-9]+]]
+// CHECK:       ^[[OUTER_RET]]:
+// CHECK-NEXT:    %[[RET:.*]] = cir.load %[[TMP]]
+// CHECK-NEXT:    cir.return %[[RET]] : !s32i
+cir.func @nested_outer_clobbers() -> !s32i {
+  %0 = cir.alloca "x" align(4) : !cir.ptr<!s32i>
+  cir.cleanup.scope {
+    cir.cleanup.scope {
+      %1 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+      cir.return %1 : !s32i
+    } cleanup normal {
+      cir.call @may_write() : () -> ()
+      cir.yield
+    }
+    cir.yield
+  } cleanup normal {
+    cir.lifetime.end %0 : !cir.ptr<!s32i>
+    cir.yield
+  }
+  cir.trap
+}
+
+// The inner cleanup writes x, so the load happens before it. The spill slot
+// is not touched by the outer cleanup, so its reload is sunk past both.
+
+// CHECK-LABEL: cir.func @nested_inner_clobbers(
+// CHECK-DAG:     %[[DEST:.*]] = cir.alloca "__cleanup_dest_slot"
+// CHECK-DAG:     %[[TMP:.*]] = cir.alloca "__ret_operand_tmp"
+// CHECK:         %[[X:.*]] = cir.alloca "x"
+// CHECK:         %[[VAL:.*]] = cir.load %[[X]]
+// CHECK-NEXT:    cir.store %[[VAL]], %[[TMP]]
+// CHECK-NEXT:    cir.br ^[[INNER_CLEANUP:bb[0-9]+]]
+// CHECK:       ^[[INNER_CLEANUP]]:
+// CHECK-NEXT:    %[[SEVEN:.*]] = cir.const #cir.int<7>
+// CHECK-NEXT:    cir.store %[[SEVEN]], %[[X]]
+// CHECK-NEXT:    cir.br ^[[INNER_EXIT:bb[0-9]+]]
+// CHECK:       ^[[INNER_EXIT]]:
+// CHECK-NEXT:    %[[RET_ID:.*]] = cir.const #cir.int<1>
+// CHECK-NEXT:    cir.store %[[RET_ID]], %[[DEST]]
+// CHECK-NEXT:    cir.br ^[[OUTER_CLEANUP:bb[0-9]+]]
+// CHECK:       ^[[OUTER_CLEANUP]]:
+// CHECK-NEXT:    cir.call @may_write()
+// CHECK:         cir.switch.flat
+// CHECK-NEXT:      0: ^{{bb[0-9]+}},
+// CHECK-NEXT:      1: ^[[OUTER_RET:bb[0-9]+]]
+// CHECK:       ^[[OUTER_RET]]:
+// CHECK-NEXT:    %[[RET:.*]] = cir.load %[[TMP]]
+// CHECK-NEXT:    cir.return %[[RET]] : !s32i
+cir.func @nested_inner_clobbers() -> !s32i {
+  %0 = cir.alloca "x" align(4) : !cir.ptr<!s32i>
+  cir.cleanup.scope {
+    cir.cleanup.scope {
+      %1 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+      cir.return %1 : !s32i
+    } cleanup normal {
+      %2 = cir.const #cir.int<7> : !s32i
+      cir.store %2, %0 : !s32i, !cir.ptr<!s32i>
+      cir.yield
+    }
+    cir.yield
+  } cleanup normal {
+    cir.call @may_write() : () -> ()
+    cir.yield
+  }
+  cir.trap
+}


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

Reply via email to