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