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