LLVM API Documentation

Passes.cpp
Go to the documentation of this file.
00001 //===-- Passes.cpp - Target independent code generation passes ------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines interfaces to access the target independent code
00011 // generation passes provided by the LLVM backend.
00012 //
00013 //===---------------------------------------------------------------------===//
00014 
00015 #include "llvm/CodeGen/Passes.h"
00016 #include "llvm/Analysis/Passes.h"
00017 #include "llvm/Analysis/Verifier.h"
00018 #include "llvm/Assembly/PrintModulePass.h"
00019 #include "llvm/CodeGen/GCStrategy.h"
00020 #include "llvm/CodeGen/MachineFunctionPass.h"
00021 #include "llvm/CodeGen/RegAllocRegistry.h"
00022 #include "llvm/MC/MCAsmInfo.h"
00023 #include "llvm/PassManager.h"
00024 #include "llvm/Support/CommandLine.h"
00025 #include "llvm/Support/Debug.h"
00026 #include "llvm/Support/ErrorHandling.h"
00027 #include "llvm/Target/TargetLowering.h"
00028 #include "llvm/Target/TargetSubtargetInfo.h"
00029 #include "llvm/Transforms/Scalar.h"
00030 
00031 using namespace llvm;
00032 
00033 static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden,
00034     cl::desc("Disable Post Regalloc"));
00035 static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden,
00036     cl::desc("Disable branch folding"));
00037 static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
00038     cl::desc("Disable tail duplication"));
00039 static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
00040     cl::desc("Disable pre-register allocation tail duplication"));
00041 static cl::opt<bool> DisableBlockPlacement("disable-block-placement",
00042     cl::Hidden, cl::desc("Disable probability-driven block placement"));
00043 static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats",
00044     cl::Hidden, cl::desc("Collect probability-driven block placement stats"));
00045 static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
00046     cl::desc("Disable Stack Slot Coloring"));
00047 static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden,
00048     cl::desc("Disable Machine Dead Code Elimination"));
00049 static cl::opt<bool> DisableEarlyIfConversion("disable-early-ifcvt", cl::Hidden,
00050     cl::desc("Disable Early If-conversion"));
00051 static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
00052     cl::desc("Disable Machine LICM"));
00053 static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden,
00054     cl::desc("Disable Machine Common Subexpression Elimination"));
00055 static cl::opt<cl::boolOrDefault>
00056 OptimizeRegAlloc("optimize-regalloc", cl::Hidden,
00057     cl::desc("Enable optimized register allocation compilation path."));
00058 static cl::opt<cl::boolOrDefault>
00059 EnableMachineSched("enable-misched", cl::Hidden,
00060     cl::desc("Enable the machine instruction scheduling pass."));
00061 static cl::opt<bool> EnableStrongPHIElim("strong-phi-elim", cl::Hidden,
00062     cl::desc("Use strong PHI elimination."));
00063 static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
00064     cl::Hidden,
00065     cl::desc("Disable Machine LICM"));
00066 static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
00067     cl::desc("Disable Machine Sinking"));
00068 static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
00069     cl::desc("Disable Loop Strength Reduction Pass"));
00070 static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
00071     cl::desc("Disable Codegen Prepare"));
00072 static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden,
00073     cl::desc("Disable Copy Propagation pass"));
00074 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
00075     cl::desc("Print LLVM IR produced by the loop-reduce pass"));
00076 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
00077     cl::desc("Print LLVM IR input to isel pass"));
00078 static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
00079     cl::desc("Dump garbage collector data"));
00080 static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
00081     cl::desc("Verify generated machine code"),
00082     cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
00083 static cl::opt<std::string>
00084 PrintMachineInstrs("print-machineinstrs", cl::ValueOptional,
00085                    cl::desc("Print machine instrs"),
00086                    cl::value_desc("pass-name"), cl::init("option-unspecified"));
00087 
00088 // Experimental option to run live interval analysis early.
00089 static cl::opt<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden,
00090     cl::desc("Run live interval analysis earlier in the pipeline"));
00091 
00092 /// Allow standard passes to be disabled by command line options. This supports
00093 /// simple binary flags that either suppress the pass or do nothing.
00094 /// i.e. -disable-mypass=false has no effect.
00095 /// These should be converted to boolOrDefault in order to use applyOverride.
00096 static IdentifyingPassPtr applyDisable(IdentifyingPassPtr PassID,
00097                                        bool Override) {
00098   if (Override)
00099     return IdentifyingPassPtr();
00100   return PassID;
00101 }
00102 
00103 /// Allow Pass selection to be overriden by command line options. This supports
00104 /// flags with ternary conditions. TargetID is passed through by default. The
00105 /// pass is suppressed when the option is false. When the option is true, the
00106 /// StandardID is selected if the target provides no default.
00107 static IdentifyingPassPtr applyOverride(IdentifyingPassPtr TargetID,
00108                                         cl::boolOrDefault Override,
00109                                         AnalysisID StandardID) {
00110   switch (Override) {
00111   case cl::BOU_UNSET:
00112     return TargetID;
00113   case cl::BOU_TRUE:
00114     if (TargetID.isValid())
00115       return TargetID;
00116     if (StandardID == 0)
00117       report_fatal_error("Target cannot enable pass");
00118     return StandardID;
00119   case cl::BOU_FALSE:
00120     return IdentifyingPassPtr();
00121   }
00122   llvm_unreachable("Invalid command line option state");
00123 }
00124 
00125 /// Allow standard passes to be disabled by the command line, regardless of who
00126 /// is adding the pass.
00127 ///
00128 /// StandardID is the pass identified in the standard pass pipeline and provided
00129 /// to addPass(). It may be a target-specific ID in the case that the target
00130 /// directly adds its own pass, but in that case we harmlessly fall through.
00131 ///
00132 /// TargetID is the pass that the target has configured to override StandardID.
00133 ///
00134 /// StandardID may be a pseudo ID. In that case TargetID is the name of the real
00135 /// pass to run. This allows multiple options to control a single pass depending
00136 /// on where in the pipeline that pass is added.
00137 static IdentifyingPassPtr overridePass(AnalysisID StandardID,
00138                                        IdentifyingPassPtr TargetID) {
00139   if (StandardID == &PostRASchedulerID)
00140     return applyDisable(TargetID, DisablePostRA);
00141 
00142   if (StandardID == &BranchFolderPassID)
00143     return applyDisable(TargetID, DisableBranchFold);
00144 
00145   if (StandardID == &TailDuplicateID)
00146     return applyDisable(TargetID, DisableTailDuplicate);
00147 
00148   if (StandardID == &TargetPassConfig::EarlyTailDuplicateID)
00149     return applyDisable(TargetID, DisableEarlyTailDup);
00150 
00151   if (StandardID == &MachineBlockPlacementID)
00152     return applyDisable(TargetID, DisableBlockPlacement);
00153 
00154   if (StandardID == &StackSlotColoringID)
00155     return applyDisable(TargetID, DisableSSC);
00156 
00157   if (StandardID == &DeadMachineInstructionElimID)
00158     return applyDisable(TargetID, DisableMachineDCE);
00159 
00160   if (StandardID == &EarlyIfConverterID)
00161     return applyDisable(TargetID, DisableEarlyIfConversion);
00162 
00163   if (StandardID == &MachineLICMID)
00164     return applyDisable(TargetID, DisableMachineLICM);
00165 
00166   if (StandardID == &MachineCSEID)
00167     return applyDisable(TargetID, DisableMachineCSE);
00168 
00169   if (StandardID == &MachineSchedulerID)
00170     return applyOverride(TargetID, EnableMachineSched, StandardID);
00171 
00172   if (StandardID == &TargetPassConfig::PostRAMachineLICMID)
00173     return applyDisable(TargetID, DisablePostRAMachineLICM);
00174 
00175   if (StandardID == &MachineSinkingID)
00176     return applyDisable(TargetID, DisableMachineSink);
00177 
00178   if (StandardID == &MachineCopyPropagationID)
00179     return applyDisable(TargetID, DisableCopyProp);
00180 
00181   return TargetID;
00182 }
00183 
00184 //===---------------------------------------------------------------------===//
00185 /// TargetPassConfig
00186 //===---------------------------------------------------------------------===//
00187 
00188 INITIALIZE_PASS(TargetPassConfig, "targetpassconfig",
00189                 "Target Pass Configuration", false, false)
00190 char TargetPassConfig::ID = 0;
00191 
00192 // Pseudo Pass IDs.
00193 char TargetPassConfig::EarlyTailDuplicateID = 0;
00194 char TargetPassConfig::PostRAMachineLICMID = 0;
00195 
00196 namespace llvm {
00197 class PassConfigImpl {
00198 public:
00199   // List of passes explicitly substituted by this target. Normally this is
00200   // empty, but it is a convenient way to suppress or replace specific passes
00201   // that are part of a standard pass pipeline without overridding the entire
00202   // pipeline. This mechanism allows target options to inherit a standard pass's
00203   // user interface. For example, a target may disable a standard pass by
00204   // default by substituting a pass ID of zero, and the user may still enable
00205   // that standard pass with an explicit command line option.
00206   DenseMap<AnalysisID,IdentifyingPassPtr> TargetPasses;
00207 
00208   /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass
00209   /// is inserted after each instance of the first one.
00210   SmallVector<std::pair<AnalysisID, IdentifyingPassPtr>, 4> InsertedPasses;
00211 };
00212 } // namespace llvm
00213 
00214 // Out of line virtual method.
00215 TargetPassConfig::~TargetPassConfig() {
00216   delete Impl;
00217 }
00218 
00219 // Out of line constructor provides default values for pass options and
00220 // registers all common codegen passes.
00221 TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm)
00222   : ImmutablePass(ID), PM(&pm), StartAfter(0), StopAfter(0),
00223     Started(true), Stopped(false), TM(tm), Impl(0), Initialized(false),
00224     DisableVerify(false),
00225     EnableTailMerge(true) {
00226 
00227   Impl = new PassConfigImpl();
00228 
00229   // Register all target independent codegen passes to activate their PassIDs,
00230   // including this pass itself.
00231   initializeCodeGen(*PassRegistry::getPassRegistry());
00232 
00233   // Substitute Pseudo Pass IDs for real ones.
00234   substitutePass(&EarlyTailDuplicateID, &TailDuplicateID);
00235   substitutePass(&PostRAMachineLICMID, &MachineLICMID);
00236 
00237   // Temporarily disable experimental passes.
00238   const TargetSubtargetInfo &ST = TM->getSubtarget<TargetSubtargetInfo>();
00239   if (!ST.enableMachineScheduler())
00240     disablePass(&MachineSchedulerID);
00241 }
00242 
00243 /// Insert InsertedPassID pass after TargetPassID.
00244 void TargetPassConfig::insertPass(AnalysisID TargetPassID,
00245                                   IdentifyingPassPtr InsertedPassID) {
00246   assert(((!InsertedPassID.isInstance() &&
00247            TargetPassID != InsertedPassID.getID()) ||
00248           (InsertedPassID.isInstance() &&
00249            TargetPassID != InsertedPassID.getInstance()->getPassID())) &&
00250          "Insert a pass after itself!");
00251   std::pair<AnalysisID, IdentifyingPassPtr> P(TargetPassID, InsertedPassID);
00252   Impl->InsertedPasses.push_back(P);
00253 }
00254 
00255 /// createPassConfig - Create a pass configuration object to be used by
00256 /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
00257 ///
00258 /// Targets may override this to extend TargetPassConfig.
00259 TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
00260   return new TargetPassConfig(this, PM);
00261 }
00262 
00263 TargetPassConfig::TargetPassConfig()
00264   : ImmutablePass(ID), PM(0) {
00265   llvm_unreachable("TargetPassConfig should not be constructed on-the-fly");
00266 }
00267 
00268 // Helper to verify the analysis is really immutable.
00269 void TargetPassConfig::setOpt(bool &Opt, bool Val) {
00270   assert(!Initialized && "PassConfig is immutable");
00271   Opt = Val;
00272 }
00273 
00274 void TargetPassConfig::substitutePass(AnalysisID StandardID,
00275                                       IdentifyingPassPtr TargetID) {
00276   Impl->TargetPasses[StandardID] = TargetID;
00277 }
00278 
00279 IdentifyingPassPtr TargetPassConfig::getPassSubstitution(AnalysisID ID) const {
00280   DenseMap<AnalysisID, IdentifyingPassPtr>::const_iterator
00281     I = Impl->TargetPasses.find(ID);
00282   if (I == Impl->TargetPasses.end())
00283     return ID;
00284   return I->second;
00285 }
00286 
00287 /// Add a pass to the PassManager if that pass is supposed to be run.  If the
00288 /// Started/Stopped flags indicate either that the compilation should start at
00289 /// a later pass or that it should stop after an earlier pass, then do not add
00290 /// the pass.  Finally, compare the current pass against the StartAfter
00291 /// and StopAfter options and change the Started/Stopped flags accordingly.
00292 void TargetPassConfig::addPass(Pass *P) {
00293   assert(!Initialized && "PassConfig is immutable");
00294 
00295   // Cache the Pass ID here in case the pass manager finds this pass is
00296   // redundant with ones already scheduled / available, and deletes it.
00297   // Fundamentally, once we add the pass to the manager, we no longer own it
00298   // and shouldn't reference it.
00299   AnalysisID PassID = P->getPassID();
00300 
00301   if (Started && !Stopped)
00302     PM->add(P);
00303   if (StopAfter == PassID)
00304     Stopped = true;
00305   if (StartAfter == PassID)
00306     Started = true;
00307   if (Stopped && !Started)
00308     report_fatal_error("Cannot stop compilation after pass that is not run");
00309 }
00310 
00311 /// Add a CodeGen pass at this point in the pipeline after checking for target
00312 /// and command line overrides.
00313 ///
00314 /// addPass cannot return a pointer to the pass instance because is internal the
00315 /// PassManager and the instance we create here may already be freed.
00316 AnalysisID TargetPassConfig::addPass(AnalysisID PassID) {
00317   IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
00318   IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID);
00319   if (!FinalPtr.isValid())
00320     return 0;
00321 
00322   Pass *P;
00323   if (FinalPtr.isInstance())
00324     P = FinalPtr.getInstance();
00325   else {
00326     P = Pass::createPass(FinalPtr.getID());
00327     if (!P)
00328       llvm_unreachable("Pass ID not registered");
00329   }
00330   AnalysisID FinalID = P->getPassID();
00331   addPass(P); // Ends the lifetime of P.
00332 
00333   // Add the passes after the pass P if there is any.
00334   for (SmallVector<std::pair<AnalysisID, IdentifyingPassPtr>, 4>::iterator
00335          I = Impl->InsertedPasses.begin(), E = Impl->InsertedPasses.end();
00336        I != E; ++I) {
00337     if ((*I).first == PassID) {
00338       assert((*I).second.isValid() && "Illegal Pass ID!");
00339       Pass *NP;
00340       if ((*I).second.isInstance())
00341         NP = (*I).second.getInstance();
00342       else {
00343         NP = Pass::createPass((*I).second.getID());
00344         assert(NP && "Pass ID not registered");
00345       }
00346       addPass(NP);
00347     }
00348   }
00349   return FinalID;
00350 }
00351 
00352 void TargetPassConfig::printAndVerify(const char *Banner) {
00353   if (TM->shouldPrintMachineCode())
00354     addPass(createMachineFunctionPrinterPass(dbgs(), Banner));
00355 
00356   if (VerifyMachineCode)
00357     addPass(createMachineVerifierPass(Banner));
00358 }
00359 
00360 /// Add common target configurable passes that perform LLVM IR to IR transforms
00361 /// following machine independent optimization.
00362 void TargetPassConfig::addIRPasses() {
00363   // Basic AliasAnalysis support.
00364   // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
00365   // BasicAliasAnalysis wins if they disagree. This is intended to help
00366   // support "obvious" type-punning idioms.
00367   addPass(createTypeBasedAliasAnalysisPass());
00368   addPass(createBasicAliasAnalysisPass());
00369 
00370   // Before running any passes, run the verifier to determine if the input
00371   // coming from the front-end and/or optimizer is valid.
00372   if (!DisableVerify)
00373     addPass(createVerifierPass());
00374 
00375   // Run loop strength reduction before anything else.
00376   if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
00377     addPass(createLoopStrengthReducePass());
00378     if (PrintLSR)
00379       addPass(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
00380   }
00381 
00382   addPass(createGCLoweringPass());
00383 
00384   // Make sure that no unreachable blocks are instruction selected.
00385   addPass(createUnreachableBlockEliminationPass());
00386 }
00387 
00388 /// Turn exception handling constructs into something the code generators can
00389 /// handle.
00390 void TargetPassConfig::addPassesToHandleExceptions() {
00391   switch (TM->getMCAsmInfo()->getExceptionHandlingType()) {
00392   case ExceptionHandling::SjLj:
00393     // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
00394     // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
00395     // catch info can get misplaced when a selector ends up more than one block
00396     // removed from the parent invoke(s). This could happen when a landing
00397     // pad is shared by multiple invokes and is also a target of a normal
00398     // edge from elsewhere.
00399     addPass(createSjLjEHPreparePass(TM->getTargetLowering()));
00400     // FALLTHROUGH
00401   case ExceptionHandling::DwarfCFI:
00402   case ExceptionHandling::ARM:
00403   case ExceptionHandling::Win64:
00404     addPass(createDwarfEHPass(TM->getTargetLowering()));
00405     break;
00406   case ExceptionHandling::None:
00407     addPass(createLowerInvokePass(TM->getTargetLowering()));
00408 
00409     // The lower invoke pass may create unreachable code. Remove it.
00410     addPass(createUnreachableBlockEliminationPass());
00411     break;
00412   }
00413 }
00414 
00415 /// Add pass to prepare the LLVM IR for code generation. This should be done
00416 /// before exception handling preparation passes.
00417 void TargetPassConfig::addCodeGenPrepare() {
00418   if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
00419     addPass(createCodeGenPreparePass(getTargetLowering()));
00420 }
00421 
00422 /// Add common passes that perform LLVM IR to IR transforms in preparation for
00423 /// instruction selection.
00424 void TargetPassConfig::addISelPrepare() {
00425   addPass(createStackProtectorPass(getTargetLowering()));
00426 
00427   addPreISel();
00428 
00429   if (PrintISelInput)
00430     addPass(createPrintFunctionPass("\n\n"
00431                                     "*** Final LLVM Code input to ISel ***\n",
00432                                     &dbgs()));
00433 
00434   // All passes which modify the LLVM IR are now complete; run the verifier
00435   // to ensure that the IR is valid.
00436   if (!DisableVerify)
00437     addPass(createVerifierPass());
00438 }
00439 
00440 /// Add the complete set of target-independent postISel code generator passes.
00441 ///
00442 /// This can be read as the standard order of major LLVM CodeGen stages. Stages
00443 /// with nontrivial configuration or multiple passes are broken out below in
00444 /// add%Stage routines.
00445 ///
00446 /// Any TargetPassConfig::addXX routine may be overriden by the Target. The
00447 /// addPre/Post methods with empty header implementations allow injecting
00448 /// target-specific fixups just before or after major stages. Additionally,
00449 /// targets have the flexibility to change pass order within a stage by
00450 /// overriding default implementation of add%Stage routines below. Each
00451 /// technique has maintainability tradeoffs because alternate pass orders are
00452 /// not well supported. addPre/Post works better if the target pass is easily
00453 /// tied to a common pass. But if it has subtle dependencies on multiple passes,
00454 /// the target should override the stage instead.
00455 ///
00456 /// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
00457 /// before/after any target-independent pass. But it's currently overkill.
00458 void TargetPassConfig::addMachinePasses() {
00459   // Insert a machine instr printer pass after the specified pass.
00460   // If -print-machineinstrs specified, print machineinstrs after all passes.
00461   if (StringRef(PrintMachineInstrs.getValue()).equals(""))
00462     TM->Options.PrintMachineCode = true;
00463   else if (!StringRef(PrintMachineInstrs.getValue())
00464            .equals("option-unspecified")) {
00465     const PassRegistry *PR = PassRegistry::getPassRegistry();
00466     const PassInfo *TPI = PR->getPassInfo(PrintMachineInstrs.getValue());
00467     const PassInfo *IPI = PR->getPassInfo(StringRef("print-machineinstrs"));
00468     assert (TPI && IPI && "Pass ID not registered!");
00469     const char *TID = (const char *)(TPI->getTypeInfo());
00470     const char *IID = (const char *)(IPI->getTypeInfo());
00471     insertPass(TID, IID);
00472   }
00473 
00474   // Print the instruction selected machine code...
00475   printAndVerify("After Instruction Selection");
00476 
00477   // Expand pseudo-instructions emitted by ISel.
00478   if (addPass(&ExpandISelPseudosID))
00479     printAndVerify("After ExpandISelPseudos");
00480 
00481   // Add passes that optimize machine instructions in SSA form.
00482   if (getOptLevel() != CodeGenOpt::None) {
00483     addMachineSSAOptimization();
00484   } else {
00485     // If the target requests it, assign local variables to stack slots relative
00486     // to one another and simplify frame index references where possible.
00487     addPass(&LocalStackSlotAllocationID);
00488   }
00489 
00490   // Run pre-ra passes.
00491   if (addPreRegAlloc())
00492     printAndVerify("After PreRegAlloc passes");
00493 
00494   // Run register allocation and passes that are tightly coupled with it,
00495   // including phi elimination and scheduling.
00496   if (getOptimizeRegAlloc())
00497     addOptimizedRegAlloc(createRegAllocPass(true));
00498   else
00499     addFastRegAlloc(createRegAllocPass(false));
00500 
00501   // Run post-ra passes.
00502   if (addPostRegAlloc())
00503     printAndVerify("After PostRegAlloc passes");
00504 
00505   // Insert prolog/epilog code.  Eliminate abstract frame index references...
00506   addPass(&PrologEpilogCodeInserterID);
00507   printAndVerify("After PrologEpilogCodeInserter");
00508 
00509   /// Add passes that optimize machine instructions after register allocation.
00510   if (getOptLevel() != CodeGenOpt::None)
00511     addMachineLateOptimization();
00512 
00513   // Expand pseudo instructions before second scheduling pass.
00514   addPass(&ExpandPostRAPseudosID);
00515   printAndVerify("After ExpandPostRAPseudos");
00516 
00517   // Run pre-sched2 passes.
00518   if (addPreSched2())
00519     printAndVerify("After PreSched2 passes");
00520 
00521   // Second pass scheduler.
00522   if (getOptLevel() != CodeGenOpt::None) {
00523     addPass(&PostRASchedulerID);
00524     printAndVerify("After PostRAScheduler");
00525   }
00526 
00527   // GC
00528   if (addGCPasses()) {
00529     if (PrintGCInfo)
00530       addPass(createGCInfoPrinter(dbgs()));
00531   }
00532 
00533   // Basic block placement.
00534   if (getOptLevel() != CodeGenOpt::None)
00535     addBlockPlacement();
00536 
00537   if (addPreEmitPass())
00538     printAndVerify("After PreEmit passes");
00539 }
00540 
00541 /// Add passes that optimize machine instructions in SSA form.
00542 void TargetPassConfig::addMachineSSAOptimization() {
00543   // Pre-ra tail duplication.
00544   if (addPass(&EarlyTailDuplicateID))
00545     printAndVerify("After Pre-RegAlloc TailDuplicate");
00546 
00547   // Optimize PHIs before DCE: removing dead PHI cycles may make more
00548   // instructions dead.
00549   addPass(&OptimizePHIsID);
00550 
00551   // This pass merges large allocas. StackSlotColoring is a different pass
00552   // which merges spill slots.
00553   addPass(&StackColoringID);
00554 
00555   // If the target requests it, assign local variables to stack slots relative
00556   // to one another and simplify frame index references where possible.
00557   addPass(&LocalStackSlotAllocationID);
00558 
00559   // With optimization, dead code should already be eliminated. However
00560   // there is one known exception: lowered code for arguments that are only
00561   // used by tail calls, where the tail calls reuse the incoming stack
00562   // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
00563   addPass(&DeadMachineInstructionElimID);
00564   printAndVerify("After codegen DCE pass");
00565 
00566   // Allow targets to insert passes that improve instruction level parallelism,
00567   // like if-conversion. Such passes will typically need dominator trees and
00568   // loop info, just like LICM and CSE below.
00569   if (addILPOpts())
00570     printAndVerify("After ILP optimizations");
00571 
00572   addPass(&MachineLICMID);
00573   addPass(&MachineCSEID);
00574   addPass(&MachineSinkingID);
00575   printAndVerify("After Machine LICM, CSE and Sinking passes");
00576 
00577   addPass(&PeepholeOptimizerID);
00578   printAndVerify("After codegen peephole optimization pass");
00579 }
00580 
00581 //===---------------------------------------------------------------------===//
00582 /// Register Allocation Pass Configuration
00583 //===---------------------------------------------------------------------===//
00584 
00585 bool TargetPassConfig::getOptimizeRegAlloc() const {
00586   switch (OptimizeRegAlloc) {
00587   case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
00588   case cl::BOU_TRUE:  return true;
00589   case cl::BOU_FALSE: return false;
00590   }
00591   llvm_unreachable("Invalid optimize-regalloc state");
00592 }
00593 
00594 /// RegisterRegAlloc's global Registry tracks allocator registration.
00595 MachinePassRegistry RegisterRegAlloc::Registry;
00596 
00597 /// A dummy default pass factory indicates whether the register allocator is
00598 /// overridden on the command line.
00599 static FunctionPass *useDefaultRegisterAllocator() { return 0; }
00600 static RegisterRegAlloc
00601 defaultRegAlloc("default",
00602                 "pick register allocator based on -O option",
00603                 useDefaultRegisterAllocator);
00604 
00605 /// -regalloc=... command line option.
00606 static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
00607                RegisterPassParser<RegisterRegAlloc> >
00608 RegAlloc("regalloc",
00609          cl::init(&useDefaultRegisterAllocator),
00610          cl::desc("Register allocator to use"));
00611 
00612 
00613 /// Instantiate the default register allocator pass for this target for either
00614 /// the optimized or unoptimized allocation path. This will be added to the pass
00615 /// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
00616 /// in the optimized case.
00617 ///
00618 /// A target that uses the standard regalloc pass order for fast or optimized
00619 /// allocation may still override this for per-target regalloc
00620 /// selection. But -regalloc=... always takes precedence.
00621 FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
00622   if (Optimized)
00623     return createGreedyRegisterAllocator();
00624   else
00625     return createFastRegisterAllocator();
00626 }
00627 
00628 /// Find and instantiate the register allocation pass requested by this target
00629 /// at the current optimization level.  Different register allocators are
00630 /// defined as separate passes because they may require different analysis.
00631 ///
00632 /// This helper ensures that the regalloc= option is always available,
00633 /// even for targets that override the default allocator.
00634 ///
00635 /// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
00636 /// this can be folded into addPass.
00637 FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
00638   RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
00639 
00640   // Initialize the global default.
00641   if (!Ctor) {
00642     Ctor = RegAlloc;
00643     RegisterRegAlloc::setDefault(RegAlloc);
00644   }
00645   if (Ctor != useDefaultRegisterAllocator)
00646     return Ctor();
00647 
00648   // With no -regalloc= override, ask the target for a regalloc pass.
00649   return createTargetRegisterAllocator(Optimized);
00650 }
00651 
00652 /// Add the minimum set of target-independent passes that are required for
00653 /// register allocation. No coalescing or scheduling.
00654 void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
00655   addPass(&PHIEliminationID);
00656   addPass(&TwoAddressInstructionPassID);
00657 
00658   addPass(RegAllocPass);
00659   printAndVerify("After Register Allocation");
00660 }
00661 
00662 /// Add standard target-independent passes that are tightly coupled with
00663 /// optimized register allocation, including coalescing, machine instruction
00664 /// scheduling, and register allocation itself.
00665 void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
00666   addPass(&ProcessImplicitDefsID);
00667 
00668   // LiveVariables currently requires pure SSA form.
00669   //
00670   // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
00671   // LiveVariables can be removed completely, and LiveIntervals can be directly
00672   // computed. (We still either need to regenerate kill flags after regalloc, or
00673   // preferably fix the scavenger to not depend on them).
00674   addPass(&LiveVariablesID);
00675 
00676   // Add passes that move from transformed SSA into conventional SSA. This is a
00677   // "copy coalescing" problem.
00678   //
00679   if (!EnableStrongPHIElim) {
00680     // Edge splitting is smarter with machine loop info.
00681     addPass(&MachineLoopInfoID);
00682     addPass(&PHIEliminationID);
00683   }
00684 
00685   // Eventually, we want to run LiveIntervals before PHI elimination.
00686   if (EarlyLiveIntervals)
00687     addPass(&LiveIntervalsID);
00688 
00689   addPass(&TwoAddressInstructionPassID);
00690 
00691   if (EnableStrongPHIElim)
00692     addPass(&StrongPHIEliminationID);
00693 
00694   addPass(&RegisterCoalescerID);
00695 
00696   // PreRA instruction scheduling.
00697   if (addPass(&MachineSchedulerID))
00698     printAndVerify("After Machine Scheduling");
00699 
00700   // Add the selected register allocation pass.
00701   addPass(RegAllocPass);
00702   printAndVerify("After Register Allocation, before rewriter");
00703 
00704   // Allow targets to change the register assignments before rewriting.
00705   if (addPreRewrite())
00706     printAndVerify("After pre-rewrite passes");
00707 
00708   // Finally rewrite virtual registers.
00709   addPass(&VirtRegRewriterID);
00710   printAndVerify("After Virtual Register Rewriter");
00711 
00712   // Perform stack slot coloring and post-ra machine LICM.
00713   //
00714   // FIXME: Re-enable coloring with register when it's capable of adding
00715   // kill markers.
00716   addPass(&StackSlotColoringID);
00717 
00718   // Run post-ra machine LICM to hoist reloads / remats.
00719   //
00720   // FIXME: can this move into MachineLateOptimization?
00721   addPass(&PostRAMachineLICMID);
00722 
00723   printAndVerify("After StackSlotColoring and postra Machine LICM");
00724 }
00725 
00726 //===---------------------------------------------------------------------===//
00727 /// Post RegAlloc Pass Configuration
00728 //===---------------------------------------------------------------------===//
00729 
00730 /// Add passes that optimize machine instructions after register allocation.
00731 void TargetPassConfig::addMachineLateOptimization() {
00732   // Branch folding must be run after regalloc and prolog/epilog insertion.
00733   if (addPass(&BranchFolderPassID))
00734     printAndVerify("After BranchFolding");
00735 
00736   // Tail duplication.
00737   if (addPass(&TailDuplicateID))
00738     printAndVerify("After TailDuplicate");
00739 
00740   // Copy propagation.
00741   if (addPass(&MachineCopyPropagationID))
00742     printAndVerify("After copy propagation pass");
00743 }
00744 
00745 /// Add standard GC passes.
00746 bool TargetPassConfig::addGCPasses() {
00747   addPass(&GCMachineCodeAnalysisID);
00748   return true;
00749 }
00750 
00751 /// Add standard basic block placement passes.
00752 void TargetPassConfig::addBlockPlacement() {
00753   if (addPass(&MachineBlockPlacementID)) {
00754     // Run a separate pass to collect block placement statistics.
00755     if (EnableBlockPlacementStats)
00756       addPass(&MachineBlockPlacementStatsID);
00757 
00758     printAndVerify("After machine block placement.");
00759   }
00760 }