xref: /llvm-project/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp (revision a13ec9cd54deba3a6779ce1114503cf86a3f8658)
1 //===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
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 // Top-level implementation for the PowerPC target.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "PPCTargetMachine.h"
14 #include "MCTargetDesc/PPCMCTargetDesc.h"
15 #include "PPC.h"
16 #include "PPCMachineFunctionInfo.h"
17 #include "PPCMachineScheduler.h"
18 #include "PPCMacroFusion.h"
19 #include "PPCSubtarget.h"
20 #include "PPCTargetObjectFile.h"
21 #include "PPCTargetTransformInfo.h"
22 #include "TargetInfo/PowerPCTargetInfo.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Analysis/TargetTransformInfo.h"
25 #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
26 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
27 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
28 #include "llvm/CodeGen/GlobalISel/Legalizer.h"
29 #include "llvm/CodeGen/GlobalISel/Localizer.h"
30 #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
31 #include "llvm/CodeGen/MachineScheduler.h"
32 #include "llvm/CodeGen/Passes.h"
33 #include "llvm/CodeGen/TargetPassConfig.h"
34 #include "llvm/IR/Attributes.h"
35 #include "llvm/IR/DataLayout.h"
36 #include "llvm/IR/Function.h"
37 #include "llvm/InitializePasses.h"
38 #include "llvm/MC/TargetRegistry.h"
39 #include "llvm/Pass.h"
40 #include "llvm/Support/CodeGen.h"
41 #include "llvm/Support/CommandLine.h"
42 #include "llvm/Target/TargetLoweringObjectFile.h"
43 #include "llvm/Target/TargetOptions.h"
44 #include "llvm/TargetParser/Triple.h"
45 #include "llvm/Transforms/Scalar.h"
46 #include <cassert>
47 #include <memory>
48 #include <optional>
49 #include <string>
50 
51 using namespace llvm;
52 
53 
54 static cl::opt<bool>
55     EnableBranchCoalescing("enable-ppc-branch-coalesce", cl::Hidden,
56                            cl::desc("enable coalescing of duplicate branches for PPC"));
57 static cl::
58 opt<bool> DisableCTRLoops("disable-ppc-ctrloops", cl::Hidden,
59                         cl::desc("Disable CTR loops for PPC"));
60 
61 static cl::
62 opt<bool> DisableInstrFormPrep("disable-ppc-instr-form-prep", cl::Hidden,
63                             cl::desc("Disable PPC loop instr form prep"));
64 
65 static cl::opt<bool>
66 VSXFMAMutateEarly("schedule-ppc-vsx-fma-mutation-early",
67   cl::Hidden, cl::desc("Schedule VSX FMA instruction mutation early"));
68 
69 static cl::
70 opt<bool> DisableVSXSwapRemoval("disable-ppc-vsx-swap-removal", cl::Hidden,
71                                 cl::desc("Disable VSX Swap Removal for PPC"));
72 
73 static cl::
74 opt<bool> DisableMIPeephole("disable-ppc-peephole", cl::Hidden,
75                             cl::desc("Disable machine peepholes for PPC"));
76 
77 static cl::opt<bool>
78 EnableGEPOpt("ppc-gep-opt", cl::Hidden,
79              cl::desc("Enable optimizations on complex GEPs"),
80              cl::init(true));
81 
82 static cl::opt<bool>
83 EnablePrefetch("enable-ppc-prefetching",
84                   cl::desc("enable software prefetching on PPC"),
85                   cl::init(false), cl::Hidden);
86 
87 static cl::opt<bool>
88 EnableExtraTOCRegDeps("enable-ppc-extra-toc-reg-deps",
89                       cl::desc("Add extra TOC register dependencies"),
90                       cl::init(true), cl::Hidden);
91 
92 static cl::opt<bool>
93 EnableMachineCombinerPass("ppc-machine-combiner",
94                           cl::desc("Enable the machine combiner pass"),
95                           cl::init(true), cl::Hidden);
96 
97 static cl::opt<bool>
98   ReduceCRLogical("ppc-reduce-cr-logicals",
99                   cl::desc("Expand eligible cr-logical binary ops to branches"),
100                   cl::init(true), cl::Hidden);
101 
102 static cl::opt<bool> EnablePPCGenScalarMASSEntries(
103     "enable-ppc-gen-scalar-mass", cl::init(false),
104     cl::desc("Enable lowering math functions to their corresponding MASS "
105              "(scalar) entries"),
106     cl::Hidden);
107 
108 static cl::opt<bool>
109     EnableGlobalMerge("ppc-global-merge", cl::Hidden, cl::init(false),
110                       cl::desc("Enable the global merge pass"));
111 
112 static cl::opt<unsigned>
113     GlobalMergeMaxOffset("ppc-global-merge-max-offset", cl::Hidden,
114                          cl::init(0x7fff),
115                          cl::desc("Maximum global merge offset"));
116 
117 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCTarget() {
118   // Register the targets
119   RegisterTargetMachine<PPCTargetMachine> A(getThePPC32Target());
120   RegisterTargetMachine<PPCTargetMachine> B(getThePPC32LETarget());
121   RegisterTargetMachine<PPCTargetMachine> C(getThePPC64Target());
122   RegisterTargetMachine<PPCTargetMachine> D(getThePPC64LETarget());
123 
124   PassRegistry &PR = *PassRegistry::getPassRegistry();
125 #ifndef NDEBUG
126   initializePPCCTRLoopsVerifyPass(PR);
127 #endif
128   initializePPCLoopInstrFormPrepPass(PR);
129   initializePPCTOCRegDepsPass(PR);
130   initializePPCEarlyReturnPass(PR);
131   initializePPCVSXCopyPass(PR);
132   initializePPCVSXFMAMutatePass(PR);
133   initializePPCVSXSwapRemovalPass(PR);
134   initializePPCReduceCRLogicalsPass(PR);
135   initializePPCBSelPass(PR);
136   initializePPCBranchCoalescingPass(PR);
137   initializePPCBoolRetToIntPass(PR);
138   initializePPCPreEmitPeepholePass(PR);
139   initializePPCTLSDynamicCallPass(PR);
140   initializePPCMIPeepholePass(PR);
141   initializePPCLowerMASSVEntriesPass(PR);
142   initializePPCGenScalarMASSEntriesPass(PR);
143   initializePPCExpandAtomicPseudoPass(PR);
144   initializeGlobalISel(PR);
145   initializePPCCTRLoopsPass(PR);
146   initializePPCDAGToDAGISelLegacyPass(PR);
147 }
148 
149 static bool isLittleEndianTriple(const Triple &T) {
150   return T.getArch() == Triple::ppc64le || T.getArch() == Triple::ppcle;
151 }
152 
153 /// Return the datalayout string of a subtarget.
154 static std::string getDataLayoutString(const Triple &T) {
155   bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
156   std::string Ret;
157 
158   // Most PPC* platforms are big endian, PPC(64)LE is little endian.
159   if (isLittleEndianTriple(T))
160     Ret = "e";
161   else
162     Ret = "E";
163 
164   Ret += DataLayout::getManglingComponent(T);
165 
166   // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
167   // pointers.
168   if (!is64Bit || T.getOS() == Triple::Lv2)
169     Ret += "-p:32:32";
170 
171   // If the target ABI uses function descriptors, then the alignment of function
172   // pointers depends on the alignment used to emit the descriptor. Otherwise,
173   // function pointers are aligned to 32 bits because the instructions must be.
174   if ((T.getArch() == Triple::ppc64 && !T.isPPC64ELFv2ABI())) {
175     Ret += "-Fi64";
176   } else if (T.isOSAIX()) {
177     Ret += is64Bit ? "-Fi64" : "-Fi32";
178   } else {
179     Ret += "-Fn32";
180   }
181 
182   // Note, the alignment values for f64 and i64 on ppc64 in Darwin
183   // documentation are wrong; these are correct (i.e. "what gcc does").
184   Ret += "-i64:64";
185 
186   // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
187   if (is64Bit)
188     Ret += "-i128:128-n32:64";
189   else
190     Ret += "-n32";
191 
192   // Specify the vector alignment explicitly. For v256i1 and v512i1, the
193   // calculated alignment would be 256*alignment(i1) and 512*alignment(i1),
194   // which is 256 and 512 bytes - way over aligned.
195   if (is64Bit && (T.isOSAIX() || T.isOSLinux()))
196     Ret += "-S128-v256:256:256-v512:512:512";
197 
198   return Ret;
199 }
200 
201 static std::string computeFSAdditions(StringRef FS, CodeGenOptLevel OL,
202                                       const Triple &TT) {
203   std::string FullFS = std::string(FS);
204 
205   // Make sure 64-bit features are available when CPUname is generic
206   if (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le) {
207     if (!FullFS.empty())
208       FullFS = "+64bit," + FullFS;
209     else
210       FullFS = "+64bit";
211   }
212 
213   if (OL >= CodeGenOptLevel::Default) {
214     if (!FullFS.empty())
215       FullFS = "+crbits," + FullFS;
216     else
217       FullFS = "+crbits";
218   }
219 
220   if (OL != CodeGenOptLevel::None) {
221     if (!FullFS.empty())
222       FullFS = "+invariant-function-descriptors," + FullFS;
223     else
224       FullFS = "+invariant-function-descriptors";
225   }
226 
227   if (TT.isOSAIX()) {
228     if (!FullFS.empty())
229       FullFS = "+aix," + FullFS;
230     else
231       FullFS = "+aix";
232   }
233 
234   return FullFS;
235 }
236 
237 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
238   if (TT.isOSAIX())
239     return std::make_unique<TargetLoweringObjectFileXCOFF>();
240 
241   return std::make_unique<PPC64LinuxTargetObjectFile>();
242 }
243 
244 static PPCTargetMachine::PPCABI computeTargetABI(const Triple &TT,
245                                                  const TargetOptions &Options) {
246   if (Options.MCOptions.getABIName().starts_with("elfv1"))
247     return PPCTargetMachine::PPC_ABI_ELFv1;
248   else if (Options.MCOptions.getABIName().starts_with("elfv2"))
249     return PPCTargetMachine::PPC_ABI_ELFv2;
250 
251   assert(Options.MCOptions.getABIName().empty() &&
252          "Unknown target-abi option!");
253 
254   switch (TT.getArch()) {
255   case Triple::ppc64le:
256     return PPCTargetMachine::PPC_ABI_ELFv2;
257   case Triple::ppc64:
258     if (TT.isPPC64ELFv2ABI())
259       return PPCTargetMachine::PPC_ABI_ELFv2;
260     else
261       return PPCTargetMachine::PPC_ABI_ELFv1;
262   default:
263     return PPCTargetMachine::PPC_ABI_UNKNOWN;
264   }
265 }
266 
267 static Reloc::Model getEffectiveRelocModel(const Triple &TT,
268                                            std::optional<Reloc::Model> RM) {
269   if (TT.isOSAIX() && RM && *RM != Reloc::PIC_)
270     report_fatal_error("invalid relocation model, AIX only supports PIC",
271                        false);
272 
273   if (RM)
274     return *RM;
275 
276   // Big Endian PPC and AIX default to PIC.
277   if (TT.getArch() == Triple::ppc64 || TT.isOSAIX())
278     return Reloc::PIC_;
279 
280   // Rest are static by default.
281   return Reloc::Static;
282 }
283 
284 static CodeModel::Model
285 getEffectivePPCCodeModel(const Triple &TT, std::optional<CodeModel::Model> CM,
286                          bool JIT) {
287   if (CM) {
288     if (*CM == CodeModel::Tiny)
289       report_fatal_error("Target does not support the tiny CodeModel", false);
290     if (*CM == CodeModel::Kernel)
291       report_fatal_error("Target does not support the kernel CodeModel", false);
292     return *CM;
293   }
294 
295   if (JIT)
296     return CodeModel::Small;
297   if (TT.isOSAIX())
298     return CodeModel::Small;
299 
300   assert(TT.isOSBinFormatELF() && "All remaining PPC OSes are ELF based.");
301 
302   if (TT.isArch32Bit())
303     return CodeModel::Small;
304 
305   assert(TT.isArch64Bit() && "Unsupported PPC architecture.");
306   return CodeModel::Medium;
307 }
308 
309 
310 static ScheduleDAGInstrs *createPPCMachineScheduler(MachineSchedContext *C) {
311   const PPCSubtarget &ST = C->MF->getSubtarget<PPCSubtarget>();
312   ScheduleDAGMILive *DAG =
313     new ScheduleDAGMILive(C, ST.usePPCPreRASchedStrategy() ?
314                           std::make_unique<PPCPreRASchedStrategy>(C) :
315                           std::make_unique<GenericScheduler>(C));
316   // add DAG Mutations here.
317   DAG->addMutation(createCopyConstrainDAGMutation(DAG->TII, DAG->TRI));
318   if (ST.hasStoreFusion())
319     DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
320   if (ST.hasFusion())
321     DAG->addMutation(createPowerPCMacroFusionDAGMutation());
322 
323   return DAG;
324 }
325 
326 static ScheduleDAGInstrs *createPPCPostMachineScheduler(
327   MachineSchedContext *C) {
328   const PPCSubtarget &ST = C->MF->getSubtarget<PPCSubtarget>();
329   ScheduleDAGMI *DAG =
330     new ScheduleDAGMI(C, ST.usePPCPostRASchedStrategy() ?
331                       std::make_unique<PPCPostRASchedStrategy>(C) :
332                       std::make_unique<PostGenericScheduler>(C), true);
333   // add DAG Mutations here.
334   if (ST.hasStoreFusion())
335     DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
336   if (ST.hasFusion())
337     DAG->addMutation(createPowerPCMacroFusionDAGMutation());
338   return DAG;
339 }
340 
341 // The FeatureString here is a little subtle. We are modifying the feature
342 // string with what are (currently) non-function specific overrides as it goes
343 // into the CodeGenTargetMachineImpl constructor and then using the stored value
344 // in the Subtarget constructor below it.
345 PPCTargetMachine::PPCTargetMachine(const Target &T, const Triple &TT,
346                                    StringRef CPU, StringRef FS,
347                                    const TargetOptions &Options,
348                                    std::optional<Reloc::Model> RM,
349                                    std::optional<CodeModel::Model> CM,
350                                    CodeGenOptLevel OL, bool JIT)
351     : CodeGenTargetMachineImpl(T, getDataLayoutString(TT), TT, CPU,
352                                computeFSAdditions(FS, OL, TT), Options,
353                                getEffectiveRelocModel(TT, RM),
354                                getEffectivePPCCodeModel(TT, CM, JIT), OL),
355       TLOF(createTLOF(getTargetTriple())),
356       TargetABI(computeTargetABI(TT, Options)),
357       Endianness(isLittleEndianTriple(TT) ? Endian::LITTLE : Endian::BIG) {
358   initAsmInfo();
359 }
360 
361 PPCTargetMachine::~PPCTargetMachine() = default;
362 
363 const PPCSubtarget *
364 PPCTargetMachine::getSubtargetImpl(const Function &F) const {
365   Attribute CPUAttr = F.getFnAttribute("target-cpu");
366   Attribute TuneAttr = F.getFnAttribute("tune-cpu");
367   Attribute FSAttr = F.getFnAttribute("target-features");
368 
369   std::string CPU =
370       CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
371   std::string TuneCPU =
372       TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
373   std::string FS =
374       FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
375 
376   // FIXME: This is related to the code below to reset the target options,
377   // we need to know whether or not the soft float flag is set on the
378   // function before we can generate a subtarget. We also need to use
379   // it as a key for the subtarget since that can be the only difference
380   // between two functions.
381   bool SoftFloat = F.getFnAttribute("use-soft-float").getValueAsBool();
382   // If the soft float attribute is set on the function turn on the soft float
383   // subtarget feature.
384   if (SoftFloat)
385     FS += FS.empty() ? "-hard-float" : ",-hard-float";
386 
387   auto &I = SubtargetMap[CPU + TuneCPU + FS];
388   if (!I) {
389     // This needs to be done before we create a new subtarget since any
390     // creation will depend on the TM and the code generation flags on the
391     // function that reside in TargetOptions.
392     resetTargetOptions(F);
393     I = std::make_unique<PPCSubtarget>(
394         TargetTriple, CPU, TuneCPU,
395         // FIXME: It would be good to have the subtarget additions here
396         // not necessary. Anything that turns them on/off (overrides) ends
397         // up being put at the end of the feature string, but the defaults
398         // shouldn't require adding them. Fixing this means pulling Feature64Bit
399         // out of most of the target cpus in the .td file and making it set only
400         // as part of initialization via the TargetTriple.
401         computeFSAdditions(FS, getOptLevel(), getTargetTriple()), *this);
402   }
403   return I.get();
404 }
405 
406 //===----------------------------------------------------------------------===//
407 // Pass Pipeline Configuration
408 //===----------------------------------------------------------------------===//
409 
410 namespace {
411 
412 /// PPC Code Generator Pass Configuration Options.
413 class PPCPassConfig : public TargetPassConfig {
414 public:
415   PPCPassConfig(PPCTargetMachine &TM, PassManagerBase &PM)
416     : TargetPassConfig(TM, PM) {
417     // At any optimization level above -O0 we use the Machine Scheduler and not
418     // the default Post RA List Scheduler.
419     if (TM.getOptLevel() != CodeGenOptLevel::None)
420       substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
421   }
422 
423   PPCTargetMachine &getPPCTargetMachine() const {
424     return getTM<PPCTargetMachine>();
425   }
426 
427   void addIRPasses() override;
428   bool addPreISel() override;
429   bool addILPOpts() override;
430   bool addInstSelector() override;
431   void addMachineSSAOptimization() override;
432   void addPreRegAlloc() override;
433   void addPreSched2() override;
434   void addPreEmitPass() override;
435   void addPreEmitPass2() override;
436   // GlobalISEL
437   bool addIRTranslator() override;
438   bool addLegalizeMachineIR() override;
439   bool addRegBankSelect() override;
440   bool addGlobalInstructionSelect() override;
441 
442   ScheduleDAGInstrs *
443   createMachineScheduler(MachineSchedContext *C) const override {
444     return createPPCMachineScheduler(C);
445   }
446   ScheduleDAGInstrs *
447   createPostMachineScheduler(MachineSchedContext *C) const override {
448     return createPPCPostMachineScheduler(C);
449   }
450 };
451 
452 } // end anonymous namespace
453 
454 TargetPassConfig *PPCTargetMachine::createPassConfig(PassManagerBase &PM) {
455   return new PPCPassConfig(*this, PM);
456 }
457 
458 void PPCPassConfig::addIRPasses() {
459   if (TM->getOptLevel() != CodeGenOptLevel::None)
460     addPass(createPPCBoolRetToIntPass());
461   addPass(createAtomicExpandLegacyPass());
462 
463   // Lower generic MASSV routines to PowerPC subtarget-specific entries.
464   addPass(createPPCLowerMASSVEntriesPass());
465 
466   // Generate PowerPC target-specific entries for scalar math functions
467   // that are available in IBM MASS (scalar) library.
468   if (TM->getOptLevel() == CodeGenOptLevel::Aggressive &&
469       EnablePPCGenScalarMASSEntries) {
470     TM->Options.PPCGenScalarMASSEntries = EnablePPCGenScalarMASSEntries;
471     addPass(createPPCGenScalarMASSEntriesPass());
472   }
473 
474   // If explicitly requested, add explicit data prefetch intrinsics.
475   if (EnablePrefetch.getNumOccurrences() > 0)
476     addPass(createLoopDataPrefetchPass());
477 
478   if (TM->getOptLevel() >= CodeGenOptLevel::Default && EnableGEPOpt) {
479     // Call SeparateConstOffsetFromGEP pass to extract constants within indices
480     // and lower a GEP with multiple indices to either arithmetic operations or
481     // multiple GEPs with single index.
482     addPass(createSeparateConstOffsetFromGEPPass(true));
483     // Call EarlyCSE pass to find and remove subexpressions in the lowered
484     // result.
485     addPass(createEarlyCSEPass());
486     // Do loop invariant code motion in case part of the lowered result is
487     // invariant.
488     addPass(createLICMPass());
489   }
490 
491   TargetPassConfig::addIRPasses();
492 }
493 
494 bool PPCPassConfig::addPreISel() {
495   // The GlobalMerge pass is intended to be on by default on AIX.
496   // Specifying the command line option overrides the AIX default.
497   if ((EnableGlobalMerge.getNumOccurrences() > 0)
498           ? EnableGlobalMerge
499           : getOptLevel() != CodeGenOptLevel::None)
500     addPass(createGlobalMergePass(TM, GlobalMergeMaxOffset, false, false, true,
501                                   true));
502 
503   if (!DisableInstrFormPrep && getOptLevel() != CodeGenOptLevel::None)
504     addPass(createPPCLoopInstrFormPrepPass(getPPCTargetMachine()));
505 
506   if (!DisableCTRLoops && getOptLevel() != CodeGenOptLevel::None)
507     addPass(createHardwareLoopsLegacyPass());
508 
509   return false;
510 }
511 
512 bool PPCPassConfig::addILPOpts() {
513   addPass(&EarlyIfConverterLegacyID);
514 
515   if (EnableMachineCombinerPass)
516     addPass(&MachineCombinerID);
517 
518   return true;
519 }
520 
521 bool PPCPassConfig::addInstSelector() {
522   // Install an instruction selector.
523   addPass(createPPCISelDag(getPPCTargetMachine(), getOptLevel()));
524 
525 #ifndef NDEBUG
526   if (!DisableCTRLoops && getOptLevel() != CodeGenOptLevel::None)
527     addPass(createPPCCTRLoopsVerify());
528 #endif
529 
530   addPass(createPPCVSXCopyPass());
531   return false;
532 }
533 
534 void PPCPassConfig::addMachineSSAOptimization() {
535   // Run CTR loops pass before any cfg modification pass to prevent the
536   // canonical form of hardware loop from being destroied.
537   if (!DisableCTRLoops && getOptLevel() != CodeGenOptLevel::None)
538     addPass(createPPCCTRLoopsPass());
539 
540   // PPCBranchCoalescingPass need to be done before machine sinking
541   // since it merges empty blocks.
542   if (EnableBranchCoalescing && getOptLevel() != CodeGenOptLevel::None)
543     addPass(createPPCBranchCoalescingPass());
544   TargetPassConfig::addMachineSSAOptimization();
545   // For little endian, remove where possible the vector swap instructions
546   // introduced at code generation to normalize vector element order.
547   if (TM->getTargetTriple().getArch() == Triple::ppc64le &&
548       !DisableVSXSwapRemoval)
549     addPass(createPPCVSXSwapRemovalPass());
550   // Reduce the number of cr-logical ops.
551   if (ReduceCRLogical && getOptLevel() != CodeGenOptLevel::None)
552     addPass(createPPCReduceCRLogicalsPass());
553   // Target-specific peephole cleanups performed after instruction
554   // selection.
555   if (!DisableMIPeephole) {
556     addPass(createPPCMIPeepholePass());
557     addPass(&DeadMachineInstructionElimID);
558   }
559 }
560 
561 void PPCPassConfig::addPreRegAlloc() {
562   if (getOptLevel() != CodeGenOptLevel::None) {
563     initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
564     insertPass(VSXFMAMutateEarly ? &RegisterCoalescerID : &MachineSchedulerID,
565                &PPCVSXFMAMutateID);
566   }
567 
568   // FIXME: We probably don't need to run these for -fPIE.
569   if (getPPCTargetMachine().isPositionIndependent()) {
570     // FIXME: LiveVariables should not be necessary here!
571     // PPCTLSDynamicCallPass uses LiveIntervals which previously dependent on
572     // LiveVariables. This (unnecessary) dependency has been removed now,
573     // however a stage-2 clang build fails without LiveVariables computed here.
574     addPass(&LiveVariablesID);
575     addPass(createPPCTLSDynamicCallPass());
576   }
577   if (EnableExtraTOCRegDeps)
578     addPass(createPPCTOCRegDepsPass());
579 
580   if (getOptLevel() != CodeGenOptLevel::None)
581     addPass(&MachinePipelinerID);
582 }
583 
584 void PPCPassConfig::addPreSched2() {
585   if (getOptLevel() != CodeGenOptLevel::None)
586     addPass(&IfConverterID);
587 }
588 
589 void PPCPassConfig::addPreEmitPass() {
590   addPass(createPPCPreEmitPeepholePass());
591 
592   if (getOptLevel() != CodeGenOptLevel::None)
593     addPass(createPPCEarlyReturnPass());
594 }
595 
596 void PPCPassConfig::addPreEmitPass2() {
597   // Schedule the expansion of AMOs at the last possible moment, avoiding the
598   // possibility for other passes to break the requirements for forward
599   // progress in the LL/SC block.
600   addPass(createPPCExpandAtomicPseudoPass());
601   // Must run branch selection immediately preceding the asm printer.
602   addPass(createPPCBranchSelectionPass());
603 }
604 
605 TargetTransformInfo
606 PPCTargetMachine::getTargetTransformInfo(const Function &F) const {
607   return TargetTransformInfo(PPCTTIImpl(this, F));
608 }
609 
610 bool PPCTargetMachine::isLittleEndian() const {
611   assert(Endianness != Endian::NOT_DETECTED &&
612          "Unable to determine endianness");
613   return Endianness == Endian::LITTLE;
614 }
615 
616 MachineFunctionInfo *PPCTargetMachine::createMachineFunctionInfo(
617     BumpPtrAllocator &Allocator, const Function &F,
618     const TargetSubtargetInfo *STI) const {
619   return PPCFunctionInfo::create<PPCFunctionInfo>(Allocator, F, STI);
620 }
621 
622 static MachineSchedRegistry
623 PPCPreRASchedRegistry("ppc-prera",
624                       "Run PowerPC PreRA specific scheduler",
625                       createPPCMachineScheduler);
626 
627 static MachineSchedRegistry
628 PPCPostRASchedRegistry("ppc-postra",
629                        "Run PowerPC PostRA specific scheduler",
630                        createPPCPostMachineScheduler);
631 
632 // Global ISEL
633 bool PPCPassConfig::addIRTranslator() {
634   addPass(new IRTranslator());
635   return false;
636 }
637 
638 bool PPCPassConfig::addLegalizeMachineIR() {
639   addPass(new Legalizer());
640   return false;
641 }
642 
643 bool PPCPassConfig::addRegBankSelect() {
644   addPass(new RegBankSelect());
645   return false;
646 }
647 
648 bool PPCPassConfig::addGlobalInstructionSelect() {
649   addPass(new InstructionSelect(getOptLevel()));
650   return false;
651 }
652