blob: 427e4ac327ff217a8b5097dfb51c180e76b91f6f [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"
Andrew Trickd5422652012-02-04 02:56:48 +000025#include "llvm/Assembly/PrintModulePass.h"
26#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
Andrew Trick74613342012-02-04 02:56:45 +000028#include "llvm/Support/ErrorHandling.h"
Jim Laskey13ec7022006-08-01 14:21:23 +000029
Chris Lattneraa4c91f2003-12-28 07:59:53 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Andrew Trickd5422652012-02-04 02:56:48 +000032static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden,
33 cl::desc("Disable Post Regalloc"));
34static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden,
35 cl::desc("Disable branch folding"));
36static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
37 cl::desc("Disable tail duplication"));
38static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
39 cl::desc("Disable pre-register allocation tail duplication"));
40static cl::opt<bool> EnableBlockPlacement("enable-block-placement",
41 cl::Hidden, cl::desc("Enable probability-driven block placement"));
42static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats",
43 cl::Hidden, cl::desc("Collect probability-driven block placement stats"));
44static cl::opt<bool> DisableCodePlace("disable-code-place", cl::Hidden,
45 cl::desc("Disable code placement"));
46static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
47 cl::desc("Disable Stack Slot Coloring"));
48static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden,
49 cl::desc("Disable Machine Dead Code Elimination"));
50static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
51 cl::desc("Disable Machine LICM"));
52static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden,
53 cl::desc("Disable Machine Common Subexpression Elimination"));
Andrew Trick8dd26252012-02-10 04:10:36 +000054static cl::opt<cl::boolOrDefault>
55OptimizeRegAlloc("optimize-regalloc", cl::Hidden,
56 cl::desc("Enable optimized register allocation compilation path."));
Andrew Trick746f24b2012-02-11 07:11:32 +000057static cl::opt<cl::boolOrDefault>
58EnableMachineSched("enable-misched", cl::Hidden,
Andrew Trick8dd26252012-02-10 04:10:36 +000059 cl::desc("Enable the machine instruction scheduling pass."));
60static cl::opt<bool> EnableStrongPHIElim("strong-phi-elim", cl::Hidden,
61 cl::desc("Use strong PHI elimination."));
Andrew Trickd5422652012-02-04 02:56:48 +000062static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
63 cl::Hidden,
64 cl::desc("Disable Machine LICM"));
65static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
66 cl::desc("Disable Machine Sinking"));
67static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
68 cl::desc("Disable Loop Strength Reduction Pass"));
69static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
70 cl::desc("Disable Codegen Prepare"));
71static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden,
72 cl::desc("Disable Copy Propagation pass"));
73static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
74 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
75static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
76 cl::desc("Print LLVM IR input to isel pass"));
77static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
78 cl::desc("Dump garbage collector data"));
79static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
80 cl::desc("Verify generated machine code"),
81 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
82
Andrew Trick746f24b2012-02-11 07:11:32 +000083// Allow Pass selection to be overriden by command line options.
84//
85// DefaultID is the default pass to run which may be NoPassID, or may be
86// overriden by the target.
87//
88// OptionalID is a pass that may be forcibly enabled by the user when the
89// default is NoPassID.
90char &enablePass(char &DefaultID, cl::boolOrDefault Override,
91 char *OptionalIDPtr = &NoPassID) {
92 switch (Override) {
93 case cl::BOU_UNSET:
94 return DefaultID;
95 case cl::BOU_TRUE:
96 if (&DefaultID != &NoPassID)
97 return DefaultID;
98 if (OptionalIDPtr == &NoPassID)
99 report_fatal_error("Target cannot enable pass");
100 return *OptionalIDPtr;
101 case cl::BOU_FALSE:
102 return NoPassID;
103 }
104 llvm_unreachable("Invalid command line option state");
105}
106
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000107//===---------------------------------------------------------------------===//
Andrew Trick74613342012-02-04 02:56:45 +0000108/// TargetPassConfig
109//===---------------------------------------------------------------------===//
110
111INITIALIZE_PASS(TargetPassConfig, "targetpassconfig",
112 "Target Pass Configuration", false, false)
113char TargetPassConfig::ID = 0;
114
Andrew Trick746f24b2012-02-11 07:11:32 +0000115static char NoPassIDAnchor = 0;
116char &llvm::NoPassID = NoPassIDAnchor;
117
Andrew Trick5e108ee2012-02-15 03:21:47 +0000118namespace llvm {
119class PassConfigImpl {
120public:
121 // List of passes explicitly substituted by this target. Normally this is
122 // empty, but it is a convenient way to suppress or replace specific passes
123 // that are part of a standard pass pipeline without overridding the entire
124 // pipeline. This mechanism allows target options to inherit a standard pass's
125 // user interface. For example, a target may disable a standard pass by
126 // default by substituting NoPass, and the user may still enable that standard
127 // pass with an explicit command line option.
128 DenseMap<AnalysisID,AnalysisID> TargetPasses;
129};
130} // namespace llvm
131
Andrew Trick74613342012-02-04 02:56:45 +0000132// Out of line virtual method.
Andrew Trick5e108ee2012-02-15 03:21:47 +0000133TargetPassConfig::~TargetPassConfig() {
134 delete Impl;
135}
Andrew Trick74613342012-02-04 02:56:45 +0000136
Andrew Trick61f1e3d2012-02-08 21:22:48 +0000137// Out of line constructor provides default values for pass options and
138// registers all common codegen passes.
Andrew Trick061efcf2012-02-04 02:56:59 +0000139TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm)
Andrew Trick5e108ee2012-02-15 03:21:47 +0000140 : ImmutablePass(ID), TM(tm), PM(pm), Impl(0), Initialized(false),
Andrew Trickffea03f2012-02-08 21:22:39 +0000141 DisableVerify(false),
142 EnableTailMerge(true) {
143
Andrew Trick5e108ee2012-02-15 03:21:47 +0000144 Impl = new PassConfigImpl();
145
Andrew Trick74613342012-02-04 02:56:45 +0000146 // Register all target independent codegen passes to activate their PassIDs,
147 // including this pass itself.
148 initializeCodeGen(*PassRegistry::getPassRegistry());
149}
150
151/// createPassConfig - Create a pass configuration object to be used by
152/// addPassToEmitX methods for generating a pipeline of CodeGen passes.
153///
154/// Targets may override this to extend TargetPassConfig.
Andrew Trick061efcf2012-02-04 02:56:59 +0000155TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
156 return new TargetPassConfig(this, PM);
Andrew Trick74613342012-02-04 02:56:45 +0000157}
158
159TargetPassConfig::TargetPassConfig()
160 : ImmutablePass(ID), PM(*(PassManagerBase*)0) {
161 llvm_unreachable("TargetPassConfig should not be constructed on-the-fly");
162}
163
Andrew Trickffea03f2012-02-08 21:22:39 +0000164// Helper to verify the analysis is really immutable.
165void TargetPassConfig::setOpt(bool &Opt, bool Val) {
166 assert(!Initialized && "PassConfig is immutable");
167 Opt = Val;
168}
169
Andrew Trick5e108ee2012-02-15 03:21:47 +0000170void TargetPassConfig::substitutePass(char &StandardID, char &TargetID) {
171 Impl->TargetPasses[&StandardID] = &TargetID;
172}
Andrew Trick746f24b2012-02-11 07:11:32 +0000173
Andrew Trick5e108ee2012-02-15 03:21:47 +0000174AnalysisID TargetPassConfig::getPassSubstitution(AnalysisID ID) const {
175 DenseMap<AnalysisID, AnalysisID>::const_iterator
176 I = Impl->TargetPasses.find(ID);
177 if (I == Impl->TargetPasses.end())
178 return ID;
179 return I->second;
180}
181
182/// Add a CodeGen pass at this point in the pipeline after checking for target
183/// and command line overrides.
184AnalysisID TargetPassConfig::addPass(char &ID) {
185 assert(!Initialized && "PassConfig is immutable");
186
187 AnalysisID FinalID = getPassSubstitution(&ID);
Andrew Trickebe18ef2012-02-08 21:22:34 +0000188 // FIXME: check user overrides
Andrew Trick5e108ee2012-02-15 03:21:47 +0000189 if (FinalID == &NoPassID)
190 return FinalID;
191
192 Pass *P = Pass::createPass(FinalID);
Andrew Trickebe18ef2012-02-08 21:22:34 +0000193 if (!P)
194 llvm_unreachable("Pass ID not registered");
195 PM.add(P);
Andrew Trick5e108ee2012-02-15 03:21:47 +0000196 return FinalID;
Andrew Trick061efcf2012-02-04 02:56:59 +0000197}
Andrew Trickd5422652012-02-04 02:56:48 +0000198
199void TargetPassConfig::printNoVerify(const char *Banner) const {
200 if (TM->shouldPrintMachineCode())
201 PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
202}
203
204void TargetPassConfig::printAndVerify(const char *Banner) const {
205 if (TM->shouldPrintMachineCode())
206 PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
207
208 if (VerifyMachineCode)
209 PM.add(createMachineVerifierPass(Banner));
210}
211
Andrew Trick061efcf2012-02-04 02:56:59 +0000212/// Add common target configurable passes that perform LLVM IR to IR transforms
213/// following machine independent optimization.
214void TargetPassConfig::addIRPasses() {
Andrew Trickd5422652012-02-04 02:56:48 +0000215 // Basic AliasAnalysis support.
216 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
217 // BasicAliasAnalysis wins if they disagree. This is intended to help
218 // support "obvious" type-punning idioms.
219 PM.add(createTypeBasedAliasAnalysisPass());
220 PM.add(createBasicAliasAnalysisPass());
221
222 // Before running any passes, run the verifier to determine if the input
223 // coming from the front-end and/or optimizer is valid.
224 if (!DisableVerify)
225 PM.add(createVerifierPass());
226
227 // Run loop strength reduction before anything else.
228 if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
229 PM.add(createLoopStrengthReducePass(getTargetLowering()));
230 if (PrintLSR)
231 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
232 }
233
234 PM.add(createGCLoweringPass());
235
236 // Make sure that no unreachable blocks are instruction selected.
237 PM.add(createUnreachableBlockEliminationPass());
Andrew Trick061efcf2012-02-04 02:56:59 +0000238}
Andrew Trickd5422652012-02-04 02:56:48 +0000239
Andrew Trick061efcf2012-02-04 02:56:59 +0000240/// Add common passes that perform LLVM IR to IR transforms in preparation for
241/// instruction selection.
242void TargetPassConfig::addISelPrepare() {
Andrew Trickd5422652012-02-04 02:56:48 +0000243 if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
244 PM.add(createCodeGenPreparePass(getTargetLowering()));
245
246 PM.add(createStackProtectorPass(getTargetLowering()));
247
248 addPreISel();
249
250 if (PrintISelInput)
251 PM.add(createPrintFunctionPass("\n\n"
252 "*** Final LLVM Code input to ISel ***\n",
253 &dbgs()));
254
255 // All passes which modify the LLVM IR are now complete; run the verifier
256 // to ensure that the IR is valid.
257 if (!DisableVerify)
258 PM.add(createVerifierPass());
Andrew Trick061efcf2012-02-04 02:56:59 +0000259}
Andrew Trickd5422652012-02-04 02:56:48 +0000260
Andrew Trickf7b96312012-02-09 00:40:55 +0000261/// Add the complete set of target-independent postISel code generator passes.
262///
263/// This can be read as the standard order of major LLVM CodeGen stages. Stages
264/// with nontrivial configuration or multiple passes are broken out below in
265/// add%Stage routines.
266///
267/// Any TargetPassConfig::addXX routine may be overriden by the Target. The
268/// addPre/Post methods with empty header implementations allow injecting
269/// target-specific fixups just before or after major stages. Additionally,
270/// targets have the flexibility to change pass order within a stage by
271/// overriding default implementation of add%Stage routines below. Each
272/// technique has maintainability tradeoffs because alternate pass orders are
273/// not well supported. addPre/Post works better if the target pass is easily
274/// tied to a common pass. But if it has subtle dependencies on multiple passes,
Andrew Trick06efdd22012-02-10 07:08:25 +0000275/// the target should override the stage instead.
Andrew Trickf7b96312012-02-09 00:40:55 +0000276///
277/// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
278/// before/after any target-independent pass. But it's currently overkill.
Andrew Trick061efcf2012-02-04 02:56:59 +0000279void TargetPassConfig::addMachinePasses() {
Andrew Trickd5422652012-02-04 02:56:48 +0000280 // Print the instruction selected machine code...
281 printAndVerify("After Instruction Selection");
282
283 // Expand pseudo-instructions emitted by ISel.
Andrew Trick1dd8c852012-02-08 21:23:13 +0000284 addPass(ExpandISelPseudosID);
Andrew Trickd5422652012-02-04 02:56:48 +0000285
Andrew Trickf7b96312012-02-09 00:40:55 +0000286 // Add passes that optimize machine instructions in SSA form.
Andrew Trickd5422652012-02-04 02:56:48 +0000287 if (getOptLevel() != CodeGenOpt::None) {
Andrew Trickf7b96312012-02-09 00:40:55 +0000288 addMachineSSAOptimization();
289 }
290 else {
291 // If the target requests it, assign local variables to stack slots relative
292 // to one another and simplify frame index references where possible.
293 addPass(LocalStackSlotAllocationID);
Andrew Trickd5422652012-02-04 02:56:48 +0000294 }
295
296 // Run pre-ra passes.
297 if (addPreRegAlloc())
298 printAndVerify("After PreRegAlloc passes");
299
Andrew Trickf7b96312012-02-09 00:40:55 +0000300 // Run register allocation and passes that are tightly coupled with it,
301 // including phi elimination and scheduling.
Andrew Trick8dd26252012-02-10 04:10:36 +0000302 if (getOptimizeRegAlloc())
303 addOptimizedRegAlloc(createRegAllocPass(true));
304 else
305 addFastRegAlloc(createRegAllocPass(false));
Andrew Trickd5422652012-02-04 02:56:48 +0000306
307 // Run post-ra passes.
308 if (addPostRegAlloc())
309 printAndVerify("After PostRegAlloc passes");
310
311 // Insert prolog/epilog code. Eliminate abstract frame index references...
Andrew Trick1dd8c852012-02-08 21:23:13 +0000312 addPass(PrologEpilogCodeInserterID);
Andrew Trickd5422652012-02-04 02:56:48 +0000313 printAndVerify("After PrologEpilogCodeInserter");
314
Andrew Trickf7b96312012-02-09 00:40:55 +0000315 /// Add passes that optimize machine instructions after register allocation.
316 if (getOptLevel() != CodeGenOpt::None)
317 addMachineLateOptimization();
Andrew Trickd5422652012-02-04 02:56:48 +0000318
319 // Expand pseudo instructions before second scheduling pass.
Andrew Trick1dd8c852012-02-08 21:23:13 +0000320 addPass(ExpandPostRAPseudosID);
Andrew Trickd5422652012-02-04 02:56:48 +0000321 printNoVerify("After ExpandPostRAPseudos");
322
323 // Run pre-sched2 passes.
324 if (addPreSched2())
325 printNoVerify("After PreSched2 passes");
326
327 // Second pass scheduler.
328 if (getOptLevel() != CodeGenOpt::None && !DisablePostRA) {
Andrew Trick1dd8c852012-02-08 21:23:13 +0000329 addPass(PostRASchedulerID);
Andrew Trickd5422652012-02-04 02:56:48 +0000330 printNoVerify("After PostRAScheduler");
331 }
332
Andrew Trickf7b96312012-02-09 00:40:55 +0000333 // GC
Andrew Trick1dd8c852012-02-08 21:23:13 +0000334 addPass(GCMachineCodeAnalysisID);
Andrew Trickd5422652012-02-04 02:56:48 +0000335 if (PrintGCInfo)
336 PM.add(createGCInfoPrinter(dbgs()));
337
Andrew Trickf7b96312012-02-09 00:40:55 +0000338 // Basic block placement.
339 if (getOptLevel() != CodeGenOpt::None && !DisableCodePlace)
340 addBlockPlacement();
Andrew Trickd5422652012-02-04 02:56:48 +0000341
342 if (addPreEmitPass())
343 printNoVerify("After PreEmit passes");
Andrew Trickd5422652012-02-04 02:56:48 +0000344}
345
Andrew Trickf7b96312012-02-09 00:40:55 +0000346/// Add passes that optimize machine instructions in SSA form.
347void TargetPassConfig::addMachineSSAOptimization() {
348 // Pre-ra tail duplication.
349 if (!DisableEarlyTailDup) {
350 addPass(TailDuplicateID);
351 printAndVerify("After Pre-RegAlloc TailDuplicate");
352 }
353
354 // Optimize PHIs before DCE: removing dead PHI cycles may make more
355 // instructions dead.
356 addPass(OptimizePHIsID);
357
358 // If the target requests it, assign local variables to stack slots relative
359 // to one another and simplify frame index references where possible.
360 addPass(LocalStackSlotAllocationID);
361
362 // With optimization, dead code should already be eliminated. However
363 // there is one known exception: lowered code for arguments that are only
364 // used by tail calls, where the tail calls reuse the incoming stack
365 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
366 if (!DisableMachineDCE)
367 addPass(DeadMachineInstructionElimID);
368 printAndVerify("After codegen DCE pass");
369
370 if (!DisableMachineLICM)
371 addPass(MachineLICMID);
372 if (!DisableMachineCSE)
373 addPass(MachineCSEID);
374 if (!DisableMachineSink)
375 addPass(MachineSinkingID);
376 printAndVerify("After Machine LICM, CSE and Sinking passes");
377
378 addPass(PeepholeOptimizerID);
379 printAndVerify("After codegen peephole optimization pass");
380}
381
Andrew Trick74613342012-02-04 02:56:45 +0000382//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000383/// Register Allocation Pass Configuration
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000384//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000385
Andrew Trick8dd26252012-02-10 04:10:36 +0000386bool TargetPassConfig::getOptimizeRegAlloc() const {
387 switch (OptimizeRegAlloc) {
388 case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
389 case cl::BOU_TRUE: return true;
390 case cl::BOU_FALSE: return false;
391 }
392 llvm_unreachable("Invalid optimize-regalloc state");
393}
394
Andrew Trickf7b96312012-02-09 00:40:55 +0000395/// RegisterRegAlloc's global Registry tracks allocator registration.
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000396MachinePassRegistry RegisterRegAlloc::Registry;
397
Andrew Trickf7b96312012-02-09 00:40:55 +0000398/// A dummy default pass factory indicates whether the register allocator is
399/// overridden on the command line.
Andrew Trick8dd26252012-02-10 04:10:36 +0000400static FunctionPass *useDefaultRegisterAllocator() { return 0; }
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000401static RegisterRegAlloc
402defaultRegAlloc("default",
403 "pick register allocator based on -O option",
Andrew Trick8dd26252012-02-10 04:10:36 +0000404 useDefaultRegisterAllocator);
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000405
Andrew Trickf7b96312012-02-09 00:40:55 +0000406/// -regalloc=... command line option.
Dan Gohman844731a2008-05-13 00:00:25 +0000407static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
408 RegisterPassParser<RegisterRegAlloc> >
409RegAlloc("regalloc",
Andrew Trick8dd26252012-02-10 04:10:36 +0000410 cl::init(&useDefaultRegisterAllocator),
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000411 cl::desc("Register allocator to use"));
Alkis Evlogimenos7237ece2003-10-02 16:57:49 +0000412
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000413
Andrew Trick8dd26252012-02-10 04:10:36 +0000414/// Instantiate the default register allocator pass for this target for either
415/// the optimized or unoptimized allocation path. This will be added to the pass
416/// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
417/// in the optimized case.
418///
419/// A target that uses the standard regalloc pass order for fast or optimized
420/// allocation may still override this for per-target regalloc
421/// selection. But -regalloc=... always takes precedence.
422FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
423 if (Optimized)
424 return createGreedyRegisterAllocator();
425 else
426 return createFastRegisterAllocator();
427}
428
429/// Find and instantiate the register allocation pass requested by this target
430/// at the current optimization level. Different register allocators are
431/// defined as separate passes because they may require different analysis.
432///
433/// This helper ensures that the regalloc= option is always available,
434/// even for targets that override the default allocator.
435///
436/// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
437/// this can be folded into addPass.
438FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
Jim Laskey9ff542f2006-08-01 18:29:48 +0000439 RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000440
Andrew Trick8dd26252012-02-10 04:10:36 +0000441 // Initialize the global default.
Jim Laskey13ec7022006-08-01 14:21:23 +0000442 if (!Ctor) {
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000443 Ctor = RegAlloc;
444 RegisterRegAlloc::setDefault(RegAlloc);
Jim Laskey13ec7022006-08-01 14:21:23 +0000445 }
Andrew Trick8dd26252012-02-10 04:10:36 +0000446 if (Ctor != useDefaultRegisterAllocator)
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000447 return Ctor();
448
Andrew Trick8dd26252012-02-10 04:10:36 +0000449 // With no -regalloc= override, ask the target for a regalloc pass.
450 return createTargetRegisterAllocator(Optimized);
451}
452
453/// Add the minimum set of target-independent passes that are required for
454/// register allocation. No coalescing or scheduling.
455void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
456 addPass(PHIEliminationID);
457 addPass(TwoAddressInstructionPassID);
458
459 PM.add(RegAllocPass);
460 printAndVerify("After Register Allocation");
Jim Laskey33a0a6d2006-07-27 20:05:00 +0000461}
Andrew Trickf7b96312012-02-09 00:40:55 +0000462
463/// Add standard target-independent passes that are tightly coupled with
Andrew Trick8dd26252012-02-10 04:10:36 +0000464/// optimized register allocation, including coalescing, machine instruction
465/// scheduling, and register allocation itself.
466void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
467 // LiveVariables currently requires pure SSA form.
468 //
469 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
470 // LiveVariables can be removed completely, and LiveIntervals can be directly
471 // computed. (We still either need to regenerate kill flags after regalloc, or
472 // preferably fix the scavenger to not depend on them).
473 addPass(LiveVariablesID);
474
475 // Add passes that move from transformed SSA into conventional SSA. This is a
476 // "copy coalescing" problem.
477 //
478 if (!EnableStrongPHIElim) {
479 // Edge splitting is smarter with machine loop info.
480 addPass(MachineLoopInfoID);
481 addPass(PHIEliminationID);
482 }
483 addPass(TwoAddressInstructionPassID);
484
485 // FIXME: Either remove this pass completely, or fix it so that it works on
486 // SSA form. We could modify LiveIntervals to be independent of this pass, But
487 // it would be even better to simply eliminate *all* IMPLICIT_DEFs before
488 // leaving SSA.
489 addPass(ProcessImplicitDefsID);
490
491 if (EnableStrongPHIElim)
492 addPass(StrongPHIEliminationID);
493
494 addPass(RegisterCoalescerID);
495
496 // PreRA instruction scheduling.
Andrew Trick746f24b2012-02-11 07:11:32 +0000497 addPass(enablePass(getSchedPass(), EnableMachineSched, &MachineSchedulerID));
Andrew Trick8dd26252012-02-10 04:10:36 +0000498
499 // Add the selected register allocation pass.
500 PM.add(RegAllocPass);
Andrew Trickf7b96312012-02-09 00:40:55 +0000501 printAndVerify("After Register Allocation");
502
Andrew Trick746f24b2012-02-11 07:11:32 +0000503 // FinalizeRegAlloc is convenient until MachineInstrBundles is more mature,
504 // but eventually, all users of it should probably be moved to addPostRA and
505 // it can go away. Currently, it's the intended place for targets to run
506 // FinalizeMachineBundles, because passes other than MachineScheduling an
507 // RegAlloc itself may not be aware of bundles.
508 if (addFinalizeRegAlloc())
509 printAndVerify("After RegAlloc finalization");
510
Andrew Trickf7b96312012-02-09 00:40:55 +0000511 // Perform stack slot coloring and post-ra machine LICM.
Andrew Trick8dd26252012-02-10 04:10:36 +0000512 //
513 // FIXME: Re-enable coloring with register when it's capable of adding
514 // kill markers.
515 if (!DisableSSC)
516 addPass(StackSlotColoringID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000517
Andrew Trick8dd26252012-02-10 04:10:36 +0000518 // Run post-ra machine LICM to hoist reloads / remats.
519 //
520 // FIXME: can this move into MachineLateOptimization?
521 if (!DisablePostRAMachineLICM)
522 addPass(MachineLICMID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000523
Andrew Trick8dd26252012-02-10 04:10:36 +0000524 printAndVerify("After StackSlotColoring and postra Machine LICM");
Andrew Trickf7b96312012-02-09 00:40:55 +0000525}
526
527//===---------------------------------------------------------------------===//
528/// Post RegAlloc Pass Configuration
529//===---------------------------------------------------------------------===//
530
531/// Add passes that optimize machine instructions after register allocation.
532void TargetPassConfig::addMachineLateOptimization() {
533 // Branch folding must be run after regalloc and prolog/epilog insertion.
534 if (!DisableBranchFold) {
535 addPass(BranchFolderPassID);
536 printNoVerify("After BranchFolding");
537 }
538
539 // Tail duplication.
540 if (!DisableTailDuplicate) {
541 addPass(TailDuplicateID);
542 printNoVerify("After TailDuplicate");
543 }
544
545 // Copy propagation.
546 if (!DisableCopyProp) {
547 addPass(MachineCopyPropagationID);
548 printNoVerify("After copy propagation pass");
549 }
550}
551
552/// Add standard basic block placement passes.
553void TargetPassConfig::addBlockPlacement() {
554 if (EnableBlockPlacement) {
555 // MachineBlockPlacement is an experimental pass which is disabled by
556 // default currently. Eventually it should subsume CodePlacementOpt, so
557 // when enabled, the other is disabled.
558 addPass(MachineBlockPlacementID);
559 printNoVerify("After MachineBlockPlacement");
560 } else {
561 addPass(CodePlacementOptID);
562 printNoVerify("After CodePlacementOpt");
563 }
564
565 // Run a separate pass to collect block placement statistics.
566 if (EnableBlockPlacementStats) {
567 addPass(MachineBlockPlacementStatsID);
568 printNoVerify("After MachineBlockPlacementStats");
569 }
570}