blob: 70626682d5578ddb31e28eaed60e477ccc53b630 [file] [log] [blame]
Nick Kledzik07b4a622008-02-26 20:26:43 +00001//===-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 Wendling39d942b2012-03-31 10:50:14 +00007//
Nick Kledzik07b4a622008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Bill Wendling39d942b2012-03-31 10:50:14 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik07b4a622008-02-26 20:26:43 +000011// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000015#include "llvm/LTO/LTOCodeGenerator.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000016#include "llvm/ADT/StringExtras.h"
Nick Lewycky510dae32009-06-17 06:52:10 +000017#include "llvm/Analysis/Passes.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000018#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000019#include "llvm/Analysis/TargetTransformInfo.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000020#include "llvm/Bitcode/ReaderWriter.h"
Peter Collingbournec269ed52015-08-27 23:37:36 +000021#include "llvm/CodeGen/ParallelCG.h"
Justin Bognerb10a5202013-11-12 21:44:01 +000022#include "llvm/CodeGen/RuntimeLibcalls.h"
Bill Wendling0e1824c2012-03-31 11:15:43 +000023#include "llvm/Config/config.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/DerivedTypes.h"
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +000027#include "llvm/IR/DiagnosticInfo.h"
28#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000030#include "llvm/IR/LegacyPassManager.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000031#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Module.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000033#include "llvm/IR/Verifier.h"
Shuxin Yang1e6d80e2013-07-22 18:40:34 +000034#include "llvm/InitializePasses.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000035#include "llvm/LTO/LTOModule.h"
Chandler Carruth6cc07df2014-03-06 03:42:23 +000036#include "llvm/Linker/Linker.h"
Chris Lattner2eff5052010-03-12 18:44:54 +000037#include "llvm/MC/MCAsmInfo.h"
38#include "llvm/MC/MCContext.h"
Evan Cheng8264e272011-06-29 01:14:12 +000039#include "llvm/MC/SubtargetFeature.h"
Nick Kledzikc2323472008-07-08 21:14:10 +000040#include "llvm/Support/CommandLine.h"
Rafael Espindola40c908b2013-06-17 18:05:35 +000041#include "llvm/Support/FileSystem.h"
Michael J. Spencerab425d82010-11-29 18:47:54 +000042#include "llvm/Support/Host.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000043#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencerab425d82010-11-29 18:47:54 +000044#include "llvm/Support/Signals.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000045#include "llvm/Support/TargetRegistry.h"
46#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000047#include "llvm/Support/ToolOutputFile.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000048#include "llvm/Support/raw_ostream.h"
Justin Bognerb10a5202013-11-12 21:44:01 +000049#include "llvm/Target/TargetLowering.h"
Rafael Espindola0b385c72013-09-30 16:39:19 +000050#include "llvm/Target/TargetOptions.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000051#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000052#include "llvm/Target/TargetSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000053#include "llvm/Transforms/IPO.h"
54#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Bob Wilsonf36f15f2013-03-29 23:28:55 +000055#include "llvm/Transforms/ObjCARC.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000056#include <system_error>
Shuxin Yang76d082b2013-08-12 18:29:43 +000057using namespace llvm;
Shuxin Yang76d082b2013-08-12 18:29:43 +000058
Bill Wendling534a6582012-03-31 10:49:43 +000059const char* LTOCodeGenerator::getVersionString() {
Nick Kledzik07b4a622008-02-26 20:26:43 +000060#ifdef LLVM_VERSION_INFO
Bill Wendling534a6582012-03-31 10:49:43 +000061 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
Nick Kledzik07b4a622008-02-26 20:26:43 +000062#else
Bill Wendling534a6582012-03-31 10:49:43 +000063 return PACKAGE_NAME " version " PACKAGE_VERSION;
Nick Kledzik07b4a622008-02-26 20:26:43 +000064#endif
65}
66
Rafael Espindola7b8a24e2015-12-04 02:42:28 +000067LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
Rafael Espindolaf49a38f2015-12-04 22:08:53 +000068 : Context(Context), MergedModule(new Module("ld-temp.o", Context)),
Teresa Johnsonbef54362015-12-18 19:28:59 +000069 TheLinker(new Linker(*MergedModule)) {
Duncan P. N. Exon Smith7832e0a2015-04-27 23:19:26 +000070 initializeLTOPasses();
Duncan P. N. Exon Smithde5e32b2014-11-11 23:03:29 +000071}
72
Peter Collingbourne9c8909d2015-08-24 22:22:53 +000073LTOCodeGenerator::~LTOCodeGenerator() {}
Nick Kledzik07b4a622008-02-26 20:26:43 +000074
Yaron Keren55f5c3d2015-09-01 10:13:49 +000075// Initialize LTO passes. Please keep this function in sync with
Shuxin Yangca760852013-07-23 06:44:34 +000076// PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
Duncan P. N. Exon Smithbccb4fd2014-01-10 20:24:35 +000077// passes are initialized.
Shuxin Yang1e6d80e2013-07-22 18:40:34 +000078void LTOCodeGenerator::initializeLTOPasses() {
79 PassRegistry &R = *PassRegistry::getPassRegistry();
80
81 initializeInternalizePassPass(R);
82 initializeIPSCCPPass(R);
83 initializeGlobalOptPass(R);
84 initializeConstantMergePass(R);
85 initializeDAHPass(R);
Chandler Carruth1edb9d62015-01-20 22:44:35 +000086 initializeInstructionCombiningPassPass(R);
Shuxin Yang1e6d80e2013-07-22 18:40:34 +000087 initializeSimpleInlinerPass(R);
88 initializePruneEHPass(R);
89 initializeGlobalDCEPass(R);
90 initializeArgPromotionPass(R);
91 initializeJumpThreadingPass(R);
Chandler Carruth29a18a42015-09-12 09:09:14 +000092 initializeSROALegacyPassPass(R);
Shuxin Yang1e6d80e2013-07-22 18:40:34 +000093 initializeSROA_DTPass(R);
94 initializeSROA_SSAUpPass(R);
Chandler Carruth1926b702016-01-08 10:55:52 +000095 initializePostOrderFunctionAttrsPass(R);
96 initializeReversePostOrderFunctionAttrsPass(R);
Chandler Carruth7b560d42015-09-09 17:55:00 +000097 initializeGlobalsAAWrapperPassPass(R);
Shuxin Yang1e6d80e2013-07-22 18:40:34 +000098 initializeLICMPass(R);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000099 initializeMergedLoadStoreMotionPass(R);
Shuxin Yang1e6d80e2013-07-22 18:40:34 +0000100 initializeGVNPass(R);
101 initializeMemCpyOptPass(R);
102 initializeDCEPass(R);
Tom Stellardaa664d92013-08-06 02:43:45 +0000103 initializeCFGSimplifyPassPass(R);
Shuxin Yang1e6d80e2013-07-22 18:40:34 +0000104}
105
Duncan P. N. Exon Smithf4967752015-08-31 23:44:06 +0000106bool LTOCodeGenerator::addModule(LTOModule *Mod) {
107 assert(&Mod->getModule().getContext() == &Context &&
Duncan P. N. Exon Smith94198632014-11-11 23:13:10 +0000108 "Expected module in same context");
109
Teresa Johnsonbef54362015-12-18 19:28:59 +0000110 bool ret = TheLinker->linkInModule(Mod->takeModule());
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000111
Duncan P. N. Exon Smithf4967752015-08-31 23:44:06 +0000112 const std::vector<const char *> &undefs = Mod->getAsmUndefinedRefs();
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000113 for (int i = 0, e = undefs.size(); i != e; ++i)
Rafael Espindolac80c9692013-09-04 17:44:24 +0000114 AsmUndefinedRefs[undefs[i]] = 1;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000115
Shuxin Yangb6696a92013-08-07 05:19:23 +0000116 return !ret;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000117}
Bill Wendling39d942b2012-03-31 10:50:14 +0000118
Peter Collingbourne9c8909d2015-08-24 22:22:53 +0000119void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> Mod) {
Manman Ren6487ce92015-02-24 00:45:56 +0000120 assert(&Mod->getModule().getContext() == &Context &&
121 "Expected module in same context");
122
Manman Ren6487ce92015-02-24 00:45:56 +0000123 AsmUndefinedRefs.clear();
124
Peter Collingbourne9c8909d2015-08-24 22:22:53 +0000125 MergedModule = Mod->takeModule();
Teresa Johnsonbef54362015-12-18 19:28:59 +0000126 TheLinker = make_unique<Linker>(*MergedModule);
Manman Ren6487ce92015-02-24 00:45:56 +0000127
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 Smithf4967752015-08-31 23:44:06 +0000133void LTOCodeGenerator::setTargetOptions(TargetOptions Options) {
134 this->Options = Options;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000135}
136
Duncan P. N. Exon Smithf4967752015-08-31 23:44:06 +0000137void LTOCodeGenerator::setDebugInfo(lto_debug_model Debug) {
138 switch (Debug) {
Bill Wendling0e1824c2012-03-31 11:15:43 +0000139 case LTO_DEBUG_MODEL_NONE:
Rafael Espindolac80c9692013-09-04 17:44:24 +0000140 EmitDwarfDebugInfo = false;
Shuxin Yangb6696a92013-08-07 05:19:23 +0000141 return;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000142
Bill Wendling0e1824c2012-03-31 11:15:43 +0000143 case LTO_DEBUG_MODEL_DWARF:
Rafael Espindolac80c9692013-09-04 17:44:24 +0000144 EmitDwarfDebugInfo = true;
Shuxin Yangb6696a92013-08-07 05:19:23 +0000145 return;
Bill Wendling0e1824c2012-03-31 11:15:43 +0000146 }
147 llvm_unreachable("Unknown debug format!");
Nick Kledzik07b4a622008-02-26 20:26:43 +0000148}
149
Duncan P. N. Exon Smithf4967752015-08-31 23:44:06 +0000150void LTOCodeGenerator::setOptLevel(unsigned Level) {
151 OptLevel = Level;
Peter Collingbournec7b675f2015-08-22 02:25:53 +0000152 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 Gao8e348cc2015-11-17 19:48:12 +0000168bool LTOCodeGenerator::writeMergedModules(const char *Path) {
169 if (!determineTarget())
Shuxin Yangb6696a92013-08-07 05:19:23 +0000170 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000171
Bill Wendling2bbbfef2013-08-08 23:51:04 +0000172 // mark which symbols can not be internalized
173 applyScopeRestrictions();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000174
Chris Lattner69733952009-08-23 07:49:08 +0000175 // create output file
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000176 std::error_code EC;
Duncan P. N. Exon Smithf4967752015-08-31 23:44:06 +0000177 tool_output_file Out(Path, EC, sys::fs::F_None);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000178 if (EC) {
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000179 std::string ErrMsg = "could not open bitcode file for writing: ";
Duncan P. N. Exon Smithf4967752015-08-31 23:44:06 +0000180 ErrMsg += Path;
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000181 emitError(ErrMsg);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000182 return false;
Chris Lattner69733952009-08-23 07:49:08 +0000183 }
Bill Wendling39d942b2012-03-31 10:50:14 +0000184
Chris Lattner69733952009-08-23 07:49:08 +0000185 // write bitcode to it
Peter Collingbourne9c8909d2015-08-24 22:22:53 +0000186 WriteBitcodeToFile(MergedModule.get(), Out.os(), ShouldEmbedUselists);
Dan Gohmana2233f22010-09-01 14:20:41 +0000187 Out.os().close();
Dan Gohmanab366f02010-05-27 20:19:47 +0000188
Dan Gohmana2233f22010-09-01 14:20:41 +0000189 if (Out.os().has_error()) {
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000190 std::string ErrMsg = "could not write bitcode file: ";
Duncan P. N. Exon Smithf4967752015-08-31 23:44:06 +0000191 ErrMsg += Path;
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000192 emitError(ErrMsg);
Dan Gohmana2233f22010-09-01 14:20:41 +0000193 Out.os().clear_error();
Shuxin Yangb6696a92013-08-07 05:19:23 +0000194 return false;
Chris Lattner69733952009-08-23 07:49:08 +0000195 }
Bill Wendling39d942b2012-03-31 10:50:14 +0000196
Dan Gohman8525fe72010-08-20 16:59:15 +0000197 Out.keep();
Shuxin Yangb6696a92013-08-07 05:19:23 +0000198 return true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000199}
200
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000201bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
Tobias Edler von Koch4d450902015-11-19 23:59:24 +0000202 // make unique temp output file to put generated code
Shuxin Yang1826ae22013-08-12 21:07:31 +0000203 SmallString<128> Filename;
204 int FD;
Tobias Edler von Koch4d450902015-11-19 23:59:24 +0000205
206 const char *Extension =
207 (FileType == TargetMachine::CGFT_AssemblyFile ? "s" : "o");
208
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000209 std::error_code EC =
Tobias Edler von Koch4d450902015-11-19 23:59:24 +0000210 sys::fs::createTemporaryFile("lto-llvm", Extension, FD, Filename);
Shuxin Yang1826ae22013-08-12 21:07:31 +0000211 if (EC) {
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000212 emitError(EC.message());
Shuxin Yangb6696a92013-08-07 05:19:23 +0000213 return false;
Shuxin Yang1826ae22013-08-12 21:07:31 +0000214 }
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000215
Shuxin Yang1826ae22013-08-12 21:07:31 +0000216 // generate object file
217 tool_output_file objFile(Filename.c_str(), FD);
Bill Wendling0e1824c2012-03-31 11:15:43 +0000218
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000219 bool genResult = compileOptimized(&objFile.os());
Shuxin Yang1826ae22013-08-12 21:07:31 +0000220 objFile.os().close();
221 if (objFile.os().has_error()) {
222 objFile.os().clear_error();
223 sys::fs::remove(Twine(Filename));
Shuxin Yangb6696a92013-08-07 05:19:23 +0000224 return false;
Shuxin Yang1826ae22013-08-12 21:07:31 +0000225 }
Bill Wendling0e1824c2012-03-31 11:15:43 +0000226
Shuxin Yang1826ae22013-08-12 21:07:31 +0000227 objFile.keep();
228 if (!genResult) {
229 sys::fs::remove(Twine(Filename));
Shuxin Yangb6696a92013-08-07 05:19:23 +0000230 return false;
Shuxin Yang1826ae22013-08-12 21:07:31 +0000231 }
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000232
Rafael Espindolac80c9692013-09-04 17:44:24 +0000233 NativeObjectPath = Filename.c_str();
Duncan P. N. Exon Smithf4967752015-08-31 23:44:06 +0000234 *Name = NativeObjectPath.c_str();
Shuxin Yangb6696a92013-08-07 05:19:23 +0000235 return true;
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000236}
237
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000238std::unique_ptr<MemoryBuffer>
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000239LTOCodeGenerator::compileOptimized() {
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000240 const char *name;
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000241 if (!compileOptimizedToFile(&name))
Craig Topper2617dcc2014-04-15 06:32:26 +0000242 return nullptr;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000243
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000244 // read .o file into memory buffer
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000245 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
246 MemoryBuffer::getFile(name, -1, false);
247 if (std::error_code EC = BufferOrErr.getError()) {
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000248 emitError(EC.message());
Rafael Espindolac80c9692013-09-04 17:44:24 +0000249 sys::fs::remove(NativeObjectPath);
Craig Topper2617dcc2014-04-15 06:32:26 +0000250 return nullptr;
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000251 }
Rafael Espindolafac373c2011-02-24 21:04:06 +0000252
Shuxin Yang1826ae22013-08-12 21:07:31 +0000253 // remove temp files
Rafael Espindolac80c9692013-09-04 17:44:24 +0000254 sys::fs::remove(NativeObjectPath);
Rafael Espindolafac373c2011-02-24 21:04:06 +0000255
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000256 return std::move(*BufferOrErr);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000257}
258
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000259bool LTOCodeGenerator::compile_to_file(const char **Name, bool DisableVerify,
260 bool DisableInline,
Duncan P. N. Exon Smithf4967752015-08-31 23:44:06 +0000261 bool DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000262 bool DisableVectorization) {
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000263 if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000264 DisableVectorization))
Manman Ren8121e1d2015-02-03 18:39:15 +0000265 return false;
266
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000267 return compileOptimizedToFile(Name);
Manman Ren8121e1d2015-02-03 18:39:15 +0000268}
269
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000270std::unique_ptr<MemoryBuffer>
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000271LTOCodeGenerator::compile(bool DisableVerify, bool DisableInline,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000272 bool DisableGVNLoadPRE, bool DisableVectorization) {
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000273 if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000274 DisableVectorization))
Manman Ren8121e1d2015-02-03 18:39:15 +0000275 return nullptr;
276
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000277 return compileOptimized();
Manman Ren8121e1d2015-02-03 18:39:15 +0000278}
279
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000280bool LTOCodeGenerator::determineTarget() {
Craig Topper2617dcc2014-04-15 06:32:26 +0000281 if (TargetMach)
Shuxin Yang95866fa2013-08-06 21:51:21 +0000282 return true;
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000283
Peter Collingbourne9c8909d2015-08-24 22:22:53 +0000284 std::string TripleStr = MergedModule->getTargetTriple();
Peter Collingbournec7b675f2015-08-22 02:25:53 +0000285 if (TripleStr.empty()) {
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000286 TripleStr = sys::getDefaultTargetTriple();
Peter Collingbourne9c8909d2015-08-24 22:22:53 +0000287 MergedModule->setTargetTriple(TripleStr);
Peter Collingbournec7b675f2015-08-22 02:25:53 +0000288 }
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000289 llvm::Triple Triple(TripleStr);
Bill Wendling9b2c5732008-06-18 06:35:30 +0000290
Bill Wendling45f74e32012-08-06 22:52:45 +0000291 // create target machine from info for merged modules
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000292 std::string ErrMsg;
Duncan P. N. Exon Smithf4967752015-08-31 23:44:06 +0000293 const Target *march = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000294 if (!march) {
295 emitError(ErrMsg);
Shuxin Yang95866fa2013-08-06 21:51:21 +0000296 return false;
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000297 }
Bill Wendling0e1824c2012-03-31 11:15:43 +0000298
Tom Roederfd1bc602014-04-25 21:46:51 +0000299 // Construct LTOModule, hand over ownership of module and target. Use MAttr as
300 // the default set of features.
301 SubtargetFeatures Features(MAttr);
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000302 Features.getDefaultSubtargetFeatures(Triple);
Peter Collingbournec7b675f2015-08-22 02:25:53 +0000303 FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000304 // Set a default CPU for Darwin triples.
Rafael Espindolac80c9692013-09-04 17:44:24 +0000305 if (MCpu.empty() && Triple.isOSDarwin()) {
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000306 if (Triple.getArch() == llvm::Triple::x86_64)
Rafael Espindolac80c9692013-09-04 17:44:24 +0000307 MCpu = "core2";
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000308 else if (Triple.getArch() == llvm::Triple::x86)
Rafael Espindolac80c9692013-09-04 17:44:24 +0000309 MCpu = "yonah";
Tim Northovere19bed72014-07-23 12:32:47 +0000310 else if (Triple.getArch() == llvm::Triple::aarch64)
Tim Northover00ed9962014-03-29 10:18:08 +0000311 MCpu = "cyclone";
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000312 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000313
Peter Collingbourneec43d0f2015-08-21 04:45:57 +0000314 TargetMach.reset(march->createTargetMachine(TripleStr, MCpu, FeatureStr,
315 Options, RelocModel,
316 CodeModel::Default, CGOptLevel));
Shuxin Yang95866fa2013-08-06 21:51:21 +0000317 return true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000318}
319
Bill Wendling0e1824c2012-03-31 11:15:43 +0000320void LTOCodeGenerator::
321applyRestriction(GlobalValue &GV,
Craig Topper3af97222014-08-27 05:25:00 +0000322 ArrayRef<StringRef> Libcalls,
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000323 std::vector<const char*> &MustPreserveList,
Craig Topper71b7b682014-08-21 05:55:13 +0000324 SmallPtrSetImpl<GlobalValue*> &AsmUsed,
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000325 Mangler &Mangler) {
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000326 // There are no restrictions to apply to declarations.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000327 if (GV.isDeclaration())
328 return;
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000329
330 // There is nothing more restrictive than private linkage.
331 if (GV.hasPrivateLinkage())
332 return;
333
334 SmallString<64> Buffer;
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000335 TargetMach->getNameWithPrefix(Buffer, &GV, Mangler);
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000336
Rafael Espindolac80c9692013-09-04 17:44:24 +0000337 if (MustPreserveSymbols.count(Buffer))
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000338 MustPreserveList.push_back(GV.getName().data());
Rafael Espindolac80c9692013-09-04 17:44:24 +0000339 if (AsmUndefinedRefs.count(Buffer))
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000340 AsmUsed.insert(&GV);
Justin Bognerb10a5202013-11-12 21:44:01 +0000341
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 Koch8ecaf692016-01-18 23:24:54 +0000350
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 Espindola1e49a6d2011-03-02 04:14:42 +0000356}
357
358static void findUsedValues(GlobalVariable *LLVMUsed,
Craig Topper71b7b682014-08-21 05:55:13 +0000359 SmallPtrSetImpl<GlobalValue*> &UsedValues) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000360 if (!LLVMUsed) return;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000361
Rafael Espindolacc111b22013-04-24 17:54:35 +0000362 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000363 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling39d942b2012-03-31 10:50:14 +0000364 if (GlobalValue *GV =
Bill Wendling0e1824c2012-03-31 11:15:43 +0000365 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000366 UsedValues.insert(GV);
367}
368
Akira Hatanaka8fba18e2015-01-30 01:16:24 +0000369// 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 Bognerb10a5202013-11-12 21:44:01 +0000372static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls,
373 const TargetLibraryInfo& TLI,
Akira Hatanaka8fba18e2015-01-30 01:16:24 +0000374 const Module &Mod,
375 const TargetMachine &TM) {
Justin Bognerb10a5202013-11-12 21:44:01 +0000376 // 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 Hatanaka8fba18e2015-01-30 01:16:24 +0000385 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 Bognerb10a5202013-11-12 21:44:01 +0000400
Duncan P. N. Exon Smith0c8d6042013-11-16 16:15:56 +0000401 array_pod_sort(Libcalls.begin(), Libcalls.end());
Justin Bognerb10a5202013-11-12 21:44:01 +0000402 Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()),
403 Libcalls.end());
404}
405
Chris Lattner2eff5052010-03-12 18:44:54 +0000406void LTOCodeGenerator::applyScopeRestrictions() {
Manman Rence0a0662015-04-17 17:10:09 +0000407 if (ScopeRestrictionsDone || !ShouldInternalize)
Rafael Espindolac80c9692013-09-04 17:44:24 +0000408 return;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000409
Chris Lattner2eff5052010-03-12 18:44:54 +0000410 // Start off with a verification pass.
Chandler Carruth30d69c22015-02-13 10:01:29 +0000411 legacy::PassManager passes;
Chris Lattner2eff5052010-03-12 18:44:54 +0000412 passes.add(createVerifierPass());
Nick Kledzik07b4a622008-02-26 20:26:43 +0000413
Bill Wendling39d942b2012-03-31 10:50:14 +0000414 // mark which symbols can not be internalized
Rafael Espindolac233f742015-06-23 13:59:29 +0000415 Mangler Mangler;
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000416 std::vector<const char*> MustPreserveList;
417 SmallPtrSet<GlobalValue*, 8> AsmUsed;
Justin Bognerb10a5202013-11-12 21:44:01 +0000418 std::vector<StringRef> Libcalls;
Chandler Carruthc0291862015-01-24 02:06:09 +0000419 TargetLibraryInfoImpl TLII(Triple(TargetMach->getTargetTriple()));
420 TargetLibraryInfo TLI(TLII);
Akira Hatanaka8fba18e2015-01-30 01:16:24 +0000421
Peter Collingbournee34034c2015-08-24 21:15:35 +0000422 accumulateAndSortLibcalls(Libcalls, TLI, *MergedModule, *TargetMach);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000423
Peter Collingbournee34034c2015-08-24 21:15:35 +0000424 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 Espindola1e49a6d2011-03-02 04:14:42 +0000430
431 GlobalVariable *LLVMCompilerUsed =
Peter Collingbournee34034c2015-08-24 21:15:35 +0000432 MergedModule->getGlobalVariable("llvm.compiler.used");
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000433 findUsedValues(LLVMCompilerUsed, AsmUsed);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000434 if (LLVMCompilerUsed)
435 LLVMCompilerUsed->eraseFromParent();
436
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000437 if (!AsmUsed.empty()) {
Rafael Espindolac80c9692013-09-04 17:44:24 +0000438 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context);
Rafael Espindolacc111b22013-04-24 17:54:35 +0000439 std::vector<Constant*> asmUsed2;
Rafael Espindola9c8c96f2014-05-05 20:06:41 +0000440 for (auto *GV : AsmUsed) {
Rafael Espindolacc111b22013-04-24 17:54:35 +0000441 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 Collingbournee34034c2015-08-24 21:15:35 +0000447 new llvm::GlobalVariable(*MergedModule, ATy, false,
Rafael Espindolacc111b22013-04-24 17:54:35 +0000448 llvm::GlobalValue::AppendingLinkage,
449 llvm::ConstantArray::get(ATy, asmUsed2),
450 "llvm.compiler.used");
451
452 LLVMCompilerUsed->setSection("llvm.metadata");
Chris Lattner2eff5052010-03-12 18:44:54 +0000453 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000454
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +0000455 passes.add(createInternalizePass(MustPreserveList));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000456
Chris Lattner2eff5052010-03-12 18:44:54 +0000457 // apply scope restrictions
Peter Collingbournee34034c2015-08-24 21:15:35 +0000458 passes.run(*MergedModule);
Bill Wendling39d942b2012-03-31 10:50:14 +0000459
Rafael Espindolac80c9692013-09-04 17:44:24 +0000460 ScopeRestrictionsDone = true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000461}
462
Tobias Edler von Koch8ecaf692016-01-18 23:24:54 +0000463/// Restore original linkage for symbols that may have been internalized
464void 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 Kledzik07b4a622008-02-26 20:26:43 +0000492/// Optimize merged modules using various IPO passes
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000493bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
494 bool DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000495 bool DisableVectorization) {
496 if (!this->determineTarget())
Shuxin Yang95866fa2013-08-06 21:51:21 +0000497 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000498
Bill Wendlingf44b2a22013-05-23 21:21:50 +0000499 // Mark which symbols can not be internalized
Bill Wendling383fda22012-04-09 22:18:01 +0000500 this->applyScopeRestrictions();
501
Bill Wendling0e1824c2012-03-31 11:15:43 +0000502 // Instantiate the pass manager to organize the passes.
Chandler Carruth30d69c22015-02-13 10:01:29 +0000503 legacy::PassManager passes;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000504
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000505 // Add an appropriate DataLayout instance for this module...
Peter Collingbournee34034c2015-08-24 21:15:35 +0000506 MergedModule->setDataLayout(TargetMach->createDataLayout());
Bill Wendling39d942b2012-03-31 10:50:14 +0000507
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +0000508 passes.add(
509 createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
Chandler Carruth1efa12d2015-01-30 13:33:42 +0000510
Rafael Espindola216e0c02014-08-21 18:49:52 +0000511 Triple TargetTriple(TargetMach->getTargetTriple());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000512 PassManagerBuilder PMB;
513 PMB.DisableGVNLoadPRE = DisableGVNLoadPRE;
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000514 PMB.LoopVectorize = !DisableVectorization;
515 PMB.SLPVectorize = !DisableVectorization;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000516 if (!DisableInline)
517 PMB.Inliner = createFunctionInliningPass();
Chandler Carruthc0291862015-01-24 02:06:09 +0000518 PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
Peter Collingbourne070843d2015-03-19 22:01:00 +0000519 PMB.OptLevel = OptLevel;
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000520 PMB.VerifyInput = !DisableVerify;
521 PMB.VerifyOutput = !DisableVerify;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000522
Chandler Carruth1efa12d2015-01-30 13:33:42 +0000523 PMB.populateLTOPassManager(passes);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000524
Manman Ren8121e1d2015-02-03 18:39:15 +0000525 // Run our queue of passes all at once now, efficiently.
Peter Collingbournee34034c2015-08-24 21:15:35 +0000526 passes.run(*MergedModule);
Manman Ren8121e1d2015-02-03 18:39:15 +0000527
528 return true;
529}
530
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000531bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) {
532 if (!this->determineTarget())
Manman Ren8121e1d2015-02-03 18:39:15 +0000533 return false;
534
Peter Collingbournec269ed52015-08-27 23:37:36 +0000535 legacy::PassManager preCodeGenPasses;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000536
Bob Wilsonf36f15f2013-03-29 23:28:55 +0000537 // If the bitcode files contain ARC code and were compiled with optimization,
538 // the ObjCARCContractPass must be run, so do it unconditionally here.
Peter Collingbournec269ed52015-08-27 23:37:36 +0000539 preCodeGenPasses.add(createObjCARCContractPass());
540 preCodeGenPasses.run(*MergedModule);
Bob Wilsonf36f15f2013-03-29 23:28:55 +0000541
Tobias Edler von Koch8ecaf692016-01-18 23:24:54 +0000542 // Re-externalize globals that may have been internalized to increase scope
543 // for splitting
544 restoreLinkageForExternals();
545
Peter Collingbournec269ed52015-08-27 23:37:36 +0000546 // 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 Smithf4967752015-08-31 23:44:06 +0000552 splitCodeGen(std::move(MergedModule), Out, MCpu, FeatureStr, Options,
Tobias Edler von Koch8ecaf692016-01-18 23:24:54 +0000553 RelocModel, CodeModel::Default, CGOptLevel, FileType,
554 ShouldRestoreGlobalsLinkage);
Nick Lewyckyfd6a2492009-07-26 22:16:39 +0000555
Shuxin Yang95866fa2013-08-06 21:51:21 +0000556 return true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000557}
558
Bill Wendling534a6582012-03-31 10:49:43 +0000559/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
560/// LTO problems.
Duncan P. N. Exon Smithf4967752015-08-31 23:44:06 +0000561void LTOCodeGenerator::setCodeGenDebugOptions(const char *Options) {
562 for (std::pair<StringRef, StringRef> o = getToken(Options); !o.first.empty();
563 o = getToken(o.second))
Peter Collingbourne22575122015-08-21 04:45:55 +0000564 CodegenOptions.push_back(o.first);
Nick Kledzikc2323472008-07-08 21:14:10 +0000565}
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000566
567void LTOCodeGenerator::parseCodeGenDebugOptions() {
568 // if options were requested, set them
Peter Collingbourne22575122015-08-21 04:45:55 +0000569 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 Espindolaefa02d52013-10-02 14:36:23 +0000576}
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000577
578void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI,
579 void *Context) {
580 ((LTOCodeGenerator *)Context)->DiagnosticHandler2(DI);
581}
582
583void 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 Grossere8d4c9a2014-02-28 09:08:45 +0000593 case DS_Remark:
594 Severity = LTO_DS_REMARK;
595 break;
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000596 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 Tokere69170a2014-06-26 22:52:05 +0000601 std::string MsgStorage;
602 raw_string_ostream Stream(MsgStorage);
603 DiagnosticPrinterRawOStream DP(Stream);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000604 DI.print(DP);
Alp Tokere69170a2014-06-26 22:52:05 +0000605 Stream.flush();
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000606
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 Tokere69170a2014-06-26 22:52:05 +0000610 (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000611}
612
613void
614LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
615 void *Ctxt) {
616 this->DiagHandler = DiagHandler;
617 this->DiagContext = Ctxt;
618 if (!DiagHandler)
Craig Topper2617dcc2014-04-15 06:32:26 +0000619 return Context.setDiagnosticHandler(nullptr, nullptr);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000620 // Register the LTOCodeGenerator stub in the LLVMContext to forward the
621 // diagnostic to the external DiagHandler.
Duncan P. N. Exon Smithf02fe702014-10-02 21:11:04 +0000622 Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this,
623 /* RespectFilters */ true);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000624}
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000625
626namespace {
627class LTODiagnosticInfo : public DiagnosticInfo {
628 const Twine &Msg;
629public:
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
636void 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}