IR: Introduce local_unnamed_addr attribute.
If a local_unnamed_addr attribute is attached to a global, the address
is known to be insignificant within the module. It is distinct from the
existing unnamed_addr attribute in that it only describes a local property
of the module rather than a global property of the symbol.
This attribute is intended to be used by the code generator and LTO to allow
the linker to decide whether the global needs to be in the symbol table. It is
possible to exclude a global from the symbol table if three things are true:
- This attribute is present on every instance of the global (which means that
the normal rule that the global must have a unique address can be broken without
being observable by the program by performing comparisons against the global's
address)
- The global has linkonce_odr linkage (which means that each linkage unit must have
its own copy of the global if it requires one, and the copy in each linkage unit
must be the same)
- It is a constant or a function (which means that the program cannot observe that
the unique-address rule has been broken by writing to the global)
Although this attribute could in principle be computed from the module
contents, LTO clients (i.e. linkers) will normally need to be able to compute
this property as part of symbol resolution, and it would be inefficient to
materialize every module just to compute it.
See:
http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20160509/356401.html
http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20160516/356738.html
for earlier discussion.
Part of the fix for PR27553.
Differential Revision: http://reviews.llvm.org/D20348
llvm-svn: 272709
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index cc7b98f..2725386 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -429,6 +429,17 @@
return false;
}
+bool LLParser::ParseOptionalUnnamedAddr(
+ GlobalVariable::UnnamedAddr &UnnamedAddr) {
+ if (EatIfPresent(lltok::kw_unnamed_addr))
+ UnnamedAddr = GlobalValue::UnnamedAddr::Global;
+ else if (EatIfPresent(lltok::kw_local_unnamed_addr))
+ UnnamedAddr = GlobalValue::UnnamedAddr::Local;
+ else
+ UnnamedAddr = GlobalValue::UnnamedAddr::None;
+ return false;
+}
+
/// ParseUnnamedGlobal:
/// OptionalVisibility (ALIAS | IFUNC) ...
/// OptionalLinkage OptionalVisibility OptionalDLLStorageClass
@@ -455,9 +466,9 @@
bool HasLinkage;
unsigned Linkage, Visibility, DLLStorageClass;
GlobalVariable::ThreadLocalMode TLM;
- bool UnnamedAddr;
+ GlobalVariable::UnnamedAddr UnnamedAddr;
if (ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass) ||
- ParseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr))
+ ParseOptionalThreadLocal(TLM) || ParseOptionalUnnamedAddr(UnnamedAddr))
return true;
if (Lex.getKind() != lltok::kw_alias && Lex.getKind() != lltok::kw_ifunc)
@@ -481,10 +492,10 @@
bool HasLinkage;
unsigned Linkage, Visibility, DLLStorageClass;
GlobalVariable::ThreadLocalMode TLM;
- bool UnnamedAddr;
+ GlobalVariable::UnnamedAddr UnnamedAddr;
if (ParseToken(lltok::equal, "expected '=' in global variable") ||
ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass) ||
- ParseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr))
+ ParseOptionalThreadLocal(TLM) || ParseOptionalUnnamedAddr(UnnamedAddr))
return true;
if (Lex.getKind() != lltok::kw_alias && Lex.getKind() != lltok::kw_ifunc)
@@ -659,11 +670,10 @@
///
/// Everything through OptionalUnnamedAddr has already been parsed.
///
-bool LLParser::parseIndirectSymbol(const std::string &Name, LocTy NameLoc,
- unsigned L, unsigned Visibility,
- unsigned DLLStorageClass,
- GlobalVariable::ThreadLocalMode TLM,
- bool UnnamedAddr) {
+bool LLParser::parseIndirectSymbol(
+ const std::string &Name, LocTy NameLoc, unsigned L, unsigned Visibility,
+ unsigned DLLStorageClass, GlobalVariable::ThreadLocalMode TLM,
+ GlobalVariable::UnnamedAddr UnnamedAddr) {
bool IsAlias;
if (Lex.getKind() == lltok::kw_alias)
IsAlias = true;
@@ -799,7 +809,7 @@
unsigned Linkage, bool HasLinkage,
unsigned Visibility, unsigned DLLStorageClass,
GlobalVariable::ThreadLocalMode TLM,
- bool UnnamedAddr) {
+ GlobalVariable::UnnamedAddr UnnamedAddr) {
if (!isValidVisibilityForLinkage(Visibility, Linkage))
return Error(NameLoc,
"symbol with local linkage must have default visibility");
@@ -4580,7 +4590,7 @@
std::string Section;
unsigned Alignment;
std::string GC;
- bool UnnamedAddr;
+ GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
LocTy UnnamedAddrLoc;
Constant *Prefix = nullptr;
Constant *Prologue = nullptr;
@@ -4588,8 +4598,7 @@
Comdat *C;
if (ParseArgumentList(ArgList, isVarArg) ||
- ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
- &UnnamedAddrLoc) ||
+ ParseOptionalUnnamedAddr(UnnamedAddr) ||
ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
BuiltinLoc) ||
(EatIfPresent(lltok::kw_section) &&