blob: 1d9181b95d1d33703e62e0f990aa9cf1b8e926da [file] [log] [blame]
Nate Begeman0b71e002005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattnerf22556d2005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf22556d2005-08-16 17:14:42 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattnerf22556d2005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6f3b9542005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Hal Finkel934361a2015-01-14 01:07:51 +000016#include "PPCCallingConv.h"
Strahinja Petrovice682b802016-05-09 12:27:39 +000017#include "PPCCCState.h"
Jim Laskey48850c12006-11-16 22:43:37 +000018#include "PPCMachineFunctionInfo.h"
Bill Wendlingdd3fe942010-03-12 02:00:43 +000019#include "PPCPerfectShuffle.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000020#include "PPCTargetMachine.h"
Bill Schmidt22d40dc2013-05-13 19:34:37 +000021#include "PPCTargetObjectFile.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000022#include "llvm/ADT/STLExtras.h"
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +000023#include "llvm/ADT/Statistic.h"
Hal Finkel0d8db462014-05-11 19:29:11 +000024#include "llvm/ADT/StringSwitch.h"
Eric Christopher89958332014-05-31 00:07:32 +000025#include "llvm/ADT/Triple.h"
Chris Lattner4f2e4e02007-03-06 00:59:59 +000026#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner9b577f12005-08-26 21:23:58 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
Hal Finkel57725662015-01-03 17:58:24 +000030#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000032#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000033#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/CallingConv.h"
35#include "llvm/IR/Constants.h"
36#include "llvm/IR/DerivedTypes.h"
37#include "llvm/IR/Function.h"
38#include "llvm/IR/Intrinsics.h"
Chris Lattnerce645542006-11-10 02:08:47 +000039#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000040#include "llvm/Support/ErrorHandling.h"
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +000041#include "llvm/Support/Format.h"
Craig Topperb25fda92012-03-17 18:46:09 +000042#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000043#include "llvm/Support/raw_ostream.h"
Craig Topperb25fda92012-03-17 18:46:09 +000044#include "llvm/Target/TargetOptions.h"
Hal Finkel1fb10e82016-05-12 04:00:56 +000045#include <list>
Kit Bartond4eb73c2015-05-05 16:10:44 +000046
Chris Lattnerf22556d2005-08-16 17:14:42 +000047using namespace llvm;
48
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +000049#define DEBUG_TYPE "ppc-lowering"
50
Hal Finkel595817e2012-06-04 02:21:00 +000051static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
52cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000053
Hal Finkel4e9f1a82012-06-10 19:32:29 +000054static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
55cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
56
Hal Finkel8d7fbc92013-03-15 15:27:13 +000057static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
58cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
59
Chuang-Yu Cheng0600e8d2016-04-26 07:38:24 +000060static cl::opt<bool> DisableSCO("disable-ppc-sco",
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +000061cl::desc("disable sibling call optimization on ppc"), cl::Hidden);
62
63STATISTIC(NumTailCalls, "Number of tail calls");
64STATISTIC(NumSiblingCalls, "Number of sibling calls");
65
Hal Finkel940ab932014-02-28 00:27:01 +000066// FIXME: Remove this once the bug has been fixed!
67extern cl::opt<bool> ANDIGlueBug;
68
Eric Christophercccae792015-01-30 22:02:31 +000069PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM,
70 const PPCSubtarget &STI)
71 : TargetLowering(TM), Subtarget(STI) {
Chris Lattnera028e7a2005-09-27 22:18:25 +000072 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000073 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000075
Chris Lattnerd10babf2010-10-10 18:34:00 +000076 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
77 // arguments are at least 4/8 bytes aligned.
Eric Christopherb1aaebe2014-06-12 22:38:18 +000078 bool isPPC64 = Subtarget.isPPC64();
Evan Cheng39e90022012-07-02 22:39:56 +000079 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000080
Chris Lattnerf22556d2005-08-16 17:14:42 +000081 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000082 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
Eric Christopher317df662016-07-07 01:49:57 +000083 if (!useSoftFloat()) {
Petar Jovanovic280f7102015-12-14 17:57:33 +000084 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
85 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
86 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000087
Evan Cheng5d9fd972006-10-04 00:56:09 +000088 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000089 for (MVT VT : MVT::integer_valuetypes()) {
90 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
91 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand);
92 }
Duncan Sands95d46ef2008-01-23 20:39:46 +000093
Owen Anderson9f944592009-08-11 20:47:22 +000094 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000095
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000096 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000097 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
98 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
99 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
100 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
101 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
Hal Finkel65d1cbf2015-02-05 18:42:53 +0000102 setIndexedLoadAction(ISD::PRE_INC, MVT::f32, Legal);
103 setIndexedLoadAction(ISD::PRE_INC, MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000104 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
105 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
106 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
107 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
108 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Hal Finkel65d1cbf2015-02-05 18:42:53 +0000109 setIndexedStoreAction(ISD::PRE_INC, MVT::f32, Legal);
110 setIndexedStoreAction(ISD::PRE_INC, MVT::f64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +0000111
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000112 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
114
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000115 if (isPPC64 || Subtarget.hasFPCVT()) {
Hal Finkel6a56b212014-03-05 22:14:00 +0000116 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
117 AddPromotedToType (ISD::SINT_TO_FP, MVT::i1,
118 isPPC64 ? MVT::i64 : MVT::i32);
119 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000120 AddPromotedToType(ISD::UINT_TO_FP, MVT::i1,
121 isPPC64 ? MVT::i64 : MVT::i32);
Hal Finkel6a56b212014-03-05 22:14:00 +0000122 } else {
123 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom);
124 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom);
125 }
Hal Finkel940ab932014-02-28 00:27:01 +0000126
127 // PowerPC does not support direct load / store of condition registers
128 setOperationAction(ISD::LOAD, MVT::i1, Custom);
129 setOperationAction(ISD::STORE, MVT::i1, Custom);
130
131 // FIXME: Remove this once the ANDI glue bug is fixed:
132 if (ANDIGlueBug)
133 setOperationAction(ISD::TRUNCATE, MVT::i1, Custom);
134
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000135 for (MVT VT : MVT::integer_valuetypes()) {
136 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
137 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
138 setTruncStoreAction(VT, MVT::i1, Expand);
139 }
Hal Finkel940ab932014-02-28 00:27:01 +0000140
141 addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass);
142 }
143
Dale Johannesen666323e2007-10-10 01:01:31 +0000144 // This is used in the ppcf128->int sequence. Note it has different semantics
145 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +0000146 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000147
Roman Divacky1faf5b02012-08-16 18:19:29 +0000148 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000149 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
150 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
151 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
152 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
153 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000154 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000155
Chris Lattnerf22556d2005-08-16 17:14:42 +0000156 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000157 setOperationAction(ISD::SREM, MVT::i32, Expand);
158 setOperationAction(ISD::UREM, MVT::i32, Expand);
159 setOperationAction(ISD::SREM, MVT::i64, Expand);
160 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000161
162 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000163 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
164 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
165 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
166 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
167 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
168 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
169 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
170 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000171
Dan Gohman482732a2007-10-11 23:21:31 +0000172 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000173 setOperationAction(ISD::FSIN , MVT::f64, Expand);
174 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000175 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000176 setOperationAction(ISD::FREM , MVT::f64, Expand);
177 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000178 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000179 setOperationAction(ISD::FSIN , MVT::f32, Expand);
180 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000181 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000182 setOperationAction(ISD::FREM , MVT::f32, Expand);
183 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000184 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000185
Owen Anderson9f944592009-08-11 20:47:22 +0000186 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000187
Chris Lattnerf22556d2005-08-16 17:14:42 +0000188 // If we're enabling GP optimizations, use hardware square root
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000189 if (!Subtarget.hasFSQRT() &&
Eric Christophercccae792015-01-30 22:02:31 +0000190 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTE() &&
191 Subtarget.hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000192 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000193
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000194 if (!Subtarget.hasFSQRT() &&
Eric Christophercccae792015-01-30 22:02:31 +0000195 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTES() &&
196 Subtarget.hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000197 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000198
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000199 if (Subtarget.hasFCPSGN()) {
Hal Finkeldbc78e12013-08-19 05:01:02 +0000200 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
201 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
202 } else {
203 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
204 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
205 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000206
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000207 if (Subtarget.hasFPRND()) {
Hal Finkelc20a08d2013-03-29 08:57:48 +0000208 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
209 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
210 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000211 setOperationAction(ISD::FROUND, MVT::f64, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000212
213 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
214 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
215 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000216 setOperationAction(ISD::FROUND, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000217 }
218
Nate Begeman2fba8a32006-01-14 03:14:10 +0000219 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000220 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000221 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
222 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000223 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000224
Hal Finkelfa7057a2016-03-29 01:36:01 +0000225 if (Subtarget.hasPOPCNTD() == PPCSubtarget::POPCNTD_Fast) {
Hal Finkel290376d2013-04-01 15:58:15 +0000226 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000227 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
228 } else {
229 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
230 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
231 }
232
Nate Begeman1b8121b2006-01-11 21:21:00 +0000233 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000234 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
235 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000236
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000237 if (!Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000238 // PowerPC does not have Select
239 setOperationAction(ISD::SELECT, MVT::i32, Expand);
240 setOperationAction(ISD::SELECT, MVT::i64, Expand);
241 setOperationAction(ISD::SELECT, MVT::f32, Expand);
242 setOperationAction(ISD::SELECT, MVT::f64, Expand);
243 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000244
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000245 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000246 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
247 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000248
Nate Begeman7e7f4392006-02-01 07:19:44 +0000249 // PowerPC wants to optimize integer setcc a bit
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000250 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000251 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000252
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000253 // PowerPC does not have BRCOND which requires SetCC
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000254 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000255 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000256
Owen Anderson9f944592009-08-11 20:47:22 +0000257 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000258
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000259 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000260 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000261
Jim Laskey6267b2c2005-08-17 00:40:22 +0000262 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000263 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
264 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000265
Nemanja Ivanovic5ebc92d2016-03-24 13:40:33 +0000266 if (Subtarget.hasDirectMove() && isPPC64) {
Nemanja Ivanovic89224762015-12-15 14:50:34 +0000267 setOperationAction(ISD::BITCAST, MVT::f32, Legal);
268 setOperationAction(ISD::BITCAST, MVT::i32, Legal);
269 setOperationAction(ISD::BITCAST, MVT::i64, Legal);
270 setOperationAction(ISD::BITCAST, MVT::f64, Legal);
271 } else {
272 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
273 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
274 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
275 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
276 }
Chris Lattnerc46fc242005-12-23 05:13:35 +0000277
Chris Lattner84b49d52006-04-28 21:56:10 +0000278 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000279 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000280
Hal Finkel1996f3d2013-03-27 19:10:42 +0000281 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000282 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
283 // support continuation, user-level threading, and etc.. As a result, no
284 // other SjLj exception interfaces are implemented and please don't build
285 // your own exception handling based on them.
286 // LLVM/Clang supports zero-cost DWARF exception handling.
287 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
288 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000289
290 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000291 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000292 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
293 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000294 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000295 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
296 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
297 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
298 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000299 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000300 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
301 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000302
Nate Begemanf69d13b2008-08-11 17:36:31 +0000303 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000304 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000305
306 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000307 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
308 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000309
Nate Begemane74795c2006-01-25 18:21:52 +0000310 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000311 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000312
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000313 if (Subtarget.isSVR4ABI()) {
Evan Cheng39e90022012-07-02 22:39:56 +0000314 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000315 // VAARG always uses double-word chunks, so promote anything smaller.
316 setOperationAction(ISD::VAARG, MVT::i1, Promote);
317 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
318 setOperationAction(ISD::VAARG, MVT::i8, Promote);
319 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
320 setOperationAction(ISD::VAARG, MVT::i16, Promote);
321 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
322 setOperationAction(ISD::VAARG, MVT::i32, Promote);
323 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
324 setOperationAction(ISD::VAARG, MVT::Other, Expand);
325 } else {
326 // VAARG is custom lowered with the 32-bit SVR4 ABI.
327 setOperationAction(ISD::VAARG, MVT::Other, Custom);
328 setOperationAction(ISD::VAARG, MVT::i64, Custom);
329 }
Roman Divacky4394e682011-06-28 15:30:42 +0000330 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000331 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000332
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000333 if (Subtarget.isSVR4ABI() && !isPPC64)
Roman Divackyc3825df2013-07-25 21:36:47 +0000334 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
335 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
336 else
337 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
338
Chris Lattner5bd514d2006-01-15 09:02:48 +0000339 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000340 setOperationAction(ISD::VAEND , MVT::Other, Expand);
341 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
342 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
343 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
344 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Yury Gribovd7dbb662015-12-01 11:40:55 +0000345 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i32, Custom);
346 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i64, Custom);
Hal Finkel5081ac22016-09-01 10:28:47 +0000347 setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom);
348 setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000349
Chris Lattner6961fc72006-03-26 10:06:40 +0000350 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000351 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000352
Hal Finkel25c19922013-05-15 21:37:41 +0000353 // To handle counter-based loop conditions.
354 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
355
Dale Johannesen160be0f2008-11-07 22:54:33 +0000356 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000357 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
358 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
359 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
360 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
361 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
362 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
363 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
364 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
365 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
366 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
367 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
368 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000369
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000370 if (Subtarget.has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000371 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000372 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
373 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
374 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
375 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000376 // This is just the low 32 bits of a (signed) fp->i64 conversion.
377 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000378 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000379
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000380 if (Subtarget.hasLFIWAX() || Subtarget.isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000381 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000382 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000383 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000384 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000385 }
386
Hal Finkelf6d45f22013-04-01 17:52:07 +0000387 // With the instructions enabled under FPCVT, we can do everything.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000388 if (Subtarget.hasFPCVT()) {
389 if (Subtarget.has64BitSupport()) {
Hal Finkelf6d45f22013-04-01 17:52:07 +0000390 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
391 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
392 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
393 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
394 }
395
396 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
397 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
398 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
399 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
400 }
401
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000402 if (Subtarget.use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000403 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000404 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000405 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000406 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000407 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000408 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
409 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
410 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000411 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000412 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000413 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
414 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
415 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000416 }
Evan Cheng19264272006-03-01 01:11:20 +0000417
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000418 if (Subtarget.hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000419 // First set operation action for all vector types to expand. Then we
420 // will selectively turn on ones that can be effectively codegen'd.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000421 for (MVT VT : MVT::vector_valuetypes()) {
Chris Lattner06a21ba2006-04-16 01:37:57 +0000422 // add/sub are legal for all supported vector VT's.
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000423 setOperationAction(ISD::ADD, VT, Legal);
424 setOperationAction(ISD::SUB, VT, Legal);
425
Bill Schmidt433b1c32015-02-05 15:24:47 +0000426 // Vector instructions introduced in P8
Kit Bartond4eb73c2015-05-05 16:10:44 +0000427 if (Subtarget.hasP8Altivec() && (VT.SimpleTy != MVT::v1i128)) {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000428 setOperationAction(ISD::CTPOP, VT, Legal);
Bill Schmidt433b1c32015-02-05 15:24:47 +0000429 setOperationAction(ISD::CTLZ, VT, Legal);
430 }
431 else {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000432 setOperationAction(ISD::CTPOP, VT, Expand);
Bill Schmidt433b1c32015-02-05 15:24:47 +0000433 setOperationAction(ISD::CTLZ, VT, Expand);
434 }
Bill Schmidtfe88b182015-02-03 21:58:23 +0000435
Chris Lattner95c7adc2006-04-04 17:25:31 +0000436 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000437 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000438 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000439
440 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000441 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000442 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000443 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000444 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000445 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000446 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000447 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000448 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000449 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000450 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Hal Finkela2cdbce2015-08-30 22:12:50 +0000451 setOperationAction(ISD::SELECT_CC, VT, Promote);
452 AddPromotedToType (ISD::SELECT_CC, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000453 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000454 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000455
Chris Lattner06a21ba2006-04-16 01:37:57 +0000456 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000457 setOperationAction(ISD::MUL , VT, Expand);
458 setOperationAction(ISD::SDIV, VT, Expand);
459 setOperationAction(ISD::SREM, VT, Expand);
460 setOperationAction(ISD::UDIV, VT, Expand);
461 setOperationAction(ISD::UREM, VT, Expand);
462 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000463 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000464 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000465 setOperationAction(ISD::FSQRT, VT, Expand);
466 setOperationAction(ISD::FLOG, VT, Expand);
467 setOperationAction(ISD::FLOG10, VT, Expand);
468 setOperationAction(ISD::FLOG2, VT, Expand);
469 setOperationAction(ISD::FEXP, VT, Expand);
470 setOperationAction(ISD::FEXP2, VT, Expand);
471 setOperationAction(ISD::FSIN, VT, Expand);
472 setOperationAction(ISD::FCOS, VT, Expand);
473 setOperationAction(ISD::FABS, VT, Expand);
474 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000475 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000476 setOperationAction(ISD::FCEIL, VT, Expand);
477 setOperationAction(ISD::FTRUNC, VT, Expand);
478 setOperationAction(ISD::FRINT, VT, Expand);
479 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000480 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
481 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
482 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
Ulrich Weigand51eccec2014-08-04 13:27:12 +0000483 setOperationAction(ISD::MULHU, VT, Expand);
484 setOperationAction(ISD::MULHS, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000485 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
486 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
487 setOperationAction(ISD::UDIVREM, VT, Expand);
488 setOperationAction(ISD::SDIVREM, VT, Expand);
489 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
490 setOperationAction(ISD::FPOW, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000491 setOperationAction(ISD::BSWAP, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000492 setOperationAction(ISD::CTTZ, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000493 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000494 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Nemanja Ivanovic74e31bc2015-12-02 10:36:24 +0000495 setOperationAction(ISD::ROTL, VT, Expand);
496 setOperationAction(ISD::ROTR, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000497
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000498 for (MVT InnerVT : MVT::vector_valuetypes()) {
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000499 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000500 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
501 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
502 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
503 }
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000504 }
505
Chris Lattner95c7adc2006-04-04 17:25:31 +0000506 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
507 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000508 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000509
Owen Anderson9f944592009-08-11 20:47:22 +0000510 setOperationAction(ISD::AND , MVT::v4i32, Legal);
511 setOperationAction(ISD::OR , MVT::v4i32, Legal);
512 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
513 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Hal Finkel940ab932014-02-28 00:27:01 +0000514 setOperationAction(ISD::SELECT, MVT::v4i32,
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000515 Subtarget.useCRBits() ? Legal : Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000516 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000517 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
518 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
519 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
520 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000521 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
522 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
523 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
524 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000525
Craig Topperabadc662012-04-20 06:31:50 +0000526 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
527 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
528 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
529 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000530
Owen Anderson9f944592009-08-11 20:47:22 +0000531 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000532 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000533
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000534 if (TM.Options.UnsafeFPMath || Subtarget.hasVSX()) {
Hal Finkel2e103312013-04-03 04:01:11 +0000535 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
536 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
537 }
538
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000539 if (Subtarget.hasP8Altivec())
Kit Barton20d39812015-03-10 19:49:38 +0000540 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
541 else
542 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000543
Owen Anderson9f944592009-08-11 20:47:22 +0000544 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
545 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000546
Owen Anderson9f944592009-08-11 20:47:22 +0000547 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
548 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000549
Owen Anderson9f944592009-08-11 20:47:22 +0000550 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
551 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
552 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
553 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000554
555 // Altivec does not contain unordered floating-point compare instructions
556 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
557 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000558 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
559 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000560
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000561 if (Subtarget.hasVSX()) {
Hal Finkel27774d92014-03-13 07:58:58 +0000562 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
Nemanja Ivanovicd3896572015-10-09 11:12:18 +0000563 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
564 if (Subtarget.hasP8Vector()) {
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +0000565 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Nemanja Ivanovicd3896572015-10-09 11:12:18 +0000566 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
567 }
Nemanja Ivanovic5ebc92d2016-03-24 13:40:33 +0000568 if (Subtarget.hasDirectMove() && isPPC64) {
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +0000569 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Legal);
570 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Legal);
571 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Legal);
Nemanja Ivanovicbe5f0c02015-11-02 14:01:11 +0000572 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Legal);
Nemanja Ivanovicd3896572015-10-09 11:12:18 +0000573 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Legal);
574 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Legal);
575 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
576 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +0000577 }
Hal Finkel82569b62014-03-27 22:22:48 +0000578 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
Hal Finkel27774d92014-03-13 07:58:58 +0000579
580 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
581 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
582 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
583 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
584 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
585
586 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
587
588 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
589 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
590
591 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
592 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
593
Hal Finkel732f0f72014-03-26 12:49:28 +0000594 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
595 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal);
596 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
597 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
598 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
599
Hal Finkel27774d92014-03-13 07:58:58 +0000600 // Share the Altivec comparison restrictions.
601 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand);
602 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000603 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand);
604 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand);
605
Hal Finkel9281c9a2014-03-26 18:26:30 +0000606 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
607 setOperationAction(ISD::STORE, MVT::v2f64, Legal);
608
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000609 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal);
610
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +0000611 if (Subtarget.hasP8Vector())
612 addRegisterClass(MVT::f32, &PPC::VSSRCRegClass);
613
Hal Finkel19be5062014-03-29 05:29:01 +0000614 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000615
Bill Schmidt54cced52015-07-16 21:14:07 +0000616 addRegisterClass(MVT::v4i32, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000617 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass);
618 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass);
Hal Finkela6c8b512014-03-26 16:12:58 +0000619
Kit Barton0cfa7b72015-03-03 19:55:45 +0000620 if (Subtarget.hasP8Altivec()) {
Kit Bartone48b1e12015-03-05 16:24:38 +0000621 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
622 setOperationAction(ISD::SRA, MVT::v2i64, Legal);
623 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
624
Kit Barton0cfa7b72015-03-03 19:55:45 +0000625 setOperationAction(ISD::SETCC, MVT::v2i64, Legal);
626 }
627 else {
Kit Bartone48b1e12015-03-05 16:24:38 +0000628 setOperationAction(ISD::SHL, MVT::v2i64, Expand);
629 setOperationAction(ISD::SRA, MVT::v2i64, Expand);
630 setOperationAction(ISD::SRL, MVT::v2i64, Expand);
631
Kit Barton0cfa7b72015-03-03 19:55:45 +0000632 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
633
634 // VSX v2i64 only supports non-arithmetic operations.
635 setOperationAction(ISD::ADD, MVT::v2i64, Expand);
636 setOperationAction(ISD::SUB, MVT::v2i64, Expand);
637 }
Hal Finkel777c9dd2014-03-29 16:04:40 +0000638
Hal Finkel9281c9a2014-03-26 18:26:30 +0000639 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
640 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64);
641 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
642 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64);
643
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000644 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal);
645
Hal Finkel7279f4b2014-03-26 19:13:54 +0000646 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
647 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
648 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
649 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
650
Hal Finkel5c0d1452014-03-30 13:22:59 +0000651 // Vector operation legalization checks the result type of
652 // SIGN_EXTEND_INREG, overall legalization checks the inner type.
653 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal);
654 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal);
655 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
656 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
657
Kit Barton915c5ec2016-02-26 21:59:44 +0000658 setOperationAction(ISD::FNEG, MVT::v4f32, Legal);
659 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
Kit Bartona1d6a6f2016-03-09 17:48:01 +0000660 setOperationAction(ISD::FABS, MVT::v4f32, Legal);
661 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
Kit Barton915c5ec2016-02-26 21:59:44 +0000662
Hal Finkela6c8b512014-03-26 16:12:58 +0000663 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000664 }
Bill Schmidtfe88b182015-02-03 21:58:23 +0000665
Kit Bartond4eb73c2015-05-05 16:10:44 +0000666 if (Subtarget.hasP8Altivec()) {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000667 addRegisterClass(MVT::v2i64, &PPC::VRRCRegClass);
Kit Bartond4eb73c2015-05-05 16:10:44 +0000668 addRegisterClass(MVT::v1i128, &PPC::VRRCRegClass);
669 }
Nemanja Ivanovicd5deb482016-09-14 14:19:09 +0000670
Nemanja Ivanovicb43bb612016-07-12 21:00:10 +0000671 if (Subtarget.hasP9Vector()) {
Nemanja Ivanovicd5deb482016-09-14 14:19:09 +0000672 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
673 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nemanja Ivanovicb43bb612016-07-12 21:00:10 +0000674 }
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000675 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000676
Hal Finkelc93a9a22015-02-25 01:06:45 +0000677 if (Subtarget.hasQPX()) {
678 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
679 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
680 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
681 setOperationAction(ISD::FREM, MVT::v4f64, Expand);
682
683 setOperationAction(ISD::FCOPYSIGN, MVT::v4f64, Legal);
684 setOperationAction(ISD::FGETSIGN, MVT::v4f64, Expand);
685
686 setOperationAction(ISD::LOAD , MVT::v4f64, Custom);
687 setOperationAction(ISD::STORE , MVT::v4f64, Custom);
688
689 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Custom);
690 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Custom);
691
692 if (!Subtarget.useCRBits())
693 setOperationAction(ISD::SELECT, MVT::v4f64, Expand);
694 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
695
696 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f64, Legal);
697 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f64, Expand);
698 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f64, Expand);
699 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f64, Expand);
700 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f64, Custom);
701 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f64, Legal);
702 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
703
704 setOperationAction(ISD::FP_TO_SINT , MVT::v4f64, Legal);
705 setOperationAction(ISD::FP_TO_UINT , MVT::v4f64, Expand);
706
707 setOperationAction(ISD::FP_ROUND , MVT::v4f32, Legal);
708 setOperationAction(ISD::FP_ROUND_INREG , MVT::v4f32, Expand);
709 setOperationAction(ISD::FP_EXTEND, MVT::v4f64, Legal);
710
711 setOperationAction(ISD::FNEG , MVT::v4f64, Legal);
712 setOperationAction(ISD::FABS , MVT::v4f64, Legal);
713 setOperationAction(ISD::FSIN , MVT::v4f64, Expand);
714 setOperationAction(ISD::FCOS , MVT::v4f64, Expand);
715 setOperationAction(ISD::FPOWI , MVT::v4f64, Expand);
716 setOperationAction(ISD::FPOW , MVT::v4f64, Expand);
717 setOperationAction(ISD::FLOG , MVT::v4f64, Expand);
718 setOperationAction(ISD::FLOG2 , MVT::v4f64, Expand);
719 setOperationAction(ISD::FLOG10 , MVT::v4f64, Expand);
720 setOperationAction(ISD::FEXP , MVT::v4f64, Expand);
721 setOperationAction(ISD::FEXP2 , MVT::v4f64, Expand);
722
723 setOperationAction(ISD::FMINNUM, MVT::v4f64, Legal);
724 setOperationAction(ISD::FMAXNUM, MVT::v4f64, Legal);
725
726 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f64, Legal);
727 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f64, Legal);
728
729 addRegisterClass(MVT::v4f64, &PPC::QFRCRegClass);
730
731 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
732 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
733 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
734 setOperationAction(ISD::FREM, MVT::v4f32, Expand);
735
736 setOperationAction(ISD::FCOPYSIGN, MVT::v4f32, Legal);
737 setOperationAction(ISD::FGETSIGN, MVT::v4f32, Expand);
738
739 setOperationAction(ISD::LOAD , MVT::v4f32, Custom);
740 setOperationAction(ISD::STORE , MVT::v4f32, Custom);
741
742 if (!Subtarget.useCRBits())
743 setOperationAction(ISD::SELECT, MVT::v4f32, Expand);
744 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
745
746 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f32, Legal);
747 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f32, Expand);
748 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f32, Expand);
749 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f32, Expand);
750 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f32, Custom);
751 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
752 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
753
754 setOperationAction(ISD::FP_TO_SINT , MVT::v4f32, Legal);
755 setOperationAction(ISD::FP_TO_UINT , MVT::v4f32, Expand);
756
757 setOperationAction(ISD::FNEG , MVT::v4f32, Legal);
758 setOperationAction(ISD::FABS , MVT::v4f32, Legal);
759 setOperationAction(ISD::FSIN , MVT::v4f32, Expand);
760 setOperationAction(ISD::FCOS , MVT::v4f32, Expand);
761 setOperationAction(ISD::FPOWI , MVT::v4f32, Expand);
762 setOperationAction(ISD::FPOW , MVT::v4f32, Expand);
763 setOperationAction(ISD::FLOG , MVT::v4f32, Expand);
764 setOperationAction(ISD::FLOG2 , MVT::v4f32, Expand);
765 setOperationAction(ISD::FLOG10 , MVT::v4f32, Expand);
766 setOperationAction(ISD::FEXP , MVT::v4f32, Expand);
767 setOperationAction(ISD::FEXP2 , MVT::v4f32, Expand);
768
769 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
770 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
771
772 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f32, Legal);
773 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f32, Legal);
774
775 addRegisterClass(MVT::v4f32, &PPC::QSRCRegClass);
776
777 setOperationAction(ISD::AND , MVT::v4i1, Legal);
778 setOperationAction(ISD::OR , MVT::v4i1, Legal);
779 setOperationAction(ISD::XOR , MVT::v4i1, Legal);
780
781 if (!Subtarget.useCRBits())
782 setOperationAction(ISD::SELECT, MVT::v4i1, Expand);
783 setOperationAction(ISD::VSELECT, MVT::v4i1, Legal);
784
785 setOperationAction(ISD::LOAD , MVT::v4i1, Custom);
786 setOperationAction(ISD::STORE , MVT::v4i1, Custom);
787
788 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4i1, Custom);
789 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4i1, Expand);
790 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4i1, Expand);
791 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4i1, Expand);
792 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4i1, Custom);
793 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i1, Expand);
794 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i1, Custom);
795
796 setOperationAction(ISD::SINT_TO_FP, MVT::v4i1, Custom);
797 setOperationAction(ISD::UINT_TO_FP, MVT::v4i1, Custom);
798
799 addRegisterClass(MVT::v4i1, &PPC::QBRCRegClass);
800
801 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
802 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
803 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
804 setOperationAction(ISD::FROUND, MVT::v4f64, Legal);
805
806 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
807 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
808 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
809 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
810
811 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Expand);
812 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
813
814 // These need to set FE_INEXACT, and so cannot be vectorized here.
815 setOperationAction(ISD::FRINT, MVT::v4f64, Expand);
816 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
817
818 if (TM.Options.UnsafeFPMath) {
819 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
820 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
821
822 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
823 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
824 } else {
825 setOperationAction(ISD::FDIV, MVT::v4f64, Expand);
826 setOperationAction(ISD::FSQRT, MVT::v4f64, Expand);
827
828 setOperationAction(ISD::FDIV, MVT::v4f32, Expand);
829 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
830 }
831 }
832
Hal Finkel01fa7702014-12-03 00:19:17 +0000833 if (Subtarget.has64BitSupport())
Hal Finkel322e41a2012-04-01 20:08:17 +0000834 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel01fa7702014-12-03 00:19:17 +0000835
836 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, isPPC64 ? Legal : Custom);
Hal Finkel322e41a2012-04-01 20:08:17 +0000837
Robin Morissete1ca44b2014-10-02 22:27:07 +0000838 if (!isPPC64) {
839 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
840 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
841 }
Eli Friedman7dfa7912011-08-29 18:23:02 +0000842
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000843 setBooleanContents(ZeroOrOneBooleanContent);
Hal Finkelc93a9a22015-02-25 01:06:45 +0000844
845 if (Subtarget.hasAltivec()) {
846 // Altivec instructions set fields to all zeros or all ones.
847 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
848 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000849
Joerg Sonnenbergerb5459e62014-07-24 22:20:10 +0000850 if (!isPPC64) {
851 // These libcalls are not available in 32-bit.
852 setLibcallName(RTLIB::SHL_I128, nullptr);
853 setLibcallName(RTLIB::SRL_I128, nullptr);
854 setLibcallName(RTLIB::SRA_I128, nullptr);
855 }
856
Joseph Tremouletf748c892015-11-07 01:11:31 +0000857 setStackPointerRegisterToSaveRestore(isPPC64 ? PPC::X1 : PPC::R1);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000858
Chris Lattnerf4184352006-03-01 04:57:39 +0000859 // We have target-specific dag combine patterns for the following nodes:
860 setTargetDAGCombine(ISD::SINT_TO_FP);
Nemanja Ivanovic44513e52016-07-05 09:22:29 +0000861 setTargetDAGCombine(ISD::BUILD_VECTOR);
Hal Finkel5efb9182015-01-06 06:01:57 +0000862 if (Subtarget.hasFPCVT())
863 setTargetDAGCombine(ISD::UINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000864 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000865 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000866 setTargetDAGCombine(ISD::BR_CC);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000867 if (Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000868 setTargetDAGCombine(ISD::BRCOND);
Chris Lattnera7976d32006-07-10 20:56:58 +0000869 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000870 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Bill Schmidtfae5d712014-12-09 16:35:51 +0000871 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
872 setTargetDAGCombine(ISD::INTRINSIC_VOID);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000873
Hal Finkel46043ed2014-03-01 21:36:57 +0000874 setTargetDAGCombine(ISD::SIGN_EXTEND);
875 setTargetDAGCombine(ISD::ZERO_EXTEND);
876 setTargetDAGCombine(ISD::ANY_EXTEND);
877
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000878 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000879 setTargetDAGCombine(ISD::TRUNCATE);
880 setTargetDAGCombine(ISD::SETCC);
881 setTargetDAGCombine(ISD::SELECT_CC);
882 }
883
Hal Finkel2e103312013-04-03 04:01:11 +0000884 // Use reciprocal estimates.
885 if (TM.Options.UnsafeFPMath) {
886 setTargetDAGCombine(ISD::FDIV);
887 setTargetDAGCombine(ISD::FSQRT);
888 }
889
Dale Johannesen10432e52007-10-19 00:59:18 +0000890 // Darwin long double math library functions have $LDBL128 appended.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000891 if (Subtarget.isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000892 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000893 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
894 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000895 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
896 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000897 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
898 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
899 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
900 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
901 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000902 }
903
Hal Finkel940ab932014-02-28 00:27:01 +0000904 // With 32 condition bits, we don't need to sink (and duplicate) compares
905 // aggressively in CodeGenPrep.
Hal Finkel7a0516e2015-02-12 01:02:52 +0000906 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000907 setHasMultipleConditionRegisters();
Hal Finkel7a0516e2015-02-12 01:02:52 +0000908 setJumpIsExpensive();
909 }
Hal Finkel940ab932014-02-28 00:27:01 +0000910
Hal Finkel65298572011-10-17 18:53:03 +0000911 setMinFunctionAlignment(2);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000912 if (Subtarget.isDarwin())
Hal Finkel65298572011-10-17 18:53:03 +0000913 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000914
Hal Finkeld73bfba2015-01-03 14:58:25 +0000915 switch (Subtarget.getDarwinDirective()) {
916 default: break;
917 case PPC::DIR_970:
918 case PPC::DIR_A2:
919 case PPC::DIR_E500mc:
920 case PPC::DIR_E5500:
921 case PPC::DIR_PWR4:
922 case PPC::DIR_PWR5:
923 case PPC::DIR_PWR5X:
924 case PPC::DIR_PWR6:
925 case PPC::DIR_PWR6X:
926 case PPC::DIR_PWR7:
927 case PPC::DIR_PWR8:
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +0000928 case PPC::DIR_PWR9:
Hal Finkeld73bfba2015-01-03 14:58:25 +0000929 setPrefFunctionAlignment(4);
930 setPrefLoopAlignment(4);
931 break;
932 }
933
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000934 if (Subtarget.enableMachineScheduler())
Hal Finkel21442b22013-09-11 23:05:25 +0000935 setSchedulingPreference(Sched::Source);
936 else
937 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000938
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000939 computeRegisterProperties(STI.getRegisterInfo());
Hal Finkel742b5352012-08-28 16:12:39 +0000940
Hal Finkeld73bfba2015-01-03 14:58:25 +0000941 // The Freescale cores do better with aggressive inlining of memcpy and
942 // friends. GCC uses same threshold of 128 bytes (= 32 word stores).
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000943 if (Subtarget.getDarwinDirective() == PPC::DIR_E500mc ||
944 Subtarget.getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000945 MaxStoresPerMemset = 32;
946 MaxStoresPerMemsetOptSize = 16;
947 MaxStoresPerMemcpy = 32;
948 MaxStoresPerMemcpyOptSize = 8;
949 MaxStoresPerMemmove = 32;
950 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel5c3cacf2015-02-27 19:58:28 +0000951 } else if (Subtarget.getDarwinDirective() == PPC::DIR_A2) {
952 // The A2 also benefits from (very) aggressive inlining of memcpy and
953 // friends. The overhead of a the function call, even when warm, can be
954 // over one hundred cycles.
955 MaxStoresPerMemset = 128;
956 MaxStoresPerMemcpy = 128;
957 MaxStoresPerMemmove = 128;
Hal Finkel742b5352012-08-28 16:12:39 +0000958 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000959}
960
Hal Finkel262a2242013-09-12 23:20:06 +0000961/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
962/// the desired ByVal argument alignment.
Pete Cooper2e201472015-07-27 17:15:24 +0000963static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
Hal Finkel262a2242013-09-12 23:20:06 +0000964 unsigned MaxMaxAlign) {
965 if (MaxAlign == MaxMaxAlign)
966 return;
Pete Cooper2e201472015-07-27 17:15:24 +0000967 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Hal Finkel262a2242013-09-12 23:20:06 +0000968 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
969 MaxAlign = 32;
970 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
971 MaxAlign = 16;
Pete Cooper2e201472015-07-27 17:15:24 +0000972 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Hal Finkel262a2242013-09-12 23:20:06 +0000973 unsigned EltAlign = 0;
974 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
975 if (EltAlign > MaxAlign)
976 MaxAlign = EltAlign;
Pete Cooper2e201472015-07-27 17:15:24 +0000977 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
978 for (auto *EltTy : STy->elements()) {
Hal Finkel262a2242013-09-12 23:20:06 +0000979 unsigned EltAlign = 0;
Pete Cooper0debbdc2015-07-24 18:55:49 +0000980 getMaxByValAlign(EltTy, EltAlign, MaxMaxAlign);
Hal Finkel262a2242013-09-12 23:20:06 +0000981 if (EltAlign > MaxAlign)
982 MaxAlign = EltAlign;
983 if (MaxAlign == MaxMaxAlign)
984 break;
985 }
986 }
987}
988
Dale Johannesencbde4c22008-02-28 22:31:51 +0000989/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
990/// function arguments in the caller parameter area.
Mehdi Amini5c183d52015-07-09 02:09:28 +0000991unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty,
992 const DataLayout &DL) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000993 // Darwin passes everything on 4 byte boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000994 if (Subtarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000995 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000996
997 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000998 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000999 unsigned Align = Subtarget.isPPC64() ? 8 : 4;
1000 if (Subtarget.hasAltivec() || Subtarget.hasQPX())
1001 getMaxByValAlign(Ty, Align, Subtarget.hasQPX() ? 32 : 16);
Hal Finkel262a2242013-09-12 23:20:06 +00001002 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +00001003}
1004
Petar Jovanovic280f7102015-12-14 17:57:33 +00001005bool PPCTargetLowering::useSoftFloat() const {
1006 return Subtarget.useSoftFloat();
1007}
1008
Chris Lattner347ed8a2006-01-09 23:52:17 +00001009const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001010 switch ((PPCISD::NodeType)Opcode) {
1011 case PPCISD::FIRST_NUMBER: break;
Evan Cheng32e376f2008-07-12 02:23:19 +00001012 case PPCISD::FSEL: return "PPCISD::FSEL";
1013 case PPCISD::FCFID: return "PPCISD::FCFID";
Hal Finkel3fe09ea2015-01-06 07:02:15 +00001014 case PPCISD::FCFIDU: return "PPCISD::FCFIDU";
1015 case PPCISD::FCFIDS: return "PPCISD::FCFIDS";
1016 case PPCISD::FCFIDUS: return "PPCISD::FCFIDUS";
Evan Cheng32e376f2008-07-12 02:23:19 +00001017 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
1018 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel3fe09ea2015-01-06 07:02:15 +00001019 case PPCISD::FCTIDUZ: return "PPCISD::FCTIDUZ";
1020 case PPCISD::FCTIWUZ: return "PPCISD::FCTIWUZ";
Hal Finkel2e103312013-04-03 04:01:11 +00001021 case PPCISD::FRE: return "PPCISD::FRE";
1022 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +00001023 case PPCISD::STFIWX: return "PPCISD::STFIWX";
1024 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
1025 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
1026 case PPCISD::VPERM: return "PPCISD::VPERM";
Nemanja Ivanovic1a2b2f02016-05-04 16:04:02 +00001027 case PPCISD::XXSPLT: return "PPCISD::XXSPLT";
Nemanja Ivanovicb43bb612016-07-12 21:00:10 +00001028 case PPCISD::XXINSERT: return "PPCISD::XXINSERT";
1029 case PPCISD::VECSHL: return "PPCISD::VECSHL";
Hal Finkel4edc66b2015-01-03 01:16:37 +00001030 case PPCISD::CMPB: return "PPCISD::CMPB";
Evan Cheng32e376f2008-07-12 02:23:19 +00001031 case PPCISD::Hi: return "PPCISD::Hi";
1032 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001033 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Evan Cheng32e376f2008-07-12 02:23:19 +00001034 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
Yury Gribovd7dbb662015-12-01 11:40:55 +00001035 case PPCISD::DYNAREAOFFSET: return "PPCISD::DYNAREAOFFSET";
Evan Cheng32e376f2008-07-12 02:23:19 +00001036 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
1037 case PPCISD::SRL: return "PPCISD::SRL";
1038 case PPCISD::SRA: return "PPCISD::SRA";
1039 case PPCISD::SHL: return "PPCISD::SHL";
Matthias Braund04893f2015-05-07 21:33:59 +00001040 case PPCISD::SRA_ADDZE: return "PPCISD::SRA_ADDZE";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00001041 case PPCISD::CALL: return "PPCISD::CALL";
1042 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +00001043 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00001044 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Hal Finkelfc096c92014-12-23 22:29:40 +00001045 case PPCISD::BCTRL_LOAD_TOC: return "PPCISD::BCTRL_LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +00001046 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkelbbdee932014-12-02 22:01:00 +00001047 case PPCISD::READ_TIME_BASE: return "PPCISD::READ_TIME_BASE";
Hal Finkel756810f2013-03-21 21:37:52 +00001048 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
1049 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001050 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001051 case PPCISD::MFVSR: return "PPCISD::MFVSR";
1052 case PPCISD::MTVSRA: return "PPCISD::MTVSRA";
1053 case PPCISD::MTVSRZ: return "PPCISD::MTVSRZ";
Nemanja Ivanovic44513e52016-07-05 09:22:29 +00001054 case PPCISD::SINT_VEC_TO_FP: return "PPCISD::SINT_VEC_TO_FP";
1055 case PPCISD::UINT_VEC_TO_FP: return "PPCISD::UINT_VEC_TO_FP";
Matthias Braund04893f2015-05-07 21:33:59 +00001056 case PPCISD::ANDIo_1_EQ_BIT: return "PPCISD::ANDIo_1_EQ_BIT";
1057 case PPCISD::ANDIo_1_GT_BIT: return "PPCISD::ANDIo_1_GT_BIT";
Evan Cheng32e376f2008-07-12 02:23:19 +00001058 case PPCISD::VCMP: return "PPCISD::VCMP";
1059 case PPCISD::VCMPo: return "PPCISD::VCMPo";
1060 case PPCISD::LBRX: return "PPCISD::LBRX";
1061 case PPCISD::STBRX: return "PPCISD::STBRX";
Hal Finkel3fe09ea2015-01-06 07:02:15 +00001062 case PPCISD::LFIWAX: return "PPCISD::LFIWAX";
1063 case PPCISD::LFIWZX: return "PPCISD::LFIWZX";
Matthias Braund04893f2015-05-07 21:33:59 +00001064 case PPCISD::LXVD2X: return "PPCISD::LXVD2X";
1065 case PPCISD::STXVD2X: return "PPCISD::STXVD2X";
Evan Cheng32e376f2008-07-12 02:23:19 +00001066 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +00001067 case PPCISD::BDNZ: return "PPCISD::BDNZ";
1068 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +00001069 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +00001070 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +00001071 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +00001072 case PPCISD::CR6SET: return "PPCISD::CR6SET";
1073 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Roman Divacky32143e22013-12-20 18:08:54 +00001074 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Matthias Braund04893f2015-05-07 21:33:59 +00001075 case PPCISD::PPC32_PICGOT: return "PPCISD::PPC32_PICGOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001076 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
1077 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001078 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001079 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
1080 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
Bill Schmidt82f1c772015-02-10 19:09:05 +00001081 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
1082 case PPCISD::ADDI_TLSGD_L_ADDR: return "PPCISD::ADDI_TLSGD_L_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001083 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
1084 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
Bill Schmidt82f1c772015-02-10 19:09:05 +00001085 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
1086 case PPCISD::ADDI_TLSLD_L_ADDR: return "PPCISD::ADDI_TLSLD_L_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001087 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
1088 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +00001089 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +00001090 case PPCISD::SC: return "PPCISD::SC";
Bill Schmidte26236e2015-05-22 16:44:10 +00001091 case PPCISD::CLRBHRB: return "PPCISD::CLRBHRB";
1092 case PPCISD::MFBHRBE: return "PPCISD::MFBHRBE";
1093 case PPCISD::RFEBB: return "PPCISD::RFEBB";
Matthias Braund04893f2015-05-07 21:33:59 +00001094 case PPCISD::XXSWAPD: return "PPCISD::XXSWAPD";
Nemanja Ivanoviceebbcb62016-07-12 12:16:27 +00001095 case PPCISD::SWAP_NO_CHAIN: return "PPCISD::SWAP_NO_CHAIN";
Hal Finkelc93a9a22015-02-25 01:06:45 +00001096 case PPCISD::QVFPERM: return "PPCISD::QVFPERM";
1097 case PPCISD::QVGPCI: return "PPCISD::QVGPCI";
1098 case PPCISD::QVALIGNI: return "PPCISD::QVALIGNI";
1099 case PPCISD::QVESPLATI: return "PPCISD::QVESPLATI";
1100 case PPCISD::QBFLT: return "PPCISD::QBFLT";
1101 case PPCISD::QVLFSb: return "PPCISD::QVLFSb";
Chris Lattner347ed8a2006-01-09 23:52:17 +00001102 }
Matthias Braund04893f2015-05-07 21:33:59 +00001103 return nullptr;
Chris Lattner347ed8a2006-01-09 23:52:17 +00001104}
1105
Mehdi Amini44ede332015-07-09 02:09:04 +00001106EVT PPCTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &C,
1107 EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00001108 if (!VT.isVector())
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001109 return Subtarget.useCRBits() ? MVT::i1 : MVT::i32;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001110
1111 if (Subtarget.hasQPX())
1112 return EVT::getVectorVT(C, MVT::i1, VT.getVectorNumElements());
1113
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00001114 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +00001115}
1116
Hal Finkel62ac7362014-09-19 11:42:56 +00001117bool PPCTargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1118 assert(VT.isFloatingPoint() && "Non-floating-point FMA?");
1119 return true;
1120}
1121
Chris Lattner4211ca92006-04-14 06:01:58 +00001122//===----------------------------------------------------------------------===//
1123// Node matching predicates, for use by the tblgen matching code.
1124//===----------------------------------------------------------------------===//
1125
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001126/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001127static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001128 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001129 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001130 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001131 // Maybe this has already been legalized into the constant pool?
1132 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001133 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001134 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001135 }
1136 return false;
1137}
1138
Chris Lattnere8b83b42006-04-06 17:23:16 +00001139/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
1140/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001141static bool isConstantOrUndef(int Op, int Val) {
1142 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001143}
1144
1145/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
1146/// VPKUHUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001147/// The ShuffleKind distinguishes between big-endian operations with
1148/// two different inputs (0), either-endian operations with two identical
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001149/// inputs (1), and little-endian operations with two different inputs (2).
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001150/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1151bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +00001152 SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001153 bool IsLE = DAG.getDataLayout().isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001154 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +00001155 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001156 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001157 for (unsigned i = 0; i != 16; ++i)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001158 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001159 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001160 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +00001161 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001162 return false;
1163 for (unsigned i = 0; i != 16; ++i)
1164 if (!isConstantOrUndef(N->getMaskElt(i), i*2))
1165 return false;
1166 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +00001167 unsigned j = IsLE ? 0 : 1;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001168 for (unsigned i = 0; i != 8; ++i)
Bill Schmidtf910a062014-06-10 14:35:01 +00001169 if (!isConstantOrUndef(N->getMaskElt(i), i*2+j) ||
1170 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001171 return false;
1172 }
Chris Lattner1d338192006-04-06 18:26:28 +00001173 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001174}
1175
1176/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
1177/// VPKUWUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001178/// The ShuffleKind distinguishes between big-endian operations with
1179/// two different inputs (0), either-endian operations with two identical
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001180/// inputs (1), and little-endian operations with two different inputs (2).
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001181/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1182bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +00001183 SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001184 bool IsLE = DAG.getDataLayout().isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001185 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +00001186 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001187 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001188 for (unsigned i = 0; i != 16; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001189 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
1190 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001191 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001192 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +00001193 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001194 return false;
1195 for (unsigned i = 0; i != 16; i += 2)
1196 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) ||
1197 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1))
1198 return false;
1199 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +00001200 unsigned j = IsLE ? 0 : 2;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001201 for (unsigned i = 0; i != 8; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001202 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) ||
1203 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) ||
1204 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) ||
1205 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001206 return false;
1207 }
Chris Lattner1d338192006-04-06 18:26:28 +00001208 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001209}
1210
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001211/// isVPKUDUMShuffleMask - Return true if this is the shuffle mask for a
Bill Schmidte13ac912015-05-21 20:48:49 +00001212/// VPKUDUM instruction, AND the VPKUDUM instruction exists for the
1213/// current subtarget.
1214///
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001215/// The ShuffleKind distinguishes between big-endian operations with
1216/// two different inputs (0), either-endian operations with two identical
1217/// inputs (1), and little-endian operations with two different inputs (2).
1218/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1219bool PPC::isVPKUDUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
1220 SelectionDAG &DAG) {
Bill Schmidte13ac912015-05-21 20:48:49 +00001221 const PPCSubtarget& Subtarget =
1222 static_cast<const PPCSubtarget&>(DAG.getSubtarget());
1223 if (!Subtarget.hasP8Vector())
1224 return false;
1225
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001226 bool IsLE = DAG.getDataLayout().isLittleEndian();
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001227 if (ShuffleKind == 0) {
1228 if (IsLE)
1229 return false;
1230 for (unsigned i = 0; i != 16; i += 4)
1231 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+4) ||
1232 !isConstantOrUndef(N->getMaskElt(i+1), i*2+5) ||
1233 !isConstantOrUndef(N->getMaskElt(i+2), i*2+6) ||
1234 !isConstantOrUndef(N->getMaskElt(i+3), i*2+7))
1235 return false;
1236 } else if (ShuffleKind == 2) {
1237 if (!IsLE)
1238 return false;
1239 for (unsigned i = 0; i != 16; i += 4)
1240 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) ||
1241 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1) ||
1242 !isConstantOrUndef(N->getMaskElt(i+2), i*2+2) ||
1243 !isConstantOrUndef(N->getMaskElt(i+3), i*2+3))
1244 return false;
1245 } else if (ShuffleKind == 1) {
1246 unsigned j = IsLE ? 0 : 4;
1247 for (unsigned i = 0; i != 8; i += 4)
1248 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) ||
1249 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) ||
1250 !isConstantOrUndef(N->getMaskElt(i+2), i*2+j+2) ||
1251 !isConstantOrUndef(N->getMaskElt(i+3), i*2+j+3) ||
1252 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) ||
1253 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1) ||
1254 !isConstantOrUndef(N->getMaskElt(i+10), i*2+j+2) ||
1255 !isConstantOrUndef(N->getMaskElt(i+11), i*2+j+3))
1256 return false;
1257 }
1258 return true;
1259}
1260
Chris Lattnerf38e0332006-04-06 22:02:42 +00001261/// isVMerge - Common function, used to match vmrg* shuffles.
1262///
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001263static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +00001264 unsigned LHSStart, unsigned RHSStart) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001265 if (N->getValueType(0) != MVT::v16i8)
1266 return false;
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001267 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
1268 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001269
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001270 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
1271 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001272 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +00001273 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001274 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +00001275 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001276 return false;
1277 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001278 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +00001279}
1280
1281/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +00001282/// a VMRGL* instruction with the specified unit size (1,2 or 4 bytes).
NAKAMURA Takumi84965032015-09-22 11:14:12 +00001283/// The ShuffleKind distinguishes between big-endian merges with two
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001284/// different inputs (0), either-endian merges with two identical inputs (1),
1285/// and little-endian merges with two different inputs (2). For the latter,
1286/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +00001287bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001288 unsigned ShuffleKind, SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001289 if (DAG.getDataLayout().isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001290 if (ShuffleKind == 1) // unary
1291 return isVMerge(N, UnitSize, 0, 0);
1292 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +00001293 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001294 else
1295 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001296 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001297 if (ShuffleKind == 1) // unary
1298 return isVMerge(N, UnitSize, 8, 8);
1299 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +00001300 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001301 else
1302 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001303 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001304}
1305
1306/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +00001307/// a VMRGH* instruction with the specified unit size (1,2 or 4 bytes).
NAKAMURA Takumi84965032015-09-22 11:14:12 +00001308/// The ShuffleKind distinguishes between big-endian merges with two
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001309/// different inputs (0), either-endian merges with two identical inputs (1),
1310/// and little-endian merges with two different inputs (2). For the latter,
1311/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +00001312bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001313 unsigned ShuffleKind, SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001314 if (DAG.getDataLayout().isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001315 if (ShuffleKind == 1) // unary
1316 return isVMerge(N, UnitSize, 8, 8);
1317 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +00001318 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001319 else
1320 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001321 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001322 if (ShuffleKind == 1) // unary
1323 return isVMerge(N, UnitSize, 0, 0);
1324 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +00001325 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001326 else
1327 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001328 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001329}
1330
Kit Barton13894c72015-06-25 15:17:40 +00001331/**
1332 * \brief Common function used to match vmrgew and vmrgow shuffles
1333 *
1334 * The indexOffset determines whether to look for even or odd words in
1335 * the shuffle mask. This is based on the of the endianness of the target
1336 * machine.
1337 * - Little Endian:
1338 * - Use offset of 0 to check for odd elements
1339 * - Use offset of 4 to check for even elements
1340 * - Big Endian:
1341 * - Use offset of 0 to check for even elements
1342 * - Use offset of 4 to check for odd elements
1343 * A detailed description of the vector element ordering for little endian and
NAKAMURA Takumi520b45d2015-06-25 23:38:44 +00001344 * big endian can be found at
1345 * http://www.ibm.com/developerworks/library/l-ibm-xl-c-cpp-compiler/index.html
Kit Barton13894c72015-06-25 15:17:40 +00001346 * Targeting your applications - what little endian and big endian IBM XL C/C++
NAKAMURA Takumi520b45d2015-06-25 23:38:44 +00001347 * compiler differences mean to you
Kit Barton13894c72015-06-25 15:17:40 +00001348 *
1349 * The mask to the shuffle vector instruction specifies the indices of the
1350 * elements from the two input vectors to place in the result. The elements are
1351 * numbered in array-access order, starting with the first vector. These vectors
1352 * are always of type v16i8, thus each vector will contain 16 elements of size
NAKAMURA Takumi520b45d2015-06-25 23:38:44 +00001353 * 8. More info on the shuffle vector can be found in the
1354 * http://llvm.org/docs/LangRef.html#shufflevector-instruction
1355 * Language Reference.
Kit Barton13894c72015-06-25 15:17:40 +00001356 *
1357 * The RHSStartValue indicates whether the same input vectors are used (unary)
1358 * or two different input vectors are used, based on the following:
1359 * - If the instruction uses the same vector for both inputs, the range of the
1360 * indices will be 0 to 15. In this case, the RHSStart value passed should
1361 * be 0.
1362 * - If the instruction has two different vectors then the range of the
1363 * indices will be 0 to 31. In this case, the RHSStart value passed should
1364 * be 16 (indices 0-15 specify elements in the first vector while indices 16
1365 * to 31 specify elements in the second vector).
1366 *
1367 * \param[in] N The shuffle vector SD Node to analyze
1368 * \param[in] IndexOffset Specifies whether to look for even or odd elements
1369 * \param[in] RHSStartValue Specifies the starting index for the righthand input
1370 * vector to the shuffle_vector instruction
1371 * \return true iff this shuffle vector represents an even or odd word merge
1372 */
1373static bool isVMerge(ShuffleVectorSDNode *N, unsigned IndexOffset,
1374 unsigned RHSStartValue) {
1375 if (N->getValueType(0) != MVT::v16i8)
1376 return false;
1377
1378 for (unsigned i = 0; i < 2; ++i)
1379 for (unsigned j = 0; j < 4; ++j)
1380 if (!isConstantOrUndef(N->getMaskElt(i*4+j),
1381 i*RHSStartValue+j+IndexOffset) ||
1382 !isConstantOrUndef(N->getMaskElt(i*4+j+8),
1383 i*RHSStartValue+j+IndexOffset+8))
1384 return false;
1385 return true;
1386}
1387
1388/**
1389 * \brief Determine if the specified shuffle mask is suitable for the vmrgew or
1390 * vmrgow instructions.
1391 *
1392 * \param[in] N The shuffle vector SD Node to analyze
1393 * \param[in] CheckEven Check for an even merge (true) or an odd merge (false)
1394 * \param[in] ShuffleKind Identify the type of merge:
1395 * - 0 = big-endian merge with two different inputs;
1396 * - 1 = either-endian merge with two identical inputs;
1397 * - 2 = little-endian merge with two different inputs (inputs are swapped for
1398 * little-endian merges).
1399 * \param[in] DAG The current SelectionDAG
NAKAMURA Takumi84965032015-09-22 11:14:12 +00001400 * \return true iff this shuffle mask
Kit Barton13894c72015-06-25 15:17:40 +00001401 */
1402bool PPC::isVMRGEOShuffleMask(ShuffleVectorSDNode *N, bool CheckEven,
1403 unsigned ShuffleKind, SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001404 if (DAG.getDataLayout().isLittleEndian()) {
Kit Barton13894c72015-06-25 15:17:40 +00001405 unsigned indexOffset = CheckEven ? 4 : 0;
1406 if (ShuffleKind == 1) // Unary
1407 return isVMerge(N, indexOffset, 0);
1408 else if (ShuffleKind == 2) // swapped
1409 return isVMerge(N, indexOffset, 16);
1410 else
1411 return false;
1412 }
1413 else {
1414 unsigned indexOffset = CheckEven ? 0 : 4;
1415 if (ShuffleKind == 1) // Unary
1416 return isVMerge(N, indexOffset, 0);
1417 else if (ShuffleKind == 0) // Normal
1418 return isVMerge(N, indexOffset, 16);
1419 else
1420 return false;
1421 }
1422 return false;
1423}
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001424
Chris Lattner1d338192006-04-06 18:26:28 +00001425/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
1426/// amount, otherwise return -1.
NAKAMURA Takumi84965032015-09-22 11:14:12 +00001427/// The ShuffleKind distinguishes between big-endian operations with two
Bill Schmidt42a69362014-08-05 20:47:25 +00001428/// different inputs (0), either-endian operations with two identical inputs
1429/// (1), and little-endian operations with two different inputs (2). For the
1430/// latter, the input operands are swapped (see PPCInstrAltivec.td).
1431int PPC::isVSLDOIShuffleMask(SDNode *N, unsigned ShuffleKind,
1432 SelectionDAG &DAG) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001433 if (N->getValueType(0) != MVT::v16i8)
Hal Finkela775e512014-04-08 19:00:27 +00001434 return -1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001435
1436 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +00001437
Chris Lattner1d338192006-04-06 18:26:28 +00001438 // Find the first non-undef value in the shuffle mask.
1439 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001440 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +00001441 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001442
Chris Lattner1d338192006-04-06 18:26:28 +00001443 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001444
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001445 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +00001446 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001447 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +00001448 if (ShiftAmt < i) return -1;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001449
Bill Schmidtf04e9982014-08-04 23:21:01 +00001450 ShiftAmt -= i;
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001451 bool isLE = DAG.getDataLayout().isLittleEndian();
Bill Schmidtf910a062014-06-10 14:35:01 +00001452
Bill Schmidt42a69362014-08-05 20:47:25 +00001453 if ((ShuffleKind == 0 && !isLE) || (ShuffleKind == 2 && isLE)) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001454 // Check the rest of the elements to see if they are consecutive.
1455 for (++i; i != 16; ++i)
1456 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
1457 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001458 } else if (ShuffleKind == 1) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001459 // Check the rest of the elements to see if they are consecutive.
1460 for (++i; i != 16; ++i)
1461 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
1462 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001463 } else
1464 return -1;
1465
Bill Schmidt1e77bb12015-07-15 15:45:30 +00001466 if (isLE)
Bill Schmidt42a69362014-08-05 20:47:25 +00001467 ShiftAmt = 16 - ShiftAmt;
Bill Schmidtf04e9982014-08-04 23:21:01 +00001468
Chris Lattner1d338192006-04-06 18:26:28 +00001469 return ShiftAmt;
1470}
Chris Lattnerffc47562006-03-20 06:33:01 +00001471
1472/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
1473/// specifies a splat of a single element that is suitable for input to
1474/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001475bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +00001476 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +00001477 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001478
Bill Schmidt42ddd712015-07-29 14:31:57 +00001479 // The consecutive indices need to specify an element, not part of two
1480 // different elements. So abandon ship early if this isn't the case.
1481 if (N->getMaskElt(0) % EltSize != 0)
1482 return false;
1483
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001484 // This is a splat operation if each element of the permute is the same, and
1485 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001486 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001487
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001488 // FIXME: Handle UNDEF elements too!
1489 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +00001490 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001491
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001492 // Check that the indices are consecutive, in the case of a multi-byte element
1493 // splatted with a v16i8 mask.
1494 for (unsigned i = 1; i != EltSize; ++i)
1495 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001496 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001497
Chris Lattner95c7adc2006-04-04 17:25:31 +00001498 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001499 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +00001500 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001501 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001502 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001503 }
Chris Lattner95c7adc2006-04-04 17:25:31 +00001504 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +00001505}
1506
Nemanja Ivanovicb43bb612016-07-12 21:00:10 +00001507bool PPC::isXXINSERTWMask(ShuffleVectorSDNode *N, unsigned &ShiftElts,
1508 unsigned &InsertAtByte, bool &Swap, bool IsLE) {
1509
1510 // Check that the mask is shuffling words
1511 for (unsigned i = 0; i < 4; ++i) {
1512 unsigned B0 = N->getMaskElt(i*4);
1513 unsigned B1 = N->getMaskElt(i*4+1);
1514 unsigned B2 = N->getMaskElt(i*4+2);
1515 unsigned B3 = N->getMaskElt(i*4+3);
1516 if (B0 % 4)
1517 return false;
1518 if (B1 != B0+1 || B2 != B1+1 || B3 != B2+1)
1519 return false;
1520 }
1521
1522 // Now we look at mask elements 0,4,8,12
1523 unsigned M0 = N->getMaskElt(0) / 4;
1524 unsigned M1 = N->getMaskElt(4) / 4;
1525 unsigned M2 = N->getMaskElt(8) / 4;
1526 unsigned M3 = N->getMaskElt(12) / 4;
1527 unsigned LittleEndianShifts[] = { 2, 1, 0, 3 };
1528 unsigned BigEndianShifts[] = { 3, 0, 1, 2 };
1529
1530 // Below, let H and L be arbitrary elements of the shuffle mask
1531 // where H is in the range [4,7] and L is in the range [0,3].
1532 // H, 1, 2, 3 or L, 5, 6, 7
1533 if ((M0 > 3 && M1 == 1 && M2 == 2 && M3 == 3) ||
1534 (M0 < 4 && M1 == 5 && M2 == 6 && M3 == 7)) {
1535 ShiftElts = IsLE ? LittleEndianShifts[M0 & 0x3] : BigEndianShifts[M0 & 0x3];
1536 InsertAtByte = IsLE ? 12 : 0;
1537 Swap = M0 < 4;
1538 return true;
1539 }
1540 // 0, H, 2, 3 or 4, L, 6, 7
1541 if ((M1 > 3 && M0 == 0 && M2 == 2 && M3 == 3) ||
1542 (M1 < 4 && M0 == 4 && M2 == 6 && M3 == 7)) {
1543 ShiftElts = IsLE ? LittleEndianShifts[M1 & 0x3] : BigEndianShifts[M1 & 0x3];
1544 InsertAtByte = IsLE ? 8 : 4;
1545 Swap = M1 < 4;
1546 return true;
1547 }
1548 // 0, 1, H, 3 or 4, 5, L, 7
1549 if ((M2 > 3 && M0 == 0 && M1 == 1 && M3 == 3) ||
1550 (M2 < 4 && M0 == 4 && M1 == 5 && M3 == 7)) {
1551 ShiftElts = IsLE ? LittleEndianShifts[M2 & 0x3] : BigEndianShifts[M2 & 0x3];
1552 InsertAtByte = IsLE ? 4 : 8;
1553 Swap = M2 < 4;
1554 return true;
1555 }
1556 // 0, 1, 2, H or 4, 5, 6, L
1557 if ((M3 > 3 && M0 == 0 && M1 == 1 && M2 == 2) ||
1558 (M3 < 4 && M0 == 4 && M1 == 5 && M2 == 6)) {
1559 ShiftElts = IsLE ? LittleEndianShifts[M3 & 0x3] : BigEndianShifts[M3 & 0x3];
1560 InsertAtByte = IsLE ? 0 : 12;
1561 Swap = M3 < 4;
1562 return true;
1563 }
1564
1565 // If both vector operands for the shuffle are the same vector, the mask will
1566 // contain only elements from the first one and the second one will be undef.
1567 if (N->getOperand(1).isUndef()) {
1568 ShiftElts = 0;
1569 Swap = true;
1570 unsigned XXINSERTWSrcElem = IsLE ? 2 : 1;
1571 if (M0 == XXINSERTWSrcElem && M1 == 1 && M2 == 2 && M3 == 3) {
1572 InsertAtByte = IsLE ? 12 : 0;
1573 return true;
1574 }
1575 if (M0 == 0 && M1 == XXINSERTWSrcElem && M2 == 2 && M3 == 3) {
1576 InsertAtByte = IsLE ? 8 : 4;
1577 return true;
1578 }
1579 if (M0 == 0 && M1 == 1 && M2 == XXINSERTWSrcElem && M3 == 3) {
1580 InsertAtByte = IsLE ? 4 : 8;
1581 return true;
1582 }
1583 if (M0 == 0 && M1 == 1 && M2 == 2 && M3 == XXINSERTWSrcElem) {
1584 InsertAtByte = IsLE ? 0 : 12;
1585 return true;
1586 }
1587 }
1588
1589 return false;
1590}
1591
Chris Lattnerffc47562006-03-20 06:33:01 +00001592/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
1593/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Bill Schmidtf910a062014-06-10 14:35:01 +00001594unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize,
1595 SelectionDAG &DAG) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001596 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1597 assert(isSplatShuffleMask(SVOp, EltSize));
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001598 if (DAG.getDataLayout().isLittleEndian())
Bill Schmidtf910a062014-06-10 14:35:01 +00001599 return (16 / EltSize) - 1 - (SVOp->getMaskElt(0) / EltSize);
1600 else
1601 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +00001602}
1603
Chris Lattner74cf9ff2006-04-12 17:37:20 +00001604/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001605/// by using a vspltis[bhw] instruction of the specified element size, return
1606/// the constant being splatted. The ByteSize field indicates the number of
1607/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001608SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001609 SDValue OpVal(nullptr, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001610
1611 // If ByteSize of the splat is bigger than the element size of the
1612 // build_vector, then we have a case where we are checking for a splat where
1613 // multiple elements of the buildvector are folded together into a single
1614 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
1615 unsigned EltSize = 16/N->getNumOperands();
1616 if (EltSize < ByteSize) {
1617 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001618 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001619 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001620
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001621 // See if all of the elements in the buildvector agree across.
1622 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00001623 if (N->getOperand(i).isUndef()) continue;
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001624 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001625 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001626
Scott Michelcf0da6c2009-02-17 22:15:04 +00001627
Craig Topper062a2ba2014-04-25 05:30:21 +00001628 if (!UniquedVals[i&(Multiple-1)].getNode())
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001629 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
1630 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001631 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001632 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001633
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001634 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
1635 // either constant or undef values that are identical for each chunk. See
1636 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001637
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001638 // Check to see if all of the leading entries are either 0 or -1. If
1639 // neither, then this won't fit into the immediate field.
1640 bool LeadingZero = true;
1641 bool LeadingOnes = true;
1642 for (unsigned i = 0; i != Multiple-1; ++i) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001643 if (!UniquedVals[i].getNode()) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001644
Artyom Skrobov314ee042015-11-25 19:41:11 +00001645 LeadingZero &= isNullConstant(UniquedVals[i]);
1646 LeadingOnes &= isAllOnesConstant(UniquedVals[i]);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001647 }
1648 // Finally, check the least significant entry.
1649 if (LeadingZero) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001650 if (!UniquedVals[Multiple-1].getNode())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001651 return DAG.getTargetConstant(0, SDLoc(N), MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00001652 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001653 if (Val < 16) // 0,0,0,4 -> vspltisw(4)
1654 return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001655 }
1656 if (LeadingOnes) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001657 if (!UniquedVals[Multiple-1].getNode())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001658 return DAG.getTargetConstant(~0U, SDLoc(N), MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +00001659 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001660 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001661 return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001662 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001663
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001664 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001665 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001666
Chris Lattner2771e2c2006-03-25 06:12:06 +00001667 // Check to see if this buildvec has a single non-undef value in its elements.
1668 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00001669 if (N->getOperand(i).isUndef()) continue;
Craig Topper062a2ba2014-04-25 05:30:21 +00001670 if (!OpVal.getNode())
Chris Lattner2771e2c2006-03-25 06:12:06 +00001671 OpVal = N->getOperand(i);
1672 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001673 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001674 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001675
Craig Topper062a2ba2014-04-25 05:30:21 +00001676 if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001677
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001678 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001679 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001680 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001681 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001682 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001683 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001684 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001685 }
1686
1687 // If the splat value is larger than the element value, then we can never do
1688 // this splat. The only case that we could fit the replicated bits into our
1689 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001690 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001691
Benjamin Kramerb4b51502015-03-25 16:49:59 +00001692 // If the element value is larger than the splat value, check if it consists
1693 // of a repeated bit pattern of size ByteSize.
1694 if (!APInt(ValSizeInBytes * 8, Value).isSplat(ByteSize * 8))
1695 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001696
1697 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001698 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001699
Evan Chengb1ddc982006-03-26 09:52:32 +00001700 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001701 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001702
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001703 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001704 if (SignExtend32<5>(MaskVal) == MaskVal)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001705 return DAG.getTargetConstant(MaskVal, SDLoc(N), MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001706 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001707}
1708
Hal Finkelc93a9a22015-02-25 01:06:45 +00001709/// isQVALIGNIShuffleMask - If this is a qvaligni shuffle mask, return the shift
1710/// amount, otherwise return -1.
1711int PPC::isQVALIGNIShuffleMask(SDNode *N) {
1712 EVT VT = N->getValueType(0);
1713 if (VT != MVT::v4f64 && VT != MVT::v4f32 && VT != MVT::v4i1)
1714 return -1;
1715
1716 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1717
1718 // Find the first non-undef value in the shuffle mask.
1719 unsigned i;
1720 for (i = 0; i != 4 && SVOp->getMaskElt(i) < 0; ++i)
1721 /*search*/;
1722
1723 if (i == 4) return -1; // all undef.
1724
1725 // Otherwise, check to see if the rest of the elements are consecutively
1726 // numbered from this value.
1727 unsigned ShiftAmt = SVOp->getMaskElt(i);
1728 if (ShiftAmt < i) return -1;
1729 ShiftAmt -= i;
1730
1731 // Check the rest of the elements to see if they are consecutive.
1732 for (++i; i != 4; ++i)
1733 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
1734 return -1;
1735
1736 return ShiftAmt;
1737}
1738
Chris Lattner4211ca92006-04-14 06:01:58 +00001739//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001740// Addressing Mode Selection
1741//===----------------------------------------------------------------------===//
1742
1743/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1744/// or 64-bit immediate, and if the value can be accurately represented as a
1745/// sign extension from a 16-bit value. If so, this returns true and the
1746/// immediate.
1747static bool isIntS16Immediate(SDNode *N, short &Imm) {
Adam Nemet571eb5f2014-05-20 17:20:34 +00001748 if (!isa<ConstantSDNode>(N))
Chris Lattnera801fced2006-11-08 02:15:41 +00001749 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001750
Dan Gohmaneffb8942008-09-12 16:56:44 +00001751 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001752 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001753 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001754 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001755 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001756}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001757static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001758 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001759}
1760
Chris Lattnera801fced2006-11-08 02:15:41 +00001761/// SelectAddressRegReg - Given the specified addressed, check to see if it
1762/// can be represented as an indexed [r+r] operation. Returns false if it
1763/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001764bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1765 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001766 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001767 short imm = 0;
1768 if (N.getOpcode() == ISD::ADD) {
1769 if (isIntS16Immediate(N.getOperand(1), imm))
1770 return false; // r+i
1771 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1772 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001773
Chris Lattnera801fced2006-11-08 02:15:41 +00001774 Base = N.getOperand(0);
1775 Index = N.getOperand(1);
1776 return true;
1777 } else if (N.getOpcode() == ISD::OR) {
1778 if (isIntS16Immediate(N.getOperand(1), imm))
1779 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001780
Chris Lattnera801fced2006-11-08 02:15:41 +00001781 // If this is an or of disjoint bitfields, we can codegen this as an add
1782 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1783 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001784 APInt LHSKnownZero, LHSKnownOne;
1785 APInt RHSKnownZero, RHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001786 DAG.computeKnownBits(N.getOperand(0),
1787 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001788
Dan Gohmanf19609a2008-02-27 01:23:58 +00001789 if (LHSKnownZero.getBoolValue()) {
Jay Foada0653a32014-05-14 21:14:37 +00001790 DAG.computeKnownBits(N.getOperand(1),
1791 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001792 // If all of the bits are known zero on the LHS or RHS, the add won't
1793 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001794 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001795 Base = N.getOperand(0);
1796 Index = N.getOperand(1);
1797 return true;
1798 }
1799 }
1800 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001801
Chris Lattnera801fced2006-11-08 02:15:41 +00001802 return false;
1803}
1804
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001805// If we happen to be doing an i64 load or store into a stack slot that has
1806// less than a 4-byte alignment, then the frame-index elimination may need to
1807// use an indexed load or store instruction (because the offset may not be a
1808// multiple of 4). The extra register needed to hold the offset comes from the
1809// register scavenger, and it is possible that the scavenger will need to use
1810// an emergency spill slot. As a result, we need to make sure that a spill slot
1811// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1812// stack slot.
1813static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1814 // FIXME: This does not handle the LWA case.
1815 if (VT != MVT::i64)
1816 return;
1817
Hal Finkel7ab3db52013-07-10 15:29:01 +00001818 // NOTE: We'll exclude negative FIs here, which come from argument
1819 // lowering, because there are no known test cases triggering this problem
1820 // using packed structures (or similar). We can remove this exclusion if
1821 // we find such a test case. The reason why this is so test-case driven is
1822 // because this entire 'fixup' is only to prevent crashes (from the
1823 // register scavenger) on not-really-valid inputs. For example, if we have:
1824 // %a = alloca i1
1825 // %b = bitcast i1* %a to i64*
1826 // store i64* a, i64 b
1827 // then the store should really be marked as 'align 1', but is not. If it
1828 // were marked as 'align 1' then the indexed form would have been
1829 // instruction-selected initially, and the problem this 'fixup' is preventing
1830 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001831 if (FrameIdx < 0)
1832 return;
1833
1834 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001835 MachineFrameInfo &MFI = MF.getFrameInfo();
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001836
Matthias Braun941a7052016-07-28 18:40:00 +00001837 unsigned Align = MFI.getObjectAlignment(FrameIdx);
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001838 if (Align >= 4)
1839 return;
1840
1841 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1842 FuncInfo->setHasNonRISpills();
1843}
1844
Chris Lattnera801fced2006-11-08 02:15:41 +00001845/// Returns true if the address N can be represented by a base register plus
1846/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001847/// represented as reg+reg. If Aligned is true, only accept displacements
1848/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001849bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001850 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001851 SelectionDAG &DAG,
1852 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001853 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001854 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001855 // If this can be more profitably realized as r+r, fail.
1856 if (SelectAddressRegReg(N, Disp, Base, DAG))
1857 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001858
Chris Lattnera801fced2006-11-08 02:15:41 +00001859 if (N.getOpcode() == ISD::ADD) {
1860 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001861 if (isIntS16Immediate(N.getOperand(1), imm) &&
1862 (!Aligned || (imm & 3) == 0)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001863 Disp = DAG.getTargetConstant(imm, dl, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001864 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1865 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001866 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001867 } else {
1868 Base = N.getOperand(0);
1869 }
1870 return true; // [r+i]
1871 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1872 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001873 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001874 && "Cannot handle constant offsets yet!");
1875 Disp = N.getOperand(1).getOperand(0); // The global address.
1876 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001877 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001878 Disp.getOpcode() == ISD::TargetConstantPool ||
1879 Disp.getOpcode() == ISD::TargetJumpTable);
1880 Base = N.getOperand(0);
1881 return true; // [&g+r]
1882 }
1883 } else if (N.getOpcode() == ISD::OR) {
1884 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001885 if (isIntS16Immediate(N.getOperand(1), imm) &&
1886 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001887 // If this is an or of disjoint bitfields, we can codegen this as an add
1888 // (for better address arithmetic) if the LHS and RHS of the OR are
1889 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001890 APInt LHSKnownZero, LHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001891 DAG.computeKnownBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001892
Dan Gohmanf19609a2008-02-27 01:23:58 +00001893 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001894 // If all of the bits are known zero on the LHS or RHS, the add won't
1895 // carry.
Ulrich Weigand55a96652014-07-20 22:26:40 +00001896 if (FrameIndexSDNode *FI =
1897 dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1898 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1899 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1900 } else {
1901 Base = N.getOperand(0);
1902 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001903 Disp = DAG.getTargetConstant(imm, dl, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001904 return true;
1905 }
1906 }
1907 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1908 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001909
Chris Lattnera801fced2006-11-08 02:15:41 +00001910 // If this address fits entirely in a 16-bit sext immediate field, codegen
1911 // this as "d, 0"
1912 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001913 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001914 Disp = DAG.getTargetConstant(Imm, dl, CN->getValueType(0));
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001915 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001916 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001917 return true;
1918 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001919
1920 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001921 if ((CN->getValueType(0) == MVT::i32 ||
1922 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1923 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001924 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001925
Chris Lattnera801fced2006-11-08 02:15:41 +00001926 // Otherwise, break this down into an LIS + disp.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001927 Disp = DAG.getTargetConstant((short)Addr, dl, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001928
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001929 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, dl,
1930 MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00001931 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001932 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001933 return true;
1934 }
1935 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001936
Mehdi Amini44ede332015-07-09 02:09:04 +00001937 Disp = DAG.getTargetConstant(0, dl, getPointerTy(DAG.getDataLayout()));
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001938 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001939 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001940 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1941 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001942 Base = N;
1943 return true; // [r+0]
1944}
1945
1946/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1947/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001948bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1949 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001950 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001951 // Check to see if we can easily represent this as an [r+r] address. This
1952 // will fail if it thinks that the address is more profitably represented as
1953 // reg+imm, e.g. where imm = 0.
1954 if (SelectAddressRegReg(N, Base, Index, DAG))
1955 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001956
Chris Lattnera801fced2006-11-08 02:15:41 +00001957 // If the operand is an addition, always emit this as [r+r], since this is
1958 // better (for code size, and execution, as the memop does the add for free)
1959 // than emitting an explicit add.
1960 if (N.getOpcode() == ISD::ADD) {
1961 Base = N.getOperand(0);
1962 Index = N.getOperand(1);
1963 return true;
1964 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001965
Chris Lattnera801fced2006-11-08 02:15:41 +00001966 // Otherwise, do it the hard way, using R0 as the base register.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001967 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001968 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001969 Index = N;
1970 return true;
1971}
1972
Chris Lattnera801fced2006-11-08 02:15:41 +00001973/// getPreIndexedAddressParts - returns true by value, base pointer and
1974/// offset pointer and addressing mode by reference if the node's address
1975/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001976bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1977 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001978 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001979 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001980 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001981
Ulrich Weigande90b0222013-03-22 14:58:48 +00001982 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001983 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001984 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001985 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001986 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1987 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001988 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001989 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001990 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001991 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001992 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001993 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001994 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001995 } else
1996 return false;
1997
Hal Finkelc93a9a22015-02-25 01:06:45 +00001998 // PowerPC doesn't have preinc load/store instructions for vectors (except
1999 // for QPX, which does have preinc r+r forms).
2000 if (VT.isVector()) {
2001 if (!Subtarget.hasQPX() || (VT != MVT::v4f64 && VT != MVT::v4f32)) {
2002 return false;
2003 } else if (SelectAddressRegRegOnly(Ptr, Offset, Base, DAG)) {
2004 AM = ISD::PRE_INC;
2005 return true;
2006 }
2007 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002008
Ulrich Weigande90b0222013-03-22 14:58:48 +00002009 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
2010
2011 // Common code will reject creating a pre-inc form if the base pointer
2012 // is a frame index, or if N is a store and the base pointer is either
2013 // the same as or a predecessor of the value being stored. Check for
2014 // those situations here, and try with swapped Base/Offset instead.
2015 bool Swap = false;
2016
2017 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
2018 Swap = true;
2019 else if (!isLoad) {
2020 SDValue Val = cast<StoreSDNode>(N)->getValue();
2021 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
2022 Swap = true;
2023 }
2024
2025 if (Swap)
2026 std::swap(Base, Offset);
2027
Hal Finkelca542be2012-06-20 15:43:03 +00002028 AM = ISD::PRE_INC;
2029 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00002030 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002031
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00002032 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00002033 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00002034 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00002035 return false;
2036 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00002037 // LDU/STU need an address with at least 4-byte alignment.
2038 if (Alignment < 4)
2039 return false;
2040
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00002041 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00002042 return false;
2043 }
Chris Lattnerb314b152006-11-11 00:08:42 +00002044
Chris Lattnerb314b152006-11-11 00:08:42 +00002045 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00002046 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
2047 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00002048 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00002049 LD->getExtensionType() == ISD::SEXTLOAD &&
2050 isa<ConstantSDNode>(Offset))
2051 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002052 }
2053
Chris Lattnerce645542006-11-10 02:08:47 +00002054 AM = ISD::PRE_INC;
2055 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00002056}
2057
2058//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00002059// LowerOperation implementation
2060//===----------------------------------------------------------------------===//
2061
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002062/// Return true if we should reference labels using a PICBase, set the HiOpFlags
2063/// and LoOpFlags to the target MO flags.
2064static void getLabelAccessInfo(bool IsPIC, const PPCSubtarget &Subtarget,
Eric Christophercccae792015-01-30 22:02:31 +00002065 unsigned &HiOpFlags, unsigned &LoOpFlags,
Craig Topper062a2ba2014-04-25 05:30:21 +00002066 const GlobalValue *GV = nullptr) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002067 HiOpFlags = PPCII::MO_HA;
2068 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00002069
Hal Finkel3ee2af72014-07-18 23:29:49 +00002070 // Don't use the pic base if not in PIC relocation model.
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002071 if (IsPIC) {
Chris Lattnerdd6df842010-11-15 03:13:19 +00002072 HiOpFlags |= PPCII::MO_PIC_FLAG;
2073 LoOpFlags |= PPCII::MO_PIC_FLAG;
2074 }
2075
2076 // If this is a reference to a global value that requires a non-lazy-ptr, make
2077 // sure that instruction lowering adds it.
Eric Christophere8dbfe12015-02-13 22:23:04 +00002078 if (GV && Subtarget.hasLazyResolverStub(GV)) {
Chris Lattnerdd6df842010-11-15 03:13:19 +00002079 HiOpFlags |= PPCII::MO_NLP_FLAG;
2080 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00002081
Chris Lattnerdd6df842010-11-15 03:13:19 +00002082 if (GV->hasHiddenVisibility()) {
2083 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
2084 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
2085 }
2086 }
Chris Lattneredb9d842010-11-15 02:46:57 +00002087}
2088
2089static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
2090 SelectionDAG &DAG) {
Daniel Jasper48e93f72015-04-28 13:38:35 +00002091 SDLoc DL(HiPart);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002092 EVT PtrVT = HiPart.getValueType();
2093 SDValue Zero = DAG.getConstant(0, DL, PtrVT);
Chris Lattneredb9d842010-11-15 02:46:57 +00002094
2095 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
2096 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00002097
Chris Lattneredb9d842010-11-15 02:46:57 +00002098 // With PIC, the first instruction is actually "GR+hi(&G)".
2099 if (isPIC)
2100 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
2101 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00002102
Chris Lattneredb9d842010-11-15 02:46:57 +00002103 // Generate non-pic code that has direct accesses to the constant pool.
2104 // The address of the global is just (hi(&g)+lo(&g)).
2105 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
2106}
2107
Hal Finkele6698d52015-02-01 15:03:28 +00002108static void setUsesTOCBasePtr(MachineFunction &MF) {
2109 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2110 FuncInfo->setUsesTOCBasePtr();
2111}
2112
2113static void setUsesTOCBasePtr(SelectionDAG &DAG) {
2114 setUsesTOCBasePtr(DAG.getMachineFunction());
2115}
2116
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002117static SDValue getTOCEntry(SelectionDAG &DAG, const SDLoc &dl, bool Is64Bit,
Hal Finkelcf599212015-02-25 21:36:59 +00002118 SDValue GA) {
2119 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
2120 SDValue Reg = Is64Bit ? DAG.getRegister(PPC::X2, VT) :
2121 DAG.getNode(PPCISD::GlobalBaseReg, dl, VT);
2122
2123 SDValue Ops[] = { GA, Reg };
Alex Lorenze40c8a22015-08-11 23:09:45 +00002124 return DAG.getMemIntrinsicNode(
2125 PPCISD::TOC_ENTRY, dl, DAG.getVTList(VT, MVT::Other), Ops, VT,
2126 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 0, false, true,
2127 false, 0);
Hal Finkelcf599212015-02-25 21:36:59 +00002128}
2129
Scott Michelcf0da6c2009-02-17 22:15:04 +00002130SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002131 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002132 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00002133 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002134 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00002135
Roman Divackyace47072012-08-24 16:26:02 +00002136 // 64-bit SVR4 ABI code is always position-independent.
2137 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002138 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002139 setUsesTOCBasePtr(DAG);
Roman Divackyace47072012-08-24 16:26:02 +00002140 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Hal Finkelcf599212015-02-25 21:36:59 +00002141 return getTOCEntry(DAG, SDLoc(CP), true, GA);
Roman Divackyace47072012-08-24 16:26:02 +00002142 }
2143
Chris Lattneredb9d842010-11-15 02:46:57 +00002144 unsigned MOHiFlag, MOLoFlag;
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002145 bool IsPIC = isPositionIndependent();
2146 getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002147
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002148 if (IsPIC && Subtarget.isSVR4ABI()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +00002149 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(),
2150 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00002151 return getTOCEntry(DAG, SDLoc(CP), false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002152 }
2153
Chris Lattneredb9d842010-11-15 02:46:57 +00002154 SDValue CPIHi =
2155 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
2156 SDValue CPILo =
2157 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002158 return LowerLabelRef(CPIHi, CPILo, IsPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00002159}
2160
Dan Gohman21cea8a2010-04-17 15:26:15 +00002161SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002162 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002163 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00002164
Roman Divackyace47072012-08-24 16:26:02 +00002165 // 64-bit SVR4 ABI code is always position-independent.
2166 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002167 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002168 setUsesTOCBasePtr(DAG);
Roman Divackyace47072012-08-24 16:26:02 +00002169 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Hal Finkelcf599212015-02-25 21:36:59 +00002170 return getTOCEntry(DAG, SDLoc(JT), true, GA);
Roman Divackyace47072012-08-24 16:26:02 +00002171 }
2172
Chris Lattneredb9d842010-11-15 02:46:57 +00002173 unsigned MOHiFlag, MOLoFlag;
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002174 bool IsPIC = isPositionIndependent();
2175 getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002176
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002177 if (IsPIC && Subtarget.isSVR4ABI()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +00002178 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
2179 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00002180 return getTOCEntry(DAG, SDLoc(GA), false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002181 }
2182
Chris Lattneredb9d842010-11-15 02:46:57 +00002183 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
2184 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002185 return LowerLabelRef(JTIHi, JTILo, IsPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00002186}
2187
Dan Gohman21cea8a2010-04-17 15:26:15 +00002188SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
2189 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00002190 EVT PtrVT = Op.getValueType();
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002191 BlockAddressSDNode *BASDN = cast<BlockAddressSDNode>(Op);
2192 const BlockAddress *BA = BASDN->getBlockAddress();
Bob Wilsonf84f7102009-11-04 21:31:18 +00002193
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002194 // 64-bit SVR4 ABI code is always position-independent.
2195 // The actual BlockAddress is stored in the TOC.
2196 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002197 setUsesTOCBasePtr(DAG);
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002198 SDValue GA = DAG.getTargetBlockAddress(BA, PtrVT, BASDN->getOffset());
Hal Finkelcf599212015-02-25 21:36:59 +00002199 return getTOCEntry(DAG, SDLoc(BASDN), true, GA);
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002200 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002201
Chris Lattneredb9d842010-11-15 02:46:57 +00002202 unsigned MOHiFlag, MOLoFlag;
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002203 bool IsPIC = isPositionIndependent();
2204 getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00002205 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
2206 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002207 return LowerLabelRef(TgtBAHi, TgtBALo, IsPIC, DAG);
Chris Lattneredb9d842010-11-15 02:46:57 +00002208}
2209
Roman Divackye3f15c982012-06-04 17:36:38 +00002210SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
2211 SelectionDAG &DAG) const {
2212
Bill Schmidtbdae03f2013-09-17 20:22:05 +00002213 // FIXME: TLS addresses currently use medium model code sequences,
2214 // which is the most useful form. Eventually support for small and
2215 // large models could be added if users need it, at the cost of
2216 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00002217 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002218 if (DAG.getTarget().Options.EmulatedTLS)
2219 return LowerToTLSEmulatedModel(GA, DAG);
2220
Andrew Trickef9de2a2013-05-25 02:42:55 +00002221 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00002222 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002223 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002224 bool is64bit = Subtarget.isPPC64();
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002225 const Module *M = DAG.getMachineFunction().getFunction()->getParent();
2226 PICLevel::Level picLevel = M->getPICLevel();
Roman Divackye3f15c982012-06-04 17:36:38 +00002227
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002228 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00002229
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002230 if (Model == TLSModel::LocalExec) {
2231 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002232 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002233 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002234 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002235 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
2236 is64bit ? MVT::i64 : MVT::i32);
2237 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
2238 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
2239 }
Roman Divackye3f15c982012-06-04 17:36:38 +00002240
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002241 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00002242 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00002243 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2244 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00002245 SDValue GOTPtr;
2246 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00002247 setUsesTOCBasePtr(DAG);
Roman Divacky32143e22013-12-20 18:08:54 +00002248 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
2249 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
2250 PtrVT, GOTReg, TGA);
2251 } else
2252 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00002253 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00002254 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00002255 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002256 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002257
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002258 if (Model == TLSModel::GeneralDynamic) {
Bill Schmidt82f1c772015-02-10 19:09:05 +00002259 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002260 SDValue GOTPtr;
2261 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00002262 setUsesTOCBasePtr(DAG);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002263 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
2264 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
2265 GOTReg, TGA);
2266 } else {
Davide Italiano4cccc482016-06-17 18:07:14 +00002267 if (picLevel == PICLevel::SmallPIC)
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002268 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
2269 else
2270 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002271 }
Bill Schmidt82f1c772015-02-10 19:09:05 +00002272 return DAG.getNode(PPCISD::ADDI_TLSGD_L_ADDR, dl, PtrVT,
2273 GOTPtr, TGA, TGA);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002274 }
2275
Bill Schmidt24b8dd62012-12-12 19:29:35 +00002276 if (Model == TLSModel::LocalDynamic) {
Bill Schmidt82f1c772015-02-10 19:09:05 +00002277 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002278 SDValue GOTPtr;
2279 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00002280 setUsesTOCBasePtr(DAG);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002281 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
2282 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
2283 GOTReg, TGA);
2284 } else {
Davide Italiano4cccc482016-06-17 18:07:14 +00002285 if (picLevel == PICLevel::SmallPIC)
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002286 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
2287 else
2288 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002289 }
Bill Schmidt82f1c772015-02-10 19:09:05 +00002290 SDValue TLSAddr = DAG.getNode(PPCISD::ADDI_TLSLD_L_ADDR, dl,
2291 PtrVT, GOTPtr, TGA, TGA);
2292 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl,
2293 PtrVT, TLSAddr, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00002294 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
2295 }
2296
2297 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00002298}
2299
Chris Lattneredb9d842010-11-15 02:46:57 +00002300SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
2301 SelectionDAG &DAG) const {
2302 EVT PtrVT = Op.getValueType();
2303 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002304 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00002305 const GlobalValue *GV = GSDN->getGlobal();
2306
Chris Lattneredb9d842010-11-15 02:46:57 +00002307 // 64-bit SVR4 ABI code is always position-independent.
2308 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002309 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002310 setUsesTOCBasePtr(DAG);
Chris Lattneredb9d842010-11-15 02:46:57 +00002311 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
Hal Finkelcf599212015-02-25 21:36:59 +00002312 return getTOCEntry(DAG, DL, true, GA);
Chris Lattneredb9d842010-11-15 02:46:57 +00002313 }
2314
Chris Lattnerdd6df842010-11-15 03:13:19 +00002315 unsigned MOHiFlag, MOLoFlag;
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002316 bool IsPIC = isPositionIndependent();
2317 getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00002318
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002319 if (IsPIC && Subtarget.isSVR4ABI()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +00002320 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT,
2321 GSDN->getOffset(),
2322 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00002323 return getTOCEntry(DAG, DL, false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002324 }
2325
Chris Lattnerdd6df842010-11-15 03:13:19 +00002326 SDValue GAHi =
2327 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
2328 SDValue GALo =
2329 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00002330
Rafael Espindolae1d255f2016-06-27 12:56:02 +00002331 SDValue Ptr = LowerLabelRef(GAHi, GALo, IsPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00002332
Chris Lattnerdd6df842010-11-15 03:13:19 +00002333 // If the global reference is actually to a non-lazy-pointer, we have to do an
2334 // extra load to get the address of the global.
2335 if (MOHiFlag & PPCII::MO_NLP_FLAG)
Justin Lebar9c375812016-07-15 18:27:10 +00002336 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
Chris Lattnerdd6df842010-11-15 03:13:19 +00002337 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00002338}
2339
Dan Gohman21cea8a2010-04-17 15:26:15 +00002340SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00002341 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002342 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002343
Hal Finkel777c9dd2014-03-29 16:04:40 +00002344 if (Op.getValueType() == MVT::v2i64) {
2345 // When the operands themselves are v2i64 values, we need to do something
2346 // special because VSX has no underlying comparison operations for these.
2347 if (Op.getOperand(0).getValueType() == MVT::v2i64) {
2348 // Equality can be handled by casting to the legal type for Altivec
2349 // comparisons, everything else needs to be expanded.
2350 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
2351 return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
2352 DAG.getSetCC(dl, MVT::v4i32,
2353 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)),
2354 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)),
2355 CC));
2356 }
2357
2358 return SDValue();
2359 }
2360
2361 // We handle most of these in the usual way.
2362 return Op;
2363 }
2364
Chris Lattner4211ca92006-04-14 06:01:58 +00002365 // If we're comparing for equality to zero, expose the fact that this is
Sanjay Patel9cc21ac2016-07-06 16:42:46 +00002366 // implemented as a ctlz/srl pair on ppc, so that the dag combiner can
Chris Lattner4211ca92006-04-14 06:01:58 +00002367 // fold the new nodes.
Pierre Gousseau051db7d2016-08-16 13:53:53 +00002368 if (SDValue V = lowerCmpEqZeroToCtlzSrl(Op, DAG))
2369 return V;
2370
Chris Lattner4211ca92006-04-14 06:01:58 +00002371 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00002372 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00002373 // optimized. FIXME: revisit this when we can custom lower all setcc
2374 // optimizations.
2375 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002376 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00002377 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002378
Chris Lattner4211ca92006-04-14 06:01:58 +00002379 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00002380 // by xor'ing the rhs with the lhs, which is faster than setting a
2381 // condition register, reading it back out, and masking the correct bit. The
2382 // normal approach here uses sub to do this instead of xor. Using xor exposes
2383 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002384 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002385 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002386 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002387 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00002388 Op.getOperand(1));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002389 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, dl, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00002390 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002391 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00002392}
2393
Eric Christopherb976a392016-07-07 00:39:27 +00002394SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00002395 SDNode *Node = Op.getNode();
2396 EVT VT = Node->getValueType(0);
Eric Christophercd719462016-07-07 01:49:59 +00002397 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Roman Divacky4394e682011-06-28 15:30:42 +00002398 SDValue InChain = Node->getOperand(0);
2399 SDValue VAListPtr = Node->getOperand(1);
2400 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002401 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002402
Roman Divacky4394e682011-06-28 15:30:42 +00002403 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
2404
2405 // gpr_index
2406 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
Justin Lebar9c375812016-07-15 18:27:10 +00002407 VAListPtr, MachinePointerInfo(SV), MVT::i8);
Roman Divacky4394e682011-06-28 15:30:42 +00002408 InChain = GprIndex.getValue(1);
2409
2410 if (VT == MVT::i64) {
2411 // Check if GprIndex is even
2412 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002413 DAG.getConstant(1, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002414 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002415 DAG.getConstant(0, dl, MVT::i32), ISD::SETNE);
Roman Divacky4394e682011-06-28 15:30:42 +00002416 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002417 DAG.getConstant(1, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002418 // Align GprIndex to be even if it isn't
2419 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
2420 GprIndex);
2421 }
2422
2423 // fpr index is 1 byte after gpr
2424 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002425 DAG.getConstant(1, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002426
2427 // fpr
2428 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
Justin Lebar9c375812016-07-15 18:27:10 +00002429 FprPtr, MachinePointerInfo(SV), MVT::i8);
Roman Divacky4394e682011-06-28 15:30:42 +00002430 InChain = FprIndex.getValue(1);
2431
2432 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002433 DAG.getConstant(8, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002434
2435 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002436 DAG.getConstant(4, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002437
2438 // areas
Justin Lebar9c375812016-07-15 18:27:10 +00002439 SDValue OverflowArea =
2440 DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr, MachinePointerInfo());
Roman Divacky4394e682011-06-28 15:30:42 +00002441 InChain = OverflowArea.getValue(1);
2442
Justin Lebar9c375812016-07-15 18:27:10 +00002443 SDValue RegSaveArea =
2444 DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr, MachinePointerInfo());
Roman Divacky4394e682011-06-28 15:30:42 +00002445 InChain = RegSaveArea.getValue(1);
2446
2447 // select overflow_area if index > 8
2448 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002449 DAG.getConstant(8, dl, MVT::i32), ISD::SETLT);
Roman Divacky4394e682011-06-28 15:30:42 +00002450
Roman Divacky4394e682011-06-28 15:30:42 +00002451 // adjustment constant gpr_index * 4/8
2452 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
2453 VT.isInteger() ? GprIndex : FprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002454 DAG.getConstant(VT.isInteger() ? 4 : 8, dl,
Roman Divacky4394e682011-06-28 15:30:42 +00002455 MVT::i32));
2456
2457 // OurReg = RegSaveArea + RegConstant
2458 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
2459 RegConstant);
2460
2461 // Floating types are 32 bytes into RegSaveArea
2462 if (VT.isFloatingPoint())
2463 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002464 DAG.getConstant(32, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002465
2466 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
2467 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
2468 VT.isInteger() ? GprIndex : FprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002469 DAG.getConstant(VT == MVT::i64 ? 2 : 1, dl,
Roman Divacky4394e682011-06-28 15:30:42 +00002470 MVT::i32));
2471
2472 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
2473 VT.isInteger() ? VAListPtr : FprPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00002474 MachinePointerInfo(SV), MVT::i8);
Roman Divacky4394e682011-06-28 15:30:42 +00002475
2476 // determine if we should load from reg_save_area or overflow_area
2477 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
2478
2479 // increase overflow_area by 4/8 if gpr/fpr > 8
2480 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
2481 DAG.getConstant(VT.isInteger() ? 4 : 8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002482 dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002483
2484 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
2485 OverflowAreaPlusN);
2486
Justin Lebar9c375812016-07-15 18:27:10 +00002487 InChain = DAG.getTruncStore(InChain, dl, OverflowArea, OverflowAreaPtr,
2488 MachinePointerInfo(), MVT::i32);
Roman Divacky4394e682011-06-28 15:30:42 +00002489
Justin Lebar9c375812016-07-15 18:27:10 +00002490 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo());
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002491}
2492
Eric Christopherb976a392016-07-07 00:39:27 +00002493SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Roman Divackyc3825df2013-07-25 21:36:47 +00002494 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
2495
2496 // We have to copy the entire va_list struct:
2497 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
2498 return DAG.getMemcpy(Op.getOperand(0), Op,
2499 Op.getOperand(1), Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002500 DAG.getConstant(12, SDLoc(Op), MVT::i32), 8, false, true,
2501 false, MachinePointerInfo(), MachinePointerInfo());
Roman Divackyc3825df2013-07-25 21:36:47 +00002502}
2503
Duncan Sandsa0984362011-09-06 13:37:06 +00002504SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
2505 SelectionDAG &DAG) const {
2506 return Op.getOperand(0);
2507}
2508
2509SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
2510 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00002511 SDValue Chain = Op.getOperand(0);
2512 SDValue Trmp = Op.getOperand(1); // trampoline
2513 SDValue FPtr = Op.getOperand(2); // nested function
2514 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00002515 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00002516
Eric Christophercd719462016-07-07 01:49:59 +00002517 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Owen Anderson9f944592009-08-11 20:47:22 +00002518 bool isPPC64 = (PtrVT == MVT::i64);
Mehdi Aminia749f2a2015-07-09 02:09:52 +00002519 Type *IntPtrTy = DAG.getDataLayout().getIntPtrType(*DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00002520
Scott Michelcf0da6c2009-02-17 22:15:04 +00002521 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00002522 TargetLowering::ArgListEntry Entry;
2523
2524 Entry.Ty = IntPtrTy;
2525 Entry.Node = Trmp; Args.push_back(Entry);
2526
2527 // TrampSize == (isPPC64 ? 48 : 40);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002528 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002529 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00002530 Args.push_back(Entry);
2531
2532 Entry.Node = FPtr; Args.push_back(Entry);
2533 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002534
Bill Wendling95e1af22008-09-17 00:30:57 +00002535 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002536 TargetLowering::CallLoweringInfo CLI(DAG);
2537 CLI.setDebugLoc(dl).setChain(Chain)
2538 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002539 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002540 std::move(Args));
Bill Wendling95e1af22008-09-17 00:30:57 +00002541
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002542 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Duncan Sandsa0984362011-09-06 13:37:06 +00002543 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00002544}
2545
Eric Christopherb976a392016-07-07 00:39:27 +00002546SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00002547 MachineFunction &MF = DAG.getMachineFunction();
2548 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Eric Christophercd719462016-07-07 01:49:59 +00002549 EVT PtrVT = getPointerTy(MF.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002550
Andrew Trickef9de2a2013-05-25 02:42:55 +00002551 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002552
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002553 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002554 // vastart just stores the address of the VarArgsFrameIndex slot into the
2555 // memory location argument.
Dan Gohman31ae5862010-04-17 14:41:14 +00002556 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002557 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00002558 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
Justin Lebar9c375812016-07-15 18:27:10 +00002559 MachinePointerInfo(SV));
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002560 }
2561
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002562 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002563 // We suppose the given va_list is already allocated.
2564 //
2565 // typedef struct {
2566 // char gpr; /* index into the array of 8 GPRs
2567 // * stored in the register save area
2568 // * gpr=0 corresponds to r3,
2569 // * gpr=1 to r4, etc.
2570 // */
2571 // char fpr; /* index into the array of 8 FPRs
2572 // * stored in the register save area
2573 // * fpr=0 corresponds to f1,
2574 // * fpr=1 to f2, etc.
2575 // */
2576 // char *overflow_arg_area;
2577 // /* location on stack that holds
2578 // * the next overflow argument
2579 // */
2580 // char *reg_save_area;
2581 // /* where r3:r10 and f1:f8 (if saved)
2582 // * are stored
2583 // */
2584 // } va_list[1];
2585
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002586 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), dl, MVT::i32);
2587 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), dl, MVT::i32);
Dan Gohman31ae5862010-04-17 14:41:14 +00002588 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
2589 PtrVT);
2590 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2591 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002592
Duncan Sands13237ac2008-06-06 12:08:01 +00002593 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002594 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, dl, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002595
Duncan Sands13237ac2008-06-06 12:08:01 +00002596 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002597 SDValue ConstStackOffset = DAG.getConstant(StackOffset, dl, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002598
2599 uint64_t FPROffset = 1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002600 SDValue ConstFPROffset = DAG.getConstant(FPROffset, dl, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002601
Dan Gohman2d489b52008-02-06 22:27:42 +00002602 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002603
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002604 // Store first byte : number of int regs
Justin Lebar9c375812016-07-15 18:27:10 +00002605 SDValue firstStore =
2606 DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR, Op.getOperand(1),
2607 MachinePointerInfo(SV), MVT::i8);
Dan Gohman2d489b52008-02-06 22:27:42 +00002608 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002609 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002610 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002611
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002612 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002613 SDValue secondStore =
Justin Lebar9c375812016-07-15 18:27:10 +00002614 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
2615 MachinePointerInfo(SV, nextOffset), MVT::i8);
Dan Gohman2d489b52008-02-06 22:27:42 +00002616 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002617 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002618
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002619 // Store second word : arguments given on stack
Justin Lebar9c375812016-07-15 18:27:10 +00002620 SDValue thirdStore = DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
2621 MachinePointerInfo(SV, nextOffset));
Dan Gohman2d489b52008-02-06 22:27:42 +00002622 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002623 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002624
2625 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00002626 return DAG.getStore(thirdStore, dl, FR, nextPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00002627 MachinePointerInfo(SV, nextOffset));
Chris Lattner4211ca92006-04-14 06:01:58 +00002628}
2629
Chris Lattner4f2e4e02007-03-06 00:59:59 +00002630#include "PPCGenCallingConv.inc"
2631
NAKAMURA Takumi84965032015-09-22 11:14:12 +00002632// Function whose sole purpose is to kill compiler warnings
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002633// stemming from unused functions included from PPCGenCallingConv.inc.
2634CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00002635 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002636}
2637
Bill Schmidt230b4512013-06-12 16:39:22 +00002638bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
2639 CCValAssign::LocInfo &LocInfo,
2640 ISD::ArgFlagsTy &ArgFlags,
2641 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002642 return true;
2643}
2644
Bill Schmidt230b4512013-06-12 16:39:22 +00002645bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
2646 MVT &LocVT,
2647 CCValAssign::LocInfo &LocInfo,
2648 ISD::ArgFlagsTy &ArgFlags,
2649 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002650 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002651 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2652 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2653 };
2654 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002655
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002656 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002657
2658 // Skip one register if the first unallocated register has an even register
2659 // number and there are still argument registers available which have not been
2660 // allocated yet. RegNum is actually an index into ArgRegs, which means we
2661 // need to skip a register if RegNum is odd.
2662 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
2663 State.AllocateReg(ArgRegs[RegNum]);
2664 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002665
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002666 // Always return false here, as this function only makes sure that the first
2667 // unallocated register has an odd register number and does not actually
2668 // allocate a register for the current argument.
2669 return false;
2670}
2671
Strahinja Petrovic30e0ce82016-08-05 08:47:26 +00002672bool
2673llvm::CC_PPC32_SVR4_Custom_SkipLastArgRegsPPCF128(unsigned &ValNo, MVT &ValVT,
2674 MVT &LocVT,
2675 CCValAssign::LocInfo &LocInfo,
2676 ISD::ArgFlagsTy &ArgFlags,
2677 CCState &State) {
2678 static const MCPhysReg ArgRegs[] = {
2679 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2680 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2681 };
2682 const unsigned NumArgRegs = array_lengthof(ArgRegs);
2683
2684 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
2685 int RegsLeft = NumArgRegs - RegNum;
2686
2687 // Skip if there is not enough registers left for long double type (4 gpr regs
2688 // in soft float mode) and put long double argument on the stack.
2689 if (RegNum != NumArgRegs && RegsLeft < 4) {
2690 for (int i = 0; i < RegsLeft; i++) {
2691 State.AllocateReg(ArgRegs[RegNum + i]);
2692 }
2693 }
2694
2695 return false;
2696}
2697
Bill Schmidt230b4512013-06-12 16:39:22 +00002698bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
2699 MVT &LocVT,
2700 CCValAssign::LocInfo &LocInfo,
2701 ISD::ArgFlagsTy &ArgFlags,
2702 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002703 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002704 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2705 PPC::F8
2706 };
2707
2708 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002709
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002710 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002711
2712 // If there is only one Floating-point register left we need to put both f64
2713 // values of a split ppc_fp128 value on the stack.
2714 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
2715 State.AllocateReg(ArgRegs[RegNum]);
2716 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002717
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002718 // Always return false here, as this function only makes sure that the two f64
2719 // values a ppc_fp128 value is split into are both passed in registers or both
2720 // passed on the stack and does not actually allocate a register for the
2721 // current argument.
2722 return false;
2723}
2724
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002725/// FPR - The set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002726/// on Darwin.
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002727static const MCPhysReg FPR[] = {PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5,
2728 PPC::F6, PPC::F7, PPC::F8, PPC::F9, PPC::F10,
2729 PPC::F11, PPC::F12, PPC::F13};
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002730
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002731/// QFPR - The set of QPX registers that should be allocated for arguments.
2732static const MCPhysReg QFPR[] = {
2733 PPC::QF1, PPC::QF2, PPC::QF3, PPC::QF4, PPC::QF5, PPC::QF6, PPC::QF7,
2734 PPC::QF8, PPC::QF9, PPC::QF10, PPC::QF11, PPC::QF12, PPC::QF13};
Hal Finkelc93a9a22015-02-25 01:06:45 +00002735
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002736/// CalculateStackSlotSize - Calculates the size reserved for this argument on
2737/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002738static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002739 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00002740 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002741 if (Flags.isByVal())
2742 ArgSize = Flags.getByValSize();
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002743
2744 // Round up to multiples of the pointer size, except for array members,
2745 // which are always packed.
2746 if (!Flags.isInConsecutiveRegs())
2747 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002748
2749 return ArgSize;
2750}
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002751
2752/// CalculateStackSlotAlignment - Calculates the alignment of this argument
2753/// on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002754static unsigned CalculateStackSlotAlignment(EVT ArgVT, EVT OrigVT,
2755 ISD::ArgFlagsTy Flags,
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002756 unsigned PtrByteSize) {
2757 unsigned Align = PtrByteSize;
2758
2759 // Altivec parameters are padded to a 16 byte boundary.
2760 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2761 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
Kit Bartond4eb73c2015-05-05 16:10:44 +00002762 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 ||
2763 ArgVT == MVT::v1i128)
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002764 Align = 16;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002765 // QPX vector types stored in double-precision are padded to a 32 byte
2766 // boundary.
2767 else if (ArgVT == MVT::v4f64 || ArgVT == MVT::v4i1)
2768 Align = 32;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002769
2770 // ByVal parameters are aligned as requested.
2771 if (Flags.isByVal()) {
2772 unsigned BVAlign = Flags.getByValAlign();
2773 if (BVAlign > PtrByteSize) {
2774 if (BVAlign % PtrByteSize != 0)
2775 llvm_unreachable(
2776 "ByVal alignment is not a multiple of the pointer size");
2777
2778 Align = BVAlign;
2779 }
2780 }
2781
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002782 // Array members are always packed to their original alignment.
2783 if (Flags.isInConsecutiveRegs()) {
2784 // If the array member was split into multiple registers, the first
2785 // needs to be aligned to the size of the full type. (Except for
2786 // ppcf128, which is only aligned as its f64 components.)
2787 if (Flags.isSplit() && OrigVT != MVT::ppcf128)
2788 Align = OrigVT.getStoreSize();
2789 else
2790 Align = ArgVT.getStoreSize();
2791 }
2792
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002793 return Align;
2794}
2795
Ulrich Weigand8658f172014-07-20 23:43:15 +00002796/// CalculateStackSlotUsed - Return whether this argument will use its
2797/// stack slot (instead of being passed in registers). ArgOffset,
2798/// AvailableFPRs, and AvailableVRs must hold the current argument
2799/// position, and will be updated to account for this argument.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002800static bool CalculateStackSlotUsed(EVT ArgVT, EVT OrigVT,
2801 ISD::ArgFlagsTy Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00002802 unsigned PtrByteSize,
2803 unsigned LinkageSize,
2804 unsigned ParamAreaSize,
2805 unsigned &ArgOffset,
2806 unsigned &AvailableFPRs,
Hal Finkelc93a9a22015-02-25 01:06:45 +00002807 unsigned &AvailableVRs, bool HasQPX) {
Ulrich Weigand8658f172014-07-20 23:43:15 +00002808 bool UseMemory = false;
2809
2810 // Respect alignment of argument on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002811 unsigned Align =
2812 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigand8658f172014-07-20 23:43:15 +00002813 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
2814 // If there's no space left in the argument save area, we must
2815 // use memory (this check also catches zero-sized arguments).
2816 if (ArgOffset >= LinkageSize + ParamAreaSize)
2817 UseMemory = true;
2818
2819 // Allocate argument on the stack.
2820 ArgOffset += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002821 if (Flags.isInConsecutiveRegsLast())
2822 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand8658f172014-07-20 23:43:15 +00002823 // If we overran the argument save area, we must use memory
2824 // (this check catches arguments passed partially in memory)
2825 if (ArgOffset > LinkageSize + ParamAreaSize)
2826 UseMemory = true;
2827
2828 // However, if the argument is actually passed in an FPR or a VR,
2829 // we don't use memory after all.
2830 if (!Flags.isByVal()) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00002831 if (ArgVT == MVT::f32 || ArgVT == MVT::f64 ||
2832 // QPX registers overlap with the scalar FP registers.
2833 (HasQPX && (ArgVT == MVT::v4f32 ||
2834 ArgVT == MVT::v4f64 ||
2835 ArgVT == MVT::v4i1)))
Ulrich Weigand8658f172014-07-20 23:43:15 +00002836 if (AvailableFPRs > 0) {
2837 --AvailableFPRs;
2838 return false;
2839 }
2840 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2841 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
Kit Bartond4eb73c2015-05-05 16:10:44 +00002842 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 ||
2843 ArgVT == MVT::v1i128)
Ulrich Weigand8658f172014-07-20 23:43:15 +00002844 if (AvailableVRs > 0) {
2845 --AvailableVRs;
2846 return false;
2847 }
2848 }
2849
2850 return UseMemory;
2851}
2852
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002853/// EnsureStackAlignment - Round stack frame size up from NumBytes to
2854/// ensure minimum alignment required for target.
Eric Christophercccae792015-01-30 22:02:31 +00002855static unsigned EnsureStackAlignment(const PPCFrameLowering *Lowering,
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002856 unsigned NumBytes) {
Eric Christophercccae792015-01-30 22:02:31 +00002857 unsigned TargetAlign = Lowering->getStackAlignment();
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002858 unsigned AlignMask = TargetAlign - 1;
2859 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2860 return NumBytes;
2861}
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002862
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002863SDValue PPCTargetLowering::LowerFormalArguments(
2864 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
2865 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
2866 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002867 if (Subtarget.isSVR4ABI()) {
2868 if (Subtarget.isPPC64())
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002869 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
2870 dl, DAG, InVals);
2871 else
2872 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2873 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002874 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002875 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2876 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002877 }
2878}
2879
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002880SDValue PPCTargetLowering::LowerFormalArguments_32SVR4(
2881 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
2882 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
2883 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002884
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002885 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002886 // +-----------------------------------+
2887 // +--> | Back chain |
2888 // | +-----------------------------------+
2889 // | | Floating-point register save area |
2890 // | +-----------------------------------+
2891 // | | General register save area |
2892 // | +-----------------------------------+
2893 // | | CR save word |
2894 // | +-----------------------------------+
2895 // | | VRSAVE save word |
2896 // | +-----------------------------------+
2897 // | | Alignment padding |
2898 // | +-----------------------------------+
2899 // | | Vector register save area |
2900 // | +-----------------------------------+
2901 // | | Local variable space |
2902 // | +-----------------------------------+
2903 // | | Parameter list area |
2904 // | +-----------------------------------+
2905 // | | LR save word |
2906 // | +-----------------------------------+
2907 // SP--> +--- | Back chain |
2908 // +-----------------------------------+
2909 //
2910 // Specifications:
2911 // System V Application Binary Interface PowerPC Processor Supplement
2912 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002913
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002914 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002915 MachineFrameInfo &MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002916 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002917
Eric Christophercd719462016-07-07 01:49:59 +00002918 EVT PtrVT = getPointerTy(MF.getDataLayout());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002919 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002920 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2921 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002922 unsigned PtrByteSize = 4;
2923
2924 // Assign locations to all of the incoming arguments.
2925 SmallVector<CCValAssign, 16> ArgLocs;
Strahinja Petrovice682b802016-05-09 12:27:39 +00002926 PPCCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
Eric Christopherb5217502014-08-06 18:45:26 +00002927 *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002928
2929 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00002930 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002931 CCInfo.AllocateStack(LinkageSize, PtrByteSize);
Eric Christopher317df662016-07-07 01:49:57 +00002932 if (useSoftFloat())
Strahinja Petrovice682b802016-05-09 12:27:39 +00002933 CCInfo.PreAnalyzeFormalArguments(Ins);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002934
Bill Schmidtef17c142013-02-06 17:33:58 +00002935 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Strahinja Petrovice682b802016-05-09 12:27:39 +00002936 CCInfo.clearWasPPCF128();
Wesley Peck527da1b2010-11-23 03:31:01 +00002937
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002938 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2939 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002940
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002941 // Arguments stored in registers.
2942 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002943 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002944 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002945
Owen Anderson9f944592009-08-11 20:47:22 +00002946 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002947 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002948 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002949 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002950 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002951 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002952 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002953 case MVT::f32:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00002954 if (Subtarget.hasP8Vector())
2955 RC = &PPC::VSSRCRegClass;
2956 else
2957 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002958 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002959 case MVT::f64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002960 if (Subtarget.hasVSX())
Hal Finkel19be5062014-03-29 05:29:01 +00002961 RC = &PPC::VSFRCRegClass;
2962 else
2963 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002964 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002965 case MVT::v16i8:
2966 case MVT::v8i16:
2967 case MVT::v4i32:
Hal Finkel7811c612014-03-28 19:58:11 +00002968 RC = &PPC::VRRCRegClass;
2969 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002970 case MVT::v4f32:
2971 RC = Subtarget.hasQPX() ? &PPC::QSRCRegClass : &PPC::VRRCRegClass;
2972 break;
Hal Finkel27774d92014-03-13 07:58:58 +00002973 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002974 case MVT::v2i64:
Hal Finkel7811c612014-03-28 19:58:11 +00002975 RC = &PPC::VSHRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002976 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002977 case MVT::v4f64:
2978 RC = &PPC::QFRCRegClass;
2979 break;
2980 case MVT::v4i1:
2981 RC = &PPC::QBRCRegClass;
2982 break;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002983 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002984
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002985 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002986 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002987 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2988 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2989
2990 if (ValVT == MVT::i1)
2991 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002992
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002993 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002994 } else {
2995 // Argument stored in memory.
2996 assert(VA.isMemLoc());
2997
Hal Finkel940ab932014-02-28 00:27:01 +00002998 unsigned ArgSize = VA.getLocVT().getStoreSize();
Matthias Braun941a7052016-07-28 18:40:00 +00002999 int FI = MFI.CreateFixedObject(ArgSize, VA.getLocMemOffset(),
3000 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003001
3002 // Create load nodes to retrieve arguments from the stack.
3003 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00003004 InVals.push_back(
3005 DAG.getLoad(VA.getValVT(), dl, Chain, FIN, MachinePointerInfo()));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003006 }
3007 }
3008
3009 // Assign locations to all of the incoming aggregate by value arguments.
3010 // Aggregates passed by value are stored in the local variable space of the
3011 // caller's stack frame, right above the parameter list area.
3012 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003013 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Eric Christopherb5217502014-08-06 18:45:26 +00003014 ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003015
3016 // Reserve stack space for the allocations in CCInfo.
3017 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3018
Bill Schmidtef17c142013-02-06 17:33:58 +00003019 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003020
3021 // Area that is at least reserved in the caller of this function.
3022 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003023 MinReservedArea = std::max(MinReservedArea, LinkageSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003024
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003025 // Set the size that is at least reserved in caller of this function. Tail
3026 // call optimized function's reserved stack space needs to be aligned so that
3027 // taking the difference between two stack areas will result in an aligned
3028 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00003029 MinReservedArea =
3030 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003031 FuncInfo->setMinReservedArea(MinReservedArea);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003032
3033 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00003034
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003035 // If the function takes variable number of arguments, make a frame index for
3036 // the start of the first vararg value... for expansion of llvm.va_start.
3037 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +00003038 static const MCPhysReg GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003039 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
3040 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
3041 };
3042 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
3043
Craig Topper840beec2014-04-04 05:16:06 +00003044 static const MCPhysReg FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003045 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
3046 PPC::F8
3047 };
Joerg Sonnenbergereb8655a2014-08-08 16:46:10 +00003048 unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
Petar Jovanovic280f7102015-12-14 17:57:33 +00003049
Eric Christopher317df662016-07-07 01:49:57 +00003050 if (useSoftFloat())
Petar Jovanovic280f7102015-12-14 17:57:33 +00003051 NumFPArgRegs = 0;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003052
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003053 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs));
3054 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003055
3056 // Make room for NumGPArgRegs and NumFPArgRegs.
3057 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Craig Topper7ff15922014-09-10 04:51:36 +00003058 NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003059
Dan Gohman31ae5862010-04-17 14:41:14 +00003060 FuncInfo->setVarArgsStackOffset(
Matthias Braun941a7052016-07-28 18:40:00 +00003061 MFI.CreateFixedObject(PtrVT.getSizeInBits()/8,
3062 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003063
Matthias Braun941a7052016-07-28 18:40:00 +00003064 FuncInfo->setVarArgsFrameIndex(MFI.CreateStackObject(Depth, 8, false));
Dan Gohman31ae5862010-04-17 14:41:14 +00003065 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003066
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00003067 // The fixed integer arguments of a variadic function are stored to the
Nick Lewycky99800752016-06-28 01:45:05 +00003068 // VarArgsFrameIndex on the stack so that they may be loaded by
3069 // dereferencing the result of va_next.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00003070 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
3071 // Get an existing live-in vreg, or add a new one.
3072 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
3073 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00003074 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003075
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003076 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00003077 SDValue Store =
3078 DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003079 MemOps.push_back(Store);
3080 // Increment the address by four for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003081 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003082 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
3083 }
3084
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003085 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
3086 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003087 // The double arguments are stored to the VarArgsFrameIndex
3088 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00003089 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
3090 // Get an existing live-in vreg, or add a new one.
3091 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
3092 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00003093 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003094
Owen Anderson9f944592009-08-11 20:47:22 +00003095 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Justin Lebar9c375812016-07-15 18:27:10 +00003096 SDValue Store =
3097 DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003098 MemOps.push_back(Store);
3099 // Increment the address by eight for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003100 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8, dl,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003101 PtrVT);
3102 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
3103 }
3104 }
3105
3106 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003107 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003108
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003109 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003110}
3111
Bill Schmidt57d6de52012-10-23 15:51:16 +00003112// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
3113// value to MVT::i64 and then truncate to the correct register size.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003114SDValue PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags,
3115 EVT ObjectVT, SelectionDAG &DAG,
3116 SDValue ArgVal,
3117 const SDLoc &dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003118 if (Flags.isSExt())
3119 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
3120 DAG.getValueType(ObjectVT));
3121 else if (Flags.isZExt())
3122 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
3123 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00003124
Hal Finkel940ab932014-02-28 00:27:01 +00003125 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003126}
3127
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003128SDValue PPCTargetLowering::LowerFormalArguments_64SVR4(
3129 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
3130 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
3131 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003132 // TODO: add description of PPC stack frame format, or at least some docs.
3133 //
Ulrich Weigand8658f172014-07-20 23:43:15 +00003134 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00003135 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003136 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003137 MachineFrameInfo &MFI = MF.getFrameInfo();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003138 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
3139
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003140 assert(!(CallConv == CallingConv::Fast && isVarArg) &&
3141 "fastcc not supported on varargs functions");
3142
Eric Christophercd719462016-07-07 01:49:59 +00003143 EVT PtrVT = getPointerTy(MF.getDataLayout());
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003144 // Potential tail calls could cause overwriting of argument stack slots.
3145 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
3146 (CallConv == CallingConv::Fast));
3147 unsigned PtrByteSize = 8;
Eric Christophera4ae2132015-02-13 22:22:57 +00003148 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003149
Craig Topper840beec2014-04-04 05:16:06 +00003150 static const MCPhysReg GPR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003151 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3152 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3153 };
Craig Topper840beec2014-04-04 05:16:06 +00003154 static const MCPhysReg VR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003155 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3156 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3157 };
Craig Topper840beec2014-04-04 05:16:06 +00003158 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00003159 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
3160 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
3161 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003162
3163 const unsigned Num_GPR_Regs = array_lengthof(GPR);
3164 const unsigned Num_FPR_Regs = 13;
3165 const unsigned Num_VR_Regs = array_lengthof(VR);
Hal Finkelc93a9a22015-02-25 01:06:45 +00003166 const unsigned Num_QFPR_Regs = Num_FPR_Regs;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003167
Ulrich Weigand8658f172014-07-20 23:43:15 +00003168 // Do a first pass over the arguments to determine whether the ABI
3169 // guarantees that our caller has allocated the parameter save area
3170 // on its stack frame. In the ELFv1 ABI, this is always the case;
3171 // in the ELFv2 ABI, it is true if this is a vararg function or if
3172 // any parameter is located in a stack slot.
3173
3174 bool HasParameterArea = !isELFv2ABI || isVarArg;
3175 unsigned ParamAreaSize = Num_GPR_Regs * PtrByteSize;
3176 unsigned NumBytes = LinkageSize;
3177 unsigned AvailableFPRs = Num_FPR_Regs;
3178 unsigned AvailableVRs = Num_VR_Regs;
Hal Finkel965cea52015-07-12 00:37:44 +00003179 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
3180 if (Ins[i].Flags.isNest())
3181 continue;
3182
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003183 if (CalculateStackSlotUsed(Ins[i].VT, Ins[i].ArgVT, Ins[i].Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00003184 PtrByteSize, LinkageSize, ParamAreaSize,
Hal Finkelc93a9a22015-02-25 01:06:45 +00003185 NumBytes, AvailableFPRs, AvailableVRs,
3186 Subtarget.hasQPX()))
Ulrich Weigand8658f172014-07-20 23:43:15 +00003187 HasParameterArea = true;
Hal Finkel965cea52015-07-12 00:37:44 +00003188 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003189
3190 // Add DAG nodes to load the arguments or copy them out of registers. On
3191 // entry to a function on PPC, the arguments start after the linkage area,
3192 // although the first ones are often in registers.
3193
Ulrich Weigand8658f172014-07-20 23:43:15 +00003194 unsigned ArgOffset = LinkageSize;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003195 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Hal Finkelc93a9a22015-02-25 01:06:45 +00003196 unsigned &QFPR_idx = FPR_idx;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003197 SmallVector<SDValue, 8> MemOps;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003198 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00003199 unsigned CurArgIdx = 0;
3200 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003201 SDValue ArgVal;
3202 bool needsLoad = false;
3203 EVT ObjectVT = Ins[ArgNo].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003204 EVT OrigVT = Ins[ArgNo].ArgVT;
Hal Finkel940ab932014-02-28 00:27:01 +00003205 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003206 unsigned ArgSize = ObjSize;
3207 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Andrew Trick05938a52015-02-16 18:10:47 +00003208 if (Ins[ArgNo].isOrigArg()) {
3209 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx);
3210 CurArgIdx = Ins[ArgNo].getOrigArgIndex();
3211 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003212 // We re-align the argument offset for each argument, except when using the
3213 // fast calling convention, when we need to make sure we do that only when
3214 // we'll actually use a stack slot.
3215 unsigned CurArgOffset, Align;
3216 auto ComputeArgOffset = [&]() {
3217 /* Respect alignment of argument on the stack. */
3218 Align = CalculateStackSlotAlignment(ObjectVT, OrigVT, Flags, PtrByteSize);
3219 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
3220 CurArgOffset = ArgOffset;
3221 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003222
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003223 if (CallConv != CallingConv::Fast) {
3224 ComputeArgOffset();
3225
3226 /* Compute GPR index associated with argument offset. */
3227 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
3228 GPR_idx = std::min(GPR_idx, Num_GPR_Regs);
3229 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003230
3231 // FIXME the codegen can be much improved in some cases.
3232 // We do not have to keep everything in memory.
3233 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003234 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit");
3235
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003236 if (CallConv == CallingConv::Fast)
3237 ComputeArgOffset();
3238
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003239 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
3240 ObjSize = Flags.getByValSize();
3241 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00003242 // Empty aggregate parameters do not take up registers. Examples:
3243 // struct { } a;
3244 // union { } b;
3245 // int c[0];
3246 // etc. However, we have to provide a place-holder in InVals, so
3247 // pretend we have an 8-byte item at the current address for that
3248 // purpose.
3249 if (!ObjSize) {
Matthias Braun941a7052016-07-28 18:40:00 +00003250 int FI = MFI.CreateFixedObject(PtrByteSize, ArgOffset, true);
Bill Schmidt9953cf22012-10-31 01:15:05 +00003251 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
3252 InVals.push_back(FIN);
3253 continue;
3254 }
Hal Finkel262a2242013-09-12 23:20:06 +00003255
Ulrich Weigand24195972014-07-20 22:36:52 +00003256 // Create a stack object covering all stack doublewords occupied
Ulrich Weigand8658f172014-07-20 23:43:15 +00003257 // by the argument. If the argument is (fully or partially) on
3258 // the stack, or if the argument is fully in registers but the
3259 // caller has allocated the parameter save anyway, we can refer
3260 // directly to the caller's stack frame. Otherwise, create a
3261 // local copy in our own frame.
3262 int FI;
3263 if (HasParameterArea ||
3264 ArgSize + ArgOffset > LinkageSize + Num_GPR_Regs * PtrByteSize)
Matthias Braun941a7052016-07-28 18:40:00 +00003265 FI = MFI.CreateFixedObject(ArgSize, ArgOffset, false, true);
Ulrich Weigand8658f172014-07-20 23:43:15 +00003266 else
Matthias Braun941a7052016-07-28 18:40:00 +00003267 FI = MFI.CreateStackObject(ArgSize, Align, false);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003268 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003269
Ulrich Weigand24195972014-07-20 22:36:52 +00003270 // Handle aggregates smaller than 8 bytes.
3271 if (ObjSize < PtrByteSize) {
3272 // The value of the object is its address, which differs from the
3273 // address of the enclosing doubleword on big-endian systems.
3274 SDValue Arg = FIN;
3275 if (!isLittleEndian) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003276 SDValue ArgOff = DAG.getConstant(PtrByteSize - ObjSize, dl, PtrVT);
Ulrich Weigand24195972014-07-20 22:36:52 +00003277 Arg = DAG.getNode(ISD::ADD, dl, ArgOff.getValueType(), Arg, ArgOff);
3278 }
3279 InVals.push_back(Arg);
3280
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003281 if (GPR_idx != Num_GPR_Regs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003282 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003283 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003284 SDValue Store;
3285
3286 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
3287 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
3288 (ObjSize == 2 ? MVT::i16 : MVT::i32));
Ulrich Weigand24195972014-07-20 22:36:52 +00003289 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, Arg,
Justin Lebar9c375812016-07-15 18:27:10 +00003290 MachinePointerInfo(&*FuncArg), ObjType);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003291 } else {
3292 // For sizes that don't fit a truncating store (3, 5, 6, 7),
3293 // store the whole register as-is to the parameter save area
Ulrich Weigand24195972014-07-20 22:36:52 +00003294 // slot.
Justin Lebar9c375812016-07-15 18:27:10 +00003295 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3296 MachinePointerInfo(&*FuncArg));
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003297 }
3298
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003299 MemOps.push_back(Store);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003300 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003301 // Whether we copied from a register or not, advance the offset
3302 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003303 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003304 continue;
3305 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003306
Ulrich Weigand24195972014-07-20 22:36:52 +00003307 // The value of the object is its address, which is the address of
3308 // its first stack doubleword.
3309 InVals.push_back(FIN);
3310
3311 // Store whatever pieces of the object are in registers to memory.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003312 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
Ulrich Weigand24195972014-07-20 22:36:52 +00003313 if (GPR_idx == Num_GPR_Regs)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003314 break;
Ulrich Weigand24195972014-07-20 22:36:52 +00003315
3316 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3317 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
3318 SDValue Addr = FIN;
3319 if (j) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003320 SDValue Off = DAG.getConstant(j, dl, PtrVT);
Ulrich Weigand24195972014-07-20 22:36:52 +00003321 Addr = DAG.getNode(ISD::ADD, dl, Off.getValueType(), Addr, Off);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003322 }
Justin Lebar9c375812016-07-15 18:27:10 +00003323 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, Addr,
3324 MachinePointerInfo(&*FuncArg, j));
Ulrich Weigand24195972014-07-20 22:36:52 +00003325 MemOps.push_back(Store);
3326 ++GPR_idx;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003327 }
Ulrich Weigand24195972014-07-20 22:36:52 +00003328 ArgOffset += ArgSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003329 continue;
3330 }
3331
3332 switch (ObjectVT.getSimpleVT().SimpleTy) {
3333 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00003334 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003335 case MVT::i32:
3336 case MVT::i64:
Hal Finkel965cea52015-07-12 00:37:44 +00003337 if (Flags.isNest()) {
3338 // The 'nest' parameter, if any, is passed in R11.
3339 unsigned VReg = MF.addLiveIn(PPC::X11, &PPC::G8RCRegClass);
3340 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
3341
3342 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
3343 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
3344
3345 break;
3346 }
3347
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003348 // These can be scalar arguments or elements of an integer array type
3349 // passed directly. Clang may use those instead of "byval" aggregate
3350 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003351 if (GPR_idx != Num_GPR_Regs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003352 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003353 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
3354
Hal Finkel940ab932014-02-28 00:27:01 +00003355 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003356 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
3357 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003358 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003359 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003360 if (CallConv == CallingConv::Fast)
3361 ComputeArgOffset();
3362
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003363 needsLoad = true;
3364 ArgSize = PtrByteSize;
3365 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003366 if (CallConv != CallingConv::Fast || needsLoad)
3367 ArgOffset += 8;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003368 break;
3369
3370 case MVT::f32:
3371 case MVT::f64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003372 // These can be scalar arguments or elements of a float array type
3373 // passed directly. The latter are used to implement ELFv2 homogenous
3374 // float aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003375 if (FPR_idx != Num_FPR_Regs) {
3376 unsigned VReg;
3377
3378 if (ObjectVT == MVT::f32)
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00003379 VReg = MF.addLiveIn(FPR[FPR_idx],
3380 Subtarget.hasP8Vector()
3381 ? &PPC::VSSRCRegClass
3382 : &PPC::F4RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003383 else
Eric Christophercccae792015-01-30 22:02:31 +00003384 VReg = MF.addLiveIn(FPR[FPR_idx], Subtarget.hasVSX()
3385 ? &PPC::VSFRCRegClass
3386 : &PPC::F8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003387
3388 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
3389 ++FPR_idx;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003390 } else if (GPR_idx != Num_GPR_Regs && CallConv != CallingConv::Fast) {
Hal Finkel8ea446b2015-01-18 14:31:10 +00003391 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8
3392 // once we support fp <-> gpr moves.
3393
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003394 // This can only ever happen in the presence of f32 array types,
3395 // since otherwise we never run out of FPRs before running out
3396 // of GPRs.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003397 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003398 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
3399
3400 if (ObjectVT == MVT::f32) {
3401 if ((ArgOffset % PtrByteSize) == (isLittleEndian ? 4 : 0))
3402 ArgVal = DAG.getNode(ISD::SRL, dl, MVT::i64, ArgVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003403 DAG.getConstant(32, dl, MVT::i32));
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003404 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
3405 }
3406
3407 ArgVal = DAG.getNode(ISD::BITCAST, dl, ObjectVT, ArgVal);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003408 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003409 if (CallConv == CallingConv::Fast)
3410 ComputeArgOffset();
3411
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003412 needsLoad = true;
3413 }
3414
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003415 // When passing an array of floats, the array occupies consecutive
3416 // space in the argument area; only round up to the next doubleword
3417 // at the end of the array. Otherwise, each float takes 8 bytes.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003418 if (CallConv != CallingConv::Fast || needsLoad) {
3419 ArgSize = Flags.isInConsecutiveRegs() ? ObjSize : PtrByteSize;
3420 ArgOffset += ArgSize;
3421 if (Flags.isInConsecutiveRegsLast())
3422 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
3423 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003424 break;
3425 case MVT::v4f32:
3426 case MVT::v4i32:
3427 case MVT::v8i16:
3428 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00003429 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00003430 case MVT::v2i64:
Kit Bartond4eb73c2015-05-05 16:10:44 +00003431 case MVT::v1i128:
Hal Finkelc93a9a22015-02-25 01:06:45 +00003432 if (!Subtarget.hasQPX()) {
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003433 // These can be scalar arguments or elements of a vector array type
3434 // passed directly. The latter are used to implement ELFv2 homogenous
3435 // vector aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003436 if (VR_idx != Num_VR_Regs) {
Hal Finkel7811c612014-03-28 19:58:11 +00003437 unsigned VReg = (ObjectVT == MVT::v2f64 || ObjectVT == MVT::v2i64) ?
3438 MF.addLiveIn(VSRH[VR_idx], &PPC::VSHRCRegClass) :
3439 MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003440 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003441 ++VR_idx;
3442 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003443 if (CallConv == CallingConv::Fast)
3444 ComputeArgOffset();
3445
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003446 needsLoad = true;
3447 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003448 if (CallConv != CallingConv::Fast || needsLoad)
3449 ArgOffset += 16;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003450 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00003451 } // not QPX
3452
3453 assert(ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 &&
3454 "Invalid QPX parameter type");
3455 /* fall through */
3456
3457 case MVT::v4f64:
3458 case MVT::v4i1:
3459 // QPX vectors are treated like their scalar floating-point subregisters
3460 // (except that they're larger).
3461 unsigned Sz = ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 ? 16 : 32;
3462 if (QFPR_idx != Num_QFPR_Regs) {
3463 const TargetRegisterClass *RC;
3464 switch (ObjectVT.getSimpleVT().SimpleTy) {
3465 case MVT::v4f64: RC = &PPC::QFRCRegClass; break;
3466 case MVT::v4f32: RC = &PPC::QSRCRegClass; break;
3467 default: RC = &PPC::QBRCRegClass; break;
3468 }
3469
3470 unsigned VReg = MF.addLiveIn(QFPR[QFPR_idx], RC);
3471 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
3472 ++QFPR_idx;
3473 } else {
3474 if (CallConv == CallingConv::Fast)
3475 ComputeArgOffset();
3476 needsLoad = true;
3477 }
3478 if (CallConv != CallingConv::Fast || needsLoad)
3479 ArgOffset += Sz;
3480 break;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003481 }
3482
3483 // We need to load the argument to a virtual register if we determined
3484 // above that we ran out of physical registers of the appropriate type.
3485 if (needsLoad) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00003486 if (ObjSize < ArgSize && !isLittleEndian)
3487 CurArgOffset += ArgSize - ObjSize;
Matthias Braun941a7052016-07-28 18:40:00 +00003488 int FI = MFI.CreateFixedObject(ObjSize, CurArgOffset, isImmutable);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003489 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00003490 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo());
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003491 }
3492
3493 InVals.push_back(ArgVal);
3494 }
3495
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003496 // Area that is at least reserved in the caller of this function.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00003497 unsigned MinReservedArea;
Ulrich Weigand8658f172014-07-20 23:43:15 +00003498 if (HasParameterArea)
3499 MinReservedArea = std::max(ArgOffset, LinkageSize + 8 * PtrByteSize);
3500 else
3501 MinReservedArea = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003502
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003503 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003504 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003505 // taking the difference between two stack areas will result in an aligned
3506 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00003507 MinReservedArea =
3508 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003509 FuncInfo->setMinReservedArea(MinReservedArea);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003510
3511 // If the function takes variable number of arguments, make a frame index for
3512 // the start of the first vararg value... for expansion of llvm.va_start.
3513 if (isVarArg) {
3514 int Depth = ArgOffset;
3515
3516 FuncInfo->setVarArgsFrameIndex(
Matthias Braun941a7052016-07-28 18:40:00 +00003517 MFI.CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003518 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
3519
3520 // If this function is vararg, store any remaining integer argument regs
Nick Lewycky99800752016-06-28 01:45:05 +00003521 // to their spots on the stack so that they may be loaded by dereferencing
3522 // the result of va_next.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00003523 for (GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
3524 GPR_idx < Num_GPR_Regs; ++GPR_idx) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003525 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3526 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00003527 SDValue Store =
3528 DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo());
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003529 MemOps.push_back(Store);
3530 // Increment the address by four for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003531 SDValue PtrOff = DAG.getConstant(PtrByteSize, dl, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003532 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
3533 }
3534 }
3535
3536 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003537 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003538
3539 return Chain;
3540}
3541
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003542SDValue PPCTargetLowering::LowerFormalArguments_Darwin(
3543 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
3544 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
3545 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003546 // TODO: add description of PPC stack frame format, or at least some docs.
3547 //
3548 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003549 MachineFrameInfo &MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00003550 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003551
Eric Christophercd719462016-07-07 01:49:59 +00003552 EVT PtrVT = getPointerTy(MF.getDataLayout());
Owen Anderson9f944592009-08-11 20:47:22 +00003553 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003554 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003555 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
3556 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00003557 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Eric Christophera4ae2132015-02-13 22:22:57 +00003558 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003559 unsigned ArgOffset = LinkageSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003560 // Area that is at least reserved in caller of this function.
3561 unsigned MinReservedArea = ArgOffset;
3562
Craig Topper840beec2014-04-04 05:16:06 +00003563 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003564 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
3565 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
3566 };
Craig Topper840beec2014-04-04 05:16:06 +00003567 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00003568 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3569 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3570 };
Craig Topper840beec2014-04-04 05:16:06 +00003571 static const MCPhysReg VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003572 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3573 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3574 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00003575
Owen Andersone2f23a32007-09-07 04:06:50 +00003576 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003577 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00003578 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00003579
3580 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003581
Craig Topper840beec2014-04-04 05:16:06 +00003582 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003583
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003584 // In 32-bit non-varargs functions, the stack space for vectors is after the
3585 // stack space for non-vectors. We do not use this space unless we have
3586 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00003587 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003588 // that out...for the pathological case, compute VecArgOffset as the
3589 // start of the vector parameter area. Computing VecArgOffset is the
3590 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003591 unsigned VecArgOffset = ArgOffset;
3592 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003593 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003594 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003595 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003596 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003597
Duncan Sandsd97eea32008-03-21 09:14:45 +00003598 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003599 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00003600 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003601 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003602 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
3603 VecArgOffset += ArgSize;
3604 continue;
3605 }
3606
Owen Anderson9f944592009-08-11 20:47:22 +00003607 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003608 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003609 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003610 case MVT::i32:
3611 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003612 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003613 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003614 case MVT::i64: // PPC64
3615 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003616 // FIXME: We are guaranteed to be !isPPC64 at this point.
3617 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003618 VecArgOffset += 8;
3619 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003620 case MVT::v4f32:
3621 case MVT::v4i32:
3622 case MVT::v8i16:
3623 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003624 // Nothing to do, we're only looking at Nonvector args here.
3625 break;
3626 }
3627 }
3628 }
3629 // We've found where the vector parameter area in memory is. Skip the
3630 // first 12 parameters; these don't use that memory.
3631 VecArgOffset = ((VecArgOffset+15)/16)*16;
3632 VecArgOffset += 12*16;
3633
Chris Lattner4302e8f2006-05-16 18:18:50 +00003634 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00003635 // entry to a function on PPC, the arguments start after the linkage area,
3636 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00003637
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003638 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003639 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00003640 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00003641 unsigned CurArgIdx = 0;
3642 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003643 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003644 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00003645 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00003646 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00003647 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003648 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Andrew Trick05938a52015-02-16 18:10:47 +00003649 if (Ins[ArgNo].isOrigArg()) {
3650 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx);
3651 CurArgIdx = Ins[ArgNo].getOrigArgIndex();
3652 }
Chris Lattner318f0d22006-05-16 18:51:52 +00003653 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003654
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003655 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00003656 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
3657 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003658 if (isVarArg || isPPC64) {
3659 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003660 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003661 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003662 PtrByteSize);
3663 } else nAltivecParamsAtEnd++;
3664 } else
3665 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003666 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003667 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003668 PtrByteSize);
3669
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003670 // FIXME the codegen can be much improved in some cases.
3671 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003672 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003673 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit");
3674
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003675 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003676 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003677 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003678 // Objects of size 1 and 2 are right justified, everything else is
3679 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00003680 if (ObjSize==1 || ObjSize==2) {
3681 CurArgOffset = CurArgOffset + (4 - ObjSize);
3682 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003683 // The value of the object is its address.
Matthias Braun941a7052016-07-28 18:40:00 +00003684 int FI = MFI.CreateFixedObject(ObjSize, CurArgOffset, false, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003685 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003686 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003687 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00003688 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003689 unsigned VReg;
3690 if (isPPC64)
3691 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3692 else
3693 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003694 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003695 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Justin Lebar9c375812016-07-15 18:27:10 +00003696 SDValue Store =
3697 DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
3698 MachinePointerInfo(&*FuncArg), ObjType);
Dale Johannesen21a8f142008-03-08 01:41:42 +00003699 MemOps.push_back(Store);
3700 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00003701 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003702
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003703 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00003704
Dale Johannesen21a8f142008-03-08 01:41:42 +00003705 continue;
3706 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003707 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
3708 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003709 // to memory. ArgOffset will be the address of the beginning
3710 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003711 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003712 unsigned VReg;
3713 if (isPPC64)
3714 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3715 else
3716 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Matthias Braun941a7052016-07-28 18:40:00 +00003717 int FI = MFI.CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003718 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003719 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00003720 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3721 MachinePointerInfo(&*FuncArg, j));
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003722 MemOps.push_back(Store);
3723 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003724 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003725 } else {
3726 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
3727 break;
3728 }
3729 }
3730 continue;
3731 }
3732
Owen Anderson9f944592009-08-11 20:47:22 +00003733 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003734 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003735 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003736 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00003737 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00003738 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003739 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003740 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Hal Finkel7f908e82014-03-06 00:45:19 +00003741
3742 if (ObjectVT == MVT::i1)
3743 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal);
3744
Bill Wendling968f32c2008-03-07 20:49:02 +00003745 ++GPR_idx;
3746 } else {
3747 needsLoad = true;
3748 ArgSize = PtrByteSize;
3749 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003750 // All int arguments reserve stack space in the Darwin ABI.
3751 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00003752 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003753 }
Justin Bognerb03fd122016-08-17 05:10:15 +00003754 LLVM_FALLTHROUGH;
Owen Anderson9f944592009-08-11 20:47:22 +00003755 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00003756 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003757 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003758 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00003759
Hal Finkel940ab932014-02-28 00:27:01 +00003760 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00003761 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00003762 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003763 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00003764
Chris Lattnerec78cad2006-06-26 22:48:35 +00003765 ++GPR_idx;
3766 } else {
3767 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00003768 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003769 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003770 // All int arguments reserve stack space in the Darwin ABI.
3771 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003772 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003773
Owen Anderson9f944592009-08-11 20:47:22 +00003774 case MVT::f32:
3775 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00003776 // Every 4 bytes of argument space consumes one of the GPRs available for
3777 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003778 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003779 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00003780 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003781 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00003782 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003783 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003784 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003785
Owen Anderson9f944592009-08-11 20:47:22 +00003786 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00003787 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003788 else
Devang Patelf3292b22011-02-21 23:21:26 +00003789 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003790
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003791 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003792 ++FPR_idx;
3793 } else {
3794 needsLoad = true;
3795 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003796
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003797 // All FP arguments reserve stack space in the Darwin ABI.
3798 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003799 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003800 case MVT::v4f32:
3801 case MVT::v4i32:
3802 case MVT::v8i16:
3803 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00003804 // Note that vector arguments in registers don't reserve stack space,
3805 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003806 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003807 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003808 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00003809 if (isVarArg) {
3810 while ((ArgOffset % 16) != 0) {
3811 ArgOffset += PtrByteSize;
3812 if (GPR_idx != Num_GPR_Regs)
3813 GPR_idx++;
3814 }
3815 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003816 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00003817 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003818 ++VR_idx;
3819 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003820 if (!isVarArg && !isPPC64) {
3821 // Vectors go after all the nonvectors.
3822 CurArgOffset = VecArgOffset;
3823 VecArgOffset += 16;
3824 } else {
3825 // Vectors are aligned.
3826 ArgOffset = ((ArgOffset+15)/16)*16;
3827 CurArgOffset = ArgOffset;
3828 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00003829 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003830 needsLoad = true;
3831 }
3832 break;
3833 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003834
Chris Lattner4302e8f2006-05-16 18:18:50 +00003835 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003836 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003837 if (needsLoad) {
Matthias Braun941a7052016-07-28 18:40:00 +00003838 int FI = MFI.CreateFixedObject(ObjSize,
3839 CurArgOffset + (ArgSize - ObjSize),
3840 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003841 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00003842 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo());
Chris Lattner4302e8f2006-05-16 18:18:50 +00003843 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003844
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003845 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003846 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003847
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003848 // Allow for Altivec parameters at the end, if needed.
3849 if (nAltivecParamsAtEnd) {
3850 MinReservedArea = ((MinReservedArea+15)/16)*16;
3851 MinReservedArea += 16*nAltivecParamsAtEnd;
3852 }
3853
3854 // Area that is at least reserved in the caller of this function.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003855 MinReservedArea = std::max(MinReservedArea, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003856
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003857 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003858 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003859 // taking the difference between two stack areas will result in an aligned
3860 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00003861 MinReservedArea =
3862 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003863 FuncInfo->setMinReservedArea(MinReservedArea);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003864
Chris Lattner4302e8f2006-05-16 18:18:50 +00003865 // If the function takes variable number of arguments, make a frame index for
3866 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003867 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003868 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003869
Dan Gohman31ae5862010-04-17 14:41:14 +00003870 FuncInfo->setVarArgsFrameIndex(
Matthias Braun941a7052016-07-28 18:40:00 +00003871 MFI.CreateFixedObject(PtrVT.getSizeInBits()/8,
3872 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00003873 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003874
Chris Lattner4302e8f2006-05-16 18:18:50 +00003875 // If this function is vararg, store any remaining integer argument regs
Nick Lewycky99800752016-06-28 01:45:05 +00003876 // to their spots on the stack so that they may be loaded by dereferencing
3877 // the result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003878 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00003879 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00003880
Chris Lattner2cca3852006-11-18 01:57:19 +00003881 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00003882 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003883 else
Devang Patelf3292b22011-02-21 23:21:26 +00003884 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003885
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003886 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00003887 SDValue Store =
3888 DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo());
Chris Lattner4302e8f2006-05-16 18:18:50 +00003889 MemOps.push_back(Store);
3890 // Increment the address by four for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003891 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00003892 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003893 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003894 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003895
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003896 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003897 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003898
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003899 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003900}
3901
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003902/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003903/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00003904static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003905 unsigned ParamSize) {
3906
Dale Johannesen86dcae12009-11-24 01:09:07 +00003907 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003908
3909 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
3910 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3911 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3912 // Remember only if the new adjustement is bigger.
3913 if (SPDiff < FI->getTailCallSPDelta())
3914 FI->setTailCallSPDelta(SPDiff);
3915
3916 return SPDiff;
3917}
3918
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00003919static bool isFunctionGlobalAddress(SDValue Callee);
3920
3921static bool
3922resideInSameModule(SDValue Callee, Reloc::Model RelMod) {
3923 // If !G, Callee can be an external symbol.
3924 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
3925 if (!G) return false;
3926
3927 const GlobalValue *GV = G->getGlobal();
3928
3929 if (GV->isDeclaration()) return false;
3930
3931 switch(GV->getLinkage()) {
3932 default: llvm_unreachable("unknow linkage type");
3933 case GlobalValue::AvailableExternallyLinkage:
3934 case GlobalValue::ExternalWeakLinkage:
3935 return false;
3936
3937 // Callee with weak linkage is allowed if it has hidden or protected
3938 // visibility
3939 case GlobalValue::LinkOnceAnyLinkage:
3940 case GlobalValue::LinkOnceODRLinkage: // e.g. c++ inline functions
3941 case GlobalValue::WeakAnyLinkage:
3942 case GlobalValue::WeakODRLinkage: // e.g. c++ template instantiation
3943 if (GV->hasDefaultVisibility())
3944 return false;
3945
3946 case GlobalValue::ExternalLinkage:
3947 case GlobalValue::InternalLinkage:
3948 case GlobalValue::PrivateLinkage:
3949 break;
3950 }
3951
3952 // With '-fPIC', calling default visiblity function need insert 'nop' after
3953 // function call, no matter that function resides in same module or not, so
3954 // we treat it as in different module.
3955 if (RelMod == Reloc::PIC_ && GV->hasDefaultVisibility())
3956 return false;
3957
3958 return true;
3959}
3960
3961static bool
3962needStackSlotPassParameters(const PPCSubtarget &Subtarget,
3963 const SmallVectorImpl<ISD::OutputArg> &Outs) {
3964 assert(Subtarget.isSVR4ABI() && Subtarget.isPPC64());
3965
3966 const unsigned PtrByteSize = 8;
3967 const unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
3968
3969 static const MCPhysReg GPR[] = {
3970 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3971 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3972 };
3973 static const MCPhysReg VR[] = {
3974 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3975 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3976 };
3977
3978 const unsigned NumGPRs = array_lengthof(GPR);
3979 const unsigned NumFPRs = 13;
3980 const unsigned NumVRs = array_lengthof(VR);
3981 const unsigned ParamAreaSize = NumGPRs * PtrByteSize;
3982
3983 unsigned NumBytes = LinkageSize;
3984 unsigned AvailableFPRs = NumFPRs;
3985 unsigned AvailableVRs = NumVRs;
3986
3987 for (const ISD::OutputArg& Param : Outs) {
3988 if (Param.Flags.isNest()) continue;
3989
3990 if (CalculateStackSlotUsed(Param.VT, Param.ArgVT, Param.Flags,
3991 PtrByteSize, LinkageSize, ParamAreaSize,
3992 NumBytes, AvailableFPRs, AvailableVRs,
3993 Subtarget.hasQPX()))
3994 return true;
3995 }
3996 return false;
3997}
3998
3999static bool
4000hasSameArgumentList(const Function *CallerFn, ImmutableCallSite *CS) {
4001 if (CS->arg_size() != CallerFn->getArgumentList().size())
4002 return false;
4003
4004 ImmutableCallSite::arg_iterator CalleeArgIter = CS->arg_begin();
4005 ImmutableCallSite::arg_iterator CalleeArgEnd = CS->arg_end();
4006 Function::const_arg_iterator CallerArgIter = CallerFn->arg_begin();
4007
4008 for (; CalleeArgIter != CalleeArgEnd; ++CalleeArgIter, ++CallerArgIter) {
4009 const Value* CalleeArg = *CalleeArgIter;
4010 const Value* CallerArg = &(*CallerArgIter);
4011 if (CalleeArg == CallerArg)
4012 continue;
4013
4014 // e.g. @caller([4 x i64] %a, [4 x i64] %b) {
4015 // tail call @callee([4 x i64] undef, [4 x i64] %b)
4016 // }
4017 // 1st argument of callee is undef and has the same type as caller.
4018 if (CalleeArg->getType() == CallerArg->getType() &&
4019 isa<UndefValue>(CalleeArg))
4020 continue;
4021
4022 return false;
4023 }
4024
4025 return true;
4026}
4027
4028bool
4029PPCTargetLowering::IsEligibleForTailCallOptimization_64SVR4(
4030 SDValue Callee,
4031 CallingConv::ID CalleeCC,
4032 ImmutableCallSite *CS,
4033 bool isVarArg,
4034 const SmallVectorImpl<ISD::OutputArg> &Outs,
4035 const SmallVectorImpl<ISD::InputArg> &Ins,
4036 SelectionDAG& DAG) const {
4037 bool TailCallOpt = getTargetMachine().Options.GuaranteedTailCallOpt;
4038
4039 if (DisableSCO && !TailCallOpt) return false;
4040
4041 // Variadic argument functions are not supported.
4042 if (isVarArg) return false;
4043
4044 MachineFunction &MF = DAG.getMachineFunction();
4045 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
4046
4047 // Tail or Sibling call optimization (TCO/SCO) needs callee and caller has
4048 // the same calling convention
4049 if (CallerCC != CalleeCC) return false;
4050
4051 // SCO support C calling convention
4052 if (CalleeCC != CallingConv::Fast && CalleeCC != CallingConv::C)
4053 return false;
4054
Chuang-Yu Chengf7ba7162016-08-17 03:17:44 +00004055 // Caller contains any byval parameter is not supported.
David Majnemer0a16c222016-08-11 21:15:00 +00004056 if (any_of(Ins, [](const ISD::InputArg &IA) { return IA.Flags.isByVal(); }))
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00004057 return false;
4058
Chuang-Yu Chengf7ba7162016-08-17 03:17:44 +00004059 // Callee contains any byval parameter is not supported, too.
4060 // Note: This is a quick work around, because in some cases, e.g.
4061 // caller's stack size > callee's stack size, we are still able to apply
4062 // sibling call optimization. See: https://reviews.llvm.org/D23441#513574
4063 if (any_of(Outs, [](const ISD::OutputArg& OA) { return OA.Flags.isByVal(); }))
4064 return false;
4065
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00004066 // No TCO/SCO on indirect call because Caller have to restore its TOC
4067 if (!isFunctionGlobalAddress(Callee) &&
4068 !isa<ExternalSymbolSDNode>(Callee))
4069 return false;
4070
4071 // Check if Callee resides in the same module, because for now, PPC64 SVR4 ABI
4072 // (ELFv1/ELFv2) doesn't allow tail calls to a symbol resides in another
4073 // module.
4074 // ref: https://bugzilla.mozilla.org/show_bug.cgi?id=973977
4075 if (!resideInSameModule(Callee, getTargetMachine().getRelocationModel()))
4076 return false;
4077
4078 // TCO allows altering callee ABI, so we don't have to check further.
4079 if (CalleeCC == CallingConv::Fast && TailCallOpt)
4080 return true;
4081
4082 if (DisableSCO) return false;
4083
4084 // If callee use the same argument list that caller is using, then we can
4085 // apply SCO on this case. If it is not, then we need to check if callee needs
4086 // stack for passing arguments.
4087 if (!hasSameArgumentList(MF.getFunction(), CS) &&
4088 needStackSlotPassParameters(Subtarget, Outs)) {
4089 return false;
4090 }
4091
4092 return true;
4093}
4094
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004095/// IsEligibleForTailCallOptimization - Check whether the call is eligible
4096/// for tail call optimization. Targets which want to do tail call
4097/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004098bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004099PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004100 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004101 bool isVarArg,
4102 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004103 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004104 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00004105 return false;
4106
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004107 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004108 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00004109 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004110
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004111 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00004112 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004113 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
4114 // Functions containing by val parameters are not supported.
4115 for (unsigned i = 0; i != Ins.size(); i++) {
4116 ISD::ArgFlagsTy Flags = Ins[i].Flags;
4117 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004118 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004119
Alp Tokerf907b892013-12-05 05:44:44 +00004120 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004121 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
4122 return true;
4123
4124 // At the moment we can only do local tail calls (in same module, hidden
4125 // or protected) if we are generating PIC.
4126 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
4127 return G->getGlobal()->hasHiddenVisibility()
4128 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004129 }
4130
4131 return false;
4132}
4133
Chris Lattnereb755fc2006-05-17 19:00:46 +00004134/// isCallCompatibleAddress - Return the immediate to use if the specified
4135/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004136static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00004137 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00004138 if (!C) return nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004139
Dan Gohmaneffb8942008-09-12 16:56:44 +00004140 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00004141 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00004142 SignExtend32<26>(Addr) != Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +00004143 return nullptr; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004144
Eric Christophercd719462016-07-07 01:49:59 +00004145 return DAG
4146 .getConstant(
4147 (int)C->getZExtValue() >> 2, SDLoc(Op),
4148 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()))
4149 .getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00004150}
4151
Dan Gohmand78c4002008-05-13 00:00:25 +00004152namespace {
4153
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004154struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004155 SDValue Arg;
4156 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004157 int FrameIdx;
4158
4159 TailCallArgumentInfo() : FrameIdx(0) {}
4160};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00004161}
Dan Gohmand78c4002008-05-13 00:00:25 +00004162
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004163/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004164static void StoreTailCallArgumentsToStackSlot(
4165 SelectionDAG &DAG, SDValue Chain,
4166 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
4167 SmallVectorImpl<SDValue> &MemOpChains, const SDLoc &dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004168 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004169 SDValue Arg = TailCallArgs[i].Arg;
4170 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004171 int FI = TailCallArgs[i].FrameIdx;
4172 // Store relative to framepointer.
Alex Lorenze40c8a22015-08-11 23:09:45 +00004173 MemOpChains.push_back(DAG.getStore(
4174 Chain, dl, Arg, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00004175 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004176 }
4177}
4178
4179/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
4180/// the appropriate stack slot for the tail call optimized function call.
Eric Christopherc16ccbe2016-07-07 00:39:30 +00004181static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG, SDValue Chain,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004182 SDValue OldRetAddr, SDValue OldFP,
Eric Christopher327e4402016-07-07 01:08:17 +00004183 int SPDiff, const SDLoc &dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004184 if (SPDiff) {
4185 // Calculate the new stack slot for the return address.
Eric Christopherc16ccbe2016-07-07 00:39:30 +00004186 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherade4eed2016-07-07 00:39:32 +00004187 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
4188 const PPCFrameLowering *FL = Subtarget.getFrameLowering();
4189 bool isPPC64 = Subtarget.isPPC64();
4190 int SlotSize = isPPC64 ? 8 : 4;
Eric Christopherdc3a8a42015-02-13 00:39:38 +00004191 int NewRetAddrLoc = SPDiff + FL->getReturnSaveOffset();
Matthias Braun941a7052016-07-28 18:40:00 +00004192 int NewRetAddr = MF.getFrameInfo().CreateFixedObject(SlotSize,
4193 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00004194 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004195 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Eric Christopherc16ccbe2016-07-07 00:39:30 +00004196 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Justin Lebar9c375812016-07-15 18:27:10 +00004197 MachinePointerInfo::getFixedStack(MF, NewRetAddr));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004198
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004199 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
4200 // slot as the FP is never overwritten.
Eric Christopher327e4402016-07-07 01:08:17 +00004201 if (Subtarget.isDarwinABI()) {
Eric Christopherdc3a8a42015-02-13 00:39:38 +00004202 int NewFPLoc = SPDiff + FL->getFramePointerSaveOffset();
Matthias Braun941a7052016-07-28 18:40:00 +00004203 int NewFPIdx = MF.getFrameInfo().CreateFixedObject(SlotSize, NewFPLoc,
4204 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004205 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
Justin Lebar9c375812016-07-15 18:27:10 +00004206 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
4207 MachinePointerInfo::getFixedStack(
4208 DAG.getMachineFunction(), NewFPIdx));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004209 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004210 }
4211 return Chain;
4212}
4213
4214/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
4215/// the position of the argument.
4216static void
4217CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004218 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00004219 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004220 int Offset = ArgOffset + SPDiff;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00004221 uint32_t OpSize = (Arg.getValueSizeInBits() + 7) / 8;
Matthias Braun941a7052016-07-28 18:40:00 +00004222 int FI = MF.getFrameInfo().CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00004223 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004224 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004225 TailCallArgumentInfo Info;
4226 Info.Arg = Arg;
4227 Info.FrameIdxOp = FIN;
4228 Info.FrameIdx = FI;
4229 TailCallArguments.push_back(Info);
4230}
4231
4232/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
4233/// stack slot. Returns the chain as result and the loaded frame pointers in
4234/// LROpOut/FPOpout. Used when tail calling.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004235SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(
4236 SelectionDAG &DAG, int SPDiff, SDValue Chain, SDValue &LROpOut,
Eric Christophere0d09ba2016-07-07 01:08:21 +00004237 SDValue &FPOpOut, const SDLoc &dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004238 if (SPDiff) {
4239 // Load the LR and FP stack slot for later adjusting.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004240 EVT VT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004241 LROpOut = getReturnAddrFrameIndex(DAG);
Justin Lebar9c375812016-07-15 18:27:10 +00004242 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo());
Gabor Greiff304a7a2008-08-28 21:40:38 +00004243 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00004244
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004245 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
4246 // slot as the FP is never overwritten.
Eric Christophere0d09ba2016-07-07 01:08:21 +00004247 if (Subtarget.isDarwinABI()) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004248 FPOpOut = getFramePointerFrameIndex(DAG);
Justin Lebar9c375812016-07-15 18:27:10 +00004249 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004250 Chain = SDValue(FPOpOut.getNode(), 1);
4251 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004252 }
4253 return Chain;
4254}
4255
Dale Johannesen85d41a12008-03-04 23:17:14 +00004256/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00004257/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00004258/// specified by the specific parameter attribute. The copy will be passed as
4259/// a byval function parameter.
4260/// Sometimes what we are copying is the end of a larger object, the part that
4261/// does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004262static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
4263 SDValue Chain, ISD::ArgFlagsTy Flags,
4264 SelectionDAG &DAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004265 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00004266 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004267 false, false, false, MachinePointerInfo(),
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004268 MachinePointerInfo());
Dale Johannesen85d41a12008-03-04 23:17:14 +00004269}
Chris Lattner43df5b32007-02-25 05:34:32 +00004270
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004271/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
4272/// tail calls.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004273static void LowerMemOpCallTo(
4274 SelectionDAG &DAG, MachineFunction &MF, SDValue Chain, SDValue Arg,
4275 SDValue PtrOff, int SPDiff, unsigned ArgOffset, bool isPPC64,
4276 bool isTailCall, bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
4277 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments, const SDLoc &dl) {
Mehdi Amini44ede332015-07-09 02:09:04 +00004278 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004279 if (!isTailCall) {
4280 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004281 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004282 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004283 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004284 else
Owen Anderson9f944592009-08-11 20:47:22 +00004285 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00004286 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004287 DAG.getConstant(ArgOffset, dl, PtrVT));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004288 }
Justin Lebar9c375812016-07-15 18:27:10 +00004289 MemOpChains.push_back(
4290 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
4291 // Calculate and remember argument location.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004292 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
4293 TailCallArguments);
4294}
4295
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004296static void
4297PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Eric Christopher327e4402016-07-07 01:08:17 +00004298 const SDLoc &dl, int SPDiff, unsigned NumBytes, SDValue LROp,
4299 SDValue FPOp,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004300 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004301 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
4302 // might overwrite each other in case of tail call optimization.
4303 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00004304 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004305 InFlag = SDValue();
4306 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
4307 MemOpChains2, dl);
4308 if (!MemOpChains2.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004309 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004310
4311 // Store the return address to the appropriate stack slot.
Eric Christopher327e4402016-07-07 01:08:17 +00004312 Chain = EmitTailCallStoreFPAndRetAddr(DAG, Chain, LROp, FPOp, SPDiff, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004313
4314 // Emit callseq_end just before tailcall node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004315 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
4316 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004317 InFlag = Chain.getValue(1);
4318}
4319
Hal Finkel87deb0b2015-01-12 04:34:47 +00004320// Is this global address that of a function that can be called by name? (as
4321// opposed to something that must hold a descriptor for an indirect call).
4322static bool isFunctionGlobalAddress(SDValue Callee) {
4323 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
4324 if (Callee.getOpcode() == ISD::GlobalTLSAddress ||
4325 Callee.getOpcode() == ISD::TargetGlobalTLSAddress)
4326 return false;
4327
Manuel Jacob5f6eaac2016-01-16 20:30:46 +00004328 return G->getGlobal()->getValueType()->isFunctionTy();
Hal Finkel87deb0b2015-01-12 04:34:47 +00004329 }
4330
4331 return false;
4332}
4333
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004334static unsigned
4335PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag, SDValue &Chain,
4336 SDValue CallSeqStart, const SDLoc &dl, int SPDiff, bool isTailCall,
Eric Christopher2454a3b2016-07-07 01:08:23 +00004337 bool isPatchPoint, bool hasNest,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004338 SmallVectorImpl<std::pair<unsigned, SDValue>> &RegsToPass,
4339 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
4340 ImmutableCallSite *CS, const PPCSubtarget &Subtarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004341
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004342 bool isPPC64 = Subtarget.isPPC64();
4343 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004344 bool isELFv2ABI = Subtarget.isELFv2ABI();
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004345
Mehdi Amini44ede332015-07-09 02:09:04 +00004346 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Owen Anderson9f944592009-08-11 20:47:22 +00004347 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004348 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004349
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004350 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004351
Torok Edwin31e90d22010-08-04 20:47:44 +00004352 bool needIndirectCall = true;
Ulrich Weigand9aa09ef2014-06-18 16:14:04 +00004353 if (!isSVR4ABI || !isPPC64)
4354 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
4355 // If this is an absolute destination address, use the munged value.
4356 Callee = SDValue(Dest, 0);
4357 needIndirectCall = false;
4358 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004359
Rafael Espindola53fd4252016-06-23 18:43:06 +00004360 // PC-relative references to external symbols should go through $stub, unless
4361 // we're building with the leopard linker or later, which automatically
4362 // synthesizes these stubs.
Rafael Espindola3beef8d2016-06-27 23:15:57 +00004363 const TargetMachine &TM = DAG.getTarget();
Rafael Espindola53fd4252016-06-23 18:43:06 +00004364 const Module *Mod = DAG.getMachineFunction().getFunction()->getParent();
4365 const GlobalValue *GV = nullptr;
4366 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee))
4367 GV = G->getGlobal();
Rafael Espindola3beef8d2016-06-27 23:15:57 +00004368 bool Local = TM.shouldAssumeDSOLocal(*Mod, GV);
Rafael Espindolaa99ccfc2016-06-29 14:59:50 +00004369 bool UsePlt = !Local && Subtarget.isTargetELF() && !isPPC64;
Rafael Espindola53fd4252016-06-23 18:43:06 +00004370
Hal Finkel87deb0b2015-01-12 04:34:47 +00004371 if (isFunctionGlobalAddress(Callee)) {
4372 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Callee);
4373 // A call to a TLS address is actually an indirect call to a
4374 // thread-specific pointer.
Eric Christopher79cc1e32014-09-02 22:28:02 +00004375 unsigned OpFlags = 0;
Rafael Espindola53fd4252016-06-23 18:43:06 +00004376 if (UsePlt)
Rafael Espindolaa99ccfc2016-06-29 14:59:50 +00004377 OpFlags = PPCII::MO_PLT;
Eric Christopher79cc1e32014-09-02 22:28:02 +00004378
4379 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
4380 // every direct call is) turn it into a TargetGlobalAddress /
4381 // TargetExternalSymbol node so that legalize doesn't hack it.
4382 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
4383 Callee.getValueType(), 0, OpFlags);
4384 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00004385 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004386
Torok Edwin31e90d22010-08-04 20:47:44 +00004387 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004388 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00004389
Rafael Espindola53fd4252016-06-23 18:43:06 +00004390 if (UsePlt)
Rafael Espindolaa99ccfc2016-06-29 14:59:50 +00004391 OpFlags = PPCII::MO_PLT;
Wesley Peck527da1b2010-11-23 03:31:01 +00004392
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004393 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
4394 OpFlags);
4395 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00004396 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004397
Eric Christopher2454a3b2016-07-07 01:08:23 +00004398 if (isPatchPoint) {
Hal Finkel934361a2015-01-14 01:07:51 +00004399 // We'll form an invalid direct call when lowering a patchpoint; the full
4400 // sequence for an indirect call is complicated, and many of the
4401 // instructions introduced might have side effects (and, thus, can't be
4402 // removed later). The call itself will be removed as soon as the
4403 // argument/return lowering is complete, so the fact that it has the wrong
4404 // kind of operands should not really matter.
4405 needIndirectCall = false;
4406 }
4407
Torok Edwin31e90d22010-08-04 20:47:44 +00004408 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004409 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
4410 // to do the call, we can't use PPCISD::CALL.
4411 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00004412
Hal Finkel63fb9282015-01-13 18:25:05 +00004413 if (isSVR4ABI && isPPC64 && !isELFv2ABI) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00004414 // Function pointers in the 64-bit SVR4 ABI do not point to the function
4415 // entry point, but to the function descriptor (the function entry point
4416 // address is part of the function descriptor though).
4417 // The function descriptor is a three doubleword structure with the
4418 // following fields: function entry point, TOC base address and
4419 // environment pointer.
4420 // Thus for a call through a function pointer, the following actions need
4421 // to be performed:
4422 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00004423 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00004424 // 2. Load the address of the function entry point from the function
4425 // descriptor.
4426 // 3. Load the TOC of the callee from the function descriptor into r2.
4427 // 4. Load the environment pointer from the function descriptor into
4428 // r11.
4429 // 5. Branch to the function entry point address.
4430 // 6. On return of the callee, the TOC of the caller needs to be
4431 // restored (this is done in FinishCall()).
4432 //
Hal Finkele2ab0f12015-01-15 21:17:34 +00004433 // The loads are scheduled at the beginning of the call sequence, and the
4434 // register copies are flagged together to ensure that no other
Tilmann Scheller79fef932009-12-18 13:00:15 +00004435 // operations can be scheduled in between. E.g. without flagging the
Hal Finkele2ab0f12015-01-15 21:17:34 +00004436 // copies together, a TOC access in the caller could be scheduled between
4437 // the assignment of the callee TOC and the branch to the callee, which
Tilmann Scheller79fef932009-12-18 13:00:15 +00004438 // results in the TOC access going through the TOC of the callee instead
4439 // of going through the TOC of the caller, which leads to incorrect code.
4440
4441 // Load the address of the function entry point from the function
4442 // descriptor.
Hal Finkele2ab0f12015-01-15 21:17:34 +00004443 SDValue LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-1);
4444 if (LDChain.getValueType() == MVT::Glue)
4445 LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-2);
4446
Justin Lebar9c375812016-07-15 18:27:10 +00004447 auto MMOFlags = Subtarget.hasInvariantFunctionDescriptors()
Justin Lebaradbf09e2016-09-11 01:38:58 +00004448 ? (MachineMemOperand::MODereferenceable |
4449 MachineMemOperand::MOInvariant)
Justin Lebar9c375812016-07-15 18:27:10 +00004450 : MachineMemOperand::MONone;
Hal Finkele2ab0f12015-01-15 21:17:34 +00004451
4452 MachinePointerInfo MPI(CS ? CS->getCalledValue() : nullptr);
4453 SDValue LoadFuncPtr = DAG.getLoad(MVT::i64, dl, LDChain, Callee, MPI,
Justin Lebar9c375812016-07-15 18:27:10 +00004454 /* Alignment = */ 8, MMOFlags);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004455
4456 // Load environment pointer into r11.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004457 SDValue PtrOff = DAG.getIntPtrConstant(16, dl);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004458 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +00004459 SDValue LoadEnvPtr =
4460 DAG.getLoad(MVT::i64, dl, LDChain, AddPtr, MPI.getWithOffset(16),
4461 /* Alignment = */ 8, MMOFlags);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004462
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004463 SDValue TOCOff = DAG.getIntPtrConstant(8, dl);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004464 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, TOCOff);
Justin Lebar9c375812016-07-15 18:27:10 +00004465 SDValue TOCPtr =
4466 DAG.getLoad(MVT::i64, dl, LDChain, AddTOC, MPI.getWithOffset(8),
4467 /* Alignment = */ 8, MMOFlags);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004468
Hal Finkele6698d52015-02-01 15:03:28 +00004469 setUsesTOCBasePtr(DAG);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004470 SDValue TOCVal = DAG.getCopyToReg(Chain, dl, PPC::X2, TOCPtr,
4471 InFlag);
4472 Chain = TOCVal.getValue(0);
4473 InFlag = TOCVal.getValue(1);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004474
Hal Finkel965cea52015-07-12 00:37:44 +00004475 // If the function call has an explicit 'nest' parameter, it takes the
4476 // place of the environment pointer.
4477 if (!hasNest) {
4478 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
4479 InFlag);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004480
Hal Finkel965cea52015-07-12 00:37:44 +00004481 Chain = EnvVal.getValue(0);
4482 InFlag = EnvVal.getValue(1);
4483 }
Tilmann Scheller79fef932009-12-18 13:00:15 +00004484
Tilmann Scheller79fef932009-12-18 13:00:15 +00004485 MTCTROps[0] = Chain;
4486 MTCTROps[1] = LoadFuncPtr;
4487 MTCTROps[2] = InFlag;
4488 }
4489
Hal Finkel63fb9282015-01-13 18:25:05 +00004490 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys,
4491 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
4492 InFlag = Chain.getValue(1);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004493
4494 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00004495 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004496 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004497 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004498 CallOpc = PPCISD::BCTRL;
Craig Topper062a2ba2014-04-25 05:30:21 +00004499 Callee.setNode(nullptr);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004500 // Add use of X11 (holding environment pointer)
Hal Finkel965cea52015-07-12 00:37:44 +00004501 if (isSVR4ABI && isPPC64 && !isELFv2ABI && !hasNest)
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004502 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004503 // Add CTR register as callee so a bctr can be emitted later.
4504 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00004505 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004506 }
4507
4508 // If this is a direct call, pass the chain and the callee.
4509 if (Callee.getNode()) {
4510 Ops.push_back(Chain);
4511 Ops.push_back(Callee);
4512 }
4513 // If this is a tail call add stack pointer delta.
4514 if (isTailCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004515 Ops.push_back(DAG.getConstant(SPDiff, dl, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004516
4517 // Add argument registers to the end of the list so that they are known live
4518 // into the call.
4519 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
4520 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
4521 RegsToPass[i].second.getValueType()));
4522
Hal Finkelaf519932015-01-19 07:20:27 +00004523 // All calls, in both the ELF V1 and V2 ABIs, need the TOC register live
4524 // into the call.
Eric Christopher2454a3b2016-07-07 01:08:23 +00004525 if (isSVR4ABI && isPPC64 && !isPatchPoint) {
Hal Finkele6698d52015-02-01 15:03:28 +00004526 setUsesTOCBasePtr(DAG);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004527 Ops.push_back(DAG.getRegister(PPC::X2, PtrVT));
Hal Finkele6698d52015-02-01 15:03:28 +00004528 }
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004529
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004530 return CallOpc;
4531}
4532
Roman Divacky76293062012-09-18 16:47:58 +00004533static
4534bool isLocalCall(const SDValue &Callee)
4535{
4536 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00004537 return G->getGlobal()->isStrongDefinitionForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00004538 return false;
4539}
4540
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004541SDValue PPCTargetLowering::LowerCallResult(
4542 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
4543 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
4544 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004545
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004546 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00004547 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
4548 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004549 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004550
4551 // Copy all of the result registers out of their specified physreg.
4552 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
4553 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004554 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004555
4556 SDValue Val = DAG.getCopyFromReg(Chain, dl,
4557 VA.getLocReg(), VA.getLocVT(), InFlag);
4558 Chain = Val.getValue(1);
4559 InFlag = Val.getValue(2);
4560
4561 switch (VA.getLocInfo()) {
4562 default: llvm_unreachable("Unknown loc info!");
4563 case CCValAssign::Full: break;
4564 case CCValAssign::AExt:
4565 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4566 break;
4567 case CCValAssign::ZExt:
4568 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
4569 DAG.getValueType(VA.getValVT()));
4570 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4571 break;
4572 case CCValAssign::SExt:
4573 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
4574 DAG.getValueType(VA.getValVT()));
4575 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4576 break;
4577 }
4578
4579 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004580 }
4581
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004582 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004583}
4584
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004585SDValue PPCTargetLowering::FinishCall(
4586 CallingConv::ID CallConv, const SDLoc &dl, bool isTailCall, bool isVarArg,
Eric Christopher2454a3b2016-07-07 01:08:23 +00004587 bool isPatchPoint, bool hasNest, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004588 SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass, SDValue InFlag,
4589 SDValue Chain, SDValue CallSeqStart, SDValue &Callee, int SPDiff,
4590 unsigned NumBytes, const SmallVectorImpl<ISD::InputArg> &Ins,
4591 SmallVectorImpl<SDValue> &InVals, ImmutableCallSite *CS) const {
Ulrich Weigand8658f172014-07-20 23:43:15 +00004592
Owen Anderson53aa7a92009-08-10 22:56:29 +00004593 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004594 SmallVector<SDValue, 8> Ops;
Hal Finkele2ab0f12015-01-15 21:17:34 +00004595 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, CallSeqStart, dl,
Eric Christopher2454a3b2016-07-07 01:08:23 +00004596 SPDiff, isTailCall, isPatchPoint, hasNest,
Hal Finkel965cea52015-07-12 00:37:44 +00004597 RegsToPass, Ops, NodeTys, CS, Subtarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004598
Hal Finkel5ab37802012-08-28 02:10:27 +00004599 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004600 if (isVarArg && Subtarget.isSVR4ABI() && !Subtarget.isPPC64())
Hal Finkel5ab37802012-08-28 02:10:27 +00004601 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
4602
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004603 // When performing tail call optimization the callee pops its arguments off
4604 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00004605 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004606 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004607 (CallConv == CallingConv::Fast &&
4608 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004609
Roman Divackyef21be22012-03-06 16:41:49 +00004610 // Add a register mask operand representing the call-preserved registers.
Eric Christophercccae792015-01-30 22:02:31 +00004611 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00004612 const uint32_t *Mask =
4613 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv);
Roman Divackyef21be22012-03-06 16:41:49 +00004614 assert(Mask && "Missing call preserved mask for calling convention");
4615 Ops.push_back(DAG.getRegisterMask(Mask));
4616
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004617 if (InFlag.getNode())
4618 Ops.push_back(InFlag);
4619
4620 // Emit tail call.
4621 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004622 assert(((Callee.getOpcode() == ISD::Register &&
4623 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
4624 Callee.getOpcode() == ISD::TargetExternalSymbol ||
4625 Callee.getOpcode() == ISD::TargetGlobalAddress ||
4626 isa<ConstantSDNode>(Callee)) &&
4627 "Expecting an global address, external symbol, absolute value or register");
4628
Matthias Braun941a7052016-07-28 18:40:00 +00004629 DAG.getMachineFunction().getFrameInfo().setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00004630 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004631 }
4632
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004633 // Add a NOP immediately after the branch instruction when using the 64-bit
4634 // SVR4 ABI. At link time, if caller and callee are in a different module and
4635 // thus have a different TOC, the call will be replaced with a call to a stub
4636 // function which saves the current TOC, loads the TOC of the callee and
4637 // branches to the callee. The NOP will be replaced with a load instruction
4638 // which restores the TOC of the caller from the TOC save slot of the current
4639 // stack frame. If caller and callee belong to the same module (and have the
4640 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00004641
Hal Finkel934361a2015-01-14 01:07:51 +00004642 if (!isTailCall && Subtarget.isSVR4ABI()&& Subtarget.isPPC64() &&
Eric Christopher2454a3b2016-07-07 01:08:23 +00004643 !isPatchPoint) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004644 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00004645 // This is a call through a function pointer.
4646 // Restore the caller TOC from the save area into R2.
4647 // See PrepareCall() for more information about calls through function
4648 // pointers in the 64-bit SVR4 ABI.
4649 // We are using a target-specific load with r2 hard coded, because the
4650 // result of a target-independent load would never go directly into r2,
4651 // since r2 is a reserved register (which prevents the register allocator
4652 // from allocating it), resulting in an additional register being
4653 // allocated and an unnecessary move instruction being generated.
Hal Finkelfc096c92014-12-23 22:29:40 +00004654 CallOpc = PPCISD::BCTRL_LOAD_TOC;
4655
Eric Christophercd719462016-07-07 01:49:59 +00004656 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Hal Finkelfc096c92014-12-23 22:29:40 +00004657 SDValue StackPtr = DAG.getRegister(PPC::X1, PtrVT);
Eric Christopher736d39e2015-02-13 00:39:36 +00004658 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004659 SDValue TOCOff = DAG.getIntPtrConstant(TOCSaveOffset, dl);
Hal Finkelfc096c92014-12-23 22:29:40 +00004660 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, StackPtr, TOCOff);
4661
4662 // The address needs to go after the chain input but before the flag (or
4663 // any other variadic arguments).
4664 Ops.insert(std::next(Ops.begin()), AddTOC);
Bill Schmidtcea15962013-09-26 17:09:28 +00004665 } else if ((CallOpc == PPCISD::CALL) &&
4666 (!isLocalCall(Callee) ||
Bill Schmidt82f1c772015-02-10 19:09:05 +00004667 DAG.getTarget().getRelocationModel() == Reloc::PIC_))
Roman Divacky76293062012-09-18 16:47:58 +00004668 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004669 CallOpc = PPCISD::CALL_NOP;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004670 }
4671
Craig Topper48d114b2014-04-26 18:35:24 +00004672 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Hal Finkel51861b42012-03-31 14:45:15 +00004673 InFlag = Chain.getValue(1);
4674
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004675 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
4676 DAG.getIntPtrConstant(BytesCalleePops, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00004677 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004678 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004679 InFlag = Chain.getValue(1);
4680
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004681 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
4682 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004683}
4684
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004685SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00004686PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004687 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00004688 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00004689 SDLoc &dl = CLI.DL;
4690 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
4691 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
4692 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00004693 SDValue Chain = CLI.Chain;
4694 SDValue Callee = CLI.Callee;
4695 bool &isTailCall = CLI.IsTailCall;
4696 CallingConv::ID CallConv = CLI.CallConv;
4697 bool isVarArg = CLI.IsVarArg;
Eric Christopher2454a3b2016-07-07 01:08:23 +00004698 bool isPatchPoint = CLI.IsPatchPoint;
Hal Finkele2ab0f12015-01-15 21:17:34 +00004699 ImmutableCallSite *CS = CLI.CS;
Justin Holewinskiaa583972012-05-25 16:35:28 +00004700
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00004701 if (isTailCall) {
Hal Finkelb074a602016-08-30 00:59:23 +00004702 if (Subtarget.useLongCalls() && !(CS && CS->isMustTailCall()))
4703 isTailCall = false;
4704 else if (Subtarget.isSVR4ABI() && Subtarget.isPPC64())
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00004705 isTailCall =
4706 IsEligibleForTailCallOptimization_64SVR4(Callee, CallConv, CS,
4707 isVarArg, Outs, Ins, DAG);
4708 else
4709 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
4710 Ins, DAG);
4711 if (isTailCall) {
4712 ++NumTailCalls;
4713 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
4714 ++NumSiblingCalls;
4715
4716 assert(isa<GlobalAddressSDNode>(Callee) &&
4717 "Callee should be an llvm::Function object.");
4718 DEBUG(
4719 const GlobalValue *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal();
4720 const unsigned Width = 80 - strlen("TCO caller: ")
4721 - strlen(", callee linkage: 0, 0");
4722 dbgs() << "TCO caller: "
4723 << left_justify(DAG.getMachineFunction().getName(), Width)
4724 << ", callee linkage: "
4725 << GV->getVisibility() << ", " << GV->getLinkage() << "\n"
4726 );
4727 }
4728 }
Evan Cheng67a69dd2010-01-27 00:07:07 +00004729
Hal Finkele2ab0f12015-01-15 21:17:34 +00004730 if (!isTailCall && CS && CS->isMustTailCall())
Reid Kleckner5772b772014-04-24 20:14:34 +00004731 report_fatal_error("failed to perform tail call elimination on a call "
4732 "site marked musttail");
4733
Hal Finkelb074a602016-08-30 00:59:23 +00004734 // When long calls (i.e. indirect calls) are always used, calls are always
4735 // made via function pointer. If we have a function name, first translate it
4736 // into a pointer.
4737 if (Subtarget.useLongCalls() && isa<GlobalAddressSDNode>(Callee) &&
4738 !isTailCall)
4739 Callee = LowerGlobalAddress(Callee, DAG);
4740
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004741 if (Subtarget.isSVR4ABI()) {
4742 if (Subtarget.isPPC64())
Bill Schmidt57d6de52012-10-23 15:51:16 +00004743 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
Eric Christopher2454a3b2016-07-07 01:08:23 +00004744 isTailCall, isPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004745 dl, DAG, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004746 else
4747 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
Eric Christopher2454a3b2016-07-07 01:08:23 +00004748 isTailCall, isPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004749 dl, DAG, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004750 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004751
Bill Schmidt57d6de52012-10-23 15:51:16 +00004752 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
Eric Christopher2454a3b2016-07-07 01:08:23 +00004753 isTailCall, isPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004754 dl, DAG, InVals, CS);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004755}
4756
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004757SDValue PPCTargetLowering::LowerCall_32SVR4(
4758 SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg,
Eric Christopher2454a3b2016-07-07 01:08:23 +00004759 bool isTailCall, bool isPatchPoint,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004760 const SmallVectorImpl<ISD::OutputArg> &Outs,
4761 const SmallVectorImpl<SDValue> &OutVals,
4762 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
4763 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
4764 ImmutableCallSite *CS) const {
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004765 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004766 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004767
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004768 assert((CallConv == CallingConv::C ||
4769 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004770
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004771 unsigned PtrByteSize = 4;
4772
4773 MachineFunction &MF = DAG.getMachineFunction();
4774
4775 // Mark this function as potentially containing a function that contains a
4776 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4777 // and restoring the callers stack pointer in this functions epilog. This is
4778 // done because by tail calling the called function might overwrite the value
4779 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004780 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4781 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004782 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00004783
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004784 // Count how many bytes are to be pushed on the stack, including the linkage
4785 // area, parameter list area and the part of the local variable space which
4786 // contains copies of aggregates which are passed by value.
4787
4788 // Assign locations to all of the outgoing arguments.
4789 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher606a2682016-07-07 01:08:19 +00004790 PPCCCState CCInfo(CallConv, isVarArg, MF, ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004791
4792 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00004793 CCInfo.AllocateStack(Subtarget.getFrameLowering()->getLinkageSize(),
Ulrich Weigand8658f172014-07-20 23:43:15 +00004794 PtrByteSize);
Eric Christopher317df662016-07-07 01:49:57 +00004795 if (useSoftFloat())
Strahinja Petrovice682b802016-05-09 12:27:39 +00004796 CCInfo.PreAnalyzeCallOperands(Outs);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004797
4798 if (isVarArg) {
4799 // Handle fixed and variable vector arguments differently.
4800 // Fixed vector arguments go into registers as long as registers are
4801 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004802 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00004803
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004804 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00004805 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004806 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004807 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00004808
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004809 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00004810 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
4811 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004812 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00004813 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
4814 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004815 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004816
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004817 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00004818#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00004819 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00004820 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00004821#endif
Craig Toppere73658d2014-04-28 04:05:08 +00004822 llvm_unreachable(nullptr);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004823 }
4824 }
4825 } else {
4826 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00004827 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004828 }
Strahinja Petrovice682b802016-05-09 12:27:39 +00004829 CCInfo.clearWasPPCF128();
NAKAMURA Takumifd921542016-06-20 01:05:15 +00004830
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004831 // Assign locations to all of the outgoing aggregate by value arguments.
4832 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher606a2682016-07-07 01:08:19 +00004833 CCState CCByValInfo(CallConv, isVarArg, MF, ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004834
4835 // Reserve stack space for the allocations in CCInfo.
4836 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
4837
Bill Schmidtef17c142013-02-06 17:33:58 +00004838 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004839
4840 // Size of the linkage area, parameter list area and the part of the local
4841 // space variable where copies of aggregates which are passed by value are
4842 // stored.
4843 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004844
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004845 // Calculate by how many bytes the stack has to be adjusted in case of tail
4846 // call optimization.
4847 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4848
4849 // Adjust the stack pointer for the new arguments...
4850 // These operations are automatically eliminated by the prolog/epilog pass
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004851 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00004852 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004853 SDValue CallSeqStart = Chain;
4854
4855 // Load the return address and frame pointer so it can be moved somewhere else
4856 // later.
4857 SDValue LROp, FPOp;
Eric Christophere0d09ba2016-07-07 01:08:21 +00004858 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004859
4860 // Set up a copy of the stack pointer for use loading and storing any
4861 // arguments that may not fit in the registers available for argument
4862 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00004863 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00004864
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004865 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4866 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4867 SmallVector<SDValue, 8> MemOpChains;
4868
Roman Divacky71038e72011-08-30 17:04:16 +00004869 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004870 // Walk the register/memloc assignments, inserting copies/loads.
4871 for (unsigned i = 0, j = 0, e = ArgLocs.size();
4872 i != e;
4873 ++i) {
4874 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004875 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004876 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00004877
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004878 if (Flags.isByVal()) {
4879 // Argument is an aggregate which is passed by value, thus we need to
4880 // create a copy of it in the local variable space of the current stack
4881 // frame (which is the stack frame of the caller) and pass the address of
4882 // this copy to the callee.
4883 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
4884 CCValAssign &ByValVA = ByValArgLocs[j++];
4885 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00004886
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004887 // Memory reserved in the local variable space of the callers stack frame.
4888 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004889
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004890 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00004891 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()),
4892 StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00004893
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004894 // Create a copy of the argument in the local area of the current
4895 // stack frame.
4896 SDValue MemcpyCall =
4897 CreateCopyOfByValArgument(Arg, PtrOff,
4898 CallSeqStart.getNode()->getOperand(0),
4899 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00004900
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004901 // This must go outside the CALLSEQ_START..END.
4902 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004903 CallSeqStart.getNode()->getOperand(1),
4904 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004905 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4906 NewCallSeqStart.getNode());
4907 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00004908
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004909 // Pass the address of the aggregate copy on the stack either in a
4910 // physical register or in the parameter list area of the current stack
4911 // frame to the callee.
4912 Arg = PtrOff;
4913 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004914
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004915 if (VA.isRegLoc()) {
Hal Finkel2a9d3182014-03-06 00:23:33 +00004916 if (Arg.getValueType() == MVT::i1)
4917 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg);
4918
Roman Divacky71038e72011-08-30 17:04:16 +00004919 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004920 // Put argument in a physical register.
4921 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
4922 } else {
4923 // Put argument in the parameter list area of the current stack frame.
4924 assert(VA.isMemLoc());
4925 unsigned LocMemOffset = VA.getLocMemOffset();
4926
4927 if (!isTailCall) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004928 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00004929 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()),
4930 StackPtr, PtrOff);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004931
Justin Lebar9c375812016-07-15 18:27:10 +00004932 MemOpChains.push_back(
4933 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004934 } else {
4935 // Calculate and remember argument location.
4936 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
4937 TailCallArguments);
4938 }
4939 }
4940 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004941
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004942 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004943 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Wesley Peck527da1b2010-11-23 03:31:01 +00004944
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004945 // Build a sequence of copy-to-reg nodes chained together with token chain
4946 // and flag operands which copy the outgoing args into the appropriate regs.
4947 SDValue InFlag;
4948 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4949 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4950 RegsToPass[i].second, InFlag);
4951 InFlag = Chain.getValue(1);
4952 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004953
Hal Finkel5ab37802012-08-28 02:10:27 +00004954 // Set CR bit 6 to true if this is a vararg call with floating args passed in
4955 // registers.
4956 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004957 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
4958 SDValue Ops[] = { Chain, InFlag };
4959
Hal Finkel5ab37802012-08-28 02:10:27 +00004960 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00004961 dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1));
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004962
Hal Finkel5ab37802012-08-28 02:10:27 +00004963 InFlag = Chain.getValue(1);
4964 }
4965
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004966 if (isTailCall)
Eric Christopher327e4402016-07-07 01:08:17 +00004967 PrepareTailCall(DAG, InFlag, Chain, dl, SPDiff, NumBytes, LROp, FPOp,
Eric Christopherade4eed2016-07-07 00:39:32 +00004968 TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004969
Eric Christopher2454a3b2016-07-07 01:08:23 +00004970 return FinishCall(CallConv, dl, isTailCall, isVarArg, isPatchPoint,
Hal Finkel965cea52015-07-12 00:37:44 +00004971 /* unused except on PPC64 ELFv1 */ false, DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004972 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff,
4973 NumBytes, Ins, InVals, CS);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004974}
4975
Bill Schmidt57d6de52012-10-23 15:51:16 +00004976// Copy an argument into memory, being careful to do this outside the
4977// call sequence for the call to which the argument belongs.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004978SDValue PPCTargetLowering::createMemcpyOutsideCallSeq(
4979 SDValue Arg, SDValue PtrOff, SDValue CallSeqStart, ISD::ArgFlagsTy Flags,
4980 SelectionDAG &DAG, const SDLoc &dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004981 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
4982 CallSeqStart.getNode()->getOperand(0),
4983 Flags, DAG, dl);
4984 // The MEMCPY must go outside the CALLSEQ_START..END.
4985 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004986 CallSeqStart.getNode()->getOperand(1),
4987 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004988 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4989 NewCallSeqStart.getNode());
4990 return NewCallSeqStart;
4991}
4992
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004993SDValue PPCTargetLowering::LowerCall_64SVR4(
4994 SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg,
Eric Christopher2454a3b2016-07-07 01:08:23 +00004995 bool isTailCall, bool isPatchPoint,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004996 const SmallVectorImpl<ISD::OutputArg> &Outs,
4997 const SmallVectorImpl<SDValue> &OutVals,
4998 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
4999 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
5000 ImmutableCallSite *CS) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005001
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00005002 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00005003 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidt57d6de52012-10-23 15:51:16 +00005004 unsigned NumOps = Outs.size();
Hal Finkel965cea52015-07-12 00:37:44 +00005005 bool hasNest = false;
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005006 bool IsSibCall = false;
Bill Schmidt019cc6f2012-09-19 15:42:13 +00005007
Eric Christophercd719462016-07-07 01:49:59 +00005008 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bill Schmidt57d6de52012-10-23 15:51:16 +00005009 unsigned PtrByteSize = 8;
5010
5011 MachineFunction &MF = DAG.getMachineFunction();
5012
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005013 if (isTailCall && !getTargetMachine().Options.GuaranteedTailCallOpt)
5014 IsSibCall = true;
5015
Bill Schmidt57d6de52012-10-23 15:51:16 +00005016 // Mark this function as potentially containing a function that contains a
5017 // tail call. As a consequence the frame pointer will be used for dynamicalloc
5018 // and restoring the callers stack pointer in this functions epilog. This is
5019 // done because by tail calling the called function might overwrite the value
5020 // in this function's (MF) stack pointer stack slot 0(SP).
5021 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5022 CallConv == CallingConv::Fast)
5023 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
5024
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005025 assert(!(CallConv == CallingConv::Fast && isVarArg) &&
5026 "fastcc not supported on varargs functions");
5027
Bill Schmidt57d6de52012-10-23 15:51:16 +00005028 // Count how many bytes are to be pushed on the stack, including the linkage
Ulrich Weigand8658f172014-07-20 23:43:15 +00005029 // area, and parameter passing area. On ELFv1, the linkage area is 48 bytes
5030 // reserved space for [SP][CR][LR][2 x unused][TOC]; on ELFv2, the linkage
5031 // area is 32 bytes reserved space for [SP][CR][LR][TOC].
Eric Christophera4ae2132015-02-13 22:22:57 +00005032 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005033 unsigned NumBytes = LinkageSize;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005034 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Hal Finkelc93a9a22015-02-25 01:06:45 +00005035 unsigned &QFPR_idx = FPR_idx;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005036
5037 static const MCPhysReg GPR[] = {
5038 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
5039 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
5040 };
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005041 static const MCPhysReg VR[] = {
5042 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
5043 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
5044 };
5045 static const MCPhysReg VSRH[] = {
5046 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
5047 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
5048 };
5049
5050 const unsigned NumGPRs = array_lengthof(GPR);
5051 const unsigned NumFPRs = 13;
5052 const unsigned NumVRs = array_lengthof(VR);
Hal Finkelc93a9a22015-02-25 01:06:45 +00005053 const unsigned NumQFPRs = NumFPRs;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005054
5055 // When using the fast calling convention, we don't provide backing for
5056 // arguments that will be in registers.
5057 unsigned NumGPRsUsed = 0, NumFPRsUsed = 0, NumVRsUsed = 0;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005058
5059 // Add up all the space actually used.
5060 for (unsigned i = 0; i != NumOps; ++i) {
5061 ISD::ArgFlagsTy Flags = Outs[i].Flags;
5062 EVT ArgVT = Outs[i].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005063 EVT OrigVT = Outs[i].ArgVT;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005064
Hal Finkel965cea52015-07-12 00:37:44 +00005065 if (Flags.isNest())
5066 continue;
5067
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005068 if (CallConv == CallingConv::Fast) {
5069 if (Flags.isByVal())
5070 NumGPRsUsed += (Flags.getByValSize()+7)/8;
5071 else
5072 switch (ArgVT.getSimpleVT().SimpleTy) {
5073 default: llvm_unreachable("Unexpected ValueType for argument!");
5074 case MVT::i1:
5075 case MVT::i32:
5076 case MVT::i64:
5077 if (++NumGPRsUsed <= NumGPRs)
5078 continue;
5079 break;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005080 case MVT::v4i32:
5081 case MVT::v8i16:
5082 case MVT::v16i8:
5083 case MVT::v2f64:
5084 case MVT::v2i64:
Kit Bartond4eb73c2015-05-05 16:10:44 +00005085 case MVT::v1i128:
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005086 if (++NumVRsUsed <= NumVRs)
5087 continue;
5088 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00005089 case MVT::v4f32:
NAKAMURA Takumi84965032015-09-22 11:14:12 +00005090 // When using QPX, this is handled like a FP register, otherwise, it
5091 // is an Altivec register.
Hal Finkelc93a9a22015-02-25 01:06:45 +00005092 if (Subtarget.hasQPX()) {
5093 if (++NumFPRsUsed <= NumFPRs)
5094 continue;
5095 } else {
5096 if (++NumVRsUsed <= NumVRs)
5097 continue;
5098 }
5099 break;
5100 case MVT::f32:
5101 case MVT::f64:
5102 case MVT::v4f64: // QPX
5103 case MVT::v4i1: // QPX
5104 if (++NumFPRsUsed <= NumFPRs)
5105 continue;
5106 break;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005107 }
5108 }
5109
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005110 /* Respect alignment of argument on the stack. */
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005111 unsigned Align =
5112 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005113 NumBytes = ((NumBytes + Align - 1) / Align) * Align;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005114
5115 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005116 if (Flags.isInConsecutiveRegsLast())
5117 NumBytes = ((NumBytes + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005118 }
5119
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005120 unsigned NumBytesActuallyUsed = NumBytes;
5121
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005122 // The prolog code of the callee may store up to 8 GPR argument registers to
5123 // the stack, allowing va_start to index over them in memory if its varargs.
5124 // Because we cannot tell if this is needed on the caller side, we have to
5125 // conservatively assume that it is needed. As such, make sure we have at
5126 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00005127 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005128 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005129
5130 // Tail call needs the stack to be aligned.
5131 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5132 CallConv == CallingConv::Fast)
Eric Christophercccae792015-01-30 22:02:31 +00005133 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005134
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005135 int SPDiff = 0;
5136
Bill Schmidt57d6de52012-10-23 15:51:16 +00005137 // Calculate by how many bytes the stack has to be adjusted in case of tail
5138 // call optimization.
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005139 if (!IsSibCall)
5140 SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005141
5142 // To protect arguments on the stack from being clobbered in a tail call,
5143 // force all the loads to happen before doing any other lowering.
5144 if (isTailCall)
5145 Chain = DAG.getStackArgumentTokenFactor(Chain);
5146
5147 // Adjust the stack pointer for the new arguments...
5148 // These operations are automatically eliminated by the prolog/epilog pass
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005149 if (!IsSibCall)
5150 Chain = DAG.getCALLSEQ_START(Chain,
5151 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005152 SDValue CallSeqStart = Chain;
5153
5154 // Load the return address and frame pointer so it can be move somewhere else
5155 // later.
5156 SDValue LROp, FPOp;
Eric Christophere0d09ba2016-07-07 01:08:21 +00005157 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005158
5159 // Set up a copy of the stack pointer for use loading and storing any
5160 // arguments that may not fit in the registers available for argument
5161 // passing.
5162 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
5163
5164 // Figure out which arguments are going to go in registers, and which in
5165 // memory. Also, if this is a vararg function, floating point operations
5166 // must be stored to our stack, and loaded into integer regs as well, if
5167 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005168 unsigned ArgOffset = LinkageSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005169
5170 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
5171 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
5172
5173 SmallVector<SDValue, 8> MemOpChains;
5174 for (unsigned i = 0; i != NumOps; ++i) {
5175 SDValue Arg = OutVals[i];
5176 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005177 EVT ArgVT = Outs[i].VT;
5178 EVT OrigVT = Outs[i].ArgVT;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005179
5180 // PtrOff will be used to store the current argument to the stack if a
5181 // register cannot be found for it.
5182 SDValue PtrOff;
5183
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005184 // We re-align the argument offset for each argument, except when using the
5185 // fast calling convention, when we need to make sure we do that only when
5186 // we'll actually use a stack slot.
5187 auto ComputePtrOff = [&]() {
5188 /* Respect alignment of argument on the stack. */
5189 unsigned Align =
5190 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
5191 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005192
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005193 PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType());
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005194
5195 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
5196 };
5197
5198 if (CallConv != CallingConv::Fast) {
5199 ComputePtrOff();
5200
5201 /* Compute GPR index associated with argument offset. */
5202 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
5203 GPR_idx = std::min(GPR_idx, NumGPRs);
5204 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005205
5206 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00005207 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005208 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
5209 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
5210 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
5211 }
5212
5213 // FIXME memcpy is used way more than necessary. Correctness first.
5214 // Note: "by value" is code for passing a structure by value, not
5215 // basic types.
5216 if (Flags.isByVal()) {
5217 // Note: Size includes alignment padding, so
5218 // struct x { short a; char b; }
5219 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
5220 // These are the proper values we need for right-justifying the
5221 // aggregate in a parameter register.
5222 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00005223
5224 // An empty aggregate parameter takes up no storage and no
5225 // registers.
5226 if (Size == 0)
5227 continue;
5228
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005229 if (CallConv == CallingConv::Fast)
5230 ComputePtrOff();
5231
Bill Schmidt57d6de52012-10-23 15:51:16 +00005232 // All aggregates smaller than 8 bytes must be passed right-justified.
5233 if (Size==1 || Size==2 || Size==4) {
5234 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
5235 if (GPR_idx != NumGPRs) {
5236 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Justin Lebar9c375812016-07-15 18:27:10 +00005237 MachinePointerInfo(), VT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005238 MemOpChains.push_back(Load.getValue(1));
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005239 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005240
5241 ArgOffset += PtrByteSize;
5242 continue;
5243 }
5244 }
5245
5246 if (GPR_idx == NumGPRs && Size < 8) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00005247 SDValue AddPtr = PtrOff;
5248 if (!isLittleEndian) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005249 SDValue Const = DAG.getConstant(PtrByteSize - Size, dl,
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00005250 PtrOff.getValueType());
5251 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
5252 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005253 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
5254 CallSeqStart,
5255 Flags, DAG, dl);
5256 ArgOffset += PtrByteSize;
5257 continue;
5258 }
5259 // Copy entire object into memory. There are cases where gcc-generated
5260 // code assumes it is there, even if it could be put entirely into
5261 // registers. (This is not what the doc says.)
5262
5263 // FIXME: The above statement is likely due to a misunderstanding of the
5264 // documents. All arguments must be copied into the parameter area BY
5265 // THE CALLEE in the event that the callee takes the address of any
5266 // formal argument. That has not yet been implemented. However, it is
5267 // reasonable to use the stack area as a staging area for the register
5268 // load.
5269
5270 // Skip this for small aggregates, as we will use the same slot for a
5271 // right-justified copy, below.
5272 if (Size >= 8)
5273 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
5274 CallSeqStart,
5275 Flags, DAG, dl);
5276
5277 // When a register is available, pass a small aggregate right-justified.
5278 if (Size < 8 && GPR_idx != NumGPRs) {
5279 // The easiest way to get this right-justified in a register
5280 // is to copy the structure into the rightmost portion of a
5281 // local variable slot, then load the whole slot into the
5282 // register.
5283 // FIXME: The memcpy seems to produce pretty awful code for
5284 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00005285 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00005286 // parameter save area instead of a new local variable.
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00005287 SDValue AddPtr = PtrOff;
5288 if (!isLittleEndian) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005289 SDValue Const = DAG.getConstant(8 - Size, dl, PtrOff.getValueType());
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00005290 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
5291 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005292 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
5293 CallSeqStart,
5294 Flags, DAG, dl);
5295
5296 // Load the slot into the register.
Justin Lebar9c375812016-07-15 18:27:10 +00005297 SDValue Load =
5298 DAG.getLoad(PtrVT, dl, Chain, PtrOff, MachinePointerInfo());
Bill Schmidt57d6de52012-10-23 15:51:16 +00005299 MemOpChains.push_back(Load.getValue(1));
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005300 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005301
5302 // Done with this argument.
5303 ArgOffset += PtrByteSize;
5304 continue;
5305 }
5306
5307 // For aggregates larger than PtrByteSize, copy the pieces of the
5308 // object that fit into registers from the parameter save area.
5309 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005310 SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType());
Bill Schmidt57d6de52012-10-23 15:51:16 +00005311 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
5312 if (GPR_idx != NumGPRs) {
Justin Lebar9c375812016-07-15 18:27:10 +00005313 SDValue Load =
5314 DAG.getLoad(PtrVT, dl, Chain, AddArg, MachinePointerInfo());
Bill Schmidt57d6de52012-10-23 15:51:16 +00005315 MemOpChains.push_back(Load.getValue(1));
5316 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5317 ArgOffset += PtrByteSize;
5318 } else {
5319 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
5320 break;
5321 }
5322 }
5323 continue;
5324 }
5325
Craig Topper56710102013-08-15 02:33:50 +00005326 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005327 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00005328 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00005329 case MVT::i32:
5330 case MVT::i64:
Hal Finkel965cea52015-07-12 00:37:44 +00005331 if (Flags.isNest()) {
5332 // The 'nest' parameter, if any, is passed in R11.
5333 RegsToPass.push_back(std::make_pair(PPC::X11, Arg));
5334 hasNest = true;
5335 break;
5336 }
5337
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005338 // These can be scalar arguments or elements of an integer array type
5339 // passed directly. Clang may use those instead of "byval" aggregate
5340 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidt57d6de52012-10-23 15:51:16 +00005341 if (GPR_idx != NumGPRs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005342 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005343 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005344 if (CallConv == CallingConv::Fast)
5345 ComputePtrOff();
5346
Bill Schmidt57d6de52012-10-23 15:51:16 +00005347 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5348 true, isTailCall, false, MemOpChains,
5349 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005350 if (CallConv == CallingConv::Fast)
5351 ArgOffset += PtrByteSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005352 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005353 if (CallConv != CallingConv::Fast)
5354 ArgOffset += PtrByteSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005355 break;
5356 case MVT::f32:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005357 case MVT::f64: {
5358 // These can be scalar arguments or elements of a float array type
5359 // passed directly. The latter are used to implement ELFv2 homogenous
5360 // float aggregates.
5361
5362 // Named arguments go into FPRs first, and once they overflow, the
5363 // remaining arguments go into GPRs and then the parameter save area.
5364 // Unnamed arguments for vararg functions always go to GPRs and
5365 // then the parameter save area. For now, put all arguments to vararg
5366 // routines always in both locations (FPR *and* GPR or stack slot).
5367 bool NeedGPROrStack = isVarArg || FPR_idx == NumFPRs;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005368 bool NeededLoad = false;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005369
5370 // First load the argument into the next available FPR.
5371 if (FPR_idx != NumFPRs)
Bill Schmidt57d6de52012-10-23 15:51:16 +00005372 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
5373
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005374 // Next, load the argument into GPR or stack slot if needed.
5375 if (!NeedGPROrStack)
5376 ;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005377 else if (GPR_idx != NumGPRs && CallConv != CallingConv::Fast) {
Hal Finkel8ea446b2015-01-18 14:31:10 +00005378 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8
5379 // once we support fp <-> gpr moves.
5380
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005381 // In the non-vararg case, this can only ever happen in the
5382 // presence of f32 array types, since otherwise we never run
5383 // out of FPRs before running out of GPRs.
5384 SDValue ArgVal;
Bill Schmidtbd4ac262012-10-29 21:18:16 +00005385
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005386 // Double values are always passed in a single GPR.
5387 if (Arg.getValueType() != MVT::f32) {
5388 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005389
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005390 // Non-array float values are extended and passed in a GPR.
5391 } else if (!Flags.isInConsecutiveRegs()) {
5392 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
5393 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
5394
5395 // If we have an array of floats, we collect every odd element
5396 // together with its predecessor into one GPR.
5397 } else if (ArgOffset % PtrByteSize != 0) {
5398 SDValue Lo, Hi;
5399 Lo = DAG.getNode(ISD::BITCAST, dl, MVT::i32, OutVals[i - 1]);
5400 Hi = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
5401 if (!isLittleEndian)
5402 std::swap(Lo, Hi);
5403 ArgVal = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
5404
5405 // The final element, if even, goes into the first half of a GPR.
5406 } else if (Flags.isInConsecutiveRegsLast()) {
5407 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
5408 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
5409 if (!isLittleEndian)
5410 ArgVal = DAG.getNode(ISD::SHL, dl, MVT::i64, ArgVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005411 DAG.getConstant(32, dl, MVT::i32));
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005412
5413 // Non-final even elements are skipped; they will be handled
5414 // together the with subsequent argument on the next go-around.
5415 } else
5416 ArgVal = SDValue();
5417
5418 if (ArgVal.getNode())
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005419 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], ArgVal));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005420 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005421 if (CallConv == CallingConv::Fast)
5422 ComputePtrOff();
5423
Bill Schmidt57d6de52012-10-23 15:51:16 +00005424 // Single-precision floating-point values are mapped to the
5425 // second (rightmost) word of the stack doubleword.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005426 if (Arg.getValueType() == MVT::f32 &&
5427 !isLittleEndian && !Flags.isInConsecutiveRegs()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005428 SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType());
Bill Schmidt57d6de52012-10-23 15:51:16 +00005429 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
5430 }
5431
5432 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5433 true, isTailCall, false, MemOpChains,
5434 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005435
5436 NeededLoad = true;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005437 }
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005438 // When passing an array of floats, the array occupies consecutive
5439 // space in the argument area; only round up to the next doubleword
5440 // at the end of the array. Otherwise, each float takes 8 bytes.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005441 if (CallConv != CallingConv::Fast || NeededLoad) {
5442 ArgOffset += (Arg.getValueType() == MVT::f32 &&
5443 Flags.isInConsecutiveRegs()) ? 4 : 8;
5444 if (Flags.isInConsecutiveRegsLast())
5445 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
5446 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005447 break;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005448 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005449 case MVT::v4f32:
5450 case MVT::v4i32:
5451 case MVT::v8i16:
5452 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00005453 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00005454 case MVT::v2i64:
Kit Bartond4eb73c2015-05-05 16:10:44 +00005455 case MVT::v1i128:
Hal Finkelc93a9a22015-02-25 01:06:45 +00005456 if (!Subtarget.hasQPX()) {
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005457 // These can be scalar arguments or elements of a vector array type
5458 // passed directly. The latter are used to implement ELFv2 homogenous
5459 // vector aggregates.
5460
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00005461 // For a varargs call, named arguments go into VRs or on the stack as
5462 // usual; unnamed arguments always go to the stack or the corresponding
5463 // GPRs when within range. For now, we always put the value in both
5464 // locations (or even all three).
Bill Schmidt57d6de52012-10-23 15:51:16 +00005465 if (isVarArg) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005466 // We could elide this store in the case where the object fits
5467 // entirely in R registers. Maybe later.
Justin Lebar9c375812016-07-15 18:27:10 +00005468 SDValue Store =
5469 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo());
Bill Schmidt57d6de52012-10-23 15:51:16 +00005470 MemOpChains.push_back(Store);
5471 if (VR_idx != NumVRs) {
Justin Lebar9c375812016-07-15 18:27:10 +00005472 SDValue Load =
5473 DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, MachinePointerInfo());
Bill Schmidt57d6de52012-10-23 15:51:16 +00005474 MemOpChains.push_back(Load.getValue(1));
Hal Finkel7811c612014-03-28 19:58:11 +00005475
5476 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
5477 Arg.getSimpleValueType() == MVT::v2i64) ?
5478 VSRH[VR_idx] : VR[VR_idx];
5479 ++VR_idx;
5480
5481 RegsToPass.push_back(std::make_pair(VReg, Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005482 }
5483 ArgOffset += 16;
5484 for (unsigned i=0; i<16; i+=PtrByteSize) {
5485 if (GPR_idx == NumGPRs)
5486 break;
5487 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005488 DAG.getConstant(i, dl, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00005489 SDValue Load =
5490 DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo());
Bill Schmidt57d6de52012-10-23 15:51:16 +00005491 MemOpChains.push_back(Load.getValue(1));
5492 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5493 }
5494 break;
5495 }
5496
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00005497 // Non-varargs Altivec params go into VRs or on the stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00005498 if (VR_idx != NumVRs) {
Hal Finkel7811c612014-03-28 19:58:11 +00005499 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
5500 Arg.getSimpleValueType() == MVT::v2i64) ?
5501 VSRH[VR_idx] : VR[VR_idx];
5502 ++VR_idx;
5503
5504 RegsToPass.push_back(std::make_pair(VReg, Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005505 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005506 if (CallConv == CallingConv::Fast)
5507 ComputePtrOff();
5508
Bill Schmidt57d6de52012-10-23 15:51:16 +00005509 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5510 true, isTailCall, true, MemOpChains,
5511 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005512 if (CallConv == CallingConv::Fast)
5513 ArgOffset += 16;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005514 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005515
5516 if (CallConv != CallingConv::Fast)
5517 ArgOffset += 16;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005518 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00005519 } // not QPX
5520
5521 assert(Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32 &&
5522 "Invalid QPX parameter type");
5523
5524 /* fall through */
5525 case MVT::v4f64:
5526 case MVT::v4i1: {
5527 bool IsF32 = Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32;
5528 if (isVarArg) {
5529 // We could elide this store in the case where the object fits
5530 // entirely in R registers. Maybe later.
Justin Lebar9c375812016-07-15 18:27:10 +00005531 SDValue Store =
5532 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo());
Hal Finkelc93a9a22015-02-25 01:06:45 +00005533 MemOpChains.push_back(Store);
5534 if (QFPR_idx != NumQFPRs) {
Justin Lebar9c375812016-07-15 18:27:10 +00005535 SDValue Load = DAG.getLoad(IsF32 ? MVT::v4f32 : MVT::v4f64, dl, Store,
5536 PtrOff, MachinePointerInfo());
Hal Finkelc93a9a22015-02-25 01:06:45 +00005537 MemOpChains.push_back(Load.getValue(1));
5538 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Load));
5539 }
5540 ArgOffset += (IsF32 ? 16 : 32);
Aaron Ballman70c27de2015-02-25 13:02:23 +00005541 for (unsigned i = 0; i < (IsF32 ? 16U : 32U); i += PtrByteSize) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00005542 if (GPR_idx == NumGPRs)
5543 break;
5544 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005545 DAG.getConstant(i, dl, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00005546 SDValue Load =
5547 DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo());
Hal Finkelc93a9a22015-02-25 01:06:45 +00005548 MemOpChains.push_back(Load.getValue(1));
5549 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5550 }
5551 break;
5552 }
5553
5554 // Non-varargs QPX params go into registers or on the stack.
5555 if (QFPR_idx != NumQFPRs) {
5556 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Arg));
5557 } else {
5558 if (CallConv == CallingConv::Fast)
5559 ComputePtrOff();
5560
5561 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5562 true, isTailCall, true, MemOpChains,
5563 TailCallArguments, dl);
5564 if (CallConv == CallingConv::Fast)
5565 ArgOffset += (IsF32 ? 16 : 32);
5566 }
5567
5568 if (CallConv != CallingConv::Fast)
5569 ArgOffset += (IsF32 ? 16 : 32);
5570 break;
5571 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005572 }
5573 }
5574
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005575 assert(NumBytesActuallyUsed == ArgOffset);
Ulrich Weigandde8641b2014-07-07 19:39:44 +00005576 (void)NumBytesActuallyUsed;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005577
Bill Schmidt57d6de52012-10-23 15:51:16 +00005578 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00005579 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005580
5581 // Check if this is an indirect call (MTCTR/BCTRL).
5582 // See PrepareCall() for more information about calls through function
5583 // pointers in the 64-bit SVR4 ABI.
Eric Christopher2454a3b2016-07-07 01:08:23 +00005584 if (!isTailCall && !isPatchPoint &&
Hal Finkel87deb0b2015-01-12 04:34:47 +00005585 !isFunctionGlobalAddress(Callee) &&
5586 !isa<ExternalSymbolSDNode>(Callee)) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005587 // Load r2 into a virtual register and store it to the TOC save area.
Hal Finkele6698d52015-02-01 15:03:28 +00005588 setUsesTOCBasePtr(DAG);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005589 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
5590 // TOC save area offset.
Eric Christopher736d39e2015-02-13 00:39:36 +00005591 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005592 SDValue PtrOff = DAG.getIntPtrConstant(TOCSaveOffset, dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005593 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00005594 Chain = DAG.getStore(
5595 Val.getValue(1), dl, Val, AddPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00005596 MachinePointerInfo::getStack(DAG.getMachineFunction(), TOCSaveOffset));
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00005597 // In the ELFv2 ABI, R12 must contain the address of an indirect callee.
5598 // This does not mean the MTCTR instruction must use R12; it's easier
5599 // to model this as an extra parameter, so do that.
Eric Christopher2454a3b2016-07-07 01:08:23 +00005600 if (isELFv2ABI && !isPatchPoint)
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00005601 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005602 }
5603
5604 // Build a sequence of copy-to-reg nodes chained together with token chain
5605 // and flag operands which copy the outgoing args into the appropriate regs.
5606 SDValue InFlag;
5607 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
5608 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
5609 RegsToPass[i].second, InFlag);
5610 InFlag = Chain.getValue(1);
5611 }
5612
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005613 if (isTailCall && !IsSibCall)
Eric Christopher327e4402016-07-07 01:08:17 +00005614 PrepareTailCall(DAG, InFlag, Chain, dl, SPDiff, NumBytes, LROp, FPOp,
Eric Christopherade4eed2016-07-07 00:39:32 +00005615 TailCallArguments);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005616
Eric Christopher2454a3b2016-07-07 01:08:23 +00005617 return FinishCall(CallConv, dl, isTailCall, isVarArg, isPatchPoint, hasNest,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005618 DAG, RegsToPass, InFlag, Chain, CallSeqStart, Callee,
5619 SPDiff, NumBytes, Ins, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005620}
5621
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005622SDValue PPCTargetLowering::LowerCall_Darwin(
5623 SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg,
Eric Christopher2454a3b2016-07-07 01:08:23 +00005624 bool isTailCall, bool isPatchPoint,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005625 const SmallVectorImpl<ISD::OutputArg> &Outs,
5626 const SmallVectorImpl<SDValue> &OutVals,
5627 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
5628 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
5629 ImmutableCallSite *CS) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005630
5631 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005632
Eric Christophercd719462016-07-07 01:49:59 +00005633 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Owen Anderson9f944592009-08-11 20:47:22 +00005634 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005635 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005636
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005637 MachineFunction &MF = DAG.getMachineFunction();
5638
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005639 // Mark this function as potentially containing a function that contains a
5640 // tail call. As a consequence the frame pointer will be used for dynamicalloc
5641 // and restoring the callers stack pointer in this functions epilog. This is
5642 // done because by tail calling the called function might overwrite the value
5643 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00005644 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5645 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005646 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
5647
Chris Lattneraa40ec12006-05-16 22:56:08 +00005648 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00005649 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00005650 // prereserved space for [SP][CR][LR][3 x unused].
Eric Christophera4ae2132015-02-13 22:22:57 +00005651 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005652 unsigned NumBytes = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005653
5654 // Add up all the space actually used.
5655 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
5656 // they all go in registers, but we must reserve stack space for them for
5657 // possible use by the caller. In varargs or 64-bit calls, parameters are
5658 // assigned stack space in order, with padding so Altivec parameters are
5659 // 16-byte aligned.
5660 unsigned nAltivecParamsAtEnd = 0;
5661 for (unsigned i = 0; i != NumOps; ++i) {
5662 ISD::ArgFlagsTy Flags = Outs[i].Flags;
5663 EVT ArgVT = Outs[i].VT;
5664 // Varargs Altivec parameters are padded to a 16 byte boundary.
5665 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
5666 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
5667 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64) {
5668 if (!isVarArg && !isPPC64) {
5669 // Non-varargs Altivec parameters go after all the non-Altivec
5670 // parameters; handle those later so we know how much padding we need.
5671 nAltivecParamsAtEnd++;
5672 continue;
5673 }
5674 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
5675 NumBytes = ((NumBytes+15)/16)*16;
5676 }
5677 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
5678 }
5679
5680 // Allow for Altivec parameters at the end, if needed.
5681 if (nAltivecParamsAtEnd) {
5682 NumBytes = ((NumBytes+15)/16)*16;
5683 NumBytes += 16*nAltivecParamsAtEnd;
5684 }
5685
5686 // The prolog code of the callee may store up to 8 GPR argument registers to
5687 // the stack, allowing va_start to index over them in memory if its varargs.
5688 // Because we cannot tell if this is needed on the caller side, we have to
5689 // conservatively assume that it is needed. As such, make sure we have at
5690 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005691 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005692
5693 // Tail call needs the stack to be aligned.
5694 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5695 CallConv == CallingConv::Fast)
Eric Christophercccae792015-01-30 22:02:31 +00005696 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005697
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005698 // Calculate by how many bytes the stack has to be adjusted in case of tail
5699 // call optimization.
5700 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005701
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005702 // To protect arguments on the stack from being clobbered in a tail call,
5703 // force all the loads to happen before doing any other lowering.
5704 if (isTailCall)
5705 Chain = DAG.getStackArgumentTokenFactor(Chain);
5706
Chris Lattnerb7552a82006-05-17 00:15:40 +00005707 // Adjust the stack pointer for the new arguments...
5708 // These operations are automatically eliminated by the prolog/epilog pass
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005709 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00005710 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005711 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005712
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005713 // Load the return address and frame pointer so it can be move somewhere else
5714 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005715 SDValue LROp, FPOp;
Eric Christophere0d09ba2016-07-07 01:08:21 +00005716 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005717
Chris Lattnerb7552a82006-05-17 00:15:40 +00005718 // Set up a copy of the stack pointer for use loading and storing any
5719 // arguments that may not fit in the registers available for argument
5720 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005721 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005722 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00005723 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005724 else
Owen Anderson9f944592009-08-11 20:47:22 +00005725 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005726
Chris Lattnerb7552a82006-05-17 00:15:40 +00005727 // Figure out which arguments are going to go in registers, and which in
5728 // memory. Also, if this is a vararg function, floating point operations
5729 // must be stored to our stack, and loaded into integer regs as well, if
5730 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005731 unsigned ArgOffset = LinkageSize;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005732 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005733
Craig Topper840beec2014-04-04 05:16:06 +00005734 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005735 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
5736 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
5737 };
Craig Topper840beec2014-04-04 05:16:06 +00005738 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00005739 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
5740 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
5741 };
Craig Topper840beec2014-04-04 05:16:06 +00005742 static const MCPhysReg VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005743 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
5744 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
5745 };
Owen Andersone2f23a32007-09-07 04:06:50 +00005746 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005747 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00005748 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005749
Craig Topper840beec2014-04-04 05:16:06 +00005750 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005751
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005752 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005753 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
5754
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005755 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00005756 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005757 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005758 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00005759
Chris Lattnerb7552a82006-05-17 00:15:40 +00005760 // PtrOff will be used to store the current argument to the stack if a
5761 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005762 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005763
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005764 PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00005765
Dale Johannesen679073b2009-02-04 02:34:38 +00005766 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005767
5768 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00005769 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00005770 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
5771 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00005772 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005773 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00005774
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005775 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00005776 // Note: "by value" is code for passing a structure by value, not
5777 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00005778 if (Flags.isByVal()) {
5779 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00005780 // Very small objects are passed right-justified. Everything else is
5781 // passed left-justified.
5782 if (Size==1 || Size==2) {
5783 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005784 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00005785 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Justin Lebar9c375812016-07-15 18:27:10 +00005786 MachinePointerInfo(), VT);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005787 MemOpChains.push_back(Load.getValue(1));
5788 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005789
5790 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005791 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005792 SDValue Const = DAG.getConstant(PtrByteSize - Size, dl,
Bill Schmidt48081ca2012-10-16 13:30:53 +00005793 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005794 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005795 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
5796 CallSeqStart,
5797 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005798 ArgOffset += PtrByteSize;
5799 }
5800 continue;
5801 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00005802 // Copy entire object into memory. There are cases where gcc-generated
5803 // code assumes it is there, even if it could be put entirely into
5804 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00005805 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
5806 CallSeqStart,
5807 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00005808
5809 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
5810 // copy the pieces of the object that fit into registers from the
5811 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00005812 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005813 SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005814 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00005815 if (GPR_idx != NumGPRs) {
Justin Lebar9c375812016-07-15 18:27:10 +00005816 SDValue Load =
5817 DAG.getLoad(PtrVT, dl, Chain, AddArg, MachinePointerInfo());
Dale Johannesen0d235052008-03-05 23:31:27 +00005818 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00005819 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005820 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00005821 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00005822 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005823 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00005824 }
5825 }
5826 continue;
5827 }
5828
Craig Topper56710102013-08-15 02:33:50 +00005829 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005830 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00005831 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00005832 case MVT::i32:
5833 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005834 if (GPR_idx != NumGPRs) {
Hal Finkel7f908e82014-03-06 00:45:19 +00005835 if (Arg.getValueType() == MVT::i1)
5836 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg);
5837
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005838 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00005839 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005840 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5841 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005842 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00005843 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005844 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00005845 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005846 case MVT::f32:
5847 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005848 if (FPR_idx != NumFPRs) {
5849 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
5850
Chris Lattnerb7552a82006-05-17 00:15:40 +00005851 if (isVarArg) {
Justin Lebar9c375812016-07-15 18:27:10 +00005852 SDValue Store =
5853 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo());
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005854 MemOpChains.push_back(Store);
5855
Chris Lattnerb7552a82006-05-17 00:15:40 +00005856 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005857 if (GPR_idx != NumGPRs) {
Justin Lebar9c375812016-07-15 18:27:10 +00005858 SDValue Load =
5859 DAG.getLoad(PtrVT, dl, Store, PtrOff, MachinePointerInfo());
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005860 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005861 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00005862 }
Owen Anderson9f944592009-08-11 20:47:22 +00005863 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005864 SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005865 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Justin Lebar9c375812016-07-15 18:27:10 +00005866 SDValue Load =
5867 DAG.getLoad(PtrVT, dl, Store, PtrOff, MachinePointerInfo());
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005868 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005869 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00005870 }
5871 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00005872 // If we have any FPRs remaining, we may also have GPRs remaining.
5873 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
5874 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005875 if (GPR_idx != NumGPRs)
5876 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00005877 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005878 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
5879 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00005880 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005881 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005882 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5883 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005884 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005885 if (isPPC64)
5886 ArgOffset += 8;
5887 else
Owen Anderson9f944592009-08-11 20:47:22 +00005888 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00005889 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005890 case MVT::v4f32:
5891 case MVT::v4i32:
5892 case MVT::v8i16:
5893 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00005894 if (isVarArg) {
5895 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00005896 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00005897 // V registers; in fact gcc does this only for arguments that are
5898 // prototyped, not for those that match the ... We do it for all
5899 // arguments, seems to work.
5900 while (ArgOffset % 16 !=0) {
5901 ArgOffset += PtrByteSize;
5902 if (GPR_idx != NumGPRs)
5903 GPR_idx++;
5904 }
5905 // We could elide this store in the case where the object fits
5906 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005907 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005908 DAG.getConstant(ArgOffset, dl, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00005909 SDValue Store =
5910 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo());
Dale Johannesenb28456e2008-03-12 00:22:17 +00005911 MemOpChains.push_back(Store);
5912 if (VR_idx != NumVRs) {
Justin Lebar9c375812016-07-15 18:27:10 +00005913 SDValue Load =
5914 DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, MachinePointerInfo());
Dale Johannesenb28456e2008-03-12 00:22:17 +00005915 MemOpChains.push_back(Load.getValue(1));
5916 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
5917 }
5918 ArgOffset += 16;
5919 for (unsigned i=0; i<16; i+=PtrByteSize) {
5920 if (GPR_idx == NumGPRs)
5921 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00005922 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005923 DAG.getConstant(i, dl, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00005924 SDValue Load =
5925 DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo());
Dale Johannesenb28456e2008-03-12 00:22:17 +00005926 MemOpChains.push_back(Load.getValue(1));
5927 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5928 }
5929 break;
5930 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005931
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005932 // Non-varargs Altivec params generally go in registers, but have
5933 // stack space allocated at the end.
5934 if (VR_idx != NumVRs) {
5935 // Doesn't have GPR space allocated.
5936 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
5937 } else if (nAltivecParamsAtEnd==0) {
5938 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005939 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5940 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005941 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005942 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00005943 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00005944 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00005945 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00005946 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005947 // If all Altivec parameters fit in registers, as they usually do,
5948 // they get stack space following the non-Altivec parameters. We
5949 // don't track this here because nobody below needs it.
5950 // If there are more Altivec parameters than fit in registers emit
5951 // the stores here.
5952 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
5953 unsigned j = 0;
5954 // Offset is aligned; skip 1st 12 params which go in V registers.
5955 ArgOffset = ((ArgOffset+15)/16)*16;
5956 ArgOffset += 12*16;
5957 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005958 SDValue Arg = OutVals[i];
5959 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00005960 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
5961 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005962 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005963 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005964 // We are emitting Altivec params in order.
5965 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5966 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005967 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005968 ArgOffset += 16;
5969 }
5970 }
5971 }
5972 }
5973
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005974 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00005975 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005976
Dale Johannesen90eab672010-03-09 20:15:42 +00005977 // On Darwin, R12 must contain the address of an indirect callee. This does
5978 // not mean the MTCTR instruction must use R12; it's easier to model this as
5979 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00005980 if (!isTailCall &&
Hal Finkel87deb0b2015-01-12 04:34:47 +00005981 !isFunctionGlobalAddress(Callee) &&
5982 !isa<ExternalSymbolSDNode>(Callee) &&
Dale Johannesen90eab672010-03-09 20:15:42 +00005983 !isBLACompatibleAddress(Callee, DAG))
5984 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
5985 PPC::R12), Callee));
5986
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005987 // Build a sequence of copy-to-reg nodes chained together with token chain
5988 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005989 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005990 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00005991 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00005992 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005993 InFlag = Chain.getValue(1);
5994 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005995
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00005996 if (isTailCall)
Eric Christopher327e4402016-07-07 01:08:17 +00005997 PrepareTailCall(DAG, InFlag, Chain, dl, SPDiff, NumBytes, LROp, FPOp,
Eric Christopherade4eed2016-07-07 00:39:32 +00005998 TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005999
Eric Christopher2454a3b2016-07-07 01:08:23 +00006000 return FinishCall(CallConv, dl, isTailCall, isVarArg, isPatchPoint,
Hal Finkel965cea52015-07-12 00:37:44 +00006001 /* unused except on PPC64 ELFv1 */ false, DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00006002 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff,
6003 NumBytes, Ins, InVals, CS);
Chris Lattneraa40ec12006-05-16 22:56:08 +00006004}
6005
Hal Finkel450128a2011-10-14 19:51:36 +00006006bool
6007PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
6008 MachineFunction &MF, bool isVarArg,
6009 const SmallVectorImpl<ISD::OutputArg> &Outs,
6010 LLVMContext &Context) const {
6011 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00006012 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Hal Finkel450128a2011-10-14 19:51:36 +00006013 return CCInfo.CheckReturn(Outs, RetCC_PPC);
6014}
6015
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00006016SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006017PPCTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
6018 bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00006019 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00006020 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006021 const SDLoc &dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00006022
Chris Lattner4f2e4e02007-03-06 00:59:59 +00006023 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00006024 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
6025 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00006026 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006027
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006028 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00006029 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006030
Chris Lattner4f2e4e02007-03-06 00:59:59 +00006031 // Copy the result values into the output registers.
6032 for (unsigned i = 0; i != RVLocs.size(); ++i) {
6033 CCValAssign &VA = RVLocs[i];
6034 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00006035
6036 SDValue Arg = OutVals[i];
6037
6038 switch (VA.getLocInfo()) {
6039 default: llvm_unreachable("Unknown loc info!");
6040 case CCValAssign::Full: break;
6041 case CCValAssign::AExt:
6042 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
6043 break;
6044 case CCValAssign::ZExt:
6045 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
6046 break;
6047 case CCValAssign::SExt:
6048 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
6049 break;
6050 }
6051
6052 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00006053 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00006054 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00006055 }
6056
Chuang-Yu Cheng98c18942016-04-08 12:04:32 +00006057 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo();
6058 const MCPhysReg *I =
6059 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
6060 if (I) {
6061 for (; *I; ++I) {
6062
6063 if (PPC::G8RCRegClass.contains(*I))
6064 RetOps.push_back(DAG.getRegister(*I, MVT::i64));
6065 else if (PPC::F8RCRegClass.contains(*I))
6066 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64)));
6067 else if (PPC::CRRCRegClass.contains(*I))
6068 RetOps.push_back(DAG.getRegister(*I, MVT::i1));
6069 else if (PPC::VRRCRegClass.contains(*I))
6070 RetOps.push_back(DAG.getRegister(*I, MVT::Other));
6071 else
6072 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
6073 }
6074 }
6075
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00006076 RetOps[0] = Chain; // Update chain.
6077
6078 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006079 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00006080 RetOps.push_back(Flag);
6081
Craig Topper48d114b2014-04-26 18:35:24 +00006082 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps);
Chris Lattner4211ca92006-04-14 06:01:58 +00006083}
6084
Eric Christopherb976a392016-07-07 00:39:27 +00006085SDValue
6086PPCTargetLowering::LowerGET_DYNAMIC_AREA_OFFSET(SDValue Op,
6087 SelectionDAG &DAG) const {
Yury Gribovd7dbb662015-12-01 11:40:55 +00006088 SDLoc dl(Op);
6089
6090 // Get the corect type for integers.
6091 EVT IntVT = Op.getValueType();
6092
6093 // Get the inputs.
6094 SDValue Chain = Op.getOperand(0);
6095 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
6096 // Build a DYNAREAOFFSET node.
6097 SDValue Ops[2] = {Chain, FPSIdx};
6098 SDVTList VTs = DAG.getVTList(IntVT);
6099 return DAG.getNode(PPCISD::DYNAREAOFFSET, dl, VTs, Ops);
6100}
6101
Eric Christopherb976a392016-07-07 00:39:27 +00006102SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op,
6103 SelectionDAG &DAG) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00006104 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006105 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006106
Jim Laskeye4f4d042006-12-04 22:04:42 +00006107 // Get the corect type for pointers.
Eric Christophercd719462016-07-07 01:49:59 +00006108 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Laskeye4f4d042006-12-04 22:04:42 +00006109
6110 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00006111 bool isPPC64 = Subtarget.isPPC64();
6112 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006113 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00006114
6115 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006116 SDValue Chain = Op.getOperand(0);
6117 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006118
Jim Laskeye4f4d042006-12-04 22:04:42 +00006119 // Load the old link SP.
Justin Lebar9c375812016-07-15 18:27:10 +00006120 SDValue LoadLinkSP =
6121 DAG.getLoad(PtrVT, dl, Chain, StackPtr, MachinePointerInfo());
Scott Michelcf0da6c2009-02-17 22:15:04 +00006122
Jim Laskeye4f4d042006-12-04 22:04:42 +00006123 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00006124 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006125
Jim Laskeye4f4d042006-12-04 22:04:42 +00006126 // Store the old link SP.
Justin Lebar9c375812016-07-15 18:27:10 +00006127 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo());
Jim Laskeye4f4d042006-12-04 22:04:42 +00006128}
6129
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00006130SDValue PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG &DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00006131 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006132 bool isPPC64 = Subtarget.isPPC64();
Eric Christophercd719462016-07-07 01:49:59 +00006133 EVT PtrVT = getPointerTy(MF.getDataLayout());
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006134
6135 // Get current frame pointer save index. The users of this index will be
6136 // primarily DYNALLOC instructions.
6137 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
6138 int RASI = FI->getReturnAddrSaveIndex();
6139
6140 // If the frame pointer save index hasn't been defined yet.
6141 if (!RASI) {
6142 // Find out what the fix offset of the frame pointer save area.
Eric Christopherf71609b2015-02-13 00:39:27 +00006143 int LROffset = Subtarget.getFrameLowering()->getReturnSaveOffset();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006144 // Allocate the frame index for frame pointer save area.
Matthias Braun941a7052016-07-28 18:40:00 +00006145 RASI = MF.getFrameInfo().CreateFixedObject(isPPC64? 8 : 4, LROffset, false);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006146 // Save the result.
6147 FI->setReturnAddrSaveIndex(RASI);
6148 }
6149 return DAG.getFrameIndex(RASI, PtrVT);
6150}
6151
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006152SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006153PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
6154 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006155 bool isPPC64 = Subtarget.isPPC64();
Eric Christophercd719462016-07-07 01:49:59 +00006156 EVT PtrVT = getPointerTy(MF.getDataLayout());
Jim Laskey48850c12006-11-16 22:43:37 +00006157
6158 // Get current frame pointer save index. The users of this index will be
6159 // primarily DYNALLOC instructions.
6160 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
6161 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006162
Jim Laskey48850c12006-11-16 22:43:37 +00006163 // If the frame pointer save index hasn't been defined yet.
6164 if (!FPSI) {
6165 // Find out what the fix offset of the frame pointer save area.
Eric Christopherdc3a8a42015-02-13 00:39:38 +00006166 int FPOffset = Subtarget.getFrameLowering()->getFramePointerSaveOffset();
Jim Laskey48850c12006-11-16 22:43:37 +00006167 // Allocate the frame index for frame pointer save area.
Matthias Braun941a7052016-07-28 18:40:00 +00006168 FPSI = MF.getFrameInfo().CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00006169 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006170 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00006171 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006172 return DAG.getFrameIndex(FPSI, PtrVT);
6173}
Jim Laskey48850c12006-11-16 22:43:37 +00006174
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006175SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Eric Christopherb976a392016-07-07 00:39:27 +00006176 SelectionDAG &DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00006177 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006178 SDValue Chain = Op.getOperand(0);
6179 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006180 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006181
Jim Laskey48850c12006-11-16 22:43:37 +00006182 // Get the corect type for pointers.
Eric Christophercd719462016-07-07 01:49:59 +00006183 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Laskey48850c12006-11-16 22:43:37 +00006184 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00006185 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006186 DAG.getConstant(0, dl, PtrVT), Size);
Jim Laskey48850c12006-11-16 22:43:37 +00006187 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006188 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00006189 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006190 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00006191 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Craig Topper48d114b2014-04-26 18:35:24 +00006192 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops);
Jim Laskey48850c12006-11-16 22:43:37 +00006193}
6194
Hal Finkel5081ac22016-09-01 10:28:47 +00006195SDValue PPCTargetLowering::LowerEH_DWARF_CFA(SDValue Op,
6196 SelectionDAG &DAG) const {
6197 MachineFunction &MF = DAG.getMachineFunction();
6198
6199 bool isPPC64 = Subtarget.isPPC64();
6200 EVT PtrVT = getPointerTy(DAG.getDataLayout());
6201
6202 int FI = MF.getFrameInfo().CreateFixedObject(isPPC64 ? 8 : 4, 0, false);
6203 return DAG.getFrameIndex(FI, PtrVT);
6204}
6205
Hal Finkel756810f2013-03-21 21:37:52 +00006206SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
6207 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006208 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00006209 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
6210 DAG.getVTList(MVT::i32, MVT::Other),
6211 Op.getOperand(0), Op.getOperand(1));
6212}
6213
6214SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
6215 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006216 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00006217 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
6218 Op.getOperand(0), Op.getOperand(1));
6219}
6220
Hal Finkel940ab932014-02-28 00:27:01 +00006221SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +00006222 if (Op.getValueType().isVector())
6223 return LowerVectorLoad(Op, DAG);
6224
Hal Finkel940ab932014-02-28 00:27:01 +00006225 assert(Op.getValueType() == MVT::i1 &&
6226 "Custom lowering only for i1 loads");
6227
6228 // First, load 8 bits into 32 bits, then truncate to 1 bit.
6229
6230 SDLoc dl(Op);
6231 LoadSDNode *LD = cast<LoadSDNode>(Op);
6232
6233 SDValue Chain = LD->getChain();
6234 SDValue BasePtr = LD->getBasePtr();
6235 MachineMemOperand *MMO = LD->getMemOperand();
6236
Mehdi Amini44ede332015-07-09 02:09:04 +00006237 SDValue NewLD =
6238 DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(DAG.getDataLayout()), Chain,
6239 BasePtr, MVT::i8, MMO);
Hal Finkel940ab932014-02-28 00:27:01 +00006240 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
6241
6242 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00006243 return DAG.getMergeValues(Ops, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00006244}
6245
6246SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +00006247 if (Op.getOperand(1).getValueType().isVector())
6248 return LowerVectorStore(Op, DAG);
6249
Hal Finkel940ab932014-02-28 00:27:01 +00006250 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
6251 "Custom lowering only for i1 stores");
6252
6253 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
6254
6255 SDLoc dl(Op);
6256 StoreSDNode *ST = cast<StoreSDNode>(Op);
6257
6258 SDValue Chain = ST->getChain();
6259 SDValue BasePtr = ST->getBasePtr();
6260 SDValue Value = ST->getValue();
6261 MachineMemOperand *MMO = ST->getMemOperand();
6262
Mehdi Amini44ede332015-07-09 02:09:04 +00006263 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(DAG.getDataLayout()),
6264 Value);
Hal Finkel940ab932014-02-28 00:27:01 +00006265 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
6266}
6267
6268// FIXME: Remove this once the ANDI glue bug is fixed:
6269SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
6270 assert(Op.getValueType() == MVT::i1 &&
6271 "Custom lowering only for i1 results");
6272
6273 SDLoc DL(Op);
6274 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
6275 Op.getOperand(0));
6276}
6277
Chris Lattner4211ca92006-04-14 06:01:58 +00006278/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
6279/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00006280SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00006281 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00006282 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
6283 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00006284 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006285
Hal Finkel81f87992013-04-07 22:11:09 +00006286 // We might be able to do better than this under some circumstances, but in
6287 // general, fsel-based lowering of select is a finite-math-only optimization.
6288 // For more information, see section F.3 of the 2.06 ISA specification.
6289 if (!DAG.getTarget().Options.NoInfsFPMath ||
6290 !DAG.getTarget().Options.NoNaNsFPMath)
6291 return Op;
Sanjay Patela2607012015-09-16 16:31:21 +00006292 // TODO: Propagate flags from the select rather than global settings.
6293 SDNodeFlags Flags;
6294 Flags.setNoInfs(true);
6295 Flags.setNoNaNs(true);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +00006296
Hal Finkel81f87992013-04-07 22:11:09 +00006297 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006298
Owen Anderson53aa7a92009-08-10 22:56:29 +00006299 EVT ResVT = Op.getValueType();
6300 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006301 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
6302 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006303 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006304
Chris Lattner4211ca92006-04-14 06:01:58 +00006305 // If the RHS of the comparison is a 0.0, we don't need to do the
6306 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00006307 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00006308 if (isFloatingPointZero(RHS))
6309 switch (CC) {
6310 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00006311 case ISD::SETNE:
6312 std::swap(TV, FV);
6313 case ISD::SETEQ:
6314 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
6315 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
6316 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
6317 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
6318 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
6319 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
6320 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006321 case ISD::SETULT:
6322 case ISD::SETLT:
6323 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00006324 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00006325 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00006326 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
6327 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00006328 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006329 case ISD::SETUGT:
6330 case ISD::SETGT:
6331 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00006332 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00006333 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00006334 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
6335 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00006336 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00006337 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006338 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006339
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006340 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00006341 switch (CC) {
6342 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00006343 case ISD::SETNE:
6344 std::swap(TV, FV);
6345 case ISD::SETEQ:
Sanjay Patela2607012015-09-16 16:31:21 +00006346 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, &Flags);
Hal Finkel81f87992013-04-07 22:11:09 +00006347 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6348 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
6349 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
6350 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
6351 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
6352 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
6353 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006354 case ISD::SETULT:
6355 case ISD::SETLT:
Sanjay Patela2607012015-09-16 16:31:21 +00006356 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, &Flags);
Owen Anderson9f944592009-08-11 20:47:22 +00006357 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6358 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00006359 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00006360 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00006361 case ISD::SETGE:
Sanjay Patela2607012015-09-16 16:31:21 +00006362 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, &Flags);
Owen Anderson9f944592009-08-11 20:47:22 +00006363 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6364 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00006365 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006366 case ISD::SETUGT:
6367 case ISD::SETGT:
Sanjay Patela2607012015-09-16 16:31:21 +00006368 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS, &Flags);
Owen Anderson9f944592009-08-11 20:47:22 +00006369 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6370 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00006371 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00006372 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00006373 case ISD::SETLE:
Sanjay Patela2607012015-09-16 16:31:21 +00006374 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS, &Flags);
Owen Anderson9f944592009-08-11 20:47:22 +00006375 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6376 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00006377 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006378 }
Eli Friedman5806e182009-05-28 04:31:08 +00006379 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00006380}
6381
Hal Finkeled844c42015-01-06 22:31:02 +00006382void PPCTargetLowering::LowerFP_TO_INTForReuse(SDValue Op, ReuseLoadInfo &RLI,
6383 SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006384 const SDLoc &dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00006385 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006386 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00006387 if (Src.getValueType() == MVT::f32)
6388 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00006389
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006390 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00006391 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006392 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00006393 case MVT::i32:
Eric Christophercccae792015-01-30 22:02:31 +00006394 Tmp = DAG.getNode(
6395 Op.getOpcode() == ISD::FP_TO_SINT
6396 ? PPCISD::FCTIWZ
6397 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ),
6398 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00006399 break;
Owen Anderson9f944592009-08-11 20:47:22 +00006400 case MVT::i64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006401 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) &&
Hal Finkel3f88d082013-04-01 18:42:58 +00006402 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00006403 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
6404 PPCISD::FCTIDUZ,
6405 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00006406 break;
6407 }
Duncan Sands2a287912008-07-19 16:26:02 +00006408
Chris Lattner4211ca92006-04-14 06:01:58 +00006409 // Convert the FP value to an int value through memory.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006410 bool i32Stack = Op.getValueType() == MVT::i32 && Subtarget.hasSTFIWX() &&
6411 (Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT());
Hal Finkelf6d45f22013-04-01 17:52:07 +00006412 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
6413 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
Alex Lorenze40c8a22015-08-11 23:09:45 +00006414 MachinePointerInfo MPI =
6415 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
Duncan Sands2a287912008-07-19 16:26:02 +00006416
Chris Lattner06a49542007-10-15 20:14:52 +00006417 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00006418 SDValue Chain;
6419 if (i32Stack) {
6420 MachineFunction &MF = DAG.getMachineFunction();
6421 MachineMemOperand *MMO =
6422 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
6423 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
6424 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00006425 DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO);
Hal Finkelf6d45f22013-04-01 17:52:07 +00006426 } else
Justin Lebar9c375812016-07-15 18:27:10 +00006427 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr, MPI);
Chris Lattner06a49542007-10-15 20:14:52 +00006428
6429 // Result is a load from the stack slot. If loading 4 bytes, make sure to
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00006430 // add in a bias on big endian.
Hal Finkelf6d45f22013-04-01 17:52:07 +00006431 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00006432 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006433 DAG.getConstant(4, dl, FIPtr.getValueType()));
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00006434 MPI = MPI.getWithOffset(Subtarget.isLittleEndian() ? 0 : 4);
Hal Finkelf6d45f22013-04-01 17:52:07 +00006435 }
6436
Hal Finkeled844c42015-01-06 22:31:02 +00006437 RLI.Chain = Chain;
6438 RLI.Ptr = FIPtr;
6439 RLI.MPI = MPI;
6440}
6441
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006442/// \brief Custom lowers floating point to integer conversions to use
6443/// the direct move instructions available in ISA 2.07 to avoid the
6444/// need for load/store combinations.
6445SDValue PPCTargetLowering::LowerFP_TO_INTDirectMove(SDValue Op,
6446 SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006447 const SDLoc &dl) const {
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006448 assert(Op.getOperand(0).getValueType().isFloatingPoint());
6449 SDValue Src = Op.getOperand(0);
6450
6451 if (Src.getValueType() == MVT::f32)
6452 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
6453
6454 SDValue Tmp;
6455 switch (Op.getSimpleValueType().SimpleTy) {
6456 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
6457 case MVT::i32:
6458 Tmp = DAG.getNode(
6459 Op.getOpcode() == ISD::FP_TO_SINT
6460 ? PPCISD::FCTIWZ
6461 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ),
6462 dl, MVT::f64, Src);
6463 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i32, Tmp);
6464 break;
6465 case MVT::i64:
6466 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) &&
6467 "i64 FP_TO_UINT is supported only with FPCVT");
6468 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
6469 PPCISD::FCTIDUZ,
6470 dl, MVT::f64, Src);
6471 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i64, Tmp);
6472 break;
6473 }
6474 return Tmp;
6475}
6476
Hal Finkeled844c42015-01-06 22:31:02 +00006477SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006478 const SDLoc &dl) const {
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006479 if (Subtarget.hasDirectMove() && Subtarget.isPPC64())
6480 return LowerFP_TO_INTDirectMove(Op, DAG, dl);
6481
Hal Finkeled844c42015-01-06 22:31:02 +00006482 ReuseLoadInfo RLI;
6483 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl);
6484
Justin Lebar9c375812016-07-15 18:27:10 +00006485 return DAG.getLoad(Op.getValueType(), dl, RLI.Chain, RLI.Ptr, RLI.MPI,
Justin Lebaradbf09e2016-09-11 01:38:58 +00006486 RLI.Alignment, RLI.MMOFlags(), RLI.AAInfo, RLI.Ranges);
Hal Finkeled844c42015-01-06 22:31:02 +00006487}
6488
6489// We're trying to insert a regular store, S, and then a load, L. If the
6490// incoming value, O, is a load, we might just be able to have our load use the
6491// address used by O. However, we don't know if anything else will store to
6492// that address before we can load from it. To prevent this situation, we need
6493// to insert our load, L, into the chain as a peer of O. To do this, we give L
6494// the same chain operand as O, we create a token factor from the chain results
6495// of O and L, and we replace all uses of O's chain result with that token
6496// factor (see spliceIntoChain below for this last part).
6497bool PPCTargetLowering::canReuseLoadAddress(SDValue Op, EVT MemVT,
6498 ReuseLoadInfo &RLI,
Hal Finkel6c392692015-01-09 01:34:30 +00006499 SelectionDAG &DAG,
6500 ISD::LoadExtType ET) const {
Hal Finkeled844c42015-01-06 22:31:02 +00006501 SDLoc dl(Op);
Hal Finkel6c392692015-01-09 01:34:30 +00006502 if (ET == ISD::NON_EXTLOAD &&
6503 (Op.getOpcode() == ISD::FP_TO_UINT ||
Hal Finkeled844c42015-01-06 22:31:02 +00006504 Op.getOpcode() == ISD::FP_TO_SINT) &&
6505 isOperationLegalOrCustom(Op.getOpcode(),
6506 Op.getOperand(0).getValueType())) {
6507
6508 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl);
6509 return true;
6510 }
6511
6512 LoadSDNode *LD = dyn_cast<LoadSDNode>(Op);
Hal Finkel6c392692015-01-09 01:34:30 +00006513 if (!LD || LD->getExtensionType() != ET || LD->isVolatile() ||
6514 LD->isNonTemporal())
Hal Finkeled844c42015-01-06 22:31:02 +00006515 return false;
6516 if (LD->getMemoryVT() != MemVT)
6517 return false;
6518
6519 RLI.Ptr = LD->getBasePtr();
Sanjay Patel75068522016-03-14 18:09:43 +00006520 if (LD->isIndexed() && !LD->getOffset().isUndef()) {
Hal Finkeled844c42015-01-06 22:31:02 +00006521 assert(LD->getAddressingMode() == ISD::PRE_INC &&
6522 "Non-pre-inc AM on PPC?");
6523 RLI.Ptr = DAG.getNode(ISD::ADD, dl, RLI.Ptr.getValueType(), RLI.Ptr,
6524 LD->getOffset());
6525 }
6526
6527 RLI.Chain = LD->getChain();
6528 RLI.MPI = LD->getPointerInfo();
Justin Lebaradbf09e2016-09-11 01:38:58 +00006529 RLI.IsDereferenceable = LD->isDereferenceable();
Hal Finkeled844c42015-01-06 22:31:02 +00006530 RLI.IsInvariant = LD->isInvariant();
6531 RLI.Alignment = LD->getAlignment();
6532 RLI.AAInfo = LD->getAAInfo();
6533 RLI.Ranges = LD->getRanges();
6534
6535 RLI.ResChain = SDValue(LD, LD->isIndexed() ? 2 : 1);
6536 return true;
6537}
6538
6539// Given the head of the old chain, ResChain, insert a token factor containing
6540// it and NewResChain, and make users of ResChain now be users of that token
6541// factor.
6542void PPCTargetLowering::spliceIntoChain(SDValue ResChain,
6543 SDValue NewResChain,
6544 SelectionDAG &DAG) const {
6545 if (!ResChain)
6546 return;
6547
6548 SDLoc dl(NewResChain);
6549
6550 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
6551 NewResChain, DAG.getUNDEF(MVT::Other));
6552 assert(TF.getNode() != NewResChain.getNode() &&
6553 "A new TF really is required here");
6554
6555 DAG.ReplaceAllUsesOfValueWith(ResChain, TF);
6556 DAG.UpdateNodeOperands(TF.getNode(), ResChain, NewResChain);
Chris Lattner4211ca92006-04-14 06:01:58 +00006557}
6558
Ehsan Amiri322eca32016-04-06 20:12:29 +00006559/// \brief Analyze profitability of direct move
6560/// prefer float load to int load plus direct move
6561/// when there is no integer use of int load
6562static bool directMoveIsProfitable(const SDValue &Op) {
6563 SDNode *Origin = Op.getOperand(0).getNode();
6564 if (Origin->getOpcode() != ISD::LOAD)
6565 return true;
6566
6567 for (SDNode::use_iterator UI = Origin->use_begin(),
6568 UE = Origin->use_end();
6569 UI != UE; ++UI) {
6570
6571 // Only look at the users of the loaded value.
6572 if (UI.getUse().get().getResNo() != 0)
6573 continue;
6574
6575 if (UI->getOpcode() != ISD::SINT_TO_FP &&
6576 UI->getOpcode() != ISD::UINT_TO_FP)
6577 return true;
6578 }
6579
6580 return false;
6581}
6582
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006583/// \brief Custom lowers integer to floating point conversions to use
6584/// the direct move instructions available in ISA 2.07 to avoid the
6585/// need for load/store combinations.
6586SDValue PPCTargetLowering::LowerINT_TO_FPDirectMove(SDValue Op,
6587 SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006588 const SDLoc &dl) const {
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006589 assert((Op.getValueType() == MVT::f32 ||
6590 Op.getValueType() == MVT::f64) &&
6591 "Invalid floating point type as target of conversion");
6592 assert(Subtarget.hasFPCVT() &&
6593 "Int to FP conversions with direct moves require FPCVT");
6594 SDValue FP;
6595 SDValue Src = Op.getOperand(0);
6596 bool SinglePrec = Op.getValueType() == MVT::f32;
6597 bool WordInt = Src.getSimpleValueType().SimpleTy == MVT::i32;
6598 bool Signed = Op.getOpcode() == ISD::SINT_TO_FP;
6599 unsigned ConvOp = Signed ? (SinglePrec ? PPCISD::FCFIDS : PPCISD::FCFID) :
6600 (SinglePrec ? PPCISD::FCFIDUS : PPCISD::FCFIDU);
6601
6602 if (WordInt) {
6603 FP = DAG.getNode(Signed ? PPCISD::MTVSRA : PPCISD::MTVSRZ,
6604 dl, MVT::f64, Src);
6605 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP);
6606 }
6607 else {
6608 FP = DAG.getNode(PPCISD::MTVSRA, dl, MVT::f64, Src);
6609 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP);
6610 }
6611
6612 return FP;
6613}
6614
Hal Finkelf6d45f22013-04-01 17:52:07 +00006615SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Hal Finkeled844c42015-01-06 22:31:02 +00006616 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006617 SDLoc dl(Op);
Hal Finkelc93a9a22015-02-25 01:06:45 +00006618
6619 if (Subtarget.hasQPX() && Op.getOperand(0).getValueType() == MVT::v4i1) {
6620 if (Op.getValueType() != MVT::v4f32 && Op.getValueType() != MVT::v4f64)
6621 return SDValue();
6622
6623 SDValue Value = Op.getOperand(0);
6624 // The values are now known to be -1 (false) or 1 (true). To convert this
6625 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
6626 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
6627 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +00006628
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006629 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64);
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00006630
Hal Finkelc93a9a22015-02-25 01:06:45 +00006631 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
6632
6633 if (Op.getValueType() != MVT::v4f64)
6634 Value = DAG.getNode(ISD::FP_ROUND, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006635 Op.getValueType(), Value,
6636 DAG.getIntPtrConstant(1, dl));
Hal Finkelc93a9a22015-02-25 01:06:45 +00006637 return Value;
6638 }
6639
Dan Gohmand6819da2008-03-11 01:59:03 +00006640 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00006641 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006642 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00006643
Hal Finkel6a56b212014-03-05 22:14:00 +00006644 if (Op.getOperand(0).getValueType() == MVT::i1)
6645 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006646 DAG.getConstantFP(1.0, dl, Op.getValueType()),
6647 DAG.getConstantFP(0.0, dl, Op.getValueType()));
Hal Finkel6a56b212014-03-05 22:14:00 +00006648
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006649 // If we have direct moves, we can do all the conversion, skip the store/load
6650 // however, without FPCVT we can't do most conversions.
Ehsan Amiri322eca32016-04-06 20:12:29 +00006651 if (Subtarget.hasDirectMove() && directMoveIsProfitable(Op) &&
6652 Subtarget.isPPC64() && Subtarget.hasFPCVT())
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006653 return LowerINT_TO_FPDirectMove(Op, DAG, dl);
6654
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006655 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006656 "UINT_TO_FP is supported only with FPCVT");
6657
6658 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00006659 // Otherwise, convert to double-precision and then round.
Eric Christophercccae792015-01-30 22:02:31 +00006660 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
6661 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS
6662 : PPCISD::FCFIDS)
6663 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU
6664 : PPCISD::FCFID);
6665 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
6666 ? MVT::f32
6667 : MVT::f64;
Hal Finkelf6d45f22013-04-01 17:52:07 +00006668
Owen Anderson9f944592009-08-11 20:47:22 +00006669 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006670 SDValue SINT = Op.getOperand(0);
6671 // When converting to single-precision, we actually need to convert
6672 // to double-precision first and then round to single-precision.
6673 // To avoid double-rounding effects during that operation, we have
6674 // to prepare the input operand. Bits that might be truncated when
6675 // converting to double-precision are replaced by a bit that won't
6676 // be lost at this stage, but is below the single-precision rounding
6677 // position.
6678 //
6679 // However, if -enable-unsafe-fp-math is in effect, accept double
6680 // rounding to avoid the extra overhead.
6681 if (Op.getValueType() == MVT::f32 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006682 !Subtarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006683 !DAG.getTarget().Options.UnsafeFPMath) {
6684
6685 // Twiddle input to make sure the low 11 bits are zero. (If this
6686 // is the case, we are guaranteed the value will fit into the 53 bit
6687 // mantissa of an IEEE double-precision value without rounding.)
6688 // If any of those low 11 bits were not zero originally, make sure
6689 // bit 12 (value 2048) is set instead, so that the final rounding
6690 // to single-precision gets the correct result.
6691 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006692 SINT, DAG.getConstant(2047, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006693 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006694 Round, DAG.getConstant(2047, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006695 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
6696 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006697 Round, DAG.getConstant(-2048, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006698
6699 // However, we cannot use that value unconditionally: if the magnitude
6700 // of the input value is small, the bit-twiddling we did above might
6701 // end up visibly changing the output. Fortunately, in that case, we
6702 // don't need to twiddle bits since the original input will convert
6703 // exactly to double-precision floating-point already. Therefore,
6704 // construct a conditional to use the original value if the top 11
6705 // bits are all sign-bit copies, and use the rounded value computed
6706 // above otherwise.
6707 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006708 SINT, DAG.getConstant(53, dl, MVT::i32));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006709 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006710 Cond, DAG.getConstant(1, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006711 Cond = DAG.getSetCC(dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006712 Cond, DAG.getConstant(1, dl, MVT::i64), ISD::SETUGT);
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006713
6714 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
6715 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00006716
Hal Finkeled844c42015-01-06 22:31:02 +00006717 ReuseLoadInfo RLI;
6718 SDValue Bits;
6719
Hal Finkel6c392692015-01-09 01:34:30 +00006720 MachineFunction &MF = DAG.getMachineFunction();
Hal Finkeled844c42015-01-06 22:31:02 +00006721 if (canReuseLoadAddress(SINT, MVT::i64, RLI, DAG)) {
Justin Lebaradbf09e2016-09-11 01:38:58 +00006722 Bits = DAG.getLoad(MVT::f64, dl, RLI.Chain, RLI.Ptr, RLI.MPI,
6723 RLI.Alignment, RLI.MMOFlags(), RLI.AAInfo, RLI.Ranges);
Hal Finkeled844c42015-01-06 22:31:02 +00006724 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
Hal Finkel6c392692015-01-09 01:34:30 +00006725 } else if (Subtarget.hasLFIWAX() &&
6726 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::SEXTLOAD)) {
6727 MachineMemOperand *MMO =
6728 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6729 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6730 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6731 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWAX, dl,
6732 DAG.getVTList(MVT::f64, MVT::Other),
6733 Ops, MVT::i32, MMO);
6734 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
6735 } else if (Subtarget.hasFPCVT() &&
6736 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::ZEXTLOAD)) {
6737 MachineMemOperand *MMO =
6738 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6739 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6740 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6741 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWZX, dl,
6742 DAG.getVTList(MVT::f64, MVT::Other),
6743 Ops, MVT::i32, MMO);
6744 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
6745 } else if (((Subtarget.hasLFIWAX() &&
6746 SINT.getOpcode() == ISD::SIGN_EXTEND) ||
6747 (Subtarget.hasFPCVT() &&
6748 SINT.getOpcode() == ISD::ZERO_EXTEND)) &&
6749 SINT.getOperand(0).getValueType() == MVT::i32) {
Matthias Braun941a7052016-07-28 18:40:00 +00006750 MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christophercd719462016-07-07 01:49:59 +00006751 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Hal Finkel6c392692015-01-09 01:34:30 +00006752
Matthias Braun941a7052016-07-28 18:40:00 +00006753 int FrameIdx = MFI.CreateStackObject(4, 4, false);
Hal Finkel6c392692015-01-09 01:34:30 +00006754 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
6755
Justin Lebar9c375812016-07-15 18:27:10 +00006756 SDValue Store =
6757 DAG.getStore(DAG.getEntryNode(), dl, SINT.getOperand(0), FIdx,
6758 MachinePointerInfo::getFixedStack(
6759 DAG.getMachineFunction(), FrameIdx));
Hal Finkel6c392692015-01-09 01:34:30 +00006760
6761 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
6762 "Expected an i32 store");
6763
6764 RLI.Ptr = FIdx;
6765 RLI.Chain = Store;
Alex Lorenze40c8a22015-08-11 23:09:45 +00006766 RLI.MPI =
6767 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Hal Finkel6c392692015-01-09 01:34:30 +00006768 RLI.Alignment = 4;
6769
6770 MachineMemOperand *MMO =
6771 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6772 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6773 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6774 Bits = DAG.getMemIntrinsicNode(SINT.getOpcode() == ISD::ZERO_EXTEND ?
6775 PPCISD::LFIWZX : PPCISD::LFIWAX,
6776 dl, DAG.getVTList(MVT::f64, MVT::Other),
6777 Ops, MVT::i32, MMO);
Hal Finkeled844c42015-01-06 22:31:02 +00006778 } else
6779 Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
6780
Hal Finkelf6d45f22013-04-01 17:52:07 +00006781 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
6782
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006783 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00006784 FP = DAG.getNode(ISD::FP_ROUND, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006785 MVT::f32, FP, DAG.getIntPtrConstant(0, dl));
Chris Lattner4211ca92006-04-14 06:01:58 +00006786 return FP;
6787 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006788
Owen Anderson9f944592009-08-11 20:47:22 +00006789 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006790 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00006791 // Since we only generate this in 64-bit mode, we can take advantage of
6792 // 64-bit registers. In particular, sign extend the input value into the
6793 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
6794 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00006795 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00006796 MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christophercd719462016-07-07 01:49:59 +00006797 EVT PtrVT = getPointerTy(MF.getDataLayout());
Scott Michelcf0da6c2009-02-17 22:15:04 +00006798
Hal Finkelbeb296b2013-03-31 10:12:51 +00006799 SDValue Ld;
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006800 if (Subtarget.hasLFIWAX() || Subtarget.hasFPCVT()) {
Hal Finkeled844c42015-01-06 22:31:02 +00006801 ReuseLoadInfo RLI;
6802 bool ReusingLoad;
6803 if (!(ReusingLoad = canReuseLoadAddress(Op.getOperand(0), MVT::i32, RLI,
6804 DAG))) {
Matthias Braun941a7052016-07-28 18:40:00 +00006805 int FrameIdx = MFI.CreateStackObject(4, 4, false);
Hal Finkeled844c42015-01-06 22:31:02 +00006806 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006807
Justin Lebar9c375812016-07-15 18:27:10 +00006808 SDValue Store =
6809 DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
6810 MachinePointerInfo::getFixedStack(
6811 DAG.getMachineFunction(), FrameIdx));
Hal Finkele53429a2013-03-31 01:58:02 +00006812
Hal Finkeled844c42015-01-06 22:31:02 +00006813 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
6814 "Expected an i32 store");
6815
6816 RLI.Ptr = FIdx;
6817 RLI.Chain = Store;
Alex Lorenze40c8a22015-08-11 23:09:45 +00006818 RLI.MPI =
6819 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Hal Finkeled844c42015-01-06 22:31:02 +00006820 RLI.Alignment = 4;
6821 }
6822
Hal Finkelbeb296b2013-03-31 10:12:51 +00006823 MachineMemOperand *MMO =
Hal Finkeled844c42015-01-06 22:31:02 +00006824 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6825 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6826 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
Hal Finkelf6d45f22013-04-01 17:52:07 +00006827 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
6828 PPCISD::LFIWZX : PPCISD::LFIWAX,
6829 dl, DAG.getVTList(MVT::f64, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00006830 Ops, MVT::i32, MMO);
Hal Finkeled844c42015-01-06 22:31:02 +00006831 if (ReusingLoad)
6832 spliceIntoChain(RLI.ResChain, Ld.getValue(1), DAG);
Hal Finkelbeb296b2013-03-31 10:12:51 +00006833 } else {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006834 assert(Subtarget.isPPC64() &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006835 "i32->FP without LFIWAX supported only on PPC64");
6836
Matthias Braun941a7052016-07-28 18:40:00 +00006837 int FrameIdx = MFI.CreateStackObject(8, 8, false);
Hal Finkelbeb296b2013-03-31 10:12:51 +00006838 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
6839
6840 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
6841 Op.getOperand(0));
6842
6843 // STD the extended value into the stack slot.
Alex Lorenze40c8a22015-08-11 23:09:45 +00006844 SDValue Store = DAG.getStore(
6845 DAG.getEntryNode(), dl, Ext64, FIdx,
Justin Lebar9c375812016-07-15 18:27:10 +00006846 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx));
Hal Finkelbeb296b2013-03-31 10:12:51 +00006847
6848 // Load the value as a double.
Alex Lorenze40c8a22015-08-11 23:09:45 +00006849 Ld = DAG.getLoad(
6850 MVT::f64, dl, Store, FIdx,
Justin Lebar9c375812016-07-15 18:27:10 +00006851 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx));
Hal Finkelbeb296b2013-03-31 10:12:51 +00006852 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006853
Chris Lattner4211ca92006-04-14 06:01:58 +00006854 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00006855 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006856 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006857 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP,
6858 DAG.getIntPtrConstant(0, dl));
Chris Lattner4211ca92006-04-14 06:01:58 +00006859 return FP;
6860}
6861
Dan Gohman21cea8a2010-04-17 15:26:15 +00006862SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
6863 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006864 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006865 /*
6866 The rounding mode is in bits 30:31 of FPSR, and has the following
6867 settings:
6868 00 Round to nearest
6869 01 Round to 0
6870 10 Round to +inf
6871 11 Round to -inf
6872
6873 FLT_ROUNDS, on the other hand, expects the following:
6874 -1 Undefined
6875 0 Round to 0
6876 1 Round to nearest
6877 2 Round to +inf
6878 3 Round to -inf
6879
6880 To perform the conversion, we do:
6881 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
6882 */
6883
6884 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00006885 EVT VT = Op.getValueType();
Eric Christophercd719462016-07-07 01:49:59 +00006886 EVT PtrVT = getPointerTy(MF.getDataLayout());
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006887
6888 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00006889 EVT NodeTys[] = {
6890 MVT::f64, // return register
6891 MVT::Glue // unused in this context
6892 };
Craig Topper2d2aa0c2014-04-30 07:17:30 +00006893 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, None);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006894
6895 // Save FP register to stack slot
Matthias Braun941a7052016-07-28 18:40:00 +00006896 int SSFI = MF.getFrameInfo().CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006897 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00006898 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain, StackSlot,
6899 MachinePointerInfo());
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006900
6901 // Load FP Control Word from low 32 bits of stack slot.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006902 SDValue Four = DAG.getConstant(4, dl, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00006903 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Justin Lebar9c375812016-07-15 18:27:10 +00006904 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo());
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006905
6906 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006907 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00006908 DAG.getNode(ISD::AND, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006909 CWD, DAG.getConstant(3, dl, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006910 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00006911 DAG.getNode(ISD::SRL, dl, MVT::i32,
6912 DAG.getNode(ISD::AND, dl, MVT::i32,
6913 DAG.getNode(ISD::XOR, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006914 CWD, DAG.getConstant(3, dl, MVT::i32)),
6915 DAG.getConstant(3, dl, MVT::i32)),
6916 DAG.getConstant(1, dl, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006917
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006918 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00006919 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006920
Duncan Sands13237ac2008-06-06 12:08:01 +00006921 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00006922 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006923}
6924
Dan Gohman21cea8a2010-04-17 15:26:15 +00006925SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006926 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006927 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006928 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00006929 assert(Op.getNumOperands() == 3 &&
6930 VT == Op.getOperand(1).getValueType() &&
6931 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006932
Chris Lattner601b8652006-09-20 03:47:40 +00006933 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00006934 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006935 SDValue Lo = Op.getOperand(0);
6936 SDValue Hi = Op.getOperand(1);
6937 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006938 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006939
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006940 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006941 DAG.getConstant(BitWidth, dl, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006942 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
6943 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
6944 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
6945 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006946 DAG.getConstant(-BitWidth, dl, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006947 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
6948 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
6949 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006950 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006951 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006952}
6953
Dan Gohman21cea8a2010-04-17 15:26:15 +00006954SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006955 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006956 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00006957 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00006958 assert(Op.getNumOperands() == 3 &&
6959 VT == Op.getOperand(1).getValueType() &&
6960 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006961
Dan Gohman8d2ead22008-03-07 20:36:53 +00006962 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00006963 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006964 SDValue Lo = Op.getOperand(0);
6965 SDValue Hi = Op.getOperand(1);
6966 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006967 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006968
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006969 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006970 DAG.getConstant(BitWidth, dl, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006971 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
6972 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
6973 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
6974 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006975 DAG.getConstant(-BitWidth, dl, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006976 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
6977 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
6978 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006979 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006980 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006981}
6982
Dan Gohman21cea8a2010-04-17 15:26:15 +00006983SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006984 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006985 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006986 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00006987 assert(Op.getNumOperands() == 3 &&
6988 VT == Op.getOperand(1).getValueType() &&
6989 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006990
Dan Gohman8d2ead22008-03-07 20:36:53 +00006991 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006992 SDValue Lo = Op.getOperand(0);
6993 SDValue Hi = Op.getOperand(1);
6994 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006995 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006996
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006997 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006998 DAG.getConstant(BitWidth, dl, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006999 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
7000 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
7001 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
7002 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007003 DAG.getConstant(-BitWidth, dl, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00007004 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
7005 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007006 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, dl, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00007007 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007008 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00007009 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00007010}
7011
7012//===----------------------------------------------------------------------===//
7013// Vector related lowering.
7014//
7015
Chris Lattner2a099c02006-04-17 06:00:21 +00007016/// BuildSplatI - Build a canonical splati of Val with an element size of
7017/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00007018static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007019 SelectionDAG &DAG, const SDLoc &dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00007020 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00007021
Benjamin Kramer7149aab2015-03-01 18:09:56 +00007022 static const MVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00007023 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00007024 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00007025
Owen Anderson9f944592009-08-11 20:47:22 +00007026 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007027
Chris Lattner09ed0ff2006-12-01 01:45:39 +00007028 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
7029 if (Val == -1)
7030 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007031
Owen Anderson53aa7a92009-08-10 22:56:29 +00007032 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007033
Chris Lattner2a099c02006-04-17 06:00:21 +00007034 // Build a canonical splat for this value.
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007035 return DAG.getBitcast(ReqVT, DAG.getConstant(Val, dl, CanonicalVT));
Chris Lattner2a099c02006-04-17 06:00:21 +00007036}
7037
Hal Finkelcf2e9082013-05-24 23:00:14 +00007038/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
7039/// specified intrinsic ID.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007040static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op, SelectionDAG &DAG,
7041 const SDLoc &dl, EVT DestVT = MVT::Other) {
Hal Finkelcf2e9082013-05-24 23:00:14 +00007042 if (DestVT == MVT::Other) DestVT = Op.getValueType();
7043 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007044 DAG.getConstant(IID, dl, MVT::i32), Op);
Hal Finkelcf2e9082013-05-24 23:00:14 +00007045}
7046
Chris Lattnera2cae1b2006-04-18 03:24:30 +00007047/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00007048/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007049static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007050 SelectionDAG &DAG, const SDLoc &dl,
Owen Anderson9f944592009-08-11 20:47:22 +00007051 EVT DestVT = MVT::Other) {
7052 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007053 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007054 DAG.getConstant(IID, dl, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00007055}
7056
Chris Lattnera2cae1b2006-04-18 03:24:30 +00007057/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
7058/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007059static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007060 SDValue Op2, SelectionDAG &DAG, const SDLoc &dl,
7061 EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00007062 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007063 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007064 DAG.getConstant(IID, dl, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00007065}
7066
Chris Lattner264c9082006-04-17 17:55:10 +00007067/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
7068/// amount. The result has the specified value type.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007069static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt, EVT VT,
7070 SelectionDAG &DAG, const SDLoc &dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00007071 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00007072 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
7073 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00007074
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007075 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00007076 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007077 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00007078 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00007079 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00007080}
7081
Chris Lattner19e90552006-04-14 05:19:18 +00007082// If this is a case we can't handle, return null and let the default
7083// expansion code take care of it. If we CAN select this case, and if it
7084// selects to a single instruction, return Op. Otherwise, if we can codegen
7085// this case more efficiently than a constant pool load, lower it to the
7086// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00007087SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
7088 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007089 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007090 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
Craig Toppere73658d2014-04-28 04:05:08 +00007091 assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00007092
Hal Finkelc93a9a22015-02-25 01:06:45 +00007093 if (Subtarget.hasQPX() && Op.getValueType() == MVT::v4i1) {
7094 // We first build an i32 vector, load it into a QPX register,
7095 // then convert it to a floating-point vector and compare it
7096 // to a zero vector to get the boolean result.
Matthias Braun941a7052016-07-28 18:40:00 +00007097 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
7098 int FrameIdx = MFI.CreateStackObject(16, 16, false);
Alex Lorenze40c8a22015-08-11 23:09:45 +00007099 MachinePointerInfo PtrInfo =
7100 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Mehdi Amini44ede332015-07-09 02:09:04 +00007101 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007102 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
7103
7104 assert(BVN->getNumOperands() == 4 &&
7105 "BUILD_VECTOR for v4i1 does not have 4 operands");
7106
7107 bool IsConst = true;
7108 for (unsigned i = 0; i < 4; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00007109 if (BVN->getOperand(i).isUndef()) continue;
Hal Finkelc93a9a22015-02-25 01:06:45 +00007110 if (!isa<ConstantSDNode>(BVN->getOperand(i))) {
7111 IsConst = false;
7112 break;
7113 }
7114 }
7115
7116 if (IsConst) {
7117 Constant *One =
7118 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), 1.0);
7119 Constant *NegOne =
7120 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), -1.0);
7121
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007122 Constant *CV[4];
Hal Finkelc93a9a22015-02-25 01:06:45 +00007123 for (unsigned i = 0; i < 4; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00007124 if (BVN->getOperand(i).isUndef())
Hal Finkelc93a9a22015-02-25 01:06:45 +00007125 CV[i] = UndefValue::get(Type::getFloatTy(*DAG.getContext()));
Artyom Skrobov314ee042015-11-25 19:41:11 +00007126 else if (isNullConstant(BVN->getOperand(i)))
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007127 CV[i] = NegOne;
Hal Finkelc93a9a22015-02-25 01:06:45 +00007128 else
7129 CV[i] = One;
7130 }
7131
7132 Constant *CP = ConstantVector::get(CV);
Mehdi Amini44ede332015-07-09 02:09:04 +00007133 SDValue CPIdx = DAG.getConstantPool(CP, getPointerTy(DAG.getDataLayout()),
7134 16 /* alignment */);
7135
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007136 SDValue Ops[] = {DAG.getEntryNode(), CPIdx};
7137 SDVTList VTs = DAG.getVTList({MVT::v4i1, /*chain*/ MVT::Other});
Alex Lorenze40c8a22015-08-11 23:09:45 +00007138 return DAG.getMemIntrinsicNode(
7139 PPCISD::QVLFSb, dl, VTs, Ops, MVT::v4f32,
7140 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007141 }
7142
7143 SmallVector<SDValue, 4> Stores;
7144 for (unsigned i = 0; i < 4; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00007145 if (BVN->getOperand(i).isUndef()) continue;
Hal Finkelc93a9a22015-02-25 01:06:45 +00007146
7147 unsigned Offset = 4*i;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007148 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007149 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
7150
7151 unsigned StoreSize = BVN->getOperand(i).getValueType().getStoreSize();
7152 if (StoreSize > 4) {
Justin Lebar9c375812016-07-15 18:27:10 +00007153 Stores.push_back(
7154 DAG.getTruncStore(DAG.getEntryNode(), dl, BVN->getOperand(i), Idx,
7155 PtrInfo.getWithOffset(Offset), MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007156 } else {
7157 SDValue StoreValue = BVN->getOperand(i);
7158 if (StoreSize < 4)
7159 StoreValue = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, StoreValue);
7160
Justin Lebar9c375812016-07-15 18:27:10 +00007161 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, StoreValue, Idx,
7162 PtrInfo.getWithOffset(Offset)));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007163 }
7164 }
7165
7166 SDValue StoreChain;
7167 if (!Stores.empty())
7168 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
7169 else
7170 StoreChain = DAG.getEntryNode();
7171
7172 // Now load from v4i32 into the QPX register; this will extend it to
7173 // v4i64 but not yet convert it to a floating point. Nevertheless, this
7174 // is typed as v4f64 because the QPX register integer states are not
7175 // explicitly represented.
7176
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007177 SDValue Ops[] = {StoreChain,
7178 DAG.getConstant(Intrinsic::ppc_qpx_qvlfiwz, dl, MVT::i32),
7179 FIdx};
7180 SDVTList VTs = DAG.getVTList({MVT::v4f64, /*chain*/ MVT::Other});
Hal Finkelc93a9a22015-02-25 01:06:45 +00007181
7182 SDValue LoadedVect = DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN,
7183 dl, VTs, Ops, MVT::v4i32, PtrInfo);
7184 LoadedVect = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007185 DAG.getConstant(Intrinsic::ppc_qpx_qvfcfidu, dl, MVT::i32),
Hal Finkelc93a9a22015-02-25 01:06:45 +00007186 LoadedVect);
7187
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007188 SDValue FPZeros = DAG.getConstantFP(0.0, dl, MVT::v4f64);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007189
7190 return DAG.getSetCC(dl, MVT::v4i1, LoadedVect, FPZeros, ISD::SETEQ);
7191 }
7192
7193 // All other QPX vectors are handled by generic code.
7194 if (Subtarget.hasQPX())
7195 return SDValue();
7196
Bob Wilson85cefe82009-03-02 23:24:16 +00007197 // Check if this is a splat of a constant value.
7198 APInt APSplatBits, APSplatUndef;
7199 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007200 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00007201 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Bill Schmidt91dd7652015-04-03 13:48:24 +00007202 HasAnyUndefs, 0, !Subtarget.isLittleEndian()) ||
7203 SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00007204 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00007205
Bob Wilson530e0382009-03-03 19:26:27 +00007206 unsigned SplatBits = APSplatBits.getZExtValue();
7207 unsigned SplatUndef = APSplatUndef.getZExtValue();
7208 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007209
Bob Wilson530e0382009-03-03 19:26:27 +00007210 // First, handle single instruction cases.
7211
7212 // All zeros?
7213 if (SplatBits == 0) {
7214 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00007215 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007216 SDValue Z = DAG.getConstant(0, dl, MVT::v4i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00007217 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00007218 }
Bob Wilson530e0382009-03-03 19:26:27 +00007219 return Op;
7220 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00007221
Bob Wilson530e0382009-03-03 19:26:27 +00007222 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
7223 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
7224 (32-SplatBitSize));
7225 if (SextVal >= -16 && SextVal <= 15)
7226 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007227
Bob Wilson530e0382009-03-03 19:26:27 +00007228 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007229
Bob Wilson530e0382009-03-03 19:26:27 +00007230 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00007231 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
7232 // If this value is in the range [17,31] and is odd, use:
7233 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
7234 // If this value is in the range [-31,-17] and is odd, use:
7235 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
7236 // Note the last two are three-instruction sequences.
7237 if (SextVal >= -32 && SextVal <= 31) {
7238 // To avoid having these optimizations undone by constant folding,
7239 // we convert to a pseudo that will be expanded later into one of
7240 // the above forms.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007241 SDValue Elt = DAG.getConstant(SextVal, dl, MVT::i32);
Bill Schmidt71dddd52014-05-27 15:57:51 +00007242 EVT VT = (SplatSize == 1 ? MVT::v16i8 :
7243 (SplatSize == 2 ? MVT::v8i16 : MVT::v4i32));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007244 SDValue EltSize = DAG.getConstant(SplatSize, dl, MVT::i32);
Bill Schmidt71dddd52014-05-27 15:57:51 +00007245 SDValue RetVal = DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
7246 if (VT == Op.getValueType())
7247 return RetVal;
7248 else
7249 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), RetVal);
Bob Wilson530e0382009-03-03 19:26:27 +00007250 }
7251
7252 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
7253 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
7254 // for fneg/fabs.
7255 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
7256 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00007257 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007258
7259 // Make the VSLW intrinsic, computing 0x8000_0000.
7260 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
7261 OnesV, DAG, dl);
7262
7263 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00007264 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00007265 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00007266 }
7267
7268 // Check to see if this is a wide variety of vsplti*, binop self cases.
7269 static const signed char SplatCsts[] = {
7270 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
7271 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
7272 };
7273
7274 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
7275 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
7276 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
7277 int i = SplatCsts[idx];
7278
7279 // Figure out what shift amount will be used by altivec if shifted by i in
7280 // this splat size.
7281 unsigned TypeShiftAmt = i & (SplatBitSize-1);
7282
7283 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00007284 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00007285 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007286 static const unsigned IIDs[] = { // Intrinsic to use for each size.
7287 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
7288 Intrinsic::ppc_altivec_vslw
7289 };
7290 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00007291 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00007292 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007293
Bob Wilson530e0382009-03-03 19:26:27 +00007294 // vsplti + srl self.
7295 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00007296 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007297 static const unsigned IIDs[] = { // Intrinsic to use for each size.
7298 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
7299 Intrinsic::ppc_altivec_vsrw
7300 };
7301 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00007302 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00007303 }
7304
Bob Wilson530e0382009-03-03 19:26:27 +00007305 // vsplti + sra self.
7306 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00007307 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007308 static const unsigned IIDs[] = { // Intrinsic to use for each size.
7309 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
7310 Intrinsic::ppc_altivec_vsraw
7311 };
7312 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00007313 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00007314 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007315
Bob Wilson530e0382009-03-03 19:26:27 +00007316 // vsplti + rol self.
7317 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
7318 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00007319 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007320 static const unsigned IIDs[] = { // Intrinsic to use for each size.
7321 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
7322 Intrinsic::ppc_altivec_vrlw
7323 };
7324 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00007325 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00007326 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007327
Bob Wilson530e0382009-03-03 19:26:27 +00007328 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00007329 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00007330 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bill Schmidt1e77bb12015-07-15 15:45:30 +00007331 unsigned Amt = Subtarget.isLittleEndian() ? 15 : 1;
7332 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00007333 }
Bob Wilson530e0382009-03-03 19:26:27 +00007334 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00007335 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00007336 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bill Schmidt1e77bb12015-07-15 15:45:30 +00007337 unsigned Amt = Subtarget.isLittleEndian() ? 14 : 2;
7338 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00007339 }
Bob Wilson530e0382009-03-03 19:26:27 +00007340 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00007341 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00007342 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bill Schmidt1e77bb12015-07-15 15:45:30 +00007343 unsigned Amt = Subtarget.isLittleEndian() ? 13 : 3;
7344 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007345 }
7346 }
7347
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007348 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00007349}
7350
Chris Lattner071ad012006-04-17 05:28:54 +00007351/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
7352/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007353static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00007354 SDValue RHS, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007355 const SDLoc &dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00007356 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00007357 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00007358 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007359
Chris Lattner071ad012006-04-17 05:28:54 +00007360 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00007361 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 +00007362 OP_VMRGHW,
7363 OP_VMRGLW,
7364 OP_VSPLTISW0,
7365 OP_VSPLTISW1,
7366 OP_VSPLTISW2,
7367 OP_VSPLTISW3,
7368 OP_VSLDOI4,
7369 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00007370 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00007371 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00007372
Chris Lattner071ad012006-04-17 05:28:54 +00007373 if (OpNum == OP_COPY) {
7374 if (LHSID == (1*9+2)*9+3) return LHS;
7375 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
7376 return RHS;
7377 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007378
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007379 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007380 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
7381 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007382
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007383 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00007384 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007385 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00007386 case OP_VMRGHW:
7387 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
7388 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
7389 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
7390 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
7391 break;
7392 case OP_VMRGLW:
7393 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
7394 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
7395 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
7396 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
7397 break;
7398 case OP_VSPLTISW0:
7399 for (unsigned i = 0; i != 16; ++i)
7400 ShufIdxs[i] = (i&3)+0;
7401 break;
7402 case OP_VSPLTISW1:
7403 for (unsigned i = 0; i != 16; ++i)
7404 ShufIdxs[i] = (i&3)+4;
7405 break;
7406 case OP_VSPLTISW2:
7407 for (unsigned i = 0; i != 16; ++i)
7408 ShufIdxs[i] = (i&3)+8;
7409 break;
7410 case OP_VSPLTISW3:
7411 for (unsigned i = 0; i != 16; ++i)
7412 ShufIdxs[i] = (i&3)+12;
7413 break;
7414 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007415 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007416 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007417 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007418 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007419 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007420 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00007421 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00007422 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
7423 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00007424 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00007425 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00007426}
7427
Chris Lattner19e90552006-04-14 05:19:18 +00007428/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
7429/// is a shuffle we can handle in a single instruction, return it. Otherwise,
7430/// return the code it can be lowered into. Worst case, it can always be
7431/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007432SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007433 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007434 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007435 SDValue V1 = Op.getOperand(0);
7436 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007437 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007438 EVT VT = Op.getValueType();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007439 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00007440
Nemanja Ivanovicb43bb612016-07-12 21:00:10 +00007441 unsigned ShiftElts, InsertAtByte;
7442 bool Swap;
7443 if (Subtarget.hasP9Vector() &&
7444 PPC::isXXINSERTWMask(SVOp, ShiftElts, InsertAtByte, Swap,
7445 isLittleEndian)) {
7446 if (Swap)
7447 std::swap(V1, V2);
7448 SDValue Conv1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
7449 SDValue Conv2 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V2);
7450 if (ShiftElts) {
7451 SDValue Shl = DAG.getNode(PPCISD::VECSHL, dl, MVT::v4i32, Conv2, Conv2,
7452 DAG.getConstant(ShiftElts, dl, MVT::i32));
7453 SDValue Ins = DAG.getNode(PPCISD::XXINSERT, dl, MVT::v4i32, Conv1, Shl,
7454 DAG.getConstant(InsertAtByte, dl, MVT::i32));
7455 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Ins);
7456 }
7457 SDValue Ins = DAG.getNode(PPCISD::XXINSERT, dl, MVT::v4i32, Conv1, Conv2,
7458 DAG.getConstant(InsertAtByte, dl, MVT::i32));
7459 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Ins);
7460 }
7461
Nemanja Ivanovic1a2b2f02016-05-04 16:04:02 +00007462 if (Subtarget.hasVSX()) {
7463 if (V2.isUndef() && PPC::isSplatShuffleMask(SVOp, 4)) {
7464 int SplatIdx = PPC::getVSPLTImmediate(SVOp, 4, DAG);
7465 SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
7466 SDValue Splat = DAG.getNode(PPCISD::XXSPLT, dl, MVT::v4i32, Conv,
7467 DAG.getConstant(SplatIdx, dl, MVT::i32));
7468 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Splat);
7469 }
Nemanja Ivanoviceebbcb62016-07-12 12:16:27 +00007470
7471 // Left shifts of 8 bytes are actually swaps. Convert accordingly.
7472 if (V2.isUndef() && PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) == 8) {
7473 SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
7474 SDValue Swap = DAG.getNode(PPCISD::SWAP_NO_CHAIN, dl, MVT::v2f64, Conv);
7475 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Swap);
7476 }
7477
Nemanja Ivanovic1a2b2f02016-05-04 16:04:02 +00007478 }
7479
Hal Finkelc93a9a22015-02-25 01:06:45 +00007480 if (Subtarget.hasQPX()) {
7481 if (VT.getVectorNumElements() != 4)
7482 return SDValue();
7483
Sanjay Patel57195842016-03-14 17:28:46 +00007484 if (V2.isUndef()) V2 = V1;
Hal Finkelc93a9a22015-02-25 01:06:45 +00007485
7486 int AlignIdx = PPC::isQVALIGNIShuffleMask(SVOp);
7487 if (AlignIdx != -1) {
7488 return DAG.getNode(PPCISD::QVALIGNI, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007489 DAG.getConstant(AlignIdx, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007490 } else if (SVOp->isSplat()) {
7491 int SplatIdx = SVOp->getSplatIndex();
7492 if (SplatIdx >= 4) {
7493 std::swap(V1, V2);
7494 SplatIdx -= 4;
7495 }
7496
Hal Finkelc93a9a22015-02-25 01:06:45 +00007497 return DAG.getNode(PPCISD::QVESPLATI, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007498 DAG.getConstant(SplatIdx, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007499 }
7500
7501 // Lower this into a qvgpci/qvfperm pair.
7502
7503 // Compute the qvgpci literal
7504 unsigned idx = 0;
7505 for (unsigned i = 0; i < 4; ++i) {
7506 int m = SVOp->getMaskElt(i);
7507 unsigned mm = m >= 0 ? (unsigned) m : i;
7508 idx |= mm << (3-i)*3;
7509 }
7510
7511 SDValue V3 = DAG.getNode(PPCISD::QVGPCI, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007512 DAG.getConstant(idx, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007513 return DAG.getNode(PPCISD::QVFPERM, dl, VT, V1, V2, V3);
7514 }
7515
Chris Lattner19e90552006-04-14 05:19:18 +00007516 // Cases that are handled by instructions that take permute immediates
7517 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
7518 // selected by the instruction selector.
Sanjay Patel57195842016-03-14 17:28:46 +00007519 if (V2.isUndef()) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007520 if (PPC::isSplatShuffleMask(SVOp, 1) ||
7521 PPC::isSplatShuffleMask(SVOp, 2) ||
7522 PPC::isSplatShuffleMask(SVOp, 4) ||
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00007523 PPC::isVPKUWUMShuffleMask(SVOp, 1, DAG) ||
7524 PPC::isVPKUHUMShuffleMask(SVOp, 1, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00007525 PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00007526 PPC::isVMRGLShuffleMask(SVOp, 1, 1, DAG) ||
7527 PPC::isVMRGLShuffleMask(SVOp, 2, 1, DAG) ||
7528 PPC::isVMRGLShuffleMask(SVOp, 4, 1, DAG) ||
7529 PPC::isVMRGHShuffleMask(SVOp, 1, 1, DAG) ||
7530 PPC::isVMRGHShuffleMask(SVOp, 2, 1, DAG) ||
Kit Barton13894c72015-06-25 15:17:40 +00007531 PPC::isVMRGHShuffleMask(SVOp, 4, 1, DAG) ||
Hal Finkel77c8b7f2015-09-02 16:52:37 +00007532 (Subtarget.hasP8Altivec() && (
7533 PPC::isVPKUDUMShuffleMask(SVOp, 1, DAG) ||
7534 PPC::isVMRGEOShuffleMask(SVOp, true, 1, DAG) ||
7535 PPC::isVMRGEOShuffleMask(SVOp, false, 1, DAG)))) {
Chris Lattner19e90552006-04-14 05:19:18 +00007536 return Op;
7537 }
7538 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007539
Chris Lattner19e90552006-04-14 05:19:18 +00007540 // Altivec has a variety of "shuffle immediates" that take two vector inputs
7541 // and produce a fixed permutation. If any of these match, do not lower to
7542 // VPERM.
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00007543 unsigned int ShuffleKind = isLittleEndian ? 2 : 0;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00007544 if (PPC::isVPKUWUMShuffleMask(SVOp, ShuffleKind, DAG) ||
7545 PPC::isVPKUHUMShuffleMask(SVOp, ShuffleKind, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00007546 PPC::isVSLDOIShuffleMask(SVOp, ShuffleKind, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00007547 PPC::isVMRGLShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
7548 PPC::isVMRGLShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
7549 PPC::isVMRGLShuffleMask(SVOp, 4, ShuffleKind, DAG) ||
7550 PPC::isVMRGHShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
7551 PPC::isVMRGHShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
Kit Barton13894c72015-06-25 15:17:40 +00007552 PPC::isVMRGHShuffleMask(SVOp, 4, ShuffleKind, DAG) ||
Hal Finkel77c8b7f2015-09-02 16:52:37 +00007553 (Subtarget.hasP8Altivec() && (
7554 PPC::isVPKUDUMShuffleMask(SVOp, ShuffleKind, DAG) ||
7555 PPC::isVMRGEOShuffleMask(SVOp, true, ShuffleKind, DAG) ||
7556 PPC::isVMRGEOShuffleMask(SVOp, false, ShuffleKind, DAG))))
Chris Lattner19e90552006-04-14 05:19:18 +00007557 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007558
Chris Lattner071ad012006-04-17 05:28:54 +00007559 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
7560 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00007561 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00007562
Chris Lattner071ad012006-04-17 05:28:54 +00007563 unsigned PFIndexes[4];
7564 bool isFourElementShuffle = true;
7565 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
7566 unsigned EltNo = 8; // Start out undef.
7567 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007568 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00007569 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007570
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007571 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00007572 if ((ByteSource & 3) != j) {
7573 isFourElementShuffle = false;
7574 break;
7575 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007576
Chris Lattner071ad012006-04-17 05:28:54 +00007577 if (EltNo == 8) {
7578 EltNo = ByteSource/4;
7579 } else if (EltNo != ByteSource/4) {
7580 isFourElementShuffle = false;
7581 break;
7582 }
7583 }
7584 PFIndexes[i] = EltNo;
7585 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007586
7587 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00007588 // perfect shuffle vector to determine if it is cost effective to do this as
7589 // discrete instructions, or whether we should use a vperm.
Bill Schmidtf910a062014-06-10 14:35:01 +00007590 // For now, we skip this for little endian until such time as we have a
7591 // little-endian perfect shuffle table.
7592 if (isFourElementShuffle && !isLittleEndian) {
Chris Lattner071ad012006-04-17 05:28:54 +00007593 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007594 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00007595 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007596
Chris Lattner071ad012006-04-17 05:28:54 +00007597 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
7598 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007599
Chris Lattner071ad012006-04-17 05:28:54 +00007600 // Determining when to avoid vperm is tricky. Many things affect the cost
7601 // of vperm, particularly how many times the perm mask needs to be computed.
7602 // For example, if the perm mask can be hoisted out of a loop or is already
7603 // used (perhaps because there are multiple permutes with the same shuffle
7604 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
7605 // the loop requires an extra register.
7606 //
7607 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00007608 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00007609 // available, if this block is within a loop, we should avoid using vperm
7610 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007611 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007612 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007613 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007614
Chris Lattner19e90552006-04-14 05:19:18 +00007615 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
7616 // vector that will get spilled to the constant pool.
Sanjay Patel57195842016-03-14 17:28:46 +00007617 if (V2.isUndef()) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007618
Chris Lattner19e90552006-04-14 05:19:18 +00007619 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
7620 // that it is in input element units, not in bytes. Convert now.
Bill Schmidt4aedff82014-06-06 14:06:26 +00007621
7622 // For little endian, the order of the input vectors is reversed, and
7623 // the permutation mask is complemented with respect to 31. This is
7624 // necessary to produce proper semantics with the big-endian-biased vperm
7625 // instruction.
Owen Anderson53aa7a92009-08-10 22:56:29 +00007626 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00007627 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007628
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007629 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007630 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
7631 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007632
Chris Lattner19e90552006-04-14 05:19:18 +00007633 for (unsigned j = 0; j != BytesPerElement; ++j)
Bill Schmidt4aedff82014-06-06 14:06:26 +00007634 if (isLittleEndian)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007635 ResultMask.push_back(DAG.getConstant(31 - (SrcElt*BytesPerElement + j),
7636 dl, MVT::i32));
Bill Schmidt4aedff82014-06-06 14:06:26 +00007637 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007638 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement + j, dl,
Bill Schmidt4aedff82014-06-06 14:06:26 +00007639 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00007640 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007641
Ahmed Bougacha128f8732016-04-26 21:15:30 +00007642 SDValue VPermMask = DAG.getBuildVector(MVT::v16i8, dl, ResultMask);
Bill Schmidt4aedff82014-06-06 14:06:26 +00007643 if (isLittleEndian)
7644 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
7645 V2, V1, VPermMask);
7646 else
7647 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
7648 V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00007649}
7650
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +00007651/// getVectorCompareInfo - Given an intrinsic, return false if it is not a
7652/// vector comparison. If it is, return true and fill in Opc/isDot with
Chris Lattner9754d142006-04-18 17:59:36 +00007653/// information about the intrinsic.
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +00007654static bool getVectorCompareInfo(SDValue Intrin, int &CompareOpc,
7655 bool &isDot, const PPCSubtarget &Subtarget) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00007656 unsigned IntrinsicID =
7657 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00007658 CompareOpc = -1;
7659 isDot = false;
7660 switch (IntrinsicID) {
7661 default: return false;
7662 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00007663 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
7664 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
7665 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
7666 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
7667 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007668 case Intrinsic::ppc_altivec_vcmpequd_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007669 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007670 CompareOpc = 199;
7671 isDot = 1;
7672 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007673 return false;
7674
7675 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007676 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
7677 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
7678 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
7679 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
7680 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007681 case Intrinsic::ppc_altivec_vcmpgtsd_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007682 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007683 CompareOpc = 967;
7684 isDot = 1;
7685 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007686 return false;
7687
7688 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007689 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
7690 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
7691 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007692 case Intrinsic::ppc_altivec_vcmpgtud_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007693 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007694 CompareOpc = 711;
7695 isDot = 1;
7696 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007697 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007698
Kit Barton0cfa7b72015-03-03 19:55:45 +00007699 break;
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +00007700 // VSX predicate comparisons use the same infrastructure
7701 case Intrinsic::ppc_vsx_xvcmpeqdp_p:
7702 case Intrinsic::ppc_vsx_xvcmpgedp_p:
7703 case Intrinsic::ppc_vsx_xvcmpgtdp_p:
7704 case Intrinsic::ppc_vsx_xvcmpeqsp_p:
7705 case Intrinsic::ppc_vsx_xvcmpgesp_p:
7706 case Intrinsic::ppc_vsx_xvcmpgtsp_p:
7707 if (Subtarget.hasVSX()) {
7708 switch (IntrinsicID) {
7709 case Intrinsic::ppc_vsx_xvcmpeqdp_p: CompareOpc = 99; break;
7710 case Intrinsic::ppc_vsx_xvcmpgedp_p: CompareOpc = 115; break;
7711 case Intrinsic::ppc_vsx_xvcmpgtdp_p: CompareOpc = 107; break;
7712 case Intrinsic::ppc_vsx_xvcmpeqsp_p: CompareOpc = 67; break;
7713 case Intrinsic::ppc_vsx_xvcmpgesp_p: CompareOpc = 83; break;
7714 case Intrinsic::ppc_vsx_xvcmpgtsp_p: CompareOpc = 75; break;
7715 }
7716 isDot = 1;
7717 }
7718 else
7719 return false;
7720
7721 break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007722
Chris Lattner4211ca92006-04-14 06:01:58 +00007723 // Normal Comparisons.
7724 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
7725 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
7726 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
7727 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
7728 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007729 case Intrinsic::ppc_altivec_vcmpequd:
7730 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007731 CompareOpc = 199;
7732 isDot = 0;
7733 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007734 return false;
7735
7736 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007737 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
7738 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
7739 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
7740 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
7741 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007742 case Intrinsic::ppc_altivec_vcmpgtsd:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007743 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007744 CompareOpc = 967;
7745 isDot = 0;
7746 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007747 return false;
7748
7749 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007750 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
7751 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
7752 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007753 case Intrinsic::ppc_altivec_vcmpgtud:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007754 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007755 CompareOpc = 711;
7756 isDot = 0;
7757 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007758 return false;
7759
7760 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007761 }
Chris Lattner9754d142006-04-18 17:59:36 +00007762 return true;
7763}
7764
7765/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
7766/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007767SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007768 SelectionDAG &DAG) const {
Marcin Koscielnicki0cfb6122016-04-26 10:37:22 +00007769 unsigned IntrinsicID =
7770 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7771
7772 if (IntrinsicID == Intrinsic::thread_pointer) {
7773 // Reads the thread pointer register, used for __builtin_thread_pointer.
7774 bool is64bit = Subtarget.isPPC64();
7775 return DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
7776 is64bit ? MVT::i64 : MVT::i32);
7777 }
7778
Chris Lattner9754d142006-04-18 17:59:36 +00007779 // If this is a lowered altivec predicate compare, CompareOpc is set to the
7780 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00007781 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00007782 int CompareOpc;
7783 bool isDot;
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +00007784 if (!getVectorCompareInfo(Op, CompareOpc, isDot, Subtarget))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007785 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007786
Chris Lattner9754d142006-04-18 17:59:36 +00007787 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00007788 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00007789 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00007790 Op.getOperand(1), Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007791 DAG.getConstant(CompareOpc, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00007792 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00007793 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007794
Chris Lattner4211ca92006-04-14 06:01:58 +00007795 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007796 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007797 Op.getOperand(2), // LHS
7798 Op.getOperand(3), // RHS
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007799 DAG.getConstant(CompareOpc, dl, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007800 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00007801 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00007802 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007803
Chris Lattner4211ca92006-04-14 06:01:58 +00007804 // Now that we have the comparison, emit a copy from the CR to a GPR.
7805 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00007806 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00007807 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00007808 CompNode.getValue(1));
7809
Chris Lattner4211ca92006-04-14 06:01:58 +00007810 // Unpack the result based on how the target uses it.
7811 unsigned BitNo; // Bit # of CR6.
7812 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00007813 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00007814 default: // Can't happen, don't crash on invalid number though.
7815 case 0: // Return the value of the EQ bit of CR6.
7816 BitNo = 0; InvertBit = false;
7817 break;
7818 case 1: // Return the inverted value of the EQ bit of CR6.
7819 BitNo = 0; InvertBit = true;
7820 break;
7821 case 2: // Return the value of the LT bit of CR6.
7822 BitNo = 2; InvertBit = false;
7823 break;
7824 case 3: // Return the inverted value of the LT bit of CR6.
7825 BitNo = 2; InvertBit = true;
7826 break;
7827 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007828
Chris Lattner4211ca92006-04-14 06:01:58 +00007829 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00007830 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007831 DAG.getConstant(8 - (3 - BitNo), dl, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00007832 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00007833 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007834 DAG.getConstant(1, dl, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00007835
Chris Lattner4211ca92006-04-14 06:01:58 +00007836 // If we are supposed to, toggle the bit.
7837 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00007838 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007839 DAG.getConstant(1, dl, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00007840 return Flags;
7841}
7842
Hal Finkel5c0d1452014-03-30 13:22:59 +00007843SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
7844 SelectionDAG &DAG) const {
7845 SDLoc dl(Op);
7846 // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int
7847 // instructions), but for smaller types, we need to first extend up to v2i32
7848 // before doing going farther.
7849 if (Op.getValueType() == MVT::v2i64) {
7850 EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
7851 if (ExtVT != MVT::v2i32) {
7852 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0));
7853 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op,
7854 DAG.getValueType(EVT::getVectorVT(*DAG.getContext(),
7855 ExtVT.getVectorElementType(), 4)));
7856 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op);
7857 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op,
7858 DAG.getValueType(MVT::v2i32));
7859 }
7860
7861 return Op;
7862 }
7863
7864 return SDValue();
7865}
7866
Scott Michelcf0da6c2009-02-17 22:15:04 +00007867SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007868 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007869 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00007870 // Create a stack slot that is 16-byte aligned.
Matthias Braun941a7052016-07-28 18:40:00 +00007871 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
7872 int FrameIdx = MFI.CreateStackObject(16, 16, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00007873 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007874 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007875
Chris Lattner4211ca92006-04-14 06:01:58 +00007876 // Store the input value into Value#0 of the stack slot.
Justin Lebar9c375812016-07-15 18:27:10 +00007877 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
7878 MachinePointerInfo());
Chris Lattner4211ca92006-04-14 06:01:58 +00007879 // Load it out.
Justin Lebar9c375812016-07-15 18:27:10 +00007880 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo());
Chris Lattner4211ca92006-04-14 06:01:58 +00007881}
7882
Nemanja Ivanovicd5deb482016-09-14 14:19:09 +00007883SDValue PPCTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
7884 SelectionDAG &DAG) const {
7885 assert(Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
7886 "Should only be called for ISD::INSERT_VECTOR_ELT");
7887 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(2));
7888 // We have legal lowering for constant indices but not for variable ones.
7889 if (C)
7890 return Op;
7891 return SDValue();
7892}
7893
Hal Finkelc93a9a22015-02-25 01:06:45 +00007894SDValue PPCTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7895 SelectionDAG &DAG) const {
7896 SDLoc dl(Op);
7897 SDNode *N = Op.getNode();
7898
7899 assert(N->getOperand(0).getValueType() == MVT::v4i1 &&
7900 "Unknown extract_vector_elt type");
7901
7902 SDValue Value = N->getOperand(0);
7903
7904 // The first part of this is like the store lowering except that we don't
7905 // need to track the chain.
7906
7907 // The values are now known to be -1 (false) or 1 (true). To convert this
7908 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
7909 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
7910 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
7911
7912 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to
7913 // understand how to form the extending load.
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007914 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007915
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007916 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007917
7918 // Now convert to an integer and store.
7919 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007920 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32),
Hal Finkelc93a9a22015-02-25 01:06:45 +00007921 Value);
7922
Matthias Braun941a7052016-07-28 18:40:00 +00007923 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
7924 int FrameIdx = MFI.CreateStackObject(16, 16, false);
Alex Lorenze40c8a22015-08-11 23:09:45 +00007925 MachinePointerInfo PtrInfo =
7926 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Mehdi Amini44ede332015-07-09 02:09:04 +00007927 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007928 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
7929
7930 SDValue StoreChain = DAG.getEntryNode();
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007931 SDValue Ops[] = {StoreChain,
7932 DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32),
7933 Value, FIdx};
7934 SDVTList VTs = DAG.getVTList(/*chain*/ MVT::Other);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007935
7936 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID,
7937 dl, VTs, Ops, MVT::v4i32, PtrInfo);
7938
7939 // Extract the value requested.
7940 unsigned Offset = 4*cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007941 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007942 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
7943
Justin Lebar9c375812016-07-15 18:27:10 +00007944 SDValue IntVal =
7945 DAG.getLoad(MVT::i32, dl, StoreChain, Idx, PtrInfo.getWithOffset(Offset));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007946
7947 if (!Subtarget.useCRBits())
7948 return IntVal;
7949
7950 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, IntVal);
7951}
7952
7953/// Lowering for QPX v4i1 loads
7954SDValue PPCTargetLowering::LowerVectorLoad(SDValue Op,
7955 SelectionDAG &DAG) const {
7956 SDLoc dl(Op);
7957 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
7958 SDValue LoadChain = LN->getChain();
7959 SDValue BasePtr = LN->getBasePtr();
7960
7961 if (Op.getValueType() == MVT::v4f64 ||
7962 Op.getValueType() == MVT::v4f32) {
7963 EVT MemVT = LN->getMemoryVT();
7964 unsigned Alignment = LN->getAlignment();
7965
7966 // If this load is properly aligned, then it is legal.
7967 if (Alignment >= MemVT.getStoreSize())
7968 return Op;
7969
7970 EVT ScalarVT = Op.getValueType().getScalarType(),
7971 ScalarMemVT = MemVT.getScalarType();
7972 unsigned Stride = ScalarMemVT.getStoreSize();
7973
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007974 SDValue Vals[4], LoadChains[4];
Hal Finkelc93a9a22015-02-25 01:06:45 +00007975 for (unsigned Idx = 0; Idx < 4; ++Idx) {
7976 SDValue Load;
7977 if (ScalarVT != ScalarMemVT)
Justin Lebar9c375812016-07-15 18:27:10 +00007978 Load = DAG.getExtLoad(LN->getExtensionType(), dl, ScalarVT, LoadChain,
7979 BasePtr,
7980 LN->getPointerInfo().getWithOffset(Idx * Stride),
7981 ScalarMemVT, MinAlign(Alignment, Idx * Stride),
7982 LN->getMemOperand()->getFlags(), LN->getAAInfo());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007983 else
Justin Lebar9c375812016-07-15 18:27:10 +00007984 Load = DAG.getLoad(ScalarVT, dl, LoadChain, BasePtr,
7985 LN->getPointerInfo().getWithOffset(Idx * Stride),
7986 MinAlign(Alignment, Idx * Stride),
7987 LN->getMemOperand()->getFlags(), LN->getAAInfo());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007988
7989 if (Idx == 0 && LN->isIndexed()) {
7990 assert(LN->getAddressingMode() == ISD::PRE_INC &&
7991 "Unknown addressing mode on vector load");
7992 Load = DAG.getIndexedLoad(Load, dl, BasePtr, LN->getOffset(),
7993 LN->getAddressingMode());
7994 }
7995
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00007996 Vals[Idx] = Load;
7997 LoadChains[Idx] = Load.getValue(1);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007998
7999 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008000 DAG.getConstant(Stride, dl,
8001 BasePtr.getValueType()));
Hal Finkelc93a9a22015-02-25 01:06:45 +00008002 }
8003
8004 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00008005 SDValue Value = DAG.getBuildVector(Op.getValueType(), dl, Vals);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008006
8007 if (LN->isIndexed()) {
8008 SDValue RetOps[] = { Value, Vals[0].getValue(1), TF };
8009 return DAG.getMergeValues(RetOps, dl);
8010 }
8011
8012 SDValue RetOps[] = { Value, TF };
8013 return DAG.getMergeValues(RetOps, dl);
8014 }
8015
8016 assert(Op.getValueType() == MVT::v4i1 && "Unknown load to lower");
8017 assert(LN->isUnindexed() && "Indexed v4i1 loads are not supported");
8018
8019 // To lower v4i1 from a byte array, we load the byte elements of the
8020 // vector and then reuse the BUILD_VECTOR logic.
8021
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008022 SDValue VectElmts[4], VectElmtChains[4];
Hal Finkelc93a9a22015-02-25 01:06:45 +00008023 for (unsigned i = 0; i < 4; ++i) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008024 SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008025 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx);
8026
Justin Lebar9c375812016-07-15 18:27:10 +00008027 VectElmts[i] = DAG.getExtLoad(
8028 ISD::EXTLOAD, dl, MVT::i32, LoadChain, Idx,
8029 LN->getPointerInfo().getWithOffset(i), MVT::i8,
8030 /* Alignment = */ 1, LN->getMemOperand()->getFlags(), LN->getAAInfo());
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008031 VectElmtChains[i] = VectElmts[i].getValue(1);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008032 }
8033
8034 LoadChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, VectElmtChains);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00008035 SDValue Value = DAG.getBuildVector(MVT::v4i1, dl, VectElmts);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008036
8037 SDValue RVals[] = { Value, LoadChain };
8038 return DAG.getMergeValues(RVals, dl);
8039}
8040
8041/// Lowering for QPX v4i1 stores
8042SDValue PPCTargetLowering::LowerVectorStore(SDValue Op,
8043 SelectionDAG &DAG) const {
8044 SDLoc dl(Op);
8045 StoreSDNode *SN = cast<StoreSDNode>(Op.getNode());
8046 SDValue StoreChain = SN->getChain();
8047 SDValue BasePtr = SN->getBasePtr();
8048 SDValue Value = SN->getValue();
8049
8050 if (Value.getValueType() == MVT::v4f64 ||
8051 Value.getValueType() == MVT::v4f32) {
8052 EVT MemVT = SN->getMemoryVT();
8053 unsigned Alignment = SN->getAlignment();
8054
8055 // If this store is properly aligned, then it is legal.
8056 if (Alignment >= MemVT.getStoreSize())
8057 return Op;
8058
8059 EVT ScalarVT = Value.getValueType().getScalarType(),
8060 ScalarMemVT = MemVT.getScalarType();
8061 unsigned Stride = ScalarMemVT.getStoreSize();
8062
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008063 SDValue Stores[4];
Hal Finkelc93a9a22015-02-25 01:06:45 +00008064 for (unsigned Idx = 0; Idx < 4; ++Idx) {
Mehdi Amini44ede332015-07-09 02:09:04 +00008065 SDValue Ex = DAG.getNode(
8066 ISD::EXTRACT_VECTOR_ELT, dl, ScalarVT, Value,
8067 DAG.getConstant(Idx, dl, getVectorIdxTy(DAG.getDataLayout())));
Hal Finkelc93a9a22015-02-25 01:06:45 +00008068 SDValue Store;
8069 if (ScalarVT != ScalarMemVT)
8070 Store =
Justin Lebar9c375812016-07-15 18:27:10 +00008071 DAG.getTruncStore(StoreChain, dl, Ex, BasePtr,
8072 SN->getPointerInfo().getWithOffset(Idx * Stride),
8073 ScalarMemVT, MinAlign(Alignment, Idx * Stride),
8074 SN->getMemOperand()->getFlags(), SN->getAAInfo());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008075 else
Justin Lebar9c375812016-07-15 18:27:10 +00008076 Store = DAG.getStore(StoreChain, dl, Ex, BasePtr,
8077 SN->getPointerInfo().getWithOffset(Idx * Stride),
8078 MinAlign(Alignment, Idx * Stride),
8079 SN->getMemOperand()->getFlags(), SN->getAAInfo());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008080
8081 if (Idx == 0 && SN->isIndexed()) {
8082 assert(SN->getAddressingMode() == ISD::PRE_INC &&
8083 "Unknown addressing mode on vector store");
8084 Store = DAG.getIndexedStore(Store, dl, BasePtr, SN->getOffset(),
8085 SN->getAddressingMode());
8086 }
8087
8088 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008089 DAG.getConstant(Stride, dl,
8090 BasePtr.getValueType()));
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008091 Stores[Idx] = Store;
Hal Finkelc93a9a22015-02-25 01:06:45 +00008092 }
8093
8094 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
8095
8096 if (SN->isIndexed()) {
8097 SDValue RetOps[] = { TF, Stores[0].getValue(1) };
8098 return DAG.getMergeValues(RetOps, dl);
8099 }
8100
8101 return TF;
8102 }
8103
8104 assert(SN->isUnindexed() && "Indexed v4i1 stores are not supported");
8105 assert(Value.getValueType() == MVT::v4i1 && "Unknown store to lower");
8106
8107 // The values are now known to be -1 (false) or 1 (true). To convert this
8108 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
8109 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
8110 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
8111
8112 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to
8113 // understand how to form the extending load.
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00008114 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008115
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00008116 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008117
8118 // Now convert to an integer and store.
8119 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008120 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32),
Hal Finkelc93a9a22015-02-25 01:06:45 +00008121 Value);
8122
Matthias Braun941a7052016-07-28 18:40:00 +00008123 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
8124 int FrameIdx = MFI.CreateStackObject(16, 16, false);
Alex Lorenze40c8a22015-08-11 23:09:45 +00008125 MachinePointerInfo PtrInfo =
8126 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Mehdi Amini44ede332015-07-09 02:09:04 +00008127 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008128 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
8129
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008130 SDValue Ops[] = {StoreChain,
8131 DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32),
8132 Value, FIdx};
8133 SDVTList VTs = DAG.getVTList(/*chain*/ MVT::Other);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008134
8135 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID,
8136 dl, VTs, Ops, MVT::v4i32, PtrInfo);
8137
8138 // Move data into the byte array.
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008139 SDValue Loads[4], LoadChains[4];
Hal Finkelc93a9a22015-02-25 01:06:45 +00008140 for (unsigned i = 0; i < 4; ++i) {
8141 unsigned Offset = 4*i;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008142 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008143 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
8144
Justin Lebar9c375812016-07-15 18:27:10 +00008145 Loads[i] = DAG.getLoad(MVT::i32, dl, StoreChain, Idx,
8146 PtrInfo.getWithOffset(Offset));
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008147 LoadChains[i] = Loads[i].getValue(1);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008148 }
8149
8150 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8151
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008152 SDValue Stores[4];
Hal Finkelc93a9a22015-02-25 01:06:45 +00008153 for (unsigned i = 0; i < 4; ++i) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008154 SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008155 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx);
8156
Benjamin Kramer1d67ac52016-06-17 13:15:10 +00008157 Stores[i] = DAG.getTruncStore(
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00008158 StoreChain, dl, Loads[i], Idx, SN->getPointerInfo().getWithOffset(i),
Justin Lebar9c375812016-07-15 18:27:10 +00008159 MVT::i8, /* Alignment = */ 1, SN->getMemOperand()->getFlags(),
8160 SN->getAAInfo());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008161 }
8162
8163 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
8164
8165 return StoreChain;
8166}
8167
Dan Gohman21cea8a2010-04-17 15:26:15 +00008168SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008169 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00008170 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008171 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008172
Owen Anderson9f944592009-08-11 20:47:22 +00008173 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
8174 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008175
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008176 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00008177 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008178
Chris Lattner7e4398742006-04-18 03:43:48 +00008179 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00008180 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
8181 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
8182 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008183
Chris Lattner7e4398742006-04-18 03:43:48 +00008184 // Low parts multiplied together, generating 32-bit results (we ignore the
8185 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008186 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00008187 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008188
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008189 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00008190 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00008191 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008192 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00008193 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00008194 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
8195 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008196 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008197
Owen Anderson9f944592009-08-11 20:47:22 +00008198 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00008199
Chris Lattner96d50482006-04-18 04:28:57 +00008200 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00008201 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00008202 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008203 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008204 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00008205
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00008206 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008207 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00008208 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00008209 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008210
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00008211 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008212 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00008213 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00008214 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008215
Bill Schmidt42995e82014-06-09 16:06:29 +00008216 // Merge the results together. Because vmuleub and vmuloub are
8217 // instructions with a big-endian bias, we must reverse the
8218 // element numbering and reverse the meaning of "odd" and "even"
8219 // when generating little endian code.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00008220 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00008221 for (unsigned i = 0; i != 8; ++i) {
Bill Schmidt42995e82014-06-09 16:06:29 +00008222 if (isLittleEndian) {
8223 Ops[i*2 ] = 2*i;
8224 Ops[i*2+1] = 2*i+16;
8225 } else {
8226 Ops[i*2 ] = 2*i+1;
8227 Ops[i*2+1] = 2*i+1+16;
8228 }
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00008229 }
Bill Schmidt42995e82014-06-09 16:06:29 +00008230 if (isLittleEndian)
8231 return DAG.getVectorShuffle(MVT::v16i8, dl, OddParts, EvenParts, Ops);
8232 else
8233 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00008234 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008235 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00008236 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00008237}
8238
Chris Lattnerf3d06c62005-08-26 00:52:45 +00008239/// LowerOperation - Provide custom lowering hooks for some operations.
8240///
Dan Gohman21cea8a2010-04-17 15:26:15 +00008241SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00008242 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008243 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00008244 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00008245 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00008246 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00008247 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00008248 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00008249 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00008250 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
8251 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008252 case ISD::VASTART:
Eric Christopherb976a392016-07-07 00:39:27 +00008253 return LowerVASTART(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008254
8255 case ISD::VAARG:
Eric Christopherb976a392016-07-07 00:39:27 +00008256 return LowerVAARG(Op, DAG);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00008257
Roman Divackyc3825df2013-07-25 21:36:47 +00008258 case ISD::VACOPY:
Eric Christopherb976a392016-07-07 00:39:27 +00008259 return LowerVACOPY(Op, DAG);
Roman Divackyc3825df2013-07-25 21:36:47 +00008260
Eric Christopherb976a392016-07-07 00:39:27 +00008261 case ISD::STACKRESTORE:
8262 return LowerSTACKRESTORE(Op, DAG);
8263
Chris Lattner43df5b32007-02-25 05:34:32 +00008264 case ISD::DYNAMIC_STACKALLOC:
Eric Christopherb976a392016-07-07 00:39:27 +00008265 return LowerDYNAMIC_STACKALLOC(Op, DAG);
8266
8267 case ISD::GET_DYNAMIC_AREA_OFFSET:
8268 return LowerGET_DYNAMIC_AREA_OFFSET(Op, DAG);
Evan Cheng51096af2008-04-19 01:30:48 +00008269
Hal Finkel5081ac22016-09-01 10:28:47 +00008270 case ISD::EH_DWARF_CFA:
8271 return LowerEH_DWARF_CFA(Op, DAG);
8272
Hal Finkel756810f2013-03-21 21:37:52 +00008273 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
8274 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
8275
Hal Finkel940ab932014-02-28 00:27:01 +00008276 case ISD::LOAD: return LowerLOAD(Op, DAG);
8277 case ISD::STORE: return LowerSTORE(Op, DAG);
8278 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00008279 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00008280 case ISD::FP_TO_UINT:
8281 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Hal Finkeled844c42015-01-06 22:31:02 +00008282 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00008283 case ISD::UINT_TO_FP:
8284 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00008285 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00008286
Chris Lattner4211ca92006-04-14 06:01:58 +00008287 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00008288 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
8289 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
8290 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00008291
Chris Lattner4211ca92006-04-14 06:01:58 +00008292 // Vector-related lowering.
8293 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
8294 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8295 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
8296 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Hal Finkel5c0d1452014-03-30 13:22:59 +00008297 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008298 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Nemanja Ivanovicd5deb482016-09-14 14:19:09 +00008299 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00008300 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008301
Hal Finkel25c19922013-05-15 21:37:41 +00008302 // For counter-based loop handling.
8303 case ISD::INTRINSIC_W_CHAIN: return SDValue();
8304
Chris Lattnerf6a81562007-12-08 06:59:59 +00008305 // Frame & Return address.
8306 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008307 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00008308 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00008309}
8310
Duncan Sands6ed40142008-12-01 11:39:25 +00008311void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
8312 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00008313 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008314 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00008315 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00008316 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008317 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkelbbdee932014-12-02 22:01:00 +00008318 case ISD::READCYCLECOUNTER: {
8319 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
8320 SDValue RTB = DAG.getNode(PPCISD::READ_TIME_BASE, dl, VTs, N->getOperand(0));
8321
8322 Results.push_back(RTB);
8323 Results.push_back(RTB.getValue(1));
8324 Results.push_back(RTB.getValue(2));
8325 break;
8326 }
Hal Finkel25c19922013-05-15 21:37:41 +00008327 case ISD::INTRINSIC_W_CHAIN: {
8328 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
8329 Intrinsic::ppc_is_decremented_ctr_nonzero)
8330 break;
8331
8332 assert(N->getValueType(0) == MVT::i1 &&
8333 "Unexpected result type for CTR decrement intrinsic");
Mehdi Amini44ede332015-07-09 02:09:04 +00008334 EVT SVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
8335 N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00008336 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
8337 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00008338 N->getOperand(1));
Hal Finkel25c19922013-05-15 21:37:41 +00008339
8340 Results.push_back(NewInt);
8341 Results.push_back(NewInt.getValue(1));
8342 break;
8343 }
Roman Divacky4394e682011-06-28 15:30:42 +00008344 case ISD::VAARG: {
Eric Christophercccae792015-01-30 22:02:31 +00008345 if (!Subtarget.isSVR4ABI() || Subtarget.isPPC64())
Roman Divacky4394e682011-06-28 15:30:42 +00008346 return;
8347
8348 EVT VT = N->getValueType(0);
8349
8350 if (VT == MVT::i64) {
Eric Christopherb976a392016-07-07 00:39:27 +00008351 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG);
Roman Divacky4394e682011-06-28 15:30:42 +00008352
8353 Results.push_back(NewNode);
8354 Results.push_back(NewNode.getValue(1));
8355 }
8356 return;
8357 }
Duncan Sands6ed40142008-12-01 11:39:25 +00008358 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00008359 assert(N->getValueType(0) == MVT::ppcf128);
8360 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008361 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00008362 MVT::f64, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008363 DAG.getIntPtrConstant(0, dl));
Dale Johannesenf80493b2009-02-05 22:07:54 +00008364 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00008365 MVT::f64, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008366 DAG.getIntPtrConstant(1, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00008367
Ulrich Weigand874fc622013-03-26 10:56:22 +00008368 // Add the two halves of the long double in round-to-zero mode.
8369 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00008370
8371 // We know the low half is about to be thrown away, so just use something
8372 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00008373 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00008374 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00008375 return;
Duncan Sands2a287912008-07-19 16:26:02 +00008376 }
Duncan Sands6ed40142008-12-01 11:39:25 +00008377 case ISD::FP_TO_SINT:
Hal Finkel93138502015-04-10 03:39:00 +00008378 case ISD::FP_TO_UINT:
Bill Schmidt41221692013-07-09 18:50:20 +00008379 // LowerFP_TO_INT() can only handle f32 and f64.
8380 if (N->getOperand(0).getValueType() == MVT::ppcf128)
8381 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00008382 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00008383 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00008384 }
8385}
8386
Chris Lattner4211ca92006-04-14 06:01:58 +00008387//===----------------------------------------------------------------------===//
8388// Other Lowering Code
8389//===----------------------------------------------------------------------===//
8390
Robin Morisset22129962014-09-23 20:46:49 +00008391static Instruction* callIntrinsic(IRBuilder<> &Builder, Intrinsic::ID Id) {
8392 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
8393 Function *Func = Intrinsic::getDeclaration(M, Id);
David Blaikieff6409d2015-05-18 22:13:54 +00008394 return Builder.CreateCall(Func, {});
Robin Morisset22129962014-09-23 20:46:49 +00008395}
8396
8397// The mappings for emitLeading/TrailingFence is taken from
8398// http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
8399Instruction* PPCTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
8400 AtomicOrdering Ord, bool IsStore,
8401 bool IsLoad) const {
JF Bastien800f87a2016-04-06 21:19:33 +00008402 if (Ord == AtomicOrdering::SequentiallyConsistent)
Robin Morisset22129962014-09-23 20:46:49 +00008403 return callIntrinsic(Builder, Intrinsic::ppc_sync);
JF Bastien800f87a2016-04-06 21:19:33 +00008404 if (isReleaseOrStronger(Ord))
Robin Morisset22129962014-09-23 20:46:49 +00008405 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
David Blaikieff6409d2015-05-18 22:13:54 +00008406 return nullptr;
Robin Morisset22129962014-09-23 20:46:49 +00008407}
8408
8409Instruction* PPCTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
8410 AtomicOrdering Ord, bool IsStore,
8411 bool IsLoad) const {
JF Bastien800f87a2016-04-06 21:19:33 +00008412 if (IsLoad && isAcquireOrStronger(Ord))
Robin Morisset22129962014-09-23 20:46:49 +00008413 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
8414 // FIXME: this is too conservative, a dependent branch + isync is enough.
8415 // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html and
8416 // http://www.rdrop.com/users/paulmck/scalability/paper/N2745r.2011.03.04a.html
8417 // and http://www.cl.cam.ac.uk/~pes20/cppppc/ for justification.
David Blaikieff6409d2015-05-18 22:13:54 +00008418 return nullptr;
Robin Morisset22129962014-09-23 20:46:49 +00008419}
8420
Chris Lattner9b577f12005-08-26 21:23:58 +00008421MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008422PPCTargetLowering::EmitAtomicBinary(MachineInstr &MI, MachineBasicBlock *BB,
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008423 unsigned AtomicSize,
Hal Finkel57282002016-08-28 16:17:58 +00008424 unsigned BinOpcode,
8425 unsigned CmpOpcode,
8426 unsigned CmpPred) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008427 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christophercccae792015-01-30 22:02:31 +00008428 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Dale Johannesend4eb0522008-08-25 22:34:37 +00008429
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008430 auto LoadMnemonic = PPC::LDARX;
8431 auto StoreMnemonic = PPC::STDCX;
8432 switch (AtomicSize) {
8433 default:
8434 llvm_unreachable("Unexpected size of atomic entity");
8435 case 1:
8436 LoadMnemonic = PPC::LBARX;
8437 StoreMnemonic = PPC::STBCX;
8438 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4");
8439 break;
8440 case 2:
8441 LoadMnemonic = PPC::LHARX;
8442 StoreMnemonic = PPC::STHCX;
8443 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4");
8444 break;
8445 case 4:
8446 LoadMnemonic = PPC::LWARX;
8447 StoreMnemonic = PPC::STWCX;
8448 break;
8449 case 8:
8450 LoadMnemonic = PPC::LDARX;
8451 StoreMnemonic = PPC::STDCX;
8452 break;
8453 }
8454
Dale Johannesend4eb0522008-08-25 22:34:37 +00008455 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8456 MachineFunction *F = BB->getParent();
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00008457 MachineFunction::iterator It = ++BB->getIterator();
Dale Johannesend4eb0522008-08-25 22:34:37 +00008458
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008459 unsigned dest = MI.getOperand(0).getReg();
8460 unsigned ptrA = MI.getOperand(1).getReg();
8461 unsigned ptrB = MI.getOperand(2).getReg();
8462 unsigned incr = MI.getOperand(3).getReg();
8463 DebugLoc dl = MI.getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00008464
8465 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
Hal Finkel57282002016-08-28 16:17:58 +00008466 MachineBasicBlock *loop2MBB =
8467 CmpOpcode ? F->CreateMachineBasicBlock(LLVM_BB) : nullptr;
Dale Johannesend4eb0522008-08-25 22:34:37 +00008468 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
8469 F->insert(It, loopMBB);
Hal Finkel57282002016-08-28 16:17:58 +00008470 if (CmpOpcode)
8471 F->insert(It, loop2MBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008472 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008473 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008474 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008475 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008476
8477 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008478 unsigned TmpReg = (!BinOpcode) ? incr :
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008479 RegInfo.createVirtualRegister( AtomicSize == 8 ? &PPC::G8RCRegClass
Craig Topper61e88f42014-11-21 05:58:21 +00008480 : &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008481
8482 // thisMBB:
8483 // ...
8484 // fallthrough --> loopMBB
8485 BB->addSuccessor(loopMBB);
8486
8487 // loopMBB:
8488 // l[wd]arx dest, ptr
8489 // add r0, dest, incr
8490 // st[wd]cx. r0, ptr
8491 // bne- loopMBB
8492 // fallthrough --> exitMBB
Hal Finkel57282002016-08-28 16:17:58 +00008493
8494 // For max/min...
8495 // loopMBB:
8496 // l[wd]arx dest, ptr
8497 // cmpl?[wd] incr, dest
8498 // bgt exitMBB
8499 // loop2MBB:
8500 // st[wd]cx. dest, ptr
8501 // bne- loopMBB
8502 // fallthrough --> exitMBB
8503
Dale Johannesend4eb0522008-08-25 22:34:37 +00008504 BB = loopMBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008505 BuildMI(BB, dl, TII->get(LoadMnemonic), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00008506 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008507 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008508 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
Hal Finkel57282002016-08-28 16:17:58 +00008509 if (CmpOpcode) {
8510 BuildMI(BB, dl, TII->get(CmpOpcode), PPC::CR0)
8511 .addReg(incr).addReg(dest);
8512 BuildMI(BB, dl, TII->get(PPC::BCC))
8513 .addImm(CmpPred).addReg(PPC::CR0).addMBB(exitMBB);
8514 BB->addSuccessor(loop2MBB);
8515 BB->addSuccessor(exitMBB);
8516 BB = loop2MBB;
8517 }
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008518 BuildMI(BB, dl, TII->get(StoreMnemonic))
Dale Johannesend4eb0522008-08-25 22:34:37 +00008519 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008520 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00008521 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008522 BB->addSuccessor(loopMBB);
8523 BB->addSuccessor(exitMBB);
8524
8525 // exitMBB:
8526 // ...
8527 BB = exitMBB;
8528 return BB;
8529}
8530
8531MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008532PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr &MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00008533 MachineBasicBlock *BB,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008534 bool is8bit, // operation
Hal Finkel57282002016-08-28 16:17:58 +00008535 unsigned BinOpcode,
8536 unsigned CmpOpcode,
8537 unsigned CmpPred) const {
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008538 // If we support part-word atomic mnemonics, just use them
8539 if (Subtarget.hasPartwordAtomics())
Hal Finkel57282002016-08-28 16:17:58 +00008540 return EmitAtomicBinary(MI, BB, is8bit ? 1 : 2, BinOpcode,
8541 CmpOpcode, CmpPred);
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008542
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008543 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christophercccae792015-01-30 22:02:31 +00008544 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Dale Johannesena32affb2008-08-28 17:53:09 +00008545 // In 64 bit mode we have to use 64 bits for addresses, even though the
8546 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
8547 // registers without caring whether they're 32 or 64, but here we're
8548 // doing actual arithmetic on the addresses.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008549 bool is64bit = Subtarget.isPPC64();
Hal Finkel3d70a9d2016-08-29 22:25:36 +00008550 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelf70c41e2013-03-21 23:45:03 +00008551 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00008552
8553 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8554 MachineFunction *F = BB->getParent();
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00008555 MachineFunction::iterator It = ++BB->getIterator();
Dale Johannesena32affb2008-08-28 17:53:09 +00008556
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008557 unsigned dest = MI.getOperand(0).getReg();
8558 unsigned ptrA = MI.getOperand(1).getReg();
8559 unsigned ptrB = MI.getOperand(2).getReg();
8560 unsigned incr = MI.getOperand(3).getReg();
8561 DebugLoc dl = MI.getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00008562
8563 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
Hal Finkel57282002016-08-28 16:17:58 +00008564 MachineBasicBlock *loop2MBB =
8565 CmpOpcode ? F->CreateMachineBasicBlock(LLVM_BB) : nullptr;
Dale Johannesena32affb2008-08-28 17:53:09 +00008566 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
8567 F->insert(It, loopMBB);
Hal Finkel57282002016-08-28 16:17:58 +00008568 if (CmpOpcode)
8569 F->insert(It, loop2MBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00008570 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008571 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008572 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008573 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00008574
8575 MachineRegisterInfo &RegInfo = F->getRegInfo();
Craig Topper61e88f42014-11-21 05:58:21 +00008576 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass
8577 : &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00008578 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
8579 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
Hal Finkel3d70a9d2016-08-29 22:25:36 +00008580 unsigned ShiftReg =
8581 isLittleEndian ? Shift1Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00008582 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
8583 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
8584 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
8585 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
8586 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
8587 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
8588 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008589 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00008590 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008591 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00008592
8593 // thisMBB:
8594 // ...
8595 // fallthrough --> loopMBB
8596 BB->addSuccessor(loopMBB);
8597
8598 // The 4-byte load must be aligned, while a char or short may be
8599 // anywhere in the word. Hence all this nasty bookkeeping code.
8600 // add ptr1, ptrA, ptrB [copy if ptrA==0]
8601 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00008602 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00008603 // rlwinm ptr, ptr1, 0, 0, 29
8604 // slw incr2, incr, shift
8605 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
8606 // slw mask, mask2, shift
8607 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00008608 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008609 // add tmp, tmpDest, incr2
8610 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00008611 // and tmp3, tmp, mask
8612 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00008613 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00008614 // bne- loopMBB
8615 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008616 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008617 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00008618 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008619 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008620 .addReg(ptrA).addReg(ptrB);
8621 } else {
8622 Ptr1Reg = ptrB;
8623 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008624 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008625 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Hal Finkel3d70a9d2016-08-29 22:25:36 +00008626 if (!isLittleEndian)
8627 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
8628 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
Dale Johannesena32affb2008-08-28 17:53:09 +00008629 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008630 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008631 .addReg(Ptr1Reg).addImm(0).addImm(61);
8632 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00008633 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008634 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008635 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008636 .addReg(incr).addReg(ShiftReg);
8637 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008638 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00008639 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00008640 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
8641 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00008642 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008643 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008644 .addReg(Mask2Reg).addReg(ShiftReg);
8645
8646 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00008647 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008648 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008649 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008650 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008651 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008652 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008653 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008654 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008655 .addReg(TmpReg).addReg(MaskReg);
Hal Finkel57282002016-08-28 16:17:58 +00008656 if (CmpOpcode) {
8657 // For unsigned comparisons, we can directly compare the shifted values.
8658 // For signed comparisons we shift and sign extend.
8659 unsigned SReg = RegInfo.createVirtualRegister(RC);
8660 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), SReg)
8661 .addReg(TmpDestReg).addReg(MaskReg);
8662 unsigned ValueReg = SReg;
8663 unsigned CmpReg = Incr2Reg;
8664 if (CmpOpcode == PPC::CMPW) {
8665 ValueReg = RegInfo.createVirtualRegister(RC);
8666 BuildMI(BB, dl, TII->get(PPC::SRW), ValueReg)
8667 .addReg(SReg).addReg(ShiftReg);
8668 unsigned ValueSReg = RegInfo.createVirtualRegister(RC);
8669 BuildMI(BB, dl, TII->get(is8bit ? PPC::EXTSB : PPC::EXTSH), ValueSReg)
8670 .addReg(ValueReg);
8671 ValueReg = ValueSReg;
8672 CmpReg = incr;
8673 }
8674 BuildMI(BB, dl, TII->get(CmpOpcode), PPC::CR0)
8675 .addReg(CmpReg).addReg(ValueReg);
8676 BuildMI(BB, dl, TII->get(PPC::BCC))
8677 .addImm(CmpPred).addReg(PPC::CR0).addMBB(exitMBB);
8678 BB->addSuccessor(loop2MBB);
8679 BB->addSuccessor(exitMBB);
8680 BB = loop2MBB;
8681 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008682 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008683 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00008684 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008685 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008686 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00008687 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00008688 BB->addSuccessor(loopMBB);
8689 BB->addSuccessor(exitMBB);
8690
8691 // exitMBB:
8692 // ...
8693 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00008694 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
8695 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00008696 return BB;
8697}
8698
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008699llvm::MachineBasicBlock *
8700PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr &MI,
Hal Finkel756810f2013-03-21 21:37:52 +00008701 MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008702 DebugLoc DL = MI.getDebugLoc();
Eric Christophercccae792015-01-30 22:02:31 +00008703 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00008704
8705 MachineFunction *MF = MBB->getParent();
8706 MachineRegisterInfo &MRI = MF->getRegInfo();
8707
8708 const BasicBlock *BB = MBB->getBasicBlock();
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00008709 MachineFunction::iterator I = ++MBB->getIterator();
Hal Finkel756810f2013-03-21 21:37:52 +00008710
8711 // Memory Reference
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008712 MachineInstr::mmo_iterator MMOBegin = MI.memoperands_begin();
8713 MachineInstr::mmo_iterator MMOEnd = MI.memoperands_end();
Hal Finkel756810f2013-03-21 21:37:52 +00008714
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008715 unsigned DstReg = MI.getOperand(0).getReg();
Hal Finkel756810f2013-03-21 21:37:52 +00008716 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
8717 assert(RC->hasType(MVT::i32) && "Invalid destination!");
8718 unsigned mainDstReg = MRI.createVirtualRegister(RC);
8719 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
8720
Mehdi Amini44ede332015-07-09 02:09:04 +00008721 MVT PVT = getPointerTy(MF->getDataLayout());
Hal Finkel756810f2013-03-21 21:37:52 +00008722 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
8723 "Invalid Pointer Size!");
8724 // For v = setjmp(buf), we generate
8725 //
8726 // thisMBB:
8727 // SjLjSetup mainMBB
8728 // bl mainMBB
8729 // v_restore = 1
8730 // b sinkMBB
8731 //
8732 // mainMBB:
8733 // buf[LabelOffset] = LR
8734 // v_main = 0
8735 //
8736 // sinkMBB:
8737 // v = phi(main, restore)
8738 //
8739
8740 MachineBasicBlock *thisMBB = MBB;
8741 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
8742 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
8743 MF->insert(I, mainMBB);
8744 MF->insert(I, sinkMBB);
8745
8746 MachineInstrBuilder MIB;
8747
8748 // Transfer the remainder of BB and its successor edges to sinkMBB.
8749 sinkMBB->splice(sinkMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008750 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Hal Finkel756810f2013-03-21 21:37:52 +00008751 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
8752
8753 // Note that the structure of the jmp_buf used here is not compatible
8754 // with that used by libc, and is not designed to be. Specifically, it
8755 // stores only those 'reserved' registers that LLVM does not otherwise
8756 // understand how to spill. Also, by convention, by the time this
8757 // intrinsic is called, Clang has already stored the frame address in the
8758 // first slot of the buffer and stack address in the third. Following the
8759 // X86 target code, we'll store the jump address in the second slot. We also
8760 // need to save the TOC pointer (R2) to handle jumps between shared
8761 // libraries, and that will be stored in the fourth slot. The thread
8762 // identifier (R13) is not affected.
8763
8764 // thisMBB:
8765 const int64_t LabelOffset = 1 * PVT.getStoreSize();
8766 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00008767 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00008768
8769 // Prepare IP either in reg.
8770 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
8771 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008772 unsigned BufReg = MI.getOperand(1).getReg();
Hal Finkel756810f2013-03-21 21:37:52 +00008773
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008774 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI()) {
Hal Finkele6698d52015-02-01 15:03:28 +00008775 setUsesTOCBasePtr(*MBB->getParent());
Hal Finkel756810f2013-03-21 21:37:52 +00008776 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
8777 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008778 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008779 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00008780 MIB.setMemRefs(MMOBegin, MMOEnd);
8781 }
8782
Hal Finkelf05d6c72013-07-17 23:50:51 +00008783 // Naked functions never have a base pointer, and so we use r1. For all
8784 // other functions, this decision must be delayed until during PEI.
8785 unsigned BaseReg;
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +00008786 if (MF->getFunction()->hasFnAttribute(Attribute::Naked))
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008787 BaseReg = Subtarget.isPPC64() ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00008788 else
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008789 BaseReg = Subtarget.isPPC64() ? PPC::BP8 : PPC::BP;
Hal Finkelf05d6c72013-07-17 23:50:51 +00008790
8791 MIB = BuildMI(*thisMBB, MI, DL,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008792 TII->get(Subtarget.isPPC64() ? PPC::STD : PPC::STW))
Eric Christophercccae792015-01-30 22:02:31 +00008793 .addReg(BaseReg)
8794 .addImm(BPOffset)
8795 .addReg(BufReg);
Hal Finkelf05d6c72013-07-17 23:50:51 +00008796 MIB.setMemRefs(MMOBegin, MMOEnd);
8797
Hal Finkel756810f2013-03-21 21:37:52 +00008798 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00008799 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Eric Christophercccae792015-01-30 22:02:31 +00008800 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo();
Bill Wendling5e7656b2013-06-07 07:55:53 +00008801 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00008802
8803 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
8804
8805 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
8806 .addMBB(mainMBB);
8807 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
8808
Cong Hou1938f2e2015-11-24 08:51:23 +00008809 thisMBB->addSuccessor(mainMBB, BranchProbability::getZero());
8810 thisMBB->addSuccessor(sinkMBB, BranchProbability::getOne());
Hal Finkel756810f2013-03-21 21:37:52 +00008811
8812 // mainMBB:
8813 // mainDstReg = 0
Eric Christophercccae792015-01-30 22:02:31 +00008814 MIB =
8815 BuildMI(mainMBB, DL,
8816 TII->get(Subtarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
Hal Finkel756810f2013-03-21 21:37:52 +00008817
8818 // Store IP
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008819 if (Subtarget.isPPC64()) {
Hal Finkel756810f2013-03-21 21:37:52 +00008820 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
8821 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008822 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008823 .addReg(BufReg);
8824 } else {
8825 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
8826 .addReg(LabelReg)
8827 .addImm(LabelOffset)
8828 .addReg(BufReg);
8829 }
8830
8831 MIB.setMemRefs(MMOBegin, MMOEnd);
8832
8833 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
8834 mainMBB->addSuccessor(sinkMBB);
8835
8836 // sinkMBB:
8837 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
8838 TII->get(PPC::PHI), DstReg)
8839 .addReg(mainDstReg).addMBB(mainMBB)
8840 .addReg(restoreDstReg).addMBB(thisMBB);
8841
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008842 MI.eraseFromParent();
Hal Finkel756810f2013-03-21 21:37:52 +00008843 return sinkMBB;
8844}
8845
8846MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008847PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr &MI,
Hal Finkel756810f2013-03-21 21:37:52 +00008848 MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008849 DebugLoc DL = MI.getDebugLoc();
Eric Christophercccae792015-01-30 22:02:31 +00008850 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00008851
8852 MachineFunction *MF = MBB->getParent();
8853 MachineRegisterInfo &MRI = MF->getRegInfo();
8854
8855 // Memory Reference
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008856 MachineInstr::mmo_iterator MMOBegin = MI.memoperands_begin();
8857 MachineInstr::mmo_iterator MMOEnd = MI.memoperands_end();
Hal Finkel756810f2013-03-21 21:37:52 +00008858
Mehdi Amini44ede332015-07-09 02:09:04 +00008859 MVT PVT = getPointerTy(MF->getDataLayout());
Hal Finkel756810f2013-03-21 21:37:52 +00008860 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
8861 "Invalid Pointer Size!");
8862
8863 const TargetRegisterClass *RC =
8864 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
8865 unsigned Tmp = MRI.createVirtualRegister(RC);
8866 // Since FP is only updated here but NOT referenced, it's treated as GPR.
8867 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
8868 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Eric Christophercccae792015-01-30 22:02:31 +00008869 unsigned BP =
8870 (PVT == MVT::i64)
8871 ? PPC::X30
Rafael Espindola21d22a02016-06-27 14:05:43 +00008872 : (Subtarget.isSVR4ABI() && isPositionIndependent() ? PPC::R29
8873 : PPC::R30);
Hal Finkel756810f2013-03-21 21:37:52 +00008874
8875 MachineInstrBuilder MIB;
8876
8877 const int64_t LabelOffset = 1 * PVT.getStoreSize();
8878 const int64_t SPOffset = 2 * PVT.getStoreSize();
8879 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00008880 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00008881
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008882 unsigned BufReg = MI.getOperand(0).getReg();
Hal Finkel756810f2013-03-21 21:37:52 +00008883
8884 // Reload FP (the jumped-to function may not have had a
8885 // frame pointer, and if so, then its r31 will be restored
8886 // as necessary).
8887 if (PVT == MVT::i64) {
8888 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
8889 .addImm(0)
8890 .addReg(BufReg);
8891 } else {
8892 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
8893 .addImm(0)
8894 .addReg(BufReg);
8895 }
8896 MIB.setMemRefs(MMOBegin, MMOEnd);
8897
8898 // Reload IP
8899 if (PVT == MVT::i64) {
8900 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008901 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008902 .addReg(BufReg);
8903 } else {
8904 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
8905 .addImm(LabelOffset)
8906 .addReg(BufReg);
8907 }
8908 MIB.setMemRefs(MMOBegin, MMOEnd);
8909
8910 // Reload SP
8911 if (PVT == MVT::i64) {
8912 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008913 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008914 .addReg(BufReg);
8915 } else {
8916 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
8917 .addImm(SPOffset)
8918 .addReg(BufReg);
8919 }
8920 MIB.setMemRefs(MMOBegin, MMOEnd);
8921
Hal Finkelf05d6c72013-07-17 23:50:51 +00008922 // Reload BP
8923 if (PVT == MVT::i64) {
8924 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
8925 .addImm(BPOffset)
8926 .addReg(BufReg);
8927 } else {
8928 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
8929 .addImm(BPOffset)
8930 .addReg(BufReg);
8931 }
8932 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00008933
8934 // Reload TOC
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008935 if (PVT == MVT::i64 && Subtarget.isSVR4ABI()) {
Hal Finkele6698d52015-02-01 15:03:28 +00008936 setUsesTOCBasePtr(*MBB->getParent());
Hal Finkel756810f2013-03-21 21:37:52 +00008937 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008938 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008939 .addReg(BufReg);
8940
8941 MIB.setMemRefs(MMOBegin, MMOEnd);
8942 }
8943
8944 // Jump
8945 BuildMI(*MBB, MI, DL,
8946 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
8947 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
8948
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008949 MI.eraseFromParent();
Hal Finkel756810f2013-03-21 21:37:52 +00008950 return MBB;
8951}
8952
Dale Johannesena32affb2008-08-28 17:53:09 +00008953MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008954PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00008955 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008956 if (MI.getOpcode() == TargetOpcode::STACKMAP ||
8957 MI.getOpcode() == TargetOpcode::PATCHPOINT) {
Hal Finkelaf519932015-01-19 07:20:27 +00008958 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI() &&
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008959 MI.getOpcode() == TargetOpcode::PATCHPOINT) {
Hal Finkelaf519932015-01-19 07:20:27 +00008960 // Call lowering should have added an r2 operand to indicate a dependence
8961 // on the TOC base pointer value. It can't however, because there is no
8962 // way to mark the dependence as implicit there, and so the stackmap code
8963 // will confuse it with a regular operand. Instead, add the dependence
8964 // here.
Hal Finkele6698d52015-02-01 15:03:28 +00008965 setUsesTOCBasePtr(*BB->getParent());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008966 MI.addOperand(MachineOperand::CreateReg(PPC::X2, false, true));
Hal Finkelaf519932015-01-19 07:20:27 +00008967 }
8968
Hal Finkel934361a2015-01-14 01:07:51 +00008969 return emitPatchPoint(MI, BB);
Hal Finkelaf519932015-01-19 07:20:27 +00008970 }
Hal Finkel934361a2015-01-14 01:07:51 +00008971
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008972 if (MI.getOpcode() == PPC::EH_SjLj_SetJmp32 ||
8973 MI.getOpcode() == PPC::EH_SjLj_SetJmp64) {
Hal Finkel756810f2013-03-21 21:37:52 +00008974 return emitEHSjLjSetJmp(MI, BB);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008975 } else if (MI.getOpcode() == PPC::EH_SjLj_LongJmp32 ||
8976 MI.getOpcode() == PPC::EH_SjLj_LongJmp64) {
Hal Finkel756810f2013-03-21 21:37:52 +00008977 return emitEHSjLjLongJmp(MI, BB);
8978 }
8979
Eric Christophercccae792015-01-30 22:02:31 +00008980 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00008981
8982 // To "insert" these instructions we actually have to insert their
8983 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00008984 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00008985 MachineFunction::iterator It = ++BB->getIterator();
Evan Cheng32e376f2008-07-12 02:23:19 +00008986
Dan Gohman3b460302008-07-07 23:14:23 +00008987 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00008988
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008989 if (Subtarget.hasISEL() &&
8990 (MI.getOpcode() == PPC::SELECT_CC_I4 ||
8991 MI.getOpcode() == PPC::SELECT_CC_I8 ||
8992 MI.getOpcode() == PPC::SELECT_I4 || MI.getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00008993 SmallVector<MachineOperand, 2> Cond;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008994 if (MI.getOpcode() == PPC::SELECT_CC_I4 ||
8995 MI.getOpcode() == PPC::SELECT_CC_I8)
8996 Cond.push_back(MI.getOperand(4));
Hal Finkel940ab932014-02-28 00:27:01 +00008997 else
8998 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008999 Cond.push_back(MI.getOperand(1));
Hal Finkeled6a2852013-04-05 23:29:01 +00009000
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009001 DebugLoc dl = MI.getDebugLoc();
9002 TII->insertSelect(*BB, MI, dl, MI.getOperand(0).getReg(), Cond,
9003 MI.getOperand(2).getReg(), MI.getOperand(3).getReg());
9004 } else if (MI.getOpcode() == PPC::SELECT_CC_I4 ||
9005 MI.getOpcode() == PPC::SELECT_CC_I8 ||
9006 MI.getOpcode() == PPC::SELECT_CC_F4 ||
9007 MI.getOpcode() == PPC::SELECT_CC_F8 ||
9008 MI.getOpcode() == PPC::SELECT_CC_QFRC ||
9009 MI.getOpcode() == PPC::SELECT_CC_QSRC ||
9010 MI.getOpcode() == PPC::SELECT_CC_QBRC ||
9011 MI.getOpcode() == PPC::SELECT_CC_VRRC ||
9012 MI.getOpcode() == PPC::SELECT_CC_VSFRC ||
9013 MI.getOpcode() == PPC::SELECT_CC_VSSRC ||
9014 MI.getOpcode() == PPC::SELECT_CC_VSRC ||
9015 MI.getOpcode() == PPC::SELECT_I4 ||
9016 MI.getOpcode() == PPC::SELECT_I8 ||
9017 MI.getOpcode() == PPC::SELECT_F4 ||
9018 MI.getOpcode() == PPC::SELECT_F8 ||
9019 MI.getOpcode() == PPC::SELECT_QFRC ||
9020 MI.getOpcode() == PPC::SELECT_QSRC ||
9021 MI.getOpcode() == PPC::SELECT_QBRC ||
9022 MI.getOpcode() == PPC::SELECT_VRRC ||
9023 MI.getOpcode() == PPC::SELECT_VSFRC ||
9024 MI.getOpcode() == PPC::SELECT_VSSRC ||
9025 MI.getOpcode() == PPC::SELECT_VSRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00009026 // The incoming instruction knows the destination vreg to set, the
9027 // condition code register to branch on, the true/false values to
9028 // select between, and a branch opcode to use.
9029
9030 // thisMBB:
9031 // ...
9032 // TrueVal = ...
9033 // cmpTY ccX, r1, r2
9034 // bCC copy1MBB
9035 // fallthrough --> copy0MBB
9036 MachineBasicBlock *thisMBB = BB;
9037 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
9038 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009039 DebugLoc dl = MI.getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00009040 F->insert(It, copy0MBB);
9041 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00009042
9043 // Transfer the remainder of BB and its successor edges to sinkMBB.
9044 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00009045 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00009046 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
9047
Evan Cheng32e376f2008-07-12 02:23:19 +00009048 // Next, add the true and fallthrough blocks as its successors.
9049 BB->addSuccessor(copy0MBB);
9050 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009051
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009052 if (MI.getOpcode() == PPC::SELECT_I4 || MI.getOpcode() == PPC::SELECT_I8 ||
9053 MI.getOpcode() == PPC::SELECT_F4 || MI.getOpcode() == PPC::SELECT_F8 ||
9054 MI.getOpcode() == PPC::SELECT_QFRC ||
9055 MI.getOpcode() == PPC::SELECT_QSRC ||
9056 MI.getOpcode() == PPC::SELECT_QBRC ||
9057 MI.getOpcode() == PPC::SELECT_VRRC ||
9058 MI.getOpcode() == PPC::SELECT_VSFRC ||
9059 MI.getOpcode() == PPC::SELECT_VSSRC ||
9060 MI.getOpcode() == PPC::SELECT_VSRC) {
Hal Finkel940ab932014-02-28 00:27:01 +00009061 BuildMI(BB, dl, TII->get(PPC::BC))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009062 .addReg(MI.getOperand(1).getReg())
9063 .addMBB(sinkMBB);
Hal Finkel940ab932014-02-28 00:27:01 +00009064 } else {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009065 unsigned SelectPred = MI.getOperand(4).getImm();
Hal Finkel940ab932014-02-28 00:27:01 +00009066 BuildMI(BB, dl, TII->get(PPC::BCC))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009067 .addImm(SelectPred)
9068 .addReg(MI.getOperand(1).getReg())
9069 .addMBB(sinkMBB);
Hal Finkel940ab932014-02-28 00:27:01 +00009070 }
Dan Gohman34396292010-07-06 20:24:04 +00009071
Evan Cheng32e376f2008-07-12 02:23:19 +00009072 // copy0MBB:
9073 // %FalseValue = ...
9074 // # fallthrough to sinkMBB
9075 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009076
Evan Cheng32e376f2008-07-12 02:23:19 +00009077 // Update machine-CFG edges
9078 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009079
Evan Cheng32e376f2008-07-12 02:23:19 +00009080 // sinkMBB:
9081 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
9082 // ...
9083 BB = sinkMBB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009084 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::PHI), MI.getOperand(0).getReg())
9085 .addReg(MI.getOperand(3).getReg())
9086 .addMBB(copy0MBB)
9087 .addReg(MI.getOperand(2).getReg())
9088 .addMBB(thisMBB);
9089 } else if (MI.getOpcode() == PPC::ReadTB) {
Hal Finkelbbdee932014-12-02 22:01:00 +00009090 // To read the 64-bit time-base register on a 32-bit target, we read the
9091 // two halves. Should the counter have wrapped while it was being read, we
9092 // need to try again.
9093 // ...
9094 // readLoop:
9095 // mfspr Rx,TBU # load from TBU
9096 // mfspr Ry,TB # load from TB
9097 // mfspr Rz,TBU # load from TBU
NAKAMURA Takumibf9cc7f2015-09-22 11:10:08 +00009098 // cmpw crX,Rx,Rz # check if 'old'='new'
Hal Finkelbbdee932014-12-02 22:01:00 +00009099 // bne readLoop # branch if they're not equal
9100 // ...
9101
9102 MachineBasicBlock *readMBB = F->CreateMachineBasicBlock(LLVM_BB);
9103 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009104 DebugLoc dl = MI.getDebugLoc();
Hal Finkelbbdee932014-12-02 22:01:00 +00009105 F->insert(It, readMBB);
9106 F->insert(It, sinkMBB);
9107
9108 // Transfer the remainder of BB and its successor edges to sinkMBB.
9109 sinkMBB->splice(sinkMBB->begin(), BB,
9110 std::next(MachineBasicBlock::iterator(MI)), BB->end());
9111 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
9112
9113 BB->addSuccessor(readMBB);
9114 BB = readMBB;
9115
9116 MachineRegisterInfo &RegInfo = F->getRegInfo();
9117 unsigned ReadAgainReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009118 unsigned LoReg = MI.getOperand(0).getReg();
9119 unsigned HiReg = MI.getOperand(1).getReg();
Hal Finkelbbdee932014-12-02 22:01:00 +00009120
9121 BuildMI(BB, dl, TII->get(PPC::MFSPR), HiReg).addImm(269);
9122 BuildMI(BB, dl, TII->get(PPC::MFSPR), LoReg).addImm(268);
9123 BuildMI(BB, dl, TII->get(PPC::MFSPR), ReadAgainReg).addImm(269);
9124
9125 unsigned CmpReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
9126
9127 BuildMI(BB, dl, TII->get(PPC::CMPW), CmpReg)
9128 .addReg(HiReg).addReg(ReadAgainReg);
9129 BuildMI(BB, dl, TII->get(PPC::BCC))
9130 .addImm(PPC::PRED_NE).addReg(CmpReg).addMBB(readMBB);
9131
9132 BB->addSuccessor(readMBB);
9133 BB->addSuccessor(sinkMBB);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009134 } else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
Dale Johannesena32affb2008-08-28 17:53:09 +00009135 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009136 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
Dale Johannesena32affb2008-08-28 17:53:09 +00009137 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009138 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009139 BB = EmitAtomicBinary(MI, BB, 4, PPC::ADD4);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009140 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009141 BB = EmitAtomicBinary(MI, BB, 8, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00009142
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009143 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
Dale Johannesena32affb2008-08-28 17:53:09 +00009144 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009145 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
Dale Johannesena32affb2008-08-28 17:53:09 +00009146 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009147 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009148 BB = EmitAtomicBinary(MI, BB, 4, PPC::AND);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009149 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009150 BB = EmitAtomicBinary(MI, BB, 8, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00009151
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009152 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
Dale Johannesena32affb2008-08-28 17:53:09 +00009153 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009154 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
Dale Johannesena32affb2008-08-28 17:53:09 +00009155 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009156 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009157 BB = EmitAtomicBinary(MI, BB, 4, PPC::OR);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009158 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009159 BB = EmitAtomicBinary(MI, BB, 8, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00009160
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009161 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
Dale Johannesena32affb2008-08-28 17:53:09 +00009162 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009163 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
Dale Johannesena32affb2008-08-28 17:53:09 +00009164 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009165 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009166 BB = EmitAtomicBinary(MI, BB, 4, PPC::XOR);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009167 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009168 BB = EmitAtomicBinary(MI, BB, 8, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00009169
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009170 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00009171 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::NAND);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009172 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00009173 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::NAND);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009174 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009175 BB = EmitAtomicBinary(MI, BB, 4, PPC::NAND);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009176 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009177 BB = EmitAtomicBinary(MI, BB, 8, PPC::NAND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00009178
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009179 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
Dale Johannesena32affb2008-08-28 17:53:09 +00009180 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009181 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
Dale Johannesena32affb2008-08-28 17:53:09 +00009182 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009183 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009184 BB = EmitAtomicBinary(MI, BB, 4, PPC::SUBF);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009185 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009186 BB = EmitAtomicBinary(MI, BB, 8, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00009187
Hal Finkel57282002016-08-28 16:17:58 +00009188 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I8)
9189 BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPW, PPC::PRED_GE);
9190 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I16)
9191 BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPW, PPC::PRED_GE);
9192 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I32)
9193 BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPW, PPC::PRED_GE);
9194 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I64)
9195 BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPD, PPC::PRED_GE);
9196
9197 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I8)
9198 BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPW, PPC::PRED_LE);
9199 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I16)
9200 BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPW, PPC::PRED_LE);
9201 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I32)
9202 BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPW, PPC::PRED_LE);
9203 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I64)
9204 BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPD, PPC::PRED_LE);
9205
9206 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I8)
9207 BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPLW, PPC::PRED_GE);
9208 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I16)
9209 BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPLW, PPC::PRED_GE);
9210 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I32)
9211 BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPLW, PPC::PRED_GE);
9212 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I64)
9213 BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPLD, PPC::PRED_GE);
9214
9215 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I8)
9216 BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPLW, PPC::PRED_LE);
9217 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I16)
9218 BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPLW, PPC::PRED_LE);
9219 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I32)
9220 BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPLW, PPC::PRED_LE);
9221 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I64)
9222 BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPLD, PPC::PRED_LE);
9223
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009224 else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I8)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00009225 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009226 else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I16)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00009227 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009228 else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009229 BB = EmitAtomicBinary(MI, BB, 4, 0);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009230 else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009231 BB = EmitAtomicBinary(MI, BB, 8, 0);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00009232
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009233 else if (MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
9234 MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I64 ||
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009235 (Subtarget.hasPartwordAtomics() &&
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009236 MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I8) ||
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009237 (Subtarget.hasPartwordAtomics() &&
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009238 MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I16)) {
9239 bool is64bit = MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
Evan Cheng32e376f2008-07-12 02:23:19 +00009240
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009241 auto LoadMnemonic = PPC::LDARX;
9242 auto StoreMnemonic = PPC::STDCX;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009243 switch (MI.getOpcode()) {
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009244 default:
9245 llvm_unreachable("Compare and swap of unknown size");
9246 case PPC::ATOMIC_CMP_SWAP_I8:
9247 LoadMnemonic = PPC::LBARX;
9248 StoreMnemonic = PPC::STBCX;
9249 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics.");
9250 break;
9251 case PPC::ATOMIC_CMP_SWAP_I16:
9252 LoadMnemonic = PPC::LHARX;
9253 StoreMnemonic = PPC::STHCX;
9254 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics.");
9255 break;
9256 case PPC::ATOMIC_CMP_SWAP_I32:
9257 LoadMnemonic = PPC::LWARX;
9258 StoreMnemonic = PPC::STWCX;
9259 break;
9260 case PPC::ATOMIC_CMP_SWAP_I64:
9261 LoadMnemonic = PPC::LDARX;
9262 StoreMnemonic = PPC::STDCX;
9263 break;
9264 }
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009265 unsigned dest = MI.getOperand(0).getReg();
9266 unsigned ptrA = MI.getOperand(1).getReg();
9267 unsigned ptrB = MI.getOperand(2).getReg();
9268 unsigned oldval = MI.getOperand(3).getReg();
9269 unsigned newval = MI.getOperand(4).getReg();
9270 DebugLoc dl = MI.getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00009271
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009272 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
9273 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
9274 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009275 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009276 F->insert(It, loop1MBB);
9277 F->insert(It, loop2MBB);
9278 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009279 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00009280 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00009281 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00009282 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009283
9284 // thisMBB:
9285 // ...
9286 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009287 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009288
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009289 // loop1MBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009290 // l[bhwd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009291 // cmp[wd] dest, oldval
9292 // bne- midMBB
9293 // loop2MBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009294 // st[bhwd]cx. newval, ptr
Evan Cheng32e376f2008-07-12 02:23:19 +00009295 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009296 // b exitBB
9297 // midMBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009298 // st[bhwd]cx. dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009299 // exitBB:
9300 BB = loop1MBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009301 BuildMI(BB, dl, TII->get(LoadMnemonic), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00009302 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009303 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00009304 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009305 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009306 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
9307 BB->addSuccessor(loop2MBB);
9308 BB->addSuccessor(midMBB);
9309
9310 BB = loop2MBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009311 BuildMI(BB, dl, TII->get(StoreMnemonic))
Evan Cheng32e376f2008-07-12 02:23:19 +00009312 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009313 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009314 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009315 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009316 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009317 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009318
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009319 BB = midMBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009320 BuildMI(BB, dl, TII->get(StoreMnemonic))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009321 .addReg(dest).addReg(ptrA).addReg(ptrB);
9322 BB->addSuccessor(exitMBB);
9323
Evan Cheng32e376f2008-07-12 02:23:19 +00009324 // exitMBB:
9325 // ...
9326 BB = exitMBB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009327 } else if (MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
9328 MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
Dale Johannesen340d2642008-08-30 00:08:53 +00009329 // We must use 64-bit registers for addresses when targeting 64-bit,
9330 // since we're actually doing arithmetic on them. Other registers
9331 // can be 32-bit.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009332 bool is64bit = Subtarget.isPPC64();
Hal Finkel3d70a9d2016-08-29 22:25:36 +00009333 bool isLittleEndian = Subtarget.isLittleEndian();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009334 bool is8bit = MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
Dale Johannesen340d2642008-08-30 00:08:53 +00009335
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009336 unsigned dest = MI.getOperand(0).getReg();
9337 unsigned ptrA = MI.getOperand(1).getReg();
9338 unsigned ptrB = MI.getOperand(2).getReg();
9339 unsigned oldval = MI.getOperand(3).getReg();
9340 unsigned newval = MI.getOperand(4).getReg();
9341 DebugLoc dl = MI.getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00009342
9343 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
9344 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
9345 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
9346 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
9347 F->insert(It, loop1MBB);
9348 F->insert(It, loop2MBB);
9349 F->insert(It, midMBB);
9350 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00009351 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00009352 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00009353 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00009354
9355 MachineRegisterInfo &RegInfo = F->getRegInfo();
Craig Topper61e88f42014-11-21 05:58:21 +00009356 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass
9357 : &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00009358 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
9359 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
Hal Finkel3d70a9d2016-08-29 22:25:36 +00009360 unsigned ShiftReg =
9361 isLittleEndian ? Shift1Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesen340d2642008-08-30 00:08:53 +00009362 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
9363 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
9364 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
9365 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
9366 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
9367 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
9368 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
9369 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
9370 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
9371 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
9372 unsigned Ptr1Reg;
9373 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00009374 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00009375 // thisMBB:
9376 // ...
9377 // fallthrough --> loopMBB
9378 BB->addSuccessor(loop1MBB);
9379
9380 // The 4-byte load must be aligned, while a char or short may be
9381 // anywhere in the word. Hence all this nasty bookkeeping code.
9382 // add ptr1, ptrA, ptrB [copy if ptrA==0]
9383 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00009384 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00009385 // rlwinm ptr, ptr1, 0, 0, 29
9386 // slw newval2, newval, shift
9387 // slw oldval2, oldval,shift
9388 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
9389 // slw mask, mask2, shift
9390 // and newval3, newval2, mask
9391 // and oldval3, oldval2, mask
9392 // loop1MBB:
9393 // lwarx tmpDest, ptr
9394 // and tmp, tmpDest, mask
9395 // cmpw tmp, oldval3
9396 // bne- midMBB
9397 // loop2MBB:
9398 // andc tmp2, tmpDest, mask
9399 // or tmp4, tmp2, newval3
9400 // stwcx. tmp4, ptr
9401 // bne- loop1MBB
9402 // b exitBB
9403 // midMBB:
9404 // stwcx. tmpDest, ptr
9405 // exitBB:
9406 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00009407 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00009408 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009409 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009410 .addReg(ptrA).addReg(ptrB);
9411 } else {
9412 Ptr1Reg = ptrB;
9413 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00009414 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009415 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Hal Finkel3d70a9d2016-08-29 22:25:36 +00009416 if (!isLittleEndian)
9417 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
9418 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
Dale Johannesen340d2642008-08-30 00:08:53 +00009419 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00009420 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009421 .addReg(Ptr1Reg).addImm(0).addImm(61);
9422 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00009423 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009424 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009425 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009426 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009427 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009428 .addReg(oldval).addReg(ShiftReg);
9429 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00009430 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00009431 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00009432 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
9433 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
9434 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00009435 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00009436 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009437 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009438 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009439 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009440 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009441 .addReg(OldVal2Reg).addReg(MaskReg);
9442
9443 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00009444 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00009445 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009446 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
9447 .addReg(TmpDestReg).addReg(MaskReg);
9448 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00009449 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009450 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00009451 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
9452 BB->addSuccessor(loop2MBB);
9453 BB->addSuccessor(midMBB);
9454
9455 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00009456 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
9457 .addReg(TmpDestReg).addReg(MaskReg);
9458 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
9459 .addReg(Tmp2Reg).addReg(NewVal3Reg);
9460 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00009461 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009462 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00009463 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009464 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00009465 BB->addSuccessor(loop1MBB);
9466 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009467
Dale Johannesen340d2642008-08-30 00:08:53 +00009468 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00009469 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00009470 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00009471 BB->addSuccessor(exitMBB);
9472
9473 // exitMBB:
9474 // ...
9475 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00009476 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
9477 .addReg(ShiftReg);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009478 } else if (MI.getOpcode() == PPC::FADDrtz) {
Ulrich Weigand874fc622013-03-26 10:56:22 +00009479 // This pseudo performs an FADD with rounding mode temporarily forced
9480 // to round-to-zero. We emit this via custom inserter since the FPSCR
9481 // is not modeled at the SelectionDAG level.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009482 unsigned Dest = MI.getOperand(0).getReg();
9483 unsigned Src1 = MI.getOperand(1).getReg();
9484 unsigned Src2 = MI.getOperand(2).getReg();
9485 DebugLoc dl = MI.getDebugLoc();
Ulrich Weigand874fc622013-03-26 10:56:22 +00009486
9487 MachineRegisterInfo &RegInfo = F->getRegInfo();
9488 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
9489
9490 // Save FPSCR value.
9491 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
9492
9493 // Set rounding mode to round-to-zero.
9494 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
9495 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
9496
9497 // Perform addition.
9498 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
9499
9500 // Restore FPSCR value.
Hal Finkel64202162015-01-15 01:00:53 +00009501 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSFb)).addImm(1).addReg(MFFSReg);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009502 } else if (MI.getOpcode() == PPC::ANDIo_1_EQ_BIT ||
9503 MI.getOpcode() == PPC::ANDIo_1_GT_BIT ||
9504 MI.getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
9505 MI.getOpcode() == PPC::ANDIo_1_GT_BIT8) {
9506 unsigned Opcode = (MI.getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
9507 MI.getOpcode() == PPC::ANDIo_1_GT_BIT8)
9508 ? PPC::ANDIo8
9509 : PPC::ANDIo;
9510 bool isEQ = (MI.getOpcode() == PPC::ANDIo_1_EQ_BIT ||
9511 MI.getOpcode() == PPC::ANDIo_1_EQ_BIT8);
Hal Finkel940ab932014-02-28 00:27:01 +00009512
9513 MachineRegisterInfo &RegInfo = F->getRegInfo();
9514 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
9515 &PPC::GPRCRegClass :
9516 &PPC::G8RCRegClass);
9517
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009518 DebugLoc dl = MI.getDebugLoc();
Hal Finkel940ab932014-02-28 00:27:01 +00009519 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009520 .addReg(MI.getOperand(1).getReg())
9521 .addImm(1);
Hal Finkel940ab932014-02-28 00:27:01 +00009522 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009523 MI.getOperand(0).getReg())
9524 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
9525 } else if (MI.getOpcode() == PPC::TCHECK_RET) {
9526 DebugLoc Dl = MI.getDebugLoc();
Kit Barton535e69d2015-03-25 19:36:23 +00009527 MachineRegisterInfo &RegInfo = F->getRegInfo();
9528 unsigned CRReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
9529 BuildMI(*BB, MI, Dl, TII->get(PPC::TCHECK), CRReg);
9530 return BB;
Dale Johannesen340d2642008-08-30 00:08:53 +00009531 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009532 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00009533 }
Chris Lattner9b577f12005-08-26 21:23:58 +00009534
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009535 MI.eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00009536 return BB;
9537}
9538
Chris Lattner4211ca92006-04-14 06:01:58 +00009539//===----------------------------------------------------------------------===//
9540// Target Optimization Hooks
9541//===----------------------------------------------------------------------===//
9542
Hal Finkelcbf08922015-07-12 02:33:57 +00009543static std::string getRecipOp(const char *Base, EVT VT) {
9544 std::string RecipOp(Base);
9545 if (VT.getScalarType() == MVT::f64)
9546 RecipOp += "d";
9547 else
9548 RecipOp += "f";
9549
9550 if (VT.isVector())
9551 RecipOp = "vec-" + RecipOp;
9552
9553 return RecipOp;
9554}
9555
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009556SDValue PPCTargetLowering::getRsqrtEstimate(SDValue Operand,
9557 DAGCombinerInfo &DCI,
Sanjay Patel957efc232014-10-24 17:02:16 +00009558 unsigned &RefinementSteps,
9559 bool &UseOneConstNR) const {
Sanjay Patelbdf1e382014-09-26 23:01:47 +00009560 EVT VT = Operand.getValueType();
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009561 if ((VT == MVT::f32 && Subtarget.hasFRSQRTES()) ||
Eric Christophercccae792015-01-30 22:02:31 +00009562 (VT == MVT::f64 && Subtarget.hasFRSQRTE()) ||
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009563 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00009564 (VT == MVT::v2f64 && Subtarget.hasVSX()) ||
9565 (VT == MVT::v4f32 && Subtarget.hasQPX()) ||
9566 (VT == MVT::v4f64 && Subtarget.hasQPX())) {
Hal Finkelcbf08922015-07-12 02:33:57 +00009567 TargetRecip Recips = DCI.DAG.getTarget().Options.Reciprocals;
9568 std::string RecipOp = getRecipOp("sqrt", VT);
9569 if (!Recips.isEnabled(RecipOp))
9570 return SDValue();
9571
9572 RefinementSteps = Recips.getRefinementSteps(RecipOp);
Sanjay Patel957efc232014-10-24 17:02:16 +00009573 UseOneConstNR = true;
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009574 return DCI.DAG.getNode(PPCISD::FRSQRTE, SDLoc(Operand), VT, Operand);
Hal Finkel2e103312013-04-03 04:01:11 +00009575 }
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009576 return SDValue();
9577}
9578
9579SDValue PPCTargetLowering::getRecipEstimate(SDValue Operand,
9580 DAGCombinerInfo &DCI,
9581 unsigned &RefinementSteps) const {
9582 EVT VT = Operand.getValueType();
9583 if ((VT == MVT::f32 && Subtarget.hasFRES()) ||
Eric Christophercccae792015-01-30 22:02:31 +00009584 (VT == MVT::f64 && Subtarget.hasFRE()) ||
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009585 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00009586 (VT == MVT::v2f64 && Subtarget.hasVSX()) ||
9587 (VT == MVT::v4f32 && Subtarget.hasQPX()) ||
9588 (VT == MVT::v4f64 && Subtarget.hasQPX())) {
Hal Finkelcbf08922015-07-12 02:33:57 +00009589 TargetRecip Recips = DCI.DAG.getTarget().Options.Reciprocals;
9590 std::string RecipOp = getRecipOp("div", VT);
9591 if (!Recips.isEnabled(RecipOp))
9592 return SDValue();
9593
9594 RefinementSteps = Recips.getRefinementSteps(RecipOp);
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009595 return DCI.DAG.getNode(PPCISD::FRE, SDLoc(Operand), VT, Operand);
9596 }
9597 return SDValue();
Hal Finkel2e103312013-04-03 04:01:11 +00009598}
9599
Sanjay Patel1dd15592015-07-28 23:05:48 +00009600unsigned PPCTargetLowering::combineRepeatedFPDivisors() const {
Hal Finkel360f2132014-11-24 23:45:21 +00009601 // Note: This functionality is used only when unsafe-fp-math is enabled, and
9602 // on cores with reciprocal estimates (which are used when unsafe-fp-math is
9603 // enabled for division), this functionality is redundant with the default
9604 // combiner logic (once the division -> reciprocal/multiply transformation
9605 // has taken place). As a result, this matters more for older cores than for
9606 // newer ones.
9607
9608 // Combine multiple FDIVs with the same divisor into multiple FMULs by the
9609 // reciprocal if there are two or more FDIVs (for embedded cores with only
9610 // one FP pipeline) for three or more FDIVs (for generic OOO cores).
9611 switch (Subtarget.getDarwinDirective()) {
9612 default:
Sanjay Patel1dd15592015-07-28 23:05:48 +00009613 return 3;
Hal Finkel360f2132014-11-24 23:45:21 +00009614 case PPC::DIR_440:
9615 case PPC::DIR_A2:
9616 case PPC::DIR_E500mc:
9617 case PPC::DIR_E5500:
Sanjay Patel1dd15592015-07-28 23:05:48 +00009618 return 2;
Hal Finkel360f2132014-11-24 23:45:21 +00009619 }
9620}
9621
Hal Finkele6702ca2015-09-03 22:37:44 +00009622// isConsecutiveLSLoc needs to work even if all adds have not yet been
9623// collapsed, and so we need to look through chains of them.
9624static void getBaseWithConstantOffset(SDValue Loc, SDValue &Base,
9625 int64_t& Offset, SelectionDAG &DAG) {
9626 if (DAG.isBaseWithConstantOffset(Loc)) {
9627 Base = Loc.getOperand(0);
9628 Offset += cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
9629
9630 // The base might itself be a base plus an offset, and if so, accumulate
9631 // that as well.
9632 getBaseWithConstantOffset(Loc.getOperand(0), Base, Offset, DAG);
9633 }
9634}
9635
Hal Finkel3604bf72014-08-01 01:02:01 +00009636static bool isConsecutiveLSLoc(SDValue Loc, EVT VT, LSBaseSDNode *Base,
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009637 unsigned Bytes, int Dist,
9638 SelectionDAG &DAG) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009639 if (VT.getSizeInBits() / 8 != Bytes)
9640 return false;
9641
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009642 SDValue BaseLoc = Base->getBasePtr();
9643 if (Loc.getOpcode() == ISD::FrameIndex) {
9644 if (BaseLoc.getOpcode() != ISD::FrameIndex)
9645 return false;
Matthias Braun941a7052016-07-28 18:40:00 +00009646 const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009647 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
9648 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Matthias Braun941a7052016-07-28 18:40:00 +00009649 int FS = MFI.getObjectSize(FI);
9650 int BFS = MFI.getObjectSize(BFI);
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009651 if (FS != BFS || FS != (int)Bytes) return false;
Matthias Braun941a7052016-07-28 18:40:00 +00009652 return MFI.getObjectOffset(FI) == (MFI.getObjectOffset(BFI) + Dist*Bytes);
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009653 }
9654
Hal Finkele6702ca2015-09-03 22:37:44 +00009655 SDValue Base1 = Loc, Base2 = BaseLoc;
9656 int64_t Offset1 = 0, Offset2 = 0;
9657 getBaseWithConstantOffset(Loc, Base1, Offset1, DAG);
9658 getBaseWithConstantOffset(BaseLoc, Base2, Offset2, DAG);
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00009659 if (Base1 == Base2 && Offset1 == (Offset2 + Dist * Bytes))
9660 return true;
9661
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009662 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Craig Topper062a2ba2014-04-25 05:30:21 +00009663 const GlobalValue *GV1 = nullptr;
9664 const GlobalValue *GV2 = nullptr;
Hal Finkele6702ca2015-09-03 22:37:44 +00009665 Offset1 = 0;
9666 Offset2 = 0;
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009667 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
9668 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
9669 if (isGA1 && isGA2 && GV1 == GV2)
9670 return Offset1 == (Offset2 + Dist*Bytes);
9671 return false;
9672}
9673
Hal Finkel3604bf72014-08-01 01:02:01 +00009674// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
9675// not enforce equality of the chain operands.
9676static bool isConsecutiveLS(SDNode *N, LSBaseSDNode *Base,
9677 unsigned Bytes, int Dist,
9678 SelectionDAG &DAG) {
9679 if (LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(N)) {
9680 EVT VT = LS->getMemoryVT();
9681 SDValue Loc = LS->getBasePtr();
9682 return isConsecutiveLSLoc(Loc, VT, Base, Bytes, Dist, DAG);
9683 }
9684
9685 if (N->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
9686 EVT VT;
9687 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9688 default: return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00009689 case Intrinsic::ppc_qpx_qvlfd:
9690 case Intrinsic::ppc_qpx_qvlfda:
9691 VT = MVT::v4f64;
9692 break;
9693 case Intrinsic::ppc_qpx_qvlfs:
9694 case Intrinsic::ppc_qpx_qvlfsa:
9695 VT = MVT::v4f32;
9696 break;
9697 case Intrinsic::ppc_qpx_qvlfcd:
9698 case Intrinsic::ppc_qpx_qvlfcda:
9699 VT = MVT::v2f64;
9700 break;
9701 case Intrinsic::ppc_qpx_qvlfcs:
9702 case Intrinsic::ppc_qpx_qvlfcsa:
9703 VT = MVT::v2f32;
9704 break;
9705 case Intrinsic::ppc_qpx_qvlfiwa:
9706 case Intrinsic::ppc_qpx_qvlfiwz:
Hal Finkel3604bf72014-08-01 01:02:01 +00009707 case Intrinsic::ppc_altivec_lvx:
9708 case Intrinsic::ppc_altivec_lvxl:
Bill Schmidt72954782014-11-12 04:19:40 +00009709 case Intrinsic::ppc_vsx_lxvw4x:
Hal Finkel3604bf72014-08-01 01:02:01 +00009710 VT = MVT::v4i32;
9711 break;
Bill Schmidt72954782014-11-12 04:19:40 +00009712 case Intrinsic::ppc_vsx_lxvd2x:
9713 VT = MVT::v2f64;
9714 break;
Hal Finkel3604bf72014-08-01 01:02:01 +00009715 case Intrinsic::ppc_altivec_lvebx:
9716 VT = MVT::i8;
9717 break;
9718 case Intrinsic::ppc_altivec_lvehx:
9719 VT = MVT::i16;
9720 break;
9721 case Intrinsic::ppc_altivec_lvewx:
9722 VT = MVT::i32;
9723 break;
9724 }
9725
9726 return isConsecutiveLSLoc(N->getOperand(2), VT, Base, Bytes, Dist, DAG);
9727 }
9728
9729 if (N->getOpcode() == ISD::INTRINSIC_VOID) {
9730 EVT VT;
9731 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9732 default: return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00009733 case Intrinsic::ppc_qpx_qvstfd:
9734 case Intrinsic::ppc_qpx_qvstfda:
9735 VT = MVT::v4f64;
9736 break;
9737 case Intrinsic::ppc_qpx_qvstfs:
9738 case Intrinsic::ppc_qpx_qvstfsa:
9739 VT = MVT::v4f32;
9740 break;
9741 case Intrinsic::ppc_qpx_qvstfcd:
9742 case Intrinsic::ppc_qpx_qvstfcda:
9743 VT = MVT::v2f64;
9744 break;
9745 case Intrinsic::ppc_qpx_qvstfcs:
9746 case Intrinsic::ppc_qpx_qvstfcsa:
9747 VT = MVT::v2f32;
9748 break;
9749 case Intrinsic::ppc_qpx_qvstfiw:
9750 case Intrinsic::ppc_qpx_qvstfiwa:
Hal Finkel3604bf72014-08-01 01:02:01 +00009751 case Intrinsic::ppc_altivec_stvx:
9752 case Intrinsic::ppc_altivec_stvxl:
Bill Schmidt72954782014-11-12 04:19:40 +00009753 case Intrinsic::ppc_vsx_stxvw4x:
Hal Finkel3604bf72014-08-01 01:02:01 +00009754 VT = MVT::v4i32;
9755 break;
Bill Schmidt72954782014-11-12 04:19:40 +00009756 case Intrinsic::ppc_vsx_stxvd2x:
9757 VT = MVT::v2f64;
9758 break;
Hal Finkel3604bf72014-08-01 01:02:01 +00009759 case Intrinsic::ppc_altivec_stvebx:
9760 VT = MVT::i8;
9761 break;
9762 case Intrinsic::ppc_altivec_stvehx:
9763 VT = MVT::i16;
9764 break;
9765 case Intrinsic::ppc_altivec_stvewx:
9766 VT = MVT::i32;
9767 break;
9768 }
9769
9770 return isConsecutiveLSLoc(N->getOperand(3), VT, Base, Bytes, Dist, DAG);
9771 }
9772
9773 return false;
9774}
9775
Hal Finkel7d8a6912013-05-26 18:08:30 +00009776// Return true is there is a nearyby consecutive load to the one provided
9777// (regardless of alignment). We search up and down the chain, looking though
Matt Arsenault57e74d22014-07-29 00:02:40 +00009778// token factors and other loads (but nothing else). As a result, a true result
9779// indicates that it is safe to create a new consecutive load adjacent to the
9780// load provided.
Hal Finkel7d8a6912013-05-26 18:08:30 +00009781static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
9782 SDValue Chain = LD->getChain();
9783 EVT VT = LD->getMemoryVT();
9784
9785 SmallSet<SDNode *, 16> LoadRoots;
9786 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
9787 SmallSet<SDNode *, 16> Visited;
9788
9789 // First, search up the chain, branching to follow all token-factor operands.
9790 // If we find a consecutive load, then we're done, otherwise, record all
9791 // nodes just above the top-level loads and token factors.
9792 while (!Queue.empty()) {
9793 SDNode *ChainNext = Queue.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00009794 if (!Visited.insert(ChainNext).second)
Hal Finkel7d8a6912013-05-26 18:08:30 +00009795 continue;
9796
Hal Finkel3604bf72014-08-01 01:02:01 +00009797 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009798 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00009799 return true;
9800
9801 if (!Visited.count(ChainLD->getChain().getNode()))
9802 Queue.push_back(ChainLD->getChain().getNode());
9803 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
Craig Topper66e588b2014-06-29 00:40:57 +00009804 for (const SDUse &O : ChainNext->ops())
9805 if (!Visited.count(O.getNode()))
9806 Queue.push_back(O.getNode());
Hal Finkel7d8a6912013-05-26 18:08:30 +00009807 } else
9808 LoadRoots.insert(ChainNext);
9809 }
9810
9811 // Second, search down the chain, starting from the top-level nodes recorded
9812 // in the first phase. These top-level nodes are the nodes just above all
9813 // loads and token factors. Starting with their uses, recursively look though
9814 // all loads (just the chain uses) and token factors to find a consecutive
9815 // load.
9816 Visited.clear();
9817 Queue.clear();
9818
9819 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
9820 IE = LoadRoots.end(); I != IE; ++I) {
9821 Queue.push_back(*I);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +00009822
Hal Finkel7d8a6912013-05-26 18:08:30 +00009823 while (!Queue.empty()) {
9824 SDNode *LoadRoot = Queue.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00009825 if (!Visited.insert(LoadRoot).second)
Hal Finkel7d8a6912013-05-26 18:08:30 +00009826 continue;
9827
Hal Finkel3604bf72014-08-01 01:02:01 +00009828 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009829 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00009830 return true;
9831
9832 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
9833 UE = LoadRoot->use_end(); UI != UE; ++UI)
Hal Finkel3604bf72014-08-01 01:02:01 +00009834 if (((isa<MemSDNode>(*UI) &&
9835 cast<MemSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
Hal Finkel7d8a6912013-05-26 18:08:30 +00009836 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
9837 Queue.push_back(*UI);
9838 }
9839 }
9840
9841 return false;
9842}
9843
Hal Finkel940ab932014-02-28 00:27:01 +00009844SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
9845 DAGCombinerInfo &DCI) const {
9846 SelectionDAG &DAG = DCI.DAG;
9847 SDLoc dl(N);
9848
Eric Christophercccae792015-01-30 22:02:31 +00009849 assert(Subtarget.useCRBits() && "Expecting to be tracking CR bits");
Hal Finkel940ab932014-02-28 00:27:01 +00009850 // If we're tracking CR bits, we need to be careful that we don't have:
9851 // trunc(binary-ops(zext(x), zext(y)))
9852 // or
9853 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
9854 // such that we're unnecessarily moving things into GPRs when it would be
9855 // better to keep them in CR bits.
9856
9857 // Note that trunc here can be an actual i1 trunc, or can be the effective
9858 // truncation that comes from a setcc or select_cc.
9859 if (N->getOpcode() == ISD::TRUNCATE &&
9860 N->getValueType(0) != MVT::i1)
9861 return SDValue();
9862
9863 if (N->getOperand(0).getValueType() != MVT::i32 &&
9864 N->getOperand(0).getValueType() != MVT::i64)
9865 return SDValue();
9866
9867 if (N->getOpcode() == ISD::SETCC ||
9868 N->getOpcode() == ISD::SELECT_CC) {
9869 // If we're looking at a comparison, then we need to make sure that the
9870 // high bits (all except for the first) don't matter the result.
9871 ISD::CondCode CC =
9872 cast<CondCodeSDNode>(N->getOperand(
9873 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
9874 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
9875
9876 if (ISD::isSignedIntSetCC(CC)) {
9877 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
9878 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
9879 return SDValue();
9880 } else if (ISD::isUnsignedIntSetCC(CC)) {
9881 if (!DAG.MaskedValueIsZero(N->getOperand(0),
9882 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
9883 !DAG.MaskedValueIsZero(N->getOperand(1),
9884 APInt::getHighBitsSet(OpBits, OpBits-1)))
9885 return SDValue();
9886 } else {
9887 // This is neither a signed nor an unsigned comparison, just make sure
9888 // that the high bits are equal.
9889 APInt Op1Zero, Op1One;
9890 APInt Op2Zero, Op2One;
Jay Foada0653a32014-05-14 21:14:37 +00009891 DAG.computeKnownBits(N->getOperand(0), Op1Zero, Op1One);
9892 DAG.computeKnownBits(N->getOperand(1), Op2Zero, Op2One);
Hal Finkel940ab932014-02-28 00:27:01 +00009893
9894 // We don't really care about what is known about the first bit (if
9895 // anything), so clear it in all masks prior to comparing them.
9896 Op1Zero.clearBit(0); Op1One.clearBit(0);
9897 Op2Zero.clearBit(0); Op2One.clearBit(0);
9898
9899 if (Op1Zero != Op2Zero || Op1One != Op2One)
9900 return SDValue();
9901 }
9902 }
9903
9904 // We now know that the higher-order bits are irrelevant, we just need to
9905 // make sure that all of the intermediate operations are bit operations, and
9906 // all inputs are extensions.
9907 if (N->getOperand(0).getOpcode() != ISD::AND &&
9908 N->getOperand(0).getOpcode() != ISD::OR &&
9909 N->getOperand(0).getOpcode() != ISD::XOR &&
9910 N->getOperand(0).getOpcode() != ISD::SELECT &&
9911 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
9912 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
9913 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
9914 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
9915 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
9916 return SDValue();
9917
9918 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
9919 N->getOperand(1).getOpcode() != ISD::AND &&
9920 N->getOperand(1).getOpcode() != ISD::OR &&
9921 N->getOperand(1).getOpcode() != ISD::XOR &&
9922 N->getOperand(1).getOpcode() != ISD::SELECT &&
9923 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
9924 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
9925 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
9926 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
9927 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
9928 return SDValue();
9929
9930 SmallVector<SDValue, 4> Inputs;
9931 SmallVector<SDValue, 8> BinOps, PromOps;
9932 SmallPtrSet<SDNode *, 16> Visited;
9933
9934 for (unsigned i = 0; i < 2; ++i) {
9935 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9936 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9937 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
9938 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
9939 isa<ConstantSDNode>(N->getOperand(i)))
9940 Inputs.push_back(N->getOperand(i));
9941 else
9942 BinOps.push_back(N->getOperand(i));
9943
9944 if (N->getOpcode() == ISD::TRUNCATE)
9945 break;
9946 }
9947
9948 // Visit all inputs, collect all binary operations (and, or, xor and
NAKAMURA Takumi84965032015-09-22 11:14:12 +00009949 // select) that are all fed by extensions.
Hal Finkel940ab932014-02-28 00:27:01 +00009950 while (!BinOps.empty()) {
9951 SDValue BinOp = BinOps.back();
9952 BinOps.pop_back();
9953
David Blaikie70573dc2014-11-19 07:49:26 +00009954 if (!Visited.insert(BinOp.getNode()).second)
Hal Finkel940ab932014-02-28 00:27:01 +00009955 continue;
9956
9957 PromOps.push_back(BinOp);
9958
9959 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
9960 // The condition of the select is not promoted.
9961 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
9962 continue;
9963 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
9964 continue;
9965
9966 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9967 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9968 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
9969 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
9970 isa<ConstantSDNode>(BinOp.getOperand(i))) {
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00009971 Inputs.push_back(BinOp.getOperand(i));
Hal Finkel940ab932014-02-28 00:27:01 +00009972 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
9973 BinOp.getOperand(i).getOpcode() == ISD::OR ||
9974 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
9975 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
9976 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
9977 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
9978 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9979 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9980 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
9981 BinOps.push_back(BinOp.getOperand(i));
9982 } else {
9983 // We have an input that is not an extension or another binary
9984 // operation; we'll abort this transformation.
9985 return SDValue();
9986 }
9987 }
9988 }
9989
9990 // Make sure that this is a self-contained cluster of operations (which
9991 // is not quite the same thing as saying that everything has only one
9992 // use).
9993 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9994 if (isa<ConstantSDNode>(Inputs[i]))
9995 continue;
9996
9997 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
9998 UE = Inputs[i].getNode()->use_end();
9999 UI != UE; ++UI) {
10000 SDNode *User = *UI;
10001 if (User != N && !Visited.count(User))
10002 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +000010003
10004 // Make sure that we're not going to promote the non-output-value
10005 // operand(s) or SELECT or SELECT_CC.
10006 // FIXME: Although we could sometimes handle this, and it does occur in
10007 // practice that one of the condition inputs to the select is also one of
10008 // the outputs, we currently can't deal with this.
10009 if (User->getOpcode() == ISD::SELECT) {
10010 if (User->getOperand(0) == Inputs[i])
10011 return SDValue();
10012 } else if (User->getOpcode() == ISD::SELECT_CC) {
10013 if (User->getOperand(0) == Inputs[i] ||
10014 User->getOperand(1) == Inputs[i])
10015 return SDValue();
10016 }
Hal Finkel940ab932014-02-28 00:27:01 +000010017 }
10018 }
10019
10020 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
10021 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
10022 UE = PromOps[i].getNode()->use_end();
10023 UI != UE; ++UI) {
10024 SDNode *User = *UI;
10025 if (User != N && !Visited.count(User))
10026 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +000010027
10028 // Make sure that we're not going to promote the non-output-value
10029 // operand(s) or SELECT or SELECT_CC.
10030 // FIXME: Although we could sometimes handle this, and it does occur in
10031 // practice that one of the condition inputs to the select is also one of
10032 // the outputs, we currently can't deal with this.
10033 if (User->getOpcode() == ISD::SELECT) {
10034 if (User->getOperand(0) == PromOps[i])
10035 return SDValue();
10036 } else if (User->getOpcode() == ISD::SELECT_CC) {
10037 if (User->getOperand(0) == PromOps[i] ||
10038 User->getOperand(1) == PromOps[i])
10039 return SDValue();
10040 }
Hal Finkel940ab932014-02-28 00:27:01 +000010041 }
10042 }
10043
10044 // Replace all inputs with the extension operand.
10045 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
10046 // Constants may have users outside the cluster of to-be-promoted nodes,
10047 // and so we need to replace those as we do the promotions.
10048 if (isa<ConstantSDNode>(Inputs[i]))
10049 continue;
10050 else
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +000010051 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
Hal Finkel940ab932014-02-28 00:27:01 +000010052 }
10053
Hal Finkel1fb10e82016-05-12 04:00:56 +000010054 std::list<HandleSDNode> PromOpHandles;
10055 for (auto &PromOp : PromOps)
NAKAMURA Takumiae7c97d2016-06-20 00:49:20 +000010056 PromOpHandles.emplace_back(PromOp);
Hal Finkel1fb10e82016-05-12 04:00:56 +000010057
Hal Finkel940ab932014-02-28 00:27:01 +000010058 // Replace all operations (these are all the same, but have a different
10059 // (i1) return type). DAG.getNode will validate that the types of
10060 // a binary operator match, so go through the list in reverse so that
10061 // we've likely promoted both operands first. Any intermediate truncations or
10062 // extensions disappear.
Hal Finkel1fb10e82016-05-12 04:00:56 +000010063 while (!PromOpHandles.empty()) {
10064 SDValue PromOp = PromOpHandles.back().getValue();
10065 PromOpHandles.pop_back();
Hal Finkel940ab932014-02-28 00:27:01 +000010066
10067 if (PromOp.getOpcode() == ISD::TRUNCATE ||
10068 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
10069 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
10070 PromOp.getOpcode() == ISD::ANY_EXTEND) {
10071 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
10072 PromOp.getOperand(0).getValueType() != MVT::i1) {
10073 // The operand is not yet ready (see comment below).
Hal Finkel1fb10e82016-05-12 04:00:56 +000010074 PromOpHandles.emplace_front(PromOp);
Hal Finkel940ab932014-02-28 00:27:01 +000010075 continue;
10076 }
10077
10078 SDValue RepValue = PromOp.getOperand(0);
10079 if (isa<ConstantSDNode>(RepValue))
10080 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
10081
10082 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
10083 continue;
10084 }
10085
10086 unsigned C;
10087 switch (PromOp.getOpcode()) {
10088 default: C = 0; break;
10089 case ISD::SELECT: C = 1; break;
10090 case ISD::SELECT_CC: C = 2; break;
10091 }
10092
10093 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
10094 PromOp.getOperand(C).getValueType() != MVT::i1) ||
10095 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
10096 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
10097 // The to-be-promoted operands of this node have not yet been
10098 // promoted (this should be rare because we're going through the
10099 // list backward, but if one of the operands has several users in
10100 // this cluster of to-be-promoted nodes, it is possible).
Hal Finkel1fb10e82016-05-12 04:00:56 +000010101 PromOpHandles.emplace_front(PromOp);
Hal Finkel940ab932014-02-28 00:27:01 +000010102 continue;
10103 }
10104
10105 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
10106 PromOp.getNode()->op_end());
10107
10108 // If there are any constant inputs, make sure they're replaced now.
10109 for (unsigned i = 0; i < 2; ++i)
10110 if (isa<ConstantSDNode>(Ops[C+i]))
10111 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
10112
10113 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +000010114 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops));
Hal Finkel940ab932014-02-28 00:27:01 +000010115 }
10116
10117 // Now we're left with the initial truncation itself.
10118 if (N->getOpcode() == ISD::TRUNCATE)
10119 return N->getOperand(0);
10120
10121 // Otherwise, this is a comparison. The operands to be compared have just
10122 // changed type (to i1), but everything else is the same.
10123 return SDValue(N, 0);
10124}
10125
10126SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
10127 DAGCombinerInfo &DCI) const {
10128 SelectionDAG &DAG = DCI.DAG;
10129 SDLoc dl(N);
10130
Hal Finkel940ab932014-02-28 00:27:01 +000010131 // If we're tracking CR bits, we need to be careful that we don't have:
10132 // zext(binary-ops(trunc(x), trunc(y)))
10133 // or
10134 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
10135 // such that we're unnecessarily moving things into CR bits that can more
10136 // efficiently stay in GPRs. Note that if we're not certain that the high
10137 // bits are set as required by the final extension, we still may need to do
10138 // some masking to get the proper behavior.
10139
Hal Finkel46043ed2014-03-01 21:36:57 +000010140 // This same functionality is important on PPC64 when dealing with
10141 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
10142 // the return values of functions. Because it is so similar, it is handled
10143 // here as well.
10144
Hal Finkel940ab932014-02-28 00:27:01 +000010145 if (N->getValueType(0) != MVT::i32 &&
10146 N->getValueType(0) != MVT::i64)
10147 return SDValue();
10148
Eric Christophercccae792015-01-30 22:02:31 +000010149 if (!((N->getOperand(0).getValueType() == MVT::i1 && Subtarget.useCRBits()) ||
10150 (N->getOperand(0).getValueType() == MVT::i32 && Subtarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +000010151 return SDValue();
10152
10153 if (N->getOperand(0).getOpcode() != ISD::AND &&
10154 N->getOperand(0).getOpcode() != ISD::OR &&
10155 N->getOperand(0).getOpcode() != ISD::XOR &&
10156 N->getOperand(0).getOpcode() != ISD::SELECT &&
10157 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
10158 return SDValue();
10159
10160 SmallVector<SDValue, 4> Inputs;
10161 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
10162 SmallPtrSet<SDNode *, 16> Visited;
10163
10164 // Visit all inputs, collect all binary operations (and, or, xor and
NAKAMURA Takumi84965032015-09-22 11:14:12 +000010165 // select) that are all fed by truncations.
Hal Finkel940ab932014-02-28 00:27:01 +000010166 while (!BinOps.empty()) {
10167 SDValue BinOp = BinOps.back();
10168 BinOps.pop_back();
10169
David Blaikie70573dc2014-11-19 07:49:26 +000010170 if (!Visited.insert(BinOp.getNode()).second)
Hal Finkel940ab932014-02-28 00:27:01 +000010171 continue;
10172
10173 PromOps.push_back(BinOp);
10174
10175 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
10176 // The condition of the select is not promoted.
10177 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
10178 continue;
10179 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
10180 continue;
10181
10182 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
10183 isa<ConstantSDNode>(BinOp.getOperand(i))) {
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +000010184 Inputs.push_back(BinOp.getOperand(i));
Hal Finkel940ab932014-02-28 00:27:01 +000010185 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
10186 BinOp.getOperand(i).getOpcode() == ISD::OR ||
10187 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
10188 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
10189 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
10190 BinOps.push_back(BinOp.getOperand(i));
10191 } else {
10192 // We have an input that is not a truncation or another binary
10193 // operation; we'll abort this transformation.
10194 return SDValue();
10195 }
10196 }
10197 }
10198
Hal Finkel4104a1a2014-12-14 05:53:19 +000010199 // The operands of a select that must be truncated when the select is
10200 // promoted because the operand is actually part of the to-be-promoted set.
10201 DenseMap<SDNode *, EVT> SelectTruncOp[2];
10202
Hal Finkel940ab932014-02-28 00:27:01 +000010203 // Make sure that this is a self-contained cluster of operations (which
10204 // is not quite the same thing as saying that everything has only one
10205 // use).
10206 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
10207 if (isa<ConstantSDNode>(Inputs[i]))
10208 continue;
10209
10210 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
10211 UE = Inputs[i].getNode()->use_end();
10212 UI != UE; ++UI) {
10213 SDNode *User = *UI;
10214 if (User != N && !Visited.count(User))
10215 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +000010216
Hal Finkel4104a1a2014-12-14 05:53:19 +000010217 // If we're going to promote the non-output-value operand(s) or SELECT or
10218 // SELECT_CC, record them for truncation.
Hal Finkel46043ed2014-03-01 21:36:57 +000010219 if (User->getOpcode() == ISD::SELECT) {
10220 if (User->getOperand(0) == Inputs[i])
Hal Finkel4104a1a2014-12-14 05:53:19 +000010221 SelectTruncOp[0].insert(std::make_pair(User,
10222 User->getOperand(0).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +000010223 } else if (User->getOpcode() == ISD::SELECT_CC) {
Hal Finkel4104a1a2014-12-14 05:53:19 +000010224 if (User->getOperand(0) == Inputs[i])
10225 SelectTruncOp[0].insert(std::make_pair(User,
10226 User->getOperand(0).getValueType()));
10227 if (User->getOperand(1) == Inputs[i])
10228 SelectTruncOp[1].insert(std::make_pair(User,
10229 User->getOperand(1).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +000010230 }
Hal Finkel940ab932014-02-28 00:27:01 +000010231 }
10232 }
10233
10234 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
10235 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
10236 UE = PromOps[i].getNode()->use_end();
10237 UI != UE; ++UI) {
10238 SDNode *User = *UI;
10239 if (User != N && !Visited.count(User))
10240 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +000010241
Hal Finkel4104a1a2014-12-14 05:53:19 +000010242 // If we're going to promote the non-output-value operand(s) or SELECT or
10243 // SELECT_CC, record them for truncation.
Hal Finkel46043ed2014-03-01 21:36:57 +000010244 if (User->getOpcode() == ISD::SELECT) {
10245 if (User->getOperand(0) == PromOps[i])
Hal Finkel4104a1a2014-12-14 05:53:19 +000010246 SelectTruncOp[0].insert(std::make_pair(User,
10247 User->getOperand(0).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +000010248 } else if (User->getOpcode() == ISD::SELECT_CC) {
Hal Finkel4104a1a2014-12-14 05:53:19 +000010249 if (User->getOperand(0) == PromOps[i])
10250 SelectTruncOp[0].insert(std::make_pair(User,
10251 User->getOperand(0).getValueType()));
10252 if (User->getOperand(1) == PromOps[i])
10253 SelectTruncOp[1].insert(std::make_pair(User,
10254 User->getOperand(1).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +000010255 }
Hal Finkel940ab932014-02-28 00:27:01 +000010256 }
10257 }
10258
Hal Finkel46043ed2014-03-01 21:36:57 +000010259 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +000010260 bool ReallyNeedsExt = false;
10261 if (N->getOpcode() != ISD::ANY_EXTEND) {
10262 // If all of the inputs are not already sign/zero extended, then
10263 // we'll still need to do that at the end.
10264 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
10265 if (isa<ConstantSDNode>(Inputs[i]))
10266 continue;
10267
10268 unsigned OpBits =
10269 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +000010270 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
10271
Hal Finkel940ab932014-02-28 00:27:01 +000010272 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
10273 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +000010274 APInt::getHighBitsSet(OpBits,
10275 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +000010276 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +000010277 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
10278 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +000010279 ReallyNeedsExt = true;
10280 break;
10281 }
10282 }
10283 }
10284
10285 // Replace all inputs, either with the truncation operand, or a
10286 // truncation or extension to the final output type.
10287 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
10288 // Constant inputs need to be replaced with the to-be-promoted nodes that
10289 // use them because they might have users outside of the cluster of
10290 // promoted nodes.
10291 if (isa<ConstantSDNode>(Inputs[i]))
10292 continue;
10293
10294 SDValue InSrc = Inputs[i].getOperand(0);
10295 if (Inputs[i].getValueType() == N->getValueType(0))
10296 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
10297 else if (N->getOpcode() == ISD::SIGN_EXTEND)
10298 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
10299 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
10300 else if (N->getOpcode() == ISD::ZERO_EXTEND)
10301 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
10302 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
10303 else
10304 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
10305 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
10306 }
10307
Hal Finkel1fb10e82016-05-12 04:00:56 +000010308 std::list<HandleSDNode> PromOpHandles;
10309 for (auto &PromOp : PromOps)
NAKAMURA Takumiae7c97d2016-06-20 00:49:20 +000010310 PromOpHandles.emplace_back(PromOp);
Hal Finkel1fb10e82016-05-12 04:00:56 +000010311
Hal Finkel940ab932014-02-28 00:27:01 +000010312 // Replace all operations (these are all the same, but have a different
10313 // (promoted) return type). DAG.getNode will validate that the types of
10314 // a binary operator match, so go through the list in reverse so that
10315 // we've likely promoted both operands first.
Hal Finkel1fb10e82016-05-12 04:00:56 +000010316 while (!PromOpHandles.empty()) {
10317 SDValue PromOp = PromOpHandles.back().getValue();
10318 PromOpHandles.pop_back();
Hal Finkel940ab932014-02-28 00:27:01 +000010319
10320 unsigned C;
10321 switch (PromOp.getOpcode()) {
10322 default: C = 0; break;
10323 case ISD::SELECT: C = 1; break;
10324 case ISD::SELECT_CC: C = 2; break;
10325 }
10326
10327 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
10328 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
10329 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
10330 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
10331 // The to-be-promoted operands of this node have not yet been
10332 // promoted (this should be rare because we're going through the
10333 // list backward, but if one of the operands has several users in
10334 // this cluster of to-be-promoted nodes, it is possible).
Hal Finkel1fb10e82016-05-12 04:00:56 +000010335 PromOpHandles.emplace_front(PromOp);
Hal Finkel940ab932014-02-28 00:27:01 +000010336 continue;
10337 }
10338
Hal Finkel4104a1a2014-12-14 05:53:19 +000010339 // For SELECT and SELECT_CC nodes, we do a similar check for any
10340 // to-be-promoted comparison inputs.
10341 if (PromOp.getOpcode() == ISD::SELECT ||
10342 PromOp.getOpcode() == ISD::SELECT_CC) {
10343 if ((SelectTruncOp[0].count(PromOp.getNode()) &&
10344 PromOp.getOperand(0).getValueType() != N->getValueType(0)) ||
10345 (SelectTruncOp[1].count(PromOp.getNode()) &&
10346 PromOp.getOperand(1).getValueType() != N->getValueType(0))) {
Hal Finkel1fb10e82016-05-12 04:00:56 +000010347 PromOpHandles.emplace_front(PromOp);
Hal Finkel4104a1a2014-12-14 05:53:19 +000010348 continue;
10349 }
10350 }
10351
Hal Finkel940ab932014-02-28 00:27:01 +000010352 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
10353 PromOp.getNode()->op_end());
10354
10355 // If this node has constant inputs, then they'll need to be promoted here.
10356 for (unsigned i = 0; i < 2; ++i) {
10357 if (!isa<ConstantSDNode>(Ops[C+i]))
10358 continue;
10359 if (Ops[C+i].getValueType() == N->getValueType(0))
10360 continue;
10361
10362 if (N->getOpcode() == ISD::SIGN_EXTEND)
10363 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
10364 else if (N->getOpcode() == ISD::ZERO_EXTEND)
10365 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
10366 else
10367 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
10368 }
10369
Hal Finkel4104a1a2014-12-14 05:53:19 +000010370 // If we've promoted the comparison inputs of a SELECT or SELECT_CC,
10371 // truncate them again to the original value type.
10372 if (PromOp.getOpcode() == ISD::SELECT ||
10373 PromOp.getOpcode() == ISD::SELECT_CC) {
10374 auto SI0 = SelectTruncOp[0].find(PromOp.getNode());
10375 if (SI0 != SelectTruncOp[0].end())
10376 Ops[0] = DAG.getNode(ISD::TRUNCATE, dl, SI0->second, Ops[0]);
10377 auto SI1 = SelectTruncOp[1].find(PromOp.getNode());
10378 if (SI1 != SelectTruncOp[1].end())
10379 Ops[1] = DAG.getNode(ISD::TRUNCATE, dl, SI1->second, Ops[1]);
10380 }
10381
Hal Finkel940ab932014-02-28 00:27:01 +000010382 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +000010383 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops));
Hal Finkel940ab932014-02-28 00:27:01 +000010384 }
10385
10386 // Now we're left with the initial extension itself.
10387 if (!ReallyNeedsExt)
10388 return N->getOperand(0);
10389
Hal Finkel46043ed2014-03-01 21:36:57 +000010390 // To zero extend, just mask off everything except for the first bit (in the
10391 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +000010392 if (N->getOpcode() == ISD::ZERO_EXTEND)
10393 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +000010394 DAG.getConstant(APInt::getLowBitsSet(
10395 N->getValueSizeInBits(0), PromBits),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010396 dl, N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +000010397
10398 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
10399 "Invalid extension type");
Mehdi Amini9639d652015-07-09 02:09:20 +000010400 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0), DAG.getDataLayout());
Hal Finkel940ab932014-02-28 00:27:01 +000010401 SDValue ShiftCst =
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +000010402 DAG.getConstant(N->getValueSizeInBits(0) - PromBits, dl, ShiftAmountTy);
10403 return DAG.getNode(
10404 ISD::SRA, dl, N->getValueType(0),
10405 DAG.getNode(ISD::SHL, dl, N->getValueType(0), N->getOperand(0), ShiftCst),
10406 ShiftCst);
Hal Finkel940ab932014-02-28 00:27:01 +000010407}
10408
Nemanja Ivanovic44513e52016-07-05 09:22:29 +000010409SDValue PPCTargetLowering::DAGCombineBuildVector(SDNode *N,
10410 DAGCombinerInfo &DCI) const {
10411 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
10412 "Should be called with a BUILD_VECTOR node");
10413
10414 SelectionDAG &DAG = DCI.DAG;
10415 SDLoc dl(N);
10416 if (N->getValueType(0) != MVT::v2f64 || !Subtarget.hasVSX())
10417 return SDValue();
10418
10419 // Looking for:
10420 // (build_vector ([su]int_to_fp (extractelt 0)), [su]int_to_fp (extractelt 1))
10421 if (N->getOperand(0).getOpcode() != ISD::SINT_TO_FP &&
10422 N->getOperand(0).getOpcode() != ISD::UINT_TO_FP)
10423 return SDValue();
10424 if (N->getOperand(1).getOpcode() != ISD::SINT_TO_FP &&
10425 N->getOperand(1).getOpcode() != ISD::UINT_TO_FP)
10426 return SDValue();
10427 if (N->getOperand(0).getOpcode() != N->getOperand(1).getOpcode())
10428 return SDValue();
10429
10430 SDValue Ext1 = N->getOperand(0).getOperand(0);
10431 SDValue Ext2 = N->getOperand(1).getOperand(0);
10432 if(Ext1.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
10433 Ext2.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10434 return SDValue();
10435
10436 ConstantSDNode *Ext1Op = dyn_cast<ConstantSDNode>(Ext1.getOperand(1));
10437 ConstantSDNode *Ext2Op = dyn_cast<ConstantSDNode>(Ext2.getOperand(1));
10438 if (!Ext1Op || !Ext2Op)
10439 return SDValue();
10440 if (Ext1.getValueType() != MVT::i32 ||
10441 Ext2.getValueType() != MVT::i32)
10442 if (Ext1.getOperand(0) != Ext2.getOperand(0))
10443 return SDValue();
10444
10445 int FirstElem = Ext1Op->getZExtValue();
10446 int SecondElem = Ext2Op->getZExtValue();
10447 int SubvecIdx;
10448 if (FirstElem == 0 && SecondElem == 1)
10449 SubvecIdx = Subtarget.isLittleEndian() ? 1 : 0;
10450 else if (FirstElem == 2 && SecondElem == 3)
10451 SubvecIdx = Subtarget.isLittleEndian() ? 0 : 1;
10452 else
10453 return SDValue();
10454
10455 SDValue SrcVec = Ext1.getOperand(0);
10456 auto NodeType = (N->getOperand(1).getOpcode() == ISD::SINT_TO_FP) ?
10457 PPCISD::SINT_VEC_TO_FP : PPCISD::UINT_VEC_TO_FP;
10458 return DAG.getNode(NodeType, dl, MVT::v2f64,
10459 SrcVec, DAG.getIntPtrConstant(SubvecIdx, dl));
10460}
10461
Hal Finkel5efb9182015-01-06 06:01:57 +000010462SDValue PPCTargetLowering::combineFPToIntToFP(SDNode *N,
10463 DAGCombinerInfo &DCI) const {
10464 assert((N->getOpcode() == ISD::SINT_TO_FP ||
10465 N->getOpcode() == ISD::UINT_TO_FP) &&
10466 "Need an int -> FP conversion node here");
10467
10468 if (!Subtarget.has64BitSupport())
10469 return SDValue();
10470
10471 SelectionDAG &DAG = DCI.DAG;
10472 SDLoc dl(N);
10473 SDValue Op(N, 0);
10474
10475 // Don't handle ppc_fp128 here or i1 conversions.
10476 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
10477 return SDValue();
10478 if (Op.getOperand(0).getValueType() == MVT::i1)
10479 return SDValue();
10480
10481 // For i32 intermediate values, unfortunately, the conversion functions
10482 // leave the upper 32 bits of the value are undefined. Within the set of
10483 // scalar instructions, we have no method for zero- or sign-extending the
10484 // value. Thus, we cannot handle i32 intermediate values here.
10485 if (Op.getOperand(0).getValueType() == MVT::i32)
10486 return SDValue();
10487
10488 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
10489 "UINT_TO_FP is supported only with FPCVT");
10490
10491 // If we have FCFIDS, then use it when converting to single-precision.
10492 // Otherwise, convert to double-precision and then round.
Eric Christophercccae792015-01-30 22:02:31 +000010493 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
10494 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS
10495 : PPCISD::FCFIDS)
10496 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU
10497 : PPCISD::FCFID);
10498 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
10499 ? MVT::f32
10500 : MVT::f64;
Hal Finkel5efb9182015-01-06 06:01:57 +000010501
10502 // If we're converting from a float, to an int, and back to a float again,
10503 // then we don't need the store/load pair at all.
10504 if ((Op.getOperand(0).getOpcode() == ISD::FP_TO_UINT &&
10505 Subtarget.hasFPCVT()) ||
10506 (Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT)) {
10507 SDValue Src = Op.getOperand(0).getOperand(0);
10508 if (Src.getValueType() == MVT::f32) {
10509 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
10510 DCI.AddToWorklist(Src.getNode());
Hal Finkelbe78c252015-08-20 01:18:20 +000010511 } else if (Src.getValueType() != MVT::f64) {
10512 // Make sure that we don't pick up a ppc_fp128 source value.
10513 return SDValue();
Hal Finkel5efb9182015-01-06 06:01:57 +000010514 }
10515
10516 unsigned FCTOp =
10517 Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
10518 PPCISD::FCTIDUZ;
10519
10520 SDValue Tmp = DAG.getNode(FCTOp, dl, MVT::f64, Src);
10521 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Tmp);
10522
10523 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) {
10524 FP = DAG.getNode(ISD::FP_ROUND, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010525 MVT::f32, FP, DAG.getIntPtrConstant(0, dl));
Hal Finkel5efb9182015-01-06 06:01:57 +000010526 DCI.AddToWorklist(FP.getNode());
10527 }
10528
10529 return FP;
10530 }
10531
10532 return SDValue();
10533}
10534
Bill Schmidtfae5d712014-12-09 16:35:51 +000010535// expandVSXLoadForLE - Convert VSX loads (which may be intrinsics for
10536// builtins) into loads with swaps.
10537SDValue PPCTargetLowering::expandVSXLoadForLE(SDNode *N,
10538 DAGCombinerInfo &DCI) const {
10539 SelectionDAG &DAG = DCI.DAG;
10540 SDLoc dl(N);
10541 SDValue Chain;
10542 SDValue Base;
10543 MachineMemOperand *MMO;
10544
10545 switch (N->getOpcode()) {
10546 default:
10547 llvm_unreachable("Unexpected opcode for little endian VSX load");
10548 case ISD::LOAD: {
10549 LoadSDNode *LD = cast<LoadSDNode>(N);
10550 Chain = LD->getChain();
10551 Base = LD->getBasePtr();
10552 MMO = LD->getMemOperand();
10553 // If the MMO suggests this isn't a load of a full vector, leave
10554 // things alone. For a built-in, we have to make the change for
10555 // correctness, so if there is a size problem that will be a bug.
10556 if (MMO->getSize() < 16)
10557 return SDValue();
10558 break;
10559 }
10560 case ISD::INTRINSIC_W_CHAIN: {
10561 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
10562 Chain = Intrin->getChain();
Nemanja Ivanovic7df26c92015-06-30 20:01:16 +000010563 // Similarly to the store case below, Intrin->getBasePtr() doesn't get
Nemanja Ivanovic9c8d4cf2015-06-30 19:45:45 +000010564 // us what we want. Get operand 2 instead.
Nemanja Ivanovic9c8d4cf2015-06-30 19:45:45 +000010565 Base = Intrin->getOperand(2);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010566 MMO = Intrin->getMemOperand();
10567 break;
10568 }
10569 }
10570
10571 MVT VecTy = N->getValueType(0).getSimpleVT();
10572 SDValue LoadOps[] = { Chain, Base };
10573 SDValue Load = DAG.getMemIntrinsicNode(PPCISD::LXVD2X, dl,
Nirav Dave1f51c332016-04-15 15:01:38 +000010574 DAG.getVTList(MVT::v2f64, MVT::Other),
10575 LoadOps, MVT::v2f64, MMO);
10576
Bill Schmidtfae5d712014-12-09 16:35:51 +000010577 DCI.AddToWorklist(Load.getNode());
10578 Chain = Load.getValue(1);
Nirav Dave1f51c332016-04-15 15:01:38 +000010579 SDValue Swap = DAG.getNode(
10580 PPCISD::XXSWAPD, dl, DAG.getVTList(MVT::v2f64, MVT::Other), Chain, Load);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010581 DCI.AddToWorklist(Swap.getNode());
Nirav Dave1f51c332016-04-15 15:01:38 +000010582
10583 // Add a bitcast if the resulting load type doesn't match v2f64.
10584 if (VecTy != MVT::v2f64) {
10585 SDValue N = DAG.getNode(ISD::BITCAST, dl, VecTy, Swap);
10586 DCI.AddToWorklist(N.getNode());
10587 // Package {bitcast value, swap's chain} to match Load's shape.
10588 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(VecTy, MVT::Other),
10589 N, Swap.getValue(1));
10590 }
10591
Bill Schmidtfae5d712014-12-09 16:35:51 +000010592 return Swap;
10593}
10594
10595// expandVSXStoreForLE - Convert VSX stores (which may be intrinsics for
10596// builtins) into stores with swaps.
10597SDValue PPCTargetLowering::expandVSXStoreForLE(SDNode *N,
10598 DAGCombinerInfo &DCI) const {
10599 SelectionDAG &DAG = DCI.DAG;
10600 SDLoc dl(N);
10601 SDValue Chain;
10602 SDValue Base;
10603 unsigned SrcOpnd;
10604 MachineMemOperand *MMO;
10605
10606 switch (N->getOpcode()) {
10607 default:
10608 llvm_unreachable("Unexpected opcode for little endian VSX store");
10609 case ISD::STORE: {
10610 StoreSDNode *ST = cast<StoreSDNode>(N);
10611 Chain = ST->getChain();
10612 Base = ST->getBasePtr();
10613 MMO = ST->getMemOperand();
10614 SrcOpnd = 1;
10615 // If the MMO suggests this isn't a store of a full vector, leave
10616 // things alone. For a built-in, we have to make the change for
10617 // correctness, so if there is a size problem that will be a bug.
10618 if (MMO->getSize() < 16)
10619 return SDValue();
10620 break;
10621 }
10622 case ISD::INTRINSIC_VOID: {
10623 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
10624 Chain = Intrin->getChain();
10625 // Intrin->getBasePtr() oddly does not get what we want.
10626 Base = Intrin->getOperand(3);
10627 MMO = Intrin->getMemOperand();
10628 SrcOpnd = 2;
10629 break;
10630 }
10631 }
10632
10633 SDValue Src = N->getOperand(SrcOpnd);
10634 MVT VecTy = Src.getValueType().getSimpleVT();
Nirav Dave1f51c332016-04-15 15:01:38 +000010635
10636 // All stores are done as v2f64 and possible bit cast.
10637 if (VecTy != MVT::v2f64) {
10638 Src = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Src);
10639 DCI.AddToWorklist(Src.getNode());
10640 }
10641
Bill Schmidtfae5d712014-12-09 16:35:51 +000010642 SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl,
Nirav Dave1f51c332016-04-15 15:01:38 +000010643 DAG.getVTList(MVT::v2f64, MVT::Other), Chain, Src);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010644 DCI.AddToWorklist(Swap.getNode());
10645 Chain = Swap.getValue(1);
10646 SDValue StoreOps[] = { Chain, Swap, Base };
10647 SDValue Store = DAG.getMemIntrinsicNode(PPCISD::STXVD2X, dl,
10648 DAG.getVTList(MVT::Other),
10649 StoreOps, VecTy, MMO);
10650 DCI.AddToWorklist(Store.getNode());
10651 return Store;
10652}
10653
Duncan Sandsdc2dac12008-11-24 14:53:14 +000010654SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
10655 DAGCombinerInfo &DCI) const {
Chris Lattnerf4184352006-03-01 04:57:39 +000010656 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +000010657 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +000010658 switch (N->getOpcode()) {
10659 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +000010660 case PPCISD::SHL:
Artyom Skrobov314ee042015-11-25 19:41:11 +000010661 if (isNullConstant(N->getOperand(0))) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +000010662 return N->getOperand(0);
Chris Lattner3c48ea52006-09-19 05:22:59 +000010663 break;
10664 case PPCISD::SRL:
Artyom Skrobov314ee042015-11-25 19:41:11 +000010665 if (isNullConstant(N->getOperand(0))) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +000010666 return N->getOperand(0);
Chris Lattner3c48ea52006-09-19 05:22:59 +000010667 break;
10668 case PPCISD::SRA:
10669 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +000010670 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +000010671 C->isAllOnesValue()) // -1 >>s V -> -1.
10672 return N->getOperand(0);
10673 }
10674 break;
Hal Finkel940ab932014-02-28 00:27:01 +000010675 case ISD::SIGN_EXTEND:
10676 case ISD::ZERO_EXTEND:
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +000010677 case ISD::ANY_EXTEND:
Hal Finkel940ab932014-02-28 00:27:01 +000010678 return DAGCombineExtBoolTrunc(N, DCI);
10679 case ISD::TRUNCATE:
10680 case ISD::SETCC:
10681 case ISD::SELECT_CC:
10682 return DAGCombineTruncBoolExt(N, DCI);
Chris Lattnerf4184352006-03-01 04:57:39 +000010683 case ISD::SINT_TO_FP:
Hal Finkel5efb9182015-01-06 06:01:57 +000010684 case ISD::UINT_TO_FP:
10685 return combineFPToIntToFP(N, DCI);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010686 case ISD::STORE: {
Chris Lattner27f53452006-03-01 05:50:56 +000010687 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
Eric Christophercccae792015-01-30 22:02:31 +000010688 if (Subtarget.hasSTFIWX() && !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +000010689 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +000010690 N->getOperand(1).getValueType() == MVT::i32 &&
10691 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010692 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +000010693 if (Val.getValueType() == MVT::f32) {
10694 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010695 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +000010696 }
Owen Anderson9f944592009-08-11 20:47:22 +000010697 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010698 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +000010699
Hal Finkel60c75102013-04-01 15:37:53 +000010700 SDValue Ops[] = {
10701 N->getOperand(0), Val, N->getOperand(2),
10702 DAG.getValueType(N->getOperand(1).getValueType())
10703 };
10704
10705 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +000010706 DAG.getVTList(MVT::Other), Ops,
Hal Finkel60c75102013-04-01 15:37:53 +000010707 cast<StoreSDNode>(N)->getMemoryVT(),
10708 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +000010709 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +000010710 return Val;
10711 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010712
Chris Lattnera7976d32006-07-10 20:56:58 +000010713 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +000010714 if (cast<StoreSDNode>(N)->isUnindexed() &&
10715 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +000010716 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +000010717 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +000010718 N->getOperand(1).getValueType() == MVT::i16 ||
Eric Christophercccae792015-01-30 22:02:31 +000010719 (Subtarget.hasLDBRX() && Subtarget.isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +000010720 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010721 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +000010722 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +000010723 if (BSwapOp.getValueType() == MVT::i16)
10724 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +000010725
Dan Gohman48b185d2009-09-25 20:36:54 +000010726 SDValue Ops[] = {
10727 N->getOperand(0), BSwapOp, N->getOperand(2),
10728 DAG.getValueType(N->getOperand(1).getValueType())
10729 };
10730 return
10731 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +000010732 Ops, cast<StoreSDNode>(N)->getMemoryVT(),
Dan Gohman48b185d2009-09-25 20:36:54 +000010733 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +000010734 }
Bill Schmidtfae5d712014-12-09 16:35:51 +000010735
10736 // For little endian, VSX stores require generating xxswapd/lxvd2x.
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +000010737 // Not needed on ISA 3.0 based CPUs since we have a non-permuting store.
Bill Schmidtfae5d712014-12-09 16:35:51 +000010738 EVT VT = N->getOperand(1).getValueType();
10739 if (VT.isSimple()) {
10740 MVT StoreVT = VT.getSimpleVT();
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +000010741 if (Subtarget.needsSwapsForVSXMemOps() &&
Bill Schmidtfae5d712014-12-09 16:35:51 +000010742 (StoreVT == MVT::v2f64 || StoreVT == MVT::v2i64 ||
10743 StoreVT == MVT::v4f32 || StoreVT == MVT::v4i32))
10744 return expandVSXStoreForLE(N, DCI);
10745 }
Chris Lattnera7976d32006-07-10 20:56:58 +000010746 break;
Bill Schmidtfae5d712014-12-09 16:35:51 +000010747 }
Hal Finkelcf2e9082013-05-24 23:00:14 +000010748 case ISD::LOAD: {
10749 LoadSDNode *LD = cast<LoadSDNode>(N);
10750 EVT VT = LD->getValueType(0);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010751
10752 // For little endian, VSX loads require generating lxvd2x/xxswapd.
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +000010753 // Not needed on ISA 3.0 based CPUs since we have a non-permuting load.
Bill Schmidtfae5d712014-12-09 16:35:51 +000010754 if (VT.isSimple()) {
10755 MVT LoadVT = VT.getSimpleVT();
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +000010756 if (Subtarget.needsSwapsForVSXMemOps() &&
Bill Schmidtfae5d712014-12-09 16:35:51 +000010757 (LoadVT == MVT::v2f64 || LoadVT == MVT::v2i64 ||
10758 LoadVT == MVT::v4f32 || LoadVT == MVT::v4i32))
10759 return expandVSXLoadForLE(N, DCI);
10760 }
10761
Hal Finkel851b33a2016-03-31 02:56:05 +000010762 // We sometimes end up with a 64-bit integer load, from which we extract
10763 // two single-precision floating-point numbers. This happens with
10764 // std::complex<float>, and other similar structures, because of the way we
10765 // canonicalize structure copies. However, if we lack direct moves,
10766 // then the final bitcasts from the extracted integer values to the
10767 // floating-point numbers turn into store/load pairs. Even with direct moves,
10768 // just loading the two floating-point numbers is likely better.
10769 auto ReplaceTwoFloatLoad = [&]() {
10770 if (VT != MVT::i64)
10771 return false;
10772
10773 if (LD->getExtensionType() != ISD::NON_EXTLOAD ||
10774 LD->isVolatile())
10775 return false;
10776
10777 // We're looking for a sequence like this:
10778 // t13: i64,ch = load<LD8[%ref.tmp]> t0, t6, undef:i64
10779 // t16: i64 = srl t13, Constant:i32<32>
10780 // t17: i32 = truncate t16
10781 // t18: f32 = bitcast t17
10782 // t19: i32 = truncate t13
10783 // t20: f32 = bitcast t19
10784
10785 if (!LD->hasNUsesOfValue(2, 0))
10786 return false;
10787
10788 auto UI = LD->use_begin();
10789 while (UI.getUse().getResNo() != 0) ++UI;
10790 SDNode *Trunc = *UI++;
10791 while (UI.getUse().getResNo() != 0) ++UI;
10792 SDNode *RightShift = *UI;
10793 if (Trunc->getOpcode() != ISD::TRUNCATE)
10794 std::swap(Trunc, RightShift);
10795
10796 if (Trunc->getOpcode() != ISD::TRUNCATE ||
10797 Trunc->getValueType(0) != MVT::i32 ||
10798 !Trunc->hasOneUse())
10799 return false;
10800 if (RightShift->getOpcode() != ISD::SRL ||
10801 !isa<ConstantSDNode>(RightShift->getOperand(1)) ||
10802 RightShift->getConstantOperandVal(1) != 32 ||
10803 !RightShift->hasOneUse())
10804 return false;
10805
10806 SDNode *Trunc2 = *RightShift->use_begin();
10807 if (Trunc2->getOpcode() != ISD::TRUNCATE ||
10808 Trunc2->getValueType(0) != MVT::i32 ||
10809 !Trunc2->hasOneUse())
10810 return false;
10811
10812 SDNode *Bitcast = *Trunc->use_begin();
10813 SDNode *Bitcast2 = *Trunc2->use_begin();
10814
10815 if (Bitcast->getOpcode() != ISD::BITCAST ||
10816 Bitcast->getValueType(0) != MVT::f32)
10817 return false;
NAKAMURA Takumiae7c97d2016-06-20 00:49:20 +000010818 if (Bitcast2->getOpcode() != ISD::BITCAST ||
Hal Finkel851b33a2016-03-31 02:56:05 +000010819 Bitcast2->getValueType(0) != MVT::f32)
10820 return false;
10821
10822 if (Subtarget.isLittleEndian())
10823 std::swap(Bitcast, Bitcast2);
10824
10825 // Bitcast has the second float (in memory-layout order) and Bitcast2
10826 // has the first one.
10827
10828 SDValue BasePtr = LD->getBasePtr();
10829 if (LD->isIndexed()) {
10830 assert(LD->getAddressingMode() == ISD::PRE_INC &&
10831 "Non-pre-inc AM on PPC?");
10832 BasePtr =
10833 DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
10834 LD->getOffset());
10835 }
10836
Justin Lebar9c375812016-07-15 18:27:10 +000010837 auto MMOFlags =
10838 LD->getMemOperand()->getFlags() & ~MachineMemOperand::MOVolatile;
10839 SDValue FloatLoad = DAG.getLoad(MVT::f32, dl, LD->getChain(), BasePtr,
10840 LD->getPointerInfo(), LD->getAlignment(),
10841 MMOFlags, LD->getAAInfo());
Hal Finkel851b33a2016-03-31 02:56:05 +000010842 SDValue AddPtr =
10843 DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(),
10844 BasePtr, DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +000010845 SDValue FloatLoad2 = DAG.getLoad(
10846 MVT::f32, dl, SDValue(FloatLoad.getNode(), 1), AddPtr,
10847 LD->getPointerInfo().getWithOffset(4),
10848 MinAlign(LD->getAlignment(), 4), MMOFlags, LD->getAAInfo());
Hal Finkel851b33a2016-03-31 02:56:05 +000010849
10850 if (LD->isIndexed()) {
NAKAMURA Takumife1202c2016-06-20 00:37:41 +000010851 // Note that DAGCombine should re-form any pre-increment load(s) from
10852 // what is produced here if that makes sense.
Hal Finkel851b33a2016-03-31 02:56:05 +000010853 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), BasePtr);
10854 }
10855
10856 DCI.CombineTo(Bitcast2, FloatLoad);
10857 DCI.CombineTo(Bitcast, FloatLoad2);
10858
10859 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, LD->isIndexed() ? 2 : 1),
10860 SDValue(FloatLoad2.getNode(), 1));
10861 return true;
10862 };
10863
10864 if (ReplaceTwoFloatLoad())
10865 return SDValue(N, 0);
10866
Hal Finkelc93a9a22015-02-25 01:06:45 +000010867 EVT MemVT = LD->getMemoryVT();
10868 Type *Ty = MemVT.getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +000010869 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
Hal Finkelc93a9a22015-02-25 01:06:45 +000010870 Type *STy = MemVT.getScalarType().getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +000010871 unsigned ScalarABIAlignment = DAG.getDataLayout().getABITypeAlignment(STy);
Hal Finkelc93a9a22015-02-25 01:06:45 +000010872 if (LD->isUnindexed() && VT.isVector() &&
10873 ((Subtarget.hasAltivec() && ISD::isNON_EXTLoad(N) &&
10874 // P8 and later hardware should just use LOAD.
10875 !Subtarget.hasP8Vector() && (VT == MVT::v16i8 || VT == MVT::v8i16 ||
10876 VT == MVT::v4i32 || VT == MVT::v4f32)) ||
10877 (Subtarget.hasQPX() && (VT == MVT::v4f64 || VT == MVT::v4f32) &&
10878 LD->getAlignment() >= ScalarABIAlignment)) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +000010879 LD->getAlignment() < ABIAlignment) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000010880 // This is a type-legal unaligned Altivec or QPX load.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010881 SDValue Chain = LD->getChain();
10882 SDValue Ptr = LD->getBasePtr();
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010883 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelcf2e9082013-05-24 23:00:14 +000010884
10885 // This implements the loading of unaligned vectors as described in
10886 // the venerable Apple Velocity Engine overview. Specifically:
10887 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
10888 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
10889 //
10890 // The general idea is to expand a sequence of one or more unaligned
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010891 // loads into an alignment-based permutation-control instruction (lvsl
10892 // or lvsr), a series of regular vector loads (which always truncate
10893 // their input address to an aligned address), and a series of
10894 // permutations. The results of these permutations are the requested
10895 // loaded values. The trick is that the last "extra" load is not taken
10896 // from the address you might suspect (sizeof(vector) bytes after the
10897 // last requested load), but rather sizeof(vector) - 1 bytes after the
10898 // last requested vector. The point of this is to avoid a page fault if
10899 // the base address happened to be aligned. This works because if the
10900 // base address is aligned, then adding less than a full vector length
10901 // will cause the last vector in the sequence to be (re)loaded.
10902 // Otherwise, the next vector will be fetched as you might suspect was
10903 // necessary.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010904
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010905 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +000010906 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010907 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
10908 // optimization later.
Hal Finkelc93a9a22015-02-25 01:06:45 +000010909 Intrinsic::ID Intr, IntrLD, IntrPerm;
10910 MVT PermCntlTy, PermTy, LDTy;
10911 if (Subtarget.hasAltivec()) {
10912 Intr = isLittleEndian ? Intrinsic::ppc_altivec_lvsr :
10913 Intrinsic::ppc_altivec_lvsl;
10914 IntrLD = Intrinsic::ppc_altivec_lvx;
10915 IntrPerm = Intrinsic::ppc_altivec_vperm;
10916 PermCntlTy = MVT::v16i8;
10917 PermTy = MVT::v4i32;
10918 LDTy = MVT::v4i32;
10919 } else {
10920 Intr = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlpcld :
10921 Intrinsic::ppc_qpx_qvlpcls;
10922 IntrLD = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlfd :
10923 Intrinsic::ppc_qpx_qvlfs;
10924 IntrPerm = Intrinsic::ppc_qpx_qvfperm;
10925 PermCntlTy = MVT::v4f64;
10926 PermTy = MVT::v4f64;
10927 LDTy = MemVT.getSimpleVT();
10928 }
10929
10930 SDValue PermCntl = BuildIntrinsicOp(Intr, Ptr, DAG, dl, PermCntlTy);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010931
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010932 // Create the new MMO for the new base load. It is like the original MMO,
10933 // but represents an area in memory almost twice the vector size centered
10934 // on the original address. If the address is unaligned, we might start
10935 // reading up to (sizeof(vector)-1) bytes below the address of the
10936 // original unaligned load.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010937 MachineFunction &MF = DAG.getMachineFunction();
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010938 MachineMemOperand *BaseMMO =
Hal Finkel99d95322015-09-03 21:12:15 +000010939 MF.getMachineMemOperand(LD->getMemOperand(),
10940 -(long)MemVT.getStoreSize()+1,
Hal Finkelc93a9a22015-02-25 01:06:45 +000010941 2*MemVT.getStoreSize()-1);
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010942
10943 // Create the new base load.
Mehdi Amini44ede332015-07-09 02:09:04 +000010944 SDValue LDXIntID =
10945 DAG.getTargetConstant(IntrLD, dl, getPointerTy(MF.getDataLayout()));
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010946 SDValue BaseLoadOps[] = { Chain, LDXIntID, Ptr };
10947 SDValue BaseLoad =
10948 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
Hal Finkelc93a9a22015-02-25 01:06:45 +000010949 DAG.getVTList(PermTy, MVT::Other),
10950 BaseLoadOps, LDTy, BaseMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010951
10952 // Note that the value of IncOffset (which is provided to the next
10953 // load's pointer info offset value, and thus used to calculate the
10954 // alignment), and the value of IncValue (which is actually used to
10955 // increment the pointer value) are different! This is because we
10956 // require the next load to appear to be aligned, even though it
10957 // is actually offset from the base pointer by a lesser amount.
10958 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +000010959 int IncValue = IncOffset;
10960
10961 // Walk (both up and down) the chain looking for another load at the real
10962 // (aligned) offset (the alignment of the other load does not matter in
10963 // this case). If found, then do not use the offset reduction trick, as
10964 // that will prevent the loads from being later combined (as they would
10965 // otherwise be duplicates).
10966 if (!findConsecutiveLoad(LD, DAG))
10967 --IncValue;
10968
Mehdi Amini44ede332015-07-09 02:09:04 +000010969 SDValue Increment =
10970 DAG.getConstant(IncValue, dl, getPointerTy(MF.getDataLayout()));
Hal Finkelcf2e9082013-05-24 23:00:14 +000010971 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
10972
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010973 MachineMemOperand *ExtraMMO =
10974 MF.getMachineMemOperand(LD->getMemOperand(),
Hal Finkelc93a9a22015-02-25 01:06:45 +000010975 1, 2*MemVT.getStoreSize()-1);
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010976 SDValue ExtraLoadOps[] = { Chain, LDXIntID, Ptr };
Hal Finkelcf2e9082013-05-24 23:00:14 +000010977 SDValue ExtraLoad =
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010978 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
Hal Finkelc93a9a22015-02-25 01:06:45 +000010979 DAG.getVTList(PermTy, MVT::Other),
10980 ExtraLoadOps, LDTy, ExtraMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010981
10982 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
10983 BaseLoad.getValue(1), ExtraLoad.getValue(1));
10984
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010985 // Because vperm has a big-endian bias, we must reverse the order
10986 // of the input vectors and complement the permute control vector
10987 // when generating little endian code. We have already handled the
10988 // latter by using lvsr instead of lvsl, so just reverse BaseLoad
10989 // and ExtraLoad here.
10990 SDValue Perm;
10991 if (isLittleEndian)
Hal Finkelc93a9a22015-02-25 01:06:45 +000010992 Perm = BuildIntrinsicOp(IntrPerm,
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010993 ExtraLoad, BaseLoad, PermCntl, DAG, dl);
10994 else
Hal Finkelc93a9a22015-02-25 01:06:45 +000010995 Perm = BuildIntrinsicOp(IntrPerm,
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010996 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010997
Hal Finkelc93a9a22015-02-25 01:06:45 +000010998 if (VT != PermTy)
10999 Perm = Subtarget.hasAltivec() ?
11000 DAG.getNode(ISD::BITCAST, dl, VT, Perm) :
11001 DAG.getNode(ISD::FP_ROUND, dl, VT, Perm, // QPX
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011002 DAG.getTargetConstant(1, dl, MVT::i64));
Hal Finkelc93a9a22015-02-25 01:06:45 +000011003 // second argument is 1 because this rounding
11004 // is always exact.
Hal Finkelcf2e9082013-05-24 23:00:14 +000011005
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000011006 // The output of the permutation is our loaded result, the TokenFactor is
11007 // our new chain.
11008 DCI.CombineTo(N, Perm, TF);
Hal Finkelcf2e9082013-05-24 23:00:14 +000011009 return SDValue(N, 0);
11010 }
11011 }
11012 break;
Eric Christophercccae792015-01-30 22:02:31 +000011013 case ISD::INTRINSIC_WO_CHAIN: {
11014 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelc93a9a22015-02-25 01:06:45 +000011015 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
Eric Christophercccae792015-01-30 22:02:31 +000011016 Intrinsic::ID Intr = (isLittleEndian ? Intrinsic::ppc_altivec_lvsr
11017 : Intrinsic::ppc_altivec_lvsl);
Hal Finkelc93a9a22015-02-25 01:06:45 +000011018 if ((IID == Intr ||
11019 IID == Intrinsic::ppc_qpx_qvlpcld ||
11020 IID == Intrinsic::ppc_qpx_qvlpcls) &&
11021 N->getOperand(1)->getOpcode() == ISD::ADD) {
Eric Christophercccae792015-01-30 22:02:31 +000011022 SDValue Add = N->getOperand(1);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000011023
Hal Finkelc93a9a22015-02-25 01:06:45 +000011024 int Bits = IID == Intrinsic::ppc_qpx_qvlpcld ?
11025 5 /* 32 byte alignment */ : 4 /* 16 byte alignment */;
11026
Sanjay Patel5f6bb6c2016-09-14 15:43:44 +000011027 if (DAG.MaskedValueIsZero(Add->getOperand(1),
11028 APInt::getAllOnesValue(Bits /* alignment */)
11029 .zext(Add.getScalarValueSizeInBits()))) {
Eric Christophercccae792015-01-30 22:02:31 +000011030 SDNode *BasePtr = Add->getOperand(0).getNode();
11031 for (SDNode::use_iterator UI = BasePtr->use_begin(),
11032 UE = BasePtr->use_end();
11033 UI != UE; ++UI) {
11034 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
Hal Finkelc93a9a22015-02-25 01:06:45 +000011035 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() == IID) {
Eric Christophercccae792015-01-30 22:02:31 +000011036 // We've found another LVSL/LVSR, and this address is an aligned
11037 // multiple of that one. The results will be the same, so use the
11038 // one we've just found instead.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000011039
Eric Christophercccae792015-01-30 22:02:31 +000011040 return SDValue(*UI, 0);
11041 }
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000011042 }
11043 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000011044
11045 if (isa<ConstantSDNode>(Add->getOperand(1))) {
11046 SDNode *BasePtr = Add->getOperand(0).getNode();
11047 for (SDNode::use_iterator UI = BasePtr->use_begin(),
11048 UE = BasePtr->use_end(); UI != UE; ++UI) {
11049 if (UI->getOpcode() == ISD::ADD &&
11050 isa<ConstantSDNode>(UI->getOperand(1)) &&
11051 (cast<ConstantSDNode>(Add->getOperand(1))->getZExtValue() -
11052 cast<ConstantSDNode>(UI->getOperand(1))->getZExtValue()) %
Aaron Ballman5561ed42015-02-25 13:05:24 +000011053 (1ULL << Bits) == 0) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000011054 SDNode *OtherAdd = *UI;
11055 for (SDNode::use_iterator VI = OtherAdd->use_begin(),
11056 VE = OtherAdd->use_end(); VI != VE; ++VI) {
11057 if (VI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
11058 cast<ConstantSDNode>(VI->getOperand(0))->getZExtValue() == IID) {
11059 return SDValue(*VI, 0);
11060 }
11061 }
11062 }
11063 }
11064 }
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000011065 }
11066 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +000011067
11068 break;
Bill Schmidtfae5d712014-12-09 16:35:51 +000011069 case ISD::INTRINSIC_W_CHAIN: {
11070 // For little endian, VSX loads require generating lxvd2x/xxswapd.
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +000011071 // Not needed on ISA 3.0 based CPUs since we have a non-permuting load.
11072 if (Subtarget.needsSwapsForVSXMemOps()) {
Bill Schmidtfae5d712014-12-09 16:35:51 +000011073 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
11074 default:
11075 break;
11076 case Intrinsic::ppc_vsx_lxvw4x:
11077 case Intrinsic::ppc_vsx_lxvd2x:
11078 return expandVSXLoadForLE(N, DCI);
11079 }
11080 }
11081 break;
11082 }
11083 case ISD::INTRINSIC_VOID: {
11084 // For little endian, VSX stores require generating xxswapd/stxvd2x.
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +000011085 // Not needed on ISA 3.0 based CPUs since we have a non-permuting store.
11086 if (Subtarget.needsSwapsForVSXMemOps()) {
Bill Schmidtfae5d712014-12-09 16:35:51 +000011087 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
11088 default:
11089 break;
11090 case Intrinsic::ppc_vsx_stxvw4x:
11091 case Intrinsic::ppc_vsx_stxvd2x:
11092 return expandVSXStoreForLE(N, DCI);
11093 }
11094 }
11095 break;
11096 }
Chris Lattnera7976d32006-07-10 20:56:58 +000011097 case ISD::BSWAP:
11098 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +000011099 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +000011100 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +000011101 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
Eric Christophercccae792015-01-30 22:02:31 +000011102 (Subtarget.hasLDBRX() && Subtarget.isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +000011103 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011104 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +000011105 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +000011106 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011107 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +000011108 LD->getChain(), // Chain
11109 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +000011110 DAG.getValueType(N->getValueType(0)) // VT
11111 };
Dan Gohman48b185d2009-09-25 20:36:54 +000011112 SDValue BSLoad =
11113 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +000011114 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
11115 MVT::i64 : MVT::i32, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +000011116 Ops, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +000011117
Scott Michelcf0da6c2009-02-17 22:15:04 +000011118 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011119 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +000011120 if (N->getValueType(0) == MVT::i16)
11121 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011122
Chris Lattnera7976d32006-07-10 20:56:58 +000011123 // First, combine the bswap away. This makes the value produced by the
11124 // load dead.
11125 DCI.CombineTo(N, ResVal);
11126
11127 // Next, combine the load away, we give it a bogus result value but a real
11128 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +000011129 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +000011130
Chris Lattnera7976d32006-07-10 20:56:58 +000011131 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011132 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +000011133 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011134
Chris Lattner27f53452006-03-01 05:50:56 +000011135 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +000011136 case PPCISD::VCMP: {
11137 // If a VCMPo node already exists with exactly the same operands as this
11138 // node, use its result instead of this node (VCMPo computes both a CR6 and
11139 // a normal output).
11140 //
11141 if (!N->getOperand(0).hasOneUse() &&
11142 !N->getOperand(1).hasOneUse() &&
11143 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000011144
Chris Lattnerd4058a52006-03-31 06:02:07 +000011145 // Scan all of the users of the LHS, looking for VCMPo's that match.
Craig Topper062a2ba2014-04-25 05:30:21 +000011146 SDNode *VCMPoNode = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011147
Gabor Greiff304a7a2008-08-28 21:40:38 +000011148 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +000011149 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
11150 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +000011151 if (UI->getOpcode() == PPCISD::VCMPo &&
11152 UI->getOperand(1) == N->getOperand(1) &&
11153 UI->getOperand(2) == N->getOperand(2) &&
11154 UI->getOperand(0) == N->getOperand(0)) {
11155 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +000011156 break;
11157 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011158
Chris Lattner518834c2006-04-18 18:28:22 +000011159 // If there is no VCMPo node, or if the flag value has a single use, don't
11160 // transform this.
11161 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
11162 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011163
11164 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +000011165 // chain, this transformation is more complex. Note that multiple things
11166 // could use the value result, which we should ignore.
Craig Topper062a2ba2014-04-25 05:30:21 +000011167 SDNode *FlagUser = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011168 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Craig Topper062a2ba2014-04-25 05:30:21 +000011169 FlagUser == nullptr; ++UI) {
Chris Lattner518834c2006-04-18 18:28:22 +000011170 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +000011171 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +000011172 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011173 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +000011174 FlagUser = User;
11175 break;
11176 }
11177 }
11178 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011179
Ulrich Weigandd5ebc622013-07-03 17:05:42 +000011180 // If the user is a MFOCRF instruction, we know this is safe.
11181 // Otherwise we give up for right now.
11182 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011183 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +000011184 }
11185 break;
11186 }
Hal Finkel940ab932014-02-28 00:27:01 +000011187 case ISD::BRCOND: {
11188 SDValue Cond = N->getOperand(1);
11189 SDValue Target = N->getOperand(2);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +000011190
Hal Finkel940ab932014-02-28 00:27:01 +000011191 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
11192 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
11193 Intrinsic::ppc_is_decremented_ctr_nonzero) {
11194
11195 // We now need to make the intrinsic dead (it cannot be instruction
11196 // selected).
11197 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
11198 assert(Cond.getNode()->hasOneUse() &&
11199 "Counter decrement has more than one use");
11200
11201 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
11202 N->getOperand(0), Target);
11203 }
11204 }
11205 break;
Chris Lattner9754d142006-04-18 17:59:36 +000011206 case ISD::BR_CC: {
11207 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +000011208 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +000011209 // lowering is done pre-legalize, because the legalizer lowers the predicate
11210 // compare down to code that is difficult to reassemble.
11211 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011212 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +000011213
11214 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
11215 // value. If so, pass-through the AND to get to the intrinsic.
11216 if (LHS.getOpcode() == ISD::AND &&
11217 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
11218 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
11219 Intrinsic::ppc_is_decremented_ctr_nonzero &&
11220 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Artyom Skrobov314ee042015-11-25 19:41:11 +000011221 !isNullConstant(LHS.getOperand(1)))
Hal Finkel25c19922013-05-15 21:37:41 +000011222 LHS = LHS.getOperand(0);
11223
11224 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
11225 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
11226 Intrinsic::ppc_is_decremented_ctr_nonzero &&
11227 isa<ConstantSDNode>(RHS)) {
11228 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
11229 "Counter decrement comparison is not EQ or NE");
11230
11231 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
11232 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
11233 (CC == ISD::SETNE && !Val);
11234
11235 // We now need to make the intrinsic dead (it cannot be instruction
11236 // selected).
11237 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
11238 assert(LHS.getNode()->hasOneUse() &&
11239 "Counter decrement has more than one use");
11240
11241 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
11242 N->getOperand(0), N->getOperand(4));
11243 }
11244
Chris Lattner9754d142006-04-18 17:59:36 +000011245 int CompareOpc;
11246 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011247
Chris Lattner9754d142006-04-18 17:59:36 +000011248 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
11249 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +000011250 getVectorCompareInfo(LHS, CompareOpc, isDot, Subtarget)) {
Chris Lattner9754d142006-04-18 17:59:36 +000011251 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +000011252
Chris Lattner9754d142006-04-18 17:59:36 +000011253 // If this is a comparison against something other than 0/1, then we know
11254 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +000011255 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +000011256 if (Val != 0 && Val != 1) {
11257 if (CC == ISD::SETEQ) // Cond never true, remove branch.
11258 return N->getOperand(0);
11259 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +000011260 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +000011261 N->getOperand(0), N->getOperand(4));
11262 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011263
Chris Lattner9754d142006-04-18 17:59:36 +000011264 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011265
Chris Lattner9754d142006-04-18 17:59:36 +000011266 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011267 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +000011268 LHS.getOperand(2), // LHS of compare
11269 LHS.getOperand(3), // RHS of compare
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011270 DAG.getConstant(CompareOpc, dl, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +000011271 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +000011272 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +000011273 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011274
Chris Lattner9754d142006-04-18 17:59:36 +000011275 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011276 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +000011277 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +000011278 default: // Can't happen, don't crash on invalid number though.
11279 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011280 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +000011281 break;
11282 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011283 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +000011284 break;
11285 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011286 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +000011287 break;
11288 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011289 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +000011290 break;
11291 }
11292
Owen Anderson9f944592009-08-11 20:47:22 +000011293 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011294 DAG.getConstant(CompOpc, dl, MVT::i32),
Owen Anderson9f944592009-08-11 20:47:22 +000011295 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +000011296 N->getOperand(4), CompNode.getValue(1));
11297 }
11298 break;
11299 }
Nemanja Ivanovic44513e52016-07-05 09:22:29 +000011300 case ISD::BUILD_VECTOR:
11301 return DAGCombineBuildVector(N, DCI);
Chris Lattnerf4184352006-03-01 04:57:39 +000011302 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011303
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011304 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +000011305}
11306
Hal Finkel13d104b2014-12-11 18:37:52 +000011307SDValue
11308PPCTargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
11309 SelectionDAG &DAG,
11310 std::vector<SDNode *> *Created) const {
11311 // fold (sdiv X, pow2)
11312 EVT VT = N->getValueType(0);
Hal Finkel04b16b52014-12-23 08:38:50 +000011313 if (VT == MVT::i64 && !Subtarget.isPPC64())
11314 return SDValue();
Hal Finkel13d104b2014-12-11 18:37:52 +000011315 if ((VT != MVT::i32 && VT != MVT::i64) ||
11316 !(Divisor.isPowerOf2() || (-Divisor).isPowerOf2()))
11317 return SDValue();
11318
11319 SDLoc DL(N);
11320 SDValue N0 = N->getOperand(0);
11321
11322 bool IsNegPow2 = (-Divisor).isPowerOf2();
11323 unsigned Lg2 = (IsNegPow2 ? -Divisor : Divisor).countTrailingZeros();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011324 SDValue ShiftAmt = DAG.getConstant(Lg2, DL, VT);
Hal Finkel13d104b2014-12-11 18:37:52 +000011325
11326 SDValue Op = DAG.getNode(PPCISD::SRA_ADDZE, DL, VT, N0, ShiftAmt);
11327 if (Created)
11328 Created->push_back(Op.getNode());
11329
11330 if (IsNegPow2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011331 Op = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Op);
Hal Finkel13d104b2014-12-11 18:37:52 +000011332 if (Created)
11333 Created->push_back(Op.getNode());
11334 }
11335
11336 return Op;
11337}
11338
Chris Lattner4211ca92006-04-14 06:01:58 +000011339//===----------------------------------------------------------------------===//
11340// Inline Assembly Support
11341//===----------------------------------------------------------------------===//
11342
Jay Foada0653a32014-05-14 21:14:37 +000011343void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
11344 APInt &KnownZero,
11345 APInt &KnownOne,
11346 const SelectionDAG &DAG,
11347 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +000011348 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +000011349 switch (Op.getOpcode()) {
11350 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +000011351 case PPCISD::LBRX: {
11352 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +000011353 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +000011354 KnownZero = 0xFFFF0000;
11355 break;
11356 }
Chris Lattnerc5287c02006-04-02 06:26:07 +000011357 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011358 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +000011359 default: break;
11360 case Intrinsic::ppc_altivec_vcmpbfp_p:
11361 case Intrinsic::ppc_altivec_vcmpeqfp_p:
11362 case Intrinsic::ppc_altivec_vcmpequb_p:
11363 case Intrinsic::ppc_altivec_vcmpequh_p:
11364 case Intrinsic::ppc_altivec_vcmpequw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000011365 case Intrinsic::ppc_altivec_vcmpequd_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000011366 case Intrinsic::ppc_altivec_vcmpgefp_p:
11367 case Intrinsic::ppc_altivec_vcmpgtfp_p:
11368 case Intrinsic::ppc_altivec_vcmpgtsb_p:
11369 case Intrinsic::ppc_altivec_vcmpgtsh_p:
11370 case Intrinsic::ppc_altivec_vcmpgtsw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000011371 case Intrinsic::ppc_altivec_vcmpgtsd_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000011372 case Intrinsic::ppc_altivec_vcmpgtub_p:
11373 case Intrinsic::ppc_altivec_vcmpgtuh_p:
11374 case Intrinsic::ppc_altivec_vcmpgtuw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000011375 case Intrinsic::ppc_altivec_vcmpgtud_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000011376 KnownZero = ~1U; // All bits but the low one are known to be zero.
11377 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011378 }
Chris Lattnerc5287c02006-04-02 06:26:07 +000011379 }
11380 }
11381}
11382
Hal Finkel57725662015-01-03 17:58:24 +000011383unsigned PPCTargetLowering::getPrefLoopAlignment(MachineLoop *ML) const {
11384 switch (Subtarget.getDarwinDirective()) {
11385 default: break;
11386 case PPC::DIR_970:
11387 case PPC::DIR_PWR4:
11388 case PPC::DIR_PWR5:
11389 case PPC::DIR_PWR5X:
11390 case PPC::DIR_PWR6:
11391 case PPC::DIR_PWR6X:
11392 case PPC::DIR_PWR7:
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +000011393 case PPC::DIR_PWR8:
11394 case PPC::DIR_PWR9: {
Hal Finkel57725662015-01-03 17:58:24 +000011395 if (!ML)
11396 break;
11397
Eric Christophercccae792015-01-30 22:02:31 +000011398 const PPCInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel57725662015-01-03 17:58:24 +000011399
11400 // For small loops (between 5 and 8 instructions), align to a 32-byte
11401 // boundary so that the entire loop fits in one instruction-cache line.
11402 uint64_t LoopSize = 0;
11403 for (auto I = ML->block_begin(), IE = ML->block_end(); I != IE; ++I)
Chad Rosierbc9d4f92015-12-14 14:44:06 +000011404 for (auto J = (*I)->begin(), JE = (*I)->end(); J != JE; ++J) {
Sjoerd Meijer89217f82016-07-28 16:32:22 +000011405 LoopSize += TII->getInstSizeInBytes(*J);
Chad Rosierbc9d4f92015-12-14 14:44:06 +000011406 if (LoopSize > 32)
11407 break;
11408 }
Hal Finkel57725662015-01-03 17:58:24 +000011409
11410 if (LoopSize > 16 && LoopSize <= 32)
11411 return 5;
11412
11413 break;
11414 }
11415 }
11416
11417 return TargetLowering::getPrefLoopAlignment(ML);
11418}
Chris Lattnerc5287c02006-04-02 06:26:07 +000011419
Chris Lattnerd6855142007-03-25 02:14:49 +000011420/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +000011421/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +000011422PPCTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011423PPCTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000011424 if (Constraint.size() == 1) {
11425 switch (Constraint[0]) {
11426 default: break;
11427 case 'b':
11428 case 'r':
11429 case 'f':
Eric Christopherb979d512016-03-24 21:04:52 +000011430 case 'd':
Chris Lattnerd6855142007-03-25 02:14:49 +000011431 case 'v':
11432 case 'y':
11433 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +000011434 case 'Z':
11435 // FIXME: While Z does indicate a memory constraint, it specifically
11436 // indicates an r+r address (used in conjunction with the 'y' modifier
11437 // in the replacement string). Currently, we're forcing the base
11438 // register to be r0 in the asm printer (which is interpreted as zero)
11439 // and forming the complete address in the second register. This is
11440 // suboptimal.
11441 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000011442 }
Hal Finkel6aca2372014-03-02 18:23:39 +000011443 } else if (Constraint == "wc") { // individual CR bits.
11444 return C_RegisterClass;
Hal Finkel27774d92014-03-13 07:58:58 +000011445 } else if (Constraint == "wa" || Constraint == "wd" ||
11446 Constraint == "wf" || Constraint == "ws") {
11447 return C_RegisterClass; // VSX registers.
Chris Lattnerd6855142007-03-25 02:14:49 +000011448 }
11449 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +000011450}
11451
John Thompsone8360b72010-10-29 17:29:13 +000011452/// Examine constraint type and operand type and determine a weight value.
11453/// This object must already have been set up with the operand type
11454/// and the current alternative constraint selected.
11455TargetLowering::ConstraintWeight
11456PPCTargetLowering::getSingleConstraintMatchWeight(
11457 AsmOperandInfo &info, const char *constraint) const {
11458 ConstraintWeight weight = CW_Invalid;
11459 Value *CallOperandVal = info.CallOperandVal;
11460 // If we don't have a value, we can't do a match,
11461 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000011462 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000011463 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000011464 Type *type = CallOperandVal->getType();
Hal Finkel6aca2372014-03-02 18:23:39 +000011465
John Thompsone8360b72010-10-29 17:29:13 +000011466 // Look at the constraint type.
Hal Finkel6aca2372014-03-02 18:23:39 +000011467 if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
11468 return CW_Register; // an individual CR bit.
Hal Finkel27774d92014-03-13 07:58:58 +000011469 else if ((StringRef(constraint) == "wa" ||
11470 StringRef(constraint) == "wd" ||
11471 StringRef(constraint) == "wf") &&
11472 type->isVectorTy())
11473 return CW_Register;
11474 else if (StringRef(constraint) == "ws" && type->isDoubleTy())
11475 return CW_Register;
Hal Finkel6aca2372014-03-02 18:23:39 +000011476
John Thompsone8360b72010-10-29 17:29:13 +000011477 switch (*constraint) {
11478 default:
11479 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11480 break;
11481 case 'b':
11482 if (type->isIntegerTy())
11483 weight = CW_Register;
11484 break;
11485 case 'f':
11486 if (type->isFloatTy())
11487 weight = CW_Register;
11488 break;
11489 case 'd':
11490 if (type->isDoubleTy())
11491 weight = CW_Register;
11492 break;
11493 case 'v':
11494 if (type->isVectorTy())
11495 weight = CW_Register;
11496 break;
11497 case 'y':
11498 weight = CW_Register;
11499 break;
Hal Finkel4f24c622012-11-05 18:18:42 +000011500 case 'Z':
11501 weight = CW_Memory;
11502 break;
John Thompsone8360b72010-10-29 17:29:13 +000011503 }
11504 return weight;
11505}
11506
Eric Christopher11e4df72015-02-26 22:38:43 +000011507std::pair<unsigned, const TargetRegisterClass *>
11508PPCTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011509 StringRef Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000011510 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +000011511 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +000011512 // GCC RS6000 Constraint Letters
11513 switch (Constraint[0]) {
11514 case 'b': // R1-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011515 if (VT == MVT::i64 && Subtarget.isPPC64())
Hal Finkel638a9fa2013-03-19 18:51:05 +000011516 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
11517 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000011518 case 'r': // R0-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011519 if (VT == MVT::i64 && Subtarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +000011520 return std::make_pair(0U, &PPC::G8RCRegClass);
11521 return std::make_pair(0U, &PPC::GPRCRegClass);
Eric Christopherb979d512016-03-24 21:04:52 +000011522 // 'd' and 'f' constraints are both defined to be "the floating point
11523 // registers", where one is for 32-bit and the other for 64-bit. We don't
11524 // really care overly much here so just give them all the same reg classes.
11525 case 'd':
Chris Lattner584a11a2006-11-02 01:44:04 +000011526 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +000011527 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +000011528 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +000011529 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +000011530 return std::make_pair(0U, &PPC::F8RCRegClass);
Hal Finkelc93a9a22015-02-25 01:06:45 +000011531 if (VT == MVT::v4f64 && Subtarget.hasQPX())
11532 return std::make_pair(0U, &PPC::QFRCRegClass);
11533 if (VT == MVT::v4f32 && Subtarget.hasQPX())
11534 return std::make_pair(0U, &PPC::QSRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000011535 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011536 case 'v':
Hal Finkelc93a9a22015-02-25 01:06:45 +000011537 if (VT == MVT::v4f64 && Subtarget.hasQPX())
11538 return std::make_pair(0U, &PPC::QFRCRegClass);
11539 if (VT == MVT::v4f32 && Subtarget.hasQPX())
11540 return std::make_pair(0U, &PPC::QSRCRegClass);
Hal Finkelbdd292a2015-10-28 23:03:45 +000011541 if (Subtarget.hasAltivec())
11542 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000011543 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +000011544 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +000011545 }
Hal Finkel34d41492015-10-28 22:25:52 +000011546 } else if (Constraint == "wc" && Subtarget.useCRBits()) {
11547 // An individual CR bit.
Hal Finkel6aca2372014-03-02 18:23:39 +000011548 return std::make_pair(0U, &PPC::CRBITRCRegClass);
Hal Finkelbdd292a2015-10-28 23:03:45 +000011549 } else if ((Constraint == "wa" || Constraint == "wd" ||
11550 Constraint == "wf") && Subtarget.hasVSX()) {
Hal Finkel27774d92014-03-13 07:58:58 +000011551 return std::make_pair(0U, &PPC::VSRCRegClass);
Hal Finkelbdd292a2015-10-28 23:03:45 +000011552 } else if (Constraint == "ws" && Subtarget.hasVSX()) {
11553 if (VT == MVT::f32 && Subtarget.hasP8Vector())
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +000011554 return std::make_pair(0U, &PPC::VSSRCRegClass);
11555 else
11556 return std::make_pair(0U, &PPC::VSFRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +000011557 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011558
Eric Christopher11e4df72015-02-26 22:38:43 +000011559 std::pair<unsigned, const TargetRegisterClass *> R =
11560 TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Hal Finkelb176acb2013-08-03 12:25:10 +000011561
11562 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
11563 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
11564 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
11565 // register.
11566 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
11567 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011568 if (R.first && VT == MVT::i64 && Subtarget.isPPC64() &&
Eric Christopher11e4df72015-02-26 22:38:43 +000011569 PPC::GPRCRegClass.contains(R.first))
Hal Finkelb176acb2013-08-03 12:25:10 +000011570 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +000011571 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +000011572 &PPC::G8RCRegClass);
Hal Finkelb176acb2013-08-03 12:25:10 +000011573
Hal Finkelaa10b3c2014-12-08 22:54:22 +000011574 // GCC accepts 'cc' as an alias for 'cr0', and we need to do the same.
11575 if (!R.second && StringRef("{cc}").equals_lower(Constraint)) {
11576 R.first = PPC::CR0;
11577 R.second = &PPC::CRRCRegClass;
11578 }
11579
Hal Finkelb176acb2013-08-03 12:25:10 +000011580 return R;
Chris Lattner01513612006-01-31 19:20:21 +000011581}
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011582
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000011583/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +000011584/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +000011585void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000011586 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011587 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +000011588 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000011589 SDValue Result;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011590
Eric Christopherde9399b2011-06-02 23:16:42 +000011591 // Only support length 1 constraints.
11592 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011593
Eric Christopherde9399b2011-06-02 23:16:42 +000011594 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011595 switch (Letter) {
11596 default: break;
11597 case 'I':
11598 case 'J':
11599 case 'K':
11600 case 'L':
11601 case 'M':
11602 case 'N':
11603 case 'O':
11604 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +000011605 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000011606 if (!CST) return; // Must be an immediate to match.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011607 SDLoc dl(Op);
Hal Finkelc91fc112014-12-03 09:37:50 +000011608 int64_t Value = CST->getSExtValue();
11609 EVT TCVT = MVT::i64; // All constants taken to be 64 bits so that negative
11610 // numbers are printed as such.
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011611 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000011612 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011613 case 'I': // "I" is a signed 16-bit constant.
Hal Finkelc91fc112014-12-03 09:37:50 +000011614 if (isInt<16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011615 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011616 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011617 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
Hal Finkelc91fc112014-12-03 09:37:50 +000011618 if (isShiftedUInt<16, 16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011619 Result = DAG.getTargetConstant(Value, dl, TCVT);
Hal Finkelc91fc112014-12-03 09:37:50 +000011620 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011621 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Hal Finkelc91fc112014-12-03 09:37:50 +000011622 if (isShiftedInt<16, 16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011623 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011624 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011625 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Hal Finkelc91fc112014-12-03 09:37:50 +000011626 if (isUInt<16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011627 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011628 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011629 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +000011630 if (Value > 31)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011631 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011632 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011633 case 'N': // "N" is a positive constant that is an exact power of two.
Hal Finkelc91fc112014-12-03 09:37:50 +000011634 if (Value > 0 && isPowerOf2_64(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011635 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011636 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011637 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +000011638 if (Value == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011639 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011640 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011641 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Hal Finkelc91fc112014-12-03 09:37:50 +000011642 if (isInt<16>(-Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011643 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011644 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011645 }
11646 break;
11647 }
11648 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011649
Gabor Greiff304a7a2008-08-28 21:40:38 +000011650 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000011651 Ops.push_back(Result);
11652 return;
11653 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011654
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011655 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +000011656 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011657}
Evan Cheng2dd2c652006-03-13 23:20:37 +000011658
Chris Lattner1eb94d92007-03-30 23:15:24 +000011659// isLegalAddressingMode - Return true if the addressing mode represented
11660// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011661bool PPCTargetLowering::isLegalAddressingMode(const DataLayout &DL,
11662 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000011663 unsigned AS) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +000011664 // PPC does not allow r+i addressing modes for vectors!
11665 if (Ty->isVectorTy() && AM.BaseOffs != 0)
11666 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011667
Chris Lattner1eb94d92007-03-30 23:15:24 +000011668 // PPC allows a sign-extended 16-bit immediate field.
11669 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
11670 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011671
Chris Lattner1eb94d92007-03-30 23:15:24 +000011672 // No global is ever allowed as a base.
11673 if (AM.BaseGV)
11674 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011675
11676 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +000011677 switch (AM.Scale) {
11678 case 0: // "r+i" or just "i", depending on HasBaseReg.
11679 break;
11680 case 1:
11681 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
11682 return false;
11683 // Otherwise we have r+r or r+i.
11684 break;
11685 case 2:
11686 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
11687 return false;
11688 // Allow 2*r as r+r.
11689 break;
Chris Lattner19ccd622007-04-09 22:10:05 +000011690 default:
11691 // No other scales are supported.
11692 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +000011693 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011694
Chris Lattner1eb94d92007-03-30 23:15:24 +000011695 return true;
11696}
11697
Dan Gohman21cea8a2010-04-17 15:26:15 +000011698SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
11699 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +000011700 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +000011701 MachineFrameInfo &MFI = MF.getFrameInfo();
11702 MFI.setReturnAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +000011703
Bill Wendling908bf812014-01-06 00:43:20 +000011704 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +000011705 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +000011706
Andrew Trickef9de2a2013-05-25 02:42:55 +000011707 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +000011708 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +000011709
Dale Johannesen81bfca72010-05-03 22:59:34 +000011710 // Make sure the function does not optimize away the store of the RA to
11711 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +000011712 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +000011713 FuncInfo->setLRStoreRequired();
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011714 bool isPPC64 = Subtarget.isPPC64();
Mehdi Amini44ede332015-07-09 02:09:04 +000011715 auto PtrVT = getPointerTy(MF.getDataLayout());
Dale Johannesen81bfca72010-05-03 22:59:34 +000011716
11717 if (Depth > 0) {
11718 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
11719 SDValue Offset =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011720 DAG.getConstant(Subtarget.getFrameLowering()->getReturnSaveOffset(), dl,
Eric Christopherf71609b2015-02-13 00:39:27 +000011721 isPPC64 ? MVT::i64 : MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +000011722 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
11723 DAG.getNode(ISD::ADD, dl, PtrVT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +000011724 MachinePointerInfo());
Dale Johannesen81bfca72010-05-03 22:59:34 +000011725 }
Chris Lattnerf6a81562007-12-08 06:59:59 +000011726
Chris Lattnerf6a81562007-12-08 06:59:59 +000011727 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011728 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Mehdi Amini44ede332015-07-09 02:09:04 +000011729 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), RetAddrFI,
Justin Lebar9c375812016-07-15 18:27:10 +000011730 MachinePointerInfo());
Chris Lattnerf6a81562007-12-08 06:59:59 +000011731}
11732
Dan Gohman21cea8a2010-04-17 15:26:15 +000011733SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
11734 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +000011735 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +000011736 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +000011737
Nicolas Geoffray75ab9792007-03-01 13:11:38 +000011738 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +000011739 MachineFrameInfo &MFI = MF.getFrameInfo();
11740 MFI.setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +000011741
Eric Christophercd719462016-07-07 01:49:59 +000011742 EVT PtrVT = getPointerTy(MF.getDataLayout());
Mehdi Amini44ede332015-07-09 02:09:04 +000011743 bool isPPC64 = PtrVT == MVT::i64;
11744
Hal Finkelaa03c032013-03-21 19:03:19 +000011745 // Naked functions never have a frame pointer, and so we use r1. For all
11746 // other functions, this decision must be delayed until during PEI.
11747 unsigned FrameReg;
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +000011748 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Hal Finkelaa03c032013-03-21 19:03:19 +000011749 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
11750 else
11751 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
11752
Dale Johannesen81bfca72010-05-03 22:59:34 +000011753 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
11754 PtrVT);
11755 while (Depth--)
11756 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Justin Lebar9c375812016-07-15 18:27:10 +000011757 FrameAddr, MachinePointerInfo());
Dale Johannesen81bfca72010-05-03 22:59:34 +000011758 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +000011759}
Dan Gohmanc14e5222008-10-21 03:41:46 +000011760
Hal Finkel0d8db462014-05-11 19:29:11 +000011761// FIXME? Maybe this could be a TableGen attribute on some registers and
11762// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +000011763unsigned PPCTargetLowering::getRegisterByName(const char* RegName, EVT VT,
11764 SelectionDAG &DAG) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011765 bool isPPC64 = Subtarget.isPPC64();
11766 bool isDarwinABI = Subtarget.isDarwinABI();
Hal Finkel0d8db462014-05-11 19:29:11 +000011767
11768 if ((isPPC64 && VT != MVT::i64 && VT != MVT::i32) ||
11769 (!isPPC64 && VT != MVT::i32))
11770 report_fatal_error("Invalid register global variable type");
11771
11772 bool is64Bit = isPPC64 && VT == MVT::i64;
11773 unsigned Reg = StringSwitch<unsigned>(RegName)
11774 .Case("r1", is64Bit ? PPC::X1 : PPC::R1)
Hal Finkele6698d52015-02-01 15:03:28 +000011775 .Case("r2", (isDarwinABI || isPPC64) ? 0 : PPC::R2)
Hal Finkel0d8db462014-05-11 19:29:11 +000011776 .Case("r13", (!isPPC64 && isDarwinABI) ? 0 :
11777 (is64Bit ? PPC::X13 : PPC::R13))
11778 .Default(0);
11779
11780 if (Reg)
11781 return Reg;
11782 report_fatal_error("Invalid register name global variable");
11783}
11784
Dan Gohmanc14e5222008-10-21 03:41:46 +000011785bool
11786PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11787 // The PowerPC target isn't yet aware of offsets.
11788 return false;
11789}
Tilmann Schellerb93960d2009-07-03 06:45:56 +000011790
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011791bool PPCTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11792 const CallInst &I,
11793 unsigned Intrinsic) const {
11794
11795 switch (Intrinsic) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000011796 case Intrinsic::ppc_qpx_qvlfd:
11797 case Intrinsic::ppc_qpx_qvlfs:
11798 case Intrinsic::ppc_qpx_qvlfcd:
11799 case Intrinsic::ppc_qpx_qvlfcs:
11800 case Intrinsic::ppc_qpx_qvlfiwa:
11801 case Intrinsic::ppc_qpx_qvlfiwz:
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011802 case Intrinsic::ppc_altivec_lvx:
11803 case Intrinsic::ppc_altivec_lvxl:
11804 case Intrinsic::ppc_altivec_lvebx:
11805 case Intrinsic::ppc_altivec_lvehx:
Bill Schmidt72954782014-11-12 04:19:40 +000011806 case Intrinsic::ppc_altivec_lvewx:
11807 case Intrinsic::ppc_vsx_lxvd2x:
11808 case Intrinsic::ppc_vsx_lxvw4x: {
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011809 EVT VT;
11810 switch (Intrinsic) {
11811 case Intrinsic::ppc_altivec_lvebx:
11812 VT = MVT::i8;
11813 break;
11814 case Intrinsic::ppc_altivec_lvehx:
11815 VT = MVT::i16;
11816 break;
11817 case Intrinsic::ppc_altivec_lvewx:
11818 VT = MVT::i32;
11819 break;
Bill Schmidt72954782014-11-12 04:19:40 +000011820 case Intrinsic::ppc_vsx_lxvd2x:
11821 VT = MVT::v2f64;
11822 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +000011823 case Intrinsic::ppc_qpx_qvlfd:
11824 VT = MVT::v4f64;
11825 break;
11826 case Intrinsic::ppc_qpx_qvlfs:
11827 VT = MVT::v4f32;
11828 break;
11829 case Intrinsic::ppc_qpx_qvlfcd:
11830 VT = MVT::v2f64;
11831 break;
11832 case Intrinsic::ppc_qpx_qvlfcs:
11833 VT = MVT::v2f32;
11834 break;
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011835 default:
11836 VT = MVT::v4i32;
11837 break;
11838 }
11839
11840 Info.opc = ISD::INTRINSIC_W_CHAIN;
11841 Info.memVT = VT;
11842 Info.ptrVal = I.getArgOperand(0);
11843 Info.offset = -VT.getStoreSize()+1;
11844 Info.size = 2*VT.getStoreSize()-1;
11845 Info.align = 1;
11846 Info.vol = false;
11847 Info.readMem = true;
11848 Info.writeMem = false;
11849 return true;
11850 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000011851 case Intrinsic::ppc_qpx_qvlfda:
11852 case Intrinsic::ppc_qpx_qvlfsa:
11853 case Intrinsic::ppc_qpx_qvlfcda:
11854 case Intrinsic::ppc_qpx_qvlfcsa:
11855 case Intrinsic::ppc_qpx_qvlfiwaa:
11856 case Intrinsic::ppc_qpx_qvlfiwza: {
11857 EVT VT;
11858 switch (Intrinsic) {
11859 case Intrinsic::ppc_qpx_qvlfda:
11860 VT = MVT::v4f64;
11861 break;
11862 case Intrinsic::ppc_qpx_qvlfsa:
11863 VT = MVT::v4f32;
11864 break;
11865 case Intrinsic::ppc_qpx_qvlfcda:
11866 VT = MVT::v2f64;
11867 break;
11868 case Intrinsic::ppc_qpx_qvlfcsa:
11869 VT = MVT::v2f32;
11870 break;
11871 default:
11872 VT = MVT::v4i32;
11873 break;
11874 }
11875
11876 Info.opc = ISD::INTRINSIC_W_CHAIN;
11877 Info.memVT = VT;
11878 Info.ptrVal = I.getArgOperand(0);
11879 Info.offset = 0;
11880 Info.size = VT.getStoreSize();
11881 Info.align = 1;
11882 Info.vol = false;
11883 Info.readMem = true;
11884 Info.writeMem = false;
11885 return true;
11886 }
11887 case Intrinsic::ppc_qpx_qvstfd:
11888 case Intrinsic::ppc_qpx_qvstfs:
11889 case Intrinsic::ppc_qpx_qvstfcd:
11890 case Intrinsic::ppc_qpx_qvstfcs:
11891 case Intrinsic::ppc_qpx_qvstfiw:
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011892 case Intrinsic::ppc_altivec_stvx:
11893 case Intrinsic::ppc_altivec_stvxl:
11894 case Intrinsic::ppc_altivec_stvebx:
11895 case Intrinsic::ppc_altivec_stvehx:
Bill Schmidt72954782014-11-12 04:19:40 +000011896 case Intrinsic::ppc_altivec_stvewx:
11897 case Intrinsic::ppc_vsx_stxvd2x:
11898 case Intrinsic::ppc_vsx_stxvw4x: {
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011899 EVT VT;
11900 switch (Intrinsic) {
11901 case Intrinsic::ppc_altivec_stvebx:
11902 VT = MVT::i8;
11903 break;
11904 case Intrinsic::ppc_altivec_stvehx:
11905 VT = MVT::i16;
11906 break;
11907 case Intrinsic::ppc_altivec_stvewx:
11908 VT = MVT::i32;
11909 break;
Bill Schmidt72954782014-11-12 04:19:40 +000011910 case Intrinsic::ppc_vsx_stxvd2x:
11911 VT = MVT::v2f64;
11912 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +000011913 case Intrinsic::ppc_qpx_qvstfd:
11914 VT = MVT::v4f64;
11915 break;
11916 case Intrinsic::ppc_qpx_qvstfs:
11917 VT = MVT::v4f32;
11918 break;
11919 case Intrinsic::ppc_qpx_qvstfcd:
11920 VT = MVT::v2f64;
11921 break;
11922 case Intrinsic::ppc_qpx_qvstfcs:
11923 VT = MVT::v2f32;
11924 break;
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011925 default:
11926 VT = MVT::v4i32;
11927 break;
11928 }
11929
11930 Info.opc = ISD::INTRINSIC_VOID;
11931 Info.memVT = VT;
11932 Info.ptrVal = I.getArgOperand(1);
11933 Info.offset = -VT.getStoreSize()+1;
11934 Info.size = 2*VT.getStoreSize()-1;
11935 Info.align = 1;
11936 Info.vol = false;
11937 Info.readMem = false;
11938 Info.writeMem = true;
11939 return true;
11940 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000011941 case Intrinsic::ppc_qpx_qvstfda:
11942 case Intrinsic::ppc_qpx_qvstfsa:
11943 case Intrinsic::ppc_qpx_qvstfcda:
11944 case Intrinsic::ppc_qpx_qvstfcsa:
11945 case Intrinsic::ppc_qpx_qvstfiwa: {
11946 EVT VT;
11947 switch (Intrinsic) {
11948 case Intrinsic::ppc_qpx_qvstfda:
11949 VT = MVT::v4f64;
11950 break;
11951 case Intrinsic::ppc_qpx_qvstfsa:
11952 VT = MVT::v4f32;
11953 break;
11954 case Intrinsic::ppc_qpx_qvstfcda:
11955 VT = MVT::v2f64;
11956 break;
11957 case Intrinsic::ppc_qpx_qvstfcsa:
11958 VT = MVT::v2f32;
11959 break;
11960 default:
11961 VT = MVT::v4i32;
11962 break;
11963 }
11964
11965 Info.opc = ISD::INTRINSIC_VOID;
11966 Info.memVT = VT;
11967 Info.ptrVal = I.getArgOperand(1);
11968 Info.offset = 0;
11969 Info.size = VT.getStoreSize();
11970 Info.align = 1;
11971 Info.vol = false;
11972 Info.readMem = false;
11973 Info.writeMem = true;
11974 return true;
11975 }
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011976 default:
11977 break;
11978 }
11979
11980 return false;
11981}
11982
Evan Chengd9929f02010-04-01 20:10:42 +000011983/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +000011984/// and store operations as a result of memset, memcpy, and memmove
11985/// lowering. If DstAlign is zero that means it's safe to destination
11986/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
11987/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +000011988/// probably because the source does not need to be loaded. If 'IsMemset' is
11989/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
11990/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
11991/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +000011992/// It returns EVT::Other if the type should be determined using generic
11993/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +000011994EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
11995 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000011996 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +000011997 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +000011998 MachineFunction &MF) const {
Hal Finkel52368d42015-03-31 20:56:09 +000011999 if (getTargetMachine().getOptLevel() != CodeGenOpt::None) {
12000 const Function *F = MF.getFunction();
12001 // When expanding a memset, require at least two QPX instructions to cover
12002 // the cost of loading the value to be stored from the constant pool.
12003 if (Subtarget.hasQPX() && Size >= 32 && (!IsMemset || Size >= 64) &&
12004 (!SrcAlign || SrcAlign >= 32) && (!DstAlign || DstAlign >= 32) &&
12005 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
12006 return MVT::v4f64;
12007 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000012008
Hal Finkel52368d42015-03-31 20:56:09 +000012009 // We should use Altivec/VSX loads and stores when available. For unaligned
12010 // addresses, unaligned VSX loads are only fast starting with the P8.
12011 if (Subtarget.hasAltivec() && Size >= 16 &&
12012 (((!SrcAlign || SrcAlign >= 16) && (!DstAlign || DstAlign >= 16)) ||
12013 ((IsMemset && Subtarget.hasVSX()) || Subtarget.hasP8Vector())))
12014 return MVT::v4i32;
12015 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000012016
Eric Christopherd90a8742014-06-12 22:38:20 +000012017 if (Subtarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +000012018 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +000012019 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000012020
12021 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +000012022}
Hal Finkel88ed4e32012-04-01 19:23:08 +000012023
Hal Finkel34974ed2014-04-12 21:52:38 +000012024/// \brief Returns true if it is beneficial to convert a load of a constant
12025/// to just the constant itself.
12026bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
12027 Type *Ty) const {
12028 assert(Ty->isIntegerTy());
12029
12030 unsigned BitSize = Ty->getPrimitiveSizeInBits();
Alexander Kornienko175a7cb2015-12-28 13:38:42 +000012031 return !(BitSize == 0 || BitSize > 64);
Hal Finkel34974ed2014-04-12 21:52:38 +000012032}
12033
12034bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
12035 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
12036 return false;
12037 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12038 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
12039 return NumBits1 == 64 && NumBits2 == 32;
12040}
12041
12042bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
12043 if (!VT1.isInteger() || !VT2.isInteger())
12044 return false;
12045 unsigned NumBits1 = VT1.getSizeInBits();
12046 unsigned NumBits2 = VT2.getSizeInBits();
12047 return NumBits1 == 64 && NumBits2 == 32;
12048}
12049
Hal Finkel5d5d1532015-01-10 08:21:59 +000012050bool PPCTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12051 // Generally speaking, zexts are not free, but they are free when they can be
12052 // folded with other operations.
12053 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Val)) {
12054 EVT MemVT = LD->getMemoryVT();
12055 if ((MemVT == MVT::i1 || MemVT == MVT::i8 || MemVT == MVT::i16 ||
12056 (Subtarget.isPPC64() && MemVT == MVT::i32)) &&
12057 (LD->getExtensionType() == ISD::NON_EXTLOAD ||
12058 LD->getExtensionType() == ISD::ZEXTLOAD))
12059 return true;
12060 }
12061
12062 // FIXME: Add other cases...
12063 // - 32-bit shifts with a zext to i64
12064 // - zext after ctlz, bswap, etc.
12065 // - zext after and by a constant mask
12066
12067 return TargetLowering::isZExtFree(Val, VT2);
12068}
12069
Olivier Sallenave32509692015-01-13 15:06:36 +000012070bool PPCTargetLowering::isFPExtFree(EVT VT) const {
12071 assert(VT.isFloatingPoint());
12072 return true;
12073}
12074
Hal Finkel34974ed2014-04-12 21:52:38 +000012075bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
12076 return isInt<16>(Imm) || isUInt<16>(Imm);
12077}
12078
12079bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const {
12080 return isInt<16>(Imm) || isUInt<16>(Imm);
12081}
12082
Matt Arsenault6f2a5262014-07-27 17:46:40 +000012083bool PPCTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
12084 unsigned,
12085 unsigned,
12086 bool *Fast) const {
Hal Finkel8d7fbc92013-03-15 15:27:13 +000012087 if (DisablePPCUnaligned)
12088 return false;
12089
12090 // PowerPC supports unaligned memory access for simple non-vector types.
12091 // Although accessing unaligned addresses is not as efficient as accessing
12092 // aligned addresses, it is generally more efficient than manual expansion,
12093 // and generally only traps for software emulation when crossing page
12094 // boundaries.
12095
12096 if (!VT.isSimple())
12097 return false;
12098
Hal Finkel6e28e6a2014-03-26 19:39:09 +000012099 if (VT.getSimpleVT().isVector()) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000012100 if (Subtarget.hasVSX()) {
Bill Schmidt2d1128a2014-10-17 15:13:38 +000012101 if (VT != MVT::v2f64 && VT != MVT::v2i64 &&
12102 VT != MVT::v4f32 && VT != MVT::v4i32)
Hal Finkel6e28e6a2014-03-26 19:39:09 +000012103 return false;
12104 } else {
12105 return false;
12106 }
12107 }
Hal Finkel8d7fbc92013-03-15 15:27:13 +000012108
12109 if (VT == MVT::ppcf128)
12110 return false;
12111
12112 if (Fast)
12113 *Fast = true;
12114
12115 return true;
12116}
12117
Stephen Lin73de7bf2013-07-09 18:16:56 +000012118bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
12119 VT = VT.getScalarType();
12120
Hal Finkel0a479ae2012-06-22 00:49:52 +000012121 if (!VT.isSimple())
12122 return false;
12123
12124 switch (VT.getSimpleVT().SimpleTy) {
12125 case MVT::f32:
12126 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +000012127 return true;
12128 default:
12129 break;
12130 }
12131
12132 return false;
12133}
12134
Hal Finkel934361a2015-01-14 01:07:51 +000012135const MCPhysReg *
12136PPCTargetLowering::getScratchRegisters(CallingConv::ID) const {
12137 // LR is a callee-save register, but we must treat it as clobbered by any call
12138 // site. Hence we include LR in the scratch registers, which are in turn added
12139 // as implicit-defs for stackmaps and patchpoints. The same reasoning applies
12140 // to CTR, which is used by any indirect call.
12141 static const MCPhysReg ScratchRegs[] = {
Hal Finkelc19805a2015-01-17 03:57:34 +000012142 PPC::X12, PPC::LR8, PPC::CTR8, 0
Hal Finkel934361a2015-01-14 01:07:51 +000012143 };
12144
12145 return ScratchRegs;
12146}
12147
Joseph Tremouletf748c892015-11-07 01:11:31 +000012148unsigned PPCTargetLowering::getExceptionPointerRegister(
12149 const Constant *PersonalityFn) const {
12150 return Subtarget.isPPC64() ? PPC::X3 : PPC::R3;
12151}
12152
12153unsigned PPCTargetLowering::getExceptionSelectorRegister(
12154 const Constant *PersonalityFn) const {
12155 return Subtarget.isPPC64() ? PPC::X4 : PPC::R4;
12156}
12157
Hal Finkelb4240ca2014-03-31 17:48:16 +000012158bool
12159PPCTargetLowering::shouldExpandBuildVectorWithShuffles(
12160 EVT VT , unsigned DefinedValues) const {
12161 if (VT == MVT::v2i64)
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +000012162 return Subtarget.hasDirectMove(); // Don't need stack ops with direct moves
Hal Finkelb4240ca2014-03-31 17:48:16 +000012163
Guozhi Weifa3e0422016-04-29 17:00:54 +000012164 if (Subtarget.hasVSX() || Subtarget.hasQPX())
12165 return true;
Hal Finkelc93a9a22015-02-25 01:06:45 +000012166
Hal Finkelb4240ca2014-03-31 17:48:16 +000012167 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
12168}
12169
Hal Finkel88ed4e32012-04-01 19:23:08 +000012170Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000012171 if (DisableILPPref || Subtarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +000012172 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +000012173
Hal Finkel4e9f1a82012-06-10 19:32:29 +000012174 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +000012175}
12176
Bill Schmidt0cf702f2013-07-30 00:50:39 +000012177// Create a fast isel object.
12178FastISel *
12179PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
12180 const TargetLibraryInfo *LibInfo) const {
12181 return PPC::createFastISel(FuncInfo, LibInfo);
12182}
Chuang-Yu Cheng98c18942016-04-08 12:04:32 +000012183
12184void PPCTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const {
12185 if (Subtarget.isDarwinABI()) return;
12186 if (!Subtarget.isPPC64()) return;
12187
12188 // Update IsSplitCSR in PPCFunctionInfo
12189 PPCFunctionInfo *PFI = Entry->getParent()->getInfo<PPCFunctionInfo>();
12190 PFI->setIsSplitCSR(true);
12191}
12192
12193void PPCTargetLowering::insertCopiesSplitCSR(
12194 MachineBasicBlock *Entry,
12195 const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
12196 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo();
12197 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
12198 if (!IStart)
12199 return;
12200
12201 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
12202 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
12203 MachineBasicBlock::iterator MBBI = Entry->begin();
12204 for (const MCPhysReg *I = IStart; *I; ++I) {
12205 const TargetRegisterClass *RC = nullptr;
12206 if (PPC::G8RCRegClass.contains(*I))
12207 RC = &PPC::G8RCRegClass;
12208 else if (PPC::F8RCRegClass.contains(*I))
12209 RC = &PPC::F8RCRegClass;
12210 else if (PPC::CRRCRegClass.contains(*I))
12211 RC = &PPC::CRRCRegClass;
12212 else if (PPC::VRRCRegClass.contains(*I))
12213 RC = &PPC::VRRCRegClass;
12214 else
12215 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
12216
12217 unsigned NewVR = MRI->createVirtualRegister(RC);
12218 // Create copy from CSR to a virtual register.
12219 // FIXME: this currently does not emit CFI pseudo-instructions, it works
12220 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be
12221 // nounwind. If we want to generalize this later, we may need to emit
12222 // CFI pseudo-instructions.
12223 assert(Entry->getParent()->getFunction()->hasFnAttribute(
12224 Attribute::NoUnwind) &&
12225 "Function should be nounwind in insertCopiesSplitCSR!");
12226 Entry->addLiveIn(*I);
12227 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
12228 .addReg(*I);
12229
12230 // Insert the copy-back instructions right before the terminator
12231 for (auto *Exit : Exits)
12232 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
12233 TII->get(TargetOpcode::COPY), *I)
12234 .addReg(NewVR);
12235 }
12236}
Tim Shena1d8bc52016-04-19 20:14:52 +000012237
12238// Override to enable LOAD_STACK_GUARD lowering on Linux.
12239bool PPCTargetLowering::useLoadStackGuardNode() const {
12240 if (!Subtarget.isTargetLinux())
12241 return TargetLowering::useLoadStackGuardNode();
12242 return true;
12243}
12244
12245// Override to disable global variable loading on Linux.
12246void PPCTargetLowering::insertSSPDeclarations(Module &M) const {
12247 if (!Subtarget.isTargetLinux())
12248 return TargetLowering::insertSSPDeclarations(M);
12249}