blob: 637254279334e322bc47f7539f38a63df2935173 [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00009//
Chris Lattnerb0096bd2005-08-15 23:47:04 +000010// Top-level implementation for the PowerPC target.
Misha Brukman5dfe3a92004-06-21 16:55:25 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner26689592005-10-14 23:51:18 +000014#include "PPC.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000015#include "PPCMCAsmInfo.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000016#include "PPCTargetMachine.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000017#include "llvm/PassManager.h"
Dale Johannesen72324642008-07-31 18:13:12 +000018#include "llvm/Target/TargetOptions.h"
Daniel Dunbar0c795d62009-07-25 06:49:55 +000019#include "llvm/Target/TargetRegistry.h"
David Greene71847812009-07-14 20:18:05 +000020#include "llvm/Support/FormattedStream.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000021using namespace llvm;
22
Daniel Dunbar2928c832009-11-06 10:58:06 +000023static const MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
Chris Lattnera7ac47c2009-08-12 07:22:17 +000024 Triple TheTriple(TT);
Daniel Dunbarb42dad42009-08-13 17:03:38 +000025 bool isPPC64 = TheTriple.getArch() == Triple::ppc64;
Chris Lattnera7ac47c2009-08-12 07:22:17 +000026 if (TheTriple.getOS() == Triple::Darwin)
Chris Lattner2807afa2009-08-22 21:03:30 +000027 return new PPCMCAsmInfoDarwin(isPPC64);
Chris Lattneraf76e592009-08-22 20:48:53 +000028 return new PPCLinuxMCAsmInfo(isPPC64);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000029
30}
31
Daniel Dunbar0c795d62009-07-25 06:49:55 +000032extern "C" void LLVMInitializePowerPCTarget() {
33 // Register the targets
34 RegisterTargetMachine<PPC32TargetMachine> A(ThePPC32Target);
35 RegisterTargetMachine<PPC64TargetMachine> B(ThePPC64Target);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000036
Chris Lattneraf76e592009-08-22 20:48:53 +000037 RegisterAsmInfoFn C(ThePPC32Target, createMCAsmInfo);
38 RegisterAsmInfoFn D(ThePPC64Target, createMCAsmInfo);
Daniel Dunbar0c795d62009-07-25 06:49:55 +000039}
Douglas Gregor1555a232009-06-16 20:12:29 +000040
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000041
Chris Lattner0a31d2f2009-08-11 20:42:37 +000042PPCTargetMachine::PPCTargetMachine(const Target &T, const std::string &TT,
Daniel Dunbar51b198a2009-07-15 20:24:03 +000043 const std::string &FS, bool is64Bit)
Chris Lattner0a31d2f2009-08-11 20:42:37 +000044 : LLVMTargetMachine(T, TT),
Daniel Dunbare28039c2009-08-02 23:37:13 +000045 Subtarget(TT, FS, is64Bit),
Chris Lattnerb1d26f62006-06-17 00:01:04 +000046 DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
Chris Lattnerff790892006-11-18 01:34:43 +000047 FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
Chris Lattner6914b862010-02-02 19:23:55 +000048 InstrItins(Subtarget.getInstrItineraryData()) {
Chris Lattner94de9a82006-06-16 01:37:27 +000049
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +000050 if (getRelocationModel() == Reloc::Default) {
Evan Cheng4c1aa862006-02-22 20:19:42 +000051 if (Subtarget.isDarwin())
52 setRelocationModel(Reloc::DynamicNoPIC);
53 else
Jim Laskeybf111822006-12-21 20:26:09 +000054 setRelocationModel(Reloc::Static);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +000055 }
Nate Begeman21e463b2005-10-16 05:39:50 +000056}
57
Dale Johannesen81da02b2007-05-22 17:14:46 +000058/// Override this for PowerPC. Tail merging happily breaks up instruction issue
59/// groups, which typically degrades performance.
Dan Gohman50cdabc2007-11-19 20:46:23 +000060bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
Dale Johannesen81da02b2007-05-22 17:14:46 +000061
Daniel Dunbare28039c2009-08-02 23:37:13 +000062PPC32TargetMachine::PPC32TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbar51b198a2009-07-15 20:24:03 +000063 const std::string &FS)
Daniel Dunbare28039c2009-08-02 23:37:13 +000064 : PPCTargetMachine(T, TT, FS, false) {
Chris Lattner94de9a82006-06-16 01:37:27 +000065}
66
67
Daniel Dunbare28039c2009-08-02 23:37:13 +000068PPC64TargetMachine::PPC64TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbar51b198a2009-07-15 20:24:03 +000069 const std::string &FS)
Daniel Dunbare28039c2009-08-02 23:37:13 +000070 : PPCTargetMachine(T, TT, FS, true) {
Chris Lattner94de9a82006-06-16 01:37:27 +000071}
72
Misha Brukmanb5f662f2005-04-21 23:30:14 +000073
Chris Lattner1911fd42006-09-04 04:14:57 +000074//===----------------------------------------------------------------------===//
75// Pass Pipeline Configuration
76//===----------------------------------------------------------------------===//
Nate Begeman7a4fe9b2004-08-11 07:40:04 +000077
Bill Wendling98a366d2009-04-29 23:29:43 +000078bool PPCTargetMachine::addInstSelector(PassManagerBase &PM,
79 CodeGenOpt::Level OptLevel) {
Chris Lattner8482dd82005-08-17 19:33:30 +000080 // Install an instruction selector.
Chris Lattner05f1fe82006-01-12 01:46:07 +000081 PM.add(createPPCISelDag(*this));
Nate Begeman7a4fe9b2004-08-11 07:40:04 +000082 return false;
83}
84
Bill Wendling98a366d2009-04-29 23:29:43 +000085bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM,
86 CodeGenOpt::Level OptLevel) {
Chris Lattner1911fd42006-09-04 04:14:57 +000087 // Must run branch selection immediately preceding the asm printer.
88 PM.add(createPPCBranchSelectionPass());
89 return false;
90}
91
Bill Wendling98a366d2009-04-29 23:29:43 +000092bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
93 CodeGenOpt::Level OptLevel,
Daniel Dunbarcfe9a602009-07-15 22:33:19 +000094 MachineCodeEmitter &MCE) {
Chris Lattnere150b8e2006-12-08 04:54:03 +000095 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
Chris Lattner1911fd42006-09-04 04:14:57 +000096 // FIXME: This should be moved to TargetJITInfo!!
Chris Lattnere150b8e2006-12-08 04:54:03 +000097 if (Subtarget.isPPC64()) {
98 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
99 // instructions to materialize arbitrary global variable + function +
100 // constant pool addresses.
101 setRelocationModel(Reloc::PIC_);
Dale Johannesen72324642008-07-31 18:13:12 +0000102 // Temporary workaround for the inability of PPC64 JIT to handle jump
103 // tables.
104 DisableJumpTables = true;
Chris Lattnere150b8e2006-12-08 04:54:03 +0000105 } else {
106 setRelocationModel(Reloc::Static);
107 }
108
Chris Lattner57fc62c2006-12-11 23:22:45 +0000109 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
110 // writing?
111 Subtarget.SetJITMode();
Chris Lattner1911fd42006-09-04 04:14:57 +0000112
113 // Machine code emitter pass for PowerPC.
Nate Begemaneb883af2006-08-23 21:08:52 +0000114 PM.add(createPPCCodeEmitterPass(*this, MCE));
Anton Korobeynikov06be9972008-08-17 13:54:28 +0000115
Nate Begemaneb883af2006-08-23 21:08:52 +0000116 return false;
117}
Chris Lattner1911fd42006-09-04 04:14:57 +0000118
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000119bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
120 CodeGenOpt::Level OptLevel,
Daniel Dunbarcfe9a602009-07-15 22:33:19 +0000121 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000122 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
123 // FIXME: This should be moved to TargetJITInfo!!
124 if (Subtarget.isPPC64()) {
125 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
126 // instructions to materialize arbitrary global variable + function +
127 // constant pool addresses.
128 setRelocationModel(Reloc::PIC_);
129 // Temporary workaround for the inability of PPC64 JIT to handle jump
130 // tables.
131 DisableJumpTables = true;
132 } else {
133 setRelocationModel(Reloc::Static);
134 }
135
136 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
137 // writing?
138 Subtarget.SetJITMode();
139
140 // Machine code emitter pass for PowerPC.
141 PM.add(createPPCJITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000142
143 return false;
144}
145
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000146bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
147 CodeGenOpt::Level OptLevel,
Daniel Dunbarcfe9a602009-07-15 22:33:19 +0000148 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000149 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
150 // FIXME: This should be moved to TargetJITInfo!!
151 if (Subtarget.isPPC64()) {
152 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
153 // instructions to materialize arbitrary global variable + function +
154 // constant pool addresses.
155 setRelocationModel(Reloc::PIC_);
156 // Temporary workaround for the inability of PPC64 JIT to handle jump
157 // tables.
158 DisableJumpTables = true;
159 } else {
160 setRelocationModel(Reloc::Static);
161 }
162
163 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
164 // writing?
165 Subtarget.SetJITMode();
166
167 // Machine code emitter pass for PowerPC.
168 PM.add(createPPCObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000169
170 return false;
171}
172
Bill Wendling98a366d2009-04-29 23:29:43 +0000173bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
174 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000175 MachineCodeEmitter &MCE) {
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000176 // Machine code emitter pass for PowerPC.
177 PM.add(createPPCCodeEmitterPass(*this, MCE));
178 return false;
179}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000180
181bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
182 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000183 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000184 // Machine code emitter pass for PowerPC.
185 PM.add(createPPCJITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000186 return false;
187}
188
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000189bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
190 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000191 ObjectCodeEmitter &OCE) {
192 // Machine code emitter pass for PowerPC.
193 PM.add(createPPCObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000194 return false;
195}
196
Bill Wendling48814682010-01-18 22:36:35 +0000197/// getLSDAEncoding - Returns the LSDA pointer encoding. The choices are 4-byte,
198/// 8-byte, and target default. The CIE is hard-coded to indicate that the LSDA
199/// pointer in the FDE section is an "sdata4", and should be encoded as a 4-byte
200/// pointer by default. However, some systems may require a different size due
201/// to bugs or other conditions. We will default to a 4-byte encoding unless the
202/// system tells us otherwise.
203///
Bill Wendlinge1e0f482010-01-19 02:44:01 +0000204/// The issue is when the CIE says their is an LSDA. That mandates that every
205/// FDE have an LSDA slot. But if the function does not need an LSDA. There
206/// needs to be some way to signify there is none. The LSDA is encoded as
207/// pc-rel. But you don't look for some magic value after adding the pc. You
208/// have to look for a zero before adding the pc. The problem is that the size
209/// of the zero to look for depends on the encoding. The unwinder bug in SL is
210/// that it always checks for a pointer-size zero. So on x86_64 it looks for 8
211/// bytes of zero. If you have an LSDA, it works fine since the 8-bytes are
212/// non-zero so it goes ahead and then reads the value based on the encoding.
213/// But if you use sdata4 and there is no LSDA, then the test for zero gives a
214/// false negative and the unwinder thinks there is an LSDA.
215///
Bill Wendling48814682010-01-18 22:36:35 +0000216/// FIXME: This call-back isn't good! We should be using the correct encoding
217/// regardless of the system. However, there are some systems which have bugs
218/// that prevent this from occuring.
219DwarfLSDAEncoding::Encoding PPCTargetMachine::getLSDAEncoding() const {
220 if (Subtarget.isDarwin() && Subtarget.getDarwinVers() != 10)
221 return DwarfLSDAEncoding::Default;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000222
Bill Wendling48814682010-01-18 22:36:35 +0000223 return DwarfLSDAEncoding::EightByte;
224}