blob: 249b2d0f6bb95300909d7a0f450d8617fdb4445a [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
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>
Stephen Hines36b56882014-04-23 16:57:46 -070059EnableMachineSched("enable-misched",
Andrew Trick8dd26252012-02-10 04:10:36 +000060 cl::desc("Enable the machine instruction scheduling pass."));
Andrew Trickd5422652012-02-04 02:56:48 +000061static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
62 cl::Hidden,
63 cl::desc("Disable Machine LICM"));
64static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
65 cl::desc("Disable Machine Sinking"));
66static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
67 cl::desc("Disable Loop Strength Reduction Pass"));
Stephen Hines36b56882014-04-23 16:57:46 -070068static cl::opt<bool> DisableConstantHoisting("disable-constant-hoisting",
69 cl::Hidden, cl::desc("Disable ConstantHoisting"));
Andrew Trickd5422652012-02-04 02:56:48 +000070static 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"),
Stephen Hinesdce4a402014-05-29 02:49:00 -070082 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=nullptr));
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
Stephen Hines36b56882014-04-23 16:57:46 -070088// Temporary option to allow experimenting with MachineScheduler as a post-RA
89// scheduler. Targets can "properly" enable this with
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070090// substitutePass(&PostRASchedulerID, &PostMachineSchedulerID); Ideally it
91// wouldn't be part of the standard pass pipeline, and the target would just add
92// a PostRA scheduling pass wherever it wants.
Stephen Hines36b56882014-04-23 16:57:46 -070093static cl::opt<bool> MISchedPostRA("misched-postra", cl::Hidden,
94 cl::desc("Run MachineScheduler post regalloc (independent of preRA sched)"));
95
Cameron Zwarichd7c7a682013-02-10 06:42:34 +000096// Experimental option to run live interval analysis early.
Jakob Stoklund Olesendcc44362012-08-03 22:12:54 +000097static cl::opt<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden,
98 cl::desc("Run live interval analysis earlier in the pipeline"));
99
Andrew Trick79bf2882012-02-15 03:21:51 +0000100/// Allow standard passes to be disabled by command line options. This supports
101/// simple binary flags that either suppress the pass or do nothing.
102/// i.e. -disable-mypass=false has no effect.
103/// These should be converted to boolOrDefault in order to use applyOverride.
Andrew Trick5ed02832013-04-10 01:06:56 +0000104static IdentifyingPassPtr applyDisable(IdentifyingPassPtr PassID,
105 bool Override) {
Andrew Trick79bf2882012-02-15 03:21:51 +0000106 if (Override)
Andrew Trick5ed02832013-04-10 01:06:56 +0000107 return IdentifyingPassPtr();
Bob Wilson3fb99a72012-07-02 19:48:37 +0000108 return PassID;
Andrew Trick79bf2882012-02-15 03:21:51 +0000109}
110
111/// Allow Pass selection to be overriden by command line options. This supports
112/// flags with ternary conditions. TargetID is passed through by default. The
113/// pass is suppressed when the option is false. When the option is true, the
114/// StandardID is selected if the target provides no default.
Andrew Trick5ed02832013-04-10 01:06:56 +0000115static IdentifyingPassPtr applyOverride(IdentifyingPassPtr TargetID,
116 cl::boolOrDefault Override,
117 AnalysisID StandardID) {
Andrew Trick746f24b2012-02-11 07:11:32 +0000118 switch (Override) {
119 case cl::BOU_UNSET:
Andrew Trick79bf2882012-02-15 03:21:51 +0000120 return TargetID;
Andrew Trick746f24b2012-02-11 07:11:32 +0000121 case cl::BOU_TRUE:
Andrew Trick5ed02832013-04-10 01:06:56 +0000122 if (TargetID.isValid())
Andrew Trick79bf2882012-02-15 03:21:51 +0000123 return TargetID;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700124 if (StandardID == nullptr)
Andrew Trick746f24b2012-02-11 07:11:32 +0000125 report_fatal_error("Target cannot enable pass");
Andrew Trick79bf2882012-02-15 03:21:51 +0000126 return StandardID;
Andrew Trick746f24b2012-02-11 07:11:32 +0000127 case cl::BOU_FALSE:
Andrew Trick5ed02832013-04-10 01:06:56 +0000128 return IdentifyingPassPtr();
Andrew Trick746f24b2012-02-11 07:11:32 +0000129 }
130 llvm_unreachable("Invalid command line option state");
131}
132
Andrew Trick79bf2882012-02-15 03:21:51 +0000133/// Allow standard passes to be disabled by the command line, regardless of who
134/// is adding the pass.
135///
136/// StandardID is the pass identified in the standard pass pipeline and provided
137/// to addPass(). It may be a target-specific ID in the case that the target
138/// directly adds its own pass, but in that case we harmlessly fall through.
139///
140/// TargetID is the pass that the target has configured to override StandardID.
141///
142/// StandardID may be a pseudo ID. In that case TargetID is the name of the real
143/// pass to run. This allows multiple options to control a single pass depending
144/// on where in the pipeline that pass is added.
Andrew Trick5ed02832013-04-10 01:06:56 +0000145static IdentifyingPassPtr overridePass(AnalysisID StandardID,
146 IdentifyingPassPtr TargetID) {
Andrew Trick79bf2882012-02-15 03:21:51 +0000147 if (StandardID == &PostRASchedulerID)
148 return applyDisable(TargetID, DisablePostRA);
149
150 if (StandardID == &BranchFolderPassID)
151 return applyDisable(TargetID, DisableBranchFold);
152
153 if (StandardID == &TailDuplicateID)
154 return applyDisable(TargetID, DisableTailDuplicate);
155
156 if (StandardID == &TargetPassConfig::EarlyTailDuplicateID)
157 return applyDisable(TargetID, DisableEarlyTailDup);
158
159 if (StandardID == &MachineBlockPlacementID)
Benjamin Kramer74a45332013-03-29 17:14:24 +0000160 return applyDisable(TargetID, DisableBlockPlacement);
Andrew Trick79bf2882012-02-15 03:21:51 +0000161
162 if (StandardID == &StackSlotColoringID)
163 return applyDisable(TargetID, DisableSSC);
164
165 if (StandardID == &DeadMachineInstructionElimID)
166 return applyDisable(TargetID, DisableMachineDCE);
167
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000168 if (StandardID == &EarlyIfConverterID)
Jakob Stoklund Olesen0d141f82012-10-03 00:51:32 +0000169 return applyDisable(TargetID, DisableEarlyIfConversion);
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000170
Andrew Trick79bf2882012-02-15 03:21:51 +0000171 if (StandardID == &MachineLICMID)
172 return applyDisable(TargetID, DisableMachineLICM);
173
174 if (StandardID == &MachineCSEID)
175 return applyDisable(TargetID, DisableMachineCSE);
176
177 if (StandardID == &MachineSchedulerID)
178 return applyOverride(TargetID, EnableMachineSched, StandardID);
179
180 if (StandardID == &TargetPassConfig::PostRAMachineLICMID)
181 return applyDisable(TargetID, DisablePostRAMachineLICM);
182
183 if (StandardID == &MachineSinkingID)
184 return applyDisable(TargetID, DisableMachineSink);
185
186 if (StandardID == &MachineCopyPropagationID)
187 return applyDisable(TargetID, DisableCopyProp);
188
189 return TargetID;
190}
191
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000192//===---------------------------------------------------------------------===//
Andrew Trick74613342012-02-04 02:56:45 +0000193/// TargetPassConfig
194//===---------------------------------------------------------------------===//
195
196INITIALIZE_PASS(TargetPassConfig, "targetpassconfig",
197 "Target Pass Configuration", false, false)
198char TargetPassConfig::ID = 0;
199
Andrew Trick79bf2882012-02-15 03:21:51 +0000200// Pseudo Pass IDs.
201char TargetPassConfig::EarlyTailDuplicateID = 0;
202char TargetPassConfig::PostRAMachineLICMID = 0;
203
Andrew Trick5e108ee2012-02-15 03:21:47 +0000204namespace llvm {
205class PassConfigImpl {
206public:
207 // List of passes explicitly substituted by this target. Normally this is
208 // empty, but it is a convenient way to suppress or replace specific passes
209 // that are part of a standard pass pipeline without overridding the entire
210 // pipeline. This mechanism allows target options to inherit a standard pass's
211 // user interface. For example, a target may disable a standard pass by
Bob Wilson3fb99a72012-07-02 19:48:37 +0000212 // default by substituting a pass ID of zero, and the user may still enable
213 // that standard pass with an explicit command line option.
Andrew Trick5ed02832013-04-10 01:06:56 +0000214 DenseMap<AnalysisID,IdentifyingPassPtr> TargetPasses;
Bob Wilson6e1b8122012-05-30 00:17:12 +0000215
216 /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass
217 /// is inserted after each instance of the first one.
Andrew Trick5ed02832013-04-10 01:06:56 +0000218 SmallVector<std::pair<AnalysisID, IdentifyingPassPtr>, 4> InsertedPasses;
Andrew Trick5e108ee2012-02-15 03:21:47 +0000219};
220} // namespace llvm
221
Andrew Trick74613342012-02-04 02:56:45 +0000222// Out of line virtual method.
Andrew Trick5e108ee2012-02-15 03:21:47 +0000223TargetPassConfig::~TargetPassConfig() {
224 delete Impl;
225}
Andrew Trick74613342012-02-04 02:56:45 +0000226
Andrew Trick61f1e3d2012-02-08 21:22:48 +0000227// Out of line constructor provides default values for pass options and
228// registers all common codegen passes.
Andrew Trick061efcf2012-02-04 02:56:59 +0000229TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700230 : ImmutablePass(ID), PM(&pm), StartAfter(nullptr), StopAfter(nullptr),
231 Started(true), Stopped(false), TM(tm), Impl(nullptr), Initialized(false),
Andrew Trickffea03f2012-02-08 21:22:39 +0000232 DisableVerify(false),
233 EnableTailMerge(true) {
234
Andrew Trick5e108ee2012-02-15 03:21:47 +0000235 Impl = new PassConfigImpl();
236
Andrew Trick74613342012-02-04 02:56:45 +0000237 // Register all target independent codegen passes to activate their PassIDs,
238 // including this pass itself.
239 initializeCodeGen(*PassRegistry::getPassRegistry());
Andrew Trick79bf2882012-02-15 03:21:51 +0000240
241 // Substitute Pseudo Pass IDs for real ones.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000242 substitutePass(&EarlyTailDuplicateID, &TailDuplicateID);
243 substitutePass(&PostRAMachineLICMID, &MachineLICMID);
Andrew Trick79bf2882012-02-15 03:21:51 +0000244
245 // Temporarily disable experimental passes.
Andrew Trickad1cc1d2012-11-13 08:47:29 +0000246 const TargetSubtargetInfo &ST = TM->getSubtarget<TargetSubtargetInfo>();
Andrew Trickb6ac11c2013-09-26 05:53:35 +0000247 if (!ST.useMachineScheduler())
Andrew Trickad1cc1d2012-11-13 08:47:29 +0000248 disablePass(&MachineSchedulerID);
Andrew Trick74613342012-02-04 02:56:45 +0000249}
250
Bob Wilson6e1b8122012-05-30 00:17:12 +0000251/// Insert InsertedPassID pass after TargetPassID.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000252void TargetPassConfig::insertPass(AnalysisID TargetPassID,
Andrew Trick5ed02832013-04-10 01:06:56 +0000253 IdentifyingPassPtr InsertedPassID) {
Benjamin Kramerfdca2212013-04-11 11:57:01 +0000254 assert(((!InsertedPassID.isInstance() &&
255 TargetPassID != InsertedPassID.getID()) ||
256 (InsertedPassID.isInstance() &&
257 TargetPassID != InsertedPassID.getInstance()->getPassID())) &&
Andrew Trick5ed02832013-04-10 01:06:56 +0000258 "Insert a pass after itself!");
259 std::pair<AnalysisID, IdentifyingPassPtr> P(TargetPassID, InsertedPassID);
Bob Wilson6e1b8122012-05-30 00:17:12 +0000260 Impl->InsertedPasses.push_back(P);
261}
262
Andrew Trick74613342012-02-04 02:56:45 +0000263/// createPassConfig - Create a pass configuration object to be used by
264/// addPassToEmitX methods for generating a pipeline of CodeGen passes.
265///
266/// Targets may override this to extend TargetPassConfig.
Andrew Trick061efcf2012-02-04 02:56:59 +0000267TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
268 return new TargetPassConfig(this, PM);
Andrew Trick74613342012-02-04 02:56:45 +0000269}
270
271TargetPassConfig::TargetPassConfig()
Stephen Hinesdce4a402014-05-29 02:49:00 -0700272 : ImmutablePass(ID), PM(nullptr) {
Andrew Trick74613342012-02-04 02:56:45 +0000273 llvm_unreachable("TargetPassConfig should not be constructed on-the-fly");
274}
275
Andrew Trickffea03f2012-02-08 21:22:39 +0000276// Helper to verify the analysis is really immutable.
277void TargetPassConfig::setOpt(bool &Opt, bool Val) {
278 assert(!Initialized && "PassConfig is immutable");
279 Opt = Val;
280}
281
Bob Wilson3fb99a72012-07-02 19:48:37 +0000282void TargetPassConfig::substitutePass(AnalysisID StandardID,
Andrew Trick5ed02832013-04-10 01:06:56 +0000283 IdentifyingPassPtr TargetID) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000284 Impl->TargetPasses[StandardID] = TargetID;
Andrew Trick5e108ee2012-02-15 03:21:47 +0000285}
Andrew Trick746f24b2012-02-11 07:11:32 +0000286
Andrew Trick5ed02832013-04-10 01:06:56 +0000287IdentifyingPassPtr TargetPassConfig::getPassSubstitution(AnalysisID ID) const {
288 DenseMap<AnalysisID, IdentifyingPassPtr>::const_iterator
Andrew Trick5e108ee2012-02-15 03:21:47 +0000289 I = Impl->TargetPasses.find(ID);
290 if (I == Impl->TargetPasses.end())
291 return ID;
292 return I->second;
293}
294
Bob Wilson30a507a2012-07-02 19:48:45 +0000295/// Add a pass to the PassManager if that pass is supposed to be run. If the
296/// Started/Stopped flags indicate either that the compilation should start at
297/// a later pass or that it should stop after an earlier pass, then do not add
298/// the pass. Finally, compare the current pass against the StartAfter
299/// and StopAfter options and change the Started/Stopped flags accordingly.
Bob Wilson564fbf62012-07-02 19:48:31 +0000300void TargetPassConfig::addPass(Pass *P) {
Bob Wilson6b2bb152012-07-02 19:48:39 +0000301 assert(!Initialized && "PassConfig is immutable");
302
Chandler Carruth6068c482012-07-02 22:56:41 +0000303 // Cache the Pass ID here in case the pass manager finds this pass is
304 // redundant with ones already scheduled / available, and deletes it.
305 // Fundamentally, once we add the pass to the manager, we no longer own it
306 // and shouldn't reference it.
307 AnalysisID PassID = P->getPassID();
308
Bob Wilson30a507a2012-07-02 19:48:45 +0000309 if (Started && !Stopped)
310 PM->add(P);
Benjamin Kramerf8e16c62013-08-05 11:11:11 +0000311 else
312 delete P;
Chandler Carruth6068c482012-07-02 22:56:41 +0000313 if (StopAfter == PassID)
Bob Wilson30a507a2012-07-02 19:48:45 +0000314 Stopped = true;
Chandler Carruth6068c482012-07-02 22:56:41 +0000315 if (StartAfter == PassID)
Bob Wilson30a507a2012-07-02 19:48:45 +0000316 Started = true;
317 if (Stopped && !Started)
318 report_fatal_error("Cannot stop compilation after pass that is not run");
Bob Wilson564fbf62012-07-02 19:48:31 +0000319}
320
Andrew Trick5e108ee2012-02-15 03:21:47 +0000321/// Add a CodeGen pass at this point in the pipeline after checking for target
322/// and command line overrides.
Andrew Trick5ed02832013-04-10 01:06:56 +0000323///
324/// addPass cannot return a pointer to the pass instance because is internal the
325/// PassManager and the instance we create here may already be freed.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000326AnalysisID TargetPassConfig::addPass(AnalysisID PassID) {
Andrew Trick5ed02832013-04-10 01:06:56 +0000327 IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
328 IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID);
329 if (!FinalPtr.isValid())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700330 return nullptr;
Andrew Trick5e108ee2012-02-15 03:21:47 +0000331
Andrew Trick5ed02832013-04-10 01:06:56 +0000332 Pass *P;
333 if (FinalPtr.isInstance())
334 P = FinalPtr.getInstance();
335 else {
336 P = Pass::createPass(FinalPtr.getID());
337 if (!P)
338 llvm_unreachable("Pass ID not registered");
339 }
340 AnalysisID FinalID = P->getPassID();
341 addPass(P); // Ends the lifetime of P.
342
Bob Wilson6e1b8122012-05-30 00:17:12 +0000343 // Add the passes after the pass P if there is any.
Craig Topperf22fd3f2013-07-03 05:11:49 +0000344 for (SmallVectorImpl<std::pair<AnalysisID, IdentifyingPassPtr> >::iterator
Bob Wilson6e1b8122012-05-30 00:17:12 +0000345 I = Impl->InsertedPasses.begin(), E = Impl->InsertedPasses.end();
346 I != E; ++I) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000347 if ((*I).first == PassID) {
Andrew Trick5ed02832013-04-10 01:06:56 +0000348 assert((*I).second.isValid() && "Illegal Pass ID!");
349 Pass *NP;
350 if ((*I).second.isInstance())
351 NP = (*I).second.getInstance();
352 else {
353 NP = Pass::createPass((*I).second.getID());
354 assert(NP && "Pass ID not registered");
355 }
Bob Wilson564fbf62012-07-02 19:48:31 +0000356 addPass(NP);
Bob Wilson6e1b8122012-05-30 00:17:12 +0000357 }
358 }
Andrew Trick5e108ee2012-02-15 03:21:47 +0000359 return FinalID;
Andrew Trick061efcf2012-02-04 02:56:59 +0000360}
Andrew Trickd5422652012-02-04 02:56:48 +0000361
Bob Wilson564fbf62012-07-02 19:48:31 +0000362void TargetPassConfig::printAndVerify(const char *Banner) {
Andrew Trickd5422652012-02-04 02:56:48 +0000363 if (TM->shouldPrintMachineCode())
Bob Wilson564fbf62012-07-02 19:48:31 +0000364 addPass(createMachineFunctionPrinterPass(dbgs(), Banner));
Andrew Trickd5422652012-02-04 02:56:48 +0000365
366 if (VerifyMachineCode)
Bob Wilson564fbf62012-07-02 19:48:31 +0000367 addPass(createMachineVerifierPass(Banner));
Andrew Trickd5422652012-02-04 02:56:48 +0000368}
369
Andrew Trick061efcf2012-02-04 02:56:59 +0000370/// Add common target configurable passes that perform LLVM IR to IR transforms
371/// following machine independent optimization.
372void TargetPassConfig::addIRPasses() {
Andrew Trickd5422652012-02-04 02:56:48 +0000373 // Basic AliasAnalysis support.
374 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
375 // BasicAliasAnalysis wins if they disagree. This is intended to help
376 // support "obvious" type-punning idioms.
Bob Wilson564fbf62012-07-02 19:48:31 +0000377 addPass(createTypeBasedAliasAnalysisPass());
378 addPass(createBasicAliasAnalysisPass());
Andrew Trickd5422652012-02-04 02:56:48 +0000379
380 // Before running any passes, run the verifier to determine if the input
381 // coming from the front-end and/or optimizer is valid.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700382 if (!DisableVerify) {
Bob Wilson564fbf62012-07-02 19:48:31 +0000383 addPass(createVerifierPass());
Stephen Hinesdce4a402014-05-29 02:49:00 -0700384 addPass(createDebugInfoVerifierPass());
385 }
Andrew Trickd5422652012-02-04 02:56:48 +0000386
387 // Run loop strength reduction before anything else.
388 if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
Chandler Carruthe4ba75f2013-01-07 14:41:08 +0000389 addPass(createLoopStrengthReducePass());
Andrew Trickd5422652012-02-04 02:56:48 +0000390 if (PrintLSR)
Stephen Hines36b56882014-04-23 16:57:46 -0700391 addPass(createPrintFunctionPass(dbgs(), "\n\n*** Code after LSR ***\n"));
Andrew Trickd5422652012-02-04 02:56:48 +0000392 }
393
Bob Wilson564fbf62012-07-02 19:48:31 +0000394 addPass(createGCLoweringPass());
Andrew Trickd5422652012-02-04 02:56:48 +0000395
396 // Make sure that no unreachable blocks are instruction selected.
Bob Wilson564fbf62012-07-02 19:48:31 +0000397 addPass(createUnreachableBlockEliminationPass());
Stephen Hines36b56882014-04-23 16:57:46 -0700398
399 // Prepare expensive constants for SelectionDAG.
400 if (getOptLevel() != CodeGenOpt::None && !DisableConstantHoisting)
401 addPass(createConstantHoistingPass());
Bob Wilson564fbf62012-07-02 19:48:31 +0000402}
403
404/// Turn exception handling constructs into something the code generators can
405/// handle.
406void TargetPassConfig::addPassesToHandleExceptions() {
407 switch (TM->getMCAsmInfo()->getExceptionHandlingType()) {
408 case ExceptionHandling::SjLj:
409 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
410 // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
411 // catch info can get misplaced when a selector ends up more than one block
412 // removed from the parent invoke(s). This could happen when a landing
413 // pad is shared by multiple invokes and is also a target of a normal
414 // edge from elsewhere.
Bill Wendlingea442812013-06-19 20:51:24 +0000415 addPass(createSjLjEHPreparePass(TM));
Bob Wilson564fbf62012-07-02 19:48:31 +0000416 // FALLTHROUGH
417 case ExceptionHandling::DwarfCFI:
418 case ExceptionHandling::ARM:
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700419 case ExceptionHandling::WinEH:
Bill Wendlingea442812013-06-19 20:51:24 +0000420 addPass(createDwarfEHPass(TM));
Bob Wilson564fbf62012-07-02 19:48:31 +0000421 break;
422 case ExceptionHandling::None:
Stephen Hines36b56882014-04-23 16:57:46 -0700423 addPass(createLowerInvokePass());
Bob Wilson564fbf62012-07-02 19:48:31 +0000424
425 // The lower invoke pass may create unreachable code. Remove it.
426 addPass(createUnreachableBlockEliminationPass());
427 break;
428 }
Andrew Trick061efcf2012-02-04 02:56:59 +0000429}
Andrew Trickd5422652012-02-04 02:56:48 +0000430
Bill Wendling08510b12012-11-30 22:08:55 +0000431/// Add pass to prepare the LLVM IR for code generation. This should be done
432/// before exception handling preparation passes.
433void TargetPassConfig::addCodeGenPrepare() {
434 if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
Bill Wendlingf9fd58a2013-06-19 21:07:11 +0000435 addPass(createCodeGenPreparePass(TM));
Bill Wendling08510b12012-11-30 22:08:55 +0000436}
437
Andrew Trick061efcf2012-02-04 02:56:59 +0000438/// Add common passes that perform LLVM IR to IR transforms in preparation for
439/// instruction selection.
440void TargetPassConfig::addISelPrepare() {
Andrew Trickd5422652012-02-04 02:56:48 +0000441 addPreISel();
442
Stephen Hinesdce4a402014-05-29 02:49:00 -0700443 // Need to verify DebugInfo *before* creating the stack protector analysis.
444 // It's a function pass, and verifying between it and its users causes a
445 // crash.
446 if (!DisableVerify)
447 addPass(createDebugInfoVerifierPass());
448
Stephen Hines36b56882014-04-23 16:57:46 -0700449 addPass(createStackProtectorPass(TM));
450
Andrew Trickd5422652012-02-04 02:56:48 +0000451 if (PrintISelInput)
Stephen Hines36b56882014-04-23 16:57:46 -0700452 addPass(createPrintFunctionPass(
453 dbgs(), "\n\n*** Final LLVM Code input to ISel ***\n"));
Andrew Trickd5422652012-02-04 02:56:48 +0000454
455 // All passes which modify the LLVM IR are now complete; run the verifier
456 // to ensure that the IR is valid.
457 if (!DisableVerify)
Bob Wilson564fbf62012-07-02 19:48:31 +0000458 addPass(createVerifierPass());
Andrew Trick061efcf2012-02-04 02:56:59 +0000459}
Andrew Trickd5422652012-02-04 02:56:48 +0000460
Andrew Trickf7b96312012-02-09 00:40:55 +0000461/// Add the complete set of target-independent postISel code generator passes.
462///
463/// This can be read as the standard order of major LLVM CodeGen stages. Stages
464/// with nontrivial configuration or multiple passes are broken out below in
465/// add%Stage routines.
466///
467/// Any TargetPassConfig::addXX routine may be overriden by the Target. The
468/// addPre/Post methods with empty header implementations allow injecting
469/// target-specific fixups just before or after major stages. Additionally,
470/// targets have the flexibility to change pass order within a stage by
471/// overriding default implementation of add%Stage routines below. Each
472/// technique has maintainability tradeoffs because alternate pass orders are
473/// not well supported. addPre/Post works better if the target pass is easily
474/// tied to a common pass. But if it has subtle dependencies on multiple passes,
Andrew Trick06efdd22012-02-10 07:08:25 +0000475/// the target should override the stage instead.
Andrew Trickf7b96312012-02-09 00:40:55 +0000476///
477/// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
478/// before/after any target-independent pass. But it's currently overkill.
Andrew Trick061efcf2012-02-04 02:56:59 +0000479void TargetPassConfig::addMachinePasses() {
Bob Wilson6e1b8122012-05-30 00:17:12 +0000480 // Insert a machine instr printer pass after the specified pass.
481 // If -print-machineinstrs specified, print machineinstrs after all passes.
482 if (StringRef(PrintMachineInstrs.getValue()).equals(""))
483 TM->Options.PrintMachineCode = true;
484 else if (!StringRef(PrintMachineInstrs.getValue())
485 .equals("option-unspecified")) {
486 const PassRegistry *PR = PassRegistry::getPassRegistry();
487 const PassInfo *TPI = PR->getPassInfo(PrintMachineInstrs.getValue());
488 const PassInfo *IPI = PR->getPassInfo(StringRef("print-machineinstrs"));
489 assert (TPI && IPI && "Pass ID not registered!");
Roman Divacky59324292012-09-05 22:26:57 +0000490 const char *TID = (const char *)(TPI->getTypeInfo());
491 const char *IID = (const char *)(IPI->getTypeInfo());
Bob Wilson3fb99a72012-07-02 19:48:37 +0000492 insertPass(TID, IID);
Bob Wilson6e1b8122012-05-30 00:17:12 +0000493 }
494
Jakob Stoklund Olesenf86c00f2012-07-04 19:28:27 +0000495 // Print the instruction selected machine code...
496 printAndVerify("After Instruction Selection");
497
Andrew Trickd5422652012-02-04 02:56:48 +0000498 // Expand pseudo-instructions emitted by ISel.
Jakob Stoklund Olesen228e3f52012-08-20 20:52:08 +0000499 if (addPass(&ExpandISelPseudosID))
500 printAndVerify("After ExpandISelPseudos");
Andrew Trickd5422652012-02-04 02:56:48 +0000501
Andrew Trickf7b96312012-02-09 00:40:55 +0000502 // Add passes that optimize machine instructions in SSA form.
Andrew Trickd5422652012-02-04 02:56:48 +0000503 if (getOptLevel() != CodeGenOpt::None) {
Andrew Trickf7b96312012-02-09 00:40:55 +0000504 addMachineSSAOptimization();
Craig Topper8f54a532012-11-19 00:11:50 +0000505 } else {
Andrew Trickf7b96312012-02-09 00:40:55 +0000506 // If the target requests it, assign local variables to stack slots relative
507 // to one another and simplify frame index references where possible.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000508 addPass(&LocalStackSlotAllocationID);
Andrew Trickd5422652012-02-04 02:56:48 +0000509 }
510
511 // Run pre-ra passes.
512 if (addPreRegAlloc())
513 printAndVerify("After PreRegAlloc passes");
514
Andrew Trickf7b96312012-02-09 00:40:55 +0000515 // Run register allocation and passes that are tightly coupled with it,
516 // including phi elimination and scheduling.
Andrew Trick8dd26252012-02-10 04:10:36 +0000517 if (getOptimizeRegAlloc())
518 addOptimizedRegAlloc(createRegAllocPass(true));
519 else
520 addFastRegAlloc(createRegAllocPass(false));
Andrew Trickd5422652012-02-04 02:56:48 +0000521
522 // Run post-ra passes.
523 if (addPostRegAlloc())
524 printAndVerify("After PostRegAlloc passes");
525
526 // Insert prolog/epilog code. Eliminate abstract frame index references...
Bob Wilson3fb99a72012-07-02 19:48:37 +0000527 addPass(&PrologEpilogCodeInserterID);
Andrew Trickd5422652012-02-04 02:56:48 +0000528 printAndVerify("After PrologEpilogCodeInserter");
529
Andrew Trickf7b96312012-02-09 00:40:55 +0000530 /// Add passes that optimize machine instructions after register allocation.
531 if (getOptLevel() != CodeGenOpt::None)
532 addMachineLateOptimization();
Andrew Trickd5422652012-02-04 02:56:48 +0000533
534 // Expand pseudo instructions before second scheduling pass.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000535 addPass(&ExpandPostRAPseudosID);
Jakob Stoklund Olesen2ef5bf62012-03-28 20:49:30 +0000536 printAndVerify("After ExpandPostRAPseudos");
Andrew Trickd5422652012-02-04 02:56:48 +0000537
538 // Run pre-sched2 passes.
539 if (addPreSched2())
Jakob Stoklund Olesen78811662012-03-28 23:31:15 +0000540 printAndVerify("After PreSched2 passes");
Andrew Trickd5422652012-02-04 02:56:48 +0000541
542 // Second pass scheduler.
Andrew Trick79bf2882012-02-15 03:21:51 +0000543 if (getOptLevel() != CodeGenOpt::None) {
Stephen Hines36b56882014-04-23 16:57:46 -0700544 if (MISchedPostRA)
545 addPass(&PostMachineSchedulerID);
546 else
547 addPass(&PostRASchedulerID);
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000548 printAndVerify("After PostRAScheduler");
Andrew Trickd5422652012-02-04 02:56:48 +0000549 }
550
Andrew Trickf7b96312012-02-09 00:40:55 +0000551 // GC
Evan Chengab37b2c2012-12-21 02:57:04 +0000552 if (addGCPasses()) {
553 if (PrintGCInfo)
554 addPass(createGCInfoPrinter(dbgs()));
555 }
Andrew Trickd5422652012-02-04 02:56:48 +0000556
Andrew Trickf7b96312012-02-09 00:40:55 +0000557 // Basic block placement.
Andrew Trick79bf2882012-02-15 03:21:51 +0000558 if (getOptLevel() != CodeGenOpt::None)
Andrew Trickf7b96312012-02-09 00:40:55 +0000559 addBlockPlacement();
Andrew Trickd5422652012-02-04 02:56:48 +0000560
561 if (addPreEmitPass())
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000562 printAndVerify("After PreEmit passes");
Stephen Hines36b56882014-04-23 16:57:46 -0700563
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700564 addPass(&StackMapLivenessID);
Andrew Trickd5422652012-02-04 02:56:48 +0000565}
566
Andrew Trickf7b96312012-02-09 00:40:55 +0000567/// Add passes that optimize machine instructions in SSA form.
568void TargetPassConfig::addMachineSSAOptimization() {
569 // Pre-ra tail duplication.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000570 if (addPass(&EarlyTailDuplicateID))
Andrew Trickf7b96312012-02-09 00:40:55 +0000571 printAndVerify("After Pre-RegAlloc TailDuplicate");
Andrew Trickf7b96312012-02-09 00:40:55 +0000572
573 // Optimize PHIs before DCE: removing dead PHI cycles may make more
574 // instructions dead.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000575 addPass(&OptimizePHIsID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000576
Nadav Rotemc05d3062012-09-06 09:17:37 +0000577 // This pass merges large allocas. StackSlotColoring is a different pass
578 // which merges spill slots.
579 addPass(&StackColoringID);
580
Andrew Trickf7b96312012-02-09 00:40:55 +0000581 // If the target requests it, assign local variables to stack slots relative
582 // to one another and simplify frame index references where possible.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000583 addPass(&LocalStackSlotAllocationID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000584
585 // With optimization, dead code should already be eliminated. However
586 // there is one known exception: lowered code for arguments that are only
587 // used by tail calls, where the tail calls reuse the incoming stack
588 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
Bob Wilson3fb99a72012-07-02 19:48:37 +0000589 addPass(&DeadMachineInstructionElimID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000590 printAndVerify("After codegen DCE pass");
591
Jakob Stoklund Olesen02c63252013-01-17 00:58:38 +0000592 // Allow targets to insert passes that improve instruction level parallelism,
593 // like if-conversion. Such passes will typically need dominator trees and
594 // loop info, just like LICM and CSE below.
595 if (addILPOpts())
596 printAndVerify("After ILP optimizations");
597
Bob Wilson3fb99a72012-07-02 19:48:37 +0000598 addPass(&MachineLICMID);
599 addPass(&MachineCSEID);
600 addPass(&MachineSinkingID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000601 printAndVerify("After Machine LICM, CSE and Sinking passes");
602
Bob Wilson3fb99a72012-07-02 19:48:37 +0000603 addPass(&PeepholeOptimizerID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000604 printAndVerify("After codegen peephole optimization pass");
605}
606
Andrew Trick74613342012-02-04 02:56:45 +0000607//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000608/// Register Allocation Pass Configuration
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000609//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000610
Andrew Trick8dd26252012-02-10 04:10:36 +0000611bool TargetPassConfig::getOptimizeRegAlloc() const {
612 switch (OptimizeRegAlloc) {
613 case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
614 case cl::BOU_TRUE: return true;
615 case cl::BOU_FALSE: return false;
616 }
617 llvm_unreachable("Invalid optimize-regalloc state");
618}
619
Andrew Trickf7b96312012-02-09 00:40:55 +0000620/// RegisterRegAlloc's global Registry tracks allocator registration.
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000621MachinePassRegistry RegisterRegAlloc::Registry;
622
Andrew Trickf7b96312012-02-09 00:40:55 +0000623/// A dummy default pass factory indicates whether the register allocator is
624/// overridden on the command line.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700625static FunctionPass *useDefaultRegisterAllocator() { return nullptr; }
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000626static RegisterRegAlloc
627defaultRegAlloc("default",
628 "pick register allocator based on -O option",
Andrew Trick8dd26252012-02-10 04:10:36 +0000629 useDefaultRegisterAllocator);
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000630
Andrew Trickf7b96312012-02-09 00:40:55 +0000631/// -regalloc=... command line option.
Dan Gohman844731a2008-05-13 00:00:25 +0000632static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
633 RegisterPassParser<RegisterRegAlloc> >
634RegAlloc("regalloc",
Andrew Trick8dd26252012-02-10 04:10:36 +0000635 cl::init(&useDefaultRegisterAllocator),
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000636 cl::desc("Register allocator to use"));
Alkis Evlogimenos7237ece2003-10-02 16:57:49 +0000637
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000638
Andrew Trick8dd26252012-02-10 04:10:36 +0000639/// Instantiate the default register allocator pass for this target for either
640/// the optimized or unoptimized allocation path. This will be added to the pass
641/// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
642/// in the optimized case.
643///
644/// A target that uses the standard regalloc pass order for fast or optimized
645/// allocation may still override this for per-target regalloc
646/// selection. But -regalloc=... always takes precedence.
647FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
648 if (Optimized)
649 return createGreedyRegisterAllocator();
650 else
651 return createFastRegisterAllocator();
652}
653
654/// Find and instantiate the register allocation pass requested by this target
655/// at the current optimization level. Different register allocators are
656/// defined as separate passes because they may require different analysis.
657///
658/// This helper ensures that the regalloc= option is always available,
659/// even for targets that override the default allocator.
660///
661/// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
662/// this can be folded into addPass.
663FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
Jim Laskey9ff542f2006-08-01 18:29:48 +0000664 RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000665
Andrew Trick8dd26252012-02-10 04:10:36 +0000666 // Initialize the global default.
Jim Laskey13ec7022006-08-01 14:21:23 +0000667 if (!Ctor) {
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000668 Ctor = RegAlloc;
669 RegisterRegAlloc::setDefault(RegAlloc);
Jim Laskey13ec7022006-08-01 14:21:23 +0000670 }
Andrew Trick8dd26252012-02-10 04:10:36 +0000671 if (Ctor != useDefaultRegisterAllocator)
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000672 return Ctor();
673
Andrew Trick8dd26252012-02-10 04:10:36 +0000674 // With no -regalloc= override, ask the target for a regalloc pass.
675 return createTargetRegisterAllocator(Optimized);
676}
677
678/// Add the minimum set of target-independent passes that are required for
679/// register allocation. No coalescing or scheduling.
680void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000681 addPass(&PHIEliminationID);
682 addPass(&TwoAddressInstructionPassID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000683
Bob Wilson564fbf62012-07-02 19:48:31 +0000684 addPass(RegAllocPass);
Andrew Trick8dd26252012-02-10 04:10:36 +0000685 printAndVerify("After Register Allocation");
Jim Laskey33a0a6d2006-07-27 20:05:00 +0000686}
Andrew Trickf7b96312012-02-09 00:40:55 +0000687
688/// Add standard target-independent passes that are tightly coupled with
Andrew Trick8dd26252012-02-10 04:10:36 +0000689/// optimized register allocation, including coalescing, machine instruction
690/// scheduling, and register allocation itself.
691void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000692 addPass(&ProcessImplicitDefsID);
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +0000693
Andrew Trick8dd26252012-02-10 04:10:36 +0000694 // LiveVariables currently requires pure SSA form.
695 //
696 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
697 // LiveVariables can be removed completely, and LiveIntervals can be directly
698 // computed. (We still either need to regenerate kill flags after regalloc, or
699 // preferably fix the scavenger to not depend on them).
Bob Wilson3fb99a72012-07-02 19:48:37 +0000700 addPass(&LiveVariablesID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000701
Rafael Espindola67b28822013-10-14 16:39:04 +0000702 // Edge splitting is smarter with machine loop info.
703 addPass(&MachineLoopInfoID);
704 addPass(&PHIEliminationID);
Jakob Stoklund Olesendcc44362012-08-03 22:12:54 +0000705
706 // Eventually, we want to run LiveIntervals before PHI elimination.
707 if (EarlyLiveIntervals)
708 addPass(&LiveIntervalsID);
709
Bob Wilson3fb99a72012-07-02 19:48:37 +0000710 addPass(&TwoAddressInstructionPassID);
Bob Wilson3fb99a72012-07-02 19:48:37 +0000711 addPass(&RegisterCoalescerID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000712
713 // PreRA instruction scheduling.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000714 if (addPass(&MachineSchedulerID))
Andrew Trick17d35e52012-03-14 04:00:41 +0000715 printAndVerify("After Machine Scheduling");
Andrew Trick8dd26252012-02-10 04:10:36 +0000716
717 // Add the selected register allocation pass.
Bob Wilson564fbf62012-07-02 19:48:31 +0000718 addPass(RegAllocPass);
Jakob Stoklund Olesen34f5a2b2012-06-26 17:09:29 +0000719 printAndVerify("After Register Allocation, before rewriter");
720
721 // Allow targets to change the register assignments before rewriting.
722 if (addPreRewrite())
723 printAndVerify("After pre-rewrite passes");
Andrew Trickf7b96312012-02-09 00:40:55 +0000724
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000725 // Finally rewrite virtual registers.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000726 addPass(&VirtRegRewriterID);
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000727 printAndVerify("After Virtual Register Rewriter");
728
Andrew Trickf7b96312012-02-09 00:40:55 +0000729 // Perform stack slot coloring and post-ra machine LICM.
Andrew Trick8dd26252012-02-10 04:10:36 +0000730 //
731 // FIXME: Re-enable coloring with register when it's capable of adding
732 // kill markers.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000733 addPass(&StackSlotColoringID);
Andrew Trick900d7b72012-02-15 07:57:03 +0000734
735 // Run post-ra machine LICM to hoist reloads / remats.
736 //
737 // FIXME: can this move into MachineLateOptimization?
Bob Wilson3fb99a72012-07-02 19:48:37 +0000738 addPass(&PostRAMachineLICMID);
Andrew Trick900d7b72012-02-15 07:57:03 +0000739
740 printAndVerify("After StackSlotColoring and postra Machine LICM");
Andrew Trickf7b96312012-02-09 00:40:55 +0000741}
742
743//===---------------------------------------------------------------------===//
744/// Post RegAlloc Pass Configuration
745//===---------------------------------------------------------------------===//
746
747/// Add passes that optimize machine instructions after register allocation.
748void TargetPassConfig::addMachineLateOptimization() {
749 // Branch folding must be run after regalloc and prolog/epilog insertion.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000750 if (addPass(&BranchFolderPassID))
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000751 printAndVerify("After BranchFolding");
Andrew Trickf7b96312012-02-09 00:40:55 +0000752
753 // Tail duplication.
Stephen Hines36b56882014-04-23 16:57:46 -0700754 // Note that duplicating tail just increases code size and degrades
755 // performance for targets that require Structured Control Flow.
756 // In addition it can also make CFG irreducible. Thus we disable it.
757 if (!TM->requiresStructuredCFG() && addPass(&TailDuplicateID))
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000758 printAndVerify("After TailDuplicate");
Andrew Trickf7b96312012-02-09 00:40:55 +0000759
760 // Copy propagation.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000761 if (addPass(&MachineCopyPropagationID))
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000762 printAndVerify("After copy propagation pass");
Andrew Trickf7b96312012-02-09 00:40:55 +0000763}
764
Evan Chengab37b2c2012-12-21 02:57:04 +0000765/// Add standard GC passes.
766bool TargetPassConfig::addGCPasses() {
767 addPass(&GCMachineCodeAnalysisID);
768 return true;
769}
770
Andrew Trickf7b96312012-02-09 00:40:55 +0000771/// Add standard basic block placement passes.
772void TargetPassConfig::addBlockPlacement() {
Benjamin Kramer74a45332013-03-29 17:14:24 +0000773 if (addPass(&MachineBlockPlacementID)) {
Andrew Trick79bf2882012-02-15 03:21:51 +0000774 // Run a separate pass to collect block placement statistics.
775 if (EnableBlockPlacementStats)
Bob Wilson3fb99a72012-07-02 19:48:37 +0000776 addPass(&MachineBlockPlacementStatsID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000777
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000778 printAndVerify("After machine block placement.");
Andrew Trickf7b96312012-02-09 00:40:55 +0000779 }
780}