This is an archive of the discontinued LLVM Phabricator instance.

[CodeGenObjC] When available, emit a direct call to objc_alloc_init(cls) instead of [objc_alloc(cls) init]
ClosedPublic

Authored by erik.pilkington on Feb 7 2019, 6:38 PM.

Diff Detail

Event Timeline

erik.pilkington created this revision.Feb 7 2019, 6:38 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 7 2019, 6:38 PM
rjmccall added inline comments.Feb 7 2019, 7:11 PM
clang/lib/CodeGen/CGObjC.cpp
459

You might need to check for a super message send, which can be a class message send in a class method.

Also, I think getInterface() can return null here, although it might need to be contrived to fit the other requirements. Class<SomeProtocol>, where that protocol declares +alloc? If there isn't a way to just emit the receiver pointer of a normal message send from an ObjCMessageExpr, that's really too bad.

gparker42 added inline comments.
clang/lib/CodeGen/CGObjC.cpp
459

Should this be done inside tryGenerateSpecializedMessageSend, or from the same place that calls tryGenerateSpecializedMessageSend? That position already rejects [super …] call sites, and might make it more obvious how the objc_alloc and objc_alloc_init optimizations interact. (Currently it takes some digging to verify that the objc_alloc_init generator does not need to look for the pattern [objc_alloc(cls) init]).

gparker42 added inline comments.Feb 8 2019, 3:18 AM
clang/lib/CodeGen/CGObjC.cpp
459

(Never mind, that super check wouldn't help catch [[super alloc] init]because it's checking init's receiver.)

erik.pilkington marked an inline comment as done.

Address review comments.

erik.pilkington marked 2 inline comments as done.Feb 13 2019, 6:24 PM
erik.pilkington added inline comments.
clang/lib/CodeGen/CGObjC.cpp
459

You might need to check for a super message send, which can be a class message send in a class method.

Ah, good catch. New patch only does this for class message sends.

475–483

Also, I think getInterface() can return null here, although it might need to be contrived to fit the other requirements. Class<SomeProtocol>, where that protocol declares +alloc? If there isn't a way to just emit the receiver pointer of a normal message send from an ObjCMessageExpr, that's really too bad.

Are you sure? It looks like we unconditionally dereference getInterface() here, and we would take this path if not for the new check above.

rjmccall accepted this revision.Feb 13 2019, 7:19 PM
rjmccall added inline comments.
clang/lib/CodeGen/CGObjC.cpp
475–483

It looks like ObjCMessageExpr::Class is only for messages to type names, and Sema rejects types in that case that aren't just an ObjC class, so you're right, I misunderstood.

This revision is now accepted and ready to land.Feb 13 2019, 7:19 PM
This revision was automatically updated to reflect the committed changes.