blob: 1fefb9d45f47918607113e0be963f2f647d933c4 [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
Andrew Trickd5422652012-02-04 02:56:48 +000015#include "llvm/Analysis/Passes.h"
16#include "llvm/Analysis/Verifier.h"
17#include "llvm/Transforms/Scalar.h"
18#include "llvm/PassManager.h"
19#include "llvm/CodeGen/GCStrategy.h"
Andrew Trickd5422652012-02-04 02:56:48 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Alkis Evlogimenos7237ece2003-10-02 16:57:49 +000021#include "llvm/CodeGen/Passes.h"
Andrew Trickd5422652012-02-04 02:56:48 +000022#include "llvm/CodeGen/RegAllocRegistry.h"
23#include "llvm/Target/TargetLowering.h"
Andrew Trickd5422652012-02-04 02:56:48 +000024#include "llvm/Target/TargetOptions.h"
Bob Wilson564fbf62012-07-02 19:48:31 +000025#include "llvm/MC/MCAsmInfo.h"
Andrew Trickd5422652012-02-04 02:56:48 +000026#include "llvm/Assembly/PrintModulePass.h"
27#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/Debug.h"
Andrew Trick74613342012-02-04 02:56:45 +000029#include "llvm/Support/ErrorHandling.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",
42 cl::Hidden, cl::desc("Disable the probability-driven block placement, and "
43 "re-enable the old code placement pass"));
Andrew Trickd5422652012-02-04 02:56:48 +000044static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats",
45 cl::Hidden, cl::desc("Collect probability-driven block placement stats"));
46static cl::opt<bool> DisableCodePlace("disable-code-place", cl::Hidden,
47 cl::desc("Disable code placement"));
48static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
49 cl::desc("Disable Stack Slot Coloring"));
50static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden,
51 cl::desc("Disable Machine Dead Code Elimination"));
52static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
53 cl::desc("Disable Machine LICM"));
54static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden,
55 cl::desc("Disable Machine Common Subexpression Elimination"));
Andrew Trick8dd26252012-02-10 04:10:36 +000056static cl::opt<cl::boolOrDefault>
57OptimizeRegAlloc("optimize-regalloc", cl::Hidden,
58 cl::desc("Enable optimized register allocation compilation path."));
Andrew Trick746f24b2012-02-11 07:11:32 +000059static cl::opt<cl::boolOrDefault>
60EnableMachineSched("enable-misched", cl::Hidden,
Andrew Trick8dd26252012-02-10 04:10:36 +000061 cl::desc("Enable the machine instruction scheduling pass."));
62static cl::opt<bool> EnableStrongPHIElim("strong-phi-elim", cl::Hidden,
63 cl::desc("Use strong PHI elimination."));
Andrew Trickd5422652012-02-04 02:56:48 +000064static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
65 cl::Hidden,
66 cl::desc("Disable Machine LICM"));
67static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
68 cl::desc("Disable Machine Sinking"));
69static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
70 cl::desc("Disable Loop Strength Reduction Pass"));
71static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
72 cl::desc("Disable Codegen Prepare"));
73static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden,
Evan Cheng01b623c2012-02-20 23:28:17 +000074 cl::desc("Disable Copy Propagation pass"));
Andrew Trickd5422652012-02-04 02:56:48 +000075static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
76 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
77static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
78 cl::desc("Print LLVM IR input to isel pass"));
79static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
80 cl::desc("Dump garbage collector data"));
81static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
82 cl::desc("Verify generated machine code"),
83 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
Bob Wilson6e1b8122012-05-30 00:17:12 +000084static cl::opt<std::string>
85PrintMachineInstrs("print-machineinstrs", cl::ValueOptional,
86 cl::desc("Print machine instrs"),
87 cl::value_desc("pass-name"), cl::init("option-unspecified"));
Andrew Trickd5422652012-02-04 02:56:48 +000088
Andrew Trick79bf2882012-02-15 03:21:51 +000089/// Allow standard passes to be disabled by command line options. This supports
90/// simple binary flags that either suppress the pass or do nothing.
91/// i.e. -disable-mypass=false has no effect.
92/// These should be converted to boolOrDefault in order to use applyOverride.
Bob Wilson3fb99a72012-07-02 19:48:37 +000093static AnalysisID applyDisable(AnalysisID PassID, bool Override) {
Andrew Trick79bf2882012-02-15 03:21:51 +000094 if (Override)
Bob Wilson3fb99a72012-07-02 19:48:37 +000095 return 0;
96 return PassID;
Andrew Trick79bf2882012-02-15 03:21:51 +000097}
98
99/// Allow Pass selection to be overriden by command line options. This supports
100/// flags with ternary conditions. TargetID is passed through by default. The
101/// pass is suppressed when the option is false. When the option is true, the
102/// StandardID is selected if the target provides no default.
103static AnalysisID applyOverride(AnalysisID TargetID, cl::boolOrDefault Override,
104 AnalysisID StandardID) {
Andrew Trick746f24b2012-02-11 07:11:32 +0000105 switch (Override) {
106 case cl::BOU_UNSET:
Andrew Trick79bf2882012-02-15 03:21:51 +0000107 return TargetID;
Andrew Trick746f24b2012-02-11 07:11:32 +0000108 case cl::BOU_TRUE:
Bob Wilson3fb99a72012-07-02 19:48:37 +0000109 if (TargetID)
Andrew Trick79bf2882012-02-15 03:21:51 +0000110 return TargetID;
Bob Wilson3fb99a72012-07-02 19:48:37 +0000111 if (StandardID == 0)
Andrew Trick746f24b2012-02-11 07:11:32 +0000112 report_fatal_error("Target cannot enable pass");
Andrew Trick79bf2882012-02-15 03:21:51 +0000113 return StandardID;
Andrew Trick746f24b2012-02-11 07:11:32 +0000114 case cl::BOU_FALSE:
Bob Wilson3fb99a72012-07-02 19:48:37 +0000115 return 0;
Andrew Trick746f24b2012-02-11 07:11:32 +0000116 }
117 llvm_unreachable("Invalid command line option state");
118}
119
Andrew Trick79bf2882012-02-15 03:21:51 +0000120/// Allow standard passes to be disabled by the command line, regardless of who
121/// is adding the pass.
122///
123/// StandardID is the pass identified in the standard pass pipeline and provided
124/// to addPass(). It may be a target-specific ID in the case that the target
125/// directly adds its own pass, but in that case we harmlessly fall through.
126///
127/// TargetID is the pass that the target has configured to override StandardID.
128///
129/// StandardID may be a pseudo ID. In that case TargetID is the name of the real
130/// pass to run. This allows multiple options to control a single pass depending
131/// on where in the pipeline that pass is added.
132static AnalysisID overridePass(AnalysisID StandardID, AnalysisID TargetID) {
133 if (StandardID == &PostRASchedulerID)
134 return applyDisable(TargetID, DisablePostRA);
135
136 if (StandardID == &BranchFolderPassID)
137 return applyDisable(TargetID, DisableBranchFold);
138
139 if (StandardID == &TailDuplicateID)
140 return applyDisable(TargetID, DisableTailDuplicate);
141
142 if (StandardID == &TargetPassConfig::EarlyTailDuplicateID)
143 return applyDisable(TargetID, DisableEarlyTailDup);
144
145 if (StandardID == &MachineBlockPlacementID)
146 return applyDisable(TargetID, DisableCodePlace);
147
148 if (StandardID == &CodePlacementOptID)
149 return applyDisable(TargetID, DisableCodePlace);
150
151 if (StandardID == &StackSlotColoringID)
152 return applyDisable(TargetID, DisableSSC);
153
154 if (StandardID == &DeadMachineInstructionElimID)
155 return applyDisable(TargetID, DisableMachineDCE);
156
157 if (StandardID == &MachineLICMID)
158 return applyDisable(TargetID, DisableMachineLICM);
159
160 if (StandardID == &MachineCSEID)
161 return applyDisable(TargetID, DisableMachineCSE);
162
163 if (StandardID == &MachineSchedulerID)
164 return applyOverride(TargetID, EnableMachineSched, StandardID);
165
166 if (StandardID == &TargetPassConfig::PostRAMachineLICMID)
167 return applyDisable(TargetID, DisablePostRAMachineLICM);
168
169 if (StandardID == &MachineSinkingID)
170 return applyDisable(TargetID, DisableMachineSink);
171
172 if (StandardID == &MachineCopyPropagationID)
173 return applyDisable(TargetID, DisableCopyProp);
174
175 return TargetID;
176}
177
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000178//===---------------------------------------------------------------------===//
Andrew Trick74613342012-02-04 02:56:45 +0000179/// TargetPassConfig
180//===---------------------------------------------------------------------===//
181
182INITIALIZE_PASS(TargetPassConfig, "targetpassconfig",
183 "Target Pass Configuration", false, false)
184char TargetPassConfig::ID = 0;
185
Andrew Trick79bf2882012-02-15 03:21:51 +0000186// Pseudo Pass IDs.
187char TargetPassConfig::EarlyTailDuplicateID = 0;
188char TargetPassConfig::PostRAMachineLICMID = 0;
189
Andrew Trick5e108ee2012-02-15 03:21:47 +0000190namespace llvm {
191class PassConfigImpl {
192public:
193 // List of passes explicitly substituted by this target. Normally this is
194 // empty, but it is a convenient way to suppress or replace specific passes
195 // that are part of a standard pass pipeline without overridding the entire
196 // pipeline. This mechanism allows target options to inherit a standard pass's
197 // user interface. For example, a target may disable a standard pass by
Bob Wilson3fb99a72012-07-02 19:48:37 +0000198 // default by substituting a pass ID of zero, and the user may still enable
199 // that standard pass with an explicit command line option.
Andrew Trick5e108ee2012-02-15 03:21:47 +0000200 DenseMap<AnalysisID,AnalysisID> TargetPasses;
Bob Wilson6e1b8122012-05-30 00:17:12 +0000201
202 /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass
203 /// is inserted after each instance of the first one.
204 SmallVector<std::pair<AnalysisID, AnalysisID>, 4> InsertedPasses;
Andrew Trick5e108ee2012-02-15 03:21:47 +0000205};
206} // namespace llvm
207
Andrew Trick74613342012-02-04 02:56:45 +0000208// Out of line virtual method.
Andrew Trick5e108ee2012-02-15 03:21:47 +0000209TargetPassConfig::~TargetPassConfig() {
210 delete Impl;
211}
Andrew Trick74613342012-02-04 02:56:45 +0000212
Andrew Trick61f1e3d2012-02-08 21:22:48 +0000213// Out of line constructor provides default values for pass options and
214// registers all common codegen passes.
Andrew Trick061efcf2012-02-04 02:56:59 +0000215TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm)
Bob Wilson30a507a2012-07-02 19:48:45 +0000216 : ImmutablePass(ID), PM(&pm), StartAfter(0), StopAfter(0),
217 Started(true), Stopped(false), TM(tm), Impl(0), Initialized(false),
Andrew Trickffea03f2012-02-08 21:22:39 +0000218 DisableVerify(false),
219 EnableTailMerge(true) {
220
Andrew Trick5e108ee2012-02-15 03:21:47 +0000221 Impl = new PassConfigImpl();
222
Andrew Trick74613342012-02-04 02:56:45 +0000223 // Register all target independent codegen passes to activate their PassIDs,
224 // including this pass itself.
225 initializeCodeGen(*PassRegistry::getPassRegistry());
Andrew Trick79bf2882012-02-15 03:21:51 +0000226
227 // Substitute Pseudo Pass IDs for real ones.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000228 substitutePass(&EarlyTailDuplicateID, &TailDuplicateID);
229 substitutePass(&PostRAMachineLICMID, &MachineLICMID);
Andrew Trick79bf2882012-02-15 03:21:51 +0000230
231 // Temporarily disable experimental passes.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000232 substitutePass(&MachineSchedulerID, 0);
Andrew Trick74613342012-02-04 02:56:45 +0000233}
234
Bob Wilson6e1b8122012-05-30 00:17:12 +0000235/// Insert InsertedPassID pass after TargetPassID.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000236void TargetPassConfig::insertPass(AnalysisID TargetPassID,
237 AnalysisID InsertedPassID) {
238 assert(TargetPassID != InsertedPassID && "Insert a pass after itself!");
239 std::pair<AnalysisID, AnalysisID> P(TargetPassID, InsertedPassID);
Bob Wilson6e1b8122012-05-30 00:17:12 +0000240 Impl->InsertedPasses.push_back(P);
241}
242
Andrew Trick74613342012-02-04 02:56:45 +0000243/// createPassConfig - Create a pass configuration object to be used by
244/// addPassToEmitX methods for generating a pipeline of CodeGen passes.
245///
246/// Targets may override this to extend TargetPassConfig.
Andrew Trick061efcf2012-02-04 02:56:59 +0000247TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
248 return new TargetPassConfig(this, PM);
Andrew Trick74613342012-02-04 02:56:45 +0000249}
250
251TargetPassConfig::TargetPassConfig()
Bill Wendling7c4ce302012-05-01 08:27:43 +0000252 : ImmutablePass(ID), PM(0) {
Andrew Trick74613342012-02-04 02:56:45 +0000253 llvm_unreachable("TargetPassConfig should not be constructed on-the-fly");
254}
255
Andrew Trickffea03f2012-02-08 21:22:39 +0000256// Helper to verify the analysis is really immutable.
257void TargetPassConfig::setOpt(bool &Opt, bool Val) {
258 assert(!Initialized && "PassConfig is immutable");
259 Opt = Val;
260}
261
Bob Wilson3fb99a72012-07-02 19:48:37 +0000262void TargetPassConfig::substitutePass(AnalysisID StandardID,
263 AnalysisID TargetID) {
264 Impl->TargetPasses[StandardID] = TargetID;
Andrew Trick5e108ee2012-02-15 03:21:47 +0000265}
Andrew Trick746f24b2012-02-11 07:11:32 +0000266
Andrew Trick5e108ee2012-02-15 03:21:47 +0000267AnalysisID TargetPassConfig::getPassSubstitution(AnalysisID ID) const {
268 DenseMap<AnalysisID, AnalysisID>::const_iterator
269 I = Impl->TargetPasses.find(ID);
270 if (I == Impl->TargetPasses.end())
271 return ID;
272 return I->second;
273}
274
Bob Wilson30a507a2012-07-02 19:48:45 +0000275/// Add a pass to the PassManager if that pass is supposed to be run. If the
276/// Started/Stopped flags indicate either that the compilation should start at
277/// a later pass or that it should stop after an earlier pass, then do not add
278/// the pass. Finally, compare the current pass against the StartAfter
279/// and StopAfter options and change the Started/Stopped flags accordingly.
Bob Wilson564fbf62012-07-02 19:48:31 +0000280void TargetPassConfig::addPass(Pass *P) {
Bob Wilson6b2bb152012-07-02 19:48:39 +0000281 assert(!Initialized && "PassConfig is immutable");
282
Bob Wilson30a507a2012-07-02 19:48:45 +0000283 if (Started && !Stopped)
284 PM->add(P);
285 if (StopAfter == P->getPassID())
286 Stopped = true;
287 if (StartAfter == P->getPassID())
288 Started = true;
289 if (Stopped && !Started)
290 report_fatal_error("Cannot stop compilation after pass that is not run");
Bob Wilson564fbf62012-07-02 19:48:31 +0000291}
292
Andrew Trick5e108ee2012-02-15 03:21:47 +0000293/// Add a CodeGen pass at this point in the pipeline after checking for target
294/// and command line overrides.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000295AnalysisID TargetPassConfig::addPass(AnalysisID PassID) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000296 AnalysisID TargetID = getPassSubstitution(PassID);
297 AnalysisID FinalID = overridePass(PassID, TargetID);
298 if (FinalID == 0)
Andrew Trick5e108ee2012-02-15 03:21:47 +0000299 return FinalID;
300
301 Pass *P = Pass::createPass(FinalID);
Andrew Trickebe18ef2012-02-08 21:22:34 +0000302 if (!P)
303 llvm_unreachable("Pass ID not registered");
Bob Wilson564fbf62012-07-02 19:48:31 +0000304 addPass(P);
Bob Wilson6e1b8122012-05-30 00:17:12 +0000305 // Add the passes after the pass P if there is any.
306 for (SmallVector<std::pair<AnalysisID, AnalysisID>, 4>::iterator
307 I = Impl->InsertedPasses.begin(), E = Impl->InsertedPasses.end();
308 I != E; ++I) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000309 if ((*I).first == PassID) {
Bob Wilson6e1b8122012-05-30 00:17:12 +0000310 assert((*I).second && "Illegal Pass ID!");
311 Pass *NP = Pass::createPass((*I).second);
312 assert(NP && "Pass ID not registered");
Bob Wilson564fbf62012-07-02 19:48:31 +0000313 addPass(NP);
Bob Wilson6e1b8122012-05-30 00:17:12 +0000314 }
315 }
Andrew Trick5e108ee2012-02-15 03:21:47 +0000316 return FinalID;
Andrew Trick061efcf2012-02-04 02:56:59 +0000317}
Andrew Trickd5422652012-02-04 02:56:48 +0000318
Bob Wilson564fbf62012-07-02 19:48:31 +0000319void TargetPassConfig::printAndVerify(const char *Banner) {
Andrew Trickd5422652012-02-04 02:56:48 +0000320 if (TM->shouldPrintMachineCode())
Bob Wilson564fbf62012-07-02 19:48:31 +0000321 addPass(createMachineFunctionPrinterPass(dbgs(), Banner));
Andrew Trickd5422652012-02-04 02:56:48 +0000322
323 if (VerifyMachineCode)
Bob Wilson564fbf62012-07-02 19:48:31 +0000324 addPass(createMachineVerifierPass(Banner));
Andrew Trickd5422652012-02-04 02:56:48 +0000325}
326
Andrew Trick061efcf2012-02-04 02:56:59 +0000327/// Add common target configurable passes that perform LLVM IR to IR transforms
328/// following machine independent optimization.
329void TargetPassConfig::addIRPasses() {
Andrew Trickd5422652012-02-04 02:56:48 +0000330 // Basic AliasAnalysis support.
331 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
332 // BasicAliasAnalysis wins if they disagree. This is intended to help
333 // support "obvious" type-punning idioms.
Bob Wilson564fbf62012-07-02 19:48:31 +0000334 addPass(createTypeBasedAliasAnalysisPass());
335 addPass(createBasicAliasAnalysisPass());
Andrew Trickd5422652012-02-04 02:56:48 +0000336
337 // Before running any passes, run the verifier to determine if the input
338 // coming from the front-end and/or optimizer is valid.
339 if (!DisableVerify)
Bob Wilson564fbf62012-07-02 19:48:31 +0000340 addPass(createVerifierPass());
Andrew Trickd5422652012-02-04 02:56:48 +0000341
342 // Run loop strength reduction before anything else.
343 if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
Bob Wilson564fbf62012-07-02 19:48:31 +0000344 addPass(createLoopStrengthReducePass(getTargetLowering()));
Andrew Trickd5422652012-02-04 02:56:48 +0000345 if (PrintLSR)
Bob Wilson564fbf62012-07-02 19:48:31 +0000346 addPass(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
Andrew Trickd5422652012-02-04 02:56:48 +0000347 }
348
Bob Wilson564fbf62012-07-02 19:48:31 +0000349 addPass(createGCLoweringPass());
Andrew Trickd5422652012-02-04 02:56:48 +0000350
351 // Make sure that no unreachable blocks are instruction selected.
Bob Wilson564fbf62012-07-02 19:48:31 +0000352 addPass(createUnreachableBlockEliminationPass());
353}
354
355/// Turn exception handling constructs into something the code generators can
356/// handle.
357void TargetPassConfig::addPassesToHandleExceptions() {
358 switch (TM->getMCAsmInfo()->getExceptionHandlingType()) {
359 case ExceptionHandling::SjLj:
360 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
361 // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
362 // catch info can get misplaced when a selector ends up more than one block
363 // removed from the parent invoke(s). This could happen when a landing
364 // pad is shared by multiple invokes and is also a target of a normal
365 // edge from elsewhere.
366 addPass(createSjLjEHPreparePass(TM->getTargetLowering()));
367 // FALLTHROUGH
368 case ExceptionHandling::DwarfCFI:
369 case ExceptionHandling::ARM:
370 case ExceptionHandling::Win64:
371 addPass(createDwarfEHPass(TM));
372 break;
373 case ExceptionHandling::None:
374 addPass(createLowerInvokePass(TM->getTargetLowering()));
375
376 // The lower invoke pass may create unreachable code. Remove it.
377 addPass(createUnreachableBlockEliminationPass());
378 break;
379 }
Andrew Trick061efcf2012-02-04 02:56:59 +0000380}
Andrew Trickd5422652012-02-04 02:56:48 +0000381
Andrew Trick061efcf2012-02-04 02:56:59 +0000382/// Add common passes that perform LLVM IR to IR transforms in preparation for
383/// instruction selection.
384void TargetPassConfig::addISelPrepare() {
Andrew Trickd5422652012-02-04 02:56:48 +0000385 if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
Bob Wilson564fbf62012-07-02 19:48:31 +0000386 addPass(createCodeGenPreparePass(getTargetLowering()));
Andrew Trickd5422652012-02-04 02:56:48 +0000387
Bob Wilson564fbf62012-07-02 19:48:31 +0000388 addPass(createStackProtectorPass(getTargetLowering()));
Andrew Trickd5422652012-02-04 02:56:48 +0000389
390 addPreISel();
391
392 if (PrintISelInput)
Bob Wilson564fbf62012-07-02 19:48:31 +0000393 addPass(createPrintFunctionPass("\n\n"
Bill Wendling7c4ce302012-05-01 08:27:43 +0000394 "*** Final LLVM Code input to ISel ***\n",
395 &dbgs()));
Andrew Trickd5422652012-02-04 02:56:48 +0000396
397 // All passes which modify the LLVM IR are now complete; run the verifier
398 // to ensure that the IR is valid.
399 if (!DisableVerify)
Bob Wilson564fbf62012-07-02 19:48:31 +0000400 addPass(createVerifierPass());
Andrew Trick061efcf2012-02-04 02:56:59 +0000401}
Andrew Trickd5422652012-02-04 02:56:48 +0000402
Andrew Trickf7b96312012-02-09 00:40:55 +0000403/// Add the complete set of target-independent postISel code generator passes.
404///
405/// This can be read as the standard order of major LLVM CodeGen stages. Stages
406/// with nontrivial configuration or multiple passes are broken out below in
407/// add%Stage routines.
408///
409/// Any TargetPassConfig::addXX routine may be overriden by the Target. The
410/// addPre/Post methods with empty header implementations allow injecting
411/// target-specific fixups just before or after major stages. Additionally,
412/// targets have the flexibility to change pass order within a stage by
413/// overriding default implementation of add%Stage routines below. Each
414/// technique has maintainability tradeoffs because alternate pass orders are
415/// not well supported. addPre/Post works better if the target pass is easily
416/// tied to a common pass. But if it has subtle dependencies on multiple passes,
Andrew Trick06efdd22012-02-10 07:08:25 +0000417/// the target should override the stage instead.
Andrew Trickf7b96312012-02-09 00:40:55 +0000418///
419/// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
420/// before/after any target-independent pass. But it's currently overkill.
Andrew Trick061efcf2012-02-04 02:56:59 +0000421void TargetPassConfig::addMachinePasses() {
Andrew Trickd5422652012-02-04 02:56:48 +0000422 // Print the instruction selected machine code...
423 printAndVerify("After Instruction Selection");
424
Bob Wilson6e1b8122012-05-30 00:17:12 +0000425 // Insert a machine instr printer pass after the specified pass.
426 // If -print-machineinstrs specified, print machineinstrs after all passes.
427 if (StringRef(PrintMachineInstrs.getValue()).equals(""))
428 TM->Options.PrintMachineCode = true;
429 else if (!StringRef(PrintMachineInstrs.getValue())
430 .equals("option-unspecified")) {
431 const PassRegistry *PR = PassRegistry::getPassRegistry();
432 const PassInfo *TPI = PR->getPassInfo(PrintMachineInstrs.getValue());
433 const PassInfo *IPI = PR->getPassInfo(StringRef("print-machineinstrs"));
434 assert (TPI && IPI && "Pass ID not registered!");
435 const char *TID = (char *)(TPI->getTypeInfo());
436 const char *IID = (char *)(IPI->getTypeInfo());
Bob Wilson3fb99a72012-07-02 19:48:37 +0000437 insertPass(TID, IID);
Bob Wilson6e1b8122012-05-30 00:17:12 +0000438 }
439
Andrew Trickd5422652012-02-04 02:56:48 +0000440 // Expand pseudo-instructions emitted by ISel.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000441 addPass(&ExpandISelPseudosID);
Andrew Trickd5422652012-02-04 02:56:48 +0000442
Andrew Trickf7b96312012-02-09 00:40:55 +0000443 // Add passes that optimize machine instructions in SSA form.
Andrew Trickd5422652012-02-04 02:56:48 +0000444 if (getOptLevel() != CodeGenOpt::None) {
Andrew Trickf7b96312012-02-09 00:40:55 +0000445 addMachineSSAOptimization();
446 }
447 else {
448 // If the target requests it, assign local variables to stack slots relative
449 // to one another and simplify frame index references where possible.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000450 addPass(&LocalStackSlotAllocationID);
Andrew Trickd5422652012-02-04 02:56:48 +0000451 }
452
453 // Run pre-ra passes.
454 if (addPreRegAlloc())
455 printAndVerify("After PreRegAlloc passes");
456
Andrew Trickf7b96312012-02-09 00:40:55 +0000457 // Run register allocation and passes that are tightly coupled with it,
458 // including phi elimination and scheduling.
Andrew Trick8dd26252012-02-10 04:10:36 +0000459 if (getOptimizeRegAlloc())
460 addOptimizedRegAlloc(createRegAllocPass(true));
461 else
462 addFastRegAlloc(createRegAllocPass(false));
Andrew Trickd5422652012-02-04 02:56:48 +0000463
464 // Run post-ra passes.
465 if (addPostRegAlloc())
466 printAndVerify("After PostRegAlloc passes");
467
468 // Insert prolog/epilog code. Eliminate abstract frame index references...
Bob Wilson3fb99a72012-07-02 19:48:37 +0000469 addPass(&PrologEpilogCodeInserterID);
Andrew Trickd5422652012-02-04 02:56:48 +0000470 printAndVerify("After PrologEpilogCodeInserter");
471
Andrew Trickf7b96312012-02-09 00:40:55 +0000472 /// Add passes that optimize machine instructions after register allocation.
473 if (getOptLevel() != CodeGenOpt::None)
474 addMachineLateOptimization();
Andrew Trickd5422652012-02-04 02:56:48 +0000475
476 // Expand pseudo instructions before second scheduling pass.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000477 addPass(&ExpandPostRAPseudosID);
Jakob Stoklund Olesen2ef5bf62012-03-28 20:49:30 +0000478 printAndVerify("After ExpandPostRAPseudos");
Andrew Trickd5422652012-02-04 02:56:48 +0000479
480 // Run pre-sched2 passes.
481 if (addPreSched2())
Jakob Stoklund Olesen78811662012-03-28 23:31:15 +0000482 printAndVerify("After PreSched2 passes");
Andrew Trickd5422652012-02-04 02:56:48 +0000483
484 // Second pass scheduler.
Andrew Trick79bf2882012-02-15 03:21:51 +0000485 if (getOptLevel() != CodeGenOpt::None) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000486 addPass(&PostRASchedulerID);
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000487 printAndVerify("After PostRAScheduler");
Andrew Trickd5422652012-02-04 02:56:48 +0000488 }
489
Andrew Trickf7b96312012-02-09 00:40:55 +0000490 // GC
Bob Wilson3fb99a72012-07-02 19:48:37 +0000491 addPass(&GCMachineCodeAnalysisID);
Andrew Trickd5422652012-02-04 02:56:48 +0000492 if (PrintGCInfo)
Bob Wilson564fbf62012-07-02 19:48:31 +0000493 addPass(createGCInfoPrinter(dbgs()));
Andrew Trickd5422652012-02-04 02:56:48 +0000494
Andrew Trickf7b96312012-02-09 00:40:55 +0000495 // Basic block placement.
Andrew Trick79bf2882012-02-15 03:21:51 +0000496 if (getOptLevel() != CodeGenOpt::None)
Andrew Trickf7b96312012-02-09 00:40:55 +0000497 addBlockPlacement();
Andrew Trickd5422652012-02-04 02:56:48 +0000498
499 if (addPreEmitPass())
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000500 printAndVerify("After PreEmit passes");
Andrew Trickd5422652012-02-04 02:56:48 +0000501}
502
Andrew Trickf7b96312012-02-09 00:40:55 +0000503/// Add passes that optimize machine instructions in SSA form.
504void TargetPassConfig::addMachineSSAOptimization() {
505 // Pre-ra tail duplication.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000506 if (addPass(&EarlyTailDuplicateID))
Andrew Trickf7b96312012-02-09 00:40:55 +0000507 printAndVerify("After Pre-RegAlloc TailDuplicate");
Andrew Trickf7b96312012-02-09 00:40:55 +0000508
509 // Optimize PHIs before DCE: removing dead PHI cycles may make more
510 // instructions dead.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000511 addPass(&OptimizePHIsID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000512
513 // If the target requests it, assign local variables to stack slots relative
514 // to one another and simplify frame index references where possible.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000515 addPass(&LocalStackSlotAllocationID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000516
517 // With optimization, dead code should already be eliminated. However
518 // there is one known exception: lowered code for arguments that are only
519 // used by tail calls, where the tail calls reuse the incoming stack
520 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
Bob Wilson3fb99a72012-07-02 19:48:37 +0000521 addPass(&DeadMachineInstructionElimID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000522 printAndVerify("After codegen DCE pass");
523
Bob Wilson3fb99a72012-07-02 19:48:37 +0000524 addPass(&MachineLICMID);
525 addPass(&MachineCSEID);
526 addPass(&MachineSinkingID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000527 printAndVerify("After Machine LICM, CSE and Sinking passes");
528
Bob Wilson3fb99a72012-07-02 19:48:37 +0000529 addPass(&PeepholeOptimizerID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000530 printAndVerify("After codegen peephole optimization pass");
531}
532
Andrew Trick74613342012-02-04 02:56:45 +0000533//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000534/// Register Allocation Pass Configuration
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000535//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000536
Andrew Trick8dd26252012-02-10 04:10:36 +0000537bool TargetPassConfig::getOptimizeRegAlloc() const {
538 switch (OptimizeRegAlloc) {
539 case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
540 case cl::BOU_TRUE: return true;
541 case cl::BOU_FALSE: return false;
542 }
543 llvm_unreachable("Invalid optimize-regalloc state");
544}
545
Andrew Trickf7b96312012-02-09 00:40:55 +0000546/// RegisterRegAlloc's global Registry tracks allocator registration.
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000547MachinePassRegistry RegisterRegAlloc::Registry;
548
Andrew Trickf7b96312012-02-09 00:40:55 +0000549/// A dummy default pass factory indicates whether the register allocator is
550/// overridden on the command line.
Andrew Trick8dd26252012-02-10 04:10:36 +0000551static FunctionPass *useDefaultRegisterAllocator() { return 0; }
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000552static RegisterRegAlloc
553defaultRegAlloc("default",
554 "pick register allocator based on -O option",
Andrew Trick8dd26252012-02-10 04:10:36 +0000555 useDefaultRegisterAllocator);
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000556
Andrew Trickf7b96312012-02-09 00:40:55 +0000557/// -regalloc=... command line option.
Dan Gohman844731a2008-05-13 00:00:25 +0000558static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
559 RegisterPassParser<RegisterRegAlloc> >
560RegAlloc("regalloc",
Andrew Trick8dd26252012-02-10 04:10:36 +0000561 cl::init(&useDefaultRegisterAllocator),
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000562 cl::desc("Register allocator to use"));
Alkis Evlogimenos7237ece2003-10-02 16:57:49 +0000563
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000564
Andrew Trick8dd26252012-02-10 04:10:36 +0000565/// Instantiate the default register allocator pass for this target for either
566/// the optimized or unoptimized allocation path. This will be added to the pass
567/// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
568/// in the optimized case.
569///
570/// A target that uses the standard regalloc pass order for fast or optimized
571/// allocation may still override this for per-target regalloc
572/// selection. But -regalloc=... always takes precedence.
573FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
574 if (Optimized)
575 return createGreedyRegisterAllocator();
576 else
577 return createFastRegisterAllocator();
578}
579
580/// Find and instantiate the register allocation pass requested by this target
581/// at the current optimization level. Different register allocators are
582/// defined as separate passes because they may require different analysis.
583///
584/// This helper ensures that the regalloc= option is always available,
585/// even for targets that override the default allocator.
586///
587/// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
588/// this can be folded into addPass.
589FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
Jim Laskey9ff542f2006-08-01 18:29:48 +0000590 RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000591
Andrew Trick8dd26252012-02-10 04:10:36 +0000592 // Initialize the global default.
Jim Laskey13ec7022006-08-01 14:21:23 +0000593 if (!Ctor) {
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000594 Ctor = RegAlloc;
595 RegisterRegAlloc::setDefault(RegAlloc);
Jim Laskey13ec7022006-08-01 14:21:23 +0000596 }
Andrew Trick8dd26252012-02-10 04:10:36 +0000597 if (Ctor != useDefaultRegisterAllocator)
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000598 return Ctor();
599
Andrew Trick8dd26252012-02-10 04:10:36 +0000600 // With no -regalloc= override, ask the target for a regalloc pass.
601 return createTargetRegisterAllocator(Optimized);
602}
603
604/// Add the minimum set of target-independent passes that are required for
605/// register allocation. No coalescing or scheduling.
606void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000607 addPass(&PHIEliminationID);
608 addPass(&TwoAddressInstructionPassID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000609
Bob Wilson564fbf62012-07-02 19:48:31 +0000610 addPass(RegAllocPass);
Andrew Trick8dd26252012-02-10 04:10:36 +0000611 printAndVerify("After Register Allocation");
Jim Laskey33a0a6d2006-07-27 20:05:00 +0000612}
Andrew Trickf7b96312012-02-09 00:40:55 +0000613
614/// Add standard target-independent passes that are tightly coupled with
Andrew Trick8dd26252012-02-10 04:10:36 +0000615/// optimized register allocation, including coalescing, machine instruction
616/// scheduling, and register allocation itself.
617void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000618 addPass(&ProcessImplicitDefsID);
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +0000619
Andrew Trick8dd26252012-02-10 04:10:36 +0000620 // LiveVariables currently requires pure SSA form.
621 //
622 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
623 // LiveVariables can be removed completely, and LiveIntervals can be directly
624 // computed. (We still either need to regenerate kill flags after regalloc, or
625 // preferably fix the scavenger to not depend on them).
Bob Wilson3fb99a72012-07-02 19:48:37 +0000626 addPass(&LiveVariablesID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000627
628 // Add passes that move from transformed SSA into conventional SSA. This is a
629 // "copy coalescing" problem.
630 //
631 if (!EnableStrongPHIElim) {
632 // Edge splitting is smarter with machine loop info.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000633 addPass(&MachineLoopInfoID);
634 addPass(&PHIEliminationID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000635 }
Bob Wilson3fb99a72012-07-02 19:48:37 +0000636 addPass(&TwoAddressInstructionPassID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000637
Andrew Trick8dd26252012-02-10 04:10:36 +0000638 if (EnableStrongPHIElim)
Bob Wilson3fb99a72012-07-02 19:48:37 +0000639 addPass(&StrongPHIEliminationID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000640
Bob Wilson3fb99a72012-07-02 19:48:37 +0000641 addPass(&RegisterCoalescerID);
Andrew Trick8dd26252012-02-10 04:10:36 +0000642
643 // PreRA instruction scheduling.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000644 if (addPass(&MachineSchedulerID))
Andrew Trick17d35e52012-03-14 04:00:41 +0000645 printAndVerify("After Machine Scheduling");
Andrew Trick8dd26252012-02-10 04:10:36 +0000646
647 // Add the selected register allocation pass.
Bob Wilson564fbf62012-07-02 19:48:31 +0000648 addPass(RegAllocPass);
Jakob Stoklund Olesen34f5a2b2012-06-26 17:09:29 +0000649 printAndVerify("After Register Allocation, before rewriter");
650
651 // Allow targets to change the register assignments before rewriting.
652 if (addPreRewrite())
653 printAndVerify("After pre-rewrite passes");
Andrew Trickf7b96312012-02-09 00:40:55 +0000654
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000655 // Finally rewrite virtual registers.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000656 addPass(&VirtRegRewriterID);
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000657 printAndVerify("After Virtual Register Rewriter");
658
Andrew Trick746f24b2012-02-11 07:11:32 +0000659 // FinalizeRegAlloc is convenient until MachineInstrBundles is more mature,
660 // but eventually, all users of it should probably be moved to addPostRA and
661 // it can go away. Currently, it's the intended place for targets to run
662 // FinalizeMachineBundles, because passes other than MachineScheduling an
663 // RegAlloc itself may not be aware of bundles.
664 if (addFinalizeRegAlloc())
665 printAndVerify("After RegAlloc finalization");
666
Andrew Trickf7b96312012-02-09 00:40:55 +0000667 // Perform stack slot coloring and post-ra machine LICM.
Andrew Trick8dd26252012-02-10 04:10:36 +0000668 //
669 // FIXME: Re-enable coloring with register when it's capable of adding
670 // kill markers.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000671 addPass(&StackSlotColoringID);
Andrew Trick900d7b72012-02-15 07:57:03 +0000672
673 // Run post-ra machine LICM to hoist reloads / remats.
674 //
675 // FIXME: can this move into MachineLateOptimization?
Bob Wilson3fb99a72012-07-02 19:48:37 +0000676 addPass(&PostRAMachineLICMID);
Andrew Trick900d7b72012-02-15 07:57:03 +0000677
678 printAndVerify("After StackSlotColoring and postra Machine LICM");
Andrew Trickf7b96312012-02-09 00:40:55 +0000679}
680
681//===---------------------------------------------------------------------===//
682/// Post RegAlloc Pass Configuration
683//===---------------------------------------------------------------------===//
684
685/// Add passes that optimize machine instructions after register allocation.
686void TargetPassConfig::addMachineLateOptimization() {
687 // Branch folding must be run after regalloc and prolog/epilog insertion.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000688 if (addPass(&BranchFolderPassID))
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000689 printAndVerify("After BranchFolding");
Andrew Trickf7b96312012-02-09 00:40:55 +0000690
691 // Tail duplication.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000692 if (addPass(&TailDuplicateID))
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000693 printAndVerify("After TailDuplicate");
Andrew Trickf7b96312012-02-09 00:40:55 +0000694
695 // Copy propagation.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000696 if (addPass(&MachineCopyPropagationID))
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000697 printAndVerify("After copy propagation pass");
Andrew Trickf7b96312012-02-09 00:40:55 +0000698}
699
700/// Add standard basic block placement passes.
701void TargetPassConfig::addBlockPlacement() {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000702 AnalysisID PassID = 0;
Chandler Carruth9e67db42012-04-16 13:49:17 +0000703 if (!DisableBlockPlacement) {
704 // MachineBlockPlacement is a new pass which subsumes the functionality of
705 // CodPlacementOpt. The old code placement pass can be restored by
706 // disabling block placement, but eventually it will be removed.
Bob Wilson3fb99a72012-07-02 19:48:37 +0000707 PassID = addPass(&MachineBlockPlacementID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000708 } else {
Bob Wilson3fb99a72012-07-02 19:48:37 +0000709 PassID = addPass(&CodePlacementOptID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000710 }
Bob Wilson3fb99a72012-07-02 19:48:37 +0000711 if (PassID) {
Andrew Trick79bf2882012-02-15 03:21:51 +0000712 // Run a separate pass to collect block placement statistics.
713 if (EnableBlockPlacementStats)
Bob Wilson3fb99a72012-07-02 19:48:37 +0000714 addPass(&MachineBlockPlacementStatsID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000715
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000716 printAndVerify("After machine block placement.");
Andrew Trickf7b96312012-02-09 00:40:55 +0000717 }
718}