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