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