Skip to content

Commit 45e6c19

Browse files
committed
First step in fixing PR8927:
Add a unnamed_addr bit to global variables and functions. This will be used to indicate that the address is not significant and therefore the constant or function can be merged with others. If an optimization pass can show that an address is not used, it can set this. Examples of things that can have this set by the FE are globals created to hold string literals and C++ constructors. Adding unnamed_addr to a non-const global should have no effect unless an optimization can transform that global into a constant. Aliases are not allowed to have unnamed_addr since I couldn't figure out any use for it. llvm-svn: 123063
1 parent 80bd9af commit 45e6c19

File tree

12 files changed

+98
-13
lines changed

12 files changed

+98
-13
lines changed

llvm/docs/BitCodeFormat.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,9 @@
922922
<li><i>threadlocal</i>: If present and non-zero, indicates that the variable
923923
is <tt>thread_local</tt></li>
924924

925+
<li><i>unnamed_addr</i>: If present and non-zero, indicates that the variable
926+
has <tt>unnamed_addr<tt></li>
927+
925928
</ul>
926929
</div>
927930

@@ -975,6 +978,10 @@
975978
<li><i>gc</i>: If present and nonzero, the 1-based garbage collector
976979
index in the table of
977980
<a href="#MODULE_CODE_GCNAME">MODULE_CODE_GCNAME</a> entries.</li>
981+
982+
<li><i>unnamed_addr</i>: If present and non-zero, indicates that the function
983+
has <tt>unnamed_addr<tt></li>
984+
978985
</ul>
979986
</div>
980987

llvm/docs/LangRef.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,10 @@
846846
region of memory, and all memory objects in LLVM are accessed through
847847
pointers.</p>
848848

849+
<p>Global variables can be marked with <tt>unnamed_addr</tt> which indicates
850+
that the address is not significant, only the content. Constants marked
851+
like this can be merged if they have the same content.</p>
852+
849853
<p>A global variable may be declared to reside in a target-specific numbered
850854
address space. For targets that support them, address spaces may affect how
851855
optimizations are performed and/or what target instructions are used to
@@ -885,7 +889,8 @@
885889
<p>LLVM function definitions consist of the "<tt>define</tt>" keyword, an
886890
optional <a href="#linkage">linkage type</a>, an optional
887891
<a href="#visibility">visibility style</a>, an optional
888-
<a href="#callingconv">calling convention</a>, a return type, an optional
892+
<a href="#callingconv">calling convention</a>,
893+
an optional <tt>unnamed_addr</tt> attribute, a return type, an optional
889894
<a href="#paramattrs">parameter attribute</a> for the return type, a function
890895
name, a (possibly empty) argument list (each with optional
891896
<a href="#paramattrs">parameter attributes</a>), optional
@@ -896,7 +901,8 @@
896901
<p>LLVM function declarations consist of the "<tt>declare</tt>" keyword, an
897902
optional <a href="#linkage">linkage type</a>, an optional
898903
<a href="#visibility">visibility style</a>, an optional
899-
<a href="#callingconv">calling convention</a>, a return type, an optional
904+
<a href="#callingconv">calling convention</a>,
905+
an optional <tt>unnamed_addr</tt> attribute, a return type, an optional
900906
<a href="#paramattrs">parameter attribute</a> for the return type, a function
901907
name, a possibly empty list of arguments, an optional alignment, and an
902908
optional <a href="#gc">garbage collector name</a>.</p>
@@ -922,6 +928,9 @@
922928
specified, the function is forced to have at least that much alignment. All
923929
alignments must be a power of 2.</p>
924930

931+
<p>If the <tt>unnamed_addr</tt> attribute is given, the address is know to not
932+
be significant and two identical functions can be merged</p>.
933+
925934
<h5>Syntax:</h5>
926935
<pre class="doc_code">
927936
define [<a href="#linkage">linkage</a>] [<a href="#visibility">visibility</a>]

llvm/include/llvm/GlobalValue.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class GlobalValue : public Constant {
6060
GlobalValue(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
6161
LinkageTypes linkage, const Twine &Name)
6262
: Constant(ty, vty, Ops, NumOps), Parent(0),
63-
Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
63+
Linkage(linkage), Visibility(DefaultVisibility), Alignment(0),
64+
UnnamedAddr(0) {
6465
setName(Name);
6566
}
6667

@@ -70,6 +71,7 @@ class GlobalValue : public Constant {
7071
LinkageTypes Linkage : 5; // The linkage of this global
7172
unsigned Visibility : 2; // The visibility style of this global
7273
unsigned Alignment : 16; // Alignment of this symbol, must be power of two
74+
unsigned UnnamedAddr : 1; // This value's address is not significant
7375
std::string Section; // Section to emit this into, empty mean default
7476
public:
7577
~GlobalValue() {
@@ -81,6 +83,9 @@ class GlobalValue : public Constant {
8183
}
8284
void setAlignment(unsigned Align);
8385

86+
bool hasUnnamedAddr() const { return UnnamedAddr; }
87+
void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
88+
8489
VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
8590
bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
8691
bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ lltok::Kind LLLexer::LexIdentifier() {
509509
KEYWORD(default);
510510
KEYWORD(hidden);
511511
KEYWORD(protected);
512+
KEYWORD(unnamed_addr);
512513
KEYWORD(extern_weak);
513514
KEYWORD(external);
514515
KEYWORD(thread_local);

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ bool LLParser::ParseTopLevelEntities() {
194194
// The Global variable production with no name can have many different
195195
// optional leading prefixes, the production is:
196196
// GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
197-
// OptionalAddrSpace ('constant'|'global') ...
197+
// OptionalAddrSpace OptionalUnNammedAddr
198+
// ('constant'|'global') ...
198199
case lltok::kw_private: // OptionalLinkage
199200
case lltok::kw_linker_private: // OptionalLinkage
200201
case lltok::kw_linker_private_weak: // OptionalLinkage
@@ -682,22 +683,23 @@ bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
682683

683684
/// ParseGlobal
684685
/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalThreadLocal
685-
/// OptionalAddrSpace GlobalType Type Const
686+
/// OptionalAddrSpace OptionalUnNammedAddr GlobalType Type Const
686687
/// ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
687-
/// OptionalAddrSpace GlobalType Type Const
688+
/// OptionalAddrSpace OptionalUnNammedAddr GlobalType Type Const
688689
///
689690
/// Everything through visibility has been parsed already.
690691
///
691692
bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
692693
unsigned Linkage, bool HasLinkage,
693694
unsigned Visibility) {
694695
unsigned AddrSpace;
695-
bool ThreadLocal, IsConstant;
696+
bool ThreadLocal, IsConstant, UnnamedAddr;
696697
LocTy TyLoc;
697698

698699
PATypeHolder Ty(Type::getVoidTy(Context));
699700
if (ParseOptionalToken(lltok::kw_thread_local, ThreadLocal) ||
700701
ParseOptionalAddrSpace(AddrSpace) ||
702+
ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr) ||
701703
ParseGlobalType(IsConstant) ||
702704
ParseType(Ty, TyLoc))
703705
return true;
@@ -755,6 +757,7 @@ bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
755757
GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
756758
GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
757759
GV->setThreadLocal(ThreadLocal);
760+
GV->setUnnamedAddr(UnnamedAddr);
758761

759762
// Parse attributes on the global.
760763
while (Lex.getKind() == lltok::comma) {
@@ -2657,21 +2660,23 @@ bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
26572660

26582661
/// FunctionHeader
26592662
/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
2660-
/// Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
2663+
/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
26612664
/// OptionalAlign OptGC
26622665
bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
26632666
// Parse the linkage.
26642667
LocTy LinkageLoc = Lex.getLoc();
26652668
unsigned Linkage;
26662669

26672670
unsigned Visibility, RetAttrs;
2671+
bool UnnamedAddr;
26682672
CallingConv::ID CC;
26692673
PATypeHolder RetType(Type::getVoidTy(Context));
26702674
LocTy RetTypeLoc = Lex.getLoc();
26712675
if (ParseOptionalLinkage(Linkage) ||
26722676
ParseOptionalVisibility(Visibility) ||
26732677
ParseOptionalCallingConv(CC) ||
26742678
ParseOptionalAttrs(RetAttrs, 1) ||
2679+
ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr) ||
26752680
ParseType(RetType, RetTypeLoc, true /*void allowed*/))
26762681
return true;
26772682

@@ -2841,6 +2846,7 @@ bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
28412846
Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
28422847
Fn->setCallingConv(CC);
28432848
Fn->setAttributes(PAL);
2849+
Fn->setUnnamedAddr(UnnamedAddr);
28442850
Fn->setAlignment(Alignment);
28452851
Fn->setSection(Section);
28462852
if (!GC.empty()) Fn->setGC(GC.c_str());

llvm/lib/AsmParser/LLToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace lltok {
4242
kw_linkonce, kw_linkonce_odr, kw_weak, kw_weak_odr, kw_appending,
4343
kw_dllimport, kw_dllexport, kw_common, kw_available_externally,
4444
kw_default, kw_hidden, kw_protected,
45+
kw_unnamed_addr,
4546
kw_extern_weak,
4647
kw_external, kw_thread_local,
4748
kw_zeroinitializer,

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,8 @@ bool BitcodeReader::ParseModule() {
14221422
break;
14231423
}
14241424
// GLOBALVAR: [pointer type, isconst, initid,
1425-
// linkage, alignment, section, visibility, threadlocal]
1425+
// linkage, alignment, section, visibility, threadlocal,
1426+
// unnamed_addr]
14261427
case bitc::MODULE_CODE_GLOBALVAR: {
14271428
if (Record.size() < 6)
14281429
return Error("Invalid MODULE_CODE_GLOBALVAR record");
@@ -1449,6 +1450,10 @@ bool BitcodeReader::ParseModule() {
14491450
if (Record.size() > 7)
14501451
isThreadLocal = Record[7];
14511452

1453+
bool UnnamedAddr = false;
1454+
if (Record.size() > 8)
1455+
UnnamedAddr = Record[8];
1456+
14521457
GlobalVariable *NewGV =
14531458
new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0,
14541459
isThreadLocal, AddressSpace);
@@ -1457,6 +1462,7 @@ bool BitcodeReader::ParseModule() {
14571462
NewGV->setSection(Section);
14581463
NewGV->setVisibility(Visibility);
14591464
NewGV->setThreadLocal(isThreadLocal);
1465+
NewGV->setUnnamedAddr(UnnamedAddr);
14601466

14611467
ValueList.push_back(NewGV);
14621468

@@ -1466,7 +1472,7 @@ bool BitcodeReader::ParseModule() {
14661472
break;
14671473
}
14681474
// FUNCTION: [type, callingconv, isproto, linkage, paramattr,
1469-
// alignment, section, visibility, gc]
1475+
// alignment, section, visibility, gc, unnamed_addr]
14701476
case bitc::MODULE_CODE_FUNCTION: {
14711477
if (Record.size() < 8)
14721478
return Error("Invalid MODULE_CODE_FUNCTION record");
@@ -1499,6 +1505,10 @@ bool BitcodeReader::ParseModule() {
14991505
return Error("Invalid GC ID");
15001506
Func->setGC(GCTable[Record[8]-1].c_str());
15011507
}
1508+
bool UnnamedAddr = false;
1509+
if (Record.size() > 9)
1510+
UnnamedAddr = Record[9];
1511+
Func->setUnnamedAddr(UnnamedAddr);
15021512
ValueList.push_back(Func);
15031513

15041514
// If this is a function with a body, remember the prototype we are

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,8 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
404404
unsigned AbbrevToUse = 0;
405405

406406
// GLOBALVAR: [type, isconst, initid,
407-
// linkage, alignment, section, visibility, threadlocal]
407+
// linkage, alignment, section, visibility, threadlocal,
408+
// unnamed_addr]
408409
Vals.push_back(VE.getTypeID(GV->getType()));
409410
Vals.push_back(GV->isConstant());
410411
Vals.push_back(GV->isDeclaration() ? 0 :
@@ -413,9 +414,11 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
413414
Vals.push_back(Log2_32(GV->getAlignment())+1);
414415
Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
415416
if (GV->isThreadLocal() ||
416-
GV->getVisibility() != GlobalValue::DefaultVisibility) {
417+
GV->getVisibility() != GlobalValue::DefaultVisibility ||
418+
GV->hasUnnamedAddr()) {
417419
Vals.push_back(getEncodedVisibility(GV));
418420
Vals.push_back(GV->isThreadLocal());
421+
Vals.push_back(GV->hasUnnamedAddr());
419422
} else {
420423
AbbrevToUse = SimpleGVarAbbrev;
421424
}
@@ -427,7 +430,7 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
427430
// Emit the function proto information.
428431
for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
429432
// FUNCTION: [type, callingconv, isproto, paramattr,
430-
// linkage, alignment, section, visibility, gc]
433+
// linkage, alignment, section, visibility, gc, unnamed_addr]
431434
Vals.push_back(VE.getTypeID(F->getType()));
432435
Vals.push_back(F->getCallingConv());
433436
Vals.push_back(F->isDeclaration());
@@ -437,6 +440,7 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
437440
Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
438441
Vals.push_back(getEncodedVisibility(F));
439442
Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0);
443+
Vals.push_back(F->hasUnnamedAddr());
440444

441445
unsigned AbbrevToUse = 0;
442446
Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);

llvm/lib/VMCore/AsmWriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,7 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
14591459
if (GV->isThreadLocal()) Out << "thread_local ";
14601460
if (unsigned AddressSpace = GV->getType()->getAddressSpace())
14611461
Out << "addrspace(" << AddressSpace << ") ";
1462+
if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
14621463
Out << (GV->isConstant() ? "constant " : "global ");
14631464
TypePrinter.print(GV->getType()->getElementType(), Out);
14641465

@@ -1589,6 +1590,8 @@ void AssemblyWriter::printFunction(const Function *F) {
15891590
Attributes RetAttrs = Attrs.getRetAttributes();
15901591
if (RetAttrs != Attribute::None)
15911592
Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
1593+
if (F->hasUnnamedAddr())
1594+
Out << "unnamed_addr ";
15921595
TypePrinter.print(F->getReturnType(), Out);
15931596
Out << ' ';
15941597
WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());

llvm/lib/VMCore/Verifier.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ void Verifier::visitGlobalAlias(GlobalAlias &GA) {
484484
"Aliasee cannot be NULL!", &GA);
485485
Assert1(GA.getType() == GA.getAliasee()->getType(),
486486
"Alias and aliasee types should match!", &GA);
487+
Assert1(!GA.hasUnnamedAddr(), "Alias cannot have unnamed_addr!", &GA);
487488

488489
if (!isa<GlobalValue>(GA.getAliasee())) {
489490
const ConstantExpr *CE = dyn_cast<ConstantExpr>(GA.getAliasee());

llvm/test/Assembler/unnamed-addr.ll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: llvm-as < %s | llvm-dis | FileCheck %s
2+
3+
%struct.foobar = type { i32 }
4+
5+
@bar.d = internal unnamed_addr constant %struct.foobar zeroinitializer, align 4
6+
@foo.d = internal constant %struct.foobar zeroinitializer, align 4
7+
8+
define unnamed_addr i32 @main() nounwind ssp {
9+
entry:
10+
%call2 = tail call i32 @zed(%struct.foobar* @foo.d, %struct.foobar* @bar.d) nounwind
11+
ret i32 0
12+
}
13+
14+
declare i32 @zed(%struct.foobar*, %struct.foobar*)
15+
16+
; CHECK: @bar.d = internal unnamed_addr constant %struct.foobar zeroinitializer, align 4
17+
; CHECK: @foo.d = internal constant %struct.foobar zeroinitializer, align 4
18+
; CHECK: define unnamed_addr i32 @main() nounwind ssp {

llvm/unittests/VMCore/VerifierTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
#include "llvm/Constants.h"
1111
#include "llvm/DerivedTypes.h"
1212
#include "llvm/Function.h"
13+
#include "llvm/GlobalAlias.h"
14+
#include "llvm/GlobalVariable.h"
1315
#include "llvm/Instructions.h"
1416
#include "llvm/LLVMContext.h"
17+
#include "llvm/Module.h"
1518
#include "llvm/ADT/OwningPtr.h"
1619
#include "llvm/Analysis/Verifier.h"
1720
#include "gtest/gtest.h"
@@ -41,5 +44,22 @@ TEST(VerifierTest, Branch_i1) {
4144
EXPECT_TRUE(verifyFunction(*F, ReturnStatusAction));
4245
}
4346

47+
TEST(VerifierTest, AliasUnnamedAddr) {
48+
LLVMContext &C = getGlobalContext();
49+
Module M("M", C);
50+
const Type *Ty = Type::getInt8Ty(C);
51+
Constant *Init = Constant::getNullValue(Ty);
52+
GlobalVariable *Aliasee = new GlobalVariable(M, Ty, true,
53+
GlobalValue::ExternalLinkage,
54+
Init, "foo");
55+
GlobalAlias *GA = new GlobalAlias(Type::getInt8PtrTy(C),
56+
GlobalValue::ExternalLinkage,
57+
"bar", Aliasee, &M);
58+
GA->setUnnamedAddr(true);
59+
std::string Error;
60+
EXPECT_TRUE(verifyModule(M, ReturnStatusAction, &Error));
61+
EXPECT_TRUE(StringRef(Error).startswith("Alias cannot have unnamed_addr"));
62+
}
63+
4464
}
4565
}

0 commit comments

Comments
 (0)