[PATCH] [CodeGen] tighten objc ivar invariant.load attribution

Saleem Abdulrasool compnerd at compnerd.org
Wed Feb 13 22:38:29 PST 2013


Hi rjmccall,

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.

Signed-off-by: Saleem Abdulrasool <compnerd at compnerd.org>

http://llvm-reviews.chandlerc.com/D406

Files:
  lib/CodeGen/CGObjCMac.cpp

Index: lib/CodeGen/CGObjCMac.cpp
===================================================================
--- lib/CodeGen/CGObjCMac.cpp
+++ lib/CodeGen/CGObjCMac.cpp
@@ -6449,10 +6449,23 @@
                                                unsigned CVRQualifiers) {
   ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
   llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
+
+  // 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 (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
-    LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), 
-                   llvm::MDNode::get(VMContext,
-                   ArrayRef<llvm::Value*>()));
+    if (dyn_cast<ObjCMethodDecl>(CGF.CurFuncDecl))
+      if (ID->isSuperClassOf(Ivar->getContainingInterface()))
+        LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
+                        llvm::MDNode::get(VMContext, ArrayRef<llvm::Value*>()));
+
   return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
                                   Offset);
 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D406.1.patch
Type: text/x-patch
Size: 1625 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130213/b2ccef30/attachment.bin>


More information about the cfe-commits mailing list