Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 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. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 15 | #include "LTOModule.h" |
| 16 | #include "LTOCodeGenerator.h" |
| 17 | |
| 18 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
| 20 | #include "llvm/DerivedTypes.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 21 | #include "llvm/Linker.h" |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 22 | #include "llvm/LLVMContext.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 24 | #include "llvm/ModuleProvider.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 25 | #include "llvm/PassManager.h" |
| 26 | #include "llvm/ADT/StringExtras.h" |
Viktor Kutuzov | 51cdac0 | 2009-11-17 18:48:27 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Triple.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/Passes.h" |
| 29 | #include "llvm/Analysis/LoopPass.h" |
| 30 | #include "llvm/Analysis/Verifier.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 31 | #include "llvm/Bitcode/ReaderWriter.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/FileWriters.h" |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 34 | #include "llvm/Support/FormattedStream.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Mangler.h" |
| 36 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 006a034 | 2009-06-03 21:06:14 +0000 | [diff] [blame] | 37 | #include "llvm/Support/StandardPasses.h" |
| 38 | #include "llvm/Support/SystemUtils.h" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 39 | #include "llvm/System/Host.h" |
Chris Lattner | b683ea4 | 2009-08-23 21:36:09 +0000 | [diff] [blame] | 40 | #include "llvm/System/Program.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 41 | #include "llvm/System/Signals.h" |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 42 | #include "llvm/Target/SubtargetFeature.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 44 | #include "llvm/MC/MCAsmInfo.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetData.h" |
| 46 | #include "llvm/Target/TargetMachine.h" |
Daniel Dunbar | ff9834a | 2009-07-16 02:41:19 +0000 | [diff] [blame] | 47 | #include "llvm/Target/TargetRegistry.h" |
Chris Lattner | 2deb58f | 2009-06-17 16:42:19 +0000 | [diff] [blame] | 48 | #include "llvm/Target/TargetSelect.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 49 | #include "llvm/Transforms/IPO.h" |
| 50 | #include "llvm/Transforms/Scalar.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 51 | #include "llvm/Config/config.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 52 | #include <cstdlib> |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 53 | #include <unistd.h> |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 54 | #include <fcntl.h> |
| 55 | |
| 56 | |
| 57 | using namespace llvm; |
| 58 | |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 59 | static cl::opt<bool> DisableInline("disable-inlining", |
| 60 | cl::desc("Do not run the inliner pass")); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | const char* LTOCodeGenerator::getVersionString() |
| 64 | { |
| 65 | #ifdef LLVM_VERSION_INFO |
| 66 | return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO; |
| 67 | #else |
| 68 | return PACKAGE_NAME " version " PACKAGE_VERSION; |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 73 | LTOCodeGenerator::LTOCodeGenerator() |
| 74 | : _context(getGlobalContext()), |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 75 | _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL), |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 76 | _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false), |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 77 | _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), |
Nick Lewycky | 3e4c41a | 2009-08-03 07:16:42 +0000 | [diff] [blame] | 78 | _nativeObjectFile(NULL), _assemblerPath(NULL) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 79 | { |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 80 | InitializeAllTargets(); |
| 81 | InitializeAllAsmPrinters(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | LTOCodeGenerator::~LTOCodeGenerator() |
| 85 | { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 86 | delete _target; |
| 87 | delete _nativeObjectFile; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) |
| 93 | { |
| 94 | return _linker.LinkInModule(mod->getLLVVMModule(), &errMsg); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg) |
| 99 | { |
| 100 | switch (debug) { |
| 101 | case LTO_DEBUG_MODEL_NONE: |
| 102 | _emitDwarfDebugInfo = false; |
| 103 | return false; |
| 104 | |
| 105 | case LTO_DEBUG_MODEL_DWARF: |
| 106 | _emitDwarfDebugInfo = true; |
| 107 | return false; |
| 108 | } |
| 109 | errMsg = "unknown debug format"; |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model, |
Evan Cheng | 855a168 | 2009-06-26 06:57:16 +0000 | [diff] [blame] | 115 | std::string& errMsg) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 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 | errMsg = "unknown pic model"; |
| 125 | return true; |
| 126 | } |
| 127 | |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 128 | void LTOCodeGenerator::setAssemblerPath(const char* path) |
| 129 | { |
| 130 | if ( _assemblerPath ) |
| 131 | delete _assemblerPath; |
| 132 | _assemblerPath = new sys::Path(path); |
| 133 | } |
| 134 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 135 | void LTOCodeGenerator::addMustPreserveSymbol(const char* sym) |
| 136 | { |
| 137 | _mustPreserveSymbols[sym] = 1; |
| 138 | } |
| 139 | |
| 140 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 141 | bool LTOCodeGenerator::writeMergedModules(const char *path, |
| 142 | std::string &errMsg) { |
| 143 | if (determineTarget(errMsg)) |
| 144 | return true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 145 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 146 | // mark which symbols can not be internalized |
| 147 | applyScopeRestrictions(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 148 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 149 | // create output file |
| 150 | std::string ErrInfo; |
| 151 | raw_fd_ostream Out(path, ErrInfo, |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 152 | raw_fd_ostream::F_Binary); |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 153 | if (!ErrInfo.empty()) { |
| 154 | errMsg = "could not open bitcode file for writing: "; |
| 155 | errMsg += path; |
| 156 | return true; |
| 157 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 158 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 159 | // write bitcode to it |
| 160 | WriteBitcodeToFile(_linker.getModule(), Out); |
| 161 | |
| 162 | if (Out.has_error()) { |
| 163 | errMsg = "could not write bitcode file: "; |
| 164 | errMsg += path; |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | return false; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 172 | const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 173 | { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 174 | // make unique temp .s file to put generated assembly code |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 175 | sys::Path uniqueAsmPath("lto-llvm.s"); |
| 176 | if ( uniqueAsmPath.createTemporaryFileOnDisk(true, &errMsg) ) |
| 177 | return NULL; |
| 178 | sys::RemoveFileOnSignal(uniqueAsmPath); |
| 179 | |
| 180 | // generate assembly code |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 181 | bool genResult = false; |
| 182 | { |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 183 | raw_fd_ostream asmFD(uniqueAsmPath.c_str(), errMsg); |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 184 | formatted_raw_ostream asmFile(asmFD); |
Dan Gohman | ed3e8b4 | 2008-08-21 15:33:45 +0000 | [diff] [blame] | 185 | if (!errMsg.empty()) |
| 186 | return NULL; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 187 | genResult = this->generateAssemblyCode(asmFile, errMsg); |
| 188 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 189 | if ( genResult ) { |
| 190 | if ( uniqueAsmPath.exists() ) |
| 191 | uniqueAsmPath.eraseFromDisk(); |
| 192 | return NULL; |
| 193 | } |
| 194 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 195 | // make unique temp .o file to put generated object file |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 196 | sys::PathWithStatus uniqueObjPath("lto-llvm.o"); |
| 197 | if ( uniqueObjPath.createTemporaryFileOnDisk(true, &errMsg) ) { |
| 198 | if ( uniqueAsmPath.exists() ) |
| 199 | uniqueAsmPath.eraseFromDisk(); |
| 200 | return NULL; |
| 201 | } |
| 202 | sys::RemoveFileOnSignal(uniqueObjPath); |
| 203 | |
| 204 | // assemble the assembly code |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 205 | const std::string& uniqueObjStr = uniqueObjPath.str(); |
| 206 | bool asmResult = this->assemble(uniqueAsmPath.str(), uniqueObjStr, errMsg); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 207 | if ( !asmResult ) { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 208 | // remove old buffer if compile() called twice |
| 209 | delete _nativeObjectFile; |
| 210 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 211 | // read .o file into memory buffer |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 212 | _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 213 | } |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 214 | |
| 215 | // remove temp files |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 216 | uniqueAsmPath.eraseFromDisk(); |
| 217 | uniqueObjPath.eraseFromDisk(); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 218 | |
| 219 | // return buffer, unless error |
| 220 | if ( _nativeObjectFile == NULL ) |
| 221 | return NULL; |
| 222 | *length = _nativeObjectFile->getBufferSize(); |
| 223 | return _nativeObjectFile->getBufferStart(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
| 227 | bool LTOCodeGenerator::assemble(const std::string& asmPath, |
| 228 | const std::string& objPath, std::string& errMsg) |
| 229 | { |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 230 | sys::Path tool; |
| 231 | bool needsCompilerOptions = true; |
| 232 | if ( _assemblerPath ) { |
| 233 | tool = *_assemblerPath; |
| 234 | needsCompilerOptions = false; |
Nick Lewycky | 195bea3 | 2009-04-30 15:24:09 +0000 | [diff] [blame] | 235 | } else { |
| 236 | // find compiler driver |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 237 | tool = sys::Program::FindProgramByName("gcc"); |
| 238 | if ( tool.isEmpty() ) { |
Nick Lewycky | 195bea3 | 2009-04-30 15:24:09 +0000 | [diff] [blame] | 239 | errMsg = "can't locate gcc"; |
| 240 | return true; |
| 241 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // build argument list |
| 245 | std::vector<const char*> args; |
Viktor Kutuzov | 51cdac0 | 2009-11-17 18:48:27 +0000 | [diff] [blame] | 246 | llvm::Triple targetTriple(_linker.getModule()->getTargetTriple()); |
| 247 | const char *arch = targetTriple.getArchNameForAssembler(); |
| 248 | |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 249 | args.push_back(tool.c_str()); |
Viktor Kutuzov | 51cdac0 | 2009-11-17 18:48:27 +0000 | [diff] [blame] | 250 | |
| 251 | if (targetTriple.getOS() == Triple::Darwin) { |
Nick Kledzik | d8b4711 | 2009-06-04 19:14:08 +0000 | [diff] [blame] | 252 | // darwin specific command line options |
Viktor Kutuzov | 51cdac0 | 2009-11-17 18:48:27 +0000 | [diff] [blame] | 253 | if (arch != NULL) { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 254 | args.push_back("-arch"); |
Viktor Kutuzov | 51cdac0 | 2009-11-17 18:48:27 +0000 | [diff] [blame] | 255 | args.push_back(arch); |
Bob Wilson | 75d6ffd | 2009-06-22 18:01:28 +0000 | [diff] [blame] | 256 | } |
Nick Kledzik | d8b4711 | 2009-06-04 19:14:08 +0000 | [diff] [blame] | 257 | // add -static to assembler command line when code model requires |
| 258 | if ( (_assemblerPath != NULL) && (_codeModel == LTO_CODEGEN_PIC_MODEL_STATIC) ) |
| 259 | args.push_back("-static"); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 260 | } |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 261 | if ( needsCompilerOptions ) { |
| 262 | args.push_back("-c"); |
| 263 | args.push_back("-x"); |
| 264 | args.push_back("assembler"); |
| 265 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 266 | args.push_back("-o"); |
| 267 | args.push_back(objPath.c_str()); |
| 268 | args.push_back(asmPath.c_str()); |
| 269 | args.push_back(0); |
| 270 | |
| 271 | // invoke assembler |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 272 | if ( sys::Program::ExecuteAndWait(tool, &args[0], 0, 0, 0, 0, &errMsg) ) { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 273 | errMsg = "error in assembly"; |
| 274 | return true; |
| 275 | } |
| 276 | return false; // success |
| 277 | } |
| 278 | |
| 279 | |
| 280 | |
| 281 | bool LTOCodeGenerator::determineTarget(std::string& errMsg) |
| 282 | { |
| 283 | if ( _target == NULL ) { |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 284 | std::string Triple = _linker.getModule()->getTargetTriple(); |
| 285 | if (Triple.empty()) |
| 286 | Triple = sys::getHostTriple(); |
| 287 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 288 | // create target machine from info for merged modules |
Daniel Dunbar | 4bd03ab | 2009-08-03 04:20:57 +0000 | [diff] [blame] | 289 | const Target *march = TargetRegistry::lookupTarget(Triple, errMsg); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 290 | if ( march == NULL ) |
| 291 | return true; |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 292 | |
Nick Kledzik | f5a1c35f1 | 2009-06-03 22:52:12 +0000 | [diff] [blame] | 293 | // The relocation model is actually a static member of TargetMachine |
| 294 | // and needs to be set before the TargetMachine is instantiated. |
| 295 | switch( _codeModel ) { |
| 296 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 297 | TargetMachine::setRelocationModel(Reloc::Static); |
| 298 | break; |
| 299 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 300 | TargetMachine::setRelocationModel(Reloc::PIC_); |
| 301 | break; |
| 302 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 303 | TargetMachine::setRelocationModel(Reloc::DynamicNoPIC); |
| 304 | break; |
| 305 | } |
| 306 | |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 307 | // construct LTModule, hand over ownership of module and target |
Viktor Kutuzov | 308f663 | 2009-11-25 22:44:18 +0000 | [diff] [blame] | 308 | const std::string FeatureStr = |
| 309 | SubtargetFeatures::getDefaultSubtargetFeatures(llvm::Triple(Triple)); |
| 310 | _target = march->createTargetMachine(Triple, FeatureStr); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 311 | } |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | void LTOCodeGenerator::applyScopeRestrictions() |
| 316 | { |
| 317 | if ( !_scopeRestrictionsDone ) { |
| 318 | Module* mergedModule = _linker.getModule(); |
| 319 | |
| 320 | // Start off with a verification pass. |
| 321 | PassManager passes; |
| 322 | passes.add(createVerifierPass()); |
| 323 | |
| 324 | // mark which symbols can not be internalized |
| 325 | if ( !_mustPreserveSymbols.empty() ) { |
| 326 | Mangler mangler(*mergedModule, |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 327 | _target->getMCAsmInfo()->getGlobalPrefix()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 328 | std::vector<const char*> mustPreserveList; |
| 329 | for (Module::iterator f = mergedModule->begin(), |
| 330 | e = mergedModule->end(); f != e; ++f) { |
| 331 | if ( !f->isDeclaration() |
Chris Lattner | 4693404 | 2010-01-16 18:12:14 +0000 | [diff] [blame] | 332 | && _mustPreserveSymbols.count(mangler.getNameWithPrefix(f)) ) |
Daniel Dunbar | 3d5126f | 2009-07-22 21:33:09 +0000 | [diff] [blame] | 333 | mustPreserveList.push_back(::strdup(f->getNameStr().c_str())); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 334 | } |
| 335 | for (Module::global_iterator v = mergedModule->global_begin(), |
| 336 | e = mergedModule->global_end(); v != e; ++v) { |
| 337 | if ( !v->isDeclaration() |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 338 | && _mustPreserveSymbols.count(mangler.getMangledName(v)) ) |
Daniel Dunbar | 3d5126f | 2009-07-22 21:33:09 +0000 | [diff] [blame] | 339 | mustPreserveList.push_back(::strdup(v->getNameStr().c_str())); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 340 | } |
| 341 | passes.add(createInternalizePass(mustPreserveList)); |
| 342 | } |
| 343 | // apply scope restrictions |
| 344 | passes.run(*mergedModule); |
| 345 | |
| 346 | _scopeRestrictionsDone = true; |
| 347 | } |
| 348 | } |
| 349 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 350 | /// Optimize merged modules using various IPO passes |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 351 | bool LTOCodeGenerator::generateAssemblyCode(formatted_raw_ostream& out, |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 352 | std::string& errMsg) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 353 | { |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 354 | if ( this->determineTarget(errMsg) ) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 355 | return true; |
| 356 | |
| 357 | // mark which symbols can not be internalized |
| 358 | this->applyScopeRestrictions(); |
| 359 | |
| 360 | Module* mergedModule = _linker.getModule(); |
| 361 | |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 362 | // If target supports exception handling then enable it now. |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 363 | switch (_target->getMCAsmInfo()->getExceptionHandlingType()) { |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 364 | case ExceptionHandling::Dwarf: |
| 365 | llvm::DwarfExceptionHandling = true; |
| 366 | break; |
| 367 | case ExceptionHandling::SjLj: |
| 368 | llvm::SjLjExceptionHandling = true; |
| 369 | break; |
| 370 | case ExceptionHandling::None: |
| 371 | break; |
| 372 | default: |
| 373 | assert (0 && "Unknown exception handling model!"); |
| 374 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 375 | |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 376 | // if options were requested, set them |
| 377 | if ( !_codegenOptions.empty() ) |
| 378 | cl::ParseCommandLineOptions(_codegenOptions.size(), |
| 379 | (char**)&_codegenOptions[0]); |
Devang Patel | a93ae71 | 2008-07-03 22:53:14 +0000 | [diff] [blame] | 380 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 381 | // Instantiate the pass manager to organize the passes. |
| 382 | PassManager passes; |
| 383 | |
| 384 | // Start off with a verification pass. |
| 385 | passes.add(createVerifierPass()); |
| 386 | |
| 387 | // Add an appropriate TargetData instance for this module... |
| 388 | passes.add(new TargetData(*_target->getTargetData())); |
| 389 | |
Daniel Dunbar | 006a034 | 2009-06-03 21:06:14 +0000 | [diff] [blame] | 390 | createStandardLTOPasses(&passes, /*Internalize=*/ false, !DisableInline, |
Daniel Dunbar | 006a034 | 2009-06-03 21:06:14 +0000 | [diff] [blame] | 391 | /*VerifyEach=*/ false); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 392 | |
| 393 | // Make sure everything is still good. |
| 394 | passes.add(createVerifierPass()); |
| 395 | |
| 396 | FunctionPassManager* codeGenPasses = |
| 397 | new FunctionPassManager(new ExistingModuleProvider(mergedModule)); |
| 398 | |
| 399 | codeGenPasses->add(new TargetData(*_target->getTargetData())); |
| 400 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 401 | ObjectCodeEmitter* oce = NULL; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 402 | |
| 403 | switch (_target->addPassesToEmitFile(*codeGenPasses, out, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 404 | TargetMachine::AssemblyFile, |
Bill Wendling | b8cb0bb | 2009-04-29 23:40:42 +0000 | [diff] [blame] | 405 | CodeGenOpt::Aggressive)) { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 406 | case FileModel::ElfFile: |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 407 | oce = AddELFWriter(*codeGenPasses, out, *_target); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 408 | break; |
| 409 | case FileModel::AsmFile: |
| 410 | break; |
Nate Begeman | 3fe980b | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 411 | case FileModel::MachOFile: |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 412 | case FileModel::Error: |
| 413 | case FileModel::None: |
| 414 | errMsg = "target file type not supported"; |
| 415 | return true; |
| 416 | } |
| 417 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 418 | if (_target->addPassesToEmitFileFinish(*codeGenPasses, oce, |
Bill Wendling | b8cb0bb | 2009-04-29 23:40:42 +0000 | [diff] [blame] | 419 | CodeGenOpt::Aggressive)) { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 420 | errMsg = "target does not support generation of this file type"; |
| 421 | return true; |
| 422 | } |
| 423 | |
| 424 | // Run our queue of passes all at once now, efficiently. |
| 425 | passes.run(*mergedModule); |
| 426 | |
| 427 | // Run the code generator, and write assembly file |
| 428 | codeGenPasses->doInitialization(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 429 | |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 430 | for (Module::iterator |
| 431 | it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it) |
| 432 | if (!it->isDeclaration()) |
| 433 | codeGenPasses->run(*it); |
| 434 | |
| 435 | codeGenPasses->doFinalization(); |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 436 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 437 | return false; // success |
| 438 | } |
| 439 | |
| 440 | |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 441 | /// Optimize merged modules using various IPO passes |
| 442 | void LTOCodeGenerator::setCodeGenDebugOptions(const char* options) |
| 443 | { |
Benjamin Kramer | d4f1959 | 2010-01-11 18:03:24 +0000 | [diff] [blame] | 444 | for (std::pair<StringRef, StringRef> o = getToken(options); |
| 445 | !o.first.empty(); o = getToken(o.second)) { |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 446 | // ParseCommandLineOptions() expects argv[0] to be program name. |
| 447 | // Lazily add that. |
| 448 | if ( _codegenOptions.empty() ) |
| 449 | _codegenOptions.push_back("libLTO"); |
Benjamin Kramer | d4f1959 | 2010-01-11 18:03:24 +0000 | [diff] [blame] | 450 | _codegenOptions.push_back(strdup(o.first.str().c_str())); |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 451 | } |
| 452 | } |