blob: b9ae5b66b81a5e396317406f78047da2f96c2ee3 [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
Chris Lattner6f3b9542005-10-14 23:59:06 +000014#include "PPCTargetMachine.h"
Craig Topperb25fda92012-03-17 18:46:09 +000015#include "PPC.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000016#include "PPCTargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000017#include "PPCTargetTransformInfo.h"
Andrew Trickccb67362012-02-03 05:12:41 +000018#include "llvm/CodeGen/Passes.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000019#include "llvm/IR/Function.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000020#include "llvm/IR/LegacyPassManager.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCStreamer.h"
Hal Finkel96c2d4d2012-06-08 15:38:21 +000022#include "llvm/Support/CommandLine.h"
David Greenea31f96c2009-07-14 20:18:05 +000023#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000024#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Target/TargetOptions.h"
Hal Finkelf413be12014-11-21 04:35:51 +000026#include "llvm/Transforms/Scalar.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000027using namespace llvm;
28
Hal Finkel96c2d4d2012-06-08 15:38:21 +000029static cl::
Hal Finkelc6b5deb2012-06-08 19:19:53 +000030opt<bool> DisableCTRLoops("disable-ppc-ctrloops", cl::Hidden,
31 cl::desc("Disable CTR loops for PPC"));
Hal Finkel96c2d4d2012-06-08 15:38:21 +000032
Hal Finkelc9dd0202015-02-05 18:43:00 +000033static cl::
34opt<bool> DisablePreIncPrep("disable-ppc-preinc-prep", cl::Hidden,
35 cl::desc("Disable PPC loop preinc prep"));
36
Hal Finkel174e5902014-03-25 23:29:21 +000037static cl::opt<bool>
38VSXFMAMutateEarly("schedule-ppc-vsx-fma-mutation-early",
39 cl::Hidden, cl::desc("Schedule VSX FMA instruction mutation early"));
40
Bill Schmidtfe723b92015-04-27 19:57:34 +000041static cl::
42opt<bool> DisableVSXSwapRemoval("disable-ppc-vsx-swap-removal", cl::Hidden,
43 cl::desc("Disable VSX Swap Removal for PPC"));
44
Hal Finkelf413be12014-11-21 04:35:51 +000045static cl::opt<bool>
46EnableGEPOpt("ppc-gep-opt", cl::Hidden,
47 cl::desc("Enable optimizations on complex GEPs"),
48 cl::init(true));
49
Hal Finkele5aaf3f2015-02-20 05:08:21 +000050static cl::opt<bool>
51EnablePrefetch("enable-ppc-prefetching",
52 cl::desc("disable software prefetching on PPC"),
53 cl::init(false), cl::Hidden);
54
Hal Finkel8340de12015-05-18 06:25:59 +000055static cl::opt<bool>
56EnableExtraTOCRegDeps("enable-ppc-extra-toc-reg-deps",
57 cl::desc("Add extra TOC register dependencies"),
58 cl::init(true), cl::Hidden);
59
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000060extern "C" void LLVMInitializePowerPCTarget() {
61 // Register the targets
Andrew Trick808a7a62012-02-03 05:12:30 +000062 RegisterTargetMachine<PPC32TargetMachine> A(ThePPC32Target);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000063 RegisterTargetMachine<PPC64TargetMachine> B(ThePPC64Target);
Bill Schmidt0a9170d2013-07-26 01:35:43 +000064 RegisterTargetMachine<PPC64TargetMachine> C(ThePPC64LETarget);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000065}
Douglas Gregor1b731d52009-06-16 20:12:29 +000066
Eric Christopher8b770652015-01-26 19:03:15 +000067/// Return the datalayout string of a subtarget.
68static std::string getDataLayoutString(const Triple &T) {
69 bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
70 std::string Ret;
71
72 // Most PPC* platforms are big endian, PPC64LE is little endian.
73 if (T.getArch() == Triple::ppc64le)
74 Ret = "e";
75 else
76 Ret = "E";
77
78 Ret += DataLayout::getManglingComponent(T);
79
80 // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
81 // pointers.
82 if (!is64Bit || T.getOS() == Triple::Lv2)
83 Ret += "-p:32:32";
84
85 // Note, the alignment values for f64 and i64 on ppc64 in Darwin
86 // documentation are wrong; these are correct (i.e. "what gcc does").
87 if (is64Bit || !T.isOSDarwin())
88 Ret += "-i64:64";
89 else
90 Ret += "-f64:32:64";
91
92 // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
93 if (is64Bit)
94 Ret += "-n32:64";
95 else
96 Ret += "-n32";
97
98 return Ret;
99}
100
Daniel Sanders335487a2015-06-16 13:15:50 +0000101static std::string computeFSAdditions(StringRef FS, CodeGenOpt::Level OL,
102 const Triple &TT) {
Eric Christopher36448af2014-10-01 20:38:26 +0000103 std::string FullFS = FS;
Eric Christopher36448af2014-10-01 20:38:26 +0000104
105 // Make sure 64-bit features are available when CPUname is generic
Daniel Sanders335487a2015-06-16 13:15:50 +0000106 if (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le) {
Eric Christopher36448af2014-10-01 20:38:26 +0000107 if (!FullFS.empty())
108 FullFS = "+64bit," + FullFS;
109 else
110 FullFS = "+64bit";
111 }
112
113 if (OL >= CodeGenOpt::Default) {
114 if (!FullFS.empty())
115 FullFS = "+crbits," + FullFS;
116 else
117 FullFS = "+crbits";
118 }
Hal Finkele2ab0f12015-01-15 21:17:34 +0000119
120 if (OL != CodeGenOpt::None) {
121 if (!FullFS.empty())
122 FullFS = "+invariant-function-descriptors," + FullFS;
123 else
124 FullFS = "+invariant-function-descriptors";
125 }
126
Eric Christopher36448af2014-10-01 20:38:26 +0000127 return FullFS;
128}
129
Aditya Nandakumara2719322014-11-13 09:26:31 +0000130static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
131 // If it isn't a Mach-O file then it's going to be a linux ELF
132 // object file.
133 if (TT.isOSDarwin())
134 return make_unique<TargetLoweringObjectFileMachO>();
135
136 return make_unique<PPC64LinuxTargetObjectFile>();
137}
138
Eric Christopherfee6aaf2015-02-17 06:45:15 +0000139static PPCTargetMachine::PPCABI computeTargetABI(const Triple &TT,
140 const TargetOptions &Options) {
141 if (Options.MCOptions.getABIName().startswith("elfv1"))
142 return PPCTargetMachine::PPC_ABI_ELFv1;
143 else if (Options.MCOptions.getABIName().startswith("elfv2"))
144 return PPCTargetMachine::PPC_ABI_ELFv2;
145
146 assert(Options.MCOptions.getABIName().empty() &&
147 "Unknown target-abi option!");
148
149 if (!TT.isMacOSX()) {
150 switch (TT.getArch()) {
151 case Triple::ppc64le:
152 return PPCTargetMachine::PPC_ABI_ELFv2;
153 case Triple::ppc64:
154 return PPCTargetMachine::PPC_ABI_ELFv1;
155 default:
156 // Fallthrough.
157 ;
158 }
159 }
160 return PPCTargetMachine::PPC_ABI_UNKNOWN;
161}
162
Eric Christopher36448af2014-10-01 20:38:26 +0000163// The FeatureString here is a little subtle. We are modifying the feature string
164// with what are (currently) non-function specific overrides as it goes into the
165// LLVMTargetMachine constructor and then using the stored value in the
166// Subtarget constructor below it.
Daniel Sanders3e5de882015-06-11 19:41:26 +0000167PPCTargetMachine::PPCTargetMachine(const Target &T, const Triple &TT,
168 StringRef CPU, StringRef FS,
169 const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +0000170 Reloc::Model RM, CodeModel::Model CM,
Eric Christopher3770cf52014-08-09 04:38:56 +0000171 CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000172 : LLVMTargetMachine(T, getDataLayoutString(TT), TT, CPU,
Daniel Sanders335487a2015-06-16 13:15:50 +0000173 computeFSAdditions(FS, OL, TT), Options, RM, CM, OL),
Aditya Nandakumara2719322014-11-13 09:26:31 +0000174 TLOF(createTLOF(Triple(getTargetTriple()))),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000175 TargetABI(computeTargetABI(TT, Options)) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000176 initAsmInfo();
Nate Begeman6cca84e2005-10-16 05:39:50 +0000177}
178
Reid Kleckner357600e2014-11-20 23:37:18 +0000179PPCTargetMachine::~PPCTargetMachine() {}
180
David Blaikiea379b1812011-12-20 02:50:00 +0000181void PPC32TargetMachine::anchor() { }
182
Daniel Sanders3e5de882015-06-11 19:41:26 +0000183PPC32TargetMachine::PPC32TargetMachine(const Target &T, const Triple &TT,
Evan Chengefd9b422011-07-20 07:51:56 +0000184 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000185 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000186 Reloc::Model RM, CodeModel::Model CM,
187 CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000188 : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {}
Chris Lattner0c4aa142006-06-16 01:37:27 +0000189
David Blaikiea379b1812011-12-20 02:50:00 +0000190void PPC64TargetMachine::anchor() { }
Chris Lattner0c4aa142006-06-16 01:37:27 +0000191
Daniel Sanders3e5de882015-06-11 19:41:26 +0000192PPC64TargetMachine::PPC64TargetMachine(const Target &T, const Triple &TT,
193 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000194 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000195 Reloc::Model RM, CodeModel::Model CM,
196 CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000197 : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {}
Chris Lattner0c4aa142006-06-16 01:37:27 +0000198
Eric Christopher3faf2f12014-10-06 06:45:36 +0000199const PPCSubtarget *
200PPCTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +0000201 Attribute CPUAttr = F.getFnAttribute("target-cpu");
202 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000203
204 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
205 ? CPUAttr.getValueAsString().str()
206 : TargetCPU;
207 std::string FS = !FSAttr.hasAttribute(Attribute::None)
208 ? FSAttr.getValueAsString().str()
209 : TargetFS;
210
211 auto &I = SubtargetMap[CPU + FS];
212 if (!I) {
213 // This needs to be done before we create a new subtarget since any
214 // creation will depend on the TM and the code generation flags on the
215 // function that reside in TargetOptions.
216 resetTargetOptions(F);
Eric Christophered1042b2015-03-26 00:50:23 +0000217 I = llvm::make_unique<PPCSubtarget>(
Daniel Sandersa73f1fd2015-06-10 12:11:26 +0000218 Triple(TargetTriple), CPU,
Eric Christophered1042b2015-03-26 00:50:23 +0000219 // FIXME: It would be good to have the subtarget additions here
220 // not necessary. Anything that turns them on/off (overrides) ends
221 // up being put at the end of the feature string, but the defaults
222 // shouldn't require adding them. Fixing this means pulling Feature64Bit
223 // out of most of the target cpus in the .td file and making it set only
224 // as part of initialization via the TargetTriple.
225 computeFSAdditions(FS, getOptLevel(), getTargetTriple()), *this);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000226 }
227 return I.get();
228}
Misha Brukmanb4402432005-04-21 23:30:14 +0000229
Chris Lattner12e97302006-09-04 04:14:57 +0000230//===----------------------------------------------------------------------===//
231// Pass Pipeline Configuration
232//===----------------------------------------------------------------------===//
Nate Begemanf17ea0f2004-08-11 07:40:04 +0000233
Andrew Trickccb67362012-02-03 05:12:41 +0000234namespace {
235/// PPC Code Generator Pass Configuration Options.
236class PPCPassConfig : public TargetPassConfig {
237public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000238 PPCPassConfig(PPCTargetMachine *TM, PassManagerBase &PM)
239 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000240
241 PPCTargetMachine &getPPCTargetMachine() const {
242 return getTM<PPCTargetMachine>();
243 }
244
Robin Morisset22129962014-09-23 20:46:49 +0000245 void addIRPasses() override;
Craig Topper0d3fa922014-04-29 07:57:37 +0000246 bool addPreISel() override;
247 bool addILPOpts() override;
248 bool addInstSelector() override;
Bill Schmidtfe723b92015-04-27 19:57:34 +0000249 void addMachineSSAOptimization() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000250 void addPreRegAlloc() override;
251 void addPreSched2() override;
252 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000253};
254} // namespace
255
Andrew Trickf8ea1082012-02-04 02:56:59 +0000256TargetPassConfig *PPCTargetMachine::createPassConfig(PassManagerBase &PM) {
Hal Finkeleb50c2d2012-06-09 03:14:50 +0000257 return new PPCPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000258}
259
Robin Morisset22129962014-09-23 20:46:49 +0000260void PPCPassConfig::addIRPasses() {
261 addPass(createAtomicExpandPass(&getPPCTargetMachine()));
Hal Finkelf413be12014-11-21 04:35:51 +0000262
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000263 // For the BG/Q (or if explicitly requested), add explicit data prefetch
264 // intrinsics.
265 bool UsePrefetching =
266 Triple(TM->getTargetTriple()).getVendor() == Triple::BGQ &&
267 getOptLevel() != CodeGenOpt::None;
268 if (EnablePrefetch.getNumOccurrences() > 0)
269 UsePrefetching = EnablePrefetch;
270 if (UsePrefetching)
271 addPass(createPPCLoopDataPrefetchPass());
272
Hal Finkelf413be12014-11-21 04:35:51 +0000273 if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
274 // Call SeparateConstOffsetFromGEP pass to extract constants within indices
275 // and lower a GEP with multiple indices to either arithmetic operations or
276 // multiple GEPs with single index.
277 addPass(createSeparateConstOffsetFromGEPPass(TM, true));
278 // Call EarlyCSE pass to find and remove subexpressions in the lowered
279 // result.
280 addPass(createEarlyCSEPass());
281 // Do loop invariant code motion in case part of the lowered result is
282 // invariant.
283 addPass(createLICMPass());
284 }
285
Robin Morisset22129962014-09-23 20:46:49 +0000286 TargetPassConfig::addIRPasses();
287}
288
Hal Finkel25c19922013-05-15 21:37:41 +0000289bool PPCPassConfig::addPreISel() {
Hal Finkelc9dd0202015-02-05 18:43:00 +0000290 if (!DisablePreIncPrep && getOptLevel() != CodeGenOpt::None)
291 addPass(createPPCLoopPreIncPrepPass(getPPCTargetMachine()));
292
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000293 if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
Hal Finkel25c19922013-05-15 21:37:41 +0000294 addPass(createPPCCTRLoops(getPPCTargetMachine()));
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000295
296 return false;
297}
298
Hal Finkeled6a2852013-04-05 23:29:01 +0000299bool PPCPassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000300 addPass(&EarlyIfConverterID);
301 return true;
Hal Finkeled6a2852013-04-05 23:29:01 +0000302}
303
Andrew Trickccb67362012-02-03 05:12:41 +0000304bool PPCPassConfig::addInstSelector() {
Chris Lattnerc6aa8062005-08-17 19:33:30 +0000305 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000306 addPass(createPPCISelDag(getPPCTargetMachine()));
Hal Finkel8ca38842013-05-20 16:08:17 +0000307
308#ifndef NDEBUG
309 if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
310 addPass(createPPCCTRLoopsVerify());
311#endif
312
Eric Christopherd71e4442014-05-22 01:21:35 +0000313 addPass(createPPCVSXCopyPass());
Nate Begemanf17ea0f2004-08-11 07:40:04 +0000314 return false;
315}
316
Bill Schmidtfe723b92015-04-27 19:57:34 +0000317void PPCPassConfig::addMachineSSAOptimization() {
318 TargetPassConfig::addMachineSSAOptimization();
319 // For little endian, remove where possible the vector swap instructions
320 // introduced at code generation to normalize vector element order.
321 if (Triple(TM->getTargetTriple()).getArch() == Triple::ppc64le &&
322 !DisableVSXSwapRemoval)
323 addPass(createPPCVSXSwapRemovalPass());
324}
325
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000326void PPCPassConfig::addPreRegAlloc() {
Eric Christopherd71e4442014-05-22 01:21:35 +0000327 initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
328 insertPass(VSXFMAMutateEarly ? &RegisterCoalescerID : &MachineSchedulerID,
329 &PPCVSXFMAMutateID);
Bill Schmidt82f1c772015-02-10 19:09:05 +0000330 if (getPPCTargetMachine().getRelocationModel() == Reloc::PIC_)
331 addPass(createPPCTLSDynamicCallPass());
Hal Finkel8340de12015-05-18 06:25:59 +0000332 if (EnableExtraTOCRegDeps)
333 addPass(createPPCTOCRegDepsPass());
Hal Finkel174e5902014-03-25 23:29:21 +0000334}
335
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000336void PPCPassConfig::addPreSched2() {
Hal Finkel5711eca2013-04-09 22:58:37 +0000337 if (getOptLevel() != CodeGenOpt::None)
338 addPass(&IfConverterID);
Hal Finkel5711eca2013-04-09 22:58:37 +0000339}
340
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000341void PPCPassConfig::addPreEmitPass() {
Hal Finkelb5aa7e52013-04-08 16:24:03 +0000342 if (getOptLevel() != CodeGenOpt::None)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000343 addPass(createPPCEarlyReturnPass(), false);
Chris Lattner12e97302006-09-04 04:14:57 +0000344 // Must run branch selection immediately preceding the asm printer.
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000345 addPass(createPPCBranchSelectionPass(), false);
Chris Lattner12e97302006-09-04 04:14:57 +0000346}
347
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000348TargetIRAnalysis PPCTargetMachine::getTargetIRAnalysis() {
349 return TargetIRAnalysis(
350 [this](Function &F) { return TargetTransformInfo(PPCTTIImpl(this, F)); });
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000351}