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