[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / tools / opt / NewPMDriver.cpp
blob631d8eed5d7a8a3ee2ac6f616431b254b1a4864e
1 //===- NewPMDriver.cpp - Driver for opt with new PM -----------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This file is just a split of the code that logically belongs in opt.cpp but
11 /// that includes the new pass manager headers.
12 ///
13 //===----------------------------------------------------------------------===//
15 #include "NewPMDriver.h"
16 #include "PassPrinters.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/Analysis/CGSCCPassManager.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/Bitcode/BitcodeWriterPass.h"
23 #include "llvm/Config/llvm-config.h"
24 #include "llvm/IR/Dominators.h"
25 #include "llvm/IR/IRPrintingPasses.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/PassManager.h"
29 #include "llvm/IR/Verifier.h"
30 #include "llvm/Passes/PassBuilder.h"
31 #include "llvm/Passes/PassPlugin.h"
32 #include "llvm/Passes/StandardInstrumentations.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/ToolOutputFile.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
37 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
38 #include "llvm/Transforms/Scalar/LoopPassManager.h"
39 #include "llvm/Transforms/Utils/Debugify.h"
41 using namespace llvm;
42 using namespace opt_tool;
44 namespace llvm {
45 cl::opt<bool> DebugifyEach(
46 "debugify-each",
47 cl::desc("Start each pass with debugify and end it with check-debugify"));
49 cl::opt<std::string>
50 DebugifyExport("debugify-export",
51 cl::desc("Export per-pass debugify statistics to this file"),
52 cl::value_desc("filename"));
53 } // namespace llvm
55 enum class DebugLogging { None, Normal, Verbose, Quiet };
57 static cl::opt<DebugLogging> DebugPM(
58 "debug-pass-manager", cl::Hidden, cl::ValueOptional,
59 cl::desc("Print pass management debugging information"),
60 cl::init(DebugLogging::None),
61 cl::values(
62 clEnumValN(DebugLogging::Normal, "", ""),
63 clEnumValN(DebugLogging::Quiet, "quiet",
64 "Skip printing info about analyses"),
65 clEnumValN(
66 DebugLogging::Verbose, "verbose",
67 "Print extra information about adaptors and pass managers")));
69 static cl::list<std::string>
70 PassPlugins("load-pass-plugin",
71 cl::desc("Load passes from plugin library"));
73 // This flag specifies a textual description of the alias analysis pipeline to
74 // use when querying for aliasing information. It only works in concert with
75 // the "passes" flag above.
76 static cl::opt<std::string>
77 AAPipeline("aa-pipeline",
78 cl::desc("A textual description of the alias analysis "
79 "pipeline for handling managed aliasing queries"),
80 cl::Hidden, cl::init("default"));
82 /// {{@ These options accept textual pipeline descriptions which will be
83 /// inserted into default pipelines at the respective extension points
84 static cl::opt<std::string> PeepholeEPPipeline(
85 "passes-ep-peephole",
86 cl::desc("A textual description of the function pass pipeline inserted at "
87 "the Peephole extension points into default pipelines"),
88 cl::Hidden);
89 static cl::opt<std::string> LateLoopOptimizationsEPPipeline(
90 "passes-ep-late-loop-optimizations",
91 cl::desc(
92 "A textual description of the loop pass pipeline inserted at "
93 "the LateLoopOptimizations extension point into default pipelines"),
94 cl::Hidden);
95 static cl::opt<std::string> LoopOptimizerEndEPPipeline(
96 "passes-ep-loop-optimizer-end",
97 cl::desc("A textual description of the loop pass pipeline inserted at "
98 "the LoopOptimizerEnd extension point into default pipelines"),
99 cl::Hidden);
100 static cl::opt<std::string> ScalarOptimizerLateEPPipeline(
101 "passes-ep-scalar-optimizer-late",
102 cl::desc("A textual description of the function pass pipeline inserted at "
103 "the ScalarOptimizerLate extension point into default pipelines"),
104 cl::Hidden);
105 static cl::opt<std::string> CGSCCOptimizerLateEPPipeline(
106 "passes-ep-cgscc-optimizer-late",
107 cl::desc("A textual description of the cgscc pass pipeline inserted at "
108 "the CGSCCOptimizerLate extension point into default pipelines"),
109 cl::Hidden);
110 static cl::opt<std::string> VectorizerStartEPPipeline(
111 "passes-ep-vectorizer-start",
112 cl::desc("A textual description of the function pass pipeline inserted at "
113 "the VectorizerStart extension point into default pipelines"),
114 cl::Hidden);
115 static cl::opt<std::string> PipelineStartEPPipeline(
116 "passes-ep-pipeline-start",
117 cl::desc("A textual description of the module pass pipeline inserted at "
118 "the PipelineStart extension point into default pipelines"),
119 cl::Hidden);
120 static cl::opt<std::string> PipelineEarlySimplificationEPPipeline(
121 "passes-ep-pipeline-early-simplification",
122 cl::desc("A textual description of the module pass pipeline inserted at "
123 "the EarlySimplification extension point into default pipelines"),
124 cl::Hidden);
125 static cl::opt<std::string> OptimizerLastEPPipeline(
126 "passes-ep-optimizer-last",
127 cl::desc("A textual description of the module pass pipeline inserted at "
128 "the OptimizerLast extension point into default pipelines"),
129 cl::Hidden);
131 // Individual pipeline tuning options.
132 extern cl::opt<bool> DisableLoopUnrolling;
134 namespace llvm {
135 extern cl::opt<PGOKind> PGOKindFlag;
136 extern cl::opt<std::string> ProfileFile;
137 extern cl::opt<CSPGOKind> CSPGOKindFlag;
138 extern cl::opt<std::string> CSProfileGenFile;
139 extern cl::opt<bool> DisableBasicAA;
140 extern cl::opt<bool> PrintPipelinePasses;
141 } // namespace llvm
143 static cl::opt<std::string>
144 ProfileRemappingFile("profile-remapping-file",
145 cl::desc("Path to the profile remapping file."),
146 cl::Hidden);
147 static cl::opt<bool> DebugInfoForProfiling(
148 "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden,
149 cl::desc("Emit special debug info to enable PGO profile generation."));
150 static cl::opt<bool> PseudoProbeForProfiling(
151 "new-pm-pseudo-probe-for-profiling", cl::init(false), cl::Hidden,
152 cl::desc("Emit pseudo probes to enable PGO profile generation."));
153 /// @}}
155 template <typename PassManagerT>
156 bool tryParsePipelineText(PassBuilder &PB,
157 const cl::opt<std::string> &PipelineOpt) {
158 if (PipelineOpt.empty())
159 return false;
161 // Verify the pipeline is parseable:
162 PassManagerT PM;
163 if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) {
164 errs() << "Could not parse -" << PipelineOpt.ArgStr
165 << " pipeline: " << toString(std::move(Err))
166 << "... I'm going to ignore it.\n";
167 return false;
169 return true;
172 /// If one of the EPPipeline command line options was given, register callbacks
173 /// for parsing and inserting the given pipeline
174 static void registerEPCallbacks(PassBuilder &PB) {
175 if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline))
176 PB.registerPeepholeEPCallback(
177 [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
178 ExitOnError Err("Unable to parse PeepholeEP pipeline: ");
179 Err(PB.parsePassPipeline(PM, PeepholeEPPipeline));
181 if (tryParsePipelineText<LoopPassManager>(PB,
182 LateLoopOptimizationsEPPipeline))
183 PB.registerLateLoopOptimizationsEPCallback(
184 [&PB](LoopPassManager &PM, OptimizationLevel Level) {
185 ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: ");
186 Err(PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline));
188 if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline))
189 PB.registerLoopOptimizerEndEPCallback(
190 [&PB](LoopPassManager &PM, OptimizationLevel Level) {
191 ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: ");
192 Err(PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline));
194 if (tryParsePipelineText<FunctionPassManager>(PB,
195 ScalarOptimizerLateEPPipeline))
196 PB.registerScalarOptimizerLateEPCallback(
197 [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
198 ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: ");
199 Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline));
201 if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline))
202 PB.registerCGSCCOptimizerLateEPCallback(
203 [&PB](CGSCCPassManager &PM, OptimizationLevel Level) {
204 ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: ");
205 Err(PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline));
207 if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline))
208 PB.registerVectorizerStartEPCallback(
209 [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
210 ExitOnError Err("Unable to parse VectorizerStartEP pipeline: ");
211 Err(PB.parsePassPipeline(PM, VectorizerStartEPPipeline));
213 if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline))
214 PB.registerPipelineStartEPCallback(
215 [&PB](ModulePassManager &PM, OptimizationLevel) {
216 ExitOnError Err("Unable to parse PipelineStartEP pipeline: ");
217 Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline));
219 if (tryParsePipelineText<ModulePassManager>(
220 PB, PipelineEarlySimplificationEPPipeline))
221 PB.registerPipelineEarlySimplificationEPCallback(
222 [&PB](ModulePassManager &PM, OptimizationLevel) {
223 ExitOnError Err("Unable to parse EarlySimplification pipeline: ");
224 Err(PB.parsePassPipeline(PM, PipelineEarlySimplificationEPPipeline));
226 if (tryParsePipelineText<FunctionPassManager>(PB, OptimizerLastEPPipeline))
227 PB.registerOptimizerLastEPCallback(
228 [&PB](ModulePassManager &PM, OptimizationLevel) {
229 ExitOnError Err("Unable to parse OptimizerLastEP pipeline: ");
230 Err(PB.parsePassPipeline(PM, OptimizerLastEPPipeline));
234 #define HANDLE_EXTENSION(Ext) \
235 llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
236 #include "llvm/Support/Extension.def"
238 bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
239 TargetLibraryInfoImpl *TLII, ToolOutputFile *Out,
240 ToolOutputFile *ThinLTOLinkOut,
241 ToolOutputFile *OptRemarkFile,
242 StringRef PassPipeline, ArrayRef<StringRef> Passes,
243 OutputKind OK, VerifierKind VK,
244 bool ShouldPreserveAssemblyUseListOrder,
245 bool ShouldPreserveBitcodeUseListOrder,
246 bool EmitSummaryIndex, bool EmitModuleHash,
247 bool EnableDebugify) {
248 bool VerifyEachPass = VK == VK_VerifyEachPass;
250 Optional<PGOOptions> P;
251 switch (PGOKindFlag) {
252 case InstrGen:
253 P = PGOOptions(ProfileFile, "", "", PGOOptions::IRInstr);
254 break;
255 case InstrUse:
256 P = PGOOptions(ProfileFile, "", ProfileRemappingFile, PGOOptions::IRUse);
257 break;
258 case SampleUse:
259 P = PGOOptions(ProfileFile, "", ProfileRemappingFile,
260 PGOOptions::SampleUse);
261 break;
262 case NoPGO:
263 if (DebugInfoForProfiling || PseudoProbeForProfiling)
264 P = PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction,
265 DebugInfoForProfiling, PseudoProbeForProfiling);
266 else
267 P = None;
269 if (CSPGOKindFlag != NoCSPGO) {
270 if (P && (P->Action == PGOOptions::IRInstr ||
271 P->Action == PGOOptions::SampleUse))
272 errs() << "CSPGOKind cannot be used with IRInstr or SampleUse";
273 if (CSPGOKindFlag == CSInstrGen) {
274 if (CSProfileGenFile.empty())
275 errs() << "CSInstrGen needs to specify CSProfileGenFile";
276 if (P) {
277 P->CSAction = PGOOptions::CSIRInstr;
278 P->CSProfileGenFile = CSProfileGenFile;
279 } else
280 P = PGOOptions("", CSProfileGenFile, ProfileRemappingFile,
281 PGOOptions::NoAction, PGOOptions::CSIRInstr);
282 } else /* CSPGOKindFlag == CSInstrUse */ {
283 if (!P)
284 errs() << "CSInstrUse needs to be together with InstrUse";
285 P->CSAction = PGOOptions::CSIRUse;
288 if (TM)
289 TM->setPGOOption(P);
291 LoopAnalysisManager LAM;
292 FunctionAnalysisManager FAM;
293 CGSCCAnalysisManager CGAM;
294 ModuleAnalysisManager MAM;
296 PassInstrumentationCallbacks PIC;
297 PrintPassOptions PrintPassOpts;
298 PrintPassOpts.Verbose = DebugPM == DebugLogging::Verbose;
299 PrintPassOpts.SkipAnalyses = DebugPM == DebugLogging::Quiet;
300 StandardInstrumentations SI(DebugPM != DebugLogging::None, VerifyEachPass,
301 PrintPassOpts);
302 SI.registerCallbacks(PIC, &FAM);
303 DebugifyEachInstrumentation Debugify;
304 if (DebugifyEach)
305 Debugify.registerCallbacks(PIC);
307 PipelineTuningOptions PTO;
308 // LoopUnrolling defaults on to true and DisableLoopUnrolling is initialized
309 // to false above so we shouldn't necessarily need to check whether or not the
310 // option has been enabled.
311 PTO.LoopUnrolling = !DisableLoopUnrolling;
312 PassBuilder PB(TM, PTO, P, &PIC);
313 registerEPCallbacks(PB);
315 // Load requested pass plugins and let them register pass builder callbacks
316 for (auto &PluginFN : PassPlugins) {
317 auto PassPlugin = PassPlugin::Load(PluginFN);
318 if (!PassPlugin) {
319 errs() << "Failed to load passes from '" << PluginFN
320 << "'. Request ignored.\n";
321 continue;
324 PassPlugin->registerPassBuilderCallbacks(PB);
327 // Register a callback that creates the debugify passes as needed.
328 PB.registerPipelineParsingCallback(
329 [](StringRef Name, ModulePassManager &MPM,
330 ArrayRef<PassBuilder::PipelineElement>) {
331 if (Name == "debugify") {
332 MPM.addPass(NewPMDebugifyPass());
333 return true;
334 } else if (Name == "check-debugify") {
335 MPM.addPass(NewPMCheckDebugifyPass());
336 return true;
338 return false;
340 PB.registerPipelineParsingCallback(
341 [](StringRef Name, ModulePassManager &MPM,
342 ArrayRef<PassBuilder::PipelineElement>) {
343 AddressSanitizerOptions Opts;
344 if (Name == "asan-pipeline") {
345 MPM.addPass(
346 RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
347 MPM.addPass(ModuleAddressSanitizerPass(Opts));
348 return true;
349 } else if (Name == "asan-function-pipeline") {
350 MPM.addPass(
351 RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
352 MPM.addPass(
353 createModuleToFunctionPassAdaptor(AddressSanitizerPass(Opts)));
354 return true;
356 return false;
359 #define HANDLE_EXTENSION(Ext) \
360 get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
361 #include "llvm/Support/Extension.def"
363 // Specially handle the alias analysis manager so that we can register
364 // a custom pipeline of AA passes with it.
365 AAManager AA;
366 if (Passes.empty()) {
367 if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) {
368 errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
369 return false;
373 // For compatibility with the legacy PM AA pipeline.
374 // AAResultsWrapperPass by default provides basic-aa in the legacy PM
375 // unless -disable-basic-aa is specified.
376 // TODO: remove this once tests implicitly requiring basic-aa use -passes= and
377 // -aa-pipeline=basic-aa.
378 if (!Passes.empty() && !DisableBasicAA) {
379 if (auto Err = PB.parseAAPipeline(AA, "basic-aa")) {
380 errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
381 return false;
385 // For compatibility with legacy pass manager.
386 // Alias analyses are not specially specified when using the legacy PM.
387 for (auto PassName : Passes) {
388 if (PB.isAAPassName(PassName)) {
389 if (auto Err = PB.parseAAPipeline(AA, PassName)) {
390 errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
391 return false;
396 // Register the AA manager first so that our version is the one used.
397 FAM.registerPass([&] { return std::move(AA); });
398 // Register our TargetLibraryInfoImpl.
399 FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
401 // Register all the basic analyses with the managers.
402 PB.registerModuleAnalyses(MAM);
403 PB.registerCGSCCAnalyses(CGAM);
404 PB.registerFunctionAnalyses(FAM);
405 PB.registerLoopAnalyses(LAM);
406 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
408 ModulePassManager MPM;
409 if (VK > VK_NoVerifier)
410 MPM.addPass(VerifierPass());
411 if (EnableDebugify)
412 MPM.addPass(NewPMDebugifyPass());
414 // Add passes according to the -passes options.
415 if (!PassPipeline.empty()) {
416 assert(Passes.empty() &&
417 "PassPipeline and Passes should not both contain passes");
418 if (auto Err = PB.parsePassPipeline(MPM, PassPipeline)) {
419 errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
420 return false;
423 // Add passes specified using the legacy PM syntax (i.e. not using
424 // -passes). This should be removed later when such support has been
425 // deprecated, i.e. when all lit tests running opt (and not using
426 // -enable-new-pm=0) have been updated to use -passes.
427 for (auto PassName : Passes) {
428 std::string ModifiedPassName(PassName.begin(), PassName.end());
429 if (PB.isAnalysisPassName(PassName))
430 ModifiedPassName = "require<" + ModifiedPassName + ">";
431 // FIXME: These translations are supposed to be removed when lit tests that
432 // use these names have been updated to use the -passes syntax (and when the
433 // support for using the old syntax to specify passes is considered as
434 // deprecated for the new PM).
435 if (ModifiedPassName == "early-cse-memssa")
436 ModifiedPassName = "early-cse<memssa>";
437 else if (ModifiedPassName == "post-inline-ee-instrument")
438 ModifiedPassName = "ee-instrument<post-inline>";
439 else if (ModifiedPassName == "loop-extract-single")
440 ModifiedPassName = "loop-extract<single>";
441 else if (ModifiedPassName == "lower-matrix-intrinsics-minimal")
442 ModifiedPassName = "lower-matrix-intrinsics<minimal>";
443 if (auto Err = PB.parsePassPipeline(MPM, ModifiedPassName)) {
444 errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
445 return false;
449 if (VK > VK_NoVerifier)
450 MPM.addPass(VerifierPass());
451 if (EnableDebugify)
452 MPM.addPass(NewPMCheckDebugifyPass());
454 // Add any relevant output pass at the end of the pipeline.
455 switch (OK) {
456 case OK_NoOutput:
457 break; // No output pass needed.
458 case OK_OutputAssembly:
459 MPM.addPass(
460 PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
461 break;
462 case OK_OutputBitcode:
463 MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
464 EmitSummaryIndex, EmitModuleHash));
465 break;
466 case OK_OutputThinLTOBitcode:
467 MPM.addPass(ThinLTOBitcodeWriterPass(
468 Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
469 break;
472 // Before executing passes, print the final values of the LLVM options.
473 cl::PrintOptionValues();
475 // Print a textual, '-passes=' compatible, representation of pipeline if
476 // requested.
477 if (PrintPipelinePasses) {
478 MPM.printPipeline(outs(), [&PIC](StringRef ClassName) {
479 auto PassName = PIC.getPassNameForClassName(ClassName);
480 return PassName.empty() ? ClassName : PassName;
482 outs() << "\n";
483 return true;
486 // Now that we have all of the passes ready, run them.
487 MPM.run(M, MAM);
489 // Declare success.
490 if (OK != OK_NoOutput) {
491 Out->keep();
492 if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
493 ThinLTOLinkOut->keep();
496 if (OptRemarkFile)
497 OptRemarkFile->keep();
499 if (DebugifyEach && !DebugifyExport.empty())
500 exportDebugifyStats(DebugifyExport, Debugify.StatsMap);
502 return true;
505 void llvm::printPasses(raw_ostream &OS) {
506 PassBuilder PB;
507 PB.printPassNames(OS);