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