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