Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 1 | //===- TargetPassConfig.cpp - Target independent code generation passes ---===// |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 5facafa | 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 | |
Matthias Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/TargetPassConfig.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
| 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | 17e0bc3 | 2015-08-06 07:33:15 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/CFLAndersAliasAnalysis.h" |
| 21 | #include "llvm/Analysis/CFLSteensAliasAnalysis.h" |
Mehdi Amini | bbacddf | 2016-06-10 16:19:46 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/CallGraphSCCPass.h" |
Chandler Carruth | 42ff448 | 2015-08-14 02:55:50 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ScopedNoAliasAA.h" |
Matthias Braun | c7c06f1 | 2017-06-06 00:26:13 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 1db2282 | 2015-08-14 03:33:48 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/TypeBasedAliasAnalysis.h" |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachinePassRegistry.h" |
| 28 | #include "llvm/CodeGen/Passes.h" |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/RegAllocRegistry.h" |
Chandler Carruth | b8ddc70 | 2014-01-12 11:10:32 +0000 | [diff] [blame] | 30 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 31 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Verifier.h" |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCAsmInfo.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCTargetOptions.h" |
| 35 | #include "llvm/Pass.h" |
| 36 | #include "llvm/Support/CodeGen.h" |
| 37 | #include "llvm/Support/CommandLine.h" |
| 38 | #include "llvm/Support/Compiler.h" |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
Andrew Trick | b755133 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 40 | #include "llvm/Support/ErrorHandling.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Threading.h" |
Matthias Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetMachine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 43 | #include "llvm/Transforms/Scalar.h" |
Saleem Abdulrasool | 5898e09 | 2014-11-07 21:32:08 +0000 | [diff] [blame] | 44 | #include "llvm/Transforms/Utils/SymbolRewriter.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 45 | #include <cassert> |
| 46 | #include <string> |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 27dd642 | 2003-12-28 07:59:53 +0000 | [diff] [blame] | 48 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 49 | |
Matt Arsenault | 81da0d4 | 2017-08-14 19:54:47 +0000 | [diff] [blame] | 50 | cl::opt<bool> EnableIPRA("enable-ipra", cl::init(false), cl::Hidden, |
| 51 | cl::desc("Enable interprocedural register allocation " |
| 52 | "to reduce load/store at procedure calls.")); |
Matthias Braun | e2d2ead | 2016-12-08 00:16:08 +0000 | [diff] [blame] | 53 | static cl::opt<bool> DisablePostRASched("disable-post-ra", cl::Hidden, |
| 54 | cl::desc("Disable Post Regalloc Scheduler")); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 55 | static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden, |
| 56 | cl::desc("Disable branch folding")); |
| 57 | static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden, |
| 58 | cl::desc("Disable tail duplication")); |
| 59 | static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden, |
| 60 | cl::desc("Disable pre-register allocation tail duplication")); |
Chandler Carruth | 4190b50 | 2012-04-16 13:49:17 +0000 | [diff] [blame] | 61 | static cl::opt<bool> DisableBlockPlacement("disable-block-placement", |
Benjamin Kramer | 70671b9 | 2013-03-29 17:14:24 +0000 | [diff] [blame] | 62 | cl::Hidden, cl::desc("Disable probability-driven block placement")); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 63 | static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats", |
| 64 | cl::Hidden, cl::desc("Collect probability-driven block placement stats")); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 65 | static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden, |
| 66 | cl::desc("Disable Stack Slot Coloring")); |
| 67 | static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden, |
| 68 | cl::desc("Disable Machine Dead Code Elimination")); |
Jakob Stoklund Olesen | 0f6e8bb | 2012-10-03 00:51:32 +0000 | [diff] [blame] | 69 | static cl::opt<bool> DisableEarlyIfConversion("disable-early-ifcvt", cl::Hidden, |
| 70 | cl::desc("Disable Early If-conversion")); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 71 | static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden, |
| 72 | cl::desc("Disable Machine LICM")); |
| 73 | static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden, |
| 74 | cl::desc("Disable Machine Common Subexpression Elimination")); |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 75 | static cl::opt<cl::boolOrDefault> OptimizeRegAlloc( |
| 76 | "optimize-regalloc", cl::Hidden, |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 77 | cl::desc("Enable optimized register allocation compilation path.")); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 78 | static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm", |
| 79 | cl::Hidden, |
| 80 | cl::desc("Disable Machine LICM")); |
| 81 | static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden, |
| 82 | cl::desc("Disable Machine Sinking")); |
| 83 | static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden, |
| 84 | cl::desc("Disable Loop Strength Reduction Pass")); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 85 | static cl::opt<bool> DisableConstantHoisting("disable-constant-hoisting", |
| 86 | cl::Hidden, cl::desc("Disable ConstantHoisting")); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 87 | static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden, |
| 88 | cl::desc("Disable Codegen Prepare")); |
| 89 | static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden, |
Evan Cheng | 63618f9 | 2012-02-20 23:28:17 +0000 | [diff] [blame] | 90 | cl::desc("Disable Copy Propagation pass")); |
James Molloy | bc9fed8 | 2014-07-23 13:33:00 +0000 | [diff] [blame] | 91 | static cl::opt<bool> DisablePartialLibcallInlining("disable-partial-libcall-inlining", |
| 92 | cl::Hidden, cl::desc("Disable Partial Libcall Inlining")); |
Sanjoy Das | 69fad07 | 2015-06-15 18:44:27 +0000 | [diff] [blame] | 93 | static cl::opt<bool> EnableImplicitNullChecks( |
| 94 | "enable-implicit-null-checks", |
| 95 | cl::desc("Fold null checks into faulting memory operations"), |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 96 | cl::init(false), cl::Hidden); |
| 97 | static cl::opt<bool> |
| 98 | EnableMergeICmps("enable-mergeicmps", |
| 99 | cl::desc("Merge ICmp chains into a single memcmp"), |
| 100 | cl::init(false), cl::Hidden); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 101 | static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden, |
| 102 | cl::desc("Print LLVM IR produced by the loop-reduce pass")); |
| 103 | static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden, |
| 104 | cl::desc("Print LLVM IR input to isel pass")); |
| 105 | static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden, |
| 106 | cl::desc("Dump garbage collector data")); |
| 107 | static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden, |
| 108 | cl::desc("Verify generated machine code"), |
Owen Anderson | 21b1788 | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 109 | cl::init(false), |
| 110 | cl::ZeroOrMore); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 111 | static cl::opt<bool> EnableMachineOutliner("enable-machine-outliner", |
| 112 | cl::Hidden, |
| 113 | cl::desc("Enable machine outliner")); |
Jessica Paquette | 1359384 | 2017-10-07 00:16:34 +0000 | [diff] [blame] | 114 | static cl::opt<bool> EnableLinkOnceODROutlining( |
| 115 | "enable-linkonceodr-outlining", |
| 116 | cl::Hidden, |
| 117 | cl::desc("Enable the machine outliner on linkonceodr functions"), |
| 118 | cl::init(false)); |
Matthias Braun | c7c06f1 | 2017-06-06 00:26:13 +0000 | [diff] [blame] | 119 | // Enable or disable FastISel. Both options are needed, because |
| 120 | // FastISel is enabled by default with -fast, and we wish to be |
| 121 | // able to enable or disable fast-isel independently from -O0. |
| 122 | static cl::opt<cl::boolOrDefault> |
| 123 | EnableFastISelOption("fast-isel", cl::Hidden, |
| 124 | cl::desc("Enable the \"fast\" instruction selector")); |
| 125 | |
| 126 | static cl::opt<cl::boolOrDefault> |
| 127 | EnableGlobalISel("global-isel", cl::Hidden, |
| 128 | cl::desc("Enable the \"global\" instruction selector")); |
Owen Anderson | 21b1788 | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 129 | |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 130 | static cl::opt<std::string> PrintMachineInstrs( |
| 131 | "print-machineinstrs", cl::ValueOptional, cl::desc("Print machine instrs"), |
| 132 | cl::value_desc("pass-name"), cl::init("option-unspecified"), cl::Hidden); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 133 | |
Quentin Colombet | 1c06a73 | 2016-08-31 18:43:04 +0000 | [diff] [blame] | 134 | static cl::opt<int> EnableGlobalISelAbort( |
Quentin Colombet | 0de43b2 | 2016-08-26 22:32:59 +0000 | [diff] [blame] | 135 | "global-isel-abort", cl::Hidden, |
| 136 | cl::desc("Enable abort calls when \"global\" instruction selection " |
Quentin Colombet | 1c06a73 | 2016-08-31 18:43:04 +0000 | [diff] [blame] | 137 | "fails to lower/select an instruction: 0 disable the abort, " |
| 138 | "1 enable the abort, and " |
| 139 | "2 disable the abort but emit a diagnostic on failure"), |
| 140 | cl::init(1)); |
Quentin Colombet | 0de43b2 | 2016-08-26 22:32:59 +0000 | [diff] [blame] | 141 | |
Andrew Trick | 17080b9 | 2013-12-28 21:56:51 +0000 | [diff] [blame] | 142 | // Temporary option to allow experimenting with MachineScheduler as a post-RA |
| 143 | // scheduler. Targets can "properly" enable this with |
Jonas Paulsson | e451eef | 2015-12-10 09:10:07 +0000 | [diff] [blame] | 144 | // substitutePass(&PostRASchedulerID, &PostMachineSchedulerID). |
| 145 | // Targets can return true in targetSchedulesPostRAScheduling() and |
| 146 | // insert a PostRA scheduling pass wherever it wants. |
| 147 | cl::opt<bool> MISchedPostRA("misched-postra", cl::Hidden, |
Andrew Trick | 17080b9 | 2013-12-28 21:56:51 +0000 | [diff] [blame] | 148 | cl::desc("Run MachineScheduler post regalloc (independent of preRA sched)")); |
| 149 | |
Cameron Zwarich | 71f0acb | 2013-02-10 06:42:34 +0000 | [diff] [blame] | 150 | // Experimental option to run live interval analysis early. |
Jakob Stoklund Olesen | 1c46589 | 2012-08-03 22:12:54 +0000 | [diff] [blame] | 151 | static cl::opt<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden, |
| 152 | cl::desc("Run live interval analysis earlier in the pipeline")); |
| 153 | |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 154 | // Experimental option to use CFL-AA in codegen |
| 155 | enum class CFLAAType { None, Steensgaard, Andersen, Both }; |
| 156 | static cl::opt<CFLAAType> UseCFLAA( |
| 157 | "use-cfl-aa-in-codegen", cl::init(CFLAAType::None), cl::Hidden, |
| 158 | cl::desc("Enable the new, experimental CFL alias analysis in CodeGen"), |
| 159 | cl::values(clEnumValN(CFLAAType::None, "none", "Disable CFL-AA"), |
| 160 | clEnumValN(CFLAAType::Steensgaard, "steens", |
| 161 | "Enable unification-based CFL-AA"), |
| 162 | clEnumValN(CFLAAType::Andersen, "anders", |
| 163 | "Enable inclusion-based CFL-AA"), |
| 164 | clEnumValN(CFLAAType::Both, "both", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 165 | "Enable both variants of CFL-AA"))); |
Hal Finkel | 445dda5 | 2014-09-02 22:12:54 +0000 | [diff] [blame] | 166 | |
Quentin Colombet | 15f6ffb | 2017-07-31 18:24:07 +0000 | [diff] [blame] | 167 | /// Option names for limiting the codegen pipeline. |
| 168 | /// Those are used in error reporting and we didn't want |
| 169 | /// to duplicate their names all over the place. |
| 170 | const char *StartAfterOptName = "start-after"; |
| 171 | const char *StartBeforeOptName = "start-before"; |
| 172 | const char *StopAfterOptName = "stop-after"; |
| 173 | const char *StopBeforeOptName = "stop-before"; |
| 174 | |
| 175 | static cl::opt<std::string> |
| 176 | StartAfterOpt(StringRef(StartAfterOptName), |
| 177 | cl::desc("Resume compilation after a specific pass"), |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 178 | cl::value_desc("pass-name"), cl::init(""), cl::Hidden); |
Quentin Colombet | 15f6ffb | 2017-07-31 18:24:07 +0000 | [diff] [blame] | 179 | |
| 180 | static cl::opt<std::string> |
| 181 | StartBeforeOpt(StringRef(StartBeforeOptName), |
| 182 | cl::desc("Resume compilation before a specific pass"), |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 183 | cl::value_desc("pass-name"), cl::init(""), cl::Hidden); |
Quentin Colombet | 15f6ffb | 2017-07-31 18:24:07 +0000 | [diff] [blame] | 184 | |
| 185 | static cl::opt<std::string> |
| 186 | StopAfterOpt(StringRef(StopAfterOptName), |
| 187 | cl::desc("Stop compilation after a specific pass"), |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 188 | cl::value_desc("pass-name"), cl::init(""), cl::Hidden); |
Quentin Colombet | 15f6ffb | 2017-07-31 18:24:07 +0000 | [diff] [blame] | 189 | |
| 190 | static cl::opt<std::string> |
| 191 | StopBeforeOpt(StringRef(StopBeforeOptName), |
| 192 | cl::desc("Stop compilation before a specific pass"), |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 193 | cl::value_desc("pass-name"), cl::init(""), cl::Hidden); |
Quentin Colombet | 15f6ffb | 2017-07-31 18:24:07 +0000 | [diff] [blame] | 194 | |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 195 | /// Allow standard passes to be disabled by command line options. This supports |
| 196 | /// simple binary flags that either suppress the pass or do nothing. |
| 197 | /// i.e. -disable-mypass=false has no effect. |
| 198 | /// These should be converted to boolOrDefault in order to use applyOverride. |
Andrew Trick | e220323 | 2013-04-10 01:06:56 +0000 | [diff] [blame] | 199 | static IdentifyingPassPtr applyDisable(IdentifyingPassPtr PassID, |
| 200 | bool Override) { |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 201 | if (Override) |
Andrew Trick | e220323 | 2013-04-10 01:06:56 +0000 | [diff] [blame] | 202 | return IdentifyingPassPtr(); |
Bob Wilson | b9b6936 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 203 | return PassID; |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 206 | /// Allow standard passes to be disabled by the command line, regardless of who |
| 207 | /// is adding the pass. |
| 208 | /// |
| 209 | /// StandardID is the pass identified in the standard pass pipeline and provided |
| 210 | /// to addPass(). It may be a target-specific ID in the case that the target |
| 211 | /// directly adds its own pass, but in that case we harmlessly fall through. |
| 212 | /// |
| 213 | /// TargetID is the pass that the target has configured to override StandardID. |
| 214 | /// |
| 215 | /// StandardID may be a pseudo ID. In that case TargetID is the name of the real |
| 216 | /// pass to run. This allows multiple options to control a single pass depending |
| 217 | /// on where in the pipeline that pass is added. |
Andrew Trick | e220323 | 2013-04-10 01:06:56 +0000 | [diff] [blame] | 218 | static IdentifyingPassPtr overridePass(AnalysisID StandardID, |
| 219 | IdentifyingPassPtr TargetID) { |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 220 | if (StandardID == &PostRASchedulerID) |
Matthias Braun | e2d2ead | 2016-12-08 00:16:08 +0000 | [diff] [blame] | 221 | return applyDisable(TargetID, DisablePostRASched); |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 222 | |
| 223 | if (StandardID == &BranchFolderPassID) |
| 224 | return applyDisable(TargetID, DisableBranchFold); |
| 225 | |
| 226 | if (StandardID == &TailDuplicateID) |
| 227 | return applyDisable(TargetID, DisableTailDuplicate); |
| 228 | |
| 229 | if (StandardID == &TargetPassConfig::EarlyTailDuplicateID) |
| 230 | return applyDisable(TargetID, DisableEarlyTailDup); |
| 231 | |
| 232 | if (StandardID == &MachineBlockPlacementID) |
Benjamin Kramer | 70671b9 | 2013-03-29 17:14:24 +0000 | [diff] [blame] | 233 | return applyDisable(TargetID, DisableBlockPlacement); |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 234 | |
| 235 | if (StandardID == &StackSlotColoringID) |
| 236 | return applyDisable(TargetID, DisableSSC); |
| 237 | |
| 238 | if (StandardID == &DeadMachineInstructionElimID) |
| 239 | return applyDisable(TargetID, DisableMachineDCE); |
| 240 | |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 241 | if (StandardID == &EarlyIfConverterID) |
Jakob Stoklund Olesen | 0f6e8bb | 2012-10-03 00:51:32 +0000 | [diff] [blame] | 242 | return applyDisable(TargetID, DisableEarlyIfConversion); |
Jakob Stoklund Olesen | f8a63a1 | 2012-07-04 00:09:54 +0000 | [diff] [blame] | 243 | |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 244 | if (StandardID == &MachineLICMID) |
| 245 | return applyDisable(TargetID, DisableMachineLICM); |
| 246 | |
| 247 | if (StandardID == &MachineCSEID) |
| 248 | return applyDisable(TargetID, DisableMachineCSE); |
| 249 | |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 250 | if (StandardID == &TargetPassConfig::PostRAMachineLICMID) |
| 251 | return applyDisable(TargetID, DisablePostRAMachineLICM); |
| 252 | |
| 253 | if (StandardID == &MachineSinkingID) |
| 254 | return applyDisable(TargetID, DisableMachineSink); |
| 255 | |
| 256 | if (StandardID == &MachineCopyPropagationID) |
| 257 | return applyDisable(TargetID, DisableCopyProp); |
| 258 | |
| 259 | return TargetID; |
| 260 | } |
| 261 | |
Jim Laskey | 29e635d | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 262 | //===---------------------------------------------------------------------===// |
Andrew Trick | b755133 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 263 | /// TargetPassConfig |
| 264 | //===---------------------------------------------------------------------===// |
| 265 | |
| 266 | INITIALIZE_PASS(TargetPassConfig, "targetpassconfig", |
| 267 | "Target Pass Configuration", false, false) |
| 268 | char TargetPassConfig::ID = 0; |
| 269 | |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 270 | // Pseudo Pass IDs. |
| 271 | char TargetPassConfig::EarlyTailDuplicateID = 0; |
| 272 | char TargetPassConfig::PostRAMachineLICMID = 0; |
| 273 | |
Justin Bogner | 468c998 | 2015-10-08 00:36:22 +0000 | [diff] [blame] | 274 | namespace { |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 275 | |
Justin Bogner | 468c998 | 2015-10-08 00:36:22 +0000 | [diff] [blame] | 276 | struct InsertedPass { |
| 277 | AnalysisID TargetPassID; |
| 278 | IdentifyingPassPtr InsertedPassID; |
| 279 | bool VerifyAfter; |
| 280 | bool PrintAfter; |
| 281 | |
| 282 | InsertedPass(AnalysisID TargetPassID, IdentifyingPassPtr InsertedPassID, |
| 283 | bool VerifyAfter, bool PrintAfter) |
| 284 | : TargetPassID(TargetPassID), InsertedPassID(InsertedPassID), |
| 285 | VerifyAfter(VerifyAfter), PrintAfter(PrintAfter) {} |
| 286 | |
| 287 | Pass *getInsertedPass() const { |
| 288 | assert(InsertedPassID.isValid() && "Illegal Pass ID!"); |
| 289 | if (InsertedPassID.isInstance()) |
| 290 | return InsertedPassID.getInstance(); |
| 291 | Pass *NP = Pass::createPass(InsertedPassID.getID()); |
| 292 | assert(NP && "Pass ID not registered"); |
| 293 | return NP; |
| 294 | } |
| 295 | }; |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 296 | |
| 297 | } // end anonymous namespace |
Justin Bogner | 468c998 | 2015-10-08 00:36:22 +0000 | [diff] [blame] | 298 | |
Andrew Trick | c9ce9d2 | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 299 | namespace llvm { |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 300 | |
Andrew Trick | c9ce9d2 | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 301 | class PassConfigImpl { |
| 302 | public: |
| 303 | // List of passes explicitly substituted by this target. Normally this is |
| 304 | // empty, but it is a convenient way to suppress or replace specific passes |
| 305 | // that are part of a standard pass pipeline without overridding the entire |
| 306 | // pipeline. This mechanism allows target options to inherit a standard pass's |
| 307 | // user interface. For example, a target may disable a standard pass by |
Bob Wilson | b9b6936 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 308 | // default by substituting a pass ID of zero, and the user may still enable |
| 309 | // that standard pass with an explicit command line option. |
Andrew Trick | e220323 | 2013-04-10 01:06:56 +0000 | [diff] [blame] | 310 | DenseMap<AnalysisID,IdentifyingPassPtr> TargetPasses; |
Bob Wilson | 33e5188 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 311 | |
| 312 | /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass |
| 313 | /// is inserted after each instance of the first one. |
Justin Bogner | 468c998 | 2015-10-08 00:36:22 +0000 | [diff] [blame] | 314 | SmallVector<InsertedPass, 4> InsertedPasses; |
Andrew Trick | c9ce9d2 | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 315 | }; |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 316 | |
| 317 | } // end namespace llvm |
Andrew Trick | c9ce9d2 | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 318 | |
Andrew Trick | b755133 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 319 | // Out of line virtual method. |
Andrew Trick | c9ce9d2 | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 320 | TargetPassConfig::~TargetPassConfig() { |
| 321 | delete Impl; |
| 322 | } |
Andrew Trick | b755133 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 323 | |
Quentin Colombet | 15f6ffb | 2017-07-31 18:24:07 +0000 | [diff] [blame] | 324 | static const PassInfo *getPassInfo(StringRef PassName) { |
| 325 | if (PassName.empty()) |
| 326 | return nullptr; |
| 327 | |
| 328 | const PassRegistry &PR = *PassRegistry::getPassRegistry(); |
| 329 | const PassInfo *PI = PR.getPassInfo(PassName); |
| 330 | if (!PI) |
| 331 | report_fatal_error(Twine('\"') + Twine(PassName) + |
| 332 | Twine("\" pass is not registered.")); |
| 333 | return PI; |
| 334 | } |
| 335 | |
| 336 | static AnalysisID getPassIDFromName(StringRef PassName) { |
| 337 | const PassInfo *PI = getPassInfo(PassName); |
| 338 | return PI ? PI->getTypeInfo() : nullptr; |
| 339 | } |
| 340 | |
| 341 | void TargetPassConfig::setStartStopPasses() { |
| 342 | StartBefore = getPassIDFromName(StartBeforeOpt); |
| 343 | StartAfter = getPassIDFromName(StartAfterOpt); |
| 344 | StopBefore = getPassIDFromName(StopBeforeOpt); |
| 345 | StopAfter = getPassIDFromName(StopAfterOpt); |
| 346 | if (StartBefore && StartAfter) |
| 347 | report_fatal_error(Twine(StartBeforeOptName) + Twine(" and ") + |
| 348 | Twine(StartAfterOptName) + Twine(" specified!")); |
| 349 | if (StopBefore && StopAfter) |
| 350 | report_fatal_error(Twine(StopBeforeOptName) + Twine(" and ") + |
| 351 | Twine(StopAfterOptName) + Twine(" specified!")); |
| 352 | Started = (StartAfter == nullptr) && (StartBefore == nullptr); |
| 353 | } |
| 354 | |
Andrew Trick | 58648e4 | 2012-02-08 21:22:48 +0000 | [diff] [blame] | 355 | // Out of line constructor provides default values for pass options and |
| 356 | // registers all common codegen passes. |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 357 | TargetPassConfig::TargetPassConfig(LLVMTargetMachine &TM, PassManagerBase &pm) |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 358 | : ImmutablePass(ID), PM(&pm), TM(&TM) { |
Andrew Trick | c9ce9d2 | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 359 | Impl = new PassConfigImpl(); |
| 360 | |
Andrew Trick | b755133 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 361 | // Register all target independent codegen passes to activate their PassIDs, |
| 362 | // including this pass itself. |
| 363 | initializeCodeGen(*PassRegistry::getPassRegistry()); |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 364 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 365 | // Also register alias analysis passes required by codegen passes. |
| 366 | initializeBasicAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 367 | initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 368 | |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 369 | // Substitute Pseudo Pass IDs for real ones. |
Bob Wilson | b9b6936 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 370 | substitutePass(&EarlyTailDuplicateID, &TailDuplicateID); |
| 371 | substitutePass(&PostRAMachineLICMID, &MachineLICMID); |
Matthias Braun | 0663b61 | 2016-05-10 04:51:04 +0000 | [diff] [blame] | 372 | |
| 373 | if (StringRef(PrintMachineInstrs.getValue()).equals("")) |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 374 | TM.Options.PrintMachineCode = true; |
Matt Arsenault | 7b0d947 | 2017-04-04 23:44:46 +0000 | [diff] [blame] | 375 | |
Matt Arsenault | 81da0d4 | 2017-08-14 19:54:47 +0000 | [diff] [blame] | 376 | if (EnableIPRA.getNumOccurrences()) |
| 377 | TM.Options.EnableIPRA = EnableIPRA; |
| 378 | else { |
| 379 | // If not explicitly specified, use target default. |
| 380 | TM.Options.EnableIPRA = TM.useIPRA(); |
| 381 | } |
| 382 | |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 383 | if (TM.Options.EnableIPRA) |
Matt Arsenault | 7b0d947 | 2017-04-04 23:44:46 +0000 | [diff] [blame] | 384 | setRequiresCodeGenSCCOrder(); |
Quentin Colombet | 15f6ffb | 2017-07-31 18:24:07 +0000 | [diff] [blame] | 385 | |
| 386 | setStartStopPasses(); |
Andrew Trick | b755133 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Matthias Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 389 | CodeGenOpt::Level TargetPassConfig::getOptLevel() const { |
| 390 | return TM->getOptLevel(); |
| 391 | } |
| 392 | |
Bob Wilson | 33e5188 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 393 | /// Insert InsertedPassID pass after TargetPassID. |
Bob Wilson | b9b6936 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 394 | void TargetPassConfig::insertPass(AnalysisID TargetPassID, |
Justin Bogner | 468c998 | 2015-10-08 00:36:22 +0000 | [diff] [blame] | 395 | IdentifyingPassPtr InsertedPassID, |
| 396 | bool VerifyAfter, bool PrintAfter) { |
Benjamin Kramer | e7c45bc | 2013-04-11 11:57:01 +0000 | [diff] [blame] | 397 | assert(((!InsertedPassID.isInstance() && |
| 398 | TargetPassID != InsertedPassID.getID()) || |
| 399 | (InsertedPassID.isInstance() && |
| 400 | TargetPassID != InsertedPassID.getInstance()->getPassID())) && |
Andrew Trick | e220323 | 2013-04-10 01:06:56 +0000 | [diff] [blame] | 401 | "Insert a pass after itself!"); |
Justin Bogner | 468c998 | 2015-10-08 00:36:22 +0000 | [diff] [blame] | 402 | Impl->InsertedPasses.emplace_back(TargetPassID, InsertedPassID, VerifyAfter, |
| 403 | PrintAfter); |
Bob Wilson | 33e5188 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Andrew Trick | b755133 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 406 | /// createPassConfig - Create a pass configuration object to be used by |
| 407 | /// addPassToEmitX methods for generating a pipeline of CodeGen passes. |
| 408 | /// |
| 409 | /// Targets may override this to extend TargetPassConfig. |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 410 | TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) { |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 411 | return new TargetPassConfig(*this, PM); |
Andrew Trick | b755133 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | TargetPassConfig::TargetPassConfig() |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 415 | : ImmutablePass(ID) { |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 416 | report_fatal_error("Trying to construct TargetPassConfig without a target " |
| 417 | "machine. Scheduling a CodeGen pass without a target " |
| 418 | "triple set?"); |
Andrew Trick | b755133 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Quentin Colombet | 15f6ffb | 2017-07-31 18:24:07 +0000 | [diff] [blame] | 421 | bool TargetPassConfig::hasLimitedCodeGenPipeline() const { |
| 422 | return StartBefore || StartAfter || StopBefore || StopAfter; |
| 423 | } |
| 424 | |
| 425 | std::string |
| 426 | TargetPassConfig::getLimitedCodeGenPipelineReason(const char *Separator) const { |
| 427 | if (!hasLimitedCodeGenPipeline()) |
| 428 | return std::string(); |
| 429 | std::string Res; |
| 430 | static cl::opt<std::string> *PassNames[] = {&StartAfterOpt, &StartBeforeOpt, |
| 431 | &StopAfterOpt, &StopBeforeOpt}; |
| 432 | static const char *OptNames[] = {StartAfterOptName, StartBeforeOptName, |
| 433 | StopAfterOptName, StopBeforeOptName}; |
| 434 | bool IsFirst = true; |
| 435 | for (int Idx = 0; Idx < 4; ++Idx) |
| 436 | if (!PassNames[Idx]->empty()) { |
| 437 | if (!IsFirst) |
| 438 | Res += Separator; |
| 439 | IsFirst = false; |
| 440 | Res += OptNames[Idx]; |
| 441 | } |
| 442 | return Res; |
| 443 | } |
| 444 | |
Andrew Trick | dd37d52 | 2012-02-08 21:22:39 +0000 | [diff] [blame] | 445 | // Helper to verify the analysis is really immutable. |
| 446 | void TargetPassConfig::setOpt(bool &Opt, bool Val) { |
| 447 | assert(!Initialized && "PassConfig is immutable"); |
| 448 | Opt = Val; |
| 449 | } |
| 450 | |
Bob Wilson | b9b6936 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 451 | void TargetPassConfig::substitutePass(AnalysisID StandardID, |
Andrew Trick | e220323 | 2013-04-10 01:06:56 +0000 | [diff] [blame] | 452 | IdentifyingPassPtr TargetID) { |
Bob Wilson | b9b6936 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 453 | Impl->TargetPasses[StandardID] = TargetID; |
Andrew Trick | c9ce9d2 | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 454 | } |
Andrew Trick | ee874db | 2012-02-11 07:11:32 +0000 | [diff] [blame] | 455 | |
Andrew Trick | e220323 | 2013-04-10 01:06:56 +0000 | [diff] [blame] | 456 | IdentifyingPassPtr TargetPassConfig::getPassSubstitution(AnalysisID ID) const { |
| 457 | DenseMap<AnalysisID, IdentifyingPassPtr>::const_iterator |
Andrew Trick | c9ce9d2 | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 458 | I = Impl->TargetPasses.find(ID); |
| 459 | if (I == Impl->TargetPasses.end()) |
| 460 | return ID; |
| 461 | return I->second; |
| 462 | } |
| 463 | |
Derek Schuff | 1aaf87e | 2016-05-17 08:49:59 +0000 | [diff] [blame] | 464 | bool TargetPassConfig::isPassSubstitutedOrOverridden(AnalysisID ID) const { |
| 465 | IdentifyingPassPtr TargetID = getPassSubstitution(ID); |
| 466 | IdentifyingPassPtr FinalPtr = overridePass(ID, TargetID); |
| 467 | return !FinalPtr.isValid() || FinalPtr.isInstance() || |
| 468 | FinalPtr.getID() != ID; |
| 469 | } |
| 470 | |
Bob Wilson | cac3b90 | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 471 | /// Add a pass to the PassManager if that pass is supposed to be run. If the |
| 472 | /// Started/Stopped flags indicate either that the compilation should start at |
| 473 | /// a later pass or that it should stop after an earlier pass, then do not add |
| 474 | /// the pass. Finally, compare the current pass against the StartAfter |
| 475 | /// and StopAfter options and change the Started/Stopped flags accordingly. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 476 | void TargetPassConfig::addPass(Pass *P, bool verifyAfter, bool printAfter) { |
Bob Wilson | a3f9fa7 | 2012-07-02 19:48:39 +0000 | [diff] [blame] | 477 | assert(!Initialized && "PassConfig is immutable"); |
| 478 | |
Chandler Carruth | 34263a0 | 2012-07-02 22:56:41 +0000 | [diff] [blame] | 479 | // Cache the Pass ID here in case the pass manager finds this pass is |
| 480 | // redundant with ones already scheduled / available, and deletes it. |
| 481 | // Fundamentally, once we add the pass to the manager, we no longer own it |
| 482 | // and shouldn't reference it. |
| 483 | AnalysisID PassID = P->getPassID(); |
| 484 | |
Alex Lorenz | e2d7523 | 2015-07-06 17:44:26 +0000 | [diff] [blame] | 485 | if (StartBefore == PassID) |
| 486 | Started = true; |
Matthias Braun | 729c989 | 2016-09-23 21:46:02 +0000 | [diff] [blame] | 487 | if (StopBefore == PassID) |
| 488 | Stopped = true; |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 489 | if (Started && !Stopped) { |
| 490 | std::string Banner; |
| 491 | // Construct banner message before PM->add() as that may delete the pass. |
| 492 | if (AddingMachinePasses && (printAfter || verifyAfter)) |
| 493 | Banner = std::string("After ") + std::string(P->getPassName()); |
Bob Wilson | cac3b90 | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 494 | PM->add(P); |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 495 | if (AddingMachinePasses) { |
| 496 | if (printAfter) |
| 497 | addPrintPass(Banner); |
| 498 | if (verifyAfter) |
| 499 | addVerifyPass(Banner); |
| 500 | } |
Akira Hatanaka | c100c56 | 2015-06-05 21:58:14 +0000 | [diff] [blame] | 501 | |
| 502 | // Add the passes after the pass P if there is any. |
Justin Bogner | 468c998 | 2015-10-08 00:36:22 +0000 | [diff] [blame] | 503 | for (auto IP : Impl->InsertedPasses) { |
| 504 | if (IP.TargetPassID == PassID) |
| 505 | addPass(IP.getInsertedPass(), IP.VerifyAfter, IP.PrintAfter); |
Akira Hatanaka | c100c56 | 2015-06-05 21:58:14 +0000 | [diff] [blame] | 506 | } |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 507 | } else { |
Benjamin Kramer | 483b9fb | 2013-08-05 11:11:11 +0000 | [diff] [blame] | 508 | delete P; |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 509 | } |
Chandler Carruth | 34263a0 | 2012-07-02 22:56:41 +0000 | [diff] [blame] | 510 | if (StopAfter == PassID) |
Bob Wilson | cac3b90 | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 511 | Stopped = true; |
Chandler Carruth | 34263a0 | 2012-07-02 22:56:41 +0000 | [diff] [blame] | 512 | if (StartAfter == PassID) |
Bob Wilson | cac3b90 | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 513 | Started = true; |
| 514 | if (Stopped && !Started) |
| 515 | report_fatal_error("Cannot stop compilation after pass that is not run"); |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Andrew Trick | c9ce9d2 | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 518 | /// Add a CodeGen pass at this point in the pipeline after checking for target |
| 519 | /// and command line overrides. |
Andrew Trick | e220323 | 2013-04-10 01:06:56 +0000 | [diff] [blame] | 520 | /// |
| 521 | /// addPass cannot return a pointer to the pass instance because is internal the |
| 522 | /// PassManager and the instance we create here may already be freed. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 523 | AnalysisID TargetPassConfig::addPass(AnalysisID PassID, bool verifyAfter, |
| 524 | bool printAfter) { |
Andrew Trick | e220323 | 2013-04-10 01:06:56 +0000 | [diff] [blame] | 525 | IdentifyingPassPtr TargetID = getPassSubstitution(PassID); |
| 526 | IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID); |
| 527 | if (!FinalPtr.isValid()) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 528 | return nullptr; |
Andrew Trick | c9ce9d2 | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 529 | |
Andrew Trick | e220323 | 2013-04-10 01:06:56 +0000 | [diff] [blame] | 530 | Pass *P; |
| 531 | if (FinalPtr.isInstance()) |
| 532 | P = FinalPtr.getInstance(); |
| 533 | else { |
| 534 | P = Pass::createPass(FinalPtr.getID()); |
| 535 | if (!P) |
| 536 | llvm_unreachable("Pass ID not registered"); |
| 537 | } |
| 538 | AnalysisID FinalID = P->getPassID(); |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 539 | addPass(P, verifyAfter, printAfter); // Ends the lifetime of P. |
Andrew Trick | e220323 | 2013-04-10 01:06:56 +0000 | [diff] [blame] | 540 | |
Andrew Trick | c9ce9d2 | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 541 | return FinalID; |
Andrew Trick | f8ea108 | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 542 | } |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 543 | |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 544 | void TargetPassConfig::printAndVerify(const std::string &Banner) { |
| 545 | addPrintPass(Banner); |
| 546 | addVerifyPass(Banner); |
| 547 | } |
Matthias Braun | a7c82a9 | 2014-12-11 19:42:05 +0000 | [diff] [blame] | 548 | |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 549 | void TargetPassConfig::addPrintPass(const std::string &Banner) { |
| 550 | if (TM->shouldPrintMachineCode()) |
| 551 | PM->add(createMachineFunctionPrinterPass(dbgs(), Banner)); |
| 552 | } |
| 553 | |
| 554 | void TargetPassConfig::addVerifyPass(const std::string &Banner) { |
Matthias Braun | d6a36ae | 2017-05-31 18:41:23 +0000 | [diff] [blame] | 555 | bool Verify = VerifyMachineCode; |
| 556 | #ifdef EXPENSIVE_CHECKS |
| 557 | if (VerifyMachineCode == cl::BOU_UNSET) |
| 558 | Verify = TM->isMachineVerifierClean(); |
| 559 | #endif |
| 560 | if (Verify) |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 561 | PM->add(createMachineVerifierPass(Banner)); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Andrew Trick | f8ea108 | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 564 | /// Add common target configurable passes that perform LLVM IR to IR transforms |
| 565 | /// following machine independent optimization. |
| 566 | void TargetPassConfig::addIRPasses() { |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 567 | switch (UseCFLAA) { |
| 568 | case CFLAAType::Steensgaard: |
| 569 | addPass(createCFLSteensAAWrapperPass()); |
| 570 | break; |
| 571 | case CFLAAType::Andersen: |
| 572 | addPass(createCFLAndersAAWrapperPass()); |
| 573 | break; |
| 574 | case CFLAAType::Both: |
| 575 | addPass(createCFLAndersAAWrapperPass()); |
| 576 | addPass(createCFLSteensAAWrapperPass()); |
| 577 | break; |
| 578 | default: |
| 579 | break; |
| 580 | } |
| 581 | |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 582 | // Basic AliasAnalysis support. |
| 583 | // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that |
| 584 | // BasicAliasAnalysis wins if they disagree. This is intended to help |
| 585 | // support "obvious" type-punning idioms. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 586 | addPass(createTypeBasedAAWrapperPass()); |
| 587 | addPass(createScopedNoAliasAAWrapperPass()); |
| 588 | addPass(createBasicAAWrapperPass()); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 589 | |
| 590 | // Before running any passes, run the verifier to determine if the input |
| 591 | // coming from the front-end and/or optimizer is valid. |
Duncan P. N. Exon Smith | ab58a56 | 2015-03-19 22:24:17 +0000 | [diff] [blame] | 592 | if (!DisableVerify) |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 593 | addPass(createVerifierPass()); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 594 | |
| 595 | // Run loop strength reduction before anything else. |
| 596 | if (getOptLevel() != CodeGenOpt::None && !DisableLSR) { |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 597 | addPass(createLoopStrengthReducePass()); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 598 | if (PrintLSR) |
Chandler Carruth | 9d80513 | 2014-01-12 11:30:46 +0000 | [diff] [blame] | 599 | addPass(createPrintFunctionPass(dbgs(), "\n\n*** Code after LSR ***\n")); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Clement Courbet | 063bed9 | 2017-11-03 12:12:27 +0000 | [diff] [blame] | 602 | if (getOptLevel() != CodeGenOpt::None) { |
| 603 | // The MergeICmpsPass tries to create memcmp calls by grouping sequences of |
| 604 | // loads and compares. ExpandMemCmpPass then tries to expand those calls |
| 605 | // into optimally-sized loads and compares. The transforms are enabled by a |
| 606 | // target lowering hook. |
| 607 | if (EnableMergeICmps) |
| 608 | addPass(createMergeICmpsPass()); |
| 609 | addPass(createExpandMemCmpPass()); |
Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Philip Reames | 23cf2e2 | 2015-01-28 19:28:03 +0000 | [diff] [blame] | 612 | // Run GC lowering passes for builtin collectors |
| 613 | // TODO: add a pass insertion point here |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 614 | addPass(createGCLoweringPass()); |
Philip Reames | 23cf2e2 | 2015-01-28 19:28:03 +0000 | [diff] [blame] | 615 | addPass(createShadowStackGCLoweringPass()); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 616 | |
| 617 | // Make sure that no unreachable blocks are instruction selected. |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 618 | addPass(createUnreachableBlockEliminationPass()); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 619 | |
| 620 | // Prepare expensive constants for SelectionDAG. |
| 621 | if (getOptLevel() != CodeGenOpt::None && !DisableConstantHoisting) |
| 622 | addPass(createConstantHoistingPass()); |
James Molloy | bc9fed8 | 2014-07-23 13:33:00 +0000 | [diff] [blame] | 623 | |
| 624 | if (getOptLevel() != CodeGenOpt::None && !DisablePartialLibcallInlining) |
| 625 | addPass(createPartiallyInlineLibCallsPass()); |
Hal Finkel | 40d7f5c | 2016-09-01 09:42:39 +0000 | [diff] [blame] | 626 | |
Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 627 | // Instrument function entry and exit, e.g. with calls to mcount(). |
| 628 | addPass(createPostInlineEntryExitInstrumenterPass()); |
Amara Emerson | 836b0f4 | 2017-05-10 09:42:49 +0000 | [diff] [blame] | 629 | |
Ayman Musa | c5490e5 | 2017-05-15 11:30:54 +0000 | [diff] [blame] | 630 | // Add scalarization of target's unsupported masked memory intrinsics pass. |
| 631 | // the unsupported intrinsic will be replaced with a chain of basic blocks, |
| 632 | // that stores/loads element one-by-one if the appropriate mask bit is set. |
| 633 | addPass(createScalarizeMaskedMemIntrinPass()); |
| 634 | |
Amara Emerson | 836b0f4 | 2017-05-10 09:42:49 +0000 | [diff] [blame] | 635 | // Expand reduction intrinsics into shuffle sequences if the target wants to. |
| 636 | addPass(createExpandReductionsPass()); |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /// Turn exception handling constructs into something the code generators can |
| 640 | /// handle. |
| 641 | void TargetPassConfig::addPassesToHandleExceptions() { |
Alex Bradbury | 3447ca3 | 2016-08-18 13:08:58 +0000 | [diff] [blame] | 642 | const MCAsmInfo *MCAI = TM->getMCAsmInfo(); |
| 643 | assert(MCAI && "No MCAsmInfo"); |
| 644 | switch (MCAI->getExceptionHandlingType()) { |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 645 | case ExceptionHandling::SjLj: |
| 646 | // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both |
| 647 | // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise, |
| 648 | // catch info can get misplaced when a selector ends up more than one block |
| 649 | // removed from the parent invoke(s). This could happen when a landing |
| 650 | // pad is shared by multiple invokes and is also a target of a normal |
| 651 | // edge from elsewhere. |
Mehdi Amini | f50daed | 2015-07-08 01:00:31 +0000 | [diff] [blame] | 652 | addPass(createSjLjEHPreparePass()); |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 653 | LLVM_FALLTHROUGH; |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 654 | case ExceptionHandling::DwarfCFI: |
| 655 | case ExceptionHandling::ARM: |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 656 | addPass(createDwarfEHPass()); |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 657 | break; |
Reid Kleckner | 1185fce | 2015-01-29 00:41:44 +0000 | [diff] [blame] | 658 | case ExceptionHandling::WinEH: |
Reid Kleckner | 47c8e7a | 2015-03-12 00:36:20 +0000 | [diff] [blame] | 659 | // We support using both GCC-style and MSVC-style exceptions on Windows, so |
| 660 | // add both preparation passes. Each pass will only actually run if it |
| 661 | // recognizes the personality function. |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 662 | addPass(createWinEHPass()); |
| 663 | addPass(createDwarfEHPass()); |
Reid Kleckner | 1185fce | 2015-01-29 00:41:44 +0000 | [diff] [blame] | 664 | break; |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 665 | case ExceptionHandling::None: |
Mark Seaborn | b6118c5 | 2014-03-20 19:54:47 +0000 | [diff] [blame] | 666 | addPass(createLowerInvokePass()); |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 667 | |
| 668 | // The lower invoke pass may create unreachable code. Remove it. |
| 669 | addPass(createUnreachableBlockEliminationPass()); |
| 670 | break; |
| 671 | } |
Andrew Trick | f8ea108 | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 672 | } |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 673 | |
Bill Wendling | c786b31 | 2012-11-30 22:08:55 +0000 | [diff] [blame] | 674 | /// Add pass to prepare the LLVM IR for code generation. This should be done |
| 675 | /// before exception handling preparation passes. |
| 676 | void TargetPassConfig::addCodeGenPrepare() { |
| 677 | if (getOptLevel() != CodeGenOpt::None && !DisableCGP) |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 678 | addPass(createCodeGenPreparePass()); |
Saleem Abdulrasool | d2c5d7f | 2014-11-08 00:00:50 +0000 | [diff] [blame] | 679 | addPass(createRewriteSymbolsPass()); |
Bill Wendling | c786b31 | 2012-11-30 22:08:55 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Andrew Trick | f8ea108 | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 682 | /// Add common passes that perform LLVM IR to IR transforms in preparation for |
| 683 | /// instruction selection. |
| 684 | void TargetPassConfig::addISelPrepare() { |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 685 | addPreISel(); |
| 686 | |
Mehdi Amini | bbacddf | 2016-06-10 16:19:46 +0000 | [diff] [blame] | 687 | // Force codegen to run according to the callgraph. |
Matt Arsenault | 7b0d947 | 2017-04-04 23:44:46 +0000 | [diff] [blame] | 688 | if (requiresCodeGenSCCOrder()) |
Mehdi Amini | bbacddf | 2016-06-10 16:19:46 +0000 | [diff] [blame] | 689 | addPass(new DummyCGSCCPass); |
| 690 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 691 | // Add both the safe stack and the stack protection passes: each of them will |
| 692 | // only protect functions that have corresponding attributes. |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 693 | addPass(createSafeStackPass()); |
| 694 | addPass(createStackProtectorPass()); |
Josh Magee | 22b8ba2 | 2013-12-19 03:17:11 +0000 | [diff] [blame] | 695 | |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 696 | if (PrintISelInput) |
Chandler Carruth | 9d80513 | 2014-01-12 11:30:46 +0000 | [diff] [blame] | 697 | addPass(createPrintFunctionPass( |
| 698 | dbgs(), "\n\n*** Final LLVM Code input to ISel ***\n")); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 699 | |
| 700 | // All passes which modify the LLVM IR are now complete; run the verifier |
| 701 | // to ensure that the IR is valid. |
| 702 | if (!DisableVerify) |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 703 | addPass(createVerifierPass()); |
Andrew Trick | f8ea108 | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 704 | } |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 705 | |
Matthias Braun | c7c06f1 | 2017-06-06 00:26:13 +0000 | [diff] [blame] | 706 | bool TargetPassConfig::addCoreISelPasses() { |
| 707 | // Enable FastISel with -fast, but allow that to be overridden. |
| 708 | TM->setO0WantsFastISel(EnableFastISelOption != cl::BOU_FALSE); |
| 709 | if (EnableFastISelOption == cl::BOU_TRUE || |
| 710 | (TM->getOptLevel() == CodeGenOpt::None && TM->getO0WantsFastISel())) |
| 711 | TM->setFastISel(true); |
| 712 | |
| 713 | // Ask the target for an isel. |
| 714 | // Enable GlobalISel if the target wants to, but allow that to be overriden. |
Amara Emerson | 854d10d | 2018-01-02 16:30:47 +0000 | [diff] [blame^] | 715 | // Explicitly enabling fast-isel should override implicitly enabled |
| 716 | // global-isel. |
Matthias Braun | c7c06f1 | 2017-06-06 00:26:13 +0000 | [diff] [blame] | 717 | if (EnableGlobalISel == cl::BOU_TRUE || |
Amara Emerson | 854d10d | 2018-01-02 16:30:47 +0000 | [diff] [blame^] | 718 | (EnableGlobalISel == cl::BOU_UNSET && isGlobalISelEnabled() && |
| 719 | EnableFastISelOption != cl::BOU_TRUE)) { |
Matthias Braun | c7c06f1 | 2017-06-06 00:26:13 +0000 | [diff] [blame] | 720 | if (addIRTranslator()) |
| 721 | return true; |
| 722 | |
| 723 | addPreLegalizeMachineIR(); |
| 724 | |
| 725 | if (addLegalizeMachineIR()) |
| 726 | return true; |
| 727 | |
| 728 | // Before running the register bank selector, ask the target if it |
| 729 | // wants to run some passes. |
| 730 | addPreRegBankSelect(); |
| 731 | |
| 732 | if (addRegBankSelect()) |
| 733 | return true; |
| 734 | |
| 735 | addPreGlobalInstructionSelect(); |
| 736 | |
| 737 | if (addGlobalInstructionSelect()) |
| 738 | return true; |
| 739 | |
| 740 | // Pass to reset the MachineFunction if the ISel failed. |
| 741 | addPass(createResetMachineFunctionPass( |
| 742 | reportDiagnosticWhenGlobalISelFallback(), isGlobalISelAbortEnabled())); |
| 743 | |
| 744 | // Provide a fallback path when we do not want to abort on |
| 745 | // not-yet-supported input. |
| 746 | if (!isGlobalISelAbortEnabled() && addInstSelector()) |
| 747 | return true; |
| 748 | |
| 749 | } else if (addInstSelector()) |
| 750 | return true; |
| 751 | |
| 752 | return false; |
| 753 | } |
| 754 | |
| 755 | bool TargetPassConfig::addISelPasses() { |
| 756 | if (TM->Options.EmulatedTLS) |
| 757 | addPass(createLowerEmuTLSPass()); |
| 758 | |
| 759 | addPass(createPreISelIntrinsicLoweringPass()); |
| 760 | addPass(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis())); |
| 761 | addIRPasses(); |
| 762 | addCodeGenPrepare(); |
| 763 | addPassesToHandleExceptions(); |
| 764 | addISelPrepare(); |
| 765 | |
| 766 | return addCoreISelPasses(); |
| 767 | } |
| 768 | |
Jonas Paulsson | 0f86780 | 2017-05-17 07:36:03 +0000 | [diff] [blame] | 769 | /// -regalloc=... command line option. |
| 770 | static FunctionPass *useDefaultRegisterAllocator() { return nullptr; } |
| 771 | static cl::opt<RegisterRegAlloc::FunctionPassCtor, false, |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 772 | RegisterPassParser<RegisterRegAlloc>> |
| 773 | RegAlloc("regalloc", cl::Hidden, cl::init(&useDefaultRegisterAllocator), |
| 774 | cl::desc("Register allocator to use")); |
Jonas Paulsson | 0f86780 | 2017-05-17 07:36:03 +0000 | [diff] [blame] | 775 | |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 776 | /// Add the complete set of target-independent postISel code generator passes. |
| 777 | /// |
| 778 | /// This can be read as the standard order of major LLVM CodeGen stages. Stages |
| 779 | /// with nontrivial configuration or multiple passes are broken out below in |
| 780 | /// add%Stage routines. |
| 781 | /// |
| 782 | /// Any TargetPassConfig::addXX routine may be overriden by the Target. The |
| 783 | /// addPre/Post methods with empty header implementations allow injecting |
| 784 | /// target-specific fixups just before or after major stages. Additionally, |
| 785 | /// targets have the flexibility to change pass order within a stage by |
| 786 | /// overriding default implementation of add%Stage routines below. Each |
| 787 | /// technique has maintainability tradeoffs because alternate pass orders are |
| 788 | /// not well supported. addPre/Post works better if the target pass is easily |
| 789 | /// tied to a common pass. But if it has subtle dependencies on multiple passes, |
Andrew Trick | 09fc1bb | 2012-02-10 07:08:25 +0000 | [diff] [blame] | 790 | /// the target should override the stage instead. |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 791 | /// |
| 792 | /// TODO: We could use a single addPre/Post(ID) hook to allow pass injection |
| 793 | /// before/after any target-independent pass. But it's currently overkill. |
Andrew Trick | f8ea108 | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 794 | void TargetPassConfig::addMachinePasses() { |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 795 | AddingMachinePasses = true; |
| 796 | |
Bob Wilson | 33e5188 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 797 | // Insert a machine instr printer pass after the specified pass. |
Matthias Braun | 0663b61 | 2016-05-10 04:51:04 +0000 | [diff] [blame] | 798 | if (!StringRef(PrintMachineInstrs.getValue()).equals("") && |
| 799 | !StringRef(PrintMachineInstrs.getValue()).equals("option-unspecified")) { |
Bob Wilson | 33e5188 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 800 | const PassRegistry *PR = PassRegistry::getPassRegistry(); |
| 801 | const PassInfo *TPI = PR->getPassInfo(PrintMachineInstrs.getValue()); |
Akira Hatanaka | 7ba7830 | 2014-12-13 04:52:04 +0000 | [diff] [blame] | 802 | const PassInfo *IPI = PR->getPassInfo(StringRef("machineinstr-printer")); |
Bob Wilson | 33e5188 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 803 | assert (TPI && IPI && "Pass ID not registered!"); |
Roman Divacky | ad06cee | 2012-09-05 22:26:57 +0000 | [diff] [blame] | 804 | const char *TID = (const char *)(TPI->getTypeInfo()); |
| 805 | const char *IID = (const char *)(IPI->getTypeInfo()); |
Bob Wilson | b9b6936 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 806 | insertPass(TID, IID); |
Bob Wilson | 33e5188 | 2012-05-30 00:17:12 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Jakob Stoklund Olesen | 29506f5 | 2012-07-04 19:28:27 +0000 | [diff] [blame] | 809 | // Print the instruction selected machine code... |
| 810 | printAndVerify("After Instruction Selection"); |
| 811 | |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 812 | // Expand pseudo-instructions emitted by ISel. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 813 | addPass(&ExpandISelPseudosID); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 814 | |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 815 | // Add passes that optimize machine instructions in SSA form. |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 816 | if (getOptLevel() != CodeGenOpt::None) { |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 817 | addMachineSSAOptimization(); |
Craig Topper | 36f2912 | 2012-11-19 00:11:50 +0000 | [diff] [blame] | 818 | } else { |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 819 | // If the target requests it, assign local variables to stack slots relative |
| 820 | // to one another and simplify frame index references where possible. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 821 | addPass(&LocalStackSlotAllocationID, false); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Matt Arsenault | f9273c8 | 2017-08-14 19:54:45 +0000 | [diff] [blame] | 824 | if (TM->Options.EnableIPRA) |
| 825 | addPass(createRegUsageInfoPropPass()); |
| 826 | |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 827 | // Run pre-ra passes. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 828 | addPreRegAlloc(); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 829 | |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 830 | // Run register allocation and passes that are tightly coupled with it, |
| 831 | // including phi elimination and scheduling. |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 832 | if (getOptimizeRegAlloc()) |
| 833 | addOptimizedRegAlloc(createRegAllocPass(true)); |
Jonas Paulsson | 0f86780 | 2017-05-17 07:36:03 +0000 | [diff] [blame] | 834 | else { |
| 835 | if (RegAlloc != &useDefaultRegisterAllocator && |
| 836 | RegAlloc != &createFastRegisterAllocator) |
| 837 | report_fatal_error("Must use fast (default) register allocator for unoptimized regalloc."); |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 838 | addFastRegAlloc(createRegAllocPass(false)); |
Jonas Paulsson | 0f86780 | 2017-05-17 07:36:03 +0000 | [diff] [blame] | 839 | } |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 840 | |
| 841 | // Run post-ra passes. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 842 | addPostRegAlloc(); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 843 | |
| 844 | // Insert prolog/epilog code. Eliminate abstract frame index references... |
Junmo Park | 3347e78 | 2016-01-18 06:42:51 +0000 | [diff] [blame] | 845 | if (getOptLevel() != CodeGenOpt::None) |
Kit Barton | ae78d53 | 2015-08-14 16:54:32 +0000 | [diff] [blame] | 846 | addPass(&ShrinkWrapID); |
Kit Barton | d3cc167 | 2015-08-31 18:26:45 +0000 | [diff] [blame] | 847 | |
Derek Schuff | 1aaf87e | 2016-05-17 08:49:59 +0000 | [diff] [blame] | 848 | // Prolog/Epilog inserter needs a TargetMachine to instantiate. But only |
| 849 | // do so if it hasn't been disabled, substituted, or overridden. |
| 850 | if (!isPassSubstitutedOrOverridden(&PrologEpilogCodeInserterID)) |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 851 | addPass(createPrologEpilogInserterPass()); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 852 | |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 853 | /// Add passes that optimize machine instructions after register allocation. |
| 854 | if (getOptLevel() != CodeGenOpt::None) |
| 855 | addMachineLateOptimization(); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 856 | |
| 857 | // Expand pseudo instructions before second scheduling pass. |
Bob Wilson | b9b6936 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 858 | addPass(&ExpandPostRAPseudosID); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 859 | |
| 860 | // Run pre-sched2 passes. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 861 | addPreSched2(); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 862 | |
Sanjoy Das | 69fad07 | 2015-06-15 18:44:27 +0000 | [diff] [blame] | 863 | if (EnableImplicitNullChecks) |
| 864 | addPass(&ImplicitNullChecksID); |
| 865 | |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 866 | // Second pass scheduler. |
Jonas Paulsson | e451eef | 2015-12-10 09:10:07 +0000 | [diff] [blame] | 867 | // Let Target optionally insert this pass by itself at some other |
| 868 | // point. |
| 869 | if (getOptLevel() != CodeGenOpt::None && |
| 870 | !TM->targetSchedulesPostRAScheduling()) { |
Andrew Trick | 17080b9 | 2013-12-28 21:56:51 +0000 | [diff] [blame] | 871 | if (MISchedPostRA) |
| 872 | addPass(&PostMachineSchedulerID); |
| 873 | else |
| 874 | addPass(&PostRASchedulerID); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 877 | // GC |
Evan Cheng | 59421ae | 2012-12-21 02:57:04 +0000 | [diff] [blame] | 878 | if (addGCPasses()) { |
| 879 | if (PrintGCInfo) |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 880 | addPass(createGCInfoPrinter(dbgs()), false, false); |
Evan Cheng | 59421ae | 2012-12-21 02:57:04 +0000 | [diff] [blame] | 881 | } |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 882 | |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 883 | // Basic block placement. |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 884 | if (getOptLevel() != CodeGenOpt::None) |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 885 | addBlockPlacement(); |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 886 | |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 887 | addPreEmitPass(); |
Juergen Ributzka | e829475 | 2013-12-14 06:53:06 +0000 | [diff] [blame] | 888 | |
Mehdi Amini | cfed256 | 2016-07-13 23:39:46 +0000 | [diff] [blame] | 889 | if (TM->Options.EnableIPRA) |
Mehdi Amini | bbacddf | 2016-06-10 16:19:46 +0000 | [diff] [blame] | 890 | // Collect register usage information and produce a register mask of |
| 891 | // clobbered registers, to be used to optimize call sites. |
| 892 | addPass(createRegUsageInfoCollector()); |
| 893 | |
David Majnemer | 9789023 | 2015-09-17 20:45:18 +0000 | [diff] [blame] | 894 | addPass(&FuncletLayoutID, false); |
| 895 | |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 896 | addPass(&StackMapLivenessID, false); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 897 | addPass(&LiveDebugValuesID, false); |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 898 | |
Nirav Dave | a7c041d | 2017-01-31 17:00:27 +0000 | [diff] [blame] | 899 | // Insert before XRay Instrumentation. |
| 900 | addPass(&FEntryInserterID, false); |
| 901 | |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 902 | addPass(&XRayInstrumentationID, false); |
Sanjoy Das | c0441c2 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 903 | addPass(&PatchableFunctionID, false); |
| 904 | |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 905 | if (EnableMachineOutliner) |
Jessica Paquette | 1359384 | 2017-10-07 00:16:34 +0000 | [diff] [blame] | 906 | PM->add(createMachineOutlinerPass(EnableLinkOnceODROutlining)); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 907 | |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 908 | AddingMachinePasses = false; |
Andrew Trick | de401d3 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 911 | /// Add passes that optimize machine instructions in SSA form. |
| 912 | void TargetPassConfig::addMachineSSAOptimization() { |
| 913 | // Pre-ra tail duplication. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 914 | addPass(&EarlyTailDuplicateID); |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 915 | |
| 916 | // Optimize PHIs before DCE: removing dead PHI cycles may make more |
| 917 | // instructions dead. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 918 | addPass(&OptimizePHIsID, false); |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 919 | |
Nadav Rotem | 7c277da | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 920 | // This pass merges large allocas. StackSlotColoring is a different pass |
| 921 | // which merges spill slots. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 922 | addPass(&StackColoringID, false); |
Nadav Rotem | 7c277da | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 923 | |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 924 | // If the target requests it, assign local variables to stack slots relative |
| 925 | // to one another and simplify frame index references where possible. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 926 | addPass(&LocalStackSlotAllocationID, false); |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 927 | |
| 928 | // With optimization, dead code should already be eliminated. However |
| 929 | // there is one known exception: lowered code for arguments that are only |
| 930 | // used by tail calls, where the tail calls reuse the incoming stack |
| 931 | // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll). |
Bob Wilson | b9b6936 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 932 | addPass(&DeadMachineInstructionElimID); |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 933 | |
Jakob Stoklund Olesen | 213a2f8 | 2013-01-17 00:58:38 +0000 | [diff] [blame] | 934 | // Allow targets to insert passes that improve instruction level parallelism, |
| 935 | // like if-conversion. Such passes will typically need dominator trees and |
| 936 | // loop info, just like LICM and CSE below. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 937 | addILPOpts(); |
Jakob Stoklund Olesen | 213a2f8 | 2013-01-17 00:58:38 +0000 | [diff] [blame] | 938 | |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 939 | addPass(&MachineLICMID, false); |
| 940 | addPass(&MachineCSEID, false); |
Nemanja Ivanovic | b223cfa | 2017-03-01 20:29:34 +0000 | [diff] [blame] | 941 | |
Bob Wilson | b9b6936 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 942 | addPass(&MachineSinkingID); |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 943 | |
Matt Arsenault | 07a72ba | 2015-10-12 17:43:56 +0000 | [diff] [blame] | 944 | addPass(&PeepholeOptimizerID); |
Quentin Colombet | 03e43f8 | 2014-08-20 17:41:48 +0000 | [diff] [blame] | 945 | // Clean-up the dead code that may have been generated by peephole |
| 946 | // rewriting. |
| 947 | addPass(&DeadMachineInstructionElimID); |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Andrew Trick | b755133 | 2012-02-04 02:56:45 +0000 | [diff] [blame] | 950 | //===---------------------------------------------------------------------===// |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 951 | /// Register Allocation Pass Configuration |
Jim Laskey | 29e635d | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 952 | //===---------------------------------------------------------------------===// |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 953 | |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 954 | bool TargetPassConfig::getOptimizeRegAlloc() const { |
| 955 | switch (OptimizeRegAlloc) { |
| 956 | case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None; |
| 957 | case cl::BOU_TRUE: return true; |
| 958 | case cl::BOU_FALSE: return false; |
| 959 | } |
| 960 | llvm_unreachable("Invalid optimize-regalloc state"); |
| 961 | } |
| 962 | |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 963 | /// RegisterRegAlloc's global Registry tracks allocator registration. |
Jim Laskey | 29e635d | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 964 | MachinePassRegistry RegisterRegAlloc::Registry; |
| 965 | |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 966 | /// A dummy default pass factory indicates whether the register allocator is |
| 967 | /// overridden on the command line. |
Kamil Rytarowski | 5d2bd8d | 2017-02-05 21:13:06 +0000 | [diff] [blame] | 968 | static llvm::once_flag InitializeDefaultRegisterAllocatorFlag; |
Jonas Paulsson | 0f86780 | 2017-05-17 07:36:03 +0000 | [diff] [blame] | 969 | |
Jakob Stoklund Olesen | b613ae2 | 2010-05-27 23:57:25 +0000 | [diff] [blame] | 970 | static RegisterRegAlloc |
| 971 | defaultRegAlloc("default", |
| 972 | "pick register allocator based on -O option", |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 973 | useDefaultRegisterAllocator); |
Jim Laskey | 29e635d | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 974 | |
David Majnemer | d9d02d8 | 2016-07-08 16:39:00 +0000 | [diff] [blame] | 975 | static void initializeDefaultRegisterAllocatorOnce() { |
| 976 | RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault(); |
| 977 | |
| 978 | if (!Ctor) { |
| 979 | Ctor = RegAlloc; |
| 980 | RegisterRegAlloc::setDefault(RegAlloc); |
| 981 | } |
| 982 | } |
| 983 | |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 984 | /// Instantiate the default register allocator pass for this target for either |
| 985 | /// the optimized or unoptimized allocation path. This will be added to the pass |
| 986 | /// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc |
| 987 | /// in the optimized case. |
| 988 | /// |
| 989 | /// A target that uses the standard regalloc pass order for fast or optimized |
| 990 | /// allocation may still override this for per-target regalloc |
| 991 | /// selection. But -regalloc=... always takes precedence. |
| 992 | FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) { |
| 993 | if (Optimized) |
| 994 | return createGreedyRegisterAllocator(); |
| 995 | else |
| 996 | return createFastRegisterAllocator(); |
| 997 | } |
| 998 | |
| 999 | /// Find and instantiate the register allocation pass requested by this target |
| 1000 | /// at the current optimization level. Different register allocators are |
| 1001 | /// defined as separate passes because they may require different analysis. |
| 1002 | /// |
| 1003 | /// This helper ensures that the regalloc= option is always available, |
| 1004 | /// even for targets that override the default allocator. |
| 1005 | /// |
| 1006 | /// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs, |
| 1007 | /// this can be folded into addPass. |
| 1008 | FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) { |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 1009 | // Initialize the global default. |
David Majnemer | d9d02d8 | 2016-07-08 16:39:00 +0000 | [diff] [blame] | 1010 | llvm::call_once(InitializeDefaultRegisterAllocatorFlag, |
| 1011 | initializeDefaultRegisterAllocatorOnce); |
| 1012 | |
| 1013 | RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault(); |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 1014 | if (Ctor != useDefaultRegisterAllocator) |
Jakob Stoklund Olesen | b613ae2 | 2010-05-27 23:57:25 +0000 | [diff] [blame] | 1015 | return Ctor(); |
| 1016 | |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 1017 | // With no -regalloc= override, ask the target for a regalloc pass. |
| 1018 | return createTargetRegisterAllocator(Optimized); |
| 1019 | } |
| 1020 | |
Arnaud A. de Grandmaison | a61262f | 2014-10-21 20:47:22 +0000 | [diff] [blame] | 1021 | /// Return true if the default global register allocator is in use and |
| 1022 | /// has not be overriden on the command line with '-regalloc=...' |
| 1023 | bool TargetPassConfig::usingDefaultRegAlloc() const { |
Arnaud A. de Grandmaison | 5c7fe7e9 | 2014-10-21 21:50:49 +0000 | [diff] [blame] | 1024 | return RegAlloc.getNumOccurrences() == 0; |
Arnaud A. de Grandmaison | a61262f | 2014-10-21 20:47:22 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 1027 | /// Add the minimum set of target-independent passes that are required for |
| 1028 | /// register allocation. No coalescing or scheduling. |
| 1029 | void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) { |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 1030 | addPass(&PHIEliminationID, false); |
| 1031 | addPass(&TwoAddressInstructionPassID, false); |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 1032 | |
Dan Gohman | e32c574 | 2015-09-08 20:36:33 +0000 | [diff] [blame] | 1033 | if (RegAllocPass) |
| 1034 | addPass(RegAllocPass); |
Jim Laskey | d1a714e | 2006-07-27 20:05:00 +0000 | [diff] [blame] | 1035 | } |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 1036 | |
| 1037 | /// Add standard target-independent passes that are tightly coupled with |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 1038 | /// optimized register allocation, including coalescing, machine instruction |
| 1039 | /// scheduling, and register allocation itself. |
| 1040 | void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) { |
Matthias Braun | fbe85ae | 2016-04-28 03:07:16 +0000 | [diff] [blame] | 1041 | addPass(&DetectDeadLanesID, false); |
| 1042 | |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 1043 | addPass(&ProcessImplicitDefsID, false); |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 1044 | |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 1045 | // LiveVariables currently requires pure SSA form. |
| 1046 | // |
| 1047 | // FIXME: Once TwoAddressInstruction pass no longer uses kill flags, |
| 1048 | // LiveVariables can be removed completely, and LiveIntervals can be directly |
| 1049 | // computed. (We still either need to regenerate kill flags after regalloc, or |
| 1050 | // preferably fix the scavenger to not depend on them). |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 1051 | addPass(&LiveVariablesID, false); |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 1052 | |
Rafael Espindola | 9770bde | 2013-10-14 16:39:04 +0000 | [diff] [blame] | 1053 | // Edge splitting is smarter with machine loop info. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 1054 | addPass(&MachineLoopInfoID, false); |
| 1055 | addPass(&PHIEliminationID, false); |
Jakob Stoklund Olesen | 1c46589 | 2012-08-03 22:12:54 +0000 | [diff] [blame] | 1056 | |
| 1057 | // Eventually, we want to run LiveIntervals before PHI elimination. |
| 1058 | if (EarlyLiveIntervals) |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 1059 | addPass(&LiveIntervalsID, false); |
Jakob Stoklund Olesen | 1c46589 | 2012-08-03 22:12:54 +0000 | [diff] [blame] | 1060 | |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 1061 | addPass(&TwoAddressInstructionPassID, false); |
Bob Wilson | b9b6936 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 1062 | addPass(&RegisterCoalescerID); |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 1063 | |
Matthias Braun | f9acaca | 2016-05-31 22:38:06 +0000 | [diff] [blame] | 1064 | // The machine scheduler may accidentally create disconnected components |
| 1065 | // when moving subregister definitions around, avoid this by splitting them to |
| 1066 | // separate vregs before. Splitting can also improve reg. allocation quality. |
| 1067 | addPass(&RenameIndependentSubregsID); |
| 1068 | |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 1069 | // PreRA instruction scheduling. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 1070 | addPass(&MachineSchedulerID); |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 1071 | |
Dan Gohman | e32c574 | 2015-09-08 20:36:33 +0000 | [diff] [blame] | 1072 | if (RegAllocPass) { |
| 1073 | // Add the selected register allocation pass. |
| 1074 | addPass(RegAllocPass); |
Jakob Stoklund Olesen | 59a0d32 | 2012-06-26 17:09:29 +0000 | [diff] [blame] | 1075 | |
Dan Gohman | e32c574 | 2015-09-08 20:36:33 +0000 | [diff] [blame] | 1076 | // Allow targets to change the register assignments before rewriting. |
| 1077 | addPreRewrite(); |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 1078 | |
Dan Gohman | e32c574 | 2015-09-08 20:36:33 +0000 | [diff] [blame] | 1079 | // Finally rewrite virtual registers. |
| 1080 | addPass(&VirtRegRewriterID); |
Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 1081 | |
Dan Gohman | e32c574 | 2015-09-08 20:36:33 +0000 | [diff] [blame] | 1082 | // Perform stack slot coloring and post-ra machine LICM. |
| 1083 | // |
| 1084 | // FIXME: Re-enable coloring with register when it's capable of adding |
| 1085 | // kill markers. |
| 1086 | addPass(&StackSlotColoringID); |
Andrew Trick | 899f46c | 2012-02-15 07:57:03 +0000 | [diff] [blame] | 1087 | |
Dan Gohman | e32c574 | 2015-09-08 20:36:33 +0000 | [diff] [blame] | 1088 | // Run post-ra machine LICM to hoist reloads / remats. |
| 1089 | // |
| 1090 | // FIXME: can this move into MachineLateOptimization? |
| 1091 | addPass(&PostRAMachineLICMID); |
| 1092 | } |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | //===---------------------------------------------------------------------===// |
| 1096 | /// Post RegAlloc Pass Configuration |
| 1097 | //===---------------------------------------------------------------------===// |
| 1098 | |
| 1099 | /// Add passes that optimize machine instructions after register allocation. |
| 1100 | void TargetPassConfig::addMachineLateOptimization() { |
| 1101 | // Branch folding must be run after regalloc and prolog/epilog insertion. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 1102 | addPass(&BranchFolderPassID); |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 1103 | |
| 1104 | // Tail duplication. |
Vincent Lejeune | 92b0a64 | 2013-12-07 01:49:19 +0000 | [diff] [blame] | 1105 | // Note that duplicating tail just increases code size and degrades |
| 1106 | // performance for targets that require Structured Control Flow. |
| 1107 | // In addition it can also make CFG irreducible. Thus we disable it. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 1108 | if (!TM->requiresStructuredCFG()) |
| 1109 | addPass(&TailDuplicateID); |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 1110 | |
| 1111 | // Copy propagation. |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 1112 | addPass(&MachineCopyPropagationID); |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Evan Cheng | 59421ae | 2012-12-21 02:57:04 +0000 | [diff] [blame] | 1115 | /// Add standard GC passes. |
| 1116 | bool TargetPassConfig::addGCPasses() { |
Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 1117 | addPass(&GCMachineCodeAnalysisID, false); |
Evan Cheng | 59421ae | 2012-12-21 02:57:04 +0000 | [diff] [blame] | 1118 | return true; |
| 1119 | } |
| 1120 | |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 1121 | /// Add standard basic block placement passes. |
| 1122 | void TargetPassConfig::addBlockPlacement() { |
Matt Arsenault | 8023233 | 2016-06-09 23:31:55 +0000 | [diff] [blame] | 1123 | if (addPass(&MachineBlockPlacementID)) { |
Andrew Trick | e9a951c | 2012-02-15 03:21:51 +0000 | [diff] [blame] | 1124 | // Run a separate pass to collect block placement statistics. |
| 1125 | if (EnableBlockPlacementStats) |
Bob Wilson | b9b6936 | 2012-07-02 19:48:37 +0000 | [diff] [blame] | 1126 | addPass(&MachineBlockPlacementStatsID); |
Andrew Trick | f542675 | 2012-02-09 00:40:55 +0000 | [diff] [blame] | 1127 | } |
| 1128 | } |
Quentin Colombet | 0de43b2 | 2016-08-26 22:32:59 +0000 | [diff] [blame] | 1129 | |
| 1130 | //===---------------------------------------------------------------------===// |
| 1131 | /// GlobalISel Configuration |
| 1132 | //===---------------------------------------------------------------------===// |
Ahmed Bougacha | 120ae22 | 2017-03-01 23:33:08 +0000 | [diff] [blame] | 1133 | |
| 1134 | bool TargetPassConfig::isGlobalISelEnabled() const { |
| 1135 | return false; |
| 1136 | } |
| 1137 | |
Quentin Colombet | 0de43b2 | 2016-08-26 22:32:59 +0000 | [diff] [blame] | 1138 | bool TargetPassConfig::isGlobalISelAbortEnabled() const { |
Amara Emerson | 854d10d | 2018-01-02 16:30:47 +0000 | [diff] [blame^] | 1139 | if (EnableGlobalISelAbort.getNumOccurrences() > 0) |
| 1140 | return EnableGlobalISelAbort == 1; |
| 1141 | |
| 1142 | // When no abort behaviour is specified, we don't abort if the target says |
| 1143 | // that GISel is enabled. |
| 1144 | return !isGlobalISelEnabled(); |
Quentin Colombet | 1c06a73 | 2016-08-31 18:43:04 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | bool TargetPassConfig::reportDiagnosticWhenGlobalISelFallback() const { |
| 1148 | return EnableGlobalISelAbort == 2; |
Quentin Colombet | 0de43b2 | 2016-08-26 22:32:59 +0000 | [diff] [blame] | 1149 | } |