Nick Kledzik | 07b4a62 | 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 | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 7 | // |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 10 | // This file implements the Link Time Optimization library. This library is |
Nick Kledzik | 07b4a62 | 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 | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 15 | #include "llvm/LTO/LTOCodeGenerator.h" |
| 16 | #include "llvm/LTO/LTOModule.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Nick Lewycky | 510dae3 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/Passes.h" |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/Verifier.h" |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/ReaderWriter.h" |
Justin Bogner | b10a520 | 2013-11-12 21:44:01 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/RuntimeLibcalls.h" |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 22 | #include "llvm/Config/config.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Constants.h" |
| 24 | #include "llvm/IR/DataLayout.h" |
| 25 | #include "llvm/IR/DerivedTypes.h" |
| 26 | #include "llvm/IR/LLVMContext.h" |
| 27 | #include "llvm/IR/Module.h" |
Shuxin Yang | 1e6d80e | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 28 | #include "llvm/InitializePasses.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 29 | #include "llvm/Linker.h" |
Chris Lattner | 2eff505 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCAsmInfo.h" |
| 31 | #include "llvm/MC/MCContext.h" |
Evan Cheng | 8264e27 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 32 | #include "llvm/MC/SubtargetFeature.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 33 | #include "llvm/PassManager.h" |
Nick Kledzik | c232347 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Rafael Espindola | 40c908b | 2013-06-17 18:05:35 +0000 | [diff] [blame] | 35 | #include "llvm/Support/FileSystem.h" |
David Greene | a31f96c | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 36 | #include "llvm/Support/FormattedStream.h" |
Michael J. Spencer | ab425d8 | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Host.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | ab425d8 | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Signals.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 40 | #include "llvm/Support/TargetRegistry.h" |
| 41 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 42 | #include "llvm/Support/ToolOutputFile.h" |
Michael J. Spencer | d422723 | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 43 | #include "llvm/Support/system_error.h" |
Justin Bogner | b10a520 | 2013-11-12 21:44:01 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetLibraryInfo.h" |
| 45 | #include "llvm/Target/TargetLowering.h" |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 46 | #include "llvm/Target/TargetOptions.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 47 | #include "llvm/Target/TargetRegisterInfo.h" |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 48 | #include "llvm/Target/Mangler.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 49 | #include "llvm/Transforms/IPO.h" |
| 50 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Bob Wilson | f36f15f | 2013-03-29 23:28:55 +0000 | [diff] [blame] | 51 | #include "llvm/Transforms/ObjCARC.h" |
Shuxin Yang | 76d082b | 2013-08-12 18:29:43 +0000 | [diff] [blame] | 52 | using namespace llvm; |
Shuxin Yang | 76d082b | 2013-08-12 18:29:43 +0000 | [diff] [blame] | 53 | |
Bill Wendling | 534a658 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 54 | const char* LTOCodeGenerator::getVersionString() { |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 55 | #ifdef LLVM_VERSION_INFO |
Bill Wendling | 534a658 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 56 | return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 57 | #else |
Bill Wendling | 534a658 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 58 | return PACKAGE_NAME " version " PACKAGE_VERSION; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 59 | #endif |
| 60 | } |
| 61 | |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 62 | LTOCodeGenerator::LTOCodeGenerator() |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 63 | : Context(getGlobalContext()), Linker(new Module("ld-temp.o", Context)), |
| 64 | TargetMach(NULL), EmitDwarfDebugInfo(false), ScopeRestrictionsDone(false), |
| 65 | CodeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), NativeObjectFile(NULL) { |
Shuxin Yang | 1e6d80e | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 66 | initializeLTOPasses(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Bill Wendling | 534a658 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 69 | LTOCodeGenerator::~LTOCodeGenerator() { |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 70 | delete TargetMach; |
| 71 | delete NativeObjectFile; |
Bill Wendling | 91e6f6e | 2013-10-16 08:59:57 +0000 | [diff] [blame] | 72 | TargetMach = NULL; |
| 73 | NativeObjectFile = NULL; |
| 74 | |
| 75 | Linker.deleteModule(); |
Bill Wendling | 534a658 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 76 | |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 77 | for (std::vector<char *>::iterator I = CodegenOptions.begin(), |
| 78 | E = CodegenOptions.end(); |
| 79 | I != E; ++I) |
Bill Wendling | 534a658 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 80 | free(*I); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Shuxin Yang | 1e6d80e | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 83 | // Initialize LTO passes. Please keep this funciton in sync with |
Shuxin Yang | ca76085 | 2013-07-23 06:44:34 +0000 | [diff] [blame] | 84 | // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO |
Shuxin Yang | 1e6d80e | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 85 | // passes are initialized. |
| 86 | // |
| 87 | void LTOCodeGenerator::initializeLTOPasses() { |
| 88 | PassRegistry &R = *PassRegistry::getPassRegistry(); |
| 89 | |
| 90 | initializeInternalizePassPass(R); |
| 91 | initializeIPSCCPPass(R); |
| 92 | initializeGlobalOptPass(R); |
| 93 | initializeConstantMergePass(R); |
| 94 | initializeDAHPass(R); |
| 95 | initializeInstCombinerPass(R); |
| 96 | initializeSimpleInlinerPass(R); |
| 97 | initializePruneEHPass(R); |
| 98 | initializeGlobalDCEPass(R); |
| 99 | initializeArgPromotionPass(R); |
| 100 | initializeJumpThreadingPass(R); |
| 101 | initializeSROAPass(R); |
| 102 | initializeSROA_DTPass(R); |
| 103 | initializeSROA_SSAUpPass(R); |
| 104 | initializeFunctionAttrsPass(R); |
| 105 | initializeGlobalsModRefPass(R); |
| 106 | initializeLICMPass(R); |
| 107 | initializeGVNPass(R); |
| 108 | initializeMemCpyOptPass(R); |
| 109 | initializeDCEPass(R); |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 110 | initializeCFGSimplifyPassPass(R); |
Shuxin Yang | 1e6d80e | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 113 | bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) { |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 114 | bool ret = Linker.linkInModule(mod->getLLVVMModule(), &errMsg); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 115 | |
| 116 | const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs(); |
| 117 | for (int i = 0, e = undefs.size(); i != e; ++i) |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 118 | AsmUndefinedRefs[undefs[i]] = 1; |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 119 | |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 120 | return !ret; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 121 | } |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 122 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 123 | void LTOCodeGenerator::setTargetOptions(TargetOptions options) { |
| 124 | Options.LessPreciseFPMADOption = options.LessPreciseFPMADOption; |
| 125 | Options.NoFramePointerElim = options.NoFramePointerElim; |
| 126 | Options.AllowFPOpFusion = options.AllowFPOpFusion; |
| 127 | Options.UnsafeFPMath = options.UnsafeFPMath; |
| 128 | Options.NoInfsFPMath = options.NoInfsFPMath; |
| 129 | Options.NoNaNsFPMath = options.NoNaNsFPMath; |
| 130 | Options.HonorSignDependentRoundingFPMathOption = |
| 131 | options.HonorSignDependentRoundingFPMathOption; |
| 132 | Options.UseSoftFloat = options.UseSoftFloat; |
| 133 | Options.FloatABIType = options.FloatABIType; |
| 134 | Options.NoZerosInBSS = options.NoZerosInBSS; |
| 135 | Options.GuaranteedTailCallOpt = options.GuaranteedTailCallOpt; |
| 136 | Options.DisableTailCalls = options.DisableTailCalls; |
| 137 | Options.StackAlignmentOverride = options.StackAlignmentOverride; |
| 138 | Options.TrapFuncName = options.TrapFuncName; |
| 139 | Options.PositionIndependentExecutable = options.PositionIndependentExecutable; |
| 140 | Options.EnableSegmentedStacks = options.EnableSegmentedStacks; |
| 141 | Options.UseInitArray = options.UseInitArray; |
| 142 | } |
| 143 | |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 144 | void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) { |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 145 | switch (debug) { |
| 146 | case LTO_DEBUG_MODEL_NONE: |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 147 | EmitDwarfDebugInfo = false; |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 148 | return; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 149 | |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 150 | case LTO_DEBUG_MODEL_DWARF: |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 151 | EmitDwarfDebugInfo = true; |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 152 | return; |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 153 | } |
| 154 | llvm_unreachable("Unknown debug format!"); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 157 | void LTOCodeGenerator::setCodePICModel(lto_codegen_model model) { |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 158 | switch (model) { |
| 159 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 160 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 161 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 162 | CodeModel = model; |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 163 | return; |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 164 | } |
| 165 | llvm_unreachable("Unknown PIC model!"); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Chris Lattner | 6973395 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 168 | bool LTOCodeGenerator::writeMergedModules(const char *path, |
| 169 | std::string &errMsg) { |
Shuxin Yang | 95866fa | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 170 | if (!determineTarget(errMsg)) |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 171 | return false; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 172 | |
Bill Wendling | 2bbbfef | 2013-08-08 23:51:04 +0000 | [diff] [blame] | 173 | // mark which symbols can not be internalized |
| 174 | applyScopeRestrictions(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 6973395 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 176 | // create output file |
| 177 | std::string ErrInfo; |
Rafael Espindola | 6d35481 | 2013-07-16 19:44:17 +0000 | [diff] [blame] | 178 | tool_output_file Out(path, ErrInfo, sys::fs::F_Binary); |
Chris Lattner | 6973395 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 179 | if (!ErrInfo.empty()) { |
| 180 | errMsg = "could not open bitcode file for writing: "; |
| 181 | errMsg += path; |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 182 | return false; |
Chris Lattner | 6973395 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 183 | } |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 184 | |
Chris Lattner | 6973395 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 185 | // write bitcode to it |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 186 | WriteBitcodeToFile(Linker.getModule(), Out.os()); |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 187 | Out.os().close(); |
Dan Gohman | ab366f0 | 2010-05-27 20:19:47 +0000 | [diff] [blame] | 188 | |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 189 | if (Out.os().has_error()) { |
Chris Lattner | 6973395 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 190 | errMsg = "could not write bitcode file: "; |
| 191 | errMsg += path; |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 192 | Out.os().clear_error(); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 193 | return false; |
Chris Lattner | 6973395 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 194 | } |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 195 | |
Dan Gohman | 8525fe7 | 2010-08-20 16:59:15 +0000 | [diff] [blame] | 196 | Out.keep(); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 197 | return true; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 200 | bool LTOCodeGenerator::compile_to_file(const char** name, |
| 201 | bool disableOpt, |
| 202 | bool disableInline, |
| 203 | bool disableGVNLoadPRE, |
| 204 | std::string& errMsg) { |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 205 | // make unique temp .o file to put generated object file |
| 206 | SmallString<128> Filename; |
| 207 | int FD; |
| 208 | error_code EC = sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename); |
| 209 | if (EC) { |
| 210 | errMsg = EC.message(); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 211 | return false; |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 212 | } |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 213 | |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 214 | // generate object file |
| 215 | tool_output_file objFile(Filename.c_str(), FD); |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 216 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 217 | bool genResult = generateObjectFile(objFile.os(), disableOpt, disableInline, |
| 218 | disableGVNLoadPRE, errMsg); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 219 | objFile.os().close(); |
| 220 | if (objFile.os().has_error()) { |
| 221 | objFile.os().clear_error(); |
| 222 | sys::fs::remove(Twine(Filename)); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 223 | return false; |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 224 | } |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 225 | |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 226 | objFile.keep(); |
| 227 | if (!genResult) { |
| 228 | sys::fs::remove(Twine(Filename)); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 229 | return false; |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 230 | } |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 231 | |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 232 | NativeObjectPath = Filename.c_str(); |
| 233 | *name = NativeObjectPath.c_str(); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 234 | return true; |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 237 | const void* LTOCodeGenerator::compile(size_t* length, |
| 238 | bool disableOpt, |
| 239 | bool disableInline, |
| 240 | bool disableGVNLoadPRE, |
| 241 | std::string& errMsg) { |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 242 | const char *name; |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 243 | if (!compile_to_file(&name, disableOpt, disableInline, disableGVNLoadPRE, |
| 244 | errMsg)) |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 245 | return NULL; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 246 | |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 247 | // remove old buffer if compile() called twice |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 248 | delete NativeObjectFile; |
Nick Kledzik | 91a6dcf | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 249 | |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 250 | // read .o file into memory buffer |
| 251 | OwningPtr<MemoryBuffer> BuffPtr; |
| 252 | if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) { |
| 253 | errMsg = ec.message(); |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 254 | sys::fs::remove(NativeObjectPath); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 255 | return NULL; |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 256 | } |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 257 | NativeObjectFile = BuffPtr.take(); |
Rafael Espindola | fac373c | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 258 | |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 259 | // remove temp files |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 260 | sys::fs::remove(NativeObjectPath); |
Rafael Espindola | fac373c | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 261 | |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 262 | // return buffer, unless error |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 263 | if (NativeObjectFile == NULL) |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 264 | return NULL; |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 265 | *length = NativeObjectFile->getBufferSize(); |
| 266 | return NativeObjectFile->getBufferStart(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Bill Wendling | f44b2a2 | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 269 | bool LTOCodeGenerator::determineTarget(std::string &errMsg) { |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 270 | if (TargetMach != NULL) |
Shuxin Yang | 95866fa | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 271 | return true; |
Daniel Dunbar | 0f16ea5 | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 272 | |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 273 | std::string TripleStr = Linker.getModule()->getTargetTriple(); |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 274 | if (TripleStr.empty()) |
| 275 | TripleStr = sys::getDefaultTargetTriple(); |
| 276 | llvm::Triple Triple(TripleStr); |
Bill Wendling | 9b2c573 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 277 | |
Bill Wendling | 45f74e3 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 278 | // create target machine from info for merged modules |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 279 | const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg); |
Bill Wendling | 6a0abed | 2012-08-08 22:03:50 +0000 | [diff] [blame] | 280 | if (march == NULL) |
Shuxin Yang | 95866fa | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 281 | return false; |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 282 | |
Bill Wendling | 45f74e3 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 283 | // The relocation model is actually a static member of TargetMachine and |
| 284 | // needs to be set before the TargetMachine is instantiated. |
| 285 | Reloc::Model RelocModel = Reloc::Default; |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 286 | switch (CodeModel) { |
Bill Wendling | 45f74e3 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 287 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 288 | RelocModel = Reloc::Static; |
| 289 | break; |
| 290 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 291 | RelocModel = Reloc::PIC_; |
| 292 | break; |
| 293 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 294 | RelocModel = Reloc::DynamicNoPIC; |
| 295 | break; |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 296 | } |
Bill Wendling | 45f74e3 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 297 | |
| 298 | // construct LTOModule, hand over ownership of module and target |
| 299 | SubtargetFeatures Features; |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 300 | Features.getDefaultSubtargetFeatures(Triple); |
Bill Wendling | 45f74e3 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 301 | std::string FeatureStr = Features.getString(); |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 302 | // Set a default CPU for Darwin triples. |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 303 | if (MCpu.empty() && Triple.isOSDarwin()) { |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 304 | if (Triple.getArch() == llvm::Triple::x86_64) |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 305 | MCpu = "core2"; |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 306 | else if (Triple.getArch() == llvm::Triple::x86) |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 307 | MCpu = "yonah"; |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 308 | } |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 309 | |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 310 | TargetMach = march->createTargetMachine(TripleStr, MCpu, FeatureStr, Options, |
| 311 | RelocModel, CodeModel::Default, |
| 312 | CodeGenOpt::Aggressive); |
Shuxin Yang | 95866fa | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 313 | return true; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 316 | void LTOCodeGenerator:: |
| 317 | applyRestriction(GlobalValue &GV, |
Justin Bogner | b10a520 | 2013-11-12 21:44:01 +0000 | [diff] [blame] | 318 | const ArrayRef<StringRef> &Libcalls, |
Rafael Espindola | b7c0b4a | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 319 | std::vector<const char*> &MustPreserveList, |
| 320 | SmallPtrSet<GlobalValue*, 8> &AsmUsed, |
| 321 | Mangler &Mangler) { |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 322 | SmallString<64> Buffer; |
Rafael Espindola | 117b20c | 2013-12-05 05:53:12 +0000 | [diff] [blame] | 323 | Mangler.getNameWithPrefix(Buffer, &GV); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 324 | |
| 325 | if (GV.isDeclaration()) |
| 326 | return; |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 327 | if (MustPreserveSymbols.count(Buffer)) |
Rafael Espindola | b7c0b4a | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 328 | MustPreserveList.push_back(GV.getName().data()); |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 329 | if (AsmUndefinedRefs.count(Buffer)) |
Rafael Espindola | b7c0b4a | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 330 | AsmUsed.insert(&GV); |
Justin Bogner | b10a520 | 2013-11-12 21:44:01 +0000 | [diff] [blame] | 331 | |
| 332 | // Conservatively append user-supplied runtime library functions to |
| 333 | // llvm.compiler.used. These could be internalized and deleted by |
| 334 | // optimizations like -globalopt, causing problems when later optimizations |
| 335 | // add new library calls (e.g., llvm.memset => memset and printf => puts). |
| 336 | // Leave it to the linker to remove any dead code (e.g. with -dead_strip). |
| 337 | if (isa<Function>(GV) && |
| 338 | std::binary_search(Libcalls.begin(), Libcalls.end(), GV.getName())) |
| 339 | AsmUsed.insert(&GV); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | static void findUsedValues(GlobalVariable *LLVMUsed, |
| 343 | SmallPtrSet<GlobalValue*, 8> &UsedValues) { |
| 344 | if (LLVMUsed == 0) return; |
| 345 | |
Rafael Espindola | cc111b2 | 2013-04-24 17:54:35 +0000 | [diff] [blame] | 346 | ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer()); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 347 | for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 348 | if (GlobalValue *GV = |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 349 | dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts())) |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 350 | UsedValues.insert(GV); |
| 351 | } |
| 352 | |
Justin Bogner | b10a520 | 2013-11-12 21:44:01 +0000 | [diff] [blame] | 353 | static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls, |
| 354 | const TargetLibraryInfo& TLI, |
| 355 | const TargetLowering *Lowering) |
| 356 | { |
| 357 | // TargetLibraryInfo has info on C runtime library calls on the current |
| 358 | // target. |
| 359 | for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs); |
| 360 | I != E; ++I) { |
| 361 | LibFunc::Func F = static_cast<LibFunc::Func>(I); |
| 362 | if (TLI.has(F)) |
| 363 | Libcalls.push_back(TLI.getName(F)); |
| 364 | } |
| 365 | |
| 366 | // TargetLowering has info on library calls that CodeGen expects to be |
| 367 | // available, both from the C runtime and compiler-rt. |
| 368 | if (Lowering) |
| 369 | for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL); |
| 370 | I != E; ++I) |
| 371 | if (const char *Name |
| 372 | = Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I))) |
| 373 | Libcalls.push_back(Name); |
| 374 | |
Duncan P. N. Exon Smith | 0c8d604 | 2013-11-16 16:15:56 +0000 | [diff] [blame] | 375 | array_pod_sort(Libcalls.begin(), Libcalls.end()); |
Justin Bogner | b10a520 | 2013-11-12 21:44:01 +0000 | [diff] [blame] | 376 | Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()), |
| 377 | Libcalls.end()); |
| 378 | } |
| 379 | |
Chris Lattner | 2eff505 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 380 | void LTOCodeGenerator::applyScopeRestrictions() { |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 381 | if (ScopeRestrictionsDone) |
| 382 | return; |
| 383 | Module *mergedModule = Linker.getModule(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 2eff505 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 385 | // Start off with a verification pass. |
| 386 | PassManager passes; |
| 387 | passes.add(createVerifierPass()); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 388 | |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 389 | // mark which symbols can not be internalized |
Rafael Espindola | e133ed8 | 2013-10-29 17:28:26 +0000 | [diff] [blame] | 390 | Mangler Mangler(TargetMach); |
Rafael Espindola | b7c0b4a | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 391 | std::vector<const char*> MustPreserveList; |
| 392 | SmallPtrSet<GlobalValue*, 8> AsmUsed; |
Justin Bogner | b10a520 | 2013-11-12 21:44:01 +0000 | [diff] [blame] | 393 | std::vector<StringRef> Libcalls; |
| 394 | TargetLibraryInfo TLI(Triple(TargetMach->getTargetTriple())); |
| 395 | accumulateAndSortLibcalls(Libcalls, TLI, TargetMach->getTargetLowering()); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 396 | |
| 397 | for (Module::iterator f = mergedModule->begin(), |
| 398 | e = mergedModule->end(); f != e; ++f) |
Justin Bogner | b10a520 | 2013-11-12 21:44:01 +0000 | [diff] [blame] | 399 | applyRestriction(*f, Libcalls, MustPreserveList, AsmUsed, Mangler); |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 400 | for (Module::global_iterator v = mergedModule->global_begin(), |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 401 | e = mergedModule->global_end(); v != e; ++v) |
Justin Bogner | b10a520 | 2013-11-12 21:44:01 +0000 | [diff] [blame] | 402 | applyRestriction(*v, Libcalls, MustPreserveList, AsmUsed, Mangler); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 403 | for (Module::alias_iterator a = mergedModule->alias_begin(), |
| 404 | e = mergedModule->alias_end(); a != e; ++a) |
Justin Bogner | b10a520 | 2013-11-12 21:44:01 +0000 | [diff] [blame] | 405 | applyRestriction(*a, Libcalls, MustPreserveList, AsmUsed, Mangler); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 406 | |
| 407 | GlobalVariable *LLVMCompilerUsed = |
| 408 | mergedModule->getGlobalVariable("llvm.compiler.used"); |
Rafael Espindola | b7c0b4a | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 409 | findUsedValues(LLVMCompilerUsed, AsmUsed); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 410 | if (LLVMCompilerUsed) |
| 411 | LLVMCompilerUsed->eraseFromParent(); |
| 412 | |
Rafael Espindola | b7c0b4a | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 413 | if (!AsmUsed.empty()) { |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 414 | llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context); |
Rafael Espindola | cc111b2 | 2013-04-24 17:54:35 +0000 | [diff] [blame] | 415 | std::vector<Constant*> asmUsed2; |
Rafael Espindola | b7c0b4a | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 416 | for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = AsmUsed.begin(), |
| 417 | e = AsmUsed.end(); i !=e; ++i) { |
Rafael Espindola | cc111b2 | 2013-04-24 17:54:35 +0000 | [diff] [blame] | 418 | GlobalValue *GV = *i; |
| 419 | Constant *c = ConstantExpr::getBitCast(GV, i8PTy); |
| 420 | asmUsed2.push_back(c); |
| 421 | } |
| 422 | |
| 423 | llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size()); |
| 424 | LLVMCompilerUsed = |
| 425 | new llvm::GlobalVariable(*mergedModule, ATy, false, |
| 426 | llvm::GlobalValue::AppendingLinkage, |
| 427 | llvm::ConstantArray::get(ATy, asmUsed2), |
| 428 | "llvm.compiler.used"); |
| 429 | |
| 430 | LLVMCompilerUsed->setSection("llvm.metadata"); |
Chris Lattner | 2eff505 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 431 | } |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 432 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 433 | passes.add(createInternalizePass(MustPreserveList)); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 2eff505 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 435 | // apply scope restrictions |
| 436 | passes.run(*mergedModule); |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 437 | |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 438 | ScopeRestrictionsDone = true; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 441 | /// Optimize merged modules using various IPO passes |
Chris Lattner | f200a0a | 2011-05-22 00:20:07 +0000 | [diff] [blame] | 442 | bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 443 | bool DisableOpt, |
| 444 | bool DisableInline, |
| 445 | bool DisableGVNLoadPRE, |
Chris Lattner | f200a0a | 2011-05-22 00:20:07 +0000 | [diff] [blame] | 446 | std::string &errMsg) { |
Shuxin Yang | 95866fa | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 447 | if (!this->determineTarget(errMsg)) |
| 448 | return false; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 449 | |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 450 | Module *mergedModule = Linker.getModule(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 451 | |
Bill Wendling | f44b2a2 | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 452 | // Mark which symbols can not be internalized |
Bill Wendling | 383fda2 | 2012-04-09 22:18:01 +0000 | [diff] [blame] | 453 | this->applyScopeRestrictions(); |
| 454 | |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 455 | // Instantiate the pass manager to organize the passes. |
| 456 | PassManager passes; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 457 | |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 458 | // Start off with a verification pass. |
| 459 | passes.add(createVerifierPass()); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 460 | |
Micah Villmow | 9cfc13d | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 461 | // Add an appropriate DataLayout instance for this module... |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 462 | passes.add(new DataLayout(*TargetMach->getDataLayout())); |
Yi Jiang | 53823be | 2013-12-12 01:37:39 +0000 | [diff] [blame^] | 463 | |
| 464 | // Add appropriate TargetLibraryInfo for this module. |
| 465 | passes.add(new TargetLibraryInfo(Triple(TargetMach->getTargetTriple()))); |
| 466 | |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 467 | TargetMach->addAnalysisPasses(passes); |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 468 | |
Rafael Espindola | 0ecb8fa | 2012-04-16 10:58:38 +0000 | [diff] [blame] | 469 | // Enabling internalize here would use its AllButMain variant. It |
| 470 | // keeps only main if it exists and does nothing for libraries. Instead |
| 471 | // we create the pass ourselves with the symbol list provided by the linker. |
Bill Wendling | f44b2a2 | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 472 | if (!DisableOpt) |
Bill Wendling | c7e0a04 | 2013-02-28 14:11:10 +0000 | [diff] [blame] | 473 | PassManagerBuilder().populateLTOPassManager(passes, |
Bill Wendling | 4a8fc8f | 2012-12-10 21:33:45 +0000 | [diff] [blame] | 474 | /*Internalize=*/false, |
Bill Wendling | 932b992 | 2012-04-02 22:16:50 +0000 | [diff] [blame] | 475 | !DisableInline, |
| 476 | DisableGVNLoadPRE); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 477 | |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 478 | // Make sure everything is still good. |
| 479 | passes.add(createVerifierPass()); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 480 | |
Lang Hames | dfa3f8f | 2013-03-13 21:18:46 +0000 | [diff] [blame] | 481 | PassManager codeGenPasses; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 482 | |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 483 | codeGenPasses.add(new DataLayout(*TargetMach->getDataLayout())); |
| 484 | TargetMach->addAnalysisPasses(codeGenPasses); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 485 | |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 486 | formatted_raw_ostream Out(out); |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 487 | |
Bob Wilson | f36f15f | 2013-03-29 23:28:55 +0000 | [diff] [blame] | 488 | // If the bitcode files contain ARC code and were compiled with optimization, |
| 489 | // the ObjCARCContractPass must be run, so do it unconditionally here. |
| 490 | codeGenPasses.add(createObjCARCContractPass()); |
| 491 | |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 492 | if (TargetMach->addPassesToEmitFile(codeGenPasses, Out, |
| 493 | TargetMachine::CGFT_ObjectFile)) { |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 494 | errMsg = "target file type not supported"; |
Shuxin Yang | 95866fa | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 495 | return false; |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 496 | } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 497 | |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 498 | // Run our queue of passes all at once now, efficiently. |
| 499 | passes.run(*mergedModule); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 500 | |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 501 | // Run the code generator, and write assembly file |
Lang Hames | dfa3f8f | 2013-03-13 21:18:46 +0000 | [diff] [blame] | 502 | codeGenPasses.run(*mergedModule); |
Nick Lewycky | fd6a249 | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 503 | |
Shuxin Yang | 95866fa | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 504 | return true; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Bill Wendling | 534a658 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 507 | /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging |
| 508 | /// LTO problems. |
| 509 | void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) { |
| 510 | for (std::pair<StringRef, StringRef> o = getToken(options); |
| 511 | !o.first.empty(); o = getToken(o.second)) { |
| 512 | // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add |
| 513 | // that. |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 514 | if (CodegenOptions.empty()) |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 515 | CodegenOptions.push_back(strdup("libLLVMLTO")); |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 516 | CodegenOptions.push_back(strdup(o.first.str().c_str())); |
Bill Wendling | 534a658 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 517 | } |
Nick Kledzik | c232347 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 518 | } |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 519 | |
| 520 | void LTOCodeGenerator::parseCodeGenDebugOptions() { |
| 521 | // if options were requested, set them |
| 522 | if (!CodegenOptions.empty()) |
| 523 | cl::ParseCommandLineOptions(CodegenOptions.size(), |
| 524 | const_cast<char **>(&CodegenOptions[0])); |
| 525 | } |