PPCTargetMachine.cpp revision 03f4bc5d6cf777c8aa559c299ef7f85126872881
1//===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
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"
20#include "llvm/Target/TargetOptions.h"
21#include "llvm/Support/FormattedStream.h"
22using namespace llvm;
23
24/// 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
32// Register the targets
33extern Target ThePPC32Target;
34static RegisterTarget<PPC32TargetMachine>
35X(ThePPC32Target, "ppc32", "PowerPC 32");
36
37extern Target ThePPC64Target;
38static RegisterTarget<PPC64TargetMachine>
39Y(ThePPC64Target, "ppc64", "PowerPC 64");
40
41// Force static initialization.
42extern "C" void LLVMInitializePowerPCTarget() { }
43
44// No assembler printer by default
45PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0;
46
47const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
48  if (Subtarget.isDarwin())
49    return new PPCDarwinTargetAsmInfo(*this);
50  else
51    return new PPCLinuxTargetAsmInfo(*this);
52}
53
54unsigned PPC32TargetMachine::getJITMatchQuality() {
55#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
56  if (sizeof(void*) == 4)
57    return 10;
58#endif
59  return 0;
60}
61unsigned PPC64TargetMachine::getJITMatchQuality() {
62#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
63  if (sizeof(void*) == 8)
64    return 10;
65#endif
66  return 0;
67}
68
69unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
70  // 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
75  // If the target triple is something non-powerpc, we don't match.
76  if (!TT.empty()) return 0;
77
78  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
88unsigned 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
105PPCTargetMachine::PPCTargetMachine(const Target&T, const Module &M,
106                                   const std::string &FS, bool is64Bit)
107  : LLVMTargetMachine(T),
108    Subtarget(*this, M, FS, is64Bit),
109    DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
110    FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
111    InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
112
113  if (getRelocationModel() == Reloc::Default) {
114    if (Subtarget.isDarwin())
115      setRelocationModel(Reloc::DynamicNoPIC);
116    else
117      setRelocationModel(Reloc::Static);
118  }
119}
120
121/// Override this for PowerPC.  Tail merging happily breaks up instruction issue
122/// groups, which typically degrades performance.
123bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
124
125PPC32TargetMachine::PPC32TargetMachine(const Target &T, const Module &M,
126                                       const std::string &FS)
127  : PPCTargetMachine(T, M, FS, false) {
128}
129
130
131PPC64TargetMachine::PPC64TargetMachine(const Target &T, const Module &M,
132                                       const std::string &FS)
133  : PPCTargetMachine(T, M, FS, true) {
134}
135
136
137//===----------------------------------------------------------------------===//
138// Pass Pipeline Configuration
139//===----------------------------------------------------------------------===//
140
141bool PPCTargetMachine::addInstSelector(PassManagerBase &PM,
142                                       CodeGenOpt::Level OptLevel) {
143  // Install an instruction selector.
144  PM.add(createPPCISelDag(*this));
145  return false;
146}
147
148bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM,
149                                      CodeGenOpt::Level OptLevel) {
150  // Must run branch selection immediately preceding the asm printer.
151  PM.add(createPPCBranchSelectionPass());
152  return false;
153}
154
155bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
156                                          CodeGenOpt::Level OptLevel,
157                                          bool Verbose,
158                                          formatted_raw_ostream &Out) {
159  assert(AsmPrinterCtor && "AsmPrinter was not linked in");
160  if (AsmPrinterCtor)
161    PM.add(AsmPrinterCtor(Out, *this, Verbose));
162
163  return false;
164}
165
166bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
167                                      CodeGenOpt::Level OptLevel,
168                                      bool DumpAsm, MachineCodeEmitter &MCE) {
169  // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
170  // FIXME: This should be moved to TargetJITInfo!!
171  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_);
176    // Temporary workaround for the inability of PPC64 JIT to handle jump
177    // tables.
178    DisableJumpTables = true;
179  } else {
180    setRelocationModel(Reloc::Static);
181  }
182
183  // Inform the subtarget that we are in JIT mode.  FIXME: does this break macho
184  // writing?
185  Subtarget.SetJITMode();
186
187  // Machine code emitter pass for PowerPC.
188  PM.add(createPPCCodeEmitterPass(*this, MCE));
189  if (DumpAsm) {
190    assert(AsmPrinterCtor && "AsmPrinter was not linked in");
191    if (AsmPrinterCtor)
192      PM.add(AsmPrinterCtor(ferrs(), *this, true));
193  }
194
195  return false;
196}
197
198bool 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)
224      PM.add(AsmPrinterCtor(ferrs(), *this, true));
225  }
226
227  return false;
228}
229
230bool 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)
256      PM.add(AsmPrinterCtor(ferrs(), *this, true));
257  }
258
259  return false;
260}
261
262bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
263                                            CodeGenOpt::Level OptLevel,
264                                            bool DumpAsm,
265                                            MachineCodeEmitter &MCE) {
266  // Machine code emitter pass for PowerPC.
267  PM.add(createPPCCodeEmitterPass(*this, MCE));
268  if (DumpAsm) {
269    assert(AsmPrinterCtor && "AsmPrinter was not linked in");
270    if (AsmPrinterCtor)
271      PM.add(AsmPrinterCtor(ferrs(), *this, true));
272  }
273
274  return false;
275}
276
277bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
278                                            CodeGenOpt::Level OptLevel,
279                                            bool DumpAsm,
280                                            JITCodeEmitter &JCE) {
281  // 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)
286      PM.add(AsmPrinterCtor(ferrs(), *this, true));
287  }
288
289  return false;
290}
291
292bool 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)
301      PM.add(AsmPrinterCtor(ferrs(), *this, true));
302  }
303
304  return false;
305}
306
307
308