xref: /freebsd/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Top-level implementation for the PowerPC target.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "PPCTargetMachine.h"
140b57cec5SDimitry Andric #include "MCTargetDesc/PPCMCTargetDesc.h"
150b57cec5SDimitry Andric #include "PPC.h"
16bdd1243dSDimitry Andric #include "PPCMachineFunctionInfo.h"
170b57cec5SDimitry Andric #include "PPCMachineScheduler.h"
185ffd83dbSDimitry Andric #include "PPCMacroFusion.h"
190b57cec5SDimitry Andric #include "PPCSubtarget.h"
200b57cec5SDimitry Andric #include "PPCTargetObjectFile.h"
210b57cec5SDimitry Andric #include "PPCTargetTransformInfo.h"
220b57cec5SDimitry Andric #include "TargetInfo/PowerPCTargetInfo.h"
230b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
240b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
250b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
26e8d8bef9SDimitry Andric #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
27e8d8bef9SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
2881ad6265SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
29e8d8bef9SDimitry Andric #include "llvm/CodeGen/GlobalISel/Legalizer.h"
30e8d8bef9SDimitry Andric #include "llvm/CodeGen/GlobalISel/Localizer.h"
31e8d8bef9SDimitry Andric #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
32e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineScheduler.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
350b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
360b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
370b57cec5SDimitry Andric #include "llvm/IR/Function.h"
38e8d8bef9SDimitry Andric #include "llvm/InitializePasses.h"
39349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
400b57cec5SDimitry Andric #include "llvm/Pass.h"
410b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
420b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
430b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
440b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
45*06c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
460b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h"
470b57cec5SDimitry Andric #include <cassert>
480b57cec5SDimitry Andric #include <memory>
49bdd1243dSDimitry Andric #include <optional>
500b57cec5SDimitry Andric #include <string>
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric using namespace llvm;
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric static cl::opt<bool>
560b57cec5SDimitry Andric     EnableBranchCoalescing("enable-ppc-branch-coalesce", cl::Hidden,
570b57cec5SDimitry Andric                            cl::desc("enable coalescing of duplicate branches for PPC"));
580b57cec5SDimitry Andric static cl::
590b57cec5SDimitry Andric opt<bool> DisableCTRLoops("disable-ppc-ctrloops", cl::Hidden,
600b57cec5SDimitry Andric                         cl::desc("Disable CTR loops for PPC"));
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric static cl::
63480093f4SDimitry Andric opt<bool> DisableInstrFormPrep("disable-ppc-instr-form-prep", cl::Hidden,
64480093f4SDimitry Andric                             cl::desc("Disable PPC loop instr form prep"));
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric static cl::opt<bool>
670b57cec5SDimitry Andric VSXFMAMutateEarly("schedule-ppc-vsx-fma-mutation-early",
680b57cec5SDimitry Andric   cl::Hidden, cl::desc("Schedule VSX FMA instruction mutation early"));
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric static cl::
710b57cec5SDimitry Andric opt<bool> DisableVSXSwapRemoval("disable-ppc-vsx-swap-removal", cl::Hidden,
720b57cec5SDimitry Andric                                 cl::desc("Disable VSX Swap Removal for PPC"));
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric static cl::
750b57cec5SDimitry Andric opt<bool> DisableMIPeephole("disable-ppc-peephole", cl::Hidden,
760b57cec5SDimitry Andric                             cl::desc("Disable machine peepholes for PPC"));
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric static cl::opt<bool>
790b57cec5SDimitry Andric EnableGEPOpt("ppc-gep-opt", cl::Hidden,
800b57cec5SDimitry Andric              cl::desc("Enable optimizations on complex GEPs"),
810b57cec5SDimitry Andric              cl::init(true));
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric static cl::opt<bool>
840b57cec5SDimitry Andric EnablePrefetch("enable-ppc-prefetching",
85480093f4SDimitry Andric                   cl::desc("enable software prefetching on PPC"),
860b57cec5SDimitry Andric                   cl::init(false), cl::Hidden);
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric static cl::opt<bool>
890b57cec5SDimitry Andric EnableExtraTOCRegDeps("enable-ppc-extra-toc-reg-deps",
900b57cec5SDimitry Andric                       cl::desc("Add extra TOC register dependencies"),
910b57cec5SDimitry Andric                       cl::init(true), cl::Hidden);
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric static cl::opt<bool>
940b57cec5SDimitry Andric EnableMachineCombinerPass("ppc-machine-combiner",
950b57cec5SDimitry Andric                           cl::desc("Enable the machine combiner pass"),
960b57cec5SDimitry Andric                           cl::init(true), cl::Hidden);
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric static cl::opt<bool>
990b57cec5SDimitry Andric   ReduceCRLogical("ppc-reduce-cr-logicals",
1000b57cec5SDimitry Andric                   cl::desc("Expand eligible cr-logical binary ops to branches"),
1018bcb0991SDimitry Andric                   cl::init(true), cl::Hidden);
10281ad6265SDimitry Andric 
10381ad6265SDimitry Andric static cl::opt<bool> EnablePPCGenScalarMASSEntries(
10481ad6265SDimitry Andric     "enable-ppc-gen-scalar-mass", cl::init(false),
10581ad6265SDimitry Andric     cl::desc("Enable lowering math functions to their corresponding MASS "
10681ad6265SDimitry Andric              "(scalar) entries"),
10781ad6265SDimitry Andric     cl::Hidden);
10881ad6265SDimitry Andric 
109480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCTarget() {
1100b57cec5SDimitry Andric   // Register the targets
1110b57cec5SDimitry Andric   RegisterTargetMachine<PPCTargetMachine> A(getThePPC32Target());
112e8d8bef9SDimitry Andric   RegisterTargetMachine<PPCTargetMachine> B(getThePPC32LETarget());
113e8d8bef9SDimitry Andric   RegisterTargetMachine<PPCTargetMachine> C(getThePPC64Target());
114e8d8bef9SDimitry Andric   RegisterTargetMachine<PPCTargetMachine> D(getThePPC64LETarget());
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   PassRegistry &PR = *PassRegistry::getPassRegistry();
1170b57cec5SDimitry Andric #ifndef NDEBUG
1180b57cec5SDimitry Andric   initializePPCCTRLoopsVerifyPass(PR);
1190b57cec5SDimitry Andric #endif
120480093f4SDimitry Andric   initializePPCLoopInstrFormPrepPass(PR);
1210b57cec5SDimitry Andric   initializePPCTOCRegDepsPass(PR);
1220b57cec5SDimitry Andric   initializePPCEarlyReturnPass(PR);
1230b57cec5SDimitry Andric   initializePPCVSXCopyPass(PR);
1240b57cec5SDimitry Andric   initializePPCVSXFMAMutatePass(PR);
1250b57cec5SDimitry Andric   initializePPCVSXSwapRemovalPass(PR);
1260b57cec5SDimitry Andric   initializePPCReduceCRLogicalsPass(PR);
1270b57cec5SDimitry Andric   initializePPCBSelPass(PR);
1280b57cec5SDimitry Andric   initializePPCBranchCoalescingPass(PR);
1290b57cec5SDimitry Andric   initializePPCBoolRetToIntPass(PR);
1300b57cec5SDimitry Andric   initializePPCExpandISELPass(PR);
1310b57cec5SDimitry Andric   initializePPCPreEmitPeepholePass(PR);
1320b57cec5SDimitry Andric   initializePPCTLSDynamicCallPass(PR);
1330b57cec5SDimitry Andric   initializePPCMIPeepholePass(PR);
134480093f4SDimitry Andric   initializePPCLowerMASSVEntriesPass(PR);
13581ad6265SDimitry Andric   initializePPCGenScalarMASSEntriesPass(PR);
136fe6060f1SDimitry Andric   initializePPCExpandAtomicPseudoPass(PR);
137e8d8bef9SDimitry Andric   initializeGlobalISel(PR);
13881ad6265SDimitry Andric   initializePPCCTRLoopsPass(PR);
139bdd1243dSDimitry Andric   initializePPCDAGToDAGISelPass(PR);
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric 
142fe6060f1SDimitry Andric static bool isLittleEndianTriple(const Triple &T) {
143fe6060f1SDimitry Andric   return T.getArch() == Triple::ppc64le || T.getArch() == Triple::ppcle;
144fe6060f1SDimitry Andric }
145fe6060f1SDimitry Andric 
1460b57cec5SDimitry Andric /// Return the datalayout string of a subtarget.
1470b57cec5SDimitry Andric static std::string getDataLayoutString(const Triple &T) {
1480b57cec5SDimitry Andric   bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
1490b57cec5SDimitry Andric   std::string Ret;
1500b57cec5SDimitry Andric 
151e8d8bef9SDimitry Andric   // Most PPC* platforms are big endian, PPC(64)LE is little endian.
152fe6060f1SDimitry Andric   if (isLittleEndianTriple(T))
1530b57cec5SDimitry Andric     Ret = "e";
1540b57cec5SDimitry Andric   else
1550b57cec5SDimitry Andric     Ret = "E";
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   Ret += DataLayout::getManglingComponent(T);
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
1600b57cec5SDimitry Andric   // pointers.
1610b57cec5SDimitry Andric   if (!is64Bit || T.getOS() == Triple::Lv2)
1620b57cec5SDimitry Andric     Ret += "-p:32:32";
1630b57cec5SDimitry Andric 
164*06c3fb27SDimitry Andric   // If the target ABI uses function descriptors, then the alignment of function
165*06c3fb27SDimitry Andric   // pointers depends on the alignment used to emit the descriptor. Otherwise,
166*06c3fb27SDimitry Andric   // function pointers are aligned to 32 bits because the instructions must be.
167*06c3fb27SDimitry Andric   if ((T.getArch() == Triple::ppc64 && !T.isPPC64ELFv2ABI())) {
168*06c3fb27SDimitry Andric     Ret += "-Fi64";
169*06c3fb27SDimitry Andric   } else if (T.isOSAIX()) {
170*06c3fb27SDimitry Andric     Ret += is64Bit ? "-Fi64" : "-Fi32";
171*06c3fb27SDimitry Andric   } else {
172*06c3fb27SDimitry Andric     Ret += "-Fn32";
173*06c3fb27SDimitry Andric   }
174*06c3fb27SDimitry Andric 
1750b57cec5SDimitry Andric   // Note, the alignment values for f64 and i64 on ppc64 in Darwin
1760b57cec5SDimitry Andric   // documentation are wrong; these are correct (i.e. "what gcc does").
1770b57cec5SDimitry Andric   Ret += "-i64:64";
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
1800b57cec5SDimitry Andric   if (is64Bit)
1810b57cec5SDimitry Andric     Ret += "-n32:64";
1820b57cec5SDimitry Andric   else
1830b57cec5SDimitry Andric     Ret += "-n32";
1840b57cec5SDimitry Andric 
185e8d8bef9SDimitry Andric   // Specify the vector alignment explicitly. For v256i1 and v512i1, the
186e8d8bef9SDimitry Andric   // calculated alignment would be 256*alignment(i1) and 512*alignment(i1),
187e8d8bef9SDimitry Andric   // which is 256 and 512 bytes - way over aligned.
188fe6060f1SDimitry Andric   if (is64Bit && (T.isOSAIX() || T.isOSLinux()))
189fe6060f1SDimitry Andric     Ret += "-S128-v256:256:256-v512:512:512";
190e8d8bef9SDimitry Andric 
1910b57cec5SDimitry Andric   return Ret;
1920b57cec5SDimitry Andric }
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric static std::string computeFSAdditions(StringRef FS, CodeGenOpt::Level OL,
1950b57cec5SDimitry Andric                                       const Triple &TT) {
1965ffd83dbSDimitry Andric   std::string FullFS = std::string(FS);
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric   // Make sure 64-bit features are available when CPUname is generic
1990b57cec5SDimitry Andric   if (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le) {
2000b57cec5SDimitry Andric     if (!FullFS.empty())
2010b57cec5SDimitry Andric       FullFS = "+64bit," + FullFS;
2020b57cec5SDimitry Andric     else
2030b57cec5SDimitry Andric       FullFS = "+64bit";
2040b57cec5SDimitry Andric   }
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric   if (OL >= CodeGenOpt::Default) {
2070b57cec5SDimitry Andric     if (!FullFS.empty())
2080b57cec5SDimitry Andric       FullFS = "+crbits," + FullFS;
2090b57cec5SDimitry Andric     else
2100b57cec5SDimitry Andric       FullFS = "+crbits";
2110b57cec5SDimitry Andric   }
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   if (OL != CodeGenOpt::None) {
2140b57cec5SDimitry Andric     if (!FullFS.empty())
2150b57cec5SDimitry Andric       FullFS = "+invariant-function-descriptors," + FullFS;
2160b57cec5SDimitry Andric     else
2170b57cec5SDimitry Andric       FullFS = "+invariant-function-descriptors";
2180b57cec5SDimitry Andric   }
2190b57cec5SDimitry Andric 
220e8d8bef9SDimitry Andric   if (TT.isOSAIX()) {
221e8d8bef9SDimitry Andric     if (!FullFS.empty())
222e8d8bef9SDimitry Andric       FullFS = "+aix," + FullFS;
223e8d8bef9SDimitry Andric     else
224e8d8bef9SDimitry Andric       FullFS = "+aix";
225e8d8bef9SDimitry Andric   }
226e8d8bef9SDimitry Andric 
2270b57cec5SDimitry Andric   return FullFS;
2280b57cec5SDimitry Andric }
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
2318bcb0991SDimitry Andric   if (TT.isOSAIX())
2328bcb0991SDimitry Andric     return std::make_unique<TargetLoweringObjectFileXCOFF>();
2338bcb0991SDimitry Andric 
2348bcb0991SDimitry Andric   return std::make_unique<PPC64LinuxTargetObjectFile>();
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric static PPCTargetMachine::PPCABI computeTargetABI(const Triple &TT,
2380b57cec5SDimitry Andric                                                  const TargetOptions &Options) {
2390b57cec5SDimitry Andric   if (Options.MCOptions.getABIName().startswith("elfv1"))
2400b57cec5SDimitry Andric     return PPCTargetMachine::PPC_ABI_ELFv1;
2410b57cec5SDimitry Andric   else if (Options.MCOptions.getABIName().startswith("elfv2"))
2420b57cec5SDimitry Andric     return PPCTargetMachine::PPC_ABI_ELFv2;
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric   assert(Options.MCOptions.getABIName().empty() &&
2450b57cec5SDimitry Andric          "Unknown target-abi option!");
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric   switch (TT.getArch()) {
2480b57cec5SDimitry Andric   case Triple::ppc64le:
2490b57cec5SDimitry Andric     return PPCTargetMachine::PPC_ABI_ELFv2;
2500b57cec5SDimitry Andric   case Triple::ppc64:
2511ac55f4cSDimitry Andric     if (TT.isPPC64ELFv2ABI())
252bdd1243dSDimitry Andric       return PPCTargetMachine::PPC_ABI_ELFv2;
253bdd1243dSDimitry Andric     else
2540b57cec5SDimitry Andric       return PPCTargetMachine::PPC_ABI_ELFv1;
2550b57cec5SDimitry Andric   default:
2560b57cec5SDimitry Andric     return PPCTargetMachine::PPC_ABI_UNKNOWN;
2570b57cec5SDimitry Andric   }
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric static Reloc::Model getEffectiveRelocModel(const Triple &TT,
261bdd1243dSDimitry Andric                                            std::optional<Reloc::Model> RM) {
26281ad6265SDimitry Andric   assert((!TT.isOSAIX() || !RM || *RM == Reloc::PIC_) &&
2635ffd83dbSDimitry Andric          "Invalid relocation model for AIX.");
2645ffd83dbSDimitry Andric 
26581ad6265SDimitry Andric   if (RM)
2660b57cec5SDimitry Andric     return *RM;
2670b57cec5SDimitry Andric 
2685ffd83dbSDimitry Andric   // Big Endian PPC and AIX default to PIC.
2695ffd83dbSDimitry Andric   if (TT.getArch() == Triple::ppc64 || TT.isOSAIX())
2700b57cec5SDimitry Andric     return Reloc::PIC_;
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric   // Rest are static by default.
2730b57cec5SDimitry Andric   return Reloc::Static;
2740b57cec5SDimitry Andric }
2750b57cec5SDimitry Andric 
276bdd1243dSDimitry Andric static CodeModel::Model
277bdd1243dSDimitry Andric getEffectivePPCCodeModel(const Triple &TT, std::optional<CodeModel::Model> CM,
2780b57cec5SDimitry Andric                          bool JIT) {
2790b57cec5SDimitry Andric   if (CM) {
2800b57cec5SDimitry Andric     if (*CM == CodeModel::Tiny)
2810b57cec5SDimitry Andric       report_fatal_error("Target does not support the tiny CodeModel", false);
2820b57cec5SDimitry Andric     if (*CM == CodeModel::Kernel)
2830b57cec5SDimitry Andric       report_fatal_error("Target does not support the kernel CodeModel", false);
2840b57cec5SDimitry Andric     return *CM;
2850b57cec5SDimitry Andric   }
2868bcb0991SDimitry Andric 
2878bcb0991SDimitry Andric   if (JIT)
2880b57cec5SDimitry Andric     return CodeModel::Small;
2898bcb0991SDimitry Andric   if (TT.isOSAIX())
2908bcb0991SDimitry Andric     return CodeModel::Small;
2918bcb0991SDimitry Andric 
2928bcb0991SDimitry Andric   assert(TT.isOSBinFormatELF() && "All remaining PPC OSes are ELF based.");
2938bcb0991SDimitry Andric 
2948bcb0991SDimitry Andric   if (TT.isArch32Bit())
2958bcb0991SDimitry Andric     return CodeModel::Small;
2968bcb0991SDimitry Andric 
2978bcb0991SDimitry Andric   assert(TT.isArch64Bit() && "Unsupported PPC architecture.");
2988bcb0991SDimitry Andric   return CodeModel::Medium;
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric static ScheduleDAGInstrs *createPPCMachineScheduler(MachineSchedContext *C) {
3030b57cec5SDimitry Andric   const PPCSubtarget &ST = C->MF->getSubtarget<PPCSubtarget>();
3040b57cec5SDimitry Andric   ScheduleDAGMILive *DAG =
3050b57cec5SDimitry Andric     new ScheduleDAGMILive(C, ST.usePPCPreRASchedStrategy() ?
3068bcb0991SDimitry Andric                           std::make_unique<PPCPreRASchedStrategy>(C) :
3078bcb0991SDimitry Andric                           std::make_unique<GenericScheduler>(C));
3080b57cec5SDimitry Andric   // add DAG Mutations here.
3090b57cec5SDimitry Andric   DAG->addMutation(createCopyConstrainDAGMutation(DAG->TII, DAG->TRI));
310e8d8bef9SDimitry Andric   if (ST.hasStoreFusion())
311e8d8bef9SDimitry Andric     DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
3125ffd83dbSDimitry Andric   if (ST.hasFusion())
3135ffd83dbSDimitry Andric     DAG->addMutation(createPowerPCMacroFusionDAGMutation());
3145ffd83dbSDimitry Andric 
3150b57cec5SDimitry Andric   return DAG;
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric static ScheduleDAGInstrs *createPPCPostMachineScheduler(
3190b57cec5SDimitry Andric   MachineSchedContext *C) {
3200b57cec5SDimitry Andric   const PPCSubtarget &ST = C->MF->getSubtarget<PPCSubtarget>();
3210b57cec5SDimitry Andric   ScheduleDAGMI *DAG =
3220b57cec5SDimitry Andric     new ScheduleDAGMI(C, ST.usePPCPostRASchedStrategy() ?
3238bcb0991SDimitry Andric                       std::make_unique<PPCPostRASchedStrategy>(C) :
3248bcb0991SDimitry Andric                       std::make_unique<PostGenericScheduler>(C), true);
3250b57cec5SDimitry Andric   // add DAG Mutations here.
326e8d8bef9SDimitry Andric   if (ST.hasStoreFusion())
327e8d8bef9SDimitry Andric     DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
3285ffd83dbSDimitry Andric   if (ST.hasFusion())
3295ffd83dbSDimitry Andric     DAG->addMutation(createPowerPCMacroFusionDAGMutation());
3300b57cec5SDimitry Andric   return DAG;
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric // The FeatureString here is a little subtle. We are modifying the feature
3340b57cec5SDimitry Andric // string with what are (currently) non-function specific overrides as it goes
3350b57cec5SDimitry Andric // into the LLVMTargetMachine constructor and then using the stored value in the
3360b57cec5SDimitry Andric // Subtarget constructor below it.
3370b57cec5SDimitry Andric PPCTargetMachine::PPCTargetMachine(const Target &T, const Triple &TT,
3380b57cec5SDimitry Andric                                    StringRef CPU, StringRef FS,
3390b57cec5SDimitry Andric                                    const TargetOptions &Options,
340bdd1243dSDimitry Andric                                    std::optional<Reloc::Model> RM,
341bdd1243dSDimitry Andric                                    std::optional<CodeModel::Model> CM,
3420b57cec5SDimitry Andric                                    CodeGenOpt::Level OL, bool JIT)
3430b57cec5SDimitry Andric     : LLVMTargetMachine(T, getDataLayoutString(TT), TT, CPU,
3440b57cec5SDimitry Andric                         computeFSAdditions(FS, OL, TT), Options,
3450b57cec5SDimitry Andric                         getEffectiveRelocModel(TT, RM),
3460b57cec5SDimitry Andric                         getEffectivePPCCodeModel(TT, CM, JIT), OL),
3470b57cec5SDimitry Andric       TLOF(createTLOF(getTargetTriple())),
348fe6060f1SDimitry Andric       TargetABI(computeTargetABI(TT, Options)),
349fe6060f1SDimitry Andric       Endianness(isLittleEndianTriple(TT) ? Endian::LITTLE : Endian::BIG) {
3500b57cec5SDimitry Andric   initAsmInfo();
3510b57cec5SDimitry Andric }
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric PPCTargetMachine::~PPCTargetMachine() = default;
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric const PPCSubtarget *
3560b57cec5SDimitry Andric PPCTargetMachine::getSubtargetImpl(const Function &F) const {
3570b57cec5SDimitry Andric   Attribute CPUAttr = F.getFnAttribute("target-cpu");
358bdd1243dSDimitry Andric   Attribute TuneAttr = F.getFnAttribute("tune-cpu");
3590b57cec5SDimitry Andric   Attribute FSAttr = F.getFnAttribute("target-features");
3600b57cec5SDimitry Andric 
361e8d8bef9SDimitry Andric   std::string CPU =
362e8d8bef9SDimitry Andric       CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
363bdd1243dSDimitry Andric   std::string TuneCPU =
364bdd1243dSDimitry Andric       TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
365e8d8bef9SDimitry Andric   std::string FS =
366e8d8bef9SDimitry Andric       FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric   // FIXME: This is related to the code below to reset the target options,
3690b57cec5SDimitry Andric   // we need to know whether or not the soft float flag is set on the
3700b57cec5SDimitry Andric   // function before we can generate a subtarget. We also need to use
3710b57cec5SDimitry Andric   // it as a key for the subtarget since that can be the only difference
3720b57cec5SDimitry Andric   // between two functions.
373fe6060f1SDimitry Andric   bool SoftFloat = F.getFnAttribute("use-soft-float").getValueAsBool();
3740b57cec5SDimitry Andric   // If the soft float attribute is set on the function turn on the soft float
3750b57cec5SDimitry Andric   // subtarget feature.
3760b57cec5SDimitry Andric   if (SoftFloat)
3770b57cec5SDimitry Andric     FS += FS.empty() ? "-hard-float" : ",-hard-float";
3780b57cec5SDimitry Andric 
379bdd1243dSDimitry Andric   auto &I = SubtargetMap[CPU + TuneCPU + FS];
3800b57cec5SDimitry Andric   if (!I) {
3810b57cec5SDimitry Andric     // This needs to be done before we create a new subtarget since any
3820b57cec5SDimitry Andric     // creation will depend on the TM and the code generation flags on the
3830b57cec5SDimitry Andric     // function that reside in TargetOptions.
3840b57cec5SDimitry Andric     resetTargetOptions(F);
3858bcb0991SDimitry Andric     I = std::make_unique<PPCSubtarget>(
386bdd1243dSDimitry Andric         TargetTriple, CPU, TuneCPU,
3870b57cec5SDimitry Andric         // FIXME: It would be good to have the subtarget additions here
3880b57cec5SDimitry Andric         // not necessary. Anything that turns them on/off (overrides) ends
3890b57cec5SDimitry Andric         // up being put at the end of the feature string, but the defaults
3900b57cec5SDimitry Andric         // shouldn't require adding them. Fixing this means pulling Feature64Bit
3910b57cec5SDimitry Andric         // out of most of the target cpus in the .td file and making it set only
3920b57cec5SDimitry Andric         // as part of initialization via the TargetTriple.
3930b57cec5SDimitry Andric         computeFSAdditions(FS, getOptLevel(), getTargetTriple()), *this);
3940b57cec5SDimitry Andric   }
3950b57cec5SDimitry Andric   return I.get();
3960b57cec5SDimitry Andric }
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3990b57cec5SDimitry Andric // Pass Pipeline Configuration
4000b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric namespace {
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric /// PPC Code Generator Pass Configuration Options.
4050b57cec5SDimitry Andric class PPCPassConfig : public TargetPassConfig {
4060b57cec5SDimitry Andric public:
4070b57cec5SDimitry Andric   PPCPassConfig(PPCTargetMachine &TM, PassManagerBase &PM)
4080b57cec5SDimitry Andric     : TargetPassConfig(TM, PM) {
4090b57cec5SDimitry Andric     // At any optimization level above -O0 we use the Machine Scheduler and not
4100b57cec5SDimitry Andric     // the default Post RA List Scheduler.
4110b57cec5SDimitry Andric     if (TM.getOptLevel() != CodeGenOpt::None)
4120b57cec5SDimitry Andric       substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
4130b57cec5SDimitry Andric   }
4140b57cec5SDimitry Andric 
4150b57cec5SDimitry Andric   PPCTargetMachine &getPPCTargetMachine() const {
4160b57cec5SDimitry Andric     return getTM<PPCTargetMachine>();
4170b57cec5SDimitry Andric   }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric   void addIRPasses() override;
4200b57cec5SDimitry Andric   bool addPreISel() override;
4210b57cec5SDimitry Andric   bool addILPOpts() override;
4220b57cec5SDimitry Andric   bool addInstSelector() override;
4230b57cec5SDimitry Andric   void addMachineSSAOptimization() override;
4240b57cec5SDimitry Andric   void addPreRegAlloc() override;
4250b57cec5SDimitry Andric   void addPreSched2() override;
4260b57cec5SDimitry Andric   void addPreEmitPass() override;
427fe6060f1SDimitry Andric   void addPreEmitPass2() override;
428e8d8bef9SDimitry Andric   // GlobalISEL
429e8d8bef9SDimitry Andric   bool addIRTranslator() override;
430e8d8bef9SDimitry Andric   bool addLegalizeMachineIR() override;
431e8d8bef9SDimitry Andric   bool addRegBankSelect() override;
432e8d8bef9SDimitry Andric   bool addGlobalInstructionSelect() override;
433e8d8bef9SDimitry Andric 
4340b57cec5SDimitry Andric   ScheduleDAGInstrs *
4350b57cec5SDimitry Andric   createMachineScheduler(MachineSchedContext *C) const override {
4360b57cec5SDimitry Andric     return createPPCMachineScheduler(C);
4370b57cec5SDimitry Andric   }
4380b57cec5SDimitry Andric   ScheduleDAGInstrs *
4390b57cec5SDimitry Andric   createPostMachineScheduler(MachineSchedContext *C) const override {
4400b57cec5SDimitry Andric     return createPPCPostMachineScheduler(C);
4410b57cec5SDimitry Andric   }
4420b57cec5SDimitry Andric };
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric } // end anonymous namespace
4450b57cec5SDimitry Andric 
4460b57cec5SDimitry Andric TargetPassConfig *PPCTargetMachine::createPassConfig(PassManagerBase &PM) {
4470b57cec5SDimitry Andric   return new PPCPassConfig(*this, PM);
4480b57cec5SDimitry Andric }
4490b57cec5SDimitry Andric 
4500b57cec5SDimitry Andric void PPCPassConfig::addIRPasses() {
4510b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None)
4520b57cec5SDimitry Andric     addPass(createPPCBoolRetToIntPass());
4530b57cec5SDimitry Andric   addPass(createAtomicExpandPass());
4540b57cec5SDimitry Andric 
455480093f4SDimitry Andric   // Lower generic MASSV routines to PowerPC subtarget-specific entries.
456480093f4SDimitry Andric   addPass(createPPCLowerMASSVEntriesPass());
457480093f4SDimitry Andric 
45881ad6265SDimitry Andric   // Generate PowerPC target-specific entries for scalar math functions
45981ad6265SDimitry Andric   // that are available in IBM MASS (scalar) library.
46081ad6265SDimitry Andric   if (TM->getOptLevel() == CodeGenOpt::Aggressive &&
46181ad6265SDimitry Andric       EnablePPCGenScalarMASSEntries) {
46281ad6265SDimitry Andric     TM->Options.PPCGenScalarMASSEntries = EnablePPCGenScalarMASSEntries;
46381ad6265SDimitry Andric     addPass(createPPCGenScalarMASSEntriesPass());
46481ad6265SDimitry Andric   }
46581ad6265SDimitry Andric 
466e8d8bef9SDimitry Andric   // If explicitly requested, add explicit data prefetch intrinsics.
4670b57cec5SDimitry Andric   if (EnablePrefetch.getNumOccurrences() > 0)
4680b57cec5SDimitry Andric     addPass(createLoopDataPrefetchPass());
4690b57cec5SDimitry Andric 
4700b57cec5SDimitry Andric   if (TM->getOptLevel() >= CodeGenOpt::Default && EnableGEPOpt) {
4710b57cec5SDimitry Andric     // Call SeparateConstOffsetFromGEP pass to extract constants within indices
4720b57cec5SDimitry Andric     // and lower a GEP with multiple indices to either arithmetic operations or
4730b57cec5SDimitry Andric     // multiple GEPs with single index.
4740b57cec5SDimitry Andric     addPass(createSeparateConstOffsetFromGEPPass(true));
4750b57cec5SDimitry Andric     // Call EarlyCSE pass to find and remove subexpressions in the lowered
4760b57cec5SDimitry Andric     // result.
4770b57cec5SDimitry Andric     addPass(createEarlyCSEPass());
4780b57cec5SDimitry Andric     // Do loop invariant code motion in case part of the lowered result is
4790b57cec5SDimitry Andric     // invariant.
4800b57cec5SDimitry Andric     addPass(createLICMPass());
4810b57cec5SDimitry Andric   }
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric   TargetPassConfig::addIRPasses();
4840b57cec5SDimitry Andric }
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric bool PPCPassConfig::addPreISel() {
487480093f4SDimitry Andric   if (!DisableInstrFormPrep && getOptLevel() != CodeGenOpt::None)
488480093f4SDimitry Andric     addPass(createPPCLoopInstrFormPrepPass(getPPCTargetMachine()));
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric   if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
491*06c3fb27SDimitry Andric     addPass(createHardwareLoopsLegacyPass());
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric   return false;
4940b57cec5SDimitry Andric }
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric bool PPCPassConfig::addILPOpts() {
4970b57cec5SDimitry Andric   addPass(&EarlyIfConverterID);
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric   if (EnableMachineCombinerPass)
5000b57cec5SDimitry Andric     addPass(&MachineCombinerID);
5010b57cec5SDimitry Andric 
5020b57cec5SDimitry Andric   return true;
5030b57cec5SDimitry Andric }
5040b57cec5SDimitry Andric 
5050b57cec5SDimitry Andric bool PPCPassConfig::addInstSelector() {
5060b57cec5SDimitry Andric   // Install an instruction selector.
5070b57cec5SDimitry Andric   addPass(createPPCISelDag(getPPCTargetMachine(), getOptLevel()));
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric #ifndef NDEBUG
5100b57cec5SDimitry Andric   if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
5110b57cec5SDimitry Andric     addPass(createPPCCTRLoopsVerify());
5120b57cec5SDimitry Andric #endif
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric   addPass(createPPCVSXCopyPass());
5150b57cec5SDimitry Andric   return false;
5160b57cec5SDimitry Andric }
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric void PPCPassConfig::addMachineSSAOptimization() {
519bdd1243dSDimitry Andric   // Run CTR loops pass before any cfg modification pass to prevent the
520bdd1243dSDimitry Andric   // canonical form of hardware loop from being destroied.
521bdd1243dSDimitry Andric   if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
522bdd1243dSDimitry Andric     addPass(createPPCCTRLoopsPass());
523bdd1243dSDimitry Andric 
5240b57cec5SDimitry Andric   // PPCBranchCoalescingPass need to be done before machine sinking
5250b57cec5SDimitry Andric   // since it merges empty blocks.
5260b57cec5SDimitry Andric   if (EnableBranchCoalescing && getOptLevel() != CodeGenOpt::None)
5270b57cec5SDimitry Andric     addPass(createPPCBranchCoalescingPass());
5280b57cec5SDimitry Andric   TargetPassConfig::addMachineSSAOptimization();
5290b57cec5SDimitry Andric   // For little endian, remove where possible the vector swap instructions
5300b57cec5SDimitry Andric   // introduced at code generation to normalize vector element order.
5310b57cec5SDimitry Andric   if (TM->getTargetTriple().getArch() == Triple::ppc64le &&
5320b57cec5SDimitry Andric       !DisableVSXSwapRemoval)
5330b57cec5SDimitry Andric     addPass(createPPCVSXSwapRemovalPass());
5340b57cec5SDimitry Andric   // Reduce the number of cr-logical ops.
5350b57cec5SDimitry Andric   if (ReduceCRLogical && getOptLevel() != CodeGenOpt::None)
5360b57cec5SDimitry Andric     addPass(createPPCReduceCRLogicalsPass());
5370b57cec5SDimitry Andric   // Target-specific peephole cleanups performed after instruction
5380b57cec5SDimitry Andric   // selection.
5390b57cec5SDimitry Andric   if (!DisableMIPeephole) {
5400b57cec5SDimitry Andric     addPass(createPPCMIPeepholePass());
5410b57cec5SDimitry Andric     addPass(&DeadMachineInstructionElimID);
5420b57cec5SDimitry Andric   }
5430b57cec5SDimitry Andric }
5440b57cec5SDimitry Andric 
5450b57cec5SDimitry Andric void PPCPassConfig::addPreRegAlloc() {
5460b57cec5SDimitry Andric   if (getOptLevel() != CodeGenOpt::None) {
5470b57cec5SDimitry Andric     initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
5480b57cec5SDimitry Andric     insertPass(VSXFMAMutateEarly ? &RegisterCoalescerID : &MachineSchedulerID,
5490b57cec5SDimitry Andric                &PPCVSXFMAMutateID);
5500b57cec5SDimitry Andric   }
5510b57cec5SDimitry Andric 
5520b57cec5SDimitry Andric   // FIXME: We probably don't need to run these for -fPIE.
5530b57cec5SDimitry Andric   if (getPPCTargetMachine().isPositionIndependent()) {
5540b57cec5SDimitry Andric     // FIXME: LiveVariables should not be necessary here!
5550b57cec5SDimitry Andric     // PPCTLSDynamicCallPass uses LiveIntervals which previously dependent on
5560b57cec5SDimitry Andric     // LiveVariables. This (unnecessary) dependency has been removed now,
5570b57cec5SDimitry Andric     // however a stage-2 clang build fails without LiveVariables computed here.
5585ffd83dbSDimitry Andric     addPass(&LiveVariablesID);
5590b57cec5SDimitry Andric     addPass(createPPCTLSDynamicCallPass());
5600b57cec5SDimitry Andric   }
5610b57cec5SDimitry Andric   if (EnableExtraTOCRegDeps)
5620b57cec5SDimitry Andric     addPass(createPPCTOCRegDepsPass());
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
5650b57cec5SDimitry Andric     addPass(&MachinePipelinerID);
5660b57cec5SDimitry Andric }
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric void PPCPassConfig::addPreSched2() {
569e8d8bef9SDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
5700b57cec5SDimitry Andric     addPass(&IfConverterID);
5710b57cec5SDimitry Andric }
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric void PPCPassConfig::addPreEmitPass() {
5740b57cec5SDimitry Andric   addPass(createPPCPreEmitPeepholePass());
5750b57cec5SDimitry Andric   addPass(createPPCExpandISELPass());
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
5785ffd83dbSDimitry Andric     addPass(createPPCEarlyReturnPass());
579fe6060f1SDimitry Andric }
580fe6060f1SDimitry Andric 
581fe6060f1SDimitry Andric void PPCPassConfig::addPreEmitPass2() {
582fe6060f1SDimitry Andric   // Schedule the expansion of AMOs at the last possible moment, avoiding the
583fe6060f1SDimitry Andric   // possibility for other passes to break the requirements for forward
584fe6060f1SDimitry Andric   // progress in the LL/SC block.
585fe6060f1SDimitry Andric   addPass(createPPCExpandAtomicPseudoPass());
5860b57cec5SDimitry Andric   // Must run branch selection immediately preceding the asm printer.
5875ffd83dbSDimitry Andric   addPass(createPPCBranchSelectionPass());
5880b57cec5SDimitry Andric }
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric TargetTransformInfo
59181ad6265SDimitry Andric PPCTargetMachine::getTargetTransformInfo(const Function &F) const {
5920b57cec5SDimitry Andric   return TargetTransformInfo(PPCTTIImpl(this, F));
5930b57cec5SDimitry Andric }
5940b57cec5SDimitry Andric 
595fe6060f1SDimitry Andric bool PPCTargetMachine::isLittleEndian() const {
596fe6060f1SDimitry Andric   assert(Endianness != Endian::NOT_DETECTED &&
597fe6060f1SDimitry Andric          "Unable to determine endianness");
598fe6060f1SDimitry Andric   return Endianness == Endian::LITTLE;
599fe6060f1SDimitry Andric }
600fe6060f1SDimitry Andric 
601bdd1243dSDimitry Andric MachineFunctionInfo *PPCTargetMachine::createMachineFunctionInfo(
602bdd1243dSDimitry Andric     BumpPtrAllocator &Allocator, const Function &F,
603bdd1243dSDimitry Andric     const TargetSubtargetInfo *STI) const {
604bdd1243dSDimitry Andric   return PPCFunctionInfo::create<PPCFunctionInfo>(Allocator, F, STI);
605bdd1243dSDimitry Andric }
606bdd1243dSDimitry Andric 
6070b57cec5SDimitry Andric static MachineSchedRegistry
6080b57cec5SDimitry Andric PPCPreRASchedRegistry("ppc-prera",
6090b57cec5SDimitry Andric                       "Run PowerPC PreRA specific scheduler",
6100b57cec5SDimitry Andric                       createPPCMachineScheduler);
6110b57cec5SDimitry Andric 
6120b57cec5SDimitry Andric static MachineSchedRegistry
6130b57cec5SDimitry Andric PPCPostRASchedRegistry("ppc-postra",
6140b57cec5SDimitry Andric                        "Run PowerPC PostRA specific scheduler",
6150b57cec5SDimitry Andric                        createPPCPostMachineScheduler);
616e8d8bef9SDimitry Andric 
617e8d8bef9SDimitry Andric // Global ISEL
618e8d8bef9SDimitry Andric bool PPCPassConfig::addIRTranslator() {
619e8d8bef9SDimitry Andric   addPass(new IRTranslator());
620e8d8bef9SDimitry Andric   return false;
621e8d8bef9SDimitry Andric }
622e8d8bef9SDimitry Andric 
623e8d8bef9SDimitry Andric bool PPCPassConfig::addLegalizeMachineIR() {
624e8d8bef9SDimitry Andric   addPass(new Legalizer());
625e8d8bef9SDimitry Andric   return false;
626e8d8bef9SDimitry Andric }
627e8d8bef9SDimitry Andric 
628e8d8bef9SDimitry Andric bool PPCPassConfig::addRegBankSelect() {
629e8d8bef9SDimitry Andric   addPass(new RegBankSelect());
630e8d8bef9SDimitry Andric   return false;
631e8d8bef9SDimitry Andric }
632e8d8bef9SDimitry Andric 
633e8d8bef9SDimitry Andric bool PPCPassConfig::addGlobalInstructionSelect() {
634fe6060f1SDimitry Andric   addPass(new InstructionSelect(getOptLevel()));
635e8d8bef9SDimitry Andric   return false;
636e8d8bef9SDimitry Andric }
637