PPCTargetMachine.cpp revision b8cab9227a0f6ffbdaae33e3c64268e265008a6a
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/raw_ostream.h"
22using namespace llvm;
23
24// Register the targets
25static RegisterTarget<PPC32TargetMachine>
26X("ppc32", "PowerPC 32");
27static RegisterTarget<PPC64TargetMachine>
28Y("ppc64", "PowerPC 64");
29
30// No assembler printer by default
31PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0;
32
33const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
34  if (Subtarget.isDarwin())
35    return new PPCDarwinTargetAsmInfo(*this);
36  else
37    return new PPCLinuxTargetAsmInfo(*this);
38}
39
40unsigned PPC32TargetMachine::getJITMatchQuality() {
41#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
42  if (sizeof(void*) == 4)
43    return 10;
44#endif
45  return 0;
46}
47unsigned PPC64TargetMachine::getJITMatchQuality() {
48#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
49  if (sizeof(void*) == 8)
50    return 10;
51#endif
52  return 0;
53}
54
55unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
56  // We strongly match "powerpc-*".
57  std::string TT = M.getTargetTriple();
58  if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
59    return 20;
60
61  // If the target triple is something non-powerpc, we don't match.
62  if (!TT.empty()) return 0;
63
64  if (M.getEndianness()  == Module::BigEndian &&
65      M.getPointerSize() == Module::Pointer32)
66    return 10;                                   // Weak match
67  else if (M.getEndianness() != Module::AnyEndianness ||
68           M.getPointerSize() != Module::AnyPointerSize)
69    return 0;                                    // Match for some other target
70
71  return getJITMatchQuality()/2;
72}
73
74unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
75  // We strongly match "powerpc64-*".
76  std::string TT = M.getTargetTriple();
77  if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
78    return 20;
79
80  if (M.getEndianness()  == Module::BigEndian &&
81      M.getPointerSize() == Module::Pointer64)
82    return 10;                                   // Weak match
83  else if (M.getEndianness() != Module::AnyEndianness ||
84           M.getPointerSize() != Module::AnyPointerSize)
85    return 0;                                    // Match for some other target
86
87  return getJITMatchQuality()/2;
88}
89
90
91PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
92                                   bool is64Bit)
93  : Subtarget(*this, M, FS, is64Bit),
94    DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
95    FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
96    InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
97
98  if (getRelocationModel() == Reloc::Default) {
99    if (Subtarget.isDarwin())
100      setRelocationModel(Reloc::DynamicNoPIC);
101    else
102      setRelocationModel(Reloc::Static);
103  }
104}
105
106/// Override this for PowerPC.  Tail merging happily breaks up instruction issue
107/// groups, which typically degrades performance.
108bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
109
110PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
111  : PPCTargetMachine(M, FS, false) {
112}
113
114
115PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
116  : PPCTargetMachine(M, FS, true) {
117}
118
119
120//===----------------------------------------------------------------------===//
121// Pass Pipeline Configuration
122//===----------------------------------------------------------------------===//
123
124bool PPCTargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
125  // Install an instruction selector.
126  PM.add(createPPCISelDag(*this));
127  return false;
128}
129
130bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM, bool Fast) {
131
132  // Must run branch selection immediately preceding the asm printer.
133  PM.add(createPPCBranchSelectionPass());
134  return false;
135}
136
137bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
138                                          raw_ostream &Out) {
139  assert(AsmPrinterCtor && "AsmPrinter was not linked in");
140  if (AsmPrinterCtor)
141    PM.add(AsmPrinterCtor(Out, *this));
142
143  return false;
144}
145
146bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
147                                      bool DumpAsm, MachineCodeEmitter &MCE) {
148  // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
149  // FIXME: This should be moved to TargetJITInfo!!
150  if (Subtarget.isPPC64()) {
151    // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
152    // instructions to materialize arbitrary global variable + function +
153    // constant pool addresses.
154    setRelocationModel(Reloc::PIC_);
155    // Temporary workaround for the inability of PPC64 JIT to handle jump
156    // tables.
157    DisableJumpTables = true;
158  } else {
159    setRelocationModel(Reloc::Static);
160  }
161
162  // Inform the subtarget that we are in JIT mode.  FIXME: does this break macho
163  // writing?
164  Subtarget.SetJITMode();
165
166  // Machine code emitter pass for PowerPC.
167  PM.add(createPPCCodeEmitterPass(*this, MCE));
168  if (DumpAsm) {
169    assert(AsmPrinterCtor && "AsmPrinter was not linked in");
170    if (AsmPrinterCtor)
171      PM.add(AsmPrinterCtor(errs(), *this));
172  }
173
174  return false;
175}
176
177bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
178                                            bool DumpAsm, MachineCodeEmitter &MCE) {
179  // Machine code emitter pass for PowerPC.
180  PM.add(createPPCCodeEmitterPass(*this, MCE));
181  if (DumpAsm) {
182    assert(AsmPrinterCtor && "AsmPrinter was not linked in");
183    if (AsmPrinterCtor)
184      PM.add(AsmPrinterCtor(errs(), *this));
185  }
186
187  return false;
188}
189