blob: 7c90eca3c44d977818f1f3cd54c53a09c2c82726 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021using namespace llvm;
22
Dan Gohman089efff2008-05-13 00:00:25 +000023// Register the targets
24static RegisterTarget<PPC32TargetMachine>
25X("ppc32", " PowerPC 32");
26static RegisterTarget<PPC64TargetMachine>
27Y("ppc64", " PowerPC 64");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028
29const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
30 if (Subtarget.isDarwin())
Anton Korobeynikovd15bc312008-07-19 21:44:57 +000031 return new PPCDarwinTargetAsmInfo(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 else
Anton Korobeynikovd15bc312008-07-19 21:44:57 +000033 return new PPCLinuxTargetAsmInfo(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034}
35
36unsigned PPC32TargetMachine::getJITMatchQuality() {
37#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
38 if (sizeof(void*) == 4)
39 return 10;
40#endif
41 return 0;
42}
43unsigned PPC64TargetMachine::getJITMatchQuality() {
44#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
45 if (sizeof(void*) == 8)
46 return 10;
47#endif
48 return 0;
49}
50
51unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
52 // We strongly match "powerpc-*".
53 std::string TT = M.getTargetTriple();
54 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
55 return 20;
56
57 // If the target triple is something non-powerpc, we don't match.
58 if (!TT.empty()) return 0;
59
60 if (M.getEndianness() == Module::BigEndian &&
61 M.getPointerSize() == Module::Pointer32)
62 return 10; // Weak match
63 else if (M.getEndianness() != Module::AnyEndianness ||
64 M.getPointerSize() != Module::AnyPointerSize)
65 return 0; // Match for some other target
66
67 return getJITMatchQuality()/2;
68}
69
70unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
71 // We strongly match "powerpc64-*".
72 std::string TT = M.getTargetTriple();
73 if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
74 return 20;
75
76 if (M.getEndianness() == Module::BigEndian &&
77 M.getPointerSize() == Module::Pointer64)
78 return 10; // Weak match
79 else if (M.getEndianness() != Module::AnyEndianness ||
80 M.getPointerSize() != Module::AnyPointerSize)
81 return 0; // Match for some other target
82
83 return getJITMatchQuality()/2;
84}
85
86
87PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
88 bool is64Bit)
89 : Subtarget(*this, M, FS, is64Bit),
90 DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
91 FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
92 InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
93
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000094 if (getRelocationModel() == Reloc::Default) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 if (Subtarget.isDarwin())
96 setRelocationModel(Reloc::DynamicNoPIC);
97 else
98 setRelocationModel(Reloc::Static);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000099 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100}
101
102/// Override this for PowerPC. Tail merging happily breaks up instruction issue
103/// groups, which typically degrades performance.
Dan Gohman62ea18a2007-11-19 20:46:23 +0000104bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105
106PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
107 : PPCTargetMachine(M, FS, false) {
108}
109
110
111PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
112 : PPCTargetMachine(M, FS, true) {
113}
114
115
116//===----------------------------------------------------------------------===//
117// Pass Pipeline Configuration
118//===----------------------------------------------------------------------===//
119
Dan Gohmane34aa772008-03-11 22:29:46 +0000120bool PPCTargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 // Install an instruction selector.
122 PM.add(createPPCISelDag(*this));
123 return false;
124}
125
Dan Gohmane34aa772008-03-11 22:29:46 +0000126bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127
128 // Must run branch selection immediately preceding the asm printer.
129 PM.add(createPPCBranchSelectionPass());
130 return false;
131}
132
Dan Gohmane34aa772008-03-11 22:29:46 +0000133bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 std::ostream &Out) {
135 PM.add(createPPCAsmPrinterPass(Out, *this));
136 return false;
137}
138
Dan Gohmane34aa772008-03-11 22:29:46 +0000139bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000140 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
142 // FIXME: This should be moved to TargetJITInfo!!
143 if (Subtarget.isPPC64()) {
144 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
145 // instructions to materialize arbitrary global variable + function +
146 // constant pool addresses.
147 setRelocationModel(Reloc::PIC_);
Dale Johannesen493492f2008-07-31 18:13:12 +0000148 // Temporary workaround for the inability of PPC64 JIT to handle jump
149 // tables.
150 DisableJumpTables = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 } else {
152 setRelocationModel(Reloc::Static);
153 }
154
155 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
156 // writing?
157 Subtarget.SetJITMode();
158
159 // Machine code emitter pass for PowerPC.
160 PM.add(createPPCCodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000161 if (DumpAsm)
162 PM.add(createPPCAsmPrinterPass(*cerr.stream(), *this));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 return false;
164}
165
Dan Gohmane34aa772008-03-11 22:29:46 +0000166bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000167 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 // Machine code emitter pass for PowerPC.
169 PM.add(createPPCCodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000170 if (DumpAsm)
171 PM.add(createPPCAsmPrinterPass(*cerr.stream(), *this));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 return false;
173}