<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>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 <i>it</i> do the comparison to nil, and then have it clean things up properly to return 0. (In fact, you can poke at the <a href="http://www.opensource.apple.com/source/objc4/objc4-532.2/runtime/Messengers.subproj/objc-msg-x86_64.s">source</a> for objc_msgSend and see exactly how much work it is: not much, but still more than doing the check yourself.)</div><div><br></div><div>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 <i>and</i> minimize code size. If you agree, you can file a bug at <a href="http://llvm.org/bugs/">http://llvm.org/bugs/</a>, or against Apple at <a href="http://bugreport.apple.com">http://bugreport.apple.com</a>.</div><div><br></div><div>Jordan</div><div><br></div><br><div><div>On Feb 11, 2014, at 19:24 , Kyle Sluder <<a href="mailto:kyle@ksluder.com">kyle@ksluder.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Just curious if clang optimizes ObjC messages that it knows have a nil<br>receiver?<br><br>I'm converting a subclass of NSTextStorage to lazily create its backing<br>store. To make this very clear to readers, I want to wrap all accesses<br>to the backing store in conditional checks.<br><br>For example:<br><br>- (NSUInteger)length;<br>{<br> return _contents ? _contents.length : 0;<br>}<br><br>Strictly speaking, this is unnecessary, because sending -length to nil<br>will result in 0 anyway.<br><br>Part of the reason I'm making this change is for optimization, so I'd<br>feel a lot better if I knew that clang would optimize away conditional<br>expressions when it determines that they are 1) solely determined by the<br>NULL-ness of an object and 2) the result of evaluating either branch is<br>known to be exactly equal to sending the message to the receiver.<br><br>Is this something that clang does already?<br><br>--Kyle Sluder<br>_______________________________________________<br>cfe-users mailing list<br><a href="mailto:cfe-users@cs.uiuc.edu">cfe-users@cs.uiuc.edu</a><br>http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users<br></blockquote></div><br></body></html>