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 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 24 | // Register the targets |
| 25 | static RegisterTarget<PPC32TargetMachine> |
| 26 | X("ppc32", " PowerPC 32"); |
| 27 | static RegisterTarget<PPC64TargetMachine> |
| 28 | Y("ppc64", " PowerPC 64"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 29 | |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 30 | // No assembler printer by default |
| 31 | PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0; |
| 32 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 33 | const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const { |
| 34 | if (Subtarget.isDarwin()) |
Anton Korobeynikov | d15bc31 | 2008-07-19 21:44:57 +0000 | [diff] [blame] | 35 | return new PPCDarwinTargetAsmInfo(*this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 36 | else |
Anton Korobeynikov | d15bc31 | 2008-07-19 21:44:57 +0000 | [diff] [blame] | 37 | return new PPCLinuxTargetAsmInfo(*this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | unsigned PPC32TargetMachine::getJITMatchQuality() { |
| 41 | #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__) |
| 42 | if (sizeof(void*) == 4) |
| 43 | return 10; |
| 44 | #endif |
| 45 | return 0; |
| 46 | } |
| 47 | unsigned PPC64TargetMachine::getJITMatchQuality() { |
| 48 | #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__) |
| 49 | if (sizeof(void*) == 8) |
| 50 | return 10; |
| 51 | #endif |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) { |
| 56 | // We strongly match "powerpc-*". |
| 57 | std::string TT = M.getTargetTriple(); |
| 58 | if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-") |
| 59 | return 20; |
| 60 | |
| 61 | // If the target triple is something non-powerpc, we don't match. |
| 62 | if (!TT.empty()) return 0; |
| 63 | |
| 64 | if (M.getEndianness() == Module::BigEndian && |
| 65 | M.getPointerSize() == Module::Pointer32) |
| 66 | return 10; // Weak match |
| 67 | else if (M.getEndianness() != Module::AnyEndianness || |
| 68 | M.getPointerSize() != Module::AnyPointerSize) |
| 69 | return 0; // Match for some other target |
| 70 | |
| 71 | return getJITMatchQuality()/2; |
| 72 | } |
| 73 | |
| 74 | unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) { |
| 75 | // We strongly match "powerpc64-*". |
| 76 | std::string TT = M.getTargetTriple(); |
| 77 | if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-") |
| 78 | return 20; |
| 79 | |
| 80 | if (M.getEndianness() == Module::BigEndian && |
| 81 | M.getPointerSize() == Module::Pointer64) |
| 82 | return 10; // Weak match |
| 83 | else if (M.getEndianness() != Module::AnyEndianness || |
| 84 | M.getPointerSize() != Module::AnyPointerSize) |
| 85 | return 0; // Match for some other target |
| 86 | |
| 87 | return getJITMatchQuality()/2; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS, |
| 92 | bool is64Bit) |
| 93 | : Subtarget(*this, M, FS, is64Bit), |
| 94 | DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this), |
| 95 | FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this), |
| 96 | InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) { |
| 97 | |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 98 | if (getRelocationModel() == Reloc::Default) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 99 | if (Subtarget.isDarwin()) |
| 100 | setRelocationModel(Reloc::DynamicNoPIC); |
| 101 | else |
| 102 | setRelocationModel(Reloc::Static); |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 103 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /// Override this for PowerPC. Tail merging happily breaks up instruction issue |
| 107 | /// groups, which typically degrades performance. |
Dan Gohman | 62ea18a | 2007-11-19 20:46:23 +0000 | [diff] [blame] | 108 | bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 109 | |
| 110 | PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS) |
| 111 | : PPCTargetMachine(M, FS, false) { |
| 112 | } |
| 113 | |
| 114 | |
| 115 | PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS) |
| 116 | : PPCTargetMachine(M, FS, true) { |
| 117 | } |
| 118 | |
| 119 | |
| 120 | //===----------------------------------------------------------------------===// |
| 121 | // Pass Pipeline Configuration |
| 122 | //===----------------------------------------------------------------------===// |
| 123 | |
Dan Gohman | e34aa77 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 124 | bool PPCTargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 125 | // Install an instruction selector. |
| 126 | PM.add(createPPCISelDag(*this)); |
| 127 | return false; |
| 128 | } |
| 129 | |
Dan Gohman | e34aa77 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 130 | bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM, bool Fast) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 131 | |
| 132 | // Must run branch selection immediately preceding the asm printer. |
| 133 | PM.add(createPPCBranchSelectionPass()); |
| 134 | return false; |
| 135 | } |
| 136 | |
Dan Gohman | e34aa77 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 137 | bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast, |
Owen Anderson | 847b99b | 2008-08-21 00:14:44 +0000 | [diff] [blame^] | 138 | raw_ostream &Out) { |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 139 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 140 | if (AsmPrinterCtor) |
| 141 | PM.add(AsmPrinterCtor(Out, *this)); |
| 142 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | |
Dan Gohman | e34aa77 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 146 | bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast, |
Evan Cheng | 7754721 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 147 | bool DumpAsm, MachineCodeEmitter &MCE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 148 | // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64. |
| 149 | // FIXME: This should be moved to TargetJITInfo!! |
| 150 | if (Subtarget.isPPC64()) { |
| 151 | // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many |
| 152 | // instructions to materialize arbitrary global variable + function + |
| 153 | // constant pool addresses. |
| 154 | setRelocationModel(Reloc::PIC_); |
Dale Johannesen | 493492f | 2008-07-31 18:13:12 +0000 | [diff] [blame] | 155 | // Temporary workaround for the inability of PPC64 JIT to handle jump |
| 156 | // tables. |
| 157 | DisableJumpTables = true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 158 | } else { |
| 159 | setRelocationModel(Reloc::Static); |
| 160 | } |
| 161 | |
| 162 | // Inform the subtarget that we are in JIT mode. FIXME: does this break macho |
| 163 | // writing? |
| 164 | Subtarget.SetJITMode(); |
| 165 | |
| 166 | // Machine code emitter pass for PowerPC. |
| 167 | PM.add(createPPCCodeEmitterPass(*this, MCE)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 168 | if (DumpAsm) { |
| 169 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 170 | if (AsmPrinterCtor) |
Owen Anderson | 847b99b | 2008-08-21 00:14:44 +0000 | [diff] [blame^] | 171 | PM.add(AsmPrinterCtor(errs(), *this)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 174 | return false; |
| 175 | } |
| 176 | |
Dan Gohman | e34aa77 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 177 | bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast, |
Evan Cheng | 7754721 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 178 | bool DumpAsm, MachineCodeEmitter &MCE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 179 | // Machine code emitter pass for PowerPC. |
| 180 | PM.add(createPPCCodeEmitterPass(*this, MCE)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 181 | if (DumpAsm) { |
| 182 | assert(AsmPrinterCtor && "AsmPrinter was not linked in"); |
| 183 | if (AsmPrinterCtor) |
Owen Anderson | 847b99b | 2008-08-21 00:14:44 +0000 | [diff] [blame^] | 184 | PM.add(AsmPrinterCtor(errs(), *this)); |
Anton Korobeynikov | 01c0e9f | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 187 | return false; |
| 188 | } |