LLVM API Documentation

PPCTargetMachine.cpp

Go to the documentation of this file.
00001 //===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
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 // Top-level implementation for the PowerPC target.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "PPC.h"
00015 #include "PPCTargetAsmInfo.h"
00016 #include "PPCTargetMachine.h"
00017 #include "llvm/Module.h"
00018 #include "llvm/PassManager.h"
00019 #include "llvm/Target/TargetMachineRegistry.h"
00020 #include "llvm/Target/TargetOptions.h"
00021 #include "llvm/Support/raw_ostream.h"
00022 using namespace llvm;
00023 
00024 /// PowerPCTargetMachineModule - Note that this is used on hosts that
00025 /// cannot link in a library unless there are references into the
00026 /// library.  In particular, it seems that it is not possible to get
00027 /// things to work on Win32 without this.  Though it is unused, do not
00028 /// remove it.
00029 extern "C" int PowerPCTargetMachineModule;
00030 int PowerPCTargetMachineModule = 0;
00031 
00032 // Register the targets
00033 static RegisterTarget<PPC32TargetMachine>
00034 X("ppc32", "PowerPC 32");
00035 static RegisterTarget<PPC64TargetMachine>
00036 Y("ppc64", "PowerPC 64");
00037 
00038 // No assembler printer by default
00039 PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0;
00040 
00041 const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
00042   if (Subtarget.isDarwin())
00043     return new PPCDarwinTargetAsmInfo(*this);
00044   else
00045     return new PPCLinuxTargetAsmInfo(*this);
00046 }
00047 
00048 unsigned PPC32TargetMachine::getJITMatchQuality() {
00049 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
00050   if (sizeof(void*) == 4)
00051     return 10;
00052 #endif
00053   return 0;
00054 }
00055 unsigned PPC64TargetMachine::getJITMatchQuality() {
00056 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
00057   if (sizeof(void*) == 8)
00058     return 10;
00059 #endif
00060   return 0;
00061 }
00062 
00063 unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
00064   // We strongly match "powerpc-*".
00065   std::string TT = M.getTargetTriple();
00066   if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
00067     return 20;
00068   
00069   // If the target triple is something non-powerpc, we don't match.
00070   if (!TT.empty()) return 0;
00071   
00072   if (M.getEndianness()  == Module::BigEndian &&
00073       M.getPointerSize() == Module::Pointer32)
00074     return 10;                                   // Weak match
00075   else if (M.getEndianness() != Module::AnyEndianness ||
00076            M.getPointerSize() != Module::AnyPointerSize)
00077     return 0;                                    // Match for some other target
00078   
00079   return getJITMatchQuality()/2;
00080 }
00081 
00082 unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
00083   // We strongly match "powerpc64-*".
00084   std::string TT = M.getTargetTriple();
00085   if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
00086     return 20;
00087   
00088   if (M.getEndianness()  == Module::BigEndian &&
00089       M.getPointerSize() == Module::Pointer64)
00090     return 10;                                   // Weak match
00091   else if (M.getEndianness() != Module::AnyEndianness ||
00092            M.getPointerSize() != Module::AnyPointerSize)
00093     return 0;                                    // Match for some other target
00094   
00095   return getJITMatchQuality()/2;
00096 }
00097 
00098 
00099 PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
00100                                    bool is64Bit)
00101   : Subtarget(*this, M, FS, is64Bit),
00102     DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
00103     FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
00104     InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
00105 
00106   if (getRelocationModel() == Reloc::Default) {
00107     if (Subtarget.isDarwin())
00108       setRelocationModel(Reloc::DynamicNoPIC);
00109     else
00110       setRelocationModel(Reloc::Static);
00111   }
00112 }
00113 
00114 /// Override this for PowerPC.  Tail merging happily breaks up instruction issue
00115 /// groups, which typically degrades performance.
00116 bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
00117 
00118 PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS) 
00119   : PPCTargetMachine(M, FS, false) {
00120 }
00121 
00122 
00123 PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
00124   : PPCTargetMachine(M, FS, true) {
00125 }
00126 
00127 
00128 //===----------------------------------------------------------------------===//
00129 // Pass Pipeline Configuration
00130 //===----------------------------------------------------------------------===//
00131 
00132 bool PPCTargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
00133   // Install an instruction selector.
00134   PM.add(createPPCISelDag(*this));
00135   return false;
00136 }
00137 
00138 bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM, bool Fast) {
00139   
00140   // Must run branch selection immediately preceding the asm printer.
00141   PM.add(createPPCBranchSelectionPass());
00142   return false;
00143 }
00144 
00145 bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast, 
00146                                           raw_ostream &Out) {
00147   assert(AsmPrinterCtor && "AsmPrinter was not linked in");
00148   if (AsmPrinterCtor)
00149     PM.add(AsmPrinterCtor(Out, *this));
00150 
00151   return false;
00152 }
00153 
00154 bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
00155                                       bool DumpAsm, MachineCodeEmitter &MCE) {
00156   // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
00157   // FIXME: This should be moved to TargetJITInfo!!
00158   if (Subtarget.isPPC64()) {
00159     // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
00160     // instructions to materialize arbitrary global variable + function +
00161     // constant pool addresses.
00162     setRelocationModel(Reloc::PIC_);
00163     // Temporary workaround for the inability of PPC64 JIT to handle jump
00164     // tables.
00165     DisableJumpTables = true;      
00166   } else {
00167     setRelocationModel(Reloc::Static);
00168   }
00169   
00170   // Inform the subtarget that we are in JIT mode.  FIXME: does this break macho
00171   // writing?
00172   Subtarget.SetJITMode();
00173   
00174   // Machine code emitter pass for PowerPC.
00175   PM.add(createPPCCodeEmitterPass(*this, MCE));
00176   if (DumpAsm) {
00177     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
00178     if (AsmPrinterCtor)
00179       PM.add(AsmPrinterCtor(errs(), *this));
00180   }
00181 
00182   return false;
00183 }
00184 
00185 bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
00186                                             bool DumpAsm, MachineCodeEmitter &MCE) {
00187   // Machine code emitter pass for PowerPC.
00188   PM.add(createPPCCodeEmitterPass(*this, MCE));
00189   if (DumpAsm) {
00190     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
00191     if (AsmPrinterCtor)
00192       PM.add(AsmPrinterCtor(errs(), *this));
00193   }
00194 
00195   return false;
00196 }