r179599 - Objective-C IRGen. Use llvm::WeakVH

Fariborz Jahanian fjahanian at apple.com
Tue Apr 16 08:25:39 PDT 2013


Author: fjahanian
Date: Tue Apr 16 10:25:39 2013
New Revision: 179599

URL: http://llvm.org/viewvc/llvm-project?rev=179599&view=rev
Log:
Objective-C IRGen. Use llvm::WeakVH
for caching couple of global symbols used
for generation of CF/NS string meta-data
so they are not released prematuely in certain
corner cases. // rdar:// 13598026.
Reviewed by John M.

Added:
    cfe/trunk/test/CodeGenObjC/tentative-cfconstantstring.m
Modified:
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=179599&r1=179598&r2=179599&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Tue Apr 16 10:25:39 2013
@@ -2279,7 +2279,8 @@ CodeGenModule::GetAddrOfConstantCFString
 
   llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
   llvm::Constant *Zeros[] = { Zero, Zero };
-
+  llvm::Value *V;
+  
   // If we don't already have it, get __CFConstantStringClassReference.
   if (!CFConstantStringClassRef) {
     llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
@@ -2287,9 +2288,11 @@ CodeGenModule::GetAddrOfConstantCFString
     llvm::Constant *GV = CreateRuntimeVariable(Ty,
                                            "__CFConstantStringClassReference");
     // Decay array -> ptr
-    CFConstantStringClassRef =
-      llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
+    V = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
+    CFConstantStringClassRef = V;
   }
+  else
+    V = CFConstantStringClassRef;
 
   QualType CFTy = getContext().getCFConstantStringType();
 
@@ -2299,7 +2302,7 @@ CodeGenModule::GetAddrOfConstantCFString
   llvm::Constant *Fields[4];
 
   // Class pointer.
-  Fields[0] = CFConstantStringClassRef;
+  Fields[0] = cast<llvm::ConstantExpr>(V);
 
   // Flags.
   llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
@@ -2386,7 +2389,7 @@ CodeGenModule::GetAddrOfConstantString(c
   
   llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
   llvm::Constant *Zeros[] = { Zero, Zero };
-  
+  llvm::Value *V;
   // If we don't already have it, get _NSConstantStringClassReference.
   if (!ConstantStringClassRef) {
     std::string StringClass(getLangOpts().ObjCConstantStringClass);
@@ -2399,8 +2402,8 @@ CodeGenModule::GetAddrOfConstantString(c
       GV = getObjCRuntime().GetClassGlobal(str);
       // Make sure the result is of the correct type.
       llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
-      ConstantStringClassRef =
-        llvm::ConstantExpr::getBitCast(GV, PTy);
+      V = llvm::ConstantExpr::getBitCast(GV, PTy);
+      ConstantStringClassRef = V;
     } else {
       std::string str =
         StringClass.empty() ? "_NSConstantStringClassReference"
@@ -2408,10 +2411,12 @@ CodeGenModule::GetAddrOfConstantString(c
       llvm::Type *PTy = llvm::ArrayType::get(Ty, 0);
       GV = CreateRuntimeVariable(PTy, str);
       // Decay array -> ptr
-      ConstantStringClassRef = 
-        llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
+      V = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
+      ConstantStringClassRef = V;
     }
   }
+  else
+    V = ConstantStringClassRef;
 
   if (!NSConstantStringType) {
     // Construct the type for a constant NSString.
@@ -2450,7 +2455,7 @@ CodeGenModule::GetAddrOfConstantString(c
   llvm::Constant *Fields[3];
   
   // Class pointer.
-  Fields[0] = ConstantStringClassRef;
+  Fields[0] = cast<llvm::ConstantExpr>(V);
   
   // String pointer.
   llvm::Constant *C =

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=179599&r1=179598&r2=179599&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Tue Apr 16 10:25:39 2013
@@ -346,11 +346,11 @@ class CodeGenModule : public CodeGenType
 
   /// CFConstantStringClassRef - Cached reference to the class for constant
   /// strings. This value has type int * but is actually an Obj-C class pointer.
-  llvm::Constant *CFConstantStringClassRef;
+  llvm::WeakVH CFConstantStringClassRef;
 
   /// ConstantStringClassRef - Cached reference to the class for constant
   /// strings. This value has type int * but is actually an Obj-C class pointer.
-  llvm::Constant *ConstantStringClassRef;
+  llvm::WeakVH ConstantStringClassRef;
 
   /// \brief The LLVM type corresponding to NSConstantString.
   llvm::StructType *NSConstantStringType;

Added: cfe/trunk/test/CodeGenObjC/tentative-cfconstantstring.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/tentative-cfconstantstring.m?rev=179599&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenObjC/tentative-cfconstantstring.m (added)
+++ cfe/trunk/test/CodeGenObjC/tentative-cfconstantstring.m Tue Apr 16 10:25:39 2013
@@ -0,0 +1,44 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s
+// rdar://13598026
+
+ at interface NSObject @end
+
+ at class NSString;
+
+int __CFConstantStringClassReference[24];
+
+ at interface Bar : NSObject
++(void)format:(NSString *)format,...;
+ at end
+
+ at interface Foo : NSObject
+ at end
+
+
+static inline void _inlineFunction() {
+    [Bar format:@" "];
+}
+
+ at implementation Foo
+
+
++(NSString *)someMethod {
+   return @"";
+}
+
+-(void)someMethod {
+   _inlineFunction();
+}
+ at end
+
+// CHECK: @__CFConstantStringClassReference = common global [24 x i32] zeroinitializer, align 16
+// CHECK: @_unnamed_cfstring_{{.*}} = private constant %struct.NSConstantString { i32* getelementptr inbounds ([24 x i32]* @__CFConstantStringClassReference, i32 0, i32 0)
+
+// CHECK: define internal void @_inlineFunction()
+// CHECK-NEXT: entry:
+// CHECK-NEXT:  [[ZERO:%.*]] = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_
+// CHECK-NEXT:   [[ONE:%.*]] = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_"
+// CHECK-NEXT:   [[TWO:%.*]] = bitcast %struct._class_t* [[ZERO]] to i8*
+// CHECK-NEXT:   call{{.*}}@objc_msgSend{{.*}}(i8* [[TWO]], i8* [[ONE]], [[ZERO]]* bitcast (%struct.NSConstantString* @_unnamed_cfstring_{{.*}}
+// CHECK-NEXT:   ret void
+





More information about the cfe-commits mailing list