From 5f25bc30d17719806a1df3e1805f528a65289cca Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 17 Feb 2013 04:03:34 +0000 Subject: [PATCH] [CodeGen] tighten objc ivar invariant.load attribution An ivar ofset cannot be marked as invariant load in all cases. The ivar offset is a lazily initialised constant, which is dependent on an objc_msgSend invocation to perform a fixup of the offset. If the load is being performed on a method implemented by the class then this load can safely be marked as an inviarant because a message must have been passed to the class at some point, forcing the ivar offset to be resolved. An additional heuristic that can be used to identify an invariant load would be if the ivar offset base is a parameter to an objc method. However, without the parameters available at hand, this is currently not possible. Reviewed-by: John McCall Signed-off-by: Saleem Abdulrasool llvm-svn: 175386 --- clang/lib/CodeGen/CGObjCMac.cpp | 29 ++++++++++++-- clang/test/CodeGenObjC/ivar-invariant.m | 53 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 clang/test/CodeGenObjC/ivar-invariant.m diff --git a/clang/lib/CodeGen/CGObjCMac.cpp b/clang/lib/CodeGen/CGObjCMac.cpp index b61852765a67..bf02439ec6c2 100644 --- a/clang/lib/CodeGen/CGObjCMac.cpp +++ b/clang/lib/CodeGen/CGObjCMac.cpp @@ -1432,6 +1432,25 @@ private: /// class implementation is "non-lazy". bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; + bool IsIvarOffsetKnownIdempotent(const CodeGen::CodeGenFunction &CGF, + const ObjCInterfaceDecl *ID, + const ObjCIvarDecl *IV) { + // Annotate the load as an invariant load iff the object type is the type, + // or a derived type, of the class containing the ivar within an ObjC + // method. This check is needed because the ivar offset is a lazily + // initialised value that may depend on objc_msgSend to perform a fixup on + // the first message dispatch. + // + // An additional opportunity to mark the load as invariant arises when the + // base of the ivar access is a parameter to an Objective C method. + // However, because the parameters are not available in the current + // interface, we cannot perform this check. + if (dyn_cast(CGF.CurFuncDecl)) + if (IV->getContainingInterface()->isSuperClassOf(ID)) + return true; + return false; + } + public: CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); // FIXME. All stubs for now! @@ -6449,10 +6468,12 @@ LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( unsigned CVRQualifiers) { ObjCInterfaceDecl *ID = ObjectTy->getAs()->getInterface(); llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar); - if (llvm::LoadInst *LI = dyn_cast(Offset)) - LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), - llvm::MDNode::get(VMContext, - ArrayRef())); + + if (IsIvarOffsetKnownIdempotent(CGF, ID, Ivar)) + if (llvm::LoadInst *LI = cast(Offset)) + LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), + llvm::MDNode::get(VMContext, ArrayRef())); + return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, Offset); } diff --git a/clang/test/CodeGenObjC/ivar-invariant.m b/clang/test/CodeGenObjC/ivar-invariant.m new file mode 100644 index 000000000000..f59265445bb3 --- /dev/null +++ b/clang/test/CodeGenObjC/ivar-invariant.m @@ -0,0 +1,53 @@ +// RUN: %clang_cc1 -triple x86_64-apple-darwin -x objective-c %s -o - | FileCheck %s + +@interface NSObject ++ (id) new; +- (id) init; +@end + +@interface Base : NSObject @end + +// @implementation Base +// { +// int dummy; +// } +// @end + +@interface Derived : Base +{ + @public int member; +} +@end + +@implementation Derived +- (id) init +{ + self = [super init]; + member = 42; + return self; +} +@end + +// CHECK: [[IVAR:%.*]] = load i64* @"OBJC_IVAR_$_Derived.member", !invariant.load + +void * variant_load_1(int i) { + void *ptr; + while (i--) { + Derived *d = [Derived new]; + ptr = &d->member; + } + return ptr; +} + +// CHECK: [[IVAR:%.*]] = load i64* @"OBJC_IVAR_$_Derived.member"{{$}} + +@interface Container : Derived @end +@implementation Container +- (void *) invariant_load_1 +{ + return &self->member; +} +@end + +// CHECK: [[IVAR:%.*]] = load i64* @"OBJC_IVAR_$_Derived.member", !invariant.load +