blob: 1885af5b8c02f02e3ff47c485789b6c826e8474b [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"
17#include "llvm/Analysis/Verifier.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/Assembly/PrintModulePass.h"
Andrew Trickd5422652012-02-04 02:56:48 +000019#include "llvm/CodeGen/GCStrategy.h"
Andrew Trickd5422652012-02-04 02:56:48 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Andrew Trickd5422652012-02-04 02:56:48 +000021#include "llvm/CodeGen/RegAllocRegistry.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
Andrew Trickd5422652012-02-04 02:56:48 +000033static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden,
34 cl::desc("Disable Post Regalloc"));
35static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden,
36 cl::desc("Disable branch folding"));
37static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
38 cl::desc("Disable tail duplication"));
39static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
40 cl::desc("Disable pre-register allocation tail duplication"));
Chandler Carruth9e67db42012-04-16 13:49:17 +000041static cl::opt<bool> DisableBlockPlacement("disable-block-placement",
Benjamin Kramer74a45332013-03-29 17:14:24 +000042 cl::Hidden, cl::desc("Disable probability-driven block placement"));
Andrew Trickd5422652012-02-04 02:56:48 +000043static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats",
44 cl::Hidden, cl::desc("Collect probability-driven block placement stats"));
Andrew Trickd5422652012-02-04 02:56:48 +000045static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
46 cl::desc("Disable Stack Slot Coloring"));
47static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden,
48 cl::desc("Disable Machine Dead Code Elimination"));
Jakob Stoklund Olesen0d141f82012-10-03 00:51:32 +000049static cl::opt<bool> DisableEarlyIfConversion("disable-early-ifcvt", cl::Hidden,
50 cl::desc("Disable Early If-conversion"));
Andrew Trickd5422652012-02-04 02:56:48 +000051static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
52 cl::desc("Disable Machine LICM"));
53static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden,
54 cl::desc("Disable Machine Common Subexpression Elimination"));
Andrew Trick8dd26252012-02-10 04:10:36 +000055static cl::opt<cl::boolOrDefault>
56OptimizeRegAlloc("optimize-regalloc", cl::Hidden,
57 cl::desc("Enable optimized register allocation compilation path."));
Andrew Trick746f24b2012-02-11 07:11:32 +000058static cl::opt<cl::boolOrDefault>
59EnableMachineSched("enable-misched", cl::Hidden,
Andrew Trick8dd26252012-02-10 04:10:36 +000060 cl::desc("Enable the machine instruction scheduling pass."));
61static cl::opt<bool> EnableStrongPHIElim("strong-phi-elim", cl::Hidden,
62 cl::desc("Use strong PHI elimination."));
Andrew Trickd5422652012-02-04 02:56:48 +000063static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
64 cl::Hidden,
65 cl::desc("Disable Machine LICM"));
66static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
67 cl::desc("Disable Machine Sinking"));
68static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
69 cl::desc("Disable Loop Strength Reduction Pass"));
70static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
71 cl::desc("Disable Codegen Prepare"));
72static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden,
Evan Cheng01b623c2012-02-20 23:28:17 +000073 cl::desc("Disable Copy Propagation pass"));
Andrew Trickd5422652012-02-04 02:56:48 +000074static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
75 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
76static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
77 cl::desc("Print LLVM IR input to isel pass"));
78static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
79 cl::desc("Dump garbage collector data"));
80static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
81 cl::desc("Verify generated machine code"),
82 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
Bob Wilson6e1b8122012-05-30 00:17:12 +000083static cl::opt<std::string>
84PrintMachineInstrs("print-machineinstrs", cl::ValueOptional,
85 cl::desc("Print machine instrs"),
86 cl::value_desc("pass-name"), cl::init("option-unspecified"));
Andrew Trickd5422652012-02-04 02:56:48 +000087
Cameron Zwarichd7c7a682013-02-10 06:42:34 +000088// Experimental option to run live interval analysis early.
Jakob Stoklund Olesendcc44362012-08-03 22:12:54 +000089static cl::opt<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden,
90 cl::desc("Run live interval analysis earlier in the pipeline"));
91
Andrew Trick79bf2882012-02-15 03:21:51 +000092/// Allow standard passes to be disabled by command line options. This supports
93/// simple binary flags that either suppress the pass or do nothing.
94/// i.e. -disable-mypass=false has no effect.
95/// These should be converted to boolOrDefault in order to use applyOverride.
Andrew Trick5ed02832013-04-10 01:06:56 +000096static IdentifyingPassPtr applyDisable(IdentifyingPassPtr PassID,
97 bool Override) {
Andrew Trick79bf2882012-02-15 03:21:51 +000098 if (Override)
Andrew Trick5ed02832013-04-10 01:06:56 +000099 return IdentifyingPassPtr();
Bob Wilson3fb99a72012-07-02 19:48:37 +0000100 return PassID;
Andrew Trick79bf2882012-02-15 03:21:51 +0000101}
102
103/// Allow Pass selection to be overriden by command line options. This supports
104/// flags with ternary conditions. TargetID is passed through by default. The
105/// pass is suppressed when the option is false. When the option is true, the
106/// StandardID is selected if the target provides no default.
Andrew Trick5ed02832013-04-10 01:06:56 +0000107static IdentifyingPassPtr applyOverride(IdentifyingPassPtr TargetID,
108 cl::boolOrDefault Override,
109 AnalysisID StandardID) {
Andrew Trick746f24b2012-02-11 07:11:32 +0000110 switch (Override) {
111 case cl::BOU_UNSET:
Andrew Trick79bf2882012-02-15 03:21:51 +0000112 return TargetID;
Andrew Trick746f24b2012-02-11 07:11:32 +0000113 case cl::BOU_TRUE:
Andrew Trick5ed02832013-04-10 01:06:56 +0000114 if (TargetID.isValid())
Andrew Trick79bf2882012-02-15 03:21:51 +0000115 return TargetID;
Bob Wilson3fb99a72012-07-02 19:48:37 +0000116 if (StandardID == 0)
Andrew Trick746f24b2012-02-11 07:11:32 +0000117 report_fatal_error("Target cannot enable pass");
Andrew Trick79bf2882012-02-15 03:21:51 +0000118 return StandardID;
Andrew Trick746f24b2012-02-11 07:11:32 +0000119 case cl::BOU_FALSE:
Andrew Trick5ed02832013-04-10 01:06:56 +0000120 return IdentifyingPassPtr();
Andrew Trick746f24b2012-02-11 07:11:32 +0000121 }
122 llvm_unreachable("Invalid command line option state");
123}
124
Andrew Trick79bf2882012-02-15 03:21:51 +0000125/// Allow standard passes to be disabled by the command line, regardless of who
126/// is adding the pass.
127///
128/// StandardID is the pass identified in the standard pass pipeline and provided
129/// to addPass(). It may be a target-specific ID in the case that the target
130/// directly adds its own pass, but in that case we harmlessly fall through.
131///
132/// TargetID is the pass that the target has configured to override StandardID.
133///
134/// StandardID may be a pseudo ID. In that case TargetID is the name of the real
135/// pass to run. This allows multiple options to control a single pass depending
136/// on where in the pipeline that pass is added.
Andrew Trick5ed02832013-04-10 01:06:56 +0000137static IdentifyingPassPtr overridePass(AnalysisID StandardID,
138 IdentifyingPassPtr TargetID) {
Andrew Trick79bf2882012-02-15 03:21:51 +0000139 if (StandardID == &PostRASchedulerID)
140 return applyDisable(TargetID, DisablePostRA);
141
142 if (StandardID == &BranchFolderPassID)
143 return applyDisable(TargetID, DisableBranchFold);
144
145 if (StandardID == &TailDuplicateID)
146 return applyDisable(TargetID, DisableTailDuplicate);
147
148 if (StandardID == &TargetPassConfig::EarlyTailDuplicateID)
149 return applyDisable(TargetID, DisableEarlyTailDup);
150
151 if (StandardID == &MachineBlockPlacementID)
Benjamin Kramer74a45332013-03-29 17:14:24 +0000152 return applyDisable(TargetID, DisableBlockPlacement);
Andrew Trick79bf2882012-02-15 03:21:51 +0000153
154 if (StandardID == &StackSlotColoringID)
155 return applyDisable(TargetID, DisableSSC);
156
157 if (StandardID == &DeadMachineInstructionElimID)
158 return applyDisable(TargetID, DisableMachineDCE);
159
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000160 if (StandardID == &EarlyIfConverterID)
Jakob Stoklund Olesen0d141f82012-10-03 00:51:32 +0000161 return applyDisable(TargetID, DisableEarlyIfConversion);
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000162
Andrew Trick79bf2882012-02-15 03:21:51 +0000163 if (StandardID == &MachineLICMID)
164 return applyDisable(TargetID, DisableMachineLICM);
165
166 if (StandardID == &MachineCSEID)
167 return applyDisable(TargetID, DisableMachineCSE);
168
169 if (StandardID == &MachineSchedulerID)
170 return applyOverride(TargetID, EnableMachineSched, StandardID);
171
172 if (StandardID == &TargetPassConfig::PostRAMachineLICMID)
173 return applyDisable(TargetID, DisablePostRAMachineLICM);
174
175 if (StandardID == &MachineSinkingID)
176 return applyDisable(TargetID, DisableMachineSink);
177
178 if (StandardID == &MachineCopyPropagationID)
179 return applyDisable(TargetID, DisableCopyProp);
180
181 return TargetID;
182}
183
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000184//===---------------------------------------------------------------------===//
Andrew Trick74613342012-02-04 02:56:45 +0000185/// TargetPassConfig
186//===---------------------------------------------------------------------===//
187
188INITIALIZE_PASS(TargetPassConfig, "targetpassconfig",
189 "Target Pass Configuration", false, false)
190char TargetPassConfig::ID = 0;
191
Andrew Trick79bf2882012-02-15 03:21:51 +0000192// Pseudo Pass IDs.
193char TargetPassConfig::EarlyTailDuplicateID = 0;
194char TargetPassConfig::PostRAMachineLICMID = 0;
195
Andrew Trick5e108ee2012-02-15 03:21:47 +0000196namespace llvm {
197class PassConfigImpl {
198public:
199 // List of passes explicitly substituted by this target. Normally this is
200 // empty, but it is a convenient way to suppress or replace specific passes
201 // that are part of a standard pass pipeline without overridding the entire
202 // pipeline. This mechanism allows target options to inherit a standard pass's
203 // user interface. For example, a target may disable a standard pass by
Bob Wilson3fb99a72012-07-02 19:48:37 +0000204 // default by substituting a pass ID of zero, and the user may still enable
205 // that standard pass with an explicit command line option.
Andrew Trick5ed02832013-04-10 01:06:56 +0000206 DenseMap<AnalysisID,IdentifyingPassPtr> TargetPasses;
Bob Wilson6e1b8122012-05-30 00:17:12 +0000207
208 /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass
209 /// is inserted after each instance of the first one.
Andrew Trick5ed02832013-04-10 01:06:56 +0000210 SmallVector<std::pair<AnalysisID, IdentifyingPassPtr>, 4> InsertedPasses;
Andrew Trick5e108ee2012-02-15 03:21:47 +0000211};
212} // namespace llvm
213
Andrew Trick74613342012-02-04 02:56:45 +0000214// Out of line virtual method.
Andrew Trick5e108ee2012-02-15 03:21:47 +0000215TargetPassConfig::~TargetPassConfig() {
216 delete Impl;
217}
Andrew Trick74613342012-02-04 02:56:45 +0000218
Andrew Trick61f1e3d2012-02-08 21:22:48 +0000219// Out of line constructor provides default values for pass options and
220// registers all common codegen passes.
Andrew Trick061efcf2012-02-04 02:56:59 +0000221TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm)
Bob Wilson30a507a2012-07-02 19:48:45 +0000222 : ImmutablePass(ID), PM(&pm), StartAfter(0), StopAfter(0),
223 Started(true), Stopped(false), TM(tm), Impl(0), Initialized(false),
Andrew Trickffea03f2012-02-08 21:22:39 +0000224 DisableVerify(false),
225 EnableTailMerge(true) {
226
Andrew Trick5e108ee2012-02-15 03:21:47 +0000227 Impl = new PassConfigImpl();
228
Andrew Trick74613342012-02-04 02:56:45 +0000229 // Register all target independent codegen passes to activate their PassIDs,
230 // including this pass itself.
231 initializeCodeGen(*PassRegistry::getPassRegistry());
Andrew Trick79bf2882012-02-15 03:21:51 +0000232
233 // Substitute Pseudo Pass IDs for real ones.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000234 substitutePass(&EarlyTailDuplicateID, &TailDuplicateID);
235 substitutePass(&PostRAMachineLICMID, &MachineLICMID);
Andrew Trick79bf2882012-02-15 03:21:51 +0000236
237 // Temporarily disable experimental passes.
Andrew Trickad1cc1d2012-11-13 08:47:29 +0000238 const TargetSubtargetInfo &ST = TM->getSubtarget<TargetSubtargetInfo>();
239 if (!ST.enableMachineScheduler())
240 disablePass(&MachineSchedulerID);
Andrew Trick74613342012-02-04 02:56:45 +0000241}
242
Bob Wilson6e1b8122012-05-30 00:17:12 +0000243/// Insert InsertedPassID pass after TargetPassID.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000244void TargetPassConfig::insertPass(AnalysisID TargetPassID,
Andrew Trick5ed02832013-04-10 01:06:56 +0000245 IdentifyingPassPtr InsertedPassID) {
Benjamin Kramerfdca2212013-04-11 11:57:01 +0000246 assert(((!InsertedPassID.isInstance() &&
247 TargetPassID != InsertedPassID.getID()) ||
248 (InsertedPassID.isInstance() &&
249 TargetPassID != InsertedPassID.getInstance()->getPassID())) &&
Andrew Trick5ed02832013-04-10 01:06:56 +0000250 "Insert a pass after itself!");
251 std::pair<AnalysisID, IdentifyingPassPtr> P(TargetPassID, InsertedPassID);
Bob Wilson6e1b8122012-05-30 00:17:12 +0000252 Impl->InsertedPasses.push_back(P);
253}
254
Andrew Trick74613342012-02-04 02:56:45 +0000255/// createPassConfig - Create a pass configuration object to be used by
256/// addPassToEmitX methods for generating a pipeline of CodeGen passes.
257///
258/// Targets may override this to extend TargetPassConfig.
Andrew Trick061efcf2012-02-04 02:56:59 +0000259TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
260 return new TargetPassConfig(this, PM);
Andrew Trick74613342012-02-04 02:56:45 +0000261}
262
263TargetPassConfig::TargetPassConfig()
Bill Wendling7c4ce302012-05-01 08:27:43 +0000264 : ImmutablePass(ID), PM(0) {
Andrew Trick74613342012-02-04 02:56:45 +0000265 llvm_unreachable("TargetPassConfig should not be constructed on-the-fly");
266}
267
Andrew Trickffea03f2012-02-08 21:22:39 +0000268// Helper to verify the analysis is really immutable.
269void TargetPassConfig::setOpt(bool &Opt, bool Val) {
270 assert(!Initialized && "PassConfig is immutable");
271 Opt = Val;
272}
273
Bob Wilson3fb99a72012-07-02 19:48:37 +0000274void TargetPassConfig::substitutePass(AnalysisID StandardID,
Andrew Trick5ed02832013-04-10 01:06:56 +0000275 IdentifyingPassPtr TargetID) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000276 Impl->TargetPasses[StandardID] = TargetID;
Andrew Trick5e108ee2012-02-15 03:21:47 +0000277}
Andrew Trick746f24b2012-02-11 07:11:32 +0000278
Andrew Trick5ed02832013-04-10 01:06:56 +0000279IdentifyingPassPtr TargetPassConfig::getPassSubstitution(AnalysisID ID) const {
280 DenseMap<AnalysisID, IdentifyingPassPtr>::const_iterator
Andrew Trick5e108ee2012-02-15 03:21:47 +0000281 I = Impl->TargetPasses.find(ID);
282 if (I == Impl->TargetPasses.end())
283 return ID;
284 return I->second;
285}
286
Bob Wilson30a507a2012-07-02 19:48:45 +0000287/// Add a pass to the PassManager if that pass is supposed to be run. If the
288/// Started/Stopped flags indicate either that the compilation should start at
289/// a later pass or that it should stop after an earlier pass, then do not add
290/// the pass. Finally, compare the current pass against the StartAfter
291/// and StopAfter options and change the Started/Stopped flags accordingly.
Bob Wilson564fbf62012-07-02 19:48:31 +0000292void TargetPassConfig::addPass(Pass *P) {
Bob Wilson6b2bb152012-07-02 19:48:39 +0000293 assert(!Initialized && "PassConfig is immutable");
294
Chandler Carruth6068c482012-07-02 22:56:41 +0000295 // Cache the Pass ID here in case the pass manager finds this pass is
296 // redundant with ones already scheduled / available, and deletes it.
297 // Fundamentally, once we add the pass to the manager, we no longer own it
298 // and shouldn't reference it.
299 AnalysisID PassID = P->getPassID();
300
Bob Wilson30a507a2012-07-02 19:48:45 +0000301 if (Started && !Stopped)
302 PM->add(P);
Chandler Carruth6068c482012-07-02 22:56:41 +0000303 if (StopAfter == PassID)
Bob Wilson30a507a2012-07-02 19:48:45 +0000304 Stopped = true;
Chandler Carruth6068c482012-07-02 22:56:41 +0000305 if (StartAfter == PassID)
Bob Wilson30a507a2012-07-02 19:48:45 +0000306 Started = true;
307 if (Stopped && !Started)
308 report_fatal_error("Cannot stop compilation after pass that is not run");
Bob Wilson564fbf62012-07-02 19:48:31 +0000309}
310
Andrew Trick5e108ee2012-02-15 03:21:47 +0000311/// Add a CodeGen pass at this point in the pipeline after checking for target
312/// and command line overrides.
Andrew Trick5ed02832013-04-10 01:06:56 +0000313///
314/// addPass cannot return a pointer to the pass instance because is internal the
315/// PassManager and the instance we create here may already be freed.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000316AnalysisID TargetPassConfig::addPass(AnalysisID PassID) {
Andrew Trick5ed02832013-04-10 01:06:56 +0000317 IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
318 IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID);
319 if (!FinalPtr.isValid())
320 return 0;
Andrew Trick5e108ee2012-02-15 03:21:47 +0000321
Andrew Trick5ed02832013-04-10 01:06:56 +0000322 Pass *P;
323 if (FinalPtr.isInstance())
324 P = FinalPtr.getInstance();
325 else {
326 P = Pass::createPass(FinalPtr.getID());
327 if (!P)
328 llvm_unreachable("Pass ID not registered");
329 }
330 AnalysisID FinalID = P->getPassID();
331 addPass(P); // Ends the lifetime of P.
332
Bob Wilson6e1b8122012-05-30 00:17:12 +0000333 // Add the passes after the pass P if there is any.
Andrew Trick5ed02832013-04-10 01:06:56 +0000334 for (SmallVector<std::pair<AnalysisID, IdentifyingPassPtr>, 4>::iterator
Bob Wilson6e1b8122012-05-30 00:17:12 +0000335 I = Impl->InsertedPasses.begin(), E = Impl->InsertedPasses.end();
336 I != E; ++I) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000337 if ((*I).first == PassID) {
Andrew Trick5ed02832013-04-10 01:06:56 +0000338 assert((*I).second.isValid() && "Illegal Pass ID!");
339 Pass *NP;
340 if ((*I).second.isInstance())
341 NP = (*I).second.getInstance();
342 else {
343 NP = Pass::createPass((*I).second.getID());
344 assert(NP && "Pass ID not registered");
345 }
Bob Wilson564fbf62012-07-02 19:48:31 +0000346 addPass(NP);
Bob Wilson6e1b8122012-05-30 00:17:12 +0000347 }
348 }
Andrew Trick5e108ee2012-02-15 03:21:47 +0000349 return FinalID;
Andrew Trick061efcf2012-02-04 02:56:59 +0000350}
Andrew Trickd5422652012-02-04 02:56:48 +0000351
Bob Wilson564fbf62012-07-02 19:48:31 +0000352void TargetPassConfig::printAndVerify(const char *Banner) {
Andrew Trickd5422652012-02-04 02:56:48 +0000353 if (TM->shouldPrintMachineCode())
Bob Wilson564fbf62012-07-02 19:48:31 +0000354 addPass(createMachineFunctionPrinterPass(dbgs(), Banner));
Andrew Trickd5422652012-02-04 02:56:48 +0000355
356 if (VerifyMachineCode)
Bob Wilson564fbf62012-07-02 19:48:31 +0000357 addPass(createMachineVerifierPass(Banner));
Andrew Trickd5422652012-02-04 02:56:48 +0000358}
359
Andrew Trick061efcf2012-02-04 02:56:59 +0000360/// Add common target configurable passes that perform LLVM IR to IR transforms
361/// following machine independent optimization.
362void TargetPassConfig::addIRPasses() {
Andrew Trickd5422652012-02-04 02:56:48 +0000363 // Basic AliasAnalysis support.
364 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
365 // BasicAliasAnalysis wins if they disagree. This is intended to help
366 // support "obvious" type-punning idioms.
Bob Wilson564fbf62012-07-02 19:48:31 +0000367 addPass(createTypeBasedAliasAnalysisPass());
368 addPass(createBasicAliasAnalysisPass());
Andrew Trickd5422652012-02-04 02:56:48 +0000369
370 // Before running any passes, run the verifier to determine if the input
371 // coming from the front-end and/or optimizer is valid.
372 if (!DisableVerify)
Bob Wilson564fbf62012-07-02 19:48:31 +0000373 addPass(createVerifierPass());
Andrew Trickd5422652012-02-04 02:56:48 +0000374
375 // Run loop strength reduction before anything else.
376 if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
Chandler Carruthe4ba75f2013-01-07 14:41:08 +0000377 addPass(createLoopStrengthReducePass());
Andrew Trickd5422652012-02-04 02:56:48 +0000378 if (PrintLSR)
Bob Wilson564fbf62012-07-02 19:48:31 +0000379 addPass(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
Andrew Trickd5422652012-02-04 02:56:48 +0000380 }
381
Bob Wilson564fbf62012-07-02 19:48:31 +0000382 addPass(createGCLoweringPass());
Andrew Trickd5422652012-02-04 02:56:48 +0000383
384 // Make sure that no unreachable blocks are instruction selected.
Bob Wilson564fbf62012-07-02 19:48:31 +0000385 addPass(createUnreachableBlockEliminationPass());
386}
387
388/// Turn exception handling constructs into something the code generators can
389/// handle.
390void TargetPassConfig::addPassesToHandleExceptions() {
391 switch (TM->getMCAsmInfo()->getExceptionHandlingType()) {
392 case ExceptionHandling::SjLj:
393 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
394 // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
395 // catch info can get misplaced when a selector ends up more than one block
396 // removed from the parent invoke(s). This could happen when a landing
397 // pad is shared by multiple invokes and is also a target of a normal
398 // edge from elsewhere.
Bill Wendlingea442812013-06-19 20:51:24 +0000399 addPass(createSjLjEHPreparePass(TM));
Bob Wilson564fbf62012-07-02 19:48:31 +0000400 // FALLTHROUGH
401 case ExceptionHandling::DwarfCFI:
402 case ExceptionHandling::ARM:
403 case ExceptionHandling::Win64:
Bill Wendlingea442812013-06-19 20:51:24 +0000404 addPass(createDwarfEHPass(TM));
Bob Wilson564fbf62012-07-02 19:48:31 +0000405 break;
406 case ExceptionHandling::None:
Nadav Rotema04a4a72012-10-19 21:28:43 +0000407 addPass(createLowerInvokePass(TM->getTargetLowering()));
Bob Wilson564fbf62012-07-02 19:48:31 +0000408
409 // The lower invoke pass may create unreachable code. Remove it.
410 addPass(createUnreachableBlockEliminationPass());
411 break;
412 }
Andrew Trick061efcf2012-02-04 02:56:59 +0000413}
Andrew Trickd5422652012-02-04 02:56:48 +0000414
Bill Wendling08510b12012-11-30 22:08:55 +0000415/// Add pass to prepare the LLVM IR for code generation. This should be done
416/// before exception handling preparation passes.
417void TargetPassConfig::addCodeGenPrepare() {
418 if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
419 addPass(createCodeGenPreparePass(getTargetLowering()));
420}
421
Andrew Trick061efcf2012-02-04 02:56:59 +0000422/// Add common passes that perform LLVM IR to IR transforms in preparation for
423/// instruction selection.
424void TargetPassConfig::addISelPrepare() {
Bill Wendlingea442812013-06-19 20:51:24 +0000425 addPass(createStackProtectorPass(TM));
Andrew Trickd5422652012-02-04 02:56:48 +0000426
427 addPreISel();
428
429 if (PrintISelInput)
Bob Wilson564fbf62012-07-02 19:48:31 +0000430 addPass(createPrintFunctionPass("\n\n"
Bill Wendling7c4ce302012-05-01 08:27:43 +0000431 "*** Final LLVM Code input to ISel ***\n",
432 &dbgs()));
Andrew Trickd5422652012-02-04 02:56:48 +0000433
434 // All passes which modify the LLVM IR are now complete; run the verifier
435 // to ensure that the IR is valid.
436 if (!DisableVerify)
Bob Wilson564fbf62012-07-02 19:48:31 +0000437 addPass(createVerifierPass());
Andrew Trick061efcf2012-02-04 02:56:59 +0000438}
Andrew Trickd5422652012-02-04 02:56:48 +0000439
Andrew Trickf7b96312012-02-09 00:40:55 +0000440/// Add the complete set of target-independent postISel code generator passes.
441///
442/// This can be read as the standard order of major LLVM CodeGen stages. Stages
443/// with nontrivial configuration or multiple passes are broken out below in
444/// add%Stage routines.
445///
446/// Any TargetPassConfig::addXX routine may be overriden by the Target. The
447/// addPre/Post methods with empty header implementations allow injecting
448/// target-specific fixups just before or after major stages. Additionally,
449/// targets have the flexibility to change pass order within a stage by
450/// overriding default implementation of add%Stage routines below. Each
451/// technique has maintainability tradeoffs because alternate pass orders are
452/// not well supported. addPre/Post works better if the target pass is easily
453/// tied to a common pass. But if it has subtle dependencies on multiple passes,
Andrew Trick06efdd22012-02-10 07:08:25 +0000454/// the target should override the stage instead.
Andrew Trickf7b96312012-02-09 00:40:55 +0000455///
456/// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
457/// before/after any target-independent pass. But it's currently overkill.
Andrew Trick061efcf2012-02-04 02:56:59 +0000458void TargetPassConfig::addMachinePasses() {
Bob Wilson6e1b8122012-05-30 00:17:12 +0000459 // Insert a machine instr printer pass after the specified pass.
460 // If -print-machineinstrs specified, print machineinstrs after all passes.
461 if (StringRef(PrintMachineInstrs.getValue()).equals(""))
462 TM->Options.PrintMachineCode = true;
463 else if (!StringRef(PrintMachineInstrs.getValue())
464 .equals("option-unspecified")) {
465 const PassRegistry *PR = PassRegistry::getPassRegistry();
466 const PassInfo *TPI = PR->getPassInfo(PrintMachineInstrs.getValue());
467 const PassInfo *IPI = PR->getPassInfo(StringRef("print-machineinstrs"));
468 assert (TPI && IPI && "Pass ID not registered!");
Roman Divacky59324292012-09-05 22:26:57 +0000469 const char *TID = (const char *)(TPI->getTypeInfo());
470 const char *IID = (const char *)(IPI->getTypeInfo());
Bob Wilson3fb99a72012-07-02 19:48:37 +0000471 insertPass(TID, IID);
Bob Wilson6e1b8122012-05-30 00:17:12 +0000472 }
473
Jakob Stoklund Olesenf86c00f2012-07-04 19:28:27 +0000474 // Print the instruction selected machine code...
475 printAndVerify("After Instruction Selection");
476
Andrew Trickd5422652012-02-04 02:56:48 +0000477 // Expand pseudo-instructions emitted by ISel.
Jakob Stoklund Olesen228e3f52012-08-20 20:52:08 +0000478 if (addPass(&ExpandISelPseudosID))
479 printAndVerify("After ExpandISelPseudos");
Andrew Trickd5422652012-02-04 02:56:48 +0000480
Andrew Trickf7b96312012-02-09 00:40:55 +0000481 // Add passes that optimize machine instructions in SSA form.
Andrew Trickd5422652012-02-04 02:56:48 +0000482 if (getOptLevel() != CodeGenOpt::None) {
Andrew Trickf7b96312012-02-09 00:40:55 +0000483 addMachineSSAOptimization();
Craig Topper8f54a532012-11-19 00:11:50 +0000484 } else {
Andrew Trickf7b96312012-02-09 00:40:55 +0000485 // If the target requests it, assign local variables to stack slots relative
486 // to one another and simplify frame index references where possible.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000487 addPass(&LocalStackSlotAllocationID);
Andrew Trickd5422652012-02-04 02:56:48 +0000488 }
489
490 // Run pre-ra passes.
491 if (addPreRegAlloc())
492 printAndVerify("After PreRegAlloc passes");
493
Andrew Trickf7b96312012-02-09 00:40:55 +0000494 // Run register allocation and passes that are tightly coupled with it,
495 // including phi elimination and scheduling.
Andrew Trick8dd26252012-02-10 04:10:36 +0000496 if (getOptimizeRegAlloc())
497 addOptimizedRegAlloc(createRegAllocPass(true));
498 else
499 addFastRegAlloc(createRegAllocPass(false));
Andrew Trickd5422652012-02-04 02:56:48 +0000500
501 // Run post-ra passes.
502 if (addPostRegAlloc())
503 printAndVerify("After PostRegAlloc passes");
504
505 // Insert prolog/epilog code. Eliminate abstract frame index references...
Bob Wilson3fb99a72012-07-02 19:48:37 +0000506 addPass(&PrologEpilogCodeInserterID);
Andrew Trickd5422652012-02-04 02:56:48 +0000507 printAndVerify("After PrologEpilogCodeInserter");
508
Andrew Trickf7b96312012-02-09 00:40:55 +0000509 /// Add passes that optimize machine instructions after register allocation.
510 if (getOptLevel() != CodeGenOpt::None)
511 addMachineLateOptimization();
Andrew Trickd5422652012-02-04 02:56:48 +0000512
513 // Expand pseudo instructions before second scheduling pass.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000514 addPass(&ExpandPostRAPseudosID);
Jakob Stoklund Olesen2ef5bf62012-03-28 20:49:30 +0000515 printAndVerify("After ExpandPostRAPseudos");
Andrew Trickd5422652012-02-04 02:56:48 +0000516
517 // Run pre-sched2 passes.
518 if (addPreSched2())
Jakob Stoklund Olesen78811662012-03-28 23:31:15 +0000519 printAndVerify("After PreSched2 passes");
Andrew Trickd5422652012-02-04 02:56:48 +0000520
521 // Second pass scheduler.
Andrew Trick79bf2882012-02-15 03:21:51 +0000522 if (getOptLevel() != CodeGenOpt::None) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000523 addPass(&PostRASchedulerID);
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000524 printAndVerify("After PostRAScheduler");
Andrew Trickd5422652012-02-04 02:56:48 +0000525 }
526
Andrew Trickf7b96312012-02-09 00:40:55 +0000527 // GC
Evan Chengab37b2c2012-12-21 02:57:04 +0000528 if (addGCPasses()) {
529 if (PrintGCInfo)
530 addPass(createGCInfoPrinter(dbgs()));
531 }
Andrew Trickd5422652012-02-04 02:56:48 +0000532
Andrew Trickf7b96312012-02-09 00:40:55 +0000533 // Basic block placement.
Andrew Trick79bf2882012-02-15 03:21:51 +0000534 if (getOptLevel() != CodeGenOpt::None)
Andrew Trickf7b96312012-02-09 00:40:55 +0000535 addBlockPlacement();
Andrew Trickd5422652012-02-04 02:56:48 +0000536
537 if (addPreEmitPass())
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000538 printAndVerify("After PreEmit passes");
Andrew Trickd5422652012-02-04 02:56:48 +0000539}
540
Andrew Trickf7b96312012-02-09 00:40:55 +0000541/// Add passes that optimize machine instructions in SSA form.
542void TargetPassConfig::addMachineSSAOptimization() {
543 // Pre-ra tail duplication.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000544 if (addPass(&EarlyTailDuplicateID))
Andrew Trickf7b96312012-02-09 00:40:55 +0000545 printAndVerify("After Pre-RegAlloc TailDuplicate");
Andrew Trickf7b96312012-02-09 00:40:55 +0000546
547 // Optimize PHIs before DCE: removing dead PHI cycles may make more
548 // instructions dead.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000549 addPass(&OptimizePHIsID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000550
Nadav Rotemc05d3062012-09-06 09:17:37 +0000551 // This pass merges large allocas. StackSlotColoring is a different pass
552 // which merges spill slots.
553 addPass(&StackColoringID);
554
Andrew Trickf7b96312012-02-09 00:40:55 +0000555 // If the target requests it, assign local variables to stack slots relative
556 // to one another and simplify frame index references where possible.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000557 addPass(&LocalStackSlotAllocationID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000558
559 // With optimization, dead code should already be eliminated. However
560 // there is one known exception: lowered code for arguments that are only
561 // used by tail calls, where the tail calls reuse the incoming stack
562 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
Bob Wilson3fb99a72012-07-02 19:48:37 +0000563 addPass(&DeadMachineInstructionElimID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000564 printAndVerify("After codegen DCE pass");
565
Jakob Stoklund Olesen02c63252013-01-17 00:58:38 +0000566 // Allow targets to insert passes that improve instruction level parallelism,
567 // like if-conversion. Such passes will typically need dominator trees and
568 // loop info, just like LICM and CSE below.
569 if (addILPOpts())
570 printAndVerify("After ILP optimizations");
571
Bob Wilson3fb99a72012-07-02 19:48:37 +0000572 addPass(&MachineLICMID);
573 addPass(&MachineCSEID);
574 addPass(&MachineSinkingID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000575 printAndVerify("After Machine LICM, CSE and Sinking passes");
576
Bob Wilson3fb99a72012-07-02 19:48:37 +0000577 addPass(&PeepholeOptimizerID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000578 printAndVerify("After codegen peephole optimization pass");
579}
580
Andrew Trick74613342012-02-04 02:56:45 +0000581//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000582/// Register Allocation Pass Configuration
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000583//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000584
Andrew Trick8dd26252012-02-10 04:10:36 +0000585bool TargetPassConfig::getOptimizeRegAlloc() const {
586 switch (OptimizeRegAlloc) {
587 case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
588 case cl::BOU_TRUE: return true;
589 case cl::BOU_FALSE: return false;
590 }
591 llvm_unreachable("Invalid optimize-regalloc state");
592}
593
Andrew Trickf7b96312012-02-09 00:40:55 +0000594/// RegisterRegAlloc's global Registry tracks allocator registration.
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000595MachinePassRegistry RegisterRegAlloc::Registry;
596
Andrew Trickf7b96312012-02-09 00:40:55 +0000597/// A dummy default pass factory indicates whether the register allocator is
598/// overridden on the command line.
Andrew Trick8dd26252012-02-10 04:10:36 +0000599static FunctionPass *useDefaultRegisterAllocator() { return 0; }
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000600static RegisterRegAlloc
601defaultRegAlloc("default",
602 "pick register allocator based on -O option",
Andrew Trick8dd26252012-02-10 04:10:36 +0000603 useDefaultRegisterAllocator);
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000604
Andrew Trickf7b96312012-02-09 00:40:55 +0000605/// -regalloc=... command line option.
Dan Gohman844731a2008-05-13 00:00:25 +0000606static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
607 RegisterPassParser<RegisterRegAlloc> >
608RegAlloc("regalloc",
Andrew Trick8dd26252012-02-10 04:10:36 +0000609 cl::init(&useDefaultRegisterAllocator),
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000610 cl::desc("Register allocator to use"));
Alkis Evlogimenos7237ece2003-10-02 16:57:49 +0000611
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000612
Andrew Trick8dd26252012-02-10 04:10:36 +0000613/// Instantiate the default register allocator pass for this target for either
614/// the optimized or unoptimized allocation path. This will be added to the pass
615/// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
616/// in the optimized case.
617///
618/// A target that uses the standard regalloc pass order for fast or optimized
619/// allocation may still override this for per-target regalloc
620/// selection. But -regalloc=... always takes precedence.
621FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
622 if (Optimized)
623 return createGreedyRegisterAllocator();
624 else
625 return createFastRegisterAllocator();
626}
627
628/// Find and instantiate the register allocation pass requested by this target
629/// at the current optimization level. Different register allocators are
630/// defined as separate passes because they may require different analysis.
631///
632/// This helper ensures that the regalloc= option is always available,
633/// even for targets that override the default allocator.
634///
635/// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
636/// this can be folded into addPass.
637FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
Jim Laskey9ff542f2006-08-01 18:29:48 +0000638 RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000639
Andrew Trick8dd26252012-02-10 04:10:36 +0000640 // Initialize the global default.
Jim Laskey13ec7022006-08-01 14:21:23 +0000641 if (!Ctor) {
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000642 Ctor = RegAlloc;
643 RegisterRegAlloc::setDefault(RegAlloc);
Jim Laskey13ec7022006-08-01 14:21:23 +0000644 }
Andrew Trick8dd26252012-02-10 04:10:36 +0000645 if (Ctor != useDefaultRegisterAllocator)
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000646 return Ctor();
647
Andrew Trick8dd26252012-02-10 04:10:36 +0000648 // With no -regalloc= override, ask the target for a regalloc pass.
649 return createTargetRegisterAllocator(Optimized);
650}
651
652/// Add the minimum set of target-independent passes that are required for
653/// register allocation. No coalescing or scheduling.
654void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000655 addPass(&PHIEliminationID);
656 addPass(&TwoAddressInstructionPassID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000657
Bob Wilson564fbf62012-07-02 19:48:31 +0000658 addPass(RegAllocPass);
Andrew Trick8dd26252012-02-10 04:10:36 +0000659 printAndVerify("After Register Allocation");
Jim Laskey33a0a6d2006-07-27 20:05:00 +0000660}
Andrew Trickf7b96312012-02-09 00:40:55 +0000661
662/// Add standard target-independent passes that are tightly coupled with
Andrew Trick8dd26252012-02-10 04:10:36 +0000663/// optimized register allocation, including coalescing, machine instruction
664/// scheduling, and register allocation itself.
665void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000666 addPass(&ProcessImplicitDefsID);
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +0000667
Andrew Trick8dd26252012-02-10 04:10:36 +0000668 // LiveVariables currently requires pure SSA form.
669 //
670 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
671 // LiveVariables can be removed completely, and LiveIntervals can be directly
672 // computed. (We still either need to regenerate kill flags after regalloc, or
673 // preferably fix the scavenger to not depend on them).
Bob Wilson3fb99a72012-07-02 19:48:37 +0000674 addPass(&LiveVariablesID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000675
676 // Add passes that move from transformed SSA into conventional SSA. This is a
677 // "copy coalescing" problem.
678 //
679 if (!EnableStrongPHIElim) {
680 // Edge splitting is smarter with machine loop info.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000681 addPass(&MachineLoopInfoID);
682 addPass(&PHIEliminationID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000683 }
Jakob Stoklund Olesendcc44362012-08-03 22:12:54 +0000684
685 // Eventually, we want to run LiveIntervals before PHI elimination.
686 if (EarlyLiveIntervals)
687 addPass(&LiveIntervalsID);
688
Bob Wilson3fb99a72012-07-02 19:48:37 +0000689 addPass(&TwoAddressInstructionPassID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000690
Andrew Trick8dd26252012-02-10 04:10:36 +0000691 if (EnableStrongPHIElim)
Bob Wilson3fb99a72012-07-02 19:48:37 +0000692 addPass(&StrongPHIEliminationID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000693
Bob Wilson3fb99a72012-07-02 19:48:37 +0000694 addPass(&RegisterCoalescerID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000695
696 // PreRA instruction scheduling.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000697 if (addPass(&MachineSchedulerID))
Andrew Trick17d35e52012-03-14 04:00:41 +0000698 printAndVerify("After Machine Scheduling");
Andrew Trick8dd26252012-02-10 04:10:36 +0000699
700 // Add the selected register allocation pass.
Bob Wilson564fbf62012-07-02 19:48:31 +0000701 addPass(RegAllocPass);
Jakob Stoklund Olesen34f5a2b2012-06-26 17:09:29 +0000702 printAndVerify("After Register Allocation, before rewriter");
703
704 // Allow targets to change the register assignments before rewriting.
705 if (addPreRewrite())
706 printAndVerify("After pre-rewrite passes");
Andrew Trickf7b96312012-02-09 00:40:55 +0000707
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000708 // Finally rewrite virtual registers.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000709 addPass(&VirtRegRewriterID);
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000710 printAndVerify("After Virtual Register Rewriter");
711
Andrew Trickf7b96312012-02-09 00:40:55 +0000712 // Perform stack slot coloring and post-ra machine LICM.
Andrew Trick8dd26252012-02-10 04:10:36 +0000713 //
714 // FIXME: Re-enable coloring with register when it's capable of adding
715 // kill markers.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000716 addPass(&StackSlotColoringID);
Andrew Trick900d7b72012-02-15 07:57:03 +0000717
718 // Run post-ra machine LICM to hoist reloads / remats.
719 //
720 // FIXME: can this move into MachineLateOptimization?
Bob Wilson3fb99a72012-07-02 19:48:37 +0000721 addPass(&PostRAMachineLICMID);
Andrew Trick900d7b72012-02-15 07:57:03 +0000722
723 printAndVerify("After StackSlotColoring and postra Machine LICM");
Andrew Trickf7b96312012-02-09 00:40:55 +0000724}
725
726//===---------------------------------------------------------------------===//
727/// Post RegAlloc Pass Configuration
728//===---------------------------------------------------------------------===//
729
730/// Add passes that optimize machine instructions after register allocation.
731void TargetPassConfig::addMachineLateOptimization() {
732 // Branch folding must be run after regalloc and prolog/epilog insertion.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000733 if (addPass(&BranchFolderPassID))
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000734 printAndVerify("After BranchFolding");
Andrew Trickf7b96312012-02-09 00:40:55 +0000735
736 // Tail duplication.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000737 if (addPass(&TailDuplicateID))
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000738 printAndVerify("After TailDuplicate");
Andrew Trickf7b96312012-02-09 00:40:55 +0000739
740 // Copy propagation.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000741 if (addPass(&MachineCopyPropagationID))
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000742 printAndVerify("After copy propagation pass");
Andrew Trickf7b96312012-02-09 00:40:55 +0000743}
744
Evan Chengab37b2c2012-12-21 02:57:04 +0000745/// Add standard GC passes.
746bool TargetPassConfig::addGCPasses() {
747 addPass(&GCMachineCodeAnalysisID);
748 return true;
749}
750
Andrew Trickf7b96312012-02-09 00:40:55 +0000751/// Add standard basic block placement passes.
752void TargetPassConfig::addBlockPlacement() {
Benjamin Kramer74a45332013-03-29 17:14:24 +0000753 if (addPass(&MachineBlockPlacementID)) {
Andrew Trick79bf2882012-02-15 03:21:51 +0000754 // Run a separate pass to collect block placement statistics.
755 if (EnableBlockPlacementStats)
Bob Wilson3fb99a72012-07-02 19:48:37 +0000756 addPass(&MachineBlockPlacementStatsID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000757
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000758 printAndVerify("After machine block placement.");
Andrew Trickf7b96312012-02-09 00:40:55 +0000759 }
760}