blob: f770eeb54eccabb097982437cd4c975830b8edbd [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"
20using namespace llvm;
21
22namespace {
23 // Register the targets
24 RegisterTarget<PPC32TargetMachine>
25 X("ppc32", " PowerPC 32");
26 RegisterTarget<PPC64TargetMachine>
27 Y("ppc64", " PowerPC 64");
28}
29
30const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
31 if (Subtarget.isDarwin())
32 return new DarwinTargetAsmInfo(*this);
33 else
34 return new LinuxTargetAsmInfo(*this);
35}
36
37unsigned PPC32TargetMachine::getJITMatchQuality() {
38#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
39 if (sizeof(void*) == 4)
40 return 10;
41#endif
42 return 0;
43}
44unsigned PPC64TargetMachine::getJITMatchQuality() {
45#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
46 if (sizeof(void*) == 8)
47 return 10;
48#endif
49 return 0;
50}
51
52unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
53 // We strongly match "powerpc-*".
54 std::string TT = M.getTargetTriple();
55 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
56 return 20;
57
58 // If the target triple is something non-powerpc, we don't match.
59 if (!TT.empty()) return 0;
60
61 if (M.getEndianness() == Module::BigEndian &&
62 M.getPointerSize() == Module::Pointer32)
63 return 10; // Weak match
64 else if (M.getEndianness() != Module::AnyEndianness ||
65 M.getPointerSize() != Module::AnyPointerSize)
66 return 0; // Match for some other target
67
68 return getJITMatchQuality()/2;
69}
70
71unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
72 // We strongly match "powerpc64-*".
73 std::string TT = M.getTargetTriple();
74 if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
75 return 20;
76
77 if (M.getEndianness() == Module::BigEndian &&
78 M.getPointerSize() == Module::Pointer64)
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
87
88PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
89 bool is64Bit)
90 : Subtarget(*this, M, FS, is64Bit),
91 DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
92 FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
93 InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
94
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000095 if (getRelocationModel() == Reloc::Default) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 if (Subtarget.isDarwin())
97 setRelocationModel(Reloc::DynamicNoPIC);
98 else
99 setRelocationModel(Reloc::Static);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000100 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101}
102
103/// Override this for PowerPC. Tail merging happily breaks up instruction issue
104/// groups, which typically degrades performance.
Dan Gohman62ea18a2007-11-19 20:46:23 +0000105bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
107PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
108 : PPCTargetMachine(M, FS, false) {
109}
110
111
112PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
113 : PPCTargetMachine(M, FS, true) {
114}
115
116
117//===----------------------------------------------------------------------===//
118// Pass Pipeline Configuration
119//===----------------------------------------------------------------------===//
120
121bool PPCTargetMachine::addInstSelector(FunctionPassManager &PM, bool Fast) {
122 // Install an instruction selector.
123 PM.add(createPPCISelDag(*this));
124 return false;
125}
126
127bool PPCTargetMachine::addPreEmitPass(FunctionPassManager &PM, bool Fast) {
128
129 // Must run branch selection immediately preceding the asm printer.
130 PM.add(createPPCBranchSelectionPass());
131 return false;
132}
133
134bool PPCTargetMachine::addAssemblyEmitter(FunctionPassManager &PM, bool Fast,
135 std::ostream &Out) {
136 PM.add(createPPCAsmPrinterPass(Out, *this));
137 return false;
138}
139
140bool PPCTargetMachine::addCodeEmitter(FunctionPassManager &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000141 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
143 // FIXME: This should be moved to TargetJITInfo!!
144 if (Subtarget.isPPC64()) {
145 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
146 // instructions to materialize arbitrary global variable + function +
147 // constant pool addresses.
148 setRelocationModel(Reloc::PIC_);
149 } else {
150 setRelocationModel(Reloc::Static);
151 }
152
153 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
154 // writing?
155 Subtarget.SetJITMode();
156
157 // Machine code emitter pass for PowerPC.
158 PM.add(createPPCCodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000159 if (DumpAsm)
160 PM.add(createPPCAsmPrinterPass(*cerr.stream(), *this));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 return false;
162}
163
164bool PPCTargetMachine::addSimpleCodeEmitter(FunctionPassManager &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000165 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 // Machine code emitter pass for PowerPC.
167 PM.add(createPPCCodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000168 if (DumpAsm)
169 PM.add(createPPCAsmPrinterPass(*cerr.stream(), *this));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 return false;
171}