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