Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 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 Johannesen | 493492f | 2008-07-31 18:13:12 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetOptions.h" |
David Greene | 302008d | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FormattedStream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Oscar Fuentes | 4f01235 | 2008-11-15 21:36:30 +0000 | [diff] [blame] | 24 | /// PowerPCTargetMachineModule - Note that this is used on hosts that |
| 25 | /// cannot link in a library unless there are references into the |
| 26 | /// library. In particular, it seems that it is not possible to get |
| 27 | /// things to work on Win32 without this. Though it is unused, do not |
| 28 | /// remove it. |
| 29 | extern "C" int PowerPCTargetMachineModule; |
| 30 | int PowerPCTargetMachineModule = 0; |
| 31 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 32 | // Register the targets |
Daniel Dunbar | 6fa4f57 | 2009-07-15 09:22:31 +0000 | [diff] [blame^] | 33 | extern Target ThePPC32Target; |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 34 | static RegisterTarget<PPC32TargetMachine> |
Daniel Dunbar | 6fa4f57 | 2009-07-15 09:22:31 +0000 | [diff] [blame^] | 35 | X(ThePPC32Target, "ppc32", "PowerPC 32"); |
| 36 | |
| 37 | extern Target ThePPC64Target; |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 38 | static RegisterTarget<PPC64TargetMachine> |
Daniel Dunbar | 6fa4f57 | 2009-07-15 09:22:31 +0000 | [diff] [blame^] | 39 | Y(ThePPC64Target, "ppc64", "PowerPC 64"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 40 | |
Bob Wilson | ebbc1c4 | 2009-06-23 23:59:40 +0000 | [diff] [blame] | 41 | // Force static initialization. |
| 42 | extern "C" void LLVMInitializePowerPCTarget() { } |
Douglas Gregor | 1dc5ff4 | 2009-06-16 20:12:29 +0000 | [diff] [blame] | 43 | |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 44 | // No assembler printer by default |
| 45 | PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0; |
| 46 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 47 | const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const { |
| 48 | if (Subtarget.isDarwin()) |
Anton Korobeynikov | d15bc31 | 2008-07-19 21:44:57 +0000 | [diff] [blame] | 49 | return new PPCDarwinTargetAsmInfo(*this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 50 | else |
Anton Korobeynikov | d15bc31 | 2008-07-19 21:44:57 +0000 | [diff] [blame] | 51 | return new PPCLinuxTargetAsmInfo(*this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | unsigned PPC32TargetMachine::getJITMatchQuality() { |
| 55 | #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__) |
| 56 | if (sizeof(void*) == 4) |
| 57 | return 10; |
| 58 | #endif |
| 59 | return 0; |
| 60 | } |
| 61 | unsigned PPC64TargetMachine::getJITMatchQuality() { |
| 62 | #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__) |
| 63 | if (sizeof(void*) == 8) |
| 64 | return 10; |
| 65 | #endif |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) { |
| 70 | // We strongly match "powerpc-*". |
| 71 | std::string TT = M.getTargetTriple(); |
| 72 | if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-") |
| 73 | return 20; |
| 74 | |
| 75 | // If the target triple is something non-powerpc, we don't match. |
| 76 | if (!TT.empty()) return 0; |
| 77 | |
| 78 | if (M.getEndianness() == Module::BigEndian && |
| 79 | M.getPointerSize() == Module::Pointer32) |
| 80 | return 10; // Weak match |
| 81 | else if (M.getEndianness() != Module::AnyEndianness || |
| 82 | M.getPointerSize() != Module::AnyPointerSize) |
| 83 | return 0; // Match for some other target |
| 84 | |
| 85 | return getJITMatchQuality()/2; |
| 86 | } |
| 87 | |
| 88 | unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) { |
| 89 | // We strongly match "powerpc64-*". |
| 90 | std::string TT = M.getTargetTriple(); |
| 91 | if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-") |
| 92 | return 20; |
| 93 | |
| 94 | if (M.getEndianness() == Module::BigEndian && |
| 95 | M.getPointerSize() == Module::Pointer64) |
| 96 | return 10; // Weak match |
| 97 | else if (M.getEndianness() != Module::AnyEndianness || |
| 98 | M.getPointerSize() != Module::AnyPointerSize) |
| 99 | return 0; // Match for some other target |
| 100 | |
| 101 | return getJITMatchQuality()/2; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS, |
| 106 | bool is64Bit) |
| 107 | : Subtarget(*this, M, FS, is64Bit), |
| 108 | DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this), |
| 109 | FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this), |
| 110 | InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) { |
| 111 | |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 112 | if (getRelocationModel() == Reloc::Default) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 113 | if (Subtarget.isDarwin()) |
| 114 | setRelocationModel(Reloc::DynamicNoPIC); |
| 115 | else |
| 116 | setRelocationModel(Reloc::Static); |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 117 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /// Override this for PowerPC. Tail merging happily breaks up instruction issue |
| 121 | /// groups, which typically degrades performance. |
Dan Gohman | 62ea18a | 2007-11-19 20:46:23 +0000 | [diff] [blame] | 122 | bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 123 | |
| 124 | PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS) |
| 125 | : PPCTargetMachine(M, FS, false) { |
| 126 | } |
| 127 | |
| 128 | |
| 129 | PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS) |
| 130 | : PPCTargetMachine(M, FS, true) { |
| 131 | } |
| 132 | |
| 133 | |
| 134 | //===----------------------------------------------------------------------===// |
| 135 | // Pass Pipeline Configuration |
| 136 | //===----------------------------------------------------------------------===// |
| 137 | |
Bill Wendling | 5ed22ac | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 138 | bool PPCTargetMachine::addInstSelector(PassManagerBase &PM, |
| 139 | CodeGenOpt::Level OptLevel) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 140 | // Install an instruction selector. |
| 141 | PM.add(createPPCISelDag(*this)); |
| 142 | return false; |
| 143 | } |
| 144 | |
Bill Wendling | 5ed22ac | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 145 | bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM, |
| 146 | CodeGenOpt::Level OptLevel) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 147 | // Must run branch selection immediately preceding the asm printer. |
| 148 | PM.add(createPPCBranchSelectionPass()); |
| 149 | return false; |
| 150 | } |
| 151 | |
Bill Wendling | 58ed5d2 | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 152 | bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM, |
Bill Wendling | 5ed22ac | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 153 | CodeGenOpt::Level OptLevel, |
Bill Wendling | 58ed5d2 | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 154 | bool Verbose, |
David Greene | 302008d | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 155 | formatted_raw_ostream &Out) { |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 156 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 157 | if (AsmPrinterCtor) |
Daniel Dunbar | b10d222 | 2009-07-01 01:48:54 +0000 | [diff] [blame] | 158 | PM.add(AsmPrinterCtor(Out, *this, Verbose)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 159 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | |
Bill Wendling | 5ed22ac | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 163 | bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, |
| 164 | CodeGenOpt::Level OptLevel, |
Evan Cheng | 7754721 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 165 | bool DumpAsm, MachineCodeEmitter &MCE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 166 | // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64. |
| 167 | // FIXME: This should be moved to TargetJITInfo!! |
| 168 | if (Subtarget.isPPC64()) { |
| 169 | // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many |
| 170 | // instructions to materialize arbitrary global variable + function + |
| 171 | // constant pool addresses. |
| 172 | setRelocationModel(Reloc::PIC_); |
Dale Johannesen | 493492f | 2008-07-31 18:13:12 +0000 | [diff] [blame] | 173 | // Temporary workaround for the inability of PPC64 JIT to handle jump |
| 174 | // tables. |
| 175 | DisableJumpTables = true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 176 | } else { |
| 177 | setRelocationModel(Reloc::Static); |
| 178 | } |
| 179 | |
| 180 | // Inform the subtarget that we are in JIT mode. FIXME: does this break macho |
| 181 | // writing? |
| 182 | Subtarget.SetJITMode(); |
| 183 | |
| 184 | // Machine code emitter pass for PowerPC. |
| 185 | PM.add(createPPCCodeEmitterPass(*this, MCE)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 186 | if (DumpAsm) { |
| 187 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 188 | if (AsmPrinterCtor) |
David Greene | 302008d | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 189 | PM.add(AsmPrinterCtor(ferrs(), *this, true)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 192 | return false; |
| 193 | } |
| 194 | |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 195 | bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, |
| 196 | CodeGenOpt::Level OptLevel, |
| 197 | bool DumpAsm, JITCodeEmitter &JCE) { |
| 198 | // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64. |
| 199 | // FIXME: This should be moved to TargetJITInfo!! |
| 200 | if (Subtarget.isPPC64()) { |
| 201 | // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many |
| 202 | // instructions to materialize arbitrary global variable + function + |
| 203 | // constant pool addresses. |
| 204 | setRelocationModel(Reloc::PIC_); |
| 205 | // Temporary workaround for the inability of PPC64 JIT to handle jump |
| 206 | // tables. |
| 207 | DisableJumpTables = true; |
| 208 | } else { |
| 209 | setRelocationModel(Reloc::Static); |
| 210 | } |
| 211 | |
| 212 | // Inform the subtarget that we are in JIT mode. FIXME: does this break macho |
| 213 | // writing? |
| 214 | Subtarget.SetJITMode(); |
| 215 | |
| 216 | // Machine code emitter pass for PowerPC. |
| 217 | PM.add(createPPCJITCodeEmitterPass(*this, JCE)); |
| 218 | if (DumpAsm) { |
| 219 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 220 | if (AsmPrinterCtor) |
David Greene | 302008d | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 221 | PM.add(AsmPrinterCtor(ferrs(), *this, true)); |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | return false; |
| 225 | } |
| 226 | |
Bruno Cardoso Lopes | aabb9a5 | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 227 | bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, |
| 228 | CodeGenOpt::Level OptLevel, |
| 229 | bool DumpAsm, ObjectCodeEmitter &OCE) { |
| 230 | // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64. |
| 231 | // FIXME: This should be moved to TargetJITInfo!! |
| 232 | if (Subtarget.isPPC64()) { |
| 233 | // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many |
| 234 | // instructions to materialize arbitrary global variable + function + |
| 235 | // constant pool addresses. |
| 236 | setRelocationModel(Reloc::PIC_); |
| 237 | // Temporary workaround for the inability of PPC64 JIT to handle jump |
| 238 | // tables. |
| 239 | DisableJumpTables = true; |
| 240 | } else { |
| 241 | setRelocationModel(Reloc::Static); |
| 242 | } |
| 243 | |
| 244 | // Inform the subtarget that we are in JIT mode. FIXME: does this break macho |
| 245 | // writing? |
| 246 | Subtarget.SetJITMode(); |
| 247 | |
| 248 | // Machine code emitter pass for PowerPC. |
| 249 | PM.add(createPPCObjectCodeEmitterPass(*this, OCE)); |
| 250 | if (DumpAsm) { |
| 251 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 252 | if (AsmPrinterCtor) |
David Greene | 302008d | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 253 | PM.add(AsmPrinterCtor(ferrs(), *this, true)); |
Bruno Cardoso Lopes | aabb9a5 | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | return false; |
| 257 | } |
| 258 | |
Bill Wendling | 5ed22ac | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 259 | bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, |
| 260 | CodeGenOpt::Level OptLevel, |
Bruno Cardoso Lopes | 8e2537b | 2009-06-01 19:57:37 +0000 | [diff] [blame] | 261 | bool DumpAsm, |
| 262 | MachineCodeEmitter &MCE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 263 | // Machine code emitter pass for PowerPC. |
| 264 | PM.add(createPPCCodeEmitterPass(*this, MCE)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 265 | if (DumpAsm) { |
| 266 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 267 | if (AsmPrinterCtor) |
David Greene | 302008d | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 268 | PM.add(AsmPrinterCtor(ferrs(), *this, true)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 271 | return false; |
| 272 | } |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 273 | |
| 274 | bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, |
| 275 | CodeGenOpt::Level OptLevel, |
Bruno Cardoso Lopes | 8e2537b | 2009-06-01 19:57:37 +0000 | [diff] [blame] | 276 | bool DumpAsm, |
| 277 | JITCodeEmitter &JCE) { |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 278 | // Machine code emitter pass for PowerPC. |
| 279 | PM.add(createPPCJITCodeEmitterPass(*this, JCE)); |
| 280 | if (DumpAsm) { |
| 281 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 282 | if (AsmPrinterCtor) |
David Greene | 302008d | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 283 | PM.add(AsmPrinterCtor(ferrs(), *this, true)); |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | return false; |
| 287 | } |
| 288 | |
Bruno Cardoso Lopes | aabb9a5 | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 289 | bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, |
| 290 | CodeGenOpt::Level OptLevel, |
| 291 | bool DumpAsm, |
| 292 | ObjectCodeEmitter &OCE) { |
| 293 | // Machine code emitter pass for PowerPC. |
| 294 | PM.add(createPPCObjectCodeEmitterPass(*this, OCE)); |
| 295 | if (DumpAsm) { |
| 296 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 297 | if (AsmPrinterCtor) |
David Greene | 302008d | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 298 | PM.add(AsmPrinterCtor(ferrs(), *this, true)); |
Bruno Cardoso Lopes | aabb9a5 | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | |