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 | 5c73220 | 2016-07-14 21:21:16 +0000 | [diff] [blame] | 15 | #include "llvm/LTO/legacy/LTOCodeGenerator.h" |
Mehdi Amini | f59f2bb1 | 2016-04-12 06:34:10 +0000 | [diff] [blame] | 16 | |
Adam Nemet | 106feda | 2016-02-16 21:41:51 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Nick Lewycky | 510dae3 | 2009-06-17 06:52:10 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 93dcdc4 | 2015-01-31 11:17:59 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/TargetTransformInfo.h" |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/ReaderWriter.h" |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/ParallelCG.h" |
Justin Bogner | b10a520 | 2013-11-12 21:44:01 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/RuntimeLibcalls.h" |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 25 | #include "llvm/Config/config.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Constants.h" |
| 27 | #include "llvm/IR/DataLayout.h" |
Adrian Prantl | fe7a382 | 2016-05-09 19:57:15 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DerivedTypes.h" |
Quentin Colombet | 5fa1f6f | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DiagnosticInfo.h" |
| 31 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 33 | #include "llvm/IR/LegacyPassManager.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Mangler.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Module.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Verifier.h" |
Shuxin Yang | 1e6d80e | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 37 | #include "llvm/InitializePasses.h" |
Peter Collingbourne | 5c73220 | 2016-07-14 21:21:16 +0000 | [diff] [blame] | 38 | #include "llvm/LTO/legacy/LTOModule.h" |
| 39 | #include "llvm/LTO/legacy/UpdateCompilerUsed.h" |
Chandler Carruth | 6cc07df | 2014-03-06 03:42:23 +0000 | [diff] [blame] | 40 | #include "llvm/Linker/Linker.h" |
Chris Lattner | 2eff505 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 41 | #include "llvm/MC/MCAsmInfo.h" |
| 42 | #include "llvm/MC/MCContext.h" |
Evan Cheng | 8264e27 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 43 | #include "llvm/MC/SubtargetFeature.h" |
Nick Kledzik | c232347 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 44 | #include "llvm/Support/CommandLine.h" |
Rafael Espindola | 40c908b | 2013-06-17 18:05:35 +0000 | [diff] [blame] | 45 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | ab425d8 | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Host.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 47 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | ab425d8 | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Signals.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 49 | #include "llvm/Support/TargetRegistry.h" |
| 50 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 51 | #include "llvm/Support/ToolOutputFile.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 52 | #include "llvm/Support/raw_ostream.h" |
Justin Bogner | b10a520 | 2013-11-12 21:44:01 +0000 | [diff] [blame] | 53 | #include "llvm/Target/TargetLowering.h" |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 54 | #include "llvm/Target/TargetOptions.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 55 | #include "llvm/Target/TargetRegisterInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 56 | #include "llvm/Target/TargetSubtargetInfo.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 57 | #include "llvm/Transforms/IPO.h" |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 58 | #include "llvm/Transforms/IPO/Internalize.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 59 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Bob Wilson | f36f15f | 2013-03-29 23:28:55 +0000 | [diff] [blame] | 60 | #include "llvm/Transforms/ObjCARC.h" |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame^] | 61 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 62 | #include <system_error> |
Shuxin Yang | 76d082b | 2013-08-12 18:29:43 +0000 | [diff] [blame] | 63 | using namespace llvm; |
Shuxin Yang | 76d082b | 2013-08-12 18:29:43 +0000 | [diff] [blame] | 64 | |
Bill Wendling | 534a658 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 65 | const char* LTOCodeGenerator::getVersionString() { |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 66 | #ifdef LLVM_VERSION_INFO |
Bill Wendling | 534a658 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 67 | return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 68 | #else |
Bill Wendling | 534a658 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 69 | return PACKAGE_NAME " version " PACKAGE_VERSION; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 70 | #endif |
| 71 | } |
| 72 | |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 73 | namespace llvm { |
| 74 | cl::opt<bool> LTODiscardValueNames( |
Mehdi Amini | 1592cb9 | 2016-03-10 17:06:52 +0000 | [diff] [blame] | 75 | "lto-discard-value-names", |
| 76 | cl::desc("Strip names from Value during LTO (other than GlobalValue)."), |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 77 | #ifdef NDEBUG |
| 78 | cl::init(true), |
| 79 | #else |
| 80 | cl::init(false), |
| 81 | #endif |
| 82 | cl::Hidden); |
Adrian Prantl | fe7a382 | 2016-05-09 19:57:15 +0000 | [diff] [blame] | 83 | |
| 84 | cl::opt<bool> LTOStripInvalidDebugInfo( |
| 85 | "lto-strip-invalid-debug-info", |
| 86 | cl::desc("Strip invalid debug info metadata during LTO instead of aborting."), |
| 87 | #ifdef NDEBUG |
| 88 | cl::init(true), |
| 89 | #else |
| 90 | cl::init(false), |
| 91 | #endif |
| 92 | cl::Hidden); |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Rafael Espindola | 7b8a24e | 2015-12-04 02:42:28 +0000 | [diff] [blame] | 95 | LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context) |
Rafael Espindola | f49a38f | 2015-12-04 22:08:53 +0000 | [diff] [blame] | 96 | : Context(Context), MergedModule(new Module("ld-temp.o", Context)), |
Teresa Johnson | bef5436 | 2015-12-18 19:28:59 +0000 | [diff] [blame] | 97 | TheLinker(new Linker(*MergedModule)) { |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 98 | Context.setDiscardValueNames(LTODiscardValueNames); |
Duncan P. N. Exon Smith | ed8fdb2 | 2016-04-19 04:55:25 +0000 | [diff] [blame] | 99 | Context.enableDebugTypeODRUniquing(); |
Duncan P. N. Exon Smith | 7832e0a | 2015-04-27 23:19:26 +0000 | [diff] [blame] | 100 | initializeLTOPasses(); |
Duncan P. N. Exon Smith | de5e32b | 2014-11-11 23:03:29 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Peter Collingbourne | 9c8909d | 2015-08-24 22:22:53 +0000 | [diff] [blame] | 103 | LTOCodeGenerator::~LTOCodeGenerator() {} |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 104 | |
Yaron Keren | 55f5c3d | 2015-09-01 10:13:49 +0000 | [diff] [blame] | 105 | // Initialize LTO passes. Please keep this function in sync with |
Shuxin Yang | ca76085 | 2013-07-23 06:44:34 +0000 | [diff] [blame] | 106 | // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO |
Duncan P. N. Exon Smith | bccb4fd | 2014-01-10 20:24:35 +0000 | [diff] [blame] | 107 | // passes are initialized. |
Shuxin Yang | 1e6d80e | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 108 | void LTOCodeGenerator::initializeLTOPasses() { |
| 109 | PassRegistry &R = *PassRegistry::getPassRegistry(); |
| 110 | |
Justin Bogner | 4563a06 | 2016-04-26 20:15:52 +0000 | [diff] [blame] | 111 | initializeInternalizeLegacyPassPass(R); |
Davide Italiano | f54f2f0 | 2016-05-05 21:05:36 +0000 | [diff] [blame] | 112 | initializeIPSCCPLegacyPassPass(R); |
Justin Bogner | 1a07501 | 2016-04-26 00:28:01 +0000 | [diff] [blame] | 113 | initializeGlobalOptLegacyPassPass(R); |
Davide Italiano | 164b9bc | 2016-05-05 00:51:09 +0000 | [diff] [blame] | 114 | initializeConstantMergeLegacyPassPass(R); |
Shuxin Yang | 1e6d80e | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 115 | initializeDAHPass(R); |
Chandler Carruth | 1edb9d6 | 2015-01-20 22:44:35 +0000 | [diff] [blame] | 116 | initializeInstructionCombiningPassPass(R); |
Shuxin Yang | 1e6d80e | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 117 | initializeSimpleInlinerPass(R); |
| 118 | initializePruneEHPass(R); |
Davide Italiano | 66228c4 | 2016-05-03 19:39:15 +0000 | [diff] [blame] | 119 | initializeGlobalDCELegacyPassPass(R); |
Shuxin Yang | 1e6d80e | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 120 | initializeArgPromotionPass(R); |
| 121 | initializeJumpThreadingPass(R); |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 122 | initializeSROALegacyPassPass(R); |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 123 | initializePostOrderFunctionAttrsLegacyPassPass(R); |
Sean Silva | f508019 | 2016-06-12 07:48:51 +0000 | [diff] [blame] | 124 | initializeReversePostOrderFunctionAttrsLegacyPassPass(R); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 125 | initializeGlobalsAAWrapperPassPass(R); |
Dehao Chen | 9cba1f4 | 2016-07-12 22:37:48 +0000 | [diff] [blame] | 126 | initializeLegacyLICMPassPass(R); |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 127 | initializeMergedLoadStoreMotionLegacyPassPass(R); |
Chandler Carruth | 89c45a1 | 2016-03-11 08:50:55 +0000 | [diff] [blame] | 128 | initializeGVNLegacyPassPass(R); |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 129 | initializeMemCpyOptLegacyPassPass(R); |
Justin Bogner | 395c212 | 2016-04-22 19:40:41 +0000 | [diff] [blame] | 130 | initializeDCELegacyPassPass(R); |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 131 | initializeCFGSimplifyPassPass(R); |
Shuxin Yang | 1e6d80e | 2013-07-22 18:40:34 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Davide Italiano | fc4430e | 2016-08-23 12:32:57 +0000 | [diff] [blame] | 134 | void LTOCodeGenerator::setAsmUndefinedRefs(LTOModule *Mod) { |
Mehdi Amini | dc5a507 | 2016-10-07 19:05:14 +0000 | [diff] [blame] | 135 | const std::vector<StringRef> &undefs = Mod->getAsmUndefinedRefs(); |
Davide Italiano | fc4430e | 2016-08-23 12:32:57 +0000 | [diff] [blame] | 136 | for (int i = 0, e = undefs.size(); i != e; ++i) |
| 137 | AsmUndefinedRefs[undefs[i]] = 1; |
| 138 | } |
| 139 | |
Duncan P. N. Exon Smith | f496775 | 2015-08-31 23:44:06 +0000 | [diff] [blame] | 140 | bool LTOCodeGenerator::addModule(LTOModule *Mod) { |
| 141 | assert(&Mod->getModule().getContext() == &Context && |
Duncan P. N. Exon Smith | 9419863 | 2014-11-11 23:13:10 +0000 | [diff] [blame] | 142 | "Expected module in same context"); |
| 143 | |
Teresa Johnson | bef5436 | 2015-12-18 19:28:59 +0000 | [diff] [blame] | 144 | bool ret = TheLinker->linkInModule(Mod->takeModule()); |
Davide Italiano | fc4430e | 2016-08-23 12:32:57 +0000 | [diff] [blame] | 145 | setAsmUndefinedRefs(Mod); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 146 | |
Duncan P. N. Exon Smith | b011ad7 | 2016-04-20 17:48:22 +0000 | [diff] [blame] | 147 | // We've just changed the input, so let's make sure we verify it. |
| 148 | HasVerifiedInput = false; |
| 149 | |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 150 | return !ret; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 151 | } |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 152 | |
Peter Collingbourne | 9c8909d | 2015-08-24 22:22:53 +0000 | [diff] [blame] | 153 | void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> Mod) { |
Manman Ren | 6487ce9 | 2015-02-24 00:45:56 +0000 | [diff] [blame] | 154 | assert(&Mod->getModule().getContext() == &Context && |
| 155 | "Expected module in same context"); |
| 156 | |
Manman Ren | 6487ce9 | 2015-02-24 00:45:56 +0000 | [diff] [blame] | 157 | AsmUndefinedRefs.clear(); |
| 158 | |
Peter Collingbourne | 9c8909d | 2015-08-24 22:22:53 +0000 | [diff] [blame] | 159 | MergedModule = Mod->takeModule(); |
Teresa Johnson | bef5436 | 2015-12-18 19:28:59 +0000 | [diff] [blame] | 160 | TheLinker = make_unique<Linker>(*MergedModule); |
Davide Italiano | fc4430e | 2016-08-23 12:32:57 +0000 | [diff] [blame] | 161 | setAsmUndefinedRefs(&*Mod); |
Duncan P. N. Exon Smith | b011ad7 | 2016-04-20 17:48:22 +0000 | [diff] [blame] | 162 | |
| 163 | // We've just changed the input, so let's make sure we verify it. |
| 164 | HasVerifiedInput = false; |
Manman Ren | 6487ce9 | 2015-02-24 00:45:56 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 167 | void LTOCodeGenerator::setTargetOptions(const TargetOptions &Options) { |
Duncan P. N. Exon Smith | f496775 | 2015-08-31 23:44:06 +0000 | [diff] [blame] | 168 | this->Options = Options; |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Duncan P. N. Exon Smith | f496775 | 2015-08-31 23:44:06 +0000 | [diff] [blame] | 171 | void LTOCodeGenerator::setDebugInfo(lto_debug_model Debug) { |
| 172 | switch (Debug) { |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 173 | case LTO_DEBUG_MODEL_NONE: |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 174 | EmitDwarfDebugInfo = false; |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 175 | return; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 176 | |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 177 | case LTO_DEBUG_MODEL_DWARF: |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 178 | EmitDwarfDebugInfo = true; |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 179 | return; |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 180 | } |
| 181 | llvm_unreachable("Unknown debug format!"); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Duncan P. N. Exon Smith | f496775 | 2015-08-31 23:44:06 +0000 | [diff] [blame] | 184 | void LTOCodeGenerator::setOptLevel(unsigned Level) { |
| 185 | OptLevel = Level; |
Peter Collingbourne | c7b675f | 2015-08-22 02:25:53 +0000 | [diff] [blame] | 186 | switch (OptLevel) { |
| 187 | case 0: |
| 188 | CGOptLevel = CodeGenOpt::None; |
Davide Italiano | f8014f8 | 2016-08-26 15:22:59 +0000 | [diff] [blame] | 189 | return; |
Peter Collingbourne | c7b675f | 2015-08-22 02:25:53 +0000 | [diff] [blame] | 190 | case 1: |
| 191 | CGOptLevel = CodeGenOpt::Less; |
Davide Italiano | f8014f8 | 2016-08-26 15:22:59 +0000 | [diff] [blame] | 192 | return; |
Peter Collingbourne | c7b675f | 2015-08-22 02:25:53 +0000 | [diff] [blame] | 193 | case 2: |
| 194 | CGOptLevel = CodeGenOpt::Default; |
Davide Italiano | f8014f8 | 2016-08-26 15:22:59 +0000 | [diff] [blame] | 195 | return; |
Peter Collingbourne | c7b675f | 2015-08-22 02:25:53 +0000 | [diff] [blame] | 196 | case 3: |
| 197 | CGOptLevel = CodeGenOpt::Aggressive; |
Davide Italiano | f8014f8 | 2016-08-26 15:22:59 +0000 | [diff] [blame] | 198 | return; |
Peter Collingbourne | c7b675f | 2015-08-22 02:25:53 +0000 | [diff] [blame] | 199 | } |
Davide Italiano | f8014f8 | 2016-08-26 15:22:59 +0000 | [diff] [blame] | 200 | llvm_unreachable("Unknown optimization level!"); |
Peter Collingbourne | c7b675f | 2015-08-22 02:25:53 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Mehdi Amini | 4cc259a | 2016-10-01 01:18:23 +0000 | [diff] [blame] | 203 | bool LTOCodeGenerator::writeMergedModules(StringRef Path) { |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 204 | if (!determineTarget()) |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 205 | return false; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 206 | |
Duncan P. N. Exon Smith | b011ad7 | 2016-04-20 17:48:22 +0000 | [diff] [blame] | 207 | // We always run the verifier once on the merged module. |
| 208 | verifyMergedModuleOnce(); |
| 209 | |
Bill Wendling | 2bbbfef | 2013-08-08 23:51:04 +0000 | [diff] [blame] | 210 | // mark which symbols can not be internalized |
| 211 | applyScopeRestrictions(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 6973395 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 213 | // create output file |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 214 | std::error_code EC; |
Duncan P. N. Exon Smith | f496775 | 2015-08-31 23:44:06 +0000 | [diff] [blame] | 215 | tool_output_file Out(Path, EC, sys::fs::F_None); |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 216 | if (EC) { |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 217 | std::string ErrMsg = "could not open bitcode file for writing: "; |
Duncan P. N. Exon Smith | f496775 | 2015-08-31 23:44:06 +0000 | [diff] [blame] | 218 | ErrMsg += Path; |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 219 | emitError(ErrMsg); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 220 | return false; |
Chris Lattner | 6973395 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 221 | } |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 6973395 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 223 | // write bitcode to it |
Mehdi Amini | c7b9501 | 2016-04-27 05:11:44 +0000 | [diff] [blame] | 224 | WriteBitcodeToFile(MergedModule.get(), Out.os(), ShouldEmbedUselists); |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 225 | Out.os().close(); |
Dan Gohman | ab366f0 | 2010-05-27 20:19:47 +0000 | [diff] [blame] | 226 | |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 227 | if (Out.os().has_error()) { |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 228 | std::string ErrMsg = "could not write bitcode file: "; |
Duncan P. N. Exon Smith | f496775 | 2015-08-31 23:44:06 +0000 | [diff] [blame] | 229 | ErrMsg += Path; |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 230 | emitError(ErrMsg); |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 231 | Out.os().clear_error(); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 232 | return false; |
Chris Lattner | 6973395 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 233 | } |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 234 | |
Dan Gohman | 8525fe7 | 2010-08-20 16:59:15 +0000 | [diff] [blame] | 235 | Out.keep(); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 236 | return true; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 239 | bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) { |
Tobias Edler von Koch | 4d45090 | 2015-11-19 23:59:24 +0000 | [diff] [blame] | 240 | // make unique temp output file to put generated code |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 241 | SmallString<128> Filename; |
| 242 | int FD; |
Tobias Edler von Koch | 4d45090 | 2015-11-19 23:59:24 +0000 | [diff] [blame] | 243 | |
Mehdi Amini | 4cc259a | 2016-10-01 01:18:23 +0000 | [diff] [blame] | 244 | StringRef Extension |
Tobias Edler von Koch | 4d45090 | 2015-11-19 23:59:24 +0000 | [diff] [blame] | 245 | (FileType == TargetMachine::CGFT_AssemblyFile ? "s" : "o"); |
| 246 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 247 | std::error_code EC = |
Tobias Edler von Koch | 4d45090 | 2015-11-19 23:59:24 +0000 | [diff] [blame] | 248 | sys::fs::createTemporaryFile("lto-llvm", Extension, FD, Filename); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 249 | if (EC) { |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 250 | emitError(EC.message()); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 251 | return false; |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 252 | } |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 253 | |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 254 | // generate object file |
Mehdi Amini | 4cc259a | 2016-10-01 01:18:23 +0000 | [diff] [blame] | 255 | tool_output_file objFile(Filename, FD); |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 256 | |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 257 | bool genResult = compileOptimized(&objFile.os()); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 258 | objFile.os().close(); |
| 259 | if (objFile.os().has_error()) { |
| 260 | objFile.os().clear_error(); |
| 261 | sys::fs::remove(Twine(Filename)); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 262 | return false; |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 263 | } |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 264 | |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 265 | objFile.keep(); |
| 266 | if (!genResult) { |
| 267 | sys::fs::remove(Twine(Filename)); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 268 | return false; |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 269 | } |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 270 | |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 271 | NativeObjectPath = Filename.c_str(); |
Duncan P. N. Exon Smith | f496775 | 2015-08-31 23:44:06 +0000 | [diff] [blame] | 272 | *Name = NativeObjectPath.c_str(); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 273 | return true; |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 276 | std::unique_ptr<MemoryBuffer> |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 277 | LTOCodeGenerator::compileOptimized() { |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 278 | const char *name; |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 279 | if (!compileOptimizedToFile(&name)) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 280 | return nullptr; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 281 | |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 282 | // read .o file into memory buffer |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 283 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 284 | MemoryBuffer::getFile(name, -1, false); |
| 285 | if (std::error_code EC = BufferOrErr.getError()) { |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 286 | emitError(EC.message()); |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 287 | sys::fs::remove(NativeObjectPath); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 288 | return nullptr; |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 289 | } |
Rafael Espindola | fac373c | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 290 | |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 291 | // remove temp files |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 292 | sys::fs::remove(NativeObjectPath); |
Rafael Espindola | fac373c | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 293 | |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 294 | return std::move(*BufferOrErr); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 297 | bool LTOCodeGenerator::compile_to_file(const char **Name, bool DisableVerify, |
| 298 | bool DisableInline, |
Duncan P. N. Exon Smith | f496775 | 2015-08-31 23:44:06 +0000 | [diff] [blame] | 299 | bool DisableGVNLoadPRE, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 300 | bool DisableVectorization) { |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 301 | if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 302 | DisableVectorization)) |
Manman Ren | 8121e1d | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 303 | return false; |
| 304 | |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 305 | return compileOptimizedToFile(Name); |
Manman Ren | 8121e1d | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 308 | std::unique_ptr<MemoryBuffer> |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 309 | LTOCodeGenerator::compile(bool DisableVerify, bool DisableInline, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 310 | bool DisableGVNLoadPRE, bool DisableVectorization) { |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 311 | if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 312 | DisableVectorization)) |
Manman Ren | 8121e1d | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 313 | return nullptr; |
| 314 | |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 315 | return compileOptimized(); |
Manman Ren | 8121e1d | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 318 | bool LTOCodeGenerator::determineTarget() { |
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 | 3c1c987 | 2016-04-17 18:42:27 +0000 | [diff] [blame] | 322 | TripleStr = MergedModule->getTargetTriple(); |
Peter Collingbourne | c7b675f | 2015-08-22 02:25:53 +0000 | [diff] [blame] | 323 | if (TripleStr.empty()) { |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 324 | TripleStr = sys::getDefaultTargetTriple(); |
Peter Collingbourne | 9c8909d | 2015-08-24 22:22:53 +0000 | [diff] [blame] | 325 | MergedModule->setTargetTriple(TripleStr); |
Peter Collingbourne | c7b675f | 2015-08-22 02:25:53 +0000 | [diff] [blame] | 326 | } |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 327 | llvm::Triple Triple(TripleStr); |
Bill Wendling | 9b2c573 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 328 | |
Bill Wendling | 45f74e3 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 329 | // create target machine from info for merged modules |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 330 | std::string ErrMsg; |
Rafael Espindola | 3c1c987 | 2016-04-17 18:42:27 +0000 | [diff] [blame] | 331 | MArch = TargetRegistry::lookupTarget(TripleStr, ErrMsg); |
| 332 | if (!MArch) { |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 333 | emitError(ErrMsg); |
Shuxin Yang | 95866fa | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 334 | return false; |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 335 | } |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 336 | |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 337 | // Construct LTOModule, hand over ownership of module and target. Use MAttr as |
| 338 | // the default set of features. |
| 339 | SubtargetFeatures Features(MAttr); |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 340 | Features.getDefaultSubtargetFeatures(Triple); |
Peter Collingbourne | c7b675f | 2015-08-22 02:25:53 +0000 | [diff] [blame] | 341 | FeatureStr = Features.getString(); |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 342 | // Set a default CPU for Darwin triples. |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 343 | if (MCpu.empty() && Triple.isOSDarwin()) { |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 344 | if (Triple.getArch() == llvm::Triple::x86_64) |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 345 | MCpu = "core2"; |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 346 | else if (Triple.getArch() == llvm::Triple::x86) |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 347 | MCpu = "yonah"; |
Tim Northover | e19bed7 | 2014-07-23 12:32:47 +0000 | [diff] [blame] | 348 | else if (Triple.getArch() == llvm::Triple::aarch64) |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 349 | MCpu = "cyclone"; |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 350 | } |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 351 | |
Rafael Espindola | 3c1c987 | 2016-04-17 18:42:27 +0000 | [diff] [blame] | 352 | TargetMach = createTargetMachine(); |
Shuxin Yang | 95866fa | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 353 | return true; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Rafael Espindola | 3c1c987 | 2016-04-17 18:42:27 +0000 | [diff] [blame] | 356 | std::unique_ptr<TargetMachine> LTOCodeGenerator::createTargetMachine() { |
| 357 | return std::unique_ptr<TargetMachine>( |
| 358 | MArch->createTargetMachine(TripleStr, MCpu, FeatureStr, Options, |
| 359 | RelocModel, CodeModel::Default, CGOptLevel)); |
| 360 | } |
| 361 | |
Mehdi Amini | 022b5bc | 2016-05-05 05:14:24 +0000 | [diff] [blame] | 362 | // If a linkonce global is present in the MustPreserveSymbols, we need to make |
| 363 | // sure we honor this. To force the compiler to not drop it, we add it to the |
| 364 | // "llvm.compiler.used" global. |
Mehdi Amini | 8eb3397 | 2016-05-05 20:05:33 +0000 | [diff] [blame] | 365 | void LTOCodeGenerator::preserveDiscardableGVs( |
Mehdi Amini | 022b5bc | 2016-05-05 05:14:24 +0000 | [diff] [blame] | 366 | Module &TheModule, |
| 367 | llvm::function_ref<bool(const GlobalValue &)> mustPreserveGV) { |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame^] | 368 | std::vector<GlobalValue *> Used; |
Mehdi Amini | 022b5bc | 2016-05-05 05:14:24 +0000 | [diff] [blame] | 369 | auto mayPreserveGlobal = [&](GlobalValue &GV) { |
Davide Italiano | 41bfd6b | 2016-09-02 16:37:31 +0000 | [diff] [blame] | 370 | if (!GV.isDiscardableIfUnused() || GV.isDeclaration() || |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame^] | 371 | !mustPreserveGV(GV)) |
Mehdi Amini | 022b5bc | 2016-05-05 05:14:24 +0000 | [diff] [blame] | 372 | return; |
Davide Italiano | 41bfd6b | 2016-09-02 16:37:31 +0000 | [diff] [blame] | 373 | if (GV.hasAvailableExternallyLinkage()) |
| 374 | return emitWarning( |
Mehdi Amini | 8eb3397 | 2016-05-05 20:05:33 +0000 | [diff] [blame] | 375 | (Twine("Linker asked to preserve available_externally global: '") + |
| 376 | GV.getName() + "'").str()); |
Davide Italiano | 41bfd6b | 2016-09-02 16:37:31 +0000 | [diff] [blame] | 377 | if (GV.hasInternalLinkage()) |
| 378 | return emitWarning((Twine("Linker asked to preserve internal global: '") + |
Mehdi Amini | 8eb3397 | 2016-05-05 20:05:33 +0000 | [diff] [blame] | 379 | GV.getName() + "'").str()); |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame^] | 380 | Used.push_back(&GV); |
Mehdi Amini | 022b5bc | 2016-05-05 05:14:24 +0000 | [diff] [blame] | 381 | }; |
| 382 | for (auto &GV : TheModule) |
| 383 | mayPreserveGlobal(GV); |
| 384 | for (auto &GV : TheModule.globals()) |
| 385 | mayPreserveGlobal(GV); |
| 386 | for (auto &GV : TheModule.aliases()) |
| 387 | mayPreserveGlobal(GV); |
| 388 | |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame^] | 389 | if (Used.empty()) |
Mehdi Amini | 022b5bc | 2016-05-05 05:14:24 +0000 | [diff] [blame] | 390 | return; |
| 391 | |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame^] | 392 | appendToCompilerUsed(TheModule, Used); |
Mehdi Amini | 022b5bc | 2016-05-05 05:14:24 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Chris Lattner | 2eff505 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 395 | void LTOCodeGenerator::applyScopeRestrictions() { |
Mehdi Amini | 022b5bc | 2016-05-05 05:14:24 +0000 | [diff] [blame] | 396 | if (ScopeRestrictionsDone) |
| 397 | return; |
| 398 | |
| 399 | // Declare a callback for the internalize pass that will ask for every |
| 400 | // candidate GlobalValue if it can be internalized or not. |
Peter Collingbourne | 46aafc1 | 2016-10-12 20:12:19 +0000 | [diff] [blame] | 401 | Mangler Mang; |
Mehdi Amini | 022b5bc | 2016-05-05 05:14:24 +0000 | [diff] [blame] | 402 | SmallString<64> MangledName; |
| 403 | auto mustPreserveGV = [&](const GlobalValue &GV) -> bool { |
| 404 | // Unnamed globals can't be mangled, but they can't be preserved either. |
| 405 | if (!GV.hasName()) |
| 406 | return false; |
| 407 | |
| 408 | // Need to mangle the GV as the "MustPreserveSymbols" StringSet is filled |
| 409 | // with the linker supplied name, which on Darwin includes a leading |
| 410 | // underscore. |
| 411 | MangledName.clear(); |
| 412 | MangledName.reserve(GV.getName().size() + 1); |
Peter Collingbourne | 46aafc1 | 2016-10-12 20:12:19 +0000 | [diff] [blame] | 413 | Mang.getNameWithPrefix(MangledName, &GV, /*CannotUsePrivateLabel=*/false); |
Mehdi Amini | 022b5bc | 2016-05-05 05:14:24 +0000 | [diff] [blame] | 414 | return MustPreserveSymbols.count(MangledName); |
| 415 | }; |
| 416 | |
| 417 | // Preserve linkonce value on linker request |
| 418 | preserveDiscardableGVs(*MergedModule, mustPreserveGV); |
| 419 | |
| 420 | if (!ShouldInternalize) |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 421 | return; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 422 | |
Mehdi Amini | deee003 | 2016-04-13 05:36:06 +0000 | [diff] [blame] | 423 | if (ShouldRestoreGlobalsLinkage) { |
| 424 | // Record the linkage type of non-local symbols so they can be restored |
Mehdi Amini | 752ffe9 | 2016-05-05 05:14:20 +0000 | [diff] [blame] | 425 | // prior |
| 426 | // to module splitting. |
Mehdi Amini | deee003 | 2016-04-13 05:36:06 +0000 | [diff] [blame] | 427 | auto RecordLinkage = [&](const GlobalValue &GV) { |
| 428 | if (!GV.hasAvailableExternallyLinkage() && !GV.hasLocalLinkage() && |
| 429 | GV.hasName()) |
| 430 | ExternalSymbols.insert(std::make_pair(GV.getName(), GV.getLinkage())); |
| 431 | }; |
| 432 | for (auto &GV : *MergedModule) |
| 433 | RecordLinkage(GV); |
| 434 | for (auto &GV : MergedModule->globals()) |
| 435 | RecordLinkage(GV); |
| 436 | for (auto &GV : MergedModule->aliases()) |
| 437 | RecordLinkage(GV); |
| 438 | } |
| 439 | |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 440 | // Update the llvm.compiler_used globals to force preserving libcalls and |
| 441 | // symbols referenced from asm |
Davide Italiano | 53d457c | 2016-06-22 19:50:42 +0000 | [diff] [blame] | 442 | updateCompilerUsed(*MergedModule, *TargetMach, AsmUndefinedRefs); |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 443 | |
Mehdi Amini | 022b5bc | 2016-05-05 05:14:24 +0000 | [diff] [blame] | 444 | internalizeModule(*MergedModule, mustPreserveGV); |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 445 | |
Rafael Espindola | c80c969 | 2013-09-04 17:44:24 +0000 | [diff] [blame] | 446 | ScopeRestrictionsDone = true; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Tobias Edler von Koch | 8ecaf69 | 2016-01-18 23:24:54 +0000 | [diff] [blame] | 449 | /// Restore original linkage for symbols that may have been internalized |
| 450 | void LTOCodeGenerator::restoreLinkageForExternals() { |
| 451 | if (!ShouldInternalize || !ShouldRestoreGlobalsLinkage) |
| 452 | return; |
| 453 | |
| 454 | assert(ScopeRestrictionsDone && |
| 455 | "Cannot externalize without internalization!"); |
| 456 | |
| 457 | if (ExternalSymbols.empty()) |
| 458 | return; |
| 459 | |
| 460 | auto externalize = [this](GlobalValue &GV) { |
| 461 | if (!GV.hasLocalLinkage() || !GV.hasName()) |
| 462 | return; |
| 463 | |
| 464 | auto I = ExternalSymbols.find(GV.getName()); |
| 465 | if (I == ExternalSymbols.end()) |
| 466 | return; |
| 467 | |
| 468 | GV.setLinkage(I->second); |
| 469 | }; |
| 470 | |
| 471 | std::for_each(MergedModule->begin(), MergedModule->end(), externalize); |
| 472 | std::for_each(MergedModule->global_begin(), MergedModule->global_end(), |
| 473 | externalize); |
| 474 | std::for_each(MergedModule->alias_begin(), MergedModule->alias_end(), |
| 475 | externalize); |
| 476 | } |
| 477 | |
Duncan P. N. Exon Smith | b011ad7 | 2016-04-20 17:48:22 +0000 | [diff] [blame] | 478 | void LTOCodeGenerator::verifyMergedModuleOnce() { |
| 479 | // Only run on the first call. |
| 480 | if (HasVerifiedInput) |
| 481 | return; |
| 482 | HasVerifiedInput = true; |
| 483 | |
Adrian Prantl | fe7a382 | 2016-05-09 19:57:15 +0000 | [diff] [blame] | 484 | if (LTOStripInvalidDebugInfo) { |
| 485 | bool BrokenDebugInfo = false; |
| 486 | if (verifyModule(*MergedModule, &dbgs(), &BrokenDebugInfo)) |
| 487 | report_fatal_error("Broken module found, compilation aborted!"); |
| 488 | if (BrokenDebugInfo) { |
| 489 | emitWarning("Invalid debug info found, debug info will be stripped"); |
| 490 | StripDebugInfo(*MergedModule); |
| 491 | } |
| 492 | } |
Duncan P. N. Exon Smith | b011ad7 | 2016-04-20 17:48:22 +0000 | [diff] [blame] | 493 | if (verifyModule(*MergedModule, &dbgs())) |
| 494 | report_fatal_error("Broken module found, compilation aborted!"); |
| 495 | } |
| 496 | |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 497 | /// Optimize merged modules using various IPO passes |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 498 | bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline, |
| 499 | bool DisableGVNLoadPRE, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 500 | bool DisableVectorization) { |
| 501 | if (!this->determineTarget()) |
Shuxin Yang | 95866fa | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 502 | return false; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 503 | |
Mehdi Amini | f59f2bb1 | 2016-04-12 06:34:10 +0000 | [diff] [blame] | 504 | // We always run the verifier once on the merged module, the `DisableVerify` |
| 505 | // parameter only applies to subsequent verify. |
Duncan P. N. Exon Smith | b011ad7 | 2016-04-20 17:48:22 +0000 | [diff] [blame] | 506 | verifyMergedModuleOnce(); |
Mehdi Amini | f59f2bb1 | 2016-04-12 06:34:10 +0000 | [diff] [blame] | 507 | |
Bill Wendling | f44b2a2 | 2013-05-23 21:21:50 +0000 | [diff] [blame] | 508 | // Mark which symbols can not be internalized |
Bill Wendling | 383fda2 | 2012-04-09 22:18:01 +0000 | [diff] [blame] | 509 | this->applyScopeRestrictions(); |
| 510 | |
Bill Wendling | 0e1824c | 2012-03-31 11:15:43 +0000 | [diff] [blame] | 511 | // Instantiate the pass manager to organize the passes. |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 512 | legacy::PassManager passes; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 513 | |
Micah Villmow | 9cfc13d | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 514 | // Add an appropriate DataLayout instance for this module... |
Peter Collingbourne | e34034c | 2015-08-24 21:15:35 +0000 | [diff] [blame] | 515 | MergedModule->setDataLayout(TargetMach->createDataLayout()); |
Bill Wendling | 39d942b | 2012-03-31 10:50:14 +0000 | [diff] [blame] | 516 | |
Chandler Carruth | 5ec2b1d | 2015-02-01 12:26:09 +0000 | [diff] [blame] | 517 | passes.add( |
| 518 | createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis())); |
Chandler Carruth | 1efa12d | 2015-01-30 13:33:42 +0000 | [diff] [blame] | 519 | |
Rafael Espindola | 216e0c0 | 2014-08-21 18:49:52 +0000 | [diff] [blame] | 520 | Triple TargetTriple(TargetMach->getTargetTriple()); |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 521 | PassManagerBuilder PMB; |
| 522 | PMB.DisableGVNLoadPRE = DisableGVNLoadPRE; |
Arnold Schwaighofer | eb1a38f | 2014-10-26 21:50:58 +0000 | [diff] [blame] | 523 | PMB.LoopVectorize = !DisableVectorization; |
| 524 | PMB.SLPVectorize = !DisableVectorization; |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 525 | if (!DisableInline) |
| 526 | PMB.Inliner = createFunctionInliningPass(); |
Chandler Carruth | c029186 | 2015-01-24 02:06:09 +0000 | [diff] [blame] | 527 | PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple); |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 528 | PMB.OptLevel = OptLevel; |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 529 | PMB.VerifyInput = !DisableVerify; |
| 530 | PMB.VerifyOutput = !DisableVerify; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 531 | |
Chandler Carruth | 1efa12d | 2015-01-30 13:33:42 +0000 | [diff] [blame] | 532 | PMB.populateLTOPassManager(passes); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 533 | |
Manman Ren | 8121e1d | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 534 | // Run our queue of passes all at once now, efficiently. |
Peter Collingbourne | e34034c | 2015-08-24 21:15:35 +0000 | [diff] [blame] | 535 | passes.run(*MergedModule); |
Manman Ren | 8121e1d | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 536 | |
| 537 | return true; |
| 538 | } |
| 539 | |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 540 | bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) { |
| 541 | if (!this->determineTarget()) |
Manman Ren | 8121e1d | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 542 | return false; |
| 543 | |
Duncan P. N. Exon Smith | b011ad7 | 2016-04-20 17:48:22 +0000 | [diff] [blame] | 544 | // We always run the verifier once on the merged module. If it has already |
| 545 | // been called in optimize(), this call will return early. |
| 546 | verifyMergedModuleOnce(); |
| 547 | |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 548 | legacy::PassManager preCodeGenPasses; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 549 | |
Bob Wilson | f36f15f | 2013-03-29 23:28:55 +0000 | [diff] [blame] | 550 | // If the bitcode files contain ARC code and were compiled with optimization, |
| 551 | // the ObjCARCContractPass must be run, so do it unconditionally here. |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 552 | preCodeGenPasses.add(createObjCARCContractPass()); |
| 553 | preCodeGenPasses.run(*MergedModule); |
Bob Wilson | f36f15f | 2013-03-29 23:28:55 +0000 | [diff] [blame] | 554 | |
Tobias Edler von Koch | 8ecaf69 | 2016-01-18 23:24:54 +0000 | [diff] [blame] | 555 | // Re-externalize globals that may have been internalized to increase scope |
| 556 | // for splitting |
| 557 | restoreLinkageForExternals(); |
| 558 | |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 559 | // Do code generation. We need to preserve the module in case the client calls |
| 560 | // writeMergedModules() after compilation, but we only need to allow this at |
| 561 | // parallelism level 1. This is achieved by having splitCodeGen return the |
| 562 | // original module at parallelism level 1 which we then assign back to |
| 563 | // MergedModule. |
Rafael Espindola | 3c1c987 | 2016-04-17 18:42:27 +0000 | [diff] [blame] | 564 | MergedModule = splitCodeGen(std::move(MergedModule), Out, {}, |
| 565 | [&]() { return createTargetMachine(); }, FileType, |
| 566 | ShouldRestoreGlobalsLinkage); |
Nick Lewycky | fd6a249 | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 567 | |
Adam Nemet | 106feda | 2016-02-16 21:41:51 +0000 | [diff] [blame] | 568 | // If statistics were requested, print them out after codegen. |
| 569 | if (llvm::AreStatisticsEnabled()) |
| 570 | llvm::PrintStatistics(); |
| 571 | |
Shuxin Yang | 95866fa | 2013-08-06 21:51:21 +0000 | [diff] [blame] | 572 | return true; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Bill Wendling | 534a658 | 2012-03-31 10:49:43 +0000 | [diff] [blame] | 575 | /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging |
| 576 | /// LTO problems. |
Mehdi Amini | 4cc259a | 2016-10-01 01:18:23 +0000 | [diff] [blame] | 577 | void LTOCodeGenerator::setCodeGenDebugOptions(StringRef Options) { |
Duncan P. N. Exon Smith | f496775 | 2015-08-31 23:44:06 +0000 | [diff] [blame] | 578 | for (std::pair<StringRef, StringRef> o = getToken(Options); !o.first.empty(); |
| 579 | o = getToken(o.second)) |
Peter Collingbourne | 2257512 | 2015-08-21 04:45:55 +0000 | [diff] [blame] | 580 | CodegenOptions.push_back(o.first); |
Nick Kledzik | c232347 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 581 | } |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 582 | |
| 583 | void LTOCodeGenerator::parseCodeGenDebugOptions() { |
| 584 | // if options were requested, set them |
Peter Collingbourne | 2257512 | 2015-08-21 04:45:55 +0000 | [diff] [blame] | 585 | if (!CodegenOptions.empty()) { |
| 586 | // ParseCommandLineOptions() expects argv[0] to be program name. |
| 587 | std::vector<const char *> CodegenArgv(1, "libLLVMLTO"); |
| 588 | for (std::string &Arg : CodegenOptions) |
| 589 | CodegenArgv.push_back(Arg.c_str()); |
| 590 | cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data()); |
| 591 | } |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 592 | } |
Quentin Colombet | 5fa1f6f | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 593 | |
| 594 | void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI, |
| 595 | void *Context) { |
| 596 | ((LTOCodeGenerator *)Context)->DiagnosticHandler2(DI); |
| 597 | } |
| 598 | |
| 599 | void LTOCodeGenerator::DiagnosticHandler2(const DiagnosticInfo &DI) { |
| 600 | // Map the LLVM internal diagnostic severity to the LTO diagnostic severity. |
| 601 | lto_codegen_diagnostic_severity_t Severity; |
| 602 | switch (DI.getSeverity()) { |
| 603 | case DS_Error: |
| 604 | Severity = LTO_DS_ERROR; |
| 605 | break; |
| 606 | case DS_Warning: |
| 607 | Severity = LTO_DS_WARNING; |
| 608 | break; |
Tobias Grosser | e8d4c9a | 2014-02-28 09:08:45 +0000 | [diff] [blame] | 609 | case DS_Remark: |
| 610 | Severity = LTO_DS_REMARK; |
| 611 | break; |
Quentin Colombet | 5fa1f6f | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 612 | case DS_Note: |
| 613 | Severity = LTO_DS_NOTE; |
| 614 | break; |
| 615 | } |
| 616 | // Create the string that will be reported to the external diagnostic handler. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 617 | std::string MsgStorage; |
| 618 | raw_string_ostream Stream(MsgStorage); |
| 619 | DiagnosticPrinterRawOStream DP(Stream); |
Quentin Colombet | 5fa1f6f | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 620 | DI.print(DP); |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 621 | Stream.flush(); |
Quentin Colombet | 5fa1f6f | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 622 | |
| 623 | // If this method has been called it means someone has set up an external |
| 624 | // diagnostic handler. Assert on that. |
| 625 | assert(DiagHandler && "Invalid diagnostic handler"); |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 626 | (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext); |
Quentin Colombet | 5fa1f6f | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | void |
| 630 | LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler, |
| 631 | void *Ctxt) { |
| 632 | this->DiagHandler = DiagHandler; |
| 633 | this->DiagContext = Ctxt; |
| 634 | if (!DiagHandler) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 635 | return Context.setDiagnosticHandler(nullptr, nullptr); |
Quentin Colombet | 5fa1f6f | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 636 | // Register the LTOCodeGenerator stub in the LLVMContext to forward the |
| 637 | // diagnostic to the external DiagHandler. |
Duncan P. N. Exon Smith | f02fe70 | 2014-10-02 21:11:04 +0000 | [diff] [blame] | 638 | Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this, |
| 639 | /* RespectFilters */ true); |
Quentin Colombet | 5fa1f6f | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 640 | } |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 641 | |
| 642 | namespace { |
| 643 | class LTODiagnosticInfo : public DiagnosticInfo { |
| 644 | const Twine &Msg; |
| 645 | public: |
| 646 | LTODiagnosticInfo(const Twine &DiagMsg, DiagnosticSeverity Severity=DS_Error) |
| 647 | : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {} |
| 648 | void print(DiagnosticPrinter &DP) const override { DP << Msg; } |
| 649 | }; |
| 650 | } |
| 651 | |
| 652 | void LTOCodeGenerator::emitError(const std::string &ErrMsg) { |
| 653 | if (DiagHandler) |
| 654 | (*DiagHandler)(LTO_DS_ERROR, ErrMsg.c_str(), DiagContext); |
| 655 | else |
| 656 | Context.diagnose(LTODiagnosticInfo(ErrMsg)); |
| 657 | } |
Mehdi Amini | 8eb3397 | 2016-05-05 20:05:33 +0000 | [diff] [blame] | 658 | |
| 659 | void LTOCodeGenerator::emitWarning(const std::string &ErrMsg) { |
| 660 | if (DiagHandler) |
| 661 | (*DiagHandler)(LTO_DS_WARNING, ErrMsg.c_str(), DiagContext); |
| 662 | else |
| 663 | Context.diagnose(LTODiagnosticInfo(ErrMsg, DS_Warning)); |
| 664 | } |