llvm.org GIT mirror llvm / 266c7bb
Add a new "available_externally" linkage type. This is intended to support C99 inline, GNU extern inline, etc. Related bugzilla's include PR3517, PR3100, & PR2933. Nothing uses this yet, but it appears to work. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@68940 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 10 years ago
19 changed file(s) with 120 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
508508 'static' keyword in C.
509509
510510
511
available_externally:
512
513
514
Globals with "available_externally" linkage are never emitted
515 into the object file corresponding to the LLVM module. They exist to
516 allow inlining and other optimizations to take place given knowledge of the
517 definition of the global, which is known to be somewhere outside the module.
518 Globals with available_externally linkage are allowed to be discarded
519 at will, and are otherwise the same as linkonce_odr. This linkage
520 type is only allowed on definitions, not declarations.
521
511522
linkonce:
512523
513524
Globals with "linkonce" linkage are merged with other globals of
553564
554565
linkonce_odr:
555566
weak_odr:
556
Some languages allow inequivalent globals to be merged, such as two
567
Some languages allow differing globals to be merged, such as two
557568 functions with different semantics. Other languages, such as C++,
558569 ensure that only equivalent globals are ever merged (the "one definition
559 rule" - odr). Such languages can use the linkonce_odr
570 rule" - "ODR"). Such languages can use the linkonce_odr
560571 and weak_odr linkage types to indicate that the global will only
561572 be merged with equivalent globals. These linkage types are otherwise the
562573 same as their non-odr versions.
2323
2424 namespace llvm {
2525
26 // FIXME: This pass should declare that the pass does not invalidate any LLVM
27 // passes.
2628 struct MachineFunctionPass : public FunctionPass {
27
2829 explicit MachineFunctionPass(intptr_t ID) : FunctionPass(ID) {}
2930 explicit MachineFunctionPass(void *ID) : FunctionPass(ID) {}
3031
3536 virtual bool runOnMachineFunction(MachineFunction &MF) = 0;
3637
3738 public:
38 // FIXME: This pass should declare that the pass does not invalidate any LLVM
39 // passes.
40 bool runOnFunction(Function &F) {
41 return runOnMachineFunction(MachineFunction::get(&F));
42 }
43
44 private:
45 virtual void virtfn(); // out of line virtual fn to give class a home.
39 bool runOnFunction(Function &F);
4640 };
4741
4842 } // End llvm namespace
3030 /// @brief An enumeration for the kinds of linkage for global values.
3131 enum LinkageTypes {
3232 ExternalLinkage = 0,///< Externally visible function
33 AvailableExternallyLinkage, ///< Available for inspection, not emission.
3334 LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
3435 LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
3536 WeakAnyLinkage, ///< Keep one copy of named function when linking (weak)
108109 }
109110
110111 bool hasExternalLinkage() const { return Linkage == ExternalLinkage; }
112 bool hasAvailableExternallyLinkage() const {
113 return Linkage == AvailableExternallyLinkage;
114 }
111115 bool hasLinkOnceLinkage() const {
112116 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
113117 }
118122 bool hasInternalLinkage() const { return Linkage == InternalLinkage; }
119123 bool hasPrivateLinkage() const { return Linkage == PrivateLinkage; }
120124 bool hasLocalLinkage() const {
121 return Linkage == InternalLinkage || Linkage == PrivateLinkage;
125 return Linkage == InternalLinkage || Linkage == PrivateLinkage ||
126 Linkage == AvailableExternallyLinkage;
122127 }
123128 bool hasDLLImportLinkage() const { return Linkage == DLLImportLinkage; }
124129 bool hasDLLExportLinkage() const { return Linkage == DLLExportLinkage; }
140145 }
141146
142147 /// isWeakForLinker - Whether the definition of this global may be replaced at
143 /// link time, whether the replacement is equivalent to the original or not.
148 /// link time.
144149 bool isWeakForLinker() const {
145 return (Linkage == WeakAnyLinkage ||
150 return (Linkage == AvailableExternallyLinkage ||
151 Linkage == WeakAnyLinkage ||
146152 Linkage == WeakODRLinkage ||
147153 Linkage == LinkOnceAnyLinkage ||
148154 Linkage == LinkOnceODRLinkage ||
486486
487487 KEYWORD(private);
488488 KEYWORD(internal);
489 KEYWORD(available_externally);
489490 KEYWORD(linkonce);
490491 KEYWORD(linkonce_odr);
491492 KEYWORD(weak);
763763 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
764764 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
765765 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
766 case lltok::kw_available_externally:
767 Res = GlobalValue::AvailableExternallyLinkage;
768 break;
766769 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
767770 case lltok::kw_dllexport: Res = GlobalValue::DLLExportLinkage; break;
768771 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
3636 kw_global, kw_constant,
3737
3838 kw_private, kw_internal, kw_linkonce, kw_linkonce_odr, kw_weak, kw_weak_odr,
39 kw_appending, kw_dllimport, kw_dllexport, kw_common,
39 kw_appending, kw_dllimport, kw_dllexport, kw_common,kw_available_externally,
4040 kw_default, kw_hidden, kw_protected,
4141 kw_extern_weak,
4242 kw_external, kw_thread_local,
6969 case 9: return GlobalValue::PrivateLinkage;
7070 case 10: return GlobalValue::WeakODRLinkage;
7171 case 11: return GlobalValue::LinkOnceODRLinkage;
72 case 12: return GlobalValue::AvailableExternallyLinkage;
7273 }
7374 }
7475
286286 case GlobalValue::PrivateLinkage: return 9;
287287 case GlobalValue::WeakODRLinkage: return 10;
288288 case GlobalValue::LinkOnceODRLinkage: return 11;
289 case GlobalValue::AvailableExternallyLinkage: return 12;
289290 }
290291 }
291292
442442 }
443443
444444 // Ignore debug and non-emitted data.
445 if (GV->getSection() == "llvm.metadata") return true;
445 if (GV->getSection() == "llvm.metadata" ||
446 GV->hasAvailableExternallyLinkage())
447 return true;
446448
447449 if (!GV->hasAppendingLinkage()) return false;
448450
35183518 ///
35193519 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
35203520 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3521
3522 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
3523 "Should not emit 'available externally' functions at all");
35213524
35223525 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
35233526
3737 static AnnotationID MF_AID(
3838 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
3939
40 // Out of line virtual function to home classes.
41 void MachineFunctionPass::virtfn() {}
40 bool MachineFunctionPass::runOnFunction(Function &F) {
41 // Do not codegen any 'available_externally' functions at all, they have
42 // definitions outside the translation unit.
43 if (F.hasAvailableExternallyLinkage())
44 return false;
45
46 return runOnMachineFunction(MachineFunction::get(&F));
47 }
4248
4349 namespace {
4450 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
479479 } else if (Src->isWeakForLinker()) {
480480 // At this point we know that Dest has LinkOnce, External*, Weak, Common,
481481 // or DLL* linkage.
482 if ((Dest->hasLinkOnceLinkage() &&
483 (Src->hasWeakLinkage() || Src->hasCommonLinkage())) ||
484 Dest->hasExternalWeakLinkage()) {
482 if (Dest->hasExternalWeakLinkage() ||
483 Dest->hasAvailableExternallyLinkage() ||
484 (Dest->hasLinkOnceLinkage() &&
485 (Src->hasWeakLinkage() || Src->hasCommonLinkage()))) {
485486 LinkFromSrc = true;
486487 LT = Src->getLinkage();
487488 } else {
293293 Out << "GlobalValue::InternalLinkage"; break;
294294 case GlobalValue::PrivateLinkage:
295295 Out << "GlobalValue::PrivateLinkage"; break;
296 case GlobalValue::AvailableExternallyLinkage:
297 Out << "GlobalValue::AvailableExternallyLinkage "; break;
296298 case GlobalValue::LinkOnceAnyLinkage:
297299 Out << "GlobalValue::LinkOnceAnyLinkage "; break;
298300 case GlobalValue::LinkOnceODRLinkage:
12241224 switch (LT) {
12251225 case GlobalValue::PrivateLinkage: Out << "private "; break;
12261226 case GlobalValue::InternalLinkage: Out << "internal "; break;
1227 case GlobalValue::AvailableExternallyLinkage:
1228 Out << "available_externally ";
1229 break;
12271230 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
12281231 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
12291232 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
0 ; RUN: llvm-as < %s | llc | not grep test_
1
2 ; test_function should not be emitted to the .s file.
3 define available_externally i32 @test_function() {
4 ret i32 4
5 }
6
7 ; test_global should not be emitted to the .s file.
8 @test_global = available_externally global i32 4
9
0 ; RUN: llvm-as < %s | opt -globaldce | llvm-dis | not grep test_
1
2 ; test_function should not be emitted to the .s file.
3 define available_externally i32 @test_function() {
4 ret i32 4
5 }
6
7 ; test_global should not be emitted to the .s file.
8 @test_global = available_externally global i32 4
9
0 ; RUN: llvm-as < %s | opt -inline -constprop | llvm-dis > %t
1 ; RUN: not grep test_function %t
2 ; RUN: grep {ret i32 5} %t
3
4
5 ; test_function should not be emitted to the .s file.
6 define available_externally i32 @test_function() {
7 ret i32 4
8 }
9
10
11 define i32 @result() {
12 %A = call i32 @test_function()
13 %B = add i32 %A, 1
14 ret i32 %B
15 }
0 ; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep {ret i32 10}
1
2 @g1 = available_externally constant i32 1
3 @g2 = linkonce_odr constant i32 2
4 @g3 = weak_odr constant i32 3
5 @g4 = internal constant i32 4
6
7 define i32 @test() {
8 %A = load i32* @g1
9 %B = load i32* @g2
10 %C = load i32* @g3
11 %D = load i32* @g4
12
13 %a = add i32 %A, %B
14 %b = add i32 %a, %C
15 %c = add i32 %b, %D
16 ret i32 %c
17 }
18
6868 }
6969
7070 static char TypeCharForSymbol(GlobalValue &GV) {
71 /* FIXME: what to do with private linkage? */
7271 if (GV.isDeclaration()) return 'U';
7372 if (GV.hasLinkOnceLinkage()) return 'C';
7473 if (GV.hasCommonLinkage()) return 'C';
8685 }
8786
8887 static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
88 // Private linkage and available_externally linkage don't exist in symtab.
89 if (GV.hasPrivateLinkage() || GV.hasAvailableExternallyLinkage()) return;
90
8991 const std::string SymbolAddrStr = " "; // Not used yet...
90 char TypeChar = TypeCharForSymbol (GV);
92 char TypeChar = TypeCharForSymbol(GV);
9193 if ((TypeChar != 'U') && UndefinedOnly)
9294 return;
9395 if ((TypeChar == 'U') && DefinedOnly)
9597 if (GV.hasLocalLinkage () && ExternalOnly)
9698 return;
9799 if (OutputFormat == posix) {
98 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
100 std::cout << GV.getName () << " " << TypeCharForSymbol(GV) << " "
99101 << SymbolAddrStr << "\n";
100102 } else if (OutputFormat == bsd) {
101 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
103 std::cout << SymbolAddrStr << " " << TypeCharForSymbol(GV) << " "
102104 << GV.getName () << "\n";
103105 } else if (OutputFormat == sysv) {
104106 std::string PaddedName (GV.getName ());
105107 while (PaddedName.length () < 20)
106108 PaddedName += " ";
107109 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
108 << TypeCharForSymbol (GV)
110 << TypeCharForSymbol(GV)
109111 << " | | | |\n";
110112 }
111113 }
121123 << "Name Value Class Type"
122124 << " Size Line Section\n";
123125 }
124 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
125 std::for_each (M->global_begin (), M->global_end (),
126 std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
127 std::for_each (M->global_begin(), M->global_end(),
126128 DumpSymbolNameForGlobalValue);
127 std::for_each (M->alias_begin (), M->alias_end (),
129 std::for_each (M->alias_begin(), M->alias_end(),
128130 DumpSymbolNameForGlobalValue);
129131 }
130132