LLVM API Documentation
00001 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the X86 specific subclass of TargetMachine. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "X86TargetMachine.h" 00015 #include "X86.h" 00016 #include "llvm/Module.h" 00017 #include "llvm/PassManager.h" 00018 #include "llvm/CodeGen/MachineFunction.h" 00019 #include "llvm/CodeGen/Passes.h" 00020 #include "llvm/Target/TargetOptions.h" 00021 #include "llvm/Target/TargetMachineRegistry.h" 00022 #include "llvm/Transforms/Scalar.h" 00023 #include "llvm/Support/CommandLine.h" 00024 #include "llvm/ADT/Statistic.h" 00025 #include <iostream> 00026 using namespace llvm; 00027 00028 /// X86TargetMachineModule - Note that this is used on hosts that cannot link 00029 /// in a library unless there are references into the library. In particular, 00030 /// it seems that it is not possible to get things to work on Win32 without 00031 /// this. Though it is unused, do not remove it. 00032 extern "C" int X86TargetMachineModule; 00033 int X86TargetMachineModule = 0; 00034 00035 namespace { 00036 cl::opt<bool> DisableOutput("disable-x86-llc-output", cl::Hidden, 00037 cl::desc("Disable the X86 asm printer, for use " 00038 "when profiling the code generator.")); 00039 // Register the target. 00040 RegisterTarget<X86TargetMachine> X("x86", " IA-32 (Pentium and above)"); 00041 } 00042 00043 unsigned X86TargetMachine::getJITMatchQuality() { 00044 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86) 00045 return 10; 00046 #else 00047 return 0; 00048 #endif 00049 } 00050 00051 unsigned X86TargetMachine::getModuleMatchQuality(const Module &M) { 00052 // We strongly match "i[3-9]86-*". 00053 std::string TT = M.getTargetTriple(); 00054 if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' && 00055 TT[4] == '-' && TT[1] - '3' < 6) 00056 return 20; 00057 00058 if (M.getEndianness() == Module::LittleEndian && 00059 M.getPointerSize() == Module::Pointer32) 00060 return 10; // Weak match 00061 else if (M.getEndianness() != Module::AnyEndianness || 00062 M.getPointerSize() != Module::AnyPointerSize) 00063 return 0; // Match for some other target 00064 00065 return getJITMatchQuality()/2; 00066 } 00067 00068 /// X86TargetMachine ctor - Create an ILP32 architecture model 00069 /// 00070 X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS) 00071 : TargetMachine("X86"), 00072 Subtarget(M, FS), 00073 DataLayout("e-p:32:32-d:32-l:32"), 00074 FrameInfo(TargetFrameInfo::StackGrowsDown, 00075 Subtarget.getStackAlignment(), -4), 00076 InstrInfo(*this), JITInfo(*this), TLInfo(*this) { 00077 if (getRelocationModel() == Reloc::Default) 00078 if (Subtarget.isTargetDarwin()) 00079 setRelocationModel(Reloc::DynamicNoPIC); 00080 else 00081 setRelocationModel(Reloc::PIC_); 00082 } 00083 00084 00085 // addPassesToEmitFile - We currently use all of the same passes as the JIT 00086 // does to emit statically compiled machine code. 00087 bool X86TargetMachine::addPassesToEmitFile(PassManager &PM, std::ostream &Out, 00088 CodeGenFileType FileType, 00089 bool Fast) { 00090 if (FileType != TargetMachine::AssemblyFile && 00091 FileType != TargetMachine::ObjectFile) return true; 00092 00093 // Run loop strength reduction before anything else. 00094 PM.add(createLoopStrengthReducePass(&TLInfo)); 00095 00096 // FIXME: Implement efficient support for garbage collection intrinsics. 00097 PM.add(createLowerGCPass()); 00098 00099 // FIXME: Implement the invoke/unwind instructions! 00100 PM.add(createLowerInvokePass()); 00101 00102 // Make sure that no unreachable blocks are instruction selected. 00103 PM.add(createUnreachableBlockEliminationPass()); 00104 00105 // Install an instruction selector. 00106 PM.add(createX86ISelDag(*this)); 00107 00108 // Print the instruction selected machine code... 00109 if (PrintMachineCode) 00110 PM.add(createMachineFunctionPrinterPass(&std::cerr)); 00111 00112 // Perform register allocation to convert to a concrete x86 representation 00113 PM.add(createRegisterAllocator()); 00114 00115 if (PrintMachineCode) 00116 PM.add(createMachineFunctionPrinterPass(&std::cerr)); 00117 00118 PM.add(createX86FloatingPointStackifierPass()); 00119 00120 if (PrintMachineCode) 00121 PM.add(createMachineFunctionPrinterPass(&std::cerr)); 00122 00123 // Insert prolog/epilog code. Eliminate abstract frame index references... 00124 PM.add(createPrologEpilogCodeInserter()); 00125 00126 if (PrintMachineCode) // Print the register-allocated code 00127 PM.add(createX86CodePrinterPass(std::cerr, *this)); 00128 00129 if (!DisableOutput) 00130 switch (FileType) { 00131 default: 00132 assert(0 && "Unexpected filetype here!"); 00133 case TargetMachine::AssemblyFile: 00134 PM.add(createX86CodePrinterPass(Out, *this)); 00135 break; 00136 case TargetMachine::ObjectFile: 00137 // FIXME: We only support emission of ELF files for now, this should check 00138 // the target triple and decide on the format to write (e.g. COFF on 00139 // win32). 00140 addX86ELFObjectWriterPass(PM, Out, *this); 00141 break; 00142 } 00143 00144 // Delete machine code for this function 00145 PM.add(createMachineCodeDeleter()); 00146 00147 return false; // success! 00148 } 00149 00150 /// addPassesToJITCompile - Add passes to the specified pass manager to 00151 /// implement a fast dynamic compiler for this target. Return true if this is 00152 /// not supported for this target. 00153 /// 00154 void X86JITInfo::addPassesToJITCompile(FunctionPassManager &PM) { 00155 // The JIT should use static relocation model. 00156 TM.setRelocationModel(Reloc::Static); 00157 00158 // Run loop strength reduction before anything else. 00159 PM.add(createLoopStrengthReducePass(TM.getTargetLowering())); 00160 00161 // FIXME: Implement efficient support for garbage collection intrinsics. 00162 PM.add(createLowerGCPass()); 00163 00164 // FIXME: Implement the invoke/unwind instructions! 00165 PM.add(createLowerInvokePass()); 00166 00167 // Make sure that no unreachable blocks are instruction selected. 00168 PM.add(createUnreachableBlockEliminationPass()); 00169 00170 // Install an instruction selector. 00171 PM.add(createX86ISelDag(TM)); 00172 00173 // Print the instruction selected machine code... 00174 if (PrintMachineCode) 00175 PM.add(createMachineFunctionPrinterPass(&std::cerr)); 00176 00177 // Perform register allocation to convert to a concrete x86 representation 00178 PM.add(createRegisterAllocator()); 00179 00180 if (PrintMachineCode) 00181 PM.add(createMachineFunctionPrinterPass(&std::cerr)); 00182 00183 PM.add(createX86FloatingPointStackifierPass()); 00184 00185 if (PrintMachineCode) 00186 PM.add(createMachineFunctionPrinterPass(&std::cerr)); 00187 00188 // Insert prolog/epilog code. Eliminate abstract frame index references... 00189 PM.add(createPrologEpilogCodeInserter()); 00190 00191 if (PrintMachineCode) // Print the register-allocated code 00192 PM.add(createX86CodePrinterPass(std::cerr, TM)); 00193 } 00194 00195 bool X86TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM, 00196 MachineCodeEmitter &MCE) { 00197 PM.add(createX86CodeEmitterPass(*this, MCE)); 00198 // Delete machine code for this function 00199 PM.add(createMachineCodeDeleter()); 00200 return false; 00201 }