xref: /aosp_15_r20/external/llvm/lib/CodeGen/TargetPassConfig.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- TargetPassConfig.cpp - Target independent code generation passes --===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines interfaces to access the target independent code
11*9880d681SAndroid Build Coastguard Worker // generation passes provided by the LLVM backend.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===---------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker 
15*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/TargetPassConfig.h"
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/BasicAliasAnalysis.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CFLAndersAliasAnalysis.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CFLSteensAliasAnalysis.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CallGraphSCCPass.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/Passes.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ScopedNoAliasAA.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/RegAllocRegistry.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/RegisterUsageInfo.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IRPrintingPasses.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LegacyPassManager.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Verifier.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCAsmInfo.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Instrumentation.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/SymbolRewriter.h"
38*9880d681SAndroid Build Coastguard Worker 
39*9880d681SAndroid Build Coastguard Worker using namespace llvm;
40*9880d681SAndroid Build Coastguard Worker 
41*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden,
42*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable Post Regalloc"));
43*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden,
44*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable branch folding"));
45*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
46*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable tail duplication"));
47*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
48*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable pre-register allocation tail duplication"));
49*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableBlockPlacement("disable-block-placement",
50*9880d681SAndroid Build Coastguard Worker     cl::Hidden, cl::desc("Disable probability-driven block placement"));
51*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats",
52*9880d681SAndroid Build Coastguard Worker     cl::Hidden, cl::desc("Collect probability-driven block placement stats"));
53*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
54*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable Stack Slot Coloring"));
55*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden,
56*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable Machine Dead Code Elimination"));
57*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableEarlyIfConversion("disable-early-ifcvt", cl::Hidden,
58*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable Early If-conversion"));
59*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
60*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable Machine LICM"));
61*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden,
62*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable Machine Common Subexpression Elimination"));
63*9880d681SAndroid Build Coastguard Worker static cl::opt<cl::boolOrDefault> OptimizeRegAlloc(
64*9880d681SAndroid Build Coastguard Worker     "optimize-regalloc", cl::Hidden,
65*9880d681SAndroid Build Coastguard Worker     cl::desc("Enable optimized register allocation compilation path."));
66*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
67*9880d681SAndroid Build Coastguard Worker     cl::Hidden,
68*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable Machine LICM"));
69*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
70*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable Machine Sinking"));
71*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
72*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable Loop Strength Reduction Pass"));
73*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableConstantHoisting("disable-constant-hoisting",
74*9880d681SAndroid Build Coastguard Worker     cl::Hidden, cl::desc("Disable ConstantHoisting"));
75*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
76*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable Codegen Prepare"));
77*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden,
78*9880d681SAndroid Build Coastguard Worker     cl::desc("Disable Copy Propagation pass"));
79*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisablePartialLibcallInlining("disable-partial-libcall-inlining",
80*9880d681SAndroid Build Coastguard Worker     cl::Hidden, cl::desc("Disable Partial Libcall Inlining"));
81*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> EnableImplicitNullChecks(
82*9880d681SAndroid Build Coastguard Worker     "enable-implicit-null-checks",
83*9880d681SAndroid Build Coastguard Worker     cl::desc("Fold null checks into faulting memory operations"),
84*9880d681SAndroid Build Coastguard Worker     cl::init(false));
85*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
86*9880d681SAndroid Build Coastguard Worker     cl::desc("Print LLVM IR produced by the loop-reduce pass"));
87*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
88*9880d681SAndroid Build Coastguard Worker     cl::desc("Print LLVM IR input to isel pass"));
89*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
90*9880d681SAndroid Build Coastguard Worker     cl::desc("Dump garbage collector data"));
91*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
92*9880d681SAndroid Build Coastguard Worker     cl::desc("Verify generated machine code"),
93*9880d681SAndroid Build Coastguard Worker     cl::init(false),
94*9880d681SAndroid Build Coastguard Worker     cl::ZeroOrMore);
95*9880d681SAndroid Build Coastguard Worker 
96*9880d681SAndroid Build Coastguard Worker static cl::opt<std::string>
97*9880d681SAndroid Build Coastguard Worker PrintMachineInstrs("print-machineinstrs", cl::ValueOptional,
98*9880d681SAndroid Build Coastguard Worker                    cl::desc("Print machine instrs"),
99*9880d681SAndroid Build Coastguard Worker                    cl::value_desc("pass-name"), cl::init("option-unspecified"));
100*9880d681SAndroid Build Coastguard Worker 
101*9880d681SAndroid Build Coastguard Worker // Temporary option to allow experimenting with MachineScheduler as a post-RA
102*9880d681SAndroid Build Coastguard Worker // scheduler. Targets can "properly" enable this with
103*9880d681SAndroid Build Coastguard Worker // substitutePass(&PostRASchedulerID, &PostMachineSchedulerID).
104*9880d681SAndroid Build Coastguard Worker // Targets can return true in targetSchedulesPostRAScheduling() and
105*9880d681SAndroid Build Coastguard Worker // insert a PostRA scheduling pass wherever it wants.
106*9880d681SAndroid Build Coastguard Worker cl::opt<bool> MISchedPostRA("misched-postra", cl::Hidden,
107*9880d681SAndroid Build Coastguard Worker   cl::desc("Run MachineScheduler post regalloc (independent of preRA sched)"));
108*9880d681SAndroid Build Coastguard Worker 
109*9880d681SAndroid Build Coastguard Worker // Experimental option to run live interval analysis early.
110*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden,
111*9880d681SAndroid Build Coastguard Worker     cl::desc("Run live interval analysis earlier in the pipeline"));
112*9880d681SAndroid Build Coastguard Worker 
113*9880d681SAndroid Build Coastguard Worker // Experimental option to use CFL-AA in codegen
114*9880d681SAndroid Build Coastguard Worker enum class CFLAAType { None, Steensgaard, Andersen, Both };
115*9880d681SAndroid Build Coastguard Worker static cl::opt<CFLAAType> UseCFLAA(
116*9880d681SAndroid Build Coastguard Worker     "use-cfl-aa-in-codegen", cl::init(CFLAAType::None), cl::Hidden,
117*9880d681SAndroid Build Coastguard Worker     cl::desc("Enable the new, experimental CFL alias analysis in CodeGen"),
118*9880d681SAndroid Build Coastguard Worker     cl::values(clEnumValN(CFLAAType::None, "none", "Disable CFL-AA"),
119*9880d681SAndroid Build Coastguard Worker                clEnumValN(CFLAAType::Steensgaard, "steens",
120*9880d681SAndroid Build Coastguard Worker                           "Enable unification-based CFL-AA"),
121*9880d681SAndroid Build Coastguard Worker                clEnumValN(CFLAAType::Andersen, "anders",
122*9880d681SAndroid Build Coastguard Worker                           "Enable inclusion-based CFL-AA"),
123*9880d681SAndroid Build Coastguard Worker                clEnumValN(CFLAAType::Both, "both",
124*9880d681SAndroid Build Coastguard Worker                           "Enable both variants of CFL-AA"),
125*9880d681SAndroid Build Coastguard Worker                clEnumValEnd));
126*9880d681SAndroid Build Coastguard Worker 
127*9880d681SAndroid Build Coastguard Worker /// Allow standard passes to be disabled by command line options. This supports
128*9880d681SAndroid Build Coastguard Worker /// simple binary flags that either suppress the pass or do nothing.
129*9880d681SAndroid Build Coastguard Worker /// i.e. -disable-mypass=false has no effect.
130*9880d681SAndroid Build Coastguard Worker /// These should be converted to boolOrDefault in order to use applyOverride.
applyDisable(IdentifyingPassPtr PassID,bool Override)131*9880d681SAndroid Build Coastguard Worker static IdentifyingPassPtr applyDisable(IdentifyingPassPtr PassID,
132*9880d681SAndroid Build Coastguard Worker                                        bool Override) {
133*9880d681SAndroid Build Coastguard Worker   if (Override)
134*9880d681SAndroid Build Coastguard Worker     return IdentifyingPassPtr();
135*9880d681SAndroid Build Coastguard Worker   return PassID;
136*9880d681SAndroid Build Coastguard Worker }
137*9880d681SAndroid Build Coastguard Worker 
138*9880d681SAndroid Build Coastguard Worker /// Allow standard passes to be disabled by the command line, regardless of who
139*9880d681SAndroid Build Coastguard Worker /// is adding the pass.
140*9880d681SAndroid Build Coastguard Worker ///
141*9880d681SAndroid Build Coastguard Worker /// StandardID is the pass identified in the standard pass pipeline and provided
142*9880d681SAndroid Build Coastguard Worker /// to addPass(). It may be a target-specific ID in the case that the target
143*9880d681SAndroid Build Coastguard Worker /// directly adds its own pass, but in that case we harmlessly fall through.
144*9880d681SAndroid Build Coastguard Worker ///
145*9880d681SAndroid Build Coastguard Worker /// TargetID is the pass that the target has configured to override StandardID.
146*9880d681SAndroid Build Coastguard Worker ///
147*9880d681SAndroid Build Coastguard Worker /// StandardID may be a pseudo ID. In that case TargetID is the name of the real
148*9880d681SAndroid Build Coastguard Worker /// pass to run. This allows multiple options to control a single pass depending
149*9880d681SAndroid Build Coastguard Worker /// on where in the pipeline that pass is added.
overridePass(AnalysisID StandardID,IdentifyingPassPtr TargetID)150*9880d681SAndroid Build Coastguard Worker static IdentifyingPassPtr overridePass(AnalysisID StandardID,
151*9880d681SAndroid Build Coastguard Worker                                        IdentifyingPassPtr TargetID) {
152*9880d681SAndroid Build Coastguard Worker   if (StandardID == &PostRASchedulerID)
153*9880d681SAndroid Build Coastguard Worker     return applyDisable(TargetID, DisablePostRA);
154*9880d681SAndroid Build Coastguard Worker 
155*9880d681SAndroid Build Coastguard Worker   if (StandardID == &BranchFolderPassID)
156*9880d681SAndroid Build Coastguard Worker     return applyDisable(TargetID, DisableBranchFold);
157*9880d681SAndroid Build Coastguard Worker 
158*9880d681SAndroid Build Coastguard Worker   if (StandardID == &TailDuplicateID)
159*9880d681SAndroid Build Coastguard Worker     return applyDisable(TargetID, DisableTailDuplicate);
160*9880d681SAndroid Build Coastguard Worker 
161*9880d681SAndroid Build Coastguard Worker   if (StandardID == &TargetPassConfig::EarlyTailDuplicateID)
162*9880d681SAndroid Build Coastguard Worker     return applyDisable(TargetID, DisableEarlyTailDup);
163*9880d681SAndroid Build Coastguard Worker 
164*9880d681SAndroid Build Coastguard Worker   if (StandardID == &MachineBlockPlacementID)
165*9880d681SAndroid Build Coastguard Worker     return applyDisable(TargetID, DisableBlockPlacement);
166*9880d681SAndroid Build Coastguard Worker 
167*9880d681SAndroid Build Coastguard Worker   if (StandardID == &StackSlotColoringID)
168*9880d681SAndroid Build Coastguard Worker     return applyDisable(TargetID, DisableSSC);
169*9880d681SAndroid Build Coastguard Worker 
170*9880d681SAndroid Build Coastguard Worker   if (StandardID == &DeadMachineInstructionElimID)
171*9880d681SAndroid Build Coastguard Worker     return applyDisable(TargetID, DisableMachineDCE);
172*9880d681SAndroid Build Coastguard Worker 
173*9880d681SAndroid Build Coastguard Worker   if (StandardID == &EarlyIfConverterID)
174*9880d681SAndroid Build Coastguard Worker     return applyDisable(TargetID, DisableEarlyIfConversion);
175*9880d681SAndroid Build Coastguard Worker 
176*9880d681SAndroid Build Coastguard Worker   if (StandardID == &MachineLICMID)
177*9880d681SAndroid Build Coastguard Worker     return applyDisable(TargetID, DisableMachineLICM);
178*9880d681SAndroid Build Coastguard Worker 
179*9880d681SAndroid Build Coastguard Worker   if (StandardID == &MachineCSEID)
180*9880d681SAndroid Build Coastguard Worker     return applyDisable(TargetID, DisableMachineCSE);
181*9880d681SAndroid Build Coastguard Worker 
182*9880d681SAndroid Build Coastguard Worker   if (StandardID == &TargetPassConfig::PostRAMachineLICMID)
183*9880d681SAndroid Build Coastguard Worker     return applyDisable(TargetID, DisablePostRAMachineLICM);
184*9880d681SAndroid Build Coastguard Worker 
185*9880d681SAndroid Build Coastguard Worker   if (StandardID == &MachineSinkingID)
186*9880d681SAndroid Build Coastguard Worker     return applyDisable(TargetID, DisableMachineSink);
187*9880d681SAndroid Build Coastguard Worker 
188*9880d681SAndroid Build Coastguard Worker   if (StandardID == &MachineCopyPropagationID)
189*9880d681SAndroid Build Coastguard Worker     return applyDisable(TargetID, DisableCopyProp);
190*9880d681SAndroid Build Coastguard Worker 
191*9880d681SAndroid Build Coastguard Worker   return TargetID;
192*9880d681SAndroid Build Coastguard Worker }
193*9880d681SAndroid Build Coastguard Worker 
194*9880d681SAndroid Build Coastguard Worker //===---------------------------------------------------------------------===//
195*9880d681SAndroid Build Coastguard Worker /// TargetPassConfig
196*9880d681SAndroid Build Coastguard Worker //===---------------------------------------------------------------------===//
197*9880d681SAndroid Build Coastguard Worker 
198*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(TargetPassConfig, "targetpassconfig",
199*9880d681SAndroid Build Coastguard Worker                 "Target Pass Configuration", false, false)
200*9880d681SAndroid Build Coastguard Worker char TargetPassConfig::ID = 0;
201*9880d681SAndroid Build Coastguard Worker 
202*9880d681SAndroid Build Coastguard Worker // Pseudo Pass IDs.
203*9880d681SAndroid Build Coastguard Worker char TargetPassConfig::EarlyTailDuplicateID = 0;
204*9880d681SAndroid Build Coastguard Worker char TargetPassConfig::PostRAMachineLICMID = 0;
205*9880d681SAndroid Build Coastguard Worker 
206*9880d681SAndroid Build Coastguard Worker namespace {
207*9880d681SAndroid Build Coastguard Worker struct InsertedPass {
208*9880d681SAndroid Build Coastguard Worker   AnalysisID TargetPassID;
209*9880d681SAndroid Build Coastguard Worker   IdentifyingPassPtr InsertedPassID;
210*9880d681SAndroid Build Coastguard Worker   bool VerifyAfter;
211*9880d681SAndroid Build Coastguard Worker   bool PrintAfter;
212*9880d681SAndroid Build Coastguard Worker 
InsertedPass__anonb3ad4f200111::InsertedPass213*9880d681SAndroid Build Coastguard Worker   InsertedPass(AnalysisID TargetPassID, IdentifyingPassPtr InsertedPassID,
214*9880d681SAndroid Build Coastguard Worker                bool VerifyAfter, bool PrintAfter)
215*9880d681SAndroid Build Coastguard Worker       : TargetPassID(TargetPassID), InsertedPassID(InsertedPassID),
216*9880d681SAndroid Build Coastguard Worker         VerifyAfter(VerifyAfter), PrintAfter(PrintAfter) {}
217*9880d681SAndroid Build Coastguard Worker 
getInsertedPass__anonb3ad4f200111::InsertedPass218*9880d681SAndroid Build Coastguard Worker   Pass *getInsertedPass() const {
219*9880d681SAndroid Build Coastguard Worker     assert(InsertedPassID.isValid() && "Illegal Pass ID!");
220*9880d681SAndroid Build Coastguard Worker     if (InsertedPassID.isInstance())
221*9880d681SAndroid Build Coastguard Worker       return InsertedPassID.getInstance();
222*9880d681SAndroid Build Coastguard Worker     Pass *NP = Pass::createPass(InsertedPassID.getID());
223*9880d681SAndroid Build Coastguard Worker     assert(NP && "Pass ID not registered");
224*9880d681SAndroid Build Coastguard Worker     return NP;
225*9880d681SAndroid Build Coastguard Worker   }
226*9880d681SAndroid Build Coastguard Worker };
227*9880d681SAndroid Build Coastguard Worker }
228*9880d681SAndroid Build Coastguard Worker 
229*9880d681SAndroid Build Coastguard Worker namespace llvm {
230*9880d681SAndroid Build Coastguard Worker class PassConfigImpl {
231*9880d681SAndroid Build Coastguard Worker public:
232*9880d681SAndroid Build Coastguard Worker   // List of passes explicitly substituted by this target. Normally this is
233*9880d681SAndroid Build Coastguard Worker   // empty, but it is a convenient way to suppress or replace specific passes
234*9880d681SAndroid Build Coastguard Worker   // that are part of a standard pass pipeline without overridding the entire
235*9880d681SAndroid Build Coastguard Worker   // pipeline. This mechanism allows target options to inherit a standard pass's
236*9880d681SAndroid Build Coastguard Worker   // user interface. For example, a target may disable a standard pass by
237*9880d681SAndroid Build Coastguard Worker   // default by substituting a pass ID of zero, and the user may still enable
238*9880d681SAndroid Build Coastguard Worker   // that standard pass with an explicit command line option.
239*9880d681SAndroid Build Coastguard Worker   DenseMap<AnalysisID,IdentifyingPassPtr> TargetPasses;
240*9880d681SAndroid Build Coastguard Worker 
241*9880d681SAndroid Build Coastguard Worker   /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass
242*9880d681SAndroid Build Coastguard Worker   /// is inserted after each instance of the first one.
243*9880d681SAndroid Build Coastguard Worker   SmallVector<InsertedPass, 4> InsertedPasses;
244*9880d681SAndroid Build Coastguard Worker };
245*9880d681SAndroid Build Coastguard Worker } // namespace llvm
246*9880d681SAndroid Build Coastguard Worker 
247*9880d681SAndroid Build Coastguard Worker // Out of line virtual method.
~TargetPassConfig()248*9880d681SAndroid Build Coastguard Worker TargetPassConfig::~TargetPassConfig() {
249*9880d681SAndroid Build Coastguard Worker   delete Impl;
250*9880d681SAndroid Build Coastguard Worker }
251*9880d681SAndroid Build Coastguard Worker 
252*9880d681SAndroid Build Coastguard Worker // Out of line constructor provides default values for pass options and
253*9880d681SAndroid Build Coastguard Worker // registers all common codegen passes.
TargetPassConfig(TargetMachine * tm,PassManagerBase & pm)254*9880d681SAndroid Build Coastguard Worker TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm)
255*9880d681SAndroid Build Coastguard Worker     : ImmutablePass(ID), PM(&pm), StartBefore(nullptr), StartAfter(nullptr),
256*9880d681SAndroid Build Coastguard Worker       StopAfter(nullptr), Started(true), Stopped(false),
257*9880d681SAndroid Build Coastguard Worker       AddingMachinePasses(false), TM(tm), Impl(nullptr), Initialized(false),
258*9880d681SAndroid Build Coastguard Worker       DisableVerify(false), EnableTailMerge(true) {
259*9880d681SAndroid Build Coastguard Worker 
260*9880d681SAndroid Build Coastguard Worker   Impl = new PassConfigImpl();
261*9880d681SAndroid Build Coastguard Worker 
262*9880d681SAndroid Build Coastguard Worker   // Register all target independent codegen passes to activate their PassIDs,
263*9880d681SAndroid Build Coastguard Worker   // including this pass itself.
264*9880d681SAndroid Build Coastguard Worker   initializeCodeGen(*PassRegistry::getPassRegistry());
265*9880d681SAndroid Build Coastguard Worker 
266*9880d681SAndroid Build Coastguard Worker   // Also register alias analysis passes required by codegen passes.
267*9880d681SAndroid Build Coastguard Worker   initializeBasicAAWrapperPassPass(*PassRegistry::getPassRegistry());
268*9880d681SAndroid Build Coastguard Worker   initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
269*9880d681SAndroid Build Coastguard Worker 
270*9880d681SAndroid Build Coastguard Worker   // Substitute Pseudo Pass IDs for real ones.
271*9880d681SAndroid Build Coastguard Worker   substitutePass(&EarlyTailDuplicateID, &TailDuplicateID);
272*9880d681SAndroid Build Coastguard Worker   substitutePass(&PostRAMachineLICMID, &MachineLICMID);
273*9880d681SAndroid Build Coastguard Worker 
274*9880d681SAndroid Build Coastguard Worker   if (StringRef(PrintMachineInstrs.getValue()).equals(""))
275*9880d681SAndroid Build Coastguard Worker     TM->Options.PrintMachineCode = true;
276*9880d681SAndroid Build Coastguard Worker }
277*9880d681SAndroid Build Coastguard Worker 
getOptLevel() const278*9880d681SAndroid Build Coastguard Worker CodeGenOpt::Level TargetPassConfig::getOptLevel() const {
279*9880d681SAndroid Build Coastguard Worker   return TM->getOptLevel();
280*9880d681SAndroid Build Coastguard Worker }
281*9880d681SAndroid Build Coastguard Worker 
282*9880d681SAndroid Build Coastguard Worker /// Insert InsertedPassID pass after TargetPassID.
insertPass(AnalysisID TargetPassID,IdentifyingPassPtr InsertedPassID,bool VerifyAfter,bool PrintAfter)283*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::insertPass(AnalysisID TargetPassID,
284*9880d681SAndroid Build Coastguard Worker                                   IdentifyingPassPtr InsertedPassID,
285*9880d681SAndroid Build Coastguard Worker                                   bool VerifyAfter, bool PrintAfter) {
286*9880d681SAndroid Build Coastguard Worker   assert(((!InsertedPassID.isInstance() &&
287*9880d681SAndroid Build Coastguard Worker            TargetPassID != InsertedPassID.getID()) ||
288*9880d681SAndroid Build Coastguard Worker           (InsertedPassID.isInstance() &&
289*9880d681SAndroid Build Coastguard Worker            TargetPassID != InsertedPassID.getInstance()->getPassID())) &&
290*9880d681SAndroid Build Coastguard Worker          "Insert a pass after itself!");
291*9880d681SAndroid Build Coastguard Worker   Impl->InsertedPasses.emplace_back(TargetPassID, InsertedPassID, VerifyAfter,
292*9880d681SAndroid Build Coastguard Worker                                     PrintAfter);
293*9880d681SAndroid Build Coastguard Worker }
294*9880d681SAndroid Build Coastguard Worker 
295*9880d681SAndroid Build Coastguard Worker /// createPassConfig - Create a pass configuration object to be used by
296*9880d681SAndroid Build Coastguard Worker /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
297*9880d681SAndroid Build Coastguard Worker ///
298*9880d681SAndroid Build Coastguard Worker /// Targets may override this to extend TargetPassConfig.
createPassConfig(PassManagerBase & PM)299*9880d681SAndroid Build Coastguard Worker TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
300*9880d681SAndroid Build Coastguard Worker   return new TargetPassConfig(this, PM);
301*9880d681SAndroid Build Coastguard Worker }
302*9880d681SAndroid Build Coastguard Worker 
TargetPassConfig()303*9880d681SAndroid Build Coastguard Worker TargetPassConfig::TargetPassConfig()
304*9880d681SAndroid Build Coastguard Worker   : ImmutablePass(ID), PM(nullptr) {
305*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("TargetPassConfig should not be constructed on-the-fly");
306*9880d681SAndroid Build Coastguard Worker }
307*9880d681SAndroid Build Coastguard Worker 
308*9880d681SAndroid Build Coastguard Worker // Helper to verify the analysis is really immutable.
setOpt(bool & Opt,bool Val)309*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::setOpt(bool &Opt, bool Val) {
310*9880d681SAndroid Build Coastguard Worker   assert(!Initialized && "PassConfig is immutable");
311*9880d681SAndroid Build Coastguard Worker   Opt = Val;
312*9880d681SAndroid Build Coastguard Worker }
313*9880d681SAndroid Build Coastguard Worker 
substitutePass(AnalysisID StandardID,IdentifyingPassPtr TargetID)314*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::substitutePass(AnalysisID StandardID,
315*9880d681SAndroid Build Coastguard Worker                                       IdentifyingPassPtr TargetID) {
316*9880d681SAndroid Build Coastguard Worker   Impl->TargetPasses[StandardID] = TargetID;
317*9880d681SAndroid Build Coastguard Worker }
318*9880d681SAndroid Build Coastguard Worker 
getPassSubstitution(AnalysisID ID) const319*9880d681SAndroid Build Coastguard Worker IdentifyingPassPtr TargetPassConfig::getPassSubstitution(AnalysisID ID) const {
320*9880d681SAndroid Build Coastguard Worker   DenseMap<AnalysisID, IdentifyingPassPtr>::const_iterator
321*9880d681SAndroid Build Coastguard Worker     I = Impl->TargetPasses.find(ID);
322*9880d681SAndroid Build Coastguard Worker   if (I == Impl->TargetPasses.end())
323*9880d681SAndroid Build Coastguard Worker     return ID;
324*9880d681SAndroid Build Coastguard Worker   return I->second;
325*9880d681SAndroid Build Coastguard Worker }
326*9880d681SAndroid Build Coastguard Worker 
isPassSubstitutedOrOverridden(AnalysisID ID) const327*9880d681SAndroid Build Coastguard Worker bool TargetPassConfig::isPassSubstitutedOrOverridden(AnalysisID ID) const {
328*9880d681SAndroid Build Coastguard Worker   IdentifyingPassPtr TargetID = getPassSubstitution(ID);
329*9880d681SAndroid Build Coastguard Worker   IdentifyingPassPtr FinalPtr = overridePass(ID, TargetID);
330*9880d681SAndroid Build Coastguard Worker   return !FinalPtr.isValid() || FinalPtr.isInstance() ||
331*9880d681SAndroid Build Coastguard Worker       FinalPtr.getID() != ID;
332*9880d681SAndroid Build Coastguard Worker }
333*9880d681SAndroid Build Coastguard Worker 
334*9880d681SAndroid Build Coastguard Worker /// Add a pass to the PassManager if that pass is supposed to be run.  If the
335*9880d681SAndroid Build Coastguard Worker /// Started/Stopped flags indicate either that the compilation should start at
336*9880d681SAndroid Build Coastguard Worker /// a later pass or that it should stop after an earlier pass, then do not add
337*9880d681SAndroid Build Coastguard Worker /// the pass.  Finally, compare the current pass against the StartAfter
338*9880d681SAndroid Build Coastguard Worker /// and StopAfter options and change the Started/Stopped flags accordingly.
addPass(Pass * P,bool verifyAfter,bool printAfter)339*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::addPass(Pass *P, bool verifyAfter, bool printAfter) {
340*9880d681SAndroid Build Coastguard Worker   assert(!Initialized && "PassConfig is immutable");
341*9880d681SAndroid Build Coastguard Worker 
342*9880d681SAndroid Build Coastguard Worker   // Cache the Pass ID here in case the pass manager finds this pass is
343*9880d681SAndroid Build Coastguard Worker   // redundant with ones already scheduled / available, and deletes it.
344*9880d681SAndroid Build Coastguard Worker   // Fundamentally, once we add the pass to the manager, we no longer own it
345*9880d681SAndroid Build Coastguard Worker   // and shouldn't reference it.
346*9880d681SAndroid Build Coastguard Worker   AnalysisID PassID = P->getPassID();
347*9880d681SAndroid Build Coastguard Worker 
348*9880d681SAndroid Build Coastguard Worker   if (StartBefore == PassID)
349*9880d681SAndroid Build Coastguard Worker     Started = true;
350*9880d681SAndroid Build Coastguard Worker   if (Started && !Stopped) {
351*9880d681SAndroid Build Coastguard Worker     std::string Banner;
352*9880d681SAndroid Build Coastguard Worker     // Construct banner message before PM->add() as that may delete the pass.
353*9880d681SAndroid Build Coastguard Worker     if (AddingMachinePasses && (printAfter || verifyAfter))
354*9880d681SAndroid Build Coastguard Worker       Banner = std::string("After ") + std::string(P->getPassName());
355*9880d681SAndroid Build Coastguard Worker     PM->add(P);
356*9880d681SAndroid Build Coastguard Worker     if (AddingMachinePasses) {
357*9880d681SAndroid Build Coastguard Worker       if (printAfter)
358*9880d681SAndroid Build Coastguard Worker         addPrintPass(Banner);
359*9880d681SAndroid Build Coastguard Worker       if (verifyAfter)
360*9880d681SAndroid Build Coastguard Worker         addVerifyPass(Banner);
361*9880d681SAndroid Build Coastguard Worker     }
362*9880d681SAndroid Build Coastguard Worker 
363*9880d681SAndroid Build Coastguard Worker     // Add the passes after the pass P if there is any.
364*9880d681SAndroid Build Coastguard Worker     for (auto IP : Impl->InsertedPasses) {
365*9880d681SAndroid Build Coastguard Worker       if (IP.TargetPassID == PassID)
366*9880d681SAndroid Build Coastguard Worker         addPass(IP.getInsertedPass(), IP.VerifyAfter, IP.PrintAfter);
367*9880d681SAndroid Build Coastguard Worker     }
368*9880d681SAndroid Build Coastguard Worker   } else {
369*9880d681SAndroid Build Coastguard Worker     delete P;
370*9880d681SAndroid Build Coastguard Worker   }
371*9880d681SAndroid Build Coastguard Worker   if (StopAfter == PassID)
372*9880d681SAndroid Build Coastguard Worker     Stopped = true;
373*9880d681SAndroid Build Coastguard Worker   if (StartAfter == PassID)
374*9880d681SAndroid Build Coastguard Worker     Started = true;
375*9880d681SAndroid Build Coastguard Worker   if (Stopped && !Started)
376*9880d681SAndroid Build Coastguard Worker     report_fatal_error("Cannot stop compilation after pass that is not run");
377*9880d681SAndroid Build Coastguard Worker }
378*9880d681SAndroid Build Coastguard Worker 
379*9880d681SAndroid Build Coastguard Worker /// Add a CodeGen pass at this point in the pipeline after checking for target
380*9880d681SAndroid Build Coastguard Worker /// and command line overrides.
381*9880d681SAndroid Build Coastguard Worker ///
382*9880d681SAndroid Build Coastguard Worker /// addPass cannot return a pointer to the pass instance because is internal the
383*9880d681SAndroid Build Coastguard Worker /// PassManager and the instance we create here may already be freed.
addPass(AnalysisID PassID,bool verifyAfter,bool printAfter)384*9880d681SAndroid Build Coastguard Worker AnalysisID TargetPassConfig::addPass(AnalysisID PassID, bool verifyAfter,
385*9880d681SAndroid Build Coastguard Worker                                      bool printAfter) {
386*9880d681SAndroid Build Coastguard Worker   IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
387*9880d681SAndroid Build Coastguard Worker   IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID);
388*9880d681SAndroid Build Coastguard Worker   if (!FinalPtr.isValid())
389*9880d681SAndroid Build Coastguard Worker     return nullptr;
390*9880d681SAndroid Build Coastguard Worker 
391*9880d681SAndroid Build Coastguard Worker   Pass *P;
392*9880d681SAndroid Build Coastguard Worker   if (FinalPtr.isInstance())
393*9880d681SAndroid Build Coastguard Worker     P = FinalPtr.getInstance();
394*9880d681SAndroid Build Coastguard Worker   else {
395*9880d681SAndroid Build Coastguard Worker     P = Pass::createPass(FinalPtr.getID());
396*9880d681SAndroid Build Coastguard Worker     if (!P)
397*9880d681SAndroid Build Coastguard Worker       llvm_unreachable("Pass ID not registered");
398*9880d681SAndroid Build Coastguard Worker   }
399*9880d681SAndroid Build Coastguard Worker   AnalysisID FinalID = P->getPassID();
400*9880d681SAndroid Build Coastguard Worker   addPass(P, verifyAfter, printAfter); // Ends the lifetime of P.
401*9880d681SAndroid Build Coastguard Worker 
402*9880d681SAndroid Build Coastguard Worker   return FinalID;
403*9880d681SAndroid Build Coastguard Worker }
404*9880d681SAndroid Build Coastguard Worker 
printAndVerify(const std::string & Banner)405*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::printAndVerify(const std::string &Banner) {
406*9880d681SAndroid Build Coastguard Worker   addPrintPass(Banner);
407*9880d681SAndroid Build Coastguard Worker   addVerifyPass(Banner);
408*9880d681SAndroid Build Coastguard Worker }
409*9880d681SAndroid Build Coastguard Worker 
addPrintPass(const std::string & Banner)410*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::addPrintPass(const std::string &Banner) {
411*9880d681SAndroid Build Coastguard Worker   if (TM->shouldPrintMachineCode())
412*9880d681SAndroid Build Coastguard Worker     PM->add(createMachineFunctionPrinterPass(dbgs(), Banner));
413*9880d681SAndroid Build Coastguard Worker }
414*9880d681SAndroid Build Coastguard Worker 
addVerifyPass(const std::string & Banner)415*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::addVerifyPass(const std::string &Banner) {
416*9880d681SAndroid Build Coastguard Worker   if (VerifyMachineCode)
417*9880d681SAndroid Build Coastguard Worker     PM->add(createMachineVerifierPass(Banner));
418*9880d681SAndroid Build Coastguard Worker }
419*9880d681SAndroid Build Coastguard Worker 
420*9880d681SAndroid Build Coastguard Worker /// Add common target configurable passes that perform LLVM IR to IR transforms
421*9880d681SAndroid Build Coastguard Worker /// following machine independent optimization.
addIRPasses()422*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::addIRPasses() {
423*9880d681SAndroid Build Coastguard Worker   switch (UseCFLAA) {
424*9880d681SAndroid Build Coastguard Worker   case CFLAAType::Steensgaard:
425*9880d681SAndroid Build Coastguard Worker     addPass(createCFLSteensAAWrapperPass());
426*9880d681SAndroid Build Coastguard Worker     break;
427*9880d681SAndroid Build Coastguard Worker   case CFLAAType::Andersen:
428*9880d681SAndroid Build Coastguard Worker     addPass(createCFLAndersAAWrapperPass());
429*9880d681SAndroid Build Coastguard Worker     break;
430*9880d681SAndroid Build Coastguard Worker   case CFLAAType::Both:
431*9880d681SAndroid Build Coastguard Worker     addPass(createCFLAndersAAWrapperPass());
432*9880d681SAndroid Build Coastguard Worker     addPass(createCFLSteensAAWrapperPass());
433*9880d681SAndroid Build Coastguard Worker     break;
434*9880d681SAndroid Build Coastguard Worker   default:
435*9880d681SAndroid Build Coastguard Worker     break;
436*9880d681SAndroid Build Coastguard Worker   }
437*9880d681SAndroid Build Coastguard Worker 
438*9880d681SAndroid Build Coastguard Worker   // Basic AliasAnalysis support.
439*9880d681SAndroid Build Coastguard Worker   // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
440*9880d681SAndroid Build Coastguard Worker   // BasicAliasAnalysis wins if they disagree. This is intended to help
441*9880d681SAndroid Build Coastguard Worker   // support "obvious" type-punning idioms.
442*9880d681SAndroid Build Coastguard Worker   addPass(createTypeBasedAAWrapperPass());
443*9880d681SAndroid Build Coastguard Worker   addPass(createScopedNoAliasAAWrapperPass());
444*9880d681SAndroid Build Coastguard Worker   addPass(createBasicAAWrapperPass());
445*9880d681SAndroid Build Coastguard Worker 
446*9880d681SAndroid Build Coastguard Worker   // Before running any passes, run the verifier to determine if the input
447*9880d681SAndroid Build Coastguard Worker   // coming from the front-end and/or optimizer is valid.
448*9880d681SAndroid Build Coastguard Worker   if (!DisableVerify)
449*9880d681SAndroid Build Coastguard Worker     addPass(createVerifierPass());
450*9880d681SAndroid Build Coastguard Worker 
451*9880d681SAndroid Build Coastguard Worker   // Run loop strength reduction before anything else.
452*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
453*9880d681SAndroid Build Coastguard Worker     addPass(createLoopStrengthReducePass());
454*9880d681SAndroid Build Coastguard Worker     if (PrintLSR)
455*9880d681SAndroid Build Coastguard Worker       addPass(createPrintFunctionPass(dbgs(), "\n\n*** Code after LSR ***\n"));
456*9880d681SAndroid Build Coastguard Worker   }
457*9880d681SAndroid Build Coastguard Worker 
458*9880d681SAndroid Build Coastguard Worker   // Run GC lowering passes for builtin collectors
459*9880d681SAndroid Build Coastguard Worker   // TODO: add a pass insertion point here
460*9880d681SAndroid Build Coastguard Worker   addPass(createGCLoweringPass());
461*9880d681SAndroid Build Coastguard Worker   addPass(createShadowStackGCLoweringPass());
462*9880d681SAndroid Build Coastguard Worker 
463*9880d681SAndroid Build Coastguard Worker   // Make sure that no unreachable blocks are instruction selected.
464*9880d681SAndroid Build Coastguard Worker   addPass(createUnreachableBlockEliminationPass());
465*9880d681SAndroid Build Coastguard Worker 
466*9880d681SAndroid Build Coastguard Worker   // Prepare expensive constants for SelectionDAG.
467*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None && !DisableConstantHoisting)
468*9880d681SAndroid Build Coastguard Worker     addPass(createConstantHoistingPass());
469*9880d681SAndroid Build Coastguard Worker 
470*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None && !DisablePartialLibcallInlining)
471*9880d681SAndroid Build Coastguard Worker     addPass(createPartiallyInlineLibCallsPass());
472*9880d681SAndroid Build Coastguard Worker }
473*9880d681SAndroid Build Coastguard Worker 
474*9880d681SAndroid Build Coastguard Worker /// Turn exception handling constructs into something the code generators can
475*9880d681SAndroid Build Coastguard Worker /// handle.
addPassesToHandleExceptions()476*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::addPassesToHandleExceptions() {
477*9880d681SAndroid Build Coastguard Worker   switch (TM->getMCAsmInfo()->getExceptionHandlingType()) {
478*9880d681SAndroid Build Coastguard Worker   case ExceptionHandling::SjLj:
479*9880d681SAndroid Build Coastguard Worker     // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
480*9880d681SAndroid Build Coastguard Worker     // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
481*9880d681SAndroid Build Coastguard Worker     // catch info can get misplaced when a selector ends up more than one block
482*9880d681SAndroid Build Coastguard Worker     // removed from the parent invoke(s). This could happen when a landing
483*9880d681SAndroid Build Coastguard Worker     // pad is shared by multiple invokes and is also a target of a normal
484*9880d681SAndroid Build Coastguard Worker     // edge from elsewhere.
485*9880d681SAndroid Build Coastguard Worker     addPass(createSjLjEHPreparePass());
486*9880d681SAndroid Build Coastguard Worker     // FALLTHROUGH
487*9880d681SAndroid Build Coastguard Worker   case ExceptionHandling::DwarfCFI:
488*9880d681SAndroid Build Coastguard Worker   case ExceptionHandling::ARM:
489*9880d681SAndroid Build Coastguard Worker     addPass(createDwarfEHPass(TM));
490*9880d681SAndroid Build Coastguard Worker     break;
491*9880d681SAndroid Build Coastguard Worker   case ExceptionHandling::WinEH:
492*9880d681SAndroid Build Coastguard Worker     // We support using both GCC-style and MSVC-style exceptions on Windows, so
493*9880d681SAndroid Build Coastguard Worker     // add both preparation passes. Each pass will only actually run if it
494*9880d681SAndroid Build Coastguard Worker     // recognizes the personality function.
495*9880d681SAndroid Build Coastguard Worker     addPass(createWinEHPass(TM));
496*9880d681SAndroid Build Coastguard Worker     addPass(createDwarfEHPass(TM));
497*9880d681SAndroid Build Coastguard Worker     break;
498*9880d681SAndroid Build Coastguard Worker   case ExceptionHandling::None:
499*9880d681SAndroid Build Coastguard Worker     addPass(createLowerInvokePass());
500*9880d681SAndroid Build Coastguard Worker 
501*9880d681SAndroid Build Coastguard Worker     // The lower invoke pass may create unreachable code. Remove it.
502*9880d681SAndroid Build Coastguard Worker     addPass(createUnreachableBlockEliminationPass());
503*9880d681SAndroid Build Coastguard Worker     break;
504*9880d681SAndroid Build Coastguard Worker   }
505*9880d681SAndroid Build Coastguard Worker }
506*9880d681SAndroid Build Coastguard Worker 
507*9880d681SAndroid Build Coastguard Worker /// Add pass to prepare the LLVM IR for code generation. This should be done
508*9880d681SAndroid Build Coastguard Worker /// before exception handling preparation passes.
addCodeGenPrepare()509*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::addCodeGenPrepare() {
510*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
511*9880d681SAndroid Build Coastguard Worker     addPass(createCodeGenPreparePass(TM));
512*9880d681SAndroid Build Coastguard Worker   addPass(createRewriteSymbolsPass());
513*9880d681SAndroid Build Coastguard Worker }
514*9880d681SAndroid Build Coastguard Worker 
515*9880d681SAndroid Build Coastguard Worker /// Add common passes that perform LLVM IR to IR transforms in preparation for
516*9880d681SAndroid Build Coastguard Worker /// instruction selection.
addISelPrepare()517*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::addISelPrepare() {
518*9880d681SAndroid Build Coastguard Worker   addPreISel();
519*9880d681SAndroid Build Coastguard Worker 
520*9880d681SAndroid Build Coastguard Worker   // Force codegen to run according to the callgraph.
521*9880d681SAndroid Build Coastguard Worker   if (TM->Options.EnableIPRA)
522*9880d681SAndroid Build Coastguard Worker     addPass(new DummyCGSCCPass);
523*9880d681SAndroid Build Coastguard Worker 
524*9880d681SAndroid Build Coastguard Worker   // Add both the safe stack and the stack protection passes: each of them will
525*9880d681SAndroid Build Coastguard Worker   // only protect functions that have corresponding attributes.
526*9880d681SAndroid Build Coastguard Worker   addPass(createSafeStackPass(TM));
527*9880d681SAndroid Build Coastguard Worker   addPass(createStackProtectorPass(TM));
528*9880d681SAndroid Build Coastguard Worker 
529*9880d681SAndroid Build Coastguard Worker   if (PrintISelInput)
530*9880d681SAndroid Build Coastguard Worker     addPass(createPrintFunctionPass(
531*9880d681SAndroid Build Coastguard Worker         dbgs(), "\n\n*** Final LLVM Code input to ISel ***\n"));
532*9880d681SAndroid Build Coastguard Worker 
533*9880d681SAndroid Build Coastguard Worker   // All passes which modify the LLVM IR are now complete; run the verifier
534*9880d681SAndroid Build Coastguard Worker   // to ensure that the IR is valid.
535*9880d681SAndroid Build Coastguard Worker   if (!DisableVerify)
536*9880d681SAndroid Build Coastguard Worker     addPass(createVerifierPass());
537*9880d681SAndroid Build Coastguard Worker }
538*9880d681SAndroid Build Coastguard Worker 
539*9880d681SAndroid Build Coastguard Worker /// Add the complete set of target-independent postISel code generator passes.
540*9880d681SAndroid Build Coastguard Worker ///
541*9880d681SAndroid Build Coastguard Worker /// This can be read as the standard order of major LLVM CodeGen stages. Stages
542*9880d681SAndroid Build Coastguard Worker /// with nontrivial configuration or multiple passes are broken out below in
543*9880d681SAndroid Build Coastguard Worker /// add%Stage routines.
544*9880d681SAndroid Build Coastguard Worker ///
545*9880d681SAndroid Build Coastguard Worker /// Any TargetPassConfig::addXX routine may be overriden by the Target. The
546*9880d681SAndroid Build Coastguard Worker /// addPre/Post methods with empty header implementations allow injecting
547*9880d681SAndroid Build Coastguard Worker /// target-specific fixups just before or after major stages. Additionally,
548*9880d681SAndroid Build Coastguard Worker /// targets have the flexibility to change pass order within a stage by
549*9880d681SAndroid Build Coastguard Worker /// overriding default implementation of add%Stage routines below. Each
550*9880d681SAndroid Build Coastguard Worker /// technique has maintainability tradeoffs because alternate pass orders are
551*9880d681SAndroid Build Coastguard Worker /// not well supported. addPre/Post works better if the target pass is easily
552*9880d681SAndroid Build Coastguard Worker /// tied to a common pass. But if it has subtle dependencies on multiple passes,
553*9880d681SAndroid Build Coastguard Worker /// the target should override the stage instead.
554*9880d681SAndroid Build Coastguard Worker ///
555*9880d681SAndroid Build Coastguard Worker /// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
556*9880d681SAndroid Build Coastguard Worker /// before/after any target-independent pass. But it's currently overkill.
addMachinePasses()557*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::addMachinePasses() {
558*9880d681SAndroid Build Coastguard Worker   AddingMachinePasses = true;
559*9880d681SAndroid Build Coastguard Worker 
560*9880d681SAndroid Build Coastguard Worker   if (TM->Options.EnableIPRA)
561*9880d681SAndroid Build Coastguard Worker     addPass(createRegUsageInfoPropPass());
562*9880d681SAndroid Build Coastguard Worker 
563*9880d681SAndroid Build Coastguard Worker   // Insert a machine instr printer pass after the specified pass.
564*9880d681SAndroid Build Coastguard Worker   if (!StringRef(PrintMachineInstrs.getValue()).equals("") &&
565*9880d681SAndroid Build Coastguard Worker       !StringRef(PrintMachineInstrs.getValue()).equals("option-unspecified")) {
566*9880d681SAndroid Build Coastguard Worker     const PassRegistry *PR = PassRegistry::getPassRegistry();
567*9880d681SAndroid Build Coastguard Worker     const PassInfo *TPI = PR->getPassInfo(PrintMachineInstrs.getValue());
568*9880d681SAndroid Build Coastguard Worker     const PassInfo *IPI = PR->getPassInfo(StringRef("machineinstr-printer"));
569*9880d681SAndroid Build Coastguard Worker     assert (TPI && IPI && "Pass ID not registered!");
570*9880d681SAndroid Build Coastguard Worker     const char *TID = (const char *)(TPI->getTypeInfo());
571*9880d681SAndroid Build Coastguard Worker     const char *IID = (const char *)(IPI->getTypeInfo());
572*9880d681SAndroid Build Coastguard Worker     insertPass(TID, IID);
573*9880d681SAndroid Build Coastguard Worker   }
574*9880d681SAndroid Build Coastguard Worker 
575*9880d681SAndroid Build Coastguard Worker   // Print the instruction selected machine code...
576*9880d681SAndroid Build Coastguard Worker   printAndVerify("After Instruction Selection");
577*9880d681SAndroid Build Coastguard Worker 
578*9880d681SAndroid Build Coastguard Worker   // Expand pseudo-instructions emitted by ISel.
579*9880d681SAndroid Build Coastguard Worker   addPass(&ExpandISelPseudosID);
580*9880d681SAndroid Build Coastguard Worker 
581*9880d681SAndroid Build Coastguard Worker   // Add passes that optimize machine instructions in SSA form.
582*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None) {
583*9880d681SAndroid Build Coastguard Worker     addMachineSSAOptimization();
584*9880d681SAndroid Build Coastguard Worker   } else {
585*9880d681SAndroid Build Coastguard Worker     // If the target requests it, assign local variables to stack slots relative
586*9880d681SAndroid Build Coastguard Worker     // to one another and simplify frame index references where possible.
587*9880d681SAndroid Build Coastguard Worker     addPass(&LocalStackSlotAllocationID, false);
588*9880d681SAndroid Build Coastguard Worker   }
589*9880d681SAndroid Build Coastguard Worker 
590*9880d681SAndroid Build Coastguard Worker   // Run pre-ra passes.
591*9880d681SAndroid Build Coastguard Worker   addPreRegAlloc();
592*9880d681SAndroid Build Coastguard Worker 
593*9880d681SAndroid Build Coastguard Worker   // Run register allocation and passes that are tightly coupled with it,
594*9880d681SAndroid Build Coastguard Worker   // including phi elimination and scheduling.
595*9880d681SAndroid Build Coastguard Worker   if (getOptimizeRegAlloc())
596*9880d681SAndroid Build Coastguard Worker     addOptimizedRegAlloc(createRegAllocPass(true));
597*9880d681SAndroid Build Coastguard Worker   else
598*9880d681SAndroid Build Coastguard Worker     addFastRegAlloc(createRegAllocPass(false));
599*9880d681SAndroid Build Coastguard Worker 
600*9880d681SAndroid Build Coastguard Worker   // Run post-ra passes.
601*9880d681SAndroid Build Coastguard Worker   addPostRegAlloc();
602*9880d681SAndroid Build Coastguard Worker 
603*9880d681SAndroid Build Coastguard Worker   // Insert prolog/epilog code.  Eliminate abstract frame index references...
604*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None)
605*9880d681SAndroid Build Coastguard Worker     addPass(&ShrinkWrapID);
606*9880d681SAndroid Build Coastguard Worker 
607*9880d681SAndroid Build Coastguard Worker   // Prolog/Epilog inserter needs a TargetMachine to instantiate. But only
608*9880d681SAndroid Build Coastguard Worker   // do so if it hasn't been disabled, substituted, or overridden.
609*9880d681SAndroid Build Coastguard Worker   if (!isPassSubstitutedOrOverridden(&PrologEpilogCodeInserterID))
610*9880d681SAndroid Build Coastguard Worker       addPass(createPrologEpilogInserterPass(TM));
611*9880d681SAndroid Build Coastguard Worker 
612*9880d681SAndroid Build Coastguard Worker   /// Add passes that optimize machine instructions after register allocation.
613*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None)
614*9880d681SAndroid Build Coastguard Worker     addMachineLateOptimization();
615*9880d681SAndroid Build Coastguard Worker 
616*9880d681SAndroid Build Coastguard Worker   // Expand pseudo instructions before second scheduling pass.
617*9880d681SAndroid Build Coastguard Worker   addPass(&ExpandPostRAPseudosID);
618*9880d681SAndroid Build Coastguard Worker 
619*9880d681SAndroid Build Coastguard Worker   // Run pre-sched2 passes.
620*9880d681SAndroid Build Coastguard Worker   addPreSched2();
621*9880d681SAndroid Build Coastguard Worker 
622*9880d681SAndroid Build Coastguard Worker   if (EnableImplicitNullChecks)
623*9880d681SAndroid Build Coastguard Worker     addPass(&ImplicitNullChecksID);
624*9880d681SAndroid Build Coastguard Worker 
625*9880d681SAndroid Build Coastguard Worker   // Second pass scheduler.
626*9880d681SAndroid Build Coastguard Worker   // Let Target optionally insert this pass by itself at some other
627*9880d681SAndroid Build Coastguard Worker   // point.
628*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None &&
629*9880d681SAndroid Build Coastguard Worker       !TM->targetSchedulesPostRAScheduling()) {
630*9880d681SAndroid Build Coastguard Worker     if (MISchedPostRA)
631*9880d681SAndroid Build Coastguard Worker       addPass(&PostMachineSchedulerID);
632*9880d681SAndroid Build Coastguard Worker     else
633*9880d681SAndroid Build Coastguard Worker       addPass(&PostRASchedulerID);
634*9880d681SAndroid Build Coastguard Worker   }
635*9880d681SAndroid Build Coastguard Worker 
636*9880d681SAndroid Build Coastguard Worker   // GC
637*9880d681SAndroid Build Coastguard Worker   if (addGCPasses()) {
638*9880d681SAndroid Build Coastguard Worker     if (PrintGCInfo)
639*9880d681SAndroid Build Coastguard Worker       addPass(createGCInfoPrinter(dbgs()), false, false);
640*9880d681SAndroid Build Coastguard Worker   }
641*9880d681SAndroid Build Coastguard Worker 
642*9880d681SAndroid Build Coastguard Worker   // Basic block placement.
643*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None)
644*9880d681SAndroid Build Coastguard Worker     addBlockPlacement();
645*9880d681SAndroid Build Coastguard Worker 
646*9880d681SAndroid Build Coastguard Worker   addPreEmitPass();
647*9880d681SAndroid Build Coastguard Worker 
648*9880d681SAndroid Build Coastguard Worker   if (TM->Options.EnableIPRA)
649*9880d681SAndroid Build Coastguard Worker     // Collect register usage information and produce a register mask of
650*9880d681SAndroid Build Coastguard Worker     // clobbered registers, to be used to optimize call sites.
651*9880d681SAndroid Build Coastguard Worker     addPass(createRegUsageInfoCollector());
652*9880d681SAndroid Build Coastguard Worker 
653*9880d681SAndroid Build Coastguard Worker   addPass(&FuncletLayoutID, false);
654*9880d681SAndroid Build Coastguard Worker 
655*9880d681SAndroid Build Coastguard Worker   addPass(&StackMapLivenessID, false);
656*9880d681SAndroid Build Coastguard Worker   addPass(&LiveDebugValuesID, false);
657*9880d681SAndroid Build Coastguard Worker 
658*9880d681SAndroid Build Coastguard Worker   addPass(&XRayInstrumentationID, false);
659*9880d681SAndroid Build Coastguard Worker   addPass(&PatchableFunctionID, false);
660*9880d681SAndroid Build Coastguard Worker 
661*9880d681SAndroid Build Coastguard Worker   AddingMachinePasses = false;
662*9880d681SAndroid Build Coastguard Worker }
663*9880d681SAndroid Build Coastguard Worker 
664*9880d681SAndroid Build Coastguard Worker /// Add passes that optimize machine instructions in SSA form.
addMachineSSAOptimization()665*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::addMachineSSAOptimization() {
666*9880d681SAndroid Build Coastguard Worker   // Pre-ra tail duplication.
667*9880d681SAndroid Build Coastguard Worker   addPass(&EarlyTailDuplicateID);
668*9880d681SAndroid Build Coastguard Worker 
669*9880d681SAndroid Build Coastguard Worker   // Optimize PHIs before DCE: removing dead PHI cycles may make more
670*9880d681SAndroid Build Coastguard Worker   // instructions dead.
671*9880d681SAndroid Build Coastguard Worker   addPass(&OptimizePHIsID, false);
672*9880d681SAndroid Build Coastguard Worker 
673*9880d681SAndroid Build Coastguard Worker   // This pass merges large allocas. StackSlotColoring is a different pass
674*9880d681SAndroid Build Coastguard Worker   // which merges spill slots.
675*9880d681SAndroid Build Coastguard Worker   addPass(&StackColoringID, false);
676*9880d681SAndroid Build Coastguard Worker 
677*9880d681SAndroid Build Coastguard Worker   // If the target requests it, assign local variables to stack slots relative
678*9880d681SAndroid Build Coastguard Worker   // to one another and simplify frame index references where possible.
679*9880d681SAndroid Build Coastguard Worker   addPass(&LocalStackSlotAllocationID, false);
680*9880d681SAndroid Build Coastguard Worker 
681*9880d681SAndroid Build Coastguard Worker   // With optimization, dead code should already be eliminated. However
682*9880d681SAndroid Build Coastguard Worker   // there is one known exception: lowered code for arguments that are only
683*9880d681SAndroid Build Coastguard Worker   // used by tail calls, where the tail calls reuse the incoming stack
684*9880d681SAndroid Build Coastguard Worker   // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
685*9880d681SAndroid Build Coastguard Worker   addPass(&DeadMachineInstructionElimID);
686*9880d681SAndroid Build Coastguard Worker 
687*9880d681SAndroid Build Coastguard Worker   // Allow targets to insert passes that improve instruction level parallelism,
688*9880d681SAndroid Build Coastguard Worker   // like if-conversion. Such passes will typically need dominator trees and
689*9880d681SAndroid Build Coastguard Worker   // loop info, just like LICM and CSE below.
690*9880d681SAndroid Build Coastguard Worker   addILPOpts();
691*9880d681SAndroid Build Coastguard Worker 
692*9880d681SAndroid Build Coastguard Worker   addPass(&MachineLICMID, false);
693*9880d681SAndroid Build Coastguard Worker   addPass(&MachineCSEID, false);
694*9880d681SAndroid Build Coastguard Worker   addPass(&MachineSinkingID);
695*9880d681SAndroid Build Coastguard Worker 
696*9880d681SAndroid Build Coastguard Worker   addPass(&PeepholeOptimizerID);
697*9880d681SAndroid Build Coastguard Worker   // Clean-up the dead code that may have been generated by peephole
698*9880d681SAndroid Build Coastguard Worker   // rewriting.
699*9880d681SAndroid Build Coastguard Worker   addPass(&DeadMachineInstructionElimID);
700*9880d681SAndroid Build Coastguard Worker }
701*9880d681SAndroid Build Coastguard Worker 
702*9880d681SAndroid Build Coastguard Worker //===---------------------------------------------------------------------===//
703*9880d681SAndroid Build Coastguard Worker /// Register Allocation Pass Configuration
704*9880d681SAndroid Build Coastguard Worker //===---------------------------------------------------------------------===//
705*9880d681SAndroid Build Coastguard Worker 
getOptimizeRegAlloc() const706*9880d681SAndroid Build Coastguard Worker bool TargetPassConfig::getOptimizeRegAlloc() const {
707*9880d681SAndroid Build Coastguard Worker   switch (OptimizeRegAlloc) {
708*9880d681SAndroid Build Coastguard Worker   case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
709*9880d681SAndroid Build Coastguard Worker   case cl::BOU_TRUE:  return true;
710*9880d681SAndroid Build Coastguard Worker   case cl::BOU_FALSE: return false;
711*9880d681SAndroid Build Coastguard Worker   }
712*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("Invalid optimize-regalloc state");
713*9880d681SAndroid Build Coastguard Worker }
714*9880d681SAndroid Build Coastguard Worker 
715*9880d681SAndroid Build Coastguard Worker /// RegisterRegAlloc's global Registry tracks allocator registration.
716*9880d681SAndroid Build Coastguard Worker MachinePassRegistry RegisterRegAlloc::Registry;
717*9880d681SAndroid Build Coastguard Worker 
718*9880d681SAndroid Build Coastguard Worker /// A dummy default pass factory indicates whether the register allocator is
719*9880d681SAndroid Build Coastguard Worker /// overridden on the command line.
720*9880d681SAndroid Build Coastguard Worker LLVM_DEFINE_ONCE_FLAG(InitializeDefaultRegisterAllocatorFlag);
useDefaultRegisterAllocator()721*9880d681SAndroid Build Coastguard Worker static FunctionPass *useDefaultRegisterAllocator() { return nullptr; }
722*9880d681SAndroid Build Coastguard Worker static RegisterRegAlloc
723*9880d681SAndroid Build Coastguard Worker defaultRegAlloc("default",
724*9880d681SAndroid Build Coastguard Worker                 "pick register allocator based on -O option",
725*9880d681SAndroid Build Coastguard Worker                 useDefaultRegisterAllocator);
726*9880d681SAndroid Build Coastguard Worker 
727*9880d681SAndroid Build Coastguard Worker /// -regalloc=... command line option.
728*9880d681SAndroid Build Coastguard Worker static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
729*9880d681SAndroid Build Coastguard Worker                RegisterPassParser<RegisterRegAlloc> >
730*9880d681SAndroid Build Coastguard Worker RegAlloc("regalloc",
731*9880d681SAndroid Build Coastguard Worker          cl::init(&useDefaultRegisterAllocator),
732*9880d681SAndroid Build Coastguard Worker          cl::desc("Register allocator to use"));
733*9880d681SAndroid Build Coastguard Worker 
initializeDefaultRegisterAllocatorOnce()734*9880d681SAndroid Build Coastguard Worker static void initializeDefaultRegisterAllocatorOnce() {
735*9880d681SAndroid Build Coastguard Worker   RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
736*9880d681SAndroid Build Coastguard Worker 
737*9880d681SAndroid Build Coastguard Worker   if (!Ctor) {
738*9880d681SAndroid Build Coastguard Worker     Ctor = RegAlloc;
739*9880d681SAndroid Build Coastguard Worker     RegisterRegAlloc::setDefault(RegAlloc);
740*9880d681SAndroid Build Coastguard Worker   }
741*9880d681SAndroid Build Coastguard Worker }
742*9880d681SAndroid Build Coastguard Worker 
743*9880d681SAndroid Build Coastguard Worker 
744*9880d681SAndroid Build Coastguard Worker /// Instantiate the default register allocator pass for this target for either
745*9880d681SAndroid Build Coastguard Worker /// the optimized or unoptimized allocation path. This will be added to the pass
746*9880d681SAndroid Build Coastguard Worker /// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
747*9880d681SAndroid Build Coastguard Worker /// in the optimized case.
748*9880d681SAndroid Build Coastguard Worker ///
749*9880d681SAndroid Build Coastguard Worker /// A target that uses the standard regalloc pass order for fast or optimized
750*9880d681SAndroid Build Coastguard Worker /// allocation may still override this for per-target regalloc
751*9880d681SAndroid Build Coastguard Worker /// selection. But -regalloc=... always takes precedence.
createTargetRegisterAllocator(bool Optimized)752*9880d681SAndroid Build Coastguard Worker FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
753*9880d681SAndroid Build Coastguard Worker   if (Optimized)
754*9880d681SAndroid Build Coastguard Worker     return createGreedyRegisterAllocator();
755*9880d681SAndroid Build Coastguard Worker   else
756*9880d681SAndroid Build Coastguard Worker     return createFastRegisterAllocator();
757*9880d681SAndroid Build Coastguard Worker }
758*9880d681SAndroid Build Coastguard Worker 
759*9880d681SAndroid Build Coastguard Worker /// Find and instantiate the register allocation pass requested by this target
760*9880d681SAndroid Build Coastguard Worker /// at the current optimization level.  Different register allocators are
761*9880d681SAndroid Build Coastguard Worker /// defined as separate passes because they may require different analysis.
762*9880d681SAndroid Build Coastguard Worker ///
763*9880d681SAndroid Build Coastguard Worker /// This helper ensures that the regalloc= option is always available,
764*9880d681SAndroid Build Coastguard Worker /// even for targets that override the default allocator.
765*9880d681SAndroid Build Coastguard Worker ///
766*9880d681SAndroid Build Coastguard Worker /// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
767*9880d681SAndroid Build Coastguard Worker /// this can be folded into addPass.
createRegAllocPass(bool Optimized)768*9880d681SAndroid Build Coastguard Worker FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
769*9880d681SAndroid Build Coastguard Worker   // Initialize the global default.
770*9880d681SAndroid Build Coastguard Worker   llvm::call_once(InitializeDefaultRegisterAllocatorFlag,
771*9880d681SAndroid Build Coastguard Worker                   initializeDefaultRegisterAllocatorOnce);
772*9880d681SAndroid Build Coastguard Worker 
773*9880d681SAndroid Build Coastguard Worker   RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
774*9880d681SAndroid Build Coastguard Worker   if (Ctor != useDefaultRegisterAllocator)
775*9880d681SAndroid Build Coastguard Worker     return Ctor();
776*9880d681SAndroid Build Coastguard Worker 
777*9880d681SAndroid Build Coastguard Worker   // With no -regalloc= override, ask the target for a regalloc pass.
778*9880d681SAndroid Build Coastguard Worker   return createTargetRegisterAllocator(Optimized);
779*9880d681SAndroid Build Coastguard Worker }
780*9880d681SAndroid Build Coastguard Worker 
781*9880d681SAndroid Build Coastguard Worker /// Return true if the default global register allocator is in use and
782*9880d681SAndroid Build Coastguard Worker /// has not be overriden on the command line with '-regalloc=...'
usingDefaultRegAlloc() const783*9880d681SAndroid Build Coastguard Worker bool TargetPassConfig::usingDefaultRegAlloc() const {
784*9880d681SAndroid Build Coastguard Worker   return RegAlloc.getNumOccurrences() == 0;
785*9880d681SAndroid Build Coastguard Worker }
786*9880d681SAndroid Build Coastguard Worker 
787*9880d681SAndroid Build Coastguard Worker /// Add the minimum set of target-independent passes that are required for
788*9880d681SAndroid Build Coastguard Worker /// register allocation. No coalescing or scheduling.
addFastRegAlloc(FunctionPass * RegAllocPass)789*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
790*9880d681SAndroid Build Coastguard Worker   addPass(&PHIEliminationID, false);
791*9880d681SAndroid Build Coastguard Worker   addPass(&TwoAddressInstructionPassID, false);
792*9880d681SAndroid Build Coastguard Worker 
793*9880d681SAndroid Build Coastguard Worker   if (RegAllocPass)
794*9880d681SAndroid Build Coastguard Worker     addPass(RegAllocPass);
795*9880d681SAndroid Build Coastguard Worker }
796*9880d681SAndroid Build Coastguard Worker 
797*9880d681SAndroid Build Coastguard Worker /// Add standard target-independent passes that are tightly coupled with
798*9880d681SAndroid Build Coastguard Worker /// optimized register allocation, including coalescing, machine instruction
799*9880d681SAndroid Build Coastguard Worker /// scheduling, and register allocation itself.
addOptimizedRegAlloc(FunctionPass * RegAllocPass)800*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
801*9880d681SAndroid Build Coastguard Worker   addPass(&DetectDeadLanesID, false);
802*9880d681SAndroid Build Coastguard Worker 
803*9880d681SAndroid Build Coastguard Worker   addPass(&ProcessImplicitDefsID, false);
804*9880d681SAndroid Build Coastguard Worker 
805*9880d681SAndroid Build Coastguard Worker   // LiveVariables currently requires pure SSA form.
806*9880d681SAndroid Build Coastguard Worker   //
807*9880d681SAndroid Build Coastguard Worker   // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
808*9880d681SAndroid Build Coastguard Worker   // LiveVariables can be removed completely, and LiveIntervals can be directly
809*9880d681SAndroid Build Coastguard Worker   // computed. (We still either need to regenerate kill flags after regalloc, or
810*9880d681SAndroid Build Coastguard Worker   // preferably fix the scavenger to not depend on them).
811*9880d681SAndroid Build Coastguard Worker   addPass(&LiveVariablesID, false);
812*9880d681SAndroid Build Coastguard Worker 
813*9880d681SAndroid Build Coastguard Worker   // Edge splitting is smarter with machine loop info.
814*9880d681SAndroid Build Coastguard Worker   addPass(&MachineLoopInfoID, false);
815*9880d681SAndroid Build Coastguard Worker   addPass(&PHIEliminationID, false);
816*9880d681SAndroid Build Coastguard Worker 
817*9880d681SAndroid Build Coastguard Worker   // Eventually, we want to run LiveIntervals before PHI elimination.
818*9880d681SAndroid Build Coastguard Worker   if (EarlyLiveIntervals)
819*9880d681SAndroid Build Coastguard Worker     addPass(&LiveIntervalsID, false);
820*9880d681SAndroid Build Coastguard Worker 
821*9880d681SAndroid Build Coastguard Worker   addPass(&TwoAddressInstructionPassID, false);
822*9880d681SAndroid Build Coastguard Worker   addPass(&RegisterCoalescerID);
823*9880d681SAndroid Build Coastguard Worker 
824*9880d681SAndroid Build Coastguard Worker   // The machine scheduler may accidentally create disconnected components
825*9880d681SAndroid Build Coastguard Worker   // when moving subregister definitions around, avoid this by splitting them to
826*9880d681SAndroid Build Coastguard Worker   // separate vregs before. Splitting can also improve reg. allocation quality.
827*9880d681SAndroid Build Coastguard Worker   addPass(&RenameIndependentSubregsID);
828*9880d681SAndroid Build Coastguard Worker 
829*9880d681SAndroid Build Coastguard Worker   // PreRA instruction scheduling.
830*9880d681SAndroid Build Coastguard Worker   addPass(&MachineSchedulerID);
831*9880d681SAndroid Build Coastguard Worker 
832*9880d681SAndroid Build Coastguard Worker   if (RegAllocPass) {
833*9880d681SAndroid Build Coastguard Worker     // Add the selected register allocation pass.
834*9880d681SAndroid Build Coastguard Worker     addPass(RegAllocPass);
835*9880d681SAndroid Build Coastguard Worker 
836*9880d681SAndroid Build Coastguard Worker     // Allow targets to change the register assignments before rewriting.
837*9880d681SAndroid Build Coastguard Worker     addPreRewrite();
838*9880d681SAndroid Build Coastguard Worker 
839*9880d681SAndroid Build Coastguard Worker     // Finally rewrite virtual registers.
840*9880d681SAndroid Build Coastguard Worker     addPass(&VirtRegRewriterID);
841*9880d681SAndroid Build Coastguard Worker 
842*9880d681SAndroid Build Coastguard Worker     // Perform stack slot coloring and post-ra machine LICM.
843*9880d681SAndroid Build Coastguard Worker     //
844*9880d681SAndroid Build Coastguard Worker     // FIXME: Re-enable coloring with register when it's capable of adding
845*9880d681SAndroid Build Coastguard Worker     // kill markers.
846*9880d681SAndroid Build Coastguard Worker     addPass(&StackSlotColoringID);
847*9880d681SAndroid Build Coastguard Worker 
848*9880d681SAndroid Build Coastguard Worker     // Run post-ra machine LICM to hoist reloads / remats.
849*9880d681SAndroid Build Coastguard Worker     //
850*9880d681SAndroid Build Coastguard Worker     // FIXME: can this move into MachineLateOptimization?
851*9880d681SAndroid Build Coastguard Worker     addPass(&PostRAMachineLICMID);
852*9880d681SAndroid Build Coastguard Worker   }
853*9880d681SAndroid Build Coastguard Worker }
854*9880d681SAndroid Build Coastguard Worker 
855*9880d681SAndroid Build Coastguard Worker //===---------------------------------------------------------------------===//
856*9880d681SAndroid Build Coastguard Worker /// Post RegAlloc Pass Configuration
857*9880d681SAndroid Build Coastguard Worker //===---------------------------------------------------------------------===//
858*9880d681SAndroid Build Coastguard Worker 
859*9880d681SAndroid Build Coastguard Worker /// Add passes that optimize machine instructions after register allocation.
addMachineLateOptimization()860*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::addMachineLateOptimization() {
861*9880d681SAndroid Build Coastguard Worker   // Branch folding must be run after regalloc and prolog/epilog insertion.
862*9880d681SAndroid Build Coastguard Worker   addPass(&BranchFolderPassID);
863*9880d681SAndroid Build Coastguard Worker 
864*9880d681SAndroid Build Coastguard Worker   // Tail duplication.
865*9880d681SAndroid Build Coastguard Worker   // Note that duplicating tail just increases code size and degrades
866*9880d681SAndroid Build Coastguard Worker   // performance for targets that require Structured Control Flow.
867*9880d681SAndroid Build Coastguard Worker   // In addition it can also make CFG irreducible. Thus we disable it.
868*9880d681SAndroid Build Coastguard Worker   if (!TM->requiresStructuredCFG())
869*9880d681SAndroid Build Coastguard Worker     addPass(&TailDuplicateID);
870*9880d681SAndroid Build Coastguard Worker 
871*9880d681SAndroid Build Coastguard Worker   // Copy propagation.
872*9880d681SAndroid Build Coastguard Worker   addPass(&MachineCopyPropagationID);
873*9880d681SAndroid Build Coastguard Worker }
874*9880d681SAndroid Build Coastguard Worker 
875*9880d681SAndroid Build Coastguard Worker /// Add standard GC passes.
addGCPasses()876*9880d681SAndroid Build Coastguard Worker bool TargetPassConfig::addGCPasses() {
877*9880d681SAndroid Build Coastguard Worker   addPass(&GCMachineCodeAnalysisID, false);
878*9880d681SAndroid Build Coastguard Worker   return true;
879*9880d681SAndroid Build Coastguard Worker }
880*9880d681SAndroid Build Coastguard Worker 
881*9880d681SAndroid Build Coastguard Worker /// Add standard basic block placement passes.
addBlockPlacement()882*9880d681SAndroid Build Coastguard Worker void TargetPassConfig::addBlockPlacement() {
883*9880d681SAndroid Build Coastguard Worker   if (addPass(&MachineBlockPlacementID)) {
884*9880d681SAndroid Build Coastguard Worker     // Run a separate pass to collect block placement statistics.
885*9880d681SAndroid Build Coastguard Worker     if (EnableBlockPlacementStats)
886*9880d681SAndroid Build Coastguard Worker       addPass(&MachineBlockPlacementStatsID);
887*9880d681SAndroid Build Coastguard Worker   }
888*9880d681SAndroid Build Coastguard Worker }
889