blob: 2600ee5db17991103725b880f48756ce975066d6 [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"
Jim Laskey48850c12006-11-16 22:43:37 +000017#include "PPCMachineFunctionInfo.h"
Bill Wendlingdd3fe942010-03-12 02:00:43 +000018#include "PPCPerfectShuffle.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000019#include "PPCTargetMachine.h"
Bill Schmidt22d40dc2013-05-13 19:34:37 +000020#include "PPCTargetObjectFile.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000021#include "llvm/ADT/STLExtras.h"
Hal Finkel0d8db462014-05-11 19:29:11 +000022#include "llvm/ADT/StringSwitch.h"
Eric Christopher89958332014-05-31 00:07:32 +000023#include "llvm/ADT/Triple.h"
Chris Lattner4f2e4e02007-03-06 00:59:59 +000024#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner9b577f12005-08-26 21:23:58 +000027#include "llvm/CodeGen/MachineInstrBuilder.h"
Hal Finkel57725662015-01-03 17:58:24 +000028#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000030#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000031#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/CallingConv.h"
33#include "llvm/IR/Constants.h"
34#include "llvm/IR/DerivedTypes.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/Intrinsics.h"
Chris Lattnerce645542006-11-10 02:08:47 +000037#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000038#include "llvm/Support/ErrorHandling.h"
Craig Topperb25fda92012-03-17 18:46:09 +000039#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000040#include "llvm/Support/raw_ostream.h"
Craig Topperb25fda92012-03-17 18:46:09 +000041#include "llvm/Target/TargetOptions.h"
Kit Bartond4eb73c2015-05-05 16:10:44 +000042
Chris Lattnerf22556d2005-08-16 17:14:42 +000043using namespace llvm;
44
Joerg Sonnenbergereb8655a2014-08-08 16:46:10 +000045// FIXME: Remove this once soft-float is supported.
46static cl::opt<bool> DisablePPCFloatInVariadic("disable-ppc-float-in-variadic",
47cl::desc("disable saving float registers for va_start on PPC"), cl::Hidden);
48
Hal Finkel595817e2012-06-04 02:21:00 +000049static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
50cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000051
Hal Finkel4e9f1a82012-06-10 19:32:29 +000052static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
53cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
54
Hal Finkel8d7fbc92013-03-15 15:27:13 +000055static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
56cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
57
Hal Finkel940ab932014-02-28 00:27:01 +000058// FIXME: Remove this once the bug has been fixed!
59extern cl::opt<bool> ANDIGlueBug;
60
Eric Christophercccae792015-01-30 22:02:31 +000061PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM,
62 const PPCSubtarget &STI)
63 : TargetLowering(TM), Subtarget(STI) {
Chris Lattnera028e7a2005-09-27 22:18:25 +000064 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000065 setUseUnderscoreSetJmp(true);
66 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000067
Chris Lattnerd10babf2010-10-10 18:34:00 +000068 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
69 // arguments are at least 4/8 bytes aligned.
Eric Christopherb1aaebe2014-06-12 22:38:18 +000070 bool isPPC64 = Subtarget.isPPC64();
Evan Cheng39e90022012-07-02 22:39:56 +000071 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000072
Chris Lattnerf22556d2005-08-16 17:14:42 +000073 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000074 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
75 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
76 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +000077
Evan Cheng5d9fd972006-10-04 00:56:09 +000078 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000079 for (MVT VT : MVT::integer_valuetypes()) {
80 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
81 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand);
82 }
Duncan Sands95d46ef2008-01-23 20:39:46 +000083
Owen Anderson9f944592009-08-11 20:47:22 +000084 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000085
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000086 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000087 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
88 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
89 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
90 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
91 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
Hal Finkel65d1cbf2015-02-05 18:42:53 +000092 setIndexedLoadAction(ISD::PRE_INC, MVT::f32, Legal);
93 setIndexedLoadAction(ISD::PRE_INC, MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +000094 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
95 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
96 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
97 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
98 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Hal Finkel65d1cbf2015-02-05 18:42:53 +000099 setIndexedStoreAction(ISD::PRE_INC, MVT::f32, Legal);
100 setIndexedStoreAction(ISD::PRE_INC, MVT::f64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +0000101
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000102 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000103 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
104
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000105 if (isPPC64 || Subtarget.hasFPCVT()) {
Hal Finkel6a56b212014-03-05 22:14:00 +0000106 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
107 AddPromotedToType (ISD::SINT_TO_FP, MVT::i1,
108 isPPC64 ? MVT::i64 : MVT::i32);
109 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
110 AddPromotedToType (ISD::UINT_TO_FP, MVT::i1,
111 isPPC64 ? MVT::i64 : MVT::i32);
112 } else {
113 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom);
114 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom);
115 }
Hal Finkel940ab932014-02-28 00:27:01 +0000116
117 // PowerPC does not support direct load / store of condition registers
118 setOperationAction(ISD::LOAD, MVT::i1, Custom);
119 setOperationAction(ISD::STORE, MVT::i1, Custom);
120
121 // FIXME: Remove this once the ANDI glue bug is fixed:
122 if (ANDIGlueBug)
123 setOperationAction(ISD::TRUNCATE, MVT::i1, Custom);
124
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000125 for (MVT VT : MVT::integer_valuetypes()) {
126 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
127 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
128 setTruncStoreAction(VT, MVT::i1, Expand);
129 }
Hal Finkel940ab932014-02-28 00:27:01 +0000130
131 addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass);
132 }
133
Dale Johannesen666323e2007-10-10 01:01:31 +0000134 // This is used in the ppcf128->int sequence. Note it has different semantics
135 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +0000136 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000137
Roman Divacky1faf5b02012-08-16 18:19:29 +0000138 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000139 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
140 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
141 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
142 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
143 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000144 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000145
Chris Lattnerf22556d2005-08-16 17:14:42 +0000146 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000147 setOperationAction(ISD::SREM, MVT::i32, Expand);
148 setOperationAction(ISD::UREM, MVT::i32, Expand);
149 setOperationAction(ISD::SREM, MVT::i64, Expand);
150 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000151
152 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000153 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
154 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
155 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
156 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
157 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
158 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
159 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
160 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000161
Dan Gohman482732a2007-10-11 23:21:31 +0000162 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000163 setOperationAction(ISD::FSIN , MVT::f64, Expand);
164 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000165 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000166 setOperationAction(ISD::FREM , MVT::f64, Expand);
167 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000168 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000169 setOperationAction(ISD::FSIN , MVT::f32, Expand);
170 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000171 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000172 setOperationAction(ISD::FREM , MVT::f32, Expand);
173 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000174 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000175
Owen Anderson9f944592009-08-11 20:47:22 +0000176 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000177
Chris Lattnerf22556d2005-08-16 17:14:42 +0000178 // If we're enabling GP optimizations, use hardware square root
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000179 if (!Subtarget.hasFSQRT() &&
Eric Christophercccae792015-01-30 22:02:31 +0000180 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTE() &&
181 Subtarget.hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000182 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000183
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000184 if (!Subtarget.hasFSQRT() &&
Eric Christophercccae792015-01-30 22:02:31 +0000185 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTES() &&
186 Subtarget.hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000187 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000188
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000189 if (Subtarget.hasFCPSGN()) {
Hal Finkeldbc78e12013-08-19 05:01:02 +0000190 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
191 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
192 } else {
193 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
194 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
195 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000196
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000197 if (Subtarget.hasFPRND()) {
Hal Finkelc20a08d2013-03-29 08:57:48 +0000198 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
199 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
200 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000201 setOperationAction(ISD::FROUND, MVT::f64, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000202
203 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
204 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
205 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000206 setOperationAction(ISD::FROUND, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000207 }
208
Nate Begeman2fba8a32006-01-14 03:14:10 +0000209 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000210 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000211 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000212 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
213 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000214 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000215 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000216 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
217 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000218
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000219 if (Subtarget.hasPOPCNTD()) {
Hal Finkel290376d2013-04-01 15:58:15 +0000220 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000221 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
222 } else {
223 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
224 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
225 }
226
Nate Begeman1b8121b2006-01-11 21:21:00 +0000227 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000228 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
229 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000230
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000231 if (!Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000232 // PowerPC does not have Select
233 setOperationAction(ISD::SELECT, MVT::i32, Expand);
234 setOperationAction(ISD::SELECT, MVT::i64, Expand);
235 setOperationAction(ISD::SELECT, MVT::f32, Expand);
236 setOperationAction(ISD::SELECT, MVT::f64, Expand);
237 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000238
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000239 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000240 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
241 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000242
Nate Begeman7e7f4392006-02-01 07:19:44 +0000243 // PowerPC wants to optimize integer setcc a bit
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000244 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000245 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000246
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000247 // PowerPC does not have BRCOND which requires SetCC
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000248 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000249 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000250
Owen Anderson9f944592009-08-11 20:47:22 +0000251 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000252
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000253 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000254 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000255
Jim Laskey6267b2c2005-08-17 00:40:22 +0000256 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000257 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
258 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000259
Wesley Peck527da1b2010-11-23 03:31:01 +0000260 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
261 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
262 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
263 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattnerc46fc242005-12-23 05:13:35 +0000264
Chris Lattner84b49d52006-04-28 21:56:10 +0000265 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000266 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000267
Hal Finkel1996f3d2013-03-27 19:10:42 +0000268 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000269 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
270 // support continuation, user-level threading, and etc.. As a result, no
271 // other SjLj exception interfaces are implemented and please don't build
272 // your own exception handling based on them.
273 // LLVM/Clang supports zero-cost DWARF exception handling.
274 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
275 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000276
277 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000278 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000279 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
280 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000281 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000282 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
283 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
284 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
285 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000286 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000287 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
288 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000289
Nate Begemanf69d13b2008-08-11 17:36:31 +0000290 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000291 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000292
293 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000294 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
295 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000296
Nate Begemane74795c2006-01-25 18:21:52 +0000297 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000298 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000299
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000300 if (Subtarget.isSVR4ABI()) {
Evan Cheng39e90022012-07-02 22:39:56 +0000301 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000302 // VAARG always uses double-word chunks, so promote anything smaller.
303 setOperationAction(ISD::VAARG, MVT::i1, Promote);
304 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
305 setOperationAction(ISD::VAARG, MVT::i8, Promote);
306 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
307 setOperationAction(ISD::VAARG, MVT::i16, Promote);
308 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
309 setOperationAction(ISD::VAARG, MVT::i32, Promote);
310 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
311 setOperationAction(ISD::VAARG, MVT::Other, Expand);
312 } else {
313 // VAARG is custom lowered with the 32-bit SVR4 ABI.
314 setOperationAction(ISD::VAARG, MVT::Other, Custom);
315 setOperationAction(ISD::VAARG, MVT::i64, Custom);
316 }
Roman Divacky4394e682011-06-28 15:30:42 +0000317 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000318 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000319
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000320 if (Subtarget.isSVR4ABI() && !isPPC64)
Roman Divackyc3825df2013-07-25 21:36:47 +0000321 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
322 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
323 else
324 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
325
Chris Lattner5bd514d2006-01-15 09:02:48 +0000326 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000327 setOperationAction(ISD::VAEND , MVT::Other, Expand);
328 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
329 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
330 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
331 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000332
Chris Lattner6961fc72006-03-26 10:06:40 +0000333 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000334 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000335
Hal Finkel25c19922013-05-15 21:37:41 +0000336 // To handle counter-based loop conditions.
337 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
338
Dale Johannesen160be0f2008-11-07 22:54:33 +0000339 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000340 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
341 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
342 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
343 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
344 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
345 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
346 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
347 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
348 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
349 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
350 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
351 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000352
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000353 if (Subtarget.has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000354 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000355 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
356 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
357 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
358 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000359 // This is just the low 32 bits of a (signed) fp->i64 conversion.
360 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000361 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000362
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000363 if (Subtarget.hasLFIWAX() || Subtarget.isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000364 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000365 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000366 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000367 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000368 }
369
Hal Finkelf6d45f22013-04-01 17:52:07 +0000370 // With the instructions enabled under FPCVT, we can do everything.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000371 if (Subtarget.hasFPCVT()) {
372 if (Subtarget.has64BitSupport()) {
Hal Finkelf6d45f22013-04-01 17:52:07 +0000373 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
374 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
375 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
376 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
377 }
378
379 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
380 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
381 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
382 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
383 }
384
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000385 if (Subtarget.use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000386 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000387 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000388 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000389 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000390 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000391 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
392 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
393 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000394 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000395 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000396 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
397 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
398 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000399 }
Evan Cheng19264272006-03-01 01:11:20 +0000400
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000401 if (Subtarget.hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000402 // First set operation action for all vector types to expand. Then we
403 // will selectively turn on ones that can be effectively codegen'd.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000404 for (MVT VT : MVT::vector_valuetypes()) {
Chris Lattner06a21ba2006-04-16 01:37:57 +0000405 // add/sub are legal for all supported vector VT's.
Kit Barton66460332015-05-25 15:49:26 +0000406 setOperationAction(ISD::ADD , VT, Legal);
407 setOperationAction(ISD::SUB , VT, Legal);
Kit Bartond4eb73c2015-05-05 16:10:44 +0000408
Bill Schmidt433b1c32015-02-05 15:24:47 +0000409 // Vector instructions introduced in P8
Kit Bartond4eb73c2015-05-05 16:10:44 +0000410 if (Subtarget.hasP8Altivec() && (VT.SimpleTy != MVT::v1i128)) {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000411 setOperationAction(ISD::CTPOP, VT, Legal);
Bill Schmidt433b1c32015-02-05 15:24:47 +0000412 setOperationAction(ISD::CTLZ, VT, Legal);
413 }
414 else {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000415 setOperationAction(ISD::CTPOP, VT, Expand);
Bill Schmidt433b1c32015-02-05 15:24:47 +0000416 setOperationAction(ISD::CTLZ, VT, Expand);
417 }
Bill Schmidtfe88b182015-02-03 21:58:23 +0000418
Chris Lattner95c7adc2006-04-04 17:25:31 +0000419 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000420 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000421 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000422
423 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000424 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000425 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000426 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000427 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000428 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000429 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000430 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000431 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000432 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000433 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000434 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000435 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000436
Chris Lattner06a21ba2006-04-16 01:37:57 +0000437 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000438 setOperationAction(ISD::MUL , VT, Expand);
439 setOperationAction(ISD::SDIV, VT, Expand);
440 setOperationAction(ISD::SREM, VT, Expand);
441 setOperationAction(ISD::UDIV, VT, Expand);
442 setOperationAction(ISD::UREM, VT, Expand);
443 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000444 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000445 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000446 setOperationAction(ISD::FSQRT, VT, Expand);
447 setOperationAction(ISD::FLOG, VT, Expand);
448 setOperationAction(ISD::FLOG10, VT, Expand);
449 setOperationAction(ISD::FLOG2, VT, Expand);
450 setOperationAction(ISD::FEXP, VT, Expand);
451 setOperationAction(ISD::FEXP2, VT, Expand);
452 setOperationAction(ISD::FSIN, VT, Expand);
453 setOperationAction(ISD::FCOS, VT, Expand);
454 setOperationAction(ISD::FABS, VT, Expand);
455 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000456 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000457 setOperationAction(ISD::FCEIL, VT, Expand);
458 setOperationAction(ISD::FTRUNC, VT, Expand);
459 setOperationAction(ISD::FRINT, VT, Expand);
460 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000461 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
462 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
463 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
Ulrich Weigand51eccec2014-08-04 13:27:12 +0000464 setOperationAction(ISD::MULHU, VT, Expand);
465 setOperationAction(ISD::MULHS, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000466 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
467 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
468 setOperationAction(ISD::UDIVREM, VT, Expand);
469 setOperationAction(ISD::SDIVREM, VT, Expand);
470 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
471 setOperationAction(ISD::FPOW, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000472 setOperationAction(ISD::BSWAP, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000473 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000474 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000475 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000476 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000477 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
478
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000479 for (MVT InnerVT : MVT::vector_valuetypes()) {
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000480 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000481 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
482 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
483 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
484 }
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000485 }
486
Chris Lattner95c7adc2006-04-04 17:25:31 +0000487 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
488 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000489 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000490
Owen Anderson9f944592009-08-11 20:47:22 +0000491 setOperationAction(ISD::AND , MVT::v4i32, Legal);
492 setOperationAction(ISD::OR , MVT::v4i32, Legal);
493 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
494 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Hal Finkel940ab932014-02-28 00:27:01 +0000495 setOperationAction(ISD::SELECT, MVT::v4i32,
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000496 Subtarget.useCRBits() ? Legal : Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000497 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000498 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
499 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
500 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
501 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000502 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
503 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
504 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
505 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000506
Craig Topperabadc662012-04-20 06:31:50 +0000507 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
508 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
509 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
510 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000511
Owen Anderson9f944592009-08-11 20:47:22 +0000512 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000513 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000514
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000515 if (TM.Options.UnsafeFPMath || Subtarget.hasVSX()) {
Hal Finkel2e103312013-04-03 04:01:11 +0000516 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
517 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
518 }
519
Kit Barton20d39812015-03-10 19:49:38 +0000520
521 if (Subtarget.hasP8Altivec())
522 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
523 else
524 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
525
Owen Anderson9f944592009-08-11 20:47:22 +0000526 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
527 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000528
Owen Anderson9f944592009-08-11 20:47:22 +0000529 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
530 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000531
Owen Anderson9f944592009-08-11 20:47:22 +0000532 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
533 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
534 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
535 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000536
537 // Altivec does not contain unordered floating-point compare instructions
538 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
539 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000540 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
541 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000542
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000543 if (Subtarget.hasVSX()) {
Hal Finkel27774d92014-03-13 07:58:58 +0000544 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
Hal Finkel82569b62014-03-27 22:22:48 +0000545 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
Hal Finkel27774d92014-03-13 07:58:58 +0000546
547 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
548 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
549 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
550 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
551 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
552
553 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
554
555 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
556 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
557
558 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
559 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
560
Hal Finkel732f0f72014-03-26 12:49:28 +0000561 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
562 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal);
563 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
564 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
565 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
566
Hal Finkel27774d92014-03-13 07:58:58 +0000567 // Share the Altivec comparison restrictions.
568 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand);
569 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000570 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand);
571 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand);
572
Hal Finkel9281c9a2014-03-26 18:26:30 +0000573 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
574 setOperationAction(ISD::STORE, MVT::v2f64, Legal);
575
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000576 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal);
577
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +0000578 if (Subtarget.hasP8Vector())
579 addRegisterClass(MVT::f32, &PPC::VSSRCRegClass);
580
Hal Finkel19be5062014-03-29 05:29:01 +0000581 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000582
583 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass);
584 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass);
Hal Finkela6c8b512014-03-26 16:12:58 +0000585
Kit Barton0cfa7b72015-03-03 19:55:45 +0000586 if (Subtarget.hasP8Altivec()) {
Kit Bartone48b1e12015-03-05 16:24:38 +0000587 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
588 setOperationAction(ISD::SRA, MVT::v2i64, Legal);
589 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
590
Kit Barton0cfa7b72015-03-03 19:55:45 +0000591 setOperationAction(ISD::SETCC, MVT::v2i64, Legal);
592 }
593 else {
Kit Bartone48b1e12015-03-05 16:24:38 +0000594 setOperationAction(ISD::SHL, MVT::v2i64, Expand);
595 setOperationAction(ISD::SRA, MVT::v2i64, Expand);
596 setOperationAction(ISD::SRL, MVT::v2i64, Expand);
597
Kit Barton0cfa7b72015-03-03 19:55:45 +0000598 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
599
600 // VSX v2i64 only supports non-arithmetic operations.
601 setOperationAction(ISD::ADD, MVT::v2i64, Expand);
602 setOperationAction(ISD::SUB, MVT::v2i64, Expand);
603 }
Hal Finkel777c9dd2014-03-29 16:04:40 +0000604
Hal Finkel9281c9a2014-03-26 18:26:30 +0000605 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
606 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64);
607 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
608 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64);
609
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000610 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal);
611
Hal Finkel7279f4b2014-03-26 19:13:54 +0000612 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
613 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
614 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
615 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
616
Hal Finkel5c0d1452014-03-30 13:22:59 +0000617 // Vector operation legalization checks the result type of
618 // SIGN_EXTEND_INREG, overall legalization checks the inner type.
619 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal);
620 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal);
621 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
622 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
623
Hal Finkela6c8b512014-03-26 16:12:58 +0000624 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000625 }
Bill Schmidtfe88b182015-02-03 21:58:23 +0000626
Kit Bartond4eb73c2015-05-05 16:10:44 +0000627 if (Subtarget.hasP8Altivec()) {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000628 addRegisterClass(MVT::v2i64, &PPC::VRRCRegClass);
Kit Bartond4eb73c2015-05-05 16:10:44 +0000629 addRegisterClass(MVT::v1i128, &PPC::VRRCRegClass);
630 }
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000631 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000632
Hal Finkelc93a9a22015-02-25 01:06:45 +0000633 if (Subtarget.hasQPX()) {
634 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
635 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
636 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
637 setOperationAction(ISD::FREM, MVT::v4f64, Expand);
638
639 setOperationAction(ISD::FCOPYSIGN, MVT::v4f64, Legal);
640 setOperationAction(ISD::FGETSIGN, MVT::v4f64, Expand);
641
642 setOperationAction(ISD::LOAD , MVT::v4f64, Custom);
643 setOperationAction(ISD::STORE , MVT::v4f64, Custom);
644
645 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Custom);
646 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Custom);
647
648 if (!Subtarget.useCRBits())
649 setOperationAction(ISD::SELECT, MVT::v4f64, Expand);
650 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
651
652 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f64, Legal);
653 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f64, Expand);
654 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f64, Expand);
655 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f64, Expand);
656 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f64, Custom);
657 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f64, Legal);
658 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
659
660 setOperationAction(ISD::FP_TO_SINT , MVT::v4f64, Legal);
661 setOperationAction(ISD::FP_TO_UINT , MVT::v4f64, Expand);
662
663 setOperationAction(ISD::FP_ROUND , MVT::v4f32, Legal);
664 setOperationAction(ISD::FP_ROUND_INREG , MVT::v4f32, Expand);
665 setOperationAction(ISD::FP_EXTEND, MVT::v4f64, Legal);
666
667 setOperationAction(ISD::FNEG , MVT::v4f64, Legal);
668 setOperationAction(ISD::FABS , MVT::v4f64, Legal);
669 setOperationAction(ISD::FSIN , MVT::v4f64, Expand);
670 setOperationAction(ISD::FCOS , MVT::v4f64, Expand);
671 setOperationAction(ISD::FPOWI , MVT::v4f64, Expand);
672 setOperationAction(ISD::FPOW , MVT::v4f64, Expand);
673 setOperationAction(ISD::FLOG , MVT::v4f64, Expand);
674 setOperationAction(ISD::FLOG2 , MVT::v4f64, Expand);
675 setOperationAction(ISD::FLOG10 , MVT::v4f64, Expand);
676 setOperationAction(ISD::FEXP , MVT::v4f64, Expand);
677 setOperationAction(ISD::FEXP2 , MVT::v4f64, Expand);
678
679 setOperationAction(ISD::FMINNUM, MVT::v4f64, Legal);
680 setOperationAction(ISD::FMAXNUM, MVT::v4f64, Legal);
681
682 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f64, Legal);
683 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f64, Legal);
684
685 addRegisterClass(MVT::v4f64, &PPC::QFRCRegClass);
686
687 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
688 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
689 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
690 setOperationAction(ISD::FREM, MVT::v4f32, Expand);
691
692 setOperationAction(ISD::FCOPYSIGN, MVT::v4f32, Legal);
693 setOperationAction(ISD::FGETSIGN, MVT::v4f32, Expand);
694
695 setOperationAction(ISD::LOAD , MVT::v4f32, Custom);
696 setOperationAction(ISD::STORE , MVT::v4f32, Custom);
697
698 if (!Subtarget.useCRBits())
699 setOperationAction(ISD::SELECT, MVT::v4f32, Expand);
700 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
701
702 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f32, Legal);
703 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f32, Expand);
704 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f32, Expand);
705 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f32, Expand);
706 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f32, Custom);
707 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
708 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
709
710 setOperationAction(ISD::FP_TO_SINT , MVT::v4f32, Legal);
711 setOperationAction(ISD::FP_TO_UINT , MVT::v4f32, Expand);
712
713 setOperationAction(ISD::FNEG , MVT::v4f32, Legal);
714 setOperationAction(ISD::FABS , MVT::v4f32, Legal);
715 setOperationAction(ISD::FSIN , MVT::v4f32, Expand);
716 setOperationAction(ISD::FCOS , MVT::v4f32, Expand);
717 setOperationAction(ISD::FPOWI , MVT::v4f32, Expand);
718 setOperationAction(ISD::FPOW , MVT::v4f32, Expand);
719 setOperationAction(ISD::FLOG , MVT::v4f32, Expand);
720 setOperationAction(ISD::FLOG2 , MVT::v4f32, Expand);
721 setOperationAction(ISD::FLOG10 , MVT::v4f32, Expand);
722 setOperationAction(ISD::FEXP , MVT::v4f32, Expand);
723 setOperationAction(ISD::FEXP2 , MVT::v4f32, Expand);
724
725 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
726 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
727
728 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f32, Legal);
729 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f32, Legal);
730
731 addRegisterClass(MVT::v4f32, &PPC::QSRCRegClass);
732
733 setOperationAction(ISD::AND , MVT::v4i1, Legal);
734 setOperationAction(ISD::OR , MVT::v4i1, Legal);
735 setOperationAction(ISD::XOR , MVT::v4i1, Legal);
736
737 if (!Subtarget.useCRBits())
738 setOperationAction(ISD::SELECT, MVT::v4i1, Expand);
739 setOperationAction(ISD::VSELECT, MVT::v4i1, Legal);
740
741 setOperationAction(ISD::LOAD , MVT::v4i1, Custom);
742 setOperationAction(ISD::STORE , MVT::v4i1, Custom);
743
744 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4i1, Custom);
745 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4i1, Expand);
746 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4i1, Expand);
747 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4i1, Expand);
748 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4i1, Custom);
749 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i1, Expand);
750 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i1, Custom);
751
752 setOperationAction(ISD::SINT_TO_FP, MVT::v4i1, Custom);
753 setOperationAction(ISD::UINT_TO_FP, MVT::v4i1, Custom);
754
755 addRegisterClass(MVT::v4i1, &PPC::QBRCRegClass);
756
757 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
758 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
759 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
760 setOperationAction(ISD::FROUND, MVT::v4f64, Legal);
761
762 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
763 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
764 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
765 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
766
767 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Expand);
768 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
769
770 // These need to set FE_INEXACT, and so cannot be vectorized here.
771 setOperationAction(ISD::FRINT, MVT::v4f64, Expand);
772 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
773
774 if (TM.Options.UnsafeFPMath) {
775 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
776 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
777
778 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
779 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
780 } else {
781 setOperationAction(ISD::FDIV, MVT::v4f64, Expand);
782 setOperationAction(ISD::FSQRT, MVT::v4f64, Expand);
783
784 setOperationAction(ISD::FDIV, MVT::v4f32, Expand);
785 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
786 }
787 }
788
Hal Finkel01fa7702014-12-03 00:19:17 +0000789 if (Subtarget.has64BitSupport())
Hal Finkel322e41a2012-04-01 20:08:17 +0000790 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel01fa7702014-12-03 00:19:17 +0000791
792 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, isPPC64 ? Legal : Custom);
Hal Finkel322e41a2012-04-01 20:08:17 +0000793
Robin Morissete1ca44b2014-10-02 22:27:07 +0000794 if (!isPPC64) {
795 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
796 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
797 }
Eli Friedman7dfa7912011-08-29 18:23:02 +0000798
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000799 setBooleanContents(ZeroOrOneBooleanContent);
Hal Finkelc93a9a22015-02-25 01:06:45 +0000800
801 if (Subtarget.hasAltivec()) {
802 // Altivec instructions set fields to all zeros or all ones.
803 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
804 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000805
Joerg Sonnenbergerb5459e62014-07-24 22:20:10 +0000806 if (!isPPC64) {
807 // These libcalls are not available in 32-bit.
808 setLibcallName(RTLIB::SHL_I128, nullptr);
809 setLibcallName(RTLIB::SRL_I128, nullptr);
810 setLibcallName(RTLIB::SRA_I128, nullptr);
811 }
812
Evan Cheng39e90022012-07-02 22:39:56 +0000813 if (isPPC64) {
Chris Lattner454436d2006-10-18 01:20:43 +0000814 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000815 setExceptionPointerRegister(PPC::X3);
816 setExceptionSelectorRegister(PPC::X4);
817 } else {
Chris Lattner454436d2006-10-18 01:20:43 +0000818 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000819 setExceptionPointerRegister(PPC::R3);
820 setExceptionSelectorRegister(PPC::R4);
821 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000822
Chris Lattnerf4184352006-03-01 04:57:39 +0000823 // We have target-specific dag combine patterns for the following nodes:
824 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkel5efb9182015-01-06 06:01:57 +0000825 if (Subtarget.hasFPCVT())
826 setTargetDAGCombine(ISD::UINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000827 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000828 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000829 setTargetDAGCombine(ISD::BR_CC);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000830 if (Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000831 setTargetDAGCombine(ISD::BRCOND);
Chris Lattnera7976d32006-07-10 20:56:58 +0000832 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000833 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Bill Schmidtfae5d712014-12-09 16:35:51 +0000834 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
835 setTargetDAGCombine(ISD::INTRINSIC_VOID);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000836
Hal Finkel46043ed2014-03-01 21:36:57 +0000837 setTargetDAGCombine(ISD::SIGN_EXTEND);
838 setTargetDAGCombine(ISD::ZERO_EXTEND);
839 setTargetDAGCombine(ISD::ANY_EXTEND);
840
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000841 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000842 setTargetDAGCombine(ISD::TRUNCATE);
843 setTargetDAGCombine(ISD::SETCC);
844 setTargetDAGCombine(ISD::SELECT_CC);
845 }
846
Hal Finkel2e103312013-04-03 04:01:11 +0000847 // Use reciprocal estimates.
848 if (TM.Options.UnsafeFPMath) {
849 setTargetDAGCombine(ISD::FDIV);
850 setTargetDAGCombine(ISD::FSQRT);
851 }
852
Dale Johannesen10432e52007-10-19 00:59:18 +0000853 // Darwin long double math library functions have $LDBL128 appended.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000854 if (Subtarget.isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000855 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000856 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
857 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000858 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
859 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000860 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
861 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
862 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
863 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
864 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000865 }
866
Hal Finkel940ab932014-02-28 00:27:01 +0000867 // With 32 condition bits, we don't need to sink (and duplicate) compares
868 // aggressively in CodeGenPrep.
Hal Finkel7a0516e2015-02-12 01:02:52 +0000869 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000870 setHasMultipleConditionRegisters();
Hal Finkel7a0516e2015-02-12 01:02:52 +0000871 setJumpIsExpensive();
872 }
Hal Finkel940ab932014-02-28 00:27:01 +0000873
Hal Finkel65298572011-10-17 18:53:03 +0000874 setMinFunctionAlignment(2);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000875 if (Subtarget.isDarwin())
Hal Finkel65298572011-10-17 18:53:03 +0000876 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000877
Hal Finkeld73bfba2015-01-03 14:58:25 +0000878 switch (Subtarget.getDarwinDirective()) {
879 default: break;
880 case PPC::DIR_970:
881 case PPC::DIR_A2:
882 case PPC::DIR_E500mc:
883 case PPC::DIR_E5500:
884 case PPC::DIR_PWR4:
885 case PPC::DIR_PWR5:
886 case PPC::DIR_PWR5X:
887 case PPC::DIR_PWR6:
888 case PPC::DIR_PWR6X:
889 case PPC::DIR_PWR7:
890 case PPC::DIR_PWR8:
891 setPrefFunctionAlignment(4);
892 setPrefLoopAlignment(4);
893 break;
894 }
895
Eli Friedman30a49e92011-08-03 21:06:02 +0000896 setInsertFencesForAtomic(true);
897
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000898 if (Subtarget.enableMachineScheduler())
Hal Finkel21442b22013-09-11 23:05:25 +0000899 setSchedulingPreference(Sched::Source);
900 else
901 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000902
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000903 computeRegisterProperties(STI.getRegisterInfo());
Hal Finkel742b5352012-08-28 16:12:39 +0000904
Hal Finkeld73bfba2015-01-03 14:58:25 +0000905 // The Freescale cores do better with aggressive inlining of memcpy and
906 // friends. GCC uses same threshold of 128 bytes (= 32 word stores).
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000907 if (Subtarget.getDarwinDirective() == PPC::DIR_E500mc ||
908 Subtarget.getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000909 MaxStoresPerMemset = 32;
910 MaxStoresPerMemsetOptSize = 16;
911 MaxStoresPerMemcpy = 32;
912 MaxStoresPerMemcpyOptSize = 8;
913 MaxStoresPerMemmove = 32;
914 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel5c3cacf2015-02-27 19:58:28 +0000915 } else if (Subtarget.getDarwinDirective() == PPC::DIR_A2) {
916 // The A2 also benefits from (very) aggressive inlining of memcpy and
917 // friends. The overhead of a the function call, even when warm, can be
918 // over one hundred cycles.
919 MaxStoresPerMemset = 128;
920 MaxStoresPerMemcpy = 128;
921 MaxStoresPerMemmove = 128;
Hal Finkel742b5352012-08-28 16:12:39 +0000922 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000923}
924
Hal Finkel262a2242013-09-12 23:20:06 +0000925/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
926/// the desired ByVal argument alignment.
927static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
928 unsigned MaxMaxAlign) {
929 if (MaxAlign == MaxMaxAlign)
930 return;
931 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
932 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
933 MaxAlign = 32;
934 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
935 MaxAlign = 16;
936 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
937 unsigned EltAlign = 0;
938 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
939 if (EltAlign > MaxAlign)
940 MaxAlign = EltAlign;
941 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
942 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
943 unsigned EltAlign = 0;
944 getMaxByValAlign(STy->getElementType(i), EltAlign, MaxMaxAlign);
945 if (EltAlign > MaxAlign)
946 MaxAlign = EltAlign;
947 if (MaxAlign == MaxMaxAlign)
948 break;
949 }
950 }
951}
952
Dale Johannesencbde4c22008-02-28 22:31:51 +0000953/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
954/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000955unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000956 // Darwin passes everything on 4 byte boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000957 if (Subtarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000958 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000959
960 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000961 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000962 unsigned Align = Subtarget.isPPC64() ? 8 : 4;
963 if (Subtarget.hasAltivec() || Subtarget.hasQPX())
964 getMaxByValAlign(Ty, Align, Subtarget.hasQPX() ? 32 : 16);
Hal Finkel262a2242013-09-12 23:20:06 +0000965 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000966}
967
Chris Lattner347ed8a2006-01-09 23:52:17 +0000968const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +0000969 switch ((PPCISD::NodeType)Opcode) {
970 case PPCISD::FIRST_NUMBER: break;
Evan Cheng32e376f2008-07-12 02:23:19 +0000971 case PPCISD::FSEL: return "PPCISD::FSEL";
972 case PPCISD::FCFID: return "PPCISD::FCFID";
Hal Finkel3fe09ea2015-01-06 07:02:15 +0000973 case PPCISD::FCFIDU: return "PPCISD::FCFIDU";
974 case PPCISD::FCFIDS: return "PPCISD::FCFIDS";
975 case PPCISD::FCFIDUS: return "PPCISD::FCFIDUS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000976 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
977 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel3fe09ea2015-01-06 07:02:15 +0000978 case PPCISD::FCTIDUZ: return "PPCISD::FCTIDUZ";
979 case PPCISD::FCTIWUZ: return "PPCISD::FCTIWUZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000980 case PPCISD::FRE: return "PPCISD::FRE";
981 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000982 case PPCISD::STFIWX: return "PPCISD::STFIWX";
983 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
984 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
985 case PPCISD::VPERM: return "PPCISD::VPERM";
Hal Finkel4edc66b2015-01-03 01:16:37 +0000986 case PPCISD::CMPB: return "PPCISD::CMPB";
Evan Cheng32e376f2008-07-12 02:23:19 +0000987 case PPCISD::Hi: return "PPCISD::Hi";
988 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000989 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Evan Cheng32e376f2008-07-12 02:23:19 +0000990 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
991 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
992 case PPCISD::SRL: return "PPCISD::SRL";
993 case PPCISD::SRA: return "PPCISD::SRA";
994 case PPCISD::SHL: return "PPCISD::SHL";
Matthias Braund04893f2015-05-07 21:33:59 +0000995 case PPCISD::SRA_ADDZE: return "PPCISD::SRA_ADDZE";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000996 case PPCISD::CALL: return "PPCISD::CALL";
997 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000998 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000999 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Hal Finkelfc096c92014-12-23 22:29:40 +00001000 case PPCISD::BCTRL_LOAD_TOC: return "PPCISD::BCTRL_LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +00001001 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkelbbdee932014-12-02 22:01:00 +00001002 case PPCISD::READ_TIME_BASE: return "PPCISD::READ_TIME_BASE";
Hal Finkel756810f2013-03-21 21:37:52 +00001003 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
1004 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001005 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001006 case PPCISD::MFVSR: return "PPCISD::MFVSR";
1007 case PPCISD::MTVSRA: return "PPCISD::MTVSRA";
1008 case PPCISD::MTVSRZ: return "PPCISD::MTVSRZ";
Matthias Braund04893f2015-05-07 21:33:59 +00001009 case PPCISD::ANDIo_1_EQ_BIT: return "PPCISD::ANDIo_1_EQ_BIT";
1010 case PPCISD::ANDIo_1_GT_BIT: return "PPCISD::ANDIo_1_GT_BIT";
Evan Cheng32e376f2008-07-12 02:23:19 +00001011 case PPCISD::VCMP: return "PPCISD::VCMP";
1012 case PPCISD::VCMPo: return "PPCISD::VCMPo";
1013 case PPCISD::LBRX: return "PPCISD::LBRX";
1014 case PPCISD::STBRX: return "PPCISD::STBRX";
Hal Finkel3fe09ea2015-01-06 07:02:15 +00001015 case PPCISD::LFIWAX: return "PPCISD::LFIWAX";
1016 case PPCISD::LFIWZX: return "PPCISD::LFIWZX";
Matthias Braund04893f2015-05-07 21:33:59 +00001017 case PPCISD::LXVD2X: return "PPCISD::LXVD2X";
1018 case PPCISD::STXVD2X: return "PPCISD::STXVD2X";
Evan Cheng32e376f2008-07-12 02:23:19 +00001019 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +00001020 case PPCISD::BDNZ: return "PPCISD::BDNZ";
1021 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +00001022 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +00001023 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +00001024 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +00001025 case PPCISD::CR6SET: return "PPCISD::CR6SET";
1026 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Roman Divacky32143e22013-12-20 18:08:54 +00001027 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Matthias Braund04893f2015-05-07 21:33:59 +00001028 case PPCISD::PPC32_PICGOT: return "PPCISD::PPC32_PICGOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001029 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
1030 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001031 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001032 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
1033 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
Bill Schmidt82f1c772015-02-10 19:09:05 +00001034 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
1035 case PPCISD::ADDI_TLSGD_L_ADDR: return "PPCISD::ADDI_TLSGD_L_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001036 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
1037 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
Bill Schmidt82f1c772015-02-10 19:09:05 +00001038 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
1039 case PPCISD::ADDI_TLSLD_L_ADDR: return "PPCISD::ADDI_TLSLD_L_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001040 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
1041 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +00001042 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +00001043 case PPCISD::SC: return "PPCISD::SC";
Bill Schmidte26236e2015-05-22 16:44:10 +00001044 case PPCISD::CLRBHRB: return "PPCISD::CLRBHRB";
1045 case PPCISD::MFBHRBE: return "PPCISD::MFBHRBE";
1046 case PPCISD::RFEBB: return "PPCISD::RFEBB";
Matthias Braund04893f2015-05-07 21:33:59 +00001047 case PPCISD::XXSWAPD: return "PPCISD::XXSWAPD";
Hal Finkelc93a9a22015-02-25 01:06:45 +00001048 case PPCISD::QVFPERM: return "PPCISD::QVFPERM";
1049 case PPCISD::QVGPCI: return "PPCISD::QVGPCI";
1050 case PPCISD::QVALIGNI: return "PPCISD::QVALIGNI";
1051 case PPCISD::QVESPLATI: return "PPCISD::QVESPLATI";
1052 case PPCISD::QBFLT: return "PPCISD::QBFLT";
1053 case PPCISD::QVLFSb: return "PPCISD::QVLFSb";
Chris Lattner347ed8a2006-01-09 23:52:17 +00001054 }
Matthias Braund04893f2015-05-07 21:33:59 +00001055 return nullptr;
Chris Lattner347ed8a2006-01-09 23:52:17 +00001056}
1057
Hal Finkelc93a9a22015-02-25 01:06:45 +00001058EVT PPCTargetLowering::getSetCCResultType(LLVMContext &C, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00001059 if (!VT.isVector())
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001060 return Subtarget.useCRBits() ? MVT::i1 : MVT::i32;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001061
1062 if (Subtarget.hasQPX())
1063 return EVT::getVectorVT(C, MVT::i1, VT.getVectorNumElements());
1064
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00001065 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +00001066}
1067
Hal Finkel62ac7362014-09-19 11:42:56 +00001068bool PPCTargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1069 assert(VT.isFloatingPoint() && "Non-floating-point FMA?");
1070 return true;
1071}
1072
Chris Lattner4211ca92006-04-14 06:01:58 +00001073//===----------------------------------------------------------------------===//
1074// Node matching predicates, for use by the tblgen matching code.
1075//===----------------------------------------------------------------------===//
1076
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001077/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001078static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001079 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001080 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001081 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001082 // Maybe this has already been legalized into the constant pool?
1083 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001084 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001085 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001086 }
1087 return false;
1088}
1089
Chris Lattnere8b83b42006-04-06 17:23:16 +00001090/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
1091/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001092static bool isConstantOrUndef(int Op, int Val) {
1093 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001094}
1095
1096/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
1097/// VPKUHUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001098/// The ShuffleKind distinguishes between big-endian operations with
1099/// two different inputs (0), either-endian operations with two identical
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001100/// inputs (1), and little-endian operations with two different inputs (2).
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001101/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1102bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +00001103 SelectionDAG &DAG) {
Eric Christopher8b770652015-01-26 19:03:15 +00001104 bool IsLE = DAG.getTarget().getDataLayout()->isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001105 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +00001106 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001107 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001108 for (unsigned i = 0; i != 16; ++i)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001109 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001110 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001111 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +00001112 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001113 return false;
1114 for (unsigned i = 0; i != 16; ++i)
1115 if (!isConstantOrUndef(N->getMaskElt(i), i*2))
1116 return false;
1117 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +00001118 unsigned j = IsLE ? 0 : 1;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001119 for (unsigned i = 0; i != 8; ++i)
Bill Schmidtf910a062014-06-10 14:35:01 +00001120 if (!isConstantOrUndef(N->getMaskElt(i), i*2+j) ||
1121 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001122 return false;
1123 }
Chris Lattner1d338192006-04-06 18:26:28 +00001124 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001125}
1126
1127/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
1128/// VPKUWUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001129/// The ShuffleKind distinguishes between big-endian operations with
1130/// two different inputs (0), either-endian operations with two identical
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001131/// inputs (1), and little-endian operations with two different inputs (2).
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001132/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1133bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +00001134 SelectionDAG &DAG) {
Eric Christopher8b770652015-01-26 19:03:15 +00001135 bool IsLE = DAG.getTarget().getDataLayout()->isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001136 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +00001137 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001138 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001139 for (unsigned i = 0; i != 16; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001140 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
1141 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001142 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001143 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +00001144 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001145 return false;
1146 for (unsigned i = 0; i != 16; i += 2)
1147 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) ||
1148 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1))
1149 return false;
1150 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +00001151 unsigned j = IsLE ? 0 : 2;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001152 for (unsigned i = 0; i != 8; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001153 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) ||
1154 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) ||
1155 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) ||
1156 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001157 return false;
1158 }
Chris Lattner1d338192006-04-06 18:26:28 +00001159 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001160}
1161
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001162/// isVPKUDUMShuffleMask - Return true if this is the shuffle mask for a
Bill Schmidte13ac912015-05-21 20:48:49 +00001163/// VPKUDUM instruction, AND the VPKUDUM instruction exists for the
1164/// current subtarget.
1165///
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001166/// The ShuffleKind distinguishes between big-endian operations with
1167/// two different inputs (0), either-endian operations with two identical
1168/// inputs (1), and little-endian operations with two different inputs (2).
1169/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1170bool PPC::isVPKUDUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
1171 SelectionDAG &DAG) {
Bill Schmidte13ac912015-05-21 20:48:49 +00001172 const PPCSubtarget& Subtarget =
1173 static_cast<const PPCSubtarget&>(DAG.getSubtarget());
1174 if (!Subtarget.hasP8Vector())
1175 return false;
1176
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001177 bool IsLE = DAG.getTarget().getDataLayout()->isLittleEndian();
1178 if (ShuffleKind == 0) {
1179 if (IsLE)
1180 return false;
1181 for (unsigned i = 0; i != 16; i += 4)
1182 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+4) ||
1183 !isConstantOrUndef(N->getMaskElt(i+1), i*2+5) ||
1184 !isConstantOrUndef(N->getMaskElt(i+2), i*2+6) ||
1185 !isConstantOrUndef(N->getMaskElt(i+3), i*2+7))
1186 return false;
1187 } else if (ShuffleKind == 2) {
1188 if (!IsLE)
1189 return false;
1190 for (unsigned i = 0; i != 16; i += 4)
1191 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) ||
1192 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1) ||
1193 !isConstantOrUndef(N->getMaskElt(i+2), i*2+2) ||
1194 !isConstantOrUndef(N->getMaskElt(i+3), i*2+3))
1195 return false;
1196 } else if (ShuffleKind == 1) {
1197 unsigned j = IsLE ? 0 : 4;
1198 for (unsigned i = 0; i != 8; i += 4)
1199 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) ||
1200 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) ||
1201 !isConstantOrUndef(N->getMaskElt(i+2), i*2+j+2) ||
1202 !isConstantOrUndef(N->getMaskElt(i+3), i*2+j+3) ||
1203 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) ||
1204 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1) ||
1205 !isConstantOrUndef(N->getMaskElt(i+10), i*2+j+2) ||
1206 !isConstantOrUndef(N->getMaskElt(i+11), i*2+j+3))
1207 return false;
1208 }
1209 return true;
1210}
1211
Chris Lattnerf38e0332006-04-06 22:02:42 +00001212/// isVMerge - Common function, used to match vmrg* shuffles.
1213///
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001214static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +00001215 unsigned LHSStart, unsigned RHSStart) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001216 if (N->getValueType(0) != MVT::v16i8)
1217 return false;
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001218 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
1219 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001220
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001221 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
1222 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001223 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +00001224 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001225 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +00001226 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001227 return false;
1228 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001229 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +00001230}
1231
1232/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +00001233/// a VMRGL* instruction with the specified unit size (1,2 or 4 bytes).
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001234/// The ShuffleKind distinguishes between big-endian merges with two
1235/// different inputs (0), either-endian merges with two identical inputs (1),
1236/// and little-endian merges with two different inputs (2). For the latter,
1237/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +00001238bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001239 unsigned ShuffleKind, SelectionDAG &DAG) {
Eric Christopher8b770652015-01-26 19:03:15 +00001240 if (DAG.getTarget().getDataLayout()->isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001241 if (ShuffleKind == 1) // unary
1242 return isVMerge(N, UnitSize, 0, 0);
1243 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +00001244 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001245 else
1246 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001247 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001248 if (ShuffleKind == 1) // unary
1249 return isVMerge(N, UnitSize, 8, 8);
1250 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +00001251 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001252 else
1253 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001254 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001255}
1256
1257/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +00001258/// a VMRGH* instruction with the specified unit size (1,2 or 4 bytes).
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001259/// The ShuffleKind distinguishes between big-endian merges with two
1260/// different inputs (0), either-endian merges with two identical inputs (1),
1261/// and little-endian merges with two different inputs (2). For the latter,
1262/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +00001263bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001264 unsigned ShuffleKind, SelectionDAG &DAG) {
Eric Christopher8b770652015-01-26 19:03:15 +00001265 if (DAG.getTarget().getDataLayout()->isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001266 if (ShuffleKind == 1) // unary
1267 return isVMerge(N, UnitSize, 8, 8);
1268 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +00001269 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001270 else
1271 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001272 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001273 if (ShuffleKind == 1) // unary
1274 return isVMerge(N, UnitSize, 0, 0);
1275 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +00001276 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001277 else
1278 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001279 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001280}
1281
1282
Chris Lattner1d338192006-04-06 18:26:28 +00001283/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
1284/// amount, otherwise return -1.
Bill Schmidt42a69362014-08-05 20:47:25 +00001285/// The ShuffleKind distinguishes between big-endian operations with two
1286/// different inputs (0), either-endian operations with two identical inputs
1287/// (1), and little-endian operations with two different inputs (2). For the
1288/// latter, the input operands are swapped (see PPCInstrAltivec.td).
1289int PPC::isVSLDOIShuffleMask(SDNode *N, unsigned ShuffleKind,
1290 SelectionDAG &DAG) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001291 if (N->getValueType(0) != MVT::v16i8)
Hal Finkela775e512014-04-08 19:00:27 +00001292 return -1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001293
1294 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +00001295
Chris Lattner1d338192006-04-06 18:26:28 +00001296 // Find the first non-undef value in the shuffle mask.
1297 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001298 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +00001299 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001300
Chris Lattner1d338192006-04-06 18:26:28 +00001301 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001302
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001303 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +00001304 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001305 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +00001306 if (ShiftAmt < i) return -1;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001307
Bill Schmidtf04e9982014-08-04 23:21:01 +00001308 ShiftAmt -= i;
Eric Christopher8b770652015-01-26 19:03:15 +00001309 bool isLE = DAG.getTarget().getDataLayout()->isLittleEndian();
Bill Schmidtf910a062014-06-10 14:35:01 +00001310
Bill Schmidt42a69362014-08-05 20:47:25 +00001311 if ((ShuffleKind == 0 && !isLE) || (ShuffleKind == 2 && isLE)) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001312 // Check the rest of the elements to see if they are consecutive.
1313 for (++i; i != 16; ++i)
1314 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
1315 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001316 } else if (ShuffleKind == 1) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001317 // Check the rest of the elements to see if they are consecutive.
1318 for (++i; i != 16; ++i)
1319 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
1320 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001321 } else
1322 return -1;
1323
1324 if (ShuffleKind == 2 && isLE)
1325 ShiftAmt = 16 - ShiftAmt;
Bill Schmidtf04e9982014-08-04 23:21:01 +00001326
Chris Lattner1d338192006-04-06 18:26:28 +00001327 return ShiftAmt;
1328}
Chris Lattnerffc47562006-03-20 06:33:01 +00001329
1330/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
1331/// specifies a splat of a single element that is suitable for input to
1332/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001333bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +00001334 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +00001335 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001336
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001337 // This is a splat operation if each element of the permute is the same, and
1338 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001339 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001340
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001341 // FIXME: Handle UNDEF elements too!
1342 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +00001343 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001344
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001345 // Check that the indices are consecutive, in the case of a multi-byte element
1346 // splatted with a v16i8 mask.
1347 for (unsigned i = 1; i != EltSize; ++i)
1348 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001349 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001350
Chris Lattner95c7adc2006-04-04 17:25:31 +00001351 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001352 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +00001353 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001354 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001355 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001356 }
Chris Lattner95c7adc2006-04-04 17:25:31 +00001357 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +00001358}
1359
1360/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
1361/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Bill Schmidtf910a062014-06-10 14:35:01 +00001362unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize,
1363 SelectionDAG &DAG) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001364 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1365 assert(isSplatShuffleMask(SVOp, EltSize));
Eric Christopher8b770652015-01-26 19:03:15 +00001366 if (DAG.getTarget().getDataLayout()->isLittleEndian())
Bill Schmidtf910a062014-06-10 14:35:01 +00001367 return (16 / EltSize) - 1 - (SVOp->getMaskElt(0) / EltSize);
1368 else
1369 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +00001370}
1371
Chris Lattner74cf9ff2006-04-12 17:37:20 +00001372/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001373/// by using a vspltis[bhw] instruction of the specified element size, return
1374/// the constant being splatted. The ByteSize field indicates the number of
1375/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001376SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001377 SDValue OpVal(nullptr, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001378
1379 // If ByteSize of the splat is bigger than the element size of the
1380 // build_vector, then we have a case where we are checking for a splat where
1381 // multiple elements of the buildvector are folded together into a single
1382 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
1383 unsigned EltSize = 16/N->getNumOperands();
1384 if (EltSize < ByteSize) {
1385 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001386 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001387 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001388
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001389 // See if all of the elements in the buildvector agree across.
1390 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1391 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1392 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001393 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001394
Scott Michelcf0da6c2009-02-17 22:15:04 +00001395
Craig Topper062a2ba2014-04-25 05:30:21 +00001396 if (!UniquedVals[i&(Multiple-1)].getNode())
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001397 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
1398 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001399 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001400 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001401
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001402 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
1403 // either constant or undef values that are identical for each chunk. See
1404 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001405
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001406 // Check to see if all of the leading entries are either 0 or -1. If
1407 // neither, then this won't fit into the immediate field.
1408 bool LeadingZero = true;
1409 bool LeadingOnes = true;
1410 for (unsigned i = 0; i != Multiple-1; ++i) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001411 if (!UniquedVals[i].getNode()) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001412
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001413 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
1414 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
1415 }
1416 // Finally, check the least significant entry.
1417 if (LeadingZero) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001418 if (!UniquedVals[Multiple-1].getNode())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001419 return DAG.getTargetConstant(0, SDLoc(N), MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00001420 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001421 if (Val < 16) // 0,0,0,4 -> vspltisw(4)
1422 return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001423 }
1424 if (LeadingOnes) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001425 if (!UniquedVals[Multiple-1].getNode())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001426 return DAG.getTargetConstant(~0U, SDLoc(N), MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +00001427 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001428 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001429 return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001430 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001431
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001432 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001433 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001434
Chris Lattner2771e2c2006-03-25 06:12:06 +00001435 // Check to see if this buildvec has a single non-undef value in its elements.
1436 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1437 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Craig Topper062a2ba2014-04-25 05:30:21 +00001438 if (!OpVal.getNode())
Chris Lattner2771e2c2006-03-25 06:12:06 +00001439 OpVal = N->getOperand(i);
1440 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001441 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001442 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001443
Craig Topper062a2ba2014-04-25 05:30:21 +00001444 if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001445
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001446 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001447 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001448 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001449 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001450 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001451 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001452 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001453 }
1454
1455 // If the splat value is larger than the element value, then we can never do
1456 // this splat. The only case that we could fit the replicated bits into our
1457 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001458 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001459
Benjamin Kramerb4b51502015-03-25 16:49:59 +00001460 // If the element value is larger than the splat value, check if it consists
1461 // of a repeated bit pattern of size ByteSize.
1462 if (!APInt(ValSizeInBytes * 8, Value).isSplat(ByteSize * 8))
1463 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001464
1465 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001466 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001467
Evan Chengb1ddc982006-03-26 09:52:32 +00001468 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001469 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001470
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001471 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001472 if (SignExtend32<5>(MaskVal) == MaskVal)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001473 return DAG.getTargetConstant(MaskVal, SDLoc(N), MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001474 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001475}
1476
Hal Finkelc93a9a22015-02-25 01:06:45 +00001477/// isQVALIGNIShuffleMask - If this is a qvaligni shuffle mask, return the shift
1478/// amount, otherwise return -1.
1479int PPC::isQVALIGNIShuffleMask(SDNode *N) {
1480 EVT VT = N->getValueType(0);
1481 if (VT != MVT::v4f64 && VT != MVT::v4f32 && VT != MVT::v4i1)
1482 return -1;
1483
1484 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1485
1486 // Find the first non-undef value in the shuffle mask.
1487 unsigned i;
1488 for (i = 0; i != 4 && SVOp->getMaskElt(i) < 0; ++i)
1489 /*search*/;
1490
1491 if (i == 4) return -1; // all undef.
1492
1493 // Otherwise, check to see if the rest of the elements are consecutively
1494 // numbered from this value.
1495 unsigned ShiftAmt = SVOp->getMaskElt(i);
1496 if (ShiftAmt < i) return -1;
1497 ShiftAmt -= i;
1498
1499 // Check the rest of the elements to see if they are consecutive.
1500 for (++i; i != 4; ++i)
1501 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
1502 return -1;
1503
1504 return ShiftAmt;
1505}
1506
Chris Lattner4211ca92006-04-14 06:01:58 +00001507//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001508// Addressing Mode Selection
1509//===----------------------------------------------------------------------===//
1510
1511/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1512/// or 64-bit immediate, and if the value can be accurately represented as a
1513/// sign extension from a 16-bit value. If so, this returns true and the
1514/// immediate.
1515static bool isIntS16Immediate(SDNode *N, short &Imm) {
Adam Nemet571eb5f2014-05-20 17:20:34 +00001516 if (!isa<ConstantSDNode>(N))
Chris Lattnera801fced2006-11-08 02:15:41 +00001517 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001518
Dan Gohmaneffb8942008-09-12 16:56:44 +00001519 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001520 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001521 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001522 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001523 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001524}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001525static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001526 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001527}
1528
1529
1530/// SelectAddressRegReg - Given the specified addressed, check to see if it
1531/// can be represented as an indexed [r+r] operation. Returns false if it
1532/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001533bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1534 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001535 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001536 short imm = 0;
1537 if (N.getOpcode() == ISD::ADD) {
1538 if (isIntS16Immediate(N.getOperand(1), imm))
1539 return false; // r+i
1540 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1541 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001542
Chris Lattnera801fced2006-11-08 02:15:41 +00001543 Base = N.getOperand(0);
1544 Index = N.getOperand(1);
1545 return true;
1546 } else if (N.getOpcode() == ISD::OR) {
1547 if (isIntS16Immediate(N.getOperand(1), imm))
1548 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001549
Chris Lattnera801fced2006-11-08 02:15:41 +00001550 // If this is an or of disjoint bitfields, we can codegen this as an add
1551 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1552 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001553 APInt LHSKnownZero, LHSKnownOne;
1554 APInt RHSKnownZero, RHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001555 DAG.computeKnownBits(N.getOperand(0),
1556 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001557
Dan Gohmanf19609a2008-02-27 01:23:58 +00001558 if (LHSKnownZero.getBoolValue()) {
Jay Foada0653a32014-05-14 21:14:37 +00001559 DAG.computeKnownBits(N.getOperand(1),
1560 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001561 // If all of the bits are known zero on the LHS or RHS, the add won't
1562 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001563 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001564 Base = N.getOperand(0);
1565 Index = N.getOperand(1);
1566 return true;
1567 }
1568 }
1569 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001570
Chris Lattnera801fced2006-11-08 02:15:41 +00001571 return false;
1572}
1573
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001574// If we happen to be doing an i64 load or store into a stack slot that has
1575// less than a 4-byte alignment, then the frame-index elimination may need to
1576// use an indexed load or store instruction (because the offset may not be a
1577// multiple of 4). The extra register needed to hold the offset comes from the
1578// register scavenger, and it is possible that the scavenger will need to use
1579// an emergency spill slot. As a result, we need to make sure that a spill slot
1580// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1581// stack slot.
1582static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1583 // FIXME: This does not handle the LWA case.
1584 if (VT != MVT::i64)
1585 return;
1586
Hal Finkel7ab3db52013-07-10 15:29:01 +00001587 // NOTE: We'll exclude negative FIs here, which come from argument
1588 // lowering, because there are no known test cases triggering this problem
1589 // using packed structures (or similar). We can remove this exclusion if
1590 // we find such a test case. The reason why this is so test-case driven is
1591 // because this entire 'fixup' is only to prevent crashes (from the
1592 // register scavenger) on not-really-valid inputs. For example, if we have:
1593 // %a = alloca i1
1594 // %b = bitcast i1* %a to i64*
1595 // store i64* a, i64 b
1596 // then the store should really be marked as 'align 1', but is not. If it
1597 // were marked as 'align 1' then the indexed form would have been
1598 // instruction-selected initially, and the problem this 'fixup' is preventing
1599 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001600 if (FrameIdx < 0)
1601 return;
1602
1603 MachineFunction &MF = DAG.getMachineFunction();
1604 MachineFrameInfo *MFI = MF.getFrameInfo();
1605
1606 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1607 if (Align >= 4)
1608 return;
1609
1610 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1611 FuncInfo->setHasNonRISpills();
1612}
1613
Chris Lattnera801fced2006-11-08 02:15:41 +00001614/// Returns true if the address N can be represented by a base register plus
1615/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001616/// represented as reg+reg. If Aligned is true, only accept displacements
1617/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001618bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001619 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001620 SelectionDAG &DAG,
1621 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001622 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001623 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001624 // If this can be more profitably realized as r+r, fail.
1625 if (SelectAddressRegReg(N, Disp, Base, DAG))
1626 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001627
Chris Lattnera801fced2006-11-08 02:15:41 +00001628 if (N.getOpcode() == ISD::ADD) {
1629 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001630 if (isIntS16Immediate(N.getOperand(1), imm) &&
1631 (!Aligned || (imm & 3) == 0)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001632 Disp = DAG.getTargetConstant(imm, dl, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001633 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1634 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001635 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001636 } else {
1637 Base = N.getOperand(0);
1638 }
1639 return true; // [r+i]
1640 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1641 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001642 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001643 && "Cannot handle constant offsets yet!");
1644 Disp = N.getOperand(1).getOperand(0); // The global address.
1645 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001646 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001647 Disp.getOpcode() == ISD::TargetConstantPool ||
1648 Disp.getOpcode() == ISD::TargetJumpTable);
1649 Base = N.getOperand(0);
1650 return true; // [&g+r]
1651 }
1652 } else if (N.getOpcode() == ISD::OR) {
1653 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001654 if (isIntS16Immediate(N.getOperand(1), imm) &&
1655 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001656 // If this is an or of disjoint bitfields, we can codegen this as an add
1657 // (for better address arithmetic) if the LHS and RHS of the OR are
1658 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001659 APInt LHSKnownZero, LHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001660 DAG.computeKnownBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001661
Dan Gohmanf19609a2008-02-27 01:23:58 +00001662 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001663 // If all of the bits are known zero on the LHS or RHS, the add won't
1664 // carry.
Ulrich Weigand55a96652014-07-20 22:26:40 +00001665 if (FrameIndexSDNode *FI =
1666 dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1667 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1668 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1669 } else {
1670 Base = N.getOperand(0);
1671 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001672 Disp = DAG.getTargetConstant(imm, dl, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001673 return true;
1674 }
1675 }
1676 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1677 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001678
Chris Lattnera801fced2006-11-08 02:15:41 +00001679 // If this address fits entirely in a 16-bit sext immediate field, codegen
1680 // this as "d, 0"
1681 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001682 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001683 Disp = DAG.getTargetConstant(Imm, dl, CN->getValueType(0));
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001684 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001685 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001686 return true;
1687 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001688
1689 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001690 if ((CN->getValueType(0) == MVT::i32 ||
1691 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1692 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001693 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001694
Chris Lattnera801fced2006-11-08 02:15:41 +00001695 // Otherwise, break this down into an LIS + disp.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001696 Disp = DAG.getTargetConstant((short)Addr, dl, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001697
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001698 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, dl,
1699 MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00001700 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001701 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001702 return true;
1703 }
1704 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001705
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001706 Disp = DAG.getTargetConstant(0, dl, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001707 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001708 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001709 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1710 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001711 Base = N;
1712 return true; // [r+0]
1713}
1714
1715/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1716/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001717bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1718 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001719 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001720 // Check to see if we can easily represent this as an [r+r] address. This
1721 // will fail if it thinks that the address is more profitably represented as
1722 // reg+imm, e.g. where imm = 0.
1723 if (SelectAddressRegReg(N, Base, Index, DAG))
1724 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001725
Chris Lattnera801fced2006-11-08 02:15:41 +00001726 // If the operand is an addition, always emit this as [r+r], since this is
1727 // better (for code size, and execution, as the memop does the add for free)
1728 // than emitting an explicit add.
1729 if (N.getOpcode() == ISD::ADD) {
1730 Base = N.getOperand(0);
1731 Index = N.getOperand(1);
1732 return true;
1733 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001734
Chris Lattnera801fced2006-11-08 02:15:41 +00001735 // Otherwise, do it the hard way, using R0 as the base register.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001736 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001737 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001738 Index = N;
1739 return true;
1740}
1741
Chris Lattnera801fced2006-11-08 02:15:41 +00001742/// getPreIndexedAddressParts - returns true by value, base pointer and
1743/// offset pointer and addressing mode by reference if the node's address
1744/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001745bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1746 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001747 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001748 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001749 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001750
Ulrich Weigande90b0222013-03-22 14:58:48 +00001751 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001752 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001753 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001754 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001755 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1756 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001757 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001758 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001759 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001760 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001761 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001762 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001763 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001764 } else
1765 return false;
1766
Hal Finkelc93a9a22015-02-25 01:06:45 +00001767 // PowerPC doesn't have preinc load/store instructions for vectors (except
1768 // for QPX, which does have preinc r+r forms).
1769 if (VT.isVector()) {
1770 if (!Subtarget.hasQPX() || (VT != MVT::v4f64 && VT != MVT::v4f32)) {
1771 return false;
1772 } else if (SelectAddressRegRegOnly(Ptr, Offset, Base, DAG)) {
1773 AM = ISD::PRE_INC;
1774 return true;
1775 }
1776 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001777
Ulrich Weigande90b0222013-03-22 14:58:48 +00001778 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1779
1780 // Common code will reject creating a pre-inc form if the base pointer
1781 // is a frame index, or if N is a store and the base pointer is either
1782 // the same as or a predecessor of the value being stored. Check for
1783 // those situations here, and try with swapped Base/Offset instead.
1784 bool Swap = false;
1785
1786 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1787 Swap = true;
1788 else if (!isLoad) {
1789 SDValue Val = cast<StoreSDNode>(N)->getValue();
1790 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1791 Swap = true;
1792 }
1793
1794 if (Swap)
1795 std::swap(Base, Offset);
1796
Hal Finkelca542be2012-06-20 15:43:03 +00001797 AM = ISD::PRE_INC;
1798 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001799 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001800
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001801 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001802 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001803 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001804 return false;
1805 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001806 // LDU/STU need an address with at least 4-byte alignment.
1807 if (Alignment < 4)
1808 return false;
1809
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001810 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001811 return false;
1812 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001813
Chris Lattnerb314b152006-11-11 00:08:42 +00001814 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001815 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1816 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001817 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001818 LD->getExtensionType() == ISD::SEXTLOAD &&
1819 isa<ConstantSDNode>(Offset))
1820 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001821 }
1822
Chris Lattnerce645542006-11-10 02:08:47 +00001823 AM = ISD::PRE_INC;
1824 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001825}
1826
1827//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001828// LowerOperation implementation
1829//===----------------------------------------------------------------------===//
1830
Chris Lattneredb9d842010-11-15 02:46:57 +00001831/// GetLabelAccessInfo - Return true if we should reference labels using a
1832/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
Eric Christophercccae792015-01-30 22:02:31 +00001833static bool GetLabelAccessInfo(const TargetMachine &TM,
1834 const PPCSubtarget &Subtarget,
1835 unsigned &HiOpFlags, unsigned &LoOpFlags,
Craig Topper062a2ba2014-04-25 05:30:21 +00001836 const GlobalValue *GV = nullptr) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001837 HiOpFlags = PPCII::MO_HA;
1838 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001839
Hal Finkel3ee2af72014-07-18 23:29:49 +00001840 // Don't use the pic base if not in PIC relocation model.
1841 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
1842
Chris Lattnerdd6df842010-11-15 03:13:19 +00001843 if (isPIC) {
1844 HiOpFlags |= PPCII::MO_PIC_FLAG;
1845 LoOpFlags |= PPCII::MO_PIC_FLAG;
1846 }
1847
1848 // If this is a reference to a global value that requires a non-lazy-ptr, make
1849 // sure that instruction lowering adds it.
Eric Christophere8dbfe12015-02-13 22:23:04 +00001850 if (GV && Subtarget.hasLazyResolverStub(GV)) {
Chris Lattnerdd6df842010-11-15 03:13:19 +00001851 HiOpFlags |= PPCII::MO_NLP_FLAG;
1852 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001853
Chris Lattnerdd6df842010-11-15 03:13:19 +00001854 if (GV->hasHiddenVisibility()) {
1855 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1856 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1857 }
1858 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001859
Chris Lattneredb9d842010-11-15 02:46:57 +00001860 return isPIC;
1861}
1862
1863static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1864 SelectionDAG &DAG) {
Daniel Jasper48e93f72015-04-28 13:38:35 +00001865 SDLoc DL(HiPart);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001866 EVT PtrVT = HiPart.getValueType();
1867 SDValue Zero = DAG.getConstant(0, DL, PtrVT);
Chris Lattneredb9d842010-11-15 02:46:57 +00001868
1869 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1870 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001871
Chris Lattneredb9d842010-11-15 02:46:57 +00001872 // With PIC, the first instruction is actually "GR+hi(&G)".
1873 if (isPIC)
1874 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1875 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001876
Chris Lattneredb9d842010-11-15 02:46:57 +00001877 // Generate non-pic code that has direct accesses to the constant pool.
1878 // The address of the global is just (hi(&g)+lo(&g)).
1879 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1880}
1881
Hal Finkele6698d52015-02-01 15:03:28 +00001882static void setUsesTOCBasePtr(MachineFunction &MF) {
1883 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1884 FuncInfo->setUsesTOCBasePtr();
1885}
1886
1887static void setUsesTOCBasePtr(SelectionDAG &DAG) {
1888 setUsesTOCBasePtr(DAG.getMachineFunction());
1889}
1890
Hal Finkelcf599212015-02-25 21:36:59 +00001891static SDValue getTOCEntry(SelectionDAG &DAG, SDLoc dl, bool Is64Bit,
1892 SDValue GA) {
1893 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
1894 SDValue Reg = Is64Bit ? DAG.getRegister(PPC::X2, VT) :
1895 DAG.getNode(PPCISD::GlobalBaseReg, dl, VT);
1896
1897 SDValue Ops[] = { GA, Reg };
1898 return DAG.getMemIntrinsicNode(PPCISD::TOC_ENTRY, dl,
1899 DAG.getVTList(VT, MVT::Other), Ops, VT,
1900 MachinePointerInfo::getGOT(), 0, false, true,
1901 false, 0);
1902}
1903
Scott Michelcf0da6c2009-02-17 22:15:04 +00001904SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001905 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001906 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001907 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001908 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001909
Roman Divackyace47072012-08-24 16:26:02 +00001910 // 64-bit SVR4 ABI code is always position-independent.
1911 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001912 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00001913 setUsesTOCBasePtr(DAG);
Roman Divackyace47072012-08-24 16:26:02 +00001914 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Hal Finkelcf599212015-02-25 21:36:59 +00001915 return getTOCEntry(DAG, SDLoc(CP), true, GA);
Roman Divackyace47072012-08-24 16:26:02 +00001916 }
1917
Chris Lattneredb9d842010-11-15 02:46:57 +00001918 unsigned MOHiFlag, MOLoFlag;
Eric Christophercccae792015-01-30 22:02:31 +00001919 bool isPIC =
1920 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001921
1922 if (isPIC && Subtarget.isSVR4ABI()) {
1923 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(),
1924 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00001925 return getTOCEntry(DAG, SDLoc(CP), false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001926 }
1927
Chris Lattneredb9d842010-11-15 02:46:57 +00001928 SDValue CPIHi =
1929 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1930 SDValue CPILo =
1931 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1932 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001933}
1934
Dan Gohman21cea8a2010-04-17 15:26:15 +00001935SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001936 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001937 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001938
Roman Divackyace47072012-08-24 16:26:02 +00001939 // 64-bit SVR4 ABI code is always position-independent.
1940 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001941 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00001942 setUsesTOCBasePtr(DAG);
Roman Divackyace47072012-08-24 16:26:02 +00001943 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Hal Finkelcf599212015-02-25 21:36:59 +00001944 return getTOCEntry(DAG, SDLoc(JT), true, GA);
Roman Divackyace47072012-08-24 16:26:02 +00001945 }
1946
Chris Lattneredb9d842010-11-15 02:46:57 +00001947 unsigned MOHiFlag, MOLoFlag;
Eric Christophercccae792015-01-30 22:02:31 +00001948 bool isPIC =
1949 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001950
1951 if (isPIC && Subtarget.isSVR4ABI()) {
1952 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1953 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00001954 return getTOCEntry(DAG, SDLoc(GA), false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001955 }
1956
Chris Lattneredb9d842010-11-15 02:46:57 +00001957 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1958 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1959 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001960}
1961
Dan Gohman21cea8a2010-04-17 15:26:15 +00001962SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1963 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001964 EVT PtrVT = Op.getValueType();
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001965 BlockAddressSDNode *BASDN = cast<BlockAddressSDNode>(Op);
1966 const BlockAddress *BA = BASDN->getBlockAddress();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001967
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001968 // 64-bit SVR4 ABI code is always position-independent.
1969 // The actual BlockAddress is stored in the TOC.
1970 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00001971 setUsesTOCBasePtr(DAG);
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001972 SDValue GA = DAG.getTargetBlockAddress(BA, PtrVT, BASDN->getOffset());
Hal Finkelcf599212015-02-25 21:36:59 +00001973 return getTOCEntry(DAG, SDLoc(BASDN), true, GA);
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001974 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001975
Chris Lattneredb9d842010-11-15 02:46:57 +00001976 unsigned MOHiFlag, MOLoFlag;
Eric Christophercccae792015-01-30 22:02:31 +00001977 bool isPIC =
1978 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001979 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1980 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001981 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1982}
1983
Roman Divackye3f15c982012-06-04 17:36:38 +00001984SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1985 SelectionDAG &DAG) const {
1986
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001987 // FIXME: TLS addresses currently use medium model code sequences,
1988 // which is the most useful form. Eventually support for small and
1989 // large models could be added if users need it, at the cost of
1990 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001991 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001992 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001993 const GlobalValue *GV = GA->getGlobal();
1994 EVT PtrVT = getPointerTy();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001995 bool is64bit = Subtarget.isPPC64();
Justin Hibbitsa88b6052014-11-12 15:16:30 +00001996 const Module *M = DAG.getMachineFunction().getFunction()->getParent();
1997 PICLevel::Level picLevel = M->getPICLevel();
Roman Divackye3f15c982012-06-04 17:36:38 +00001998
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001999 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00002000
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002001 if (Model == TLSModel::LocalExec) {
2002 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002003 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002004 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002005 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002006 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
2007 is64bit ? MVT::i64 : MVT::i32);
2008 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
2009 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
2010 }
Roman Divackye3f15c982012-06-04 17:36:38 +00002011
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002012 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00002013 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00002014 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2015 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00002016 SDValue GOTPtr;
2017 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00002018 setUsesTOCBasePtr(DAG);
Roman Divacky32143e22013-12-20 18:08:54 +00002019 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
2020 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
2021 PtrVT, GOTReg, TGA);
2022 } else
2023 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00002024 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00002025 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00002026 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002027 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002028
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002029 if (Model == TLSModel::GeneralDynamic) {
Bill Schmidt82f1c772015-02-10 19:09:05 +00002030 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002031 SDValue GOTPtr;
2032 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00002033 setUsesTOCBasePtr(DAG);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002034 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
2035 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
2036 GOTReg, TGA);
2037 } else {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002038 if (picLevel == PICLevel::Small)
2039 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
2040 else
2041 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002042 }
Bill Schmidt82f1c772015-02-10 19:09:05 +00002043 return DAG.getNode(PPCISD::ADDI_TLSGD_L_ADDR, dl, PtrVT,
2044 GOTPtr, TGA, TGA);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002045 }
2046
Bill Schmidt24b8dd62012-12-12 19:29:35 +00002047 if (Model == TLSModel::LocalDynamic) {
Bill Schmidt82f1c772015-02-10 19:09:05 +00002048 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002049 SDValue GOTPtr;
2050 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00002051 setUsesTOCBasePtr(DAG);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002052 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
2053 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
2054 GOTReg, TGA);
2055 } else {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002056 if (picLevel == PICLevel::Small)
2057 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
2058 else
2059 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002060 }
Bill Schmidt82f1c772015-02-10 19:09:05 +00002061 SDValue TLSAddr = DAG.getNode(PPCISD::ADDI_TLSLD_L_ADDR, dl,
2062 PtrVT, GOTPtr, TGA, TGA);
2063 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl,
2064 PtrVT, TLSAddr, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00002065 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
2066 }
2067
2068 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00002069}
2070
Chris Lattneredb9d842010-11-15 02:46:57 +00002071SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
2072 SelectionDAG &DAG) const {
2073 EVT PtrVT = Op.getValueType();
2074 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002075 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00002076 const GlobalValue *GV = GSDN->getGlobal();
2077
Chris Lattneredb9d842010-11-15 02:46:57 +00002078 // 64-bit SVR4 ABI code is always position-independent.
2079 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002080 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002081 setUsesTOCBasePtr(DAG);
Chris Lattneredb9d842010-11-15 02:46:57 +00002082 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
Hal Finkelcf599212015-02-25 21:36:59 +00002083 return getTOCEntry(DAG, DL, true, GA);
Chris Lattneredb9d842010-11-15 02:46:57 +00002084 }
2085
Chris Lattnerdd6df842010-11-15 03:13:19 +00002086 unsigned MOHiFlag, MOLoFlag;
Eric Christophercccae792015-01-30 22:02:31 +00002087 bool isPIC =
2088 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00002089
Hal Finkel3ee2af72014-07-18 23:29:49 +00002090 if (isPIC && Subtarget.isSVR4ABI()) {
2091 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT,
2092 GSDN->getOffset(),
2093 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00002094 return getTOCEntry(DAG, DL, false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002095 }
2096
Chris Lattnerdd6df842010-11-15 03:13:19 +00002097 SDValue GAHi =
2098 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
2099 SDValue GALo =
2100 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00002101
Chris Lattnerdd6df842010-11-15 03:13:19 +00002102 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00002103
Chris Lattnerdd6df842010-11-15 03:13:19 +00002104 // If the global reference is actually to a non-lazy-pointer, we have to do an
2105 // extra load to get the address of the global.
2106 if (MOHiFlag & PPCII::MO_NLP_FLAG)
2107 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002108 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00002109 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00002110}
2111
Dan Gohman21cea8a2010-04-17 15:26:15 +00002112SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00002113 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002114 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002115
Hal Finkel777c9dd2014-03-29 16:04:40 +00002116 if (Op.getValueType() == MVT::v2i64) {
2117 // When the operands themselves are v2i64 values, we need to do something
2118 // special because VSX has no underlying comparison operations for these.
2119 if (Op.getOperand(0).getValueType() == MVT::v2i64) {
2120 // Equality can be handled by casting to the legal type for Altivec
2121 // comparisons, everything else needs to be expanded.
2122 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
2123 return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
2124 DAG.getSetCC(dl, MVT::v4i32,
2125 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)),
2126 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)),
2127 CC));
2128 }
2129
2130 return SDValue();
2131 }
2132
2133 // We handle most of these in the usual way.
2134 return Op;
2135 }
2136
Chris Lattner4211ca92006-04-14 06:01:58 +00002137 // If we're comparing for equality to zero, expose the fact that this is
2138 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
2139 // fold the new nodes.
2140 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2141 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002142 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002143 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00002144 if (VT.bitsLT(MVT::i32)) {
2145 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002146 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002147 }
Duncan Sands13237ac2008-06-06 12:08:01 +00002148 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002149 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
2150 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002151 DAG.getConstant(Log2b, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00002152 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00002153 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002154 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00002155 // optimized. FIXME: revisit this when we can custom lower all setcc
2156 // optimizations.
2157 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002158 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00002159 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002160
Chris Lattner4211ca92006-04-14 06:01:58 +00002161 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00002162 // by xor'ing the rhs with the lhs, which is faster than setting a
2163 // condition register, reading it back out, and masking the correct bit. The
2164 // normal approach here uses sub to do this instead of xor. Using xor exposes
2165 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002166 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002167 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002168 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002169 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00002170 Op.getOperand(1));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002171 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, dl, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00002172 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002173 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00002174}
2175
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002176SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002177 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00002178 SDNode *Node = Op.getNode();
2179 EVT VT = Node->getValueType(0);
2180 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2181 SDValue InChain = Node->getOperand(0);
2182 SDValue VAListPtr = Node->getOperand(1);
2183 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002184 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002185
Roman Divacky4394e682011-06-28 15:30:42 +00002186 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
2187
2188 // gpr_index
2189 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
2190 VAListPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00002191 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002192 InChain = GprIndex.getValue(1);
2193
2194 if (VT == MVT::i64) {
2195 // Check if GprIndex is even
2196 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002197 DAG.getConstant(1, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002198 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002199 DAG.getConstant(0, dl, MVT::i32), ISD::SETNE);
Roman Divacky4394e682011-06-28 15:30:42 +00002200 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002201 DAG.getConstant(1, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002202 // Align GprIndex to be even if it isn't
2203 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
2204 GprIndex);
2205 }
2206
2207 // fpr index is 1 byte after gpr
2208 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002209 DAG.getConstant(1, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002210
2211 // fpr
2212 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
2213 FprPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00002214 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002215 InChain = FprIndex.getValue(1);
2216
2217 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002218 DAG.getConstant(8, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002219
2220 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002221 DAG.getConstant(4, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002222
2223 // areas
2224 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002225 MachinePointerInfo(), false, false,
2226 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002227 InChain = OverflowArea.getValue(1);
2228
2229 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002230 MachinePointerInfo(), false, false,
2231 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002232 InChain = RegSaveArea.getValue(1);
2233
2234 // select overflow_area if index > 8
2235 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002236 DAG.getConstant(8, dl, MVT::i32), ISD::SETLT);
Roman Divacky4394e682011-06-28 15:30:42 +00002237
Roman Divacky4394e682011-06-28 15:30:42 +00002238 // adjustment constant gpr_index * 4/8
2239 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
2240 VT.isInteger() ? GprIndex : FprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002241 DAG.getConstant(VT.isInteger() ? 4 : 8, dl,
Roman Divacky4394e682011-06-28 15:30:42 +00002242 MVT::i32));
2243
2244 // OurReg = RegSaveArea + RegConstant
2245 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
2246 RegConstant);
2247
2248 // Floating types are 32 bytes into RegSaveArea
2249 if (VT.isFloatingPoint())
2250 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002251 DAG.getConstant(32, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002252
2253 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
2254 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
2255 VT.isInteger() ? GprIndex : FprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002256 DAG.getConstant(VT == MVT::i64 ? 2 : 1, dl,
Roman Divacky4394e682011-06-28 15:30:42 +00002257 MVT::i32));
2258
2259 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
2260 VT.isInteger() ? VAListPtr : FprPtr,
2261 MachinePointerInfo(SV),
2262 MVT::i8, false, false, 0);
2263
2264 // determine if we should load from reg_save_area or overflow_area
2265 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
2266
2267 // increase overflow_area by 4/8 if gpr/fpr > 8
2268 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
2269 DAG.getConstant(VT.isInteger() ? 4 : 8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002270 dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002271
2272 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
2273 OverflowAreaPlusN);
2274
2275 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
2276 OverflowAreaPtr,
2277 MachinePointerInfo(),
2278 MVT::i32, false, false, 0);
2279
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00002280 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002281 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002282}
2283
Roman Divackyc3825df2013-07-25 21:36:47 +00002284SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
2285 const PPCSubtarget &Subtarget) const {
2286 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
2287
2288 // We have to copy the entire va_list struct:
2289 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
2290 return DAG.getMemcpy(Op.getOperand(0), Op,
2291 Op.getOperand(1), Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002292 DAG.getConstant(12, SDLoc(Op), MVT::i32), 8, false, true,
2293 false, MachinePointerInfo(), MachinePointerInfo());
Roman Divackyc3825df2013-07-25 21:36:47 +00002294}
2295
Duncan Sandsa0984362011-09-06 13:37:06 +00002296SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
2297 SelectionDAG &DAG) const {
2298 return Op.getOperand(0);
2299}
2300
2301SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
2302 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00002303 SDValue Chain = Op.getOperand(0);
2304 SDValue Trmp = Op.getOperand(1); // trampoline
2305 SDValue FPtr = Op.getOperand(2); // nested function
2306 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00002307 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00002308
Owen Anderson53aa7a92009-08-10 22:56:29 +00002309 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002310 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00002311 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00002312 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00002313 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00002314
Scott Michelcf0da6c2009-02-17 22:15:04 +00002315 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00002316 TargetLowering::ArgListEntry Entry;
2317
2318 Entry.Ty = IntPtrTy;
2319 Entry.Node = Trmp; Args.push_back(Entry);
2320
2321 // TrampSize == (isPPC64 ? 48 : 40);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002322 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002323 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00002324 Args.push_back(Entry);
2325
2326 Entry.Node = FPtr; Args.push_back(Entry);
2327 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002328
Bill Wendling95e1af22008-09-17 00:30:57 +00002329 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002330 TargetLowering::CallLoweringInfo CLI(DAG);
2331 CLI.setDebugLoc(dl).setChain(Chain)
2332 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002333 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
2334 std::move(Args), 0);
Bill Wendling95e1af22008-09-17 00:30:57 +00002335
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002336 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Duncan Sandsa0984362011-09-06 13:37:06 +00002337 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00002338}
2339
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002340SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002341 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00002342 MachineFunction &MF = DAG.getMachineFunction();
2343 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2344
Andrew Trickef9de2a2013-05-25 02:42:55 +00002345 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002346
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002347 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002348 // vastart just stores the address of the VarArgsFrameIndex slot into the
2349 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002350 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002351 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002352 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00002353 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2354 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00002355 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002356 }
2357
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002358 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002359 // We suppose the given va_list is already allocated.
2360 //
2361 // typedef struct {
2362 // char gpr; /* index into the array of 8 GPRs
2363 // * stored in the register save area
2364 // * gpr=0 corresponds to r3,
2365 // * gpr=1 to r4, etc.
2366 // */
2367 // char fpr; /* index into the array of 8 FPRs
2368 // * stored in the register save area
2369 // * fpr=0 corresponds to f1,
2370 // * fpr=1 to f2, etc.
2371 // */
2372 // char *overflow_arg_area;
2373 // /* location on stack that holds
2374 // * the next overflow argument
2375 // */
2376 // char *reg_save_area;
2377 // /* where r3:r10 and f1:f8 (if saved)
2378 // * are stored
2379 // */
2380 // } va_list[1];
2381
2382
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002383 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), dl, MVT::i32);
2384 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), dl, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002385
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002386
Owen Anderson53aa7a92009-08-10 22:56:29 +00002387 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002388
Dan Gohman31ae5862010-04-17 14:41:14 +00002389 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
2390 PtrVT);
2391 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2392 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002393
Duncan Sands13237ac2008-06-06 12:08:01 +00002394 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002395 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, dl, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002396
Duncan Sands13237ac2008-06-06 12:08:01 +00002397 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002398 SDValue ConstStackOffset = DAG.getConstant(StackOffset, dl, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002399
2400 uint64_t FPROffset = 1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002401 SDValue ConstFPROffset = DAG.getConstant(FPROffset, dl, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002402
Dan Gohman2d489b52008-02-06 22:27:42 +00002403 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002404
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002405 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002406 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00002407 Op.getOperand(1),
2408 MachinePointerInfo(SV),
2409 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002410 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002411 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002412 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002413
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002414 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002415 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00002416 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
2417 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00002418 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002419 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002420 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002421
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002422 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002423 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00002424 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
2425 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002426 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002427 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002428 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002429
2430 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00002431 return DAG.getStore(thirdStore, dl, FR, nextPtr,
2432 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002433 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002434
Chris Lattner4211ca92006-04-14 06:01:58 +00002435}
2436
Chris Lattner4f2e4e02007-03-06 00:59:59 +00002437#include "PPCGenCallingConv.inc"
2438
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002439// Function whose sole purpose is to kill compiler warnings
2440// stemming from unused functions included from PPCGenCallingConv.inc.
2441CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00002442 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002443}
2444
Bill Schmidt230b4512013-06-12 16:39:22 +00002445bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
2446 CCValAssign::LocInfo &LocInfo,
2447 ISD::ArgFlagsTy &ArgFlags,
2448 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002449 return true;
2450}
2451
Bill Schmidt230b4512013-06-12 16:39:22 +00002452bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
2453 MVT &LocVT,
2454 CCValAssign::LocInfo &LocInfo,
2455 ISD::ArgFlagsTy &ArgFlags,
2456 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002457 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002458 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2459 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2460 };
2461 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002462
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002463 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002464
2465 // Skip one register if the first unallocated register has an even register
2466 // number and there are still argument registers available which have not been
2467 // allocated yet. RegNum is actually an index into ArgRegs, which means we
2468 // need to skip a register if RegNum is odd.
2469 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
2470 State.AllocateReg(ArgRegs[RegNum]);
2471 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002472
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002473 // Always return false here, as this function only makes sure that the first
2474 // unallocated register has an odd register number and does not actually
2475 // allocate a register for the current argument.
2476 return false;
2477}
2478
Bill Schmidt230b4512013-06-12 16:39:22 +00002479bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
2480 MVT &LocVT,
2481 CCValAssign::LocInfo &LocInfo,
2482 ISD::ArgFlagsTy &ArgFlags,
2483 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002484 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002485 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2486 PPC::F8
2487 };
2488
2489 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002490
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002491 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002492
2493 // If there is only one Floating-point register left we need to put both f64
2494 // values of a split ppc_fp128 value on the stack.
2495 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
2496 State.AllocateReg(ArgRegs[RegNum]);
2497 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002498
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002499 // Always return false here, as this function only makes sure that the two f64
2500 // values a ppc_fp128 value is split into are both passed in registers or both
2501 // passed on the stack and does not actually allocate a register for the
2502 // current argument.
2503 return false;
2504}
2505
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002506/// FPR - The set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002507/// on Darwin.
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002508static const MCPhysReg FPR[] = {PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5,
2509 PPC::F6, PPC::F7, PPC::F8, PPC::F9, PPC::F10,
2510 PPC::F11, PPC::F12, PPC::F13};
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002511
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002512/// QFPR - The set of QPX registers that should be allocated for arguments.
2513static const MCPhysReg QFPR[] = {
2514 PPC::QF1, PPC::QF2, PPC::QF3, PPC::QF4, PPC::QF5, PPC::QF6, PPC::QF7,
2515 PPC::QF8, PPC::QF9, PPC::QF10, PPC::QF11, PPC::QF12, PPC::QF13};
Hal Finkelc93a9a22015-02-25 01:06:45 +00002516
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002517/// CalculateStackSlotSize - Calculates the size reserved for this argument on
2518/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002519static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002520 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00002521 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002522 if (Flags.isByVal())
2523 ArgSize = Flags.getByValSize();
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002524
2525 // Round up to multiples of the pointer size, except for array members,
2526 // which are always packed.
2527 if (!Flags.isInConsecutiveRegs())
2528 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002529
2530 return ArgSize;
2531}
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002532
2533/// CalculateStackSlotAlignment - Calculates the alignment of this argument
2534/// on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002535static unsigned CalculateStackSlotAlignment(EVT ArgVT, EVT OrigVT,
2536 ISD::ArgFlagsTy Flags,
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002537 unsigned PtrByteSize) {
2538 unsigned Align = PtrByteSize;
2539
2540 // Altivec parameters are padded to a 16 byte boundary.
2541 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2542 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
Kit Bartond4eb73c2015-05-05 16:10:44 +00002543 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 ||
2544 ArgVT == MVT::v1i128)
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002545 Align = 16;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002546 // QPX vector types stored in double-precision are padded to a 32 byte
2547 // boundary.
2548 else if (ArgVT == MVT::v4f64 || ArgVT == MVT::v4i1)
2549 Align = 32;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002550
2551 // ByVal parameters are aligned as requested.
2552 if (Flags.isByVal()) {
2553 unsigned BVAlign = Flags.getByValAlign();
2554 if (BVAlign > PtrByteSize) {
2555 if (BVAlign % PtrByteSize != 0)
2556 llvm_unreachable(
2557 "ByVal alignment is not a multiple of the pointer size");
2558
2559 Align = BVAlign;
2560 }
2561 }
2562
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002563 // Array members are always packed to their original alignment.
2564 if (Flags.isInConsecutiveRegs()) {
2565 // If the array member was split into multiple registers, the first
2566 // needs to be aligned to the size of the full type. (Except for
2567 // ppcf128, which is only aligned as its f64 components.)
2568 if (Flags.isSplit() && OrigVT != MVT::ppcf128)
2569 Align = OrigVT.getStoreSize();
2570 else
2571 Align = ArgVT.getStoreSize();
2572 }
2573
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002574 return Align;
2575}
2576
Ulrich Weigand8658f172014-07-20 23:43:15 +00002577/// CalculateStackSlotUsed - Return whether this argument will use its
2578/// stack slot (instead of being passed in registers). ArgOffset,
2579/// AvailableFPRs, and AvailableVRs must hold the current argument
2580/// position, and will be updated to account for this argument.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002581static bool CalculateStackSlotUsed(EVT ArgVT, EVT OrigVT,
2582 ISD::ArgFlagsTy Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00002583 unsigned PtrByteSize,
2584 unsigned LinkageSize,
2585 unsigned ParamAreaSize,
2586 unsigned &ArgOffset,
2587 unsigned &AvailableFPRs,
Hal Finkelc93a9a22015-02-25 01:06:45 +00002588 unsigned &AvailableVRs, bool HasQPX) {
Ulrich Weigand8658f172014-07-20 23:43:15 +00002589 bool UseMemory = false;
2590
2591 // Respect alignment of argument on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002592 unsigned Align =
2593 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigand8658f172014-07-20 23:43:15 +00002594 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
2595 // If there's no space left in the argument save area, we must
2596 // use memory (this check also catches zero-sized arguments).
2597 if (ArgOffset >= LinkageSize + ParamAreaSize)
2598 UseMemory = true;
2599
2600 // Allocate argument on the stack.
2601 ArgOffset += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002602 if (Flags.isInConsecutiveRegsLast())
2603 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand8658f172014-07-20 23:43:15 +00002604 // If we overran the argument save area, we must use memory
2605 // (this check catches arguments passed partially in memory)
2606 if (ArgOffset > LinkageSize + ParamAreaSize)
2607 UseMemory = true;
2608
2609 // However, if the argument is actually passed in an FPR or a VR,
2610 // we don't use memory after all.
2611 if (!Flags.isByVal()) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00002612 if (ArgVT == MVT::f32 || ArgVT == MVT::f64 ||
2613 // QPX registers overlap with the scalar FP registers.
2614 (HasQPX && (ArgVT == MVT::v4f32 ||
2615 ArgVT == MVT::v4f64 ||
2616 ArgVT == MVT::v4i1)))
Ulrich Weigand8658f172014-07-20 23:43:15 +00002617 if (AvailableFPRs > 0) {
2618 --AvailableFPRs;
2619 return false;
2620 }
2621 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2622 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
Kit Bartond4eb73c2015-05-05 16:10:44 +00002623 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 ||
2624 ArgVT == MVT::v1i128)
Ulrich Weigand8658f172014-07-20 23:43:15 +00002625 if (AvailableVRs > 0) {
2626 --AvailableVRs;
2627 return false;
2628 }
2629 }
2630
2631 return UseMemory;
2632}
2633
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002634/// EnsureStackAlignment - Round stack frame size up from NumBytes to
2635/// ensure minimum alignment required for target.
Eric Christophercccae792015-01-30 22:02:31 +00002636static unsigned EnsureStackAlignment(const PPCFrameLowering *Lowering,
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002637 unsigned NumBytes) {
Eric Christophercccae792015-01-30 22:02:31 +00002638 unsigned TargetAlign = Lowering->getStackAlignment();
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002639 unsigned AlignMask = TargetAlign - 1;
2640 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2641 return NumBytes;
2642}
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002643
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002644SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002645PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002646 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002647 const SmallVectorImpl<ISD::InputArg>
2648 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002649 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002650 SmallVectorImpl<SDValue> &InVals)
2651 const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002652 if (Subtarget.isSVR4ABI()) {
2653 if (Subtarget.isPPC64())
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002654 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
2655 dl, DAG, InVals);
2656 else
2657 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2658 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002659 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002660 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2661 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002662 }
2663}
2664
2665SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002666PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002667 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002668 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002669 const SmallVectorImpl<ISD::InputArg>
2670 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002671 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002672 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002673
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002674 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002675 // +-----------------------------------+
2676 // +--> | Back chain |
2677 // | +-----------------------------------+
2678 // | | Floating-point register save area |
2679 // | +-----------------------------------+
2680 // | | General register save area |
2681 // | +-----------------------------------+
2682 // | | CR save word |
2683 // | +-----------------------------------+
2684 // | | VRSAVE save word |
2685 // | +-----------------------------------+
2686 // | | Alignment padding |
2687 // | +-----------------------------------+
2688 // | | Vector register save area |
2689 // | +-----------------------------------+
2690 // | | Local variable space |
2691 // | +-----------------------------------+
2692 // | | Parameter list area |
2693 // | +-----------------------------------+
2694 // | | LR save word |
2695 // | +-----------------------------------+
2696 // SP--> +--- | Back chain |
2697 // +-----------------------------------+
2698 //
2699 // Specifications:
2700 // System V Application Binary Interface PowerPC Processor Supplement
2701 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002702
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002703 MachineFunction &MF = DAG.getMachineFunction();
2704 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002705 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002706
Owen Anderson53aa7a92009-08-10 22:56:29 +00002707 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002708 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002709 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2710 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002711 unsigned PtrByteSize = 4;
2712
2713 // Assign locations to all of the incoming arguments.
2714 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002715 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
2716 *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002717
2718 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00002719 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002720 CCInfo.AllocateStack(LinkageSize, PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002721
Bill Schmidtef17c142013-02-06 17:33:58 +00002722 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002723
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002724 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2725 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002726
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002727 // Arguments stored in registers.
2728 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002729 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002730 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002731
Owen Anderson9f944592009-08-11 20:47:22 +00002732 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002733 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002734 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002735 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002736 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002737 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002738 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002739 case MVT::f32:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00002740 if (Subtarget.hasP8Vector())
2741 RC = &PPC::VSSRCRegClass;
2742 else
2743 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002744 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002745 case MVT::f64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002746 if (Subtarget.hasVSX())
Hal Finkel19be5062014-03-29 05:29:01 +00002747 RC = &PPC::VSFRCRegClass;
2748 else
2749 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002750 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002751 case MVT::v16i8:
2752 case MVT::v8i16:
2753 case MVT::v4i32:
Hal Finkel7811c612014-03-28 19:58:11 +00002754 RC = &PPC::VRRCRegClass;
2755 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002756 case MVT::v4f32:
2757 RC = Subtarget.hasQPX() ? &PPC::QSRCRegClass : &PPC::VRRCRegClass;
2758 break;
Hal Finkel27774d92014-03-13 07:58:58 +00002759 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002760 case MVT::v2i64:
Hal Finkel7811c612014-03-28 19:58:11 +00002761 RC = &PPC::VSHRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002762 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002763 case MVT::v4f64:
2764 RC = &PPC::QFRCRegClass;
2765 break;
2766 case MVT::v4i1:
2767 RC = &PPC::QBRCRegClass;
2768 break;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002769 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002770
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002771 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002772 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002773 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2774 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2775
2776 if (ValVT == MVT::i1)
2777 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002778
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002779 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002780 } else {
2781 // Argument stored in memory.
2782 assert(VA.isMemLoc());
2783
Hal Finkel940ab932014-02-28 00:27:01 +00002784 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002785 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002786 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002787
2788 // Create load nodes to retrieve arguments from the stack.
2789 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002790 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2791 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002792 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002793 }
2794 }
2795
2796 // Assign locations to all of the incoming aggregate by value arguments.
2797 // Aggregates passed by value are stored in the local variable space of the
2798 // caller's stack frame, right above the parameter list area.
2799 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002800 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Eric Christopherb5217502014-08-06 18:45:26 +00002801 ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002802
2803 // Reserve stack space for the allocations in CCInfo.
2804 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2805
Bill Schmidtef17c142013-02-06 17:33:58 +00002806 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002807
2808 // Area that is at least reserved in the caller of this function.
2809 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002810 MinReservedArea = std::max(MinReservedArea, LinkageSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00002811
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002812 // Set the size that is at least reserved in caller of this function. Tail
2813 // call optimized function's reserved stack space needs to be aligned so that
2814 // taking the difference between two stack areas will result in an aligned
2815 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00002816 MinReservedArea =
2817 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002818 FuncInfo->setMinReservedArea(MinReservedArea);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002819
2820 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002821
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002822 // If the function takes variable number of arguments, make a frame index for
2823 // the start of the first vararg value... for expansion of llvm.va_start.
2824 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +00002825 static const MCPhysReg GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002826 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2827 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2828 };
2829 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2830
Craig Topper840beec2014-04-04 05:16:06 +00002831 static const MCPhysReg FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002832 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2833 PPC::F8
2834 };
Joerg Sonnenbergereb8655a2014-08-08 16:46:10 +00002835 unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2836 if (DisablePPCFloatInVariadic)
2837 NumFPArgRegs = 0;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002838
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002839 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs));
2840 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002841
2842 // Make room for NumGPArgRegs and NumFPArgRegs.
2843 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Craig Topper7ff15922014-09-10 04:51:36 +00002844 NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002845
Dan Gohman31ae5862010-04-17 14:41:14 +00002846 FuncInfo->setVarArgsStackOffset(
2847 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002848 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002849
Dan Gohman31ae5862010-04-17 14:41:14 +00002850 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2851 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002852
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002853 // The fixed integer arguments of a variadic function are stored to the
2854 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2855 // the result of va_next.
2856 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2857 // Get an existing live-in vreg, or add a new one.
2858 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2859 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002860 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002861
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002862 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002863 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2864 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002865 MemOps.push_back(Store);
2866 // Increment the address by four for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002867 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002868 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2869 }
2870
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002871 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2872 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002873 // The double arguments are stored to the VarArgsFrameIndex
2874 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002875 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2876 // Get an existing live-in vreg, or add a new one.
2877 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2878 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002879 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002880
Owen Anderson9f944592009-08-11 20:47:22 +00002881 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002882 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2883 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002884 MemOps.push_back(Store);
2885 // Increment the address by eight for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002886 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8, dl,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002887 PtrVT);
2888 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2889 }
2890 }
2891
2892 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002893 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002894
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002895 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002896}
2897
Bill Schmidt57d6de52012-10-23 15:51:16 +00002898// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2899// value to MVT::i64 and then truncate to the correct register size.
2900SDValue
2901PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2902 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002903 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002904 if (Flags.isSExt())
2905 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2906 DAG.getValueType(ObjectVT));
2907 else if (Flags.isZExt())
2908 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2909 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002910
Hal Finkel940ab932014-02-28 00:27:01 +00002911 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002912}
2913
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002914SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002915PPCTargetLowering::LowerFormalArguments_64SVR4(
2916 SDValue Chain,
2917 CallingConv::ID CallConv, bool isVarArg,
2918 const SmallVectorImpl<ISD::InputArg>
2919 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002920 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002921 SmallVectorImpl<SDValue> &InVals) const {
2922 // TODO: add description of PPC stack frame format, or at least some docs.
2923 //
Ulrich Weigand8658f172014-07-20 23:43:15 +00002924 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00002925 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002926 MachineFunction &MF = DAG.getMachineFunction();
2927 MachineFrameInfo *MFI = MF.getFrameInfo();
2928 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2929
Hal Finkelf81b6dd2015-01-18 12:08:47 +00002930 assert(!(CallConv == CallingConv::Fast && isVarArg) &&
2931 "fastcc not supported on varargs functions");
2932
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002933 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2934 // Potential tail calls could cause overwriting of argument stack slots.
2935 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2936 (CallConv == CallingConv::Fast));
2937 unsigned PtrByteSize = 8;
Eric Christophera4ae2132015-02-13 22:22:57 +00002938 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002939
Craig Topper840beec2014-04-04 05:16:06 +00002940 static const MCPhysReg GPR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002941 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2942 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2943 };
Craig Topper840beec2014-04-04 05:16:06 +00002944 static const MCPhysReg VR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002945 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2946 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2947 };
Craig Topper840beec2014-04-04 05:16:06 +00002948 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00002949 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
2950 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
2951 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002952
2953 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2954 const unsigned Num_FPR_Regs = 13;
2955 const unsigned Num_VR_Regs = array_lengthof(VR);
Hal Finkelc93a9a22015-02-25 01:06:45 +00002956 const unsigned Num_QFPR_Regs = Num_FPR_Regs;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002957
Ulrich Weigand8658f172014-07-20 23:43:15 +00002958 // Do a first pass over the arguments to determine whether the ABI
2959 // guarantees that our caller has allocated the parameter save area
2960 // on its stack frame. In the ELFv1 ABI, this is always the case;
2961 // in the ELFv2 ABI, it is true if this is a vararg function or if
2962 // any parameter is located in a stack slot.
2963
2964 bool HasParameterArea = !isELFv2ABI || isVarArg;
2965 unsigned ParamAreaSize = Num_GPR_Regs * PtrByteSize;
2966 unsigned NumBytes = LinkageSize;
2967 unsigned AvailableFPRs = Num_FPR_Regs;
2968 unsigned AvailableVRs = Num_VR_Regs;
2969 for (unsigned i = 0, e = Ins.size(); i != e; ++i)
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002970 if (CalculateStackSlotUsed(Ins[i].VT, Ins[i].ArgVT, Ins[i].Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00002971 PtrByteSize, LinkageSize, ParamAreaSize,
Hal Finkelc93a9a22015-02-25 01:06:45 +00002972 NumBytes, AvailableFPRs, AvailableVRs,
2973 Subtarget.hasQPX()))
Ulrich Weigand8658f172014-07-20 23:43:15 +00002974 HasParameterArea = true;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002975
2976 // Add DAG nodes to load the arguments or copy them out of registers. On
2977 // entry to a function on PPC, the arguments start after the linkage area,
2978 // although the first ones are often in registers.
2979
Ulrich Weigand8658f172014-07-20 23:43:15 +00002980 unsigned ArgOffset = LinkageSize;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00002981 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002982 unsigned &QFPR_idx = FPR_idx;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002983 SmallVector<SDValue, 8> MemOps;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002984 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002985 unsigned CurArgIdx = 0;
2986 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002987 SDValue ArgVal;
2988 bool needsLoad = false;
2989 EVT ObjectVT = Ins[ArgNo].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002990 EVT OrigVT = Ins[ArgNo].ArgVT;
Hal Finkel940ab932014-02-28 00:27:01 +00002991 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002992 unsigned ArgSize = ObjSize;
2993 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Andrew Trick05938a52015-02-16 18:10:47 +00002994 if (Ins[ArgNo].isOrigArg()) {
2995 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx);
2996 CurArgIdx = Ins[ArgNo].getOrigArgIndex();
2997 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00002998 // We re-align the argument offset for each argument, except when using the
2999 // fast calling convention, when we need to make sure we do that only when
3000 // we'll actually use a stack slot.
3001 unsigned CurArgOffset, Align;
3002 auto ComputeArgOffset = [&]() {
3003 /* Respect alignment of argument on the stack. */
3004 Align = CalculateStackSlotAlignment(ObjectVT, OrigVT, Flags, PtrByteSize);
3005 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
3006 CurArgOffset = ArgOffset;
3007 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003008
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003009 if (CallConv != CallingConv::Fast) {
3010 ComputeArgOffset();
3011
3012 /* Compute GPR index associated with argument offset. */
3013 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
3014 GPR_idx = std::min(GPR_idx, Num_GPR_Regs);
3015 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003016
3017 // FIXME the codegen can be much improved in some cases.
3018 // We do not have to keep everything in memory.
3019 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003020 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit");
3021
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003022 if (CallConv == CallingConv::Fast)
3023 ComputeArgOffset();
3024
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003025 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
3026 ObjSize = Flags.getByValSize();
3027 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00003028 // Empty aggregate parameters do not take up registers. Examples:
3029 // struct { } a;
3030 // union { } b;
3031 // int c[0];
3032 // etc. However, we have to provide a place-holder in InVals, so
3033 // pretend we have an 8-byte item at the current address for that
3034 // purpose.
3035 if (!ObjSize) {
3036 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
3037 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
3038 InVals.push_back(FIN);
3039 continue;
3040 }
Hal Finkel262a2242013-09-12 23:20:06 +00003041
Ulrich Weigand24195972014-07-20 22:36:52 +00003042 // Create a stack object covering all stack doublewords occupied
Ulrich Weigand8658f172014-07-20 23:43:15 +00003043 // by the argument. If the argument is (fully or partially) on
3044 // the stack, or if the argument is fully in registers but the
3045 // caller has allocated the parameter save anyway, we can refer
3046 // directly to the caller's stack frame. Otherwise, create a
3047 // local copy in our own frame.
3048 int FI;
3049 if (HasParameterArea ||
3050 ArgSize + ArgOffset > LinkageSize + Num_GPR_Regs * PtrByteSize)
Hal Finkel41a55ad2014-08-16 00:17:05 +00003051 FI = MFI->CreateFixedObject(ArgSize, ArgOffset, false, true);
Ulrich Weigand8658f172014-07-20 23:43:15 +00003052 else
3053 FI = MFI->CreateStackObject(ArgSize, Align, false);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003054 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003055
Ulrich Weigand24195972014-07-20 22:36:52 +00003056 // Handle aggregates smaller than 8 bytes.
3057 if (ObjSize < PtrByteSize) {
3058 // The value of the object is its address, which differs from the
3059 // address of the enclosing doubleword on big-endian systems.
3060 SDValue Arg = FIN;
3061 if (!isLittleEndian) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003062 SDValue ArgOff = DAG.getConstant(PtrByteSize - ObjSize, dl, PtrVT);
Ulrich Weigand24195972014-07-20 22:36:52 +00003063 Arg = DAG.getNode(ISD::ADD, dl, ArgOff.getValueType(), Arg, ArgOff);
3064 }
3065 InVals.push_back(Arg);
3066
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003067 if (GPR_idx != Num_GPR_Regs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003068 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003069 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003070 SDValue Store;
3071
3072 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
3073 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
3074 (ObjSize == 2 ? MVT::i16 : MVT::i32));
Ulrich Weigand24195972014-07-20 22:36:52 +00003075 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, Arg,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003076 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003077 ObjType, false, false, 0);
3078 } else {
3079 // For sizes that don't fit a truncating store (3, 5, 6, 7),
3080 // store the whole register as-is to the parameter save area
Ulrich Weigand24195972014-07-20 22:36:52 +00003081 // slot.
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003082 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003083 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003084 false, false, 0);
3085 }
3086
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003087 MemOps.push_back(Store);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003088 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003089 // Whether we copied from a register or not, advance the offset
3090 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003091 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003092 continue;
3093 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003094
Ulrich Weigand24195972014-07-20 22:36:52 +00003095 // The value of the object is its address, which is the address of
3096 // its first stack doubleword.
3097 InVals.push_back(FIN);
3098
3099 // Store whatever pieces of the object are in registers to memory.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003100 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
Ulrich Weigand24195972014-07-20 22:36:52 +00003101 if (GPR_idx == Num_GPR_Regs)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003102 break;
Ulrich Weigand24195972014-07-20 22:36:52 +00003103
3104 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3105 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
3106 SDValue Addr = FIN;
3107 if (j) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003108 SDValue Off = DAG.getConstant(j, dl, PtrVT);
Ulrich Weigand24195972014-07-20 22:36:52 +00003109 Addr = DAG.getNode(ISD::ADD, dl, Off.getValueType(), Addr, Off);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003110 }
Ulrich Weigand24195972014-07-20 22:36:52 +00003111 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, Addr,
3112 MachinePointerInfo(FuncArg, j),
3113 false, false, 0);
3114 MemOps.push_back(Store);
3115 ++GPR_idx;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003116 }
Ulrich Weigand24195972014-07-20 22:36:52 +00003117 ArgOffset += ArgSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003118 continue;
3119 }
3120
3121 switch (ObjectVT.getSimpleVT().SimpleTy) {
3122 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00003123 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003124 case MVT::i32:
3125 case MVT::i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003126 // These can be scalar arguments or elements of an integer array type
3127 // passed directly. Clang may use those instead of "byval" aggregate
3128 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003129 if (GPR_idx != Num_GPR_Regs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003130 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003131 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
3132
Hal Finkel940ab932014-02-28 00:27:01 +00003133 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003134 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
3135 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003136 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003137 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003138 if (CallConv == CallingConv::Fast)
3139 ComputeArgOffset();
3140
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003141 needsLoad = true;
3142 ArgSize = PtrByteSize;
3143 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003144 if (CallConv != CallingConv::Fast || needsLoad)
3145 ArgOffset += 8;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003146 break;
3147
3148 case MVT::f32:
3149 case MVT::f64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003150 // These can be scalar arguments or elements of a float array type
3151 // passed directly. The latter are used to implement ELFv2 homogenous
3152 // float aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003153 if (FPR_idx != Num_FPR_Regs) {
3154 unsigned VReg;
3155
3156 if (ObjectVT == MVT::f32)
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00003157 VReg = MF.addLiveIn(FPR[FPR_idx],
3158 Subtarget.hasP8Vector()
3159 ? &PPC::VSSRCRegClass
3160 : &PPC::F4RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003161 else
Eric Christophercccae792015-01-30 22:02:31 +00003162 VReg = MF.addLiveIn(FPR[FPR_idx], Subtarget.hasVSX()
3163 ? &PPC::VSFRCRegClass
3164 : &PPC::F8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003165
3166 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
3167 ++FPR_idx;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003168 } else if (GPR_idx != Num_GPR_Regs && CallConv != CallingConv::Fast) {
Hal Finkel8ea446b2015-01-18 14:31:10 +00003169 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8
3170 // once we support fp <-> gpr moves.
3171
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003172 // This can only ever happen in the presence of f32 array types,
3173 // since otherwise we never run out of FPRs before running out
3174 // of GPRs.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003175 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003176 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
3177
3178 if (ObjectVT == MVT::f32) {
3179 if ((ArgOffset % PtrByteSize) == (isLittleEndian ? 4 : 0))
3180 ArgVal = DAG.getNode(ISD::SRL, dl, MVT::i64, ArgVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003181 DAG.getConstant(32, dl, MVT::i32));
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003182 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
3183 }
3184
3185 ArgVal = DAG.getNode(ISD::BITCAST, dl, ObjectVT, ArgVal);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003186 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003187 if (CallConv == CallingConv::Fast)
3188 ComputeArgOffset();
3189
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003190 needsLoad = true;
3191 }
3192
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003193 // When passing an array of floats, the array occupies consecutive
3194 // space in the argument area; only round up to the next doubleword
3195 // at the end of the array. Otherwise, each float takes 8 bytes.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003196 if (CallConv != CallingConv::Fast || needsLoad) {
3197 ArgSize = Flags.isInConsecutiveRegs() ? ObjSize : PtrByteSize;
3198 ArgOffset += ArgSize;
3199 if (Flags.isInConsecutiveRegsLast())
3200 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
3201 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003202 break;
3203 case MVT::v4f32:
3204 case MVT::v4i32:
3205 case MVT::v8i16:
3206 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00003207 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00003208 case MVT::v2i64:
Kit Bartond4eb73c2015-05-05 16:10:44 +00003209 case MVT::v1i128:
Hal Finkelc93a9a22015-02-25 01:06:45 +00003210 if (!Subtarget.hasQPX()) {
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003211 // These can be scalar arguments or elements of a vector array type
3212 // passed directly. The latter are used to implement ELFv2 homogenous
3213 // vector aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003214 if (VR_idx != Num_VR_Regs) {
Hal Finkel7811c612014-03-28 19:58:11 +00003215 unsigned VReg = (ObjectVT == MVT::v2f64 || ObjectVT == MVT::v2i64) ?
3216 MF.addLiveIn(VSRH[VR_idx], &PPC::VSHRCRegClass) :
3217 MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003218 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003219 ++VR_idx;
3220 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003221 if (CallConv == CallingConv::Fast)
3222 ComputeArgOffset();
3223
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003224 needsLoad = true;
3225 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003226 if (CallConv != CallingConv::Fast || needsLoad)
3227 ArgOffset += 16;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003228 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00003229 } // not QPX
3230
3231 assert(ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 &&
3232 "Invalid QPX parameter type");
3233 /* fall through */
3234
3235 case MVT::v4f64:
3236 case MVT::v4i1:
3237 // QPX vectors are treated like their scalar floating-point subregisters
3238 // (except that they're larger).
3239 unsigned Sz = ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 ? 16 : 32;
3240 if (QFPR_idx != Num_QFPR_Regs) {
3241 const TargetRegisterClass *RC;
3242 switch (ObjectVT.getSimpleVT().SimpleTy) {
3243 case MVT::v4f64: RC = &PPC::QFRCRegClass; break;
3244 case MVT::v4f32: RC = &PPC::QSRCRegClass; break;
3245 default: RC = &PPC::QBRCRegClass; break;
3246 }
3247
3248 unsigned VReg = MF.addLiveIn(QFPR[QFPR_idx], RC);
3249 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
3250 ++QFPR_idx;
3251 } else {
3252 if (CallConv == CallingConv::Fast)
3253 ComputeArgOffset();
3254 needsLoad = true;
3255 }
3256 if (CallConv != CallingConv::Fast || needsLoad)
3257 ArgOffset += Sz;
3258 break;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003259 }
3260
3261 // We need to load the argument to a virtual register if we determined
3262 // above that we ran out of physical registers of the appropriate type.
3263 if (needsLoad) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00003264 if (ObjSize < ArgSize && !isLittleEndian)
3265 CurArgOffset += ArgSize - ObjSize;
3266 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, isImmutable);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003267 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
3268 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
3269 false, false, false, 0);
3270 }
3271
3272 InVals.push_back(ArgVal);
3273 }
3274
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003275 // Area that is at least reserved in the caller of this function.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00003276 unsigned MinReservedArea;
Ulrich Weigand8658f172014-07-20 23:43:15 +00003277 if (HasParameterArea)
3278 MinReservedArea = std::max(ArgOffset, LinkageSize + 8 * PtrByteSize);
3279 else
3280 MinReservedArea = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003281
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003282 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003283 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003284 // taking the difference between two stack areas will result in an aligned
3285 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00003286 MinReservedArea =
3287 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003288 FuncInfo->setMinReservedArea(MinReservedArea);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003289
3290 // If the function takes variable number of arguments, make a frame index for
3291 // the start of the first vararg value... for expansion of llvm.va_start.
3292 if (isVarArg) {
3293 int Depth = ArgOffset;
3294
3295 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00003296 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003297 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
3298
3299 // If this function is vararg, store any remaining integer argument regs
3300 // to their spots on the stack so that they may be loaded by deferencing the
3301 // result of va_next.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00003302 for (GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
3303 GPR_idx < Num_GPR_Regs; ++GPR_idx) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003304 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3305 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
3306 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3307 MachinePointerInfo(), false, false, 0);
3308 MemOps.push_back(Store);
3309 // Increment the address by four for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003310 SDValue PtrOff = DAG.getConstant(PtrByteSize, dl, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003311 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
3312 }
3313 }
3314
3315 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003316 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003317
3318 return Chain;
3319}
3320
3321SDValue
3322PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003323 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003324 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003325 const SmallVectorImpl<ISD::InputArg>
3326 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003327 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003328 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003329 // TODO: add description of PPC stack frame format, or at least some docs.
3330 //
3331 MachineFunction &MF = DAG.getMachineFunction();
3332 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00003333 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003334
Owen Anderson53aa7a92009-08-10 22:56:29 +00003335 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003336 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003337 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003338 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
3339 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00003340 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Eric Christophera4ae2132015-02-13 22:22:57 +00003341 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003342 unsigned ArgOffset = LinkageSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003343 // Area that is at least reserved in caller of this function.
3344 unsigned MinReservedArea = ArgOffset;
3345
Craig Topper840beec2014-04-04 05:16:06 +00003346 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003347 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
3348 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
3349 };
Craig Topper840beec2014-04-04 05:16:06 +00003350 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00003351 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3352 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3353 };
Craig Topper840beec2014-04-04 05:16:06 +00003354 static const MCPhysReg VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003355 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3356 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3357 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00003358
Owen Andersone2f23a32007-09-07 04:06:50 +00003359 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003360 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00003361 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00003362
3363 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003364
Craig Topper840beec2014-04-04 05:16:06 +00003365 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003366
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003367 // In 32-bit non-varargs functions, the stack space for vectors is after the
3368 // stack space for non-vectors. We do not use this space unless we have
3369 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00003370 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003371 // that out...for the pathological case, compute VecArgOffset as the
3372 // start of the vector parameter area. Computing VecArgOffset is the
3373 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003374 unsigned VecArgOffset = ArgOffset;
3375 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003376 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003377 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003378 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003379 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003380
Duncan Sandsd97eea32008-03-21 09:14:45 +00003381 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003382 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00003383 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003384 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003385 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
3386 VecArgOffset += ArgSize;
3387 continue;
3388 }
3389
Owen Anderson9f944592009-08-11 20:47:22 +00003390 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003391 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003392 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003393 case MVT::i32:
3394 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003395 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003396 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003397 case MVT::i64: // PPC64
3398 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003399 // FIXME: We are guaranteed to be !isPPC64 at this point.
3400 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003401 VecArgOffset += 8;
3402 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003403 case MVT::v4f32:
3404 case MVT::v4i32:
3405 case MVT::v8i16:
3406 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003407 // Nothing to do, we're only looking at Nonvector args here.
3408 break;
3409 }
3410 }
3411 }
3412 // We've found where the vector parameter area in memory is. Skip the
3413 // first 12 parameters; these don't use that memory.
3414 VecArgOffset = ((VecArgOffset+15)/16)*16;
3415 VecArgOffset += 12*16;
3416
Chris Lattner4302e8f2006-05-16 18:18:50 +00003417 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00003418 // entry to a function on PPC, the arguments start after the linkage area,
3419 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00003420
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003421 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003422 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00003423 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00003424 unsigned CurArgIdx = 0;
3425 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003426 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003427 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00003428 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00003429 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00003430 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003431 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Andrew Trick05938a52015-02-16 18:10:47 +00003432 if (Ins[ArgNo].isOrigArg()) {
3433 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx);
3434 CurArgIdx = Ins[ArgNo].getOrigArgIndex();
3435 }
Chris Lattner318f0d22006-05-16 18:51:52 +00003436 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003437
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003438 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00003439 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
3440 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003441 if (isVarArg || isPPC64) {
3442 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003443 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003444 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003445 PtrByteSize);
3446 } else nAltivecParamsAtEnd++;
3447 } else
3448 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003449 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003450 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003451 PtrByteSize);
3452
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003453 // FIXME the codegen can be much improved in some cases.
3454 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003455 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003456 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit");
3457
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003458 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003459 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003460 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003461 // Objects of size 1 and 2 are right justified, everything else is
3462 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00003463 if (ObjSize==1 || ObjSize==2) {
3464 CurArgOffset = CurArgOffset + (4 - ObjSize);
3465 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003466 // The value of the object is its address.
Hal Finkel41a55ad2014-08-16 00:17:05 +00003467 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, false, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003468 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003469 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003470 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00003471 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003472 unsigned VReg;
3473 if (isPPC64)
3474 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3475 else
3476 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003477 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003478 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003479 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003480 MachinePointerInfo(FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003481 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00003482 MemOps.push_back(Store);
3483 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00003484 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003485
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003486 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00003487
Dale Johannesen21a8f142008-03-08 01:41:42 +00003488 continue;
3489 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003490 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
3491 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003492 // to memory. ArgOffset will be the address of the beginning
3493 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003494 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003495 unsigned VReg;
3496 if (isPPC64)
3497 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3498 else
3499 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00003500 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003501 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003502 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003503 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003504 MachinePointerInfo(FuncArg, j),
David Greene87a5abe2010-02-15 16:56:53 +00003505 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003506 MemOps.push_back(Store);
3507 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003508 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003509 } else {
3510 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
3511 break;
3512 }
3513 }
3514 continue;
3515 }
3516
Owen Anderson9f944592009-08-11 20:47:22 +00003517 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003518 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003519 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003520 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00003521 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00003522 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003523 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003524 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Hal Finkel7f908e82014-03-06 00:45:19 +00003525
3526 if (ObjectVT == MVT::i1)
3527 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal);
3528
Bill Wendling968f32c2008-03-07 20:49:02 +00003529 ++GPR_idx;
3530 } else {
3531 needsLoad = true;
3532 ArgSize = PtrByteSize;
3533 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003534 // All int arguments reserve stack space in the Darwin ABI.
3535 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00003536 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003537 }
Bill Wendling968f32c2008-03-07 20:49:02 +00003538 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00003539 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00003540 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003541 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003542 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00003543
Hal Finkel940ab932014-02-28 00:27:01 +00003544 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00003545 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00003546 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003547 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00003548
Chris Lattnerec78cad2006-06-26 22:48:35 +00003549 ++GPR_idx;
3550 } else {
3551 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00003552 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003553 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003554 // All int arguments reserve stack space in the Darwin ABI.
3555 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003556 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003557
Owen Anderson9f944592009-08-11 20:47:22 +00003558 case MVT::f32:
3559 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00003560 // Every 4 bytes of argument space consumes one of the GPRs available for
3561 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003562 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003563 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00003564 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003565 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00003566 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003567 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003568 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003569
Owen Anderson9f944592009-08-11 20:47:22 +00003570 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00003571 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003572 else
Devang Patelf3292b22011-02-21 23:21:26 +00003573 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003574
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003575 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003576 ++FPR_idx;
3577 } else {
3578 needsLoad = true;
3579 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003580
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003581 // All FP arguments reserve stack space in the Darwin ABI.
3582 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003583 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003584 case MVT::v4f32:
3585 case MVT::v4i32:
3586 case MVT::v8i16:
3587 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00003588 // Note that vector arguments in registers don't reserve stack space,
3589 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003590 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003591 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003592 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00003593 if (isVarArg) {
3594 while ((ArgOffset % 16) != 0) {
3595 ArgOffset += PtrByteSize;
3596 if (GPR_idx != Num_GPR_Regs)
3597 GPR_idx++;
3598 }
3599 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003600 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00003601 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003602 ++VR_idx;
3603 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003604 if (!isVarArg && !isPPC64) {
3605 // Vectors go after all the nonvectors.
3606 CurArgOffset = VecArgOffset;
3607 VecArgOffset += 16;
3608 } else {
3609 // Vectors are aligned.
3610 ArgOffset = ((ArgOffset+15)/16)*16;
3611 CurArgOffset = ArgOffset;
3612 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00003613 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003614 needsLoad = true;
3615 }
3616 break;
3617 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003618
Chris Lattner4302e8f2006-05-16 18:18:50 +00003619 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003620 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003621 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003622 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003623 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00003624 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003625 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00003626 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003627 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003628 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003629
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003630 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003631 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003632
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003633 // Allow for Altivec parameters at the end, if needed.
3634 if (nAltivecParamsAtEnd) {
3635 MinReservedArea = ((MinReservedArea+15)/16)*16;
3636 MinReservedArea += 16*nAltivecParamsAtEnd;
3637 }
3638
3639 // Area that is at least reserved in the caller of this function.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003640 MinReservedArea = std::max(MinReservedArea, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003641
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003642 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003643 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003644 // taking the difference between two stack areas will result in an aligned
3645 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00003646 MinReservedArea =
3647 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003648 FuncInfo->setMinReservedArea(MinReservedArea);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003649
Chris Lattner4302e8f2006-05-16 18:18:50 +00003650 // If the function takes variable number of arguments, make a frame index for
3651 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003652 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003653 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003654
Dan Gohman31ae5862010-04-17 14:41:14 +00003655 FuncInfo->setVarArgsFrameIndex(
3656 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00003657 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00003658 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003659
Chris Lattner4302e8f2006-05-16 18:18:50 +00003660 // If this function is vararg, store any remaining integer argument regs
3661 // to their spots on the stack so that they may be loaded by deferencing the
3662 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003663 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00003664 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00003665
Chris Lattner2cca3852006-11-18 01:57:19 +00003666 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00003667 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003668 else
Devang Patelf3292b22011-02-21 23:21:26 +00003669 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003670
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003671 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00003672 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3673 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003674 MemOps.push_back(Store);
3675 // Increment the address by four for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003676 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00003677 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003678 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003679 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003680
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003681 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003682 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003683
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003684 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003685}
3686
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003687/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003688/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00003689static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003690 unsigned ParamSize) {
3691
Dale Johannesen86dcae12009-11-24 01:09:07 +00003692 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003693
3694 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
3695 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3696 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3697 // Remember only if the new adjustement is bigger.
3698 if (SPDiff < FI->getTailCallSPDelta())
3699 FI->setTailCallSPDelta(SPDiff);
3700
3701 return SPDiff;
3702}
3703
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003704/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3705/// for tail call optimization. Targets which want to do tail call
3706/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003707bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003708PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003709 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003710 bool isVarArg,
3711 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003712 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003713 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00003714 return false;
3715
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003716 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003717 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003718 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003719
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003720 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00003721 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003722 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
3723 // Functions containing by val parameters are not supported.
3724 for (unsigned i = 0; i != Ins.size(); i++) {
3725 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3726 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003727 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003728
Alp Tokerf907b892013-12-05 05:44:44 +00003729 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003730 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
3731 return true;
3732
3733 // At the moment we can only do local tail calls (in same module, hidden
3734 // or protected) if we are generating PIC.
3735 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
3736 return G->getGlobal()->hasHiddenVisibility()
3737 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003738 }
3739
3740 return false;
3741}
3742
Chris Lattnereb755fc2006-05-17 19:00:46 +00003743/// isCallCompatibleAddress - Return the immediate to use if the specified
3744/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003745static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00003746 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003747 if (!C) return nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003748
Dan Gohmaneffb8942008-09-12 16:56:44 +00003749 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003750 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00003751 SignExtend32<26>(Addr) != Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +00003752 return nullptr; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003753
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003754 return DAG.getConstant((int)C->getZExtValue() >> 2, SDLoc(Op),
Gabor Greiff304a7a2008-08-28 21:40:38 +00003755 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003756}
3757
Dan Gohmand78c4002008-05-13 00:00:25 +00003758namespace {
3759
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003760struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003761 SDValue Arg;
3762 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003763 int FrameIdx;
3764
3765 TailCallArgumentInfo() : FrameIdx(0) {}
3766};
3767
Dan Gohmand78c4002008-05-13 00:00:25 +00003768}
3769
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003770/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3771static void
3772StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003773 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003774 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3775 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003776 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003777 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003778 SDValue Arg = TailCallArgs[i].Arg;
3779 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003780 int FI = TailCallArgs[i].FrameIdx;
3781 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003782 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003783 MachinePointerInfo::getFixedStack(FI),
3784 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003785 }
3786}
3787
3788/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3789/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003790static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003791 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003792 SDValue Chain,
3793 SDValue OldRetAddr,
3794 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003795 int SPDiff,
3796 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003797 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003798 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003799 if (SPDiff) {
3800 // Calculate the new stack slot for the return address.
3801 int SlotSize = isPPC64 ? 8 : 4;
Eric Christopherdc3a8a42015-02-13 00:39:38 +00003802 const PPCFrameLowering *FL =
3803 MF.getSubtarget<PPCSubtarget>().getFrameLowering();
3804 int NewRetAddrLoc = SPDiff + FL->getReturnSaveOffset();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003805 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003806 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003807 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003808 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003809 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003810 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003811 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003812
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003813 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3814 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003815 if (isDarwinABI) {
Eric Christopherdc3a8a42015-02-13 00:39:38 +00003816 int NewFPLoc = SPDiff + FL->getFramePointerSaveOffset();
David Greene1fbe0542009-11-12 20:49:22 +00003817 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003818 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003819 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3820 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003821 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003822 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003823 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003824 }
3825 return Chain;
3826}
3827
3828/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3829/// the position of the argument.
3830static void
3831CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003832 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003833 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003834 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003835 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003836 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003837 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003838 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003839 TailCallArgumentInfo Info;
3840 Info.Arg = Arg;
3841 Info.FrameIdxOp = FIN;
3842 Info.FrameIdx = FI;
3843 TailCallArguments.push_back(Info);
3844}
3845
3846/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3847/// stack slot. Returns the chain as result and the loaded frame pointers in
3848/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003849SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003850 int SPDiff,
3851 SDValue Chain,
3852 SDValue &LROpOut,
3853 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003854 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003855 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003856 if (SPDiff) {
3857 // Load the LR and FP stack slot for later adjusting.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003858 EVT VT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003859 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003860 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003861 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003862 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003863
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003864 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3865 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003866 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003867 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003868 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003869 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003870 Chain = SDValue(FPOpOut.getNode(), 1);
3871 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003872 }
3873 return Chain;
3874}
3875
Dale Johannesen85d41a12008-03-04 23:17:14 +00003876/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003877/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003878/// specified by the specific parameter attribute. The copy will be passed as
3879/// a byval function parameter.
3880/// Sometimes what we are copying is the end of a larger object, the part that
3881/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003882static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003883CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003884 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003885 SDLoc dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003886 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003887 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003888 false, false, false, MachinePointerInfo(),
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003889 MachinePointerInfo());
Dale Johannesen85d41a12008-03-04 23:17:14 +00003890}
Chris Lattner43df5b32007-02-25 05:34:32 +00003891
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003892/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3893/// tail calls.
3894static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003895LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3896 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003897 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003898 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3899 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003900 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003901 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003902 if (!isTailCall) {
3903 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003904 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003905 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003906 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003907 else
Owen Anderson9f944592009-08-11 20:47:22 +00003908 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003909 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003910 DAG.getConstant(ArgOffset, dl, PtrVT));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003911 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003912 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3913 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003914 // Calculate and remember argument location.
3915 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3916 TailCallArguments);
3917}
3918
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003919static
3920void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003921 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003922 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003923 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003924 MachineFunction &MF = DAG.getMachineFunction();
3925
3926 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3927 // might overwrite each other in case of tail call optimization.
3928 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003929 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003930 InFlag = SDValue();
3931 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3932 MemOpChains2, dl);
3933 if (!MemOpChains2.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003934 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003935
3936 // Store the return address to the appropriate stack slot.
3937 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3938 isPPC64, isDarwinABI, dl);
3939
3940 // Emit callseq_end just before tailcall node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003941 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
3942 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003943 InFlag = Chain.getValue(1);
3944}
3945
Hal Finkel87deb0b2015-01-12 04:34:47 +00003946// Is this global address that of a function that can be called by name? (as
3947// opposed to something that must hold a descriptor for an indirect call).
3948static bool isFunctionGlobalAddress(SDValue Callee) {
3949 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3950 if (Callee.getOpcode() == ISD::GlobalTLSAddress ||
3951 Callee.getOpcode() == ISD::TargetGlobalTLSAddress)
3952 return false;
3953
3954 return G->getGlobal()->getType()->getElementType()->isFunctionTy();
3955 }
3956
3957 return false;
3958}
3959
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003960static
3961unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Hal Finkele2ab0f12015-01-15 21:17:34 +00003962 SDValue &Chain, SDValue CallSeqStart, SDLoc dl, int SPDiff,
3963 bool isTailCall, bool IsPatchPoint,
Craig Topperb94011f2013-07-14 04:42:23 +00003964 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3965 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Hal Finkele2ab0f12015-01-15 21:17:34 +00003966 ImmutableCallSite *CS, const PPCSubtarget &Subtarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003967
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003968 bool isPPC64 = Subtarget.isPPC64();
3969 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003970 bool isELFv2ABI = Subtarget.isELFv2ABI();
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003971
Owen Anderson53aa7a92009-08-10 22:56:29 +00003972 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003973 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003974 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003975
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003976 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003977
Torok Edwin31e90d22010-08-04 20:47:44 +00003978 bool needIndirectCall = true;
Ulrich Weigand9aa09ef2014-06-18 16:14:04 +00003979 if (!isSVR4ABI || !isPPC64)
3980 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
3981 // If this is an absolute destination address, use the munged value.
3982 Callee = SDValue(Dest, 0);
3983 needIndirectCall = false;
3984 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003985
Hal Finkel87deb0b2015-01-12 04:34:47 +00003986 if (isFunctionGlobalAddress(Callee)) {
3987 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Callee);
3988 // A call to a TLS address is actually an indirect call to a
3989 // thread-specific pointer.
Eric Christopher79cc1e32014-09-02 22:28:02 +00003990 unsigned OpFlags = 0;
3991 if ((DAG.getTarget().getRelocationModel() != Reloc::Static &&
3992 (Subtarget.getTargetTriple().isMacOSX() &&
3993 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
3994 (G->getGlobal()->isDeclaration() ||
3995 G->getGlobal()->isWeakForLinker())) ||
3996 (Subtarget.isTargetELF() && !isPPC64 &&
3997 !G->getGlobal()->hasLocalLinkage() &&
3998 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
3999 // PC-relative references to external symbols should go through $stub,
4000 // unless we're building with the leopard linker or later, which
4001 // automatically synthesizes these stubs.
4002 OpFlags = PPCII::MO_PLT_OR_STUB;
Eric Christopherb9fd9ed2014-08-07 22:02:54 +00004003 }
Eric Christopher79cc1e32014-09-02 22:28:02 +00004004
4005 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
4006 // every direct call is) turn it into a TargetGlobalAddress /
4007 // TargetExternalSymbol node so that legalize doesn't hack it.
4008 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
4009 Callee.getValueType(), 0, OpFlags);
4010 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00004011 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004012
Torok Edwin31e90d22010-08-04 20:47:44 +00004013 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004014 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00004015
Hal Finkel3ee2af72014-07-18 23:29:49 +00004016 if ((DAG.getTarget().getRelocationModel() != Reloc::Static &&
4017 (Subtarget.getTargetTriple().isMacOSX() &&
4018 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5))) ||
4019 (Subtarget.isTargetELF() && !isPPC64 &&
Justin Hibbits17744c12015-01-10 07:50:31 +00004020 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004021 // PC-relative references to external symbols should go through $stub,
4022 // unless we're building with the leopard linker or later, which
4023 // automatically synthesizes these stubs.
Hal Finkel3ee2af72014-07-18 23:29:49 +00004024 OpFlags = PPCII::MO_PLT_OR_STUB;
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004025 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004026
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004027 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
4028 OpFlags);
4029 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00004030 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004031
Hal Finkel934361a2015-01-14 01:07:51 +00004032 if (IsPatchPoint) {
4033 // We'll form an invalid direct call when lowering a patchpoint; the full
4034 // sequence for an indirect call is complicated, and many of the
4035 // instructions introduced might have side effects (and, thus, can't be
4036 // removed later). The call itself will be removed as soon as the
4037 // argument/return lowering is complete, so the fact that it has the wrong
4038 // kind of operands should not really matter.
4039 needIndirectCall = false;
4040 }
4041
Torok Edwin31e90d22010-08-04 20:47:44 +00004042 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004043 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
4044 // to do the call, we can't use PPCISD::CALL.
4045 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00004046
Hal Finkel63fb9282015-01-13 18:25:05 +00004047 if (isSVR4ABI && isPPC64 && !isELFv2ABI) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00004048 // Function pointers in the 64-bit SVR4 ABI do not point to the function
4049 // entry point, but to the function descriptor (the function entry point
4050 // address is part of the function descriptor though).
4051 // The function descriptor is a three doubleword structure with the
4052 // following fields: function entry point, TOC base address and
4053 // environment pointer.
4054 // Thus for a call through a function pointer, the following actions need
4055 // to be performed:
4056 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00004057 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00004058 // 2. Load the address of the function entry point from the function
4059 // descriptor.
4060 // 3. Load the TOC of the callee from the function descriptor into r2.
4061 // 4. Load the environment pointer from the function descriptor into
4062 // r11.
4063 // 5. Branch to the function entry point address.
4064 // 6. On return of the callee, the TOC of the caller needs to be
4065 // restored (this is done in FinishCall()).
4066 //
Hal Finkele2ab0f12015-01-15 21:17:34 +00004067 // The loads are scheduled at the beginning of the call sequence, and the
4068 // register copies are flagged together to ensure that no other
Tilmann Scheller79fef932009-12-18 13:00:15 +00004069 // operations can be scheduled in between. E.g. without flagging the
Hal Finkele2ab0f12015-01-15 21:17:34 +00004070 // copies together, a TOC access in the caller could be scheduled between
4071 // the assignment of the callee TOC and the branch to the callee, which
Tilmann Scheller79fef932009-12-18 13:00:15 +00004072 // results in the TOC access going through the TOC of the callee instead
4073 // of going through the TOC of the caller, which leads to incorrect code.
4074
4075 // Load the address of the function entry point from the function
4076 // descriptor.
Hal Finkele2ab0f12015-01-15 21:17:34 +00004077 SDValue LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-1);
4078 if (LDChain.getValueType() == MVT::Glue)
4079 LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-2);
4080
4081 bool LoadsInv = Subtarget.hasInvariantFunctionDescriptors();
4082
4083 MachinePointerInfo MPI(CS ? CS->getCalledValue() : nullptr);
4084 SDValue LoadFuncPtr = DAG.getLoad(MVT::i64, dl, LDChain, Callee, MPI,
4085 false, false, LoadsInv, 8);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004086
4087 // Load environment pointer into r11.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004088 SDValue PtrOff = DAG.getIntPtrConstant(16, dl);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004089 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004090 SDValue LoadEnvPtr = DAG.getLoad(MVT::i64, dl, LDChain, AddPtr,
4091 MPI.getWithOffset(16), false, false,
4092 LoadsInv, 8);
4093
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004094 SDValue TOCOff = DAG.getIntPtrConstant(8, dl);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004095 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, TOCOff);
4096 SDValue TOCPtr = DAG.getLoad(MVT::i64, dl, LDChain, AddTOC,
4097 MPI.getWithOffset(8), false, false,
4098 LoadsInv, 8);
4099
Hal Finkele6698d52015-02-01 15:03:28 +00004100 setUsesTOCBasePtr(DAG);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004101 SDValue TOCVal = DAG.getCopyToReg(Chain, dl, PPC::X2, TOCPtr,
4102 InFlag);
4103 Chain = TOCVal.getValue(0);
4104 InFlag = TOCVal.getValue(1);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004105
4106 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
4107 InFlag);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004108
Tilmann Scheller79fef932009-12-18 13:00:15 +00004109 Chain = EnvVal.getValue(0);
4110 InFlag = EnvVal.getValue(1);
4111
Tilmann Scheller79fef932009-12-18 13:00:15 +00004112 MTCTROps[0] = Chain;
4113 MTCTROps[1] = LoadFuncPtr;
4114 MTCTROps[2] = InFlag;
4115 }
4116
Hal Finkel63fb9282015-01-13 18:25:05 +00004117 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys,
4118 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
4119 InFlag = Chain.getValue(1);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004120
4121 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00004122 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004123 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004124 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004125 CallOpc = PPCISD::BCTRL;
Craig Topper062a2ba2014-04-25 05:30:21 +00004126 Callee.setNode(nullptr);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004127 // Add use of X11 (holding environment pointer)
Hal Finkel63fb9282015-01-13 18:25:05 +00004128 if (isSVR4ABI && isPPC64 && !isELFv2ABI)
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004129 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004130 // Add CTR register as callee so a bctr can be emitted later.
4131 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00004132 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004133 }
4134
4135 // If this is a direct call, pass the chain and the callee.
4136 if (Callee.getNode()) {
4137 Ops.push_back(Chain);
4138 Ops.push_back(Callee);
4139 }
4140 // If this is a tail call add stack pointer delta.
4141 if (isTailCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004142 Ops.push_back(DAG.getConstant(SPDiff, dl, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004143
4144 // Add argument registers to the end of the list so that they are known live
4145 // into the call.
4146 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
4147 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
4148 RegsToPass[i].second.getValueType()));
4149
Hal Finkelaf519932015-01-19 07:20:27 +00004150 // All calls, in both the ELF V1 and V2 ABIs, need the TOC register live
4151 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00004152 if (isSVR4ABI && isPPC64 && !IsPatchPoint) {
4153 setUsesTOCBasePtr(DAG);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004154 Ops.push_back(DAG.getRegister(PPC::X2, PtrVT));
Hal Finkele6698d52015-02-01 15:03:28 +00004155 }
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004156
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004157 return CallOpc;
4158}
4159
Roman Divacky76293062012-09-18 16:47:58 +00004160static
4161bool isLocalCall(const SDValue &Callee)
4162{
4163 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00004164 return !G->getGlobal()->isDeclaration() &&
4165 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00004166 return false;
4167}
4168
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004169SDValue
4170PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004171 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004172 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004173 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004174 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004175
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004176 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00004177 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
4178 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004179 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004180
4181 // Copy all of the result registers out of their specified physreg.
4182 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
4183 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004184 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004185
4186 SDValue Val = DAG.getCopyFromReg(Chain, dl,
4187 VA.getLocReg(), VA.getLocVT(), InFlag);
4188 Chain = Val.getValue(1);
4189 InFlag = Val.getValue(2);
4190
4191 switch (VA.getLocInfo()) {
4192 default: llvm_unreachable("Unknown loc info!");
4193 case CCValAssign::Full: break;
4194 case CCValAssign::AExt:
4195 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4196 break;
4197 case CCValAssign::ZExt:
4198 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
4199 DAG.getValueType(VA.getValVT()));
4200 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4201 break;
4202 case CCValAssign::SExt:
4203 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
4204 DAG.getValueType(VA.getValVT()));
4205 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4206 break;
4207 }
4208
4209 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004210 }
4211
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004212 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004213}
4214
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004215SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004216PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Hal Finkel934361a2015-01-14 01:07:51 +00004217 bool isTailCall, bool isVarArg, bool IsPatchPoint,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004218 SelectionDAG &DAG,
4219 SmallVector<std::pair<unsigned, SDValue>, 8>
4220 &RegsToPass,
4221 SDValue InFlag, SDValue Chain,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004222 SDValue CallSeqStart, SDValue &Callee,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004223 int SPDiff, unsigned NumBytes,
4224 const SmallVectorImpl<ISD::InputArg> &Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004225 SmallVectorImpl<SDValue> &InVals,
4226 ImmutableCallSite *CS) const {
Ulrich Weigand8658f172014-07-20 23:43:15 +00004227
Owen Anderson53aa7a92009-08-10 22:56:29 +00004228 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004229 SmallVector<SDValue, 8> Ops;
Hal Finkele2ab0f12015-01-15 21:17:34 +00004230 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, CallSeqStart, dl,
4231 SPDiff, isTailCall, IsPatchPoint, RegsToPass,
4232 Ops, NodeTys, CS, Subtarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004233
Hal Finkel5ab37802012-08-28 02:10:27 +00004234 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004235 if (isVarArg && Subtarget.isSVR4ABI() && !Subtarget.isPPC64())
Hal Finkel5ab37802012-08-28 02:10:27 +00004236 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
4237
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004238 // When performing tail call optimization the callee pops its arguments off
4239 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00004240 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004241 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004242 (CallConv == CallingConv::Fast &&
4243 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004244
Roman Divackyef21be22012-03-06 16:41:49 +00004245 // Add a register mask operand representing the call-preserved registers.
Eric Christophercccae792015-01-30 22:02:31 +00004246 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00004247 const uint32_t *Mask =
4248 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv);
Roman Divackyef21be22012-03-06 16:41:49 +00004249 assert(Mask && "Missing call preserved mask for calling convention");
4250 Ops.push_back(DAG.getRegisterMask(Mask));
4251
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004252 if (InFlag.getNode())
4253 Ops.push_back(InFlag);
4254
4255 // Emit tail call.
4256 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004257 assert(((Callee.getOpcode() == ISD::Register &&
4258 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
4259 Callee.getOpcode() == ISD::TargetExternalSymbol ||
4260 Callee.getOpcode() == ISD::TargetGlobalAddress ||
4261 isa<ConstantSDNode>(Callee)) &&
4262 "Expecting an global address, external symbol, absolute value or register");
4263
Arnold Schwaighoferdc271142015-05-09 00:10:25 +00004264 DAG.getMachineFunction().getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00004265 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004266 }
4267
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004268 // Add a NOP immediately after the branch instruction when using the 64-bit
4269 // SVR4 ABI. At link time, if caller and callee are in a different module and
4270 // thus have a different TOC, the call will be replaced with a call to a stub
4271 // function which saves the current TOC, loads the TOC of the callee and
4272 // branches to the callee. The NOP will be replaced with a load instruction
4273 // which restores the TOC of the caller from the TOC save slot of the current
4274 // stack frame. If caller and callee belong to the same module (and have the
4275 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00004276
Hal Finkel934361a2015-01-14 01:07:51 +00004277 if (!isTailCall && Subtarget.isSVR4ABI()&& Subtarget.isPPC64() &&
4278 !IsPatchPoint) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004279 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00004280 // This is a call through a function pointer.
4281 // Restore the caller TOC from the save area into R2.
4282 // See PrepareCall() for more information about calls through function
4283 // pointers in the 64-bit SVR4 ABI.
4284 // We are using a target-specific load with r2 hard coded, because the
4285 // result of a target-independent load would never go directly into r2,
4286 // since r2 is a reserved register (which prevents the register allocator
4287 // from allocating it), resulting in an additional register being
4288 // allocated and an unnecessary move instruction being generated.
Hal Finkelfc096c92014-12-23 22:29:40 +00004289 CallOpc = PPCISD::BCTRL_LOAD_TOC;
4290
4291 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
4292 SDValue StackPtr = DAG.getRegister(PPC::X1, PtrVT);
Eric Christopher736d39e2015-02-13 00:39:36 +00004293 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004294 SDValue TOCOff = DAG.getIntPtrConstant(TOCSaveOffset, dl);
Hal Finkelfc096c92014-12-23 22:29:40 +00004295 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, StackPtr, TOCOff);
4296
4297 // The address needs to go after the chain input but before the flag (or
4298 // any other variadic arguments).
4299 Ops.insert(std::next(Ops.begin()), AddTOC);
Bill Schmidtcea15962013-09-26 17:09:28 +00004300 } else if ((CallOpc == PPCISD::CALL) &&
4301 (!isLocalCall(Callee) ||
Bill Schmidt82f1c772015-02-10 19:09:05 +00004302 DAG.getTarget().getRelocationModel() == Reloc::PIC_))
Roman Divacky76293062012-09-18 16:47:58 +00004303 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004304 CallOpc = PPCISD::CALL_NOP;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004305 }
4306
Craig Topper48d114b2014-04-26 18:35:24 +00004307 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Hal Finkel51861b42012-03-31 14:45:15 +00004308 InFlag = Chain.getValue(1);
4309
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004310 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
4311 DAG.getIntPtrConstant(BytesCalleePops, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00004312 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004313 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004314 InFlag = Chain.getValue(1);
4315
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004316 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
4317 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004318}
4319
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004320SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00004321PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004322 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00004323 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00004324 SDLoc &dl = CLI.DL;
4325 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
4326 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
4327 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00004328 SDValue Chain = CLI.Chain;
4329 SDValue Callee = CLI.Callee;
4330 bool &isTailCall = CLI.IsTailCall;
4331 CallingConv::ID CallConv = CLI.CallConv;
4332 bool isVarArg = CLI.IsVarArg;
Hal Finkel934361a2015-01-14 01:07:51 +00004333 bool IsPatchPoint = CLI.IsPatchPoint;
Hal Finkele2ab0f12015-01-15 21:17:34 +00004334 ImmutableCallSite *CS = CLI.CS;
Justin Holewinskiaa583972012-05-25 16:35:28 +00004335
Evan Cheng67a69dd2010-01-27 00:07:07 +00004336 if (isTailCall)
4337 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
4338 Ins, DAG);
4339
Hal Finkele2ab0f12015-01-15 21:17:34 +00004340 if (!isTailCall && CS && CS->isMustTailCall())
Reid Kleckner5772b772014-04-24 20:14:34 +00004341 report_fatal_error("failed to perform tail call elimination on a call "
4342 "site marked musttail");
4343
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004344 if (Subtarget.isSVR4ABI()) {
4345 if (Subtarget.isPPC64())
Bill Schmidt57d6de52012-10-23 15:51:16 +00004346 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004347 isTailCall, IsPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004348 dl, DAG, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004349 else
4350 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004351 isTailCall, IsPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004352 dl, DAG, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004353 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004354
Bill Schmidt57d6de52012-10-23 15:51:16 +00004355 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004356 isTailCall, IsPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004357 dl, DAG, InVals, CS);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004358}
4359
4360SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004361PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
4362 CallingConv::ID CallConv, bool isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004363 bool isTailCall, bool IsPatchPoint,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004364 const SmallVectorImpl<ISD::OutputArg> &Outs,
4365 const SmallVectorImpl<SDValue> &OutVals,
4366 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004367 SDLoc dl, SelectionDAG &DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004368 SmallVectorImpl<SDValue> &InVals,
4369 ImmutableCallSite *CS) const {
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004370 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004371 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004372
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004373 assert((CallConv == CallingConv::C ||
4374 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004375
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004376 unsigned PtrByteSize = 4;
4377
4378 MachineFunction &MF = DAG.getMachineFunction();
4379
4380 // Mark this function as potentially containing a function that contains a
4381 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4382 // and restoring the callers stack pointer in this functions epilog. This is
4383 // done because by tail calling the called function might overwrite the value
4384 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004385 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4386 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004387 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00004388
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004389 // Count how many bytes are to be pushed on the stack, including the linkage
4390 // area, parameter list area and the part of the local variable space which
4391 // contains copies of aggregates which are passed by value.
4392
4393 // Assign locations to all of the outgoing arguments.
4394 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00004395 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
4396 *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004397
4398 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00004399 CCInfo.AllocateStack(Subtarget.getFrameLowering()->getLinkageSize(),
Ulrich Weigand8658f172014-07-20 23:43:15 +00004400 PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004401
4402 if (isVarArg) {
4403 // Handle fixed and variable vector arguments differently.
4404 // Fixed vector arguments go into registers as long as registers are
4405 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004406 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00004407
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004408 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00004409 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004410 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004411 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00004412
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004413 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00004414 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
4415 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004416 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00004417 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
4418 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004419 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004420
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004421 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00004422#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00004423 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00004424 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00004425#endif
Craig Toppere73658d2014-04-28 04:05:08 +00004426 llvm_unreachable(nullptr);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004427 }
4428 }
4429 } else {
4430 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00004431 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004432 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004433
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004434 // Assign locations to all of the outgoing aggregate by value arguments.
4435 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004436 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Eric Christopherb5217502014-08-06 18:45:26 +00004437 ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004438
4439 // Reserve stack space for the allocations in CCInfo.
4440 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
4441
Bill Schmidtef17c142013-02-06 17:33:58 +00004442 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004443
4444 // Size of the linkage area, parameter list area and the part of the local
4445 // space variable where copies of aggregates which are passed by value are
4446 // stored.
4447 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004448
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004449 // Calculate by how many bytes the stack has to be adjusted in case of tail
4450 // call optimization.
4451 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4452
4453 // Adjust the stack pointer for the new arguments...
4454 // These operations are automatically eliminated by the prolog/epilog pass
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004455 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00004456 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004457 SDValue CallSeqStart = Chain;
4458
4459 // Load the return address and frame pointer so it can be moved somewhere else
4460 // later.
4461 SDValue LROp, FPOp;
4462 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
4463 dl);
4464
4465 // Set up a copy of the stack pointer for use loading and storing any
4466 // arguments that may not fit in the registers available for argument
4467 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00004468 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00004469
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004470 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4471 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4472 SmallVector<SDValue, 8> MemOpChains;
4473
Roman Divacky71038e72011-08-30 17:04:16 +00004474 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004475 // Walk the register/memloc assignments, inserting copies/loads.
4476 for (unsigned i = 0, j = 0, e = ArgLocs.size();
4477 i != e;
4478 ++i) {
4479 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004480 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004481 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00004482
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004483 if (Flags.isByVal()) {
4484 // Argument is an aggregate which is passed by value, thus we need to
4485 // create a copy of it in the local variable space of the current stack
4486 // frame (which is the stack frame of the caller) and pass the address of
4487 // this copy to the callee.
4488 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
4489 CCValAssign &ByValVA = ByValArgLocs[j++];
4490 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00004491
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004492 // Memory reserved in the local variable space of the callers stack frame.
4493 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004494
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004495 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004496 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00004497
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004498 // Create a copy of the argument in the local area of the current
4499 // stack frame.
4500 SDValue MemcpyCall =
4501 CreateCopyOfByValArgument(Arg, PtrOff,
4502 CallSeqStart.getNode()->getOperand(0),
4503 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00004504
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004505 // This must go outside the CALLSEQ_START..END.
4506 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004507 CallSeqStart.getNode()->getOperand(1),
4508 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004509 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4510 NewCallSeqStart.getNode());
4511 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00004512
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004513 // Pass the address of the aggregate copy on the stack either in a
4514 // physical register or in the parameter list area of the current stack
4515 // frame to the callee.
4516 Arg = PtrOff;
4517 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004518
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004519 if (VA.isRegLoc()) {
Hal Finkel2a9d3182014-03-06 00:23:33 +00004520 if (Arg.getValueType() == MVT::i1)
4521 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg);
4522
Roman Divacky71038e72011-08-30 17:04:16 +00004523 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004524 // Put argument in a physical register.
4525 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
4526 } else {
4527 // Put argument in the parameter list area of the current stack frame.
4528 assert(VA.isMemLoc());
4529 unsigned LocMemOffset = VA.getLocMemOffset();
4530
4531 if (!isTailCall) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004532 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004533 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
4534
4535 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00004536 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004537 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004538 } else {
4539 // Calculate and remember argument location.
4540 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
4541 TailCallArguments);
4542 }
4543 }
4544 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004545
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004546 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004547 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Wesley Peck527da1b2010-11-23 03:31:01 +00004548
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004549 // Build a sequence of copy-to-reg nodes chained together with token chain
4550 // and flag operands which copy the outgoing args into the appropriate regs.
4551 SDValue InFlag;
4552 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4553 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4554 RegsToPass[i].second, InFlag);
4555 InFlag = Chain.getValue(1);
4556 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004557
Hal Finkel5ab37802012-08-28 02:10:27 +00004558 // Set CR bit 6 to true if this is a vararg call with floating args passed in
4559 // registers.
4560 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004561 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
4562 SDValue Ops[] = { Chain, InFlag };
4563
Hal Finkel5ab37802012-08-28 02:10:27 +00004564 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00004565 dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1));
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004566
Hal Finkel5ab37802012-08-28 02:10:27 +00004567 InFlag = Chain.getValue(1);
4568 }
4569
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004570 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004571 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
4572 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004573
Hal Finkel934361a2015-01-14 01:07:51 +00004574 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint, DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004575 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff,
4576 NumBytes, Ins, InVals, CS);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004577}
4578
Bill Schmidt57d6de52012-10-23 15:51:16 +00004579// Copy an argument into memory, being careful to do this outside the
4580// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004581SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00004582PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
4583 SDValue CallSeqStart,
4584 ISD::ArgFlagsTy Flags,
4585 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004586 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004587 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
4588 CallSeqStart.getNode()->getOperand(0),
4589 Flags, DAG, dl);
4590 // The MEMCPY must go outside the CALLSEQ_START..END.
4591 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004592 CallSeqStart.getNode()->getOperand(1),
4593 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004594 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4595 NewCallSeqStart.getNode());
4596 return NewCallSeqStart;
4597}
4598
4599SDValue
4600PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004601 CallingConv::ID CallConv, bool isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004602 bool isTailCall, bool IsPatchPoint,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004603 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004604 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004605 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004606 SDLoc dl, SelectionDAG &DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004607 SmallVectorImpl<SDValue> &InVals,
4608 ImmutableCallSite *CS) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004609
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004610 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004611 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004612 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004613
Bill Schmidt57d6de52012-10-23 15:51:16 +00004614 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
4615 unsigned PtrByteSize = 8;
4616
4617 MachineFunction &MF = DAG.getMachineFunction();
4618
4619 // Mark this function as potentially containing a function that contains a
4620 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4621 // and restoring the callers stack pointer in this functions epilog. This is
4622 // done because by tail calling the called function might overwrite the value
4623 // in this function's (MF) stack pointer stack slot 0(SP).
4624 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4625 CallConv == CallingConv::Fast)
4626 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4627
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004628 assert(!(CallConv == CallingConv::Fast && isVarArg) &&
4629 "fastcc not supported on varargs functions");
4630
Bill Schmidt57d6de52012-10-23 15:51:16 +00004631 // Count how many bytes are to be pushed on the stack, including the linkage
Ulrich Weigand8658f172014-07-20 23:43:15 +00004632 // area, and parameter passing area. On ELFv1, the linkage area is 48 bytes
4633 // reserved space for [SP][CR][LR][2 x unused][TOC]; on ELFv2, the linkage
4634 // area is 32 bytes reserved space for [SP][CR][LR][TOC].
Eric Christophera4ae2132015-02-13 22:22:57 +00004635 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004636 unsigned NumBytes = LinkageSize;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004637 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Hal Finkelc93a9a22015-02-25 01:06:45 +00004638 unsigned &QFPR_idx = FPR_idx;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004639
4640 static const MCPhysReg GPR[] = {
4641 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4642 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4643 };
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004644 static const MCPhysReg VR[] = {
4645 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4646 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4647 };
4648 static const MCPhysReg VSRH[] = {
4649 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
4650 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
4651 };
4652
4653 const unsigned NumGPRs = array_lengthof(GPR);
4654 const unsigned NumFPRs = 13;
4655 const unsigned NumVRs = array_lengthof(VR);
Hal Finkelc93a9a22015-02-25 01:06:45 +00004656 const unsigned NumQFPRs = NumFPRs;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004657
4658 // When using the fast calling convention, we don't provide backing for
4659 // arguments that will be in registers.
4660 unsigned NumGPRsUsed = 0, NumFPRsUsed = 0, NumVRsUsed = 0;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004661
4662 // Add up all the space actually used.
4663 for (unsigned i = 0; i != NumOps; ++i) {
4664 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4665 EVT ArgVT = Outs[i].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004666 EVT OrigVT = Outs[i].ArgVT;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004667
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004668 if (CallConv == CallingConv::Fast) {
4669 if (Flags.isByVal())
4670 NumGPRsUsed += (Flags.getByValSize()+7)/8;
4671 else
4672 switch (ArgVT.getSimpleVT().SimpleTy) {
4673 default: llvm_unreachable("Unexpected ValueType for argument!");
4674 case MVT::i1:
4675 case MVT::i32:
4676 case MVT::i64:
4677 if (++NumGPRsUsed <= NumGPRs)
4678 continue;
4679 break;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004680 case MVT::v4i32:
4681 case MVT::v8i16:
4682 case MVT::v16i8:
4683 case MVT::v2f64:
4684 case MVT::v2i64:
Kit Bartond4eb73c2015-05-05 16:10:44 +00004685 case MVT::v1i128:
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004686 if (++NumVRsUsed <= NumVRs)
4687 continue;
4688 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00004689 case MVT::v4f32:
4690 // When using QPX, this is handled like a FP register, otherwise, it
4691 // is an Altivec register.
4692 if (Subtarget.hasQPX()) {
4693 if (++NumFPRsUsed <= NumFPRs)
4694 continue;
4695 } else {
4696 if (++NumVRsUsed <= NumVRs)
4697 continue;
4698 }
4699 break;
4700 case MVT::f32:
4701 case MVT::f64:
4702 case MVT::v4f64: // QPX
4703 case MVT::v4i1: // QPX
4704 if (++NumFPRsUsed <= NumFPRs)
4705 continue;
4706 break;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004707 }
4708 }
4709
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004710 /* Respect alignment of argument on the stack. */
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004711 unsigned Align =
4712 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004713 NumBytes = ((NumBytes + Align - 1) / Align) * Align;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004714
4715 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004716 if (Flags.isInConsecutiveRegsLast())
4717 NumBytes = ((NumBytes + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004718 }
4719
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004720 unsigned NumBytesActuallyUsed = NumBytes;
4721
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004722 // The prolog code of the callee may store up to 8 GPR argument registers to
4723 // the stack, allowing va_start to index over them in memory if its varargs.
4724 // Because we cannot tell if this is needed on the caller side, we have to
4725 // conservatively assume that it is needed. As such, make sure we have at
4726 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00004727 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004728 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004729
4730 // Tail call needs the stack to be aligned.
4731 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4732 CallConv == CallingConv::Fast)
Eric Christophercccae792015-01-30 22:02:31 +00004733 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004734
4735 // Calculate by how many bytes the stack has to be adjusted in case of tail
4736 // call optimization.
4737 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4738
4739 // To protect arguments on the stack from being clobbered in a tail call,
4740 // force all the loads to happen before doing any other lowering.
4741 if (isTailCall)
4742 Chain = DAG.getStackArgumentTokenFactor(Chain);
4743
4744 // Adjust the stack pointer for the new arguments...
4745 // These operations are automatically eliminated by the prolog/epilog pass
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004746 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00004747 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004748 SDValue CallSeqStart = Chain;
4749
4750 // Load the return address and frame pointer so it can be move somewhere else
4751 // later.
4752 SDValue LROp, FPOp;
4753 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4754 dl);
4755
4756 // Set up a copy of the stack pointer for use loading and storing any
4757 // arguments that may not fit in the registers available for argument
4758 // passing.
4759 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
4760
4761 // Figure out which arguments are going to go in registers, and which in
4762 // memory. Also, if this is a vararg function, floating point operations
4763 // must be stored to our stack, and loaded into integer regs as well, if
4764 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004765 unsigned ArgOffset = LinkageSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004766
4767 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4768 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4769
4770 SmallVector<SDValue, 8> MemOpChains;
4771 for (unsigned i = 0; i != NumOps; ++i) {
4772 SDValue Arg = OutVals[i];
4773 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004774 EVT ArgVT = Outs[i].VT;
4775 EVT OrigVT = Outs[i].ArgVT;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004776
4777 // PtrOff will be used to store the current argument to the stack if a
4778 // register cannot be found for it.
4779 SDValue PtrOff;
4780
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004781 // We re-align the argument offset for each argument, except when using the
4782 // fast calling convention, when we need to make sure we do that only when
4783 // we'll actually use a stack slot.
4784 auto ComputePtrOff = [&]() {
4785 /* Respect alignment of argument on the stack. */
4786 unsigned Align =
4787 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
4788 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004789
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004790 PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType());
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004791
4792 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4793 };
4794
4795 if (CallConv != CallingConv::Fast) {
4796 ComputePtrOff();
4797
4798 /* Compute GPR index associated with argument offset. */
4799 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
4800 GPR_idx = std::min(GPR_idx, NumGPRs);
4801 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004802
4803 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00004804 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004805 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4806 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4807 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
4808 }
4809
4810 // FIXME memcpy is used way more than necessary. Correctness first.
4811 // Note: "by value" is code for passing a structure by value, not
4812 // basic types.
4813 if (Flags.isByVal()) {
4814 // Note: Size includes alignment padding, so
4815 // struct x { short a; char b; }
4816 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
4817 // These are the proper values we need for right-justifying the
4818 // aggregate in a parameter register.
4819 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00004820
4821 // An empty aggregate parameter takes up no storage and no
4822 // registers.
4823 if (Size == 0)
4824 continue;
4825
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004826 if (CallConv == CallingConv::Fast)
4827 ComputePtrOff();
4828
Bill Schmidt57d6de52012-10-23 15:51:16 +00004829 // All aggregates smaller than 8 bytes must be passed right-justified.
4830 if (Size==1 || Size==2 || Size==4) {
4831 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
4832 if (GPR_idx != NumGPRs) {
4833 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
4834 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004835 false, false, false, 0);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004836 MemOpChains.push_back(Load.getValue(1));
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004837 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004838
4839 ArgOffset += PtrByteSize;
4840 continue;
4841 }
4842 }
4843
4844 if (GPR_idx == NumGPRs && Size < 8) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004845 SDValue AddPtr = PtrOff;
4846 if (!isLittleEndian) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004847 SDValue Const = DAG.getConstant(PtrByteSize - Size, dl,
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004848 PtrOff.getValueType());
4849 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4850 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004851 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4852 CallSeqStart,
4853 Flags, DAG, dl);
4854 ArgOffset += PtrByteSize;
4855 continue;
4856 }
4857 // Copy entire object into memory. There are cases where gcc-generated
4858 // code assumes it is there, even if it could be put entirely into
4859 // registers. (This is not what the doc says.)
4860
4861 // FIXME: The above statement is likely due to a misunderstanding of the
4862 // documents. All arguments must be copied into the parameter area BY
4863 // THE CALLEE in the event that the callee takes the address of any
4864 // formal argument. That has not yet been implemented. However, it is
4865 // reasonable to use the stack area as a staging area for the register
4866 // load.
4867
4868 // Skip this for small aggregates, as we will use the same slot for a
4869 // right-justified copy, below.
4870 if (Size >= 8)
4871 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4872 CallSeqStart,
4873 Flags, DAG, dl);
4874
4875 // When a register is available, pass a small aggregate right-justified.
4876 if (Size < 8 && GPR_idx != NumGPRs) {
4877 // The easiest way to get this right-justified in a register
4878 // is to copy the structure into the rightmost portion of a
4879 // local variable slot, then load the whole slot into the
4880 // register.
4881 // FIXME: The memcpy seems to produce pretty awful code for
4882 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00004883 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00004884 // parameter save area instead of a new local variable.
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004885 SDValue AddPtr = PtrOff;
4886 if (!isLittleEndian) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004887 SDValue Const = DAG.getConstant(8 - Size, dl, PtrOff.getValueType());
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004888 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4889 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004890 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4891 CallSeqStart,
4892 Flags, DAG, dl);
4893
4894 // Load the slot into the register.
4895 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
4896 MachinePointerInfo(),
4897 false, false, false, 0);
4898 MemOpChains.push_back(Load.getValue(1));
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004899 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004900
4901 // Done with this argument.
4902 ArgOffset += PtrByteSize;
4903 continue;
4904 }
4905
4906 // For aggregates larger than PtrByteSize, copy the pieces of the
4907 // object that fit into registers from the parameter save area.
4908 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004909 SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType());
Bill Schmidt57d6de52012-10-23 15:51:16 +00004910 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
4911 if (GPR_idx != NumGPRs) {
4912 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4913 MachinePointerInfo(),
4914 false, false, false, 0);
4915 MemOpChains.push_back(Load.getValue(1));
4916 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4917 ArgOffset += PtrByteSize;
4918 } else {
4919 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4920 break;
4921 }
4922 }
4923 continue;
4924 }
4925
Craig Topper56710102013-08-15 02:33:50 +00004926 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004927 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00004928 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004929 case MVT::i32:
4930 case MVT::i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004931 // These can be scalar arguments or elements of an integer array type
4932 // passed directly. Clang may use those instead of "byval" aggregate
4933 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidt57d6de52012-10-23 15:51:16 +00004934 if (GPR_idx != NumGPRs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004935 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004936 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004937 if (CallConv == CallingConv::Fast)
4938 ComputePtrOff();
4939
Bill Schmidt57d6de52012-10-23 15:51:16 +00004940 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4941 true, isTailCall, false, MemOpChains,
4942 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004943 if (CallConv == CallingConv::Fast)
4944 ArgOffset += PtrByteSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004945 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004946 if (CallConv != CallingConv::Fast)
4947 ArgOffset += PtrByteSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004948 break;
4949 case MVT::f32:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004950 case MVT::f64: {
4951 // These can be scalar arguments or elements of a float array type
4952 // passed directly. The latter are used to implement ELFv2 homogenous
4953 // float aggregates.
4954
4955 // Named arguments go into FPRs first, and once they overflow, the
4956 // remaining arguments go into GPRs and then the parameter save area.
4957 // Unnamed arguments for vararg functions always go to GPRs and
4958 // then the parameter save area. For now, put all arguments to vararg
4959 // routines always in both locations (FPR *and* GPR or stack slot).
4960 bool NeedGPROrStack = isVarArg || FPR_idx == NumFPRs;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004961 bool NeededLoad = false;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004962
4963 // First load the argument into the next available FPR.
4964 if (FPR_idx != NumFPRs)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004965 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4966
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004967 // Next, load the argument into GPR or stack slot if needed.
4968 if (!NeedGPROrStack)
4969 ;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004970 else if (GPR_idx != NumGPRs && CallConv != CallingConv::Fast) {
Hal Finkel8ea446b2015-01-18 14:31:10 +00004971 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8
4972 // once we support fp <-> gpr moves.
4973
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004974 // In the non-vararg case, this can only ever happen in the
4975 // presence of f32 array types, since otherwise we never run
4976 // out of FPRs before running out of GPRs.
4977 SDValue ArgVal;
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004978
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004979 // Double values are always passed in a single GPR.
4980 if (Arg.getValueType() != MVT::f32) {
4981 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004982
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004983 // Non-array float values are extended and passed in a GPR.
4984 } else if (!Flags.isInConsecutiveRegs()) {
4985 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4986 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
4987
4988 // If we have an array of floats, we collect every odd element
4989 // together with its predecessor into one GPR.
4990 } else if (ArgOffset % PtrByteSize != 0) {
4991 SDValue Lo, Hi;
4992 Lo = DAG.getNode(ISD::BITCAST, dl, MVT::i32, OutVals[i - 1]);
4993 Hi = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4994 if (!isLittleEndian)
4995 std::swap(Lo, Hi);
4996 ArgVal = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
4997
4998 // The final element, if even, goes into the first half of a GPR.
4999 } else if (Flags.isInConsecutiveRegsLast()) {
5000 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
5001 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
5002 if (!isLittleEndian)
5003 ArgVal = DAG.getNode(ISD::SHL, dl, MVT::i64, ArgVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005004 DAG.getConstant(32, dl, MVT::i32));
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005005
5006 // Non-final even elements are skipped; they will be handled
5007 // together the with subsequent argument on the next go-around.
5008 } else
5009 ArgVal = SDValue();
5010
5011 if (ArgVal.getNode())
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005012 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], ArgVal));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005013 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005014 if (CallConv == CallingConv::Fast)
5015 ComputePtrOff();
5016
Bill Schmidt57d6de52012-10-23 15:51:16 +00005017 // Single-precision floating-point values are mapped to the
5018 // second (rightmost) word of the stack doubleword.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005019 if (Arg.getValueType() == MVT::f32 &&
5020 !isLittleEndian && !Flags.isInConsecutiveRegs()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005021 SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType());
Bill Schmidt57d6de52012-10-23 15:51:16 +00005022 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
5023 }
5024
5025 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5026 true, isTailCall, false, MemOpChains,
5027 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005028
5029 NeededLoad = true;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005030 }
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005031 // When passing an array of floats, the array occupies consecutive
5032 // space in the argument area; only round up to the next doubleword
5033 // at the end of the array. Otherwise, each float takes 8 bytes.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005034 if (CallConv != CallingConv::Fast || NeededLoad) {
5035 ArgOffset += (Arg.getValueType() == MVT::f32 &&
5036 Flags.isInConsecutiveRegs()) ? 4 : 8;
5037 if (Flags.isInConsecutiveRegsLast())
5038 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
5039 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005040 break;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005041 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005042 case MVT::v4f32:
5043 case MVT::v4i32:
5044 case MVT::v8i16:
5045 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00005046 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00005047 case MVT::v2i64:
Kit Bartond4eb73c2015-05-05 16:10:44 +00005048 case MVT::v1i128:
Hal Finkelc93a9a22015-02-25 01:06:45 +00005049 if (!Subtarget.hasQPX()) {
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005050 // These can be scalar arguments or elements of a vector array type
5051 // passed directly. The latter are used to implement ELFv2 homogenous
5052 // vector aggregates.
5053
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00005054 // For a varargs call, named arguments go into VRs or on the stack as
5055 // usual; unnamed arguments always go to the stack or the corresponding
5056 // GPRs when within range. For now, we always put the value in both
5057 // locations (or even all three).
Bill Schmidt57d6de52012-10-23 15:51:16 +00005058 if (isVarArg) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005059 // We could elide this store in the case where the object fits
5060 // entirely in R registers. Maybe later.
Bill Schmidt57d6de52012-10-23 15:51:16 +00005061 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5062 MachinePointerInfo(), false, false, 0);
5063 MemOpChains.push_back(Store);
5064 if (VR_idx != NumVRs) {
5065 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
5066 MachinePointerInfo(),
5067 false, false, false, 0);
5068 MemOpChains.push_back(Load.getValue(1));
Hal Finkel7811c612014-03-28 19:58:11 +00005069
5070 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
5071 Arg.getSimpleValueType() == MVT::v2i64) ?
5072 VSRH[VR_idx] : VR[VR_idx];
5073 ++VR_idx;
5074
5075 RegsToPass.push_back(std::make_pair(VReg, Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005076 }
5077 ArgOffset += 16;
5078 for (unsigned i=0; i<16; i+=PtrByteSize) {
5079 if (GPR_idx == NumGPRs)
5080 break;
5081 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005082 DAG.getConstant(i, dl, PtrVT));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005083 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
5084 false, false, false, 0);
5085 MemOpChains.push_back(Load.getValue(1));
5086 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5087 }
5088 break;
5089 }
5090
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00005091 // Non-varargs Altivec params go into VRs or on the stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00005092 if (VR_idx != NumVRs) {
Hal Finkel7811c612014-03-28 19:58:11 +00005093 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
5094 Arg.getSimpleValueType() == MVT::v2i64) ?
5095 VSRH[VR_idx] : VR[VR_idx];
5096 ++VR_idx;
5097
5098 RegsToPass.push_back(std::make_pair(VReg, Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005099 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005100 if (CallConv == CallingConv::Fast)
5101 ComputePtrOff();
5102
Bill Schmidt57d6de52012-10-23 15:51:16 +00005103 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5104 true, isTailCall, true, MemOpChains,
5105 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005106 if (CallConv == CallingConv::Fast)
5107 ArgOffset += 16;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005108 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005109
5110 if (CallConv != CallingConv::Fast)
5111 ArgOffset += 16;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005112 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00005113 } // not QPX
5114
5115 assert(Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32 &&
5116 "Invalid QPX parameter type");
5117
5118 /* fall through */
5119 case MVT::v4f64:
5120 case MVT::v4i1: {
5121 bool IsF32 = Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32;
5122 if (isVarArg) {
5123 // We could elide this store in the case where the object fits
5124 // entirely in R registers. Maybe later.
5125 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5126 MachinePointerInfo(), false, false, 0);
5127 MemOpChains.push_back(Store);
5128 if (QFPR_idx != NumQFPRs) {
5129 SDValue Load = DAG.getLoad(IsF32 ? MVT::v4f32 : MVT::v4f64, dl,
5130 Store, PtrOff, MachinePointerInfo(),
5131 false, false, false, 0);
5132 MemOpChains.push_back(Load.getValue(1));
5133 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Load));
5134 }
5135 ArgOffset += (IsF32 ? 16 : 32);
Aaron Ballman70c27de2015-02-25 13:02:23 +00005136 for (unsigned i = 0; i < (IsF32 ? 16U : 32U); i += PtrByteSize) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00005137 if (GPR_idx == NumGPRs)
5138 break;
5139 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005140 DAG.getConstant(i, dl, PtrVT));
Hal Finkelc93a9a22015-02-25 01:06:45 +00005141 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
5142 false, false, false, 0);
5143 MemOpChains.push_back(Load.getValue(1));
5144 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5145 }
5146 break;
5147 }
5148
5149 // Non-varargs QPX params go into registers or on the stack.
5150 if (QFPR_idx != NumQFPRs) {
5151 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Arg));
5152 } else {
5153 if (CallConv == CallingConv::Fast)
5154 ComputePtrOff();
5155
5156 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5157 true, isTailCall, true, MemOpChains,
5158 TailCallArguments, dl);
5159 if (CallConv == CallingConv::Fast)
5160 ArgOffset += (IsF32 ? 16 : 32);
5161 }
5162
5163 if (CallConv != CallingConv::Fast)
5164 ArgOffset += (IsF32 ? 16 : 32);
5165 break;
5166 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005167 }
5168 }
5169
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005170 assert(NumBytesActuallyUsed == ArgOffset);
Ulrich Weigandde8641b2014-07-07 19:39:44 +00005171 (void)NumBytesActuallyUsed;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005172
Bill Schmidt57d6de52012-10-23 15:51:16 +00005173 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00005174 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005175
5176 // Check if this is an indirect call (MTCTR/BCTRL).
5177 // See PrepareCall() for more information about calls through function
5178 // pointers in the 64-bit SVR4 ABI.
Hal Finkel934361a2015-01-14 01:07:51 +00005179 if (!isTailCall && !IsPatchPoint &&
Hal Finkel87deb0b2015-01-12 04:34:47 +00005180 !isFunctionGlobalAddress(Callee) &&
5181 !isa<ExternalSymbolSDNode>(Callee)) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005182 // Load r2 into a virtual register and store it to the TOC save area.
Hal Finkele6698d52015-02-01 15:03:28 +00005183 setUsesTOCBasePtr(DAG);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005184 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
5185 // TOC save area offset.
Eric Christopher736d39e2015-02-13 00:39:36 +00005186 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005187 SDValue PtrOff = DAG.getIntPtrConstant(TOCSaveOffset, dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005188 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Hal Finkele2ab0f12015-01-15 21:17:34 +00005189 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr,
5190 MachinePointerInfo::getStack(TOCSaveOffset),
Bill Schmidt57d6de52012-10-23 15:51:16 +00005191 false, false, 0);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00005192 // In the ELFv2 ABI, R12 must contain the address of an indirect callee.
5193 // This does not mean the MTCTR instruction must use R12; it's easier
5194 // to model this as an extra parameter, so do that.
Hal Finkel934361a2015-01-14 01:07:51 +00005195 if (isELFv2ABI && !IsPatchPoint)
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00005196 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005197 }
5198
5199 // Build a sequence of copy-to-reg nodes chained together with token chain
5200 // and flag operands which copy the outgoing args into the appropriate regs.
5201 SDValue InFlag;
5202 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
5203 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
5204 RegsToPass[i].second, InFlag);
5205 InFlag = Chain.getValue(1);
5206 }
5207
5208 if (isTailCall)
5209 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
5210 FPOp, true, TailCallArguments);
5211
Hal Finkel934361a2015-01-14 01:07:51 +00005212 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint, DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00005213 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff,
5214 NumBytes, Ins, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005215}
5216
5217SDValue
5218PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
5219 CallingConv::ID CallConv, bool isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00005220 bool isTailCall, bool IsPatchPoint,
Bill Schmidt57d6de52012-10-23 15:51:16 +00005221 const SmallVectorImpl<ISD::OutputArg> &Outs,
5222 const SmallVectorImpl<SDValue> &OutVals,
5223 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005224 SDLoc dl, SelectionDAG &DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00005225 SmallVectorImpl<SDValue> &InVals,
5226 ImmutableCallSite *CS) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005227
5228 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005229
Owen Anderson53aa7a92009-08-10 22:56:29 +00005230 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00005231 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005232 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005233
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005234 MachineFunction &MF = DAG.getMachineFunction();
5235
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005236 // Mark this function as potentially containing a function that contains a
5237 // tail call. As a consequence the frame pointer will be used for dynamicalloc
5238 // and restoring the callers stack pointer in this functions epilog. This is
5239 // done because by tail calling the called function might overwrite the value
5240 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00005241 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5242 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005243 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
5244
Chris Lattneraa40ec12006-05-16 22:56:08 +00005245 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00005246 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00005247 // prereserved space for [SP][CR][LR][3 x unused].
Eric Christophera4ae2132015-02-13 22:22:57 +00005248 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005249 unsigned NumBytes = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005250
5251 // Add up all the space actually used.
5252 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
5253 // they all go in registers, but we must reserve stack space for them for
5254 // possible use by the caller. In varargs or 64-bit calls, parameters are
5255 // assigned stack space in order, with padding so Altivec parameters are
5256 // 16-byte aligned.
5257 unsigned nAltivecParamsAtEnd = 0;
5258 for (unsigned i = 0; i != NumOps; ++i) {
5259 ISD::ArgFlagsTy Flags = Outs[i].Flags;
5260 EVT ArgVT = Outs[i].VT;
5261 // Varargs Altivec parameters are padded to a 16 byte boundary.
5262 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
5263 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
5264 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64) {
5265 if (!isVarArg && !isPPC64) {
5266 // Non-varargs Altivec parameters go after all the non-Altivec
5267 // parameters; handle those later so we know how much padding we need.
5268 nAltivecParamsAtEnd++;
5269 continue;
5270 }
5271 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
5272 NumBytes = ((NumBytes+15)/16)*16;
5273 }
5274 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
5275 }
5276
5277 // Allow for Altivec parameters at the end, if needed.
5278 if (nAltivecParamsAtEnd) {
5279 NumBytes = ((NumBytes+15)/16)*16;
5280 NumBytes += 16*nAltivecParamsAtEnd;
5281 }
5282
5283 // The prolog code of the callee may store up to 8 GPR argument registers to
5284 // the stack, allowing va_start to index over them in memory if its varargs.
5285 // Because we cannot tell if this is needed on the caller side, we have to
5286 // conservatively assume that it is needed. As such, make sure we have at
5287 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005288 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005289
5290 // Tail call needs the stack to be aligned.
5291 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5292 CallConv == CallingConv::Fast)
Eric Christophercccae792015-01-30 22:02:31 +00005293 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005294
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005295 // Calculate by how many bytes the stack has to be adjusted in case of tail
5296 // call optimization.
5297 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005298
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005299 // To protect arguments on the stack from being clobbered in a tail call,
5300 // force all the loads to happen before doing any other lowering.
5301 if (isTailCall)
5302 Chain = DAG.getStackArgumentTokenFactor(Chain);
5303
Chris Lattnerb7552a82006-05-17 00:15:40 +00005304 // Adjust the stack pointer for the new arguments...
5305 // These operations are automatically eliminated by the prolog/epilog pass
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005306 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00005307 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005308 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005309
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005310 // Load the return address and frame pointer so it can be move somewhere else
5311 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005312 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00005313 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
5314 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005315
Chris Lattnerb7552a82006-05-17 00:15:40 +00005316 // Set up a copy of the stack pointer for use loading and storing any
5317 // arguments that may not fit in the registers available for argument
5318 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005319 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005320 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00005321 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005322 else
Owen Anderson9f944592009-08-11 20:47:22 +00005323 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005324
Chris Lattnerb7552a82006-05-17 00:15:40 +00005325 // Figure out which arguments are going to go in registers, and which in
5326 // memory. Also, if this is a vararg function, floating point operations
5327 // must be stored to our stack, and loaded into integer regs as well, if
5328 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005329 unsigned ArgOffset = LinkageSize;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005330 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005331
Craig Topper840beec2014-04-04 05:16:06 +00005332 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005333 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
5334 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
5335 };
Craig Topper840beec2014-04-04 05:16:06 +00005336 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00005337 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
5338 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
5339 };
Craig Topper840beec2014-04-04 05:16:06 +00005340 static const MCPhysReg VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005341 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
5342 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
5343 };
Owen Andersone2f23a32007-09-07 04:06:50 +00005344 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005345 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00005346 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005347
Craig Topper840beec2014-04-04 05:16:06 +00005348 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005349
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005350 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005351 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
5352
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005353 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00005354 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005355 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005356 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00005357
Chris Lattnerb7552a82006-05-17 00:15:40 +00005358 // PtrOff will be used to store the current argument to the stack if a
5359 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005360 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005361
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005362 PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00005363
Dale Johannesen679073b2009-02-04 02:34:38 +00005364 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005365
5366 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00005367 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00005368 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
5369 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00005370 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005371 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00005372
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005373 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00005374 // Note: "by value" is code for passing a structure by value, not
5375 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00005376 if (Flags.isByVal()) {
5377 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00005378 // Very small objects are passed right-justified. Everything else is
5379 // passed left-justified.
5380 if (Size==1 || Size==2) {
5381 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005382 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00005383 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00005384 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00005385 false, false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005386 MemOpChains.push_back(Load.getValue(1));
5387 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005388
5389 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005390 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005391 SDValue Const = DAG.getConstant(PtrByteSize - Size, dl,
Bill Schmidt48081ca2012-10-16 13:30:53 +00005392 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005393 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005394 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
5395 CallSeqStart,
5396 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005397 ArgOffset += PtrByteSize;
5398 }
5399 continue;
5400 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00005401 // Copy entire object into memory. There are cases where gcc-generated
5402 // code assumes it is there, even if it could be put entirely into
5403 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00005404 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
5405 CallSeqStart,
5406 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00005407
5408 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
5409 // copy the pieces of the object that fit into registers from the
5410 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00005411 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005412 SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005413 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00005414 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00005415 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
5416 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005417 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00005418 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00005419 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005420 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00005421 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00005422 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005423 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00005424 }
5425 }
5426 continue;
5427 }
5428
Craig Topper56710102013-08-15 02:33:50 +00005429 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005430 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00005431 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00005432 case MVT::i32:
5433 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005434 if (GPR_idx != NumGPRs) {
Hal Finkel7f908e82014-03-06 00:45:19 +00005435 if (Arg.getValueType() == MVT::i1)
5436 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg);
5437
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005438 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00005439 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005440 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5441 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005442 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00005443 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005444 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00005445 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005446 case MVT::f32:
5447 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005448 if (FPR_idx != NumFPRs) {
5449 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
5450
Chris Lattnerb7552a82006-05-17 00:15:40 +00005451 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00005452 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5453 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005454 MemOpChains.push_back(Store);
5455
Chris Lattnerb7552a82006-05-17 00:15:40 +00005456 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005457 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00005458 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00005459 MachinePointerInfo(), false, false,
5460 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005461 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005462 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00005463 }
Owen Anderson9f944592009-08-11 20:47:22 +00005464 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005465 SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005466 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00005467 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
5468 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005469 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005470 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005471 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00005472 }
5473 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00005474 // If we have any FPRs remaining, we may also have GPRs remaining.
5475 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
5476 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005477 if (GPR_idx != NumGPRs)
5478 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00005479 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005480 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
5481 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00005482 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005483 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005484 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5485 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005486 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005487 if (isPPC64)
5488 ArgOffset += 8;
5489 else
Owen Anderson9f944592009-08-11 20:47:22 +00005490 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00005491 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005492 case MVT::v4f32:
5493 case MVT::v4i32:
5494 case MVT::v8i16:
5495 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00005496 if (isVarArg) {
5497 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00005498 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00005499 // V registers; in fact gcc does this only for arguments that are
5500 // prototyped, not for those that match the ... We do it for all
5501 // arguments, seems to work.
5502 while (ArgOffset % 16 !=0) {
5503 ArgOffset += PtrByteSize;
5504 if (GPR_idx != NumGPRs)
5505 GPR_idx++;
5506 }
5507 // We could elide this store in the case where the object fits
5508 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005509 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005510 DAG.getConstant(ArgOffset, dl, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00005511 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5512 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005513 MemOpChains.push_back(Store);
5514 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00005515 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00005516 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005517 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005518 MemOpChains.push_back(Load.getValue(1));
5519 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
5520 }
5521 ArgOffset += 16;
5522 for (unsigned i=0; i<16; i+=PtrByteSize) {
5523 if (GPR_idx == NumGPRs)
5524 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00005525 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005526 DAG.getConstant(i, dl, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00005527 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005528 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005529 MemOpChains.push_back(Load.getValue(1));
5530 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5531 }
5532 break;
5533 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005534
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005535 // Non-varargs Altivec params generally go in registers, but have
5536 // stack space allocated at the end.
5537 if (VR_idx != NumVRs) {
5538 // Doesn't have GPR space allocated.
5539 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
5540 } else if (nAltivecParamsAtEnd==0) {
5541 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005542 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5543 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005544 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005545 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00005546 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00005547 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00005548 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00005549 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005550 // If all Altivec parameters fit in registers, as they usually do,
5551 // they get stack space following the non-Altivec parameters. We
5552 // don't track this here because nobody below needs it.
5553 // If there are more Altivec parameters than fit in registers emit
5554 // the stores here.
5555 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
5556 unsigned j = 0;
5557 // Offset is aligned; skip 1st 12 params which go in V registers.
5558 ArgOffset = ((ArgOffset+15)/16)*16;
5559 ArgOffset += 12*16;
5560 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005561 SDValue Arg = OutVals[i];
5562 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00005563 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
5564 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005565 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005566 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005567 // We are emitting Altivec params in order.
5568 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5569 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005570 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005571 ArgOffset += 16;
5572 }
5573 }
5574 }
5575 }
5576
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005577 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00005578 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005579
Dale Johannesen90eab672010-03-09 20:15:42 +00005580 // On Darwin, R12 must contain the address of an indirect callee. This does
5581 // not mean the MTCTR instruction must use R12; it's easier to model this as
5582 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00005583 if (!isTailCall &&
Hal Finkel87deb0b2015-01-12 04:34:47 +00005584 !isFunctionGlobalAddress(Callee) &&
5585 !isa<ExternalSymbolSDNode>(Callee) &&
Dale Johannesen90eab672010-03-09 20:15:42 +00005586 !isBLACompatibleAddress(Callee, DAG))
5587 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
5588 PPC::R12), Callee));
5589
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005590 // Build a sequence of copy-to-reg nodes chained together with token chain
5591 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005592 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005593 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00005594 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00005595 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005596 InFlag = Chain.getValue(1);
5597 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005598
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00005599 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005600 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
5601 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005602
Hal Finkel934361a2015-01-14 01:07:51 +00005603 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint, DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00005604 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff,
5605 NumBytes, Ins, InVals, CS);
Chris Lattneraa40ec12006-05-16 22:56:08 +00005606}
5607
Hal Finkel450128a2011-10-14 19:51:36 +00005608bool
5609PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
5610 MachineFunction &MF, bool isVarArg,
5611 const SmallVectorImpl<ISD::OutputArg> &Outs,
5612 LLVMContext &Context) const {
5613 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00005614 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Hal Finkel450128a2011-10-14 19:51:36 +00005615 return CCInfo.CheckReturn(Outs, RetCC_PPC);
5616}
5617
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005618SDValue
5619PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00005620 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005621 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005622 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005623 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005624
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005625 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00005626 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
5627 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005628 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005629
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005630 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005631 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005632
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005633 // Copy the result values into the output registers.
5634 for (unsigned i = 0; i != RVLocs.size(); ++i) {
5635 CCValAssign &VA = RVLocs[i];
5636 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00005637
5638 SDValue Arg = OutVals[i];
5639
5640 switch (VA.getLocInfo()) {
5641 default: llvm_unreachable("Unknown loc info!");
5642 case CCValAssign::Full: break;
5643 case CCValAssign::AExt:
5644 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
5645 break;
5646 case CCValAssign::ZExt:
5647 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
5648 break;
5649 case CCValAssign::SExt:
5650 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
5651 break;
5652 }
5653
5654 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005655 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005656 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005657 }
5658
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005659 RetOps[0] = Chain; // Update chain.
5660
5661 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005662 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005663 RetOps.push_back(Flag);
5664
Craig Topper48d114b2014-04-26 18:35:24 +00005665 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps);
Chris Lattner4211ca92006-04-14 06:01:58 +00005666}
5667
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005668SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005669 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00005670 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005671 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005672
Jim Laskeye4f4d042006-12-04 22:04:42 +00005673 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005674 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00005675
5676 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00005677 bool isPPC64 = Subtarget.isPPC64();
5678 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005679 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00005680
5681 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005682 SDValue Chain = Op.getOperand(0);
5683 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005684
Jim Laskeye4f4d042006-12-04 22:04:42 +00005685 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00005686 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
5687 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005688 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005689
Jim Laskeye4f4d042006-12-04 22:04:42 +00005690 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00005691 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005692
Jim Laskeye4f4d042006-12-04 22:04:42 +00005693 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00005694 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005695 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00005696}
5697
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005698
5699
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005700SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005701PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00005702 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005703 bool isPPC64 = Subtarget.isPPC64();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005704 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005705
5706 // Get current frame pointer save index. The users of this index will be
5707 // primarily DYNALLOC instructions.
5708 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
5709 int RASI = FI->getReturnAddrSaveIndex();
5710
5711 // If the frame pointer save index hasn't been defined yet.
5712 if (!RASI) {
5713 // Find out what the fix offset of the frame pointer save area.
Eric Christopherf71609b2015-02-13 00:39:27 +00005714 int LROffset = Subtarget.getFrameLowering()->getReturnSaveOffset();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005715 // Allocate the frame index for frame pointer save area.
Hal Finkel6e27c6d2014-12-23 09:45:06 +00005716 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, false);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005717 // Save the result.
5718 FI->setReturnAddrSaveIndex(RASI);
5719 }
5720 return DAG.getFrameIndex(RASI, PtrVT);
5721}
5722
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005723SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005724PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
5725 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005726 bool isPPC64 = Subtarget.isPPC64();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005727 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00005728
5729 // Get current frame pointer save index. The users of this index will be
5730 // primarily DYNALLOC instructions.
5731 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
5732 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005733
Jim Laskey48850c12006-11-16 22:43:37 +00005734 // If the frame pointer save index hasn't been defined yet.
5735 if (!FPSI) {
5736 // Find out what the fix offset of the frame pointer save area.
Eric Christopherdc3a8a42015-02-13 00:39:38 +00005737 int FPOffset = Subtarget.getFrameLowering()->getFramePointerSaveOffset();
Jim Laskey48850c12006-11-16 22:43:37 +00005738 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00005739 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00005740 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005741 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00005742 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005743 return DAG.getFrameIndex(FPSI, PtrVT);
5744}
Jim Laskey48850c12006-11-16 22:43:37 +00005745
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005746SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005747 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005748 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00005749 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005750 SDValue Chain = Op.getOperand(0);
5751 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005752 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005753
Jim Laskey48850c12006-11-16 22:43:37 +00005754 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005755 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00005756 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005757 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005758 DAG.getConstant(0, dl, PtrVT), Size);
Jim Laskey48850c12006-11-16 22:43:37 +00005759 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005760 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00005761 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005762 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00005763 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Craig Topper48d114b2014-04-26 18:35:24 +00005764 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops);
Jim Laskey48850c12006-11-16 22:43:37 +00005765}
5766
Hal Finkel756810f2013-03-21 21:37:52 +00005767SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
5768 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005769 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00005770 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
5771 DAG.getVTList(MVT::i32, MVT::Other),
5772 Op.getOperand(0), Op.getOperand(1));
5773}
5774
5775SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
5776 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005777 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00005778 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
5779 Op.getOperand(0), Op.getOperand(1));
5780}
5781
Hal Finkel940ab932014-02-28 00:27:01 +00005782SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +00005783 if (Op.getValueType().isVector())
5784 return LowerVectorLoad(Op, DAG);
5785
Hal Finkel940ab932014-02-28 00:27:01 +00005786 assert(Op.getValueType() == MVT::i1 &&
5787 "Custom lowering only for i1 loads");
5788
5789 // First, load 8 bits into 32 bits, then truncate to 1 bit.
5790
5791 SDLoc dl(Op);
5792 LoadSDNode *LD = cast<LoadSDNode>(Op);
5793
5794 SDValue Chain = LD->getChain();
5795 SDValue BasePtr = LD->getBasePtr();
5796 MachineMemOperand *MMO = LD->getMemOperand();
5797
5798 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(), Chain,
5799 BasePtr, MVT::i8, MMO);
5800 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
5801
5802 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00005803 return DAG.getMergeValues(Ops, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00005804}
5805
5806SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +00005807 if (Op.getOperand(1).getValueType().isVector())
5808 return LowerVectorStore(Op, DAG);
5809
Hal Finkel940ab932014-02-28 00:27:01 +00005810 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
5811 "Custom lowering only for i1 stores");
5812
5813 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
5814
5815 SDLoc dl(Op);
5816 StoreSDNode *ST = cast<StoreSDNode>(Op);
5817
5818 SDValue Chain = ST->getChain();
5819 SDValue BasePtr = ST->getBasePtr();
5820 SDValue Value = ST->getValue();
5821 MachineMemOperand *MMO = ST->getMemOperand();
5822
5823 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(), Value);
5824 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
5825}
5826
5827// FIXME: Remove this once the ANDI glue bug is fixed:
5828SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
5829 assert(Op.getValueType() == MVT::i1 &&
5830 "Custom lowering only for i1 results");
5831
5832 SDLoc DL(Op);
5833 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
5834 Op.getOperand(0));
5835}
5836
Chris Lattner4211ca92006-04-14 06:01:58 +00005837/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
5838/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005839SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00005840 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00005841 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
5842 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00005843 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005844
Hal Finkel81f87992013-04-07 22:11:09 +00005845 // We might be able to do better than this under some circumstances, but in
5846 // general, fsel-based lowering of select is a finite-math-only optimization.
5847 // For more information, see section F.3 of the 2.06 ISA specification.
5848 if (!DAG.getTarget().Options.NoInfsFPMath ||
5849 !DAG.getTarget().Options.NoNaNsFPMath)
5850 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005851
Hal Finkel81f87992013-04-07 22:11:09 +00005852 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005853
Owen Anderson53aa7a92009-08-10 22:56:29 +00005854 EVT ResVT = Op.getValueType();
5855 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005856 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
5857 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005858 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005859
Chris Lattner4211ca92006-04-14 06:01:58 +00005860 // If the RHS of the comparison is a 0.0, we don't need to do the
5861 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00005862 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00005863 if (isFloatingPointZero(RHS))
5864 switch (CC) {
5865 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00005866 case ISD::SETNE:
5867 std::swap(TV, FV);
5868 case ISD::SETEQ:
5869 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5870 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
5871 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
5872 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
5873 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
5874 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5875 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005876 case ISD::SETULT:
5877 case ISD::SETLT:
5878 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005879 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005880 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00005881 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5882 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005883 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005884 case ISD::SETUGT:
5885 case ISD::SETGT:
5886 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005887 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005888 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00005889 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5890 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005891 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005892 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005893 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005894
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005895 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00005896 switch (CC) {
5897 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00005898 case ISD::SETNE:
5899 std::swap(TV, FV);
5900 case ISD::SETEQ:
5901 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
5902 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5903 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
5904 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
5905 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
5906 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
5907 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5908 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005909 case ISD::SETULT:
5910 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005911 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005912 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5913 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005914 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005915 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005916 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005917 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005918 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5919 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005920 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005921 case ISD::SETUGT:
5922 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005923 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005924 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5925 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005926 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005927 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005928 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005929 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005930 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5931 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005932 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005933 }
Eli Friedman5806e182009-05-28 04:31:08 +00005934 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00005935}
5936
Hal Finkeled844c42015-01-06 22:31:02 +00005937void PPCTargetLowering::LowerFP_TO_INTForReuse(SDValue Op, ReuseLoadInfo &RLI,
5938 SelectionDAG &DAG,
5939 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00005940 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005941 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00005942 if (Src.getValueType() == MVT::f32)
5943 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00005944
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005945 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00005946 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005947 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00005948 case MVT::i32:
Eric Christophercccae792015-01-30 22:02:31 +00005949 Tmp = DAG.getNode(
5950 Op.getOpcode() == ISD::FP_TO_SINT
5951 ? PPCISD::FCTIWZ
5952 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ),
5953 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005954 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005955 case MVT::i64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005956 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) &&
Hal Finkel3f88d082013-04-01 18:42:58 +00005957 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00005958 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
5959 PPCISD::FCTIDUZ,
5960 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005961 break;
5962 }
Duncan Sands2a287912008-07-19 16:26:02 +00005963
Chris Lattner4211ca92006-04-14 06:01:58 +00005964 // Convert the FP value to an int value through memory.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005965 bool i32Stack = Op.getValueType() == MVT::i32 && Subtarget.hasSTFIWX() &&
5966 (Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT());
Hal Finkelf6d45f22013-04-01 17:52:07 +00005967 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
5968 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
5969 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00005970
Chris Lattner06a49542007-10-15 20:14:52 +00005971 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005972 SDValue Chain;
5973 if (i32Stack) {
5974 MachineFunction &MF = DAG.getMachineFunction();
5975 MachineMemOperand *MMO =
5976 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
5977 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
5978 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00005979 DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005980 } else
5981 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
5982 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00005983
5984 // Result is a load from the stack slot. If loading 4 bytes, make sure to
5985 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005986 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00005987 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005988 DAG.getConstant(4, dl, FIPtr.getValueType()));
Hal Finkeled844c42015-01-06 22:31:02 +00005989 MPI = MPI.getWithOffset(4);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005990 }
5991
Hal Finkeled844c42015-01-06 22:31:02 +00005992 RLI.Chain = Chain;
5993 RLI.Ptr = FIPtr;
5994 RLI.MPI = MPI;
5995}
5996
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00005997/// \brief Custom lowers floating point to integer conversions to use
5998/// the direct move instructions available in ISA 2.07 to avoid the
5999/// need for load/store combinations.
6000SDValue PPCTargetLowering::LowerFP_TO_INTDirectMove(SDValue Op,
6001 SelectionDAG &DAG,
6002 SDLoc dl) const {
6003 assert(Op.getOperand(0).getValueType().isFloatingPoint());
6004 SDValue Src = Op.getOperand(0);
6005
6006 if (Src.getValueType() == MVT::f32)
6007 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
6008
6009 SDValue Tmp;
6010 switch (Op.getSimpleValueType().SimpleTy) {
6011 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
6012 case MVT::i32:
6013 Tmp = DAG.getNode(
6014 Op.getOpcode() == ISD::FP_TO_SINT
6015 ? PPCISD::FCTIWZ
6016 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ),
6017 dl, MVT::f64, Src);
6018 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i32, Tmp);
6019 break;
6020 case MVT::i64:
6021 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) &&
6022 "i64 FP_TO_UINT is supported only with FPCVT");
6023 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
6024 PPCISD::FCTIDUZ,
6025 dl, MVT::f64, Src);
6026 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i64, Tmp);
6027 break;
6028 }
6029 return Tmp;
6030}
6031
Hal Finkeled844c42015-01-06 22:31:02 +00006032SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
6033 SDLoc dl) const {
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006034 if (Subtarget.hasDirectMove() && Subtarget.isPPC64())
6035 return LowerFP_TO_INTDirectMove(Op, DAG, dl);
6036
Hal Finkeled844c42015-01-06 22:31:02 +00006037 ReuseLoadInfo RLI;
6038 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl);
6039
6040 return DAG.getLoad(Op.getValueType(), dl, RLI.Chain, RLI.Ptr, RLI.MPI, false,
6041 false, RLI.IsInvariant, RLI.Alignment, RLI.AAInfo,
6042 RLI.Ranges);
6043}
6044
6045// We're trying to insert a regular store, S, and then a load, L. If the
6046// incoming value, O, is a load, we might just be able to have our load use the
6047// address used by O. However, we don't know if anything else will store to
6048// that address before we can load from it. To prevent this situation, we need
6049// to insert our load, L, into the chain as a peer of O. To do this, we give L
6050// the same chain operand as O, we create a token factor from the chain results
6051// of O and L, and we replace all uses of O's chain result with that token
6052// factor (see spliceIntoChain below for this last part).
6053bool PPCTargetLowering::canReuseLoadAddress(SDValue Op, EVT MemVT,
6054 ReuseLoadInfo &RLI,
Hal Finkel6c392692015-01-09 01:34:30 +00006055 SelectionDAG &DAG,
6056 ISD::LoadExtType ET) const {
Hal Finkeled844c42015-01-06 22:31:02 +00006057 SDLoc dl(Op);
Hal Finkel6c392692015-01-09 01:34:30 +00006058 if (ET == ISD::NON_EXTLOAD &&
6059 (Op.getOpcode() == ISD::FP_TO_UINT ||
Hal Finkeled844c42015-01-06 22:31:02 +00006060 Op.getOpcode() == ISD::FP_TO_SINT) &&
6061 isOperationLegalOrCustom(Op.getOpcode(),
6062 Op.getOperand(0).getValueType())) {
6063
6064 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl);
6065 return true;
6066 }
6067
6068 LoadSDNode *LD = dyn_cast<LoadSDNode>(Op);
Hal Finkel6c392692015-01-09 01:34:30 +00006069 if (!LD || LD->getExtensionType() != ET || LD->isVolatile() ||
6070 LD->isNonTemporal())
Hal Finkeled844c42015-01-06 22:31:02 +00006071 return false;
6072 if (LD->getMemoryVT() != MemVT)
6073 return false;
6074
6075 RLI.Ptr = LD->getBasePtr();
6076 if (LD->isIndexed() && LD->getOffset().getOpcode() != ISD::UNDEF) {
6077 assert(LD->getAddressingMode() == ISD::PRE_INC &&
6078 "Non-pre-inc AM on PPC?");
6079 RLI.Ptr = DAG.getNode(ISD::ADD, dl, RLI.Ptr.getValueType(), RLI.Ptr,
6080 LD->getOffset());
6081 }
6082
6083 RLI.Chain = LD->getChain();
6084 RLI.MPI = LD->getPointerInfo();
6085 RLI.IsInvariant = LD->isInvariant();
6086 RLI.Alignment = LD->getAlignment();
6087 RLI.AAInfo = LD->getAAInfo();
6088 RLI.Ranges = LD->getRanges();
6089
6090 RLI.ResChain = SDValue(LD, LD->isIndexed() ? 2 : 1);
6091 return true;
6092}
6093
6094// Given the head of the old chain, ResChain, insert a token factor containing
6095// it and NewResChain, and make users of ResChain now be users of that token
6096// factor.
6097void PPCTargetLowering::spliceIntoChain(SDValue ResChain,
6098 SDValue NewResChain,
6099 SelectionDAG &DAG) const {
6100 if (!ResChain)
6101 return;
6102
6103 SDLoc dl(NewResChain);
6104
6105 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
6106 NewResChain, DAG.getUNDEF(MVT::Other));
6107 assert(TF.getNode() != NewResChain.getNode() &&
6108 "A new TF really is required here");
6109
6110 DAG.ReplaceAllUsesOfValueWith(ResChain, TF);
6111 DAG.UpdateNodeOperands(TF.getNode(), ResChain, NewResChain);
Chris Lattner4211ca92006-04-14 06:01:58 +00006112}
6113
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006114/// \brief Custom lowers integer to floating point conversions to use
6115/// the direct move instructions available in ISA 2.07 to avoid the
6116/// need for load/store combinations.
6117SDValue PPCTargetLowering::LowerINT_TO_FPDirectMove(SDValue Op,
6118 SelectionDAG &DAG,
6119 SDLoc dl) const {
6120 assert((Op.getValueType() == MVT::f32 ||
6121 Op.getValueType() == MVT::f64) &&
6122 "Invalid floating point type as target of conversion");
6123 assert(Subtarget.hasFPCVT() &&
6124 "Int to FP conversions with direct moves require FPCVT");
6125 SDValue FP;
6126 SDValue Src = Op.getOperand(0);
6127 bool SinglePrec = Op.getValueType() == MVT::f32;
6128 bool WordInt = Src.getSimpleValueType().SimpleTy == MVT::i32;
6129 bool Signed = Op.getOpcode() == ISD::SINT_TO_FP;
6130 unsigned ConvOp = Signed ? (SinglePrec ? PPCISD::FCFIDS : PPCISD::FCFID) :
6131 (SinglePrec ? PPCISD::FCFIDUS : PPCISD::FCFIDU);
6132
6133 if (WordInt) {
6134 FP = DAG.getNode(Signed ? PPCISD::MTVSRA : PPCISD::MTVSRZ,
6135 dl, MVT::f64, Src);
6136 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP);
6137 }
6138 else {
6139 FP = DAG.getNode(PPCISD::MTVSRA, dl, MVT::f64, Src);
6140 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP);
6141 }
6142
6143 return FP;
6144}
6145
Hal Finkelf6d45f22013-04-01 17:52:07 +00006146SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Hal Finkeled844c42015-01-06 22:31:02 +00006147 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006148 SDLoc dl(Op);
Hal Finkelc93a9a22015-02-25 01:06:45 +00006149
6150 if (Subtarget.hasQPX() && Op.getOperand(0).getValueType() == MVT::v4i1) {
6151 if (Op.getValueType() != MVT::v4f32 && Op.getValueType() != MVT::v4f64)
6152 return SDValue();
6153
6154 SDValue Value = Op.getOperand(0);
6155 // The values are now known to be -1 (false) or 1 (true). To convert this
6156 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
6157 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
6158 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
6159
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006160 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::f64);
Hal Finkelc93a9a22015-02-25 01:06:45 +00006161 FPHalfs = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f64,
6162 FPHalfs, FPHalfs, FPHalfs, FPHalfs);
6163
6164 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
6165
6166 if (Op.getValueType() != MVT::v4f64)
6167 Value = DAG.getNode(ISD::FP_ROUND, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006168 Op.getValueType(), Value,
6169 DAG.getIntPtrConstant(1, dl));
Hal Finkelc93a9a22015-02-25 01:06:45 +00006170 return Value;
6171 }
6172
Dan Gohmand6819da2008-03-11 01:59:03 +00006173 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00006174 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006175 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00006176
Hal Finkel6a56b212014-03-05 22:14:00 +00006177 if (Op.getOperand(0).getValueType() == MVT::i1)
6178 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006179 DAG.getConstantFP(1.0, dl, Op.getValueType()),
6180 DAG.getConstantFP(0.0, dl, Op.getValueType()));
Hal Finkel6a56b212014-03-05 22:14:00 +00006181
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006182 // If we have direct moves, we can do all the conversion, skip the store/load
6183 // however, without FPCVT we can't do most conversions.
6184 if (Subtarget.hasDirectMove() && Subtarget.isPPC64() && Subtarget.hasFPCVT())
6185 return LowerINT_TO_FPDirectMove(Op, DAG, dl);
6186
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006187 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006188 "UINT_TO_FP is supported only with FPCVT");
6189
6190 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00006191 // Otherwise, convert to double-precision and then round.
Eric Christophercccae792015-01-30 22:02:31 +00006192 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
6193 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS
6194 : PPCISD::FCFIDS)
6195 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU
6196 : PPCISD::FCFID);
6197 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
6198 ? MVT::f32
6199 : MVT::f64;
Hal Finkelf6d45f22013-04-01 17:52:07 +00006200
Owen Anderson9f944592009-08-11 20:47:22 +00006201 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006202 SDValue SINT = Op.getOperand(0);
6203 // When converting to single-precision, we actually need to convert
6204 // to double-precision first and then round to single-precision.
6205 // To avoid double-rounding effects during that operation, we have
6206 // to prepare the input operand. Bits that might be truncated when
6207 // converting to double-precision are replaced by a bit that won't
6208 // be lost at this stage, but is below the single-precision rounding
6209 // position.
6210 //
6211 // However, if -enable-unsafe-fp-math is in effect, accept double
6212 // rounding to avoid the extra overhead.
6213 if (Op.getValueType() == MVT::f32 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006214 !Subtarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006215 !DAG.getTarget().Options.UnsafeFPMath) {
6216
6217 // Twiddle input to make sure the low 11 bits are zero. (If this
6218 // is the case, we are guaranteed the value will fit into the 53 bit
6219 // mantissa of an IEEE double-precision value without rounding.)
6220 // If any of those low 11 bits were not zero originally, make sure
6221 // bit 12 (value 2048) is set instead, so that the final rounding
6222 // to single-precision gets the correct result.
6223 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006224 SINT, DAG.getConstant(2047, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006225 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006226 Round, DAG.getConstant(2047, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006227 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
6228 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006229 Round, DAG.getConstant(-2048, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006230
6231 // However, we cannot use that value unconditionally: if the magnitude
6232 // of the input value is small, the bit-twiddling we did above might
6233 // end up visibly changing the output. Fortunately, in that case, we
6234 // don't need to twiddle bits since the original input will convert
6235 // exactly to double-precision floating-point already. Therefore,
6236 // construct a conditional to use the original value if the top 11
6237 // bits are all sign-bit copies, and use the rounded value computed
6238 // above otherwise.
6239 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006240 SINT, DAG.getConstant(53, dl, MVT::i32));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006241 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006242 Cond, DAG.getConstant(1, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006243 Cond = DAG.getSetCC(dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006244 Cond, DAG.getConstant(1, dl, MVT::i64), ISD::SETUGT);
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006245
6246 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
6247 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00006248
Hal Finkeled844c42015-01-06 22:31:02 +00006249 ReuseLoadInfo RLI;
6250 SDValue Bits;
6251
Hal Finkel6c392692015-01-09 01:34:30 +00006252 MachineFunction &MF = DAG.getMachineFunction();
Hal Finkeled844c42015-01-06 22:31:02 +00006253 if (canReuseLoadAddress(SINT, MVT::i64, RLI, DAG)) {
6254 Bits = DAG.getLoad(MVT::f64, dl, RLI.Chain, RLI.Ptr, RLI.MPI, false,
6255 false, RLI.IsInvariant, RLI.Alignment, RLI.AAInfo,
6256 RLI.Ranges);
6257 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
Hal Finkel6c392692015-01-09 01:34:30 +00006258 } else if (Subtarget.hasLFIWAX() &&
6259 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::SEXTLOAD)) {
6260 MachineMemOperand *MMO =
6261 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6262 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6263 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6264 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWAX, dl,
6265 DAG.getVTList(MVT::f64, MVT::Other),
6266 Ops, MVT::i32, MMO);
6267 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
6268 } else if (Subtarget.hasFPCVT() &&
6269 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::ZEXTLOAD)) {
6270 MachineMemOperand *MMO =
6271 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6272 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6273 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6274 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWZX, dl,
6275 DAG.getVTList(MVT::f64, MVT::Other),
6276 Ops, MVT::i32, MMO);
6277 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
6278 } else if (((Subtarget.hasLFIWAX() &&
6279 SINT.getOpcode() == ISD::SIGN_EXTEND) ||
6280 (Subtarget.hasFPCVT() &&
6281 SINT.getOpcode() == ISD::ZERO_EXTEND)) &&
6282 SINT.getOperand(0).getValueType() == MVT::i32) {
6283 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
6284 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
6285
6286 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
6287 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
6288
6289 SDValue Store =
6290 DAG.getStore(DAG.getEntryNode(), dl, SINT.getOperand(0), FIdx,
6291 MachinePointerInfo::getFixedStack(FrameIdx),
6292 false, false, 0);
6293
6294 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
6295 "Expected an i32 store");
6296
6297 RLI.Ptr = FIdx;
6298 RLI.Chain = Store;
6299 RLI.MPI = MachinePointerInfo::getFixedStack(FrameIdx);
6300 RLI.Alignment = 4;
6301
6302 MachineMemOperand *MMO =
6303 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6304 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6305 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6306 Bits = DAG.getMemIntrinsicNode(SINT.getOpcode() == ISD::ZERO_EXTEND ?
6307 PPCISD::LFIWZX : PPCISD::LFIWAX,
6308 dl, DAG.getVTList(MVT::f64, MVT::Other),
6309 Ops, MVT::i32, MMO);
Hal Finkeled844c42015-01-06 22:31:02 +00006310 } else
6311 Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
6312
Hal Finkelf6d45f22013-04-01 17:52:07 +00006313 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
6314
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006315 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00006316 FP = DAG.getNode(ISD::FP_ROUND, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006317 MVT::f32, FP, DAG.getIntPtrConstant(0, dl));
Chris Lattner4211ca92006-04-14 06:01:58 +00006318 return FP;
6319 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006320
Owen Anderson9f944592009-08-11 20:47:22 +00006321 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006322 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00006323 // Since we only generate this in 64-bit mode, we can take advantage of
6324 // 64-bit registers. In particular, sign extend the input value into the
6325 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
6326 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00006327 MachineFunction &MF = DAG.getMachineFunction();
6328 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00006329 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006330
Hal Finkelbeb296b2013-03-31 10:12:51 +00006331 SDValue Ld;
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006332 if (Subtarget.hasLFIWAX() || Subtarget.hasFPCVT()) {
Hal Finkeled844c42015-01-06 22:31:02 +00006333 ReuseLoadInfo RLI;
6334 bool ReusingLoad;
6335 if (!(ReusingLoad = canReuseLoadAddress(Op.getOperand(0), MVT::i32, RLI,
6336 DAG))) {
6337 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
6338 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006339
Hal Finkeled844c42015-01-06 22:31:02 +00006340 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
6341 MachinePointerInfo::getFixedStack(FrameIdx),
6342 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00006343
Hal Finkeled844c42015-01-06 22:31:02 +00006344 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
6345 "Expected an i32 store");
6346
6347 RLI.Ptr = FIdx;
6348 RLI.Chain = Store;
6349 RLI.MPI = MachinePointerInfo::getFixedStack(FrameIdx);
6350 RLI.Alignment = 4;
6351 }
6352
Hal Finkelbeb296b2013-03-31 10:12:51 +00006353 MachineMemOperand *MMO =
Hal Finkeled844c42015-01-06 22:31:02 +00006354 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6355 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6356 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
Hal Finkelf6d45f22013-04-01 17:52:07 +00006357 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
6358 PPCISD::LFIWZX : PPCISD::LFIWAX,
6359 dl, DAG.getVTList(MVT::f64, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00006360 Ops, MVT::i32, MMO);
Hal Finkeled844c42015-01-06 22:31:02 +00006361 if (ReusingLoad)
6362 spliceIntoChain(RLI.ResChain, Ld.getValue(1), DAG);
Hal Finkelbeb296b2013-03-31 10:12:51 +00006363 } else {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006364 assert(Subtarget.isPPC64() &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006365 "i32->FP without LFIWAX supported only on PPC64");
6366
Hal Finkelbeb296b2013-03-31 10:12:51 +00006367 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
6368 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
6369
6370 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
6371 Op.getOperand(0));
6372
6373 // STD the extended value into the stack slot.
6374 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
6375 MachinePointerInfo::getFixedStack(FrameIdx),
6376 false, false, 0);
6377
6378 // Load the value as a double.
6379 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
6380 MachinePointerInfo::getFixedStack(FrameIdx),
6381 false, false, false, 0);
6382 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006383
Chris Lattner4211ca92006-04-14 06:01:58 +00006384 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00006385 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006386 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006387 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP,
6388 DAG.getIntPtrConstant(0, dl));
Chris Lattner4211ca92006-04-14 06:01:58 +00006389 return FP;
6390}
6391
Dan Gohman21cea8a2010-04-17 15:26:15 +00006392SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
6393 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006394 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006395 /*
6396 The rounding mode is in bits 30:31 of FPSR, and has the following
6397 settings:
6398 00 Round to nearest
6399 01 Round to 0
6400 10 Round to +inf
6401 11 Round to -inf
6402
6403 FLT_ROUNDS, on the other hand, expects the following:
6404 -1 Undefined
6405 0 Round to 0
6406 1 Round to nearest
6407 2 Round to +inf
6408 3 Round to -inf
6409
6410 To perform the conversion, we do:
6411 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
6412 */
6413
6414 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00006415 EVT VT = Op.getValueType();
6416 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006417
6418 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00006419 EVT NodeTys[] = {
6420 MVT::f64, // return register
6421 MVT::Glue // unused in this context
6422 };
Craig Topper2d2aa0c2014-04-30 07:17:30 +00006423 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, None);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006424
6425 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00006426 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006427 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00006428 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00006429 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006430
6431 // Load FP Control Word from low 32 bits of stack slot.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006432 SDValue Four = DAG.getConstant(4, dl, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00006433 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00006434 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006435 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006436
6437 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006438 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00006439 DAG.getNode(ISD::AND, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006440 CWD, DAG.getConstant(3, dl, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006441 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00006442 DAG.getNode(ISD::SRL, dl, MVT::i32,
6443 DAG.getNode(ISD::AND, dl, MVT::i32,
6444 DAG.getNode(ISD::XOR, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006445 CWD, DAG.getConstant(3, dl, MVT::i32)),
6446 DAG.getConstant(3, dl, MVT::i32)),
6447 DAG.getConstant(1, dl, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006448
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006449 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00006450 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006451
Duncan Sands13237ac2008-06-06 12:08:01 +00006452 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00006453 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006454}
6455
Dan Gohman21cea8a2010-04-17 15:26:15 +00006456SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006457 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006458 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006459 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00006460 assert(Op.getNumOperands() == 3 &&
6461 VT == Op.getOperand(1).getValueType() &&
6462 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006463
Chris Lattner601b8652006-09-20 03:47:40 +00006464 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00006465 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006466 SDValue Lo = Op.getOperand(0);
6467 SDValue Hi = Op.getOperand(1);
6468 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006469 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006470
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006471 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006472 DAG.getConstant(BitWidth, dl, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006473 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
6474 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
6475 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
6476 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006477 DAG.getConstant(-BitWidth, dl, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006478 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
6479 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
6480 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006481 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006482 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006483}
6484
Dan Gohman21cea8a2010-04-17 15:26:15 +00006485SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006486 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006487 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00006488 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00006489 assert(Op.getNumOperands() == 3 &&
6490 VT == Op.getOperand(1).getValueType() &&
6491 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006492
Dan Gohman8d2ead22008-03-07 20:36:53 +00006493 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00006494 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006495 SDValue Lo = Op.getOperand(0);
6496 SDValue Hi = Op.getOperand(1);
6497 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006498 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006499
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006500 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006501 DAG.getConstant(BitWidth, dl, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006502 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
6503 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
6504 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
6505 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006506 DAG.getConstant(-BitWidth, dl, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006507 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
6508 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
6509 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006510 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006511 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006512}
6513
Dan Gohman21cea8a2010-04-17 15:26:15 +00006514SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006515 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006516 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006517 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00006518 assert(Op.getNumOperands() == 3 &&
6519 VT == Op.getOperand(1).getValueType() &&
6520 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006521
Dan Gohman8d2ead22008-03-07 20:36:53 +00006522 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006523 SDValue Lo = Op.getOperand(0);
6524 SDValue Hi = Op.getOperand(1);
6525 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006526 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006527
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006528 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006529 DAG.getConstant(BitWidth, dl, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006530 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
6531 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
6532 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
6533 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006534 DAG.getConstant(-BitWidth, dl, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006535 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
6536 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006537 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, dl, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00006538 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006539 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006540 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006541}
6542
6543//===----------------------------------------------------------------------===//
6544// Vector related lowering.
6545//
6546
Chris Lattner2a099c02006-04-17 06:00:21 +00006547/// BuildSplatI - Build a canonical splati of Val with an element size of
6548/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00006549static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006550 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00006551 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00006552
Benjamin Kramer7149aab2015-03-01 18:09:56 +00006553 static const MVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00006554 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00006555 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00006556
Owen Anderson9f944592009-08-11 20:47:22 +00006557 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00006558
Chris Lattner09ed0ff2006-12-01 01:45:39 +00006559 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
6560 if (Val == -1)
6561 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006562
Owen Anderson53aa7a92009-08-10 22:56:29 +00006563 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00006564
Chris Lattner2a099c02006-04-17 06:00:21 +00006565 // Build a canonical splat for this value.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006566 SDValue Elt = DAG.getConstant(Val, dl, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006567 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00006568 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Craig Topper48d114b2014-04-26 18:35:24 +00006569 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006570 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00006571}
6572
Hal Finkelcf2e9082013-05-24 23:00:14 +00006573/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
6574/// specified intrinsic ID.
6575static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006576 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00006577 EVT DestVT = MVT::Other) {
6578 if (DestVT == MVT::Other) DestVT = Op.getValueType();
6579 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006580 DAG.getConstant(IID, dl, MVT::i32), Op);
Hal Finkelcf2e9082013-05-24 23:00:14 +00006581}
6582
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006583/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00006584/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006585static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006586 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006587 EVT DestVT = MVT::Other) {
6588 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006589 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006590 DAG.getConstant(IID, dl, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00006591}
6592
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006593/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
6594/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006595static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006596 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006597 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00006598 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006599 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006600 DAG.getConstant(IID, dl, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006601}
6602
6603
Chris Lattner264c9082006-04-17 17:55:10 +00006604/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
6605/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006606static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006607 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00006608 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00006609 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
6610 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00006611
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006612 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00006613 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006614 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00006615 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006616 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00006617}
6618
Chris Lattner19e90552006-04-14 05:19:18 +00006619// If this is a case we can't handle, return null and let the default
6620// expansion code take care of it. If we CAN select this case, and if it
6621// selects to a single instruction, return Op. Otherwise, if we can codegen
6622// this case more efficiently than a constant pool load, lower it to the
6623// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00006624SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
6625 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006626 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006627 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
Craig Toppere73658d2014-04-28 04:05:08 +00006628 assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00006629
Hal Finkelc93a9a22015-02-25 01:06:45 +00006630 if (Subtarget.hasQPX() && Op.getValueType() == MVT::v4i1) {
6631 // We first build an i32 vector, load it into a QPX register,
6632 // then convert it to a floating-point vector and compare it
6633 // to a zero vector to get the boolean result.
6634 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6635 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
6636 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FrameIdx);
6637 EVT PtrVT = getPointerTy();
6638 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
6639
6640 assert(BVN->getNumOperands() == 4 &&
6641 "BUILD_VECTOR for v4i1 does not have 4 operands");
6642
6643 bool IsConst = true;
6644 for (unsigned i = 0; i < 4; ++i) {
6645 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF) continue;
6646 if (!isa<ConstantSDNode>(BVN->getOperand(i))) {
6647 IsConst = false;
6648 break;
6649 }
6650 }
6651
6652 if (IsConst) {
6653 Constant *One =
6654 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), 1.0);
6655 Constant *NegOne =
6656 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), -1.0);
6657
6658 SmallVector<Constant*, 4> CV(4, NegOne);
6659 for (unsigned i = 0; i < 4; ++i) {
6660 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF)
6661 CV[i] = UndefValue::get(Type::getFloatTy(*DAG.getContext()));
6662 else if (cast<ConstantSDNode>(BVN->getOperand(i))->
6663 getConstantIntValue()->isZero())
6664 continue;
6665 else
6666 CV[i] = One;
6667 }
6668
6669 Constant *CP = ConstantVector::get(CV);
6670 SDValue CPIdx = DAG.getConstantPool(CP, getPointerTy(),
6671 16 /* alignment */);
6672
6673 SmallVector<SDValue, 2> Ops;
6674 Ops.push_back(DAG.getEntryNode());
6675 Ops.push_back(CPIdx);
6676
6677 SmallVector<EVT, 2> ValueVTs;
6678 ValueVTs.push_back(MVT::v4i1);
6679 ValueVTs.push_back(MVT::Other); // chain
6680 SDVTList VTs = DAG.getVTList(ValueVTs);
6681
6682 return DAG.getMemIntrinsicNode(PPCISD::QVLFSb,
6683 dl, VTs, Ops, MVT::v4f32,
6684 MachinePointerInfo::getConstantPool());
6685 }
6686
6687 SmallVector<SDValue, 4> Stores;
6688 for (unsigned i = 0; i < 4; ++i) {
6689 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF) continue;
6690
6691 unsigned Offset = 4*i;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006692 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00006693 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
6694
6695 unsigned StoreSize = BVN->getOperand(i).getValueType().getStoreSize();
6696 if (StoreSize > 4) {
6697 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
6698 BVN->getOperand(i), Idx,
6699 PtrInfo.getWithOffset(Offset),
6700 MVT::i32, false, false, 0));
6701 } else {
6702 SDValue StoreValue = BVN->getOperand(i);
6703 if (StoreSize < 4)
6704 StoreValue = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, StoreValue);
6705
6706 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
6707 StoreValue, Idx,
6708 PtrInfo.getWithOffset(Offset),
6709 false, false, 0));
6710 }
6711 }
6712
6713 SDValue StoreChain;
6714 if (!Stores.empty())
6715 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
6716 else
6717 StoreChain = DAG.getEntryNode();
6718
6719 // Now load from v4i32 into the QPX register; this will extend it to
6720 // v4i64 but not yet convert it to a floating point. Nevertheless, this
6721 // is typed as v4f64 because the QPX register integer states are not
6722 // explicitly represented.
6723
6724 SmallVector<SDValue, 2> Ops;
6725 Ops.push_back(StoreChain);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006726 Ops.push_back(DAG.getConstant(Intrinsic::ppc_qpx_qvlfiwz, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00006727 Ops.push_back(FIdx);
6728
6729 SmallVector<EVT, 2> ValueVTs;
6730 ValueVTs.push_back(MVT::v4f64);
6731 ValueVTs.push_back(MVT::Other); // chain
6732 SDVTList VTs = DAG.getVTList(ValueVTs);
6733
6734 SDValue LoadedVect = DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN,
6735 dl, VTs, Ops, MVT::v4i32, PtrInfo);
6736 LoadedVect = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006737 DAG.getConstant(Intrinsic::ppc_qpx_qvfcfidu, dl, MVT::i32),
Hal Finkelc93a9a22015-02-25 01:06:45 +00006738 LoadedVect);
6739
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006740 SDValue FPZeros = DAG.getConstantFP(0.0, dl, MVT::f64);
Hal Finkelc93a9a22015-02-25 01:06:45 +00006741 FPZeros = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f64,
6742 FPZeros, FPZeros, FPZeros, FPZeros);
6743
6744 return DAG.getSetCC(dl, MVT::v4i1, LoadedVect, FPZeros, ISD::SETEQ);
6745 }
6746
6747 // All other QPX vectors are handled by generic code.
6748 if (Subtarget.hasQPX())
6749 return SDValue();
6750
Bob Wilson85cefe82009-03-02 23:24:16 +00006751 // Check if this is a splat of a constant value.
6752 APInt APSplatBits, APSplatUndef;
6753 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006754 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00006755 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Bill Schmidt91dd7652015-04-03 13:48:24 +00006756 HasAnyUndefs, 0, !Subtarget.isLittleEndian()) ||
6757 SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00006758 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00006759
Bob Wilson530e0382009-03-03 19:26:27 +00006760 unsigned SplatBits = APSplatBits.getZExtValue();
6761 unsigned SplatUndef = APSplatUndef.getZExtValue();
6762 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006763
Bob Wilson530e0382009-03-03 19:26:27 +00006764 // First, handle single instruction cases.
6765
6766 // All zeros?
6767 if (SplatBits == 0) {
6768 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00006769 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006770 SDValue Z = DAG.getConstant(0, dl, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00006771 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00006772 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00006773 }
Bob Wilson530e0382009-03-03 19:26:27 +00006774 return Op;
6775 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00006776
Bob Wilson530e0382009-03-03 19:26:27 +00006777 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
6778 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
6779 (32-SplatBitSize));
6780 if (SextVal >= -16 && SextVal <= 15)
6781 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006782
6783
Bob Wilson530e0382009-03-03 19:26:27 +00006784 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006785
Bob Wilson530e0382009-03-03 19:26:27 +00006786 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00006787 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
6788 // If this value is in the range [17,31] and is odd, use:
6789 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
6790 // If this value is in the range [-31,-17] and is odd, use:
6791 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
6792 // Note the last two are three-instruction sequences.
6793 if (SextVal >= -32 && SextVal <= 31) {
6794 // To avoid having these optimizations undone by constant folding,
6795 // we convert to a pseudo that will be expanded later into one of
6796 // the above forms.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006797 SDValue Elt = DAG.getConstant(SextVal, dl, MVT::i32);
Bill Schmidt71dddd52014-05-27 15:57:51 +00006798 EVT VT = (SplatSize == 1 ? MVT::v16i8 :
6799 (SplatSize == 2 ? MVT::v8i16 : MVT::v4i32));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006800 SDValue EltSize = DAG.getConstant(SplatSize, dl, MVT::i32);
Bill Schmidt71dddd52014-05-27 15:57:51 +00006801 SDValue RetVal = DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
6802 if (VT == Op.getValueType())
6803 return RetVal;
6804 else
6805 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), RetVal);
Bob Wilson530e0382009-03-03 19:26:27 +00006806 }
6807
6808 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
6809 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
6810 // for fneg/fabs.
6811 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
6812 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00006813 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006814
6815 // Make the VSLW intrinsic, computing 0x8000_0000.
6816 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
6817 OnesV, DAG, dl);
6818
6819 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00006820 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00006821 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00006822 }
6823
6824 // Check to see if this is a wide variety of vsplti*, binop self cases.
6825 static const signed char SplatCsts[] = {
6826 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
6827 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
6828 };
6829
6830 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
6831 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
6832 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
6833 int i = SplatCsts[idx];
6834
6835 // Figure out what shift amount will be used by altivec if shifted by i in
6836 // this splat size.
6837 unsigned TypeShiftAmt = i & (SplatBitSize-1);
6838
6839 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00006840 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00006841 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006842 static const unsigned IIDs[] = { // Intrinsic to use for each size.
6843 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
6844 Intrinsic::ppc_altivec_vslw
6845 };
6846 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00006847 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00006848 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006849
Bob Wilson530e0382009-03-03 19:26:27 +00006850 // vsplti + srl self.
6851 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00006852 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006853 static const unsigned IIDs[] = { // Intrinsic to use for each size.
6854 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
6855 Intrinsic::ppc_altivec_vsrw
6856 };
6857 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00006858 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00006859 }
6860
Bob Wilson530e0382009-03-03 19:26:27 +00006861 // vsplti + sra self.
6862 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00006863 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006864 static const unsigned IIDs[] = { // Intrinsic to use for each size.
6865 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
6866 Intrinsic::ppc_altivec_vsraw
6867 };
6868 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00006869 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00006870 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006871
Bob Wilson530e0382009-03-03 19:26:27 +00006872 // vsplti + rol self.
6873 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
6874 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00006875 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006876 static const unsigned IIDs[] = { // Intrinsic to use for each size.
6877 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
6878 Intrinsic::ppc_altivec_vrlw
6879 };
6880 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00006881 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00006882 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006883
Bob Wilson530e0382009-03-03 19:26:27 +00006884 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00006885 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00006886 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006887 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00006888 }
Bob Wilson530e0382009-03-03 19:26:27 +00006889 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00006890 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00006891 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006892 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00006893 }
Bob Wilson530e0382009-03-03 19:26:27 +00006894 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00006895 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00006896 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006897 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
6898 }
6899 }
6900
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006901 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00006902}
6903
Chris Lattner071ad012006-04-17 05:28:54 +00006904/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
6905/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006906static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00006907 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006908 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00006909 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00006910 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00006911 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006912
Chris Lattner071ad012006-04-17 05:28:54 +00006913 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00006914 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 +00006915 OP_VMRGHW,
6916 OP_VMRGLW,
6917 OP_VSPLTISW0,
6918 OP_VSPLTISW1,
6919 OP_VSPLTISW2,
6920 OP_VSPLTISW3,
6921 OP_VSLDOI4,
6922 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00006923 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00006924 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00006925
Chris Lattner071ad012006-04-17 05:28:54 +00006926 if (OpNum == OP_COPY) {
6927 if (LHSID == (1*9+2)*9+3) return LHS;
6928 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
6929 return RHS;
6930 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006931
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006932 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006933 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
6934 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006935
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006936 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00006937 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006938 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00006939 case OP_VMRGHW:
6940 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
6941 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
6942 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
6943 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
6944 break;
6945 case OP_VMRGLW:
6946 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
6947 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
6948 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
6949 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
6950 break;
6951 case OP_VSPLTISW0:
6952 for (unsigned i = 0; i != 16; ++i)
6953 ShufIdxs[i] = (i&3)+0;
6954 break;
6955 case OP_VSPLTISW1:
6956 for (unsigned i = 0; i != 16; ++i)
6957 ShufIdxs[i] = (i&3)+4;
6958 break;
6959 case OP_VSPLTISW2:
6960 for (unsigned i = 0; i != 16; ++i)
6961 ShufIdxs[i] = (i&3)+8;
6962 break;
6963 case OP_VSPLTISW3:
6964 for (unsigned i = 0; i != 16; ++i)
6965 ShufIdxs[i] = (i&3)+12;
6966 break;
6967 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006968 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006969 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006970 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006971 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006972 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006973 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00006974 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00006975 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
6976 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00006977 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00006978 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00006979}
6980
Chris Lattner19e90552006-04-14 05:19:18 +00006981/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
6982/// is a shuffle we can handle in a single instruction, return it. Otherwise,
6983/// return the code it can be lowered into. Worst case, it can always be
6984/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006985SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006986 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006987 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006988 SDValue V1 = Op.getOperand(0);
6989 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006990 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006991 EVT VT = Op.getValueType();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006992 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006993
Hal Finkelc93a9a22015-02-25 01:06:45 +00006994 if (Subtarget.hasQPX()) {
6995 if (VT.getVectorNumElements() != 4)
6996 return SDValue();
6997
6998 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
6999
7000 int AlignIdx = PPC::isQVALIGNIShuffleMask(SVOp);
7001 if (AlignIdx != -1) {
7002 return DAG.getNode(PPCISD::QVALIGNI, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007003 DAG.getConstant(AlignIdx, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007004 } else if (SVOp->isSplat()) {
7005 int SplatIdx = SVOp->getSplatIndex();
7006 if (SplatIdx >= 4) {
7007 std::swap(V1, V2);
7008 SplatIdx -= 4;
7009 }
7010
7011 // FIXME: If SplatIdx == 0 and the input came from a load, then there is
7012 // nothing to do.
7013
7014 return DAG.getNode(PPCISD::QVESPLATI, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007015 DAG.getConstant(SplatIdx, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007016 }
7017
7018 // Lower this into a qvgpci/qvfperm pair.
7019
7020 // Compute the qvgpci literal
7021 unsigned idx = 0;
7022 for (unsigned i = 0; i < 4; ++i) {
7023 int m = SVOp->getMaskElt(i);
7024 unsigned mm = m >= 0 ? (unsigned) m : i;
7025 idx |= mm << (3-i)*3;
7026 }
7027
7028 SDValue V3 = DAG.getNode(PPCISD::QVGPCI, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007029 DAG.getConstant(idx, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007030 return DAG.getNode(PPCISD::QVFPERM, dl, VT, V1, V2, V3);
7031 }
7032
Chris Lattner19e90552006-04-14 05:19:18 +00007033 // Cases that are handled by instructions that take permute immediates
7034 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
7035 // selected by the instruction selector.
7036 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007037 if (PPC::isSplatShuffleMask(SVOp, 1) ||
7038 PPC::isSplatShuffleMask(SVOp, 2) ||
7039 PPC::isSplatShuffleMask(SVOp, 4) ||
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00007040 PPC::isVPKUWUMShuffleMask(SVOp, 1, DAG) ||
7041 PPC::isVPKUHUMShuffleMask(SVOp, 1, DAG) ||
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00007042 PPC::isVPKUDUMShuffleMask(SVOp, 1, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00007043 PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00007044 PPC::isVMRGLShuffleMask(SVOp, 1, 1, DAG) ||
7045 PPC::isVMRGLShuffleMask(SVOp, 2, 1, DAG) ||
7046 PPC::isVMRGLShuffleMask(SVOp, 4, 1, DAG) ||
7047 PPC::isVMRGHShuffleMask(SVOp, 1, 1, DAG) ||
7048 PPC::isVMRGHShuffleMask(SVOp, 2, 1, DAG) ||
7049 PPC::isVMRGHShuffleMask(SVOp, 4, 1, DAG)) {
Chris Lattner19e90552006-04-14 05:19:18 +00007050 return Op;
7051 }
7052 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007053
Chris Lattner19e90552006-04-14 05:19:18 +00007054 // Altivec has a variety of "shuffle immediates" that take two vector inputs
7055 // and produce a fixed permutation. If any of these match, do not lower to
7056 // VPERM.
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00007057 unsigned int ShuffleKind = isLittleEndian ? 2 : 0;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00007058 if (PPC::isVPKUWUMShuffleMask(SVOp, ShuffleKind, DAG) ||
7059 PPC::isVPKUHUMShuffleMask(SVOp, ShuffleKind, DAG) ||
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00007060 PPC::isVPKUDUMShuffleMask(SVOp, ShuffleKind, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00007061 PPC::isVSLDOIShuffleMask(SVOp, ShuffleKind, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00007062 PPC::isVMRGLShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
7063 PPC::isVMRGLShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
7064 PPC::isVMRGLShuffleMask(SVOp, 4, ShuffleKind, DAG) ||
7065 PPC::isVMRGHShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
7066 PPC::isVMRGHShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
7067 PPC::isVMRGHShuffleMask(SVOp, 4, ShuffleKind, DAG))
Chris Lattner19e90552006-04-14 05:19:18 +00007068 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007069
Chris Lattner071ad012006-04-17 05:28:54 +00007070 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
7071 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00007072 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00007073
Chris Lattner071ad012006-04-17 05:28:54 +00007074 unsigned PFIndexes[4];
7075 bool isFourElementShuffle = true;
7076 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
7077 unsigned EltNo = 8; // Start out undef.
7078 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007079 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00007080 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007081
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007082 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00007083 if ((ByteSource & 3) != j) {
7084 isFourElementShuffle = false;
7085 break;
7086 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007087
Chris Lattner071ad012006-04-17 05:28:54 +00007088 if (EltNo == 8) {
7089 EltNo = ByteSource/4;
7090 } else if (EltNo != ByteSource/4) {
7091 isFourElementShuffle = false;
7092 break;
7093 }
7094 }
7095 PFIndexes[i] = EltNo;
7096 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007097
7098 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00007099 // perfect shuffle vector to determine if it is cost effective to do this as
7100 // discrete instructions, or whether we should use a vperm.
Bill Schmidtf910a062014-06-10 14:35:01 +00007101 // For now, we skip this for little endian until such time as we have a
7102 // little-endian perfect shuffle table.
7103 if (isFourElementShuffle && !isLittleEndian) {
Chris Lattner071ad012006-04-17 05:28:54 +00007104 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007105 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00007106 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007107
Chris Lattner071ad012006-04-17 05:28:54 +00007108 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
7109 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007110
Chris Lattner071ad012006-04-17 05:28:54 +00007111 // Determining when to avoid vperm is tricky. Many things affect the cost
7112 // of vperm, particularly how many times the perm mask needs to be computed.
7113 // For example, if the perm mask can be hoisted out of a loop or is already
7114 // used (perhaps because there are multiple permutes with the same shuffle
7115 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
7116 // the loop requires an extra register.
7117 //
7118 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00007119 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00007120 // available, if this block is within a loop, we should avoid using vperm
7121 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007122 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007123 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007124 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007125
Chris Lattner19e90552006-04-14 05:19:18 +00007126 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
7127 // vector that will get spilled to the constant pool.
7128 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007129
Chris Lattner19e90552006-04-14 05:19:18 +00007130 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
7131 // that it is in input element units, not in bytes. Convert now.
Bill Schmidt4aedff82014-06-06 14:06:26 +00007132
7133 // For little endian, the order of the input vectors is reversed, and
7134 // the permutation mask is complemented with respect to 31. This is
7135 // necessary to produce proper semantics with the big-endian-biased vperm
7136 // instruction.
Owen Anderson53aa7a92009-08-10 22:56:29 +00007137 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00007138 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007139
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007140 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007141 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
7142 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007143
Chris Lattner19e90552006-04-14 05:19:18 +00007144 for (unsigned j = 0; j != BytesPerElement; ++j)
Bill Schmidt4aedff82014-06-06 14:06:26 +00007145 if (isLittleEndian)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007146 ResultMask.push_back(DAG.getConstant(31 - (SrcElt*BytesPerElement + j),
7147 dl, MVT::i32));
Bill Schmidt4aedff82014-06-06 14:06:26 +00007148 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007149 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement + j, dl,
Bill Schmidt4aedff82014-06-06 14:06:26 +00007150 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00007151 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007152
Owen Anderson9f944592009-08-11 20:47:22 +00007153 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Craig Topper48d114b2014-04-26 18:35:24 +00007154 ResultMask);
Bill Schmidt4aedff82014-06-06 14:06:26 +00007155 if (isLittleEndian)
7156 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
7157 V2, V1, VPermMask);
7158 else
7159 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
7160 V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00007161}
7162
Chris Lattner9754d142006-04-18 17:59:36 +00007163/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
7164/// altivec comparison. If it is, return true and fill in Opc/isDot with
7165/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007166static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Kit Barton0cfa7b72015-03-03 19:55:45 +00007167 bool &isDot, const PPCSubtarget &Subtarget) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00007168 unsigned IntrinsicID =
7169 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00007170 CompareOpc = -1;
7171 isDot = false;
7172 switch (IntrinsicID) {
7173 default: return false;
7174 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00007175 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
7176 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
7177 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
7178 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
7179 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007180 case Intrinsic::ppc_altivec_vcmpequd_p:
7181 if (Subtarget.hasP8Altivec()) {
7182 CompareOpc = 199;
7183 isDot = 1;
7184 }
7185 else
7186 return false;
7187
7188 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007189 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
7190 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
7191 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
7192 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
7193 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007194 case Intrinsic::ppc_altivec_vcmpgtsd_p:
7195 if (Subtarget.hasP8Altivec()) {
7196 CompareOpc = 967;
7197 isDot = 1;
7198 }
7199 else
7200 return false;
7201
7202 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007203 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
7204 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
7205 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007206 case Intrinsic::ppc_altivec_vcmpgtud_p:
7207 if (Subtarget.hasP8Altivec()) {
7208 CompareOpc = 711;
7209 isDot = 1;
7210 }
7211 else
7212 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007213
Kit Barton0cfa7b72015-03-03 19:55:45 +00007214 break;
7215
Chris Lattner4211ca92006-04-14 06:01:58 +00007216 // Normal Comparisons.
7217 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
7218 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
7219 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
7220 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
7221 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007222 case Intrinsic::ppc_altivec_vcmpequd:
7223 if (Subtarget.hasP8Altivec()) {
7224 CompareOpc = 199;
7225 isDot = 0;
7226 }
7227 else
7228 return false;
7229
7230 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007231 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
7232 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
7233 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
7234 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
7235 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007236 case Intrinsic::ppc_altivec_vcmpgtsd:
7237 if (Subtarget.hasP8Altivec()) {
7238 CompareOpc = 967;
7239 isDot = 0;
7240 }
7241 else
7242 return false;
7243
7244 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007245 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
7246 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
7247 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007248 case Intrinsic::ppc_altivec_vcmpgtud:
7249 if (Subtarget.hasP8Altivec()) {
7250 CompareOpc = 711;
7251 isDot = 0;
7252 }
7253 else
7254 return false;
7255
7256 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007257 }
Chris Lattner9754d142006-04-18 17:59:36 +00007258 return true;
7259}
7260
7261/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
7262/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007263SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007264 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00007265 // If this is a lowered altivec predicate compare, CompareOpc is set to the
7266 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00007267 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00007268 int CompareOpc;
7269 bool isDot;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007270 if (!getAltivecCompareInfo(Op, CompareOpc, isDot, Subtarget))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007271 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007272
Chris Lattner9754d142006-04-18 17:59:36 +00007273 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00007274 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00007275 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00007276 Op.getOperand(1), Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007277 DAG.getConstant(CompareOpc, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00007278 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00007279 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007280
Chris Lattner4211ca92006-04-14 06:01:58 +00007281 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007282 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007283 Op.getOperand(2), // LHS
7284 Op.getOperand(3), // RHS
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007285 DAG.getConstant(CompareOpc, dl, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007286 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00007287 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00007288 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007289
Chris Lattner4211ca92006-04-14 06:01:58 +00007290 // Now that we have the comparison, emit a copy from the CR to a GPR.
7291 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00007292 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00007293 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00007294 CompNode.getValue(1));
7295
Chris Lattner4211ca92006-04-14 06:01:58 +00007296 // Unpack the result based on how the target uses it.
7297 unsigned BitNo; // Bit # of CR6.
7298 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00007299 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00007300 default: // Can't happen, don't crash on invalid number though.
7301 case 0: // Return the value of the EQ bit of CR6.
7302 BitNo = 0; InvertBit = false;
7303 break;
7304 case 1: // Return the inverted value of the EQ bit of CR6.
7305 BitNo = 0; InvertBit = true;
7306 break;
7307 case 2: // Return the value of the LT bit of CR6.
7308 BitNo = 2; InvertBit = false;
7309 break;
7310 case 3: // Return the inverted value of the LT bit of CR6.
7311 BitNo = 2; InvertBit = true;
7312 break;
7313 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007314
Chris Lattner4211ca92006-04-14 06:01:58 +00007315 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00007316 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007317 DAG.getConstant(8 - (3 - BitNo), dl, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00007318 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00007319 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007320 DAG.getConstant(1, dl, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00007321
Chris Lattner4211ca92006-04-14 06:01:58 +00007322 // If we are supposed to, toggle the bit.
7323 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00007324 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007325 DAG.getConstant(1, dl, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00007326 return Flags;
7327}
7328
Hal Finkel5c0d1452014-03-30 13:22:59 +00007329SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
7330 SelectionDAG &DAG) const {
7331 SDLoc dl(Op);
7332 // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int
7333 // instructions), but for smaller types, we need to first extend up to v2i32
7334 // before doing going farther.
7335 if (Op.getValueType() == MVT::v2i64) {
7336 EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
7337 if (ExtVT != MVT::v2i32) {
7338 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0));
7339 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op,
7340 DAG.getValueType(EVT::getVectorVT(*DAG.getContext(),
7341 ExtVT.getVectorElementType(), 4)));
7342 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op);
7343 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op,
7344 DAG.getValueType(MVT::v2i32));
7345 }
7346
7347 return Op;
7348 }
7349
7350 return SDValue();
7351}
7352
Scott Michelcf0da6c2009-02-17 22:15:04 +00007353SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007354 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007355 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00007356 // Create a stack slot that is 16-byte aligned.
7357 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00007358 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007359 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007360 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007361
Chris Lattner4211ca92006-04-14 06:01:58 +00007362 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00007363 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00007364 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00007365 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00007366 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00007367 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00007368 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00007369}
7370
Hal Finkelc93a9a22015-02-25 01:06:45 +00007371SDValue PPCTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7372 SelectionDAG &DAG) const {
7373 SDLoc dl(Op);
7374 SDNode *N = Op.getNode();
7375
7376 assert(N->getOperand(0).getValueType() == MVT::v4i1 &&
7377 "Unknown extract_vector_elt type");
7378
7379 SDValue Value = N->getOperand(0);
7380
7381 // The first part of this is like the store lowering except that we don't
7382 // need to track the chain.
7383
7384 // The values are now known to be -1 (false) or 1 (true). To convert this
7385 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
7386 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
7387 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
7388
7389 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to
7390 // understand how to form the extending load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007391 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::f64);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007392 FPHalfs = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f64,
7393 FPHalfs, FPHalfs, FPHalfs, FPHalfs);
7394
7395 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
7396
7397 // Now convert to an integer and store.
7398 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007399 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32),
Hal Finkelc93a9a22015-02-25 01:06:45 +00007400 Value);
7401
7402 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
7403 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
7404 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FrameIdx);
7405 EVT PtrVT = getPointerTy();
7406 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
7407
7408 SDValue StoreChain = DAG.getEntryNode();
7409 SmallVector<SDValue, 2> Ops;
7410 Ops.push_back(StoreChain);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007411 Ops.push_back(DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007412 Ops.push_back(Value);
7413 Ops.push_back(FIdx);
7414
7415 SmallVector<EVT, 2> ValueVTs;
7416 ValueVTs.push_back(MVT::Other); // chain
7417 SDVTList VTs = DAG.getVTList(ValueVTs);
7418
7419 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID,
7420 dl, VTs, Ops, MVT::v4i32, PtrInfo);
7421
7422 // Extract the value requested.
7423 unsigned Offset = 4*cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007424 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007425 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
7426
7427 SDValue IntVal = DAG.getLoad(MVT::i32, dl, StoreChain, Idx,
7428 PtrInfo.getWithOffset(Offset),
7429 false, false, false, 0);
7430
7431 if (!Subtarget.useCRBits())
7432 return IntVal;
7433
7434 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, IntVal);
7435}
7436
7437/// Lowering for QPX v4i1 loads
7438SDValue PPCTargetLowering::LowerVectorLoad(SDValue Op,
7439 SelectionDAG &DAG) const {
7440 SDLoc dl(Op);
7441 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
7442 SDValue LoadChain = LN->getChain();
7443 SDValue BasePtr = LN->getBasePtr();
7444
7445 if (Op.getValueType() == MVT::v4f64 ||
7446 Op.getValueType() == MVT::v4f32) {
7447 EVT MemVT = LN->getMemoryVT();
7448 unsigned Alignment = LN->getAlignment();
7449
7450 // If this load is properly aligned, then it is legal.
7451 if (Alignment >= MemVT.getStoreSize())
7452 return Op;
7453
7454 EVT ScalarVT = Op.getValueType().getScalarType(),
7455 ScalarMemVT = MemVT.getScalarType();
7456 unsigned Stride = ScalarMemVT.getStoreSize();
7457
7458 SmallVector<SDValue, 8> Vals, LoadChains;
7459 for (unsigned Idx = 0; Idx < 4; ++Idx) {
7460 SDValue Load;
7461 if (ScalarVT != ScalarMemVT)
7462 Load =
7463 DAG.getExtLoad(LN->getExtensionType(), dl, ScalarVT, LoadChain,
7464 BasePtr,
7465 LN->getPointerInfo().getWithOffset(Idx*Stride),
7466 ScalarMemVT, LN->isVolatile(), LN->isNonTemporal(),
7467 LN->isInvariant(), MinAlign(Alignment, Idx*Stride),
7468 LN->getAAInfo());
7469 else
7470 Load =
7471 DAG.getLoad(ScalarVT, dl, LoadChain, BasePtr,
7472 LN->getPointerInfo().getWithOffset(Idx*Stride),
7473 LN->isVolatile(), LN->isNonTemporal(),
7474 LN->isInvariant(), MinAlign(Alignment, Idx*Stride),
7475 LN->getAAInfo());
7476
7477 if (Idx == 0 && LN->isIndexed()) {
7478 assert(LN->getAddressingMode() == ISD::PRE_INC &&
7479 "Unknown addressing mode on vector load");
7480 Load = DAG.getIndexedLoad(Load, dl, BasePtr, LN->getOffset(),
7481 LN->getAddressingMode());
7482 }
7483
7484 Vals.push_back(Load);
7485 LoadChains.push_back(Load.getValue(1));
7486
7487 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007488 DAG.getConstant(Stride, dl,
7489 BasePtr.getValueType()));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007490 }
7491
7492 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
7493 SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007494 Op.getValueType(), Vals);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007495
7496 if (LN->isIndexed()) {
7497 SDValue RetOps[] = { Value, Vals[0].getValue(1), TF };
7498 return DAG.getMergeValues(RetOps, dl);
7499 }
7500
7501 SDValue RetOps[] = { Value, TF };
7502 return DAG.getMergeValues(RetOps, dl);
7503 }
7504
7505 assert(Op.getValueType() == MVT::v4i1 && "Unknown load to lower");
7506 assert(LN->isUnindexed() && "Indexed v4i1 loads are not supported");
7507
7508 // To lower v4i1 from a byte array, we load the byte elements of the
7509 // vector and then reuse the BUILD_VECTOR logic.
7510
7511 SmallVector<SDValue, 4> VectElmts, VectElmtChains;
7512 for (unsigned i = 0; i < 4; ++i) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007513 SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007514 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx);
7515
7516 VectElmts.push_back(DAG.getExtLoad(ISD::EXTLOAD,
7517 dl, MVT::i32, LoadChain, Idx,
7518 LN->getPointerInfo().getWithOffset(i),
7519 MVT::i8 /* memory type */,
7520 LN->isVolatile(), LN->isNonTemporal(),
7521 LN->isInvariant(),
7522 1 /* alignment */, LN->getAAInfo()));
7523 VectElmtChains.push_back(VectElmts[i].getValue(1));
7524 }
7525
7526 LoadChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, VectElmtChains);
7527 SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i1, VectElmts);
7528
7529 SDValue RVals[] = { Value, LoadChain };
7530 return DAG.getMergeValues(RVals, dl);
7531}
7532
7533/// Lowering for QPX v4i1 stores
7534SDValue PPCTargetLowering::LowerVectorStore(SDValue Op,
7535 SelectionDAG &DAG) const {
7536 SDLoc dl(Op);
7537 StoreSDNode *SN = cast<StoreSDNode>(Op.getNode());
7538 SDValue StoreChain = SN->getChain();
7539 SDValue BasePtr = SN->getBasePtr();
7540 SDValue Value = SN->getValue();
7541
7542 if (Value.getValueType() == MVT::v4f64 ||
7543 Value.getValueType() == MVT::v4f32) {
7544 EVT MemVT = SN->getMemoryVT();
7545 unsigned Alignment = SN->getAlignment();
7546
7547 // If this store is properly aligned, then it is legal.
7548 if (Alignment >= MemVT.getStoreSize())
7549 return Op;
7550
7551 EVT ScalarVT = Value.getValueType().getScalarType(),
7552 ScalarMemVT = MemVT.getScalarType();
7553 unsigned Stride = ScalarMemVT.getStoreSize();
7554
7555 SmallVector<SDValue, 8> Stores;
7556 for (unsigned Idx = 0; Idx < 4; ++Idx) {
7557 SDValue Ex =
7558 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ScalarVT, Value,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007559 DAG.getConstant(Idx, dl, getVectorIdxTy()));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007560 SDValue Store;
7561 if (ScalarVT != ScalarMemVT)
7562 Store =
7563 DAG.getTruncStore(StoreChain, dl, Ex, BasePtr,
7564 SN->getPointerInfo().getWithOffset(Idx*Stride),
7565 ScalarMemVT, SN->isVolatile(), SN->isNonTemporal(),
7566 MinAlign(Alignment, Idx*Stride), SN->getAAInfo());
7567 else
7568 Store =
7569 DAG.getStore(StoreChain, dl, Ex, BasePtr,
7570 SN->getPointerInfo().getWithOffset(Idx*Stride),
7571 SN->isVolatile(), SN->isNonTemporal(),
7572 MinAlign(Alignment, Idx*Stride), SN->getAAInfo());
7573
7574 if (Idx == 0 && SN->isIndexed()) {
7575 assert(SN->getAddressingMode() == ISD::PRE_INC &&
7576 "Unknown addressing mode on vector store");
7577 Store = DAG.getIndexedStore(Store, dl, BasePtr, SN->getOffset(),
7578 SN->getAddressingMode());
7579 }
7580
7581 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007582 DAG.getConstant(Stride, dl,
7583 BasePtr.getValueType()));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007584 Stores.push_back(Store);
7585 }
7586
7587 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
7588
7589 if (SN->isIndexed()) {
7590 SDValue RetOps[] = { TF, Stores[0].getValue(1) };
7591 return DAG.getMergeValues(RetOps, dl);
7592 }
7593
7594 return TF;
7595 }
7596
7597 assert(SN->isUnindexed() && "Indexed v4i1 stores are not supported");
7598 assert(Value.getValueType() == MVT::v4i1 && "Unknown store to lower");
7599
7600 // The values are now known to be -1 (false) or 1 (true). To convert this
7601 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
7602 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
7603 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
7604
7605 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to
7606 // understand how to form the extending load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007607 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::f64);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007608 FPHalfs = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f64,
7609 FPHalfs, FPHalfs, FPHalfs, FPHalfs);
7610
7611 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
7612
7613 // Now convert to an integer and store.
7614 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007615 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32),
Hal Finkelc93a9a22015-02-25 01:06:45 +00007616 Value);
7617
7618 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
7619 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
7620 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FrameIdx);
7621 EVT PtrVT = getPointerTy();
7622 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
7623
7624 SmallVector<SDValue, 2> Ops;
7625 Ops.push_back(StoreChain);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007626 Ops.push_back(DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007627 Ops.push_back(Value);
7628 Ops.push_back(FIdx);
7629
7630 SmallVector<EVT, 2> ValueVTs;
7631 ValueVTs.push_back(MVT::Other); // chain
7632 SDVTList VTs = DAG.getVTList(ValueVTs);
7633
7634 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID,
7635 dl, VTs, Ops, MVT::v4i32, PtrInfo);
7636
7637 // Move data into the byte array.
7638 SmallVector<SDValue, 4> Loads, LoadChains;
7639 for (unsigned i = 0; i < 4; ++i) {
7640 unsigned Offset = 4*i;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007641 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007642 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
7643
7644 Loads.push_back(DAG.getLoad(MVT::i32, dl, StoreChain, Idx,
7645 PtrInfo.getWithOffset(Offset),
7646 false, false, false, 0));
7647 LoadChains.push_back(Loads[i].getValue(1));
7648 }
7649
7650 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
7651
7652 SmallVector<SDValue, 4> Stores;
7653 for (unsigned i = 0; i < 4; ++i) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007654 SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007655 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx);
7656
7657 Stores.push_back(DAG.getTruncStore(StoreChain, dl, Loads[i], Idx,
7658 SN->getPointerInfo().getWithOffset(i),
7659 MVT::i8 /* memory type */,
7660 SN->isNonTemporal(), SN->isVolatile(),
7661 1 /* alignment */, SN->getAAInfo()));
7662 }
7663
7664 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
7665
7666 return StoreChain;
7667}
7668
Dan Gohman21cea8a2010-04-17 15:26:15 +00007669SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007670 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00007671 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007672 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007673
Owen Anderson9f944592009-08-11 20:47:22 +00007674 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
7675 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007676
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007677 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007678 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007679
Chris Lattner7e4398742006-04-18 03:43:48 +00007680 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00007681 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
7682 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
7683 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007684
Chris Lattner7e4398742006-04-18 03:43:48 +00007685 // Low parts multiplied together, generating 32-bit results (we ignore the
7686 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007687 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00007688 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007689
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007690 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00007691 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00007692 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007693 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007694 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00007695 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
7696 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007697 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007698
Owen Anderson9f944592009-08-11 20:47:22 +00007699 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00007700
Chris Lattner96d50482006-04-18 04:28:57 +00007701 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007702 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00007703 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007704 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007705 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00007706
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00007707 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007708 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00007709 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00007710 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007711
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00007712 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007713 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00007714 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00007715 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007716
Bill Schmidt42995e82014-06-09 16:06:29 +00007717 // Merge the results together. Because vmuleub and vmuloub are
7718 // instructions with a big-endian bias, we must reverse the
7719 // element numbering and reverse the meaning of "odd" and "even"
7720 // when generating little endian code.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007721 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00007722 for (unsigned i = 0; i != 8; ++i) {
Bill Schmidt42995e82014-06-09 16:06:29 +00007723 if (isLittleEndian) {
7724 Ops[i*2 ] = 2*i;
7725 Ops[i*2+1] = 2*i+16;
7726 } else {
7727 Ops[i*2 ] = 2*i+1;
7728 Ops[i*2+1] = 2*i+1+16;
7729 }
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00007730 }
Bill Schmidt42995e82014-06-09 16:06:29 +00007731 if (isLittleEndian)
7732 return DAG.getVectorShuffle(MVT::v16i8, dl, OddParts, EvenParts, Ops);
7733 else
7734 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00007735 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007736 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00007737 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00007738}
7739
Chris Lattnerf3d06c62005-08-26 00:52:45 +00007740/// LowerOperation - Provide custom lowering hooks for some operations.
7741///
Dan Gohman21cea8a2010-04-17 15:26:15 +00007742SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00007743 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007744 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00007745 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00007746 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00007747 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00007748 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00007749 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00007750 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00007751 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
7752 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007753 case ISD::VASTART:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007754 return LowerVASTART(Op, DAG, Subtarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007755
7756 case ISD::VAARG:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007757 return LowerVAARG(Op, DAG, Subtarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00007758
Roman Divackyc3825df2013-07-25 21:36:47 +00007759 case ISD::VACOPY:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007760 return LowerVACOPY(Op, DAG, Subtarget);
Roman Divackyc3825df2013-07-25 21:36:47 +00007761
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007762 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, Subtarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00007763 case ISD::DYNAMIC_STACKALLOC:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007764 return LowerDYNAMIC_STACKALLOC(Op, DAG, Subtarget);
Evan Cheng51096af2008-04-19 01:30:48 +00007765
Hal Finkel756810f2013-03-21 21:37:52 +00007766 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
7767 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
7768
Hal Finkel940ab932014-02-28 00:27:01 +00007769 case ISD::LOAD: return LowerLOAD(Op, DAG);
7770 case ISD::STORE: return LowerSTORE(Op, DAG);
7771 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00007772 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00007773 case ISD::FP_TO_UINT:
7774 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Hal Finkeled844c42015-01-06 22:31:02 +00007775 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00007776 case ISD::UINT_TO_FP:
7777 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00007778 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00007779
Chris Lattner4211ca92006-04-14 06:01:58 +00007780 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00007781 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
7782 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
7783 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00007784
Chris Lattner4211ca92006-04-14 06:01:58 +00007785 // Vector-related lowering.
7786 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
7787 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7788 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
7789 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Hal Finkel5c0d1452014-03-30 13:22:59 +00007790 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007791 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00007792 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007793
Hal Finkel25c19922013-05-15 21:37:41 +00007794 // For counter-based loop handling.
7795 case ISD::INTRINSIC_W_CHAIN: return SDValue();
7796
Chris Lattnerf6a81562007-12-08 06:59:59 +00007797 // Frame & Return address.
7798 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00007799 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00007800 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00007801}
7802
Duncan Sands6ed40142008-12-01 11:39:25 +00007803void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
7804 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007805 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007806 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00007807 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00007808 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007809 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkelbbdee932014-12-02 22:01:00 +00007810 case ISD::READCYCLECOUNTER: {
7811 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
7812 SDValue RTB = DAG.getNode(PPCISD::READ_TIME_BASE, dl, VTs, N->getOperand(0));
7813
7814 Results.push_back(RTB);
7815 Results.push_back(RTB.getValue(1));
7816 Results.push_back(RTB.getValue(2));
7817 break;
7818 }
Hal Finkel25c19922013-05-15 21:37:41 +00007819 case ISD::INTRINSIC_W_CHAIN: {
7820 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
7821 Intrinsic::ppc_is_decremented_ctr_nonzero)
7822 break;
7823
7824 assert(N->getValueType(0) == MVT::i1 &&
7825 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00007826 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00007827 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
7828 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
7829 N->getOperand(1));
7830
7831 Results.push_back(NewInt);
7832 Results.push_back(NewInt.getValue(1));
7833 break;
7834 }
Roman Divacky4394e682011-06-28 15:30:42 +00007835 case ISD::VAARG: {
Eric Christophercccae792015-01-30 22:02:31 +00007836 if (!Subtarget.isSVR4ABI() || Subtarget.isPPC64())
Roman Divacky4394e682011-06-28 15:30:42 +00007837 return;
7838
7839 EVT VT = N->getValueType(0);
7840
7841 if (VT == MVT::i64) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007842 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, Subtarget);
Roman Divacky4394e682011-06-28 15:30:42 +00007843
7844 Results.push_back(NewNode);
7845 Results.push_back(NewNode.getValue(1));
7846 }
7847 return;
7848 }
Duncan Sands6ed40142008-12-01 11:39:25 +00007849 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00007850 assert(N->getValueType(0) == MVT::ppcf128);
7851 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007852 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00007853 MVT::f64, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007854 DAG.getIntPtrConstant(0, dl));
Dale Johannesenf80493b2009-02-05 22:07:54 +00007855 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00007856 MVT::f64, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007857 DAG.getIntPtrConstant(1, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00007858
Ulrich Weigand874fc622013-03-26 10:56:22 +00007859 // Add the two halves of the long double in round-to-zero mode.
7860 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00007861
7862 // We know the low half is about to be thrown away, so just use something
7863 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00007864 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00007865 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00007866 return;
Duncan Sands2a287912008-07-19 16:26:02 +00007867 }
Duncan Sands6ed40142008-12-01 11:39:25 +00007868 case ISD::FP_TO_SINT:
Hal Finkel93138502015-04-10 03:39:00 +00007869 case ISD::FP_TO_UINT:
Bill Schmidt41221692013-07-09 18:50:20 +00007870 // LowerFP_TO_INT() can only handle f32 and f64.
7871 if (N->getOperand(0).getValueType() == MVT::ppcf128)
7872 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00007873 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00007874 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00007875 }
7876}
7877
7878
Chris Lattner4211ca92006-04-14 06:01:58 +00007879//===----------------------------------------------------------------------===//
7880// Other Lowering Code
7881//===----------------------------------------------------------------------===//
7882
Robin Morisset22129962014-09-23 20:46:49 +00007883static Instruction* callIntrinsic(IRBuilder<> &Builder, Intrinsic::ID Id) {
7884 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
7885 Function *Func = Intrinsic::getDeclaration(M, Id);
David Blaikieff6409d2015-05-18 22:13:54 +00007886 return Builder.CreateCall(Func, {});
Robin Morisset22129962014-09-23 20:46:49 +00007887}
7888
7889// The mappings for emitLeading/TrailingFence is taken from
7890// http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
7891Instruction* PPCTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
7892 AtomicOrdering Ord, bool IsStore,
7893 bool IsLoad) const {
7894 if (Ord == SequentiallyConsistent)
7895 return callIntrinsic(Builder, Intrinsic::ppc_sync);
David Blaikieff6409d2015-05-18 22:13:54 +00007896 if (isAtLeastRelease(Ord))
Robin Morisset22129962014-09-23 20:46:49 +00007897 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
David Blaikieff6409d2015-05-18 22:13:54 +00007898 return nullptr;
Robin Morisset22129962014-09-23 20:46:49 +00007899}
7900
7901Instruction* PPCTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
7902 AtomicOrdering Ord, bool IsStore,
7903 bool IsLoad) const {
7904 if (IsLoad && isAtLeastAcquire(Ord))
7905 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
7906 // FIXME: this is too conservative, a dependent branch + isync is enough.
7907 // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html and
7908 // http://www.rdrop.com/users/paulmck/scalability/paper/N2745r.2011.03.04a.html
7909 // and http://www.cl.cam.ac.uk/~pes20/cppppc/ for justification.
David Blaikieff6409d2015-05-18 22:13:54 +00007910 return nullptr;
Robin Morisset22129962014-09-23 20:46:49 +00007911}
7912
Chris Lattner9b577f12005-08-26 21:23:58 +00007913MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00007914PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00007915 unsigned AtomicSize,
7916 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007917 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christophercccae792015-01-30 22:02:31 +00007918 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Dale Johannesend4eb0522008-08-25 22:34:37 +00007919
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00007920 auto LoadMnemonic = PPC::LDARX;
7921 auto StoreMnemonic = PPC::STDCX;
7922 switch (AtomicSize) {
7923 default:
7924 llvm_unreachable("Unexpected size of atomic entity");
7925 case 1:
7926 LoadMnemonic = PPC::LBARX;
7927 StoreMnemonic = PPC::STBCX;
7928 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4");
7929 break;
7930 case 2:
7931 LoadMnemonic = PPC::LHARX;
7932 StoreMnemonic = PPC::STHCX;
7933 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4");
7934 break;
7935 case 4:
7936 LoadMnemonic = PPC::LWARX;
7937 StoreMnemonic = PPC::STWCX;
7938 break;
7939 case 8:
7940 LoadMnemonic = PPC::LDARX;
7941 StoreMnemonic = PPC::STDCX;
7942 break;
7943 }
7944
Dale Johannesend4eb0522008-08-25 22:34:37 +00007945 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7946 MachineFunction *F = BB->getParent();
7947 MachineFunction::iterator It = BB;
7948 ++It;
7949
7950 unsigned dest = MI->getOperand(0).getReg();
7951 unsigned ptrA = MI->getOperand(1).getReg();
7952 unsigned ptrB = MI->getOperand(2).getReg();
7953 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00007954 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00007955
7956 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
7957 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
7958 F->insert(It, loopMBB);
7959 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007960 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007961 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007962 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007963
7964 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007965 unsigned TmpReg = (!BinOpcode) ? incr :
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00007966 RegInfo.createVirtualRegister( AtomicSize == 8 ? &PPC::G8RCRegClass
Craig Topper61e88f42014-11-21 05:58:21 +00007967 : &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007968
7969 // thisMBB:
7970 // ...
7971 // fallthrough --> loopMBB
7972 BB->addSuccessor(loopMBB);
7973
7974 // loopMBB:
7975 // l[wd]arx dest, ptr
7976 // add r0, dest, incr
7977 // st[wd]cx. r0, ptr
7978 // bne- loopMBB
7979 // fallthrough --> exitMBB
7980 BB = loopMBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00007981 BuildMI(BB, dl, TII->get(LoadMnemonic), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00007982 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007983 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00007984 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00007985 BuildMI(BB, dl, TII->get(StoreMnemonic))
Dale Johannesend4eb0522008-08-25 22:34:37 +00007986 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007987 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00007988 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007989 BB->addSuccessor(loopMBB);
7990 BB->addSuccessor(exitMBB);
7991
7992 // exitMBB:
7993 // ...
7994 BB = exitMBB;
7995 return BB;
7996}
7997
7998MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00007999PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00008000 MachineBasicBlock *BB,
8001 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00008002 unsigned BinOpcode) const {
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008003 // If we support part-word atomic mnemonics, just use them
8004 if (Subtarget.hasPartwordAtomics())
8005 return EmitAtomicBinary(MI, BB, is8bit ? 1 : 2, BinOpcode);
8006
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008007 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christophercccae792015-01-30 22:02:31 +00008008 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Dale Johannesena32affb2008-08-28 17:53:09 +00008009 // In 64 bit mode we have to use 64 bits for addresses, even though the
8010 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
8011 // registers without caring whether they're 32 or 64, but here we're
8012 // doing actual arithmetic on the addresses.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008013 bool is64bit = Subtarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00008014 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00008015
8016 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8017 MachineFunction *F = BB->getParent();
8018 MachineFunction::iterator It = BB;
8019 ++It;
8020
8021 unsigned dest = MI->getOperand(0).getReg();
8022 unsigned ptrA = MI->getOperand(1).getReg();
8023 unsigned ptrB = MI->getOperand(2).getReg();
8024 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00008025 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00008026
8027 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
8028 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
8029 F->insert(It, loopMBB);
8030 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008031 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008032 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008033 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00008034
8035 MachineRegisterInfo &RegInfo = F->getRegInfo();
Craig Topper61e88f42014-11-21 05:58:21 +00008036 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass
8037 : &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00008038 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
8039 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
8040 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
8041 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
8042 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
8043 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
8044 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
8045 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
8046 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
8047 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008048 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00008049 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008050 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00008051
8052 // thisMBB:
8053 // ...
8054 // fallthrough --> loopMBB
8055 BB->addSuccessor(loopMBB);
8056
8057 // The 4-byte load must be aligned, while a char or short may be
8058 // anywhere in the word. Hence all this nasty bookkeeping code.
8059 // add ptr1, ptrA, ptrB [copy if ptrA==0]
8060 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00008061 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00008062 // rlwinm ptr, ptr1, 0, 0, 29
8063 // slw incr2, incr, shift
8064 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
8065 // slw mask, mask2, shift
8066 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00008067 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008068 // add tmp, tmpDest, incr2
8069 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00008070 // and tmp3, tmp, mask
8071 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00008072 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00008073 // bne- loopMBB
8074 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008075 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008076 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00008077 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008078 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008079 .addReg(ptrA).addReg(ptrB);
8080 } else {
8081 Ptr1Reg = ptrB;
8082 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008083 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008084 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008085 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008086 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
8087 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008088 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008089 .addReg(Ptr1Reg).addImm(0).addImm(61);
8090 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00008091 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008092 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008093 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008094 .addReg(incr).addReg(ShiftReg);
8095 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008096 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00008097 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00008098 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
8099 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00008100 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008101 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008102 .addReg(Mask2Reg).addReg(ShiftReg);
8103
8104 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00008105 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008106 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008107 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008108 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008109 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008110 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008111 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008112 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008113 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008114 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008115 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00008116 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008117 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008118 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00008119 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00008120 BB->addSuccessor(loopMBB);
8121 BB->addSuccessor(exitMBB);
8122
8123 // exitMBB:
8124 // ...
8125 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00008126 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
8127 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00008128 return BB;
8129}
8130
Hal Finkel756810f2013-03-21 21:37:52 +00008131llvm::MachineBasicBlock*
8132PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
8133 MachineBasicBlock *MBB) const {
8134 DebugLoc DL = MI->getDebugLoc();
Eric Christophercccae792015-01-30 22:02:31 +00008135 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00008136
8137 MachineFunction *MF = MBB->getParent();
8138 MachineRegisterInfo &MRI = MF->getRegInfo();
8139
8140 const BasicBlock *BB = MBB->getBasicBlock();
8141 MachineFunction::iterator I = MBB;
8142 ++I;
8143
8144 // Memory Reference
8145 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
8146 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
8147
8148 unsigned DstReg = MI->getOperand(0).getReg();
8149 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
8150 assert(RC->hasType(MVT::i32) && "Invalid destination!");
8151 unsigned mainDstReg = MRI.createVirtualRegister(RC);
8152 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
8153
8154 MVT PVT = getPointerTy();
8155 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
8156 "Invalid Pointer Size!");
8157 // For v = setjmp(buf), we generate
8158 //
8159 // thisMBB:
8160 // SjLjSetup mainMBB
8161 // bl mainMBB
8162 // v_restore = 1
8163 // b sinkMBB
8164 //
8165 // mainMBB:
8166 // buf[LabelOffset] = LR
8167 // v_main = 0
8168 //
8169 // sinkMBB:
8170 // v = phi(main, restore)
8171 //
8172
8173 MachineBasicBlock *thisMBB = MBB;
8174 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
8175 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
8176 MF->insert(I, mainMBB);
8177 MF->insert(I, sinkMBB);
8178
8179 MachineInstrBuilder MIB;
8180
8181 // Transfer the remainder of BB and its successor edges to sinkMBB.
8182 sinkMBB->splice(sinkMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008183 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Hal Finkel756810f2013-03-21 21:37:52 +00008184 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
8185
8186 // Note that the structure of the jmp_buf used here is not compatible
8187 // with that used by libc, and is not designed to be. Specifically, it
8188 // stores only those 'reserved' registers that LLVM does not otherwise
8189 // understand how to spill. Also, by convention, by the time this
8190 // intrinsic is called, Clang has already stored the frame address in the
8191 // first slot of the buffer and stack address in the third. Following the
8192 // X86 target code, we'll store the jump address in the second slot. We also
8193 // need to save the TOC pointer (R2) to handle jumps between shared
8194 // libraries, and that will be stored in the fourth slot. The thread
8195 // identifier (R13) is not affected.
8196
8197 // thisMBB:
8198 const int64_t LabelOffset = 1 * PVT.getStoreSize();
8199 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00008200 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00008201
8202 // Prepare IP either in reg.
8203 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
8204 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
8205 unsigned BufReg = MI->getOperand(1).getReg();
8206
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008207 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI()) {
Hal Finkele6698d52015-02-01 15:03:28 +00008208 setUsesTOCBasePtr(*MBB->getParent());
Hal Finkel756810f2013-03-21 21:37:52 +00008209 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
8210 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008211 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008212 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00008213 MIB.setMemRefs(MMOBegin, MMOEnd);
8214 }
8215
Hal Finkelf05d6c72013-07-17 23:50:51 +00008216 // Naked functions never have a base pointer, and so we use r1. For all
8217 // other functions, this decision must be delayed until during PEI.
8218 unsigned BaseReg;
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +00008219 if (MF->getFunction()->hasFnAttribute(Attribute::Naked))
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008220 BaseReg = Subtarget.isPPC64() ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00008221 else
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008222 BaseReg = Subtarget.isPPC64() ? PPC::BP8 : PPC::BP;
Hal Finkelf05d6c72013-07-17 23:50:51 +00008223
8224 MIB = BuildMI(*thisMBB, MI, DL,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008225 TII->get(Subtarget.isPPC64() ? PPC::STD : PPC::STW))
Eric Christophercccae792015-01-30 22:02:31 +00008226 .addReg(BaseReg)
8227 .addImm(BPOffset)
8228 .addReg(BufReg);
Hal Finkelf05d6c72013-07-17 23:50:51 +00008229 MIB.setMemRefs(MMOBegin, MMOEnd);
8230
Hal Finkel756810f2013-03-21 21:37:52 +00008231 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00008232 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Eric Christophercccae792015-01-30 22:02:31 +00008233 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo();
Bill Wendling5e7656b2013-06-07 07:55:53 +00008234 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00008235
8236 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
8237
8238 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
8239 .addMBB(mainMBB);
8240 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
8241
8242 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
8243 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
8244
8245 // mainMBB:
8246 // mainDstReg = 0
Eric Christophercccae792015-01-30 22:02:31 +00008247 MIB =
8248 BuildMI(mainMBB, DL,
8249 TII->get(Subtarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
Hal Finkel756810f2013-03-21 21:37:52 +00008250
8251 // Store IP
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008252 if (Subtarget.isPPC64()) {
Hal Finkel756810f2013-03-21 21:37:52 +00008253 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
8254 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008255 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008256 .addReg(BufReg);
8257 } else {
8258 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
8259 .addReg(LabelReg)
8260 .addImm(LabelOffset)
8261 .addReg(BufReg);
8262 }
8263
8264 MIB.setMemRefs(MMOBegin, MMOEnd);
8265
8266 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
8267 mainMBB->addSuccessor(sinkMBB);
8268
8269 // sinkMBB:
8270 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
8271 TII->get(PPC::PHI), DstReg)
8272 .addReg(mainDstReg).addMBB(mainMBB)
8273 .addReg(restoreDstReg).addMBB(thisMBB);
8274
8275 MI->eraseFromParent();
8276 return sinkMBB;
8277}
8278
8279MachineBasicBlock *
8280PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
8281 MachineBasicBlock *MBB) const {
8282 DebugLoc DL = MI->getDebugLoc();
Eric Christophercccae792015-01-30 22:02:31 +00008283 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00008284
8285 MachineFunction *MF = MBB->getParent();
8286 MachineRegisterInfo &MRI = MF->getRegInfo();
8287
8288 // Memory Reference
8289 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
8290 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
8291
8292 MVT PVT = getPointerTy();
8293 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
8294 "Invalid Pointer Size!");
8295
8296 const TargetRegisterClass *RC =
8297 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
8298 unsigned Tmp = MRI.createVirtualRegister(RC);
8299 // Since FP is only updated here but NOT referenced, it's treated as GPR.
8300 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
8301 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Eric Christophercccae792015-01-30 22:02:31 +00008302 unsigned BP =
8303 (PVT == MVT::i64)
8304 ? PPC::X30
8305 : (Subtarget.isSVR4ABI() &&
8306 MF->getTarget().getRelocationModel() == Reloc::PIC_
8307 ? PPC::R29
8308 : PPC::R30);
Hal Finkel756810f2013-03-21 21:37:52 +00008309
8310 MachineInstrBuilder MIB;
8311
8312 const int64_t LabelOffset = 1 * PVT.getStoreSize();
8313 const int64_t SPOffset = 2 * PVT.getStoreSize();
8314 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00008315 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00008316
8317 unsigned BufReg = MI->getOperand(0).getReg();
8318
8319 // Reload FP (the jumped-to function may not have had a
8320 // frame pointer, and if so, then its r31 will be restored
8321 // as necessary).
8322 if (PVT == MVT::i64) {
8323 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
8324 .addImm(0)
8325 .addReg(BufReg);
8326 } else {
8327 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
8328 .addImm(0)
8329 .addReg(BufReg);
8330 }
8331 MIB.setMemRefs(MMOBegin, MMOEnd);
8332
8333 // Reload IP
8334 if (PVT == MVT::i64) {
8335 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008336 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008337 .addReg(BufReg);
8338 } else {
8339 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
8340 .addImm(LabelOffset)
8341 .addReg(BufReg);
8342 }
8343 MIB.setMemRefs(MMOBegin, MMOEnd);
8344
8345 // Reload SP
8346 if (PVT == MVT::i64) {
8347 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008348 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008349 .addReg(BufReg);
8350 } else {
8351 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
8352 .addImm(SPOffset)
8353 .addReg(BufReg);
8354 }
8355 MIB.setMemRefs(MMOBegin, MMOEnd);
8356
Hal Finkelf05d6c72013-07-17 23:50:51 +00008357 // Reload BP
8358 if (PVT == MVT::i64) {
8359 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
8360 .addImm(BPOffset)
8361 .addReg(BufReg);
8362 } else {
8363 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
8364 .addImm(BPOffset)
8365 .addReg(BufReg);
8366 }
8367 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00008368
8369 // Reload TOC
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008370 if (PVT == MVT::i64 && Subtarget.isSVR4ABI()) {
Hal Finkele6698d52015-02-01 15:03:28 +00008371 setUsesTOCBasePtr(*MBB->getParent());
Hal Finkel756810f2013-03-21 21:37:52 +00008372 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008373 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008374 .addReg(BufReg);
8375
8376 MIB.setMemRefs(MMOBegin, MMOEnd);
8377 }
8378
8379 // Jump
8380 BuildMI(*MBB, MI, DL,
8381 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
8382 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
8383
8384 MI->eraseFromParent();
8385 return MBB;
8386}
8387
Dale Johannesena32affb2008-08-28 17:53:09 +00008388MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00008389PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00008390 MachineBasicBlock *BB) const {
Hal Finkel934361a2015-01-14 01:07:51 +00008391 if (MI->getOpcode() == TargetOpcode::STACKMAP ||
Hal Finkelaf519932015-01-19 07:20:27 +00008392 MI->getOpcode() == TargetOpcode::PATCHPOINT) {
8393 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI() &&
8394 MI->getOpcode() == TargetOpcode::PATCHPOINT) {
8395 // Call lowering should have added an r2 operand to indicate a dependence
8396 // on the TOC base pointer value. It can't however, because there is no
8397 // way to mark the dependence as implicit there, and so the stackmap code
8398 // will confuse it with a regular operand. Instead, add the dependence
8399 // here.
Hal Finkele6698d52015-02-01 15:03:28 +00008400 setUsesTOCBasePtr(*BB->getParent());
Hal Finkelaf519932015-01-19 07:20:27 +00008401 MI->addOperand(MachineOperand::CreateReg(PPC::X2, false, true));
8402 }
8403
Hal Finkel934361a2015-01-14 01:07:51 +00008404 return emitPatchPoint(MI, BB);
Hal Finkelaf519932015-01-19 07:20:27 +00008405 }
Hal Finkel934361a2015-01-14 01:07:51 +00008406
Hal Finkel756810f2013-03-21 21:37:52 +00008407 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
8408 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
8409 return emitEHSjLjSetJmp(MI, BB);
8410 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
8411 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
8412 return emitEHSjLjLongJmp(MI, BB);
8413 }
8414
Eric Christophercccae792015-01-30 22:02:31 +00008415 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00008416
8417 // To "insert" these instructions we actually have to insert their
8418 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00008419 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00008420 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00008421 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00008422
Dan Gohman3b460302008-07-07 23:14:23 +00008423 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00008424
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008425 if (Subtarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Eric Christophercccae792015-01-30 22:02:31 +00008426 MI->getOpcode() == PPC::SELECT_CC_I8 ||
8427 MI->getOpcode() == PPC::SELECT_I4 ||
8428 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00008429 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00008430 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
8431 MI->getOpcode() == PPC::SELECT_CC_I8)
8432 Cond.push_back(MI->getOperand(4));
8433 else
8434 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00008435 Cond.push_back(MI->getOperand(1));
8436
Hal Finkel460e94d2012-06-22 23:10:08 +00008437 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00008438 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
8439 Cond, MI->getOperand(2).getReg(),
8440 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00008441 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
8442 MI->getOpcode() == PPC::SELECT_CC_I8 ||
8443 MI->getOpcode() == PPC::SELECT_CC_F4 ||
8444 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008445 MI->getOpcode() == PPC::SELECT_CC_QFRC ||
8446 MI->getOpcode() == PPC::SELECT_CC_QSRC ||
8447 MI->getOpcode() == PPC::SELECT_CC_QBRC ||
Hal Finkel940ab932014-02-28 00:27:01 +00008448 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00008449 MI->getOpcode() == PPC::SELECT_CC_VSFRC ||
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00008450 MI->getOpcode() == PPC::SELECT_CC_VSSRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008451 MI->getOpcode() == PPC::SELECT_CC_VSRC ||
Hal Finkel940ab932014-02-28 00:27:01 +00008452 MI->getOpcode() == PPC::SELECT_I4 ||
8453 MI->getOpcode() == PPC::SELECT_I8 ||
8454 MI->getOpcode() == PPC::SELECT_F4 ||
8455 MI->getOpcode() == PPC::SELECT_F8 ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008456 MI->getOpcode() == PPC::SELECT_QFRC ||
8457 MI->getOpcode() == PPC::SELECT_QSRC ||
8458 MI->getOpcode() == PPC::SELECT_QBRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008459 MI->getOpcode() == PPC::SELECT_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00008460 MI->getOpcode() == PPC::SELECT_VSFRC ||
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00008461 MI->getOpcode() == PPC::SELECT_VSSRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008462 MI->getOpcode() == PPC::SELECT_VSRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00008463 // The incoming instruction knows the destination vreg to set, the
8464 // condition code register to branch on, the true/false values to
8465 // select between, and a branch opcode to use.
8466
8467 // thisMBB:
8468 // ...
8469 // TrueVal = ...
8470 // cmpTY ccX, r1, r2
8471 // bCC copy1MBB
8472 // fallthrough --> copy0MBB
8473 MachineBasicBlock *thisMBB = BB;
8474 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8475 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008476 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00008477 F->insert(It, copy0MBB);
8478 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008479
8480 // Transfer the remainder of BB and its successor edges to sinkMBB.
8481 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008482 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008483 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8484
Evan Cheng32e376f2008-07-12 02:23:19 +00008485 // Next, add the true and fallthrough blocks as its successors.
8486 BB->addSuccessor(copy0MBB);
8487 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008488
Hal Finkel940ab932014-02-28 00:27:01 +00008489 if (MI->getOpcode() == PPC::SELECT_I4 ||
8490 MI->getOpcode() == PPC::SELECT_I8 ||
8491 MI->getOpcode() == PPC::SELECT_F4 ||
8492 MI->getOpcode() == PPC::SELECT_F8 ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008493 MI->getOpcode() == PPC::SELECT_QFRC ||
8494 MI->getOpcode() == PPC::SELECT_QSRC ||
8495 MI->getOpcode() == PPC::SELECT_QBRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008496 MI->getOpcode() == PPC::SELECT_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00008497 MI->getOpcode() == PPC::SELECT_VSFRC ||
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00008498 MI->getOpcode() == PPC::SELECT_VSSRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008499 MI->getOpcode() == PPC::SELECT_VSRC) {
Hal Finkel940ab932014-02-28 00:27:01 +00008500 BuildMI(BB, dl, TII->get(PPC::BC))
8501 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
8502 } else {
8503 unsigned SelectPred = MI->getOperand(4).getImm();
8504 BuildMI(BB, dl, TII->get(PPC::BCC))
8505 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
8506 }
Dan Gohman34396292010-07-06 20:24:04 +00008507
Evan Cheng32e376f2008-07-12 02:23:19 +00008508 // copy0MBB:
8509 // %FalseValue = ...
8510 // # fallthrough to sinkMBB
8511 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008512
Evan Cheng32e376f2008-07-12 02:23:19 +00008513 // Update machine-CFG edges
8514 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008515
Evan Cheng32e376f2008-07-12 02:23:19 +00008516 // sinkMBB:
8517 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8518 // ...
8519 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00008520 BuildMI(*BB, BB->begin(), dl,
8521 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00008522 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
8523 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Hal Finkelbbdee932014-12-02 22:01:00 +00008524 } else if (MI->getOpcode() == PPC::ReadTB) {
8525 // To read the 64-bit time-base register on a 32-bit target, we read the
8526 // two halves. Should the counter have wrapped while it was being read, we
8527 // need to try again.
8528 // ...
8529 // readLoop:
8530 // mfspr Rx,TBU # load from TBU
8531 // mfspr Ry,TB # load from TB
8532 // mfspr Rz,TBU # load from TBU
8533 // cmpw crX,Rx,Rz # check if ‘old’=’new’
8534 // bne readLoop # branch if they're not equal
8535 // ...
8536
8537 MachineBasicBlock *readMBB = F->CreateMachineBasicBlock(LLVM_BB);
8538 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8539 DebugLoc dl = MI->getDebugLoc();
8540 F->insert(It, readMBB);
8541 F->insert(It, sinkMBB);
8542
8543 // Transfer the remainder of BB and its successor edges to sinkMBB.
8544 sinkMBB->splice(sinkMBB->begin(), BB,
8545 std::next(MachineBasicBlock::iterator(MI)), BB->end());
8546 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8547
8548 BB->addSuccessor(readMBB);
8549 BB = readMBB;
8550
8551 MachineRegisterInfo &RegInfo = F->getRegInfo();
8552 unsigned ReadAgainReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
8553 unsigned LoReg = MI->getOperand(0).getReg();
8554 unsigned HiReg = MI->getOperand(1).getReg();
8555
8556 BuildMI(BB, dl, TII->get(PPC::MFSPR), HiReg).addImm(269);
8557 BuildMI(BB, dl, TII->get(PPC::MFSPR), LoReg).addImm(268);
8558 BuildMI(BB, dl, TII->get(PPC::MFSPR), ReadAgainReg).addImm(269);
8559
8560 unsigned CmpReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
8561
8562 BuildMI(BB, dl, TII->get(PPC::CMPW), CmpReg)
8563 .addReg(HiReg).addReg(ReadAgainReg);
8564 BuildMI(BB, dl, TII->get(PPC::BCC))
8565 .addImm(PPC::PRED_NE).addReg(CmpReg).addMBB(readMBB);
8566
8567 BB->addSuccessor(readMBB);
8568 BB->addSuccessor(sinkMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00008569 }
Dale Johannesena32affb2008-08-28 17:53:09 +00008570 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
8571 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
8572 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
8573 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008574 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008575 BB = EmitAtomicBinary(MI, BB, 4, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008576 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008577 BB = EmitAtomicBinary(MI, BB, 8, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008578
8579 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
8580 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
8581 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
8582 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008583 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008584 BB = EmitAtomicBinary(MI, BB, 4, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008585 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008586 BB = EmitAtomicBinary(MI, BB, 8, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008587
8588 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
8589 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
8590 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
8591 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008592 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008593 BB = EmitAtomicBinary(MI, BB, 4, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008594 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008595 BB = EmitAtomicBinary(MI, BB, 8, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008596
8597 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
8598 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
8599 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
8600 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008601 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008602 BB = EmitAtomicBinary(MI, BB, 4, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008603 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008604 BB = EmitAtomicBinary(MI, BB, 8, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008605
8606 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00008607 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::NAND);
Dale Johannesena32affb2008-08-28 17:53:09 +00008608 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00008609 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008610 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008611 BB = EmitAtomicBinary(MI, BB, 4, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008612 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008613 BB = EmitAtomicBinary(MI, BB, 8, PPC::NAND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008614
8615 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
8616 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
8617 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
8618 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008619 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008620 BB = EmitAtomicBinary(MI, BB, 4, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008621 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008622 BB = EmitAtomicBinary(MI, BB, 8, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008623
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008624 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
8625 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
8626 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
8627 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
8628 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008629 BB = EmitAtomicBinary(MI, BB, 4, 0);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008630 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008631 BB = EmitAtomicBinary(MI, BB, 8, 0);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008632
Evan Cheng32e376f2008-07-12 02:23:19 +00008633 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008634 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64 ||
8635 (Subtarget.hasPartwordAtomics() &&
8636 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8) ||
8637 (Subtarget.hasPartwordAtomics() &&
8638 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16)) {
Evan Cheng32e376f2008-07-12 02:23:19 +00008639 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
8640
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008641 auto LoadMnemonic = PPC::LDARX;
8642 auto StoreMnemonic = PPC::STDCX;
8643 switch(MI->getOpcode()) {
8644 default:
8645 llvm_unreachable("Compare and swap of unknown size");
8646 case PPC::ATOMIC_CMP_SWAP_I8:
8647 LoadMnemonic = PPC::LBARX;
8648 StoreMnemonic = PPC::STBCX;
8649 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics.");
8650 break;
8651 case PPC::ATOMIC_CMP_SWAP_I16:
8652 LoadMnemonic = PPC::LHARX;
8653 StoreMnemonic = PPC::STHCX;
8654 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics.");
8655 break;
8656 case PPC::ATOMIC_CMP_SWAP_I32:
8657 LoadMnemonic = PPC::LWARX;
8658 StoreMnemonic = PPC::STWCX;
8659 break;
8660 case PPC::ATOMIC_CMP_SWAP_I64:
8661 LoadMnemonic = PPC::LDARX;
8662 StoreMnemonic = PPC::STDCX;
8663 break;
8664 }
Evan Cheng32e376f2008-07-12 02:23:19 +00008665 unsigned dest = MI->getOperand(0).getReg();
8666 unsigned ptrA = MI->getOperand(1).getReg();
8667 unsigned ptrB = MI->getOperand(2).getReg();
8668 unsigned oldval = MI->getOperand(3).getReg();
8669 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00008670 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00008671
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008672 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
8673 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
8674 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00008675 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008676 F->insert(It, loop1MBB);
8677 F->insert(It, loop2MBB);
8678 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00008679 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008680 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008681 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008682 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00008683
8684 // thisMBB:
8685 // ...
8686 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008687 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00008688
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008689 // loop1MBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008690 // l[bhwd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008691 // cmp[wd] dest, oldval
8692 // bne- midMBB
8693 // loop2MBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008694 // st[bhwd]cx. newval, ptr
Evan Cheng32e376f2008-07-12 02:23:19 +00008695 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008696 // b exitBB
8697 // midMBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008698 // st[bhwd]cx. dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008699 // exitBB:
8700 BB = loop1MBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008701 BuildMI(BB, dl, TII->get(LoadMnemonic), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00008702 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008703 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00008704 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008705 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008706 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
8707 BB->addSuccessor(loop2MBB);
8708 BB->addSuccessor(midMBB);
8709
8710 BB = loop2MBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008711 BuildMI(BB, dl, TII->get(StoreMnemonic))
Evan Cheng32e376f2008-07-12 02:23:19 +00008712 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008713 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008714 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008715 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008716 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00008717 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008718
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008719 BB = midMBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008720 BuildMI(BB, dl, TII->get(StoreMnemonic))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008721 .addReg(dest).addReg(ptrA).addReg(ptrB);
8722 BB->addSuccessor(exitMBB);
8723
Evan Cheng32e376f2008-07-12 02:23:19 +00008724 // exitMBB:
8725 // ...
8726 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00008727 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
8728 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
8729 // We must use 64-bit registers for addresses when targeting 64-bit,
8730 // since we're actually doing arithmetic on them. Other registers
8731 // can be 32-bit.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008732 bool is64bit = Subtarget.isPPC64();
Dale Johannesen340d2642008-08-30 00:08:53 +00008733 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
8734
8735 unsigned dest = MI->getOperand(0).getReg();
8736 unsigned ptrA = MI->getOperand(1).getReg();
8737 unsigned ptrB = MI->getOperand(2).getReg();
8738 unsigned oldval = MI->getOperand(3).getReg();
8739 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00008740 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00008741
8742 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
8743 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
8744 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
8745 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
8746 F->insert(It, loop1MBB);
8747 F->insert(It, loop2MBB);
8748 F->insert(It, midMBB);
8749 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008750 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008751 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008752 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00008753
8754 MachineRegisterInfo &RegInfo = F->getRegInfo();
Craig Topper61e88f42014-11-21 05:58:21 +00008755 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass
8756 : &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00008757 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
8758 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
8759 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
8760 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
8761 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
8762 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
8763 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
8764 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
8765 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
8766 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
8767 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
8768 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
8769 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
8770 unsigned Ptr1Reg;
8771 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00008772 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00008773 // thisMBB:
8774 // ...
8775 // fallthrough --> loopMBB
8776 BB->addSuccessor(loop1MBB);
8777
8778 // The 4-byte load must be aligned, while a char or short may be
8779 // anywhere in the word. Hence all this nasty bookkeeping code.
8780 // add ptr1, ptrA, ptrB [copy if ptrA==0]
8781 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00008782 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00008783 // rlwinm ptr, ptr1, 0, 0, 29
8784 // slw newval2, newval, shift
8785 // slw oldval2, oldval,shift
8786 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
8787 // slw mask, mask2, shift
8788 // and newval3, newval2, mask
8789 // and oldval3, oldval2, mask
8790 // loop1MBB:
8791 // lwarx tmpDest, ptr
8792 // and tmp, tmpDest, mask
8793 // cmpw tmp, oldval3
8794 // bne- midMBB
8795 // loop2MBB:
8796 // andc tmp2, tmpDest, mask
8797 // or tmp4, tmp2, newval3
8798 // stwcx. tmp4, ptr
8799 // bne- loop1MBB
8800 // b exitBB
8801 // midMBB:
8802 // stwcx. tmpDest, ptr
8803 // exitBB:
8804 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008805 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00008806 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008807 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008808 .addReg(ptrA).addReg(ptrB);
8809 } else {
8810 Ptr1Reg = ptrB;
8811 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008812 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008813 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008814 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008815 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
8816 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008817 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008818 .addReg(Ptr1Reg).addImm(0).addImm(61);
8819 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00008820 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008821 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008822 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008823 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008824 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008825 .addReg(oldval).addReg(ShiftReg);
8826 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008827 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00008828 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00008829 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
8830 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
8831 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00008832 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008833 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008834 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008835 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008836 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008837 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008838 .addReg(OldVal2Reg).addReg(MaskReg);
8839
8840 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00008841 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008842 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008843 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
8844 .addReg(TmpDestReg).addReg(MaskReg);
8845 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00008846 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008847 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00008848 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
8849 BB->addSuccessor(loop2MBB);
8850 BB->addSuccessor(midMBB);
8851
8852 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00008853 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
8854 .addReg(TmpDestReg).addReg(MaskReg);
8855 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
8856 .addReg(Tmp2Reg).addReg(NewVal3Reg);
8857 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008858 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008859 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00008860 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008861 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00008862 BB->addSuccessor(loop1MBB);
8863 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008864
Dale Johannesen340d2642008-08-30 00:08:53 +00008865 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00008866 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008867 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00008868 BB->addSuccessor(exitMBB);
8869
8870 // exitMBB:
8871 // ...
8872 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00008873 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
8874 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00008875 } else if (MI->getOpcode() == PPC::FADDrtz) {
8876 // This pseudo performs an FADD with rounding mode temporarily forced
8877 // to round-to-zero. We emit this via custom inserter since the FPSCR
8878 // is not modeled at the SelectionDAG level.
8879 unsigned Dest = MI->getOperand(0).getReg();
8880 unsigned Src1 = MI->getOperand(1).getReg();
8881 unsigned Src2 = MI->getOperand(2).getReg();
8882 DebugLoc dl = MI->getDebugLoc();
8883
8884 MachineRegisterInfo &RegInfo = F->getRegInfo();
8885 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
8886
8887 // Save FPSCR value.
8888 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
8889
8890 // Set rounding mode to round-to-zero.
8891 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
8892 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
8893
8894 // Perform addition.
8895 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
8896
8897 // Restore FPSCR value.
Hal Finkel64202162015-01-15 01:00:53 +00008898 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSFb)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00008899 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
8900 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
8901 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
8902 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
8903 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
8904 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
8905 PPC::ANDIo8 : PPC::ANDIo;
8906 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
8907 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
8908
8909 MachineRegisterInfo &RegInfo = F->getRegInfo();
8910 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
8911 &PPC::GPRCRegClass :
8912 &PPC::G8RCRegClass);
8913
8914 DebugLoc dl = MI->getDebugLoc();
8915 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
8916 .addReg(MI->getOperand(1).getReg()).addImm(1);
8917 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
8918 MI->getOperand(0).getReg())
8919 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Kit Barton535e69d2015-03-25 19:36:23 +00008920 } else if (MI->getOpcode() == PPC::TCHECK_RET) {
8921 DebugLoc Dl = MI->getDebugLoc();
8922 MachineRegisterInfo &RegInfo = F->getRegInfo();
8923 unsigned CRReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
8924 BuildMI(*BB, MI, Dl, TII->get(PPC::TCHECK), CRReg);
8925 return BB;
Dale Johannesen340d2642008-08-30 00:08:53 +00008926 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008927 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00008928 }
Chris Lattner9b577f12005-08-26 21:23:58 +00008929
Dan Gohman34396292010-07-06 20:24:04 +00008930 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00008931 return BB;
8932}
8933
Chris Lattner4211ca92006-04-14 06:01:58 +00008934//===----------------------------------------------------------------------===//
8935// Target Optimization Hooks
8936//===----------------------------------------------------------------------===//
8937
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008938SDValue PPCTargetLowering::getRsqrtEstimate(SDValue Operand,
8939 DAGCombinerInfo &DCI,
Sanjay Patel957efc232014-10-24 17:02:16 +00008940 unsigned &RefinementSteps,
8941 bool &UseOneConstNR) const {
Sanjay Patelbdf1e382014-09-26 23:01:47 +00008942 EVT VT = Operand.getValueType();
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008943 if ((VT == MVT::f32 && Subtarget.hasFRSQRTES()) ||
Eric Christophercccae792015-01-30 22:02:31 +00008944 (VT == MVT::f64 && Subtarget.hasFRSQRTE()) ||
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008945 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008946 (VT == MVT::v2f64 && Subtarget.hasVSX()) ||
8947 (VT == MVT::v4f32 && Subtarget.hasQPX()) ||
8948 (VT == MVT::v4f64 && Subtarget.hasQPX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00008949 // Convergence is quadratic, so we essentially double the number of digits
Sanjay Patelbdf1e382014-09-26 23:01:47 +00008950 // correct after every iteration. For both FRE and FRSQRTE, the minimum
8951 // architected relative accuracy is 2^-5. When hasRecipPrec(), this is
8952 // 2^-14. IEEE float has 23 digits and double has 52 digits.
8953 RefinementSteps = Subtarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00008954 if (VT.getScalarType() == MVT::f64)
Sanjay Patelbdf1e382014-09-26 23:01:47 +00008955 ++RefinementSteps;
Sanjay Patel957efc232014-10-24 17:02:16 +00008956 UseOneConstNR = true;
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008957 return DCI.DAG.getNode(PPCISD::FRSQRTE, SDLoc(Operand), VT, Operand);
Hal Finkel2e103312013-04-03 04:01:11 +00008958 }
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008959 return SDValue();
8960}
8961
8962SDValue PPCTargetLowering::getRecipEstimate(SDValue Operand,
8963 DAGCombinerInfo &DCI,
8964 unsigned &RefinementSteps) const {
8965 EVT VT = Operand.getValueType();
8966 if ((VT == MVT::f32 && Subtarget.hasFRES()) ||
Eric Christophercccae792015-01-30 22:02:31 +00008967 (VT == MVT::f64 && Subtarget.hasFRE()) ||
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008968 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008969 (VT == MVT::v2f64 && Subtarget.hasVSX()) ||
8970 (VT == MVT::v4f32 && Subtarget.hasQPX()) ||
8971 (VT == MVT::v4f64 && Subtarget.hasQPX())) {
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008972 // Convergence is quadratic, so we essentially double the number of digits
8973 // correct after every iteration. For both FRE and FRSQRTE, the minimum
8974 // architected relative accuracy is 2^-5. When hasRecipPrec(), this is
8975 // 2^-14. IEEE float has 23 digits and double has 52 digits.
8976 RefinementSteps = Subtarget.hasRecipPrec() ? 1 : 3;
8977 if (VT.getScalarType() == MVT::f64)
8978 ++RefinementSteps;
8979 return DCI.DAG.getNode(PPCISD::FRE, SDLoc(Operand), VT, Operand);
8980 }
8981 return SDValue();
Hal Finkel2e103312013-04-03 04:01:11 +00008982}
8983
Hal Finkel360f2132014-11-24 23:45:21 +00008984bool PPCTargetLowering::combineRepeatedFPDivisors(unsigned NumUsers) const {
8985 // Note: This functionality is used only when unsafe-fp-math is enabled, and
8986 // on cores with reciprocal estimates (which are used when unsafe-fp-math is
8987 // enabled for division), this functionality is redundant with the default
8988 // combiner logic (once the division -> reciprocal/multiply transformation
8989 // has taken place). As a result, this matters more for older cores than for
8990 // newer ones.
8991
8992 // Combine multiple FDIVs with the same divisor into multiple FMULs by the
8993 // reciprocal if there are two or more FDIVs (for embedded cores with only
8994 // one FP pipeline) for three or more FDIVs (for generic OOO cores).
8995 switch (Subtarget.getDarwinDirective()) {
8996 default:
8997 return NumUsers > 2;
8998 case PPC::DIR_440:
8999 case PPC::DIR_A2:
9000 case PPC::DIR_E500mc:
9001 case PPC::DIR_E5500:
9002 return NumUsers > 1;
9003 }
9004}
9005
Hal Finkel3604bf72014-08-01 01:02:01 +00009006static bool isConsecutiveLSLoc(SDValue Loc, EVT VT, LSBaseSDNode *Base,
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009007 unsigned Bytes, int Dist,
9008 SelectionDAG &DAG) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009009 if (VT.getSizeInBits() / 8 != Bytes)
9010 return false;
9011
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009012 SDValue BaseLoc = Base->getBasePtr();
9013 if (Loc.getOpcode() == ISD::FrameIndex) {
9014 if (BaseLoc.getOpcode() != ISD::FrameIndex)
9015 return false;
9016 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9017 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
9018 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
9019 int FS = MFI->getObjectSize(FI);
9020 int BFS = MFI->getObjectSize(BFI);
9021 if (FS != BFS || FS != (int)Bytes) return false;
9022 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
9023 }
9024
9025 // Handle X+C
9026 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
9027 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
9028 return true;
9029
9030 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Craig Topper062a2ba2014-04-25 05:30:21 +00009031 const GlobalValue *GV1 = nullptr;
9032 const GlobalValue *GV2 = nullptr;
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009033 int64_t Offset1 = 0;
9034 int64_t Offset2 = 0;
9035 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
9036 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
9037 if (isGA1 && isGA2 && GV1 == GV2)
9038 return Offset1 == (Offset2 + Dist*Bytes);
9039 return false;
9040}
9041
Hal Finkel3604bf72014-08-01 01:02:01 +00009042// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
9043// not enforce equality of the chain operands.
9044static bool isConsecutiveLS(SDNode *N, LSBaseSDNode *Base,
9045 unsigned Bytes, int Dist,
9046 SelectionDAG &DAG) {
9047 if (LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(N)) {
9048 EVT VT = LS->getMemoryVT();
9049 SDValue Loc = LS->getBasePtr();
9050 return isConsecutiveLSLoc(Loc, VT, Base, Bytes, Dist, DAG);
9051 }
9052
9053 if (N->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
9054 EVT VT;
9055 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9056 default: return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00009057 case Intrinsic::ppc_qpx_qvlfd:
9058 case Intrinsic::ppc_qpx_qvlfda:
9059 VT = MVT::v4f64;
9060 break;
9061 case Intrinsic::ppc_qpx_qvlfs:
9062 case Intrinsic::ppc_qpx_qvlfsa:
9063 VT = MVT::v4f32;
9064 break;
9065 case Intrinsic::ppc_qpx_qvlfcd:
9066 case Intrinsic::ppc_qpx_qvlfcda:
9067 VT = MVT::v2f64;
9068 break;
9069 case Intrinsic::ppc_qpx_qvlfcs:
9070 case Intrinsic::ppc_qpx_qvlfcsa:
9071 VT = MVT::v2f32;
9072 break;
9073 case Intrinsic::ppc_qpx_qvlfiwa:
9074 case Intrinsic::ppc_qpx_qvlfiwz:
Hal Finkel3604bf72014-08-01 01:02:01 +00009075 case Intrinsic::ppc_altivec_lvx:
9076 case Intrinsic::ppc_altivec_lvxl:
Bill Schmidt72954782014-11-12 04:19:40 +00009077 case Intrinsic::ppc_vsx_lxvw4x:
Hal Finkel3604bf72014-08-01 01:02:01 +00009078 VT = MVT::v4i32;
9079 break;
Bill Schmidt72954782014-11-12 04:19:40 +00009080 case Intrinsic::ppc_vsx_lxvd2x:
9081 VT = MVT::v2f64;
9082 break;
Hal Finkel3604bf72014-08-01 01:02:01 +00009083 case Intrinsic::ppc_altivec_lvebx:
9084 VT = MVT::i8;
9085 break;
9086 case Intrinsic::ppc_altivec_lvehx:
9087 VT = MVT::i16;
9088 break;
9089 case Intrinsic::ppc_altivec_lvewx:
9090 VT = MVT::i32;
9091 break;
9092 }
9093
9094 return isConsecutiveLSLoc(N->getOperand(2), VT, Base, Bytes, Dist, DAG);
9095 }
9096
9097 if (N->getOpcode() == ISD::INTRINSIC_VOID) {
9098 EVT VT;
9099 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9100 default: return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00009101 case Intrinsic::ppc_qpx_qvstfd:
9102 case Intrinsic::ppc_qpx_qvstfda:
9103 VT = MVT::v4f64;
9104 break;
9105 case Intrinsic::ppc_qpx_qvstfs:
9106 case Intrinsic::ppc_qpx_qvstfsa:
9107 VT = MVT::v4f32;
9108 break;
9109 case Intrinsic::ppc_qpx_qvstfcd:
9110 case Intrinsic::ppc_qpx_qvstfcda:
9111 VT = MVT::v2f64;
9112 break;
9113 case Intrinsic::ppc_qpx_qvstfcs:
9114 case Intrinsic::ppc_qpx_qvstfcsa:
9115 VT = MVT::v2f32;
9116 break;
9117 case Intrinsic::ppc_qpx_qvstfiw:
9118 case Intrinsic::ppc_qpx_qvstfiwa:
Hal Finkel3604bf72014-08-01 01:02:01 +00009119 case Intrinsic::ppc_altivec_stvx:
9120 case Intrinsic::ppc_altivec_stvxl:
Bill Schmidt72954782014-11-12 04:19:40 +00009121 case Intrinsic::ppc_vsx_stxvw4x:
Hal Finkel3604bf72014-08-01 01:02:01 +00009122 VT = MVT::v4i32;
9123 break;
Bill Schmidt72954782014-11-12 04:19:40 +00009124 case Intrinsic::ppc_vsx_stxvd2x:
9125 VT = MVT::v2f64;
9126 break;
Hal Finkel3604bf72014-08-01 01:02:01 +00009127 case Intrinsic::ppc_altivec_stvebx:
9128 VT = MVT::i8;
9129 break;
9130 case Intrinsic::ppc_altivec_stvehx:
9131 VT = MVT::i16;
9132 break;
9133 case Intrinsic::ppc_altivec_stvewx:
9134 VT = MVT::i32;
9135 break;
9136 }
9137
9138 return isConsecutiveLSLoc(N->getOperand(3), VT, Base, Bytes, Dist, DAG);
9139 }
9140
9141 return false;
9142}
9143
Hal Finkel7d8a6912013-05-26 18:08:30 +00009144// Return true is there is a nearyby consecutive load to the one provided
9145// (regardless of alignment). We search up and down the chain, looking though
Matt Arsenault57e74d22014-07-29 00:02:40 +00009146// token factors and other loads (but nothing else). As a result, a true result
9147// indicates that it is safe to create a new consecutive load adjacent to the
9148// load provided.
Hal Finkel7d8a6912013-05-26 18:08:30 +00009149static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
9150 SDValue Chain = LD->getChain();
9151 EVT VT = LD->getMemoryVT();
9152
9153 SmallSet<SDNode *, 16> LoadRoots;
9154 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
9155 SmallSet<SDNode *, 16> Visited;
9156
9157 // First, search up the chain, branching to follow all token-factor operands.
9158 // If we find a consecutive load, then we're done, otherwise, record all
9159 // nodes just above the top-level loads and token factors.
9160 while (!Queue.empty()) {
9161 SDNode *ChainNext = Queue.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00009162 if (!Visited.insert(ChainNext).second)
Hal Finkel7d8a6912013-05-26 18:08:30 +00009163 continue;
9164
Hal Finkel3604bf72014-08-01 01:02:01 +00009165 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009166 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00009167 return true;
9168
9169 if (!Visited.count(ChainLD->getChain().getNode()))
9170 Queue.push_back(ChainLD->getChain().getNode());
9171 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
Craig Topper66e588b2014-06-29 00:40:57 +00009172 for (const SDUse &O : ChainNext->ops())
9173 if (!Visited.count(O.getNode()))
9174 Queue.push_back(O.getNode());
Hal Finkel7d8a6912013-05-26 18:08:30 +00009175 } else
9176 LoadRoots.insert(ChainNext);
9177 }
9178
9179 // Second, search down the chain, starting from the top-level nodes recorded
9180 // in the first phase. These top-level nodes are the nodes just above all
9181 // loads and token factors. Starting with their uses, recursively look though
9182 // all loads (just the chain uses) and token factors to find a consecutive
9183 // load.
9184 Visited.clear();
9185 Queue.clear();
9186
9187 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
9188 IE = LoadRoots.end(); I != IE; ++I) {
9189 Queue.push_back(*I);
9190
9191 while (!Queue.empty()) {
9192 SDNode *LoadRoot = Queue.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00009193 if (!Visited.insert(LoadRoot).second)
Hal Finkel7d8a6912013-05-26 18:08:30 +00009194 continue;
9195
Hal Finkel3604bf72014-08-01 01:02:01 +00009196 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009197 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00009198 return true;
9199
9200 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
9201 UE = LoadRoot->use_end(); UI != UE; ++UI)
Hal Finkel3604bf72014-08-01 01:02:01 +00009202 if (((isa<MemSDNode>(*UI) &&
9203 cast<MemSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
Hal Finkel7d8a6912013-05-26 18:08:30 +00009204 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
9205 Queue.push_back(*UI);
9206 }
9207 }
9208
9209 return false;
9210}
9211
Hal Finkel940ab932014-02-28 00:27:01 +00009212SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
9213 DAGCombinerInfo &DCI) const {
9214 SelectionDAG &DAG = DCI.DAG;
9215 SDLoc dl(N);
9216
Eric Christophercccae792015-01-30 22:02:31 +00009217 assert(Subtarget.useCRBits() && "Expecting to be tracking CR bits");
Hal Finkel940ab932014-02-28 00:27:01 +00009218 // If we're tracking CR bits, we need to be careful that we don't have:
9219 // trunc(binary-ops(zext(x), zext(y)))
9220 // or
9221 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
9222 // such that we're unnecessarily moving things into GPRs when it would be
9223 // better to keep them in CR bits.
9224
9225 // Note that trunc here can be an actual i1 trunc, or can be the effective
9226 // truncation that comes from a setcc or select_cc.
9227 if (N->getOpcode() == ISD::TRUNCATE &&
9228 N->getValueType(0) != MVT::i1)
9229 return SDValue();
9230
9231 if (N->getOperand(0).getValueType() != MVT::i32 &&
9232 N->getOperand(0).getValueType() != MVT::i64)
9233 return SDValue();
9234
9235 if (N->getOpcode() == ISD::SETCC ||
9236 N->getOpcode() == ISD::SELECT_CC) {
9237 // If we're looking at a comparison, then we need to make sure that the
9238 // high bits (all except for the first) don't matter the result.
9239 ISD::CondCode CC =
9240 cast<CondCodeSDNode>(N->getOperand(
9241 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
9242 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
9243
9244 if (ISD::isSignedIntSetCC(CC)) {
9245 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
9246 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
9247 return SDValue();
9248 } else if (ISD::isUnsignedIntSetCC(CC)) {
9249 if (!DAG.MaskedValueIsZero(N->getOperand(0),
9250 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
9251 !DAG.MaskedValueIsZero(N->getOperand(1),
9252 APInt::getHighBitsSet(OpBits, OpBits-1)))
9253 return SDValue();
9254 } else {
9255 // This is neither a signed nor an unsigned comparison, just make sure
9256 // that the high bits are equal.
9257 APInt Op1Zero, Op1One;
9258 APInt Op2Zero, Op2One;
Jay Foada0653a32014-05-14 21:14:37 +00009259 DAG.computeKnownBits(N->getOperand(0), Op1Zero, Op1One);
9260 DAG.computeKnownBits(N->getOperand(1), Op2Zero, Op2One);
Hal Finkel940ab932014-02-28 00:27:01 +00009261
9262 // We don't really care about what is known about the first bit (if
9263 // anything), so clear it in all masks prior to comparing them.
9264 Op1Zero.clearBit(0); Op1One.clearBit(0);
9265 Op2Zero.clearBit(0); Op2One.clearBit(0);
9266
9267 if (Op1Zero != Op2Zero || Op1One != Op2One)
9268 return SDValue();
9269 }
9270 }
9271
9272 // We now know that the higher-order bits are irrelevant, we just need to
9273 // make sure that all of the intermediate operations are bit operations, and
9274 // all inputs are extensions.
9275 if (N->getOperand(0).getOpcode() != ISD::AND &&
9276 N->getOperand(0).getOpcode() != ISD::OR &&
9277 N->getOperand(0).getOpcode() != ISD::XOR &&
9278 N->getOperand(0).getOpcode() != ISD::SELECT &&
9279 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
9280 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
9281 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
9282 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
9283 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
9284 return SDValue();
9285
9286 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
9287 N->getOperand(1).getOpcode() != ISD::AND &&
9288 N->getOperand(1).getOpcode() != ISD::OR &&
9289 N->getOperand(1).getOpcode() != ISD::XOR &&
9290 N->getOperand(1).getOpcode() != ISD::SELECT &&
9291 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
9292 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
9293 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
9294 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
9295 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
9296 return SDValue();
9297
9298 SmallVector<SDValue, 4> Inputs;
9299 SmallVector<SDValue, 8> BinOps, PromOps;
9300 SmallPtrSet<SDNode *, 16> Visited;
9301
9302 for (unsigned i = 0; i < 2; ++i) {
9303 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9304 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9305 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
9306 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
9307 isa<ConstantSDNode>(N->getOperand(i)))
9308 Inputs.push_back(N->getOperand(i));
9309 else
9310 BinOps.push_back(N->getOperand(i));
9311
9312 if (N->getOpcode() == ISD::TRUNCATE)
9313 break;
9314 }
9315
9316 // Visit all inputs, collect all binary operations (and, or, xor and
9317 // select) that are all fed by extensions.
9318 while (!BinOps.empty()) {
9319 SDValue BinOp = BinOps.back();
9320 BinOps.pop_back();
9321
David Blaikie70573dc2014-11-19 07:49:26 +00009322 if (!Visited.insert(BinOp.getNode()).second)
Hal Finkel940ab932014-02-28 00:27:01 +00009323 continue;
9324
9325 PromOps.push_back(BinOp);
9326
9327 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
9328 // The condition of the select is not promoted.
9329 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
9330 continue;
9331 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
9332 continue;
9333
9334 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9335 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9336 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
9337 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
9338 isa<ConstantSDNode>(BinOp.getOperand(i))) {
9339 Inputs.push_back(BinOp.getOperand(i));
9340 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
9341 BinOp.getOperand(i).getOpcode() == ISD::OR ||
9342 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
9343 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
9344 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
9345 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
9346 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9347 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9348 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
9349 BinOps.push_back(BinOp.getOperand(i));
9350 } else {
9351 // We have an input that is not an extension or another binary
9352 // operation; we'll abort this transformation.
9353 return SDValue();
9354 }
9355 }
9356 }
9357
9358 // Make sure that this is a self-contained cluster of operations (which
9359 // is not quite the same thing as saying that everything has only one
9360 // use).
9361 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9362 if (isa<ConstantSDNode>(Inputs[i]))
9363 continue;
9364
9365 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
9366 UE = Inputs[i].getNode()->use_end();
9367 UI != UE; ++UI) {
9368 SDNode *User = *UI;
9369 if (User != N && !Visited.count(User))
9370 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00009371
9372 // Make sure that we're not going to promote the non-output-value
9373 // operand(s) or SELECT or SELECT_CC.
9374 // FIXME: Although we could sometimes handle this, and it does occur in
9375 // practice that one of the condition inputs to the select is also one of
9376 // the outputs, we currently can't deal with this.
9377 if (User->getOpcode() == ISD::SELECT) {
9378 if (User->getOperand(0) == Inputs[i])
9379 return SDValue();
9380 } else if (User->getOpcode() == ISD::SELECT_CC) {
9381 if (User->getOperand(0) == Inputs[i] ||
9382 User->getOperand(1) == Inputs[i])
9383 return SDValue();
9384 }
Hal Finkel940ab932014-02-28 00:27:01 +00009385 }
9386 }
9387
9388 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
9389 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
9390 UE = PromOps[i].getNode()->use_end();
9391 UI != UE; ++UI) {
9392 SDNode *User = *UI;
9393 if (User != N && !Visited.count(User))
9394 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00009395
9396 // Make sure that we're not going to promote the non-output-value
9397 // operand(s) or SELECT or SELECT_CC.
9398 // FIXME: Although we could sometimes handle this, and it does occur in
9399 // practice that one of the condition inputs to the select is also one of
9400 // the outputs, we currently can't deal with this.
9401 if (User->getOpcode() == ISD::SELECT) {
9402 if (User->getOperand(0) == PromOps[i])
9403 return SDValue();
9404 } else if (User->getOpcode() == ISD::SELECT_CC) {
9405 if (User->getOperand(0) == PromOps[i] ||
9406 User->getOperand(1) == PromOps[i])
9407 return SDValue();
9408 }
Hal Finkel940ab932014-02-28 00:27:01 +00009409 }
9410 }
9411
9412 // Replace all inputs with the extension operand.
9413 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9414 // Constants may have users outside the cluster of to-be-promoted nodes,
9415 // and so we need to replace those as we do the promotions.
9416 if (isa<ConstantSDNode>(Inputs[i]))
9417 continue;
9418 else
9419 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
9420 }
9421
9422 // Replace all operations (these are all the same, but have a different
9423 // (i1) return type). DAG.getNode will validate that the types of
9424 // a binary operator match, so go through the list in reverse so that
9425 // we've likely promoted both operands first. Any intermediate truncations or
9426 // extensions disappear.
9427 while (!PromOps.empty()) {
9428 SDValue PromOp = PromOps.back();
9429 PromOps.pop_back();
9430
9431 if (PromOp.getOpcode() == ISD::TRUNCATE ||
9432 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
9433 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
9434 PromOp.getOpcode() == ISD::ANY_EXTEND) {
9435 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
9436 PromOp.getOperand(0).getValueType() != MVT::i1) {
9437 // The operand is not yet ready (see comment below).
9438 PromOps.insert(PromOps.begin(), PromOp);
9439 continue;
9440 }
9441
9442 SDValue RepValue = PromOp.getOperand(0);
9443 if (isa<ConstantSDNode>(RepValue))
9444 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
9445
9446 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
9447 continue;
9448 }
9449
9450 unsigned C;
9451 switch (PromOp.getOpcode()) {
9452 default: C = 0; break;
9453 case ISD::SELECT: C = 1; break;
9454 case ISD::SELECT_CC: C = 2; break;
9455 }
9456
9457 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
9458 PromOp.getOperand(C).getValueType() != MVT::i1) ||
9459 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
9460 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
9461 // The to-be-promoted operands of this node have not yet been
9462 // promoted (this should be rare because we're going through the
9463 // list backward, but if one of the operands has several users in
9464 // this cluster of to-be-promoted nodes, it is possible).
9465 PromOps.insert(PromOps.begin(), PromOp);
9466 continue;
9467 }
9468
9469 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
9470 PromOp.getNode()->op_end());
9471
9472 // If there are any constant inputs, make sure they're replaced now.
9473 for (unsigned i = 0; i < 2; ++i)
9474 if (isa<ConstantSDNode>(Ops[C+i]))
9475 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
9476
9477 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00009478 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00009479 }
9480
9481 // Now we're left with the initial truncation itself.
9482 if (N->getOpcode() == ISD::TRUNCATE)
9483 return N->getOperand(0);
9484
9485 // Otherwise, this is a comparison. The operands to be compared have just
9486 // changed type (to i1), but everything else is the same.
9487 return SDValue(N, 0);
9488}
9489
9490SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
9491 DAGCombinerInfo &DCI) const {
9492 SelectionDAG &DAG = DCI.DAG;
9493 SDLoc dl(N);
9494
Hal Finkel940ab932014-02-28 00:27:01 +00009495 // If we're tracking CR bits, we need to be careful that we don't have:
9496 // zext(binary-ops(trunc(x), trunc(y)))
9497 // or
9498 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
9499 // such that we're unnecessarily moving things into CR bits that can more
9500 // efficiently stay in GPRs. Note that if we're not certain that the high
9501 // bits are set as required by the final extension, we still may need to do
9502 // some masking to get the proper behavior.
9503
Hal Finkel46043ed2014-03-01 21:36:57 +00009504 // This same functionality is important on PPC64 when dealing with
9505 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
9506 // the return values of functions. Because it is so similar, it is handled
9507 // here as well.
9508
Hal Finkel940ab932014-02-28 00:27:01 +00009509 if (N->getValueType(0) != MVT::i32 &&
9510 N->getValueType(0) != MVT::i64)
9511 return SDValue();
9512
Eric Christophercccae792015-01-30 22:02:31 +00009513 if (!((N->getOperand(0).getValueType() == MVT::i1 && Subtarget.useCRBits()) ||
9514 (N->getOperand(0).getValueType() == MVT::i32 && Subtarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +00009515 return SDValue();
9516
9517 if (N->getOperand(0).getOpcode() != ISD::AND &&
9518 N->getOperand(0).getOpcode() != ISD::OR &&
9519 N->getOperand(0).getOpcode() != ISD::XOR &&
9520 N->getOperand(0).getOpcode() != ISD::SELECT &&
9521 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
9522 return SDValue();
9523
9524 SmallVector<SDValue, 4> Inputs;
9525 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
9526 SmallPtrSet<SDNode *, 16> Visited;
9527
9528 // Visit all inputs, collect all binary operations (and, or, xor and
9529 // select) that are all fed by truncations.
9530 while (!BinOps.empty()) {
9531 SDValue BinOp = BinOps.back();
9532 BinOps.pop_back();
9533
David Blaikie70573dc2014-11-19 07:49:26 +00009534 if (!Visited.insert(BinOp.getNode()).second)
Hal Finkel940ab932014-02-28 00:27:01 +00009535 continue;
9536
9537 PromOps.push_back(BinOp);
9538
9539 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
9540 // The condition of the select is not promoted.
9541 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
9542 continue;
9543 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
9544 continue;
9545
9546 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
9547 isa<ConstantSDNode>(BinOp.getOperand(i))) {
9548 Inputs.push_back(BinOp.getOperand(i));
9549 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
9550 BinOp.getOperand(i).getOpcode() == ISD::OR ||
9551 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
9552 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
9553 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
9554 BinOps.push_back(BinOp.getOperand(i));
9555 } else {
9556 // We have an input that is not a truncation or another binary
9557 // operation; we'll abort this transformation.
9558 return SDValue();
9559 }
9560 }
9561 }
9562
Hal Finkel4104a1a2014-12-14 05:53:19 +00009563 // The operands of a select that must be truncated when the select is
9564 // promoted because the operand is actually part of the to-be-promoted set.
9565 DenseMap<SDNode *, EVT> SelectTruncOp[2];
9566
Hal Finkel940ab932014-02-28 00:27:01 +00009567 // Make sure that this is a self-contained cluster of operations (which
9568 // is not quite the same thing as saying that everything has only one
9569 // use).
9570 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9571 if (isa<ConstantSDNode>(Inputs[i]))
9572 continue;
9573
9574 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
9575 UE = Inputs[i].getNode()->use_end();
9576 UI != UE; ++UI) {
9577 SDNode *User = *UI;
9578 if (User != N && !Visited.count(User))
9579 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00009580
Hal Finkel4104a1a2014-12-14 05:53:19 +00009581 // If we're going to promote the non-output-value operand(s) or SELECT or
9582 // SELECT_CC, record them for truncation.
Hal Finkel46043ed2014-03-01 21:36:57 +00009583 if (User->getOpcode() == ISD::SELECT) {
9584 if (User->getOperand(0) == Inputs[i])
Hal Finkel4104a1a2014-12-14 05:53:19 +00009585 SelectTruncOp[0].insert(std::make_pair(User,
9586 User->getOperand(0).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +00009587 } else if (User->getOpcode() == ISD::SELECT_CC) {
Hal Finkel4104a1a2014-12-14 05:53:19 +00009588 if (User->getOperand(0) == Inputs[i])
9589 SelectTruncOp[0].insert(std::make_pair(User,
9590 User->getOperand(0).getValueType()));
9591 if (User->getOperand(1) == Inputs[i])
9592 SelectTruncOp[1].insert(std::make_pair(User,
9593 User->getOperand(1).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +00009594 }
Hal Finkel940ab932014-02-28 00:27:01 +00009595 }
9596 }
9597
9598 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
9599 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
9600 UE = PromOps[i].getNode()->use_end();
9601 UI != UE; ++UI) {
9602 SDNode *User = *UI;
9603 if (User != N && !Visited.count(User))
9604 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00009605
Hal Finkel4104a1a2014-12-14 05:53:19 +00009606 // If we're going to promote the non-output-value operand(s) or SELECT or
9607 // SELECT_CC, record them for truncation.
Hal Finkel46043ed2014-03-01 21:36:57 +00009608 if (User->getOpcode() == ISD::SELECT) {
9609 if (User->getOperand(0) == PromOps[i])
Hal Finkel4104a1a2014-12-14 05:53:19 +00009610 SelectTruncOp[0].insert(std::make_pair(User,
9611 User->getOperand(0).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +00009612 } else if (User->getOpcode() == ISD::SELECT_CC) {
Hal Finkel4104a1a2014-12-14 05:53:19 +00009613 if (User->getOperand(0) == PromOps[i])
9614 SelectTruncOp[0].insert(std::make_pair(User,
9615 User->getOperand(0).getValueType()));
9616 if (User->getOperand(1) == PromOps[i])
9617 SelectTruncOp[1].insert(std::make_pair(User,
9618 User->getOperand(1).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +00009619 }
Hal Finkel940ab932014-02-28 00:27:01 +00009620 }
9621 }
9622
Hal Finkel46043ed2014-03-01 21:36:57 +00009623 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +00009624 bool ReallyNeedsExt = false;
9625 if (N->getOpcode() != ISD::ANY_EXTEND) {
9626 // If all of the inputs are not already sign/zero extended, then
9627 // we'll still need to do that at the end.
9628 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9629 if (isa<ConstantSDNode>(Inputs[i]))
9630 continue;
9631
9632 unsigned OpBits =
9633 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +00009634 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
9635
Hal Finkel940ab932014-02-28 00:27:01 +00009636 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
9637 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00009638 APInt::getHighBitsSet(OpBits,
9639 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +00009640 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +00009641 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
9642 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +00009643 ReallyNeedsExt = true;
9644 break;
9645 }
9646 }
9647 }
9648
9649 // Replace all inputs, either with the truncation operand, or a
9650 // truncation or extension to the final output type.
9651 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9652 // Constant inputs need to be replaced with the to-be-promoted nodes that
9653 // use them because they might have users outside of the cluster of
9654 // promoted nodes.
9655 if (isa<ConstantSDNode>(Inputs[i]))
9656 continue;
9657
9658 SDValue InSrc = Inputs[i].getOperand(0);
9659 if (Inputs[i].getValueType() == N->getValueType(0))
9660 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
9661 else if (N->getOpcode() == ISD::SIGN_EXTEND)
9662 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
9663 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
9664 else if (N->getOpcode() == ISD::ZERO_EXTEND)
9665 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
9666 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
9667 else
9668 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
9669 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
9670 }
9671
9672 // Replace all operations (these are all the same, but have a different
9673 // (promoted) return type). DAG.getNode will validate that the types of
9674 // a binary operator match, so go through the list in reverse so that
9675 // we've likely promoted both operands first.
9676 while (!PromOps.empty()) {
9677 SDValue PromOp = PromOps.back();
9678 PromOps.pop_back();
9679
9680 unsigned C;
9681 switch (PromOp.getOpcode()) {
9682 default: C = 0; break;
9683 case ISD::SELECT: C = 1; break;
9684 case ISD::SELECT_CC: C = 2; break;
9685 }
9686
9687 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
9688 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
9689 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
9690 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
9691 // The to-be-promoted operands of this node have not yet been
9692 // promoted (this should be rare because we're going through the
9693 // list backward, but if one of the operands has several users in
9694 // this cluster of to-be-promoted nodes, it is possible).
9695 PromOps.insert(PromOps.begin(), PromOp);
9696 continue;
9697 }
9698
Hal Finkel4104a1a2014-12-14 05:53:19 +00009699 // For SELECT and SELECT_CC nodes, we do a similar check for any
9700 // to-be-promoted comparison inputs.
9701 if (PromOp.getOpcode() == ISD::SELECT ||
9702 PromOp.getOpcode() == ISD::SELECT_CC) {
9703 if ((SelectTruncOp[0].count(PromOp.getNode()) &&
9704 PromOp.getOperand(0).getValueType() != N->getValueType(0)) ||
9705 (SelectTruncOp[1].count(PromOp.getNode()) &&
9706 PromOp.getOperand(1).getValueType() != N->getValueType(0))) {
9707 PromOps.insert(PromOps.begin(), PromOp);
9708 continue;
9709 }
9710 }
9711
Hal Finkel940ab932014-02-28 00:27:01 +00009712 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
9713 PromOp.getNode()->op_end());
9714
9715 // If this node has constant inputs, then they'll need to be promoted here.
9716 for (unsigned i = 0; i < 2; ++i) {
9717 if (!isa<ConstantSDNode>(Ops[C+i]))
9718 continue;
9719 if (Ops[C+i].getValueType() == N->getValueType(0))
9720 continue;
9721
9722 if (N->getOpcode() == ISD::SIGN_EXTEND)
9723 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
9724 else if (N->getOpcode() == ISD::ZERO_EXTEND)
9725 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
9726 else
9727 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
9728 }
9729
Hal Finkel4104a1a2014-12-14 05:53:19 +00009730 // If we've promoted the comparison inputs of a SELECT or SELECT_CC,
9731 // truncate them again to the original value type.
9732 if (PromOp.getOpcode() == ISD::SELECT ||
9733 PromOp.getOpcode() == ISD::SELECT_CC) {
9734 auto SI0 = SelectTruncOp[0].find(PromOp.getNode());
9735 if (SI0 != SelectTruncOp[0].end())
9736 Ops[0] = DAG.getNode(ISD::TRUNCATE, dl, SI0->second, Ops[0]);
9737 auto SI1 = SelectTruncOp[1].find(PromOp.getNode());
9738 if (SI1 != SelectTruncOp[1].end())
9739 Ops[1] = DAG.getNode(ISD::TRUNCATE, dl, SI1->second, Ops[1]);
9740 }
9741
Hal Finkel940ab932014-02-28 00:27:01 +00009742 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00009743 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00009744 }
9745
9746 // Now we're left with the initial extension itself.
9747 if (!ReallyNeedsExt)
9748 return N->getOperand(0);
9749
Hal Finkel46043ed2014-03-01 21:36:57 +00009750 // To zero extend, just mask off everything except for the first bit (in the
9751 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +00009752 if (N->getOpcode() == ISD::ZERO_EXTEND)
9753 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00009754 DAG.getConstant(APInt::getLowBitsSet(
9755 N->getValueSizeInBits(0), PromBits),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009756 dl, N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +00009757
9758 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
9759 "Invalid extension type");
9760 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0));
9761 SDValue ShiftCst =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009762 DAG.getConstant(N->getValueSizeInBits(0) - PromBits, dl, ShiftAmountTy);
Hal Finkel940ab932014-02-28 00:27:01 +00009763 return DAG.getNode(ISD::SRA, dl, N->getValueType(0),
9764 DAG.getNode(ISD::SHL, dl, N->getValueType(0),
9765 N->getOperand(0), ShiftCst), ShiftCst);
9766}
9767
Hal Finkel5efb9182015-01-06 06:01:57 +00009768SDValue PPCTargetLowering::combineFPToIntToFP(SDNode *N,
9769 DAGCombinerInfo &DCI) const {
9770 assert((N->getOpcode() == ISD::SINT_TO_FP ||
9771 N->getOpcode() == ISD::UINT_TO_FP) &&
9772 "Need an int -> FP conversion node here");
9773
9774 if (!Subtarget.has64BitSupport())
9775 return SDValue();
9776
9777 SelectionDAG &DAG = DCI.DAG;
9778 SDLoc dl(N);
9779 SDValue Op(N, 0);
9780
9781 // Don't handle ppc_fp128 here or i1 conversions.
9782 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
9783 return SDValue();
9784 if (Op.getOperand(0).getValueType() == MVT::i1)
9785 return SDValue();
9786
9787 // For i32 intermediate values, unfortunately, the conversion functions
9788 // leave the upper 32 bits of the value are undefined. Within the set of
9789 // scalar instructions, we have no method for zero- or sign-extending the
9790 // value. Thus, we cannot handle i32 intermediate values here.
9791 if (Op.getOperand(0).getValueType() == MVT::i32)
9792 return SDValue();
9793
9794 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
9795 "UINT_TO_FP is supported only with FPCVT");
9796
9797 // If we have FCFIDS, then use it when converting to single-precision.
9798 // Otherwise, convert to double-precision and then round.
Eric Christophercccae792015-01-30 22:02:31 +00009799 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
9800 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS
9801 : PPCISD::FCFIDS)
9802 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU
9803 : PPCISD::FCFID);
9804 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
9805 ? MVT::f32
9806 : MVT::f64;
Hal Finkel5efb9182015-01-06 06:01:57 +00009807
9808 // If we're converting from a float, to an int, and back to a float again,
9809 // then we don't need the store/load pair at all.
9810 if ((Op.getOperand(0).getOpcode() == ISD::FP_TO_UINT &&
9811 Subtarget.hasFPCVT()) ||
9812 (Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT)) {
9813 SDValue Src = Op.getOperand(0).getOperand(0);
9814 if (Src.getValueType() == MVT::f32) {
9815 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
9816 DCI.AddToWorklist(Src.getNode());
9817 }
9818
9819 unsigned FCTOp =
9820 Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
9821 PPCISD::FCTIDUZ;
9822
9823 SDValue Tmp = DAG.getNode(FCTOp, dl, MVT::f64, Src);
9824 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Tmp);
9825
9826 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) {
9827 FP = DAG.getNode(ISD::FP_ROUND, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009828 MVT::f32, FP, DAG.getIntPtrConstant(0, dl));
Hal Finkel5efb9182015-01-06 06:01:57 +00009829 DCI.AddToWorklist(FP.getNode());
9830 }
9831
9832 return FP;
9833 }
9834
9835 return SDValue();
9836}
9837
Bill Schmidtfae5d712014-12-09 16:35:51 +00009838// expandVSXLoadForLE - Convert VSX loads (which may be intrinsics for
9839// builtins) into loads with swaps.
9840SDValue PPCTargetLowering::expandVSXLoadForLE(SDNode *N,
9841 DAGCombinerInfo &DCI) const {
9842 SelectionDAG &DAG = DCI.DAG;
9843 SDLoc dl(N);
9844 SDValue Chain;
9845 SDValue Base;
9846 MachineMemOperand *MMO;
9847
9848 switch (N->getOpcode()) {
9849 default:
9850 llvm_unreachable("Unexpected opcode for little endian VSX load");
9851 case ISD::LOAD: {
9852 LoadSDNode *LD = cast<LoadSDNode>(N);
9853 Chain = LD->getChain();
9854 Base = LD->getBasePtr();
9855 MMO = LD->getMemOperand();
9856 // If the MMO suggests this isn't a load of a full vector, leave
9857 // things alone. For a built-in, we have to make the change for
9858 // correctness, so if there is a size problem that will be a bug.
9859 if (MMO->getSize() < 16)
9860 return SDValue();
9861 break;
9862 }
9863 case ISD::INTRINSIC_W_CHAIN: {
9864 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
9865 Chain = Intrin->getChain();
9866 Base = Intrin->getBasePtr();
9867 MMO = Intrin->getMemOperand();
9868 break;
9869 }
9870 }
9871
9872 MVT VecTy = N->getValueType(0).getSimpleVT();
9873 SDValue LoadOps[] = { Chain, Base };
9874 SDValue Load = DAG.getMemIntrinsicNode(PPCISD::LXVD2X, dl,
9875 DAG.getVTList(VecTy, MVT::Other),
9876 LoadOps, VecTy, MMO);
9877 DCI.AddToWorklist(Load.getNode());
9878 Chain = Load.getValue(1);
9879 SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl,
9880 DAG.getVTList(VecTy, MVT::Other), Chain, Load);
9881 DCI.AddToWorklist(Swap.getNode());
9882 return Swap;
9883}
9884
9885// expandVSXStoreForLE - Convert VSX stores (which may be intrinsics for
9886// builtins) into stores with swaps.
9887SDValue PPCTargetLowering::expandVSXStoreForLE(SDNode *N,
9888 DAGCombinerInfo &DCI) const {
9889 SelectionDAG &DAG = DCI.DAG;
9890 SDLoc dl(N);
9891 SDValue Chain;
9892 SDValue Base;
9893 unsigned SrcOpnd;
9894 MachineMemOperand *MMO;
9895
9896 switch (N->getOpcode()) {
9897 default:
9898 llvm_unreachable("Unexpected opcode for little endian VSX store");
9899 case ISD::STORE: {
9900 StoreSDNode *ST = cast<StoreSDNode>(N);
9901 Chain = ST->getChain();
9902 Base = ST->getBasePtr();
9903 MMO = ST->getMemOperand();
9904 SrcOpnd = 1;
9905 // If the MMO suggests this isn't a store of a full vector, leave
9906 // things alone. For a built-in, we have to make the change for
9907 // correctness, so if there is a size problem that will be a bug.
9908 if (MMO->getSize() < 16)
9909 return SDValue();
9910 break;
9911 }
9912 case ISD::INTRINSIC_VOID: {
9913 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
9914 Chain = Intrin->getChain();
9915 // Intrin->getBasePtr() oddly does not get what we want.
9916 Base = Intrin->getOperand(3);
9917 MMO = Intrin->getMemOperand();
9918 SrcOpnd = 2;
9919 break;
9920 }
9921 }
9922
9923 SDValue Src = N->getOperand(SrcOpnd);
9924 MVT VecTy = Src.getValueType().getSimpleVT();
9925 SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl,
9926 DAG.getVTList(VecTy, MVT::Other), Chain, Src);
9927 DCI.AddToWorklist(Swap.getNode());
9928 Chain = Swap.getValue(1);
9929 SDValue StoreOps[] = { Chain, Swap, Base };
9930 SDValue Store = DAG.getMemIntrinsicNode(PPCISD::STXVD2X, dl,
9931 DAG.getVTList(MVT::Other),
9932 StoreOps, VecTy, MMO);
9933 DCI.AddToWorklist(Store.getNode());
9934 return Store;
9935}
9936
Duncan Sandsdc2dac12008-11-24 14:53:14 +00009937SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
9938 DAGCombinerInfo &DCI) const {
Chris Lattnerf4184352006-03-01 04:57:39 +00009939 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009940 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00009941 switch (N->getOpcode()) {
9942 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00009943 case PPCISD::SHL:
9944 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00009945 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00009946 return N->getOperand(0);
9947 }
9948 break;
9949 case PPCISD::SRL:
9950 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00009951 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00009952 return N->getOperand(0);
9953 }
9954 break;
9955 case PPCISD::SRA:
9956 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00009957 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00009958 C->isAllOnesValue()) // -1 >>s V -> -1.
9959 return N->getOperand(0);
9960 }
9961 break;
Hal Finkel940ab932014-02-28 00:27:01 +00009962 case ISD::SIGN_EXTEND:
9963 case ISD::ZERO_EXTEND:
9964 case ISD::ANY_EXTEND:
9965 return DAGCombineExtBoolTrunc(N, DCI);
9966 case ISD::TRUNCATE:
9967 case ISD::SETCC:
9968 case ISD::SELECT_CC:
9969 return DAGCombineTruncBoolExt(N, DCI);
Chris Lattnerf4184352006-03-01 04:57:39 +00009970 case ISD::SINT_TO_FP:
Hal Finkel5efb9182015-01-06 06:01:57 +00009971 case ISD::UINT_TO_FP:
9972 return combineFPToIntToFP(N, DCI);
Bill Schmidtfae5d712014-12-09 16:35:51 +00009973 case ISD::STORE: {
Chris Lattner27f53452006-03-01 05:50:56 +00009974 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
Eric Christophercccae792015-01-30 22:02:31 +00009975 if (Subtarget.hasSTFIWX() && !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00009976 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00009977 N->getOperand(1).getValueType() == MVT::i32 &&
9978 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009979 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00009980 if (Val.getValueType() == MVT::f32) {
9981 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00009982 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00009983 }
Owen Anderson9f944592009-08-11 20:47:22 +00009984 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00009985 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00009986
Hal Finkel60c75102013-04-01 15:37:53 +00009987 SDValue Ops[] = {
9988 N->getOperand(0), Val, N->getOperand(2),
9989 DAG.getValueType(N->getOperand(1).getValueType())
9990 };
9991
9992 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00009993 DAG.getVTList(MVT::Other), Ops,
Hal Finkel60c75102013-04-01 15:37:53 +00009994 cast<StoreSDNode>(N)->getMemoryVT(),
9995 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00009996 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00009997 return Val;
9998 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009999
Chris Lattnera7976d32006-07-10 20:56:58 +000010000 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +000010001 if (cast<StoreSDNode>(N)->isUnindexed() &&
10002 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +000010003 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +000010004 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +000010005 N->getOperand(1).getValueType() == MVT::i16 ||
Eric Christophercccae792015-01-30 22:02:31 +000010006 (Subtarget.hasLDBRX() && Subtarget.isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +000010007 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010008 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +000010009 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +000010010 if (BSwapOp.getValueType() == MVT::i16)
10011 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +000010012
Dan Gohman48b185d2009-09-25 20:36:54 +000010013 SDValue Ops[] = {
10014 N->getOperand(0), BSwapOp, N->getOperand(2),
10015 DAG.getValueType(N->getOperand(1).getValueType())
10016 };
10017 return
10018 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +000010019 Ops, cast<StoreSDNode>(N)->getMemoryVT(),
Dan Gohman48b185d2009-09-25 20:36:54 +000010020 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +000010021 }
Bill Schmidtfae5d712014-12-09 16:35:51 +000010022
10023 // For little endian, VSX stores require generating xxswapd/lxvd2x.
10024 EVT VT = N->getOperand(1).getValueType();
10025 if (VT.isSimple()) {
10026 MVT StoreVT = VT.getSimpleVT();
Eric Christophercccae792015-01-30 22:02:31 +000010027 if (Subtarget.hasVSX() && Subtarget.isLittleEndian() &&
Bill Schmidtfae5d712014-12-09 16:35:51 +000010028 (StoreVT == MVT::v2f64 || StoreVT == MVT::v2i64 ||
10029 StoreVT == MVT::v4f32 || StoreVT == MVT::v4i32))
10030 return expandVSXStoreForLE(N, DCI);
10031 }
Chris Lattnera7976d32006-07-10 20:56:58 +000010032 break;
Bill Schmidtfae5d712014-12-09 16:35:51 +000010033 }
Hal Finkelcf2e9082013-05-24 23:00:14 +000010034 case ISD::LOAD: {
10035 LoadSDNode *LD = cast<LoadSDNode>(N);
10036 EVT VT = LD->getValueType(0);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010037
10038 // For little endian, VSX loads require generating lxvd2x/xxswapd.
10039 if (VT.isSimple()) {
10040 MVT LoadVT = VT.getSimpleVT();
Eric Christophercccae792015-01-30 22:02:31 +000010041 if (Subtarget.hasVSX() && Subtarget.isLittleEndian() &&
Bill Schmidtfae5d712014-12-09 16:35:51 +000010042 (LoadVT == MVT::v2f64 || LoadVT == MVT::v2i64 ||
10043 LoadVT == MVT::v4f32 || LoadVT == MVT::v4i32))
10044 return expandVSXLoadForLE(N, DCI);
10045 }
10046
Hal Finkelc93a9a22015-02-25 01:06:45 +000010047 EVT MemVT = LD->getMemoryVT();
10048 Type *Ty = MemVT.getTypeForEVT(*DAG.getContext());
Hal Finkelcf2e9082013-05-24 23:00:14 +000010049 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
Hal Finkelc93a9a22015-02-25 01:06:45 +000010050 Type *STy = MemVT.getScalarType().getTypeForEVT(*DAG.getContext());
10051 unsigned ScalarABIAlignment = getDataLayout()->getABITypeAlignment(STy);
10052 if (LD->isUnindexed() && VT.isVector() &&
10053 ((Subtarget.hasAltivec() && ISD::isNON_EXTLoad(N) &&
10054 // P8 and later hardware should just use LOAD.
10055 !Subtarget.hasP8Vector() && (VT == MVT::v16i8 || VT == MVT::v8i16 ||
10056 VT == MVT::v4i32 || VT == MVT::v4f32)) ||
10057 (Subtarget.hasQPX() && (VT == MVT::v4f64 || VT == MVT::v4f32) &&
10058 LD->getAlignment() >= ScalarABIAlignment)) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +000010059 LD->getAlignment() < ABIAlignment) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000010060 // This is a type-legal unaligned Altivec or QPX load.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010061 SDValue Chain = LD->getChain();
10062 SDValue Ptr = LD->getBasePtr();
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010063 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelcf2e9082013-05-24 23:00:14 +000010064
10065 // This implements the loading of unaligned vectors as described in
10066 // the venerable Apple Velocity Engine overview. Specifically:
10067 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
10068 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
10069 //
10070 // The general idea is to expand a sequence of one or more unaligned
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010071 // loads into an alignment-based permutation-control instruction (lvsl
10072 // or lvsr), a series of regular vector loads (which always truncate
10073 // their input address to an aligned address), and a series of
10074 // permutations. The results of these permutations are the requested
10075 // loaded values. The trick is that the last "extra" load is not taken
10076 // from the address you might suspect (sizeof(vector) bytes after the
10077 // last requested load), but rather sizeof(vector) - 1 bytes after the
10078 // last requested vector. The point of this is to avoid a page fault if
10079 // the base address happened to be aligned. This works because if the
10080 // base address is aligned, then adding less than a full vector length
10081 // will cause the last vector in the sequence to be (re)loaded.
10082 // Otherwise, the next vector will be fetched as you might suspect was
10083 // necessary.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010084
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010085 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +000010086 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010087 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
10088 // optimization later.
Hal Finkelc93a9a22015-02-25 01:06:45 +000010089 Intrinsic::ID Intr, IntrLD, IntrPerm;
10090 MVT PermCntlTy, PermTy, LDTy;
10091 if (Subtarget.hasAltivec()) {
10092 Intr = isLittleEndian ? Intrinsic::ppc_altivec_lvsr :
10093 Intrinsic::ppc_altivec_lvsl;
10094 IntrLD = Intrinsic::ppc_altivec_lvx;
10095 IntrPerm = Intrinsic::ppc_altivec_vperm;
10096 PermCntlTy = MVT::v16i8;
10097 PermTy = MVT::v4i32;
10098 LDTy = MVT::v4i32;
10099 } else {
10100 Intr = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlpcld :
10101 Intrinsic::ppc_qpx_qvlpcls;
10102 IntrLD = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlfd :
10103 Intrinsic::ppc_qpx_qvlfs;
10104 IntrPerm = Intrinsic::ppc_qpx_qvfperm;
10105 PermCntlTy = MVT::v4f64;
10106 PermTy = MVT::v4f64;
10107 LDTy = MemVT.getSimpleVT();
10108 }
10109
10110 SDValue PermCntl = BuildIntrinsicOp(Intr, Ptr, DAG, dl, PermCntlTy);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010111
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010112 // Create the new MMO for the new base load. It is like the original MMO,
10113 // but represents an area in memory almost twice the vector size centered
10114 // on the original address. If the address is unaligned, we might start
10115 // reading up to (sizeof(vector)-1) bytes below the address of the
10116 // original unaligned load.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010117 MachineFunction &MF = DAG.getMachineFunction();
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010118 MachineMemOperand *BaseMMO =
Hal Finkelc93a9a22015-02-25 01:06:45 +000010119 MF.getMachineMemOperand(LD->getMemOperand(), -MemVT.getStoreSize()+1,
10120 2*MemVT.getStoreSize()-1);
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010121
10122 // Create the new base load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010123 SDValue LDXIntID = DAG.getTargetConstant(IntrLD, dl, getPointerTy());
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010124 SDValue BaseLoadOps[] = { Chain, LDXIntID, Ptr };
10125 SDValue BaseLoad =
10126 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
Hal Finkelc93a9a22015-02-25 01:06:45 +000010127 DAG.getVTList(PermTy, MVT::Other),
10128 BaseLoadOps, LDTy, BaseMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010129
10130 // Note that the value of IncOffset (which is provided to the next
10131 // load's pointer info offset value, and thus used to calculate the
10132 // alignment), and the value of IncValue (which is actually used to
10133 // increment the pointer value) are different! This is because we
10134 // require the next load to appear to be aligned, even though it
10135 // is actually offset from the base pointer by a lesser amount.
10136 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +000010137 int IncValue = IncOffset;
10138
10139 // Walk (both up and down) the chain looking for another load at the real
10140 // (aligned) offset (the alignment of the other load does not matter in
10141 // this case). If found, then do not use the offset reduction trick, as
10142 // that will prevent the loads from being later combined (as they would
10143 // otherwise be duplicates).
10144 if (!findConsecutiveLoad(LD, DAG))
10145 --IncValue;
10146
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010147 SDValue Increment = DAG.getConstant(IncValue, dl, getPointerTy());
Hal Finkelcf2e9082013-05-24 23:00:14 +000010148 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
10149
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010150 MachineMemOperand *ExtraMMO =
10151 MF.getMachineMemOperand(LD->getMemOperand(),
Hal Finkelc93a9a22015-02-25 01:06:45 +000010152 1, 2*MemVT.getStoreSize()-1);
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010153 SDValue ExtraLoadOps[] = { Chain, LDXIntID, Ptr };
Hal Finkelcf2e9082013-05-24 23:00:14 +000010154 SDValue ExtraLoad =
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010155 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
Hal Finkelc93a9a22015-02-25 01:06:45 +000010156 DAG.getVTList(PermTy, MVT::Other),
10157 ExtraLoadOps, LDTy, ExtraMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010158
10159 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
10160 BaseLoad.getValue(1), ExtraLoad.getValue(1));
10161
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010162 // Because vperm has a big-endian bias, we must reverse the order
10163 // of the input vectors and complement the permute control vector
10164 // when generating little endian code. We have already handled the
10165 // latter by using lvsr instead of lvsl, so just reverse BaseLoad
10166 // and ExtraLoad here.
10167 SDValue Perm;
10168 if (isLittleEndian)
Hal Finkelc93a9a22015-02-25 01:06:45 +000010169 Perm = BuildIntrinsicOp(IntrPerm,
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010170 ExtraLoad, BaseLoad, PermCntl, DAG, dl);
10171 else
Hal Finkelc93a9a22015-02-25 01:06:45 +000010172 Perm = BuildIntrinsicOp(IntrPerm,
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010173 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010174
Hal Finkelc93a9a22015-02-25 01:06:45 +000010175 if (VT != PermTy)
10176 Perm = Subtarget.hasAltivec() ?
10177 DAG.getNode(ISD::BITCAST, dl, VT, Perm) :
10178 DAG.getNode(ISD::FP_ROUND, dl, VT, Perm, // QPX
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010179 DAG.getTargetConstant(1, dl, MVT::i64));
Hal Finkelc93a9a22015-02-25 01:06:45 +000010180 // second argument is 1 because this rounding
10181 // is always exact.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010182
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010183 // The output of the permutation is our loaded result, the TokenFactor is
10184 // our new chain.
10185 DCI.CombineTo(N, Perm, TF);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010186 return SDValue(N, 0);
10187 }
10188 }
10189 break;
Eric Christophercccae792015-01-30 22:02:31 +000010190 case ISD::INTRINSIC_WO_CHAIN: {
10191 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelc93a9a22015-02-25 01:06:45 +000010192 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
Eric Christophercccae792015-01-30 22:02:31 +000010193 Intrinsic::ID Intr = (isLittleEndian ? Intrinsic::ppc_altivec_lvsr
10194 : Intrinsic::ppc_altivec_lvsl);
Hal Finkelc93a9a22015-02-25 01:06:45 +000010195 if ((IID == Intr ||
10196 IID == Intrinsic::ppc_qpx_qvlpcld ||
10197 IID == Intrinsic::ppc_qpx_qvlpcls) &&
10198 N->getOperand(1)->getOpcode() == ISD::ADD) {
Eric Christophercccae792015-01-30 22:02:31 +000010199 SDValue Add = N->getOperand(1);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010200
Hal Finkelc93a9a22015-02-25 01:06:45 +000010201 int Bits = IID == Intrinsic::ppc_qpx_qvlpcld ?
10202 5 /* 32 byte alignment */ : 4 /* 16 byte alignment */;
10203
Eric Christophercccae792015-01-30 22:02:31 +000010204 if (DAG.MaskedValueIsZero(
10205 Add->getOperand(1),
Hal Finkelc93a9a22015-02-25 01:06:45 +000010206 APInt::getAllOnesValue(Bits /* alignment */)
Eric Christophercccae792015-01-30 22:02:31 +000010207 .zext(
10208 Add.getValueType().getScalarType().getSizeInBits()))) {
10209 SDNode *BasePtr = Add->getOperand(0).getNode();
10210 for (SDNode::use_iterator UI = BasePtr->use_begin(),
10211 UE = BasePtr->use_end();
10212 UI != UE; ++UI) {
10213 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
Hal Finkelc93a9a22015-02-25 01:06:45 +000010214 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() == IID) {
Eric Christophercccae792015-01-30 22:02:31 +000010215 // We've found another LVSL/LVSR, and this address is an aligned
10216 // multiple of that one. The results will be the same, so use the
10217 // one we've just found instead.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010218
Eric Christophercccae792015-01-30 22:02:31 +000010219 return SDValue(*UI, 0);
10220 }
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010221 }
10222 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000010223
10224 if (isa<ConstantSDNode>(Add->getOperand(1))) {
10225 SDNode *BasePtr = Add->getOperand(0).getNode();
10226 for (SDNode::use_iterator UI = BasePtr->use_begin(),
10227 UE = BasePtr->use_end(); UI != UE; ++UI) {
10228 if (UI->getOpcode() == ISD::ADD &&
10229 isa<ConstantSDNode>(UI->getOperand(1)) &&
10230 (cast<ConstantSDNode>(Add->getOperand(1))->getZExtValue() -
10231 cast<ConstantSDNode>(UI->getOperand(1))->getZExtValue()) %
Aaron Ballman5561ed42015-02-25 13:05:24 +000010232 (1ULL << Bits) == 0) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000010233 SDNode *OtherAdd = *UI;
10234 for (SDNode::use_iterator VI = OtherAdd->use_begin(),
10235 VE = OtherAdd->use_end(); VI != VE; ++VI) {
10236 if (VI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
10237 cast<ConstantSDNode>(VI->getOperand(0))->getZExtValue() == IID) {
10238 return SDValue(*VI, 0);
10239 }
10240 }
10241 }
10242 }
10243 }
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010244 }
10245 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +000010246
10247 break;
Bill Schmidtfae5d712014-12-09 16:35:51 +000010248 case ISD::INTRINSIC_W_CHAIN: {
10249 // For little endian, VSX loads require generating lxvd2x/xxswapd.
Eric Christophercccae792015-01-30 22:02:31 +000010250 if (Subtarget.hasVSX() && Subtarget.isLittleEndian()) {
Bill Schmidtfae5d712014-12-09 16:35:51 +000010251 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10252 default:
10253 break;
10254 case Intrinsic::ppc_vsx_lxvw4x:
10255 case Intrinsic::ppc_vsx_lxvd2x:
10256 return expandVSXLoadForLE(N, DCI);
10257 }
10258 }
10259 break;
10260 }
10261 case ISD::INTRINSIC_VOID: {
10262 // For little endian, VSX stores require generating xxswapd/stxvd2x.
Eric Christophercccae792015-01-30 22:02:31 +000010263 if (Subtarget.hasVSX() && Subtarget.isLittleEndian()) {
Bill Schmidtfae5d712014-12-09 16:35:51 +000010264 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10265 default:
10266 break;
10267 case Intrinsic::ppc_vsx_stxvw4x:
10268 case Intrinsic::ppc_vsx_stxvd2x:
10269 return expandVSXStoreForLE(N, DCI);
10270 }
10271 }
10272 break;
10273 }
Chris Lattnera7976d32006-07-10 20:56:58 +000010274 case ISD::BSWAP:
10275 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +000010276 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +000010277 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +000010278 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
Eric Christophercccae792015-01-30 22:02:31 +000010279 (Subtarget.hasLDBRX() && Subtarget.isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +000010280 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010281 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +000010282 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +000010283 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010284 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +000010285 LD->getChain(), // Chain
10286 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +000010287 DAG.getValueType(N->getValueType(0)) // VT
10288 };
Dan Gohman48b185d2009-09-25 20:36:54 +000010289 SDValue BSLoad =
10290 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +000010291 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
10292 MVT::i64 : MVT::i32, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +000010293 Ops, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +000010294
Scott Michelcf0da6c2009-02-17 22:15:04 +000010295 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010296 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +000010297 if (N->getValueType(0) == MVT::i16)
10298 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010299
Chris Lattnera7976d32006-07-10 20:56:58 +000010300 // First, combine the bswap away. This makes the value produced by the
10301 // load dead.
10302 DCI.CombineTo(N, ResVal);
10303
10304 // Next, combine the load away, we give it a bogus result value but a real
10305 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +000010306 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +000010307
Chris Lattnera7976d32006-07-10 20:56:58 +000010308 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010309 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +000010310 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010311
Chris Lattner27f53452006-03-01 05:50:56 +000010312 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +000010313 case PPCISD::VCMP: {
10314 // If a VCMPo node already exists with exactly the same operands as this
10315 // node, use its result instead of this node (VCMPo computes both a CR6 and
10316 // a normal output).
10317 //
10318 if (!N->getOperand(0).hasOneUse() &&
10319 !N->getOperand(1).hasOneUse() &&
10320 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000010321
Chris Lattnerd4058a52006-03-31 06:02:07 +000010322 // Scan all of the users of the LHS, looking for VCMPo's that match.
Craig Topper062a2ba2014-04-25 05:30:21 +000010323 SDNode *VCMPoNode = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010324
Gabor Greiff304a7a2008-08-28 21:40:38 +000010325 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +000010326 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
10327 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +000010328 if (UI->getOpcode() == PPCISD::VCMPo &&
10329 UI->getOperand(1) == N->getOperand(1) &&
10330 UI->getOperand(2) == N->getOperand(2) &&
10331 UI->getOperand(0) == N->getOperand(0)) {
10332 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +000010333 break;
10334 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010335
Chris Lattner518834c2006-04-18 18:28:22 +000010336 // If there is no VCMPo node, or if the flag value has a single use, don't
10337 // transform this.
10338 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
10339 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010340
10341 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +000010342 // chain, this transformation is more complex. Note that multiple things
10343 // could use the value result, which we should ignore.
Craig Topper062a2ba2014-04-25 05:30:21 +000010344 SDNode *FlagUser = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010345 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Craig Topper062a2ba2014-04-25 05:30:21 +000010346 FlagUser == nullptr; ++UI) {
Chris Lattner518834c2006-04-18 18:28:22 +000010347 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +000010348 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +000010349 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010350 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +000010351 FlagUser = User;
10352 break;
10353 }
10354 }
10355 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010356
Ulrich Weigandd5ebc622013-07-03 17:05:42 +000010357 // If the user is a MFOCRF instruction, we know this is safe.
10358 // Otherwise we give up for right now.
10359 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010360 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +000010361 }
10362 break;
10363 }
Hal Finkel940ab932014-02-28 00:27:01 +000010364 case ISD::BRCOND: {
10365 SDValue Cond = N->getOperand(1);
10366 SDValue Target = N->getOperand(2);
10367
10368 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
10369 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
10370 Intrinsic::ppc_is_decremented_ctr_nonzero) {
10371
10372 // We now need to make the intrinsic dead (it cannot be instruction
10373 // selected).
10374 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
10375 assert(Cond.getNode()->hasOneUse() &&
10376 "Counter decrement has more than one use");
10377
10378 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
10379 N->getOperand(0), Target);
10380 }
10381 }
10382 break;
Chris Lattner9754d142006-04-18 17:59:36 +000010383 case ISD::BR_CC: {
10384 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +000010385 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +000010386 // lowering is done pre-legalize, because the legalizer lowers the predicate
10387 // compare down to code that is difficult to reassemble.
10388 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010389 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +000010390
10391 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
10392 // value. If so, pass-through the AND to get to the intrinsic.
10393 if (LHS.getOpcode() == ISD::AND &&
10394 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
10395 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
10396 Intrinsic::ppc_is_decremented_ctr_nonzero &&
10397 isa<ConstantSDNode>(LHS.getOperand(1)) &&
10398 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
10399 isZero())
10400 LHS = LHS.getOperand(0);
10401
10402 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
10403 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
10404 Intrinsic::ppc_is_decremented_ctr_nonzero &&
10405 isa<ConstantSDNode>(RHS)) {
10406 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
10407 "Counter decrement comparison is not EQ or NE");
10408
10409 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
10410 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
10411 (CC == ISD::SETNE && !Val);
10412
10413 // We now need to make the intrinsic dead (it cannot be instruction
10414 // selected).
10415 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
10416 assert(LHS.getNode()->hasOneUse() &&
10417 "Counter decrement has more than one use");
10418
10419 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
10420 N->getOperand(0), N->getOperand(4));
10421 }
10422
Chris Lattner9754d142006-04-18 17:59:36 +000010423 int CompareOpc;
10424 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010425
Chris Lattner9754d142006-04-18 17:59:36 +000010426 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
10427 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
Kit Barton0cfa7b72015-03-03 19:55:45 +000010428 getAltivecCompareInfo(LHS, CompareOpc, isDot, Subtarget)) {
Chris Lattner9754d142006-04-18 17:59:36 +000010429 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +000010430
Chris Lattner9754d142006-04-18 17:59:36 +000010431 // If this is a comparison against something other than 0/1, then we know
10432 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +000010433 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +000010434 if (Val != 0 && Val != 1) {
10435 if (CC == ISD::SETEQ) // Cond never true, remove branch.
10436 return N->getOperand(0);
10437 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +000010438 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +000010439 N->getOperand(0), N->getOperand(4));
10440 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010441
Chris Lattner9754d142006-04-18 17:59:36 +000010442 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010443
Chris Lattner9754d142006-04-18 17:59:36 +000010444 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010445 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +000010446 LHS.getOperand(2), // LHS of compare
10447 LHS.getOperand(3), // RHS of compare
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010448 DAG.getConstant(CompareOpc, dl, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +000010449 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +000010450 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +000010451 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010452
Chris Lattner9754d142006-04-18 17:59:36 +000010453 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000010454 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +000010455 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +000010456 default: // Can't happen, don't crash on invalid number though.
10457 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000010458 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +000010459 break;
10460 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000010461 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +000010462 break;
10463 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000010464 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +000010465 break;
10466 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000010467 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +000010468 break;
10469 }
10470
Owen Anderson9f944592009-08-11 20:47:22 +000010471 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010472 DAG.getConstant(CompOpc, dl, MVT::i32),
Owen Anderson9f944592009-08-11 20:47:22 +000010473 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +000010474 N->getOperand(4), CompNode.getValue(1));
10475 }
10476 break;
10477 }
Chris Lattnerf4184352006-03-01 04:57:39 +000010478 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010479
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010480 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +000010481}
10482
Hal Finkel13d104b2014-12-11 18:37:52 +000010483SDValue
10484PPCTargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
10485 SelectionDAG &DAG,
10486 std::vector<SDNode *> *Created) const {
10487 // fold (sdiv X, pow2)
10488 EVT VT = N->getValueType(0);
Hal Finkel04b16b52014-12-23 08:38:50 +000010489 if (VT == MVT::i64 && !Subtarget.isPPC64())
10490 return SDValue();
Hal Finkel13d104b2014-12-11 18:37:52 +000010491 if ((VT != MVT::i32 && VT != MVT::i64) ||
10492 !(Divisor.isPowerOf2() || (-Divisor).isPowerOf2()))
10493 return SDValue();
10494
10495 SDLoc DL(N);
10496 SDValue N0 = N->getOperand(0);
10497
10498 bool IsNegPow2 = (-Divisor).isPowerOf2();
10499 unsigned Lg2 = (IsNegPow2 ? -Divisor : Divisor).countTrailingZeros();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010500 SDValue ShiftAmt = DAG.getConstant(Lg2, DL, VT);
Hal Finkel13d104b2014-12-11 18:37:52 +000010501
10502 SDValue Op = DAG.getNode(PPCISD::SRA_ADDZE, DL, VT, N0, ShiftAmt);
10503 if (Created)
10504 Created->push_back(Op.getNode());
10505
10506 if (IsNegPow2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010507 Op = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Op);
Hal Finkel13d104b2014-12-11 18:37:52 +000010508 if (Created)
10509 Created->push_back(Op.getNode());
10510 }
10511
10512 return Op;
10513}
10514
Chris Lattner4211ca92006-04-14 06:01:58 +000010515//===----------------------------------------------------------------------===//
10516// Inline Assembly Support
10517//===----------------------------------------------------------------------===//
10518
Jay Foada0653a32014-05-14 21:14:37 +000010519void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10520 APInt &KnownZero,
10521 APInt &KnownOne,
10522 const SelectionDAG &DAG,
10523 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +000010524 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +000010525 switch (Op.getOpcode()) {
10526 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +000010527 case PPCISD::LBRX: {
10528 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +000010529 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +000010530 KnownZero = 0xFFFF0000;
10531 break;
10532 }
Chris Lattnerc5287c02006-04-02 06:26:07 +000010533 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010534 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +000010535 default: break;
10536 case Intrinsic::ppc_altivec_vcmpbfp_p:
10537 case Intrinsic::ppc_altivec_vcmpeqfp_p:
10538 case Intrinsic::ppc_altivec_vcmpequb_p:
10539 case Intrinsic::ppc_altivec_vcmpequh_p:
10540 case Intrinsic::ppc_altivec_vcmpequw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000010541 case Intrinsic::ppc_altivec_vcmpequd_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000010542 case Intrinsic::ppc_altivec_vcmpgefp_p:
10543 case Intrinsic::ppc_altivec_vcmpgtfp_p:
10544 case Intrinsic::ppc_altivec_vcmpgtsb_p:
10545 case Intrinsic::ppc_altivec_vcmpgtsh_p:
10546 case Intrinsic::ppc_altivec_vcmpgtsw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000010547 case Intrinsic::ppc_altivec_vcmpgtsd_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000010548 case Intrinsic::ppc_altivec_vcmpgtub_p:
10549 case Intrinsic::ppc_altivec_vcmpgtuh_p:
10550 case Intrinsic::ppc_altivec_vcmpgtuw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000010551 case Intrinsic::ppc_altivec_vcmpgtud_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000010552 KnownZero = ~1U; // All bits but the low one are known to be zero.
10553 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010554 }
Chris Lattnerc5287c02006-04-02 06:26:07 +000010555 }
10556 }
10557}
10558
Hal Finkel57725662015-01-03 17:58:24 +000010559unsigned PPCTargetLowering::getPrefLoopAlignment(MachineLoop *ML) const {
10560 switch (Subtarget.getDarwinDirective()) {
10561 default: break;
10562 case PPC::DIR_970:
10563 case PPC::DIR_PWR4:
10564 case PPC::DIR_PWR5:
10565 case PPC::DIR_PWR5X:
10566 case PPC::DIR_PWR6:
10567 case PPC::DIR_PWR6X:
10568 case PPC::DIR_PWR7:
10569 case PPC::DIR_PWR8: {
10570 if (!ML)
10571 break;
10572
Eric Christophercccae792015-01-30 22:02:31 +000010573 const PPCInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel57725662015-01-03 17:58:24 +000010574
10575 // For small loops (between 5 and 8 instructions), align to a 32-byte
10576 // boundary so that the entire loop fits in one instruction-cache line.
10577 uint64_t LoopSize = 0;
10578 for (auto I = ML->block_begin(), IE = ML->block_end(); I != IE; ++I)
10579 for (auto J = (*I)->begin(), JE = (*I)->end(); J != JE; ++J)
10580 LoopSize += TII->GetInstSizeInBytes(J);
10581
10582 if (LoopSize > 16 && LoopSize <= 32)
10583 return 5;
10584
10585 break;
10586 }
10587 }
10588
10589 return TargetLowering::getPrefLoopAlignment(ML);
10590}
Chris Lattnerc5287c02006-04-02 06:26:07 +000010591
Chris Lattnerd6855142007-03-25 02:14:49 +000010592/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +000010593/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +000010594PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010595PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
10596 if (Constraint.size() == 1) {
10597 switch (Constraint[0]) {
10598 default: break;
10599 case 'b':
10600 case 'r':
10601 case 'f':
10602 case 'v':
10603 case 'y':
10604 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +000010605 case 'Z':
10606 // FIXME: While Z does indicate a memory constraint, it specifically
10607 // indicates an r+r address (used in conjunction with the 'y' modifier
10608 // in the replacement string). Currently, we're forcing the base
10609 // register to be r0 in the asm printer (which is interpreted as zero)
10610 // and forming the complete address in the second register. This is
10611 // suboptimal.
10612 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010613 }
Hal Finkel6aca2372014-03-02 18:23:39 +000010614 } else if (Constraint == "wc") { // individual CR bits.
10615 return C_RegisterClass;
Hal Finkel27774d92014-03-13 07:58:58 +000010616 } else if (Constraint == "wa" || Constraint == "wd" ||
10617 Constraint == "wf" || Constraint == "ws") {
10618 return C_RegisterClass; // VSX registers.
Chris Lattnerd6855142007-03-25 02:14:49 +000010619 }
10620 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +000010621}
10622
John Thompsone8360b72010-10-29 17:29:13 +000010623/// Examine constraint type and operand type and determine a weight value.
10624/// This object must already have been set up with the operand type
10625/// and the current alternative constraint selected.
10626TargetLowering::ConstraintWeight
10627PPCTargetLowering::getSingleConstraintMatchWeight(
10628 AsmOperandInfo &info, const char *constraint) const {
10629 ConstraintWeight weight = CW_Invalid;
10630 Value *CallOperandVal = info.CallOperandVal;
10631 // If we don't have a value, we can't do a match,
10632 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010633 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010634 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010635 Type *type = CallOperandVal->getType();
Hal Finkel6aca2372014-03-02 18:23:39 +000010636
John Thompsone8360b72010-10-29 17:29:13 +000010637 // Look at the constraint type.
Hal Finkel6aca2372014-03-02 18:23:39 +000010638 if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
10639 return CW_Register; // an individual CR bit.
Hal Finkel27774d92014-03-13 07:58:58 +000010640 else if ((StringRef(constraint) == "wa" ||
10641 StringRef(constraint) == "wd" ||
10642 StringRef(constraint) == "wf") &&
10643 type->isVectorTy())
10644 return CW_Register;
10645 else if (StringRef(constraint) == "ws" && type->isDoubleTy())
10646 return CW_Register;
Hal Finkel6aca2372014-03-02 18:23:39 +000010647
John Thompsone8360b72010-10-29 17:29:13 +000010648 switch (*constraint) {
10649 default:
10650 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10651 break;
10652 case 'b':
10653 if (type->isIntegerTy())
10654 weight = CW_Register;
10655 break;
10656 case 'f':
10657 if (type->isFloatTy())
10658 weight = CW_Register;
10659 break;
10660 case 'd':
10661 if (type->isDoubleTy())
10662 weight = CW_Register;
10663 break;
10664 case 'v':
10665 if (type->isVectorTy())
10666 weight = CW_Register;
10667 break;
10668 case 'y':
10669 weight = CW_Register;
10670 break;
Hal Finkel4f24c622012-11-05 18:18:42 +000010671 case 'Z':
10672 weight = CW_Memory;
10673 break;
John Thompsone8360b72010-10-29 17:29:13 +000010674 }
10675 return weight;
10676}
10677
Eric Christopher11e4df72015-02-26 22:38:43 +000010678std::pair<unsigned, const TargetRegisterClass *>
10679PPCTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
10680 const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010681 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +000010682 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +000010683 // GCC RS6000 Constraint Letters
10684 switch (Constraint[0]) {
10685 case 'b': // R1-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010686 if (VT == MVT::i64 && Subtarget.isPPC64())
Hal Finkel638a9fa2013-03-19 18:51:05 +000010687 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
10688 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000010689 case 'r': // R0-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010690 if (VT == MVT::i64 && Subtarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +000010691 return std::make_pair(0U, &PPC::G8RCRegClass);
10692 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000010693 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +000010694 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +000010695 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +000010696 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +000010697 return std::make_pair(0U, &PPC::F8RCRegClass);
Hal Finkelc93a9a22015-02-25 01:06:45 +000010698 if (VT == MVT::v4f64 && Subtarget.hasQPX())
10699 return std::make_pair(0U, &PPC::QFRCRegClass);
10700 if (VT == MVT::v4f32 && Subtarget.hasQPX())
10701 return std::make_pair(0U, &PPC::QSRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000010702 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010703 case 'v':
Hal Finkelc93a9a22015-02-25 01:06:45 +000010704 if (VT == MVT::v4f64 && Subtarget.hasQPX())
10705 return std::make_pair(0U, &PPC::QFRCRegClass);
10706 if (VT == MVT::v4f32 && Subtarget.hasQPX())
10707 return std::make_pair(0U, &PPC::QSRCRegClass);
Craig Topperabadc662012-04-20 06:31:50 +000010708 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000010709 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +000010710 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +000010711 }
Hal Finkel6aca2372014-03-02 18:23:39 +000010712 } else if (Constraint == "wc") { // an individual CR bit.
10713 return std::make_pair(0U, &PPC::CRBITRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +000010714 } else if (Constraint == "wa" || Constraint == "wd" ||
Hal Finkel19be5062014-03-29 05:29:01 +000010715 Constraint == "wf") {
Hal Finkel27774d92014-03-13 07:58:58 +000010716 return std::make_pair(0U, &PPC::VSRCRegClass);
Hal Finkel19be5062014-03-29 05:29:01 +000010717 } else if (Constraint == "ws") {
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +000010718 if (VT == MVT::f32)
10719 return std::make_pair(0U, &PPC::VSSRCRegClass);
10720 else
10721 return std::make_pair(0U, &PPC::VSFRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +000010722 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010723
Eric Christopher11e4df72015-02-26 22:38:43 +000010724 std::pair<unsigned, const TargetRegisterClass *> R =
10725 TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Hal Finkelb176acb2013-08-03 12:25:10 +000010726
10727 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
10728 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
10729 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
10730 // register.
10731 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
10732 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010733 if (R.first && VT == MVT::i64 && Subtarget.isPPC64() &&
Eric Christopher11e4df72015-02-26 22:38:43 +000010734 PPC::GPRCRegClass.contains(R.first))
Hal Finkelb176acb2013-08-03 12:25:10 +000010735 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +000010736 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +000010737 &PPC::G8RCRegClass);
Hal Finkelb176acb2013-08-03 12:25:10 +000010738
Hal Finkelaa10b3c2014-12-08 22:54:22 +000010739 // GCC accepts 'cc' as an alias for 'cr0', and we need to do the same.
10740 if (!R.second && StringRef("{cc}").equals_lower(Constraint)) {
10741 R.first = PPC::CR0;
10742 R.second = &PPC::CRRCRegClass;
10743 }
10744
Hal Finkelb176acb2013-08-03 12:25:10 +000010745 return R;
Chris Lattner01513612006-01-31 19:20:21 +000010746}
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010747
Chris Lattner584a11a2006-11-02 01:44:04 +000010748
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000010749/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +000010750/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +000010751void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010752 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010753 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +000010754 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010755 SDValue Result;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010756
Eric Christopherde9399b2011-06-02 23:16:42 +000010757 // Only support length 1 constraints.
10758 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010759
Eric Christopherde9399b2011-06-02 23:16:42 +000010760 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010761 switch (Letter) {
10762 default: break;
10763 case 'I':
10764 case 'J':
10765 case 'K':
10766 case 'L':
10767 case 'M':
10768 case 'N':
10769 case 'O':
10770 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +000010771 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000010772 if (!CST) return; // Must be an immediate to match.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010773 SDLoc dl(Op);
Hal Finkelc91fc112014-12-03 09:37:50 +000010774 int64_t Value = CST->getSExtValue();
10775 EVT TCVT = MVT::i64; // All constants taken to be 64 bits so that negative
10776 // numbers are printed as such.
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010777 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010778 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010779 case 'I': // "I" is a signed 16-bit constant.
Hal Finkelc91fc112014-12-03 09:37:50 +000010780 if (isInt<16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010781 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010782 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010783 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
Hal Finkelc91fc112014-12-03 09:37:50 +000010784 if (isShiftedUInt<16, 16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010785 Result = DAG.getTargetConstant(Value, dl, TCVT);
Hal Finkelc91fc112014-12-03 09:37:50 +000010786 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010787 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Hal Finkelc91fc112014-12-03 09:37:50 +000010788 if (isShiftedInt<16, 16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010789 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010790 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010791 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Hal Finkelc91fc112014-12-03 09:37:50 +000010792 if (isUInt<16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010793 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010794 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010795 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +000010796 if (Value > 31)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010797 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010798 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010799 case 'N': // "N" is a positive constant that is an exact power of two.
Hal Finkelc91fc112014-12-03 09:37:50 +000010800 if (Value > 0 && isPowerOf2_64(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010801 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010802 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010803 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +000010804 if (Value == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010805 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010806 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010807 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Hal Finkelc91fc112014-12-03 09:37:50 +000010808 if (isInt<16>(-Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010809 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010810 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010811 }
10812 break;
10813 }
10814 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010815
Gabor Greiff304a7a2008-08-28 21:40:38 +000010816 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000010817 Ops.push_back(Result);
10818 return;
10819 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010820
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010821 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +000010822 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010823}
Evan Cheng2dd2c652006-03-13 23:20:37 +000010824
Chris Lattner1eb94d92007-03-30 23:15:24 +000010825// isLegalAddressingMode - Return true if the addressing mode represented
10826// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +000010827bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000010828 Type *Ty,
10829 unsigned AS) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +000010830 // PPC does not allow r+i addressing modes for vectors!
10831 if (Ty->isVectorTy() && AM.BaseOffs != 0)
10832 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010833
Chris Lattner1eb94d92007-03-30 23:15:24 +000010834 // PPC allows a sign-extended 16-bit immediate field.
10835 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
10836 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010837
Chris Lattner1eb94d92007-03-30 23:15:24 +000010838 // No global is ever allowed as a base.
10839 if (AM.BaseGV)
10840 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010841
10842 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +000010843 switch (AM.Scale) {
10844 case 0: // "r+i" or just "i", depending on HasBaseReg.
10845 break;
10846 case 1:
10847 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
10848 return false;
10849 // Otherwise we have r+r or r+i.
10850 break;
10851 case 2:
10852 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
10853 return false;
10854 // Allow 2*r as r+r.
10855 break;
Chris Lattner19ccd622007-04-09 22:10:05 +000010856 default:
10857 // No other scales are supported.
10858 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +000010859 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010860
Chris Lattner1eb94d92007-03-30 23:15:24 +000010861 return true;
10862}
10863
Dan Gohman21cea8a2010-04-17 15:26:15 +000010864SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
10865 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +000010866 MachineFunction &MF = DAG.getMachineFunction();
10867 MachineFrameInfo *MFI = MF.getFrameInfo();
10868 MFI->setReturnAddressIsTaken(true);
10869
Bill Wendling908bf812014-01-06 00:43:20 +000010870 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +000010871 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +000010872
Andrew Trickef9de2a2013-05-25 02:42:55 +000010873 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +000010874 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +000010875
Dale Johannesen81bfca72010-05-03 22:59:34 +000010876 // Make sure the function does not optimize away the store of the RA to
10877 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +000010878 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +000010879 FuncInfo->setLRStoreRequired();
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010880 bool isPPC64 = Subtarget.isPPC64();
Dale Johannesen81bfca72010-05-03 22:59:34 +000010881
10882 if (Depth > 0) {
10883 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10884 SDValue Offset =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010885 DAG.getConstant(Subtarget.getFrameLowering()->getReturnSaveOffset(), dl,
Eric Christopherf71609b2015-02-13 00:39:27 +000010886 isPPC64 ? MVT::i64 : MVT::i32);
Dale Johannesen81bfca72010-05-03 22:59:34 +000010887 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
10888 DAG.getNode(ISD::ADD, dl, getPointerTy(),
10889 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +000010890 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +000010891 }
Chris Lattnerf6a81562007-12-08 06:59:59 +000010892
Chris Lattnerf6a81562007-12-08 06:59:59 +000010893 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010894 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +000010895 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +000010896 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +000010897}
10898
Dan Gohman21cea8a2010-04-17 15:26:15 +000010899SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
10900 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010901 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +000010902 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +000010903
Owen Anderson53aa7a92009-08-10 22:56:29 +000010904 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +000010905 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010906
Nicolas Geoffray75ab9792007-03-01 13:11:38 +000010907 MachineFunction &MF = DAG.getMachineFunction();
10908 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +000010909 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +000010910
10911 // Naked functions never have a frame pointer, and so we use r1. For all
10912 // other functions, this decision must be delayed until during PEI.
10913 unsigned FrameReg;
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +000010914 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Hal Finkelaa03c032013-03-21 19:03:19 +000010915 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
10916 else
10917 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
10918
Dale Johannesen81bfca72010-05-03 22:59:34 +000010919 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
10920 PtrVT);
10921 while (Depth--)
10922 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +000010923 FrameAddr, MachinePointerInfo(), false, false,
10924 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +000010925 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +000010926}
Dan Gohmanc14e5222008-10-21 03:41:46 +000010927
Hal Finkel0d8db462014-05-11 19:29:11 +000010928// FIXME? Maybe this could be a TableGen attribute on some registers and
10929// this table could be generated automatically from RegInfo.
10930unsigned PPCTargetLowering::getRegisterByName(const char* RegName,
10931 EVT VT) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010932 bool isPPC64 = Subtarget.isPPC64();
10933 bool isDarwinABI = Subtarget.isDarwinABI();
Hal Finkel0d8db462014-05-11 19:29:11 +000010934
10935 if ((isPPC64 && VT != MVT::i64 && VT != MVT::i32) ||
10936 (!isPPC64 && VT != MVT::i32))
10937 report_fatal_error("Invalid register global variable type");
10938
10939 bool is64Bit = isPPC64 && VT == MVT::i64;
10940 unsigned Reg = StringSwitch<unsigned>(RegName)
10941 .Case("r1", is64Bit ? PPC::X1 : PPC::R1)
Hal Finkele6698d52015-02-01 15:03:28 +000010942 .Case("r2", (isDarwinABI || isPPC64) ? 0 : PPC::R2)
Hal Finkel0d8db462014-05-11 19:29:11 +000010943 .Case("r13", (!isPPC64 && isDarwinABI) ? 0 :
10944 (is64Bit ? PPC::X13 : PPC::R13))
10945 .Default(0);
10946
10947 if (Reg)
10948 return Reg;
10949 report_fatal_error("Invalid register name global variable");
10950}
10951
Dan Gohmanc14e5222008-10-21 03:41:46 +000010952bool
10953PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10954 // The PowerPC target isn't yet aware of offsets.
10955 return false;
10956}
Tilmann Schellerb93960d2009-07-03 06:45:56 +000010957
Hal Finkel46ef7ce2014-08-13 01:15:40 +000010958bool PPCTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10959 const CallInst &I,
10960 unsigned Intrinsic) const {
10961
10962 switch (Intrinsic) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000010963 case Intrinsic::ppc_qpx_qvlfd:
10964 case Intrinsic::ppc_qpx_qvlfs:
10965 case Intrinsic::ppc_qpx_qvlfcd:
10966 case Intrinsic::ppc_qpx_qvlfcs:
10967 case Intrinsic::ppc_qpx_qvlfiwa:
10968 case Intrinsic::ppc_qpx_qvlfiwz:
Hal Finkel46ef7ce2014-08-13 01:15:40 +000010969 case Intrinsic::ppc_altivec_lvx:
10970 case Intrinsic::ppc_altivec_lvxl:
10971 case Intrinsic::ppc_altivec_lvebx:
10972 case Intrinsic::ppc_altivec_lvehx:
Bill Schmidt72954782014-11-12 04:19:40 +000010973 case Intrinsic::ppc_altivec_lvewx:
10974 case Intrinsic::ppc_vsx_lxvd2x:
10975 case Intrinsic::ppc_vsx_lxvw4x: {
Hal Finkel46ef7ce2014-08-13 01:15:40 +000010976 EVT VT;
10977 switch (Intrinsic) {
10978 case Intrinsic::ppc_altivec_lvebx:
10979 VT = MVT::i8;
10980 break;
10981 case Intrinsic::ppc_altivec_lvehx:
10982 VT = MVT::i16;
10983 break;
10984 case Intrinsic::ppc_altivec_lvewx:
10985 VT = MVT::i32;
10986 break;
Bill Schmidt72954782014-11-12 04:19:40 +000010987 case Intrinsic::ppc_vsx_lxvd2x:
10988 VT = MVT::v2f64;
10989 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +000010990 case Intrinsic::ppc_qpx_qvlfd:
10991 VT = MVT::v4f64;
10992 break;
10993 case Intrinsic::ppc_qpx_qvlfs:
10994 VT = MVT::v4f32;
10995 break;
10996 case Intrinsic::ppc_qpx_qvlfcd:
10997 VT = MVT::v2f64;
10998 break;
10999 case Intrinsic::ppc_qpx_qvlfcs:
11000 VT = MVT::v2f32;
11001 break;
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011002 default:
11003 VT = MVT::v4i32;
11004 break;
11005 }
11006
11007 Info.opc = ISD::INTRINSIC_W_CHAIN;
11008 Info.memVT = VT;
11009 Info.ptrVal = I.getArgOperand(0);
11010 Info.offset = -VT.getStoreSize()+1;
11011 Info.size = 2*VT.getStoreSize()-1;
11012 Info.align = 1;
11013 Info.vol = false;
11014 Info.readMem = true;
11015 Info.writeMem = false;
11016 return true;
11017 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000011018 case Intrinsic::ppc_qpx_qvlfda:
11019 case Intrinsic::ppc_qpx_qvlfsa:
11020 case Intrinsic::ppc_qpx_qvlfcda:
11021 case Intrinsic::ppc_qpx_qvlfcsa:
11022 case Intrinsic::ppc_qpx_qvlfiwaa:
11023 case Intrinsic::ppc_qpx_qvlfiwza: {
11024 EVT VT;
11025 switch (Intrinsic) {
11026 case Intrinsic::ppc_qpx_qvlfda:
11027 VT = MVT::v4f64;
11028 break;
11029 case Intrinsic::ppc_qpx_qvlfsa:
11030 VT = MVT::v4f32;
11031 break;
11032 case Intrinsic::ppc_qpx_qvlfcda:
11033 VT = MVT::v2f64;
11034 break;
11035 case Intrinsic::ppc_qpx_qvlfcsa:
11036 VT = MVT::v2f32;
11037 break;
11038 default:
11039 VT = MVT::v4i32;
11040 break;
11041 }
11042
11043 Info.opc = ISD::INTRINSIC_W_CHAIN;
11044 Info.memVT = VT;
11045 Info.ptrVal = I.getArgOperand(0);
11046 Info.offset = 0;
11047 Info.size = VT.getStoreSize();
11048 Info.align = 1;
11049 Info.vol = false;
11050 Info.readMem = true;
11051 Info.writeMem = false;
11052 return true;
11053 }
11054 case Intrinsic::ppc_qpx_qvstfd:
11055 case Intrinsic::ppc_qpx_qvstfs:
11056 case Intrinsic::ppc_qpx_qvstfcd:
11057 case Intrinsic::ppc_qpx_qvstfcs:
11058 case Intrinsic::ppc_qpx_qvstfiw:
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011059 case Intrinsic::ppc_altivec_stvx:
11060 case Intrinsic::ppc_altivec_stvxl:
11061 case Intrinsic::ppc_altivec_stvebx:
11062 case Intrinsic::ppc_altivec_stvehx:
Bill Schmidt72954782014-11-12 04:19:40 +000011063 case Intrinsic::ppc_altivec_stvewx:
11064 case Intrinsic::ppc_vsx_stxvd2x:
11065 case Intrinsic::ppc_vsx_stxvw4x: {
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011066 EVT VT;
11067 switch (Intrinsic) {
11068 case Intrinsic::ppc_altivec_stvebx:
11069 VT = MVT::i8;
11070 break;
11071 case Intrinsic::ppc_altivec_stvehx:
11072 VT = MVT::i16;
11073 break;
11074 case Intrinsic::ppc_altivec_stvewx:
11075 VT = MVT::i32;
11076 break;
Bill Schmidt72954782014-11-12 04:19:40 +000011077 case Intrinsic::ppc_vsx_stxvd2x:
11078 VT = MVT::v2f64;
11079 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +000011080 case Intrinsic::ppc_qpx_qvstfd:
11081 VT = MVT::v4f64;
11082 break;
11083 case Intrinsic::ppc_qpx_qvstfs:
11084 VT = MVT::v4f32;
11085 break;
11086 case Intrinsic::ppc_qpx_qvstfcd:
11087 VT = MVT::v2f64;
11088 break;
11089 case Intrinsic::ppc_qpx_qvstfcs:
11090 VT = MVT::v2f32;
11091 break;
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011092 default:
11093 VT = MVT::v4i32;
11094 break;
11095 }
11096
11097 Info.opc = ISD::INTRINSIC_VOID;
11098 Info.memVT = VT;
11099 Info.ptrVal = I.getArgOperand(1);
11100 Info.offset = -VT.getStoreSize()+1;
11101 Info.size = 2*VT.getStoreSize()-1;
11102 Info.align = 1;
11103 Info.vol = false;
11104 Info.readMem = false;
11105 Info.writeMem = true;
11106 return true;
11107 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000011108 case Intrinsic::ppc_qpx_qvstfda:
11109 case Intrinsic::ppc_qpx_qvstfsa:
11110 case Intrinsic::ppc_qpx_qvstfcda:
11111 case Intrinsic::ppc_qpx_qvstfcsa:
11112 case Intrinsic::ppc_qpx_qvstfiwa: {
11113 EVT VT;
11114 switch (Intrinsic) {
11115 case Intrinsic::ppc_qpx_qvstfda:
11116 VT = MVT::v4f64;
11117 break;
11118 case Intrinsic::ppc_qpx_qvstfsa:
11119 VT = MVT::v4f32;
11120 break;
11121 case Intrinsic::ppc_qpx_qvstfcda:
11122 VT = MVT::v2f64;
11123 break;
11124 case Intrinsic::ppc_qpx_qvstfcsa:
11125 VT = MVT::v2f32;
11126 break;
11127 default:
11128 VT = MVT::v4i32;
11129 break;
11130 }
11131
11132 Info.opc = ISD::INTRINSIC_VOID;
11133 Info.memVT = VT;
11134 Info.ptrVal = I.getArgOperand(1);
11135 Info.offset = 0;
11136 Info.size = VT.getStoreSize();
11137 Info.align = 1;
11138 Info.vol = false;
11139 Info.readMem = false;
11140 Info.writeMem = true;
11141 return true;
11142 }
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011143 default:
11144 break;
11145 }
11146
11147 return false;
11148}
11149
Evan Chengd9929f02010-04-01 20:10:42 +000011150/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +000011151/// and store operations as a result of memset, memcpy, and memmove
11152/// lowering. If DstAlign is zero that means it's safe to destination
11153/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
11154/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +000011155/// probably because the source does not need to be loaded. If 'IsMemset' is
11156/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
11157/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
11158/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +000011159/// It returns EVT::Other if the type should be determined using generic
11160/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +000011161EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
11162 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000011163 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +000011164 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +000011165 MachineFunction &MF) const {
Hal Finkel52368d42015-03-31 20:56:09 +000011166 if (getTargetMachine().getOptLevel() != CodeGenOpt::None) {
11167 const Function *F = MF.getFunction();
11168 // When expanding a memset, require at least two QPX instructions to cover
11169 // the cost of loading the value to be stored from the constant pool.
11170 if (Subtarget.hasQPX() && Size >= 32 && (!IsMemset || Size >= 64) &&
11171 (!SrcAlign || SrcAlign >= 32) && (!DstAlign || DstAlign >= 32) &&
11172 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
11173 return MVT::v4f64;
11174 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000011175
Hal Finkel52368d42015-03-31 20:56:09 +000011176 // We should use Altivec/VSX loads and stores when available. For unaligned
11177 // addresses, unaligned VSX loads are only fast starting with the P8.
11178 if (Subtarget.hasAltivec() && Size >= 16 &&
11179 (((!SrcAlign || SrcAlign >= 16) && (!DstAlign || DstAlign >= 16)) ||
11180 ((IsMemset && Subtarget.hasVSX()) || Subtarget.hasP8Vector())))
11181 return MVT::v4i32;
11182 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000011183
Eric Christopherd90a8742014-06-12 22:38:20 +000011184 if (Subtarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +000011185 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +000011186 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000011187
11188 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +000011189}
Hal Finkel88ed4e32012-04-01 19:23:08 +000011190
Hal Finkel34974ed2014-04-12 21:52:38 +000011191/// \brief Returns true if it is beneficial to convert a load of a constant
11192/// to just the constant itself.
11193bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11194 Type *Ty) const {
11195 assert(Ty->isIntegerTy());
11196
11197 unsigned BitSize = Ty->getPrimitiveSizeInBits();
11198 if (BitSize == 0 || BitSize > 64)
11199 return false;
11200 return true;
11201}
11202
11203bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
11204 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
11205 return false;
11206 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11207 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
11208 return NumBits1 == 64 && NumBits2 == 32;
11209}
11210
11211bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
11212 if (!VT1.isInteger() || !VT2.isInteger())
11213 return false;
11214 unsigned NumBits1 = VT1.getSizeInBits();
11215 unsigned NumBits2 = VT2.getSizeInBits();
11216 return NumBits1 == 64 && NumBits2 == 32;
11217}
11218
Hal Finkel5d5d1532015-01-10 08:21:59 +000011219bool PPCTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
11220 // Generally speaking, zexts are not free, but they are free when they can be
11221 // folded with other operations.
11222 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Val)) {
11223 EVT MemVT = LD->getMemoryVT();
11224 if ((MemVT == MVT::i1 || MemVT == MVT::i8 || MemVT == MVT::i16 ||
11225 (Subtarget.isPPC64() && MemVT == MVT::i32)) &&
11226 (LD->getExtensionType() == ISD::NON_EXTLOAD ||
11227 LD->getExtensionType() == ISD::ZEXTLOAD))
11228 return true;
11229 }
11230
11231 // FIXME: Add other cases...
11232 // - 32-bit shifts with a zext to i64
11233 // - zext after ctlz, bswap, etc.
11234 // - zext after and by a constant mask
11235
11236 return TargetLowering::isZExtFree(Val, VT2);
11237}
11238
Olivier Sallenave32509692015-01-13 15:06:36 +000011239bool PPCTargetLowering::isFPExtFree(EVT VT) const {
11240 assert(VT.isFloatingPoint());
11241 return true;
11242}
11243
Hal Finkel34974ed2014-04-12 21:52:38 +000011244bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11245 return isInt<16>(Imm) || isUInt<16>(Imm);
11246}
11247
11248bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const {
11249 return isInt<16>(Imm) || isUInt<16>(Imm);
11250}
11251
Matt Arsenault6f2a5262014-07-27 17:46:40 +000011252bool PPCTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
11253 unsigned,
11254 unsigned,
11255 bool *Fast) const {
Hal Finkel8d7fbc92013-03-15 15:27:13 +000011256 if (DisablePPCUnaligned)
11257 return false;
11258
11259 // PowerPC supports unaligned memory access for simple non-vector types.
11260 // Although accessing unaligned addresses is not as efficient as accessing
11261 // aligned addresses, it is generally more efficient than manual expansion,
11262 // and generally only traps for software emulation when crossing page
11263 // boundaries.
11264
11265 if (!VT.isSimple())
11266 return false;
11267
Hal Finkel6e28e6a2014-03-26 19:39:09 +000011268 if (VT.getSimpleVT().isVector()) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011269 if (Subtarget.hasVSX()) {
Bill Schmidt2d1128a2014-10-17 15:13:38 +000011270 if (VT != MVT::v2f64 && VT != MVT::v2i64 &&
11271 VT != MVT::v4f32 && VT != MVT::v4i32)
Hal Finkel6e28e6a2014-03-26 19:39:09 +000011272 return false;
11273 } else {
11274 return false;
11275 }
11276 }
Hal Finkel8d7fbc92013-03-15 15:27:13 +000011277
11278 if (VT == MVT::ppcf128)
11279 return false;
11280
11281 if (Fast)
11282 *Fast = true;
11283
11284 return true;
11285}
11286
Stephen Lin73de7bf2013-07-09 18:16:56 +000011287bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
11288 VT = VT.getScalarType();
11289
Hal Finkel0a479ae2012-06-22 00:49:52 +000011290 if (!VT.isSimple())
11291 return false;
11292
11293 switch (VT.getSimpleVT().SimpleTy) {
11294 case MVT::f32:
11295 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +000011296 return true;
11297 default:
11298 break;
11299 }
11300
11301 return false;
11302}
11303
Hal Finkel934361a2015-01-14 01:07:51 +000011304const MCPhysReg *
11305PPCTargetLowering::getScratchRegisters(CallingConv::ID) const {
11306 // LR is a callee-save register, but we must treat it as clobbered by any call
11307 // site. Hence we include LR in the scratch registers, which are in turn added
11308 // as implicit-defs for stackmaps and patchpoints. The same reasoning applies
11309 // to CTR, which is used by any indirect call.
11310 static const MCPhysReg ScratchRegs[] = {
Hal Finkelc19805a2015-01-17 03:57:34 +000011311 PPC::X12, PPC::LR8, PPC::CTR8, 0
Hal Finkel934361a2015-01-14 01:07:51 +000011312 };
11313
11314 return ScratchRegs;
11315}
11316
Hal Finkelb4240ca2014-03-31 17:48:16 +000011317bool
11318PPCTargetLowering::shouldExpandBuildVectorWithShuffles(
11319 EVT VT , unsigned DefinedValues) const {
11320 if (VT == MVT::v2i64)
11321 return false;
11322
Hal Finkelc93a9a22015-02-25 01:06:45 +000011323 if (Subtarget.hasQPX()) {
11324 if (VT == MVT::v4f32 || VT == MVT::v4f64 || VT == MVT::v4i1)
11325 return true;
11326 }
11327
Hal Finkelb4240ca2014-03-31 17:48:16 +000011328 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
11329}
11330
Hal Finkel88ed4e32012-04-01 19:23:08 +000011331Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011332 if (DisableILPPref || Subtarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +000011333 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +000011334
Hal Finkel4e9f1a82012-06-10 19:32:29 +000011335 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +000011336}
11337
Bill Schmidt0cf702f2013-07-30 00:50:39 +000011338// Create a fast isel object.
11339FastISel *
11340PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
11341 const TargetLibraryInfo *LibInfo) const {
11342 return PPC::createFastISel(FuncInfo, LibInfo);
11343}