blob: 285d5df63460e2d974e75608b5c39bad72e6fe10 [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 Trick74613342012-02-04 02:56:45 +0000118// Out of line virtual method.
119TargetPassConfig::~TargetPassConfig() {}
120
Andrew Trick61f1e3d2012-02-08 21:22:48 +0000121// Out of line constructor provides default values for pass options and
122// registers all common codegen passes.
Andrew Trick061efcf2012-02-04 02:56:59 +0000123TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm)
Andrew Trickffea03f2012-02-08 21:22:39 +0000124 : ImmutablePass(ID), TM(tm), PM(pm), Initialized(false),
125 DisableVerify(false),
126 EnableTailMerge(true) {
127
Andrew Trick74613342012-02-04 02:56:45 +0000128 // Register all target independent codegen passes to activate their PassIDs,
129 // including this pass itself.
130 initializeCodeGen(*PassRegistry::getPassRegistry());
131}
132
133/// createPassConfig - Create a pass configuration object to be used by
134/// addPassToEmitX methods for generating a pipeline of CodeGen passes.
135///
136/// Targets may override this to extend TargetPassConfig.
Andrew Trick061efcf2012-02-04 02:56:59 +0000137TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
138 return new TargetPassConfig(this, PM);
Andrew Trick74613342012-02-04 02:56:45 +0000139}
140
141TargetPassConfig::TargetPassConfig()
142 : ImmutablePass(ID), PM(*(PassManagerBase*)0) {
143 llvm_unreachable("TargetPassConfig should not be constructed on-the-fly");
144}
145
Andrew Trickffea03f2012-02-08 21:22:39 +0000146// Helper to verify the analysis is really immutable.
147void TargetPassConfig::setOpt(bool &Opt, bool Val) {
148 assert(!Initialized && "PassConfig is immutable");
149 Opt = Val;
150}
151
Andrew Trickebe18ef2012-02-08 21:22:34 +0000152void TargetPassConfig::addPass(char &ID) {
Andrew Trick746f24b2012-02-11 07:11:32 +0000153 if (&ID == &NoPassID)
154 return;
155
Andrew Trickebe18ef2012-02-08 21:22:34 +0000156 // FIXME: check user overrides
157 Pass *P = Pass::createPass(ID);
158 if (!P)
159 llvm_unreachable("Pass ID not registered");
160 PM.add(P);
Andrew Trick061efcf2012-02-04 02:56:59 +0000161}
Andrew Trickd5422652012-02-04 02:56:48 +0000162
163void TargetPassConfig::printNoVerify(const char *Banner) const {
164 if (TM->shouldPrintMachineCode())
165 PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
166}
167
168void TargetPassConfig::printAndVerify(const char *Banner) const {
169 if (TM->shouldPrintMachineCode())
170 PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
171
172 if (VerifyMachineCode)
173 PM.add(createMachineVerifierPass(Banner));
174}
175
Andrew Trick061efcf2012-02-04 02:56:59 +0000176/// Add common target configurable passes that perform LLVM IR to IR transforms
177/// following machine independent optimization.
178void TargetPassConfig::addIRPasses() {
Andrew Trickd5422652012-02-04 02:56:48 +0000179 // Basic AliasAnalysis support.
180 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
181 // BasicAliasAnalysis wins if they disagree. This is intended to help
182 // support "obvious" type-punning idioms.
183 PM.add(createTypeBasedAliasAnalysisPass());
184 PM.add(createBasicAliasAnalysisPass());
185
186 // Before running any passes, run the verifier to determine if the input
187 // coming from the front-end and/or optimizer is valid.
188 if (!DisableVerify)
189 PM.add(createVerifierPass());
190
191 // Run loop strength reduction before anything else.
192 if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
193 PM.add(createLoopStrengthReducePass(getTargetLowering()));
194 if (PrintLSR)
195 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
196 }
197
198 PM.add(createGCLoweringPass());
199
200 // Make sure that no unreachable blocks are instruction selected.
201 PM.add(createUnreachableBlockEliminationPass());
Andrew Trick061efcf2012-02-04 02:56:59 +0000202}
Andrew Trickd5422652012-02-04 02:56:48 +0000203
Andrew Trick061efcf2012-02-04 02:56:59 +0000204/// Add common passes that perform LLVM IR to IR transforms in preparation for
205/// instruction selection.
206void TargetPassConfig::addISelPrepare() {
Andrew Trickd5422652012-02-04 02:56:48 +0000207 if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
208 PM.add(createCodeGenPreparePass(getTargetLowering()));
209
210 PM.add(createStackProtectorPass(getTargetLowering()));
211
212 addPreISel();
213
214 if (PrintISelInput)
215 PM.add(createPrintFunctionPass("\n\n"
216 "*** Final LLVM Code input to ISel ***\n",
217 &dbgs()));
218
219 // All passes which modify the LLVM IR are now complete; run the verifier
220 // to ensure that the IR is valid.
221 if (!DisableVerify)
222 PM.add(createVerifierPass());
Andrew Trick061efcf2012-02-04 02:56:59 +0000223}
Andrew Trickd5422652012-02-04 02:56:48 +0000224
Andrew Trickf7b96312012-02-09 00:40:55 +0000225/// Add the complete set of target-independent postISel code generator passes.
226///
227/// This can be read as the standard order of major LLVM CodeGen stages. Stages
228/// with nontrivial configuration or multiple passes are broken out below in
229/// add%Stage routines.
230///
231/// Any TargetPassConfig::addXX routine may be overriden by the Target. The
232/// addPre/Post methods with empty header implementations allow injecting
233/// target-specific fixups just before or after major stages. Additionally,
234/// targets have the flexibility to change pass order within a stage by
235/// overriding default implementation of add%Stage routines below. Each
236/// technique has maintainability tradeoffs because alternate pass orders are
237/// not well supported. addPre/Post works better if the target pass is easily
238/// tied to a common pass. But if it has subtle dependencies on multiple passes,
Andrew Trick06efdd22012-02-10 07:08:25 +0000239/// the target should override the stage instead.
Andrew Trickf7b96312012-02-09 00:40:55 +0000240///
241/// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
242/// before/after any target-independent pass. But it's currently overkill.
Andrew Trick061efcf2012-02-04 02:56:59 +0000243void TargetPassConfig::addMachinePasses() {
Andrew Trickd5422652012-02-04 02:56:48 +0000244 // Print the instruction selected machine code...
245 printAndVerify("After Instruction Selection");
246
247 // Expand pseudo-instructions emitted by ISel.
Andrew Trick1dd8c852012-02-08 21:23:13 +0000248 addPass(ExpandISelPseudosID);
Andrew Trickd5422652012-02-04 02:56:48 +0000249
Andrew Trickf7b96312012-02-09 00:40:55 +0000250 // Add passes that optimize machine instructions in SSA form.
Andrew Trickd5422652012-02-04 02:56:48 +0000251 if (getOptLevel() != CodeGenOpt::None) {
Andrew Trickf7b96312012-02-09 00:40:55 +0000252 addMachineSSAOptimization();
253 }
254 else {
255 // If the target requests it, assign local variables to stack slots relative
256 // to one another and simplify frame index references where possible.
257 addPass(LocalStackSlotAllocationID);
Andrew Trickd5422652012-02-04 02:56:48 +0000258 }
259
260 // Run pre-ra passes.
261 if (addPreRegAlloc())
262 printAndVerify("After PreRegAlloc passes");
263
Andrew Trickf7b96312012-02-09 00:40:55 +0000264 // Run register allocation and passes that are tightly coupled with it,
265 // including phi elimination and scheduling.
Andrew Trick8dd26252012-02-10 04:10:36 +0000266 if (getOptimizeRegAlloc())
267 addOptimizedRegAlloc(createRegAllocPass(true));
268 else
269 addFastRegAlloc(createRegAllocPass(false));
Andrew Trickd5422652012-02-04 02:56:48 +0000270
271 // Run post-ra passes.
272 if (addPostRegAlloc())
273 printAndVerify("After PostRegAlloc passes");
274
275 // Insert prolog/epilog code. Eliminate abstract frame index references...
Andrew Trick1dd8c852012-02-08 21:23:13 +0000276 addPass(PrologEpilogCodeInserterID);
Andrew Trickd5422652012-02-04 02:56:48 +0000277 printAndVerify("After PrologEpilogCodeInserter");
278
Andrew Trickf7b96312012-02-09 00:40:55 +0000279 /// Add passes that optimize machine instructions after register allocation.
280 if (getOptLevel() != CodeGenOpt::None)
281 addMachineLateOptimization();
Andrew Trickd5422652012-02-04 02:56:48 +0000282
283 // Expand pseudo instructions before second scheduling pass.
Andrew Trick1dd8c852012-02-08 21:23:13 +0000284 addPass(ExpandPostRAPseudosID);
Andrew Trickd5422652012-02-04 02:56:48 +0000285 printNoVerify("After ExpandPostRAPseudos");
286
287 // Run pre-sched2 passes.
288 if (addPreSched2())
289 printNoVerify("After PreSched2 passes");
290
291 // Second pass scheduler.
292 if (getOptLevel() != CodeGenOpt::None && !DisablePostRA) {
Andrew Trick1dd8c852012-02-08 21:23:13 +0000293 addPass(PostRASchedulerID);
Andrew Trickd5422652012-02-04 02:56:48 +0000294 printNoVerify("After PostRAScheduler");
295 }
296
Andrew Trickf7b96312012-02-09 00:40:55 +0000297 // GC
Andrew Trick1dd8c852012-02-08 21:23:13 +0000298 addPass(GCMachineCodeAnalysisID);
Andrew Trickd5422652012-02-04 02:56:48 +0000299 if (PrintGCInfo)
300 PM.add(createGCInfoPrinter(dbgs()));
301
Andrew Trickf7b96312012-02-09 00:40:55 +0000302 // Basic block placement.
303 if (getOptLevel() != CodeGenOpt::None && !DisableCodePlace)
304 addBlockPlacement();
Andrew Trickd5422652012-02-04 02:56:48 +0000305
306 if (addPreEmitPass())
307 printNoVerify("After PreEmit passes");
Andrew Trickd5422652012-02-04 02:56:48 +0000308}
309
Andrew Trickf7b96312012-02-09 00:40:55 +0000310/// Add passes that optimize machine instructions in SSA form.
311void TargetPassConfig::addMachineSSAOptimization() {
312 // Pre-ra tail duplication.
313 if (!DisableEarlyTailDup) {
314 addPass(TailDuplicateID);
315 printAndVerify("After Pre-RegAlloc TailDuplicate");
316 }
317
318 // Optimize PHIs before DCE: removing dead PHI cycles may make more
319 // instructions dead.
320 addPass(OptimizePHIsID);
321
322 // If the target requests it, assign local variables to stack slots relative
323 // to one another and simplify frame index references where possible.
324 addPass(LocalStackSlotAllocationID);
325
326 // With optimization, dead code should already be eliminated. However
327 // there is one known exception: lowered code for arguments that are only
328 // used by tail calls, where the tail calls reuse the incoming stack
329 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
330 if (!DisableMachineDCE)
331 addPass(DeadMachineInstructionElimID);
332 printAndVerify("After codegen DCE pass");
333
334 if (!DisableMachineLICM)
335 addPass(MachineLICMID);
336 if (!DisableMachineCSE)
337 addPass(MachineCSEID);
338 if (!DisableMachineSink)
339 addPass(MachineSinkingID);
340 printAndVerify("After Machine LICM, CSE and Sinking passes");
341
342 addPass(PeepholeOptimizerID);
343 printAndVerify("After codegen peephole optimization pass");
344}
345
Andrew Trick74613342012-02-04 02:56:45 +0000346//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000347/// Register Allocation Pass Configuration
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000348//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000349
Andrew Trick8dd26252012-02-10 04:10:36 +0000350bool TargetPassConfig::getOptimizeRegAlloc() const {
351 switch (OptimizeRegAlloc) {
352 case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
353 case cl::BOU_TRUE: return true;
354 case cl::BOU_FALSE: return false;
355 }
356 llvm_unreachable("Invalid optimize-regalloc state");
357}
358
Andrew Trickf7b96312012-02-09 00:40:55 +0000359/// RegisterRegAlloc's global Registry tracks allocator registration.
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000360MachinePassRegistry RegisterRegAlloc::Registry;
361
Andrew Trickf7b96312012-02-09 00:40:55 +0000362/// A dummy default pass factory indicates whether the register allocator is
363/// overridden on the command line.
Andrew Trick8dd26252012-02-10 04:10:36 +0000364static FunctionPass *useDefaultRegisterAllocator() { return 0; }
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000365static RegisterRegAlloc
366defaultRegAlloc("default",
367 "pick register allocator based on -O option",
Andrew Trick8dd26252012-02-10 04:10:36 +0000368 useDefaultRegisterAllocator);
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000369
Andrew Trickf7b96312012-02-09 00:40:55 +0000370/// -regalloc=... command line option.
Dan Gohman844731a2008-05-13 00:00:25 +0000371static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
372 RegisterPassParser<RegisterRegAlloc> >
373RegAlloc("regalloc",
Andrew Trick8dd26252012-02-10 04:10:36 +0000374 cl::init(&useDefaultRegisterAllocator),
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000375 cl::desc("Register allocator to use"));
Alkis Evlogimenos7237ece2003-10-02 16:57:49 +0000376
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000377
Andrew Trick8dd26252012-02-10 04:10:36 +0000378/// Instantiate the default register allocator pass for this target for either
379/// the optimized or unoptimized allocation path. This will be added to the pass
380/// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
381/// in the optimized case.
382///
383/// A target that uses the standard regalloc pass order for fast or optimized
384/// allocation may still override this for per-target regalloc
385/// selection. But -regalloc=... always takes precedence.
386FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
387 if (Optimized)
388 return createGreedyRegisterAllocator();
389 else
390 return createFastRegisterAllocator();
391}
392
393/// Find and instantiate the register allocation pass requested by this target
394/// at the current optimization level. Different register allocators are
395/// defined as separate passes because they may require different analysis.
396///
397/// This helper ensures that the regalloc= option is always available,
398/// even for targets that override the default allocator.
399///
400/// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
401/// this can be folded into addPass.
402FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
Jim Laskey9ff542f2006-08-01 18:29:48 +0000403 RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000404
Andrew Trick8dd26252012-02-10 04:10:36 +0000405 // Initialize the global default.
Jim Laskey13ec7022006-08-01 14:21:23 +0000406 if (!Ctor) {
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000407 Ctor = RegAlloc;
408 RegisterRegAlloc::setDefault(RegAlloc);
Jim Laskey13ec7022006-08-01 14:21:23 +0000409 }
Andrew Trick8dd26252012-02-10 04:10:36 +0000410 if (Ctor != useDefaultRegisterAllocator)
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000411 return Ctor();
412
Andrew Trick8dd26252012-02-10 04:10:36 +0000413 // With no -regalloc= override, ask the target for a regalloc pass.
414 return createTargetRegisterAllocator(Optimized);
415}
416
417/// Add the minimum set of target-independent passes that are required for
418/// register allocation. No coalescing or scheduling.
419void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
420 addPass(PHIEliminationID);
421 addPass(TwoAddressInstructionPassID);
422
423 PM.add(RegAllocPass);
424 printAndVerify("After Register Allocation");
Jim Laskey33a0a6d2006-07-27 20:05:00 +0000425}
Andrew Trickf7b96312012-02-09 00:40:55 +0000426
427/// Add standard target-independent passes that are tightly coupled with
Andrew Trick8dd26252012-02-10 04:10:36 +0000428/// optimized register allocation, including coalescing, machine instruction
429/// scheduling, and register allocation itself.
430void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
431 // LiveVariables currently requires pure SSA form.
432 //
433 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
434 // LiveVariables can be removed completely, and LiveIntervals can be directly
435 // computed. (We still either need to regenerate kill flags after regalloc, or
436 // preferably fix the scavenger to not depend on them).
437 addPass(LiveVariablesID);
438
439 // Add passes that move from transformed SSA into conventional SSA. This is a
440 // "copy coalescing" problem.
441 //
442 if (!EnableStrongPHIElim) {
443 // Edge splitting is smarter with machine loop info.
444 addPass(MachineLoopInfoID);
445 addPass(PHIEliminationID);
446 }
447 addPass(TwoAddressInstructionPassID);
448
449 // FIXME: Either remove this pass completely, or fix it so that it works on
450 // SSA form. We could modify LiveIntervals to be independent of this pass, But
451 // it would be even better to simply eliminate *all* IMPLICIT_DEFs before
452 // leaving SSA.
453 addPass(ProcessImplicitDefsID);
454
455 if (EnableStrongPHIElim)
456 addPass(StrongPHIEliminationID);
457
458 addPass(RegisterCoalescerID);
459
460 // PreRA instruction scheduling.
Andrew Trick746f24b2012-02-11 07:11:32 +0000461 addPass(enablePass(getSchedPass(), EnableMachineSched, &MachineSchedulerID));
Andrew Trick8dd26252012-02-10 04:10:36 +0000462
463 // Add the selected register allocation pass.
464 PM.add(RegAllocPass);
Andrew Trickf7b96312012-02-09 00:40:55 +0000465 printAndVerify("After Register Allocation");
466
Andrew Trick746f24b2012-02-11 07:11:32 +0000467 // FinalizeRegAlloc is convenient until MachineInstrBundles is more mature,
468 // but eventually, all users of it should probably be moved to addPostRA and
469 // it can go away. Currently, it's the intended place for targets to run
470 // FinalizeMachineBundles, because passes other than MachineScheduling an
471 // RegAlloc itself may not be aware of bundles.
472 if (addFinalizeRegAlloc())
473 printAndVerify("After RegAlloc finalization");
474
Andrew Trickf7b96312012-02-09 00:40:55 +0000475 // Perform stack slot coloring and post-ra machine LICM.
Andrew Trick8dd26252012-02-10 04:10:36 +0000476 //
477 // FIXME: Re-enable coloring with register when it's capable of adding
478 // kill markers.
479 if (!DisableSSC)
480 addPass(StackSlotColoringID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000481
Andrew Trick8dd26252012-02-10 04:10:36 +0000482 // Run post-ra machine LICM to hoist reloads / remats.
483 //
484 // FIXME: can this move into MachineLateOptimization?
485 if (!DisablePostRAMachineLICM)
486 addPass(MachineLICMID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000487
Andrew Trick8dd26252012-02-10 04:10:36 +0000488 printAndVerify("After StackSlotColoring and postra Machine LICM");
Andrew Trickf7b96312012-02-09 00:40:55 +0000489}
490
491//===---------------------------------------------------------------------===//
492/// Post RegAlloc Pass Configuration
493//===---------------------------------------------------------------------===//
494
495/// Add passes that optimize machine instructions after register allocation.
496void TargetPassConfig::addMachineLateOptimization() {
497 // Branch folding must be run after regalloc and prolog/epilog insertion.
498 if (!DisableBranchFold) {
499 addPass(BranchFolderPassID);
500 printNoVerify("After BranchFolding");
501 }
502
503 // Tail duplication.
504 if (!DisableTailDuplicate) {
505 addPass(TailDuplicateID);
506 printNoVerify("After TailDuplicate");
507 }
508
509 // Copy propagation.
510 if (!DisableCopyProp) {
511 addPass(MachineCopyPropagationID);
512 printNoVerify("After copy propagation pass");
513 }
514}
515
516/// Add standard basic block placement passes.
517void TargetPassConfig::addBlockPlacement() {
518 if (EnableBlockPlacement) {
519 // MachineBlockPlacement is an experimental pass which is disabled by
520 // default currently. Eventually it should subsume CodePlacementOpt, so
521 // when enabled, the other is disabled.
522 addPass(MachineBlockPlacementID);
523 printNoVerify("After MachineBlockPlacement");
524 } else {
525 addPass(CodePlacementOptID);
526 printNoVerify("After CodePlacementOpt");
527 }
528
529 // Run a separate pass to collect block placement statistics.
530 if (EnableBlockPlacementStats) {
531 addPass(MachineBlockPlacementStatsID);
532 printNoVerify("After MachineBlockPlacementStats");
533 }
534}