Passes.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. //===-- Passes.cpp - Target independent code generation passes ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines interfaces to access the target independent code
  11. // generation passes provided by the LLVM backend.
  12. //
  13. //===---------------------------------------------------------------------===//
  14. #include "llvm/CodeGen/Passes.h"
  15. #include "llvm/Analysis/Passes.h"
  16. #include "llvm/CodeGen/MachineFunctionPass.h"
  17. #include "llvm/CodeGen/RegAllocRegistry.h"
  18. #include "llvm/IR/IRPrintingPasses.h"
  19. #include "llvm/IR/LegacyPassManager.h"
  20. #include "llvm/IR/Verifier.h"
  21. #include "llvm/MC/MCAsmInfo.h"
  22. #include "llvm/Support/CommandLine.h"
  23. #include "llvm/Support/Debug.h"
  24. #include "llvm/Support/ErrorHandling.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include "llvm/Transforms/Instrumentation.h"
  27. #include "llvm/Transforms/Scalar.h"
  28. #include "llvm/Transforms/Utils/SymbolRewriter.h"
  29. using namespace llvm;
  30. static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden,
  31. cl::desc("Disable Post Regalloc"));
  32. static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden,
  33. cl::desc("Disable branch folding"));
  34. static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
  35. cl::desc("Disable tail duplication"));
  36. static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
  37. cl::desc("Disable pre-register allocation tail duplication"));
  38. static cl::opt<bool> DisableBlockPlacement("disable-block-placement",
  39. cl::Hidden, cl::desc("Disable probability-driven block placement"));
  40. static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats",
  41. cl::Hidden, cl::desc("Collect probability-driven block placement stats"));
  42. static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
  43. cl::desc("Disable Stack Slot Coloring"));
  44. static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden,
  45. cl::desc("Disable Machine Dead Code Elimination"));
  46. static cl::opt<bool> DisableEarlyIfConversion("disable-early-ifcvt", cl::Hidden,
  47. cl::desc("Disable Early If-conversion"));
  48. static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
  49. cl::desc("Disable Machine LICM"));
  50. static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden,
  51. cl::desc("Disable Machine Common Subexpression Elimination"));
  52. static cl::opt<cl::boolOrDefault>
  53. EnableShrinkWrapOpt("enable-shrink-wrap", cl::Hidden,
  54. cl::desc("enable the shrink-wrapping pass"));
  55. static cl::opt<cl::boolOrDefault> OptimizeRegAlloc(
  56. "optimize-regalloc", cl::Hidden,
  57. cl::desc("Enable optimized register allocation compilation path."));
  58. static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
  59. cl::Hidden,
  60. cl::desc("Disable Machine LICM"));
  61. static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
  62. cl::desc("Disable Machine Sinking"));
  63. static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
  64. cl::desc("Disable Loop Strength Reduction Pass"));
  65. static cl::opt<bool> DisableConstantHoisting("disable-constant-hoisting",
  66. cl::Hidden, cl::desc("Disable ConstantHoisting"));
  67. static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
  68. cl::desc("Disable Codegen Prepare"));
  69. static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden,
  70. cl::desc("Disable Copy Propagation pass"));
  71. static cl::opt<bool> DisablePartialLibcallInlining("disable-partial-libcall-inlining",
  72. cl::Hidden, cl::desc("Disable Partial Libcall Inlining"));
  73. static cl::opt<bool> EnableImplicitNullChecks(
  74. "enable-implicit-null-checks",
  75. cl::desc("Fold null checks into faulting memory operations"),
  76. cl::init(false));
  77. static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
  78. cl::desc("Print LLVM IR produced by the loop-reduce pass"));
  79. static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
  80. cl::desc("Print LLVM IR input to isel pass"));
  81. static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
  82. cl::desc("Dump garbage collector data"));
  83. static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
  84. cl::desc("Verify generated machine code"),
  85. cl::init(false),
  86. cl::ZeroOrMore);
  87. static cl::opt<std::string>
  88. PrintMachineInstrs("print-machineinstrs", cl::ValueOptional,
  89. cl::desc("Print machine instrs"),
  90. cl::value_desc("pass-name"), cl::init("option-unspecified"));
  91. // Temporary option to allow experimenting with MachineScheduler as a post-RA
  92. // scheduler. Targets can "properly" enable this with
  93. // substitutePass(&PostRASchedulerID, &PostMachineSchedulerID); Ideally it
  94. // wouldn't be part of the standard pass pipeline, and the target would just add
  95. // a PostRA scheduling pass wherever it wants.
  96. static cl::opt<bool> MISchedPostRA("misched-postra", cl::Hidden,
  97. cl::desc("Run MachineScheduler post regalloc (independent of preRA sched)"));
  98. // Experimental option to run live interval analysis early.
  99. static cl::opt<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden,
  100. cl::desc("Run live interval analysis earlier in the pipeline"));
  101. static cl::opt<bool> UseCFLAA("use-cfl-aa-in-codegen",
  102. cl::init(false), cl::Hidden,
  103. cl::desc("Enable the new, experimental CFL alias analysis in CodeGen"));
  104. /// Allow standard passes to be disabled by command line options. This supports
  105. /// simple binary flags that either suppress the pass or do nothing.
  106. /// i.e. -disable-mypass=false has no effect.
  107. /// These should be converted to boolOrDefault in order to use applyOverride.
  108. static IdentifyingPassPtr applyDisable(IdentifyingPassPtr PassID,
  109. bool Override) {
  110. if (Override)
  111. return IdentifyingPassPtr();
  112. return PassID;
  113. }
  114. /// Allow standard passes to be disabled by the command line, regardless of who
  115. /// is adding the pass.
  116. ///
  117. /// StandardID is the pass identified in the standard pass pipeline and provided
  118. /// to addPass(). It may be a target-specific ID in the case that the target
  119. /// directly adds its own pass, but in that case we harmlessly fall through.
  120. ///
  121. /// TargetID is the pass that the target has configured to override StandardID.
  122. ///
  123. /// StandardID may be a pseudo ID. In that case TargetID is the name of the real
  124. /// pass to run. This allows multiple options to control a single pass depending
  125. /// on where in the pipeline that pass is added.
  126. static IdentifyingPassPtr overridePass(AnalysisID StandardID,
  127. IdentifyingPassPtr TargetID) {
  128. if (StandardID == &PostRASchedulerID)
  129. return applyDisable(TargetID, DisablePostRA);
  130. if (StandardID == &BranchFolderPassID)
  131. return applyDisable(TargetID, DisableBranchFold);
  132. if (StandardID == &TailDuplicateID)
  133. return applyDisable(TargetID, DisableTailDuplicate);
  134. if (StandardID == &TargetPassConfig::EarlyTailDuplicateID)
  135. return applyDisable(TargetID, DisableEarlyTailDup);
  136. if (StandardID == &MachineBlockPlacementID)
  137. return applyDisable(TargetID, DisableBlockPlacement);
  138. if (StandardID == &StackSlotColoringID)
  139. return applyDisable(TargetID, DisableSSC);
  140. if (StandardID == &DeadMachineInstructionElimID)
  141. return applyDisable(TargetID, DisableMachineDCE);
  142. if (StandardID == &EarlyIfConverterID)
  143. return applyDisable(TargetID, DisableEarlyIfConversion);
  144. if (StandardID == &MachineLICMID)
  145. return applyDisable(TargetID, DisableMachineLICM);
  146. if (StandardID == &MachineCSEID)
  147. return applyDisable(TargetID, DisableMachineCSE);
  148. if (StandardID == &TargetPassConfig::PostRAMachineLICMID)
  149. return applyDisable(TargetID, DisablePostRAMachineLICM);
  150. if (StandardID == &MachineSinkingID)
  151. return applyDisable(TargetID, DisableMachineSink);
  152. if (StandardID == &MachineCopyPropagationID)
  153. return applyDisable(TargetID, DisableCopyProp);
  154. return TargetID;
  155. }
  156. //===---------------------------------------------------------------------===//
  157. /// TargetPassConfig
  158. //===---------------------------------------------------------------------===//
  159. INITIALIZE_PASS(TargetPassConfig, "targetpassconfig",
  160. "Target Pass Configuration", false, false)
  161. char TargetPassConfig::ID = 0;
  162. // Pseudo Pass IDs.
  163. char TargetPassConfig::EarlyTailDuplicateID = 0;
  164. char TargetPassConfig::PostRAMachineLICMID = 0;
  165. namespace llvm {
  166. class PassConfigImpl {
  167. public:
  168. // List of passes explicitly substituted by this target. Normally this is
  169. // empty, but it is a convenient way to suppress or replace specific passes
  170. // that are part of a standard pass pipeline without overridding the entire
  171. // pipeline. This mechanism allows target options to inherit a standard pass's
  172. // user interface. For example, a target may disable a standard pass by
  173. // default by substituting a pass ID of zero, and the user may still enable
  174. // that standard pass with an explicit command line option.
  175. DenseMap<AnalysisID,IdentifyingPassPtr> TargetPasses;
  176. /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass
  177. /// is inserted after each instance of the first one.
  178. SmallVector<std::pair<AnalysisID, IdentifyingPassPtr>, 4> InsertedPasses;
  179. };
  180. } // namespace llvm
  181. // Out of line virtual method.
  182. TargetPassConfig::~TargetPassConfig() {
  183. delete Impl;
  184. }
  185. // Out of line constructor provides default values for pass options and
  186. // registers all common codegen passes.
  187. TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm)
  188. : ImmutablePass(ID), PM(&pm), StartBefore(nullptr), StartAfter(nullptr),
  189. StopAfter(nullptr), Started(true), Stopped(false),
  190. AddingMachinePasses(false), TM(tm), Impl(nullptr), Initialized(false),
  191. DisableVerify(false), EnableTailMerge(true), EnableShrinkWrap(false) {
  192. Impl = new PassConfigImpl();
  193. // Register all target independent codegen passes to activate their PassIDs,
  194. // including this pass itself.
  195. initializeCodeGen(*PassRegistry::getPassRegistry());
  196. // Substitute Pseudo Pass IDs for real ones.
  197. substitutePass(&EarlyTailDuplicateID, &TailDuplicateID);
  198. substitutePass(&PostRAMachineLICMID, &MachineLICMID);
  199. }
  200. /// Insert InsertedPassID pass after TargetPassID.
  201. void TargetPassConfig::insertPass(AnalysisID TargetPassID,
  202. IdentifyingPassPtr InsertedPassID) {
  203. assert(((!InsertedPassID.isInstance() &&
  204. TargetPassID != InsertedPassID.getID()) ||
  205. (InsertedPassID.isInstance() &&
  206. TargetPassID != InsertedPassID.getInstance()->getPassID())) &&
  207. "Insert a pass after itself!");
  208. std::pair<AnalysisID, IdentifyingPassPtr> P(TargetPassID, InsertedPassID);
  209. Impl->InsertedPasses.push_back(P);
  210. }
  211. /// createPassConfig - Create a pass configuration object to be used by
  212. /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
  213. ///
  214. /// Targets may override this to extend TargetPassConfig.
  215. TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
  216. return new TargetPassConfig(this, PM);
  217. }
  218. TargetPassConfig::TargetPassConfig()
  219. : ImmutablePass(ID), PM(nullptr) {
  220. llvm_unreachable("TargetPassConfig should not be constructed on-the-fly");
  221. }
  222. // Helper to verify the analysis is really immutable.
  223. void TargetPassConfig::setOpt(bool &Opt, bool Val) {
  224. assert(!Initialized && "PassConfig is immutable");
  225. Opt = Val;
  226. }
  227. void TargetPassConfig::substitutePass(AnalysisID StandardID,
  228. IdentifyingPassPtr TargetID) {
  229. Impl->TargetPasses[StandardID] = TargetID;
  230. }
  231. IdentifyingPassPtr TargetPassConfig::getPassSubstitution(AnalysisID ID) const {
  232. DenseMap<AnalysisID, IdentifyingPassPtr>::const_iterator
  233. I = Impl->TargetPasses.find(ID);
  234. if (I == Impl->TargetPasses.end())
  235. return ID;
  236. return I->second;
  237. }
  238. /// Add a pass to the PassManager if that pass is supposed to be run. If the
  239. /// Started/Stopped flags indicate either that the compilation should start at
  240. /// a later pass or that it should stop after an earlier pass, then do not add
  241. /// the pass. Finally, compare the current pass against the StartAfter
  242. /// and StopAfter options and change the Started/Stopped flags accordingly.
  243. void TargetPassConfig::addPass(Pass *P, bool verifyAfter, bool printAfter) {
  244. assert(!Initialized && "PassConfig is immutable");
  245. // Cache the Pass ID here in case the pass manager finds this pass is
  246. // redundant with ones already scheduled / available, and deletes it.
  247. // Fundamentally, once we add the pass to the manager, we no longer own it
  248. // and shouldn't reference it.
  249. AnalysisID PassID = P->getPassID();
  250. if (StartBefore == PassID)
  251. Started = true;
  252. if (Started && !Stopped) {
  253. std::string Banner;
  254. // Construct banner message before PM->add() as that may delete the pass.
  255. if (AddingMachinePasses && (printAfter || verifyAfter))
  256. Banner = std::string("After ") + std::string(P->getPassName());
  257. PM->add(P);
  258. if (AddingMachinePasses) {
  259. if (printAfter)
  260. addPrintPass(Banner);
  261. if (verifyAfter)
  262. addVerifyPass(Banner);
  263. }
  264. // Add the passes after the pass P if there is any.
  265. for (SmallVectorImpl<std::pair<AnalysisID, IdentifyingPassPtr> >::iterator
  266. I = Impl->InsertedPasses.begin(),
  267. E = Impl->InsertedPasses.end();
  268. I != E; ++I) {
  269. if ((*I).first == PassID) {
  270. assert((*I).second.isValid() && "Illegal Pass ID!");
  271. Pass *NP;
  272. if ((*I).second.isInstance())
  273. NP = (*I).second.getInstance();
  274. else {
  275. NP = Pass::createPass((*I).second.getID());
  276. assert(NP && "Pass ID not registered");
  277. }
  278. addPass(NP, false, false);
  279. }
  280. }
  281. } else {
  282. delete P;
  283. }
  284. if (StopAfter == PassID)
  285. Stopped = true;
  286. if (StartAfter == PassID)
  287. Started = true;
  288. if (Stopped && !Started)
  289. report_fatal_error("Cannot stop compilation after pass that is not run");
  290. }
  291. /// Add a CodeGen pass at this point in the pipeline after checking for target
  292. /// and command line overrides.
  293. ///
  294. /// addPass cannot return a pointer to the pass instance because is internal the
  295. /// PassManager and the instance we create here may already be freed.
  296. AnalysisID TargetPassConfig::addPass(AnalysisID PassID, bool verifyAfter,
  297. bool printAfter) {
  298. IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
  299. IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID);
  300. if (!FinalPtr.isValid())
  301. return nullptr;
  302. Pass *P;
  303. if (FinalPtr.isInstance())
  304. P = FinalPtr.getInstance();
  305. else {
  306. P = Pass::createPass(FinalPtr.getID());
  307. if (!P)
  308. llvm_unreachable("Pass ID not registered");
  309. }
  310. AnalysisID FinalID = P->getPassID();
  311. addPass(P, verifyAfter, printAfter); // Ends the lifetime of P.
  312. return FinalID;
  313. }
  314. void TargetPassConfig::printAndVerify(const std::string &Banner) {
  315. addPrintPass(Banner);
  316. addVerifyPass(Banner);
  317. }
  318. void TargetPassConfig::addPrintPass(const std::string &Banner) {
  319. if (TM->shouldPrintMachineCode())
  320. PM->add(createMachineFunctionPrinterPass(dbgs(), Banner));
  321. }
  322. void TargetPassConfig::addVerifyPass(const std::string &Banner) {
  323. if (VerifyMachineCode)
  324. PM->add(createMachineVerifierPass(Banner));
  325. }
  326. /// Add common target configurable passes that perform LLVM IR to IR transforms
  327. /// following machine independent optimization.
  328. void TargetPassConfig::addIRPasses() {
  329. // Basic AliasAnalysis support.
  330. // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
  331. // BasicAliasAnalysis wins if they disagree. This is intended to help
  332. // support "obvious" type-punning idioms.
  333. if (UseCFLAA)
  334. addPass(createCFLAliasAnalysisPass());
  335. addPass(createTypeBasedAliasAnalysisPass());
  336. addPass(createScopedNoAliasAAPass());
  337. addPass(createBasicAliasAnalysisPass());
  338. // Before running any passes, run the verifier to determine if the input
  339. // coming from the front-end and/or optimizer is valid.
  340. if (!DisableVerify)
  341. addPass(createVerifierPass());
  342. // Run loop strength reduction before anything else.
  343. if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
  344. addPass(createLoopStrengthReducePass());
  345. if (PrintLSR)
  346. addPass(createPrintFunctionPass(dbgs(), "\n\n*** Code after LSR ***\n"));
  347. }
  348. // Run GC lowering passes for builtin collectors
  349. // TODO: add a pass insertion point here
  350. addPass(createGCLoweringPass());
  351. addPass(createShadowStackGCLoweringPass());
  352. // Make sure that no unreachable blocks are instruction selected.
  353. addPass(createUnreachableBlockEliminationPass());
  354. // Prepare expensive constants for SelectionDAG.
  355. if (getOptLevel() != CodeGenOpt::None && !DisableConstantHoisting)
  356. addPass(createConstantHoistingPass());
  357. if (getOptLevel() != CodeGenOpt::None && !DisablePartialLibcallInlining)
  358. addPass(createPartiallyInlineLibCallsPass());
  359. }
  360. /// Turn exception handling constructs into something the code generators can
  361. /// handle.
  362. void TargetPassConfig::addPassesToHandleExceptions() {
  363. switch (TM->getMCAsmInfo()->getExceptionHandlingType()) {
  364. case ExceptionHandling::SjLj:
  365. // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
  366. // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
  367. // catch info can get misplaced when a selector ends up more than one block
  368. // removed from the parent invoke(s). This could happen when a landing
  369. // pad is shared by multiple invokes and is also a target of a normal
  370. // edge from elsewhere.
  371. addPass(createSjLjEHPreparePass());
  372. // FALLTHROUGH
  373. case ExceptionHandling::DwarfCFI:
  374. case ExceptionHandling::ARM:
  375. addPass(createDwarfEHPass(TM));
  376. break;
  377. case ExceptionHandling::WinEH:
  378. // We support using both GCC-style and MSVC-style exceptions on Windows, so
  379. // add both preparation passes. Each pass will only actually run if it
  380. // recognizes the personality function.
  381. addPass(createWinEHPass(TM));
  382. addPass(createDwarfEHPass(TM));
  383. break;
  384. case ExceptionHandling::None:
  385. addPass(createLowerInvokePass());
  386. // The lower invoke pass may create unreachable code. Remove it.
  387. addPass(createUnreachableBlockEliminationPass());
  388. break;
  389. }
  390. }
  391. /// Add pass to prepare the LLVM IR for code generation. This should be done
  392. /// before exception handling preparation passes.
  393. void TargetPassConfig::addCodeGenPrepare() {
  394. if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
  395. addPass(createCodeGenPreparePass(TM));
  396. addPass(createRewriteSymbolsPass());
  397. }
  398. /// Add common passes that perform LLVM IR to IR transforms in preparation for
  399. /// instruction selection.
  400. void TargetPassConfig::addISelPrepare() {
  401. addPreISel();
  402. // Add both the safe stack and the stack protection passes: each of them will
  403. // only protect functions that have corresponding attributes.
  404. addPass(createSafeStackPass());
  405. addPass(createStackProtectorPass(TM));
  406. if (PrintISelInput)
  407. addPass(createPrintFunctionPass(
  408. dbgs(), "\n\n*** Final LLVM Code input to ISel ***\n"));
  409. // All passes which modify the LLVM IR are now complete; run the verifier
  410. // to ensure that the IR is valid.
  411. if (!DisableVerify)
  412. addPass(createVerifierPass());
  413. }
  414. /// Add the complete set of target-independent postISel code generator passes.
  415. ///
  416. /// This can be read as the standard order of major LLVM CodeGen stages. Stages
  417. /// with nontrivial configuration or multiple passes are broken out below in
  418. /// add%Stage routines.
  419. ///
  420. /// Any TargetPassConfig::addXX routine may be overriden by the Target. The
  421. /// addPre/Post methods with empty header implementations allow injecting
  422. /// target-specific fixups just before or after major stages. Additionally,
  423. /// targets have the flexibility to change pass order within a stage by
  424. /// overriding default implementation of add%Stage routines below. Each
  425. /// technique has maintainability tradeoffs because alternate pass orders are
  426. /// not well supported. addPre/Post works better if the target pass is easily
  427. /// tied to a common pass. But if it has subtle dependencies on multiple passes,
  428. /// the target should override the stage instead.
  429. ///
  430. /// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
  431. /// before/after any target-independent pass. But it's currently overkill.
  432. void TargetPassConfig::addMachinePasses() {
  433. AddingMachinePasses = true;
  434. // Insert a machine instr printer pass after the specified pass.
  435. // If -print-machineinstrs specified, print machineinstrs after all passes.
  436. if (StringRef(PrintMachineInstrs.getValue()).equals(""))
  437. TM->Options.PrintMachineCode = true;
  438. else if (!StringRef(PrintMachineInstrs.getValue())
  439. .equals("option-unspecified")) {
  440. const PassRegistry *PR = PassRegistry::getPassRegistry();
  441. const PassInfo *TPI = PR->getPassInfo(PrintMachineInstrs.getValue());
  442. const PassInfo *IPI = PR->getPassInfo(StringRef("machineinstr-printer"));
  443. assert (TPI && IPI && "Pass ID not registered!");
  444. const char *TID = (const char *)(TPI->getTypeInfo());
  445. const char *IID = (const char *)(IPI->getTypeInfo());
  446. insertPass(TID, IID);
  447. }
  448. // Print the instruction selected machine code...
  449. printAndVerify("After Instruction Selection");
  450. // Expand pseudo-instructions emitted by ISel.
  451. addPass(&ExpandISelPseudosID);
  452. // Add passes that optimize machine instructions in SSA form.
  453. if (getOptLevel() != CodeGenOpt::None) {
  454. addMachineSSAOptimization();
  455. } else {
  456. // If the target requests it, assign local variables to stack slots relative
  457. // to one another and simplify frame index references where possible.
  458. addPass(&LocalStackSlotAllocationID, false);
  459. }
  460. // Run pre-ra passes.
  461. addPreRegAlloc();
  462. // Run register allocation and passes that are tightly coupled with it,
  463. // including phi elimination and scheduling.
  464. if (getOptimizeRegAlloc())
  465. addOptimizedRegAlloc(createRegAllocPass(true));
  466. else
  467. addFastRegAlloc(createRegAllocPass(false));
  468. // Run post-ra passes.
  469. addPostRegAlloc();
  470. // Insert prolog/epilog code. Eliminate abstract frame index references...
  471. if (getEnableShrinkWrap())
  472. addPass(&ShrinkWrapID);
  473. addPass(&PrologEpilogCodeInserterID);
  474. /// Add passes that optimize machine instructions after register allocation.
  475. if (getOptLevel() != CodeGenOpt::None)
  476. addMachineLateOptimization();
  477. // Expand pseudo instructions before second scheduling pass.
  478. addPass(&ExpandPostRAPseudosID);
  479. // Run pre-sched2 passes.
  480. addPreSched2();
  481. if (EnableImplicitNullChecks)
  482. addPass(&ImplicitNullChecksID);
  483. // Second pass scheduler.
  484. if (getOptLevel() != CodeGenOpt::None) {
  485. if (MISchedPostRA)
  486. addPass(&PostMachineSchedulerID);
  487. else
  488. addPass(&PostRASchedulerID);
  489. }
  490. // GC
  491. if (addGCPasses()) {
  492. if (PrintGCInfo)
  493. addPass(createGCInfoPrinter(dbgs()), false, false);
  494. }
  495. // Basic block placement.
  496. if (getOptLevel() != CodeGenOpt::None)
  497. addBlockPlacement();
  498. addPreEmitPass();
  499. addPass(&StackMapLivenessID, false);
  500. AddingMachinePasses = false;
  501. }
  502. /// Add passes that optimize machine instructions in SSA form.
  503. void TargetPassConfig::addMachineSSAOptimization() {
  504. // Pre-ra tail duplication.
  505. addPass(&EarlyTailDuplicateID);
  506. // Optimize PHIs before DCE: removing dead PHI cycles may make more
  507. // instructions dead.
  508. addPass(&OptimizePHIsID, false);
  509. // This pass merges large allocas. StackSlotColoring is a different pass
  510. // which merges spill slots.
  511. addPass(&StackColoringID, false);
  512. // If the target requests it, assign local variables to stack slots relative
  513. // to one another and simplify frame index references where possible.
  514. addPass(&LocalStackSlotAllocationID, false);
  515. // With optimization, dead code should already be eliminated. However
  516. // there is one known exception: lowered code for arguments that are only
  517. // used by tail calls, where the tail calls reuse the incoming stack
  518. // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
  519. addPass(&DeadMachineInstructionElimID);
  520. // Allow targets to insert passes that improve instruction level parallelism,
  521. // like if-conversion. Such passes will typically need dominator trees and
  522. // loop info, just like LICM and CSE below.
  523. addILPOpts();
  524. addPass(&MachineLICMID, false);
  525. addPass(&MachineCSEID, false);
  526. addPass(&MachineSinkingID);
  527. addPass(&PeepholeOptimizerID, false);
  528. // Clean-up the dead code that may have been generated by peephole
  529. // rewriting.
  530. addPass(&DeadMachineInstructionElimID);
  531. }
  532. bool TargetPassConfig::getEnableShrinkWrap() const {
  533. switch (EnableShrinkWrapOpt) {
  534. case cl::BOU_UNSET:
  535. return EnableShrinkWrap && getOptLevel() != CodeGenOpt::None;
  536. // If EnableShrinkWrap is set, it takes precedence on whatever the
  537. // target sets. The rational is that we assume we want to test
  538. // something related to shrink-wrapping.
  539. case cl::BOU_TRUE:
  540. return true;
  541. case cl::BOU_FALSE:
  542. return false;
  543. }
  544. llvm_unreachable("Invalid shrink-wrapping state");
  545. }
  546. //===---------------------------------------------------------------------===//
  547. /// Register Allocation Pass Configuration
  548. //===---------------------------------------------------------------------===//
  549. bool TargetPassConfig::getOptimizeRegAlloc() const {
  550. switch (OptimizeRegAlloc) {
  551. case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
  552. case cl::BOU_TRUE: return true;
  553. case cl::BOU_FALSE: return false;
  554. }
  555. llvm_unreachable("Invalid optimize-regalloc state");
  556. }
  557. /// RegisterRegAlloc's global Registry tracks allocator registration.
  558. MachinePassRegistry RegisterRegAlloc::Registry;
  559. /// A dummy default pass factory indicates whether the register allocator is
  560. /// overridden on the command line.
  561. static FunctionPass *useDefaultRegisterAllocator() { return nullptr; }
  562. static RegisterRegAlloc
  563. defaultRegAlloc("default",
  564. "pick register allocator based on -O option",
  565. useDefaultRegisterAllocator);
  566. /// -regalloc=... command line option.
  567. static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
  568. RegisterPassParser<RegisterRegAlloc> >
  569. RegAlloc("regalloc",
  570. cl::init(&useDefaultRegisterAllocator),
  571. cl::desc("Register allocator to use"));
  572. /// Instantiate the default register allocator pass for this target for either
  573. /// the optimized or unoptimized allocation path. This will be added to the pass
  574. /// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
  575. /// in the optimized case.
  576. ///
  577. /// A target that uses the standard regalloc pass order for fast or optimized
  578. /// allocation may still override this for per-target regalloc
  579. /// selection. But -regalloc=... always takes precedence.
  580. FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
  581. if (Optimized)
  582. return createGreedyRegisterAllocator();
  583. else
  584. return createFastRegisterAllocator();
  585. }
  586. /// Find and instantiate the register allocation pass requested by this target
  587. /// at the current optimization level. Different register allocators are
  588. /// defined as separate passes because they may require different analysis.
  589. ///
  590. /// This helper ensures that the regalloc= option is always available,
  591. /// even for targets that override the default allocator.
  592. ///
  593. /// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
  594. /// this can be folded into addPass.
  595. FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
  596. RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
  597. // Initialize the global default.
  598. if (!Ctor) {
  599. Ctor = RegAlloc;
  600. RegisterRegAlloc::setDefault(RegAlloc);
  601. }
  602. if (Ctor != useDefaultRegisterAllocator)
  603. return Ctor();
  604. // With no -regalloc= override, ask the target for a regalloc pass.
  605. return createTargetRegisterAllocator(Optimized);
  606. }
  607. /// Return true if the default global register allocator is in use and
  608. /// has not be overriden on the command line with '-regalloc=...'
  609. bool TargetPassConfig::usingDefaultRegAlloc() const {
  610. return RegAlloc.getNumOccurrences() == 0;
  611. }
  612. /// Add the minimum set of target-independent passes that are required for
  613. /// register allocation. No coalescing or scheduling.
  614. void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
  615. addPass(&PHIEliminationID, false);
  616. addPass(&TwoAddressInstructionPassID, false);
  617. addPass(RegAllocPass);
  618. }
  619. /// Add standard target-independent passes that are tightly coupled with
  620. /// optimized register allocation, including coalescing, machine instruction
  621. /// scheduling, and register allocation itself.
  622. void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
  623. addPass(&ProcessImplicitDefsID, false);
  624. // LiveVariables currently requires pure SSA form.
  625. //
  626. // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
  627. // LiveVariables can be removed completely, and LiveIntervals can be directly
  628. // computed. (We still either need to regenerate kill flags after regalloc, or
  629. // preferably fix the scavenger to not depend on them).
  630. addPass(&LiveVariablesID, false);
  631. // Edge splitting is smarter with machine loop info.
  632. addPass(&MachineLoopInfoID, false);
  633. addPass(&PHIEliminationID, false);
  634. // Eventually, we want to run LiveIntervals before PHI elimination.
  635. if (EarlyLiveIntervals)
  636. addPass(&LiveIntervalsID, false);
  637. addPass(&TwoAddressInstructionPassID, false);
  638. addPass(&RegisterCoalescerID);
  639. // PreRA instruction scheduling.
  640. addPass(&MachineSchedulerID);
  641. // Add the selected register allocation pass.
  642. addPass(RegAllocPass);
  643. // Allow targets to change the register assignments before rewriting.
  644. addPreRewrite();
  645. // Finally rewrite virtual registers.
  646. addPass(&VirtRegRewriterID);
  647. // Perform stack slot coloring and post-ra machine LICM.
  648. //
  649. // FIXME: Re-enable coloring with register when it's capable of adding
  650. // kill markers.
  651. addPass(&StackSlotColoringID);
  652. // Run post-ra machine LICM to hoist reloads / remats.
  653. //
  654. // FIXME: can this move into MachineLateOptimization?
  655. addPass(&PostRAMachineLICMID);
  656. }
  657. //===---------------------------------------------------------------------===//
  658. /// Post RegAlloc Pass Configuration
  659. //===---------------------------------------------------------------------===//
  660. /// Add passes that optimize machine instructions after register allocation.
  661. void TargetPassConfig::addMachineLateOptimization() {
  662. // Branch folding must be run after regalloc and prolog/epilog insertion.
  663. addPass(&BranchFolderPassID);
  664. // Tail duplication.
  665. // Note that duplicating tail just increases code size and degrades
  666. // performance for targets that require Structured Control Flow.
  667. // In addition it can also make CFG irreducible. Thus we disable it.
  668. if (!TM->requiresStructuredCFG())
  669. addPass(&TailDuplicateID);
  670. // Copy propagation.
  671. addPass(&MachineCopyPropagationID);
  672. }
  673. /// Add standard GC passes.
  674. bool TargetPassConfig::addGCPasses() {
  675. addPass(&GCMachineCodeAnalysisID, false);
  676. return true;
  677. }
  678. /// Add standard basic block placement passes.
  679. void TargetPassConfig::addBlockPlacement() {
  680. if (addPass(&MachineBlockPlacementID, false)) {
  681. // Run a separate pass to collect block placement statistics.
  682. if (EnableBlockPlacementStats)
  683. addPass(&MachineBlockPlacementStatsID);
  684. }
  685. }