xref: /freebsd/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
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"
160b57cec5SDimitry Andric #include "PPCMachineScheduler.h"
175ffd83dbSDimitry Andric #include "PPCMacroFusion.h"
180b57cec5SDimitry Andric #include "PPCSubtarget.h"
190b57cec5SDimitry Andric #include "PPCTargetObjectFile.h"
200b57cec5SDimitry Andric #include "PPCTargetTransformInfo.h"
210b57cec5SDimitry Andric #include "TargetInfo/PowerPCTargetInfo.h"
220b57cec5SDimitry Andric #include "llvm/ADT/Optional.h"
230b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
240b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
250b57cec5SDimitry Andric #include "llvm/ADT/Triple.h"
260b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
27*e8d8bef9SDimitry Andric #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
28*e8d8bef9SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
29*e8d8bef9SDimitry Andric #include "llvm/CodeGen/GlobalISel/Legalizer.h"
30*e8d8bef9SDimitry Andric #include "llvm/CodeGen/GlobalISel/Localizer.h"
31*e8d8bef9SDimitry Andric #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
32*e8d8bef9SDimitry 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"
38*e8d8bef9SDimitry Andric #include "llvm/InitializePasses.h"
390b57cec5SDimitry Andric #include "llvm/Pass.h"
400b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
410b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
420b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h"
430b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
440b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
450b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h"
460b57cec5SDimitry Andric #include <cassert>
470b57cec5SDimitry Andric #include <memory>
480b57cec5SDimitry Andric #include <string>
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric using namespace llvm;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric static cl::opt<bool>
540b57cec5SDimitry Andric     EnableBranchCoalescing("enable-ppc-branch-coalesce", cl::Hidden,
550b57cec5SDimitry Andric                            cl::desc("enable coalescing of duplicate branches for PPC"));
560b57cec5SDimitry Andric static cl::
570b57cec5SDimitry Andric opt<bool> DisableCTRLoops("disable-ppc-ctrloops", cl::Hidden,
580b57cec5SDimitry Andric                         cl::desc("Disable CTR loops for PPC"));
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric static cl::
61480093f4SDimitry Andric opt<bool> DisableInstrFormPrep("disable-ppc-instr-form-prep", cl::Hidden,
62480093f4SDimitry Andric                             cl::desc("Disable PPC loop instr form prep"));
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric static cl::opt<bool>
650b57cec5SDimitry Andric VSXFMAMutateEarly("schedule-ppc-vsx-fma-mutation-early",
660b57cec5SDimitry Andric   cl::Hidden, cl::desc("Schedule VSX FMA instruction mutation early"));
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric static cl::
690b57cec5SDimitry Andric opt<bool> DisableVSXSwapRemoval("disable-ppc-vsx-swap-removal", cl::Hidden,
700b57cec5SDimitry Andric                                 cl::desc("Disable VSX Swap Removal for PPC"));
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric static cl::
730b57cec5SDimitry Andric opt<bool> DisableMIPeephole("disable-ppc-peephole", cl::Hidden,
740b57cec5SDimitry Andric                             cl::desc("Disable machine peepholes for PPC"));
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric static cl::opt<bool>
770b57cec5SDimitry Andric EnableGEPOpt("ppc-gep-opt", cl::Hidden,
780b57cec5SDimitry Andric              cl::desc("Enable optimizations on complex GEPs"),
790b57cec5SDimitry Andric              cl::init(true));
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric static cl::opt<bool>
820b57cec5SDimitry Andric EnablePrefetch("enable-ppc-prefetching",
83480093f4SDimitry Andric                   cl::desc("enable software prefetching on PPC"),
840b57cec5SDimitry Andric                   cl::init(false), cl::Hidden);
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric static cl::opt<bool>
870b57cec5SDimitry Andric EnableExtraTOCRegDeps("enable-ppc-extra-toc-reg-deps",
880b57cec5SDimitry Andric                       cl::desc("Add extra TOC register dependencies"),
890b57cec5SDimitry Andric                       cl::init(true), cl::Hidden);
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric static cl::opt<bool>
920b57cec5SDimitry Andric EnableMachineCombinerPass("ppc-machine-combiner",
930b57cec5SDimitry Andric                           cl::desc("Enable the machine combiner pass"),
940b57cec5SDimitry Andric                           cl::init(true), cl::Hidden);
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric static cl::opt<bool>
970b57cec5SDimitry Andric   ReduceCRLogical("ppc-reduce-cr-logicals",
980b57cec5SDimitry Andric                   cl::desc("Expand eligible cr-logical binary ops to branches"),
998bcb0991SDimitry Andric                   cl::init(true), cl::Hidden);
100480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCTarget() {
1010b57cec5SDimitry Andric   // Register the targets
1020b57cec5SDimitry Andric   RegisterTargetMachine<PPCTargetMachine> A(getThePPC32Target());
103*e8d8bef9SDimitry Andric   RegisterTargetMachine<PPCTargetMachine> B(getThePPC32LETarget());
104*e8d8bef9SDimitry Andric   RegisterTargetMachine<PPCTargetMachine> C(getThePPC64Target());
105*e8d8bef9SDimitry Andric   RegisterTargetMachine<PPCTargetMachine> D(getThePPC64LETarget());
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   PassRegistry &PR = *PassRegistry::getPassRegistry();
1080b57cec5SDimitry Andric #ifndef NDEBUG
1090b57cec5SDimitry Andric   initializePPCCTRLoopsVerifyPass(PR);
1100b57cec5SDimitry Andric #endif
111480093f4SDimitry Andric   initializePPCLoopInstrFormPrepPass(PR);
1120b57cec5SDimitry Andric   initializePPCTOCRegDepsPass(PR);
1130b57cec5SDimitry Andric   initializePPCEarlyReturnPass(PR);
1140b57cec5SDimitry Andric   initializePPCVSXCopyPass(PR);
1150b57cec5SDimitry Andric   initializePPCVSXFMAMutatePass(PR);
1160b57cec5SDimitry Andric   initializePPCVSXSwapRemovalPass(PR);
1170b57cec5SDimitry Andric   initializePPCReduceCRLogicalsPass(PR);
1180b57cec5SDimitry Andric   initializePPCBSelPass(PR);
1190b57cec5SDimitry Andric   initializePPCBranchCoalescingPass(PR);
1200b57cec5SDimitry Andric   initializePPCBoolRetToIntPass(PR);
1210b57cec5SDimitry Andric   initializePPCExpandISELPass(PR);
1220b57cec5SDimitry Andric   initializePPCPreEmitPeepholePass(PR);
1230b57cec5SDimitry Andric   initializePPCTLSDynamicCallPass(PR);
1240b57cec5SDimitry Andric   initializePPCMIPeepholePass(PR);
125480093f4SDimitry Andric   initializePPCLowerMASSVEntriesPass(PR);
126*e8d8bef9SDimitry Andric   initializeGlobalISel(PR);
1270b57cec5SDimitry Andric }
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric /// Return the datalayout string of a subtarget.
1300b57cec5SDimitry Andric static std::string getDataLayoutString(const Triple &T) {
1310b57cec5SDimitry Andric   bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
1320b57cec5SDimitry Andric   std::string Ret;
1330b57cec5SDimitry Andric 
134*e8d8bef9SDimitry Andric   // Most PPC* platforms are big endian, PPC(64)LE is little endian.
135*e8d8bef9SDimitry Andric   if (T.getArch() == Triple::ppc64le || T.getArch() == Triple::ppcle)
1360b57cec5SDimitry Andric     Ret = "e";
1370b57cec5SDimitry Andric   else
1380b57cec5SDimitry Andric     Ret = "E";
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   Ret += DataLayout::getManglingComponent(T);
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
1430b57cec5SDimitry Andric   // pointers.
1440b57cec5SDimitry Andric   if (!is64Bit || T.getOS() == Triple::Lv2)
1450b57cec5SDimitry Andric     Ret += "-p:32:32";
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric   // Note, the alignment values for f64 and i64 on ppc64 in Darwin
1480b57cec5SDimitry Andric   // documentation are wrong; these are correct (i.e. "what gcc does").
1490b57cec5SDimitry Andric   Ret += "-i64:64";
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
1520b57cec5SDimitry Andric   if (is64Bit)
1530b57cec5SDimitry Andric     Ret += "-n32:64";
1540b57cec5SDimitry Andric   else
1550b57cec5SDimitry Andric     Ret += "-n32";
1560b57cec5SDimitry Andric 
157*e8d8bef9SDimitry Andric   // Specify the vector alignment explicitly. For v256i1 and v512i1, the
158*e8d8bef9SDimitry Andric   // calculated alignment would be 256*alignment(i1) and 512*alignment(i1),
159*e8d8bef9SDimitry Andric   // which is 256 and 512 bytes - way over aligned.
160*e8d8bef9SDimitry Andric   if ((T.getArch() == Triple::ppc64le || T.getArch() == Triple::ppc64) &&
161*e8d8bef9SDimitry Andric       (T.isOSAIX() || T.isOSLinux()))
162*e8d8bef9SDimitry Andric     Ret += "-v256:256:256-v512:512:512";
163*e8d8bef9SDimitry Andric 
1640b57cec5SDimitry Andric   return Ret;
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric static std::string computeFSAdditions(StringRef FS, CodeGenOpt::Level OL,
1680b57cec5SDimitry Andric                                       const Triple &TT) {
1695ffd83dbSDimitry Andric   std::string FullFS = std::string(FS);
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   // Make sure 64-bit features are available when CPUname is generic
1720b57cec5SDimitry Andric   if (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le) {
1730b57cec5SDimitry Andric     if (!FullFS.empty())
1740b57cec5SDimitry Andric       FullFS = "+64bit," + FullFS;
1750b57cec5SDimitry Andric     else
1760b57cec5SDimitry Andric       FullFS = "+64bit";
1770b57cec5SDimitry Andric   }
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   if (OL >= CodeGenOpt::Default) {
1800b57cec5SDimitry Andric     if (!FullFS.empty())
1810b57cec5SDimitry Andric       FullFS = "+crbits," + FullFS;
1820b57cec5SDimitry Andric     else
1830b57cec5SDimitry Andric       FullFS = "+crbits";
1840b57cec5SDimitry Andric   }
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric   if (OL != CodeGenOpt::None) {
1870b57cec5SDimitry Andric     if (!FullFS.empty())
1880b57cec5SDimitry Andric       FullFS = "+invariant-function-descriptors," + FullFS;
1890b57cec5SDimitry Andric     else
1900b57cec5SDimitry Andric       FullFS = "+invariant-function-descriptors";
1910b57cec5SDimitry Andric   }
1920b57cec5SDimitry Andric 
193*e8d8bef9SDimitry Andric   if (TT.isOSAIX()) {
194*e8d8bef9SDimitry Andric     if (!FullFS.empty())
195*e8d8bef9SDimitry Andric       FullFS = "+aix," + FullFS;
196*e8d8bef9SDimitry Andric     else
197*e8d8bef9SDimitry Andric       FullFS = "+aix";
198*e8d8bef9SDimitry Andric   }
199*e8d8bef9SDimitry Andric 
2000b57cec5SDimitry Andric   return FullFS;
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
2048bcb0991SDimitry Andric   if (TT.isOSAIX())
2058bcb0991SDimitry Andric     return std::make_unique<TargetLoweringObjectFileXCOFF>();
2068bcb0991SDimitry Andric 
2078bcb0991SDimitry Andric   return std::make_unique<PPC64LinuxTargetObjectFile>();
2080b57cec5SDimitry Andric }
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric static PPCTargetMachine::PPCABI computeTargetABI(const Triple &TT,
2110b57cec5SDimitry Andric                                                  const TargetOptions &Options) {
2120b57cec5SDimitry Andric   if (Options.MCOptions.getABIName().startswith("elfv1"))
2130b57cec5SDimitry Andric     return PPCTargetMachine::PPC_ABI_ELFv1;
2140b57cec5SDimitry Andric   else if (Options.MCOptions.getABIName().startswith("elfv2"))
2150b57cec5SDimitry Andric     return PPCTargetMachine::PPC_ABI_ELFv2;
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric   assert(Options.MCOptions.getABIName().empty() &&
2180b57cec5SDimitry Andric          "Unknown target-abi option!");
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric   if (TT.isMacOSX())
2210b57cec5SDimitry Andric     return PPCTargetMachine::PPC_ABI_UNKNOWN;
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric   switch (TT.getArch()) {
2240b57cec5SDimitry Andric   case Triple::ppc64le:
2250b57cec5SDimitry Andric     return PPCTargetMachine::PPC_ABI_ELFv2;
2260b57cec5SDimitry Andric   case Triple::ppc64:
2270b57cec5SDimitry Andric     return PPCTargetMachine::PPC_ABI_ELFv1;
2280b57cec5SDimitry Andric   default:
2290b57cec5SDimitry Andric     return PPCTargetMachine::PPC_ABI_UNKNOWN;
2300b57cec5SDimitry Andric   }
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric static Reloc::Model getEffectiveRelocModel(const Triple &TT,
2340b57cec5SDimitry Andric                                            Optional<Reloc::Model> RM) {
2355ffd83dbSDimitry Andric   assert((!TT.isOSAIX() || !RM.hasValue() || *RM == Reloc::PIC_) &&
2365ffd83dbSDimitry Andric          "Invalid relocation model for AIX.");
2375ffd83dbSDimitry Andric 
2380b57cec5SDimitry Andric   if (RM.hasValue())
2390b57cec5SDimitry Andric     return *RM;
2400b57cec5SDimitry Andric 
2415ffd83dbSDimitry Andric   // Big Endian PPC and AIX default to PIC.
2425ffd83dbSDimitry Andric   if (TT.getArch() == Triple::ppc64 || TT.isOSAIX())
2430b57cec5SDimitry Andric     return Reloc::PIC_;
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   // Rest are static by default.
2460b57cec5SDimitry Andric   return Reloc::Static;
2470b57cec5SDimitry Andric }
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric static CodeModel::Model getEffectivePPCCodeModel(const Triple &TT,
2500b57cec5SDimitry Andric                                                  Optional<CodeModel::Model> CM,
2510b57cec5SDimitry Andric                                                  bool JIT) {
2520b57cec5SDimitry Andric   if (CM) {
2530b57cec5SDimitry Andric     if (*CM == CodeModel::Tiny)
2540b57cec5SDimitry Andric       report_fatal_error("Target does not support the tiny CodeModel", false);
2550b57cec5SDimitry Andric     if (*CM == CodeModel::Kernel)
2560b57cec5SDimitry Andric       report_fatal_error("Target does not support the kernel CodeModel", false);
2570b57cec5SDimitry Andric     return *CM;
2580b57cec5SDimitry Andric   }
2598bcb0991SDimitry Andric 
2608bcb0991SDimitry Andric   if (JIT)
2610b57cec5SDimitry Andric     return CodeModel::Small;
2628bcb0991SDimitry Andric   if (TT.isOSAIX())
2638bcb0991SDimitry Andric     return CodeModel::Small;
2648bcb0991SDimitry Andric 
2658bcb0991SDimitry Andric   assert(TT.isOSBinFormatELF() && "All remaining PPC OSes are ELF based.");
2668bcb0991SDimitry Andric 
2678bcb0991SDimitry Andric   if (TT.isArch32Bit())
2688bcb0991SDimitry Andric     return CodeModel::Small;
2698bcb0991SDimitry Andric 
2708bcb0991SDimitry Andric   assert(TT.isArch64Bit() && "Unsupported PPC architecture.");
2718bcb0991SDimitry Andric   return CodeModel::Medium;
2720b57cec5SDimitry Andric }
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric static ScheduleDAGInstrs *createPPCMachineScheduler(MachineSchedContext *C) {
2760b57cec5SDimitry Andric   const PPCSubtarget &ST = C->MF->getSubtarget<PPCSubtarget>();
2770b57cec5SDimitry Andric   ScheduleDAGMILive *DAG =
2780b57cec5SDimitry Andric     new ScheduleDAGMILive(C, ST.usePPCPreRASchedStrategy() ?
2798bcb0991SDimitry Andric                           std::make_unique<PPCPreRASchedStrategy>(C) :
2808bcb0991SDimitry Andric                           std::make_unique<GenericScheduler>(C));
2810b57cec5SDimitry Andric   // add DAG Mutations here.
2820b57cec5SDimitry Andric   DAG->addMutation(createCopyConstrainDAGMutation(DAG->TII, DAG->TRI));
283*e8d8bef9SDimitry Andric   if (ST.hasStoreFusion())
284*e8d8bef9SDimitry Andric     DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
2855ffd83dbSDimitry Andric   if (ST.hasFusion())
2865ffd83dbSDimitry Andric     DAG->addMutation(createPowerPCMacroFusionDAGMutation());
2875ffd83dbSDimitry Andric 
2880b57cec5SDimitry Andric   return DAG;
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric static ScheduleDAGInstrs *createPPCPostMachineScheduler(
2920b57cec5SDimitry Andric   MachineSchedContext *C) {
2930b57cec5SDimitry Andric   const PPCSubtarget &ST = C->MF->getSubtarget<PPCSubtarget>();
2940b57cec5SDimitry Andric   ScheduleDAGMI *DAG =
2950b57cec5SDimitry Andric     new ScheduleDAGMI(C, ST.usePPCPostRASchedStrategy() ?
2968bcb0991SDimitry Andric                       std::make_unique<PPCPostRASchedStrategy>(C) :
2978bcb0991SDimitry Andric                       std::make_unique<PostGenericScheduler>(C), true);
2980b57cec5SDimitry Andric   // add DAG Mutations here.
299*e8d8bef9SDimitry Andric   if (ST.hasStoreFusion())
300*e8d8bef9SDimitry Andric     DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
3015ffd83dbSDimitry Andric   if (ST.hasFusion())
3025ffd83dbSDimitry Andric     DAG->addMutation(createPowerPCMacroFusionDAGMutation());
3030b57cec5SDimitry Andric   return DAG;
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric // The FeatureString here is a little subtle. We are modifying the feature
3070b57cec5SDimitry Andric // string with what are (currently) non-function specific overrides as it goes
3080b57cec5SDimitry Andric // into the LLVMTargetMachine constructor and then using the stored value in the
3090b57cec5SDimitry Andric // Subtarget constructor below it.
3100b57cec5SDimitry Andric PPCTargetMachine::PPCTargetMachine(const Target &T, const Triple &TT,
3110b57cec5SDimitry Andric                                    StringRef CPU, StringRef FS,
3120b57cec5SDimitry Andric                                    const TargetOptions &Options,
3130b57cec5SDimitry Andric                                    Optional<Reloc::Model> RM,
3140b57cec5SDimitry Andric                                    Optional<CodeModel::Model> CM,
3150b57cec5SDimitry Andric                                    CodeGenOpt::Level OL, bool JIT)
3160b57cec5SDimitry Andric     : LLVMTargetMachine(T, getDataLayoutString(TT), TT, CPU,
3170b57cec5SDimitry Andric                         computeFSAdditions(FS, OL, TT), Options,
3180b57cec5SDimitry Andric                         getEffectiveRelocModel(TT, RM),
3190b57cec5SDimitry Andric                         getEffectivePPCCodeModel(TT, CM, JIT), OL),
3200b57cec5SDimitry Andric       TLOF(createTLOF(getTargetTriple())),
3210b57cec5SDimitry Andric       TargetABI(computeTargetABI(TT, Options)) {
3220b57cec5SDimitry Andric   initAsmInfo();
3230b57cec5SDimitry Andric }
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric PPCTargetMachine::~PPCTargetMachine() = default;
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric const PPCSubtarget *
3280b57cec5SDimitry Andric PPCTargetMachine::getSubtargetImpl(const Function &F) const {
3290b57cec5SDimitry Andric   Attribute CPUAttr = F.getFnAttribute("target-cpu");
3300b57cec5SDimitry Andric   Attribute FSAttr = F.getFnAttribute("target-features");
3310b57cec5SDimitry Andric 
332*e8d8bef9SDimitry Andric   std::string CPU =
333*e8d8bef9SDimitry Andric       CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
334*e8d8bef9SDimitry Andric   std::string FS =
335*e8d8bef9SDimitry Andric       FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   // FIXME: This is related to the code below to reset the target options,
3380b57cec5SDimitry Andric   // we need to know whether or not the soft float flag is set on the
3390b57cec5SDimitry Andric   // function before we can generate a subtarget. We also need to use
3400b57cec5SDimitry Andric   // it as a key for the subtarget since that can be the only difference
3410b57cec5SDimitry Andric   // between two functions.
3420b57cec5SDimitry Andric   bool SoftFloat =
3430b57cec5SDimitry Andric       F.getFnAttribute("use-soft-float").getValueAsString() == "true";
3440b57cec5SDimitry Andric   // If the soft float attribute is set on the function turn on the soft float
3450b57cec5SDimitry Andric   // subtarget feature.
3460b57cec5SDimitry Andric   if (SoftFloat)
3470b57cec5SDimitry Andric     FS += FS.empty() ? "-hard-float" : ",-hard-float";
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric   auto &I = SubtargetMap[CPU + FS];
3500b57cec5SDimitry Andric   if (!I) {
3510b57cec5SDimitry Andric     // This needs to be done before we create a new subtarget since any
3520b57cec5SDimitry Andric     // creation will depend on the TM and the code generation flags on the
3530b57cec5SDimitry Andric     // function that reside in TargetOptions.
3540b57cec5SDimitry Andric     resetTargetOptions(F);
3558bcb0991SDimitry Andric     I = std::make_unique<PPCSubtarget>(
3560b57cec5SDimitry Andric         TargetTriple, CPU,
3570b57cec5SDimitry Andric         // FIXME: It would be good to have the subtarget additions here
3580b57cec5SDimitry Andric         // not necessary. Anything that turns them on/off (overrides) ends
3590b57cec5SDimitry Andric         // up being put at the end of the feature string, but the defaults
3600b57cec5SDimitry Andric         // shouldn't require adding them. Fixing this means pulling Feature64Bit
3610b57cec5SDimitry Andric         // out of most of the target cpus in the .td file and making it set only
3620b57cec5SDimitry Andric         // as part of initialization via the TargetTriple.
3630b57cec5SDimitry Andric         computeFSAdditions(FS, getOptLevel(), getTargetTriple()), *this);
3640b57cec5SDimitry Andric   }
3650b57cec5SDimitry Andric   return I.get();
3660b57cec5SDimitry Andric }
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3690b57cec5SDimitry Andric // Pass Pipeline Configuration
3700b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric namespace {
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric /// PPC Code Generator Pass Configuration Options.
3750b57cec5SDimitry Andric class PPCPassConfig : public TargetPassConfig {
3760b57cec5SDimitry Andric public:
3770b57cec5SDimitry Andric   PPCPassConfig(PPCTargetMachine &TM, PassManagerBase &PM)
3780b57cec5SDimitry Andric     : TargetPassConfig(TM, PM) {
3790b57cec5SDimitry Andric     // At any optimization level above -O0 we use the Machine Scheduler and not
3800b57cec5SDimitry Andric     // the default Post RA List Scheduler.
3810b57cec5SDimitry Andric     if (TM.getOptLevel() != CodeGenOpt::None)
3820b57cec5SDimitry Andric       substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
3830b57cec5SDimitry Andric   }
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric   PPCTargetMachine &getPPCTargetMachine() const {
3860b57cec5SDimitry Andric     return getTM<PPCTargetMachine>();
3870b57cec5SDimitry Andric   }
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric   void addIRPasses() override;
3900b57cec5SDimitry Andric   bool addPreISel() override;
3910b57cec5SDimitry Andric   bool addILPOpts() override;
3920b57cec5SDimitry Andric   bool addInstSelector() override;
3930b57cec5SDimitry Andric   void addMachineSSAOptimization() override;
3940b57cec5SDimitry Andric   void addPreRegAlloc() override;
3950b57cec5SDimitry Andric   void addPreSched2() override;
3960b57cec5SDimitry Andric   void addPreEmitPass() override;
397*e8d8bef9SDimitry Andric   // GlobalISEL
398*e8d8bef9SDimitry Andric   bool addIRTranslator() override;
399*e8d8bef9SDimitry Andric   bool addLegalizeMachineIR() override;
400*e8d8bef9SDimitry Andric   bool addRegBankSelect() override;
401*e8d8bef9SDimitry Andric   bool addGlobalInstructionSelect() override;
402*e8d8bef9SDimitry Andric 
4030b57cec5SDimitry Andric   ScheduleDAGInstrs *
4040b57cec5SDimitry Andric   createMachineScheduler(MachineSchedContext *C) const override {
4050b57cec5SDimitry Andric     return createPPCMachineScheduler(C);
4060b57cec5SDimitry Andric   }
4070b57cec5SDimitry Andric   ScheduleDAGInstrs *
4080b57cec5SDimitry Andric   createPostMachineScheduler(MachineSchedContext *C) const override {
4090b57cec5SDimitry Andric     return createPPCPostMachineScheduler(C);
4100b57cec5SDimitry Andric   }
4110b57cec5SDimitry Andric };
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric } // end anonymous namespace
4140b57cec5SDimitry Andric 
4150b57cec5SDimitry Andric TargetPassConfig *PPCTargetMachine::createPassConfig(PassManagerBase &PM) {
4160b57cec5SDimitry Andric   return new PPCPassConfig(*this, PM);
4170b57cec5SDimitry Andric }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric void PPCPassConfig::addIRPasses() {
4200b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None)
4210b57cec5SDimitry Andric     addPass(createPPCBoolRetToIntPass());
4220b57cec5SDimitry Andric   addPass(createAtomicExpandPass());
4230b57cec5SDimitry Andric 
424480093f4SDimitry Andric   // Lower generic MASSV routines to PowerPC subtarget-specific entries.
425480093f4SDimitry Andric   addPass(createPPCLowerMASSVEntriesPass());
426480093f4SDimitry Andric 
427*e8d8bef9SDimitry Andric   // If explicitly requested, add explicit data prefetch intrinsics.
4280b57cec5SDimitry Andric   if (EnablePrefetch.getNumOccurrences() > 0)
4290b57cec5SDimitry Andric     addPass(createLoopDataPrefetchPass());
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric   if (TM->getOptLevel() >= CodeGenOpt::Default && EnableGEPOpt) {
4320b57cec5SDimitry Andric     // Call SeparateConstOffsetFromGEP pass to extract constants within indices
4330b57cec5SDimitry Andric     // and lower a GEP with multiple indices to either arithmetic operations or
4340b57cec5SDimitry Andric     // multiple GEPs with single index.
4350b57cec5SDimitry Andric     addPass(createSeparateConstOffsetFromGEPPass(true));
4360b57cec5SDimitry Andric     // Call EarlyCSE pass to find and remove subexpressions in the lowered
4370b57cec5SDimitry Andric     // result.
4380b57cec5SDimitry Andric     addPass(createEarlyCSEPass());
4390b57cec5SDimitry Andric     // Do loop invariant code motion in case part of the lowered result is
4400b57cec5SDimitry Andric     // invariant.
4410b57cec5SDimitry Andric     addPass(createLICMPass());
4420b57cec5SDimitry Andric   }
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric   TargetPassConfig::addIRPasses();
4450b57cec5SDimitry Andric }
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric bool PPCPassConfig::addPreISel() {
448480093f4SDimitry Andric   if (!DisableInstrFormPrep && getOptLevel() != CodeGenOpt::None)
449480093f4SDimitry Andric     addPass(createPPCLoopInstrFormPrepPass(getPPCTargetMachine()));
4500b57cec5SDimitry Andric 
4510b57cec5SDimitry Andric   if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
4520b57cec5SDimitry Andric     addPass(createHardwareLoopsPass());
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric   return false;
4550b57cec5SDimitry Andric }
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric bool PPCPassConfig::addILPOpts() {
4580b57cec5SDimitry Andric   addPass(&EarlyIfConverterID);
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric   if (EnableMachineCombinerPass)
4610b57cec5SDimitry Andric     addPass(&MachineCombinerID);
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric   return true;
4640b57cec5SDimitry Andric }
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric bool PPCPassConfig::addInstSelector() {
4670b57cec5SDimitry Andric   // Install an instruction selector.
4680b57cec5SDimitry Andric   addPass(createPPCISelDag(getPPCTargetMachine(), getOptLevel()));
4690b57cec5SDimitry Andric 
4700b57cec5SDimitry Andric #ifndef NDEBUG
4710b57cec5SDimitry Andric   if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
4720b57cec5SDimitry Andric     addPass(createPPCCTRLoopsVerify());
4730b57cec5SDimitry Andric #endif
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric   addPass(createPPCVSXCopyPass());
4760b57cec5SDimitry Andric   return false;
4770b57cec5SDimitry Andric }
4780b57cec5SDimitry Andric 
4790b57cec5SDimitry Andric void PPCPassConfig::addMachineSSAOptimization() {
4800b57cec5SDimitry Andric   // PPCBranchCoalescingPass need to be done before machine sinking
4810b57cec5SDimitry Andric   // since it merges empty blocks.
4820b57cec5SDimitry Andric   if (EnableBranchCoalescing && getOptLevel() != CodeGenOpt::None)
4830b57cec5SDimitry Andric     addPass(createPPCBranchCoalescingPass());
4840b57cec5SDimitry Andric   TargetPassConfig::addMachineSSAOptimization();
4850b57cec5SDimitry Andric   // For little endian, remove where possible the vector swap instructions
4860b57cec5SDimitry Andric   // introduced at code generation to normalize vector element order.
4870b57cec5SDimitry Andric   if (TM->getTargetTriple().getArch() == Triple::ppc64le &&
4880b57cec5SDimitry Andric       !DisableVSXSwapRemoval)
4890b57cec5SDimitry Andric     addPass(createPPCVSXSwapRemovalPass());
4900b57cec5SDimitry Andric   // Reduce the number of cr-logical ops.
4910b57cec5SDimitry Andric   if (ReduceCRLogical && getOptLevel() != CodeGenOpt::None)
4920b57cec5SDimitry Andric     addPass(createPPCReduceCRLogicalsPass());
4930b57cec5SDimitry Andric   // Target-specific peephole cleanups performed after instruction
4940b57cec5SDimitry Andric   // selection.
4950b57cec5SDimitry Andric   if (!DisableMIPeephole) {
4960b57cec5SDimitry Andric     addPass(createPPCMIPeepholePass());
4970b57cec5SDimitry Andric     addPass(&DeadMachineInstructionElimID);
4980b57cec5SDimitry Andric   }
4990b57cec5SDimitry Andric }
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric void PPCPassConfig::addPreRegAlloc() {
5020b57cec5SDimitry Andric   if (getOptLevel() != CodeGenOpt::None) {
5030b57cec5SDimitry Andric     initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
5040b57cec5SDimitry Andric     insertPass(VSXFMAMutateEarly ? &RegisterCoalescerID : &MachineSchedulerID,
5050b57cec5SDimitry Andric                &PPCVSXFMAMutateID);
5060b57cec5SDimitry Andric   }
5070b57cec5SDimitry Andric 
5080b57cec5SDimitry Andric   // FIXME: We probably don't need to run these for -fPIE.
5090b57cec5SDimitry Andric   if (getPPCTargetMachine().isPositionIndependent()) {
5100b57cec5SDimitry Andric     // FIXME: LiveVariables should not be necessary here!
5110b57cec5SDimitry Andric     // PPCTLSDynamicCallPass uses LiveIntervals which previously dependent on
5120b57cec5SDimitry Andric     // LiveVariables. This (unnecessary) dependency has been removed now,
5130b57cec5SDimitry Andric     // however a stage-2 clang build fails without LiveVariables computed here.
5145ffd83dbSDimitry Andric     addPass(&LiveVariablesID);
5150b57cec5SDimitry Andric     addPass(createPPCTLSDynamicCallPass());
5160b57cec5SDimitry Andric   }
5170b57cec5SDimitry Andric   if (EnableExtraTOCRegDeps)
5180b57cec5SDimitry Andric     addPass(createPPCTOCRegDepsPass());
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
5210b57cec5SDimitry Andric     addPass(&MachinePipelinerID);
5220b57cec5SDimitry Andric }
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric void PPCPassConfig::addPreSched2() {
525*e8d8bef9SDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
5260b57cec5SDimitry Andric     addPass(&IfConverterID);
5270b57cec5SDimitry Andric }
5280b57cec5SDimitry Andric 
5290b57cec5SDimitry Andric void PPCPassConfig::addPreEmitPass() {
5300b57cec5SDimitry Andric   addPass(createPPCPreEmitPeepholePass());
5310b57cec5SDimitry Andric   addPass(createPPCExpandISELPass());
5320b57cec5SDimitry Andric 
5330b57cec5SDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
5345ffd83dbSDimitry Andric     addPass(createPPCEarlyReturnPass());
5350b57cec5SDimitry Andric   // Must run branch selection immediately preceding the asm printer.
5365ffd83dbSDimitry Andric   addPass(createPPCBranchSelectionPass());
5370b57cec5SDimitry Andric }
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric TargetTransformInfo
5400b57cec5SDimitry Andric PPCTargetMachine::getTargetTransformInfo(const Function &F) {
5410b57cec5SDimitry Andric   return TargetTransformInfo(PPCTTIImpl(this, F));
5420b57cec5SDimitry Andric }
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric static MachineSchedRegistry
5450b57cec5SDimitry Andric PPCPreRASchedRegistry("ppc-prera",
5460b57cec5SDimitry Andric                       "Run PowerPC PreRA specific scheduler",
5470b57cec5SDimitry Andric                       createPPCMachineScheduler);
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric static MachineSchedRegistry
5500b57cec5SDimitry Andric PPCPostRASchedRegistry("ppc-postra",
5510b57cec5SDimitry Andric                        "Run PowerPC PostRA specific scheduler",
5520b57cec5SDimitry Andric                        createPPCPostMachineScheduler);
553*e8d8bef9SDimitry Andric 
554*e8d8bef9SDimitry Andric // Global ISEL
555*e8d8bef9SDimitry Andric bool PPCPassConfig::addIRTranslator() {
556*e8d8bef9SDimitry Andric   addPass(new IRTranslator());
557*e8d8bef9SDimitry Andric   return false;
558*e8d8bef9SDimitry Andric }
559*e8d8bef9SDimitry Andric 
560*e8d8bef9SDimitry Andric bool PPCPassConfig::addLegalizeMachineIR() {
561*e8d8bef9SDimitry Andric   addPass(new Legalizer());
562*e8d8bef9SDimitry Andric   return false;
563*e8d8bef9SDimitry Andric }
564*e8d8bef9SDimitry Andric 
565*e8d8bef9SDimitry Andric bool PPCPassConfig::addRegBankSelect() {
566*e8d8bef9SDimitry Andric   addPass(new RegBankSelect());
567*e8d8bef9SDimitry Andric   return false;
568*e8d8bef9SDimitry Andric }
569*e8d8bef9SDimitry Andric 
570*e8d8bef9SDimitry Andric bool PPCPassConfig::addGlobalInstructionSelect() {
571*e8d8bef9SDimitry Andric   addPass(new InstructionSelect());
572*e8d8bef9SDimitry Andric   return false;
573*e8d8bef9SDimitry Andric }
574