Patch fixes an obscure bug when 'used' attribute is applied to
variables in ObjC's Next runtime mode. Next runtime also implicitly applies
'used' attribute on some of its meta-data. This results in two 
'llvm.used' arrays to be generated, and one of them is renamed to
'llvm.used1'.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@74008 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGObjCMac.cpp b/lib/CodeGen/CGObjCMac.cpp
index 6ffca81..325b2cc 100644
--- a/lib/CodeGen/CGObjCMac.cpp
+++ b/lib/CodeGen/CGObjCMac.cpp
@@ -911,6 +911,8 @@
                                         const CallArgList &CallArgs,
                                         const ObjCCommonTypesHelper &ObjCTypes);
 
+  virtual void MergeMetadataGlobals(std::vector<llvm::Constant*> &UsedArray);
+
 public:
   CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
   { }
@@ -3426,6 +3428,16 @@
   NameOut += ']';
 }
 
+void CGObjCCommonMac::MergeMetadataGlobals(
+                                  std::vector<llvm::Constant*> &UsedArray) {
+  llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
+  for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
+       e = UsedGlobals.end(); i != e; ++i) {
+    UsedArray.push_back(llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(*i), 
+                                                       i8PTy));
+  }
+}
+
 void CGObjCMac::FinishModule() {
   EmitModuleInfo();
 
@@ -3447,22 +3459,6 @@
                                                         Values));
   }
 
-  std::vector<llvm::Constant*> Used;
-  for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), 
-         e = UsedGlobals.end(); i != e; ++i) {
-    Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
-  }
-  
-  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
-  llvm::GlobalValue *GV = 
-    new llvm::GlobalVariable(AT, false,
-                             llvm::GlobalValue::AppendingLinkage,
-                             llvm::ConstantArray::get(AT, Used),
-                             "llvm.used", 
-                             &CGM.getModule());
-
-  GV->setSection("llvm.metadata");
-
   // Add assembler directives to add lazy undefined symbol references
   // for classes which are referenced but not defined. This is
   // important for correct linker interaction.
@@ -4111,24 +4107,6 @@
   IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
   IMGV->setConstant(true);
   UsedGlobals.push_back(IMGV);
-  
-  std::vector<llvm::Constant*> Used;
-
-  for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), 
-       e = UsedGlobals.end(); i != e; ++i) {
-    Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
-  }
-
-  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
-  llvm::GlobalValue *GV = 
-  new llvm::GlobalVariable(AT, false,
-                           llvm::GlobalValue::AppendingLinkage,
-                           llvm::ConstantArray::get(AT, Used),
-                           "llvm.used", 
-                           &CGM.getModule());
-  
-  GV->setSection("llvm.metadata");
-  
 }
 
 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of