blob: 1d3787faa6da23b5809f55429db1d97d0297d080 [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
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +000038// No assembler printer by default
39PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0;
40
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
42 if (Subtarget.isDarwin())
Anton Korobeynikovd15bc312008-07-19 21:44:57 +000043 return new PPCDarwinTargetAsmInfo(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 else
Anton Korobeynikovd15bc312008-07-19 21:44:57 +000045 return new PPCLinuxTargetAsmInfo(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046}
47
48unsigned PPC32TargetMachine::getJITMatchQuality() {
49#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
50 if (sizeof(void*) == 4)
51 return 10;
52#endif
53 return 0;
54}
55unsigned PPC64TargetMachine::getJITMatchQuality() {
56#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
57 if (sizeof(void*) == 8)
58 return 10;
59#endif
60 return 0;
61}
62
63unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
64 // We strongly match "powerpc-*".
65 std::string TT = M.getTargetTriple();
66 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
67 return 20;
68
69 // If the target triple is something non-powerpc, we don't match.
70 if (!TT.empty()) return 0;
71
72 if (M.getEndianness() == Module::BigEndian &&
73 M.getPointerSize() == Module::Pointer32)
74 return 10; // Weak match
75 else if (M.getEndianness() != Module::AnyEndianness ||
76 M.getPointerSize() != Module::AnyPointerSize)
77 return 0; // Match for some other target
78
79 return getJITMatchQuality()/2;
80}
81
82unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
83 // We strongly match "powerpc64-*".
84 std::string TT = M.getTargetTriple();
85 if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
86 return 20;
87
88 if (M.getEndianness() == Module::BigEndian &&
89 M.getPointerSize() == Module::Pointer64)
90 return 10; // Weak match
91 else if (M.getEndianness() != Module::AnyEndianness ||
92 M.getPointerSize() != Module::AnyPointerSize)
93 return 0; // Match for some other target
94
95 return getJITMatchQuality()/2;
96}
97
98
99PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
100 bool is64Bit)
101 : Subtarget(*this, M, FS, is64Bit),
102 DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
103 FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
104 InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
105
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000106 if (getRelocationModel() == Reloc::Default) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 if (Subtarget.isDarwin())
108 setRelocationModel(Reloc::DynamicNoPIC);
109 else
110 setRelocationModel(Reloc::Static);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000111 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112}
113
114/// Override this for PowerPC. Tail merging happily breaks up instruction issue
115/// groups, which typically degrades performance.
Dan Gohman62ea18a2007-11-19 20:46:23 +0000116bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117
118PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
119 : PPCTargetMachine(M, FS, false) {
120}
121
122
123PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
124 : PPCTargetMachine(M, FS, true) {
125}
126
127
128//===----------------------------------------------------------------------===//
129// Pass Pipeline Configuration
130//===----------------------------------------------------------------------===//
131
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000132bool PPCTargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 // Install an instruction selector.
134 PM.add(createPPCISelDag(*this));
135 return false;
136}
137
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000138bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139
140 // Must run branch selection immediately preceding the asm printer.
141 PM.add(createPPCBranchSelectionPass());
142 return false;
143}
144
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000145bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
146 bool Verbose, raw_ostream &Out) {
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000147 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
148 if (AsmPrinterCtor)
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000149 PM.add(AsmPrinterCtor(Out, *this, Fast, Verbose));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000150
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 return false;
152}
153
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000154bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000155 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
157 // FIXME: This should be moved to TargetJITInfo!!
158 if (Subtarget.isPPC64()) {
159 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
160 // instructions to materialize arbitrary global variable + function +
161 // constant pool addresses.
162 setRelocationModel(Reloc::PIC_);
Dale Johannesen493492f2008-07-31 18:13:12 +0000163 // Temporary workaround for the inability of PPC64 JIT to handle jump
164 // tables.
165 DisableJumpTables = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 } else {
167 setRelocationModel(Reloc::Static);
168 }
169
170 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
171 // writing?
172 Subtarget.SetJITMode();
173
174 // Machine code emitter pass for PowerPC.
175 PM.add(createPPCCodeEmitterPass(*this, MCE));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000176 if (DumpAsm) {
177 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
178 if (AsmPrinterCtor)
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000179 PM.add(AsmPrinterCtor(errs(), *this, Fast, true));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000180 }
181
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 return false;
183}
184
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000185bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000186 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 // Machine code emitter pass for PowerPC.
188 PM.add(createPPCCodeEmitterPass(*this, MCE));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000189 if (DumpAsm) {
190 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
191 if (AsmPrinterCtor)
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000192 PM.add(AsmPrinterCtor(errs(), *this, Fast, true));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000193 }
194
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 return false;
196}