blob: ed881f2ddf6882d915562a90e9156cb4ceabffd2 [file] [log] [blame]
Daniel Dunbarf976d1b2010-06-07 23:20:08 +00001//===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Dunbarf976d1b2010-06-07 23:20:08 +00006//
7//===----------------------------------------------------------------------===//
8
Daniel Dunbarc1b17292010-06-15 17:48:49 +00009#include "clang/CodeGen/BackendUtil.h"
Richard Trieu63688182018-12-11 03:18:39 +000010#include "clang/Basic/CodeGenOptions.h"
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000011#include "clang/Basic/Diagnostic.h"
Dan Gohmanfec0ff82011-07-05 22:02:36 +000012#include "clang/Basic/LangOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000013#include "clang/Basic/TargetOptions.h"
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Kostya Serebryanyce2c7262013-12-27 08:11:08 +000015#include "clang/Frontend/Utils.h"
Saleem Abdulrasool888e2892017-01-05 16:02:32 +000016#include "clang/Lex/HeaderSearchOptions.h"
Hal Finkel1a328f52016-12-15 02:19:17 +000017#include "llvm/ADT/SmallSet.h"
Eric Christopher583a1f72015-09-26 01:25:08 +000018#include "llvm/ADT/StringExtras.h"
Saleem Abdulrasool62849c62014-05-08 02:28:32 +000019#include "llvm/ADT/StringSwitch.h"
Steven Wu27fb5222016-05-11 16:26:03 +000020#include "llvm/ADT/Triple.h"
Chandler Carruth418bd1a2015-01-15 02:16:55 +000021#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthaab5ec02015-01-31 11:18:46 +000022#include "llvm/Analysis/TargetTransformInfo.h"
Teresa Johnsonffc4e242016-11-11 05:35:12 +000023#include "llvm/Bitcode/BitcodeReader.h"
Chandler Carruth50f9e892016-12-23 20:44:01 +000024#include "llvm/Bitcode/BitcodeWriter.h"
25#include "llvm/Bitcode/BitcodeWriterPass.h"
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000026#include "llvm/CodeGen/RegAllocRegistry.h"
27#include "llvm/CodeGen/SchedulerRegistry.h"
David Blaikie7a4f7f52018-01-09 22:03:47 +000028#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000029#include "llvm/IR/DataLayout.h"
Chandler Carruth0a50c492014-01-12 11:11:50 +000030#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth9828e692015-02-13 09:57:03 +000031#include "llvm/IR/LegacyPassManager.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000032#include "llvm/IR/Module.h"
Chandler Carruth50f9e892016-12-23 20:44:01 +000033#include "llvm/IR/ModuleSummaryIndex.h"
Chandler Carruthca884742014-01-13 09:26:48 +000034#include "llvm/IR/Verifier.h"
Teresa Johnson9e3f4742016-08-12 18:12:08 +000035#include "llvm/LTO/LTOBackend.h"
Saleem Abdulrasool888e2892017-01-05 16:02:32 +000036#include "llvm/MC/MCAsmInfo.h"
Evan Chengeeb486d2011-06-29 01:14:32 +000037#include "llvm/MC/SubtargetFeature.h"
Chandler Carruth50f9e892016-12-23 20:44:01 +000038#include "llvm/Passes/PassBuilder.h"
Philip Pfaffee3f105c2019-02-02 23:19:32 +000039#include "llvm/Passes/PassPlugin.h"
Taewook Ohd4c50f72019-08-14 07:11:09 +000040#include "llvm/Passes/StandardInstrumentations.h"
David Blaikie9941da42018-11-17 18:04:13 +000041#include "llvm/Support/BuryPointer.h"
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000042#include "llvm/Support/CommandLine.h"
Teresa Johnson9e3f4742016-08-12 18:12:08 +000043#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000044#include "llvm/Support/PrettyStackTrace.h"
Evan Cheng494eb062011-08-24 18:09:14 +000045#include "llvm/Support/TargetRegistry.h"
Anton Afanasyevd880de22019-03-30 08:42:48 +000046#include "llvm/Support/TimeProfiler.h"
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000047#include "llvm/Support/Timer.h"
48#include "llvm/Support/raw_ostream.h"
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000049#include "llvm/Target/TargetMachine.h"
50#include "llvm/Target/TargetOptions.h"
Gor Nishanov97e3b6d2016-10-03 22:44:48 +000051#include "llvm/Transforms/Coroutines.h"
Rafael Espindola56a7dab02011-08-02 21:51:02 +000052#include "llvm/Transforms/IPO.h"
Chandler Carruthb72c19f2016-08-17 03:09:11 +000053#include "llvm/Transforms/IPO/AlwaysInliner.h"
Rafael Espindola56a7dab02011-08-02 21:51:02 +000054#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Tim Shen50fedec2017-06-01 23:27:51 +000055#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
David Blaikied2a572202018-04-24 00:59:22 +000056#include "llvm/Transforms/InstCombine/InstCombine.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000057#include "llvm/Transforms/Instrumentation.h"
Leonard Chan436fb2b2019-02-13 22:22:48 +000058#include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
Chandler Carruthafce4492017-11-14 01:47:24 +000059#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
David Blaikie4e1ae832018-03-23 22:16:59 +000060#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
Leonard Chan0cdd3b12019-05-14 21:17:21 +000061#include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
Rong Xu4059e142019-04-25 17:52:43 +000062#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
Philip Pfaffe685c76d2019-01-16 09:28:01 +000063#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
Leonard Chan007f6742019-07-25 20:53:15 +000064#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
Philip Pfaffe685c76d2019-01-16 09:28:01 +000065#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
Michael Gottesman90cae772013-01-28 01:36:00 +000066#include "llvm/Transforms/ObjCARC.h"
Rafael Espindola56a7dab02011-08-02 21:51:02 +000067#include "llvm/Transforms/Scalar.h"
Chandler Carruth4ddaadc2016-03-11 09:02:43 +000068#include "llvm/Transforms/Scalar/GVN.h"
David Blaikiec133b1e2018-03-28 17:45:10 +000069#include "llvm/Transforms/Utils.h"
Teresa Johnson6ed79132019-01-04 19:05:01 +000070#include "llvm/Transforms/Utils/CanonicalizeAliases.h"
Leonard Chanb2065132019-06-20 19:35:25 +000071#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
Tim Shen66470692017-06-29 23:08:38 +000072#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Saleem Abdulrasool76a4b952015-01-09 05:10:20 +000073#include "llvm/Transforms/Utils/SymbolRewriter.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000074#include <memory>
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000075using namespace clang;
76using namespace llvm;
77
serge_sans_paille24ab9b52019-06-08 17:37:47 +020078#define HANDLE_EXTENSION(Ext) \
79 llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
80#include "llvm/Support/Extension.def"
81
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000082namespace {
83
Davide Italiano945de432017-02-13 16:07:05 +000084// Default filename used for profile generation.
85static constexpr StringLiteral DefaultProfileGenName = "default_%m.profraw";
86
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000087class EmitAssemblyHelper {
David Blaikie9c902b52011-09-25 23:23:43 +000088 DiagnosticsEngine &Diags;
Saleem Abdulrasool888e2892017-01-05 16:02:32 +000089 const HeaderSearchOptions &HSOpts;
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000090 const CodeGenOptions &CodeGenOpts;
Nick Lewycky432add52011-12-02 22:17:00 +000091 const clang::TargetOptions &TargetOpts;
Dan Gohmanfec0ff82011-07-05 22:02:36 +000092 const LangOptions &LangOpts;
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000093 Module *TheModule;
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000094
95 Timer CodeGenerationTime;
96
Peter Collingbourne03f89072016-07-15 00:55:40 +000097 std::unique_ptr<raw_pwrite_stream> OS;
Daniel Dunbarf976d1b2010-06-07 23:20:08 +000098
Chandler Carruthd294bdb2015-02-01 12:26:23 +000099 TargetIRAnalysis getTargetIRAnalysis() const {
Chandler Carruthaab5ec02015-01-31 11:18:46 +0000100 if (TM)
Chandler Carruthd294bdb2015-02-01 12:26:23 +0000101 return TM->getTargetIRAnalysis();
Chandler Carruthaab5ec02015-01-31 11:18:46 +0000102
Chandler Carruthd294bdb2015-02-01 12:26:23 +0000103 return TargetIRAnalysis();
Chandler Carruthaab5ec02015-01-31 11:18:46 +0000104 }
105
Teresa Johnson9e3f4742016-08-12 18:12:08 +0000106 void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM);
Nadav Rotemec57ab32012-10-24 00:53:38 +0000107
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000108 /// Generates the TargetMachine.
Peter Collingbourne03f89072016-07-15 00:55:40 +0000109 /// Leaves TM unchanged if it is unable to create the target machine.
Nadav Rotemdc06b2d2012-10-24 03:52:31 +0000110 /// Some of our clang tests specify triples which are not built
111 /// into clang. This is okay because these tests check the generated
112 /// IR, and they require DataLayout which depends on the triple.
113 /// In this case, we allow this method to fail and not report an error.
114 /// When MustCreateTM is used, we print an error if we are unable to load
115 /// the requested target.
Peter Collingbourne03f89072016-07-15 00:55:40 +0000116 void CreateTargetMachine(bool MustCreateTM);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000117
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000118 /// Add passes necessary to emit assembly or LLVM IR.
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000119 ///
120 /// \return True on success.
Peter Collingbourne03f89072016-07-15 00:55:40 +0000121 bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
Peter Collingbourne91d02842018-05-22 18:52:37 +0000122 raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS);
123
124 std::unique_ptr<llvm::ToolOutputFile> openOutputFile(StringRef Path) {
125 std::error_code EC;
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +0000126 auto F = std::make_unique<llvm::ToolOutputFile>(Path, EC,
Fangrui Songd9b948b2019-08-05 05:43:48 +0000127 llvm::sys::fs::OF_None);
Peter Collingbourne91d02842018-05-22 18:52:37 +0000128 if (EC) {
129 Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message();
130 F.reset();
131 }
132 return F;
133 }
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000134
135public:
Saleem Abdulrasool888e2892017-01-05 16:02:32 +0000136 EmitAssemblyHelper(DiagnosticsEngine &_Diags,
137 const HeaderSearchOptions &HeaderSearchOpts,
138 const CodeGenOptions &CGOpts,
Nick Lewycky432add52011-12-02 22:17:00 +0000139 const clang::TargetOptions &TOpts,
Teresa Johnson4b4f4b92016-01-08 17:04:29 +0000140 const LangOptions &LOpts, Module *M)
Saleem Abdulrasool888e2892017-01-05 16:02:32 +0000141 : Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
142 TargetOpts(TOpts), LangOpts(LOpts), TheModule(M),
143 CodeGenerationTime("codegen", "Code Generation Time") {}
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000144
145 ~EmitAssemblyHelper() {
Alp Tokerf4e22382013-12-20 20:26:53 +0000146 if (CodeGenOpts.DisableFree)
David Blaikiea97eaa12014-08-29 16:53:14 +0000147 BuryPointer(std::move(TM));
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000148 }
149
Ahmed Charlesb8984322014-03-07 20:03:18 +0000150 std::unique_ptr<TargetMachine> TM;
Alp Tokerf4e22382013-12-20 20:26:53 +0000151
Peter Collingbourne03f89072016-07-15 00:55:40 +0000152 void EmitAssembly(BackendAction Action,
153 std::unique_ptr<raw_pwrite_stream> OS);
Chandler Carruth50f9e892016-12-23 20:44:01 +0000154
155 void EmitAssemblyWithNewPassManager(BackendAction Action,
156 std::unique_ptr<raw_pwrite_stream> OS);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000157};
158
Alexey Samsonovc6515b62012-12-28 09:31:34 +0000159// We need this wrapper to access LangOpts and CGOpts from extension functions
160// that we add to the PassManagerBuilder.
Alexey Samsonov0e96bec2012-11-29 22:36:21 +0000161class PassManagerBuilderWrapper : public PassManagerBuilder {
162public:
Evgeniy Stepanovdf217a22017-04-24 19:34:12 +0000163 PassManagerBuilderWrapper(const Triple &TargetTriple,
164 const CodeGenOptions &CGOpts,
Alexey Samsonov9ab73622012-12-03 19:12:58 +0000165 const LangOptions &LangOpts)
Evgeniy Stepanovdf217a22017-04-24 19:34:12 +0000166 : PassManagerBuilder(), TargetTriple(TargetTriple), CGOpts(CGOpts),
167 LangOpts(LangOpts) {}
168 const Triple &getTargetTriple() const { return TargetTriple; }
Alexey Samsonov9ab73622012-12-03 19:12:58 +0000169 const CodeGenOptions &getCGOpts() const { return CGOpts; }
Alexey Samsonov0e96bec2012-11-29 22:36:21 +0000170 const LangOptions &getLangOpts() const { return LangOpts; }
Evgeniy Stepanovdf217a22017-04-24 19:34:12 +0000171
Alexey Samsonov0e96bec2012-11-29 22:36:21 +0000172private:
Evgeniy Stepanovdf217a22017-04-24 19:34:12 +0000173 const Triple &TargetTriple;
Alexey Samsonov9ab73622012-12-03 19:12:58 +0000174 const CodeGenOptions &CGOpts;
Alexey Samsonov0e96bec2012-11-29 22:36:21 +0000175 const LangOptions &LangOpts;
176};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000177}
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000178
Dan Gohman5932ce22012-01-17 20:54:51 +0000179static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
180 if (Builder.OptLevel > 0)
181 PM.add(createObjCARCAPElimPass());
182}
183
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000184static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
185 if (Builder.OptLevel > 0)
186 PM.add(createObjCARCExpandPass());
187}
188
189static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
190 if (Builder.OptLevel > 0)
191 PM.add(createObjCARCOptPass());
192}
193
Diego Novillob56be642014-03-03 20:06:18 +0000194static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
Chandler Carruth8f4f5092015-02-13 09:47:49 +0000195 legacy::PassManagerBase &PM) {
Diego Novillob56be642014-03-03 20:06:18 +0000196 PM.add(createAddDiscriminatorsPass());
197}
198
Nuno Lopesa4255892012-05-22 17:19:45 +0000199static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
Vitaly Buka9d4eb6f2016-06-02 00:24:20 +0000200 legacy::PassManagerBase &PM) {
Chandler Carruth00a301d2017-11-14 01:30:04 +0000201 PM.add(createBoundsCheckingLegacyPass());
Nuno Lopesa4255892012-05-22 17:19:45 +0000202}
203
Leonard Chan007f6742019-07-25 20:53:15 +0000204static SanitizerCoverageOptions
205getSancovOptsFromCGOpts(const CodeGenOptions &CGOpts) {
Alexey Samsonov3f3b3ab2015-05-07 18:31:29 +0000206 SanitizerCoverageOptions Opts;
207 Opts.CoverageType =
208 static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
209 Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
210 Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
211 Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
Kostya Serebryany3b419712016-08-30 01:27:03 +0000212 Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
213 Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
Alexey Samsonov3f3b3ab2015-05-07 18:31:29 +0000214 Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
Kostya Serebryanyd4590c72016-02-17 21:34:43 +0000215 Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
Kostya Serebryany60cdd612016-09-14 01:39:49 +0000216 Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
Kostya Serebryany50fb6182017-05-05 23:28:18 +0000217 Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune;
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000218 Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters;
Kostya Serebryany61457762017-07-28 00:10:10 +0000219 Opts.PCTable = CGOpts.SanitizeCoveragePCTable;
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000220 Opts.StackDepth = CGOpts.SanitizeCoverageStackDepth;
Leonard Chan007f6742019-07-25 20:53:15 +0000221 return Opts;
222}
223
224static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
225 legacy::PassManagerBase &PM) {
226 const PassManagerBuilderWrapper &BuilderWrapper =
227 static_cast<const PassManagerBuilderWrapper &>(Builder);
228 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
229 auto Opts = getSancovOptsFromCGOpts(CGOpts);
230 PM.add(createModuleSanitizerCoverageLegacyPassPass(Opts));
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000231}
232
Evgeniy Stepanovdf217a22017-04-24 19:34:12 +0000233// Check if ASan should use GC-friendly instrumentation for globals.
234// First of all, there is no point if -fdata-sections is off (expect for MachO,
235// where this is not a factor). Also, on ELF this feature requires an assembler
236// extension that only works with -integrated-as at the moment.
237static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) {
Evgeniy Stepanovd991cdd2017-05-09 21:57:43 +0000238 if (!CGOpts.SanitizeAddressGlobalsDeadStripping)
239 return false;
Evgeniy Stepanovdf217a22017-04-24 19:34:12 +0000240 switch (T.getObjectFormat()) {
241 case Triple::MachO:
Evgeniy Stepanovdf217a22017-04-24 19:34:12 +0000242 case Triple::COFF:
Evgeniy Stepanovc7b90942017-04-26 00:51:06 +0000243 return true;
Evgeniy Stepanovdf217a22017-04-24 19:34:12 +0000244 case Triple::ELF:
245 return CGOpts.DataSections && !CGOpts.DisableIntegratedAS;
Hubert Tong2711e162019-07-19 08:46:18 +0000246 case Triple::XCOFF:
247 llvm::report_fatal_error("ASan not implemented for XCOFF.");
248 case Triple::Wasm:
249 case Triple::UnknownObjectFormat:
250 break;
Evgeniy Stepanovdf217a22017-04-24 19:34:12 +0000251 }
Hubert Tong2711e162019-07-19 08:46:18 +0000252 return false;
Evgeniy Stepanovdf217a22017-04-24 19:34:12 +0000253}
254
Alexey Samsonov0e96bec2012-11-29 22:36:21 +0000255static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
Chandler Carruth8f4f5092015-02-13 09:47:49 +0000256 legacy::PassManagerBase &PM) {
Yury Gribov5bfeca12015-11-11 10:45:48 +0000257 const PassManagerBuilderWrapper &BuilderWrapper =
258 static_cast<const PassManagerBuilderWrapper&>(Builder);
Evgeniy Stepanovdf217a22017-04-24 19:34:12 +0000259 const Triple &T = BuilderWrapper.getTargetTriple();
Yury Gribov5bfeca12015-11-11 10:45:48 +0000260 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
261 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address);
Vitaly Buka9d4eb6f2016-06-02 00:24:20 +0000262 bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope;
Vitaly Buka8076c572018-12-05 01:44:31 +0000263 bool UseOdrIndicator = CGOpts.SanitizeAddressUseOdrIndicator;
Evgeniy Stepanovdf217a22017-04-24 19:34:12 +0000264 bool UseGlobalsGC = asanUseGlobalsGC(T, CGOpts);
Vitaly Buka9d4eb6f2016-06-02 00:24:20 +0000265 PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover,
266 UseAfterScope));
Leonard Chan436fb2b2019-02-13 22:22:48 +0000267 PM.add(createModuleAddressSanitizerLegacyPassPass(
268 /*CompileKernel*/ false, Recover, UseGlobalsGC, UseOdrIndicator));
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +0000269}
270
271static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
272 legacy::PassManagerBase &PM) {
Vitaly Buka9d4eb6f2016-06-02 00:24:20 +0000273 PM.add(createAddressSanitizerFunctionPass(
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +0000274 /*CompileKernel*/ true, /*Recover*/ true, /*UseAfterScope*/ false));
Leonard Chan436fb2b2019-02-13 22:22:48 +0000275 PM.add(createModuleAddressSanitizerLegacyPassPass(
Vitaly Buka8076c572018-12-05 01:44:31 +0000276 /*CompileKernel*/ true, /*Recover*/ true, /*UseGlobalsGC*/ true,
277 /*UseOdrIndicator*/ false));
Kostya Serebryany8855ff62011-11-16 17:34:26 +0000278}
279
Evgeniy Stepanov12817e52017-12-09 01:32:07 +0000280static void addHWAddressSanitizerPasses(const PassManagerBuilder &Builder,
281 legacy::PassManagerBase &PM) {
Evgeniy Stepanov3fd1b1a2017-12-20 19:05:44 +0000282 const PassManagerBuilderWrapper &BuilderWrapper =
283 static_cast<const PassManagerBuilderWrapper &>(Builder);
284 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
285 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
Leonard Chan0cdd3b12019-05-14 21:17:21 +0000286 PM.add(
287 createHWAddressSanitizerLegacyPassPass(/*CompileKernel*/ false, Recover));
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +0000288}
289
290static void addKernelHWAddressSanitizerPasses(const PassManagerBuilder &Builder,
291 legacy::PassManagerBase &PM) {
Leonard Chan0cdd3b12019-05-14 21:17:21 +0000292 PM.add(createHWAddressSanitizerLegacyPassPass(
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +0000293 /*CompileKernel*/ true, /*Recover*/ true));
Evgeniy Stepanov12817e52017-12-09 01:32:07 +0000294}
295
Alexander Potapenkod49c32c2018-09-07 09:21:09 +0000296static void addGeneralOptsForMemorySanitizer(const PassManagerBuilder &Builder,
297 legacy::PassManagerBase &PM,
298 bool CompileKernel) {
Evgeniy Stepanovad8ab3d2012-12-24 08:42:34 +0000299 const PassManagerBuilderWrapper &BuilderWrapper =
300 static_cast<const PassManagerBuilderWrapper&>(Builder);
301 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
Evgeniy Stepanov5b5370a2016-11-07 21:02:11 +0000302 int TrackOrigins = CGOpts.SanitizeMemoryTrackOrigins;
303 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Memory);
Philip Pfaffe0ee6a932019-02-04 21:02:49 +0000304 PM.add(createMemorySanitizerLegacyPassPass(
305 MemorySanitizerOptions{TrackOrigins, Recover, CompileKernel}));
Evgeniy Stepanov10284672013-01-31 09:53:29 +0000306
307 // MemorySanitizer inserts complex instrumentation that mostly follows
308 // the logic of the original code, but operates on "shadow" values.
309 // It can benefit from re-running some general purpose optimization passes.
310 if (Builder.OptLevel > 0) {
311 PM.add(createEarlyCSEPass());
312 PM.add(createReassociatePass());
313 PM.add(createLICMPass());
314 PM.add(createGVNPass());
315 PM.add(createInstructionCombiningPass());
316 PM.add(createDeadStoreEliminationPass());
317 }
Evgeniy Stepanovaea92e52012-12-03 13:20:43 +0000318}
319
Alexander Potapenkod49c32c2018-09-07 09:21:09 +0000320static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
321 legacy::PassManagerBase &PM) {
322 addGeneralOptsForMemorySanitizer(Builder, PM, /*CompileKernel*/ false);
323}
324
325static void addKernelMemorySanitizerPass(const PassManagerBuilder &Builder,
326 legacy::PassManagerBase &PM) {
327 addGeneralOptsForMemorySanitizer(Builder, PM, /*CompileKernel*/ true);
328}
329
Kostya Serebryany28a7a112012-03-01 22:27:08 +0000330static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
Chandler Carruth8f4f5092015-02-13 09:47:49 +0000331 legacy::PassManagerBase &PM) {
Philip Pfaffe685c76d2019-01-16 09:28:01 +0000332 PM.add(createThreadSanitizerLegacyPassPass());
Kostya Serebryany28a7a112012-03-01 22:27:08 +0000333}
334
Peter Collingbournec3772752013-08-07 22:47:34 +0000335static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
Chandler Carruth8f4f5092015-02-13 09:47:49 +0000336 legacy::PassManagerBase &PM) {
Peter Collingbourne276be3c2013-08-14 18:54:18 +0000337 const PassManagerBuilderWrapper &BuilderWrapper =
338 static_cast<const PassManagerBuilderWrapper&>(Builder);
Alexey Samsonovae5804f2014-10-15 20:22:54 +0000339 const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
Alexey Samsonova511cdd2015-02-04 17:40:08 +0000340 PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
Peter Collingbournec3772752013-08-07 22:47:34 +0000341}
342
Chandler Carruth57bb7c72015-01-24 02:25:21 +0000343static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
344 const CodeGenOptions &CodeGenOpts) {
345 TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
Michael Zolotukhinc888dd02015-03-17 20:03:11 +0000346
347 switch (CodeGenOpts.getVecLib()) {
348 case CodeGenOptions::Accelerate:
349 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
350 break;
Nemanja Ivanovic6321c682019-06-05 01:57:57 +0000351 case CodeGenOptions::MASSV:
352 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::MASSV);
353 break;
Matt Masten6731dea2016-07-29 16:44:24 +0000354 case CodeGenOptions::SVML:
355 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML);
356 break;
Michael Zolotukhinc888dd02015-03-17 20:03:11 +0000357 default:
358 break;
359 }
Chandler Carruth57bb7c72015-01-24 02:25:21 +0000360 return TLII;
Rafael Espindolab8a71c52014-08-21 17:58:42 +0000361}
362
Saleem Abdulrasool76a4b952015-01-09 05:10:20 +0000363static void addSymbolRewriterPass(const CodeGenOptions &Opts,
Chandler Carruth8f4f5092015-02-13 09:47:49 +0000364 legacy::PassManager *MPM) {
Saleem Abdulrasool76a4b952015-01-09 05:10:20 +0000365 llvm::SymbolRewriter::RewriteDescriptorList DL;
366
367 llvm::SymbolRewriter::RewriteMapParser MapParser;
368 for (const auto &MapFile : Opts.RewriteMapFiles)
369 MapParser.parse(MapFile, &DL);
370
371 MPM->add(createRewriteSymbolsPass(DL));
372}
373
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000374static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) {
375 switch (CodeGenOpts.OptimizationLevel) {
376 default:
377 llvm_unreachable("Invalid optimization level!");
378 case 0:
379 return CodeGenOpt::None;
380 case 1:
381 return CodeGenOpt::Less;
382 case 2:
383 return CodeGenOpt::Default; // O2/Os/Oz
384 case 3:
385 return CodeGenOpt::Aggressive;
386 }
387}
388
Rafael Espindolaaa0226e2017-08-03 02:16:28 +0000389static Optional<llvm::CodeModel::Model>
390getCodeModel(const CodeGenOptions &CodeGenOpts) {
391 unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
David Greenecc69872018-08-22 11:34:28 +0000392 .Case("tiny", llvm::CodeModel::Tiny)
Rafael Espindolaaa0226e2017-08-03 02:16:28 +0000393 .Case("small", llvm::CodeModel::Small)
394 .Case("kernel", llvm::CodeModel::Kernel)
395 .Case("medium", llvm::CodeModel::Medium)
396 .Case("large", llvm::CodeModel::Large)
397 .Case("default", ~1u)
398 .Default(~0u);
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000399 assert(CodeModel != ~0u && "invalid code model!");
Rafael Espindolaaa0226e2017-08-03 02:16:28 +0000400 if (CodeModel == ~1u)
401 return None;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000402 return static_cast<llvm::CodeModel::Model>(CodeModel);
403}
404
Reid Kleckner1dfede32019-11-13 15:17:46 -0800405static CodeGenFileType getCodeGenFileType(BackendAction Action) {
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000406 if (Action == Backend_EmitObj)
Reid Kleckner1dfede32019-11-13 15:17:46 -0800407 return CGFT_ObjectFile;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000408 else if (Action == Backend_EmitMCNull)
Reid Kleckner1dfede32019-11-13 15:17:46 -0800409 return CGFT_Null;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000410 else {
411 assert(Action == Backend_EmitAssembly && "Invalid action!");
Reid Kleckner1dfede32019-11-13 15:17:46 -0800412 return CGFT_AssemblyFile;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000413 }
414}
415
416static void initTargetOptions(llvm::TargetOptions &Options,
417 const CodeGenOptions &CodeGenOpts,
418 const clang::TargetOptions &TargetOpts,
419 const LangOptions &LangOpts,
420 const HeaderSearchOptions &HSOpts) {
421 Options.ThreadModel =
422 llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
423 .Case("posix", llvm::ThreadModel::POSIX)
424 .Case("single", llvm::ThreadModel::Single);
425
426 // Set float ABI type.
427 assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
428 CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
429 "Invalid Floating Point ABI!");
430 Options.FloatABIType =
431 llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
432 .Case("soft", llvm::FloatABI::Soft)
433 .Case("softfp", llvm::FloatABI::Soft)
434 .Case("hard", llvm::FloatABI::Hard)
435 .Default(llvm::FloatABI::Default);
436
437 // Set FP fusion mode.
438 switch (LangOpts.getDefaultFPContractMode()) {
439 case LangOptions::FPC_Off:
Adam Nemet03af4242017-04-20 17:09:35 +0000440 // Preserve any contraction performed by the front-end. (Strict performs
Raphael Isemannb23ccec2018-12-10 12:37:46 +0000441 // splitting of the muladd intrinsic in the backend.)
Adam Nemet03af4242017-04-20 17:09:35 +0000442 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000443 break;
444 case LangOptions::FPC_On:
445 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
446 break;
447 case LangOptions::FPC_Fast:
448 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
449 break;
450 }
451
452 Options.UseInitArray = CodeGenOpts.UseInitArray;
453 Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
Saleem Abdulrasool54448902017-06-09 00:40:30 +0000454 Options.CompressDebugSections = CodeGenOpts.getCompressDebugSections();
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000455 Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
456
457 // Set EABI version.
Yuka Takahashidc771502017-07-01 07:57:23 +0000458 Options.EABIVersion = TargetOpts.EABIVersion;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000459
460 if (LangOpts.SjLjExceptions)
461 Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
Martell Malonec950c652017-11-29 07:25:12 +0000462 if (LangOpts.SEHExceptions)
463 Options.ExceptionModel = llvm::ExceptionHandling::WinEH;
464 if (LangOpts.DWARFExceptions)
465 Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI;
Heejin Ahne8b2b882019-09-12 04:01:37 +0000466 if (LangOpts.WasmExceptions)
467 Options.ExceptionModel = llvm::ExceptionHandling::Wasm;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000468
469 Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
470 Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
471 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
472 Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
473 Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
474 Options.FunctionSections = CodeGenOpts.FunctionSections;
475 Options.DataSections = CodeGenOpts.DataSections;
476 Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
477 Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
Chih-Hung Hsiehca552b82018-03-01 22:26:19 +0000478 Options.ExplicitEmulatedTLS = CodeGenOpts.ExplicitEmulatedTLS;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000479 Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
Sean Eveson5110d4f2018-01-08 13:42:26 +0000480 Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;
Peter Collingbourne14b468b2018-07-18 00:27:07 +0000481 Options.EmitAddrsig = CodeGenOpts.Addrsig;
Djordje Todorovic639d36b2019-06-26 09:38:09 +0000482 Options.EnableDebugEntryValues = CodeGenOpts.EnableDebugEntryValues;
David Candler92aa0c22019-10-31 08:55:57 +0000483 Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000484
Aaron Puchertb207bae2019-06-26 21:36:35 +0000485 Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000486 Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
487 Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
488 Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
489 Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
490 Options.MCOptions.MCIncrementalLinkerCompatible =
491 CodeGenOpts.IncrementalLinkerCompatible;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000492 Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
Brian Cain7b953b62019-08-08 19:19:20 +0000493 Options.MCOptions.MCNoWarn = CodeGenOpts.NoWarn;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000494 Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
495 Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
496 Options.MCOptions.ABIName = TargetOpts.ABI;
497 for (const auto &Entry : HSOpts.UserEntries)
498 if (!Entry.IsFramework &&
499 (Entry.Group == frontend::IncludeDirGroup::Quoted ||
500 Entry.Group == frontend::IncludeDirGroup::Angled ||
501 Entry.Group == frontend::IncludeDirGroup::System))
502 Options.MCOptions.IASSearchPaths.push_back(
503 Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
504}
David Blaikie7a4f7f52018-01-09 22:03:47 +0000505static Optional<GCOVOptions> getGCOVOptions(const CodeGenOptions &CodeGenOpts) {
506 if (CodeGenOpts.DisableGCov)
507 return None;
508 if (!CodeGenOpts.EmitGcovArcs && !CodeGenOpts.EmitGcovNotes)
509 return None;
510 // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
511 // LLVM's -default-gcov-version flag is set to something invalid.
512 GCOVOptions Options;
513 Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
514 Options.EmitData = CodeGenOpts.EmitGcovArcs;
515 llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version));
516 Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
517 Options.NoRedZone = CodeGenOpts.DisableRedZone;
518 Options.FunctionNamesInData = !CodeGenOpts.CoverageNoFunctionNamesInData;
Calixte Denizetf4bf6712018-11-17 19:41:39 +0000519 Options.Filter = CodeGenOpts.ProfileFilterFiles;
520 Options.Exclude = CodeGenOpts.ProfileExcludeFiles;
David Blaikie7a4f7f52018-01-09 22:03:47 +0000521 Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody;
522 return Options;
523}
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000524
Rong Xu4059e142019-04-25 17:52:43 +0000525static Optional<InstrProfOptions>
526getInstrProfOptions(const CodeGenOptions &CodeGenOpts,
527 const LangOptions &LangOpts) {
528 if (!CodeGenOpts.hasProfileClangInstr())
529 return None;
530 InstrProfOptions Options;
531 Options.NoRedZone = CodeGenOpts.DisableRedZone;
532 Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
533
534 // TODO: Surface the option to emit atomic profile counter increments at
535 // the driver level.
536 Options.Atomic = LangOpts.Sanitize.has(SanitizerKind::Thread);
537 return Options;
538}
539
Peter Collingbourne03f89072016-07-15 00:55:40 +0000540void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,
Teresa Johnson9e3f4742016-08-12 18:12:08 +0000541 legacy::FunctionPassManager &FPM) {
Chandler Carruth93786da2016-12-23 00:23:01 +0000542 // Handle disabling of all LLVM passes, where we want to preserve the
543 // internal module before any optimization.
Steven Wu546a1962015-07-17 20:09:56 +0000544 if (CodeGenOpts.DisableLLVMPasses)
545 return;
546
Marcin Koscielnicki9a063e72016-11-22 20:03:35 +0000547 // Figure out TargetLibraryInfo. This needs to be added to MPM and FPM
548 // manually (and not via PMBuilder), since some passes (eg. InstrProfiling)
549 // are inserted before PMBuilder ones - they'd get the default-constructed
550 // TLI with an unknown target otherwise.
Teresa Johnsonaff22322015-12-07 19:21:34 +0000551 Triple TargetTriple(TheModule->getTargetTriple());
Marcin Koscielnicki9a063e72016-11-22 20:03:35 +0000552 std::unique_ptr<TargetLibraryInfoImpl> TLII(
553 createTLII(TargetTriple, CodeGenOpts));
Teresa Johnsonaff22322015-12-07 19:21:34 +0000554
Evgeniy Stepanovdf217a22017-04-24 19:34:12 +0000555 PassManagerBuilderWrapper PMBuilder(TargetTriple, CodeGenOpts, LangOpts);
556
Chandler Carruthfcd33142016-12-23 01:24:49 +0000557 // At O0 and O1 we only run the always inliner which is more efficient. At
558 // higher optimization levels we run the normal inliner.
559 if (CodeGenOpts.OptimizationLevel <= 1) {
Mehdi Amini7f873072017-01-06 23:18:09 +0000560 bool InsertLifetimeIntrinsics = (CodeGenOpts.OptimizationLevel != 0 &&
561 !CodeGenOpts.DisableLifetimeMarkers);
Chandler Carruthfcd33142016-12-23 01:24:49 +0000562 PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics);
563 } else {
Dehao Chence39fdd2017-03-21 19:55:46 +0000564 // We do not want to inline hot callsites for SamplePGO module-summary build
565 // because profile annotation will happen again in ThinLTO backend, and we
566 // want the IR of the hot path to match the profile.
Chandler Carruthfcd33142016-12-23 01:24:49 +0000567 PMBuilder.Inliner = createFunctionInliningPass(
Dehao Chence39fdd2017-03-21 19:55:46 +0000568 CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
569 (!CodeGenOpts.SampleProfileFile.empty() &&
Tobias Edler von Koch7609cb82018-06-22 20:23:21 +0000570 CodeGenOpts.PrepareForThinLTO));
Teresa Johnsonaff22322015-12-07 19:21:34 +0000571 }
572
Chandler Carruthfcd33142016-12-23 01:24:49 +0000573 PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
Chris Lattnerecf0ba52011-05-21 23:50:44 +0000574 PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
Nick Lewyckyd3f3e4f2013-06-25 01:49:44 +0000575 PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
576 PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
Andrew Trickb2a84722011-04-05 18:49:32 +0000577
Chris Lattnerecf0ba52011-05-21 23:50:44 +0000578 PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
Alina Sbirleaa9b9ab82019-04-30 21:29:23 +0000579 // Loop interleaving in the loop vectorizer has historically been set to be
580 // enabled when loop unrolling is enabled.
581 PMBuilder.LoopsInterleaved = CodeGenOpts.UnrollLoops;
Nick Lewyckyf04f2372014-10-24 00:49:29 +0000582 PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
Tobias Edler von Koch7609cb82018-06-22 20:23:21 +0000583 PMBuilder.PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
Teresa Johnson8749d8042015-07-06 16:23:00 +0000584 PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
Hal Finkelce0697f2013-11-17 16:03:29 +0000585 PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000586
Marcin Koscielnicki9a063e72016-11-22 20:03:35 +0000587 MPM.add(new TargetLibraryInfoWrapperPass(*TLII));
588
Justin Lebar5a7df9c2016-04-27 19:12:56 +0000589 if (TM)
Stanislav Mekhanoshin61da0672017-01-26 16:49:21 +0000590 TM->adjustPassManager(PMBuilder);
Justin Lebar5a7df9c2016-04-27 19:12:56 +0000591
Dehao Chen7810d4f2017-02-21 20:36:21 +0000592 if (CodeGenOpts.DebugInfoForProfiling ||
593 !CodeGenOpts.SampleProfileFile.empty())
594 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
595 addAddDiscriminatorsPass);
Diego Novillob56be642014-03-03 20:06:18 +0000596
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000597 // In ObjC ARC mode, add the main ARC optimization passes.
598 if (LangOpts.ObjCAutoRefCount) {
599 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
600 addObjCARCExpandPass);
Dan Gohman5932ce22012-01-17 20:54:51 +0000601 PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
602 addObjCARCAPElimPass);
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000603 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
604 addObjCARCOptPass);
605 }
Kostya Serebryany8855ff62011-11-16 17:34:26 +0000606
Richard Smith10ab78e2019-02-23 21:06:26 +0000607 if (LangOpts.Coroutines)
Brian Gesiak91a4b5a2018-04-01 23:55:21 +0000608 addCoroutinePassesToExtensionPoints(PMBuilder);
609
Alexey Samsonovedf99a92014-11-07 22:29:38 +0000610 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
Nuno Lopesa4255892012-05-22 17:19:45 +0000611 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
612 addBoundsCheckingPass);
613 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
614 addBoundsCheckingPass);
615 }
616
Alexey Samsonovdfa908c2015-05-07 22:34:06 +0000617 if (CodeGenOpts.SanitizeCoverageType ||
618 CodeGenOpts.SanitizeCoverageIndirectCalls ||
619 CodeGenOpts.SanitizeCoverageTraceCmp) {
Kostya Serebryany75b4f9e2014-11-11 22:15:07 +0000620 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
621 addSanitizerCoveragePass);
622 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
623 addSanitizerCoveragePass);
624 }
625
Alexey Samsonovedf99a92014-11-07 22:29:38 +0000626 if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
Kostya Serebryany7e247f22012-10-15 14:22:56 +0000627 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
Alexey Samsonov0e96bec2012-11-29 22:36:21 +0000628 addAddressSanitizerPasses);
Kostya Serebryanyd4768572011-11-30 22:20:21 +0000629 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
Alexey Samsonov0e96bec2012-11-29 22:36:21 +0000630 addAddressSanitizerPasses);
Kostya Serebryany8855ff62011-11-16 17:34:26 +0000631 }
Kostya Serebryany28a7a112012-03-01 22:27:08 +0000632
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +0000633 if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
634 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
635 addKernelAddressSanitizerPasses);
636 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
637 addKernelAddressSanitizerPasses);
638 }
639
Evgeniy Stepanov12817e52017-12-09 01:32:07 +0000640 if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
641 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
642 addHWAddressSanitizerPasses);
643 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
644 addHWAddressSanitizerPasses);
645 }
646
Andrey Konovalov1ba9d9c2018-04-13 18:05:21 +0000647 if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
648 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
649 addKernelHWAddressSanitizerPasses);
650 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
651 addKernelHWAddressSanitizerPasses);
652 }
653
Alexey Samsonovedf99a92014-11-07 22:29:38 +0000654 if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
Evgeniy Stepanovaea92e52012-12-03 13:20:43 +0000655 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
656 addMemorySanitizerPass);
657 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
658 addMemorySanitizerPass);
659 }
660
Alexander Potapenkod49c32c2018-09-07 09:21:09 +0000661 if (LangOpts.Sanitize.has(SanitizerKind::KernelMemory)) {
662 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
663 addKernelMemorySanitizerPass);
664 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
665 addKernelMemorySanitizerPass);
666 }
667
Alexey Samsonovedf99a92014-11-07 22:29:38 +0000668 if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
Kostya Serebryanyd18cb502012-03-23 23:25:23 +0000669 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
Kostya Serebryany28a7a112012-03-01 22:27:08 +0000670 addThreadSanitizerPass);
671 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
672 addThreadSanitizerPass);
673 }
674
Alexey Samsonovedf99a92014-11-07 22:29:38 +0000675 if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
Peter Collingbournec3772752013-08-07 22:47:34 +0000676 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
677 addDataFlowSanitizerPass);
678 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
679 addDataFlowSanitizerPass);
680 }
681
Chris Lattner5c123672011-05-21 20:40:11 +0000682 // Set up the per-function pass manager.
Marcin Koscielnicki9a063e72016-11-22 20:03:35 +0000683 FPM.add(new TargetLibraryInfoWrapperPass(*TLII));
Chris Lattner5c123672011-05-21 20:40:11 +0000684 if (CodeGenOpts.VerifyModule)
Peter Collingbourne03f89072016-07-15 00:55:40 +0000685 FPM.add(createVerifierPass());
Andrew Trickb2a84722011-04-05 18:49:32 +0000686
Chris Lattner5c123672011-05-21 20:40:11 +0000687 // Set up the per-module pass manager.
Saleem Abdulrasool76a4b952015-01-09 05:10:20 +0000688 if (!CodeGenOpts.RewriteMapFiles.empty())
Peter Collingbourne03f89072016-07-15 00:55:40 +0000689 addSymbolRewriterPass(CodeGenOpts, &MPM);
Chris Lattnerd98cec52011-02-18 22:20:38 +0000690
David Blaikie7a4f7f52018-01-09 22:03:47 +0000691 if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts)) {
692 MPM.add(createGCOVProfilerPass(*Options));
Benjamin Kramer8c305922016-02-02 11:06:51 +0000693 if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
Peter Collingbourne03f89072016-07-15 00:55:40 +0000694 MPM.add(createStripSymbolsPass(true));
Nick Lewycky207bce32011-04-21 23:44:07 +0000695 }
Nadav Rotemdc06b2d2012-10-24 03:52:31 +0000696
Rong Xu4059e142019-04-25 17:52:43 +0000697 if (Optional<InstrProfOptions> Options =
698 getInstrProfOptions(CodeGenOpts, LangOpts))
699 MPM.add(createInstrProfilingLegacyPass(*Options, false));
Vedant Kumaree6c2332018-08-16 22:24:47 +0000700
Rong Xua4a09b22019-03-04 20:21:31 +0000701 bool hasIRInstr = false;
Rong Xu522b5cb2016-02-29 18:54:59 +0000702 if (CodeGenOpts.hasProfileIRInstr()) {
Xinliang David Lib65f8ae2016-07-23 04:28:59 +0000703 PMBuilder.EnablePGOInstrGen = true;
Rong Xua4a09b22019-03-04 20:21:31 +0000704 hasIRInstr = true;
705 }
706 if (CodeGenOpts.hasProfileCSIRInstr()) {
707 assert(!CodeGenOpts.hasProfileCSIRUse() &&
708 "Cannot have both CSProfileUse pass and CSProfileGen pass at the "
709 "same time");
710 assert(!hasIRInstr &&
711 "Cannot have both ProfileGen pass and CSProfileGen pass at the "
712 "same time");
713 PMBuilder.EnablePGOCSInstrGen = true;
714 hasIRInstr = true;
715 }
716 if (hasIRInstr) {
Rong Xu522b5cb2016-02-29 18:54:59 +0000717 if (!CodeGenOpts.InstrProfileOutput.empty())
718 PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
719 else
Davide Italiano945de432017-02-13 16:07:05 +0000720 PMBuilder.PGOInstrGen = DefaultProfileGenName;
Rong Xu522b5cb2016-02-29 18:54:59 +0000721 }
Rong Xua4a09b22019-03-04 20:21:31 +0000722 if (CodeGenOpts.hasProfileIRUse()) {
Rong Xu9c6f1532016-03-02 20:59:36 +0000723 PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
Rong Xua4a09b22019-03-04 20:21:31 +0000724 PMBuilder.EnablePGOCSInstrUse = CodeGenOpts.hasProfileCSIRUse();
725 }
Justin Bogner970ac602014-12-08 19:04:51 +0000726
Dehao Chen5717aff2016-12-14 21:41:04 +0000727 if (!CodeGenOpts.SampleProfileFile.empty())
728 PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile;
Diego Novillod3ef1082015-08-25 15:25:13 +0000729
Peter Collingbourne03f89072016-07-15 00:55:40 +0000730 PMBuilder.populateFunctionPassManager(FPM);
731 PMBuilder.populateModulePassManager(MPM);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000732}
733
David Blaikie6e2ec5f2017-04-19 20:08:21 +0000734static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
Yaxun Liub5e80c32016-04-12 20:22:32 +0000735 SmallVector<const char *, 16> BackendArgs;
736 BackendArgs.push_back("clang"); // Fake program name.
737 if (!CodeGenOpts.DebugPass.empty()) {
738 BackendArgs.push_back("-debug-pass");
739 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
740 }
741 if (!CodeGenOpts.LimitFloatPrecision.empty()) {
742 BackendArgs.push_back("-limit-float-precision");
743 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
744 }
Yaxun Liub5e80c32016-04-12 20:22:32 +0000745 BackendArgs.push_back(nullptr);
746 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
747 BackendArgs.data());
748}
749
Peter Collingbourne03f89072016-07-15 00:55:40 +0000750void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000751 // Create the TargetMachine for generating code.
752 std::string Error;
753 std::string Triple = TheModule->getTargetTriple();
754 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
755 if (!TheTarget) {
Nadav Rotemdc06b2d2012-10-24 03:52:31 +0000756 if (MustCreateTM)
Chad Rosierecafbe62013-03-27 00:14:35 +0000757 Diags.Report(diag::err_fe_unable_to_create_target) << Error;
Peter Collingbourne03f89072016-07-15 00:55:40 +0000758 return;
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000759 }
760
Rafael Espindolaaa0226e2017-08-03 02:16:28 +0000761 Optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
Eric Christopher583a1f72015-09-26 01:25:08 +0000762 std::string FeaturesStr =
763 llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
Rafael Espindolae1d70532018-01-18 00:20:03 +0000764 llvm::Reloc::Model RM = CodeGenOpts.RelocationModel;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000765 CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts);
Evan Chengdd286bc2011-11-16 08:38:55 +0000766
Nick Lewycky432add52011-12-02 22:17:00 +0000767 llvm::TargetOptions Options;
Teresa Johnson5ed6c102017-03-31 02:05:15 +0000768 initTargetOptions(Options, CodeGenOpts, TargetOpts, LangOpts, HSOpts);
Peter Collingbourne03f89072016-07-15 00:55:40 +0000769 TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
770 Options, RM, CM, OptLevel));
Nadav Rotemec57ab32012-10-24 00:53:38 +0000771}
772
Peter Collingbourne03f89072016-07-15 00:55:40 +0000773bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
774 BackendAction Action,
Peter Collingbourne91d02842018-05-22 18:52:37 +0000775 raw_pwrite_stream &OS,
776 raw_pwrite_stream *DwoOS) {
Chad Rosierb1cfc682012-02-29 20:14:59 +0000777 // Add LibraryInfo.
Daniel Dunbaraa437df2012-10-19 20:10:10 +0000778 llvm::Triple TargetTriple(TheModule->getTargetTriple());
Chandler Carruth57bb7c72015-01-24 02:25:21 +0000779 std::unique_ptr<TargetLibraryInfoImpl> TLII(
780 createTLII(TargetTriple, CodeGenOpts));
Peter Collingbourne03f89072016-07-15 00:55:40 +0000781 CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
Chad Rosierb1cfc682012-02-29 20:14:59 +0000782
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000783 // Normal mode, emit a .s or .o file by running the code generator. Note,
784 // this also adds codegenerator level optimization passes.
Reid Kleckner1dfede32019-11-13 15:17:46 -0800785 CodeGenFileType CGFT = getCodeGenFileType(Action);
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000786
787 // Add ObjC ARC final-cleanup optimizations. This is done as part of the
788 // "codegen" passes so that it isn't run multiple times when there is
789 // inlining happening.
Steven Wu1d56be82015-05-02 00:56:15 +0000790 if (CodeGenOpts.OptimizationLevel > 0)
Peter Collingbourne03f89072016-07-15 00:55:40 +0000791 CodeGenPasses.add(createObjCARCContractPass());
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000792
Peter Collingbourne91d02842018-05-22 18:52:37 +0000793 if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT,
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000794 /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
795 Diags.Report(diag::err_fe_unable_to_interface_with_target);
796 return false;
797 }
798
799 return true;
800}
801
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000802void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
Peter Collingbourne03f89072016-07-15 00:55:40 +0000803 std::unique_ptr<raw_pwrite_stream> OS) {
Andrew V. Tischenko8ab2c9c2018-04-23 09:22:30 +0000804 TimeRegion Region(FrontendTimesIsEnabled ? &CodeGenerationTime : nullptr);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000805
David Blaikie6e2ec5f2017-04-19 20:08:21 +0000806 setCommandLineOpts(CodeGenOpts);
Yaxun Liub5e80c32016-04-12 20:22:32 +0000807
Nadav Rotemdc06b2d2012-10-24 03:52:31 +0000808 bool UsesCodeGen = (Action != Backend_EmitNothing &&
809 Action != Backend_EmitBC &&
810 Action != Backend_EmitLL);
Peter Collingbourne03f89072016-07-15 00:55:40 +0000811 CreateTargetMachine(UsesCodeGen);
Alp Tokerf4e22382013-12-20 20:26:53 +0000812
Rafael Espindolab633d202015-06-23 13:59:36 +0000813 if (UsesCodeGen && !TM)
814 return;
815 if (TM)
Mehdi Aminica3cf9e2015-07-24 16:04:29 +0000816 TheModule->setDataLayout(TM->createDataLayout());
Teresa Johnson4b4f4b92016-01-08 17:04:29 +0000817
Peter Collingbourne03f89072016-07-15 00:55:40 +0000818 legacy::PassManager PerModulePasses;
819 PerModulePasses.add(
820 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
821
822 legacy::FunctionPassManager PerFunctionPasses(TheModule);
823 PerFunctionPasses.add(
824 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
825
Teresa Johnson9e3f4742016-08-12 18:12:08 +0000826 CreatePasses(PerModulePasses, PerFunctionPasses);
Peter Collingbourne03f89072016-07-15 00:55:40 +0000827
828 legacy::PassManager CodeGenPasses;
829 CodeGenPasses.add(
830 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
Nadav Rotemec57ab32012-10-24 00:53:38 +0000831
Peter Collingbourne91d02842018-05-22 18:52:37 +0000832 std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
Teresa Johnson488d1dc2017-03-23 19:47:49 +0000833
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000834 switch (Action) {
835 case Backend_EmitNothing:
836 break;
837
838 case Backend_EmitBC:
Eli Friedman53591232018-08-24 19:31:52 +0000839 if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
Teresa Johnson488d1dc2017-03-23 19:47:49 +0000840 if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
Peter Collingbourne91d02842018-05-22 18:52:37 +0000841 ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
842 if (!ThinLinkOS)
Teresa Johnson488d1dc2017-03-23 19:47:49 +0000843 return;
Teresa Johnson488d1dc2017-03-23 19:47:49 +0000844 }
Teresa Johnson84cecfc2019-01-11 18:32:07 +0000845 TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
846 CodeGenOpts.EnableSplitLTOUnit);
Peter Collingbourne91d02842018-05-22 18:52:37 +0000847 PerModulePasses.add(createWriteThinLTOBitcodePass(
848 *OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
Tobias Edler von Koch7609cb82018-06-22 20:23:21 +0000849 } else {
850 // Emit a module summary by default for Regular LTO except for ld64
851 // targets
852 bool EmitLTOSummary =
853 (CodeGenOpts.PrepareForLTO &&
Eli Friedman53591232018-08-24 19:31:52 +0000854 !CodeGenOpts.DisableLLVMPasses &&
Tobias Edler von Koch7609cb82018-06-22 20:23:21 +0000855 llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
856 llvm::Triple::Apple);
Teresa Johnson84cecfc2019-01-11 18:32:07 +0000857 if (EmitLTOSummary) {
858 if (!TheModule->getModuleFlag("ThinLTO"))
859 TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
860 TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
Teresa Johnson604f8022019-07-19 23:02:58 +0000861 uint32_t(1));
Teresa Johnson84cecfc2019-01-11 18:32:07 +0000862 }
Tobias Edler von Koch7609cb82018-06-22 20:23:21 +0000863
Teresa Johnson84cecfc2019-01-11 18:32:07 +0000864 PerModulePasses.add(createBitcodeWriterPass(
865 *OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
Tobias Edler von Koch7609cb82018-06-22 20:23:21 +0000866 }
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000867 break;
868
869 case Backend_EmitLL:
Peter Collingbourne03f89072016-07-15 00:55:40 +0000870 PerModulePasses.add(
Duncan P. N. Exon Smithbb9cadf2015-04-15 02:45:28 +0000871 createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000872 break;
873
874 default:
Aaron Puchertb207bae2019-06-26 21:36:35 +0000875 if (!CodeGenOpts.SplitDwarfOutput.empty()) {
Aaron Puchert922759a2019-06-15 14:07:43 +0000876 DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
Peter Collingbourne91d02842018-05-22 18:52:37 +0000877 if (!DwoOS)
878 return;
879 }
880 if (!AddEmitPasses(CodeGenPasses, Action, *OS,
881 DwoOS ? &DwoOS->os() : nullptr))
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000882 return;
883 }
884
Andrew Trick15e36e82011-04-05 18:56:55 +0000885 // Before executing passes, print the final values of the LLVM options.
886 cl::PrintOptionValues();
887
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000888 // Run passes. For now we do all passes at once, but eventually we
889 // would like to have the option of streaming code generation.
890
Peter Collingbourne03f89072016-07-15 00:55:40 +0000891 {
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000892 PrettyStackTraceString CrashInfo("Per-function optimization");
Russell Gallopdf494f72019-12-11 11:49:42 +0000893 llvm::TimeTraceScope TimeScope("PerFunctionPasses");
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000894
Peter Collingbourne03f89072016-07-15 00:55:40 +0000895 PerFunctionPasses.doInitialization();
Yaron Keren10d6d162015-06-05 09:40:53 +0000896 for (Function &F : *TheModule)
897 if (!F.isDeclaration())
Peter Collingbourne03f89072016-07-15 00:55:40 +0000898 PerFunctionPasses.run(F);
899 PerFunctionPasses.doFinalization();
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000900 }
901
Peter Collingbourne03f89072016-07-15 00:55:40 +0000902 {
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000903 PrettyStackTraceString CrashInfo("Per-module optimization passes");
Russell Gallopdf494f72019-12-11 11:49:42 +0000904 llvm::TimeTraceScope TimeScope("PerModulePasses");
Peter Collingbourne03f89072016-07-15 00:55:40 +0000905 PerModulePasses.run(*TheModule);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000906 }
907
Peter Collingbourne03f89072016-07-15 00:55:40 +0000908 {
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000909 PrettyStackTraceString CrashInfo("Code generation");
Russell Gallopdf494f72019-12-11 11:49:42 +0000910 llvm::TimeTraceScope TimeScope("CodeGenPasses");
Peter Collingbourne03f89072016-07-15 00:55:40 +0000911 CodeGenPasses.run(*TheModule);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000912 }
Peter Collingbourne91d02842018-05-22 18:52:37 +0000913
914 if (ThinLinkOS)
915 ThinLinkOS->keep();
916 if (DwoOS)
917 DwoOS->keep();
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000918}
919
Chandler Carruth50f9e892016-12-23 20:44:01 +0000920static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
921 switch (Opts.OptimizationLevel) {
922 default:
923 llvm_unreachable("Invalid optimization level!");
924
925 case 1:
926 return PassBuilder::O1;
927
928 case 2:
929 switch (Opts.OptimizeSize) {
930 default:
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000931 llvm_unreachable("Invalid optimization level for size!");
Chandler Carruth50f9e892016-12-23 20:44:01 +0000932
933 case 0:
934 return PassBuilder::O2;
935
936 case 1:
937 return PassBuilder::Os;
938
939 case 2:
940 return PassBuilder::Oz;
941 }
942
943 case 3:
944 return PassBuilder::O3;
945 }
946}
947
Benjamin Kramerba2ea932019-03-28 17:18:42 +0000948static void addSanitizersAtO0(ModulePassManager &MPM,
949 const Triple &TargetTriple,
950 const LangOptions &LangOpts,
951 const CodeGenOptions &CodeGenOpts) {
Petr Hosek366cda02019-05-09 06:09:35 +0000952 auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
Leonard Chan436fb2b2019-02-13 22:22:48 +0000953 MPM.addPass(RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
Petr Hosek366cda02019-05-09 06:09:35 +0000954 bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
955 MPM.addPass(createModuleToFunctionPassAdaptor(AddressSanitizerPass(
956 CompileKernel, Recover, CodeGenOpts.SanitizeAddressUseAfterScope)));
Leonard Chan436fb2b2019-02-13 22:22:48 +0000957 bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
Petr Hosek366cda02019-05-09 06:09:35 +0000958 MPM.addPass(
959 ModuleAddressSanitizerPass(CompileKernel, Recover, ModuleUseAfterScope,
960 CodeGenOpts.SanitizeAddressUseOdrIndicator));
961 };
962
963 if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
964 ASanPass(SanitizerKind::Address, /*CompileKernel=*/false);
965 }
966
967 if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
968 ASanPass(SanitizerKind::KernelAddress, /*CompileKernel=*/true);
Leonard Chan436fb2b2019-02-13 22:22:48 +0000969 }
Leonard Chan1a240ed2019-02-20 03:50:11 +0000970
971 if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
Vitaly Bukab46dd6e2019-10-11 08:47:03 +0000972 MPM.addPass(MemorySanitizerPass({}));
Leonard Chan1a240ed2019-02-20 03:50:11 +0000973 MPM.addPass(createModuleToFunctionPassAdaptor(MemorySanitizerPass({})));
974 }
975
Petr Hosek366cda02019-05-09 06:09:35 +0000976 if (LangOpts.Sanitize.has(SanitizerKind::KernelMemory)) {
977 MPM.addPass(createModuleToFunctionPassAdaptor(
978 MemorySanitizerPass({0, false, /*Kernel=*/true})));
979 }
980
Leonard Chan1a240ed2019-02-20 03:50:11 +0000981 if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
Vitaly Bukab46dd6e2019-10-11 08:47:03 +0000982 MPM.addPass(ThreadSanitizerPass());
Leonard Chan1a240ed2019-02-20 03:50:11 +0000983 MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
984 }
Leonard Chan436fb2b2019-02-13 22:22:48 +0000985}
986
Chandler Carruth50f9e892016-12-23 20:44:01 +0000987/// A clean version of `EmitAssembly` that uses the new pass manager.
988///
989/// Not all features are currently supported in this system, but where
990/// necessary it falls back to the legacy pass manager to at least provide
991/// basic functionality.
992///
993/// This API is planned to have its functionality finished and then to replace
994/// `EmitAssembly` at some point in the future when the default switches.
995void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
996 BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) {
Andrew V. Tischenko8ab2c9c2018-04-23 09:22:30 +0000997 TimeRegion Region(FrontendTimesIsEnabled ? &CodeGenerationTime : nullptr);
David Blaikie6e2ec5f2017-04-19 20:08:21 +0000998 setCommandLineOpts(CodeGenOpts);
Chandler Carruth50f9e892016-12-23 20:44:01 +0000999
Petr Hosek5f2e10e2019-05-06 23:24:17 +00001000 bool RequiresCodeGen = (Action != Backend_EmitNothing &&
1001 Action != Backend_EmitBC &&
1002 Action != Backend_EmitLL);
1003 CreateTargetMachine(RequiresCodeGen);
1004
1005 if (RequiresCodeGen && !TM)
Chandler Carruth50f9e892016-12-23 20:44:01 +00001006 return;
Petr Hosek5f2e10e2019-05-06 23:24:17 +00001007 if (TM)
1008 TheModule->setDataLayout(TM->createDataLayout());
Chandler Carruth50f9e892016-12-23 20:44:01 +00001009
Dehao Chenc76a27e2017-07-27 15:29:53 +00001010 Optional<PGOOptions> PGOOpt;
Davide Italiano945de432017-02-13 16:07:05 +00001011
Dehao Chenc76a27e2017-07-27 15:29:53 +00001012 if (CodeGenOpts.hasProfileIRInstr())
1013 // -fprofile-generate.
1014 PGOOpt = PGOOptions(CodeGenOpts.InstrProfileOutput.empty()
1015 ? DefaultProfileGenName
1016 : CodeGenOpts.InstrProfileOutput,
Rong Xua4a09b22019-03-04 20:21:31 +00001017 "", "", PGOOptions::IRInstr, PGOOptions::NoCSAction,
Richard Smith8654ae52018-10-10 23:13:35 +00001018 CodeGenOpts.DebugInfoForProfiling);
Rong Xua4a09b22019-03-04 20:21:31 +00001019 else if (CodeGenOpts.hasProfileIRUse()) {
Dehao Chenc76a27e2017-07-27 15:29:53 +00001020 // -fprofile-use.
Rong Xua4a09b22019-03-04 20:21:31 +00001021 auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
1022 : PGOOptions::NoCSAction;
1023 PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",
1024 CodeGenOpts.ProfileRemappingFile, PGOOptions::IRUse,
1025 CSAction, CodeGenOpts.DebugInfoForProfiling);
1026 } else if (!CodeGenOpts.SampleProfileFile.empty())
Dehao Chenc76a27e2017-07-27 15:29:53 +00001027 // -fprofile-sample-use
Rong Xua4a09b22019-03-04 20:21:31 +00001028 PGOOpt =
1029 PGOOptions(CodeGenOpts.SampleProfileFile, "",
1030 CodeGenOpts.ProfileRemappingFile, PGOOptions::SampleUse,
1031 PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling);
Dehao Chenc76a27e2017-07-27 15:29:53 +00001032 else if (CodeGenOpts.DebugInfoForProfiling)
1033 // -fdebug-info-for-profiling
Rong Xua4a09b22019-03-04 20:21:31 +00001034 PGOOpt = PGOOptions("", "", "", PGOOptions::NoAction,
1035 PGOOptions::NoCSAction, true);
1036
1037 // Check to see if we want to generate a CS profile.
1038 if (CodeGenOpts.hasProfileCSIRInstr()) {
1039 assert(!CodeGenOpts.hasProfileCSIRUse() &&
1040 "Cannot have both CSProfileUse pass and CSProfileGen pass at "
1041 "the same time");
1042 if (PGOOpt.hasValue()) {
1043 assert(PGOOpt->Action != PGOOptions::IRInstr &&
1044 PGOOpt->Action != PGOOptions::SampleUse &&
1045 "Cannot run CSProfileGen pass with ProfileGen or SampleUse "
1046 " pass");
1047 PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty()
1048 ? DefaultProfileGenName
1049 : CodeGenOpts.InstrProfileOutput;
1050 PGOOpt->CSAction = PGOOptions::CSIRInstr;
1051 } else
1052 PGOOpt = PGOOptions("",
1053 CodeGenOpts.InstrProfileOutput.empty()
1054 ? DefaultProfileGenName
1055 : CodeGenOpts.InstrProfileOutput,
1056 "", PGOOptions::NoAction, PGOOptions::CSIRInstr,
1057 CodeGenOpts.DebugInfoForProfiling);
1058 }
Davide Italiano945de432017-02-13 16:07:05 +00001059
Alina Sbirlea267ac922019-05-23 18:51:02 +00001060 PipelineTuningOptions PTO;
Alina Sbirlea21efe2a2019-05-24 17:40:52 +00001061 PTO.LoopUnrolling = CodeGenOpts.UnrollLoops;
Alina Sbirlea267ac922019-05-23 18:51:02 +00001062 // For historical reasons, loop interleaving is set to mirror setting for loop
1063 // unrolling.
1064 PTO.LoopInterleaving = CodeGenOpts.UnrollLoops;
1065 PTO.LoopVectorization = CodeGenOpts.VectorizeLoop;
1066 PTO.SLPVectorization = CodeGenOpts.VectorizeSLP;
1067
Taewook Ohd4c50f72019-08-14 07:11:09 +00001068 PassInstrumentationCallbacks PIC;
1069 StandardInstrumentations SI;
1070 SI.registerCallbacks(PIC);
1071 PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC);
Chandler Carruth50f9e892016-12-23 20:44:01 +00001072
Philip Pfaffee3f105c2019-02-02 23:19:32 +00001073 // Attempt to load pass plugins and register their callbacks with PB.
1074 for (auto &PluginFN : CodeGenOpts.PassPlugins) {
1075 auto PassPlugin = PassPlugin::Load(PluginFN);
1076 if (PassPlugin) {
1077 PassPlugin->registerPassBuilderCallbacks(PB);
1078 } else {
1079 Diags.Report(diag::err_fe_unable_to_load_plugin)
1080 << PluginFN << toString(PassPlugin.takeError());
1081 }
1082 }
serge_sans_paille24ab9b52019-06-08 17:37:47 +02001083#define HANDLE_EXTENSION(Ext) \
1084 get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
1085#include "llvm/Support/Extension.def"
Philip Pfaffee3f105c2019-02-02 23:19:32 +00001086
Craig Topper926b95c2017-11-14 08:48:28 +00001087 LoopAnalysisManager LAM(CodeGenOpts.DebugPassManager);
1088 FunctionAnalysisManager FAM(CodeGenOpts.DebugPassManager);
1089 CGSCCAnalysisManager CGAM(CodeGenOpts.DebugPassManager);
1090 ModuleAnalysisManager MAM(CodeGenOpts.DebugPassManager);
Chandler Carruth50f9e892016-12-23 20:44:01 +00001091
1092 // Register the AA manager first so that our version is the one used.
1093 FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });
1094
Chandler Carruth9fdd5fa2017-07-25 10:46:07 +00001095 // Register the target library analysis directly and give it a customized
1096 // preset TLI.
1097 Triple TargetTriple(TheModule->getTargetTriple());
1098 std::unique_ptr<TargetLibraryInfoImpl> TLII(
1099 createTLII(TargetTriple, CodeGenOpts));
1100 FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
Chandler Carruth9fdd5fa2017-07-25 10:46:07 +00001101
Chandler Carruth50f9e892016-12-23 20:44:01 +00001102 // Register all the basic analyses with the managers.
1103 PB.registerModuleAnalyses(MAM);
1104 PB.registerCGSCCAnalyses(CGAM);
1105 PB.registerFunctionAnalyses(FAM);
1106 PB.registerLoopAnalyses(LAM);
1107 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
1108
Tim Shenb13eebe2017-06-29 23:10:13 +00001109 ModulePassManager MPM(CodeGenOpts.DebugPassManager);
Chandler Carruth50f9e892016-12-23 20:44:01 +00001110
Chandler Carruth6d1b83e2016-12-27 00:13:09 +00001111 if (!CodeGenOpts.DisableLLVMPasses) {
Tobias Edler von Koch7609cb82018-06-22 20:23:21 +00001112 bool IsThinLTO = CodeGenOpts.PrepareForThinLTO;
Tim Shen66470692017-06-29 23:08:38 +00001113 bool IsLTO = CodeGenOpts.PrepareForLTO;
1114
Chandler Carruth6d1b83e2016-12-27 00:13:09 +00001115 if (CodeGenOpts.OptimizationLevel == 0) {
David Blaikieac904d02018-01-23 01:25:24 +00001116 if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts))
1117 MPM.addPass(GCOVProfilerPass(*Options));
Rong Xu4059e142019-04-25 17:52:43 +00001118 if (Optional<InstrProfOptions> Options =
1119 getInstrProfOptions(CodeGenOpts, LangOpts))
1120 MPM.addPass(InstrProfiling(*Options, false));
David Blaikieac904d02018-01-23 01:25:24 +00001121
Chandler Carruth6d1b83e2016-12-27 00:13:09 +00001122 // Build a minimal pipeline based on the semantics required by Clang,
Leonard Chan587497b2019-06-13 16:45:29 +00001123 // which is just that always inlining occurs. Further, disable generating
1124 // lifetime intrinsics to avoid enabling further optimizations during
1125 // code generation.
1126 MPM.addPass(AlwaysInlinerPass(/*InsertLifetimeIntrinsics=*/false));
Chandler Carrutha8bd4e32017-11-14 01:59:18 +00001127
Rong Xuca161fa2019-08-01 22:36:34 +00001128 // At -O0, we can still do PGO. Add all the requested passes for
1129 // instrumentation PGO, if requested.
1130 if (PGOOpt && (PGOOpt->Action == PGOOptions::IRInstr ||
1131 PGOOpt->Action == PGOOptions::IRUse))
1132 PB.addPGOInstrPassesForO0(
1133 MPM, CodeGenOpts.DebugPassManager,
1134 /* RunProfileGen */ (PGOOpt->Action == PGOOptions::IRInstr),
1135 /* IsCS */ false, PGOOpt->ProfileFile,
1136 PGOOpt->ProfileRemappingFile);
1137
Chandler Carrutha8bd4e32017-11-14 01:59:18 +00001138 // At -O0 we directly run necessary sanitizer passes.
1139 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
1140 MPM.addPass(createModuleToFunctionPassAdaptor(BoundsCheckingPass()));
1141
Teresa Johnson6ed79132019-01-04 19:05:01 +00001142 // Lastly, add semantically necessary passes for LTO.
1143 if (IsLTO || IsThinLTO) {
1144 MPM.addPass(CanonicalizeAliasesPass());
Tim Shen66470692017-06-29 23:08:38 +00001145 MPM.addPass(NameAnonGlobalPass());
Teresa Johnson6ed79132019-01-04 19:05:01 +00001146 }
Chandler Carruth6d1b83e2016-12-27 00:13:09 +00001147 } else {
Tim Shen66470692017-06-29 23:08:38 +00001148 // Map our optimization levels into one of the distinct levels used to
1149 // configure the pipeline.
Chandler Carruth6d1b83e2016-12-27 00:13:09 +00001150 PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts);
1151
Leonard Chanb2065132019-06-20 19:35:25 +00001152 PB.registerPipelineStartEPCallback([](ModulePassManager &MPM) {
1153 MPM.addPass(createModuleToFunctionPassAdaptor(
1154 EntryExitInstrumenterPass(/*PostInlining=*/false)));
1155 });
1156
Chandler Carrutha8bd4e32017-11-14 01:59:18 +00001157 // Register callbacks to schedule sanitizer passes at the appropriate part of
1158 // the pipeline.
Philip Pfaffe88a13b92019-01-17 10:10:47 +00001159 // FIXME: either handle asan/the remaining sanitizers or error out
Chandler Carrutha8bd4e32017-11-14 01:59:18 +00001160 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
1161 PB.registerScalarOptimizerLateEPCallback(
1162 [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
1163 FPM.addPass(BoundsCheckingPass());
1164 });
Vitaly Bukab46dd6e2019-10-11 08:47:03 +00001165 if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
1166 PB.registerPipelineStartEPCallback([](ModulePassManager &MPM) {
1167 MPM.addPass(MemorySanitizerPass({}));
1168 });
Philip Pfaffe88a13b92019-01-17 10:10:47 +00001169 PB.registerOptimizerLastEPCallback(
1170 [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffe0ee6a932019-02-04 21:02:49 +00001171 FPM.addPass(MemorySanitizerPass({}));
Philip Pfaffe88a13b92019-01-17 10:10:47 +00001172 });
Vitaly Bukab46dd6e2019-10-11 08:47:03 +00001173 }
1174 if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
1175 PB.registerPipelineStartEPCallback(
1176 [](ModulePassManager &MPM) { MPM.addPass(ThreadSanitizerPass()); });
Philip Pfaffe88a13b92019-01-17 10:10:47 +00001177 PB.registerOptimizerLastEPCallback(
1178 [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
1179 FPM.addPass(ThreadSanitizerPass());
1180 });
Vitaly Bukab46dd6e2019-10-11 08:47:03 +00001181 }
Leonard Chan436fb2b2019-02-13 22:22:48 +00001182 if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
1183 PB.registerPipelineStartEPCallback([&](ModulePassManager &MPM) {
1184 MPM.addPass(
1185 RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
1186 });
1187 bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::Address);
Leonard Chan619b6d52019-02-14 01:07:47 +00001188 bool UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
Leonard Chan436fb2b2019-02-13 22:22:48 +00001189 PB.registerOptimizerLastEPCallback(
Leonard Chan619b6d52019-02-14 01:07:47 +00001190 [Recover, UseAfterScope](FunctionPassManager &FPM,
1191 PassBuilder::OptimizationLevel Level) {
Leonard Chan436fb2b2019-02-13 22:22:48 +00001192 FPM.addPass(AddressSanitizerPass(
Leonard Chan619b6d52019-02-14 01:07:47 +00001193 /*CompileKernel=*/false, Recover, UseAfterScope));
Leonard Chan436fb2b2019-02-13 22:22:48 +00001194 });
1195 bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
Leonard Chan619b6d52019-02-14 01:07:47 +00001196 bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator;
1197 PB.registerPipelineStartEPCallback(
1198 [Recover, ModuleUseAfterScope,
1199 UseOdrIndicator](ModulePassManager &MPM) {
1200 MPM.addPass(ModuleAddressSanitizerPass(
1201 /*CompileKernel=*/false, Recover, ModuleUseAfterScope,
1202 UseOdrIndicator));
1203 });
Leonard Chan436fb2b2019-02-13 22:22:48 +00001204 }
David Blaikieac904d02018-01-23 01:25:24 +00001205 if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts))
1206 PB.registerPipelineStartEPCallback([Options](ModulePassManager &MPM) {
1207 MPM.addPass(GCOVProfilerPass(*Options));
1208 });
Rong Xu4059e142019-04-25 17:52:43 +00001209 if (Optional<InstrProfOptions> Options =
1210 getInstrProfOptions(CodeGenOpts, LangOpts))
1211 PB.registerPipelineStartEPCallback([Options](ModulePassManager &MPM) {
1212 MPM.addPass(InstrProfiling(*Options, false));
1213 });
Chandler Carrutha8bd4e32017-11-14 01:59:18 +00001214
Tim Shen66470692017-06-29 23:08:38 +00001215 if (IsThinLTO) {
Tim Shenb13eebe2017-06-29 23:10:13 +00001216 MPM = PB.buildThinLTOPreLinkDefaultPipeline(
1217 Level, CodeGenOpts.DebugPassManager);
Teresa Johnson6ed79132019-01-04 19:05:01 +00001218 MPM.addPass(CanonicalizeAliasesPass());
Tim Shen66470692017-06-29 23:08:38 +00001219 MPM.addPass(NameAnonGlobalPass());
1220 } else if (IsLTO) {
Tim Shenb13eebe2017-06-29 23:10:13 +00001221 MPM = PB.buildLTOPreLinkDefaultPipeline(Level,
1222 CodeGenOpts.DebugPassManager);
Teresa Johnson6ed79132019-01-04 19:05:01 +00001223 MPM.addPass(CanonicalizeAliasesPass());
Tobias Edler von Koch7609cb82018-06-22 20:23:21 +00001224 MPM.addPass(NameAnonGlobalPass());
Tim Shen66470692017-06-29 23:08:38 +00001225 } else {
Tim Shenb13eebe2017-06-29 23:10:13 +00001226 MPM = PB.buildPerModuleDefaultPipeline(Level,
1227 CodeGenOpts.DebugPassManager);
Tim Shen66470692017-06-29 23:08:38 +00001228 }
Chandler Carruth6d1b83e2016-12-27 00:13:09 +00001229 }
Leonard Chan436fb2b2019-02-13 22:22:48 +00001230
Leonard Chan486b1732019-09-08 07:30:17 +00001231 if (CodeGenOpts.SanitizeCoverageType ||
1232 CodeGenOpts.SanitizeCoverageIndirectCalls ||
1233 CodeGenOpts.SanitizeCoverageTraceCmp) {
1234 auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts);
1235 MPM.addPass(ModuleSanitizerCoveragePass(SancovOpts));
1236 }
1237
Peter Collingbourne3b82b922019-07-17 21:45:19 +00001238 if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
1239 bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
1240 MPM.addPass(HWAddressSanitizerPass(
1241 /*CompileKernel=*/false, Recover));
1242 }
1243 if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
1244 MPM.addPass(HWAddressSanitizerPass(
1245 /*CompileKernel=*/true, /*Recover=*/true));
1246 }
1247
Leonard Chan007f6742019-07-25 20:53:15 +00001248 if (CodeGenOpts.OptimizationLevel == 0) {
Leonard Chan436fb2b2019-02-13 22:22:48 +00001249 addSanitizersAtO0(MPM, TargetTriple, LangOpts, CodeGenOpts);
Leonard Chan007f6742019-07-25 20:53:15 +00001250 }
Chandler Carruth50f9e892016-12-23 20:44:01 +00001251 }
1252
1253 // FIXME: We still use the legacy pass manager to do code generation. We
1254 // create that pass manager here and use it as needed below.
1255 legacy::PassManager CodeGenPasses;
1256 bool NeedCodeGen = false;
Peter Collingbourne91d02842018-05-22 18:52:37 +00001257 std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
Chandler Carruth50f9e892016-12-23 20:44:01 +00001258
1259 // Append any output we need to the pass manager.
1260 switch (Action) {
1261 case Backend_EmitNothing:
1262 break;
1263
1264 case Backend_EmitBC:
Eli Friedman53591232018-08-24 19:31:52 +00001265 if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
Tim Shen50fedec2017-06-01 23:27:51 +00001266 if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
Peter Collingbourne91d02842018-05-22 18:52:37 +00001267 ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
1268 if (!ThinLinkOS)
Tim Shen50fedec2017-06-01 23:27:51 +00001269 return;
Tim Shen50fedec2017-06-01 23:27:51 +00001270 }
Teresa Johnson84cecfc2019-01-11 18:32:07 +00001271 TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1272 CodeGenOpts.EnableSplitLTOUnit);
Peter Collingbourne91d02842018-05-22 18:52:37 +00001273 MPM.addPass(ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &ThinLinkOS->os()
1274 : nullptr));
Tim Shen50fedec2017-06-01 23:27:51 +00001275 } else {
Tobias Edler von Koch7609cb82018-06-22 20:23:21 +00001276 // Emit a module summary by default for Regular LTO except for ld64
1277 // targets
1278 bool EmitLTOSummary =
1279 (CodeGenOpts.PrepareForLTO &&
Eli Friedman53591232018-08-24 19:31:52 +00001280 !CodeGenOpts.DisableLLVMPasses &&
Tobias Edler von Koch7609cb82018-06-22 20:23:21 +00001281 llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
1282 llvm::Triple::Apple);
Teresa Johnson84cecfc2019-01-11 18:32:07 +00001283 if (EmitLTOSummary) {
1284 if (!TheModule->getModuleFlag("ThinLTO"))
1285 TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
1286 TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
Leonard Chan19ec31d2019-08-21 17:24:14 +00001287 uint32_t(1));
Teresa Johnson84cecfc2019-01-11 18:32:07 +00001288 }
1289 MPM.addPass(
1290 BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
Tim Shen50fedec2017-06-01 23:27:51 +00001291 }
Chandler Carruth50f9e892016-12-23 20:44:01 +00001292 break;
1293
1294 case Backend_EmitLL:
1295 MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
1296 break;
1297
1298 case Backend_EmitAssembly:
1299 case Backend_EmitMCNull:
1300 case Backend_EmitObj:
1301 NeedCodeGen = true;
1302 CodeGenPasses.add(
1303 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
Aaron Puchertb207bae2019-06-26 21:36:35 +00001304 if (!CodeGenOpts.SplitDwarfOutput.empty()) {
Aaron Puchert922759a2019-06-15 14:07:43 +00001305 DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
Peter Collingbourne91d02842018-05-22 18:52:37 +00001306 if (!DwoOS)
1307 return;
1308 }
1309 if (!AddEmitPasses(CodeGenPasses, Action, *OS,
1310 DwoOS ? &DwoOS->os() : nullptr))
Chandler Carruth50f9e892016-12-23 20:44:01 +00001311 // FIXME: Should we handle this error differently?
1312 return;
1313 break;
1314 }
1315
1316 // Before executing passes, print the final values of the LLVM options.
1317 cl::PrintOptionValues();
1318
1319 // Now that we have all of the passes ready, run them.
1320 {
1321 PrettyStackTraceString CrashInfo("Optimizer");
1322 MPM.run(*TheModule, MAM);
1323 }
1324
1325 // Now if needed, run the legacy PM for codegen.
1326 if (NeedCodeGen) {
1327 PrettyStackTraceString CrashInfo("Code generation");
1328 CodeGenPasses.run(*TheModule);
1329 }
Peter Collingbourne91d02842018-05-22 18:52:37 +00001330
1331 if (ThinLinkOS)
1332 ThinLinkOS->keep();
1333 if (DwoOS)
1334 DwoOS->keep();
Chandler Carruth50f9e892016-12-23 20:44:01 +00001335}
1336
Peter Collingbourne47d23642017-01-24 19:54:37 +00001337Expected<BitcodeModule> clang::FindThinLTOModule(MemoryBufferRef MBRef) {
1338 Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
1339 if (!BMsOrErr)
1340 return BMsOrErr.takeError();
1341
Peter Collingbournedbd2fed2017-06-15 17:26:13 +00001342 // The bitcode file may contain multiple modules, we want the one that is
1343 // marked as being the ThinLTO module.
Vitaly Bukac35ff822018-02-16 23:34:16 +00001344 if (const BitcodeModule *Bm = FindThinLTOModule(*BMsOrErr))
1345 return *Bm;
Peter Collingbourne47d23642017-01-24 19:54:37 +00001346
1347 return make_error<StringError>("Could not find module summary",
1348 inconvertibleErrorCode());
1349}
1350
Vitaly Bukac35ff822018-02-16 23:34:16 +00001351BitcodeModule *clang::FindThinLTOModule(MutableArrayRef<BitcodeModule> BMs) {
1352 for (BitcodeModule &BM : BMs) {
1353 Expected<BitcodeLTOInfo> LTOInfo = BM.getLTOInfo();
1354 if (LTOInfo && LTOInfo->IsThinLTO)
1355 return &BM;
1356 }
1357 return nullptr;
1358}
1359
Teresa Johnsoncffeb542017-01-06 23:37:33 +00001360static void runThinLTOBackend(ModuleSummaryIndex *CombinedIndex, Module *M,
Teresa Johnson5ed6c102017-03-31 02:05:15 +00001361 const HeaderSearchOptions &HeaderOpts,
1362 const CodeGenOptions &CGOpts,
1363 const clang::TargetOptions &TOpts,
1364 const LangOptions &LOpts,
Dehao Chena1bd2d62017-01-13 00:51:55 +00001365 std::unique_ptr<raw_pwrite_stream> OS,
Teresa Johnson5ed6c102017-03-31 02:05:15 +00001366 std::string SampleProfile,
Richard Smith8654ae52018-10-10 23:13:35 +00001367 std::string ProfileRemapping,
Teresa Johnson5ed6c102017-03-31 02:05:15 +00001368 BackendAction Action) {
Dehao Chen5f83d0e2017-07-10 20:31:37 +00001369 StringMap<DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
Teresa Johnson9e3f4742016-08-12 18:12:08 +00001370 ModuleToDefinedGVSummaries;
1371 CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1372
David Blaikie6e2ec5f2017-04-19 20:08:21 +00001373 setCommandLineOpts(CGOpts);
1374
Teresa Johnsonf9b17d42016-12-28 18:00:08 +00001375 // We can simply import the values mentioned in the combined index, since
1376 // we should only invoke this using the individual indexes written out
1377 // via a WriteIndexesThinBackend.
Teresa Johnson9e3f4742016-08-12 18:12:08 +00001378 FunctionImporter::ImportMapTy ImportList;
Teresa Johnsonf9b17d42016-12-28 18:00:08 +00001379 for (auto &GlobalList : *CombinedIndex) {
Peter Collingbourne9667b912017-05-04 18:03:25 +00001380 // Ignore entries for undefined references.
1381 if (GlobalList.second.SummaryList.empty())
1382 continue;
1383
Teresa Johnsonf9b17d42016-12-28 18:00:08 +00001384 auto GUID = GlobalList.first;
Teresa Johnson5ed8b002018-11-29 17:02:59 +00001385 for (auto &Summary : GlobalList.second.SummaryList) {
1386 // Skip the summaries for the importing module. These are included to
1387 // e.g. record required linkage changes.
1388 if (Summary->modulePath() == M->getModuleIdentifier())
1389 continue;
1390 // Add an entry to provoke importing by thinBackend.
1391 ImportList[Summary->modulePath()].insert(GUID);
1392 }
Teresa Johnsonf9b17d42016-12-28 18:00:08 +00001393 }
Teresa Johnson9e3f4742016-08-12 18:12:08 +00001394
1395 std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports;
Peter Collingbourne1a0720e2016-12-14 01:17:59 +00001396 MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap;
Teresa Johnson9e3f4742016-08-12 18:12:08 +00001397
1398 for (auto &I : ImportList) {
1399 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
1400 llvm::MemoryBuffer::getFile(I.first());
1401 if (!MBOrErr) {
1402 errs() << "Error loading imported file '" << I.first()
1403 << "': " << MBOrErr.getError().message() << "\n";
1404 return;
1405 }
Peter Collingbourne1a0720e2016-12-14 01:17:59 +00001406
Peter Collingbourne47d23642017-01-24 19:54:37 +00001407 Expected<BitcodeModule> BMOrErr = FindThinLTOModule(**MBOrErr);
1408 if (!BMOrErr) {
1409 handleAllErrors(BMOrErr.takeError(), [&](ErrorInfoBase &EIB) {
Peter Collingbourne1a0720e2016-12-14 01:17:59 +00001410 errs() << "Error loading imported file '" << I.first()
1411 << "': " << EIB.message() << '\n';
1412 });
1413 return;
1414 }
Peter Collingbourne47d23642017-01-24 19:54:37 +00001415 ModuleMap.insert({I.first(), *BMOrErr});
Peter Collingbourne1a0720e2016-12-14 01:17:59 +00001416
Teresa Johnson9e3f4742016-08-12 18:12:08 +00001417 OwnedImports.push_back(std::move(*MBOrErr));
1418 }
Peter Collingbourne2d3a26f2016-09-23 21:43:51 +00001419 auto AddStream = [&](size_t Task) {
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001420 return std::make_unique<lto::NativeObjectStream>(std::move(OS));
Mehdi Amini406aa222016-08-17 06:23:08 +00001421 };
Teresa Johnson9e3f4742016-08-12 18:12:08 +00001422 lto::Config Conf;
Teresa Johnson9e4321c2018-04-17 16:39:25 +00001423 if (CGOpts.SaveTempsFilePrefix != "") {
1424 if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".",
1425 /* UseInputModulePath */ false)) {
1426 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1427 errs() << "Error setting up ThinLTO save-temps: " << EIB.message()
1428 << '\n';
1429 });
1430 }
1431 }
Teresa Johnson5ed6c102017-03-31 02:05:15 +00001432 Conf.CPU = TOpts.CPU;
1433 Conf.CodeModel = getCodeModel(CGOpts);
1434 Conf.MAttrs = TOpts.Features;
Rafael Espindolae1d70532018-01-18 00:20:03 +00001435 Conf.RelocModel = CGOpts.RelocationModel;
Teresa Johnson5ed6c102017-03-31 02:05:15 +00001436 Conf.CGOptLevel = getCGOptLevel(CGOpts);
Teresa Johnson867bc392019-04-23 18:56:19 +00001437 Conf.OptLevel = CGOpts.OptimizationLevel;
Teresa Johnson5ed6c102017-03-31 02:05:15 +00001438 initTargetOptions(Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
Benjamin Kramerf6021ec2017-03-21 21:35:04 +00001439 Conf.SampleProfile = std::move(SampleProfile);
Rong Xua4a09b22019-03-04 20:21:31 +00001440
1441 // Context sensitive profile.
1442 if (CGOpts.hasProfileCSIRInstr()) {
1443 Conf.RunCSIRInstr = true;
1444 Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput);
1445 } else if (CGOpts.hasProfileCSIRUse()) {
1446 Conf.RunCSIRInstr = false;
1447 Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath);
1448 }
1449
Richard Smith8654ae52018-10-10 23:13:35 +00001450 Conf.ProfileRemapping = std::move(ProfileRemapping);
Tim Shen50fedec2017-06-01 23:27:51 +00001451 Conf.UseNewPM = CGOpts.ExperimentalNewPassManager;
Teresa Johnson4cd016ab2017-11-13 15:38:33 +00001452 Conf.DebugPassManager = CGOpts.DebugPassManager;
Teresa Johnson66744f82018-05-05 14:37:29 +00001453 Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness;
1454 Conf.RemarksFilename = CGOpts.OptRecordFile;
Francis Visoiu Mistrihdd422362019-03-12 21:22:27 +00001455 Conf.RemarksPasses = CGOpts.OptRecordPasses;
Francis Visoiu Mistrih34667512019-06-17 16:06:00 +00001456 Conf.RemarksFormat = CGOpts.OptRecordFormat;
Aaron Pucherte1dc4952019-06-15 15:38:51 +00001457 Conf.SplitDwarfFile = CGOpts.SplitDwarfFile;
1458 Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput;
Teresa Johnsonb637cb02017-03-31 22:35:47 +00001459 switch (Action) {
1460 case Backend_EmitNothing:
1461 Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1462 return false;
1463 };
1464 break;
1465 case Backend_EmitLL:
1466 Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1467 M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1468 return false;
1469 };
1470 break;
1471 case Backend_EmitBC:
1472 Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
Rafael Espindola76c8f822018-02-14 19:11:37 +00001473 WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists);
Teresa Johnsonb637cb02017-03-31 22:35:47 +00001474 return false;
1475 };
1476 break;
1477 default:
1478 Conf.CGFileType = getCodeGenFileType(Action);
1479 break;
1480 }
Teresa Johnson9e3f4742016-08-12 18:12:08 +00001481 if (Error E = thinBackend(
Teresa Johnson66744f82018-05-05 14:37:29 +00001482 Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
Teresa Johnson9e3f4742016-08-12 18:12:08 +00001483 ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) {
1484 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1485 errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
1486 });
1487 }
1488}
1489
David Blaikie9c902b52011-09-25 23:23:43 +00001490void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
Saleem Abdulrasool888e2892017-01-05 16:02:32 +00001491 const HeaderSearchOptions &HeaderOpts,
David Blaikie9c902b52011-09-25 23:23:43 +00001492 const CodeGenOptions &CGOpts,
Nick Lewycky432add52011-12-02 22:17:00 +00001493 const clang::TargetOptions &TOpts,
Saleem Abdulrasool888e2892017-01-05 16:02:32 +00001494 const LangOptions &LOpts,
1495 const llvm::DataLayout &TDesc, Module *M,
1496 BackendAction Action,
Peter Collingbourne03f89072016-07-15 00:55:40 +00001497 std::unique_ptr<raw_pwrite_stream> OS) {
Anton Afanasyevd880de22019-03-30 08:42:48 +00001498
Russell Gallopdf494f72019-12-11 11:49:42 +00001499 llvm::TimeTraceScope TimeScope("Backend");
Anton Afanasyevd880de22019-03-30 08:42:48 +00001500
Vitaly Buka769134d2018-02-16 23:38:22 +00001501 std::unique_ptr<llvm::Module> EmptyModule;
Teresa Johnson9e3f4742016-08-12 18:12:08 +00001502 if (!CGOpts.ThinLTOIndexFile.empty()) {
Teresa Johnsoncffeb542017-01-06 23:37:33 +00001503 // If we are performing a ThinLTO importing compile, load the function index
1504 // into memory and pass it into runThinLTOBackend, which will run the
1505 // function importer and invoke LTO passes.
1506 Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
Teresa Johnson517729f2017-05-12 19:32:17 +00001507 llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile,
1508 /*IgnoreEmptyThinLTOIndexFile*/true);
Teresa Johnsoncffeb542017-01-06 23:37:33 +00001509 if (!IndexOrErr) {
1510 logAllUnhandledErrors(IndexOrErr.takeError(), errs(),
1511 "Error loading index file '" +
1512 CGOpts.ThinLTOIndexFile + "': ");
1513 return;
1514 }
1515 std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr);
1516 // A null CombinedIndex means we should skip ThinLTO compilation
1517 // (LLVM will optionally ignore empty index files, returning null instead
1518 // of an error).
Vitaly Buka769134d2018-02-16 23:38:22 +00001519 if (CombinedIndex) {
1520 if (!CombinedIndex->skipModuleByDistributedBackend()) {
1521 runThinLTOBackend(CombinedIndex.get(), M, HeaderOpts, CGOpts, TOpts,
1522 LOpts, std::move(OS), CGOpts.SampleProfileFile,
Richard Smith8654ae52018-10-10 23:13:35 +00001523 CGOpts.ProfileRemappingFile, Action);
Vitaly Buka769134d2018-02-16 23:38:22 +00001524 return;
1525 }
1526 // Distributed indexing detected that nothing from the module is needed
1527 // for the final linking. So we can skip the compilation. We sill need to
1528 // output an empty object file to make sure that a linker does not fail
1529 // trying to read it. Also for some features, like CFI, we must skip
1530 // the compilation as CombinedIndex does not contain all required
1531 // information.
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001532 EmptyModule = std::make_unique<llvm::Module>("empty", M->getContext());
Vitaly Buka769134d2018-02-16 23:38:22 +00001533 EmptyModule->setTargetTriple(M->getTargetTriple());
1534 M = EmptyModule.get();
Teresa Johnsoncffeb542017-01-06 23:37:33 +00001535 }
Teresa Johnson9e3f4742016-08-12 18:12:08 +00001536 }
1537
Saleem Abdulrasool888e2892017-01-05 16:02:32 +00001538 EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +00001539
Chandler Carruth50f9e892016-12-23 20:44:01 +00001540 if (CGOpts.ExperimentalNewPassManager)
1541 AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS));
1542 else
1543 AsmHelper.EmitAssembly(Action, std::move(OS));
Alp Tokere83b9062014-01-02 15:08:04 +00001544
James Y Knightb214cbc2016-03-04 19:00:41 +00001545 // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1546 // DataLayout.
1547 if (AsmHelper.TM) {
Mehdi Aminica3cf9e2015-07-24 16:04:29 +00001548 std::string DLDesc = M->getDataLayout().getStringRepresentation();
James Y Knightb214cbc2016-03-04 19:00:41 +00001549 if (DLDesc != TDesc.getStringRepresentation()) {
Alp Tokere83b9062014-01-02 15:08:04 +00001550 unsigned DiagID = Diags.getCustomDiagID(
1551 DiagnosticsEngine::Error, "backend data layout '%0' does not match "
1552 "expected target description '%1'");
James Y Knightb214cbc2016-03-04 19:00:41 +00001553 Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
Alp Tokere83b9062014-01-02 15:08:04 +00001554 }
1555 }
Daniel Dunbarf976d1b2010-06-07 23:20:08 +00001556}
Steven Wu27fb5222016-05-11 16:26:03 +00001557
Steven Wu27fb5222016-05-11 16:26:03 +00001558// With -fembed-bitcode, save a copy of the llvm IR as data in the
1559// __LLVM,__bitcode section.
1560void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
1561 llvm::MemoryBufferRef Buf) {
1562 if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
1563 return;
Teresa Johnsonc8e0bb32019-12-12 11:59:36 -08001564 llvm::EmbedBitcodeInModule(
1565 *M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker,
1566 CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode,
1567 &CGOpts.CmdArgs);
Steven Wu27fb5222016-05-11 16:26:03 +00001568}