1 //===- Construction of codegen pass pipelines ------------------*- C++ -*--===//
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 /// \file
9 ///
10 /// Interfaces for producing common pass manager configurations.
11 ///
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_PASSES_CODEGENPASSBUILDER_H
15 #define LLVM_PASSES_CODEGENPASSBUILDER_H
16
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/Analysis/BasicAliasAnalysis.h"
21 #include "llvm/Analysis/ProfileSummaryInfo.h"
22 #include "llvm/Analysis/ScopedNoAliasAA.h"
23 #include "llvm/Analysis/TargetTransformInfo.h"
24 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
25 #include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
26 #include "llvm/CodeGen/CallBrPrepare.h"
27 #include "llvm/CodeGen/CodeGenPrepare.h"
28 #include "llvm/CodeGen/DeadMachineInstructionElim.h"
29 #include "llvm/CodeGen/DwarfEHPrepare.h"
30 #include "llvm/CodeGen/ExpandMemCmp.h"
31 #include "llvm/CodeGen/ExpandReductions.h"
32 #include "llvm/CodeGen/GCMetadata.h"
33 #include "llvm/CodeGen/GlobalMerge.h"
34 #include "llvm/CodeGen/IndirectBrExpand.h"
35 #include "llvm/CodeGen/InterleavedAccess.h"
36 #include "llvm/CodeGen/InterleavedLoadCombine.h"
37 #include "llvm/CodeGen/JMCInstrumenter.h"
38 #include "llvm/CodeGen/LowerEmuTLS.h"
39 #include "llvm/CodeGen/MIRPrinter.h"
40 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
41 #include "llvm/CodeGen/MachineModuleInfo.h"
42 #include "llvm/CodeGen/MachinePassManager.h"
43 #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
44 #include "llvm/CodeGen/ReplaceWithVeclib.h"
45 #include "llvm/CodeGen/SafeStack.h"
46 #include "llvm/CodeGen/SelectOptimize.h"
47 #include "llvm/CodeGen/ShadowStackGCLowering.h"
48 #include "llvm/CodeGen/SjLjEHPrepare.h"
49 #include "llvm/CodeGen/StackProtector.h"
50 #include "llvm/CodeGen/TargetPassConfig.h"
51 #include "llvm/CodeGen/UnreachableBlockElim.h"
52 #include "llvm/CodeGen/WasmEHPrepare.h"
53 #include "llvm/CodeGen/WinEHPrepare.h"
54 #include "llvm/IR/PassManager.h"
55 #include "llvm/IR/Verifier.h"
56 #include "llvm/IRPrinter/IRPrintingPasses.h"
57 #include "llvm/MC/MCAsmInfo.h"
58 #include "llvm/MC/MCTargetOptions.h"
59 #include "llvm/Support/CodeGen.h"
60 #include "llvm/Support/Debug.h"
61 #include "llvm/Support/Error.h"
62 #include "llvm/Support/ErrorHandling.h"
63 #include "llvm/Target/CGPassBuilderOption.h"
64 #include "llvm/Target/TargetMachine.h"
65 #include "llvm/Transforms/CFGuard.h"
66 #include "llvm/Transforms/Scalar/ConstantHoisting.h"
67 #include "llvm/Transforms/Scalar/LoopPassManager.h"
68 #include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
69 #include "llvm/Transforms/Scalar/LowerConstantIntrinsics.h"
70 #include "llvm/Transforms/Scalar/MergeICmps.h"
71 #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
72 #include "llvm/Transforms/Scalar/ScalarizeMaskedMemIntrin.h"
73 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
74 #include "llvm/Transforms/Utils/LowerInvoke.h"
75 #include <cassert>
76 #include <type_traits>
77 #include <utility>
78
79 namespace llvm {
80
81 // FIXME: Dummy target independent passes definitions that have not yet been
82 // ported to new pass manager. Once they do, remove these.
83 #define DUMMY_FUNCTION_PASS(NAME, PASS_NAME) \
84 struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
85 template <typename... Ts> PASS_NAME(Ts &&...) {} \
86 PreservedAnalyses run(Function &, FunctionAnalysisManager &) { \
87 return PreservedAnalyses::all(); \
88 } \
89 };
90 #define DUMMY_MACHINE_MODULE_PASS(NAME, PASS_NAME) \
91 struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
92 template <typename... Ts> PASS_NAME(Ts &&...) {} \
93 PreservedAnalyses run(Module &, ModuleAnalysisManager &) { \
94 return PreservedAnalyses::all(); \
95 } \
96 };
97 #define DUMMY_MACHINE_FUNCTION_PASS(NAME, PASS_NAME) \
98 struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
99 template <typename... Ts> PASS_NAME(Ts &&...) {} \
100 PreservedAnalyses run(MachineFunction &, \
101 MachineFunctionAnalysisManager &) { \
102 return PreservedAnalyses::all(); \
103 } \
104 };
105 #include "llvm/Passes/MachinePassRegistry.def"
106
107 /// This class provides access to building LLVM's passes.
108 ///
109 /// Its members provide the baseline state available to passes during their
110 /// construction. The \c MachinePassRegistry.def file specifies how to construct
111 /// all of the built-in passes, and those may reference these members during
112 /// construction.
113 template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder {
114 public:
CodeGenPassBuilder(TargetMachineT & TM,const CGPassBuilderOption & Opts,PassInstrumentationCallbacks * PIC)115 explicit CodeGenPassBuilder(TargetMachineT &TM,
116 const CGPassBuilderOption &Opts,
117 PassInstrumentationCallbacks *PIC)
118 : TM(TM), Opt(Opts), PIC(PIC) {
119 // Target could set CGPassBuilderOption::MISchedPostRA to true to achieve
120 // substitutePass(&PostRASchedulerID, &PostMachineSchedulerID)
121
122 // Target should override TM.Options.EnableIPRA in their target-specific
123 // LLVMTM ctor. See TargetMachine::setGlobalISel for example.
124 if (Opt.EnableIPRA)
125 TM.Options.EnableIPRA = *Opt.EnableIPRA;
126
127 if (Opt.EnableGlobalISelAbort)
128 TM.Options.GlobalISelAbort = *Opt.EnableGlobalISelAbort;
129
130 if (!Opt.OptimizeRegAlloc)
131 Opt.OptimizeRegAlloc = getOptLevel() != CodeGenOptLevel::None;
132 }
133
134 Error buildPipeline(ModulePassManager &MPM, raw_pwrite_stream &Out,
135 raw_pwrite_stream *DwoOut,
136 CodeGenFileType FileType) const;
137
getPassInstrumentationCallbacks()138 PassInstrumentationCallbacks *getPassInstrumentationCallbacks() const {
139 return PIC;
140 }
141
142 protected:
143 template <typename PassT>
144 using is_module_pass_t = decltype(std::declval<PassT &>().run(
145 std::declval<Module &>(), std::declval<ModuleAnalysisManager &>()));
146
147 template <typename PassT>
148 using is_function_pass_t = decltype(std::declval<PassT &>().run(
149 std::declval<Function &>(), std::declval<FunctionAnalysisManager &>()));
150
151 template <typename PassT>
152 using is_machine_function_pass_t = decltype(std::declval<PassT &>().run(
153 std::declval<MachineFunction &>(),
154 std::declval<MachineFunctionAnalysisManager &>()));
155
156 // Function object to maintain state while adding codegen IR passes.
157 // TODO: add a Function -> MachineFunction adaptor and merge
158 // AddIRPass/AddMachinePass so we can have a function pipeline that runs both
159 // function passes and machine function passes.
160 class AddIRPass {
161 public:
AddIRPass(ModulePassManager & MPM,const DerivedT & PB)162 AddIRPass(ModulePassManager &MPM, const DerivedT &PB) : MPM(MPM), PB(PB) {}
~AddIRPass()163 ~AddIRPass() {
164 if (!FPM.isEmpty())
165 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
166 }
167
168 template <typename PassT>
operator()169 void operator()(PassT &&Pass, StringRef Name = PassT::name()) {
170 static_assert((is_detected<is_function_pass_t, PassT>::value ||
171 is_detected<is_module_pass_t, PassT>::value) &&
172 "Only module pass and function pass are supported.");
173
174 if (!PB.runBeforeAdding(Name))
175 return;
176
177 // Add Function Pass
178 if constexpr (is_detected<is_function_pass_t, PassT>::value) {
179 FPM.addPass(std::forward<PassT>(Pass));
180 } else {
181 // Add Module Pass
182 if (!FPM.isEmpty()) {
183 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
184 FPM = FunctionPassManager();
185 }
186
187 MPM.addPass(std::forward<PassT>(Pass));
188 }
189 }
190
191 private:
192 ModulePassManager &MPM;
193 FunctionPassManager FPM;
194 const DerivedT &PB;
195 };
196
197 // Function object to maintain state while adding codegen machine passes.
198 class AddMachinePass {
199 public:
AddMachinePass(ModulePassManager & MPM,const DerivedT & PB)200 AddMachinePass(ModulePassManager &MPM, const DerivedT &PB)
201 : MPM(MPM), PB(PB) {}
~AddMachinePass()202 ~AddMachinePass() {
203 if (!MFPM.isEmpty()) {
204 FunctionPassManager FPM;
205 FPM.addPass(
206 createFunctionToMachineFunctionPassAdaptor(std::move(MFPM)));
207 FPM.addPass(InvalidateAnalysisPass<MachineFunctionAnalysis>());
208 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
209 }
210 }
211
212 template <typename PassT>
operator()213 void operator()(PassT &&Pass, bool Force = false,
214 StringRef Name = PassT::name()) {
215 static_assert((is_detected<is_machine_function_pass_t, PassT>::value ||
216 is_detected<is_module_pass_t, PassT>::value) &&
217 "Only module pass and function pass are supported.");
218
219 if (!Force && !PB.runBeforeAdding(Name))
220 return;
221
222 // Add Function Pass
223 if constexpr (is_detected<is_machine_function_pass_t, PassT>::value) {
224 MFPM.addPass(std::forward<PassT>(Pass));
225 } else {
226 // Add Module Pass
227 if (!MFPM.isEmpty()) {
228 MPM.addPass(createModuleToFunctionPassAdaptor(
229 createFunctionToMachineFunctionPassAdaptor(std::move(MFPM))));
230 MFPM = MachineFunctionPassManager();
231 }
232
233 MPM.addPass(std::forward<PassT>(Pass));
234 }
235
236 for (auto &C : PB.AfterCallbacks)
237 C(Name, MFPM);
238 }
239
240 private:
241 ModulePassManager &MPM;
242 MachineFunctionPassManager MFPM;
243 const DerivedT &PB;
244 };
245
246 TargetMachineT &TM;
247 CGPassBuilderOption Opt;
248 PassInstrumentationCallbacks *PIC;
249
getTM()250 template <typename TMC> TMC &getTM() const { return static_cast<TMC &>(TM); }
getOptLevel()251 CodeGenOptLevel getOptLevel() const { return TM.getOptLevel(); }
252
253 /// Check whether or not GlobalISel should abort on error.
254 /// When this is disabled, GlobalISel will fall back on SDISel instead of
255 /// erroring out.
isGlobalISelAbortEnabled()256 bool isGlobalISelAbortEnabled() const {
257 return TM.Options.GlobalISelAbort == GlobalISelAbortMode::Enable;
258 }
259
260 /// Check whether or not a diagnostic should be emitted when GlobalISel
261 /// uses the fallback path. In other words, it will emit a diagnostic
262 /// when GlobalISel failed and isGlobalISelAbortEnabled is false.
reportDiagnosticWhenGlobalISelFallback()263 bool reportDiagnosticWhenGlobalISelFallback() const {
264 return TM.Options.GlobalISelAbort == GlobalISelAbortMode::DisableWithDiag;
265 }
266
267 /// addInstSelector - This method should install an instruction selector pass,
268 /// which converts from LLVM code to machine instructions.
addInstSelector(AddMachinePass &)269 Error addInstSelector(AddMachinePass &) const {
270 return make_error<StringError>("addInstSelector is not overridden",
271 inconvertibleErrorCode());
272 }
273
274 /// Target can override this to add GlobalMergePass before all IR passes.
addGlobalMergePass(AddIRPass &)275 void addGlobalMergePass(AddIRPass &) const {}
276
277 /// Add passes that optimize instruction level parallelism for out-of-order
278 /// targets. These passes are run while the machine code is still in SSA
279 /// form, so they can use MachineTraceMetrics to control their heuristics.
280 ///
281 /// All passes added here should preserve the MachineDominatorTree,
282 /// MachineLoopInfo, and MachineTraceMetrics analyses.
addILPOpts(AddMachinePass &)283 void addILPOpts(AddMachinePass &) const {}
284
285 /// This method may be implemented by targets that want to run passes
286 /// immediately before register allocation.
addPreRegAlloc(AddMachinePass &)287 void addPreRegAlloc(AddMachinePass &) const {}
288
289 /// addPreRewrite - Add passes to the optimized register allocation pipeline
290 /// after register allocation is complete, but before virtual registers are
291 /// rewritten to physical registers.
292 ///
293 /// These passes must preserve VirtRegMap and LiveIntervals, and when running
294 /// after RABasic or RAGreedy, they should take advantage of LiveRegMatrix.
295 /// When these passes run, VirtRegMap contains legal physreg assignments for
296 /// all virtual registers.
297 ///
298 /// Note if the target overloads addRegAssignAndRewriteOptimized, this may not
299 /// be honored. This is also not generally used for the fast variant,
300 /// where the allocation and rewriting are done in one pass.
addPreRewrite(AddMachinePass &)301 void addPreRewrite(AddMachinePass &) const {}
302
303 /// Add passes to be run immediately after virtual registers are rewritten
304 /// to physical registers.
addPostRewrite(AddMachinePass &)305 void addPostRewrite(AddMachinePass &) const {}
306
307 /// This method may be implemented by targets that want to run passes after
308 /// register allocation pass pipeline but before prolog-epilog insertion.
addPostRegAlloc(AddMachinePass &)309 void addPostRegAlloc(AddMachinePass &) const {}
310
311 /// This method may be implemented by targets that want to run passes after
312 /// prolog-epilog insertion and before the second instruction scheduling pass.
addPreSched2(AddMachinePass &)313 void addPreSched2(AddMachinePass &) const {}
314
315 /// This pass may be implemented by targets that want to run passes
316 /// immediately before machine code is emitted.
addPreEmitPass(AddMachinePass &)317 void addPreEmitPass(AddMachinePass &) const {}
318
319 /// Targets may add passes immediately before machine code is emitted in this
320 /// callback. This is called even later than `addPreEmitPass`.
321 // FIXME: Rename `addPreEmitPass` to something more sensible given its actual
322 // position and remove the `2` suffix here as this callback is what
323 // `addPreEmitPass` *should* be but in reality isn't.
addPreEmitPass2(AddMachinePass &)324 void addPreEmitPass2(AddMachinePass &) const {}
325
326 /// {{@ For GlobalISel
327 ///
328
329 /// addPreISel - This method should add any "last minute" LLVM->LLVM
330 /// passes (which are run just before instruction selector).
addPreISel(AddIRPass &)331 void addPreISel(AddIRPass &) const {
332 llvm_unreachable("addPreISel is not overridden");
333 }
334
335 /// This method should install an IR translator pass, which converts from
336 /// LLVM code to machine instructions with possibly generic opcodes.
addIRTranslator(AddMachinePass &)337 Error addIRTranslator(AddMachinePass &) const {
338 return make_error<StringError>("addIRTranslator is not overridden",
339 inconvertibleErrorCode());
340 }
341
342 /// This method may be implemented by targets that want to run passes
343 /// immediately before legalization.
addPreLegalizeMachineIR(AddMachinePass &)344 void addPreLegalizeMachineIR(AddMachinePass &) const {}
345
346 /// This method should install a legalize pass, which converts the instruction
347 /// sequence into one that can be selected by the target.
addLegalizeMachineIR(AddMachinePass &)348 Error addLegalizeMachineIR(AddMachinePass &) const {
349 return make_error<StringError>("addLegalizeMachineIR is not overridden",
350 inconvertibleErrorCode());
351 }
352
353 /// This method may be implemented by targets that want to run passes
354 /// immediately before the register bank selection.
addPreRegBankSelect(AddMachinePass &)355 void addPreRegBankSelect(AddMachinePass &) const {}
356
357 /// This method should install a register bank selector pass, which
358 /// assigns register banks to virtual registers without a register
359 /// class or register banks.
addRegBankSelect(AddMachinePass &)360 Error addRegBankSelect(AddMachinePass &) const {
361 return make_error<StringError>("addRegBankSelect is not overridden",
362 inconvertibleErrorCode());
363 }
364
365 /// This method may be implemented by targets that want to run passes
366 /// immediately before the (global) instruction selection.
addPreGlobalInstructionSelect(AddMachinePass &)367 void addPreGlobalInstructionSelect(AddMachinePass &) const {}
368
369 /// This method should install a (global) instruction selector pass, which
370 /// converts possibly generic instructions to fully target-specific
371 /// instructions, thereby constraining all generic virtual registers to
372 /// register classes.
addGlobalInstructionSelect(AddMachinePass &)373 Error addGlobalInstructionSelect(AddMachinePass &) const {
374 return make_error<StringError>(
375 "addGlobalInstructionSelect is not overridden",
376 inconvertibleErrorCode());
377 }
378 /// @}}
379
380 /// High level function that adds all passes necessary to go from llvm IR
381 /// representation to the MI representation.
382 /// Adds IR based lowering and target specific optimization passes and finally
383 /// the core instruction selection passes.
384 void addISelPasses(AddIRPass &) const;
385
386 /// Add the actual instruction selection passes. This does not include
387 /// preparation passes on IR.
388 Error addCoreISelPasses(AddMachinePass &) const;
389
390 /// Add the complete, standard set of LLVM CodeGen passes.
391 /// Fully developed targets will not generally override this.
392 Error addMachinePasses(AddMachinePass &) const;
393
394 /// Add passes to lower exception handling for the code generator.
395 void addPassesToHandleExceptions(AddIRPass &) const;
396
397 /// Add common target configurable passes that perform LLVM IR to IR
398 /// transforms following machine independent optimization.
399 void addIRPasses(AddIRPass &) const;
400
401 /// Add pass to prepare the LLVM IR for code generation. This should be done
402 /// before exception handling preparation passes.
403 void addCodeGenPrepare(AddIRPass &) const;
404
405 /// Add common passes that perform LLVM IR to IR transforms in preparation for
406 /// instruction selection.
407 void addISelPrepare(AddIRPass &) const;
408
409 /// Methods with trivial inline returns are convenient points in the common
410 /// codegen pass pipeline where targets may insert passes. Methods with
411 /// out-of-line standard implementations are major CodeGen stages called by
412 /// addMachinePasses. Some targets may override major stages when inserting
413 /// passes is insufficient, but maintaining overriden stages is more work.
414 ///
415
416 /// addMachineSSAOptimization - Add standard passes that optimize machine
417 /// instructions in SSA form.
418 void addMachineSSAOptimization(AddMachinePass &) const;
419
420 /// addFastRegAlloc - Add the minimum set of target-independent passes that
421 /// are required for fast register allocation.
422 Error addFastRegAlloc(AddMachinePass &) const;
423
424 /// addOptimizedRegAlloc - Add passes related to register allocation.
425 /// LLVMTargetMachine provides standard regalloc passes for most targets.
426 void addOptimizedRegAlloc(AddMachinePass &) const;
427
428 /// Add passes that optimize machine instructions after register allocation.
429 void addMachineLateOptimization(AddMachinePass &) const;
430
431 /// addGCPasses - Add late codegen passes that analyze code for garbage
432 /// collection. This should return true if GC info should be printed after
433 /// these passes.
addGCPasses(AddMachinePass &)434 void addGCPasses(AddMachinePass &) const {}
435
436 /// Add standard basic block placement passes.
437 void addBlockPlacement(AddMachinePass &) const;
438
439 using CreateMCStreamer =
440 std::function<Expected<std::unique_ptr<MCStreamer>>(MCContext &)>;
addAsmPrinter(AddMachinePass &,CreateMCStreamer)441 void addAsmPrinter(AddMachinePass &, CreateMCStreamer) const {
442 llvm_unreachable("addAsmPrinter is not overridden");
443 }
444
445 /// Utilities for targets to add passes to the pass manager.
446 ///
447
448 /// createTargetRegisterAllocator - Create the register allocator pass for
449 /// this target at the current optimization level.
450 void addTargetRegisterAllocator(AddMachinePass &, bool Optimized) const;
451
452 /// addMachinePasses helper to create the target-selected or overriden
453 /// regalloc pass.
454 void addRegAllocPass(AddMachinePass &, bool Optimized) const;
455
456 /// Add core register alloator passes which do the actual register assignment
457 /// and rewriting. \returns true if any passes were added.
458 Error addRegAssignmentFast(AddMachinePass &) const;
459 Error addRegAssignmentOptimized(AddMachinePass &) const;
460
461 /// Allow the target to disable a specific pass by default.
462 /// Backend can declare unwanted passes in constructor.
disablePass()463 template <typename... PassTs> void disablePass() {
464 BeforeCallbacks.emplace_back(
465 [](StringRef Name) { return ((Name != PassTs::name()) && ...); });
466 }
467
468 /// Insert InsertedPass pass after TargetPass pass.
469 /// Only machine function passes are supported.
470 template <typename TargetPassT, typename InsertedPassT>
insertPass(InsertedPassT && Pass)471 void insertPass(InsertedPassT &&Pass) {
472 AfterCallbacks.emplace_back(
473 [&](StringRef Name, MachineFunctionPassManager &MFPM) mutable {
474 if (Name == TargetPassT::name())
475 MFPM.addPass(std::forward<InsertedPassT>(Pass));
476 });
477 }
478
479 private:
derived()480 DerivedT &derived() { return static_cast<DerivedT &>(*this); }
derived()481 const DerivedT &derived() const {
482 return static_cast<const DerivedT &>(*this);
483 }
484
runBeforeAdding(StringRef Name)485 bool runBeforeAdding(StringRef Name) const {
486 bool ShouldAdd = true;
487 for (auto &C : BeforeCallbacks)
488 ShouldAdd &= C(Name);
489 return ShouldAdd;
490 }
491
492 void setStartStopPasses(const TargetPassConfig::StartStopInfo &Info) const;
493
494 Error verifyStartStop(const TargetPassConfig::StartStopInfo &Info) const;
495
496 mutable SmallVector<llvm::unique_function<bool(StringRef)>, 4>
497 BeforeCallbacks;
498 mutable SmallVector<
499 llvm::unique_function<void(StringRef, MachineFunctionPassManager &)>, 4>
500 AfterCallbacks;
501
502 /// Helper variable for `-start-before/-start-after/-stop-before/-stop-after`
503 mutable bool Started = true;
504 mutable bool Stopped = true;
505 };
506
507 template <typename Derived, typename TargetMachineT>
buildPipeline(ModulePassManager & MPM,raw_pwrite_stream & Out,raw_pwrite_stream * DwoOut,CodeGenFileType FileType)508 Error CodeGenPassBuilder<Derived, TargetMachineT>::buildPipeline(
509 ModulePassManager &MPM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
510 CodeGenFileType FileType) const {
511 auto StartStopInfo = TargetPassConfig::getStartStopInfo(*PIC);
512 if (!StartStopInfo)
513 return StartStopInfo.takeError();
514 setStartStopPasses(*StartStopInfo);
515
516 bool PrintAsm = TargetPassConfig::willCompleteCodeGenPipeline();
517 bool PrintMIR = !PrintAsm && FileType != CodeGenFileType::Null;
518
519 {
520 AddIRPass addIRPass(MPM, derived());
521 addIRPass(RequireAnalysisPass<MachineModuleAnalysis, Module>());
522 addIRPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
523 addIRPass(RequireAnalysisPass<CollectorMetadataAnalysis, Module>());
524 addISelPasses(addIRPass);
525 }
526
527 AddMachinePass addPass(MPM, derived());
528
529 if (PrintMIR)
530 addPass(PrintMIRPreparePass(Out), /*Force=*/true);
531
532 if (auto Err = addCoreISelPasses(addPass))
533 return std::move(Err);
534
535 if (auto Err = derived().addMachinePasses(addPass))
536 return std::move(Err);
537
538 if (PrintAsm) {
539 derived().addAsmPrinter(
540 addPass, [this, &Out, DwoOut, FileType](MCContext &Ctx) {
541 return this->TM.createMCStreamer(Out, DwoOut, FileType, Ctx);
542 });
543 }
544
545 if (PrintMIR)
546 addPass(PrintMIRPass(Out), /*Force=*/true);
547
548 return verifyStartStop(*StartStopInfo);
549 }
550
551 template <typename Derived, typename TargetMachineT>
setStartStopPasses(const TargetPassConfig::StartStopInfo & Info)552 void CodeGenPassBuilder<Derived, TargetMachineT>::setStartStopPasses(
553 const TargetPassConfig::StartStopInfo &Info) const {
554 if (!Info.StartPass.empty()) {
555 Started = false;
556 BeforeCallbacks.emplace_back([this, &Info, AfterFlag = Info.StartAfter,
557 Count = 0u](StringRef ClassName) mutable {
558 if (Count == Info.StartInstanceNum) {
559 if (AfterFlag) {
560 AfterFlag = false;
561 Started = true;
562 }
563 return Started;
564 }
565
566 auto PassName = PIC->getPassNameForClassName(ClassName);
567 if (Info.StartPass == PassName && ++Count == Info.StartInstanceNum)
568 Started = !Info.StartAfter;
569
570 return Started;
571 });
572 }
573
574 if (!Info.StopPass.empty()) {
575 Stopped = false;
576 BeforeCallbacks.emplace_back([this, &Info, AfterFlag = Info.StopAfter,
577 Count = 0u](StringRef ClassName) mutable {
578 if (Count == Info.StopInstanceNum) {
579 if (AfterFlag) {
580 AfterFlag = false;
581 Stopped = true;
582 }
583 return !Stopped;
584 }
585
586 auto PassName = PIC->getPassNameForClassName(ClassName);
587 if (Info.StopPass == PassName && ++Count == Info.StopInstanceNum)
588 Stopped = !Info.StopAfter;
589 return !Stopped;
590 });
591 }
592 }
593
594 template <typename Derived, typename TargetMachineT>
verifyStartStop(const TargetPassConfig::StartStopInfo & Info)595 Error CodeGenPassBuilder<Derived, TargetMachineT>::verifyStartStop(
596 const TargetPassConfig::StartStopInfo &Info) const {
597 if (Started && Stopped)
598 return Error::success();
599
600 if (!Started)
601 return make_error<StringError>(
602 "Can't find start pass \"" +
603 PIC->getPassNameForClassName(Info.StartPass) + "\".",
604 std::make_error_code(std::errc::invalid_argument));
605 if (!Stopped)
606 return make_error<StringError>(
607 "Can't find stop pass \"" +
608 PIC->getPassNameForClassName(Info.StopPass) + "\".",
609 std::make_error_code(std::errc::invalid_argument));
610 return Error::success();
611 }
612
613 template <typename Derived, typename TargetMachineT>
addISelPasses(AddIRPass & addPass)614 void CodeGenPassBuilder<Derived, TargetMachineT>::addISelPasses(
615 AddIRPass &addPass) const {
616 derived().addGlobalMergePass(addPass);
617 if (TM.useEmulatedTLS())
618 addPass(LowerEmuTLSPass());
619
620 addPass(PreISelIntrinsicLoweringPass(TM));
621
622 derived().addIRPasses(addPass);
623 derived().addCodeGenPrepare(addPass);
624 addPassesToHandleExceptions(addPass);
625 derived().addISelPrepare(addPass);
626 }
627
628 /// Add common target configurable passes that perform LLVM IR to IR transforms
629 /// following machine independent optimization.
630 template <typename Derived, typename TargetMachineT>
addIRPasses(AddIRPass & addPass)631 void CodeGenPassBuilder<Derived, TargetMachineT>::addIRPasses(
632 AddIRPass &addPass) const {
633 // Before running any passes, run the verifier to determine if the input
634 // coming from the front-end and/or optimizer is valid.
635 if (!Opt.DisableVerify)
636 addPass(VerifierPass());
637
638 // Run loop strength reduction before anything else.
639 if (getOptLevel() != CodeGenOptLevel::None && !Opt.DisableLSR) {
640 addPass(createFunctionToLoopPassAdaptor(LoopStrengthReducePass(),
641 /*UseMemorySSA=*/true));
642 // FIXME: use -stop-after so we could remove PrintLSR
643 if (Opt.PrintLSR)
644 addPass(PrintFunctionPass(dbgs(), "\n\n*** Code after LSR ***\n"));
645 }
646
647 if (getOptLevel() != CodeGenOptLevel::None) {
648 // The MergeICmpsPass tries to create memcmp calls by grouping sequences of
649 // loads and compares. ExpandMemCmpPass then tries to expand those calls
650 // into optimally-sized loads and compares. The transforms are enabled by a
651 // target lowering hook.
652 if (!Opt.DisableMergeICmps)
653 addPass(MergeICmpsPass());
654 addPass(ExpandMemCmpPass(&TM));
655 }
656
657 // Run GC lowering passes for builtin collectors
658 // TODO: add a pass insertion point here
659 addPass(GCLoweringPass());
660 addPass(ShadowStackGCLoweringPass());
661 addPass(LowerConstantIntrinsicsPass());
662
663 // Make sure that no unreachable blocks are instruction selected.
664 addPass(UnreachableBlockElimPass());
665
666 // Prepare expensive constants for SelectionDAG.
667 if (getOptLevel() != CodeGenOptLevel::None && !Opt.DisableConstantHoisting)
668 addPass(ConstantHoistingPass());
669
670 // Replace calls to LLVM intrinsics (e.g., exp, log) operating on vector
671 // operands with calls to the corresponding functions in a vector library.
672 if (getOptLevel() != CodeGenOptLevel::None)
673 addPass(ReplaceWithVeclib());
674
675 if (getOptLevel() != CodeGenOptLevel::None &&
676 !Opt.DisablePartialLibcallInlining)
677 addPass(PartiallyInlineLibCallsPass());
678
679 // Instrument function entry and exit, e.g. with calls to mcount().
680 addPass(EntryExitInstrumenterPass(/*PostInlining=*/true));
681
682 // Add scalarization of target's unsupported masked memory intrinsics pass.
683 // the unsupported intrinsic will be replaced with a chain of basic blocks,
684 // that stores/loads element one-by-one if the appropriate mask bit is set.
685 addPass(ScalarizeMaskedMemIntrinPass());
686
687 // Expand reduction intrinsics into shuffle sequences if the target wants to.
688 addPass(ExpandReductionsPass());
689
690 // Convert conditional moves to conditional jumps when profitable.
691 if (getOptLevel() != CodeGenOptLevel::None && !Opt.DisableSelectOptimize)
692 addPass(SelectOptimizePass(&TM));
693 }
694
695 /// Turn exception handling constructs into something the code generators can
696 /// handle.
697 template <typename Derived, typename TargetMachineT>
addPassesToHandleExceptions(AddIRPass & addPass)698 void CodeGenPassBuilder<Derived, TargetMachineT>::addPassesToHandleExceptions(
699 AddIRPass &addPass) const {
700 const MCAsmInfo *MCAI = TM.getMCAsmInfo();
701 assert(MCAI && "No MCAsmInfo");
702 switch (MCAI->getExceptionHandlingType()) {
703 case ExceptionHandling::SjLj:
704 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
705 // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
706 // catch info can get misplaced when a selector ends up more than one block
707 // removed from the parent invoke(s). This could happen when a landing
708 // pad is shared by multiple invokes and is also a target of a normal
709 // edge from elsewhere.
710 addPass(SjLjEHPreparePass(&TM));
711 [[fallthrough]];
712 case ExceptionHandling::DwarfCFI:
713 case ExceptionHandling::ARM:
714 case ExceptionHandling::AIX:
715 case ExceptionHandling::ZOS:
716 addPass(DwarfEHPreparePass(&TM));
717 break;
718 case ExceptionHandling::WinEH:
719 // We support using both GCC-style and MSVC-style exceptions on Windows, so
720 // add both preparation passes. Each pass will only actually run if it
721 // recognizes the personality function.
722 addPass(WinEHPreparePass());
723 addPass(DwarfEHPreparePass(&TM));
724 break;
725 case ExceptionHandling::Wasm:
726 // Wasm EH uses Windows EH instructions, but it does not need to demote PHIs
727 // on catchpads and cleanuppads because it does not outline them into
728 // funclets. Catchswitch blocks are not lowered in SelectionDAG, so we
729 // should remove PHIs there.
730 addPass(WinEHPreparePass(/*DemoteCatchSwitchPHIOnly=*/false));
731 addPass(WasmEHPreparePass());
732 break;
733 case ExceptionHandling::None:
734 addPass(LowerInvokePass());
735
736 // The lower invoke pass may create unreachable code. Remove it.
737 addPass(UnreachableBlockElimPass());
738 break;
739 }
740 }
741
742 /// Add pass to prepare the LLVM IR for code generation. This should be done
743 /// before exception handling preparation passes.
744 template <typename Derived, typename TargetMachineT>
addCodeGenPrepare(AddIRPass & addPass)745 void CodeGenPassBuilder<Derived, TargetMachineT>::addCodeGenPrepare(
746 AddIRPass &addPass) const {
747 if (getOptLevel() != CodeGenOptLevel::None && !Opt.DisableCGP)
748 addPass(CodeGenPreparePass(&TM));
749 // TODO: Default ctor'd RewriteSymbolPass is no-op.
750 // addPass(RewriteSymbolPass());
751 }
752
753 /// Add common passes that perform LLVM IR to IR transforms in preparation for
754 /// instruction selection.
755 template <typename Derived, typename TargetMachineT>
addISelPrepare(AddIRPass & addPass)756 void CodeGenPassBuilder<Derived, TargetMachineT>::addISelPrepare(
757 AddIRPass &addPass) const {
758 derived().addPreISel(addPass);
759
760 addPass(CallBrPreparePass());
761 // Add both the safe stack and the stack protection passes: each of them will
762 // only protect functions that have corresponding attributes.
763 addPass(SafeStackPass(&TM));
764 addPass(StackProtectorPass(&TM));
765
766 if (Opt.PrintISelInput)
767 addPass(PrintFunctionPass(dbgs(),
768 "\n\n*** Final LLVM Code input to ISel ***\n"));
769
770 // All passes which modify the LLVM IR are now complete; run the verifier
771 // to ensure that the IR is valid.
772 if (!Opt.DisableVerify)
773 addPass(VerifierPass());
774 }
775
776 template <typename Derived, typename TargetMachineT>
addCoreISelPasses(AddMachinePass & addPass)777 Error CodeGenPassBuilder<Derived, TargetMachineT>::addCoreISelPasses(
778 AddMachinePass &addPass) const {
779 // Enable FastISel with -fast-isel, but allow that to be overridden.
780 TM.setO0WantsFastISel(Opt.EnableFastISelOption.value_or(true));
781
782 // Determine an instruction selector.
783 enum class SelectorType { SelectionDAG, FastISel, GlobalISel };
784 SelectorType Selector;
785
786 if (Opt.EnableFastISelOption && *Opt.EnableFastISelOption == true)
787 Selector = SelectorType::FastISel;
788 else if ((Opt.EnableGlobalISelOption &&
789 *Opt.EnableGlobalISelOption == true) ||
790 (TM.Options.EnableGlobalISel &&
791 (!Opt.EnableGlobalISelOption ||
792 *Opt.EnableGlobalISelOption == false)))
793 Selector = SelectorType::GlobalISel;
794 else if (TM.getOptLevel() == CodeGenOptLevel::None && TM.getO0WantsFastISel())
795 Selector = SelectorType::FastISel;
796 else
797 Selector = SelectorType::SelectionDAG;
798
799 // Set consistently TM.Options.EnableFastISel and EnableGlobalISel.
800 if (Selector == SelectorType::FastISel) {
801 TM.setFastISel(true);
802 TM.setGlobalISel(false);
803 } else if (Selector == SelectorType::GlobalISel) {
804 TM.setFastISel(false);
805 TM.setGlobalISel(true);
806 }
807
808 // Add instruction selector passes.
809 if (Selector == SelectorType::GlobalISel) {
810 if (auto Err = derived().addIRTranslator(addPass))
811 return std::move(Err);
812
813 derived().addPreLegalizeMachineIR(addPass);
814
815 if (auto Err = derived().addLegalizeMachineIR(addPass))
816 return std::move(Err);
817
818 // Before running the register bank selector, ask the target if it
819 // wants to run some passes.
820 derived().addPreRegBankSelect(addPass);
821
822 if (auto Err = derived().addRegBankSelect(addPass))
823 return std::move(Err);
824
825 derived().addPreGlobalInstructionSelect(addPass);
826
827 if (auto Err = derived().addGlobalInstructionSelect(addPass))
828 return std::move(Err);
829
830 // Pass to reset the MachineFunction if the ISel failed.
831 addPass(ResetMachineFunctionPass(reportDiagnosticWhenGlobalISelFallback(),
832 isGlobalISelAbortEnabled()));
833
834 // Provide a fallback path when we do not want to abort on
835 // not-yet-supported input.
836 if (!isGlobalISelAbortEnabled())
837 if (auto Err = derived().addInstSelector(addPass))
838 return std::move(Err);
839
840 } else if (auto Err = derived().addInstSelector(addPass))
841 return std::move(Err);
842
843 // Expand pseudo-instructions emitted by ISel. Don't run the verifier before
844 // FinalizeISel.
845 addPass(FinalizeISelPass());
846
847 // // Print the instruction selected machine code...
848 // printAndVerify("After Instruction Selection");
849
850 return Error::success();
851 }
852
853 /// Add the complete set of target-independent postISel code generator passes.
854 ///
855 /// This can be read as the standard order of major LLVM CodeGen stages. Stages
856 /// with nontrivial configuration or multiple passes are broken out below in
857 /// add%Stage routines.
858 ///
859 /// Any CodeGenPassBuilder<Derived, TargetMachine>::addXX routine may be
860 /// overriden by the Target. The addPre/Post methods with empty header
861 /// implementations allow injecting target-specific fixups just before or after
862 /// major stages. Additionally, targets have the flexibility to change pass
863 /// order within a stage by overriding default implementation of add%Stage
864 /// routines below. Each technique has maintainability tradeoffs because
865 /// alternate pass orders are not well supported. addPre/Post works better if
866 /// the target pass is easily tied to a common pass. But if it has subtle
867 /// dependencies on multiple passes, the target should override the stage
868 /// instead.
869 template <typename Derived, typename TargetMachineT>
addMachinePasses(AddMachinePass & addPass)870 Error CodeGenPassBuilder<Derived, TargetMachineT>::addMachinePasses(
871 AddMachinePass &addPass) const {
872 // Add passes that optimize machine instructions in SSA form.
873 if (getOptLevel() != CodeGenOptLevel::None) {
874 derived().addMachineSSAOptimization(addPass);
875 } else {
876 // If the target requests it, assign local variables to stack slots relative
877 // to one another and simplify frame index references where possible.
878 addPass(LocalStackSlotPass());
879 }
880
881 if (TM.Options.EnableIPRA)
882 addPass(RegUsageInfoPropagationPass());
883
884 // Run pre-ra passes.
885 derived().addPreRegAlloc(addPass);
886
887 // Run register allocation and passes that are tightly coupled with it,
888 // including phi elimination and scheduling.
889 if (*Opt.OptimizeRegAlloc) {
890 derived().addOptimizedRegAlloc(addPass);
891 } else {
892 if (auto Err = derived().addFastRegAlloc(addPass))
893 return Err;
894 }
895
896 // Run post-ra passes.
897 derived().addPostRegAlloc(addPass);
898
899 addPass(RemoveRedundantDebugValuesPass());
900
901 // Insert prolog/epilog code. Eliminate abstract frame index references...
902 if (getOptLevel() != CodeGenOptLevel::None) {
903 addPass(PostRAMachineSinkingPass());
904 addPass(ShrinkWrapPass());
905 }
906
907 addPass(PrologEpilogInserterPass());
908
909 /// Add passes that optimize machine instructions after register allocation.
910 if (getOptLevel() != CodeGenOptLevel::None)
911 derived().addMachineLateOptimization(addPass);
912
913 // Expand pseudo instructions before second scheduling pass.
914 addPass(ExpandPostRAPseudosPass());
915
916 // Run pre-sched2 passes.
917 derived().addPreSched2(addPass);
918
919 if (Opt.EnableImplicitNullChecks)
920 addPass(ImplicitNullChecksPass());
921
922 // Second pass scheduler.
923 // Let Target optionally insert this pass by itself at some other
924 // point.
925 if (getOptLevel() != CodeGenOptLevel::None &&
926 !TM.targetSchedulesPostRAScheduling()) {
927 if (Opt.MISchedPostRA)
928 addPass(PostMachineSchedulerPass());
929 else
930 addPass(PostRASchedulerPass());
931 }
932
933 // GC
934 derived().addGCPasses(addPass);
935
936 // Basic block placement.
937 if (getOptLevel() != CodeGenOptLevel::None)
938 derived().addBlockPlacement(addPass);
939
940 // Insert before XRay Instrumentation.
941 addPass(FEntryInserterPass());
942
943 addPass(XRayInstrumentationPass());
944 addPass(PatchableFunctionPass());
945
946 derived().addPreEmitPass(addPass);
947
948 if (TM.Options.EnableIPRA)
949 // Collect register usage information and produce a register mask of
950 // clobbered registers, to be used to optimize call sites.
951 addPass(RegUsageInfoCollectorPass());
952
953 addPass(FuncletLayoutPass());
954
955 addPass(StackMapLivenessPass());
956 addPass(LiveDebugValuesPass());
957 addPass(MachineSanitizerBinaryMetadata());
958
959 if (TM.Options.EnableMachineOutliner &&
960 getOptLevel() != CodeGenOptLevel::None &&
961 Opt.EnableMachineOutliner != RunOutliner::NeverOutline) {
962 bool RunOnAllFunctions =
963 (Opt.EnableMachineOutliner == RunOutliner::AlwaysOutline);
964 bool AddOutliner = RunOnAllFunctions || TM.Options.SupportsDefaultOutlining;
965 if (AddOutliner)
966 addPass(MachineOutlinerPass(RunOnAllFunctions));
967 }
968
969 // Add passes that directly emit MI after all other MI passes.
970 derived().addPreEmitPass2(addPass);
971
972 return Error::success();
973 }
974
975 /// Add passes that optimize machine instructions in SSA form.
976 template <typename Derived, typename TargetMachineT>
addMachineSSAOptimization(AddMachinePass & addPass)977 void CodeGenPassBuilder<Derived, TargetMachineT>::addMachineSSAOptimization(
978 AddMachinePass &addPass) const {
979 // Pre-ra tail duplication.
980 addPass(EarlyTailDuplicatePass());
981
982 // Optimize PHIs before DCE: removing dead PHI cycles may make more
983 // instructions dead.
984 addPass(OptimizePHIsPass());
985
986 // This pass merges large allocas. StackSlotColoring is a different pass
987 // which merges spill slots.
988 addPass(StackColoringPass());
989
990 // If the target requests it, assign local variables to stack slots relative
991 // to one another and simplify frame index references where possible.
992 addPass(LocalStackSlotPass());
993
994 // With optimization, dead code should already be eliminated. However
995 // there is one known exception: lowered code for arguments that are only
996 // used by tail calls, where the tail calls reuse the incoming stack
997 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
998 addPass(DeadMachineInstructionElimPass());
999
1000 // Allow targets to insert passes that improve instruction level parallelism,
1001 // like if-conversion. Such passes will typically need dominator trees and
1002 // loop info, just like LICM and CSE below.
1003 derived().addILPOpts(addPass);
1004
1005 addPass(EarlyMachineLICMPass());
1006 addPass(MachineCSEPass());
1007
1008 addPass(MachineSinkingPass());
1009
1010 addPass(PeepholeOptimizerPass());
1011 // Clean-up the dead code that may have been generated by peephole
1012 // rewriting.
1013 addPass(DeadMachineInstructionElimPass());
1014 }
1015
1016 //===---------------------------------------------------------------------===//
1017 /// Register Allocation Pass Configuration
1018 //===---------------------------------------------------------------------===//
1019
1020 /// Instantiate the default register allocator pass for this target for either
1021 /// the optimized or unoptimized allocation path. This will be added to the pass
1022 /// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
1023 /// in the optimized case.
1024 ///
1025 /// A target that uses the standard regalloc pass order for fast or optimized
1026 /// allocation may still override this for per-target regalloc
1027 /// selection. But -regalloc=... always takes precedence.
1028 template <typename Derived, typename TargetMachineT>
addTargetRegisterAllocator(AddMachinePass & addPass,bool Optimized)1029 void CodeGenPassBuilder<Derived, TargetMachineT>::addTargetRegisterAllocator(
1030 AddMachinePass &addPass, bool Optimized) const {
1031 if (Optimized)
1032 addPass(RAGreedyPass());
1033 else
1034 addPass(RAFastPass());
1035 }
1036
1037 /// Find and instantiate the register allocation pass requested by this target
1038 /// at the current optimization level. Different register allocators are
1039 /// defined as separate passes because they may require different analysis.
1040 template <typename Derived, typename TargetMachineT>
addRegAllocPass(AddMachinePass & addPass,bool Optimized)1041 void CodeGenPassBuilder<Derived, TargetMachineT>::addRegAllocPass(
1042 AddMachinePass &addPass, bool Optimized) const {
1043 // TODO: Parse Opt.RegAlloc to add register allocator.
1044 }
1045
1046 template <typename Derived, typename TargetMachineT>
addRegAssignmentFast(AddMachinePass & addPass)1047 Error CodeGenPassBuilder<Derived, TargetMachineT>::addRegAssignmentFast(
1048 AddMachinePass &addPass) const {
1049 // TODO: Ensure allocator is default or fast.
1050 addRegAllocPass(addPass, false);
1051 return Error::success();
1052 }
1053
1054 template <typename Derived, typename TargetMachineT>
addRegAssignmentOptimized(AddMachinePass & addPass)1055 Error CodeGenPassBuilder<Derived, TargetMachineT>::addRegAssignmentOptimized(
1056 AddMachinePass &addPass) const {
1057 // Add the selected register allocation pass.
1058 addRegAllocPass(addPass, true);
1059
1060 // Allow targets to change the register assignments before rewriting.
1061 derived().addPreRewrite(addPass);
1062
1063 // Finally rewrite virtual registers.
1064 addPass(VirtRegRewriterPass());
1065 // Perform stack slot coloring and post-ra machine LICM.
1066 //
1067 // FIXME: Re-enable coloring with register when it's capable of adding
1068 // kill markers.
1069 addPass(StackSlotColoringPass());
1070
1071 return Error::success();
1072 }
1073
1074 /// Add the minimum set of target-independent passes that are required for
1075 /// register allocation. No coalescing or scheduling.
1076 template <typename Derived, typename TargetMachineT>
addFastRegAlloc(AddMachinePass & addPass)1077 Error CodeGenPassBuilder<Derived, TargetMachineT>::addFastRegAlloc(
1078 AddMachinePass &addPass) const {
1079 addPass(PHIEliminationPass());
1080 addPass(TwoAddressInstructionPass());
1081 return derived().addRegAssignmentFast(addPass);
1082 }
1083
1084 /// Add standard target-independent passes that are tightly coupled with
1085 /// optimized register allocation, including coalescing, machine instruction
1086 /// scheduling, and register allocation itself.
1087 template <typename Derived, typename TargetMachineT>
addOptimizedRegAlloc(AddMachinePass & addPass)1088 void CodeGenPassBuilder<Derived, TargetMachineT>::addOptimizedRegAlloc(
1089 AddMachinePass &addPass) const {
1090 addPass(DetectDeadLanesPass());
1091
1092 addPass(InitUndefPass());
1093
1094 addPass(ProcessImplicitDefsPass());
1095
1096 // Edge splitting is smarter with machine loop info.
1097 addPass(PHIEliminationPass());
1098
1099 // Eventually, we want to run LiveIntervals before PHI elimination.
1100 if (Opt.EarlyLiveIntervals)
1101 addPass(LiveIntervalsPass());
1102
1103 addPass(TwoAddressInstructionPass());
1104 addPass(RegisterCoalescerPass());
1105
1106 // The machine scheduler may accidentally create disconnected components
1107 // when moving subregister definitions around, avoid this by splitting them to
1108 // separate vregs before. Splitting can also improve reg. allocation quality.
1109 addPass(RenameIndependentSubregsPass());
1110
1111 // PreRA instruction scheduling.
1112 addPass(MachineSchedulerPass());
1113
1114 if (derived().addRegAssignmentOptimized(addPass)) {
1115 // Allow targets to expand pseudo instructions depending on the choice of
1116 // registers before MachineCopyPropagation.
1117 derived().addPostRewrite(addPass);
1118
1119 // Copy propagate to forward register uses and try to eliminate COPYs that
1120 // were not coalesced.
1121 addPass(MachineCopyPropagationPass());
1122
1123 // Run post-ra machine LICM to hoist reloads / remats.
1124 //
1125 // FIXME: can this move into MachineLateOptimization?
1126 addPass(MachineLICMPass());
1127 }
1128 }
1129
1130 //===---------------------------------------------------------------------===//
1131 /// Post RegAlloc Pass Configuration
1132 //===---------------------------------------------------------------------===//
1133
1134 /// Add passes that optimize machine instructions after register allocation.
1135 template <typename Derived, typename TargetMachineT>
addMachineLateOptimization(AddMachinePass & addPass)1136 void CodeGenPassBuilder<Derived, TargetMachineT>::addMachineLateOptimization(
1137 AddMachinePass &addPass) const {
1138 // Branch folding must be run after regalloc and prolog/epilog insertion.
1139 addPass(BranchFolderPass());
1140
1141 // Tail duplication.
1142 // Note that duplicating tail just increases code size and degrades
1143 // performance for targets that require Structured Control Flow.
1144 // In addition it can also make CFG irreducible. Thus we disable it.
1145 if (!TM.requiresStructuredCFG())
1146 addPass(TailDuplicatePass());
1147
1148 // Cleanup of redundant (identical) address/immediate loads.
1149 addPass(MachineLateInstrsCleanupPass());
1150
1151 // Copy propagation.
1152 addPass(MachineCopyPropagationPass());
1153 }
1154
1155 /// Add standard basic block placement passes.
1156 template <typename Derived, typename TargetMachineT>
addBlockPlacement(AddMachinePass & addPass)1157 void CodeGenPassBuilder<Derived, TargetMachineT>::addBlockPlacement(
1158 AddMachinePass &addPass) const {
1159 addPass(MachineBlockPlacementPass());
1160 // Run a separate pass to collect block placement statistics.
1161 if (Opt.EnableBlockPlacementStats)
1162 addPass(MachineBlockPlacementStatsPass());
1163 }
1164
1165 } // namespace llvm
1166
1167 #endif // LLVM_PASSES_CODEGENPASSBUILDER_H
1168