[cfe-users] Objective-C message-to-nil optimization?

Kyle Sluder kyle at ksluder.com
Wed Feb 12 09:54:59 PST 2014

On Feb 12, 2014, at 9:23 AM, Jordan Rose <jordan_rose at apple.com> wrote:
> Hi, Kyle. It looks like Clang does not perform this optimization, even under -O3 or -Oz. I can guess why: even though sending a message to nil will result in 0, it's still more work to call objc_msgSend, have it do the comparison to nil, and then have it clean things up properly to return 0. (In fact, you can poke at the source for objc_msgSend and see exactly how much work it is: not much, but still more than doing the check yourself.)

Understood. Though I’m curious: is there a hard-and-fast rule against optimizations introducing function calls and/or branches where they would otherwise not appear?

Nonetheless, this optimization also introduces the possibility of involving the nil message handler (which AFAIK is undocumented), so there is a semantic difference as well as a performance difference.

> I could see this being a valid optimization for -Os or -Oz mode, though, where the stated goal of optimization is to make the program fast and minimize code size. If you agree, you can file a bug at http://llvm.org/bugs/, or against Apple at http://bugreport.apple.com.

Will do. I might even gin up a patch myself; I'm on a bit of an exploratory kick.

--Kyle Sluder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20140212/18918da9/attachment.html>

More information about the cfe-users mailing list