9dcbc0487008b778cc131576ed608f2776dad78e
[oota-llvm.git] / lib / Target / PowerPC / PPCTargetMachine.cpp
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"
20 using namespace llvm;
21
22 namespace {
23   // Register the targets
24   RegisterTarget<PPC32TargetMachine>
25   X("ppc32", "  PowerPC 32");
26   RegisterTarget<PPC64TargetMachine>
27   Y("ppc64", "  PowerPC 64");
28 }
29
30 const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
31   return new DarwinTargetAsmInfo(*this);
32 }
33
34 unsigned PPC32TargetMachine::getJITMatchQuality() {
35 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
36   if (sizeof(void*) == 4)
37     return 10;
38 #endif
39   return 0;
40 }
41 unsigned 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
49 unsigned 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
65 unsigned 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
82 PPCTargetMachine::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, is64Bit), 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
96 PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS) 
97   : PPCTargetMachine(M, FS, false) {
98 }
99
100
101 PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
102   : PPCTargetMachine(M, FS, true) {
103 }
104
105
106 //===----------------------------------------------------------------------===//
107 // Pass Pipeline Configuration
108 //===----------------------------------------------------------------------===//
109
110 bool PPCTargetMachine::addInstSelector(FunctionPassManager &PM, bool Fast) {
111   // Install an instruction selector.
112   PM.add(createPPCISelDag(*this));
113   return false;
114 }
115
116 bool 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
123 bool PPCTargetMachine::addAssemblyEmitter(FunctionPassManager &PM, bool Fast, 
124                                           std::ostream &Out) {
125   PM.add(createPPCAsmPrinterPass(Out, *this));
126   return false;
127 }
128
129 bool 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
136 bool PPCTargetMachine::addCodeEmitter(FunctionPassManager &PM, bool Fast,
137                                       MachineCodeEmitter &MCE) {
138   // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
139   // FIXME: This should be moved to TargetJITInfo!!
140   if (Subtarget.isPPC64()) {
141     // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
142     // instructions to materialize arbitrary global variable + function +
143     // constant pool addresses.
144     setRelocationModel(Reloc::PIC_);
145   } else {
146     setRelocationModel(Reloc::Static);
147   }
148   
149   
150   // Machine code emitter pass for PowerPC.
151   PM.add(createPPCCodeEmitterPass(*this, MCE));
152   return false;
153 }
154