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