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" |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCAsmInfo.h" |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 26 | #include "llvm/Assembly/PrintModulePass.h" |
| 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/Debug.h" |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ErrorHandling.h" |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 30 | |
Chris Lattner | aa4c91f | 2003-12-28 07:59:53 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 33 | static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden, |
| 34 | cl::desc("Disable Post Regalloc")); |
| 35 | static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden, |
| 36 | cl::desc("Disable branch folding")); |
| 37 | static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden, |
| 38 | cl::desc("Disable tail duplication")); |
| 39 | static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden, |
| 40 | cl::desc("Disable pre-register allocation tail duplication")); |
Chandler Carruth | 9e67db4 | 2012-04-16 13:49:17 +0000 | [diff] [blame] | 41 | static cl::opt<bool> DisableBlockPlacement("disable-block-placement", |
| 42 | cl::Hidden, cl::desc("Disable the probability-driven block placement, and " |
| 43 | "re-enable the old code placement pass")); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 44 | static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats", |
| 45 | cl::Hidden, cl::desc("Collect probability-driven block placement stats")); |
| 46 | static cl::opt<bool> DisableCodePlace("disable-code-place", cl::Hidden, |
| 47 | cl::desc("Disable code placement")); |
| 48 | static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden, |
| 49 | cl::desc("Disable Stack Slot Coloring")); |
| 50 | static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden, |
| 51 | cl::desc("Disable Machine Dead Code Elimination")); |
| 52 | static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden, |
| 53 | cl::desc("Disable Machine LICM")); |
| 54 | static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden, |
| 55 | cl::desc("Disable Machine Common Subexpression Elimination")); |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 56 | static cl::opt<cl::boolOrDefault> |
| 57 | OptimizeRegAlloc("optimize-regalloc", cl::Hidden, |
| 58 | cl::desc("Enable optimized register allocation compilation path.")); |
Andrew Trick | 746f24b | 2012-02-11 07:11:32 +0000 | [diff] [blame] | 59 | static cl::opt<cl::boolOrDefault> |
| 60 | EnableMachineSched("enable-misched", cl::Hidden, |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 61 | cl::desc("Enable the machine instruction scheduling pass.")); |
| 62 | static cl::opt<bool> EnableStrongPHIElim("strong-phi-elim", cl::Hidden, |
| 63 | cl::desc("Use strong PHI elimination.")); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 64 | static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm", |
| 65 | cl::Hidden, |
| 66 | cl::desc("Disable Machine LICM")); |
| 67 | static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden, |
| 68 | cl::desc("Disable Machine Sinking")); |
| 69 | static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden, |
| 70 | cl::desc("Disable Loop Strength Reduction Pass")); |
| 71 | static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden, |
| 72 | cl::desc("Disable Codegen Prepare")); |
| 73 | static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden, |
Evan Cheng | 01b623c | 2012-02-20 23:28:17 +0000 | [diff] [blame] | 74 | cl::desc("Disable Copy Propagation pass")); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 75 | static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden, |
| 76 | cl::desc("Print LLVM IR produced by the loop-reduce pass")); |
| 77 | static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden, |
| 78 | cl::desc("Print LLVM IR input to isel pass")); |
| 79 | static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden, |
| 80 | cl::desc("Dump garbage collector data")); |
| 81 | static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden, |
| 82 | cl::desc("Verify generated machine code"), |
| 83 | cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL)); |
Bob Wilson | 6e1b812 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 84 | static cl::opt<std::string> |
| 85 | PrintMachineInstrs("print-machineinstrs", cl::ValueOptional, |
| 86 | cl::desc("Print machine instrs"), |
| 87 | cl::value_desc("pass-name"), cl::init("option-unspecified")); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 88 | |
Andrew Trick | 79bf288 | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 89 | /// Allow standard passes to be disabled by command line options. This supports |
| 90 | /// simple binary flags that either suppress the pass or do nothing. |
| 91 | /// i.e. -disable-mypass=false has no effect. |
| 92 | /// These should be converted to boolOrDefault in order to use applyOverride. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 93 | static AnalysisID applyDisable(AnalysisID PassID, bool Override) { |
Andrew Trick | 79bf288 | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 94 | if (Override) |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 95 | return 0; |
| 96 | return PassID; |
Andrew Trick | 79bf288 | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /// Allow Pass selection to be overriden by command line options. This supports |
| 100 | /// flags with ternary conditions. TargetID is passed through by default. The |
| 101 | /// pass is suppressed when the option is false. When the option is true, the |
| 102 | /// StandardID is selected if the target provides no default. |
| 103 | static AnalysisID applyOverride(AnalysisID TargetID, cl::boolOrDefault Override, |
| 104 | AnalysisID StandardID) { |
Andrew Trick | 746f24b | 2012-02-11 07:11:32 +0000 | [diff] [blame] | 105 | switch (Override) { |
| 106 | case cl::BOU_UNSET: |
Andrew Trick | 79bf288 | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 107 | return TargetID; |
Andrew Trick | 746f24b | 2012-02-11 07:11:32 +0000 | [diff] [blame] | 108 | case cl::BOU_TRUE: |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 109 | if (TargetID) |
Andrew Trick | 79bf288 | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 110 | return TargetID; |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 111 | if (StandardID == 0) |
Andrew Trick | 746f24b | 2012-02-11 07:11:32 +0000 | [diff] [blame] | 112 | report_fatal_error("Target cannot enable pass"); |
Andrew Trick | 79bf288 | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 113 | return StandardID; |
Andrew Trick | 746f24b | 2012-02-11 07:11:32 +0000 | [diff] [blame] | 114 | case cl::BOU_FALSE: |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 115 | return 0; |
Andrew Trick | 746f24b | 2012-02-11 07:11:32 +0000 | [diff] [blame] | 116 | } |
| 117 | llvm_unreachable("Invalid command line option state"); |
| 118 | } |
| 119 | |
Andrew Trick | 79bf288 | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 120 | /// Allow standard passes to be disabled by the command line, regardless of who |
| 121 | /// is adding the pass. |
| 122 | /// |
| 123 | /// StandardID is the pass identified in the standard pass pipeline and provided |
| 124 | /// to addPass(). It may be a target-specific ID in the case that the target |
| 125 | /// directly adds its own pass, but in that case we harmlessly fall through. |
| 126 | /// |
| 127 | /// TargetID is the pass that the target has configured to override StandardID. |
| 128 | /// |
| 129 | /// StandardID may be a pseudo ID. In that case TargetID is the name of the real |
| 130 | /// pass to run. This allows multiple options to control a single pass depending |
| 131 | /// on where in the pipeline that pass is added. |
| 132 | static AnalysisID overridePass(AnalysisID StandardID, AnalysisID TargetID) { |
| 133 | if (StandardID == &PostRASchedulerID) |
| 134 | return applyDisable(TargetID, DisablePostRA); |
| 135 | |
| 136 | if (StandardID == &BranchFolderPassID) |
| 137 | return applyDisable(TargetID, DisableBranchFold); |
| 138 | |
| 139 | if (StandardID == &TailDuplicateID) |
| 140 | return applyDisable(TargetID, DisableTailDuplicate); |
| 141 | |
| 142 | if (StandardID == &TargetPassConfig::EarlyTailDuplicateID) |
| 143 | return applyDisable(TargetID, DisableEarlyTailDup); |
| 144 | |
| 145 | if (StandardID == &MachineBlockPlacementID) |
| 146 | return applyDisable(TargetID, DisableCodePlace); |
| 147 | |
| 148 | if (StandardID == &CodePlacementOptID) |
| 149 | return applyDisable(TargetID, DisableCodePlace); |
| 150 | |
| 151 | if (StandardID == &StackSlotColoringID) |
| 152 | return applyDisable(TargetID, DisableSSC); |
| 153 | |
| 154 | if (StandardID == &DeadMachineInstructionElimID) |
| 155 | return applyDisable(TargetID, DisableMachineDCE); |
| 156 | |
| 157 | if (StandardID == &MachineLICMID) |
| 158 | return applyDisable(TargetID, DisableMachineLICM); |
| 159 | |
| 160 | if (StandardID == &MachineCSEID) |
| 161 | return applyDisable(TargetID, DisableMachineCSE); |
| 162 | |
| 163 | if (StandardID == &MachineSchedulerID) |
| 164 | return applyOverride(TargetID, EnableMachineSched, StandardID); |
| 165 | |
| 166 | if (StandardID == &TargetPassConfig::PostRAMachineLICMID) |
| 167 | return applyDisable(TargetID, DisablePostRAMachineLICM); |
| 168 | |
| 169 | if (StandardID == &MachineSinkingID) |
| 170 | return applyDisable(TargetID, DisableMachineSink); |
| 171 | |
| 172 | if (StandardID == &MachineCopyPropagationID) |
| 173 | return applyDisable(TargetID, DisableCopyProp); |
| 174 | |
| 175 | return TargetID; |
| 176 | } |
| 177 | |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 178 | //===---------------------------------------------------------------------===// |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 179 | /// TargetPassConfig |
| 180 | //===---------------------------------------------------------------------===// |
| 181 | |
| 182 | INITIALIZE_PASS(TargetPassConfig, "targetpassconfig", |
| 183 | "Target Pass Configuration", false, false) |
| 184 | char TargetPassConfig::ID = 0; |
| 185 | |
Andrew Trick | 79bf288 | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 186 | // Pseudo Pass IDs. |
| 187 | char TargetPassConfig::EarlyTailDuplicateID = 0; |
| 188 | char TargetPassConfig::PostRAMachineLICMID = 0; |
| 189 | |
Andrew Trick | 5e108ee | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 190 | namespace llvm { |
| 191 | class PassConfigImpl { |
| 192 | public: |
| 193 | // List of passes explicitly substituted by this target. Normally this is |
| 194 | // empty, but it is a convenient way to suppress or replace specific passes |
| 195 | // that are part of a standard pass pipeline without overridding the entire |
| 196 | // pipeline. This mechanism allows target options to inherit a standard pass's |
| 197 | // user interface. For example, a target may disable a standard pass by |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 198 | // default by substituting a pass ID of zero, and the user may still enable |
| 199 | // that standard pass with an explicit command line option. |
Andrew Trick | 5e108ee | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 200 | DenseMap<AnalysisID,AnalysisID> TargetPasses; |
Bob Wilson | 6e1b812 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 201 | |
| 202 | /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass |
| 203 | /// is inserted after each instance of the first one. |
| 204 | SmallVector<std::pair<AnalysisID, AnalysisID>, 4> InsertedPasses; |
Andrew Trick | 5e108ee | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 205 | }; |
| 206 | } // namespace llvm |
| 207 | |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 208 | // Out of line virtual method. |
Andrew Trick | 5e108ee | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 209 | TargetPassConfig::~TargetPassConfig() { |
| 210 | delete Impl; |
| 211 | } |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 212 | |
Andrew Trick | 61f1e3d | 2012-02-08 21:22:48 +0000 | [diff] [blame] | 213 | // Out of line constructor provides default values for pass options and |
| 214 | // registers all common codegen passes. |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 215 | TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm) |
Bob Wilson | 30a507a | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 216 | : ImmutablePass(ID), PM(&pm), StartAfter(0), StopAfter(0), |
| 217 | Started(true), Stopped(false), TM(tm), Impl(0), Initialized(false), |
Andrew Trick | ffea03f | 2012-02-08 21:22:39 +0000 | [diff] [blame] | 218 | DisableVerify(false), |
| 219 | EnableTailMerge(true) { |
| 220 | |
Andrew Trick | 5e108ee | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 221 | Impl = new PassConfigImpl(); |
| 222 | |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 223 | // Register all target independent codegen passes to activate their PassIDs, |
| 224 | // including this pass itself. |
| 225 | initializeCodeGen(*PassRegistry::getPassRegistry()); |
Andrew Trick | 79bf288 | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 226 | |
| 227 | // Substitute Pseudo Pass IDs for real ones. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 228 | substitutePass(&EarlyTailDuplicateID, &TailDuplicateID); |
| 229 | substitutePass(&PostRAMachineLICMID, &MachineLICMID); |
Andrew Trick | 79bf288 | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 230 | |
| 231 | // Temporarily disable experimental passes. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 232 | substitutePass(&MachineSchedulerID, 0); |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Bob Wilson | 6e1b812 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 235 | /// Insert InsertedPassID pass after TargetPassID. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 236 | void TargetPassConfig::insertPass(AnalysisID TargetPassID, |
| 237 | AnalysisID InsertedPassID) { |
| 238 | assert(TargetPassID != InsertedPassID && "Insert a pass after itself!"); |
| 239 | std::pair<AnalysisID, AnalysisID> P(TargetPassID, InsertedPassID); |
Bob Wilson | 6e1b812 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 240 | Impl->InsertedPasses.push_back(P); |
| 241 | } |
| 242 | |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 243 | /// createPassConfig - Create a pass configuration object to be used by |
| 244 | /// addPassToEmitX methods for generating a pipeline of CodeGen passes. |
| 245 | /// |
| 246 | /// Targets may override this to extend TargetPassConfig. |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 247 | TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) { |
| 248 | return new TargetPassConfig(this, PM); |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | TargetPassConfig::TargetPassConfig() |
Bill Wendling | 7c4ce30 | 2012-05-01 08:27:43 +0000 | [diff] [blame] | 252 | : ImmutablePass(ID), PM(0) { |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 253 | llvm_unreachable("TargetPassConfig should not be constructed on-the-fly"); |
| 254 | } |
| 255 | |
Andrew Trick | ffea03f | 2012-02-08 21:22:39 +0000 | [diff] [blame] | 256 | // Helper to verify the analysis is really immutable. |
| 257 | void TargetPassConfig::setOpt(bool &Opt, bool Val) { |
| 258 | assert(!Initialized && "PassConfig is immutable"); |
| 259 | Opt = Val; |
| 260 | } |
| 261 | |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 262 | void TargetPassConfig::substitutePass(AnalysisID StandardID, |
| 263 | AnalysisID TargetID) { |
| 264 | Impl->TargetPasses[StandardID] = TargetID; |
Andrew Trick | 5e108ee | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 265 | } |
Andrew Trick | 746f24b | 2012-02-11 07:11:32 +0000 | [diff] [blame] | 266 | |
Andrew Trick | 5e108ee | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 267 | AnalysisID TargetPassConfig::getPassSubstitution(AnalysisID ID) const { |
| 268 | DenseMap<AnalysisID, AnalysisID>::const_iterator |
| 269 | I = Impl->TargetPasses.find(ID); |
| 270 | if (I == Impl->TargetPasses.end()) |
| 271 | return ID; |
| 272 | return I->second; |
| 273 | } |
| 274 | |
Bob Wilson | 30a507a | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 275 | /// Add a pass to the PassManager if that pass is supposed to be run. If the |
| 276 | /// Started/Stopped flags indicate either that the compilation should start at |
| 277 | /// a later pass or that it should stop after an earlier pass, then do not add |
| 278 | /// the pass. Finally, compare the current pass against the StartAfter |
| 279 | /// and StopAfter options and change the Started/Stopped flags accordingly. |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 280 | void TargetPassConfig::addPass(Pass *P) { |
Bob Wilson | 6b2bb15 | 2012-07-02 19:48:39 +0000 | [diff] [blame] | 281 | assert(!Initialized && "PassConfig is immutable"); |
| 282 | |
Chandler Carruth | 6068c48 | 2012-07-02 22:56:41 +0000 | [diff] [blame^] | 283 | // Cache the Pass ID here in case the pass manager finds this pass is |
| 284 | // redundant with ones already scheduled / available, and deletes it. |
| 285 | // Fundamentally, once we add the pass to the manager, we no longer own it |
| 286 | // and shouldn't reference it. |
| 287 | AnalysisID PassID = P->getPassID(); |
| 288 | |
Bob Wilson | 30a507a | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 289 | if (Started && !Stopped) |
| 290 | PM->add(P); |
Chandler Carruth | 6068c48 | 2012-07-02 22:56:41 +0000 | [diff] [blame^] | 291 | if (StopAfter == PassID) |
Bob Wilson | 30a507a | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 292 | Stopped = true; |
Chandler Carruth | 6068c48 | 2012-07-02 22:56:41 +0000 | [diff] [blame^] | 293 | if (StartAfter == PassID) |
Bob Wilson | 30a507a | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 294 | Started = true; |
| 295 | if (Stopped && !Started) |
| 296 | report_fatal_error("Cannot stop compilation after pass that is not run"); |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Andrew Trick | 5e108ee | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 299 | /// Add a CodeGen pass at this point in the pipeline after checking for target |
| 300 | /// and command line overrides. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 301 | AnalysisID TargetPassConfig::addPass(AnalysisID PassID) { |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 302 | AnalysisID TargetID = getPassSubstitution(PassID); |
| 303 | AnalysisID FinalID = overridePass(PassID, TargetID); |
| 304 | if (FinalID == 0) |
Andrew Trick | 5e108ee | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 305 | return FinalID; |
| 306 | |
| 307 | Pass *P = Pass::createPass(FinalID); |
Andrew Trick | ebe18ef | 2012-02-08 21:22:34 +0000 | [diff] [blame] | 308 | if (!P) |
| 309 | llvm_unreachable("Pass ID not registered"); |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 310 | addPass(P); |
Bob Wilson | 6e1b812 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 311 | // Add the passes after the pass P if there is any. |
| 312 | for (SmallVector<std::pair<AnalysisID, AnalysisID>, 4>::iterator |
| 313 | I = Impl->InsertedPasses.begin(), E = Impl->InsertedPasses.end(); |
| 314 | I != E; ++I) { |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 315 | if ((*I).first == PassID) { |
Bob Wilson | 6e1b812 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 316 | assert((*I).second && "Illegal Pass ID!"); |
| 317 | Pass *NP = Pass::createPass((*I).second); |
| 318 | assert(NP && "Pass ID not registered"); |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 319 | addPass(NP); |
Bob Wilson | 6e1b812 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
Andrew Trick | 5e108ee | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 322 | return FinalID; |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 323 | } |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 324 | |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 325 | void TargetPassConfig::printAndVerify(const char *Banner) { |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 326 | if (TM->shouldPrintMachineCode()) |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 327 | addPass(createMachineFunctionPrinterPass(dbgs(), Banner)); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 328 | |
| 329 | if (VerifyMachineCode) |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 330 | addPass(createMachineVerifierPass(Banner)); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 333 | /// Add common target configurable passes that perform LLVM IR to IR transforms |
| 334 | /// following machine independent optimization. |
| 335 | void TargetPassConfig::addIRPasses() { |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 336 | // Basic AliasAnalysis support. |
| 337 | // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that |
| 338 | // BasicAliasAnalysis wins if they disagree. This is intended to help |
| 339 | // support "obvious" type-punning idioms. |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 340 | addPass(createTypeBasedAliasAnalysisPass()); |
| 341 | addPass(createBasicAliasAnalysisPass()); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 342 | |
| 343 | // Before running any passes, run the verifier to determine if the input |
| 344 | // coming from the front-end and/or optimizer is valid. |
| 345 | if (!DisableVerify) |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 346 | addPass(createVerifierPass()); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 347 | |
| 348 | // Run loop strength reduction before anything else. |
| 349 | if (getOptLevel() != CodeGenOpt::None && !DisableLSR) { |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 350 | addPass(createLoopStrengthReducePass(getTargetLowering())); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 351 | if (PrintLSR) |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 352 | addPass(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs())); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 355 | addPass(createGCLoweringPass()); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 356 | |
| 357 | // Make sure that no unreachable blocks are instruction selected. |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 358 | addPass(createUnreachableBlockEliminationPass()); |
| 359 | } |
| 360 | |
| 361 | /// Turn exception handling constructs into something the code generators can |
| 362 | /// handle. |
| 363 | void TargetPassConfig::addPassesToHandleExceptions() { |
| 364 | switch (TM->getMCAsmInfo()->getExceptionHandlingType()) { |
| 365 | case ExceptionHandling::SjLj: |
| 366 | // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both |
| 367 | // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise, |
| 368 | // catch info can get misplaced when a selector ends up more than one block |
| 369 | // removed from the parent invoke(s). This could happen when a landing |
| 370 | // pad is shared by multiple invokes and is also a target of a normal |
| 371 | // edge from elsewhere. |
| 372 | addPass(createSjLjEHPreparePass(TM->getTargetLowering())); |
| 373 | // FALLTHROUGH |
| 374 | case ExceptionHandling::DwarfCFI: |
| 375 | case ExceptionHandling::ARM: |
| 376 | case ExceptionHandling::Win64: |
| 377 | addPass(createDwarfEHPass(TM)); |
| 378 | break; |
| 379 | case ExceptionHandling::None: |
| 380 | addPass(createLowerInvokePass(TM->getTargetLowering())); |
| 381 | |
| 382 | // The lower invoke pass may create unreachable code. Remove it. |
| 383 | addPass(createUnreachableBlockEliminationPass()); |
| 384 | break; |
| 385 | } |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 386 | } |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 387 | |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 388 | /// Add common passes that perform LLVM IR to IR transforms in preparation for |
| 389 | /// instruction selection. |
| 390 | void TargetPassConfig::addISelPrepare() { |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 391 | if (getOptLevel() != CodeGenOpt::None && !DisableCGP) |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 392 | addPass(createCodeGenPreparePass(getTargetLowering())); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 393 | |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 394 | addPass(createStackProtectorPass(getTargetLowering())); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 395 | |
| 396 | addPreISel(); |
| 397 | |
| 398 | if (PrintISelInput) |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 399 | addPass(createPrintFunctionPass("\n\n" |
Bill Wendling | 7c4ce30 | 2012-05-01 08:27:43 +0000 | [diff] [blame] | 400 | "*** Final LLVM Code input to ISel ***\n", |
| 401 | &dbgs())); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 402 | |
| 403 | // All passes which modify the LLVM IR are now complete; run the verifier |
| 404 | // to ensure that the IR is valid. |
| 405 | if (!DisableVerify) |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 406 | addPass(createVerifierPass()); |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 407 | } |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 408 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 409 | /// Add the complete set of target-independent postISel code generator passes. |
| 410 | /// |
| 411 | /// This can be read as the standard order of major LLVM CodeGen stages. Stages |
| 412 | /// with nontrivial configuration or multiple passes are broken out below in |
| 413 | /// add%Stage routines. |
| 414 | /// |
| 415 | /// Any TargetPassConfig::addXX routine may be overriden by the Target. The |
| 416 | /// addPre/Post methods with empty header implementations allow injecting |
| 417 | /// target-specific fixups just before or after major stages. Additionally, |
| 418 | /// targets have the flexibility to change pass order within a stage by |
| 419 | /// overriding default implementation of add%Stage routines below. Each |
| 420 | /// technique has maintainability tradeoffs because alternate pass orders are |
| 421 | /// not well supported. addPre/Post works better if the target pass is easily |
| 422 | /// tied to a common pass. But if it has subtle dependencies on multiple passes, |
Andrew Trick | 06efdd2 | 2012-02-10 07:08:25 +0000 | [diff] [blame] | 423 | /// the target should override the stage instead. |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 424 | /// |
| 425 | /// TODO: We could use a single addPre/Post(ID) hook to allow pass injection |
| 426 | /// before/after any target-independent pass. But it's currently overkill. |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 427 | void TargetPassConfig::addMachinePasses() { |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 428 | // Print the instruction selected machine code... |
| 429 | printAndVerify("After Instruction Selection"); |
| 430 | |
Bob Wilson | 6e1b812 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 431 | // Insert a machine instr printer pass after the specified pass. |
| 432 | // If -print-machineinstrs specified, print machineinstrs after all passes. |
| 433 | if (StringRef(PrintMachineInstrs.getValue()).equals("")) |
| 434 | TM->Options.PrintMachineCode = true; |
| 435 | else if (!StringRef(PrintMachineInstrs.getValue()) |
| 436 | .equals("option-unspecified")) { |
| 437 | const PassRegistry *PR = PassRegistry::getPassRegistry(); |
| 438 | const PassInfo *TPI = PR->getPassInfo(PrintMachineInstrs.getValue()); |
| 439 | const PassInfo *IPI = PR->getPassInfo(StringRef("print-machineinstrs")); |
| 440 | assert (TPI && IPI && "Pass ID not registered!"); |
| 441 | const char *TID = (char *)(TPI->getTypeInfo()); |
| 442 | const char *IID = (char *)(IPI->getTypeInfo()); |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 443 | insertPass(TID, IID); |
Bob Wilson | 6e1b812 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 446 | // Expand pseudo-instructions emitted by ISel. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 447 | addPass(&ExpandISelPseudosID); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 448 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 449 | // Add passes that optimize machine instructions in SSA form. |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 450 | if (getOptLevel() != CodeGenOpt::None) { |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 451 | addMachineSSAOptimization(); |
| 452 | } |
| 453 | else { |
| 454 | // If the target requests it, assign local variables to stack slots relative |
| 455 | // to one another and simplify frame index references where possible. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 456 | addPass(&LocalStackSlotAllocationID); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | // Run pre-ra passes. |
| 460 | if (addPreRegAlloc()) |
| 461 | printAndVerify("After PreRegAlloc passes"); |
| 462 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 463 | // Run register allocation and passes that are tightly coupled with it, |
| 464 | // including phi elimination and scheduling. |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 465 | if (getOptimizeRegAlloc()) |
| 466 | addOptimizedRegAlloc(createRegAllocPass(true)); |
| 467 | else |
| 468 | addFastRegAlloc(createRegAllocPass(false)); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 469 | |
| 470 | // Run post-ra passes. |
| 471 | if (addPostRegAlloc()) |
| 472 | printAndVerify("After PostRegAlloc passes"); |
| 473 | |
| 474 | // Insert prolog/epilog code. Eliminate abstract frame index references... |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 475 | addPass(&PrologEpilogCodeInserterID); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 476 | printAndVerify("After PrologEpilogCodeInserter"); |
| 477 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 478 | /// Add passes that optimize machine instructions after register allocation. |
| 479 | if (getOptLevel() != CodeGenOpt::None) |
| 480 | addMachineLateOptimization(); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 481 | |
| 482 | // Expand pseudo instructions before second scheduling pass. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 483 | addPass(&ExpandPostRAPseudosID); |
Jakob Stoklund Olesen | 2ef5bf6 | 2012-03-28 20:49:30 +0000 | [diff] [blame] | 484 | printAndVerify("After ExpandPostRAPseudos"); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 485 | |
| 486 | // Run pre-sched2 passes. |
| 487 | if (addPreSched2()) |
Jakob Stoklund Olesen | 7881166 | 2012-03-28 23:31:15 +0000 | [diff] [blame] | 488 | printAndVerify("After PreSched2 passes"); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 489 | |
| 490 | // Second pass scheduler. |
Andrew Trick | 79bf288 | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 491 | if (getOptLevel() != CodeGenOpt::None) { |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 492 | addPass(&PostRASchedulerID); |
Jakob Stoklund Olesen | 8b4c502 | 2012-03-28 23:54:28 +0000 | [diff] [blame] | 493 | printAndVerify("After PostRAScheduler"); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 496 | // GC |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 497 | addPass(&GCMachineCodeAnalysisID); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 498 | if (PrintGCInfo) |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 499 | addPass(createGCInfoPrinter(dbgs())); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 500 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 501 | // Basic block placement. |
Andrew Trick | 79bf288 | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 502 | if (getOptLevel() != CodeGenOpt::None) |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 503 | addBlockPlacement(); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 504 | |
| 505 | if (addPreEmitPass()) |
Jakob Stoklund Olesen | 8b4c502 | 2012-03-28 23:54:28 +0000 | [diff] [blame] | 506 | printAndVerify("After PreEmit passes"); |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 509 | /// Add passes that optimize machine instructions in SSA form. |
| 510 | void TargetPassConfig::addMachineSSAOptimization() { |
| 511 | // Pre-ra tail duplication. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 512 | if (addPass(&EarlyTailDuplicateID)) |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 513 | printAndVerify("After Pre-RegAlloc TailDuplicate"); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 514 | |
| 515 | // Optimize PHIs before DCE: removing dead PHI cycles may make more |
| 516 | // instructions dead. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 517 | addPass(&OptimizePHIsID); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 518 | |
| 519 | // If the target requests it, assign local variables to stack slots relative |
| 520 | // to one another and simplify frame index references where possible. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 521 | addPass(&LocalStackSlotAllocationID); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 522 | |
| 523 | // With optimization, dead code should already be eliminated. However |
| 524 | // there is one known exception: lowered code for arguments that are only |
| 525 | // used by tail calls, where the tail calls reuse the incoming stack |
| 526 | // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll). |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 527 | addPass(&DeadMachineInstructionElimID); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 528 | printAndVerify("After codegen DCE pass"); |
| 529 | |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 530 | addPass(&MachineLICMID); |
| 531 | addPass(&MachineCSEID); |
| 532 | addPass(&MachineSinkingID); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 533 | printAndVerify("After Machine LICM, CSE and Sinking passes"); |
| 534 | |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 535 | addPass(&PeepholeOptimizerID); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 536 | printAndVerify("After codegen peephole optimization pass"); |
| 537 | } |
| 538 | |
Andrew Trick | 7461334 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 539 | //===---------------------------------------------------------------------===// |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 540 | /// Register Allocation Pass Configuration |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 541 | //===---------------------------------------------------------------------===// |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 542 | |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 543 | bool TargetPassConfig::getOptimizeRegAlloc() const { |
| 544 | switch (OptimizeRegAlloc) { |
| 545 | case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None; |
| 546 | case cl::BOU_TRUE: return true; |
| 547 | case cl::BOU_FALSE: return false; |
| 548 | } |
| 549 | llvm_unreachable("Invalid optimize-regalloc state"); |
| 550 | } |
| 551 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 552 | /// RegisterRegAlloc's global Registry tracks allocator registration. |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 553 | MachinePassRegistry RegisterRegAlloc::Registry; |
| 554 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 555 | /// A dummy default pass factory indicates whether the register allocator is |
| 556 | /// overridden on the command line. |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 557 | static FunctionPass *useDefaultRegisterAllocator() { return 0; } |
Jakob Stoklund Olesen | 700bfad | 2010-05-27 23:57:25 +0000 | [diff] [blame] | 558 | static RegisterRegAlloc |
| 559 | defaultRegAlloc("default", |
| 560 | "pick register allocator based on -O option", |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 561 | useDefaultRegisterAllocator); |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 562 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 563 | /// -regalloc=... command line option. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 564 | static cl::opt<RegisterRegAlloc::FunctionPassCtor, false, |
| 565 | RegisterPassParser<RegisterRegAlloc> > |
| 566 | RegAlloc("regalloc", |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 567 | cl::init(&useDefaultRegisterAllocator), |
Jakob Stoklund Olesen | 700bfad | 2010-05-27 23:57:25 +0000 | [diff] [blame] | 568 | cl::desc("Register allocator to use")); |
Alkis Evlogimenos | 7237ece | 2003-10-02 16:57:49 +0000 | [diff] [blame] | 569 | |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 570 | |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 571 | /// Instantiate the default register allocator pass for this target for either |
| 572 | /// the optimized or unoptimized allocation path. This will be added to the pass |
| 573 | /// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc |
| 574 | /// in the optimized case. |
| 575 | /// |
| 576 | /// A target that uses the standard regalloc pass order for fast or optimized |
| 577 | /// allocation may still override this for per-target regalloc |
| 578 | /// selection. But -regalloc=... always takes precedence. |
| 579 | FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) { |
| 580 | if (Optimized) |
| 581 | return createGreedyRegisterAllocator(); |
| 582 | else |
| 583 | return createFastRegisterAllocator(); |
| 584 | } |
| 585 | |
| 586 | /// Find and instantiate the register allocation pass requested by this target |
| 587 | /// at the current optimization level. Different register allocators are |
| 588 | /// defined as separate passes because they may require different analysis. |
| 589 | /// |
| 590 | /// This helper ensures that the regalloc= option is always available, |
| 591 | /// even for targets that override the default allocator. |
| 592 | /// |
| 593 | /// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs, |
| 594 | /// this can be folded into addPass. |
| 595 | FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) { |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 596 | RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault(); |
Jakob Stoklund Olesen | 700bfad | 2010-05-27 23:57:25 +0000 | [diff] [blame] | 597 | |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 598 | // Initialize the global default. |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 599 | if (!Ctor) { |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 600 | Ctor = RegAlloc; |
| 601 | RegisterRegAlloc::setDefault(RegAlloc); |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 602 | } |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 603 | if (Ctor != useDefaultRegisterAllocator) |
Jakob Stoklund Olesen | 700bfad | 2010-05-27 23:57:25 +0000 | [diff] [blame] | 604 | return Ctor(); |
| 605 | |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 606 | // With no -regalloc= override, ask the target for a regalloc pass. |
| 607 | return createTargetRegisterAllocator(Optimized); |
| 608 | } |
| 609 | |
| 610 | /// Add the minimum set of target-independent passes that are required for |
| 611 | /// register allocation. No coalescing or scheduling. |
| 612 | void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) { |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 613 | addPass(&PHIEliminationID); |
| 614 | addPass(&TwoAddressInstructionPassID); |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 615 | |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 616 | addPass(RegAllocPass); |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 617 | printAndVerify("After Register Allocation"); |
Jim Laskey | 33a0a6d | 2006-07-27 20:05:00 +0000 | [diff] [blame] | 618 | } |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 619 | |
| 620 | /// Add standard target-independent passes that are tightly coupled with |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 621 | /// optimized register allocation, including coalescing, machine instruction |
| 622 | /// scheduling, and register allocation itself. |
| 623 | void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) { |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 624 | addPass(&ProcessImplicitDefsID); |
Jakob Stoklund Olesen | 5984d2b | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 625 | |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 626 | // LiveVariables currently requires pure SSA form. |
| 627 | // |
| 628 | // FIXME: Once TwoAddressInstruction pass no longer uses kill flags, |
| 629 | // LiveVariables can be removed completely, and LiveIntervals can be directly |
| 630 | // computed. (We still either need to regenerate kill flags after regalloc, or |
| 631 | // preferably fix the scavenger to not depend on them). |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 632 | addPass(&LiveVariablesID); |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 633 | |
| 634 | // Add passes that move from transformed SSA into conventional SSA. This is a |
| 635 | // "copy coalescing" problem. |
| 636 | // |
| 637 | if (!EnableStrongPHIElim) { |
| 638 | // Edge splitting is smarter with machine loop info. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 639 | addPass(&MachineLoopInfoID); |
| 640 | addPass(&PHIEliminationID); |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 641 | } |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 642 | addPass(&TwoAddressInstructionPassID); |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 643 | |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 644 | if (EnableStrongPHIElim) |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 645 | addPass(&StrongPHIEliminationID); |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 646 | |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 647 | addPass(&RegisterCoalescerID); |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 648 | |
| 649 | // PreRA instruction scheduling. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 650 | if (addPass(&MachineSchedulerID)) |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 651 | printAndVerify("After Machine Scheduling"); |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 652 | |
| 653 | // Add the selected register allocation pass. |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 654 | addPass(RegAllocPass); |
Jakob Stoklund Olesen | 34f5a2b | 2012-06-26 17:09:29 +0000 | [diff] [blame] | 655 | printAndVerify("After Register Allocation, before rewriter"); |
| 656 | |
| 657 | // Allow targets to change the register assignments before rewriting. |
| 658 | if (addPreRewrite()) |
| 659 | printAndVerify("After pre-rewrite passes"); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 660 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 661 | // Finally rewrite virtual registers. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 662 | addPass(&VirtRegRewriterID); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 663 | printAndVerify("After Virtual Register Rewriter"); |
| 664 | |
Andrew Trick | 746f24b | 2012-02-11 07:11:32 +0000 | [diff] [blame] | 665 | // FinalizeRegAlloc is convenient until MachineInstrBundles is more mature, |
| 666 | // but eventually, all users of it should probably be moved to addPostRA and |
| 667 | // it can go away. Currently, it's the intended place for targets to run |
| 668 | // FinalizeMachineBundles, because passes other than MachineScheduling an |
| 669 | // RegAlloc itself may not be aware of bundles. |
| 670 | if (addFinalizeRegAlloc()) |
| 671 | printAndVerify("After RegAlloc finalization"); |
| 672 | |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 673 | // Perform stack slot coloring and post-ra machine LICM. |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 674 | // |
| 675 | // FIXME: Re-enable coloring with register when it's capable of adding |
| 676 | // kill markers. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 677 | addPass(&StackSlotColoringID); |
Andrew Trick | 900d7b7 | 2012-02-15 07:57:03 +0000 | [diff] [blame] | 678 | |
| 679 | // Run post-ra machine LICM to hoist reloads / remats. |
| 680 | // |
| 681 | // FIXME: can this move into MachineLateOptimization? |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 682 | addPass(&PostRAMachineLICMID); |
Andrew Trick | 900d7b7 | 2012-02-15 07:57:03 +0000 | [diff] [blame] | 683 | |
| 684 | printAndVerify("After StackSlotColoring and postra Machine LICM"); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | //===---------------------------------------------------------------------===// |
| 688 | /// Post RegAlloc Pass Configuration |
| 689 | //===---------------------------------------------------------------------===// |
| 690 | |
| 691 | /// Add passes that optimize machine instructions after register allocation. |
| 692 | void TargetPassConfig::addMachineLateOptimization() { |
| 693 | // Branch folding must be run after regalloc and prolog/epilog insertion. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 694 | if (addPass(&BranchFolderPassID)) |
Jakob Stoklund Olesen | 663ee20 | 2012-03-28 20:47:37 +0000 | [diff] [blame] | 695 | printAndVerify("After BranchFolding"); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 696 | |
| 697 | // Tail duplication. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 698 | if (addPass(&TailDuplicateID)) |
Jakob Stoklund Olesen | 663ee20 | 2012-03-28 20:47:37 +0000 | [diff] [blame] | 699 | printAndVerify("After TailDuplicate"); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 700 | |
| 701 | // Copy propagation. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 702 | if (addPass(&MachineCopyPropagationID)) |
Jakob Stoklund Olesen | 663ee20 | 2012-03-28 20:47:37 +0000 | [diff] [blame] | 703 | printAndVerify("After copy propagation pass"); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | /// Add standard basic block placement passes. |
| 707 | void TargetPassConfig::addBlockPlacement() { |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 708 | AnalysisID PassID = 0; |
Chandler Carruth | 9e67db4 | 2012-04-16 13:49:17 +0000 | [diff] [blame] | 709 | if (!DisableBlockPlacement) { |
| 710 | // MachineBlockPlacement is a new pass which subsumes the functionality of |
| 711 | // CodPlacementOpt. The old code placement pass can be restored by |
| 712 | // disabling block placement, but eventually it will be removed. |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 713 | PassID = addPass(&MachineBlockPlacementID); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 714 | } else { |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 715 | PassID = addPass(&CodePlacementOptID); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 716 | } |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 717 | if (PassID) { |
Andrew Trick | 79bf288 | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 718 | // Run a separate pass to collect block placement statistics. |
| 719 | if (EnableBlockPlacementStats) |
Bob Wilson | 3fb99a7 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 720 | addPass(&MachineBlockPlacementStatsID); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 721 | |
Jakob Stoklund Olesen | 8b4c502 | 2012-03-28 23:54:28 +0000 | [diff] [blame] | 722 | printAndVerify("After machine block placement."); |
Andrew Trick | f7b9631 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 723 | } |
| 724 | } |