blob: 34410393ef6f7125fde6d4e4809546c4dd14a90f [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukmane05203f2004-06-21 16:55:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukmane05203f2004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
Misha Brukmanb4402432005-04-21 23:30:14 +00009//
Chris Lattner73785d22005-08-15 23:47:04 +000010// Top-level implementation for the PowerPC target.
Misha Brukmane05203f2004-06-21 16:55:25 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "PPCTargetMachine.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000015#include "MCTargetDesc/PPCMCTargetDesc.h"
Craig Topperb25fda92012-03-17 18:46:09 +000016#include "PPC.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000017#include "PPCSubtarget.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "PPCTargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000019#include "PPCTargetTransformInfo.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000020#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 Trickccb67362012-02-03 05:12:41 +000025#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000026#include "llvm/CodeGen/TargetPassConfig.h"
Stefan Pintiliecb4f0c52018-07-04 18:54:25 +000027#include "llvm/CodeGen/MachineScheduler.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000028#include "llvm/IR/Attributes.h"
29#include "llvm/IR/DataLayout.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000030#include "llvm/IR/Function.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000031#include "llvm/Pass.h"
32#include "llvm/Support/CodeGen.h"
Hal Finkel96c2d4d2012-06-08 15:38:21 +000033#include "llvm/Support/CommandLine.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000034#include "llvm/Support/TargetRegistry.h"
David Blaikie6054e652018-03-23 23:58:19 +000035#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetOptions.h"
Hal Finkelf413be12014-11-21 04:35:51 +000037#include "llvm/Transforms/Scalar.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000038#include <cassert>
39#include <memory>
40#include <string>
41
Misha Brukmane05203f2004-06-21 16:55:25 +000042using namespace llvm;
43
Lei Huang34e66212017-09-12 18:39:11 +000044
45static cl::opt<bool>
46 EnableBranchCoalescing("enable-ppc-branch-coalesce", cl::Hidden,
47 cl::desc("enable coalescing of duplicate branches for PPC"));
Hal Finkel96c2d4d2012-06-08 15:38:21 +000048static cl::
Hal Finkelc6b5deb2012-06-08 19:19:53 +000049opt<bool> DisableCTRLoops("disable-ppc-ctrloops", cl::Hidden,
50 cl::desc("Disable CTR loops for PPC"));
Hal Finkel96c2d4d2012-06-08 15:38:21 +000051
Hal Finkelc9dd0202015-02-05 18:43:00 +000052static cl::
53opt<bool> DisablePreIncPrep("disable-ppc-preinc-prep", cl::Hidden,
54 cl::desc("Disable PPC loop preinc prep"));
55
Hal Finkel174e5902014-03-25 23:29:21 +000056static cl::opt<bool>
57VSXFMAMutateEarly("schedule-ppc-vsx-fma-mutation-early",
58 cl::Hidden, cl::desc("Schedule VSX FMA instruction mutation early"));
59
Bill Schmidtfe723b92015-04-27 19:57:34 +000060static cl::
61opt<bool> DisableVSXSwapRemoval("disable-ppc-vsx-swap-removal", cl::Hidden,
62 cl::desc("Disable VSX Swap Removal for PPC"));
63
Bill Schmidt34af5e12015-11-10 21:38:26 +000064static cl::
Hal Finkelfc353912016-03-31 20:39:41 +000065opt<bool> DisableQPXLoadSplat("disable-ppc-qpx-load-splat", cl::Hidden,
66 cl::desc("Disable QPX load splat simplification"));
67
68static cl::
Bill Schmidt34af5e12015-11-10 21:38:26 +000069opt<bool> DisableMIPeephole("disable-ppc-peephole", cl::Hidden,
70 cl::desc("Disable machine peepholes for PPC"));
71
Hal Finkelf413be12014-11-21 04:35:51 +000072static cl::opt<bool>
73EnableGEPOpt("ppc-gep-opt", cl::Hidden,
74 cl::desc("Enable optimizations on complex GEPs"),
75 cl::init(true));
76
Hal Finkele5aaf3f2015-02-20 05:08:21 +000077static cl::opt<bool>
78EnablePrefetch("enable-ppc-prefetching",
79 cl::desc("disable software prefetching on PPC"),
80 cl::init(false), cl::Hidden);
81
Hal Finkel8340de12015-05-18 06:25:59 +000082static cl::opt<bool>
83EnableExtraTOCRegDeps("enable-ppc-extra-toc-reg-deps",
84 cl::desc("Add extra TOC register dependencies"),
85 cl::init(true), cl::Hidden);
86
Hal Finkel5d36b232015-07-15 08:23:05 +000087static cl::opt<bool>
88EnableMachineCombinerPass("ppc-machine-combiner",
89 cl::desc("Enable the machine combiner pass"),
90 cl::init(true), cl::Hidden);
91
Nemanja Ivanovic6f590bf2017-12-13 14:47:35 +000092static cl::opt<bool>
93 ReduceCRLogical("ppc-reduce-cr-logicals",
94 cl::desc("Expand eligible cr-logical binary ops to branches"),
95 cl::init(false), cl::Hidden);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000096extern "C" void LLVMInitializePowerPCTarget() {
97 // Register the targets
Eric Christopherded727c2017-06-17 02:25:53 +000098 RegisterTargetMachine<PPCTargetMachine> A(getThePPC32Target());
99 RegisterTargetMachine<PPCTargetMachine> B(getThePPC64Target());
100 RegisterTargetMachine<PPCTargetMachine> C(getThePPC64LETarget());
Kit Bartona1c712f2015-12-07 20:50:29 +0000101
102 PassRegistry &PR = *PassRegistry::getPassRegistry();
103 initializePPCBoolRetToIntPass(PR);
Tony Jiang8e8c4442017-01-16 20:12:26 +0000104 initializePPCExpandISELPass(PR);
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +0000105 initializePPCPreEmitPeepholePass(PR);
Hiroshi Inoue6989caa2017-06-29 14:13:38 +0000106 initializePPCTLSDynamicCallPass(PR);
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +0000107 initializePPCMIPeepholePass(PR);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000108}
Douglas Gregor1b731d52009-06-16 20:12:29 +0000109
Eric Christopher8b770652015-01-26 19:03:15 +0000110/// Return the datalayout string of a subtarget.
111static std::string getDataLayoutString(const Triple &T) {
112 bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
113 std::string Ret;
114
115 // Most PPC* platforms are big endian, PPC64LE is little endian.
116 if (T.getArch() == Triple::ppc64le)
117 Ret = "e";
118 else
119 Ret = "E";
120
121 Ret += DataLayout::getManglingComponent(T);
122
123 // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
124 // pointers.
125 if (!is64Bit || T.getOS() == Triple::Lv2)
126 Ret += "-p:32:32";
127
128 // Note, the alignment values for f64 and i64 on ppc64 in Darwin
129 // documentation are wrong; these are correct (i.e. "what gcc does").
130 if (is64Bit || !T.isOSDarwin())
131 Ret += "-i64:64";
132 else
133 Ret += "-f64:32:64";
134
135 // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
136 if (is64Bit)
137 Ret += "-n32:64";
138 else
139 Ret += "-n32";
140
141 return Ret;
142}
143
Daniel Sanders335487a2015-06-16 13:15:50 +0000144static std::string computeFSAdditions(StringRef FS, CodeGenOpt::Level OL,
145 const Triple &TT) {
Eric Christopher36448af2014-10-01 20:38:26 +0000146 std::string FullFS = FS;
Eric Christopher36448af2014-10-01 20:38:26 +0000147
148 // Make sure 64-bit features are available when CPUname is generic
Daniel Sanders335487a2015-06-16 13:15:50 +0000149 if (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le) {
Eric Christopher36448af2014-10-01 20:38:26 +0000150 if (!FullFS.empty())
151 FullFS = "+64bit," + FullFS;
152 else
153 FullFS = "+64bit";
154 }
155
156 if (OL >= CodeGenOpt::Default) {
157 if (!FullFS.empty())
158 FullFS = "+crbits," + FullFS;
159 else
160 FullFS = "+crbits";
161 }
Hal Finkele2ab0f12015-01-15 21:17:34 +0000162
163 if (OL != CodeGenOpt::None) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000164 if (!FullFS.empty())
Hal Finkele2ab0f12015-01-15 21:17:34 +0000165 FullFS = "+invariant-function-descriptors," + FullFS;
166 else
167 FullFS = "+invariant-function-descriptors";
168 }
169
Eric Christopher36448af2014-10-01 20:38:26 +0000170 return FullFS;
171}
172
Aditya Nandakumara2719322014-11-13 09:26:31 +0000173static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
174 // If it isn't a Mach-O file then it's going to be a linux ELF
175 // object file.
176 if (TT.isOSDarwin())
Eugene Zelenko8187c192017-01-13 00:58:58 +0000177 return llvm::make_unique<TargetLoweringObjectFileMachO>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000178
Eugene Zelenko8187c192017-01-13 00:58:58 +0000179 return llvm::make_unique<PPC64LinuxTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000180}
181
Eric Christopherfee6aaf2015-02-17 06:45:15 +0000182static PPCTargetMachine::PPCABI computeTargetABI(const Triple &TT,
183 const TargetOptions &Options) {
Kit Barton7c80f982018-08-28 01:18:29 +0000184 if (TT.isOSDarwin())
185 report_fatal_error("Darwin is no longer supported for PowerPC");
186
Eric Christopherfee6aaf2015-02-17 06:45:15 +0000187 if (Options.MCOptions.getABIName().startswith("elfv1"))
188 return PPCTargetMachine::PPC_ABI_ELFv1;
189 else if (Options.MCOptions.getABIName().startswith("elfv2"))
190 return PPCTargetMachine::PPC_ABI_ELFv2;
191
192 assert(Options.MCOptions.getABIName().empty() &&
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000193 "Unknown target-abi option!");
Eric Christopherfee6aaf2015-02-17 06:45:15 +0000194
Eric Christopher5ec30ef2017-06-17 02:25:55 +0000195 if (TT.isMacOSX())
196 return PPCTargetMachine::PPC_ABI_UNKNOWN;
197
198 switch (TT.getArch()) {
199 case Triple::ppc64le:
200 return PPCTargetMachine::PPC_ABI_ELFv2;
201 case Triple::ppc64:
202 return PPCTargetMachine::PPC_ABI_ELFv1;
203 default:
204 return PPCTargetMachine::PPC_ABI_UNKNOWN;
Eric Christopherfee6aaf2015-02-17 06:45:15 +0000205 }
Eric Christopherfee6aaf2015-02-17 06:45:15 +0000206}
207
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000208static Reloc::Model getEffectiveRelocModel(const Triple &TT,
209 Optional<Reloc::Model> RM) {
Eric Christopherc70d07b2017-06-17 02:25:56 +0000210 if (RM.hasValue())
211 return *RM;
212
213 // Darwin defaults to dynamic-no-pic.
214 if (TT.isOSDarwin())
215 return Reloc::DynamicNoPIC;
216
Stefan Pintilie90044442018-11-16 19:24:23 +0000217 // Non-darwin 64-bit platforms are PIC by default.
218 if (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le)
219 return Reloc::PIC_;
220
221 // 32-bit is static by default.
Eric Christopherc70d07b2017-06-17 02:25:56 +0000222 return Reloc::Static;
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000223}
224
Rafael Espindola79e238a2017-08-03 02:16:21 +0000225static CodeModel::Model getEffectiveCodeModel(const Triple &TT,
Rafael Espindola278346952017-08-03 04:52:45 +0000226 Optional<CodeModel::Model> CM,
227 bool JIT) {
Rafael Espindola79e238a2017-08-03 02:16:21 +0000228 if (CM)
229 return *CM;
Rafael Espindola278346952017-08-03 04:52:45 +0000230 if (!TT.isOSDarwin() && !JIT &&
Rafael Espindola79e238a2017-08-03 02:16:21 +0000231 (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le))
232 return CodeModel::Medium;
233 return CodeModel::Small;
234}
235
NAKAMURA Takumi84965032015-09-22 11:14:12 +0000236// The FeatureString here is a little subtle. We are modifying the feature
237// string with what are (currently) non-function specific overrides as it goes
Matthias Braunbb8507e2017-10-12 22:57:28 +0000238// into the LLVMTargetMachine constructor and then using the stored value in the
Eric Christopher36448af2014-10-01 20:38:26 +0000239// Subtarget constructor below it.
Daniel Sanders3e5de882015-06-11 19:41:26 +0000240PPCTargetMachine::PPCTargetMachine(const Target &T, const Triple &TT,
241 StringRef CPU, StringRef FS,
242 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000243 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000244 Optional<CodeModel::Model> CM,
245 CodeGenOpt::Level OL, bool JIT)
Matthias Braunbb8507e2017-10-12 22:57:28 +0000246 : LLVMTargetMachine(T, getDataLayoutString(TT), TT, CPU,
247 computeFSAdditions(FS, OL, TT), Options,
248 getEffectiveRelocModel(TT, RM),
249 getEffectiveCodeModel(TT, CM, JIT), OL),
Daniel Sandersc81f4502015-06-16 15:44:21 +0000250 TLOF(createTLOF(getTargetTriple())),
Eric Christopher380611a2017-04-06 23:01:30 +0000251 TargetABI(computeTargetABI(TT, Options)) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000252 initAsmInfo();
Nate Begeman6cca84e2005-10-16 05:39:50 +0000253}
254
Eugene Zelenko8187c192017-01-13 00:58:58 +0000255PPCTargetMachine::~PPCTargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000256
Eric Christopher3faf2f12014-10-06 06:45:36 +0000257const PPCSubtarget *
258PPCTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +0000259 Attribute CPUAttr = F.getFnAttribute("target-cpu");
260 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000261
262 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
263 ? CPUAttr.getValueAsString().str()
264 : TargetCPU;
265 std::string FS = !FSAttr.hasAttribute(Attribute::None)
266 ? FSAttr.getValueAsString().str()
267 : TargetFS;
268
Petar Jovanovic280f7102015-12-14 17:57:33 +0000269 // FIXME: This is related to the code below to reset the target options,
270 // we need to know whether or not the soft float flag is set on the
271 // function before we can generate a subtarget. We also need to use
272 // it as a key for the subtarget since that can be the only difference
273 // between two functions.
274 bool SoftFloat =
Nirav Dave8dd66e52016-03-30 15:41:12 +0000275 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
Petar Jovanovic280f7102015-12-14 17:57:33 +0000276 // If the soft float attribute is set on the function turn on the soft float
277 // subtarget feature.
278 if (SoftFloat)
Hal Finkela9321052016-10-02 02:10:20 +0000279 FS += FS.empty() ? "-hard-float" : ",-hard-float";
Petar Jovanovic280f7102015-12-14 17:57:33 +0000280
Eric Christopher3faf2f12014-10-06 06:45:36 +0000281 auto &I = SubtargetMap[CPU + FS];
282 if (!I) {
283 // This needs to be done before we create a new subtarget since any
284 // creation will depend on the TM and the code generation flags on the
285 // function that reside in TargetOptions.
286 resetTargetOptions(F);
Eric Christophered1042b2015-03-26 00:50:23 +0000287 I = llvm::make_unique<PPCSubtarget>(
Daniel Sandersc81f4502015-06-16 15:44:21 +0000288 TargetTriple, CPU,
Eric Christophered1042b2015-03-26 00:50:23 +0000289 // FIXME: It would be good to have the subtarget additions here
290 // not necessary. Anything that turns them on/off (overrides) ends
291 // up being put at the end of the feature string, but the defaults
292 // shouldn't require adding them. Fixing this means pulling Feature64Bit
293 // out of most of the target cpus in the .td file and making it set only
294 // as part of initialization via the TargetTriple.
295 computeFSAdditions(FS, getOptLevel(), getTargetTriple()), *this);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000296 }
297 return I.get();
298}
Misha Brukmanb4402432005-04-21 23:30:14 +0000299
Chris Lattner12e97302006-09-04 04:14:57 +0000300//===----------------------------------------------------------------------===//
301// Pass Pipeline Configuration
302//===----------------------------------------------------------------------===//
Nate Begemanf17ea0f2004-08-11 07:40:04 +0000303
Andrew Trickccb67362012-02-03 05:12:41 +0000304namespace {
Eugene Zelenko8187c192017-01-13 00:58:58 +0000305
Andrew Trickccb67362012-02-03 05:12:41 +0000306/// PPC Code Generator Pass Configuration Options.
307class PPCPassConfig : public TargetPassConfig {
308public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000309 PPCPassConfig(PPCTargetMachine &TM, PassManagerBase &PM)
Stefan Pintiliecb4f0c52018-07-04 18:54:25 +0000310 : TargetPassConfig(TM, PM) {
311 // At any optimization level above -O0 we use the Machine Scheduler and not
312 // the default Post RA List Scheduler.
313 if (TM.getOptLevel() != CodeGenOpt::None)
314 substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
315 }
Andrew Trickccb67362012-02-03 05:12:41 +0000316
317 PPCTargetMachine &getPPCTargetMachine() const {
318 return getTM<PPCTargetMachine>();
319 }
320
Robin Morisset22129962014-09-23 20:46:49 +0000321 void addIRPasses() override;
Craig Topper0d3fa922014-04-29 07:57:37 +0000322 bool addPreISel() override;
323 bool addILPOpts() override;
324 bool addInstSelector() override;
Bill Schmidtfe723b92015-04-27 19:57:34 +0000325 void addMachineSSAOptimization() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000326 void addPreRegAlloc() override;
327 void addPreSched2() override;
328 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000329};
Eugene Zelenko8187c192017-01-13 00:58:58 +0000330
331} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000332
Andrew Trickf8ea1082012-02-04 02:56:59 +0000333TargetPassConfig *PPCTargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000334 return new PPCPassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000335}
336
Robin Morisset22129962014-09-23 20:46:49 +0000337void PPCPassConfig::addIRPasses() {
Kit Bartona1c712f2015-12-07 20:50:29 +0000338 if (TM->getOptLevel() != CodeGenOpt::None)
Eric Christopher9fd267c2017-03-31 02:16:54 +0000339 addPass(createPPCBoolRetToIntPass());
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000340 addPass(createAtomicExpandPass());
Hal Finkelf413be12014-11-21 04:35:51 +0000341
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000342 // For the BG/Q (or if explicitly requested), add explicit data prefetch
343 // intrinsics.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000344 bool UsePrefetching = TM->getTargetTriple().getVendor() == Triple::BGQ &&
345 getOptLevel() != CodeGenOpt::None;
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000346 if (EnablePrefetch.getNumOccurrences() > 0)
347 UsePrefetching = EnablePrefetch;
348 if (UsePrefetching)
Adam Nemet9d9cb272016-02-18 21:38:19 +0000349 addPass(createLoopDataPrefetchPass());
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000350
Ehsan Amiri4701a912016-04-07 15:30:55 +0000351 if (TM->getOptLevel() >= CodeGenOpt::Default && EnableGEPOpt) {
Hal Finkelf413be12014-11-21 04:35:51 +0000352 // Call SeparateConstOffsetFromGEP pass to extract constants within indices
353 // and lower a GEP with multiple indices to either arithmetic operations or
354 // multiple GEPs with single index.
David Blaikie8ad9a972018-03-28 22:28:50 +0000355 addPass(createSeparateConstOffsetFromGEPPass(true));
Hal Finkelf413be12014-11-21 04:35:51 +0000356 // Call EarlyCSE pass to find and remove subexpressions in the lowered
357 // result.
358 addPass(createEarlyCSEPass());
359 // Do loop invariant code motion in case part of the lowered result is
360 // invariant.
361 addPass(createLICMPass());
362 }
363
Robin Morisset22129962014-09-23 20:46:49 +0000364 TargetPassConfig::addIRPasses();
365}
366
Hal Finkel25c19922013-05-15 21:37:41 +0000367bool PPCPassConfig::addPreISel() {
Hal Finkelc9dd0202015-02-05 18:43:00 +0000368 if (!DisablePreIncPrep && getOptLevel() != CodeGenOpt::None)
369 addPass(createPPCLoopPreIncPrepPass(getPPCTargetMachine()));
370
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000371 if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
Eric Christopherb16eacf2017-06-29 23:28:45 +0000372 addPass(createPPCCTRLoops());
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000373
374 return false;
375}
376
Hal Finkeled6a2852013-04-05 23:29:01 +0000377bool PPCPassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000378 addPass(&EarlyIfConverterID);
Hal Finkel5d36b232015-07-15 08:23:05 +0000379
380 if (EnableMachineCombinerPass)
381 addPass(&MachineCombinerID);
382
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000383 return true;
Hal Finkeled6a2852013-04-05 23:29:01 +0000384}
385
Andrew Trickccb67362012-02-03 05:12:41 +0000386bool PPCPassConfig::addInstSelector() {
Chris Lattnerc6aa8062005-08-17 19:33:30 +0000387 // Install an instruction selector.
Hiroshi Inoue51020282017-06-27 04:52:17 +0000388 addPass(createPPCISelDag(getPPCTargetMachine(), getOptLevel()));
Hal Finkel8ca38842013-05-20 16:08:17 +0000389
390#ifndef NDEBUG
391 if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
392 addPass(createPPCCTRLoopsVerify());
393#endif
394
Eric Christopherd71e4442014-05-22 01:21:35 +0000395 addPass(createPPCVSXCopyPass());
Nate Begemanf17ea0f2004-08-11 07:40:04 +0000396 return false;
397}
398
Bill Schmidtfe723b92015-04-27 19:57:34 +0000399void PPCPassConfig::addMachineSSAOptimization() {
Lei Huang34e66212017-09-12 18:39:11 +0000400 // PPCBranchCoalescingPass need to be done before machine sinking
401 // since it merges empty blocks.
402 if (EnableBranchCoalescing && getOptLevel() != CodeGenOpt::None)
403 addPass(createPPCBranchCoalescingPass());
Bill Schmidtfe723b92015-04-27 19:57:34 +0000404 TargetPassConfig::addMachineSSAOptimization();
405 // For little endian, remove where possible the vector swap instructions
406 // introduced at code generation to normalize vector element order.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000407 if (TM->getTargetTriple().getArch() == Triple::ppc64le &&
Bill Schmidtfe723b92015-04-27 19:57:34 +0000408 !DisableVSXSwapRemoval)
409 addPass(createPPCVSXSwapRemovalPass());
Nemanja Ivanovic6f590bf2017-12-13 14:47:35 +0000410 // Reduce the number of cr-logical ops.
411 if (ReduceCRLogical && getOptLevel() != CodeGenOpt::None)
412 addPass(createPPCReduceCRLogicalsPass());
Bill Schmidt34af5e12015-11-10 21:38:26 +0000413 // Target-specific peephole cleanups performed after instruction
414 // selection.
415 if (!DisableMIPeephole) {
416 addPass(createPPCMIPeepholePass());
417 addPass(&DeadMachineInstructionElimID);
418 }
Bill Schmidtfe723b92015-04-27 19:57:34 +0000419}
420
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000421void PPCPassConfig::addPreRegAlloc() {
Andrew Kaylor289bd5f2016-04-27 19:39:32 +0000422 if (getOptLevel() != CodeGenOpt::None) {
423 initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
424 insertPass(VSXFMAMutateEarly ? &RegisterCoalescerID : &MachineSchedulerID,
425 &PPCVSXFMAMutateID);
426 }
Rafael Espindola248cfb92016-06-28 12:49:12 +0000427
428 // FIXME: We probably don't need to run these for -fPIE.
429 if (getPPCTargetMachine().isPositionIndependent()) {
Matthias Braunf84547c2016-04-28 23:42:51 +0000430 // FIXME: LiveVariables should not be necessary here!
Hiroshi Inouee7a35532017-06-20 17:53:33 +0000431 // PPCTLSDynamicCallPass uses LiveIntervals which previously dependent on
Matthias Braunf84547c2016-04-28 23:42:51 +0000432 // LiveVariables. This (unnecessary) dependency has been removed now,
433 // however a stage-2 clang build fails without LiveVariables computed here.
434 addPass(&LiveVariablesID, false);
Bill Schmidt82f1c772015-02-10 19:09:05 +0000435 addPass(createPPCTLSDynamicCallPass());
Matthias Braunf84547c2016-04-28 23:42:51 +0000436 }
Hal Finkel8340de12015-05-18 06:25:59 +0000437 if (EnableExtraTOCRegDeps)
438 addPass(createPPCTOCRegDepsPass());
Hal Finkel174e5902014-03-25 23:29:21 +0000439}
440
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000441void PPCPassConfig::addPreSched2() {
Hal Finkelfc353912016-03-31 20:39:41 +0000442 if (getOptLevel() != CodeGenOpt::None) {
Hal Finkel5711eca2013-04-09 22:58:37 +0000443 addPass(&IfConverterID);
Hal Finkelfc353912016-03-31 20:39:41 +0000444
445 // This optimization must happen after anything that might do store-to-load
446 // forwarding. Here we're after RA (and, thus, when spills are inserted)
447 // but before post-RA scheduling.
448 if (!DisableQPXLoadSplat)
449 addPass(createPPCQPXLoadSplatPass());
450 }
Hal Finkel5711eca2013-04-09 22:58:37 +0000451}
452
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000453void PPCPassConfig::addPreEmitPass() {
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +0000454 addPass(createPPCPreEmitPeepholePass());
Tony Jiang8e8c4442017-01-16 20:12:26 +0000455 addPass(createPPCExpandISELPass());
456
Hal Finkelb5aa7e52013-04-08 16:24:03 +0000457 if (getOptLevel() != CodeGenOpt::None)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000458 addPass(createPPCEarlyReturnPass(), false);
Chris Lattner12e97302006-09-04 04:14:57 +0000459 // Must run branch selection immediately preceding the asm printer.
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000460 addPass(createPPCBranchSelectionPass(), false);
Chris Lattner12e97302006-09-04 04:14:57 +0000461}
462
Sanjoy Das26d11ca2017-12-22 18:21:59 +0000463TargetTransformInfo
464PPCTargetMachine::getTargetTransformInfo(const Function &F) {
465 return TargetTransformInfo(PPCTTIImpl(this, F));
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000466}