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