Add new linkage types to support a real frontend
authorChris Lattner <sabre@nondot.org>
Wed, 16 Apr 2003 20:28:45 +0000 (20:28 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 16 Apr 2003 20:28:45 +0000 (20:28 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5786 91177308-0d34-0410-b5e6-96231b3b80d8

26 files changed:
include/llvm/Function.h
include/llvm/GlobalValue.h
include/llvm/GlobalVariable.h
lib/Analysis/DataStructure/Parallelize.cpp
lib/AsmParser/Lexer.l
lib/AsmParser/ParserInternals.h
lib/AsmParser/llvmAsmParser.y
lib/Bytecode/Reader/ConstantReader.cpp
lib/Bytecode/Reader/Reader.cpp
lib/Linker/LinkModules.cpp
lib/Target/SparcV9/SparcV9PreSelection.cpp
lib/Transforms/IPO/ExtractFunction.cpp
lib/Transforms/IPO/Internalize.cpp
lib/Transforms/IPO/MutateStructTypes.cpp
lib/Transforms/IPO/Parallelize.cpp
lib/Transforms/IPO/PoolAllocate.cpp
lib/Transforms/Instrumentation/EmitFunctions.cpp
lib/Transforms/Instrumentation/ProfilePaths/ProfilePaths.cpp
lib/Transforms/Instrumentation/TraceValues.cpp
lib/Transforms/Utils/CloneFunction.cpp
lib/Transforms/Utils/CloneModule.cpp
lib/Transforms/Utils/Linker.cpp
lib/VMCore/AsmWriter.cpp
lib/VMCore/Function.cpp
lib/VMCore/Linker.cpp
lib/VMCore/Module.cpp

index 885824d4f7e7f191873dfa7e96834c68f1950953..fe8216210d00e0ab055f7b7ef9dfdeab1d95c95e 100644 (file)
@@ -71,8 +71,8 @@ public:
   /// function is automatically inserted into the end of the function list for
   /// the module.
   ///
-  Function(const FunctionType *Ty, bool isInternal, const std::string &N = "",
-           Module *M = 0);
+  Function(const FunctionType *Ty, LinkageTypes Linkage,
+           const std::string &N = "", Module *M = 0);
   ~Function();
 
   // Specialize setName to handle symbol table majik...
index af2ff23c82edfedc56fbea6dcb7dc02e0d0a059c..13ad9237182949cd7302853cff5582949549c337 100644 (file)
@@ -16,12 +16,19 @@ class Module;
 
 class GlobalValue : public User {
   GlobalValue(const GlobalValue &);             // do not implement
+public:
+  enum LinkageTypes {
+    ExternalLinkage,   // Externally visible function
+    LinkOnceLinkage,   // Keep one copy of named function when linking (inline)
+    AppendingLinkage,  // Special purpose, only applies to global arrays
+    InternalLinkage    // Rename collisions when linking (static functions)
+  };
 protected:
-  GlobalValue(const Type *Ty, ValueTy vty, bool hasInternalLinkage,
+  GlobalValue(const Type *Ty, ValueTy vty, LinkageTypes linkage,
              const std::string &name = "")
-    : User(Ty, vty, name), HasInternalLinkage(hasInternalLinkage), Parent(0) {}
+    : User(Ty, vty, name), Linkage(linkage), Parent(0) {}
 
-  bool HasInternalLinkage;    // Is this value accessable externally?
+  LinkageTypes Linkage;   // The linkage of this global
   Module *Parent;
 public:
   ~GlobalValue() {}
@@ -31,10 +38,12 @@ public:
     return (const PointerType*)User::getType();
   }
 
-  /// Internal Linkage - True if the global value is inaccessible to 
-  bool hasInternalLinkage() const { return HasInternalLinkage; }
-  bool hasExternalLinkage() const { return !HasInternalLinkage; }
-  void setInternalLinkage(bool HIL) { HasInternalLinkage = HIL; }
+  bool hasExternalLinkage()  const { return Linkage == ExternalLinkage; }
+  bool hasLinkOnceLinkage()  const { return Linkage == LinkOnceLinkage; }
+  bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
+  bool hasInternalLinkage()  const { return Linkage == InternalLinkage; }
+  void setLinkage(LinkageTypes LT) { Linkage = LT; }
+  LinkageTypes getLinkage() const { return Linkage; }
 
   /// isExternal - Return true if the primary definition of this global value is
   /// outside of the current translation unit...
index a534d19008b9ede0c7f14e2ed7b6df880d502e9c..6fb74a3dbefb247dff94ca9f6f005ab38de52bc4 100644 (file)
@@ -35,7 +35,7 @@ public:
   /// GlobalVariable ctor - If a parent module is specified, the global is
   /// automatically inserted into the end of the specified modules global list.
   ///
-  GlobalVariable(const Type *Ty, bool isConstant, bool isInternal,
+  GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
                 Constant *Initializer = 0, const std::string &Name = "",
                  Module *Parent = 0);
 
index 08c800eb1c77063ec9dc5b6f9d1a1258c1cd2ec1..46fbcc7d855f966b8f7bbda14b05792164d82cde 100644 (file)
@@ -137,7 +137,8 @@ Cilkifier::Cilkifier(Module& M)
   DummySyncFunc = new Function(FunctionType::get( Type::VoidTy,
                                                  std::vector<const Type*>(),
                                                  /*isVararg*/ false),
-                               /*isInternal*/ false, DummySyncFuncName, &M);
+                               GlobalValue::ExternalLinkage, DummySyncFuncName,
+                               &M);
 }
 
 void Cilkifier::TransformFunc(Function* F,
index 7776c90a90a1c3ef8d17f91c2754f343b35b7e1f..f342d2fe342288f78685e7d606b9413f9830fb5b 100644 (file)
@@ -155,6 +155,8 @@ global          { return GLOBAL; }
 constant        { return CONSTANT; }
 const           { return CONST; }
 internal        { return INTERNAL; }
+linkonce        { return LINKONCE; }
+appending       { return APPENDING; }
 uninitialized   { return EXTERNAL; }    /* Deprecated, turn into external */
 external        { return EXTERNAL; }
 implementation  { return IMPLEMENTATION; }
index 496e37a5db4d86b3e3b059ff87a31d9357f7c27f..667c08b24206dae3a2559529a7f92636377af72f 100644 (file)
@@ -177,7 +177,7 @@ struct BBPlaceHolderHelper : public BasicBlock {
 
 struct MethPlaceHolderHelper : public Function {
   MethPlaceHolderHelper(const Type *Ty)
-    : Function(cast<FunctionType>(Ty), true) {}
+    : Function(cast<FunctionType>(Ty), InternalLinkage) {}
 };
 
 typedef PlaceholderValue<InstPlaceHolderHelper>  ValuePlaceHolder;
index a25aaf805de3abd29f8b843c091288b1b28ea3fb..b26565180bbc3966300b15670c47e0dcc2e2aabb 100644 (file)
@@ -522,8 +522,7 @@ static bool setValueName(Value *V, char *NameStr) {
             EGV->setInitializer(GV->getInitializer());
           if (GV->isConstant())
             EGV->setConstant(true);
-          if (GV->hasInternalLinkage())
-            EGV->setInternalLinkage(true);
+          EGV->setLinkage(GV->getLinkage());
           
          delete GV;     // Destroy the duplicate!
           return true;   // They are equivalent!
@@ -624,6 +623,7 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
   std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
   std::vector<Constant*>           *ConstVector;
 
+  GlobalValue::LinkageTypes         Linkage;
   int64_t                           SInt64Val;
   uint64_t                          UInt64Val;
   int                               SIntVal;
@@ -654,7 +654,8 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
 %type <ValueList>     IndexList                   // For GEP derived indices
 %type <TypeList>      TypeListI ArgTypeListI
 %type <JumpTable>     JumpTable
-%type <BoolVal>       GlobalType OptInternal      // GLOBAL or CONSTANT? Intern?
+%type <BoolVal>       GlobalType                  // GLOBAL or CONSTANT?
+%type <Linkage>       OptLinkage
 
 // ValueRef - Unresolved reference to a definition or BB
 %type <ValIDVal>      ValueRef ConstValueRef SymbolicValueRef
@@ -684,7 +685,8 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
 
 
 %token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT
-%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL OPAQUE NOT EXTERNAL
+%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE APPENDING
+%token OPAQUE NOT EXTERNAL
 
 // Basic Block Terminating Operators 
 %token <TermOpVal> RET BR SWITCH
@@ -748,7 +750,10 @@ OptAssign : VAR_ID '=' {
     $$ = 0; 
   };
 
-OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
+OptLinkage : INTERNAL  { $$ = GlobalValue::InternalLinkage; } |
+             LINKONCE  { $$ = GlobalValue::LinkOnceLinkage; } |
+             APPENDING { $$ = GlobalValue::AppendingLinkage; } |
+             /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
 
 //===----------------------------------------------------------------------===//
 // Types includes all predefined types... except void, because it can only be
@@ -982,7 +987,8 @@ ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
        
        // Create a placeholder for the global variable reference...
        GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
-                                                false, true);
+                                                false,
+                                                GlobalValue::ExternalLinkage);
        // Keep track of the fact that we have a forward ref to recycle it
        CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
 
@@ -1136,7 +1142,7 @@ ConstPool : ConstPool OptAssign CONST ConstVal {
   }
   | ConstPool FunctionProto {       // Function prototypes can be in const pool
   }
-  | ConstPool OptAssign OptInternal GlobalType ConstVal {
+  | ConstPool OptAssign OptLinkage GlobalType ConstVal {
     const Type *Ty = $5->getType();
     // Global declarations appear in Constant Pool
     Constant *Initializer = $5;
@@ -1159,7 +1165,7 @@ ConstPool : ConstPool OptAssign CONST ConstVal {
   | ConstPool OptAssign EXTERNAL GlobalType Types {
     const Type *Ty = *$5;
     // Global declarations appear in Constant Pool
-    GlobalVariable *GV = new GlobalVariable(Ty, $4, false);
+    GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
     if (!setValueName(GV, $2)) {   // If not redefining...
       CurModule.CurrentModule->getGlobalList().push_back(GV);
       int Slot = InsertValue(GV, CurModule.Values);
@@ -1255,7 +1261,7 @@ FunctionHeaderH : TypesV FuncName '(' ArgList ')' {
       AI->setName("");
 
   } else  {  // Not already defined?
-    Fn = new Function(FT, false, FunctionName);
+    Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
     InsertValue(Fn, CurModule.Values);
     CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
   }
@@ -1288,13 +1294,12 @@ FunctionHeaderH : TypesV FuncName '(' ArgList ')' {
 
 BEGIN : BEGINTOK | '{';                // Allow BEGIN or '{' to start a function
 
-FunctionHeader : OptInternal FunctionHeaderH BEGIN {
+FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
   $$ = CurMeth.CurrentFunction;
 
-  // Make sure that we keep track of the internal marker, even if there was
-  // a previous "declare".
-  if ($1)
-    $$->setInternalLinkage(true);
+  // Make sure that we keep track of the linkage type even if there was a
+  // previous "declare".
+  $$->setLinkage($1);
 
   // Resolve circular types before we parse the body of the function.
   ResolveTypes(CurMeth.LateResolveTypes);
index d2818ef3164d47534a4841a16e6d72681d4369ee..d1c8644ddd4fe8c5ef035eee8004014620f3b351 100644 (file)
@@ -352,7 +352,8 @@ bool BytecodeParser::parseConstantValue(const uchar *&Buf, const uchar *EndBuf,
           
           // Create a placeholder for the global variable reference...
           GlobalVariable *GVar =
-            new GlobalVariable(PT->getElementType(), false, true);
+            new GlobalVariable(PT->getElementType(), false,
+                               GlobalValue::InternalLinkage);
           
           // Keep track of the fact that we have a forward ref to recycle it
           GlobalRefs.insert(std::make_pair(std::make_pair(PT, Slot), GVar));
index 805b7e7292b2e37620a4567287af9596cc8246c4..fb561aa16ce73e84b887b3744f45f22b69ca0c87 100644 (file)
@@ -307,7 +307,8 @@ bool BytecodeParser::ParseFunction(const uchar *&Buf, const uchar *EndBuf) {
   Function *F = FunctionSignatureList.back().first;
   unsigned FunctionSlot = FunctionSignatureList.back().second;
   FunctionSignatureList.pop_back();
-  F->setInternalLinkage(isInternal != 0);
+  F->setLinkage(isInternal ? GlobalValue::InternalLinkage :
+                             GlobalValue::ExternalLinkage);
 
   const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
   Function::aiterator AI = F->abegin();
@@ -399,8 +400,13 @@ bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End){
 
     const Type *ElTy = cast<PointerType>(Ty)->getElementType();
 
+
+    GlobalValue::LinkageTypes Linkage = 
+      (VarType & 4) ? GlobalValue::InternalLinkage :
+                      GlobalValue::ExternalLinkage;
+
     // Create the global variable...
-    GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
+    GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
                                             0, "", TheModule);
     int DestSlot = insertValue(GV, ModuleValues);
     if (DestSlot == -1) return true;
@@ -435,7 +441,8 @@ bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End){
     // this placeholder is replaced.
 
     // Insert the placeholder...
-    Function *Func = new Function(cast<FunctionType>(Ty), false, "", TheModule);
+    Function *Func = new Function(cast<FunctionType>(Ty),
+                                  GlobalValue::InternalLinkage, "", TheModule);
     int DestSlot = insertValue(Func, ModuleValues);
     if (DestSlot == -1) return true;
     ResolveReferencesToValue(Func, (unsigned)DestSlot);
index 60c6e03ae128b2b6a8348c617702b5ba319e105d..b5207a8542926b01b5b3fe63b375d898834b938a 100644 (file)
@@ -189,42 +189,48 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
   //
   for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
     const GlobalVariable *SGV = I;
-    Value *V;
-
-    // If the global variable has a name, and that name is already in use in the
-    // Dest module, make sure that the name is a compatible global variable...
-    //
-    if (SGV->hasExternalLinkage() && SGV->hasName() &&
-       (V = ST->lookup(SGV->getType(), SGV->getName())) &&
-       cast<GlobalVariable>(V)->hasExternalLinkage()) {
-      // The same named thing is a global variable, because the only two things
+    GlobalVariable *DGV = 0;
+    if (SGV->hasName()) {
+      // A same named thing is a global variable, because the only two things
       // that may be in a module level symbol table are Global Vars and
       // Functions, and they both have distinct, nonoverlapping, possible types.
       // 
-      GlobalVariable *DGV = cast<GlobalVariable>(V);
+      DGV = cast_or_null<GlobalVariable>(ST->lookup(SGV->getType(),
+                                                    SGV->getName()));
+    }
 
-      // Check to see if the two GV's have the same Const'ness...
-      if (SGV->isConstant() != DGV->isConstant())
-        return Error(Err, "Global Variable Collision on '" + 
-                     SGV->getType()->getDescription() + "':%" + SGV->getName() +
-                     " - Global variables differ in const'ness");
+    assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
+           "Global must either be external or have an initializer!");
 
-      // Okay, everything is cool, remember the mapping...
-      ValueMap.insert(std::make_pair(SGV, DGV));
-    } else {
+    if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
       // No linking to be performed, simply create an identical version of the
       // symbol over in the dest module... the initializer will be filled in
       // later by LinkGlobalInits...
       //
-      GlobalVariable *DGV = 
-        new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
-                           SGV->hasInternalLinkage(), 0, SGV->getName());
-
-      // Add the new global to the dest module
-      Dest->getGlobalList().push_back(DGV);
+      DGV = new GlobalVariable(SGV->getType()->getElementType(),
+                               SGV->isConstant(), SGV->getLinkage(), /*init*/0,
+                               SGV->getName(), Dest);
 
       // Make sure to remember this mapping...
       ValueMap.insert(std::make_pair(SGV, DGV));
+    } else if (SGV->getLinkage() != DGV->getLinkage()) {
+      return Error(Err, "Global variables named '" + SGV->getName() +
+                   "' have different linkage specifiers!");
+    } else if (SGV->hasExternalLinkage() || SGV->hasLinkOnceLinkage() ||
+               SGV->hasAppendingLinkage()) {
+      // If the global variable has a name, and that name is already in use in
+      // the Dest module, make sure that the name is a compatible global
+      // variable...
+      //
+      // Check to see if the two GV's have the same Const'ness...
+      if (SGV->isConstant() != DGV->isConstant())
+        return Error(Err, "Global Variable Collision on '" + 
+                     SGV->getType()->getDescription() + "':%" + SGV->getName() +
+                     " - Global variables differ in const'ness");
+      // Okay, everything is cool, remember the mapping...
+      ValueMap.insert(std::make_pair(SGV, DGV));
+    } else {
+      assert(0 && "Unknown linkage!");
     }
   }
   return false;
@@ -245,19 +251,28 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
 
     if (SGV->hasInitializer()) {      // Only process initialized GV's
       // Figure out what the initializer looks like in the dest module...
-      Constant *DInit =
+      Constant *SInit =
         cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0));
 
       GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);    
-      if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
-         DGV->hasExternalLinkage()) {
-        if (DGV->getInitializer() != DInit)
-          return Error(Err, "Global Variable Collision on '" + 
-                       SGV->getType()->getDescription() + "':%" +SGV->getName()+
-                       " - Global variables have different initializers");
+      if (DGV->hasInitializer()) {
+        assert(SGV->getLinkage() == DGV->getLinkage());
+        if (SGV->hasExternalLinkage()) {
+          if (DGV->getInitializer() != SInit)
+            return Error(Err, "Global Variable Collision on '" + 
+                         SGV->getType()->getDescription() +"':%"+SGV->getName()+
+                         " - Global variables have different initializers");
+        } else if (DGV->hasLinkOnceLinkage()) {
+          // Nothing is required, mapped values will take the new global
+          // automatically.
+        } else if (DGV->hasAppendingLinkage()) {
+          assert(0 && "Appending linkage unimplemented!");
+        } else {
+          assert(0 && "Unknown linkage!");
+        }
       } else {
         // Copy the initializer over now...
-        DGV->setInitializer(DInit);
+        DGV->setInitializer(SInit);
       }
     }
   }
@@ -278,39 +293,42 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
   //
   for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
     const Function *SF = I;   // SrcFunction
-    Value *V;
-
-    // If the function has a name, and that name is already in use in the Dest
-    // module, make sure that the name is a compatible function...
-    //
-    if (SF->hasExternalLinkage() && SF->hasName() &&
-       (V = ST->lookup(SF->getType(), SF->getName())) &&
-       cast<Function>(V)->hasExternalLinkage()) {
+    Function *DF = 0;
+    if (SF->hasName())
       // The same named thing is a Function, because the only two things
       // that may be in a module level symbol table are Global Vars and
       // Functions, and they both have distinct, nonoverlapping, possible types.
       // 
-      Function *DF = cast<Function>(V);   // DestFunction
+      DF = cast_or_null<Function>(ST->lookup(SF->getType(), SF->getName()));
 
+    if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
+      // Function does not already exist, simply insert an external function
+      // signature identical to SF into the dest module...
+      Function *DF = new Function(SF->getFunctionType(), SF->getLinkage(),
+                                  SF->getName(), Dest);
+
+      // ... and remember this mapping...
+      ValueMap.insert(std::make_pair(SF, DF));
+    } else if (SF->getLinkage() != DF->getLinkage()) {
+      return Error(Err, "Functions named '" + SF->getName() +
+                   "' have different linkage specifiers!");
+    } else if (SF->getLinkage() == GlobalValue::AppendingLinkage) {
+      return Error(Err, "Functions named '" + SF->getName() +
+                   "' have appending linkage!");
+    } else if (SF->getLinkage() == GlobalValue::ExternalLinkage) {
+      // If the function has a name, and that name is already in use in the Dest
+      // module, make sure that the name is a compatible function...
+      //
       // Check to make sure the function is not defined in both modules...
       if (!SF->isExternal() && !DF->isExternal())
         return Error(Err, "Function '" + 
                      SF->getFunctionType()->getDescription() + "':\"" + 
                      SF->getName() + "\" - Function is already defined!");
-
+      
       // Otherwise, just remember this mapping...
       ValueMap.insert(std::make_pair(SF, DF));
-    } else {
-      // Function does not already exist, simply insert an external function
-      // signature identical to SF into the dest module...
-      Function *DF = new Function(SF->getFunctionType(),
-                                  SF->hasInternalLinkage(),
-                                  SF->getName());
-
-      // Add the function signature to the dest module...
-      Dest->getFunctionList().push_back(DF);
-
-      // ... and remember this mapping...
+    } else if (SF->getLinkage() == GlobalValue::LinkOnceLinkage) {
+      // Completely ignore the source function.
       ValueMap.insert(std::make_pair(SF, DF));
     }
   }
@@ -391,6 +409,7 @@ static bool LinkFunctionBodies(Module *Dest, const Module *Src,
 
       // DF not external SF external?
       if (!DF->isExternal()) {
+        if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
         if (Err)
           *Err = "Function '" + (SF->hasName() ? SF->getName() :std::string(""))
                + "' body multiply defined!";
index 7cd5b1dc055101756b7734868dd5ec4adbf363e2..b4bbc77a071e1279a50c35f8dba91123ed4041f0 100644 (file)
@@ -87,7 +87,8 @@ namespace {
             GV = PI->second;            // put in map
           else
             {
-              GV = new GlobalVariable(CV->getType(), true,true,CV); //put in map
+              GV = new GlobalVariable(CV->getType(), true, //put in map
+                                      GlobalValue::InternalLinkage, CV);
               myModule->getGlobalList().push_back(GV); // GV owned by module now
             }
         }
index a2d28f58c6816f897141312eb73e0773693c6fc1..f16c3f325304e020373ced5f2496e3cef8882163 100644 (file)
@@ -16,13 +16,13 @@ namespace {
       }
 
       // Make sure our result is globally accessable...
-      Named->setInternalLinkage(false);
+      Named->setLinkage(GlobalValue::ExternalLinkage);
 
       // Mark all global variables internal
       for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
         if (!I->isExternal()) {
           I->setInitializer(0);  // Make all variables external
-          I->setInternalLinkage(false); // Make sure it's not internal
+          I->setLinkage(GlobalValue::ExternalLinkage);
         }
       
       // All of the functions may be used by global variables or the named
@@ -35,7 +35,9 @@ namespace {
       
       for (Module::iterator I = M.begin(); ; ++I) {
         if (&*I != Named) {
-          Function *New = new Function(I->getFunctionType(),false,I->getName());
+          Function *New = new Function(I->getFunctionType(),
+                                       GlobalValue::ExternalLinkage,
+                                       I->getName());
           I->setName("");  // Remove Old name
           
           // If it's not the named function, delete the body of the function
index 910e1d4322dac9a1400838f5ca10c68266696596..f09fd141c72cbab8a2fd8427d26820c2cdc7ad3a 100644 (file)
@@ -29,7 +29,7 @@ namespace {
         if (&*I != MainFunc &&          // Leave the main function external
             !I->isExternal() &&         // Function must be defined here
             !I->hasInternalLinkage()) { // Can't already have internal linkage
-          I->setInternalLinkage(true);
+          I->setLinkage(GlobalValue::InternalLinkage);
           Changed = true;
           ++NumFunctions;
           DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
@@ -38,7 +38,7 @@ namespace {
       // Mark all global variables with initializers as internal as well...
       for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
         if (!I->isExternal() && I->hasExternalLinkage()) {
-          I->setInternalLinkage(true);
+          I->setLinkage(GlobalValue::InternalLinkage);
           Changed = true;
           ++NumGlobals;
           DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
index b7418ec6e3e23826e66db9f51f0fe7ba3304e613..8e58d4dbb20820f5bb75a602c8aff00629dbf430 100644 (file)
@@ -250,8 +250,7 @@ void MutateStructTypes::processGlobals(Module &M) {
         cast<FunctionType>(ConvertType(I->getFunctionType()));
       
       // Create a new function to put stuff into...
-      Function *NewMeth = new Function(NewMTy, I->hasInternalLinkage(),
-                                       I->getName());
+      Function *NewMeth = new Function(NewMTy, I->getLinkage(), I->getName());
       if (I->hasName())
         I->setName("OLD."+I->getName());
 
index 08c800eb1c77063ec9dc5b6f9d1a1258c1cd2ec1..46fbcc7d855f966b8f7bbda14b05792164d82cde 100644 (file)
@@ -137,7 +137,8 @@ Cilkifier::Cilkifier(Module& M)
   DummySyncFunc = new Function(FunctionType::get( Type::VoidTy,
                                                  std::vector<const Type*>(),
                                                  /*isVararg*/ false),
-                               /*isInternal*/ false, DummySyncFuncName, &M);
+                               GlobalValue::ExternalLinkage, DummySyncFuncName,
+                               &M);
 }
 
 void Cilkifier::TransformFunc(Function* F,
index b2efc12de2768b37a6f437e53780a9abedfb7631..844d0c15ffab5f35b4896d394e70a072305a4b2b 100644 (file)
@@ -161,7 +161,8 @@ Function *PoolAllocate::MakeFunctionClone(Function &F) {
   FunctionType *FuncTy = FunctionType::get(OldFuncTy->getReturnType(), ArgTys,
                                            OldFuncTy->isVarArg());
   // Create the new function...
-  Function *New = new Function(FuncTy, true, F.getName(), F.getParent());
+  Function *New = new Function(FuncTy, GlobalValue::InternalLinkage,
+                               F.getName(), F.getParent());
 
   // Set the rest of the new arguments names to be PDa<n> and add entries to the
   // pool descriptors map
index d4a99e2c6743f6cc4a5891884aa2eef6d46bfe7b..32488d69b53ae9a0e7716ae4808807d22504fd7f 100644 (file)
@@ -33,7 +33,8 @@ bool EmitFunctionTable::run(Module &M){
   StructType *sttype = StructType::get(vType);
   ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
 
-  GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, false, 
+  GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
+                                          GlobalValue::ExternalLinkage, 
                                           cstruct, "llvmFunctionTable");
   M.getGlobalList().push_back(gb);
   return true;  // Always modifies program
index 39e7c351210a60ee41edb389ff12905fc8c8df63..57b17a590ff36788d2e155c25b9adedda54402cf 100644 (file)
@@ -183,14 +183,19 @@ bool ProfilePaths::runOnFunction(Function &F){
   for(int xi=0; xi<numPaths; xi++)
     arrayInitialize.push_back(ConstantSInt::get(Type::IntTy, 0));
 
-  Constant *initializer =  ConstantArray::get(ArrayType::get(Type::IntTy, numPaths), arrayInitialize);
-  GlobalVariable *countVar = new GlobalVariable(ArrayType::get(Type::IntTy, numPaths), false, true, initializer, "Count", F.getParent());
+  const ArrayType *ATy = ArrayType::get(Type::IntTy, numPaths);
+  Constant *initializer =  ConstantArray::get(ATy, arrayInitialize);
+  GlobalVariable *countVar = new GlobalVariable(ATy, false,
+                                                GlobalValue::InternalLinkage, 
+                                                initializer, "Count",
+                                                F.getParent());
   static GlobalVariable *threshold = NULL;
   static bool insertedThreshold = false;
 
   if(!insertedThreshold){
-    threshold = new GlobalVariable(Type::IntTy, false, false, 0,
-                                                   "reopt_threshold");
+    threshold = new GlobalVariable(Type::IntTy, false,
+                                   GlobalValue::ExternalLinkage, 0,
+                                   "reopt_threshold");
 
     F.getParent()->getGlobalList().push_back(threshold);
     insertedThreshold = true;
index c19848dd4b5530724af08f48e8c9c19db718e02b..75149ce91232c5895ae4f1713b08d1c346adfb7b 100644 (file)
@@ -154,7 +154,8 @@ static inline GlobalVariable *getStringRef(Module *M, const string &str) {
 
   // Create the global variable and record it in the module
   // The GV will be renamed to a unique name if needed.
-  GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
+  GlobalVariable *GV = new GlobalVariable(Init->getType(), true, 
+                                          GlobalValue::InternalLinkage, Init,
                                           "trstr");
   M->getGlobalList().push_back(GV);
   return GV;
index ca22003b4094eca3158faba5ac4287d3865895e8..512bd38d839c96ad0cac52d553f99459925e0c4d 100644 (file)
@@ -110,7 +110,7 @@ Function *CloneFunction(const Function *F,
                                     ArgTypes, F->getFunctionType()->isVarArg());
 
   // Create the new function...
-  Function *NewF = new Function(FTy, F->hasInternalLinkage(), F->getName());
+  Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
   
   // Loop over the arguments, copying the names of the mapped arguments over...
   Function::aiterator DestI = NewF->abegin();
index 043e06d5437b2cc16b3c0cc6a0fc562c6905f53f..223e3fd88cd0f95039cde5b395ae26474205651d 100644 (file)
@@ -29,13 +29,14 @@ Module *CloneModule(const Module *M) {
   // don't worry about attributes or initializers, they will come later.
   //
   for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
-    ValueMap[I] = new GlobalVariable(I->getType()->getElementType(),
-                                     false, false, 0, I->getName(), New);
+    ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
+                                     GlobalValue::ExternalLinkage, 0,
+                                     I->getName(), New);
 
   // Loop over the functions in the module, making external functions as before
   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
     ValueMap[I]=new Function(cast<FunctionType>(I->getType()->getElementType()),
-                             false, I->getName(), New);
+                             GlobalValue::ExternalLinkage, I->getName(), New);
 
   // Now that all of the things that global variable initializer can refer to
   // have been created, loop through and copy the global variable referrers
@@ -46,8 +47,7 @@ Module *CloneModule(const Module *M) {
     if (I->hasInitializer())
       GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
                                                  ValueMap)));
-    if (I->hasInternalLinkage())
-      GV->setInternalLinkage(true);
+    GV->setLinkage(I->getLinkage());
   }
 
   // Similarly, copy over function bodies now...
@@ -65,8 +65,7 @@ Module *CloneModule(const Module *M) {
       CloneFunctionInto(F, I, ValueMap, Returns);
     }
 
-    if (I->hasInternalLinkage())
-      F->setInternalLinkage(true);
+    F->setLinkage(I->getLinkage());
   }
 
   return New;
index 60c6e03ae128b2b6a8348c617702b5ba319e105d..b5207a8542926b01b5b3fe63b375d898834b938a 100644 (file)
@@ -189,42 +189,48 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
   //
   for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
     const GlobalVariable *SGV = I;
-    Value *V;
-
-    // If the global variable has a name, and that name is already in use in the
-    // Dest module, make sure that the name is a compatible global variable...
-    //
-    if (SGV->hasExternalLinkage() && SGV->hasName() &&
-       (V = ST->lookup(SGV->getType(), SGV->getName())) &&
-       cast<GlobalVariable>(V)->hasExternalLinkage()) {
-      // The same named thing is a global variable, because the only two things
+    GlobalVariable *DGV = 0;
+    if (SGV->hasName()) {
+      // A same named thing is a global variable, because the only two things
       // that may be in a module level symbol table are Global Vars and
       // Functions, and they both have distinct, nonoverlapping, possible types.
       // 
-      GlobalVariable *DGV = cast<GlobalVariable>(V);
+      DGV = cast_or_null<GlobalVariable>(ST->lookup(SGV->getType(),
+                                                    SGV->getName()));
+    }
 
-      // Check to see if the two GV's have the same Const'ness...
-      if (SGV->isConstant() != DGV->isConstant())
-        return Error(Err, "Global Variable Collision on '" + 
-                     SGV->getType()->getDescription() + "':%" + SGV->getName() +
-                     " - Global variables differ in const'ness");
+    assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
+           "Global must either be external or have an initializer!");
 
-      // Okay, everything is cool, remember the mapping...
-      ValueMap.insert(std::make_pair(SGV, DGV));
-    } else {
+    if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
       // No linking to be performed, simply create an identical version of the
       // symbol over in the dest module... the initializer will be filled in
       // later by LinkGlobalInits...
       //
-      GlobalVariable *DGV = 
-        new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
-                           SGV->hasInternalLinkage(), 0, SGV->getName());
-
-      // Add the new global to the dest module
-      Dest->getGlobalList().push_back(DGV);
+      DGV = new GlobalVariable(SGV->getType()->getElementType(),
+                               SGV->isConstant(), SGV->getLinkage(), /*init*/0,
+                               SGV->getName(), Dest);
 
       // Make sure to remember this mapping...
       ValueMap.insert(std::make_pair(SGV, DGV));
+    } else if (SGV->getLinkage() != DGV->getLinkage()) {
+      return Error(Err, "Global variables named '" + SGV->getName() +
+                   "' have different linkage specifiers!");
+    } else if (SGV->hasExternalLinkage() || SGV->hasLinkOnceLinkage() ||
+               SGV->hasAppendingLinkage()) {
+      // If the global variable has a name, and that name is already in use in
+      // the Dest module, make sure that the name is a compatible global
+      // variable...
+      //
+      // Check to see if the two GV's have the same Const'ness...
+      if (SGV->isConstant() != DGV->isConstant())
+        return Error(Err, "Global Variable Collision on '" + 
+                     SGV->getType()->getDescription() + "':%" + SGV->getName() +
+                     " - Global variables differ in const'ness");
+      // Okay, everything is cool, remember the mapping...
+      ValueMap.insert(std::make_pair(SGV, DGV));
+    } else {
+      assert(0 && "Unknown linkage!");
     }
   }
   return false;
@@ -245,19 +251,28 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
 
     if (SGV->hasInitializer()) {      // Only process initialized GV's
       // Figure out what the initializer looks like in the dest module...
-      Constant *DInit =
+      Constant *SInit =
         cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0));
 
       GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);    
-      if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
-         DGV->hasExternalLinkage()) {
-        if (DGV->getInitializer() != DInit)
-          return Error(Err, "Global Variable Collision on '" + 
-                       SGV->getType()->getDescription() + "':%" +SGV->getName()+
-                       " - Global variables have different initializers");
+      if (DGV->hasInitializer()) {
+        assert(SGV->getLinkage() == DGV->getLinkage());
+        if (SGV->hasExternalLinkage()) {
+          if (DGV->getInitializer() != SInit)
+            return Error(Err, "Global Variable Collision on '" + 
+                         SGV->getType()->getDescription() +"':%"+SGV->getName()+
+                         " - Global variables have different initializers");
+        } else if (DGV->hasLinkOnceLinkage()) {
+          // Nothing is required, mapped values will take the new global
+          // automatically.
+        } else if (DGV->hasAppendingLinkage()) {
+          assert(0 && "Appending linkage unimplemented!");
+        } else {
+          assert(0 && "Unknown linkage!");
+        }
       } else {
         // Copy the initializer over now...
-        DGV->setInitializer(DInit);
+        DGV->setInitializer(SInit);
       }
     }
   }
@@ -278,39 +293,42 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
   //
   for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
     const Function *SF = I;   // SrcFunction
-    Value *V;
-
-    // If the function has a name, and that name is already in use in the Dest
-    // module, make sure that the name is a compatible function...
-    //
-    if (SF->hasExternalLinkage() && SF->hasName() &&
-       (V = ST->lookup(SF->getType(), SF->getName())) &&
-       cast<Function>(V)->hasExternalLinkage()) {
+    Function *DF = 0;
+    if (SF->hasName())
       // The same named thing is a Function, because the only two things
       // that may be in a module level symbol table are Global Vars and
       // Functions, and they both have distinct, nonoverlapping, possible types.
       // 
-      Function *DF = cast<Function>(V);   // DestFunction
+      DF = cast_or_null<Function>(ST->lookup(SF->getType(), SF->getName()));
 
+    if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
+      // Function does not already exist, simply insert an external function
+      // signature identical to SF into the dest module...
+      Function *DF = new Function(SF->getFunctionType(), SF->getLinkage(),
+                                  SF->getName(), Dest);
+
+      // ... and remember this mapping...
+      ValueMap.insert(std::make_pair(SF, DF));
+    } else if (SF->getLinkage() != DF->getLinkage()) {
+      return Error(Err, "Functions named '" + SF->getName() +
+                   "' have different linkage specifiers!");
+    } else if (SF->getLinkage() == GlobalValue::AppendingLinkage) {
+      return Error(Err, "Functions named '" + SF->getName() +
+                   "' have appending linkage!");
+    } else if (SF->getLinkage() == GlobalValue::ExternalLinkage) {
+      // If the function has a name, and that name is already in use in the Dest
+      // module, make sure that the name is a compatible function...
+      //
       // Check to make sure the function is not defined in both modules...
       if (!SF->isExternal() && !DF->isExternal())
         return Error(Err, "Function '" + 
                      SF->getFunctionType()->getDescription() + "':\"" + 
                      SF->getName() + "\" - Function is already defined!");
-
+      
       // Otherwise, just remember this mapping...
       ValueMap.insert(std::make_pair(SF, DF));
-    } else {
-      // Function does not already exist, simply insert an external function
-      // signature identical to SF into the dest module...
-      Function *DF = new Function(SF->getFunctionType(),
-                                  SF->hasInternalLinkage(),
-                                  SF->getName());
-
-      // Add the function signature to the dest module...
-      Dest->getFunctionList().push_back(DF);
-
-      // ... and remember this mapping...
+    } else if (SF->getLinkage() == GlobalValue::LinkOnceLinkage) {
+      // Completely ignore the source function.
       ValueMap.insert(std::make_pair(SF, DF));
     }
   }
@@ -391,6 +409,7 @@ static bool LinkFunctionBodies(Module *Dest, const Module *Src,
 
       // DF not external SF external?
       if (!DF->isExternal()) {
+        if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
         if (Err)
           *Err = "Function '" + (SF->hasName() ? SF->getName() :std::string(""))
                + "' body multiply defined!";
index 958980bad678c624631f6606da476430f2cf3b9c..c55a87515c5065cd9b54bc2389f0099ed6d77ad2 100644 (file)
@@ -535,8 +535,15 @@ void AssemblyWriter::printModule(const Module *M) {
 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
   if (GV->hasName()) Out << "%" << GV->getName() << " = ";
 
-  if (GV->hasInternalLinkage()) Out << "internal ";
-  if (!GV->hasInitializer()) Out << "external ";
+  if (!GV->hasInitializer()) 
+    Out << "external ";
+  else
+    switch (GV->getLinkage()) {
+    case GlobalValue::InternalLinkage: Out << "internal "; break;
+    case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
+    case GlobalValue::AppendingLinkage: Out << "appending "; break;
+    case GlobalValue::ExternalLinkage: break;
+    }
 
   Out << (GV->isConstant() ? "constant " : "global ");
   printType(GV->getType()->getElementType());
@@ -594,8 +601,18 @@ void AssemblyWriter::printConstant(const Constant *CPV) {
 //
 void AssemblyWriter::printFunction(const Function *F) {
   // Print out the return type and name...
-  Out << "\n" << (F->isExternal() ? "declare " : "")
-      << (F->hasInternalLinkage() ? "internal " : "");
+  Out << "\n";
+
+  if (F->isExternal())
+    Out << "declare ";
+  else
+    switch (F->getLinkage()) {
+    case GlobalValue::InternalLinkage: Out << "internal "; break;
+    case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
+    case GlobalValue::AppendingLinkage: Out << "appending "; break;
+    case GlobalValue::ExternalLinkage: break;
+    }
+
   printType(F->getReturnType()) << " %" << F->getName() << "(";
   Table.incorporateFunction(F);
 
index 70569c0f67ce7539f0e878921347b0fd5260ee95..3324565654b0f1bd32f61779874967e7ec5e4d9a 100644 (file)
@@ -77,9 +77,9 @@ void Argument::setParent(Function *parent) {
 // Function Implementation
 //===----------------------------------------------------------------------===//
 
-Function::Function(const FunctionType *Ty, bool isInternal,
+Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
                    const std::string &name, Module *ParentModule)
-  : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name) {
+  : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
   BasicBlocks.setItemParent(this);
   BasicBlocks.setParent(this);
   ArgumentList.setItemParent(this);
@@ -154,10 +154,10 @@ void Function::dropAllReferences() {
 // GlobalVariable Implementation
 //===----------------------------------------------------------------------===//
 
-GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
+GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
                               Constant *Initializer,
                               const std::string &Name, Module *ParentModule)
-  : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
+  : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
     isConstantGlobal(constant) {
   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
 
index 60c6e03ae128b2b6a8348c617702b5ba319e105d..b5207a8542926b01b5b3fe63b375d898834b938a 100644 (file)
@@ -189,42 +189,48 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
   //
   for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
     const GlobalVariable *SGV = I;
-    Value *V;
-
-    // If the global variable has a name, and that name is already in use in the
-    // Dest module, make sure that the name is a compatible global variable...
-    //
-    if (SGV->hasExternalLinkage() && SGV->hasName() &&
-       (V = ST->lookup(SGV->getType(), SGV->getName())) &&
-       cast<GlobalVariable>(V)->hasExternalLinkage()) {
-      // The same named thing is a global variable, because the only two things
+    GlobalVariable *DGV = 0;
+    if (SGV->hasName()) {
+      // A same named thing is a global variable, because the only two things
       // that may be in a module level symbol table are Global Vars and
       // Functions, and they both have distinct, nonoverlapping, possible types.
       // 
-      GlobalVariable *DGV = cast<GlobalVariable>(V);
+      DGV = cast_or_null<GlobalVariable>(ST->lookup(SGV->getType(),
+                                                    SGV->getName()));
+    }
 
-      // Check to see if the two GV's have the same Const'ness...
-      if (SGV->isConstant() != DGV->isConstant())
-        return Error(Err, "Global Variable Collision on '" + 
-                     SGV->getType()->getDescription() + "':%" + SGV->getName() +
-                     " - Global variables differ in const'ness");
+    assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
+           "Global must either be external or have an initializer!");
 
-      // Okay, everything is cool, remember the mapping...
-      ValueMap.insert(std::make_pair(SGV, DGV));
-    } else {
+    if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
       // No linking to be performed, simply create an identical version of the
       // symbol over in the dest module... the initializer will be filled in
       // later by LinkGlobalInits...
       //
-      GlobalVariable *DGV = 
-        new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
-                           SGV->hasInternalLinkage(), 0, SGV->getName());
-
-      // Add the new global to the dest module
-      Dest->getGlobalList().push_back(DGV);
+      DGV = new GlobalVariable(SGV->getType()->getElementType(),
+                               SGV->isConstant(), SGV->getLinkage(), /*init*/0,
+                               SGV->getName(), Dest);
 
       // Make sure to remember this mapping...
       ValueMap.insert(std::make_pair(SGV, DGV));
+    } else if (SGV->getLinkage() != DGV->getLinkage()) {
+      return Error(Err, "Global variables named '" + SGV->getName() +
+                   "' have different linkage specifiers!");
+    } else if (SGV->hasExternalLinkage() || SGV->hasLinkOnceLinkage() ||
+               SGV->hasAppendingLinkage()) {
+      // If the global variable has a name, and that name is already in use in
+      // the Dest module, make sure that the name is a compatible global
+      // variable...
+      //
+      // Check to see if the two GV's have the same Const'ness...
+      if (SGV->isConstant() != DGV->isConstant())
+        return Error(Err, "Global Variable Collision on '" + 
+                     SGV->getType()->getDescription() + "':%" + SGV->getName() +
+                     " - Global variables differ in const'ness");
+      // Okay, everything is cool, remember the mapping...
+      ValueMap.insert(std::make_pair(SGV, DGV));
+    } else {
+      assert(0 && "Unknown linkage!");
     }
   }
   return false;
@@ -245,19 +251,28 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
 
     if (SGV->hasInitializer()) {      // Only process initialized GV's
       // Figure out what the initializer looks like in the dest module...
-      Constant *DInit =
+      Constant *SInit =
         cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0));
 
       GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);    
-      if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
-         DGV->hasExternalLinkage()) {
-        if (DGV->getInitializer() != DInit)
-          return Error(Err, "Global Variable Collision on '" + 
-                       SGV->getType()->getDescription() + "':%" +SGV->getName()+
-                       " - Global variables have different initializers");
+      if (DGV->hasInitializer()) {
+        assert(SGV->getLinkage() == DGV->getLinkage());
+        if (SGV->hasExternalLinkage()) {
+          if (DGV->getInitializer() != SInit)
+            return Error(Err, "Global Variable Collision on '" + 
+                         SGV->getType()->getDescription() +"':%"+SGV->getName()+
+                         " - Global variables have different initializers");
+        } else if (DGV->hasLinkOnceLinkage()) {
+          // Nothing is required, mapped values will take the new global
+          // automatically.
+        } else if (DGV->hasAppendingLinkage()) {
+          assert(0 && "Appending linkage unimplemented!");
+        } else {
+          assert(0 && "Unknown linkage!");
+        }
       } else {
         // Copy the initializer over now...
-        DGV->setInitializer(DInit);
+        DGV->setInitializer(SInit);
       }
     }
   }
@@ -278,39 +293,42 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
   //
   for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
     const Function *SF = I;   // SrcFunction
-    Value *V;
-
-    // If the function has a name, and that name is already in use in the Dest
-    // module, make sure that the name is a compatible function...
-    //
-    if (SF->hasExternalLinkage() && SF->hasName() &&
-       (V = ST->lookup(SF->getType(), SF->getName())) &&
-       cast<Function>(V)->hasExternalLinkage()) {
+    Function *DF = 0;
+    if (SF->hasName())
       // The same named thing is a Function, because the only two things
       // that may be in a module level symbol table are Global Vars and
       // Functions, and they both have distinct, nonoverlapping, possible types.
       // 
-      Function *DF = cast<Function>(V);   // DestFunction
+      DF = cast_or_null<Function>(ST->lookup(SF->getType(), SF->getName()));
 
+    if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
+      // Function does not already exist, simply insert an external function
+      // signature identical to SF into the dest module...
+      Function *DF = new Function(SF->getFunctionType(), SF->getLinkage(),
+                                  SF->getName(), Dest);
+
+      // ... and remember this mapping...
+      ValueMap.insert(std::make_pair(SF, DF));
+    } else if (SF->getLinkage() != DF->getLinkage()) {
+      return Error(Err, "Functions named '" + SF->getName() +
+                   "' have different linkage specifiers!");
+    } else if (SF->getLinkage() == GlobalValue::AppendingLinkage) {
+      return Error(Err, "Functions named '" + SF->getName() +
+                   "' have appending linkage!");
+    } else if (SF->getLinkage() == GlobalValue::ExternalLinkage) {
+      // If the function has a name, and that name is already in use in the Dest
+      // module, make sure that the name is a compatible function...
+      //
       // Check to make sure the function is not defined in both modules...
       if (!SF->isExternal() && !DF->isExternal())
         return Error(Err, "Function '" + 
                      SF->getFunctionType()->getDescription() + "':\"" + 
                      SF->getName() + "\" - Function is already defined!");
-
+      
       // Otherwise, just remember this mapping...
       ValueMap.insert(std::make_pair(SF, DF));
-    } else {
-      // Function does not already exist, simply insert an external function
-      // signature identical to SF into the dest module...
-      Function *DF = new Function(SF->getFunctionType(),
-                                  SF->hasInternalLinkage(),
-                                  SF->getName());
-
-      // Add the function signature to the dest module...
-      Dest->getFunctionList().push_back(DF);
-
-      // ... and remember this mapping...
+    } else if (SF->getLinkage() == GlobalValue::LinkOnceLinkage) {
+      // Completely ignore the source function.
       ValueMap.insert(std::make_pair(SF, DF));
     }
   }
@@ -391,6 +409,7 @@ static bool LinkFunctionBodies(Module *Dest, const Module *Src,
 
       // DF not external SF external?
       if (!DF->isExternal()) {
+        if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
         if (Err)
           *Err = "Function '" + (SF->hasName() ? SF->getName() :std::string(""))
                + "' body multiply defined!";
index f54a6f390be7d61c3abd5a18780a9cc6362ff29c..e0a6fb271ceabbe5d95959cc7d7b8c1ab49cb5a0 100644 (file)
 Function *ilist_traits<Function>::createNode() {
   FunctionType *FTy =
     FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
-  Function *Ret = new Function(FTy, false);
+  Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
   // This should not be garbage monitored.
   LeakDetector::removeGarbageObject(Ret);
   return Ret;
 }
 GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
-  GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false, false);
+  GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
+                                           GlobalValue::ExternalLinkage);
   // This should not be garbage monitored.
   LeakDetector::removeGarbageObject(Ret);
   return Ret;
@@ -87,7 +88,7 @@ Function *Module::getOrInsertFunction(const std::string &Name,
   if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
     return cast<Function>(V);      // Yup, got it
   } else {                         // Nope, add one
-    Function *New = new Function(Ty, false, Name);
+    Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
     FunctionList.push_back(New);
     return New;                    // Return the new prototype...
   }