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. |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 7 | // |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 10 | // This file implements the Link Time Optimization library. This library is |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 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 "LTOCodeGenerator.h" |
Bill Wendling | ab53bc7 | 2012-03-31 11:10:35 +0000 | [diff] [blame] | 16 | #include "LTOModule.h" |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 17 | #include "LTOPartition.h" |
| 18 | #include "LTOPostIPODriver.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/Passes.h" |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/Verifier.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/ReaderWriter.h" |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 23 | #include "llvm/Config/config.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
| 25 | #include "llvm/IR/DataLayout.h" |
| 26 | #include "llvm/IR/DerivedTypes.h" |
| 27 | #include "llvm/IR/LLVMContext.h" |
| 28 | #include "llvm/IR/Module.h" |
Shuxin Yang | 8945f75 | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 29 | #include "llvm/InitializePasses.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 30 | #include "llvm/Linker.h" |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCAsmInfo.h" |
| 32 | #include "llvm/MC/MCContext.h" |
Evan Cheng | ab8be96 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 33 | #include "llvm/MC/SubtargetFeature.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 34 | #include "llvm/PassManager.h" |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" |
Rafael Espindola | 68c0efa | 2013-06-17 18:05:35 +0000 | [diff] [blame] | 36 | #include "llvm/Support/FileSystem.h" |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 37 | #include "llvm/Support/FormattedStream.h" |
Michael J. Spencer | 3cc52ea | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Host.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MemoryBuffer.h" |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 40 | #include "llvm/Support/Program.h" |
Michael J. Spencer | 3cc52ea | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Signals.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 42 | #include "llvm/Support/TargetRegistry.h" |
| 43 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 44 | #include "llvm/Support/ToolOutputFile.h" |
Michael J. Spencer | f2f516f | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 45 | #include "llvm/Support/system_error.h" |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 46 | #include "llvm/Support/SourceMgr.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 47 | #include "llvm/Target/Mangler.h" |
| 48 | #include "llvm/Target/TargetMachine.h" |
| 49 | #include "llvm/Target/TargetOptions.h" |
| 50 | #include "llvm/Target/TargetRegisterInfo.h" |
| 51 | #include "llvm/Transforms/IPO.h" |
| 52 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Bob Wilson | d696544 | 2013-03-29 23:28:55 +0000 | [diff] [blame] | 53 | #include "llvm/Transforms/ObjCARC.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 54 | |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 55 | using namespace llvm; |
| 56 | using namespace lto; |
| 57 | |
| 58 | // ///////////////////////////////////////////////////////////////////////////// |
| 59 | // |
| 60 | // Internal options. To avoid collision, most options start with "lto-". |
| 61 | // |
| 62 | // ///////////////////////////////////////////////////////////////////////////// |
| 63 | // |
Bill Wendling | 9ac0aaa | 2012-08-06 21:34:54 +0000 | [diff] [blame] | 64 | static cl::opt<bool> |
Bill Wendling | 6303b66 | 2013-02-28 14:11:10 +0000 | [diff] [blame] | 65 | DisableOpt("disable-opt", cl::init(false), |
| 66 | cl::desc("Do not run any optimization passes")); |
| 67 | |
| 68 | static cl::opt<bool> |
Bill Wendling | 9ac0aaa | 2012-08-06 21:34:54 +0000 | [diff] [blame] | 69 | DisableInline("disable-inlining", cl::init(false), |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 70 | cl::desc("Do not run the inliner pass")); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 71 | |
Bill Wendling | 9ac0aaa | 2012-08-06 21:34:54 +0000 | [diff] [blame] | 72 | static cl::opt<bool> |
| 73 | DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false), |
Bill Wendling | 3197b44 | 2012-04-02 22:16:50 +0000 | [diff] [blame] | 74 | cl::desc("Do not run the GVN load PRE pass")); |
| 75 | |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 76 | // To break merged module into partitions, and compile them independently. |
| 77 | static cl::opt<bool> |
| 78 | EnablePartition("lto-partition", cl::init(false), |
| 79 | cl::desc("Partition program and compile each piece in parallel")); |
| 80 | |
| 81 | // Specify the work-directory for the LTO compilation. All intermeidate |
| 82 | // files will be created immediately under this dir. If it is not |
| 83 | // specified, compiler will create an unique directory under current-dir. |
| 84 | // |
| 85 | static cl::opt<std::string> |
| 86 | TmpWorkDir("lto-workdir", cl::init(""), cl::desc("Specify working directory")); |
| 87 | |
| 88 | static cl::opt<bool> |
| 89 | KeepWorkDir("lto-keep", cl::init(false), cl::desc("Keep working directory")); |
| 90 | |
| 91 | |
| 92 | // ///////////////////////////////////////////////////////////////////////////// |
| 93 | // |
| 94 | // Implementation of LTOCodeGenerator |
| 95 | // |
| 96 | // ///////////////////////////////////////////////////////////////////////////// |
| 97 | // |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 98 | const char* LTOCodeGenerator::getVersionString() { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 99 | #ifdef LLVM_VERSION_INFO |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 100 | return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 101 | #else |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 102 | return PACKAGE_NAME " version " PACKAGE_VERSION; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 103 | #endif |
| 104 | } |
| 105 | |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 106 | LTOCodeGenerator::LTOCodeGenerator() |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 107 | : _context(getGlobalContext()), |
Rafael Espindola | 1051937 | 2013-05-04 02:43:00 +0000 | [diff] [blame] | 108 | _linker(new Module("ld-temp.o", _context)), _target(NULL), |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 109 | _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false), |
Bill Wendling | 50f3183 | 2012-12-10 21:33:45 +0000 | [diff] [blame] | 110 | _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 111 | _nativeObjectFile(NULL), PartitionMgr(FileMgr), |
| 112 | OptionsParsed(false) { |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 113 | InitializeAllTargets(); |
| 114 | InitializeAllTargetMCs(); |
| 115 | InitializeAllAsmPrinters(); |
Shuxin Yang | 8945f75 | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 116 | initializeLTOPasses(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 119 | LTOCodeGenerator::~LTOCodeGenerator() { |
| 120 | delete _target; |
| 121 | delete _nativeObjectFile; |
Rafael Espindola | 1051937 | 2013-05-04 02:43:00 +0000 | [diff] [blame] | 122 | delete _linker.getModule(); |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 123 | |
Bill Wendling | f2cc2ee | 2012-03-31 10:51:45 +0000 | [diff] [blame] | 124 | for (std::vector<char*>::iterator I = _codegenOptions.begin(), |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 125 | E = _codegenOptions.end(); I != E; ++I) |
| 126 | free(*I); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Shuxin Yang | 8945f75 | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 129 | // Initialize LTO passes. Please keep this funciton in sync with |
Shuxin Yang | c679d6b | 2013-07-23 06:44:34 +0000 | [diff] [blame] | 130 | // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO |
Shuxin Yang | 8945f75 | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 131 | // passes are initialized. |
| 132 | // |
| 133 | void LTOCodeGenerator::initializeLTOPasses() { |
| 134 | PassRegistry &R = *PassRegistry::getPassRegistry(); |
| 135 | |
| 136 | initializeInternalizePassPass(R); |
| 137 | initializeIPSCCPPass(R); |
| 138 | initializeGlobalOptPass(R); |
| 139 | initializeConstantMergePass(R); |
| 140 | initializeDAHPass(R); |
| 141 | initializeInstCombinerPass(R); |
| 142 | initializeSimpleInlinerPass(R); |
| 143 | initializePruneEHPass(R); |
| 144 | initializeGlobalDCEPass(R); |
| 145 | initializeArgPromotionPass(R); |
| 146 | initializeJumpThreadingPass(R); |
| 147 | initializeSROAPass(R); |
| 148 | initializeSROA_DTPass(R); |
| 149 | initializeSROA_SSAUpPass(R); |
| 150 | initializeFunctionAttrsPass(R); |
| 151 | initializeGlobalsModRefPass(R); |
| 152 | initializeLICMPass(R); |
| 153 | initializeGVNPass(R); |
| 154 | initializeMemCpyOptPass(R); |
| 155 | initializeDCEPass(R); |
Tom Stellard | 01d7203 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 156 | initializeCFGSimplifyPassPass(R); |
Shuxin Yang | 8945f75 | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 159 | bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) { |
Rafael Espindola | fca8863 | 2013-05-04 03:06:50 +0000 | [diff] [blame] | 160 | bool ret = _linker.linkInModule(mod->getLLVVMModule(), &errMsg); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 161 | |
| 162 | const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs(); |
| 163 | for (int i = 0, e = undefs.size(); i != e; ++i) |
| 164 | _asmUndefinedRefs[undefs[i]] = 1; |
| 165 | |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 166 | return !ret; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 167 | } |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 168 | |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 169 | void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) { |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 170 | switch (debug) { |
| 171 | case LTO_DEBUG_MODEL_NONE: |
| 172 | _emitDwarfDebugInfo = false; |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 173 | return; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 174 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 175 | case LTO_DEBUG_MODEL_DWARF: |
| 176 | _emitDwarfDebugInfo = true; |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 177 | return; |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 178 | } |
| 179 | llvm_unreachable("Unknown debug format!"); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 182 | void LTOCodeGenerator::setCodePICModel(lto_codegen_model model) { |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 183 | switch (model) { |
| 184 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 185 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 186 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 187 | _codeModel = model; |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 188 | return; |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 189 | } |
| 190 | llvm_unreachable("Unknown PIC model!"); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 193 | bool LTOCodeGenerator::writeMergedModules(const char *path, |
| 194 | std::string &errMsg) { |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 195 | if (!determineTarget(errMsg)) |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 196 | return false; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 197 | |
Bill Wendling | 6ba2ed5 | 2013-08-08 23:51:04 +0000 | [diff] [blame] | 198 | // mark which symbols can not be internalized |
| 199 | applyScopeRestrictions(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 200 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 201 | // create output file |
| 202 | std::string ErrInfo; |
Rafael Espindola | c1b49b5 | 2013-07-16 19:44:17 +0000 | [diff] [blame] | 203 | tool_output_file Out(path, ErrInfo, sys::fs::F_Binary); |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 204 | if (!ErrInfo.empty()) { |
| 205 | errMsg = "could not open bitcode file for writing: "; |
| 206 | errMsg += path; |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 207 | return false; |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 208 | } |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 209 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 210 | // write bitcode to it |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 211 | WriteBitcodeToFile(_linker.getModule(), Out.os()); |
| 212 | Out.os().close(); |
Dan Gohman | 4b7416b | 2010-05-27 20:19:47 +0000 | [diff] [blame] | 213 | |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 214 | if (Out.os().has_error()) { |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 215 | errMsg = "could not write bitcode file: "; |
| 216 | errMsg += path; |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 217 | Out.os().clear_error(); |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 218 | return false; |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 219 | } |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 220 | |
Dan Gohman | f291401 | 2010-08-20 16:59:15 +0000 | [diff] [blame] | 221 | Out.keep(); |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 222 | return true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 225 | // This function is to ensure cl::ParseCommandLineOptions() is called no more |
| 226 | // than once. It would otherwise complain and exit the compilation prematurely. |
| 227 | // |
| 228 | void LTOCodeGenerator::parseOptions() { |
| 229 | if (OptionsParsed) |
| 230 | return; |
| 231 | |
| 232 | if (!_codegenOptions.empty()) |
| 233 | cl::ParseCommandLineOptions(_codegenOptions.size(), |
| 234 | const_cast<char **>(&_codegenOptions[0])); |
| 235 | |
| 236 | OptionsParsed = true; |
| 237 | } |
| 238 | |
| 239 | // Do some prepartion right before compilation starts. |
| 240 | bool LTOCodeGenerator::prepareBeforeCompile(std::string &ErrMsg) { |
| 241 | parseOptions(); |
| 242 | |
| 243 | if (!determineTarget(ErrMsg)) |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 244 | return false; |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 245 | |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 246 | FileMgr.setWorkDir(TmpWorkDir.c_str()); |
| 247 | FileMgr.setKeepWorkDir(KeepWorkDir); |
| 248 | return FileMgr.createWorkDir(ErrMsg); |
| 249 | } |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 250 | |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 251 | bool LTOCodeGenerator::compile_to_file(const char** Name, std::string& ErrMsg) { |
| 252 | if (!prepareBeforeCompile(ErrMsg)) |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 253 | return false; |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 254 | |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 255 | performIPO(EnablePartition, ErrMsg); |
| 256 | if (!performPostIPO(ErrMsg)) |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 257 | return false; |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 258 | |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 259 | *Name = PartitionMgr.getSinglePartition()->getObjFilePath().c_str(); |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 260 | return true; |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 263 | const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) { |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 264 | const char *name; |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 265 | if (!compile_to_file(&name, errMsg)) |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 266 | return NULL; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 267 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 268 | // remove old buffer if compile() called twice |
| 269 | delete _nativeObjectFile; |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 270 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 271 | // read .o file into memory buffer |
| 272 | OwningPtr<MemoryBuffer> BuffPtr; |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 273 | const char *BufStart = 0; |
| 274 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 275 | if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) { |
| 276 | errMsg = ec.message(); |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 277 | _nativeObjectFile = 0; |
| 278 | } else { |
| 279 | if ((_nativeObjectFile = BuffPtr.take())) { |
| 280 | *length = _nativeObjectFile->getBufferSize(); |
| 281 | BufStart = _nativeObjectFile->getBufferStart(); |
| 282 | } |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 283 | } |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 284 | |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 285 | // Now that the resulting single object file is handed to linker via memory |
| 286 | // buffer, it is safe to remove all intermediate files now. |
| 287 | // |
| 288 | FileMgr.removeAllUnneededFiles(); |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 289 | |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 290 | return BufStart; |
| 291 | } |
| 292 | |
| 293 | const char *LTOCodeGenerator::getFilesNeedToRemove() { |
| 294 | IPOFileMgr::FileNameVect ToRm; |
| 295 | FileMgr.getFilesNeedToRemove(ToRm); |
| 296 | |
| 297 | ConcatStrings.clear(); |
| 298 | for (IPOFileMgr::FileNameVect::iterator I = ToRm.begin(), E = ToRm.end(); |
| 299 | I != E; I++) { |
| 300 | StringRef S(*I); |
| 301 | ConcatStrings.append(S.begin(), S.end()); |
| 302 | ConcatStrings.push_back('\0'); |
| 303 | } |
| 304 | ConcatStrings.push_back('\0'); |
| 305 | ConcatStrings.push_back('\0'); |
| 306 | |
| 307 | return ConcatStrings.data(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Bill Wendling | 8c18a6f | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 310 | bool LTOCodeGenerator::determineTarget(std::string &errMsg) { |
Bill Wendling | 0ca36af | 2012-08-08 22:03:50 +0000 | [diff] [blame] | 311 | if (_target != NULL) |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 312 | return true; |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 313 | |
Bill Wendling | 8c18a6f | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 314 | // if options were requested, set them |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 315 | parseOptions(); |
Bill Wendling | 8c18a6f | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 316 | |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 317 | std::string TripleStr = _linker.getModule()->getTargetTriple(); |
| 318 | if (TripleStr.empty()) |
| 319 | TripleStr = sys::getDefaultTargetTriple(); |
| 320 | llvm::Triple Triple(TripleStr); |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 321 | |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 322 | // create target machine from info for merged modules |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 323 | const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg); |
Bill Wendling | 0ca36af | 2012-08-08 22:03:50 +0000 | [diff] [blame] | 324 | if (march == NULL) |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 325 | return false; |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 326 | |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 327 | // The relocation model is actually a static member of TargetMachine and |
| 328 | // needs to be set before the TargetMachine is instantiated. |
| 329 | Reloc::Model RelocModel = Reloc::Default; |
Bill Wendling | 0ca36af | 2012-08-08 22:03:50 +0000 | [diff] [blame] | 330 | switch (_codeModel) { |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 331 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 332 | RelocModel = Reloc::Static; |
| 333 | break; |
| 334 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 335 | RelocModel = Reloc::PIC_; |
| 336 | break; |
| 337 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 338 | RelocModel = Reloc::DynamicNoPIC; |
| 339 | break; |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 340 | } |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 341 | |
| 342 | // construct LTOModule, hand over ownership of module and target |
| 343 | SubtargetFeatures Features; |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 344 | Features.getDefaultSubtargetFeatures(Triple); |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 345 | std::string FeatureStr = Features.getString(); |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 346 | // Set a default CPU for Darwin triples. |
| 347 | if (_mCpu.empty() && Triple.isOSDarwin()) { |
| 348 | if (Triple.getArch() == llvm::Triple::x86_64) |
| 349 | _mCpu = "core2"; |
| 350 | else if (Triple.getArch() == llvm::Triple::x86) |
| 351 | _mCpu = "yonah"; |
| 352 | } |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 353 | TargetOptions Options; |
| 354 | LTOModule::getTargetOptions(Options); |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 355 | _target = march->createTargetMachine(TripleStr, _mCpu, FeatureStr, Options, |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 356 | RelocModel, CodeModel::Default, |
| 357 | CodeGenOpt::Aggressive); |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 358 | return true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 361 | void LTOCodeGenerator:: |
| 362 | applyRestriction(GlobalValue &GV, |
| 363 | std::vector<const char*> &mustPreserveList, |
| 364 | SmallPtrSet<GlobalValue*, 8> &asmUsed, |
| 365 | Mangler &mangler) { |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 366 | SmallString<64> Buffer; |
| 367 | mangler.getNameWithPrefix(Buffer, &GV, false); |
| 368 | |
| 369 | if (GV.isDeclaration()) |
| 370 | return; |
| 371 | if (_mustPreserveSymbols.count(Buffer)) |
| 372 | mustPreserveList.push_back(GV.getName().data()); |
| 373 | if (_asmUndefinedRefs.count(Buffer)) |
| 374 | asmUsed.insert(&GV); |
| 375 | } |
| 376 | |
| 377 | static void findUsedValues(GlobalVariable *LLVMUsed, |
| 378 | SmallPtrSet<GlobalValue*, 8> &UsedValues) { |
| 379 | if (LLVMUsed == 0) return; |
| 380 | |
Rafael Espindola | d4ee392 | 2013-04-24 17:54:35 +0000 | [diff] [blame] | 381 | ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer()); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 382 | for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 383 | if (GlobalValue *GV = |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 384 | dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts())) |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 385 | UsedValues.insert(GV); |
| 386 | } |
| 387 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 388 | void LTOCodeGenerator::applyScopeRestrictions() { |
| 389 | if (_scopeRestrictionsDone) return; |
| 390 | Module *mergedModule = _linker.getModule(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 392 | // Start off with a verification pass. |
| 393 | PassManager passes; |
| 394 | passes.add(createVerifierPass()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 395 | |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 396 | // mark which symbols can not be internalized |
Bill Wendling | 99cb622 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 397 | MCContext Context(_target->getMCAsmInfo(), _target->getRegisterInfo(), NULL); |
Bill Wendling | cc5a882 | 2013-05-29 20:37:19 +0000 | [diff] [blame] | 398 | Mangler mangler(Context, _target); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 399 | std::vector<const char*> mustPreserveList; |
| 400 | SmallPtrSet<GlobalValue*, 8> asmUsed; |
| 401 | |
| 402 | for (Module::iterator f = mergedModule->begin(), |
| 403 | e = mergedModule->end(); f != e; ++f) |
| 404 | applyRestriction(*f, mustPreserveList, asmUsed, mangler); |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 405 | for (Module::global_iterator v = mergedModule->global_begin(), |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 406 | e = mergedModule->global_end(); v != e; ++v) |
| 407 | applyRestriction(*v, mustPreserveList, asmUsed, mangler); |
| 408 | for (Module::alias_iterator a = mergedModule->alias_begin(), |
| 409 | e = mergedModule->alias_end(); a != e; ++a) |
| 410 | applyRestriction(*a, mustPreserveList, asmUsed, mangler); |
| 411 | |
| 412 | GlobalVariable *LLVMCompilerUsed = |
| 413 | mergedModule->getGlobalVariable("llvm.compiler.used"); |
| 414 | findUsedValues(LLVMCompilerUsed, asmUsed); |
| 415 | if (LLVMCompilerUsed) |
| 416 | LLVMCompilerUsed->eraseFromParent(); |
| 417 | |
Rafael Espindola | d4ee392 | 2013-04-24 17:54:35 +0000 | [diff] [blame] | 418 | if (!asmUsed.empty()) { |
| 419 | llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context); |
| 420 | std::vector<Constant*> asmUsed2; |
| 421 | for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(), |
| 422 | e = asmUsed.end(); i !=e; ++i) { |
| 423 | GlobalValue *GV = *i; |
| 424 | Constant *c = ConstantExpr::getBitCast(GV, i8PTy); |
| 425 | asmUsed2.push_back(c); |
| 426 | } |
| 427 | |
| 428 | llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size()); |
| 429 | LLVMCompilerUsed = |
| 430 | new llvm::GlobalVariable(*mergedModule, ATy, false, |
| 431 | llvm::GlobalValue::AppendingLinkage, |
| 432 | llvm::ConstantArray::get(ATy, asmUsed2), |
| 433 | "llvm.compiler.used"); |
| 434 | |
| 435 | LLVMCompilerUsed->setSection("llvm.metadata"); |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 436 | } |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 437 | |
Bill Wendling | 50f3183 | 2012-12-10 21:33:45 +0000 | [diff] [blame] | 438 | passes.add(createInternalizePass(mustPreserveList)); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 439 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 440 | // apply scope restrictions |
| 441 | passes.run(*mergedModule); |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 443 | _scopeRestrictionsDone = true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame^] | 446 | void LTOCodeGenerator::performIPO(bool ToPartition, std::string &errMsg) { |
| 447 | // Mark which symbols can not be internalized |
| 448 | applyScopeRestrictions(); |
| 449 | |
| 450 | // Instantiate the pass manager to organize the passes. |
| 451 | PassManager Passes; |
| 452 | |
| 453 | // Start off with a verification pass. |
| 454 | Passes.add(createVerifierPass()); |
| 455 | |
| 456 | // Add an appropriate DataLayout instance for this module... |
| 457 | Passes.add(new DataLayout(*_target->getDataLayout())); |
| 458 | _target->addAnalysisPasses(Passes); |
| 459 | |
| 460 | // Enabling internalize here would use its AllButMain variant. It |
| 461 | // keeps only main if it exists and does nothing for libraries. Instead |
| 462 | // we create the pass ourselves with the symbol list provided by the linker. |
| 463 | if (!DisableOpt) |
| 464 | PassManagerBuilder().populateLTOPassManager(Passes, |
| 465 | /*Internalize=*/false, |
| 466 | !DisableInline, |
| 467 | DisableGVNLoadPRE); |
| 468 | // Make sure everything is still good. |
| 469 | Passes.add(createVerifierPass()); |
| 470 | |
| 471 | Module* M = _linker.getModule(); |
| 472 | if (ToPartition) |
| 473 | assert(false && "TBD"); |
| 474 | else { |
| 475 | Passes.run(*M); |
| 476 | |
| 477 | // Create a partition for the merged module. |
| 478 | PartitionMgr.createIPOPart(M); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | // Perform Post-IPO compilation. If the partition is enabled, there may |
| 483 | // be multiple partitions, and therefore there may be multiple objects. |
| 484 | // In this case, "MergeObjs" indicates to merge all object together (via ld -r) |
| 485 | // and return the path to the merged object via "MergObjPath". |
| 486 | // |
| 487 | bool LTOCodeGenerator::performPostIPO(std::string &ErrMsg, |
| 488 | bool MergeObjs, |
| 489 | const char **MergObjPath) { |
| 490 | // Determine the variant of post-ipo driver |
| 491 | PostIPODriver::VariantTy DrvTy; |
| 492 | if (!EnablePartition) { |
| 493 | assert(!MergeObjs && !MergObjPath && "Invalid parameter"); |
| 494 | DrvTy = PostIPODriver::PIDV_SERIAL; |
| 495 | } else { |
| 496 | DrvTy = PostIPODriver::PIDV_Invalid; |
| 497 | assert(false && "TBD"); |
| 498 | } |
| 499 | |
| 500 | PostIPODriver D(DrvTy, _target, PartitionMgr, FileMgr, MergeObjs); |
| 501 | if (D.Compile(ErrMsg)) { |
| 502 | if (MergeObjs) |
| 503 | *MergObjPath = D.getSingleObjFile()->getPath().c_str(); |
| 504 | return true; |
| 505 | } |
| 506 | |
| 507 | return false; |
| 508 | } |
| 509 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 510 | /// Optimize merged modules using various IPO passes |
Chris Lattner | 817a01f | 2011-05-22 00:20:07 +0000 | [diff] [blame] | 511 | bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, |
| 512 | std::string &errMsg) { |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 513 | if (!this->determineTarget(errMsg)) |
| 514 | return false; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 515 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 516 | Module* mergedModule = _linker.getModule(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 517 | |
Bill Wendling | 8c18a6f | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 518 | // Mark which symbols can not be internalized |
Bill Wendling | 64d5b28 | 2012-04-09 22:18:01 +0000 | [diff] [blame] | 519 | this->applyScopeRestrictions(); |
| 520 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 521 | // Instantiate the pass manager to organize the passes. |
| 522 | PassManager passes; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 523 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 524 | // Start off with a verification pass. |
| 525 | passes.add(createVerifierPass()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 526 | |
Micah Villmow | 791cfc2 | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 527 | // Add an appropriate DataLayout instance for this module... |
| 528 | passes.add(new DataLayout(*_target->getDataLayout())); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 529 | _target->addAnalysisPasses(passes); |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 530 | |
Rafael Espindola | 4d2e9d9 | 2012-04-16 10:58:38 +0000 | [diff] [blame] | 531 | // Enabling internalize here would use its AllButMain variant. It |
| 532 | // keeps only main if it exists and does nothing for libraries. Instead |
| 533 | // we create the pass ourselves with the symbol list provided by the linker. |
Bill Wendling | 8c18a6f | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 534 | if (!DisableOpt) |
Bill Wendling | 6303b66 | 2013-02-28 14:11:10 +0000 | [diff] [blame] | 535 | PassManagerBuilder().populateLTOPassManager(passes, |
Bill Wendling | 50f3183 | 2012-12-10 21:33:45 +0000 | [diff] [blame] | 536 | /*Internalize=*/false, |
Bill Wendling | 3197b44 | 2012-04-02 22:16:50 +0000 | [diff] [blame] | 537 | !DisableInline, |
| 538 | DisableGVNLoadPRE); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 539 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 540 | // Make sure everything is still good. |
| 541 | passes.add(createVerifierPass()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 542 | |
Lang Hames | a991b25 | 2013-03-13 21:18:46 +0000 | [diff] [blame] | 543 | PassManager codeGenPasses; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 544 | |
Lang Hames | a991b25 | 2013-03-13 21:18:46 +0000 | [diff] [blame] | 545 | codeGenPasses.add(new DataLayout(*_target->getDataLayout())); |
| 546 | _target->addAnalysisPasses(codeGenPasses); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 547 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 548 | formatted_raw_ostream Out(out); |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 549 | |
Bob Wilson | d696544 | 2013-03-29 23:28:55 +0000 | [diff] [blame] | 550 | // If the bitcode files contain ARC code and were compiled with optimization, |
| 551 | // the ObjCARCContractPass must be run, so do it unconditionally here. |
| 552 | codeGenPasses.add(createObjCARCContractPass()); |
| 553 | |
Lang Hames | a991b25 | 2013-03-13 21:18:46 +0000 | [diff] [blame] | 554 | if (_target->addPassesToEmitFile(codeGenPasses, Out, |
David Blaikie | 4f56a30 | 2012-05-30 18:42:51 +0000 | [diff] [blame] | 555 | TargetMachine::CGFT_ObjectFile)) { |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 556 | errMsg = "target file type not supported"; |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 557 | return false; |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 558 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 559 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 560 | // Run our queue of passes all at once now, efficiently. |
| 561 | passes.run(*mergedModule); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 562 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 563 | // Run the code generator, and write assembly file |
Lang Hames | a991b25 | 2013-03-13 21:18:46 +0000 | [diff] [blame] | 564 | codeGenPasses.run(*mergedModule); |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 565 | |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 566 | return true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 569 | /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging |
| 570 | /// LTO problems. |
| 571 | void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) { |
| 572 | for (std::pair<StringRef, StringRef> o = getToken(options); |
| 573 | !o.first.empty(); o = getToken(o.second)) { |
| 574 | // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add |
| 575 | // that. |
Bill Wendling | 0ca36af | 2012-08-08 22:03:50 +0000 | [diff] [blame] | 576 | if (_codegenOptions.empty()) |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 577 | _codegenOptions.push_back(strdup("libLTO")); |
| 578 | _codegenOptions.push_back(strdup(o.first.str().c_str())); |
| 579 | } |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 580 | } |