[PATCH] D104953: [ObjC][ARC] Prevent moving objc_retain calls past objc_release calls that release the retained object

Akira Hatanaka via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 25 14:26:33 PDT 2021


ahatanak created this revision.
ahatanak added reviewers: rjmccall, fhahn, gottesmm.
ahatanak added a project: LLVM.
Herald added a subscriber: hiraditya.
ahatanak requested review of this revision.

This patch fixes what looks like a longstanding bug in ARC optimizer where it reverses the order of objc_retain calls and objc_release calls that retain and release the same object.

The code in ARC optimizer that is responsible for code motion takes the following steps:

1. Traverse the CFG bottom-up and determine how far up `objc_release` calls can be moved. Determine the insertion points for the `objc_release` calls, but don't actually move them.
2. Traverse the CFG top-down and determine how far down `objc_retain` calls can be moved. Determine the insertion points for the `objc_retain` calls, but don't actually move them.
3. Try to move the `objc_retain` and `objc_release` calls if they can't be removed.

The problem is that the insertion points for the `objc_retain` calls are determined in step 2 without taking into consideration the insertion points for `objc_release` calls determined in step 1, so the order of an `objc_retain` call and an `objc_release` call can be reversed, which is incorrect, even though each step is correct in isolation.

To fix this bug, this patch teaches the top-down traversal step to take into consideration the insertion points for `objc_release` calls determined in the bottom-up traversal step. Code motion for an `objc_retain` call is disabled if there is a possibility that it can be moved past an `objc_release` call that releases the retained object.

rdar://79292791


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104953

Files:
  llvm/lib/Transforms/ObjCARC/ObjCARCOpts.cpp
  llvm/test/Transforms/ObjCARC/code-motion.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104953.354600.patch
Type: text/x-patch
Size: 7224 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210625/221b40f4/attachment.bin>


More information about the llvm-commits mailing list