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