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