TargetPassConfig.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. //===- TargetPassConfig.cpp - Target independent code generation passes ---===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines interfaces to access the target independent code
  10. // generation passes provided by the LLVM backend.
  11. //
  12. //===---------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/TargetPassConfig.h"
  14. #include "llvm/ADT/DenseMap.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/Analysis/BasicAliasAnalysis.h"
  18. #include "llvm/Analysis/CFLAndersAliasAnalysis.h"
  19. #include "llvm/Analysis/CFLSteensAliasAnalysis.h"
  20. #include "llvm/Analysis/CallGraphSCCPass.h"
  21. #include "llvm/Analysis/ScopedNoAliasAA.h"
  22. #include "llvm/Analysis/TargetTransformInfo.h"
  23. #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
  24. #include "llvm/CodeGen/CSEConfigBase.h"
  25. #include "llvm/CodeGen/MachineFunctionPass.h"
  26. #include "llvm/CodeGen/MachinePassRegistry.h"
  27. #include "llvm/CodeGen/Passes.h"
  28. #include "llvm/CodeGen/RegAllocRegistry.h"
  29. #include "llvm/IR/IRPrintingPasses.h"
  30. #include "llvm/IR/LegacyPassManager.h"
  31. #include "llvm/IR/Verifier.h"
  32. #include "llvm/MC/MCAsmInfo.h"
  33. #include "llvm/MC/MCTargetOptions.h"
  34. #include "llvm/Pass.h"
  35. #include "llvm/Support/CodeGen.h"
  36. #include "llvm/Support/CommandLine.h"
  37. #include "llvm/Support/Compiler.h"
  38. #include "llvm/Support/Debug.h"
  39. #include "llvm/Support/ErrorHandling.h"
  40. #include "llvm/Support/Threading.h"
  41. #include "llvm/Support/SaveAndRestore.h"
  42. #include "llvm/Target/TargetMachine.h"
  43. #include "llvm/Transforms/Scalar.h"
  44. #include "llvm/Transforms/Utils.h"
  45. #include "llvm/Transforms/Utils/SymbolRewriter.h"
  46. #include <cassert>
  47. #include <string>
  48. using namespace llvm;
  49. cl::opt<bool> EnableIPRA("enable-ipra", cl::init(false), cl::Hidden,
  50. cl::desc("Enable interprocedural register allocation "
  51. "to reduce load/store at procedure calls."));
  52. static cl::opt<bool> DisablePostRASched("disable-post-ra", cl::Hidden,
  53. cl::desc("Disable Post Regalloc Scheduler"));
  54. static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden,
  55. cl::desc("Disable branch folding"));
  56. static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
  57. cl::desc("Disable tail duplication"));
  58. static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
  59. cl::desc("Disable pre-register allocation tail duplication"));
  60. static cl::opt<bool> DisableBlockPlacement("disable-block-placement",
  61. cl::Hidden, cl::desc("Disable probability-driven block placement"));
  62. static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats",
  63. cl::Hidden, cl::desc("Collect probability-driven block placement stats"));
  64. static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
  65. cl::desc("Disable Stack Slot Coloring"));
  66. static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden,
  67. cl::desc("Disable Machine Dead Code Elimination"));
  68. static cl::opt<bool> DisableEarlyIfConversion("disable-early-ifcvt", cl::Hidden,
  69. cl::desc("Disable Early If-conversion"));
  70. static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
  71. cl::desc("Disable Machine LICM"));
  72. static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden,
  73. cl::desc("Disable Machine Common Subexpression Elimination"));
  74. static cl::opt<cl::boolOrDefault> OptimizeRegAlloc(
  75. "optimize-regalloc", cl::Hidden,
  76. cl::desc("Enable optimized register allocation compilation path."));
  77. static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
  78. cl::Hidden,
  79. cl::desc("Disable Machine LICM"));
  80. static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
  81. cl::desc("Disable Machine Sinking"));
  82. static cl::opt<bool> DisablePostRAMachineSink("disable-postra-machine-sink",
  83. cl::Hidden,
  84. cl::desc("Disable PostRA Machine Sinking"));
  85. static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
  86. cl::desc("Disable Loop Strength Reduction Pass"));
  87. static cl::opt<bool> DisableConstantHoisting("disable-constant-hoisting",
  88. cl::Hidden, cl::desc("Disable ConstantHoisting"));
  89. static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
  90. cl::desc("Disable Codegen Prepare"));
  91. static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden,
  92. cl::desc("Disable Copy Propagation pass"));
  93. static cl::opt<bool> DisablePartialLibcallInlining("disable-partial-libcall-inlining",
  94. cl::Hidden, cl::desc("Disable Partial Libcall Inlining"));
  95. static cl::opt<bool> EnableImplicitNullChecks(
  96. "enable-implicit-null-checks",
  97. cl::desc("Fold null checks into faulting memory operations"),
  98. cl::init(false), cl::Hidden);
  99. static cl::opt<bool> DisableMergeICmps("disable-mergeicmps",
  100. cl::desc("Disable MergeICmps Pass"),
  101. cl::init(false), cl::Hidden);
  102. static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
  103. cl::desc("Print LLVM IR produced by the loop-reduce pass"));
  104. static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
  105. cl::desc("Print LLVM IR input to isel pass"));
  106. static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
  107. cl::desc("Dump garbage collector data"));
  108. static cl::opt<cl::boolOrDefault>
  109. VerifyMachineCode("verify-machineinstrs", cl::Hidden,
  110. cl::desc("Verify generated machine code"),
  111. cl::ZeroOrMore);
  112. enum RunOutliner { AlwaysOutline, NeverOutline, TargetDefault };
  113. // Enable or disable the MachineOutliner.
  114. static cl::opt<RunOutliner> EnableMachineOutliner(
  115. "enable-machine-outliner", cl::desc("Enable the machine outliner"),
  116. cl::Hidden, cl::ValueOptional, cl::init(TargetDefault),
  117. cl::values(clEnumValN(AlwaysOutline, "always",
  118. "Run on all functions guaranteed to be beneficial"),
  119. clEnumValN(NeverOutline, "never", "Disable all outlining"),
  120. // Sentinel value for unspecified option.
  121. clEnumValN(AlwaysOutline, "", "")));
  122. // Enable or disable FastISel. Both options are needed, because
  123. // FastISel is enabled by default with -fast, and we wish to be
  124. // able to enable or disable fast-isel independently from -O0.
  125. static cl::opt<cl::boolOrDefault>
  126. EnableFastISelOption("fast-isel", cl::Hidden,
  127. cl::desc("Enable the \"fast\" instruction selector"));
  128. static cl::opt<cl::boolOrDefault> EnableGlobalISelOption(
  129. "global-isel", cl::Hidden,
  130. cl::desc("Enable the \"global\" instruction selector"));
  131. static cl::opt<std::string> PrintMachineInstrs(
  132. "print-machineinstrs", cl::ValueOptional, cl::desc("Print machine instrs"),
  133. cl::value_desc("pass-name"), cl::init("option-unspecified"), cl::Hidden);
  134. static cl::opt<GlobalISelAbortMode> EnableGlobalISelAbort(
  135. "global-isel-abort", cl::Hidden,
  136. cl::desc("Enable abort calls when \"global\" instruction selection "
  137. "fails to lower/select an instruction"),
  138. cl::values(
  139. clEnumValN(GlobalISelAbortMode::Disable, "0", "Disable the abort"),
  140. clEnumValN(GlobalISelAbortMode::Enable, "1", "Enable the abort"),
  141. clEnumValN(GlobalISelAbortMode::DisableWithDiag, "2",
  142. "Disable the abort but emit a diagnostic on failure")));
  143. // Temporary option to allow experimenting with MachineScheduler as a post-RA
  144. // scheduler. Targets can "properly" enable this with
  145. // substitutePass(&PostRASchedulerID, &PostMachineSchedulerID).
  146. // Targets can return true in targetSchedulesPostRAScheduling() and
  147. // insert a PostRA scheduling pass wherever it wants.
  148. cl::opt<bool> MISchedPostRA("misched-postra", cl::Hidden,
  149. cl::desc("Run MachineScheduler post regalloc (independent of preRA sched)"));
  150. // Experimental option to run live interval analysis early.
  151. static cl::opt<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden,
  152. cl::desc("Run live interval analysis earlier in the pipeline"));
  153. // Experimental option to use CFL-AA in codegen
  154. enum class CFLAAType { None, Steensgaard, Andersen, Both };
  155. static cl::opt<CFLAAType> UseCFLAA(
  156. "use-cfl-aa-in-codegen", cl::init(CFLAAType::None), cl::Hidden,
  157. cl::desc("Enable the new, experimental CFL alias analysis in CodeGen"),
  158. cl::values(clEnumValN(CFLAAType::None, "none", "Disable CFL-AA"),
  159. clEnumValN(CFLAAType::Steensgaard, "steens",
  160. "Enable unification-based CFL-AA"),
  161. clEnumValN(CFLAAType::Andersen, "anders",
  162. "Enable inclusion-based CFL-AA"),
  163. clEnumValN(CFLAAType::Both, "both",
  164. "Enable both variants of CFL-AA")));
  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. static cl::opt<std::string>
  173. StartAfterOpt(StringRef(StartAfterOptName),
  174. cl::desc("Resume compilation after a specific pass"),
  175. cl::value_desc("pass-name"), cl::init(""), cl::Hidden);
  176. static cl::opt<std::string>
  177. StartBeforeOpt(StringRef(StartBeforeOptName),
  178. cl::desc("Resume compilation before a specific pass"),
  179. cl::value_desc("pass-name"), cl::init(""), cl::Hidden);
  180. static cl::opt<std::string>
  181. StopAfterOpt(StringRef(StopAfterOptName),
  182. cl::desc("Stop compilation after a specific pass"),
  183. cl::value_desc("pass-name"), cl::init(""), cl::Hidden);
  184. static cl::opt<std::string>
  185. StopBeforeOpt(StringRef(StopBeforeOptName),
  186. cl::desc("Stop compilation before a specific pass"),
  187. cl::value_desc("pass-name"), cl::init(""), cl::Hidden);
  188. /// Allow standard passes to be disabled by command line options. This supports
  189. /// simple binary flags that either suppress the pass or do nothing.
  190. /// i.e. -disable-mypass=false has no effect.
  191. /// These should be converted to boolOrDefault in order to use applyOverride.
  192. static IdentifyingPassPtr applyDisable(IdentifyingPassPtr PassID,
  193. bool Override) {
  194. if (Override)
  195. return IdentifyingPassPtr();
  196. return PassID;
  197. }
  198. /// Allow standard passes to be disabled by the command line, regardless of who
  199. /// is adding the pass.
  200. ///
  201. /// StandardID is the pass identified in the standard pass pipeline and provided
  202. /// to addPass(). It may be a target-specific ID in the case that the target
  203. /// directly adds its own pass, but in that case we harmlessly fall through.
  204. ///
  205. /// TargetID is the pass that the target has configured to override StandardID.
  206. ///
  207. /// StandardID may be a pseudo ID. In that case TargetID is the name of the real
  208. /// pass to run. This allows multiple options to control a single pass depending
  209. /// on where in the pipeline that pass is added.
  210. static IdentifyingPassPtr overridePass(AnalysisID StandardID,
  211. IdentifyingPassPtr TargetID) {
  212. if (StandardID == &PostRASchedulerID)
  213. return applyDisable(TargetID, DisablePostRASched);
  214. if (StandardID == &BranchFolderPassID)
  215. return applyDisable(TargetID, DisableBranchFold);
  216. if (StandardID == &TailDuplicateID)
  217. return applyDisable(TargetID, DisableTailDuplicate);
  218. if (StandardID == &EarlyTailDuplicateID)
  219. return applyDisable(TargetID, DisableEarlyTailDup);
  220. if (StandardID == &MachineBlockPlacementID)
  221. return applyDisable(TargetID, DisableBlockPlacement);
  222. if (StandardID == &StackSlotColoringID)
  223. return applyDisable(TargetID, DisableSSC);
  224. if (StandardID == &DeadMachineInstructionElimID)
  225. return applyDisable(TargetID, DisableMachineDCE);
  226. if (StandardID == &EarlyIfConverterID)
  227. return applyDisable(TargetID, DisableEarlyIfConversion);
  228. if (StandardID == &EarlyMachineLICMID)
  229. return applyDisable(TargetID, DisableMachineLICM);
  230. if (StandardID == &MachineCSEID)
  231. return applyDisable(TargetID, DisableMachineCSE);
  232. if (StandardID == &MachineLICMID)
  233. return applyDisable(TargetID, DisablePostRAMachineLICM);
  234. if (StandardID == &MachineSinkingID)
  235. return applyDisable(TargetID, DisableMachineSink);
  236. if (StandardID == &PostRAMachineSinkingID)
  237. return applyDisable(TargetID, DisablePostRAMachineSink);
  238. if (StandardID == &MachineCopyPropagationID)
  239. return applyDisable(TargetID, DisableCopyProp);
  240. return TargetID;
  241. }
  242. //===---------------------------------------------------------------------===//
  243. /// TargetPassConfig
  244. //===---------------------------------------------------------------------===//
  245. INITIALIZE_PASS(TargetPassConfig, "targetpassconfig",
  246. "Target Pass Configuration", false, false)
  247. char TargetPassConfig::ID = 0;
  248. namespace {
  249. struct InsertedPass {
  250. AnalysisID TargetPassID;
  251. IdentifyingPassPtr InsertedPassID;
  252. bool VerifyAfter;
  253. bool PrintAfter;
  254. InsertedPass(AnalysisID TargetPassID, IdentifyingPassPtr InsertedPassID,
  255. bool VerifyAfter, bool PrintAfter)
  256. : TargetPassID(TargetPassID), InsertedPassID(InsertedPassID),
  257. VerifyAfter(VerifyAfter), PrintAfter(PrintAfter) {}
  258. Pass *getInsertedPass() const {
  259. assert(InsertedPassID.isValid() && "Illegal Pass ID!");
  260. if (InsertedPassID.isInstance())
  261. return InsertedPassID.getInstance();
  262. Pass *NP = Pass::createPass(InsertedPassID.getID());
  263. assert(NP && "Pass ID not registered");
  264. return NP;
  265. }
  266. };
  267. } // end anonymous namespace
  268. namespace llvm {
  269. class PassConfigImpl {
  270. public:
  271. // List of passes explicitly substituted by this target. Normally this is
  272. // empty, but it is a convenient way to suppress or replace specific passes
  273. // that are part of a standard pass pipeline without overridding the entire
  274. // pipeline. This mechanism allows target options to inherit a standard pass's
  275. // user interface. For example, a target may disable a standard pass by
  276. // default by substituting a pass ID of zero, and the user may still enable
  277. // that standard pass with an explicit command line option.
  278. DenseMap<AnalysisID,IdentifyingPassPtr> TargetPasses;
  279. /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass
  280. /// is inserted after each instance of the first one.
  281. SmallVector<InsertedPass, 4> InsertedPasses;
  282. };
  283. } // end namespace llvm
  284. // Out of line virtual method.
  285. TargetPassConfig::~TargetPassConfig() {
  286. delete Impl;
  287. }
  288. static const PassInfo *getPassInfo(StringRef PassName) {
  289. if (PassName.empty())
  290. return nullptr;
  291. const PassRegistry &PR = *PassRegistry::getPassRegistry();
  292. const PassInfo *PI = PR.getPassInfo(PassName);
  293. if (!PI)
  294. report_fatal_error(Twine('\"') + Twine(PassName) +
  295. Twine("\" pass is not registered."));
  296. return PI;
  297. }
  298. static AnalysisID getPassIDFromName(StringRef PassName) {
  299. const PassInfo *PI = getPassInfo(PassName);
  300. return PI ? PI->getTypeInfo() : nullptr;
  301. }
  302. static std::pair<StringRef, unsigned>
  303. getPassNameAndInstanceNum(StringRef PassName) {
  304. StringRef Name, InstanceNumStr;
  305. std::tie(Name, InstanceNumStr) = PassName.split(',');
  306. unsigned InstanceNum = 0;
  307. if (!InstanceNumStr.empty() && InstanceNumStr.getAsInteger(10, InstanceNum))
  308. report_fatal_error("invalid pass instance specifier " + PassName);
  309. return std::make_pair(Name, InstanceNum);
  310. }
  311. void TargetPassConfig::setStartStopPasses() {
  312. StringRef StartBeforeName;
  313. std::tie(StartBeforeName, StartBeforeInstanceNum) =
  314. getPassNameAndInstanceNum(StartBeforeOpt);
  315. StringRef StartAfterName;
  316. std::tie(StartAfterName, StartAfterInstanceNum) =
  317. getPassNameAndInstanceNum(StartAfterOpt);
  318. StringRef StopBeforeName;
  319. std::tie(StopBeforeName, StopBeforeInstanceNum)
  320. = getPassNameAndInstanceNum(StopBeforeOpt);
  321. StringRef StopAfterName;
  322. std::tie(StopAfterName, StopAfterInstanceNum)
  323. = getPassNameAndInstanceNum(StopAfterOpt);
  324. StartBefore = getPassIDFromName(StartBeforeName);
  325. StartAfter = getPassIDFromName(StartAfterName);
  326. StopBefore = getPassIDFromName(StopBeforeName);
  327. StopAfter = getPassIDFromName(StopAfterName);
  328. if (StartBefore && StartAfter)
  329. report_fatal_error(Twine(StartBeforeOptName) + Twine(" and ") +
  330. Twine(StartAfterOptName) + Twine(" specified!"));
  331. if (StopBefore && StopAfter)
  332. report_fatal_error(Twine(StopBeforeOptName) + Twine(" and ") +
  333. Twine(StopAfterOptName) + Twine(" specified!"));
  334. Started = (StartAfter == nullptr) && (StartBefore == nullptr);
  335. }
  336. // Out of line constructor provides default values for pass options and
  337. // registers all common codegen passes.
  338. TargetPassConfig::TargetPassConfig(LLVMTargetMachine &TM, PassManagerBase &pm)
  339. : ImmutablePass(ID), PM(&pm), TM(&TM) {
  340. Impl = new PassConfigImpl();
  341. // Register all target independent codegen passes to activate their PassIDs,
  342. // including this pass itself.
  343. initializeCodeGen(*PassRegistry::getPassRegistry());
  344. // Also register alias analysis passes required by codegen passes.
  345. initializeBasicAAWrapperPassPass(*PassRegistry::getPassRegistry());
  346. initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
  347. if (StringRef(PrintMachineInstrs.getValue()).equals(""))
  348. TM.Options.PrintMachineCode = true;
  349. if (EnableIPRA.getNumOccurrences())
  350. TM.Options.EnableIPRA = EnableIPRA;
  351. else {
  352. // If not explicitly specified, use target default.
  353. TM.Options.EnableIPRA |= TM.useIPRA();
  354. }
  355. if (TM.Options.EnableIPRA)
  356. setRequiresCodeGenSCCOrder();
  357. if (EnableGlobalISelAbort.getNumOccurrences())
  358. TM.Options.GlobalISelAbort = EnableGlobalISelAbort;
  359. setStartStopPasses();
  360. }
  361. CodeGenOpt::Level TargetPassConfig::getOptLevel() const {
  362. return TM->getOptLevel();
  363. }
  364. /// Insert InsertedPassID pass after TargetPassID.
  365. void TargetPassConfig::insertPass(AnalysisID TargetPassID,
  366. IdentifyingPassPtr InsertedPassID,
  367. bool VerifyAfter, bool PrintAfter) {
  368. assert(((!InsertedPassID.isInstance() &&
  369. TargetPassID != InsertedPassID.getID()) ||
  370. (InsertedPassID.isInstance() &&
  371. TargetPassID != InsertedPassID.getInstance()->getPassID())) &&
  372. "Insert a pass after itself!");
  373. Impl->InsertedPasses.emplace_back(TargetPassID, InsertedPassID, VerifyAfter,
  374. PrintAfter);
  375. }
  376. /// createPassConfig - Create a pass configuration object to be used by
  377. /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
  378. ///
  379. /// Targets may override this to extend TargetPassConfig.
  380. TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
  381. return new TargetPassConfig(*this, PM);
  382. }
  383. TargetPassConfig::TargetPassConfig()
  384. : ImmutablePass(ID) {
  385. report_fatal_error("Trying to construct TargetPassConfig without a target "
  386. "machine. Scheduling a CodeGen pass without a target "
  387. "triple set?");
  388. }
  389. bool TargetPassConfig::willCompleteCodeGenPipeline() {
  390. return StopBeforeOpt.empty() && StopAfterOpt.empty();
  391. }
  392. bool TargetPassConfig::hasLimitedCodeGenPipeline() {
  393. return !StartBeforeOpt.empty() || !StartAfterOpt.empty() ||
  394. !willCompleteCodeGenPipeline();
  395. }
  396. std::string
  397. TargetPassConfig::getLimitedCodeGenPipelineReason(const char *Separator) const {
  398. if (!hasLimitedCodeGenPipeline())
  399. return std::string();
  400. std::string Res;
  401. static cl::opt<std::string> *PassNames[] = {&StartAfterOpt, &StartBeforeOpt,
  402. &StopAfterOpt, &StopBeforeOpt};
  403. static const char *OptNames[] = {StartAfterOptName, StartBeforeOptName,
  404. StopAfterOptName, StopBeforeOptName};
  405. bool IsFirst = true;
  406. for (int Idx = 0; Idx < 4; ++Idx)
  407. if (!PassNames[Idx]->empty()) {
  408. if (!IsFirst)
  409. Res += Separator;
  410. IsFirst = false;
  411. Res += OptNames[Idx];
  412. }
  413. return Res;
  414. }
  415. // Helper to verify the analysis is really immutable.
  416. void TargetPassConfig::setOpt(bool &Opt, bool Val) {
  417. assert(!Initialized && "PassConfig is immutable");
  418. Opt = Val;
  419. }
  420. void TargetPassConfig::substitutePass(AnalysisID StandardID,
  421. IdentifyingPassPtr TargetID) {
  422. Impl->TargetPasses[StandardID] = TargetID;
  423. }
  424. IdentifyingPassPtr TargetPassConfig::getPassSubstitution(AnalysisID ID) const {
  425. DenseMap<AnalysisID, IdentifyingPassPtr>::const_iterator
  426. I = Impl->TargetPasses.find(ID);
  427. if (I == Impl->TargetPasses.end())
  428. return ID;
  429. return I->second;
  430. }
  431. bool TargetPassConfig::isPassSubstitutedOrOverridden(AnalysisID ID) const {
  432. IdentifyingPassPtr TargetID = getPassSubstitution(ID);
  433. IdentifyingPassPtr FinalPtr = overridePass(ID, TargetID);
  434. return !FinalPtr.isValid() || FinalPtr.isInstance() ||
  435. FinalPtr.getID() != ID;
  436. }
  437. /// Add a pass to the PassManager if that pass is supposed to be run. If the
  438. /// Started/Stopped flags indicate either that the compilation should start at
  439. /// a later pass or that it should stop after an earlier pass, then do not add
  440. /// the pass. Finally, compare the current pass against the StartAfter
  441. /// and StopAfter options and change the Started/Stopped flags accordingly.
  442. void TargetPassConfig::addPass(Pass *P, bool verifyAfter, bool printAfter) {
  443. assert(!Initialized && "PassConfig is immutable");
  444. // Cache the Pass ID here in case the pass manager finds this pass is
  445. // redundant with ones already scheduled / available, and deletes it.
  446. // Fundamentally, once we add the pass to the manager, we no longer own it
  447. // and shouldn't reference it.
  448. AnalysisID PassID = P->getPassID();
  449. if (StartBefore == PassID && StartBeforeCount++ == StartBeforeInstanceNum)
  450. Started = true;
  451. if (StopBefore == PassID && StopBeforeCount++ == StopBeforeInstanceNum)
  452. Stopped = true;
  453. if (Started && !Stopped) {
  454. std::string Banner;
  455. // Construct banner message before PM->add() as that may delete the pass.
  456. if (AddingMachinePasses && (printAfter || verifyAfter))
  457. Banner = std::string("After ") + std::string(P->getPassName());
  458. PM->add(P);
  459. if (AddingMachinePasses) {
  460. if (printAfter)
  461. addPrintPass(Banner);
  462. if (verifyAfter)
  463. addVerifyPass(Banner);
  464. }
  465. // Add the passes after the pass P if there is any.
  466. for (auto IP : Impl->InsertedPasses) {
  467. if (IP.TargetPassID == PassID)
  468. addPass(IP.getInsertedPass(), IP.VerifyAfter, IP.PrintAfter);
  469. }
  470. } else {
  471. delete P;
  472. }
  473. if (StopAfter == PassID && StopAfterCount++ == StopAfterInstanceNum)
  474. Stopped = true;
  475. if (StartAfter == PassID && StartAfterCount++ == StartAfterInstanceNum)
  476. Started = true;
  477. if (Stopped && !Started)
  478. report_fatal_error("Cannot stop compilation after pass that is not run");
  479. }
  480. /// Add a CodeGen pass at this point in the pipeline after checking for target
  481. /// and command line overrides.
  482. ///
  483. /// addPass cannot return a pointer to the pass instance because is internal the
  484. /// PassManager and the instance we create here may already be freed.
  485. AnalysisID TargetPassConfig::addPass(AnalysisID PassID, bool verifyAfter,
  486. bool printAfter) {
  487. IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
  488. IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID);
  489. if (!FinalPtr.isValid())
  490. return nullptr;
  491. Pass *P;
  492. if (FinalPtr.isInstance())
  493. P = FinalPtr.getInstance();
  494. else {
  495. P = Pass::createPass(FinalPtr.getID());
  496. if (!P)
  497. llvm_unreachable("Pass ID not registered");
  498. }
  499. AnalysisID FinalID = P->getPassID();
  500. addPass(P, verifyAfter, printAfter); // Ends the lifetime of P.
  501. return FinalID;
  502. }
  503. void TargetPassConfig::printAndVerify(const std::string &Banner) {
  504. addPrintPass(Banner);
  505. addVerifyPass(Banner);
  506. }
  507. void TargetPassConfig::addPrintPass(const std::string &Banner) {
  508. if (TM->shouldPrintMachineCode())
  509. PM->add(createMachineFunctionPrinterPass(dbgs(), Banner));
  510. }
  511. void TargetPassConfig::addVerifyPass(const std::string &Banner) {
  512. bool Verify = VerifyMachineCode == cl::BOU_TRUE;
  513. #ifdef EXPENSIVE_CHECKS
  514. if (VerifyMachineCode == cl::BOU_UNSET)
  515. Verify = TM->isMachineVerifierClean();
  516. #endif
  517. if (Verify)
  518. PM->add(createMachineVerifierPass(Banner));
  519. }
  520. /// Add common target configurable passes that perform LLVM IR to IR transforms
  521. /// following machine independent optimization.
  522. void TargetPassConfig::addIRPasses() {
  523. switch (UseCFLAA) {
  524. case CFLAAType::Steensgaard:
  525. addPass(createCFLSteensAAWrapperPass());
  526. break;
  527. case CFLAAType::Andersen:
  528. addPass(createCFLAndersAAWrapperPass());
  529. break;
  530. case CFLAAType::Both:
  531. addPass(createCFLAndersAAWrapperPass());
  532. addPass(createCFLSteensAAWrapperPass());
  533. break;
  534. default:
  535. break;
  536. }
  537. // Basic AliasAnalysis support.
  538. // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
  539. // BasicAliasAnalysis wins if they disagree. This is intended to help
  540. // support "obvious" type-punning idioms.
  541. addPass(createTypeBasedAAWrapperPass());
  542. addPass(createScopedNoAliasAAWrapperPass());
  543. addPass(createBasicAAWrapperPass());
  544. // Before running any passes, run the verifier to determine if the input
  545. // coming from the front-end and/or optimizer is valid.
  546. if (!DisableVerify)
  547. addPass(createVerifierPass());
  548. // Run loop strength reduction before anything else.
  549. if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
  550. addPass(createLoopStrengthReducePass());
  551. if (PrintLSR)
  552. addPass(createPrintFunctionPass(dbgs(), "\n\n*** Code after LSR ***\n"));
  553. }
  554. if (getOptLevel() != CodeGenOpt::None) {
  555. // The MergeICmpsPass tries to create memcmp calls by grouping sequences of
  556. // loads and compares. ExpandMemCmpPass then tries to expand those calls
  557. // into optimally-sized loads and compares. The transforms are enabled by a
  558. // target lowering hook.
  559. if (!DisableMergeICmps)
  560. addPass(createMergeICmpsLegacyPass());
  561. addPass(createExpandMemCmpPass());
  562. }
  563. // Run GC lowering passes for builtin collectors
  564. // TODO: add a pass insertion point here
  565. addPass(createGCLoweringPass());
  566. addPass(createShadowStackGCLoweringPass());
  567. // Make sure that no unreachable blocks are instruction selected.
  568. addPass(createUnreachableBlockEliminationPass());
  569. // Prepare expensive constants for SelectionDAG.
  570. if (getOptLevel() != CodeGenOpt::None && !DisableConstantHoisting)
  571. addPass(createConstantHoistingPass());
  572. if (getOptLevel() != CodeGenOpt::None && !DisablePartialLibcallInlining)
  573. addPass(createPartiallyInlineLibCallsPass());
  574. // Instrument function entry and exit, e.g. with calls to mcount().
  575. addPass(createPostInlineEntryExitInstrumenterPass());
  576. // Add scalarization of target's unsupported masked memory intrinsics pass.
  577. // the unsupported intrinsic will be replaced with a chain of basic blocks,
  578. // that stores/loads element one-by-one if the appropriate mask bit is set.
  579. addPass(createScalarizeMaskedMemIntrinPass());
  580. // Expand reduction intrinsics into shuffle sequences if the target wants to.
  581. addPass(createExpandReductionsPass());
  582. }
  583. /// Turn exception handling constructs into something the code generators can
  584. /// handle.
  585. void TargetPassConfig::addPassesToHandleExceptions() {
  586. const MCAsmInfo *MCAI = TM->getMCAsmInfo();
  587. assert(MCAI && "No MCAsmInfo");
  588. switch (MCAI->getExceptionHandlingType()) {
  589. case ExceptionHandling::SjLj:
  590. // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
  591. // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
  592. // catch info can get misplaced when a selector ends up more than one block
  593. // removed from the parent invoke(s). This could happen when a landing
  594. // pad is shared by multiple invokes and is also a target of a normal
  595. // edge from elsewhere.
  596. addPass(createSjLjEHPreparePass());
  597. LLVM_FALLTHROUGH;
  598. case ExceptionHandling::DwarfCFI:
  599. case ExceptionHandling::ARM:
  600. addPass(createDwarfEHPass());
  601. break;
  602. case ExceptionHandling::WinEH:
  603. // We support using both GCC-style and MSVC-style exceptions on Windows, so
  604. // add both preparation passes. Each pass will only actually run if it
  605. // recognizes the personality function.
  606. addPass(createWinEHPass());
  607. addPass(createDwarfEHPass());
  608. break;
  609. case ExceptionHandling::Wasm:
  610. // Wasm EH uses Windows EH instructions, but it does not need to demote PHIs
  611. // on catchpads and cleanuppads because it does not outline them into
  612. // funclets. Catchswitch blocks are not lowered in SelectionDAG, so we
  613. // should remove PHIs there.
  614. addPass(createWinEHPass(/*DemoteCatchSwitchPHIOnly=*/false));
  615. addPass(createWasmEHPass());
  616. break;
  617. case ExceptionHandling::None:
  618. addPass(createLowerInvokePass());
  619. // The lower invoke pass may create unreachable code. Remove it.
  620. addPass(createUnreachableBlockEliminationPass());
  621. break;
  622. }
  623. }
  624. /// Add pass to prepare the LLVM IR for code generation. This should be done
  625. /// before exception handling preparation passes.
  626. void TargetPassConfig::addCodeGenPrepare() {
  627. if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
  628. addPass(createCodeGenPreparePass());
  629. addPass(createRewriteSymbolsPass());
  630. }
  631. /// Add common passes that perform LLVM IR to IR transforms in preparation for
  632. /// instruction selection.
  633. void TargetPassConfig::addISelPrepare() {
  634. addPreISel();
  635. // Force codegen to run according to the callgraph.
  636. if (requiresCodeGenSCCOrder())
  637. addPass(new DummyCGSCCPass);
  638. // Add both the safe stack and the stack protection passes: each of them will
  639. // only protect functions that have corresponding attributes.
  640. addPass(createSafeStackPass());
  641. addPass(createStackProtectorPass());
  642. if (PrintISelInput)
  643. addPass(createPrintFunctionPass(
  644. dbgs(), "\n\n*** Final LLVM Code input to ISel ***\n"));
  645. // All passes which modify the LLVM IR are now complete; run the verifier
  646. // to ensure that the IR is valid.
  647. if (!DisableVerify)
  648. addPass(createVerifierPass());
  649. }
  650. bool TargetPassConfig::addCoreISelPasses() {
  651. // Enable FastISel with -fast-isel, but allow that to be overridden.
  652. TM->setO0WantsFastISel(EnableFastISelOption != cl::BOU_FALSE);
  653. // Determine an instruction selector.
  654. enum class SelectorType { SelectionDAG, FastISel, GlobalISel };
  655. SelectorType Selector;
  656. if (EnableFastISelOption == cl::BOU_TRUE)
  657. Selector = SelectorType::FastISel;
  658. else if (EnableGlobalISelOption == cl::BOU_TRUE ||
  659. (TM->Options.EnableGlobalISel &&
  660. EnableGlobalISelOption != cl::BOU_FALSE))
  661. Selector = SelectorType::GlobalISel;
  662. else if (TM->getOptLevel() == CodeGenOpt::None && TM->getO0WantsFastISel())
  663. Selector = SelectorType::FastISel;
  664. else
  665. Selector = SelectorType::SelectionDAG;
  666. // Set consistently TM->Options.EnableFastISel and EnableGlobalISel.
  667. if (Selector == SelectorType::FastISel) {
  668. TM->setFastISel(true);
  669. TM->setGlobalISel(false);
  670. } else if (Selector == SelectorType::GlobalISel) {
  671. TM->setFastISel(false);
  672. TM->setGlobalISel(true);
  673. }
  674. // Add instruction selector passes.
  675. if (Selector == SelectorType::GlobalISel) {
  676. SaveAndRestore<bool> SavedAddingMachinePasses(AddingMachinePasses, true);
  677. if (addIRTranslator())
  678. return true;
  679. addPreLegalizeMachineIR();
  680. if (addLegalizeMachineIR())
  681. return true;
  682. // Before running the register bank selector, ask the target if it
  683. // wants to run some passes.
  684. addPreRegBankSelect();
  685. if (addRegBankSelect())
  686. return true;
  687. addPreGlobalInstructionSelect();
  688. if (addGlobalInstructionSelect())
  689. return true;
  690. // Pass to reset the MachineFunction if the ISel failed.
  691. addPass(createResetMachineFunctionPass(
  692. reportDiagnosticWhenGlobalISelFallback(), isGlobalISelAbortEnabled()));
  693. // Provide a fallback path when we do not want to abort on
  694. // not-yet-supported input.
  695. if (!isGlobalISelAbortEnabled() && addInstSelector())
  696. return true;
  697. } else if (addInstSelector())
  698. return true;
  699. // Expand pseudo-instructions emitted by ISel. Don't run the verifier before
  700. // FinalizeISel.
  701. addPass(&FinalizeISelID);
  702. // Print the instruction selected machine code...
  703. printAndVerify("After Instruction Selection");
  704. return false;
  705. }
  706. bool TargetPassConfig::addISelPasses() {
  707. if (TM->useEmulatedTLS())
  708. addPass(createLowerEmuTLSPass());
  709. addPass(createPreISelIntrinsicLoweringPass());
  710. addPass(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
  711. addIRPasses();
  712. addCodeGenPrepare();
  713. addPassesToHandleExceptions();
  714. addISelPrepare();
  715. return addCoreISelPasses();
  716. }
  717. /// -regalloc=... command line option.
  718. static FunctionPass *useDefaultRegisterAllocator() { return nullptr; }
  719. static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
  720. RegisterPassParser<RegisterRegAlloc>>
  721. RegAlloc("regalloc", cl::Hidden, cl::init(&useDefaultRegisterAllocator),
  722. cl::desc("Register allocator to use"));
  723. /// Add the complete set of target-independent postISel code generator passes.
  724. ///
  725. /// This can be read as the standard order of major LLVM CodeGen stages. Stages
  726. /// with nontrivial configuration or multiple passes are broken out below in
  727. /// add%Stage routines.
  728. ///
  729. /// Any TargetPassConfig::addXX routine may be overriden by the Target. The
  730. /// addPre/Post methods with empty header implementations allow injecting
  731. /// target-specific fixups just before or after major stages. Additionally,
  732. /// targets have the flexibility to change pass order within a stage by
  733. /// overriding default implementation of add%Stage routines below. Each
  734. /// technique has maintainability tradeoffs because alternate pass orders are
  735. /// not well supported. addPre/Post works better if the target pass is easily
  736. /// tied to a common pass. But if it has subtle dependencies on multiple passes,
  737. /// the target should override the stage instead.
  738. ///
  739. /// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
  740. /// before/after any target-independent pass. But it's currently overkill.
  741. void TargetPassConfig::addMachinePasses() {
  742. AddingMachinePasses = true;
  743. // Insert a machine instr printer pass after the specified pass.
  744. StringRef PrintMachineInstrsPassName = PrintMachineInstrs.getValue();
  745. if (!PrintMachineInstrsPassName.equals("") &&
  746. !PrintMachineInstrsPassName.equals("option-unspecified")) {
  747. if (const PassInfo *TPI = getPassInfo(PrintMachineInstrsPassName)) {
  748. const PassRegistry *PR = PassRegistry::getPassRegistry();
  749. const PassInfo *IPI = PR->getPassInfo(StringRef("machineinstr-printer"));
  750. assert(IPI && "failed to get \"machineinstr-printer\" PassInfo!");
  751. const char *TID = (const char *)(TPI->getTypeInfo());
  752. const char *IID = (const char *)(IPI->getTypeInfo());
  753. insertPass(TID, IID);
  754. }
  755. }
  756. // Add passes that optimize machine instructions in SSA form.
  757. if (getOptLevel() != CodeGenOpt::None) {
  758. addMachineSSAOptimization();
  759. } else {
  760. // If the target requests it, assign local variables to stack slots relative
  761. // to one another and simplify frame index references where possible.
  762. addPass(&LocalStackSlotAllocationID, false);
  763. }
  764. if (TM->Options.EnableIPRA)
  765. addPass(createRegUsageInfoPropPass());
  766. // Run pre-ra passes.
  767. addPreRegAlloc();
  768. // Run register allocation and passes that are tightly coupled with it,
  769. // including phi elimination and scheduling.
  770. if (getOptimizeRegAlloc())
  771. addOptimizedRegAlloc();
  772. else
  773. addFastRegAlloc();
  774. // Run post-ra passes.
  775. addPostRegAlloc();
  776. // Insert prolog/epilog code. Eliminate abstract frame index references...
  777. if (getOptLevel() != CodeGenOpt::None) {
  778. addPass(&PostRAMachineSinkingID);
  779. addPass(&ShrinkWrapID);
  780. }
  781. // Prolog/Epilog inserter needs a TargetMachine to instantiate. But only
  782. // do so if it hasn't been disabled, substituted, or overridden.
  783. if (!isPassSubstitutedOrOverridden(&PrologEpilogCodeInserterID))
  784. addPass(createPrologEpilogInserterPass());
  785. /// Add passes that optimize machine instructions after register allocation.
  786. if (getOptLevel() != CodeGenOpt::None)
  787. addMachineLateOptimization();
  788. // Expand pseudo instructions before second scheduling pass.
  789. addPass(&ExpandPostRAPseudosID);
  790. // Run pre-sched2 passes.
  791. addPreSched2();
  792. if (EnableImplicitNullChecks)
  793. addPass(&ImplicitNullChecksID);
  794. // Second pass scheduler.
  795. // Let Target optionally insert this pass by itself at some other
  796. // point.
  797. if (getOptLevel() != CodeGenOpt::None &&
  798. !TM->targetSchedulesPostRAScheduling()) {
  799. if (MISchedPostRA)
  800. addPass(&PostMachineSchedulerID);
  801. else
  802. addPass(&PostRASchedulerID);
  803. }
  804. // GC
  805. if (addGCPasses()) {
  806. if (PrintGCInfo)
  807. addPass(createGCInfoPrinter(dbgs()), false, false);
  808. }
  809. // Basic block placement.
  810. if (getOptLevel() != CodeGenOpt::None)
  811. addBlockPlacement();
  812. addPreEmitPass();
  813. if (TM->Options.EnableIPRA)
  814. // Collect register usage information and produce a register mask of
  815. // clobbered registers, to be used to optimize call sites.
  816. addPass(createRegUsageInfoCollector());
  817. addPass(&FuncletLayoutID, false);
  818. addPass(&StackMapLivenessID, false);
  819. addPass(&LiveDebugValuesID, false);
  820. // Insert before XRay Instrumentation.
  821. addPass(&FEntryInserterID, false);
  822. addPass(&XRayInstrumentationID, false);
  823. addPass(&PatchableFunctionID, false);
  824. if (TM->Options.EnableMachineOutliner && getOptLevel() != CodeGenOpt::None &&
  825. EnableMachineOutliner != NeverOutline) {
  826. bool RunOnAllFunctions = (EnableMachineOutliner == AlwaysOutline);
  827. bool AddOutliner = RunOnAllFunctions ||
  828. TM->Options.SupportsDefaultOutlining;
  829. if (AddOutliner)
  830. addPass(createMachineOutlinerPass(RunOnAllFunctions));
  831. }
  832. // Add passes that directly emit MI after all other MI passes.
  833. addPreEmitPass2();
  834. AddingMachinePasses = false;
  835. }
  836. /// Add passes that optimize machine instructions in SSA form.
  837. void TargetPassConfig::addMachineSSAOptimization() {
  838. // Pre-ra tail duplication.
  839. addPass(&EarlyTailDuplicateID);
  840. // Optimize PHIs before DCE: removing dead PHI cycles may make more
  841. // instructions dead.
  842. addPass(&OptimizePHIsID, false);
  843. // This pass merges large allocas. StackSlotColoring is a different pass
  844. // which merges spill slots.
  845. addPass(&StackColoringID, false);
  846. // If the target requests it, assign local variables to stack slots relative
  847. // to one another and simplify frame index references where possible.
  848. addPass(&LocalStackSlotAllocationID, false);
  849. // With optimization, dead code should already be eliminated. However
  850. // there is one known exception: lowered code for arguments that are only
  851. // used by tail calls, where the tail calls reuse the incoming stack
  852. // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
  853. addPass(&DeadMachineInstructionElimID);
  854. // Allow targets to insert passes that improve instruction level parallelism,
  855. // like if-conversion. Such passes will typically need dominator trees and
  856. // loop info, just like LICM and CSE below.
  857. addILPOpts();
  858. addPass(&EarlyMachineLICMID, false);
  859. addPass(&MachineCSEID, false);
  860. addPass(&MachineSinkingID);
  861. addPass(&PeepholeOptimizerID);
  862. // Clean-up the dead code that may have been generated by peephole
  863. // rewriting.
  864. addPass(&DeadMachineInstructionElimID);
  865. }
  866. //===---------------------------------------------------------------------===//
  867. /// Register Allocation Pass Configuration
  868. //===---------------------------------------------------------------------===//
  869. bool TargetPassConfig::getOptimizeRegAlloc() const {
  870. switch (OptimizeRegAlloc) {
  871. case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
  872. case cl::BOU_TRUE: return true;
  873. case cl::BOU_FALSE: return false;
  874. }
  875. llvm_unreachable("Invalid optimize-regalloc state");
  876. }
  877. /// A dummy default pass factory indicates whether the register allocator is
  878. /// overridden on the command line.
  879. static llvm::once_flag InitializeDefaultRegisterAllocatorFlag;
  880. static RegisterRegAlloc
  881. defaultRegAlloc("default",
  882. "pick register allocator based on -O option",
  883. useDefaultRegisterAllocator);
  884. static void initializeDefaultRegisterAllocatorOnce() {
  885. RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
  886. if (!Ctor) {
  887. Ctor = RegAlloc;
  888. RegisterRegAlloc::setDefault(RegAlloc);
  889. }
  890. }
  891. /// Instantiate the default register allocator pass for this target for either
  892. /// the optimized or unoptimized allocation path. This will be added to the pass
  893. /// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
  894. /// in the optimized case.
  895. ///
  896. /// A target that uses the standard regalloc pass order for fast or optimized
  897. /// allocation may still override this for per-target regalloc
  898. /// selection. But -regalloc=... always takes precedence.
  899. FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
  900. if (Optimized)
  901. return createGreedyRegisterAllocator();
  902. else
  903. return createFastRegisterAllocator();
  904. }
  905. /// Find and instantiate the register allocation pass requested by this target
  906. /// at the current optimization level. Different register allocators are
  907. /// defined as separate passes because they may require different analysis.
  908. ///
  909. /// This helper ensures that the regalloc= option is always available,
  910. /// even for targets that override the default allocator.
  911. ///
  912. /// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
  913. /// this can be folded into addPass.
  914. FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
  915. // Initialize the global default.
  916. llvm::call_once(InitializeDefaultRegisterAllocatorFlag,
  917. initializeDefaultRegisterAllocatorOnce);
  918. RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
  919. if (Ctor != useDefaultRegisterAllocator)
  920. return Ctor();
  921. // With no -regalloc= override, ask the target for a regalloc pass.
  922. return createTargetRegisterAllocator(Optimized);
  923. }
  924. bool TargetPassConfig::addRegAssignmentFast() {
  925. if (RegAlloc != &useDefaultRegisterAllocator &&
  926. RegAlloc != &createFastRegisterAllocator)
  927. report_fatal_error("Must use fast (default) register allocator for unoptimized regalloc.");
  928. addPass(createRegAllocPass(false));
  929. return true;
  930. }
  931. bool TargetPassConfig::addRegAssignmentOptimized() {
  932. // Add the selected register allocation pass.
  933. addPass(createRegAllocPass(true));
  934. // Allow targets to change the register assignments before rewriting.
  935. addPreRewrite();
  936. // Finally rewrite virtual registers.
  937. addPass(&VirtRegRewriterID);
  938. // Perform stack slot coloring and post-ra machine LICM.
  939. //
  940. // FIXME: Re-enable coloring with register when it's capable of adding
  941. // kill markers.
  942. addPass(&StackSlotColoringID);
  943. return true;
  944. }
  945. /// Return true if the default global register allocator is in use and
  946. /// has not be overriden on the command line with '-regalloc=...'
  947. bool TargetPassConfig::usingDefaultRegAlloc() const {
  948. return RegAlloc.getNumOccurrences() == 0;
  949. }
  950. /// Add the minimum set of target-independent passes that are required for
  951. /// register allocation. No coalescing or scheduling.
  952. void TargetPassConfig::addFastRegAlloc() {
  953. addPass(&PHIEliminationID, false);
  954. addPass(&TwoAddressInstructionPassID, false);
  955. addRegAssignmentFast();
  956. }
  957. /// Add standard target-independent passes that are tightly coupled with
  958. /// optimized register allocation, including coalescing, machine instruction
  959. /// scheduling, and register allocation itself.
  960. void TargetPassConfig::addOptimizedRegAlloc() {
  961. addPass(&DetectDeadLanesID, false);
  962. addPass(&ProcessImplicitDefsID, false);
  963. // LiveVariables currently requires pure SSA form.
  964. //
  965. // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
  966. // LiveVariables can be removed completely, and LiveIntervals can be directly
  967. // computed. (We still either need to regenerate kill flags after regalloc, or
  968. // preferably fix the scavenger to not depend on them).
  969. addPass(&LiveVariablesID, false);
  970. // Edge splitting is smarter with machine loop info.
  971. addPass(&MachineLoopInfoID, false);
  972. addPass(&PHIEliminationID, false);
  973. // Eventually, we want to run LiveIntervals before PHI elimination.
  974. if (EarlyLiveIntervals)
  975. addPass(&LiveIntervalsID, false);
  976. addPass(&TwoAddressInstructionPassID, false);
  977. addPass(&RegisterCoalescerID);
  978. // The machine scheduler may accidentally create disconnected components
  979. // when moving subregister definitions around, avoid this by splitting them to
  980. // separate vregs before. Splitting can also improve reg. allocation quality.
  981. addPass(&RenameIndependentSubregsID);
  982. // PreRA instruction scheduling.
  983. addPass(&MachineSchedulerID);
  984. if (addRegAssignmentOptimized()) {
  985. // Allow targets to expand pseudo instructions depending on the choice of
  986. // registers before MachineCopyPropagation.
  987. addPostRewrite();
  988. // Copy propagate to forward register uses and try to eliminate COPYs that
  989. // were not coalesced.
  990. addPass(&MachineCopyPropagationID);
  991. // Run post-ra machine LICM to hoist reloads / remats.
  992. //
  993. // FIXME: can this move into MachineLateOptimization?
  994. addPass(&MachineLICMID);
  995. }
  996. }
  997. //===---------------------------------------------------------------------===//
  998. /// Post RegAlloc Pass Configuration
  999. //===---------------------------------------------------------------------===//
  1000. /// Add passes that optimize machine instructions after register allocation.
  1001. void TargetPassConfig::addMachineLateOptimization() {
  1002. // Branch folding must be run after regalloc and prolog/epilog insertion.
  1003. addPass(&BranchFolderPassID);
  1004. // Tail duplication.
  1005. // Note that duplicating tail just increases code size and degrades
  1006. // performance for targets that require Structured Control Flow.
  1007. // In addition it can also make CFG irreducible. Thus we disable it.
  1008. if (!TM->requiresStructuredCFG())
  1009. addPass(&TailDuplicateID);
  1010. // Copy propagation.
  1011. addPass(&MachineCopyPropagationID);
  1012. }
  1013. /// Add standard GC passes.
  1014. bool TargetPassConfig::addGCPasses() {
  1015. addPass(&GCMachineCodeAnalysisID, false);
  1016. return true;
  1017. }
  1018. /// Add standard basic block placement passes.
  1019. void TargetPassConfig::addBlockPlacement() {
  1020. if (addPass(&MachineBlockPlacementID)) {
  1021. // Run a separate pass to collect block placement statistics.
  1022. if (EnableBlockPlacementStats)
  1023. addPass(&MachineBlockPlacementStatsID);
  1024. }
  1025. }
  1026. //===---------------------------------------------------------------------===//
  1027. /// GlobalISel Configuration
  1028. //===---------------------------------------------------------------------===//
  1029. bool TargetPassConfig::isGlobalISelAbortEnabled() const {
  1030. return TM->Options.GlobalISelAbort == GlobalISelAbortMode::Enable;
  1031. }
  1032. bool TargetPassConfig::reportDiagnosticWhenGlobalISelFallback() const {
  1033. return TM->Options.GlobalISelAbort == GlobalISelAbortMode::DisableWithDiag;
  1034. }
  1035. bool TargetPassConfig::isGISelCSEEnabled() const {
  1036. return true;
  1037. }
  1038. std::unique_ptr<CSEConfigBase> TargetPassConfig::getCSEConfig() const {
  1039. return make_unique<CSEConfigBase>();
  1040. }