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" |
Dan Gohman | 9f36c4e | 2010-10-07 20:48:46 +0000 | [diff] [blame] | 42 | #include "llvm/Support/ToolOutputFile.h" |
Michael J. Spencer | 3cc52ea | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Host.h" |
| 44 | #include "llvm/Support/Program.h" |
| 45 | #include "llvm/Support/Signals.h" |
Michael J. Spencer | f2f516f | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 46 | #include "llvm/Support/system_error.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 47 | #include "llvm/Config/config.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 48 | #include <cstdlib> |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 49 | #include <unistd.h> |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 50 | #include <fcntl.h> |
| 51 | |
| 52 | |
| 53 | using namespace llvm; |
| 54 | |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 55 | static cl::opt<bool> DisableInline("disable-inlining", |
| 56 | cl::desc("Do not run the inliner pass")); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 57 | |
| 58 | |
| 59 | const char* LTOCodeGenerator::getVersionString() |
| 60 | { |
| 61 | #ifdef LLVM_VERSION_INFO |
| 62 | return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO; |
| 63 | #else |
| 64 | return PACKAGE_NAME " version " PACKAGE_VERSION; |
| 65 | #endif |
| 66 | } |
| 67 | |
| 68 | |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 69 | LTOCodeGenerator::LTOCodeGenerator() |
| 70 | : _context(getGlobalContext()), |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 71 | _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL), |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 72 | _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false), |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 73 | _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 74 | _nativeObjectFile(NULL) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 75 | { |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 76 | InitializeAllTargets(); |
| 77 | InitializeAllAsmPrinters(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | LTOCodeGenerator::~LTOCodeGenerator() |
| 81 | { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 82 | delete _target; |
| 83 | delete _nativeObjectFile; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) |
| 89 | { |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 90 | |
| 91 | if(mod->getLLVVMModule()->MaterializeAllPermanently(&errMsg)) |
| 92 | return true; |
| 93 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 94 | bool ret = _linker.LinkInModule(mod->getLLVVMModule(), &errMsg); |
| 95 | |
| 96 | const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs(); |
| 97 | for (int i = 0, e = undefs.size(); i != e; ++i) |
| 98 | _asmUndefinedRefs[undefs[i]] = 1; |
| 99 | |
| 100 | return ret; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | |
| 104 | bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg) |
| 105 | { |
| 106 | switch (debug) { |
| 107 | case LTO_DEBUG_MODEL_NONE: |
| 108 | _emitDwarfDebugInfo = false; |
| 109 | return false; |
| 110 | |
| 111 | case LTO_DEBUG_MODEL_DWARF: |
| 112 | _emitDwarfDebugInfo = true; |
| 113 | return false; |
| 114 | } |
| 115 | errMsg = "unknown debug format"; |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model, |
Evan Cheng | 855a168 | 2009-06-26 06:57:16 +0000 | [diff] [blame] | 121 | std::string& errMsg) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 122 | { |
| 123 | switch (model) { |
| 124 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 125 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 126 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 127 | _codeModel = model; |
| 128 | return false; |
| 129 | } |
| 130 | errMsg = "unknown pic model"; |
| 131 | return true; |
| 132 | } |
| 133 | |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 134 | void LTOCodeGenerator::setCpu(const char* mCpu) |
| 135 | { |
| 136 | _mCpu = mCpu; |
| 137 | } |
| 138 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 139 | void LTOCodeGenerator::addMustPreserveSymbol(const char* sym) |
| 140 | { |
| 141 | _mustPreserveSymbols[sym] = 1; |
| 142 | } |
| 143 | |
| 144 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 145 | bool LTOCodeGenerator::writeMergedModules(const char *path, |
| 146 | std::string &errMsg) { |
| 147 | if (determineTarget(errMsg)) |
| 148 | return true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 149 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 150 | // mark which symbols can not be internalized |
| 151 | applyScopeRestrictions(); |
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 | // create output file |
| 154 | std::string ErrInfo; |
Dan Gohman | f291401 | 2010-08-20 16:59:15 +0000 | [diff] [blame] | 155 | tool_output_file Out(path, ErrInfo, |
| 156 | raw_fd_ostream::F_Binary); |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 157 | if (!ErrInfo.empty()) { |
| 158 | errMsg = "could not open bitcode file for writing: "; |
| 159 | errMsg += path; |
| 160 | return true; |
| 161 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 162 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 163 | // write bitcode to it |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 164 | WriteBitcodeToFile(_linker.getModule(), Out.os()); |
| 165 | Out.os().close(); |
Dan Gohman | 4b7416b | 2010-05-27 20:19:47 +0000 | [diff] [blame] | 166 | |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 167 | if (Out.os().has_error()) { |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 168 | errMsg = "could not write bitcode file: "; |
| 169 | errMsg += path; |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 170 | Out.os().clear_error(); |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 171 | return true; |
| 172 | } |
| 173 | |
Dan Gohman | f291401 | 2010-08-20 16:59:15 +0000 | [diff] [blame] | 174 | Out.keep(); |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 175 | return false; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 179 | bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) |
| 180 | { |
| 181 | // make unique temp .o file to put generated object file |
| 182 | sys::PathWithStatus uniqueObjPath("lto-llvm.o"); |
| 183 | if ( uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg) ) { |
| 184 | uniqueObjPath.eraseFromDisk(); |
| 185 | return true; |
| 186 | } |
| 187 | sys::RemoveFileOnSignal(uniqueObjPath); |
| 188 | |
| 189 | // generate object file |
| 190 | bool genResult = false; |
| 191 | tool_output_file objFile(uniqueObjPath.c_str(), errMsg); |
| 192 | if (!errMsg.empty()) |
| 193 | return NULL; |
| 194 | genResult = this->generateObjectFile(objFile.os(), errMsg); |
| 195 | objFile.os().close(); |
| 196 | if (objFile.os().has_error()) { |
| 197 | objFile.os().clear_error(); |
| 198 | return true; |
| 199 | } |
| 200 | objFile.keep(); |
| 201 | if ( genResult ) { |
| 202 | uniqueObjPath.eraseFromDisk(); |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | _nativeObjectPath = uniqueObjPath.str(); |
| 207 | *name = _nativeObjectPath.c_str(); |
| 208 | return false; |
| 209 | } |
| 210 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 211 | const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 212 | { |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 213 | const char *name; |
| 214 | if (compile_to_file(&name, errMsg)) |
| 215 | return NULL; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 216 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 217 | // remove old buffer if compile() called twice |
| 218 | delete _nativeObjectFile; |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 219 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 220 | // read .o file into memory buffer |
| 221 | OwningPtr<MemoryBuffer> BuffPtr; |
| 222 | if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) { |
| 223 | errMsg = ec.message(); |
| 224 | return NULL; |
| 225 | } |
| 226 | _nativeObjectFile = BuffPtr.take(); |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 227 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 228 | // remove temp files |
| 229 | sys::Path(_nativeObjectPath).eraseFromDisk(); |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 230 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 231 | // return buffer, unless error |
| 232 | if ( _nativeObjectFile == NULL ) |
| 233 | return NULL; |
| 234 | *length = _nativeObjectFile->getBufferSize(); |
| 235 | return _nativeObjectFile->getBufferStart(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 238 | bool LTOCodeGenerator::determineTarget(std::string& errMsg) |
| 239 | { |
| 240 | if ( _target == NULL ) { |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 241 | std::string Triple = _linker.getModule()->getTargetTriple(); |
| 242 | if (Triple.empty()) |
| 243 | Triple = sys::getHostTriple(); |
| 244 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 245 | // create target machine from info for merged modules |
Daniel Dunbar | 4bd03ab | 2009-08-03 04:20:57 +0000 | [diff] [blame] | 246 | const Target *march = TargetRegistry::lookupTarget(Triple, errMsg); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 247 | if ( march == NULL ) |
| 248 | return true; |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 249 | |
Nick Kledzik | f5a1c35f1 | 2009-06-03 22:52:12 +0000 | [diff] [blame] | 250 | // The relocation model is actually a static member of TargetMachine |
| 251 | // and needs to be set before the TargetMachine is instantiated. |
| 252 | switch( _codeModel ) { |
| 253 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 254 | TargetMachine::setRelocationModel(Reloc::Static); |
| 255 | break; |
| 256 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 257 | TargetMachine::setRelocationModel(Reloc::PIC_); |
| 258 | break; |
| 259 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 260 | TargetMachine::setRelocationModel(Reloc::DynamicNoPIC); |
| 261 | break; |
| 262 | } |
| 263 | |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 264 | // construct LTModule, hand over ownership of module and target |
Bill Wendling | 81043ee | 2010-05-11 00:30:02 +0000 | [diff] [blame] | 265 | SubtargetFeatures Features; |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 266 | Features.getDefaultSubtargetFeatures(_mCpu, llvm::Triple(Triple)); |
Bill Wendling | 81043ee | 2010-05-11 00:30:02 +0000 | [diff] [blame] | 267 | std::string FeatureStr = Features.getString(); |
Viktor Kutuzov | 308f663 | 2009-11-25 22:44:18 +0000 | [diff] [blame] | 268 | _target = march->createTargetMachine(Triple, FeatureStr); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 269 | } |
| 270 | return false; |
| 271 | } |
| 272 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 273 | void LTOCodeGenerator::applyRestriction(GlobalValue &GV, |
| 274 | std::vector<const char*> &mustPreserveList, |
| 275 | SmallPtrSet<GlobalValue*, 8> &asmUsed, |
| 276 | Mangler &mangler) { |
| 277 | SmallString<64> Buffer; |
| 278 | mangler.getNameWithPrefix(Buffer, &GV, false); |
| 279 | |
| 280 | if (GV.isDeclaration()) |
| 281 | return; |
| 282 | if (_mustPreserveSymbols.count(Buffer)) |
| 283 | mustPreserveList.push_back(GV.getName().data()); |
| 284 | if (_asmUndefinedRefs.count(Buffer)) |
| 285 | asmUsed.insert(&GV); |
| 286 | } |
| 287 | |
| 288 | static void findUsedValues(GlobalVariable *LLVMUsed, |
| 289 | SmallPtrSet<GlobalValue*, 8> &UsedValues) { |
| 290 | if (LLVMUsed == 0) return; |
| 291 | |
| 292 | ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer()); |
| 293 | if (Inits == 0) return; |
| 294 | |
| 295 | for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) |
| 296 | if (GlobalValue *GV = |
| 297 | dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts())) |
| 298 | UsedValues.insert(GV); |
| 299 | } |
| 300 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 301 | void LTOCodeGenerator::applyScopeRestrictions() { |
| 302 | if (_scopeRestrictionsDone) return; |
| 303 | Module *mergedModule = _linker.getModule(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 305 | // Start off with a verification pass. |
| 306 | PassManager passes; |
| 307 | passes.add(createVerifierPass()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 309 | // mark which symbols can not be internalized |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 310 | MCContext Context(*_target->getMCAsmInfo(), NULL); |
| 311 | Mangler mangler(Context, *_target->getTargetData()); |
| 312 | std::vector<const char*> mustPreserveList; |
| 313 | SmallPtrSet<GlobalValue*, 8> asmUsed; |
| 314 | |
| 315 | for (Module::iterator f = mergedModule->begin(), |
| 316 | e = mergedModule->end(); f != e; ++f) |
| 317 | applyRestriction(*f, mustPreserveList, asmUsed, mangler); |
| 318 | for (Module::global_iterator v = mergedModule->global_begin(), |
| 319 | e = mergedModule->global_end(); v != e; ++v) |
| 320 | applyRestriction(*v, mustPreserveList, asmUsed, mangler); |
| 321 | for (Module::alias_iterator a = mergedModule->alias_begin(), |
| 322 | e = mergedModule->alias_end(); a != e; ++a) |
| 323 | applyRestriction(*a, mustPreserveList, asmUsed, mangler); |
| 324 | |
| 325 | GlobalVariable *LLVMCompilerUsed = |
| 326 | mergedModule->getGlobalVariable("llvm.compiler.used"); |
| 327 | findUsedValues(LLVMCompilerUsed, asmUsed); |
| 328 | if (LLVMCompilerUsed) |
| 329 | LLVMCompilerUsed->eraseFromParent(); |
| 330 | |
| 331 | const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context); |
| 332 | std::vector<Constant*> asmUsed2; |
| 333 | for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(), |
| 334 | e = asmUsed.end(); i !=e; ++i) { |
| 335 | GlobalValue *GV = *i; |
| 336 | Constant *c = ConstantExpr::getBitCast(GV, i8PTy); |
| 337 | asmUsed2.push_back(c); |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 338 | } |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 339 | |
| 340 | llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size()); |
| 341 | LLVMCompilerUsed = |
| 342 | new llvm::GlobalVariable(*mergedModule, ATy, false, |
| 343 | llvm::GlobalValue::AppendingLinkage, |
| 344 | llvm::ConstantArray::get(ATy, asmUsed2), |
| 345 | "llvm.compiler.used"); |
| 346 | |
| 347 | LLVMCompilerUsed->setSection("llvm.metadata"); |
| 348 | |
| 349 | passes.add(createInternalizePass(mustPreserveList)); |
| 350 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 351 | // apply scope restrictions |
| 352 | passes.run(*mergedModule); |
| 353 | |
| 354 | _scopeRestrictionsDone = true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 357 | /// Optimize merged modules using various IPO passes |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 358 | bool LTOCodeGenerator::generateObjectFile(raw_ostream& out, |
| 359 | std::string& errMsg) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 360 | { |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 361 | if ( this->determineTarget(errMsg) ) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 362 | return true; |
| 363 | |
| 364 | // mark which symbols can not be internalized |
| 365 | this->applyScopeRestrictions(); |
| 366 | |
| 367 | Module* mergedModule = _linker.getModule(); |
| 368 | |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 369 | // if options were requested, set them |
| 370 | if ( !_codegenOptions.empty() ) |
| 371 | cl::ParseCommandLineOptions(_codegenOptions.size(), |
Dan Gohman | 43bc70e | 2010-04-17 17:44:03 +0000 | [diff] [blame] | 372 | const_cast<char **>(&_codegenOptions[0])); |
Devang Patel | a93ae71 | 2008-07-03 22:53:14 +0000 | [diff] [blame] | 373 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 374 | // Instantiate the pass manager to organize the passes. |
| 375 | PassManager passes; |
| 376 | |
| 377 | // Start off with a verification pass. |
| 378 | passes.add(createVerifierPass()); |
| 379 | |
| 380 | // Add an appropriate TargetData instance for this module... |
| 381 | passes.add(new TargetData(*_target->getTargetData())); |
| 382 | |
Daniel Dunbar | 006a034 | 2009-06-03 21:06:14 +0000 | [diff] [blame] | 383 | createStandardLTOPasses(&passes, /*Internalize=*/ false, !DisableInline, |
Daniel Dunbar | 006a034 | 2009-06-03 21:06:14 +0000 | [diff] [blame] | 384 | /*VerifyEach=*/ false); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 385 | |
| 386 | // Make sure everything is still good. |
| 387 | passes.add(createVerifierPass()); |
| 388 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 389 | FunctionPassManager* codeGenPasses = new FunctionPassManager(mergedModule); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 390 | |
| 391 | codeGenPasses->add(new TargetData(*_target->getTargetData())); |
| 392 | |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 393 | formatted_raw_ostream Out(out); |
| 394 | |
| 395 | if (_target->addPassesToEmitFile(*codeGenPasses, Out, |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 396 | TargetMachine::CGFT_ObjectFile, |
Chris Lattner | 5669e30 | 2010-02-03 05:55:08 +0000 | [diff] [blame] | 397 | CodeGenOpt::Aggressive)) { |
| 398 | errMsg = "target file type not supported"; |
| 399 | return true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 402 | // Run our queue of passes all at once now, efficiently. |
| 403 | passes.run(*mergedModule); |
| 404 | |
| 405 | // Run the code generator, and write assembly file |
| 406 | codeGenPasses->doInitialization(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 407 | |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 408 | for (Module::iterator |
| 409 | it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it) |
| 410 | if (!it->isDeclaration()) |
| 411 | codeGenPasses->run(*it); |
| 412 | |
| 413 | codeGenPasses->doFinalization(); |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 414 | delete codeGenPasses; |
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 | } |