llvm.org GIT mirror llvm / bea4626
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123063 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 9 years ago
12 changed file(s) with 98 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
921921
  • threadlocal: If present and non-zero, indicates that the variable
  • 922922 is thread_local
    923923
    924
  • unnamed_addr: If present and non-zero, indicates that the variable
  • 925 has unnamed_addr
    926
    924927
    925928
    926929
    974977
  • gc: If present and nonzero, the 1-based garbage collector
  • 975978 index in the table of
    976979 MODULE_CODE_GCNAME entries.
    980
    981
  • unnamed_addr: If present and non-zero, indicates that the function
  • 982 has unnamed_addr
    983
    977984
    978985
    979986
    845845 region of memory, and all memory objects in LLVM are accessed through
    846846 pointers.

    847847
    848

    Global variables can be marked with unnamed_addr which indicates

    849 that the address is not significant, only the content. Constants marked
    850 like this can be merged if they have the same content.

    851
    848852

    A global variable may be declared to reside in a target-specific numbered

    849853 address space. For targets that support them, address spaces may affect how
    850854 optimizations are performed and/or what target instructions are used to
    884888

    LLVM function definitions consist of the "define" keyword, an

    885889 optional linkage type, an optional
    886890 visibility style, an optional
    887 calling convention, a return type, an optional
    891 calling convention,
    892 an optional unnamed_addr attribute, a return type, an optional
    888893 parameter attribute for the return type, a function
    889894 name, a (possibly empty) argument list (each with optional
    890895 parameter attributes), optional
    895900

    LLVM function declarations consist of the "declare" keyword, an

    896901 optional linkage type, an optional
    897902 visibility style, an optional
    898 calling convention, a return type, an optional
    903 calling convention,
    904 an optional unnamed_addr attribute, a return type, an optional
    899905 parameter attribute for the return type, a function
    900906 name, a possibly empty list of arguments, an optional alignment, and an
    901907 optional garbage collector name.

    920926 target to whatever it feels convenient. If an explicit alignment is
    921927 specified, the function is forced to have at least that much alignment. All
    922928 alignments must be a power of 2.

    929
    930

    If the unnamed_addr attribute is given, the address is know to not

    931 be significant and two identical functions can be merged

    .
    923932
    924933
    Syntax:
    925934
    
                      
                    
    5959 GlobalValue(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
    6060 LinkageTypes linkage, const Twine &Name)
    6161 : Constant(ty, vty, Ops, NumOps), Parent(0),
    62 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
    62 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0),
    63 UnnamedAddr(0) {
    6364 setName(Name);
    6465 }
    6566
    6970 LinkageTypes Linkage : 5; // The linkage of this global
    7071 unsigned Visibility : 2; // The visibility style of this global
    7172 unsigned Alignment : 16; // Alignment of this symbol, must be power of two
    73 unsigned UnnamedAddr : 1; // This value's address is not significant
    7274 std::string Section; // Section to emit this into, empty mean default
    7375 public:
    7476 ~GlobalValue() {
    7981 return (1u << Alignment) >> 1;
    8082 }
    8183 void setAlignment(unsigned Align);
    84
    85 bool hasUnnamedAddr() const { return UnnamedAddr; }
    86 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
    8287
    8388 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
    8489 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
    508508 KEYWORD(default);
    509509 KEYWORD(hidden);
    510510 KEYWORD(protected);
    511 KEYWORD(unnamed_addr);
    511512 KEYWORD(extern_weak);
    512513 KEYWORD(external);
    513514 KEYWORD(thread_local);
    193193 // The Global variable production with no name can have many different
    194194 // optional leading prefixes, the production is:
    195195 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
    196 // OptionalAddrSpace ('constant'|'global') ...
    196 // OptionalAddrSpace OptionalUnNammedAddr
    197 // ('constant'|'global') ...
    197198 case lltok::kw_private: // OptionalLinkage
    198199 case lltok::kw_linker_private: // OptionalLinkage
    199200 case lltok::kw_linker_private_weak: // OptionalLinkage
    681682
    682683 /// ParseGlobal
    683684 /// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalThreadLocal
    684 /// OptionalAddrSpace GlobalType Type Const
    685 /// OptionalAddrSpace OptionalUnNammedAddr GlobalType Type Const
    685686 /// ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
    686 /// OptionalAddrSpace GlobalType Type Const
    687 /// OptionalAddrSpace OptionalUnNammedAddr GlobalType Type Const
    687688 ///
    688689 /// Everything through visibility has been parsed already.
    689690 ///
    691692 unsigned Linkage, bool HasLinkage,
    692693 unsigned Visibility) {
    693694 unsigned AddrSpace;
    694 bool ThreadLocal, IsConstant;
    695 bool ThreadLocal, IsConstant, UnnamedAddr;
    695696 LocTy TyLoc;
    696697
    697698 PATypeHolder Ty(Type::getVoidTy(Context));
    698699 if (ParseOptionalToken(lltok::kw_thread_local, ThreadLocal) ||
    699700 ParseOptionalAddrSpace(AddrSpace) ||
    701 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr) ||
    700702 ParseGlobalType(IsConstant) ||
    701703 ParseType(Ty, TyLoc))
    702704 return true;
    754756 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
    755757 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
    756758 GV->setThreadLocal(ThreadLocal);
    759 GV->setUnnamedAddr(UnnamedAddr);
    757760
    758761 // Parse attributes on the global.
    759762 while (Lex.getKind() == lltok::comma) {
    26562659
    26572660 /// FunctionHeader
    26582661 /// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
    2659 /// Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
    2662 /// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
    26602663 /// OptionalAlign OptGC
    26612664 bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
    26622665 // Parse the linkage.
    26642667 unsigned Linkage;
    26652668
    26662669 unsigned Visibility, RetAttrs;
    2670 bool UnnamedAddr;
    26672671 CallingConv::ID CC;
    26682672 PATypeHolder RetType(Type::getVoidTy(Context));
    26692673 LocTy RetTypeLoc = Lex.getLoc();
    26712675 ParseOptionalVisibility(Visibility) ||
    26722676 ParseOptionalCallingConv(CC) ||
    26732677 ParseOptionalAttrs(RetAttrs, 1) ||
    2678 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr) ||
    26742679 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
    26752680 return true;
    26762681
    28402845 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
    28412846 Fn->setCallingConv(CC);
    28422847 Fn->setAttributes(PAL);
    2848 Fn->setUnnamedAddr(UnnamedAddr);
    28432849 Fn->setAlignment(Alignment);
    28442850 Fn->setSection(Section);
    28452851 if (!GC.empty()) Fn->setGC(GC.c_str());
    4141 kw_linkonce, kw_linkonce_odr, kw_weak, kw_weak_odr, kw_appending,
    4242 kw_dllimport, kw_dllexport, kw_common, kw_available_externally,
    4343 kw_default, kw_hidden, kw_protected,
    44 kw_unnamed_addr,
    4445 kw_extern_weak,
    4546 kw_external, kw_thread_local,
    4647 kw_zeroinitializer,
    14211421 break;
    14221422 }
    14231423 // GLOBALVAR: [pointer type, isconst, initid,
    1424 // linkage, alignment, section, visibility, threadlocal]
    1424 // linkage, alignment, section, visibility, threadlocal,
    1425 // unnamed_addr]
    14251426 case bitc::MODULE_CODE_GLOBALVAR: {
    14261427 if (Record.size() < 6)
    14271428 return Error("Invalid MODULE_CODE_GLOBALVAR record");
    14481449 if (Record.size() > 7)
    14491450 isThreadLocal = Record[7];
    14501451
    1452 bool UnnamedAddr = false;
    1453 if (Record.size() > 8)
    1454 UnnamedAddr = Record[8];
    1455
    14511456 GlobalVariable *NewGV =
    14521457 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0,
    14531458 isThreadLocal, AddressSpace);
    14561461 NewGV->setSection(Section);
    14571462 NewGV->setVisibility(Visibility);
    14581463 NewGV->setThreadLocal(isThreadLocal);
    1464 NewGV->setUnnamedAddr(UnnamedAddr);
    14591465
    14601466 ValueList.push_back(NewGV);
    14611467
    14651471 break;
    14661472 }
    14671473 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
    1468 // alignment, section, visibility, gc]
    1474 // alignment, section, visibility, gc, unnamed_addr]
    14691475 case bitc::MODULE_CODE_FUNCTION: {
    14701476 if (Record.size() < 8)
    14711477 return Error("Invalid MODULE_CODE_FUNCTION record");
    14981504 return Error("Invalid GC ID");
    14991505 Func->setGC(GCTable[Record[8]-1].c_str());
    15001506 }
    1507 bool UnnamedAddr = false;
    1508 if (Record.size() > 9)
    1509 UnnamedAddr = Record[9];
    1510 Func->setUnnamedAddr(UnnamedAddr);
    15011511 ValueList.push_back(Func);
    15021512
    15031513 // If this is a function with a body, remember the prototype we are
    403403 unsigned AbbrevToUse = 0;
    404404
    405405 // GLOBALVAR: [type, isconst, initid,
    406 // linkage, alignment, section, visibility, threadlocal]
    406 // linkage, alignment, section, visibility, threadlocal,
    407 // unnamed_addr]
    407408 Vals.push_back(VE.getTypeID(GV->getType()));
    408409 Vals.push_back(GV->isConstant());
    409410 Vals.push_back(GV->isDeclaration() ? 0 :
    412413 Vals.push_back(Log2_32(GV->getAlignment())+1);
    413414 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
    414415 if (GV->isThreadLocal() ||
    415 GV->getVisibility() != GlobalValue::DefaultVisibility) {
    416 GV->getVisibility() != GlobalValue::DefaultVisibility ||
    417 GV->hasUnnamedAddr()) {
    416418 Vals.push_back(getEncodedVisibility(GV));
    417419 Vals.push_back(GV->isThreadLocal());
    420 Vals.push_back(GV->hasUnnamedAddr());
    418421 } else {
    419422 AbbrevToUse = SimpleGVarAbbrev;
    420423 }
    426429 // Emit the function proto information.
    427430 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
    428431 // FUNCTION: [type, callingconv, isproto, paramattr,
    429 // linkage, alignment, section, visibility, gc]
    432 // linkage, alignment, section, visibility, gc, unnamed_addr]
    430433 Vals.push_back(VE.getTypeID(F->getType()));
    431434 Vals.push_back(F->getCallingConv());
    432435 Vals.push_back(F->isDeclaration());
    436439 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
    437440 Vals.push_back(getEncodedVisibility(F));
    438441 Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0);
    442 Vals.push_back(F->hasUnnamedAddr());
    439443
    440444 unsigned AbbrevToUse = 0;
    441445 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
    14581458 if (GV->isThreadLocal()) Out << "thread_local ";
    14591459 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
    14601460 Out << "addrspace(" << AddressSpace << ") ";
    1461 if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
    14611462 Out << (GV->isConstant() ? "constant " : "global ");
    14621463 TypePrinter.print(GV->getType()->getElementType(), Out);
    14631464
    15881589 Attributes RetAttrs = Attrs.getRetAttributes();
    15891590 if (RetAttrs != Attribute::None)
    15901591 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
    1592 if (F->hasUnnamedAddr())
    1593 Out << "unnamed_addr ";
    15911594 TypePrinter.print(F->getReturnType(), Out);
    15921595 Out << ' ';
    15931596 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
    483483 "Aliasee cannot be NULL!", &GA);
    484484 Assert1(GA.getType() == GA.getAliasee()->getType(),
    485485 "Alias and aliasee types should match!", &GA);
    486 Assert1(!GA.hasUnnamedAddr(), "Alias cannot have unnamed_addr!", &GA);
    486487
    487488 if (!isa(GA.getAliasee())) {
    488489 const ConstantExpr *CE = dyn_cast(GA.getAliasee());
    0 ; RUN: llvm-as < %s | llvm-dis | FileCheck %s
    1
    2 %struct.foobar = type { i32 }
    3
    4 @bar.d = internal unnamed_addr constant %struct.foobar zeroinitializer, align 4
    5 @foo.d = internal constant %struct.foobar zeroinitializer, align 4
    6
    7 define unnamed_addr i32 @main() nounwind ssp {
    8 entry:
    9 %call2 = tail call i32 @zed(%struct.foobar* @foo.d, %struct.foobar* @bar.d) nounwind
    10 ret i32 0
    11 }
    12
    13 declare i32 @zed(%struct.foobar*, %struct.foobar*)
    14
    15 ; CHECK: @bar.d = internal unnamed_addr constant %struct.foobar zeroinitializer, align 4
    16 ; CHECK: @foo.d = internal constant %struct.foobar zeroinitializer, align 4
    17 ; CHECK: define unnamed_addr i32 @main() nounwind ssp {
    99 #include "llvm/Constants.h"
    1010 #include "llvm/DerivedTypes.h"
    1111 #include "llvm/Function.h"
    12 #include "llvm/GlobalAlias.h"
    13 #include "llvm/GlobalVariable.h"
    1214 #include "llvm/Instructions.h"
    1315 #include "llvm/LLVMContext.h"
    16 #include "llvm/Module.h"
    1417 #include "llvm/ADT/OwningPtr.h"
    1518 #include "llvm/Analysis/Verifier.h"
    1619 #include "gtest/gtest.h"
    4043 EXPECT_TRUE(verifyFunction(*F, ReturnStatusAction));
    4144 }
    4245
    46 TEST(VerifierTest, AliasUnnamedAddr) {
    47 LLVMContext &C = getGlobalContext();
    48 Module M("M", C);
    49 const Type *Ty = Type::getInt8Ty(C);
    50 Constant *Init = Constant::getNullValue(Ty);
    51 GlobalVariable *Aliasee = new GlobalVariable(M, Ty, true,
    52 GlobalValue::ExternalLinkage,
    53 Init, "foo");
    54 GlobalAlias *GA = new GlobalAlias(Type::getInt8PtrTy(C),
    55 GlobalValue::ExternalLinkage,
    56 "bar", Aliasee, &M);
    57 GA->setUnnamedAddr(true);
    58 std::string Error;
    59 EXPECT_TRUE(verifyModule(M, ReturnStatusAction, &Error));
    60 EXPECT_TRUE(StringRef(Error).startswith("Alias cannot have unnamed_addr"));
    61 }
    62
    4363 }
    4464 }