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