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