blob: f1adc35786e8b5b941783e7276aa29ef17d80e65 [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 Lattnerbfca1ab2005-10-14 23:51:18 +000014#include "PPC.h"
Jim Laskeyae92ce82006-09-07 23:39:26 +000015#include "PPCTargetAsmInfo.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000016#include "PPCTargetMachine.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000017#include "llvm/Module.h"
18#include "llvm/PassManager.h"
Chris Lattner4266f802004-07-11 02:48:49 +000019#include "llvm/Target/TargetMachineRegistry.h"
Dale Johannesenc31eb202008-07-31 18:13:12 +000020#include "llvm/Target/TargetOptions.h"
David Greenea31f96c2009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000022using namespace llvm;
23
Oscar Fuentesba4eb2a2008-11-15 21:36:30 +000024/// PowerPCTargetMachineModule - Note that this is used on hosts that
25/// cannot link in a library unless there are references into the
26/// library. In particular, it seems that it is not possible to get
27/// things to work on Win32 without this. Though it is unused, do not
28/// remove it.
29extern "C" int PowerPCTargetMachineModule;
30int PowerPCTargetMachineModule = 0;
31
Dan Gohmand78c4002008-05-13 00:00:25 +000032// Register the targets
33static RegisterTarget<PPC32TargetMachine>
Dan Gohman9c4b7d52008-10-14 20:25:08 +000034X("ppc32", "PowerPC 32");
Dan Gohmand78c4002008-05-13 00:00:25 +000035static RegisterTarget<PPC64TargetMachine>
Dan Gohman9c4b7d52008-10-14 20:25:08 +000036Y("ppc64", "PowerPC 64");
Nate Begemanf17ea0f2004-08-11 07:40:04 +000037
Bob Wilson5a495fe2009-06-23 23:59:40 +000038// Force static initialization.
39extern "C" void LLVMInitializePowerPCTarget() { }
Douglas Gregor1b731d52009-06-16 20:12:29 +000040
Anton Korobeynikov28dc9d02008-08-17 13:54:28 +000041// No assembler printer by default
42PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0;
43
Jim Laskeyae92ce82006-09-07 23:39:26 +000044const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
Jim Laskey28663c72006-12-21 20:26:09 +000045 if (Subtarget.isDarwin())
Anton Korobeynikovbc780e32008-07-19 21:44:57 +000046 return new PPCDarwinTargetAsmInfo(*this);
Jim Laskey28663c72006-12-21 20:26:09 +000047 else
Anton Korobeynikovbc780e32008-07-19 21:44:57 +000048 return new PPCLinuxTargetAsmInfo(*this);
Jim Laskeyae92ce82006-09-07 23:39:26 +000049}
50
Chris Lattner0c4aa142006-06-16 01:37:27 +000051unsigned PPC32TargetMachine::getJITMatchQuality() {
Chris Lattner249edb82007-02-25 05:04:13 +000052#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
Chris Lattner0c4aa142006-06-16 01:37:27 +000053 if (sizeof(void*) == 4)
54 return 10;
Misha Brukmandcb0eec2004-07-12 23:36:12 +000055#endif
Chris Lattner1ec5e732006-07-12 20:42:10 +000056 return 0;
Misha Brukmandcb0eec2004-07-12 23:36:12 +000057}
Chris Lattner0c4aa142006-06-16 01:37:27 +000058unsigned PPC64TargetMachine::getJITMatchQuality() {
Chris Lattner249edb82007-02-25 05:04:13 +000059#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
Chris Lattner0c4aa142006-06-16 01:37:27 +000060 if (sizeof(void*) == 8)
Chris Lattner59b6e8a2006-07-06 17:10:42 +000061 return 10;
Chris Lattner0c4aa142006-06-16 01:37:27 +000062#endif
63 return 0;
64}
Misha Brukmandcb0eec2004-07-12 23:36:12 +000065
Chris Lattner0c4aa142006-06-16 01:37:27 +000066unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
Nate Begeman6cca84e2005-10-16 05:39:50 +000067 // We strongly match "powerpc-*".
68 std::string TT = M.getTargetTriple();
69 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
70 return 20;
71
Chris Lattner517290a2007-07-09 17:25:29 +000072 // If the target triple is something non-powerpc, we don't match.
73 if (!TT.empty()) return 0;
74
Nate Begeman6cca84e2005-10-16 05:39:50 +000075 if (M.getEndianness() == Module::BigEndian &&
76 M.getPointerSize() == Module::Pointer32)
77 return 10; // Weak match
78 else if (M.getEndianness() != Module::AnyEndianness ||
79 M.getPointerSize() != Module::AnyPointerSize)
80 return 0; // Match for some other target
81
82 return getJITMatchQuality()/2;
83}
84
Chris Lattner0c4aa142006-06-16 01:37:27 +000085unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
86 // We strongly match "powerpc64-*".
87 std::string TT = M.getTargetTriple();
88 if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
89 return 20;
90
91 if (M.getEndianness() == Module::BigEndian &&
92 M.getPointerSize() == Module::Pointer64)
93 return 10; // Weak match
94 else if (M.getEndianness() != Module::AnyEndianness ||
95 M.getPointerSize() != Module::AnyPointerSize)
96 return 0; // Match for some other target
97
98 return getJITMatchQuality()/2;
99}
100
101
102PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
103 bool is64Bit)
Chris Lattnerf4646a72006-12-11 23:22:45 +0000104 : Subtarget(*this, M, FS, is64Bit),
Chris Lattner49cadab2006-06-17 00:01:04 +0000105 DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
Chris Lattner572e2382006-11-18 01:34:43 +0000106 FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
Bill Wendling40cb8112007-01-24 03:41:36 +0000107 InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
Chris Lattner0c4aa142006-06-16 01:37:27 +0000108
Anton Korobeynikov40d67c52008-02-20 11:22:39 +0000109 if (getRelocationModel() == Reloc::Default) {
Evan Cheng73136df2006-02-22 20:19:42 +0000110 if (Subtarget.isDarwin())
111 setRelocationModel(Reloc::DynamicNoPIC);
112 else
Jim Laskey28663c72006-12-21 20:26:09 +0000113 setRelocationModel(Reloc::Static);
Anton Korobeynikov40d67c52008-02-20 11:22:39 +0000114 }
Nate Begeman6cca84e2005-10-16 05:39:50 +0000115}
116
Dale Johannesen82810c82007-05-22 17:14:46 +0000117/// Override this for PowerPC. Tail merging happily breaks up instruction issue
118/// groups, which typically degrades performance.
Dan Gohmanaad83c82007-11-19 20:46:23 +0000119bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
Dale Johannesen82810c82007-05-22 17:14:46 +0000120
Chris Lattner0c4aa142006-06-16 01:37:27 +0000121PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
122 : PPCTargetMachine(M, FS, false) {
123}
124
125
126PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
127 : PPCTargetMachine(M, FS, true) {
128}
129
Misha Brukmanb4402432005-04-21 23:30:14 +0000130
Chris Lattner12e97302006-09-04 04:14:57 +0000131//===----------------------------------------------------------------------===//
132// Pass Pipeline Configuration
133//===----------------------------------------------------------------------===//
Nate Begemanf17ea0f2004-08-11 07:40:04 +0000134
Bill Wendling026e5d72009-04-29 23:29:43 +0000135bool PPCTargetMachine::addInstSelector(PassManagerBase &PM,
136 CodeGenOpt::Level OptLevel) {
Chris Lattnerc6aa8062005-08-17 19:33:30 +0000137 // Install an instruction selector.
Chris Lattner33792a42006-01-12 01:46:07 +0000138 PM.add(createPPCISelDag(*this));
Nate Begemanf17ea0f2004-08-11 07:40:04 +0000139 return false;
140}
141
Bill Wendling026e5d72009-04-29 23:29:43 +0000142bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM,
143 CodeGenOpt::Level OptLevel) {
Chris Lattner12e97302006-09-04 04:14:57 +0000144 // Must run branch selection immediately preceding the asm printer.
145 PM.add(createPPCBranchSelectionPass());
146 return false;
147}
148
Bill Wendling084669a2009-04-29 00:15:41 +0000149bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling026e5d72009-04-29 23:29:43 +0000150 CodeGenOpt::Level OptLevel,
Bill Wendling084669a2009-04-29 00:15:41 +0000151 bool Verbose,
David Greenea31f96c2009-07-14 20:18:05 +0000152 formatted_raw_ostream &Out) {
Anton Korobeynikov28dc9d02008-08-17 13:54:28 +0000153 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
154 if (AsmPrinterCtor)
Daniel Dunbar75c12e12009-07-01 01:48:54 +0000155 PM.add(AsmPrinterCtor(Out, *this, Verbose));
Anton Korobeynikov28dc9d02008-08-17 13:54:28 +0000156
Chris Lattner12e97302006-09-04 04:14:57 +0000157 return false;
158}
159
Bill Wendling026e5d72009-04-29 23:29:43 +0000160bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
161 CodeGenOpt::Level OptLevel,
Evan Cheng9d5df0a2007-07-20 21:56:13 +0000162 bool DumpAsm, MachineCodeEmitter &MCE) {
Chris Lattner09fecf92006-12-08 04:54:03 +0000163 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
Chris Lattner12e97302006-09-04 04:14:57 +0000164 // FIXME: This should be moved to TargetJITInfo!!
Chris Lattner09fecf92006-12-08 04:54:03 +0000165 if (Subtarget.isPPC64()) {
166 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
167 // instructions to materialize arbitrary global variable + function +
168 // constant pool addresses.
169 setRelocationModel(Reloc::PIC_);
Dale Johannesenc31eb202008-07-31 18:13:12 +0000170 // Temporary workaround for the inability of PPC64 JIT to handle jump
171 // tables.
172 DisableJumpTables = true;
Chris Lattner09fecf92006-12-08 04:54:03 +0000173 } else {
174 setRelocationModel(Reloc::Static);
175 }
176
Chris Lattnerf4646a72006-12-11 23:22:45 +0000177 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
178 // writing?
179 Subtarget.SetJITMode();
Chris Lattner12e97302006-09-04 04:14:57 +0000180
181 // Machine code emitter pass for PowerPC.
Nate Begeman3cb39212006-08-23 21:08:52 +0000182 PM.add(createPPCCodeEmitterPass(*this, MCE));
Anton Korobeynikov28dc9d02008-08-17 13:54:28 +0000183 if (DumpAsm) {
184 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
185 if (AsmPrinterCtor)
David Greenea31f96c2009-07-14 20:18:05 +0000186 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Anton Korobeynikov28dc9d02008-08-17 13:54:28 +0000187 }
188
Nate Begeman3cb39212006-08-23 21:08:52 +0000189 return false;
190}
Chris Lattner12e97302006-09-04 04:14:57 +0000191
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000192bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
193 CodeGenOpt::Level OptLevel,
194 bool DumpAsm, JITCodeEmitter &JCE) {
195 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
196 // FIXME: This should be moved to TargetJITInfo!!
197 if (Subtarget.isPPC64()) {
198 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
199 // instructions to materialize arbitrary global variable + function +
200 // constant pool addresses.
201 setRelocationModel(Reloc::PIC_);
202 // Temporary workaround for the inability of PPC64 JIT to handle jump
203 // tables.
204 DisableJumpTables = true;
205 } else {
206 setRelocationModel(Reloc::Static);
207 }
208
209 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
210 // writing?
211 Subtarget.SetJITMode();
212
213 // Machine code emitter pass for PowerPC.
214 PM.add(createPPCJITCodeEmitterPass(*this, JCE));
215 if (DumpAsm) {
216 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
217 if (AsmPrinterCtor)
David Greenea31f96c2009-07-14 20:18:05 +0000218 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000219 }
220
221 return false;
222}
223
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000224bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
225 CodeGenOpt::Level OptLevel,
226 bool DumpAsm, ObjectCodeEmitter &OCE) {
227 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
228 // FIXME: This should be moved to TargetJITInfo!!
229 if (Subtarget.isPPC64()) {
230 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
231 // instructions to materialize arbitrary global variable + function +
232 // constant pool addresses.
233 setRelocationModel(Reloc::PIC_);
234 // Temporary workaround for the inability of PPC64 JIT to handle jump
235 // tables.
236 DisableJumpTables = true;
237 } else {
238 setRelocationModel(Reloc::Static);
239 }
240
241 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
242 // writing?
243 Subtarget.SetJITMode();
244
245 // Machine code emitter pass for PowerPC.
246 PM.add(createPPCObjectCodeEmitterPass(*this, OCE));
247 if (DumpAsm) {
248 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
249 if (AsmPrinterCtor)
David Greenea31f96c2009-07-14 20:18:05 +0000250 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000251 }
252
253 return false;
254}
255
Bill Wendling026e5d72009-04-29 23:29:43 +0000256bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
257 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000258 bool DumpAsm,
259 MachineCodeEmitter &MCE) {
Bill Wendling2aa9a422007-02-08 01:39:44 +0000260 // Machine code emitter pass for PowerPC.
261 PM.add(createPPCCodeEmitterPass(*this, MCE));
Anton Korobeynikov28dc9d02008-08-17 13:54:28 +0000262 if (DumpAsm) {
263 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
264 if (AsmPrinterCtor)
David Greenea31f96c2009-07-14 20:18:05 +0000265 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Anton Korobeynikov28dc9d02008-08-17 13:54:28 +0000266 }
267
Bill Wendling2aa9a422007-02-08 01:39:44 +0000268 return false;
269}
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000270
271bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
272 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000273 bool DumpAsm,
274 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000275 // Machine code emitter pass for PowerPC.
276 PM.add(createPPCJITCodeEmitterPass(*this, JCE));
277 if (DumpAsm) {
278 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
279 if (AsmPrinterCtor)
David Greenea31f96c2009-07-14 20:18:05 +0000280 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000281 }
282
283 return false;
284}
285
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000286bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
287 CodeGenOpt::Level OptLevel,
288 bool DumpAsm,
289 ObjectCodeEmitter &OCE) {
290 // Machine code emitter pass for PowerPC.
291 PM.add(createPPCObjectCodeEmitterPass(*this, OCE));
292 if (DumpAsm) {
293 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
294 if (AsmPrinterCtor)
David Greenea31f96c2009-07-14 20:18:05 +0000295 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000296 }
297
298 return false;
299}
300
301