PPCTargetMachine.cpp revision 4da1c82f724adba2832f79b3b49fc96c1467076d
1//===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
20using namespace llvm;
21
22namespace {
23  // Register the targets
24  RegisterTarget<PPC32TargetMachine>
25  X("ppc32", "  PowerPC 32");
26  RegisterTarget<PPC64TargetMachine>
27  Y("ppc64", "  PowerPC 64");
28}
29
30const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
31  return new DarwinTargetAsmInfo(*this);
32}
33
34unsigned PPC32TargetMachine::getJITMatchQuality() {
35#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
36  if (sizeof(void*) == 4)
37    return 10;
38#endif
39  return 0;
40}
41unsigned PPC64TargetMachine::getJITMatchQuality() {
42#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
43  if (sizeof(void*) == 8)
44    return 10;
45#endif
46  return 0;
47}
48
49unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
50  // We strongly match "powerpc-*".
51  std::string TT = M.getTargetTriple();
52  if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
53    return 20;
54
55  if (M.getEndianness()  == Module::BigEndian &&
56      M.getPointerSize() == Module::Pointer32)
57    return 10;                                   // Weak match
58  else if (M.getEndianness() != Module::AnyEndianness ||
59           M.getPointerSize() != Module::AnyPointerSize)
60    return 0;                                    // Match for some other target
61
62  return getJITMatchQuality()/2;
63}
64
65unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
66  // We strongly match "powerpc64-*".
67  std::string TT = M.getTargetTriple();
68  if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
69    return 20;
70
71  if (M.getEndianness()  == Module::BigEndian &&
72      M.getPointerSize() == Module::Pointer64)
73    return 10;                                   // Weak match
74  else if (M.getEndianness() != Module::AnyEndianness ||
75           M.getPointerSize() != Module::AnyPointerSize)
76    return 0;                                    // Match for some other target
77
78  return getJITMatchQuality()/2;
79}
80
81
82PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
83                                   bool is64Bit)
84  : Subtarget(M, FS, is64Bit),
85    DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
86    FrameInfo(*this, false), JITInfo(*this, is64Bit), TLInfo(*this),
87    InstrItins(Subtarget.getInstrItineraryData()) {
88
89  if (getRelocationModel() == Reloc::Default)
90    if (Subtarget.isDarwin())
91      setRelocationModel(Reloc::DynamicNoPIC);
92    else
93      setRelocationModel(Reloc::PIC_);
94}
95
96PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
97  : PPCTargetMachine(M, FS, false) {
98}
99
100
101PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
102  : PPCTargetMachine(M, FS, true) {
103}
104
105
106//===----------------------------------------------------------------------===//
107// Pass Pipeline Configuration
108//===----------------------------------------------------------------------===//
109
110bool PPCTargetMachine::addInstSelector(FunctionPassManager &PM, bool Fast) {
111  // Install an instruction selector.
112  PM.add(createPPCISelDag(*this));
113  return false;
114}
115
116bool PPCTargetMachine::addPreEmitPass(FunctionPassManager &PM, bool Fast) {
117
118  // Must run branch selection immediately preceding the asm printer.
119  PM.add(createPPCBranchSelectionPass());
120  return false;
121}
122
123bool PPCTargetMachine::addAssemblyEmitter(FunctionPassManager &PM, bool Fast,
124                                          std::ostream &Out) {
125  PM.add(createPPCAsmPrinterPass(Out, *this));
126  return false;
127}
128
129bool PPCTargetMachine::addObjectWriter(FunctionPassManager &PM, bool Fast,
130                                       std::ostream &Out) {
131  // FIXME: support PPC ELF files at some point
132  addPPCMachOObjectWriterPass(PM, Out, *this);
133  return false;
134}
135
136bool PPCTargetMachine::addCodeEmitter(FunctionPassManager &PM, bool Fast,
137                                      MachineCodeEmitter &MCE) {
138  // The JIT should use the static relocation model.
139  // FIXME: This should be moved to TargetJITInfo!!
140  setRelocationModel(Reloc::Static);
141
142  // Machine code emitter pass for PowerPC.
143  PM.add(createPPCCodeEmitterPass(*this, MCE));
144  return false;
145}
146
147