Add the PPC64 ldbrx/stdbrx instructions
[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 "PPCRegisterInfo.h"
17 #include "llvm/IR/GlobalValue.h"
18 #include "llvm/Support/Host.h"
19 #include "llvm/Support/TargetRegistry.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include <cstdlib>
22
23 #define GET_SUBTARGETINFO_TARGET_DESC
24 #define GET_SUBTARGETINFO_CTOR
25 #include "PPCGenSubtargetInfo.inc"
26
27 using namespace llvm;
28
29 PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU,
30                            const std::string &FS, bool is64Bit)
31   : PPCGenSubtargetInfo(TT, CPU, FS)
32   , StackAlignment(16)
33   , DarwinDirective(PPC::DIR_NONE)
34   , HasMFOCRF(false)
35   , Has64BitSupport(false)
36   , Use64BitRegs(false)
37   , IsPPC64(is64Bit)
38   , HasAltivec(false)
39   , HasQPX(false)
40   , HasFSQRT(false)
41   , HasSTFIWX(false)
42   , HasISEL(false)
43   , HasPOPCNTD(false)
44   , HasLDBRX(false)
45   , IsBookE(false)
46   , HasLazyResolverStubs(false)
47   , IsJITCodeModel(false)
48   , TargetTriple(TT) {
49
50   // Determine default and user specified characteristics
51   std::string CPUName = CPU;
52   if (CPUName.empty())
53     CPUName = "generic";
54 #if (defined(__APPLE__) || defined(__linux__)) && \
55     (defined(__ppc__) || defined(__powerpc__))
56   if (CPUName == "generic")
57     CPUName = sys::getHostCPUName();
58 #endif
59
60   // Initialize scheduling itinerary for the specified CPU.
61   InstrItins = getInstrItineraryForCPU(CPUName);
62
63   // Make sure 64-bit features are available when CPUname is generic
64   std::string FullFS = FS;
65
66   // If we are generating code for ppc64, verify that options make sense.
67   if (is64Bit) {
68     Has64BitSupport = true;
69     // Silently force 64-bit register use on ppc64.
70     Use64BitRegs = true;
71     if (!FullFS.empty())
72       FullFS = "+64bit," + FullFS;
73     else
74       FullFS = "+64bit";
75   }
76
77   // Parse features string.
78   ParseSubtargetFeatures(CPUName, FullFS);
79
80   // If the user requested use of 64-bit regs, but the cpu selected doesn't
81   // support it, ignore.
82   if (use64BitRegs() && !has64BitSupport())
83     Use64BitRegs = false;
84
85   // Set up darwin-specific properties.
86   if (isDarwin())
87     HasLazyResolverStubs = true;
88
89   // QPX requires a 32-byte aligned stack. Note that we need to do this if
90   // we're compiling for a BG/Q system regardless of whether or not QPX
91   // is enabled because external functions will assume this alignment.
92   if (hasQPX() || isBGQ())
93     StackAlignment = 32;
94 }
95
96 /// SetJITMode - This is called to inform the subtarget info that we are
97 /// producing code for the JIT.
98 void PPCSubtarget::SetJITMode() {
99   // JIT mode doesn't want lazy resolver stubs, it knows exactly where
100   // everything is.  This matters for PPC64, which codegens in PIC mode without
101   // stubs.
102   HasLazyResolverStubs = false;
103
104   // Calls to external functions need to use indirect calls
105   IsJITCodeModel = true;
106 }
107
108
109 /// hasLazyResolverStub - Return true if accesses to the specified global have
110 /// to go through a dyld lazy resolution stub.  This means that an extra load
111 /// is required to get the address of the global.
112 bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV,
113                                        const TargetMachine &TM) const {
114   // We never have stubs if HasLazyResolverStubs=false or if in static mode.
115   if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
116     return false;
117   // If symbol visibility is hidden, the extra load is not needed if
118   // the symbol is definitely defined in the current translation unit.
119   bool isDecl = GV->isDeclaration() && !GV->isMaterializable();
120   if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
121     return false;
122   return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
123          GV->hasCommonLinkage() || isDecl;
124 }
125
126 bool PPCSubtarget::enablePostRAScheduler(
127            CodeGenOpt::Level OptLevel,
128            TargetSubtargetInfo::AntiDepBreakMode& Mode,
129            RegClassVector& CriticalPathRCs) const {
130   // FIXME: It would be best to use TargetSubtargetInfo::ANTIDEP_ALL here,
131   // but we can't because we can't reassign the cr registers. There is a
132   // dependence between the cr register and the RLWINM instruction used
133   // to extract its value which the anti-dependency breaker can't currently
134   // see. Maybe we should make a late-expanded pseudo to encode this dependency.
135   // (the relevant code is in PPCDAGToDAGISel::SelectSETCC)
136
137   Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
138
139   CriticalPathRCs.clear();
140
141   if (isPPC64())
142     CriticalPathRCs.push_back(&PPC::G8RCRegClass);
143   else
144     CriticalPathRCs.push_back(&PPC::GPRCRegClass);
145     
146   CriticalPathRCs.push_back(&PPC::F8RCRegClass);
147   CriticalPathRCs.push_back(&PPC::VRRCRegClass);
148
149   return OptLevel >= CodeGenOpt::Default;
150 }
151