PPC: Enable aggressive anti-dependency breaking
[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   Mode = TargetSubtargetInfo::ANTIDEP_ALL;
169
170   CriticalPathRCs.clear();
171
172   if (isPPC64())
173     CriticalPathRCs.push_back(&PPC::G8RCRegClass);
174   else
175     CriticalPathRCs.push_back(&PPC::GPRCRegClass);
176     
177   return OptLevel >= CodeGenOpt::Default;
178 }
179
180 // Embedded cores need aggressive scheduling.
181 static bool needsAggressiveScheduling(unsigned Directive) {
182   switch (Directive) {
183   default: return false;
184   case PPC::DIR_440:
185   case PPC::DIR_A2:
186   case PPC::DIR_E500mc:
187   case PPC::DIR_E5500:
188     return true;
189   }
190 }
191
192 bool PPCSubtarget::enableMachineScheduler() const {
193   // Enable MI scheduling for the embedded cores.
194   // FIXME: Enable this for all cores (some additional modeling
195   // may be necessary).
196   return needsAggressiveScheduling(DarwinDirective);
197 }
198
199 void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
200                                        MachineInstr *begin,
201                                        MachineInstr *end,
202                                        unsigned NumRegionInstrs) const {
203   if (needsAggressiveScheduling(DarwinDirective)) {
204     Policy.OnlyTopDown = false;
205     Policy.OnlyBottomUp = false;
206   }
207
208   // Spilling is generally expensive on all PPC cores, so always enable
209   // register-pressure tracking.
210   Policy.ShouldTrackPressure = true;
211 }
212
213 bool PPCSubtarget::useAA() const {
214   // Use AA during code generation for the embedded cores.
215   return needsAggressiveScheduling(DarwinDirective);
216 }
217