llvm.org GIT mirror llvm / 66efc63
Rename variables to match the style guide and clang-format. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@189962 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 7 years ago
2 changed file(s) with 74 addition(s) and 75 deletion(s). Raw diff Collapse all Expand all
6969 }
7070
7171 LTOCodeGenerator::LTOCodeGenerator()
72 : _context(getGlobalContext()),
73 _linker(new Module("ld-temp.o", _context)), _target(NULL),
74 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
75 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
76 _nativeObjectFile(NULL) {
72 : Context(getGlobalContext()), Linker(new Module("ld-temp.o", Context)),
73 TargetMach(NULL), EmitDwarfDebugInfo(false), ScopeRestrictionsDone(false),
74 CodeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), NativeObjectFile(NULL) {
7775 InitializeAllTargets();
7876 InitializeAllTargetMCs();
7977 InitializeAllAsmPrinters();
8179 }
8280
8381 LTOCodeGenerator::~LTOCodeGenerator() {
84 delete _target;
85 delete _nativeObjectFile;
86 delete _linker.getModule();
87
88 for (std::vector::iterator I = _codegenOptions.begin(),
89 E = _codegenOptions.end(); I != E; ++I)
82 delete TargetMach;
83 delete NativeObjectFile;
84 delete Linker.getModule();
85
86 for (std::vector::iterator I = CodegenOptions.begin(),
87 E = CodegenOptions.end();
88 I != E; ++I)
9089 free(*I);
9190 }
9291
121120 }
122121
123122 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
124 bool ret = _linker.linkInModule(mod->getLLVVMModule(), &errMsg);
123 bool ret = Linker.linkInModule(mod->getLLVVMModule(), &errMsg);
125124
126125 const std::vector &undefs = mod->getAsmUndefinedRefs();
127126 for (int i = 0, e = undefs.size(); i != e; ++i)
128 _asmUndefinedRefs[undefs[i]] = 1;
127 AsmUndefinedRefs[undefs[i]] = 1;
129128
130129 return !ret;
131130 }
133132 void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) {
134133 switch (debug) {
135134 case LTO_DEBUG_MODEL_NONE:
136 _emitDwarfDebugInfo = false;
135 EmitDwarfDebugInfo = false;
137136 return;
138137
139138 case LTO_DEBUG_MODEL_DWARF:
140 _emitDwarfDebugInfo = true;
139 EmitDwarfDebugInfo = true;
141140 return;
142141 }
143142 llvm_unreachable("Unknown debug format!");
148147 case LTO_CODEGEN_PIC_MODEL_STATIC:
149148 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
150149 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
151 _codeModel = model;
150 CodeModel = model;
152151 return;
153152 }
154153 llvm_unreachable("Unknown PIC model!");
172171 }
173172
174173 // write bitcode to it
175 WriteBitcodeToFile(_linker.getModule(), Out.os());
174 WriteBitcodeToFile(Linker.getModule(), Out.os());
176175 Out.os().close();
177176
178177 if (Out.os().has_error()) {
213212 return false;
214213 }
215214
216 _nativeObjectPath = Filename.c_str();
217 *name = _nativeObjectPath.c_str();
215 NativeObjectPath = Filename.c_str();
216 *name = NativeObjectPath.c_str();
218217 return true;
219218 }
220219
224223 return NULL;
225224
226225 // remove old buffer if compile() called twice
227 delete _nativeObjectFile;
226 delete NativeObjectFile;
228227
229228 // read .o file into memory buffer
230229 OwningPtr BuffPtr;
231230 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
232231 errMsg = ec.message();
233 sys::fs::remove(_nativeObjectPath);
232 sys::fs::remove(NativeObjectPath);
234233 return NULL;
235234 }
236 _nativeObjectFile = BuffPtr.take();
235 NativeObjectFile = BuffPtr.take();
237236
238237 // remove temp files
239 sys::fs::remove(_nativeObjectPath);
238 sys::fs::remove(NativeObjectPath);
240239
241240 // return buffer, unless error
242 if (_nativeObjectFile == NULL)
241 if (NativeObjectFile == NULL)
243242 return NULL;
244 *length = _nativeObjectFile->getBufferSize();
245 return _nativeObjectFile->getBufferStart();
243 *length = NativeObjectFile->getBufferSize();
244 return NativeObjectFile->getBufferStart();
246245 }
247246
248247 bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
249 if (_target != NULL)
248 if (TargetMach != NULL)
250249 return true;
251250
252251 // if options were requested, set them
253 if (!_codegenOptions.empty())
254 cl::ParseCommandLineOptions(_codegenOptions.size(),
255 const_cast(&_codegenOptions[0]));
256
257 std::string TripleStr = _linker.getModule()->getTargetTriple();
252 if (!CodegenOptions.empty())
253 cl::ParseCommandLineOptions(CodegenOptions.size(),
254 const_cast(&CodegenOptions[0]));
255
256 std::string TripleStr = Linker.getModule()->getTargetTriple();
258257 if (TripleStr.empty())
259258 TripleStr = sys::getDefaultTargetTriple();
260259 llvm::Triple Triple(TripleStr);
267266 // The relocation model is actually a static member of TargetMachine and
268267 // needs to be set before the TargetMachine is instantiated.
269268 Reloc::Model RelocModel = Reloc::Default;
270 switch (_codeModel) {
269 switch (CodeModel) {
271270 case LTO_CODEGEN_PIC_MODEL_STATIC:
272271 RelocModel = Reloc::Static;
273272 break;
284283 Features.getDefaultSubtargetFeatures(Triple);
285284 std::string FeatureStr = Features.getString();
286285 // Set a default CPU for Darwin triples.
287 if (_mCpu.empty() && Triple.isOSDarwin()) {
286 if (MCpu.empty() && Triple.isOSDarwin()) {
288287 if (Triple.getArch() == llvm::Triple::x86_64)
289 _mCpu = "core2";
288 MCpu = "core2";
290289 else if (Triple.getArch() == llvm::Triple::x86)
291 _mCpu = "yonah";
290 MCpu = "yonah";
292291 }
293292 TargetOptions Options;
294293 LTOModule::getTargetOptions(Options);
295 _target = march->createTargetMachine(TripleStr, _mCpu, FeatureStr, Options,
296 RelocModel, CodeModel::Default,
297 CodeGenOpt::Aggressive);
294 TargetMach = march->createTargetMachine(TripleStr, MCpu, FeatureStr, Options,
295 RelocModel, CodeModel::Default,
296 CodeGenOpt::Aggressive);
298297 return true;
299298 }
300299
308307
309308 if (GV.isDeclaration())
310309 return;
311 if (_mustPreserveSymbols.count(Buffer))
310 if (MustPreserveSymbols.count(Buffer))
312311 mustPreserveList.push_back(GV.getName().data());
313 if (_asmUndefinedRefs.count(Buffer))
312 if (AsmUndefinedRefs.count(Buffer))
314313 asmUsed.insert(&GV);
315314 }
316315
326325 }
327326
328327 void LTOCodeGenerator::applyScopeRestrictions() {
329 if (_scopeRestrictionsDone) return;
330 Module *mergedModule = _linker.getModule();
328 if (ScopeRestrictionsDone)
329 return;
330 Module *mergedModule = Linker.getModule();
331331
332332 // Start off with a verification pass.
333333 PassManager passes;
334334 passes.add(createVerifierPass());
335335
336336 // mark which symbols can not be internalized
337 MCContext Context(_target->getMCAsmInfo(), _target->getRegisterInfo(), NULL);
338 Mangler mangler(Context, _target);
337 MCContext MContext(TargetMach->getMCAsmInfo(), TargetMach->getRegisterInfo(),
338 NULL);
339 Mangler mangler(MContext, TargetMach);
339340 std::vector mustPreserveList;
340341 SmallPtrSet asmUsed;
341342
356357 LLVMCompilerUsed->eraseFromParent();
357358
358359 if (!asmUsed.empty()) {
359 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
360 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context);
360361 std::vector asmUsed2;
361362 for (SmallPtrSet::const_iterator i = asmUsed.begin(),
362363 e = asmUsed.end(); i !=e; ++i) {
380381 // apply scope restrictions
381382 passes.run(*mergedModule);
382383
383 _scopeRestrictionsDone = true;
384 ScopeRestrictionsDone = true;
384385 }
385386
386387 /// Optimize merged modules using various IPO passes
389390 if (!this->determineTarget(errMsg))
390391 return false;
391392
392 Module* mergedModule = _linker.getModule();
393 Module *mergedModule = Linker.getModule();
393394
394395 // Mark which symbols can not be internalized
395396 this->applyScopeRestrictions();
401402 passes.add(createVerifierPass());
402403
403404 // Add an appropriate DataLayout instance for this module...
404 passes.add(new DataLayout(*_target->getDataLayout()));
405 _target->addAnalysisPasses(passes);
405 passes.add(new DataLayout(*TargetMach->getDataLayout()));
406 TargetMach->addAnalysisPasses(passes);
406407
407408 // Enabling internalize here would use its AllButMain variant. It
408409 // keeps only main if it exists and does nothing for libraries. Instead
418419
419420 PassManager codeGenPasses;
420421
421 codeGenPasses.add(new DataLayout(*_target->getDataLayout()));
422 _target->addAnalysisPasses(codeGenPasses);
422 codeGenPasses.add(new DataLayout(*TargetMach->getDataLayout()));
423 TargetMach->addAnalysisPasses(codeGenPasses);
423424
424425 formatted_raw_ostream Out(out);
425426
427428 // the ObjCARCContractPass must be run, so do it unconditionally here.
428429 codeGenPasses.add(createObjCARCContractPass());
429430
430 if (_target->addPassesToEmitFile(codeGenPasses, Out,
431 TargetMachine::CGFT_ObjectFile)) {
431 if (TargetMach->addPassesToEmitFile(codeGenPasses, Out,
432 TargetMachine::CGFT_ObjectFile)) {
432433 errMsg = "target file type not supported";
433434 return false;
434435 }
449450 !o.first.empty(); o = getToken(o.second)) {
450451 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
451452 // that.
452 if (_codegenOptions.empty())
453 _codegenOptions.push_back(strdup("libLTO"));
454 _codegenOptions.push_back(strdup(o.first.str().c_str()));
455 }
456 }
453 if (CodegenOptions.empty())
454 CodegenOptions.push_back(strdup("libLTO"));
455 CodegenOptions.push_back(strdup(o.first.str().c_str()));
456 }
457 }
6666 void setDebugInfo(lto_debug_model);
6767 void setCodePICModel(lto_codegen_model);
6868
69 void setCpu(const char* mCpu) { _mCpu = mCpu; }
69 void setCpu(const char *mCpu) { MCpu = mCpu; }
7070
71 void addMustPreserveSymbol(const char* sym) {
72 _mustPreserveSymbols[sym] = 1;
73 }
71 void addMustPreserveSymbol(const char *sym) { MustPreserveSymbols[sym] = 1; }
7472
7573 // To pass options to the driver and optimization passes. These options are
7674 // not necessarily for debugging purpose (The function name is misleading).
114112
115113 typedef llvm::StringMap StringSet;
116114
117 llvm::LLVMContext& _context;
118 llvm::Linker _linker;
119 llvm::TargetMachine* _target;
120 bool _emitDwarfDebugInfo;
121 bool _scopeRestrictionsDone;
122 lto_codegen_model _codeModel;
123 StringSet _mustPreserveSymbols;
124 StringSet _asmUndefinedRefs;
125 llvm::MemoryBuffer* _nativeObjectFile;
126 std::vector _codegenOptions;
127 std::string _mCpu;
128 std::string _nativeObjectPath;
115 llvm::LLVMContext &Context;
116 llvm::Linker Linker;
117 llvm::TargetMachine *TargetMach;
118 bool EmitDwarfDebugInfo;
119 bool ScopeRestrictionsDone;
120 lto_codegen_model CodeModel;
121 StringSet MustPreserveSymbols;
122 StringSet AsmUndefinedRefs;
123 llvm::MemoryBuffer *NativeObjectFile;
124 std::vector CodegenOptions;
125 std::string MCpu;
126 std::string NativeObjectPath;
129127 };
130128
131129 #endif // LTO_CODE_GENERATOR_H