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