Introduce new linkage types linkonce_odr, weak_odr, common_odr
and extern_weak_odr.  These are the same as the non-odr versions,
except that they indicate that the global will only be overridden
by an *equivalent* global.  In C, a function with weak linkage can
be overridden by a function which behaves completely differently.
This means that IP passes have to skip weak functions, since any
deductions made from the function definition might be wrong, since
the definition could be replaced by something completely different
at link time.   This is not allowed in C++, thanks to the ODR
(One-Definition-Rule): if a function is replaced by another at
link-time, then the new function must be the same as the original
function.  If a language knows that a function or other global can
only be overridden by an equivalent global, it can give it the
weak_odr linkage type, and the optimizers will understand that it
is alright to make deductions based on the function body.  The
code generators on the other hand map weak and weak_odr linkage
to the same thing.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66339 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/AsmPrinter/DwarfWriter.cpp b/lib/CodeGen/AsmPrinter/DwarfWriter.cpp
index 5c4957a..9181216 100644
--- a/lib/CodeGen/AsmPrinter/DwarfWriter.cpp
+++ b/lib/CodeGen/AsmPrinter/DwarfWriter.cpp
@@ -3135,9 +3135,8 @@
     GlobalVariable *GV = getGlobalVariable(V);
     if (!GV)
       return false;
-    
-    if (GV->getLinkage() != GlobalValue::InternalLinkage
-        && GV->getLinkage() != GlobalValue::LinkOnceLinkage)
+
+    if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
       return false;
 
     DIDescriptor DI(GV);
@@ -3449,8 +3448,10 @@
     }
 
     // If corresponding function is weak definition, this should be too.
-    if ((linkage == Function::WeakLinkage ||
-         linkage == Function::LinkOnceLinkage) &&
+    if ((linkage == Function::WeakAnyLinkage ||
+         linkage == Function::WeakODRLinkage ||
+         linkage == Function::LinkOnceAnyLinkage ||
+         linkage == Function::LinkOnceODRLinkage) &&
         TAI->getWeakDefDirective())
       O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
 
@@ -3461,8 +3462,10 @@
     // unwind info is to be available for non-EH uses.
     if (!EHFrameInfo.hasCalls &&
         !UnwindTablesMandatory &&
-        ((linkage != Function::WeakLinkage &&
-          linkage != Function::LinkOnceLinkage) ||
+        ((linkage != Function::WeakAnyLinkage &&
+          linkage != Function::WeakODRLinkage &&
+          linkage != Function::LinkOnceAnyLinkage &&
+          linkage != Function::LinkOnceODRLinkage) ||
          !TAI->getWeakDefDirective() ||
          TAI->getSupportsWeakOmittedEHFrame()))
     {
diff --git a/lib/CodeGen/ELFWriter.cpp b/lib/CodeGen/ELFWriter.cpp
index 7cca74b..ddc4e35 100644
--- a/lib/CodeGen/ELFWriter.cpp
+++ b/lib/CodeGen/ELFWriter.cpp
@@ -174,8 +174,10 @@
   case GlobalValue::ExternalLinkage:
     FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
     break;
-  case GlobalValue::LinkOnceLinkage:
-  case GlobalValue::WeakLinkage:
+  case GlobalValue::LinkOnceAnyLinkage:
+  case GlobalValue::LinkOnceODRLinkage:
+  case GlobalValue::WeakAnyLinkage:
+  case GlobalValue::WeakODRLinkage:
     FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
     break;
   case GlobalValue::PrivateLinkage:
diff --git a/lib/CodeGen/IntrinsicLowering.cpp b/lib/CodeGen/IntrinsicLowering.cpp
index 5b4de27..56b1736 100644
--- a/lib/CodeGen/IntrinsicLowering.cpp
+++ b/lib/CodeGen/IntrinsicLowering.cpp
@@ -316,7 +316,7 @@
       Name[i] = '_';
   Module* M = F->getParent();
   F = cast<Function>(M->getOrInsertFunction(Name, FT));
-  F->setLinkage(GlobalValue::WeakLinkage);
+  F->setLinkage(GlobalValue::WeakAnyLinkage);
 
   // If we haven't defined the impl function yet, do so now
   if (F->isDeclaration()) {
@@ -490,7 +490,7 @@
       Name[i] = '_';
   Module* M = F->getParent();
   F = cast<Function>(M->getOrInsertFunction(Name, FT));
-  F->setLinkage(GlobalValue::WeakLinkage);
+  F->setLinkage(GlobalValue::WeakAnyLinkage);
 
   // If we haven't defined the impl function yet, do so now
   if (F->isDeclaration()) {
diff --git a/lib/CodeGen/MachOWriter.cpp b/lib/CodeGen/MachOWriter.cpp
index 22c2157..5fe05a4 100644
--- a/lib/CodeGen/MachOWriter.cpp
+++ b/lib/CodeGen/MachOWriter.cpp
@@ -956,9 +956,12 @@
   default:
     assert(0 && "Unexpected linkage type!");
     break;
-  case GlobalValue::WeakLinkage:
-  case GlobalValue::LinkOnceLinkage:
-  case GlobalValue::CommonLinkage:
+  case GlobalValue::WeakAnyLinkage:
+  case GlobalValue::WeakODRLinkage:
+  case GlobalValue::LinkOnceAnyLinkage:
+  case GlobalValue::LinkOnceODRLinkage:
+  case GlobalValue::CommonAnyLinkage:
+  case GlobalValue::CommonODRLinkage:
     assert(!isa<Function>(gv) && "Unexpected linkage type for Function!");
   case GlobalValue::ExternalLinkage:
     GVName = TAI->getGlobalPrefix() + name;
diff --git a/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index 7913c01..a08dcb2 100644
--- a/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -728,7 +728,7 @@
   if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC &&
       GA &&
       !GA->getGlobal()->isDeclaration() &&
-      !GA->getGlobal()->mayBeOverridden())
+      !GA->getGlobal()->isWeakForLinker())
     return true;
 
   // Otherwise assume nothing is safe.
diff --git a/lib/CodeGen/ShadowStackGC.cpp b/lib/CodeGen/ShadowStackGC.cpp
index 5a55760..2402f81 100644
--- a/lib/CodeGen/ShadowStackGC.cpp
+++ b/lib/CodeGen/ShadowStackGC.cpp
@@ -293,12 +293,12 @@
     // If the root chain does not exist, insert a new one with linkonce
     // linkage!
     Head = new GlobalVariable(StackEntryPtrTy, false,
-                              GlobalValue::LinkOnceLinkage,
+                              GlobalValue::LinkOnceAnyLinkage,
                               Constant::getNullValue(StackEntryPtrTy),
                               "llvm_gc_root_chain", &M);
   } else if (Head->hasExternalLinkage() && Head->isDeclaration()) {
     Head->setInitializer(Constant::getNullValue(StackEntryPtrTy));
-    Head->setLinkage(GlobalValue::LinkOnceLinkage);
+    Head->setLinkage(GlobalValue::LinkOnceAnyLinkage);
   }
 
   return true;