blob: 3d737515db2465835159ac450913eb3504a6709c [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
Dan Gohman089efff2008-05-13 00:00:25 +000024// Register the targets
25static RegisterTarget<PPC32TargetMachine>
26X("ppc32", " PowerPC 32");
27static RegisterTarget<PPC64TargetMachine>
28Y("ppc64", " PowerPC 64");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +000030// No assembler printer by default
31PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0;
32
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
34 if (Subtarget.isDarwin())
Anton Korobeynikovd15bc312008-07-19 21:44:57 +000035 return new PPCDarwinTargetAsmInfo(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 else
Anton Korobeynikovd15bc312008-07-19 21:44:57 +000037 return new PPCLinuxTargetAsmInfo(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038}
39
40unsigned PPC32TargetMachine::getJITMatchQuality() {
41#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
42 if (sizeof(void*) == 4)
43 return 10;
44#endif
45 return 0;
46}
47unsigned PPC64TargetMachine::getJITMatchQuality() {
48#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
49 if (sizeof(void*) == 8)
50 return 10;
51#endif
52 return 0;
53}
54
55unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
56 // We strongly match "powerpc-*".
57 std::string TT = M.getTargetTriple();
58 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
59 return 20;
60
61 // If the target triple is something non-powerpc, we don't match.
62 if (!TT.empty()) return 0;
63
64 if (M.getEndianness() == Module::BigEndian &&
65 M.getPointerSize() == Module::Pointer32)
66 return 10; // Weak match
67 else if (M.getEndianness() != Module::AnyEndianness ||
68 M.getPointerSize() != Module::AnyPointerSize)
69 return 0; // Match for some other target
70
71 return getJITMatchQuality()/2;
72}
73
74unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
75 // We strongly match "powerpc64-*".
76 std::string TT = M.getTargetTriple();
77 if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
78 return 20;
79
80 if (M.getEndianness() == Module::BigEndian &&
81 M.getPointerSize() == Module::Pointer64)
82 return 10; // Weak match
83 else if (M.getEndianness() != Module::AnyEndianness ||
84 M.getPointerSize() != Module::AnyPointerSize)
85 return 0; // Match for some other target
86
87 return getJITMatchQuality()/2;
88}
89
90
91PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
92 bool is64Bit)
93 : Subtarget(*this, M, FS, is64Bit),
94 DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
95 FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
96 InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
97
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000098 if (getRelocationModel() == Reloc::Default) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 if (Subtarget.isDarwin())
100 setRelocationModel(Reloc::DynamicNoPIC);
101 else
102 setRelocationModel(Reloc::Static);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000103 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104}
105
106/// Override this for PowerPC. Tail merging happily breaks up instruction issue
107/// groups, which typically degrades performance.
Dan Gohman62ea18a2007-11-19 20:46:23 +0000108bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109
110PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
111 : PPCTargetMachine(M, FS, false) {
112}
113
114
115PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
116 : PPCTargetMachine(M, FS, true) {
117}
118
119
120//===----------------------------------------------------------------------===//
121// Pass Pipeline Configuration
122//===----------------------------------------------------------------------===//
123
Dan Gohmane34aa772008-03-11 22:29:46 +0000124bool PPCTargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 // Install an instruction selector.
126 PM.add(createPPCISelDag(*this));
127 return false;
128}
129
Dan Gohmane34aa772008-03-11 22:29:46 +0000130bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
132 // Must run branch selection immediately preceding the asm printer.
133 PM.add(createPPCBranchSelectionPass());
134 return false;
135}
136
Dan Gohmane34aa772008-03-11 22:29:46 +0000137bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
Owen Anderson847b99b2008-08-21 00:14:44 +0000138 raw_ostream &Out) {
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000139 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
140 if (AsmPrinterCtor)
141 PM.add(AsmPrinterCtor(Out, *this));
142
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 return false;
144}
145
Dan Gohmane34aa772008-03-11 22:29:46 +0000146bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000147 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
149 // FIXME: This should be moved to TargetJITInfo!!
150 if (Subtarget.isPPC64()) {
151 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
152 // instructions to materialize arbitrary global variable + function +
153 // constant pool addresses.
154 setRelocationModel(Reloc::PIC_);
Dale Johannesen493492f2008-07-31 18:13:12 +0000155 // Temporary workaround for the inability of PPC64 JIT to handle jump
156 // tables.
157 DisableJumpTables = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 } else {
159 setRelocationModel(Reloc::Static);
160 }
161
162 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
163 // writing?
164 Subtarget.SetJITMode();
165
166 // Machine code emitter pass for PowerPC.
167 PM.add(createPPCCodeEmitterPass(*this, MCE));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000168 if (DumpAsm) {
169 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
170 if (AsmPrinterCtor)
Owen Anderson847b99b2008-08-21 00:14:44 +0000171 PM.add(AsmPrinterCtor(errs(), *this));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000172 }
173
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 return false;
175}
176
Dan Gohmane34aa772008-03-11 22:29:46 +0000177bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000178 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 // Machine code emitter pass for PowerPC.
180 PM.add(createPPCCodeEmitterPass(*this, MCE));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000181 if (DumpAsm) {
182 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
183 if (AsmPrinterCtor)
Owen Anderson847b99b2008-08-21 00:14:44 +0000184 PM.add(AsmPrinterCtor(errs(), *this));
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000185 }
186
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 return false;
188}