blob: 0e8e50eca79ca5473923ff69ba218c786f41ad21 [file] [log] [blame]
Chris Lattner27dd6422003-12-28 07:59:53 +00001//===-- Passes.cpp - Target independent code generation passes ------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Alkis Evlogimenos5facafa2003-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 Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/CodeGen/Passes.h"
Andrew Trickde401d32012-02-04 02:56:48 +000016#include "llvm/Analysis/Passes.h"
Andrew Trickde401d32012-02-04 02:56:48 +000017#include "llvm/CodeGen/GCStrategy.h"
Andrew Trickde401d32012-02-04 02:56:48 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
Andrew Trickde401d32012-02-04 02:56:48 +000019#include "llvm/CodeGen/RegAllocRegistry.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000020#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000021#include "llvm/IR/Verifier.h"
Bob Wilsonbbd38dd2012-07-02 19:48:31 +000022#include "llvm/MC/MCAsmInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/PassManager.h"
Andrew Trickde401d32012-02-04 02:56:48 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Debug.h"
Andrew Trickb7551332012-02-04 02:56:45 +000026#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Target/TargetLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Target/TargetSubtargetInfo.h"
29#include "llvm/Transforms/Scalar.h"
Jim Laskey95eda5b2006-08-01 14:21:23 +000030
Chris Lattner27dd6422003-12-28 07:59:53 +000031using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000032
Juergen Ributzkae8294752013-12-14 06:53:06 +000033namespace llvm {
34extern cl::opt<bool> EnableStackMapLiveness;
35extern cl::opt<bool> EnablePatchPointLiveness;
36}
37
Andrew Trickde401d32012-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 Carruth4190b502012-04-16 13:49:17 +000046static cl::opt<bool> DisableBlockPlacement("disable-block-placement",
Benjamin Kramer70671b92013-03-29 17:14:24 +000047 cl::Hidden, cl::desc("Disable probability-driven block placement"));
Andrew Trickde401d32012-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 Trickde401d32012-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 Olesen0f6e8bb2012-10-03 00:51:32 +000054static cl::opt<bool> DisableEarlyIfConversion("disable-early-ifcvt", cl::Hidden,
55 cl::desc("Disable Early If-conversion"));
Andrew Trickde401d32012-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 Trickd3f8fe82012-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 Trickee874db2012-02-11 07:11:32 +000063static cl::opt<cl::boolOrDefault>
Andrew Trick7daf6a42014-01-13 20:08:27 +000064EnableMachineSched("enable-misched",
Andrew Trickd3f8fe82012-02-10 04:10:36 +000065 cl::desc("Enable the machine instruction scheduling pass."));
Andrew Trickde401d32012-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"));
73static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
74 cl::desc("Disable Codegen Prepare"));
75static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden,
Evan Cheng63618f92012-02-20 23:28:17 +000076 cl::desc("Disable Copy Propagation pass"));
Andrew Trickde401d32012-02-04 02:56:48 +000077static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
78 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
79static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
80 cl::desc("Print LLVM IR input to isel pass"));
81static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
82 cl::desc("Dump garbage collector data"));
83static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
84 cl::desc("Verify generated machine code"),
85 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
Bob Wilson33e51882012-05-30 00:17:12 +000086static cl::opt<std::string>
87PrintMachineInstrs("print-machineinstrs", cl::ValueOptional,
88 cl::desc("Print machine instrs"),
89 cl::value_desc("pass-name"), cl::init("option-unspecified"));
Andrew Trickde401d32012-02-04 02:56:48 +000090
Andrew Trick17080b92013-12-28 21:56:51 +000091// Temporary option to allow experimenting with MachineScheduler as a post-RA
92// scheduler. Targets can "properly" enable this with
93// substitutePass(&PostRASchedulerID, &MachineSchedulerID); Ideally it wouldn't
94// be part of the standard pass pipeline, and the target would just add a PostRA
95// scheduling pass wherever it wants.
96static cl::opt<bool> MISchedPostRA("misched-postra", cl::Hidden,
97 cl::desc("Run MachineScheduler post regalloc (independent of preRA sched)"));
98
Cameron Zwarich71f0acb2013-02-10 06:42:34 +000099// Experimental option to run live interval analysis early.
Jakob Stoklund Olesen1c465892012-08-03 22:12:54 +0000100static cl::opt<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden,
101 cl::desc("Run live interval analysis earlier in the pipeline"));
102
Andrew Tricke9a951c2012-02-15 03:21:51 +0000103/// Allow standard passes to be disabled by command line options. This supports
104/// simple binary flags that either suppress the pass or do nothing.
105/// i.e. -disable-mypass=false has no effect.
106/// These should be converted to boolOrDefault in order to use applyOverride.
Andrew Tricke2203232013-04-10 01:06:56 +0000107static IdentifyingPassPtr applyDisable(IdentifyingPassPtr PassID,
108 bool Override) {
Andrew Tricke9a951c2012-02-15 03:21:51 +0000109 if (Override)
Andrew Tricke2203232013-04-10 01:06:56 +0000110 return IdentifyingPassPtr();
Bob Wilsonb9b69362012-07-02 19:48:37 +0000111 return PassID;
Andrew Tricke9a951c2012-02-15 03:21:51 +0000112}
113
114/// Allow Pass selection to be overriden by command line options. This supports
115/// flags with ternary conditions. TargetID is passed through by default. The
116/// pass is suppressed when the option is false. When the option is true, the
117/// StandardID is selected if the target provides no default.
Andrew Tricke2203232013-04-10 01:06:56 +0000118static IdentifyingPassPtr applyOverride(IdentifyingPassPtr TargetID,
119 cl::boolOrDefault Override,
120 AnalysisID StandardID) {
Andrew Trickee874db2012-02-11 07:11:32 +0000121 switch (Override) {
122 case cl::BOU_UNSET:
Andrew Tricke9a951c2012-02-15 03:21:51 +0000123 return TargetID;
Andrew Trickee874db2012-02-11 07:11:32 +0000124 case cl::BOU_TRUE:
Andrew Tricke2203232013-04-10 01:06:56 +0000125 if (TargetID.isValid())
Andrew Tricke9a951c2012-02-15 03:21:51 +0000126 return TargetID;
Bob Wilsonb9b69362012-07-02 19:48:37 +0000127 if (StandardID == 0)
Andrew Trickee874db2012-02-11 07:11:32 +0000128 report_fatal_error("Target cannot enable pass");
Andrew Tricke9a951c2012-02-15 03:21:51 +0000129 return StandardID;
Andrew Trickee874db2012-02-11 07:11:32 +0000130 case cl::BOU_FALSE:
Andrew Tricke2203232013-04-10 01:06:56 +0000131 return IdentifyingPassPtr();
Andrew Trickee874db2012-02-11 07:11:32 +0000132 }
133 llvm_unreachable("Invalid command line option state");
134}
135
Andrew Tricke9a951c2012-02-15 03:21:51 +0000136/// Allow standard passes to be disabled by the command line, regardless of who
137/// is adding the pass.
138///
139/// StandardID is the pass identified in the standard pass pipeline and provided
140/// to addPass(). It may be a target-specific ID in the case that the target
141/// directly adds its own pass, but in that case we harmlessly fall through.
142///
143/// TargetID is the pass that the target has configured to override StandardID.
144///
145/// StandardID may be a pseudo ID. In that case TargetID is the name of the real
146/// pass to run. This allows multiple options to control a single pass depending
147/// on where in the pipeline that pass is added.
Andrew Tricke2203232013-04-10 01:06:56 +0000148static IdentifyingPassPtr overridePass(AnalysisID StandardID,
149 IdentifyingPassPtr TargetID) {
Andrew Tricke9a951c2012-02-15 03:21:51 +0000150 if (StandardID == &PostRASchedulerID)
151 return applyDisable(TargetID, DisablePostRA);
152
153 if (StandardID == &BranchFolderPassID)
154 return applyDisable(TargetID, DisableBranchFold);
155
156 if (StandardID == &TailDuplicateID)
157 return applyDisable(TargetID, DisableTailDuplicate);
158
159 if (StandardID == &TargetPassConfig::EarlyTailDuplicateID)
160 return applyDisable(TargetID, DisableEarlyTailDup);
161
162 if (StandardID == &MachineBlockPlacementID)
Benjamin Kramer70671b92013-03-29 17:14:24 +0000163 return applyDisable(TargetID, DisableBlockPlacement);
Andrew Tricke9a951c2012-02-15 03:21:51 +0000164
165 if (StandardID == &StackSlotColoringID)
166 return applyDisable(TargetID, DisableSSC);
167
168 if (StandardID == &DeadMachineInstructionElimID)
169 return applyDisable(TargetID, DisableMachineDCE);
170
Jakob Stoklund Olesenf8a63a12012-07-04 00:09:54 +0000171 if (StandardID == &EarlyIfConverterID)
Jakob Stoklund Olesen0f6e8bb2012-10-03 00:51:32 +0000172 return applyDisable(TargetID, DisableEarlyIfConversion);
Jakob Stoklund Olesenf8a63a12012-07-04 00:09:54 +0000173
Andrew Tricke9a951c2012-02-15 03:21:51 +0000174 if (StandardID == &MachineLICMID)
175 return applyDisable(TargetID, DisableMachineLICM);
176
177 if (StandardID == &MachineCSEID)
178 return applyDisable(TargetID, DisableMachineCSE);
179
180 if (StandardID == &MachineSchedulerID)
181 return applyOverride(TargetID, EnableMachineSched, StandardID);
182
183 if (StandardID == &TargetPassConfig::PostRAMachineLICMID)
184 return applyDisable(TargetID, DisablePostRAMachineLICM);
185
186 if (StandardID == &MachineSinkingID)
187 return applyDisable(TargetID, DisableMachineSink);
188
189 if (StandardID == &MachineCopyPropagationID)
190 return applyDisable(TargetID, DisableCopyProp);
191
192 return TargetID;
193}
194
Jim Laskey29e635d2006-08-02 12:30:23 +0000195//===---------------------------------------------------------------------===//
Andrew Trickb7551332012-02-04 02:56:45 +0000196/// TargetPassConfig
197//===---------------------------------------------------------------------===//
198
199INITIALIZE_PASS(TargetPassConfig, "targetpassconfig",
200 "Target Pass Configuration", false, false)
201char TargetPassConfig::ID = 0;
202
Andrew Tricke9a951c2012-02-15 03:21:51 +0000203// Pseudo Pass IDs.
204char TargetPassConfig::EarlyTailDuplicateID = 0;
205char TargetPassConfig::PostRAMachineLICMID = 0;
206
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000207namespace llvm {
208class PassConfigImpl {
209public:
210 // List of passes explicitly substituted by this target. Normally this is
211 // empty, but it is a convenient way to suppress or replace specific passes
212 // that are part of a standard pass pipeline without overridding the entire
213 // pipeline. This mechanism allows target options to inherit a standard pass's
214 // user interface. For example, a target may disable a standard pass by
Bob Wilsonb9b69362012-07-02 19:48:37 +0000215 // default by substituting a pass ID of zero, and the user may still enable
216 // that standard pass with an explicit command line option.
Andrew Tricke2203232013-04-10 01:06:56 +0000217 DenseMap<AnalysisID,IdentifyingPassPtr> TargetPasses;
Bob Wilson33e51882012-05-30 00:17:12 +0000218
219 /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass
220 /// is inserted after each instance of the first one.
Andrew Tricke2203232013-04-10 01:06:56 +0000221 SmallVector<std::pair<AnalysisID, IdentifyingPassPtr>, 4> InsertedPasses;
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000222};
223} // namespace llvm
224
Andrew Trickb7551332012-02-04 02:56:45 +0000225// Out of line virtual method.
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000226TargetPassConfig::~TargetPassConfig() {
227 delete Impl;
228}
Andrew Trickb7551332012-02-04 02:56:45 +0000229
Andrew Trick58648e42012-02-08 21:22:48 +0000230// Out of line constructor provides default values for pass options and
231// registers all common codegen passes.
Andrew Trickf8ea1082012-02-04 02:56:59 +0000232TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm)
Bob Wilsoncac3b902012-07-02 19:48:45 +0000233 : ImmutablePass(ID), PM(&pm), StartAfter(0), StopAfter(0),
234 Started(true), Stopped(false), TM(tm), Impl(0), Initialized(false),
Andrew Trickdd37d522012-02-08 21:22:39 +0000235 DisableVerify(false),
236 EnableTailMerge(true) {
237
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000238 Impl = new PassConfigImpl();
239
Andrew Trickb7551332012-02-04 02:56:45 +0000240 // Register all target independent codegen passes to activate their PassIDs,
241 // including this pass itself.
242 initializeCodeGen(*PassRegistry::getPassRegistry());
Andrew Tricke9a951c2012-02-15 03:21:51 +0000243
244 // Substitute Pseudo Pass IDs for real ones.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000245 substitutePass(&EarlyTailDuplicateID, &TailDuplicateID);
246 substitutePass(&PostRAMachineLICMID, &MachineLICMID);
Andrew Tricke9a951c2012-02-15 03:21:51 +0000247
248 // Temporarily disable experimental passes.
Andrew Trick108c88c2012-11-13 08:47:29 +0000249 const TargetSubtargetInfo &ST = TM->getSubtarget<TargetSubtargetInfo>();
Andrew Trick71e8bb62013-09-26 05:53:35 +0000250 if (!ST.useMachineScheduler())
Andrew Trick108c88c2012-11-13 08:47:29 +0000251 disablePass(&MachineSchedulerID);
Andrew Trickb7551332012-02-04 02:56:45 +0000252}
253
Bob Wilson33e51882012-05-30 00:17:12 +0000254/// Insert InsertedPassID pass after TargetPassID.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000255void TargetPassConfig::insertPass(AnalysisID TargetPassID,
Andrew Tricke2203232013-04-10 01:06:56 +0000256 IdentifyingPassPtr InsertedPassID) {
Benjamin Kramere7c45bc2013-04-11 11:57:01 +0000257 assert(((!InsertedPassID.isInstance() &&
258 TargetPassID != InsertedPassID.getID()) ||
259 (InsertedPassID.isInstance() &&
260 TargetPassID != InsertedPassID.getInstance()->getPassID())) &&
Andrew Tricke2203232013-04-10 01:06:56 +0000261 "Insert a pass after itself!");
262 std::pair<AnalysisID, IdentifyingPassPtr> P(TargetPassID, InsertedPassID);
Bob Wilson33e51882012-05-30 00:17:12 +0000263 Impl->InsertedPasses.push_back(P);
264}
265
Andrew Trickb7551332012-02-04 02:56:45 +0000266/// createPassConfig - Create a pass configuration object to be used by
267/// addPassToEmitX methods for generating a pipeline of CodeGen passes.
268///
269/// Targets may override this to extend TargetPassConfig.
Andrew Trickf8ea1082012-02-04 02:56:59 +0000270TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
271 return new TargetPassConfig(this, PM);
Andrew Trickb7551332012-02-04 02:56:45 +0000272}
273
274TargetPassConfig::TargetPassConfig()
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000275 : ImmutablePass(ID), PM(0) {
Andrew Trickb7551332012-02-04 02:56:45 +0000276 llvm_unreachable("TargetPassConfig should not be constructed on-the-fly");
277}
278
Andrew Trickdd37d522012-02-08 21:22:39 +0000279// Helper to verify the analysis is really immutable.
280void TargetPassConfig::setOpt(bool &Opt, bool Val) {
281 assert(!Initialized && "PassConfig is immutable");
282 Opt = Val;
283}
284
Bob Wilsonb9b69362012-07-02 19:48:37 +0000285void TargetPassConfig::substitutePass(AnalysisID StandardID,
Andrew Tricke2203232013-04-10 01:06:56 +0000286 IdentifyingPassPtr TargetID) {
Bob Wilsonb9b69362012-07-02 19:48:37 +0000287 Impl->TargetPasses[StandardID] = TargetID;
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000288}
Andrew Trickee874db2012-02-11 07:11:32 +0000289
Andrew Tricke2203232013-04-10 01:06:56 +0000290IdentifyingPassPtr TargetPassConfig::getPassSubstitution(AnalysisID ID) const {
291 DenseMap<AnalysisID, IdentifyingPassPtr>::const_iterator
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000292 I = Impl->TargetPasses.find(ID);
293 if (I == Impl->TargetPasses.end())
294 return ID;
295 return I->second;
296}
297
Bob Wilsoncac3b902012-07-02 19:48:45 +0000298/// Add a pass to the PassManager if that pass is supposed to be run. If the
299/// Started/Stopped flags indicate either that the compilation should start at
300/// a later pass or that it should stop after an earlier pass, then do not add
301/// the pass. Finally, compare the current pass against the StartAfter
302/// and StopAfter options and change the Started/Stopped flags accordingly.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000303void TargetPassConfig::addPass(Pass *P) {
Bob Wilsona3f9fa72012-07-02 19:48:39 +0000304 assert(!Initialized && "PassConfig is immutable");
305
Chandler Carruth34263a02012-07-02 22:56:41 +0000306 // Cache the Pass ID here in case the pass manager finds this pass is
307 // redundant with ones already scheduled / available, and deletes it.
308 // Fundamentally, once we add the pass to the manager, we no longer own it
309 // and shouldn't reference it.
310 AnalysisID PassID = P->getPassID();
311
Bob Wilsoncac3b902012-07-02 19:48:45 +0000312 if (Started && !Stopped)
313 PM->add(P);
Benjamin Kramer483b9fb2013-08-05 11:11:11 +0000314 else
315 delete P;
Chandler Carruth34263a02012-07-02 22:56:41 +0000316 if (StopAfter == PassID)
Bob Wilsoncac3b902012-07-02 19:48:45 +0000317 Stopped = true;
Chandler Carruth34263a02012-07-02 22:56:41 +0000318 if (StartAfter == PassID)
Bob Wilsoncac3b902012-07-02 19:48:45 +0000319 Started = true;
320 if (Stopped && !Started)
321 report_fatal_error("Cannot stop compilation after pass that is not run");
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000322}
323
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000324/// Add a CodeGen pass at this point in the pipeline after checking for target
325/// and command line overrides.
Andrew Tricke2203232013-04-10 01:06:56 +0000326///
327/// addPass cannot return a pointer to the pass instance because is internal the
328/// PassManager and the instance we create here may already be freed.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000329AnalysisID TargetPassConfig::addPass(AnalysisID PassID) {
Andrew Tricke2203232013-04-10 01:06:56 +0000330 IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
331 IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID);
332 if (!FinalPtr.isValid())
333 return 0;
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000334
Andrew Tricke2203232013-04-10 01:06:56 +0000335 Pass *P;
336 if (FinalPtr.isInstance())
337 P = FinalPtr.getInstance();
338 else {
339 P = Pass::createPass(FinalPtr.getID());
340 if (!P)
341 llvm_unreachable("Pass ID not registered");
342 }
343 AnalysisID FinalID = P->getPassID();
344 addPass(P); // Ends the lifetime of P.
345
Bob Wilson33e51882012-05-30 00:17:12 +0000346 // Add the passes after the pass P if there is any.
Craig Toppere1c1d362013-07-03 05:11:49 +0000347 for (SmallVectorImpl<std::pair<AnalysisID, IdentifyingPassPtr> >::iterator
Bob Wilson33e51882012-05-30 00:17:12 +0000348 I = Impl->InsertedPasses.begin(), E = Impl->InsertedPasses.end();
349 I != E; ++I) {
Bob Wilsonb9b69362012-07-02 19:48:37 +0000350 if ((*I).first == PassID) {
Andrew Tricke2203232013-04-10 01:06:56 +0000351 assert((*I).second.isValid() && "Illegal Pass ID!");
352 Pass *NP;
353 if ((*I).second.isInstance())
354 NP = (*I).second.getInstance();
355 else {
356 NP = Pass::createPass((*I).second.getID());
357 assert(NP && "Pass ID not registered");
358 }
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000359 addPass(NP);
Bob Wilson33e51882012-05-30 00:17:12 +0000360 }
361 }
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000362 return FinalID;
Andrew Trickf8ea1082012-02-04 02:56:59 +0000363}
Andrew Trickde401d32012-02-04 02:56:48 +0000364
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000365void TargetPassConfig::printAndVerify(const char *Banner) {
Andrew Trickde401d32012-02-04 02:56:48 +0000366 if (TM->shouldPrintMachineCode())
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000367 addPass(createMachineFunctionPrinterPass(dbgs(), Banner));
Andrew Trickde401d32012-02-04 02:56:48 +0000368
369 if (VerifyMachineCode)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000370 addPass(createMachineVerifierPass(Banner));
Andrew Trickde401d32012-02-04 02:56:48 +0000371}
372
Andrew Trickf8ea1082012-02-04 02:56:59 +0000373/// Add common target configurable passes that perform LLVM IR to IR transforms
374/// following machine independent optimization.
375void TargetPassConfig::addIRPasses() {
Andrew Trickde401d32012-02-04 02:56:48 +0000376 // Basic AliasAnalysis support.
377 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
378 // BasicAliasAnalysis wins if they disagree. This is intended to help
379 // support "obvious" type-punning idioms.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000380 addPass(createTypeBasedAliasAnalysisPass());
381 addPass(createBasicAliasAnalysisPass());
Andrew Trickde401d32012-02-04 02:56:48 +0000382
383 // Before running any passes, run the verifier to determine if the input
384 // coming from the front-end and/or optimizer is valid.
385 if (!DisableVerify)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000386 addPass(createVerifierPass());
Andrew Trickde401d32012-02-04 02:56:48 +0000387
388 // Run loop strength reduction before anything else.
389 if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
Chandler Carruth26c59fa2013-01-07 14:41:08 +0000390 addPass(createLoopStrengthReducePass());
Andrew Trickde401d32012-02-04 02:56:48 +0000391 if (PrintLSR)
Chandler Carruth9d805132014-01-12 11:30:46 +0000392 addPass(createPrintFunctionPass(dbgs(), "\n\n*** Code after LSR ***\n"));
Andrew Trickde401d32012-02-04 02:56:48 +0000393 }
394
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000395 addPass(createGCLoweringPass());
Andrew Trickde401d32012-02-04 02:56:48 +0000396
397 // Make sure that no unreachable blocks are instruction selected.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000398 addPass(createUnreachableBlockEliminationPass());
399}
400
401/// Turn exception handling constructs into something the code generators can
402/// handle.
403void TargetPassConfig::addPassesToHandleExceptions() {
404 switch (TM->getMCAsmInfo()->getExceptionHandlingType()) {
405 case ExceptionHandling::SjLj:
406 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
407 // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
408 // catch info can get misplaced when a selector ends up more than one block
409 // removed from the parent invoke(s). This could happen when a landing
410 // pad is shared by multiple invokes and is also a target of a normal
411 // edge from elsewhere.
Bill Wendlingafc10362013-06-19 20:51:24 +0000412 addPass(createSjLjEHPreparePass(TM));
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000413 // FALLTHROUGH
414 case ExceptionHandling::DwarfCFI:
415 case ExceptionHandling::ARM:
416 case ExceptionHandling::Win64:
Bill Wendlingafc10362013-06-19 20:51:24 +0000417 addPass(createDwarfEHPass(TM));
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000418 break;
419 case ExceptionHandling::None:
Bill Wendling7a639ea2013-06-19 21:07:11 +0000420 addPass(createLowerInvokePass(TM));
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000421
422 // The lower invoke pass may create unreachable code. Remove it.
423 addPass(createUnreachableBlockEliminationPass());
424 break;
425 }
Andrew Trickf8ea1082012-02-04 02:56:59 +0000426}
Andrew Trickde401d32012-02-04 02:56:48 +0000427
Bill Wendlingc786b312012-11-30 22:08:55 +0000428/// Add pass to prepare the LLVM IR for code generation. This should be done
429/// before exception handling preparation passes.
430void TargetPassConfig::addCodeGenPrepare() {
431 if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
Bill Wendling7a639ea2013-06-19 21:07:11 +0000432 addPass(createCodeGenPreparePass(TM));
Bill Wendlingc786b312012-11-30 22:08:55 +0000433}
434
Andrew Trickf8ea1082012-02-04 02:56:59 +0000435/// Add common passes that perform LLVM IR to IR transforms in preparation for
436/// instruction selection.
437void TargetPassConfig::addISelPrepare() {
Andrew Trickde401d32012-02-04 02:56:48 +0000438 addPreISel();
439
Josh Magee22b8ba22013-12-19 03:17:11 +0000440 addPass(createStackProtectorPass(TM));
441
Andrew Trickde401d32012-02-04 02:56:48 +0000442 if (PrintISelInput)
Chandler Carruth9d805132014-01-12 11:30:46 +0000443 addPass(createPrintFunctionPass(
444 dbgs(), "\n\n*** Final LLVM Code input to ISel ***\n"));
Andrew Trickde401d32012-02-04 02:56:48 +0000445
446 // All passes which modify the LLVM IR are now complete; run the verifier
447 // to ensure that the IR is valid.
448 if (!DisableVerify)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000449 addPass(createVerifierPass());
Andrew Trickf8ea1082012-02-04 02:56:59 +0000450}
Andrew Trickde401d32012-02-04 02:56:48 +0000451
Andrew Trickf5426752012-02-09 00:40:55 +0000452/// Add the complete set of target-independent postISel code generator passes.
453///
454/// This can be read as the standard order of major LLVM CodeGen stages. Stages
455/// with nontrivial configuration or multiple passes are broken out below in
456/// add%Stage routines.
457///
458/// Any TargetPassConfig::addXX routine may be overriden by the Target. The
459/// addPre/Post methods with empty header implementations allow injecting
460/// target-specific fixups just before or after major stages. Additionally,
461/// targets have the flexibility to change pass order within a stage by
462/// overriding default implementation of add%Stage routines below. Each
463/// technique has maintainability tradeoffs because alternate pass orders are
464/// not well supported. addPre/Post works better if the target pass is easily
465/// tied to a common pass. But if it has subtle dependencies on multiple passes,
Andrew Trick09fc1bb2012-02-10 07:08:25 +0000466/// the target should override the stage instead.
Andrew Trickf5426752012-02-09 00:40:55 +0000467///
468/// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
469/// before/after any target-independent pass. But it's currently overkill.
Andrew Trickf8ea1082012-02-04 02:56:59 +0000470void TargetPassConfig::addMachinePasses() {
Bob Wilson33e51882012-05-30 00:17:12 +0000471 // Insert a machine instr printer pass after the specified pass.
472 // If -print-machineinstrs specified, print machineinstrs after all passes.
473 if (StringRef(PrintMachineInstrs.getValue()).equals(""))
474 TM->Options.PrintMachineCode = true;
475 else if (!StringRef(PrintMachineInstrs.getValue())
476 .equals("option-unspecified")) {
477 const PassRegistry *PR = PassRegistry::getPassRegistry();
478 const PassInfo *TPI = PR->getPassInfo(PrintMachineInstrs.getValue());
479 const PassInfo *IPI = PR->getPassInfo(StringRef("print-machineinstrs"));
480 assert (TPI && IPI && "Pass ID not registered!");
Roman Divackyad06cee2012-09-05 22:26:57 +0000481 const char *TID = (const char *)(TPI->getTypeInfo());
482 const char *IID = (const char *)(IPI->getTypeInfo());
Bob Wilsonb9b69362012-07-02 19:48:37 +0000483 insertPass(TID, IID);
Bob Wilson33e51882012-05-30 00:17:12 +0000484 }
485
Jakob Stoklund Olesen29506f52012-07-04 19:28:27 +0000486 // Print the instruction selected machine code...
487 printAndVerify("After Instruction Selection");
488
Andrew Trickde401d32012-02-04 02:56:48 +0000489 // Expand pseudo-instructions emitted by ISel.
Jakob Stoklund Olesen1d026262012-08-20 20:52:08 +0000490 if (addPass(&ExpandISelPseudosID))
491 printAndVerify("After ExpandISelPseudos");
Andrew Trickde401d32012-02-04 02:56:48 +0000492
Andrew Trickf5426752012-02-09 00:40:55 +0000493 // Add passes that optimize machine instructions in SSA form.
Andrew Trickde401d32012-02-04 02:56:48 +0000494 if (getOptLevel() != CodeGenOpt::None) {
Andrew Trickf5426752012-02-09 00:40:55 +0000495 addMachineSSAOptimization();
Craig Topper36f29122012-11-19 00:11:50 +0000496 } else {
Andrew Trickf5426752012-02-09 00:40:55 +0000497 // If the target requests it, assign local variables to stack slots relative
498 // to one another and simplify frame index references where possible.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000499 addPass(&LocalStackSlotAllocationID);
Andrew Trickde401d32012-02-04 02:56:48 +0000500 }
501
502 // Run pre-ra passes.
503 if (addPreRegAlloc())
504 printAndVerify("After PreRegAlloc passes");
505
Andrew Trickf5426752012-02-09 00:40:55 +0000506 // Run register allocation and passes that are tightly coupled with it,
507 // including phi elimination and scheduling.
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000508 if (getOptimizeRegAlloc())
509 addOptimizedRegAlloc(createRegAllocPass(true));
510 else
511 addFastRegAlloc(createRegAllocPass(false));
Andrew Trickde401d32012-02-04 02:56:48 +0000512
513 // Run post-ra passes.
514 if (addPostRegAlloc())
515 printAndVerify("After PostRegAlloc passes");
516
517 // Insert prolog/epilog code. Eliminate abstract frame index references...
Bob Wilsonb9b69362012-07-02 19:48:37 +0000518 addPass(&PrologEpilogCodeInserterID);
Andrew Trickde401d32012-02-04 02:56:48 +0000519 printAndVerify("After PrologEpilogCodeInserter");
520
Andrew Trickf5426752012-02-09 00:40:55 +0000521 /// Add passes that optimize machine instructions after register allocation.
522 if (getOptLevel() != CodeGenOpt::None)
523 addMachineLateOptimization();
Andrew Trickde401d32012-02-04 02:56:48 +0000524
525 // Expand pseudo instructions before second scheduling pass.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000526 addPass(&ExpandPostRAPseudosID);
Jakob Stoklund Olesene433c682012-03-28 20:49:30 +0000527 printAndVerify("After ExpandPostRAPseudos");
Andrew Trickde401d32012-02-04 02:56:48 +0000528
529 // Run pre-sched2 passes.
530 if (addPreSched2())
Jakob Stoklund Olesend1bd8fb2012-03-28 23:31:15 +0000531 printAndVerify("After PreSched2 passes");
Andrew Trickde401d32012-02-04 02:56:48 +0000532
533 // Second pass scheduler.
Andrew Tricke9a951c2012-02-15 03:21:51 +0000534 if (getOptLevel() != CodeGenOpt::None) {
Andrew Trick17080b92013-12-28 21:56:51 +0000535 if (MISchedPostRA)
536 addPass(&PostMachineSchedulerID);
537 else
538 addPass(&PostRASchedulerID);
Jakob Stoklund Olesenc3e80cc2012-03-28 23:54:28 +0000539 printAndVerify("After PostRAScheduler");
Andrew Trickde401d32012-02-04 02:56:48 +0000540 }
541
Andrew Trickf5426752012-02-09 00:40:55 +0000542 // GC
Evan Cheng59421ae2012-12-21 02:57:04 +0000543 if (addGCPasses()) {
544 if (PrintGCInfo)
545 addPass(createGCInfoPrinter(dbgs()));
546 }
Andrew Trickde401d32012-02-04 02:56:48 +0000547
Andrew Trickf5426752012-02-09 00:40:55 +0000548 // Basic block placement.
Andrew Tricke9a951c2012-02-15 03:21:51 +0000549 if (getOptLevel() != CodeGenOpt::None)
Andrew Trickf5426752012-02-09 00:40:55 +0000550 addBlockPlacement();
Andrew Trickde401d32012-02-04 02:56:48 +0000551
552 if (addPreEmitPass())
Jakob Stoklund Olesenc3e80cc2012-03-28 23:54:28 +0000553 printAndVerify("After PreEmit passes");
Juergen Ributzkae8294752013-12-14 06:53:06 +0000554
555 if (EnableStackMapLiveness || EnablePatchPointLiveness)
556 addPass(&StackMapLivenessID);
Andrew Trickde401d32012-02-04 02:56:48 +0000557}
558
Andrew Trickf5426752012-02-09 00:40:55 +0000559/// Add passes that optimize machine instructions in SSA form.
560void TargetPassConfig::addMachineSSAOptimization() {
561 // Pre-ra tail duplication.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000562 if (addPass(&EarlyTailDuplicateID))
Andrew Trickf5426752012-02-09 00:40:55 +0000563 printAndVerify("After Pre-RegAlloc TailDuplicate");
Andrew Trickf5426752012-02-09 00:40:55 +0000564
565 // Optimize PHIs before DCE: removing dead PHI cycles may make more
566 // instructions dead.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000567 addPass(&OptimizePHIsID);
Andrew Trickf5426752012-02-09 00:40:55 +0000568
Nadav Rotem7c277da2012-09-06 09:17:37 +0000569 // This pass merges large allocas. StackSlotColoring is a different pass
570 // which merges spill slots.
571 addPass(&StackColoringID);
572
Andrew Trickf5426752012-02-09 00:40:55 +0000573 // If the target requests it, assign local variables to stack slots relative
574 // to one another and simplify frame index references where possible.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000575 addPass(&LocalStackSlotAllocationID);
Andrew Trickf5426752012-02-09 00:40:55 +0000576
577 // With optimization, dead code should already be eliminated. However
578 // there is one known exception: lowered code for arguments that are only
579 // used by tail calls, where the tail calls reuse the incoming stack
580 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
Bob Wilsonb9b69362012-07-02 19:48:37 +0000581 addPass(&DeadMachineInstructionElimID);
Andrew Trickf5426752012-02-09 00:40:55 +0000582 printAndVerify("After codegen DCE pass");
583
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000584 // Allow targets to insert passes that improve instruction level parallelism,
585 // like if-conversion. Such passes will typically need dominator trees and
586 // loop info, just like LICM and CSE below.
587 if (addILPOpts())
588 printAndVerify("After ILP optimizations");
589
Bob Wilsonb9b69362012-07-02 19:48:37 +0000590 addPass(&MachineLICMID);
591 addPass(&MachineCSEID);
592 addPass(&MachineSinkingID);
Andrew Trickf5426752012-02-09 00:40:55 +0000593 printAndVerify("After Machine LICM, CSE and Sinking passes");
594
Bob Wilsonb9b69362012-07-02 19:48:37 +0000595 addPass(&PeepholeOptimizerID);
Andrew Trickf5426752012-02-09 00:40:55 +0000596 printAndVerify("After codegen peephole optimization pass");
597}
598
Andrew Trickb7551332012-02-04 02:56:45 +0000599//===---------------------------------------------------------------------===//
Andrew Trickf5426752012-02-09 00:40:55 +0000600/// Register Allocation Pass Configuration
Jim Laskey29e635d2006-08-02 12:30:23 +0000601//===---------------------------------------------------------------------===//
Andrew Trickf5426752012-02-09 00:40:55 +0000602
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000603bool TargetPassConfig::getOptimizeRegAlloc() const {
604 switch (OptimizeRegAlloc) {
605 case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
606 case cl::BOU_TRUE: return true;
607 case cl::BOU_FALSE: return false;
608 }
609 llvm_unreachable("Invalid optimize-regalloc state");
610}
611
Andrew Trickf5426752012-02-09 00:40:55 +0000612/// RegisterRegAlloc's global Registry tracks allocator registration.
Jim Laskey29e635d2006-08-02 12:30:23 +0000613MachinePassRegistry RegisterRegAlloc::Registry;
614
Andrew Trickf5426752012-02-09 00:40:55 +0000615/// A dummy default pass factory indicates whether the register allocator is
616/// overridden on the command line.
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000617static FunctionPass *useDefaultRegisterAllocator() { return 0; }
Jakob Stoklund Olesenb613ae22010-05-27 23:57:25 +0000618static RegisterRegAlloc
619defaultRegAlloc("default",
620 "pick register allocator based on -O option",
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000621 useDefaultRegisterAllocator);
Jim Laskey29e635d2006-08-02 12:30:23 +0000622
Andrew Trickf5426752012-02-09 00:40:55 +0000623/// -regalloc=... command line option.
Dan Gohmand78c4002008-05-13 00:00:25 +0000624static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
625 RegisterPassParser<RegisterRegAlloc> >
626RegAlloc("regalloc",
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000627 cl::init(&useDefaultRegisterAllocator),
Jakob Stoklund Olesenb613ae22010-05-27 23:57:25 +0000628 cl::desc("Register allocator to use"));
Alkis Evlogimenos5facafa2003-10-02 16:57:49 +0000629
Jim Laskey29e635d2006-08-02 12:30:23 +0000630
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000631/// Instantiate the default register allocator pass for this target for either
632/// the optimized or unoptimized allocation path. This will be added to the pass
633/// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
634/// in the optimized case.
635///
636/// A target that uses the standard regalloc pass order for fast or optimized
637/// allocation may still override this for per-target regalloc
638/// selection. But -regalloc=... always takes precedence.
639FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
640 if (Optimized)
641 return createGreedyRegisterAllocator();
642 else
643 return createFastRegisterAllocator();
644}
645
646/// Find and instantiate the register allocation pass requested by this target
647/// at the current optimization level. Different register allocators are
648/// defined as separate passes because they may require different analysis.
649///
650/// This helper ensures that the regalloc= option is always available,
651/// even for targets that override the default allocator.
652///
653/// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
654/// this can be folded into addPass.
655FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
Jim Laskey03593f72006-08-01 18:29:48 +0000656 RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
Jakob Stoklund Olesenb613ae22010-05-27 23:57:25 +0000657
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000658 // Initialize the global default.
Jim Laskey95eda5b2006-08-01 14:21:23 +0000659 if (!Ctor) {
Jim Laskey29e635d2006-08-02 12:30:23 +0000660 Ctor = RegAlloc;
661 RegisterRegAlloc::setDefault(RegAlloc);
Jim Laskey95eda5b2006-08-01 14:21:23 +0000662 }
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000663 if (Ctor != useDefaultRegisterAllocator)
Jakob Stoklund Olesenb613ae22010-05-27 23:57:25 +0000664 return Ctor();
665
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000666 // With no -regalloc= override, ask the target for a regalloc pass.
667 return createTargetRegisterAllocator(Optimized);
668}
669
670/// Add the minimum set of target-independent passes that are required for
671/// register allocation. No coalescing or scheduling.
672void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
Bob Wilsonb9b69362012-07-02 19:48:37 +0000673 addPass(&PHIEliminationID);
674 addPass(&TwoAddressInstructionPassID);
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000675
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000676 addPass(RegAllocPass);
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000677 printAndVerify("After Register Allocation");
Jim Laskeyd1a714e2006-07-27 20:05:00 +0000678}
Andrew Trickf5426752012-02-09 00:40:55 +0000679
680/// Add standard target-independent passes that are tightly coupled with
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000681/// optimized register allocation, including coalescing, machine instruction
682/// scheduling, and register allocation itself.
683void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
Bob Wilsonb9b69362012-07-02 19:48:37 +0000684 addPass(&ProcessImplicitDefsID);
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000685
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000686 // LiveVariables currently requires pure SSA form.
687 //
688 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
689 // LiveVariables can be removed completely, and LiveIntervals can be directly
690 // computed. (We still either need to regenerate kill flags after regalloc, or
691 // preferably fix the scavenger to not depend on them).
Bob Wilsonb9b69362012-07-02 19:48:37 +0000692 addPass(&LiveVariablesID);
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000693
Rafael Espindola9770bde2013-10-14 16:39:04 +0000694 // Edge splitting is smarter with machine loop info.
695 addPass(&MachineLoopInfoID);
696 addPass(&PHIEliminationID);
Jakob Stoklund Olesen1c465892012-08-03 22:12:54 +0000697
698 // Eventually, we want to run LiveIntervals before PHI elimination.
699 if (EarlyLiveIntervals)
700 addPass(&LiveIntervalsID);
701
Bob Wilsonb9b69362012-07-02 19:48:37 +0000702 addPass(&TwoAddressInstructionPassID);
Bob Wilsonb9b69362012-07-02 19:48:37 +0000703 addPass(&RegisterCoalescerID);
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000704
705 // PreRA instruction scheduling.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000706 if (addPass(&MachineSchedulerID))
Andrew Trick8823dec2012-03-14 04:00:41 +0000707 printAndVerify("After Machine Scheduling");
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000708
709 // Add the selected register allocation pass.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000710 addPass(RegAllocPass);
Jakob Stoklund Olesen59a0d322012-06-26 17:09:29 +0000711 printAndVerify("After Register Allocation, before rewriter");
712
713 // Allow targets to change the register assignments before rewriting.
714 if (addPreRewrite())
715 printAndVerify("After pre-rewrite passes");
Andrew Trickf5426752012-02-09 00:40:55 +0000716
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000717 // Finally rewrite virtual registers.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000718 addPass(&VirtRegRewriterID);
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000719 printAndVerify("After Virtual Register Rewriter");
720
Andrew Trickf5426752012-02-09 00:40:55 +0000721 // Perform stack slot coloring and post-ra machine LICM.
Andrew Trickd3f8fe82012-02-10 04:10:36 +0000722 //
723 // FIXME: Re-enable coloring with register when it's capable of adding
724 // kill markers.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000725 addPass(&StackSlotColoringID);
Andrew Trick899f46c2012-02-15 07:57:03 +0000726
727 // Run post-ra machine LICM to hoist reloads / remats.
728 //
729 // FIXME: can this move into MachineLateOptimization?
Bob Wilsonb9b69362012-07-02 19:48:37 +0000730 addPass(&PostRAMachineLICMID);
Andrew Trick899f46c2012-02-15 07:57:03 +0000731
732 printAndVerify("After StackSlotColoring and postra Machine LICM");
Andrew Trickf5426752012-02-09 00:40:55 +0000733}
734
735//===---------------------------------------------------------------------===//
736/// Post RegAlloc Pass Configuration
737//===---------------------------------------------------------------------===//
738
739/// Add passes that optimize machine instructions after register allocation.
740void TargetPassConfig::addMachineLateOptimization() {
741 // Branch folding must be run after regalloc and prolog/epilog insertion.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000742 if (addPass(&BranchFolderPassID))
Jakob Stoklund Olesen341e06f2012-03-28 20:47:37 +0000743 printAndVerify("After BranchFolding");
Andrew Trickf5426752012-02-09 00:40:55 +0000744
745 // Tail duplication.
Vincent Lejeune92b0a642013-12-07 01:49:19 +0000746 // Note that duplicating tail just increases code size and degrades
747 // performance for targets that require Structured Control Flow.
748 // In addition it can also make CFG irreducible. Thus we disable it.
749 if (!TM->requiresStructuredCFG() && addPass(&TailDuplicateID))
Jakob Stoklund Olesen341e06f2012-03-28 20:47:37 +0000750 printAndVerify("After TailDuplicate");
Andrew Trickf5426752012-02-09 00:40:55 +0000751
752 // Copy propagation.
Bob Wilsonb9b69362012-07-02 19:48:37 +0000753 if (addPass(&MachineCopyPropagationID))
Jakob Stoklund Olesen341e06f2012-03-28 20:47:37 +0000754 printAndVerify("After copy propagation pass");
Andrew Trickf5426752012-02-09 00:40:55 +0000755}
756
Evan Cheng59421ae2012-12-21 02:57:04 +0000757/// Add standard GC passes.
758bool TargetPassConfig::addGCPasses() {
759 addPass(&GCMachineCodeAnalysisID);
760 return true;
761}
762
Andrew Trickf5426752012-02-09 00:40:55 +0000763/// Add standard basic block placement passes.
764void TargetPassConfig::addBlockPlacement() {
Benjamin Kramer70671b92013-03-29 17:14:24 +0000765 if (addPass(&MachineBlockPlacementID)) {
Andrew Tricke9a951c2012-02-15 03:21:51 +0000766 // Run a separate pass to collect block placement statistics.
767 if (EnableBlockPlacementStats)
Bob Wilsonb9b69362012-07-02 19:48:37 +0000768 addPass(&MachineBlockPlacementStatsID);
Andrew Trickf5426752012-02-09 00:40:55 +0000769
Jakob Stoklund Olesenc3e80cc2012-03-28 23:54:28 +0000770 printAndVerify("After machine block placement.");
Andrew Trickf5426752012-02-09 00:40:55 +0000771 }
772}