OLD | NEW |
1 //===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===// | 1 //===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This file implements the Link Time Optimization library. This library is | 10 // This file implements the Link Time Optimization library. This library is |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) { | 94 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) { |
95 bool ret = _linker.LinkInModule(mod->getLLVVMModule(), &errMsg); | 95 bool ret = _linker.LinkInModule(mod->getLLVVMModule(), &errMsg); |
96 | 96 |
97 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs(); | 97 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs(); |
98 for (int i = 0, e = undefs.size(); i != e; ++i) | 98 for (int i = 0, e = undefs.size(); i != e; ++i) |
99 _asmUndefinedRefs[undefs[i]] = 1; | 99 _asmUndefinedRefs[undefs[i]] = 1; |
100 | 100 |
101 return ret; | 101 return ret; |
102 } | 102 } |
103 | 103 |
| 104 // @LOCALMOD-BEGIN |
| 105 /// Add a module that will be merged with the final output module. |
| 106 /// The merging does not happen until linkGatheredModulesAndDispose(). |
| 107 bool LTOCodeGenerator::gatherModuleForLinking(LTOModule* mod) { |
| 108 _gatheredModules.push_back(mod); |
| 109 } |
| 110 |
| 111 /// Merge all modules gathered from gatherModuleForLinking(), and |
| 112 /// destroy the source modules in the process. |
| 113 bool LTOCodeGenerator::linkGatheredModulesAndDispose(std::string& errMsg) { |
| 114 |
| 115 // We gather the asm undefs earlier than addModule() does, |
| 116 // since we delete the modules during linking, and would not be |
| 117 // able to do this after linking. The undefs vector contain lists |
| 118 // of global variable names which are considered "used", which will be |
| 119 // appended into the "llvm.compiler.used" list. The names must be the |
| 120 // same before linking as they are after linking, since we have switched |
| 121 // the order. |
| 122 for (unsigned i = 0, ei = _gatheredModules.size(); i != ei; ++i) { |
| 123 const std::vector<const char*> &undefs = |
| 124 _gatheredModules[i]->getAsmUndefinedRefs(); |
| 125 for (int j = 0, ej = undefs.size(); j != ej; ++j) { |
| 126 _asmUndefinedRefs[undefs[j]] = 1; |
| 127 } |
| 128 } |
| 129 |
| 130 // Tree-reduce the mods, re-using the incoming mods as scratch |
| 131 // intermediate results. Module i is linked with (i + stride), with i as |
| 132 // the dest. We begin with a stride of 1, and double each time. E.g., |
| 133 // after the first round, only the even-indexed modules are still available, |
| 134 // and after the second, only those with index that are a multiple of 4 |
| 135 // are available. Eventually the Module with the content of all other modules |
| 136 // will be Module 0. |
| 137 // NOTE: we may be able to be smarter about linking if we did not do them |
| 138 // pairwise using Linker::LinkModules. We also disregard module sizes |
| 139 // and try our best to keep the modules in order (linking adjacent modules). |
| 140 for (unsigned stride = 1, len = _gatheredModules.size(); |
| 141 stride < len; |
| 142 stride *= 2) { |
| 143 for (unsigned i = 0; i + stride < len; i = i + (stride * 2)) { |
| 144 if (Linker::LinkModules(_gatheredModules[i]->getLLVVMModule(), |
| 145 _gatheredModules[i+stride]->getLLVVMModule(), |
| 146 Linker::DestroySource, &errMsg)) { |
| 147 errs() << "LinkModules " << i << " w/ " << i + stride << " failed...\n"; |
| 148 // We leak the memory in this case... |
| 149 return true; |
| 150 } |
| 151 delete _gatheredModules[i+stride]; |
| 152 } |
| 153 } |
| 154 |
| 155 // Finally, link Node 0 with the Dest and delete Node 0. |
| 156 if (_linker.LinkInModule(_gatheredModules[0]->getLLVVMModule(), &errMsg)) { |
| 157 errs() << "LinkModules Dst w/ _gatheredModules[0] failed...\n"; |
| 158 return true; |
| 159 } |
| 160 delete _gatheredModules[0]; |
| 161 |
| 162 return false; |
| 163 } |
| 164 // @LOCALMOD-END |
| 165 |
104 bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, | 166 bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, |
105 std::string& errMsg) { | 167 std::string& errMsg) { |
106 switch (debug) { | 168 switch (debug) { |
107 case LTO_DEBUG_MODEL_NONE: | 169 case LTO_DEBUG_MODEL_NONE: |
108 _emitDwarfDebugInfo = false; | 170 _emitDwarfDebugInfo = false; |
109 return false; | 171 return false; |
110 | 172 |
111 case LTO_DEBUG_MODEL_DWARF: | 173 case LTO_DEBUG_MODEL_DWARF: |
112 _emitDwarfDebugInfo = true; | 174 _emitDwarfDebugInfo = true; |
113 return false; | 175 return false; |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) { | 548 void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) { |
487 for (std::pair<StringRef, StringRef> o = getToken(options); | 549 for (std::pair<StringRef, StringRef> o = getToken(options); |
488 !o.first.empty(); o = getToken(o.second)) { | 550 !o.first.empty(); o = getToken(o.second)) { |
489 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add | 551 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add |
490 // that. | 552 // that. |
491 if ( _codegenOptions.empty() ) | 553 if ( _codegenOptions.empty() ) |
492 _codegenOptions.push_back(strdup("libLTO")); | 554 _codegenOptions.push_back(strdup("libLTO")); |
493 _codegenOptions.push_back(strdup(o.first.str().c_str())); | 555 _codegenOptions.push_back(strdup(o.first.str().c_str())); |
494 } | 556 } |
495 } | 557 } |
OLD | NEW |