blob: b219e93dabe4a1a0106d2b56bc19ff05031d3f48 [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
Hal Finkelf413be12014-11-21 04:35:51 +000041static cl::opt<bool>
42EnableGEPOpt("ppc-gep-opt", cl::Hidden,
43 cl::desc("Enable optimizations on complex GEPs"),
44 cl::init(true));
45
Hal Finkele5aaf3f2015-02-20 05:08:21 +000046static cl::opt<bool>
47EnablePrefetch("enable-ppc-prefetching",
48 cl::desc("disable software prefetching on PPC"),
49 cl::init(false), cl::Hidden);
50
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000051extern "C" void LLVMInitializePowerPCTarget() {
52 // Register the targets
Andrew Trick808a7a62012-02-03 05:12:30 +000053 RegisterTargetMachine<PPC32TargetMachine> A(ThePPC32Target);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000054 RegisterTargetMachine<PPC64TargetMachine> B(ThePPC64Target);
Bill Schmidt0a9170d2013-07-26 01:35:43 +000055 RegisterTargetMachine<PPC64TargetMachine> C(ThePPC64LETarget);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000056}
Douglas Gregor1b731d52009-06-16 20:12:29 +000057
Eric Christopher8b770652015-01-26 19:03:15 +000058/// Return the datalayout string of a subtarget.
59static std::string getDataLayoutString(const Triple &T) {
60 bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
61 std::string Ret;
62
63 // Most PPC* platforms are big endian, PPC64LE is little endian.
64 if (T.getArch() == Triple::ppc64le)
65 Ret = "e";
66 else
67 Ret = "E";
68
69 Ret += DataLayout::getManglingComponent(T);
70
71 // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
72 // pointers.
73 if (!is64Bit || T.getOS() == Triple::Lv2)
74 Ret += "-p:32:32";
75
76 // Note, the alignment values for f64 and i64 on ppc64 in Darwin
77 // documentation are wrong; these are correct (i.e. "what gcc does").
78 if (is64Bit || !T.isOSDarwin())
79 Ret += "-i64:64";
80 else
81 Ret += "-f64:32:64";
82
83 // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
84 if (is64Bit)
85 Ret += "-n32:64";
86 else
87 Ret += "-n32";
88
89 return Ret;
90}
91
Eric Christopher36448af2014-10-01 20:38:26 +000092static std::string computeFSAdditions(StringRef FS, CodeGenOpt::Level OL, StringRef TT) {
93 std::string FullFS = FS;
94 Triple TargetTriple(TT);
95
96 // Make sure 64-bit features are available when CPUname is generic
97 if (TargetTriple.getArch() == Triple::ppc64 ||
98 TargetTriple.getArch() == Triple::ppc64le) {
99 if (!FullFS.empty())
100 FullFS = "+64bit," + FullFS;
101 else
102 FullFS = "+64bit";
103 }
104
105 if (OL >= CodeGenOpt::Default) {
106 if (!FullFS.empty())
107 FullFS = "+crbits," + FullFS;
108 else
109 FullFS = "+crbits";
110 }
Hal Finkele2ab0f12015-01-15 21:17:34 +0000111
112 if (OL != CodeGenOpt::None) {
113 if (!FullFS.empty())
114 FullFS = "+invariant-function-descriptors," + FullFS;
115 else
116 FullFS = "+invariant-function-descriptors";
117 }
118
Eric Christopher36448af2014-10-01 20:38:26 +0000119 return FullFS;
120}
121
Aditya Nandakumara2719322014-11-13 09:26:31 +0000122static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
123 // If it isn't a Mach-O file then it's going to be a linux ELF
124 // object file.
125 if (TT.isOSDarwin())
126 return make_unique<TargetLoweringObjectFileMachO>();
127
128 return make_unique<PPC64LinuxTargetObjectFile>();
129}
130
Eric Christopherfee6aaf2015-02-17 06:45:15 +0000131static PPCTargetMachine::PPCABI computeTargetABI(const Triple &TT,
132 const TargetOptions &Options) {
133 if (Options.MCOptions.getABIName().startswith("elfv1"))
134 return PPCTargetMachine::PPC_ABI_ELFv1;
135 else if (Options.MCOptions.getABIName().startswith("elfv2"))
136 return PPCTargetMachine::PPC_ABI_ELFv2;
137
138 assert(Options.MCOptions.getABIName().empty() &&
139 "Unknown target-abi option!");
140
141 if (!TT.isMacOSX()) {
142 switch (TT.getArch()) {
143 case Triple::ppc64le:
144 return PPCTargetMachine::PPC_ABI_ELFv2;
145 case Triple::ppc64:
146 return PPCTargetMachine::PPC_ABI_ELFv1;
147 default:
148 // Fallthrough.
149 ;
150 }
151 }
152 return PPCTargetMachine::PPC_ABI_UNKNOWN;
153}
154
Eric Christopher36448af2014-10-01 20:38:26 +0000155// The FeatureString here is a little subtle. We are modifying the feature string
156// with what are (currently) non-function specific overrides as it goes into the
157// LLVMTargetMachine constructor and then using the stored value in the
158// Subtarget constructor below it.
Eric Christophera475d5c2014-06-11 00:53:17 +0000159PPCTargetMachine::PPCTargetMachine(const Target &T, StringRef TT, StringRef CPU,
160 StringRef FS, const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +0000161 Reloc::Model RM, CodeModel::Model CM,
Eric Christopher3770cf52014-08-09 04:38:56 +0000162 CodeGenOpt::Level OL)
Eric Christopher36448af2014-10-01 20:38:26 +0000163 : LLVMTargetMachine(T, TT, CPU, computeFSAdditions(FS, OL, TT), Options, RM,
164 CM, OL),
Aditya Nandakumara2719322014-11-13 09:26:31 +0000165 TLOF(createTLOF(Triple(getTargetTriple()))),
Eric Christopherfee6aaf2015-02-17 06:45:15 +0000166 TargetABI(computeTargetABI(Triple(TT), Options)),
Eric Christopher8b770652015-01-26 19:03:15 +0000167 DL(getDataLayoutString(Triple(TT))), Subtarget(TT, CPU, TargetFS, *this) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000168 initAsmInfo();
Nate Begeman6cca84e2005-10-16 05:39:50 +0000169}
170
Reid Kleckner357600e2014-11-20 23:37:18 +0000171PPCTargetMachine::~PPCTargetMachine() {}
172
David Blaikiea379b1812011-12-20 02:50:00 +0000173void PPC32TargetMachine::anchor() { }
174
Andrew Trick808a7a62012-02-03 05:12:30 +0000175PPC32TargetMachine::PPC32TargetMachine(const Target &T, StringRef TT,
Evan Chengefd9b422011-07-20 07:51:56 +0000176 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000177 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000178 Reloc::Model RM, CodeModel::Model CM,
179 CodeGenOpt::Level OL)
Eric Christopher3770cf52014-08-09 04:38:56 +0000180 : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {
Chris Lattner0c4aa142006-06-16 01:37:27 +0000181}
182
David Blaikiea379b1812011-12-20 02:50:00 +0000183void PPC64TargetMachine::anchor() { }
Chris Lattner0c4aa142006-06-16 01:37:27 +0000184
Andrew Trick808a7a62012-02-03 05:12:30 +0000185PPC64TargetMachine::PPC64TargetMachine(const Target &T, StringRef TT,
Evan Chengefd9b422011-07-20 07:51:56 +0000186 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000187 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000188 Reloc::Model RM, CodeModel::Model CM,
189 CodeGenOpt::Level OL)
Eric Christopher3770cf52014-08-09 04:38:56 +0000190 : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {
Chris Lattner0c4aa142006-06-16 01:37:27 +0000191}
192
Eric Christopher3faf2f12014-10-06 06:45:36 +0000193const PPCSubtarget *
194PPCTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +0000195 Attribute CPUAttr = F.getFnAttribute("target-cpu");
196 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000197
198 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
199 ? CPUAttr.getValueAsString().str()
200 : TargetCPU;
201 std::string FS = !FSAttr.hasAttribute(Attribute::None)
202 ? FSAttr.getValueAsString().str()
203 : TargetFS;
204
205 auto &I = SubtargetMap[CPU + FS];
206 if (!I) {
207 // This needs to be done before we create a new subtarget since any
208 // creation will depend on the TM and the code generation flags on the
209 // function that reside in TargetOptions.
210 resetTargetOptions(F);
211 I = llvm::make_unique<PPCSubtarget>(TargetTriple, CPU, FS, *this);
212 }
213 return I.get();
214}
Misha Brukmanb4402432005-04-21 23:30:14 +0000215
Chris Lattner12e97302006-09-04 04:14:57 +0000216//===----------------------------------------------------------------------===//
217// Pass Pipeline Configuration
218//===----------------------------------------------------------------------===//
Nate Begemanf17ea0f2004-08-11 07:40:04 +0000219
Andrew Trickccb67362012-02-03 05:12:41 +0000220namespace {
221/// PPC Code Generator Pass Configuration Options.
222class PPCPassConfig : public TargetPassConfig {
223public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000224 PPCPassConfig(PPCTargetMachine *TM, PassManagerBase &PM)
225 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000226
227 PPCTargetMachine &getPPCTargetMachine() const {
228 return getTM<PPCTargetMachine>();
229 }
230
Robin Morisset22129962014-09-23 20:46:49 +0000231 void addIRPasses() override;
Craig Topper0d3fa922014-04-29 07:57:37 +0000232 bool addPreISel() override;
233 bool addILPOpts() override;
234 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000235 void addPreRegAlloc() override;
236 void addPreSched2() override;
237 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000238};
239} // namespace
240
Andrew Trickf8ea1082012-02-04 02:56:59 +0000241TargetPassConfig *PPCTargetMachine::createPassConfig(PassManagerBase &PM) {
Hal Finkeleb50c2d2012-06-09 03:14:50 +0000242 return new PPCPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000243}
244
Robin Morisset22129962014-09-23 20:46:49 +0000245void PPCPassConfig::addIRPasses() {
246 addPass(createAtomicExpandPass(&getPPCTargetMachine()));
Hal Finkelf413be12014-11-21 04:35:51 +0000247
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000248 // For the BG/Q (or if explicitly requested), add explicit data prefetch
249 // intrinsics.
250 bool UsePrefetching =
251 Triple(TM->getTargetTriple()).getVendor() == Triple::BGQ &&
252 getOptLevel() != CodeGenOpt::None;
253 if (EnablePrefetch.getNumOccurrences() > 0)
254 UsePrefetching = EnablePrefetch;
255 if (UsePrefetching)
256 addPass(createPPCLoopDataPrefetchPass());
257
Hal Finkelf413be12014-11-21 04:35:51 +0000258 if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
259 // Call SeparateConstOffsetFromGEP pass to extract constants within indices
260 // and lower a GEP with multiple indices to either arithmetic operations or
261 // multiple GEPs with single index.
262 addPass(createSeparateConstOffsetFromGEPPass(TM, true));
263 // Call EarlyCSE pass to find and remove subexpressions in the lowered
264 // result.
265 addPass(createEarlyCSEPass());
266 // Do loop invariant code motion in case part of the lowered result is
267 // invariant.
268 addPass(createLICMPass());
269 }
270
Robin Morisset22129962014-09-23 20:46:49 +0000271 TargetPassConfig::addIRPasses();
272}
273
Hal Finkel25c19922013-05-15 21:37:41 +0000274bool PPCPassConfig::addPreISel() {
Hal Finkelc9dd0202015-02-05 18:43:00 +0000275 if (!DisablePreIncPrep && getOptLevel() != CodeGenOpt::None)
276 addPass(createPPCLoopPreIncPrepPass(getPPCTargetMachine()));
277
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000278 if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
Hal Finkel25c19922013-05-15 21:37:41 +0000279 addPass(createPPCCTRLoops(getPPCTargetMachine()));
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000280
281 return false;
282}
283
Hal Finkeled6a2852013-04-05 23:29:01 +0000284bool PPCPassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000285 addPass(&EarlyIfConverterID);
286 return true;
Hal Finkeled6a2852013-04-05 23:29:01 +0000287}
288
Andrew Trickccb67362012-02-03 05:12:41 +0000289bool PPCPassConfig::addInstSelector() {
Chris Lattnerc6aa8062005-08-17 19:33:30 +0000290 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000291 addPass(createPPCISelDag(getPPCTargetMachine()));
Hal Finkel8ca38842013-05-20 16:08:17 +0000292
293#ifndef NDEBUG
294 if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
295 addPass(createPPCCTRLoopsVerify());
296#endif
297
Eric Christopherd71e4442014-05-22 01:21:35 +0000298 addPass(createPPCVSXCopyPass());
Nate Begemanf17ea0f2004-08-11 07:40:04 +0000299 return false;
300}
301
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000302void PPCPassConfig::addPreRegAlloc() {
Eric Christopherd71e4442014-05-22 01:21:35 +0000303 initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
304 insertPass(VSXFMAMutateEarly ? &RegisterCoalescerID : &MachineSchedulerID,
305 &PPCVSXFMAMutateID);
Bill Schmidt82f1c772015-02-10 19:09:05 +0000306 if (getPPCTargetMachine().getRelocationModel() == Reloc::PIC_)
307 addPass(createPPCTLSDynamicCallPass());
Hal Finkel174e5902014-03-25 23:29:21 +0000308}
309
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000310void PPCPassConfig::addPreSched2() {
Hal Finkel5711eca2013-04-09 22:58:37 +0000311 if (getOptLevel() != CodeGenOpt::None)
312 addPass(&IfConverterID);
Hal Finkel5711eca2013-04-09 22:58:37 +0000313}
314
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000315void PPCPassConfig::addPreEmitPass() {
Hal Finkelb5aa7e52013-04-08 16:24:03 +0000316 if (getOptLevel() != CodeGenOpt::None)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000317 addPass(createPPCEarlyReturnPass(), false);
Chris Lattner12e97302006-09-04 04:14:57 +0000318 // Must run branch selection immediately preceding the asm printer.
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000319 addPass(createPPCBranchSelectionPass(), false);
Chris Lattner12e97302006-09-04 04:14:57 +0000320}
321
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000322TargetIRAnalysis PPCTargetMachine::getTargetIRAnalysis() {
323 return TargetIRAnalysis(
324 [this](Function &F) { return TargetTransformInfo(PPCTTIImpl(this, F)); });
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000325}