Have SCEV turn sext(x) into zext(x) when x is s>= 0. This applies many times in
[llvm.git] / tools / lto / LTOCodeGenerator.cpp
blob439bac19f6607c26945b1c4b144a348ccdb1259b
1 //===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Link Time Optimization library. This library is
11 // intended to be used by linker to optimize code at link time.
13 //===----------------------------------------------------------------------===//
15 #include "LTOModule.h"
16 #include "LTOCodeGenerator.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Linker.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/Module.h"
23 #include "llvm/PassManager.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/Triple.h"
26 #include "llvm/Analysis/Passes.h"
27 #include "llvm/Bitcode/ReaderWriter.h"
28 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/MC/MCContext.h"
30 #include "llvm/Target/Mangler.h"
31 #include "llvm/Target/SubtargetFeature.h"
32 #include "llvm/Target/TargetOptions.h"
33 #include "llvm/Target/TargetData.h"
34 #include "llvm/Target/TargetMachine.h"
35 #include "llvm/Target/TargetRegistry.h"
36 #include "llvm/Target/TargetSelect.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/FormattedStream.h"
39 #include "llvm/Support/MemoryBuffer.h"
40 #include "llvm/Support/StandardPasses.h"
41 #include "llvm/Support/SystemUtils.h"
42 #include "llvm/Support/ToolOutputFile.h"
43 #include "llvm/Support/Host.h"
44 #include "llvm/Support/Program.h"
45 #include "llvm/Support/Signals.h"
46 #include "llvm/Support/system_error.h"
47 #include "llvm/Config/config.h"
48 #include <cstdlib>
49 #include <unistd.h>
50 #include <fcntl.h>
53 using namespace llvm;
55 static cl::opt<bool> DisableInline("disable-inlining",
56 cl::desc("Do not run the inliner pass"));
59 const char* LTOCodeGenerator::getVersionString()
61 #ifdef LLVM_VERSION_INFO
62 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
63 #else
64 return PACKAGE_NAME " version " PACKAGE_VERSION;
65 #endif
69 LTOCodeGenerator::LTOCodeGenerator()
70 : _context(getGlobalContext()),
71 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
72 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
73 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
74 _nativeObjectFile(NULL), _assemblerPath(NULL)
76 InitializeAllTargets();
77 InitializeAllAsmPrinters();
80 LTOCodeGenerator::~LTOCodeGenerator()
82 delete _target;
83 delete _nativeObjectFile;
88 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg)
90 return _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
94 bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg)
96 switch (debug) {
97 case LTO_DEBUG_MODEL_NONE:
98 _emitDwarfDebugInfo = false;
99 return false;
101 case LTO_DEBUG_MODEL_DWARF:
102 _emitDwarfDebugInfo = true;
103 return false;
105 errMsg = "unknown debug format";
106 return true;
110 bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
111 std::string& errMsg)
113 switch (model) {
114 case LTO_CODEGEN_PIC_MODEL_STATIC:
115 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
116 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
117 _codeModel = model;
118 return false;
120 errMsg = "unknown pic model";
121 return true;
124 void LTOCodeGenerator::setCpu(const char* mCpu)
126 _mCpu = mCpu;
129 void LTOCodeGenerator::setAssemblerPath(const char* path)
131 if ( _assemblerPath )
132 delete _assemblerPath;
133 _assemblerPath = new sys::Path(path);
136 void LTOCodeGenerator::setAssemblerArgs(const char** args, int nargs)
138 for (int i = 0; i < nargs; ++i) {
139 const char *arg = args[i];
140 _assemblerArgs.push_back(arg);
144 void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
146 _mustPreserveSymbols[sym] = 1;
150 bool LTOCodeGenerator::writeMergedModules(const char *path,
151 std::string &errMsg) {
152 if (determineTarget(errMsg))
153 return true;
155 // mark which symbols can not be internalized
156 applyScopeRestrictions();
158 // create output file
159 std::string ErrInfo;
160 tool_output_file Out(path, ErrInfo,
161 raw_fd_ostream::F_Binary);
162 if (!ErrInfo.empty()) {
163 errMsg = "could not open bitcode file for writing: ";
164 errMsg += path;
165 return true;
168 // write bitcode to it
169 WriteBitcodeToFile(_linker.getModule(), Out.os());
170 Out.os().close();
172 if (Out.os().has_error()) {
173 errMsg = "could not write bitcode file: ";
174 errMsg += path;
175 Out.os().clear_error();
176 return true;
179 Out.keep();
180 return false;
184 const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
186 // make unique temp .s file to put generated assembly code
187 sys::Path uniqueAsmPath("lto-llvm.s");
188 if ( uniqueAsmPath.createTemporaryFileOnDisk(false, &errMsg) )
189 return NULL;
190 sys::RemoveFileOnSignal(uniqueAsmPath);
192 // generate assembly code
193 bool genResult = false;
195 tool_output_file asmFile(uniqueAsmPath.c_str(), errMsg);
196 if (!errMsg.empty())
197 return NULL;
198 genResult = this->generateAssemblyCode(asmFile.os(), errMsg);
199 asmFile.os().close();
200 if (asmFile.os().has_error()) {
201 asmFile.os().clear_error();
202 return NULL;
204 asmFile.keep();
206 if ( genResult ) {
207 uniqueAsmPath.eraseFromDisk();
208 return NULL;
211 // make unique temp .o file to put generated object file
212 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
213 if ( uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg) ) {
214 uniqueAsmPath.eraseFromDisk();
215 return NULL;
217 sys::RemoveFileOnSignal(uniqueObjPath);
219 // assemble the assembly code
220 const std::string& uniqueObjStr = uniqueObjPath.str();
221 bool asmResult = this->assemble(uniqueAsmPath.str(), uniqueObjStr, errMsg);
222 if ( !asmResult ) {
223 // remove old buffer if compile() called twice
224 delete _nativeObjectFile;
226 // read .o file into memory buffer
227 OwningPtr<MemoryBuffer> BuffPtr;
228 if (error_code ec = MemoryBuffer::getFile(uniqueObjStr.c_str(),BuffPtr))
229 errMsg = ec.message();
230 _nativeObjectFile = BuffPtr.take();
233 // remove temp files
234 uniqueAsmPath.eraseFromDisk();
235 uniqueObjPath.eraseFromDisk();
237 // return buffer, unless error
238 if ( _nativeObjectFile == NULL )
239 return NULL;
240 *length = _nativeObjectFile->getBufferSize();
241 return _nativeObjectFile->getBufferStart();
245 bool LTOCodeGenerator::assemble(const std::string& asmPath,
246 const std::string& objPath, std::string& errMsg)
248 sys::Path tool;
249 bool needsCompilerOptions = true;
250 if ( _assemblerPath ) {
251 tool = *_assemblerPath;
252 needsCompilerOptions = false;
253 } else {
254 // find compiler driver
255 tool = sys::Program::FindProgramByName("gcc");
256 if ( tool.isEmpty() ) {
257 errMsg = "can't locate gcc";
258 return true;
262 // build argument list
263 std::vector<const char*> args;
264 llvm::Triple targetTriple(_linker.getModule()->getTargetTriple());
265 const char *arch = targetTriple.getArchNameForAssembler();
267 args.push_back(tool.c_str());
269 if (targetTriple.getOS() == Triple::Darwin) {
270 // darwin specific command line options
271 if (arch != NULL) {
272 args.push_back("-arch");
273 args.push_back(arch);
275 // add -static to assembler command line when code model requires
276 if ( (_assemblerPath != NULL) &&
277 (_codeModel == LTO_CODEGEN_PIC_MODEL_STATIC) )
278 args.push_back("-static");
280 if ( needsCompilerOptions ) {
281 args.push_back("-c");
282 args.push_back("-x");
283 args.push_back("assembler");
284 } else {
285 for (std::vector<std::string>::iterator I = _assemblerArgs.begin(),
286 E = _assemblerArgs.end(); I != E; ++I) {
287 args.push_back(I->c_str());
290 args.push_back("-o");
291 args.push_back(objPath.c_str());
292 args.push_back(asmPath.c_str());
293 args.push_back(0);
295 // invoke assembler
296 if ( sys::Program::ExecuteAndWait(tool, &args[0], 0, 0, 0, 0, &errMsg) ) {
297 errMsg = "error in assembly";
298 return true;
300 return false; // success
305 bool LTOCodeGenerator::determineTarget(std::string& errMsg)
307 if ( _target == NULL ) {
308 std::string Triple = _linker.getModule()->getTargetTriple();
309 if (Triple.empty())
310 Triple = sys::getHostTriple();
312 // create target machine from info for merged modules
313 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
314 if ( march == NULL )
315 return true;
317 // The relocation model is actually a static member of TargetMachine
318 // and needs to be set before the TargetMachine is instantiated.
319 switch( _codeModel ) {
320 case LTO_CODEGEN_PIC_MODEL_STATIC:
321 TargetMachine::setRelocationModel(Reloc::Static);
322 break;
323 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
324 TargetMachine::setRelocationModel(Reloc::PIC_);
325 break;
326 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
327 TargetMachine::setRelocationModel(Reloc::DynamicNoPIC);
328 break;
331 // construct LTModule, hand over ownership of module and target
332 SubtargetFeatures Features;
333 Features.getDefaultSubtargetFeatures(_mCpu, llvm::Triple(Triple));
334 std::string FeatureStr = Features.getString();
335 _target = march->createTargetMachine(Triple, FeatureStr);
337 return false;
340 void LTOCodeGenerator::applyScopeRestrictions() {
341 if (_scopeRestrictionsDone) return;
342 Module *mergedModule = _linker.getModule();
344 // Start off with a verification pass.
345 PassManager passes;
346 passes.add(createVerifierPass());
348 // mark which symbols can not be internalized
349 if (!_mustPreserveSymbols.empty()) {
350 MCContext Context(*_target->getMCAsmInfo(), NULL);
351 Mangler mangler(Context, *_target->getTargetData());
352 std::vector<const char*> mustPreserveList;
353 for (Module::iterator f = mergedModule->begin(),
354 e = mergedModule->end(); f != e; ++f) {
355 if (!f->isDeclaration() &&
356 _mustPreserveSymbols.count(mangler.getNameWithPrefix(f)))
357 mustPreserveList.push_back(::strdup(f->getNameStr().c_str()));
359 for (Module::global_iterator v = mergedModule->global_begin(),
360 e = mergedModule->global_end(); v != e; ++v) {
361 if (!v->isDeclaration() &&
362 _mustPreserveSymbols.count(mangler.getNameWithPrefix(v)))
363 mustPreserveList.push_back(::strdup(v->getNameStr().c_str()));
365 passes.add(createInternalizePass(mustPreserveList));
368 // apply scope restrictions
369 passes.run(*mergedModule);
371 _scopeRestrictionsDone = true;
374 /// Optimize merged modules using various IPO passes
375 bool LTOCodeGenerator::generateAssemblyCode(raw_ostream& out,
376 std::string& errMsg)
378 if ( this->determineTarget(errMsg) )
379 return true;
381 // mark which symbols can not be internalized
382 this->applyScopeRestrictions();
384 Module* mergedModule = _linker.getModule();
386 // if options were requested, set them
387 if ( !_codegenOptions.empty() )
388 cl::ParseCommandLineOptions(_codegenOptions.size(),
389 const_cast<char **>(&_codegenOptions[0]));
391 // Instantiate the pass manager to organize the passes.
392 PassManager passes;
394 // Start off with a verification pass.
395 passes.add(createVerifierPass());
397 // Add an appropriate TargetData instance for this module...
398 passes.add(new TargetData(*_target->getTargetData()));
400 createStandardLTOPasses(&passes, /*Internalize=*/ false, !DisableInline,
401 /*VerifyEach=*/ false);
403 // Make sure everything is still good.
404 passes.add(createVerifierPass());
406 FunctionPassManager* codeGenPasses = new FunctionPassManager(mergedModule);
408 codeGenPasses->add(new TargetData(*_target->getTargetData()));
410 formatted_raw_ostream Out(out);
412 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
413 TargetMachine::CGFT_AssemblyFile,
414 CodeGenOpt::Aggressive)) {
415 errMsg = "target file type not supported";
416 return true;
419 // Run our queue of passes all at once now, efficiently.
420 passes.run(*mergedModule);
422 // Run the code generator, and write assembly file
423 codeGenPasses->doInitialization();
425 for (Module::iterator
426 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
427 if (!it->isDeclaration())
428 codeGenPasses->run(*it);
430 codeGenPasses->doFinalization();
432 return false; // success
436 /// Optimize merged modules using various IPO passes
437 void LTOCodeGenerator::setCodeGenDebugOptions(const char* options)
439 for (std::pair<StringRef, StringRef> o = getToken(options);
440 !o.first.empty(); o = getToken(o.second)) {
441 // ParseCommandLineOptions() expects argv[0] to be program name.
442 // Lazily add that.
443 if ( _codegenOptions.empty() )
444 _codegenOptions.push_back("libLTO");
445 _codegenOptions.push_back(strdup(o.first.str().c_str()));