1928d6ac26ef098adf1297e0ea109bbe67a9d4d8
[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 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"
22 using 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.
29 extern "C" int PowerPCTargetMachineModule;
30 int PowerPCTargetMachineModule = 0;
31
32 // Register the targets
33 extern Target ThePPC32Target;
34 static RegisterTarget<PPC32TargetMachine>
35 X(ThePPC32Target, "ppc32", "PowerPC 32");
36
37 extern Target ThePPC64Target;
38 static RegisterTarget<PPC64TargetMachine>
39 Y(ThePPC64Target, "ppc64", "PowerPC 64");
40
41 // Force static initialization.
42 extern "C" void LLVMInitializePowerPCTarget() { }
43
44 const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
45   if (Subtarget.isDarwin())
46     return new PPCDarwinTargetAsmInfo(*this);
47   else
48     return new PPCLinuxTargetAsmInfo(*this);
49 }
50
51 PPCTargetMachine::PPCTargetMachine(const Target&T, const Module &M, 
52                                    const std::string &FS, bool is64Bit)
53   : LLVMTargetMachine(T),
54     Subtarget(*this, M, FS, is64Bit),
55     DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
56     FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
57     InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
58
59   if (getRelocationModel() == Reloc::Default) {
60     if (Subtarget.isDarwin())
61       setRelocationModel(Reloc::DynamicNoPIC);
62     else
63       setRelocationModel(Reloc::Static);
64   }
65 }
66
67 /// Override this for PowerPC.  Tail merging happily breaks up instruction issue
68 /// groups, which typically degrades performance.
69 bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
70
71 PPC32TargetMachine::PPC32TargetMachine(const Target &T, const Module &M, 
72                                        const std::string &FS) 
73   : PPCTargetMachine(T, M, FS, false) {
74 }
75
76
77 PPC64TargetMachine::PPC64TargetMachine(const Target &T, const Module &M, 
78                                        const std::string &FS)
79   : PPCTargetMachine(T, M, FS, true) {
80 }
81
82
83 //===----------------------------------------------------------------------===//
84 // Pass Pipeline Configuration
85 //===----------------------------------------------------------------------===//
86
87 bool PPCTargetMachine::addInstSelector(PassManagerBase &PM,
88                                        CodeGenOpt::Level OptLevel) {
89   // Install an instruction selector.
90   PM.add(createPPCISelDag(*this));
91   return false;
92 }
93
94 bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM,
95                                       CodeGenOpt::Level OptLevel) {
96   // Must run branch selection immediately preceding the asm printer.
97   PM.add(createPPCBranchSelectionPass());
98   return false;
99 }
100
101 bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
102                                           CodeGenOpt::Level OptLevel,
103                                           bool Verbose,
104                                           formatted_raw_ostream &Out) {
105   FunctionPass *Printer = getTarget().createAsmPrinter(Out, *this, Verbose);
106   if (!Printer)
107     llvm_report_error("unable to create assembly printer");
108   PM.add(Printer);
109   return false;
110 }
111
112 bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
113                                       CodeGenOpt::Level OptLevel,
114                                       bool DumpAsm, MachineCodeEmitter &MCE) {
115   // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
116   // FIXME: This should be moved to TargetJITInfo!!
117   if (Subtarget.isPPC64()) {
118     // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
119     // instructions to materialize arbitrary global variable + function +
120     // constant pool addresses.
121     setRelocationModel(Reloc::PIC_);
122     // Temporary workaround for the inability of PPC64 JIT to handle jump
123     // tables.
124     DisableJumpTables = true;      
125   } else {
126     setRelocationModel(Reloc::Static);
127   }
128   
129   // Inform the subtarget that we are in JIT mode.  FIXME: does this break macho
130   // writing?
131   Subtarget.SetJITMode();
132   
133   // Machine code emitter pass for PowerPC.
134   PM.add(createPPCCodeEmitterPass(*this, MCE));
135   if (DumpAsm)
136     addAssemblyEmitter(PM, OptLevel, true, ferrs());
137
138   return false;
139 }
140
141 bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
142                                       CodeGenOpt::Level OptLevel,
143                                       bool DumpAsm, JITCodeEmitter &JCE) {
144   // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
145   // FIXME: This should be moved to TargetJITInfo!!
146   if (Subtarget.isPPC64()) {
147     // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
148     // instructions to materialize arbitrary global variable + function +
149     // constant pool addresses.
150     setRelocationModel(Reloc::PIC_);
151     // Temporary workaround for the inability of PPC64 JIT to handle jump
152     // tables.
153     DisableJumpTables = true;      
154   } else {
155     setRelocationModel(Reloc::Static);
156   }
157   
158   // Inform the subtarget that we are in JIT mode.  FIXME: does this break macho
159   // writing?
160   Subtarget.SetJITMode();
161   
162   // Machine code emitter pass for PowerPC.
163   PM.add(createPPCJITCodeEmitterPass(*this, JCE));
164   if (DumpAsm)
165     addAssemblyEmitter(PM, OptLevel, true, ferrs());
166
167   return false;
168 }
169
170 bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
171                                       CodeGenOpt::Level OptLevel,
172                                       bool DumpAsm, ObjectCodeEmitter &OCE) {
173   // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
174   // FIXME: This should be moved to TargetJITInfo!!
175   if (Subtarget.isPPC64()) {
176     // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
177     // instructions to materialize arbitrary global variable + function +
178     // constant pool addresses.
179     setRelocationModel(Reloc::PIC_);
180     // Temporary workaround for the inability of PPC64 JIT to handle jump
181     // tables.
182     DisableJumpTables = true;      
183   } else {
184     setRelocationModel(Reloc::Static);
185   }
186   
187   // Inform the subtarget that we are in JIT mode.  FIXME: does this break macho
188   // writing?
189   Subtarget.SetJITMode();
190   
191   // Machine code emitter pass for PowerPC.
192   PM.add(createPPCObjectCodeEmitterPass(*this, OCE));
193   if (DumpAsm)
194     addAssemblyEmitter(PM, OptLevel, true, ferrs());
195
196   return false;
197 }
198
199 bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
200                                             CodeGenOpt::Level OptLevel,
201                                             bool DumpAsm, 
202                                             MachineCodeEmitter &MCE) {
203   // Machine code emitter pass for PowerPC.
204   PM.add(createPPCCodeEmitterPass(*this, MCE));
205   if (DumpAsm)
206     addAssemblyEmitter(PM, OptLevel, true, ferrs());
207
208   return false;
209 }
210
211 bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
212                                             CodeGenOpt::Level OptLevel,
213                                             bool DumpAsm,
214                                             JITCodeEmitter &JCE) {
215   // Machine code emitter pass for PowerPC.
216   PM.add(createPPCJITCodeEmitterPass(*this, JCE));
217   if (DumpAsm)
218     addAssemblyEmitter(PM, OptLevel, true, ferrs());
219
220   return false;
221 }
222
223 bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
224                                             CodeGenOpt::Level OptLevel,
225                                             bool DumpAsm,
226                                             ObjectCodeEmitter &OCE) {
227   // Machine code emitter pass for PowerPC.
228   PM.add(createPPCObjectCodeEmitterPass(*this, OCE));
229   if (DumpAsm)
230     addAssemblyEmitter(PM, OptLevel, true, ferrs());
231
232   return false;
233 }
234
235