blob: 12d0326855ccdc551bad4cf378e45d18383ecbd1 [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- PowerPCSubtarget.cpp - PPC Subtarget Information ------------------===//
Nate Begeman8c00f8c2005-08-04 07:12:09 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman8c00f8c2005-08-04 07:12:09 +00007//
8//===----------------------------------------------------------------------===//
9//
Evan Cheng5b1b44892011-07-01 21:01:15 +000010// This file implements the PPC specific subclass of TargetSubtargetInfo.
Nate Begeman8c00f8c2005-08-04 07:12:09 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner26689592005-10-14 23:51:18 +000014#include "PPCSubtarget.h"
15#include "PPC.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "PPCRegisterInfo.h"
Hal Finkela44c37f2013-07-15 22:29:40 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/IR/Attributes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/GlobalValue.h"
Hal Finkela44c37f2013-07-15 22:29:40 +000020#include "llvm/IR/Function.h"
Hal Finkel4db738a2012-06-12 03:03:13 +000021#include "llvm/Support/Host.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000022#include "llvm/Support/TargetRegistry.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Target/TargetMachine.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000024#include <cstdlib>
Evan Cheng94214702011-07-01 20:45:01 +000025
Evan Cheng94214702011-07-01 20:45:01 +000026#define GET_SUBTARGETINFO_TARGET_DESC
Evan Chengebdeeab2011-07-08 01:53:10 +000027#define GET_SUBTARGETINFO_CTOR
Evan Cheng385e9302011-07-01 22:36:09 +000028#include "PPCGenSubtargetInfo.inc"
Evan Cheng94214702011-07-01 20:45:01 +000029
Chris Lattner3c304a32005-08-05 22:05:03 +000030using namespace llvm;
Chris Lattner3c304a32005-08-05 22:05:03 +000031
Evan Cheng276365d2011-06-30 01:53:36 +000032PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU,
33 const std::string &FS, bool is64Bit)
Evan Cheng0ddff1b2011-07-07 07:07:08 +000034 : PPCGenSubtargetInfo(TT, CPU, FS)
Chris Lattner7c1fb5f2006-06-16 17:50:12 +000035 , IsPPC64(is64Bit)
Daniel Dunbar869eca12011-04-19 20:54:28 +000036 , TargetTriple(TT) {
Hal Finkela44c37f2013-07-15 22:29:40 +000037 initializeEnvironment();
38 resetSubtargetFeatures(CPU, FS);
39}
Chris Lattner3c304a32005-08-05 22:05:03 +000040
Hal Finkela44c37f2013-07-15 22:29:40 +000041/// SetJITMode - This is called to inform the subtarget info that we are
42/// producing code for the JIT.
43void PPCSubtarget::SetJITMode() {
44 // JIT mode doesn't want lazy resolver stubs, it knows exactly where
45 // everything is. This matters for PPC64, which codegens in PIC mode without
46 // stubs.
47 HasLazyResolverStubs = false;
48
49 // Calls to external functions need to use indirect calls
50 IsJITCodeModel = true;
51}
52
53void PPCSubtarget::resetSubtargetFeatures(const MachineFunction *MF) {
54 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
55 Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
56 "target-cpu");
57 Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
58 "target-features");
59 std::string CPU =
60 !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : "";
61 std::string FS =
62 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
63 if (!FS.empty()) {
64 initializeEnvironment();
65 resetSubtargetFeatures(CPU, FS);
66 }
67}
68
69void PPCSubtarget::initializeEnvironment() {
70 StackAlignment = 16;
71 DarwinDirective = PPC::DIR_NONE;
72 HasMFOCRF = false;
73 Has64BitSupport = false;
74 Use64BitRegs = false;
75 HasAltivec = false;
76 HasQPX = false;
77 HasFSQRT = false;
78 HasFRE = false;
79 HasFRES = false;
80 HasFRSQRTE = false;
81 HasFRSQRTES = false;
82 HasRecipPrec = false;
83 HasSTFIWX = false;
84 HasLFIWAX = false;
85 HasFPRND = false;
86 HasFPCVT = false;
87 HasISEL = false;
88 HasPOPCNTD = false;
89 HasLDBRX = false;
90 IsBookE = false;
91 HasLazyResolverStubs = false;
92 IsJITCodeModel = false;
93}
94
95void PPCSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
Jim Laskeyb1e11802005-09-01 21:38:21 +000096 // Determine default and user specified characteristics
Evan Cheng276365d2011-06-30 01:53:36 +000097 std::string CPUName = CPU;
98 if (CPUName.empty())
99 CPUName = "generic";
Hal Finkel4db738a2012-06-12 03:03:13 +0000100#if (defined(__APPLE__) || defined(__linux__)) && \
101 (defined(__ppc__) || defined(__powerpc__))
Evan Cheng276365d2011-06-30 01:53:36 +0000102 if (CPUName == "generic")
Hal Finkel4db738a2012-06-12 03:03:13 +0000103 CPUName = sys::getHostCPUName();
Jim Laskeyb1e11802005-09-01 21:38:21 +0000104#endif
Jim Laskey581a8f72005-10-26 17:30:34 +0000105
Evan Cheng94214702011-07-01 20:45:01 +0000106 // Initialize scheduling itinerary for the specified CPU.
107 InstrItins = getInstrItineraryForCPU(CPUName);
108
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000109 // Make sure 64-bit features are available when CPUname is generic
110 std::string FullFS = FS;
111
Chris Lattner7c1fb5f2006-06-16 17:50:12 +0000112 // If we are generating code for ppc64, verify that options make sense.
Hal Finkela44c37f2013-07-15 22:29:40 +0000113 if (IsPPC64) {
Dale Johannesen3b407442008-02-15 18:40:53 +0000114 Has64BitSupport = true;
Chris Lattner8fa05da2006-06-16 20:05:06 +0000115 // Silently force 64-bit register use on ppc64.
116 Use64BitRegs = true;
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000117 if (!FullFS.empty())
118 FullFS = "+64bit," + FullFS;
119 else
120 FullFS = "+64bit";
Chris Lattner7c1fb5f2006-06-16 17:50:12 +0000121 }
Will Schmidte3709192012-10-04 16:20:24 +0000122
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000123 // Parse features string.
124 ParseSubtargetFeatures(CPUName, FullFS);
125
Chris Lattner7c1fb5f2006-06-16 17:50:12 +0000126 // If the user requested use of 64-bit regs, but the cpu selected doesn't
Dale Johannesen3b407442008-02-15 18:40:53 +0000127 // support it, ignore.
128 if (use64BitRegs() && !has64BitSupport())
Chris Lattner7c1fb5f2006-06-16 17:50:12 +0000129 Use64BitRegs = false;
Chris Lattner57fc62c2006-12-11 23:22:45 +0000130
131 // Set up darwin-specific properties.
Chris Lattner74da6712009-08-11 22:49:34 +0000132 if (isDarwin())
Chris Lattner57fc62c2006-12-11 23:22:45 +0000133 HasLazyResolverStubs = true;
Hal Finkel9a79b322013-01-30 23:43:27 +0000134
135 // QPX requires a 32-byte aligned stack. Note that we need to do this if
136 // we're compiling for a BG/Q system regardless of whether or not QPX
137 // is enabled because external functions will assume this alignment.
138 if (hasQPX() || isBGQ())
139 StackAlignment = 32;
Bill Schmidtf38cc382013-07-26 01:35:43 +0000140
141 // Determine endianness.
142 IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le);
Chris Lattner57fc62c2006-12-11 23:22:45 +0000143}
144
Chris Lattner57fc62c2006-12-11 23:22:45 +0000145/// hasLazyResolverStub - Return true if accesses to the specified global have
146/// to go through a dyld lazy resolution stub. This means that an extra load
147/// is required to get the address of the global.
Daniel Dunbar3be03402009-08-02 22:11:08 +0000148bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV,
149 const TargetMachine &TM) const {
Chris Lattner1e61e692010-11-15 02:46:57 +0000150 // We never have stubs if HasLazyResolverStubs=false or if in static mode.
Chris Lattner57fc62c2006-12-11 23:22:45 +0000151 if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
152 return false;
Evan Chengae94e592008-12-05 01:06:39 +0000153 // If symbol visibility is hidden, the extra load is not needed if
154 // the symbol is definitely defined in the current translation unit.
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000155 bool isDecl = GV->isDeclaration() && !GV->isMaterializable();
Evan Chengae94e592008-12-05 01:06:39 +0000156 if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
157 return false;
Chris Lattner57fc62c2006-12-11 23:22:45 +0000158 return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Evan Chengae94e592008-12-05 01:06:39 +0000159 GV->hasCommonLinkage() || isDecl;
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000160}
Hal Finkel64c34e22011-12-02 04:58:02 +0000161
162bool PPCSubtarget::enablePostRAScheduler(
163 CodeGenOpt::Level OptLevel,
164 TargetSubtargetInfo::AntiDepBreakMode& Mode,
165 RegClassVector& CriticalPathRCs) const {
Hal Finkel01a90f42012-06-10 11:15:36 +0000166 // FIXME: It would be best to use TargetSubtargetInfo::ANTIDEP_ALL here,
167 // but we can't because we can't reassign the cr registers. There is a
168 // dependence between the cr register and the RLWINM instruction used
169 // to extract its value which the anti-dependency breaker can't currently
170 // see. Maybe we should make a late-expanded pseudo to encode this dependency.
171 // (the relevant code is in PPCDAGToDAGISel::SelectSETCC)
172
173 Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
Hal Finkel64c34e22011-12-02 04:58:02 +0000174
Hal Finkel64c34e22011-12-02 04:58:02 +0000175 CriticalPathRCs.clear();
176
177 if (isPPC64())
178 CriticalPathRCs.push_back(&PPC::G8RCRegClass);
179 else
180 CriticalPathRCs.push_back(&PPC::GPRCRegClass);
Hal Finkel01a90f42012-06-10 11:15:36 +0000181
182 CriticalPathRCs.push_back(&PPC::F8RCRegClass);
183 CriticalPathRCs.push_back(&PPC::VRRCRegClass);
Hal Finkel64c34e22011-12-02 04:58:02 +0000184
185 return OptLevel >= CodeGenOpt::Default;
186}
187