blob: 25c3bc89a673ab1b31904ae8a012744f2d079dcf [file] [log] [blame]
Nate Begeman0b71e002005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattnerf22556d2005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf22556d2005-08-16 17:14:42 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattnerf22556d2005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6f3b9542005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Hal Finkel934361a2015-01-14 01:07:51 +000016#include "PPCCallingConv.h"
Strahinja Petrovice682b802016-05-09 12:27:39 +000017#include "PPCCCState.h"
Jim Laskey48850c12006-11-16 22:43:37 +000018#include "PPCMachineFunctionInfo.h"
Bill Wendlingdd3fe942010-03-12 02:00:43 +000019#include "PPCPerfectShuffle.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000020#include "PPCTargetMachine.h"
Bill Schmidt22d40dc2013-05-13 19:34:37 +000021#include "PPCTargetObjectFile.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000022#include "llvm/ADT/STLExtras.h"
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +000023#include "llvm/ADT/Statistic.h"
Hal Finkel0d8db462014-05-11 19:29:11 +000024#include "llvm/ADT/StringSwitch.h"
Eric Christopher89958332014-05-31 00:07:32 +000025#include "llvm/ADT/Triple.h"
Chris Lattner4f2e4e02007-03-06 00:59:59 +000026#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner9b577f12005-08-26 21:23:58 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
Hal Finkel57725662015-01-03 17:58:24 +000030#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000032#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000033#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/CallingConv.h"
35#include "llvm/IR/Constants.h"
36#include "llvm/IR/DerivedTypes.h"
37#include "llvm/IR/Function.h"
38#include "llvm/IR/Intrinsics.h"
Chris Lattnerce645542006-11-10 02:08:47 +000039#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000040#include "llvm/Support/ErrorHandling.h"
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +000041#include "llvm/Support/Format.h"
Craig Topperb25fda92012-03-17 18:46:09 +000042#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000043#include "llvm/Support/raw_ostream.h"
Craig Topperb25fda92012-03-17 18:46:09 +000044#include "llvm/Target/TargetOptions.h"
Hal Finkel1fb10e82016-05-12 04:00:56 +000045#include <list>
Kit Bartond4eb73c2015-05-05 16:10:44 +000046
Chris Lattnerf22556d2005-08-16 17:14:42 +000047using namespace llvm;
48
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +000049#define DEBUG_TYPE "ppc-lowering"
50
Hal Finkel595817e2012-06-04 02:21:00 +000051static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
52cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000053
Hal Finkel4e9f1a82012-06-10 19:32:29 +000054static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
55cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
56
Hal Finkel8d7fbc92013-03-15 15:27:13 +000057static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
58cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
59
Chuang-Yu Cheng0600e8d2016-04-26 07:38:24 +000060static cl::opt<bool> DisableSCO("disable-ppc-sco",
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +000061cl::desc("disable sibling call optimization on ppc"), cl::Hidden);
62
63STATISTIC(NumTailCalls, "Number of tail calls");
64STATISTIC(NumSiblingCalls, "Number of sibling calls");
65
Hal Finkel940ab932014-02-28 00:27:01 +000066// FIXME: Remove this once the bug has been fixed!
67extern cl::opt<bool> ANDIGlueBug;
68
Eric Christophercccae792015-01-30 22:02:31 +000069PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM,
70 const PPCSubtarget &STI)
71 : TargetLowering(TM), Subtarget(STI) {
Chris Lattnera028e7a2005-09-27 22:18:25 +000072 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000073 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000075
Chris Lattnerd10babf2010-10-10 18:34:00 +000076 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
77 // arguments are at least 4/8 bytes aligned.
Eric Christopherb1aaebe2014-06-12 22:38:18 +000078 bool isPPC64 = Subtarget.isPPC64();
Evan Cheng39e90022012-07-02 22:39:56 +000079 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000080
Chris Lattnerf22556d2005-08-16 17:14:42 +000081 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000082 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
Petar Jovanovic280f7102015-12-14 17:57:33 +000083 if (!Subtarget.useSoftFloat()) {
84 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
85 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
86 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000087
Evan Cheng5d9fd972006-10-04 00:56:09 +000088 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000089 for (MVT VT : MVT::integer_valuetypes()) {
90 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
91 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand);
92 }
Duncan Sands95d46ef2008-01-23 20:39:46 +000093
Owen Anderson9f944592009-08-11 20:47:22 +000094 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000095
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000096 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000097 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
98 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
99 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
100 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
101 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
Hal Finkel65d1cbf2015-02-05 18:42:53 +0000102 setIndexedLoadAction(ISD::PRE_INC, MVT::f32, Legal);
103 setIndexedLoadAction(ISD::PRE_INC, MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000104 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
105 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
106 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
107 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
108 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Hal Finkel65d1cbf2015-02-05 18:42:53 +0000109 setIndexedStoreAction(ISD::PRE_INC, MVT::f32, Legal);
110 setIndexedStoreAction(ISD::PRE_INC, MVT::f64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +0000111
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000112 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
114
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000115 if (isPPC64 || Subtarget.hasFPCVT()) {
Hal Finkel6a56b212014-03-05 22:14:00 +0000116 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
117 AddPromotedToType (ISD::SINT_TO_FP, MVT::i1,
118 isPPC64 ? MVT::i64 : MVT::i32);
119 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000120 AddPromotedToType(ISD::UINT_TO_FP, MVT::i1,
121 isPPC64 ? MVT::i64 : MVT::i32);
Hal Finkel6a56b212014-03-05 22:14:00 +0000122 } else {
123 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom);
124 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom);
125 }
Hal Finkel940ab932014-02-28 00:27:01 +0000126
127 // PowerPC does not support direct load / store of condition registers
128 setOperationAction(ISD::LOAD, MVT::i1, Custom);
129 setOperationAction(ISD::STORE, MVT::i1, Custom);
130
131 // FIXME: Remove this once the ANDI glue bug is fixed:
132 if (ANDIGlueBug)
133 setOperationAction(ISD::TRUNCATE, MVT::i1, Custom);
134
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000135 for (MVT VT : MVT::integer_valuetypes()) {
136 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
137 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
138 setTruncStoreAction(VT, MVT::i1, Expand);
139 }
Hal Finkel940ab932014-02-28 00:27:01 +0000140
141 addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass);
142 }
143
Dale Johannesen666323e2007-10-10 01:01:31 +0000144 // This is used in the ppcf128->int sequence. Note it has different semantics
145 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +0000146 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000147
Roman Divacky1faf5b02012-08-16 18:19:29 +0000148 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000149 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
150 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
151 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
152 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
153 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000154 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000155
Chris Lattnerf22556d2005-08-16 17:14:42 +0000156 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000157 setOperationAction(ISD::SREM, MVT::i32, Expand);
158 setOperationAction(ISD::UREM, MVT::i32, Expand);
159 setOperationAction(ISD::SREM, MVT::i64, Expand);
160 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000161
162 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000163 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
164 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
165 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
166 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
167 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
168 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
169 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
170 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000171
Dan Gohman482732a2007-10-11 23:21:31 +0000172 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000173 setOperationAction(ISD::FSIN , MVT::f64, Expand);
174 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000175 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000176 setOperationAction(ISD::FREM , MVT::f64, Expand);
177 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000178 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000179 setOperationAction(ISD::FSIN , MVT::f32, Expand);
180 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000181 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000182 setOperationAction(ISD::FREM , MVT::f32, Expand);
183 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000184 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000185
Owen Anderson9f944592009-08-11 20:47:22 +0000186 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000187
Chris Lattnerf22556d2005-08-16 17:14:42 +0000188 // If we're enabling GP optimizations, use hardware square root
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000189 if (!Subtarget.hasFSQRT() &&
Eric Christophercccae792015-01-30 22:02:31 +0000190 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTE() &&
191 Subtarget.hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000192 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000193
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000194 if (!Subtarget.hasFSQRT() &&
Eric Christophercccae792015-01-30 22:02:31 +0000195 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTES() &&
196 Subtarget.hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000197 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000198
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000199 if (Subtarget.hasFCPSGN()) {
Hal Finkeldbc78e12013-08-19 05:01:02 +0000200 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
201 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
202 } else {
203 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
204 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
205 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000206
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000207 if (Subtarget.hasFPRND()) {
Hal Finkelc20a08d2013-03-29 08:57:48 +0000208 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
209 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
210 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000211 setOperationAction(ISD::FROUND, MVT::f64, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000212
213 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
214 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
215 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000216 setOperationAction(ISD::FROUND, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000217 }
218
Nate Begeman2fba8a32006-01-14 03:14:10 +0000219 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000220 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000221 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
222 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000223 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000224
Hal Finkelfa7057a2016-03-29 01:36:01 +0000225 if (Subtarget.hasPOPCNTD() == PPCSubtarget::POPCNTD_Fast) {
Hal Finkel290376d2013-04-01 15:58:15 +0000226 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000227 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
228 } else {
229 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
230 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
231 }
232
Nate Begeman1b8121b2006-01-11 21:21:00 +0000233 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000234 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
235 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000236
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000237 if (!Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000238 // PowerPC does not have Select
239 setOperationAction(ISD::SELECT, MVT::i32, Expand);
240 setOperationAction(ISD::SELECT, MVT::i64, Expand);
241 setOperationAction(ISD::SELECT, MVT::f32, Expand);
242 setOperationAction(ISD::SELECT, MVT::f64, Expand);
243 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000244
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000245 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000246 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
247 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000248
Nate Begeman7e7f4392006-02-01 07:19:44 +0000249 // PowerPC wants to optimize integer setcc a bit
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000250 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000251 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000252
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000253 // PowerPC does not have BRCOND which requires SetCC
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000254 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000255 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000256
Owen Anderson9f944592009-08-11 20:47:22 +0000257 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000258
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000259 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000260 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000261
Jim Laskey6267b2c2005-08-17 00:40:22 +0000262 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000263 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
264 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000265
Nemanja Ivanovic5ebc92d2016-03-24 13:40:33 +0000266 if (Subtarget.hasDirectMove() && isPPC64) {
Nemanja Ivanovic89224762015-12-15 14:50:34 +0000267 setOperationAction(ISD::BITCAST, MVT::f32, Legal);
268 setOperationAction(ISD::BITCAST, MVT::i32, Legal);
269 setOperationAction(ISD::BITCAST, MVT::i64, Legal);
270 setOperationAction(ISD::BITCAST, MVT::f64, Legal);
271 } else {
272 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
273 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
274 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
275 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
276 }
Chris Lattnerc46fc242005-12-23 05:13:35 +0000277
Chris Lattner84b49d52006-04-28 21:56:10 +0000278 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000279 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000280
Hal Finkel1996f3d2013-03-27 19:10:42 +0000281 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000282 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
283 // support continuation, user-level threading, and etc.. As a result, no
284 // other SjLj exception interfaces are implemented and please don't build
285 // your own exception handling based on them.
286 // LLVM/Clang supports zero-cost DWARF exception handling.
287 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
288 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000289
290 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000291 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000292 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
293 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000294 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000295 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
296 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
297 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
298 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000299 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000300 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
301 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000302
Nate Begemanf69d13b2008-08-11 17:36:31 +0000303 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000304 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000305
306 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000307 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
308 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000309
Nate Begemane74795c2006-01-25 18:21:52 +0000310 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000311 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000312
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000313 if (Subtarget.isSVR4ABI()) {
Evan Cheng39e90022012-07-02 22:39:56 +0000314 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000315 // VAARG always uses double-word chunks, so promote anything smaller.
316 setOperationAction(ISD::VAARG, MVT::i1, Promote);
317 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
318 setOperationAction(ISD::VAARG, MVT::i8, Promote);
319 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
320 setOperationAction(ISD::VAARG, MVT::i16, Promote);
321 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
322 setOperationAction(ISD::VAARG, MVT::i32, Promote);
323 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
324 setOperationAction(ISD::VAARG, MVT::Other, Expand);
325 } else {
326 // VAARG is custom lowered with the 32-bit SVR4 ABI.
327 setOperationAction(ISD::VAARG, MVT::Other, Custom);
328 setOperationAction(ISD::VAARG, MVT::i64, Custom);
329 }
Roman Divacky4394e682011-06-28 15:30:42 +0000330 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000331 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000332
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000333 if (Subtarget.isSVR4ABI() && !isPPC64)
Roman Divackyc3825df2013-07-25 21:36:47 +0000334 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
335 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
336 else
337 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
338
Chris Lattner5bd514d2006-01-15 09:02:48 +0000339 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000340 setOperationAction(ISD::VAEND , MVT::Other, Expand);
341 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
342 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
343 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
344 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Yury Gribovd7dbb662015-12-01 11:40:55 +0000345 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i32, Custom);
346 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i64, Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000347
Chris Lattner6961fc72006-03-26 10:06:40 +0000348 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000349 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000350
Hal Finkel25c19922013-05-15 21:37:41 +0000351 // To handle counter-based loop conditions.
352 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
353
Dale Johannesen160be0f2008-11-07 22:54:33 +0000354 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000355 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
356 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
357 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
358 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
359 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
360 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
361 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
362 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
363 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
364 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
365 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
366 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000367
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000368 if (Subtarget.has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000369 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000370 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
371 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
372 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
373 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000374 // This is just the low 32 bits of a (signed) fp->i64 conversion.
375 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000376 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000377
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000378 if (Subtarget.hasLFIWAX() || Subtarget.isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000379 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000380 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000381 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000382 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000383 }
384
Hal Finkelf6d45f22013-04-01 17:52:07 +0000385 // With the instructions enabled under FPCVT, we can do everything.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000386 if (Subtarget.hasFPCVT()) {
387 if (Subtarget.has64BitSupport()) {
Hal Finkelf6d45f22013-04-01 17:52:07 +0000388 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
389 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
390 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
391 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
392 }
393
394 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
395 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
396 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
397 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
398 }
399
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000400 if (Subtarget.use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000401 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000402 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000403 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000404 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000405 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000406 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
407 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
408 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000409 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000410 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000411 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
412 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
413 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000414 }
Evan Cheng19264272006-03-01 01:11:20 +0000415
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000416 if (Subtarget.hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000417 // First set operation action for all vector types to expand. Then we
418 // will selectively turn on ones that can be effectively codegen'd.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000419 for (MVT VT : MVT::vector_valuetypes()) {
Chris Lattner06a21ba2006-04-16 01:37:57 +0000420 // add/sub are legal for all supported vector VT's.
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000421 setOperationAction(ISD::ADD, VT, Legal);
422 setOperationAction(ISD::SUB, VT, Legal);
423
Bill Schmidt433b1c32015-02-05 15:24:47 +0000424 // Vector instructions introduced in P8
Kit Bartond4eb73c2015-05-05 16:10:44 +0000425 if (Subtarget.hasP8Altivec() && (VT.SimpleTy != MVT::v1i128)) {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000426 setOperationAction(ISD::CTPOP, VT, Legal);
Bill Schmidt433b1c32015-02-05 15:24:47 +0000427 setOperationAction(ISD::CTLZ, VT, Legal);
428 }
429 else {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000430 setOperationAction(ISD::CTPOP, VT, Expand);
Bill Schmidt433b1c32015-02-05 15:24:47 +0000431 setOperationAction(ISD::CTLZ, VT, Expand);
432 }
Bill Schmidtfe88b182015-02-03 21:58:23 +0000433
Chris Lattner95c7adc2006-04-04 17:25:31 +0000434 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000435 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000436 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000437
438 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000439 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000440 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000441 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000442 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000443 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000444 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000445 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000446 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000447 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000448 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Hal Finkela2cdbce2015-08-30 22:12:50 +0000449 setOperationAction(ISD::SELECT_CC, VT, Promote);
450 AddPromotedToType (ISD::SELECT_CC, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000451 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000452 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000453
Chris Lattner06a21ba2006-04-16 01:37:57 +0000454 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000455 setOperationAction(ISD::MUL , VT, Expand);
456 setOperationAction(ISD::SDIV, VT, Expand);
457 setOperationAction(ISD::SREM, VT, Expand);
458 setOperationAction(ISD::UDIV, VT, Expand);
459 setOperationAction(ISD::UREM, VT, Expand);
460 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000461 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000462 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000463 setOperationAction(ISD::FSQRT, VT, Expand);
464 setOperationAction(ISD::FLOG, VT, Expand);
465 setOperationAction(ISD::FLOG10, VT, Expand);
466 setOperationAction(ISD::FLOG2, VT, Expand);
467 setOperationAction(ISD::FEXP, VT, Expand);
468 setOperationAction(ISD::FEXP2, VT, Expand);
469 setOperationAction(ISD::FSIN, VT, Expand);
470 setOperationAction(ISD::FCOS, VT, Expand);
471 setOperationAction(ISD::FABS, VT, Expand);
472 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000473 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000474 setOperationAction(ISD::FCEIL, VT, Expand);
475 setOperationAction(ISD::FTRUNC, VT, Expand);
476 setOperationAction(ISD::FRINT, VT, Expand);
477 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000478 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
479 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
480 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
Ulrich Weigand51eccec2014-08-04 13:27:12 +0000481 setOperationAction(ISD::MULHU, VT, Expand);
482 setOperationAction(ISD::MULHS, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000483 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
484 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
485 setOperationAction(ISD::UDIVREM, VT, Expand);
486 setOperationAction(ISD::SDIVREM, VT, Expand);
487 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
488 setOperationAction(ISD::FPOW, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000489 setOperationAction(ISD::BSWAP, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000490 setOperationAction(ISD::CTTZ, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000491 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000492 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Nemanja Ivanovic74e31bc2015-12-02 10:36:24 +0000493 setOperationAction(ISD::ROTL, VT, Expand);
494 setOperationAction(ISD::ROTR, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000495
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000496 for (MVT InnerVT : MVT::vector_valuetypes()) {
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000497 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000498 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
499 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
500 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
501 }
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000502 }
503
Chris Lattner95c7adc2006-04-04 17:25:31 +0000504 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
505 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000506 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000507
Owen Anderson9f944592009-08-11 20:47:22 +0000508 setOperationAction(ISD::AND , MVT::v4i32, Legal);
509 setOperationAction(ISD::OR , MVT::v4i32, Legal);
510 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
511 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Hal Finkel940ab932014-02-28 00:27:01 +0000512 setOperationAction(ISD::SELECT, MVT::v4i32,
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000513 Subtarget.useCRBits() ? Legal : Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000514 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000515 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
516 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
517 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
518 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000519 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
520 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
521 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
522 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000523
Craig Topperabadc662012-04-20 06:31:50 +0000524 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
525 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
526 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
527 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000528
Owen Anderson9f944592009-08-11 20:47:22 +0000529 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000530 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000531
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000532 if (TM.Options.UnsafeFPMath || Subtarget.hasVSX()) {
Hal Finkel2e103312013-04-03 04:01:11 +0000533 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
534 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
535 }
536
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000537 if (Subtarget.hasP8Altivec())
Kit Barton20d39812015-03-10 19:49:38 +0000538 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
539 else
540 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000541
Owen Anderson9f944592009-08-11 20:47:22 +0000542 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
543 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000544
Owen Anderson9f944592009-08-11 20:47:22 +0000545 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
546 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000547
Owen Anderson9f944592009-08-11 20:47:22 +0000548 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
549 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
550 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
551 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000552
553 // Altivec does not contain unordered floating-point compare instructions
554 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
555 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000556 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
557 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000558
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000559 if (Subtarget.hasVSX()) {
Hal Finkel27774d92014-03-13 07:58:58 +0000560 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
Nemanja Ivanovicd3896572015-10-09 11:12:18 +0000561 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
562 if (Subtarget.hasP8Vector()) {
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +0000563 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Nemanja Ivanovicd3896572015-10-09 11:12:18 +0000564 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
565 }
Nemanja Ivanovic5ebc92d2016-03-24 13:40:33 +0000566 if (Subtarget.hasDirectMove() && isPPC64) {
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +0000567 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Legal);
568 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Legal);
569 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Legal);
Nemanja Ivanovicbe5f0c02015-11-02 14:01:11 +0000570 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Legal);
Nemanja Ivanovicd3896572015-10-09 11:12:18 +0000571 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Legal);
572 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Legal);
573 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
574 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +0000575 }
Hal Finkel82569b62014-03-27 22:22:48 +0000576 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
Hal Finkel27774d92014-03-13 07:58:58 +0000577
578 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
579 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
580 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
581 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
582 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
583
584 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
585
586 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
587 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
588
589 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
590 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
591
Hal Finkel732f0f72014-03-26 12:49:28 +0000592 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
593 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal);
594 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
595 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
596 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
597
Hal Finkel27774d92014-03-13 07:58:58 +0000598 // Share the Altivec comparison restrictions.
599 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand);
600 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000601 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand);
602 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand);
603
Hal Finkel9281c9a2014-03-26 18:26:30 +0000604 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
605 setOperationAction(ISD::STORE, MVT::v2f64, Legal);
606
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000607 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal);
608
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +0000609 if (Subtarget.hasP8Vector())
610 addRegisterClass(MVT::f32, &PPC::VSSRCRegClass);
611
Hal Finkel19be5062014-03-29 05:29:01 +0000612 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000613
Bill Schmidt54cced52015-07-16 21:14:07 +0000614 addRegisterClass(MVT::v4i32, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000615 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass);
616 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass);
Hal Finkela6c8b512014-03-26 16:12:58 +0000617
Kit Barton0cfa7b72015-03-03 19:55:45 +0000618 if (Subtarget.hasP8Altivec()) {
Kit Bartone48b1e12015-03-05 16:24:38 +0000619 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
620 setOperationAction(ISD::SRA, MVT::v2i64, Legal);
621 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
622
Kit Barton0cfa7b72015-03-03 19:55:45 +0000623 setOperationAction(ISD::SETCC, MVT::v2i64, Legal);
624 }
625 else {
Kit Bartone48b1e12015-03-05 16:24:38 +0000626 setOperationAction(ISD::SHL, MVT::v2i64, Expand);
627 setOperationAction(ISD::SRA, MVT::v2i64, Expand);
628 setOperationAction(ISD::SRL, MVT::v2i64, Expand);
629
Kit Barton0cfa7b72015-03-03 19:55:45 +0000630 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
631
632 // VSX v2i64 only supports non-arithmetic operations.
633 setOperationAction(ISD::ADD, MVT::v2i64, Expand);
634 setOperationAction(ISD::SUB, MVT::v2i64, Expand);
635 }
Hal Finkel777c9dd2014-03-29 16:04:40 +0000636
Hal Finkel9281c9a2014-03-26 18:26:30 +0000637 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
638 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64);
639 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
640 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64);
641
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000642 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal);
643
Hal Finkel7279f4b2014-03-26 19:13:54 +0000644 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
645 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
646 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
647 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
648
Hal Finkel5c0d1452014-03-30 13:22:59 +0000649 // Vector operation legalization checks the result type of
650 // SIGN_EXTEND_INREG, overall legalization checks the inner type.
651 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal);
652 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal);
653 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
654 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
655
Kit Barton915c5ec2016-02-26 21:59:44 +0000656 setOperationAction(ISD::FNEG, MVT::v4f32, Legal);
657 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
Kit Bartona1d6a6f2016-03-09 17:48:01 +0000658 setOperationAction(ISD::FABS, MVT::v4f32, Legal);
659 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
Kit Barton915c5ec2016-02-26 21:59:44 +0000660
Hal Finkela6c8b512014-03-26 16:12:58 +0000661 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000662 }
Bill Schmidtfe88b182015-02-03 21:58:23 +0000663
Kit Bartond4eb73c2015-05-05 16:10:44 +0000664 if (Subtarget.hasP8Altivec()) {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000665 addRegisterClass(MVT::v2i64, &PPC::VRRCRegClass);
Kit Bartond4eb73c2015-05-05 16:10:44 +0000666 addRegisterClass(MVT::v1i128, &PPC::VRRCRegClass);
667 }
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000668 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000669
Hal Finkelc93a9a22015-02-25 01:06:45 +0000670 if (Subtarget.hasQPX()) {
671 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
672 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
673 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
674 setOperationAction(ISD::FREM, MVT::v4f64, Expand);
675
676 setOperationAction(ISD::FCOPYSIGN, MVT::v4f64, Legal);
677 setOperationAction(ISD::FGETSIGN, MVT::v4f64, Expand);
678
679 setOperationAction(ISD::LOAD , MVT::v4f64, Custom);
680 setOperationAction(ISD::STORE , MVT::v4f64, Custom);
681
682 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Custom);
683 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Custom);
684
685 if (!Subtarget.useCRBits())
686 setOperationAction(ISD::SELECT, MVT::v4f64, Expand);
687 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
688
689 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f64, Legal);
690 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f64, Expand);
691 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f64, Expand);
692 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f64, Expand);
693 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f64, Custom);
694 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f64, Legal);
695 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
696
697 setOperationAction(ISD::FP_TO_SINT , MVT::v4f64, Legal);
698 setOperationAction(ISD::FP_TO_UINT , MVT::v4f64, Expand);
699
700 setOperationAction(ISD::FP_ROUND , MVT::v4f32, Legal);
701 setOperationAction(ISD::FP_ROUND_INREG , MVT::v4f32, Expand);
702 setOperationAction(ISD::FP_EXTEND, MVT::v4f64, Legal);
703
704 setOperationAction(ISD::FNEG , MVT::v4f64, Legal);
705 setOperationAction(ISD::FABS , MVT::v4f64, Legal);
706 setOperationAction(ISD::FSIN , MVT::v4f64, Expand);
707 setOperationAction(ISD::FCOS , MVT::v4f64, Expand);
708 setOperationAction(ISD::FPOWI , MVT::v4f64, Expand);
709 setOperationAction(ISD::FPOW , MVT::v4f64, Expand);
710 setOperationAction(ISD::FLOG , MVT::v4f64, Expand);
711 setOperationAction(ISD::FLOG2 , MVT::v4f64, Expand);
712 setOperationAction(ISD::FLOG10 , MVT::v4f64, Expand);
713 setOperationAction(ISD::FEXP , MVT::v4f64, Expand);
714 setOperationAction(ISD::FEXP2 , MVT::v4f64, Expand);
715
716 setOperationAction(ISD::FMINNUM, MVT::v4f64, Legal);
717 setOperationAction(ISD::FMAXNUM, MVT::v4f64, Legal);
718
719 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f64, Legal);
720 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f64, Legal);
721
722 addRegisterClass(MVT::v4f64, &PPC::QFRCRegClass);
723
724 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
725 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
726 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
727 setOperationAction(ISD::FREM, MVT::v4f32, Expand);
728
729 setOperationAction(ISD::FCOPYSIGN, MVT::v4f32, Legal);
730 setOperationAction(ISD::FGETSIGN, MVT::v4f32, Expand);
731
732 setOperationAction(ISD::LOAD , MVT::v4f32, Custom);
733 setOperationAction(ISD::STORE , MVT::v4f32, Custom);
734
735 if (!Subtarget.useCRBits())
736 setOperationAction(ISD::SELECT, MVT::v4f32, Expand);
737 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
738
739 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f32, Legal);
740 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f32, Expand);
741 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f32, Expand);
742 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f32, Expand);
743 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f32, Custom);
744 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
745 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
746
747 setOperationAction(ISD::FP_TO_SINT , MVT::v4f32, Legal);
748 setOperationAction(ISD::FP_TO_UINT , MVT::v4f32, Expand);
749
750 setOperationAction(ISD::FNEG , MVT::v4f32, Legal);
751 setOperationAction(ISD::FABS , MVT::v4f32, Legal);
752 setOperationAction(ISD::FSIN , MVT::v4f32, Expand);
753 setOperationAction(ISD::FCOS , MVT::v4f32, Expand);
754 setOperationAction(ISD::FPOWI , MVT::v4f32, Expand);
755 setOperationAction(ISD::FPOW , MVT::v4f32, Expand);
756 setOperationAction(ISD::FLOG , MVT::v4f32, Expand);
757 setOperationAction(ISD::FLOG2 , MVT::v4f32, Expand);
758 setOperationAction(ISD::FLOG10 , MVT::v4f32, Expand);
759 setOperationAction(ISD::FEXP , MVT::v4f32, Expand);
760 setOperationAction(ISD::FEXP2 , MVT::v4f32, Expand);
761
762 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
763 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
764
765 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f32, Legal);
766 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f32, Legal);
767
768 addRegisterClass(MVT::v4f32, &PPC::QSRCRegClass);
769
770 setOperationAction(ISD::AND , MVT::v4i1, Legal);
771 setOperationAction(ISD::OR , MVT::v4i1, Legal);
772 setOperationAction(ISD::XOR , MVT::v4i1, Legal);
773
774 if (!Subtarget.useCRBits())
775 setOperationAction(ISD::SELECT, MVT::v4i1, Expand);
776 setOperationAction(ISD::VSELECT, MVT::v4i1, Legal);
777
778 setOperationAction(ISD::LOAD , MVT::v4i1, Custom);
779 setOperationAction(ISD::STORE , MVT::v4i1, Custom);
780
781 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4i1, Custom);
782 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4i1, Expand);
783 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4i1, Expand);
784 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4i1, Expand);
785 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4i1, Custom);
786 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i1, Expand);
787 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i1, Custom);
788
789 setOperationAction(ISD::SINT_TO_FP, MVT::v4i1, Custom);
790 setOperationAction(ISD::UINT_TO_FP, MVT::v4i1, Custom);
791
792 addRegisterClass(MVT::v4i1, &PPC::QBRCRegClass);
793
794 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
795 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
796 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
797 setOperationAction(ISD::FROUND, MVT::v4f64, Legal);
798
799 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
800 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
801 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
802 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
803
804 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Expand);
805 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
806
807 // These need to set FE_INEXACT, and so cannot be vectorized here.
808 setOperationAction(ISD::FRINT, MVT::v4f64, Expand);
809 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
810
811 if (TM.Options.UnsafeFPMath) {
812 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
813 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
814
815 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
816 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
817 } else {
818 setOperationAction(ISD::FDIV, MVT::v4f64, Expand);
819 setOperationAction(ISD::FSQRT, MVT::v4f64, Expand);
820
821 setOperationAction(ISD::FDIV, MVT::v4f32, Expand);
822 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
823 }
824 }
825
Hal Finkel01fa7702014-12-03 00:19:17 +0000826 if (Subtarget.has64BitSupport())
Hal Finkel322e41a2012-04-01 20:08:17 +0000827 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel01fa7702014-12-03 00:19:17 +0000828
829 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, isPPC64 ? Legal : Custom);
Hal Finkel322e41a2012-04-01 20:08:17 +0000830
Robin Morissete1ca44b2014-10-02 22:27:07 +0000831 if (!isPPC64) {
832 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
833 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
834 }
Eli Friedman7dfa7912011-08-29 18:23:02 +0000835
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000836 setBooleanContents(ZeroOrOneBooleanContent);
Hal Finkelc93a9a22015-02-25 01:06:45 +0000837
838 if (Subtarget.hasAltivec()) {
839 // Altivec instructions set fields to all zeros or all ones.
840 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
841 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000842
Joerg Sonnenbergerb5459e62014-07-24 22:20:10 +0000843 if (!isPPC64) {
844 // These libcalls are not available in 32-bit.
845 setLibcallName(RTLIB::SHL_I128, nullptr);
846 setLibcallName(RTLIB::SRL_I128, nullptr);
847 setLibcallName(RTLIB::SRA_I128, nullptr);
848 }
849
Joseph Tremouletf748c892015-11-07 01:11:31 +0000850 setStackPointerRegisterToSaveRestore(isPPC64 ? PPC::X1 : PPC::R1);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000851
Chris Lattnerf4184352006-03-01 04:57:39 +0000852 // We have target-specific dag combine patterns for the following nodes:
853 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkel5efb9182015-01-06 06:01:57 +0000854 if (Subtarget.hasFPCVT())
855 setTargetDAGCombine(ISD::UINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000856 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000857 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000858 setTargetDAGCombine(ISD::BR_CC);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000859 if (Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000860 setTargetDAGCombine(ISD::BRCOND);
Chris Lattnera7976d32006-07-10 20:56:58 +0000861 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000862 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Bill Schmidtfae5d712014-12-09 16:35:51 +0000863 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
864 setTargetDAGCombine(ISD::INTRINSIC_VOID);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000865
Hal Finkel46043ed2014-03-01 21:36:57 +0000866 setTargetDAGCombine(ISD::SIGN_EXTEND);
867 setTargetDAGCombine(ISD::ZERO_EXTEND);
868 setTargetDAGCombine(ISD::ANY_EXTEND);
869
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000870 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000871 setTargetDAGCombine(ISD::TRUNCATE);
872 setTargetDAGCombine(ISD::SETCC);
873 setTargetDAGCombine(ISD::SELECT_CC);
874 }
875
Hal Finkel2e103312013-04-03 04:01:11 +0000876 // Use reciprocal estimates.
877 if (TM.Options.UnsafeFPMath) {
878 setTargetDAGCombine(ISD::FDIV);
879 setTargetDAGCombine(ISD::FSQRT);
880 }
881
Dale Johannesen10432e52007-10-19 00:59:18 +0000882 // Darwin long double math library functions have $LDBL128 appended.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000883 if (Subtarget.isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000884 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000885 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
886 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000887 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
888 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000889 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
890 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
891 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
892 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
893 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000894 }
895
Hal Finkel940ab932014-02-28 00:27:01 +0000896 // With 32 condition bits, we don't need to sink (and duplicate) compares
897 // aggressively in CodeGenPrep.
Hal Finkel7a0516e2015-02-12 01:02:52 +0000898 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000899 setHasMultipleConditionRegisters();
Hal Finkel7a0516e2015-02-12 01:02:52 +0000900 setJumpIsExpensive();
901 }
Hal Finkel940ab932014-02-28 00:27:01 +0000902
Hal Finkel65298572011-10-17 18:53:03 +0000903 setMinFunctionAlignment(2);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000904 if (Subtarget.isDarwin())
Hal Finkel65298572011-10-17 18:53:03 +0000905 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000906
Hal Finkeld73bfba2015-01-03 14:58:25 +0000907 switch (Subtarget.getDarwinDirective()) {
908 default: break;
909 case PPC::DIR_970:
910 case PPC::DIR_A2:
911 case PPC::DIR_E500mc:
912 case PPC::DIR_E5500:
913 case PPC::DIR_PWR4:
914 case PPC::DIR_PWR5:
915 case PPC::DIR_PWR5X:
916 case PPC::DIR_PWR6:
917 case PPC::DIR_PWR6X:
918 case PPC::DIR_PWR7:
919 case PPC::DIR_PWR8:
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +0000920 case PPC::DIR_PWR9:
Hal Finkeld73bfba2015-01-03 14:58:25 +0000921 setPrefFunctionAlignment(4);
922 setPrefLoopAlignment(4);
923 break;
924 }
925
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000926 if (Subtarget.enableMachineScheduler())
Hal Finkel21442b22013-09-11 23:05:25 +0000927 setSchedulingPreference(Sched::Source);
928 else
929 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000930
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000931 computeRegisterProperties(STI.getRegisterInfo());
Hal Finkel742b5352012-08-28 16:12:39 +0000932
Hal Finkeld73bfba2015-01-03 14:58:25 +0000933 // The Freescale cores do better with aggressive inlining of memcpy and
934 // friends. GCC uses same threshold of 128 bytes (= 32 word stores).
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000935 if (Subtarget.getDarwinDirective() == PPC::DIR_E500mc ||
936 Subtarget.getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000937 MaxStoresPerMemset = 32;
938 MaxStoresPerMemsetOptSize = 16;
939 MaxStoresPerMemcpy = 32;
940 MaxStoresPerMemcpyOptSize = 8;
941 MaxStoresPerMemmove = 32;
942 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel5c3cacf2015-02-27 19:58:28 +0000943 } else if (Subtarget.getDarwinDirective() == PPC::DIR_A2) {
944 // The A2 also benefits from (very) aggressive inlining of memcpy and
945 // friends. The overhead of a the function call, even when warm, can be
946 // over one hundred cycles.
947 MaxStoresPerMemset = 128;
948 MaxStoresPerMemcpy = 128;
949 MaxStoresPerMemmove = 128;
Hal Finkel742b5352012-08-28 16:12:39 +0000950 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000951}
952
Hal Finkel262a2242013-09-12 23:20:06 +0000953/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
954/// the desired ByVal argument alignment.
Pete Cooper2e201472015-07-27 17:15:24 +0000955static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
Hal Finkel262a2242013-09-12 23:20:06 +0000956 unsigned MaxMaxAlign) {
957 if (MaxAlign == MaxMaxAlign)
958 return;
Pete Cooper2e201472015-07-27 17:15:24 +0000959 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Hal Finkel262a2242013-09-12 23:20:06 +0000960 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
961 MaxAlign = 32;
962 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
963 MaxAlign = 16;
Pete Cooper2e201472015-07-27 17:15:24 +0000964 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Hal Finkel262a2242013-09-12 23:20:06 +0000965 unsigned EltAlign = 0;
966 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
967 if (EltAlign > MaxAlign)
968 MaxAlign = EltAlign;
Pete Cooper2e201472015-07-27 17:15:24 +0000969 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
970 for (auto *EltTy : STy->elements()) {
Hal Finkel262a2242013-09-12 23:20:06 +0000971 unsigned EltAlign = 0;
Pete Cooper0debbdc2015-07-24 18:55:49 +0000972 getMaxByValAlign(EltTy, EltAlign, MaxMaxAlign);
Hal Finkel262a2242013-09-12 23:20:06 +0000973 if (EltAlign > MaxAlign)
974 MaxAlign = EltAlign;
975 if (MaxAlign == MaxMaxAlign)
976 break;
977 }
978 }
979}
980
Dale Johannesencbde4c22008-02-28 22:31:51 +0000981/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
982/// function arguments in the caller parameter area.
Mehdi Amini5c183d52015-07-09 02:09:28 +0000983unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty,
984 const DataLayout &DL) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000985 // Darwin passes everything on 4 byte boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000986 if (Subtarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000987 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000988
989 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000990 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000991 unsigned Align = Subtarget.isPPC64() ? 8 : 4;
992 if (Subtarget.hasAltivec() || Subtarget.hasQPX())
993 getMaxByValAlign(Ty, Align, Subtarget.hasQPX() ? 32 : 16);
Hal Finkel262a2242013-09-12 23:20:06 +0000994 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000995}
996
Petar Jovanovic280f7102015-12-14 17:57:33 +0000997bool PPCTargetLowering::useSoftFloat() const {
998 return Subtarget.useSoftFloat();
999}
1000
Chris Lattner347ed8a2006-01-09 23:52:17 +00001001const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001002 switch ((PPCISD::NodeType)Opcode) {
1003 case PPCISD::FIRST_NUMBER: break;
Evan Cheng32e376f2008-07-12 02:23:19 +00001004 case PPCISD::FSEL: return "PPCISD::FSEL";
1005 case PPCISD::FCFID: return "PPCISD::FCFID";
Hal Finkel3fe09ea2015-01-06 07:02:15 +00001006 case PPCISD::FCFIDU: return "PPCISD::FCFIDU";
1007 case PPCISD::FCFIDS: return "PPCISD::FCFIDS";
1008 case PPCISD::FCFIDUS: return "PPCISD::FCFIDUS";
Evan Cheng32e376f2008-07-12 02:23:19 +00001009 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
1010 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel3fe09ea2015-01-06 07:02:15 +00001011 case PPCISD::FCTIDUZ: return "PPCISD::FCTIDUZ";
1012 case PPCISD::FCTIWUZ: return "PPCISD::FCTIWUZ";
Hal Finkel2e103312013-04-03 04:01:11 +00001013 case PPCISD::FRE: return "PPCISD::FRE";
1014 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +00001015 case PPCISD::STFIWX: return "PPCISD::STFIWX";
1016 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
1017 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
1018 case PPCISD::VPERM: return "PPCISD::VPERM";
Nemanja Ivanovic1a2b2f02016-05-04 16:04:02 +00001019 case PPCISD::XXSPLT: return "PPCISD::XXSPLT";
Hal Finkel4edc66b2015-01-03 01:16:37 +00001020 case PPCISD::CMPB: return "PPCISD::CMPB";
Evan Cheng32e376f2008-07-12 02:23:19 +00001021 case PPCISD::Hi: return "PPCISD::Hi";
1022 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001023 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Evan Cheng32e376f2008-07-12 02:23:19 +00001024 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
Yury Gribovd7dbb662015-12-01 11:40:55 +00001025 case PPCISD::DYNAREAOFFSET: return "PPCISD::DYNAREAOFFSET";
Evan Cheng32e376f2008-07-12 02:23:19 +00001026 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
1027 case PPCISD::SRL: return "PPCISD::SRL";
1028 case PPCISD::SRA: return "PPCISD::SRA";
1029 case PPCISD::SHL: return "PPCISD::SHL";
Matthias Braund04893f2015-05-07 21:33:59 +00001030 case PPCISD::SRA_ADDZE: return "PPCISD::SRA_ADDZE";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00001031 case PPCISD::CALL: return "PPCISD::CALL";
1032 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +00001033 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00001034 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Hal Finkelfc096c92014-12-23 22:29:40 +00001035 case PPCISD::BCTRL_LOAD_TOC: return "PPCISD::BCTRL_LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +00001036 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkelbbdee932014-12-02 22:01:00 +00001037 case PPCISD::READ_TIME_BASE: return "PPCISD::READ_TIME_BASE";
Hal Finkel756810f2013-03-21 21:37:52 +00001038 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
1039 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001040 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001041 case PPCISD::MFVSR: return "PPCISD::MFVSR";
1042 case PPCISD::MTVSRA: return "PPCISD::MTVSRA";
1043 case PPCISD::MTVSRZ: return "PPCISD::MTVSRZ";
Matthias Braund04893f2015-05-07 21:33:59 +00001044 case PPCISD::ANDIo_1_EQ_BIT: return "PPCISD::ANDIo_1_EQ_BIT";
1045 case PPCISD::ANDIo_1_GT_BIT: return "PPCISD::ANDIo_1_GT_BIT";
Evan Cheng32e376f2008-07-12 02:23:19 +00001046 case PPCISD::VCMP: return "PPCISD::VCMP";
1047 case PPCISD::VCMPo: return "PPCISD::VCMPo";
1048 case PPCISD::LBRX: return "PPCISD::LBRX";
1049 case PPCISD::STBRX: return "PPCISD::STBRX";
Hal Finkel3fe09ea2015-01-06 07:02:15 +00001050 case PPCISD::LFIWAX: return "PPCISD::LFIWAX";
1051 case PPCISD::LFIWZX: return "PPCISD::LFIWZX";
Matthias Braund04893f2015-05-07 21:33:59 +00001052 case PPCISD::LXVD2X: return "PPCISD::LXVD2X";
1053 case PPCISD::STXVD2X: return "PPCISD::STXVD2X";
Evan Cheng32e376f2008-07-12 02:23:19 +00001054 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +00001055 case PPCISD::BDNZ: return "PPCISD::BDNZ";
1056 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +00001057 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +00001058 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +00001059 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +00001060 case PPCISD::CR6SET: return "PPCISD::CR6SET";
1061 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Roman Divacky32143e22013-12-20 18:08:54 +00001062 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Matthias Braund04893f2015-05-07 21:33:59 +00001063 case PPCISD::PPC32_PICGOT: return "PPCISD::PPC32_PICGOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001064 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
1065 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001066 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001067 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
1068 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
Bill Schmidt82f1c772015-02-10 19:09:05 +00001069 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
1070 case PPCISD::ADDI_TLSGD_L_ADDR: return "PPCISD::ADDI_TLSGD_L_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001071 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
1072 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
Bill Schmidt82f1c772015-02-10 19:09:05 +00001073 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
1074 case PPCISD::ADDI_TLSLD_L_ADDR: return "PPCISD::ADDI_TLSLD_L_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001075 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
1076 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +00001077 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +00001078 case PPCISD::SC: return "PPCISD::SC";
Bill Schmidte26236e2015-05-22 16:44:10 +00001079 case PPCISD::CLRBHRB: return "PPCISD::CLRBHRB";
1080 case PPCISD::MFBHRBE: return "PPCISD::MFBHRBE";
1081 case PPCISD::RFEBB: return "PPCISD::RFEBB";
Matthias Braund04893f2015-05-07 21:33:59 +00001082 case PPCISD::XXSWAPD: return "PPCISD::XXSWAPD";
Hal Finkelc93a9a22015-02-25 01:06:45 +00001083 case PPCISD::QVFPERM: return "PPCISD::QVFPERM";
1084 case PPCISD::QVGPCI: return "PPCISD::QVGPCI";
1085 case PPCISD::QVALIGNI: return "PPCISD::QVALIGNI";
1086 case PPCISD::QVESPLATI: return "PPCISD::QVESPLATI";
1087 case PPCISD::QBFLT: return "PPCISD::QBFLT";
1088 case PPCISD::QVLFSb: return "PPCISD::QVLFSb";
Chris Lattner347ed8a2006-01-09 23:52:17 +00001089 }
Matthias Braund04893f2015-05-07 21:33:59 +00001090 return nullptr;
Chris Lattner347ed8a2006-01-09 23:52:17 +00001091}
1092
Mehdi Amini44ede332015-07-09 02:09:04 +00001093EVT PPCTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &C,
1094 EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00001095 if (!VT.isVector())
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001096 return Subtarget.useCRBits() ? MVT::i1 : MVT::i32;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001097
1098 if (Subtarget.hasQPX())
1099 return EVT::getVectorVT(C, MVT::i1, VT.getVectorNumElements());
1100
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00001101 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +00001102}
1103
Hal Finkel62ac7362014-09-19 11:42:56 +00001104bool PPCTargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1105 assert(VT.isFloatingPoint() && "Non-floating-point FMA?");
1106 return true;
1107}
1108
Chris Lattner4211ca92006-04-14 06:01:58 +00001109//===----------------------------------------------------------------------===//
1110// Node matching predicates, for use by the tblgen matching code.
1111//===----------------------------------------------------------------------===//
1112
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001113/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001114static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001115 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001116 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001117 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001118 // Maybe this has already been legalized into the constant pool?
1119 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001120 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001121 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001122 }
1123 return false;
1124}
1125
Chris Lattnere8b83b42006-04-06 17:23:16 +00001126/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
1127/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001128static bool isConstantOrUndef(int Op, int Val) {
1129 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001130}
1131
1132/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
1133/// VPKUHUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001134/// The ShuffleKind distinguishes between big-endian operations with
1135/// two different inputs (0), either-endian operations with two identical
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001136/// inputs (1), and little-endian operations with two different inputs (2).
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001137/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1138bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +00001139 SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001140 bool IsLE = DAG.getDataLayout().isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001141 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +00001142 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001143 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001144 for (unsigned i = 0; i != 16; ++i)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001145 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001146 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001147 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +00001148 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001149 return false;
1150 for (unsigned i = 0; i != 16; ++i)
1151 if (!isConstantOrUndef(N->getMaskElt(i), i*2))
1152 return false;
1153 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +00001154 unsigned j = IsLE ? 0 : 1;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001155 for (unsigned i = 0; i != 8; ++i)
Bill Schmidtf910a062014-06-10 14:35:01 +00001156 if (!isConstantOrUndef(N->getMaskElt(i), i*2+j) ||
1157 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001158 return false;
1159 }
Chris Lattner1d338192006-04-06 18:26:28 +00001160 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001161}
1162
1163/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
1164/// VPKUWUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001165/// The ShuffleKind distinguishes between big-endian operations with
1166/// two different inputs (0), either-endian operations with two identical
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001167/// inputs (1), and little-endian operations with two different inputs (2).
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001168/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1169bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +00001170 SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001171 bool IsLE = DAG.getDataLayout().isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001172 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +00001173 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001174 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001175 for (unsigned i = 0; i != 16; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001176 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
1177 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001178 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001179 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +00001180 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001181 return false;
1182 for (unsigned i = 0; i != 16; i += 2)
1183 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) ||
1184 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1))
1185 return false;
1186 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +00001187 unsigned j = IsLE ? 0 : 2;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001188 for (unsigned i = 0; i != 8; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001189 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) ||
1190 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) ||
1191 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) ||
1192 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001193 return false;
1194 }
Chris Lattner1d338192006-04-06 18:26:28 +00001195 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001196}
1197
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001198/// isVPKUDUMShuffleMask - Return true if this is the shuffle mask for a
Bill Schmidte13ac912015-05-21 20:48:49 +00001199/// VPKUDUM instruction, AND the VPKUDUM instruction exists for the
1200/// current subtarget.
1201///
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001202/// The ShuffleKind distinguishes between big-endian operations with
1203/// two different inputs (0), either-endian operations with two identical
1204/// inputs (1), and little-endian operations with two different inputs (2).
1205/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1206bool PPC::isVPKUDUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
1207 SelectionDAG &DAG) {
Bill Schmidte13ac912015-05-21 20:48:49 +00001208 const PPCSubtarget& Subtarget =
1209 static_cast<const PPCSubtarget&>(DAG.getSubtarget());
1210 if (!Subtarget.hasP8Vector())
1211 return false;
1212
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001213 bool IsLE = DAG.getDataLayout().isLittleEndian();
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001214 if (ShuffleKind == 0) {
1215 if (IsLE)
1216 return false;
1217 for (unsigned i = 0; i != 16; i += 4)
1218 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+4) ||
1219 !isConstantOrUndef(N->getMaskElt(i+1), i*2+5) ||
1220 !isConstantOrUndef(N->getMaskElt(i+2), i*2+6) ||
1221 !isConstantOrUndef(N->getMaskElt(i+3), i*2+7))
1222 return false;
1223 } else if (ShuffleKind == 2) {
1224 if (!IsLE)
1225 return false;
1226 for (unsigned i = 0; i != 16; i += 4)
1227 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) ||
1228 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1) ||
1229 !isConstantOrUndef(N->getMaskElt(i+2), i*2+2) ||
1230 !isConstantOrUndef(N->getMaskElt(i+3), i*2+3))
1231 return false;
1232 } else if (ShuffleKind == 1) {
1233 unsigned j = IsLE ? 0 : 4;
1234 for (unsigned i = 0; i != 8; i += 4)
1235 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) ||
1236 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) ||
1237 !isConstantOrUndef(N->getMaskElt(i+2), i*2+j+2) ||
1238 !isConstantOrUndef(N->getMaskElt(i+3), i*2+j+3) ||
1239 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) ||
1240 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1) ||
1241 !isConstantOrUndef(N->getMaskElt(i+10), i*2+j+2) ||
1242 !isConstantOrUndef(N->getMaskElt(i+11), i*2+j+3))
1243 return false;
1244 }
1245 return true;
1246}
1247
Chris Lattnerf38e0332006-04-06 22:02:42 +00001248/// isVMerge - Common function, used to match vmrg* shuffles.
1249///
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001250static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +00001251 unsigned LHSStart, unsigned RHSStart) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001252 if (N->getValueType(0) != MVT::v16i8)
1253 return false;
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001254 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
1255 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001256
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001257 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
1258 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001259 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +00001260 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001261 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +00001262 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001263 return false;
1264 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001265 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +00001266}
1267
1268/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +00001269/// a VMRGL* instruction with the specified unit size (1,2 or 4 bytes).
NAKAMURA Takumi84965032015-09-22 11:14:12 +00001270/// The ShuffleKind distinguishes between big-endian merges with two
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001271/// different inputs (0), either-endian merges with two identical inputs (1),
1272/// and little-endian merges with two different inputs (2). For the latter,
1273/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +00001274bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001275 unsigned ShuffleKind, SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001276 if (DAG.getDataLayout().isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001277 if (ShuffleKind == 1) // unary
1278 return isVMerge(N, UnitSize, 0, 0);
1279 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +00001280 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001281 else
1282 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001283 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001284 if (ShuffleKind == 1) // unary
1285 return isVMerge(N, UnitSize, 8, 8);
1286 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +00001287 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001288 else
1289 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001290 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001291}
1292
1293/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +00001294/// a VMRGH* instruction with the specified unit size (1,2 or 4 bytes).
NAKAMURA Takumi84965032015-09-22 11:14:12 +00001295/// The ShuffleKind distinguishes between big-endian merges with two
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001296/// different inputs (0), either-endian merges with two identical inputs (1),
1297/// and little-endian merges with two different inputs (2). For the latter,
1298/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +00001299bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001300 unsigned ShuffleKind, SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001301 if (DAG.getDataLayout().isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001302 if (ShuffleKind == 1) // unary
1303 return isVMerge(N, UnitSize, 8, 8);
1304 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +00001305 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001306 else
1307 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001308 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001309 if (ShuffleKind == 1) // unary
1310 return isVMerge(N, UnitSize, 0, 0);
1311 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +00001312 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001313 else
1314 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001315 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001316}
1317
Kit Barton13894c72015-06-25 15:17:40 +00001318/**
1319 * \brief Common function used to match vmrgew and vmrgow shuffles
1320 *
1321 * The indexOffset determines whether to look for even or odd words in
1322 * the shuffle mask. This is based on the of the endianness of the target
1323 * machine.
1324 * - Little Endian:
1325 * - Use offset of 0 to check for odd elements
1326 * - Use offset of 4 to check for even elements
1327 * - Big Endian:
1328 * - Use offset of 0 to check for even elements
1329 * - Use offset of 4 to check for odd elements
1330 * A detailed description of the vector element ordering for little endian and
NAKAMURA Takumi520b45d2015-06-25 23:38:44 +00001331 * big endian can be found at
1332 * http://www.ibm.com/developerworks/library/l-ibm-xl-c-cpp-compiler/index.html
Kit Barton13894c72015-06-25 15:17:40 +00001333 * Targeting your applications - what little endian and big endian IBM XL C/C++
NAKAMURA Takumi520b45d2015-06-25 23:38:44 +00001334 * compiler differences mean to you
Kit Barton13894c72015-06-25 15:17:40 +00001335 *
1336 * The mask to the shuffle vector instruction specifies the indices of the
1337 * elements from the two input vectors to place in the result. The elements are
1338 * numbered in array-access order, starting with the first vector. These vectors
1339 * are always of type v16i8, thus each vector will contain 16 elements of size
NAKAMURA Takumi520b45d2015-06-25 23:38:44 +00001340 * 8. More info on the shuffle vector can be found in the
1341 * http://llvm.org/docs/LangRef.html#shufflevector-instruction
1342 * Language Reference.
Kit Barton13894c72015-06-25 15:17:40 +00001343 *
1344 * The RHSStartValue indicates whether the same input vectors are used (unary)
1345 * or two different input vectors are used, based on the following:
1346 * - If the instruction uses the same vector for both inputs, the range of the
1347 * indices will be 0 to 15. In this case, the RHSStart value passed should
1348 * be 0.
1349 * - If the instruction has two different vectors then the range of the
1350 * indices will be 0 to 31. In this case, the RHSStart value passed should
1351 * be 16 (indices 0-15 specify elements in the first vector while indices 16
1352 * to 31 specify elements in the second vector).
1353 *
1354 * \param[in] N The shuffle vector SD Node to analyze
1355 * \param[in] IndexOffset Specifies whether to look for even or odd elements
1356 * \param[in] RHSStartValue Specifies the starting index for the righthand input
1357 * vector to the shuffle_vector instruction
1358 * \return true iff this shuffle vector represents an even or odd word merge
1359 */
1360static bool isVMerge(ShuffleVectorSDNode *N, unsigned IndexOffset,
1361 unsigned RHSStartValue) {
1362 if (N->getValueType(0) != MVT::v16i8)
1363 return false;
1364
1365 for (unsigned i = 0; i < 2; ++i)
1366 for (unsigned j = 0; j < 4; ++j)
1367 if (!isConstantOrUndef(N->getMaskElt(i*4+j),
1368 i*RHSStartValue+j+IndexOffset) ||
1369 !isConstantOrUndef(N->getMaskElt(i*4+j+8),
1370 i*RHSStartValue+j+IndexOffset+8))
1371 return false;
1372 return true;
1373}
1374
1375/**
1376 * \brief Determine if the specified shuffle mask is suitable for the vmrgew or
1377 * vmrgow instructions.
1378 *
1379 * \param[in] N The shuffle vector SD Node to analyze
1380 * \param[in] CheckEven Check for an even merge (true) or an odd merge (false)
1381 * \param[in] ShuffleKind Identify the type of merge:
1382 * - 0 = big-endian merge with two different inputs;
1383 * - 1 = either-endian merge with two identical inputs;
1384 * - 2 = little-endian merge with two different inputs (inputs are swapped for
1385 * little-endian merges).
1386 * \param[in] DAG The current SelectionDAG
NAKAMURA Takumi84965032015-09-22 11:14:12 +00001387 * \return true iff this shuffle mask
Kit Barton13894c72015-06-25 15:17:40 +00001388 */
1389bool PPC::isVMRGEOShuffleMask(ShuffleVectorSDNode *N, bool CheckEven,
1390 unsigned ShuffleKind, SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001391 if (DAG.getDataLayout().isLittleEndian()) {
Kit Barton13894c72015-06-25 15:17:40 +00001392 unsigned indexOffset = CheckEven ? 4 : 0;
1393 if (ShuffleKind == 1) // Unary
1394 return isVMerge(N, indexOffset, 0);
1395 else if (ShuffleKind == 2) // swapped
1396 return isVMerge(N, indexOffset, 16);
1397 else
1398 return false;
1399 }
1400 else {
1401 unsigned indexOffset = CheckEven ? 0 : 4;
1402 if (ShuffleKind == 1) // Unary
1403 return isVMerge(N, indexOffset, 0);
1404 else if (ShuffleKind == 0) // Normal
1405 return isVMerge(N, indexOffset, 16);
1406 else
1407 return false;
1408 }
1409 return false;
1410}
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001411
Chris Lattner1d338192006-04-06 18:26:28 +00001412/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
1413/// amount, otherwise return -1.
NAKAMURA Takumi84965032015-09-22 11:14:12 +00001414/// The ShuffleKind distinguishes between big-endian operations with two
Bill Schmidt42a69362014-08-05 20:47:25 +00001415/// different inputs (0), either-endian operations with two identical inputs
1416/// (1), and little-endian operations with two different inputs (2). For the
1417/// latter, the input operands are swapped (see PPCInstrAltivec.td).
1418int PPC::isVSLDOIShuffleMask(SDNode *N, unsigned ShuffleKind,
1419 SelectionDAG &DAG) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001420 if (N->getValueType(0) != MVT::v16i8)
Hal Finkela775e512014-04-08 19:00:27 +00001421 return -1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001422
1423 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +00001424
Chris Lattner1d338192006-04-06 18:26:28 +00001425 // Find the first non-undef value in the shuffle mask.
1426 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001427 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +00001428 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001429
Chris Lattner1d338192006-04-06 18:26:28 +00001430 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001431
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001432 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +00001433 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001434 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +00001435 if (ShiftAmt < i) return -1;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001436
Bill Schmidtf04e9982014-08-04 23:21:01 +00001437 ShiftAmt -= i;
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001438 bool isLE = DAG.getDataLayout().isLittleEndian();
Bill Schmidtf910a062014-06-10 14:35:01 +00001439
Bill Schmidt42a69362014-08-05 20:47:25 +00001440 if ((ShuffleKind == 0 && !isLE) || (ShuffleKind == 2 && isLE)) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001441 // Check the rest of the elements to see if they are consecutive.
1442 for (++i; i != 16; ++i)
1443 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
1444 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001445 } else if (ShuffleKind == 1) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001446 // Check the rest of the elements to see if they are consecutive.
1447 for (++i; i != 16; ++i)
1448 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
1449 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001450 } else
1451 return -1;
1452
Bill Schmidt1e77bb12015-07-15 15:45:30 +00001453 if (isLE)
Bill Schmidt42a69362014-08-05 20:47:25 +00001454 ShiftAmt = 16 - ShiftAmt;
Bill Schmidtf04e9982014-08-04 23:21:01 +00001455
Chris Lattner1d338192006-04-06 18:26:28 +00001456 return ShiftAmt;
1457}
Chris Lattnerffc47562006-03-20 06:33:01 +00001458
1459/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
1460/// specifies a splat of a single element that is suitable for input to
1461/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001462bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +00001463 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +00001464 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001465
Bill Schmidt42ddd712015-07-29 14:31:57 +00001466 // The consecutive indices need to specify an element, not part of two
1467 // different elements. So abandon ship early if this isn't the case.
1468 if (N->getMaskElt(0) % EltSize != 0)
1469 return false;
1470
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001471 // This is a splat operation if each element of the permute is the same, and
1472 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001473 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001474
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001475 // FIXME: Handle UNDEF elements too!
1476 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +00001477 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001478
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001479 // Check that the indices are consecutive, in the case of a multi-byte element
1480 // splatted with a v16i8 mask.
1481 for (unsigned i = 1; i != EltSize; ++i)
1482 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001483 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001484
Chris Lattner95c7adc2006-04-04 17:25:31 +00001485 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001486 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +00001487 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001488 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001489 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001490 }
Chris Lattner95c7adc2006-04-04 17:25:31 +00001491 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +00001492}
1493
1494/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
1495/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Bill Schmidtf910a062014-06-10 14:35:01 +00001496unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize,
1497 SelectionDAG &DAG) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001498 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1499 assert(isSplatShuffleMask(SVOp, EltSize));
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001500 if (DAG.getDataLayout().isLittleEndian())
Bill Schmidtf910a062014-06-10 14:35:01 +00001501 return (16 / EltSize) - 1 - (SVOp->getMaskElt(0) / EltSize);
1502 else
1503 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +00001504}
1505
Chris Lattner74cf9ff2006-04-12 17:37:20 +00001506/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001507/// by using a vspltis[bhw] instruction of the specified element size, return
1508/// the constant being splatted. The ByteSize field indicates the number of
1509/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001510SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001511 SDValue OpVal(nullptr, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001512
1513 // If ByteSize of the splat is bigger than the element size of the
1514 // build_vector, then we have a case where we are checking for a splat where
1515 // multiple elements of the buildvector are folded together into a single
1516 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
1517 unsigned EltSize = 16/N->getNumOperands();
1518 if (EltSize < ByteSize) {
1519 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001520 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001521 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001522
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001523 // See if all of the elements in the buildvector agree across.
1524 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00001525 if (N->getOperand(i).isUndef()) continue;
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001526 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001527 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001528
Scott Michelcf0da6c2009-02-17 22:15:04 +00001529
Craig Topper062a2ba2014-04-25 05:30:21 +00001530 if (!UniquedVals[i&(Multiple-1)].getNode())
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001531 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
1532 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001533 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001534 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001535
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001536 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
1537 // either constant or undef values that are identical for each chunk. See
1538 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001539
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001540 // Check to see if all of the leading entries are either 0 or -1. If
1541 // neither, then this won't fit into the immediate field.
1542 bool LeadingZero = true;
1543 bool LeadingOnes = true;
1544 for (unsigned i = 0; i != Multiple-1; ++i) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001545 if (!UniquedVals[i].getNode()) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001546
Artyom Skrobov314ee042015-11-25 19:41:11 +00001547 LeadingZero &= isNullConstant(UniquedVals[i]);
1548 LeadingOnes &= isAllOnesConstant(UniquedVals[i]);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001549 }
1550 // Finally, check the least significant entry.
1551 if (LeadingZero) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001552 if (!UniquedVals[Multiple-1].getNode())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001553 return DAG.getTargetConstant(0, SDLoc(N), MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00001554 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001555 if (Val < 16) // 0,0,0,4 -> vspltisw(4)
1556 return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001557 }
1558 if (LeadingOnes) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001559 if (!UniquedVals[Multiple-1].getNode())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001560 return DAG.getTargetConstant(~0U, SDLoc(N), MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +00001561 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001562 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001563 return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001564 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001565
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001566 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001567 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001568
Chris Lattner2771e2c2006-03-25 06:12:06 +00001569 // Check to see if this buildvec has a single non-undef value in its elements.
1570 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00001571 if (N->getOperand(i).isUndef()) continue;
Craig Topper062a2ba2014-04-25 05:30:21 +00001572 if (!OpVal.getNode())
Chris Lattner2771e2c2006-03-25 06:12:06 +00001573 OpVal = N->getOperand(i);
1574 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001575 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001576 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001577
Craig Topper062a2ba2014-04-25 05:30:21 +00001578 if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001579
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001580 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001581 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001582 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001583 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001584 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001585 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001586 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001587 }
1588
1589 // If the splat value is larger than the element value, then we can never do
1590 // this splat. The only case that we could fit the replicated bits into our
1591 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001592 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001593
Benjamin Kramerb4b51502015-03-25 16:49:59 +00001594 // If the element value is larger than the splat value, check if it consists
1595 // of a repeated bit pattern of size ByteSize.
1596 if (!APInt(ValSizeInBytes * 8, Value).isSplat(ByteSize * 8))
1597 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001598
1599 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001600 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001601
Evan Chengb1ddc982006-03-26 09:52:32 +00001602 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001603 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001604
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001605 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001606 if (SignExtend32<5>(MaskVal) == MaskVal)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001607 return DAG.getTargetConstant(MaskVal, SDLoc(N), MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001608 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001609}
1610
Hal Finkelc93a9a22015-02-25 01:06:45 +00001611/// isQVALIGNIShuffleMask - If this is a qvaligni shuffle mask, return the shift
1612/// amount, otherwise return -1.
1613int PPC::isQVALIGNIShuffleMask(SDNode *N) {
1614 EVT VT = N->getValueType(0);
1615 if (VT != MVT::v4f64 && VT != MVT::v4f32 && VT != MVT::v4i1)
1616 return -1;
1617
1618 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1619
1620 // Find the first non-undef value in the shuffle mask.
1621 unsigned i;
1622 for (i = 0; i != 4 && SVOp->getMaskElt(i) < 0; ++i)
1623 /*search*/;
1624
1625 if (i == 4) return -1; // all undef.
1626
1627 // Otherwise, check to see if the rest of the elements are consecutively
1628 // numbered from this value.
1629 unsigned ShiftAmt = SVOp->getMaskElt(i);
1630 if (ShiftAmt < i) return -1;
1631 ShiftAmt -= i;
1632
1633 // Check the rest of the elements to see if they are consecutive.
1634 for (++i; i != 4; ++i)
1635 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
1636 return -1;
1637
1638 return ShiftAmt;
1639}
1640
Chris Lattner4211ca92006-04-14 06:01:58 +00001641//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001642// Addressing Mode Selection
1643//===----------------------------------------------------------------------===//
1644
1645/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1646/// or 64-bit immediate, and if the value can be accurately represented as a
1647/// sign extension from a 16-bit value. If so, this returns true and the
1648/// immediate.
1649static bool isIntS16Immediate(SDNode *N, short &Imm) {
Adam Nemet571eb5f2014-05-20 17:20:34 +00001650 if (!isa<ConstantSDNode>(N))
Chris Lattnera801fced2006-11-08 02:15:41 +00001651 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001652
Dan Gohmaneffb8942008-09-12 16:56:44 +00001653 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001654 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001655 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001656 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001657 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001658}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001659static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001660 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001661}
1662
Chris Lattnera801fced2006-11-08 02:15:41 +00001663/// SelectAddressRegReg - Given the specified addressed, check to see if it
1664/// can be represented as an indexed [r+r] operation. Returns false if it
1665/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001666bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1667 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001668 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001669 short imm = 0;
1670 if (N.getOpcode() == ISD::ADD) {
1671 if (isIntS16Immediate(N.getOperand(1), imm))
1672 return false; // r+i
1673 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1674 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001675
Chris Lattnera801fced2006-11-08 02:15:41 +00001676 Base = N.getOperand(0);
1677 Index = N.getOperand(1);
1678 return true;
1679 } else if (N.getOpcode() == ISD::OR) {
1680 if (isIntS16Immediate(N.getOperand(1), imm))
1681 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001682
Chris Lattnera801fced2006-11-08 02:15:41 +00001683 // If this is an or of disjoint bitfields, we can codegen this as an add
1684 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1685 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001686 APInt LHSKnownZero, LHSKnownOne;
1687 APInt RHSKnownZero, RHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001688 DAG.computeKnownBits(N.getOperand(0),
1689 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001690
Dan Gohmanf19609a2008-02-27 01:23:58 +00001691 if (LHSKnownZero.getBoolValue()) {
Jay Foada0653a32014-05-14 21:14:37 +00001692 DAG.computeKnownBits(N.getOperand(1),
1693 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001694 // If all of the bits are known zero on the LHS or RHS, the add won't
1695 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001696 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001697 Base = N.getOperand(0);
1698 Index = N.getOperand(1);
1699 return true;
1700 }
1701 }
1702 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001703
Chris Lattnera801fced2006-11-08 02:15:41 +00001704 return false;
1705}
1706
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001707// If we happen to be doing an i64 load or store into a stack slot that has
1708// less than a 4-byte alignment, then the frame-index elimination may need to
1709// use an indexed load or store instruction (because the offset may not be a
1710// multiple of 4). The extra register needed to hold the offset comes from the
1711// register scavenger, and it is possible that the scavenger will need to use
1712// an emergency spill slot. As a result, we need to make sure that a spill slot
1713// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1714// stack slot.
1715static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1716 // FIXME: This does not handle the LWA case.
1717 if (VT != MVT::i64)
1718 return;
1719
Hal Finkel7ab3db52013-07-10 15:29:01 +00001720 // NOTE: We'll exclude negative FIs here, which come from argument
1721 // lowering, because there are no known test cases triggering this problem
1722 // using packed structures (or similar). We can remove this exclusion if
1723 // we find such a test case. The reason why this is so test-case driven is
1724 // because this entire 'fixup' is only to prevent crashes (from the
1725 // register scavenger) on not-really-valid inputs. For example, if we have:
1726 // %a = alloca i1
1727 // %b = bitcast i1* %a to i64*
1728 // store i64* a, i64 b
1729 // then the store should really be marked as 'align 1', but is not. If it
1730 // were marked as 'align 1' then the indexed form would have been
1731 // instruction-selected initially, and the problem this 'fixup' is preventing
1732 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001733 if (FrameIdx < 0)
1734 return;
1735
1736 MachineFunction &MF = DAG.getMachineFunction();
1737 MachineFrameInfo *MFI = MF.getFrameInfo();
1738
1739 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1740 if (Align >= 4)
1741 return;
1742
1743 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1744 FuncInfo->setHasNonRISpills();
1745}
1746
Chris Lattnera801fced2006-11-08 02:15:41 +00001747/// Returns true if the address N can be represented by a base register plus
1748/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001749/// represented as reg+reg. If Aligned is true, only accept displacements
1750/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001751bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001752 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001753 SelectionDAG &DAG,
1754 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001755 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001756 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001757 // If this can be more profitably realized as r+r, fail.
1758 if (SelectAddressRegReg(N, Disp, Base, DAG))
1759 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001760
Chris Lattnera801fced2006-11-08 02:15:41 +00001761 if (N.getOpcode() == ISD::ADD) {
1762 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001763 if (isIntS16Immediate(N.getOperand(1), imm) &&
1764 (!Aligned || (imm & 3) == 0)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001765 Disp = DAG.getTargetConstant(imm, dl, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001766 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1767 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001768 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001769 } else {
1770 Base = N.getOperand(0);
1771 }
1772 return true; // [r+i]
1773 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1774 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001775 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001776 && "Cannot handle constant offsets yet!");
1777 Disp = N.getOperand(1).getOperand(0); // The global address.
1778 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001779 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001780 Disp.getOpcode() == ISD::TargetConstantPool ||
1781 Disp.getOpcode() == ISD::TargetJumpTable);
1782 Base = N.getOperand(0);
1783 return true; // [&g+r]
1784 }
1785 } else if (N.getOpcode() == ISD::OR) {
1786 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001787 if (isIntS16Immediate(N.getOperand(1), imm) &&
1788 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001789 // If this is an or of disjoint bitfields, we can codegen this as an add
1790 // (for better address arithmetic) if the LHS and RHS of the OR are
1791 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001792 APInt LHSKnownZero, LHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001793 DAG.computeKnownBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001794
Dan Gohmanf19609a2008-02-27 01:23:58 +00001795 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001796 // If all of the bits are known zero on the LHS or RHS, the add won't
1797 // carry.
Ulrich Weigand55a96652014-07-20 22:26:40 +00001798 if (FrameIndexSDNode *FI =
1799 dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1800 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1801 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1802 } else {
1803 Base = N.getOperand(0);
1804 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001805 Disp = DAG.getTargetConstant(imm, dl, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001806 return true;
1807 }
1808 }
1809 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1810 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001811
Chris Lattnera801fced2006-11-08 02:15:41 +00001812 // If this address fits entirely in a 16-bit sext immediate field, codegen
1813 // this as "d, 0"
1814 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001815 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001816 Disp = DAG.getTargetConstant(Imm, dl, CN->getValueType(0));
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001817 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001818 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001819 return true;
1820 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001821
1822 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001823 if ((CN->getValueType(0) == MVT::i32 ||
1824 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1825 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001826 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001827
Chris Lattnera801fced2006-11-08 02:15:41 +00001828 // Otherwise, break this down into an LIS + disp.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001829 Disp = DAG.getTargetConstant((short)Addr, dl, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001830
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001831 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, dl,
1832 MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00001833 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001834 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001835 return true;
1836 }
1837 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001838
Mehdi Amini44ede332015-07-09 02:09:04 +00001839 Disp = DAG.getTargetConstant(0, dl, getPointerTy(DAG.getDataLayout()));
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001840 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001841 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001842 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1843 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001844 Base = N;
1845 return true; // [r+0]
1846}
1847
1848/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1849/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001850bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1851 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001852 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001853 // Check to see if we can easily represent this as an [r+r] address. This
1854 // will fail if it thinks that the address is more profitably represented as
1855 // reg+imm, e.g. where imm = 0.
1856 if (SelectAddressRegReg(N, Base, Index, DAG))
1857 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001858
Chris Lattnera801fced2006-11-08 02:15:41 +00001859 // If the operand is an addition, always emit this as [r+r], since this is
1860 // better (for code size, and execution, as the memop does the add for free)
1861 // than emitting an explicit add.
1862 if (N.getOpcode() == ISD::ADD) {
1863 Base = N.getOperand(0);
1864 Index = N.getOperand(1);
1865 return true;
1866 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001867
Chris Lattnera801fced2006-11-08 02:15:41 +00001868 // Otherwise, do it the hard way, using R0 as the base register.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001869 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001870 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001871 Index = N;
1872 return true;
1873}
1874
Chris Lattnera801fced2006-11-08 02:15:41 +00001875/// getPreIndexedAddressParts - returns true by value, base pointer and
1876/// offset pointer and addressing mode by reference if the node's address
1877/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001878bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1879 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001880 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001881 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001882 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001883
Ulrich Weigande90b0222013-03-22 14:58:48 +00001884 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001885 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001886 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001887 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001888 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1889 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001890 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001891 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001892 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001893 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001894 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001895 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001896 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001897 } else
1898 return false;
1899
Hal Finkelc93a9a22015-02-25 01:06:45 +00001900 // PowerPC doesn't have preinc load/store instructions for vectors (except
1901 // for QPX, which does have preinc r+r forms).
1902 if (VT.isVector()) {
1903 if (!Subtarget.hasQPX() || (VT != MVT::v4f64 && VT != MVT::v4f32)) {
1904 return false;
1905 } else if (SelectAddressRegRegOnly(Ptr, Offset, Base, DAG)) {
1906 AM = ISD::PRE_INC;
1907 return true;
1908 }
1909 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001910
Ulrich Weigande90b0222013-03-22 14:58:48 +00001911 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1912
1913 // Common code will reject creating a pre-inc form if the base pointer
1914 // is a frame index, or if N is a store and the base pointer is either
1915 // the same as or a predecessor of the value being stored. Check for
1916 // those situations here, and try with swapped Base/Offset instead.
1917 bool Swap = false;
1918
1919 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1920 Swap = true;
1921 else if (!isLoad) {
1922 SDValue Val = cast<StoreSDNode>(N)->getValue();
1923 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1924 Swap = true;
1925 }
1926
1927 if (Swap)
1928 std::swap(Base, Offset);
1929
Hal Finkelca542be2012-06-20 15:43:03 +00001930 AM = ISD::PRE_INC;
1931 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001932 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001933
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001934 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001935 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001936 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001937 return false;
1938 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001939 // LDU/STU need an address with at least 4-byte alignment.
1940 if (Alignment < 4)
1941 return false;
1942
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001943 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001944 return false;
1945 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001946
Chris Lattnerb314b152006-11-11 00:08:42 +00001947 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001948 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1949 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001950 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001951 LD->getExtensionType() == ISD::SEXTLOAD &&
1952 isa<ConstantSDNode>(Offset))
1953 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001954 }
1955
Chris Lattnerce645542006-11-10 02:08:47 +00001956 AM = ISD::PRE_INC;
1957 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001958}
1959
1960//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001961// LowerOperation implementation
1962//===----------------------------------------------------------------------===//
1963
Rafael Espindolae1d255f2016-06-27 12:56:02 +00001964/// Return true if we should reference labels using a PICBase, set the HiOpFlags
1965/// and LoOpFlags to the target MO flags.
1966static void getLabelAccessInfo(bool IsPIC, const PPCSubtarget &Subtarget,
Eric Christophercccae792015-01-30 22:02:31 +00001967 unsigned &HiOpFlags, unsigned &LoOpFlags,
Craig Topper062a2ba2014-04-25 05:30:21 +00001968 const GlobalValue *GV = nullptr) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001969 HiOpFlags = PPCII::MO_HA;
1970 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001971
Hal Finkel3ee2af72014-07-18 23:29:49 +00001972 // Don't use the pic base if not in PIC relocation model.
Rafael Espindolae1d255f2016-06-27 12:56:02 +00001973 if (IsPIC) {
Chris Lattnerdd6df842010-11-15 03:13:19 +00001974 HiOpFlags |= PPCII::MO_PIC_FLAG;
1975 LoOpFlags |= PPCII::MO_PIC_FLAG;
1976 }
1977
1978 // If this is a reference to a global value that requires a non-lazy-ptr, make
1979 // sure that instruction lowering adds it.
Eric Christophere8dbfe12015-02-13 22:23:04 +00001980 if (GV && Subtarget.hasLazyResolverStub(GV)) {
Chris Lattnerdd6df842010-11-15 03:13:19 +00001981 HiOpFlags |= PPCII::MO_NLP_FLAG;
1982 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001983
Chris Lattnerdd6df842010-11-15 03:13:19 +00001984 if (GV->hasHiddenVisibility()) {
1985 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1986 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1987 }
1988 }
Chris Lattneredb9d842010-11-15 02:46:57 +00001989}
1990
1991static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1992 SelectionDAG &DAG) {
Daniel Jasper48e93f72015-04-28 13:38:35 +00001993 SDLoc DL(HiPart);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001994 EVT PtrVT = HiPart.getValueType();
1995 SDValue Zero = DAG.getConstant(0, DL, PtrVT);
Chris Lattneredb9d842010-11-15 02:46:57 +00001996
1997 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1998 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001999
Chris Lattneredb9d842010-11-15 02:46:57 +00002000 // With PIC, the first instruction is actually "GR+hi(&G)".
2001 if (isPIC)
2002 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
2003 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00002004
Chris Lattneredb9d842010-11-15 02:46:57 +00002005 // Generate non-pic code that has direct accesses to the constant pool.
2006 // The address of the global is just (hi(&g)+lo(&g)).
2007 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
2008}
2009
Hal Finkele6698d52015-02-01 15:03:28 +00002010static void setUsesTOCBasePtr(MachineFunction &MF) {
2011 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2012 FuncInfo->setUsesTOCBasePtr();
2013}
2014
2015static void setUsesTOCBasePtr(SelectionDAG &DAG) {
2016 setUsesTOCBasePtr(DAG.getMachineFunction());
2017}
2018
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002019static SDValue getTOCEntry(SelectionDAG &DAG, const SDLoc &dl, bool Is64Bit,
Hal Finkelcf599212015-02-25 21:36:59 +00002020 SDValue GA) {
2021 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
2022 SDValue Reg = Is64Bit ? DAG.getRegister(PPC::X2, VT) :
2023 DAG.getNode(PPCISD::GlobalBaseReg, dl, VT);
2024
2025 SDValue Ops[] = { GA, Reg };
Alex Lorenze40c8a22015-08-11 23:09:45 +00002026 return DAG.getMemIntrinsicNode(
2027 PPCISD::TOC_ENTRY, dl, DAG.getVTList(VT, MVT::Other), Ops, VT,
2028 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 0, false, true,
2029 false, 0);
Hal Finkelcf599212015-02-25 21:36:59 +00002030}
2031
Scott Michelcf0da6c2009-02-17 22:15:04 +00002032SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002033 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002034 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00002035 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002036 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00002037
Roman Divackyace47072012-08-24 16:26:02 +00002038 // 64-bit SVR4 ABI code is always position-independent.
2039 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002040 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002041 setUsesTOCBasePtr(DAG);
Roman Divackyace47072012-08-24 16:26:02 +00002042 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Hal Finkelcf599212015-02-25 21:36:59 +00002043 return getTOCEntry(DAG, SDLoc(CP), true, GA);
Roman Divackyace47072012-08-24 16:26:02 +00002044 }
2045
Chris Lattneredb9d842010-11-15 02:46:57 +00002046 unsigned MOHiFlag, MOLoFlag;
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002047 bool IsPIC = isPositionIndependent();
2048 getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002049
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002050 if (IsPIC && Subtarget.isSVR4ABI()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +00002051 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(),
2052 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00002053 return getTOCEntry(DAG, SDLoc(CP), false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002054 }
2055
Chris Lattneredb9d842010-11-15 02:46:57 +00002056 SDValue CPIHi =
2057 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
2058 SDValue CPILo =
2059 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002060 return LowerLabelRef(CPIHi, CPILo, IsPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00002061}
2062
Dan Gohman21cea8a2010-04-17 15:26:15 +00002063SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002064 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002065 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00002066
Roman Divackyace47072012-08-24 16:26:02 +00002067 // 64-bit SVR4 ABI code is always position-independent.
2068 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002069 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002070 setUsesTOCBasePtr(DAG);
Roman Divackyace47072012-08-24 16:26:02 +00002071 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Hal Finkelcf599212015-02-25 21:36:59 +00002072 return getTOCEntry(DAG, SDLoc(JT), true, GA);
Roman Divackyace47072012-08-24 16:26:02 +00002073 }
2074
Chris Lattneredb9d842010-11-15 02:46:57 +00002075 unsigned MOHiFlag, MOLoFlag;
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002076 bool IsPIC = isPositionIndependent();
2077 getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002078
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002079 if (IsPIC && Subtarget.isSVR4ABI()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +00002080 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
2081 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00002082 return getTOCEntry(DAG, SDLoc(GA), false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002083 }
2084
Chris Lattneredb9d842010-11-15 02:46:57 +00002085 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
2086 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002087 return LowerLabelRef(JTIHi, JTILo, IsPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00002088}
2089
Dan Gohman21cea8a2010-04-17 15:26:15 +00002090SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
2091 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00002092 EVT PtrVT = Op.getValueType();
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002093 BlockAddressSDNode *BASDN = cast<BlockAddressSDNode>(Op);
2094 const BlockAddress *BA = BASDN->getBlockAddress();
Bob Wilsonf84f7102009-11-04 21:31:18 +00002095
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002096 // 64-bit SVR4 ABI code is always position-independent.
2097 // The actual BlockAddress is stored in the TOC.
2098 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002099 setUsesTOCBasePtr(DAG);
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002100 SDValue GA = DAG.getTargetBlockAddress(BA, PtrVT, BASDN->getOffset());
Hal Finkelcf599212015-02-25 21:36:59 +00002101 return getTOCEntry(DAG, SDLoc(BASDN), true, GA);
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002102 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002103
Chris Lattneredb9d842010-11-15 02:46:57 +00002104 unsigned MOHiFlag, MOLoFlag;
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002105 bool IsPIC = isPositionIndependent();
2106 getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00002107 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
2108 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002109 return LowerLabelRef(TgtBAHi, TgtBALo, IsPIC, DAG);
Chris Lattneredb9d842010-11-15 02:46:57 +00002110}
2111
Roman Divackye3f15c982012-06-04 17:36:38 +00002112SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
2113 SelectionDAG &DAG) const {
2114
Bill Schmidtbdae03f2013-09-17 20:22:05 +00002115 // FIXME: TLS addresses currently use medium model code sequences,
2116 // which is the most useful form. Eventually support for small and
2117 // large models could be added if users need it, at the cost of
2118 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00002119 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002120 if (DAG.getTarget().Options.EmulatedTLS)
2121 return LowerToTLSEmulatedModel(GA, DAG);
2122
Andrew Trickef9de2a2013-05-25 02:42:55 +00002123 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00002124 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002125 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002126 bool is64bit = Subtarget.isPPC64();
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002127 const Module *M = DAG.getMachineFunction().getFunction()->getParent();
2128 PICLevel::Level picLevel = M->getPICLevel();
Roman Divackye3f15c982012-06-04 17:36:38 +00002129
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002130 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00002131
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002132 if (Model == TLSModel::LocalExec) {
2133 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002134 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002135 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002136 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002137 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
2138 is64bit ? MVT::i64 : MVT::i32);
2139 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
2140 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
2141 }
Roman Divackye3f15c982012-06-04 17:36:38 +00002142
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002143 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00002144 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00002145 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2146 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00002147 SDValue GOTPtr;
2148 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00002149 setUsesTOCBasePtr(DAG);
Roman Divacky32143e22013-12-20 18:08:54 +00002150 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
2151 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
2152 PtrVT, GOTReg, TGA);
2153 } else
2154 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00002155 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00002156 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00002157 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002158 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002159
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002160 if (Model == TLSModel::GeneralDynamic) {
Bill Schmidt82f1c772015-02-10 19:09:05 +00002161 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002162 SDValue GOTPtr;
2163 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00002164 setUsesTOCBasePtr(DAG);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002165 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
2166 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
2167 GOTReg, TGA);
2168 } else {
Davide Italiano4cccc482016-06-17 18:07:14 +00002169 if (picLevel == PICLevel::SmallPIC)
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002170 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
2171 else
2172 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002173 }
Bill Schmidt82f1c772015-02-10 19:09:05 +00002174 return DAG.getNode(PPCISD::ADDI_TLSGD_L_ADDR, dl, PtrVT,
2175 GOTPtr, TGA, TGA);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002176 }
2177
Bill Schmidt24b8dd62012-12-12 19:29:35 +00002178 if (Model == TLSModel::LocalDynamic) {
Bill Schmidt82f1c772015-02-10 19:09:05 +00002179 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002180 SDValue GOTPtr;
2181 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00002182 setUsesTOCBasePtr(DAG);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002183 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
2184 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
2185 GOTReg, TGA);
2186 } else {
Davide Italiano4cccc482016-06-17 18:07:14 +00002187 if (picLevel == PICLevel::SmallPIC)
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002188 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
2189 else
2190 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002191 }
Bill Schmidt82f1c772015-02-10 19:09:05 +00002192 SDValue TLSAddr = DAG.getNode(PPCISD::ADDI_TLSLD_L_ADDR, dl,
2193 PtrVT, GOTPtr, TGA, TGA);
2194 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl,
2195 PtrVT, TLSAddr, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00002196 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
2197 }
2198
2199 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00002200}
2201
Chris Lattneredb9d842010-11-15 02:46:57 +00002202SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
2203 SelectionDAG &DAG) const {
2204 EVT PtrVT = Op.getValueType();
2205 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002206 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00002207 const GlobalValue *GV = GSDN->getGlobal();
2208
Chris Lattneredb9d842010-11-15 02:46:57 +00002209 // 64-bit SVR4 ABI code is always position-independent.
2210 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002211 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002212 setUsesTOCBasePtr(DAG);
Chris Lattneredb9d842010-11-15 02:46:57 +00002213 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
Hal Finkelcf599212015-02-25 21:36:59 +00002214 return getTOCEntry(DAG, DL, true, GA);
Chris Lattneredb9d842010-11-15 02:46:57 +00002215 }
2216
Chris Lattnerdd6df842010-11-15 03:13:19 +00002217 unsigned MOHiFlag, MOLoFlag;
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002218 bool IsPIC = isPositionIndependent();
2219 getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00002220
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002221 if (IsPIC && Subtarget.isSVR4ABI()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +00002222 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT,
2223 GSDN->getOffset(),
2224 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00002225 return getTOCEntry(DAG, DL, false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002226 }
2227
Chris Lattnerdd6df842010-11-15 03:13:19 +00002228 SDValue GAHi =
2229 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
2230 SDValue GALo =
2231 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00002232
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002233 SDValue Ptr = LowerLabelRef(GAHi, GALo, IsPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00002234
Chris Lattnerdd6df842010-11-15 03:13:19 +00002235 // If the global reference is actually to a non-lazy-pointer, we have to do an
2236 // extra load to get the address of the global.
2237 if (MOHiFlag & PPCII::MO_NLP_FLAG)
2238 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002239 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00002240 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00002241}
2242
Dan Gohman21cea8a2010-04-17 15:26:15 +00002243SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00002244 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002245 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002246
Hal Finkel777c9dd2014-03-29 16:04:40 +00002247 if (Op.getValueType() == MVT::v2i64) {
2248 // When the operands themselves are v2i64 values, we need to do something
2249 // special because VSX has no underlying comparison operations for these.
2250 if (Op.getOperand(0).getValueType() == MVT::v2i64) {
2251 // Equality can be handled by casting to the legal type for Altivec
2252 // comparisons, everything else needs to be expanded.
2253 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
2254 return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
2255 DAG.getSetCC(dl, MVT::v4i32,
2256 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)),
2257 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)),
2258 CC));
2259 }
2260
2261 return SDValue();
2262 }
2263
2264 // We handle most of these in the usual way.
2265 return Op;
2266 }
2267
Chris Lattner4211ca92006-04-14 06:01:58 +00002268 // If we're comparing for equality to zero, expose the fact that this is
2269 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
2270 // fold the new nodes.
2271 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2272 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002273 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002274 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00002275 if (VT.bitsLT(MVT::i32)) {
2276 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002277 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002278 }
Duncan Sands13237ac2008-06-06 12:08:01 +00002279 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002280 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
2281 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002282 DAG.getConstant(Log2b, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00002283 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00002284 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002285 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00002286 // optimized. FIXME: revisit this when we can custom lower all setcc
2287 // optimizations.
2288 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002289 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00002290 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002291
Chris Lattner4211ca92006-04-14 06:01:58 +00002292 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00002293 // by xor'ing the rhs with the lhs, which is faster than setting a
2294 // condition register, reading it back out, and masking the correct bit. The
2295 // normal approach here uses sub to do this instead of xor. Using xor exposes
2296 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002297 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002298 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002299 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002300 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00002301 Op.getOperand(1));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002302 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, dl, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00002303 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002304 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00002305}
2306
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002307SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002308 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00002309 SDNode *Node = Op.getNode();
2310 EVT VT = Node->getValueType(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00002311 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Roman Divacky4394e682011-06-28 15:30:42 +00002312 SDValue InChain = Node->getOperand(0);
2313 SDValue VAListPtr = Node->getOperand(1);
2314 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002315 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002316
Roman Divacky4394e682011-06-28 15:30:42 +00002317 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
2318
2319 // gpr_index
2320 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
2321 VAListPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00002322 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002323 InChain = GprIndex.getValue(1);
2324
2325 if (VT == MVT::i64) {
2326 // Check if GprIndex is even
2327 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002328 DAG.getConstant(1, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002329 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002330 DAG.getConstant(0, dl, MVT::i32), ISD::SETNE);
Roman Divacky4394e682011-06-28 15:30:42 +00002331 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002332 DAG.getConstant(1, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002333 // Align GprIndex to be even if it isn't
2334 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
2335 GprIndex);
2336 }
2337
2338 // fpr index is 1 byte after gpr
2339 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002340 DAG.getConstant(1, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002341
2342 // fpr
2343 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
2344 FprPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00002345 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002346 InChain = FprIndex.getValue(1);
2347
2348 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002349 DAG.getConstant(8, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002350
2351 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002352 DAG.getConstant(4, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002353
2354 // areas
2355 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002356 MachinePointerInfo(), false, false,
2357 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002358 InChain = OverflowArea.getValue(1);
2359
2360 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002361 MachinePointerInfo(), false, false,
2362 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002363 InChain = RegSaveArea.getValue(1);
2364
2365 // select overflow_area if index > 8
2366 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002367 DAG.getConstant(8, dl, MVT::i32), ISD::SETLT);
Roman Divacky4394e682011-06-28 15:30:42 +00002368
Roman Divacky4394e682011-06-28 15:30:42 +00002369 // adjustment constant gpr_index * 4/8
2370 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
2371 VT.isInteger() ? GprIndex : FprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002372 DAG.getConstant(VT.isInteger() ? 4 : 8, dl,
Roman Divacky4394e682011-06-28 15:30:42 +00002373 MVT::i32));
2374
2375 // OurReg = RegSaveArea + RegConstant
2376 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
2377 RegConstant);
2378
2379 // Floating types are 32 bytes into RegSaveArea
2380 if (VT.isFloatingPoint())
2381 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002382 DAG.getConstant(32, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002383
2384 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
2385 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
2386 VT.isInteger() ? GprIndex : FprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002387 DAG.getConstant(VT == MVT::i64 ? 2 : 1, dl,
Roman Divacky4394e682011-06-28 15:30:42 +00002388 MVT::i32));
2389
2390 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
2391 VT.isInteger() ? VAListPtr : FprPtr,
2392 MachinePointerInfo(SV),
2393 MVT::i8, false, false, 0);
2394
2395 // determine if we should load from reg_save_area or overflow_area
2396 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
2397
2398 // increase overflow_area by 4/8 if gpr/fpr > 8
2399 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
2400 DAG.getConstant(VT.isInteger() ? 4 : 8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002401 dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002402
2403 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
2404 OverflowAreaPlusN);
2405
2406 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
2407 OverflowAreaPtr,
2408 MachinePointerInfo(),
2409 MVT::i32, false, false, 0);
2410
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00002411 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002412 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002413}
2414
Roman Divackyc3825df2013-07-25 21:36:47 +00002415SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
2416 const PPCSubtarget &Subtarget) const {
2417 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
2418
2419 // We have to copy the entire va_list struct:
2420 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
2421 return DAG.getMemcpy(Op.getOperand(0), Op,
2422 Op.getOperand(1), Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002423 DAG.getConstant(12, SDLoc(Op), MVT::i32), 8, false, true,
2424 false, MachinePointerInfo(), MachinePointerInfo());
Roman Divackyc3825df2013-07-25 21:36:47 +00002425}
2426
Duncan Sandsa0984362011-09-06 13:37:06 +00002427SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
2428 SelectionDAG &DAG) const {
2429 return Op.getOperand(0);
2430}
2431
2432SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
2433 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00002434 SDValue Chain = Op.getOperand(0);
2435 SDValue Trmp = Op.getOperand(1); // trampoline
2436 SDValue FPtr = Op.getOperand(2); // nested function
2437 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00002438 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00002439
Mehdi Amini44ede332015-07-09 02:09:04 +00002440 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Owen Anderson9f944592009-08-11 20:47:22 +00002441 bool isPPC64 = (PtrVT == MVT::i64);
Mehdi Aminia749f2a2015-07-09 02:09:52 +00002442 Type *IntPtrTy = DAG.getDataLayout().getIntPtrType(*DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00002443
Scott Michelcf0da6c2009-02-17 22:15:04 +00002444 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00002445 TargetLowering::ArgListEntry Entry;
2446
2447 Entry.Ty = IntPtrTy;
2448 Entry.Node = Trmp; Args.push_back(Entry);
2449
2450 // TrampSize == (isPPC64 ? 48 : 40);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002451 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002452 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00002453 Args.push_back(Entry);
2454
2455 Entry.Node = FPtr; Args.push_back(Entry);
2456 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002457
Bill Wendling95e1af22008-09-17 00:30:57 +00002458 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002459 TargetLowering::CallLoweringInfo CLI(DAG);
2460 CLI.setDebugLoc(dl).setChain(Chain)
2461 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002462 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002463 std::move(Args));
Bill Wendling95e1af22008-09-17 00:30:57 +00002464
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002465 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Duncan Sandsa0984362011-09-06 13:37:06 +00002466 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00002467}
2468
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002469SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002470 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00002471 MachineFunction &MF = DAG.getMachineFunction();
2472 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2473
Andrew Trickef9de2a2013-05-25 02:42:55 +00002474 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002475
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002476 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002477 // vastart just stores the address of the VarArgsFrameIndex slot into the
2478 // memory location argument.
Mehdi Amini44ede332015-07-09 02:09:04 +00002479 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002480 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002481 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00002482 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2483 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00002484 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002485 }
2486
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002487 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002488 // We suppose the given va_list is already allocated.
2489 //
2490 // typedef struct {
2491 // char gpr; /* index into the array of 8 GPRs
2492 // * stored in the register save area
2493 // * gpr=0 corresponds to r3,
2494 // * gpr=1 to r4, etc.
2495 // */
2496 // char fpr; /* index into the array of 8 FPRs
2497 // * stored in the register save area
2498 // * fpr=0 corresponds to f1,
2499 // * fpr=1 to f2, etc.
2500 // */
2501 // char *overflow_arg_area;
2502 // /* location on stack that holds
2503 // * the next overflow argument
2504 // */
2505 // char *reg_save_area;
2506 // /* where r3:r10 and f1:f8 (if saved)
2507 // * are stored
2508 // */
2509 // } va_list[1];
2510
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002511 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), dl, MVT::i32);
2512 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), dl, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002513
Mehdi Amini44ede332015-07-09 02:09:04 +00002514 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Scott Michelcf0da6c2009-02-17 22:15:04 +00002515
Dan Gohman31ae5862010-04-17 14:41:14 +00002516 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
2517 PtrVT);
2518 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2519 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002520
Duncan Sands13237ac2008-06-06 12:08:01 +00002521 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002522 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, dl, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002523
Duncan Sands13237ac2008-06-06 12:08:01 +00002524 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002525 SDValue ConstStackOffset = DAG.getConstant(StackOffset, dl, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002526
2527 uint64_t FPROffset = 1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002528 SDValue ConstFPROffset = DAG.getConstant(FPROffset, dl, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002529
Dan Gohman2d489b52008-02-06 22:27:42 +00002530 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002531
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002532 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002533 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00002534 Op.getOperand(1),
2535 MachinePointerInfo(SV),
2536 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002537 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002538 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002539 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002540
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002541 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002542 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00002543 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
2544 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00002545 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002546 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002547 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002548
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002549 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002550 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00002551 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
2552 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002553 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002554 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002555 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002556
2557 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00002558 return DAG.getStore(thirdStore, dl, FR, nextPtr,
2559 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002560 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002561
Chris Lattner4211ca92006-04-14 06:01:58 +00002562}
2563
Chris Lattner4f2e4e02007-03-06 00:59:59 +00002564#include "PPCGenCallingConv.inc"
2565
NAKAMURA Takumi84965032015-09-22 11:14:12 +00002566// Function whose sole purpose is to kill compiler warnings
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002567// stemming from unused functions included from PPCGenCallingConv.inc.
2568CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00002569 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002570}
2571
Bill Schmidt230b4512013-06-12 16:39:22 +00002572bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
2573 CCValAssign::LocInfo &LocInfo,
2574 ISD::ArgFlagsTy &ArgFlags,
2575 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002576 return true;
2577}
2578
Bill Schmidt230b4512013-06-12 16:39:22 +00002579bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
2580 MVT &LocVT,
2581 CCValAssign::LocInfo &LocInfo,
2582 ISD::ArgFlagsTy &ArgFlags,
2583 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002584 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002585 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2586 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2587 };
2588 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002589
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002590 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002591
2592 // Skip one register if the first unallocated register has an even register
2593 // number and there are still argument registers available which have not been
2594 // allocated yet. RegNum is actually an index into ArgRegs, which means we
2595 // need to skip a register if RegNum is odd.
2596 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
2597 State.AllocateReg(ArgRegs[RegNum]);
2598 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002599
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002600 // Always return false here, as this function only makes sure that the first
2601 // unallocated register has an odd register number and does not actually
2602 // allocate a register for the current argument.
2603 return false;
2604}
2605
Bill Schmidt230b4512013-06-12 16:39:22 +00002606bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
2607 MVT &LocVT,
2608 CCValAssign::LocInfo &LocInfo,
2609 ISD::ArgFlagsTy &ArgFlags,
2610 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002611 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002612 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2613 PPC::F8
2614 };
2615
2616 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002617
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002618 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002619
2620 // If there is only one Floating-point register left we need to put both f64
2621 // values of a split ppc_fp128 value on the stack.
2622 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
2623 State.AllocateReg(ArgRegs[RegNum]);
2624 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002625
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002626 // Always return false here, as this function only makes sure that the two f64
2627 // values a ppc_fp128 value is split into are both passed in registers or both
2628 // passed on the stack and does not actually allocate a register for the
2629 // current argument.
2630 return false;
2631}
2632
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002633/// FPR - The set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002634/// on Darwin.
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002635static const MCPhysReg FPR[] = {PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5,
2636 PPC::F6, PPC::F7, PPC::F8, PPC::F9, PPC::F10,
2637 PPC::F11, PPC::F12, PPC::F13};
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002638
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002639/// QFPR - The set of QPX registers that should be allocated for arguments.
2640static const MCPhysReg QFPR[] = {
2641 PPC::QF1, PPC::QF2, PPC::QF3, PPC::QF4, PPC::QF5, PPC::QF6, PPC::QF7,
2642 PPC::QF8, PPC::QF9, PPC::QF10, PPC::QF11, PPC::QF12, PPC::QF13};
Hal Finkelc93a9a22015-02-25 01:06:45 +00002643
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002644/// CalculateStackSlotSize - Calculates the size reserved for this argument on
2645/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002646static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002647 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00002648 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002649 if (Flags.isByVal())
2650 ArgSize = Flags.getByValSize();
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002651
2652 // Round up to multiples of the pointer size, except for array members,
2653 // which are always packed.
2654 if (!Flags.isInConsecutiveRegs())
2655 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002656
2657 return ArgSize;
2658}
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002659
2660/// CalculateStackSlotAlignment - Calculates the alignment of this argument
2661/// on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002662static unsigned CalculateStackSlotAlignment(EVT ArgVT, EVT OrigVT,
2663 ISD::ArgFlagsTy Flags,
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002664 unsigned PtrByteSize) {
2665 unsigned Align = PtrByteSize;
2666
2667 // Altivec parameters are padded to a 16 byte boundary.
2668 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2669 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
Kit Bartond4eb73c2015-05-05 16:10:44 +00002670 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 ||
2671 ArgVT == MVT::v1i128)
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002672 Align = 16;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002673 // QPX vector types stored in double-precision are padded to a 32 byte
2674 // boundary.
2675 else if (ArgVT == MVT::v4f64 || ArgVT == MVT::v4i1)
2676 Align = 32;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002677
2678 // ByVal parameters are aligned as requested.
2679 if (Flags.isByVal()) {
2680 unsigned BVAlign = Flags.getByValAlign();
2681 if (BVAlign > PtrByteSize) {
2682 if (BVAlign % PtrByteSize != 0)
2683 llvm_unreachable(
2684 "ByVal alignment is not a multiple of the pointer size");
2685
2686 Align = BVAlign;
2687 }
2688 }
2689
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002690 // Array members are always packed to their original alignment.
2691 if (Flags.isInConsecutiveRegs()) {
2692 // If the array member was split into multiple registers, the first
2693 // needs to be aligned to the size of the full type. (Except for
2694 // ppcf128, which is only aligned as its f64 components.)
2695 if (Flags.isSplit() && OrigVT != MVT::ppcf128)
2696 Align = OrigVT.getStoreSize();
2697 else
2698 Align = ArgVT.getStoreSize();
2699 }
2700
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002701 return Align;
2702}
2703
Ulrich Weigand8658f172014-07-20 23:43:15 +00002704/// CalculateStackSlotUsed - Return whether this argument will use its
2705/// stack slot (instead of being passed in registers). ArgOffset,
2706/// AvailableFPRs, and AvailableVRs must hold the current argument
2707/// position, and will be updated to account for this argument.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002708static bool CalculateStackSlotUsed(EVT ArgVT, EVT OrigVT,
2709 ISD::ArgFlagsTy Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00002710 unsigned PtrByteSize,
2711 unsigned LinkageSize,
2712 unsigned ParamAreaSize,
2713 unsigned &ArgOffset,
2714 unsigned &AvailableFPRs,
Hal Finkelc93a9a22015-02-25 01:06:45 +00002715 unsigned &AvailableVRs, bool HasQPX) {
Ulrich Weigand8658f172014-07-20 23:43:15 +00002716 bool UseMemory = false;
2717
2718 // Respect alignment of argument on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002719 unsigned Align =
2720 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigand8658f172014-07-20 23:43:15 +00002721 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
2722 // If there's no space left in the argument save area, we must
2723 // use memory (this check also catches zero-sized arguments).
2724 if (ArgOffset >= LinkageSize + ParamAreaSize)
2725 UseMemory = true;
2726
2727 // Allocate argument on the stack.
2728 ArgOffset += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002729 if (Flags.isInConsecutiveRegsLast())
2730 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand8658f172014-07-20 23:43:15 +00002731 // If we overran the argument save area, we must use memory
2732 // (this check catches arguments passed partially in memory)
2733 if (ArgOffset > LinkageSize + ParamAreaSize)
2734 UseMemory = true;
2735
2736 // However, if the argument is actually passed in an FPR or a VR,
2737 // we don't use memory after all.
2738 if (!Flags.isByVal()) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00002739 if (ArgVT == MVT::f32 || ArgVT == MVT::f64 ||
2740 // QPX registers overlap with the scalar FP registers.
2741 (HasQPX && (ArgVT == MVT::v4f32 ||
2742 ArgVT == MVT::v4f64 ||
2743 ArgVT == MVT::v4i1)))
Ulrich Weigand8658f172014-07-20 23:43:15 +00002744 if (AvailableFPRs > 0) {
2745 --AvailableFPRs;
2746 return false;
2747 }
2748 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2749 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
Kit Bartond4eb73c2015-05-05 16:10:44 +00002750 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 ||
2751 ArgVT == MVT::v1i128)
Ulrich Weigand8658f172014-07-20 23:43:15 +00002752 if (AvailableVRs > 0) {
2753 --AvailableVRs;
2754 return false;
2755 }
2756 }
2757
2758 return UseMemory;
2759}
2760
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002761/// EnsureStackAlignment - Round stack frame size up from NumBytes to
2762/// ensure minimum alignment required for target.
Eric Christophercccae792015-01-30 22:02:31 +00002763static unsigned EnsureStackAlignment(const PPCFrameLowering *Lowering,
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002764 unsigned NumBytes) {
Eric Christophercccae792015-01-30 22:02:31 +00002765 unsigned TargetAlign = Lowering->getStackAlignment();
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002766 unsigned AlignMask = TargetAlign - 1;
2767 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2768 return NumBytes;
2769}
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002770
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002771SDValue PPCTargetLowering::LowerFormalArguments(
2772 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
2773 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
2774 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002775 if (Subtarget.isSVR4ABI()) {
2776 if (Subtarget.isPPC64())
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002777 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
2778 dl, DAG, InVals);
2779 else
2780 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2781 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002782 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002783 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2784 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002785 }
2786}
2787
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002788SDValue PPCTargetLowering::LowerFormalArguments_32SVR4(
2789 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
2790 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
2791 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002792
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002793 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002794 // +-----------------------------------+
2795 // +--> | Back chain |
2796 // | +-----------------------------------+
2797 // | | Floating-point register save area |
2798 // | +-----------------------------------+
2799 // | | General register save area |
2800 // | +-----------------------------------+
2801 // | | CR save word |
2802 // | +-----------------------------------+
2803 // | | VRSAVE save word |
2804 // | +-----------------------------------+
2805 // | | Alignment padding |
2806 // | +-----------------------------------+
2807 // | | Vector register save area |
2808 // | +-----------------------------------+
2809 // | | Local variable space |
2810 // | +-----------------------------------+
2811 // | | Parameter list area |
2812 // | +-----------------------------------+
2813 // | | LR save word |
2814 // | +-----------------------------------+
2815 // SP--> +--- | Back chain |
2816 // +-----------------------------------+
2817 //
2818 // Specifications:
2819 // System V Application Binary Interface PowerPC Processor Supplement
2820 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002821
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002822 MachineFunction &MF = DAG.getMachineFunction();
2823 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002824 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002825
Mehdi Amini44ede332015-07-09 02:09:04 +00002826 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002827 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002828 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2829 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002830 unsigned PtrByteSize = 4;
2831
2832 // Assign locations to all of the incoming arguments.
2833 SmallVector<CCValAssign, 16> ArgLocs;
Strahinja Petrovice682b802016-05-09 12:27:39 +00002834 PPCCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
Eric Christopherb5217502014-08-06 18:45:26 +00002835 *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002836
2837 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00002838 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002839 CCInfo.AllocateStack(LinkageSize, PtrByteSize);
Strahinja Petrovice682b802016-05-09 12:27:39 +00002840 if (Subtarget.useSoftFloat())
2841 CCInfo.PreAnalyzeFormalArguments(Ins);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002842
Bill Schmidtef17c142013-02-06 17:33:58 +00002843 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Strahinja Petrovice682b802016-05-09 12:27:39 +00002844 CCInfo.clearWasPPCF128();
Wesley Peck527da1b2010-11-23 03:31:01 +00002845
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002846 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2847 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002848
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002849 // Arguments stored in registers.
2850 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002851 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002852 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002853
Owen Anderson9f944592009-08-11 20:47:22 +00002854 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002855 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002856 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002857 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002858 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002859 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002860 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002861 case MVT::f32:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00002862 if (Subtarget.hasP8Vector())
2863 RC = &PPC::VSSRCRegClass;
2864 else
2865 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002866 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002867 case MVT::f64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002868 if (Subtarget.hasVSX())
Hal Finkel19be5062014-03-29 05:29:01 +00002869 RC = &PPC::VSFRCRegClass;
2870 else
2871 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002872 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002873 case MVT::v16i8:
2874 case MVT::v8i16:
2875 case MVT::v4i32:
Hal Finkel7811c612014-03-28 19:58:11 +00002876 RC = &PPC::VRRCRegClass;
2877 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002878 case MVT::v4f32:
2879 RC = Subtarget.hasQPX() ? &PPC::QSRCRegClass : &PPC::VRRCRegClass;
2880 break;
Hal Finkel27774d92014-03-13 07:58:58 +00002881 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002882 case MVT::v2i64:
Hal Finkel7811c612014-03-28 19:58:11 +00002883 RC = &PPC::VSHRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002884 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002885 case MVT::v4f64:
2886 RC = &PPC::QFRCRegClass;
2887 break;
2888 case MVT::v4i1:
2889 RC = &PPC::QBRCRegClass;
2890 break;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002891 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002892
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002893 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002894 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002895 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2896 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2897
2898 if (ValVT == MVT::i1)
2899 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002900
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002901 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002902 } else {
2903 // Argument stored in memory.
2904 assert(VA.isMemLoc());
2905
Hal Finkel940ab932014-02-28 00:27:01 +00002906 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002907 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002908 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002909
2910 // Create load nodes to retrieve arguments from the stack.
2911 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002912 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2913 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002914 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002915 }
2916 }
2917
2918 // Assign locations to all of the incoming aggregate by value arguments.
2919 // Aggregates passed by value are stored in the local variable space of the
2920 // caller's stack frame, right above the parameter list area.
2921 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002922 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Eric Christopherb5217502014-08-06 18:45:26 +00002923 ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002924
2925 // Reserve stack space for the allocations in CCInfo.
2926 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2927
Bill Schmidtef17c142013-02-06 17:33:58 +00002928 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002929
2930 // Area that is at least reserved in the caller of this function.
2931 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002932 MinReservedArea = std::max(MinReservedArea, LinkageSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00002933
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002934 // Set the size that is at least reserved in caller of this function. Tail
2935 // call optimized function's reserved stack space needs to be aligned so that
2936 // taking the difference between two stack areas will result in an aligned
2937 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00002938 MinReservedArea =
2939 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002940 FuncInfo->setMinReservedArea(MinReservedArea);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002941
2942 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002943
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002944 // If the function takes variable number of arguments, make a frame index for
2945 // the start of the first vararg value... for expansion of llvm.va_start.
2946 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +00002947 static const MCPhysReg GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002948 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2949 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2950 };
2951 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2952
Craig Topper840beec2014-04-04 05:16:06 +00002953 static const MCPhysReg FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002954 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2955 PPC::F8
2956 };
Joerg Sonnenbergereb8655a2014-08-08 16:46:10 +00002957 unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
Petar Jovanovic280f7102015-12-14 17:57:33 +00002958
2959 if (Subtarget.useSoftFloat())
2960 NumFPArgRegs = 0;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002961
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002962 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs));
2963 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002964
2965 // Make room for NumGPArgRegs and NumFPArgRegs.
2966 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Craig Topper7ff15922014-09-10 04:51:36 +00002967 NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002968
Dan Gohman31ae5862010-04-17 14:41:14 +00002969 FuncInfo->setVarArgsStackOffset(
2970 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002971 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002972
Dan Gohman31ae5862010-04-17 14:41:14 +00002973 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2974 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002975
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002976 // The fixed integer arguments of a variadic function are stored to the
Nick Lewycky99800752016-06-28 01:45:05 +00002977 // VarArgsFrameIndex on the stack so that they may be loaded by
2978 // dereferencing the result of va_next.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002979 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2980 // Get an existing live-in vreg, or add a new one.
2981 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2982 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002983 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002984
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002985 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002986 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2987 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002988 MemOps.push_back(Store);
2989 // Increment the address by four for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002990 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002991 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2992 }
2993
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002994 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2995 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002996 // The double arguments are stored to the VarArgsFrameIndex
2997 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002998 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2999 // Get an existing live-in vreg, or add a new one.
3000 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
3001 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00003002 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003003
Owen Anderson9f944592009-08-11 20:47:22 +00003004 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00003005 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3006 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003007 MemOps.push_back(Store);
3008 // Increment the address by eight for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003009 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8, dl,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003010 PtrVT);
3011 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
3012 }
3013 }
3014
3015 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003016 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003017
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003018 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003019}
3020
Bill Schmidt57d6de52012-10-23 15:51:16 +00003021// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
3022// value to MVT::i64 and then truncate to the correct register size.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003023SDValue PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags,
3024 EVT ObjectVT, SelectionDAG &DAG,
3025 SDValue ArgVal,
3026 const SDLoc &dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003027 if (Flags.isSExt())
3028 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
3029 DAG.getValueType(ObjectVT));
3030 else if (Flags.isZExt())
3031 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
3032 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00003033
Hal Finkel940ab932014-02-28 00:27:01 +00003034 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003035}
3036
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003037SDValue PPCTargetLowering::LowerFormalArguments_64SVR4(
3038 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
3039 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
3040 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003041 // TODO: add description of PPC stack frame format, or at least some docs.
3042 //
Ulrich Weigand8658f172014-07-20 23:43:15 +00003043 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00003044 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003045 MachineFunction &MF = DAG.getMachineFunction();
3046 MachineFrameInfo *MFI = MF.getFrameInfo();
3047 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
3048
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003049 assert(!(CallConv == CallingConv::Fast && isVarArg) &&
3050 "fastcc not supported on varargs functions");
3051
Mehdi Amini44ede332015-07-09 02:09:04 +00003052 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003053 // Potential tail calls could cause overwriting of argument stack slots.
3054 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
3055 (CallConv == CallingConv::Fast));
3056 unsigned PtrByteSize = 8;
Eric Christophera4ae2132015-02-13 22:22:57 +00003057 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003058
Craig Topper840beec2014-04-04 05:16:06 +00003059 static const MCPhysReg GPR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003060 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3061 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3062 };
Craig Topper840beec2014-04-04 05:16:06 +00003063 static const MCPhysReg VR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003064 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3065 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3066 };
Craig Topper840beec2014-04-04 05:16:06 +00003067 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00003068 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
3069 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
3070 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003071
3072 const unsigned Num_GPR_Regs = array_lengthof(GPR);
3073 const unsigned Num_FPR_Regs = 13;
3074 const unsigned Num_VR_Regs = array_lengthof(VR);
Hal Finkelc93a9a22015-02-25 01:06:45 +00003075 const unsigned Num_QFPR_Regs = Num_FPR_Regs;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003076
Ulrich Weigand8658f172014-07-20 23:43:15 +00003077 // Do a first pass over the arguments to determine whether the ABI
3078 // guarantees that our caller has allocated the parameter save area
3079 // on its stack frame. In the ELFv1 ABI, this is always the case;
3080 // in the ELFv2 ABI, it is true if this is a vararg function or if
3081 // any parameter is located in a stack slot.
3082
3083 bool HasParameterArea = !isELFv2ABI || isVarArg;
3084 unsigned ParamAreaSize = Num_GPR_Regs * PtrByteSize;
3085 unsigned NumBytes = LinkageSize;
3086 unsigned AvailableFPRs = Num_FPR_Regs;
3087 unsigned AvailableVRs = Num_VR_Regs;
Hal Finkel965cea52015-07-12 00:37:44 +00003088 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
3089 if (Ins[i].Flags.isNest())
3090 continue;
3091
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003092 if (CalculateStackSlotUsed(Ins[i].VT, Ins[i].ArgVT, Ins[i].Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00003093 PtrByteSize, LinkageSize, ParamAreaSize,
Hal Finkelc93a9a22015-02-25 01:06:45 +00003094 NumBytes, AvailableFPRs, AvailableVRs,
3095 Subtarget.hasQPX()))
Ulrich Weigand8658f172014-07-20 23:43:15 +00003096 HasParameterArea = true;
Hal Finkel965cea52015-07-12 00:37:44 +00003097 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003098
3099 // Add DAG nodes to load the arguments or copy them out of registers. On
3100 // entry to a function on PPC, the arguments start after the linkage area,
3101 // although the first ones are often in registers.
3102
Ulrich Weigand8658f172014-07-20 23:43:15 +00003103 unsigned ArgOffset = LinkageSize;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003104 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Hal Finkelc93a9a22015-02-25 01:06:45 +00003105 unsigned &QFPR_idx = FPR_idx;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003106 SmallVector<SDValue, 8> MemOps;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003107 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00003108 unsigned CurArgIdx = 0;
3109 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003110 SDValue ArgVal;
3111 bool needsLoad = false;
3112 EVT ObjectVT = Ins[ArgNo].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003113 EVT OrigVT = Ins[ArgNo].ArgVT;
Hal Finkel940ab932014-02-28 00:27:01 +00003114 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003115 unsigned ArgSize = ObjSize;
3116 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Andrew Trick05938a52015-02-16 18:10:47 +00003117 if (Ins[ArgNo].isOrigArg()) {
3118 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx);
3119 CurArgIdx = Ins[ArgNo].getOrigArgIndex();
3120 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003121 // We re-align the argument offset for each argument, except when using the
3122 // fast calling convention, when we need to make sure we do that only when
3123 // we'll actually use a stack slot.
3124 unsigned CurArgOffset, Align;
3125 auto ComputeArgOffset = [&]() {
3126 /* Respect alignment of argument on the stack. */
3127 Align = CalculateStackSlotAlignment(ObjectVT, OrigVT, Flags, PtrByteSize);
3128 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
3129 CurArgOffset = ArgOffset;
3130 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003131
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003132 if (CallConv != CallingConv::Fast) {
3133 ComputeArgOffset();
3134
3135 /* Compute GPR index associated with argument offset. */
3136 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
3137 GPR_idx = std::min(GPR_idx, Num_GPR_Regs);
3138 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003139
3140 // FIXME the codegen can be much improved in some cases.
3141 // We do not have to keep everything in memory.
3142 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003143 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit");
3144
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003145 if (CallConv == CallingConv::Fast)
3146 ComputeArgOffset();
3147
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003148 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
3149 ObjSize = Flags.getByValSize();
3150 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00003151 // Empty aggregate parameters do not take up registers. Examples:
3152 // struct { } a;
3153 // union { } b;
3154 // int c[0];
3155 // etc. However, we have to provide a place-holder in InVals, so
3156 // pretend we have an 8-byte item at the current address for that
3157 // purpose.
3158 if (!ObjSize) {
3159 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
3160 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
3161 InVals.push_back(FIN);
3162 continue;
3163 }
Hal Finkel262a2242013-09-12 23:20:06 +00003164
Ulrich Weigand24195972014-07-20 22:36:52 +00003165 // Create a stack object covering all stack doublewords occupied
Ulrich Weigand8658f172014-07-20 23:43:15 +00003166 // by the argument. If the argument is (fully or partially) on
3167 // the stack, or if the argument is fully in registers but the
3168 // caller has allocated the parameter save anyway, we can refer
3169 // directly to the caller's stack frame. Otherwise, create a
3170 // local copy in our own frame.
3171 int FI;
3172 if (HasParameterArea ||
3173 ArgSize + ArgOffset > LinkageSize + Num_GPR_Regs * PtrByteSize)
Hal Finkel41a55ad2014-08-16 00:17:05 +00003174 FI = MFI->CreateFixedObject(ArgSize, ArgOffset, false, true);
Ulrich Weigand8658f172014-07-20 23:43:15 +00003175 else
3176 FI = MFI->CreateStackObject(ArgSize, Align, false);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003177 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003178
Ulrich Weigand24195972014-07-20 22:36:52 +00003179 // Handle aggregates smaller than 8 bytes.
3180 if (ObjSize < PtrByteSize) {
3181 // The value of the object is its address, which differs from the
3182 // address of the enclosing doubleword on big-endian systems.
3183 SDValue Arg = FIN;
3184 if (!isLittleEndian) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003185 SDValue ArgOff = DAG.getConstant(PtrByteSize - ObjSize, dl, PtrVT);
Ulrich Weigand24195972014-07-20 22:36:52 +00003186 Arg = DAG.getNode(ISD::ADD, dl, ArgOff.getValueType(), Arg, ArgOff);
3187 }
3188 InVals.push_back(Arg);
3189
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003190 if (GPR_idx != Num_GPR_Regs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003191 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003192 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003193 SDValue Store;
3194
3195 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
3196 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
3197 (ObjSize == 2 ? MVT::i16 : MVT::i32));
Ulrich Weigand24195972014-07-20 22:36:52 +00003198 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, Arg,
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00003199 MachinePointerInfo(&*FuncArg), ObjType,
3200 false, false, 0);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003201 } else {
3202 // For sizes that don't fit a truncating store (3, 5, 6, 7),
3203 // store the whole register as-is to the parameter save area
Ulrich Weigand24195972014-07-20 22:36:52 +00003204 // slot.
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00003205 Store =
3206 DAG.getStore(Val.getValue(1), dl, Val, FIN,
3207 MachinePointerInfo(&*FuncArg), false, false, 0);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003208 }
3209
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003210 MemOps.push_back(Store);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003211 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003212 // Whether we copied from a register or not, advance the offset
3213 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003214 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003215 continue;
3216 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003217
Ulrich Weigand24195972014-07-20 22:36:52 +00003218 // The value of the object is its address, which is the address of
3219 // its first stack doubleword.
3220 InVals.push_back(FIN);
3221
3222 // Store whatever pieces of the object are in registers to memory.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003223 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
Ulrich Weigand24195972014-07-20 22:36:52 +00003224 if (GPR_idx == Num_GPR_Regs)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003225 break;
Ulrich Weigand24195972014-07-20 22:36:52 +00003226
3227 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3228 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
3229 SDValue Addr = FIN;
3230 if (j) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003231 SDValue Off = DAG.getConstant(j, dl, PtrVT);
Ulrich Weigand24195972014-07-20 22:36:52 +00003232 Addr = DAG.getNode(ISD::ADD, dl, Off.getValueType(), Addr, Off);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003233 }
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00003234 SDValue Store =
3235 DAG.getStore(Val.getValue(1), dl, Val, Addr,
3236 MachinePointerInfo(&*FuncArg, j), false, false, 0);
Ulrich Weigand24195972014-07-20 22:36:52 +00003237 MemOps.push_back(Store);
3238 ++GPR_idx;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003239 }
Ulrich Weigand24195972014-07-20 22:36:52 +00003240 ArgOffset += ArgSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003241 continue;
3242 }
3243
3244 switch (ObjectVT.getSimpleVT().SimpleTy) {
3245 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00003246 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003247 case MVT::i32:
3248 case MVT::i64:
Hal Finkel965cea52015-07-12 00:37:44 +00003249 if (Flags.isNest()) {
3250 // The 'nest' parameter, if any, is passed in R11.
3251 unsigned VReg = MF.addLiveIn(PPC::X11, &PPC::G8RCRegClass);
3252 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
3253
3254 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
3255 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
3256
3257 break;
3258 }
3259
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003260 // These can be scalar arguments or elements of an integer array type
3261 // passed directly. Clang may use those instead of "byval" aggregate
3262 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003263 if (GPR_idx != Num_GPR_Regs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003264 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003265 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
3266
Hal Finkel940ab932014-02-28 00:27:01 +00003267 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003268 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
3269 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003270 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003271 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003272 if (CallConv == CallingConv::Fast)
3273 ComputeArgOffset();
3274
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003275 needsLoad = true;
3276 ArgSize = PtrByteSize;
3277 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003278 if (CallConv != CallingConv::Fast || needsLoad)
3279 ArgOffset += 8;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003280 break;
3281
3282 case MVT::f32:
3283 case MVT::f64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003284 // These can be scalar arguments or elements of a float array type
3285 // passed directly. The latter are used to implement ELFv2 homogenous
3286 // float aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003287 if (FPR_idx != Num_FPR_Regs) {
3288 unsigned VReg;
3289
3290 if (ObjectVT == MVT::f32)
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00003291 VReg = MF.addLiveIn(FPR[FPR_idx],
3292 Subtarget.hasP8Vector()
3293 ? &PPC::VSSRCRegClass
3294 : &PPC::F4RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003295 else
Eric Christophercccae792015-01-30 22:02:31 +00003296 VReg = MF.addLiveIn(FPR[FPR_idx], Subtarget.hasVSX()
3297 ? &PPC::VSFRCRegClass
3298 : &PPC::F8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003299
3300 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
3301 ++FPR_idx;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003302 } else if (GPR_idx != Num_GPR_Regs && CallConv != CallingConv::Fast) {
Hal Finkel8ea446b2015-01-18 14:31:10 +00003303 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8
3304 // once we support fp <-> gpr moves.
3305
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003306 // This can only ever happen in the presence of f32 array types,
3307 // since otherwise we never run out of FPRs before running out
3308 // of GPRs.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003309 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003310 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
3311
3312 if (ObjectVT == MVT::f32) {
3313 if ((ArgOffset % PtrByteSize) == (isLittleEndian ? 4 : 0))
3314 ArgVal = DAG.getNode(ISD::SRL, dl, MVT::i64, ArgVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003315 DAG.getConstant(32, dl, MVT::i32));
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003316 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
3317 }
3318
3319 ArgVal = DAG.getNode(ISD::BITCAST, dl, ObjectVT, ArgVal);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003320 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003321 if (CallConv == CallingConv::Fast)
3322 ComputeArgOffset();
3323
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003324 needsLoad = true;
3325 }
3326
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003327 // When passing an array of floats, the array occupies consecutive
3328 // space in the argument area; only round up to the next doubleword
3329 // at the end of the array. Otherwise, each float takes 8 bytes.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003330 if (CallConv != CallingConv::Fast || needsLoad) {
3331 ArgSize = Flags.isInConsecutiveRegs() ? ObjSize : PtrByteSize;
3332 ArgOffset += ArgSize;
3333 if (Flags.isInConsecutiveRegsLast())
3334 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
3335 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003336 break;
3337 case MVT::v4f32:
3338 case MVT::v4i32:
3339 case MVT::v8i16:
3340 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00003341 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00003342 case MVT::v2i64:
Kit Bartond4eb73c2015-05-05 16:10:44 +00003343 case MVT::v1i128:
Hal Finkelc93a9a22015-02-25 01:06:45 +00003344 if (!Subtarget.hasQPX()) {
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003345 // These can be scalar arguments or elements of a vector array type
3346 // passed directly. The latter are used to implement ELFv2 homogenous
3347 // vector aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003348 if (VR_idx != Num_VR_Regs) {
Hal Finkel7811c612014-03-28 19:58:11 +00003349 unsigned VReg = (ObjectVT == MVT::v2f64 || ObjectVT == MVT::v2i64) ?
3350 MF.addLiveIn(VSRH[VR_idx], &PPC::VSHRCRegClass) :
3351 MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003352 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003353 ++VR_idx;
3354 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003355 if (CallConv == CallingConv::Fast)
3356 ComputeArgOffset();
3357
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003358 needsLoad = true;
3359 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003360 if (CallConv != CallingConv::Fast || needsLoad)
3361 ArgOffset += 16;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003362 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00003363 } // not QPX
3364
3365 assert(ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 &&
3366 "Invalid QPX parameter type");
3367 /* fall through */
3368
3369 case MVT::v4f64:
3370 case MVT::v4i1:
3371 // QPX vectors are treated like their scalar floating-point subregisters
3372 // (except that they're larger).
3373 unsigned Sz = ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 ? 16 : 32;
3374 if (QFPR_idx != Num_QFPR_Regs) {
3375 const TargetRegisterClass *RC;
3376 switch (ObjectVT.getSimpleVT().SimpleTy) {
3377 case MVT::v4f64: RC = &PPC::QFRCRegClass; break;
3378 case MVT::v4f32: RC = &PPC::QSRCRegClass; break;
3379 default: RC = &PPC::QBRCRegClass; break;
3380 }
3381
3382 unsigned VReg = MF.addLiveIn(QFPR[QFPR_idx], RC);
3383 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
3384 ++QFPR_idx;
3385 } else {
3386 if (CallConv == CallingConv::Fast)
3387 ComputeArgOffset();
3388 needsLoad = true;
3389 }
3390 if (CallConv != CallingConv::Fast || needsLoad)
3391 ArgOffset += Sz;
3392 break;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003393 }
3394
3395 // We need to load the argument to a virtual register if we determined
3396 // above that we ran out of physical registers of the appropriate type.
3397 if (needsLoad) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00003398 if (ObjSize < ArgSize && !isLittleEndian)
3399 CurArgOffset += ArgSize - ObjSize;
3400 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, isImmutable);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003401 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
3402 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
3403 false, false, false, 0);
3404 }
3405
3406 InVals.push_back(ArgVal);
3407 }
3408
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003409 // Area that is at least reserved in the caller of this function.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00003410 unsigned MinReservedArea;
Ulrich Weigand8658f172014-07-20 23:43:15 +00003411 if (HasParameterArea)
3412 MinReservedArea = std::max(ArgOffset, LinkageSize + 8 * PtrByteSize);
3413 else
3414 MinReservedArea = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003415
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003416 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003417 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003418 // taking the difference between two stack areas will result in an aligned
3419 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00003420 MinReservedArea =
3421 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003422 FuncInfo->setMinReservedArea(MinReservedArea);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003423
3424 // If the function takes variable number of arguments, make a frame index for
3425 // the start of the first vararg value... for expansion of llvm.va_start.
3426 if (isVarArg) {
3427 int Depth = ArgOffset;
3428
3429 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00003430 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003431 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
3432
3433 // If this function is vararg, store any remaining integer argument regs
Nick Lewycky99800752016-06-28 01:45:05 +00003434 // to their spots on the stack so that they may be loaded by dereferencing
3435 // the result of va_next.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00003436 for (GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
3437 GPR_idx < Num_GPR_Regs; ++GPR_idx) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003438 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3439 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
3440 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3441 MachinePointerInfo(), false, false, 0);
3442 MemOps.push_back(Store);
3443 // Increment the address by four for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003444 SDValue PtrOff = DAG.getConstant(PtrByteSize, dl, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003445 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
3446 }
3447 }
3448
3449 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003450 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003451
3452 return Chain;
3453}
3454
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003455SDValue PPCTargetLowering::LowerFormalArguments_Darwin(
3456 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
3457 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
3458 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003459 // TODO: add description of PPC stack frame format, or at least some docs.
3460 //
3461 MachineFunction &MF = DAG.getMachineFunction();
3462 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00003463 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003464
Mehdi Amini44ede332015-07-09 02:09:04 +00003465 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Owen Anderson9f944592009-08-11 20:47:22 +00003466 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003467 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003468 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
3469 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00003470 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Eric Christophera4ae2132015-02-13 22:22:57 +00003471 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003472 unsigned ArgOffset = LinkageSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003473 // Area that is at least reserved in caller of this function.
3474 unsigned MinReservedArea = ArgOffset;
3475
Craig Topper840beec2014-04-04 05:16:06 +00003476 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003477 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
3478 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
3479 };
Craig Topper840beec2014-04-04 05:16:06 +00003480 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00003481 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3482 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3483 };
Craig Topper840beec2014-04-04 05:16:06 +00003484 static const MCPhysReg VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003485 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3486 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3487 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00003488
Owen Andersone2f23a32007-09-07 04:06:50 +00003489 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003490 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00003491 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00003492
3493 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003494
Craig Topper840beec2014-04-04 05:16:06 +00003495 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003496
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003497 // In 32-bit non-varargs functions, the stack space for vectors is after the
3498 // stack space for non-vectors. We do not use this space unless we have
3499 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00003500 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003501 // that out...for the pathological case, compute VecArgOffset as the
3502 // start of the vector parameter area. Computing VecArgOffset is the
3503 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003504 unsigned VecArgOffset = ArgOffset;
3505 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003506 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003507 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003508 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003509 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003510
Duncan Sandsd97eea32008-03-21 09:14:45 +00003511 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003512 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00003513 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003514 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003515 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
3516 VecArgOffset += ArgSize;
3517 continue;
3518 }
3519
Owen Anderson9f944592009-08-11 20:47:22 +00003520 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003521 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003522 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003523 case MVT::i32:
3524 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003525 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003526 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003527 case MVT::i64: // PPC64
3528 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003529 // FIXME: We are guaranteed to be !isPPC64 at this point.
3530 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003531 VecArgOffset += 8;
3532 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003533 case MVT::v4f32:
3534 case MVT::v4i32:
3535 case MVT::v8i16:
3536 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003537 // Nothing to do, we're only looking at Nonvector args here.
3538 break;
3539 }
3540 }
3541 }
3542 // We've found where the vector parameter area in memory is. Skip the
3543 // first 12 parameters; these don't use that memory.
3544 VecArgOffset = ((VecArgOffset+15)/16)*16;
3545 VecArgOffset += 12*16;
3546
Chris Lattner4302e8f2006-05-16 18:18:50 +00003547 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00003548 // entry to a function on PPC, the arguments start after the linkage area,
3549 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00003550
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003551 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003552 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00003553 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00003554 unsigned CurArgIdx = 0;
3555 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003556 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003557 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00003558 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00003559 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00003560 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003561 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Andrew Trick05938a52015-02-16 18:10:47 +00003562 if (Ins[ArgNo].isOrigArg()) {
3563 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx);
3564 CurArgIdx = Ins[ArgNo].getOrigArgIndex();
3565 }
Chris Lattner318f0d22006-05-16 18:51:52 +00003566 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003567
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003568 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00003569 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
3570 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003571 if (isVarArg || isPPC64) {
3572 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003573 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003574 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003575 PtrByteSize);
3576 } else nAltivecParamsAtEnd++;
3577 } else
3578 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003579 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003580 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003581 PtrByteSize);
3582
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003583 // FIXME the codegen can be much improved in some cases.
3584 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003585 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003586 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit");
3587
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003588 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003589 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003590 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003591 // Objects of size 1 and 2 are right justified, everything else is
3592 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00003593 if (ObjSize==1 || ObjSize==2) {
3594 CurArgOffset = CurArgOffset + (4 - ObjSize);
3595 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003596 // The value of the object is its address.
Hal Finkel41a55ad2014-08-16 00:17:05 +00003597 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, false, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003598 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003599 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003600 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00003601 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003602 unsigned VReg;
3603 if (isPPC64)
3604 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3605 else
3606 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003607 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003608 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003609 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00003610 MachinePointerInfo(&*FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003611 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00003612 MemOps.push_back(Store);
3613 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00003614 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003615
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003616 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00003617
Dale Johannesen21a8f142008-03-08 01:41:42 +00003618 continue;
3619 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003620 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
3621 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003622 // to memory. ArgOffset will be the address of the beginning
3623 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003624 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003625 unsigned VReg;
3626 if (isPPC64)
3627 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3628 else
3629 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00003630 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003631 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003632 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00003633 SDValue Store =
3634 DAG.getStore(Val.getValue(1), dl, Val, FIN,
3635 MachinePointerInfo(&*FuncArg, j), false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003636 MemOps.push_back(Store);
3637 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003638 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003639 } else {
3640 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
3641 break;
3642 }
3643 }
3644 continue;
3645 }
3646
Owen Anderson9f944592009-08-11 20:47:22 +00003647 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003648 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003649 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003650 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00003651 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00003652 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003653 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003654 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Hal Finkel7f908e82014-03-06 00:45:19 +00003655
3656 if (ObjectVT == MVT::i1)
3657 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal);
3658
Bill Wendling968f32c2008-03-07 20:49:02 +00003659 ++GPR_idx;
3660 } else {
3661 needsLoad = true;
3662 ArgSize = PtrByteSize;
3663 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003664 // All int arguments reserve stack space in the Darwin ABI.
3665 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00003666 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003667 }
Bill Wendling968f32c2008-03-07 20:49:02 +00003668 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00003669 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00003670 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003671 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003672 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00003673
Hal Finkel940ab932014-02-28 00:27:01 +00003674 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00003675 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00003676 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003677 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00003678
Chris Lattnerec78cad2006-06-26 22:48:35 +00003679 ++GPR_idx;
3680 } else {
3681 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00003682 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003683 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003684 // All int arguments reserve stack space in the Darwin ABI.
3685 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003686 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003687
Owen Anderson9f944592009-08-11 20:47:22 +00003688 case MVT::f32:
3689 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00003690 // Every 4 bytes of argument space consumes one of the GPRs available for
3691 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003692 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003693 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00003694 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003695 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00003696 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003697 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003698 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003699
Owen Anderson9f944592009-08-11 20:47:22 +00003700 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00003701 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003702 else
Devang Patelf3292b22011-02-21 23:21:26 +00003703 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003704
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003705 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003706 ++FPR_idx;
3707 } else {
3708 needsLoad = true;
3709 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003710
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003711 // All FP arguments reserve stack space in the Darwin ABI.
3712 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003713 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003714 case MVT::v4f32:
3715 case MVT::v4i32:
3716 case MVT::v8i16:
3717 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00003718 // Note that vector arguments in registers don't reserve stack space,
3719 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003720 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003721 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003722 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00003723 if (isVarArg) {
3724 while ((ArgOffset % 16) != 0) {
3725 ArgOffset += PtrByteSize;
3726 if (GPR_idx != Num_GPR_Regs)
3727 GPR_idx++;
3728 }
3729 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003730 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00003731 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003732 ++VR_idx;
3733 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003734 if (!isVarArg && !isPPC64) {
3735 // Vectors go after all the nonvectors.
3736 CurArgOffset = VecArgOffset;
3737 VecArgOffset += 16;
3738 } else {
3739 // Vectors are aligned.
3740 ArgOffset = ((ArgOffset+15)/16)*16;
3741 CurArgOffset = ArgOffset;
3742 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00003743 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003744 needsLoad = true;
3745 }
3746 break;
3747 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003748
Chris Lattner4302e8f2006-05-16 18:18:50 +00003749 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003750 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003751 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003752 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003753 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00003754 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003755 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00003756 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003757 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003758 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003759
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003760 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003761 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003762
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003763 // Allow for Altivec parameters at the end, if needed.
3764 if (nAltivecParamsAtEnd) {
3765 MinReservedArea = ((MinReservedArea+15)/16)*16;
3766 MinReservedArea += 16*nAltivecParamsAtEnd;
3767 }
3768
3769 // Area that is at least reserved in the caller of this function.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003770 MinReservedArea = std::max(MinReservedArea, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003771
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003772 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003773 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003774 // taking the difference between two stack areas will result in an aligned
3775 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00003776 MinReservedArea =
3777 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003778 FuncInfo->setMinReservedArea(MinReservedArea);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003779
Chris Lattner4302e8f2006-05-16 18:18:50 +00003780 // If the function takes variable number of arguments, make a frame index for
3781 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003782 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003783 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003784
Dan Gohman31ae5862010-04-17 14:41:14 +00003785 FuncInfo->setVarArgsFrameIndex(
3786 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00003787 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00003788 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003789
Chris Lattner4302e8f2006-05-16 18:18:50 +00003790 // If this function is vararg, store any remaining integer argument regs
Nick Lewycky99800752016-06-28 01:45:05 +00003791 // to their spots on the stack so that they may be loaded by dereferencing
3792 // the result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003793 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00003794 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00003795
Chris Lattner2cca3852006-11-18 01:57:19 +00003796 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00003797 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003798 else
Devang Patelf3292b22011-02-21 23:21:26 +00003799 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003800
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003801 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00003802 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3803 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003804 MemOps.push_back(Store);
3805 // Increment the address by four for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003806 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00003807 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003808 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003809 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003810
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003811 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003812 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003813
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003814 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003815}
3816
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003817/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003818/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00003819static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003820 unsigned ParamSize) {
3821
Dale Johannesen86dcae12009-11-24 01:09:07 +00003822 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003823
3824 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
3825 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3826 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3827 // Remember only if the new adjustement is bigger.
3828 if (SPDiff < FI->getTailCallSPDelta())
3829 FI->setTailCallSPDelta(SPDiff);
3830
3831 return SPDiff;
3832}
3833
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00003834static bool isFunctionGlobalAddress(SDValue Callee);
3835
3836static bool
3837resideInSameModule(SDValue Callee, Reloc::Model RelMod) {
3838 // If !G, Callee can be an external symbol.
3839 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
3840 if (!G) return false;
3841
3842 const GlobalValue *GV = G->getGlobal();
3843
3844 if (GV->isDeclaration()) return false;
3845
3846 switch(GV->getLinkage()) {
3847 default: llvm_unreachable("unknow linkage type");
3848 case GlobalValue::AvailableExternallyLinkage:
3849 case GlobalValue::ExternalWeakLinkage:
3850 return false;
3851
3852 // Callee with weak linkage is allowed if it has hidden or protected
3853 // visibility
3854 case GlobalValue::LinkOnceAnyLinkage:
3855 case GlobalValue::LinkOnceODRLinkage: // e.g. c++ inline functions
3856 case GlobalValue::WeakAnyLinkage:
3857 case GlobalValue::WeakODRLinkage: // e.g. c++ template instantiation
3858 if (GV->hasDefaultVisibility())
3859 return false;
3860
3861 case GlobalValue::ExternalLinkage:
3862 case GlobalValue::InternalLinkage:
3863 case GlobalValue::PrivateLinkage:
3864 break;
3865 }
3866
3867 // With '-fPIC', calling default visiblity function need insert 'nop' after
3868 // function call, no matter that function resides in same module or not, so
3869 // we treat it as in different module.
3870 if (RelMod == Reloc::PIC_ && GV->hasDefaultVisibility())
3871 return false;
3872
3873 return true;
3874}
3875
3876static bool
3877needStackSlotPassParameters(const PPCSubtarget &Subtarget,
3878 const SmallVectorImpl<ISD::OutputArg> &Outs) {
3879 assert(Subtarget.isSVR4ABI() && Subtarget.isPPC64());
3880
3881 const unsigned PtrByteSize = 8;
3882 const unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
3883
3884 static const MCPhysReg GPR[] = {
3885 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3886 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3887 };
3888 static const MCPhysReg VR[] = {
3889 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3890 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3891 };
3892
3893 const unsigned NumGPRs = array_lengthof(GPR);
3894 const unsigned NumFPRs = 13;
3895 const unsigned NumVRs = array_lengthof(VR);
3896 const unsigned ParamAreaSize = NumGPRs * PtrByteSize;
3897
3898 unsigned NumBytes = LinkageSize;
3899 unsigned AvailableFPRs = NumFPRs;
3900 unsigned AvailableVRs = NumVRs;
3901
3902 for (const ISD::OutputArg& Param : Outs) {
3903 if (Param.Flags.isNest()) continue;
3904
3905 if (CalculateStackSlotUsed(Param.VT, Param.ArgVT, Param.Flags,
3906 PtrByteSize, LinkageSize, ParamAreaSize,
3907 NumBytes, AvailableFPRs, AvailableVRs,
3908 Subtarget.hasQPX()))
3909 return true;
3910 }
3911 return false;
3912}
3913
3914static bool
3915hasSameArgumentList(const Function *CallerFn, ImmutableCallSite *CS) {
3916 if (CS->arg_size() != CallerFn->getArgumentList().size())
3917 return false;
3918
3919 ImmutableCallSite::arg_iterator CalleeArgIter = CS->arg_begin();
3920 ImmutableCallSite::arg_iterator CalleeArgEnd = CS->arg_end();
3921 Function::const_arg_iterator CallerArgIter = CallerFn->arg_begin();
3922
3923 for (; CalleeArgIter != CalleeArgEnd; ++CalleeArgIter, ++CallerArgIter) {
3924 const Value* CalleeArg = *CalleeArgIter;
3925 const Value* CallerArg = &(*CallerArgIter);
3926 if (CalleeArg == CallerArg)
3927 continue;
3928
3929 // e.g. @caller([4 x i64] %a, [4 x i64] %b) {
3930 // tail call @callee([4 x i64] undef, [4 x i64] %b)
3931 // }
3932 // 1st argument of callee is undef and has the same type as caller.
3933 if (CalleeArg->getType() == CallerArg->getType() &&
3934 isa<UndefValue>(CalleeArg))
3935 continue;
3936
3937 return false;
3938 }
3939
3940 return true;
3941}
3942
3943bool
3944PPCTargetLowering::IsEligibleForTailCallOptimization_64SVR4(
3945 SDValue Callee,
3946 CallingConv::ID CalleeCC,
3947 ImmutableCallSite *CS,
3948 bool isVarArg,
3949 const SmallVectorImpl<ISD::OutputArg> &Outs,
3950 const SmallVectorImpl<ISD::InputArg> &Ins,
3951 SelectionDAG& DAG) const {
3952 bool TailCallOpt = getTargetMachine().Options.GuaranteedTailCallOpt;
3953
3954 if (DisableSCO && !TailCallOpt) return false;
3955
3956 // Variadic argument functions are not supported.
3957 if (isVarArg) return false;
3958
3959 MachineFunction &MF = DAG.getMachineFunction();
3960 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
3961
3962 // Tail or Sibling call optimization (TCO/SCO) needs callee and caller has
3963 // the same calling convention
3964 if (CallerCC != CalleeCC) return false;
3965
3966 // SCO support C calling convention
3967 if (CalleeCC != CallingConv::Fast && CalleeCC != CallingConv::C)
3968 return false;
3969
3970 // Functions containing by val parameters are not supported.
3971 if (std::any_of(Ins.begin(), Ins.end(),
3972 [](const ISD::InputArg& IA) { return IA.Flags.isByVal(); }))
3973 return false;
3974
3975 // No TCO/SCO on indirect call because Caller have to restore its TOC
3976 if (!isFunctionGlobalAddress(Callee) &&
3977 !isa<ExternalSymbolSDNode>(Callee))
3978 return false;
3979
3980 // Check if Callee resides in the same module, because for now, PPC64 SVR4 ABI
3981 // (ELFv1/ELFv2) doesn't allow tail calls to a symbol resides in another
3982 // module.
3983 // ref: https://bugzilla.mozilla.org/show_bug.cgi?id=973977
3984 if (!resideInSameModule(Callee, getTargetMachine().getRelocationModel()))
3985 return false;
3986
3987 // TCO allows altering callee ABI, so we don't have to check further.
3988 if (CalleeCC == CallingConv::Fast && TailCallOpt)
3989 return true;
3990
3991 if (DisableSCO) return false;
3992
3993 // If callee use the same argument list that caller is using, then we can
3994 // apply SCO on this case. If it is not, then we need to check if callee needs
3995 // stack for passing arguments.
3996 if (!hasSameArgumentList(MF.getFunction(), CS) &&
3997 needStackSlotPassParameters(Subtarget, Outs)) {
3998 return false;
3999 }
4000
4001 return true;
4002}
4003
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004004/// IsEligibleForTailCallOptimization - Check whether the call is eligible
4005/// for tail call optimization. Targets which want to do tail call
4006/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004007bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004008PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004009 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004010 bool isVarArg,
4011 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004012 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004013 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00004014 return false;
4015
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004016 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004017 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00004018 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004019
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004020 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00004021 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004022 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
4023 // Functions containing by val parameters are not supported.
4024 for (unsigned i = 0; i != Ins.size(); i++) {
4025 ISD::ArgFlagsTy Flags = Ins[i].Flags;
4026 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004027 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004028
Alp Tokerf907b892013-12-05 05:44:44 +00004029 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004030 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
4031 return true;
4032
4033 // At the moment we can only do local tail calls (in same module, hidden
4034 // or protected) if we are generating PIC.
4035 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
4036 return G->getGlobal()->hasHiddenVisibility()
4037 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004038 }
4039
4040 return false;
4041}
4042
Chris Lattnereb755fc2006-05-17 19:00:46 +00004043/// isCallCompatibleAddress - Return the immediate to use if the specified
4044/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004045static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00004046 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00004047 if (!C) return nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004048
Dan Gohmaneffb8942008-09-12 16:56:44 +00004049 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00004050 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00004051 SignExtend32<26>(Addr) != Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +00004052 return nullptr; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004053
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004054 return DAG.getConstant((int)C->getZExtValue() >> 2, SDLoc(Op),
Mehdi Amini44ede332015-07-09 02:09:04 +00004055 DAG.getTargetLoweringInfo().getPointerTy(
4056 DAG.getDataLayout())).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00004057}
4058
Dan Gohmand78c4002008-05-13 00:00:25 +00004059namespace {
4060
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004061struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004062 SDValue Arg;
4063 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004064 int FrameIdx;
4065
4066 TailCallArgumentInfo() : FrameIdx(0) {}
4067};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00004068}
Dan Gohmand78c4002008-05-13 00:00:25 +00004069
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004070/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004071static void StoreTailCallArgumentsToStackSlot(
4072 SelectionDAG &DAG, SDValue Chain,
4073 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
4074 SmallVectorImpl<SDValue> &MemOpChains, const SDLoc &dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004075 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004076 SDValue Arg = TailCallArgs[i].Arg;
4077 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004078 int FI = TailCallArgs[i].FrameIdx;
4079 // Store relative to framepointer.
Alex Lorenze40c8a22015-08-11 23:09:45 +00004080 MemOpChains.push_back(DAG.getStore(
4081 Chain, dl, Arg, FIN,
4082 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false,
4083 false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004084 }
4085}
4086
4087/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
4088/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004089static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004090 MachineFunction &MF, SDValue Chain,
4091 SDValue OldRetAddr, SDValue OldFP,
4092 int SPDiff, bool isPPC64,
4093 bool isDarwinABI,
4094 const SDLoc &dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004095 if (SPDiff) {
4096 // Calculate the new stack slot for the return address.
4097 int SlotSize = isPPC64 ? 8 : 4;
Eric Christopherdc3a8a42015-02-13 00:39:38 +00004098 const PPCFrameLowering *FL =
4099 MF.getSubtarget<PPCSubtarget>().getFrameLowering();
4100 int NewRetAddrLoc = SPDiff + FL->getReturnSaveOffset();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004101 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00004102 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00004103 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004104 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00004105 Chain = DAG.getStore(
4106 Chain, dl, OldRetAddr, NewRetAddrFrIdx,
4107 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), NewRetAddr),
4108 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004109
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004110 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
4111 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004112 if (isDarwinABI) {
Eric Christopherdc3a8a42015-02-13 00:39:38 +00004113 int NewFPLoc = SPDiff + FL->getFramePointerSaveOffset();
David Greene1fbe0542009-11-12 20:49:22 +00004114 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00004115 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004116 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00004117 Chain = DAG.getStore(
4118 Chain, dl, OldFP, NewFramePtrIdx,
4119 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), NewFPIdx),
4120 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004121 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004122 }
4123 return Chain;
4124}
4125
4126/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
4127/// the position of the argument.
4128static void
4129CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004130 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00004131 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004132 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00004133 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00004134 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00004135 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004136 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004137 TailCallArgumentInfo Info;
4138 Info.Arg = Arg;
4139 Info.FrameIdxOp = FIN;
4140 Info.FrameIdx = FI;
4141 TailCallArguments.push_back(Info);
4142}
4143
4144/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
4145/// stack slot. Returns the chain as result and the loaded frame pointers in
4146/// LROpOut/FPOpout. Used when tail calling.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004147SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(
4148 SelectionDAG &DAG, int SPDiff, SDValue Chain, SDValue &LROpOut,
4149 SDValue &FPOpOut, bool isDarwinABI, const SDLoc &dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004150 if (SPDiff) {
4151 // Load the LR and FP stack slot for later adjusting.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004152 EVT VT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004153 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00004154 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004155 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004156 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00004157
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004158 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
4159 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004160 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004161 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00004162 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004163 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004164 Chain = SDValue(FPOpOut.getNode(), 1);
4165 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004166 }
4167 return Chain;
4168}
4169
Dale Johannesen85d41a12008-03-04 23:17:14 +00004170/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00004171/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00004172/// specified by the specific parameter attribute. The copy will be passed as
4173/// a byval function parameter.
4174/// Sometimes what we are copying is the end of a larger object, the part that
4175/// does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004176static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
4177 SDValue Chain, ISD::ArgFlagsTy Flags,
4178 SelectionDAG &DAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004179 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00004180 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004181 false, false, false, MachinePointerInfo(),
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004182 MachinePointerInfo());
Dale Johannesen85d41a12008-03-04 23:17:14 +00004183}
Chris Lattner43df5b32007-02-25 05:34:32 +00004184
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004185/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
4186/// tail calls.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004187static void LowerMemOpCallTo(
4188 SelectionDAG &DAG, MachineFunction &MF, SDValue Chain, SDValue Arg,
4189 SDValue PtrOff, int SPDiff, unsigned ArgOffset, bool isPPC64,
4190 bool isTailCall, bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
4191 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments, const SDLoc &dl) {
Mehdi Amini44ede332015-07-09 02:09:04 +00004192 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004193 if (!isTailCall) {
4194 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004195 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004196 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004197 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004198 else
Owen Anderson9f944592009-08-11 20:47:22 +00004199 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00004200 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004201 DAG.getConstant(ArgOffset, dl, PtrVT));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004202 }
Chris Lattner676c61d2010-09-21 18:41:36 +00004203 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
4204 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004205 // Calculate and remember argument location.
4206 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
4207 TailCallArguments);
4208}
4209
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004210static void
4211PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
4212 const SDLoc &dl, bool isPPC64, int SPDiff, unsigned NumBytes,
4213 SDValue LROp, SDValue FPOp, bool isDarwinABI,
4214 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004215 MachineFunction &MF = DAG.getMachineFunction();
4216
4217 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
4218 // might overwrite each other in case of tail call optimization.
4219 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00004220 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004221 InFlag = SDValue();
4222 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
4223 MemOpChains2, dl);
4224 if (!MemOpChains2.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004225 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004226
4227 // Store the return address to the appropriate stack slot.
4228 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
4229 isPPC64, isDarwinABI, dl);
4230
4231 // Emit callseq_end just before tailcall node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004232 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
4233 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004234 InFlag = Chain.getValue(1);
4235}
4236
Hal Finkel87deb0b2015-01-12 04:34:47 +00004237// Is this global address that of a function that can be called by name? (as
4238// opposed to something that must hold a descriptor for an indirect call).
4239static bool isFunctionGlobalAddress(SDValue Callee) {
4240 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
4241 if (Callee.getOpcode() == ISD::GlobalTLSAddress ||
4242 Callee.getOpcode() == ISD::TargetGlobalTLSAddress)
4243 return false;
4244
Manuel Jacob5f6eaac2016-01-16 20:30:46 +00004245 return G->getGlobal()->getValueType()->isFunctionTy();
Hal Finkel87deb0b2015-01-12 04:34:47 +00004246 }
4247
4248 return false;
4249}
4250
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004251static unsigned
4252PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag, SDValue &Chain,
4253 SDValue CallSeqStart, const SDLoc &dl, int SPDiff, bool isTailCall,
4254 bool IsPatchPoint, bool hasNest,
4255 SmallVectorImpl<std::pair<unsigned, SDValue>> &RegsToPass,
4256 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
4257 ImmutableCallSite *CS, const PPCSubtarget &Subtarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004258
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004259 bool isPPC64 = Subtarget.isPPC64();
4260 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004261 bool isELFv2ABI = Subtarget.isELFv2ABI();
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004262
Mehdi Amini44ede332015-07-09 02:09:04 +00004263 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Owen Anderson9f944592009-08-11 20:47:22 +00004264 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004265 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004266
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004267 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004268
Torok Edwin31e90d22010-08-04 20:47:44 +00004269 bool needIndirectCall = true;
Ulrich Weigand9aa09ef2014-06-18 16:14:04 +00004270 if (!isSVR4ABI || !isPPC64)
4271 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
4272 // If this is an absolute destination address, use the munged value.
4273 Callee = SDValue(Dest, 0);
4274 needIndirectCall = false;
4275 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004276
Rafael Espindola53fd4252016-06-23 18:43:06 +00004277 // PC-relative references to external symbols should go through $stub, unless
4278 // we're building with the leopard linker or later, which automatically
4279 // synthesizes these stubs.
Rafael Espindola3beef8d2016-06-27 23:15:57 +00004280 const TargetMachine &TM = DAG.getTarget();
Rafael Espindola53fd4252016-06-23 18:43:06 +00004281 const Module *Mod = DAG.getMachineFunction().getFunction()->getParent();
4282 const GlobalValue *GV = nullptr;
4283 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee))
4284 GV = G->getGlobal();
Rafael Espindola3beef8d2016-06-27 23:15:57 +00004285 bool Local = TM.shouldAssumeDSOLocal(*Mod, GV);
Rafael Espindolaa99ccfc2016-06-29 14:59:50 +00004286 bool UsePlt = !Local && Subtarget.isTargetELF() && !isPPC64;
Rafael Espindola53fd4252016-06-23 18:43:06 +00004287
Hal Finkel87deb0b2015-01-12 04:34:47 +00004288 if (isFunctionGlobalAddress(Callee)) {
4289 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Callee);
4290 // A call to a TLS address is actually an indirect call to a
4291 // thread-specific pointer.
Eric Christopher79cc1e32014-09-02 22:28:02 +00004292 unsigned OpFlags = 0;
Rafael Espindola53fd4252016-06-23 18:43:06 +00004293 if (UsePlt)
Rafael Espindolaa99ccfc2016-06-29 14:59:50 +00004294 OpFlags = PPCII::MO_PLT;
Eric Christopher79cc1e32014-09-02 22:28:02 +00004295
4296 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
4297 // every direct call is) turn it into a TargetGlobalAddress /
4298 // TargetExternalSymbol node so that legalize doesn't hack it.
4299 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
4300 Callee.getValueType(), 0, OpFlags);
4301 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00004302 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004303
Torok Edwin31e90d22010-08-04 20:47:44 +00004304 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004305 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00004306
Rafael Espindola53fd4252016-06-23 18:43:06 +00004307 if (UsePlt)
Rafael Espindolaa99ccfc2016-06-29 14:59:50 +00004308 OpFlags = PPCII::MO_PLT;
Wesley Peck527da1b2010-11-23 03:31:01 +00004309
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004310 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
4311 OpFlags);
4312 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00004313 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004314
Hal Finkel934361a2015-01-14 01:07:51 +00004315 if (IsPatchPoint) {
4316 // We'll form an invalid direct call when lowering a patchpoint; the full
4317 // sequence for an indirect call is complicated, and many of the
4318 // instructions introduced might have side effects (and, thus, can't be
4319 // removed later). The call itself will be removed as soon as the
4320 // argument/return lowering is complete, so the fact that it has the wrong
4321 // kind of operands should not really matter.
4322 needIndirectCall = false;
4323 }
4324
Torok Edwin31e90d22010-08-04 20:47:44 +00004325 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004326 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
4327 // to do the call, we can't use PPCISD::CALL.
4328 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00004329
Hal Finkel63fb9282015-01-13 18:25:05 +00004330 if (isSVR4ABI && isPPC64 && !isELFv2ABI) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00004331 // Function pointers in the 64-bit SVR4 ABI do not point to the function
4332 // entry point, but to the function descriptor (the function entry point
4333 // address is part of the function descriptor though).
4334 // The function descriptor is a three doubleword structure with the
4335 // following fields: function entry point, TOC base address and
4336 // environment pointer.
4337 // Thus for a call through a function pointer, the following actions need
4338 // to be performed:
4339 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00004340 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00004341 // 2. Load the address of the function entry point from the function
4342 // descriptor.
4343 // 3. Load the TOC of the callee from the function descriptor into r2.
4344 // 4. Load the environment pointer from the function descriptor into
4345 // r11.
4346 // 5. Branch to the function entry point address.
4347 // 6. On return of the callee, the TOC of the caller needs to be
4348 // restored (this is done in FinishCall()).
4349 //
Hal Finkele2ab0f12015-01-15 21:17:34 +00004350 // The loads are scheduled at the beginning of the call sequence, and the
4351 // register copies are flagged together to ensure that no other
Tilmann Scheller79fef932009-12-18 13:00:15 +00004352 // operations can be scheduled in between. E.g. without flagging the
Hal Finkele2ab0f12015-01-15 21:17:34 +00004353 // copies together, a TOC access in the caller could be scheduled between
4354 // the assignment of the callee TOC and the branch to the callee, which
Tilmann Scheller79fef932009-12-18 13:00:15 +00004355 // results in the TOC access going through the TOC of the callee instead
4356 // of going through the TOC of the caller, which leads to incorrect code.
4357
4358 // Load the address of the function entry point from the function
4359 // descriptor.
Hal Finkele2ab0f12015-01-15 21:17:34 +00004360 SDValue LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-1);
4361 if (LDChain.getValueType() == MVT::Glue)
4362 LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-2);
4363
4364 bool LoadsInv = Subtarget.hasInvariantFunctionDescriptors();
4365
4366 MachinePointerInfo MPI(CS ? CS->getCalledValue() : nullptr);
4367 SDValue LoadFuncPtr = DAG.getLoad(MVT::i64, dl, LDChain, Callee, MPI,
4368 false, false, LoadsInv, 8);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004369
4370 // Load environment pointer into r11.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004371 SDValue PtrOff = DAG.getIntPtrConstant(16, dl);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004372 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004373 SDValue LoadEnvPtr = DAG.getLoad(MVT::i64, dl, LDChain, AddPtr,
4374 MPI.getWithOffset(16), false, false,
4375 LoadsInv, 8);
4376
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004377 SDValue TOCOff = DAG.getIntPtrConstant(8, dl);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004378 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, TOCOff);
4379 SDValue TOCPtr = DAG.getLoad(MVT::i64, dl, LDChain, AddTOC,
4380 MPI.getWithOffset(8), false, false,
4381 LoadsInv, 8);
4382
Hal Finkele6698d52015-02-01 15:03:28 +00004383 setUsesTOCBasePtr(DAG);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004384 SDValue TOCVal = DAG.getCopyToReg(Chain, dl, PPC::X2, TOCPtr,
4385 InFlag);
4386 Chain = TOCVal.getValue(0);
4387 InFlag = TOCVal.getValue(1);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004388
Hal Finkel965cea52015-07-12 00:37:44 +00004389 // If the function call has an explicit 'nest' parameter, it takes the
4390 // place of the environment pointer.
4391 if (!hasNest) {
4392 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
4393 InFlag);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004394
Hal Finkel965cea52015-07-12 00:37:44 +00004395 Chain = EnvVal.getValue(0);
4396 InFlag = EnvVal.getValue(1);
4397 }
Tilmann Scheller79fef932009-12-18 13:00:15 +00004398
Tilmann Scheller79fef932009-12-18 13:00:15 +00004399 MTCTROps[0] = Chain;
4400 MTCTROps[1] = LoadFuncPtr;
4401 MTCTROps[2] = InFlag;
4402 }
4403
Hal Finkel63fb9282015-01-13 18:25:05 +00004404 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys,
4405 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
4406 InFlag = Chain.getValue(1);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004407
4408 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00004409 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004410 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004411 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004412 CallOpc = PPCISD::BCTRL;
Craig Topper062a2ba2014-04-25 05:30:21 +00004413 Callee.setNode(nullptr);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004414 // Add use of X11 (holding environment pointer)
Hal Finkel965cea52015-07-12 00:37:44 +00004415 if (isSVR4ABI && isPPC64 && !isELFv2ABI && !hasNest)
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004416 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004417 // Add CTR register as callee so a bctr can be emitted later.
4418 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00004419 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004420 }
4421
4422 // If this is a direct call, pass the chain and the callee.
4423 if (Callee.getNode()) {
4424 Ops.push_back(Chain);
4425 Ops.push_back(Callee);
4426 }
4427 // If this is a tail call add stack pointer delta.
4428 if (isTailCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004429 Ops.push_back(DAG.getConstant(SPDiff, dl, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004430
4431 // Add argument registers to the end of the list so that they are known live
4432 // into the call.
4433 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
4434 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
4435 RegsToPass[i].second.getValueType()));
4436
Hal Finkelaf519932015-01-19 07:20:27 +00004437 // All calls, in both the ELF V1 and V2 ABIs, need the TOC register live
4438 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00004439 if (isSVR4ABI && isPPC64 && !IsPatchPoint) {
4440 setUsesTOCBasePtr(DAG);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004441 Ops.push_back(DAG.getRegister(PPC::X2, PtrVT));
Hal Finkele6698d52015-02-01 15:03:28 +00004442 }
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004443
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004444 return CallOpc;
4445}
4446
Roman Divacky76293062012-09-18 16:47:58 +00004447static
4448bool isLocalCall(const SDValue &Callee)
4449{
4450 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00004451 return G->getGlobal()->isStrongDefinitionForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00004452 return false;
4453}
4454
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004455SDValue PPCTargetLowering::LowerCallResult(
4456 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
4457 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
4458 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004459
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004460 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00004461 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
4462 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004463 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004464
4465 // Copy all of the result registers out of their specified physreg.
4466 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
4467 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004468 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004469
4470 SDValue Val = DAG.getCopyFromReg(Chain, dl,
4471 VA.getLocReg(), VA.getLocVT(), InFlag);
4472 Chain = Val.getValue(1);
4473 InFlag = Val.getValue(2);
4474
4475 switch (VA.getLocInfo()) {
4476 default: llvm_unreachable("Unknown loc info!");
4477 case CCValAssign::Full: break;
4478 case CCValAssign::AExt:
4479 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4480 break;
4481 case CCValAssign::ZExt:
4482 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
4483 DAG.getValueType(VA.getValVT()));
4484 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4485 break;
4486 case CCValAssign::SExt:
4487 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
4488 DAG.getValueType(VA.getValVT()));
4489 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4490 break;
4491 }
4492
4493 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004494 }
4495
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004496 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004497}
4498
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004499SDValue PPCTargetLowering::FinishCall(
4500 CallingConv::ID CallConv, const SDLoc &dl, bool isTailCall, bool isVarArg,
4501 bool IsPatchPoint, bool hasNest, SelectionDAG &DAG,
4502 SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass, SDValue InFlag,
4503 SDValue Chain, SDValue CallSeqStart, SDValue &Callee, int SPDiff,
4504 unsigned NumBytes, const SmallVectorImpl<ISD::InputArg> &Ins,
4505 SmallVectorImpl<SDValue> &InVals, ImmutableCallSite *CS) const {
Ulrich Weigand8658f172014-07-20 23:43:15 +00004506
Owen Anderson53aa7a92009-08-10 22:56:29 +00004507 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004508 SmallVector<SDValue, 8> Ops;
Hal Finkele2ab0f12015-01-15 21:17:34 +00004509 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, CallSeqStart, dl,
Hal Finkel965cea52015-07-12 00:37:44 +00004510 SPDiff, isTailCall, IsPatchPoint, hasNest,
4511 RegsToPass, Ops, NodeTys, CS, Subtarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004512
Hal Finkel5ab37802012-08-28 02:10:27 +00004513 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004514 if (isVarArg && Subtarget.isSVR4ABI() && !Subtarget.isPPC64())
Hal Finkel5ab37802012-08-28 02:10:27 +00004515 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
4516
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004517 // When performing tail call optimization the callee pops its arguments off
4518 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00004519 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004520 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004521 (CallConv == CallingConv::Fast &&
4522 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004523
Roman Divackyef21be22012-03-06 16:41:49 +00004524 // Add a register mask operand representing the call-preserved registers.
Eric Christophercccae792015-01-30 22:02:31 +00004525 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00004526 const uint32_t *Mask =
4527 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv);
Roman Divackyef21be22012-03-06 16:41:49 +00004528 assert(Mask && "Missing call preserved mask for calling convention");
4529 Ops.push_back(DAG.getRegisterMask(Mask));
4530
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004531 if (InFlag.getNode())
4532 Ops.push_back(InFlag);
4533
4534 // Emit tail call.
4535 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004536 assert(((Callee.getOpcode() == ISD::Register &&
4537 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
4538 Callee.getOpcode() == ISD::TargetExternalSymbol ||
4539 Callee.getOpcode() == ISD::TargetGlobalAddress ||
4540 isa<ConstantSDNode>(Callee)) &&
4541 "Expecting an global address, external symbol, absolute value or register");
4542
Arnold Schwaighoferdc271142015-05-09 00:10:25 +00004543 DAG.getMachineFunction().getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00004544 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004545 }
4546
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004547 // Add a NOP immediately after the branch instruction when using the 64-bit
4548 // SVR4 ABI. At link time, if caller and callee are in a different module and
4549 // thus have a different TOC, the call will be replaced with a call to a stub
4550 // function which saves the current TOC, loads the TOC of the callee and
4551 // branches to the callee. The NOP will be replaced with a load instruction
4552 // which restores the TOC of the caller from the TOC save slot of the current
4553 // stack frame. If caller and callee belong to the same module (and have the
4554 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00004555
Hal Finkel934361a2015-01-14 01:07:51 +00004556 if (!isTailCall && Subtarget.isSVR4ABI()&& Subtarget.isPPC64() &&
4557 !IsPatchPoint) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004558 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00004559 // This is a call through a function pointer.
4560 // Restore the caller TOC from the save area into R2.
4561 // See PrepareCall() for more information about calls through function
4562 // pointers in the 64-bit SVR4 ABI.
4563 // We are using a target-specific load with r2 hard coded, because the
4564 // result of a target-independent load would never go directly into r2,
4565 // since r2 is a reserved register (which prevents the register allocator
4566 // from allocating it), resulting in an additional register being
4567 // allocated and an unnecessary move instruction being generated.
Hal Finkelfc096c92014-12-23 22:29:40 +00004568 CallOpc = PPCISD::BCTRL_LOAD_TOC;
4569
Mehdi Amini44ede332015-07-09 02:09:04 +00004570 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Hal Finkelfc096c92014-12-23 22:29:40 +00004571 SDValue StackPtr = DAG.getRegister(PPC::X1, PtrVT);
Eric Christopher736d39e2015-02-13 00:39:36 +00004572 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004573 SDValue TOCOff = DAG.getIntPtrConstant(TOCSaveOffset, dl);
Hal Finkelfc096c92014-12-23 22:29:40 +00004574 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, StackPtr, TOCOff);
4575
4576 // The address needs to go after the chain input but before the flag (or
4577 // any other variadic arguments).
4578 Ops.insert(std::next(Ops.begin()), AddTOC);
Bill Schmidtcea15962013-09-26 17:09:28 +00004579 } else if ((CallOpc == PPCISD::CALL) &&
4580 (!isLocalCall(Callee) ||
Bill Schmidt82f1c772015-02-10 19:09:05 +00004581 DAG.getTarget().getRelocationModel() == Reloc::PIC_))
Roman Divacky76293062012-09-18 16:47:58 +00004582 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004583 CallOpc = PPCISD::CALL_NOP;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004584 }
4585
Craig Topper48d114b2014-04-26 18:35:24 +00004586 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Hal Finkel51861b42012-03-31 14:45:15 +00004587 InFlag = Chain.getValue(1);
4588
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004589 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
4590 DAG.getIntPtrConstant(BytesCalleePops, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00004591 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004592 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004593 InFlag = Chain.getValue(1);
4594
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004595 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
4596 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004597}
4598
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004599SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00004600PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004601 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00004602 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00004603 SDLoc &dl = CLI.DL;
4604 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
4605 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
4606 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00004607 SDValue Chain = CLI.Chain;
4608 SDValue Callee = CLI.Callee;
4609 bool &isTailCall = CLI.IsTailCall;
4610 CallingConv::ID CallConv = CLI.CallConv;
4611 bool isVarArg = CLI.IsVarArg;
Hal Finkel934361a2015-01-14 01:07:51 +00004612 bool IsPatchPoint = CLI.IsPatchPoint;
Hal Finkele2ab0f12015-01-15 21:17:34 +00004613 ImmutableCallSite *CS = CLI.CS;
Justin Holewinskiaa583972012-05-25 16:35:28 +00004614
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00004615 if (isTailCall) {
4616 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64())
4617 isTailCall =
4618 IsEligibleForTailCallOptimization_64SVR4(Callee, CallConv, CS,
4619 isVarArg, Outs, Ins, DAG);
4620 else
4621 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
4622 Ins, DAG);
4623 if (isTailCall) {
4624 ++NumTailCalls;
4625 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
4626 ++NumSiblingCalls;
4627
4628 assert(isa<GlobalAddressSDNode>(Callee) &&
4629 "Callee should be an llvm::Function object.");
4630 DEBUG(
4631 const GlobalValue *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal();
4632 const unsigned Width = 80 - strlen("TCO caller: ")
4633 - strlen(", callee linkage: 0, 0");
4634 dbgs() << "TCO caller: "
4635 << left_justify(DAG.getMachineFunction().getName(), Width)
4636 << ", callee linkage: "
4637 << GV->getVisibility() << ", " << GV->getLinkage() << "\n"
4638 );
4639 }
4640 }
Evan Cheng67a69dd2010-01-27 00:07:07 +00004641
Hal Finkele2ab0f12015-01-15 21:17:34 +00004642 if (!isTailCall && CS && CS->isMustTailCall())
Reid Kleckner5772b772014-04-24 20:14:34 +00004643 report_fatal_error("failed to perform tail call elimination on a call "
4644 "site marked musttail");
4645
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004646 if (Subtarget.isSVR4ABI()) {
4647 if (Subtarget.isPPC64())
Bill Schmidt57d6de52012-10-23 15:51:16 +00004648 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004649 isTailCall, IsPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004650 dl, DAG, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004651 else
4652 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004653 isTailCall, IsPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004654 dl, DAG, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004655 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004656
Bill Schmidt57d6de52012-10-23 15:51:16 +00004657 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004658 isTailCall, IsPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004659 dl, DAG, InVals, CS);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004660}
4661
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004662SDValue PPCTargetLowering::LowerCall_32SVR4(
4663 SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg,
4664 bool isTailCall, bool IsPatchPoint,
4665 const SmallVectorImpl<ISD::OutputArg> &Outs,
4666 const SmallVectorImpl<SDValue> &OutVals,
4667 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
4668 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
4669 ImmutableCallSite *CS) const {
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004670 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004671 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004672
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004673 assert((CallConv == CallingConv::C ||
4674 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004675
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004676 unsigned PtrByteSize = 4;
4677
4678 MachineFunction &MF = DAG.getMachineFunction();
4679
4680 // Mark this function as potentially containing a function that contains a
4681 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4682 // and restoring the callers stack pointer in this functions epilog. This is
4683 // done because by tail calling the called function might overwrite the value
4684 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004685 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4686 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004687 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00004688
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004689 // Count how many bytes are to be pushed on the stack, including the linkage
4690 // area, parameter list area and the part of the local variable space which
4691 // contains copies of aggregates which are passed by value.
4692
4693 // Assign locations to all of the outgoing arguments.
4694 SmallVector<CCValAssign, 16> ArgLocs;
Strahinja Petrovice682b802016-05-09 12:27:39 +00004695 PPCCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
Eric Christopherb5217502014-08-06 18:45:26 +00004696 *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004697
4698 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00004699 CCInfo.AllocateStack(Subtarget.getFrameLowering()->getLinkageSize(),
Ulrich Weigand8658f172014-07-20 23:43:15 +00004700 PtrByteSize);
Strahinja Petrovice682b802016-05-09 12:27:39 +00004701 if (Subtarget.useSoftFloat())
4702 CCInfo.PreAnalyzeCallOperands(Outs);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004703
4704 if (isVarArg) {
4705 // Handle fixed and variable vector arguments differently.
4706 // Fixed vector arguments go into registers as long as registers are
4707 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004708 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00004709
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004710 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00004711 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004712 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004713 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00004714
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004715 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00004716 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
4717 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004718 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00004719 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
4720 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004721 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004722
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004723 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00004724#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00004725 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00004726 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00004727#endif
Craig Toppere73658d2014-04-28 04:05:08 +00004728 llvm_unreachable(nullptr);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004729 }
4730 }
4731 } else {
4732 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00004733 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004734 }
Strahinja Petrovice682b802016-05-09 12:27:39 +00004735 CCInfo.clearWasPPCF128();
NAKAMURA Takumifd921542016-06-20 01:05:15 +00004736
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004737 // Assign locations to all of the outgoing aggregate by value arguments.
4738 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004739 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Eric Christopherb5217502014-08-06 18:45:26 +00004740 ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004741
4742 // Reserve stack space for the allocations in CCInfo.
4743 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
4744
Bill Schmidtef17c142013-02-06 17:33:58 +00004745 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004746
4747 // Size of the linkage area, parameter list area and the part of the local
4748 // space variable where copies of aggregates which are passed by value are
4749 // stored.
4750 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004751
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004752 // Calculate by how many bytes the stack has to be adjusted in case of tail
4753 // call optimization.
4754 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4755
4756 // Adjust the stack pointer for the new arguments...
4757 // These operations are automatically eliminated by the prolog/epilog pass
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004758 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00004759 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004760 SDValue CallSeqStart = Chain;
4761
4762 // Load the return address and frame pointer so it can be moved somewhere else
4763 // later.
4764 SDValue LROp, FPOp;
4765 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
4766 dl);
4767
4768 // Set up a copy of the stack pointer for use loading and storing any
4769 // arguments that may not fit in the registers available for argument
4770 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00004771 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00004772
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004773 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4774 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4775 SmallVector<SDValue, 8> MemOpChains;
4776
Roman Divacky71038e72011-08-30 17:04:16 +00004777 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004778 // Walk the register/memloc assignments, inserting copies/loads.
4779 for (unsigned i = 0, j = 0, e = ArgLocs.size();
4780 i != e;
4781 ++i) {
4782 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004783 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004784 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00004785
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004786 if (Flags.isByVal()) {
4787 // Argument is an aggregate which is passed by value, thus we need to
4788 // create a copy of it in the local variable space of the current stack
4789 // frame (which is the stack frame of the caller) and pass the address of
4790 // this copy to the callee.
4791 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
4792 CCValAssign &ByValVA = ByValArgLocs[j++];
4793 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00004794
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004795 // Memory reserved in the local variable space of the callers stack frame.
4796 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004797
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004798 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00004799 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()),
4800 StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00004801
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004802 // Create a copy of the argument in the local area of the current
4803 // stack frame.
4804 SDValue MemcpyCall =
4805 CreateCopyOfByValArgument(Arg, PtrOff,
4806 CallSeqStart.getNode()->getOperand(0),
4807 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00004808
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004809 // This must go outside the CALLSEQ_START..END.
4810 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004811 CallSeqStart.getNode()->getOperand(1),
4812 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004813 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4814 NewCallSeqStart.getNode());
4815 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00004816
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004817 // Pass the address of the aggregate copy on the stack either in a
4818 // physical register or in the parameter list area of the current stack
4819 // frame to the callee.
4820 Arg = PtrOff;
4821 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004822
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004823 if (VA.isRegLoc()) {
Hal Finkel2a9d3182014-03-06 00:23:33 +00004824 if (Arg.getValueType() == MVT::i1)
4825 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg);
4826
Roman Divacky71038e72011-08-30 17:04:16 +00004827 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004828 // Put argument in a physical register.
4829 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
4830 } else {
4831 // Put argument in the parameter list area of the current stack frame.
4832 assert(VA.isMemLoc());
4833 unsigned LocMemOffset = VA.getLocMemOffset();
4834
4835 if (!isTailCall) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004836 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00004837 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()),
4838 StackPtr, PtrOff);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004839
4840 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00004841 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004842 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004843 } else {
4844 // Calculate and remember argument location.
4845 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
4846 TailCallArguments);
4847 }
4848 }
4849 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004850
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004851 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004852 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Wesley Peck527da1b2010-11-23 03:31:01 +00004853
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004854 // Build a sequence of copy-to-reg nodes chained together with token chain
4855 // and flag operands which copy the outgoing args into the appropriate regs.
4856 SDValue InFlag;
4857 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4858 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4859 RegsToPass[i].second, InFlag);
4860 InFlag = Chain.getValue(1);
4861 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004862
Hal Finkel5ab37802012-08-28 02:10:27 +00004863 // Set CR bit 6 to true if this is a vararg call with floating args passed in
4864 // registers.
4865 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004866 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
4867 SDValue Ops[] = { Chain, InFlag };
4868
Hal Finkel5ab37802012-08-28 02:10:27 +00004869 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00004870 dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1));
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004871
Hal Finkel5ab37802012-08-28 02:10:27 +00004872 InFlag = Chain.getValue(1);
4873 }
4874
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004875 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004876 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
4877 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004878
Hal Finkel965cea52015-07-12 00:37:44 +00004879 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint,
4880 /* unused except on PPC64 ELFv1 */ false, DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004881 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff,
4882 NumBytes, Ins, InVals, CS);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004883}
4884
Bill Schmidt57d6de52012-10-23 15:51:16 +00004885// Copy an argument into memory, being careful to do this outside the
4886// call sequence for the call to which the argument belongs.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004887SDValue PPCTargetLowering::createMemcpyOutsideCallSeq(
4888 SDValue Arg, SDValue PtrOff, SDValue CallSeqStart, ISD::ArgFlagsTy Flags,
4889 SelectionDAG &DAG, const SDLoc &dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004890 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
4891 CallSeqStart.getNode()->getOperand(0),
4892 Flags, DAG, dl);
4893 // The MEMCPY must go outside the CALLSEQ_START..END.
4894 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004895 CallSeqStart.getNode()->getOperand(1),
4896 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004897 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4898 NewCallSeqStart.getNode());
4899 return NewCallSeqStart;
4900}
4901
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004902SDValue PPCTargetLowering::LowerCall_64SVR4(
4903 SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg,
4904 bool isTailCall, bool IsPatchPoint,
4905 const SmallVectorImpl<ISD::OutputArg> &Outs,
4906 const SmallVectorImpl<SDValue> &OutVals,
4907 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
4908 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
4909 ImmutableCallSite *CS) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004910
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004911 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004912 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004913 unsigned NumOps = Outs.size();
Hal Finkel965cea52015-07-12 00:37:44 +00004914 bool hasNest = false;
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00004915 bool IsSibCall = false;
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004916
Mehdi Amini44ede332015-07-09 02:09:04 +00004917 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Bill Schmidt57d6de52012-10-23 15:51:16 +00004918 unsigned PtrByteSize = 8;
4919
4920 MachineFunction &MF = DAG.getMachineFunction();
4921
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00004922 if (isTailCall && !getTargetMachine().Options.GuaranteedTailCallOpt)
4923 IsSibCall = true;
4924
Bill Schmidt57d6de52012-10-23 15:51:16 +00004925 // Mark this function as potentially containing a function that contains a
4926 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4927 // and restoring the callers stack pointer in this functions epilog. This is
4928 // done because by tail calling the called function might overwrite the value
4929 // in this function's (MF) stack pointer stack slot 0(SP).
4930 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4931 CallConv == CallingConv::Fast)
4932 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4933
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004934 assert(!(CallConv == CallingConv::Fast && isVarArg) &&
4935 "fastcc not supported on varargs functions");
4936
Bill Schmidt57d6de52012-10-23 15:51:16 +00004937 // Count how many bytes are to be pushed on the stack, including the linkage
Ulrich Weigand8658f172014-07-20 23:43:15 +00004938 // area, and parameter passing area. On ELFv1, the linkage area is 48 bytes
4939 // reserved space for [SP][CR][LR][2 x unused][TOC]; on ELFv2, the linkage
4940 // area is 32 bytes reserved space for [SP][CR][LR][TOC].
Eric Christophera4ae2132015-02-13 22:22:57 +00004941 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004942 unsigned NumBytes = LinkageSize;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004943 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Hal Finkelc93a9a22015-02-25 01:06:45 +00004944 unsigned &QFPR_idx = FPR_idx;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004945
4946 static const MCPhysReg GPR[] = {
4947 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4948 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4949 };
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004950 static const MCPhysReg VR[] = {
4951 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4952 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4953 };
4954 static const MCPhysReg VSRH[] = {
4955 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
4956 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
4957 };
4958
4959 const unsigned NumGPRs = array_lengthof(GPR);
4960 const unsigned NumFPRs = 13;
4961 const unsigned NumVRs = array_lengthof(VR);
Hal Finkelc93a9a22015-02-25 01:06:45 +00004962 const unsigned NumQFPRs = NumFPRs;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004963
4964 // When using the fast calling convention, we don't provide backing for
4965 // arguments that will be in registers.
4966 unsigned NumGPRsUsed = 0, NumFPRsUsed = 0, NumVRsUsed = 0;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004967
4968 // Add up all the space actually used.
4969 for (unsigned i = 0; i != NumOps; ++i) {
4970 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4971 EVT ArgVT = Outs[i].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004972 EVT OrigVT = Outs[i].ArgVT;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004973
Hal Finkel965cea52015-07-12 00:37:44 +00004974 if (Flags.isNest())
4975 continue;
4976
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004977 if (CallConv == CallingConv::Fast) {
4978 if (Flags.isByVal())
4979 NumGPRsUsed += (Flags.getByValSize()+7)/8;
4980 else
4981 switch (ArgVT.getSimpleVT().SimpleTy) {
4982 default: llvm_unreachable("Unexpected ValueType for argument!");
4983 case MVT::i1:
4984 case MVT::i32:
4985 case MVT::i64:
4986 if (++NumGPRsUsed <= NumGPRs)
4987 continue;
4988 break;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004989 case MVT::v4i32:
4990 case MVT::v8i16:
4991 case MVT::v16i8:
4992 case MVT::v2f64:
4993 case MVT::v2i64:
Kit Bartond4eb73c2015-05-05 16:10:44 +00004994 case MVT::v1i128:
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004995 if (++NumVRsUsed <= NumVRs)
4996 continue;
4997 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00004998 case MVT::v4f32:
NAKAMURA Takumi84965032015-09-22 11:14:12 +00004999 // When using QPX, this is handled like a FP register, otherwise, it
5000 // is an Altivec register.
Hal Finkelc93a9a22015-02-25 01:06:45 +00005001 if (Subtarget.hasQPX()) {
5002 if (++NumFPRsUsed <= NumFPRs)
5003 continue;
5004 } else {
5005 if (++NumVRsUsed <= NumVRs)
5006 continue;
5007 }
5008 break;
5009 case MVT::f32:
5010 case MVT::f64:
5011 case MVT::v4f64: // QPX
5012 case MVT::v4i1: // QPX
5013 if (++NumFPRsUsed <= NumFPRs)
5014 continue;
5015 break;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005016 }
5017 }
5018
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005019 /* Respect alignment of argument on the stack. */
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005020 unsigned Align =
5021 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005022 NumBytes = ((NumBytes + Align - 1) / Align) * Align;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005023
5024 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005025 if (Flags.isInConsecutiveRegsLast())
5026 NumBytes = ((NumBytes + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005027 }
5028
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005029 unsigned NumBytesActuallyUsed = NumBytes;
5030
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005031 // The prolog code of the callee may store up to 8 GPR argument registers to
5032 // the stack, allowing va_start to index over them in memory if its varargs.
5033 // Because we cannot tell if this is needed on the caller side, we have to
5034 // conservatively assume that it is needed. As such, make sure we have at
5035 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00005036 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005037 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005038
5039 // Tail call needs the stack to be aligned.
5040 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5041 CallConv == CallingConv::Fast)
Eric Christophercccae792015-01-30 22:02:31 +00005042 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005043
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005044 int SPDiff = 0;
5045
Bill Schmidt57d6de52012-10-23 15:51:16 +00005046 // Calculate by how many bytes the stack has to be adjusted in case of tail
5047 // call optimization.
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005048 if (!IsSibCall)
5049 SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005050
5051 // To protect arguments on the stack from being clobbered in a tail call,
5052 // force all the loads to happen before doing any other lowering.
5053 if (isTailCall)
5054 Chain = DAG.getStackArgumentTokenFactor(Chain);
5055
5056 // Adjust the stack pointer for the new arguments...
5057 // These operations are automatically eliminated by the prolog/epilog pass
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005058 if (!IsSibCall)
5059 Chain = DAG.getCALLSEQ_START(Chain,
5060 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005061 SDValue CallSeqStart = Chain;
5062
5063 // Load the return address and frame pointer so it can be move somewhere else
5064 // later.
5065 SDValue LROp, FPOp;
5066 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
5067 dl);
5068
5069 // Set up a copy of the stack pointer for use loading and storing any
5070 // arguments that may not fit in the registers available for argument
5071 // passing.
5072 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
5073
5074 // Figure out which arguments are going to go in registers, and which in
5075 // memory. Also, if this is a vararg function, floating point operations
5076 // must be stored to our stack, and loaded into integer regs as well, if
5077 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005078 unsigned ArgOffset = LinkageSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005079
5080 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
5081 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
5082
5083 SmallVector<SDValue, 8> MemOpChains;
5084 for (unsigned i = 0; i != NumOps; ++i) {
5085 SDValue Arg = OutVals[i];
5086 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005087 EVT ArgVT = Outs[i].VT;
5088 EVT OrigVT = Outs[i].ArgVT;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005089
5090 // PtrOff will be used to store the current argument to the stack if a
5091 // register cannot be found for it.
5092 SDValue PtrOff;
5093
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005094 // We re-align the argument offset for each argument, except when using the
5095 // fast calling convention, when we need to make sure we do that only when
5096 // we'll actually use a stack slot.
5097 auto ComputePtrOff = [&]() {
5098 /* Respect alignment of argument on the stack. */
5099 unsigned Align =
5100 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
5101 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005102
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005103 PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType());
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005104
5105 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
5106 };
5107
5108 if (CallConv != CallingConv::Fast) {
5109 ComputePtrOff();
5110
5111 /* Compute GPR index associated with argument offset. */
5112 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
5113 GPR_idx = std::min(GPR_idx, NumGPRs);
5114 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005115
5116 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00005117 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005118 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
5119 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
5120 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
5121 }
5122
5123 // FIXME memcpy is used way more than necessary. Correctness first.
5124 // Note: "by value" is code for passing a structure by value, not
5125 // basic types.
5126 if (Flags.isByVal()) {
5127 // Note: Size includes alignment padding, so
5128 // struct x { short a; char b; }
5129 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
5130 // These are the proper values we need for right-justifying the
5131 // aggregate in a parameter register.
5132 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00005133
5134 // An empty aggregate parameter takes up no storage and no
5135 // registers.
5136 if (Size == 0)
5137 continue;
5138
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005139 if (CallConv == CallingConv::Fast)
5140 ComputePtrOff();
5141
Bill Schmidt57d6de52012-10-23 15:51:16 +00005142 // All aggregates smaller than 8 bytes must be passed right-justified.
5143 if (Size==1 || Size==2 || Size==4) {
5144 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
5145 if (GPR_idx != NumGPRs) {
5146 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
5147 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00005148 false, false, false, 0);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005149 MemOpChains.push_back(Load.getValue(1));
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005150 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005151
5152 ArgOffset += PtrByteSize;
5153 continue;
5154 }
5155 }
5156
5157 if (GPR_idx == NumGPRs && Size < 8) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00005158 SDValue AddPtr = PtrOff;
5159 if (!isLittleEndian) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005160 SDValue Const = DAG.getConstant(PtrByteSize - Size, dl,
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00005161 PtrOff.getValueType());
5162 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
5163 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005164 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
5165 CallSeqStart,
5166 Flags, DAG, dl);
5167 ArgOffset += PtrByteSize;
5168 continue;
5169 }
5170 // Copy entire object into memory. There are cases where gcc-generated
5171 // code assumes it is there, even if it could be put entirely into
5172 // registers. (This is not what the doc says.)
5173
5174 // FIXME: The above statement is likely due to a misunderstanding of the
5175 // documents. All arguments must be copied into the parameter area BY
5176 // THE CALLEE in the event that the callee takes the address of any
5177 // formal argument. That has not yet been implemented. However, it is
5178 // reasonable to use the stack area as a staging area for the register
5179 // load.
5180
5181 // Skip this for small aggregates, as we will use the same slot for a
5182 // right-justified copy, below.
5183 if (Size >= 8)
5184 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
5185 CallSeqStart,
5186 Flags, DAG, dl);
5187
5188 // When a register is available, pass a small aggregate right-justified.
5189 if (Size < 8 && GPR_idx != NumGPRs) {
5190 // The easiest way to get this right-justified in a register
5191 // is to copy the structure into the rightmost portion of a
5192 // local variable slot, then load the whole slot into the
5193 // register.
5194 // FIXME: The memcpy seems to produce pretty awful code for
5195 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00005196 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00005197 // parameter save area instead of a new local variable.
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00005198 SDValue AddPtr = PtrOff;
5199 if (!isLittleEndian) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005200 SDValue Const = DAG.getConstant(8 - Size, dl, PtrOff.getValueType());
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00005201 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
5202 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005203 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
5204 CallSeqStart,
5205 Flags, DAG, dl);
5206
5207 // Load the slot into the register.
5208 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
5209 MachinePointerInfo(),
5210 false, false, false, 0);
5211 MemOpChains.push_back(Load.getValue(1));
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005212 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005213
5214 // Done with this argument.
5215 ArgOffset += PtrByteSize;
5216 continue;
5217 }
5218
5219 // For aggregates larger than PtrByteSize, copy the pieces of the
5220 // object that fit into registers from the parameter save area.
5221 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005222 SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType());
Bill Schmidt57d6de52012-10-23 15:51:16 +00005223 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
5224 if (GPR_idx != NumGPRs) {
5225 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
5226 MachinePointerInfo(),
5227 false, false, false, 0);
5228 MemOpChains.push_back(Load.getValue(1));
5229 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5230 ArgOffset += PtrByteSize;
5231 } else {
5232 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
5233 break;
5234 }
5235 }
5236 continue;
5237 }
5238
Craig Topper56710102013-08-15 02:33:50 +00005239 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005240 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00005241 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00005242 case MVT::i32:
5243 case MVT::i64:
Hal Finkel965cea52015-07-12 00:37:44 +00005244 if (Flags.isNest()) {
5245 // The 'nest' parameter, if any, is passed in R11.
5246 RegsToPass.push_back(std::make_pair(PPC::X11, Arg));
5247 hasNest = true;
5248 break;
5249 }
5250
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005251 // These can be scalar arguments or elements of an integer array type
5252 // passed directly. Clang may use those instead of "byval" aggregate
5253 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidt57d6de52012-10-23 15:51:16 +00005254 if (GPR_idx != NumGPRs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005255 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005256 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005257 if (CallConv == CallingConv::Fast)
5258 ComputePtrOff();
5259
Bill Schmidt57d6de52012-10-23 15:51:16 +00005260 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5261 true, isTailCall, false, MemOpChains,
5262 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005263 if (CallConv == CallingConv::Fast)
5264 ArgOffset += PtrByteSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005265 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005266 if (CallConv != CallingConv::Fast)
5267 ArgOffset += PtrByteSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005268 break;
5269 case MVT::f32:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005270 case MVT::f64: {
5271 // These can be scalar arguments or elements of a float array type
5272 // passed directly. The latter are used to implement ELFv2 homogenous
5273 // float aggregates.
5274
5275 // Named arguments go into FPRs first, and once they overflow, the
5276 // remaining arguments go into GPRs and then the parameter save area.
5277 // Unnamed arguments for vararg functions always go to GPRs and
5278 // then the parameter save area. For now, put all arguments to vararg
5279 // routines always in both locations (FPR *and* GPR or stack slot).
5280 bool NeedGPROrStack = isVarArg || FPR_idx == NumFPRs;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005281 bool NeededLoad = false;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005282
5283 // First load the argument into the next available FPR.
5284 if (FPR_idx != NumFPRs)
Bill Schmidt57d6de52012-10-23 15:51:16 +00005285 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
5286
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005287 // Next, load the argument into GPR or stack slot if needed.
5288 if (!NeedGPROrStack)
5289 ;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005290 else if (GPR_idx != NumGPRs && CallConv != CallingConv::Fast) {
Hal Finkel8ea446b2015-01-18 14:31:10 +00005291 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8
5292 // once we support fp <-> gpr moves.
5293
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005294 // In the non-vararg case, this can only ever happen in the
5295 // presence of f32 array types, since otherwise we never run
5296 // out of FPRs before running out of GPRs.
5297 SDValue ArgVal;
Bill Schmidtbd4ac262012-10-29 21:18:16 +00005298
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005299 // Double values are always passed in a single GPR.
5300 if (Arg.getValueType() != MVT::f32) {
5301 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005302
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005303 // Non-array float values are extended and passed in a GPR.
5304 } else if (!Flags.isInConsecutiveRegs()) {
5305 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
5306 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
5307
5308 // If we have an array of floats, we collect every odd element
5309 // together with its predecessor into one GPR.
5310 } else if (ArgOffset % PtrByteSize != 0) {
5311 SDValue Lo, Hi;
5312 Lo = DAG.getNode(ISD::BITCAST, dl, MVT::i32, OutVals[i - 1]);
5313 Hi = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
5314 if (!isLittleEndian)
5315 std::swap(Lo, Hi);
5316 ArgVal = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
5317
5318 // The final element, if even, goes into the first half of a GPR.
5319 } else if (Flags.isInConsecutiveRegsLast()) {
5320 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
5321 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
5322 if (!isLittleEndian)
5323 ArgVal = DAG.getNode(ISD::SHL, dl, MVT::i64, ArgVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005324 DAG.getConstant(32, dl, MVT::i32));
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005325
5326 // Non-final even elements are skipped; they will be handled
5327 // together the with subsequent argument on the next go-around.
5328 } else
5329 ArgVal = SDValue();
5330
5331 if (ArgVal.getNode())
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005332 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], ArgVal));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005333 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005334 if (CallConv == CallingConv::Fast)
5335 ComputePtrOff();
5336
Bill Schmidt57d6de52012-10-23 15:51:16 +00005337 // Single-precision floating-point values are mapped to the
5338 // second (rightmost) word of the stack doubleword.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005339 if (Arg.getValueType() == MVT::f32 &&
5340 !isLittleEndian && !Flags.isInConsecutiveRegs()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005341 SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType());
Bill Schmidt57d6de52012-10-23 15:51:16 +00005342 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
5343 }
5344
5345 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5346 true, isTailCall, false, MemOpChains,
5347 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005348
5349 NeededLoad = true;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005350 }
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005351 // When passing an array of floats, the array occupies consecutive
5352 // space in the argument area; only round up to the next doubleword
5353 // at the end of the array. Otherwise, each float takes 8 bytes.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005354 if (CallConv != CallingConv::Fast || NeededLoad) {
5355 ArgOffset += (Arg.getValueType() == MVT::f32 &&
5356 Flags.isInConsecutiveRegs()) ? 4 : 8;
5357 if (Flags.isInConsecutiveRegsLast())
5358 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
5359 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005360 break;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005361 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005362 case MVT::v4f32:
5363 case MVT::v4i32:
5364 case MVT::v8i16:
5365 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00005366 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00005367 case MVT::v2i64:
Kit Bartond4eb73c2015-05-05 16:10:44 +00005368 case MVT::v1i128:
Hal Finkelc93a9a22015-02-25 01:06:45 +00005369 if (!Subtarget.hasQPX()) {
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005370 // These can be scalar arguments or elements of a vector array type
5371 // passed directly. The latter are used to implement ELFv2 homogenous
5372 // vector aggregates.
5373
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00005374 // For a varargs call, named arguments go into VRs or on the stack as
5375 // usual; unnamed arguments always go to the stack or the corresponding
5376 // GPRs when within range. For now, we always put the value in both
5377 // locations (or even all three).
Bill Schmidt57d6de52012-10-23 15:51:16 +00005378 if (isVarArg) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005379 // We could elide this store in the case where the object fits
5380 // entirely in R registers. Maybe later.
Bill Schmidt57d6de52012-10-23 15:51:16 +00005381 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5382 MachinePointerInfo(), false, false, 0);
5383 MemOpChains.push_back(Store);
5384 if (VR_idx != NumVRs) {
5385 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
5386 MachinePointerInfo(),
5387 false, false, false, 0);
5388 MemOpChains.push_back(Load.getValue(1));
Hal Finkel7811c612014-03-28 19:58:11 +00005389
5390 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
5391 Arg.getSimpleValueType() == MVT::v2i64) ?
5392 VSRH[VR_idx] : VR[VR_idx];
5393 ++VR_idx;
5394
5395 RegsToPass.push_back(std::make_pair(VReg, Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005396 }
5397 ArgOffset += 16;
5398 for (unsigned i=0; i<16; i+=PtrByteSize) {
5399 if (GPR_idx == NumGPRs)
5400 break;
5401 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005402 DAG.getConstant(i, dl, PtrVT));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005403 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
5404 false, false, false, 0);
5405 MemOpChains.push_back(Load.getValue(1));
5406 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5407 }
5408 break;
5409 }
5410
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00005411 // Non-varargs Altivec params go into VRs or on the stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00005412 if (VR_idx != NumVRs) {
Hal Finkel7811c612014-03-28 19:58:11 +00005413 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
5414 Arg.getSimpleValueType() == MVT::v2i64) ?
5415 VSRH[VR_idx] : VR[VR_idx];
5416 ++VR_idx;
5417
5418 RegsToPass.push_back(std::make_pair(VReg, Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005419 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005420 if (CallConv == CallingConv::Fast)
5421 ComputePtrOff();
5422
Bill Schmidt57d6de52012-10-23 15:51:16 +00005423 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5424 true, isTailCall, true, MemOpChains,
5425 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005426 if (CallConv == CallingConv::Fast)
5427 ArgOffset += 16;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005428 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005429
5430 if (CallConv != CallingConv::Fast)
5431 ArgOffset += 16;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005432 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00005433 } // not QPX
5434
5435 assert(Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32 &&
5436 "Invalid QPX parameter type");
5437
5438 /* fall through */
5439 case MVT::v4f64:
5440 case MVT::v4i1: {
5441 bool IsF32 = Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32;
5442 if (isVarArg) {
5443 // We could elide this store in the case where the object fits
5444 // entirely in R registers. Maybe later.
5445 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5446 MachinePointerInfo(), false, false, 0);
5447 MemOpChains.push_back(Store);
5448 if (QFPR_idx != NumQFPRs) {
5449 SDValue Load = DAG.getLoad(IsF32 ? MVT::v4f32 : MVT::v4f64, dl,
5450 Store, PtrOff, MachinePointerInfo(),
5451 false, false, false, 0);
5452 MemOpChains.push_back(Load.getValue(1));
5453 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Load));
5454 }
5455 ArgOffset += (IsF32 ? 16 : 32);
Aaron Ballman70c27de2015-02-25 13:02:23 +00005456 for (unsigned i = 0; i < (IsF32 ? 16U : 32U); i += PtrByteSize) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00005457 if (GPR_idx == NumGPRs)
5458 break;
5459 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005460 DAG.getConstant(i, dl, PtrVT));
Hal Finkelc93a9a22015-02-25 01:06:45 +00005461 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
5462 false, false, false, 0);
5463 MemOpChains.push_back(Load.getValue(1));
5464 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5465 }
5466 break;
5467 }
5468
5469 // Non-varargs QPX params go into registers or on the stack.
5470 if (QFPR_idx != NumQFPRs) {
5471 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Arg));
5472 } else {
5473 if (CallConv == CallingConv::Fast)
5474 ComputePtrOff();
5475
5476 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5477 true, isTailCall, true, MemOpChains,
5478 TailCallArguments, dl);
5479 if (CallConv == CallingConv::Fast)
5480 ArgOffset += (IsF32 ? 16 : 32);
5481 }
5482
5483 if (CallConv != CallingConv::Fast)
5484 ArgOffset += (IsF32 ? 16 : 32);
5485 break;
5486 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005487 }
5488 }
5489
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005490 assert(NumBytesActuallyUsed == ArgOffset);
Ulrich Weigandde8641b2014-07-07 19:39:44 +00005491 (void)NumBytesActuallyUsed;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005492
Bill Schmidt57d6de52012-10-23 15:51:16 +00005493 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00005494 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005495
5496 // Check if this is an indirect call (MTCTR/BCTRL).
5497 // See PrepareCall() for more information about calls through function
5498 // pointers in the 64-bit SVR4 ABI.
Hal Finkel934361a2015-01-14 01:07:51 +00005499 if (!isTailCall && !IsPatchPoint &&
Hal Finkel87deb0b2015-01-12 04:34:47 +00005500 !isFunctionGlobalAddress(Callee) &&
5501 !isa<ExternalSymbolSDNode>(Callee)) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005502 // Load r2 into a virtual register and store it to the TOC save area.
Hal Finkele6698d52015-02-01 15:03:28 +00005503 setUsesTOCBasePtr(DAG);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005504 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
5505 // TOC save area offset.
Eric Christopher736d39e2015-02-13 00:39:36 +00005506 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005507 SDValue PtrOff = DAG.getIntPtrConstant(TOCSaveOffset, dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005508 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00005509 Chain = DAG.getStore(
5510 Val.getValue(1), dl, Val, AddPtr,
5511 MachinePointerInfo::getStack(DAG.getMachineFunction(), TOCSaveOffset),
5512 false, false, 0);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00005513 // In the ELFv2 ABI, R12 must contain the address of an indirect callee.
5514 // This does not mean the MTCTR instruction must use R12; it's easier
5515 // to model this as an extra parameter, so do that.
Hal Finkel934361a2015-01-14 01:07:51 +00005516 if (isELFv2ABI && !IsPatchPoint)
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00005517 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005518 }
5519
5520 // Build a sequence of copy-to-reg nodes chained together with token chain
5521 // and flag operands which copy the outgoing args into the appropriate regs.
5522 SDValue InFlag;
5523 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
5524 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
5525 RegsToPass[i].second, InFlag);
5526 InFlag = Chain.getValue(1);
5527 }
5528
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005529 if (isTailCall && !IsSibCall)
Bill Schmidt57d6de52012-10-23 15:51:16 +00005530 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
5531 FPOp, true, TailCallArguments);
5532
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005533 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint, hasNest,
5534 DAG, RegsToPass, InFlag, Chain, CallSeqStart, Callee,
5535 SPDiff, NumBytes, Ins, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005536}
5537
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005538SDValue PPCTargetLowering::LowerCall_Darwin(
5539 SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg,
5540 bool isTailCall, bool IsPatchPoint,
5541 const SmallVectorImpl<ISD::OutputArg> &Outs,
5542 const SmallVectorImpl<SDValue> &OutVals,
5543 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
5544 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
5545 ImmutableCallSite *CS) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005546
5547 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005548
Mehdi Amini44ede332015-07-09 02:09:04 +00005549 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Owen Anderson9f944592009-08-11 20:47:22 +00005550 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005551 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005552
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005553 MachineFunction &MF = DAG.getMachineFunction();
5554
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005555 // Mark this function as potentially containing a function that contains a
5556 // tail call. As a consequence the frame pointer will be used for dynamicalloc
5557 // and restoring the callers stack pointer in this functions epilog. This is
5558 // done because by tail calling the called function might overwrite the value
5559 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00005560 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5561 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005562 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
5563
Chris Lattneraa40ec12006-05-16 22:56:08 +00005564 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00005565 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00005566 // prereserved space for [SP][CR][LR][3 x unused].
Eric Christophera4ae2132015-02-13 22:22:57 +00005567 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005568 unsigned NumBytes = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005569
5570 // Add up all the space actually used.
5571 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
5572 // they all go in registers, but we must reserve stack space for them for
5573 // possible use by the caller. In varargs or 64-bit calls, parameters are
5574 // assigned stack space in order, with padding so Altivec parameters are
5575 // 16-byte aligned.
5576 unsigned nAltivecParamsAtEnd = 0;
5577 for (unsigned i = 0; i != NumOps; ++i) {
5578 ISD::ArgFlagsTy Flags = Outs[i].Flags;
5579 EVT ArgVT = Outs[i].VT;
5580 // Varargs Altivec parameters are padded to a 16 byte boundary.
5581 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
5582 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
5583 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64) {
5584 if (!isVarArg && !isPPC64) {
5585 // Non-varargs Altivec parameters go after all the non-Altivec
5586 // parameters; handle those later so we know how much padding we need.
5587 nAltivecParamsAtEnd++;
5588 continue;
5589 }
5590 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
5591 NumBytes = ((NumBytes+15)/16)*16;
5592 }
5593 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
5594 }
5595
5596 // Allow for Altivec parameters at the end, if needed.
5597 if (nAltivecParamsAtEnd) {
5598 NumBytes = ((NumBytes+15)/16)*16;
5599 NumBytes += 16*nAltivecParamsAtEnd;
5600 }
5601
5602 // The prolog code of the callee may store up to 8 GPR argument registers to
5603 // the stack, allowing va_start to index over them in memory if its varargs.
5604 // Because we cannot tell if this is needed on the caller side, we have to
5605 // conservatively assume that it is needed. As such, make sure we have at
5606 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005607 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005608
5609 // Tail call needs the stack to be aligned.
5610 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5611 CallConv == CallingConv::Fast)
Eric Christophercccae792015-01-30 22:02:31 +00005612 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005613
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005614 // Calculate by how many bytes the stack has to be adjusted in case of tail
5615 // call optimization.
5616 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005617
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005618 // To protect arguments on the stack from being clobbered in a tail call,
5619 // force all the loads to happen before doing any other lowering.
5620 if (isTailCall)
5621 Chain = DAG.getStackArgumentTokenFactor(Chain);
5622
Chris Lattnerb7552a82006-05-17 00:15:40 +00005623 // Adjust the stack pointer for the new arguments...
5624 // These operations are automatically eliminated by the prolog/epilog pass
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005625 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00005626 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005627 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005628
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005629 // Load the return address and frame pointer so it can be move somewhere else
5630 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005631 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00005632 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
5633 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005634
Chris Lattnerb7552a82006-05-17 00:15:40 +00005635 // Set up a copy of the stack pointer for use loading and storing any
5636 // arguments that may not fit in the registers available for argument
5637 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005638 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005639 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00005640 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005641 else
Owen Anderson9f944592009-08-11 20:47:22 +00005642 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005643
Chris Lattnerb7552a82006-05-17 00:15:40 +00005644 // Figure out which arguments are going to go in registers, and which in
5645 // memory. Also, if this is a vararg function, floating point operations
5646 // must be stored to our stack, and loaded into integer regs as well, if
5647 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005648 unsigned ArgOffset = LinkageSize;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005649 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005650
Craig Topper840beec2014-04-04 05:16:06 +00005651 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005652 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
5653 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
5654 };
Craig Topper840beec2014-04-04 05:16:06 +00005655 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00005656 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
5657 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
5658 };
Craig Topper840beec2014-04-04 05:16:06 +00005659 static const MCPhysReg VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005660 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
5661 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
5662 };
Owen Andersone2f23a32007-09-07 04:06:50 +00005663 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005664 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00005665 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005666
Craig Topper840beec2014-04-04 05:16:06 +00005667 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005668
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005669 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005670 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
5671
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005672 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00005673 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005674 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005675 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00005676
Chris Lattnerb7552a82006-05-17 00:15:40 +00005677 // PtrOff will be used to store the current argument to the stack if a
5678 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005679 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005680
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005681 PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00005682
Dale Johannesen679073b2009-02-04 02:34:38 +00005683 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005684
5685 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00005686 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00005687 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
5688 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00005689 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005690 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00005691
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005692 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00005693 // Note: "by value" is code for passing a structure by value, not
5694 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00005695 if (Flags.isByVal()) {
5696 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00005697 // Very small objects are passed right-justified. Everything else is
5698 // passed left-justified.
5699 if (Size==1 || Size==2) {
5700 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005701 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00005702 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00005703 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00005704 false, false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005705 MemOpChains.push_back(Load.getValue(1));
5706 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005707
5708 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005709 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005710 SDValue Const = DAG.getConstant(PtrByteSize - Size, dl,
Bill Schmidt48081ca2012-10-16 13:30:53 +00005711 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005712 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005713 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
5714 CallSeqStart,
5715 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005716 ArgOffset += PtrByteSize;
5717 }
5718 continue;
5719 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00005720 // Copy entire object into memory. There are cases where gcc-generated
5721 // code assumes it is there, even if it could be put entirely into
5722 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00005723 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
5724 CallSeqStart,
5725 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00005726
5727 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
5728 // copy the pieces of the object that fit into registers from the
5729 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00005730 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005731 SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005732 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00005733 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00005734 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
5735 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005736 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00005737 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00005738 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005739 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00005740 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00005741 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005742 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00005743 }
5744 }
5745 continue;
5746 }
5747
Craig Topper56710102013-08-15 02:33:50 +00005748 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005749 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00005750 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00005751 case MVT::i32:
5752 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005753 if (GPR_idx != NumGPRs) {
Hal Finkel7f908e82014-03-06 00:45:19 +00005754 if (Arg.getValueType() == MVT::i1)
5755 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg);
5756
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005757 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00005758 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005759 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5760 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005761 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00005762 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005763 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00005764 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005765 case MVT::f32:
5766 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005767 if (FPR_idx != NumFPRs) {
5768 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
5769
Chris Lattnerb7552a82006-05-17 00:15:40 +00005770 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00005771 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5772 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005773 MemOpChains.push_back(Store);
5774
Chris Lattnerb7552a82006-05-17 00:15:40 +00005775 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005776 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00005777 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00005778 MachinePointerInfo(), false, false,
5779 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005780 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005781 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00005782 }
Owen Anderson9f944592009-08-11 20:47:22 +00005783 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005784 SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005785 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00005786 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
5787 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005788 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005789 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005790 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00005791 }
5792 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00005793 // If we have any FPRs remaining, we may also have GPRs remaining.
5794 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
5795 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005796 if (GPR_idx != NumGPRs)
5797 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00005798 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005799 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
5800 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00005801 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005802 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005803 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5804 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005805 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005806 if (isPPC64)
5807 ArgOffset += 8;
5808 else
Owen Anderson9f944592009-08-11 20:47:22 +00005809 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00005810 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005811 case MVT::v4f32:
5812 case MVT::v4i32:
5813 case MVT::v8i16:
5814 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00005815 if (isVarArg) {
5816 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00005817 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00005818 // V registers; in fact gcc does this only for arguments that are
5819 // prototyped, not for those that match the ... We do it for all
5820 // arguments, seems to work.
5821 while (ArgOffset % 16 !=0) {
5822 ArgOffset += PtrByteSize;
5823 if (GPR_idx != NumGPRs)
5824 GPR_idx++;
5825 }
5826 // We could elide this store in the case where the object fits
5827 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005828 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005829 DAG.getConstant(ArgOffset, dl, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00005830 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5831 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005832 MemOpChains.push_back(Store);
5833 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00005834 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00005835 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005836 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005837 MemOpChains.push_back(Load.getValue(1));
5838 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
5839 }
5840 ArgOffset += 16;
5841 for (unsigned i=0; i<16; i+=PtrByteSize) {
5842 if (GPR_idx == NumGPRs)
5843 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00005844 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005845 DAG.getConstant(i, dl, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00005846 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005847 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005848 MemOpChains.push_back(Load.getValue(1));
5849 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5850 }
5851 break;
5852 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005853
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005854 // Non-varargs Altivec params generally go in registers, but have
5855 // stack space allocated at the end.
5856 if (VR_idx != NumVRs) {
5857 // Doesn't have GPR space allocated.
5858 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
5859 } else if (nAltivecParamsAtEnd==0) {
5860 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005861 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5862 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005863 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005864 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00005865 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00005866 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00005867 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00005868 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005869 // If all Altivec parameters fit in registers, as they usually do,
5870 // they get stack space following the non-Altivec parameters. We
5871 // don't track this here because nobody below needs it.
5872 // If there are more Altivec parameters than fit in registers emit
5873 // the stores here.
5874 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
5875 unsigned j = 0;
5876 // Offset is aligned; skip 1st 12 params which go in V registers.
5877 ArgOffset = ((ArgOffset+15)/16)*16;
5878 ArgOffset += 12*16;
5879 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005880 SDValue Arg = OutVals[i];
5881 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00005882 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
5883 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005884 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005885 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005886 // We are emitting Altivec params in order.
5887 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5888 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005889 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005890 ArgOffset += 16;
5891 }
5892 }
5893 }
5894 }
5895
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005896 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00005897 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005898
Dale Johannesen90eab672010-03-09 20:15:42 +00005899 // On Darwin, R12 must contain the address of an indirect callee. This does
5900 // not mean the MTCTR instruction must use R12; it's easier to model this as
5901 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00005902 if (!isTailCall &&
Hal Finkel87deb0b2015-01-12 04:34:47 +00005903 !isFunctionGlobalAddress(Callee) &&
5904 !isa<ExternalSymbolSDNode>(Callee) &&
Dale Johannesen90eab672010-03-09 20:15:42 +00005905 !isBLACompatibleAddress(Callee, DAG))
5906 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
5907 PPC::R12), Callee));
5908
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005909 // Build a sequence of copy-to-reg nodes chained together with token chain
5910 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005911 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005912 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00005913 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00005914 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005915 InFlag = Chain.getValue(1);
5916 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005917
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00005918 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005919 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
5920 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005921
Hal Finkel965cea52015-07-12 00:37:44 +00005922 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint,
5923 /* unused except on PPC64 ELFv1 */ false, DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00005924 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff,
5925 NumBytes, Ins, InVals, CS);
Chris Lattneraa40ec12006-05-16 22:56:08 +00005926}
5927
Hal Finkel450128a2011-10-14 19:51:36 +00005928bool
5929PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
5930 MachineFunction &MF, bool isVarArg,
5931 const SmallVectorImpl<ISD::OutputArg> &Outs,
5932 LLVMContext &Context) const {
5933 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00005934 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Hal Finkel450128a2011-10-14 19:51:36 +00005935 return CCInfo.CheckReturn(Outs, RetCC_PPC);
5936}
5937
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005938SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005939PPCTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
5940 bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005941 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005942 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005943 const SDLoc &dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005944
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005945 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00005946 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
5947 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005948 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005949
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005950 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005951 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005952
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005953 // Copy the result values into the output registers.
5954 for (unsigned i = 0; i != RVLocs.size(); ++i) {
5955 CCValAssign &VA = RVLocs[i];
5956 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00005957
5958 SDValue Arg = OutVals[i];
5959
5960 switch (VA.getLocInfo()) {
5961 default: llvm_unreachable("Unknown loc info!");
5962 case CCValAssign::Full: break;
5963 case CCValAssign::AExt:
5964 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
5965 break;
5966 case CCValAssign::ZExt:
5967 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
5968 break;
5969 case CCValAssign::SExt:
5970 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
5971 break;
5972 }
5973
5974 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005975 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005976 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005977 }
5978
Chuang-Yu Cheng98c18942016-04-08 12:04:32 +00005979 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo();
5980 const MCPhysReg *I =
5981 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
5982 if (I) {
5983 for (; *I; ++I) {
5984
5985 if (PPC::G8RCRegClass.contains(*I))
5986 RetOps.push_back(DAG.getRegister(*I, MVT::i64));
5987 else if (PPC::F8RCRegClass.contains(*I))
5988 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64)));
5989 else if (PPC::CRRCRegClass.contains(*I))
5990 RetOps.push_back(DAG.getRegister(*I, MVT::i1));
5991 else if (PPC::VRRCRegClass.contains(*I))
5992 RetOps.push_back(DAG.getRegister(*I, MVT::Other));
5993 else
5994 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
5995 }
5996 }
5997
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005998 RetOps[0] = Chain; // Update chain.
5999
6000 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006001 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00006002 RetOps.push_back(Flag);
6003
Craig Topper48d114b2014-04-26 18:35:24 +00006004 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps);
Chris Lattner4211ca92006-04-14 06:01:58 +00006005}
6006
Yury Gribovd7dbb662015-12-01 11:40:55 +00006007SDValue PPCTargetLowering::LowerGET_DYNAMIC_AREA_OFFSET(
6008 SDValue Op, SelectionDAG &DAG, const PPCSubtarget &Subtarget) const {
6009 SDLoc dl(Op);
6010
6011 // Get the corect type for integers.
6012 EVT IntVT = Op.getValueType();
6013
6014 // Get the inputs.
6015 SDValue Chain = Op.getOperand(0);
6016 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
6017 // Build a DYNAREAOFFSET node.
6018 SDValue Ops[2] = {Chain, FPSIdx};
6019 SDVTList VTs = DAG.getVTList(IntVT);
6020 return DAG.getNode(PPCISD::DYNAREAOFFSET, dl, VTs, Ops);
6021}
6022
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006023SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006024 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00006025 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006026 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006027
Jim Laskeye4f4d042006-12-04 22:04:42 +00006028 // Get the corect type for pointers.
Mehdi Amini44ede332015-07-09 02:09:04 +00006029 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Jim Laskeye4f4d042006-12-04 22:04:42 +00006030
6031 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00006032 bool isPPC64 = Subtarget.isPPC64();
6033 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006034 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00006035
6036 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006037 SDValue Chain = Op.getOperand(0);
6038 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006039
Jim Laskeye4f4d042006-12-04 22:04:42 +00006040 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00006041 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
6042 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006043 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006044
Jim Laskeye4f4d042006-12-04 22:04:42 +00006045 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00006046 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006047
Jim Laskeye4f4d042006-12-04 22:04:42 +00006048 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00006049 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00006050 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00006051}
6052
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00006053SDValue PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG &DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00006054 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006055 bool isPPC64 = Subtarget.isPPC64();
Mehdi Amini44ede332015-07-09 02:09:04 +00006056 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006057
6058 // Get current frame pointer save index. The users of this index will be
6059 // primarily DYNALLOC instructions.
6060 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
6061 int RASI = FI->getReturnAddrSaveIndex();
6062
6063 // If the frame pointer save index hasn't been defined yet.
6064 if (!RASI) {
6065 // Find out what the fix offset of the frame pointer save area.
Eric Christopherf71609b2015-02-13 00:39:27 +00006066 int LROffset = Subtarget.getFrameLowering()->getReturnSaveOffset();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006067 // Allocate the frame index for frame pointer save area.
Hal Finkel6e27c6d2014-12-23 09:45:06 +00006068 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, false);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006069 // Save the result.
6070 FI->setReturnAddrSaveIndex(RASI);
6071 }
6072 return DAG.getFrameIndex(RASI, PtrVT);
6073}
6074
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006075SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006076PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
6077 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006078 bool isPPC64 = Subtarget.isPPC64();
Mehdi Amini44ede332015-07-09 02:09:04 +00006079 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Jim Laskey48850c12006-11-16 22:43:37 +00006080
6081 // Get current frame pointer save index. The users of this index will be
6082 // primarily DYNALLOC instructions.
6083 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
6084 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006085
Jim Laskey48850c12006-11-16 22:43:37 +00006086 // If the frame pointer save index hasn't been defined yet.
6087 if (!FPSI) {
6088 // Find out what the fix offset of the frame pointer save area.
Eric Christopherdc3a8a42015-02-13 00:39:38 +00006089 int FPOffset = Subtarget.getFrameLowering()->getFramePointerSaveOffset();
Jim Laskey48850c12006-11-16 22:43:37 +00006090 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00006091 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00006092 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006093 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00006094 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006095 return DAG.getFrameIndex(FPSI, PtrVT);
6096}
Jim Laskey48850c12006-11-16 22:43:37 +00006097
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006098SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006099 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006100 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00006101 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006102 SDValue Chain = Op.getOperand(0);
6103 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006104 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006105
Jim Laskey48850c12006-11-16 22:43:37 +00006106 // Get the corect type for pointers.
Mehdi Amini44ede332015-07-09 02:09:04 +00006107 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Jim Laskey48850c12006-11-16 22:43:37 +00006108 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00006109 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006110 DAG.getConstant(0, dl, PtrVT), Size);
Jim Laskey48850c12006-11-16 22:43:37 +00006111 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006112 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00006113 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006114 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00006115 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Craig Topper48d114b2014-04-26 18:35:24 +00006116 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops);
Jim Laskey48850c12006-11-16 22:43:37 +00006117}
6118
Hal Finkel756810f2013-03-21 21:37:52 +00006119SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
6120 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006121 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00006122 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
6123 DAG.getVTList(MVT::i32, MVT::Other),
6124 Op.getOperand(0), Op.getOperand(1));
6125}
6126
6127SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
6128 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006129 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00006130 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
6131 Op.getOperand(0), Op.getOperand(1));
6132}
6133
Hal Finkel940ab932014-02-28 00:27:01 +00006134SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +00006135 if (Op.getValueType().isVector())
6136 return LowerVectorLoad(Op, DAG);
6137
Hal Finkel940ab932014-02-28 00:27:01 +00006138 assert(Op.getValueType() == MVT::i1 &&
6139 "Custom lowering only for i1 loads");
6140
6141 // First, load 8 bits into 32 bits, then truncate to 1 bit.
6142
6143 SDLoc dl(Op);
6144 LoadSDNode *LD = cast<LoadSDNode>(Op);
6145
6146 SDValue Chain = LD->getChain();
6147 SDValue BasePtr = LD->getBasePtr();
6148 MachineMemOperand *MMO = LD->getMemOperand();
6149
Mehdi Amini44ede332015-07-09 02:09:04 +00006150 SDValue NewLD =
6151 DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(DAG.getDataLayout()), Chain,
6152 BasePtr, MVT::i8, MMO);
Hal Finkel940ab932014-02-28 00:27:01 +00006153 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
6154
6155 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00006156 return DAG.getMergeValues(Ops, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00006157}
6158
6159SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +00006160 if (Op.getOperand(1).getValueType().isVector())
6161 return LowerVectorStore(Op, DAG);
6162
Hal Finkel940ab932014-02-28 00:27:01 +00006163 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
6164 "Custom lowering only for i1 stores");
6165
6166 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
6167
6168 SDLoc dl(Op);
6169 StoreSDNode *ST = cast<StoreSDNode>(Op);
6170
6171 SDValue Chain = ST->getChain();
6172 SDValue BasePtr = ST->getBasePtr();
6173 SDValue Value = ST->getValue();
6174 MachineMemOperand *MMO = ST->getMemOperand();
6175
Mehdi Amini44ede332015-07-09 02:09:04 +00006176 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(DAG.getDataLayout()),
6177 Value);
Hal Finkel940ab932014-02-28 00:27:01 +00006178 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
6179}
6180
6181// FIXME: Remove this once the ANDI glue bug is fixed:
6182SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
6183 assert(Op.getValueType() == MVT::i1 &&
6184 "Custom lowering only for i1 results");
6185
6186 SDLoc DL(Op);
6187 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
6188 Op.getOperand(0));
6189}
6190
Chris Lattner4211ca92006-04-14 06:01:58 +00006191/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
6192/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00006193SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00006194 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00006195 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
6196 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00006197 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006198
Hal Finkel81f87992013-04-07 22:11:09 +00006199 // We might be able to do better than this under some circumstances, but in
6200 // general, fsel-based lowering of select is a finite-math-only optimization.
6201 // For more information, see section F.3 of the 2.06 ISA specification.
6202 if (!DAG.getTarget().Options.NoInfsFPMath ||
6203 !DAG.getTarget().Options.NoNaNsFPMath)
6204 return Op;
Sanjay Patela2607012015-09-16 16:31:21 +00006205 // TODO: Propagate flags from the select rather than global settings.
6206 SDNodeFlags Flags;
6207 Flags.setNoInfs(true);
6208 Flags.setNoNaNs(true);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +00006209
Hal Finkel81f87992013-04-07 22:11:09 +00006210 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006211
Owen Anderson53aa7a92009-08-10 22:56:29 +00006212 EVT ResVT = Op.getValueType();
6213 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006214 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
6215 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006216 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006217
Chris Lattner4211ca92006-04-14 06:01:58 +00006218 // If the RHS of the comparison is a 0.0, we don't need to do the
6219 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00006220 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00006221 if (isFloatingPointZero(RHS))
6222 switch (CC) {
6223 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00006224 case ISD::SETNE:
6225 std::swap(TV, FV);
6226 case ISD::SETEQ:
6227 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
6228 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
6229 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
6230 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
6231 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
6232 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
6233 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006234 case ISD::SETULT:
6235 case ISD::SETLT:
6236 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00006237 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00006238 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00006239 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
6240 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00006241 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006242 case ISD::SETUGT:
6243 case ISD::SETGT:
6244 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00006245 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00006246 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00006247 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
6248 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00006249 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00006250 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006251 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006252
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006253 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00006254 switch (CC) {
6255 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00006256 case ISD::SETNE:
6257 std::swap(TV, FV);
6258 case ISD::SETEQ:
Sanjay Patela2607012015-09-16 16:31:21 +00006259 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, &Flags);
Hal Finkel81f87992013-04-07 22:11:09 +00006260 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6261 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
6262 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
6263 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
6264 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
6265 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
6266 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006267 case ISD::SETULT:
6268 case ISD::SETLT:
Sanjay Patela2607012015-09-16 16:31:21 +00006269 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, &Flags);
Owen Anderson9f944592009-08-11 20:47:22 +00006270 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6271 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00006272 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00006273 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00006274 case ISD::SETGE:
Sanjay Patela2607012015-09-16 16:31:21 +00006275 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, &Flags);
Owen Anderson9f944592009-08-11 20:47:22 +00006276 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6277 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00006278 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006279 case ISD::SETUGT:
6280 case ISD::SETGT:
Sanjay Patela2607012015-09-16 16:31:21 +00006281 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS, &Flags);
Owen Anderson9f944592009-08-11 20:47:22 +00006282 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6283 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00006284 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00006285 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00006286 case ISD::SETLE:
Sanjay Patela2607012015-09-16 16:31:21 +00006287 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS, &Flags);
Owen Anderson9f944592009-08-11 20:47:22 +00006288 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6289 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00006290 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006291 }
Eli Friedman5806e182009-05-28 04:31:08 +00006292 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00006293}
6294
Hal Finkeled844c42015-01-06 22:31:02 +00006295void PPCTargetLowering::LowerFP_TO_INTForReuse(SDValue Op, ReuseLoadInfo &RLI,
6296 SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006297 const SDLoc &dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00006298 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006299 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00006300 if (Src.getValueType() == MVT::f32)
6301 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00006302
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006303 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00006304 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006305 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00006306 case MVT::i32:
Eric Christophercccae792015-01-30 22:02:31 +00006307 Tmp = DAG.getNode(
6308 Op.getOpcode() == ISD::FP_TO_SINT
6309 ? PPCISD::FCTIWZ
6310 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ),
6311 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00006312 break;
Owen Anderson9f944592009-08-11 20:47:22 +00006313 case MVT::i64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006314 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) &&
Hal Finkel3f88d082013-04-01 18:42:58 +00006315 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00006316 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
6317 PPCISD::FCTIDUZ,
6318 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00006319 break;
6320 }
Duncan Sands2a287912008-07-19 16:26:02 +00006321
Chris Lattner4211ca92006-04-14 06:01:58 +00006322 // Convert the FP value to an int value through memory.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006323 bool i32Stack = Op.getValueType() == MVT::i32 && Subtarget.hasSTFIWX() &&
6324 (Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT());
Hal Finkelf6d45f22013-04-01 17:52:07 +00006325 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
6326 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
Alex Lorenze40c8a22015-08-11 23:09:45 +00006327 MachinePointerInfo MPI =
6328 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
Duncan Sands2a287912008-07-19 16:26:02 +00006329
Chris Lattner06a49542007-10-15 20:14:52 +00006330 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00006331 SDValue Chain;
6332 if (i32Stack) {
6333 MachineFunction &MF = DAG.getMachineFunction();
6334 MachineMemOperand *MMO =
6335 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
6336 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
6337 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00006338 DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO);
Hal Finkelf6d45f22013-04-01 17:52:07 +00006339 } else
6340 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
6341 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00006342
6343 // Result is a load from the stack slot. If loading 4 bytes, make sure to
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00006344 // add in a bias on big endian.
Hal Finkelf6d45f22013-04-01 17:52:07 +00006345 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00006346 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006347 DAG.getConstant(4, dl, FIPtr.getValueType()));
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00006348 MPI = MPI.getWithOffset(Subtarget.isLittleEndian() ? 0 : 4);
Hal Finkelf6d45f22013-04-01 17:52:07 +00006349 }
6350
Hal Finkeled844c42015-01-06 22:31:02 +00006351 RLI.Chain = Chain;
6352 RLI.Ptr = FIPtr;
6353 RLI.MPI = MPI;
6354}
6355
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006356/// \brief Custom lowers floating point to integer conversions to use
6357/// the direct move instructions available in ISA 2.07 to avoid the
6358/// need for load/store combinations.
6359SDValue PPCTargetLowering::LowerFP_TO_INTDirectMove(SDValue Op,
6360 SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006361 const SDLoc &dl) const {
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006362 assert(Op.getOperand(0).getValueType().isFloatingPoint());
6363 SDValue Src = Op.getOperand(0);
6364
6365 if (Src.getValueType() == MVT::f32)
6366 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
6367
6368 SDValue Tmp;
6369 switch (Op.getSimpleValueType().SimpleTy) {
6370 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
6371 case MVT::i32:
6372 Tmp = DAG.getNode(
6373 Op.getOpcode() == ISD::FP_TO_SINT
6374 ? PPCISD::FCTIWZ
6375 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ),
6376 dl, MVT::f64, Src);
6377 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i32, Tmp);
6378 break;
6379 case MVT::i64:
6380 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) &&
6381 "i64 FP_TO_UINT is supported only with FPCVT");
6382 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
6383 PPCISD::FCTIDUZ,
6384 dl, MVT::f64, Src);
6385 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i64, Tmp);
6386 break;
6387 }
6388 return Tmp;
6389}
6390
Hal Finkeled844c42015-01-06 22:31:02 +00006391SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006392 const SDLoc &dl) const {
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006393 if (Subtarget.hasDirectMove() && Subtarget.isPPC64())
6394 return LowerFP_TO_INTDirectMove(Op, DAG, dl);
6395
Hal Finkeled844c42015-01-06 22:31:02 +00006396 ReuseLoadInfo RLI;
6397 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl);
6398
6399 return DAG.getLoad(Op.getValueType(), dl, RLI.Chain, RLI.Ptr, RLI.MPI, false,
6400 false, RLI.IsInvariant, RLI.Alignment, RLI.AAInfo,
6401 RLI.Ranges);
6402}
6403
6404// We're trying to insert a regular store, S, and then a load, L. If the
6405// incoming value, O, is a load, we might just be able to have our load use the
6406// address used by O. However, we don't know if anything else will store to
6407// that address before we can load from it. To prevent this situation, we need
6408// to insert our load, L, into the chain as a peer of O. To do this, we give L
6409// the same chain operand as O, we create a token factor from the chain results
6410// of O and L, and we replace all uses of O's chain result with that token
6411// factor (see spliceIntoChain below for this last part).
6412bool PPCTargetLowering::canReuseLoadAddress(SDValue Op, EVT MemVT,
6413 ReuseLoadInfo &RLI,
Hal Finkel6c392692015-01-09 01:34:30 +00006414 SelectionDAG &DAG,
6415 ISD::LoadExtType ET) const {
Hal Finkeled844c42015-01-06 22:31:02 +00006416 SDLoc dl(Op);
Hal Finkel6c392692015-01-09 01:34:30 +00006417 if (ET == ISD::NON_EXTLOAD &&
6418 (Op.getOpcode() == ISD::FP_TO_UINT ||
Hal Finkeled844c42015-01-06 22:31:02 +00006419 Op.getOpcode() == ISD::FP_TO_SINT) &&
6420 isOperationLegalOrCustom(Op.getOpcode(),
6421 Op.getOperand(0).getValueType())) {
6422
6423 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl);
6424 return true;
6425 }
6426
6427 LoadSDNode *LD = dyn_cast<LoadSDNode>(Op);
Hal Finkel6c392692015-01-09 01:34:30 +00006428 if (!LD || LD->getExtensionType() != ET || LD->isVolatile() ||
6429 LD->isNonTemporal())
Hal Finkeled844c42015-01-06 22:31:02 +00006430 return false;
6431 if (LD->getMemoryVT() != MemVT)
6432 return false;
6433
6434 RLI.Ptr = LD->getBasePtr();
Sanjay Patel75068522016-03-14 18:09:43 +00006435 if (LD->isIndexed() && !LD->getOffset().isUndef()) {
Hal Finkeled844c42015-01-06 22:31:02 +00006436 assert(LD->getAddressingMode() == ISD::PRE_INC &&
6437 "Non-pre-inc AM on PPC?");
6438 RLI.Ptr = DAG.getNode(ISD::ADD, dl, RLI.Ptr.getValueType(), RLI.Ptr,
6439 LD->getOffset());
6440 }
6441
6442 RLI.Chain = LD->getChain();
6443 RLI.MPI = LD->getPointerInfo();
6444 RLI.IsInvariant = LD->isInvariant();
6445 RLI.Alignment = LD->getAlignment();
6446 RLI.AAInfo = LD->getAAInfo();
6447 RLI.Ranges = LD->getRanges();
6448
6449 RLI.ResChain = SDValue(LD, LD->isIndexed() ? 2 : 1);
6450 return true;
6451}
6452
6453// Given the head of the old chain, ResChain, insert a token factor containing
6454// it and NewResChain, and make users of ResChain now be users of that token
6455// factor.
6456void PPCTargetLowering::spliceIntoChain(SDValue ResChain,
6457 SDValue NewResChain,
6458 SelectionDAG &DAG) const {
6459 if (!ResChain)
6460 return;
6461
6462 SDLoc dl(NewResChain);
6463
6464 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
6465 NewResChain, DAG.getUNDEF(MVT::Other));
6466 assert(TF.getNode() != NewResChain.getNode() &&
6467 "A new TF really is required here");
6468
6469 DAG.ReplaceAllUsesOfValueWith(ResChain, TF);
6470 DAG.UpdateNodeOperands(TF.getNode(), ResChain, NewResChain);
Chris Lattner4211ca92006-04-14 06:01:58 +00006471}
6472
Ehsan Amiri322eca32016-04-06 20:12:29 +00006473/// \brief Analyze profitability of direct move
6474/// prefer float load to int load plus direct move
6475/// when there is no integer use of int load
6476static bool directMoveIsProfitable(const SDValue &Op) {
6477 SDNode *Origin = Op.getOperand(0).getNode();
6478 if (Origin->getOpcode() != ISD::LOAD)
6479 return true;
6480
6481 for (SDNode::use_iterator UI = Origin->use_begin(),
6482 UE = Origin->use_end();
6483 UI != UE; ++UI) {
6484
6485 // Only look at the users of the loaded value.
6486 if (UI.getUse().get().getResNo() != 0)
6487 continue;
6488
6489 if (UI->getOpcode() != ISD::SINT_TO_FP &&
6490 UI->getOpcode() != ISD::UINT_TO_FP)
6491 return true;
6492 }
6493
6494 return false;
6495}
6496
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006497/// \brief Custom lowers integer to floating point conversions to use
6498/// the direct move instructions available in ISA 2.07 to avoid the
6499/// need for load/store combinations.
6500SDValue PPCTargetLowering::LowerINT_TO_FPDirectMove(SDValue Op,
6501 SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006502 const SDLoc &dl) const {
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006503 assert((Op.getValueType() == MVT::f32 ||
6504 Op.getValueType() == MVT::f64) &&
6505 "Invalid floating point type as target of conversion");
6506 assert(Subtarget.hasFPCVT() &&
6507 "Int to FP conversions with direct moves require FPCVT");
6508 SDValue FP;
6509 SDValue Src = Op.getOperand(0);
6510 bool SinglePrec = Op.getValueType() == MVT::f32;
6511 bool WordInt = Src.getSimpleValueType().SimpleTy == MVT::i32;
6512 bool Signed = Op.getOpcode() == ISD::SINT_TO_FP;
6513 unsigned ConvOp = Signed ? (SinglePrec ? PPCISD::FCFIDS : PPCISD::FCFID) :
6514 (SinglePrec ? PPCISD::FCFIDUS : PPCISD::FCFIDU);
6515
6516 if (WordInt) {
6517 FP = DAG.getNode(Signed ? PPCISD::MTVSRA : PPCISD::MTVSRZ,
6518 dl, MVT::f64, Src);
6519 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP);
6520 }
6521 else {
6522 FP = DAG.getNode(PPCISD::MTVSRA, dl, MVT::f64, Src);
6523 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP);
6524 }
6525
6526 return FP;
6527}
6528
Hal Finkelf6d45f22013-04-01 17:52:07 +00006529SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Hal Finkeled844c42015-01-06 22:31:02 +00006530 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006531 SDLoc dl(Op);
Hal Finkelc93a9a22015-02-25 01:06:45 +00006532
6533 if (Subtarget.hasQPX() && Op.getOperand(0).getValueType() == MVT::v4i1) {
6534 if (Op.getValueType() != MVT::v4f32 && Op.getValueType() != MVT::v4f64)
6535 return SDValue();
6536
6537 SDValue Value = Op.getOperand(0);
6538 // The values are now known to be -1 (false) or 1 (true). To convert this
6539 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
6540 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
6541 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +00006542
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006543 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64);
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00006544
Hal Finkelc93a9a22015-02-25 01:06:45 +00006545 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
6546
6547 if (Op.getValueType() != MVT::v4f64)
6548 Value = DAG.getNode(ISD::FP_ROUND, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006549 Op.getValueType(), Value,
6550 DAG.getIntPtrConstant(1, dl));
Hal Finkelc93a9a22015-02-25 01:06:45 +00006551 return Value;
6552 }
6553
Dan Gohmand6819da2008-03-11 01:59:03 +00006554 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00006555 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006556 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00006557
Hal Finkel6a56b212014-03-05 22:14:00 +00006558 if (Op.getOperand(0).getValueType() == MVT::i1)
6559 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006560 DAG.getConstantFP(1.0, dl, Op.getValueType()),
6561 DAG.getConstantFP(0.0, dl, Op.getValueType()));
Hal Finkel6a56b212014-03-05 22:14:00 +00006562
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006563 // If we have direct moves, we can do all the conversion, skip the store/load
6564 // however, without FPCVT we can't do most conversions.
Ehsan Amiri322eca32016-04-06 20:12:29 +00006565 if (Subtarget.hasDirectMove() && directMoveIsProfitable(Op) &&
6566 Subtarget.isPPC64() && Subtarget.hasFPCVT())
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006567 return LowerINT_TO_FPDirectMove(Op, DAG, dl);
6568
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006569 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006570 "UINT_TO_FP is supported only with FPCVT");
6571
6572 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00006573 // Otherwise, convert to double-precision and then round.
Eric Christophercccae792015-01-30 22:02:31 +00006574 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
6575 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS
6576 : PPCISD::FCFIDS)
6577 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU
6578 : PPCISD::FCFID);
6579 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
6580 ? MVT::f32
6581 : MVT::f64;
Hal Finkelf6d45f22013-04-01 17:52:07 +00006582
Owen Anderson9f944592009-08-11 20:47:22 +00006583 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006584 SDValue SINT = Op.getOperand(0);
6585 // When converting to single-precision, we actually need to convert
6586 // to double-precision first and then round to single-precision.
6587 // To avoid double-rounding effects during that operation, we have
6588 // to prepare the input operand. Bits that might be truncated when
6589 // converting to double-precision are replaced by a bit that won't
6590 // be lost at this stage, but is below the single-precision rounding
6591 // position.
6592 //
6593 // However, if -enable-unsafe-fp-math is in effect, accept double
6594 // rounding to avoid the extra overhead.
6595 if (Op.getValueType() == MVT::f32 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006596 !Subtarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006597 !DAG.getTarget().Options.UnsafeFPMath) {
6598
6599 // Twiddle input to make sure the low 11 bits are zero. (If this
6600 // is the case, we are guaranteed the value will fit into the 53 bit
6601 // mantissa of an IEEE double-precision value without rounding.)
6602 // If any of those low 11 bits were not zero originally, make sure
6603 // bit 12 (value 2048) is set instead, so that the final rounding
6604 // to single-precision gets the correct result.
6605 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006606 SINT, DAG.getConstant(2047, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006607 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006608 Round, DAG.getConstant(2047, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006609 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
6610 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006611 Round, DAG.getConstant(-2048, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006612
6613 // However, we cannot use that value unconditionally: if the magnitude
6614 // of the input value is small, the bit-twiddling we did above might
6615 // end up visibly changing the output. Fortunately, in that case, we
6616 // don't need to twiddle bits since the original input will convert
6617 // exactly to double-precision floating-point already. Therefore,
6618 // construct a conditional to use the original value if the top 11
6619 // bits are all sign-bit copies, and use the rounded value computed
6620 // above otherwise.
6621 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006622 SINT, DAG.getConstant(53, dl, MVT::i32));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006623 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006624 Cond, DAG.getConstant(1, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006625 Cond = DAG.getSetCC(dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006626 Cond, DAG.getConstant(1, dl, MVT::i64), ISD::SETUGT);
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006627
6628 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
6629 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00006630
Hal Finkeled844c42015-01-06 22:31:02 +00006631 ReuseLoadInfo RLI;
6632 SDValue Bits;
6633
Hal Finkel6c392692015-01-09 01:34:30 +00006634 MachineFunction &MF = DAG.getMachineFunction();
Hal Finkeled844c42015-01-06 22:31:02 +00006635 if (canReuseLoadAddress(SINT, MVT::i64, RLI, DAG)) {
6636 Bits = DAG.getLoad(MVT::f64, dl, RLI.Chain, RLI.Ptr, RLI.MPI, false,
6637 false, RLI.IsInvariant, RLI.Alignment, RLI.AAInfo,
6638 RLI.Ranges);
6639 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
Hal Finkel6c392692015-01-09 01:34:30 +00006640 } else if (Subtarget.hasLFIWAX() &&
6641 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::SEXTLOAD)) {
6642 MachineMemOperand *MMO =
6643 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6644 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6645 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6646 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWAX, dl,
6647 DAG.getVTList(MVT::f64, MVT::Other),
6648 Ops, MVT::i32, MMO);
6649 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
6650 } else if (Subtarget.hasFPCVT() &&
6651 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::ZEXTLOAD)) {
6652 MachineMemOperand *MMO =
6653 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6654 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6655 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6656 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWZX, dl,
6657 DAG.getVTList(MVT::f64, MVT::Other),
6658 Ops, MVT::i32, MMO);
6659 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
6660 } else if (((Subtarget.hasLFIWAX() &&
6661 SINT.getOpcode() == ISD::SIGN_EXTEND) ||
6662 (Subtarget.hasFPCVT() &&
6663 SINT.getOpcode() == ISD::ZERO_EXTEND)) &&
6664 SINT.getOperand(0).getValueType() == MVT::i32) {
6665 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +00006666 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Hal Finkel6c392692015-01-09 01:34:30 +00006667
6668 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
6669 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
6670
Alex Lorenze40c8a22015-08-11 23:09:45 +00006671 SDValue Store = DAG.getStore(
6672 DAG.getEntryNode(), dl, SINT.getOperand(0), FIdx,
6673 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx),
6674 false, false, 0);
Hal Finkel6c392692015-01-09 01:34:30 +00006675
6676 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
6677 "Expected an i32 store");
6678
6679 RLI.Ptr = FIdx;
6680 RLI.Chain = Store;
Alex Lorenze40c8a22015-08-11 23:09:45 +00006681 RLI.MPI =
6682 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Hal Finkel6c392692015-01-09 01:34:30 +00006683 RLI.Alignment = 4;
6684
6685 MachineMemOperand *MMO =
6686 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6687 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6688 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6689 Bits = DAG.getMemIntrinsicNode(SINT.getOpcode() == ISD::ZERO_EXTEND ?
6690 PPCISD::LFIWZX : PPCISD::LFIWAX,
6691 dl, DAG.getVTList(MVT::f64, MVT::Other),
6692 Ops, MVT::i32, MMO);
Hal Finkeled844c42015-01-06 22:31:02 +00006693 } else
6694 Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
6695
Hal Finkelf6d45f22013-04-01 17:52:07 +00006696 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
6697
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006698 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00006699 FP = DAG.getNode(ISD::FP_ROUND, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006700 MVT::f32, FP, DAG.getIntPtrConstant(0, dl));
Chris Lattner4211ca92006-04-14 06:01:58 +00006701 return FP;
6702 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006703
Owen Anderson9f944592009-08-11 20:47:22 +00006704 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006705 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00006706 // Since we only generate this in 64-bit mode, we can take advantage of
6707 // 64-bit registers. In particular, sign extend the input value into the
6708 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
6709 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00006710 MachineFunction &MF = DAG.getMachineFunction();
6711 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +00006712 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Scott Michelcf0da6c2009-02-17 22:15:04 +00006713
Hal Finkelbeb296b2013-03-31 10:12:51 +00006714 SDValue Ld;
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006715 if (Subtarget.hasLFIWAX() || Subtarget.hasFPCVT()) {
Hal Finkeled844c42015-01-06 22:31:02 +00006716 ReuseLoadInfo RLI;
6717 bool ReusingLoad;
6718 if (!(ReusingLoad = canReuseLoadAddress(Op.getOperand(0), MVT::i32, RLI,
6719 DAG))) {
6720 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
6721 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006722
Alex Lorenze40c8a22015-08-11 23:09:45 +00006723 SDValue Store = DAG.getStore(
6724 DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
6725 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx),
6726 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00006727
Hal Finkeled844c42015-01-06 22:31:02 +00006728 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
6729 "Expected an i32 store");
6730
6731 RLI.Ptr = FIdx;
6732 RLI.Chain = Store;
Alex Lorenze40c8a22015-08-11 23:09:45 +00006733 RLI.MPI =
6734 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Hal Finkeled844c42015-01-06 22:31:02 +00006735 RLI.Alignment = 4;
6736 }
6737
Hal Finkelbeb296b2013-03-31 10:12:51 +00006738 MachineMemOperand *MMO =
Hal Finkeled844c42015-01-06 22:31:02 +00006739 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6740 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6741 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
Hal Finkelf6d45f22013-04-01 17:52:07 +00006742 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
6743 PPCISD::LFIWZX : PPCISD::LFIWAX,
6744 dl, DAG.getVTList(MVT::f64, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00006745 Ops, MVT::i32, MMO);
Hal Finkeled844c42015-01-06 22:31:02 +00006746 if (ReusingLoad)
6747 spliceIntoChain(RLI.ResChain, Ld.getValue(1), DAG);
Hal Finkelbeb296b2013-03-31 10:12:51 +00006748 } else {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006749 assert(Subtarget.isPPC64() &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006750 "i32->FP without LFIWAX supported only on PPC64");
6751
Hal Finkelbeb296b2013-03-31 10:12:51 +00006752 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
6753 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
6754
6755 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
6756 Op.getOperand(0));
6757
6758 // STD the extended value into the stack slot.
Alex Lorenze40c8a22015-08-11 23:09:45 +00006759 SDValue Store = DAG.getStore(
6760 DAG.getEntryNode(), dl, Ext64, FIdx,
6761 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx),
6762 false, false, 0);
Hal Finkelbeb296b2013-03-31 10:12:51 +00006763
6764 // Load the value as a double.
Alex Lorenze40c8a22015-08-11 23:09:45 +00006765 Ld = DAG.getLoad(
6766 MVT::f64, dl, Store, FIdx,
6767 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx),
6768 false, false, false, 0);
Hal Finkelbeb296b2013-03-31 10:12:51 +00006769 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006770
Chris Lattner4211ca92006-04-14 06:01:58 +00006771 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00006772 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006773 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006774 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP,
6775 DAG.getIntPtrConstant(0, dl));
Chris Lattner4211ca92006-04-14 06:01:58 +00006776 return FP;
6777}
6778
Dan Gohman21cea8a2010-04-17 15:26:15 +00006779SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
6780 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006781 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006782 /*
6783 The rounding mode is in bits 30:31 of FPSR, and has the following
6784 settings:
6785 00 Round to nearest
6786 01 Round to 0
6787 10 Round to +inf
6788 11 Round to -inf
6789
6790 FLT_ROUNDS, on the other hand, expects the following:
6791 -1 Undefined
6792 0 Round to 0
6793 1 Round to nearest
6794 2 Round to +inf
6795 3 Round to -inf
6796
6797 To perform the conversion, we do:
6798 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
6799 */
6800
6801 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00006802 EVT VT = Op.getValueType();
Mehdi Amini44ede332015-07-09 02:09:04 +00006803 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006804
6805 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00006806 EVT NodeTys[] = {
6807 MVT::f64, // return register
6808 MVT::Glue // unused in this context
6809 };
Craig Topper2d2aa0c2014-04-30 07:17:30 +00006810 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, None);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006811
6812 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00006813 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006814 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00006815 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00006816 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006817
6818 // Load FP Control Word from low 32 bits of stack slot.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006819 SDValue Four = DAG.getConstant(4, dl, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00006820 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00006821 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006822 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006823
6824 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006825 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00006826 DAG.getNode(ISD::AND, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006827 CWD, DAG.getConstant(3, dl, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006828 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00006829 DAG.getNode(ISD::SRL, dl, MVT::i32,
6830 DAG.getNode(ISD::AND, dl, MVT::i32,
6831 DAG.getNode(ISD::XOR, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006832 CWD, DAG.getConstant(3, dl, MVT::i32)),
6833 DAG.getConstant(3, dl, MVT::i32)),
6834 DAG.getConstant(1, dl, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006835
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006836 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00006837 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006838
Duncan Sands13237ac2008-06-06 12:08:01 +00006839 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00006840 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006841}
6842
Dan Gohman21cea8a2010-04-17 15:26:15 +00006843SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006844 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006845 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006846 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00006847 assert(Op.getNumOperands() == 3 &&
6848 VT == Op.getOperand(1).getValueType() &&
6849 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006850
Chris Lattner601b8652006-09-20 03:47:40 +00006851 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00006852 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006853 SDValue Lo = Op.getOperand(0);
6854 SDValue Hi = Op.getOperand(1);
6855 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006856 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006857
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006858 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006859 DAG.getConstant(BitWidth, dl, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006860 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
6861 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
6862 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
6863 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006864 DAG.getConstant(-BitWidth, dl, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006865 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
6866 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
6867 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006868 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006869 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006870}
6871
Dan Gohman21cea8a2010-04-17 15:26:15 +00006872SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006873 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006874 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00006875 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00006876 assert(Op.getNumOperands() == 3 &&
6877 VT == Op.getOperand(1).getValueType() &&
6878 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006879
Dan Gohman8d2ead22008-03-07 20:36:53 +00006880 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00006881 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006882 SDValue Lo = Op.getOperand(0);
6883 SDValue Hi = Op.getOperand(1);
6884 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006885 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006886
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006887 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006888 DAG.getConstant(BitWidth, dl, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006889 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
6890 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
6891 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
6892 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006893 DAG.getConstant(-BitWidth, dl, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006894 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
6895 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
6896 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006897 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006898 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006899}
6900
Dan Gohman21cea8a2010-04-17 15:26:15 +00006901SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006902 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006903 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006904 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00006905 assert(Op.getNumOperands() == 3 &&
6906 VT == Op.getOperand(1).getValueType() &&
6907 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006908
Dan Gohman8d2ead22008-03-07 20:36:53 +00006909 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006910 SDValue Lo = Op.getOperand(0);
6911 SDValue Hi = Op.getOperand(1);
6912 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006913 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006914
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006915 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006916 DAG.getConstant(BitWidth, dl, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006917 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
6918 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
6919 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
6920 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006921 DAG.getConstant(-BitWidth, dl, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006922 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
6923 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006924 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, dl, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00006925 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006926 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006927 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006928}
6929
6930//===----------------------------------------------------------------------===//
6931// Vector related lowering.
6932//
6933
Chris Lattner2a099c02006-04-17 06:00:21 +00006934/// BuildSplatI - Build a canonical splati of Val with an element size of
6935/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00006936static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006937 SelectionDAG &DAG, const SDLoc &dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00006938 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00006939
Benjamin Kramer7149aab2015-03-01 18:09:56 +00006940 static const MVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00006941 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00006942 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00006943
Owen Anderson9f944592009-08-11 20:47:22 +00006944 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00006945
Chris Lattner09ed0ff2006-12-01 01:45:39 +00006946 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
6947 if (Val == -1)
6948 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006949
Owen Anderson53aa7a92009-08-10 22:56:29 +00006950 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00006951
Chris Lattner2a099c02006-04-17 06:00:21 +00006952 // Build a canonical splat for this value.
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006953 return DAG.getBitcast(ReqVT, DAG.getConstant(Val, dl, CanonicalVT));
Chris Lattner2a099c02006-04-17 06:00:21 +00006954}
6955
Hal Finkelcf2e9082013-05-24 23:00:14 +00006956/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
6957/// specified intrinsic ID.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006958static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op, SelectionDAG &DAG,
6959 const SDLoc &dl, EVT DestVT = MVT::Other) {
Hal Finkelcf2e9082013-05-24 23:00:14 +00006960 if (DestVT == MVT::Other) DestVT = Op.getValueType();
6961 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006962 DAG.getConstant(IID, dl, MVT::i32), Op);
Hal Finkelcf2e9082013-05-24 23:00:14 +00006963}
6964
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006965/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00006966/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006967static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006968 SelectionDAG &DAG, const SDLoc &dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006969 EVT DestVT = MVT::Other) {
6970 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006971 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006972 DAG.getConstant(IID, dl, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00006973}
6974
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006975/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
6976/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006977static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006978 SDValue Op2, SelectionDAG &DAG, const SDLoc &dl,
6979 EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00006980 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006981 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006982 DAG.getConstant(IID, dl, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006983}
6984
Chris Lattner264c9082006-04-17 17:55:10 +00006985/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
6986/// amount. The result has the specified value type.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006987static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt, EVT VT,
6988 SelectionDAG &DAG, const SDLoc &dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00006989 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00006990 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
6991 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00006992
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006993 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00006994 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006995 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00006996 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006997 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00006998}
6999
Chris Lattner19e90552006-04-14 05:19:18 +00007000// If this is a case we can't handle, return null and let the default
7001// expansion code take care of it. If we CAN select this case, and if it
7002// selects to a single instruction, return Op. Otherwise, if we can codegen
7003// this case more efficiently than a constant pool load, lower it to the
7004// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00007005SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
7006 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007007 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007008 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
Craig Toppere73658d2014-04-28 04:05:08 +00007009 assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00007010
Hal Finkelc93a9a22015-02-25 01:06:45 +00007011 if (Subtarget.hasQPX() && Op.getValueType() == MVT::v4i1) {
7012 // We first build an i32 vector, load it into a QPX register,
7013 // then convert it to a floating-point vector and compare it
7014 // to a zero vector to get the boolean result.
7015 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
7016 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Alex Lorenze40c8a22015-08-11 23:09:45 +00007017 MachinePointerInfo PtrInfo =
7018 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Mehdi Amini44ede332015-07-09 02:09:04 +00007019 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007020 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
7021
7022 assert(BVN->getNumOperands() == 4 &&
7023 "BUILD_VECTOR for v4i1 does not have 4 operands");
7024
7025 bool IsConst = true;
7026 for (unsigned i = 0; i < 4; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00007027 if (BVN->getOperand(i).isUndef()) continue;
Hal Finkelc93a9a22015-02-25 01:06:45 +00007028 if (!isa<ConstantSDNode>(BVN->getOperand(i))) {
7029 IsConst = false;
7030 break;
7031 }
7032 }
7033
7034 if (IsConst) {
7035 Constant *One =
7036 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), 1.0);
7037 Constant *NegOne =
7038 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), -1.0);
7039
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007040 Constant *CV[4];
Hal Finkelc93a9a22015-02-25 01:06:45 +00007041 for (unsigned i = 0; i < 4; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00007042 if (BVN->getOperand(i).isUndef())
Hal Finkelc93a9a22015-02-25 01:06:45 +00007043 CV[i] = UndefValue::get(Type::getFloatTy(*DAG.getContext()));
Artyom Skrobov314ee042015-11-25 19:41:11 +00007044 else if (isNullConstant(BVN->getOperand(i)))
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007045 CV[i] = NegOne;
Hal Finkelc93a9a22015-02-25 01:06:45 +00007046 else
7047 CV[i] = One;
7048 }
7049
7050 Constant *CP = ConstantVector::get(CV);
Mehdi Amini44ede332015-07-09 02:09:04 +00007051 SDValue CPIdx = DAG.getConstantPool(CP, getPointerTy(DAG.getDataLayout()),
7052 16 /* alignment */);
7053
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007054 SDValue Ops[] = {DAG.getEntryNode(), CPIdx};
7055 SDVTList VTs = DAG.getVTList({MVT::v4i1, /*chain*/ MVT::Other});
Alex Lorenze40c8a22015-08-11 23:09:45 +00007056 return DAG.getMemIntrinsicNode(
7057 PPCISD::QVLFSb, dl, VTs, Ops, MVT::v4f32,
7058 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007059 }
7060
7061 SmallVector<SDValue, 4> Stores;
7062 for (unsigned i = 0; i < 4; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00007063 if (BVN->getOperand(i).isUndef()) continue;
Hal Finkelc93a9a22015-02-25 01:06:45 +00007064
7065 unsigned Offset = 4*i;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007066 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007067 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
7068
7069 unsigned StoreSize = BVN->getOperand(i).getValueType().getStoreSize();
7070 if (StoreSize > 4) {
7071 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
7072 BVN->getOperand(i), Idx,
7073 PtrInfo.getWithOffset(Offset),
7074 MVT::i32, false, false, 0));
7075 } else {
7076 SDValue StoreValue = BVN->getOperand(i);
7077 if (StoreSize < 4)
7078 StoreValue = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, StoreValue);
7079
7080 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
7081 StoreValue, Idx,
7082 PtrInfo.getWithOffset(Offset),
7083 false, false, 0));
7084 }
7085 }
7086
7087 SDValue StoreChain;
7088 if (!Stores.empty())
7089 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
7090 else
7091 StoreChain = DAG.getEntryNode();
7092
7093 // Now load from v4i32 into the QPX register; this will extend it to
7094 // v4i64 but not yet convert it to a floating point. Nevertheless, this
7095 // is typed as v4f64 because the QPX register integer states are not
7096 // explicitly represented.
7097
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007098 SDValue Ops[] = {StoreChain,
7099 DAG.getConstant(Intrinsic::ppc_qpx_qvlfiwz, dl, MVT::i32),
7100 FIdx};
7101 SDVTList VTs = DAG.getVTList({MVT::v4f64, /*chain*/ MVT::Other});
Hal Finkelc93a9a22015-02-25 01:06:45 +00007102
7103 SDValue LoadedVect = DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN,
7104 dl, VTs, Ops, MVT::v4i32, PtrInfo);
7105 LoadedVect = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007106 DAG.getConstant(Intrinsic::ppc_qpx_qvfcfidu, dl, MVT::i32),
Hal Finkelc93a9a22015-02-25 01:06:45 +00007107 LoadedVect);
7108
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007109 SDValue FPZeros = DAG.getConstantFP(0.0, dl, MVT::v4f64);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007110
7111 return DAG.getSetCC(dl, MVT::v4i1, LoadedVect, FPZeros, ISD::SETEQ);
7112 }
7113
7114 // All other QPX vectors are handled by generic code.
7115 if (Subtarget.hasQPX())
7116 return SDValue();
7117
Bob Wilson85cefe82009-03-02 23:24:16 +00007118 // Check if this is a splat of a constant value.
7119 APInt APSplatBits, APSplatUndef;
7120 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007121 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00007122 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Bill Schmidt91dd7652015-04-03 13:48:24 +00007123 HasAnyUndefs, 0, !Subtarget.isLittleEndian()) ||
7124 SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00007125 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00007126
Bob Wilson530e0382009-03-03 19:26:27 +00007127 unsigned SplatBits = APSplatBits.getZExtValue();
7128 unsigned SplatUndef = APSplatUndef.getZExtValue();
7129 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007130
Bob Wilson530e0382009-03-03 19:26:27 +00007131 // First, handle single instruction cases.
7132
7133 // All zeros?
7134 if (SplatBits == 0) {
7135 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00007136 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007137 SDValue Z = DAG.getConstant(0, dl, MVT::v4i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00007138 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00007139 }
Bob Wilson530e0382009-03-03 19:26:27 +00007140 return Op;
7141 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00007142
Bob Wilson530e0382009-03-03 19:26:27 +00007143 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
7144 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
7145 (32-SplatBitSize));
7146 if (SextVal >= -16 && SextVal <= 15)
7147 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007148
Bob Wilson530e0382009-03-03 19:26:27 +00007149 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007150
Bob Wilson530e0382009-03-03 19:26:27 +00007151 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00007152 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
7153 // If this value is in the range [17,31] and is odd, use:
7154 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
7155 // If this value is in the range [-31,-17] and is odd, use:
7156 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
7157 // Note the last two are three-instruction sequences.
7158 if (SextVal >= -32 && SextVal <= 31) {
7159 // To avoid having these optimizations undone by constant folding,
7160 // we convert to a pseudo that will be expanded later into one of
7161 // the above forms.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007162 SDValue Elt = DAG.getConstant(SextVal, dl, MVT::i32);
Bill Schmidt71dddd52014-05-27 15:57:51 +00007163 EVT VT = (SplatSize == 1 ? MVT::v16i8 :
7164 (SplatSize == 2 ? MVT::v8i16 : MVT::v4i32));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007165 SDValue EltSize = DAG.getConstant(SplatSize, dl, MVT::i32);
Bill Schmidt71dddd52014-05-27 15:57:51 +00007166 SDValue RetVal = DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
7167 if (VT == Op.getValueType())
7168 return RetVal;
7169 else
7170 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), RetVal);
Bob Wilson530e0382009-03-03 19:26:27 +00007171 }
7172
7173 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
7174 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
7175 // for fneg/fabs.
7176 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
7177 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00007178 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007179
7180 // Make the VSLW intrinsic, computing 0x8000_0000.
7181 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
7182 OnesV, DAG, dl);
7183
7184 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00007185 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00007186 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00007187 }
7188
7189 // Check to see if this is a wide variety of vsplti*, binop self cases.
7190 static const signed char SplatCsts[] = {
7191 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
7192 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
7193 };
7194
7195 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
7196 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
7197 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
7198 int i = SplatCsts[idx];
7199
7200 // Figure out what shift amount will be used by altivec if shifted by i in
7201 // this splat size.
7202 unsigned TypeShiftAmt = i & (SplatBitSize-1);
7203
7204 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00007205 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00007206 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007207 static const unsigned IIDs[] = { // Intrinsic to use for each size.
7208 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
7209 Intrinsic::ppc_altivec_vslw
7210 };
7211 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00007212 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00007213 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007214
Bob Wilson530e0382009-03-03 19:26:27 +00007215 // vsplti + srl self.
7216 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00007217 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007218 static const unsigned IIDs[] = { // Intrinsic to use for each size.
7219 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
7220 Intrinsic::ppc_altivec_vsrw
7221 };
7222 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00007223 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00007224 }
7225
Bob Wilson530e0382009-03-03 19:26:27 +00007226 // vsplti + sra self.
7227 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00007228 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007229 static const unsigned IIDs[] = { // Intrinsic to use for each size.
7230 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
7231 Intrinsic::ppc_altivec_vsraw
7232 };
7233 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00007234 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00007235 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007236
Bob Wilson530e0382009-03-03 19:26:27 +00007237 // vsplti + rol self.
7238 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
7239 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00007240 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007241 static const unsigned IIDs[] = { // Intrinsic to use for each size.
7242 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
7243 Intrinsic::ppc_altivec_vrlw
7244 };
7245 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00007246 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00007247 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007248
Bob Wilson530e0382009-03-03 19:26:27 +00007249 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00007250 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00007251 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bill Schmidt1e77bb12015-07-15 15:45:30 +00007252 unsigned Amt = Subtarget.isLittleEndian() ? 15 : 1;
7253 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00007254 }
Bob Wilson530e0382009-03-03 19:26:27 +00007255 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00007256 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00007257 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bill Schmidt1e77bb12015-07-15 15:45:30 +00007258 unsigned Amt = Subtarget.isLittleEndian() ? 14 : 2;
7259 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00007260 }
Bob Wilson530e0382009-03-03 19:26:27 +00007261 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00007262 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00007263 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bill Schmidt1e77bb12015-07-15 15:45:30 +00007264 unsigned Amt = Subtarget.isLittleEndian() ? 13 : 3;
7265 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007266 }
7267 }
7268
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007269 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00007270}
7271
Chris Lattner071ad012006-04-17 05:28:54 +00007272/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
7273/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007274static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00007275 SDValue RHS, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007276 const SDLoc &dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00007277 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00007278 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00007279 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007280
Chris Lattner071ad012006-04-17 05:28:54 +00007281 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00007282 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
Chris Lattner071ad012006-04-17 05:28:54 +00007283 OP_VMRGHW,
7284 OP_VMRGLW,
7285 OP_VSPLTISW0,
7286 OP_VSPLTISW1,
7287 OP_VSPLTISW2,
7288 OP_VSPLTISW3,
7289 OP_VSLDOI4,
7290 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00007291 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00007292 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00007293
Chris Lattner071ad012006-04-17 05:28:54 +00007294 if (OpNum == OP_COPY) {
7295 if (LHSID == (1*9+2)*9+3) return LHS;
7296 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
7297 return RHS;
7298 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007299
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007300 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007301 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
7302 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007303
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007304 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00007305 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007306 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00007307 case OP_VMRGHW:
7308 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
7309 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
7310 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
7311 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
7312 break;
7313 case OP_VMRGLW:
7314 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
7315 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
7316 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
7317 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
7318 break;
7319 case OP_VSPLTISW0:
7320 for (unsigned i = 0; i != 16; ++i)
7321 ShufIdxs[i] = (i&3)+0;
7322 break;
7323 case OP_VSPLTISW1:
7324 for (unsigned i = 0; i != 16; ++i)
7325 ShufIdxs[i] = (i&3)+4;
7326 break;
7327 case OP_VSPLTISW2:
7328 for (unsigned i = 0; i != 16; ++i)
7329 ShufIdxs[i] = (i&3)+8;
7330 break;
7331 case OP_VSPLTISW3:
7332 for (unsigned i = 0; i != 16; ++i)
7333 ShufIdxs[i] = (i&3)+12;
7334 break;
7335 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007336 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007337 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007338 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007339 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007340 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007341 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00007342 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00007343 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
7344 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00007345 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00007346 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00007347}
7348
Chris Lattner19e90552006-04-14 05:19:18 +00007349/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
7350/// is a shuffle we can handle in a single instruction, return it. Otherwise,
7351/// return the code it can be lowered into. Worst case, it can always be
7352/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007353SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007354 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007355 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007356 SDValue V1 = Op.getOperand(0);
7357 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007358 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007359 EVT VT = Op.getValueType();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007360 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00007361
Nemanja Ivanovic1a2b2f02016-05-04 16:04:02 +00007362 if (Subtarget.hasVSX()) {
7363 if (V2.isUndef() && PPC::isSplatShuffleMask(SVOp, 4)) {
7364 int SplatIdx = PPC::getVSPLTImmediate(SVOp, 4, DAG);
7365 SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
7366 SDValue Splat = DAG.getNode(PPCISD::XXSPLT, dl, MVT::v4i32, Conv,
7367 DAG.getConstant(SplatIdx, dl, MVT::i32));
7368 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Splat);
7369 }
7370 }
7371
Hal Finkelc93a9a22015-02-25 01:06:45 +00007372 if (Subtarget.hasQPX()) {
7373 if (VT.getVectorNumElements() != 4)
7374 return SDValue();
7375
Sanjay Patel57195842016-03-14 17:28:46 +00007376 if (V2.isUndef()) V2 = V1;
Hal Finkelc93a9a22015-02-25 01:06:45 +00007377
7378 int AlignIdx = PPC::isQVALIGNIShuffleMask(SVOp);
7379 if (AlignIdx != -1) {
7380 return DAG.getNode(PPCISD::QVALIGNI, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007381 DAG.getConstant(AlignIdx, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007382 } else if (SVOp->isSplat()) {
7383 int SplatIdx = SVOp->getSplatIndex();
7384 if (SplatIdx >= 4) {
7385 std::swap(V1, V2);
7386 SplatIdx -= 4;
7387 }
7388
Hal Finkelc93a9a22015-02-25 01:06:45 +00007389 return DAG.getNode(PPCISD::QVESPLATI, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007390 DAG.getConstant(SplatIdx, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007391 }
7392
7393 // Lower this into a qvgpci/qvfperm pair.
7394
7395 // Compute the qvgpci literal
7396 unsigned idx = 0;
7397 for (unsigned i = 0; i < 4; ++i) {
7398 int m = SVOp->getMaskElt(i);
7399 unsigned mm = m >= 0 ? (unsigned) m : i;
7400 idx |= mm << (3-i)*3;
7401 }
7402
7403 SDValue V3 = DAG.getNode(PPCISD::QVGPCI, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007404 DAG.getConstant(idx, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007405 return DAG.getNode(PPCISD::QVFPERM, dl, VT, V1, V2, V3);
7406 }
7407
Chris Lattner19e90552006-04-14 05:19:18 +00007408 // Cases that are handled by instructions that take permute immediates
7409 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
7410 // selected by the instruction selector.
Sanjay Patel57195842016-03-14 17:28:46 +00007411 if (V2.isUndef()) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007412 if (PPC::isSplatShuffleMask(SVOp, 1) ||
7413 PPC::isSplatShuffleMask(SVOp, 2) ||
7414 PPC::isSplatShuffleMask(SVOp, 4) ||
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00007415 PPC::isVPKUWUMShuffleMask(SVOp, 1, DAG) ||
7416 PPC::isVPKUHUMShuffleMask(SVOp, 1, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00007417 PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00007418 PPC::isVMRGLShuffleMask(SVOp, 1, 1, DAG) ||
7419 PPC::isVMRGLShuffleMask(SVOp, 2, 1, DAG) ||
7420 PPC::isVMRGLShuffleMask(SVOp, 4, 1, DAG) ||
7421 PPC::isVMRGHShuffleMask(SVOp, 1, 1, DAG) ||
7422 PPC::isVMRGHShuffleMask(SVOp, 2, 1, DAG) ||
Kit Barton13894c72015-06-25 15:17:40 +00007423 PPC::isVMRGHShuffleMask(SVOp, 4, 1, DAG) ||
Hal Finkel77c8b7f2015-09-02 16:52:37 +00007424 (Subtarget.hasP8Altivec() && (
7425 PPC::isVPKUDUMShuffleMask(SVOp, 1, DAG) ||
7426 PPC::isVMRGEOShuffleMask(SVOp, true, 1, DAG) ||
7427 PPC::isVMRGEOShuffleMask(SVOp, false, 1, DAG)))) {
Chris Lattner19e90552006-04-14 05:19:18 +00007428 return Op;
7429 }
7430 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007431
Chris Lattner19e90552006-04-14 05:19:18 +00007432 // Altivec has a variety of "shuffle immediates" that take two vector inputs
7433 // and produce a fixed permutation. If any of these match, do not lower to
7434 // VPERM.
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00007435 unsigned int ShuffleKind = isLittleEndian ? 2 : 0;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00007436 if (PPC::isVPKUWUMShuffleMask(SVOp, ShuffleKind, DAG) ||
7437 PPC::isVPKUHUMShuffleMask(SVOp, ShuffleKind, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00007438 PPC::isVSLDOIShuffleMask(SVOp, ShuffleKind, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00007439 PPC::isVMRGLShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
7440 PPC::isVMRGLShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
7441 PPC::isVMRGLShuffleMask(SVOp, 4, ShuffleKind, DAG) ||
7442 PPC::isVMRGHShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
7443 PPC::isVMRGHShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
Kit Barton13894c72015-06-25 15:17:40 +00007444 PPC::isVMRGHShuffleMask(SVOp, 4, ShuffleKind, DAG) ||
Hal Finkel77c8b7f2015-09-02 16:52:37 +00007445 (Subtarget.hasP8Altivec() && (
7446 PPC::isVPKUDUMShuffleMask(SVOp, ShuffleKind, DAG) ||
7447 PPC::isVMRGEOShuffleMask(SVOp, true, ShuffleKind, DAG) ||
7448 PPC::isVMRGEOShuffleMask(SVOp, false, ShuffleKind, DAG))))
Chris Lattner19e90552006-04-14 05:19:18 +00007449 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007450
Chris Lattner071ad012006-04-17 05:28:54 +00007451 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
7452 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00007453 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00007454
Chris Lattner071ad012006-04-17 05:28:54 +00007455 unsigned PFIndexes[4];
7456 bool isFourElementShuffle = true;
7457 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
7458 unsigned EltNo = 8; // Start out undef.
7459 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007460 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00007461 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007462
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007463 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00007464 if ((ByteSource & 3) != j) {
7465 isFourElementShuffle = false;
7466 break;
7467 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007468
Chris Lattner071ad012006-04-17 05:28:54 +00007469 if (EltNo == 8) {
7470 EltNo = ByteSource/4;
7471 } else if (EltNo != ByteSource/4) {
7472 isFourElementShuffle = false;
7473 break;
7474 }
7475 }
7476 PFIndexes[i] = EltNo;
7477 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007478
7479 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00007480 // perfect shuffle vector to determine if it is cost effective to do this as
7481 // discrete instructions, or whether we should use a vperm.
Bill Schmidtf910a062014-06-10 14:35:01 +00007482 // For now, we skip this for little endian until such time as we have a
7483 // little-endian perfect shuffle table.
7484 if (isFourElementShuffle && !isLittleEndian) {
Chris Lattner071ad012006-04-17 05:28:54 +00007485 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007486 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00007487 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007488
Chris Lattner071ad012006-04-17 05:28:54 +00007489 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
7490 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007491
Chris Lattner071ad012006-04-17 05:28:54 +00007492 // Determining when to avoid vperm is tricky. Many things affect the cost
7493 // of vperm, particularly how many times the perm mask needs to be computed.
7494 // For example, if the perm mask can be hoisted out of a loop or is already
7495 // used (perhaps because there are multiple permutes with the same shuffle
7496 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
7497 // the loop requires an extra register.
7498 //
7499 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00007500 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00007501 // available, if this block is within a loop, we should avoid using vperm
7502 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007503 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007504 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007505 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007506
Chris Lattner19e90552006-04-14 05:19:18 +00007507 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
7508 // vector that will get spilled to the constant pool.
Sanjay Patel57195842016-03-14 17:28:46 +00007509 if (V2.isUndef()) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007510
Chris Lattner19e90552006-04-14 05:19:18 +00007511 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
7512 // that it is in input element units, not in bytes. Convert now.
Bill Schmidt4aedff82014-06-06 14:06:26 +00007513
7514 // For little endian, the order of the input vectors is reversed, and
7515 // the permutation mask is complemented with respect to 31. This is
7516 // necessary to produce proper semantics with the big-endian-biased vperm
7517 // instruction.
Owen Anderson53aa7a92009-08-10 22:56:29 +00007518 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00007519 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007520
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007521 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007522 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
7523 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007524
Chris Lattner19e90552006-04-14 05:19:18 +00007525 for (unsigned j = 0; j != BytesPerElement; ++j)
Bill Schmidt4aedff82014-06-06 14:06:26 +00007526 if (isLittleEndian)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007527 ResultMask.push_back(DAG.getConstant(31 - (SrcElt*BytesPerElement + j),
7528 dl, MVT::i32));
Bill Schmidt4aedff82014-06-06 14:06:26 +00007529 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007530 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement + j, dl,
Bill Schmidt4aedff82014-06-06 14:06:26 +00007531 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00007532 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007533
Ahmed Bougacha128f8732016-04-26 21:15:30 +00007534 SDValue VPermMask = DAG.getBuildVector(MVT::v16i8, dl, ResultMask);
Bill Schmidt4aedff82014-06-06 14:06:26 +00007535 if (isLittleEndian)
7536 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
7537 V2, V1, VPermMask);
7538 else
7539 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
7540 V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00007541}
7542
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +00007543/// getVectorCompareInfo - Given an intrinsic, return false if it is not a
7544/// vector comparison. If it is, return true and fill in Opc/isDot with
Chris Lattner9754d142006-04-18 17:59:36 +00007545/// information about the intrinsic.
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +00007546static bool getVectorCompareInfo(SDValue Intrin, int &CompareOpc,
7547 bool &isDot, const PPCSubtarget &Subtarget) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00007548 unsigned IntrinsicID =
7549 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00007550 CompareOpc = -1;
7551 isDot = false;
7552 switch (IntrinsicID) {
7553 default: return false;
7554 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00007555 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
7556 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
7557 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
7558 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
7559 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007560 case Intrinsic::ppc_altivec_vcmpequd_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007561 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007562 CompareOpc = 199;
7563 isDot = 1;
7564 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007565 return false;
7566
7567 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007568 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
7569 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
7570 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
7571 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
7572 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007573 case Intrinsic::ppc_altivec_vcmpgtsd_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007574 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007575 CompareOpc = 967;
7576 isDot = 1;
7577 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007578 return false;
7579
7580 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007581 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
7582 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
7583 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007584 case Intrinsic::ppc_altivec_vcmpgtud_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007585 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007586 CompareOpc = 711;
7587 isDot = 1;
7588 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007589 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007590
Kit Barton0cfa7b72015-03-03 19:55:45 +00007591 break;
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +00007592 // VSX predicate comparisons use the same infrastructure
7593 case Intrinsic::ppc_vsx_xvcmpeqdp_p:
7594 case Intrinsic::ppc_vsx_xvcmpgedp_p:
7595 case Intrinsic::ppc_vsx_xvcmpgtdp_p:
7596 case Intrinsic::ppc_vsx_xvcmpeqsp_p:
7597 case Intrinsic::ppc_vsx_xvcmpgesp_p:
7598 case Intrinsic::ppc_vsx_xvcmpgtsp_p:
7599 if (Subtarget.hasVSX()) {
7600 switch (IntrinsicID) {
7601 case Intrinsic::ppc_vsx_xvcmpeqdp_p: CompareOpc = 99; break;
7602 case Intrinsic::ppc_vsx_xvcmpgedp_p: CompareOpc = 115; break;
7603 case Intrinsic::ppc_vsx_xvcmpgtdp_p: CompareOpc = 107; break;
7604 case Intrinsic::ppc_vsx_xvcmpeqsp_p: CompareOpc = 67; break;
7605 case Intrinsic::ppc_vsx_xvcmpgesp_p: CompareOpc = 83; break;
7606 case Intrinsic::ppc_vsx_xvcmpgtsp_p: CompareOpc = 75; break;
7607 }
7608 isDot = 1;
7609 }
7610 else
7611 return false;
7612
7613 break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007614
Chris Lattner4211ca92006-04-14 06:01:58 +00007615 // Normal Comparisons.
7616 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
7617 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
7618 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
7619 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
7620 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007621 case Intrinsic::ppc_altivec_vcmpequd:
7622 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007623 CompareOpc = 199;
7624 isDot = 0;
7625 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007626 return false;
7627
7628 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007629 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
7630 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
7631 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
7632 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
7633 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007634 case Intrinsic::ppc_altivec_vcmpgtsd:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007635 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007636 CompareOpc = 967;
7637 isDot = 0;
7638 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007639 return false;
7640
7641 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007642 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
7643 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
7644 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007645 case Intrinsic::ppc_altivec_vcmpgtud:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007646 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007647 CompareOpc = 711;
7648 isDot = 0;
7649 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007650 return false;
7651
7652 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007653 }
Chris Lattner9754d142006-04-18 17:59:36 +00007654 return true;
7655}
7656
7657/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
7658/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007659SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007660 SelectionDAG &DAG) const {
Marcin Koscielnicki0cfb6122016-04-26 10:37:22 +00007661 unsigned IntrinsicID =
7662 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7663
7664 if (IntrinsicID == Intrinsic::thread_pointer) {
7665 // Reads the thread pointer register, used for __builtin_thread_pointer.
7666 bool is64bit = Subtarget.isPPC64();
7667 return DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
7668 is64bit ? MVT::i64 : MVT::i32);
7669 }
7670
Chris Lattner9754d142006-04-18 17:59:36 +00007671 // If this is a lowered altivec predicate compare, CompareOpc is set to the
7672 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00007673 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00007674 int CompareOpc;
7675 bool isDot;
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +00007676 if (!getVectorCompareInfo(Op, CompareOpc, isDot, Subtarget))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007677 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007678
Chris Lattner9754d142006-04-18 17:59:36 +00007679 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00007680 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00007681 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00007682 Op.getOperand(1), Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007683 DAG.getConstant(CompareOpc, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00007684 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00007685 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007686
Chris Lattner4211ca92006-04-14 06:01:58 +00007687 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007688 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007689 Op.getOperand(2), // LHS
7690 Op.getOperand(3), // RHS
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007691 DAG.getConstant(CompareOpc, dl, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007692 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00007693 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00007694 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007695
Chris Lattner4211ca92006-04-14 06:01:58 +00007696 // Now that we have the comparison, emit a copy from the CR to a GPR.
7697 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00007698 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00007699 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00007700 CompNode.getValue(1));
7701
Chris Lattner4211ca92006-04-14 06:01:58 +00007702 // Unpack the result based on how the target uses it.
7703 unsigned BitNo; // Bit # of CR6.
7704 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00007705 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00007706 default: // Can't happen, don't crash on invalid number though.
7707 case 0: // Return the value of the EQ bit of CR6.
7708 BitNo = 0; InvertBit = false;
7709 break;
7710 case 1: // Return the inverted value of the EQ bit of CR6.
7711 BitNo = 0; InvertBit = true;
7712 break;
7713 case 2: // Return the value of the LT bit of CR6.
7714 BitNo = 2; InvertBit = false;
7715 break;
7716 case 3: // Return the inverted value of the LT bit of CR6.
7717 BitNo = 2; InvertBit = true;
7718 break;
7719 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007720
Chris Lattner4211ca92006-04-14 06:01:58 +00007721 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00007722 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007723 DAG.getConstant(8 - (3 - BitNo), dl, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00007724 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00007725 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007726 DAG.getConstant(1, dl, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00007727
Chris Lattner4211ca92006-04-14 06:01:58 +00007728 // If we are supposed to, toggle the bit.
7729 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00007730 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007731 DAG.getConstant(1, dl, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00007732 return Flags;
7733}
7734
Hal Finkel5c0d1452014-03-30 13:22:59 +00007735SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
7736 SelectionDAG &DAG) const {
7737 SDLoc dl(Op);
7738 // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int
7739 // instructions), but for smaller types, we need to first extend up to v2i32
7740 // before doing going farther.
7741 if (Op.getValueType() == MVT::v2i64) {
7742 EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
7743 if (ExtVT != MVT::v2i32) {
7744 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0));
7745 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op,
7746 DAG.getValueType(EVT::getVectorVT(*DAG.getContext(),
7747 ExtVT.getVectorElementType(), 4)));
7748 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op);
7749 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op,
7750 DAG.getValueType(MVT::v2i32));
7751 }
7752
7753 return Op;
7754 }
7755
7756 return SDValue();
7757}
7758
Scott Michelcf0da6c2009-02-17 22:15:04 +00007759SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007760 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007761 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00007762 // Create a stack slot that is 16-byte aligned.
7763 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00007764 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00007765 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007766 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007767
Chris Lattner4211ca92006-04-14 06:01:58 +00007768 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00007769 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00007770 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00007771 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00007772 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00007773 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00007774 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00007775}
7776
Hal Finkelc93a9a22015-02-25 01:06:45 +00007777SDValue PPCTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7778 SelectionDAG &DAG) const {
7779 SDLoc dl(Op);
7780 SDNode *N = Op.getNode();
7781
7782 assert(N->getOperand(0).getValueType() == MVT::v4i1 &&
7783 "Unknown extract_vector_elt type");
7784
7785 SDValue Value = N->getOperand(0);
7786
7787 // The first part of this is like the store lowering except that we don't
7788 // need to track the chain.
7789
7790 // The values are now known to be -1 (false) or 1 (true). To convert this
7791 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
7792 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
7793 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
7794
7795 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to
7796 // understand how to form the extending load.
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007797 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007798
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007799 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007800
7801 // Now convert to an integer and store.
7802 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007803 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32),
Hal Finkelc93a9a22015-02-25 01:06:45 +00007804 Value);
7805
7806 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
7807 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Alex Lorenze40c8a22015-08-11 23:09:45 +00007808 MachinePointerInfo PtrInfo =
7809 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Mehdi Amini44ede332015-07-09 02:09:04 +00007810 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007811 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
7812
7813 SDValue StoreChain = DAG.getEntryNode();
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007814 SDValue Ops[] = {StoreChain,
7815 DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32),
7816 Value, FIdx};
7817 SDVTList VTs = DAG.getVTList(/*chain*/ MVT::Other);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007818
7819 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID,
7820 dl, VTs, Ops, MVT::v4i32, PtrInfo);
7821
7822 // Extract the value requested.
7823 unsigned Offset = 4*cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007824 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007825 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
7826
7827 SDValue IntVal = DAG.getLoad(MVT::i32, dl, StoreChain, Idx,
7828 PtrInfo.getWithOffset(Offset),
7829 false, false, false, 0);
7830
7831 if (!Subtarget.useCRBits())
7832 return IntVal;
7833
7834 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, IntVal);
7835}
7836
7837/// Lowering for QPX v4i1 loads
7838SDValue PPCTargetLowering::LowerVectorLoad(SDValue Op,
7839 SelectionDAG &DAG) const {
7840 SDLoc dl(Op);
7841 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
7842 SDValue LoadChain = LN->getChain();
7843 SDValue BasePtr = LN->getBasePtr();
7844
7845 if (Op.getValueType() == MVT::v4f64 ||
7846 Op.getValueType() == MVT::v4f32) {
7847 EVT MemVT = LN->getMemoryVT();
7848 unsigned Alignment = LN->getAlignment();
7849
7850 // If this load is properly aligned, then it is legal.
7851 if (Alignment >= MemVT.getStoreSize())
7852 return Op;
7853
7854 EVT ScalarVT = Op.getValueType().getScalarType(),
7855 ScalarMemVT = MemVT.getScalarType();
7856 unsigned Stride = ScalarMemVT.getStoreSize();
7857
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007858 SDValue Vals[4], LoadChains[4];
Hal Finkelc93a9a22015-02-25 01:06:45 +00007859 for (unsigned Idx = 0; Idx < 4; ++Idx) {
7860 SDValue Load;
7861 if (ScalarVT != ScalarMemVT)
7862 Load =
7863 DAG.getExtLoad(LN->getExtensionType(), dl, ScalarVT, LoadChain,
7864 BasePtr,
7865 LN->getPointerInfo().getWithOffset(Idx*Stride),
7866 ScalarMemVT, LN->isVolatile(), LN->isNonTemporal(),
7867 LN->isInvariant(), MinAlign(Alignment, Idx*Stride),
7868 LN->getAAInfo());
7869 else
7870 Load =
7871 DAG.getLoad(ScalarVT, dl, LoadChain, BasePtr,
7872 LN->getPointerInfo().getWithOffset(Idx*Stride),
7873 LN->isVolatile(), LN->isNonTemporal(),
7874 LN->isInvariant(), MinAlign(Alignment, Idx*Stride),
7875 LN->getAAInfo());
7876
7877 if (Idx == 0 && LN->isIndexed()) {
7878 assert(LN->getAddressingMode() == ISD::PRE_INC &&
7879 "Unknown addressing mode on vector load");
7880 Load = DAG.getIndexedLoad(Load, dl, BasePtr, LN->getOffset(),
7881 LN->getAddressingMode());
7882 }
7883
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007884 Vals[Idx] = Load;
7885 LoadChains[Idx] = Load.getValue(1);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007886
7887 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007888 DAG.getConstant(Stride, dl,
7889 BasePtr.getValueType()));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007890 }
7891
7892 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00007893 SDValue Value = DAG.getBuildVector(Op.getValueType(), dl, Vals);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007894
7895 if (LN->isIndexed()) {
7896 SDValue RetOps[] = { Value, Vals[0].getValue(1), TF };
7897 return DAG.getMergeValues(RetOps, dl);
7898 }
7899
7900 SDValue RetOps[] = { Value, TF };
7901 return DAG.getMergeValues(RetOps, dl);
7902 }
7903
7904 assert(Op.getValueType() == MVT::v4i1 && "Unknown load to lower");
7905 assert(LN->isUnindexed() && "Indexed v4i1 loads are not supported");
7906
7907 // To lower v4i1 from a byte array, we load the byte elements of the
7908 // vector and then reuse the BUILD_VECTOR logic.
7909
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007910 SDValue VectElmts[4], VectElmtChains[4];
Hal Finkelc93a9a22015-02-25 01:06:45 +00007911 for (unsigned i = 0; i < 4; ++i) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007912 SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007913 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx);
7914
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007915 VectElmts[i] = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::i32, LoadChain, Idx,
7916 LN->getPointerInfo().getWithOffset(i),
7917 MVT::i8 /* memory type */, LN->isVolatile(),
7918 LN->isNonTemporal(), LN->isInvariant(),
7919 1 /* alignment */, LN->getAAInfo());
7920 VectElmtChains[i] = VectElmts[i].getValue(1);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007921 }
7922
7923 LoadChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, VectElmtChains);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00007924 SDValue Value = DAG.getBuildVector(MVT::v4i1, dl, VectElmts);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007925
7926 SDValue RVals[] = { Value, LoadChain };
7927 return DAG.getMergeValues(RVals, dl);
7928}
7929
7930/// Lowering for QPX v4i1 stores
7931SDValue PPCTargetLowering::LowerVectorStore(SDValue Op,
7932 SelectionDAG &DAG) const {
7933 SDLoc dl(Op);
7934 StoreSDNode *SN = cast<StoreSDNode>(Op.getNode());
7935 SDValue StoreChain = SN->getChain();
7936 SDValue BasePtr = SN->getBasePtr();
7937 SDValue Value = SN->getValue();
7938
7939 if (Value.getValueType() == MVT::v4f64 ||
7940 Value.getValueType() == MVT::v4f32) {
7941 EVT MemVT = SN->getMemoryVT();
7942 unsigned Alignment = SN->getAlignment();
7943
7944 // If this store is properly aligned, then it is legal.
7945 if (Alignment >= MemVT.getStoreSize())
7946 return Op;
7947
7948 EVT ScalarVT = Value.getValueType().getScalarType(),
7949 ScalarMemVT = MemVT.getScalarType();
7950 unsigned Stride = ScalarMemVT.getStoreSize();
7951
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007952 SDValue Stores[4];
Hal Finkelc93a9a22015-02-25 01:06:45 +00007953 for (unsigned Idx = 0; Idx < 4; ++Idx) {
Mehdi Amini44ede332015-07-09 02:09:04 +00007954 SDValue Ex = DAG.getNode(
7955 ISD::EXTRACT_VECTOR_ELT, dl, ScalarVT, Value,
7956 DAG.getConstant(Idx, dl, getVectorIdxTy(DAG.getDataLayout())));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007957 SDValue Store;
7958 if (ScalarVT != ScalarMemVT)
7959 Store =
7960 DAG.getTruncStore(StoreChain, dl, Ex, BasePtr,
7961 SN->getPointerInfo().getWithOffset(Idx*Stride),
7962 ScalarMemVT, SN->isVolatile(), SN->isNonTemporal(),
7963 MinAlign(Alignment, Idx*Stride), SN->getAAInfo());
7964 else
7965 Store =
7966 DAG.getStore(StoreChain, dl, Ex, BasePtr,
7967 SN->getPointerInfo().getWithOffset(Idx*Stride),
7968 SN->isVolatile(), SN->isNonTemporal(),
7969 MinAlign(Alignment, Idx*Stride), SN->getAAInfo());
7970
7971 if (Idx == 0 && SN->isIndexed()) {
7972 assert(SN->getAddressingMode() == ISD::PRE_INC &&
7973 "Unknown addressing mode on vector store");
7974 Store = DAG.getIndexedStore(Store, dl, BasePtr, SN->getOffset(),
7975 SN->getAddressingMode());
7976 }
7977
7978 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007979 DAG.getConstant(Stride, dl,
7980 BasePtr.getValueType()));
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007981 Stores[Idx] = Store;
Hal Finkelc93a9a22015-02-25 01:06:45 +00007982 }
7983
7984 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
7985
7986 if (SN->isIndexed()) {
7987 SDValue RetOps[] = { TF, Stores[0].getValue(1) };
7988 return DAG.getMergeValues(RetOps, dl);
7989 }
7990
7991 return TF;
7992 }
7993
7994 assert(SN->isUnindexed() && "Indexed v4i1 stores are not supported");
7995 assert(Value.getValueType() == MVT::v4i1 && "Unknown store to lower");
7996
7997 // The values are now known to be -1 (false) or 1 (true). To convert this
7998 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
7999 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
8000 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
8001
8002 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to
8003 // understand how to form the extending load.
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00008004 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008005
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00008006 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008007
8008 // Now convert to an integer and store.
8009 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008010 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32),
Hal Finkelc93a9a22015-02-25 01:06:45 +00008011 Value);
8012
8013 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
8014 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Alex Lorenze40c8a22015-08-11 23:09:45 +00008015 MachinePointerInfo PtrInfo =
8016 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Mehdi Amini44ede332015-07-09 02:09:04 +00008017 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008018 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
8019
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008020 SDValue Ops[] = {StoreChain,
8021 DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32),
8022 Value, FIdx};
8023 SDVTList VTs = DAG.getVTList(/*chain*/ MVT::Other);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008024
8025 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID,
8026 dl, VTs, Ops, MVT::v4i32, PtrInfo);
8027
8028 // Move data into the byte array.
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008029 SDValue Loads[4], LoadChains[4];
Hal Finkelc93a9a22015-02-25 01:06:45 +00008030 for (unsigned i = 0; i < 4; ++i) {
8031 unsigned Offset = 4*i;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008032 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008033 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
8034
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008035 Loads[i] =
8036 DAG.getLoad(MVT::i32, dl, StoreChain, Idx,
8037 PtrInfo.getWithOffset(Offset), false, false, false, 0);
8038 LoadChains[i] = Loads[i].getValue(1);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008039 }
8040
8041 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8042
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008043 SDValue Stores[4];
Hal Finkelc93a9a22015-02-25 01:06:45 +00008044 for (unsigned i = 0; i < 4; ++i) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008045 SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008046 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx);
8047
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008048 Stores[i] = DAG.getTruncStore(
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00008049 StoreChain, dl, Loads[i], Idx, SN->getPointerInfo().getWithOffset(i),
8050 MVT::i8 /* memory type */, SN->isNonTemporal(), SN->isVolatile(),
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008051 1 /* alignment */, SN->getAAInfo());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008052 }
8053
8054 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
8055
8056 return StoreChain;
8057}
8058
Dan Gohman21cea8a2010-04-17 15:26:15 +00008059SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008060 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00008061 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008062 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008063
Owen Anderson9f944592009-08-11 20:47:22 +00008064 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
8065 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008066
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008067 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00008068 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008069
Chris Lattner7e4398742006-04-18 03:43:48 +00008070 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00008071 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
8072 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
8073 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008074
Chris Lattner7e4398742006-04-18 03:43:48 +00008075 // Low parts multiplied together, generating 32-bit results (we ignore the
8076 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008077 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00008078 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008079
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008080 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00008081 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00008082 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008083 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00008084 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00008085 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
8086 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008087 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008088
Owen Anderson9f944592009-08-11 20:47:22 +00008089 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00008090
Chris Lattner96d50482006-04-18 04:28:57 +00008091 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00008092 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00008093 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008094 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008095 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00008096
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00008097 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008098 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00008099 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00008100 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008101
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00008102 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008103 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00008104 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00008105 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008106
Bill Schmidt42995e82014-06-09 16:06:29 +00008107 // Merge the results together. Because vmuleub and vmuloub are
8108 // instructions with a big-endian bias, we must reverse the
8109 // element numbering and reverse the meaning of "odd" and "even"
8110 // when generating little endian code.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00008111 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00008112 for (unsigned i = 0; i != 8; ++i) {
Bill Schmidt42995e82014-06-09 16:06:29 +00008113 if (isLittleEndian) {
8114 Ops[i*2 ] = 2*i;
8115 Ops[i*2+1] = 2*i+16;
8116 } else {
8117 Ops[i*2 ] = 2*i+1;
8118 Ops[i*2+1] = 2*i+1+16;
8119 }
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00008120 }
Bill Schmidt42995e82014-06-09 16:06:29 +00008121 if (isLittleEndian)
8122 return DAG.getVectorShuffle(MVT::v16i8, dl, OddParts, EvenParts, Ops);
8123 else
8124 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00008125 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008126 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00008127 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00008128}
8129
Chris Lattnerf3d06c62005-08-26 00:52:45 +00008130/// LowerOperation - Provide custom lowering hooks for some operations.
8131///
Dan Gohman21cea8a2010-04-17 15:26:15 +00008132SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00008133 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008134 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00008135 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00008136 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00008137 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00008138 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00008139 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00008140 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00008141 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
8142 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008143 case ISD::VASTART:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008144 return LowerVASTART(Op, DAG, Subtarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008145
8146 case ISD::VAARG:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008147 return LowerVAARG(Op, DAG, Subtarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00008148
Roman Divackyc3825df2013-07-25 21:36:47 +00008149 case ISD::VACOPY:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008150 return LowerVACOPY(Op, DAG, Subtarget);
Roman Divackyc3825df2013-07-25 21:36:47 +00008151
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008152 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, Subtarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00008153 case ISD::DYNAMIC_STACKALLOC:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008154 return LowerDYNAMIC_STACKALLOC(Op, DAG, Subtarget);
Yury Gribovd7dbb662015-12-01 11:40:55 +00008155 case ISD::GET_DYNAMIC_AREA_OFFSET: return LowerGET_DYNAMIC_AREA_OFFSET(Op, DAG, Subtarget);
Evan Cheng51096af2008-04-19 01:30:48 +00008156
Hal Finkel756810f2013-03-21 21:37:52 +00008157 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
8158 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
8159
Hal Finkel940ab932014-02-28 00:27:01 +00008160 case ISD::LOAD: return LowerLOAD(Op, DAG);
8161 case ISD::STORE: return LowerSTORE(Op, DAG);
8162 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00008163 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00008164 case ISD::FP_TO_UINT:
8165 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Hal Finkeled844c42015-01-06 22:31:02 +00008166 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00008167 case ISD::UINT_TO_FP:
8168 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00008169 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00008170
Chris Lattner4211ca92006-04-14 06:01:58 +00008171 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00008172 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
8173 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
8174 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00008175
Chris Lattner4211ca92006-04-14 06:01:58 +00008176 // Vector-related lowering.
8177 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
8178 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8179 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
8180 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Hal Finkel5c0d1452014-03-30 13:22:59 +00008181 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008182 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00008183 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008184
Hal Finkel25c19922013-05-15 21:37:41 +00008185 // For counter-based loop handling.
8186 case ISD::INTRINSIC_W_CHAIN: return SDValue();
8187
Chris Lattnerf6a81562007-12-08 06:59:59 +00008188 // Frame & Return address.
8189 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008190 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00008191 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00008192}
8193
Duncan Sands6ed40142008-12-01 11:39:25 +00008194void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
8195 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00008196 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008197 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00008198 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00008199 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008200 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkelbbdee932014-12-02 22:01:00 +00008201 case ISD::READCYCLECOUNTER: {
8202 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
8203 SDValue RTB = DAG.getNode(PPCISD::READ_TIME_BASE, dl, VTs, N->getOperand(0));
8204
8205 Results.push_back(RTB);
8206 Results.push_back(RTB.getValue(1));
8207 Results.push_back(RTB.getValue(2));
8208 break;
8209 }
Hal Finkel25c19922013-05-15 21:37:41 +00008210 case ISD::INTRINSIC_W_CHAIN: {
8211 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
8212 Intrinsic::ppc_is_decremented_ctr_nonzero)
8213 break;
8214
8215 assert(N->getValueType(0) == MVT::i1 &&
8216 "Unexpected result type for CTR decrement intrinsic");
Mehdi Amini44ede332015-07-09 02:09:04 +00008217 EVT SVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
8218 N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00008219 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
8220 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00008221 N->getOperand(1));
Hal Finkel25c19922013-05-15 21:37:41 +00008222
8223 Results.push_back(NewInt);
8224 Results.push_back(NewInt.getValue(1));
8225 break;
8226 }
Roman Divacky4394e682011-06-28 15:30:42 +00008227 case ISD::VAARG: {
Eric Christophercccae792015-01-30 22:02:31 +00008228 if (!Subtarget.isSVR4ABI() || Subtarget.isPPC64())
Roman Divacky4394e682011-06-28 15:30:42 +00008229 return;
8230
8231 EVT VT = N->getValueType(0);
8232
8233 if (VT == MVT::i64) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008234 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, Subtarget);
Roman Divacky4394e682011-06-28 15:30:42 +00008235
8236 Results.push_back(NewNode);
8237 Results.push_back(NewNode.getValue(1));
8238 }
8239 return;
8240 }
Duncan Sands6ed40142008-12-01 11:39:25 +00008241 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00008242 assert(N->getValueType(0) == MVT::ppcf128);
8243 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008244 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00008245 MVT::f64, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008246 DAG.getIntPtrConstant(0, dl));
Dale Johannesenf80493b2009-02-05 22:07:54 +00008247 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00008248 MVT::f64, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008249 DAG.getIntPtrConstant(1, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00008250
Ulrich Weigand874fc622013-03-26 10:56:22 +00008251 // Add the two halves of the long double in round-to-zero mode.
8252 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00008253
8254 // We know the low half is about to be thrown away, so just use something
8255 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00008256 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00008257 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00008258 return;
Duncan Sands2a287912008-07-19 16:26:02 +00008259 }
Duncan Sands6ed40142008-12-01 11:39:25 +00008260 case ISD::FP_TO_SINT:
Hal Finkel93138502015-04-10 03:39:00 +00008261 case ISD::FP_TO_UINT:
Bill Schmidt41221692013-07-09 18:50:20 +00008262 // LowerFP_TO_INT() can only handle f32 and f64.
8263 if (N->getOperand(0).getValueType() == MVT::ppcf128)
8264 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00008265 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00008266 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00008267 }
8268}
8269
Chris Lattner4211ca92006-04-14 06:01:58 +00008270//===----------------------------------------------------------------------===//
8271// Other Lowering Code
8272//===----------------------------------------------------------------------===//
8273
Robin Morisset22129962014-09-23 20:46:49 +00008274static Instruction* callIntrinsic(IRBuilder<> &Builder, Intrinsic::ID Id) {
8275 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
8276 Function *Func = Intrinsic::getDeclaration(M, Id);
David Blaikieff6409d2015-05-18 22:13:54 +00008277 return Builder.CreateCall(Func, {});
Robin Morisset22129962014-09-23 20:46:49 +00008278}
8279
8280// The mappings for emitLeading/TrailingFence is taken from
8281// http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
8282Instruction* PPCTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
8283 AtomicOrdering Ord, bool IsStore,
8284 bool IsLoad) const {
JF Bastien800f87a2016-04-06 21:19:33 +00008285 if (Ord == AtomicOrdering::SequentiallyConsistent)
Robin Morisset22129962014-09-23 20:46:49 +00008286 return callIntrinsic(Builder, Intrinsic::ppc_sync);
JF Bastien800f87a2016-04-06 21:19:33 +00008287 if (isReleaseOrStronger(Ord))
Robin Morisset22129962014-09-23 20:46:49 +00008288 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
David Blaikieff6409d2015-05-18 22:13:54 +00008289 return nullptr;
Robin Morisset22129962014-09-23 20:46:49 +00008290}
8291
8292Instruction* PPCTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
8293 AtomicOrdering Ord, bool IsStore,
8294 bool IsLoad) const {
JF Bastien800f87a2016-04-06 21:19:33 +00008295 if (IsLoad && isAcquireOrStronger(Ord))
Robin Morisset22129962014-09-23 20:46:49 +00008296 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
8297 // FIXME: this is too conservative, a dependent branch + isync is enough.
8298 // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html and
8299 // http://www.rdrop.com/users/paulmck/scalability/paper/N2745r.2011.03.04a.html
8300 // and http://www.cl.cam.ac.uk/~pes20/cppppc/ for justification.
David Blaikieff6409d2015-05-18 22:13:54 +00008301 return nullptr;
Robin Morisset22129962014-09-23 20:46:49 +00008302}
8303
Chris Lattner9b577f12005-08-26 21:23:58 +00008304MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00008305PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008306 unsigned AtomicSize,
8307 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008308 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christophercccae792015-01-30 22:02:31 +00008309 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Dale Johannesend4eb0522008-08-25 22:34:37 +00008310
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008311 auto LoadMnemonic = PPC::LDARX;
8312 auto StoreMnemonic = PPC::STDCX;
8313 switch (AtomicSize) {
8314 default:
8315 llvm_unreachable("Unexpected size of atomic entity");
8316 case 1:
8317 LoadMnemonic = PPC::LBARX;
8318 StoreMnemonic = PPC::STBCX;
8319 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4");
8320 break;
8321 case 2:
8322 LoadMnemonic = PPC::LHARX;
8323 StoreMnemonic = PPC::STHCX;
8324 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4");
8325 break;
8326 case 4:
8327 LoadMnemonic = PPC::LWARX;
8328 StoreMnemonic = PPC::STWCX;
8329 break;
8330 case 8:
8331 LoadMnemonic = PPC::LDARX;
8332 StoreMnemonic = PPC::STDCX;
8333 break;
8334 }
8335
Dale Johannesend4eb0522008-08-25 22:34:37 +00008336 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8337 MachineFunction *F = BB->getParent();
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00008338 MachineFunction::iterator It = ++BB->getIterator();
Dale Johannesend4eb0522008-08-25 22:34:37 +00008339
8340 unsigned dest = MI->getOperand(0).getReg();
8341 unsigned ptrA = MI->getOperand(1).getReg();
8342 unsigned ptrB = MI->getOperand(2).getReg();
8343 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00008344 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00008345
8346 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
8347 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
8348 F->insert(It, loopMBB);
8349 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008350 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008351 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008352 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008353
8354 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008355 unsigned TmpReg = (!BinOpcode) ? incr :
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008356 RegInfo.createVirtualRegister( AtomicSize == 8 ? &PPC::G8RCRegClass
Craig Topper61e88f42014-11-21 05:58:21 +00008357 : &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008358
8359 // thisMBB:
8360 // ...
8361 // fallthrough --> loopMBB
8362 BB->addSuccessor(loopMBB);
8363
8364 // loopMBB:
8365 // l[wd]arx dest, ptr
8366 // add r0, dest, incr
8367 // st[wd]cx. r0, ptr
8368 // bne- loopMBB
8369 // fallthrough --> exitMBB
8370 BB = loopMBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008371 BuildMI(BB, dl, TII->get(LoadMnemonic), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00008372 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008373 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008374 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008375 BuildMI(BB, dl, TII->get(StoreMnemonic))
Dale Johannesend4eb0522008-08-25 22:34:37 +00008376 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008377 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00008378 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008379 BB->addSuccessor(loopMBB);
8380 BB->addSuccessor(exitMBB);
8381
8382 // exitMBB:
8383 // ...
8384 BB = exitMBB;
8385 return BB;
8386}
8387
8388MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00008389PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00008390 MachineBasicBlock *BB,
8391 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00008392 unsigned BinOpcode) const {
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008393 // If we support part-word atomic mnemonics, just use them
8394 if (Subtarget.hasPartwordAtomics())
8395 return EmitAtomicBinary(MI, BB, is8bit ? 1 : 2, BinOpcode);
8396
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008397 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christophercccae792015-01-30 22:02:31 +00008398 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Dale Johannesena32affb2008-08-28 17:53:09 +00008399 // In 64 bit mode we have to use 64 bits for addresses, even though the
8400 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
8401 // registers without caring whether they're 32 or 64, but here we're
8402 // doing actual arithmetic on the addresses.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008403 bool is64bit = Subtarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00008404 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00008405
8406 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8407 MachineFunction *F = BB->getParent();
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00008408 MachineFunction::iterator It = ++BB->getIterator();
Dale Johannesena32affb2008-08-28 17:53:09 +00008409
8410 unsigned dest = MI->getOperand(0).getReg();
8411 unsigned ptrA = MI->getOperand(1).getReg();
8412 unsigned ptrB = MI->getOperand(2).getReg();
8413 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00008414 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00008415
8416 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
8417 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
8418 F->insert(It, loopMBB);
8419 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008420 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008421 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008422 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00008423
8424 MachineRegisterInfo &RegInfo = F->getRegInfo();
Craig Topper61e88f42014-11-21 05:58:21 +00008425 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass
8426 : &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00008427 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
8428 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
8429 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
8430 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
8431 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
8432 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
8433 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
8434 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
8435 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
8436 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008437 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00008438 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008439 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00008440
8441 // thisMBB:
8442 // ...
8443 // fallthrough --> loopMBB
8444 BB->addSuccessor(loopMBB);
8445
8446 // The 4-byte load must be aligned, while a char or short may be
8447 // anywhere in the word. Hence all this nasty bookkeeping code.
8448 // add ptr1, ptrA, ptrB [copy if ptrA==0]
8449 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00008450 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00008451 // rlwinm ptr, ptr1, 0, 0, 29
8452 // slw incr2, incr, shift
8453 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
8454 // slw mask, mask2, shift
8455 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00008456 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008457 // add tmp, tmpDest, incr2
8458 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00008459 // and tmp3, tmp, mask
8460 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00008461 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00008462 // bne- loopMBB
8463 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008464 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008465 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00008466 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008467 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008468 .addReg(ptrA).addReg(ptrB);
8469 } else {
8470 Ptr1Reg = ptrB;
8471 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008472 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008473 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008474 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008475 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
8476 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008477 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008478 .addReg(Ptr1Reg).addImm(0).addImm(61);
8479 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00008480 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008481 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008482 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008483 .addReg(incr).addReg(ShiftReg);
8484 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008485 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00008486 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00008487 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
8488 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00008489 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008490 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008491 .addReg(Mask2Reg).addReg(ShiftReg);
8492
8493 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00008494 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008495 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008496 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008497 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008498 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008499 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008500 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008501 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008502 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008503 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008504 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00008505 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008506 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008507 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00008508 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00008509 BB->addSuccessor(loopMBB);
8510 BB->addSuccessor(exitMBB);
8511
8512 // exitMBB:
8513 // ...
8514 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00008515 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
8516 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00008517 return BB;
8518}
8519
Hal Finkel756810f2013-03-21 21:37:52 +00008520llvm::MachineBasicBlock*
8521PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
8522 MachineBasicBlock *MBB) const {
8523 DebugLoc DL = MI->getDebugLoc();
Eric Christophercccae792015-01-30 22:02:31 +00008524 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00008525
8526 MachineFunction *MF = MBB->getParent();
8527 MachineRegisterInfo &MRI = MF->getRegInfo();
8528
8529 const BasicBlock *BB = MBB->getBasicBlock();
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00008530 MachineFunction::iterator I = ++MBB->getIterator();
Hal Finkel756810f2013-03-21 21:37:52 +00008531
8532 // Memory Reference
8533 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
8534 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
8535
8536 unsigned DstReg = MI->getOperand(0).getReg();
8537 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
8538 assert(RC->hasType(MVT::i32) && "Invalid destination!");
8539 unsigned mainDstReg = MRI.createVirtualRegister(RC);
8540 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
8541
Mehdi Amini44ede332015-07-09 02:09:04 +00008542 MVT PVT = getPointerTy(MF->getDataLayout());
Hal Finkel756810f2013-03-21 21:37:52 +00008543 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
8544 "Invalid Pointer Size!");
8545 // For v = setjmp(buf), we generate
8546 //
8547 // thisMBB:
8548 // SjLjSetup mainMBB
8549 // bl mainMBB
8550 // v_restore = 1
8551 // b sinkMBB
8552 //
8553 // mainMBB:
8554 // buf[LabelOffset] = LR
8555 // v_main = 0
8556 //
8557 // sinkMBB:
8558 // v = phi(main, restore)
8559 //
8560
8561 MachineBasicBlock *thisMBB = MBB;
8562 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
8563 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
8564 MF->insert(I, mainMBB);
8565 MF->insert(I, sinkMBB);
8566
8567 MachineInstrBuilder MIB;
8568
8569 // Transfer the remainder of BB and its successor edges to sinkMBB.
8570 sinkMBB->splice(sinkMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008571 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Hal Finkel756810f2013-03-21 21:37:52 +00008572 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
8573
8574 // Note that the structure of the jmp_buf used here is not compatible
8575 // with that used by libc, and is not designed to be. Specifically, it
8576 // stores only those 'reserved' registers that LLVM does not otherwise
8577 // understand how to spill. Also, by convention, by the time this
8578 // intrinsic is called, Clang has already stored the frame address in the
8579 // first slot of the buffer and stack address in the third. Following the
8580 // X86 target code, we'll store the jump address in the second slot. We also
8581 // need to save the TOC pointer (R2) to handle jumps between shared
8582 // libraries, and that will be stored in the fourth slot. The thread
8583 // identifier (R13) is not affected.
8584
8585 // thisMBB:
8586 const int64_t LabelOffset = 1 * PVT.getStoreSize();
8587 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00008588 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00008589
8590 // Prepare IP either in reg.
8591 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
8592 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
8593 unsigned BufReg = MI->getOperand(1).getReg();
8594
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008595 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI()) {
Hal Finkele6698d52015-02-01 15:03:28 +00008596 setUsesTOCBasePtr(*MBB->getParent());
Hal Finkel756810f2013-03-21 21:37:52 +00008597 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
8598 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008599 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008600 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00008601 MIB.setMemRefs(MMOBegin, MMOEnd);
8602 }
8603
Hal Finkelf05d6c72013-07-17 23:50:51 +00008604 // Naked functions never have a base pointer, and so we use r1. For all
8605 // other functions, this decision must be delayed until during PEI.
8606 unsigned BaseReg;
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +00008607 if (MF->getFunction()->hasFnAttribute(Attribute::Naked))
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008608 BaseReg = Subtarget.isPPC64() ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00008609 else
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008610 BaseReg = Subtarget.isPPC64() ? PPC::BP8 : PPC::BP;
Hal Finkelf05d6c72013-07-17 23:50:51 +00008611
8612 MIB = BuildMI(*thisMBB, MI, DL,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008613 TII->get(Subtarget.isPPC64() ? PPC::STD : PPC::STW))
Eric Christophercccae792015-01-30 22:02:31 +00008614 .addReg(BaseReg)
8615 .addImm(BPOffset)
8616 .addReg(BufReg);
Hal Finkelf05d6c72013-07-17 23:50:51 +00008617 MIB.setMemRefs(MMOBegin, MMOEnd);
8618
Hal Finkel756810f2013-03-21 21:37:52 +00008619 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00008620 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Eric Christophercccae792015-01-30 22:02:31 +00008621 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo();
Bill Wendling5e7656b2013-06-07 07:55:53 +00008622 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00008623
8624 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
8625
8626 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
8627 .addMBB(mainMBB);
8628 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
8629
Cong Hou1938f2e2015-11-24 08:51:23 +00008630 thisMBB->addSuccessor(mainMBB, BranchProbability::getZero());
8631 thisMBB->addSuccessor(sinkMBB, BranchProbability::getOne());
Hal Finkel756810f2013-03-21 21:37:52 +00008632
8633 // mainMBB:
8634 // mainDstReg = 0
Eric Christophercccae792015-01-30 22:02:31 +00008635 MIB =
8636 BuildMI(mainMBB, DL,
8637 TII->get(Subtarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
Hal Finkel756810f2013-03-21 21:37:52 +00008638
8639 // Store IP
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008640 if (Subtarget.isPPC64()) {
Hal Finkel756810f2013-03-21 21:37:52 +00008641 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
8642 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008643 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008644 .addReg(BufReg);
8645 } else {
8646 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
8647 .addReg(LabelReg)
8648 .addImm(LabelOffset)
8649 .addReg(BufReg);
8650 }
8651
8652 MIB.setMemRefs(MMOBegin, MMOEnd);
8653
8654 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
8655 mainMBB->addSuccessor(sinkMBB);
8656
8657 // sinkMBB:
8658 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
8659 TII->get(PPC::PHI), DstReg)
8660 .addReg(mainDstReg).addMBB(mainMBB)
8661 .addReg(restoreDstReg).addMBB(thisMBB);
8662
8663 MI->eraseFromParent();
8664 return sinkMBB;
8665}
8666
8667MachineBasicBlock *
8668PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
8669 MachineBasicBlock *MBB) const {
8670 DebugLoc DL = MI->getDebugLoc();
Eric Christophercccae792015-01-30 22:02:31 +00008671 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00008672
8673 MachineFunction *MF = MBB->getParent();
8674 MachineRegisterInfo &MRI = MF->getRegInfo();
8675
8676 // Memory Reference
8677 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
8678 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
8679
Mehdi Amini44ede332015-07-09 02:09:04 +00008680 MVT PVT = getPointerTy(MF->getDataLayout());
Hal Finkel756810f2013-03-21 21:37:52 +00008681 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
8682 "Invalid Pointer Size!");
8683
8684 const TargetRegisterClass *RC =
8685 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
8686 unsigned Tmp = MRI.createVirtualRegister(RC);
8687 // Since FP is only updated here but NOT referenced, it's treated as GPR.
8688 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
8689 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Eric Christophercccae792015-01-30 22:02:31 +00008690 unsigned BP =
8691 (PVT == MVT::i64)
8692 ? PPC::X30
Rafael Espindola21d22a02016-06-27 14:05:43 +00008693 : (Subtarget.isSVR4ABI() && isPositionIndependent() ? PPC::R29
8694 : PPC::R30);
Hal Finkel756810f2013-03-21 21:37:52 +00008695
8696 MachineInstrBuilder MIB;
8697
8698 const int64_t LabelOffset = 1 * PVT.getStoreSize();
8699 const int64_t SPOffset = 2 * PVT.getStoreSize();
8700 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00008701 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00008702
8703 unsigned BufReg = MI->getOperand(0).getReg();
8704
8705 // Reload FP (the jumped-to function may not have had a
8706 // frame pointer, and if so, then its r31 will be restored
8707 // as necessary).
8708 if (PVT == MVT::i64) {
8709 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
8710 .addImm(0)
8711 .addReg(BufReg);
8712 } else {
8713 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
8714 .addImm(0)
8715 .addReg(BufReg);
8716 }
8717 MIB.setMemRefs(MMOBegin, MMOEnd);
8718
8719 // Reload IP
8720 if (PVT == MVT::i64) {
8721 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008722 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008723 .addReg(BufReg);
8724 } else {
8725 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
8726 .addImm(LabelOffset)
8727 .addReg(BufReg);
8728 }
8729 MIB.setMemRefs(MMOBegin, MMOEnd);
8730
8731 // Reload SP
8732 if (PVT == MVT::i64) {
8733 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008734 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008735 .addReg(BufReg);
8736 } else {
8737 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
8738 .addImm(SPOffset)
8739 .addReg(BufReg);
8740 }
8741 MIB.setMemRefs(MMOBegin, MMOEnd);
8742
Hal Finkelf05d6c72013-07-17 23:50:51 +00008743 // Reload BP
8744 if (PVT == MVT::i64) {
8745 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
8746 .addImm(BPOffset)
8747 .addReg(BufReg);
8748 } else {
8749 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
8750 .addImm(BPOffset)
8751 .addReg(BufReg);
8752 }
8753 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00008754
8755 // Reload TOC
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008756 if (PVT == MVT::i64 && Subtarget.isSVR4ABI()) {
Hal Finkele6698d52015-02-01 15:03:28 +00008757 setUsesTOCBasePtr(*MBB->getParent());
Hal Finkel756810f2013-03-21 21:37:52 +00008758 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008759 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008760 .addReg(BufReg);
8761
8762 MIB.setMemRefs(MMOBegin, MMOEnd);
8763 }
8764
8765 // Jump
8766 BuildMI(*MBB, MI, DL,
8767 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
8768 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
8769
8770 MI->eraseFromParent();
8771 return MBB;
8772}
8773
Dale Johannesena32affb2008-08-28 17:53:09 +00008774MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00008775PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00008776 MachineBasicBlock *BB) const {
Hal Finkel934361a2015-01-14 01:07:51 +00008777 if (MI->getOpcode() == TargetOpcode::STACKMAP ||
Hal Finkelaf519932015-01-19 07:20:27 +00008778 MI->getOpcode() == TargetOpcode::PATCHPOINT) {
8779 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI() &&
8780 MI->getOpcode() == TargetOpcode::PATCHPOINT) {
8781 // Call lowering should have added an r2 operand to indicate a dependence
8782 // on the TOC base pointer value. It can't however, because there is no
8783 // way to mark the dependence as implicit there, and so the stackmap code
8784 // will confuse it with a regular operand. Instead, add the dependence
8785 // here.
Hal Finkele6698d52015-02-01 15:03:28 +00008786 setUsesTOCBasePtr(*BB->getParent());
Hal Finkelaf519932015-01-19 07:20:27 +00008787 MI->addOperand(MachineOperand::CreateReg(PPC::X2, false, true));
8788 }
8789
Hal Finkel934361a2015-01-14 01:07:51 +00008790 return emitPatchPoint(MI, BB);
Hal Finkelaf519932015-01-19 07:20:27 +00008791 }
Hal Finkel934361a2015-01-14 01:07:51 +00008792
Hal Finkel756810f2013-03-21 21:37:52 +00008793 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
8794 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
8795 return emitEHSjLjSetJmp(MI, BB);
8796 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
8797 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
8798 return emitEHSjLjLongJmp(MI, BB);
8799 }
8800
Eric Christophercccae792015-01-30 22:02:31 +00008801 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00008802
8803 // To "insert" these instructions we actually have to insert their
8804 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00008805 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00008806 MachineFunction::iterator It = ++BB->getIterator();
Evan Cheng32e376f2008-07-12 02:23:19 +00008807
Dan Gohman3b460302008-07-07 23:14:23 +00008808 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00008809
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008810 if (Subtarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Eric Christophercccae792015-01-30 22:02:31 +00008811 MI->getOpcode() == PPC::SELECT_CC_I8 ||
8812 MI->getOpcode() == PPC::SELECT_I4 ||
8813 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00008814 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00008815 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
8816 MI->getOpcode() == PPC::SELECT_CC_I8)
8817 Cond.push_back(MI->getOperand(4));
8818 else
8819 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00008820 Cond.push_back(MI->getOperand(1));
8821
Hal Finkel460e94d2012-06-22 23:10:08 +00008822 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00008823 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
8824 Cond, MI->getOperand(2).getReg(),
8825 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00008826 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
8827 MI->getOpcode() == PPC::SELECT_CC_I8 ||
8828 MI->getOpcode() == PPC::SELECT_CC_F4 ||
8829 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008830 MI->getOpcode() == PPC::SELECT_CC_QFRC ||
8831 MI->getOpcode() == PPC::SELECT_CC_QSRC ||
8832 MI->getOpcode() == PPC::SELECT_CC_QBRC ||
Hal Finkel940ab932014-02-28 00:27:01 +00008833 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00008834 MI->getOpcode() == PPC::SELECT_CC_VSFRC ||
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00008835 MI->getOpcode() == PPC::SELECT_CC_VSSRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008836 MI->getOpcode() == PPC::SELECT_CC_VSRC ||
Hal Finkel940ab932014-02-28 00:27:01 +00008837 MI->getOpcode() == PPC::SELECT_I4 ||
8838 MI->getOpcode() == PPC::SELECT_I8 ||
8839 MI->getOpcode() == PPC::SELECT_F4 ||
8840 MI->getOpcode() == PPC::SELECT_F8 ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008841 MI->getOpcode() == PPC::SELECT_QFRC ||
8842 MI->getOpcode() == PPC::SELECT_QSRC ||
8843 MI->getOpcode() == PPC::SELECT_QBRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008844 MI->getOpcode() == PPC::SELECT_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00008845 MI->getOpcode() == PPC::SELECT_VSFRC ||
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00008846 MI->getOpcode() == PPC::SELECT_VSSRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008847 MI->getOpcode() == PPC::SELECT_VSRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00008848 // The incoming instruction knows the destination vreg to set, the
8849 // condition code register to branch on, the true/false values to
8850 // select between, and a branch opcode to use.
8851
8852 // thisMBB:
8853 // ...
8854 // TrueVal = ...
8855 // cmpTY ccX, r1, r2
8856 // bCC copy1MBB
8857 // fallthrough --> copy0MBB
8858 MachineBasicBlock *thisMBB = BB;
8859 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8860 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008861 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00008862 F->insert(It, copy0MBB);
8863 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008864
8865 // Transfer the remainder of BB and its successor edges to sinkMBB.
8866 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008867 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008868 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8869
Evan Cheng32e376f2008-07-12 02:23:19 +00008870 // Next, add the true and fallthrough blocks as its successors.
8871 BB->addSuccessor(copy0MBB);
8872 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008873
Hal Finkel940ab932014-02-28 00:27:01 +00008874 if (MI->getOpcode() == PPC::SELECT_I4 ||
8875 MI->getOpcode() == PPC::SELECT_I8 ||
8876 MI->getOpcode() == PPC::SELECT_F4 ||
8877 MI->getOpcode() == PPC::SELECT_F8 ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008878 MI->getOpcode() == PPC::SELECT_QFRC ||
8879 MI->getOpcode() == PPC::SELECT_QSRC ||
8880 MI->getOpcode() == PPC::SELECT_QBRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008881 MI->getOpcode() == PPC::SELECT_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00008882 MI->getOpcode() == PPC::SELECT_VSFRC ||
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00008883 MI->getOpcode() == PPC::SELECT_VSSRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008884 MI->getOpcode() == PPC::SELECT_VSRC) {
Hal Finkel940ab932014-02-28 00:27:01 +00008885 BuildMI(BB, dl, TII->get(PPC::BC))
8886 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
8887 } else {
8888 unsigned SelectPred = MI->getOperand(4).getImm();
8889 BuildMI(BB, dl, TII->get(PPC::BCC))
8890 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
8891 }
Dan Gohman34396292010-07-06 20:24:04 +00008892
Evan Cheng32e376f2008-07-12 02:23:19 +00008893 // copy0MBB:
8894 // %FalseValue = ...
8895 // # fallthrough to sinkMBB
8896 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008897
Evan Cheng32e376f2008-07-12 02:23:19 +00008898 // Update machine-CFG edges
8899 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008900
Evan Cheng32e376f2008-07-12 02:23:19 +00008901 // sinkMBB:
8902 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8903 // ...
8904 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00008905 BuildMI(*BB, BB->begin(), dl,
8906 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00008907 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
8908 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Hal Finkelbbdee932014-12-02 22:01:00 +00008909 } else if (MI->getOpcode() == PPC::ReadTB) {
8910 // To read the 64-bit time-base register on a 32-bit target, we read the
8911 // two halves. Should the counter have wrapped while it was being read, we
8912 // need to try again.
8913 // ...
8914 // readLoop:
8915 // mfspr Rx,TBU # load from TBU
8916 // mfspr Ry,TB # load from TB
8917 // mfspr Rz,TBU # load from TBU
NAKAMURA Takumibf9cc7f2015-09-22 11:10:08 +00008918 // cmpw crX,Rx,Rz # check if 'old'='new'
Hal Finkelbbdee932014-12-02 22:01:00 +00008919 // bne readLoop # branch if they're not equal
8920 // ...
8921
8922 MachineBasicBlock *readMBB = F->CreateMachineBasicBlock(LLVM_BB);
8923 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8924 DebugLoc dl = MI->getDebugLoc();
8925 F->insert(It, readMBB);
8926 F->insert(It, sinkMBB);
8927
8928 // Transfer the remainder of BB and its successor edges to sinkMBB.
8929 sinkMBB->splice(sinkMBB->begin(), BB,
8930 std::next(MachineBasicBlock::iterator(MI)), BB->end());
8931 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8932
8933 BB->addSuccessor(readMBB);
8934 BB = readMBB;
8935
8936 MachineRegisterInfo &RegInfo = F->getRegInfo();
8937 unsigned ReadAgainReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
8938 unsigned LoReg = MI->getOperand(0).getReg();
8939 unsigned HiReg = MI->getOperand(1).getReg();
8940
8941 BuildMI(BB, dl, TII->get(PPC::MFSPR), HiReg).addImm(269);
8942 BuildMI(BB, dl, TII->get(PPC::MFSPR), LoReg).addImm(268);
8943 BuildMI(BB, dl, TII->get(PPC::MFSPR), ReadAgainReg).addImm(269);
8944
8945 unsigned CmpReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
8946
8947 BuildMI(BB, dl, TII->get(PPC::CMPW), CmpReg)
8948 .addReg(HiReg).addReg(ReadAgainReg);
8949 BuildMI(BB, dl, TII->get(PPC::BCC))
8950 .addImm(PPC::PRED_NE).addReg(CmpReg).addMBB(readMBB);
8951
8952 BB->addSuccessor(readMBB);
8953 BB->addSuccessor(sinkMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00008954 }
Dale Johannesena32affb2008-08-28 17:53:09 +00008955 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
8956 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
8957 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
8958 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008959 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008960 BB = EmitAtomicBinary(MI, BB, 4, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008961 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008962 BB = EmitAtomicBinary(MI, BB, 8, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008963
8964 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
8965 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
8966 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
8967 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008968 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008969 BB = EmitAtomicBinary(MI, BB, 4, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008970 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008971 BB = EmitAtomicBinary(MI, BB, 8, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008972
8973 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
8974 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
8975 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
8976 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008977 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008978 BB = EmitAtomicBinary(MI, BB, 4, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008979 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008980 BB = EmitAtomicBinary(MI, BB, 8, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008981
8982 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
8983 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
8984 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
8985 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008986 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008987 BB = EmitAtomicBinary(MI, BB, 4, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008988 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008989 BB = EmitAtomicBinary(MI, BB, 8, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008990
8991 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00008992 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::NAND);
Dale Johannesena32affb2008-08-28 17:53:09 +00008993 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00008994 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008995 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008996 BB = EmitAtomicBinary(MI, BB, 4, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008997 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008998 BB = EmitAtomicBinary(MI, BB, 8, PPC::NAND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008999
9000 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
9001 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
9002 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
9003 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009004 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009005 BB = EmitAtomicBinary(MI, BB, 4, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009006 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009007 BB = EmitAtomicBinary(MI, BB, 8, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00009008
Dale Johannesenf0a88d62008-08-29 18:29:46 +00009009 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
9010 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
9011 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
9012 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
9013 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009014 BB = EmitAtomicBinary(MI, BB, 4, 0);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00009015 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009016 BB = EmitAtomicBinary(MI, BB, 8, 0);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00009017
Evan Cheng32e376f2008-07-12 02:23:19 +00009018 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009019 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64 ||
9020 (Subtarget.hasPartwordAtomics() &&
9021 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8) ||
9022 (Subtarget.hasPartwordAtomics() &&
9023 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16)) {
Evan Cheng32e376f2008-07-12 02:23:19 +00009024 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
9025
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009026 auto LoadMnemonic = PPC::LDARX;
9027 auto StoreMnemonic = PPC::STDCX;
9028 switch(MI->getOpcode()) {
9029 default:
9030 llvm_unreachable("Compare and swap of unknown size");
9031 case PPC::ATOMIC_CMP_SWAP_I8:
9032 LoadMnemonic = PPC::LBARX;
9033 StoreMnemonic = PPC::STBCX;
9034 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics.");
9035 break;
9036 case PPC::ATOMIC_CMP_SWAP_I16:
9037 LoadMnemonic = PPC::LHARX;
9038 StoreMnemonic = PPC::STHCX;
9039 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics.");
9040 break;
9041 case PPC::ATOMIC_CMP_SWAP_I32:
9042 LoadMnemonic = PPC::LWARX;
9043 StoreMnemonic = PPC::STWCX;
9044 break;
9045 case PPC::ATOMIC_CMP_SWAP_I64:
9046 LoadMnemonic = PPC::LDARX;
9047 StoreMnemonic = PPC::STDCX;
9048 break;
9049 }
Evan Cheng32e376f2008-07-12 02:23:19 +00009050 unsigned dest = MI->getOperand(0).getReg();
9051 unsigned ptrA = MI->getOperand(1).getReg();
9052 unsigned ptrB = MI->getOperand(2).getReg();
9053 unsigned oldval = MI->getOperand(3).getReg();
9054 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00009055 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00009056
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009057 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
9058 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
9059 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009060 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009061 F->insert(It, loop1MBB);
9062 F->insert(It, loop2MBB);
9063 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009064 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00009065 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00009066 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00009067 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009068
9069 // thisMBB:
9070 // ...
9071 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009072 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009073
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009074 // loop1MBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009075 // l[bhwd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009076 // cmp[wd] dest, oldval
9077 // bne- midMBB
9078 // loop2MBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009079 // st[bhwd]cx. newval, ptr
Evan Cheng32e376f2008-07-12 02:23:19 +00009080 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009081 // b exitBB
9082 // midMBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009083 // st[bhwd]cx. dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009084 // exitBB:
9085 BB = loop1MBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009086 BuildMI(BB, dl, TII->get(LoadMnemonic), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00009087 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009088 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00009089 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009090 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009091 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
9092 BB->addSuccessor(loop2MBB);
9093 BB->addSuccessor(midMBB);
9094
9095 BB = loop2MBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009096 BuildMI(BB, dl, TII->get(StoreMnemonic))
Evan Cheng32e376f2008-07-12 02:23:19 +00009097 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009098 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009099 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009100 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009101 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009102 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009103
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009104 BB = midMBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009105 BuildMI(BB, dl, TII->get(StoreMnemonic))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009106 .addReg(dest).addReg(ptrA).addReg(ptrB);
9107 BB->addSuccessor(exitMBB);
9108
Evan Cheng32e376f2008-07-12 02:23:19 +00009109 // exitMBB:
9110 // ...
9111 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00009112 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
9113 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
9114 // We must use 64-bit registers for addresses when targeting 64-bit,
9115 // since we're actually doing arithmetic on them. Other registers
9116 // can be 32-bit.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009117 bool is64bit = Subtarget.isPPC64();
Dale Johannesen340d2642008-08-30 00:08:53 +00009118 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
9119
9120 unsigned dest = MI->getOperand(0).getReg();
9121 unsigned ptrA = MI->getOperand(1).getReg();
9122 unsigned ptrB = MI->getOperand(2).getReg();
9123 unsigned oldval = MI->getOperand(3).getReg();
9124 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00009125 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00009126
9127 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
9128 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
9129 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
9130 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
9131 F->insert(It, loop1MBB);
9132 F->insert(It, loop2MBB);
9133 F->insert(It, midMBB);
9134 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00009135 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00009136 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00009137 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00009138
9139 MachineRegisterInfo &RegInfo = F->getRegInfo();
Craig Topper61e88f42014-11-21 05:58:21 +00009140 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass
9141 : &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00009142 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
9143 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
9144 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
9145 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
9146 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
9147 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
9148 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
9149 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
9150 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
9151 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
9152 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
9153 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
9154 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
9155 unsigned Ptr1Reg;
9156 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00009157 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00009158 // thisMBB:
9159 // ...
9160 // fallthrough --> loopMBB
9161 BB->addSuccessor(loop1MBB);
9162
9163 // The 4-byte load must be aligned, while a char or short may be
9164 // anywhere in the word. Hence all this nasty bookkeeping code.
9165 // add ptr1, ptrA, ptrB [copy if ptrA==0]
9166 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00009167 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00009168 // rlwinm ptr, ptr1, 0, 0, 29
9169 // slw newval2, newval, shift
9170 // slw oldval2, oldval,shift
9171 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
9172 // slw mask, mask2, shift
9173 // and newval3, newval2, mask
9174 // and oldval3, oldval2, mask
9175 // loop1MBB:
9176 // lwarx tmpDest, ptr
9177 // and tmp, tmpDest, mask
9178 // cmpw tmp, oldval3
9179 // bne- midMBB
9180 // loop2MBB:
9181 // andc tmp2, tmpDest, mask
9182 // or tmp4, tmp2, newval3
9183 // stwcx. tmp4, ptr
9184 // bne- loop1MBB
9185 // b exitBB
9186 // midMBB:
9187 // stwcx. tmpDest, ptr
9188 // exitBB:
9189 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00009190 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00009191 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009192 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009193 .addReg(ptrA).addReg(ptrB);
9194 } else {
9195 Ptr1Reg = ptrB;
9196 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00009197 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009198 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009199 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009200 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
9201 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00009202 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009203 .addReg(Ptr1Reg).addImm(0).addImm(61);
9204 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00009205 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009206 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009207 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009208 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009209 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009210 .addReg(oldval).addReg(ShiftReg);
9211 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00009212 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00009213 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00009214 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
9215 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
9216 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00009217 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00009218 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009219 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009220 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009221 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009222 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009223 .addReg(OldVal2Reg).addReg(MaskReg);
9224
9225 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00009226 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00009227 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009228 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
9229 .addReg(TmpDestReg).addReg(MaskReg);
9230 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00009231 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009232 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00009233 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
9234 BB->addSuccessor(loop2MBB);
9235 BB->addSuccessor(midMBB);
9236
9237 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00009238 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
9239 .addReg(TmpDestReg).addReg(MaskReg);
9240 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
9241 .addReg(Tmp2Reg).addReg(NewVal3Reg);
9242 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00009243 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009244 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00009245 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009246 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00009247 BB->addSuccessor(loop1MBB);
9248 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009249
Dale Johannesen340d2642008-08-30 00:08:53 +00009250 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00009251 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00009252 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00009253 BB->addSuccessor(exitMBB);
9254
9255 // exitMBB:
9256 // ...
9257 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00009258 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
9259 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00009260 } else if (MI->getOpcode() == PPC::FADDrtz) {
9261 // This pseudo performs an FADD with rounding mode temporarily forced
9262 // to round-to-zero. We emit this via custom inserter since the FPSCR
9263 // is not modeled at the SelectionDAG level.
9264 unsigned Dest = MI->getOperand(0).getReg();
9265 unsigned Src1 = MI->getOperand(1).getReg();
9266 unsigned Src2 = MI->getOperand(2).getReg();
9267 DebugLoc dl = MI->getDebugLoc();
9268
9269 MachineRegisterInfo &RegInfo = F->getRegInfo();
9270 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
9271
9272 // Save FPSCR value.
9273 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
9274
9275 // Set rounding mode to round-to-zero.
9276 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
9277 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
9278
9279 // Perform addition.
9280 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
9281
9282 // Restore FPSCR value.
Hal Finkel64202162015-01-15 01:00:53 +00009283 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSFb)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00009284 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
9285 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
9286 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
9287 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
9288 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
9289 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
9290 PPC::ANDIo8 : PPC::ANDIo;
9291 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
9292 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
9293
9294 MachineRegisterInfo &RegInfo = F->getRegInfo();
9295 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
9296 &PPC::GPRCRegClass :
9297 &PPC::G8RCRegClass);
9298
9299 DebugLoc dl = MI->getDebugLoc();
9300 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
9301 .addReg(MI->getOperand(1).getReg()).addImm(1);
9302 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
9303 MI->getOperand(0).getReg())
9304 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Kit Barton535e69d2015-03-25 19:36:23 +00009305 } else if (MI->getOpcode() == PPC::TCHECK_RET) {
9306 DebugLoc Dl = MI->getDebugLoc();
9307 MachineRegisterInfo &RegInfo = F->getRegInfo();
9308 unsigned CRReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
9309 BuildMI(*BB, MI, Dl, TII->get(PPC::TCHECK), CRReg);
9310 return BB;
Dale Johannesen340d2642008-08-30 00:08:53 +00009311 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009312 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00009313 }
Chris Lattner9b577f12005-08-26 21:23:58 +00009314
Dan Gohman34396292010-07-06 20:24:04 +00009315 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00009316 return BB;
9317}
9318
Chris Lattner4211ca92006-04-14 06:01:58 +00009319//===----------------------------------------------------------------------===//
9320// Target Optimization Hooks
9321//===----------------------------------------------------------------------===//
9322
Hal Finkelcbf08922015-07-12 02:33:57 +00009323static std::string getRecipOp(const char *Base, EVT VT) {
9324 std::string RecipOp(Base);
9325 if (VT.getScalarType() == MVT::f64)
9326 RecipOp += "d";
9327 else
9328 RecipOp += "f";
9329
9330 if (VT.isVector())
9331 RecipOp = "vec-" + RecipOp;
9332
9333 return RecipOp;
9334}
9335
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009336SDValue PPCTargetLowering::getRsqrtEstimate(SDValue Operand,
9337 DAGCombinerInfo &DCI,
Sanjay Patel957efc232014-10-24 17:02:16 +00009338 unsigned &RefinementSteps,
9339 bool &UseOneConstNR) const {
Sanjay Patelbdf1e382014-09-26 23:01:47 +00009340 EVT VT = Operand.getValueType();
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009341 if ((VT == MVT::f32 && Subtarget.hasFRSQRTES()) ||
Eric Christophercccae792015-01-30 22:02:31 +00009342 (VT == MVT::f64 && Subtarget.hasFRSQRTE()) ||
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009343 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00009344 (VT == MVT::v2f64 && Subtarget.hasVSX()) ||
9345 (VT == MVT::v4f32 && Subtarget.hasQPX()) ||
9346 (VT == MVT::v4f64 && Subtarget.hasQPX())) {
Hal Finkelcbf08922015-07-12 02:33:57 +00009347 TargetRecip Recips = DCI.DAG.getTarget().Options.Reciprocals;
9348 std::string RecipOp = getRecipOp("sqrt", VT);
9349 if (!Recips.isEnabled(RecipOp))
9350 return SDValue();
9351
9352 RefinementSteps = Recips.getRefinementSteps(RecipOp);
Sanjay Patel957efc232014-10-24 17:02:16 +00009353 UseOneConstNR = true;
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009354 return DCI.DAG.getNode(PPCISD::FRSQRTE, SDLoc(Operand), VT, Operand);
Hal Finkel2e103312013-04-03 04:01:11 +00009355 }
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009356 return SDValue();
9357}
9358
9359SDValue PPCTargetLowering::getRecipEstimate(SDValue Operand,
9360 DAGCombinerInfo &DCI,
9361 unsigned &RefinementSteps) const {
9362 EVT VT = Operand.getValueType();
9363 if ((VT == MVT::f32 && Subtarget.hasFRES()) ||
Eric Christophercccae792015-01-30 22:02:31 +00009364 (VT == MVT::f64 && Subtarget.hasFRE()) ||
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009365 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00009366 (VT == MVT::v2f64 && Subtarget.hasVSX()) ||
9367 (VT == MVT::v4f32 && Subtarget.hasQPX()) ||
9368 (VT == MVT::v4f64 && Subtarget.hasQPX())) {
Hal Finkelcbf08922015-07-12 02:33:57 +00009369 TargetRecip Recips = DCI.DAG.getTarget().Options.Reciprocals;
9370 std::string RecipOp = getRecipOp("div", VT);
9371 if (!Recips.isEnabled(RecipOp))
9372 return SDValue();
9373
9374 RefinementSteps = Recips.getRefinementSteps(RecipOp);
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009375 return DCI.DAG.getNode(PPCISD::FRE, SDLoc(Operand), VT, Operand);
9376 }
9377 return SDValue();
Hal Finkel2e103312013-04-03 04:01:11 +00009378}
9379
Sanjay Patel1dd15592015-07-28 23:05:48 +00009380unsigned PPCTargetLowering::combineRepeatedFPDivisors() const {
Hal Finkel360f2132014-11-24 23:45:21 +00009381 // Note: This functionality is used only when unsafe-fp-math is enabled, and
9382 // on cores with reciprocal estimates (which are used when unsafe-fp-math is
9383 // enabled for division), this functionality is redundant with the default
9384 // combiner logic (once the division -> reciprocal/multiply transformation
9385 // has taken place). As a result, this matters more for older cores than for
9386 // newer ones.
9387
9388 // Combine multiple FDIVs with the same divisor into multiple FMULs by the
9389 // reciprocal if there are two or more FDIVs (for embedded cores with only
9390 // one FP pipeline) for three or more FDIVs (for generic OOO cores).
9391 switch (Subtarget.getDarwinDirective()) {
9392 default:
Sanjay Patel1dd15592015-07-28 23:05:48 +00009393 return 3;
Hal Finkel360f2132014-11-24 23:45:21 +00009394 case PPC::DIR_440:
9395 case PPC::DIR_A2:
9396 case PPC::DIR_E500mc:
9397 case PPC::DIR_E5500:
Sanjay Patel1dd15592015-07-28 23:05:48 +00009398 return 2;
Hal Finkel360f2132014-11-24 23:45:21 +00009399 }
9400}
9401
Hal Finkele6702ca2015-09-03 22:37:44 +00009402// isConsecutiveLSLoc needs to work even if all adds have not yet been
9403// collapsed, and so we need to look through chains of them.
9404static void getBaseWithConstantOffset(SDValue Loc, SDValue &Base,
9405 int64_t& Offset, SelectionDAG &DAG) {
9406 if (DAG.isBaseWithConstantOffset(Loc)) {
9407 Base = Loc.getOperand(0);
9408 Offset += cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
9409
9410 // The base might itself be a base plus an offset, and if so, accumulate
9411 // that as well.
9412 getBaseWithConstantOffset(Loc.getOperand(0), Base, Offset, DAG);
9413 }
9414}
9415
Hal Finkel3604bf72014-08-01 01:02:01 +00009416static bool isConsecutiveLSLoc(SDValue Loc, EVT VT, LSBaseSDNode *Base,
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009417 unsigned Bytes, int Dist,
9418 SelectionDAG &DAG) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009419 if (VT.getSizeInBits() / 8 != Bytes)
9420 return false;
9421
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009422 SDValue BaseLoc = Base->getBasePtr();
9423 if (Loc.getOpcode() == ISD::FrameIndex) {
9424 if (BaseLoc.getOpcode() != ISD::FrameIndex)
9425 return false;
9426 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9427 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
9428 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
9429 int FS = MFI->getObjectSize(FI);
9430 int BFS = MFI->getObjectSize(BFI);
9431 if (FS != BFS || FS != (int)Bytes) return false;
9432 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
9433 }
9434
Hal Finkele6702ca2015-09-03 22:37:44 +00009435 SDValue Base1 = Loc, Base2 = BaseLoc;
9436 int64_t Offset1 = 0, Offset2 = 0;
9437 getBaseWithConstantOffset(Loc, Base1, Offset1, DAG);
9438 getBaseWithConstantOffset(BaseLoc, Base2, Offset2, DAG);
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00009439 if (Base1 == Base2 && Offset1 == (Offset2 + Dist * Bytes))
9440 return true;
9441
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009442 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Craig Topper062a2ba2014-04-25 05:30:21 +00009443 const GlobalValue *GV1 = nullptr;
9444 const GlobalValue *GV2 = nullptr;
Hal Finkele6702ca2015-09-03 22:37:44 +00009445 Offset1 = 0;
9446 Offset2 = 0;
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009447 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
9448 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
9449 if (isGA1 && isGA2 && GV1 == GV2)
9450 return Offset1 == (Offset2 + Dist*Bytes);
9451 return false;
9452}
9453
Hal Finkel3604bf72014-08-01 01:02:01 +00009454// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
9455// not enforce equality of the chain operands.
9456static bool isConsecutiveLS(SDNode *N, LSBaseSDNode *Base,
9457 unsigned Bytes, int Dist,
9458 SelectionDAG &DAG) {
9459 if (LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(N)) {
9460 EVT VT = LS->getMemoryVT();
9461 SDValue Loc = LS->getBasePtr();
9462 return isConsecutiveLSLoc(Loc, VT, Base, Bytes, Dist, DAG);
9463 }
9464
9465 if (N->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
9466 EVT VT;
9467 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9468 default: return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00009469 case Intrinsic::ppc_qpx_qvlfd:
9470 case Intrinsic::ppc_qpx_qvlfda:
9471 VT = MVT::v4f64;
9472 break;
9473 case Intrinsic::ppc_qpx_qvlfs:
9474 case Intrinsic::ppc_qpx_qvlfsa:
9475 VT = MVT::v4f32;
9476 break;
9477 case Intrinsic::ppc_qpx_qvlfcd:
9478 case Intrinsic::ppc_qpx_qvlfcda:
9479 VT = MVT::v2f64;
9480 break;
9481 case Intrinsic::ppc_qpx_qvlfcs:
9482 case Intrinsic::ppc_qpx_qvlfcsa:
9483 VT = MVT::v2f32;
9484 break;
9485 case Intrinsic::ppc_qpx_qvlfiwa:
9486 case Intrinsic::ppc_qpx_qvlfiwz:
Hal Finkel3604bf72014-08-01 01:02:01 +00009487 case Intrinsic::ppc_altivec_lvx:
9488 case Intrinsic::ppc_altivec_lvxl:
Bill Schmidt72954782014-11-12 04:19:40 +00009489 case Intrinsic::ppc_vsx_lxvw4x:
Hal Finkel3604bf72014-08-01 01:02:01 +00009490 VT = MVT::v4i32;
9491 break;
Bill Schmidt72954782014-11-12 04:19:40 +00009492 case Intrinsic::ppc_vsx_lxvd2x:
9493 VT = MVT::v2f64;
9494 break;
Hal Finkel3604bf72014-08-01 01:02:01 +00009495 case Intrinsic::ppc_altivec_lvebx:
9496 VT = MVT::i8;
9497 break;
9498 case Intrinsic::ppc_altivec_lvehx:
9499 VT = MVT::i16;
9500 break;
9501 case Intrinsic::ppc_altivec_lvewx:
9502 VT = MVT::i32;
9503 break;
9504 }
9505
9506 return isConsecutiveLSLoc(N->getOperand(2), VT, Base, Bytes, Dist, DAG);
9507 }
9508
9509 if (N->getOpcode() == ISD::INTRINSIC_VOID) {
9510 EVT VT;
9511 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9512 default: return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00009513 case Intrinsic::ppc_qpx_qvstfd:
9514 case Intrinsic::ppc_qpx_qvstfda:
9515 VT = MVT::v4f64;
9516 break;
9517 case Intrinsic::ppc_qpx_qvstfs:
9518 case Intrinsic::ppc_qpx_qvstfsa:
9519 VT = MVT::v4f32;
9520 break;
9521 case Intrinsic::ppc_qpx_qvstfcd:
9522 case Intrinsic::ppc_qpx_qvstfcda:
9523 VT = MVT::v2f64;
9524 break;
9525 case Intrinsic::ppc_qpx_qvstfcs:
9526 case Intrinsic::ppc_qpx_qvstfcsa:
9527 VT = MVT::v2f32;
9528 break;
9529 case Intrinsic::ppc_qpx_qvstfiw:
9530 case Intrinsic::ppc_qpx_qvstfiwa:
Hal Finkel3604bf72014-08-01 01:02:01 +00009531 case Intrinsic::ppc_altivec_stvx:
9532 case Intrinsic::ppc_altivec_stvxl:
Bill Schmidt72954782014-11-12 04:19:40 +00009533 case Intrinsic::ppc_vsx_stxvw4x:
Hal Finkel3604bf72014-08-01 01:02:01 +00009534 VT = MVT::v4i32;
9535 break;
Bill Schmidt72954782014-11-12 04:19:40 +00009536 case Intrinsic::ppc_vsx_stxvd2x:
9537 VT = MVT::v2f64;
9538 break;
Hal Finkel3604bf72014-08-01 01:02:01 +00009539 case Intrinsic::ppc_altivec_stvebx:
9540 VT = MVT::i8;
9541 break;
9542 case Intrinsic::ppc_altivec_stvehx:
9543 VT = MVT::i16;
9544 break;
9545 case Intrinsic::ppc_altivec_stvewx:
9546 VT = MVT::i32;
9547 break;
9548 }
9549
9550 return isConsecutiveLSLoc(N->getOperand(3), VT, Base, Bytes, Dist, DAG);
9551 }
9552
9553 return false;
9554}
9555
Hal Finkel7d8a6912013-05-26 18:08:30 +00009556// Return true is there is a nearyby consecutive load to the one provided
9557// (regardless of alignment). We search up and down the chain, looking though
Matt Arsenault57e74d22014-07-29 00:02:40 +00009558// token factors and other loads (but nothing else). As a result, a true result
9559// indicates that it is safe to create a new consecutive load adjacent to the
9560// load provided.
Hal Finkel7d8a6912013-05-26 18:08:30 +00009561static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
9562 SDValue Chain = LD->getChain();
9563 EVT VT = LD->getMemoryVT();
9564
9565 SmallSet<SDNode *, 16> LoadRoots;
9566 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
9567 SmallSet<SDNode *, 16> Visited;
9568
9569 // First, search up the chain, branching to follow all token-factor operands.
9570 // If we find a consecutive load, then we're done, otherwise, record all
9571 // nodes just above the top-level loads and token factors.
9572 while (!Queue.empty()) {
9573 SDNode *ChainNext = Queue.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00009574 if (!Visited.insert(ChainNext).second)
Hal Finkel7d8a6912013-05-26 18:08:30 +00009575 continue;
9576
Hal Finkel3604bf72014-08-01 01:02:01 +00009577 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009578 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00009579 return true;
9580
9581 if (!Visited.count(ChainLD->getChain().getNode()))
9582 Queue.push_back(ChainLD->getChain().getNode());
9583 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
Craig Topper66e588b2014-06-29 00:40:57 +00009584 for (const SDUse &O : ChainNext->ops())
9585 if (!Visited.count(O.getNode()))
9586 Queue.push_back(O.getNode());
Hal Finkel7d8a6912013-05-26 18:08:30 +00009587 } else
9588 LoadRoots.insert(ChainNext);
9589 }
9590
9591 // Second, search down the chain, starting from the top-level nodes recorded
9592 // in the first phase. These top-level nodes are the nodes just above all
9593 // loads and token factors. Starting with their uses, recursively look though
9594 // all loads (just the chain uses) and token factors to find a consecutive
9595 // load.
9596 Visited.clear();
9597 Queue.clear();
9598
9599 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
9600 IE = LoadRoots.end(); I != IE; ++I) {
9601 Queue.push_back(*I);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +00009602
Hal Finkel7d8a6912013-05-26 18:08:30 +00009603 while (!Queue.empty()) {
9604 SDNode *LoadRoot = Queue.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00009605 if (!Visited.insert(LoadRoot).second)
Hal Finkel7d8a6912013-05-26 18:08:30 +00009606 continue;
9607
Hal Finkel3604bf72014-08-01 01:02:01 +00009608 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009609 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00009610 return true;
9611
9612 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
9613 UE = LoadRoot->use_end(); UI != UE; ++UI)
Hal Finkel3604bf72014-08-01 01:02:01 +00009614 if (((isa<MemSDNode>(*UI) &&
9615 cast<MemSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
Hal Finkel7d8a6912013-05-26 18:08:30 +00009616 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
9617 Queue.push_back(*UI);
9618 }
9619 }
9620
9621 return false;
9622}
9623
Hal Finkel940ab932014-02-28 00:27:01 +00009624SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
9625 DAGCombinerInfo &DCI) const {
9626 SelectionDAG &DAG = DCI.DAG;
9627 SDLoc dl(N);
9628
Eric Christophercccae792015-01-30 22:02:31 +00009629 assert(Subtarget.useCRBits() && "Expecting to be tracking CR bits");
Hal Finkel940ab932014-02-28 00:27:01 +00009630 // If we're tracking CR bits, we need to be careful that we don't have:
9631 // trunc(binary-ops(zext(x), zext(y)))
9632 // or
9633 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
9634 // such that we're unnecessarily moving things into GPRs when it would be
9635 // better to keep them in CR bits.
9636
9637 // Note that trunc here can be an actual i1 trunc, or can be the effective
9638 // truncation that comes from a setcc or select_cc.
9639 if (N->getOpcode() == ISD::TRUNCATE &&
9640 N->getValueType(0) != MVT::i1)
9641 return SDValue();
9642
9643 if (N->getOperand(0).getValueType() != MVT::i32 &&
9644 N->getOperand(0).getValueType() != MVT::i64)
9645 return SDValue();
9646
9647 if (N->getOpcode() == ISD::SETCC ||
9648 N->getOpcode() == ISD::SELECT_CC) {
9649 // If we're looking at a comparison, then we need to make sure that the
9650 // high bits (all except for the first) don't matter the result.
9651 ISD::CondCode CC =
9652 cast<CondCodeSDNode>(N->getOperand(
9653 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
9654 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
9655
9656 if (ISD::isSignedIntSetCC(CC)) {
9657 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
9658 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
9659 return SDValue();
9660 } else if (ISD::isUnsignedIntSetCC(CC)) {
9661 if (!DAG.MaskedValueIsZero(N->getOperand(0),
9662 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
9663 !DAG.MaskedValueIsZero(N->getOperand(1),
9664 APInt::getHighBitsSet(OpBits, OpBits-1)))
9665 return SDValue();
9666 } else {
9667 // This is neither a signed nor an unsigned comparison, just make sure
9668 // that the high bits are equal.
9669 APInt Op1Zero, Op1One;
9670 APInt Op2Zero, Op2One;
Jay Foada0653a32014-05-14 21:14:37 +00009671 DAG.computeKnownBits(N->getOperand(0), Op1Zero, Op1One);
9672 DAG.computeKnownBits(N->getOperand(1), Op2Zero, Op2One);
Hal Finkel940ab932014-02-28 00:27:01 +00009673
9674 // We don't really care about what is known about the first bit (if
9675 // anything), so clear it in all masks prior to comparing them.
9676 Op1Zero.clearBit(0); Op1One.clearBit(0);
9677 Op2Zero.clearBit(0); Op2One.clearBit(0);
9678
9679 if (Op1Zero != Op2Zero || Op1One != Op2One)
9680 return SDValue();
9681 }
9682 }
9683
9684 // We now know that the higher-order bits are irrelevant, we just need to
9685 // make sure that all of the intermediate operations are bit operations, and
9686 // all inputs are extensions.
9687 if (N->getOperand(0).getOpcode() != ISD::AND &&
9688 N->getOperand(0).getOpcode() != ISD::OR &&
9689 N->getOperand(0).getOpcode() != ISD::XOR &&
9690 N->getOperand(0).getOpcode() != ISD::SELECT &&
9691 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
9692 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
9693 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
9694 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
9695 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
9696 return SDValue();
9697
9698 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
9699 N->getOperand(1).getOpcode() != ISD::AND &&
9700 N->getOperand(1).getOpcode() != ISD::OR &&
9701 N->getOperand(1).getOpcode() != ISD::XOR &&
9702 N->getOperand(1).getOpcode() != ISD::SELECT &&
9703 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
9704 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
9705 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
9706 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
9707 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
9708 return SDValue();
9709
9710 SmallVector<SDValue, 4> Inputs;
9711 SmallVector<SDValue, 8> BinOps, PromOps;
9712 SmallPtrSet<SDNode *, 16> Visited;
9713
9714 for (unsigned i = 0; i < 2; ++i) {
9715 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9716 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9717 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
9718 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
9719 isa<ConstantSDNode>(N->getOperand(i)))
9720 Inputs.push_back(N->getOperand(i));
9721 else
9722 BinOps.push_back(N->getOperand(i));
9723
9724 if (N->getOpcode() == ISD::TRUNCATE)
9725 break;
9726 }
9727
9728 // Visit all inputs, collect all binary operations (and, or, xor and
NAKAMURA Takumi84965032015-09-22 11:14:12 +00009729 // select) that are all fed by extensions.
Hal Finkel940ab932014-02-28 00:27:01 +00009730 while (!BinOps.empty()) {
9731 SDValue BinOp = BinOps.back();
9732 BinOps.pop_back();
9733
David Blaikie70573dc2014-11-19 07:49:26 +00009734 if (!Visited.insert(BinOp.getNode()).second)
Hal Finkel940ab932014-02-28 00:27:01 +00009735 continue;
9736
9737 PromOps.push_back(BinOp);
9738
9739 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
9740 // The condition of the select is not promoted.
9741 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
9742 continue;
9743 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
9744 continue;
9745
9746 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9747 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9748 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
9749 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
9750 isa<ConstantSDNode>(BinOp.getOperand(i))) {
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00009751 Inputs.push_back(BinOp.getOperand(i));
Hal Finkel940ab932014-02-28 00:27:01 +00009752 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
9753 BinOp.getOperand(i).getOpcode() == ISD::OR ||
9754 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
9755 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
9756 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
9757 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
9758 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9759 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9760 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
9761 BinOps.push_back(BinOp.getOperand(i));
9762 } else {
9763 // We have an input that is not an extension or another binary
9764 // operation; we'll abort this transformation.
9765 return SDValue();
9766 }
9767 }
9768 }
9769
9770 // Make sure that this is a self-contained cluster of operations (which
9771 // is not quite the same thing as saying that everything has only one
9772 // use).
9773 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9774 if (isa<ConstantSDNode>(Inputs[i]))
9775 continue;
9776
9777 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
9778 UE = Inputs[i].getNode()->use_end();
9779 UI != UE; ++UI) {
9780 SDNode *User = *UI;
9781 if (User != N && !Visited.count(User))
9782 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00009783
9784 // Make sure that we're not going to promote the non-output-value
9785 // operand(s) or SELECT or SELECT_CC.
9786 // FIXME: Although we could sometimes handle this, and it does occur in
9787 // practice that one of the condition inputs to the select is also one of
9788 // the outputs, we currently can't deal with this.
9789 if (User->getOpcode() == ISD::SELECT) {
9790 if (User->getOperand(0) == Inputs[i])
9791 return SDValue();
9792 } else if (User->getOpcode() == ISD::SELECT_CC) {
9793 if (User->getOperand(0) == Inputs[i] ||
9794 User->getOperand(1) == Inputs[i])
9795 return SDValue();
9796 }
Hal Finkel940ab932014-02-28 00:27:01 +00009797 }
9798 }
9799
9800 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
9801 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
9802 UE = PromOps[i].getNode()->use_end();
9803 UI != UE; ++UI) {
9804 SDNode *User = *UI;
9805 if (User != N && !Visited.count(User))
9806 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00009807
9808 // Make sure that we're not going to promote the non-output-value
9809 // operand(s) or SELECT or SELECT_CC.
9810 // FIXME: Although we could sometimes handle this, and it does occur in
9811 // practice that one of the condition inputs to the select is also one of
9812 // the outputs, we currently can't deal with this.
9813 if (User->getOpcode() == ISD::SELECT) {
9814 if (User->getOperand(0) == PromOps[i])
9815 return SDValue();
9816 } else if (User->getOpcode() == ISD::SELECT_CC) {
9817 if (User->getOperand(0) == PromOps[i] ||
9818 User->getOperand(1) == PromOps[i])
9819 return SDValue();
9820 }
Hal Finkel940ab932014-02-28 00:27:01 +00009821 }
9822 }
9823
9824 // Replace all inputs with the extension operand.
9825 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9826 // Constants may have users outside the cluster of to-be-promoted nodes,
9827 // and so we need to replace those as we do the promotions.
9828 if (isa<ConstantSDNode>(Inputs[i]))
9829 continue;
9830 else
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00009831 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
Hal Finkel940ab932014-02-28 00:27:01 +00009832 }
9833
Hal Finkel1fb10e82016-05-12 04:00:56 +00009834 std::list<HandleSDNode> PromOpHandles;
9835 for (auto &PromOp : PromOps)
NAKAMURA Takumiae7c97d2016-06-20 00:49:20 +00009836 PromOpHandles.emplace_back(PromOp);
Hal Finkel1fb10e82016-05-12 04:00:56 +00009837
Hal Finkel940ab932014-02-28 00:27:01 +00009838 // Replace all operations (these are all the same, but have a different
9839 // (i1) return type). DAG.getNode will validate that the types of
9840 // a binary operator match, so go through the list in reverse so that
9841 // we've likely promoted both operands first. Any intermediate truncations or
9842 // extensions disappear.
Hal Finkel1fb10e82016-05-12 04:00:56 +00009843 while (!PromOpHandles.empty()) {
9844 SDValue PromOp = PromOpHandles.back().getValue();
9845 PromOpHandles.pop_back();
Hal Finkel940ab932014-02-28 00:27:01 +00009846
9847 if (PromOp.getOpcode() == ISD::TRUNCATE ||
9848 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
9849 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
9850 PromOp.getOpcode() == ISD::ANY_EXTEND) {
9851 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
9852 PromOp.getOperand(0).getValueType() != MVT::i1) {
9853 // The operand is not yet ready (see comment below).
Hal Finkel1fb10e82016-05-12 04:00:56 +00009854 PromOpHandles.emplace_front(PromOp);
Hal Finkel940ab932014-02-28 00:27:01 +00009855 continue;
9856 }
9857
9858 SDValue RepValue = PromOp.getOperand(0);
9859 if (isa<ConstantSDNode>(RepValue))
9860 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
9861
9862 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
9863 continue;
9864 }
9865
9866 unsigned C;
9867 switch (PromOp.getOpcode()) {
9868 default: C = 0; break;
9869 case ISD::SELECT: C = 1; break;
9870 case ISD::SELECT_CC: C = 2; break;
9871 }
9872
9873 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
9874 PromOp.getOperand(C).getValueType() != MVT::i1) ||
9875 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
9876 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
9877 // The to-be-promoted operands of this node have not yet been
9878 // promoted (this should be rare because we're going through the
9879 // list backward, but if one of the operands has several users in
9880 // this cluster of to-be-promoted nodes, it is possible).
Hal Finkel1fb10e82016-05-12 04:00:56 +00009881 PromOpHandles.emplace_front(PromOp);
Hal Finkel940ab932014-02-28 00:27:01 +00009882 continue;
9883 }
9884
9885 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
9886 PromOp.getNode()->op_end());
9887
9888 // If there are any constant inputs, make sure they're replaced now.
9889 for (unsigned i = 0; i < 2; ++i)
9890 if (isa<ConstantSDNode>(Ops[C+i]))
9891 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
9892
9893 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00009894 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00009895 }
9896
9897 // Now we're left with the initial truncation itself.
9898 if (N->getOpcode() == ISD::TRUNCATE)
9899 return N->getOperand(0);
9900
9901 // Otherwise, this is a comparison. The operands to be compared have just
9902 // changed type (to i1), but everything else is the same.
9903 return SDValue(N, 0);
9904}
9905
9906SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
9907 DAGCombinerInfo &DCI) const {
9908 SelectionDAG &DAG = DCI.DAG;
9909 SDLoc dl(N);
9910
Hal Finkel940ab932014-02-28 00:27:01 +00009911 // If we're tracking CR bits, we need to be careful that we don't have:
9912 // zext(binary-ops(trunc(x), trunc(y)))
9913 // or
9914 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
9915 // such that we're unnecessarily moving things into CR bits that can more
9916 // efficiently stay in GPRs. Note that if we're not certain that the high
9917 // bits are set as required by the final extension, we still may need to do
9918 // some masking to get the proper behavior.
9919
Hal Finkel46043ed2014-03-01 21:36:57 +00009920 // This same functionality is important on PPC64 when dealing with
9921 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
9922 // the return values of functions. Because it is so similar, it is handled
9923 // here as well.
9924
Hal Finkel940ab932014-02-28 00:27:01 +00009925 if (N->getValueType(0) != MVT::i32 &&
9926 N->getValueType(0) != MVT::i64)
9927 return SDValue();
9928
Eric Christophercccae792015-01-30 22:02:31 +00009929 if (!((N->getOperand(0).getValueType() == MVT::i1 && Subtarget.useCRBits()) ||
9930 (N->getOperand(0).getValueType() == MVT::i32 && Subtarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +00009931 return SDValue();
9932
9933 if (N->getOperand(0).getOpcode() != ISD::AND &&
9934 N->getOperand(0).getOpcode() != ISD::OR &&
9935 N->getOperand(0).getOpcode() != ISD::XOR &&
9936 N->getOperand(0).getOpcode() != ISD::SELECT &&
9937 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
9938 return SDValue();
9939
9940 SmallVector<SDValue, 4> Inputs;
9941 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
9942 SmallPtrSet<SDNode *, 16> Visited;
9943
9944 // Visit all inputs, collect all binary operations (and, or, xor and
NAKAMURA Takumi84965032015-09-22 11:14:12 +00009945 // select) that are all fed by truncations.
Hal Finkel940ab932014-02-28 00:27:01 +00009946 while (!BinOps.empty()) {
9947 SDValue BinOp = BinOps.back();
9948 BinOps.pop_back();
9949
David Blaikie70573dc2014-11-19 07:49:26 +00009950 if (!Visited.insert(BinOp.getNode()).second)
Hal Finkel940ab932014-02-28 00:27:01 +00009951 continue;
9952
9953 PromOps.push_back(BinOp);
9954
9955 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
9956 // The condition of the select is not promoted.
9957 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
9958 continue;
9959 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
9960 continue;
9961
9962 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
9963 isa<ConstantSDNode>(BinOp.getOperand(i))) {
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00009964 Inputs.push_back(BinOp.getOperand(i));
Hal Finkel940ab932014-02-28 00:27:01 +00009965 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
9966 BinOp.getOperand(i).getOpcode() == ISD::OR ||
9967 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
9968 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
9969 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
9970 BinOps.push_back(BinOp.getOperand(i));
9971 } else {
9972 // We have an input that is not a truncation or another binary
9973 // operation; we'll abort this transformation.
9974 return SDValue();
9975 }
9976 }
9977 }
9978
Hal Finkel4104a1a2014-12-14 05:53:19 +00009979 // The operands of a select that must be truncated when the select is
9980 // promoted because the operand is actually part of the to-be-promoted set.
9981 DenseMap<SDNode *, EVT> SelectTruncOp[2];
9982
Hal Finkel940ab932014-02-28 00:27:01 +00009983 // Make sure that this is a self-contained cluster of operations (which
9984 // is not quite the same thing as saying that everything has only one
9985 // use).
9986 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9987 if (isa<ConstantSDNode>(Inputs[i]))
9988 continue;
9989
9990 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
9991 UE = Inputs[i].getNode()->use_end();
9992 UI != UE; ++UI) {
9993 SDNode *User = *UI;
9994 if (User != N && !Visited.count(User))
9995 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00009996
Hal Finkel4104a1a2014-12-14 05:53:19 +00009997 // If we're going to promote the non-output-value operand(s) or SELECT or
9998 // SELECT_CC, record them for truncation.
Hal Finkel46043ed2014-03-01 21:36:57 +00009999 if (User->getOpcode() == ISD::SELECT) {
10000 if (User->getOperand(0) == Inputs[i])
Hal Finkel4104a1a2014-12-14 05:53:19 +000010001 SelectTruncOp[0].insert(std::make_pair(User,
10002 User->getOperand(0).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +000010003 } else if (User->getOpcode() == ISD::SELECT_CC) {
Hal Finkel4104a1a2014-12-14 05:53:19 +000010004 if (User->getOperand(0) == Inputs[i])
10005 SelectTruncOp[0].insert(std::make_pair(User,
10006 User->getOperand(0).getValueType()));
10007 if (User->getOperand(1) == Inputs[i])
10008 SelectTruncOp[1].insert(std::make_pair(User,
10009 User->getOperand(1).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +000010010 }
Hal Finkel940ab932014-02-28 00:27:01 +000010011 }
10012 }
10013
10014 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
10015 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
10016 UE = PromOps[i].getNode()->use_end();
10017 UI != UE; ++UI) {
10018 SDNode *User = *UI;
10019 if (User != N && !Visited.count(User))
10020 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +000010021
Hal Finkel4104a1a2014-12-14 05:53:19 +000010022 // If we're going to promote the non-output-value operand(s) or SELECT or
10023 // SELECT_CC, record them for truncation.
Hal Finkel46043ed2014-03-01 21:36:57 +000010024 if (User->getOpcode() == ISD::SELECT) {
10025 if (User->getOperand(0) == PromOps[i])
Hal Finkel4104a1a2014-12-14 05:53:19 +000010026 SelectTruncOp[0].insert(std::make_pair(User,
10027 User->getOperand(0).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +000010028 } else if (User->getOpcode() == ISD::SELECT_CC) {
Hal Finkel4104a1a2014-12-14 05:53:19 +000010029 if (User->getOperand(0) == PromOps[i])
10030 SelectTruncOp[0].insert(std::make_pair(User,
10031 User->getOperand(0).getValueType()));
10032 if (User->getOperand(1) == PromOps[i])
10033 SelectTruncOp[1].insert(std::make_pair(User,
10034 User->getOperand(1).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +000010035 }
Hal Finkel940ab932014-02-28 00:27:01 +000010036 }
10037 }
10038
Hal Finkel46043ed2014-03-01 21:36:57 +000010039 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +000010040 bool ReallyNeedsExt = false;
10041 if (N->getOpcode() != ISD::ANY_EXTEND) {
10042 // If all of the inputs are not already sign/zero extended, then
10043 // we'll still need to do that at the end.
10044 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
10045 if (isa<ConstantSDNode>(Inputs[i]))
10046 continue;
10047
10048 unsigned OpBits =
10049 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +000010050 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
10051
Hal Finkel940ab932014-02-28 00:27:01 +000010052 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
10053 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +000010054 APInt::getHighBitsSet(OpBits,
10055 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +000010056 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +000010057 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
10058 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +000010059 ReallyNeedsExt = true;
10060 break;
10061 }
10062 }
10063 }
10064
10065 // Replace all inputs, either with the truncation operand, or a
10066 // truncation or extension to the final output type.
10067 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
10068 // Constant inputs need to be replaced with the to-be-promoted nodes that
10069 // use them because they might have users outside of the cluster of
10070 // promoted nodes.
10071 if (isa<ConstantSDNode>(Inputs[i]))
10072 continue;
10073
10074 SDValue InSrc = Inputs[i].getOperand(0);
10075 if (Inputs[i].getValueType() == N->getValueType(0))
10076 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
10077 else if (N->getOpcode() == ISD::SIGN_EXTEND)
10078 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
10079 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
10080 else if (N->getOpcode() == ISD::ZERO_EXTEND)
10081 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
10082 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
10083 else
10084 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
10085 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
10086 }
10087
Hal Finkel1fb10e82016-05-12 04:00:56 +000010088 std::list<HandleSDNode> PromOpHandles;
10089 for (auto &PromOp : PromOps)
NAKAMURA Takumiae7c97d2016-06-20 00:49:20 +000010090 PromOpHandles.emplace_back(PromOp);
Hal Finkel1fb10e82016-05-12 04:00:56 +000010091
Hal Finkel940ab932014-02-28 00:27:01 +000010092 // Replace all operations (these are all the same, but have a different
10093 // (promoted) return type). DAG.getNode will validate that the types of
10094 // a binary operator match, so go through the list in reverse so that
10095 // we've likely promoted both operands first.
Hal Finkel1fb10e82016-05-12 04:00:56 +000010096 while (!PromOpHandles.empty()) {
10097 SDValue PromOp = PromOpHandles.back().getValue();
10098 PromOpHandles.pop_back();
Hal Finkel940ab932014-02-28 00:27:01 +000010099
10100 unsigned C;
10101 switch (PromOp.getOpcode()) {
10102 default: C = 0; break;
10103 case ISD::SELECT: C = 1; break;
10104 case ISD::SELECT_CC: C = 2; break;
10105 }
10106
10107 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
10108 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
10109 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
10110 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
10111 // The to-be-promoted operands of this node have not yet been
10112 // promoted (this should be rare because we're going through the
10113 // list backward, but if one of the operands has several users in
10114 // this cluster of to-be-promoted nodes, it is possible).
Hal Finkel1fb10e82016-05-12 04:00:56 +000010115 PromOpHandles.emplace_front(PromOp);
Hal Finkel940ab932014-02-28 00:27:01 +000010116 continue;
10117 }
10118
Hal Finkel4104a1a2014-12-14 05:53:19 +000010119 // For SELECT and SELECT_CC nodes, we do a similar check for any
10120 // to-be-promoted comparison inputs.
10121 if (PromOp.getOpcode() == ISD::SELECT ||
10122 PromOp.getOpcode() == ISD::SELECT_CC) {
10123 if ((SelectTruncOp[0].count(PromOp.getNode()) &&
10124 PromOp.getOperand(0).getValueType() != N->getValueType(0)) ||
10125 (SelectTruncOp[1].count(PromOp.getNode()) &&
10126 PromOp.getOperand(1).getValueType() != N->getValueType(0))) {
Hal Finkel1fb10e82016-05-12 04:00:56 +000010127 PromOpHandles.emplace_front(PromOp);
Hal Finkel4104a1a2014-12-14 05:53:19 +000010128 continue;
10129 }
10130 }
10131
Hal Finkel940ab932014-02-28 00:27:01 +000010132 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
10133 PromOp.getNode()->op_end());
10134
10135 // If this node has constant inputs, then they'll need to be promoted here.
10136 for (unsigned i = 0; i < 2; ++i) {
10137 if (!isa<ConstantSDNode>(Ops[C+i]))
10138 continue;
10139 if (Ops[C+i].getValueType() == N->getValueType(0))
10140 continue;
10141
10142 if (N->getOpcode() == ISD::SIGN_EXTEND)
10143 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
10144 else if (N->getOpcode() == ISD::ZERO_EXTEND)
10145 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
10146 else
10147 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
10148 }
10149
Hal Finkel4104a1a2014-12-14 05:53:19 +000010150 // If we've promoted the comparison inputs of a SELECT or SELECT_CC,
10151 // truncate them again to the original value type.
10152 if (PromOp.getOpcode() == ISD::SELECT ||
10153 PromOp.getOpcode() == ISD::SELECT_CC) {
10154 auto SI0 = SelectTruncOp[0].find(PromOp.getNode());
10155 if (SI0 != SelectTruncOp[0].end())
10156 Ops[0] = DAG.getNode(ISD::TRUNCATE, dl, SI0->second, Ops[0]);
10157 auto SI1 = SelectTruncOp[1].find(PromOp.getNode());
10158 if (SI1 != SelectTruncOp[1].end())
10159 Ops[1] = DAG.getNode(ISD::TRUNCATE, dl, SI1->second, Ops[1]);
10160 }
10161
Hal Finkel940ab932014-02-28 00:27:01 +000010162 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +000010163 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops));
Hal Finkel940ab932014-02-28 00:27:01 +000010164 }
10165
10166 // Now we're left with the initial extension itself.
10167 if (!ReallyNeedsExt)
10168 return N->getOperand(0);
10169
Hal Finkel46043ed2014-03-01 21:36:57 +000010170 // To zero extend, just mask off everything except for the first bit (in the
10171 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +000010172 if (N->getOpcode() == ISD::ZERO_EXTEND)
10173 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +000010174 DAG.getConstant(APInt::getLowBitsSet(
10175 N->getValueSizeInBits(0), PromBits),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010176 dl, N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +000010177
10178 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
10179 "Invalid extension type");
Mehdi Amini9639d652015-07-09 02:09:20 +000010180 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0), DAG.getDataLayout());
Hal Finkel940ab932014-02-28 00:27:01 +000010181 SDValue ShiftCst =
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +000010182 DAG.getConstant(N->getValueSizeInBits(0) - PromBits, dl, ShiftAmountTy);
10183 return DAG.getNode(
10184 ISD::SRA, dl, N->getValueType(0),
10185 DAG.getNode(ISD::SHL, dl, N->getValueType(0), N->getOperand(0), ShiftCst),
10186 ShiftCst);
Hal Finkel940ab932014-02-28 00:27:01 +000010187}
10188
Hal Finkel5efb9182015-01-06 06:01:57 +000010189SDValue PPCTargetLowering::combineFPToIntToFP(SDNode *N,
10190 DAGCombinerInfo &DCI) const {
10191 assert((N->getOpcode() == ISD::SINT_TO_FP ||
10192 N->getOpcode() == ISD::UINT_TO_FP) &&
10193 "Need an int -> FP conversion node here");
10194
10195 if (!Subtarget.has64BitSupport())
10196 return SDValue();
10197
10198 SelectionDAG &DAG = DCI.DAG;
10199 SDLoc dl(N);
10200 SDValue Op(N, 0);
10201
10202 // Don't handle ppc_fp128 here or i1 conversions.
10203 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
10204 return SDValue();
10205 if (Op.getOperand(0).getValueType() == MVT::i1)
10206 return SDValue();
10207
10208 // For i32 intermediate values, unfortunately, the conversion functions
10209 // leave the upper 32 bits of the value are undefined. Within the set of
10210 // scalar instructions, we have no method for zero- or sign-extending the
10211 // value. Thus, we cannot handle i32 intermediate values here.
10212 if (Op.getOperand(0).getValueType() == MVT::i32)
10213 return SDValue();
10214
10215 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
10216 "UINT_TO_FP is supported only with FPCVT");
10217
10218 // If we have FCFIDS, then use it when converting to single-precision.
10219 // Otherwise, convert to double-precision and then round.
Eric Christophercccae792015-01-30 22:02:31 +000010220 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
10221 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS
10222 : PPCISD::FCFIDS)
10223 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU
10224 : PPCISD::FCFID);
10225 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
10226 ? MVT::f32
10227 : MVT::f64;
Hal Finkel5efb9182015-01-06 06:01:57 +000010228
10229 // If we're converting from a float, to an int, and back to a float again,
10230 // then we don't need the store/load pair at all.
10231 if ((Op.getOperand(0).getOpcode() == ISD::FP_TO_UINT &&
10232 Subtarget.hasFPCVT()) ||
10233 (Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT)) {
10234 SDValue Src = Op.getOperand(0).getOperand(0);
10235 if (Src.getValueType() == MVT::f32) {
10236 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
10237 DCI.AddToWorklist(Src.getNode());
Hal Finkelbe78c252015-08-20 01:18:20 +000010238 } else if (Src.getValueType() != MVT::f64) {
10239 // Make sure that we don't pick up a ppc_fp128 source value.
10240 return SDValue();
Hal Finkel5efb9182015-01-06 06:01:57 +000010241 }
10242
10243 unsigned FCTOp =
10244 Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
10245 PPCISD::FCTIDUZ;
10246
10247 SDValue Tmp = DAG.getNode(FCTOp, dl, MVT::f64, Src);
10248 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Tmp);
10249
10250 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) {
10251 FP = DAG.getNode(ISD::FP_ROUND, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010252 MVT::f32, FP, DAG.getIntPtrConstant(0, dl));
Hal Finkel5efb9182015-01-06 06:01:57 +000010253 DCI.AddToWorklist(FP.getNode());
10254 }
10255
10256 return FP;
10257 }
10258
10259 return SDValue();
10260}
10261
Bill Schmidtfae5d712014-12-09 16:35:51 +000010262// expandVSXLoadForLE - Convert VSX loads (which may be intrinsics for
10263// builtins) into loads with swaps.
10264SDValue PPCTargetLowering::expandVSXLoadForLE(SDNode *N,
10265 DAGCombinerInfo &DCI) const {
10266 SelectionDAG &DAG = DCI.DAG;
10267 SDLoc dl(N);
10268 SDValue Chain;
10269 SDValue Base;
10270 MachineMemOperand *MMO;
10271
10272 switch (N->getOpcode()) {
10273 default:
10274 llvm_unreachable("Unexpected opcode for little endian VSX load");
10275 case ISD::LOAD: {
10276 LoadSDNode *LD = cast<LoadSDNode>(N);
10277 Chain = LD->getChain();
10278 Base = LD->getBasePtr();
10279 MMO = LD->getMemOperand();
10280 // If the MMO suggests this isn't a load of a full vector, leave
10281 // things alone. For a built-in, we have to make the change for
10282 // correctness, so if there is a size problem that will be a bug.
10283 if (MMO->getSize() < 16)
10284 return SDValue();
10285 break;
10286 }
10287 case ISD::INTRINSIC_W_CHAIN: {
10288 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
10289 Chain = Intrin->getChain();
Nemanja Ivanovic7df26c92015-06-30 20:01:16 +000010290 // Similarly to the store case below, Intrin->getBasePtr() doesn't get
Nemanja Ivanovic9c8d4cf2015-06-30 19:45:45 +000010291 // us what we want. Get operand 2 instead.
Nemanja Ivanovic9c8d4cf2015-06-30 19:45:45 +000010292 Base = Intrin->getOperand(2);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010293 MMO = Intrin->getMemOperand();
10294 break;
10295 }
10296 }
10297
10298 MVT VecTy = N->getValueType(0).getSimpleVT();
10299 SDValue LoadOps[] = { Chain, Base };
10300 SDValue Load = DAG.getMemIntrinsicNode(PPCISD::LXVD2X, dl,
Nirav Dave1f51c332016-04-15 15:01:38 +000010301 DAG.getVTList(MVT::v2f64, MVT::Other),
10302 LoadOps, MVT::v2f64, MMO);
10303
Bill Schmidtfae5d712014-12-09 16:35:51 +000010304 DCI.AddToWorklist(Load.getNode());
10305 Chain = Load.getValue(1);
Nirav Dave1f51c332016-04-15 15:01:38 +000010306 SDValue Swap = DAG.getNode(
10307 PPCISD::XXSWAPD, dl, DAG.getVTList(MVT::v2f64, MVT::Other), Chain, Load);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010308 DCI.AddToWorklist(Swap.getNode());
Nirav Dave1f51c332016-04-15 15:01:38 +000010309
10310 // Add a bitcast if the resulting load type doesn't match v2f64.
10311 if (VecTy != MVT::v2f64) {
10312 SDValue N = DAG.getNode(ISD::BITCAST, dl, VecTy, Swap);
10313 DCI.AddToWorklist(N.getNode());
10314 // Package {bitcast value, swap's chain} to match Load's shape.
10315 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(VecTy, MVT::Other),
10316 N, Swap.getValue(1));
10317 }
10318
Bill Schmidtfae5d712014-12-09 16:35:51 +000010319 return Swap;
10320}
10321
10322// expandVSXStoreForLE - Convert VSX stores (which may be intrinsics for
10323// builtins) into stores with swaps.
10324SDValue PPCTargetLowering::expandVSXStoreForLE(SDNode *N,
10325 DAGCombinerInfo &DCI) const {
10326 SelectionDAG &DAG = DCI.DAG;
10327 SDLoc dl(N);
10328 SDValue Chain;
10329 SDValue Base;
10330 unsigned SrcOpnd;
10331 MachineMemOperand *MMO;
10332
10333 switch (N->getOpcode()) {
10334 default:
10335 llvm_unreachable("Unexpected opcode for little endian VSX store");
10336 case ISD::STORE: {
10337 StoreSDNode *ST = cast<StoreSDNode>(N);
10338 Chain = ST->getChain();
10339 Base = ST->getBasePtr();
10340 MMO = ST->getMemOperand();
10341 SrcOpnd = 1;
10342 // If the MMO suggests this isn't a store of a full vector, leave
10343 // things alone. For a built-in, we have to make the change for
10344 // correctness, so if there is a size problem that will be a bug.
10345 if (MMO->getSize() < 16)
10346 return SDValue();
10347 break;
10348 }
10349 case ISD::INTRINSIC_VOID: {
10350 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
10351 Chain = Intrin->getChain();
10352 // Intrin->getBasePtr() oddly does not get what we want.
10353 Base = Intrin->getOperand(3);
10354 MMO = Intrin->getMemOperand();
10355 SrcOpnd = 2;
10356 break;
10357 }
10358 }
10359
10360 SDValue Src = N->getOperand(SrcOpnd);
10361 MVT VecTy = Src.getValueType().getSimpleVT();
Nirav Dave1f51c332016-04-15 15:01:38 +000010362
10363 // All stores are done as v2f64 and possible bit cast.
10364 if (VecTy != MVT::v2f64) {
10365 Src = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Src);
10366 DCI.AddToWorklist(Src.getNode());
10367 }
10368
Bill Schmidtfae5d712014-12-09 16:35:51 +000010369 SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl,
Nirav Dave1f51c332016-04-15 15:01:38 +000010370 DAG.getVTList(MVT::v2f64, MVT::Other), Chain, Src);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010371 DCI.AddToWorklist(Swap.getNode());
10372 Chain = Swap.getValue(1);
10373 SDValue StoreOps[] = { Chain, Swap, Base };
10374 SDValue Store = DAG.getMemIntrinsicNode(PPCISD::STXVD2X, dl,
10375 DAG.getVTList(MVT::Other),
10376 StoreOps, VecTy, MMO);
10377 DCI.AddToWorklist(Store.getNode());
10378 return Store;
10379}
10380
Duncan Sandsdc2dac12008-11-24 14:53:14 +000010381SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
10382 DAGCombinerInfo &DCI) const {
Chris Lattnerf4184352006-03-01 04:57:39 +000010383 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +000010384 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +000010385 switch (N->getOpcode()) {
10386 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +000010387 case PPCISD::SHL:
Artyom Skrobov314ee042015-11-25 19:41:11 +000010388 if (isNullConstant(N->getOperand(0))) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +000010389 return N->getOperand(0);
Chris Lattner3c48ea52006-09-19 05:22:59 +000010390 break;
10391 case PPCISD::SRL:
Artyom Skrobov314ee042015-11-25 19:41:11 +000010392 if (isNullConstant(N->getOperand(0))) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +000010393 return N->getOperand(0);
Chris Lattner3c48ea52006-09-19 05:22:59 +000010394 break;
10395 case PPCISD::SRA:
10396 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +000010397 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +000010398 C->isAllOnesValue()) // -1 >>s V -> -1.
10399 return N->getOperand(0);
10400 }
10401 break;
Hal Finkel940ab932014-02-28 00:27:01 +000010402 case ISD::SIGN_EXTEND:
10403 case ISD::ZERO_EXTEND:
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +000010404 case ISD::ANY_EXTEND:
Hal Finkel940ab932014-02-28 00:27:01 +000010405 return DAGCombineExtBoolTrunc(N, DCI);
10406 case ISD::TRUNCATE:
10407 case ISD::SETCC:
10408 case ISD::SELECT_CC:
10409 return DAGCombineTruncBoolExt(N, DCI);
Chris Lattnerf4184352006-03-01 04:57:39 +000010410 case ISD::SINT_TO_FP:
Hal Finkel5efb9182015-01-06 06:01:57 +000010411 case ISD::UINT_TO_FP:
10412 return combineFPToIntToFP(N, DCI);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010413 case ISD::STORE: {
Chris Lattner27f53452006-03-01 05:50:56 +000010414 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
Eric Christophercccae792015-01-30 22:02:31 +000010415 if (Subtarget.hasSTFIWX() && !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +000010416 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +000010417 N->getOperand(1).getValueType() == MVT::i32 &&
10418 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010419 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +000010420 if (Val.getValueType() == MVT::f32) {
10421 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010422 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +000010423 }
Owen Anderson9f944592009-08-11 20:47:22 +000010424 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010425 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +000010426
Hal Finkel60c75102013-04-01 15:37:53 +000010427 SDValue Ops[] = {
10428 N->getOperand(0), Val, N->getOperand(2),
10429 DAG.getValueType(N->getOperand(1).getValueType())
10430 };
10431
10432 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +000010433 DAG.getVTList(MVT::Other), Ops,
Hal Finkel60c75102013-04-01 15:37:53 +000010434 cast<StoreSDNode>(N)->getMemoryVT(),
10435 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +000010436 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +000010437 return Val;
10438 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010439
Chris Lattnera7976d32006-07-10 20:56:58 +000010440 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +000010441 if (cast<StoreSDNode>(N)->isUnindexed() &&
10442 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +000010443 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +000010444 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +000010445 N->getOperand(1).getValueType() == MVT::i16 ||
Eric Christophercccae792015-01-30 22:02:31 +000010446 (Subtarget.hasLDBRX() && Subtarget.isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +000010447 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010448 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +000010449 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +000010450 if (BSwapOp.getValueType() == MVT::i16)
10451 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +000010452
Dan Gohman48b185d2009-09-25 20:36:54 +000010453 SDValue Ops[] = {
10454 N->getOperand(0), BSwapOp, N->getOperand(2),
10455 DAG.getValueType(N->getOperand(1).getValueType())
10456 };
10457 return
10458 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +000010459 Ops, cast<StoreSDNode>(N)->getMemoryVT(),
Dan Gohman48b185d2009-09-25 20:36:54 +000010460 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +000010461 }
Bill Schmidtfae5d712014-12-09 16:35:51 +000010462
10463 // For little endian, VSX stores require generating xxswapd/lxvd2x.
10464 EVT VT = N->getOperand(1).getValueType();
10465 if (VT.isSimple()) {
10466 MVT StoreVT = VT.getSimpleVT();
Eric Christophercccae792015-01-30 22:02:31 +000010467 if (Subtarget.hasVSX() && Subtarget.isLittleEndian() &&
Bill Schmidtfae5d712014-12-09 16:35:51 +000010468 (StoreVT == MVT::v2f64 || StoreVT == MVT::v2i64 ||
10469 StoreVT == MVT::v4f32 || StoreVT == MVT::v4i32))
10470 return expandVSXStoreForLE(N, DCI);
10471 }
Chris Lattnera7976d32006-07-10 20:56:58 +000010472 break;
Bill Schmidtfae5d712014-12-09 16:35:51 +000010473 }
Hal Finkelcf2e9082013-05-24 23:00:14 +000010474 case ISD::LOAD: {
10475 LoadSDNode *LD = cast<LoadSDNode>(N);
10476 EVT VT = LD->getValueType(0);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010477
10478 // For little endian, VSX loads require generating lxvd2x/xxswapd.
10479 if (VT.isSimple()) {
10480 MVT LoadVT = VT.getSimpleVT();
Eric Christophercccae792015-01-30 22:02:31 +000010481 if (Subtarget.hasVSX() && Subtarget.isLittleEndian() &&
Bill Schmidtfae5d712014-12-09 16:35:51 +000010482 (LoadVT == MVT::v2f64 || LoadVT == MVT::v2i64 ||
10483 LoadVT == MVT::v4f32 || LoadVT == MVT::v4i32))
10484 return expandVSXLoadForLE(N, DCI);
10485 }
10486
Hal Finkel851b33a2016-03-31 02:56:05 +000010487 // We sometimes end up with a 64-bit integer load, from which we extract
10488 // two single-precision floating-point numbers. This happens with
10489 // std::complex<float>, and other similar structures, because of the way we
10490 // canonicalize structure copies. However, if we lack direct moves,
10491 // then the final bitcasts from the extracted integer values to the
10492 // floating-point numbers turn into store/load pairs. Even with direct moves,
10493 // just loading the two floating-point numbers is likely better.
10494 auto ReplaceTwoFloatLoad = [&]() {
10495 if (VT != MVT::i64)
10496 return false;
10497
10498 if (LD->getExtensionType() != ISD::NON_EXTLOAD ||
10499 LD->isVolatile())
10500 return false;
10501
10502 // We're looking for a sequence like this:
10503 // t13: i64,ch = load<LD8[%ref.tmp]> t0, t6, undef:i64
10504 // t16: i64 = srl t13, Constant:i32<32>
10505 // t17: i32 = truncate t16
10506 // t18: f32 = bitcast t17
10507 // t19: i32 = truncate t13
10508 // t20: f32 = bitcast t19
10509
10510 if (!LD->hasNUsesOfValue(2, 0))
10511 return false;
10512
10513 auto UI = LD->use_begin();
10514 while (UI.getUse().getResNo() != 0) ++UI;
10515 SDNode *Trunc = *UI++;
10516 while (UI.getUse().getResNo() != 0) ++UI;
10517 SDNode *RightShift = *UI;
10518 if (Trunc->getOpcode() != ISD::TRUNCATE)
10519 std::swap(Trunc, RightShift);
10520
10521 if (Trunc->getOpcode() != ISD::TRUNCATE ||
10522 Trunc->getValueType(0) != MVT::i32 ||
10523 !Trunc->hasOneUse())
10524 return false;
10525 if (RightShift->getOpcode() != ISD::SRL ||
10526 !isa<ConstantSDNode>(RightShift->getOperand(1)) ||
10527 RightShift->getConstantOperandVal(1) != 32 ||
10528 !RightShift->hasOneUse())
10529 return false;
10530
10531 SDNode *Trunc2 = *RightShift->use_begin();
10532 if (Trunc2->getOpcode() != ISD::TRUNCATE ||
10533 Trunc2->getValueType(0) != MVT::i32 ||
10534 !Trunc2->hasOneUse())
10535 return false;
10536
10537 SDNode *Bitcast = *Trunc->use_begin();
10538 SDNode *Bitcast2 = *Trunc2->use_begin();
10539
10540 if (Bitcast->getOpcode() != ISD::BITCAST ||
10541 Bitcast->getValueType(0) != MVT::f32)
10542 return false;
NAKAMURA Takumiae7c97d2016-06-20 00:49:20 +000010543 if (Bitcast2->getOpcode() != ISD::BITCAST ||
Hal Finkel851b33a2016-03-31 02:56:05 +000010544 Bitcast2->getValueType(0) != MVT::f32)
10545 return false;
10546
10547 if (Subtarget.isLittleEndian())
10548 std::swap(Bitcast, Bitcast2);
10549
10550 // Bitcast has the second float (in memory-layout order) and Bitcast2
10551 // has the first one.
10552
10553 SDValue BasePtr = LD->getBasePtr();
10554 if (LD->isIndexed()) {
10555 assert(LD->getAddressingMode() == ISD::PRE_INC &&
10556 "Non-pre-inc AM on PPC?");
10557 BasePtr =
10558 DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
10559 LD->getOffset());
10560 }
10561
10562 SDValue FloatLoad =
10563 DAG.getLoad(MVT::f32, dl, LD->getChain(), BasePtr,
10564 LD->getPointerInfo(), false, LD->isNonTemporal(),
10565 LD->isInvariant(), LD->getAlignment(), LD->getAAInfo());
10566 SDValue AddPtr =
10567 DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(),
10568 BasePtr, DAG.getIntPtrConstant(4, dl));
10569 SDValue FloatLoad2 =
10570 DAG.getLoad(MVT::f32, dl, SDValue(FloatLoad.getNode(), 1), AddPtr,
10571 LD->getPointerInfo().getWithOffset(4), false,
10572 LD->isNonTemporal(), LD->isInvariant(),
10573 MinAlign(LD->getAlignment(), 4), LD->getAAInfo());
10574
10575 if (LD->isIndexed()) {
NAKAMURA Takumife1202c2016-06-20 00:37:41 +000010576 // Note that DAGCombine should re-form any pre-increment load(s) from
10577 // what is produced here if that makes sense.
Hal Finkel851b33a2016-03-31 02:56:05 +000010578 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), BasePtr);
10579 }
10580
10581 DCI.CombineTo(Bitcast2, FloatLoad);
10582 DCI.CombineTo(Bitcast, FloatLoad2);
10583
10584 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, LD->isIndexed() ? 2 : 1),
10585 SDValue(FloatLoad2.getNode(), 1));
10586 return true;
10587 };
10588
10589 if (ReplaceTwoFloatLoad())
10590 return SDValue(N, 0);
10591
Hal Finkelc93a9a22015-02-25 01:06:45 +000010592 EVT MemVT = LD->getMemoryVT();
10593 Type *Ty = MemVT.getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +000010594 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
Hal Finkelc93a9a22015-02-25 01:06:45 +000010595 Type *STy = MemVT.getScalarType().getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +000010596 unsigned ScalarABIAlignment = DAG.getDataLayout().getABITypeAlignment(STy);
Hal Finkelc93a9a22015-02-25 01:06:45 +000010597 if (LD->isUnindexed() && VT.isVector() &&
10598 ((Subtarget.hasAltivec() && ISD::isNON_EXTLoad(N) &&
10599 // P8 and later hardware should just use LOAD.
10600 !Subtarget.hasP8Vector() && (VT == MVT::v16i8 || VT == MVT::v8i16 ||
10601 VT == MVT::v4i32 || VT == MVT::v4f32)) ||
10602 (Subtarget.hasQPX() && (VT == MVT::v4f64 || VT == MVT::v4f32) &&
10603 LD->getAlignment() >= ScalarABIAlignment)) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +000010604 LD->getAlignment() < ABIAlignment) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000010605 // This is a type-legal unaligned Altivec or QPX load.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010606 SDValue Chain = LD->getChain();
10607 SDValue Ptr = LD->getBasePtr();
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010608 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelcf2e9082013-05-24 23:00:14 +000010609
10610 // This implements the loading of unaligned vectors as described in
10611 // the venerable Apple Velocity Engine overview. Specifically:
10612 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
10613 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
10614 //
10615 // The general idea is to expand a sequence of one or more unaligned
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010616 // loads into an alignment-based permutation-control instruction (lvsl
10617 // or lvsr), a series of regular vector loads (which always truncate
10618 // their input address to an aligned address), and a series of
10619 // permutations. The results of these permutations are the requested
10620 // loaded values. The trick is that the last "extra" load is not taken
10621 // from the address you might suspect (sizeof(vector) bytes after the
10622 // last requested load), but rather sizeof(vector) - 1 bytes after the
10623 // last requested vector. The point of this is to avoid a page fault if
10624 // the base address happened to be aligned. This works because if the
10625 // base address is aligned, then adding less than a full vector length
10626 // will cause the last vector in the sequence to be (re)loaded.
10627 // Otherwise, the next vector will be fetched as you might suspect was
10628 // necessary.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010629
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010630 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +000010631 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010632 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
10633 // optimization later.
Hal Finkelc93a9a22015-02-25 01:06:45 +000010634 Intrinsic::ID Intr, IntrLD, IntrPerm;
10635 MVT PermCntlTy, PermTy, LDTy;
10636 if (Subtarget.hasAltivec()) {
10637 Intr = isLittleEndian ? Intrinsic::ppc_altivec_lvsr :
10638 Intrinsic::ppc_altivec_lvsl;
10639 IntrLD = Intrinsic::ppc_altivec_lvx;
10640 IntrPerm = Intrinsic::ppc_altivec_vperm;
10641 PermCntlTy = MVT::v16i8;
10642 PermTy = MVT::v4i32;
10643 LDTy = MVT::v4i32;
10644 } else {
10645 Intr = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlpcld :
10646 Intrinsic::ppc_qpx_qvlpcls;
10647 IntrLD = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlfd :
10648 Intrinsic::ppc_qpx_qvlfs;
10649 IntrPerm = Intrinsic::ppc_qpx_qvfperm;
10650 PermCntlTy = MVT::v4f64;
10651 PermTy = MVT::v4f64;
10652 LDTy = MemVT.getSimpleVT();
10653 }
10654
10655 SDValue PermCntl = BuildIntrinsicOp(Intr, Ptr, DAG, dl, PermCntlTy);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010656
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010657 // Create the new MMO for the new base load. It is like the original MMO,
10658 // but represents an area in memory almost twice the vector size centered
10659 // on the original address. If the address is unaligned, we might start
10660 // reading up to (sizeof(vector)-1) bytes below the address of the
10661 // original unaligned load.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010662 MachineFunction &MF = DAG.getMachineFunction();
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010663 MachineMemOperand *BaseMMO =
Hal Finkel99d95322015-09-03 21:12:15 +000010664 MF.getMachineMemOperand(LD->getMemOperand(),
10665 -(long)MemVT.getStoreSize()+1,
Hal Finkelc93a9a22015-02-25 01:06:45 +000010666 2*MemVT.getStoreSize()-1);
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010667
10668 // Create the new base load.
Mehdi Amini44ede332015-07-09 02:09:04 +000010669 SDValue LDXIntID =
10670 DAG.getTargetConstant(IntrLD, dl, getPointerTy(MF.getDataLayout()));
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010671 SDValue BaseLoadOps[] = { Chain, LDXIntID, Ptr };
10672 SDValue BaseLoad =
10673 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
Hal Finkelc93a9a22015-02-25 01:06:45 +000010674 DAG.getVTList(PermTy, MVT::Other),
10675 BaseLoadOps, LDTy, BaseMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010676
10677 // Note that the value of IncOffset (which is provided to the next
10678 // load's pointer info offset value, and thus used to calculate the
10679 // alignment), and the value of IncValue (which is actually used to
10680 // increment the pointer value) are different! This is because we
10681 // require the next load to appear to be aligned, even though it
10682 // is actually offset from the base pointer by a lesser amount.
10683 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +000010684 int IncValue = IncOffset;
10685
10686 // Walk (both up and down) the chain looking for another load at the real
10687 // (aligned) offset (the alignment of the other load does not matter in
10688 // this case). If found, then do not use the offset reduction trick, as
10689 // that will prevent the loads from being later combined (as they would
10690 // otherwise be duplicates).
10691 if (!findConsecutiveLoad(LD, DAG))
10692 --IncValue;
10693
Mehdi Amini44ede332015-07-09 02:09:04 +000010694 SDValue Increment =
10695 DAG.getConstant(IncValue, dl, getPointerTy(MF.getDataLayout()));
Hal Finkelcf2e9082013-05-24 23:00:14 +000010696 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
10697
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010698 MachineMemOperand *ExtraMMO =
10699 MF.getMachineMemOperand(LD->getMemOperand(),
Hal Finkelc93a9a22015-02-25 01:06:45 +000010700 1, 2*MemVT.getStoreSize()-1);
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010701 SDValue ExtraLoadOps[] = { Chain, LDXIntID, Ptr };
Hal Finkelcf2e9082013-05-24 23:00:14 +000010702 SDValue ExtraLoad =
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010703 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
Hal Finkelc93a9a22015-02-25 01:06:45 +000010704 DAG.getVTList(PermTy, MVT::Other),
10705 ExtraLoadOps, LDTy, ExtraMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010706
10707 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
10708 BaseLoad.getValue(1), ExtraLoad.getValue(1));
10709
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010710 // Because vperm has a big-endian bias, we must reverse the order
10711 // of the input vectors and complement the permute control vector
10712 // when generating little endian code. We have already handled the
10713 // latter by using lvsr instead of lvsl, so just reverse BaseLoad
10714 // and ExtraLoad here.
10715 SDValue Perm;
10716 if (isLittleEndian)
Hal Finkelc93a9a22015-02-25 01:06:45 +000010717 Perm = BuildIntrinsicOp(IntrPerm,
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010718 ExtraLoad, BaseLoad, PermCntl, DAG, dl);
10719 else
Hal Finkelc93a9a22015-02-25 01:06:45 +000010720 Perm = BuildIntrinsicOp(IntrPerm,
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010721 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010722
Hal Finkelc93a9a22015-02-25 01:06:45 +000010723 if (VT != PermTy)
10724 Perm = Subtarget.hasAltivec() ?
10725 DAG.getNode(ISD::BITCAST, dl, VT, Perm) :
10726 DAG.getNode(ISD::FP_ROUND, dl, VT, Perm, // QPX
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010727 DAG.getTargetConstant(1, dl, MVT::i64));
Hal Finkelc93a9a22015-02-25 01:06:45 +000010728 // second argument is 1 because this rounding
10729 // is always exact.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010730
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010731 // The output of the permutation is our loaded result, the TokenFactor is
10732 // our new chain.
10733 DCI.CombineTo(N, Perm, TF);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010734 return SDValue(N, 0);
10735 }
10736 }
10737 break;
Eric Christophercccae792015-01-30 22:02:31 +000010738 case ISD::INTRINSIC_WO_CHAIN: {
10739 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelc93a9a22015-02-25 01:06:45 +000010740 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
Eric Christophercccae792015-01-30 22:02:31 +000010741 Intrinsic::ID Intr = (isLittleEndian ? Intrinsic::ppc_altivec_lvsr
10742 : Intrinsic::ppc_altivec_lvsl);
Hal Finkelc93a9a22015-02-25 01:06:45 +000010743 if ((IID == Intr ||
10744 IID == Intrinsic::ppc_qpx_qvlpcld ||
10745 IID == Intrinsic::ppc_qpx_qvlpcls) &&
10746 N->getOperand(1)->getOpcode() == ISD::ADD) {
Eric Christophercccae792015-01-30 22:02:31 +000010747 SDValue Add = N->getOperand(1);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010748
Hal Finkelc93a9a22015-02-25 01:06:45 +000010749 int Bits = IID == Intrinsic::ppc_qpx_qvlpcld ?
10750 5 /* 32 byte alignment */ : 4 /* 16 byte alignment */;
10751
Eric Christophercccae792015-01-30 22:02:31 +000010752 if (DAG.MaskedValueIsZero(
10753 Add->getOperand(1),
Hal Finkelc93a9a22015-02-25 01:06:45 +000010754 APInt::getAllOnesValue(Bits /* alignment */)
Eric Christophercccae792015-01-30 22:02:31 +000010755 .zext(
10756 Add.getValueType().getScalarType().getSizeInBits()))) {
10757 SDNode *BasePtr = Add->getOperand(0).getNode();
10758 for (SDNode::use_iterator UI = BasePtr->use_begin(),
10759 UE = BasePtr->use_end();
10760 UI != UE; ++UI) {
10761 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
Hal Finkelc93a9a22015-02-25 01:06:45 +000010762 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() == IID) {
Eric Christophercccae792015-01-30 22:02:31 +000010763 // We've found another LVSL/LVSR, and this address is an aligned
10764 // multiple of that one. The results will be the same, so use the
10765 // one we've just found instead.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010766
Eric Christophercccae792015-01-30 22:02:31 +000010767 return SDValue(*UI, 0);
10768 }
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010769 }
10770 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000010771
10772 if (isa<ConstantSDNode>(Add->getOperand(1))) {
10773 SDNode *BasePtr = Add->getOperand(0).getNode();
10774 for (SDNode::use_iterator UI = BasePtr->use_begin(),
10775 UE = BasePtr->use_end(); UI != UE; ++UI) {
10776 if (UI->getOpcode() == ISD::ADD &&
10777 isa<ConstantSDNode>(UI->getOperand(1)) &&
10778 (cast<ConstantSDNode>(Add->getOperand(1))->getZExtValue() -
10779 cast<ConstantSDNode>(UI->getOperand(1))->getZExtValue()) %
Aaron Ballman5561ed42015-02-25 13:05:24 +000010780 (1ULL << Bits) == 0) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000010781 SDNode *OtherAdd = *UI;
10782 for (SDNode::use_iterator VI = OtherAdd->use_begin(),
10783 VE = OtherAdd->use_end(); VI != VE; ++VI) {
10784 if (VI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
10785 cast<ConstantSDNode>(VI->getOperand(0))->getZExtValue() == IID) {
10786 return SDValue(*VI, 0);
10787 }
10788 }
10789 }
10790 }
10791 }
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010792 }
10793 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +000010794
10795 break;
Bill Schmidtfae5d712014-12-09 16:35:51 +000010796 case ISD::INTRINSIC_W_CHAIN: {
10797 // For little endian, VSX loads require generating lxvd2x/xxswapd.
Eric Christophercccae792015-01-30 22:02:31 +000010798 if (Subtarget.hasVSX() && Subtarget.isLittleEndian()) {
Bill Schmidtfae5d712014-12-09 16:35:51 +000010799 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10800 default:
10801 break;
10802 case Intrinsic::ppc_vsx_lxvw4x:
10803 case Intrinsic::ppc_vsx_lxvd2x:
10804 return expandVSXLoadForLE(N, DCI);
10805 }
10806 }
10807 break;
10808 }
10809 case ISD::INTRINSIC_VOID: {
10810 // For little endian, VSX stores require generating xxswapd/stxvd2x.
Eric Christophercccae792015-01-30 22:02:31 +000010811 if (Subtarget.hasVSX() && Subtarget.isLittleEndian()) {
Bill Schmidtfae5d712014-12-09 16:35:51 +000010812 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10813 default:
10814 break;
10815 case Intrinsic::ppc_vsx_stxvw4x:
10816 case Intrinsic::ppc_vsx_stxvd2x:
10817 return expandVSXStoreForLE(N, DCI);
10818 }
10819 }
10820 break;
10821 }
Chris Lattnera7976d32006-07-10 20:56:58 +000010822 case ISD::BSWAP:
10823 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +000010824 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +000010825 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +000010826 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
Eric Christophercccae792015-01-30 22:02:31 +000010827 (Subtarget.hasLDBRX() && Subtarget.isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +000010828 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010829 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +000010830 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +000010831 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010832 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +000010833 LD->getChain(), // Chain
10834 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +000010835 DAG.getValueType(N->getValueType(0)) // VT
10836 };
Dan Gohman48b185d2009-09-25 20:36:54 +000010837 SDValue BSLoad =
10838 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +000010839 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
10840 MVT::i64 : MVT::i32, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +000010841 Ops, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +000010842
Scott Michelcf0da6c2009-02-17 22:15:04 +000010843 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010844 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +000010845 if (N->getValueType(0) == MVT::i16)
10846 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010847
Chris Lattnera7976d32006-07-10 20:56:58 +000010848 // First, combine the bswap away. This makes the value produced by the
10849 // load dead.
10850 DCI.CombineTo(N, ResVal);
10851
10852 // Next, combine the load away, we give it a bogus result value but a real
10853 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +000010854 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +000010855
Chris Lattnera7976d32006-07-10 20:56:58 +000010856 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010857 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +000010858 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010859
Chris Lattner27f53452006-03-01 05:50:56 +000010860 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +000010861 case PPCISD::VCMP: {
10862 // If a VCMPo node already exists with exactly the same operands as this
10863 // node, use its result instead of this node (VCMPo computes both a CR6 and
10864 // a normal output).
10865 //
10866 if (!N->getOperand(0).hasOneUse() &&
10867 !N->getOperand(1).hasOneUse() &&
10868 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000010869
Chris Lattnerd4058a52006-03-31 06:02:07 +000010870 // Scan all of the users of the LHS, looking for VCMPo's that match.
Craig Topper062a2ba2014-04-25 05:30:21 +000010871 SDNode *VCMPoNode = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010872
Gabor Greiff304a7a2008-08-28 21:40:38 +000010873 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +000010874 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
10875 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +000010876 if (UI->getOpcode() == PPCISD::VCMPo &&
10877 UI->getOperand(1) == N->getOperand(1) &&
10878 UI->getOperand(2) == N->getOperand(2) &&
10879 UI->getOperand(0) == N->getOperand(0)) {
10880 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +000010881 break;
10882 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010883
Chris Lattner518834c2006-04-18 18:28:22 +000010884 // If there is no VCMPo node, or if the flag value has a single use, don't
10885 // transform this.
10886 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
10887 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010888
10889 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +000010890 // chain, this transformation is more complex. Note that multiple things
10891 // could use the value result, which we should ignore.
Craig Topper062a2ba2014-04-25 05:30:21 +000010892 SDNode *FlagUser = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010893 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Craig Topper062a2ba2014-04-25 05:30:21 +000010894 FlagUser == nullptr; ++UI) {
Chris Lattner518834c2006-04-18 18:28:22 +000010895 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +000010896 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +000010897 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010898 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +000010899 FlagUser = User;
10900 break;
10901 }
10902 }
10903 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010904
Ulrich Weigandd5ebc622013-07-03 17:05:42 +000010905 // If the user is a MFOCRF instruction, we know this is safe.
10906 // Otherwise we give up for right now.
10907 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010908 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +000010909 }
10910 break;
10911 }
Hal Finkel940ab932014-02-28 00:27:01 +000010912 case ISD::BRCOND: {
10913 SDValue Cond = N->getOperand(1);
10914 SDValue Target = N->getOperand(2);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +000010915
Hal Finkel940ab932014-02-28 00:27:01 +000010916 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
10917 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
10918 Intrinsic::ppc_is_decremented_ctr_nonzero) {
10919
10920 // We now need to make the intrinsic dead (it cannot be instruction
10921 // selected).
10922 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
10923 assert(Cond.getNode()->hasOneUse() &&
10924 "Counter decrement has more than one use");
10925
10926 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
10927 N->getOperand(0), Target);
10928 }
10929 }
10930 break;
Chris Lattner9754d142006-04-18 17:59:36 +000010931 case ISD::BR_CC: {
10932 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +000010933 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +000010934 // lowering is done pre-legalize, because the legalizer lowers the predicate
10935 // compare down to code that is difficult to reassemble.
10936 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010937 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +000010938
10939 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
10940 // value. If so, pass-through the AND to get to the intrinsic.
10941 if (LHS.getOpcode() == ISD::AND &&
10942 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
10943 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
10944 Intrinsic::ppc_is_decremented_ctr_nonzero &&
10945 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Artyom Skrobov314ee042015-11-25 19:41:11 +000010946 !isNullConstant(LHS.getOperand(1)))
Hal Finkel25c19922013-05-15 21:37:41 +000010947 LHS = LHS.getOperand(0);
10948
10949 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
10950 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
10951 Intrinsic::ppc_is_decremented_ctr_nonzero &&
10952 isa<ConstantSDNode>(RHS)) {
10953 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
10954 "Counter decrement comparison is not EQ or NE");
10955
10956 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
10957 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
10958 (CC == ISD::SETNE && !Val);
10959
10960 // We now need to make the intrinsic dead (it cannot be instruction
10961 // selected).
10962 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
10963 assert(LHS.getNode()->hasOneUse() &&
10964 "Counter decrement has more than one use");
10965
10966 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
10967 N->getOperand(0), N->getOperand(4));
10968 }
10969
Chris Lattner9754d142006-04-18 17:59:36 +000010970 int CompareOpc;
10971 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010972
Chris Lattner9754d142006-04-18 17:59:36 +000010973 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
10974 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +000010975 getVectorCompareInfo(LHS, CompareOpc, isDot, Subtarget)) {
Chris Lattner9754d142006-04-18 17:59:36 +000010976 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +000010977
Chris Lattner9754d142006-04-18 17:59:36 +000010978 // If this is a comparison against something other than 0/1, then we know
10979 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +000010980 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +000010981 if (Val != 0 && Val != 1) {
10982 if (CC == ISD::SETEQ) // Cond never true, remove branch.
10983 return N->getOperand(0);
10984 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +000010985 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +000010986 N->getOperand(0), N->getOperand(4));
10987 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010988
Chris Lattner9754d142006-04-18 17:59:36 +000010989 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010990
Chris Lattner9754d142006-04-18 17:59:36 +000010991 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010992 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +000010993 LHS.getOperand(2), // LHS of compare
10994 LHS.getOperand(3), // RHS of compare
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010995 DAG.getConstant(CompareOpc, dl, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +000010996 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +000010997 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +000010998 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010999
Chris Lattner9754d142006-04-18 17:59:36 +000011000 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011001 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +000011002 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +000011003 default: // Can't happen, don't crash on invalid number though.
11004 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011005 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +000011006 break;
11007 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011008 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +000011009 break;
11010 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011011 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +000011012 break;
11013 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011014 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +000011015 break;
11016 }
11017
Owen Anderson9f944592009-08-11 20:47:22 +000011018 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011019 DAG.getConstant(CompOpc, dl, MVT::i32),
Owen Anderson9f944592009-08-11 20:47:22 +000011020 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +000011021 N->getOperand(4), CompNode.getValue(1));
11022 }
11023 break;
11024 }
Chris Lattnerf4184352006-03-01 04:57:39 +000011025 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011026
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011027 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +000011028}
11029
Hal Finkel13d104b2014-12-11 18:37:52 +000011030SDValue
11031PPCTargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
11032 SelectionDAG &DAG,
11033 std::vector<SDNode *> *Created) const {
11034 // fold (sdiv X, pow2)
11035 EVT VT = N->getValueType(0);
Hal Finkel04b16b52014-12-23 08:38:50 +000011036 if (VT == MVT::i64 && !Subtarget.isPPC64())
11037 return SDValue();
Hal Finkel13d104b2014-12-11 18:37:52 +000011038 if ((VT != MVT::i32 && VT != MVT::i64) ||
11039 !(Divisor.isPowerOf2() || (-Divisor).isPowerOf2()))
11040 return SDValue();
11041
11042 SDLoc DL(N);
11043 SDValue N0 = N->getOperand(0);
11044
11045 bool IsNegPow2 = (-Divisor).isPowerOf2();
11046 unsigned Lg2 = (IsNegPow2 ? -Divisor : Divisor).countTrailingZeros();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011047 SDValue ShiftAmt = DAG.getConstant(Lg2, DL, VT);
Hal Finkel13d104b2014-12-11 18:37:52 +000011048
11049 SDValue Op = DAG.getNode(PPCISD::SRA_ADDZE, DL, VT, N0, ShiftAmt);
11050 if (Created)
11051 Created->push_back(Op.getNode());
11052
11053 if (IsNegPow2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011054 Op = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Op);
Hal Finkel13d104b2014-12-11 18:37:52 +000011055 if (Created)
11056 Created->push_back(Op.getNode());
11057 }
11058
11059 return Op;
11060}
11061
Chris Lattner4211ca92006-04-14 06:01:58 +000011062//===----------------------------------------------------------------------===//
11063// Inline Assembly Support
11064//===----------------------------------------------------------------------===//
11065
Jay Foada0653a32014-05-14 21:14:37 +000011066void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
11067 APInt &KnownZero,
11068 APInt &KnownOne,
11069 const SelectionDAG &DAG,
11070 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +000011071 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +000011072 switch (Op.getOpcode()) {
11073 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +000011074 case PPCISD::LBRX: {
11075 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +000011076 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +000011077 KnownZero = 0xFFFF0000;
11078 break;
11079 }
Chris Lattnerc5287c02006-04-02 06:26:07 +000011080 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011081 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +000011082 default: break;
11083 case Intrinsic::ppc_altivec_vcmpbfp_p:
11084 case Intrinsic::ppc_altivec_vcmpeqfp_p:
11085 case Intrinsic::ppc_altivec_vcmpequb_p:
11086 case Intrinsic::ppc_altivec_vcmpequh_p:
11087 case Intrinsic::ppc_altivec_vcmpequw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000011088 case Intrinsic::ppc_altivec_vcmpequd_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000011089 case Intrinsic::ppc_altivec_vcmpgefp_p:
11090 case Intrinsic::ppc_altivec_vcmpgtfp_p:
11091 case Intrinsic::ppc_altivec_vcmpgtsb_p:
11092 case Intrinsic::ppc_altivec_vcmpgtsh_p:
11093 case Intrinsic::ppc_altivec_vcmpgtsw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000011094 case Intrinsic::ppc_altivec_vcmpgtsd_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000011095 case Intrinsic::ppc_altivec_vcmpgtub_p:
11096 case Intrinsic::ppc_altivec_vcmpgtuh_p:
11097 case Intrinsic::ppc_altivec_vcmpgtuw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000011098 case Intrinsic::ppc_altivec_vcmpgtud_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000011099 KnownZero = ~1U; // All bits but the low one are known to be zero.
11100 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011101 }
Chris Lattnerc5287c02006-04-02 06:26:07 +000011102 }
11103 }
11104}
11105
Hal Finkel57725662015-01-03 17:58:24 +000011106unsigned PPCTargetLowering::getPrefLoopAlignment(MachineLoop *ML) const {
11107 switch (Subtarget.getDarwinDirective()) {
11108 default: break;
11109 case PPC::DIR_970:
11110 case PPC::DIR_PWR4:
11111 case PPC::DIR_PWR5:
11112 case PPC::DIR_PWR5X:
11113 case PPC::DIR_PWR6:
11114 case PPC::DIR_PWR6X:
11115 case PPC::DIR_PWR7:
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +000011116 case PPC::DIR_PWR8:
11117 case PPC::DIR_PWR9: {
Hal Finkel57725662015-01-03 17:58:24 +000011118 if (!ML)
11119 break;
11120
Eric Christophercccae792015-01-30 22:02:31 +000011121 const PPCInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel57725662015-01-03 17:58:24 +000011122
11123 // For small loops (between 5 and 8 instructions), align to a 32-byte
11124 // boundary so that the entire loop fits in one instruction-cache line.
11125 uint64_t LoopSize = 0;
11126 for (auto I = ML->block_begin(), IE = ML->block_end(); I != IE; ++I)
Chad Rosierbc9d4f92015-12-14 14:44:06 +000011127 for (auto J = (*I)->begin(), JE = (*I)->end(); J != JE; ++J) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000011128 LoopSize += TII->GetInstSizeInBytes(*J);
Chad Rosierbc9d4f92015-12-14 14:44:06 +000011129 if (LoopSize > 32)
11130 break;
11131 }
Hal Finkel57725662015-01-03 17:58:24 +000011132
11133 if (LoopSize > 16 && LoopSize <= 32)
11134 return 5;
11135
11136 break;
11137 }
11138 }
11139
11140 return TargetLowering::getPrefLoopAlignment(ML);
11141}
Chris Lattnerc5287c02006-04-02 06:26:07 +000011142
Chris Lattnerd6855142007-03-25 02:14:49 +000011143/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +000011144/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +000011145PPCTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011146PPCTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000011147 if (Constraint.size() == 1) {
11148 switch (Constraint[0]) {
11149 default: break;
11150 case 'b':
11151 case 'r':
11152 case 'f':
Eric Christopherb979d512016-03-24 21:04:52 +000011153 case 'd':
Chris Lattnerd6855142007-03-25 02:14:49 +000011154 case 'v':
11155 case 'y':
11156 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +000011157 case 'Z':
11158 // FIXME: While Z does indicate a memory constraint, it specifically
11159 // indicates an r+r address (used in conjunction with the 'y' modifier
11160 // in the replacement string). Currently, we're forcing the base
11161 // register to be r0 in the asm printer (which is interpreted as zero)
11162 // and forming the complete address in the second register. This is
11163 // suboptimal.
11164 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000011165 }
Hal Finkel6aca2372014-03-02 18:23:39 +000011166 } else if (Constraint == "wc") { // individual CR bits.
11167 return C_RegisterClass;
Hal Finkel27774d92014-03-13 07:58:58 +000011168 } else if (Constraint == "wa" || Constraint == "wd" ||
11169 Constraint == "wf" || Constraint == "ws") {
11170 return C_RegisterClass; // VSX registers.
Chris Lattnerd6855142007-03-25 02:14:49 +000011171 }
11172 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +000011173}
11174
John Thompsone8360b72010-10-29 17:29:13 +000011175/// Examine constraint type and operand type and determine a weight value.
11176/// This object must already have been set up with the operand type
11177/// and the current alternative constraint selected.
11178TargetLowering::ConstraintWeight
11179PPCTargetLowering::getSingleConstraintMatchWeight(
11180 AsmOperandInfo &info, const char *constraint) const {
11181 ConstraintWeight weight = CW_Invalid;
11182 Value *CallOperandVal = info.CallOperandVal;
11183 // If we don't have a value, we can't do a match,
11184 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000011185 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000011186 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000011187 Type *type = CallOperandVal->getType();
Hal Finkel6aca2372014-03-02 18:23:39 +000011188
John Thompsone8360b72010-10-29 17:29:13 +000011189 // Look at the constraint type.
Hal Finkel6aca2372014-03-02 18:23:39 +000011190 if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
11191 return CW_Register; // an individual CR bit.
Hal Finkel27774d92014-03-13 07:58:58 +000011192 else if ((StringRef(constraint) == "wa" ||
11193 StringRef(constraint) == "wd" ||
11194 StringRef(constraint) == "wf") &&
11195 type->isVectorTy())
11196 return CW_Register;
11197 else if (StringRef(constraint) == "ws" && type->isDoubleTy())
11198 return CW_Register;
Hal Finkel6aca2372014-03-02 18:23:39 +000011199
John Thompsone8360b72010-10-29 17:29:13 +000011200 switch (*constraint) {
11201 default:
11202 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11203 break;
11204 case 'b':
11205 if (type->isIntegerTy())
11206 weight = CW_Register;
11207 break;
11208 case 'f':
11209 if (type->isFloatTy())
11210 weight = CW_Register;
11211 break;
11212 case 'd':
11213 if (type->isDoubleTy())
11214 weight = CW_Register;
11215 break;
11216 case 'v':
11217 if (type->isVectorTy())
11218 weight = CW_Register;
11219 break;
11220 case 'y':
11221 weight = CW_Register;
11222 break;
Hal Finkel4f24c622012-11-05 18:18:42 +000011223 case 'Z':
11224 weight = CW_Memory;
11225 break;
John Thompsone8360b72010-10-29 17:29:13 +000011226 }
11227 return weight;
11228}
11229
Eric Christopher11e4df72015-02-26 22:38:43 +000011230std::pair<unsigned, const TargetRegisterClass *>
11231PPCTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011232 StringRef Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000011233 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +000011234 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +000011235 // GCC RS6000 Constraint Letters
11236 switch (Constraint[0]) {
11237 case 'b': // R1-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011238 if (VT == MVT::i64 && Subtarget.isPPC64())
Hal Finkel638a9fa2013-03-19 18:51:05 +000011239 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
11240 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000011241 case 'r': // R0-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011242 if (VT == MVT::i64 && Subtarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +000011243 return std::make_pair(0U, &PPC::G8RCRegClass);
11244 return std::make_pair(0U, &PPC::GPRCRegClass);
Eric Christopherb979d512016-03-24 21:04:52 +000011245 // 'd' and 'f' constraints are both defined to be "the floating point
11246 // registers", where one is for 32-bit and the other for 64-bit. We don't
11247 // really care overly much here so just give them all the same reg classes.
11248 case 'd':
Chris Lattner584a11a2006-11-02 01:44:04 +000011249 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +000011250 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +000011251 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +000011252 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +000011253 return std::make_pair(0U, &PPC::F8RCRegClass);
Hal Finkelc93a9a22015-02-25 01:06:45 +000011254 if (VT == MVT::v4f64 && Subtarget.hasQPX())
11255 return std::make_pair(0U, &PPC::QFRCRegClass);
11256 if (VT == MVT::v4f32 && Subtarget.hasQPX())
11257 return std::make_pair(0U, &PPC::QSRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000011258 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011259 case 'v':
Hal Finkelc93a9a22015-02-25 01:06:45 +000011260 if (VT == MVT::v4f64 && Subtarget.hasQPX())
11261 return std::make_pair(0U, &PPC::QFRCRegClass);
11262 if (VT == MVT::v4f32 && Subtarget.hasQPX())
11263 return std::make_pair(0U, &PPC::QSRCRegClass);
Hal Finkelbdd292a2015-10-28 23:03:45 +000011264 if (Subtarget.hasAltivec())
11265 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000011266 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +000011267 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +000011268 }
Hal Finkel34d41492015-10-28 22:25:52 +000011269 } else if (Constraint == "wc" && Subtarget.useCRBits()) {
11270 // An individual CR bit.
Hal Finkel6aca2372014-03-02 18:23:39 +000011271 return std::make_pair(0U, &PPC::CRBITRCRegClass);
Hal Finkelbdd292a2015-10-28 23:03:45 +000011272 } else if ((Constraint == "wa" || Constraint == "wd" ||
11273 Constraint == "wf") && Subtarget.hasVSX()) {
Hal Finkel27774d92014-03-13 07:58:58 +000011274 return std::make_pair(0U, &PPC::VSRCRegClass);
Hal Finkelbdd292a2015-10-28 23:03:45 +000011275 } else if (Constraint == "ws" && Subtarget.hasVSX()) {
11276 if (VT == MVT::f32 && Subtarget.hasP8Vector())
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +000011277 return std::make_pair(0U, &PPC::VSSRCRegClass);
11278 else
11279 return std::make_pair(0U, &PPC::VSFRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +000011280 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011281
Eric Christopher11e4df72015-02-26 22:38:43 +000011282 std::pair<unsigned, const TargetRegisterClass *> R =
11283 TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Hal Finkelb176acb2013-08-03 12:25:10 +000011284
11285 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
11286 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
11287 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
11288 // register.
11289 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
11290 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011291 if (R.first && VT == MVT::i64 && Subtarget.isPPC64() &&
Eric Christopher11e4df72015-02-26 22:38:43 +000011292 PPC::GPRCRegClass.contains(R.first))
Hal Finkelb176acb2013-08-03 12:25:10 +000011293 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +000011294 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +000011295 &PPC::G8RCRegClass);
Hal Finkelb176acb2013-08-03 12:25:10 +000011296
Hal Finkelaa10b3c2014-12-08 22:54:22 +000011297 // GCC accepts 'cc' as an alias for 'cr0', and we need to do the same.
11298 if (!R.second && StringRef("{cc}").equals_lower(Constraint)) {
11299 R.first = PPC::CR0;
11300 R.second = &PPC::CRRCRegClass;
11301 }
11302
Hal Finkelb176acb2013-08-03 12:25:10 +000011303 return R;
Chris Lattner01513612006-01-31 19:20:21 +000011304}
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011305
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000011306/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +000011307/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +000011308void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000011309 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011310 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +000011311 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000011312 SDValue Result;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011313
Eric Christopherde9399b2011-06-02 23:16:42 +000011314 // Only support length 1 constraints.
11315 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011316
Eric Christopherde9399b2011-06-02 23:16:42 +000011317 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011318 switch (Letter) {
11319 default: break;
11320 case 'I':
11321 case 'J':
11322 case 'K':
11323 case 'L':
11324 case 'M':
11325 case 'N':
11326 case 'O':
11327 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +000011328 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000011329 if (!CST) return; // Must be an immediate to match.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011330 SDLoc dl(Op);
Hal Finkelc91fc112014-12-03 09:37:50 +000011331 int64_t Value = CST->getSExtValue();
11332 EVT TCVT = MVT::i64; // All constants taken to be 64 bits so that negative
11333 // numbers are printed as such.
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011334 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000011335 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011336 case 'I': // "I" is a signed 16-bit constant.
Hal Finkelc91fc112014-12-03 09:37:50 +000011337 if (isInt<16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011338 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011339 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011340 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
Hal Finkelc91fc112014-12-03 09:37:50 +000011341 if (isShiftedUInt<16, 16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011342 Result = DAG.getTargetConstant(Value, dl, TCVT);
Hal Finkelc91fc112014-12-03 09:37:50 +000011343 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011344 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Hal Finkelc91fc112014-12-03 09:37:50 +000011345 if (isShiftedInt<16, 16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011346 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011347 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011348 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Hal Finkelc91fc112014-12-03 09:37:50 +000011349 if (isUInt<16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011350 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011351 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011352 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +000011353 if (Value > 31)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011354 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011355 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011356 case 'N': // "N" is a positive constant that is an exact power of two.
Hal Finkelc91fc112014-12-03 09:37:50 +000011357 if (Value > 0 && isPowerOf2_64(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011358 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011359 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011360 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +000011361 if (Value == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011362 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011363 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011364 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Hal Finkelc91fc112014-12-03 09:37:50 +000011365 if (isInt<16>(-Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011366 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011367 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011368 }
11369 break;
11370 }
11371 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011372
Gabor Greiff304a7a2008-08-28 21:40:38 +000011373 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000011374 Ops.push_back(Result);
11375 return;
11376 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011377
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011378 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +000011379 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011380}
Evan Cheng2dd2c652006-03-13 23:20:37 +000011381
Chris Lattner1eb94d92007-03-30 23:15:24 +000011382// isLegalAddressingMode - Return true if the addressing mode represented
11383// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011384bool PPCTargetLowering::isLegalAddressingMode(const DataLayout &DL,
11385 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000011386 unsigned AS) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +000011387 // PPC does not allow r+i addressing modes for vectors!
11388 if (Ty->isVectorTy() && AM.BaseOffs != 0)
11389 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011390
Chris Lattner1eb94d92007-03-30 23:15:24 +000011391 // PPC allows a sign-extended 16-bit immediate field.
11392 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
11393 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011394
Chris Lattner1eb94d92007-03-30 23:15:24 +000011395 // No global is ever allowed as a base.
11396 if (AM.BaseGV)
11397 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011398
11399 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +000011400 switch (AM.Scale) {
11401 case 0: // "r+i" or just "i", depending on HasBaseReg.
11402 break;
11403 case 1:
11404 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
11405 return false;
11406 // Otherwise we have r+r or r+i.
11407 break;
11408 case 2:
11409 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
11410 return false;
11411 // Allow 2*r as r+r.
11412 break;
Chris Lattner19ccd622007-04-09 22:10:05 +000011413 default:
11414 // No other scales are supported.
11415 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +000011416 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011417
Chris Lattner1eb94d92007-03-30 23:15:24 +000011418 return true;
11419}
11420
Dan Gohman21cea8a2010-04-17 15:26:15 +000011421SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
11422 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +000011423 MachineFunction &MF = DAG.getMachineFunction();
11424 MachineFrameInfo *MFI = MF.getFrameInfo();
11425 MFI->setReturnAddressIsTaken(true);
11426
Bill Wendling908bf812014-01-06 00:43:20 +000011427 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +000011428 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +000011429
Andrew Trickef9de2a2013-05-25 02:42:55 +000011430 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +000011431 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +000011432
Dale Johannesen81bfca72010-05-03 22:59:34 +000011433 // Make sure the function does not optimize away the store of the RA to
11434 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +000011435 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +000011436 FuncInfo->setLRStoreRequired();
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011437 bool isPPC64 = Subtarget.isPPC64();
Mehdi Amini44ede332015-07-09 02:09:04 +000011438 auto PtrVT = getPointerTy(MF.getDataLayout());
Dale Johannesen81bfca72010-05-03 22:59:34 +000011439
11440 if (Depth > 0) {
11441 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
11442 SDValue Offset =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011443 DAG.getConstant(Subtarget.getFrameLowering()->getReturnSaveOffset(), dl,
Eric Christopherf71609b2015-02-13 00:39:27 +000011444 isPPC64 ? MVT::i64 : MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +000011445 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
11446 DAG.getNode(ISD::ADD, dl, PtrVT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +000011447 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +000011448 }
Chris Lattnerf6a81562007-12-08 06:59:59 +000011449
Chris Lattnerf6a81562007-12-08 06:59:59 +000011450 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011451 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Mehdi Amini44ede332015-07-09 02:09:04 +000011452 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), RetAddrFI,
11453 MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +000011454}
11455
Dan Gohman21cea8a2010-04-17 15:26:15 +000011456SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
11457 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +000011458 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +000011459 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +000011460
Nicolas Geoffray75ab9792007-03-01 13:11:38 +000011461 MachineFunction &MF = DAG.getMachineFunction();
11462 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +000011463 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +000011464
Mehdi Amini44ede332015-07-09 02:09:04 +000011465 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
11466 bool isPPC64 = PtrVT == MVT::i64;
11467
Hal Finkelaa03c032013-03-21 19:03:19 +000011468 // Naked functions never have a frame pointer, and so we use r1. For all
11469 // other functions, this decision must be delayed until during PEI.
11470 unsigned FrameReg;
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +000011471 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Hal Finkelaa03c032013-03-21 19:03:19 +000011472 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
11473 else
11474 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
11475
Dale Johannesen81bfca72010-05-03 22:59:34 +000011476 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
11477 PtrVT);
11478 while (Depth--)
11479 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +000011480 FrameAddr, MachinePointerInfo(), false, false,
11481 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +000011482 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +000011483}
Dan Gohmanc14e5222008-10-21 03:41:46 +000011484
Hal Finkel0d8db462014-05-11 19:29:11 +000011485// FIXME? Maybe this could be a TableGen attribute on some registers and
11486// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +000011487unsigned PPCTargetLowering::getRegisterByName(const char* RegName, EVT VT,
11488 SelectionDAG &DAG) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011489 bool isPPC64 = Subtarget.isPPC64();
11490 bool isDarwinABI = Subtarget.isDarwinABI();
Hal Finkel0d8db462014-05-11 19:29:11 +000011491
11492 if ((isPPC64 && VT != MVT::i64 && VT != MVT::i32) ||
11493 (!isPPC64 && VT != MVT::i32))
11494 report_fatal_error("Invalid register global variable type");
11495
11496 bool is64Bit = isPPC64 && VT == MVT::i64;
11497 unsigned Reg = StringSwitch<unsigned>(RegName)
11498 .Case("r1", is64Bit ? PPC::X1 : PPC::R1)
Hal Finkele6698d52015-02-01 15:03:28 +000011499 .Case("r2", (isDarwinABI || isPPC64) ? 0 : PPC::R2)
Hal Finkel0d8db462014-05-11 19:29:11 +000011500 .Case("r13", (!isPPC64 && isDarwinABI) ? 0 :
11501 (is64Bit ? PPC::X13 : PPC::R13))
11502 .Default(0);
11503
11504 if (Reg)
11505 return Reg;
11506 report_fatal_error("Invalid register name global variable");
11507}
11508
Dan Gohmanc14e5222008-10-21 03:41:46 +000011509bool
11510PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11511 // The PowerPC target isn't yet aware of offsets.
11512 return false;
11513}
Tilmann Schellerb93960d2009-07-03 06:45:56 +000011514
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011515bool PPCTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11516 const CallInst &I,
11517 unsigned Intrinsic) const {
11518
11519 switch (Intrinsic) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000011520 case Intrinsic::ppc_qpx_qvlfd:
11521 case Intrinsic::ppc_qpx_qvlfs:
11522 case Intrinsic::ppc_qpx_qvlfcd:
11523 case Intrinsic::ppc_qpx_qvlfcs:
11524 case Intrinsic::ppc_qpx_qvlfiwa:
11525 case Intrinsic::ppc_qpx_qvlfiwz:
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011526 case Intrinsic::ppc_altivec_lvx:
11527 case Intrinsic::ppc_altivec_lvxl:
11528 case Intrinsic::ppc_altivec_lvebx:
11529 case Intrinsic::ppc_altivec_lvehx:
Bill Schmidt72954782014-11-12 04:19:40 +000011530 case Intrinsic::ppc_altivec_lvewx:
11531 case Intrinsic::ppc_vsx_lxvd2x:
11532 case Intrinsic::ppc_vsx_lxvw4x: {
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011533 EVT VT;
11534 switch (Intrinsic) {
11535 case Intrinsic::ppc_altivec_lvebx:
11536 VT = MVT::i8;
11537 break;
11538 case Intrinsic::ppc_altivec_lvehx:
11539 VT = MVT::i16;
11540 break;
11541 case Intrinsic::ppc_altivec_lvewx:
11542 VT = MVT::i32;
11543 break;
Bill Schmidt72954782014-11-12 04:19:40 +000011544 case Intrinsic::ppc_vsx_lxvd2x:
11545 VT = MVT::v2f64;
11546 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +000011547 case Intrinsic::ppc_qpx_qvlfd:
11548 VT = MVT::v4f64;
11549 break;
11550 case Intrinsic::ppc_qpx_qvlfs:
11551 VT = MVT::v4f32;
11552 break;
11553 case Intrinsic::ppc_qpx_qvlfcd:
11554 VT = MVT::v2f64;
11555 break;
11556 case Intrinsic::ppc_qpx_qvlfcs:
11557 VT = MVT::v2f32;
11558 break;
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011559 default:
11560 VT = MVT::v4i32;
11561 break;
11562 }
11563
11564 Info.opc = ISD::INTRINSIC_W_CHAIN;
11565 Info.memVT = VT;
11566 Info.ptrVal = I.getArgOperand(0);
11567 Info.offset = -VT.getStoreSize()+1;
11568 Info.size = 2*VT.getStoreSize()-1;
11569 Info.align = 1;
11570 Info.vol = false;
11571 Info.readMem = true;
11572 Info.writeMem = false;
11573 return true;
11574 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000011575 case Intrinsic::ppc_qpx_qvlfda:
11576 case Intrinsic::ppc_qpx_qvlfsa:
11577 case Intrinsic::ppc_qpx_qvlfcda:
11578 case Intrinsic::ppc_qpx_qvlfcsa:
11579 case Intrinsic::ppc_qpx_qvlfiwaa:
11580 case Intrinsic::ppc_qpx_qvlfiwza: {
11581 EVT VT;
11582 switch (Intrinsic) {
11583 case Intrinsic::ppc_qpx_qvlfda:
11584 VT = MVT::v4f64;
11585 break;
11586 case Intrinsic::ppc_qpx_qvlfsa:
11587 VT = MVT::v4f32;
11588 break;
11589 case Intrinsic::ppc_qpx_qvlfcda:
11590 VT = MVT::v2f64;
11591 break;
11592 case Intrinsic::ppc_qpx_qvlfcsa:
11593 VT = MVT::v2f32;
11594 break;
11595 default:
11596 VT = MVT::v4i32;
11597 break;
11598 }
11599
11600 Info.opc = ISD::INTRINSIC_W_CHAIN;
11601 Info.memVT = VT;
11602 Info.ptrVal = I.getArgOperand(0);
11603 Info.offset = 0;
11604 Info.size = VT.getStoreSize();
11605 Info.align = 1;
11606 Info.vol = false;
11607 Info.readMem = true;
11608 Info.writeMem = false;
11609 return true;
11610 }
11611 case Intrinsic::ppc_qpx_qvstfd:
11612 case Intrinsic::ppc_qpx_qvstfs:
11613 case Intrinsic::ppc_qpx_qvstfcd:
11614 case Intrinsic::ppc_qpx_qvstfcs:
11615 case Intrinsic::ppc_qpx_qvstfiw:
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011616 case Intrinsic::ppc_altivec_stvx:
11617 case Intrinsic::ppc_altivec_stvxl:
11618 case Intrinsic::ppc_altivec_stvebx:
11619 case Intrinsic::ppc_altivec_stvehx:
Bill Schmidt72954782014-11-12 04:19:40 +000011620 case Intrinsic::ppc_altivec_stvewx:
11621 case Intrinsic::ppc_vsx_stxvd2x:
11622 case Intrinsic::ppc_vsx_stxvw4x: {
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011623 EVT VT;
11624 switch (Intrinsic) {
11625 case Intrinsic::ppc_altivec_stvebx:
11626 VT = MVT::i8;
11627 break;
11628 case Intrinsic::ppc_altivec_stvehx:
11629 VT = MVT::i16;
11630 break;
11631 case Intrinsic::ppc_altivec_stvewx:
11632 VT = MVT::i32;
11633 break;
Bill Schmidt72954782014-11-12 04:19:40 +000011634 case Intrinsic::ppc_vsx_stxvd2x:
11635 VT = MVT::v2f64;
11636 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +000011637 case Intrinsic::ppc_qpx_qvstfd:
11638 VT = MVT::v4f64;
11639 break;
11640 case Intrinsic::ppc_qpx_qvstfs:
11641 VT = MVT::v4f32;
11642 break;
11643 case Intrinsic::ppc_qpx_qvstfcd:
11644 VT = MVT::v2f64;
11645 break;
11646 case Intrinsic::ppc_qpx_qvstfcs:
11647 VT = MVT::v2f32;
11648 break;
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011649 default:
11650 VT = MVT::v4i32;
11651 break;
11652 }
11653
11654 Info.opc = ISD::INTRINSIC_VOID;
11655 Info.memVT = VT;
11656 Info.ptrVal = I.getArgOperand(1);
11657 Info.offset = -VT.getStoreSize()+1;
11658 Info.size = 2*VT.getStoreSize()-1;
11659 Info.align = 1;
11660 Info.vol = false;
11661 Info.readMem = false;
11662 Info.writeMem = true;
11663 return true;
11664 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000011665 case Intrinsic::ppc_qpx_qvstfda:
11666 case Intrinsic::ppc_qpx_qvstfsa:
11667 case Intrinsic::ppc_qpx_qvstfcda:
11668 case Intrinsic::ppc_qpx_qvstfcsa:
11669 case Intrinsic::ppc_qpx_qvstfiwa: {
11670 EVT VT;
11671 switch (Intrinsic) {
11672 case Intrinsic::ppc_qpx_qvstfda:
11673 VT = MVT::v4f64;
11674 break;
11675 case Intrinsic::ppc_qpx_qvstfsa:
11676 VT = MVT::v4f32;
11677 break;
11678 case Intrinsic::ppc_qpx_qvstfcda:
11679 VT = MVT::v2f64;
11680 break;
11681 case Intrinsic::ppc_qpx_qvstfcsa:
11682 VT = MVT::v2f32;
11683 break;
11684 default:
11685 VT = MVT::v4i32;
11686 break;
11687 }
11688
11689 Info.opc = ISD::INTRINSIC_VOID;
11690 Info.memVT = VT;
11691 Info.ptrVal = I.getArgOperand(1);
11692 Info.offset = 0;
11693 Info.size = VT.getStoreSize();
11694 Info.align = 1;
11695 Info.vol = false;
11696 Info.readMem = false;
11697 Info.writeMem = true;
11698 return true;
11699 }
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011700 default:
11701 break;
11702 }
11703
11704 return false;
11705}
11706
Evan Chengd9929f02010-04-01 20:10:42 +000011707/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +000011708/// and store operations as a result of memset, memcpy, and memmove
11709/// lowering. If DstAlign is zero that means it's safe to destination
11710/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
11711/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +000011712/// probably because the source does not need to be loaded. If 'IsMemset' is
11713/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
11714/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
11715/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +000011716/// It returns EVT::Other if the type should be determined using generic
11717/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +000011718EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
11719 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000011720 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +000011721 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +000011722 MachineFunction &MF) const {
Hal Finkel52368d42015-03-31 20:56:09 +000011723 if (getTargetMachine().getOptLevel() != CodeGenOpt::None) {
11724 const Function *F = MF.getFunction();
11725 // When expanding a memset, require at least two QPX instructions to cover
11726 // the cost of loading the value to be stored from the constant pool.
11727 if (Subtarget.hasQPX() && Size >= 32 && (!IsMemset || Size >= 64) &&
11728 (!SrcAlign || SrcAlign >= 32) && (!DstAlign || DstAlign >= 32) &&
11729 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
11730 return MVT::v4f64;
11731 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000011732
Hal Finkel52368d42015-03-31 20:56:09 +000011733 // We should use Altivec/VSX loads and stores when available. For unaligned
11734 // addresses, unaligned VSX loads are only fast starting with the P8.
11735 if (Subtarget.hasAltivec() && Size >= 16 &&
11736 (((!SrcAlign || SrcAlign >= 16) && (!DstAlign || DstAlign >= 16)) ||
11737 ((IsMemset && Subtarget.hasVSX()) || Subtarget.hasP8Vector())))
11738 return MVT::v4i32;
11739 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000011740
Eric Christopherd90a8742014-06-12 22:38:20 +000011741 if (Subtarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +000011742 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +000011743 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000011744
11745 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +000011746}
Hal Finkel88ed4e32012-04-01 19:23:08 +000011747
Hal Finkel34974ed2014-04-12 21:52:38 +000011748/// \brief Returns true if it is beneficial to convert a load of a constant
11749/// to just the constant itself.
11750bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11751 Type *Ty) const {
11752 assert(Ty->isIntegerTy());
11753
11754 unsigned BitSize = Ty->getPrimitiveSizeInBits();
Alexander Kornienko175a7cb2015-12-28 13:38:42 +000011755 return !(BitSize == 0 || BitSize > 64);
Hal Finkel34974ed2014-04-12 21:52:38 +000011756}
11757
11758bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
11759 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
11760 return false;
11761 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11762 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
11763 return NumBits1 == 64 && NumBits2 == 32;
11764}
11765
11766bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
11767 if (!VT1.isInteger() || !VT2.isInteger())
11768 return false;
11769 unsigned NumBits1 = VT1.getSizeInBits();
11770 unsigned NumBits2 = VT2.getSizeInBits();
11771 return NumBits1 == 64 && NumBits2 == 32;
11772}
11773
Hal Finkel5d5d1532015-01-10 08:21:59 +000011774bool PPCTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
11775 // Generally speaking, zexts are not free, but they are free when they can be
11776 // folded with other operations.
11777 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Val)) {
11778 EVT MemVT = LD->getMemoryVT();
11779 if ((MemVT == MVT::i1 || MemVT == MVT::i8 || MemVT == MVT::i16 ||
11780 (Subtarget.isPPC64() && MemVT == MVT::i32)) &&
11781 (LD->getExtensionType() == ISD::NON_EXTLOAD ||
11782 LD->getExtensionType() == ISD::ZEXTLOAD))
11783 return true;
11784 }
11785
11786 // FIXME: Add other cases...
11787 // - 32-bit shifts with a zext to i64
11788 // - zext after ctlz, bswap, etc.
11789 // - zext after and by a constant mask
11790
11791 return TargetLowering::isZExtFree(Val, VT2);
11792}
11793
Olivier Sallenave32509692015-01-13 15:06:36 +000011794bool PPCTargetLowering::isFPExtFree(EVT VT) const {
11795 assert(VT.isFloatingPoint());
11796 return true;
11797}
11798
Hal Finkel34974ed2014-04-12 21:52:38 +000011799bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11800 return isInt<16>(Imm) || isUInt<16>(Imm);
11801}
11802
11803bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const {
11804 return isInt<16>(Imm) || isUInt<16>(Imm);
11805}
11806
Matt Arsenault6f2a5262014-07-27 17:46:40 +000011807bool PPCTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
11808 unsigned,
11809 unsigned,
11810 bool *Fast) const {
Hal Finkel8d7fbc92013-03-15 15:27:13 +000011811 if (DisablePPCUnaligned)
11812 return false;
11813
11814 // PowerPC supports unaligned memory access for simple non-vector types.
11815 // Although accessing unaligned addresses is not as efficient as accessing
11816 // aligned addresses, it is generally more efficient than manual expansion,
11817 // and generally only traps for software emulation when crossing page
11818 // boundaries.
11819
11820 if (!VT.isSimple())
11821 return false;
11822
Hal Finkel6e28e6a2014-03-26 19:39:09 +000011823 if (VT.getSimpleVT().isVector()) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011824 if (Subtarget.hasVSX()) {
Bill Schmidt2d1128a2014-10-17 15:13:38 +000011825 if (VT != MVT::v2f64 && VT != MVT::v2i64 &&
11826 VT != MVT::v4f32 && VT != MVT::v4i32)
Hal Finkel6e28e6a2014-03-26 19:39:09 +000011827 return false;
11828 } else {
11829 return false;
11830 }
11831 }
Hal Finkel8d7fbc92013-03-15 15:27:13 +000011832
11833 if (VT == MVT::ppcf128)
11834 return false;
11835
11836 if (Fast)
11837 *Fast = true;
11838
11839 return true;
11840}
11841
Stephen Lin73de7bf2013-07-09 18:16:56 +000011842bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
11843 VT = VT.getScalarType();
11844
Hal Finkel0a479ae2012-06-22 00:49:52 +000011845 if (!VT.isSimple())
11846 return false;
11847
11848 switch (VT.getSimpleVT().SimpleTy) {
11849 case MVT::f32:
11850 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +000011851 return true;
11852 default:
11853 break;
11854 }
11855
11856 return false;
11857}
11858
Hal Finkel934361a2015-01-14 01:07:51 +000011859const MCPhysReg *
11860PPCTargetLowering::getScratchRegisters(CallingConv::ID) const {
11861 // LR is a callee-save register, but we must treat it as clobbered by any call
11862 // site. Hence we include LR in the scratch registers, which are in turn added
11863 // as implicit-defs for stackmaps and patchpoints. The same reasoning applies
11864 // to CTR, which is used by any indirect call.
11865 static const MCPhysReg ScratchRegs[] = {
Hal Finkelc19805a2015-01-17 03:57:34 +000011866 PPC::X12, PPC::LR8, PPC::CTR8, 0
Hal Finkel934361a2015-01-14 01:07:51 +000011867 };
11868
11869 return ScratchRegs;
11870}
11871
Joseph Tremouletf748c892015-11-07 01:11:31 +000011872unsigned PPCTargetLowering::getExceptionPointerRegister(
11873 const Constant *PersonalityFn) const {
11874 return Subtarget.isPPC64() ? PPC::X3 : PPC::R3;
11875}
11876
11877unsigned PPCTargetLowering::getExceptionSelectorRegister(
11878 const Constant *PersonalityFn) const {
11879 return Subtarget.isPPC64() ? PPC::X4 : PPC::R4;
11880}
11881
Hal Finkelb4240ca2014-03-31 17:48:16 +000011882bool
11883PPCTargetLowering::shouldExpandBuildVectorWithShuffles(
11884 EVT VT , unsigned DefinedValues) const {
11885 if (VT == MVT::v2i64)
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +000011886 return Subtarget.hasDirectMove(); // Don't need stack ops with direct moves
Hal Finkelb4240ca2014-03-31 17:48:16 +000011887
Guozhi Weifa3e0422016-04-29 17:00:54 +000011888 if (Subtarget.hasVSX() || Subtarget.hasQPX())
11889 return true;
Hal Finkelc93a9a22015-02-25 01:06:45 +000011890
Hal Finkelb4240ca2014-03-31 17:48:16 +000011891 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
11892}
11893
Hal Finkel88ed4e32012-04-01 19:23:08 +000011894Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011895 if (DisableILPPref || Subtarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +000011896 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +000011897
Hal Finkel4e9f1a82012-06-10 19:32:29 +000011898 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +000011899}
11900
Bill Schmidt0cf702f2013-07-30 00:50:39 +000011901// Create a fast isel object.
11902FastISel *
11903PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
11904 const TargetLibraryInfo *LibInfo) const {
11905 return PPC::createFastISel(FuncInfo, LibInfo);
11906}
Chuang-Yu Cheng98c18942016-04-08 12:04:32 +000011907
11908void PPCTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const {
11909 if (Subtarget.isDarwinABI()) return;
11910 if (!Subtarget.isPPC64()) return;
11911
11912 // Update IsSplitCSR in PPCFunctionInfo
11913 PPCFunctionInfo *PFI = Entry->getParent()->getInfo<PPCFunctionInfo>();
11914 PFI->setIsSplitCSR(true);
11915}
11916
11917void PPCTargetLowering::insertCopiesSplitCSR(
11918 MachineBasicBlock *Entry,
11919 const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
11920 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo();
11921 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
11922 if (!IStart)
11923 return;
11924
11925 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
11926 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
11927 MachineBasicBlock::iterator MBBI = Entry->begin();
11928 for (const MCPhysReg *I = IStart; *I; ++I) {
11929 const TargetRegisterClass *RC = nullptr;
11930 if (PPC::G8RCRegClass.contains(*I))
11931 RC = &PPC::G8RCRegClass;
11932 else if (PPC::F8RCRegClass.contains(*I))
11933 RC = &PPC::F8RCRegClass;
11934 else if (PPC::CRRCRegClass.contains(*I))
11935 RC = &PPC::CRRCRegClass;
11936 else if (PPC::VRRCRegClass.contains(*I))
11937 RC = &PPC::VRRCRegClass;
11938 else
11939 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
11940
11941 unsigned NewVR = MRI->createVirtualRegister(RC);
11942 // Create copy from CSR to a virtual register.
11943 // FIXME: this currently does not emit CFI pseudo-instructions, it works
11944 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be
11945 // nounwind. If we want to generalize this later, we may need to emit
11946 // CFI pseudo-instructions.
11947 assert(Entry->getParent()->getFunction()->hasFnAttribute(
11948 Attribute::NoUnwind) &&
11949 "Function should be nounwind in insertCopiesSplitCSR!");
11950 Entry->addLiveIn(*I);
11951 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
11952 .addReg(*I);
11953
11954 // Insert the copy-back instructions right before the terminator
11955 for (auto *Exit : Exits)
11956 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
11957 TII->get(TargetOpcode::COPY), *I)
11958 .addReg(NewVR);
11959 }
11960}
Tim Shena1d8bc52016-04-19 20:14:52 +000011961
11962// Override to enable LOAD_STACK_GUARD lowering on Linux.
11963bool PPCTargetLowering::useLoadStackGuardNode() const {
11964 if (!Subtarget.isTargetLinux())
11965 return TargetLowering::useLoadStackGuardNode();
11966 return true;
11967}
11968
11969// Override to disable global variable loading on Linux.
11970void PPCTargetLowering::insertSSPDeclarations(Module &M) const {
11971 if (!Subtarget.isTargetLinux())
11972 return TargetLowering::insertSSPDeclarations(M);
11973}