blob: 53d1fcf7377ad524e15ef4b44c40d9d80af0a3c4 [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,
Evan Cheng01b623c2012-02-20 23:28:17 +000072 cl::desc("Disable Copy Propagation pass"));
Andrew Trickd5422652012-02-04 02:56:48 +000073static 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 Trick79bf2882012-02-15 03:21:51 +000083/// Allow standard passes to be disabled by command line options. This supports
84/// simple binary flags that either suppress the pass or do nothing.
85/// i.e. -disable-mypass=false has no effect.
86/// These should be converted to boolOrDefault in order to use applyOverride.
87static AnalysisID applyDisable(AnalysisID ID, bool Override) {
88 if (Override)
89 return &NoPassID;
90 return ID;
91}
92
93/// Allow Pass selection to be overriden by command line options. This supports
94/// flags with ternary conditions. TargetID is passed through by default. The
95/// pass is suppressed when the option is false. When the option is true, the
96/// StandardID is selected if the target provides no default.
97static AnalysisID applyOverride(AnalysisID TargetID, cl::boolOrDefault Override,
98 AnalysisID StandardID) {
Andrew Trick746f24b2012-02-11 07:11:32 +000099 switch (Override) {
100 case cl::BOU_UNSET:
Andrew Trick79bf2882012-02-15 03:21:51 +0000101 return TargetID;
Andrew Trick746f24b2012-02-11 07:11:32 +0000102 case cl::BOU_TRUE:
Andrew Trick79bf2882012-02-15 03:21:51 +0000103 if (TargetID != &NoPassID)
104 return TargetID;
105 if (StandardID == &NoPassID)
Andrew Trick746f24b2012-02-11 07:11:32 +0000106 report_fatal_error("Target cannot enable pass");
Andrew Trick79bf2882012-02-15 03:21:51 +0000107 return StandardID;
Andrew Trick746f24b2012-02-11 07:11:32 +0000108 case cl::BOU_FALSE:
Andrew Trick79bf2882012-02-15 03:21:51 +0000109 return &NoPassID;
Andrew Trick746f24b2012-02-11 07:11:32 +0000110 }
111 llvm_unreachable("Invalid command line option state");
112}
113
Andrew Trick79bf2882012-02-15 03:21:51 +0000114/// Allow standard passes to be disabled by the command line, regardless of who
115/// is adding the pass.
116///
117/// StandardID is the pass identified in the standard pass pipeline and provided
118/// to addPass(). It may be a target-specific ID in the case that the target
119/// directly adds its own pass, but in that case we harmlessly fall through.
120///
121/// TargetID is the pass that the target has configured to override StandardID.
122///
123/// StandardID may be a pseudo ID. In that case TargetID is the name of the real
124/// pass to run. This allows multiple options to control a single pass depending
125/// on where in the pipeline that pass is added.
126static AnalysisID overridePass(AnalysisID StandardID, AnalysisID TargetID) {
127 if (StandardID == &PostRASchedulerID)
128 return applyDisable(TargetID, DisablePostRA);
129
130 if (StandardID == &BranchFolderPassID)
131 return applyDisable(TargetID, DisableBranchFold);
132
133 if (StandardID == &TailDuplicateID)
134 return applyDisable(TargetID, DisableTailDuplicate);
135
136 if (StandardID == &TargetPassConfig::EarlyTailDuplicateID)
137 return applyDisable(TargetID, DisableEarlyTailDup);
138
139 if (StandardID == &MachineBlockPlacementID)
140 return applyDisable(TargetID, DisableCodePlace);
141
142 if (StandardID == &CodePlacementOptID)
143 return applyDisable(TargetID, DisableCodePlace);
144
145 if (StandardID == &StackSlotColoringID)
146 return applyDisable(TargetID, DisableSSC);
147
148 if (StandardID == &DeadMachineInstructionElimID)
149 return applyDisable(TargetID, DisableMachineDCE);
150
151 if (StandardID == &MachineLICMID)
152 return applyDisable(TargetID, DisableMachineLICM);
153
154 if (StandardID == &MachineCSEID)
155 return applyDisable(TargetID, DisableMachineCSE);
156
157 if (StandardID == &MachineSchedulerID)
158 return applyOverride(TargetID, EnableMachineSched, StandardID);
159
160 if (StandardID == &TargetPassConfig::PostRAMachineLICMID)
161 return applyDisable(TargetID, DisablePostRAMachineLICM);
162
163 if (StandardID == &MachineSinkingID)
164 return applyDisable(TargetID, DisableMachineSink);
165
166 if (StandardID == &MachineCopyPropagationID)
167 return applyDisable(TargetID, DisableCopyProp);
168
169 return TargetID;
170}
171
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000172//===---------------------------------------------------------------------===//
Andrew Trick74613342012-02-04 02:56:45 +0000173/// TargetPassConfig
174//===---------------------------------------------------------------------===//
175
176INITIALIZE_PASS(TargetPassConfig, "targetpassconfig",
177 "Target Pass Configuration", false, false)
178char TargetPassConfig::ID = 0;
179
Andrew Trick746f24b2012-02-11 07:11:32 +0000180static char NoPassIDAnchor = 0;
181char &llvm::NoPassID = NoPassIDAnchor;
182
Andrew Trick79bf2882012-02-15 03:21:51 +0000183// Pseudo Pass IDs.
184char TargetPassConfig::EarlyTailDuplicateID = 0;
185char TargetPassConfig::PostRAMachineLICMID = 0;
186
Andrew Trick5e108ee2012-02-15 03:21:47 +0000187namespace llvm {
188class PassConfigImpl {
189public:
190 // List of passes explicitly substituted by this target. Normally this is
191 // empty, but it is a convenient way to suppress or replace specific passes
192 // that are part of a standard pass pipeline without overridding the entire
193 // pipeline. This mechanism allows target options to inherit a standard pass's
194 // user interface. For example, a target may disable a standard pass by
195 // default by substituting NoPass, and the user may still enable that standard
196 // pass with an explicit command line option.
197 DenseMap<AnalysisID,AnalysisID> TargetPasses;
198};
199} // namespace llvm
200
Andrew Trick74613342012-02-04 02:56:45 +0000201// Out of line virtual method.
Andrew Trick5e108ee2012-02-15 03:21:47 +0000202TargetPassConfig::~TargetPassConfig() {
203 delete Impl;
204}
Andrew Trick74613342012-02-04 02:56:45 +0000205
Andrew Trick61f1e3d2012-02-08 21:22:48 +0000206// Out of line constructor provides default values for pass options and
207// registers all common codegen passes.
Andrew Trick061efcf2012-02-04 02:56:59 +0000208TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm)
Andrew Trick5e108ee2012-02-15 03:21:47 +0000209 : ImmutablePass(ID), TM(tm), PM(pm), Impl(0), Initialized(false),
Andrew Trickffea03f2012-02-08 21:22:39 +0000210 DisableVerify(false),
211 EnableTailMerge(true) {
212
Andrew Trick5e108ee2012-02-15 03:21:47 +0000213 Impl = new PassConfigImpl();
214
Andrew Trick74613342012-02-04 02:56:45 +0000215 // Register all target independent codegen passes to activate their PassIDs,
216 // including this pass itself.
217 initializeCodeGen(*PassRegistry::getPassRegistry());
Andrew Trick79bf2882012-02-15 03:21:51 +0000218
219 // Substitute Pseudo Pass IDs for real ones.
220 substitutePass(EarlyTailDuplicateID, TailDuplicateID);
221 substitutePass(PostRAMachineLICMID, MachineLICMID);
222
223 // Temporarily disable experimental passes.
224 substitutePass(MachineSchedulerID, NoPassID);
Andrew Trick74613342012-02-04 02:56:45 +0000225}
226
227/// createPassConfig - Create a pass configuration object to be used by
228/// addPassToEmitX methods for generating a pipeline of CodeGen passes.
229///
230/// Targets may override this to extend TargetPassConfig.
Andrew Trick061efcf2012-02-04 02:56:59 +0000231TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
232 return new TargetPassConfig(this, PM);
Andrew Trick74613342012-02-04 02:56:45 +0000233}
234
235TargetPassConfig::TargetPassConfig()
236 : ImmutablePass(ID), PM(*(PassManagerBase*)0) {
237 llvm_unreachable("TargetPassConfig should not be constructed on-the-fly");
238}
239
Andrew Trickffea03f2012-02-08 21:22:39 +0000240// Helper to verify the analysis is really immutable.
241void TargetPassConfig::setOpt(bool &Opt, bool Val) {
242 assert(!Initialized && "PassConfig is immutable");
243 Opt = Val;
244}
245
Andrew Trick5e108ee2012-02-15 03:21:47 +0000246void TargetPassConfig::substitutePass(char &StandardID, char &TargetID) {
247 Impl->TargetPasses[&StandardID] = &TargetID;
248}
Andrew Trick746f24b2012-02-11 07:11:32 +0000249
Andrew Trick5e108ee2012-02-15 03:21:47 +0000250AnalysisID TargetPassConfig::getPassSubstitution(AnalysisID ID) const {
251 DenseMap<AnalysisID, AnalysisID>::const_iterator
252 I = Impl->TargetPasses.find(ID);
253 if (I == Impl->TargetPasses.end())
254 return ID;
255 return I->second;
256}
257
258/// Add a CodeGen pass at this point in the pipeline after checking for target
259/// and command line overrides.
260AnalysisID TargetPassConfig::addPass(char &ID) {
261 assert(!Initialized && "PassConfig is immutable");
262
Andrew Trick79bf2882012-02-15 03:21:51 +0000263 AnalysisID TargetID = getPassSubstitution(&ID);
264 AnalysisID FinalID = overridePass(&ID, TargetID);
Andrew Trick5e108ee2012-02-15 03:21:47 +0000265 if (FinalID == &NoPassID)
266 return FinalID;
267
268 Pass *P = Pass::createPass(FinalID);
Andrew Trickebe18ef2012-02-08 21:22:34 +0000269 if (!P)
270 llvm_unreachable("Pass ID not registered");
271 PM.add(P);
Andrew Trick5e108ee2012-02-15 03:21:47 +0000272 return FinalID;
Andrew Trick061efcf2012-02-04 02:56:59 +0000273}
Andrew Trickd5422652012-02-04 02:56:48 +0000274
Andrew Trickd5422652012-02-04 02:56:48 +0000275void TargetPassConfig::printAndVerify(const char *Banner) const {
276 if (TM->shouldPrintMachineCode())
277 PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
278
279 if (VerifyMachineCode)
280 PM.add(createMachineVerifierPass(Banner));
281}
282
Andrew Trick061efcf2012-02-04 02:56:59 +0000283/// Add common target configurable passes that perform LLVM IR to IR transforms
284/// following machine independent optimization.
285void TargetPassConfig::addIRPasses() {
Andrew Trickd5422652012-02-04 02:56:48 +0000286 // Basic AliasAnalysis support.
287 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
288 // BasicAliasAnalysis wins if they disagree. This is intended to help
289 // support "obvious" type-punning idioms.
290 PM.add(createTypeBasedAliasAnalysisPass());
291 PM.add(createBasicAliasAnalysisPass());
292
293 // Before running any passes, run the verifier to determine if the input
294 // coming from the front-end and/or optimizer is valid.
295 if (!DisableVerify)
296 PM.add(createVerifierPass());
297
298 // Run loop strength reduction before anything else.
299 if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
300 PM.add(createLoopStrengthReducePass(getTargetLowering()));
301 if (PrintLSR)
302 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
303 }
304
305 PM.add(createGCLoweringPass());
306
307 // Make sure that no unreachable blocks are instruction selected.
308 PM.add(createUnreachableBlockEliminationPass());
Andrew Trick061efcf2012-02-04 02:56:59 +0000309}
Andrew Trickd5422652012-02-04 02:56:48 +0000310
Andrew Trick061efcf2012-02-04 02:56:59 +0000311/// Add common passes that perform LLVM IR to IR transforms in preparation for
312/// instruction selection.
313void TargetPassConfig::addISelPrepare() {
Andrew Trickd5422652012-02-04 02:56:48 +0000314 if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
315 PM.add(createCodeGenPreparePass(getTargetLowering()));
316
317 PM.add(createStackProtectorPass(getTargetLowering()));
318
319 addPreISel();
320
321 if (PrintISelInput)
322 PM.add(createPrintFunctionPass("\n\n"
323 "*** Final LLVM Code input to ISel ***\n",
324 &dbgs()));
325
326 // All passes which modify the LLVM IR are now complete; run the verifier
327 // to ensure that the IR is valid.
328 if (!DisableVerify)
329 PM.add(createVerifierPass());
Andrew Trick061efcf2012-02-04 02:56:59 +0000330}
Andrew Trickd5422652012-02-04 02:56:48 +0000331
Andrew Trickf7b96312012-02-09 00:40:55 +0000332/// Add the complete set of target-independent postISel code generator passes.
333///
334/// This can be read as the standard order of major LLVM CodeGen stages. Stages
335/// with nontrivial configuration or multiple passes are broken out below in
336/// add%Stage routines.
337///
338/// Any TargetPassConfig::addXX routine may be overriden by the Target. The
339/// addPre/Post methods with empty header implementations allow injecting
340/// target-specific fixups just before or after major stages. Additionally,
341/// targets have the flexibility to change pass order within a stage by
342/// overriding default implementation of add%Stage routines below. Each
343/// technique has maintainability tradeoffs because alternate pass orders are
344/// not well supported. addPre/Post works better if the target pass is easily
345/// tied to a common pass. But if it has subtle dependencies on multiple passes,
Andrew Trick06efdd22012-02-10 07:08:25 +0000346/// the target should override the stage instead.
Andrew Trickf7b96312012-02-09 00:40:55 +0000347///
348/// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
349/// before/after any target-independent pass. But it's currently overkill.
Andrew Trick061efcf2012-02-04 02:56:59 +0000350void TargetPassConfig::addMachinePasses() {
Andrew Trickd5422652012-02-04 02:56:48 +0000351 // Print the instruction selected machine code...
352 printAndVerify("After Instruction Selection");
353
354 // Expand pseudo-instructions emitted by ISel.
Andrew Trick1dd8c852012-02-08 21:23:13 +0000355 addPass(ExpandISelPseudosID);
Andrew Trickd5422652012-02-04 02:56:48 +0000356
Andrew Trickf7b96312012-02-09 00:40:55 +0000357 // Add passes that optimize machine instructions in SSA form.
Andrew Trickd5422652012-02-04 02:56:48 +0000358 if (getOptLevel() != CodeGenOpt::None) {
Andrew Trickf7b96312012-02-09 00:40:55 +0000359 addMachineSSAOptimization();
360 }
361 else {
362 // If the target requests it, assign local variables to stack slots relative
363 // to one another and simplify frame index references where possible.
364 addPass(LocalStackSlotAllocationID);
Andrew Trickd5422652012-02-04 02:56:48 +0000365 }
366
367 // Run pre-ra passes.
368 if (addPreRegAlloc())
369 printAndVerify("After PreRegAlloc passes");
370
Andrew Trickf7b96312012-02-09 00:40:55 +0000371 // Run register allocation and passes that are tightly coupled with it,
372 // including phi elimination and scheduling.
Andrew Trick8dd26252012-02-10 04:10:36 +0000373 if (getOptimizeRegAlloc())
374 addOptimizedRegAlloc(createRegAllocPass(true));
375 else
376 addFastRegAlloc(createRegAllocPass(false));
Andrew Trickd5422652012-02-04 02:56:48 +0000377
378 // Run post-ra passes.
379 if (addPostRegAlloc())
380 printAndVerify("After PostRegAlloc passes");
381
382 // Insert prolog/epilog code. Eliminate abstract frame index references...
Andrew Trick1dd8c852012-02-08 21:23:13 +0000383 addPass(PrologEpilogCodeInserterID);
Andrew Trickd5422652012-02-04 02:56:48 +0000384 printAndVerify("After PrologEpilogCodeInserter");
385
Andrew Trickf7b96312012-02-09 00:40:55 +0000386 /// Add passes that optimize machine instructions after register allocation.
387 if (getOptLevel() != CodeGenOpt::None)
388 addMachineLateOptimization();
Andrew Trickd5422652012-02-04 02:56:48 +0000389
390 // Expand pseudo instructions before second scheduling pass.
Andrew Trick1dd8c852012-02-08 21:23:13 +0000391 addPass(ExpandPostRAPseudosID);
Jakob Stoklund Olesen2ef5bf62012-03-28 20:49:30 +0000392 printAndVerify("After ExpandPostRAPseudos");
Andrew Trickd5422652012-02-04 02:56:48 +0000393
394 // Run pre-sched2 passes.
395 if (addPreSched2())
Jakob Stoklund Olesen78811662012-03-28 23:31:15 +0000396 printAndVerify("After PreSched2 passes");
Andrew Trickd5422652012-02-04 02:56:48 +0000397
398 // Second pass scheduler.
Andrew Trick79bf2882012-02-15 03:21:51 +0000399 if (getOptLevel() != CodeGenOpt::None) {
Andrew Trick1dd8c852012-02-08 21:23:13 +0000400 addPass(PostRASchedulerID);
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000401 printAndVerify("After PostRAScheduler");
Andrew Trickd5422652012-02-04 02:56:48 +0000402 }
403
Andrew Trickf7b96312012-02-09 00:40:55 +0000404 // GC
Andrew Trick1dd8c852012-02-08 21:23:13 +0000405 addPass(GCMachineCodeAnalysisID);
Andrew Trickd5422652012-02-04 02:56:48 +0000406 if (PrintGCInfo)
407 PM.add(createGCInfoPrinter(dbgs()));
408
Andrew Trickf7b96312012-02-09 00:40:55 +0000409 // Basic block placement.
Andrew Trick79bf2882012-02-15 03:21:51 +0000410 if (getOptLevel() != CodeGenOpt::None)
Andrew Trickf7b96312012-02-09 00:40:55 +0000411 addBlockPlacement();
Andrew Trickd5422652012-02-04 02:56:48 +0000412
413 if (addPreEmitPass())
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000414 printAndVerify("After PreEmit passes");
Andrew Trickd5422652012-02-04 02:56:48 +0000415}
416
Andrew Trickf7b96312012-02-09 00:40:55 +0000417/// Add passes that optimize machine instructions in SSA form.
418void TargetPassConfig::addMachineSSAOptimization() {
419 // Pre-ra tail duplication.
Andrew Trick79bf2882012-02-15 03:21:51 +0000420 if (addPass(EarlyTailDuplicateID) != &NoPassID)
Andrew Trickf7b96312012-02-09 00:40:55 +0000421 printAndVerify("After Pre-RegAlloc TailDuplicate");
Andrew Trickf7b96312012-02-09 00:40:55 +0000422
423 // Optimize PHIs before DCE: removing dead PHI cycles may make more
424 // instructions dead.
425 addPass(OptimizePHIsID);
426
427 // If the target requests it, assign local variables to stack slots relative
428 // to one another and simplify frame index references where possible.
429 addPass(LocalStackSlotAllocationID);
430
431 // With optimization, dead code should already be eliminated. However
432 // there is one known exception: lowered code for arguments that are only
433 // used by tail calls, where the tail calls reuse the incoming stack
434 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
Andrew Trick79bf2882012-02-15 03:21:51 +0000435 addPass(DeadMachineInstructionElimID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000436 printAndVerify("After codegen DCE pass");
437
Andrew Trick79bf2882012-02-15 03:21:51 +0000438 addPass(MachineLICMID);
439 addPass(MachineCSEID);
440 addPass(MachineSinkingID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000441 printAndVerify("After Machine LICM, CSE and Sinking passes");
442
443 addPass(PeepholeOptimizerID);
444 printAndVerify("After codegen peephole optimization pass");
445}
446
Andrew Trick74613342012-02-04 02:56:45 +0000447//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000448/// Register Allocation Pass Configuration
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000449//===---------------------------------------------------------------------===//
Andrew Trickf7b96312012-02-09 00:40:55 +0000450
Andrew Trick8dd26252012-02-10 04:10:36 +0000451bool TargetPassConfig::getOptimizeRegAlloc() const {
452 switch (OptimizeRegAlloc) {
453 case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
454 case cl::BOU_TRUE: return true;
455 case cl::BOU_FALSE: return false;
456 }
457 llvm_unreachable("Invalid optimize-regalloc state");
458}
459
Andrew Trickf7b96312012-02-09 00:40:55 +0000460/// RegisterRegAlloc's global Registry tracks allocator registration.
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000461MachinePassRegistry RegisterRegAlloc::Registry;
462
Andrew Trickf7b96312012-02-09 00:40:55 +0000463/// A dummy default pass factory indicates whether the register allocator is
464/// overridden on the command line.
Andrew Trick8dd26252012-02-10 04:10:36 +0000465static FunctionPass *useDefaultRegisterAllocator() { return 0; }
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000466static RegisterRegAlloc
467defaultRegAlloc("default",
468 "pick register allocator based on -O option",
Andrew Trick8dd26252012-02-10 04:10:36 +0000469 useDefaultRegisterAllocator);
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000470
Andrew Trickf7b96312012-02-09 00:40:55 +0000471/// -regalloc=... command line option.
Dan Gohman844731a2008-05-13 00:00:25 +0000472static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
473 RegisterPassParser<RegisterRegAlloc> >
474RegAlloc("regalloc",
Andrew Trick8dd26252012-02-10 04:10:36 +0000475 cl::init(&useDefaultRegisterAllocator),
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000476 cl::desc("Register allocator to use"));
Alkis Evlogimenos7237ece2003-10-02 16:57:49 +0000477
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000478
Andrew Trick8dd26252012-02-10 04:10:36 +0000479/// Instantiate the default register allocator pass for this target for either
480/// the optimized or unoptimized allocation path. This will be added to the pass
481/// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
482/// in the optimized case.
483///
484/// A target that uses the standard regalloc pass order for fast or optimized
485/// allocation may still override this for per-target regalloc
486/// selection. But -regalloc=... always takes precedence.
487FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
488 if (Optimized)
489 return createGreedyRegisterAllocator();
490 else
491 return createFastRegisterAllocator();
492}
493
494/// Find and instantiate the register allocation pass requested by this target
495/// at the current optimization level. Different register allocators are
496/// defined as separate passes because they may require different analysis.
497///
498/// This helper ensures that the regalloc= option is always available,
499/// even for targets that override the default allocator.
500///
501/// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
502/// this can be folded into addPass.
503FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
Jim Laskey9ff542f2006-08-01 18:29:48 +0000504 RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000505
Andrew Trick8dd26252012-02-10 04:10:36 +0000506 // Initialize the global default.
Jim Laskey13ec7022006-08-01 14:21:23 +0000507 if (!Ctor) {
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000508 Ctor = RegAlloc;
509 RegisterRegAlloc::setDefault(RegAlloc);
Jim Laskey13ec7022006-08-01 14:21:23 +0000510 }
Andrew Trick8dd26252012-02-10 04:10:36 +0000511 if (Ctor != useDefaultRegisterAllocator)
Jakob Stoklund Olesen700bfad2010-05-27 23:57:25 +0000512 return Ctor();
513
Andrew Trick8dd26252012-02-10 04:10:36 +0000514 // With no -regalloc= override, ask the target for a regalloc pass.
515 return createTargetRegisterAllocator(Optimized);
516}
517
518/// Add the minimum set of target-independent passes that are required for
519/// register allocation. No coalescing or scheduling.
520void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
521 addPass(PHIEliminationID);
522 addPass(TwoAddressInstructionPassID);
523
524 PM.add(RegAllocPass);
525 printAndVerify("After Register Allocation");
Jim Laskey33a0a6d2006-07-27 20:05:00 +0000526}
Andrew Trickf7b96312012-02-09 00:40:55 +0000527
528/// Add standard target-independent passes that are tightly coupled with
Andrew Trick8dd26252012-02-10 04:10:36 +0000529/// optimized register allocation, including coalescing, machine instruction
530/// scheduling, and register allocation itself.
531void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
532 // LiveVariables currently requires pure SSA form.
533 //
534 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
535 // LiveVariables can be removed completely, and LiveIntervals can be directly
536 // computed. (We still either need to regenerate kill flags after regalloc, or
537 // preferably fix the scavenger to not depend on them).
538 addPass(LiveVariablesID);
539
540 // Add passes that move from transformed SSA into conventional SSA. This is a
541 // "copy coalescing" problem.
542 //
543 if (!EnableStrongPHIElim) {
544 // Edge splitting is smarter with machine loop info.
545 addPass(MachineLoopInfoID);
546 addPass(PHIEliminationID);
547 }
548 addPass(TwoAddressInstructionPassID);
549
550 // FIXME: Either remove this pass completely, or fix it so that it works on
551 // SSA form. We could modify LiveIntervals to be independent of this pass, But
552 // it would be even better to simply eliminate *all* IMPLICIT_DEFs before
553 // leaving SSA.
554 addPass(ProcessImplicitDefsID);
555
556 if (EnableStrongPHIElim)
557 addPass(StrongPHIEliminationID);
558
559 addPass(RegisterCoalescerID);
560
561 // PreRA instruction scheduling.
Andrew Trick17d35e52012-03-14 04:00:41 +0000562 if (addPass(MachineSchedulerID) != &NoPassID)
563 printAndVerify("After Machine Scheduling");
Andrew Trick8dd26252012-02-10 04:10:36 +0000564
565 // Add the selected register allocation pass.
566 PM.add(RegAllocPass);
Andrew Trickf7b96312012-02-09 00:40:55 +0000567 printAndVerify("After Register Allocation");
568
Andrew Trick746f24b2012-02-11 07:11:32 +0000569 // FinalizeRegAlloc is convenient until MachineInstrBundles is more mature,
570 // but eventually, all users of it should probably be moved to addPostRA and
571 // it can go away. Currently, it's the intended place for targets to run
572 // FinalizeMachineBundles, because passes other than MachineScheduling an
573 // RegAlloc itself may not be aware of bundles.
574 if (addFinalizeRegAlloc())
575 printAndVerify("After RegAlloc finalization");
576
Andrew Trickf7b96312012-02-09 00:40:55 +0000577 // Perform stack slot coloring and post-ra machine LICM.
Andrew Trick8dd26252012-02-10 04:10:36 +0000578 //
579 // FIXME: Re-enable coloring with register when it's capable of adding
580 // kill markers.
Andrew Trick900d7b72012-02-15 07:57:03 +0000581 addPass(StackSlotColoringID);
582
583 // Run post-ra machine LICM to hoist reloads / remats.
584 //
585 // FIXME: can this move into MachineLateOptimization?
586 addPass(PostRAMachineLICMID);
587
588 printAndVerify("After StackSlotColoring and postra Machine LICM");
Andrew Trickf7b96312012-02-09 00:40:55 +0000589}
590
591//===---------------------------------------------------------------------===//
592/// Post RegAlloc Pass Configuration
593//===---------------------------------------------------------------------===//
594
595/// Add passes that optimize machine instructions after register allocation.
596void TargetPassConfig::addMachineLateOptimization() {
597 // Branch folding must be run after regalloc and prolog/epilog insertion.
Andrew Trick79bf2882012-02-15 03:21:51 +0000598 if (addPass(BranchFolderPassID) != &NoPassID)
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000599 printAndVerify("After BranchFolding");
Andrew Trickf7b96312012-02-09 00:40:55 +0000600
601 // Tail duplication.
Andrew Trick79bf2882012-02-15 03:21:51 +0000602 if (addPass(TailDuplicateID) != &NoPassID)
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000603 printAndVerify("After TailDuplicate");
Andrew Trickf7b96312012-02-09 00:40:55 +0000604
605 // Copy propagation.
Andrew Trick79bf2882012-02-15 03:21:51 +0000606 if (addPass(MachineCopyPropagationID) != &NoPassID)
Jakob Stoklund Olesen663ee202012-03-28 20:47:37 +0000607 printAndVerify("After copy propagation pass");
Andrew Trickf7b96312012-02-09 00:40:55 +0000608}
609
610/// Add standard basic block placement passes.
611void TargetPassConfig::addBlockPlacement() {
Andrew Trick79bf2882012-02-15 03:21:51 +0000612 AnalysisID ID = &NoPassID;
Andrew Trickf7b96312012-02-09 00:40:55 +0000613 if (EnableBlockPlacement) {
614 // MachineBlockPlacement is an experimental pass which is disabled by
615 // default currently. Eventually it should subsume CodePlacementOpt, so
616 // when enabled, the other is disabled.
Andrew Trick79bf2882012-02-15 03:21:51 +0000617 ID = addPass(MachineBlockPlacementID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000618 } else {
Andrew Trick79bf2882012-02-15 03:21:51 +0000619 ID = addPass(CodePlacementOptID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000620 }
Andrew Trick79bf2882012-02-15 03:21:51 +0000621 if (ID != &NoPassID) {
622 // Run a separate pass to collect block placement statistics.
623 if (EnableBlockPlacementStats)
624 addPass(MachineBlockPlacementStatsID);
Andrew Trickf7b96312012-02-09 00:40:55 +0000625
Jakob Stoklund Olesen8b4c5022012-03-28 23:54:28 +0000626 printAndVerify("After machine block placement.");
Andrew Trickf7b96312012-02-09 00:40:55 +0000627 }
628}