LLVM API Documentation

X86TargetMachine.cpp

Go to the documentation of this file.
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/Module.h"
00018 #include "llvm/PassManager.h"
00019 #include "llvm/CodeGen/MachineFunction.h"
00020 #include "llvm/CodeGen/Passes.h"
00021 #include "llvm/Support/raw_ostream.h"
00022 #include "llvm/Target/TargetOptions.h"
00023 #include "llvm/Target/TargetMachineRegistry.h"
00024 using namespace llvm;
00025 
00026 /// X86TargetMachineModule - Note that this is used on hosts that cannot link
00027 /// in a library unless there are references into the library.  In particular,
00028 /// it seems that it is not possible to get things to work on Win32 without
00029 /// this.  Though it is unused, do not remove it.
00030 extern "C" int X86TargetMachineModule;
00031 int X86TargetMachineModule = 0;
00032 
00033 // Register the target.
00034 static RegisterTarget<X86_32TargetMachine>
00035 X("x86",    "32-bit X86: Pentium-Pro and above");
00036 static RegisterTarget<X86_64TargetMachine>
00037 Y("x86-64", "64-bit X86: EM64T and AMD64");
00038 
00039 // No assembler printer by default
00040 X86TargetMachine::AsmPrinterCtorFn X86TargetMachine::AsmPrinterCtor = 0;
00041 
00042 const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
00043   if (Subtarget.isFlavorIntel())
00044     return new X86WinTargetAsmInfo(*this);
00045   else
00046     switch (Subtarget.TargetType) {
00047      case X86Subtarget::isDarwin:
00048       return new X86DarwinTargetAsmInfo(*this);
00049      case X86Subtarget::isELF:
00050       return new X86ELFTargetAsmInfo(*this);
00051      case X86Subtarget::isMingw:
00052      case X86Subtarget::isCygwin:
00053       return new X86COFFTargetAsmInfo(*this);
00054      case X86Subtarget::isWindows:
00055       return new X86WinTargetAsmInfo(*this);
00056      default:
00057       return new X86GenericTargetAsmInfo(*this);
00058     }
00059 }
00060 
00061 unsigned X86_32TargetMachine::getJITMatchQuality() {
00062 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
00063   return 10;
00064 #endif
00065   return 0;
00066 }
00067 
00068 unsigned X86_64TargetMachine::getJITMatchQuality() {
00069 #if defined(__x86_64__) || defined(_M_AMD64)
00070   return 10;
00071 #endif
00072   return 0;
00073 }
00074 
00075 unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
00076   // We strongly match "i[3-9]86-*".
00077   std::string TT = M.getTargetTriple();
00078   if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
00079       TT[4] == '-' && TT[1] - '3' < 6)
00080     return 20;
00081   // If the target triple is something non-X86, we don't match.
00082   if (!TT.empty()) return 0;
00083 
00084   if (M.getEndianness()  == Module::LittleEndian &&
00085       M.getPointerSize() == Module::Pointer32)
00086     return 10;                                   // Weak match
00087   else if (M.getEndianness() != Module::AnyEndianness ||
00088            M.getPointerSize() != Module::AnyPointerSize)
00089     return 0;                                    // Match for some other target
00090 
00091   return getJITMatchQuality()/2;
00092 }
00093 
00094 unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
00095   // We strongly match "x86_64-*".
00096   std::string TT = M.getTargetTriple();
00097   if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
00098       TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
00099     return 20;
00100 
00101   // We strongly match "amd64-*".
00102   if (TT.size() >= 6 && TT[0] == 'a' && TT[1] == 'm' && TT[2] == 'd' &&
00103       TT[3] == '6' && TT[4] == '4' && TT[5] == '-')
00104     return 20;
00105   
00106   // If the target triple is something non-X86-64, we don't match.
00107   if (!TT.empty()) return 0;
00108 
00109   if (M.getEndianness()  == Module::LittleEndian &&
00110       M.getPointerSize() == Module::Pointer64)
00111     return 10;                                   // Weak match
00112   else if (M.getEndianness() != Module::AnyEndianness ||
00113            M.getPointerSize() != Module::AnyPointerSize)
00114     return 0;                                    // Match for some other target
00115 
00116   return getJITMatchQuality()/2;
00117 }
00118 
00119 X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS) 
00120   : X86TargetMachine(M, FS, false) {
00121 }
00122 
00123 
00124 X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
00125   : X86TargetMachine(M, FS, true) {
00126 }
00127 
00128 /// X86TargetMachine ctor - Create an ILP32 architecture model
00129 ///
00130 X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
00131                                    bool is64Bit)
00132   : Subtarget(M, FS, is64Bit),
00133     DataLayout(Subtarget.getDataLayout()),
00134     FrameInfo(TargetFrameInfo::StackGrowsDown,
00135               Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
00136     InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
00137   DefRelocModel = getRelocationModel();
00138   // FIXME: Correctly select PIC model for Win64 stuff
00139   if (getRelocationModel() == Reloc::Default) {
00140     if (Subtarget.isTargetDarwin() ||
00141         (Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64()))
00142       setRelocationModel(Reloc::DynamicNoPIC);
00143     else
00144       setRelocationModel(Reloc::Static);
00145   }
00146 
00147   // ELF doesn't have a distinct dynamic-no-PIC model. Dynamic-no-PIC
00148   // is defined as a model for code which may be used in static or
00149   // dynamic executables but not necessarily a shared library. On ELF
00150   // implement this by using the Static model.
00151   if (Subtarget.isTargetELF() &&
00152       getRelocationModel() == Reloc::DynamicNoPIC)
00153     setRelocationModel(Reloc::Static);
00154 
00155   if (Subtarget.is64Bit()) {
00156     // No DynamicNoPIC support under X86-64.
00157     if (getRelocationModel() == Reloc::DynamicNoPIC)
00158       setRelocationModel(Reloc::PIC_);
00159     // Default X86-64 code model is small.
00160     if (getCodeModel() == CodeModel::Default)
00161       setCodeModel(CodeModel::Small);
00162   }
00163 
00164   if (Subtarget.isTargetCygMing())
00165     Subtarget.setPICStyle(PICStyles::WinPIC);
00166   else if (Subtarget.isTargetDarwin()) {
00167     if (Subtarget.is64Bit())
00168       Subtarget.setPICStyle(PICStyles::RIPRel);
00169     else
00170       Subtarget.setPICStyle(PICStyles::Stub);
00171   } else if (Subtarget.isTargetELF()) {
00172     if (Subtarget.is64Bit())
00173       Subtarget.setPICStyle(PICStyles::RIPRel);
00174     else
00175       Subtarget.setPICStyle(PICStyles::GOT);
00176   }
00177 }
00178 
00179 //===----------------------------------------------------------------------===//
00180 // Pass Pipeline Configuration
00181 //===----------------------------------------------------------------------===//
00182 
00183 bool X86TargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
00184   // Install an instruction selector.
00185   PM.add(createX86ISelDag(*this, Fast));
00186 
00187   // If we're using Fast-ISel, clean up the mess.
00188   if (EnableFastISel)
00189     PM.add(createDeadMachineInstructionElimPass());
00190 
00191   // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
00192   PM.add(createX87FPRegKillInserterPass());
00193 
00194   return false;
00195 }
00196 
00197 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM, bool Fast) {
00198   // Calculate and set max stack object alignment early, so we can decide
00199   // whether we will need stack realignment (and thus FP).
00200   PM.add(createX86MaxStackAlignmentCalculatorPass());
00201   return false;  // -print-machineinstr shouldn't print after this.
00202 }
00203 
00204 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM, bool Fast) {
00205   PM.add(createX86FloatingPointStackifierPass());
00206   return true;  // -print-machineinstr should print after this.
00207 }
00208 
00209 bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast, 
00210                                           raw_ostream &Out) {
00211   assert(AsmPrinterCtor && "AsmPrinter was not linked in");
00212   if (AsmPrinterCtor)
00213     PM.add(AsmPrinterCtor(Out, *this));
00214   return false;
00215 }
00216 
00217 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
00218                                       bool DumpAsm, MachineCodeEmitter &MCE) {
00219   // FIXME: Move this to TargetJITInfo!
00220   // On Darwin, do not override 64-bit setting made in X86TargetMachine().
00221   if (DefRelocModel == Reloc::Default && 
00222         (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit()))
00223     setRelocationModel(Reloc::Static);
00224   
00225   // 64-bit JIT places everything in the same buffer except external functions.
00226   // On Darwin, use small code model but hack the call instruction for 
00227   // externals.  Elsewhere, do not assume globals are in the lower 4G.
00228   if (Subtarget.is64Bit()) {
00229     if (Subtarget.isTargetDarwin())
00230       setCodeModel(CodeModel::Small);
00231     else
00232       setCodeModel(CodeModel::Large);
00233   }
00234 
00235   PM.add(createX86CodeEmitterPass(*this, MCE));
00236   if (DumpAsm) {
00237     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
00238     if (AsmPrinterCtor)
00239       PM.add(AsmPrinterCtor(errs(), *this));
00240   }
00241 
00242   return false;
00243 }
00244 
00245 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
00246                                         bool DumpAsm, MachineCodeEmitter &MCE) {
00247   PM.add(createX86CodeEmitterPass(*this, MCE));
00248   if (DumpAsm) {
00249     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
00250     if (AsmPrinterCtor)
00251       PM.add(AsmPrinterCtor(errs(), *this));
00252   }
00253 
00254   return false;
00255 }
00256 
00257 // symbolicAddressesAreRIPRel - Return true if symbolic addresses are
00258 // RIP-relative on this machine, taking into consideration the relocation
00259 // model and subtarget. RIP-relative addresses cannot have a separate
00260 // base or index register.
00261 bool X86TargetMachine::symbolicAddressesAreRIPRel() const {
00262   return getRelocationModel() != Reloc::Static &&
00263          Subtarget.isPICStyleRIPRel();
00264 }



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.