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