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