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