llvm.org GIT mirror llvm / 27c076a
Fix most of PR10367. This patch changes the design of GlobalAlias so that it doesn't take a ConstantExpr anymore. It now points directly to a GlobalObject, but its type is independent of the aliasee type. To avoid changing all alias related tests in this patches, I kept the common syntax @foo = alias i32* @bar to mean the same as now. The cases that used to use cast now use the more general syntax @foo = alias i16, i32* @bar. Note that GlobalAlias now behaves a bit more like GlobalVariable. We know that its type is always a pointer, so we omit the '*'. For the bitcode, a nice surprise is that we were writing both identical types already, so the format change is minimal. Auto upgrade is handled by looking through the casts and no new fields are needed for now. New bitcode will simply have different types for Alias and Aliasee. One last interesting point in the patch is that replaceAllUsesWith becomes smart enough to avoid putting a ConstantExpr in the aliasee. This seems better than checking and updating every caller. A followup patch will delete getAliasedGlobal now that it is redundant. Another patch will add support for an explicit offset. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@209007 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
41 changed file(s) with 312 addition(s) and 160 deletion(s). Raw diff Collapse all Expand all
4040 /// If a parent module is specified, the alias is automatically inserted into
4141 /// the end of the specified module's alias list.
4242 GlobalAlias(Type *Ty, LinkageTypes Linkage, const Twine &Name = "",
43 Constant* Aliasee = nullptr, Module *Parent = nullptr,
43 GlobalObject *Aliasee = nullptr, Module *Parent = nullptr,
4444 unsigned AddressSpace = 0);
4545
4646 /// Provide fast operand accessors
5757 void eraseFromParent() override;
5858
5959 /// set/getAliasee - These methods retrive and set alias target.
60 void setAliasee(Constant *GV);
61 const Constant *getAliasee() const {
62 return getOperand(0);
63 }
64 Constant *getAliasee() {
65 return getOperand(0);
60 void setAliasee(GlobalObject *GO);
61 const GlobalObject *getAliasee() const {
62 return const_cast(this)->getAliasee();
6663 }
6764
68 /// This method tries to ultimately resolve the alias by going through the
69 /// aliasing chain and trying to find the very last global. Returns NULL if a
70 /// cycle was found.
71 GlobalObject *getAliasedGlobal();
65 GlobalObject *getAliasee() {
66 return cast_or_null(getOperand(0));
67 }
68
69 GlobalObject *getAliasedGlobal() {
70 return getAliasee();
71 }
72
7273 const GlobalObject *getAliasedGlobal() const {
7374 return const_cast(this)->getAliasedGlobal();
7475 }
629629 /// ParseAlias:
630630 /// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass 'alias'
631631 /// OptionalLinkage Aliasee
632 /// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass 'alias'
633 /// OptionalLinkage OptionalAddrSpace Type, Aliasee
634 ///
632635 /// Aliasee
633636 /// ::= TypeAndValue
634 /// ::= 'bitcast' '(' TypeAndValue 'to' Type ')'
635 /// ::= 'getelementptr' 'inbounds'? '(' ... ')'
636637 ///
637638 /// Everything through DLL storage class has already been parsed.
638639 ///
654655 return Error(LinkageLoc,
655656 "symbol with local linkage must have default visibility");
656657
657 Constant *Aliasee;
658 bool HasAddrSpace = Lex.getKind() == lltok::kw_addrspace;
659 unsigned AddrSpace;
660 LocTy AddrSpaceLoc = Lex.getLoc();
661 if (ParseOptionalAddrSpace(AddrSpace))
662 return true;
663
664 LocTy TyLoc = Lex.getLoc();
665 Type *Ty = nullptr;
666 if (ParseType(Ty))
667 return true;
668
669 bool DifferentType = EatIfPresent(lltok::comma);
670 if (HasAddrSpace && !DifferentType)
671 return Error(AddrSpaceLoc, "A type is required if addrspace is given");
672
673 Type *AliaseeType = nullptr;
674 if (DifferentType) {
675 if (ParseType(AliaseeType))
676 return true;
677 } else {
678 AliaseeType = Ty;
679 auto *PTy = dyn_cast(Ty);
680 if (!PTy)
681 return Error(TyLoc, "An alias must have pointer type");
682 Ty = PTy->getElementType();
683 AddrSpace = PTy->getAddressSpace();
684 }
685
658686 LocTy AliaseeLoc = Lex.getLoc();
659 if (Lex.getKind() != lltok::kw_bitcast &&
660 Lex.getKind() != lltok::kw_getelementptr) {
661 if (ParseGlobalTypeAndValue(Aliasee)) return true;
662 } else {
663 // The bitcast dest type is not present, it is implied by the dest type.
664 ValID ID;
665 if (ParseValID(ID)) return true;
666 if (ID.Kind != ValID::t_Constant)
667 return Error(AliaseeLoc, "invalid aliasee");
668 Aliasee = ID.ConstantVal;
669 }
670
671 if (!Aliasee->getType()->isPointerTy())
672 return Error(AliaseeLoc, "alias must have pointer type");
687 Constant *C;
688 if (ParseGlobalValue(AliaseeType, C))
689 return true;
690
691 auto *Aliasee = dyn_cast(C);
692 if (!Aliasee)
693 return Error(AliaseeLoc, "Alias must point to function or variable");
694
695 assert(Aliasee->getType()->isPointerTy());
673696
674697 // Okay, create the alias but do not insert it into the module yet.
675 PointerType *PTy = cast(Aliasee->getType());
676698 std::unique_ptr GA(
677 new GlobalAlias(PTy->getElementType(), (GlobalValue::LinkageTypes)Linkage,
678 Name, Aliasee, nullptr, PTy->getAddressSpace()));
699 new GlobalAlias(Ty, (GlobalValue::LinkageTypes)Linkage, Name, Aliasee,
700 /*Parent*/ nullptr, AddrSpace));
679701 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
680702 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
681703
697719
698720 // If they agree, just RAUW the old value with the alias and remove the
699721 // forward ref info.
722 for (auto *User : Val->users()) {
723 if (auto *GA = dyn_cast(User))
724 return Error(NameLoc, "Alias is pointed by alias " + GA->getName());
725 }
726
700727 Val->replaceAllUsesWith(GA.get());
701728 Val->eraseFromParent();
702729 ForwardRefVals.erase(I);
10911091 return 1ULL << 63;
10921092 }
10931093
1094 // FIXME: Delete this in LLVM 4.0 and just assert that the aliasee is a
1095 // GlobalObject.
1096 static GlobalObject &
1097 getGlobalObjectInExpr(const DenseMap &Map,
1098 Constant &C) {
1099 auto *GO = dyn_cast(&C);
1100 if (GO)
1101 return *GO;
1102
1103 auto *GA = dyn_cast(&C);
1104 if (GA)
1105 return getGlobalObjectInExpr(Map, *Map.find(GA)->second);
1106
1107 auto &CE = cast(C);
1108 assert(CE.getOpcode() == Instruction::BitCast ||
1109 CE.getOpcode() == Instruction::GetElementPtr ||
1110 CE.getOpcode() == Instruction::AddrSpaceCast);
1111 if (CE.getOpcode() == Instruction::GetElementPtr)
1112 assert(cast(CE).hasAllZeroIndices());
1113 return getGlobalObjectInExpr(Map, *CE.getOperand(0));
1114 }
1115
10941116 /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
10951117 /// values and aliases that we can.
10961118 error_code BitcodeReader::ResolveGlobalAndAliasInits() {
11161138 GlobalInitWorklist.pop_back();
11171139 }
11181140
1141 // FIXME: Delete this in LLVM 4.0
1142 // Older versions of llvm could write an alias pointing to another. We cannot
1143 // construct those aliases, so we first collect an alias to aliasee expression
1144 // and then compute the actual aliasee.
1145 DenseMap AliasInit;
1146
11191147 while (!AliasInitWorklist.empty()) {
11201148 unsigned ValID = AliasInitWorklist.back().second;
11211149 if (ValID >= ValueList.size()) {
11221150 AliasInits.push_back(AliasInitWorklist.back());
11231151 } else {
11241152 if (Constant *C = dyn_cast_or_null(ValueList[ValID]))
1125 AliasInitWorklist.back().first->setAliasee(C);
1153 AliasInit.insert(std::make_pair(AliasInitWorklist.back().first, C));
11261154 else
11271155 return Error(ExpectedConstant);
11281156 }
11291157 AliasInitWorklist.pop_back();
1158 }
1159
1160 for (auto &Pair : AliasInit) {
1161 auto &GO = getGlobalObjectInExpr(AliasInit, *Pair.second);
1162 Pair.first->setAliasee(&GO);
11301163 }
11311164
11321165 while (!FunctionPrefixWorklist.empty()) {
14921492
14931493 PrintLinkage(GA->getLinkage(), Out);
14941494
1495 PointerType *Ty = GA->getType();
14951496 const Constant *Aliasee = GA->getAliasee();
1497 if (!Aliasee || Ty != Aliasee->getType()) {
1498 if (unsigned AddressSpace = Ty->getAddressSpace())
1499 Out << "addrspace(" << AddressSpace << ") ";
1500 TypePrinter.print(Ty->getElementType(), Out);
1501 Out << ", ";
1502 }
14961503
14971504 if (!Aliasee) {
1498 TypePrinter.print(GA->getType(), Out);
14991505 Out << " <>";
15001506 } else {
15011507 writeOperand(Aliasee, !isa(Aliasee));
14901490 auto *PTy = cast(unwrap(Ty));
14911491 return wrap(new GlobalAlias(
14921492 PTy->getElementType(), GlobalValue::ExternalLinkage, Name,
1493 unwrap<Constant>(Aliasee), unwrap(M), PTy->getAddressSpace()));
1493 unwrap<GlobalObject>(Aliasee), unwrap(M), PTy->getAddressSpace()));
14941494 }
14951495
14961496 /*--.. Operations on functions .............................................--*/
213213 //===----------------------------------------------------------------------===//
214214
215215 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link, const Twine &Name,
216 Constant *Aliasee, Module *ParentModule,
216 GlobalObject *Aliasee, Module *ParentModule,
217217 unsigned AddressSpace)
218218 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal,
219219 &Op<0>(), 1, Link, Name) {
220220 LeakDetector::addGarbageObject(this);
221
222 if (Aliasee)
223 assert(Aliasee->getType() == getType() &&
224 "Alias and aliasee types should match!");
225221 Op<0>() = Aliasee;
226222
227223 if (ParentModule)
244240 getParent()->getAliasList().erase(this);
245241 }
246242
247 void GlobalAlias::setAliasee(Constant *Aliasee) {
248 assert((!Aliasee || Aliasee->getType() == getType()) &&
249 "Alias and aliasee types should match!");
250
251 setOperand(0, Aliasee);
252 }
253
254 static GlobalValue *getAliaseeGV(GlobalAlias *GA) {
255 Constant *C = GA->getAliasee();
256 assert(C && "Must alias something");
257
258 if (GlobalValue *GV = dyn_cast(C))
259 return GV;
260
261 ConstantExpr *CE = cast(C);
262 assert((CE->getOpcode() == Instruction::BitCast ||
263 CE->getOpcode() == Instruction::AddrSpaceCast ||
264 CE->getOpcode() == Instruction::GetElementPtr) &&
265 "Unsupported aliasee");
266
267 return cast(CE->getOperand(0));
268 }
269
270 GlobalObject *GlobalAlias::getAliasedGlobal() {
271 SmallPtrSet Visited;
272
273 GlobalAlias *GA = this;
274
275 for (;;) {
276 GlobalValue *GV = getAliaseeGV(GA);
277 if (!Visited.insert(GV))
278 return nullptr;
279
280 // Iterate over aliasing chain.
281 GA = dyn_cast(GV);
282 if (!GA)
283 return cast(GV);
284 }
285 }
243 void GlobalAlias::setAliasee(GlobalObject *Aliasee) { setOperand(0, Aliasee); }
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 auto *Old = &cast(*U);
320 assert(Old != &Replacement &&
321 "replaceAliasUseWith cannot form an alias cycle");
322 U.set(&Replacement);
323 }
324
303325 #ifndef NDEBUG
304326 static bool contains(SmallPtrSet &Cache, ConstantExpr *Expr,
305327 Constant *C) {
350372 Use &U = *UseList;
351373 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
352374 // constant because they are uniqued.
353 if (Constant *C = dyn_cast(U.getUser())) {
375 if (auto *C = dyn_cast(U.getUser())) {
376 if (isa(C)) {
377 replaceAliasUseWith(U, New);
378 continue;
379 }
354380 if (!isa(C)) {
355381 C->replaceUsesOfWithOnConstant(this, New, &U);
356382 continue;
472472 "Alias should have external or external weak linkage!", &GA);
473473 Assert1(GA.getAliasee(),
474474 "Aliasee cannot be NULL!", &GA);
475 Assert1(GA.getType() == GA.getAliasee()->getType(),
476 "Alias and aliasee types should match!", &GA);
477475 Assert1(!GA.hasUnnamedAddr(), "Alias cannot have unnamed_addr!", &GA);
478476
479477 const Constant *Aliasee = GA.getAliasee();
504502 Assert1(!GAAliasee->mayBeOverridden(), "Alias cannot point to a weak alias",
505503 &GA);
506504 }
507
508 const GlobalValue *AG = GA.getAliasedGlobal();
509 Assert1(AG, "Aliasing chain should end with function or global variable",
510 &GA);
511505
512506 visitGlobalValue(GA);
513507 }
388388 /// actually need, but this allows us to reuse the ValueMapper code.
389389 ValueToValueMapTy ValueMap;
390390
391 std::vector> ReplaceWithAlias;
392
391393 struct AppendingVarInfo {
392394 GlobalVariable *NewGV; // New aggregate global in dest module.
393395 Constant *DstInit; // Old initializer from dest module.
919921 // If there is no linkage to be performed or we're linking from the source,
920922 // bring over SGA.
921923 auto *PTy = cast(TypeMap.get(SGA->getType()));
922 GlobalAlias *NewDA =
924 auto *NewDA =
923925 new GlobalAlias(PTy->getElementType(), SGA->getLinkage(), SGA->getName(),
924926 /*aliasee*/ nullptr, DstM, PTy->getAddressSpace());
925927 copyGVAttributes(NewDA, SGA);
926928 if (NewVisibility)
927929 NewDA->setVisibility(*NewVisibility);
928930
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 }
931 if (DGV)
932 ReplaceWithAlias.push_back(std::make_pair(DGV, NewDA));
934933
935934 ValueMap[SGA] = NewDA;
936935 return false;
10161015
10171016 }
10181017
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));
1029 }
1030
10191031 /// linkAliasBodies - Insert all of the aliases in Src into the Dest module.
10201032 void ModuleLinker::linkAliasBodies() {
10211033 for (Module::alias_iterator I = SrcM->alias_begin(), E = SrcM->alias_end();
10241036 continue;
10251037 if (Constant *Aliasee = I->getAliasee()) {
10261038 GlobalAlias *DA = cast(ValueMap[I]);
1027 DA->setAliasee(MapValue(Aliasee, ValueMap, RF_None,
1028 &TypeMap, &ValMaterializer));
1029 }
1039 Constant *Val =
1040 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();
10301059 }
10311060 }
10321061
28602860 if (!hasUsesToReplace(*J, Used, RenameTarget))
28612861 continue;
28622862
2863 J->replaceAllUsesWith(Aliasee);
2863 J->replaceAllUsesWith(ConstantExpr::getBitCast(Aliasee, J->getType()));
28642864 ++NumAliasesResolved;
28652865 Changed = true;
28662866
13261326
13271327 // Replace G with an alias to F and delete G.
13281328 void MergeFunctions::writeAlias(Function *F, Function *G) {
1329 Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType());
13301329 PointerType *PTy = G->getType();
1331 GlobalAlias *GA =
1332 new GlobalAlias(PTy->getElementType(), G->getLinkage(), "", BitcastF,
1333 G->getParent(), PTy->getAddressSpace());
1330 auto *GA = new GlobalAlias(PTy->getElementType(), G->getLinkage(), "", F,
1331 G->getParent(), PTy->getAddressSpace());
13341332 F->setAlignment(std::max(F->getAlignment(), G->getAlignment()));
13351333 GA->takeName(G);
13361334 GA->setVisibility(G->getVisibility());
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 Constant *C = I->getAliasee())
110 GA->setAliasee(MapValue(C, VMap));
109 if (const GlobalObject *C = I->getAliasee())
110 GA->setAliasee(cast(MapValue(C, VMap)));
111111 }
112112
113113 // And named metadata....
+0
-8
test/Assembler/2009-04-25-AliasGEP.ll less more
None ; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis
1 ; PR4066
2 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
3 target triple = "i386-apple-darwin9"
4 %struct.i2c_device_id = type { }
5 @w83l785ts_id = internal constant [0 x %struct.i2c_device_id] zeroinitializer, align 1 ; <[0 x %struct.i2c_device_id]*> [#uses=1]
6
7 @__mod_i2c_device_table = alias getelementptr ([0 x %struct.i2c_device_id]* @w83l785ts_id, i32 0, i32 0) ; <%struct.i2c_device_id*> [#uses=0]
None ; RUN: llvm-as -disable-output %s
0 ; RUN: llvm-as < %s | llvm-dis | FileCheck %s
11
22 ; Test that global aliases are allowed to be constant addrspacecast
33
44 @i = internal addrspace(1) global i8 42
5 @ia = alias internal i8 addrspace(2)* addrspacecast (i8 addrspace(1)* @i to i8 addrspace(2)*)
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
0 ; 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 ; 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 ; 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 ; 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
0 ; RUN: llvm-dis < %s.bc | FileCheck %s
1
2 ; old-aliases.bc consist of this file assembled with an old llvm-as (3.5 trunk)
3 ; from when aliases contained a ConstantExpr.
4
5 @v1 = global i32 0
6 ; CHECK: @v1 = global i32 0
7
8 @v2 = global [1 x i32] zeroinitializer
9 ; CHECK: @v2 = global [1 x i32] zeroinitializer
10
11 @v3 = alias bitcast (i32* @v1 to i16*)
12 ; CHECK: @v3 = alias i16, i32* @v1
13
14 @v4 = alias getelementptr ([1 x i32]* @v2, i32 0, i32 0)
15 ; CHECK: @v4 = alias i32, [1 x i32]* @v2
16
17 @v5 = alias i32 addrspace(2)* addrspacecast (i32 addrspace(0)* @v1 to i32 addrspace(2)*)
18 ; CHECK: @v5 = alias addrspace(2) i32, i32* @v1
19
20 @v6 = alias i16* @v3
21 ; CHECK: @v6 = alias i16, i32* @v1
2828
2929 @bar_i = alias internal i32* @bar
3030
31 @A = alias bitcast (i32* @bar to i64*)
31 @A = alias i64, i32* @bar
3232
3333 define i32 @test() {
3434 entry:
2121 @bar_i = alias internal i32* @bar
2222
2323 ; CHECK-DAG: .globl A
24 @A = alias bitcast (i32* @bar to i64*)
24 @A = alias i64, i32* @bar
2525
2626 ; CHECK-DAG: .globl bar_h
2727 ; CHECK-DAG: .hidden bar_h
6565
6666 ; CHECK: .globl alias3
6767 ; CHECK: alias3 = notExported
68 @alias3 = dllexport alias void()* @alias
68 @alias3 = dllexport alias void()* @notExported
6969
7070 ; CHECK: .weak weak_alias
7171 ; CHECK: weak_alias = f1
8484
8585 ; CHECK: .globl _alias3
8686 ; CHECK: _alias3 = _notExported
87 @alias3 = dllexport alias void()* @alias
87 @alias3 = dllexport alias void()* @notExported
8888
8989 ; CHECK: .weak _weak_alias
9090 ; CHECK: _weak_alias = _f1
0 ; RUN: llvm-as < %s | llvm-dis | FileCheck %s
1
2 @v1 = global i32 0
3 ; CHECK: @v1 = global i32 0
4
5 @v2 = global [1 x i32] zeroinitializer
6 ; CHECK: @v2 = global [1 x i32] zeroinitializer
7
8 @v3 = alias i16, i32* @v1
9 ; CHECK: @v3 = alias i16, i32* @v1
10
11 @v4 = alias i32, [1 x i32]* @v2
12 ; CHECK: @v4 = alias i32, [1 x i32]* @v2
13
14 @v5 = alias addrspace(2) i32, i32* @v1
15 ; CHECK: @v5 = alias addrspace(2) i32, i32* @v1
16
17 @v6 = alias i16, i32* @v1
18 ; CHECK: @v6 = alias i16, i32* @v1
66 @bar = global i32 0
77 @foo1 = alias i32* @bar
88 @foo2 = alias i32* @bar
9 @foo3 = alias i32* @foo2
109
1110 %FunTy = type i32()
1211
1413 ret i32 0
1514 }
1615 @bar_f = alias weak_odr %FunTy* @foo_f
17 @bar_ff = alias i32()* @bar_f
1816
1917 @bar_i = alias internal i32* @bar
2018
21 @A = alias bitcast (i32* @bar to i64*)
19 @A = alias i64, i32* @bar
2220
2321 define i32 @test() {
2422 entry:
0 %foo = type { [8 x i8] }
11 %bar = type { [9 x i8] }
22
3 @zed = alias bitcast (void (%bar*)* @xyz to void (%foo*)*)
3 @zed = alias void (%foo*), void (%bar*)* @xyz
44
55 define void @xyz(%bar* %this) {
66 entry:
0 @zed = global i32 42
1 @foo = alias i32* @zed
2 @foo2 = alias i16, i32* @zed
0 @foo = alias i32* @bar
1 @bar = weak global i32 0
0 ; RUN: llvm-link %s %S/Inputs/alias.ll -S -o - | FileCheck %s
1 ; RUN: llvm-link %S/Inputs/alias.ll %s -S -o - | FileCheck %s
2
3 @foo = weak global i32 0
4 ; CHECK-DAG: @foo = alias i32* @zed
5
6 @bar = alias i32* @foo
7 ; CHECK-DAG: @bar = alias i32* @zed
8
9 @foo2 = weak global i32 0
10 ; CHECK-DAG: @foo2 = alias i16, i32* @zed
11
12 @bar2 = alias i32* @foo2
13 ; CHECK-DAG: @bar2 = alias i32* @zed
14
15 ; CHECK-DAG: @zed = global i32 42
0 ; 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
1313 ; DELETE: @zed = global i32 0
1414 ; DELETE: @zeda0 = alias i32* @zed
1515 ; DELETE-NEXT: @a0foo = alias i32* ()* @foo
16 ; DELETE-NEXT: @a0a0bar = alias void ()* @a0bar
16 ; DELETE-NEXT: @a0a0bar = alias void ()* @bar
1717 ; DELETE-NEXT: @a0bar = alias void ()* @bar
1818 ; DELETE: declare i32* @foo()
1919 ; DELETE: define void @bar() {
2424 ; ALIAS: @zed = external global i32
2525 ; ALIAS: @zeda0 = alias i32* @zed
2626
27 ; ALIASRE: @a0a0bar = alias void ()* @a0bar
27 ; ALIASRE: @a0a0bar = alias void ()* @bar
2828 ; ALIASRE: @a0bar = alias void ()* @bar
2929 ; ALIASRE: declare void @bar()
3030
3838 ret i32* @zeda0
3939 }
4040
41 @a0a0bar = alias void ()* @a0bar
41 @a0a0bar = alias void ()* @bar
4242
4343 @a0bar = alias void ()* @bar
4444
1010 @L1 = alias i32* @A
1111 ; CHECK: @L1 = alias i32* @A
1212
13 @L2 = alias internal i32* @L1
14 ; CHECK: @L2 = alias internal i32* @L1
13 @L2 = alias internal i32* @A
14 ; DEAD-NOT: @L2
1515
16 @L3 = alias i32* @L2
17 ; CHECK: @L3 = alias i32* @L2
16 @L3 = alias i32* @A
17 ; CHECK: @L3 = alias i32* @A
11
22 @g = global i32 0
33
4 @a = alias bitcast (i32* @g to i8*)
4 @a = alias i8, i32* @g
55
66 define void @f() {
77 %tmp = load i8* @a
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
11
2 @foo1 = alias void ()* @foo2
2 @foo1 = alias void ()* @bar2
33 ; CHECK: @foo1 = alias void ()* @bar2
44
5 @foo2 = alias void()* @bar1
5 @foo2 = alias void()* @bar2
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 bitcast (i32 (i32)* @func_i32 to float (float)*)
8 @alias_i32_to_f32 = alias float (float), i32 (i32)* @func_i32
99
1010 ; Test cast between vectors with same number of elements and bit sizes
11 @alias_v2i32_to_v2f32 = alias bitcast (<2 x i32> (<2 x i32>)* @func_v2i32 to <2 x float> (<2 x float>)*)
11 @alias_v2i32_to_v2f32 = alias <2 x float> (<2 x float>), <2 x i32> (<2 x i32>)* @func_v2i32
1212
1313 ; Test cast from vector to scalar with same number of bits
14 @alias_v2f32_to_i64 = alias bitcast (i64 (i64)* @func_i64 to <2 x float> (<2 x float>)*)
14 @alias_v2f32_to_i64 = alias <2 x float> (<2 x float>), i64 (i64)* @func_i64
1515
1616 ; Test cast from scalar to vector with same number of bits
17 @alias_i64_to_v2f32 = alias bitcast (<2 x float> (<2 x float>)* @func_v2f32 to i64 (i64)*)
17 @alias_i64_to_v2f32 = alias i64 (i64), <2 x float> (<2 x float>)* @func_v2f32
1818
1919 ; Test cast between vectors of pointers
20 @alias_v2i32p_to_v2i64p = alias bitcast (<2 x i32*> (<2 x i32*>)* @func_v2i32p to <2 x i64*> (<2 x i64*>)*)
20 @alias_v2i32p_to_v2i64p = alias <2 x i64*> (<2 x i64*>), <2 x i32*> (<2 x i32*>)* @func_v2i32p
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 bitcast (i64 (i64)* @func_i64 to float (float)*)
26 @alias_i64_to_f32 = alias float (float), i64 (i64)* @func_i64
2727
2828 ; Test cast between vectors with different bit sizes but the
2929 ; same number of elements
30 @alias_v2i64_to_v2f32 = alias bitcast (<2 x i64> (<2 x i64>)* @func_v2i64 to <2 x float> (<2 x float>)*)
30 @alias_v2i64_to_v2f32 = alias <2 x float> (<2 x float>), <2 x i64> (<2 x i64>)* @func_v2i64
3131
3232 ; Test cast between vectors with same number of bits and different
3333 ; numbers of elements
34 @alias_v2i32_to_v4f32 = alias bitcast (<2 x i32> (<2 x i32>)* @func_v2i32 to <4 x float> (<4 x float>)*)
34 @alias_v2i32_to_v4f32 = alias <4 x float> (<4 x float>), <2 x i32> (<2 x i32>)* @func_v2i32
3535
3636 ; Test cast between scalar and vector with different number of bits
37 @alias_i64_to_v4f32 = alias bitcast (<4 x float> (<4 x float>)* @func_v4f32 to i64 (i64)*)
37 @alias_i64_to_v4f32 = alias i64 (i64), <4 x float> (<4 x float>)* @func_v4f32
3838
3939 ; Test cast between vector and scalar with different number of bits
40 @alias_v4f32_to_i64 = alias bitcast (i64 (i64)* @func_i64 to <4 x float> (<4 x float>)*)
40 @alias_v4f32_to_i64 = alias <4 x float> (<4 x float>), i64 (i64)* @func_i64
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 bitcast (<2 x i32*> (<2 x i32*>)* @func_v2i32p to i64 (i64)*)
44 @alias_i64_to_v2i32p = alias i64 (i64), <2 x i32*> (<2 x i32*>)* @func_v2i32p
4545
4646 ; Test cast between vector of pointers and scalar with different number of bits
47 @alias_v4i32p_to_i64 = alias bitcast (i64 (i64)* @func_i64 to <4 x i32*> (<4 x i32*>)*)
47 @alias_v4i32p_to_i64 = alias <4 x i32*> (<4 x i32*>), i64 (i64)* @func_i64
4848
4949
5050
55 @B = alias i32* @A
66 ; CHECK: @B = alias internal i32* @A
77
8 @C = alias i32* @B
9 ; CHECK: @C = alias internal i32* @B
8 @C = alias i32* @A
9 ; CHECK: @C = alias internal i32* @A
1010
1111 define i32 @main() {
1212 %tmp = load i32* @C
+0
-6
test/Verifier/aliasing-chain.ll less more
None ; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s
1 ; CHECK: Aliasing chain should end with function or global variable
2 ; Test that alising chain does not create a cycle
3
4 @b1 = alias i32* @c1
5 @c1 = alias i32* @b1
+0
-10
test/Verifier/bitcast-alias-address-space.ll less more
None ; 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)*)
267267 EXPECT_DEATH(Global->replaceAllUsesWith(GEP),
268268 "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
269269 }
270
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 = new GlobalAlias(Int32Ty, GlobalValue::ExternalLinkage, "alias",
277 Global, M.get());
278 EXPECT_DEATH(Global->replaceAllUsesWith(GA),
279 "replaceAliasUseWith cannot form an alias cycle");
280 }
281
270282 #endif
271283 #endif
272284
3333 M, ST, false, GlobalValue::ExternalLinkage, 0, Name);
3434 }
3535
36 GlobalAlias *makeAlias(StringRef Name, GlobalValue *Aliasee) {
36 GlobalAlias *makeAlias(StringRef Name, GlobalObject *Aliasee) {
3737 return new GlobalAlias(Aliasee->getType()->getElementType(),
3838 GlobalValue::ExternalLinkage, Name, Aliasee,
3939 Aliasee->getParent());