llvm.org GIT mirror llvm / c94c562
Indent according to LLVM's style guide. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153808 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 7 years ago
1 changed file(s) with 145 addition(s) and 154 deletion(s). Raw diff Collapse all Expand all
1919 #include "llvm/LLVMContext.h"
2020 #include "llvm/Module.h"
2121 #include "llvm/PassManager.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/Triple.h"
2422 #include "llvm/Analysis/Passes.h"
2523 #include "llvm/Analysis/Verifier.h"
2624 #include "llvm/Bitcode/ReaderWriter.h"
25 #include "llvm/Config/config.h"
2726 #include "llvm/MC/MCAsmInfo.h"
2827 #include "llvm/MC/MCContext.h"
2928 #include "llvm/MC/SubtargetFeature.h"
3231 #include "llvm/Target/TargetData.h"
3332 #include "llvm/Target/TargetMachine.h"
3433 #include "llvm/Target/TargetRegisterInfo.h"
34 #include "llvm/Transforms/IPO.h"
35 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
3536 #include "llvm/Support/CommandLine.h"
3637 #include "llvm/Support/ErrorHandling.h"
3738 #include "llvm/Support/FormattedStream.h"
4445 #include "llvm/Support/TargetRegistry.h"
4546 #include "llvm/Support/TargetSelect.h"
4647 #include "llvm/Support/system_error.h"
47 #include "llvm/Config/config.h"
48 #include "llvm/Transforms/IPO.h"
49 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
48 #include "llvm/ADT/StringExtras.h"
49 #include "llvm/ADT/Triple.h"
5050 #include
5151 #include
5252 #include
6464 }
6565
6666 LTOCodeGenerator::LTOCodeGenerator()
67 : _context(getGlobalContext()),
68 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
69 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
70 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
71 _nativeObjectFile(NULL)
72 {
73 InitializeAllTargets();
74 InitializeAllTargetMCs();
75 InitializeAllAsmPrinters();
67 : _context(getGlobalContext()),
68 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
69 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
70 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
71 _nativeObjectFile(NULL) {
72 InitializeAllTargets();
73 InitializeAllTargetMCs();
74 InitializeAllAsmPrinters();
7675 }
7776
7877 LTOCodeGenerator::~LTOCodeGenerator() {
8483 free(*I);
8584 }
8685
87 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg)
88 {
86 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
8987 bool ret = _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
9088
9189 const std::vector &undefs = mod->getAsmUndefinedRefs();
9593 return ret;
9694 }
9795
98
99 bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg)
100 {
101 switch (debug) {
102 case LTO_DEBUG_MODEL_NONE:
103 _emitDwarfDebugInfo = false;
104 return false;
105
106 case LTO_DEBUG_MODEL_DWARF:
107 _emitDwarfDebugInfo = true;
108 return false;
109 }
110 llvm_unreachable("Unknown debug format!");
111 }
112
96 bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug,
97 std::string& errMsg) {
98 switch (debug) {
99 case LTO_DEBUG_MODEL_NONE:
100 _emitDwarfDebugInfo = false;
101 return false;
102
103 case LTO_DEBUG_MODEL_DWARF:
104 _emitDwarfDebugInfo = true;
105 return false;
106 }
107 llvm_unreachable("Unknown debug format!");
108 }
113109
114110 bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
115 std::string& errMsg)
116 {
117 switch (model) {
118 case LTO_CODEGEN_PIC_MODEL_STATIC:
119 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
120 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
121 _codeModel = model;
122 return false;
123 }
124 llvm_unreachable("Unknown PIC model!");
125 }
126
127 void LTOCodeGenerator::setCpu(const char* mCpu)
128 {
111 std::string& errMsg) {
112 switch (model) {
113 case LTO_CODEGEN_PIC_MODEL_STATIC:
114 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
115 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
116 _codeModel = model;
117 return false;
118 }
119 llvm_unreachable("Unknown PIC model!");
120 }
121
122 void LTOCodeGenerator::setCpu(const char* mCpu) {
129123 _mCpu = mCpu;
130124 }
131125
132 void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
133 {
134 _mustPreserveSymbols[sym] = 1;
135 }
136
126 void LTOCodeGenerator::addMustPreserveSymbol(const char* sym) {
127 _mustPreserveSymbols[sym] = 1;
128 }
137129
138130 bool LTOCodeGenerator::writeMergedModules(const char *path,
139131 std::string &errMsg) {
168160 return false;
169161 }
170162
171
172 bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg)
173 {
163 bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
174164 // make unique temp .o file to put generated object file
175165 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
176166 if ( uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg) ) {
184174 tool_output_file objFile(uniqueObjPath.c_str(), errMsg);
185175 if (!errMsg.empty())
186176 return true;
177
187178 genResult = this->generateObjectFile(objFile.os(), errMsg);
188179 objFile.os().close();
189180 if (objFile.os().has_error()) {
190181 objFile.os().clear_error();
191182 return true;
192183 }
184
193185 objFile.keep();
194186 if ( genResult ) {
195187 uniqueObjPath.eraseFromDisk();
201193 return false;
202194 }
203195
204 const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
205 {
196 const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
206197 const char *name;
207198 if (compile_to_file(&name, errMsg))
208199 return NULL;
228219 return _nativeObjectFile->getBufferStart();
229220 }
230221
231 bool LTOCodeGenerator::determineTarget(std::string& errMsg)
232 {
233 if ( _target == NULL ) {
234 std::string Triple = _linker.getModule()->getTargetTriple();
235 if (Triple.empty())
236 Triple = sys::getDefaultTargetTriple();
237
238 // create target machine from info for merged modules
239 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
240 if ( march == NULL )
241 return true;
242
243 // The relocation model is actually a static member of TargetMachine
244 // and needs to be set before the TargetMachine is instantiated.
245 Reloc::Model RelocModel = Reloc::Default;
246 switch( _codeModel ) {
247 case LTO_CODEGEN_PIC_MODEL_STATIC:
248 RelocModel = Reloc::Static;
249 break;
250 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
251 RelocModel = Reloc::PIC_;
252 break;
253 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
254 RelocModel = Reloc::DynamicNoPIC;
255 break;
256 }
257
258 // construct LTOModule, hand over ownership of module and target
259 SubtargetFeatures Features;
260 Features.getDefaultSubtargetFeatures(llvm::Triple(Triple));
261 std::string FeatureStr = Features.getString();
262 TargetOptions Options;
263 _target = march->createTargetMachine(Triple, _mCpu, FeatureStr, Options,
264 RelocModel);
222 bool LTOCodeGenerator::determineTarget(std::string& errMsg) {
223 if ( _target == NULL ) {
224 std::string Triple = _linker.getModule()->getTargetTriple();
225 if (Triple.empty())
226 Triple = sys::getDefaultTargetTriple();
227
228 // create target machine from info for merged modules
229 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
230 if ( march == NULL )
231 return true;
232
233 // The relocation model is actually a static member of TargetMachine and
234 // needs to be set before the TargetMachine is instantiated.
235 Reloc::Model RelocModel = Reloc::Default;
236 switch( _codeModel ) {
237 case LTO_CODEGEN_PIC_MODEL_STATIC:
238 RelocModel = Reloc::Static;
239 break;
240 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
241 RelocModel = Reloc::PIC_;
242 break;
243 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
244 RelocModel = Reloc::DynamicNoPIC;
245 break;
265246 }
266 return false;
267 }
268
269 void LTOCodeGenerator::applyRestriction(GlobalValue &GV,
270 std::vector &mustPreserveList,
271 SmallPtrSet &asmUsed,
272 Mangler &mangler) {
247
248 // construct LTOModule, hand over ownership of module and target
249 SubtargetFeatures Features;
250 Features.getDefaultSubtargetFeatures(llvm::Triple(Triple));
251 std::string FeatureStr = Features.getString();
252 TargetOptions Options;
253 _target = march->createTargetMachine(Triple, _mCpu, FeatureStr, Options,
254 RelocModel);
255 }
256 return false;
257 }
258
259 void LTOCodeGenerator::
260 applyRestriction(GlobalValue &GV,
261 std::vector &mustPreserveList,
262 SmallPtrSet &asmUsed,
263 Mangler &mangler) {
273264 SmallString<64> Buffer;
274265 mangler.getNameWithPrefix(Buffer, &GV, false);
275266
290281
291282 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
292283 if (GlobalValue *GV =
293 dyn_cast(Inits->getOperand(i)->stripPointerCasts()))
284 dyn_cast(Inits->getOperand(i)->stripPointerCasts()))
294285 UsedValues.insert(GV);
295286 }
296287
303294 passes.add(createVerifierPass());
304295
305296 // mark which symbols can not be internalized
306 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), NULL);
297 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
307298 Mangler mangler(Context, *_target->getTargetData());
308299 std::vector mustPreserveList;
309300 SmallPtrSet asmUsed;
353344 /// Optimize merged modules using various IPO passes
354345 bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
355346 std::string &errMsg) {
356 if ( this->determineTarget(errMsg) )
357 return true;
358
359 // mark which symbols can not be internalized
360 this->applyScopeRestrictions();
361
362 Module* mergedModule = _linker.getModule();
363
364 // if options were requested, set them
365 if ( !_codegenOptions.empty() )
366 cl::ParseCommandLineOptions(_codegenOptions.size(),
367 const_cast(&_codegenOptions[0]));
368
369 // Instantiate the pass manager to organize the passes.
370 PassManager passes;
371
372 // Start off with a verification pass.
373 passes.add(createVerifierPass());
374
375 // Add an appropriate TargetData instance for this module...
376 passes.add(new TargetData(*_target->getTargetData()));
377
378 PassManagerBuilder().populateLTOPassManager(passes, /*Internalize=*/ false,
379 !DisableInline);
380
381 // Make sure everything is still good.
382 passes.add(createVerifierPass());
383
384 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
385
386 codeGenPasses->add(new TargetData(*_target->getTargetData()));
387
388 formatted_raw_ostream Out(out);
389
390 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
391 TargetMachine::CGFT_ObjectFile,
392 CodeGenOpt::Aggressive)) {
393 errMsg = "target file type not supported";
394 return true;
395 }
396
397 // Run our queue of passes all at once now, efficiently.
398 passes.run(*mergedModule);
399
400 // Run the code generator, and write assembly file
401 codeGenPasses->doInitialization();
402
403 for (Module::iterator
404 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
405 if (!it->isDeclaration())
406 codeGenPasses->run(*it);
407
408 codeGenPasses->doFinalization();
409 delete codeGenPasses;
410
411 return false; // success
347 if ( this->determineTarget(errMsg) )
348 return true;
349
350 // mark which symbols can not be internalized
351 this->applyScopeRestrictions();
352
353 Module* mergedModule = _linker.getModule();
354
355 // if options were requested, set them
356 if ( !_codegenOptions.empty() )
357 cl::ParseCommandLineOptions(_codegenOptions.size(),
358 const_cast(&_codegenOptions[0]));
359
360 // Instantiate the pass manager to organize the passes.
361 PassManager passes;
362
363 // Start off with a verification pass.
364 passes.add(createVerifierPass());
365
366 // Add an appropriate TargetData instance for this module...
367 passes.add(new TargetData(*_target->getTargetData()));
368
369 PassManagerBuilder().populateLTOPassManager(passes, /*Internalize=*/ false,
370 !DisableInline);
371
372 // Make sure everything is still good.
373 passes.add(createVerifierPass());
374
375 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
376
377 codeGenPasses->add(new TargetData(*_target->getTargetData()));
378
379 formatted_raw_ostream Out(out);
380
381 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
382 TargetMachine::CGFT_ObjectFile,
383 CodeGenOpt::Aggressive)) {
384 errMsg = "target file type not supported";
385 return true;
386 }
387
388 // Run our queue of passes all at once now, efficiently.
389 passes.run(*mergedModule);
390
391 // Run the code generator, and write assembly file
392 codeGenPasses->doInitialization();
393
394 for (Module::iterator
395 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
396 if (!it->isDeclaration())
397 codeGenPasses->run(*it);
398
399 codeGenPasses->doFinalization();
400 delete codeGenPasses;
401
402 return false; // success
412403 }
413404
414405 /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging