LLVM API Documentation
00001 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the X86 specific subclass of TargetMachine. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "X86TargetAsmInfo.h" 00015 #include "X86TargetMachine.h" 00016 #include "X86.h" 00017 #include "llvm/PassManager.h" 00018 #include "llvm/CodeGen/MachineFunction.h" 00019 #include "llvm/CodeGen/Passes.h" 00020 #include "llvm/Support/FormattedStream.h" 00021 #include "llvm/Target/TargetOptions.h" 00022 #include "llvm/Target/TargetRegistry.h" 00023 using namespace llvm; 00024 00025 static const TargetAsmInfo *createTargetAsmInfo(const Target &T, 00026 const StringRef &TT) { 00027 Triple TheTriple(TT); 00028 switch (TheTriple.getOS()) { 00029 case Triple::Darwin: 00030 return new X86DarwinTargetAsmInfo(TheTriple); 00031 case Triple::MinGW32: 00032 case Triple::MinGW64: 00033 case Triple::Cygwin: 00034 return new X86COFFTargetAsmInfo(TheTriple); 00035 case Triple::Win32: 00036 return new X86WinTargetAsmInfo(TheTriple); 00037 default: 00038 return new X86ELFTargetAsmInfo(TheTriple); 00039 } 00040 } 00041 00042 extern "C" void LLVMInitializeX86Target() { 00043 // Register the target. 00044 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target); 00045 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target); 00046 00047 // Register the target asm info. 00048 RegisterAsmInfoFn A(TheX86_32Target, createTargetAsmInfo); 00049 RegisterAsmInfoFn B(TheX86_64Target, createTargetAsmInfo); 00050 } 00051 00052 00053 X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT, 00054 const std::string &FS) 00055 : X86TargetMachine(T, TT, FS, false) { 00056 } 00057 00058 00059 X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT, 00060 const std::string &FS) 00061 : X86TargetMachine(T, TT, FS, true) { 00062 } 00063 00064 /// X86TargetMachine ctor - Create an X86 target. 00065 /// 00066 X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT, 00067 const std::string &FS, bool is64Bit) 00068 : LLVMTargetMachine(T, TT), 00069 Subtarget(TT, FS, is64Bit), 00070 DataLayout(Subtarget.getDataLayout()), 00071 FrameInfo(TargetFrameInfo::StackGrowsDown, 00072 Subtarget.getStackAlignment(), 00073 (Subtarget.isTargetWin64() ? -40 : 00074 (Subtarget.is64Bit() ? -8 : -4))), 00075 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) { 00076 DefRelocModel = getRelocationModel(); 00077 00078 // If no relocation model was picked, default as appropriate for the target. 00079 if (getRelocationModel() == Reloc::Default) { 00080 if (!Subtarget.isTargetDarwin()) 00081 setRelocationModel(Reloc::Static); 00082 else if (Subtarget.is64Bit()) 00083 setRelocationModel(Reloc::PIC_); 00084 else 00085 setRelocationModel(Reloc::DynamicNoPIC); 00086 } 00087 00088 assert(getRelocationModel() != Reloc::Default && 00089 "Relocation mode not picked"); 00090 00091 // If no code model is picked, default to small. 00092 if (getCodeModel() == CodeModel::Default) 00093 setCodeModel(CodeModel::Small); 00094 00095 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC 00096 // is defined as a model for code which may be used in static or dynamic 00097 // executables but not necessarily a shared library. On X86-32 we just 00098 // compile in -static mode, in x86-64 we use PIC. 00099 if (getRelocationModel() == Reloc::DynamicNoPIC) { 00100 if (is64Bit) 00101 setRelocationModel(Reloc::PIC_); 00102 else if (!Subtarget.isTargetDarwin()) 00103 setRelocationModel(Reloc::Static); 00104 } 00105 00106 // If we are on Darwin, disallow static relocation model in X86-64 mode, since 00107 // the Mach-O file format doesn't support it. 00108 if (getRelocationModel() == Reloc::Static && 00109 Subtarget.isTargetDarwin() && 00110 is64Bit) 00111 setRelocationModel(Reloc::PIC_); 00112 00113 // Determine the PICStyle based on the target selected. 00114 if (getRelocationModel() == Reloc::Static) { 00115 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None. 00116 Subtarget.setPICStyle(PICStyles::None); 00117 } else if (Subtarget.isTargetCygMing()) { 00118 Subtarget.setPICStyle(PICStyles::None); 00119 } else if (Subtarget.isTargetDarwin()) { 00120 if (Subtarget.is64Bit()) 00121 Subtarget.setPICStyle(PICStyles::RIPRel); 00122 else if (getRelocationModel() == Reloc::PIC_) 00123 Subtarget.setPICStyle(PICStyles::StubPIC); 00124 else { 00125 assert(getRelocationModel() == Reloc::DynamicNoPIC); 00126 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC); 00127 } 00128 } else if (Subtarget.isTargetELF()) { 00129 if (Subtarget.is64Bit()) 00130 Subtarget.setPICStyle(PICStyles::RIPRel); 00131 else 00132 Subtarget.setPICStyle(PICStyles::GOT); 00133 } 00134 00135 // Finally, if we have "none" as our PIC style, force to static mode. 00136 if (Subtarget.getPICStyle() == PICStyles::None) 00137 setRelocationModel(Reloc::Static); 00138 } 00139 00140 //===----------------------------------------------------------------------===// 00141 // Pass Pipeline Configuration 00142 //===----------------------------------------------------------------------===// 00143 00144 bool X86TargetMachine::addInstSelector(PassManagerBase &PM, 00145 CodeGenOpt::Level OptLevel) { 00146 // Install an instruction selector. 00147 PM.add(createX86ISelDag(*this, OptLevel)); 00148 00149 // If we're using Fast-ISel, clean up the mess. 00150 if (EnableFastISel) 00151 PM.add(createDeadMachineInstructionElimPass()); 00152 00153 // Install a pass to insert x87 FP_REG_KILL instructions, as needed. 00154 PM.add(createX87FPRegKillInserterPass()); 00155 00156 return false; 00157 } 00158 00159 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM, 00160 CodeGenOpt::Level OptLevel) { 00161 // Calculate and set max stack object alignment early, so we can decide 00162 // whether we will need stack realignment (and thus FP). 00163 PM.add(createX86MaxStackAlignmentCalculatorPass()); 00164 return false; // -print-machineinstr shouldn't print after this. 00165 } 00166 00167 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM, 00168 CodeGenOpt::Level OptLevel) { 00169 PM.add(createX86FloatingPointStackifierPass()); 00170 return true; // -print-machineinstr should print after this. 00171 } 00172 00173 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, 00174 CodeGenOpt::Level OptLevel, 00175 MachineCodeEmitter &MCE) { 00176 // FIXME: Move this to TargetJITInfo! 00177 // On Darwin, do not override 64-bit setting made in X86TargetMachine(). 00178 if (DefRelocModel == Reloc::Default && 00179 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) { 00180 setRelocationModel(Reloc::Static); 00181 Subtarget.setPICStyle(PICStyles::None); 00182 } 00183 00184 // 64-bit JIT places everything in the same buffer except external functions. 00185 // On Darwin, use small code model but hack the call instruction for 00186 // externals. Elsewhere, do not assume globals are in the lower 4G. 00187 if (Subtarget.is64Bit()) { 00188 if (Subtarget.isTargetDarwin()) 00189 setCodeModel(CodeModel::Small); 00190 else 00191 setCodeModel(CodeModel::Large); 00192 } 00193 00194 PM.add(createX86CodeEmitterPass(*this, MCE)); 00195 00196 return false; 00197 } 00198 00199 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, 00200 CodeGenOpt::Level OptLevel, 00201 JITCodeEmitter &JCE) { 00202 // FIXME: Move this to TargetJITInfo! 00203 // On Darwin, do not override 64-bit setting made in X86TargetMachine(). 00204 if (DefRelocModel == Reloc::Default && 00205 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) { 00206 setRelocationModel(Reloc::Static); 00207 Subtarget.setPICStyle(PICStyles::None); 00208 } 00209 00210 // 64-bit JIT places everything in the same buffer except external functions. 00211 // On Darwin, use small code model but hack the call instruction for 00212 // externals. Elsewhere, do not assume globals are in the lower 4G. 00213 if (Subtarget.is64Bit()) { 00214 if (Subtarget.isTargetDarwin()) 00215 setCodeModel(CodeModel::Small); 00216 else 00217 setCodeModel(CodeModel::Large); 00218 } 00219 00220 PM.add(createX86JITCodeEmitterPass(*this, JCE)); 00221 00222 return false; 00223 } 00224 00225 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, 00226 CodeGenOpt::Level OptLevel, 00227 ObjectCodeEmitter &OCE) { 00228 PM.add(createX86ObjectCodeEmitterPass(*this, OCE)); 00229 return false; 00230 } 00231 00232 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, 00233 CodeGenOpt::Level OptLevel, 00234 MachineCodeEmitter &MCE) { 00235 PM.add(createX86CodeEmitterPass(*this, MCE)); 00236 return false; 00237 } 00238 00239 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, 00240 CodeGenOpt::Level OptLevel, 00241 JITCodeEmitter &JCE) { 00242 PM.add(createX86JITCodeEmitterPass(*this, JCE)); 00243 return false; 00244 } 00245 00246 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, 00247 CodeGenOpt::Level OptLevel, 00248 ObjectCodeEmitter &OCE) { 00249 PM.add(createX86ObjectCodeEmitterPass(*this, OCE)); 00250 return false; 00251 }