blob: 080b20dd31d9298040f020d47e19bfb3aa10267e [file] [log] [blame]
Chris Lattneraa4c91f2003-12-28 07:59:53 +00001//===-- Passes.cpp - Target independent code generation passes ------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Alkis Evlogimenos7237ece2003-10-02 16:57:49 +00009//
10// This file defines interfaces to access the target independent code
11// generation passes provided by the LLVM backend.
12//
13//===---------------------------------------------------------------------===//
14
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/CodeGen/Passes.h"
Andrew Trickd5422652012-02-04 02:56:48 +000016#include "llvm/Analysis/Passes.h"
Andrew Trickd5422652012-02-04 02:56:48 +000017#include "llvm/CodeGen/GCStrategy.h"
Andrew Trickd5422652012-02-04 02:56:48 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
Andrew Trickd5422652012-02-04 02:56:48 +000019#include "llvm/CodeGen/RegAllocRegistry.h"
Stephen Hines36b56882014-04-23 16:57:46 -070020#include "llvm/IR/IRPrintingPasses.h"
21#include "llvm/IR/Verifier.h"
Bob Wilson564fbf62012-07-02 19:48:31 +000022#include "llvm/MC/MCAsmInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/PassManager.h"
Andrew Trickd5422652012-02-04 02:56:48 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Debug.h"
Andrew Trick74613342012-02-04 02:56:45 +000026#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/Target/TargetLowering.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000028#include "llvm/Target/TargetSubtargetInfo.h"
29#include "llvm/Transforms/Scalar.h"
Jim Laskey13ec7022006-08-01 14:21:23 +000030
Chris Lattneraa4c91f2003-12-28 07:59:53 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Stephen Hines36b56882014-04-23 16:57:46 -070033namespace llvm {
34extern cl::opt<bool> EnableStackMapLiveness;
35extern cl::opt<bool> EnablePatchPointLiveness;
36}
37
Andrew Trickd5422652012-02-04 02:56:48 +000038static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden,
39 cl::desc("Disable Post Regalloc"));
40static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden,
41 cl::desc("Disable branch folding"));
42static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
43 cl::desc("Disable tail duplication"));
44static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
45 cl::desc("Disable pre-register allocation tail duplication"));
Chandler Carruth9e67db42012-04-16 13:49:17 +000046static cl::opt<bool> DisableBlockPlacement("disable-block-placement",
Benjamin Kramer74a45332013-03-29 17:14:24 +000047 cl::Hidden, cl::desc("Disable probability-driven block placement"));
Andrew Trickd5422652012-02-04 02:56:48 +000048static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats",
49 cl::Hidden, cl::desc("Collect probability-driven block placement stats"));
Andrew Trickd5422652012-02-04 02:56:48 +000050static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
51 cl::desc("Disable Stack Slot Coloring"));
52static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden,
53 cl::desc("Disable Machine Dead Code Elimination"));
Jakob Stoklund Olesen0d141f82012-10-03 00:51:32 +000054static cl::opt<bool> DisableEarlyIfConversion("disable-early-ifcvt", cl::Hidden,
55 cl::desc("Disable Early If-conversion"));
Andrew Trickd5422652012-02-04 02:56:48 +000056static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
57 cl::desc("Disable Machine LICM"));
58static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden,
59 cl::desc("Disable Machine Common Subexpression Elimination"));
Andrew Trick8dd26252012-02-10 04:10:36 +000060static cl::opt<cl::boolOrDefault>
61OptimizeRegAlloc("optimize-regalloc", cl::Hidden,
62 cl::desc("Enable optimized register allocation compilation path."));
Andrew Trick746f24b2012-02-11 07:11:32 +000063static cl::opt<cl::boolOrDefault>
Stephen Hines36b56882014-04-23 16:57:46 -070064EnableMachineSched("enable-misched",
Andrew Trick8dd26252012-02-10 04:10:36 +000065 cl::desc("Enable the machine instruction scheduling pass."));
Andrew Trickd5422652012-02-04 02:56:48 +000066static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
67 cl::Hidden,
68 cl::desc("Disable Machine LICM"));
69static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
70 cl::desc("Disable Machine Sinking"));
71static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
72 cl::desc("Disable Loop Strength Reduction Pass"));
Stephen Hines36b56882014-04-23 16:57:46 -070073static cl::opt<bool> DisableConstantHoisting("disable-constant-hoisting",
74 cl::Hidden, cl::desc("Disable ConstantHoisting"));
Andrew Trickd5422652012-02-04 02:56:48 +000075static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
76 cl::desc("Disable Codegen Prepare"));
77static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden,
Evan Cheng01b623c2012-02-20 23:28:17 +000078 cl::desc("Disable Copy Propagation pass"));
Andrew Trickd5422652012-02-04 02:56:48 +000079static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
80 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
81static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
82 cl::desc("Print LLVM IR input to isel pass"));
83static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
84 cl::desc("Dump garbage collector data"));
85static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
86 cl::desc("Verify generated machine code"),
87 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
Bob Wilson6e1b8122012-05-30 00:17:12 +000088static cl::opt<std::string>
89PrintMachineInstrs("print-machineinstrs", cl::ValueOptional,
90 cl::desc("Print machine instrs"),
91 cl::value_desc("pass-name"), cl::init("option-unspecified"));
Andrew Trickd5422652012-02-04 02:56:48 +000092
Stephen Hines36b56882014-04-23 16:57:46 -070093// Temporary option to allow experimenting with MachineScheduler as a post-RA
94// scheduler. Targets can "properly" enable this with
95// substitutePass(&PostRASchedulerID, &MachineSchedulerID); Ideally it wouldn't
96// be part of the standard pass pipeline, and the target would just add a PostRA
97// scheduling pass wherever it wants.
98static cl::opt<bool> MISchedPostRA("misched-postra", cl::Hidden,
99 cl::desc("Run MachineScheduler post regalloc (independent of preRA sched)"));
100
Cameron Zwarichd7c7a682013-02-10 06:42:34 +0000101// Experimental option to run live interval analysis early.
Jakob Stoklund Olesendcc44362012-08-03 22:12:54 +0000102static cl::opt<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden,
103 cl::desc("Run live interval analysis earlier in the pipeline"));
104
Andrew Trick79bf2882012-02-15 03:21:51 +0000105/// Allow standard passes to be disabled by command line options. This supports
106/// simple binary flags that either suppress the pass or do nothing.
107/// i.e. -disable-mypass=false has no effect.
108/// These should be converted to boolOrDefault in order to use applyOverride.
Andrew Trick5ed02832013-04-10 01:06:56 +0000109static IdentifyingPassPtr applyDisable(IdentifyingPassPtr PassID,
110 bool Override) {
Andrew Trick79bf2882012-02-15 03:21:51 +0000111 if (Override)
Andrew Trick5ed02832013-04-10 01:06:56 +0000112 return IdentifyingPassPtr();
Bob Wilson3fb99a72012-07-02 19:48:37 +0000113 return PassID;
Andrew Trick79bf2882012-02-15 03:21:51 +0000114}
115
116/// Allow Pass selection to be overriden by command line options. This supports
117/// flags with ternary conditions. TargetID is passed through by default. The
118/// pass is suppressed when the option is false. When the option is true, the
119/// StandardID is selected if the target provides no default.
Andrew Trick5ed02832013-04-10 01:06:56 +0000120static IdentifyingPassPtr applyOverride(IdentifyingPassPtr TargetID,
121 cl::boolOrDefault Override,
122 AnalysisID StandardID) {
Andrew Trick746f24b2012-02-11 07:11:32 +0000123 switch (Override) {
124 case cl::BOU_UNSET:
Andrew Trick79bf2882012-02-15 03:21:51 +0000125 return TargetID;
Andrew Trick746f24b2012-02-11 07:11:32 +0000126 case cl::BOU_TRUE:
Andrew Trick5ed02832013-04-10 01:06:56 +0000127 if (TargetID.isValid())
Andrew Trick79bf2882012-02-15 03:21:51 +0000128 return TargetID;
Bob Wilson3fb99a72012-07-02 19:48:37 +0000129 if (StandardID == 0)
Andrew Trick746f24b2012-02-11 07:11:32 +0000130 report_fatal_error("Target cannot enable pass");
Andrew Trick79bf2882012-02-15 03:21:51 +0000131 return StandardID;
Andrew Trick746f24b2012-02-11 07:11:32 +0000132 case cl::BOU_FALSE:
Andrew Trick5ed02832013-04-10 01:06:56 +0000133 return IdentifyingPassPtr();
Andrew Trick746f24b2012-02-11 07:11:32 +0000134 }
135 llvm_unreachable("Invalid command line option state");
136}
137
Andrew Trick79bf2882012-02-15 03:21:51 +0000138/// Allow standard passes to be disabled by the command line, regardless of who
139/// is adding the pass.
140///
141/// StandardID is the pass identified in the standard pass pipeline and provided
142/// to addPass(). It may be a target-specific ID in the case that the target
143/// directly adds its own pass, but in that case we harmlessly fall through.
144///
145/// TargetID is the pass that the target has configured to override StandardID.
146///
147/// StandardID may be a pseudo ID. In that case TargetID is the name of the real
148/// pass to run. This allows multiple options to control a single pass depending
149/// on where in the pipeline that pass is added.
Andrew Trick5ed02832013-04-10 01:06:56 +0000150static IdentifyingPassPtr overridePass(AnalysisID StandardID,
151 IdentifyingPassPtr TargetID) {
Andrew Trick79bf2882012-02-15 03:21:51 +0000152 if (StandardID == &PostRASchedulerID)
153 return applyDisable(TargetID, DisablePostRA);
154
155 if (StandardID == &BranchFolderPassID)
156 return applyDisable(TargetID, DisableBranchFold);
157
158 if (StandardID == &TailDuplicateID)
159 return applyDisable(TargetID, DisableTailDuplicate);
160
161 if (StandardID == &TargetPassConfig::EarlyTailDuplicateID)
162 return applyDisable(TargetID, DisableEarlyTailDup);
163
164 if (StandardID == &MachineBlockPlacementID)
Benjamin Kramer74a45332013-03-29 17:14:24 +0000165 return applyDisable(TargetID, DisableBlockPlacement);
Andrew Trick79bf2882012-02-15 03:21:51 +0000166
167 if (StandardID == &StackSlotColoringID)
168 return applyDisable(TargetID, DisableSSC);
169
170 if (StandardID == &DeadMachineInstructionElimID)
171 return applyDisable(TargetID, DisableMachineDCE);
172
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000173 if (StandardID == &EarlyIfConverterID)
Jakob Stoklund Olesen0d141f82012-10-03 00:51:32 +0000174 return applyDisable(TargetID, DisableEarlyIfConversion);
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000175
Andrew Trick79bf2882012-02-15 03:21:51 +0000176 if (StandardID == &MachineLICMID)
177 return applyDisable(TargetID, DisableMachineLICM);
178
179 if (StandardID == &MachineCSEID)
180 return applyDisable(TargetID, DisableMachineCSE);
181
182 if (StandardID == &MachineSchedulerID)
183 return applyOverride(TargetID, EnableMachineSched, StandardID);
184
185 if (StandardID == &TargetPassConfig::PostRAMachineLICMID)
186 return applyDisable(TargetID, DisablePostRAMachineLICM);
187
188 if (StandardID == &MachineSinkingID)
189 return applyDisable(TargetID, DisableMachineSink);
190
191 if (StandardID == &MachineCopyPropagationID)
192 return applyDisable(TargetID, DisableCopyProp);
193
194 return TargetID;
195}
196
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000197//===---------------------------------------------------------------------===//
Andrew Trick74613342012-02-04 02:56:45 +0000198/// TargetPassConfig
199//===---------------------------------------------------------------------===//
200
201INITIALIZE_PASS(TargetPassConfig, "targetpassconfig",
202 "Target Pass Configuration", false, false)
203char TargetPassConfig::ID = 0;
204
Andrew Trick79bf2882012-02-15 03:21:51 +0000205// Pseudo Pass IDs.
206char TargetPassConfig::EarlyTailDuplicateID = 0;
207char TargetPassConfig::PostRAMachineLICMID = 0;
208
Andrew Trick5e108ee2012-02-15 03:21:47 +0000209namespace llvm {
210class PassConfigImpl {
211public:
212 // List of passes explicitly substituted by this target. Normally this is
213 // empty, but it is a convenient way to suppress or replace specific passes
214 // that are part of a standard pass pipeline without overridding the entire
215 // pipeline. This mechanism allows target options to inherit a standard pass's
216 // user interface. For example, a target may disable a standard pass by
Bob Wilson3fb99a72012-07-02 19:48:37 +0000217 // default by substituting a pass ID of zero, and the user may still enable
218 // that standard pass with an explicit command line option.
Andrew Trick5ed02832013-04-10 01:06:56 +0000219 DenseMap<AnalysisID,IdentifyingPassPtr> TargetPasses;
Bob Wilson6e1b8122012-05-30 00:17:12 +0000220
221 /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass
222 /// is inserted after each instance of the first one.
Andrew Trick5ed02832013-04-10 01:06:56 +0000223 SmallVector<std::pair<AnalysisID, IdentifyingPassPtr>, 4> InsertedPasses;
Andrew Trick5e108ee2012-02-15 03:21:47 +0000224};
225} // namespace llvm
226
Andrew Trick74613342012-02-04 02:56:45 +0000227// Out of line virtual method.
Andrew Trick5e108ee2012-02-15 03:21:47 +0000228TargetPassConfig::~TargetPassConfig() {
229 delete Impl;
230}
Andrew Trick74613342012-02-04 02:56:45 +0000231
Andrew Trick61f1e3d2012-02-08 21:22:48 +0000232// Out of line constructor provides default values for pass options and
233// registers all common codegen passes.
Andrew Trick061efcf2012-02-04 02:56:59 +0000234TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm)
Bob Wilson30a507a2012-07-02 19:48:45 +0000235 : ImmutablePass(ID), PM(&pm), StartAfter(0), StopAfter(0),
236 Started(true), Stopped(false), TM(tm), Impl(0), Initialized(false),
Andrew Trickffea03f2012-02-08 21:22:39 +0000237 DisableVerify(false),
238 EnableTailMerge(true) {
239
Andrew Trick5e108ee2012-02-15 03:21:47 +0000240 Impl = new PassConfigImpl();
241
Andrew Trick74613342012-02-04 02:56:45 +0000242 // Register all target independent codegen passes to activate their PassIDs,
243 // including this pass itself.
244 initializeCodeGen(*PassRegistry::getPassRegistry());
Andrew Trick79bf2882012-02-15 03:21:51 +0000245
246 // Substitute Pseudo Pass IDs for real ones.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000247 substitutePass(&EarlyTailDuplicateID, &TailDuplicateID);
248 substitutePass(&PostRAMachineLICMID, &MachineLICMID);
Andrew Trick79bf2882012-02-15 03:21:51 +0000249
250 // Temporarily disable experimental passes.
Andrew Trickad1cc1d2012-11-13 08:47:29 +0000251 const TargetSubtargetInfo &ST = TM->getSubtarget<TargetSubtargetInfo>();
Andrew Trickb6ac11c2013-09-26 05:53:35 +0000252 if (!ST.useMachineScheduler())
Andrew Trickad1cc1d2012-11-13 08:47:29 +0000253 disablePass(&MachineSchedulerID);
Andrew Trick74613342012-02-04 02:56:45 +0000254}
255
Bob Wilson6e1b8122012-05-30 00:17:12 +0000256/// Insert InsertedPassID pass after TargetPassID.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000257void TargetPassConfig::insertPass(AnalysisID TargetPassID,
Andrew Trick5ed02832013-04-10 01:06:56 +0000258 IdentifyingPassPtr InsertedPassID) {
Benjamin Kramerfdca2212013-04-11 11:57:01 +0000259 assert(((!InsertedPassID.isInstance() &&
260 TargetPassID != InsertedPassID.getID()) ||
261 (InsertedPassID.isInstance() &&
262 TargetPassID != InsertedPassID.getInstance()->getPassID())) &&
Andrew Trick5ed02832013-04-10 01:06:56 +0000263 "Insert a pass after itself!");
264 std::pair<AnalysisID, IdentifyingPassPtr> P(TargetPassID, InsertedPassID);
Bob Wilson6e1b8122012-05-30 00:17:12 +0000265 Impl->InsertedPasses.push_back(P);
266}
267
Andrew Trick74613342012-02-04 02:56:45 +0000268/// createPassConfig - Create a pass configuration object to be used by
269/// addPassToEmitX methods for generating a pipeline of CodeGen passes.
270///
271/// Targets may override this to extend TargetPassConfig.
Andrew Trick061efcf2012-02-04 02:56:59 +0000272TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
273 return new TargetPassConfig(this, PM);
Andrew Trick74613342012-02-04 02:56:45 +0000274}
275
276TargetPassConfig::TargetPassConfig()
Bill Wendling7c4ce302012-05-01 08:27:43 +0000277 : ImmutablePass(ID), PM(0) {
Andrew Trick74613342012-02-04 02:56:45 +0000278 llvm_unreachable("TargetPassConfig should not be constructed on-the-fly");
279}
280
Andrew Trickffea03f2012-02-08 21:22:39 +0000281// Helper to verify the analysis is really immutable.
282void TargetPassConfig::setOpt(bool &Opt, bool Val) {
283 assert(!Initialized && "PassConfig is immutable");
284 Opt = Val;
285}
286
Bob Wilson3fb99a72012-07-02 19:48:37 +0000287void TargetPassConfig::substitutePass(AnalysisID StandardID,
Andrew Trick5ed02832013-04-10 01:06:56 +0000288 IdentifyingPassPtr TargetID) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000289 Impl->TargetPasses[StandardID] = TargetID;
Andrew Trick5e108ee2012-02-15 03:21:47 +0000290}
Andrew Trick746f24b2012-02-11 07:11:32 +0000291
Andrew Trick5ed02832013-04-10 01:06:56 +0000292IdentifyingPassPtr TargetPassConfig::getPassSubstitution(AnalysisID ID) const {
293 DenseMap<AnalysisID, IdentifyingPassPtr>::const_iterator
Andrew Trick5e108ee2012-02-15 03:21:47 +0000294 I = Impl->TargetPasses.find(ID);
295 if (I == Impl->TargetPasses.end())
296 return ID;
297 return I->second;
298}
299
Bob Wilson30a507a2012-07-02 19:48:45 +0000300/// Add a pass to the PassManager if that pass is supposed to be run. If the
301/// Started/Stopped flags indicate either that the compilation should start at
302/// a later pass or that it should stop after an earlier pass, then do not add
303/// the pass. Finally, compare the current pass against the StartAfter
304/// and StopAfter options and change the Started/Stopped flags accordingly.
Bob Wilson564fbf62012-07-02 19:48:31 +0000305void TargetPassConfig::addPass(Pass *P) {
Bob Wilson6b2bb152012-07-02 19:48:39 +0000306 assert(!Initialized && "PassConfig is immutable");
307
Chandler Carruth6068c482012-07-02 22:56:41 +0000308 // Cache the Pass ID here in case the pass manager finds this pass is
309 // redundant with ones already scheduled / available, and deletes it.
310 // Fundamentally, once we add the pass to the manager, we no longer own it
311 // and shouldn't reference it.
312 AnalysisID PassID = P->getPassID();
313
Bob Wilson30a507a2012-07-02 19:48:45 +0000314 if (Started && !Stopped)
315 PM->add(P);
Benjamin Kramerf8e16c62013-08-05 11:11:11 +0000316 else
317 delete P;
Chandler Carruth6068c482012-07-02 22:56:41 +0000318 if (StopAfter == PassID)
Bob Wilson30a507a2012-07-02 19:48:45 +0000319 Stopped = true;
Chandler Carruth6068c482012-07-02 22:56:41 +0000320 if (StartAfter == PassID)
Bob Wilson30a507a2012-07-02 19:48:45 +0000321 Started = true;
322 if (Stopped && !Started)
323 report_fatal_error("Cannot stop compilation after pass that is not run");
Bob Wilson564fbf62012-07-02 19:48:31 +0000324}
325
Andrew Trick5e108ee2012-02-15 03:21:47 +0000326/// Add a CodeGen pass at this point in the pipeline after checking for target
327/// and command line overrides.
Andrew Trick5ed02832013-04-10 01:06:56 +0000328///
329/// addPass cannot return a pointer to the pass instance because is internal the
330/// PassManager and the instance we create here may already be freed.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000331AnalysisID TargetPassConfig::addPass(AnalysisID PassID) {
Andrew Trick5ed02832013-04-10 01:06:56 +0000332 IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
333 IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID);
334 if (!FinalPtr.isValid())
335 return 0;
Andrew Trick5e108ee2012-02-15 03:21:47 +0000336
Andrew Trick5ed02832013-04-10 01:06:56 +0000337 Pass *P;
338 if (FinalPtr.isInstance())
339 P = FinalPtr.getInstance();
340 else {
341 P = Pass::createPass(FinalPtr.getID());
342 if (!P)
343 llvm_unreachable("Pass ID not registered");
344 }
345 AnalysisID FinalID = P->getPassID();
346 addPass(P); // Ends the lifetime of P.
347
Bob Wilson6e1b8122012-05-30 00:17:12 +0000348 // Add the passes after the pass P if there is any.
Craig Topperf22fd3f2013-07-03 05:11:49 +0000349 for (SmallVectorImpl<std::pair<AnalysisID, IdentifyingPassPtr> >::iterator
Bob Wilson6e1b8122012-05-30 00:17:12 +0000350 I = Impl->InsertedPasses.begin(), E = Impl->InsertedPasses.end();
351 I != E; ++I) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000352 if ((*I).first == PassID) {
Andrew Trick5ed02832013-04-10 01:06:56 +0000353 assert((*I).second.isValid() && "Illegal Pass ID!");
354 Pass *NP;
355 if ((*I).second.isInstance())
356 NP = (*I).second.getInstance();
357 else {
358 NP = Pass::createPass((*I).second.getID());
359 assert(NP && "Pass ID not registered");
360 }
Bob Wilson564fbf62012-07-02 19:48:31 +0000361 addPass(NP);
Bob Wilson6e1b8122012-05-30 00:17:12 +0000362 }
363 }
Andrew Trick5e108ee2012-02-15 03:21:47 +0000364 return FinalID;
Andrew Trick061efcf2012-02-04 02:56:59 +0000365}
Andrew Trickd5422652012-02-04 02:56:48 +0000366
Bob Wilson564fbf62012-07-02 19:48:31 +0000367void TargetPassConfig::printAndVerify(const char *Banner) {
Andrew Trickd5422652012-02-04 02:56:48 +0000368 if (TM->shouldPrintMachineCode())
Bob Wilson564fbf62012-07-02 19:48:31 +0000369 addPass(createMachineFunctionPrinterPass(dbgs(), Banner));
Andrew Trickd5422652012-02-04 02:56:48 +0000370
371 if (VerifyMachineCode)
Bob Wilson564fbf62012-07-02 19:48:31 +0000372 addPass(createMachineVerifierPass(Banner));
Andrew Trickd5422652012-02-04 02:56:48 +0000373}
374
Andrew Trick061efcf2012-02-04 02:56:59 +0000375/// Add common target configurable passes that perform LLVM IR to IR transforms
376/// following machine independent optimization.
377void TargetPassConfig::addIRPasses() {
Andrew Trickd5422652012-02-04 02:56:48 +0000378 // Basic AliasAnalysis support.
379 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
380 // BasicAliasAnalysis wins if they disagree. This is intended to help
381 // support "obvious" type-punning idioms.
Bob Wilson564fbf62012-07-02 19:48:31 +0000382 addPass(createTypeBasedAliasAnalysisPass());
383 addPass(createBasicAliasAnalysisPass());
Andrew Trickd5422652012-02-04 02:56:48 +0000384
385 // Before running any passes, run the verifier to determine if the input
386 // coming from the front-end and/or optimizer is valid.
387 if (!DisableVerify)
Bob Wilson564fbf62012-07-02 19:48:31 +0000388 addPass(createVerifierPass());
Andrew Trickd5422652012-02-04 02:56:48 +0000389
390 // Run loop strength reduction before anything else.
391 if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
Chandler Carruthe4ba75f2013-01-07 14:41:08 +0000392 addPass(createLoopStrengthReducePass());
Andrew Trickd5422652012-02-04 02:56:48 +0000393 if (PrintLSR)
Stephen Hines36b56882014-04-23 16:57:46 -0700394 addPass(createPrintFunctionPass(dbgs(), "\n\n*** Code after LSR ***\n"));
Andrew Trickd5422652012-02-04 02:56:48 +0000395 }
396
Bob Wilson564fbf62012-07-02 19:48:31 +0000397 addPass(createGCLoweringPass());
Andrew Trickd5422652012-02-04 02:56:48 +0000398
399 // Make sure that no unreachable blocks are instruction selected.
Bob Wilson564fbf62012-07-02 19:48:31 +0000400 addPass(createUnreachableBlockEliminationPass());
Stephen Hines36b56882014-04-23 16:57:46 -0700401
402 // Prepare expensive constants for SelectionDAG.
403 if (getOptLevel() != CodeGenOpt::None && !DisableConstantHoisting)
404 addPass(createConstantHoistingPass());
Bob Wilson564fbf62012-07-02 19:48:31 +0000405}
406
407/// Turn exception handling constructs into something the code generators can
408/// handle.
409void TargetPassConfig::addPassesToHandleExceptions() {
410 switch (TM->getMCAsmInfo()->getExceptionHandlingType()) {
411 case ExceptionHandling::SjLj:
412 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
413 // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
414 // catch info can get misplaced when a selector ends up more than one block
415 // removed from the parent invoke(s). This could happen when a landing
416 // pad is shared by multiple invokes and is also a target of a normal
417 // edge from elsewhere.
Bill Wendlingea442812013-06-19 20:51:24 +0000418 addPass(createSjLjEHPreparePass(TM));
Bob Wilson564fbf62012-07-02 19:48:31 +0000419 // FALLTHROUGH
420 case ExceptionHandling::DwarfCFI:
421 case ExceptionHandling::ARM:
422 case ExceptionHandling::Win64:
Bill Wendlingea442812013-06-19 20:51:24 +0000423 addPass(createDwarfEHPass(TM));
Bob Wilson564fbf62012-07-02 19:48:31 +0000424 break;
425 case ExceptionHandling::None:
Stephen Hines36b56882014-04-23 16:57:46 -0700426 addPass(createLowerInvokePass());
Bob Wilson564fbf62012-07-02 19:48:31 +0000427
428 // The lower invoke pass may create unreachable code. Remove it.
429 addPass(createUnreachableBlockEliminationPass());
430 break;
431 }
Andrew Trick061efcf2012-02-04 02:56:59 +0000432}
Andrew Trickd5422652012-02-04 02:56:48 +0000433
Bill Wendling08510b12012-11-30 22:08:55 +0000434/// Add pass to prepare the LLVM IR for code generation. This should be done
435/// before exception handling preparation passes.
436void TargetPassConfig::addCodeGenPrepare() {
437 if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
Bill Wendlingf9fd58a2013-06-19 21:07:11 +0000438 addPass(createCodeGenPreparePass(TM));
Bill Wendling08510b12012-11-30 22:08:55 +0000439}
440
Andrew Trick061efcf2012-02-04 02:56:59 +0000441/// Add common passes that perform LLVM IR to IR transforms in preparation for
442/// instruction selection.
443void TargetPassConfig::addISelPrepare() {
Andrew Trickd5422652012-02-04 02:56:48 +0000444 addPreISel();
445
Stephen Hines36b56882014-04-23 16:57:46 -0700446 addPass(createStackProtectorPass(TM));
447
Andrew Trickd5422652012-02-04 02:56:48 +0000448 if (PrintISelInput)
Stephen Hines36b56882014-04-23 16:57:46 -0700449 addPass(createPrintFunctionPass(
450 dbgs(), "\n\n*** Final LLVM Code input to ISel ***\n"));
Andrew Trickd5422652012-02-04 02:56:48 +0000451
452 // All passes which modify the LLVM IR are now complete; run the verifier
453 // to ensure that the IR is valid.
454 if (!DisableVerify)
Bob Wilson564fbf62012-07-02 19:48:31 +0000455 addPass(createVerifierPass());
Andrew Trick061efcf2012-02-04 02:56:59 +0000456}
Andrew Trickd5422652012-02-04 02:56:48 +0000457
Andrew Trickf7b96312012-02-09 00:40:55 +0000458/// Add the complete set of target-independent postISel code generator passes.
459///
460/// This can be read as the standard order of major LLVM CodeGen stages. Stages
461/// with nontrivial configuration or multiple passes are broken out below in
462/// add%Stage routines.
463///
464/// Any TargetPassConfig::addXX routine may be overriden by the Target. The
465/// addPre/Post methods with empty header implementations allow injecting
466/// target-specific fixups just before or after major stages. Additionally,
467/// targets have the flexibility to change pass order within a stage by
468/// overriding default implementation of add%Stage routines below. Each
469/// technique has maintainability tradeoffs because alternate pass orders are
470/// not well supported. addPre/Post works better if the target pass is easily
471/// tied to a common pass. But if it has subtle dependencies on multiple passes,
Andrew Trick06efdd22012-02-10 07:08:25 +0000472/// the target should override the stage instead.
Andrew Trickf7b96312012-02-09 00:40:55 +0000473///
474/// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
475/// before/after any target-independent pass. But it's currently overkill.
Andrew Trick061efcf2012-02-04 02:56:59 +0000476void TargetPassConfig::addMachinePasses() {
Bob Wilson6e1b8122012-05-30 00:17:12 +0000477 // Insert a machine instr printer pass after the specified pass.
478 // If -print-machineinstrs specified, print machineinstrs after all passes.
479 if (StringRef(PrintMachineInstrs.getValue()).equals(""))
480 TM->Options.PrintMachineCode = true;
481 else if (!StringRef(PrintMachineInstrs.getValue())
482 .equals("option-unspecified")) {
483 const PassRegistry *PR = PassRegistry::getPassRegistry();
484 const PassInfo *TPI = PR->getPassInfo(PrintMachineInstrs.getValue());
485 const PassInfo *IPI = PR->getPassInfo(StringRef("print-machineinstrs"));
486 assert (TPI && IPI && "Pass ID not registered!");
Roman Divacky59324292012-09-05 22:26:57 +0000487 const char *TID = (const char *)(TPI->getTypeInfo());
488 const char *IID = (const char *)(IPI->getTypeInfo());
Bob Wilson3fb99a72012-07-02 19:48:37 +0000489 insertPass(TID, IID);
Bob Wilson6e1b8122012-05-30 00:17:12 +0000490 }
491
Jakob Stoklund Olesenf86c00f2012-07-04 19:28:27 +0000492 // Print the instruction selected machine code...
493 printAndVerify("After Instruction Selection");
494
Andrew Trickd5422652012-02-04 02:56:48 +0000495 // Expand pseudo-instructions emitted by ISel.
Jakob Stoklund Olesen228e3f52012-08-20 20:52:08 +0000496 if (addPass(&ExpandISelPseudosID))
497 printAndVerify("After ExpandISelPseudos");
Andrew Trickd5422652012-02-04 02:56:48 +0000498
Andrew Trickf7b96312012-02-09 00:40:55 +0000499 // Add passes that optimize machine instructions in SSA form.
Andrew Trickd5422652012-02-04 02:56:48 +0000500 if (getOptLevel() != CodeGenOpt::None) {
Andrew Trickf7b96312012-02-09 00:40:55 +0000501 addMachineSSAOptimization();
Craig Topper8f54a532012-11-19 00:11:50 +0000502 } else {
Andrew Trickf7b96312012-02-09 00:40:55 +0000503 // If the target requests it, assign local variables to stack slots relative
504 // to one another and simplify frame index references where possible.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000505 addPass(&LocalStackSlotAllocationID);
Andrew Trickd5422652012-02-04 02:56:48 +0000506 }
507
508 // Run pre-ra passes.
509 if (addPreRegAlloc())
510 printAndVerify("After PreRegAlloc passes");
511
Andrew Trickf7b96312012-02-09 00:40:55 +0000512 // Run register allocation and passes that are tightly coupled with it,
513 // including phi elimination and scheduling.
Andrew Trick8dd26252012-02-10 04:10:36 +0000514 if (getOptimizeRegAlloc())
515 addOptimizedRegAlloc(createRegAllocPass(true));
516 else
517 addFastRegAlloc(createRegAllocPass(false));
Andrew Trickd5422652012-02-04 02:56:48 +0000518
519 // Run post-ra passes.
520 if (addPostRegAlloc())
521 printAndVerify("After PostRegAlloc passes");
522
523 // Insert prolog/epilog code. Eliminate abstract frame index references...
Bob Wilson3fb99a72012-07-02 19:48:37 +0000524 addPass(&PrologEpilogCodeInserterID);
Andrew Trickd5422652012-02-04 02:56:48 +0000525 printAndVerify("After PrologEpilogCodeInserter");
526
Andrew Trickf7b96312012-02-09 00:40:55 +0000527 /// Add passes that optimize machine instructions after register allocation.
528 if (getOptLevel() != CodeGenOpt::None)
529 addMachineLateOptimization();
Andrew Trickd5422652012-02-04 02:56:48 +0000530
531 // Expand pseudo instructions before second scheduling pass.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000532 addPass(&ExpandPostRAPseudosID);
Jakob Stoklund Olesen2ef5bf62012-03-28 20:49:30 +0000533 printAndVerify("After ExpandPostRAPseudos");
Andrew Trickd5422652012-02-04 02:56:48 +0000534
535 // Run pre-sched2 passes.
536 if (addPreSched2())
Jakob Stoklund Olesen78811662012-03-28 23:31:15 +0000537 printAndVerify("After PreSched2 passes");
Andrew Trickd5422652012-02-04 02:56:48 +0000538
539 // Second pass scheduler.
Andrew Trick79bf2882012-02-15 03:21:51 +0000540 if (getOptLevel() != CodeGenOpt::None) {
Stephen Hines36b56882014-04-23 16:57:46 -0700541 if (MISchedPostRA)
542 addPass(&PostMachineSchedulerID);
543 else
544 addPass(&PostRASchedulerID);
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000545 printAndVerify("After PostRAScheduler");
Andrew Trickd5422652012-02-04 02:56:48 +0000546 }
547
Andrew Trickf7b96312012-02-09 00:40:55 +0000548 // GC
Evan Chengab37b2c2012-12-21 02:57:04 +0000549 if (addGCPasses()) {
550 if (PrintGCInfo)
551 addPass(createGCInfoPrinter(dbgs()));
552 }
Andrew Trickd5422652012-02-04 02:56:48 +0000553
Andrew Trickf7b96312012-02-09 00:40:55 +0000554 // Basic block placement.
Andrew Trick79bf2882012-02-15 03:21:51 +0000555 if (getOptLevel() != CodeGenOpt::None)
Andrew Trickf7b96312012-02-09 00:40:55 +0000556 addBlockPlacement();
Andrew Trickd5422652012-02-04 02:56:48 +0000557
558 if (addPreEmitPass())
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000559 printAndVerify("After PreEmit passes");
Stephen Hines36b56882014-04-23 16:57:46 -0700560
561 if (EnableStackMapLiveness || EnablePatchPointLiveness)
562 addPass(&StackMapLivenessID);
Andrew Trickd5422652012-02-04 02:56:48 +0000563}
564
Andrew Trickf7b96312012-02-09 00:40:55 +0000565/// Add passes that optimize machine instructions in SSA form.
566void TargetPassConfig::addMachineSSAOptimization() {
567 // Pre-ra tail duplication.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000568 if (addPass(&EarlyTailDuplicateID))
Andrew Trickf7b96312012-02-09 00:40:55 +0000569 printAndVerify("After Pre-RegAlloc TailDuplicate");
Andrew Trickf7b96312012-02-09 00:40:55 +0000570
571 // Optimize PHIs before DCE: removing dead PHI cycles may make more
572 // instructions dead.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000573 addPass(&OptimizePHIsID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000574
Nadav Rotemc05d3062012-09-06 09:17:37 +0000575 // This pass merges large allocas. StackSlotColoring is a different pass
576 // which merges spill slots.
577 addPass(&StackColoringID);
578
Andrew Trickf7b96312012-02-09 00:40:55 +0000579 // If the target requests it, assign local variables to stack slots relative
580 // to one another and simplify frame index references where possible.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000581 addPass(&LocalStackSlotAllocationID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000582
583 // With optimization, dead code should already be eliminated. However
584 // there is one known exception: lowered code for arguments that are only
585 // used by tail calls, where the tail calls reuse the incoming stack
586 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
Bob Wilson3fb99a72012-07-02 19:48:37 +0000587 addPass(&DeadMachineInstructionElimID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000588 printAndVerify("After codegen DCE pass");
589
Jakob Stoklund Olesen02c63252013-01-17 00:58:38 +0000590 // Allow targets to insert passes that improve instruction level parallelism,
591 // like if-conversion. Such passes will typically need dominator trees and
592 // loop info, just like LICM and CSE below.
593 if (addILPOpts())
594 printAndVerify("After ILP optimizations");
595
Bob Wilson3fb99a72012-07-02 19:48:37 +0000596 addPass(&MachineLICMID);
597 addPass(&MachineCSEID);
598 addPass(&MachineSinkingID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000599 printAndVerify("After Machine LICM, CSE and Sinking passes");
600
Bob Wilson3fb99a72012-07-02 19:48:37 +0000601 addPass(&PeepholeOptimizerID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000602 printAndVerify("After codegen peephole optimization pass");
603}
604
Andrew Trick74613342012-02-04 02:56:45 +0000605//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000606/// Register Allocation Pass Configuration
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000607//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000608
Andrew Trick8dd26252012-02-10 04:10:36 +0000609bool TargetPassConfig::getOptimizeRegAlloc() const {
610 switch (OptimizeRegAlloc) {
611 case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
612 case cl::BOU_TRUE: return true;
613 case cl::BOU_FALSE: return false;
614 }
615 llvm_unreachable("Invalid optimize-regalloc state");
616}
617
Andrew Trickf7b96312012-02-09 00:40:55 +0000618/// RegisterRegAlloc's global Registry tracks allocator registration.
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000619MachinePassRegistry RegisterRegAlloc::Registry;
620
Andrew Trickf7b96312012-02-09 00:40:55 +0000621/// A dummy default pass factory indicates whether the register allocator is
622/// overridden on the command line.
Andrew Trick8dd26252012-02-10 04:10:36 +0000623static FunctionPass *useDefaultRegisterAllocator() { return 0; }
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000624static RegisterRegAlloc
625defaultRegAlloc("default",
626 "pick register allocator based on -O option",
Andrew Trick8dd26252012-02-10 04:10:36 +0000627 useDefaultRegisterAllocator);
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000628
Andrew Trickf7b96312012-02-09 00:40:55 +0000629/// -regalloc=... command line option.
Dan Gohman844731a2008-05-13 00:00:25 +0000630static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
631 RegisterPassParser<RegisterRegAlloc> >
632RegAlloc("regalloc",
Andrew Trick8dd26252012-02-10 04:10:36 +0000633 cl::init(&useDefaultRegisterAllocator),
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000634 cl::desc("Register allocator to use"));
Alkis Evlogimenos7237ece2003-10-02 16:57:49 +0000635
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000636
Andrew Trick8dd26252012-02-10 04:10:36 +0000637/// Instantiate the default register allocator pass for this target for either
638/// the optimized or unoptimized allocation path. This will be added to the pass
639/// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
640/// in the optimized case.
641///
642/// A target that uses the standard regalloc pass order for fast or optimized
643/// allocation may still override this for per-target regalloc
644/// selection. But -regalloc=... always takes precedence.
645FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
646 if (Optimized)
647 return createGreedyRegisterAllocator();
648 else
649 return createFastRegisterAllocator();
650}
651
652/// Find and instantiate the register allocation pass requested by this target
653/// at the current optimization level. Different register allocators are
654/// defined as separate passes because they may require different analysis.
655///
656/// This helper ensures that the regalloc= option is always available,
657/// even for targets that override the default allocator.
658///
659/// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
660/// this can be folded into addPass.
661FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
Jim Laskey9ff542f2006-08-01 18:29:48 +0000662 RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000663
Andrew Trick8dd26252012-02-10 04:10:36 +0000664 // Initialize the global default.
Jim Laskey13ec7022006-08-01 14:21:23 +0000665 if (!Ctor) {
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000666 Ctor = RegAlloc;
667 RegisterRegAlloc::setDefault(RegAlloc);
Jim Laskey13ec7022006-08-01 14:21:23 +0000668 }
Andrew Trick8dd26252012-02-10 04:10:36 +0000669 if (Ctor != useDefaultRegisterAllocator)
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000670 return Ctor();
671
Andrew Trick8dd26252012-02-10 04:10:36 +0000672 // With no -regalloc= override, ask the target for a regalloc pass.
673 return createTargetRegisterAllocator(Optimized);
674}
675
676/// Add the minimum set of target-independent passes that are required for
677/// register allocation. No coalescing or scheduling.
678void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000679 addPass(&PHIEliminationID);
680 addPass(&TwoAddressInstructionPassID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000681
Bob Wilson564fbf62012-07-02 19:48:31 +0000682 addPass(RegAllocPass);
Andrew Trick8dd26252012-02-10 04:10:36 +0000683 printAndVerify("After Register Allocation");
Jim Laskey33a0a6d2006-07-27 20:05:00 +0000684}
Andrew Trickf7b96312012-02-09 00:40:55 +0000685
686/// Add standard target-independent passes that are tightly coupled with
Andrew Trick8dd26252012-02-10 04:10:36 +0000687/// optimized register allocation, including coalescing, machine instruction
688/// scheduling, and register allocation itself.
689void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000690 addPass(&ProcessImplicitDefsID);
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +0000691
Andrew Trick8dd26252012-02-10 04:10:36 +0000692 // LiveVariables currently requires pure SSA form.
693 //
694 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
695 // LiveVariables can be removed completely, and LiveIntervals can be directly
696 // computed. (We still either need to regenerate kill flags after regalloc, or
697 // preferably fix the scavenger to not depend on them).
Bob Wilson3fb99a72012-07-02 19:48:37 +0000698 addPass(&LiveVariablesID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000699
Rafael Espindola67b28822013-10-14 16:39:04 +0000700 // Edge splitting is smarter with machine loop info.
701 addPass(&MachineLoopInfoID);
702 addPass(&PHIEliminationID);
Jakob Stoklund Olesendcc44362012-08-03 22:12:54 +0000703
704 // Eventually, we want to run LiveIntervals before PHI elimination.
705 if (EarlyLiveIntervals)
706 addPass(&LiveIntervalsID);
707
Bob Wilson3fb99a72012-07-02 19:48:37 +0000708 addPass(&TwoAddressInstructionPassID);
Bob Wilson3fb99a72012-07-02 19:48:37 +0000709 addPass(&RegisterCoalescerID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000710
711 // PreRA instruction scheduling.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000712 if (addPass(&MachineSchedulerID))
Andrew Trick17d35e52012-03-14 04:00:41 +0000713 printAndVerify("After Machine Scheduling");
Andrew Trick8dd26252012-02-10 04:10:36 +0000714
715 // Add the selected register allocation pass.
Bob Wilson564fbf62012-07-02 19:48:31 +0000716 addPass(RegAllocPass);
Jakob Stoklund Olesen34f5a2b2012-06-26 17:09:29 +0000717 printAndVerify("After Register Allocation, before rewriter");
718
719 // Allow targets to change the register assignments before rewriting.
720 if (addPreRewrite())
721 printAndVerify("After pre-rewrite passes");
Andrew Trickf7b96312012-02-09 00:40:55 +0000722
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000723 // Finally rewrite virtual registers.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000724 addPass(&VirtRegRewriterID);
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000725 printAndVerify("After Virtual Register Rewriter");
726
Andrew Trickf7b96312012-02-09 00:40:55 +0000727 // Perform stack slot coloring and post-ra machine LICM.
Andrew Trick8dd26252012-02-10 04:10:36 +0000728 //
729 // FIXME: Re-enable coloring with register when it's capable of adding
730 // kill markers.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000731 addPass(&StackSlotColoringID);
Andrew Trick900d7b72012-02-15 07:57:03 +0000732
733 // Run post-ra machine LICM to hoist reloads / remats.
734 //
735 // FIXME: can this move into MachineLateOptimization?
Bob Wilson3fb99a72012-07-02 19:48:37 +0000736 addPass(&PostRAMachineLICMID);
Andrew Trick900d7b72012-02-15 07:57:03 +0000737
738 printAndVerify("After StackSlotColoring and postra Machine LICM");
Andrew Trickf7b96312012-02-09 00:40:55 +0000739}
740
741//===---------------------------------------------------------------------===//
742/// Post RegAlloc Pass Configuration
743//===---------------------------------------------------------------------===//
744
745/// Add passes that optimize machine instructions after register allocation.
746void TargetPassConfig::addMachineLateOptimization() {
747 // Branch folding must be run after regalloc and prolog/epilog insertion.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000748 if (addPass(&BranchFolderPassID))
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000749 printAndVerify("After BranchFolding");
Andrew Trickf7b96312012-02-09 00:40:55 +0000750
751 // Tail duplication.
Stephen Hines36b56882014-04-23 16:57:46 -0700752 // Note that duplicating tail just increases code size and degrades
753 // performance for targets that require Structured Control Flow.
754 // In addition it can also make CFG irreducible. Thus we disable it.
755 if (!TM->requiresStructuredCFG() && addPass(&TailDuplicateID))
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000756 printAndVerify("After TailDuplicate");
Andrew Trickf7b96312012-02-09 00:40:55 +0000757
758 // Copy propagation.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000759 if (addPass(&MachineCopyPropagationID))
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000760 printAndVerify("After copy propagation pass");
Andrew Trickf7b96312012-02-09 00:40:55 +0000761}
762
Evan Chengab37b2c2012-12-21 02:57:04 +0000763/// Add standard GC passes.
764bool TargetPassConfig::addGCPasses() {
765 addPass(&GCMachineCodeAnalysisID);
766 return true;
767}
768
Andrew Trickf7b96312012-02-09 00:40:55 +0000769/// Add standard basic block placement passes.
770void TargetPassConfig::addBlockPlacement() {
Benjamin Kramer74a45332013-03-29 17:14:24 +0000771 if (addPass(&MachineBlockPlacementID)) {
Andrew Trick79bf2882012-02-15 03:21:51 +0000772 // Run a separate pass to collect block placement statistics.
773 if (EnableBlockPlacementStats)
Bob Wilson3fb99a72012-07-02 19:48:37 +0000774 addPass(&MachineBlockPlacementStatsID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000775
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000776 printAndVerify("After machine block placement.");
Andrew Trickf7b96312012-02-09 00:40:55 +0000777 }
778}