llvm.org GIT mirror llvm / 2d21b25
Allow alias to point to an arbitrary ConstantExpr. This patch changes GlobalAlias to point to an arbitrary ConstantExpr and it is up to MC (or the system assembler) to decide if that expression is valid or not. This reduces our ability to diagnose invalid uses and how early we can spot them, but it also lets us do things like @test5 = alias inttoptr(i32 sub (i32 ptrtoint (i32* @test2 to i32), i32 ptrtoint (i32* @bar to i32)) to i32*) An important implication of this patch is that the notion of aliased global doesn't exist any more. The alias has to encode the information needed to access it in its metadata (linkage, visibility, type, etc). Another consequence to notice is that getSection has to return a "const char *". It could return a NullTerminatedStringRef if there was such a thing, but when that was proposed the decision was to just uses "const char*" for that. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@210062 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
42 changed file(s) with 272 addition(s) and 321 deletion(s). Raw diff Collapse all Expand all
678678 Aliases
679679 -------
680680
681 Aliases act as "second name" for the aliasee value (which can be either
682 function, global variable, another alias or bitcast of global value).
681 Aliases, unlike function or variables, don't create any new data. They
682 are just a new symbol and metadata for an existing position.
683
684 Aliases have a name and an aliasee that is either a global value or a
685 constant expression.
686
683687 Aliases may have an optional :ref:`linkage type `, an optional
684 :ref:`visibility style `, and an optional :ref:`DLL storage class
685 `.
688 :ref:`visibility style `, an optional :ref:`DLL storage class
689 ` and an optional :ref:`tls model `.
686690
687691 Syntax::
688692
690694
691695 The linkage must be one of ``private``, ``internal``, ``linkonce``, ``weak``,
692696 ``linkonce_odr``, ``weak_odr``, ``external``. Note that some system linkers
693 might not correctly handle dropping a weak symbol that is aliased by a non-weak
694 alias.
697 might not correctly handle dropping a weak symbol that is aliased.
695698
696699 Alias that are not ``unnamed_addr`` are guaranteed to have the same address as
697700 the aliasee.
698701
699 The aliasee must be a definition.
700
701 Aliases are not allowed to point to aliases with linkages that can be
702 overridden. Since they are only a second name, the possibility of the
703 intermediate alias being overridden cannot be represented in an object file.
702 Since aliases are only a second name, some restrictions apply, of which
703 some can only be checked when producing an object file:
704
705 * The expression defining the aliasee must be computable at assembly
706 time. Since it is just a name, no relocations can be used.
707
708 * No alias in the expression can be weak as the possibility of the
709 intermediate alias being overridden cannot be represented in an
710 object file.
711
712 * No global value in the expression can be a declaration, since that
713 would require a relocation, which is not possible.
704714
705715 .. _namedmetadatastructure:
706716
3333 void setParent(Module *parent);
3434
3535 GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage,
36 const Twine &Name, GlobalObject *Aliasee, Module *Parent);
36 const Twine &Name, Constant *Aliasee, Module *Parent);
3737
3838 public:
3939 // allocate space for exactly one operand
4545 /// the end of the specified module's alias list.
4646 static GlobalAlias *create(Type *Ty, unsigned AddressSpace,
4747 LinkageTypes Linkage, const Twine &Name,
48 GlobalObject *Aliasee, Module *Parent);
48 Constant *Aliasee, Module *Parent);
4949
5050 // Without the Aliasee.
5151 static GlobalAlias *create(Type *Ty, unsigned AddressSpace,
5555 // The module is taken from the Aliasee.
5656 static GlobalAlias *create(Type *Ty, unsigned AddressSpace,
5757 LinkageTypes Linkage, const Twine &Name,
58 GlobalObject *Aliasee);
58 GlobalValue *Aliasee);
5959
6060 // Type, Parent and AddressSpace taken from the Aliasee.
6161 static GlobalAlias *create(LinkageTypes Linkage, const Twine &Name,
62 GlobalObject *Aliasee);
62 GlobalValue *Aliasee);
6363
6464 // Linkage, Type, Parent and AddressSpace taken from the Aliasee.
65 static GlobalAlias *create(const Twine &Name, GlobalObject *Aliasee);
65 static GlobalAlias *create(const Twine &Name, GlobalValue *Aliasee);
6666
6767 /// Provide fast operand accessors
6868 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
7777 ///
7878 void eraseFromParent() override;
7979
80 /// set/getAliasee - These methods retrive and set alias target.
81 void setAliasee(GlobalObject *GO);
82 const GlobalObject *getAliasee() const {
80 /// These methods retrive and set alias target.
81 void setAliasee(Constant *Aliasee);
82 const Constant *getAliasee() const {
8383 return const_cast(this)->getAliasee();
8484 }
85
86 GlobalObject *getAliasee() {
87 return cast_or_null(getOperand(0));
85 Constant *getAliasee() {
86 return getOperand(0);
8887 }
8988
9089 static bool isValidLinkage(LinkageTypes L) {
3939 }
4040 void setAlignment(unsigned Align);
4141
42 bool hasSection() const { return !getSection().empty(); }
43 const std::string &getSection() const { return Section; }
42 bool hasSection() const { return !StringRef(getSection()).empty(); }
43 const char *getSection() const { return Section.c_str(); }
4444 void setSection(StringRef S);
4545
4646 void copyAttributesFrom(const GlobalValue *Src) override;
145145 }
146146 void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; }
147147
148 bool hasSection() const { return !getSection().empty(); }
149 const std::string &getSection() const;
148 bool hasSection() const { return !StringRef(getSection()).empty(); }
149 // It is unfortunate that we have to use "char *" in here since this is
150 // always non NULL, but:
151 // * The C API expects a null terminated string, so we cannot use StringRef.
152 // * The C API expects us to own it, so we cannot use a std:string.
153 // * For GlobalAliases we can fail to find the section and we have to
154 // return "", so we cannot use a "const std::string &".
155 const char *getSection() const;
150156
151157 /// Global values are always pointers.
152158 inline PointerType *getType() const {
673673 return Error(LinkageLoc,
674674 "symbol with local linkage must have default visibility");
675675
676 bool HasAddrSpace = Lex.getKind() == lltok::kw_addrspace;
677 unsigned AddrSpace;
678 LocTy AddrSpaceLoc = Lex.getLoc();
679 if (ParseOptionalAddrSpace(AddrSpace))
680 return true;
681
682 LocTy TyLoc = Lex.getLoc();
683 Type *Ty = nullptr;
684 if (ParseType(Ty))
685 return true;
686
687 bool DifferentType = EatIfPresent(lltok::comma);
688 if (HasAddrSpace && !DifferentType)
689 return Error(AddrSpaceLoc, "A type is required if addrspace is given");
690
691 Type *AliaseeType = nullptr;
692 if (DifferentType) {
693 if (ParseType(AliaseeType))
676 Constant *Aliasee;
677 LocTy AliaseeLoc = Lex.getLoc();
678 if (Lex.getKind() != lltok::kw_bitcast &&
679 Lex.getKind() != lltok::kw_getelementptr &&
680 Lex.getKind() != lltok::kw_addrspacecast &&
681 Lex.getKind() != lltok::kw_inttoptr) {
682 if (ParseGlobalTypeAndValue(Aliasee))
694683 return true;
695684 } else {
696 AliaseeType = Ty;
697 auto *PTy = dyn_cast(Ty);
698 if (!PTy)
699 return Error(TyLoc, "An alias must have pointer type");
700 Ty = PTy->getElementType();
701 AddrSpace = PTy->getAddressSpace();
702 }
703
704 LocTy AliaseeLoc = Lex.getLoc();
705 Constant *C;
706 if (ParseGlobalValue(AliaseeType, C))
707 return true;
708
709 auto *Aliasee = dyn_cast(C);
710 if (!Aliasee)
711 return Error(AliaseeLoc, "Alias must point to function or variable");
712
713 assert(Aliasee->getType()->isPointerTy());
685 // The bitcast dest type is not present, it is implied by the dest type.
686 ValID ID;
687 if (ParseValID(ID))
688 return true;
689 if (ID.Kind != ValID::t_Constant)
690 return Error(AliaseeLoc, "invalid aliasee");
691 Aliasee = ID.ConstantVal;
692 }
693
694 Type *AliaseeType = Aliasee->getType();
695 auto *PTy = dyn_cast(AliaseeType);
696 if (!PTy)
697 return Error(AliaseeLoc, "An alias must have pointer type");
698 Type *Ty = PTy->getElementType();
699 unsigned AddrSpace = PTy->getAddressSpace();
714700
715701 // Okay, create the alias but do not insert it into the module yet.
716702 std::unique_ptr GA(
738724
739725 // If they agree, just RAUW the old value with the alias and remove the
740726 // forward ref info.
741 for (auto *User : Val->users()) {
742 if (auto *GA = dyn_cast(User))
743 return Error(NameLoc, "Alias is pointed by alias " + GA->getName());
744 }
745
746727 Val->replaceAllUsesWith(GA.get());
747728 Val->eraseFromParent();
748729 ForwardRefVals.erase(I);
10931093 return 1ULL << 63;
10941094 }
10951095
1096 // FIXME: Delete this in LLVM 4.0 and just assert that the aliasee is a
1097 // GlobalObject.
1098 static GlobalObject &
1099 getGlobalObjectInExpr(const DenseMap &Map,
1100 Constant &C) {
1101 auto *GO = dyn_cast(&C);
1102 if (GO)
1103 return *GO;
1104
1105 auto *GA = dyn_cast(&C);
1106 if (GA)
1107 return getGlobalObjectInExpr(Map, *Map.find(GA)->second);
1108
1109 auto &CE = cast(C);
1110 assert(CE.getOpcode() == Instruction::BitCast ||
1111 CE.getOpcode() == Instruction::GetElementPtr ||
1112 CE.getOpcode() == Instruction::AddrSpaceCast);
1113 if (CE.getOpcode() == Instruction::GetElementPtr)
1114 assert(cast(CE).hasAllZeroIndices());
1115 return getGlobalObjectInExpr(Map, *CE.getOperand(0));
1116 }
1117
11181096 /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
11191097 /// values and aliases that we can.
11201098 error_code BitcodeReader::ResolveGlobalAndAliasInits() {
11401118 GlobalInitWorklist.pop_back();
11411119 }
11421120
1143 // FIXME: Delete this in LLVM 4.0
1144 // Older versions of llvm could write an alias pointing to another. We cannot
1145 // construct those aliases, so we first collect an alias to aliasee expression
1146 // and then compute the actual aliasee.
1147 DenseMap AliasInit;
1148
11491121 while (!AliasInitWorklist.empty()) {
11501122 unsigned ValID = AliasInitWorklist.back().second;
11511123 if (ValID >= ValueList.size()) {
11521124 AliasInits.push_back(AliasInitWorklist.back());
11531125 } else {
11541126 if (Constant *C = dyn_cast_or_null(ValueList[ValID]))
1155 AliasInit.insert(std::make_pair(AliasInitWorklist.back().first, C));
1127 AliasInitWorklist.back().first->setAliasee(C);
11561128 else
11571129 return Error(ExpectedConstant);
11581130 }
11591131 AliasInitWorklist.pop_back();
1160 }
1161
1162 for (auto &Pair : AliasInit) {
1163 auto &GO = getGlobalObjectInExpr(AliasInit, *Pair.second);
1164 Pair.first->setAliasee(&GO);
11651132 }
11661133
11671134 while (!FunctionPrefixWorklist.empty()) {
869869 OutStreamer.AddBlankLine();
870870 }
871871
872 static const MCExpr *lowerConstant(const Constant *CV, AsmPrinter &AP);
873
872874 bool AsmPrinter::doFinalization(Module &M) {
873875 // Emit global variables.
874876 for (const auto &G : M.globals())
931933 for (const auto &Alias : M.aliases()) {
932934 MCSymbol *Name = getSymbol(&Alias);
933935
934 const GlobalValue *GV = Alias.getAliasee();
935 assert(!GV->isDeclaration());
936 MCSymbol *Target = getSymbol(GV);
937
938936 if (Alias.hasExternalLinkage() || !MAI->getWeakRefDirective())
939937 OutStreamer.EmitSymbolAttribute(Name, MCSA_Global);
940938 else if (Alias.hasWeakLinkage() || Alias.hasLinkOnceLinkage())
946944
947945 // Emit the directives as assignments aka .set:
948946 OutStreamer.EmitAssignment(Name,
949 MCSymbolRefExpr::Create(Target, OutContext));
947 lowerConstant(Alias.getAliasee(), *this));
950948 }
951949 }
952950
12471245 }
12481246
12491247 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
1250 if (GV->getSection() == "llvm.metadata" ||
1248 if (StringRef(GV->getSection()) == "llvm.metadata" ||
12511249 GV->hasAvailableExternallyLinkage())
12521250 return true;
12531251
3131 #include "llvm/IR/DebugInfo.h"
3232 #include "llvm/IR/DerivedTypes.h"
3333 #include "llvm/IR/Module.h"
34 #include "llvm/IR/Operator.h"
3435 #include "llvm/IR/ValueHandle.h"
3536 #include "llvm/IR/ValueMap.h"
3637 #include "llvm/Support/Debug.h"
683684 //===----------------------------------------------------------------------===//
684685 // JITEmitter code.
685686 //
687
688 static GlobalObject *getSimpleAliasee(Constant *C) {
689 C = cast(C->stripPointerCasts());
690 return dyn_cast(C);
691 }
692
686693 void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
687694 bool MayNeedFarStub) {
688695 if (GlobalVariable *GV = dyn_cast(V))
689696 return TheJIT->getOrEmitGlobalVariable(GV);
690697
691 if (GlobalAlias *GA = dyn_cast(V))
692 return TheJIT->getPointerToGlobal(GA->getAliasee());
698 if (GlobalAlias *GA = dyn_cast(V)) {
699 // We can only handle simple cases.
700 if (GlobalValue *GV = getSimpleAliasee(GA->getAliasee()))
701 return TheJIT->getPointerToGlobal(GV);
702 return nullptr;
703 }
693704
694705 // If we have already compiled the function, return a pointer to its body.
695706 Function *F = cast(V);
14931493
14941494 PrintLinkage(GA->getLinkage(), Out);
14951495
1496 PointerType *Ty = GA->getType();
14971496 const Constant *Aliasee = GA->getAliasee();
1498 if (!Aliasee || Ty != Aliasee->getType()) {
1499 if (unsigned AddressSpace = Ty->getAddressSpace())
1500 Out << "addrspace(" << AddressSpace << ") ";
1501 TypePrinter.print(Ty->getElementType(), Out);
1502 Out << ", ";
1503 }
15041497
15051498 if (!Aliasee) {
1499 TypePrinter.print(GA->getType(), Out);
15061500 Out << " <>";
15071501 } else {
15081502 writeOperand(Aliasee, !isa(Aliasee));
12851285 }
12861286
12871287 const char *LLVMGetSection(LLVMValueRef Global) {
1288 return unwrap(Global)->getSection().c_str();
1288 return unwrap(Global)->getSection();
12891289 }
12901290
12911291 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1919 #include "llvm/IR/GlobalVariable.h"
2020 #include "llvm/IR/LeakDetector.h"
2121 #include "llvm/IR/Module.h"
22 #include "llvm/IR/Operator.h"
2223 #include "llvm/Support/ErrorHandling.h"
2324 using namespace llvm;
2425
5758 setDLLStorageClass(Src->getDLLStorageClass());
5859 }
5960
61 const GlobalObject *getBaseObject(const Constant &C) {
62 // FIXME: We should probably return a base + offset pair for non-zero GEPs.
63 return dyn_cast(C.stripPointerCasts());
64 }
65
6066 unsigned GlobalValue::getAlignment() const {
61 if (auto *GA = dyn_cast(this))
62 return GA->getAliasee()->getAlignment();
63
67 if (auto *GA = dyn_cast(this)) {
68 // In general we cannot compute this at the IR level, but we try.
69 if (const GlobalObject *GO = getBaseObject(*GA->getAliasee()))
70 return GO->getAlignment();
71
72 // FIXME: we should also be able to handle:
73 // Alias = Global + Offset
74 // Alias = Absolute
75 return 0;
76 }
6477 return cast(this)->getAlignment();
6578 }
6679
7992 setSection(GV->getSection());
8093 }
8194
82 const std::string &GlobalValue::getSection() const {
83 if (auto *GA = dyn_cast(this))
84 return GA->getAliasee()->getSection();
95 const char *GlobalValue::getSection() const {
96 if (auto *GA = dyn_cast(this)) {
97 // In general we cannot compute this at the IR level, but we try.
98 if (const GlobalObject *GO = getBaseObject(*GA->getAliasee()))
99 return GO->getSection();
100 return "";
101 }
85102 return cast(this)->getSection();
86103 }
87104
215232 //===----------------------------------------------------------------------===//
216233
217234 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
218 const Twine &Name, GlobalObject *Aliasee,
235 const Twine &Name, Constant *Aliasee,
219236 Module *ParentModule)
220237 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal,
221238 &Op<0>(), 1, Link, Name) {
228245
229246 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
230247 LinkageTypes Link, const Twine &Name,
231 GlobalObject *Aliasee, Module *ParentModule) {
248 Constant *Aliasee, Module *ParentModule) {
232249 return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
233250 }
234251
240257
241258 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
242259 LinkageTypes Linkage, const Twine &Name,
243 GlobalObject *Aliasee) {
260 GlobalValue *Aliasee) {
244261 return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
245262 }
246263
247264 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
248 GlobalObject *Aliasee) {
265 GlobalValue *Aliasee) {
249266 PointerType *PTy = Aliasee->getType();
250267 return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
251268 Aliasee);
252269 }
253270
254 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalObject *Aliasee) {
271 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
255272 return create(Aliasee->getLinkage(), Name, Aliasee);
256273 }
257274
271288 getParent()->getAliasList().erase(this);
272289 }
273290
274 void GlobalAlias::setAliasee(GlobalObject *Aliasee) { setOperand(0, Aliasee); }
291 void GlobalAlias::setAliasee(Constant *Aliasee) {
292 assert(!Aliasee || Aliasee->getType() == getType() &&
293 "Alias and aliasee types should match!");
294 setOperand(0, Aliasee);
295 }
300300 ST->reinsertValue(this);
301301 }
302302
303 static GlobalObject &findReplacementForAliasUse(Value &C) {
304 if (auto *GO = dyn_cast(&C))
305 return *GO;
306 if (auto *GA = dyn_cast(&C))
307 return *GA->getAliasee();
308 auto *CE = cast(&C);
309 assert(CE->getOpcode() == Instruction::BitCast ||
310 CE->getOpcode() == Instruction::GetElementPtr ||
311 CE->getOpcode() == Instruction::AddrSpaceCast);
312 if (CE->getOpcode() == Instruction::GetElementPtr)
313 assert(cast(CE)->hasAllZeroIndices());
314 return findReplacementForAliasUse(*CE->getOperand(0));
315 }
316
317 static void replaceAliasUseWith(Use &U, Value *New) {
318 GlobalObject &Replacement = findReplacementForAliasUse(*New);
319 assert(&cast(*U) != &Replacement &&
320 "replaceAliasUseWith cannot form an alias cycle");
321 U.set(&Replacement);
322 }
323
324303 #ifndef NDEBUG
325304 static bool contains(SmallPtrSet &Cache, ConstantExpr *Expr,
326305 Constant *C) {
372351 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
373352 // constant because they are uniqued.
374353 if (auto *C = dyn_cast(U.getUser())) {
375 if (isa(C)) {
376 replaceAliasUseWith(U, New);
377 continue;
378 }
379354 if (!isa(C)) {
380355 C->replaceUsesOfWithOnConstant(this, New, &U);
381356 continue;
240240 void visitGlobalValue(const GlobalValue &GV);
241241 void visitGlobalVariable(const GlobalVariable &GV);
242242 void visitGlobalAlias(const GlobalAlias &GA);
243 void visitAliaseeSubExpr(const GlobalAlias &A, const Constant &C);
244 void visitAliaseeSubExpr(SmallPtrSet &Visited,
245 const GlobalAlias &A, const Constant &C);
243246 void visitNamedMDNode(const NamedMDNode &NMD);
244247 void visitMDNode(MDNode &MD, Function *F);
245248 void visitModuleIdents(const Module &M);
473476 visitGlobalValue(GV);
474477 }
475478
479 void Verifier::visitAliaseeSubExpr(const GlobalAlias &GA, const Constant &C) {
480 SmallPtrSet Visited;
481 Visited.insert(&GA);
482 visitAliaseeSubExpr(Visited, GA, C);
483 }
484
485 void Verifier::visitAliaseeSubExpr(SmallPtrSet &Visited,
486 const GlobalAlias &GA, const Constant &C) {
487 if (const auto *GV = dyn_cast(&C)) {
488 Assert1(!GV->isDeclaration(), "Alias must point to a definition", &GA);
489
490 if (const auto *GA2 = dyn_cast(GV)) {
491 Assert1(Visited.insert(GA2), "Aliases cannot form a cycle", &GA);
492
493 Assert1(!GA2->mayBeOverridden(), "Alias cannot point to a weak alias",
494 &GA);
495 }
496 }
497
498 if (const auto *CE = dyn_cast(&C))
499 VerifyConstantExprBitcastType(CE);
500
501 for (const Use &U : C.operands()) {
502 Value *V = &*U;
503 if (const auto *GA2 = dyn_cast(V))
504 visitAliaseeSubExpr(Visited, GA, *GA2->getAliasee());
505 else if (const auto *C2 = dyn_cast(V))
506 visitAliaseeSubExpr(Visited, GA, *C2);
507 }
508 }
509
476510 void Verifier::visitGlobalAlias(const GlobalAlias &GA) {
477511 Assert1(!GA.getName().empty(),
478512 "Alias name cannot be empty!", &GA);
479513 Assert1(GlobalAlias::isValidLinkage(GA.getLinkage()),
480514 "Alias should have external or external weak linkage!", &GA);
481 Assert1(GA.getAliasee(),
482 "Aliasee cannot be NULL!", &GA);
515 const Constant *Aliasee = GA.getAliasee();
516 Assert1(Aliasee, "Aliasee cannot be NULL!", &GA);
517 Assert1(GA.getType() == Aliasee->getType(),
518 "Alias and aliasee types should match!", &GA);
483519 Assert1(!GA.hasUnnamedAddr(), "Alias cannot have unnamed_addr!", &GA);
484520
485 const Constant *Aliasee = GA.getAliasee();
486 const GlobalValue *GV = dyn_cast(Aliasee);
487
488 if (!GV) {
489 const ConstantExpr *CE = dyn_cast(Aliasee);
490 if (CE && (CE->getOpcode() == Instruction::BitCast ||
491 CE->getOpcode() == Instruction::AddrSpaceCast ||
492 CE->getOpcode() == Instruction::GetElementPtr))
493 GV = dyn_cast(CE->getOperand(0));
494
495 Assert1(GV, "Aliasee should be either GlobalValue, bitcast or "
496 "addrspacecast of GlobalValue",
497 &GA);
498
499 VerifyConstantExprBitcastType(CE);
500 }
501 Assert1(!GV->isDeclaration(), "Alias must point to a definition", &GA);
502 if (const GlobalAlias *GAAliasee = dyn_cast(GV)) {
503 Assert1(!GAAliasee->mayBeOverridden(), "Alias cannot point to a weak alias",
504 &GA);
505 }
521 Assert1(isa(Aliasee) || isa(Aliasee),
522 "Aliasee should be either GlobalValue or ConstantExpr", &GA);
523
524 visitAliaseeSubExpr(GA, *Aliasee);
506525
507526 visitGlobalValue(GA);
508527 }
333333 // from the ObjC data structures generated by the front end.
334334
335335 // special case if this data blob is an ObjC class definition
336 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
336 std::string Section = v->getSection();
337 if (Section.compare(0, 15, "__OBJC,__class,") == 0) {
337338 if (const GlobalVariable *gv = dyn_cast(v)) {
338339 addObjCClass(gv);
339340 }
340341 }
341342
342343 // special case if this data blob is an ObjC category definition
343 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
344 else if (Section.compare(0, 18, "__OBJC,__category,") == 0) {
344345 if (const GlobalVariable *gv = dyn_cast(v)) {
345346 addObjCCategory(gv);
346347 }
347348 }
348349
349350 // special case if this data blob is the list of referenced classes
350 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
351 else if (Section.compare(0, 18, "__OBJC,__cls_refs,") == 0) {
351352 if (const GlobalVariable *gv = dyn_cast(v)) {
352353 addObjCClassRef(gv);
353354 }
388388 /// actually need, but this allows us to reuse the ValueMapper code.
389389 ValueToValueMapTy ValueMap;
390390
391 std::vector> ReplaceWithAlias;
392
393391 struct AppendingVarInfo {
394392 GlobalVariable *NewGV; // New aggregate global in dest module.
395393 Constant *DstInit; // Old initializer from dest module.
722720 return emitError(
723721 "Appending variables with different unnamed_addr need to be linked!");
724722
725 if (DstGV->getSection() != SrcGV->getSection())
723 if (StringRef(DstGV->getSection()) != SrcGV->getSection())
726724 return emitError(
727725 "Appending variables with different section name need to be linked!");
728726
928926 if (NewVisibility)
929927 NewDA->setVisibility(*NewVisibility);
930928
931 if (DGV)
932 ReplaceWithAlias.push_back(std::make_pair(DGV, NewDA));
929 if (DGV) {
930 // Any uses of DGV need to change to NewDA, with cast.
931 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDA, DGV->getType()));
932 DGV->eraseFromParent();
933 }
933934
934935 ValueMap[SGA] = NewDA;
935936 return false;
10131014 I != E; ++I)
10141015 ValueMap.erase(I);
10151016
1016 }
1017
1018 static GlobalObject &getGlobalObjectInExpr(Constant &C) {
1019 auto *GO = dyn_cast(&C);
1020 if (GO)
1021 return *GO;
1022 auto *GA = dyn_cast(&C);
1023 if (GA)
1024 return *GA->getAliasee();
1025 auto &CE = cast(C);
1026 assert(CE.getOpcode() == Instruction::BitCast ||
1027 CE.getOpcode() == Instruction::AddrSpaceCast);
1028 return getGlobalObjectInExpr(*CE.getOperand(0));
10291017 }
10301018
10311019 /// linkAliasBodies - Insert all of the aliases in Src into the Dest module.
10381026 GlobalAlias *DA = cast(ValueMap[I]);
10391027 Constant *Val =
10401028 MapValue(Aliasee, ValueMap, RF_None, &TypeMap, &ValMaterializer);
1041 DA->setAliasee(&getGlobalObjectInExpr(*Val));
1042 }
1043 }
1044
1045 // Any uses of DGV need to change to NewDA, with cast.
1046 for (auto &Pair : ReplaceWithAlias) {
1047 GlobalValue *DGV = Pair.first;
1048 GlobalAlias *NewDA = Pair.second;
1049
1050 for (auto *User : DGV->users()) {
1051 if (auto *GA = dyn_cast(User)) {
1052 if (GA == NewDA)
1053 report_fatal_error("Linking these modules creates an alias cycle.");
1054 }
1055 }
1056
1057 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDA, DGV->getType()));
1058 DGV->eraseFromParent();
1029 DA->setAliasee(Val);
1030 }
10591031 }
10601032 }
10611033
13551355
13561356 // Skip meta data
13571357 if (GVar->hasSection()) {
1358 if (GVar->getSection() == "llvm.metadata")
1358 if (GVar->getSection() == StringRef("llvm.metadata"))
13591359 return;
13601360 }
13611361
272272 SelectionDAG &DAG) const {
273273 // FIXME there is no actual debug info here
274274 SDLoc dl(GA);
275 const GlobalValue *UnderlyingGV = GV;
276 // If GV is an alias then use the aliasee to determine the wrapper type
277 if (const GlobalAlias *GA = dyn_cast(GV))
278 UnderlyingGV = GA->getAliasee();
279 if (const GlobalVariable *GVar = dyn_cast(UnderlyingGV)) {
280 if ((GVar->isConstant() && GV->hasLocalLinkage()) ||
281 (GVar->hasSection() &&
282 StringRef(GVar->getSection()).startswith(".cp.")))
283 return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, GA);
284 return DAG.getNode(XCoreISD::DPRelativeWrapper, dl, MVT::i32, GA);
285 }
286 return DAG.getNode(XCoreISD::PCRelativeWrapper, dl, MVT::i32, GA);
275
276 if (GV->getType()->getElementType()->isFunctionTy())
277 return DAG.getNode(XCoreISD::PCRelativeWrapper, dl, MVT::i32, GA);
278
279 const auto *GVar = dyn_cast(GV);
280 if ((GV->hasSection() && StringRef(GV->getSection()).startswith(".cp.")) ||
281 (GVar && GVar->isConstant() && GV->hasLocalLinkage()))
282 return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, GA);
283
284 return DAG.getNode(XCoreISD::DPRelativeWrapper, dl, MVT::i32, GA);
287285 }
288286
289287 static bool IsSmallObject(const GlobalValue *GV, const XCoreTargetLowering &XTL) {
106106 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
107107 I != E; ++I) {
108108 GlobalAlias *GA = cast(VMap[I]);
109 if (const GlobalObject *C = I->getAliasee())
109 if (const Constant *C = I->getAliasee())
110110 GA->setAliasee(cast(MapValue(C, VMap)));
111111 }
112112
22 ; Test that global aliases are allowed to be constant addrspacecast
33
44 @i = internal addrspace(1) global i8 42
5 @ia = alias internal addrspace(2) i8 addrspace(3)*, i8 addrspace(1)* @i
6 ; CHECK: @ia = alias internal addrspace(2) i8 addrspace(3)*, i8 addrspace(1)* @i
5 @ia = alias internal addrspacecast (i8 addrspace(1)* @i to i8 addrspace(2)* addrspace(3)*)
6 ; CHECK: @ia = alias internal addrspacecast (i8 addrspace(1)* @i to i8 addrspace(2)* addrspace(3)*)
+0
-6
test/Assembler/alias-addrspace.ll less more
None ; RUN: not llvm-as %s 2>&1 | FileCheck %s
1
2 @foo = global i32 42
3 @bar = alias internal addrspace(1) i32* @foo
4
5 CHECK: error: A type is required if addrspace is given
+0
-5
test/Assembler/alias-to-alias.ll less more
None ; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s
1 ; CHECK: Alias must point to function or variable
2
3 @b1 = alias i32* @c1
4 @c1 = alias i32* @b1
+0
-7
test/Assembler/alias-to-alias2.ll less more
None ; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s
1 ; CHECK: error: Alias is pointed by alias b1
2
3 @g = global i32 42
4
5 @b1 = alias i32* @c1
6 @c1 = alias i32* @g
+0
-6
test/Assembler/alias-type.ll less more
None ; RUN: not llvm-as %s 2>&1 | FileCheck %s
1
2 @foo = global i32 42
3 @bar = alias i32 @foo
4
5 CHECK: error: An alias must have pointer type
99 ; CHECK: @v2 = global [1 x i32] zeroinitializer
1010
1111 @v3 = alias bitcast (i32* @v1 to i16*)
12 ; CHECK: @v3 = alias i16, i32* @v1
12 ; CHECK: @v3 = alias bitcast (i32* @v1 to i16*)
1313
1414 @v4 = alias getelementptr ([1 x i32]* @v2, i32 0, i32 0)
15 ; CHECK: @v4 = alias i32, [1 x i32]* @v2
15 ; CHECK: @v4 = alias getelementptr inbounds ([1 x i32]* @v2, i32 0, i32 0)
1616
1717 @v5 = alias i32 addrspace(2)* addrspacecast (i32 addrspace(0)* @v1 to i32 addrspace(2)*)
18 ; CHECK: @v5 = alias addrspace(2) i32, i32* @v1
18 ; CHECK: @v5 = alias addrspacecast (i32* @v1 to i32 addrspace(2)*)
1919
2020 @v6 = alias i16* @v3
21 ; CHECK: @v6 = alias i16, i32* @v1
21 ; CHECK: @v6 = alias i16* @v3
2828
2929 @bar_i = alias internal i32* @bar
3030
31 @A = alias i64, i32* @bar
31 @A = alias bitcast (i32* @bar to i64*)
3232
3333 define i32 @test() {
3434 entry:
3737 @bar_i = alias internal i32* @bar
3838
3939 ; CHECK-DAG: .globl A
40 @A = alias i64, i32* @bar
40 @A = alias bitcast (i32* @bar to i64*)
4141
4242 ; CHECK-DAG: .globl bar_h
4343 ; CHECK-DAG: .hidden bar_h
4646 ; CHECK-DAG: .globl bar_p
4747 ; CHECK-DAG: .protected bar_p
4848 @bar_p = protected alias i32* @bar
49
50 ; CHECK-DAG: test2 = bar+4
51 @test2 = alias getelementptr(i32 *@bar, i32 1)
52
53 ; CHECK-DAG: test3 = 42
54 @test3 = alias inttoptr(i32 42 to i32*)
55
56 ; CHECK-DAG: test4 = bar
57 @test4 = alias inttoptr(i64 ptrtoint (i32* @bar to i64) to i32*)
58
59 ; CHECK-DAG: test5 = test2-bar
60 @test5 = alias inttoptr(i32 sub (i32 ptrtoint (i32* @test2 to i32),
61 i32 ptrtoint (i32* @bar to i32)) to i32*)
4962
5063 ; CHECK-DAG: .globl test
5164 define i32 @test() {
7272 @weak_alias = dllexport alias weak_odr void()* @f1
7373
7474 @blob = global [6 x i8] c"\B8*\00\00\00\C3", section ".text", align 16
75 @blob_alias = dllexport alias i32 (), [6 x i8]* @blob
75 @blob_alias = dllexport alias bitcast ([6 x i8]* @blob to i32 ()*)
7676
7777 ; CHECK: .section .drectve
7878 ; WIN32: " /EXPORT:Var1,DATA"
55 @v2 = global [1 x i32] zeroinitializer
66 ; CHECK: @v2 = global [1 x i32] zeroinitializer
77
8 @a1 = alias i16, i32* @v1
9 ; CHECK: @a1 = alias i16, i32* @v1
8 @v3 = global [2 x i16] zeroinitializer
9 ; CHECK: @v3 = global [2 x i16] zeroinitializer
1010
11 @a2 = alias i32, [1 x i32]* @v2
12 ; CHECK: @a2 = alias i32, [1 x i32]* @v2
11 @a1 = alias bitcast (i32* @v1 to i16*)
12 ; CHECK: @a1 = alias bitcast (i32* @v1 to i16*)
1313
14 @a3 = alias addrspace(2) i32, i32* @v1
15 ; CHECK: @a3 = alias addrspace(2) i32, i32* @v1
14 @a2 = alias bitcast([1 x i32]* @v2 to i32*)
15 ; CHECK: @a2 = alias getelementptr inbounds ([1 x i32]* @v2, i32 0, i32 0)
1616
17 @a4 = alias i16, i32* @v1
18 ; CHECK: @a4 = alias i16, i32* @v1
17 @a3 = alias addrspacecast (i32* @v1 to i32 addrspace(2)*)
18 ; CHECK: @a3 = alias addrspacecast (i32* @v1 to i32 addrspace(2)*)
19
20 @a4 = alias bitcast (i32* @v1 to i16*)
21 ; CHECK: @a4 = alias bitcast (i32* @v1 to i16*)
1922
2023 @a5 = thread_local(localdynamic) alias i32* @v1
2124 ; CHECK: @a5 = thread_local(localdynamic) alias i32* @v1
25
26 @a6 = alias getelementptr ([2 x i16]* @v3, i32 1, i32 1)
27 ; CHECK: @a6 = alias getelementptr ([2 x i16]* @v3, i32 1, i32 1)
66 @bar = global i32 0
77 @foo1 = alias i32* @bar
88 @foo2 = alias i32* @bar
9 @foo3 = alias i32* @foo2
910
1011 %FunTy = type i32()
1112
1314 ret i32 0
1415 }
1516 @bar_f = alias weak_odr %FunTy* @foo_f
17 @bar_ff = alias i32()* @bar_f
1618
1719 @bar_i = alias internal i32* @bar
1820
19 @A = alias i64, i32* @bar
21 @A = alias bitcast (i32* @bar to i64*)
2022
2123 define i32 @test() {
2224 entry:
0 %foo = type { [8 x i8] }
11 %bar = type { [9 x i8] }
22
3 @zed = alias void (%foo*), void (%bar*)* @xyz
3 @zed = alias bitcast (void (%bar*)* @xyz to void (%foo*)*)
44
55 define void @xyz(%bar* %this) {
66 entry:
0 @zed = global i32 42
11 @foo = alias i32* @zed
2 @foo2 = alias i16, i32* @zed
2 @foo2 = alias bitcast (i32* @zed to i16*)
+0
-2
test/Linker/Inputs/cycle.ll less more
None @foo = alias i32* @bar
1 @bar = weak global i32 0
44 ; CHECK-DAG: @foo = alias i32* @zed
55
66 @bar = alias i32* @foo
7 ; CHECK-DAG: @bar = alias i32* @zed
7 ; CHECK-DAG: @bar = alias i32* @foo
88
99 @foo2 = weak global i32 0
10 ; CHECK-DAG: @foo2 = alias i16, i32* @zed
10 ; CHECK-DAG: @foo2 = alias bitcast (i32* @zed to i16*)
1111
1212 @bar2 = alias i32* @foo2
13 ; CHECK-DAG: @bar2 = alias i32* @zed
13 ; CHECK-DAG: @bar2 = alias bitcast (i16* @foo2 to i32*)
1414
1515 ; CHECK-DAG: @zed = global i32 42
+0
-7
test/Linker/cycle.ll less more
None ; RUN: not llvm-link %s %S/Inputs/cycle.ll 2>&1 | FileCheck %s
1 ; RUN: not llvm-link %S/Inputs/cycle.ll %s 2>&1 | FileCheck %s
2
3 ; CHECK: Linking these modules creates an alias cycle
4
5 @foo = weak global i32 0
6 @bar = alias i32* @foo
1010 @L1 = alias i32* @A
1111 ; CHECK: @L1 = alias i32* @A
1212
13 @L2 = alias internal i32* @A
14 ; DEAD-NOT: @L2
13 @L2 = alias internal i32* @L1
14 ; CHECK: @L2 = alias internal i32* @L1
1515
16 @L3 = alias i32* @A
17 ; CHECK: @L3 = alias i32* @A
16 @L3 = alias i32* @L2
17 ; CHECK: @L3 = alias i32* @L2
11
22 @g = global i32 0
33
4 @a = alias i8, i32* @g
4 @a = alias bitcast (i32* @g to i8*)
55
66 define void @f() {
77 %tmp = load i8* @a
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
11
2 @foo1 = alias void ()* @bar2
2 @foo1 = alias void ()* @foo2
33 ; CHECK: @foo1 = alias void ()* @bar2
44
5 @foo2 = alias void()* @bar2
5 @foo2 = alias void()* @bar1
66 ; CHECK: @foo2 = alias void ()* @bar2
77
88 @bar1 = alias void ()* @bar2
55 ; Cases that should be bitcast
66
77 ; Test cast between scalars with same bit sizes
8 @alias_i32_to_f32 = alias float (float), i32 (i32)* @func_i32
8 @alias_i32_to_f32 = alias bitcast (i32 (i32)* @func_i32 to float (float)*)
99
1010 ; Test cast between vectors with same number of elements and bit sizes
11 @alias_v2i32_to_v2f32 = alias <2 x float> (<2 x float>), <2 x i32> (<2 x i32>)* @func_v2i32
11 @alias_v2i32_to_v2f32 = alias bitcast (<2 x i32> (<2 x i32>)* @func_v2i32 to <2 x float> (<2 x float>)*)
1212
1313 ; Test cast from vector to scalar with same number of bits
14 @alias_v2f32_to_i64 = alias <2 x float> (<2 x float>), i64 (i64)* @func_i64
14 @alias_v2f32_to_i64 = alias bitcast (i64 (i64)* @func_i64 to <2 x float> (<2 x float>)*)
1515
1616 ; Test cast from scalar to vector with same number of bits
17 @alias_i64_to_v2f32 = alias i64 (i64), <2 x float> (<2 x float>)* @func_v2f32
17 @alias_i64_to_v2f32 = alias bitcast (<2 x float> (<2 x float>)* @func_v2f32 to i64 (i64)*)
1818
1919 ; Test cast between vectors of pointers
20 @alias_v2i32p_to_v2i64p = alias <2 x i64*> (<2 x i64*>), <2 x i32*> (<2 x i32*>)* @func_v2i32p
20 @alias_v2i32p_to_v2i64p = alias bitcast (<2 x i32*> (<2 x i32*>)* @func_v2i32p to <2 x i64*> (<2 x i64*>)*)
2121
2222
2323 ; Cases that should be invalid and unchanged
2424
2525 ; Test cast between scalars with different bit sizes
26 @alias_i64_to_f32 = alias float (float), i64 (i64)* @func_i64
26 @alias_i64_to_f32 = alias bitcast (i64 (i64)* @func_i64 to float (float)*)
2727
2828 ; Test cast between vectors with different bit sizes but the
2929 ; same number of elements
30 @alias_v2i64_to_v2f32 = alias <2 x float> (<2 x float>), <2 x i64> (<2 x i64>)* @func_v2i64
30 @alias_v2i64_to_v2f32 = alias bitcast (<2 x i64> (<2 x i64>)* @func_v2i64 to <2 x float> (<2 x float>)*)
3131
3232 ; Test cast between vectors with same number of bits and different
3333 ; numbers of elements
34 @alias_v2i32_to_v4f32 = alias <4 x float> (<4 x float>), <2 x i32> (<2 x i32>)* @func_v2i32
34 @alias_v2i32_to_v4f32 = alias bitcast (<2 x i32> (<2 x i32>)* @func_v2i32 to <4 x float> (<4 x float>)*)
3535
3636 ; Test cast between scalar and vector with different number of bits
37 @alias_i64_to_v4f32 = alias i64 (i64), <4 x float> (<4 x float>)* @func_v4f32
37 @alias_i64_to_v4f32 = alias bitcast (<4 x float> (<4 x float>)* @func_v4f32 to i64 (i64)*)
3838
3939 ; Test cast between vector and scalar with different number of bits
40 @alias_v4f32_to_i64 = alias <4 x float> (<4 x float>), i64 (i64)* @func_i64
40 @alias_v4f32_to_i64 = alias bitcast (i64 (i64)* @func_i64 to <4 x float> (<4 x float>)*)
4141
4242 ; Test cast from scalar to vector of pointers with same number of bits
4343 ; We don't know the pointer size at this point, so this can't be done
44 @alias_i64_to_v2i32p = alias i64 (i64), <2 x i32*> (<2 x i32*>)* @func_v2i32p
44 @alias_i64_to_v2i32p = alias bitcast (<2 x i32*> (<2 x i32*>)* @func_v2i32p to i64 (i64)*)
4545
4646 ; Test cast between vector of pointers and scalar with different number of bits
47 @alias_v4i32p_to_i64 = alias <4 x i32*> (<4 x i32*>), i64 (i64)* @func_i64
47 @alias_v4i32p_to_i64 = alias bitcast (i64 (i64)* @func_i64 to <4 x i32*> (<4 x i32*>)*)
4848
4949
5050
99 @ga = alias i32* @g
1010 ; CHECK: Alias must point to a definition
1111 ; CHECK-NEXT: @ga
12
13
14 @test2_a = alias i32* @test2_b
15 @test2_b = alias i32* @test2_a
16 ; CHECK: Aliases cannot form a cycle
17 ; CHECK-NEXT: i32* @test2_a
18 ; CHECK-NEXT: Aliases cannot form a cycle
19 ; CHECK-NEXT: i32* @test2_b
20
21
22 @test3_a = global i32 42
23 @test3_b = alias weak i32* @test3_a
24 @test3_c = alias i32* @test3_b
25 ; CHECK: Alias cannot point to a weak alias
26 ; CHECK-NEXT: i32* @test3_c
0 ; RUN: not llvm-as -disable-output %s 2>&1 | FileCheck %s
1
2 ; CHECK: error: invalid cast opcode for cast from 'i32 addrspace(2)*' to 'i32 addrspace(1)*'
3
4 target datalayout = "e-p:32:32:32-p1:16:16:16-p2:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:32:64-v128:32:128-a0:0:32-n8:16:32"
5
6
7 @data = addrspace(2) global i32 27
8
9 @illegal_alias_data = alias bitcast (i32 addrspace(2)* @data to i32 addrspace(1)*)
414414 }
415415
416416 static char getSymbolNMTypeChar(const GlobalValue &GV) {
417 if (isa(GV))
417 if (GV.getType()->getElementType()->isFunctionTy())
418418 return 't';
419419 // FIXME: should we print 'b'? At the IR level we cannot be sure if this
420420 // will be in bss or not, but we could approximate.
421 if (isa(GV))
422 return 'd';
423 const GlobalAlias *GA = cast(&GV);
424 const GlobalValue *AliasedGV = GA->getAliasee();
425 return getSymbolNMTypeChar(*AliasedGV);
421 return 'd';
426422 }
427423
428424 static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
268268 "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
269269 }
270270
271 TEST(ConstantsTest, ReplaceInAliasTest) {
272 std::unique_ptr M(new Module("MyModule", getGlobalContext()));
273
274 Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
275 auto *Global = cast(M->getOrInsertGlobal("dummy", Int32Ty));
276 auto *GA = GlobalAlias::create(GlobalValue::ExternalLinkage, "alias", Global);
277 EXPECT_DEATH(Global->replaceAllUsesWith(GA),
278 "replaceAliasUseWith cannot form an alias cycle");
279 }
280
281271 #endif
282272 #endif
283273