blob: 08ecc6fb9b57b8f0460b0a65f694c73ff5313fd0 [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 Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCStreamer.h"
21#include "llvm/PassManager.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 Finkel174e5902014-03-25 23:29:21 +000033static cl::opt<bool>
34VSXFMAMutateEarly("schedule-ppc-vsx-fma-mutation-early",
35 cl::Hidden, cl::desc("Schedule VSX FMA instruction mutation early"));
36
Hal Finkelf413be12014-11-21 04:35:51 +000037static cl::opt<bool>
38EnableGEPOpt("ppc-gep-opt", cl::Hidden,
39 cl::desc("Enable optimizations on complex GEPs"),
40 cl::init(true));
41
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000042extern "C" void LLVMInitializePowerPCTarget() {
43 // Register the targets
Andrew Trick808a7a62012-02-03 05:12:30 +000044 RegisterTargetMachine<PPC32TargetMachine> A(ThePPC32Target);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000045 RegisterTargetMachine<PPC64TargetMachine> B(ThePPC64Target);
Bill Schmidt0a9170d2013-07-26 01:35:43 +000046 RegisterTargetMachine<PPC64TargetMachine> C(ThePPC64LETarget);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000047}
Douglas Gregor1b731d52009-06-16 20:12:29 +000048
Eric Christopher8b770652015-01-26 19:03:15 +000049/// Return the datalayout string of a subtarget.
50static std::string getDataLayoutString(const Triple &T) {
51 bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
52 std::string Ret;
53
54 // Most PPC* platforms are big endian, PPC64LE is little endian.
55 if (T.getArch() == Triple::ppc64le)
56 Ret = "e";
57 else
58 Ret = "E";
59
60 Ret += DataLayout::getManglingComponent(T);
61
62 // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
63 // pointers.
64 if (!is64Bit || T.getOS() == Triple::Lv2)
65 Ret += "-p:32:32";
66
67 // Note, the alignment values for f64 and i64 on ppc64 in Darwin
68 // documentation are wrong; these are correct (i.e. "what gcc does").
69 if (is64Bit || !T.isOSDarwin())
70 Ret += "-i64:64";
71 else
72 Ret += "-f64:32:64";
73
74 // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
75 if (is64Bit)
76 Ret += "-n32:64";
77 else
78 Ret += "-n32";
79
80 return Ret;
81}
82
Eric Christopher36448af2014-10-01 20:38:26 +000083static std::string computeFSAdditions(StringRef FS, CodeGenOpt::Level OL, StringRef TT) {
84 std::string FullFS = FS;
85 Triple TargetTriple(TT);
86
87 // Make sure 64-bit features are available when CPUname is generic
88 if (TargetTriple.getArch() == Triple::ppc64 ||
89 TargetTriple.getArch() == Triple::ppc64le) {
90 if (!FullFS.empty())
91 FullFS = "+64bit," + FullFS;
92 else
93 FullFS = "+64bit";
94 }
95
96 if (OL >= CodeGenOpt::Default) {
97 if (!FullFS.empty())
98 FullFS = "+crbits," + FullFS;
99 else
100 FullFS = "+crbits";
101 }
Hal Finkele2ab0f12015-01-15 21:17:34 +0000102
103 if (OL != CodeGenOpt::None) {
104 if (!FullFS.empty())
105 FullFS = "+invariant-function-descriptors," + FullFS;
106 else
107 FullFS = "+invariant-function-descriptors";
108 }
109
Eric Christopher36448af2014-10-01 20:38:26 +0000110 return FullFS;
111}
112
Aditya Nandakumara2719322014-11-13 09:26:31 +0000113static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
114 // If it isn't a Mach-O file then it's going to be a linux ELF
115 // object file.
116 if (TT.isOSDarwin())
117 return make_unique<TargetLoweringObjectFileMachO>();
118
119 return make_unique<PPC64LinuxTargetObjectFile>();
120}
121
Eric Christopher36448af2014-10-01 20:38:26 +0000122// The FeatureString here is a little subtle. We are modifying the feature string
123// with what are (currently) non-function specific overrides as it goes into the
124// LLVMTargetMachine constructor and then using the stored value in the
125// Subtarget constructor below it.
Eric Christophera475d5c2014-06-11 00:53:17 +0000126PPCTargetMachine::PPCTargetMachine(const Target &T, StringRef TT, StringRef CPU,
127 StringRef FS, const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +0000128 Reloc::Model RM, CodeModel::Model CM,
Eric Christopher3770cf52014-08-09 04:38:56 +0000129 CodeGenOpt::Level OL)
Eric Christopher36448af2014-10-01 20:38:26 +0000130 : LLVMTargetMachine(T, TT, CPU, computeFSAdditions(FS, OL, TT), Options, RM,
131 CM, OL),
Aditya Nandakumara2719322014-11-13 09:26:31 +0000132 TLOF(createTLOF(Triple(getTargetTriple()))),
Eric Christopher8b770652015-01-26 19:03:15 +0000133 DL(getDataLayoutString(Triple(TT))), Subtarget(TT, CPU, TargetFS, *this) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000134 initAsmInfo();
Nate Begeman6cca84e2005-10-16 05:39:50 +0000135}
136
Reid Kleckner357600e2014-11-20 23:37:18 +0000137PPCTargetMachine::~PPCTargetMachine() {}
138
David Blaikiea379b1812011-12-20 02:50:00 +0000139void PPC32TargetMachine::anchor() { }
140
Andrew Trick808a7a62012-02-03 05:12:30 +0000141PPC32TargetMachine::PPC32TargetMachine(const Target &T, StringRef TT,
Evan Chengefd9b422011-07-20 07:51:56 +0000142 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000143 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000144 Reloc::Model RM, CodeModel::Model CM,
145 CodeGenOpt::Level OL)
Eric Christopher3770cf52014-08-09 04:38:56 +0000146 : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {
Chris Lattner0c4aa142006-06-16 01:37:27 +0000147}
148
David Blaikiea379b1812011-12-20 02:50:00 +0000149void PPC64TargetMachine::anchor() { }
Chris Lattner0c4aa142006-06-16 01:37:27 +0000150
Andrew Trick808a7a62012-02-03 05:12:30 +0000151PPC64TargetMachine::PPC64TargetMachine(const Target &T, StringRef TT,
Evan Chengefd9b422011-07-20 07:51:56 +0000152 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000153 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000154 Reloc::Model RM, CodeModel::Model CM,
155 CodeGenOpt::Level OL)
Eric Christopher3770cf52014-08-09 04:38:56 +0000156 : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {
Chris Lattner0c4aa142006-06-16 01:37:27 +0000157}
158
Eric Christopher3faf2f12014-10-06 06:45:36 +0000159const PPCSubtarget *
160PPCTargetMachine::getSubtargetImpl(const Function &F) const {
161 AttributeSet FnAttrs = F.getAttributes();
162 Attribute CPUAttr =
163 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
164 Attribute FSAttr =
165 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
166
167 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
168 ? CPUAttr.getValueAsString().str()
169 : TargetCPU;
170 std::string FS = !FSAttr.hasAttribute(Attribute::None)
171 ? FSAttr.getValueAsString().str()
172 : TargetFS;
173
174 auto &I = SubtargetMap[CPU + FS];
175 if (!I) {
176 // This needs to be done before we create a new subtarget since any
177 // creation will depend on the TM and the code generation flags on the
178 // function that reside in TargetOptions.
179 resetTargetOptions(F);
180 I = llvm::make_unique<PPCSubtarget>(TargetTriple, CPU, FS, *this);
181 }
182 return I.get();
183}
Misha Brukmanb4402432005-04-21 23:30:14 +0000184
Chris Lattner12e97302006-09-04 04:14:57 +0000185//===----------------------------------------------------------------------===//
186// Pass Pipeline Configuration
187//===----------------------------------------------------------------------===//
Nate Begemanf17ea0f2004-08-11 07:40:04 +0000188
Andrew Trickccb67362012-02-03 05:12:41 +0000189namespace {
190/// PPC Code Generator Pass Configuration Options.
191class PPCPassConfig : public TargetPassConfig {
192public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000193 PPCPassConfig(PPCTargetMachine *TM, PassManagerBase &PM)
194 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000195
196 PPCTargetMachine &getPPCTargetMachine() const {
197 return getTM<PPCTargetMachine>();
198 }
199
Robin Morisset22129962014-09-23 20:46:49 +0000200 void addIRPasses() override;
Craig Topper0d3fa922014-04-29 07:57:37 +0000201 bool addPreISel() override;
202 bool addILPOpts() override;
203 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000204 void addPreRegAlloc() override;
205 void addPreSched2() override;
206 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000207};
208} // namespace
209
Andrew Trickf8ea1082012-02-04 02:56:59 +0000210TargetPassConfig *PPCTargetMachine::createPassConfig(PassManagerBase &PM) {
Hal Finkeleb50c2d2012-06-09 03:14:50 +0000211 return new PPCPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000212}
213
Robin Morisset22129962014-09-23 20:46:49 +0000214void PPCPassConfig::addIRPasses() {
215 addPass(createAtomicExpandPass(&getPPCTargetMachine()));
Hal Finkelf413be12014-11-21 04:35:51 +0000216
217 if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
218 // Call SeparateConstOffsetFromGEP pass to extract constants within indices
219 // and lower a GEP with multiple indices to either arithmetic operations or
220 // multiple GEPs with single index.
221 addPass(createSeparateConstOffsetFromGEPPass(TM, true));
222 // Call EarlyCSE pass to find and remove subexpressions in the lowered
223 // result.
224 addPass(createEarlyCSEPass());
225 // Do loop invariant code motion in case part of the lowered result is
226 // invariant.
227 addPass(createLICMPass());
228 }
229
Robin Morisset22129962014-09-23 20:46:49 +0000230 TargetPassConfig::addIRPasses();
231}
232
Hal Finkel25c19922013-05-15 21:37:41 +0000233bool PPCPassConfig::addPreISel() {
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000234 if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
Hal Finkel25c19922013-05-15 21:37:41 +0000235 addPass(createPPCCTRLoops(getPPCTargetMachine()));
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000236
237 return false;
238}
239
Hal Finkeled6a2852013-04-05 23:29:01 +0000240bool PPCPassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000241 addPass(&EarlyIfConverterID);
242 return true;
Hal Finkeled6a2852013-04-05 23:29:01 +0000243}
244
Andrew Trickccb67362012-02-03 05:12:41 +0000245bool PPCPassConfig::addInstSelector() {
Chris Lattnerc6aa8062005-08-17 19:33:30 +0000246 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000247 addPass(createPPCISelDag(getPPCTargetMachine()));
Hal Finkel8ca38842013-05-20 16:08:17 +0000248
249#ifndef NDEBUG
250 if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
251 addPass(createPPCCTRLoopsVerify());
252#endif
253
Eric Christopherd71e4442014-05-22 01:21:35 +0000254 addPass(createPPCVSXCopyPass());
Nate Begemanf17ea0f2004-08-11 07:40:04 +0000255 return false;
256}
257
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000258void PPCPassConfig::addPreRegAlloc() {
Eric Christopherd71e4442014-05-22 01:21:35 +0000259 initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
260 insertPass(VSXFMAMutateEarly ? &RegisterCoalescerID : &MachineSchedulerID,
261 &PPCVSXFMAMutateID);
Hal Finkel174e5902014-03-25 23:29:21 +0000262}
263
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000264void PPCPassConfig::addPreSched2() {
265 addPass(createPPCVSXCopyCleanupPass(), false);
Hal Finkelc6fc9b82014-03-27 23:12:31 +0000266
Hal Finkel5711eca2013-04-09 22:58:37 +0000267 if (getOptLevel() != CodeGenOpt::None)
268 addPass(&IfConverterID);
Hal Finkel5711eca2013-04-09 22:58:37 +0000269}
270
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000271void PPCPassConfig::addPreEmitPass() {
Hal Finkelb5aa7e52013-04-08 16:24:03 +0000272 if (getOptLevel() != CodeGenOpt::None)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000273 addPass(createPPCEarlyReturnPass(), false);
Chris Lattner12e97302006-09-04 04:14:57 +0000274 // Must run branch selection immediately preceding the asm printer.
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000275 addPass(createPPCBranchSelectionPass(), false);
Chris Lattner12e97302006-09-04 04:14:57 +0000276}
277
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000278TargetIRAnalysis PPCTargetMachine::getTargetIRAnalysis() {
279 return TargetIRAnalysis(
280 [this](Function &F) { return TargetTransformInfo(PPCTTIImpl(this, F)); });
Hal Finkel4e5ca9e2013-01-25 23:05:59 +0000281}