Chris Lattner | aa4c91f | 2003-12-28 07:59:53 +0000 | [diff] [blame] | 1 | //===-- Passes.cpp - Target independent code generation passes ------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 7237ece | 2003-10-02 16:57:49 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines interfaces to access the target independent code |
| 11 | // generation passes provided by the LLVM backend. |
| 12 | // |
| 13 | //===---------------------------------------------------------------------===// |
| 14 | |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 15 | #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 Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Alkis Evlogimenos | 7237ece | 2003-10-02 16:57:49 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/Passes.h" |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 23 | #include "llvm/Target/TargetLowering.h" |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetOptions.h" |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 25 | #include "llvm/Assembly/PrintModulePass.h" |
| 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/Debug.h" |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 29 | |
Chris Lattner | aa4c91f | 2003-12-28 07:59:53 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 32 | static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden, |
| 33 | cl::desc("Disable Post Regalloc")); |
| 34 | static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden, |
| 35 | cl::desc("Disable branch folding")); |
| 36 | static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden, |
| 37 | cl::desc("Disable tail duplication")); |
| 38 | static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden, |
| 39 | cl::desc("Disable pre-register allocation tail duplication")); |
| 40 | static cl::opt<bool> EnableBlockPlacement("enable-block-placement", |
| 41 | cl::Hidden, cl::desc("Enable probability-driven block placement")); |
| 42 | static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats", |
| 43 | cl::Hidden, cl::desc("Collect probability-driven block placement stats")); |
| 44 | static cl::opt<bool> DisableCodePlace("disable-code-place", cl::Hidden, |
| 45 | cl::desc("Disable code placement")); |
| 46 | static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden, |
| 47 | cl::desc("Disable Stack Slot Coloring")); |
| 48 | static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden, |
| 49 | cl::desc("Disable Machine Dead Code Elimination")); |
| 50 | static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden, |
| 51 | cl::desc("Disable Machine LICM")); |
| 52 | static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden, |
| 53 | cl::desc("Disable Machine Common Subexpression Elimination")); |
| 54 | static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm", |
| 55 | cl::Hidden, |
| 56 | cl::desc("Disable Machine LICM")); |
| 57 | static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden, |
| 58 | cl::desc("Disable Machine Sinking")); |
| 59 | static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden, |
| 60 | cl::desc("Disable Loop Strength Reduction Pass")); |
| 61 | static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden, |
| 62 | cl::desc("Disable Codegen Prepare")); |
| 63 | static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden, |
| 64 | cl::desc("Disable Copy Propagation pass")); |
| 65 | static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden, |
| 66 | cl::desc("Print LLVM IR produced by the loop-reduce pass")); |
| 67 | static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden, |
| 68 | cl::desc("Print LLVM IR input to isel pass")); |
| 69 | static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden, |
| 70 | cl::desc("Dump garbage collector data")); |
| 71 | static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden, |
| 72 | cl::desc("Verify generated machine code"), |
| 73 | cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL)); |
| 74 | |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 75 | //===---------------------------------------------------------------------===// |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 76 | /// TargetPassConfig |
| 77 | //===---------------------------------------------------------------------===// |
| 78 | |
| 79 | INITIALIZE_PASS(TargetPassConfig, "targetpassconfig", |
| 80 | "Target Pass Configuration", false, false) |
| 81 | char TargetPassConfig::ID = 0; |
| 82 | |
| 83 | // Out of line virtual method. |
| 84 | TargetPassConfig::~TargetPassConfig() {} |
| 85 | |
Andrew Trick | 61f1e3d | 2012-02-08 21:22:48 +0000 | [diff] [blame] | 86 | // Out of line constructor provides default values for pass options and |
| 87 | // registers all common codegen passes. |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 88 | TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm) |
Andrew Trick | ffea03f | 2012-02-08 21:22:39 +0000 | [diff] [blame] | 89 | : ImmutablePass(ID), TM(tm), PM(pm), Initialized(false), |
| 90 | DisableVerify(false), |
| 91 | EnableTailMerge(true) { |
| 92 | |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 93 | // Register all target independent codegen passes to activate their PassIDs, |
| 94 | // including this pass itself. |
| 95 | initializeCodeGen(*PassRegistry::getPassRegistry()); |
| 96 | } |
| 97 | |
| 98 | /// createPassConfig - Create a pass configuration object to be used by |
| 99 | /// addPassToEmitX methods for generating a pipeline of CodeGen passes. |
| 100 | /// |
| 101 | /// Targets may override this to extend TargetPassConfig. |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 102 | TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) { |
| 103 | return new TargetPassConfig(this, PM); |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | TargetPassConfig::TargetPassConfig() |
| 107 | : ImmutablePass(ID), PM(*(PassManagerBase*)0) { |
| 108 | llvm_unreachable("TargetPassConfig should not be constructed on-the-fly"); |
| 109 | } |
| 110 | |
Andrew Trick | ffea03f | 2012-02-08 21:22:39 +0000 | [diff] [blame] | 111 | // Helper to verify the analysis is really immutable. |
| 112 | void TargetPassConfig::setOpt(bool &Opt, bool Val) { |
| 113 | assert(!Initialized && "PassConfig is immutable"); |
| 114 | Opt = Val; |
| 115 | } |
| 116 | |
Andrew Trick | ebe18ef | 2012-02-08 21:22:34 +0000 | [diff] [blame] | 117 | void TargetPassConfig::addPass(char &ID) { |
| 118 | // FIXME: check user overrides |
| 119 | Pass *P = Pass::createPass(ID); |
| 120 | if (!P) |
| 121 | llvm_unreachable("Pass ID not registered"); |
| 122 | PM.add(P); |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 123 | } |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 124 | |
| 125 | void TargetPassConfig::printNoVerify(const char *Banner) const { |
| 126 | if (TM->shouldPrintMachineCode()) |
| 127 | PM.add(createMachineFunctionPrinterPass(dbgs(), Banner)); |
| 128 | } |
| 129 | |
| 130 | void TargetPassConfig::printAndVerify(const char *Banner) const { |
| 131 | if (TM->shouldPrintMachineCode()) |
| 132 | PM.add(createMachineFunctionPrinterPass(dbgs(), Banner)); |
| 133 | |
| 134 | if (VerifyMachineCode) |
| 135 | PM.add(createMachineVerifierPass(Banner)); |
| 136 | } |
| 137 | |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 138 | /// Add common target configurable passes that perform LLVM IR to IR transforms |
| 139 | /// following machine independent optimization. |
| 140 | void TargetPassConfig::addIRPasses() { |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 141 | // Basic AliasAnalysis support. |
| 142 | // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that |
| 143 | // BasicAliasAnalysis wins if they disagree. This is intended to help |
| 144 | // support "obvious" type-punning idioms. |
| 145 | PM.add(createTypeBasedAliasAnalysisPass()); |
| 146 | PM.add(createBasicAliasAnalysisPass()); |
| 147 | |
| 148 | // Before running any passes, run the verifier to determine if the input |
| 149 | // coming from the front-end and/or optimizer is valid. |
| 150 | if (!DisableVerify) |
| 151 | PM.add(createVerifierPass()); |
| 152 | |
| 153 | // Run loop strength reduction before anything else. |
| 154 | if (getOptLevel() != CodeGenOpt::None && !DisableLSR) { |
| 155 | PM.add(createLoopStrengthReducePass(getTargetLowering())); |
| 156 | if (PrintLSR) |
| 157 | PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs())); |
| 158 | } |
| 159 | |
| 160 | PM.add(createGCLoweringPass()); |
| 161 | |
| 162 | // Make sure that no unreachable blocks are instruction selected. |
| 163 | PM.add(createUnreachableBlockEliminationPass()); |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 164 | } |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 165 | |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 166 | /// Add common passes that perform LLVM IR to IR transforms in preparation for |
| 167 | /// instruction selection. |
| 168 | void TargetPassConfig::addISelPrepare() { |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 169 | if (getOptLevel() != CodeGenOpt::None && !DisableCGP) |
| 170 | PM.add(createCodeGenPreparePass(getTargetLowering())); |
| 171 | |
| 172 | PM.add(createStackProtectorPass(getTargetLowering())); |
| 173 | |
| 174 | addPreISel(); |
| 175 | |
| 176 | if (PrintISelInput) |
| 177 | PM.add(createPrintFunctionPass("\n\n" |
| 178 | "*** Final LLVM Code input to ISel ***\n", |
| 179 | &dbgs())); |
| 180 | |
| 181 | // All passes which modify the LLVM IR are now complete; run the verifier |
| 182 | // to ensure that the IR is valid. |
| 183 | if (!DisableVerify) |
| 184 | PM.add(createVerifierPass()); |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 185 | } |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 186 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 187 | /// Add the complete set of target-independent postISel code generator passes. |
| 188 | /// |
| 189 | /// This can be read as the standard order of major LLVM CodeGen stages. Stages |
| 190 | /// with nontrivial configuration or multiple passes are broken out below in |
| 191 | /// add%Stage routines. |
| 192 | /// |
| 193 | /// Any TargetPassConfig::addXX routine may be overriden by the Target. The |
| 194 | /// addPre/Post methods with empty header implementations allow injecting |
| 195 | /// target-specific fixups just before or after major stages. Additionally, |
| 196 | /// targets have the flexibility to change pass order within a stage by |
| 197 | /// overriding default implementation of add%Stage routines below. Each |
| 198 | /// technique has maintainability tradeoffs because alternate pass orders are |
| 199 | /// not well supported. addPre/Post works better if the target pass is easily |
| 200 | /// tied to a common pass. But if it has subtle dependencies on multiple passes, |
| 201 | /// overriding the stage instead. |
| 202 | /// |
| 203 | /// TODO: We could use a single addPre/Post(ID) hook to allow pass injection |
| 204 | /// before/after any target-independent pass. But it's currently overkill. |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 205 | void TargetPassConfig::addMachinePasses() { |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 206 | // Print the instruction selected machine code... |
| 207 | printAndVerify("After Instruction Selection"); |
| 208 | |
| 209 | // Expand pseudo-instructions emitted by ISel. |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 210 | addPass(ExpandISelPseudosID); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 211 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 212 | // Add passes that optimize machine instructions in SSA form. |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 213 | if (getOptLevel() != CodeGenOpt::None) { |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 214 | addMachineSSAOptimization(); |
| 215 | } |
| 216 | else { |
| 217 | // If the target requests it, assign local variables to stack slots relative |
| 218 | // to one another and simplify frame index references where possible. |
| 219 | addPass(LocalStackSlotAllocationID); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // Run pre-ra passes. |
| 223 | if (addPreRegAlloc()) |
| 224 | printAndVerify("After PreRegAlloc passes"); |
| 225 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 226 | // Run register allocation and passes that are tightly coupled with it, |
| 227 | // including phi elimination and scheduling. |
| 228 | addRegAlloc(); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 229 | |
| 230 | // Run post-ra passes. |
| 231 | if (addPostRegAlloc()) |
| 232 | printAndVerify("After PostRegAlloc passes"); |
| 233 | |
| 234 | // Insert prolog/epilog code. Eliminate abstract frame index references... |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 235 | addPass(PrologEpilogCodeInserterID); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 236 | printAndVerify("After PrologEpilogCodeInserter"); |
| 237 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 238 | /// Add passes that optimize machine instructions after register allocation. |
| 239 | if (getOptLevel() != CodeGenOpt::None) |
| 240 | addMachineLateOptimization(); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 241 | |
| 242 | // Expand pseudo instructions before second scheduling pass. |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 243 | addPass(ExpandPostRAPseudosID); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 244 | printNoVerify("After ExpandPostRAPseudos"); |
| 245 | |
| 246 | // Run pre-sched2 passes. |
| 247 | if (addPreSched2()) |
| 248 | printNoVerify("After PreSched2 passes"); |
| 249 | |
| 250 | // Second pass scheduler. |
| 251 | if (getOptLevel() != CodeGenOpt::None && !DisablePostRA) { |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 252 | addPass(PostRASchedulerID); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 253 | printNoVerify("After PostRAScheduler"); |
| 254 | } |
| 255 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 256 | // GC |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 257 | addPass(GCMachineCodeAnalysisID); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 258 | if (PrintGCInfo) |
| 259 | PM.add(createGCInfoPrinter(dbgs())); |
| 260 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 261 | // Basic block placement. |
| 262 | if (getOptLevel() != CodeGenOpt::None && !DisableCodePlace) |
| 263 | addBlockPlacement(); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 264 | |
| 265 | if (addPreEmitPass()) |
| 266 | printNoVerify("After PreEmit passes"); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 269 | /// Add passes that optimize machine instructions in SSA form. |
| 270 | void TargetPassConfig::addMachineSSAOptimization() { |
| 271 | // Pre-ra tail duplication. |
| 272 | if (!DisableEarlyTailDup) { |
| 273 | addPass(TailDuplicateID); |
| 274 | printAndVerify("After Pre-RegAlloc TailDuplicate"); |
| 275 | } |
| 276 | |
| 277 | // Optimize PHIs before DCE: removing dead PHI cycles may make more |
| 278 | // instructions dead. |
| 279 | addPass(OptimizePHIsID); |
| 280 | |
| 281 | // If the target requests it, assign local variables to stack slots relative |
| 282 | // to one another and simplify frame index references where possible. |
| 283 | addPass(LocalStackSlotAllocationID); |
| 284 | |
| 285 | // With optimization, dead code should already be eliminated. However |
| 286 | // there is one known exception: lowered code for arguments that are only |
| 287 | // used by tail calls, where the tail calls reuse the incoming stack |
| 288 | // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll). |
| 289 | if (!DisableMachineDCE) |
| 290 | addPass(DeadMachineInstructionElimID); |
| 291 | printAndVerify("After codegen DCE pass"); |
| 292 | |
| 293 | if (!DisableMachineLICM) |
| 294 | addPass(MachineLICMID); |
| 295 | if (!DisableMachineCSE) |
| 296 | addPass(MachineCSEID); |
| 297 | if (!DisableMachineSink) |
| 298 | addPass(MachineSinkingID); |
| 299 | printAndVerify("After Machine LICM, CSE and Sinking passes"); |
| 300 | |
| 301 | addPass(PeepholeOptimizerID); |
| 302 | printAndVerify("After codegen peephole optimization pass"); |
| 303 | } |
| 304 | |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 305 | //===---------------------------------------------------------------------===// |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 306 | /// Register Allocation Pass Configuration |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 307 | //===---------------------------------------------------------------------===// |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 308 | |
| 309 | /// RegisterRegAlloc's global Registry tracks allocator registration. |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 310 | MachinePassRegistry RegisterRegAlloc::Registry; |
| 311 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 312 | /// A dummy default pass factory indicates whether the register allocator is |
| 313 | /// overridden on the command line. |
Jakob Stoklund Olesen | 700bfad | 2010-05-27 23:57:25 +0000 | [diff] [blame] | 314 | static FunctionPass *createDefaultRegisterAllocator() { return 0; } |
| 315 | static RegisterRegAlloc |
| 316 | defaultRegAlloc("default", |
| 317 | "pick register allocator based on -O option", |
| 318 | createDefaultRegisterAllocator); |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 319 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 320 | /// -regalloc=... command line option. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 321 | static cl::opt<RegisterRegAlloc::FunctionPassCtor, false, |
| 322 | RegisterPassParser<RegisterRegAlloc> > |
| 323 | RegAlloc("regalloc", |
Jakob Stoklund Olesen | 700bfad | 2010-05-27 23:57:25 +0000 | [diff] [blame] | 324 | cl::init(&createDefaultRegisterAllocator), |
| 325 | cl::desc("Register allocator to use")); |
Alkis Evlogimenos | 7237ece | 2003-10-02 16:57:49 +0000 | [diff] [blame] | 326 | |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 327 | |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 328 | /// createRegisterAllocator - choose the appropriate register allocator. |
Jakob Stoklund Olesen | 700bfad | 2010-05-27 23:57:25 +0000 | [diff] [blame] | 329 | FunctionPass *llvm::createRegisterAllocator(CodeGenOpt::Level OptLevel) { |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 330 | RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault(); |
Jakob Stoklund Olesen | 700bfad | 2010-05-27 23:57:25 +0000 | [diff] [blame] | 331 | |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 332 | if (!Ctor) { |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 333 | Ctor = RegAlloc; |
| 334 | RegisterRegAlloc::setDefault(RegAlloc); |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 335 | } |
Jakob Stoklund Olesen | 700bfad | 2010-05-27 23:57:25 +0000 | [diff] [blame] | 336 | |
| 337 | if (Ctor != createDefaultRegisterAllocator) |
| 338 | return Ctor(); |
| 339 | |
| 340 | // When the 'default' allocator is requested, pick one based on OptLevel. |
| 341 | switch (OptLevel) { |
| 342 | case CodeGenOpt::None: |
Jakob Stoklund Olesen | 8b89c64 | 2010-06-03 00:39:06 +0000 | [diff] [blame] | 343 | return createFastRegisterAllocator(); |
Jakob Stoklund Olesen | 700bfad | 2010-05-27 23:57:25 +0000 | [diff] [blame] | 344 | default: |
Jakob Stoklund Olesen | 5aa3211 | 2011-04-30 01:37:54 +0000 | [diff] [blame] | 345 | return createGreedyRegisterAllocator(); |
Jakob Stoklund Olesen | 700bfad | 2010-05-27 23:57:25 +0000 | [diff] [blame] | 346 | } |
Jim Laskey | 33a0a6d | 2006-07-27 20:05:00 +0000 | [diff] [blame] | 347 | } |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 348 | |
| 349 | /// Add standard target-independent passes that are tightly coupled with |
| 350 | /// register allocation, including coalescing, machine instruction scheduling, |
| 351 | /// and register allocation itself. |
| 352 | /// |
| 353 | /// FIXME: This will become the register allocation "super pass" pipeline. |
| 354 | void TargetPassConfig::addRegAlloc() { |
| 355 | // Perform register allocation. |
| 356 | PM.add(createRegisterAllocator(getOptLevel())); |
| 357 | printAndVerify("After Register Allocation"); |
| 358 | |
| 359 | // Perform stack slot coloring and post-ra machine LICM. |
| 360 | if (getOptLevel() != CodeGenOpt::None) { |
| 361 | // FIXME: Re-enable coloring with register when it's capable of adding |
| 362 | // kill markers. |
| 363 | if (!DisableSSC) |
| 364 | addPass(StackSlotColoringID); |
| 365 | |
| 366 | // Run post-ra machine LICM to hoist reloads / remats. |
| 367 | // |
| 368 | // FIXME: can this move into MachineLateOptimization? |
| 369 | if (!DisablePostRAMachineLICM) |
| 370 | addPass(MachineLICMID); |
| 371 | |
| 372 | printAndVerify("After StackSlotColoring and postra Machine LICM"); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | //===---------------------------------------------------------------------===// |
| 377 | /// Post RegAlloc Pass Configuration |
| 378 | //===---------------------------------------------------------------------===// |
| 379 | |
| 380 | /// Add passes that optimize machine instructions after register allocation. |
| 381 | void TargetPassConfig::addMachineLateOptimization() { |
| 382 | // Branch folding must be run after regalloc and prolog/epilog insertion. |
| 383 | if (!DisableBranchFold) { |
| 384 | addPass(BranchFolderPassID); |
| 385 | printNoVerify("After BranchFolding"); |
| 386 | } |
| 387 | |
| 388 | // Tail duplication. |
| 389 | if (!DisableTailDuplicate) { |
| 390 | addPass(TailDuplicateID); |
| 391 | printNoVerify("After TailDuplicate"); |
| 392 | } |
| 393 | |
| 394 | // Copy propagation. |
| 395 | if (!DisableCopyProp) { |
| 396 | addPass(MachineCopyPropagationID); |
| 397 | printNoVerify("After copy propagation pass"); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /// Add standard basic block placement passes. |
| 402 | void TargetPassConfig::addBlockPlacement() { |
| 403 | if (EnableBlockPlacement) { |
| 404 | // MachineBlockPlacement is an experimental pass which is disabled by |
| 405 | // default currently. Eventually it should subsume CodePlacementOpt, so |
| 406 | // when enabled, the other is disabled. |
| 407 | addPass(MachineBlockPlacementID); |
| 408 | printNoVerify("After MachineBlockPlacement"); |
| 409 | } else { |
| 410 | addPass(CodePlacementOptID); |
| 411 | printNoVerify("After CodePlacementOpt"); |
| 412 | } |
| 413 | |
| 414 | // Run a separate pass to collect block placement statistics. |
| 415 | if (EnableBlockPlacementStats) { |
| 416 | addPass(MachineBlockPlacementStatsID); |
| 417 | printNoVerify("After MachineBlockPlacementStats"); |
| 418 | } |
| 419 | } |