Skip to content

Relax the rules around objc_alloc and objc_alloc_init optimizations. #580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions clang/lib/CodeGen/CGObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,38 +460,39 @@ tryEmitSpecializedAllocInit(CodeGenFunction &CGF, const ObjCMessageExpr *OME) {
Sel.getNameForSlot(0) != "init")
return None;

// Okay, this is '[receiver init]', check if 'receiver' is '[cls alloc]' or
// we are in an ObjC class method and 'receiver' is '[self alloc]'.
// Okay, this is '[receiver init]', check if 'receiver' is '[cls alloc]'
// with 'cls' a Class.
auto *SubOME =
dyn_cast<ObjCMessageExpr>(OME->getInstanceReceiver()->IgnoreParenCasts());
if (!SubOME)
return None;
Selector SubSel = SubOME->getSelector();

// Check if we are in an ObjC class method and the receiver expression is
// 'self'.
const Expr *SelfInClassMethod = nullptr;
if (const auto *CurMD = dyn_cast_or_null<ObjCMethodDecl>(CGF.CurFuncDecl))
if (CurMD->isClassMethod())
if ((SelfInClassMethod = SubOME->getInstanceReceiver()))
if (!SelfInClassMethod->isObjCSelfExpr())
SelfInClassMethod = nullptr;

if ((SubOME->getReceiverKind() != ObjCMessageExpr::Class &&
!SelfInClassMethod) || !SubOME->getType()->isObjCObjectPointerType() ||
if (!SubOME->getType()->isObjCObjectPointerType() ||
!SubSel.isUnarySelector() || SubSel.getNameForSlot(0) != "alloc")
return None;

llvm::Value *Receiver;
if (SelfInClassMethod) {
Receiver = CGF.EmitScalarExpr(SelfInClassMethod);
} else {
llvm::Value *Receiver = nullptr;
switch (SubOME->getReceiverKind()) {
case ObjCMessageExpr::Instance:
if (!SubOME->getInstanceReceiver()->getType()->isObjCClassType())
return None;
Receiver = CGF.EmitScalarExpr(SubOME->getInstanceReceiver());
break;

case ObjCMessageExpr::Class: {
QualType ReceiverType = SubOME->getClassReceiver();
const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>();
const ObjCInterfaceDecl *ID = ObjTy->getInterface();
assert(ID && "null interface should be impossible here");
Receiver = CGF.CGM.getObjCRuntime().GetClass(CGF, ID);
break;
}
case ObjCMessageExpr::SuperInstance:
case ObjCMessageExpr::SuperClass:
return None;
}

return CGF.EmitObjCAllocInit(Receiver, CGF.ConvertType(OME->getType()));
}

Expand Down Expand Up @@ -539,10 +540,7 @@ RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
switch (E->getReceiverKind()) {
case ObjCMessageExpr::Instance:
ReceiverType = E->getInstanceReceiver()->getType();
if (auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(CurFuncDecl))
if (OMD->isClassMethod())
if (E->getInstanceReceiver()->isObjCSelfExpr())
isClassMessage = true;
isClassMessage = ReceiverType->isObjCClassType();
if (retainSelf) {
TryEmitResult ter = tryEmitARCRetainScalarExpr(*this,
E->getInstanceReceiver());
Expand Down
11 changes: 10 additions & 1 deletion clang/test/CodeGenObjC/objc-alloc-init.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,30 @@ void f() {
}

@interface Y : X
+(Class)class;
+(void)meth;
-(void)instanceMeth;
@end

@implementation Y
+(Class)class {
return self;
}
+(void)meth {
[[self alloc] init];
// OPTIMIZED: call i8* @objc_alloc_init(
// NOT_OPTIMIZED: call i8* @objc_alloc(
}
+ (void)meth2 {
[[[self class] alloc] init];
// OPTIMIZED: call i8* @objc_alloc_init(
// NOT_OPTIMIZED: call i8* @objc_alloc(
}
-(void)instanceMeth {
// EITHER-NOT: call i8* @objc_alloc
// EITHER: call {{.*}} @objc_msgSend
// EITHER: call {{.*}} @objc_msgSend
[[self alloc] init];
[[(id)self alloc] init];
}
@end

Expand Down