blob: 914d6728c6d499d87e9ded8a32ae1ac640da4242 [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanb5f662f2005-04-21 23:30:14 +00007//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00009//
Chris Lattnerb0096bd2005-08-15 23:47:04 +000010// Top-level implementation for the PowerPC target.
Misha Brukman5dfe3a92004-06-21 16:55:25 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner26689592005-10-14 23:51:18 +000014#include "PPC.h"
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000015#include "PPCTargetAsmInfo.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000016#include "PPCTargetMachine.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000017#include "llvm/Module.h"
18#include "llvm/PassManager.h"
Chris Lattnerd36c9702004-07-11 02:48:49 +000019#include "llvm/Target/TargetMachineRegistry.h"
Dale Johannesen72324642008-07-31 18:13:12 +000020#include "llvm/Target/TargetOptions.h"
David Greene71847812009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000022using namespace llvm;
23
Oscar Fuentes92adc192008-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 Gohman844731a2008-05-13 00:00:25 +000032// Register the targets
Daniel Dunbar42467902009-07-15 09:22:31 +000033extern Target ThePPC32Target;
Dan Gohman844731a2008-05-13 00:00:25 +000034static RegisterTarget<PPC32TargetMachine>
Daniel Dunbar42467902009-07-15 09:22:31 +000035X(ThePPC32Target, "ppc32", "PowerPC 32");
36
37extern Target ThePPC64Target;
Dan Gohman844731a2008-05-13 00:00:25 +000038static RegisterTarget<PPC64TargetMachine>
Daniel Dunbar42467902009-07-15 09:22:31 +000039Y(ThePPC64Target, "ppc64", "PowerPC 64");
Nate Begeman7a4fe9b2004-08-11 07:40:04 +000040
Bob Wilsona96751f2009-06-23 23:59:40 +000041// Force static initialization.
42extern "C" void LLVMInitializePowerPCTarget() { }
Douglas Gregor1555a232009-06-16 20:12:29 +000043
Anton Korobeynikov06be9972008-08-17 13:54:28 +000044// No assembler printer by default
45PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0;
46
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000047const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
Jim Laskeybf111822006-12-21 20:26:09 +000048 if (Subtarget.isDarwin())
Anton Korobeynikovbadd8df2008-07-19 21:44:57 +000049 return new PPCDarwinTargetAsmInfo(*this);
Jim Laskeybf111822006-12-21 20:26:09 +000050 else
Anton Korobeynikovbadd8df2008-07-19 21:44:57 +000051 return new PPCLinuxTargetAsmInfo(*this);
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000052}
53
Chris Lattner94de9a82006-06-16 01:37:27 +000054unsigned PPC32TargetMachine::getJITMatchQuality() {
Chris Lattner456bc872007-02-25 05:04:13 +000055#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
Chris Lattner94de9a82006-06-16 01:37:27 +000056 if (sizeof(void*) == 4)
57 return 10;
Misha Brukman01eca8d2004-07-12 23:36:12 +000058#endif
Chris Lattner3d6721a2006-07-12 20:42:10 +000059 return 0;
Misha Brukman01eca8d2004-07-12 23:36:12 +000060}
Chris Lattner94de9a82006-06-16 01:37:27 +000061unsigned PPC64TargetMachine::getJITMatchQuality() {
Chris Lattner456bc872007-02-25 05:04:13 +000062#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
Chris Lattner94de9a82006-06-16 01:37:27 +000063 if (sizeof(void*) == 8)
Chris Lattner90ac1c02006-07-06 17:10:42 +000064 return 10;
Chris Lattner94de9a82006-06-16 01:37:27 +000065#endif
66 return 0;
67}
Misha Brukman01eca8d2004-07-12 23:36:12 +000068
Chris Lattner94de9a82006-06-16 01:37:27 +000069unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
Nate Begeman21e463b2005-10-16 05:39:50 +000070 // We strongly match "powerpc-*".
71 std::string TT = M.getTargetTriple();
72 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
73 return 20;
74
Chris Lattner87bdba62007-07-09 17:25:29 +000075 // If the target triple is something non-powerpc, we don't match.
76 if (!TT.empty()) return 0;
77
Nate Begeman21e463b2005-10-16 05:39:50 +000078 if (M.getEndianness() == Module::BigEndian &&
79 M.getPointerSize() == Module::Pointer32)
80 return 10; // Weak match
81 else if (M.getEndianness() != Module::AnyEndianness ||
82 M.getPointerSize() != Module::AnyPointerSize)
83 return 0; // Match for some other target
84
85 return getJITMatchQuality()/2;
86}
87
Chris Lattner94de9a82006-06-16 01:37:27 +000088unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
89 // We strongly match "powerpc64-*".
90 std::string TT = M.getTargetTriple();
91 if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
92 return 20;
93
94 if (M.getEndianness() == Module::BigEndian &&
95 M.getPointerSize() == Module::Pointer64)
96 return 10; // Weak match
97 else if (M.getEndianness() != Module::AnyEndianness ||
98 M.getPointerSize() != Module::AnyPointerSize)
99 return 0; // Match for some other target
100
101 return getJITMatchQuality()/2;
102}
103
104
Daniel Dunbar03f4bc52009-07-15 12:11:05 +0000105PPCTargetMachine::PPCTargetMachine(const Target&T, const Module &M,
106 const std::string &FS, bool is64Bit)
107 : LLVMTargetMachine(T),
108 Subtarget(*this, M, FS, is64Bit),
Chris Lattnerb1d26f62006-06-17 00:01:04 +0000109 DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
Chris Lattnerff790892006-11-18 01:34:43 +0000110 FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
Bill Wendling0ea18ff2007-01-24 03:41:36 +0000111 InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
Chris Lattner94de9a82006-06-16 01:37:27 +0000112
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000113 if (getRelocationModel() == Reloc::Default) {
Evan Cheng4c1aa862006-02-22 20:19:42 +0000114 if (Subtarget.isDarwin())
115 setRelocationModel(Reloc::DynamicNoPIC);
116 else
Jim Laskeybf111822006-12-21 20:26:09 +0000117 setRelocationModel(Reloc::Static);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000118 }
Nate Begeman21e463b2005-10-16 05:39:50 +0000119}
120
Dale Johannesen81da02b2007-05-22 17:14:46 +0000121/// Override this for PowerPC. Tail merging happily breaks up instruction issue
122/// groups, which typically degrades performance.
Dan Gohman50cdabc2007-11-19 20:46:23 +0000123bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
Dale Johannesen81da02b2007-05-22 17:14:46 +0000124
Daniel Dunbar03f4bc52009-07-15 12:11:05 +0000125PPC32TargetMachine::PPC32TargetMachine(const Target &T, const Module &M,
126 const std::string &FS)
127 : PPCTargetMachine(T, M, FS, false) {
Chris Lattner94de9a82006-06-16 01:37:27 +0000128}
129
130
Daniel Dunbar03f4bc52009-07-15 12:11:05 +0000131PPC64TargetMachine::PPC64TargetMachine(const Target &T, const Module &M,
132 const std::string &FS)
133 : PPCTargetMachine(T, M, FS, true) {
Chris Lattner94de9a82006-06-16 01:37:27 +0000134}
135
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000136
Chris Lattner1911fd42006-09-04 04:14:57 +0000137//===----------------------------------------------------------------------===//
138// Pass Pipeline Configuration
139//===----------------------------------------------------------------------===//
Nate Begeman7a4fe9b2004-08-11 07:40:04 +0000140
Bill Wendling98a366d2009-04-29 23:29:43 +0000141bool PPCTargetMachine::addInstSelector(PassManagerBase &PM,
142 CodeGenOpt::Level OptLevel) {
Chris Lattner8482dd82005-08-17 19:33:30 +0000143 // Install an instruction selector.
Chris Lattner05f1fe82006-01-12 01:46:07 +0000144 PM.add(createPPCISelDag(*this));
Nate Begeman7a4fe9b2004-08-11 07:40:04 +0000145 return false;
146}
147
Bill Wendling98a366d2009-04-29 23:29:43 +0000148bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM,
149 CodeGenOpt::Level OptLevel) {
Chris Lattner1911fd42006-09-04 04:14:57 +0000150 // Must run branch selection immediately preceding the asm printer.
151 PM.add(createPPCBranchSelectionPass());
152 return false;
153}
154
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000155bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000156 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000157 bool Verbose,
David Greene71847812009-07-14 20:18:05 +0000158 formatted_raw_ostream &Out) {
Anton Korobeynikov06be9972008-08-17 13:54:28 +0000159 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
160 if (AsmPrinterCtor)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000161 PM.add(AsmPrinterCtor(Out, *this, Verbose));
Anton Korobeynikov06be9972008-08-17 13:54:28 +0000162
Chris Lattner1911fd42006-09-04 04:14:57 +0000163 return false;
164}
165
Bill Wendling98a366d2009-04-29 23:29:43 +0000166bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
167 CodeGenOpt::Level OptLevel,
Evan Cheng8bd60352007-07-20 21:56:13 +0000168 bool DumpAsm, MachineCodeEmitter &MCE) {
Chris Lattnere150b8e2006-12-08 04:54:03 +0000169 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
Chris Lattner1911fd42006-09-04 04:14:57 +0000170 // FIXME: This should be moved to TargetJITInfo!!
Chris Lattnere150b8e2006-12-08 04:54:03 +0000171 if (Subtarget.isPPC64()) {
172 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
173 // instructions to materialize arbitrary global variable + function +
174 // constant pool addresses.
175 setRelocationModel(Reloc::PIC_);
Dale Johannesen72324642008-07-31 18:13:12 +0000176 // Temporary workaround for the inability of PPC64 JIT to handle jump
177 // tables.
178 DisableJumpTables = true;
Chris Lattnere150b8e2006-12-08 04:54:03 +0000179 } else {
180 setRelocationModel(Reloc::Static);
181 }
182
Chris Lattner57fc62c2006-12-11 23:22:45 +0000183 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
184 // writing?
185 Subtarget.SetJITMode();
Chris Lattner1911fd42006-09-04 04:14:57 +0000186
187 // Machine code emitter pass for PowerPC.
Nate Begemaneb883af2006-08-23 21:08:52 +0000188 PM.add(createPPCCodeEmitterPass(*this, MCE));
Anton Korobeynikov06be9972008-08-17 13:54:28 +0000189 if (DumpAsm) {
190 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
191 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000192 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Anton Korobeynikov06be9972008-08-17 13:54:28 +0000193 }
194
Nate Begemaneb883af2006-08-23 21:08:52 +0000195 return false;
196}
Chris Lattner1911fd42006-09-04 04:14:57 +0000197
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000198bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
199 CodeGenOpt::Level OptLevel,
200 bool DumpAsm, JITCodeEmitter &JCE) {
201 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
202 // FIXME: This should be moved to TargetJITInfo!!
203 if (Subtarget.isPPC64()) {
204 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
205 // instructions to materialize arbitrary global variable + function +
206 // constant pool addresses.
207 setRelocationModel(Reloc::PIC_);
208 // Temporary workaround for the inability of PPC64 JIT to handle jump
209 // tables.
210 DisableJumpTables = true;
211 } else {
212 setRelocationModel(Reloc::Static);
213 }
214
215 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
216 // writing?
217 Subtarget.SetJITMode();
218
219 // Machine code emitter pass for PowerPC.
220 PM.add(createPPCJITCodeEmitterPass(*this, JCE));
221 if (DumpAsm) {
222 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
223 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000224 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000225 }
226
227 return false;
228}
229
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000230bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
231 CodeGenOpt::Level OptLevel,
232 bool DumpAsm, ObjectCodeEmitter &OCE) {
233 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
234 // FIXME: This should be moved to TargetJITInfo!!
235 if (Subtarget.isPPC64()) {
236 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
237 // instructions to materialize arbitrary global variable + function +
238 // constant pool addresses.
239 setRelocationModel(Reloc::PIC_);
240 // Temporary workaround for the inability of PPC64 JIT to handle jump
241 // tables.
242 DisableJumpTables = true;
243 } else {
244 setRelocationModel(Reloc::Static);
245 }
246
247 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
248 // writing?
249 Subtarget.SetJITMode();
250
251 // Machine code emitter pass for PowerPC.
252 PM.add(createPPCObjectCodeEmitterPass(*this, OCE));
253 if (DumpAsm) {
254 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
255 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000256 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000257 }
258
259 return false;
260}
261
Bill Wendling98a366d2009-04-29 23:29:43 +0000262bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
263 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000264 bool DumpAsm,
265 MachineCodeEmitter &MCE) {
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000266 // Machine code emitter pass for PowerPC.
267 PM.add(createPPCCodeEmitterPass(*this, MCE));
Anton Korobeynikov06be9972008-08-17 13:54:28 +0000268 if (DumpAsm) {
269 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
270 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000271 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Anton Korobeynikov06be9972008-08-17 13:54:28 +0000272 }
273
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000274 return false;
275}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000276
277bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
278 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000279 bool DumpAsm,
280 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000281 // Machine code emitter pass for PowerPC.
282 PM.add(createPPCJITCodeEmitterPass(*this, JCE));
283 if (DumpAsm) {
284 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
285 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000286 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000287 }
288
289 return false;
290}
291
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000292bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
293 CodeGenOpt::Level OptLevel,
294 bool DumpAsm,
295 ObjectCodeEmitter &OCE) {
296 // Machine code emitter pass for PowerPC.
297 PM.add(createPPCObjectCodeEmitterPass(*this, OCE));
298 if (DumpAsm) {
299 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
300 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000301 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000302 }
303
304 return false;
305}
306
307