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