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