Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===// |
| 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 | // This file defines the X86 specific subclass of TargetMachine. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "X86TargetAsmInfo.h" |
| 15 | #include "X86TargetMachine.h" |
| 16 | #include "X86.h" |
| 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/PassManager.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
| 20 | #include "llvm/CodeGen/Passes.h" |
| 21 | #include "llvm/Target/TargetOptions.h" |
| 22 | #include "llvm/Target/TargetMachineRegistry.h" |
| 23 | #include "llvm/Transforms/Scalar.h" |
| 24 | using namespace llvm; |
| 25 | |
| 26 | /// X86TargetMachineModule - Note that this is used on hosts that cannot link |
| 27 | /// in a library unless there are references into the library. In particular, |
| 28 | /// it seems that it is not possible to get things to work on Win32 without |
| 29 | /// this. Though it is unused, do not remove it. |
| 30 | extern "C" int X86TargetMachineModule; |
| 31 | int X86TargetMachineModule = 0; |
| 32 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 33 | // Register the target. |
| 34 | static RegisterTarget<X86_32TargetMachine> |
| 35 | X("x86", " 32-bit X86: Pentium-Pro and above"); |
| 36 | static RegisterTarget<X86_64TargetMachine> |
| 37 | Y("x86-64", " 64-bit X86: EM64T and AMD64"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 38 | |
| 39 | const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const { |
| 40 | return new X86TargetAsmInfo(*this); |
| 41 | } |
| 42 | |
| 43 | unsigned X86_32TargetMachine::getJITMatchQuality() { |
| 44 | #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86) |
| 45 | return 10; |
| 46 | #endif |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | unsigned X86_64TargetMachine::getJITMatchQuality() { |
Anton Korobeynikov | 5734866 | 2008-03-23 13:43:47 +0000 | [diff] [blame] | 51 | #if defined(__x86_64__) || defined(_M_AMD64) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 52 | return 10; |
| 53 | #endif |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) { |
| 58 | // We strongly match "i[3-9]86-*". |
| 59 | std::string TT = M.getTargetTriple(); |
| 60 | if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' && |
| 61 | TT[4] == '-' && TT[1] - '3' < 6) |
| 62 | return 20; |
| 63 | // If the target triple is something non-X86, we don't match. |
| 64 | if (!TT.empty()) return 0; |
| 65 | |
| 66 | if (M.getEndianness() == Module::LittleEndian && |
| 67 | M.getPointerSize() == Module::Pointer32) |
| 68 | return 10; // Weak match |
| 69 | else if (M.getEndianness() != Module::AnyEndianness || |
| 70 | M.getPointerSize() != Module::AnyPointerSize) |
| 71 | return 0; // Match for some other target |
| 72 | |
| 73 | return getJITMatchQuality()/2; |
| 74 | } |
| 75 | |
| 76 | unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) { |
| 77 | // We strongly match "x86_64-*". |
| 78 | std::string TT = M.getTargetTriple(); |
| 79 | if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' && |
| 80 | TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-') |
| 81 | return 20; |
| 82 | |
| 83 | // We strongly match "amd64-*". |
| 84 | if (TT.size() >= 6 && TT[0] == 'a' && TT[1] == 'm' && TT[2] == 'd' && |
| 85 | TT[3] == '6' && TT[4] == '4' && TT[5] == '-') |
| 86 | return 20; |
| 87 | |
| 88 | // If the target triple is something non-X86-64, we don't match. |
| 89 | if (!TT.empty()) return 0; |
| 90 | |
| 91 | if (M.getEndianness() == Module::LittleEndian && |
| 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 | X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS) |
| 102 | : X86TargetMachine(M, FS, false) { |
| 103 | } |
| 104 | |
| 105 | |
| 106 | X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS) |
| 107 | : X86TargetMachine(M, FS, true) { |
| 108 | } |
| 109 | |
| 110 | /// X86TargetMachine ctor - Create an ILP32 architecture model |
| 111 | /// |
| 112 | X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS, |
| 113 | bool is64Bit) |
| 114 | : Subtarget(M, FS, is64Bit), |
Dale Johannesen | d4c671c | 2007-08-06 21:48:35 +0000 | [diff] [blame] | 115 | DataLayout(Subtarget.getDataLayout()), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 116 | FrameInfo(TargetFrameInfo::StackGrowsDown, |
| 117 | Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4), |
| 118 | InstrInfo(*this), JITInfo(*this), TLInfo(*this) { |
Evan Cheng | 8ee6bab | 2007-12-22 09:40:20 +0000 | [diff] [blame] | 119 | DefRelocModel = getRelocationModel(); |
Anton Korobeynikov | 0ab9fa8 | 2008-03-23 13:41:18 +0000 | [diff] [blame] | 120 | // FIXME: Correctly select PIC model for Win64 stuff |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 121 | if (getRelocationModel() == Reloc::Default) { |
Anton Korobeynikov | 0ab9fa8 | 2008-03-23 13:41:18 +0000 | [diff] [blame] | 122 | if (Subtarget.isTargetDarwin() || |
| 123 | (Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 124 | setRelocationModel(Reloc::DynamicNoPIC); |
| 125 | else |
| 126 | setRelocationModel(Reloc::Static); |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 127 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 128 | if (Subtarget.is64Bit()) { |
| 129 | // No DynamicNoPIC support under X86-64. |
| 130 | if (getRelocationModel() == Reloc::DynamicNoPIC) |
| 131 | setRelocationModel(Reloc::PIC_); |
| 132 | // Default X86-64 code model is small. |
| 133 | if (getCodeModel() == CodeModel::Default) |
| 134 | setCodeModel(CodeModel::Small); |
| 135 | } |
| 136 | |
| 137 | if (Subtarget.isTargetCygMing()) |
| 138 | Subtarget.setPICStyle(PICStyle::WinPIC); |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 139 | else if (Subtarget.isTargetDarwin()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 140 | if (Subtarget.is64Bit()) |
| 141 | Subtarget.setPICStyle(PICStyle::RIPRel); |
| 142 | else |
| 143 | Subtarget.setPICStyle(PICStyle::Stub); |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 144 | } else if (Subtarget.isTargetELF()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 145 | if (Subtarget.is64Bit()) |
| 146 | Subtarget.setPICStyle(PICStyle::RIPRel); |
| 147 | else |
| 148 | Subtarget.setPICStyle(PICStyle::GOT); |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 149 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | //===----------------------------------------------------------------------===// |
| 153 | // Pass Pipeline Configuration |
| 154 | //===----------------------------------------------------------------------===// |
| 155 | |
Dan Gohman | e34aa77 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 156 | bool X86TargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 157 | // Install an instruction selector. |
| 158 | PM.add(createX86ISelDag(*this, Fast)); |
| 159 | return false; |
| 160 | } |
| 161 | |
Anton Korobeynikov | 194ae6c | 2008-04-23 18:23:05 +0000 | [diff] [blame] | 162 | bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM, bool Fast) { |
Anton Korobeynikov | 8c91289 | 2008-04-23 18:23:30 +0000 | [diff] [blame] | 163 | // Calculate and set max stack object alignment early, so we can decide |
| 164 | // whether we will need stack realignment (and thus FP). |
Anton Korobeynikov | 194ae6c | 2008-04-23 18:23:05 +0000 | [diff] [blame] | 165 | PM.add(createX86MaxStackAlignmentCalculatorPass()); |
| 166 | return false; // -print-machineinstr shouldn't print after this. |
| 167 | } |
| 168 | |
Dan Gohman | e34aa77 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 169 | bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM, bool Fast) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 170 | PM.add(createX86FloatingPointStackifierPass()); |
| 171 | return true; // -print-machineinstr should print after this. |
| 172 | } |
| 173 | |
Dan Gohman | e34aa77 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 174 | bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 175 | std::ostream &Out) { |
| 176 | PM.add(createX86CodePrinterPass(Out, *this)); |
| 177 | return false; |
| 178 | } |
| 179 | |
Dan Gohman | e34aa77 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 180 | bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast, |
Evan Cheng | 7754721 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 181 | bool DumpAsm, MachineCodeEmitter &MCE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 182 | // FIXME: Move this to TargetJITInfo! |
Evan Cheng | b00ae57 | 2008-01-08 02:07:10 +0000 | [diff] [blame] | 183 | if (DefRelocModel == Reloc::Default) { |
Evan Cheng | 8ee6bab | 2007-12-22 09:40:20 +0000 | [diff] [blame] | 184 | setRelocationModel(Reloc::Static); |
Evan Cheng | b00ae57 | 2008-01-08 02:07:10 +0000 | [diff] [blame] | 185 | Subtarget.setPICStyle(PICStyle::None); |
| 186 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 187 | |
| 188 | // JIT cannot ensure globals are placed in the lower 4G of address. |
| 189 | if (Subtarget.is64Bit()) |
| 190 | setCodeModel(CodeModel::Large); |
| 191 | |
| 192 | PM.add(createX86CodeEmitterPass(*this, MCE)); |
Evan Cheng | 7754721 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 193 | if (DumpAsm) |
| 194 | PM.add(createX86CodePrinterPass(*cerr.stream(), *this)); |
| 195 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 196 | return false; |
| 197 | } |
| 198 | |
Dan Gohman | e34aa77 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 199 | bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast, |
Evan Cheng | 7754721 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 200 | bool DumpAsm, MachineCodeEmitter &MCE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 201 | PM.add(createX86CodeEmitterPass(*this, MCE)); |
Evan Cheng | 7754721 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 202 | if (DumpAsm) |
| 203 | PM.add(createX86CodePrinterPass(*cerr.stream(), *this)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 204 | return false; |
| 205 | } |