llvm.org GIT mirror llvm / 4b678bf
Bring r226038 back. No change in this commit, but clang was changed to also produce trivial comdats when needed. Original message: Don't create new comdats in CodeGen. This patch stops the implicit creation of comdats during codegen. Clang now sets the comdat explicitly when it is required. With this patch clang and gcc now produce the same result in pr19848. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226467 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
15 changed file(s) with 64 addition(s) and 138 deletion(s). Raw diff Collapse all Expand all
155155 return false;
156156 }
157157
158 static bool hasImplicitComdat(size_t Val) {
159 switch (Val) {
160 default:
161 return false;
162 case 1: // Old WeakAnyLinkage
163 case 4: // Old LinkOnceAnyLinkage
164 case 10: // Old WeakODRLinkage
165 case 11: // Old LinkOnceODRLinkage
166 return true;
167 }
168 }
169
158170 static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
159171 switch (Val) {
160172 default: // Map unknown/new linkages to external
161173 case 0:
162174 return GlobalValue::ExternalLinkage;
163 case 1:
164 return GlobalValue::WeakAnyLinkage;
165175 case 2:
166176 return GlobalValue::AppendingLinkage;
167177 case 3:
168178 return GlobalValue::InternalLinkage;
169 case 4:
170 return GlobalValue::LinkOnceAnyLinkage;
171179 case 5:
172180 return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
173181 case 6:
178186 return GlobalValue::CommonLinkage;
179187 case 9:
180188 return GlobalValue::PrivateLinkage;
181 case 10:
182 return GlobalValue::WeakODRLinkage;
183 case 11:
184 return GlobalValue::LinkOnceODRLinkage;
185189 case 12:
186190 return GlobalValue::AvailableExternallyLinkage;
187191 case 13:
190194 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
191195 case 15:
192196 return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
197 case 1: // Old value with implicit comdat.
198 case 16:
199 return GlobalValue::WeakAnyLinkage;
200 case 10: // Old value with implicit comdat.
201 case 17:
202 return GlobalValue::WeakODRLinkage;
203 case 4: // Old value with implicit comdat.
204 case 18:
205 return GlobalValue::LinkOnceAnyLinkage;
206 case 11: // Old value with implicit comdat.
207 case 19:
208 return GlobalValue::LinkOnceODRLinkage;
193209 }
194210 }
195211
11171133 Value *V = ValueList[ValueID];
11181134
11191135 V->setName(StringRef(ValueName.data(), ValueName.size()));
1136 if (auto *GO = dyn_cast(V)) {
1137 if (GO->getComdat() == reinterpret_cast(1))
1138 GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
1139 }
11201140 ValueName.clear();
11211141 break;
11221142 }
21392159 Ty = cast(Ty)->getElementType();
21402160
21412161 bool isConstant = Record[1];
2142 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(Record[3]);
2162 uint64_t RawLinkage = Record[3];
2163 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
21432164 unsigned Alignment = (1 << Record[4]) >> 1;
21442165 std::string Section;
21452166 if (Record[5]) {
21772198 if (Record.size() > 10)
21782199 NewGV->setDLLStorageClass(GetDecodedDLLStorageClass(Record[10]));
21792200 else
2180 UpgradeDLLImportExportLinkage(NewGV, Record[3]);
2201 UpgradeDLLImportExportLinkage(NewGV, RawLinkage);
21812202
21822203 ValueList.push_back(NewGV);
21832204
21852206 if (unsigned InitID = Record[2])
21862207 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
21872208
2188 if (Record.size() > 11)
2209 if (Record.size() > 11) {
21892210 if (unsigned ComdatID = Record[11]) {
21902211 assert(ComdatID <= ComdatList.size());
21912212 NewGV->setComdat(ComdatList[ComdatID - 1]);
21922213 }
2214 } else if (hasImplicitComdat(RawLinkage)) {
2215 NewGV->setComdat(reinterpret_cast(1));
2216 }
21932217 break;
21942218 }
21952219 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
22132237
22142238 Func->setCallingConv(static_cast(Record[1]));
22152239 bool isProto = Record[2];
2216 Func->setLinkage(getDecodedLinkage(Record[3]));
2240 uint64_t RawLinkage = Record[3];
2241 Func->setLinkage(getDecodedLinkage(RawLinkage));
22172242 Func->setAttributes(getAttributes(Record[4]));
22182243
22192244 Func->setAlignment((1 << Record[5]) >> 1);
22412266 if (Record.size() > 11)
22422267 Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11]));
22432268 else
2244 UpgradeDLLImportExportLinkage(Func, Record[3]);
2245
2246 if (Record.size() > 12)
2269 UpgradeDLLImportExportLinkage(Func, RawLinkage);
2270
2271 if (Record.size() > 12) {
22472272 if (unsigned ComdatID = Record[12]) {
22482273 assert(ComdatID <= ComdatList.size());
22492274 Func->setComdat(ComdatList[ComdatID - 1]);
22502275 }
2276 } else if (hasImplicitComdat(RawLinkage)) {
2277 Func->setComdat(reinterpret_cast(1));
2278 }
22512279
22522280 if (Record.size() > 13 && Record[13] != 0)
22532281 FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1));
479479 case GlobalValue::ExternalLinkage:
480480 return 0;
481481 case GlobalValue::WeakAnyLinkage:
482 return 1;
482 return 16;
483483 case GlobalValue::AppendingLinkage:
484484 return 2;
485485 case GlobalValue::InternalLinkage:
486486 return 3;
487487 case GlobalValue::LinkOnceAnyLinkage:
488 return 4;
488 return 18;
489489 case GlobalValue::ExternalWeakLinkage:
490490 return 7;
491491 case GlobalValue::CommonLinkage:
493493 case GlobalValue::PrivateLinkage:
494494 return 9;
495495 case GlobalValue::WeakODRLinkage:
496 return 10;
496 return 17;
497497 case GlobalValue::LinkOnceODRLinkage:
498 return 11;
498 return 19;
499499 case GlobalValue::AvailableExternallyLinkage:
500500 return 12;
501501 }
628628 Log2_32_Ceil(MaxGlobalType+1)));
629629 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
630630 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
631 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage.
631 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
632632 if (MaxAlignment == 0) // Alignment.
633633 Abbv->Add(BitCodeAbbrevOp(0));
634634 else {
256256
257257 // If this global is linkonce/weak and the target handles this by emitting it
258258 // into a 'uniqued' section name, create and return the section now.
259 if ((GV->isWeakForLinker() || EmitUniquedSection || GV->hasComdat()) &&
260 !Kind.isCommon()) {
259 if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) {
261260 StringRef Prefix = getSectionPrefixForGlobal(Kind);
262261
263262 SmallString<128> Name(Prefix);
265264
266265 StringRef Group = "";
267266 unsigned Flags = getELFSectionFlags(Kind);
268 if (GV->isWeakForLinker() || GV->hasComdat()) {
269 if (const Comdat *C = getELFComdat(GV))
270 Group = C->getName();
271 else
272 Group = Name.substr(Prefix.size());
267 if (const Comdat *C = getELFComdat(GV)) {
273268 Flags |= ELF::SHF_GROUP;
269 Group = C->getName();
274270 }
275271
276272 return getContext().getELFSection(Name.str(),
800796 unsigned Characteristics = getCOFFSectionFlags(Kind);
801797 StringRef Name = GV->getSection();
802798 StringRef COMDATSymName = "";
803 if ((GV->isWeakForLinker() || GV->hasComdat()) && !Kind.isCommon()) {
799 if (GV->hasComdat()) {
804800 Selection = getSelectionForCOFF(GV);
805801 const GlobalValue *ComdatGV;
806802 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
847843 else
848844 EmitUniquedSection = TM.getDataSections();
849845
850 // If this global is linkonce/weak and the target handles this by emitting it
851 // into a 'uniqued' section name, create and return the section now.
852 // Section names depend on the name of the symbol which is not feasible if the
853 // symbol has private linkage.
854 if ((GV->isWeakForLinker() || EmitUniquedSection || GV->hasComdat()) &&
855 !Kind.isCommon()) {
846 if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) {
856847 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
857848 unsigned Characteristics = getCOFFSectionFlags(Kind);
858849
105105 OutContext));
106106 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
107107 GV->hasCommonLinkage()) {
108 // TODO Use COMDAT groups for LinkOnceLinkage
109108 OutStreamer.EmitSymbolAttribute(SymGlob, MCSA_Weak);
110109 }
111110 }
140139 emitArrayBound(GVSym, GV);
141140 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
142141
143 // TODO Use COMDAT groups for LinkOnceLinkage
144142 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
145143 GV->hasCommonLinkage())
146144 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Weak);
3232 ; CHECK: @available_externally.var = available_externally constant i32 0{{$}}
3333
3434 @linkonce.var = linkonce constant i32 0
35 ; CHECK: @linkonce.var = linkonce constant i32 0{{$}}
35 ; CHECK: @linkonce.var = linkonce constant i32 0, comdat{{$}}
3636
3737 @weak.var = weak constant i32 0
38 ; CHECK: @weak.var = weak constant i32 0{{$}}
38 ; CHECK: @weak.var = weak constant i32 0, comdat{{$}}
3939
4040 @linkonce_odr.var = linkonce_odr constant i32 0
41 ; CHECK: @linkonce_odr.var = linkonce_odr constant i32 0{{$}}
41 ; CHECK: @linkonce_odr.var = linkonce_odr constant i32 0, comdat{{$}}
4242
4343 @linkonce_odr_auto_hide.var = linkonce_odr_auto_hide constant i32 0
4444 ; CHECK: @linkonce_odr_auto_hide.var = constant i32 0{{$}}
8989 }
9090
9191 define linkonce void @linkonce()
92 ; CHECK: define linkonce void @linkonce() {
92 ; CHECK: define linkonce void @linkonce() comdat {
9393 {
9494 ret void
9595 }
9696
9797 define weak void @weak()
98 ; CHECK: define weak void @weak() {
98 ; CHECK: define weak void @weak() comdat {
9999 {
100100 ret void
101101 }
102102
103103 define linkonce_odr void @linkonce_odr()
104 ; CHECK: define linkonce_odr void @linkonce_odr() {
104 ; CHECK: define linkonce_odr void @linkonce_odr() comdat {
105105 {
106106 ret void
107107 }
+0
-16
test/CodeGen/ARM/odr_comdat.ll less more
None ; RUN: llc < %s -mtriple=arm-linux-gnueabi | FileCheck %s -check-prefix=ARMGNUEABI
1
2 ; Checking that a comdat group gets generated correctly for a static member
3 ; of instantiated C++ templates.
4 ; see http://sourcery.mentor.com/public/cxx-abi/abi.html#vague-itemplate
5 ; section 5.2.6 Instantiated templates
6 ; "Any static member data object is emitted in a COMDAT identified by its mangled
7 ; name, in any object file with a reference to its name symbol."
8
9 ; Case 1: variable is not explicitly initialized, and ends up in a .bss section
10 ; ARMGNUEABI: .section .bss._ZN1CIiE1iE,"aGw",%nobits,_ZN1CIiE1iE,comdat
11 @_ZN1CIiE1iE = weak_odr global i32 0, align 4
12
13 ; Case 2: variable is explicitly initialized, and ends up in a .data section
14 ; ARMGNUEABI: .section .data._ZN1CIiE1jE,"aGw",%progbits,_ZN1CIiE1jE,comdat
15 @_ZN1CIiE1jE = weak_odr global i32 12, align 4
1515 ret void
1616 }
1717
18 ; CHECK: .section .text.test3,"axG",%progbits,test3,comdat
18 ; CHECK: .text
1919 ; CHECK: .weak test3
2020 ; CHECK: .type test3,%function
2121 define linkonce_odr void @test3() {
1616 ret void
1717 }
1818
19 ; CHECK: .section .text,"xr",discard,lnk1
2019 ; CHECK: .globl lnk1
2120 define linkonce_odr dllexport void @lnk1() {
2221 ret void
2322 }
2423
25 ; CHECK: .section .text,"xr",discard,lnk2
2624 ; CHECK: .globl lnk2
2725 define linkonce_odr dllexport void @lnk2() alwaysinline {
2826 ret void
2927 }
3028
31 ; CHECK: .section .text,"xr",discard,weak1
3229 ; CHECK: .globl weak1
3330 define weak_odr dllexport void @weak1() {
3431 ret void
4643 ; CHECK: .comm Var3
4744 @Var3 = common dllexport global i32 0, align 4
4845
49 ; CHECK: .section .data,"wd",discard,WeakVar1
5046 ; CHECK: .globl WeakVar1
5147 @WeakVar1 = weak_odr dllexport global i32 1, align 4
5248
53 ; CHECK: .section .rdata,"rd",discard,WeakVar2
5449 ; CHECK: .globl WeakVar2
5550 @WeakVar2 = weak_odr dllexport unnamed_addr constant i32 1
5651
3535 ret void
3636 }
3737
38 ; CHECK: .section .text,"xr",discard,_lnk1
3938 ; CHECK: .globl _lnk1
4039 define linkonce_odr dllexport void @lnk1() {
4140 ret void
4241 }
4342
44 ; CHECK: .section .text,"xr",discard,_lnk2
4543 ; CHECK: .globl _lnk2
4644 define linkonce_odr dllexport void @lnk2() alwaysinline {
4745 ret void
4846 }
4947
50 ; CHECK: .section .text,"xr",discard,_weak1
5148 ; CHECK: .globl _weak1
5249 define weak_odr dllexport void @weak1() {
5350 ret void
6562 ; CHECK: .comm _Var3
6663 @Var3 = common dllexport global i32 0, align 4
6764
68 ; CHECK: .section .data,"wd",discard,_WeakVar1
6965 ; CHECK: .globl _WeakVar1
7066 @WeakVar1 = weak_odr dllexport global i32 1, align 4
7167
72 ; CHECK: .section .rdata,"rd",discard,_WeakVar2
7368 ; CHECK: .globl _WeakVar2
7469 @WeakVar2 = weak_odr dllexport unnamed_addr constant i32 1
7570
8484 @"foo bar" = linkonce global i32 42
8585
8686 ; LINUX: .type "foo bar",@object
87 ; LINUX: .section ".data.foo bar","aGw",@progbits,"foo bar",comdat
8887 ; LINUX: .weak "foo bar"
8988 ; LINUX: "foo bar":
9089
9796 @G6 = weak_odr unnamed_addr constant [1 x i8] c"\01"
9897
9998 ; LINUX: .type G6,@object
100 ; LINUX: .section .rodata.G6,"aG",@progbits,G6,comdat
10199 ; LINUX: .weak G6
102100 ; LINUX: G6:
103101 ; LINUX: .byte 1
+0
-16
test/CodeGen/X86/odr_comdat.ll less more
None ; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s -check-prefix=X86LINUX
1
2 ; Checking that a comdat group gets generated correctly for a static member
3 ; of instantiated C++ templates.
4 ; see http://sourcery.mentor.com/public/cxx-abi/abi.html#vague-itemplate
5 ; section 5.2.6 Instantiated templates
6 ; "Any static member data object is emitted in a COMDAT identified by its mangled
7 ; name, in any object file with a reference to its name symbol."
8
9 ; Case 1: variable is not explicitly initialized, and ends up in a .bss section
10 ; X86LINUX: .section .bss._ZN1CIiE1iE,"aGw",@nobits,_ZN1CIiE1iE,comdat
11 @_ZN1CIiE1iE = weak_odr global i32 0, align 4
12
13 ; Case 2: variable is explicitly initialized, and ends up in a .data section
14 ; X86LINUX: .section .data._ZN1CIiE1jE,"aGw",@progbits,_ZN1CIiE1jE,comdat
15 @_ZN1CIiE1jE = weak_odr global i32 12, align 4
99
1010 declare void @_Z3bari(i32)
1111
12 ; CHECK-LINUX: .text._Z3fooILi1EEvi,"axG",@progbits,_Z3fooILi1EEvi,comdat
12 ; CHECK-LINUX: _Z3fooILi1EEvi:
1313 define linkonce void @_Z3fooILi1EEvi(i32 %Y) nounwind {
1414 entry:
1515 ; CHECK: L0$pb
44 @"\01?thingy@@3Ufoo@@B" = global %struct.foo zeroinitializer, align 4
55 ; CHECK: .bss
66
7 @thingy_linkonce = linkonce_odr global %struct.foo zeroinitializer, align 4
7 $thingy_linkonce = comdat any
8 @thingy_linkonce = linkonce_odr global %struct.foo zeroinitializer, comdat, align 4
89 ; CHECK: .section .bss,"wb",discard,_thingy_linkonce
77 ; CHECK: .section .CRT$XLB,"rd"
88
99 @weak_array = weak_odr unnamed_addr constant [1 x i8*] [i8* bitcast (void ()* @f to i8*)]
10 ; CHECK: .section .rdata,"rd",discard,weak_array
10 ; CHECK: .section .rdata,"rd"
+0
-48
test/MC/COFF/weak-symbol.ll less more
None ; Test that weak functions and globals are placed into selectany COMDAT
1 ; sections with the mangled name as suffix. Ensure that the weak linkage
2 ; type is not ignored by the backend if the section was specialized.
3 ;
4 ; RUN: llc -mtriple=i686-pc-win32 %s -o - | FileCheck %s --check-prefix=X86
5 ; RUN: llc -mtriple=i686-pc-mingw32 %s -o - | FileCheck %s --check-prefix=X86
6 ; RUN: llc -mtriple=x86_64-pc-win32 %s -o - | FileCheck %s --check-prefix=X64
7 ; RUN: llc -mtriple=x86_64-pc-mingw32 %s -o - | FileCheck %s --check-prefix=X64
8
9 ; Mangled function
10 ; X86: .section .text,"xr",discard,__Z3foo
11 ; X86: .globl __Z3foo
12 ;
13 ; X64: .section .text,"xr",discard,_Z3foo
14 ; X64: .globl _Z3foo
15 define weak void @_Z3foo() {
16 ret void
17 }
18
19 ; Unmangled function
20 ; X86: .section .sect,"xr",discard,_f
21 ; X86: .globl _f
22 ;
23 ; X64: .section .sect,"xr",discard,f
24 ; X64: .globl f
25 define weak void @f() section ".sect" {
26 ret void
27 }
28
29 ; Weak global
30 ; X86: .section .data,"rd",discard,_a
31 ; X86: .globl _a
32 ; X86: .zero 12
33 ;
34 ; X64: .section .data,"rd",discard,a
35 ; X64: .globl a
36 ; X64: .zero 12
37 @a = weak unnamed_addr constant { i32, i32, i32 } { i32 0, i32 0, i32 0}, section ".data"
38
39 ; X86: .section .tls$,"wd",discard,_b
40 ; X86: .globl _b
41 ; X86: .long 0
42 ;
43 ; X64: .section .tls$,"wd",discard,b
44 ; X64: .globl b
45 ; X64: .long 0
46
47 @b = weak_odr thread_local global i32 0, align 4