[PPC64LE] Temporarily disable VSX support in little-endian mode
[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/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineScheduler.h"
19 #include "llvm/IR/Attributes.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/GlobalValue.h"
22 #include "llvm/Support/Host.h"
23 #include "llvm/Support/TargetRegistry.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include <cstdlib>
26
27 using namespace llvm;
28
29 #define DEBUG_TYPE "ppc-subtarget"
30
31 #define GET_SUBTARGETINFO_TARGET_DESC
32 #define GET_SUBTARGETINFO_CTOR
33 #include "PPCGenSubtargetInfo.inc"
34
35 PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU,
36                            const std::string &FS, bool is64Bit,
37                            CodeGenOpt::Level OptLevel)
38     : PPCGenSubtargetInfo(TT, CPU, FS), IsPPC64(is64Bit), TargetTriple(TT),
39       OptLevel(OptLevel) {
40   initializeEnvironment();
41   resetSubtargetFeatures(CPU, FS);
42 }
43
44 /// SetJITMode - This is called to inform the subtarget info that we are
45 /// producing code for the JIT.
46 void PPCSubtarget::SetJITMode() {
47   // JIT mode doesn't want lazy resolver stubs, it knows exactly where
48   // everything is.  This matters for PPC64, which codegens in PIC mode without
49   // stubs.
50   HasLazyResolverStubs = false;
51
52   // Calls to external functions need to use indirect calls
53   IsJITCodeModel = true;
54 }
55
56 void PPCSubtarget::resetSubtargetFeatures(const MachineFunction *MF) {
57   AttributeSet FnAttrs = MF->getFunction()->getAttributes();
58   Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
59                                            "target-cpu");
60   Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
61                                           "target-features");
62   std::string CPU =
63     !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : "";
64   std::string FS =
65     !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
66   if (!FS.empty()) {
67     initializeEnvironment();
68     resetSubtargetFeatures(CPU, FS);
69   }
70 }
71
72 void PPCSubtarget::initializeEnvironment() {
73   StackAlignment = 16;
74   DarwinDirective = PPC::DIR_NONE;
75   HasMFOCRF = false;
76   Has64BitSupport = false;
77   Use64BitRegs = false;
78   UseCRBits = false;
79   HasAltivec = false;
80   HasQPX = false;
81   HasVSX = false;
82   HasFCPSGN = false;
83   HasFSQRT = false;
84   HasFRE = false;
85   HasFRES = false;
86   HasFRSQRTE = false;
87   HasFRSQRTES = false;
88   HasRecipPrec = false;
89   HasSTFIWX = false;
90   HasLFIWAX = false;
91   HasFPRND = false;
92   HasFPCVT = false;
93   HasISEL = false;
94   HasPOPCNTD = false;
95   HasLDBRX = false;
96   IsBookE = false;
97   DeprecatedMFTB = false;
98   DeprecatedDST = false;
99   HasLazyResolverStubs = false;
100   IsJITCodeModel = false;
101 }
102
103 void PPCSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
104   // Determine default and user specified characteristics
105   std::string CPUName = CPU;
106   if (CPUName.empty())
107     CPUName = "generic";
108 #if (defined(__APPLE__) || defined(__linux__)) && \
109     (defined(__ppc__) || defined(__powerpc__))
110   if (CPUName == "generic")
111     CPUName = sys::getHostCPUName();
112 #endif
113
114   // Initialize scheduling itinerary for the specified CPU.
115   InstrItins = getInstrItineraryForCPU(CPUName);
116
117   // Make sure 64-bit features are available when CPUname is generic
118   std::string FullFS = FS;
119
120   // If we are generating code for ppc64, verify that options make sense.
121   if (IsPPC64) {
122     Has64BitSupport = true;
123     // Silently force 64-bit register use on ppc64.
124     Use64BitRegs = true;
125     if (!FullFS.empty())
126       FullFS = "+64bit," + FullFS;
127     else
128       FullFS = "+64bit";
129   }
130
131   // At -O2 and above, track CR bits as individual registers.
132   if (OptLevel >= CodeGenOpt::Default) {
133     if (!FullFS.empty())
134       FullFS = "+crbits," + FullFS;
135     else
136       FullFS = "+crbits";
137   }
138
139   // Parse features string.
140   ParseSubtargetFeatures(CPUName, FullFS);
141
142   // If the user requested use of 64-bit regs, but the cpu selected doesn't
143   // support it, ignore.
144   if (use64BitRegs() && !has64BitSupport())
145     Use64BitRegs = false;
146
147   // Set up darwin-specific properties.
148   if (isDarwin())
149     HasLazyResolverStubs = true;
150
151   // QPX requires a 32-byte aligned stack. Note that we need to do this if
152   // we're compiling for a BG/Q system regardless of whether or not QPX
153   // is enabled because external functions will assume this alignment.
154   if (hasQPX() || isBGQ())
155     StackAlignment = 32;
156
157   // Determine endianness.
158   IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le);
159
160   // FIXME: For now, we disable VSX in little-endian mode until endian
161   // issues in those instructions can be addressed.
162   if (IsLittleEndian)
163     HasVSX = false;
164 }
165
166 /// hasLazyResolverStub - Return true if accesses to the specified global have
167 /// to go through a dyld lazy resolution stub.  This means that an extra load
168 /// is required to get the address of the global.
169 bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV,
170                                        const TargetMachine &TM) const {
171   // We never have stubs if HasLazyResolverStubs=false or if in static mode.
172   if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
173     return false;
174   // If symbol visibility is hidden, the extra load is not needed if
175   // the symbol is definitely defined in the current translation unit.
176   bool isDecl = GV->isDeclaration() && !GV->isMaterializable();
177   if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
178     return false;
179   return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
180          GV->hasCommonLinkage() || isDecl;
181 }
182
183 bool PPCSubtarget::enablePostRAScheduler(
184            CodeGenOpt::Level OptLevel,
185            TargetSubtargetInfo::AntiDepBreakMode& Mode,
186            RegClassVector& CriticalPathRCs) const {
187   Mode = TargetSubtargetInfo::ANTIDEP_ALL;
188
189   CriticalPathRCs.clear();
190
191   if (isPPC64())
192     CriticalPathRCs.push_back(&PPC::G8RCRegClass);
193   else
194     CriticalPathRCs.push_back(&PPC::GPRCRegClass);
195     
196   return OptLevel >= CodeGenOpt::Default;
197 }
198
199 // Embedded cores need aggressive scheduling (and some others also benefit).
200 static bool needsAggressiveScheduling(unsigned Directive) {
201   switch (Directive) {
202   default: return false;
203   case PPC::DIR_440:
204   case PPC::DIR_A2:
205   case PPC::DIR_E500mc:
206   case PPC::DIR_E5500:
207   case PPC::DIR_PWR7:
208     return true;
209   }
210 }
211
212 bool PPCSubtarget::enableMachineScheduler() const {
213   // Enable MI scheduling for the embedded cores.
214   // FIXME: Enable this for all cores (some additional modeling
215   // may be necessary).
216   return needsAggressiveScheduling(DarwinDirective);
217 }
218
219 void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
220                                        MachineInstr *begin,
221                                        MachineInstr *end,
222                                        unsigned NumRegionInstrs) const {
223   if (needsAggressiveScheduling(DarwinDirective)) {
224     Policy.OnlyTopDown = false;
225     Policy.OnlyBottomUp = false;
226   }
227
228   // Spilling is generally expensive on all PPC cores, so always enable
229   // register-pressure tracking.
230   Policy.ShouldTrackPressure = true;
231 }
232
233 bool PPCSubtarget::useAA() const {
234   // Use AA during code generation for the embedded cores.
235   return needsAggressiveScheduling(DarwinDirective);
236 }
237