Change createAsmParser to take a MCSubtargetInfo instead of triple,
[oota-llvm.git] / lib / Target / PowerPC / PPCSubtarget.cpp
1 //===- PowerPCSubtarget.cpp - PPC Subtarget Information -------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the PPC specific subclass of TargetSubtargetInfo.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "PPCSubtarget.h"
15 #include "PPC.h"
16 #include "llvm/GlobalValue.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Target/TargetRegistry.h"
19 #include <cstdlib>
20
21 #define GET_SUBTARGETINFO_ENUM
22 #define GET_SUBTARGETINFO_MC_DESC
23 #define GET_SUBTARGETINFO_TARGET_DESC
24 #define GET_SUBTARGETINFO_CTOR
25 #include "PPCGenSubtargetInfo.inc"
26
27 using namespace llvm;
28
29 #if defined(__APPLE__)
30 #include <mach/mach.h>
31 #include <mach/mach_host.h>
32 #include <mach/host_info.h>
33 #include <mach/machine.h>
34
35 /// GetCurrentPowerPCFeatures - Returns the current CPUs features.
36 static const char *GetCurrentPowerPCCPU() {
37   host_basic_info_data_t hostInfo;
38   mach_msg_type_number_t infoCount;
39
40   infoCount = HOST_BASIC_INFO_COUNT;
41   host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, 
42             &infoCount);
43             
44   if (hostInfo.cpu_type != CPU_TYPE_POWERPC) return "generic";
45
46   switch(hostInfo.cpu_subtype) {
47   case CPU_SUBTYPE_POWERPC_601:   return "601";
48   case CPU_SUBTYPE_POWERPC_602:   return "602";
49   case CPU_SUBTYPE_POWERPC_603:   return "603";
50   case CPU_SUBTYPE_POWERPC_603e:  return "603e";
51   case CPU_SUBTYPE_POWERPC_603ev: return "603ev";
52   case CPU_SUBTYPE_POWERPC_604:   return "604";
53   case CPU_SUBTYPE_POWERPC_604e:  return "604e";
54   case CPU_SUBTYPE_POWERPC_620:   return "620";
55   case CPU_SUBTYPE_POWERPC_750:   return "750";
56   case CPU_SUBTYPE_POWERPC_7400:  return "7400";
57   case CPU_SUBTYPE_POWERPC_7450:  return "7450";
58   case CPU_SUBTYPE_POWERPC_970:   return "970";
59   default: ;
60   }
61   
62   return "generic";
63 }
64 #endif
65
66
67 PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU,
68                            const std::string &FS, bool is64Bit)
69   : PPCGenSubtargetInfo(TT, CPU, FS)
70   , StackAlignment(16)
71   , DarwinDirective(PPC::DIR_NONE)
72   , IsGigaProcessor(false)
73   , Has64BitSupport(false)
74   , Use64BitRegs(false)
75   , IsPPC64(is64Bit)
76   , HasAltivec(false)
77   , HasFSQRT(false)
78   , HasSTFIWX(false)
79   , HasLazyResolverStubs(false)
80   , IsJITCodeModel(false)
81   , TargetTriple(TT) {
82
83   // Determine default and user specified characteristics
84   std::string CPUName = CPU;
85   if (CPUName.empty())
86     CPUName = "generic";
87 #if defined(__APPLE__)
88   if (CPUName == "generic")
89     CPUName = GetCurrentPowerPCCPU();
90 #endif
91
92   // Parse features string.
93   ParseSubtargetFeatures(CPUName, FS);
94
95   // Initialize scheduling itinerary for the specified CPU.
96   InstrItins = getInstrItineraryForCPU(CPUName);
97
98   // If we are generating code for ppc64, verify that options make sense.
99   if (is64Bit) {
100     Has64BitSupport = true;
101     // Silently force 64-bit register use on ppc64.
102     Use64BitRegs = true;
103   }
104   
105   // If the user requested use of 64-bit regs, but the cpu selected doesn't
106   // support it, ignore.
107   if (use64BitRegs() && !has64BitSupport())
108     Use64BitRegs = false;
109
110   // Set up darwin-specific properties.
111   if (isDarwin())
112     HasLazyResolverStubs = true;
113 }
114
115 /// SetJITMode - This is called to inform the subtarget info that we are
116 /// producing code for the JIT.
117 void PPCSubtarget::SetJITMode() {
118   // JIT mode doesn't want lazy resolver stubs, it knows exactly where
119   // everything is.  This matters for PPC64, which codegens in PIC mode without
120   // stubs.
121   HasLazyResolverStubs = false;
122
123   // Calls to external functions need to use indirect calls
124   IsJITCodeModel = true;
125 }
126
127
128 /// hasLazyResolverStub - Return true if accesses to the specified global have
129 /// to go through a dyld lazy resolution stub.  This means that an extra load
130 /// is required to get the address of the global.
131 bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV,
132                                        const TargetMachine &TM) const {
133   // We never have stubs if HasLazyResolverStubs=false or if in static mode.
134   if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
135     return false;
136   // If symbol visibility is hidden, the extra load is not needed if
137   // the symbol is definitely defined in the current translation unit.
138   bool isDecl = GV->isDeclaration() && !GV->isMaterializable();
139   if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
140     return false;
141   return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
142          GV->hasCommonLinkage() || isDecl;
143 }
144
145 MCSubtargetInfo *createPPCMCSubtargetInfo(StringRef TT, StringRef CPU,
146                                           StringRef FS) {
147   MCSubtargetInfo *X = new MCSubtargetInfo();
148   InitPPCMCSubtargetInfo(X, CPU, FS);
149   return X;
150 }
151
152 extern "C" void LLVMInitializePowerPCMCSubtargetInfo() {
153   TargetRegistry::RegisterMCSubtargetInfo(ThePPC32Target,
154                                           createPPCMCSubtargetInfo);
155   TargetRegistry::RegisterMCSubtargetInfo(ThePPC64Target,
156                                           createPPCMCSubtargetInfo);
157 }