blob: c261f8793ff37fe79acf5aea10032a224fcbf8f8 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Top-level implementation for the PowerPC target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PPC.h"
15#include "PPCTargetAsmInfo.h"
16#include "PPCTargetMachine.h"
17#include "llvm/Module.h"
18#include "llvm/PassManager.h"
19#include "llvm/Target/TargetMachineRegistry.h"
Dale Johannesen493492f2008-07-31 18:13:12 +000020#include "llvm/Target/TargetOptions.h"
David Greene302008d2009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022using namespace llvm;
23
Oscar Fuentes4f012352008-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 Gohman089efff2008-05-13 00:00:25 +000032// Register the targets
33static RegisterTarget<PPC32TargetMachine>
Stuart Hastings1721b8d2009-07-15 17:27:11 +000034X("ppc32", "PowerPC 32");
Dan Gohman089efff2008-05-13 00:00:25 +000035static RegisterTarget<PPC64TargetMachine>
Stuart Hastings1721b8d2009-07-15 17:27:11 +000036Y("ppc64", "PowerPC 64");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037
Bob Wilsonebbc1c42009-06-23 23:59:40 +000038// Force static initialization.
39extern "C" void LLVMInitializePowerPCTarget() { }
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000040
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +000041// No assembler printer by default
42PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0;
43
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
45 if (Subtarget.isDarwin())
Anton Korobeynikovd15bc312008-07-19 21:44:57 +000046 return new PPCDarwinTargetAsmInfo(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 else
Anton Korobeynikovd15bc312008-07-19 21:44:57 +000048 return new PPCLinuxTargetAsmInfo(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049}
50
Stuart Hastings1721b8d2009-07-15 17:27:11 +000051unsigned PPC32TargetMachine::getJITMatchQuality() {
52#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
53 if (sizeof(void*) == 4)
54 return 10;
55#endif
56 return 0;
57}
58unsigned PPC64TargetMachine::getJITMatchQuality() {
59#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
60 if (sizeof(void*) == 8)
61 return 10;
62#endif
63 return 0;
64}
65
66unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
67 // 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
72 // If the target triple is something non-powerpc, we don't match.
73 if (!TT.empty()) return 0;
74
75 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
85unsigned 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)
104 : Subtarget(*this, M, FS, is64Bit),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
106 FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
107 InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
108
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000109 if (getRelocationModel() == Reloc::Default) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 if (Subtarget.isDarwin())
111 setRelocationModel(Reloc::DynamicNoPIC);
112 else
113 setRelocationModel(Reloc::Static);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000114 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115}
116
117/// Override this for PowerPC. Tail merging happily breaks up instruction issue
118/// groups, which typically degrades performance.
Dan Gohman62ea18a2007-11-19 20:46:23 +0000119bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120
Stuart Hastings1721b8d2009-07-15 17:27:11 +0000121PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
122 : PPCTargetMachine(M, FS, false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123}
124
125
Stuart Hastings1721b8d2009-07-15 17:27:11 +0000126PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
127 : PPCTargetMachine(M, FS, true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128}
129
130
131//===----------------------------------------------------------------------===//
132// Pass Pipeline Configuration
133//===----------------------------------------------------------------------===//
134
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000135bool PPCTargetMachine::addInstSelector(PassManagerBase &PM,
136 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 // Install an instruction selector.
138 PM.add(createPPCISelDag(*this));
139 return false;
140}
141
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000142bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM,
143 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 // Must run branch selection immediately preceding the asm printer.
145 PM.add(createPPCBranchSelectionPass());
146 return false;
147}
148
Bill Wendling58ed5d22009-04-29 00:15:41 +0000149bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000150 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000151 bool Verbose,
David Greene302008d2009-07-14 20:18:05 +0000152 formatted_raw_ostream &Out) {
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000153 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
154 if (AsmPrinterCtor)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000155 PM.add(AsmPrinterCtor(Out, *this, Verbose));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000156
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 return false;
158}
159
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000160bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
161 CodeGenOpt::Level OptLevel,
Evan Cheng77547212007-07-20 21:56:13 +0000162 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
164 // FIXME: This should be moved to TargetJITInfo!!
165 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 Johannesen493492f2008-07-31 18:13:12 +0000170 // Temporary workaround for the inability of PPC64 JIT to handle jump
171 // tables.
172 DisableJumpTables = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 } else {
174 setRelocationModel(Reloc::Static);
175 }
176
177 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
178 // writing?
179 Subtarget.SetJITMode();
180
181 // Machine code emitter pass for PowerPC.
182 PM.add(createPPCCodeEmitterPass(*this, MCE));
Daniel Dunbar8ba03b72009-07-15 12:49:15 +0000183 if (DumpAsm)
184 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000185
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 return false;
187}
188
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000189bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
190 CodeGenOpt::Level OptLevel,
191 bool DumpAsm, JITCodeEmitter &JCE) {
192 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
193 // FIXME: This should be moved to TargetJITInfo!!
194 if (Subtarget.isPPC64()) {
195 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
196 // instructions to materialize arbitrary global variable + function +
197 // constant pool addresses.
198 setRelocationModel(Reloc::PIC_);
199 // Temporary workaround for the inability of PPC64 JIT to handle jump
200 // tables.
201 DisableJumpTables = true;
202 } else {
203 setRelocationModel(Reloc::Static);
204 }
205
206 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
207 // writing?
208 Subtarget.SetJITMode();
209
210 // Machine code emitter pass for PowerPC.
211 PM.add(createPPCJITCodeEmitterPass(*this, JCE));
Daniel Dunbar8ba03b72009-07-15 12:49:15 +0000212 if (DumpAsm)
213 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000214
215 return false;
216}
217
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000218bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
219 CodeGenOpt::Level OptLevel,
220 bool DumpAsm, ObjectCodeEmitter &OCE) {
221 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
222 // FIXME: This should be moved to TargetJITInfo!!
223 if (Subtarget.isPPC64()) {
224 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
225 // instructions to materialize arbitrary global variable + function +
226 // constant pool addresses.
227 setRelocationModel(Reloc::PIC_);
228 // Temporary workaround for the inability of PPC64 JIT to handle jump
229 // tables.
230 DisableJumpTables = true;
231 } else {
232 setRelocationModel(Reloc::Static);
233 }
234
235 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
236 // writing?
237 Subtarget.SetJITMode();
238
239 // Machine code emitter pass for PowerPC.
240 PM.add(createPPCObjectCodeEmitterPass(*this, OCE));
Daniel Dunbar8ba03b72009-07-15 12:49:15 +0000241 if (DumpAsm)
242 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000243
244 return false;
245}
246
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000247bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
248 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000249 bool DumpAsm,
250 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 // Machine code emitter pass for PowerPC.
252 PM.add(createPPCCodeEmitterPass(*this, MCE));
Daniel Dunbar8ba03b72009-07-15 12:49:15 +0000253 if (DumpAsm)
254 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000255
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 return false;
257}
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000258
259bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
260 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000261 bool DumpAsm,
262 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000263 // Machine code emitter pass for PowerPC.
264 PM.add(createPPCJITCodeEmitterPass(*this, JCE));
Daniel Dunbar8ba03b72009-07-15 12:49:15 +0000265 if (DumpAsm)
266 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000267
268 return false;
269}
270
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000271bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
272 CodeGenOpt::Level OptLevel,
273 bool DumpAsm,
274 ObjectCodeEmitter &OCE) {
275 // Machine code emitter pass for PowerPC.
276 PM.add(createPPCObjectCodeEmitterPass(*this, OCE));
Daniel Dunbar8ba03b72009-07-15 12:49:15 +0000277 if (DumpAsm)
278 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000279
280 return false;
281}
282
283