TargetPassConfig.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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. // Make sure that no unreachable blocks are instruction selected.
  571. addPass(createUnreachableBlockEliminationPass());
  572. // Prepare expensive constants for SelectionDAG.
  573. if (getOptLevel() != CodeGenOpt::None && !DisableConstantHoisting)
  574. addPass(createConstantHoistingPass());
  575. if (getOptLevel() != CodeGenOpt::None && !DisablePartialLibcallInlining)
  576. addPass(createPartiallyInlineLibCallsPass());
  577. // Instrument function entry and exit, e.g. with calls to mcount().
  578. addPass(createPostInlineEntryExitInstrumenterPass());
  579. // Add scalarization of target's unsupported masked memory intrinsics pass.
  580. // the unsupported intrinsic will be replaced with a chain of basic blocks,
  581. // that stores/loads element one-by-one if the appropriate mask bit is set.
  582. addPass(createScalarizeMaskedMemIntrinPass());
  583. // Expand reduction intrinsics into shuffle sequences if the target wants to.
  584. addPass(createExpandReductionsPass());
  585. }
  586. /// Turn exception handling constructs into something the code generators can
  587. /// handle.
  588. void TargetPassConfig::addPassesToHandleExceptions() {
  589. const MCAsmInfo *MCAI = TM->getMCAsmInfo();
  590. assert(MCAI && "No MCAsmInfo");
  591. switch (MCAI->getExceptionHandlingType()) {
  592. case ExceptionHandling::SjLj:
  593. // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
  594. // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
  595. // catch info can get misplaced when a selector ends up more than one block
  596. // removed from the parent invoke(s). This could happen when a landing
  597. // pad is shared by multiple invokes and is also a target of a normal
  598. // edge from elsewhere.
  599. addPass(createSjLjEHPreparePass());
  600. LLVM_FALLTHROUGH;
  601. case ExceptionHandling::DwarfCFI:
  602. case ExceptionHandling::ARM:
  603. addPass(createDwarfEHPass());
  604. break;
  605. case ExceptionHandling::WinEH:
  606. // We support using both GCC-style and MSVC-style exceptions on Windows, so
  607. // add both preparation passes. Each pass will only actually run if it
  608. // recognizes the personality function.
  609. addPass(createWinEHPass());
  610. addPass(createDwarfEHPass());
  611. break;
  612. case ExceptionHandling::Wasm:
  613. // Wasm EH uses Windows EH instructions, but it does not need to demote PHIs
  614. // on catchpads and cleanuppads because it does not outline them into
  615. // funclets. Catchswitch blocks are not lowered in SelectionDAG, so we
  616. // should remove PHIs there.
  617. addPass(createWinEHPass(/*DemoteCatchSwitchPHIOnly=*/false));
  618. addPass(createWasmEHPass());
  619. break;
  620. case ExceptionHandling::None:
  621. addPass(createLowerInvokePass());
  622. // The lower invoke pass may create unreachable code. Remove it.
  623. addPass(createUnreachableBlockEliminationPass());
  624. break;
  625. }
  626. }
  627. /// Add pass to prepare the LLVM IR for code generation. This should be done
  628. /// before exception handling preparation passes.
  629. void TargetPassConfig::addCodeGenPrepare() {
  630. if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
  631. addPass(createCodeGenPreparePass());
  632. addPass(createRewriteSymbolsPass());
  633. }
  634. /// Add common passes that perform LLVM IR to IR transforms in preparation for
  635. /// instruction selection.
  636. void TargetPassConfig::addISelPrepare() {
  637. addPreISel();
  638. // Force codegen to run according to the callgraph.
  639. if (requiresCodeGenSCCOrder())
  640. addPass(new DummyCGSCCPass);
  641. // Add both the safe stack and the stack protection passes: each of them will
  642. // only protect functions that have corresponding attributes.
  643. addPass(createSafeStackPass());
  644. addPass(createStackProtectorPass());
  645. if (PrintISelInput)
  646. addPass(createPrintFunctionPass(
  647. dbgs(), "\n\n*** Final LLVM Code input to ISel ***\n"));
  648. // All passes which modify the LLVM IR are now complete; run the verifier
  649. // to ensure that the IR is valid.
  650. if (!DisableVerify)
  651. addPass(createVerifierPass());
  652. }
  653. bool TargetPassConfig::addCoreISelPasses() {
  654. // Enable FastISel with -fast-isel, but allow that to be overridden.
  655. TM->setO0WantsFastISel(EnableFastISelOption != cl::BOU_FALSE);
  656. // Determine an instruction selector.
  657. enum class SelectorType { SelectionDAG, FastISel, GlobalISel };
  658. SelectorType Selector;
  659. if (EnableFastISelOption == cl::BOU_TRUE)
  660. Selector = SelectorType::FastISel;
  661. else if (EnableGlobalISelOption == cl::BOU_TRUE ||
  662. (TM->Options.EnableGlobalISel &&
  663. EnableGlobalISelOption != cl::BOU_FALSE))
  664. Selector = SelectorType::GlobalISel;
  665. else if (TM->getOptLevel() == CodeGenOpt::None && TM->getO0WantsFastISel())
  666. Selector = SelectorType::FastISel;
  667. else
  668. Selector = SelectorType::SelectionDAG;
  669. // Set consistently TM->Options.EnableFastISel and EnableGlobalISel.
  670. if (Selector == SelectorType::FastISel) {
  671. TM->setFastISel(true);
  672. TM->setGlobalISel(false);
  673. } else if (Selector == SelectorType::GlobalISel) {
  674. TM->setFastISel(false);
  675. TM->setGlobalISel(true);
  676. }
  677. // Add instruction selector passes.
  678. if (Selector == SelectorType::GlobalISel) {
  679. SaveAndRestore<bool> SavedAddingMachinePasses(AddingMachinePasses, true);
  680. if (addIRTranslator())
  681. return true;
  682. addPreLegalizeMachineIR();
  683. if (addLegalizeMachineIR())
  684. return true;
  685. // Before running the register bank selector, ask the target if it
  686. // wants to run some passes.
  687. addPreRegBankSelect();
  688. if (addRegBankSelect())
  689. return true;
  690. addPreGlobalInstructionSelect();
  691. if (addGlobalInstructionSelect())
  692. return true;
  693. // Pass to reset the MachineFunction if the ISel failed.
  694. addPass(createResetMachineFunctionPass(
  695. reportDiagnosticWhenGlobalISelFallback(), isGlobalISelAbortEnabled()));
  696. // Provide a fallback path when we do not want to abort on
  697. // not-yet-supported input.
  698. if (!isGlobalISelAbortEnabled() && addInstSelector())
  699. return true;
  700. } else if (addInstSelector())
  701. return true;
  702. // Expand pseudo-instructions emitted by ISel. Don't run the verifier before
  703. // FinalizeISel.
  704. addPass(&FinalizeISelID);
  705. // Print the instruction selected machine code...
  706. printAndVerify("After Instruction Selection");
  707. return false;
  708. }
  709. bool TargetPassConfig::addISelPasses() {
  710. if (TM->useEmulatedTLS())
  711. addPass(createLowerEmuTLSPass());
  712. addPass(createPreISelIntrinsicLoweringPass());
  713. addPass(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
  714. addIRPasses();
  715. addCodeGenPrepare();
  716. addPassesToHandleExceptions();
  717. addISelPrepare();
  718. return addCoreISelPasses();
  719. }
  720. /// -regalloc=... command line option.
  721. static FunctionPass *useDefaultRegisterAllocator() { return nullptr; }
  722. static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
  723. RegisterPassParser<RegisterRegAlloc>>
  724. RegAlloc("regalloc", cl::Hidden, cl::init(&useDefaultRegisterAllocator),
  725. cl::desc("Register allocator to use"));
  726. /// Add the complete set of target-independent postISel code generator passes.
  727. ///
  728. /// This can be read as the standard order of major LLVM CodeGen stages. Stages
  729. /// with nontrivial configuration or multiple passes are broken out below in
  730. /// add%Stage routines.
  731. ///
  732. /// Any TargetPassConfig::addXX routine may be overriden by the Target. The
  733. /// addPre/Post methods with empty header implementations allow injecting
  734. /// target-specific fixups just before or after major stages. Additionally,
  735. /// targets have the flexibility to change pass order within a stage by
  736. /// overriding default implementation of add%Stage routines below. Each
  737. /// technique has maintainability tradeoffs because alternate pass orders are
  738. /// not well supported. addPre/Post works better if the target pass is easily
  739. /// tied to a common pass. But if it has subtle dependencies on multiple passes,
  740. /// the target should override the stage instead.
  741. ///
  742. /// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
  743. /// before/after any target-independent pass. But it's currently overkill.
  744. void TargetPassConfig::addMachinePasses() {
  745. AddingMachinePasses = true;
  746. // Insert a machine instr printer pass after the specified pass.
  747. StringRef PrintMachineInstrsPassName = PrintMachineInstrs.getValue();
  748. if (!PrintMachineInstrsPassName.equals("") &&
  749. !PrintMachineInstrsPassName.equals("option-unspecified")) {
  750. if (const PassInfo *TPI = getPassInfo(PrintMachineInstrsPassName)) {
  751. const PassRegistry *PR = PassRegistry::getPassRegistry();
  752. const PassInfo *IPI = PR->getPassInfo(StringRef("machineinstr-printer"));
  753. assert(IPI && "failed to get \"machineinstr-printer\" PassInfo!");
  754. const char *TID = (const char *)(TPI->getTypeInfo());
  755. const char *IID = (const char *)(IPI->getTypeInfo());
  756. insertPass(TID, IID);
  757. }
  758. }
  759. // Add passes that optimize machine instructions in SSA form.
  760. if (getOptLevel() != CodeGenOpt::None) {
  761. addMachineSSAOptimization();
  762. } else {
  763. // If the target requests it, assign local variables to stack slots relative
  764. // to one another and simplify frame index references where possible.
  765. addPass(&LocalStackSlotAllocationID, false);
  766. }
  767. if (TM->Options.EnableIPRA)
  768. addPass(createRegUsageInfoPropPass());
  769. // Run pre-ra passes.
  770. addPreRegAlloc();
  771. // Run register allocation and passes that are tightly coupled with it,
  772. // including phi elimination and scheduling.
  773. if (getOptimizeRegAlloc())
  774. addOptimizedRegAlloc();
  775. else
  776. addFastRegAlloc();
  777. // Run post-ra passes.
  778. addPostRegAlloc();
  779. // Insert prolog/epilog code. Eliminate abstract frame index references...
  780. if (getOptLevel() != CodeGenOpt::None) {
  781. addPass(&PostRAMachineSinkingID);
  782. addPass(&ShrinkWrapID);
  783. }
  784. // Prolog/Epilog inserter needs a TargetMachine to instantiate. But only
  785. // do so if it hasn't been disabled, substituted, or overridden.
  786. if (!isPassSubstitutedOrOverridden(&PrologEpilogCodeInserterID))
  787. addPass(createPrologEpilogInserterPass());
  788. /// Add passes that optimize machine instructions after register allocation.
  789. if (getOptLevel() != CodeGenOpt::None)
  790. addMachineLateOptimization();
  791. // Expand pseudo instructions before second scheduling pass.
  792. addPass(&ExpandPostRAPseudosID);
  793. // Run pre-sched2 passes.
  794. addPreSched2();
  795. if (EnableImplicitNullChecks)
  796. addPass(&ImplicitNullChecksID);
  797. // Second pass scheduler.
  798. // Let Target optionally insert this pass by itself at some other
  799. // point.
  800. if (getOptLevel() != CodeGenOpt::None &&
  801. !TM->targetSchedulesPostRAScheduling()) {
  802. if (MISchedPostRA)
  803. addPass(&PostMachineSchedulerID);
  804. else
  805. addPass(&PostRASchedulerID);
  806. }
  807. // GC
  808. if (addGCPasses()) {
  809. if (PrintGCInfo)
  810. addPass(createGCInfoPrinter(dbgs()), false, false);
  811. }
  812. // Basic block placement.
  813. if (getOptLevel() != CodeGenOpt::None)
  814. addBlockPlacement();
  815. addPreEmitPass();
  816. if (TM->Options.EnableIPRA)
  817. // Collect register usage information and produce a register mask of
  818. // clobbered registers, to be used to optimize call sites.
  819. addPass(createRegUsageInfoCollector());
  820. addPass(&FuncletLayoutID, false);
  821. addPass(&StackMapLivenessID, false);
  822. addPass(&LiveDebugValuesID, false);
  823. // Insert before XRay Instrumentation.
  824. addPass(&FEntryInserterID, false);
  825. addPass(&XRayInstrumentationID, false);
  826. addPass(&PatchableFunctionID, false);
  827. if (TM->Options.EnableMachineOutliner && getOptLevel() != CodeGenOpt::None &&
  828. EnableMachineOutliner != NeverOutline) {
  829. bool RunOnAllFunctions = (EnableMachineOutliner == AlwaysOutline);
  830. bool AddOutliner = RunOnAllFunctions ||
  831. TM->Options.SupportsDefaultOutlining;
  832. if (AddOutliner)
  833. addPass(createMachineOutlinerPass(RunOnAllFunctions));
  834. }
  835. // Add passes that directly emit MI after all other MI passes.
  836. addPreEmitPass2();
  837. AddingMachinePasses = false;
  838. }
  839. /// Add passes that optimize machine instructions in SSA form.
  840. void TargetPassConfig::addMachineSSAOptimization() {
  841. // Pre-ra tail duplication.
  842. addPass(&EarlyTailDuplicateID);
  843. // Optimize PHIs before DCE: removing dead PHI cycles may make more
  844. // instructions dead.
  845. addPass(&OptimizePHIsID, false);
  846. // This pass merges large allocas. StackSlotColoring is a different pass
  847. // which merges spill slots.
  848. addPass(&StackColoringID, false);
  849. // If the target requests it, assign local variables to stack slots relative
  850. // to one another and simplify frame index references where possible.
  851. addPass(&LocalStackSlotAllocationID, false);
  852. // With optimization, dead code should already be eliminated. However
  853. // there is one known exception: lowered code for arguments that are only
  854. // used by tail calls, where the tail calls reuse the incoming stack
  855. // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
  856. addPass(&DeadMachineInstructionElimID);
  857. // Allow targets to insert passes that improve instruction level parallelism,
  858. // like if-conversion. Such passes will typically need dominator trees and
  859. // loop info, just like LICM and CSE below.
  860. addILPOpts();
  861. addPass(&EarlyMachineLICMID, false);
  862. addPass(&MachineCSEID, false);
  863. addPass(&MachineSinkingID);
  864. addPass(&PeepholeOptimizerID);
  865. // Clean-up the dead code that may have been generated by peephole
  866. // rewriting.
  867. addPass(&DeadMachineInstructionElimID);
  868. }
  869. //===---------------------------------------------------------------------===//
  870. /// Register Allocation Pass Configuration
  871. //===---------------------------------------------------------------------===//
  872. bool TargetPassConfig::getOptimizeRegAlloc() const {
  873. switch (OptimizeRegAlloc) {
  874. case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
  875. case cl::BOU_TRUE: return true;
  876. case cl::BOU_FALSE: return false;
  877. }
  878. llvm_unreachable("Invalid optimize-regalloc state");
  879. }
  880. /// A dummy default pass factory indicates whether the register allocator is
  881. /// overridden on the command line.
  882. static llvm::once_flag InitializeDefaultRegisterAllocatorFlag;
  883. static RegisterRegAlloc
  884. defaultRegAlloc("default",
  885. "pick register allocator based on -O option",
  886. useDefaultRegisterAllocator);
  887. static void initializeDefaultRegisterAllocatorOnce() {
  888. if (!RegisterRegAlloc::getDefault())
  889. RegisterRegAlloc::setDefault(RegAlloc);
  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 std::make_unique<CSEConfigBase>();
  1040. }