[llvm-commits] [llvm] r51567 - in /llvm/trunk: include/llvm/Function.h include/llvm/GlobalValue.h include/llvm/GlobalVariable.h lib/Linker/LinkModules.cpp lib/Transforms/IPO/ArgumentPromotion.cpp lib/Transforms/IPO/DeadArgumentElimination.cpp lib/Transforms/IPO/ExtractGV.cpp lib/Transforms/IPO/StructRetPromotion.cpp lib/Transforms/Utils/CloneFunction.cpp lib/Transforms/Utils/CloneModule.cpp lib/VMCore/Function.cpp lib/VMCore/Globals.cpp

Duncan Sands baldrick at free.fr
Mon May 26 12:59:00 PDT 2008


Author: baldrick
Date: Mon May 26 14:58:59 2008
New Revision: 51567

URL: http://llvm.org/viewvc/llvm-project?rev=51567&view=rev
Log:
Factor code to copy global value attributes like
the section or the visibility from one global
value to another: copyAttributesFrom.  This is
particularly useful for duplicating functions:
previously this was done by explicitly copying
each attribute in turn at each place where a
new function was created out of an old one, with
the result that obscure attributes were regularly
forgotten (like the collector or the section).
Hopefully now everything is uniform and nothing
is forgotten.

Modified:
    llvm/trunk/include/llvm/Function.h
    llvm/trunk/include/llvm/GlobalValue.h
    llvm/trunk/include/llvm/GlobalVariable.h
    llvm/trunk/lib/Linker/LinkModules.cpp
    llvm/trunk/lib/Transforms/IPO/ArgumentPromotion.cpp
    llvm/trunk/lib/Transforms/IPO/DeadArgumentElimination.cpp
    llvm/trunk/lib/Transforms/IPO/ExtractGV.cpp
    llvm/trunk/lib/Transforms/IPO/StructRetPromotion.cpp
    llvm/trunk/lib/Transforms/Utils/CloneFunction.cpp
    llvm/trunk/lib/Transforms/Utils/CloneModule.cpp
    llvm/trunk/lib/VMCore/Function.cpp
    llvm/trunk/lib/VMCore/Globals.cpp

Modified: llvm/trunk/include/llvm/Function.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Function.h?rev=51567&r1=51566&r2=51567&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Function.h (original)
+++ llvm/trunk/include/llvm/Function.h Mon May 26 14:58:59 2008
@@ -204,6 +204,10 @@
     return paramHasAttr(1, ParamAttr::StructRet);
   }
 
+  /// copyAttributesFrom - copy all additional attributes (those not needed to
+  /// create a Function) from the Function Src to this one.
+  void copyAttributesFrom(const GlobalValue *Src);
+
   /// deleteBody - This method deletes the body of the function, and converts
   /// the linkage to external.
   ///

Modified: llvm/trunk/include/llvm/GlobalValue.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/GlobalValue.h?rev=51567&r1=51566&r2=51567&view=diff

==============================================================================
--- llvm/trunk/include/llvm/GlobalValue.h (original)
+++ llvm/trunk/include/llvm/GlobalValue.h Mon May 26 14:58:59 2008
@@ -110,6 +110,10 @@
   void setLinkage(LinkageTypes LT) { Linkage = LT; }
   LinkageTypes getLinkage() const { return Linkage; }
 
+  /// copyAttributesFrom - copy all additional attributes (those not needed to
+  /// create a GlobalValue) from the GlobalValue Src to this one.
+  virtual void copyAttributesFrom(const GlobalValue *Src);
+
   /// hasNotBeenReadFromBitcode - If a module provider is being used to lazily
   /// stream in functions from disk, this method can be used to check to see if
   /// the function has been read in yet or not.  Unless you are working on the

Modified: llvm/trunk/include/llvm/GlobalVariable.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/GlobalVariable.h?rev=51567&r1=51566&r2=51567&view=diff

==============================================================================
--- llvm/trunk/include/llvm/GlobalVariable.h (original)
+++ llvm/trunk/include/llvm/GlobalVariable.h Mon May 26 14:58:59 2008
@@ -118,6 +118,10 @@
   bool isThreadLocal() const { return isThreadLocalSymbol; }
   void setThreadLocal(bool Val) { isThreadLocalSymbol = Val; }
 
+  /// copyAttributesFrom - copy all additional attributes (those not needed to
+  /// create a GlobalVariable) from the GlobalVariable Src to this one.
+  void copyAttributesFrom(const GlobalValue *Src);
+
   /// removeFromParent - This method unlinks 'this' from the containing module,
   /// but does not delete it.
   ///

Modified: llvm/trunk/lib/Linker/LinkModules.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/LinkModules.cpp?rev=51567&r1=51566&r2=51567&view=diff

==============================================================================
--- llvm/trunk/lib/Linker/LinkModules.cpp (original)
+++ llvm/trunk/lib/Linker/LinkModules.cpp Mon May 26 14:58:59 2008
@@ -351,20 +351,10 @@
 /// CopyGVAttributes - copy additional attributes (those not needed to construct
 /// a GlobalValue) from the SrcGV to the DestGV. 
 static void CopyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) {
-  // Propagate alignment, visibility and section info.
-  DestGV->setAlignment(std::max(DestGV->getAlignment(), SrcGV->getAlignment()));
-  DestGV->setSection(SrcGV->getSection());
-  DestGV->setVisibility(SrcGV->getVisibility());
-  if (const Function *SrcF = dyn_cast<Function>(SrcGV)) {
-    Function *DestF = cast<Function>(DestGV);
-    DestF->setCallingConv(SrcF->getCallingConv());
-    DestF->setParamAttrs(SrcF->getParamAttrs());
-    if (SrcF->hasCollector())
-      DestF->setCollector(SrcF->getCollector());
-  } else if (const GlobalVariable *SrcVar = dyn_cast<GlobalVariable>(SrcGV)) {
-    GlobalVariable *DestVar = cast<GlobalVariable>(DestGV);
-    DestVar->setThreadLocal(SrcVar->isThreadLocal());
-  }
+  // Use the maximum alignment, rather than just copying the alignment of SrcGV.
+  unsigned Alignment = std::max(DestGV->getAlignment(), SrcGV->getAlignment());
+  DestGV->copyAttributesFrom(SrcGV);
+  DestGV->setAlignment(Alignment);
 }
 
 /// GetLinkageResult - This analyzes the two global values and determines what

Modified: llvm/trunk/lib/Transforms/IPO/ArgumentPromotion.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/ArgumentPromotion.cpp?rev=51567&r1=51566&r2=51567&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/IPO/ArgumentPromotion.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/ArgumentPromotion.cpp Mon May 26 14:58:59 2008
@@ -478,15 +478,13 @@
 
   // Create the new function body and insert it into the module...
   Function *NF = Function::Create(NFTy, F->getLinkage(), F->getName());
-  NF->setCallingConv(F->getCallingConv());
+  NF->copyAttributesFrom(F);
 
   // Recompute the parameter attributes list based on the new arguments for
   // the function.
   NF->setParamAttrs(PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end()));
   ParamAttrsVec.clear();
-  
-  if (F->hasCollector())
-    NF->setCollector(F->getCollector());
+
   F->getParent()->getFunctionList().insert(F, NF);
   NF->takeName(F);
 

Modified: llvm/trunk/lib/Transforms/IPO/DeadArgumentElimination.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/DeadArgumentElimination.cpp?rev=51567&r1=51566&r2=51567&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/IPO/DeadArgumentElimination.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/DeadArgumentElimination.cpp Mon May 26 14:58:59 2008
@@ -163,10 +163,7 @@
 
   // Create the new function body and insert it into the module...
   Function *NF = Function::Create(NFTy, Fn.getLinkage());
-  NF->setCallingConv(Fn.getCallingConv());
-  NF->setParamAttrs(Fn.getParamAttrs());
-  if (Fn.hasCollector())
-    NF->setCollector(Fn.getCollector());
+  NF->copyAttributesFrom(&Fn);
   Fn.getParent()->getFunctionList().insert(&Fn, NF);
   NF->takeName(&Fn);
 
@@ -556,10 +553,8 @@
 
   // Create the new function body and insert it into the module...
   Function *NF = Function::Create(NFTy, F->getLinkage());
-  NF->setCallingConv(F->getCallingConv());
+  NF->copyAttributesFrom(F);
   NF->setParamAttrs(NewPAL);
-  if (F->hasCollector())
-    NF->setCollector(F->getCollector());
   F->getParent()->getFunctionList().insert(F, NF);
   NF->takeName(F);
 

Modified: llvm/trunk/lib/Transforms/IPO/ExtractGV.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/ExtractGV.cpp?rev=51567&r1=51566&r2=51567&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/IPO/ExtractGV.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/ExtractGV.cpp Mon May 26 14:58:59 2008
@@ -123,10 +123,7 @@
         if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) {
           Function *New = Function::Create(I->getFunctionType(),
                                            GlobalValue::ExternalLinkage);
-          New->setCallingConv(I->getCallingConv());
-          New->setParamAttrs(I->getParamAttrs());
-          if (I->hasCollector())
-            New->setCollector(I->getCollector());
+          New->copyAttributesFrom(I);
 
           // If it's not the named function, delete the body of the function
           I->dropAllReferences();

Modified: llvm/trunk/lib/Transforms/IPO/StructRetPromotion.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/StructRetPromotion.cpp?rev=51567&r1=51566&r2=51567&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/IPO/StructRetPromotion.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/StructRetPromotion.cpp Mon May 26 14:58:59 2008
@@ -232,7 +232,7 @@
 
   FunctionType *NFTy = FunctionType::get(STy, Params, FTy->isVarArg());
   Function *NF = Function::Create(NFTy, F->getLinkage(), F->getName());
-  NF->setCallingConv(F->getCallingConv());
+  NF->copyAttributesFrom(F);
   NF->setParamAttrs(PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end()));
   F->getParent()->getFunctionList().insert(F, NF);
   NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());

Modified: llvm/trunk/lib/Transforms/Utils/CloneFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/CloneFunction.cpp?rev=51567&r1=51566&r2=51567&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/CloneFunction.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/CloneFunction.cpp Mon May 26 14:58:59 2008
@@ -80,11 +80,8 @@
     assert(ValueMap.count(I) && "No mapping from source argument specified!");
 #endif
 
-  // Clone the parameter attributes
-  NewFunc->setParamAttrs(OldFunc->getParamAttrs());
-
-  // Clone the calling convention
-  NewFunc->setCallingConv(OldFunc->getCallingConv());
+  // Clone any attributes.
+  NewFunc->copyAttributesFrom(OldFunc);
 
   // Loop over all of the basic blocks in the function, cloning them as
   // appropriate.  Note that we save BE this way in order to handle cloning of
@@ -339,8 +336,8 @@
        E = OldFunc->arg_end(); II != E; ++II)
     assert(ValueMap.count(II) && "No mapping from source argument specified!");
 #endif
-  
-  PruningFunctionCloner PFC(NewFunc, OldFunc, ValueMap, Returns, 
+
+  PruningFunctionCloner PFC(NewFunc, OldFunc, ValueMap, Returns,
                             NameSuffix, CodeInfo, TD);
 
   // Clone the entry block, and anything recursively reachable from it.

Modified: llvm/trunk/lib/Transforms/Utils/CloneModule.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/CloneModule.cpp?rev=51567&r1=51566&r2=51567&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/CloneModule.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/CloneModule.cpp Mon May 26 14:58:59 2008
@@ -65,10 +65,7 @@
     Function *NF =
       Function::Create(cast<FunctionType>(I->getType()->getElementType()),
                        GlobalValue::ExternalLinkage, I->getName(), New);
-    NF->setCallingConv(I->getCallingConv());
-    NF->setParamAttrs(I->getParamAttrs());
-    if (I->hasCollector())
-      NF->setCollector(I->getCollector());
+    NF->copyAttributesFrom(I);
     ValueMap[I]= NF;
   }
 

Modified: llvm/trunk/lib/VMCore/Function.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Function.cpp?rev=51567&r1=51566&r2=51567&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Function.cpp (original)
+++ llvm/trunk/lib/VMCore/Function.cpp Mon May 26 14:58:59 2008
@@ -284,6 +284,18 @@
   }
 }
 
+/// copyAttributesFrom - copy all additional attributes (those not needed to
+/// create a Function) from the Function Src to this one.
+void Function::copyAttributesFrom(const GlobalValue *Src) {
+  assert(isa<Function>(Src) && "Expected a Function!");
+  GlobalValue::copyAttributesFrom(Src);
+  const Function *SrcF = cast<Function>(Src);
+  setCallingConv(SrcF->getCallingConv());
+  setParamAttrs(SrcF->getParamAttrs());
+  if (SrcF->hasCollector())
+    setCollector(SrcF->getCollector());
+}
+
 /// getIntrinsicID - This method returns the ID number of the specified
 /// function, or Intrinsic::not_intrinsic if the function is not an
 /// intrinsic, or if the pointer is null.  This value is always defined to be

Modified: llvm/trunk/lib/VMCore/Globals.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Globals.cpp?rev=51567&r1=51566&r2=51567&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Globals.cpp (original)
+++ llvm/trunk/lib/VMCore/Globals.cpp Mon May 26 14:58:59 2008
@@ -79,7 +79,16 @@
   assert(0 && "You can't GV->destroyConstant()!");
   abort();
 }
-  
+
+/// copyAttributesFrom - copy all additional attributes (those not needed to
+/// create a GlobalValue) from the GlobalValue Src to this one.
+void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
+  setAlignment(Src->getAlignment());
+  setSection(Src->getSection());
+  setVisibility(Src->getVisibility());
+}
+
+
 //===----------------------------------------------------------------------===//
 // GlobalVariable Implementation
 //===----------------------------------------------------------------------===//
@@ -160,6 +169,16 @@
   this->setOperand(0, cast<Constant>(To));
 }
 
+/// copyAttributesFrom - copy all additional attributes (those not needed to
+/// create a GlobalVariable) from the GlobalVariable Src to this one.
+void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
+  assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
+  GlobalValue::copyAttributesFrom(Src);
+  const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
+  setThreadLocal(SrcVar->isThreadLocal());
+}
+
+
 //===----------------------------------------------------------------------===//
 // GlobalAlias Implementation
 //===----------------------------------------------------------------------===//





More information about the llvm-commits mailing list