| Nate Begeman | 6cca84e | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 1 | //===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===// |
| Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
| Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 6 | // |
| Misha Brukman | e05203f | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 8 | // |
| Chris Lattner | 73785d2 | 2005-08-15 23:47:04 +0000 | [diff] [blame] | 9 | // Top-level implementation for the PowerPC target. |
| Misha Brukman | e05203f | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 13 | #include "PPCTargetMachine.h" |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 14 | #include "MCTargetDesc/PPCMCTargetDesc.h" |
| Craig Topper | b25fda9 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 15 | #include "PPC.h" |
| QingShan Zhang | 5321dcd | 2019-03-27 03:50:16 +0000 | [diff] [blame] | 16 | #include "PPCMachineScheduler.h" |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 17 | #include "PPCSubtarget.h" |
| Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 18 | #include "PPCTargetObjectFile.h" |
| Chandler Carruth | 93dcdc4 | 2015-01-31 11:17:59 +0000 | [diff] [blame] | 19 | #include "PPCTargetTransformInfo.h" |
| Richard Trieu | ee6ced1 | 2019-05-15 00:09:58 +0000 | [diff] [blame] | 20 | #include "TargetInfo/PowerPCTargetInfo.h" |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Optional.h" |
| 22 | #include "llvm/ADT/STLExtras.h" |
| 23 | #include "llvm/ADT/StringRef.h" |
| 24 | #include "llvm/ADT/Triple.h" |
| 25 | #include "llvm/Analysis/TargetTransformInfo.h" |
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/Passes.h" |
| Matthias Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/TargetPassConfig.h" |
| Stefan Pintilie | cb4f0c5 | 2018-07-04 18:54:25 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineScheduler.h" |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Attributes.h" |
| 30 | #include "llvm/IR/DataLayout.h" |
| Eric Christopher | 3faf2f1 | 2014-10-06 06:45:36 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Function.h" |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 32 | #include "llvm/Pass.h" |
| 33 | #include "llvm/Support/CodeGen.h" |
| Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
| Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 35 | #include "llvm/Support/TargetRegistry.h" |
| David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetOptions.h" |
| Hal Finkel | f413be1 | 2014-11-21 04:35:51 +0000 | [diff] [blame] | 38 | #include "llvm/Transforms/Scalar.h" |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 39 | #include <cassert> |
| 40 | #include <memory> |
| 41 | #include <string> |
| 42 | |
| Misha Brukman | e05203f | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 43 | using namespace llvm; |
| 44 | |
| Lei Huang | 34e6621 | 2017-09-12 18:39:11 +0000 | [diff] [blame] | 45 | |
| 46 | static cl::opt<bool> |
| 47 | EnableBranchCoalescing("enable-ppc-branch-coalesce", cl::Hidden, |
| 48 | cl::desc("enable coalescing of duplicate branches for PPC")); |
| Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 49 | static cl:: |
| Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 50 | opt<bool> DisableCTRLoops("disable-ppc-ctrloops", cl::Hidden, |
| 51 | cl::desc("Disable CTR loops for PPC")); |
| Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 52 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 53 | static cl:: |
| 54 | opt<bool> DisablePreIncPrep("disable-ppc-preinc-prep", cl::Hidden, |
| 55 | cl::desc("Disable PPC loop preinc prep")); |
| 56 | |
| Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 57 | static cl::opt<bool> |
| 58 | VSXFMAMutateEarly("schedule-ppc-vsx-fma-mutation-early", |
| 59 | cl::Hidden, cl::desc("Schedule VSX FMA instruction mutation early")); |
| 60 | |
| Bill Schmidt | fe723b9 | 2015-04-27 19:57:34 +0000 | [diff] [blame] | 61 | static cl:: |
| 62 | opt<bool> DisableVSXSwapRemoval("disable-ppc-vsx-swap-removal", cl::Hidden, |
| 63 | cl::desc("Disable VSX Swap Removal for PPC")); |
| 64 | |
| Bill Schmidt | 34af5e1 | 2015-11-10 21:38:26 +0000 | [diff] [blame] | 65 | static cl:: |
| Hal Finkel | fc35391 | 2016-03-31 20:39:41 +0000 | [diff] [blame] | 66 | opt<bool> DisableQPXLoadSplat("disable-ppc-qpx-load-splat", cl::Hidden, |
| 67 | cl::desc("Disable QPX load splat simplification")); |
| 68 | |
| 69 | static cl:: |
| Bill Schmidt | 34af5e1 | 2015-11-10 21:38:26 +0000 | [diff] [blame] | 70 | opt<bool> DisableMIPeephole("disable-ppc-peephole", cl::Hidden, |
| 71 | cl::desc("Disable machine peepholes for PPC")); |
| 72 | |
| Hal Finkel | f413be1 | 2014-11-21 04:35:51 +0000 | [diff] [blame] | 73 | static cl::opt<bool> |
| 74 | EnableGEPOpt("ppc-gep-opt", cl::Hidden, |
| 75 | cl::desc("Enable optimizations on complex GEPs"), |
| 76 | cl::init(true)); |
| 77 | |
| Hal Finkel | e5aaf3f | 2015-02-20 05:08:21 +0000 | [diff] [blame] | 78 | static cl::opt<bool> |
| 79 | EnablePrefetch("enable-ppc-prefetching", |
| 80 | cl::desc("disable software prefetching on PPC"), |
| 81 | cl::init(false), cl::Hidden); |
| 82 | |
| Hal Finkel | 8340de1 | 2015-05-18 06:25:59 +0000 | [diff] [blame] | 83 | static cl::opt<bool> |
| 84 | EnableExtraTOCRegDeps("enable-ppc-extra-toc-reg-deps", |
| 85 | cl::desc("Add extra TOC register dependencies"), |
| 86 | cl::init(true), cl::Hidden); |
| 87 | |
| Hal Finkel | 5d36b23 | 2015-07-15 08:23:05 +0000 | [diff] [blame] | 88 | static cl::opt<bool> |
| 89 | EnableMachineCombinerPass("ppc-machine-combiner", |
| 90 | cl::desc("Enable the machine combiner pass"), |
| 91 | cl::init(true), cl::Hidden); |
| 92 | |
| Nemanja Ivanovic | 6f590bf | 2017-12-13 14:47:35 +0000 | [diff] [blame] | 93 | static cl::opt<bool> |
| 94 | ReduceCRLogical("ppc-reduce-cr-logicals", |
| 95 | cl::desc("Expand eligible cr-logical binary ops to branches"), |
| 96 | cl::init(false), cl::Hidden); |
| Daniel Dunbar | 5680b4f | 2009-07-25 06:49:55 +0000 | [diff] [blame] | 97 | extern "C" void LLVMInitializePowerPCTarget() { |
| 98 | // Register the targets |
| Eric Christopher | ded727c | 2017-06-17 02:25:53 +0000 | [diff] [blame] | 99 | RegisterTargetMachine<PPCTargetMachine> A(getThePPC32Target()); |
| 100 | RegisterTargetMachine<PPCTargetMachine> B(getThePPC64Target()); |
| 101 | RegisterTargetMachine<PPCTargetMachine> C(getThePPC64LETarget()); |
| Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 102 | |
| 103 | PassRegistry &PR = *PassRegistry::getPassRegistry(); |
| Kang Zhang | 2446f84 | 2019-04-12 09:59:40 +0000 | [diff] [blame] | 104 | initializePPCCTRLoopsPass(PR); |
| 105 | #ifndef NDEBUG |
| 106 | initializePPCCTRLoopsVerifyPass(PR); |
| 107 | #endif |
| 108 | initializePPCLoopPreIncPrepPass(PR); |
| 109 | initializePPCTOCRegDepsPass(PR); |
| 110 | initializePPCEarlyReturnPass(PR); |
| 111 | initializePPCVSXCopyPass(PR); |
| 112 | initializePPCVSXFMAMutatePass(PR); |
| 113 | initializePPCVSXSwapRemovalPass(PR); |
| 114 | initializePPCReduceCRLogicalsPass(PR); |
| 115 | initializePPCBSelPass(PR); |
| 116 | initializePPCBranchCoalescingPass(PR); |
| 117 | initializePPCQPXLoadSplatPass(PR); |
| Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 118 | initializePPCBoolRetToIntPass(PR); |
| Tony Jiang | 8e8c444 | 2017-01-16 20:12:26 +0000 | [diff] [blame] | 119 | initializePPCExpandISELPass(PR); |
| Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 120 | initializePPCPreEmitPeepholePass(PR); |
| Hiroshi Inoue | 6989caa | 2017-06-29 14:13:38 +0000 | [diff] [blame] | 121 | initializePPCTLSDynamicCallPass(PR); |
| Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 122 | initializePPCMIPeepholePass(PR); |
| Daniel Dunbar | 5680b4f | 2009-07-25 06:49:55 +0000 | [diff] [blame] | 123 | } |
| Douglas Gregor | 1b731d5 | 2009-06-16 20:12:29 +0000 | [diff] [blame] | 124 | |
| Eric Christopher | 8b77065 | 2015-01-26 19:03:15 +0000 | [diff] [blame] | 125 | /// Return the datalayout string of a subtarget. |
| 126 | static std::string getDataLayoutString(const Triple &T) { |
| 127 | bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le; |
| 128 | std::string Ret; |
| 129 | |
| 130 | // Most PPC* platforms are big endian, PPC64LE is little endian. |
| 131 | if (T.getArch() == Triple::ppc64le) |
| 132 | Ret = "e"; |
| 133 | else |
| 134 | Ret = "E"; |
| 135 | |
| 136 | Ret += DataLayout::getManglingComponent(T); |
| 137 | |
| 138 | // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit |
| 139 | // pointers. |
| 140 | if (!is64Bit || T.getOS() == Triple::Lv2) |
| 141 | Ret += "-p:32:32"; |
| 142 | |
| 143 | // Note, the alignment values for f64 and i64 on ppc64 in Darwin |
| 144 | // documentation are wrong; these are correct (i.e. "what gcc does"). |
| 145 | if (is64Bit || !T.isOSDarwin()) |
| 146 | Ret += "-i64:64"; |
| 147 | else |
| 148 | Ret += "-f64:32:64"; |
| 149 | |
| 150 | // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones. |
| 151 | if (is64Bit) |
| 152 | Ret += "-n32:64"; |
| 153 | else |
| 154 | Ret += "-n32"; |
| 155 | |
| 156 | return Ret; |
| 157 | } |
| 158 | |
| Daniel Sanders | 335487a | 2015-06-16 13:15:50 +0000 | [diff] [blame] | 159 | static std::string computeFSAdditions(StringRef FS, CodeGenOpt::Level OL, |
| 160 | const Triple &TT) { |
| Eric Christopher | 36448af | 2014-10-01 20:38:26 +0000 | [diff] [blame] | 161 | std::string FullFS = FS; |
| Eric Christopher | 36448af | 2014-10-01 20:38:26 +0000 | [diff] [blame] | 162 | |
| 163 | // Make sure 64-bit features are available when CPUname is generic |
| Daniel Sanders | 335487a | 2015-06-16 13:15:50 +0000 | [diff] [blame] | 164 | if (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le) { |
| Eric Christopher | 36448af | 2014-10-01 20:38:26 +0000 | [diff] [blame] | 165 | if (!FullFS.empty()) |
| 166 | FullFS = "+64bit," + FullFS; |
| 167 | else |
| 168 | FullFS = "+64bit"; |
| 169 | } |
| 170 | |
| 171 | if (OL >= CodeGenOpt::Default) { |
| 172 | if (!FullFS.empty()) |
| 173 | FullFS = "+crbits," + FullFS; |
| 174 | else |
| 175 | FullFS = "+crbits"; |
| 176 | } |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 177 | |
| 178 | if (OL != CodeGenOpt::None) { |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 179 | if (!FullFS.empty()) |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 180 | FullFS = "+invariant-function-descriptors," + FullFS; |
| 181 | else |
| 182 | FullFS = "+invariant-function-descriptors"; |
| 183 | } |
| 184 | |
| Eric Christopher | 36448af | 2014-10-01 20:38:26 +0000 | [diff] [blame] | 185 | return FullFS; |
| 186 | } |
| 187 | |
| Aditya Nandakumar | a271932 | 2014-11-13 09:26:31 +0000 | [diff] [blame] | 188 | static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) { |
| 189 | // If it isn't a Mach-O file then it's going to be a linux ELF |
| 190 | // object file. |
| 191 | if (TT.isOSDarwin()) |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 192 | return llvm::make_unique<TargetLoweringObjectFileMachO>(); |
| Aditya Nandakumar | a271932 | 2014-11-13 09:26:31 +0000 | [diff] [blame] | 193 | |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 194 | return llvm::make_unique<PPC64LinuxTargetObjectFile>(); |
| Aditya Nandakumar | a271932 | 2014-11-13 09:26:31 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| Eric Christopher | fee6aaf | 2015-02-17 06:45:15 +0000 | [diff] [blame] | 197 | static PPCTargetMachine::PPCABI computeTargetABI(const Triple &TT, |
| 198 | const TargetOptions &Options) { |
| Kit Barton | 7c80f98 | 2018-08-28 01:18:29 +0000 | [diff] [blame] | 199 | if (TT.isOSDarwin()) |
| 200 | report_fatal_error("Darwin is no longer supported for PowerPC"); |
| 201 | |
| Eric Christopher | fee6aaf | 2015-02-17 06:45:15 +0000 | [diff] [blame] | 202 | if (Options.MCOptions.getABIName().startswith("elfv1")) |
| 203 | return PPCTargetMachine::PPC_ABI_ELFv1; |
| 204 | else if (Options.MCOptions.getABIName().startswith("elfv2")) |
| 205 | return PPCTargetMachine::PPC_ABI_ELFv2; |
| 206 | |
| 207 | assert(Options.MCOptions.getABIName().empty() && |
| NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 208 | "Unknown target-abi option!"); |
| Eric Christopher | fee6aaf | 2015-02-17 06:45:15 +0000 | [diff] [blame] | 209 | |
| Eric Christopher | 5ec30ef | 2017-06-17 02:25:55 +0000 | [diff] [blame] | 210 | if (TT.isMacOSX()) |
| 211 | return PPCTargetMachine::PPC_ABI_UNKNOWN; |
| 212 | |
| 213 | switch (TT.getArch()) { |
| 214 | case Triple::ppc64le: |
| 215 | return PPCTargetMachine::PPC_ABI_ELFv2; |
| 216 | case Triple::ppc64: |
| 217 | return PPCTargetMachine::PPC_ABI_ELFv1; |
| 218 | default: |
| 219 | return PPCTargetMachine::PPC_ABI_UNKNOWN; |
| Eric Christopher | fee6aaf | 2015-02-17 06:45:15 +0000 | [diff] [blame] | 220 | } |
| Eric Christopher | fee6aaf | 2015-02-17 06:45:15 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 223 | static Reloc::Model getEffectiveRelocModel(const Triple &TT, |
| 224 | Optional<Reloc::Model> RM) { |
| Eric Christopher | c70d07b | 2017-06-17 02:25:56 +0000 | [diff] [blame] | 225 | if (RM.hasValue()) |
| 226 | return *RM; |
| 227 | |
| 228 | // Darwin defaults to dynamic-no-pic. |
| 229 | if (TT.isOSDarwin()) |
| 230 | return Reloc::DynamicNoPIC; |
| 231 | |
| Stefan Pintilie | 46f840f | 2018-12-04 20:14:57 +0000 | [diff] [blame] | 232 | // Big Endian PPC is PIC by default. |
| 233 | if (TT.getArch() == Triple::ppc64) |
| Stefan Pintilie | 9004444 | 2018-11-16 19:24:23 +0000 | [diff] [blame] | 234 | return Reloc::PIC_; |
| 235 | |
| Stefan Pintilie | 46f840f | 2018-12-04 20:14:57 +0000 | [diff] [blame] | 236 | // Rest are static by default. |
| Eric Christopher | c70d07b | 2017-06-17 02:25:56 +0000 | [diff] [blame] | 237 | return Reloc::Static; |
| Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| David Green | ca29c27 | 2018-12-07 12:10:23 +0000 | [diff] [blame] | 240 | static CodeModel::Model getEffectivePPCCodeModel(const Triple &TT, |
| 241 | Optional<CodeModel::Model> CM, |
| 242 | bool JIT) { |
| 243 | if (CM) { |
| 244 | if (*CM == CodeModel::Tiny) |
| 245 | report_fatal_error("Target does not support the tiny CodeModel"); |
| 246 | if (*CM == CodeModel::Kernel) |
| 247 | report_fatal_error("Target does not support the kernel CodeModel"); |
| Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 248 | return *CM; |
| David Green | ca29c27 | 2018-12-07 12:10:23 +0000 | [diff] [blame] | 249 | } |
| Rafael Espindola | 27834695 | 2017-08-03 04:52:45 +0000 | [diff] [blame] | 250 | if (!TT.isOSDarwin() && !JIT && |
| Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 251 | (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le)) |
| 252 | return CodeModel::Medium; |
| 253 | return CodeModel::Small; |
| 254 | } |
| 255 | |
| QingShan Zhang | 5321dcd | 2019-03-27 03:50:16 +0000 | [diff] [blame] | 256 | |
| 257 | static ScheduleDAGInstrs *createPPCMachineScheduler(MachineSchedContext *C) { |
| 258 | ScheduleDAGMILive *DAG = |
| 259 | new ScheduleDAGMILive(C, llvm::make_unique<PPCPreRASchedStrategy>(C)); |
| 260 | // add DAG Mutations here. |
| 261 | DAG->addMutation(createCopyConstrainDAGMutation(DAG->TII, DAG->TRI)); |
| 262 | return DAG; |
| 263 | } |
| 264 | |
| 265 | static ScheduleDAGInstrs *createPPCPostMachineScheduler( |
| 266 | MachineSchedContext *C) { |
| 267 | ScheduleDAGMI *DAG = |
| 268 | new ScheduleDAGMI(C, llvm::make_unique<PPCPostRASchedStrategy>(C), true); |
| 269 | // add DAG Mutations here. |
| 270 | return DAG; |
| 271 | } |
| 272 | |
| NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 273 | // The FeatureString here is a little subtle. We are modifying the feature |
| 274 | // string with what are (currently) non-function specific overrides as it goes |
| Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 275 | // into the LLVMTargetMachine constructor and then using the stored value in the |
| Eric Christopher | 36448af | 2014-10-01 20:38:26 +0000 | [diff] [blame] | 276 | // Subtarget constructor below it. |
| Daniel Sanders | 3e5de88 | 2015-06-11 19:41:26 +0000 | [diff] [blame] | 277 | PPCTargetMachine::PPCTargetMachine(const Target &T, const Triple &TT, |
| 278 | StringRef CPU, StringRef FS, |
| 279 | const TargetOptions &Options, |
| Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 280 | Optional<Reloc::Model> RM, |
| Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 281 | Optional<CodeModel::Model> CM, |
| 282 | CodeGenOpt::Level OL, bool JIT) |
| Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 283 | : LLVMTargetMachine(T, getDataLayoutString(TT), TT, CPU, |
| 284 | computeFSAdditions(FS, OL, TT), Options, |
| 285 | getEffectiveRelocModel(TT, RM), |
| David Green | ca29c27 | 2018-12-07 12:10:23 +0000 | [diff] [blame] | 286 | getEffectivePPCCodeModel(TT, CM, JIT), OL), |
| Daniel Sanders | c81f450 | 2015-06-16 15:44:21 +0000 | [diff] [blame] | 287 | TLOF(createTLOF(getTargetTriple())), |
| Eric Christopher | 380611a | 2017-04-06 23:01:30 +0000 | [diff] [blame] | 288 | TargetABI(computeTargetABI(TT, Options)) { |
| Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 289 | initAsmInfo(); |
| Nate Begeman | 6cca84e | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 292 | PPCTargetMachine::~PPCTargetMachine() = default; |
| Reid Kleckner | 357600e | 2014-11-20 23:37:18 +0000 | [diff] [blame] | 293 | |
| Eric Christopher | 3faf2f1 | 2014-10-06 06:45:36 +0000 | [diff] [blame] | 294 | const PPCSubtarget * |
| 295 | PPCTargetMachine::getSubtargetImpl(const Function &F) const { |
| Duncan P. N. Exon Smith | 5bedaf93 | 2015-02-14 02:54:07 +0000 | [diff] [blame] | 296 | Attribute CPUAttr = F.getFnAttribute("target-cpu"); |
| 297 | Attribute FSAttr = F.getFnAttribute("target-features"); |
| Eric Christopher | 3faf2f1 | 2014-10-06 06:45:36 +0000 | [diff] [blame] | 298 | |
| 299 | std::string CPU = !CPUAttr.hasAttribute(Attribute::None) |
| 300 | ? CPUAttr.getValueAsString().str() |
| 301 | : TargetCPU; |
| 302 | std::string FS = !FSAttr.hasAttribute(Attribute::None) |
| 303 | ? FSAttr.getValueAsString().str() |
| 304 | : TargetFS; |
| 305 | |
| Petar Jovanovic | 280f710 | 2015-12-14 17:57:33 +0000 | [diff] [blame] | 306 | // FIXME: This is related to the code below to reset the target options, |
| 307 | // we need to know whether or not the soft float flag is set on the |
| 308 | // function before we can generate a subtarget. We also need to use |
| 309 | // it as a key for the subtarget since that can be the only difference |
| 310 | // between two functions. |
| 311 | bool SoftFloat = |
| Nirav Dave | 8dd66e5 | 2016-03-30 15:41:12 +0000 | [diff] [blame] | 312 | F.getFnAttribute("use-soft-float").getValueAsString() == "true"; |
| Petar Jovanovic | 280f710 | 2015-12-14 17:57:33 +0000 | [diff] [blame] | 313 | // If the soft float attribute is set on the function turn on the soft float |
| 314 | // subtarget feature. |
| 315 | if (SoftFloat) |
| Hal Finkel | a932105 | 2016-10-02 02:10:20 +0000 | [diff] [blame] | 316 | FS += FS.empty() ? "-hard-float" : ",-hard-float"; |
| Petar Jovanovic | 280f710 | 2015-12-14 17:57:33 +0000 | [diff] [blame] | 317 | |
| Eric Christopher | 3faf2f1 | 2014-10-06 06:45:36 +0000 | [diff] [blame] | 318 | auto &I = SubtargetMap[CPU + FS]; |
| 319 | if (!I) { |
| 320 | // This needs to be done before we create a new subtarget since any |
| 321 | // creation will depend on the TM and the code generation flags on the |
| 322 | // function that reside in TargetOptions. |
| 323 | resetTargetOptions(F); |
| Eric Christopher | ed1042b | 2015-03-26 00:50:23 +0000 | [diff] [blame] | 324 | I = llvm::make_unique<PPCSubtarget>( |
| Daniel Sanders | c81f450 | 2015-06-16 15:44:21 +0000 | [diff] [blame] | 325 | TargetTriple, CPU, |
| Eric Christopher | ed1042b | 2015-03-26 00:50:23 +0000 | [diff] [blame] | 326 | // FIXME: It would be good to have the subtarget additions here |
| 327 | // not necessary. Anything that turns them on/off (overrides) ends |
| 328 | // up being put at the end of the feature string, but the defaults |
| 329 | // shouldn't require adding them. Fixing this means pulling Feature64Bit |
| 330 | // out of most of the target cpus in the .td file and making it set only |
| 331 | // as part of initialization via the TargetTriple. |
| 332 | computeFSAdditions(FS, getOptLevel(), getTargetTriple()), *this); |
| Eric Christopher | 3faf2f1 | 2014-10-06 06:45:36 +0000 | [diff] [blame] | 333 | } |
| 334 | return I.get(); |
| 335 | } |
| Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 336 | |
| Chris Lattner | 12e9730 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 337 | //===----------------------------------------------------------------------===// |
| 338 | // Pass Pipeline Configuration |
| 339 | //===----------------------------------------------------------------------===// |
| Nate Begeman | f17ea0f | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 340 | |
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 341 | namespace { |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 342 | |
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 343 | /// PPC Code Generator Pass Configuration Options. |
| 344 | class PPCPassConfig : public TargetPassConfig { |
| 345 | public: |
| Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 346 | PPCPassConfig(PPCTargetMachine &TM, PassManagerBase &PM) |
| Stefan Pintilie | cb4f0c5 | 2018-07-04 18:54:25 +0000 | [diff] [blame] | 347 | : TargetPassConfig(TM, PM) { |
| 348 | // At any optimization level above -O0 we use the Machine Scheduler and not |
| 349 | // the default Post RA List Scheduler. |
| 350 | if (TM.getOptLevel() != CodeGenOpt::None) |
| 351 | substitutePass(&PostRASchedulerID, &PostMachineSchedulerID); |
| 352 | } |
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 353 | |
| 354 | PPCTargetMachine &getPPCTargetMachine() const { |
| 355 | return getTM<PPCTargetMachine>(); |
| 356 | } |
| 357 | |
| Robin Morisset | 2212996 | 2014-09-23 20:46:49 +0000 | [diff] [blame] | 358 | void addIRPasses() override; |
| Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 359 | bool addPreISel() override; |
| 360 | bool addILPOpts() override; |
| 361 | bool addInstSelector() override; |
| Bill Schmidt | fe723b9 | 2015-04-27 19:57:34 +0000 | [diff] [blame] | 362 | void addMachineSSAOptimization() override; |
| Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 363 | void addPreRegAlloc() override; |
| 364 | void addPreSched2() override; |
| 365 | void addPreEmitPass() override; |
| QingShan Zhang | 5321dcd | 2019-03-27 03:50:16 +0000 | [diff] [blame] | 366 | ScheduleDAGInstrs * |
| 367 | createMachineScheduler(MachineSchedContext *C) const override { |
| 368 | const PPCSubtarget &ST = C->MF->getSubtarget<PPCSubtarget>(); |
| 369 | if (ST.usePPCPreRASchedStrategy()) |
| 370 | return createPPCMachineScheduler(C); |
| 371 | return nullptr; |
| 372 | } |
| 373 | ScheduleDAGInstrs * |
| 374 | createPostMachineScheduler(MachineSchedContext *C) const override { |
| 375 | const PPCSubtarget &ST = C->MF->getSubtarget<PPCSubtarget>(); |
| 376 | if (ST.usePPCPostRASchedStrategy()) |
| 377 | return createPPCPostMachineScheduler(C); |
| 378 | return nullptr; |
| 379 | } |
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 380 | }; |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 381 | |
| 382 | } // end anonymous namespace |
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 383 | |
| Andrew Trick | f8ea108 | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 384 | TargetPassConfig *PPCTargetMachine::createPassConfig(PassManagerBase &PM) { |
| Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 385 | return new PPCPassConfig(*this, PM); |
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| Robin Morisset | 2212996 | 2014-09-23 20:46:49 +0000 | [diff] [blame] | 388 | void PPCPassConfig::addIRPasses() { |
| Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 389 | if (TM->getOptLevel() != CodeGenOpt::None) |
| Eric Christopher | 9fd267c | 2017-03-31 02:16:54 +0000 | [diff] [blame] | 390 | addPass(createPPCBoolRetToIntPass()); |
| Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 391 | addPass(createAtomicExpandPass()); |
| Hal Finkel | f413be1 | 2014-11-21 04:35:51 +0000 | [diff] [blame] | 392 | |
| Hal Finkel | e5aaf3f | 2015-02-20 05:08:21 +0000 | [diff] [blame] | 393 | // For the BG/Q (or if explicitly requested), add explicit data prefetch |
| 394 | // intrinsics. |
| Daniel Sanders | c81f450 | 2015-06-16 15:44:21 +0000 | [diff] [blame] | 395 | bool UsePrefetching = TM->getTargetTriple().getVendor() == Triple::BGQ && |
| 396 | getOptLevel() != CodeGenOpt::None; |
| Hal Finkel | e5aaf3f | 2015-02-20 05:08:21 +0000 | [diff] [blame] | 397 | if (EnablePrefetch.getNumOccurrences() > 0) |
| 398 | UsePrefetching = EnablePrefetch; |
| 399 | if (UsePrefetching) |
| Adam Nemet | 9d9cb27 | 2016-02-18 21:38:19 +0000 | [diff] [blame] | 400 | addPass(createLoopDataPrefetchPass()); |
| Hal Finkel | e5aaf3f | 2015-02-20 05:08:21 +0000 | [diff] [blame] | 401 | |
| Ehsan Amiri | 4701a91 | 2016-04-07 15:30:55 +0000 | [diff] [blame] | 402 | if (TM->getOptLevel() >= CodeGenOpt::Default && EnableGEPOpt) { |
| Hal Finkel | f413be1 | 2014-11-21 04:35:51 +0000 | [diff] [blame] | 403 | // Call SeparateConstOffsetFromGEP pass to extract constants within indices |
| 404 | // and lower a GEP with multiple indices to either arithmetic operations or |
| 405 | // multiple GEPs with single index. |
| David Blaikie | 8ad9a97 | 2018-03-28 22:28:50 +0000 | [diff] [blame] | 406 | addPass(createSeparateConstOffsetFromGEPPass(true)); |
| Hal Finkel | f413be1 | 2014-11-21 04:35:51 +0000 | [diff] [blame] | 407 | // Call EarlyCSE pass to find and remove subexpressions in the lowered |
| 408 | // result. |
| 409 | addPass(createEarlyCSEPass()); |
| 410 | // Do loop invariant code motion in case part of the lowered result is |
| 411 | // invariant. |
| 412 | addPass(createLICMPass()); |
| 413 | } |
| 414 | |
| Robin Morisset | 2212996 | 2014-09-23 20:46:49 +0000 | [diff] [blame] | 415 | TargetPassConfig::addIRPasses(); |
| 416 | } |
| 417 | |
| Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 418 | bool PPCPassConfig::addPreISel() { |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 419 | if (!DisablePreIncPrep && getOptLevel() != CodeGenOpt::None) |
| 420 | addPass(createPPCLoopPreIncPrepPass(getPPCTargetMachine())); |
| 421 | |
| Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 422 | if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None) |
| Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 423 | addPass(createPPCCTRLoops()); |
| Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 424 | |
| 425 | return false; |
| 426 | } |
| 427 | |
| Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 428 | bool PPCPassConfig::addILPOpts() { |
| Eric Christopher | 6b0fcfe | 2014-05-21 23:40:26 +0000 | [diff] [blame] | 429 | addPass(&EarlyIfConverterID); |
| Hal Finkel | 5d36b23 | 2015-07-15 08:23:05 +0000 | [diff] [blame] | 430 | |
| 431 | if (EnableMachineCombinerPass) |
| 432 | addPass(&MachineCombinerID); |
| 433 | |
| Eric Christopher | 6b0fcfe | 2014-05-21 23:40:26 +0000 | [diff] [blame] | 434 | return true; |
| Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 437 | bool PPCPassConfig::addInstSelector() { |
| Chris Lattner | c6aa806 | 2005-08-17 19:33:30 +0000 | [diff] [blame] | 438 | // Install an instruction selector. |
| Hiroshi Inoue | 5102028 | 2017-06-27 04:52:17 +0000 | [diff] [blame] | 439 | addPass(createPPCISelDag(getPPCTargetMachine(), getOptLevel())); |
| Hal Finkel | 8ca3884 | 2013-05-20 16:08:17 +0000 | [diff] [blame] | 440 | |
| 441 | #ifndef NDEBUG |
| 442 | if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None) |
| 443 | addPass(createPPCCTRLoopsVerify()); |
| 444 | #endif |
| 445 | |
| Eric Christopher | d71e444 | 2014-05-22 01:21:35 +0000 | [diff] [blame] | 446 | addPass(createPPCVSXCopyPass()); |
| Nate Begeman | f17ea0f | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 447 | return false; |
| 448 | } |
| 449 | |
| Bill Schmidt | fe723b9 | 2015-04-27 19:57:34 +0000 | [diff] [blame] | 450 | void PPCPassConfig::addMachineSSAOptimization() { |
| Lei Huang | 34e6621 | 2017-09-12 18:39:11 +0000 | [diff] [blame] | 451 | // PPCBranchCoalescingPass need to be done before machine sinking |
| 452 | // since it merges empty blocks. |
| 453 | if (EnableBranchCoalescing && getOptLevel() != CodeGenOpt::None) |
| 454 | addPass(createPPCBranchCoalescingPass()); |
| Bill Schmidt | fe723b9 | 2015-04-27 19:57:34 +0000 | [diff] [blame] | 455 | TargetPassConfig::addMachineSSAOptimization(); |
| 456 | // For little endian, remove where possible the vector swap instructions |
| 457 | // introduced at code generation to normalize vector element order. |
| Daniel Sanders | c81f450 | 2015-06-16 15:44:21 +0000 | [diff] [blame] | 458 | if (TM->getTargetTriple().getArch() == Triple::ppc64le && |
| Bill Schmidt | fe723b9 | 2015-04-27 19:57:34 +0000 | [diff] [blame] | 459 | !DisableVSXSwapRemoval) |
| 460 | addPass(createPPCVSXSwapRemovalPass()); |
| Nemanja Ivanovic | 6f590bf | 2017-12-13 14:47:35 +0000 | [diff] [blame] | 461 | // Reduce the number of cr-logical ops. |
| 462 | if (ReduceCRLogical && getOptLevel() != CodeGenOpt::None) |
| 463 | addPass(createPPCReduceCRLogicalsPass()); |
| Bill Schmidt | 34af5e1 | 2015-11-10 21:38:26 +0000 | [diff] [blame] | 464 | // Target-specific peephole cleanups performed after instruction |
| 465 | // selection. |
| 466 | if (!DisableMIPeephole) { |
| 467 | addPass(createPPCMIPeepholePass()); |
| 468 | addPass(&DeadMachineInstructionElimID); |
| 469 | } |
| Bill Schmidt | fe723b9 | 2015-04-27 19:57:34 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 472 | void PPCPassConfig::addPreRegAlloc() { |
| Andrew Kaylor | 289bd5f | 2016-04-27 19:39:32 +0000 | [diff] [blame] | 473 | if (getOptLevel() != CodeGenOpt::None) { |
| 474 | initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry()); |
| 475 | insertPass(VSXFMAMutateEarly ? &RegisterCoalescerID : &MachineSchedulerID, |
| 476 | &PPCVSXFMAMutateID); |
| 477 | } |
| Rafael Espindola | 248cfb9 | 2016-06-28 12:49:12 +0000 | [diff] [blame] | 478 | |
| 479 | // FIXME: We probably don't need to run these for -fPIE. |
| 480 | if (getPPCTargetMachine().isPositionIndependent()) { |
| Matthias Braun | f84547c | 2016-04-28 23:42:51 +0000 | [diff] [blame] | 481 | // FIXME: LiveVariables should not be necessary here! |
| Hiroshi Inoue | e7a3553 | 2017-06-20 17:53:33 +0000 | [diff] [blame] | 482 | // PPCTLSDynamicCallPass uses LiveIntervals which previously dependent on |
| Matthias Braun | f84547c | 2016-04-28 23:42:51 +0000 | [diff] [blame] | 483 | // LiveVariables. This (unnecessary) dependency has been removed now, |
| 484 | // however a stage-2 clang build fails without LiveVariables computed here. |
| 485 | addPass(&LiveVariablesID, false); |
| Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 486 | addPass(createPPCTLSDynamicCallPass()); |
| Matthias Braun | f84547c | 2016-04-28 23:42:51 +0000 | [diff] [blame] | 487 | } |
| Hal Finkel | 8340de1 | 2015-05-18 06:25:59 +0000 | [diff] [blame] | 488 | if (EnableExtraTOCRegDeps) |
| 489 | addPass(createPPCTOCRegDepsPass()); |
| Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 492 | void PPCPassConfig::addPreSched2() { |
| Hal Finkel | fc35391 | 2016-03-31 20:39:41 +0000 | [diff] [blame] | 493 | if (getOptLevel() != CodeGenOpt::None) { |
| Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 494 | addPass(&IfConverterID); |
| Hal Finkel | fc35391 | 2016-03-31 20:39:41 +0000 | [diff] [blame] | 495 | |
| 496 | // This optimization must happen after anything that might do store-to-load |
| 497 | // forwarding. Here we're after RA (and, thus, when spills are inserted) |
| 498 | // but before post-RA scheduling. |
| 499 | if (!DisableQPXLoadSplat) |
| 500 | addPass(createPPCQPXLoadSplatPass()); |
| 501 | } |
| Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 504 | void PPCPassConfig::addPreEmitPass() { |
| Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 505 | addPass(createPPCPreEmitPeepholePass()); |
| Tony Jiang | 8e8c444 | 2017-01-16 20:12:26 +0000 | [diff] [blame] | 506 | addPass(createPPCExpandISELPass()); |
| 507 | |
| Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 508 | if (getOptLevel() != CodeGenOpt::None) |
| Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 509 | addPass(createPPCEarlyReturnPass(), false); |
| Chris Lattner | 12e9730 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 510 | // Must run branch selection immediately preceding the asm printer. |
| Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 511 | addPass(createPPCBranchSelectionPass(), false); |
| Chris Lattner | 12e9730 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| Sanjoy Das | 26d11ca | 2017-12-22 18:21:59 +0000 | [diff] [blame] | 514 | TargetTransformInfo |
| 515 | PPCTargetMachine::getTargetTransformInfo(const Function &F) { |
| 516 | return TargetTransformInfo(PPCTTIImpl(this, F)); |
| Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 517 | } |
| QingShan Zhang | 5321dcd | 2019-03-27 03:50:16 +0000 | [diff] [blame] | 518 | |
| 519 | static MachineSchedRegistry |
| 520 | PPCPreRASchedRegistry("ppc-prera", |
| 521 | "Run PowerPC PreRA specific scheduler", |
| 522 | createPPCMachineScheduler); |
| 523 | |
| 524 | static MachineSchedRegistry |
| 525 | PPCPostRASchedRegistry("ppc-postra", |
| 526 | "Run PowerPC PostRA specific scheduler", |
| 527 | createPPCPostMachineScheduler); |