blob: 58e9caeff0fb12a188a3896faea343caa2cf1e6d [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
Tim Shen6b4114182017-06-01 01:02:12 +000051bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
52 tool_output_file *Out,
53 tool_output_file *ThinLTOLinkOut,
Chandler Carruthe0385522015-02-01 10:11:22 +000054 StringRef PassPipeline, OutputKind OK,
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +000055 VerifierKind VK,
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +000056 bool ShouldPreserveAssemblyUseListOrder,
Teresa Johnsonf93b2462016-08-12 13:53:02 +000057 bool ShouldPreserveBitcodeUseListOrder,
58 bool EmitSummaryIndex, bool EmitModuleHash) {
Chandler Carruth1ff77242015-03-07 09:02:36 +000059 PassBuilder PB(TM);
Chandler Carruth2dc92e92015-02-01 07:40:05 +000060
Chandler Carruthedf59962016-02-18 09:45:17 +000061 // Specially handle the alias analysis manager so that we can register
62 // a custom pipeline of AA passes with it.
63 AAManager AA;
64 if (!PB.parseAAPipeline(AA, AAPipeline)) {
65 errs() << Arg0 << ": unable to parse AA pipeline description.\n";
66 return false;
67 }
68
Justin Bognereecc3c82016-02-25 07:23:08 +000069 LoopAnalysisManager LAM(DebugPM);
Chandler Carruth14a759e2015-01-13 22:42:38 +000070 FunctionAnalysisManager FAM(DebugPM);
71 CGSCCAnalysisManager CGAM(DebugPM);
72 ModuleAnalysisManager MAM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +000073
Chandler Carruthedf59962016-02-18 09:45:17 +000074 // Register the AA manager first so that our version is the one used.
75 FAM.registerPass([&] { return std::move(AA); });
76
Chandler Carruthb70f6732015-01-06 02:21:37 +000077 // Register all the basic analyses with the managers.
Chandler Carruth1ff77242015-03-07 09:02:36 +000078 PB.registerModuleAnalyses(MAM);
79 PB.registerCGSCCAnalyses(CGAM);
80 PB.registerFunctionAnalyses(FAM);
Justin Bognereecc3c82016-02-25 07:23:08 +000081 PB.registerLoopAnalyses(LAM);
Davide Italianob6ccd6b2016-05-14 23:21:50 +000082 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
Chandler Carruthc68d0822014-02-06 04:25:13 +000083
Chandler Carruth14a759e2015-01-13 22:42:38 +000084 ModulePassManager MPM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +000085 if (VK > VK_NoVerifier)
86 MPM.addPass(VerifierPass());
87
Chandler Carruth1ff77242015-03-07 09:02:36 +000088 if (!PB.parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass,
89 DebugPM)) {
Chandler Carruth66445382014-01-11 08:16:35 +000090 errs() << Arg0 << ": unable to parse pass pipeline description.\n";
91 return false;
92 }
93
Chandler Carruth4d356312014-01-20 11:34:08 +000094 if (VK > VK_NoVerifier)
95 MPM.addPass(VerifierPass());
96
Chandler Carruthb353c3f2014-01-13 05:16:45 +000097 // Add any relevant output pass at the end of the pipeline.
98 switch (OK) {
99 case OK_NoOutput:
100 break; // No output pass needed.
101 case OK_OutputAssembly:
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000102 MPM.addPass(
103 PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000104 break;
105 case OK_OutputBitcode:
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000106 MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
107 EmitSummaryIndex, EmitModuleHash));
Chandler Carruthb7bdfd62014-01-13 07:38:24 +0000108 break;
Tim Shen6b4114182017-06-01 01:02:12 +0000109 case OK_OutputThinLTOBitcode:
110 MPM.addPass(ThinLTOBitcodeWriterPass(
111 Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
112 break;
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000113 }
114
115 // Before executing passes, print the final values of the LLVM options.
116 cl::PrintOptionValues();
117
Chandler Carruth66445382014-01-11 08:16:35 +0000118 // Now that we have all of the passes ready, run them.
Chandler Carruthb47f8012016-03-11 11:05:24 +0000119 MPM.run(M, MAM);
Chandler Carruth66445382014-01-11 08:16:35 +0000120
121 // Declare success.
Tim Shen6b4114182017-06-01 01:02:12 +0000122 if (OK != OK_NoOutput) {
Chandler Carruth66445382014-01-11 08:16:35 +0000123 Out->keep();
Tim Shen6b4114182017-06-01 01:02:12 +0000124 if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
125 ThinLTOLinkOut->keep();
126 }
Chandler Carruth66445382014-01-11 08:16:35 +0000127 return true;
128}