blob: df467da690e76768d798f5a00458c5182baddd39 [file] [log] [blame]
Chandler Carruth66445382014-01-11 08:16:35 +00001//===- NewPMDriver.cpp - Driver for opt with new PM -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
11/// This file is just a split of the code that logically belongs in opt.cpp but
12/// that includes the new pass manager headers.
13///
14//===----------------------------------------------------------------------===//
15
16#include "NewPMDriver.h"
Chandler Carruth66445382014-01-11 08:16:35 +000017#include "llvm/ADT/StringRef.h"
Chandler Carruthedf59962016-02-18 09:45:17 +000018#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000019#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000020#include "llvm/Bitcode/BitcodeWriterPass.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000021#include "llvm/IR/Dominators.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000022#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000023#include "llvm/IR/LLVMContext.h"
24#include "llvm/IR/Module.h"
25#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000026#include "llvm/IR/Verifier.h"
Chandler Carruth1ff77242015-03-07 09:02:36 +000027#include "llvm/Passes/PassBuilder.h"
Chandler Carruth66445382014-01-11 08:16:35 +000028#include "llvm/Support/CommandLine.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000029#include "llvm/Support/ErrorHandling.h"
Chandler Carruth66445382014-01-11 08:16:35 +000030#include "llvm/Support/ToolOutputFile.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000031#include "llvm/Target/TargetMachine.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000032#include "llvm/Transforms/Scalar/LoopPassManager.h"
Chandler Carruth66445382014-01-11 08:16:35 +000033
34using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000035using namespace opt_tool;
Chandler Carruth66445382014-01-11 08:16:35 +000036
Chandler Carruth14a759e2015-01-13 22:42:38 +000037static cl::opt<bool>
38 DebugPM("debug-pass-manager", cl::Hidden,
39 cl::desc("Print pass management debugging information"));
40
Chandler Carruthedf59962016-02-18 09:45:17 +000041// This flag specifies a textual description of the alias analysis pipeline to
42// use when querying for aliasing information. It only works in concert with
43// the "passes" flag above.
44static cl::opt<std::string>
45 AAPipeline("aa-pipeline",
46 cl::desc("A textual description of the alias analysis "
47 "pipeline for handling managed aliasing queries"),
48 cl::Hidden);
49
Davide Italianoc5d0a5c2016-09-07 00:48:47 +000050bool llvm::runPassPipeline(StringRef Arg0, Module &M,
Chandler Carruthe0385522015-02-01 10:11:22 +000051 TargetMachine *TM, tool_output_file *Out,
52 StringRef PassPipeline, OutputKind OK,
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +000053 VerifierKind VK,
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +000054 bool ShouldPreserveAssemblyUseListOrder,
Teresa Johnsonf93b2462016-08-12 13:53:02 +000055 bool ShouldPreserveBitcodeUseListOrder,
56 bool EmitSummaryIndex, bool EmitModuleHash) {
Chandler Carruth1ff77242015-03-07 09:02:36 +000057 PassBuilder PB(TM);
Chandler Carruth2dc92e92015-02-01 07:40:05 +000058
Chandler Carruthedf59962016-02-18 09:45:17 +000059 // Specially handle the alias analysis manager so that we can register
60 // a custom pipeline of AA passes with it.
61 AAManager AA;
62 if (!PB.parseAAPipeline(AA, AAPipeline)) {
63 errs() << Arg0 << ": unable to parse AA pipeline description.\n";
64 return false;
65 }
66
Justin Bognereecc3c82016-02-25 07:23:08 +000067 LoopAnalysisManager LAM(DebugPM);
Chandler Carruth14a759e2015-01-13 22:42:38 +000068 FunctionAnalysisManager FAM(DebugPM);
69 CGSCCAnalysisManager CGAM(DebugPM);
70 ModuleAnalysisManager MAM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +000071
Chandler Carruthedf59962016-02-18 09:45:17 +000072 // Register the AA manager first so that our version is the one used.
73 FAM.registerPass([&] { return std::move(AA); });
74
Chandler Carruthb70f6732015-01-06 02:21:37 +000075 // Register all the basic analyses with the managers.
Chandler Carruth1ff77242015-03-07 09:02:36 +000076 PB.registerModuleAnalyses(MAM);
77 PB.registerCGSCCAnalyses(CGAM);
78 PB.registerFunctionAnalyses(FAM);
Justin Bognereecc3c82016-02-25 07:23:08 +000079 PB.registerLoopAnalyses(LAM);
Davide Italianob6ccd6b2016-05-14 23:21:50 +000080 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
Chandler Carruthc68d0822014-02-06 04:25:13 +000081
Chandler Carruth14a759e2015-01-13 22:42:38 +000082 ModulePassManager MPM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +000083 if (VK > VK_NoVerifier)
84 MPM.addPass(VerifierPass());
85
Chandler Carruth1ff77242015-03-07 09:02:36 +000086 if (!PB.parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass,
87 DebugPM)) {
Chandler Carruth66445382014-01-11 08:16:35 +000088 errs() << Arg0 << ": unable to parse pass pipeline description.\n";
89 return false;
90 }
91
Chandler Carruth4d356312014-01-20 11:34:08 +000092 if (VK > VK_NoVerifier)
93 MPM.addPass(VerifierPass());
94
Chandler Carruthb353c3f2014-01-13 05:16:45 +000095 // Add any relevant output pass at the end of the pipeline.
96 switch (OK) {
97 case OK_NoOutput:
98 break; // No output pass needed.
99 case OK_OutputAssembly:
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000100 MPM.addPass(
101 PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000102 break;
103 case OK_OutputBitcode:
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000104 MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
105 EmitSummaryIndex, EmitModuleHash));
Chandler Carruthb7bdfd62014-01-13 07:38:24 +0000106 break;
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000107 }
108
109 // Before executing passes, print the final values of the LLVM options.
110 cl::PrintOptionValues();
111
Chandler Carruth66445382014-01-11 08:16:35 +0000112 // Now that we have all of the passes ready, run them.
Chandler Carruthb47f8012016-03-11 11:05:24 +0000113 MPM.run(M, MAM);
Chandler Carruth66445382014-01-11 08:16:35 +0000114
115 // Declare success.
Chandler Carruth949282e2014-01-13 03:08:40 +0000116 if (OK != OK_NoOutput)
Chandler Carruth66445382014-01-11 08:16:35 +0000117 Out->keep();
118 return true;
119}