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