[llvm] [ObjCARC] Delete empty autoreleasepools with no autoreleases in them (PR #144788)
Jon Roelofs via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 18 16:53:27 PDT 2025
================
@@ -2485,6 +2490,203 @@ bool ObjCARCOpt::run(Function &F, AAResults &AA) {
return Changed;
}
+/// Helper function to recursively check if a value eventually leads to the
+/// target instruction through pointer casts and uses, up to a specified depth.
+static bool checkLeadsToTarget(Value *Val, User *Target, unsigned MaxDepth,
+ SmallPtrSet<Value *, 8> &Visited) {
+ if (MaxDepth == 0)
+ return false;
+
+ // Avoid infinite recursion by tracking visited values
+ if (!Visited.insert(Val).second)
+ return false;
+
+ for (User *U : Val->users()) {
+ if (U == Target) {
+ return true;
+ }
+
+ // For pointer casts, recursively check their users
+ if (isa<CastInst>(U) || isa<BitCastInst>(U)) {
+ Value *CastResult = dyn_cast<Value>(U);
+ if (CastResult &&
+ checkLeadsToTarget(CastResult, Target, MaxDepth - 1, Visited)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+/// Optimize autorelease pools by eliminating empty push/pop pairs.
+void ObjCARCOpt::OptimizeAutoreleasePools(Function &F) {
+ LLVM_DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeAutoreleasePools ==\n");
+
+ OptimizationRemarkEmitter ORE(&F);
+
+ // Track empty autorelease pool push/pop pairs
+ SmallVector<std::pair<CallInst *, CallInst *>, 4> EmptyPoolPairs;
+
+ // Process each basic block independently.
+ // TODO: Can we optimize inter-block autorelease pool pairs?
+ // This would involve tracking autorelease pool state across blocks.
+ for (BasicBlock &BB : F) {
+ // Use a stack to track nested autorelease pools
+ SmallVector<std::pair<CallInst *, bool>, 4>
+ PoolStack; // {push_inst, has_autorelease_in_scope}
+
+ for (Instruction &Inst : BB) {
+ ARCInstKind Class = GetBasicARCInstKind(&Inst);
+
+ switch (Class) {
+ case ARCInstKind::AutoreleasepoolPush: {
+ // Start tracking a new autorelease pool scope
+ CallInst *Push = cast<CallInst>(&Inst);
----------------
jroelofs wrote:
`auto *Push`, since the type is mentioned in the `cast<>`
https://github.com/llvm/llvm-project/pull/144788
More information about the llvm-commits
mailing list