blob: 3e89885a77eb77e67a496d73fb1efda0625974b8 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Dale Johannesen493492f2008-07-31 18:13:12 +000020#include "llvm/Target/TargetOptions.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000021#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022using namespace llvm;
23
Oscar Fuentes4f012352008-11-15 21:36:30 +000024/// 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.
29extern "C" int PowerPCTargetMachineModule;
30int PowerPCTargetMachineModule = 0;
31
Dan Gohman089efff2008-05-13 00:00:25 +000032// Register the targets
33static RegisterTarget<PPC32TargetMachine>
Dan Gohman669b9bf2008-10-14 20:25:08 +000034X("ppc32", "PowerPC 32");
Dan Gohman089efff2008-05-13 00:00:25 +000035static RegisterTarget<PPC64TargetMachine>
Dan Gohman669b9bf2008-10-14 20:25:08 +000036Y("ppc64", "PowerPC 64");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000038// Force static initialization when called from llvm/InitializeAllTargets.h
39namespace llvm {
40 void InitializePowerPCTarget() { }
41}
42
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +000043// No assembler printer by default
44PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0;
45
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
47 if (Subtarget.isDarwin())
Anton Korobeynikovd15bc312008-07-19 21:44:57 +000048 return new PPCDarwinTargetAsmInfo(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 else
Anton Korobeynikovd15bc312008-07-19 21:44:57 +000050 return new PPCLinuxTargetAsmInfo(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051}
52
53unsigned PPC32TargetMachine::getJITMatchQuality() {
54#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
55 if (sizeof(void*) == 4)
56 return 10;
57#endif
58 return 0;
59}
60unsigned PPC64TargetMachine::getJITMatchQuality() {
61#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
62 if (sizeof(void*) == 8)
63 return 10;
64#endif
65 return 0;
66}
67
68unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
69 // We strongly match "powerpc-*".
70 std::string TT = M.getTargetTriple();
71 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
72 return 20;
73
74 // If the target triple is something non-powerpc, we don't match.
75 if (!TT.empty()) return 0;
76
77 if (M.getEndianness() == Module::BigEndian &&
78 M.getPointerSize() == Module::Pointer32)
79 return 10; // Weak match
80 else if (M.getEndianness() != Module::AnyEndianness ||
81 M.getPointerSize() != Module::AnyPointerSize)
82 return 0; // Match for some other target
83
84 return getJITMatchQuality()/2;
85}
86
87unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
88 // We strongly match "powerpc64-*".
89 std::string TT = M.getTargetTriple();
90 if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
91 return 20;
92
93 if (M.getEndianness() == Module::BigEndian &&
94 M.getPointerSize() == Module::Pointer64)
95 return 10; // Weak match
96 else if (M.getEndianness() != Module::AnyEndianness ||
97 M.getPointerSize() != Module::AnyPointerSize)
98 return 0; // Match for some other target
99
100 return getJITMatchQuality()/2;
101}
102
103
104PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
105 bool is64Bit)
106 : Subtarget(*this, M, FS, is64Bit),
107 DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
108 FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
109 InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
110
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000111 if (getRelocationModel() == Reloc::Default) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 if (Subtarget.isDarwin())
113 setRelocationModel(Reloc::DynamicNoPIC);
114 else
115 setRelocationModel(Reloc::Static);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000116 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117}
118
119/// Override this for PowerPC. Tail merging happily breaks up instruction issue
120/// groups, which typically degrades performance.
Dan Gohman62ea18a2007-11-19 20:46:23 +0000121bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122
123PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
124 : PPCTargetMachine(M, FS, false) {
125}
126
127
128PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
129 : PPCTargetMachine(M, FS, true) {
130}
131
132
133//===----------------------------------------------------------------------===//
134// Pass Pipeline Configuration
135//===----------------------------------------------------------------------===//
136
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000137bool PPCTargetMachine::addInstSelector(PassManagerBase &PM,
138 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 // Install an instruction selector.
140 PM.add(createPPCISelDag(*this));
141 return false;
142}
143
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000144bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM,
145 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 // Must run branch selection immediately preceding the asm printer.
147 PM.add(createPPCBranchSelectionPass());
148 return false;
149}
150
Bill Wendling58ed5d22009-04-29 00:15:41 +0000151bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000152 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000153 bool Verbose,
154 raw_ostream &Out) {
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000155 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
156 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000157 PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000158
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 return false;
160}
161
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000162bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
163 CodeGenOpt::Level OptLevel,
Evan Cheng77547212007-07-20 21:56:13 +0000164 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
166 // FIXME: This should be moved to TargetJITInfo!!
167 if (Subtarget.isPPC64()) {
168 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
169 // instructions to materialize arbitrary global variable + function +
170 // constant pool addresses.
171 setRelocationModel(Reloc::PIC_);
Dale Johannesen493492f2008-07-31 18:13:12 +0000172 // Temporary workaround for the inability of PPC64 JIT to handle jump
173 // tables.
174 DisableJumpTables = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 } else {
176 setRelocationModel(Reloc::Static);
177 }
178
179 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
180 // writing?
181 Subtarget.SetJITMode();
182
183 // Machine code emitter pass for PowerPC.
184 PM.add(createPPCCodeEmitterPass(*this, MCE));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000185 if (DumpAsm) {
186 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
187 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000188 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000189 }
190
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 return false;
192}
193
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000194bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
195 CodeGenOpt::Level OptLevel,
196 bool DumpAsm, JITCodeEmitter &JCE) {
197 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
198 // FIXME: This should be moved to TargetJITInfo!!
199 if (Subtarget.isPPC64()) {
200 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
201 // instructions to materialize arbitrary global variable + function +
202 // constant pool addresses.
203 setRelocationModel(Reloc::PIC_);
204 // Temporary workaround for the inability of PPC64 JIT to handle jump
205 // tables.
206 DisableJumpTables = true;
207 } else {
208 setRelocationModel(Reloc::Static);
209 }
210
211 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
212 // writing?
213 Subtarget.SetJITMode();
214
215 // Machine code emitter pass for PowerPC.
216 PM.add(createPPCJITCodeEmitterPass(*this, JCE));
217 if (DumpAsm) {
218 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
219 if (AsmPrinterCtor)
220 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
221 }
222
223 return false;
224}
225
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000226bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
227 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000228 bool DumpAsm,
229 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 // Machine code emitter pass for PowerPC.
231 PM.add(createPPCCodeEmitterPass(*this, MCE));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000232 if (DumpAsm) {
233 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
234 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000235 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000236 }
237
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 return false;
239}
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000240
241bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
242 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000243 bool DumpAsm,
244 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000245 // Machine code emitter pass for PowerPC.
246 PM.add(createPPCJITCodeEmitterPass(*this, JCE));
247 if (DumpAsm) {
248 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
249 if (AsmPrinterCtor)
250 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
251 }
252
253 return false;
254}
255