blob: 0501e80e1a0620eede56cfcf5ac2a314cddcdbb1 [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"
Tim Shen6b4114182017-06-01 01:02:12 +000032#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000033#include "llvm/Transforms/Scalar/LoopPassManager.h"
Chandler Carruth66445382014-01-11 08:16:35 +000034
35using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000036using namespace opt_tool;
Chandler Carruth66445382014-01-11 08:16:35 +000037
Chandler Carruth14a759e2015-01-13 22:42:38 +000038static cl::opt<bool>
39 DebugPM("debug-pass-manager", cl::Hidden,
40 cl::desc("Print pass management debugging information"));
41
Chandler Carruthedf59962016-02-18 09:45:17 +000042// This flag specifies a textual description of the alias analysis pipeline to
43// use when querying for aliasing information. It only works in concert with
44// the "passes" flag above.
45static cl::opt<std::string>
46 AAPipeline("aa-pipeline",
47 cl::desc("A textual description of the alias analysis "
48 "pipeline for handling managed aliasing queries"),
49 cl::Hidden);
50
Philip Pfaffe730f2f92017-07-10 10:57:55 +000051/// {{@ These options accept textual pipeline descriptions which will be
52/// inserted into default pipelines at the respective extension points
53static cl::opt<std::string> PeepholeEPPipeline(
54 "passes-ep-peephole",
55 cl::desc("A textual description of the function pass pipeline inserted at "
56 "the Peephole extension points into default pipelines"),
57 cl::Hidden);
58static cl::opt<std::string> LateLoopOptimizationsEPPipeline(
59 "passes-ep-late-loop-optimizations",
60 cl::desc(
61 "A textual description of the loop pass pipeline inserted at "
62 "the LateLoopOptimizations extension point into default pipelines"),
63 cl::Hidden);
64static cl::opt<std::string> LoopOptimizerEndEPPipeline(
65 "passes-ep-loop-optimizer-end",
66 cl::desc("A textual description of the loop pass pipeline inserted at "
67 "the LoopOptimizerEnd extension point into default pipelines"),
68 cl::Hidden);
69static cl::opt<std::string> ScalarOptimizerLateEPPipeline(
70 "passes-ep-scalar-optimizer-late",
71 cl::desc("A textual description of the function pass pipeline inserted at "
72 "the ScalarOptimizerLate extension point into default pipelines"),
73 cl::Hidden);
74static cl::opt<std::string> CGSCCOptimizerLateEPPipeline(
75 "passes-ep-cgscc-optimizer-late",
76 cl::desc("A textual description of the cgscc pass pipeline inserted at "
77 "the CGSCCOptimizerLate extension point into default pipelines"),
78 cl::Hidden);
79static cl::opt<std::string> VectorizerStartEPPipeline(
80 "passes-ep-vectorizer-start",
81 cl::desc("A textual description of the function pass pipeline inserted at "
82 "the VectorizerStart extension point into default pipelines"),
83 cl::Hidden);
84/// @}}
85
86/// If one of the EPPipeline command line options was given, register callbacks
87/// for parsing and inserting the given pipeline
88static void registerEPCallbacks(PassBuilder &PB, bool VerifyEachPass,
89 bool DebugLogging) {
90 if (!PeepholeEPPipeline.empty())
Philip Pfaffea3b84162017-07-10 12:48:51 +000091 PB.registerPeepholeEPCallback([&PB, VerifyEachPass, DebugLogging](
92 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
93 return PB.parsePassPipeline(PM, PeepholeEPPipeline, VerifyEachPass,
94 DebugPM);
95 });
Philip Pfaffe730f2f92017-07-10 10:57:55 +000096 if (!LateLoopOptimizationsEPPipeline.empty())
97 PB.registerLateLoopOptimizationsEPCallback(
Philip Pfaffea3b84162017-07-10 12:48:51 +000098 [&PB, VerifyEachPass, DebugLogging](
99 LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000100 return PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline,
101 VerifyEachPass, DebugPM);
102 });
103 if (!LoopOptimizerEndEPPipeline.empty())
Philip Pfaffea3b84162017-07-10 12:48:51 +0000104 PB.registerLoopOptimizerEndEPCallback([&PB, VerifyEachPass, DebugLogging](
105 LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
106 return PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline,
107 VerifyEachPass, DebugPM);
108 });
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000109 if (!ScalarOptimizerLateEPPipeline.empty())
110 PB.registerScalarOptimizerLateEPCallback(
Philip Pfaffea3b84162017-07-10 12:48:51 +0000111 [&PB, VerifyEachPass, DebugLogging](
112 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000113 return PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline,
114 VerifyEachPass, DebugPM);
115 });
116 if (!CGSCCOptimizerLateEPPipeline.empty())
Philip Pfaffea3b84162017-07-10 12:48:51 +0000117 PB.registerCGSCCOptimizerLateEPCallback([&PB, VerifyEachPass, DebugLogging](
118 CGSCCPassManager &PM, PassBuilder::OptimizationLevel Level) {
119 return PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline,
120 VerifyEachPass, DebugPM);
121 });
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000122 if (!VectorizerStartEPPipeline.empty())
Philip Pfaffea3b84162017-07-10 12:48:51 +0000123 PB.registerVectorizerStartEPCallback([&PB, VerifyEachPass, DebugLogging](
124 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
125 return PB.parsePassPipeline(PM, VectorizerStartEPPipeline, VerifyEachPass,
126 DebugPM);
127 });
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000128}
129
Tim Shen6b4114182017-06-01 01:02:12 +0000130bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
131 tool_output_file *Out,
132 tool_output_file *ThinLTOLinkOut,
Chandler Carruthe0385522015-02-01 10:11:22 +0000133 StringRef PassPipeline, OutputKind OK,
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +0000134 VerifierKind VK,
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000135 bool ShouldPreserveAssemblyUseListOrder,
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000136 bool ShouldPreserveBitcodeUseListOrder,
137 bool EmitSummaryIndex, bool EmitModuleHash) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000138 bool VerifyEachPass = VK == VK_VerifyEachPass;
Chandler Carruth1ff77242015-03-07 09:02:36 +0000139 PassBuilder PB(TM);
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000140 registerEPCallbacks(PB, VerifyEachPass, DebugPM);
Chandler Carruth2dc92e92015-02-01 07:40:05 +0000141
Chandler Carruthedf59962016-02-18 09:45:17 +0000142 // Specially handle the alias analysis manager so that we can register
143 // a custom pipeline of AA passes with it.
144 AAManager AA;
145 if (!PB.parseAAPipeline(AA, AAPipeline)) {
146 errs() << Arg0 << ": unable to parse AA pipeline description.\n";
147 return false;
148 }
149
Justin Bognereecc3c82016-02-25 07:23:08 +0000150 LoopAnalysisManager LAM(DebugPM);
Chandler Carruth14a759e2015-01-13 22:42:38 +0000151 FunctionAnalysisManager FAM(DebugPM);
152 CGSCCAnalysisManager CGAM(DebugPM);
153 ModuleAnalysisManager MAM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +0000154
Chandler Carruthedf59962016-02-18 09:45:17 +0000155 // Register the AA manager first so that our version is the one used.
156 FAM.registerPass([&] { return std::move(AA); });
157
Chandler Carruthb70f6732015-01-06 02:21:37 +0000158 // Register all the basic analyses with the managers.
Chandler Carruth1ff77242015-03-07 09:02:36 +0000159 PB.registerModuleAnalyses(MAM);
160 PB.registerCGSCCAnalyses(CGAM);
161 PB.registerFunctionAnalyses(FAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000162 PB.registerLoopAnalyses(LAM);
Davide Italianob6ccd6b2016-05-14 23:21:50 +0000163 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
Chandler Carruthc68d0822014-02-06 04:25:13 +0000164
Chandler Carruth14a759e2015-01-13 22:42:38 +0000165 ModulePassManager MPM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +0000166 if (VK > VK_NoVerifier)
167 MPM.addPass(VerifierPass());
168
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000169 if (!PB.parsePassPipeline(MPM, PassPipeline, VerifyEachPass, DebugPM)) {
Chandler Carruth66445382014-01-11 08:16:35 +0000170 errs() << Arg0 << ": unable to parse pass pipeline description.\n";
171 return false;
172 }
173
Chandler Carruth4d356312014-01-20 11:34:08 +0000174 if (VK > VK_NoVerifier)
175 MPM.addPass(VerifierPass());
176
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000177 // Add any relevant output pass at the end of the pipeline.
178 switch (OK) {
179 case OK_NoOutput:
180 break; // No output pass needed.
181 case OK_OutputAssembly:
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000182 MPM.addPass(
183 PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000184 break;
185 case OK_OutputBitcode:
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000186 MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
187 EmitSummaryIndex, EmitModuleHash));
Chandler Carruthb7bdfd62014-01-13 07:38:24 +0000188 break;
Tim Shen6b4114182017-06-01 01:02:12 +0000189 case OK_OutputThinLTOBitcode:
190 MPM.addPass(ThinLTOBitcodeWriterPass(
191 Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
192 break;
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000193 }
194
195 // Before executing passes, print the final values of the LLVM options.
196 cl::PrintOptionValues();
197
Chandler Carruth66445382014-01-11 08:16:35 +0000198 // Now that we have all of the passes ready, run them.
Chandler Carruthb47f8012016-03-11 11:05:24 +0000199 MPM.run(M, MAM);
Chandler Carruth66445382014-01-11 08:16:35 +0000200
201 // Declare success.
Tim Shen6b4114182017-06-01 01:02:12 +0000202 if (OK != OK_NoOutput) {
Chandler Carruth66445382014-01-11 08:16:35 +0000203 Out->keep();
Tim Shen6b4114182017-06-01 01:02:12 +0000204 if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
205 ThinLTOLinkOut->keep();
206 }
Chandler Carruth66445382014-01-11 08:16:35 +0000207 return true;
208}