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