llvm.org GIT mirror llvm / 5e563c3
- Introduces versioning macro LLVM_LTO_VERSION - Communicate symbol visibility - Communicate code generation model git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@46033 91177308-0d34-0410-b5e6-96231b3b80d8 Devang Patel 11 years ago
2 changed file(s) with 76 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
1818 #include
1919 #include
2020 #include
21
22 #define LLVM_LTO_VERSION 2
2123
2224 namespace llvm {
2325
4446 LTOInternalLinkage // Rename collisions when linking (static functions)
4547 };
4648
49 enum LTOVisibilityTypes {
50 LTODefaultVisibility = 0, ///< The GV is visible
51 LTOHiddenVisibility, ///< The GV is hidden
52 LTOProtectedVisibility ///< The GV is protected
53 };
54
55
56 enum LTOCodeGenModel {
57 LTO_CGM_Static,
58 LTO_CGM_Dynamic,
59 LTO_CGM_DynamicNoPIC
60 };
61
4762 /// This class represents LLVM symbol information without exposing details
4863 /// of LLVM global values. It encapsulates symbol linkage information. This
4964 /// is typically used in hash_map where associated name identifies the
5368 public:
5469
5570 LTOLinkageTypes getLinkage() const { return linkage; }
71 LTOVisibilityTypes getVisibility() const { return visibility; }
5672 void mayBeNotUsed();
5773
58 LLVMSymbol (enum LTOLinkageTypes lt, GlobalValue *g, const std::string &n,
59 const std::string &m, int a) : linkage(lt), gv(g), name(n),
74 LLVMSymbol (enum LTOLinkageTypes lt, enum LTOVisibilityTypes vis,
75 GlobalValue *g, const std::string &n,
76 const std::string &m, int a) : linkage(lt), visibility(vis),
77 gv(g), name(n),
6078 mangledName(m), alignment(a) {}
6179
6280 const char *getName() { return name.c_str(); }
6583
6684 private:
6785 enum LTOLinkageTypes linkage;
86 enum LTOVisibilityTypes visibility;
6887 GlobalValue *gv;
6988 std::string name;
7089 std::string mangledName;
90109 NameToSymbolMap &,
91110 std::set &) = 0;
92111 virtual enum LTOStatus optimizeModules(const std::string &,
93 std::vector &,
94 std::string &, bool,
95 const char *) = 0;
112 std::vector &exportList,
113 std::string &targetTriple,
114 bool saveTemps, const char *) = 0;
96115 virtual void getTargetTriple(const std::string &, std::string &) = 0;
97116 virtual void removeModule (const std::string &InputFilename) = 0;
117 virtual void setCodeGenModel(LTOCodeGenModel CGM) = 0;
98118 virtual void printVersion () = 0;
99119 virtual ~LinkTimeOptimizer() = 0;
100120 };
114134 std::set &references);
115135 enum LTOStatus optimizeModules(const std::string &OutputFilename,
116136 std::vector &exportList,
117 std::string &targetTriple, bool saveTemps,
118 const char *);
137 std::string &targetTriple,
138 bool saveTemps, const char *);
119139 void getTargetTriple(const std::string &InputFilename,
120140 std::string &targetTriple);
121141 void removeModule (const std::string &InputFilename);
122142 void printVersion();
123143
144 void setCodeGenModel(LTOCodeGenModel CGM) {
145 CGModel = CGM;
146 }
147
124148 // Constructors and destructors
125 LTO() {
149 LTO() : Target(NULL), CGModel(LTO_CGM_Dynamic) {
126150 /// TODO: Use Target info, it is available at this time.
127 Target = NULL;
128151 }
129152 ~LTO();
130153
139162 NameToSymbolMap allSymbols;
140163 NameToModuleMap allModules;
141164 TargetMachine *Target;
165 LTOCodeGenModel CGModel;
142166 };
143167
144168 } // End llvm namespace
147171 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
148172 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
149173 extern "C"
150 llvm::LinkTimeOptimizer *createLLVMOptimizer();
174 llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION = LLVM_LTO_VERSION);
151175
152176 #endif
4444 using namespace llvm;
4545
4646 extern "C"
47 llvm::LinkTimeOptimizer *createLLVMOptimizer()
48 {
47 llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION)
48 {
49 // Linker records LLVM_LTO_VERSION based on llvm optimizer available
50 // during linker build. Match linker's recorded LTO VERSION number
51 // with installed llvm optimizer version. If these numbers do not match
52 // then linker may not be able to use llvm optimizer dynamically.
53 if (VERSION != LLVM_LTO_VERSION)
54 return NULL;
55
4956 llvm::LTO *l = new llvm::LTO();
5057 return l;
5158 }
7380 return lt;
7481 }
7582
83 // MAP LLVM VisibilityType to LTO VisibilityType
84 static LTOVisibilityTypes
85 getLTOVisibilityType(GlobalValue *v)
86 {
87 LTOVisibilityTypes vis;
88 if (v->hasHiddenVisibility())
89 vis = LTOHiddenVisibility;
90 else if (v->hasProtectedVisibility())
91 vis = LTOProtectedVisibility;
92 else
93 vis = LTODefaultVisibility;
94 return vis;
95 }
96
7697 // Find exeternal symbols referenced by VALUE. This is a recursive function.
7798 static void
7899 findExternalRefs(Value *value, std::set &references,
163184 modules.push_back(m);
164185
165186 for (Module::iterator f = m->begin(), e = m->end(); f != e; ++f) {
166
167187 LTOLinkageTypes lt = getLTOLinkageType(f);
168
188 LTOVisibilityTypes vis = getLTOVisibilityType(f);
169189 if (!f->isDeclaration() && lt != LTOInternalLinkage
170190 && strncmp (f->getName().c_str(), "llvm.", 5)) {
171191 int alignment = ( 16 > f->getAlignment() ? 16 : f->getAlignment());
172 LLVMSymbol *newSymbol = new LLVMSymbol(lt, f, f->getName(),
192 LLVMSymbol *newSymbol = new LLVMSymbol(lt, vis, f, f->getName(),
173193 mangler.getValueName(f),
174194 Log2_32(alignment));
175195 symbols[newSymbol->getMangledName()] = newSymbol;
179199 // Collect external symbols referenced by this function.
180200 for (Function::iterator b = f->begin(), fe = f->end(); b != fe; ++b)
181201 for (BasicBlock::iterator i = b->begin(), be = b->end();
182 i != be; ++i)
202 i != be; ++i) {
183203 for (unsigned count = 0, total = i->getNumOperands();
184204 count != total; ++count)
185205 findExternalRefs(i->getOperand(count), references, mangler);
206 }
186207 }
187208
188209 for (Module::global_iterator v = m->global_begin(), e = m->global_end();
189210 v != e; ++v) {
190211 LTOLinkageTypes lt = getLTOLinkageType(v);
212 LTOVisibilityTypes vis = getLTOVisibilityType(v);
191213 if (!v->isDeclaration() && lt != LTOInternalLinkage
192214 && strncmp (v->getName().c_str(), "llvm.", 5)) {
193215 const TargetData *TD = Target->getTargetData();
194 LLVMSymbol *newSymbol = new LLVMSymbol(lt, v, v->getName(),
216 LLVMSymbol *newSymbol = new LLVMSymbol(lt, vis, v, v->getName(),
195217 mangler.getValueName(v),
196218 TD->getPreferredAlignmentLog(v));
197219 symbols[newSymbol->getMangledName()] = newSymbol;
353375 LTO::optimizeModules(const std::string &OutputFilename,
354376 std::vector &exportList,
355377 std::string &targetTriple,
356 bool saveTemps,
357 const char *FinalOutputFilename)
378 bool saveTemps, const char *FinalOutputFilename)
358379 {
359380 if (modules.empty())
360381 return LTO_NO_WORK;
372393
373394 sys::Path FinalOutputPath(FinalOutputFilename);
374395 FinalOutputPath.eraseSuffix();
396
397 switch(CGModel) {
398 case LTO_CGM_Dynamic:
399 Target->setRelocationModel(Reloc::PIC_);
400 break;
401 case LTO_CGM_DynamicNoPIC:
402 Target->setRelocationModel(Reloc::DynamicNoPIC);
403 break;
404 case LTO_CGM_Static:
405 Target->setRelocationModel(Reloc::Static);
406 break;
407 }
375408
376409 if (saveTemps) {
377410 std::string tempFileName(FinalOutputPath.c_str());