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 | |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 15 | #include "llvm/LTO/LTOCodeGenerator.h" |
| 16 | #include "llvm/LTO/LTOModule.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Nick Lewycky | 8189d40 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/Passes.h" |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/Verifier.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/ReaderWriter.h" |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 21 | #include "llvm/Config/config.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Constants.h" |
| 23 | #include "llvm/IR/DataLayout.h" |
| 24 | #include "llvm/IR/DerivedTypes.h" |
| 25 | #include "llvm/IR/LLVMContext.h" |
| 26 | #include "llvm/IR/Module.h" |
Shuxin Yang | 8945f75 | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 27 | #include "llvm/InitializePasses.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 28 | #include "llvm/Linker.h" |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCAsmInfo.h" |
| 30 | #include "llvm/MC/MCContext.h" |
Evan Cheng | ab8be96 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 31 | #include "llvm/MC/SubtargetFeature.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 32 | #include "llvm/PassManager.h" |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Rafael Espindola | 68c0efa | 2013-06-17 18:05:35 +0000 | [diff] [blame] | 34 | #include "llvm/Support/FileSystem.h" |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 35 | #include "llvm/Support/FormattedStream.h" |
Michael J. Spencer | 3cc52ea | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Host.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 3cc52ea | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Signals.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 39 | #include "llvm/Support/TargetRegistry.h" |
| 40 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 41 | #include "llvm/Support/ToolOutputFile.h" |
Michael J. Spencer | f2f516f | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 42 | #include "llvm/Support/system_error.h" |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetOptions.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetRegisterInfo.h" |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 45 | #include "llvm/Target/Mangler.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 46 | #include "llvm/Transforms/IPO.h" |
| 47 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Bob Wilson | d696544 | 2013-03-29 23:28:55 +0000 | [diff] [blame] | 48 | #include "llvm/Transforms/ObjCARC.h" |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame] | 49 | using namespace llvm; |
Shuxin Yang | 67d135a | 2013-08-12 18:29:43 +0000 | [diff] [blame] | 50 | |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 51 | const char* LTOCodeGenerator::getVersionString() { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 52 | #ifdef LLVM_VERSION_INFO |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 53 | return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 54 | #else |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 55 | return PACKAGE_NAME " version " PACKAGE_VERSION; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 56 | #endif |
| 57 | } |
| 58 | |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 59 | LTOCodeGenerator::LTOCodeGenerator() |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 60 | : Context(getGlobalContext()), Linker(new Module("ld-temp.o", Context)), |
| 61 | TargetMach(NULL), EmitDwarfDebugInfo(false), ScopeRestrictionsDone(false), |
| 62 | CodeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), NativeObjectFile(NULL) { |
Shuxin Yang | 8945f75 | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 63 | initializeLTOPasses(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 66 | LTOCodeGenerator::~LTOCodeGenerator() { |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 67 | delete TargetMach; |
| 68 | delete NativeObjectFile; |
| 69 | delete Linker.getModule(); |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 70 | |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 71 | for (std::vector<char *>::iterator I = CodegenOptions.begin(), |
| 72 | E = CodegenOptions.end(); |
| 73 | I != E; ++I) |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 74 | free(*I); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Shuxin Yang | 8945f75 | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 77 | // Initialize LTO passes. Please keep this funciton in sync with |
Shuxin Yang | c679d6b | 2013-07-23 06:44:34 +0000 | [diff] [blame] | 78 | // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO |
Shuxin Yang | 8945f75 | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 79 | // passes are initialized. |
| 80 | // |
| 81 | void LTOCodeGenerator::initializeLTOPasses() { |
| 82 | PassRegistry &R = *PassRegistry::getPassRegistry(); |
| 83 | |
| 84 | initializeInternalizePassPass(R); |
| 85 | initializeIPSCCPPass(R); |
| 86 | initializeGlobalOptPass(R); |
| 87 | initializeConstantMergePass(R); |
| 88 | initializeDAHPass(R); |
| 89 | initializeInstCombinerPass(R); |
| 90 | initializeSimpleInlinerPass(R); |
| 91 | initializePruneEHPass(R); |
| 92 | initializeGlobalDCEPass(R); |
| 93 | initializeArgPromotionPass(R); |
| 94 | initializeJumpThreadingPass(R); |
| 95 | initializeSROAPass(R); |
| 96 | initializeSROA_DTPass(R); |
| 97 | initializeSROA_SSAUpPass(R); |
| 98 | initializeFunctionAttrsPass(R); |
| 99 | initializeGlobalsModRefPass(R); |
| 100 | initializeLICMPass(R); |
| 101 | initializeGVNPass(R); |
| 102 | initializeMemCpyOptPass(R); |
| 103 | initializeDCEPass(R); |
Tom Stellard | 01d7203 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 104 | initializeCFGSimplifyPassPass(R); |
Shuxin Yang | 8945f75 | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 107 | bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) { |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 108 | bool ret = Linker.linkInModule(mod->getLLVVMModule(), &errMsg); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 109 | |
| 110 | const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs(); |
| 111 | for (int i = 0, e = undefs.size(); i != e; ++i) |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 112 | AsmUndefinedRefs[undefs[i]] = 1; |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 113 | |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 114 | return !ret; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 115 | } |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 116 | |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 117 | void LTOCodeGenerator::setTargetOptions(TargetOptions options) { |
| 118 | Options.LessPreciseFPMADOption = options.LessPreciseFPMADOption; |
| 119 | Options.NoFramePointerElim = options.NoFramePointerElim; |
| 120 | Options.AllowFPOpFusion = options.AllowFPOpFusion; |
| 121 | Options.UnsafeFPMath = options.UnsafeFPMath; |
| 122 | Options.NoInfsFPMath = options.NoInfsFPMath; |
| 123 | Options.NoNaNsFPMath = options.NoNaNsFPMath; |
| 124 | Options.HonorSignDependentRoundingFPMathOption = |
| 125 | options.HonorSignDependentRoundingFPMathOption; |
| 126 | Options.UseSoftFloat = options.UseSoftFloat; |
| 127 | Options.FloatABIType = options.FloatABIType; |
| 128 | Options.NoZerosInBSS = options.NoZerosInBSS; |
| 129 | Options.GuaranteedTailCallOpt = options.GuaranteedTailCallOpt; |
| 130 | Options.DisableTailCalls = options.DisableTailCalls; |
| 131 | Options.StackAlignmentOverride = options.StackAlignmentOverride; |
| 132 | Options.TrapFuncName = options.TrapFuncName; |
| 133 | Options.PositionIndependentExecutable = options.PositionIndependentExecutable; |
| 134 | Options.EnableSegmentedStacks = options.EnableSegmentedStacks; |
| 135 | Options.UseInitArray = options.UseInitArray; |
| 136 | } |
| 137 | |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 138 | void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) { |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 139 | switch (debug) { |
| 140 | case LTO_DEBUG_MODEL_NONE: |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 141 | EmitDwarfDebugInfo = false; |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 142 | return; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 143 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 144 | case LTO_DEBUG_MODEL_DWARF: |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 145 | EmitDwarfDebugInfo = true; |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 146 | return; |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 147 | } |
| 148 | llvm_unreachable("Unknown debug format!"); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 151 | void LTOCodeGenerator::setCodePICModel(lto_codegen_model model) { |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 152 | switch (model) { |
| 153 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 154 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 155 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 156 | CodeModel = model; |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 157 | return; |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 158 | } |
| 159 | llvm_unreachable("Unknown PIC model!"); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 162 | bool LTOCodeGenerator::writeMergedModules(const char *path, |
| 163 | std::string &errMsg) { |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 164 | if (!determineTarget(errMsg)) |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 165 | return false; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 166 | |
Bill Wendling | 6ba2ed5 | 2013-08-08 23:51:04 +0000 | [diff] [blame] | 167 | // mark which symbols can not be internalized |
| 168 | applyScopeRestrictions(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 169 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 170 | // create output file |
| 171 | std::string ErrInfo; |
Rafael Espindola | c1b49b5 | 2013-07-16 19:44:17 +0000 | [diff] [blame] | 172 | tool_output_file Out(path, ErrInfo, sys::fs::F_Binary); |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 173 | if (!ErrInfo.empty()) { |
| 174 | errMsg = "could not open bitcode file for writing: "; |
| 175 | errMsg += path; |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 176 | return false; |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 177 | } |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 178 | |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 179 | // write bitcode to it |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 180 | WriteBitcodeToFile(Linker.getModule(), Out.os()); |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 181 | Out.os().close(); |
Dan Gohman | 4b7416b | 2010-05-27 20:19:47 +0000 | [diff] [blame] | 182 | |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 183 | if (Out.os().has_error()) { |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 184 | errMsg = "could not write bitcode file: "; |
| 185 | errMsg += path; |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 186 | Out.os().clear_error(); |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 187 | return false; |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 188 | } |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 189 | |
Dan Gohman | f291401 | 2010-08-20 16:59:15 +0000 | [diff] [blame] | 190 | Out.keep(); |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 191 | return true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 194 | bool LTOCodeGenerator::compile_to_file(const char** name, |
| 195 | bool disableOpt, |
| 196 | bool disableInline, |
| 197 | bool disableGVNLoadPRE, |
| 198 | std::string& errMsg) { |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 199 | // make unique temp .o file to put generated object file |
| 200 | SmallString<128> Filename; |
| 201 | int FD; |
| 202 | error_code EC = sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename); |
| 203 | if (EC) { |
| 204 | errMsg = EC.message(); |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 205 | return false; |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 206 | } |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 207 | |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 208 | // generate object file |
| 209 | tool_output_file objFile(Filename.c_str(), FD); |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 210 | |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 211 | bool genResult = generateObjectFile(objFile.os(), disableOpt, disableInline, |
| 212 | disableGVNLoadPRE, errMsg); |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 213 | objFile.os().close(); |
| 214 | if (objFile.os().has_error()) { |
| 215 | objFile.os().clear_error(); |
| 216 | sys::fs::remove(Twine(Filename)); |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 217 | return false; |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 218 | } |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 219 | |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 220 | objFile.keep(); |
| 221 | if (!genResult) { |
| 222 | sys::fs::remove(Twine(Filename)); |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 223 | return false; |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 224 | } |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 225 | |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 226 | NativeObjectPath = Filename.c_str(); |
| 227 | *name = NativeObjectPath.c_str(); |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 228 | return true; |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 231 | const void* LTOCodeGenerator::compile(size_t* length, |
| 232 | bool disableOpt, |
| 233 | bool disableInline, |
| 234 | bool disableGVNLoadPRE, |
| 235 | std::string& errMsg) { |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 236 | const char *name; |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 237 | if (!compile_to_file(&name, disableOpt, disableInline, disableGVNLoadPRE, |
| 238 | errMsg)) |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 239 | return NULL; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 240 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 241 | // remove old buffer if compile() called twice |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 242 | delete NativeObjectFile; |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 243 | |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 244 | // read .o file into memory buffer |
| 245 | OwningPtr<MemoryBuffer> BuffPtr; |
| 246 | if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) { |
| 247 | errMsg = ec.message(); |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 248 | sys::fs::remove(NativeObjectPath); |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 249 | return NULL; |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 250 | } |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 251 | NativeObjectFile = BuffPtr.take(); |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 252 | |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 253 | // remove temp files |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 254 | sys::fs::remove(NativeObjectPath); |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 255 | |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 256 | // return buffer, unless error |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 257 | if (NativeObjectFile == NULL) |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 258 | return NULL; |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 259 | *length = NativeObjectFile->getBufferSize(); |
| 260 | return NativeObjectFile->getBufferStart(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Bill Wendling | 8c18a6f | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 263 | bool LTOCodeGenerator::determineTarget(std::string &errMsg) { |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 264 | if (TargetMach != NULL) |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 265 | return true; |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 266 | |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 267 | std::string TripleStr = Linker.getModule()->getTargetTriple(); |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 268 | if (TripleStr.empty()) |
| 269 | TripleStr = sys::getDefaultTargetTriple(); |
| 270 | llvm::Triple Triple(TripleStr); |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 271 | |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 272 | // create target machine from info for merged modules |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 273 | const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg); |
Bill Wendling | 0ca36af | 2012-08-08 22:03:50 +0000 | [diff] [blame] | 274 | if (march == NULL) |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 275 | return false; |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 276 | |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 277 | // The relocation model is actually a static member of TargetMachine and |
| 278 | // needs to be set before the TargetMachine is instantiated. |
| 279 | Reloc::Model RelocModel = Reloc::Default; |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 280 | switch (CodeModel) { |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 281 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 282 | RelocModel = Reloc::Static; |
| 283 | break; |
| 284 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 285 | RelocModel = Reloc::PIC_; |
| 286 | break; |
| 287 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 288 | RelocModel = Reloc::DynamicNoPIC; |
| 289 | break; |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 290 | } |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 291 | |
| 292 | // construct LTOModule, hand over ownership of module and target |
| 293 | SubtargetFeatures Features; |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 294 | Features.getDefaultSubtargetFeatures(Triple); |
Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 295 | std::string FeatureStr = Features.getString(); |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 296 | // Set a default CPU for Darwin triples. |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 297 | if (MCpu.empty() && Triple.isOSDarwin()) { |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 298 | if (Triple.getArch() == llvm::Triple::x86_64) |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 299 | MCpu = "core2"; |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 300 | else if (Triple.getArch() == llvm::Triple::x86) |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 301 | MCpu = "yonah"; |
Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 302 | } |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 303 | |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 304 | TargetMach = march->createTargetMachine(TripleStr, MCpu, FeatureStr, Options, |
| 305 | RelocModel, CodeModel::Default, |
| 306 | CodeGenOpt::Aggressive); |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 307 | return true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 310 | void LTOCodeGenerator:: |
| 311 | applyRestriction(GlobalValue &GV, |
Rafael Espindola | 775079c | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 312 | std::vector<const char*> &MustPreserveList, |
| 313 | SmallPtrSet<GlobalValue*, 8> &AsmUsed, |
| 314 | Mangler &Mangler) { |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 315 | SmallString<64> Buffer; |
Rafael Espindola | 775079c | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 316 | Mangler.getNameWithPrefix(Buffer, &GV, false); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 317 | |
| 318 | if (GV.isDeclaration()) |
| 319 | return; |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 320 | if (MustPreserveSymbols.count(Buffer)) |
Rafael Espindola | 775079c | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 321 | MustPreserveList.push_back(GV.getName().data()); |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 322 | if (AsmUndefinedRefs.count(Buffer)) |
Rafael Espindola | 775079c | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 323 | AsmUsed.insert(&GV); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static void findUsedValues(GlobalVariable *LLVMUsed, |
| 327 | SmallPtrSet<GlobalValue*, 8> &UsedValues) { |
| 328 | if (LLVMUsed == 0) return; |
| 329 | |
Rafael Espindola | d4ee392 | 2013-04-24 17:54:35 +0000 | [diff] [blame] | 330 | ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer()); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 331 | for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 332 | if (GlobalValue *GV = |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 333 | dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts())) |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 334 | UsedValues.insert(GV); |
| 335 | } |
| 336 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 337 | void LTOCodeGenerator::applyScopeRestrictions() { |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 338 | if (ScopeRestrictionsDone) |
| 339 | return; |
| 340 | Module *mergedModule = Linker.getModule(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 342 | // Start off with a verification pass. |
| 343 | PassManager passes; |
| 344 | passes.add(createVerifierPass()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 345 | |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 346 | // mark which symbols can not be internalized |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 347 | MCContext MContext(TargetMach->getMCAsmInfo(), TargetMach->getRegisterInfo(), |
| 348 | NULL); |
Rafael Espindola | 775079c | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 349 | Mangler Mangler(MContext, TargetMach); |
| 350 | std::vector<const char*> MustPreserveList; |
| 351 | SmallPtrSet<GlobalValue*, 8> AsmUsed; |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 352 | |
| 353 | for (Module::iterator f = mergedModule->begin(), |
| 354 | e = mergedModule->end(); f != e; ++f) |
Rafael Espindola | 775079c | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 355 | applyRestriction(*f, MustPreserveList, AsmUsed, Mangler); |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 356 | for (Module::global_iterator v = mergedModule->global_begin(), |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 357 | e = mergedModule->global_end(); v != e; ++v) |
Rafael Espindola | 775079c | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 358 | applyRestriction(*v, MustPreserveList, AsmUsed, Mangler); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 359 | for (Module::alias_iterator a = mergedModule->alias_begin(), |
| 360 | e = mergedModule->alias_end(); a != e; ++a) |
Rafael Espindola | 775079c | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 361 | applyRestriction(*a, MustPreserveList, AsmUsed, Mangler); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 362 | |
| 363 | GlobalVariable *LLVMCompilerUsed = |
| 364 | mergedModule->getGlobalVariable("llvm.compiler.used"); |
Rafael Espindola | 775079c | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 365 | findUsedValues(LLVMCompilerUsed, AsmUsed); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 366 | if (LLVMCompilerUsed) |
| 367 | LLVMCompilerUsed->eraseFromParent(); |
| 368 | |
Rafael Espindola | 775079c | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 369 | if (!AsmUsed.empty()) { |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 370 | llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context); |
Rafael Espindola | d4ee392 | 2013-04-24 17:54:35 +0000 | [diff] [blame] | 371 | std::vector<Constant*> asmUsed2; |
Rafael Espindola | 775079c | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 372 | for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = AsmUsed.begin(), |
| 373 | e = AsmUsed.end(); i !=e; ++i) { |
Rafael Espindola | d4ee392 | 2013-04-24 17:54:35 +0000 | [diff] [blame] | 374 | GlobalValue *GV = *i; |
| 375 | Constant *c = ConstantExpr::getBitCast(GV, i8PTy); |
| 376 | asmUsed2.push_back(c); |
| 377 | } |
| 378 | |
| 379 | llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size()); |
| 380 | LLVMCompilerUsed = |
| 381 | new llvm::GlobalVariable(*mergedModule, ATy, false, |
| 382 | llvm::GlobalValue::AppendingLinkage, |
| 383 | llvm::ConstantArray::get(ATy, asmUsed2), |
| 384 | "llvm.compiler.used"); |
| 385 | |
| 386 | LLVMCompilerUsed->setSection("llvm.metadata"); |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 387 | } |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 388 | |
Rafael Espindola | 775079c | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 389 | passes.add(createInternalizePass(MustPreserveList)); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 390 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 391 | // apply scope restrictions |
| 392 | passes.run(*mergedModule); |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 393 | |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 394 | ScopeRestrictionsDone = true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 397 | /// Optimize merged modules using various IPO passes |
Chris Lattner | 817a01f | 2011-05-22 00:20:07 +0000 | [diff] [blame] | 398 | bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 399 | bool DisableOpt, |
| 400 | bool DisableInline, |
| 401 | bool DisableGVNLoadPRE, |
Chris Lattner | 817a01f | 2011-05-22 00:20:07 +0000 | [diff] [blame] | 402 | std::string &errMsg) { |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 403 | if (!this->determineTarget(errMsg)) |
| 404 | return false; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 405 | |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 406 | Module *mergedModule = Linker.getModule(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 407 | |
Bill Wendling | 8c18a6f | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 408 | // Mark which symbols can not be internalized |
Bill Wendling | 64d5b28 | 2012-04-09 22:18:01 +0000 | [diff] [blame] | 409 | this->applyScopeRestrictions(); |
| 410 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 411 | // Instantiate the pass manager to organize the passes. |
| 412 | PassManager passes; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 413 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 414 | // Start off with a verification pass. |
| 415 | passes.add(createVerifierPass()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 416 | |
Micah Villmow | 791cfc2 | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 417 | // Add an appropriate DataLayout instance for this module... |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 418 | passes.add(new DataLayout(*TargetMach->getDataLayout())); |
| 419 | TargetMach->addAnalysisPasses(passes); |
Bill Wendling | 76b13ed | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 420 | |
Rafael Espindola | 4d2e9d9 | 2012-04-16 10:58:38 +0000 | [diff] [blame] | 421 | // Enabling internalize here would use its AllButMain variant. It |
| 422 | // keeps only main if it exists and does nothing for libraries. Instead |
| 423 | // 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] | 424 | if (!DisableOpt) |
Bill Wendling | 6303b66 | 2013-02-28 14:11:10 +0000 | [diff] [blame] | 425 | PassManagerBuilder().populateLTOPassManager(passes, |
Bill Wendling | 50f3183 | 2012-12-10 21:33:45 +0000 | [diff] [blame] | 426 | /*Internalize=*/false, |
Bill Wendling | 3197b44 | 2012-04-02 22:16:50 +0000 | [diff] [blame] | 427 | !DisableInline, |
| 428 | DisableGVNLoadPRE); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 429 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 430 | // Make sure everything is still good. |
| 431 | passes.add(createVerifierPass()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 432 | |
Lang Hames | a991b25 | 2013-03-13 21:18:46 +0000 | [diff] [blame] | 433 | PassManager codeGenPasses; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 434 | |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 435 | codeGenPasses.add(new DataLayout(*TargetMach->getDataLayout())); |
| 436 | TargetMach->addAnalysisPasses(codeGenPasses); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 437 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 438 | formatted_raw_ostream Out(out); |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 439 | |
Bob Wilson | d696544 | 2013-03-29 23:28:55 +0000 | [diff] [blame] | 440 | // If the bitcode files contain ARC code and were compiled with optimization, |
| 441 | // the ObjCARCContractPass must be run, so do it unconditionally here. |
| 442 | codeGenPasses.add(createObjCARCContractPass()); |
| 443 | |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 444 | if (TargetMach->addPassesToEmitFile(codeGenPasses, Out, |
| 445 | TargetMachine::CGFT_ObjectFile)) { |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 446 | errMsg = "target file type not supported"; |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 447 | return false; |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 448 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 449 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 450 | // Run our queue of passes all at once now, efficiently. |
| 451 | passes.run(*mergedModule); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 452 | |
Bill Wendling | c94c562 | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 453 | // Run the code generator, and write assembly file |
Lang Hames | a991b25 | 2013-03-13 21:18:46 +0000 | [diff] [blame] | 454 | codeGenPasses.run(*mergedModule); |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 455 | |
Shuxin Yang | 0880939 | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 456 | return true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 459 | /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging |
| 460 | /// LTO problems. |
| 461 | void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) { |
| 462 | for (std::pair<StringRef, StringRef> o = getToken(options); |
| 463 | !o.first.empty(); o = getToken(o.second)) { |
| 464 | // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add |
| 465 | // that. |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 466 | if (CodegenOptions.empty()) |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 467 | CodegenOptions.push_back(strdup("libLLVMLTO")); |
Rafael Espindola | 66efc63 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 468 | CodegenOptions.push_back(strdup(o.first.str().c_str())); |
Bill Wendling | caf71d4 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 469 | } |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 470 | } |
Rafael Espindola | 0e95b3a | 2013-10-02 14:36:23 +0000 | [diff] [blame^] | 471 | |
| 472 | void LTOCodeGenerator::parseCodeGenDebugOptions() { |
| 473 | // if options were requested, set them |
| 474 | if (!CodegenOptions.empty()) |
| 475 | cl::ParseCommandLineOptions(CodegenOptions.size(), |
| 476 | const_cast<char **>(&CodegenOptions[0])); |
| 477 | } |