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