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