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" |
Owen Anderson | 847b99b | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.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 |
| 33 | static RegisterTarget<PPC32TargetMachine> |
Dan Gohman | 669b9bf | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 34 | X("ppc32", "PowerPC 32"); |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 35 | static RegisterTarget<PPC64TargetMachine> |
Dan Gohman | 669b9bf | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 36 | Y("ppc64", "PowerPC 64"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 37 | |
Douglas Gregor | 1dc5ff4 | 2009-06-16 20:12:29 +0000 | [diff] [blame^] | 38 | // Force static initialization when called from llvm/InitializeAllTargets.h |
| 39 | namespace llvm { |
| 40 | void InitializePowerPCTarget() { } |
| 41 | } |
| 42 | |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 43 | // No assembler printer by default |
| 44 | PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0; |
| 45 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 46 | const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const { |
| 47 | if (Subtarget.isDarwin()) |
Anton Korobeynikov | d15bc31 | 2008-07-19 21:44:57 +0000 | [diff] [blame] | 48 | return new PPCDarwinTargetAsmInfo(*this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 49 | else |
Anton Korobeynikov | d15bc31 | 2008-07-19 21:44:57 +0000 | [diff] [blame] | 50 | return new PPCLinuxTargetAsmInfo(*this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | unsigned PPC32TargetMachine::getJITMatchQuality() { |
| 54 | #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__) |
| 55 | if (sizeof(void*) == 4) |
| 56 | return 10; |
| 57 | #endif |
| 58 | return 0; |
| 59 | } |
| 60 | unsigned PPC64TargetMachine::getJITMatchQuality() { |
| 61 | #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__) |
| 62 | if (sizeof(void*) == 8) |
| 63 | return 10; |
| 64 | #endif |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) { |
| 69 | // We strongly match "powerpc-*". |
| 70 | std::string TT = M.getTargetTriple(); |
| 71 | if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-") |
| 72 | return 20; |
| 73 | |
| 74 | // If the target triple is something non-powerpc, we don't match. |
| 75 | if (!TT.empty()) return 0; |
| 76 | |
| 77 | if (M.getEndianness() == Module::BigEndian && |
| 78 | M.getPointerSize() == Module::Pointer32) |
| 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 | unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) { |
| 88 | // We strongly match "powerpc64-*". |
| 89 | std::string TT = M.getTargetTriple(); |
| 90 | if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-") |
| 91 | return 20; |
| 92 | |
| 93 | if (M.getEndianness() == Module::BigEndian && |
| 94 | M.getPointerSize() == Module::Pointer64) |
| 95 | return 10; // Weak match |
| 96 | else if (M.getEndianness() != Module::AnyEndianness || |
| 97 | M.getPointerSize() != Module::AnyPointerSize) |
| 98 | return 0; // Match for some other target |
| 99 | |
| 100 | return getJITMatchQuality()/2; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS, |
| 105 | bool is64Bit) |
| 106 | : Subtarget(*this, M, FS, is64Bit), |
| 107 | DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this), |
| 108 | FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this), |
| 109 | InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) { |
| 110 | |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 111 | if (getRelocationModel() == Reloc::Default) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 112 | if (Subtarget.isDarwin()) |
| 113 | setRelocationModel(Reloc::DynamicNoPIC); |
| 114 | else |
| 115 | setRelocationModel(Reloc::Static); |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 116 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /// Override this for PowerPC. Tail merging happily breaks up instruction issue |
| 120 | /// groups, which typically degrades performance. |
Dan Gohman | 62ea18a | 2007-11-19 20:46:23 +0000 | [diff] [blame] | 121 | bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 122 | |
| 123 | PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS) |
| 124 | : PPCTargetMachine(M, FS, false) { |
| 125 | } |
| 126 | |
| 127 | |
| 128 | PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS) |
| 129 | : PPCTargetMachine(M, FS, true) { |
| 130 | } |
| 131 | |
| 132 | |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | // Pass Pipeline Configuration |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | |
Bill Wendling | 5ed22ac | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 137 | bool PPCTargetMachine::addInstSelector(PassManagerBase &PM, |
| 138 | CodeGenOpt::Level OptLevel) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 139 | // Install an instruction selector. |
| 140 | PM.add(createPPCISelDag(*this)); |
| 141 | return false; |
| 142 | } |
| 143 | |
Bill Wendling | 5ed22ac | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 144 | bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM, |
| 145 | CodeGenOpt::Level OptLevel) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 146 | // Must run branch selection immediately preceding the asm printer. |
| 147 | PM.add(createPPCBranchSelectionPass()); |
| 148 | return false; |
| 149 | } |
| 150 | |
Bill Wendling | 58ed5d2 | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 151 | bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM, |
Bill Wendling | 5ed22ac | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 152 | CodeGenOpt::Level OptLevel, |
Bill Wendling | 58ed5d2 | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 153 | bool Verbose, |
| 154 | raw_ostream &Out) { |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 155 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 156 | if (AsmPrinterCtor) |
Bill Wendling | 58ed5d2 | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 157 | PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 158 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
Bill Wendling | 5ed22ac | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 162 | bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, |
| 163 | CodeGenOpt::Level OptLevel, |
Evan Cheng | 7754721 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 164 | bool DumpAsm, MachineCodeEmitter &MCE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 165 | // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64. |
| 166 | // FIXME: This should be moved to TargetJITInfo!! |
| 167 | if (Subtarget.isPPC64()) { |
| 168 | // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many |
| 169 | // instructions to materialize arbitrary global variable + function + |
| 170 | // constant pool addresses. |
| 171 | setRelocationModel(Reloc::PIC_); |
Dale Johannesen | 493492f | 2008-07-31 18:13:12 +0000 | [diff] [blame] | 172 | // Temporary workaround for the inability of PPC64 JIT to handle jump |
| 173 | // tables. |
| 174 | DisableJumpTables = true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 175 | } else { |
| 176 | setRelocationModel(Reloc::Static); |
| 177 | } |
| 178 | |
| 179 | // Inform the subtarget that we are in JIT mode. FIXME: does this break macho |
| 180 | // writing? |
| 181 | Subtarget.SetJITMode(); |
| 182 | |
| 183 | // Machine code emitter pass for PowerPC. |
| 184 | PM.add(createPPCCodeEmitterPass(*this, MCE)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 185 | if (DumpAsm) { |
| 186 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 187 | if (AsmPrinterCtor) |
Bill Wendling | 58ed5d2 | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 188 | PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 191 | return false; |
| 192 | } |
| 193 | |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 194 | bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, |
| 195 | CodeGenOpt::Level OptLevel, |
| 196 | bool DumpAsm, JITCodeEmitter &JCE) { |
| 197 | // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64. |
| 198 | // FIXME: This should be moved to TargetJITInfo!! |
| 199 | if (Subtarget.isPPC64()) { |
| 200 | // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many |
| 201 | // instructions to materialize arbitrary global variable + function + |
| 202 | // constant pool addresses. |
| 203 | setRelocationModel(Reloc::PIC_); |
| 204 | // Temporary workaround for the inability of PPC64 JIT to handle jump |
| 205 | // tables. |
| 206 | DisableJumpTables = true; |
| 207 | } else { |
| 208 | setRelocationModel(Reloc::Static); |
| 209 | } |
| 210 | |
| 211 | // Inform the subtarget that we are in JIT mode. FIXME: does this break macho |
| 212 | // writing? |
| 213 | Subtarget.SetJITMode(); |
| 214 | |
| 215 | // Machine code emitter pass for PowerPC. |
| 216 | PM.add(createPPCJITCodeEmitterPass(*this, JCE)); |
| 217 | if (DumpAsm) { |
| 218 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 219 | if (AsmPrinterCtor) |
| 220 | PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true)); |
| 221 | } |
| 222 | |
| 223 | return false; |
| 224 | } |
| 225 | |
Bill Wendling | 5ed22ac | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 226 | bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, |
| 227 | CodeGenOpt::Level OptLevel, |
Bruno Cardoso Lopes | 8e2537b | 2009-06-01 19:57:37 +0000 | [diff] [blame] | 228 | bool DumpAsm, |
| 229 | MachineCodeEmitter &MCE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 230 | // Machine code emitter pass for PowerPC. |
| 231 | PM.add(createPPCCodeEmitterPass(*this, MCE)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 232 | if (DumpAsm) { |
| 233 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 234 | if (AsmPrinterCtor) |
Bill Wendling | 58ed5d2 | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 235 | PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 238 | return false; |
| 239 | } |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 240 | |
| 241 | bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, |
| 242 | CodeGenOpt::Level OptLevel, |
Bruno Cardoso Lopes | 8e2537b | 2009-06-01 19:57:37 +0000 | [diff] [blame] | 243 | bool DumpAsm, |
| 244 | JITCodeEmitter &JCE) { |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 245 | // Machine code emitter pass for PowerPC. |
| 246 | PM.add(createPPCJITCodeEmitterPass(*this, JCE)); |
| 247 | if (DumpAsm) { |
| 248 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 249 | if (AsmPrinterCtor) |
| 250 | PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true)); |
| 251 | } |
| 252 | |
| 253 | return false; |
| 254 | } |
| 255 | |