blob: 3030d65743af2d11d0a72d80a5f948dab7909d42 [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 Carruth572e3402014-04-21 11:12:00 +000018#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000019#include "llvm/Bitcode/BitcodeWriterPass.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000020#include "llvm/IR/Dominators.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000021#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000022#include "llvm/IR/LLVMContext.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000025#include "llvm/IR/Verifier.h"
Chandler Carruth1ff77242015-03-07 09:02:36 +000026#include "llvm/Passes/PassBuilder.h"
Chandler Carruth66445382014-01-11 08:16:35 +000027#include "llvm/Support/CommandLine.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000028#include "llvm/Support/ErrorHandling.h"
Chandler Carruth66445382014-01-11 08:16:35 +000029#include "llvm/Support/ToolOutputFile.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000030#include "llvm/Target/TargetMachine.h"
Chandler Carruth66445382014-01-11 08:16:35 +000031
32using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000033using namespace opt_tool;
Chandler Carruth66445382014-01-11 08:16:35 +000034
Chandler Carruth14a759e2015-01-13 22:42:38 +000035static cl::opt<bool>
36 DebugPM("debug-pass-manager", cl::Hidden,
37 cl::desc("Print pass management debugging information"));
38
Chandler Carruth66445382014-01-11 08:16:35 +000039bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
Chandler Carruthe0385522015-02-01 10:11:22 +000040 TargetMachine *TM, tool_output_file *Out,
41 StringRef PassPipeline, OutputKind OK,
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +000042 VerifierKind VK,
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +000043 bool ShouldPreserveAssemblyUseListOrder,
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +000044 bool ShouldPreserveBitcodeUseListOrder) {
Chandler Carruth1ff77242015-03-07 09:02:36 +000045 PassBuilder PB(TM);
Chandler Carruth2dc92e92015-02-01 07:40:05 +000046
Chandler Carruth14a759e2015-01-13 22:42:38 +000047 FunctionAnalysisManager FAM(DebugPM);
48 CGSCCAnalysisManager CGAM(DebugPM);
49 ModuleAnalysisManager MAM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +000050
Chandler Carruthb70f6732015-01-06 02:21:37 +000051 // Register all the basic analyses with the managers.
Chandler Carruth1ff77242015-03-07 09:02:36 +000052 PB.registerModuleAnalyses(MAM);
53 PB.registerCGSCCAnalyses(CGAM);
54 PB.registerFunctionAnalyses(FAM);
Chandler Carruthbf71a342014-02-06 04:37:03 +000055
Chandler Carruthc68d0822014-02-06 04:25:13 +000056 // Cross register the analysis managers through their proxies.
57 MAM.registerPass(FunctionAnalysisManagerModuleProxy(FAM));
Chandler Carruth572e3402014-04-21 11:12:00 +000058 MAM.registerPass(CGSCCAnalysisManagerModuleProxy(CGAM));
59 CGAM.registerPass(FunctionAnalysisManagerCGSCCProxy(FAM));
60 CGAM.registerPass(ModuleAnalysisManagerCGSCCProxy(MAM));
61 FAM.registerPass(CGSCCAnalysisManagerFunctionProxy(CGAM));
Chandler Carruthc68d0822014-02-06 04:25:13 +000062 FAM.registerPass(ModuleAnalysisManagerFunctionProxy(MAM));
63
Chandler Carruth14a759e2015-01-13 22:42:38 +000064 ModulePassManager MPM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +000065 if (VK > VK_NoVerifier)
66 MPM.addPass(VerifierPass());
67
Chandler Carruth1ff77242015-03-07 09:02:36 +000068 if (!PB.parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass,
69 DebugPM)) {
Chandler Carruth66445382014-01-11 08:16:35 +000070 errs() << Arg0 << ": unable to parse pass pipeline description.\n";
71 return false;
72 }
73
Chandler Carruth4d356312014-01-20 11:34:08 +000074 if (VK > VK_NoVerifier)
75 MPM.addPass(VerifierPass());
76
Chandler Carruthb353c3f2014-01-13 05:16:45 +000077 // Add any relevant output pass at the end of the pipeline.
78 switch (OK) {
79 case OK_NoOutput:
80 break; // No output pass needed.
81 case OK_OutputAssembly:
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +000082 MPM.addPass(
83 PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
Chandler Carruthb353c3f2014-01-13 05:16:45 +000084 break;
85 case OK_OutputBitcode:
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +000086 MPM.addPass(
87 BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder));
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000088 break;
Chandler Carruthb353c3f2014-01-13 05:16:45 +000089 }
90
91 // Before executing passes, print the final values of the LLVM options.
92 cl::PrintOptionValues();
93
Chandler Carruth66445382014-01-11 08:16:35 +000094 // Now that we have all of the passes ready, run them.
Chandler Carruthd174ce42015-01-05 02:47:05 +000095 MPM.run(M, &MAM);
Chandler Carruth66445382014-01-11 08:16:35 +000096
97 // Declare success.
Chandler Carruth949282e2014-01-13 03:08:40 +000098 if (OK != OK_NoOutput)
Chandler Carruth66445382014-01-11 08:16:35 +000099 Out->keep();
100 return true;
101}