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