blob: bb6a0c5bbd3b33aac229333b484760fe069068a5 [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"
Kit Bartond4eb73c2015-05-05 16:10:44 +000045
Chris Lattnerf22556d2005-08-16 17:14:42 +000046using namespace llvm;
47
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +000048#define DEBUG_TYPE "ppc-lowering"
49
Hal Finkel595817e2012-06-04 02:21:00 +000050static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
51cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000052
Hal Finkel4e9f1a82012-06-10 19:32:29 +000053static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
54cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
55
Hal Finkel8d7fbc92013-03-15 15:27:13 +000056static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
57cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
58
Chuang-Yu Cheng0600e8d2016-04-26 07:38:24 +000059static cl::opt<bool> DisableSCO("disable-ppc-sco",
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +000060cl::desc("disable sibling call optimization on ppc"), cl::Hidden);
61
62STATISTIC(NumTailCalls, "Number of tail calls");
63STATISTIC(NumSiblingCalls, "Number of sibling calls");
64
Hal Finkel940ab932014-02-28 00:27:01 +000065// FIXME: Remove this once the bug has been fixed!
66extern cl::opt<bool> ANDIGlueBug;
67
Eric Christophercccae792015-01-30 22:02:31 +000068PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM,
69 const PPCSubtarget &STI)
70 : TargetLowering(TM), Subtarget(STI) {
Chris Lattnera028e7a2005-09-27 22:18:25 +000071 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000072 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000074
Chris Lattnerd10babf2010-10-10 18:34:00 +000075 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
76 // arguments are at least 4/8 bytes aligned.
Eric Christopherb1aaebe2014-06-12 22:38:18 +000077 bool isPPC64 = Subtarget.isPPC64();
Evan Cheng39e90022012-07-02 22:39:56 +000078 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000079
Chris Lattnerf22556d2005-08-16 17:14:42 +000080 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000081 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
Petar Jovanovic280f7102015-12-14 17:57:33 +000082 if (!Subtarget.useSoftFloat()) {
83 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
84 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
85 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000086
Evan Cheng5d9fd972006-10-04 00:56:09 +000087 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000088 for (MVT VT : MVT::integer_valuetypes()) {
89 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
90 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand);
91 }
Duncan Sands95d46ef2008-01-23 20:39:46 +000092
Owen Anderson9f944592009-08-11 20:47:22 +000093 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000094
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000095 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000096 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
97 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
98 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
99 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
100 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
Hal Finkel65d1cbf2015-02-05 18:42:53 +0000101 setIndexedLoadAction(ISD::PRE_INC, MVT::f32, Legal);
102 setIndexedLoadAction(ISD::PRE_INC, MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000103 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
104 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
105 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
106 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
107 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Hal Finkel65d1cbf2015-02-05 18:42:53 +0000108 setIndexedStoreAction(ISD::PRE_INC, MVT::f32, Legal);
109 setIndexedStoreAction(ISD::PRE_INC, MVT::f64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +0000110
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000111 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
113
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000114 if (isPPC64 || Subtarget.hasFPCVT()) {
Hal Finkel6a56b212014-03-05 22:14:00 +0000115 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
116 AddPromotedToType (ISD::SINT_TO_FP, MVT::i1,
117 isPPC64 ? MVT::i64 : MVT::i32);
118 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000119 AddPromotedToType(ISD::UINT_TO_FP, MVT::i1,
120 isPPC64 ? MVT::i64 : MVT::i32);
Hal Finkel6a56b212014-03-05 22:14:00 +0000121 } else {
122 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom);
123 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom);
124 }
Hal Finkel940ab932014-02-28 00:27:01 +0000125
126 // PowerPC does not support direct load / store of condition registers
127 setOperationAction(ISD::LOAD, MVT::i1, Custom);
128 setOperationAction(ISD::STORE, MVT::i1, Custom);
129
130 // FIXME: Remove this once the ANDI glue bug is fixed:
131 if (ANDIGlueBug)
132 setOperationAction(ISD::TRUNCATE, MVT::i1, Custom);
133
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000134 for (MVT VT : MVT::integer_valuetypes()) {
135 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
136 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
137 setTruncStoreAction(VT, MVT::i1, Expand);
138 }
Hal Finkel940ab932014-02-28 00:27:01 +0000139
140 addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass);
141 }
142
Dale Johannesen666323e2007-10-10 01:01:31 +0000143 // This is used in the ppcf128->int sequence. Note it has different semantics
144 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +0000145 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000146
Roman Divacky1faf5b02012-08-16 18:19:29 +0000147 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000148 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
149 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
150 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
151 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
152 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000153 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000154
Chris Lattnerf22556d2005-08-16 17:14:42 +0000155 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000156 setOperationAction(ISD::SREM, MVT::i32, Expand);
157 setOperationAction(ISD::UREM, MVT::i32, Expand);
158 setOperationAction(ISD::SREM, MVT::i64, Expand);
159 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000160
161 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000162 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
163 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
164 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
165 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
166 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
167 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
168 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
169 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000170
Dan Gohman482732a2007-10-11 23:21:31 +0000171 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000172 setOperationAction(ISD::FSIN , MVT::f64, Expand);
173 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000174 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000175 setOperationAction(ISD::FREM , MVT::f64, Expand);
176 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000177 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000178 setOperationAction(ISD::FSIN , MVT::f32, Expand);
179 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000180 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000181 setOperationAction(ISD::FREM , MVT::f32, Expand);
182 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000183 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000184
Owen Anderson9f944592009-08-11 20:47:22 +0000185 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000186
Chris Lattnerf22556d2005-08-16 17:14:42 +0000187 // If we're enabling GP optimizations, use hardware square root
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000188 if (!Subtarget.hasFSQRT() &&
Eric Christophercccae792015-01-30 22:02:31 +0000189 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTE() &&
190 Subtarget.hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000191 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000192
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000193 if (!Subtarget.hasFSQRT() &&
Eric Christophercccae792015-01-30 22:02:31 +0000194 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTES() &&
195 Subtarget.hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000196 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000197
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000198 if (Subtarget.hasFCPSGN()) {
Hal Finkeldbc78e12013-08-19 05:01:02 +0000199 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
200 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
201 } else {
202 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
203 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
204 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000205
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000206 if (Subtarget.hasFPRND()) {
Hal Finkelc20a08d2013-03-29 08:57:48 +0000207 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
208 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
209 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000210 setOperationAction(ISD::FROUND, MVT::f64, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000211
212 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
213 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
214 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000215 setOperationAction(ISD::FROUND, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000216 }
217
Nate Begeman2fba8a32006-01-14 03:14:10 +0000218 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000219 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000220 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
221 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000222 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000223
Hal Finkelfa7057a2016-03-29 01:36:01 +0000224 if (Subtarget.hasPOPCNTD() == PPCSubtarget::POPCNTD_Fast) {
Hal Finkel290376d2013-04-01 15:58:15 +0000225 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000226 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
227 } else {
228 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
229 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
230 }
231
Nate Begeman1b8121b2006-01-11 21:21:00 +0000232 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000233 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
234 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000235
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000236 if (!Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000237 // PowerPC does not have Select
238 setOperationAction(ISD::SELECT, MVT::i32, Expand);
239 setOperationAction(ISD::SELECT, MVT::i64, Expand);
240 setOperationAction(ISD::SELECT, MVT::f32, Expand);
241 setOperationAction(ISD::SELECT, MVT::f64, Expand);
242 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000243
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000244 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000245 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
246 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000247
Nate Begeman7e7f4392006-02-01 07:19:44 +0000248 // PowerPC wants to optimize integer setcc a bit
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000249 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000250 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000251
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000252 // PowerPC does not have BRCOND which requires SetCC
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000253 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000254 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000255
Owen Anderson9f944592009-08-11 20:47:22 +0000256 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000257
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000258 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000259 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000260
Jim Laskey6267b2c2005-08-17 00:40:22 +0000261 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000262 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
263 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000264
Nemanja Ivanovic5ebc92d2016-03-24 13:40:33 +0000265 if (Subtarget.hasDirectMove() && isPPC64) {
Nemanja Ivanovic89224762015-12-15 14:50:34 +0000266 setOperationAction(ISD::BITCAST, MVT::f32, Legal);
267 setOperationAction(ISD::BITCAST, MVT::i32, Legal);
268 setOperationAction(ISD::BITCAST, MVT::i64, Legal);
269 setOperationAction(ISD::BITCAST, MVT::f64, Legal);
270 } else {
271 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
272 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
273 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
274 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
275 }
Chris Lattnerc46fc242005-12-23 05:13:35 +0000276
Chris Lattner84b49d52006-04-28 21:56:10 +0000277 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000278 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000279
Hal Finkel1996f3d2013-03-27 19:10:42 +0000280 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000281 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
282 // support continuation, user-level threading, and etc.. As a result, no
283 // other SjLj exception interfaces are implemented and please don't build
284 // your own exception handling based on them.
285 // LLVM/Clang supports zero-cost DWARF exception handling.
286 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
287 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000288
289 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000290 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000291 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
292 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000293 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000294 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
295 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
296 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
297 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000298 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000299 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
300 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000301
Nate Begemanf69d13b2008-08-11 17:36:31 +0000302 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000303 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000304
305 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000306 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
307 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000308
Nate Begemane74795c2006-01-25 18:21:52 +0000309 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000310 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000311
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000312 if (Subtarget.isSVR4ABI()) {
Evan Cheng39e90022012-07-02 22:39:56 +0000313 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000314 // VAARG always uses double-word chunks, so promote anything smaller.
315 setOperationAction(ISD::VAARG, MVT::i1, Promote);
316 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
317 setOperationAction(ISD::VAARG, MVT::i8, Promote);
318 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
319 setOperationAction(ISD::VAARG, MVT::i16, Promote);
320 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
321 setOperationAction(ISD::VAARG, MVT::i32, Promote);
322 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
323 setOperationAction(ISD::VAARG, MVT::Other, Expand);
324 } else {
325 // VAARG is custom lowered with the 32-bit SVR4 ABI.
326 setOperationAction(ISD::VAARG, MVT::Other, Custom);
327 setOperationAction(ISD::VAARG, MVT::i64, Custom);
328 }
Roman Divacky4394e682011-06-28 15:30:42 +0000329 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000330 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000331
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000332 if (Subtarget.isSVR4ABI() && !isPPC64)
Roman Divackyc3825df2013-07-25 21:36:47 +0000333 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
334 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
335 else
336 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
337
Chris Lattner5bd514d2006-01-15 09:02:48 +0000338 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000339 setOperationAction(ISD::VAEND , MVT::Other, Expand);
340 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
341 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
342 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
343 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Yury Gribovd7dbb662015-12-01 11:40:55 +0000344 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i32, Custom);
345 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i64, Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000346
Chris Lattner6961fc72006-03-26 10:06:40 +0000347 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000348 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000349
Hal Finkel25c19922013-05-15 21:37:41 +0000350 // To handle counter-based loop conditions.
351 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
352
Dale Johannesen160be0f2008-11-07 22:54:33 +0000353 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000354 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
355 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
356 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
357 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
358 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
359 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
360 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
361 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
362 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
363 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
364 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
365 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000366
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000367 if (Subtarget.has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000368 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000369 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
370 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
371 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
372 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000373 // This is just the low 32 bits of a (signed) fp->i64 conversion.
374 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000375 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000376
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000377 if (Subtarget.hasLFIWAX() || Subtarget.isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000378 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000379 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000380 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000381 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000382 }
383
Hal Finkelf6d45f22013-04-01 17:52:07 +0000384 // With the instructions enabled under FPCVT, we can do everything.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000385 if (Subtarget.hasFPCVT()) {
386 if (Subtarget.has64BitSupport()) {
Hal Finkelf6d45f22013-04-01 17:52:07 +0000387 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
388 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
389 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
390 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
391 }
392
393 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
394 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
395 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
396 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
397 }
398
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000399 if (Subtarget.use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000400 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000401 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000402 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000403 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000404 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000405 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
406 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
407 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000408 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000409 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000410 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
411 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
412 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000413 }
Evan Cheng19264272006-03-01 01:11:20 +0000414
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000415 if (Subtarget.hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000416 // First set operation action for all vector types to expand. Then we
417 // will selectively turn on ones that can be effectively codegen'd.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000418 for (MVT VT : MVT::vector_valuetypes()) {
Chris Lattner06a21ba2006-04-16 01:37:57 +0000419 // add/sub are legal for all supported vector VT's.
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000420 setOperationAction(ISD::ADD, VT, Legal);
421 setOperationAction(ISD::SUB, VT, Legal);
422
Bill Schmidt433b1c32015-02-05 15:24:47 +0000423 // Vector instructions introduced in P8
Kit Bartond4eb73c2015-05-05 16:10:44 +0000424 if (Subtarget.hasP8Altivec() && (VT.SimpleTy != MVT::v1i128)) {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000425 setOperationAction(ISD::CTPOP, VT, Legal);
Bill Schmidt433b1c32015-02-05 15:24:47 +0000426 setOperationAction(ISD::CTLZ, VT, Legal);
427 }
428 else {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000429 setOperationAction(ISD::CTPOP, VT, Expand);
Bill Schmidt433b1c32015-02-05 15:24:47 +0000430 setOperationAction(ISD::CTLZ, VT, Expand);
431 }
Bill Schmidtfe88b182015-02-03 21:58:23 +0000432
Chris Lattner95c7adc2006-04-04 17:25:31 +0000433 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000434 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000435 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000436
437 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000438 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000439 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000440 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000441 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000442 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000443 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000444 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000445 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000446 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000447 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Hal Finkela2cdbce2015-08-30 22:12:50 +0000448 setOperationAction(ISD::SELECT_CC, VT, Promote);
449 AddPromotedToType (ISD::SELECT_CC, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000450 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000451 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000452
Chris Lattner06a21ba2006-04-16 01:37:57 +0000453 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000454 setOperationAction(ISD::MUL , VT, Expand);
455 setOperationAction(ISD::SDIV, VT, Expand);
456 setOperationAction(ISD::SREM, VT, Expand);
457 setOperationAction(ISD::UDIV, VT, Expand);
458 setOperationAction(ISD::UREM, VT, Expand);
459 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000460 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000461 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000462 setOperationAction(ISD::FSQRT, VT, Expand);
463 setOperationAction(ISD::FLOG, VT, Expand);
464 setOperationAction(ISD::FLOG10, VT, Expand);
465 setOperationAction(ISD::FLOG2, VT, Expand);
466 setOperationAction(ISD::FEXP, VT, Expand);
467 setOperationAction(ISD::FEXP2, VT, Expand);
468 setOperationAction(ISD::FSIN, VT, Expand);
469 setOperationAction(ISD::FCOS, VT, Expand);
470 setOperationAction(ISD::FABS, VT, Expand);
471 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000472 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000473 setOperationAction(ISD::FCEIL, VT, Expand);
474 setOperationAction(ISD::FTRUNC, VT, Expand);
475 setOperationAction(ISD::FRINT, VT, Expand);
476 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000477 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
478 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
479 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
Ulrich Weigand51eccec2014-08-04 13:27:12 +0000480 setOperationAction(ISD::MULHU, VT, Expand);
481 setOperationAction(ISD::MULHS, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000482 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
483 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
484 setOperationAction(ISD::UDIVREM, VT, Expand);
485 setOperationAction(ISD::SDIVREM, VT, Expand);
486 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
487 setOperationAction(ISD::FPOW, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000488 setOperationAction(ISD::BSWAP, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000489 setOperationAction(ISD::CTTZ, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000490 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000491 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Nemanja Ivanovic74e31bc2015-12-02 10:36:24 +0000492 setOperationAction(ISD::ROTL, VT, Expand);
493 setOperationAction(ISD::ROTR, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000494
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000495 for (MVT InnerVT : MVT::vector_valuetypes()) {
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000496 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000497 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
498 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
499 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
500 }
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000501 }
502
Chris Lattner95c7adc2006-04-04 17:25:31 +0000503 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
504 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000505 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000506
Owen Anderson9f944592009-08-11 20:47:22 +0000507 setOperationAction(ISD::AND , MVT::v4i32, Legal);
508 setOperationAction(ISD::OR , MVT::v4i32, Legal);
509 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
510 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Hal Finkel940ab932014-02-28 00:27:01 +0000511 setOperationAction(ISD::SELECT, MVT::v4i32,
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000512 Subtarget.useCRBits() ? Legal : Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000513 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000514 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
515 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
516 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
517 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000518 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
519 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
520 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
521 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000522
Craig Topperabadc662012-04-20 06:31:50 +0000523 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
524 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
525 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
526 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000527
Owen Anderson9f944592009-08-11 20:47:22 +0000528 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000529 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000530
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000531 if (TM.Options.UnsafeFPMath || Subtarget.hasVSX()) {
Hal Finkel2e103312013-04-03 04:01:11 +0000532 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
533 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
534 }
535
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000536 if (Subtarget.hasP8Altivec())
Kit Barton20d39812015-03-10 19:49:38 +0000537 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
538 else
539 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000540
Owen Anderson9f944592009-08-11 20:47:22 +0000541 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
542 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000543
Owen Anderson9f944592009-08-11 20:47:22 +0000544 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
545 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000546
Owen Anderson9f944592009-08-11 20:47:22 +0000547 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
548 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
549 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
550 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000551
552 // Altivec does not contain unordered floating-point compare instructions
553 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
554 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000555 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
556 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000557
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000558 if (Subtarget.hasVSX()) {
Hal Finkel27774d92014-03-13 07:58:58 +0000559 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
Nemanja Ivanovicd3896572015-10-09 11:12:18 +0000560 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
561 if (Subtarget.hasP8Vector()) {
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +0000562 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Nemanja Ivanovicd3896572015-10-09 11:12:18 +0000563 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
564 }
Nemanja Ivanovic5ebc92d2016-03-24 13:40:33 +0000565 if (Subtarget.hasDirectMove() && isPPC64) {
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +0000566 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Legal);
567 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Legal);
568 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Legal);
Nemanja Ivanovicbe5f0c02015-11-02 14:01:11 +0000569 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Legal);
Nemanja Ivanovicd3896572015-10-09 11:12:18 +0000570 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Legal);
571 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Legal);
572 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
573 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +0000574 }
Hal Finkel82569b62014-03-27 22:22:48 +0000575 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
Hal Finkel27774d92014-03-13 07:58:58 +0000576
577 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
578 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
579 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
580 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
581 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
582
583 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
584
585 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
586 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
587
588 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
589 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
590
Hal Finkel732f0f72014-03-26 12:49:28 +0000591 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
592 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal);
593 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
594 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
595 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
596
Hal Finkel27774d92014-03-13 07:58:58 +0000597 // Share the Altivec comparison restrictions.
598 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand);
599 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000600 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand);
601 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand);
602
Hal Finkel9281c9a2014-03-26 18:26:30 +0000603 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
604 setOperationAction(ISD::STORE, MVT::v2f64, Legal);
605
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000606 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal);
607
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +0000608 if (Subtarget.hasP8Vector())
609 addRegisterClass(MVT::f32, &PPC::VSSRCRegClass);
610
Hal Finkel19be5062014-03-29 05:29:01 +0000611 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000612
Bill Schmidt54cced52015-07-16 21:14:07 +0000613 addRegisterClass(MVT::v4i32, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000614 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass);
615 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass);
Hal Finkela6c8b512014-03-26 16:12:58 +0000616
Kit Barton0cfa7b72015-03-03 19:55:45 +0000617 if (Subtarget.hasP8Altivec()) {
Kit Bartone48b1e12015-03-05 16:24:38 +0000618 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
619 setOperationAction(ISD::SRA, MVT::v2i64, Legal);
620 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
621
Kit Barton0cfa7b72015-03-03 19:55:45 +0000622 setOperationAction(ISD::SETCC, MVT::v2i64, Legal);
623 }
624 else {
Kit Bartone48b1e12015-03-05 16:24:38 +0000625 setOperationAction(ISD::SHL, MVT::v2i64, Expand);
626 setOperationAction(ISD::SRA, MVT::v2i64, Expand);
627 setOperationAction(ISD::SRL, MVT::v2i64, Expand);
628
Kit Barton0cfa7b72015-03-03 19:55:45 +0000629 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
630
631 // VSX v2i64 only supports non-arithmetic operations.
632 setOperationAction(ISD::ADD, MVT::v2i64, Expand);
633 setOperationAction(ISD::SUB, MVT::v2i64, Expand);
634 }
Hal Finkel777c9dd2014-03-29 16:04:40 +0000635
Hal Finkel9281c9a2014-03-26 18:26:30 +0000636 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
637 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64);
638 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
639 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64);
640
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000641 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal);
642
Hal Finkel7279f4b2014-03-26 19:13:54 +0000643 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
644 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
645 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
646 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
647
Hal Finkel5c0d1452014-03-30 13:22:59 +0000648 // Vector operation legalization checks the result type of
649 // SIGN_EXTEND_INREG, overall legalization checks the inner type.
650 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal);
651 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal);
652 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
653 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
654
Kit Barton915c5ec2016-02-26 21:59:44 +0000655 setOperationAction(ISD::FNEG, MVT::v4f32, Legal);
656 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
Kit Bartona1d6a6f2016-03-09 17:48:01 +0000657 setOperationAction(ISD::FABS, MVT::v4f32, Legal);
658 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
Kit Barton915c5ec2016-02-26 21:59:44 +0000659
Hal Finkela6c8b512014-03-26 16:12:58 +0000660 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000661 }
Bill Schmidtfe88b182015-02-03 21:58:23 +0000662
Kit Bartond4eb73c2015-05-05 16:10:44 +0000663 if (Subtarget.hasP8Altivec()) {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000664 addRegisterClass(MVT::v2i64, &PPC::VRRCRegClass);
Kit Bartond4eb73c2015-05-05 16:10:44 +0000665 addRegisterClass(MVT::v1i128, &PPC::VRRCRegClass);
666 }
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000667 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000668
Hal Finkelc93a9a22015-02-25 01:06:45 +0000669 if (Subtarget.hasQPX()) {
670 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
671 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
672 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
673 setOperationAction(ISD::FREM, MVT::v4f64, Expand);
674
675 setOperationAction(ISD::FCOPYSIGN, MVT::v4f64, Legal);
676 setOperationAction(ISD::FGETSIGN, MVT::v4f64, Expand);
677
678 setOperationAction(ISD::LOAD , MVT::v4f64, Custom);
679 setOperationAction(ISD::STORE , MVT::v4f64, Custom);
680
681 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Custom);
682 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Custom);
683
684 if (!Subtarget.useCRBits())
685 setOperationAction(ISD::SELECT, MVT::v4f64, Expand);
686 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
687
688 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f64, Legal);
689 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f64, Expand);
690 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f64, Expand);
691 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f64, Expand);
692 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f64, Custom);
693 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f64, Legal);
694 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
695
696 setOperationAction(ISD::FP_TO_SINT , MVT::v4f64, Legal);
697 setOperationAction(ISD::FP_TO_UINT , MVT::v4f64, Expand);
698
699 setOperationAction(ISD::FP_ROUND , MVT::v4f32, Legal);
700 setOperationAction(ISD::FP_ROUND_INREG , MVT::v4f32, Expand);
701 setOperationAction(ISD::FP_EXTEND, MVT::v4f64, Legal);
702
703 setOperationAction(ISD::FNEG , MVT::v4f64, Legal);
704 setOperationAction(ISD::FABS , MVT::v4f64, Legal);
705 setOperationAction(ISD::FSIN , MVT::v4f64, Expand);
706 setOperationAction(ISD::FCOS , MVT::v4f64, Expand);
707 setOperationAction(ISD::FPOWI , MVT::v4f64, Expand);
708 setOperationAction(ISD::FPOW , MVT::v4f64, Expand);
709 setOperationAction(ISD::FLOG , MVT::v4f64, Expand);
710 setOperationAction(ISD::FLOG2 , MVT::v4f64, Expand);
711 setOperationAction(ISD::FLOG10 , MVT::v4f64, Expand);
712 setOperationAction(ISD::FEXP , MVT::v4f64, Expand);
713 setOperationAction(ISD::FEXP2 , MVT::v4f64, Expand);
714
715 setOperationAction(ISD::FMINNUM, MVT::v4f64, Legal);
716 setOperationAction(ISD::FMAXNUM, MVT::v4f64, Legal);
717
718 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f64, Legal);
719 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f64, Legal);
720
721 addRegisterClass(MVT::v4f64, &PPC::QFRCRegClass);
722
723 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
724 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
725 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
726 setOperationAction(ISD::FREM, MVT::v4f32, Expand);
727
728 setOperationAction(ISD::FCOPYSIGN, MVT::v4f32, Legal);
729 setOperationAction(ISD::FGETSIGN, MVT::v4f32, Expand);
730
731 setOperationAction(ISD::LOAD , MVT::v4f32, Custom);
732 setOperationAction(ISD::STORE , MVT::v4f32, Custom);
733
734 if (!Subtarget.useCRBits())
735 setOperationAction(ISD::SELECT, MVT::v4f32, Expand);
736 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
737
738 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f32, Legal);
739 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f32, Expand);
740 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f32, Expand);
741 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f32, Expand);
742 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f32, Custom);
743 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
744 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
745
746 setOperationAction(ISD::FP_TO_SINT , MVT::v4f32, Legal);
747 setOperationAction(ISD::FP_TO_UINT , MVT::v4f32, Expand);
748
749 setOperationAction(ISD::FNEG , MVT::v4f32, Legal);
750 setOperationAction(ISD::FABS , MVT::v4f32, Legal);
751 setOperationAction(ISD::FSIN , MVT::v4f32, Expand);
752 setOperationAction(ISD::FCOS , MVT::v4f32, Expand);
753 setOperationAction(ISD::FPOWI , MVT::v4f32, Expand);
754 setOperationAction(ISD::FPOW , MVT::v4f32, Expand);
755 setOperationAction(ISD::FLOG , MVT::v4f32, Expand);
756 setOperationAction(ISD::FLOG2 , MVT::v4f32, Expand);
757 setOperationAction(ISD::FLOG10 , MVT::v4f32, Expand);
758 setOperationAction(ISD::FEXP , MVT::v4f32, Expand);
759 setOperationAction(ISD::FEXP2 , MVT::v4f32, Expand);
760
761 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
762 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
763
764 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f32, Legal);
765 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f32, Legal);
766
767 addRegisterClass(MVT::v4f32, &PPC::QSRCRegClass);
768
769 setOperationAction(ISD::AND , MVT::v4i1, Legal);
770 setOperationAction(ISD::OR , MVT::v4i1, Legal);
771 setOperationAction(ISD::XOR , MVT::v4i1, Legal);
772
773 if (!Subtarget.useCRBits())
774 setOperationAction(ISD::SELECT, MVT::v4i1, Expand);
775 setOperationAction(ISD::VSELECT, MVT::v4i1, Legal);
776
777 setOperationAction(ISD::LOAD , MVT::v4i1, Custom);
778 setOperationAction(ISD::STORE , MVT::v4i1, Custom);
779
780 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4i1, Custom);
781 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4i1, Expand);
782 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4i1, Expand);
783 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4i1, Expand);
784 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4i1, Custom);
785 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i1, Expand);
786 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i1, Custom);
787
788 setOperationAction(ISD::SINT_TO_FP, MVT::v4i1, Custom);
789 setOperationAction(ISD::UINT_TO_FP, MVT::v4i1, Custom);
790
791 addRegisterClass(MVT::v4i1, &PPC::QBRCRegClass);
792
793 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
794 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
795 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
796 setOperationAction(ISD::FROUND, MVT::v4f64, Legal);
797
798 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
799 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
800 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
801 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
802
803 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Expand);
804 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
805
806 // These need to set FE_INEXACT, and so cannot be vectorized here.
807 setOperationAction(ISD::FRINT, MVT::v4f64, Expand);
808 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
809
810 if (TM.Options.UnsafeFPMath) {
811 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
812 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
813
814 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
815 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
816 } else {
817 setOperationAction(ISD::FDIV, MVT::v4f64, Expand);
818 setOperationAction(ISD::FSQRT, MVT::v4f64, Expand);
819
820 setOperationAction(ISD::FDIV, MVT::v4f32, Expand);
821 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
822 }
823 }
824
Hal Finkel01fa7702014-12-03 00:19:17 +0000825 if (Subtarget.has64BitSupport())
Hal Finkel322e41a2012-04-01 20:08:17 +0000826 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel01fa7702014-12-03 00:19:17 +0000827
828 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, isPPC64 ? Legal : Custom);
Hal Finkel322e41a2012-04-01 20:08:17 +0000829
Robin Morissete1ca44b2014-10-02 22:27:07 +0000830 if (!isPPC64) {
831 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
832 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
833 }
Eli Friedman7dfa7912011-08-29 18:23:02 +0000834
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000835 setBooleanContents(ZeroOrOneBooleanContent);
Hal Finkelc93a9a22015-02-25 01:06:45 +0000836
837 if (Subtarget.hasAltivec()) {
838 // Altivec instructions set fields to all zeros or all ones.
839 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
840 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000841
Joerg Sonnenbergerb5459e62014-07-24 22:20:10 +0000842 if (!isPPC64) {
843 // These libcalls are not available in 32-bit.
844 setLibcallName(RTLIB::SHL_I128, nullptr);
845 setLibcallName(RTLIB::SRL_I128, nullptr);
846 setLibcallName(RTLIB::SRA_I128, nullptr);
847 }
848
Joseph Tremouletf748c892015-11-07 01:11:31 +0000849 setStackPointerRegisterToSaveRestore(isPPC64 ? PPC::X1 : PPC::R1);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000850
Chris Lattnerf4184352006-03-01 04:57:39 +0000851 // We have target-specific dag combine patterns for the following nodes:
852 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkel5efb9182015-01-06 06:01:57 +0000853 if (Subtarget.hasFPCVT())
854 setTargetDAGCombine(ISD::UINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000855 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000856 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000857 setTargetDAGCombine(ISD::BR_CC);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000858 if (Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000859 setTargetDAGCombine(ISD::BRCOND);
Chris Lattnera7976d32006-07-10 20:56:58 +0000860 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000861 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Bill Schmidtfae5d712014-12-09 16:35:51 +0000862 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
863 setTargetDAGCombine(ISD::INTRINSIC_VOID);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000864
Hal Finkel46043ed2014-03-01 21:36:57 +0000865 setTargetDAGCombine(ISD::SIGN_EXTEND);
866 setTargetDAGCombine(ISD::ZERO_EXTEND);
867 setTargetDAGCombine(ISD::ANY_EXTEND);
868
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000869 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000870 setTargetDAGCombine(ISD::TRUNCATE);
871 setTargetDAGCombine(ISD::SETCC);
872 setTargetDAGCombine(ISD::SELECT_CC);
873 }
874
Hal Finkel2e103312013-04-03 04:01:11 +0000875 // Use reciprocal estimates.
876 if (TM.Options.UnsafeFPMath) {
877 setTargetDAGCombine(ISD::FDIV);
878 setTargetDAGCombine(ISD::FSQRT);
879 }
880
Dale Johannesen10432e52007-10-19 00:59:18 +0000881 // Darwin long double math library functions have $LDBL128 appended.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000882 if (Subtarget.isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000883 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000884 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
885 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000886 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
887 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000888 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
889 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
890 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
891 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
892 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000893 }
894
Hal Finkel940ab932014-02-28 00:27:01 +0000895 // With 32 condition bits, we don't need to sink (and duplicate) compares
896 // aggressively in CodeGenPrep.
Hal Finkel7a0516e2015-02-12 01:02:52 +0000897 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000898 setHasMultipleConditionRegisters();
Hal Finkel7a0516e2015-02-12 01:02:52 +0000899 setJumpIsExpensive();
900 }
Hal Finkel940ab932014-02-28 00:27:01 +0000901
Hal Finkel65298572011-10-17 18:53:03 +0000902 setMinFunctionAlignment(2);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000903 if (Subtarget.isDarwin())
Hal Finkel65298572011-10-17 18:53:03 +0000904 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000905
Hal Finkeld73bfba2015-01-03 14:58:25 +0000906 switch (Subtarget.getDarwinDirective()) {
907 default: break;
908 case PPC::DIR_970:
909 case PPC::DIR_A2:
910 case PPC::DIR_E500mc:
911 case PPC::DIR_E5500:
912 case PPC::DIR_PWR4:
913 case PPC::DIR_PWR5:
914 case PPC::DIR_PWR5X:
915 case PPC::DIR_PWR6:
916 case PPC::DIR_PWR6X:
917 case PPC::DIR_PWR7:
918 case PPC::DIR_PWR8:
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +0000919 case PPC::DIR_PWR9:
Hal Finkeld73bfba2015-01-03 14:58:25 +0000920 setPrefFunctionAlignment(4);
921 setPrefLoopAlignment(4);
922 break;
923 }
924
Eli Friedman30a49e92011-08-03 21:06:02 +0000925
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000926 if (Subtarget.enableMachineScheduler())
Hal Finkel21442b22013-09-11 23:05:25 +0000927 setSchedulingPreference(Sched::Source);
928 else
929 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000930
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000931 computeRegisterProperties(STI.getRegisterInfo());
Hal Finkel742b5352012-08-28 16:12:39 +0000932
Hal Finkeld73bfba2015-01-03 14:58:25 +0000933 // The Freescale cores do better with aggressive inlining of memcpy and
934 // friends. GCC uses same threshold of 128 bytes (= 32 word stores).
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000935 if (Subtarget.getDarwinDirective() == PPC::DIR_E500mc ||
936 Subtarget.getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000937 MaxStoresPerMemset = 32;
938 MaxStoresPerMemsetOptSize = 16;
939 MaxStoresPerMemcpy = 32;
940 MaxStoresPerMemcpyOptSize = 8;
941 MaxStoresPerMemmove = 32;
942 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel5c3cacf2015-02-27 19:58:28 +0000943 } else if (Subtarget.getDarwinDirective() == PPC::DIR_A2) {
944 // The A2 also benefits from (very) aggressive inlining of memcpy and
945 // friends. The overhead of a the function call, even when warm, can be
946 // over one hundred cycles.
947 MaxStoresPerMemset = 128;
948 MaxStoresPerMemcpy = 128;
949 MaxStoresPerMemmove = 128;
Hal Finkel742b5352012-08-28 16:12:39 +0000950 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000951}
952
Hal Finkel262a2242013-09-12 23:20:06 +0000953/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
954/// the desired ByVal argument alignment.
Pete Cooper2e201472015-07-27 17:15:24 +0000955static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
Hal Finkel262a2242013-09-12 23:20:06 +0000956 unsigned MaxMaxAlign) {
957 if (MaxAlign == MaxMaxAlign)
958 return;
Pete Cooper2e201472015-07-27 17:15:24 +0000959 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Hal Finkel262a2242013-09-12 23:20:06 +0000960 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
961 MaxAlign = 32;
962 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
963 MaxAlign = 16;
Pete Cooper2e201472015-07-27 17:15:24 +0000964 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Hal Finkel262a2242013-09-12 23:20:06 +0000965 unsigned EltAlign = 0;
966 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
967 if (EltAlign > MaxAlign)
968 MaxAlign = EltAlign;
Pete Cooper2e201472015-07-27 17:15:24 +0000969 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
970 for (auto *EltTy : STy->elements()) {
Hal Finkel262a2242013-09-12 23:20:06 +0000971 unsigned EltAlign = 0;
Pete Cooper0debbdc2015-07-24 18:55:49 +0000972 getMaxByValAlign(EltTy, EltAlign, MaxMaxAlign);
Hal Finkel262a2242013-09-12 23:20:06 +0000973 if (EltAlign > MaxAlign)
974 MaxAlign = EltAlign;
975 if (MaxAlign == MaxMaxAlign)
976 break;
977 }
978 }
979}
980
Dale Johannesencbde4c22008-02-28 22:31:51 +0000981/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
982/// function arguments in the caller parameter area.
Mehdi Amini5c183d52015-07-09 02:09:28 +0000983unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty,
984 const DataLayout &DL) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000985 // Darwin passes everything on 4 byte boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000986 if (Subtarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000987 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000988
989 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000990 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000991 unsigned Align = Subtarget.isPPC64() ? 8 : 4;
992 if (Subtarget.hasAltivec() || Subtarget.hasQPX())
993 getMaxByValAlign(Ty, Align, Subtarget.hasQPX() ? 32 : 16);
Hal Finkel262a2242013-09-12 23:20:06 +0000994 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000995}
996
Petar Jovanovic280f7102015-12-14 17:57:33 +0000997bool PPCTargetLowering::useSoftFloat() const {
998 return Subtarget.useSoftFloat();
999}
1000
Chris Lattner347ed8a2006-01-09 23:52:17 +00001001const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001002 switch ((PPCISD::NodeType)Opcode) {
1003 case PPCISD::FIRST_NUMBER: break;
Evan Cheng32e376f2008-07-12 02:23:19 +00001004 case PPCISD::FSEL: return "PPCISD::FSEL";
1005 case PPCISD::FCFID: return "PPCISD::FCFID";
Hal Finkel3fe09ea2015-01-06 07:02:15 +00001006 case PPCISD::FCFIDU: return "PPCISD::FCFIDU";
1007 case PPCISD::FCFIDS: return "PPCISD::FCFIDS";
1008 case PPCISD::FCFIDUS: return "PPCISD::FCFIDUS";
Evan Cheng32e376f2008-07-12 02:23:19 +00001009 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
1010 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel3fe09ea2015-01-06 07:02:15 +00001011 case PPCISD::FCTIDUZ: return "PPCISD::FCTIDUZ";
1012 case PPCISD::FCTIWUZ: return "PPCISD::FCTIWUZ";
Hal Finkel2e103312013-04-03 04:01:11 +00001013 case PPCISD::FRE: return "PPCISD::FRE";
1014 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +00001015 case PPCISD::STFIWX: return "PPCISD::STFIWX";
1016 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
1017 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
1018 case PPCISD::VPERM: return "PPCISD::VPERM";
Nemanja Ivanovic1a2b2f02016-05-04 16:04:02 +00001019 case PPCISD::XXSPLT: return "PPCISD::XXSPLT";
Hal Finkel4edc66b2015-01-03 01:16:37 +00001020 case PPCISD::CMPB: return "PPCISD::CMPB";
Evan Cheng32e376f2008-07-12 02:23:19 +00001021 case PPCISD::Hi: return "PPCISD::Hi";
1022 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001023 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Evan Cheng32e376f2008-07-12 02:23:19 +00001024 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
Yury Gribovd7dbb662015-12-01 11:40:55 +00001025 case PPCISD::DYNAREAOFFSET: return "PPCISD::DYNAREAOFFSET";
Evan Cheng32e376f2008-07-12 02:23:19 +00001026 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
1027 case PPCISD::SRL: return "PPCISD::SRL";
1028 case PPCISD::SRA: return "PPCISD::SRA";
1029 case PPCISD::SHL: return "PPCISD::SHL";
Matthias Braund04893f2015-05-07 21:33:59 +00001030 case PPCISD::SRA_ADDZE: return "PPCISD::SRA_ADDZE";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00001031 case PPCISD::CALL: return "PPCISD::CALL";
1032 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +00001033 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00001034 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Hal Finkelfc096c92014-12-23 22:29:40 +00001035 case PPCISD::BCTRL_LOAD_TOC: return "PPCISD::BCTRL_LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +00001036 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkelbbdee932014-12-02 22:01:00 +00001037 case PPCISD::READ_TIME_BASE: return "PPCISD::READ_TIME_BASE";
Hal Finkel756810f2013-03-21 21:37:52 +00001038 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
1039 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001040 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001041 case PPCISD::MFVSR: return "PPCISD::MFVSR";
1042 case PPCISD::MTVSRA: return "PPCISD::MTVSRA";
1043 case PPCISD::MTVSRZ: return "PPCISD::MTVSRZ";
Matthias Braund04893f2015-05-07 21:33:59 +00001044 case PPCISD::ANDIo_1_EQ_BIT: return "PPCISD::ANDIo_1_EQ_BIT";
1045 case PPCISD::ANDIo_1_GT_BIT: return "PPCISD::ANDIo_1_GT_BIT";
Evan Cheng32e376f2008-07-12 02:23:19 +00001046 case PPCISD::VCMP: return "PPCISD::VCMP";
1047 case PPCISD::VCMPo: return "PPCISD::VCMPo";
1048 case PPCISD::LBRX: return "PPCISD::LBRX";
1049 case PPCISD::STBRX: return "PPCISD::STBRX";
Hal Finkel3fe09ea2015-01-06 07:02:15 +00001050 case PPCISD::LFIWAX: return "PPCISD::LFIWAX";
1051 case PPCISD::LFIWZX: return "PPCISD::LFIWZX";
Matthias Braund04893f2015-05-07 21:33:59 +00001052 case PPCISD::LXVD2X: return "PPCISD::LXVD2X";
1053 case PPCISD::STXVD2X: return "PPCISD::STXVD2X";
Evan Cheng32e376f2008-07-12 02:23:19 +00001054 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +00001055 case PPCISD::BDNZ: return "PPCISD::BDNZ";
1056 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +00001057 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +00001058 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +00001059 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +00001060 case PPCISD::CR6SET: return "PPCISD::CR6SET";
1061 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Roman Divacky32143e22013-12-20 18:08:54 +00001062 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Matthias Braund04893f2015-05-07 21:33:59 +00001063 case PPCISD::PPC32_PICGOT: return "PPCISD::PPC32_PICGOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001064 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
1065 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001066 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001067 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
1068 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
Bill Schmidt82f1c772015-02-10 19:09:05 +00001069 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
1070 case PPCISD::ADDI_TLSGD_L_ADDR: return "PPCISD::ADDI_TLSGD_L_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001071 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
1072 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
Bill Schmidt82f1c772015-02-10 19:09:05 +00001073 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
1074 case PPCISD::ADDI_TLSLD_L_ADDR: return "PPCISD::ADDI_TLSLD_L_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001075 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
1076 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +00001077 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +00001078 case PPCISD::SC: return "PPCISD::SC";
Bill Schmidte26236e2015-05-22 16:44:10 +00001079 case PPCISD::CLRBHRB: return "PPCISD::CLRBHRB";
1080 case PPCISD::MFBHRBE: return "PPCISD::MFBHRBE";
1081 case PPCISD::RFEBB: return "PPCISD::RFEBB";
Matthias Braund04893f2015-05-07 21:33:59 +00001082 case PPCISD::XXSWAPD: return "PPCISD::XXSWAPD";
Hal Finkelc93a9a22015-02-25 01:06:45 +00001083 case PPCISD::QVFPERM: return "PPCISD::QVFPERM";
1084 case PPCISD::QVGPCI: return "PPCISD::QVGPCI";
1085 case PPCISD::QVALIGNI: return "PPCISD::QVALIGNI";
1086 case PPCISD::QVESPLATI: return "PPCISD::QVESPLATI";
1087 case PPCISD::QBFLT: return "PPCISD::QBFLT";
1088 case PPCISD::QVLFSb: return "PPCISD::QVLFSb";
Chris Lattner347ed8a2006-01-09 23:52:17 +00001089 }
Matthias Braund04893f2015-05-07 21:33:59 +00001090 return nullptr;
Chris Lattner347ed8a2006-01-09 23:52:17 +00001091}
1092
Mehdi Amini44ede332015-07-09 02:09:04 +00001093EVT PPCTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &C,
1094 EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00001095 if (!VT.isVector())
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001096 return Subtarget.useCRBits() ? MVT::i1 : MVT::i32;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001097
1098 if (Subtarget.hasQPX())
1099 return EVT::getVectorVT(C, MVT::i1, VT.getVectorNumElements());
1100
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00001101 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +00001102}
1103
Hal Finkel62ac7362014-09-19 11:42:56 +00001104bool PPCTargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1105 assert(VT.isFloatingPoint() && "Non-floating-point FMA?");
1106 return true;
1107}
1108
Chris Lattner4211ca92006-04-14 06:01:58 +00001109//===----------------------------------------------------------------------===//
1110// Node matching predicates, for use by the tblgen matching code.
1111//===----------------------------------------------------------------------===//
1112
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001113/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001114static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001115 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001116 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001117 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001118 // Maybe this has already been legalized into the constant pool?
1119 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001120 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001121 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001122 }
1123 return false;
1124}
1125
Chris Lattnere8b83b42006-04-06 17:23:16 +00001126/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
1127/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001128static bool isConstantOrUndef(int Op, int Val) {
1129 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001130}
1131
1132/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
1133/// VPKUHUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001134/// The ShuffleKind distinguishes between big-endian operations with
1135/// two different inputs (0), either-endian operations with two identical
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001136/// inputs (1), and little-endian operations with two different inputs (2).
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001137/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1138bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +00001139 SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001140 bool IsLE = DAG.getDataLayout().isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001141 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +00001142 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001143 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001144 for (unsigned i = 0; i != 16; ++i)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001145 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001146 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001147 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +00001148 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001149 return false;
1150 for (unsigned i = 0; i != 16; ++i)
1151 if (!isConstantOrUndef(N->getMaskElt(i), i*2))
1152 return false;
1153 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +00001154 unsigned j = IsLE ? 0 : 1;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001155 for (unsigned i = 0; i != 8; ++i)
Bill Schmidtf910a062014-06-10 14:35:01 +00001156 if (!isConstantOrUndef(N->getMaskElt(i), i*2+j) ||
1157 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001158 return false;
1159 }
Chris Lattner1d338192006-04-06 18:26:28 +00001160 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001161}
1162
1163/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
1164/// VPKUWUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001165/// The ShuffleKind distinguishes between big-endian operations with
1166/// two different inputs (0), either-endian operations with two identical
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001167/// inputs (1), and little-endian operations with two different inputs (2).
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001168/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1169bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +00001170 SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001171 bool IsLE = DAG.getDataLayout().isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001172 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +00001173 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001174 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001175 for (unsigned i = 0; i != 16; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001176 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
1177 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001178 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001179 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +00001180 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001181 return false;
1182 for (unsigned i = 0; i != 16; i += 2)
1183 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) ||
1184 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1))
1185 return false;
1186 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +00001187 unsigned j = IsLE ? 0 : 2;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001188 for (unsigned i = 0; i != 8; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001189 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) ||
1190 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) ||
1191 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) ||
1192 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001193 return false;
1194 }
Chris Lattner1d338192006-04-06 18:26:28 +00001195 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001196}
1197
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001198/// isVPKUDUMShuffleMask - Return true if this is the shuffle mask for a
Bill Schmidte13ac912015-05-21 20:48:49 +00001199/// VPKUDUM instruction, AND the VPKUDUM instruction exists for the
1200/// current subtarget.
1201///
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001202/// The ShuffleKind distinguishes between big-endian operations with
1203/// two different inputs (0), either-endian operations with two identical
1204/// inputs (1), and little-endian operations with two different inputs (2).
1205/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1206bool PPC::isVPKUDUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
1207 SelectionDAG &DAG) {
Bill Schmidte13ac912015-05-21 20:48:49 +00001208 const PPCSubtarget& Subtarget =
1209 static_cast<const PPCSubtarget&>(DAG.getSubtarget());
1210 if (!Subtarget.hasP8Vector())
1211 return false;
1212
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001213 bool IsLE = DAG.getDataLayout().isLittleEndian();
Bill Schmidt5ed84cd2015-05-16 01:02:12 +00001214 if (ShuffleKind == 0) {
1215 if (IsLE)
1216 return false;
1217 for (unsigned i = 0; i != 16; i += 4)
1218 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+4) ||
1219 !isConstantOrUndef(N->getMaskElt(i+1), i*2+5) ||
1220 !isConstantOrUndef(N->getMaskElt(i+2), i*2+6) ||
1221 !isConstantOrUndef(N->getMaskElt(i+3), i*2+7))
1222 return false;
1223 } else if (ShuffleKind == 2) {
1224 if (!IsLE)
1225 return false;
1226 for (unsigned i = 0; i != 16; i += 4)
1227 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) ||
1228 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1) ||
1229 !isConstantOrUndef(N->getMaskElt(i+2), i*2+2) ||
1230 !isConstantOrUndef(N->getMaskElt(i+3), i*2+3))
1231 return false;
1232 } else if (ShuffleKind == 1) {
1233 unsigned j = IsLE ? 0 : 4;
1234 for (unsigned i = 0; i != 8; i += 4)
1235 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) ||
1236 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) ||
1237 !isConstantOrUndef(N->getMaskElt(i+2), i*2+j+2) ||
1238 !isConstantOrUndef(N->getMaskElt(i+3), i*2+j+3) ||
1239 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) ||
1240 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1) ||
1241 !isConstantOrUndef(N->getMaskElt(i+10), i*2+j+2) ||
1242 !isConstantOrUndef(N->getMaskElt(i+11), i*2+j+3))
1243 return false;
1244 }
1245 return true;
1246}
1247
Chris Lattnerf38e0332006-04-06 22:02:42 +00001248/// isVMerge - Common function, used to match vmrg* shuffles.
1249///
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001250static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +00001251 unsigned LHSStart, unsigned RHSStart) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001252 if (N->getValueType(0) != MVT::v16i8)
1253 return false;
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001254 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
1255 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001256
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001257 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
1258 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001259 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +00001260 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001261 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +00001262 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001263 return false;
1264 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001265 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +00001266}
1267
1268/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +00001269/// a VMRGL* instruction with the specified unit size (1,2 or 4 bytes).
NAKAMURA Takumi84965032015-09-22 11:14:12 +00001270/// The ShuffleKind distinguishes between big-endian merges with two
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001271/// different inputs (0), either-endian merges with two identical inputs (1),
1272/// and little-endian merges with two different inputs (2). For the latter,
1273/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +00001274bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001275 unsigned ShuffleKind, SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001276 if (DAG.getDataLayout().isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001277 if (ShuffleKind == 1) // unary
1278 return isVMerge(N, UnitSize, 0, 0);
1279 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +00001280 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001281 else
1282 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001283 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001284 if (ShuffleKind == 1) // unary
1285 return isVMerge(N, UnitSize, 8, 8);
1286 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +00001287 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001288 else
1289 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001290 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001291}
1292
1293/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +00001294/// a VMRGH* instruction with the specified unit size (1,2 or 4 bytes).
NAKAMURA Takumi84965032015-09-22 11:14:12 +00001295/// The ShuffleKind distinguishes between big-endian merges with two
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001296/// different inputs (0), either-endian merges with two identical inputs (1),
1297/// and little-endian merges with two different inputs (2). For the latter,
1298/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +00001299bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001300 unsigned ShuffleKind, SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001301 if (DAG.getDataLayout().isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001302 if (ShuffleKind == 1) // unary
1303 return isVMerge(N, UnitSize, 8, 8);
1304 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +00001305 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001306 else
1307 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001308 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001309 if (ShuffleKind == 1) // unary
1310 return isVMerge(N, UnitSize, 0, 0);
1311 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +00001312 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001313 else
1314 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001315 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001316}
1317
Kit Barton13894c72015-06-25 15:17:40 +00001318/**
1319 * \brief Common function used to match vmrgew and vmrgow shuffles
1320 *
1321 * The indexOffset determines whether to look for even or odd words in
1322 * the shuffle mask. This is based on the of the endianness of the target
1323 * machine.
1324 * - Little Endian:
1325 * - Use offset of 0 to check for odd elements
1326 * - Use offset of 4 to check for even elements
1327 * - Big Endian:
1328 * - Use offset of 0 to check for even elements
1329 * - Use offset of 4 to check for odd elements
1330 * A detailed description of the vector element ordering for little endian and
NAKAMURA Takumi520b45d2015-06-25 23:38:44 +00001331 * big endian can be found at
1332 * http://www.ibm.com/developerworks/library/l-ibm-xl-c-cpp-compiler/index.html
Kit Barton13894c72015-06-25 15:17:40 +00001333 * Targeting your applications - what little endian and big endian IBM XL C/C++
NAKAMURA Takumi520b45d2015-06-25 23:38:44 +00001334 * compiler differences mean to you
Kit Barton13894c72015-06-25 15:17:40 +00001335 *
1336 * The mask to the shuffle vector instruction specifies the indices of the
1337 * elements from the two input vectors to place in the result. The elements are
1338 * numbered in array-access order, starting with the first vector. These vectors
1339 * are always of type v16i8, thus each vector will contain 16 elements of size
NAKAMURA Takumi520b45d2015-06-25 23:38:44 +00001340 * 8. More info on the shuffle vector can be found in the
1341 * http://llvm.org/docs/LangRef.html#shufflevector-instruction
1342 * Language Reference.
Kit Barton13894c72015-06-25 15:17:40 +00001343 *
1344 * The RHSStartValue indicates whether the same input vectors are used (unary)
1345 * or two different input vectors are used, based on the following:
1346 * - If the instruction uses the same vector for both inputs, the range of the
1347 * indices will be 0 to 15. In this case, the RHSStart value passed should
1348 * be 0.
1349 * - If the instruction has two different vectors then the range of the
1350 * indices will be 0 to 31. In this case, the RHSStart value passed should
1351 * be 16 (indices 0-15 specify elements in the first vector while indices 16
1352 * to 31 specify elements in the second vector).
1353 *
1354 * \param[in] N The shuffle vector SD Node to analyze
1355 * \param[in] IndexOffset Specifies whether to look for even or odd elements
1356 * \param[in] RHSStartValue Specifies the starting index for the righthand input
1357 * vector to the shuffle_vector instruction
1358 * \return true iff this shuffle vector represents an even or odd word merge
1359 */
1360static bool isVMerge(ShuffleVectorSDNode *N, unsigned IndexOffset,
1361 unsigned RHSStartValue) {
1362 if (N->getValueType(0) != MVT::v16i8)
1363 return false;
1364
1365 for (unsigned i = 0; i < 2; ++i)
1366 for (unsigned j = 0; j < 4; ++j)
1367 if (!isConstantOrUndef(N->getMaskElt(i*4+j),
1368 i*RHSStartValue+j+IndexOffset) ||
1369 !isConstantOrUndef(N->getMaskElt(i*4+j+8),
1370 i*RHSStartValue+j+IndexOffset+8))
1371 return false;
1372 return true;
1373}
1374
1375/**
1376 * \brief Determine if the specified shuffle mask is suitable for the vmrgew or
1377 * vmrgow instructions.
1378 *
1379 * \param[in] N The shuffle vector SD Node to analyze
1380 * \param[in] CheckEven Check for an even merge (true) or an odd merge (false)
1381 * \param[in] ShuffleKind Identify the type of merge:
1382 * - 0 = big-endian merge with two different inputs;
1383 * - 1 = either-endian merge with two identical inputs;
1384 * - 2 = little-endian merge with two different inputs (inputs are swapped for
1385 * little-endian merges).
1386 * \param[in] DAG The current SelectionDAG
NAKAMURA Takumi84965032015-09-22 11:14:12 +00001387 * \return true iff this shuffle mask
Kit Barton13894c72015-06-25 15:17:40 +00001388 */
1389bool PPC::isVMRGEOShuffleMask(ShuffleVectorSDNode *N, bool CheckEven,
1390 unsigned ShuffleKind, SelectionDAG &DAG) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001391 if (DAG.getDataLayout().isLittleEndian()) {
Kit Barton13894c72015-06-25 15:17:40 +00001392 unsigned indexOffset = CheckEven ? 4 : 0;
1393 if (ShuffleKind == 1) // Unary
1394 return isVMerge(N, indexOffset, 0);
1395 else if (ShuffleKind == 2) // swapped
1396 return isVMerge(N, indexOffset, 16);
1397 else
1398 return false;
1399 }
1400 else {
1401 unsigned indexOffset = CheckEven ? 0 : 4;
1402 if (ShuffleKind == 1) // Unary
1403 return isVMerge(N, indexOffset, 0);
1404 else if (ShuffleKind == 0) // Normal
1405 return isVMerge(N, indexOffset, 16);
1406 else
1407 return false;
1408 }
1409 return false;
1410}
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001411
Chris Lattner1d338192006-04-06 18:26:28 +00001412/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
1413/// amount, otherwise return -1.
NAKAMURA Takumi84965032015-09-22 11:14:12 +00001414/// The ShuffleKind distinguishes between big-endian operations with two
Bill Schmidt42a69362014-08-05 20:47:25 +00001415/// different inputs (0), either-endian operations with two identical inputs
1416/// (1), and little-endian operations with two different inputs (2). For the
1417/// latter, the input operands are swapped (see PPCInstrAltivec.td).
1418int PPC::isVSLDOIShuffleMask(SDNode *N, unsigned ShuffleKind,
1419 SelectionDAG &DAG) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001420 if (N->getValueType(0) != MVT::v16i8)
Hal Finkela775e512014-04-08 19:00:27 +00001421 return -1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001422
1423 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +00001424
Chris Lattner1d338192006-04-06 18:26:28 +00001425 // Find the first non-undef value in the shuffle mask.
1426 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001427 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +00001428 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001429
Chris Lattner1d338192006-04-06 18:26:28 +00001430 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001431
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001432 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +00001433 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001434 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +00001435 if (ShiftAmt < i) return -1;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001436
Bill Schmidtf04e9982014-08-04 23:21:01 +00001437 ShiftAmt -= i;
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001438 bool isLE = DAG.getDataLayout().isLittleEndian();
Bill Schmidtf910a062014-06-10 14:35:01 +00001439
Bill Schmidt42a69362014-08-05 20:47:25 +00001440 if ((ShuffleKind == 0 && !isLE) || (ShuffleKind == 2 && isLE)) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001441 // Check the rest of the elements to see if they are consecutive.
1442 for (++i; i != 16; ++i)
1443 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
1444 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001445 } else if (ShuffleKind == 1) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001446 // Check the rest of the elements to see if they are consecutive.
1447 for (++i; i != 16; ++i)
1448 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
1449 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001450 } else
1451 return -1;
1452
Bill Schmidt1e77bb12015-07-15 15:45:30 +00001453 if (isLE)
Bill Schmidt42a69362014-08-05 20:47:25 +00001454 ShiftAmt = 16 - ShiftAmt;
Bill Schmidtf04e9982014-08-04 23:21:01 +00001455
Chris Lattner1d338192006-04-06 18:26:28 +00001456 return ShiftAmt;
1457}
Chris Lattnerffc47562006-03-20 06:33:01 +00001458
1459/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
1460/// specifies a splat of a single element that is suitable for input to
1461/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001462bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +00001463 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +00001464 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001465
Bill Schmidt42ddd712015-07-29 14:31:57 +00001466 // The consecutive indices need to specify an element, not part of two
1467 // different elements. So abandon ship early if this isn't the case.
1468 if (N->getMaskElt(0) % EltSize != 0)
1469 return false;
1470
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001471 // This is a splat operation if each element of the permute is the same, and
1472 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001473 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001474
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001475 // FIXME: Handle UNDEF elements too!
1476 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +00001477 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001478
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001479 // Check that the indices are consecutive, in the case of a multi-byte element
1480 // splatted with a v16i8 mask.
1481 for (unsigned i = 1; i != EltSize; ++i)
1482 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001483 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001484
Chris Lattner95c7adc2006-04-04 17:25:31 +00001485 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001486 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +00001487 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001488 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001489 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001490 }
Chris Lattner95c7adc2006-04-04 17:25:31 +00001491 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +00001492}
1493
1494/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
1495/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Bill Schmidtf910a062014-06-10 14:35:01 +00001496unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize,
1497 SelectionDAG &DAG) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001498 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1499 assert(isSplatShuffleMask(SVOp, EltSize));
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001500 if (DAG.getDataLayout().isLittleEndian())
Bill Schmidtf910a062014-06-10 14:35:01 +00001501 return (16 / EltSize) - 1 - (SVOp->getMaskElt(0) / EltSize);
1502 else
1503 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +00001504}
1505
Chris Lattner74cf9ff2006-04-12 17:37:20 +00001506/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001507/// by using a vspltis[bhw] instruction of the specified element size, return
1508/// the constant being splatted. The ByteSize field indicates the number of
1509/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001510SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001511 SDValue OpVal(nullptr, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001512
1513 // If ByteSize of the splat is bigger than the element size of the
1514 // build_vector, then we have a case where we are checking for a splat where
1515 // multiple elements of the buildvector are folded together into a single
1516 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
1517 unsigned EltSize = 16/N->getNumOperands();
1518 if (EltSize < ByteSize) {
1519 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001520 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001521 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001522
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001523 // See if all of the elements in the buildvector agree across.
1524 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00001525 if (N->getOperand(i).isUndef()) continue;
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001526 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001527 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001528
Scott Michelcf0da6c2009-02-17 22:15:04 +00001529
Craig Topper062a2ba2014-04-25 05:30:21 +00001530 if (!UniquedVals[i&(Multiple-1)].getNode())
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001531 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
1532 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001533 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001534 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001535
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001536 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
1537 // either constant or undef values that are identical for each chunk. See
1538 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001539
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001540 // Check to see if all of the leading entries are either 0 or -1. If
1541 // neither, then this won't fit into the immediate field.
1542 bool LeadingZero = true;
1543 bool LeadingOnes = true;
1544 for (unsigned i = 0; i != Multiple-1; ++i) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001545 if (!UniquedVals[i].getNode()) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001546
Artyom Skrobov314ee042015-11-25 19:41:11 +00001547 LeadingZero &= isNullConstant(UniquedVals[i]);
1548 LeadingOnes &= isAllOnesConstant(UniquedVals[i]);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001549 }
1550 // Finally, check the least significant entry.
1551 if (LeadingZero) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001552 if (!UniquedVals[Multiple-1].getNode())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001553 return DAG.getTargetConstant(0, SDLoc(N), MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00001554 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001555 if (Val < 16) // 0,0,0,4 -> vspltisw(4)
1556 return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001557 }
1558 if (LeadingOnes) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001559 if (!UniquedVals[Multiple-1].getNode())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001560 return DAG.getTargetConstant(~0U, SDLoc(N), MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +00001561 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001562 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001563 return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001564 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001565
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001566 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001567 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001568
Chris Lattner2771e2c2006-03-25 06:12:06 +00001569 // Check to see if this buildvec has a single non-undef value in its elements.
1570 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00001571 if (N->getOperand(i).isUndef()) continue;
Craig Topper062a2ba2014-04-25 05:30:21 +00001572 if (!OpVal.getNode())
Chris Lattner2771e2c2006-03-25 06:12:06 +00001573 OpVal = N->getOperand(i);
1574 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001575 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001576 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001577
Craig Topper062a2ba2014-04-25 05:30:21 +00001578 if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001579
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001580 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001581 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001582 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001583 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001584 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001585 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001586 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001587 }
1588
1589 // If the splat value is larger than the element value, then we can never do
1590 // this splat. The only case that we could fit the replicated bits into our
1591 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001592 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001593
Benjamin Kramerb4b51502015-03-25 16:49:59 +00001594 // If the element value is larger than the splat value, check if it consists
1595 // of a repeated bit pattern of size ByteSize.
1596 if (!APInt(ValSizeInBytes * 8, Value).isSplat(ByteSize * 8))
1597 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001598
1599 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001600 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001601
Evan Chengb1ddc982006-03-26 09:52:32 +00001602 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001603 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001604
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001605 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001606 if (SignExtend32<5>(MaskVal) == MaskVal)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001607 return DAG.getTargetConstant(MaskVal, SDLoc(N), MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001608 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001609}
1610
Hal Finkelc93a9a22015-02-25 01:06:45 +00001611/// isQVALIGNIShuffleMask - If this is a qvaligni shuffle mask, return the shift
1612/// amount, otherwise return -1.
1613int PPC::isQVALIGNIShuffleMask(SDNode *N) {
1614 EVT VT = N->getValueType(0);
1615 if (VT != MVT::v4f64 && VT != MVT::v4f32 && VT != MVT::v4i1)
1616 return -1;
1617
1618 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1619
1620 // Find the first non-undef value in the shuffle mask.
1621 unsigned i;
1622 for (i = 0; i != 4 && SVOp->getMaskElt(i) < 0; ++i)
1623 /*search*/;
1624
1625 if (i == 4) return -1; // all undef.
1626
1627 // Otherwise, check to see if the rest of the elements are consecutively
1628 // numbered from this value.
1629 unsigned ShiftAmt = SVOp->getMaskElt(i);
1630 if (ShiftAmt < i) return -1;
1631 ShiftAmt -= i;
1632
1633 // Check the rest of the elements to see if they are consecutive.
1634 for (++i; i != 4; ++i)
1635 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
1636 return -1;
1637
1638 return ShiftAmt;
1639}
1640
Chris Lattner4211ca92006-04-14 06:01:58 +00001641//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001642// Addressing Mode Selection
1643//===----------------------------------------------------------------------===//
1644
1645/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1646/// or 64-bit immediate, and if the value can be accurately represented as a
1647/// sign extension from a 16-bit value. If so, this returns true and the
1648/// immediate.
1649static bool isIntS16Immediate(SDNode *N, short &Imm) {
Adam Nemet571eb5f2014-05-20 17:20:34 +00001650 if (!isa<ConstantSDNode>(N))
Chris Lattnera801fced2006-11-08 02:15:41 +00001651 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001652
Dan Gohmaneffb8942008-09-12 16:56:44 +00001653 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001654 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001655 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001656 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001657 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001658}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001659static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001660 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001661}
1662
Chris Lattnera801fced2006-11-08 02:15:41 +00001663/// SelectAddressRegReg - Given the specified addressed, check to see if it
1664/// can be represented as an indexed [r+r] operation. Returns false if it
1665/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001666bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1667 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001668 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001669 short imm = 0;
1670 if (N.getOpcode() == ISD::ADD) {
1671 if (isIntS16Immediate(N.getOperand(1), imm))
1672 return false; // r+i
1673 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1674 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001675
Chris Lattnera801fced2006-11-08 02:15:41 +00001676 Base = N.getOperand(0);
1677 Index = N.getOperand(1);
1678 return true;
1679 } else if (N.getOpcode() == ISD::OR) {
1680 if (isIntS16Immediate(N.getOperand(1), imm))
1681 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001682
Chris Lattnera801fced2006-11-08 02:15:41 +00001683 // If this is an or of disjoint bitfields, we can codegen this as an add
1684 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1685 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001686 APInt LHSKnownZero, LHSKnownOne;
1687 APInt RHSKnownZero, RHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001688 DAG.computeKnownBits(N.getOperand(0),
1689 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001690
Dan Gohmanf19609a2008-02-27 01:23:58 +00001691 if (LHSKnownZero.getBoolValue()) {
Jay Foada0653a32014-05-14 21:14:37 +00001692 DAG.computeKnownBits(N.getOperand(1),
1693 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001694 // If all of the bits are known zero on the LHS or RHS, the add won't
1695 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001696 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001697 Base = N.getOperand(0);
1698 Index = N.getOperand(1);
1699 return true;
1700 }
1701 }
1702 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001703
Chris Lattnera801fced2006-11-08 02:15:41 +00001704 return false;
1705}
1706
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001707// If we happen to be doing an i64 load or store into a stack slot that has
1708// less than a 4-byte alignment, then the frame-index elimination may need to
1709// use an indexed load or store instruction (because the offset may not be a
1710// multiple of 4). The extra register needed to hold the offset comes from the
1711// register scavenger, and it is possible that the scavenger will need to use
1712// an emergency spill slot. As a result, we need to make sure that a spill slot
1713// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1714// stack slot.
1715static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1716 // FIXME: This does not handle the LWA case.
1717 if (VT != MVT::i64)
1718 return;
1719
Hal Finkel7ab3db52013-07-10 15:29:01 +00001720 // NOTE: We'll exclude negative FIs here, which come from argument
1721 // lowering, because there are no known test cases triggering this problem
1722 // using packed structures (or similar). We can remove this exclusion if
1723 // we find such a test case. The reason why this is so test-case driven is
1724 // because this entire 'fixup' is only to prevent crashes (from the
1725 // register scavenger) on not-really-valid inputs. For example, if we have:
1726 // %a = alloca i1
1727 // %b = bitcast i1* %a to i64*
1728 // store i64* a, i64 b
1729 // then the store should really be marked as 'align 1', but is not. If it
1730 // were marked as 'align 1' then the indexed form would have been
1731 // instruction-selected initially, and the problem this 'fixup' is preventing
1732 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001733 if (FrameIdx < 0)
1734 return;
1735
1736 MachineFunction &MF = DAG.getMachineFunction();
1737 MachineFrameInfo *MFI = MF.getFrameInfo();
1738
1739 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1740 if (Align >= 4)
1741 return;
1742
1743 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1744 FuncInfo->setHasNonRISpills();
1745}
1746
Chris Lattnera801fced2006-11-08 02:15:41 +00001747/// Returns true if the address N can be represented by a base register plus
1748/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001749/// represented as reg+reg. If Aligned is true, only accept displacements
1750/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001751bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001752 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001753 SelectionDAG &DAG,
1754 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001755 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001756 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001757 // If this can be more profitably realized as r+r, fail.
1758 if (SelectAddressRegReg(N, Disp, Base, DAG))
1759 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001760
Chris Lattnera801fced2006-11-08 02:15:41 +00001761 if (N.getOpcode() == ISD::ADD) {
1762 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001763 if (isIntS16Immediate(N.getOperand(1), imm) &&
1764 (!Aligned || (imm & 3) == 0)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001765 Disp = DAG.getTargetConstant(imm, dl, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001766 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1767 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001768 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001769 } else {
1770 Base = N.getOperand(0);
1771 }
1772 return true; // [r+i]
1773 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1774 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001775 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001776 && "Cannot handle constant offsets yet!");
1777 Disp = N.getOperand(1).getOperand(0); // The global address.
1778 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001779 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001780 Disp.getOpcode() == ISD::TargetConstantPool ||
1781 Disp.getOpcode() == ISD::TargetJumpTable);
1782 Base = N.getOperand(0);
1783 return true; // [&g+r]
1784 }
1785 } else if (N.getOpcode() == ISD::OR) {
1786 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001787 if (isIntS16Immediate(N.getOperand(1), imm) &&
1788 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001789 // If this is an or of disjoint bitfields, we can codegen this as an add
1790 // (for better address arithmetic) if the LHS and RHS of the OR are
1791 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001792 APInt LHSKnownZero, LHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001793 DAG.computeKnownBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001794
Dan Gohmanf19609a2008-02-27 01:23:58 +00001795 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001796 // If all of the bits are known zero on the LHS or RHS, the add won't
1797 // carry.
Ulrich Weigand55a96652014-07-20 22:26:40 +00001798 if (FrameIndexSDNode *FI =
1799 dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1800 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1801 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1802 } else {
1803 Base = N.getOperand(0);
1804 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001805 Disp = DAG.getTargetConstant(imm, dl, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001806 return true;
1807 }
1808 }
1809 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1810 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001811
Chris Lattnera801fced2006-11-08 02:15:41 +00001812 // If this address fits entirely in a 16-bit sext immediate field, codegen
1813 // this as "d, 0"
1814 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001815 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001816 Disp = DAG.getTargetConstant(Imm, dl, CN->getValueType(0));
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001817 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001818 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001819 return true;
1820 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001821
1822 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001823 if ((CN->getValueType(0) == MVT::i32 ||
1824 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1825 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001826 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001827
Chris Lattnera801fced2006-11-08 02:15:41 +00001828 // Otherwise, break this down into an LIS + disp.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001829 Disp = DAG.getTargetConstant((short)Addr, dl, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001830
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001831 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, dl,
1832 MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00001833 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001834 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001835 return true;
1836 }
1837 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001838
Mehdi Amini44ede332015-07-09 02:09:04 +00001839 Disp = DAG.getTargetConstant(0, dl, getPointerTy(DAG.getDataLayout()));
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001840 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001841 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001842 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1843 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001844 Base = N;
1845 return true; // [r+0]
1846}
1847
1848/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1849/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001850bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1851 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001852 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001853 // Check to see if we can easily represent this as an [r+r] address. This
1854 // will fail if it thinks that the address is more profitably represented as
1855 // reg+imm, e.g. where imm = 0.
1856 if (SelectAddressRegReg(N, Base, Index, DAG))
1857 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001858
Chris Lattnera801fced2006-11-08 02:15:41 +00001859 // If the operand is an addition, always emit this as [r+r], since this is
1860 // better (for code size, and execution, as the memop does the add for free)
1861 // than emitting an explicit add.
1862 if (N.getOpcode() == ISD::ADD) {
1863 Base = N.getOperand(0);
1864 Index = N.getOperand(1);
1865 return true;
1866 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001867
Chris Lattnera801fced2006-11-08 02:15:41 +00001868 // Otherwise, do it the hard way, using R0 as the base register.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001869 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001870 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001871 Index = N;
1872 return true;
1873}
1874
Chris Lattnera801fced2006-11-08 02:15:41 +00001875/// getPreIndexedAddressParts - returns true by value, base pointer and
1876/// offset pointer and addressing mode by reference if the node's address
1877/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001878bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1879 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001880 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001881 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001882 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001883
Ulrich Weigande90b0222013-03-22 14:58:48 +00001884 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001885 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001886 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001887 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001888 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1889 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001890 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001891 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001892 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001893 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001894 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001895 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001896 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001897 } else
1898 return false;
1899
Hal Finkelc93a9a22015-02-25 01:06:45 +00001900 // PowerPC doesn't have preinc load/store instructions for vectors (except
1901 // for QPX, which does have preinc r+r forms).
1902 if (VT.isVector()) {
1903 if (!Subtarget.hasQPX() || (VT != MVT::v4f64 && VT != MVT::v4f32)) {
1904 return false;
1905 } else if (SelectAddressRegRegOnly(Ptr, Offset, Base, DAG)) {
1906 AM = ISD::PRE_INC;
1907 return true;
1908 }
1909 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001910
Ulrich Weigande90b0222013-03-22 14:58:48 +00001911 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1912
1913 // Common code will reject creating a pre-inc form if the base pointer
1914 // is a frame index, or if N is a store and the base pointer is either
1915 // the same as or a predecessor of the value being stored. Check for
1916 // those situations here, and try with swapped Base/Offset instead.
1917 bool Swap = false;
1918
1919 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1920 Swap = true;
1921 else if (!isLoad) {
1922 SDValue Val = cast<StoreSDNode>(N)->getValue();
1923 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1924 Swap = true;
1925 }
1926
1927 if (Swap)
1928 std::swap(Base, Offset);
1929
Hal Finkelca542be2012-06-20 15:43:03 +00001930 AM = ISD::PRE_INC;
1931 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001932 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001933
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001934 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001935 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001936 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001937 return false;
1938 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001939 // LDU/STU need an address with at least 4-byte alignment.
1940 if (Alignment < 4)
1941 return false;
1942
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001943 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001944 return false;
1945 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001946
Chris Lattnerb314b152006-11-11 00:08:42 +00001947 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001948 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1949 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001950 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001951 LD->getExtensionType() == ISD::SEXTLOAD &&
1952 isa<ConstantSDNode>(Offset))
1953 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001954 }
1955
Chris Lattnerce645542006-11-10 02:08:47 +00001956 AM = ISD::PRE_INC;
1957 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001958}
1959
1960//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001961// LowerOperation implementation
1962//===----------------------------------------------------------------------===//
1963
Chris Lattneredb9d842010-11-15 02:46:57 +00001964/// GetLabelAccessInfo - Return true if we should reference labels using a
1965/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
Eric Christophercccae792015-01-30 22:02:31 +00001966static bool GetLabelAccessInfo(const TargetMachine &TM,
1967 const PPCSubtarget &Subtarget,
1968 unsigned &HiOpFlags, unsigned &LoOpFlags,
Craig Topper062a2ba2014-04-25 05:30:21 +00001969 const GlobalValue *GV = nullptr) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001970 HiOpFlags = PPCII::MO_HA;
1971 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001972
Hal Finkel3ee2af72014-07-18 23:29:49 +00001973 // Don't use the pic base if not in PIC relocation model.
1974 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
1975
Chris Lattnerdd6df842010-11-15 03:13:19 +00001976 if (isPIC) {
1977 HiOpFlags |= PPCII::MO_PIC_FLAG;
1978 LoOpFlags |= PPCII::MO_PIC_FLAG;
1979 }
1980
1981 // If this is a reference to a global value that requires a non-lazy-ptr, make
1982 // sure that instruction lowering adds it.
Eric Christophere8dbfe12015-02-13 22:23:04 +00001983 if (GV && Subtarget.hasLazyResolverStub(GV)) {
Chris Lattnerdd6df842010-11-15 03:13:19 +00001984 HiOpFlags |= PPCII::MO_NLP_FLAG;
1985 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001986
Chris Lattnerdd6df842010-11-15 03:13:19 +00001987 if (GV->hasHiddenVisibility()) {
1988 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1989 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1990 }
1991 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001992
Chris Lattneredb9d842010-11-15 02:46:57 +00001993 return isPIC;
1994}
1995
1996static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1997 SelectionDAG &DAG) {
Daniel Jasper48e93f72015-04-28 13:38:35 +00001998 SDLoc DL(HiPart);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001999 EVT PtrVT = HiPart.getValueType();
2000 SDValue Zero = DAG.getConstant(0, DL, PtrVT);
Chris Lattneredb9d842010-11-15 02:46:57 +00002001
2002 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
2003 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00002004
Chris Lattneredb9d842010-11-15 02:46:57 +00002005 // With PIC, the first instruction is actually "GR+hi(&G)".
2006 if (isPIC)
2007 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
2008 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00002009
Chris Lattneredb9d842010-11-15 02:46:57 +00002010 // Generate non-pic code that has direct accesses to the constant pool.
2011 // The address of the global is just (hi(&g)+lo(&g)).
2012 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
2013}
2014
Hal Finkele6698d52015-02-01 15:03:28 +00002015static void setUsesTOCBasePtr(MachineFunction &MF) {
2016 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2017 FuncInfo->setUsesTOCBasePtr();
2018}
2019
2020static void setUsesTOCBasePtr(SelectionDAG &DAG) {
2021 setUsesTOCBasePtr(DAG.getMachineFunction());
2022}
2023
Hal Finkelcf599212015-02-25 21:36:59 +00002024static SDValue getTOCEntry(SelectionDAG &DAG, SDLoc dl, bool Is64Bit,
2025 SDValue GA) {
2026 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
2027 SDValue Reg = Is64Bit ? DAG.getRegister(PPC::X2, VT) :
2028 DAG.getNode(PPCISD::GlobalBaseReg, dl, VT);
2029
2030 SDValue Ops[] = { GA, Reg };
Alex Lorenze40c8a22015-08-11 23:09:45 +00002031 return DAG.getMemIntrinsicNode(
2032 PPCISD::TOC_ENTRY, dl, DAG.getVTList(VT, MVT::Other), Ops, VT,
2033 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 0, false, true,
2034 false, 0);
Hal Finkelcf599212015-02-25 21:36:59 +00002035}
2036
Scott Michelcf0da6c2009-02-17 22:15:04 +00002037SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002038 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002039 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00002040 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002041 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00002042
Roman Divackyace47072012-08-24 16:26:02 +00002043 // 64-bit SVR4 ABI code is always position-independent.
2044 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002045 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002046 setUsesTOCBasePtr(DAG);
Roman Divackyace47072012-08-24 16:26:02 +00002047 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Hal Finkelcf599212015-02-25 21:36:59 +00002048 return getTOCEntry(DAG, SDLoc(CP), true, GA);
Roman Divackyace47072012-08-24 16:26:02 +00002049 }
2050
Chris Lattneredb9d842010-11-15 02:46:57 +00002051 unsigned MOHiFlag, MOLoFlag;
Eric Christophercccae792015-01-30 22:02:31 +00002052 bool isPIC =
2053 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002054
2055 if (isPIC && Subtarget.isSVR4ABI()) {
2056 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(),
2057 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00002058 return getTOCEntry(DAG, SDLoc(CP), false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002059 }
2060
Chris Lattneredb9d842010-11-15 02:46:57 +00002061 SDValue CPIHi =
2062 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
2063 SDValue CPILo =
2064 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
2065 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00002066}
2067
Dan Gohman21cea8a2010-04-17 15:26:15 +00002068SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002069 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002070 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00002071
Roman Divackyace47072012-08-24 16:26:02 +00002072 // 64-bit SVR4 ABI code is always position-independent.
2073 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002074 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002075 setUsesTOCBasePtr(DAG);
Roman Divackyace47072012-08-24 16:26:02 +00002076 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Hal Finkelcf599212015-02-25 21:36:59 +00002077 return getTOCEntry(DAG, SDLoc(JT), true, GA);
Roman Divackyace47072012-08-24 16:26:02 +00002078 }
2079
Chris Lattneredb9d842010-11-15 02:46:57 +00002080 unsigned MOHiFlag, MOLoFlag;
Eric Christophercccae792015-01-30 22:02:31 +00002081 bool isPIC =
2082 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002083
2084 if (isPIC && Subtarget.isSVR4ABI()) {
2085 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
2086 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00002087 return getTOCEntry(DAG, SDLoc(GA), false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002088 }
2089
Chris Lattneredb9d842010-11-15 02:46:57 +00002090 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
2091 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
2092 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00002093}
2094
Dan Gohman21cea8a2010-04-17 15:26:15 +00002095SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
2096 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00002097 EVT PtrVT = Op.getValueType();
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002098 BlockAddressSDNode *BASDN = cast<BlockAddressSDNode>(Op);
2099 const BlockAddress *BA = BASDN->getBlockAddress();
Bob Wilsonf84f7102009-11-04 21:31:18 +00002100
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002101 // 64-bit SVR4 ABI code is always position-independent.
2102 // The actual BlockAddress is stored in the TOC.
2103 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002104 setUsesTOCBasePtr(DAG);
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002105 SDValue GA = DAG.getTargetBlockAddress(BA, PtrVT, BASDN->getOffset());
Hal Finkelcf599212015-02-25 21:36:59 +00002106 return getTOCEntry(DAG, SDLoc(BASDN), true, GA);
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002107 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002108
Chris Lattneredb9d842010-11-15 02:46:57 +00002109 unsigned MOHiFlag, MOLoFlag;
Eric Christophercccae792015-01-30 22:02:31 +00002110 bool isPIC =
2111 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00002112 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
2113 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00002114 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
2115}
2116
Roman Divackye3f15c982012-06-04 17:36:38 +00002117SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
2118 SelectionDAG &DAG) const {
2119
Bill Schmidtbdae03f2013-09-17 20:22:05 +00002120 // FIXME: TLS addresses currently use medium model code sequences,
2121 // which is the most useful form. Eventually support for small and
2122 // large models could be added if users need it, at the cost of
2123 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00002124 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002125 if (DAG.getTarget().Options.EmulatedTLS)
2126 return LowerToTLSEmulatedModel(GA, DAG);
2127
Andrew Trickef9de2a2013-05-25 02:42:55 +00002128 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00002129 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002130 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002131 bool is64bit = Subtarget.isPPC64();
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002132 const Module *M = DAG.getMachineFunction().getFunction()->getParent();
2133 PICLevel::Level picLevel = M->getPICLevel();
Roman Divackye3f15c982012-06-04 17:36:38 +00002134
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002135 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00002136
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002137 if (Model == TLSModel::LocalExec) {
2138 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002139 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002140 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002141 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002142 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
2143 is64bit ? MVT::i64 : MVT::i32);
2144 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
2145 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
2146 }
Roman Divackye3f15c982012-06-04 17:36:38 +00002147
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002148 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00002149 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00002150 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2151 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00002152 SDValue GOTPtr;
2153 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00002154 setUsesTOCBasePtr(DAG);
Roman Divacky32143e22013-12-20 18:08:54 +00002155 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
2156 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
2157 PtrVT, GOTReg, TGA);
2158 } else
2159 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00002160 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00002161 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00002162 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002163 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00002164
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002165 if (Model == TLSModel::GeneralDynamic) {
Bill Schmidt82f1c772015-02-10 19:09:05 +00002166 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002167 SDValue GOTPtr;
2168 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00002169 setUsesTOCBasePtr(DAG);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002170 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
2171 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
2172 GOTReg, TGA);
2173 } else {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002174 if (picLevel == PICLevel::Small)
2175 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
2176 else
2177 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002178 }
Bill Schmidt82f1c772015-02-10 19:09:05 +00002179 return DAG.getNode(PPCISD::ADDI_TLSGD_L_ADDR, dl, PtrVT,
2180 GOTPtr, TGA, TGA);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00002181 }
2182
Bill Schmidt24b8dd62012-12-12 19:29:35 +00002183 if (Model == TLSModel::LocalDynamic) {
Bill Schmidt82f1c772015-02-10 19:09:05 +00002184 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002185 SDValue GOTPtr;
2186 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00002187 setUsesTOCBasePtr(DAG);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002188 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
2189 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
2190 GOTReg, TGA);
2191 } else {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002192 if (picLevel == PICLevel::Small)
2193 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
2194 else
2195 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002196 }
Bill Schmidt82f1c772015-02-10 19:09:05 +00002197 SDValue TLSAddr = DAG.getNode(PPCISD::ADDI_TLSLD_L_ADDR, dl,
2198 PtrVT, GOTPtr, TGA, TGA);
2199 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl,
2200 PtrVT, TLSAddr, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00002201 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
2202 }
2203
2204 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00002205}
2206
Chris Lattneredb9d842010-11-15 02:46:57 +00002207SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
2208 SelectionDAG &DAG) const {
2209 EVT PtrVT = Op.getValueType();
2210 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002211 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00002212 const GlobalValue *GV = GSDN->getGlobal();
2213
Chris Lattneredb9d842010-11-15 02:46:57 +00002214 // 64-bit SVR4 ABI code is always position-independent.
2215 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002216 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002217 setUsesTOCBasePtr(DAG);
Chris Lattneredb9d842010-11-15 02:46:57 +00002218 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
Hal Finkelcf599212015-02-25 21:36:59 +00002219 return getTOCEntry(DAG, DL, true, GA);
Chris Lattneredb9d842010-11-15 02:46:57 +00002220 }
2221
Chris Lattnerdd6df842010-11-15 03:13:19 +00002222 unsigned MOHiFlag, MOLoFlag;
Eric Christophercccae792015-01-30 22:02:31 +00002223 bool isPIC =
2224 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00002225
Hal Finkel3ee2af72014-07-18 23:29:49 +00002226 if (isPIC && Subtarget.isSVR4ABI()) {
2227 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT,
2228 GSDN->getOffset(),
2229 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00002230 return getTOCEntry(DAG, DL, false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002231 }
2232
Chris Lattnerdd6df842010-11-15 03:13:19 +00002233 SDValue GAHi =
2234 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
2235 SDValue GALo =
2236 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00002237
Chris Lattnerdd6df842010-11-15 03:13:19 +00002238 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00002239
Chris Lattnerdd6df842010-11-15 03:13:19 +00002240 // If the global reference is actually to a non-lazy-pointer, we have to do an
2241 // extra load to get the address of the global.
2242 if (MOHiFlag & PPCII::MO_NLP_FLAG)
2243 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002244 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00002245 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00002246}
2247
Dan Gohman21cea8a2010-04-17 15:26:15 +00002248SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00002249 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002250 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002251
Hal Finkel777c9dd2014-03-29 16:04:40 +00002252 if (Op.getValueType() == MVT::v2i64) {
2253 // When the operands themselves are v2i64 values, we need to do something
2254 // special because VSX has no underlying comparison operations for these.
2255 if (Op.getOperand(0).getValueType() == MVT::v2i64) {
2256 // Equality can be handled by casting to the legal type for Altivec
2257 // comparisons, everything else needs to be expanded.
2258 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
2259 return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
2260 DAG.getSetCC(dl, MVT::v4i32,
2261 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)),
2262 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)),
2263 CC));
2264 }
2265
2266 return SDValue();
2267 }
2268
2269 // We handle most of these in the usual way.
2270 return Op;
2271 }
2272
Chris Lattner4211ca92006-04-14 06:01:58 +00002273 // If we're comparing for equality to zero, expose the fact that this is
2274 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
2275 // fold the new nodes.
2276 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2277 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002278 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002279 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00002280 if (VT.bitsLT(MVT::i32)) {
2281 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002282 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002283 }
Duncan Sands13237ac2008-06-06 12:08:01 +00002284 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002285 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
2286 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002287 DAG.getConstant(Log2b, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00002288 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00002289 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002290 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00002291 // optimized. FIXME: revisit this when we can custom lower all setcc
2292 // optimizations.
2293 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002294 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00002295 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002296
Chris Lattner4211ca92006-04-14 06:01:58 +00002297 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00002298 // by xor'ing the rhs with the lhs, which is faster than setting a
2299 // condition register, reading it back out, and masking the correct bit. The
2300 // normal approach here uses sub to do this instead of xor. Using xor exposes
2301 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002302 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002303 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002304 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002305 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00002306 Op.getOperand(1));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002307 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, dl, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00002308 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002309 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00002310}
2311
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002312SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002313 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00002314 SDNode *Node = Op.getNode();
2315 EVT VT = Node->getValueType(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00002316 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Roman Divacky4394e682011-06-28 15:30:42 +00002317 SDValue InChain = Node->getOperand(0);
2318 SDValue VAListPtr = Node->getOperand(1);
2319 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002320 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002321
Roman Divacky4394e682011-06-28 15:30:42 +00002322 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
2323
2324 // gpr_index
2325 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
2326 VAListPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00002327 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002328 InChain = GprIndex.getValue(1);
2329
2330 if (VT == MVT::i64) {
2331 // Check if GprIndex is even
2332 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002333 DAG.getConstant(1, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002334 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002335 DAG.getConstant(0, dl, MVT::i32), ISD::SETNE);
Roman Divacky4394e682011-06-28 15:30:42 +00002336 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002337 DAG.getConstant(1, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002338 // Align GprIndex to be even if it isn't
2339 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
2340 GprIndex);
2341 }
2342
2343 // fpr index is 1 byte after gpr
2344 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002345 DAG.getConstant(1, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002346
2347 // fpr
2348 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
2349 FprPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00002350 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002351 InChain = FprIndex.getValue(1);
2352
2353 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002354 DAG.getConstant(8, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002355
2356 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002357 DAG.getConstant(4, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002358
2359 // areas
2360 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002361 MachinePointerInfo(), false, false,
2362 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002363 InChain = OverflowArea.getValue(1);
2364
2365 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002366 MachinePointerInfo(), false, false,
2367 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002368 InChain = RegSaveArea.getValue(1);
2369
2370 // select overflow_area if index > 8
2371 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002372 DAG.getConstant(8, dl, MVT::i32), ISD::SETLT);
Roman Divacky4394e682011-06-28 15:30:42 +00002373
Roman Divacky4394e682011-06-28 15:30:42 +00002374 // adjustment constant gpr_index * 4/8
2375 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
2376 VT.isInteger() ? GprIndex : FprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002377 DAG.getConstant(VT.isInteger() ? 4 : 8, dl,
Roman Divacky4394e682011-06-28 15:30:42 +00002378 MVT::i32));
2379
2380 // OurReg = RegSaveArea + RegConstant
2381 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
2382 RegConstant);
2383
2384 // Floating types are 32 bytes into RegSaveArea
2385 if (VT.isFloatingPoint())
2386 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002387 DAG.getConstant(32, dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002388
2389 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
2390 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
2391 VT.isInteger() ? GprIndex : FprIndex,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002392 DAG.getConstant(VT == MVT::i64 ? 2 : 1, dl,
Roman Divacky4394e682011-06-28 15:30:42 +00002393 MVT::i32));
2394
2395 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
2396 VT.isInteger() ? VAListPtr : FprPtr,
2397 MachinePointerInfo(SV),
2398 MVT::i8, false, false, 0);
2399
2400 // determine if we should load from reg_save_area or overflow_area
2401 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
2402
2403 // increase overflow_area by 4/8 if gpr/fpr > 8
2404 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
2405 DAG.getConstant(VT.isInteger() ? 4 : 8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002406 dl, MVT::i32));
Roman Divacky4394e682011-06-28 15:30:42 +00002407
2408 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
2409 OverflowAreaPlusN);
2410
2411 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
2412 OverflowAreaPtr,
2413 MachinePointerInfo(),
2414 MVT::i32, false, false, 0);
2415
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00002416 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002417 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002418}
2419
Roman Divackyc3825df2013-07-25 21:36:47 +00002420SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
2421 const PPCSubtarget &Subtarget) const {
2422 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
2423
2424 // We have to copy the entire va_list struct:
2425 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
2426 return DAG.getMemcpy(Op.getOperand(0), Op,
2427 Op.getOperand(1), Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002428 DAG.getConstant(12, SDLoc(Op), MVT::i32), 8, false, true,
2429 false, MachinePointerInfo(), MachinePointerInfo());
Roman Divackyc3825df2013-07-25 21:36:47 +00002430}
2431
Duncan Sandsa0984362011-09-06 13:37:06 +00002432SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
2433 SelectionDAG &DAG) const {
2434 return Op.getOperand(0);
2435}
2436
2437SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
2438 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00002439 SDValue Chain = Op.getOperand(0);
2440 SDValue Trmp = Op.getOperand(1); // trampoline
2441 SDValue FPtr = Op.getOperand(2); // nested function
2442 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00002443 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00002444
Mehdi Amini44ede332015-07-09 02:09:04 +00002445 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Owen Anderson9f944592009-08-11 20:47:22 +00002446 bool isPPC64 = (PtrVT == MVT::i64);
Mehdi Aminia749f2a2015-07-09 02:09:52 +00002447 Type *IntPtrTy = DAG.getDataLayout().getIntPtrType(*DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00002448
Scott Michelcf0da6c2009-02-17 22:15:04 +00002449 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00002450 TargetLowering::ArgListEntry Entry;
2451
2452 Entry.Ty = IntPtrTy;
2453 Entry.Node = Trmp; Args.push_back(Entry);
2454
2455 // TrampSize == (isPPC64 ? 48 : 40);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002456 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002457 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00002458 Args.push_back(Entry);
2459
2460 Entry.Node = FPtr; Args.push_back(Entry);
2461 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002462
Bill Wendling95e1af22008-09-17 00:30:57 +00002463 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002464 TargetLowering::CallLoweringInfo CLI(DAG);
2465 CLI.setDebugLoc(dl).setChain(Chain)
2466 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002467 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
2468 std::move(Args), 0);
Bill Wendling95e1af22008-09-17 00:30:57 +00002469
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002470 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Duncan Sandsa0984362011-09-06 13:37:06 +00002471 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00002472}
2473
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002474SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002475 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00002476 MachineFunction &MF = DAG.getMachineFunction();
2477 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2478
Andrew Trickef9de2a2013-05-25 02:42:55 +00002479 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002480
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002481 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002482 // vastart just stores the address of the VarArgsFrameIndex slot into the
2483 // memory location argument.
Mehdi Amini44ede332015-07-09 02:09:04 +00002484 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002485 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002486 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00002487 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2488 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00002489 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002490 }
2491
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002492 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002493 // We suppose the given va_list is already allocated.
2494 //
2495 // typedef struct {
2496 // char gpr; /* index into the array of 8 GPRs
2497 // * stored in the register save area
2498 // * gpr=0 corresponds to r3,
2499 // * gpr=1 to r4, etc.
2500 // */
2501 // char fpr; /* index into the array of 8 FPRs
2502 // * stored in the register save area
2503 // * fpr=0 corresponds to f1,
2504 // * fpr=1 to f2, etc.
2505 // */
2506 // char *overflow_arg_area;
2507 // /* location on stack that holds
2508 // * the next overflow argument
2509 // */
2510 // char *reg_save_area;
2511 // /* where r3:r10 and f1:f8 (if saved)
2512 // * are stored
2513 // */
2514 // } va_list[1];
2515
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002516 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), dl, MVT::i32);
2517 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), dl, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002518
Mehdi Amini44ede332015-07-09 02:09:04 +00002519 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Scott Michelcf0da6c2009-02-17 22:15:04 +00002520
Dan Gohman31ae5862010-04-17 14:41:14 +00002521 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
2522 PtrVT);
2523 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2524 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002525
Duncan Sands13237ac2008-06-06 12:08:01 +00002526 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002527 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, dl, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002528
Duncan Sands13237ac2008-06-06 12:08:01 +00002529 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002530 SDValue ConstStackOffset = DAG.getConstant(StackOffset, dl, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002531
2532 uint64_t FPROffset = 1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002533 SDValue ConstFPROffset = DAG.getConstant(FPROffset, dl, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002534
Dan Gohman2d489b52008-02-06 22:27:42 +00002535 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002536
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002537 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002538 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00002539 Op.getOperand(1),
2540 MachinePointerInfo(SV),
2541 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002542 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002543 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002544 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002545
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002546 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002547 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00002548 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
2549 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00002550 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002551 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002552 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002553
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002554 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002555 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00002556 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
2557 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002558 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002559 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002560 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002561
2562 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00002563 return DAG.getStore(thirdStore, dl, FR, nextPtr,
2564 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002565 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002566
Chris Lattner4211ca92006-04-14 06:01:58 +00002567}
2568
Chris Lattner4f2e4e02007-03-06 00:59:59 +00002569#include "PPCGenCallingConv.inc"
2570
NAKAMURA Takumi84965032015-09-22 11:14:12 +00002571// Function whose sole purpose is to kill compiler warnings
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002572// stemming from unused functions included from PPCGenCallingConv.inc.
2573CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00002574 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002575}
2576
Bill Schmidt230b4512013-06-12 16:39:22 +00002577bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
2578 CCValAssign::LocInfo &LocInfo,
2579 ISD::ArgFlagsTy &ArgFlags,
2580 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002581 return true;
2582}
2583
Bill Schmidt230b4512013-06-12 16:39:22 +00002584bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
2585 MVT &LocVT,
2586 CCValAssign::LocInfo &LocInfo,
2587 ISD::ArgFlagsTy &ArgFlags,
2588 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002589 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002590 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2591 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2592 };
2593 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002594
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002595 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002596
2597 // Skip one register if the first unallocated register has an even register
2598 // number and there are still argument registers available which have not been
2599 // allocated yet. RegNum is actually an index into ArgRegs, which means we
2600 // need to skip a register if RegNum is odd.
2601 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
2602 State.AllocateReg(ArgRegs[RegNum]);
2603 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002604
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002605 // Always return false here, as this function only makes sure that the first
2606 // unallocated register has an odd register number and does not actually
2607 // allocate a register for the current argument.
2608 return false;
2609}
2610
Bill Schmidt230b4512013-06-12 16:39:22 +00002611bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
2612 MVT &LocVT,
2613 CCValAssign::LocInfo &LocInfo,
2614 ISD::ArgFlagsTy &ArgFlags,
2615 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002616 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002617 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2618 PPC::F8
2619 };
2620
2621 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002622
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002623 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002624
2625 // If there is only one Floating-point register left we need to put both f64
2626 // values of a split ppc_fp128 value on the stack.
2627 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
2628 State.AllocateReg(ArgRegs[RegNum]);
2629 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002630
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002631 // Always return false here, as this function only makes sure that the two f64
2632 // values a ppc_fp128 value is split into are both passed in registers or both
2633 // passed on the stack and does not actually allocate a register for the
2634 // current argument.
2635 return false;
2636}
2637
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002638/// FPR - The set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002639/// on Darwin.
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002640static const MCPhysReg FPR[] = {PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5,
2641 PPC::F6, PPC::F7, PPC::F8, PPC::F9, PPC::F10,
2642 PPC::F11, PPC::F12, PPC::F13};
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002643
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002644/// QFPR - The set of QPX registers that should be allocated for arguments.
2645static const MCPhysReg QFPR[] = {
2646 PPC::QF1, PPC::QF2, PPC::QF3, PPC::QF4, PPC::QF5, PPC::QF6, PPC::QF7,
2647 PPC::QF8, PPC::QF9, PPC::QF10, PPC::QF11, PPC::QF12, PPC::QF13};
Hal Finkelc93a9a22015-02-25 01:06:45 +00002648
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002649/// CalculateStackSlotSize - Calculates the size reserved for this argument on
2650/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002651static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002652 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00002653 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002654 if (Flags.isByVal())
2655 ArgSize = Flags.getByValSize();
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002656
2657 // Round up to multiples of the pointer size, except for array members,
2658 // which are always packed.
2659 if (!Flags.isInConsecutiveRegs())
2660 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002661
2662 return ArgSize;
2663}
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002664
2665/// CalculateStackSlotAlignment - Calculates the alignment of this argument
2666/// on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002667static unsigned CalculateStackSlotAlignment(EVT ArgVT, EVT OrigVT,
2668 ISD::ArgFlagsTy Flags,
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002669 unsigned PtrByteSize) {
2670 unsigned Align = PtrByteSize;
2671
2672 // Altivec parameters are padded to a 16 byte boundary.
2673 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2674 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
Kit Bartond4eb73c2015-05-05 16:10:44 +00002675 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 ||
2676 ArgVT == MVT::v1i128)
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002677 Align = 16;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002678 // QPX vector types stored in double-precision are padded to a 32 byte
2679 // boundary.
2680 else if (ArgVT == MVT::v4f64 || ArgVT == MVT::v4i1)
2681 Align = 32;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002682
2683 // ByVal parameters are aligned as requested.
2684 if (Flags.isByVal()) {
2685 unsigned BVAlign = Flags.getByValAlign();
2686 if (BVAlign > PtrByteSize) {
2687 if (BVAlign % PtrByteSize != 0)
2688 llvm_unreachable(
2689 "ByVal alignment is not a multiple of the pointer size");
2690
2691 Align = BVAlign;
2692 }
2693 }
2694
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002695 // Array members are always packed to their original alignment.
2696 if (Flags.isInConsecutiveRegs()) {
2697 // If the array member was split into multiple registers, the first
2698 // needs to be aligned to the size of the full type. (Except for
2699 // ppcf128, which is only aligned as its f64 components.)
2700 if (Flags.isSplit() && OrigVT != MVT::ppcf128)
2701 Align = OrigVT.getStoreSize();
2702 else
2703 Align = ArgVT.getStoreSize();
2704 }
2705
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002706 return Align;
2707}
2708
Ulrich Weigand8658f172014-07-20 23:43:15 +00002709/// CalculateStackSlotUsed - Return whether this argument will use its
2710/// stack slot (instead of being passed in registers). ArgOffset,
2711/// AvailableFPRs, and AvailableVRs must hold the current argument
2712/// position, and will be updated to account for this argument.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002713static bool CalculateStackSlotUsed(EVT ArgVT, EVT OrigVT,
2714 ISD::ArgFlagsTy Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00002715 unsigned PtrByteSize,
2716 unsigned LinkageSize,
2717 unsigned ParamAreaSize,
2718 unsigned &ArgOffset,
2719 unsigned &AvailableFPRs,
Hal Finkelc93a9a22015-02-25 01:06:45 +00002720 unsigned &AvailableVRs, bool HasQPX) {
Ulrich Weigand8658f172014-07-20 23:43:15 +00002721 bool UseMemory = false;
2722
2723 // Respect alignment of argument on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002724 unsigned Align =
2725 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigand8658f172014-07-20 23:43:15 +00002726 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
2727 // If there's no space left in the argument save area, we must
2728 // use memory (this check also catches zero-sized arguments).
2729 if (ArgOffset >= LinkageSize + ParamAreaSize)
2730 UseMemory = true;
2731
2732 // Allocate argument on the stack.
2733 ArgOffset += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002734 if (Flags.isInConsecutiveRegsLast())
2735 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand8658f172014-07-20 23:43:15 +00002736 // If we overran the argument save area, we must use memory
2737 // (this check catches arguments passed partially in memory)
2738 if (ArgOffset > LinkageSize + ParamAreaSize)
2739 UseMemory = true;
2740
2741 // However, if the argument is actually passed in an FPR or a VR,
2742 // we don't use memory after all.
2743 if (!Flags.isByVal()) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00002744 if (ArgVT == MVT::f32 || ArgVT == MVT::f64 ||
2745 // QPX registers overlap with the scalar FP registers.
2746 (HasQPX && (ArgVT == MVT::v4f32 ||
2747 ArgVT == MVT::v4f64 ||
2748 ArgVT == MVT::v4i1)))
Ulrich Weigand8658f172014-07-20 23:43:15 +00002749 if (AvailableFPRs > 0) {
2750 --AvailableFPRs;
2751 return false;
2752 }
2753 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2754 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
Kit Bartond4eb73c2015-05-05 16:10:44 +00002755 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 ||
2756 ArgVT == MVT::v1i128)
Ulrich Weigand8658f172014-07-20 23:43:15 +00002757 if (AvailableVRs > 0) {
2758 --AvailableVRs;
2759 return false;
2760 }
2761 }
2762
2763 return UseMemory;
2764}
2765
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002766/// EnsureStackAlignment - Round stack frame size up from NumBytes to
2767/// ensure minimum alignment required for target.
Eric Christophercccae792015-01-30 22:02:31 +00002768static unsigned EnsureStackAlignment(const PPCFrameLowering *Lowering,
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002769 unsigned NumBytes) {
Eric Christophercccae792015-01-30 22:02:31 +00002770 unsigned TargetAlign = Lowering->getStackAlignment();
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002771 unsigned AlignMask = TargetAlign - 1;
2772 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2773 return NumBytes;
2774}
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002775
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002776SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002777PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002778 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002779 const SmallVectorImpl<ISD::InputArg>
2780 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002781 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002782 SmallVectorImpl<SDValue> &InVals)
2783 const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002784 if (Subtarget.isSVR4ABI()) {
2785 if (Subtarget.isPPC64())
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002786 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
2787 dl, DAG, InVals);
2788 else
2789 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2790 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002791 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002792 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2793 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002794 }
2795}
2796
2797SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002798PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002799 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002800 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002801 const SmallVectorImpl<ISD::InputArg>
2802 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002803 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002804 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002805
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002806 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002807 // +-----------------------------------+
2808 // +--> | Back chain |
2809 // | +-----------------------------------+
2810 // | | Floating-point register save area |
2811 // | +-----------------------------------+
2812 // | | General register save area |
2813 // | +-----------------------------------+
2814 // | | CR save word |
2815 // | +-----------------------------------+
2816 // | | VRSAVE save word |
2817 // | +-----------------------------------+
2818 // | | Alignment padding |
2819 // | +-----------------------------------+
2820 // | | Vector register save area |
2821 // | +-----------------------------------+
2822 // | | Local variable space |
2823 // | +-----------------------------------+
2824 // | | Parameter list area |
2825 // | +-----------------------------------+
2826 // | | LR save word |
2827 // | +-----------------------------------+
2828 // SP--> +--- | Back chain |
2829 // +-----------------------------------+
2830 //
2831 // Specifications:
2832 // System V Application Binary Interface PowerPC Processor Supplement
2833 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002834
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002835 MachineFunction &MF = DAG.getMachineFunction();
2836 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002837 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002838
Mehdi Amini44ede332015-07-09 02:09:04 +00002839 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002840 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002841 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2842 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002843 unsigned PtrByteSize = 4;
2844
2845 // Assign locations to all of the incoming arguments.
2846 SmallVector<CCValAssign, 16> ArgLocs;
Strahinja Petrovice682b802016-05-09 12:27:39 +00002847 PPCCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
Eric Christopherb5217502014-08-06 18:45:26 +00002848 *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002849
2850 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00002851 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002852 CCInfo.AllocateStack(LinkageSize, PtrByteSize);
Strahinja Petrovice682b802016-05-09 12:27:39 +00002853 if (Subtarget.useSoftFloat())
2854 CCInfo.PreAnalyzeFormalArguments(Ins);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002855
Bill Schmidtef17c142013-02-06 17:33:58 +00002856 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Strahinja Petrovice682b802016-05-09 12:27:39 +00002857 CCInfo.clearWasPPCF128();
Wesley Peck527da1b2010-11-23 03:31:01 +00002858
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002859 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2860 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002861
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002862 // Arguments stored in registers.
2863 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002864 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002865 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002866
Owen Anderson9f944592009-08-11 20:47:22 +00002867 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002868 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002869 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002870 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002871 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002872 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002873 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002874 case MVT::f32:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00002875 if (Subtarget.hasP8Vector())
2876 RC = &PPC::VSSRCRegClass;
2877 else
2878 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002879 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002880 case MVT::f64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002881 if (Subtarget.hasVSX())
Hal Finkel19be5062014-03-29 05:29:01 +00002882 RC = &PPC::VSFRCRegClass;
2883 else
2884 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002885 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002886 case MVT::v16i8:
2887 case MVT::v8i16:
2888 case MVT::v4i32:
Hal Finkel7811c612014-03-28 19:58:11 +00002889 RC = &PPC::VRRCRegClass;
2890 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002891 case MVT::v4f32:
2892 RC = Subtarget.hasQPX() ? &PPC::QSRCRegClass : &PPC::VRRCRegClass;
2893 break;
Hal Finkel27774d92014-03-13 07:58:58 +00002894 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002895 case MVT::v2i64:
Hal Finkel7811c612014-03-28 19:58:11 +00002896 RC = &PPC::VSHRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002897 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002898 case MVT::v4f64:
2899 RC = &PPC::QFRCRegClass;
2900 break;
2901 case MVT::v4i1:
2902 RC = &PPC::QBRCRegClass;
2903 break;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002904 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002905
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002906 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002907 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002908 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2909 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2910
2911 if (ValVT == MVT::i1)
2912 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002913
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002914 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002915 } else {
2916 // Argument stored in memory.
2917 assert(VA.isMemLoc());
2918
Hal Finkel940ab932014-02-28 00:27:01 +00002919 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002920 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002921 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002922
2923 // Create load nodes to retrieve arguments from the stack.
2924 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002925 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2926 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002927 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002928 }
2929 }
2930
2931 // Assign locations to all of the incoming aggregate by value arguments.
2932 // Aggregates passed by value are stored in the local variable space of the
2933 // caller's stack frame, right above the parameter list area.
2934 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002935 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Eric Christopherb5217502014-08-06 18:45:26 +00002936 ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002937
2938 // Reserve stack space for the allocations in CCInfo.
2939 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2940
Bill Schmidtef17c142013-02-06 17:33:58 +00002941 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002942
2943 // Area that is at least reserved in the caller of this function.
2944 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002945 MinReservedArea = std::max(MinReservedArea, LinkageSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00002946
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002947 // Set the size that is at least reserved in caller of this function. Tail
2948 // call optimized function's reserved stack space needs to be aligned so that
2949 // taking the difference between two stack areas will result in an aligned
2950 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00002951 MinReservedArea =
2952 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002953 FuncInfo->setMinReservedArea(MinReservedArea);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002954
2955 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002956
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002957 // If the function takes variable number of arguments, make a frame index for
2958 // the start of the first vararg value... for expansion of llvm.va_start.
2959 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +00002960 static const MCPhysReg GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002961 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2962 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2963 };
2964 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2965
Craig Topper840beec2014-04-04 05:16:06 +00002966 static const MCPhysReg FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002967 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2968 PPC::F8
2969 };
Joerg Sonnenbergereb8655a2014-08-08 16:46:10 +00002970 unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
Petar Jovanovic280f7102015-12-14 17:57:33 +00002971
2972 if (Subtarget.useSoftFloat())
2973 NumFPArgRegs = 0;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002974
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002975 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs));
2976 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002977
2978 // Make room for NumGPArgRegs and NumFPArgRegs.
2979 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Craig Topper7ff15922014-09-10 04:51:36 +00002980 NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002981
Dan Gohman31ae5862010-04-17 14:41:14 +00002982 FuncInfo->setVarArgsStackOffset(
2983 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002984 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002985
Dan Gohman31ae5862010-04-17 14:41:14 +00002986 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2987 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002988
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002989 // The fixed integer arguments of a variadic function are stored to the
2990 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2991 // the result of va_next.
2992 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2993 // Get an existing live-in vreg, or add a new one.
2994 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2995 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002996 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002997
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002998 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002999 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3000 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003001 MemOps.push_back(Store);
3002 // Increment the address by four for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003003 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003004 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
3005 }
3006
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003007 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
3008 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003009 // The double arguments are stored to the VarArgsFrameIndex
3010 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00003011 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
3012 // Get an existing live-in vreg, or add a new one.
3013 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
3014 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00003015 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003016
Owen Anderson9f944592009-08-11 20:47:22 +00003017 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00003018 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3019 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003020 MemOps.push_back(Store);
3021 // Increment the address by eight for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003022 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8, dl,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003023 PtrVT);
3024 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
3025 }
3026 }
3027
3028 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003029 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003030
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003031 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003032}
3033
Bill Schmidt57d6de52012-10-23 15:51:16 +00003034// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
3035// value to MVT::i64 and then truncate to the correct register size.
3036SDValue
3037PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
3038 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003039 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003040 if (Flags.isSExt())
3041 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
3042 DAG.getValueType(ObjectVT));
3043 else if (Flags.isZExt())
3044 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
3045 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00003046
Hal Finkel940ab932014-02-28 00:27:01 +00003047 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003048}
3049
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003050SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003051PPCTargetLowering::LowerFormalArguments_64SVR4(
3052 SDValue Chain,
3053 CallingConv::ID CallConv, bool isVarArg,
3054 const SmallVectorImpl<ISD::InputArg>
3055 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003056 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003057 SmallVectorImpl<SDValue> &InVals) const {
3058 // TODO: add description of PPC stack frame format, or at least some docs.
3059 //
Ulrich Weigand8658f172014-07-20 23:43:15 +00003060 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00003061 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003062 MachineFunction &MF = DAG.getMachineFunction();
3063 MachineFrameInfo *MFI = MF.getFrameInfo();
3064 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
3065
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003066 assert(!(CallConv == CallingConv::Fast && isVarArg) &&
3067 "fastcc not supported on varargs functions");
3068
Mehdi Amini44ede332015-07-09 02:09:04 +00003069 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003070 // Potential tail calls could cause overwriting of argument stack slots.
3071 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
3072 (CallConv == CallingConv::Fast));
3073 unsigned PtrByteSize = 8;
Eric Christophera4ae2132015-02-13 22:22:57 +00003074 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003075
Craig Topper840beec2014-04-04 05:16:06 +00003076 static const MCPhysReg GPR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003077 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3078 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3079 };
Craig Topper840beec2014-04-04 05:16:06 +00003080 static const MCPhysReg VR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003081 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3082 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3083 };
Craig Topper840beec2014-04-04 05:16:06 +00003084 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00003085 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
3086 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
3087 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003088
3089 const unsigned Num_GPR_Regs = array_lengthof(GPR);
3090 const unsigned Num_FPR_Regs = 13;
3091 const unsigned Num_VR_Regs = array_lengthof(VR);
Hal Finkelc93a9a22015-02-25 01:06:45 +00003092 const unsigned Num_QFPR_Regs = Num_FPR_Regs;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003093
Ulrich Weigand8658f172014-07-20 23:43:15 +00003094 // Do a first pass over the arguments to determine whether the ABI
3095 // guarantees that our caller has allocated the parameter save area
3096 // on its stack frame. In the ELFv1 ABI, this is always the case;
3097 // in the ELFv2 ABI, it is true if this is a vararg function or if
3098 // any parameter is located in a stack slot.
3099
3100 bool HasParameterArea = !isELFv2ABI || isVarArg;
3101 unsigned ParamAreaSize = Num_GPR_Regs * PtrByteSize;
3102 unsigned NumBytes = LinkageSize;
3103 unsigned AvailableFPRs = Num_FPR_Regs;
3104 unsigned AvailableVRs = Num_VR_Regs;
Hal Finkel965cea52015-07-12 00:37:44 +00003105 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
3106 if (Ins[i].Flags.isNest())
3107 continue;
3108
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003109 if (CalculateStackSlotUsed(Ins[i].VT, Ins[i].ArgVT, Ins[i].Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00003110 PtrByteSize, LinkageSize, ParamAreaSize,
Hal Finkelc93a9a22015-02-25 01:06:45 +00003111 NumBytes, AvailableFPRs, AvailableVRs,
3112 Subtarget.hasQPX()))
Ulrich Weigand8658f172014-07-20 23:43:15 +00003113 HasParameterArea = true;
Hal Finkel965cea52015-07-12 00:37:44 +00003114 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003115
3116 // Add DAG nodes to load the arguments or copy them out of registers. On
3117 // entry to a function on PPC, the arguments start after the linkage area,
3118 // although the first ones are often in registers.
3119
Ulrich Weigand8658f172014-07-20 23:43:15 +00003120 unsigned ArgOffset = LinkageSize;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003121 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Hal Finkelc93a9a22015-02-25 01:06:45 +00003122 unsigned &QFPR_idx = FPR_idx;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003123 SmallVector<SDValue, 8> MemOps;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003124 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00003125 unsigned CurArgIdx = 0;
3126 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003127 SDValue ArgVal;
3128 bool needsLoad = false;
3129 EVT ObjectVT = Ins[ArgNo].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003130 EVT OrigVT = Ins[ArgNo].ArgVT;
Hal Finkel940ab932014-02-28 00:27:01 +00003131 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003132 unsigned ArgSize = ObjSize;
3133 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Andrew Trick05938a52015-02-16 18:10:47 +00003134 if (Ins[ArgNo].isOrigArg()) {
3135 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx);
3136 CurArgIdx = Ins[ArgNo].getOrigArgIndex();
3137 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003138 // We re-align the argument offset for each argument, except when using the
3139 // fast calling convention, when we need to make sure we do that only when
3140 // we'll actually use a stack slot.
3141 unsigned CurArgOffset, Align;
3142 auto ComputeArgOffset = [&]() {
3143 /* Respect alignment of argument on the stack. */
3144 Align = CalculateStackSlotAlignment(ObjectVT, OrigVT, Flags, PtrByteSize);
3145 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
3146 CurArgOffset = ArgOffset;
3147 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003148
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003149 if (CallConv != CallingConv::Fast) {
3150 ComputeArgOffset();
3151
3152 /* Compute GPR index associated with argument offset. */
3153 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
3154 GPR_idx = std::min(GPR_idx, Num_GPR_Regs);
3155 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003156
3157 // FIXME the codegen can be much improved in some cases.
3158 // We do not have to keep everything in memory.
3159 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003160 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit");
3161
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003162 if (CallConv == CallingConv::Fast)
3163 ComputeArgOffset();
3164
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003165 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
3166 ObjSize = Flags.getByValSize();
3167 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00003168 // Empty aggregate parameters do not take up registers. Examples:
3169 // struct { } a;
3170 // union { } b;
3171 // int c[0];
3172 // etc. However, we have to provide a place-holder in InVals, so
3173 // pretend we have an 8-byte item at the current address for that
3174 // purpose.
3175 if (!ObjSize) {
3176 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
3177 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
3178 InVals.push_back(FIN);
3179 continue;
3180 }
Hal Finkel262a2242013-09-12 23:20:06 +00003181
Ulrich Weigand24195972014-07-20 22:36:52 +00003182 // Create a stack object covering all stack doublewords occupied
Ulrich Weigand8658f172014-07-20 23:43:15 +00003183 // by the argument. If the argument is (fully or partially) on
3184 // the stack, or if the argument is fully in registers but the
3185 // caller has allocated the parameter save anyway, we can refer
3186 // directly to the caller's stack frame. Otherwise, create a
3187 // local copy in our own frame.
3188 int FI;
3189 if (HasParameterArea ||
3190 ArgSize + ArgOffset > LinkageSize + Num_GPR_Regs * PtrByteSize)
Hal Finkel41a55ad2014-08-16 00:17:05 +00003191 FI = MFI->CreateFixedObject(ArgSize, ArgOffset, false, true);
Ulrich Weigand8658f172014-07-20 23:43:15 +00003192 else
3193 FI = MFI->CreateStackObject(ArgSize, Align, false);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003194 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003195
Ulrich Weigand24195972014-07-20 22:36:52 +00003196 // Handle aggregates smaller than 8 bytes.
3197 if (ObjSize < PtrByteSize) {
3198 // The value of the object is its address, which differs from the
3199 // address of the enclosing doubleword on big-endian systems.
3200 SDValue Arg = FIN;
3201 if (!isLittleEndian) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003202 SDValue ArgOff = DAG.getConstant(PtrByteSize - ObjSize, dl, PtrVT);
Ulrich Weigand24195972014-07-20 22:36:52 +00003203 Arg = DAG.getNode(ISD::ADD, dl, ArgOff.getValueType(), Arg, ArgOff);
3204 }
3205 InVals.push_back(Arg);
3206
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003207 if (GPR_idx != Num_GPR_Regs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003208 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003209 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003210 SDValue Store;
3211
3212 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
3213 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
3214 (ObjSize == 2 ? MVT::i16 : MVT::i32));
Ulrich Weigand24195972014-07-20 22:36:52 +00003215 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, Arg,
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00003216 MachinePointerInfo(&*FuncArg), ObjType,
3217 false, false, 0);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003218 } else {
3219 // For sizes that don't fit a truncating store (3, 5, 6, 7),
3220 // store the whole register as-is to the parameter save area
Ulrich Weigand24195972014-07-20 22:36:52 +00003221 // slot.
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00003222 Store =
3223 DAG.getStore(Val.getValue(1), dl, Val, FIN,
3224 MachinePointerInfo(&*FuncArg), false, false, 0);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003225 }
3226
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003227 MemOps.push_back(Store);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003228 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003229 // Whether we copied from a register or not, advance the offset
3230 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003231 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003232 continue;
3233 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003234
Ulrich Weigand24195972014-07-20 22:36:52 +00003235 // The value of the object is its address, which is the address of
3236 // its first stack doubleword.
3237 InVals.push_back(FIN);
3238
3239 // Store whatever pieces of the object are in registers to memory.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003240 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
Ulrich Weigand24195972014-07-20 22:36:52 +00003241 if (GPR_idx == Num_GPR_Regs)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003242 break;
Ulrich Weigand24195972014-07-20 22:36:52 +00003243
3244 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3245 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
3246 SDValue Addr = FIN;
3247 if (j) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003248 SDValue Off = DAG.getConstant(j, dl, PtrVT);
Ulrich Weigand24195972014-07-20 22:36:52 +00003249 Addr = DAG.getNode(ISD::ADD, dl, Off.getValueType(), Addr, Off);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003250 }
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00003251 SDValue Store =
3252 DAG.getStore(Val.getValue(1), dl, Val, Addr,
3253 MachinePointerInfo(&*FuncArg, j), false, false, 0);
Ulrich Weigand24195972014-07-20 22:36:52 +00003254 MemOps.push_back(Store);
3255 ++GPR_idx;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003256 }
Ulrich Weigand24195972014-07-20 22:36:52 +00003257 ArgOffset += ArgSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003258 continue;
3259 }
3260
3261 switch (ObjectVT.getSimpleVT().SimpleTy) {
3262 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00003263 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003264 case MVT::i32:
3265 case MVT::i64:
Hal Finkel965cea52015-07-12 00:37:44 +00003266 if (Flags.isNest()) {
3267 // The 'nest' parameter, if any, is passed in R11.
3268 unsigned VReg = MF.addLiveIn(PPC::X11, &PPC::G8RCRegClass);
3269 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
3270
3271 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
3272 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
3273
3274 break;
3275 }
3276
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003277 // These can be scalar arguments or elements of an integer array type
3278 // passed directly. Clang may use those instead of "byval" aggregate
3279 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003280 if (GPR_idx != Num_GPR_Regs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003281 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003282 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
3283
Hal Finkel940ab932014-02-28 00:27:01 +00003284 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003285 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
3286 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003287 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003288 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003289 if (CallConv == CallingConv::Fast)
3290 ComputeArgOffset();
3291
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003292 needsLoad = true;
3293 ArgSize = PtrByteSize;
3294 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003295 if (CallConv != CallingConv::Fast || needsLoad)
3296 ArgOffset += 8;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003297 break;
3298
3299 case MVT::f32:
3300 case MVT::f64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003301 // These can be scalar arguments or elements of a float array type
3302 // passed directly. The latter are used to implement ELFv2 homogenous
3303 // float aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003304 if (FPR_idx != Num_FPR_Regs) {
3305 unsigned VReg;
3306
3307 if (ObjectVT == MVT::f32)
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00003308 VReg = MF.addLiveIn(FPR[FPR_idx],
3309 Subtarget.hasP8Vector()
3310 ? &PPC::VSSRCRegClass
3311 : &PPC::F4RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003312 else
Eric Christophercccae792015-01-30 22:02:31 +00003313 VReg = MF.addLiveIn(FPR[FPR_idx], Subtarget.hasVSX()
3314 ? &PPC::VSFRCRegClass
3315 : &PPC::F8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003316
3317 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
3318 ++FPR_idx;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003319 } else if (GPR_idx != Num_GPR_Regs && CallConv != CallingConv::Fast) {
Hal Finkel8ea446b2015-01-18 14:31:10 +00003320 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8
3321 // once we support fp <-> gpr moves.
3322
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003323 // This can only ever happen in the presence of f32 array types,
3324 // since otherwise we never run out of FPRs before running out
3325 // of GPRs.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003326 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003327 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
3328
3329 if (ObjectVT == MVT::f32) {
3330 if ((ArgOffset % PtrByteSize) == (isLittleEndian ? 4 : 0))
3331 ArgVal = DAG.getNode(ISD::SRL, dl, MVT::i64, ArgVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003332 DAG.getConstant(32, dl, MVT::i32));
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003333 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
3334 }
3335
3336 ArgVal = DAG.getNode(ISD::BITCAST, dl, ObjectVT, ArgVal);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003337 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003338 if (CallConv == CallingConv::Fast)
3339 ComputeArgOffset();
3340
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003341 needsLoad = true;
3342 }
3343
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003344 // When passing an array of floats, the array occupies consecutive
3345 // space in the argument area; only round up to the next doubleword
3346 // at the end of the array. Otherwise, each float takes 8 bytes.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003347 if (CallConv != CallingConv::Fast || needsLoad) {
3348 ArgSize = Flags.isInConsecutiveRegs() ? ObjSize : PtrByteSize;
3349 ArgOffset += ArgSize;
3350 if (Flags.isInConsecutiveRegsLast())
3351 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
3352 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003353 break;
3354 case MVT::v4f32:
3355 case MVT::v4i32:
3356 case MVT::v8i16:
3357 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00003358 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00003359 case MVT::v2i64:
Kit Bartond4eb73c2015-05-05 16:10:44 +00003360 case MVT::v1i128:
Hal Finkelc93a9a22015-02-25 01:06:45 +00003361 if (!Subtarget.hasQPX()) {
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003362 // These can be scalar arguments or elements of a vector array type
3363 // passed directly. The latter are used to implement ELFv2 homogenous
3364 // vector aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003365 if (VR_idx != Num_VR_Regs) {
Hal Finkel7811c612014-03-28 19:58:11 +00003366 unsigned VReg = (ObjectVT == MVT::v2f64 || ObjectVT == MVT::v2i64) ?
3367 MF.addLiveIn(VSRH[VR_idx], &PPC::VSHRCRegClass) :
3368 MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003369 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003370 ++VR_idx;
3371 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003372 if (CallConv == CallingConv::Fast)
3373 ComputeArgOffset();
3374
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003375 needsLoad = true;
3376 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003377 if (CallConv != CallingConv::Fast || needsLoad)
3378 ArgOffset += 16;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003379 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00003380 } // not QPX
3381
3382 assert(ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 &&
3383 "Invalid QPX parameter type");
3384 /* fall through */
3385
3386 case MVT::v4f64:
3387 case MVT::v4i1:
3388 // QPX vectors are treated like their scalar floating-point subregisters
3389 // (except that they're larger).
3390 unsigned Sz = ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 ? 16 : 32;
3391 if (QFPR_idx != Num_QFPR_Regs) {
3392 const TargetRegisterClass *RC;
3393 switch (ObjectVT.getSimpleVT().SimpleTy) {
3394 case MVT::v4f64: RC = &PPC::QFRCRegClass; break;
3395 case MVT::v4f32: RC = &PPC::QSRCRegClass; break;
3396 default: RC = &PPC::QBRCRegClass; break;
3397 }
3398
3399 unsigned VReg = MF.addLiveIn(QFPR[QFPR_idx], RC);
3400 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
3401 ++QFPR_idx;
3402 } else {
3403 if (CallConv == CallingConv::Fast)
3404 ComputeArgOffset();
3405 needsLoad = true;
3406 }
3407 if (CallConv != CallingConv::Fast || needsLoad)
3408 ArgOffset += Sz;
3409 break;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003410 }
3411
3412 // We need to load the argument to a virtual register if we determined
3413 // above that we ran out of physical registers of the appropriate type.
3414 if (needsLoad) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00003415 if (ObjSize < ArgSize && !isLittleEndian)
3416 CurArgOffset += ArgSize - ObjSize;
3417 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, isImmutable);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003418 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
3419 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
3420 false, false, false, 0);
3421 }
3422
3423 InVals.push_back(ArgVal);
3424 }
3425
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003426 // Area that is at least reserved in the caller of this function.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00003427 unsigned MinReservedArea;
Ulrich Weigand8658f172014-07-20 23:43:15 +00003428 if (HasParameterArea)
3429 MinReservedArea = std::max(ArgOffset, LinkageSize + 8 * PtrByteSize);
3430 else
3431 MinReservedArea = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003432
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003433 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003434 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003435 // taking the difference between two stack areas will result in an aligned
3436 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00003437 MinReservedArea =
3438 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003439 FuncInfo->setMinReservedArea(MinReservedArea);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003440
3441 // If the function takes variable number of arguments, make a frame index for
3442 // the start of the first vararg value... for expansion of llvm.va_start.
3443 if (isVarArg) {
3444 int Depth = ArgOffset;
3445
3446 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00003447 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003448 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
3449
3450 // If this function is vararg, store any remaining integer argument regs
3451 // to their spots on the stack so that they may be loaded by deferencing the
3452 // result of va_next.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00003453 for (GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
3454 GPR_idx < Num_GPR_Regs; ++GPR_idx) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003455 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3456 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
3457 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3458 MachinePointerInfo(), false, false, 0);
3459 MemOps.push_back(Store);
3460 // Increment the address by four for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003461 SDValue PtrOff = DAG.getConstant(PtrByteSize, dl, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003462 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
3463 }
3464 }
3465
3466 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003467 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003468
3469 return Chain;
3470}
3471
3472SDValue
3473PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003474 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003475 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003476 const SmallVectorImpl<ISD::InputArg>
3477 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003478 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003479 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003480 // TODO: add description of PPC stack frame format, or at least some docs.
3481 //
3482 MachineFunction &MF = DAG.getMachineFunction();
3483 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00003484 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003485
Mehdi Amini44ede332015-07-09 02:09:04 +00003486 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Owen Anderson9f944592009-08-11 20:47:22 +00003487 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003488 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003489 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
3490 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00003491 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Eric Christophera4ae2132015-02-13 22:22:57 +00003492 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003493 unsigned ArgOffset = LinkageSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003494 // Area that is at least reserved in caller of this function.
3495 unsigned MinReservedArea = ArgOffset;
3496
Craig Topper840beec2014-04-04 05:16:06 +00003497 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003498 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
3499 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
3500 };
Craig Topper840beec2014-04-04 05:16:06 +00003501 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00003502 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3503 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3504 };
Craig Topper840beec2014-04-04 05:16:06 +00003505 static const MCPhysReg VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003506 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3507 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3508 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00003509
Owen Andersone2f23a32007-09-07 04:06:50 +00003510 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003511 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00003512 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00003513
3514 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003515
Craig Topper840beec2014-04-04 05:16:06 +00003516 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003517
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003518 // In 32-bit non-varargs functions, the stack space for vectors is after the
3519 // stack space for non-vectors. We do not use this space unless we have
3520 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00003521 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003522 // that out...for the pathological case, compute VecArgOffset as the
3523 // start of the vector parameter area. Computing VecArgOffset is the
3524 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003525 unsigned VecArgOffset = ArgOffset;
3526 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003527 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003528 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003529 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003530 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003531
Duncan Sandsd97eea32008-03-21 09:14:45 +00003532 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003533 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00003534 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003535 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003536 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
3537 VecArgOffset += ArgSize;
3538 continue;
3539 }
3540
Owen Anderson9f944592009-08-11 20:47:22 +00003541 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003542 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003543 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003544 case MVT::i32:
3545 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003546 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003547 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003548 case MVT::i64: // PPC64
3549 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003550 // FIXME: We are guaranteed to be !isPPC64 at this point.
3551 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003552 VecArgOffset += 8;
3553 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003554 case MVT::v4f32:
3555 case MVT::v4i32:
3556 case MVT::v8i16:
3557 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003558 // Nothing to do, we're only looking at Nonvector args here.
3559 break;
3560 }
3561 }
3562 }
3563 // We've found where the vector parameter area in memory is. Skip the
3564 // first 12 parameters; these don't use that memory.
3565 VecArgOffset = ((VecArgOffset+15)/16)*16;
3566 VecArgOffset += 12*16;
3567
Chris Lattner4302e8f2006-05-16 18:18:50 +00003568 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00003569 // entry to a function on PPC, the arguments start after the linkage area,
3570 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00003571
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003572 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003573 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00003574 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00003575 unsigned CurArgIdx = 0;
3576 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003577 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003578 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00003579 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00003580 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00003581 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003582 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Andrew Trick05938a52015-02-16 18:10:47 +00003583 if (Ins[ArgNo].isOrigArg()) {
3584 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx);
3585 CurArgIdx = Ins[ArgNo].getOrigArgIndex();
3586 }
Chris Lattner318f0d22006-05-16 18:51:52 +00003587 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003588
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003589 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00003590 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
3591 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003592 if (isVarArg || isPPC64) {
3593 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003594 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003595 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003596 PtrByteSize);
3597 } else nAltivecParamsAtEnd++;
3598 } else
3599 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003600 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003601 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003602 PtrByteSize);
3603
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003604 // FIXME the codegen can be much improved in some cases.
3605 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003606 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003607 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit");
3608
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003609 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003610 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003611 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003612 // Objects of size 1 and 2 are right justified, everything else is
3613 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00003614 if (ObjSize==1 || ObjSize==2) {
3615 CurArgOffset = CurArgOffset + (4 - ObjSize);
3616 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003617 // The value of the object is its address.
Hal Finkel41a55ad2014-08-16 00:17:05 +00003618 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, false, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003619 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003620 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003621 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00003622 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003623 unsigned VReg;
3624 if (isPPC64)
3625 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3626 else
3627 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003628 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003629 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003630 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00003631 MachinePointerInfo(&*FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003632 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00003633 MemOps.push_back(Store);
3634 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00003635 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003636
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003637 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00003638
Dale Johannesen21a8f142008-03-08 01:41:42 +00003639 continue;
3640 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003641 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
3642 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003643 // to memory. ArgOffset will be the address of the beginning
3644 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003645 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003646 unsigned VReg;
3647 if (isPPC64)
3648 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3649 else
3650 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00003651 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003652 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003653 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00003654 SDValue Store =
3655 DAG.getStore(Val.getValue(1), dl, Val, FIN,
3656 MachinePointerInfo(&*FuncArg, j), false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003657 MemOps.push_back(Store);
3658 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003659 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003660 } else {
3661 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
3662 break;
3663 }
3664 }
3665 continue;
3666 }
3667
Owen Anderson9f944592009-08-11 20:47:22 +00003668 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003669 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003670 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003671 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00003672 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00003673 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003674 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003675 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Hal Finkel7f908e82014-03-06 00:45:19 +00003676
3677 if (ObjectVT == MVT::i1)
3678 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal);
3679
Bill Wendling968f32c2008-03-07 20:49:02 +00003680 ++GPR_idx;
3681 } else {
3682 needsLoad = true;
3683 ArgSize = PtrByteSize;
3684 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003685 // All int arguments reserve stack space in the Darwin ABI.
3686 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00003687 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003688 }
Bill Wendling968f32c2008-03-07 20:49:02 +00003689 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00003690 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00003691 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003692 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003693 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00003694
Hal Finkel940ab932014-02-28 00:27:01 +00003695 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00003696 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00003697 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003698 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00003699
Chris Lattnerec78cad2006-06-26 22:48:35 +00003700 ++GPR_idx;
3701 } else {
3702 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00003703 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003704 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003705 // All int arguments reserve stack space in the Darwin ABI.
3706 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003707 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003708
Owen Anderson9f944592009-08-11 20:47:22 +00003709 case MVT::f32:
3710 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00003711 // Every 4 bytes of argument space consumes one of the GPRs available for
3712 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003713 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003714 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00003715 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003716 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00003717 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003718 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003719 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003720
Owen Anderson9f944592009-08-11 20:47:22 +00003721 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00003722 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003723 else
Devang Patelf3292b22011-02-21 23:21:26 +00003724 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003725
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003726 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003727 ++FPR_idx;
3728 } else {
3729 needsLoad = true;
3730 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003731
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003732 // All FP arguments reserve stack space in the Darwin ABI.
3733 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003734 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003735 case MVT::v4f32:
3736 case MVT::v4i32:
3737 case MVT::v8i16:
3738 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00003739 // Note that vector arguments in registers don't reserve stack space,
3740 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003741 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003742 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003743 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00003744 if (isVarArg) {
3745 while ((ArgOffset % 16) != 0) {
3746 ArgOffset += PtrByteSize;
3747 if (GPR_idx != Num_GPR_Regs)
3748 GPR_idx++;
3749 }
3750 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003751 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00003752 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003753 ++VR_idx;
3754 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003755 if (!isVarArg && !isPPC64) {
3756 // Vectors go after all the nonvectors.
3757 CurArgOffset = VecArgOffset;
3758 VecArgOffset += 16;
3759 } else {
3760 // Vectors are aligned.
3761 ArgOffset = ((ArgOffset+15)/16)*16;
3762 CurArgOffset = ArgOffset;
3763 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00003764 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003765 needsLoad = true;
3766 }
3767 break;
3768 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003769
Chris Lattner4302e8f2006-05-16 18:18:50 +00003770 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003771 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003772 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003773 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003774 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00003775 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003776 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00003777 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003778 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003779 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003780
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003781 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003782 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003783
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003784 // Allow for Altivec parameters at the end, if needed.
3785 if (nAltivecParamsAtEnd) {
3786 MinReservedArea = ((MinReservedArea+15)/16)*16;
3787 MinReservedArea += 16*nAltivecParamsAtEnd;
3788 }
3789
3790 // Area that is at least reserved in the caller of this function.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003791 MinReservedArea = std::max(MinReservedArea, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003792
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003793 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003794 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003795 // taking the difference between two stack areas will result in an aligned
3796 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00003797 MinReservedArea =
3798 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003799 FuncInfo->setMinReservedArea(MinReservedArea);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003800
Chris Lattner4302e8f2006-05-16 18:18:50 +00003801 // If the function takes variable number of arguments, make a frame index for
3802 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003803 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003804 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003805
Dan Gohman31ae5862010-04-17 14:41:14 +00003806 FuncInfo->setVarArgsFrameIndex(
3807 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00003808 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00003809 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003810
Chris Lattner4302e8f2006-05-16 18:18:50 +00003811 // If this function is vararg, store any remaining integer argument regs
3812 // to their spots on the stack so that they may be loaded by deferencing the
3813 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003814 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00003815 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00003816
Chris Lattner2cca3852006-11-18 01:57:19 +00003817 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00003818 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003819 else
Devang Patelf3292b22011-02-21 23:21:26 +00003820 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003821
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003822 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00003823 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3824 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003825 MemOps.push_back(Store);
3826 // Increment the address by four for the next argument to store
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003827 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00003828 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003829 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003830 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003831
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003832 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003833 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003834
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003835 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003836}
3837
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003838/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003839/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00003840static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003841 unsigned ParamSize) {
3842
Dale Johannesen86dcae12009-11-24 01:09:07 +00003843 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003844
3845 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
3846 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3847 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3848 // Remember only if the new adjustement is bigger.
3849 if (SPDiff < FI->getTailCallSPDelta())
3850 FI->setTailCallSPDelta(SPDiff);
3851
3852 return SPDiff;
3853}
3854
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00003855static bool isFunctionGlobalAddress(SDValue Callee);
3856
3857static bool
3858resideInSameModule(SDValue Callee, Reloc::Model RelMod) {
3859 // If !G, Callee can be an external symbol.
3860 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
3861 if (!G) return false;
3862
3863 const GlobalValue *GV = G->getGlobal();
3864
3865 if (GV->isDeclaration()) return false;
3866
3867 switch(GV->getLinkage()) {
3868 default: llvm_unreachable("unknow linkage type");
3869 case GlobalValue::AvailableExternallyLinkage:
3870 case GlobalValue::ExternalWeakLinkage:
3871 return false;
3872
3873 // Callee with weak linkage is allowed if it has hidden or protected
3874 // visibility
3875 case GlobalValue::LinkOnceAnyLinkage:
3876 case GlobalValue::LinkOnceODRLinkage: // e.g. c++ inline functions
3877 case GlobalValue::WeakAnyLinkage:
3878 case GlobalValue::WeakODRLinkage: // e.g. c++ template instantiation
3879 if (GV->hasDefaultVisibility())
3880 return false;
3881
3882 case GlobalValue::ExternalLinkage:
3883 case GlobalValue::InternalLinkage:
3884 case GlobalValue::PrivateLinkage:
3885 break;
3886 }
3887
3888 // With '-fPIC', calling default visiblity function need insert 'nop' after
3889 // function call, no matter that function resides in same module or not, so
3890 // we treat it as in different module.
3891 if (RelMod == Reloc::PIC_ && GV->hasDefaultVisibility())
3892 return false;
3893
3894 return true;
3895}
3896
3897static bool
3898needStackSlotPassParameters(const PPCSubtarget &Subtarget,
3899 const SmallVectorImpl<ISD::OutputArg> &Outs) {
3900 assert(Subtarget.isSVR4ABI() && Subtarget.isPPC64());
3901
3902 const unsigned PtrByteSize = 8;
3903 const unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
3904
3905 static const MCPhysReg GPR[] = {
3906 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3907 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3908 };
3909 static const MCPhysReg VR[] = {
3910 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3911 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3912 };
3913
3914 const unsigned NumGPRs = array_lengthof(GPR);
3915 const unsigned NumFPRs = 13;
3916 const unsigned NumVRs = array_lengthof(VR);
3917 const unsigned ParamAreaSize = NumGPRs * PtrByteSize;
3918
3919 unsigned NumBytes = LinkageSize;
3920 unsigned AvailableFPRs = NumFPRs;
3921 unsigned AvailableVRs = NumVRs;
3922
3923 for (const ISD::OutputArg& Param : Outs) {
3924 if (Param.Flags.isNest()) continue;
3925
3926 if (CalculateStackSlotUsed(Param.VT, Param.ArgVT, Param.Flags,
3927 PtrByteSize, LinkageSize, ParamAreaSize,
3928 NumBytes, AvailableFPRs, AvailableVRs,
3929 Subtarget.hasQPX()))
3930 return true;
3931 }
3932 return false;
3933}
3934
3935static bool
3936hasSameArgumentList(const Function *CallerFn, ImmutableCallSite *CS) {
3937 if (CS->arg_size() != CallerFn->getArgumentList().size())
3938 return false;
3939
3940 ImmutableCallSite::arg_iterator CalleeArgIter = CS->arg_begin();
3941 ImmutableCallSite::arg_iterator CalleeArgEnd = CS->arg_end();
3942 Function::const_arg_iterator CallerArgIter = CallerFn->arg_begin();
3943
3944 for (; CalleeArgIter != CalleeArgEnd; ++CalleeArgIter, ++CallerArgIter) {
3945 const Value* CalleeArg = *CalleeArgIter;
3946 const Value* CallerArg = &(*CallerArgIter);
3947 if (CalleeArg == CallerArg)
3948 continue;
3949
3950 // e.g. @caller([4 x i64] %a, [4 x i64] %b) {
3951 // tail call @callee([4 x i64] undef, [4 x i64] %b)
3952 // }
3953 // 1st argument of callee is undef and has the same type as caller.
3954 if (CalleeArg->getType() == CallerArg->getType() &&
3955 isa<UndefValue>(CalleeArg))
3956 continue;
3957
3958 return false;
3959 }
3960
3961 return true;
3962}
3963
3964bool
3965PPCTargetLowering::IsEligibleForTailCallOptimization_64SVR4(
3966 SDValue Callee,
3967 CallingConv::ID CalleeCC,
3968 ImmutableCallSite *CS,
3969 bool isVarArg,
3970 const SmallVectorImpl<ISD::OutputArg> &Outs,
3971 const SmallVectorImpl<ISD::InputArg> &Ins,
3972 SelectionDAG& DAG) const {
3973 bool TailCallOpt = getTargetMachine().Options.GuaranteedTailCallOpt;
3974
3975 if (DisableSCO && !TailCallOpt) return false;
3976
3977 // Variadic argument functions are not supported.
3978 if (isVarArg) return false;
3979
3980 MachineFunction &MF = DAG.getMachineFunction();
3981 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
3982
3983 // Tail or Sibling call optimization (TCO/SCO) needs callee and caller has
3984 // the same calling convention
3985 if (CallerCC != CalleeCC) return false;
3986
3987 // SCO support C calling convention
3988 if (CalleeCC != CallingConv::Fast && CalleeCC != CallingConv::C)
3989 return false;
3990
3991 // Functions containing by val parameters are not supported.
3992 if (std::any_of(Ins.begin(), Ins.end(),
3993 [](const ISD::InputArg& IA) { return IA.Flags.isByVal(); }))
3994 return false;
3995
3996 // No TCO/SCO on indirect call because Caller have to restore its TOC
3997 if (!isFunctionGlobalAddress(Callee) &&
3998 !isa<ExternalSymbolSDNode>(Callee))
3999 return false;
4000
4001 // Check if Callee resides in the same module, because for now, PPC64 SVR4 ABI
4002 // (ELFv1/ELFv2) doesn't allow tail calls to a symbol resides in another
4003 // module.
4004 // ref: https://bugzilla.mozilla.org/show_bug.cgi?id=973977
4005 if (!resideInSameModule(Callee, getTargetMachine().getRelocationModel()))
4006 return false;
4007
4008 // TCO allows altering callee ABI, so we don't have to check further.
4009 if (CalleeCC == CallingConv::Fast && TailCallOpt)
4010 return true;
4011
4012 if (DisableSCO) return false;
4013
4014 // If callee use the same argument list that caller is using, then we can
4015 // apply SCO on this case. If it is not, then we need to check if callee needs
4016 // stack for passing arguments.
4017 if (!hasSameArgumentList(MF.getFunction(), CS) &&
4018 needStackSlotPassParameters(Subtarget, Outs)) {
4019 return false;
4020 }
4021
4022 return true;
4023}
4024
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004025/// IsEligibleForTailCallOptimization - Check whether the call is eligible
4026/// for tail call optimization. Targets which want to do tail call
4027/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004028bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004029PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004030 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004031 bool isVarArg,
4032 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004033 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004034 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00004035 return false;
4036
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004037 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004038 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00004039 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004040
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004041 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00004042 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004043 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
4044 // Functions containing by val parameters are not supported.
4045 for (unsigned i = 0; i != Ins.size(); i++) {
4046 ISD::ArgFlagsTy Flags = Ins[i].Flags;
4047 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004048 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004049
Alp Tokerf907b892013-12-05 05:44:44 +00004050 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004051 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
4052 return true;
4053
4054 // At the moment we can only do local tail calls (in same module, hidden
4055 // or protected) if we are generating PIC.
4056 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
4057 return G->getGlobal()->hasHiddenVisibility()
4058 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004059 }
4060
4061 return false;
4062}
4063
Chris Lattnereb755fc2006-05-17 19:00:46 +00004064/// isCallCompatibleAddress - Return the immediate to use if the specified
4065/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004066static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00004067 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00004068 if (!C) return nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004069
Dan Gohmaneffb8942008-09-12 16:56:44 +00004070 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00004071 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00004072 SignExtend32<26>(Addr) != Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +00004073 return nullptr; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004074
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004075 return DAG.getConstant((int)C->getZExtValue() >> 2, SDLoc(Op),
Mehdi Amini44ede332015-07-09 02:09:04 +00004076 DAG.getTargetLoweringInfo().getPointerTy(
4077 DAG.getDataLayout())).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00004078}
4079
Dan Gohmand78c4002008-05-13 00:00:25 +00004080namespace {
4081
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004082struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004083 SDValue Arg;
4084 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004085 int FrameIdx;
4086
4087 TailCallArgumentInfo() : FrameIdx(0) {}
4088};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00004089}
Dan Gohmand78c4002008-05-13 00:00:25 +00004090
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004091/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
4092static void
4093StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004094 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00004095 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
4096 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004097 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004098 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004099 SDValue Arg = TailCallArgs[i].Arg;
4100 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004101 int FI = TailCallArgs[i].FrameIdx;
4102 // Store relative to framepointer.
Alex Lorenze40c8a22015-08-11 23:09:45 +00004103 MemOpChains.push_back(DAG.getStore(
4104 Chain, dl, Arg, FIN,
4105 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false,
4106 false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004107 }
4108}
4109
4110/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
4111/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004112static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004113 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004114 SDValue Chain,
4115 SDValue OldRetAddr,
4116 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004117 int SPDiff,
4118 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004119 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004120 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004121 if (SPDiff) {
4122 // Calculate the new stack slot for the return address.
4123 int SlotSize = isPPC64 ? 8 : 4;
Eric Christopherdc3a8a42015-02-13 00:39:38 +00004124 const PPCFrameLowering *FL =
4125 MF.getSubtarget<PPCSubtarget>().getFrameLowering();
4126 int NewRetAddrLoc = SPDiff + FL->getReturnSaveOffset();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004127 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00004128 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00004129 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004130 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00004131 Chain = DAG.getStore(
4132 Chain, dl, OldRetAddr, NewRetAddrFrIdx,
4133 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), NewRetAddr),
4134 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004135
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004136 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
4137 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004138 if (isDarwinABI) {
Eric Christopherdc3a8a42015-02-13 00:39:38 +00004139 int NewFPLoc = SPDiff + FL->getFramePointerSaveOffset();
David Greene1fbe0542009-11-12 20:49:22 +00004140 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00004141 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004142 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00004143 Chain = DAG.getStore(
4144 Chain, dl, OldFP, NewFramePtrIdx,
4145 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), NewFPIdx),
4146 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004147 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004148 }
4149 return Chain;
4150}
4151
4152/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
4153/// the position of the argument.
4154static void
4155CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004156 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00004157 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004158 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00004159 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00004160 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00004161 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004162 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004163 TailCallArgumentInfo Info;
4164 Info.Arg = Arg;
4165 Info.FrameIdxOp = FIN;
4166 Info.FrameIdx = FI;
4167 TailCallArguments.push_back(Info);
4168}
4169
4170/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
4171/// stack slot. Returns the chain as result and the loaded frame pointers in
4172/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004173SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00004174 int SPDiff,
4175 SDValue Chain,
4176 SDValue &LROpOut,
4177 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004178 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004179 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004180 if (SPDiff) {
4181 // Load the LR and FP stack slot for later adjusting.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004182 EVT VT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004183 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00004184 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004185 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004186 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00004187
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004188 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
4189 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004190 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004191 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00004192 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004193 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004194 Chain = SDValue(FPOpOut.getNode(), 1);
4195 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004196 }
4197 return Chain;
4198}
4199
Dale Johannesen85d41a12008-03-04 23:17:14 +00004200/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00004201/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00004202/// specified by the specific parameter attribute. The copy will be passed as
4203/// a byval function parameter.
4204/// Sometimes what we are copying is the end of a larger object, the part that
4205/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004206static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004207CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00004208 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004209 SDLoc dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004210 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00004211 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004212 false, false, false, MachinePointerInfo(),
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004213 MachinePointerInfo());
Dale Johannesen85d41a12008-03-04 23:17:14 +00004214}
Chris Lattner43df5b32007-02-25 05:34:32 +00004215
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004216/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
4217/// tail calls.
4218static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004219LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
4220 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004221 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00004222 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
4223 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004224 SDLoc dl) {
Mehdi Amini44ede332015-07-09 02:09:04 +00004225 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004226 if (!isTailCall) {
4227 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004228 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004229 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004230 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004231 else
Owen Anderson9f944592009-08-11 20:47:22 +00004232 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00004233 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004234 DAG.getConstant(ArgOffset, dl, PtrVT));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004235 }
Chris Lattner676c61d2010-09-21 18:41:36 +00004236 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
4237 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004238 // Calculate and remember argument location.
4239 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
4240 TailCallArguments);
4241}
4242
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004243static
4244void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004245 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004246 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00004247 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004248 MachineFunction &MF = DAG.getMachineFunction();
4249
4250 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
4251 // might overwrite each other in case of tail call optimization.
4252 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00004253 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004254 InFlag = SDValue();
4255 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
4256 MemOpChains2, dl);
4257 if (!MemOpChains2.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004258 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004259
4260 // Store the return address to the appropriate stack slot.
4261 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
4262 isPPC64, isDarwinABI, dl);
4263
4264 // Emit callseq_end just before tailcall node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004265 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
4266 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004267 InFlag = Chain.getValue(1);
4268}
4269
Hal Finkel87deb0b2015-01-12 04:34:47 +00004270// Is this global address that of a function that can be called by name? (as
4271// opposed to something that must hold a descriptor for an indirect call).
4272static bool isFunctionGlobalAddress(SDValue Callee) {
4273 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
4274 if (Callee.getOpcode() == ISD::GlobalTLSAddress ||
4275 Callee.getOpcode() == ISD::TargetGlobalTLSAddress)
4276 return false;
4277
Manuel Jacob5f6eaac2016-01-16 20:30:46 +00004278 return G->getGlobal()->getValueType()->isFunctionTy();
Hal Finkel87deb0b2015-01-12 04:34:47 +00004279 }
4280
4281 return false;
4282}
4283
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004284static
4285unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004286 SDValue &Chain, SDValue CallSeqStart, SDLoc dl, int SPDiff,
Hal Finkel965cea52015-07-12 00:37:44 +00004287 bool isTailCall, bool IsPatchPoint, bool hasNest,
Craig Topperb94011f2013-07-14 04:42:23 +00004288 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
4289 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004290 ImmutableCallSite *CS, const PPCSubtarget &Subtarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004291
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004292 bool isPPC64 = Subtarget.isPPC64();
4293 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004294 bool isELFv2ABI = Subtarget.isELFv2ABI();
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004295
Mehdi Amini44ede332015-07-09 02:09:04 +00004296 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Owen Anderson9f944592009-08-11 20:47:22 +00004297 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004298 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004299
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004300 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004301
Torok Edwin31e90d22010-08-04 20:47:44 +00004302 bool needIndirectCall = true;
Ulrich Weigand9aa09ef2014-06-18 16:14:04 +00004303 if (!isSVR4ABI || !isPPC64)
4304 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
4305 // If this is an absolute destination address, use the munged value.
4306 Callee = SDValue(Dest, 0);
4307 needIndirectCall = false;
4308 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004309
Hal Finkel87deb0b2015-01-12 04:34:47 +00004310 if (isFunctionGlobalAddress(Callee)) {
4311 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Callee);
4312 // A call to a TLS address is actually an indirect call to a
4313 // thread-specific pointer.
Eric Christopher79cc1e32014-09-02 22:28:02 +00004314 unsigned OpFlags = 0;
4315 if ((DAG.getTarget().getRelocationModel() != Reloc::Static &&
4316 (Subtarget.getTargetTriple().isMacOSX() &&
4317 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00004318 !G->getGlobal()->isStrongDefinitionForLinker()) ||
Eric Christopher79cc1e32014-09-02 22:28:02 +00004319 (Subtarget.isTargetELF() && !isPPC64 &&
4320 !G->getGlobal()->hasLocalLinkage() &&
4321 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
4322 // PC-relative references to external symbols should go through $stub,
4323 // unless we're building with the leopard linker or later, which
4324 // automatically synthesizes these stubs.
4325 OpFlags = PPCII::MO_PLT_OR_STUB;
Eric Christopherb9fd9ed2014-08-07 22:02:54 +00004326 }
Eric Christopher79cc1e32014-09-02 22:28:02 +00004327
4328 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
4329 // every direct call is) turn it into a TargetGlobalAddress /
4330 // TargetExternalSymbol node so that legalize doesn't hack it.
4331 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
4332 Callee.getValueType(), 0, OpFlags);
4333 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00004334 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004335
Torok Edwin31e90d22010-08-04 20:47:44 +00004336 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004337 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00004338
Hal Finkel3ee2af72014-07-18 23:29:49 +00004339 if ((DAG.getTarget().getRelocationModel() != Reloc::Static &&
4340 (Subtarget.getTargetTriple().isMacOSX() &&
4341 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5))) ||
4342 (Subtarget.isTargetELF() && !isPPC64 &&
Justin Hibbits17744c12015-01-10 07:50:31 +00004343 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004344 // PC-relative references to external symbols should go through $stub,
4345 // unless we're building with the leopard linker or later, which
4346 // automatically synthesizes these stubs.
Hal Finkel3ee2af72014-07-18 23:29:49 +00004347 OpFlags = PPCII::MO_PLT_OR_STUB;
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004348 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004349
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004350 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
4351 OpFlags);
4352 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00004353 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004354
Hal Finkel934361a2015-01-14 01:07:51 +00004355 if (IsPatchPoint) {
4356 // We'll form an invalid direct call when lowering a patchpoint; the full
4357 // sequence for an indirect call is complicated, and many of the
4358 // instructions introduced might have side effects (and, thus, can't be
4359 // removed later). The call itself will be removed as soon as the
4360 // argument/return lowering is complete, so the fact that it has the wrong
4361 // kind of operands should not really matter.
4362 needIndirectCall = false;
4363 }
4364
Torok Edwin31e90d22010-08-04 20:47:44 +00004365 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004366 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
4367 // to do the call, we can't use PPCISD::CALL.
4368 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00004369
Hal Finkel63fb9282015-01-13 18:25:05 +00004370 if (isSVR4ABI && isPPC64 && !isELFv2ABI) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00004371 // Function pointers in the 64-bit SVR4 ABI do not point to the function
4372 // entry point, but to the function descriptor (the function entry point
4373 // address is part of the function descriptor though).
4374 // The function descriptor is a three doubleword structure with the
4375 // following fields: function entry point, TOC base address and
4376 // environment pointer.
4377 // Thus for a call through a function pointer, the following actions need
4378 // to be performed:
4379 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00004380 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00004381 // 2. Load the address of the function entry point from the function
4382 // descriptor.
4383 // 3. Load the TOC of the callee from the function descriptor into r2.
4384 // 4. Load the environment pointer from the function descriptor into
4385 // r11.
4386 // 5. Branch to the function entry point address.
4387 // 6. On return of the callee, the TOC of the caller needs to be
4388 // restored (this is done in FinishCall()).
4389 //
Hal Finkele2ab0f12015-01-15 21:17:34 +00004390 // The loads are scheduled at the beginning of the call sequence, and the
4391 // register copies are flagged together to ensure that no other
Tilmann Scheller79fef932009-12-18 13:00:15 +00004392 // operations can be scheduled in between. E.g. without flagging the
Hal Finkele2ab0f12015-01-15 21:17:34 +00004393 // copies together, a TOC access in the caller could be scheduled between
4394 // the assignment of the callee TOC and the branch to the callee, which
Tilmann Scheller79fef932009-12-18 13:00:15 +00004395 // results in the TOC access going through the TOC of the callee instead
4396 // of going through the TOC of the caller, which leads to incorrect code.
4397
4398 // Load the address of the function entry point from the function
4399 // descriptor.
Hal Finkele2ab0f12015-01-15 21:17:34 +00004400 SDValue LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-1);
4401 if (LDChain.getValueType() == MVT::Glue)
4402 LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-2);
4403
4404 bool LoadsInv = Subtarget.hasInvariantFunctionDescriptors();
4405
4406 MachinePointerInfo MPI(CS ? CS->getCalledValue() : nullptr);
4407 SDValue LoadFuncPtr = DAG.getLoad(MVT::i64, dl, LDChain, Callee, MPI,
4408 false, false, LoadsInv, 8);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004409
4410 // Load environment pointer into r11.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004411 SDValue PtrOff = DAG.getIntPtrConstant(16, dl);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004412 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004413 SDValue LoadEnvPtr = DAG.getLoad(MVT::i64, dl, LDChain, AddPtr,
4414 MPI.getWithOffset(16), false, false,
4415 LoadsInv, 8);
4416
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004417 SDValue TOCOff = DAG.getIntPtrConstant(8, dl);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004418 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, TOCOff);
4419 SDValue TOCPtr = DAG.getLoad(MVT::i64, dl, LDChain, AddTOC,
4420 MPI.getWithOffset(8), false, false,
4421 LoadsInv, 8);
4422
Hal Finkele6698d52015-02-01 15:03:28 +00004423 setUsesTOCBasePtr(DAG);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004424 SDValue TOCVal = DAG.getCopyToReg(Chain, dl, PPC::X2, TOCPtr,
4425 InFlag);
4426 Chain = TOCVal.getValue(0);
4427 InFlag = TOCVal.getValue(1);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004428
Hal Finkel965cea52015-07-12 00:37:44 +00004429 // If the function call has an explicit 'nest' parameter, it takes the
4430 // place of the environment pointer.
4431 if (!hasNest) {
4432 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
4433 InFlag);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004434
Hal Finkel965cea52015-07-12 00:37:44 +00004435 Chain = EnvVal.getValue(0);
4436 InFlag = EnvVal.getValue(1);
4437 }
Tilmann Scheller79fef932009-12-18 13:00:15 +00004438
Tilmann Scheller79fef932009-12-18 13:00:15 +00004439 MTCTROps[0] = Chain;
4440 MTCTROps[1] = LoadFuncPtr;
4441 MTCTROps[2] = InFlag;
4442 }
4443
Hal Finkel63fb9282015-01-13 18:25:05 +00004444 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys,
4445 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
4446 InFlag = Chain.getValue(1);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004447
4448 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00004449 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004450 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004451 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004452 CallOpc = PPCISD::BCTRL;
Craig Topper062a2ba2014-04-25 05:30:21 +00004453 Callee.setNode(nullptr);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004454 // Add use of X11 (holding environment pointer)
Hal Finkel965cea52015-07-12 00:37:44 +00004455 if (isSVR4ABI && isPPC64 && !isELFv2ABI && !hasNest)
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004456 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004457 // Add CTR register as callee so a bctr can be emitted later.
4458 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00004459 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004460 }
4461
4462 // If this is a direct call, pass the chain and the callee.
4463 if (Callee.getNode()) {
4464 Ops.push_back(Chain);
4465 Ops.push_back(Callee);
4466 }
4467 // If this is a tail call add stack pointer delta.
4468 if (isTailCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004469 Ops.push_back(DAG.getConstant(SPDiff, dl, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004470
4471 // Add argument registers to the end of the list so that they are known live
4472 // into the call.
4473 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
4474 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
4475 RegsToPass[i].second.getValueType()));
4476
Hal Finkelaf519932015-01-19 07:20:27 +00004477 // All calls, in both the ELF V1 and V2 ABIs, need the TOC register live
4478 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00004479 if (isSVR4ABI && isPPC64 && !IsPatchPoint) {
4480 setUsesTOCBasePtr(DAG);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004481 Ops.push_back(DAG.getRegister(PPC::X2, PtrVT));
Hal Finkele6698d52015-02-01 15:03:28 +00004482 }
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004483
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004484 return CallOpc;
4485}
4486
Roman Divacky76293062012-09-18 16:47:58 +00004487static
4488bool isLocalCall(const SDValue &Callee)
4489{
4490 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00004491 return G->getGlobal()->isStrongDefinitionForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00004492 return false;
4493}
4494
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004495SDValue
4496PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004497 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004498 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004499 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004500 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004501
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004502 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00004503 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
4504 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004505 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004506
4507 // Copy all of the result registers out of their specified physreg.
4508 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
4509 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004510 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004511
4512 SDValue Val = DAG.getCopyFromReg(Chain, dl,
4513 VA.getLocReg(), VA.getLocVT(), InFlag);
4514 Chain = Val.getValue(1);
4515 InFlag = Val.getValue(2);
4516
4517 switch (VA.getLocInfo()) {
4518 default: llvm_unreachable("Unknown loc info!");
4519 case CCValAssign::Full: break;
4520 case CCValAssign::AExt:
4521 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4522 break;
4523 case CCValAssign::ZExt:
4524 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
4525 DAG.getValueType(VA.getValVT()));
4526 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4527 break;
4528 case CCValAssign::SExt:
4529 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
4530 DAG.getValueType(VA.getValVT()));
4531 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4532 break;
4533 }
4534
4535 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004536 }
4537
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004538 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004539}
4540
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004541SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004542PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Hal Finkel934361a2015-01-14 01:07:51 +00004543 bool isTailCall, bool isVarArg, bool IsPatchPoint,
Hal Finkel965cea52015-07-12 00:37:44 +00004544 bool hasNest, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004545 SmallVector<std::pair<unsigned, SDValue>, 8>
4546 &RegsToPass,
4547 SDValue InFlag, SDValue Chain,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004548 SDValue CallSeqStart, SDValue &Callee,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004549 int SPDiff, unsigned NumBytes,
4550 const SmallVectorImpl<ISD::InputArg> &Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004551 SmallVectorImpl<SDValue> &InVals,
4552 ImmutableCallSite *CS) const {
Ulrich Weigand8658f172014-07-20 23:43:15 +00004553
Owen Anderson53aa7a92009-08-10 22:56:29 +00004554 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004555 SmallVector<SDValue, 8> Ops;
Hal Finkele2ab0f12015-01-15 21:17:34 +00004556 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, CallSeqStart, dl,
Hal Finkel965cea52015-07-12 00:37:44 +00004557 SPDiff, isTailCall, IsPatchPoint, hasNest,
4558 RegsToPass, Ops, NodeTys, CS, Subtarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004559
Hal Finkel5ab37802012-08-28 02:10:27 +00004560 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004561 if (isVarArg && Subtarget.isSVR4ABI() && !Subtarget.isPPC64())
Hal Finkel5ab37802012-08-28 02:10:27 +00004562 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
4563
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004564 // When performing tail call optimization the callee pops its arguments off
4565 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00004566 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004567 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004568 (CallConv == CallingConv::Fast &&
4569 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004570
Roman Divackyef21be22012-03-06 16:41:49 +00004571 // Add a register mask operand representing the call-preserved registers.
Eric Christophercccae792015-01-30 22:02:31 +00004572 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00004573 const uint32_t *Mask =
4574 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv);
Roman Divackyef21be22012-03-06 16:41:49 +00004575 assert(Mask && "Missing call preserved mask for calling convention");
4576 Ops.push_back(DAG.getRegisterMask(Mask));
4577
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004578 if (InFlag.getNode())
4579 Ops.push_back(InFlag);
4580
4581 // Emit tail call.
4582 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004583 assert(((Callee.getOpcode() == ISD::Register &&
4584 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
4585 Callee.getOpcode() == ISD::TargetExternalSymbol ||
4586 Callee.getOpcode() == ISD::TargetGlobalAddress ||
4587 isa<ConstantSDNode>(Callee)) &&
4588 "Expecting an global address, external symbol, absolute value or register");
4589
Arnold Schwaighoferdc271142015-05-09 00:10:25 +00004590 DAG.getMachineFunction().getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00004591 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004592 }
4593
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004594 // Add a NOP immediately after the branch instruction when using the 64-bit
4595 // SVR4 ABI. At link time, if caller and callee are in a different module and
4596 // thus have a different TOC, the call will be replaced with a call to a stub
4597 // function which saves the current TOC, loads the TOC of the callee and
4598 // branches to the callee. The NOP will be replaced with a load instruction
4599 // which restores the TOC of the caller from the TOC save slot of the current
4600 // stack frame. If caller and callee belong to the same module (and have the
4601 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00004602
Hal Finkel934361a2015-01-14 01:07:51 +00004603 if (!isTailCall && Subtarget.isSVR4ABI()&& Subtarget.isPPC64() &&
4604 !IsPatchPoint) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004605 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00004606 // This is a call through a function pointer.
4607 // Restore the caller TOC from the save area into R2.
4608 // See PrepareCall() for more information about calls through function
4609 // pointers in the 64-bit SVR4 ABI.
4610 // We are using a target-specific load with r2 hard coded, because the
4611 // result of a target-independent load would never go directly into r2,
4612 // since r2 is a reserved register (which prevents the register allocator
4613 // from allocating it), resulting in an additional register being
4614 // allocated and an unnecessary move instruction being generated.
Hal Finkelfc096c92014-12-23 22:29:40 +00004615 CallOpc = PPCISD::BCTRL_LOAD_TOC;
4616
Mehdi Amini44ede332015-07-09 02:09:04 +00004617 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Hal Finkelfc096c92014-12-23 22:29:40 +00004618 SDValue StackPtr = DAG.getRegister(PPC::X1, PtrVT);
Eric Christopher736d39e2015-02-13 00:39:36 +00004619 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004620 SDValue TOCOff = DAG.getIntPtrConstant(TOCSaveOffset, dl);
Hal Finkelfc096c92014-12-23 22:29:40 +00004621 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, StackPtr, TOCOff);
4622
4623 // The address needs to go after the chain input but before the flag (or
4624 // any other variadic arguments).
4625 Ops.insert(std::next(Ops.begin()), AddTOC);
Bill Schmidtcea15962013-09-26 17:09:28 +00004626 } else if ((CallOpc == PPCISD::CALL) &&
4627 (!isLocalCall(Callee) ||
Bill Schmidt82f1c772015-02-10 19:09:05 +00004628 DAG.getTarget().getRelocationModel() == Reloc::PIC_))
Roman Divacky76293062012-09-18 16:47:58 +00004629 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004630 CallOpc = PPCISD::CALL_NOP;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004631 }
4632
Craig Topper48d114b2014-04-26 18:35:24 +00004633 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Hal Finkel51861b42012-03-31 14:45:15 +00004634 InFlag = Chain.getValue(1);
4635
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004636 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
4637 DAG.getIntPtrConstant(BytesCalleePops, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00004638 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004639 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004640 InFlag = Chain.getValue(1);
4641
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004642 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
4643 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004644}
4645
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004646SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00004647PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004648 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00004649 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00004650 SDLoc &dl = CLI.DL;
4651 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
4652 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
4653 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00004654 SDValue Chain = CLI.Chain;
4655 SDValue Callee = CLI.Callee;
4656 bool &isTailCall = CLI.IsTailCall;
4657 CallingConv::ID CallConv = CLI.CallConv;
4658 bool isVarArg = CLI.IsVarArg;
Hal Finkel934361a2015-01-14 01:07:51 +00004659 bool IsPatchPoint = CLI.IsPatchPoint;
Hal Finkele2ab0f12015-01-15 21:17:34 +00004660 ImmutableCallSite *CS = CLI.CS;
Justin Holewinskiaa583972012-05-25 16:35:28 +00004661
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00004662 if (isTailCall) {
4663 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64())
4664 isTailCall =
4665 IsEligibleForTailCallOptimization_64SVR4(Callee, CallConv, CS,
4666 isVarArg, Outs, Ins, DAG);
4667 else
4668 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
4669 Ins, DAG);
4670 if (isTailCall) {
4671 ++NumTailCalls;
4672 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
4673 ++NumSiblingCalls;
4674
4675 assert(isa<GlobalAddressSDNode>(Callee) &&
4676 "Callee should be an llvm::Function object.");
4677 DEBUG(
4678 const GlobalValue *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal();
4679 const unsigned Width = 80 - strlen("TCO caller: ")
4680 - strlen(", callee linkage: 0, 0");
4681 dbgs() << "TCO caller: "
4682 << left_justify(DAG.getMachineFunction().getName(), Width)
4683 << ", callee linkage: "
4684 << GV->getVisibility() << ", " << GV->getLinkage() << "\n"
4685 );
4686 }
4687 }
Evan Cheng67a69dd2010-01-27 00:07:07 +00004688
Hal Finkele2ab0f12015-01-15 21:17:34 +00004689 if (!isTailCall && CS && CS->isMustTailCall())
Reid Kleckner5772b772014-04-24 20:14:34 +00004690 report_fatal_error("failed to perform tail call elimination on a call "
4691 "site marked musttail");
4692
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004693 if (Subtarget.isSVR4ABI()) {
4694 if (Subtarget.isPPC64())
Bill Schmidt57d6de52012-10-23 15:51:16 +00004695 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004696 isTailCall, IsPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004697 dl, DAG, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004698 else
4699 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004700 isTailCall, IsPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004701 dl, DAG, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004702 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004703
Bill Schmidt57d6de52012-10-23 15:51:16 +00004704 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004705 isTailCall, IsPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004706 dl, DAG, InVals, CS);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004707}
4708
4709SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004710PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
4711 CallingConv::ID CallConv, bool isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004712 bool isTailCall, bool IsPatchPoint,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004713 const SmallVectorImpl<ISD::OutputArg> &Outs,
4714 const SmallVectorImpl<SDValue> &OutVals,
4715 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004716 SDLoc dl, SelectionDAG &DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004717 SmallVectorImpl<SDValue> &InVals,
4718 ImmutableCallSite *CS) const {
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004719 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004720 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004721
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004722 assert((CallConv == CallingConv::C ||
4723 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004724
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004725 unsigned PtrByteSize = 4;
4726
4727 MachineFunction &MF = DAG.getMachineFunction();
4728
4729 // Mark this function as potentially containing a function that contains a
4730 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4731 // and restoring the callers stack pointer in this functions epilog. This is
4732 // done because by tail calling the called function might overwrite the value
4733 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004734 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4735 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004736 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00004737
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004738 // Count how many bytes are to be pushed on the stack, including the linkage
4739 // area, parameter list area and the part of the local variable space which
4740 // contains copies of aggregates which are passed by value.
4741
4742 // Assign locations to all of the outgoing arguments.
4743 SmallVector<CCValAssign, 16> ArgLocs;
Strahinja Petrovice682b802016-05-09 12:27:39 +00004744 PPCCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
Eric Christopherb5217502014-08-06 18:45:26 +00004745 *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004746
4747 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00004748 CCInfo.AllocateStack(Subtarget.getFrameLowering()->getLinkageSize(),
Ulrich Weigand8658f172014-07-20 23:43:15 +00004749 PtrByteSize);
Strahinja Petrovice682b802016-05-09 12:27:39 +00004750 if (Subtarget.useSoftFloat())
4751 CCInfo.PreAnalyzeCallOperands(Outs);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004752
4753 if (isVarArg) {
4754 // Handle fixed and variable vector arguments differently.
4755 // Fixed vector arguments go into registers as long as registers are
4756 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004757 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00004758
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004759 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00004760 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004761 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004762 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00004763
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004764 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00004765 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
4766 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004767 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00004768 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
4769 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004770 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004771
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004772 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00004773#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00004774 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00004775 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00004776#endif
Craig Toppere73658d2014-04-28 04:05:08 +00004777 llvm_unreachable(nullptr);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004778 }
4779 }
4780 } else {
4781 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00004782 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004783 }
Strahinja Petrovice682b802016-05-09 12:27:39 +00004784 CCInfo.clearWasPPCF128();
4785
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004786 // Assign locations to all of the outgoing aggregate by value arguments.
4787 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004788 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Eric Christopherb5217502014-08-06 18:45:26 +00004789 ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004790
4791 // Reserve stack space for the allocations in CCInfo.
4792 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
4793
Bill Schmidtef17c142013-02-06 17:33:58 +00004794 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004795
4796 // Size of the linkage area, parameter list area and the part of the local
4797 // space variable where copies of aggregates which are passed by value are
4798 // stored.
4799 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004800
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004801 // Calculate by how many bytes the stack has to be adjusted in case of tail
4802 // call optimization.
4803 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4804
4805 // Adjust the stack pointer for the new arguments...
4806 // These operations are automatically eliminated by the prolog/epilog pass
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004807 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00004808 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004809 SDValue CallSeqStart = Chain;
4810
4811 // Load the return address and frame pointer so it can be moved somewhere else
4812 // later.
4813 SDValue LROp, FPOp;
4814 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
4815 dl);
4816
4817 // Set up a copy of the stack pointer for use loading and storing any
4818 // arguments that may not fit in the registers available for argument
4819 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00004820 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00004821
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004822 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4823 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4824 SmallVector<SDValue, 8> MemOpChains;
4825
Roman Divacky71038e72011-08-30 17:04:16 +00004826 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004827 // Walk the register/memloc assignments, inserting copies/loads.
4828 for (unsigned i = 0, j = 0, e = ArgLocs.size();
4829 i != e;
4830 ++i) {
4831 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004832 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004833 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00004834
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004835 if (Flags.isByVal()) {
4836 // Argument is an aggregate which is passed by value, thus we need to
4837 // create a copy of it in the local variable space of the current stack
4838 // frame (which is the stack frame of the caller) and pass the address of
4839 // this copy to the callee.
4840 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
4841 CCValAssign &ByValVA = ByValArgLocs[j++];
4842 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00004843
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004844 // Memory reserved in the local variable space of the callers stack frame.
4845 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004846
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004847 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00004848 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()),
4849 StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00004850
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004851 // Create a copy of the argument in the local area of the current
4852 // stack frame.
4853 SDValue MemcpyCall =
4854 CreateCopyOfByValArgument(Arg, PtrOff,
4855 CallSeqStart.getNode()->getOperand(0),
4856 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00004857
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004858 // This must go outside the CALLSEQ_START..END.
4859 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004860 CallSeqStart.getNode()->getOperand(1),
4861 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004862 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4863 NewCallSeqStart.getNode());
4864 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00004865
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004866 // Pass the address of the aggregate copy on the stack either in a
4867 // physical register or in the parameter list area of the current stack
4868 // frame to the callee.
4869 Arg = PtrOff;
4870 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004871
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004872 if (VA.isRegLoc()) {
Hal Finkel2a9d3182014-03-06 00:23:33 +00004873 if (Arg.getValueType() == MVT::i1)
4874 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg);
4875
Roman Divacky71038e72011-08-30 17:04:16 +00004876 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004877 // Put argument in a physical register.
4878 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
4879 } else {
4880 // Put argument in the parameter list area of the current stack frame.
4881 assert(VA.isMemLoc());
4882 unsigned LocMemOffset = VA.getLocMemOffset();
4883
4884 if (!isTailCall) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004885 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00004886 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()),
4887 StackPtr, PtrOff);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004888
4889 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00004890 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004891 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004892 } else {
4893 // Calculate and remember argument location.
4894 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
4895 TailCallArguments);
4896 }
4897 }
4898 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004899
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004900 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004901 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Wesley Peck527da1b2010-11-23 03:31:01 +00004902
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004903 // Build a sequence of copy-to-reg nodes chained together with token chain
4904 // and flag operands which copy the outgoing args into the appropriate regs.
4905 SDValue InFlag;
4906 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4907 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4908 RegsToPass[i].second, InFlag);
4909 InFlag = Chain.getValue(1);
4910 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004911
Hal Finkel5ab37802012-08-28 02:10:27 +00004912 // Set CR bit 6 to true if this is a vararg call with floating args passed in
4913 // registers.
4914 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004915 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
4916 SDValue Ops[] = { Chain, InFlag };
4917
Hal Finkel5ab37802012-08-28 02:10:27 +00004918 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00004919 dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1));
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004920
Hal Finkel5ab37802012-08-28 02:10:27 +00004921 InFlag = Chain.getValue(1);
4922 }
4923
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004924 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004925 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
4926 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004927
Hal Finkel965cea52015-07-12 00:37:44 +00004928 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint,
4929 /* unused except on PPC64 ELFv1 */ false, DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004930 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff,
4931 NumBytes, Ins, InVals, CS);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004932}
4933
Bill Schmidt57d6de52012-10-23 15:51:16 +00004934// Copy an argument into memory, being careful to do this outside the
4935// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004936SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00004937PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
4938 SDValue CallSeqStart,
4939 ISD::ArgFlagsTy Flags,
4940 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004941 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004942 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
4943 CallSeqStart.getNode()->getOperand(0),
4944 Flags, DAG, dl);
4945 // The MEMCPY must go outside the CALLSEQ_START..END.
4946 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004947 CallSeqStart.getNode()->getOperand(1),
4948 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004949 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4950 NewCallSeqStart.getNode());
4951 return NewCallSeqStart;
4952}
4953
4954SDValue
4955PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004956 CallingConv::ID CallConv, bool isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004957 bool isTailCall, bool IsPatchPoint,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004958 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004959 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004960 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004961 SDLoc dl, SelectionDAG &DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004962 SmallVectorImpl<SDValue> &InVals,
4963 ImmutableCallSite *CS) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004964
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004965 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004966 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004967 unsigned NumOps = Outs.size();
Hal Finkel965cea52015-07-12 00:37:44 +00004968 bool hasNest = false;
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00004969 bool IsSibCall = false;
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004970
Mehdi Amini44ede332015-07-09 02:09:04 +00004971 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Bill Schmidt57d6de52012-10-23 15:51:16 +00004972 unsigned PtrByteSize = 8;
4973
4974 MachineFunction &MF = DAG.getMachineFunction();
4975
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00004976 if (isTailCall && !getTargetMachine().Options.GuaranteedTailCallOpt)
4977 IsSibCall = true;
4978
Bill Schmidt57d6de52012-10-23 15:51:16 +00004979 // Mark this function as potentially containing a function that contains a
4980 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4981 // and restoring the callers stack pointer in this functions epilog. This is
4982 // done because by tail calling the called function might overwrite the value
4983 // in this function's (MF) stack pointer stack slot 0(SP).
4984 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4985 CallConv == CallingConv::Fast)
4986 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4987
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004988 assert(!(CallConv == CallingConv::Fast && isVarArg) &&
4989 "fastcc not supported on varargs functions");
4990
Bill Schmidt57d6de52012-10-23 15:51:16 +00004991 // Count how many bytes are to be pushed on the stack, including the linkage
Ulrich Weigand8658f172014-07-20 23:43:15 +00004992 // area, and parameter passing area. On ELFv1, the linkage area is 48 bytes
4993 // reserved space for [SP][CR][LR][2 x unused][TOC]; on ELFv2, the linkage
4994 // area is 32 bytes reserved space for [SP][CR][LR][TOC].
Eric Christophera4ae2132015-02-13 22:22:57 +00004995 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004996 unsigned NumBytes = LinkageSize;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004997 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Hal Finkelc93a9a22015-02-25 01:06:45 +00004998 unsigned &QFPR_idx = FPR_idx;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004999
5000 static const MCPhysReg GPR[] = {
5001 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
5002 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
5003 };
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005004 static const MCPhysReg VR[] = {
5005 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
5006 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
5007 };
5008 static const MCPhysReg VSRH[] = {
5009 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
5010 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
5011 };
5012
5013 const unsigned NumGPRs = array_lengthof(GPR);
5014 const unsigned NumFPRs = 13;
5015 const unsigned NumVRs = array_lengthof(VR);
Hal Finkelc93a9a22015-02-25 01:06:45 +00005016 const unsigned NumQFPRs = NumFPRs;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005017
5018 // When using the fast calling convention, we don't provide backing for
5019 // arguments that will be in registers.
5020 unsigned NumGPRsUsed = 0, NumFPRsUsed = 0, NumVRsUsed = 0;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005021
5022 // Add up all the space actually used.
5023 for (unsigned i = 0; i != NumOps; ++i) {
5024 ISD::ArgFlagsTy Flags = Outs[i].Flags;
5025 EVT ArgVT = Outs[i].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005026 EVT OrigVT = Outs[i].ArgVT;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005027
Hal Finkel965cea52015-07-12 00:37:44 +00005028 if (Flags.isNest())
5029 continue;
5030
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005031 if (CallConv == CallingConv::Fast) {
5032 if (Flags.isByVal())
5033 NumGPRsUsed += (Flags.getByValSize()+7)/8;
5034 else
5035 switch (ArgVT.getSimpleVT().SimpleTy) {
5036 default: llvm_unreachable("Unexpected ValueType for argument!");
5037 case MVT::i1:
5038 case MVT::i32:
5039 case MVT::i64:
5040 if (++NumGPRsUsed <= NumGPRs)
5041 continue;
5042 break;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005043 case MVT::v4i32:
5044 case MVT::v8i16:
5045 case MVT::v16i8:
5046 case MVT::v2f64:
5047 case MVT::v2i64:
Kit Bartond4eb73c2015-05-05 16:10:44 +00005048 case MVT::v1i128:
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005049 if (++NumVRsUsed <= NumVRs)
5050 continue;
5051 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00005052 case MVT::v4f32:
NAKAMURA Takumi84965032015-09-22 11:14:12 +00005053 // When using QPX, this is handled like a FP register, otherwise, it
5054 // is an Altivec register.
Hal Finkelc93a9a22015-02-25 01:06:45 +00005055 if (Subtarget.hasQPX()) {
5056 if (++NumFPRsUsed <= NumFPRs)
5057 continue;
5058 } else {
5059 if (++NumVRsUsed <= NumVRs)
5060 continue;
5061 }
5062 break;
5063 case MVT::f32:
5064 case MVT::f64:
5065 case MVT::v4f64: // QPX
5066 case MVT::v4i1: // QPX
5067 if (++NumFPRsUsed <= NumFPRs)
5068 continue;
5069 break;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005070 }
5071 }
5072
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005073 /* Respect alignment of argument on the stack. */
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005074 unsigned Align =
5075 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005076 NumBytes = ((NumBytes + Align - 1) / Align) * Align;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005077
5078 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005079 if (Flags.isInConsecutiveRegsLast())
5080 NumBytes = ((NumBytes + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005081 }
5082
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005083 unsigned NumBytesActuallyUsed = NumBytes;
5084
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005085 // The prolog code of the callee may store up to 8 GPR argument registers to
5086 // the stack, allowing va_start to index over them in memory if its varargs.
5087 // Because we cannot tell if this is needed on the caller side, we have to
5088 // conservatively assume that it is needed. As such, make sure we have at
5089 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00005090 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005091 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005092
5093 // Tail call needs the stack to be aligned.
5094 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5095 CallConv == CallingConv::Fast)
Eric Christophercccae792015-01-30 22:02:31 +00005096 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005097
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005098 int SPDiff = 0;
5099
Bill Schmidt57d6de52012-10-23 15:51:16 +00005100 // Calculate by how many bytes the stack has to be adjusted in case of tail
5101 // call optimization.
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005102 if (!IsSibCall)
5103 SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005104
5105 // To protect arguments on the stack from being clobbered in a tail call,
5106 // force all the loads to happen before doing any other lowering.
5107 if (isTailCall)
5108 Chain = DAG.getStackArgumentTokenFactor(Chain);
5109
5110 // Adjust the stack pointer for the new arguments...
5111 // These operations are automatically eliminated by the prolog/epilog pass
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005112 if (!IsSibCall)
5113 Chain = DAG.getCALLSEQ_START(Chain,
5114 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005115 SDValue CallSeqStart = Chain;
5116
5117 // Load the return address and frame pointer so it can be move somewhere else
5118 // later.
5119 SDValue LROp, FPOp;
5120 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
5121 dl);
5122
5123 // Set up a copy of the stack pointer for use loading and storing any
5124 // arguments that may not fit in the registers available for argument
5125 // passing.
5126 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
5127
5128 // Figure out which arguments are going to go in registers, and which in
5129 // memory. Also, if this is a vararg function, floating point operations
5130 // must be stored to our stack, and loaded into integer regs as well, if
5131 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005132 unsigned ArgOffset = LinkageSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005133
5134 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
5135 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
5136
5137 SmallVector<SDValue, 8> MemOpChains;
5138 for (unsigned i = 0; i != NumOps; ++i) {
5139 SDValue Arg = OutVals[i];
5140 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005141 EVT ArgVT = Outs[i].VT;
5142 EVT OrigVT = Outs[i].ArgVT;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005143
5144 // PtrOff will be used to store the current argument to the stack if a
5145 // register cannot be found for it.
5146 SDValue PtrOff;
5147
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005148 // We re-align the argument offset for each argument, except when using the
5149 // fast calling convention, when we need to make sure we do that only when
5150 // we'll actually use a stack slot.
5151 auto ComputePtrOff = [&]() {
5152 /* Respect alignment of argument on the stack. */
5153 unsigned Align =
5154 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
5155 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005156
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005157 PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType());
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005158
5159 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
5160 };
5161
5162 if (CallConv != CallingConv::Fast) {
5163 ComputePtrOff();
5164
5165 /* Compute GPR index associated with argument offset. */
5166 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
5167 GPR_idx = std::min(GPR_idx, NumGPRs);
5168 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005169
5170 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00005171 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005172 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
5173 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
5174 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
5175 }
5176
5177 // FIXME memcpy is used way more than necessary. Correctness first.
5178 // Note: "by value" is code for passing a structure by value, not
5179 // basic types.
5180 if (Flags.isByVal()) {
5181 // Note: Size includes alignment padding, so
5182 // struct x { short a; char b; }
5183 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
5184 // These are the proper values we need for right-justifying the
5185 // aggregate in a parameter register.
5186 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00005187
5188 // An empty aggregate parameter takes up no storage and no
5189 // registers.
5190 if (Size == 0)
5191 continue;
5192
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005193 if (CallConv == CallingConv::Fast)
5194 ComputePtrOff();
5195
Bill Schmidt57d6de52012-10-23 15:51:16 +00005196 // All aggregates smaller than 8 bytes must be passed right-justified.
5197 if (Size==1 || Size==2 || Size==4) {
5198 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
5199 if (GPR_idx != NumGPRs) {
5200 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
5201 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00005202 false, false, false, 0);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005203 MemOpChains.push_back(Load.getValue(1));
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005204 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005205
5206 ArgOffset += PtrByteSize;
5207 continue;
5208 }
5209 }
5210
5211 if (GPR_idx == NumGPRs && Size < 8) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00005212 SDValue AddPtr = PtrOff;
5213 if (!isLittleEndian) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005214 SDValue Const = DAG.getConstant(PtrByteSize - Size, dl,
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00005215 PtrOff.getValueType());
5216 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
5217 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005218 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
5219 CallSeqStart,
5220 Flags, DAG, dl);
5221 ArgOffset += PtrByteSize;
5222 continue;
5223 }
5224 // Copy entire object into memory. There are cases where gcc-generated
5225 // code assumes it is there, even if it could be put entirely into
5226 // registers. (This is not what the doc says.)
5227
5228 // FIXME: The above statement is likely due to a misunderstanding of the
5229 // documents. All arguments must be copied into the parameter area BY
5230 // THE CALLEE in the event that the callee takes the address of any
5231 // formal argument. That has not yet been implemented. However, it is
5232 // reasonable to use the stack area as a staging area for the register
5233 // load.
5234
5235 // Skip this for small aggregates, as we will use the same slot for a
5236 // right-justified copy, below.
5237 if (Size >= 8)
5238 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
5239 CallSeqStart,
5240 Flags, DAG, dl);
5241
5242 // When a register is available, pass a small aggregate right-justified.
5243 if (Size < 8 && GPR_idx != NumGPRs) {
5244 // The easiest way to get this right-justified in a register
5245 // is to copy the structure into the rightmost portion of a
5246 // local variable slot, then load the whole slot into the
5247 // register.
5248 // FIXME: The memcpy seems to produce pretty awful code for
5249 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00005250 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00005251 // parameter save area instead of a new local variable.
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00005252 SDValue AddPtr = PtrOff;
5253 if (!isLittleEndian) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005254 SDValue Const = DAG.getConstant(8 - Size, dl, PtrOff.getValueType());
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00005255 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
5256 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005257 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
5258 CallSeqStart,
5259 Flags, DAG, dl);
5260
5261 // Load the slot into the register.
5262 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
5263 MachinePointerInfo(),
5264 false, false, false, 0);
5265 MemOpChains.push_back(Load.getValue(1));
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005266 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005267
5268 // Done with this argument.
5269 ArgOffset += PtrByteSize;
5270 continue;
5271 }
5272
5273 // For aggregates larger than PtrByteSize, copy the pieces of the
5274 // object that fit into registers from the parameter save area.
5275 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005276 SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType());
Bill Schmidt57d6de52012-10-23 15:51:16 +00005277 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
5278 if (GPR_idx != NumGPRs) {
5279 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
5280 MachinePointerInfo(),
5281 false, false, false, 0);
5282 MemOpChains.push_back(Load.getValue(1));
5283 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5284 ArgOffset += PtrByteSize;
5285 } else {
5286 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
5287 break;
5288 }
5289 }
5290 continue;
5291 }
5292
Craig Topper56710102013-08-15 02:33:50 +00005293 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005294 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00005295 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00005296 case MVT::i32:
5297 case MVT::i64:
Hal Finkel965cea52015-07-12 00:37:44 +00005298 if (Flags.isNest()) {
5299 // The 'nest' parameter, if any, is passed in R11.
5300 RegsToPass.push_back(std::make_pair(PPC::X11, Arg));
5301 hasNest = true;
5302 break;
5303 }
5304
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005305 // These can be scalar arguments or elements of an integer array type
5306 // passed directly. Clang may use those instead of "byval" aggregate
5307 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidt57d6de52012-10-23 15:51:16 +00005308 if (GPR_idx != NumGPRs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005309 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005310 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005311 if (CallConv == CallingConv::Fast)
5312 ComputePtrOff();
5313
Bill Schmidt57d6de52012-10-23 15:51:16 +00005314 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5315 true, isTailCall, false, MemOpChains,
5316 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005317 if (CallConv == CallingConv::Fast)
5318 ArgOffset += PtrByteSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005319 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005320 if (CallConv != CallingConv::Fast)
5321 ArgOffset += PtrByteSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005322 break;
5323 case MVT::f32:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005324 case MVT::f64: {
5325 // These can be scalar arguments or elements of a float array type
5326 // passed directly. The latter are used to implement ELFv2 homogenous
5327 // float aggregates.
5328
5329 // Named arguments go into FPRs first, and once they overflow, the
5330 // remaining arguments go into GPRs and then the parameter save area.
5331 // Unnamed arguments for vararg functions always go to GPRs and
5332 // then the parameter save area. For now, put all arguments to vararg
5333 // routines always in both locations (FPR *and* GPR or stack slot).
5334 bool NeedGPROrStack = isVarArg || FPR_idx == NumFPRs;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005335 bool NeededLoad = false;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005336
5337 // First load the argument into the next available FPR.
5338 if (FPR_idx != NumFPRs)
Bill Schmidt57d6de52012-10-23 15:51:16 +00005339 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
5340
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005341 // Next, load the argument into GPR or stack slot if needed.
5342 if (!NeedGPROrStack)
5343 ;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005344 else if (GPR_idx != NumGPRs && CallConv != CallingConv::Fast) {
Hal Finkel8ea446b2015-01-18 14:31:10 +00005345 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8
5346 // once we support fp <-> gpr moves.
5347
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005348 // In the non-vararg case, this can only ever happen in the
5349 // presence of f32 array types, since otherwise we never run
5350 // out of FPRs before running out of GPRs.
5351 SDValue ArgVal;
Bill Schmidtbd4ac262012-10-29 21:18:16 +00005352
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005353 // Double values are always passed in a single GPR.
5354 if (Arg.getValueType() != MVT::f32) {
5355 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005356
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005357 // Non-array float values are extended and passed in a GPR.
5358 } else if (!Flags.isInConsecutiveRegs()) {
5359 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
5360 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
5361
5362 // If we have an array of floats, we collect every odd element
5363 // together with its predecessor into one GPR.
5364 } else if (ArgOffset % PtrByteSize != 0) {
5365 SDValue Lo, Hi;
5366 Lo = DAG.getNode(ISD::BITCAST, dl, MVT::i32, OutVals[i - 1]);
5367 Hi = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
5368 if (!isLittleEndian)
5369 std::swap(Lo, Hi);
5370 ArgVal = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
5371
5372 // The final element, if even, goes into the first half of a GPR.
5373 } else if (Flags.isInConsecutiveRegsLast()) {
5374 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
5375 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
5376 if (!isLittleEndian)
5377 ArgVal = DAG.getNode(ISD::SHL, dl, MVT::i64, ArgVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005378 DAG.getConstant(32, dl, MVT::i32));
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005379
5380 // Non-final even elements are skipped; they will be handled
5381 // together the with subsequent argument on the next go-around.
5382 } else
5383 ArgVal = SDValue();
5384
5385 if (ArgVal.getNode())
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005386 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], ArgVal));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005387 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005388 if (CallConv == CallingConv::Fast)
5389 ComputePtrOff();
5390
Bill Schmidt57d6de52012-10-23 15:51:16 +00005391 // Single-precision floating-point values are mapped to the
5392 // second (rightmost) word of the stack doubleword.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005393 if (Arg.getValueType() == MVT::f32 &&
5394 !isLittleEndian && !Flags.isInConsecutiveRegs()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005395 SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType());
Bill Schmidt57d6de52012-10-23 15:51:16 +00005396 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
5397 }
5398
5399 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5400 true, isTailCall, false, MemOpChains,
5401 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005402
5403 NeededLoad = true;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005404 }
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005405 // When passing an array of floats, the array occupies consecutive
5406 // space in the argument area; only round up to the next doubleword
5407 // at the end of the array. Otherwise, each float takes 8 bytes.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005408 if (CallConv != CallingConv::Fast || NeededLoad) {
5409 ArgOffset += (Arg.getValueType() == MVT::f32 &&
5410 Flags.isInConsecutiveRegs()) ? 4 : 8;
5411 if (Flags.isInConsecutiveRegsLast())
5412 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
5413 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005414 break;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005415 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005416 case MVT::v4f32:
5417 case MVT::v4i32:
5418 case MVT::v8i16:
5419 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00005420 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00005421 case MVT::v2i64:
Kit Bartond4eb73c2015-05-05 16:10:44 +00005422 case MVT::v1i128:
Hal Finkelc93a9a22015-02-25 01:06:45 +00005423 if (!Subtarget.hasQPX()) {
Ulrich Weigand85d5df22014-07-21 00:13:26 +00005424 // These can be scalar arguments or elements of a vector array type
5425 // passed directly. The latter are used to implement ELFv2 homogenous
5426 // vector aggregates.
5427
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00005428 // For a varargs call, named arguments go into VRs or on the stack as
5429 // usual; unnamed arguments always go to the stack or the corresponding
5430 // GPRs when within range. For now, we always put the value in both
5431 // locations (or even all three).
Bill Schmidt57d6de52012-10-23 15:51:16 +00005432 if (isVarArg) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005433 // We could elide this store in the case where the object fits
5434 // entirely in R registers. Maybe later.
Bill Schmidt57d6de52012-10-23 15:51:16 +00005435 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5436 MachinePointerInfo(), false, false, 0);
5437 MemOpChains.push_back(Store);
5438 if (VR_idx != NumVRs) {
5439 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
5440 MachinePointerInfo(),
5441 false, false, false, 0);
5442 MemOpChains.push_back(Load.getValue(1));
Hal Finkel7811c612014-03-28 19:58:11 +00005443
5444 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
5445 Arg.getSimpleValueType() == MVT::v2i64) ?
5446 VSRH[VR_idx] : VR[VR_idx];
5447 ++VR_idx;
5448
5449 RegsToPass.push_back(std::make_pair(VReg, Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005450 }
5451 ArgOffset += 16;
5452 for (unsigned i=0; i<16; i+=PtrByteSize) {
5453 if (GPR_idx == NumGPRs)
5454 break;
5455 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005456 DAG.getConstant(i, dl, PtrVT));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005457 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
5458 false, false, false, 0);
5459 MemOpChains.push_back(Load.getValue(1));
5460 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5461 }
5462 break;
5463 }
5464
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00005465 // Non-varargs Altivec params go into VRs or on the stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00005466 if (VR_idx != NumVRs) {
Hal Finkel7811c612014-03-28 19:58:11 +00005467 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
5468 Arg.getSimpleValueType() == MVT::v2i64) ?
5469 VSRH[VR_idx] : VR[VR_idx];
5470 ++VR_idx;
5471
5472 RegsToPass.push_back(std::make_pair(VReg, Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005473 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005474 if (CallConv == CallingConv::Fast)
5475 ComputePtrOff();
5476
Bill Schmidt57d6de52012-10-23 15:51:16 +00005477 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5478 true, isTailCall, true, MemOpChains,
5479 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005480 if (CallConv == CallingConv::Fast)
5481 ArgOffset += 16;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005482 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005483
5484 if (CallConv != CallingConv::Fast)
5485 ArgOffset += 16;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005486 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00005487 } // not QPX
5488
5489 assert(Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32 &&
5490 "Invalid QPX parameter type");
5491
5492 /* fall through */
5493 case MVT::v4f64:
5494 case MVT::v4i1: {
5495 bool IsF32 = Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32;
5496 if (isVarArg) {
5497 // We could elide this store in the case where the object fits
5498 // entirely in R registers. Maybe later.
5499 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5500 MachinePointerInfo(), false, false, 0);
5501 MemOpChains.push_back(Store);
5502 if (QFPR_idx != NumQFPRs) {
5503 SDValue Load = DAG.getLoad(IsF32 ? MVT::v4f32 : MVT::v4f64, dl,
5504 Store, PtrOff, MachinePointerInfo(),
5505 false, false, false, 0);
5506 MemOpChains.push_back(Load.getValue(1));
5507 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Load));
5508 }
5509 ArgOffset += (IsF32 ? 16 : 32);
Aaron Ballman70c27de2015-02-25 13:02:23 +00005510 for (unsigned i = 0; i < (IsF32 ? 16U : 32U); i += PtrByteSize) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00005511 if (GPR_idx == NumGPRs)
5512 break;
5513 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005514 DAG.getConstant(i, dl, PtrVT));
Hal Finkelc93a9a22015-02-25 01:06:45 +00005515 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
5516 false, false, false, 0);
5517 MemOpChains.push_back(Load.getValue(1));
5518 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5519 }
5520 break;
5521 }
5522
5523 // Non-varargs QPX params go into registers or on the stack.
5524 if (QFPR_idx != NumQFPRs) {
5525 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Arg));
5526 } else {
5527 if (CallConv == CallingConv::Fast)
5528 ComputePtrOff();
5529
5530 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5531 true, isTailCall, true, MemOpChains,
5532 TailCallArguments, dl);
5533 if (CallConv == CallingConv::Fast)
5534 ArgOffset += (IsF32 ? 16 : 32);
5535 }
5536
5537 if (CallConv != CallingConv::Fast)
5538 ArgOffset += (IsF32 ? 16 : 32);
5539 break;
5540 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005541 }
5542 }
5543
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005544 assert(NumBytesActuallyUsed == ArgOffset);
Ulrich Weigandde8641b2014-07-07 19:39:44 +00005545 (void)NumBytesActuallyUsed;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005546
Bill Schmidt57d6de52012-10-23 15:51:16 +00005547 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00005548 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005549
5550 // Check if this is an indirect call (MTCTR/BCTRL).
5551 // See PrepareCall() for more information about calls through function
5552 // pointers in the 64-bit SVR4 ABI.
Hal Finkel934361a2015-01-14 01:07:51 +00005553 if (!isTailCall && !IsPatchPoint &&
Hal Finkel87deb0b2015-01-12 04:34:47 +00005554 !isFunctionGlobalAddress(Callee) &&
5555 !isa<ExternalSymbolSDNode>(Callee)) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005556 // Load r2 into a virtual register and store it to the TOC save area.
Hal Finkele6698d52015-02-01 15:03:28 +00005557 setUsesTOCBasePtr(DAG);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005558 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
5559 // TOC save area offset.
Eric Christopher736d39e2015-02-13 00:39:36 +00005560 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005561 SDValue PtrOff = DAG.getIntPtrConstant(TOCSaveOffset, dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005562 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00005563 Chain = DAG.getStore(
5564 Val.getValue(1), dl, Val, AddPtr,
5565 MachinePointerInfo::getStack(DAG.getMachineFunction(), TOCSaveOffset),
5566 false, false, 0);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00005567 // In the ELFv2 ABI, R12 must contain the address of an indirect callee.
5568 // This does not mean the MTCTR instruction must use R12; it's easier
5569 // to model this as an extra parameter, so do that.
Hal Finkel934361a2015-01-14 01:07:51 +00005570 if (isELFv2ABI && !IsPatchPoint)
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00005571 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005572 }
5573
5574 // Build a sequence of copy-to-reg nodes chained together with token chain
5575 // and flag operands which copy the outgoing args into the appropriate regs.
5576 SDValue InFlag;
5577 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
5578 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
5579 RegsToPass[i].second, InFlag);
5580 InFlag = Chain.getValue(1);
5581 }
5582
Chuang-Yu Cheng2e5973e2016-04-06 02:04:38 +00005583 if (isTailCall && !IsSibCall)
Bill Schmidt57d6de52012-10-23 15:51:16 +00005584 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
5585 FPOp, true, TailCallArguments);
5586
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005587 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint, hasNest,
5588 DAG, RegsToPass, InFlag, Chain, CallSeqStart, Callee,
5589 SPDiff, NumBytes, Ins, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005590}
5591
5592SDValue
5593PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
5594 CallingConv::ID CallConv, bool isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00005595 bool isTailCall, bool IsPatchPoint,
Bill Schmidt57d6de52012-10-23 15:51:16 +00005596 const SmallVectorImpl<ISD::OutputArg> &Outs,
5597 const SmallVectorImpl<SDValue> &OutVals,
5598 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005599 SDLoc dl, SelectionDAG &DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00005600 SmallVectorImpl<SDValue> &InVals,
5601 ImmutableCallSite *CS) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005602
5603 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005604
Mehdi Amini44ede332015-07-09 02:09:04 +00005605 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Owen Anderson9f944592009-08-11 20:47:22 +00005606 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005607 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005608
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005609 MachineFunction &MF = DAG.getMachineFunction();
5610
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005611 // Mark this function as potentially containing a function that contains a
5612 // tail call. As a consequence the frame pointer will be used for dynamicalloc
5613 // and restoring the callers stack pointer in this functions epilog. This is
5614 // done because by tail calling the called function might overwrite the value
5615 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00005616 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5617 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005618 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
5619
Chris Lattneraa40ec12006-05-16 22:56:08 +00005620 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00005621 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00005622 // prereserved space for [SP][CR][LR][3 x unused].
Eric Christophera4ae2132015-02-13 22:22:57 +00005623 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005624 unsigned NumBytes = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005625
5626 // Add up all the space actually used.
5627 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
5628 // they all go in registers, but we must reserve stack space for them for
5629 // possible use by the caller. In varargs or 64-bit calls, parameters are
5630 // assigned stack space in order, with padding so Altivec parameters are
5631 // 16-byte aligned.
5632 unsigned nAltivecParamsAtEnd = 0;
5633 for (unsigned i = 0; i != NumOps; ++i) {
5634 ISD::ArgFlagsTy Flags = Outs[i].Flags;
5635 EVT ArgVT = Outs[i].VT;
5636 // Varargs Altivec parameters are padded to a 16 byte boundary.
5637 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
5638 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
5639 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64) {
5640 if (!isVarArg && !isPPC64) {
5641 // Non-varargs Altivec parameters go after all the non-Altivec
5642 // parameters; handle those later so we know how much padding we need.
5643 nAltivecParamsAtEnd++;
5644 continue;
5645 }
5646 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
5647 NumBytes = ((NumBytes+15)/16)*16;
5648 }
5649 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
5650 }
5651
5652 // Allow for Altivec parameters at the end, if needed.
5653 if (nAltivecParamsAtEnd) {
5654 NumBytes = ((NumBytes+15)/16)*16;
5655 NumBytes += 16*nAltivecParamsAtEnd;
5656 }
5657
5658 // The prolog code of the callee may store up to 8 GPR argument registers to
5659 // the stack, allowing va_start to index over them in memory if its varargs.
5660 // Because we cannot tell if this is needed on the caller side, we have to
5661 // conservatively assume that it is needed. As such, make sure we have at
5662 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005663 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005664
5665 // Tail call needs the stack to be aligned.
5666 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5667 CallConv == CallingConv::Fast)
Eric Christophercccae792015-01-30 22:02:31 +00005668 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005669
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005670 // Calculate by how many bytes the stack has to be adjusted in case of tail
5671 // call optimization.
5672 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005673
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005674 // To protect arguments on the stack from being clobbered in a tail call,
5675 // force all the loads to happen before doing any other lowering.
5676 if (isTailCall)
5677 Chain = DAG.getStackArgumentTokenFactor(Chain);
5678
Chris Lattnerb7552a82006-05-17 00:15:40 +00005679 // Adjust the stack pointer for the new arguments...
5680 // These operations are automatically eliminated by the prolog/epilog pass
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005681 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00005682 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005683 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005684
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005685 // Load the return address and frame pointer so it can be move somewhere else
5686 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005687 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00005688 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
5689 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005690
Chris Lattnerb7552a82006-05-17 00:15:40 +00005691 // Set up a copy of the stack pointer for use loading and storing any
5692 // arguments that may not fit in the registers available for argument
5693 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005694 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005695 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00005696 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005697 else
Owen Anderson9f944592009-08-11 20:47:22 +00005698 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005699
Chris Lattnerb7552a82006-05-17 00:15:40 +00005700 // Figure out which arguments are going to go in registers, and which in
5701 // memory. Also, if this is a vararg function, floating point operations
5702 // must be stored to our stack, and loaded into integer regs as well, if
5703 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005704 unsigned ArgOffset = LinkageSize;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005705 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005706
Craig Topper840beec2014-04-04 05:16:06 +00005707 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005708 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
5709 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
5710 };
Craig Topper840beec2014-04-04 05:16:06 +00005711 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00005712 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
5713 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
5714 };
Craig Topper840beec2014-04-04 05:16:06 +00005715 static const MCPhysReg VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005716 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
5717 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
5718 };
Owen Andersone2f23a32007-09-07 04:06:50 +00005719 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005720 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00005721 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005722
Craig Topper840beec2014-04-04 05:16:06 +00005723 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005724
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005725 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005726 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
5727
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005728 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00005729 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005730 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005731 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00005732
Chris Lattnerb7552a82006-05-17 00:15:40 +00005733 // PtrOff will be used to store the current argument to the stack if a
5734 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005735 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005736
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005737 PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00005738
Dale Johannesen679073b2009-02-04 02:34:38 +00005739 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005740
5741 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00005742 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00005743 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
5744 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00005745 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005746 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00005747
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005748 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00005749 // Note: "by value" is code for passing a structure by value, not
5750 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00005751 if (Flags.isByVal()) {
5752 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00005753 // Very small objects are passed right-justified. Everything else is
5754 // passed left-justified.
5755 if (Size==1 || Size==2) {
5756 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005757 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00005758 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00005759 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00005760 false, false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005761 MemOpChains.push_back(Load.getValue(1));
5762 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005763
5764 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005765 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005766 SDValue Const = DAG.getConstant(PtrByteSize - Size, dl,
Bill Schmidt48081ca2012-10-16 13:30:53 +00005767 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005768 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005769 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
5770 CallSeqStart,
5771 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005772 ArgOffset += PtrByteSize;
5773 }
5774 continue;
5775 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00005776 // Copy entire object into memory. There are cases where gcc-generated
5777 // code assumes it is there, even if it could be put entirely into
5778 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00005779 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
5780 CallSeqStart,
5781 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00005782
5783 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
5784 // copy the pieces of the object that fit into registers from the
5785 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00005786 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005787 SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005788 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00005789 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00005790 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
5791 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005792 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00005793 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00005794 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005795 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00005796 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00005797 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005798 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00005799 }
5800 }
5801 continue;
5802 }
5803
Craig Topper56710102013-08-15 02:33:50 +00005804 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005805 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00005806 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00005807 case MVT::i32:
5808 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005809 if (GPR_idx != NumGPRs) {
Hal Finkel7f908e82014-03-06 00:45:19 +00005810 if (Arg.getValueType() == MVT::i1)
5811 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg);
5812
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005813 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00005814 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005815 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5816 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005817 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00005818 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005819 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00005820 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005821 case MVT::f32:
5822 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005823 if (FPR_idx != NumFPRs) {
5824 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
5825
Chris Lattnerb7552a82006-05-17 00:15:40 +00005826 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00005827 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5828 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005829 MemOpChains.push_back(Store);
5830
Chris Lattnerb7552a82006-05-17 00:15:40 +00005831 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005832 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00005833 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00005834 MachinePointerInfo(), false, false,
5835 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005836 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005837 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00005838 }
Owen Anderson9f944592009-08-11 20:47:22 +00005839 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005840 SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005841 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00005842 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
5843 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005844 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005845 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005846 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00005847 }
5848 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00005849 // If we have any FPRs remaining, we may also have GPRs remaining.
5850 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
5851 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005852 if (GPR_idx != NumGPRs)
5853 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00005854 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005855 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
5856 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00005857 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005858 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005859 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5860 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005861 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005862 if (isPPC64)
5863 ArgOffset += 8;
5864 else
Owen Anderson9f944592009-08-11 20:47:22 +00005865 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00005866 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005867 case MVT::v4f32:
5868 case MVT::v4i32:
5869 case MVT::v8i16:
5870 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00005871 if (isVarArg) {
5872 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00005873 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00005874 // V registers; in fact gcc does this only for arguments that are
5875 // prototyped, not for those that match the ... We do it for all
5876 // arguments, seems to work.
5877 while (ArgOffset % 16 !=0) {
5878 ArgOffset += PtrByteSize;
5879 if (GPR_idx != NumGPRs)
5880 GPR_idx++;
5881 }
5882 // We could elide this store in the case where the object fits
5883 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005884 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005885 DAG.getConstant(ArgOffset, dl, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00005886 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5887 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005888 MemOpChains.push_back(Store);
5889 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00005890 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00005891 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005892 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005893 MemOpChains.push_back(Load.getValue(1));
5894 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
5895 }
5896 ArgOffset += 16;
5897 for (unsigned i=0; i<16; i+=PtrByteSize) {
5898 if (GPR_idx == NumGPRs)
5899 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00005900 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005901 DAG.getConstant(i, dl, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00005902 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005903 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005904 MemOpChains.push_back(Load.getValue(1));
5905 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5906 }
5907 break;
5908 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005909
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005910 // Non-varargs Altivec params generally go in registers, but have
5911 // stack space allocated at the end.
5912 if (VR_idx != NumVRs) {
5913 // Doesn't have GPR space allocated.
5914 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
5915 } else if (nAltivecParamsAtEnd==0) {
5916 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005917 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5918 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005919 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005920 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00005921 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00005922 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00005923 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00005924 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005925 // If all Altivec parameters fit in registers, as they usually do,
5926 // they get stack space following the non-Altivec parameters. We
5927 // don't track this here because nobody below needs it.
5928 // If there are more Altivec parameters than fit in registers emit
5929 // the stores here.
5930 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
5931 unsigned j = 0;
5932 // Offset is aligned; skip 1st 12 params which go in V registers.
5933 ArgOffset = ((ArgOffset+15)/16)*16;
5934 ArgOffset += 12*16;
5935 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005936 SDValue Arg = OutVals[i];
5937 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00005938 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
5939 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005940 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005941 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005942 // We are emitting Altivec params in order.
5943 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5944 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005945 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005946 ArgOffset += 16;
5947 }
5948 }
5949 }
5950 }
5951
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005952 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00005953 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005954
Dale Johannesen90eab672010-03-09 20:15:42 +00005955 // On Darwin, R12 must contain the address of an indirect callee. This does
5956 // not mean the MTCTR instruction must use R12; it's easier to model this as
5957 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00005958 if (!isTailCall &&
Hal Finkel87deb0b2015-01-12 04:34:47 +00005959 !isFunctionGlobalAddress(Callee) &&
5960 !isa<ExternalSymbolSDNode>(Callee) &&
Dale Johannesen90eab672010-03-09 20:15:42 +00005961 !isBLACompatibleAddress(Callee, DAG))
5962 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
5963 PPC::R12), Callee));
5964
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005965 // Build a sequence of copy-to-reg nodes chained together with token chain
5966 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005967 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005968 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00005969 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00005970 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005971 InFlag = Chain.getValue(1);
5972 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005973
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00005974 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005975 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
5976 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005977
Hal Finkel965cea52015-07-12 00:37:44 +00005978 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint,
5979 /* unused except on PPC64 ELFv1 */ false, DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00005980 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff,
5981 NumBytes, Ins, InVals, CS);
Chris Lattneraa40ec12006-05-16 22:56:08 +00005982}
5983
Hal Finkel450128a2011-10-14 19:51:36 +00005984bool
5985PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
5986 MachineFunction &MF, bool isVarArg,
5987 const SmallVectorImpl<ISD::OutputArg> &Outs,
5988 LLVMContext &Context) const {
5989 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00005990 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Hal Finkel450128a2011-10-14 19:51:36 +00005991 return CCInfo.CheckReturn(Outs, RetCC_PPC);
5992}
5993
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005994SDValue
5995PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00005996 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005997 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005998 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005999 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00006000
Chris Lattner4f2e4e02007-03-06 00:59:59 +00006001 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00006002 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
6003 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00006004 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006005
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006006 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00006007 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006008
Chris Lattner4f2e4e02007-03-06 00:59:59 +00006009 // Copy the result values into the output registers.
6010 for (unsigned i = 0; i != RVLocs.size(); ++i) {
6011 CCValAssign &VA = RVLocs[i];
6012 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00006013
6014 SDValue Arg = OutVals[i];
6015
6016 switch (VA.getLocInfo()) {
6017 default: llvm_unreachable("Unknown loc info!");
6018 case CCValAssign::Full: break;
6019 case CCValAssign::AExt:
6020 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
6021 break;
6022 case CCValAssign::ZExt:
6023 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
6024 break;
6025 case CCValAssign::SExt:
6026 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
6027 break;
6028 }
6029
6030 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00006031 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00006032 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00006033 }
6034
Chuang-Yu Cheng98c18942016-04-08 12:04:32 +00006035 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo();
6036 const MCPhysReg *I =
6037 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
6038 if (I) {
6039 for (; *I; ++I) {
6040
6041 if (PPC::G8RCRegClass.contains(*I))
6042 RetOps.push_back(DAG.getRegister(*I, MVT::i64));
6043 else if (PPC::F8RCRegClass.contains(*I))
6044 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64)));
6045 else if (PPC::CRRCRegClass.contains(*I))
6046 RetOps.push_back(DAG.getRegister(*I, MVT::i1));
6047 else if (PPC::VRRCRegClass.contains(*I))
6048 RetOps.push_back(DAG.getRegister(*I, MVT::Other));
6049 else
6050 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
6051 }
6052 }
6053
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00006054 RetOps[0] = Chain; // Update chain.
6055
6056 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006057 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00006058 RetOps.push_back(Flag);
6059
Craig Topper48d114b2014-04-26 18:35:24 +00006060 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps);
Chris Lattner4211ca92006-04-14 06:01:58 +00006061}
6062
Yury Gribovd7dbb662015-12-01 11:40:55 +00006063SDValue PPCTargetLowering::LowerGET_DYNAMIC_AREA_OFFSET(
6064 SDValue Op, SelectionDAG &DAG, const PPCSubtarget &Subtarget) const {
6065 SDLoc dl(Op);
6066
6067 // Get the corect type for integers.
6068 EVT IntVT = Op.getValueType();
6069
6070 // Get the inputs.
6071 SDValue Chain = Op.getOperand(0);
6072 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
6073 // Build a DYNAREAOFFSET node.
6074 SDValue Ops[2] = {Chain, FPSIdx};
6075 SDVTList VTs = DAG.getVTList(IntVT);
6076 return DAG.getNode(PPCISD::DYNAREAOFFSET, dl, VTs, Ops);
6077}
6078
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006079SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006080 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00006081 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006082 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006083
Jim Laskeye4f4d042006-12-04 22:04:42 +00006084 // Get the corect type for pointers.
Mehdi Amini44ede332015-07-09 02:09:04 +00006085 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Jim Laskeye4f4d042006-12-04 22:04:42 +00006086
6087 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00006088 bool isPPC64 = Subtarget.isPPC64();
6089 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006090 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00006091
6092 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006093 SDValue Chain = Op.getOperand(0);
6094 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006095
Jim Laskeye4f4d042006-12-04 22:04:42 +00006096 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00006097 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
6098 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006099 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006100
Jim Laskeye4f4d042006-12-04 22:04:42 +00006101 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00006102 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006103
Jim Laskeye4f4d042006-12-04 22:04:42 +00006104 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00006105 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00006106 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00006107}
6108
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00006109SDValue PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG &DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00006110 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006111 bool isPPC64 = Subtarget.isPPC64();
Mehdi Amini44ede332015-07-09 02:09:04 +00006112 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006113
6114 // Get current frame pointer save index. The users of this index will be
6115 // primarily DYNALLOC instructions.
6116 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
6117 int RASI = FI->getReturnAddrSaveIndex();
6118
6119 // If the frame pointer save index hasn't been defined yet.
6120 if (!RASI) {
6121 // Find out what the fix offset of the frame pointer save area.
Eric Christopherf71609b2015-02-13 00:39:27 +00006122 int LROffset = Subtarget.getFrameLowering()->getReturnSaveOffset();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006123 // Allocate the frame index for frame pointer save area.
Hal Finkel6e27c6d2014-12-23 09:45:06 +00006124 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, false);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006125 // Save the result.
6126 FI->setReturnAddrSaveIndex(RASI);
6127 }
6128 return DAG.getFrameIndex(RASI, PtrVT);
6129}
6130
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006131SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006132PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
6133 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006134 bool isPPC64 = Subtarget.isPPC64();
Mehdi Amini44ede332015-07-09 02:09:04 +00006135 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Jim Laskey48850c12006-11-16 22:43:37 +00006136
6137 // Get current frame pointer save index. The users of this index will be
6138 // primarily DYNALLOC instructions.
6139 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
6140 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006141
Jim Laskey48850c12006-11-16 22:43:37 +00006142 // If the frame pointer save index hasn't been defined yet.
6143 if (!FPSI) {
6144 // Find out what the fix offset of the frame pointer save area.
Eric Christopherdc3a8a42015-02-13 00:39:38 +00006145 int FPOffset = Subtarget.getFrameLowering()->getFramePointerSaveOffset();
Jim Laskey48850c12006-11-16 22:43:37 +00006146 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00006147 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00006148 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006149 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00006150 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006151 return DAG.getFrameIndex(FPSI, PtrVT);
6152}
Jim Laskey48850c12006-11-16 22:43:37 +00006153
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006154SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00006155 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006156 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00006157 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006158 SDValue Chain = Op.getOperand(0);
6159 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006160 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006161
Jim Laskey48850c12006-11-16 22:43:37 +00006162 // Get the corect type for pointers.
Mehdi Amini44ede332015-07-09 02:09:04 +00006163 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Jim Laskey48850c12006-11-16 22:43:37 +00006164 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00006165 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006166 DAG.getConstant(0, dl, PtrVT), Size);
Jim Laskey48850c12006-11-16 22:43:37 +00006167 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006168 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00006169 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006170 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00006171 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Craig Topper48d114b2014-04-26 18:35:24 +00006172 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops);
Jim Laskey48850c12006-11-16 22:43:37 +00006173}
6174
Hal Finkel756810f2013-03-21 21:37:52 +00006175SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
6176 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006177 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00006178 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
6179 DAG.getVTList(MVT::i32, MVT::Other),
6180 Op.getOperand(0), Op.getOperand(1));
6181}
6182
6183SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
6184 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006185 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00006186 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
6187 Op.getOperand(0), Op.getOperand(1));
6188}
6189
Hal Finkel940ab932014-02-28 00:27:01 +00006190SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +00006191 if (Op.getValueType().isVector())
6192 return LowerVectorLoad(Op, DAG);
6193
Hal Finkel940ab932014-02-28 00:27:01 +00006194 assert(Op.getValueType() == MVT::i1 &&
6195 "Custom lowering only for i1 loads");
6196
6197 // First, load 8 bits into 32 bits, then truncate to 1 bit.
6198
6199 SDLoc dl(Op);
6200 LoadSDNode *LD = cast<LoadSDNode>(Op);
6201
6202 SDValue Chain = LD->getChain();
6203 SDValue BasePtr = LD->getBasePtr();
6204 MachineMemOperand *MMO = LD->getMemOperand();
6205
Mehdi Amini44ede332015-07-09 02:09:04 +00006206 SDValue NewLD =
6207 DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(DAG.getDataLayout()), Chain,
6208 BasePtr, MVT::i8, MMO);
Hal Finkel940ab932014-02-28 00:27:01 +00006209 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
6210
6211 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00006212 return DAG.getMergeValues(Ops, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00006213}
6214
6215SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +00006216 if (Op.getOperand(1).getValueType().isVector())
6217 return LowerVectorStore(Op, DAG);
6218
Hal Finkel940ab932014-02-28 00:27:01 +00006219 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
6220 "Custom lowering only for i1 stores");
6221
6222 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
6223
6224 SDLoc dl(Op);
6225 StoreSDNode *ST = cast<StoreSDNode>(Op);
6226
6227 SDValue Chain = ST->getChain();
6228 SDValue BasePtr = ST->getBasePtr();
6229 SDValue Value = ST->getValue();
6230 MachineMemOperand *MMO = ST->getMemOperand();
6231
Mehdi Amini44ede332015-07-09 02:09:04 +00006232 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(DAG.getDataLayout()),
6233 Value);
Hal Finkel940ab932014-02-28 00:27:01 +00006234 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
6235}
6236
6237// FIXME: Remove this once the ANDI glue bug is fixed:
6238SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
6239 assert(Op.getValueType() == MVT::i1 &&
6240 "Custom lowering only for i1 results");
6241
6242 SDLoc DL(Op);
6243 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
6244 Op.getOperand(0));
6245}
6246
Chris Lattner4211ca92006-04-14 06:01:58 +00006247/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
6248/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00006249SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00006250 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00006251 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
6252 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00006253 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006254
Hal Finkel81f87992013-04-07 22:11:09 +00006255 // We might be able to do better than this under some circumstances, but in
6256 // general, fsel-based lowering of select is a finite-math-only optimization.
6257 // For more information, see section F.3 of the 2.06 ISA specification.
6258 if (!DAG.getTarget().Options.NoInfsFPMath ||
6259 !DAG.getTarget().Options.NoNaNsFPMath)
6260 return Op;
Sanjay Patela2607012015-09-16 16:31:21 +00006261 // TODO: Propagate flags from the select rather than global settings.
6262 SDNodeFlags Flags;
6263 Flags.setNoInfs(true);
6264 Flags.setNoNaNs(true);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +00006265
Hal Finkel81f87992013-04-07 22:11:09 +00006266 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006267
Owen Anderson53aa7a92009-08-10 22:56:29 +00006268 EVT ResVT = Op.getValueType();
6269 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006270 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
6271 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006272 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006273
Chris Lattner4211ca92006-04-14 06:01:58 +00006274 // If the RHS of the comparison is a 0.0, we don't need to do the
6275 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00006276 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00006277 if (isFloatingPointZero(RHS))
6278 switch (CC) {
6279 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00006280 case ISD::SETNE:
6281 std::swap(TV, FV);
6282 case ISD::SETEQ:
6283 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
6284 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
6285 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
6286 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
6287 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
6288 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
6289 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006290 case ISD::SETULT:
6291 case ISD::SETLT:
6292 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00006293 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00006294 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00006295 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
6296 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00006297 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006298 case ISD::SETUGT:
6299 case ISD::SETGT:
6300 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00006301 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00006302 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00006303 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
6304 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00006305 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00006306 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006307 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006308
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006309 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00006310 switch (CC) {
6311 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00006312 case ISD::SETNE:
6313 std::swap(TV, FV);
6314 case ISD::SETEQ:
Sanjay Patela2607012015-09-16 16:31:21 +00006315 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, &Flags);
Hal Finkel81f87992013-04-07 22:11:09 +00006316 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6317 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
6318 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
6319 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
6320 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
6321 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
6322 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006323 case ISD::SETULT:
6324 case ISD::SETLT:
Sanjay Patela2607012015-09-16 16:31:21 +00006325 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, &Flags);
Owen Anderson9f944592009-08-11 20:47:22 +00006326 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6327 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00006328 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00006329 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00006330 case ISD::SETGE:
Sanjay Patela2607012015-09-16 16:31:21 +00006331 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, &Flags);
Owen Anderson9f944592009-08-11 20:47:22 +00006332 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6333 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00006334 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006335 case ISD::SETUGT:
6336 case ISD::SETGT:
Sanjay Patela2607012015-09-16 16:31:21 +00006337 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS, &Flags);
Owen Anderson9f944592009-08-11 20:47:22 +00006338 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6339 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00006340 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00006341 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00006342 case ISD::SETLE:
Sanjay Patela2607012015-09-16 16:31:21 +00006343 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS, &Flags);
Owen Anderson9f944592009-08-11 20:47:22 +00006344 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
6345 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00006346 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00006347 }
Eli Friedman5806e182009-05-28 04:31:08 +00006348 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00006349}
6350
Hal Finkeled844c42015-01-06 22:31:02 +00006351void PPCTargetLowering::LowerFP_TO_INTForReuse(SDValue Op, ReuseLoadInfo &RLI,
6352 SelectionDAG &DAG,
6353 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00006354 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006355 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00006356 if (Src.getValueType() == MVT::f32)
6357 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00006358
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006359 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00006360 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006361 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00006362 case MVT::i32:
Eric Christophercccae792015-01-30 22:02:31 +00006363 Tmp = DAG.getNode(
6364 Op.getOpcode() == ISD::FP_TO_SINT
6365 ? PPCISD::FCTIWZ
6366 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ),
6367 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00006368 break;
Owen Anderson9f944592009-08-11 20:47:22 +00006369 case MVT::i64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006370 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) &&
Hal Finkel3f88d082013-04-01 18:42:58 +00006371 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00006372 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
6373 PPCISD::FCTIDUZ,
6374 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00006375 break;
6376 }
Duncan Sands2a287912008-07-19 16:26:02 +00006377
Chris Lattner4211ca92006-04-14 06:01:58 +00006378 // Convert the FP value to an int value through memory.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006379 bool i32Stack = Op.getValueType() == MVT::i32 && Subtarget.hasSTFIWX() &&
6380 (Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT());
Hal Finkelf6d45f22013-04-01 17:52:07 +00006381 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
6382 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
Alex Lorenze40c8a22015-08-11 23:09:45 +00006383 MachinePointerInfo MPI =
6384 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
Duncan Sands2a287912008-07-19 16:26:02 +00006385
Chris Lattner06a49542007-10-15 20:14:52 +00006386 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00006387 SDValue Chain;
6388 if (i32Stack) {
6389 MachineFunction &MF = DAG.getMachineFunction();
6390 MachineMemOperand *MMO =
6391 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
6392 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
6393 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00006394 DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO);
Hal Finkelf6d45f22013-04-01 17:52:07 +00006395 } else
6396 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
6397 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00006398
6399 // Result is a load from the stack slot. If loading 4 bytes, make sure to
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00006400 // add in a bias on big endian.
Hal Finkelf6d45f22013-04-01 17:52:07 +00006401 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00006402 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006403 DAG.getConstant(4, dl, FIPtr.getValueType()));
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00006404 MPI = MPI.getWithOffset(Subtarget.isLittleEndian() ? 0 : 4);
Hal Finkelf6d45f22013-04-01 17:52:07 +00006405 }
6406
Hal Finkeled844c42015-01-06 22:31:02 +00006407 RLI.Chain = Chain;
6408 RLI.Ptr = FIPtr;
6409 RLI.MPI = MPI;
6410}
6411
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006412/// \brief Custom lowers floating point to integer conversions to use
6413/// the direct move instructions available in ISA 2.07 to avoid the
6414/// need for load/store combinations.
6415SDValue PPCTargetLowering::LowerFP_TO_INTDirectMove(SDValue Op,
6416 SelectionDAG &DAG,
6417 SDLoc dl) const {
6418 assert(Op.getOperand(0).getValueType().isFloatingPoint());
6419 SDValue Src = Op.getOperand(0);
6420
6421 if (Src.getValueType() == MVT::f32)
6422 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
6423
6424 SDValue Tmp;
6425 switch (Op.getSimpleValueType().SimpleTy) {
6426 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
6427 case MVT::i32:
6428 Tmp = DAG.getNode(
6429 Op.getOpcode() == ISD::FP_TO_SINT
6430 ? PPCISD::FCTIWZ
6431 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ),
6432 dl, MVT::f64, Src);
6433 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i32, Tmp);
6434 break;
6435 case MVT::i64:
6436 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) &&
6437 "i64 FP_TO_UINT is supported only with FPCVT");
6438 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
6439 PPCISD::FCTIDUZ,
6440 dl, MVT::f64, Src);
6441 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i64, Tmp);
6442 break;
6443 }
6444 return Tmp;
6445}
6446
Hal Finkeled844c42015-01-06 22:31:02 +00006447SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
6448 SDLoc dl) const {
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006449 if (Subtarget.hasDirectMove() && Subtarget.isPPC64())
6450 return LowerFP_TO_INTDirectMove(Op, DAG, dl);
6451
Hal Finkeled844c42015-01-06 22:31:02 +00006452 ReuseLoadInfo RLI;
6453 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl);
6454
6455 return DAG.getLoad(Op.getValueType(), dl, RLI.Chain, RLI.Ptr, RLI.MPI, false,
6456 false, RLI.IsInvariant, RLI.Alignment, RLI.AAInfo,
6457 RLI.Ranges);
6458}
6459
6460// We're trying to insert a regular store, S, and then a load, L. If the
6461// incoming value, O, is a load, we might just be able to have our load use the
6462// address used by O. However, we don't know if anything else will store to
6463// that address before we can load from it. To prevent this situation, we need
6464// to insert our load, L, into the chain as a peer of O. To do this, we give L
6465// the same chain operand as O, we create a token factor from the chain results
6466// of O and L, and we replace all uses of O's chain result with that token
6467// factor (see spliceIntoChain below for this last part).
6468bool PPCTargetLowering::canReuseLoadAddress(SDValue Op, EVT MemVT,
6469 ReuseLoadInfo &RLI,
Hal Finkel6c392692015-01-09 01:34:30 +00006470 SelectionDAG &DAG,
6471 ISD::LoadExtType ET) const {
Hal Finkeled844c42015-01-06 22:31:02 +00006472 SDLoc dl(Op);
Hal Finkel6c392692015-01-09 01:34:30 +00006473 if (ET == ISD::NON_EXTLOAD &&
6474 (Op.getOpcode() == ISD::FP_TO_UINT ||
Hal Finkeled844c42015-01-06 22:31:02 +00006475 Op.getOpcode() == ISD::FP_TO_SINT) &&
6476 isOperationLegalOrCustom(Op.getOpcode(),
6477 Op.getOperand(0).getValueType())) {
6478
6479 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl);
6480 return true;
6481 }
6482
6483 LoadSDNode *LD = dyn_cast<LoadSDNode>(Op);
Hal Finkel6c392692015-01-09 01:34:30 +00006484 if (!LD || LD->getExtensionType() != ET || LD->isVolatile() ||
6485 LD->isNonTemporal())
Hal Finkeled844c42015-01-06 22:31:02 +00006486 return false;
6487 if (LD->getMemoryVT() != MemVT)
6488 return false;
6489
6490 RLI.Ptr = LD->getBasePtr();
Sanjay Patel75068522016-03-14 18:09:43 +00006491 if (LD->isIndexed() && !LD->getOffset().isUndef()) {
Hal Finkeled844c42015-01-06 22:31:02 +00006492 assert(LD->getAddressingMode() == ISD::PRE_INC &&
6493 "Non-pre-inc AM on PPC?");
6494 RLI.Ptr = DAG.getNode(ISD::ADD, dl, RLI.Ptr.getValueType(), RLI.Ptr,
6495 LD->getOffset());
6496 }
6497
6498 RLI.Chain = LD->getChain();
6499 RLI.MPI = LD->getPointerInfo();
6500 RLI.IsInvariant = LD->isInvariant();
6501 RLI.Alignment = LD->getAlignment();
6502 RLI.AAInfo = LD->getAAInfo();
6503 RLI.Ranges = LD->getRanges();
6504
6505 RLI.ResChain = SDValue(LD, LD->isIndexed() ? 2 : 1);
6506 return true;
6507}
6508
6509// Given the head of the old chain, ResChain, insert a token factor containing
6510// it and NewResChain, and make users of ResChain now be users of that token
6511// factor.
6512void PPCTargetLowering::spliceIntoChain(SDValue ResChain,
6513 SDValue NewResChain,
6514 SelectionDAG &DAG) const {
6515 if (!ResChain)
6516 return;
6517
6518 SDLoc dl(NewResChain);
6519
6520 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
6521 NewResChain, DAG.getUNDEF(MVT::Other));
6522 assert(TF.getNode() != NewResChain.getNode() &&
6523 "A new TF really is required here");
6524
6525 DAG.ReplaceAllUsesOfValueWith(ResChain, TF);
6526 DAG.UpdateNodeOperands(TF.getNode(), ResChain, NewResChain);
Chris Lattner4211ca92006-04-14 06:01:58 +00006527}
6528
Ehsan Amiri322eca32016-04-06 20:12:29 +00006529/// \brief Analyze profitability of direct move
6530/// prefer float load to int load plus direct move
6531/// when there is no integer use of int load
6532static bool directMoveIsProfitable(const SDValue &Op) {
6533 SDNode *Origin = Op.getOperand(0).getNode();
6534 if (Origin->getOpcode() != ISD::LOAD)
6535 return true;
6536
6537 for (SDNode::use_iterator UI = Origin->use_begin(),
6538 UE = Origin->use_end();
6539 UI != UE; ++UI) {
6540
6541 // Only look at the users of the loaded value.
6542 if (UI.getUse().get().getResNo() != 0)
6543 continue;
6544
6545 if (UI->getOpcode() != ISD::SINT_TO_FP &&
6546 UI->getOpcode() != ISD::UINT_TO_FP)
6547 return true;
6548 }
6549
6550 return false;
6551}
6552
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006553/// \brief Custom lowers integer to floating point conversions to use
6554/// the direct move instructions available in ISA 2.07 to avoid the
6555/// need for load/store combinations.
6556SDValue PPCTargetLowering::LowerINT_TO_FPDirectMove(SDValue Op,
6557 SelectionDAG &DAG,
6558 SDLoc dl) const {
6559 assert((Op.getValueType() == MVT::f32 ||
6560 Op.getValueType() == MVT::f64) &&
6561 "Invalid floating point type as target of conversion");
6562 assert(Subtarget.hasFPCVT() &&
6563 "Int to FP conversions with direct moves require FPCVT");
6564 SDValue FP;
6565 SDValue Src = Op.getOperand(0);
6566 bool SinglePrec = Op.getValueType() == MVT::f32;
6567 bool WordInt = Src.getSimpleValueType().SimpleTy == MVT::i32;
6568 bool Signed = Op.getOpcode() == ISD::SINT_TO_FP;
6569 unsigned ConvOp = Signed ? (SinglePrec ? PPCISD::FCFIDS : PPCISD::FCFID) :
6570 (SinglePrec ? PPCISD::FCFIDUS : PPCISD::FCFIDU);
6571
6572 if (WordInt) {
6573 FP = DAG.getNode(Signed ? PPCISD::MTVSRA : PPCISD::MTVSRZ,
6574 dl, MVT::f64, Src);
6575 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP);
6576 }
6577 else {
6578 FP = DAG.getNode(PPCISD::MTVSRA, dl, MVT::f64, Src);
6579 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP);
6580 }
6581
6582 return FP;
6583}
6584
Hal Finkelf6d45f22013-04-01 17:52:07 +00006585SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Hal Finkeled844c42015-01-06 22:31:02 +00006586 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006587 SDLoc dl(Op);
Hal Finkelc93a9a22015-02-25 01:06:45 +00006588
6589 if (Subtarget.hasQPX() && Op.getOperand(0).getValueType() == MVT::v4i1) {
6590 if (Op.getValueType() != MVT::v4f32 && Op.getValueType() != MVT::v4f64)
6591 return SDValue();
6592
6593 SDValue Value = Op.getOperand(0);
6594 // The values are now known to be -1 (false) or 1 (true). To convert this
6595 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
6596 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
6597 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +00006598
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006599 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64);
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00006600
Hal Finkelc93a9a22015-02-25 01:06:45 +00006601 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
6602
6603 if (Op.getValueType() != MVT::v4f64)
6604 Value = DAG.getNode(ISD::FP_ROUND, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006605 Op.getValueType(), Value,
6606 DAG.getIntPtrConstant(1, dl));
Hal Finkelc93a9a22015-02-25 01:06:45 +00006607 return Value;
6608 }
6609
Dan Gohmand6819da2008-03-11 01:59:03 +00006610 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00006611 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006612 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00006613
Hal Finkel6a56b212014-03-05 22:14:00 +00006614 if (Op.getOperand(0).getValueType() == MVT::i1)
6615 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006616 DAG.getConstantFP(1.0, dl, Op.getValueType()),
6617 DAG.getConstantFP(0.0, dl, Op.getValueType()));
Hal Finkel6a56b212014-03-05 22:14:00 +00006618
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006619 // If we have direct moves, we can do all the conversion, skip the store/load
6620 // however, without FPCVT we can't do most conversions.
Ehsan Amiri322eca32016-04-06 20:12:29 +00006621 if (Subtarget.hasDirectMove() && directMoveIsProfitable(Op) &&
6622 Subtarget.isPPC64() && Subtarget.hasFPCVT())
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006623 return LowerINT_TO_FPDirectMove(Op, DAG, dl);
6624
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006625 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006626 "UINT_TO_FP is supported only with FPCVT");
6627
6628 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00006629 // Otherwise, convert to double-precision and then round.
Eric Christophercccae792015-01-30 22:02:31 +00006630 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
6631 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS
6632 : PPCISD::FCFIDS)
6633 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU
6634 : PPCISD::FCFID);
6635 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
6636 ? MVT::f32
6637 : MVT::f64;
Hal Finkelf6d45f22013-04-01 17:52:07 +00006638
Owen Anderson9f944592009-08-11 20:47:22 +00006639 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006640 SDValue SINT = Op.getOperand(0);
6641 // When converting to single-precision, we actually need to convert
6642 // to double-precision first and then round to single-precision.
6643 // To avoid double-rounding effects during that operation, we have
6644 // to prepare the input operand. Bits that might be truncated when
6645 // converting to double-precision are replaced by a bit that won't
6646 // be lost at this stage, but is below the single-precision rounding
6647 // position.
6648 //
6649 // However, if -enable-unsafe-fp-math is in effect, accept double
6650 // rounding to avoid the extra overhead.
6651 if (Op.getValueType() == MVT::f32 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006652 !Subtarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006653 !DAG.getTarget().Options.UnsafeFPMath) {
6654
6655 // Twiddle input to make sure the low 11 bits are zero. (If this
6656 // is the case, we are guaranteed the value will fit into the 53 bit
6657 // mantissa of an IEEE double-precision value without rounding.)
6658 // If any of those low 11 bits were not zero originally, make sure
6659 // bit 12 (value 2048) is set instead, so that the final rounding
6660 // to single-precision gets the correct result.
6661 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006662 SINT, DAG.getConstant(2047, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006663 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006664 Round, DAG.getConstant(2047, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006665 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
6666 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006667 Round, DAG.getConstant(-2048, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006668
6669 // However, we cannot use that value unconditionally: if the magnitude
6670 // of the input value is small, the bit-twiddling we did above might
6671 // end up visibly changing the output. Fortunately, in that case, we
6672 // don't need to twiddle bits since the original input will convert
6673 // exactly to double-precision floating-point already. Therefore,
6674 // construct a conditional to use the original value if the top 11
6675 // bits are all sign-bit copies, and use the rounded value computed
6676 // above otherwise.
6677 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006678 SINT, DAG.getConstant(53, dl, MVT::i32));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006679 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006680 Cond, DAG.getConstant(1, dl, MVT::i64));
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006681 Cond = DAG.getSetCC(dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006682 Cond, DAG.getConstant(1, dl, MVT::i64), ISD::SETUGT);
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006683
6684 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
6685 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00006686
Hal Finkeled844c42015-01-06 22:31:02 +00006687 ReuseLoadInfo RLI;
6688 SDValue Bits;
6689
Hal Finkel6c392692015-01-09 01:34:30 +00006690 MachineFunction &MF = DAG.getMachineFunction();
Hal Finkeled844c42015-01-06 22:31:02 +00006691 if (canReuseLoadAddress(SINT, MVT::i64, RLI, DAG)) {
6692 Bits = DAG.getLoad(MVT::f64, dl, RLI.Chain, RLI.Ptr, RLI.MPI, false,
6693 false, RLI.IsInvariant, RLI.Alignment, RLI.AAInfo,
6694 RLI.Ranges);
6695 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
Hal Finkel6c392692015-01-09 01:34:30 +00006696 } else if (Subtarget.hasLFIWAX() &&
6697 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::SEXTLOAD)) {
6698 MachineMemOperand *MMO =
6699 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6700 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6701 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6702 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWAX, dl,
6703 DAG.getVTList(MVT::f64, MVT::Other),
6704 Ops, MVT::i32, MMO);
6705 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
6706 } else if (Subtarget.hasFPCVT() &&
6707 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::ZEXTLOAD)) {
6708 MachineMemOperand *MMO =
6709 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6710 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6711 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6712 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWZX, dl,
6713 DAG.getVTList(MVT::f64, MVT::Other),
6714 Ops, MVT::i32, MMO);
6715 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
6716 } else if (((Subtarget.hasLFIWAX() &&
6717 SINT.getOpcode() == ISD::SIGN_EXTEND) ||
6718 (Subtarget.hasFPCVT() &&
6719 SINT.getOpcode() == ISD::ZERO_EXTEND)) &&
6720 SINT.getOperand(0).getValueType() == MVT::i32) {
6721 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +00006722 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Hal Finkel6c392692015-01-09 01:34:30 +00006723
6724 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
6725 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
6726
Alex Lorenze40c8a22015-08-11 23:09:45 +00006727 SDValue Store = DAG.getStore(
6728 DAG.getEntryNode(), dl, SINT.getOperand(0), FIdx,
6729 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx),
6730 false, false, 0);
Hal Finkel6c392692015-01-09 01:34:30 +00006731
6732 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
6733 "Expected an i32 store");
6734
6735 RLI.Ptr = FIdx;
6736 RLI.Chain = Store;
Alex Lorenze40c8a22015-08-11 23:09:45 +00006737 RLI.MPI =
6738 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Hal Finkel6c392692015-01-09 01:34:30 +00006739 RLI.Alignment = 4;
6740
6741 MachineMemOperand *MMO =
6742 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6743 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6744 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6745 Bits = DAG.getMemIntrinsicNode(SINT.getOpcode() == ISD::ZERO_EXTEND ?
6746 PPCISD::LFIWZX : PPCISD::LFIWAX,
6747 dl, DAG.getVTList(MVT::f64, MVT::Other),
6748 Ops, MVT::i32, MMO);
Hal Finkeled844c42015-01-06 22:31:02 +00006749 } else
6750 Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
6751
Hal Finkelf6d45f22013-04-01 17:52:07 +00006752 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
6753
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006754 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00006755 FP = DAG.getNode(ISD::FP_ROUND, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006756 MVT::f32, FP, DAG.getIntPtrConstant(0, dl));
Chris Lattner4211ca92006-04-14 06:01:58 +00006757 return FP;
6758 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006759
Owen Anderson9f944592009-08-11 20:47:22 +00006760 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006761 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00006762 // Since we only generate this in 64-bit mode, we can take advantage of
6763 // 64-bit registers. In particular, sign extend the input value into the
6764 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
6765 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00006766 MachineFunction &MF = DAG.getMachineFunction();
6767 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +00006768 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Scott Michelcf0da6c2009-02-17 22:15:04 +00006769
Hal Finkelbeb296b2013-03-31 10:12:51 +00006770 SDValue Ld;
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006771 if (Subtarget.hasLFIWAX() || Subtarget.hasFPCVT()) {
Hal Finkeled844c42015-01-06 22:31:02 +00006772 ReuseLoadInfo RLI;
6773 bool ReusingLoad;
6774 if (!(ReusingLoad = canReuseLoadAddress(Op.getOperand(0), MVT::i32, RLI,
6775 DAG))) {
6776 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
6777 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006778
Alex Lorenze40c8a22015-08-11 23:09:45 +00006779 SDValue Store = DAG.getStore(
6780 DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
6781 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx),
6782 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00006783
Hal Finkeled844c42015-01-06 22:31:02 +00006784 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
6785 "Expected an i32 store");
6786
6787 RLI.Ptr = FIdx;
6788 RLI.Chain = Store;
Alex Lorenze40c8a22015-08-11 23:09:45 +00006789 RLI.MPI =
6790 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Hal Finkeled844c42015-01-06 22:31:02 +00006791 RLI.Alignment = 4;
6792 }
6793
Hal Finkelbeb296b2013-03-31 10:12:51 +00006794 MachineMemOperand *MMO =
Hal Finkeled844c42015-01-06 22:31:02 +00006795 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6796 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6797 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
Hal Finkelf6d45f22013-04-01 17:52:07 +00006798 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
6799 PPCISD::LFIWZX : PPCISD::LFIWAX,
6800 dl, DAG.getVTList(MVT::f64, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00006801 Ops, MVT::i32, MMO);
Hal Finkeled844c42015-01-06 22:31:02 +00006802 if (ReusingLoad)
6803 spliceIntoChain(RLI.ResChain, Ld.getValue(1), DAG);
Hal Finkelbeb296b2013-03-31 10:12:51 +00006804 } else {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006805 assert(Subtarget.isPPC64() &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006806 "i32->FP without LFIWAX supported only on PPC64");
6807
Hal Finkelbeb296b2013-03-31 10:12:51 +00006808 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
6809 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
6810
6811 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
6812 Op.getOperand(0));
6813
6814 // STD the extended value into the stack slot.
Alex Lorenze40c8a22015-08-11 23:09:45 +00006815 SDValue Store = DAG.getStore(
6816 DAG.getEntryNode(), dl, Ext64, FIdx,
6817 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx),
6818 false, false, 0);
Hal Finkelbeb296b2013-03-31 10:12:51 +00006819
6820 // Load the value as a double.
Alex Lorenze40c8a22015-08-11 23:09:45 +00006821 Ld = DAG.getLoad(
6822 MVT::f64, dl, Store, FIdx,
6823 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx),
6824 false, false, false, 0);
Hal Finkelbeb296b2013-03-31 10:12:51 +00006825 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006826
Chris Lattner4211ca92006-04-14 06:01:58 +00006827 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00006828 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006829 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006830 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP,
6831 DAG.getIntPtrConstant(0, dl));
Chris Lattner4211ca92006-04-14 06:01:58 +00006832 return FP;
6833}
6834
Dan Gohman21cea8a2010-04-17 15:26:15 +00006835SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
6836 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006837 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006838 /*
6839 The rounding mode is in bits 30:31 of FPSR, and has the following
6840 settings:
6841 00 Round to nearest
6842 01 Round to 0
6843 10 Round to +inf
6844 11 Round to -inf
6845
6846 FLT_ROUNDS, on the other hand, expects the following:
6847 -1 Undefined
6848 0 Round to 0
6849 1 Round to nearest
6850 2 Round to +inf
6851 3 Round to -inf
6852
6853 To perform the conversion, we do:
6854 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
6855 */
6856
6857 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00006858 EVT VT = Op.getValueType();
Mehdi Amini44ede332015-07-09 02:09:04 +00006859 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006860
6861 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00006862 EVT NodeTys[] = {
6863 MVT::f64, // return register
6864 MVT::Glue // unused in this context
6865 };
Craig Topper2d2aa0c2014-04-30 07:17:30 +00006866 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, None);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006867
6868 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00006869 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006870 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00006871 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00006872 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006873
6874 // Load FP Control Word from low 32 bits of stack slot.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006875 SDValue Four = DAG.getConstant(4, dl, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00006876 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00006877 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006878 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006879
6880 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006881 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00006882 DAG.getNode(ISD::AND, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006883 CWD, DAG.getConstant(3, dl, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006884 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00006885 DAG.getNode(ISD::SRL, dl, MVT::i32,
6886 DAG.getNode(ISD::AND, dl, MVT::i32,
6887 DAG.getNode(ISD::XOR, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006888 CWD, DAG.getConstant(3, dl, MVT::i32)),
6889 DAG.getConstant(3, dl, MVT::i32)),
6890 DAG.getConstant(1, dl, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006891
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006892 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00006893 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006894
Duncan Sands13237ac2008-06-06 12:08:01 +00006895 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00006896 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006897}
6898
Dan Gohman21cea8a2010-04-17 15:26:15 +00006899SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006900 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006901 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006902 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00006903 assert(Op.getNumOperands() == 3 &&
6904 VT == Op.getOperand(1).getValueType() &&
6905 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006906
Chris Lattner601b8652006-09-20 03:47:40 +00006907 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00006908 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006909 SDValue Lo = Op.getOperand(0);
6910 SDValue Hi = Op.getOperand(1);
6911 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006912 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006913
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006914 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006915 DAG.getConstant(BitWidth, dl, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006916 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
6917 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
6918 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
6919 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006920 DAG.getConstant(-BitWidth, dl, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006921 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
6922 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
6923 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006924 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006925 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006926}
6927
Dan Gohman21cea8a2010-04-17 15:26:15 +00006928SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006929 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006930 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00006931 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00006932 assert(Op.getNumOperands() == 3 &&
6933 VT == Op.getOperand(1).getValueType() &&
6934 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006935
Dan Gohman8d2ead22008-03-07 20:36:53 +00006936 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00006937 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006938 SDValue Lo = Op.getOperand(0);
6939 SDValue Hi = Op.getOperand(1);
6940 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006941 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006942
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006943 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006944 DAG.getConstant(BitWidth, dl, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006945 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
6946 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
6947 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
6948 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006949 DAG.getConstant(-BitWidth, dl, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006950 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
6951 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
6952 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006953 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006954 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006955}
6956
Dan Gohman21cea8a2010-04-17 15:26:15 +00006957SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006958 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006959 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006960 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00006961 assert(Op.getNumOperands() == 3 &&
6962 VT == Op.getOperand(1).getValueType() &&
6963 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006964
Dan Gohman8d2ead22008-03-07 20:36:53 +00006965 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006966 SDValue Lo = Op.getOperand(0);
6967 SDValue Hi = Op.getOperand(1);
6968 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006969 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006970
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006971 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006972 DAG.getConstant(BitWidth, dl, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006973 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
6974 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
6975 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
6976 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006977 DAG.getConstant(-BitWidth, dl, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006978 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
6979 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006980 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, dl, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00006981 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006982 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006983 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006984}
6985
6986//===----------------------------------------------------------------------===//
6987// Vector related lowering.
6988//
6989
Chris Lattner2a099c02006-04-17 06:00:21 +00006990/// BuildSplatI - Build a canonical splati of Val with an element size of
6991/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00006992static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006993 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00006994 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00006995
Benjamin Kramer7149aab2015-03-01 18:09:56 +00006996 static const MVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00006997 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00006998 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00006999
Owen Anderson9f944592009-08-11 20:47:22 +00007000 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007001
Chris Lattner09ed0ff2006-12-01 01:45:39 +00007002 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
7003 if (Val == -1)
7004 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007005
Owen Anderson53aa7a92009-08-10 22:56:29 +00007006 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007007
Chris Lattner2a099c02006-04-17 06:00:21 +00007008 // Build a canonical splat for this value.
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007009 return DAG.getBitcast(ReqVT, DAG.getConstant(Val, dl, CanonicalVT));
Chris Lattner2a099c02006-04-17 06:00:21 +00007010}
7011
Hal Finkelcf2e9082013-05-24 23:00:14 +00007012/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
7013/// specified intrinsic ID.
7014static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00007015 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00007016 EVT DestVT = MVT::Other) {
7017 if (DestVT == MVT::Other) DestVT = Op.getValueType();
7018 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007019 DAG.getConstant(IID, dl, MVT::i32), Op);
Hal Finkelcf2e9082013-05-24 23:00:14 +00007020}
7021
Chris Lattnera2cae1b2006-04-18 03:24:30 +00007022/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00007023/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007024static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00007025 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00007026 EVT DestVT = MVT::Other) {
7027 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007028 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007029 DAG.getConstant(IID, dl, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00007030}
7031
Chris Lattnera2cae1b2006-04-18 03:24:30 +00007032/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
7033/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007034static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007035 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00007036 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00007037 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007038 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007039 DAG.getConstant(IID, dl, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00007040}
7041
Chris Lattner264c9082006-04-17 17:55:10 +00007042/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
7043/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007044static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00007045 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00007046 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00007047 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
7048 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00007049
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007050 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00007051 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007052 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00007053 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00007054 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00007055}
7056
Chris Lattner19e90552006-04-14 05:19:18 +00007057// If this is a case we can't handle, return null and let the default
7058// expansion code take care of it. If we CAN select this case, and if it
7059// selects to a single instruction, return Op. Otherwise, if we can codegen
7060// this case more efficiently than a constant pool load, lower it to the
7061// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00007062SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
7063 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007064 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007065 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
Craig Toppere73658d2014-04-28 04:05:08 +00007066 assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00007067
Hal Finkelc93a9a22015-02-25 01:06:45 +00007068 if (Subtarget.hasQPX() && Op.getValueType() == MVT::v4i1) {
7069 // We first build an i32 vector, load it into a QPX register,
7070 // then convert it to a floating-point vector and compare it
7071 // to a zero vector to get the boolean result.
7072 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
7073 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Alex Lorenze40c8a22015-08-11 23:09:45 +00007074 MachinePointerInfo PtrInfo =
7075 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Mehdi Amini44ede332015-07-09 02:09:04 +00007076 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007077 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
7078
7079 assert(BVN->getNumOperands() == 4 &&
7080 "BUILD_VECTOR for v4i1 does not have 4 operands");
7081
7082 bool IsConst = true;
7083 for (unsigned i = 0; i < 4; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00007084 if (BVN->getOperand(i).isUndef()) continue;
Hal Finkelc93a9a22015-02-25 01:06:45 +00007085 if (!isa<ConstantSDNode>(BVN->getOperand(i))) {
7086 IsConst = false;
7087 break;
7088 }
7089 }
7090
7091 if (IsConst) {
7092 Constant *One =
7093 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), 1.0);
7094 Constant *NegOne =
7095 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), -1.0);
7096
7097 SmallVector<Constant*, 4> CV(4, NegOne);
7098 for (unsigned i = 0; i < 4; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00007099 if (BVN->getOperand(i).isUndef())
Hal Finkelc93a9a22015-02-25 01:06:45 +00007100 CV[i] = UndefValue::get(Type::getFloatTy(*DAG.getContext()));
Artyom Skrobov314ee042015-11-25 19:41:11 +00007101 else if (isNullConstant(BVN->getOperand(i)))
Hal Finkelc93a9a22015-02-25 01:06:45 +00007102 continue;
7103 else
7104 CV[i] = One;
7105 }
7106
7107 Constant *CP = ConstantVector::get(CV);
Mehdi Amini44ede332015-07-09 02:09:04 +00007108 SDValue CPIdx = DAG.getConstantPool(CP, getPointerTy(DAG.getDataLayout()),
7109 16 /* alignment */);
7110
Hal Finkelc93a9a22015-02-25 01:06:45 +00007111 SmallVector<SDValue, 2> Ops;
7112 Ops.push_back(DAG.getEntryNode());
7113 Ops.push_back(CPIdx);
7114
7115 SmallVector<EVT, 2> ValueVTs;
7116 ValueVTs.push_back(MVT::v4i1);
7117 ValueVTs.push_back(MVT::Other); // chain
7118 SDVTList VTs = DAG.getVTList(ValueVTs);
7119
Alex Lorenze40c8a22015-08-11 23:09:45 +00007120 return DAG.getMemIntrinsicNode(
7121 PPCISD::QVLFSb, dl, VTs, Ops, MVT::v4f32,
7122 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007123 }
7124
7125 SmallVector<SDValue, 4> Stores;
7126 for (unsigned i = 0; i < 4; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00007127 if (BVN->getOperand(i).isUndef()) continue;
Hal Finkelc93a9a22015-02-25 01:06:45 +00007128
7129 unsigned Offset = 4*i;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007130 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007131 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
7132
7133 unsigned StoreSize = BVN->getOperand(i).getValueType().getStoreSize();
7134 if (StoreSize > 4) {
7135 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
7136 BVN->getOperand(i), Idx,
7137 PtrInfo.getWithOffset(Offset),
7138 MVT::i32, false, false, 0));
7139 } else {
7140 SDValue StoreValue = BVN->getOperand(i);
7141 if (StoreSize < 4)
7142 StoreValue = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, StoreValue);
7143
7144 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
7145 StoreValue, Idx,
7146 PtrInfo.getWithOffset(Offset),
7147 false, false, 0));
7148 }
7149 }
7150
7151 SDValue StoreChain;
7152 if (!Stores.empty())
7153 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
7154 else
7155 StoreChain = DAG.getEntryNode();
7156
7157 // Now load from v4i32 into the QPX register; this will extend it to
7158 // v4i64 but not yet convert it to a floating point. Nevertheless, this
7159 // is typed as v4f64 because the QPX register integer states are not
7160 // explicitly represented.
7161
7162 SmallVector<SDValue, 2> Ops;
7163 Ops.push_back(StoreChain);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007164 Ops.push_back(DAG.getConstant(Intrinsic::ppc_qpx_qvlfiwz, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007165 Ops.push_back(FIdx);
7166
7167 SmallVector<EVT, 2> ValueVTs;
7168 ValueVTs.push_back(MVT::v4f64);
7169 ValueVTs.push_back(MVT::Other); // chain
7170 SDVTList VTs = DAG.getVTList(ValueVTs);
7171
7172 SDValue LoadedVect = DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN,
7173 dl, VTs, Ops, MVT::v4i32, PtrInfo);
7174 LoadedVect = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007175 DAG.getConstant(Intrinsic::ppc_qpx_qvfcfidu, dl, MVT::i32),
Hal Finkelc93a9a22015-02-25 01:06:45 +00007176 LoadedVect);
7177
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007178 SDValue FPZeros = DAG.getConstantFP(0.0, dl, MVT::v4f64);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007179
7180 return DAG.getSetCC(dl, MVT::v4i1, LoadedVect, FPZeros, ISD::SETEQ);
7181 }
7182
7183 // All other QPX vectors are handled by generic code.
7184 if (Subtarget.hasQPX())
7185 return SDValue();
7186
Bob Wilson85cefe82009-03-02 23:24:16 +00007187 // Check if this is a splat of a constant value.
7188 APInt APSplatBits, APSplatUndef;
7189 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00007190 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00007191 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Bill Schmidt91dd7652015-04-03 13:48:24 +00007192 HasAnyUndefs, 0, !Subtarget.isLittleEndian()) ||
7193 SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00007194 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00007195
Bob Wilson530e0382009-03-03 19:26:27 +00007196 unsigned SplatBits = APSplatBits.getZExtValue();
7197 unsigned SplatUndef = APSplatUndef.getZExtValue();
7198 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007199
Bob Wilson530e0382009-03-03 19:26:27 +00007200 // First, handle single instruction cases.
7201
7202 // All zeros?
7203 if (SplatBits == 0) {
7204 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00007205 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007206 SDValue Z = DAG.getConstant(0, dl, MVT::v4i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00007207 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00007208 }
Bob Wilson530e0382009-03-03 19:26:27 +00007209 return Op;
7210 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00007211
Bob Wilson530e0382009-03-03 19:26:27 +00007212 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
7213 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
7214 (32-SplatBitSize));
7215 if (SextVal >= -16 && SextVal <= 15)
7216 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007217
Bob Wilson530e0382009-03-03 19:26:27 +00007218 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007219
Bob Wilson530e0382009-03-03 19:26:27 +00007220 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00007221 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
7222 // If this value is in the range [17,31] and is odd, use:
7223 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
7224 // If this value is in the range [-31,-17] and is odd, use:
7225 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
7226 // Note the last two are three-instruction sequences.
7227 if (SextVal >= -32 && SextVal <= 31) {
7228 // To avoid having these optimizations undone by constant folding,
7229 // we convert to a pseudo that will be expanded later into one of
7230 // the above forms.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007231 SDValue Elt = DAG.getConstant(SextVal, dl, MVT::i32);
Bill Schmidt71dddd52014-05-27 15:57:51 +00007232 EVT VT = (SplatSize == 1 ? MVT::v16i8 :
7233 (SplatSize == 2 ? MVT::v8i16 : MVT::v4i32));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007234 SDValue EltSize = DAG.getConstant(SplatSize, dl, MVT::i32);
Bill Schmidt71dddd52014-05-27 15:57:51 +00007235 SDValue RetVal = DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
7236 if (VT == Op.getValueType())
7237 return RetVal;
7238 else
7239 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), RetVal);
Bob Wilson530e0382009-03-03 19:26:27 +00007240 }
7241
7242 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
7243 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
7244 // for fneg/fabs.
7245 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
7246 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00007247 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007248
7249 // Make the VSLW intrinsic, computing 0x8000_0000.
7250 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
7251 OnesV, DAG, dl);
7252
7253 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00007254 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00007255 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00007256 }
7257
7258 // Check to see if this is a wide variety of vsplti*, binop self cases.
7259 static const signed char SplatCsts[] = {
7260 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
7261 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
7262 };
7263
7264 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
7265 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
7266 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
7267 int i = SplatCsts[idx];
7268
7269 // Figure out what shift amount will be used by altivec if shifted by i in
7270 // this splat size.
7271 unsigned TypeShiftAmt = i & (SplatBitSize-1);
7272
7273 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00007274 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00007275 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007276 static const unsigned IIDs[] = { // Intrinsic to use for each size.
7277 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
7278 Intrinsic::ppc_altivec_vslw
7279 };
7280 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00007281 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00007282 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007283
Bob Wilson530e0382009-03-03 19:26:27 +00007284 // vsplti + srl self.
7285 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00007286 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007287 static const unsigned IIDs[] = { // Intrinsic to use for each size.
7288 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
7289 Intrinsic::ppc_altivec_vsrw
7290 };
7291 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00007292 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00007293 }
7294
Bob Wilson530e0382009-03-03 19:26:27 +00007295 // vsplti + sra self.
7296 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00007297 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007298 static const unsigned IIDs[] = { // Intrinsic to use for each size.
7299 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
7300 Intrinsic::ppc_altivec_vsraw
7301 };
7302 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00007303 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00007304 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007305
Bob Wilson530e0382009-03-03 19:26:27 +00007306 // vsplti + rol self.
7307 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
7308 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00007309 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007310 static const unsigned IIDs[] = { // Intrinsic to use for each size.
7311 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
7312 Intrinsic::ppc_altivec_vrlw
7313 };
7314 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00007315 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00007316 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007317
Bob Wilson530e0382009-03-03 19:26:27 +00007318 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00007319 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00007320 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bill Schmidt1e77bb12015-07-15 15:45:30 +00007321 unsigned Amt = Subtarget.isLittleEndian() ? 15 : 1;
7322 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00007323 }
Bob Wilson530e0382009-03-03 19:26:27 +00007324 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00007325 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00007326 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bill Schmidt1e77bb12015-07-15 15:45:30 +00007327 unsigned Amt = Subtarget.isLittleEndian() ? 14 : 2;
7328 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00007329 }
Bob Wilson530e0382009-03-03 19:26:27 +00007330 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00007331 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00007332 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bill Schmidt1e77bb12015-07-15 15:45:30 +00007333 unsigned Amt = Subtarget.isLittleEndian() ? 13 : 3;
7334 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00007335 }
7336 }
7337
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007338 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00007339}
7340
Chris Lattner071ad012006-04-17 05:28:54 +00007341/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
7342/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007343static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00007344 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00007345 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00007346 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00007347 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00007348 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007349
Chris Lattner071ad012006-04-17 05:28:54 +00007350 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00007351 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 +00007352 OP_VMRGHW,
7353 OP_VMRGLW,
7354 OP_VSPLTISW0,
7355 OP_VSPLTISW1,
7356 OP_VSPLTISW2,
7357 OP_VSPLTISW3,
7358 OP_VSLDOI4,
7359 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00007360 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00007361 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00007362
Chris Lattner071ad012006-04-17 05:28:54 +00007363 if (OpNum == OP_COPY) {
7364 if (LHSID == (1*9+2)*9+3) return LHS;
7365 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
7366 return RHS;
7367 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007368
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007369 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007370 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
7371 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007372
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007373 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00007374 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007375 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00007376 case OP_VMRGHW:
7377 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
7378 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
7379 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
7380 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
7381 break;
7382 case OP_VMRGLW:
7383 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
7384 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
7385 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
7386 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
7387 break;
7388 case OP_VSPLTISW0:
7389 for (unsigned i = 0; i != 16; ++i)
7390 ShufIdxs[i] = (i&3)+0;
7391 break;
7392 case OP_VSPLTISW1:
7393 for (unsigned i = 0; i != 16; ++i)
7394 ShufIdxs[i] = (i&3)+4;
7395 break;
7396 case OP_VSPLTISW2:
7397 for (unsigned i = 0; i != 16; ++i)
7398 ShufIdxs[i] = (i&3)+8;
7399 break;
7400 case OP_VSPLTISW3:
7401 for (unsigned i = 0; i != 16; ++i)
7402 ShufIdxs[i] = (i&3)+12;
7403 break;
7404 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007405 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007406 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007407 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007408 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007409 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007410 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00007411 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00007412 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
7413 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00007414 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00007415 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00007416}
7417
Chris Lattner19e90552006-04-14 05:19:18 +00007418/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
7419/// is a shuffle we can handle in a single instruction, return it. Otherwise,
7420/// return the code it can be lowered into. Worst case, it can always be
7421/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007422SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007423 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007424 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007425 SDValue V1 = Op.getOperand(0);
7426 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007427 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007428 EVT VT = Op.getValueType();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007429 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00007430
Nemanja Ivanovic1a2b2f02016-05-04 16:04:02 +00007431 if (Subtarget.hasVSX()) {
7432 if (V2.isUndef() && PPC::isSplatShuffleMask(SVOp, 4)) {
7433 int SplatIdx = PPC::getVSPLTImmediate(SVOp, 4, DAG);
7434 SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
7435 SDValue Splat = DAG.getNode(PPCISD::XXSPLT, dl, MVT::v4i32, Conv,
7436 DAG.getConstant(SplatIdx, dl, MVT::i32));
7437 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Splat);
7438 }
7439 }
7440
Hal Finkelc93a9a22015-02-25 01:06:45 +00007441 if (Subtarget.hasQPX()) {
7442 if (VT.getVectorNumElements() != 4)
7443 return SDValue();
7444
Sanjay Patel57195842016-03-14 17:28:46 +00007445 if (V2.isUndef()) V2 = V1;
Hal Finkelc93a9a22015-02-25 01:06:45 +00007446
7447 int AlignIdx = PPC::isQVALIGNIShuffleMask(SVOp);
7448 if (AlignIdx != -1) {
7449 return DAG.getNode(PPCISD::QVALIGNI, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007450 DAG.getConstant(AlignIdx, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007451 } else if (SVOp->isSplat()) {
7452 int SplatIdx = SVOp->getSplatIndex();
7453 if (SplatIdx >= 4) {
7454 std::swap(V1, V2);
7455 SplatIdx -= 4;
7456 }
7457
Hal Finkelc93a9a22015-02-25 01:06:45 +00007458 return DAG.getNode(PPCISD::QVESPLATI, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007459 DAG.getConstant(SplatIdx, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007460 }
7461
7462 // Lower this into a qvgpci/qvfperm pair.
7463
7464 // Compute the qvgpci literal
7465 unsigned idx = 0;
7466 for (unsigned i = 0; i < 4; ++i) {
7467 int m = SVOp->getMaskElt(i);
7468 unsigned mm = m >= 0 ? (unsigned) m : i;
7469 idx |= mm << (3-i)*3;
7470 }
7471
7472 SDValue V3 = DAG.getNode(PPCISD::QVGPCI, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007473 DAG.getConstant(idx, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007474 return DAG.getNode(PPCISD::QVFPERM, dl, VT, V1, V2, V3);
7475 }
7476
Chris Lattner19e90552006-04-14 05:19:18 +00007477 // Cases that are handled by instructions that take permute immediates
7478 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
7479 // selected by the instruction selector.
Sanjay Patel57195842016-03-14 17:28:46 +00007480 if (V2.isUndef()) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007481 if (PPC::isSplatShuffleMask(SVOp, 1) ||
7482 PPC::isSplatShuffleMask(SVOp, 2) ||
7483 PPC::isSplatShuffleMask(SVOp, 4) ||
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00007484 PPC::isVPKUWUMShuffleMask(SVOp, 1, DAG) ||
7485 PPC::isVPKUHUMShuffleMask(SVOp, 1, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00007486 PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00007487 PPC::isVMRGLShuffleMask(SVOp, 1, 1, DAG) ||
7488 PPC::isVMRGLShuffleMask(SVOp, 2, 1, DAG) ||
7489 PPC::isVMRGLShuffleMask(SVOp, 4, 1, DAG) ||
7490 PPC::isVMRGHShuffleMask(SVOp, 1, 1, DAG) ||
7491 PPC::isVMRGHShuffleMask(SVOp, 2, 1, DAG) ||
Kit Barton13894c72015-06-25 15:17:40 +00007492 PPC::isVMRGHShuffleMask(SVOp, 4, 1, DAG) ||
Hal Finkel77c8b7f2015-09-02 16:52:37 +00007493 (Subtarget.hasP8Altivec() && (
7494 PPC::isVPKUDUMShuffleMask(SVOp, 1, DAG) ||
7495 PPC::isVMRGEOShuffleMask(SVOp, true, 1, DAG) ||
7496 PPC::isVMRGEOShuffleMask(SVOp, false, 1, DAG)))) {
Chris Lattner19e90552006-04-14 05:19:18 +00007497 return Op;
7498 }
7499 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007500
Chris Lattner19e90552006-04-14 05:19:18 +00007501 // Altivec has a variety of "shuffle immediates" that take two vector inputs
7502 // and produce a fixed permutation. If any of these match, do not lower to
7503 // VPERM.
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00007504 unsigned int ShuffleKind = isLittleEndian ? 2 : 0;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00007505 if (PPC::isVPKUWUMShuffleMask(SVOp, ShuffleKind, DAG) ||
7506 PPC::isVPKUHUMShuffleMask(SVOp, ShuffleKind, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00007507 PPC::isVSLDOIShuffleMask(SVOp, ShuffleKind, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00007508 PPC::isVMRGLShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
7509 PPC::isVMRGLShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
7510 PPC::isVMRGLShuffleMask(SVOp, 4, ShuffleKind, DAG) ||
7511 PPC::isVMRGHShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
7512 PPC::isVMRGHShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
Kit Barton13894c72015-06-25 15:17:40 +00007513 PPC::isVMRGHShuffleMask(SVOp, 4, ShuffleKind, DAG) ||
Hal Finkel77c8b7f2015-09-02 16:52:37 +00007514 (Subtarget.hasP8Altivec() && (
7515 PPC::isVPKUDUMShuffleMask(SVOp, ShuffleKind, DAG) ||
7516 PPC::isVMRGEOShuffleMask(SVOp, true, ShuffleKind, DAG) ||
7517 PPC::isVMRGEOShuffleMask(SVOp, false, ShuffleKind, DAG))))
Chris Lattner19e90552006-04-14 05:19:18 +00007518 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007519
Chris Lattner071ad012006-04-17 05:28:54 +00007520 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
7521 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00007522 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00007523
Chris Lattner071ad012006-04-17 05:28:54 +00007524 unsigned PFIndexes[4];
7525 bool isFourElementShuffle = true;
7526 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
7527 unsigned EltNo = 8; // Start out undef.
7528 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007529 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00007530 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007531
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007532 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00007533 if ((ByteSource & 3) != j) {
7534 isFourElementShuffle = false;
7535 break;
7536 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007537
Chris Lattner071ad012006-04-17 05:28:54 +00007538 if (EltNo == 8) {
7539 EltNo = ByteSource/4;
7540 } else if (EltNo != ByteSource/4) {
7541 isFourElementShuffle = false;
7542 break;
7543 }
7544 }
7545 PFIndexes[i] = EltNo;
7546 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007547
7548 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00007549 // perfect shuffle vector to determine if it is cost effective to do this as
7550 // discrete instructions, or whether we should use a vperm.
Bill Schmidtf910a062014-06-10 14:35:01 +00007551 // For now, we skip this for little endian until such time as we have a
7552 // little-endian perfect shuffle table.
7553 if (isFourElementShuffle && !isLittleEndian) {
Chris Lattner071ad012006-04-17 05:28:54 +00007554 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007555 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00007556 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007557
Chris Lattner071ad012006-04-17 05:28:54 +00007558 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
7559 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007560
Chris Lattner071ad012006-04-17 05:28:54 +00007561 // Determining when to avoid vperm is tricky. Many things affect the cost
7562 // of vperm, particularly how many times the perm mask needs to be computed.
7563 // For example, if the perm mask can be hoisted out of a loop or is already
7564 // used (perhaps because there are multiple permutes with the same shuffle
7565 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
7566 // the loop requires an extra register.
7567 //
7568 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00007569 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00007570 // available, if this block is within a loop, we should avoid using vperm
7571 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007572 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007573 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007574 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007575
Chris Lattner19e90552006-04-14 05:19:18 +00007576 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
7577 // vector that will get spilled to the constant pool.
Sanjay Patel57195842016-03-14 17:28:46 +00007578 if (V2.isUndef()) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007579
Chris Lattner19e90552006-04-14 05:19:18 +00007580 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
7581 // that it is in input element units, not in bytes. Convert now.
Bill Schmidt4aedff82014-06-06 14:06:26 +00007582
7583 // For little endian, the order of the input vectors is reversed, and
7584 // the permutation mask is complemented with respect to 31. This is
7585 // necessary to produce proper semantics with the big-endian-biased vperm
7586 // instruction.
Owen Anderson53aa7a92009-08-10 22:56:29 +00007587 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00007588 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007589
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007590 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007591 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
7592 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007593
Chris Lattner19e90552006-04-14 05:19:18 +00007594 for (unsigned j = 0; j != BytesPerElement; ++j)
Bill Schmidt4aedff82014-06-06 14:06:26 +00007595 if (isLittleEndian)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007596 ResultMask.push_back(DAG.getConstant(31 - (SrcElt*BytesPerElement + j),
7597 dl, MVT::i32));
Bill Schmidt4aedff82014-06-06 14:06:26 +00007598 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007599 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement + j, dl,
Bill Schmidt4aedff82014-06-06 14:06:26 +00007600 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00007601 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007602
Ahmed Bougacha128f8732016-04-26 21:15:30 +00007603 SDValue VPermMask = DAG.getBuildVector(MVT::v16i8, dl, ResultMask);
Bill Schmidt4aedff82014-06-06 14:06:26 +00007604 if (isLittleEndian)
7605 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
7606 V2, V1, VPermMask);
7607 else
7608 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
7609 V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00007610}
7611
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +00007612/// getVectorCompareInfo - Given an intrinsic, return false if it is not a
7613/// vector comparison. If it is, return true and fill in Opc/isDot with
Chris Lattner9754d142006-04-18 17:59:36 +00007614/// information about the intrinsic.
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +00007615static bool getVectorCompareInfo(SDValue Intrin, int &CompareOpc,
7616 bool &isDot, const PPCSubtarget &Subtarget) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00007617 unsigned IntrinsicID =
7618 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00007619 CompareOpc = -1;
7620 isDot = false;
7621 switch (IntrinsicID) {
7622 default: return false;
7623 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00007624 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
7625 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
7626 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
7627 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
7628 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007629 case Intrinsic::ppc_altivec_vcmpequd_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007630 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007631 CompareOpc = 199;
7632 isDot = 1;
7633 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007634 return false;
7635
7636 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007637 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
7638 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
7639 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
7640 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
7641 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007642 case Intrinsic::ppc_altivec_vcmpgtsd_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007643 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007644 CompareOpc = 967;
7645 isDot = 1;
7646 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007647 return false;
7648
7649 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007650 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
7651 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
7652 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007653 case Intrinsic::ppc_altivec_vcmpgtud_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007654 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007655 CompareOpc = 711;
7656 isDot = 1;
7657 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007658 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007659
Kit Barton0cfa7b72015-03-03 19:55:45 +00007660 break;
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +00007661 // VSX predicate comparisons use the same infrastructure
7662 case Intrinsic::ppc_vsx_xvcmpeqdp_p:
7663 case Intrinsic::ppc_vsx_xvcmpgedp_p:
7664 case Intrinsic::ppc_vsx_xvcmpgtdp_p:
7665 case Intrinsic::ppc_vsx_xvcmpeqsp_p:
7666 case Intrinsic::ppc_vsx_xvcmpgesp_p:
7667 case Intrinsic::ppc_vsx_xvcmpgtsp_p:
7668 if (Subtarget.hasVSX()) {
7669 switch (IntrinsicID) {
7670 case Intrinsic::ppc_vsx_xvcmpeqdp_p: CompareOpc = 99; break;
7671 case Intrinsic::ppc_vsx_xvcmpgedp_p: CompareOpc = 115; break;
7672 case Intrinsic::ppc_vsx_xvcmpgtdp_p: CompareOpc = 107; break;
7673 case Intrinsic::ppc_vsx_xvcmpeqsp_p: CompareOpc = 67; break;
7674 case Intrinsic::ppc_vsx_xvcmpgesp_p: CompareOpc = 83; break;
7675 case Intrinsic::ppc_vsx_xvcmpgtsp_p: CompareOpc = 75; break;
7676 }
7677 isDot = 1;
7678 }
7679 else
7680 return false;
7681
7682 break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007683
Chris Lattner4211ca92006-04-14 06:01:58 +00007684 // Normal Comparisons.
7685 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
7686 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
7687 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
7688 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
7689 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007690 case Intrinsic::ppc_altivec_vcmpequd:
7691 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007692 CompareOpc = 199;
7693 isDot = 0;
7694 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007695 return false;
7696
7697 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007698 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
7699 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
7700 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
7701 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
7702 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007703 case Intrinsic::ppc_altivec_vcmpgtsd:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007704 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007705 CompareOpc = 967;
7706 isDot = 0;
7707 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007708 return false;
7709
7710 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007711 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
7712 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
7713 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007714 case Intrinsic::ppc_altivec_vcmpgtud:
Kit Barton0cfa7b72015-03-03 19:55:45 +00007715 if (Subtarget.hasP8Altivec()) {
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00007716 CompareOpc = 711;
7717 isDot = 0;
7718 } else
Kit Barton0cfa7b72015-03-03 19:55:45 +00007719 return false;
7720
7721 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007722 }
Chris Lattner9754d142006-04-18 17:59:36 +00007723 return true;
7724}
7725
7726/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
7727/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007728SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007729 SelectionDAG &DAG) const {
Marcin Koscielnicki0cfb6122016-04-26 10:37:22 +00007730 unsigned IntrinsicID =
7731 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7732
7733 if (IntrinsicID == Intrinsic::thread_pointer) {
7734 // Reads the thread pointer register, used for __builtin_thread_pointer.
7735 bool is64bit = Subtarget.isPPC64();
7736 return DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
7737 is64bit ? MVT::i64 : MVT::i32);
7738 }
7739
Chris Lattner9754d142006-04-18 17:59:36 +00007740 // If this is a lowered altivec predicate compare, CompareOpc is set to the
7741 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00007742 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00007743 int CompareOpc;
7744 bool isDot;
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +00007745 if (!getVectorCompareInfo(Op, CompareOpc, isDot, Subtarget))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007746 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007747
Chris Lattner9754d142006-04-18 17:59:36 +00007748 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00007749 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00007750 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00007751 Op.getOperand(1), Op.getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007752 DAG.getConstant(CompareOpc, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00007753 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00007754 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007755
Chris Lattner4211ca92006-04-14 06:01:58 +00007756 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007757 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007758 Op.getOperand(2), // LHS
7759 Op.getOperand(3), // RHS
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007760 DAG.getConstant(CompareOpc, dl, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007761 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00007762 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00007763 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007764
Chris Lattner4211ca92006-04-14 06:01:58 +00007765 // Now that we have the comparison, emit a copy from the CR to a GPR.
7766 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00007767 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00007768 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00007769 CompNode.getValue(1));
7770
Chris Lattner4211ca92006-04-14 06:01:58 +00007771 // Unpack the result based on how the target uses it.
7772 unsigned BitNo; // Bit # of CR6.
7773 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00007774 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00007775 default: // Can't happen, don't crash on invalid number though.
7776 case 0: // Return the value of the EQ bit of CR6.
7777 BitNo = 0; InvertBit = false;
7778 break;
7779 case 1: // Return the inverted value of the EQ bit of CR6.
7780 BitNo = 0; InvertBit = true;
7781 break;
7782 case 2: // Return the value of the LT bit of CR6.
7783 BitNo = 2; InvertBit = false;
7784 break;
7785 case 3: // Return the inverted value of the LT bit of CR6.
7786 BitNo = 2; InvertBit = true;
7787 break;
7788 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007789
Chris Lattner4211ca92006-04-14 06:01:58 +00007790 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00007791 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007792 DAG.getConstant(8 - (3 - BitNo), dl, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00007793 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00007794 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007795 DAG.getConstant(1, dl, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00007796
Chris Lattner4211ca92006-04-14 06:01:58 +00007797 // If we are supposed to, toggle the bit.
7798 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00007799 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007800 DAG.getConstant(1, dl, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00007801 return Flags;
7802}
7803
Hal Finkel5c0d1452014-03-30 13:22:59 +00007804SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
7805 SelectionDAG &DAG) const {
7806 SDLoc dl(Op);
7807 // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int
7808 // instructions), but for smaller types, we need to first extend up to v2i32
7809 // before doing going farther.
7810 if (Op.getValueType() == MVT::v2i64) {
7811 EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
7812 if (ExtVT != MVT::v2i32) {
7813 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0));
7814 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op,
7815 DAG.getValueType(EVT::getVectorVT(*DAG.getContext(),
7816 ExtVT.getVectorElementType(), 4)));
7817 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op);
7818 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op,
7819 DAG.getValueType(MVT::v2i32));
7820 }
7821
7822 return Op;
7823 }
7824
7825 return SDValue();
7826}
7827
Scott Michelcf0da6c2009-02-17 22:15:04 +00007828SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007829 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007830 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00007831 // Create a stack slot that is 16-byte aligned.
7832 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00007833 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00007834 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007835 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007836
Chris Lattner4211ca92006-04-14 06:01:58 +00007837 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00007838 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00007839 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00007840 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00007841 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00007842 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00007843 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00007844}
7845
Hal Finkelc93a9a22015-02-25 01:06:45 +00007846SDValue PPCTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7847 SelectionDAG &DAG) const {
7848 SDLoc dl(Op);
7849 SDNode *N = Op.getNode();
7850
7851 assert(N->getOperand(0).getValueType() == MVT::v4i1 &&
7852 "Unknown extract_vector_elt type");
7853
7854 SDValue Value = N->getOperand(0);
7855
7856 // The first part of this is like the store lowering except that we don't
7857 // need to track the chain.
7858
7859 // The values are now known to be -1 (false) or 1 (true). To convert this
7860 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
7861 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
7862 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
7863
7864 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to
7865 // understand how to form the extending load.
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007866 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007867
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00007868 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007869
7870 // Now convert to an integer and store.
7871 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007872 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32),
Hal Finkelc93a9a22015-02-25 01:06:45 +00007873 Value);
7874
7875 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
7876 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Alex Lorenze40c8a22015-08-11 23:09:45 +00007877 MachinePointerInfo PtrInfo =
7878 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Mehdi Amini44ede332015-07-09 02:09:04 +00007879 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007880 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
7881
7882 SDValue StoreChain = DAG.getEntryNode();
7883 SmallVector<SDValue, 2> Ops;
7884 Ops.push_back(StoreChain);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007885 Ops.push_back(DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007886 Ops.push_back(Value);
7887 Ops.push_back(FIdx);
7888
7889 SmallVector<EVT, 2> ValueVTs;
7890 ValueVTs.push_back(MVT::Other); // chain
7891 SDVTList VTs = DAG.getVTList(ValueVTs);
7892
7893 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID,
7894 dl, VTs, Ops, MVT::v4i32, PtrInfo);
7895
7896 // Extract the value requested.
7897 unsigned Offset = 4*cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007898 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007899 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
7900
7901 SDValue IntVal = DAG.getLoad(MVT::i32, dl, StoreChain, Idx,
7902 PtrInfo.getWithOffset(Offset),
7903 false, false, false, 0);
7904
7905 if (!Subtarget.useCRBits())
7906 return IntVal;
7907
7908 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, IntVal);
7909}
7910
7911/// Lowering for QPX v4i1 loads
7912SDValue PPCTargetLowering::LowerVectorLoad(SDValue Op,
7913 SelectionDAG &DAG) const {
7914 SDLoc dl(Op);
7915 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
7916 SDValue LoadChain = LN->getChain();
7917 SDValue BasePtr = LN->getBasePtr();
7918
7919 if (Op.getValueType() == MVT::v4f64 ||
7920 Op.getValueType() == MVT::v4f32) {
7921 EVT MemVT = LN->getMemoryVT();
7922 unsigned Alignment = LN->getAlignment();
7923
7924 // If this load is properly aligned, then it is legal.
7925 if (Alignment >= MemVT.getStoreSize())
7926 return Op;
7927
7928 EVT ScalarVT = Op.getValueType().getScalarType(),
7929 ScalarMemVT = MemVT.getScalarType();
7930 unsigned Stride = ScalarMemVT.getStoreSize();
7931
7932 SmallVector<SDValue, 8> Vals, LoadChains;
7933 for (unsigned Idx = 0; Idx < 4; ++Idx) {
7934 SDValue Load;
7935 if (ScalarVT != ScalarMemVT)
7936 Load =
7937 DAG.getExtLoad(LN->getExtensionType(), dl, ScalarVT, LoadChain,
7938 BasePtr,
7939 LN->getPointerInfo().getWithOffset(Idx*Stride),
7940 ScalarMemVT, LN->isVolatile(), LN->isNonTemporal(),
7941 LN->isInvariant(), MinAlign(Alignment, Idx*Stride),
7942 LN->getAAInfo());
7943 else
7944 Load =
7945 DAG.getLoad(ScalarVT, dl, LoadChain, BasePtr,
7946 LN->getPointerInfo().getWithOffset(Idx*Stride),
7947 LN->isVolatile(), LN->isNonTemporal(),
7948 LN->isInvariant(), MinAlign(Alignment, Idx*Stride),
7949 LN->getAAInfo());
7950
7951 if (Idx == 0 && LN->isIndexed()) {
7952 assert(LN->getAddressingMode() == ISD::PRE_INC &&
7953 "Unknown addressing mode on vector load");
7954 Load = DAG.getIndexedLoad(Load, dl, BasePtr, LN->getOffset(),
7955 LN->getAddressingMode());
7956 }
7957
7958 Vals.push_back(Load);
7959 LoadChains.push_back(Load.getValue(1));
7960
7961 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007962 DAG.getConstant(Stride, dl,
7963 BasePtr.getValueType()));
Hal Finkelc93a9a22015-02-25 01:06:45 +00007964 }
7965
7966 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00007967 SDValue Value = DAG.getBuildVector(Op.getValueType(), dl, Vals);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007968
7969 if (LN->isIndexed()) {
7970 SDValue RetOps[] = { Value, Vals[0].getValue(1), TF };
7971 return DAG.getMergeValues(RetOps, dl);
7972 }
7973
7974 SDValue RetOps[] = { Value, TF };
7975 return DAG.getMergeValues(RetOps, dl);
7976 }
7977
7978 assert(Op.getValueType() == MVT::v4i1 && "Unknown load to lower");
7979 assert(LN->isUnindexed() && "Indexed v4i1 loads are not supported");
7980
7981 // To lower v4i1 from a byte array, we load the byte elements of the
7982 // vector and then reuse the BUILD_VECTOR logic.
7983
7984 SmallVector<SDValue, 4> VectElmts, VectElmtChains;
7985 for (unsigned i = 0; i < 4; ++i) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007986 SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00007987 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx);
7988
7989 VectElmts.push_back(DAG.getExtLoad(ISD::EXTLOAD,
7990 dl, MVT::i32, LoadChain, Idx,
7991 LN->getPointerInfo().getWithOffset(i),
7992 MVT::i8 /* memory type */,
7993 LN->isVolatile(), LN->isNonTemporal(),
7994 LN->isInvariant(),
7995 1 /* alignment */, LN->getAAInfo()));
7996 VectElmtChains.push_back(VectElmts[i].getValue(1));
7997 }
7998
7999 LoadChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, VectElmtChains);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00008000 SDValue Value = DAG.getBuildVector(MVT::v4i1, dl, VectElmts);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008001
8002 SDValue RVals[] = { Value, LoadChain };
8003 return DAG.getMergeValues(RVals, dl);
8004}
8005
8006/// Lowering for QPX v4i1 stores
8007SDValue PPCTargetLowering::LowerVectorStore(SDValue Op,
8008 SelectionDAG &DAG) const {
8009 SDLoc dl(Op);
8010 StoreSDNode *SN = cast<StoreSDNode>(Op.getNode());
8011 SDValue StoreChain = SN->getChain();
8012 SDValue BasePtr = SN->getBasePtr();
8013 SDValue Value = SN->getValue();
8014
8015 if (Value.getValueType() == MVT::v4f64 ||
8016 Value.getValueType() == MVT::v4f32) {
8017 EVT MemVT = SN->getMemoryVT();
8018 unsigned Alignment = SN->getAlignment();
8019
8020 // If this store is properly aligned, then it is legal.
8021 if (Alignment >= MemVT.getStoreSize())
8022 return Op;
8023
8024 EVT ScalarVT = Value.getValueType().getScalarType(),
8025 ScalarMemVT = MemVT.getScalarType();
8026 unsigned Stride = ScalarMemVT.getStoreSize();
8027
8028 SmallVector<SDValue, 8> Stores;
8029 for (unsigned Idx = 0; Idx < 4; ++Idx) {
Mehdi Amini44ede332015-07-09 02:09:04 +00008030 SDValue Ex = DAG.getNode(
8031 ISD::EXTRACT_VECTOR_ELT, dl, ScalarVT, Value,
8032 DAG.getConstant(Idx, dl, getVectorIdxTy(DAG.getDataLayout())));
Hal Finkelc93a9a22015-02-25 01:06:45 +00008033 SDValue Store;
8034 if (ScalarVT != ScalarMemVT)
8035 Store =
8036 DAG.getTruncStore(StoreChain, dl, Ex, BasePtr,
8037 SN->getPointerInfo().getWithOffset(Idx*Stride),
8038 ScalarMemVT, SN->isVolatile(), SN->isNonTemporal(),
8039 MinAlign(Alignment, Idx*Stride), SN->getAAInfo());
8040 else
8041 Store =
8042 DAG.getStore(StoreChain, dl, Ex, BasePtr,
8043 SN->getPointerInfo().getWithOffset(Idx*Stride),
8044 SN->isVolatile(), SN->isNonTemporal(),
8045 MinAlign(Alignment, Idx*Stride), SN->getAAInfo());
8046
8047 if (Idx == 0 && SN->isIndexed()) {
8048 assert(SN->getAddressingMode() == ISD::PRE_INC &&
8049 "Unknown addressing mode on vector store");
8050 Store = DAG.getIndexedStore(Store, dl, BasePtr, SN->getOffset(),
8051 SN->getAddressingMode());
8052 }
8053
8054 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008055 DAG.getConstant(Stride, dl,
8056 BasePtr.getValueType()));
Hal Finkelc93a9a22015-02-25 01:06:45 +00008057 Stores.push_back(Store);
8058 }
8059
8060 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
8061
8062 if (SN->isIndexed()) {
8063 SDValue RetOps[] = { TF, Stores[0].getValue(1) };
8064 return DAG.getMergeValues(RetOps, dl);
8065 }
8066
8067 return TF;
8068 }
8069
8070 assert(SN->isUnindexed() && "Indexed v4i1 stores are not supported");
8071 assert(Value.getValueType() == MVT::v4i1 && "Unknown store to lower");
8072
8073 // The values are now known to be -1 (false) or 1 (true). To convert this
8074 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
8075 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
8076 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
8077
8078 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to
8079 // understand how to form the extending load.
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00008080 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008081
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00008082 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008083
8084 // Now convert to an integer and store.
8085 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008086 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32),
Hal Finkelc93a9a22015-02-25 01:06:45 +00008087 Value);
8088
8089 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
8090 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Alex Lorenze40c8a22015-08-11 23:09:45 +00008091 MachinePointerInfo PtrInfo =
8092 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
Mehdi Amini44ede332015-07-09 02:09:04 +00008093 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008094 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
8095
8096 SmallVector<SDValue, 2> Ops;
8097 Ops.push_back(StoreChain);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008098 Ops.push_back(DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32));
Hal Finkelc93a9a22015-02-25 01:06:45 +00008099 Ops.push_back(Value);
8100 Ops.push_back(FIdx);
8101
8102 SmallVector<EVT, 2> ValueVTs;
8103 ValueVTs.push_back(MVT::Other); // chain
8104 SDVTList VTs = DAG.getVTList(ValueVTs);
8105
8106 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID,
8107 dl, VTs, Ops, MVT::v4i32, PtrInfo);
8108
8109 // Move data into the byte array.
8110 SmallVector<SDValue, 4> Loads, LoadChains;
8111 for (unsigned i = 0; i < 4; ++i) {
8112 unsigned Offset = 4*i;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008113 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008114 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
8115
8116 Loads.push_back(DAG.getLoad(MVT::i32, dl, StoreChain, Idx,
8117 PtrInfo.getWithOffset(Offset),
8118 false, false, false, 0));
8119 LoadChains.push_back(Loads[i].getValue(1));
8120 }
8121
8122 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8123
8124 SmallVector<SDValue, 4> Stores;
8125 for (unsigned i = 0; i < 4; ++i) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008126 SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType());
Hal Finkelc93a9a22015-02-25 01:06:45 +00008127 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx);
8128
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00008129 Stores.push_back(DAG.getTruncStore(
8130 StoreChain, dl, Loads[i], Idx, SN->getPointerInfo().getWithOffset(i),
8131 MVT::i8 /* memory type */, SN->isNonTemporal(), SN->isVolatile(),
8132 1 /* alignment */, SN->getAAInfo()));
Hal Finkelc93a9a22015-02-25 01:06:45 +00008133 }
8134
8135 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
8136
8137 return StoreChain;
8138}
8139
Dan Gohman21cea8a2010-04-17 15:26:15 +00008140SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008141 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00008142 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008143 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008144
Owen Anderson9f944592009-08-11 20:47:22 +00008145 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
8146 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008147
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008148 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00008149 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008150
Chris Lattner7e4398742006-04-18 03:43:48 +00008151 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00008152 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
8153 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
8154 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008155
Chris Lattner7e4398742006-04-18 03:43:48 +00008156 // Low parts multiplied together, generating 32-bit results (we ignore the
8157 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008158 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00008159 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008160
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008161 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00008162 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00008163 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008164 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00008165 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00008166 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
8167 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008168 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008169
Owen Anderson9f944592009-08-11 20:47:22 +00008170 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00008171
Chris Lattner96d50482006-04-18 04:28:57 +00008172 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00008173 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00008174 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008175 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008176 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00008177
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00008178 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008179 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00008180 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00008181 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008182
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00008183 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008184 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00008185 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00008186 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008187
Bill Schmidt42995e82014-06-09 16:06:29 +00008188 // Merge the results together. Because vmuleub and vmuloub are
8189 // instructions with a big-endian bias, we must reverse the
8190 // element numbering and reverse the meaning of "odd" and "even"
8191 // when generating little endian code.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00008192 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00008193 for (unsigned i = 0; i != 8; ++i) {
Bill Schmidt42995e82014-06-09 16:06:29 +00008194 if (isLittleEndian) {
8195 Ops[i*2 ] = 2*i;
8196 Ops[i*2+1] = 2*i+16;
8197 } else {
8198 Ops[i*2 ] = 2*i+1;
8199 Ops[i*2+1] = 2*i+1+16;
8200 }
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00008201 }
Bill Schmidt42995e82014-06-09 16:06:29 +00008202 if (isLittleEndian)
8203 return DAG.getVectorShuffle(MVT::v16i8, dl, OddParts, EvenParts, Ops);
8204 else
8205 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00008206 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008207 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00008208 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00008209}
8210
Chris Lattnerf3d06c62005-08-26 00:52:45 +00008211/// LowerOperation - Provide custom lowering hooks for some operations.
8212///
Dan Gohman21cea8a2010-04-17 15:26:15 +00008213SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00008214 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008215 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00008216 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00008217 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00008218 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00008219 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00008220 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00008221 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00008222 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
8223 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008224 case ISD::VASTART:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008225 return LowerVASTART(Op, DAG, Subtarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008226
8227 case ISD::VAARG:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008228 return LowerVAARG(Op, DAG, Subtarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00008229
Roman Divackyc3825df2013-07-25 21:36:47 +00008230 case ISD::VACOPY:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008231 return LowerVACOPY(Op, DAG, Subtarget);
Roman Divackyc3825df2013-07-25 21:36:47 +00008232
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008233 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, Subtarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00008234 case ISD::DYNAMIC_STACKALLOC:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008235 return LowerDYNAMIC_STACKALLOC(Op, DAG, Subtarget);
Yury Gribovd7dbb662015-12-01 11:40:55 +00008236 case ISD::GET_DYNAMIC_AREA_OFFSET: return LowerGET_DYNAMIC_AREA_OFFSET(Op, DAG, Subtarget);
Evan Cheng51096af2008-04-19 01:30:48 +00008237
Hal Finkel756810f2013-03-21 21:37:52 +00008238 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
8239 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
8240
Hal Finkel940ab932014-02-28 00:27:01 +00008241 case ISD::LOAD: return LowerLOAD(Op, DAG);
8242 case ISD::STORE: return LowerSTORE(Op, DAG);
8243 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00008244 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00008245 case ISD::FP_TO_UINT:
8246 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Hal Finkeled844c42015-01-06 22:31:02 +00008247 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00008248 case ISD::UINT_TO_FP:
8249 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00008250 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00008251
Chris Lattner4211ca92006-04-14 06:01:58 +00008252 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00008253 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
8254 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
8255 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00008256
Chris Lattner4211ca92006-04-14 06:01:58 +00008257 // Vector-related lowering.
8258 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
8259 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8260 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
8261 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Hal Finkel5c0d1452014-03-30 13:22:59 +00008262 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Hal Finkelc93a9a22015-02-25 01:06:45 +00008263 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00008264 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008265
Hal Finkel25c19922013-05-15 21:37:41 +00008266 // For counter-based loop handling.
8267 case ISD::INTRINSIC_W_CHAIN: return SDValue();
8268
Chris Lattnerf6a81562007-12-08 06:59:59 +00008269 // Frame & Return address.
8270 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008271 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00008272 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00008273}
8274
Duncan Sands6ed40142008-12-01 11:39:25 +00008275void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
8276 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00008277 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008278 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00008279 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00008280 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008281 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkelbbdee932014-12-02 22:01:00 +00008282 case ISD::READCYCLECOUNTER: {
8283 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
8284 SDValue RTB = DAG.getNode(PPCISD::READ_TIME_BASE, dl, VTs, N->getOperand(0));
8285
8286 Results.push_back(RTB);
8287 Results.push_back(RTB.getValue(1));
8288 Results.push_back(RTB.getValue(2));
8289 break;
8290 }
Hal Finkel25c19922013-05-15 21:37:41 +00008291 case ISD::INTRINSIC_W_CHAIN: {
8292 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
8293 Intrinsic::ppc_is_decremented_ctr_nonzero)
8294 break;
8295
8296 assert(N->getValueType(0) == MVT::i1 &&
8297 "Unexpected result type for CTR decrement intrinsic");
Mehdi Amini44ede332015-07-09 02:09:04 +00008298 EVT SVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
8299 N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00008300 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
8301 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00008302 N->getOperand(1));
Hal Finkel25c19922013-05-15 21:37:41 +00008303
8304 Results.push_back(NewInt);
8305 Results.push_back(NewInt.getValue(1));
8306 break;
8307 }
Roman Divacky4394e682011-06-28 15:30:42 +00008308 case ISD::VAARG: {
Eric Christophercccae792015-01-30 22:02:31 +00008309 if (!Subtarget.isSVR4ABI() || Subtarget.isPPC64())
Roman Divacky4394e682011-06-28 15:30:42 +00008310 return;
8311
8312 EVT VT = N->getValueType(0);
8313
8314 if (VT == MVT::i64) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008315 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, Subtarget);
Roman Divacky4394e682011-06-28 15:30:42 +00008316
8317 Results.push_back(NewNode);
8318 Results.push_back(NewNode.getValue(1));
8319 }
8320 return;
8321 }
Duncan Sands6ed40142008-12-01 11:39:25 +00008322 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00008323 assert(N->getValueType(0) == MVT::ppcf128);
8324 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008325 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00008326 MVT::f64, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008327 DAG.getIntPtrConstant(0, dl));
Dale Johannesenf80493b2009-02-05 22:07:54 +00008328 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00008329 MVT::f64, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008330 DAG.getIntPtrConstant(1, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00008331
Ulrich Weigand874fc622013-03-26 10:56:22 +00008332 // Add the two halves of the long double in round-to-zero mode.
8333 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00008334
8335 // We know the low half is about to be thrown away, so just use something
8336 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00008337 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00008338 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00008339 return;
Duncan Sands2a287912008-07-19 16:26:02 +00008340 }
Duncan Sands6ed40142008-12-01 11:39:25 +00008341 case ISD::FP_TO_SINT:
Hal Finkel93138502015-04-10 03:39:00 +00008342 case ISD::FP_TO_UINT:
Bill Schmidt41221692013-07-09 18:50:20 +00008343 // LowerFP_TO_INT() can only handle f32 and f64.
8344 if (N->getOperand(0).getValueType() == MVT::ppcf128)
8345 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00008346 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00008347 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00008348 }
8349}
8350
Chris Lattner4211ca92006-04-14 06:01:58 +00008351//===----------------------------------------------------------------------===//
8352// Other Lowering Code
8353//===----------------------------------------------------------------------===//
8354
Robin Morisset22129962014-09-23 20:46:49 +00008355static Instruction* callIntrinsic(IRBuilder<> &Builder, Intrinsic::ID Id) {
8356 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
8357 Function *Func = Intrinsic::getDeclaration(M, Id);
David Blaikieff6409d2015-05-18 22:13:54 +00008358 return Builder.CreateCall(Func, {});
Robin Morisset22129962014-09-23 20:46:49 +00008359}
8360
8361// The mappings for emitLeading/TrailingFence is taken from
8362// http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
8363Instruction* PPCTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
8364 AtomicOrdering Ord, bool IsStore,
8365 bool IsLoad) const {
JF Bastien800f87a2016-04-06 21:19:33 +00008366 if (Ord == AtomicOrdering::SequentiallyConsistent)
Robin Morisset22129962014-09-23 20:46:49 +00008367 return callIntrinsic(Builder, Intrinsic::ppc_sync);
JF Bastien800f87a2016-04-06 21:19:33 +00008368 if (isReleaseOrStronger(Ord))
Robin Morisset22129962014-09-23 20:46:49 +00008369 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
David Blaikieff6409d2015-05-18 22:13:54 +00008370 return nullptr;
Robin Morisset22129962014-09-23 20:46:49 +00008371}
8372
8373Instruction* PPCTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
8374 AtomicOrdering Ord, bool IsStore,
8375 bool IsLoad) const {
JF Bastien800f87a2016-04-06 21:19:33 +00008376 if (IsLoad && isAcquireOrStronger(Ord))
Robin Morisset22129962014-09-23 20:46:49 +00008377 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
8378 // FIXME: this is too conservative, a dependent branch + isync is enough.
8379 // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html and
8380 // http://www.rdrop.com/users/paulmck/scalability/paper/N2745r.2011.03.04a.html
8381 // and http://www.cl.cam.ac.uk/~pes20/cppppc/ for justification.
David Blaikieff6409d2015-05-18 22:13:54 +00008382 return nullptr;
Robin Morisset22129962014-09-23 20:46:49 +00008383}
8384
Chris Lattner9b577f12005-08-26 21:23:58 +00008385MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00008386PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008387 unsigned AtomicSize,
8388 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008389 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christophercccae792015-01-30 22:02:31 +00008390 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Dale Johannesend4eb0522008-08-25 22:34:37 +00008391
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008392 auto LoadMnemonic = PPC::LDARX;
8393 auto StoreMnemonic = PPC::STDCX;
8394 switch (AtomicSize) {
8395 default:
8396 llvm_unreachable("Unexpected size of atomic entity");
8397 case 1:
8398 LoadMnemonic = PPC::LBARX;
8399 StoreMnemonic = PPC::STBCX;
8400 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4");
8401 break;
8402 case 2:
8403 LoadMnemonic = PPC::LHARX;
8404 StoreMnemonic = PPC::STHCX;
8405 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4");
8406 break;
8407 case 4:
8408 LoadMnemonic = PPC::LWARX;
8409 StoreMnemonic = PPC::STWCX;
8410 break;
8411 case 8:
8412 LoadMnemonic = PPC::LDARX;
8413 StoreMnemonic = PPC::STDCX;
8414 break;
8415 }
8416
Dale Johannesend4eb0522008-08-25 22:34:37 +00008417 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8418 MachineFunction *F = BB->getParent();
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00008419 MachineFunction::iterator It = ++BB->getIterator();
Dale Johannesend4eb0522008-08-25 22:34:37 +00008420
8421 unsigned dest = MI->getOperand(0).getReg();
8422 unsigned ptrA = MI->getOperand(1).getReg();
8423 unsigned ptrB = MI->getOperand(2).getReg();
8424 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00008425 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00008426
8427 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
8428 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
8429 F->insert(It, loopMBB);
8430 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008431 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008432 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008433 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008434
8435 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008436 unsigned TmpReg = (!BinOpcode) ? incr :
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008437 RegInfo.createVirtualRegister( AtomicSize == 8 ? &PPC::G8RCRegClass
Craig Topper61e88f42014-11-21 05:58:21 +00008438 : &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008439
8440 // thisMBB:
8441 // ...
8442 // fallthrough --> loopMBB
8443 BB->addSuccessor(loopMBB);
8444
8445 // loopMBB:
8446 // l[wd]arx dest, ptr
8447 // add r0, dest, incr
8448 // st[wd]cx. r0, ptr
8449 // bne- loopMBB
8450 // fallthrough --> exitMBB
8451 BB = loopMBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008452 BuildMI(BB, dl, TII->get(LoadMnemonic), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00008453 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008454 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008455 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008456 BuildMI(BB, dl, TII->get(StoreMnemonic))
Dale Johannesend4eb0522008-08-25 22:34:37 +00008457 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008458 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00008459 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008460 BB->addSuccessor(loopMBB);
8461 BB->addSuccessor(exitMBB);
8462
8463 // exitMBB:
8464 // ...
8465 BB = exitMBB;
8466 return BB;
8467}
8468
8469MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00008470PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00008471 MachineBasicBlock *BB,
8472 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00008473 unsigned BinOpcode) const {
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008474 // If we support part-word atomic mnemonics, just use them
8475 if (Subtarget.hasPartwordAtomics())
8476 return EmitAtomicBinary(MI, BB, is8bit ? 1 : 2, BinOpcode);
8477
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008478 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christophercccae792015-01-30 22:02:31 +00008479 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Dale Johannesena32affb2008-08-28 17:53:09 +00008480 // In 64 bit mode we have to use 64 bits for addresses, even though the
8481 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
8482 // registers without caring whether they're 32 or 64, but here we're
8483 // doing actual arithmetic on the addresses.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008484 bool is64bit = Subtarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00008485 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00008486
8487 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8488 MachineFunction *F = BB->getParent();
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00008489 MachineFunction::iterator It = ++BB->getIterator();
Dale Johannesena32affb2008-08-28 17:53:09 +00008490
8491 unsigned dest = MI->getOperand(0).getReg();
8492 unsigned ptrA = MI->getOperand(1).getReg();
8493 unsigned ptrB = MI->getOperand(2).getReg();
8494 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00008495 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00008496
8497 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
8498 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
8499 F->insert(It, loopMBB);
8500 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008501 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008502 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008503 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00008504
8505 MachineRegisterInfo &RegInfo = F->getRegInfo();
Craig Topper61e88f42014-11-21 05:58:21 +00008506 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass
8507 : &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00008508 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
8509 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
8510 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
8511 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
8512 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
8513 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
8514 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
8515 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
8516 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
8517 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008518 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00008519 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008520 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00008521
8522 // thisMBB:
8523 // ...
8524 // fallthrough --> loopMBB
8525 BB->addSuccessor(loopMBB);
8526
8527 // The 4-byte load must be aligned, while a char or short may be
8528 // anywhere in the word. Hence all this nasty bookkeeping code.
8529 // add ptr1, ptrA, ptrB [copy if ptrA==0]
8530 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00008531 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00008532 // rlwinm ptr, ptr1, 0, 0, 29
8533 // slw incr2, incr, shift
8534 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
8535 // slw mask, mask2, shift
8536 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00008537 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008538 // add tmp, tmpDest, incr2
8539 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00008540 // and tmp3, tmp, mask
8541 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00008542 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00008543 // bne- loopMBB
8544 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008545 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008546 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00008547 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008548 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008549 .addReg(ptrA).addReg(ptrB);
8550 } else {
8551 Ptr1Reg = ptrB;
8552 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008553 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008554 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008555 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008556 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
8557 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008558 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008559 .addReg(Ptr1Reg).addImm(0).addImm(61);
8560 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00008561 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008562 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008563 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008564 .addReg(incr).addReg(ShiftReg);
8565 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008566 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00008567 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00008568 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
8569 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00008570 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008571 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008572 .addReg(Mask2Reg).addReg(ShiftReg);
8573
8574 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00008575 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008576 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008577 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008578 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008579 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008580 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008581 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008582 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008583 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008584 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008585 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00008586 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008587 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008588 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00008589 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00008590 BB->addSuccessor(loopMBB);
8591 BB->addSuccessor(exitMBB);
8592
8593 // exitMBB:
8594 // ...
8595 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00008596 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
8597 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00008598 return BB;
8599}
8600
Hal Finkel756810f2013-03-21 21:37:52 +00008601llvm::MachineBasicBlock*
8602PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
8603 MachineBasicBlock *MBB) const {
8604 DebugLoc DL = MI->getDebugLoc();
Eric Christophercccae792015-01-30 22:02:31 +00008605 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00008606
8607 MachineFunction *MF = MBB->getParent();
8608 MachineRegisterInfo &MRI = MF->getRegInfo();
8609
8610 const BasicBlock *BB = MBB->getBasicBlock();
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00008611 MachineFunction::iterator I = ++MBB->getIterator();
Hal Finkel756810f2013-03-21 21:37:52 +00008612
8613 // Memory Reference
8614 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
8615 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
8616
8617 unsigned DstReg = MI->getOperand(0).getReg();
8618 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
8619 assert(RC->hasType(MVT::i32) && "Invalid destination!");
8620 unsigned mainDstReg = MRI.createVirtualRegister(RC);
8621 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
8622
Mehdi Amini44ede332015-07-09 02:09:04 +00008623 MVT PVT = getPointerTy(MF->getDataLayout());
Hal Finkel756810f2013-03-21 21:37:52 +00008624 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
8625 "Invalid Pointer Size!");
8626 // For v = setjmp(buf), we generate
8627 //
8628 // thisMBB:
8629 // SjLjSetup mainMBB
8630 // bl mainMBB
8631 // v_restore = 1
8632 // b sinkMBB
8633 //
8634 // mainMBB:
8635 // buf[LabelOffset] = LR
8636 // v_main = 0
8637 //
8638 // sinkMBB:
8639 // v = phi(main, restore)
8640 //
8641
8642 MachineBasicBlock *thisMBB = MBB;
8643 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
8644 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
8645 MF->insert(I, mainMBB);
8646 MF->insert(I, sinkMBB);
8647
8648 MachineInstrBuilder MIB;
8649
8650 // Transfer the remainder of BB and its successor edges to sinkMBB.
8651 sinkMBB->splice(sinkMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008652 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Hal Finkel756810f2013-03-21 21:37:52 +00008653 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
8654
8655 // Note that the structure of the jmp_buf used here is not compatible
8656 // with that used by libc, and is not designed to be. Specifically, it
8657 // stores only those 'reserved' registers that LLVM does not otherwise
8658 // understand how to spill. Also, by convention, by the time this
8659 // intrinsic is called, Clang has already stored the frame address in the
8660 // first slot of the buffer and stack address in the third. Following the
8661 // X86 target code, we'll store the jump address in the second slot. We also
8662 // need to save the TOC pointer (R2) to handle jumps between shared
8663 // libraries, and that will be stored in the fourth slot. The thread
8664 // identifier (R13) is not affected.
8665
8666 // thisMBB:
8667 const int64_t LabelOffset = 1 * PVT.getStoreSize();
8668 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00008669 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00008670
8671 // Prepare IP either in reg.
8672 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
8673 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
8674 unsigned BufReg = MI->getOperand(1).getReg();
8675
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008676 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI()) {
Hal Finkele6698d52015-02-01 15:03:28 +00008677 setUsesTOCBasePtr(*MBB->getParent());
Hal Finkel756810f2013-03-21 21:37:52 +00008678 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
8679 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008680 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008681 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00008682 MIB.setMemRefs(MMOBegin, MMOEnd);
8683 }
8684
Hal Finkelf05d6c72013-07-17 23:50:51 +00008685 // Naked functions never have a base pointer, and so we use r1. For all
8686 // other functions, this decision must be delayed until during PEI.
8687 unsigned BaseReg;
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +00008688 if (MF->getFunction()->hasFnAttribute(Attribute::Naked))
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008689 BaseReg = Subtarget.isPPC64() ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00008690 else
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008691 BaseReg = Subtarget.isPPC64() ? PPC::BP8 : PPC::BP;
Hal Finkelf05d6c72013-07-17 23:50:51 +00008692
8693 MIB = BuildMI(*thisMBB, MI, DL,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008694 TII->get(Subtarget.isPPC64() ? PPC::STD : PPC::STW))
Eric Christophercccae792015-01-30 22:02:31 +00008695 .addReg(BaseReg)
8696 .addImm(BPOffset)
8697 .addReg(BufReg);
Hal Finkelf05d6c72013-07-17 23:50:51 +00008698 MIB.setMemRefs(MMOBegin, MMOEnd);
8699
Hal Finkel756810f2013-03-21 21:37:52 +00008700 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00008701 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Eric Christophercccae792015-01-30 22:02:31 +00008702 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo();
Bill Wendling5e7656b2013-06-07 07:55:53 +00008703 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00008704
8705 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
8706
8707 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
8708 .addMBB(mainMBB);
8709 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
8710
Cong Hou1938f2e2015-11-24 08:51:23 +00008711 thisMBB->addSuccessor(mainMBB, BranchProbability::getZero());
8712 thisMBB->addSuccessor(sinkMBB, BranchProbability::getOne());
Hal Finkel756810f2013-03-21 21:37:52 +00008713
8714 // mainMBB:
8715 // mainDstReg = 0
Eric Christophercccae792015-01-30 22:02:31 +00008716 MIB =
8717 BuildMI(mainMBB, DL,
8718 TII->get(Subtarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
Hal Finkel756810f2013-03-21 21:37:52 +00008719
8720 // Store IP
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008721 if (Subtarget.isPPC64()) {
Hal Finkel756810f2013-03-21 21:37:52 +00008722 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
8723 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008724 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008725 .addReg(BufReg);
8726 } else {
8727 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
8728 .addReg(LabelReg)
8729 .addImm(LabelOffset)
8730 .addReg(BufReg);
8731 }
8732
8733 MIB.setMemRefs(MMOBegin, MMOEnd);
8734
8735 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
8736 mainMBB->addSuccessor(sinkMBB);
8737
8738 // sinkMBB:
8739 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
8740 TII->get(PPC::PHI), DstReg)
8741 .addReg(mainDstReg).addMBB(mainMBB)
8742 .addReg(restoreDstReg).addMBB(thisMBB);
8743
8744 MI->eraseFromParent();
8745 return sinkMBB;
8746}
8747
8748MachineBasicBlock *
8749PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
8750 MachineBasicBlock *MBB) const {
8751 DebugLoc DL = MI->getDebugLoc();
Eric Christophercccae792015-01-30 22:02:31 +00008752 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00008753
8754 MachineFunction *MF = MBB->getParent();
8755 MachineRegisterInfo &MRI = MF->getRegInfo();
8756
8757 // Memory Reference
8758 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
8759 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
8760
Mehdi Amini44ede332015-07-09 02:09:04 +00008761 MVT PVT = getPointerTy(MF->getDataLayout());
Hal Finkel756810f2013-03-21 21:37:52 +00008762 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
8763 "Invalid Pointer Size!");
8764
8765 const TargetRegisterClass *RC =
8766 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
8767 unsigned Tmp = MRI.createVirtualRegister(RC);
8768 // Since FP is only updated here but NOT referenced, it's treated as GPR.
8769 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
8770 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Eric Christophercccae792015-01-30 22:02:31 +00008771 unsigned BP =
8772 (PVT == MVT::i64)
8773 ? PPC::X30
8774 : (Subtarget.isSVR4ABI() &&
8775 MF->getTarget().getRelocationModel() == Reloc::PIC_
8776 ? PPC::R29
8777 : PPC::R30);
Hal Finkel756810f2013-03-21 21:37:52 +00008778
8779 MachineInstrBuilder MIB;
8780
8781 const int64_t LabelOffset = 1 * PVT.getStoreSize();
8782 const int64_t SPOffset = 2 * PVT.getStoreSize();
8783 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00008784 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00008785
8786 unsigned BufReg = MI->getOperand(0).getReg();
8787
8788 // Reload FP (the jumped-to function may not have had a
8789 // frame pointer, and if so, then its r31 will be restored
8790 // as necessary).
8791 if (PVT == MVT::i64) {
8792 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
8793 .addImm(0)
8794 .addReg(BufReg);
8795 } else {
8796 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
8797 .addImm(0)
8798 .addReg(BufReg);
8799 }
8800 MIB.setMemRefs(MMOBegin, MMOEnd);
8801
8802 // Reload IP
8803 if (PVT == MVT::i64) {
8804 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008805 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008806 .addReg(BufReg);
8807 } else {
8808 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
8809 .addImm(LabelOffset)
8810 .addReg(BufReg);
8811 }
8812 MIB.setMemRefs(MMOBegin, MMOEnd);
8813
8814 // Reload SP
8815 if (PVT == MVT::i64) {
8816 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008817 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008818 .addReg(BufReg);
8819 } else {
8820 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
8821 .addImm(SPOffset)
8822 .addReg(BufReg);
8823 }
8824 MIB.setMemRefs(MMOBegin, MMOEnd);
8825
Hal Finkelf05d6c72013-07-17 23:50:51 +00008826 // Reload BP
8827 if (PVT == MVT::i64) {
8828 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
8829 .addImm(BPOffset)
8830 .addReg(BufReg);
8831 } else {
8832 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
8833 .addImm(BPOffset)
8834 .addReg(BufReg);
8835 }
8836 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00008837
8838 // Reload TOC
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008839 if (PVT == MVT::i64 && Subtarget.isSVR4ABI()) {
Hal Finkele6698d52015-02-01 15:03:28 +00008840 setUsesTOCBasePtr(*MBB->getParent());
Hal Finkel756810f2013-03-21 21:37:52 +00008841 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008842 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008843 .addReg(BufReg);
8844
8845 MIB.setMemRefs(MMOBegin, MMOEnd);
8846 }
8847
8848 // Jump
8849 BuildMI(*MBB, MI, DL,
8850 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
8851 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
8852
8853 MI->eraseFromParent();
8854 return MBB;
8855}
8856
Dale Johannesena32affb2008-08-28 17:53:09 +00008857MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00008858PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00008859 MachineBasicBlock *BB) const {
Hal Finkel934361a2015-01-14 01:07:51 +00008860 if (MI->getOpcode() == TargetOpcode::STACKMAP ||
Hal Finkelaf519932015-01-19 07:20:27 +00008861 MI->getOpcode() == TargetOpcode::PATCHPOINT) {
8862 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI() &&
8863 MI->getOpcode() == TargetOpcode::PATCHPOINT) {
8864 // Call lowering should have added an r2 operand to indicate a dependence
8865 // on the TOC base pointer value. It can't however, because there is no
8866 // way to mark the dependence as implicit there, and so the stackmap code
8867 // will confuse it with a regular operand. Instead, add the dependence
8868 // here.
Hal Finkele6698d52015-02-01 15:03:28 +00008869 setUsesTOCBasePtr(*BB->getParent());
Hal Finkelaf519932015-01-19 07:20:27 +00008870 MI->addOperand(MachineOperand::CreateReg(PPC::X2, false, true));
8871 }
8872
Hal Finkel934361a2015-01-14 01:07:51 +00008873 return emitPatchPoint(MI, BB);
Hal Finkelaf519932015-01-19 07:20:27 +00008874 }
Hal Finkel934361a2015-01-14 01:07:51 +00008875
Hal Finkel756810f2013-03-21 21:37:52 +00008876 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
8877 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
8878 return emitEHSjLjSetJmp(MI, BB);
8879 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
8880 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
8881 return emitEHSjLjLongJmp(MI, BB);
8882 }
8883
Eric Christophercccae792015-01-30 22:02:31 +00008884 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00008885
8886 // To "insert" these instructions we actually have to insert their
8887 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00008888 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00008889 MachineFunction::iterator It = ++BB->getIterator();
Evan Cheng32e376f2008-07-12 02:23:19 +00008890
Dan Gohman3b460302008-07-07 23:14:23 +00008891 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00008892
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008893 if (Subtarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Eric Christophercccae792015-01-30 22:02:31 +00008894 MI->getOpcode() == PPC::SELECT_CC_I8 ||
8895 MI->getOpcode() == PPC::SELECT_I4 ||
8896 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00008897 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00008898 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
8899 MI->getOpcode() == PPC::SELECT_CC_I8)
8900 Cond.push_back(MI->getOperand(4));
8901 else
8902 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00008903 Cond.push_back(MI->getOperand(1));
8904
Hal Finkel460e94d2012-06-22 23:10:08 +00008905 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00008906 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
8907 Cond, MI->getOperand(2).getReg(),
8908 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00008909 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
8910 MI->getOpcode() == PPC::SELECT_CC_I8 ||
8911 MI->getOpcode() == PPC::SELECT_CC_F4 ||
8912 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008913 MI->getOpcode() == PPC::SELECT_CC_QFRC ||
8914 MI->getOpcode() == PPC::SELECT_CC_QSRC ||
8915 MI->getOpcode() == PPC::SELECT_CC_QBRC ||
Hal Finkel940ab932014-02-28 00:27:01 +00008916 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00008917 MI->getOpcode() == PPC::SELECT_CC_VSFRC ||
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00008918 MI->getOpcode() == PPC::SELECT_CC_VSSRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008919 MI->getOpcode() == PPC::SELECT_CC_VSRC ||
Hal Finkel940ab932014-02-28 00:27:01 +00008920 MI->getOpcode() == PPC::SELECT_I4 ||
8921 MI->getOpcode() == PPC::SELECT_I8 ||
8922 MI->getOpcode() == PPC::SELECT_F4 ||
8923 MI->getOpcode() == PPC::SELECT_F8 ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008924 MI->getOpcode() == PPC::SELECT_QFRC ||
8925 MI->getOpcode() == PPC::SELECT_QSRC ||
8926 MI->getOpcode() == PPC::SELECT_QBRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008927 MI->getOpcode() == PPC::SELECT_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00008928 MI->getOpcode() == PPC::SELECT_VSFRC ||
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00008929 MI->getOpcode() == PPC::SELECT_VSSRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008930 MI->getOpcode() == PPC::SELECT_VSRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00008931 // The incoming instruction knows the destination vreg to set, the
8932 // condition code register to branch on, the true/false values to
8933 // select between, and a branch opcode to use.
8934
8935 // thisMBB:
8936 // ...
8937 // TrueVal = ...
8938 // cmpTY ccX, r1, r2
8939 // bCC copy1MBB
8940 // fallthrough --> copy0MBB
8941 MachineBasicBlock *thisMBB = BB;
8942 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8943 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008944 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00008945 F->insert(It, copy0MBB);
8946 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008947
8948 // Transfer the remainder of BB and its successor edges to sinkMBB.
8949 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008950 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008951 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8952
Evan Cheng32e376f2008-07-12 02:23:19 +00008953 // Next, add the true and fallthrough blocks as its successors.
8954 BB->addSuccessor(copy0MBB);
8955 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008956
Hal Finkel940ab932014-02-28 00:27:01 +00008957 if (MI->getOpcode() == PPC::SELECT_I4 ||
8958 MI->getOpcode() == PPC::SELECT_I8 ||
8959 MI->getOpcode() == PPC::SELECT_F4 ||
8960 MI->getOpcode() == PPC::SELECT_F8 ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008961 MI->getOpcode() == PPC::SELECT_QFRC ||
8962 MI->getOpcode() == PPC::SELECT_QSRC ||
8963 MI->getOpcode() == PPC::SELECT_QBRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008964 MI->getOpcode() == PPC::SELECT_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00008965 MI->getOpcode() == PPC::SELECT_VSFRC ||
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00008966 MI->getOpcode() == PPC::SELECT_VSSRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008967 MI->getOpcode() == PPC::SELECT_VSRC) {
Hal Finkel940ab932014-02-28 00:27:01 +00008968 BuildMI(BB, dl, TII->get(PPC::BC))
8969 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
8970 } else {
8971 unsigned SelectPred = MI->getOperand(4).getImm();
8972 BuildMI(BB, dl, TII->get(PPC::BCC))
8973 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
8974 }
Dan Gohman34396292010-07-06 20:24:04 +00008975
Evan Cheng32e376f2008-07-12 02:23:19 +00008976 // copy0MBB:
8977 // %FalseValue = ...
8978 // # fallthrough to sinkMBB
8979 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008980
Evan Cheng32e376f2008-07-12 02:23:19 +00008981 // Update machine-CFG edges
8982 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008983
Evan Cheng32e376f2008-07-12 02:23:19 +00008984 // sinkMBB:
8985 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8986 // ...
8987 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00008988 BuildMI(*BB, BB->begin(), dl,
8989 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00008990 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
8991 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Hal Finkelbbdee932014-12-02 22:01:00 +00008992 } else if (MI->getOpcode() == PPC::ReadTB) {
8993 // To read the 64-bit time-base register on a 32-bit target, we read the
8994 // two halves. Should the counter have wrapped while it was being read, we
8995 // need to try again.
8996 // ...
8997 // readLoop:
8998 // mfspr Rx,TBU # load from TBU
8999 // mfspr Ry,TB # load from TB
9000 // mfspr Rz,TBU # load from TBU
NAKAMURA Takumibf9cc7f2015-09-22 11:10:08 +00009001 // cmpw crX,Rx,Rz # check if 'old'='new'
Hal Finkelbbdee932014-12-02 22:01:00 +00009002 // bne readLoop # branch if they're not equal
9003 // ...
9004
9005 MachineBasicBlock *readMBB = F->CreateMachineBasicBlock(LLVM_BB);
9006 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
9007 DebugLoc dl = MI->getDebugLoc();
9008 F->insert(It, readMBB);
9009 F->insert(It, sinkMBB);
9010
9011 // Transfer the remainder of BB and its successor edges to sinkMBB.
9012 sinkMBB->splice(sinkMBB->begin(), BB,
9013 std::next(MachineBasicBlock::iterator(MI)), BB->end());
9014 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
9015
9016 BB->addSuccessor(readMBB);
9017 BB = readMBB;
9018
9019 MachineRegisterInfo &RegInfo = F->getRegInfo();
9020 unsigned ReadAgainReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
9021 unsigned LoReg = MI->getOperand(0).getReg();
9022 unsigned HiReg = MI->getOperand(1).getReg();
9023
9024 BuildMI(BB, dl, TII->get(PPC::MFSPR), HiReg).addImm(269);
9025 BuildMI(BB, dl, TII->get(PPC::MFSPR), LoReg).addImm(268);
9026 BuildMI(BB, dl, TII->get(PPC::MFSPR), ReadAgainReg).addImm(269);
9027
9028 unsigned CmpReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
9029
9030 BuildMI(BB, dl, TII->get(PPC::CMPW), CmpReg)
9031 .addReg(HiReg).addReg(ReadAgainReg);
9032 BuildMI(BB, dl, TII->get(PPC::BCC))
9033 .addImm(PPC::PRED_NE).addReg(CmpReg).addMBB(readMBB);
9034
9035 BB->addSuccessor(readMBB);
9036 BB->addSuccessor(sinkMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009037 }
Dale Johannesena32affb2008-08-28 17:53:09 +00009038 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
9039 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
9040 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
9041 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009042 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009043 BB = EmitAtomicBinary(MI, BB, 4, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009044 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009045 BB = EmitAtomicBinary(MI, BB, 8, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00009046
9047 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
9048 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
9049 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
9050 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009051 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009052 BB = EmitAtomicBinary(MI, BB, 4, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009053 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009054 BB = EmitAtomicBinary(MI, BB, 8, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00009055
9056 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
9057 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
9058 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
9059 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009060 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009061 BB = EmitAtomicBinary(MI, BB, 4, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009062 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009063 BB = EmitAtomicBinary(MI, BB, 8, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00009064
9065 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
9066 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
9067 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
9068 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009069 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009070 BB = EmitAtomicBinary(MI, BB, 4, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009071 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009072 BB = EmitAtomicBinary(MI, BB, 8, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00009073
9074 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00009075 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::NAND);
Dale Johannesena32affb2008-08-28 17:53:09 +00009076 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00009077 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009078 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009079 BB = EmitAtomicBinary(MI, BB, 4, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009080 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009081 BB = EmitAtomicBinary(MI, BB, 8, PPC::NAND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00009082
9083 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
9084 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
9085 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
9086 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009087 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009088 BB = EmitAtomicBinary(MI, BB, 4, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00009089 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009090 BB = EmitAtomicBinary(MI, BB, 8, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00009091
Dale Johannesenf0a88d62008-08-29 18:29:46 +00009092 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
9093 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
9094 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
9095 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
9096 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009097 BB = EmitAtomicBinary(MI, BB, 4, 0);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00009098 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009099 BB = EmitAtomicBinary(MI, BB, 8, 0);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00009100
Evan Cheng32e376f2008-07-12 02:23:19 +00009101 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009102 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64 ||
9103 (Subtarget.hasPartwordAtomics() &&
9104 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8) ||
9105 (Subtarget.hasPartwordAtomics() &&
9106 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16)) {
Evan Cheng32e376f2008-07-12 02:23:19 +00009107 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
9108
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009109 auto LoadMnemonic = PPC::LDARX;
9110 auto StoreMnemonic = PPC::STDCX;
9111 switch(MI->getOpcode()) {
9112 default:
9113 llvm_unreachable("Compare and swap of unknown size");
9114 case PPC::ATOMIC_CMP_SWAP_I8:
9115 LoadMnemonic = PPC::LBARX;
9116 StoreMnemonic = PPC::STBCX;
9117 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics.");
9118 break;
9119 case PPC::ATOMIC_CMP_SWAP_I16:
9120 LoadMnemonic = PPC::LHARX;
9121 StoreMnemonic = PPC::STHCX;
9122 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics.");
9123 break;
9124 case PPC::ATOMIC_CMP_SWAP_I32:
9125 LoadMnemonic = PPC::LWARX;
9126 StoreMnemonic = PPC::STWCX;
9127 break;
9128 case PPC::ATOMIC_CMP_SWAP_I64:
9129 LoadMnemonic = PPC::LDARX;
9130 StoreMnemonic = PPC::STDCX;
9131 break;
9132 }
Evan Cheng32e376f2008-07-12 02:23:19 +00009133 unsigned dest = MI->getOperand(0).getReg();
9134 unsigned ptrA = MI->getOperand(1).getReg();
9135 unsigned ptrB = MI->getOperand(2).getReg();
9136 unsigned oldval = MI->getOperand(3).getReg();
9137 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00009138 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00009139
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009140 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
9141 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
9142 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009143 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009144 F->insert(It, loop1MBB);
9145 F->insert(It, loop2MBB);
9146 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009147 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00009148 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00009149 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00009150 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009151
9152 // thisMBB:
9153 // ...
9154 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009155 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009156
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009157 // loop1MBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009158 // l[bhwd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009159 // cmp[wd] dest, oldval
9160 // bne- midMBB
9161 // loop2MBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009162 // st[bhwd]cx. newval, ptr
Evan Cheng32e376f2008-07-12 02:23:19 +00009163 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009164 // b exitBB
9165 // midMBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009166 // st[bhwd]cx. dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009167 // exitBB:
9168 BB = loop1MBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009169 BuildMI(BB, dl, TII->get(LoadMnemonic), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00009170 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009171 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00009172 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009173 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009174 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
9175 BB->addSuccessor(loop2MBB);
9176 BB->addSuccessor(midMBB);
9177
9178 BB = loop2MBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009179 BuildMI(BB, dl, TII->get(StoreMnemonic))
Evan Cheng32e376f2008-07-12 02:23:19 +00009180 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009181 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009182 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009183 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009184 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00009185 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009186
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009187 BB = midMBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00009188 BuildMI(BB, dl, TII->get(StoreMnemonic))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00009189 .addReg(dest).addReg(ptrA).addReg(ptrB);
9190 BB->addSuccessor(exitMBB);
9191
Evan Cheng32e376f2008-07-12 02:23:19 +00009192 // exitMBB:
9193 // ...
9194 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00009195 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
9196 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
9197 // We must use 64-bit registers for addresses when targeting 64-bit,
9198 // since we're actually doing arithmetic on them. Other registers
9199 // can be 32-bit.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009200 bool is64bit = Subtarget.isPPC64();
Dale Johannesen340d2642008-08-30 00:08:53 +00009201 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
9202
9203 unsigned dest = MI->getOperand(0).getReg();
9204 unsigned ptrA = MI->getOperand(1).getReg();
9205 unsigned ptrB = MI->getOperand(2).getReg();
9206 unsigned oldval = MI->getOperand(3).getReg();
9207 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00009208 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00009209
9210 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
9211 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
9212 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
9213 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
9214 F->insert(It, loop1MBB);
9215 F->insert(It, loop2MBB);
9216 F->insert(It, midMBB);
9217 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00009218 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00009219 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00009220 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00009221
9222 MachineRegisterInfo &RegInfo = F->getRegInfo();
Craig Topper61e88f42014-11-21 05:58:21 +00009223 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass
9224 : &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00009225 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
9226 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
9227 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
9228 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
9229 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
9230 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
9231 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
9232 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
9233 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
9234 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
9235 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
9236 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
9237 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
9238 unsigned Ptr1Reg;
9239 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00009240 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00009241 // thisMBB:
9242 // ...
9243 // fallthrough --> loopMBB
9244 BB->addSuccessor(loop1MBB);
9245
9246 // The 4-byte load must be aligned, while a char or short may be
9247 // anywhere in the word. Hence all this nasty bookkeeping code.
9248 // add ptr1, ptrA, ptrB [copy if ptrA==0]
9249 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00009250 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00009251 // rlwinm ptr, ptr1, 0, 0, 29
9252 // slw newval2, newval, shift
9253 // slw oldval2, oldval,shift
9254 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
9255 // slw mask, mask2, shift
9256 // and newval3, newval2, mask
9257 // and oldval3, oldval2, mask
9258 // loop1MBB:
9259 // lwarx tmpDest, ptr
9260 // and tmp, tmpDest, mask
9261 // cmpw tmp, oldval3
9262 // bne- midMBB
9263 // loop2MBB:
9264 // andc tmp2, tmpDest, mask
9265 // or tmp4, tmp2, newval3
9266 // stwcx. tmp4, ptr
9267 // bne- loop1MBB
9268 // b exitBB
9269 // midMBB:
9270 // stwcx. tmpDest, ptr
9271 // exitBB:
9272 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00009273 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00009274 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009275 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009276 .addReg(ptrA).addReg(ptrB);
9277 } else {
9278 Ptr1Reg = ptrB;
9279 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00009280 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009281 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009282 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009283 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
9284 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00009285 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009286 .addReg(Ptr1Reg).addImm(0).addImm(61);
9287 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00009288 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009289 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009290 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009291 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009292 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009293 .addReg(oldval).addReg(ShiftReg);
9294 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00009295 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00009296 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00009297 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
9298 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
9299 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00009300 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00009301 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009302 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009303 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009304 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009305 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00009306 .addReg(OldVal2Reg).addReg(MaskReg);
9307
9308 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00009309 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00009310 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009311 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
9312 .addReg(TmpDestReg).addReg(MaskReg);
9313 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00009314 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009315 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00009316 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
9317 BB->addSuccessor(loop2MBB);
9318 BB->addSuccessor(midMBB);
9319
9320 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00009321 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
9322 .addReg(TmpDestReg).addReg(MaskReg);
9323 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
9324 .addReg(Tmp2Reg).addReg(NewVal3Reg);
9325 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00009326 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009327 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00009328 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00009329 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00009330 BB->addSuccessor(loop1MBB);
9331 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009332
Dale Johannesen340d2642008-08-30 00:08:53 +00009333 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00009334 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00009335 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00009336 BB->addSuccessor(exitMBB);
9337
9338 // exitMBB:
9339 // ...
9340 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00009341 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
9342 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00009343 } else if (MI->getOpcode() == PPC::FADDrtz) {
9344 // This pseudo performs an FADD with rounding mode temporarily forced
9345 // to round-to-zero. We emit this via custom inserter since the FPSCR
9346 // is not modeled at the SelectionDAG level.
9347 unsigned Dest = MI->getOperand(0).getReg();
9348 unsigned Src1 = MI->getOperand(1).getReg();
9349 unsigned Src2 = MI->getOperand(2).getReg();
9350 DebugLoc dl = MI->getDebugLoc();
9351
9352 MachineRegisterInfo &RegInfo = F->getRegInfo();
9353 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
9354
9355 // Save FPSCR value.
9356 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
9357
9358 // Set rounding mode to round-to-zero.
9359 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
9360 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
9361
9362 // Perform addition.
9363 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
9364
9365 // Restore FPSCR value.
Hal Finkel64202162015-01-15 01:00:53 +00009366 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSFb)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00009367 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
9368 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
9369 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
9370 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
9371 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
9372 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
9373 PPC::ANDIo8 : PPC::ANDIo;
9374 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
9375 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
9376
9377 MachineRegisterInfo &RegInfo = F->getRegInfo();
9378 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
9379 &PPC::GPRCRegClass :
9380 &PPC::G8RCRegClass);
9381
9382 DebugLoc dl = MI->getDebugLoc();
9383 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
9384 .addReg(MI->getOperand(1).getReg()).addImm(1);
9385 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
9386 MI->getOperand(0).getReg())
9387 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Kit Barton535e69d2015-03-25 19:36:23 +00009388 } else if (MI->getOpcode() == PPC::TCHECK_RET) {
9389 DebugLoc Dl = MI->getDebugLoc();
9390 MachineRegisterInfo &RegInfo = F->getRegInfo();
9391 unsigned CRReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
9392 BuildMI(*BB, MI, Dl, TII->get(PPC::TCHECK), CRReg);
9393 return BB;
Dale Johannesen340d2642008-08-30 00:08:53 +00009394 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009395 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00009396 }
Chris Lattner9b577f12005-08-26 21:23:58 +00009397
Dan Gohman34396292010-07-06 20:24:04 +00009398 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00009399 return BB;
9400}
9401
Chris Lattner4211ca92006-04-14 06:01:58 +00009402//===----------------------------------------------------------------------===//
9403// Target Optimization Hooks
9404//===----------------------------------------------------------------------===//
9405
Hal Finkelcbf08922015-07-12 02:33:57 +00009406static std::string getRecipOp(const char *Base, EVT VT) {
9407 std::string RecipOp(Base);
9408 if (VT.getScalarType() == MVT::f64)
9409 RecipOp += "d";
9410 else
9411 RecipOp += "f";
9412
9413 if (VT.isVector())
9414 RecipOp = "vec-" + RecipOp;
9415
9416 return RecipOp;
9417}
9418
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009419SDValue PPCTargetLowering::getRsqrtEstimate(SDValue Operand,
9420 DAGCombinerInfo &DCI,
Sanjay Patel957efc232014-10-24 17:02:16 +00009421 unsigned &RefinementSteps,
9422 bool &UseOneConstNR) const {
Sanjay Patelbdf1e382014-09-26 23:01:47 +00009423 EVT VT = Operand.getValueType();
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009424 if ((VT == MVT::f32 && Subtarget.hasFRSQRTES()) ||
Eric Christophercccae792015-01-30 22:02:31 +00009425 (VT == MVT::f64 && Subtarget.hasFRSQRTE()) ||
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009426 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00009427 (VT == MVT::v2f64 && Subtarget.hasVSX()) ||
9428 (VT == MVT::v4f32 && Subtarget.hasQPX()) ||
9429 (VT == MVT::v4f64 && Subtarget.hasQPX())) {
Hal Finkelcbf08922015-07-12 02:33:57 +00009430 TargetRecip Recips = DCI.DAG.getTarget().Options.Reciprocals;
9431 std::string RecipOp = getRecipOp("sqrt", VT);
9432 if (!Recips.isEnabled(RecipOp))
9433 return SDValue();
9434
9435 RefinementSteps = Recips.getRefinementSteps(RecipOp);
Sanjay Patel957efc232014-10-24 17:02:16 +00009436 UseOneConstNR = true;
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009437 return DCI.DAG.getNode(PPCISD::FRSQRTE, SDLoc(Operand), VT, Operand);
Hal Finkel2e103312013-04-03 04:01:11 +00009438 }
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009439 return SDValue();
9440}
9441
9442SDValue PPCTargetLowering::getRecipEstimate(SDValue Operand,
9443 DAGCombinerInfo &DCI,
9444 unsigned &RefinementSteps) const {
9445 EVT VT = Operand.getValueType();
9446 if ((VT == MVT::f32 && Subtarget.hasFRES()) ||
Eric Christophercccae792015-01-30 22:02:31 +00009447 (VT == MVT::f64 && Subtarget.hasFRE()) ||
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009448 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00009449 (VT == MVT::v2f64 && Subtarget.hasVSX()) ||
9450 (VT == MVT::v4f32 && Subtarget.hasQPX()) ||
9451 (VT == MVT::v4f64 && Subtarget.hasQPX())) {
Hal Finkelcbf08922015-07-12 02:33:57 +00009452 TargetRecip Recips = DCI.DAG.getTarget().Options.Reciprocals;
9453 std::string RecipOp = getRecipOp("div", VT);
9454 if (!Recips.isEnabled(RecipOp))
9455 return SDValue();
9456
9457 RefinementSteps = Recips.getRefinementSteps(RecipOp);
Sanjay Patel8fde95c2014-09-30 20:28:48 +00009458 return DCI.DAG.getNode(PPCISD::FRE, SDLoc(Operand), VT, Operand);
9459 }
9460 return SDValue();
Hal Finkel2e103312013-04-03 04:01:11 +00009461}
9462
Sanjay Patel1dd15592015-07-28 23:05:48 +00009463unsigned PPCTargetLowering::combineRepeatedFPDivisors() const {
Hal Finkel360f2132014-11-24 23:45:21 +00009464 // Note: This functionality is used only when unsafe-fp-math is enabled, and
9465 // on cores with reciprocal estimates (which are used when unsafe-fp-math is
9466 // enabled for division), this functionality is redundant with the default
9467 // combiner logic (once the division -> reciprocal/multiply transformation
9468 // has taken place). As a result, this matters more for older cores than for
9469 // newer ones.
9470
9471 // Combine multiple FDIVs with the same divisor into multiple FMULs by the
9472 // reciprocal if there are two or more FDIVs (for embedded cores with only
9473 // one FP pipeline) for three or more FDIVs (for generic OOO cores).
9474 switch (Subtarget.getDarwinDirective()) {
9475 default:
Sanjay Patel1dd15592015-07-28 23:05:48 +00009476 return 3;
Hal Finkel360f2132014-11-24 23:45:21 +00009477 case PPC::DIR_440:
9478 case PPC::DIR_A2:
9479 case PPC::DIR_E500mc:
9480 case PPC::DIR_E5500:
Sanjay Patel1dd15592015-07-28 23:05:48 +00009481 return 2;
Hal Finkel360f2132014-11-24 23:45:21 +00009482 }
9483}
9484
Hal Finkele6702ca2015-09-03 22:37:44 +00009485// isConsecutiveLSLoc needs to work even if all adds have not yet been
9486// collapsed, and so we need to look through chains of them.
9487static void getBaseWithConstantOffset(SDValue Loc, SDValue &Base,
9488 int64_t& Offset, SelectionDAG &DAG) {
9489 if (DAG.isBaseWithConstantOffset(Loc)) {
9490 Base = Loc.getOperand(0);
9491 Offset += cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
9492
9493 // The base might itself be a base plus an offset, and if so, accumulate
9494 // that as well.
9495 getBaseWithConstantOffset(Loc.getOperand(0), Base, Offset, DAG);
9496 }
9497}
9498
Hal Finkel3604bf72014-08-01 01:02:01 +00009499static bool isConsecutiveLSLoc(SDValue Loc, EVT VT, LSBaseSDNode *Base,
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009500 unsigned Bytes, int Dist,
9501 SelectionDAG &DAG) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009502 if (VT.getSizeInBits() / 8 != Bytes)
9503 return false;
9504
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009505 SDValue BaseLoc = Base->getBasePtr();
9506 if (Loc.getOpcode() == ISD::FrameIndex) {
9507 if (BaseLoc.getOpcode() != ISD::FrameIndex)
9508 return false;
9509 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9510 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
9511 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
9512 int FS = MFI->getObjectSize(FI);
9513 int BFS = MFI->getObjectSize(BFI);
9514 if (FS != BFS || FS != (int)Bytes) return false;
9515 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
9516 }
9517
Hal Finkele6702ca2015-09-03 22:37:44 +00009518 SDValue Base1 = Loc, Base2 = BaseLoc;
9519 int64_t Offset1 = 0, Offset2 = 0;
9520 getBaseWithConstantOffset(Loc, Base1, Offset1, DAG);
9521 getBaseWithConstantOffset(BaseLoc, Base2, Offset2, DAG);
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00009522 if (Base1 == Base2 && Offset1 == (Offset2 + Dist * Bytes))
9523 return true;
9524
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009525 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Craig Topper062a2ba2014-04-25 05:30:21 +00009526 const GlobalValue *GV1 = nullptr;
9527 const GlobalValue *GV2 = nullptr;
Hal Finkele6702ca2015-09-03 22:37:44 +00009528 Offset1 = 0;
9529 Offset2 = 0;
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009530 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
9531 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
9532 if (isGA1 && isGA2 && GV1 == GV2)
9533 return Offset1 == (Offset2 + Dist*Bytes);
9534 return false;
9535}
9536
Hal Finkel3604bf72014-08-01 01:02:01 +00009537// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
9538// not enforce equality of the chain operands.
9539static bool isConsecutiveLS(SDNode *N, LSBaseSDNode *Base,
9540 unsigned Bytes, int Dist,
9541 SelectionDAG &DAG) {
9542 if (LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(N)) {
9543 EVT VT = LS->getMemoryVT();
9544 SDValue Loc = LS->getBasePtr();
9545 return isConsecutiveLSLoc(Loc, VT, Base, Bytes, Dist, DAG);
9546 }
9547
9548 if (N->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
9549 EVT VT;
9550 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9551 default: return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00009552 case Intrinsic::ppc_qpx_qvlfd:
9553 case Intrinsic::ppc_qpx_qvlfda:
9554 VT = MVT::v4f64;
9555 break;
9556 case Intrinsic::ppc_qpx_qvlfs:
9557 case Intrinsic::ppc_qpx_qvlfsa:
9558 VT = MVT::v4f32;
9559 break;
9560 case Intrinsic::ppc_qpx_qvlfcd:
9561 case Intrinsic::ppc_qpx_qvlfcda:
9562 VT = MVT::v2f64;
9563 break;
9564 case Intrinsic::ppc_qpx_qvlfcs:
9565 case Intrinsic::ppc_qpx_qvlfcsa:
9566 VT = MVT::v2f32;
9567 break;
9568 case Intrinsic::ppc_qpx_qvlfiwa:
9569 case Intrinsic::ppc_qpx_qvlfiwz:
Hal Finkel3604bf72014-08-01 01:02:01 +00009570 case Intrinsic::ppc_altivec_lvx:
9571 case Intrinsic::ppc_altivec_lvxl:
Bill Schmidt72954782014-11-12 04:19:40 +00009572 case Intrinsic::ppc_vsx_lxvw4x:
Hal Finkel3604bf72014-08-01 01:02:01 +00009573 VT = MVT::v4i32;
9574 break;
Bill Schmidt72954782014-11-12 04:19:40 +00009575 case Intrinsic::ppc_vsx_lxvd2x:
9576 VT = MVT::v2f64;
9577 break;
Hal Finkel3604bf72014-08-01 01:02:01 +00009578 case Intrinsic::ppc_altivec_lvebx:
9579 VT = MVT::i8;
9580 break;
9581 case Intrinsic::ppc_altivec_lvehx:
9582 VT = MVT::i16;
9583 break;
9584 case Intrinsic::ppc_altivec_lvewx:
9585 VT = MVT::i32;
9586 break;
9587 }
9588
9589 return isConsecutiveLSLoc(N->getOperand(2), VT, Base, Bytes, Dist, DAG);
9590 }
9591
9592 if (N->getOpcode() == ISD::INTRINSIC_VOID) {
9593 EVT VT;
9594 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9595 default: return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00009596 case Intrinsic::ppc_qpx_qvstfd:
9597 case Intrinsic::ppc_qpx_qvstfda:
9598 VT = MVT::v4f64;
9599 break;
9600 case Intrinsic::ppc_qpx_qvstfs:
9601 case Intrinsic::ppc_qpx_qvstfsa:
9602 VT = MVT::v4f32;
9603 break;
9604 case Intrinsic::ppc_qpx_qvstfcd:
9605 case Intrinsic::ppc_qpx_qvstfcda:
9606 VT = MVT::v2f64;
9607 break;
9608 case Intrinsic::ppc_qpx_qvstfcs:
9609 case Intrinsic::ppc_qpx_qvstfcsa:
9610 VT = MVT::v2f32;
9611 break;
9612 case Intrinsic::ppc_qpx_qvstfiw:
9613 case Intrinsic::ppc_qpx_qvstfiwa:
Hal Finkel3604bf72014-08-01 01:02:01 +00009614 case Intrinsic::ppc_altivec_stvx:
9615 case Intrinsic::ppc_altivec_stvxl:
Bill Schmidt72954782014-11-12 04:19:40 +00009616 case Intrinsic::ppc_vsx_stxvw4x:
Hal Finkel3604bf72014-08-01 01:02:01 +00009617 VT = MVT::v4i32;
9618 break;
Bill Schmidt72954782014-11-12 04:19:40 +00009619 case Intrinsic::ppc_vsx_stxvd2x:
9620 VT = MVT::v2f64;
9621 break;
Hal Finkel3604bf72014-08-01 01:02:01 +00009622 case Intrinsic::ppc_altivec_stvebx:
9623 VT = MVT::i8;
9624 break;
9625 case Intrinsic::ppc_altivec_stvehx:
9626 VT = MVT::i16;
9627 break;
9628 case Intrinsic::ppc_altivec_stvewx:
9629 VT = MVT::i32;
9630 break;
9631 }
9632
9633 return isConsecutiveLSLoc(N->getOperand(3), VT, Base, Bytes, Dist, DAG);
9634 }
9635
9636 return false;
9637}
9638
Hal Finkel7d8a6912013-05-26 18:08:30 +00009639// Return true is there is a nearyby consecutive load to the one provided
9640// (regardless of alignment). We search up and down the chain, looking though
Matt Arsenault57e74d22014-07-29 00:02:40 +00009641// token factors and other loads (but nothing else). As a result, a true result
9642// indicates that it is safe to create a new consecutive load adjacent to the
9643// load provided.
Hal Finkel7d8a6912013-05-26 18:08:30 +00009644static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
9645 SDValue Chain = LD->getChain();
9646 EVT VT = LD->getMemoryVT();
9647
9648 SmallSet<SDNode *, 16> LoadRoots;
9649 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
9650 SmallSet<SDNode *, 16> Visited;
9651
9652 // First, search up the chain, branching to follow all token-factor operands.
9653 // If we find a consecutive load, then we're done, otherwise, record all
9654 // nodes just above the top-level loads and token factors.
9655 while (!Queue.empty()) {
9656 SDNode *ChainNext = Queue.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00009657 if (!Visited.insert(ChainNext).second)
Hal Finkel7d8a6912013-05-26 18:08:30 +00009658 continue;
9659
Hal Finkel3604bf72014-08-01 01:02:01 +00009660 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009661 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00009662 return true;
9663
9664 if (!Visited.count(ChainLD->getChain().getNode()))
9665 Queue.push_back(ChainLD->getChain().getNode());
9666 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
Craig Topper66e588b2014-06-29 00:40:57 +00009667 for (const SDUse &O : ChainNext->ops())
9668 if (!Visited.count(O.getNode()))
9669 Queue.push_back(O.getNode());
Hal Finkel7d8a6912013-05-26 18:08:30 +00009670 } else
9671 LoadRoots.insert(ChainNext);
9672 }
9673
9674 // Second, search down the chain, starting from the top-level nodes recorded
9675 // in the first phase. These top-level nodes are the nodes just above all
9676 // loads and token factors. Starting with their uses, recursively look though
9677 // all loads (just the chain uses) and token factors to find a consecutive
9678 // load.
9679 Visited.clear();
9680 Queue.clear();
9681
9682 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
9683 IE = LoadRoots.end(); I != IE; ++I) {
9684 Queue.push_back(*I);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +00009685
Hal Finkel7d8a6912013-05-26 18:08:30 +00009686 while (!Queue.empty()) {
9687 SDNode *LoadRoot = Queue.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00009688 if (!Visited.insert(LoadRoot).second)
Hal Finkel7d8a6912013-05-26 18:08:30 +00009689 continue;
9690
Hal Finkel3604bf72014-08-01 01:02:01 +00009691 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009692 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00009693 return true;
9694
9695 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
9696 UE = LoadRoot->use_end(); UI != UE; ++UI)
Hal Finkel3604bf72014-08-01 01:02:01 +00009697 if (((isa<MemSDNode>(*UI) &&
9698 cast<MemSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
Hal Finkel7d8a6912013-05-26 18:08:30 +00009699 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
9700 Queue.push_back(*UI);
9701 }
9702 }
9703
9704 return false;
9705}
9706
Hal Finkel940ab932014-02-28 00:27:01 +00009707SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
9708 DAGCombinerInfo &DCI) const {
9709 SelectionDAG &DAG = DCI.DAG;
9710 SDLoc dl(N);
9711
Eric Christophercccae792015-01-30 22:02:31 +00009712 assert(Subtarget.useCRBits() && "Expecting to be tracking CR bits");
Hal Finkel940ab932014-02-28 00:27:01 +00009713 // If we're tracking CR bits, we need to be careful that we don't have:
9714 // trunc(binary-ops(zext(x), zext(y)))
9715 // or
9716 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
9717 // such that we're unnecessarily moving things into GPRs when it would be
9718 // better to keep them in CR bits.
9719
9720 // Note that trunc here can be an actual i1 trunc, or can be the effective
9721 // truncation that comes from a setcc or select_cc.
9722 if (N->getOpcode() == ISD::TRUNCATE &&
9723 N->getValueType(0) != MVT::i1)
9724 return SDValue();
9725
9726 if (N->getOperand(0).getValueType() != MVT::i32 &&
9727 N->getOperand(0).getValueType() != MVT::i64)
9728 return SDValue();
9729
9730 if (N->getOpcode() == ISD::SETCC ||
9731 N->getOpcode() == ISD::SELECT_CC) {
9732 // If we're looking at a comparison, then we need to make sure that the
9733 // high bits (all except for the first) don't matter the result.
9734 ISD::CondCode CC =
9735 cast<CondCodeSDNode>(N->getOperand(
9736 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
9737 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
9738
9739 if (ISD::isSignedIntSetCC(CC)) {
9740 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
9741 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
9742 return SDValue();
9743 } else if (ISD::isUnsignedIntSetCC(CC)) {
9744 if (!DAG.MaskedValueIsZero(N->getOperand(0),
9745 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
9746 !DAG.MaskedValueIsZero(N->getOperand(1),
9747 APInt::getHighBitsSet(OpBits, OpBits-1)))
9748 return SDValue();
9749 } else {
9750 // This is neither a signed nor an unsigned comparison, just make sure
9751 // that the high bits are equal.
9752 APInt Op1Zero, Op1One;
9753 APInt Op2Zero, Op2One;
Jay Foada0653a32014-05-14 21:14:37 +00009754 DAG.computeKnownBits(N->getOperand(0), Op1Zero, Op1One);
9755 DAG.computeKnownBits(N->getOperand(1), Op2Zero, Op2One);
Hal Finkel940ab932014-02-28 00:27:01 +00009756
9757 // We don't really care about what is known about the first bit (if
9758 // anything), so clear it in all masks prior to comparing them.
9759 Op1Zero.clearBit(0); Op1One.clearBit(0);
9760 Op2Zero.clearBit(0); Op2One.clearBit(0);
9761
9762 if (Op1Zero != Op2Zero || Op1One != Op2One)
9763 return SDValue();
9764 }
9765 }
9766
9767 // We now know that the higher-order bits are irrelevant, we just need to
9768 // make sure that all of the intermediate operations are bit operations, and
9769 // all inputs are extensions.
9770 if (N->getOperand(0).getOpcode() != ISD::AND &&
9771 N->getOperand(0).getOpcode() != ISD::OR &&
9772 N->getOperand(0).getOpcode() != ISD::XOR &&
9773 N->getOperand(0).getOpcode() != ISD::SELECT &&
9774 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
9775 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
9776 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
9777 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
9778 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
9779 return SDValue();
9780
9781 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
9782 N->getOperand(1).getOpcode() != ISD::AND &&
9783 N->getOperand(1).getOpcode() != ISD::OR &&
9784 N->getOperand(1).getOpcode() != ISD::XOR &&
9785 N->getOperand(1).getOpcode() != ISD::SELECT &&
9786 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
9787 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
9788 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
9789 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
9790 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
9791 return SDValue();
9792
9793 SmallVector<SDValue, 4> Inputs;
9794 SmallVector<SDValue, 8> BinOps, PromOps;
9795 SmallPtrSet<SDNode *, 16> Visited;
9796
9797 for (unsigned i = 0; i < 2; ++i) {
9798 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9799 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9800 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
9801 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
9802 isa<ConstantSDNode>(N->getOperand(i)))
9803 Inputs.push_back(N->getOperand(i));
9804 else
9805 BinOps.push_back(N->getOperand(i));
9806
9807 if (N->getOpcode() == ISD::TRUNCATE)
9808 break;
9809 }
9810
9811 // Visit all inputs, collect all binary operations (and, or, xor and
NAKAMURA Takumi84965032015-09-22 11:14:12 +00009812 // select) that are all fed by extensions.
Hal Finkel940ab932014-02-28 00:27:01 +00009813 while (!BinOps.empty()) {
9814 SDValue BinOp = BinOps.back();
9815 BinOps.pop_back();
9816
David Blaikie70573dc2014-11-19 07:49:26 +00009817 if (!Visited.insert(BinOp.getNode()).second)
Hal Finkel940ab932014-02-28 00:27:01 +00009818 continue;
9819
9820 PromOps.push_back(BinOp);
9821
9822 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
9823 // The condition of the select is not promoted.
9824 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
9825 continue;
9826 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
9827 continue;
9828
9829 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9830 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9831 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
9832 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
9833 isa<ConstantSDNode>(BinOp.getOperand(i))) {
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00009834 Inputs.push_back(BinOp.getOperand(i));
Hal Finkel940ab932014-02-28 00:27:01 +00009835 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
9836 BinOp.getOperand(i).getOpcode() == ISD::OR ||
9837 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
9838 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
9839 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
9840 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
9841 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9842 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9843 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
9844 BinOps.push_back(BinOp.getOperand(i));
9845 } else {
9846 // We have an input that is not an extension or another binary
9847 // operation; we'll abort this transformation.
9848 return SDValue();
9849 }
9850 }
9851 }
9852
9853 // Make sure that this is a self-contained cluster of operations (which
9854 // is not quite the same thing as saying that everything has only one
9855 // use).
9856 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9857 if (isa<ConstantSDNode>(Inputs[i]))
9858 continue;
9859
9860 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
9861 UE = Inputs[i].getNode()->use_end();
9862 UI != UE; ++UI) {
9863 SDNode *User = *UI;
9864 if (User != N && !Visited.count(User))
9865 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00009866
9867 // Make sure that we're not going to promote the non-output-value
9868 // operand(s) or SELECT or SELECT_CC.
9869 // FIXME: Although we could sometimes handle this, and it does occur in
9870 // practice that one of the condition inputs to the select is also one of
9871 // the outputs, we currently can't deal with this.
9872 if (User->getOpcode() == ISD::SELECT) {
9873 if (User->getOperand(0) == Inputs[i])
9874 return SDValue();
9875 } else if (User->getOpcode() == ISD::SELECT_CC) {
9876 if (User->getOperand(0) == Inputs[i] ||
9877 User->getOperand(1) == Inputs[i])
9878 return SDValue();
9879 }
Hal Finkel940ab932014-02-28 00:27:01 +00009880 }
9881 }
9882
9883 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
9884 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
9885 UE = PromOps[i].getNode()->use_end();
9886 UI != UE; ++UI) {
9887 SDNode *User = *UI;
9888 if (User != N && !Visited.count(User))
9889 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00009890
9891 // Make sure that we're not going to promote the non-output-value
9892 // operand(s) or SELECT or SELECT_CC.
9893 // FIXME: Although we could sometimes handle this, and it does occur in
9894 // practice that one of the condition inputs to the select is also one of
9895 // the outputs, we currently can't deal with this.
9896 if (User->getOpcode() == ISD::SELECT) {
9897 if (User->getOperand(0) == PromOps[i])
9898 return SDValue();
9899 } else if (User->getOpcode() == ISD::SELECT_CC) {
9900 if (User->getOperand(0) == PromOps[i] ||
9901 User->getOperand(1) == PromOps[i])
9902 return SDValue();
9903 }
Hal Finkel940ab932014-02-28 00:27:01 +00009904 }
9905 }
9906
9907 // Replace all inputs with the extension operand.
9908 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9909 // Constants may have users outside the cluster of to-be-promoted nodes,
9910 // and so we need to replace those as we do the promotions.
9911 if (isa<ConstantSDNode>(Inputs[i]))
9912 continue;
9913 else
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00009914 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
Hal Finkel940ab932014-02-28 00:27:01 +00009915 }
9916
9917 // Replace all operations (these are all the same, but have a different
9918 // (i1) return type). DAG.getNode will validate that the types of
9919 // a binary operator match, so go through the list in reverse so that
9920 // we've likely promoted both operands first. Any intermediate truncations or
9921 // extensions disappear.
9922 while (!PromOps.empty()) {
9923 SDValue PromOp = PromOps.back();
9924 PromOps.pop_back();
9925
9926 if (PromOp.getOpcode() == ISD::TRUNCATE ||
9927 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
9928 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
9929 PromOp.getOpcode() == ISD::ANY_EXTEND) {
9930 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
9931 PromOp.getOperand(0).getValueType() != MVT::i1) {
9932 // The operand is not yet ready (see comment below).
9933 PromOps.insert(PromOps.begin(), PromOp);
9934 continue;
9935 }
9936
9937 SDValue RepValue = PromOp.getOperand(0);
9938 if (isa<ConstantSDNode>(RepValue))
9939 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
9940
9941 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
9942 continue;
9943 }
9944
9945 unsigned C;
9946 switch (PromOp.getOpcode()) {
9947 default: C = 0; break;
9948 case ISD::SELECT: C = 1; break;
9949 case ISD::SELECT_CC: C = 2; break;
9950 }
9951
9952 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
9953 PromOp.getOperand(C).getValueType() != MVT::i1) ||
9954 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
9955 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
9956 // The to-be-promoted operands of this node have not yet been
9957 // promoted (this should be rare because we're going through the
9958 // list backward, but if one of the operands has several users in
9959 // this cluster of to-be-promoted nodes, it is possible).
9960 PromOps.insert(PromOps.begin(), PromOp);
9961 continue;
9962 }
9963
9964 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
9965 PromOp.getNode()->op_end());
9966
9967 // If there are any constant inputs, make sure they're replaced now.
9968 for (unsigned i = 0; i < 2; ++i)
9969 if (isa<ConstantSDNode>(Ops[C+i]))
9970 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
9971
9972 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00009973 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00009974 }
9975
9976 // Now we're left with the initial truncation itself.
9977 if (N->getOpcode() == ISD::TRUNCATE)
9978 return N->getOperand(0);
9979
9980 // Otherwise, this is a comparison. The operands to be compared have just
9981 // changed type (to i1), but everything else is the same.
9982 return SDValue(N, 0);
9983}
9984
9985SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
9986 DAGCombinerInfo &DCI) const {
9987 SelectionDAG &DAG = DCI.DAG;
9988 SDLoc dl(N);
9989
Hal Finkel940ab932014-02-28 00:27:01 +00009990 // If we're tracking CR bits, we need to be careful that we don't have:
9991 // zext(binary-ops(trunc(x), trunc(y)))
9992 // or
9993 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
9994 // such that we're unnecessarily moving things into CR bits that can more
9995 // efficiently stay in GPRs. Note that if we're not certain that the high
9996 // bits are set as required by the final extension, we still may need to do
9997 // some masking to get the proper behavior.
9998
Hal Finkel46043ed2014-03-01 21:36:57 +00009999 // This same functionality is important on PPC64 when dealing with
10000 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
10001 // the return values of functions. Because it is so similar, it is handled
10002 // here as well.
10003
Hal Finkel940ab932014-02-28 00:27:01 +000010004 if (N->getValueType(0) != MVT::i32 &&
10005 N->getValueType(0) != MVT::i64)
10006 return SDValue();
10007
Eric Christophercccae792015-01-30 22:02:31 +000010008 if (!((N->getOperand(0).getValueType() == MVT::i1 && Subtarget.useCRBits()) ||
10009 (N->getOperand(0).getValueType() == MVT::i32 && Subtarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +000010010 return SDValue();
10011
10012 if (N->getOperand(0).getOpcode() != ISD::AND &&
10013 N->getOperand(0).getOpcode() != ISD::OR &&
10014 N->getOperand(0).getOpcode() != ISD::XOR &&
10015 N->getOperand(0).getOpcode() != ISD::SELECT &&
10016 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
10017 return SDValue();
10018
10019 SmallVector<SDValue, 4> Inputs;
10020 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
10021 SmallPtrSet<SDNode *, 16> Visited;
10022
10023 // Visit all inputs, collect all binary operations (and, or, xor and
NAKAMURA Takumi84965032015-09-22 11:14:12 +000010024 // select) that are all fed by truncations.
Hal Finkel940ab932014-02-28 00:27:01 +000010025 while (!BinOps.empty()) {
10026 SDValue BinOp = BinOps.back();
10027 BinOps.pop_back();
10028
David Blaikie70573dc2014-11-19 07:49:26 +000010029 if (!Visited.insert(BinOp.getNode()).second)
Hal Finkel940ab932014-02-28 00:27:01 +000010030 continue;
10031
10032 PromOps.push_back(BinOp);
10033
10034 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
10035 // The condition of the select is not promoted.
10036 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
10037 continue;
10038 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
10039 continue;
10040
10041 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
10042 isa<ConstantSDNode>(BinOp.getOperand(i))) {
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +000010043 Inputs.push_back(BinOp.getOperand(i));
Hal Finkel940ab932014-02-28 00:27:01 +000010044 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
10045 BinOp.getOperand(i).getOpcode() == ISD::OR ||
10046 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
10047 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
10048 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
10049 BinOps.push_back(BinOp.getOperand(i));
10050 } else {
10051 // We have an input that is not a truncation or another binary
10052 // operation; we'll abort this transformation.
10053 return SDValue();
10054 }
10055 }
10056 }
10057
Hal Finkel4104a1a2014-12-14 05:53:19 +000010058 // The operands of a select that must be truncated when the select is
10059 // promoted because the operand is actually part of the to-be-promoted set.
10060 DenseMap<SDNode *, EVT> SelectTruncOp[2];
10061
Hal Finkel940ab932014-02-28 00:27:01 +000010062 // Make sure that this is a self-contained cluster of operations (which
10063 // is not quite the same thing as saying that everything has only one
10064 // use).
10065 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
10066 if (isa<ConstantSDNode>(Inputs[i]))
10067 continue;
10068
10069 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
10070 UE = Inputs[i].getNode()->use_end();
10071 UI != UE; ++UI) {
10072 SDNode *User = *UI;
10073 if (User != N && !Visited.count(User))
10074 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +000010075
Hal Finkel4104a1a2014-12-14 05:53:19 +000010076 // If we're going to promote the non-output-value operand(s) or SELECT or
10077 // SELECT_CC, record them for truncation.
Hal Finkel46043ed2014-03-01 21:36:57 +000010078 if (User->getOpcode() == ISD::SELECT) {
10079 if (User->getOperand(0) == Inputs[i])
Hal Finkel4104a1a2014-12-14 05:53:19 +000010080 SelectTruncOp[0].insert(std::make_pair(User,
10081 User->getOperand(0).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +000010082 } else if (User->getOpcode() == ISD::SELECT_CC) {
Hal Finkel4104a1a2014-12-14 05:53:19 +000010083 if (User->getOperand(0) == Inputs[i])
10084 SelectTruncOp[0].insert(std::make_pair(User,
10085 User->getOperand(0).getValueType()));
10086 if (User->getOperand(1) == Inputs[i])
10087 SelectTruncOp[1].insert(std::make_pair(User,
10088 User->getOperand(1).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +000010089 }
Hal Finkel940ab932014-02-28 00:27:01 +000010090 }
10091 }
10092
10093 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
10094 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
10095 UE = PromOps[i].getNode()->use_end();
10096 UI != UE; ++UI) {
10097 SDNode *User = *UI;
10098 if (User != N && !Visited.count(User))
10099 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +000010100
Hal Finkel4104a1a2014-12-14 05:53:19 +000010101 // If we're going to promote the non-output-value operand(s) or SELECT or
10102 // SELECT_CC, record them for truncation.
Hal Finkel46043ed2014-03-01 21:36:57 +000010103 if (User->getOpcode() == ISD::SELECT) {
10104 if (User->getOperand(0) == PromOps[i])
Hal Finkel4104a1a2014-12-14 05:53:19 +000010105 SelectTruncOp[0].insert(std::make_pair(User,
10106 User->getOperand(0).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +000010107 } else if (User->getOpcode() == ISD::SELECT_CC) {
Hal Finkel4104a1a2014-12-14 05:53:19 +000010108 if (User->getOperand(0) == PromOps[i])
10109 SelectTruncOp[0].insert(std::make_pair(User,
10110 User->getOperand(0).getValueType()));
10111 if (User->getOperand(1) == PromOps[i])
10112 SelectTruncOp[1].insert(std::make_pair(User,
10113 User->getOperand(1).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +000010114 }
Hal Finkel940ab932014-02-28 00:27:01 +000010115 }
10116 }
10117
Hal Finkel46043ed2014-03-01 21:36:57 +000010118 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +000010119 bool ReallyNeedsExt = false;
10120 if (N->getOpcode() != ISD::ANY_EXTEND) {
10121 // If all of the inputs are not already sign/zero extended, then
10122 // we'll still need to do that at the end.
10123 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
10124 if (isa<ConstantSDNode>(Inputs[i]))
10125 continue;
10126
10127 unsigned OpBits =
10128 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +000010129 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
10130
Hal Finkel940ab932014-02-28 00:27:01 +000010131 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
10132 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +000010133 APInt::getHighBitsSet(OpBits,
10134 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +000010135 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +000010136 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
10137 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +000010138 ReallyNeedsExt = true;
10139 break;
10140 }
10141 }
10142 }
10143
10144 // Replace all inputs, either with the truncation operand, or a
10145 // truncation or extension to the final output type.
10146 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
10147 // Constant inputs need to be replaced with the to-be-promoted nodes that
10148 // use them because they might have users outside of the cluster of
10149 // promoted nodes.
10150 if (isa<ConstantSDNode>(Inputs[i]))
10151 continue;
10152
10153 SDValue InSrc = Inputs[i].getOperand(0);
10154 if (Inputs[i].getValueType() == N->getValueType(0))
10155 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
10156 else if (N->getOpcode() == ISD::SIGN_EXTEND)
10157 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
10158 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
10159 else if (N->getOpcode() == ISD::ZERO_EXTEND)
10160 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
10161 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
10162 else
10163 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
10164 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
10165 }
10166
10167 // Replace all operations (these are all the same, but have a different
10168 // (promoted) return type). DAG.getNode will validate that the types of
10169 // a binary operator match, so go through the list in reverse so that
10170 // we've likely promoted both operands first.
10171 while (!PromOps.empty()) {
10172 SDValue PromOp = PromOps.back();
10173 PromOps.pop_back();
10174
10175 unsigned C;
10176 switch (PromOp.getOpcode()) {
10177 default: C = 0; break;
10178 case ISD::SELECT: C = 1; break;
10179 case ISD::SELECT_CC: C = 2; break;
10180 }
10181
10182 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
10183 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
10184 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
10185 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
10186 // The to-be-promoted operands of this node have not yet been
10187 // promoted (this should be rare because we're going through the
10188 // list backward, but if one of the operands has several users in
10189 // this cluster of to-be-promoted nodes, it is possible).
10190 PromOps.insert(PromOps.begin(), PromOp);
10191 continue;
10192 }
10193
Hal Finkel4104a1a2014-12-14 05:53:19 +000010194 // For SELECT and SELECT_CC nodes, we do a similar check for any
10195 // to-be-promoted comparison inputs.
10196 if (PromOp.getOpcode() == ISD::SELECT ||
10197 PromOp.getOpcode() == ISD::SELECT_CC) {
10198 if ((SelectTruncOp[0].count(PromOp.getNode()) &&
10199 PromOp.getOperand(0).getValueType() != N->getValueType(0)) ||
10200 (SelectTruncOp[1].count(PromOp.getNode()) &&
10201 PromOp.getOperand(1).getValueType() != N->getValueType(0))) {
10202 PromOps.insert(PromOps.begin(), PromOp);
10203 continue;
10204 }
10205 }
10206
Hal Finkel940ab932014-02-28 00:27:01 +000010207 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
10208 PromOp.getNode()->op_end());
10209
10210 // If this node has constant inputs, then they'll need to be promoted here.
10211 for (unsigned i = 0; i < 2; ++i) {
10212 if (!isa<ConstantSDNode>(Ops[C+i]))
10213 continue;
10214 if (Ops[C+i].getValueType() == N->getValueType(0))
10215 continue;
10216
10217 if (N->getOpcode() == ISD::SIGN_EXTEND)
10218 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
10219 else if (N->getOpcode() == ISD::ZERO_EXTEND)
10220 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
10221 else
10222 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
10223 }
10224
Hal Finkel4104a1a2014-12-14 05:53:19 +000010225 // If we've promoted the comparison inputs of a SELECT or SELECT_CC,
10226 // truncate them again to the original value type.
10227 if (PromOp.getOpcode() == ISD::SELECT ||
10228 PromOp.getOpcode() == ISD::SELECT_CC) {
10229 auto SI0 = SelectTruncOp[0].find(PromOp.getNode());
10230 if (SI0 != SelectTruncOp[0].end())
10231 Ops[0] = DAG.getNode(ISD::TRUNCATE, dl, SI0->second, Ops[0]);
10232 auto SI1 = SelectTruncOp[1].find(PromOp.getNode());
10233 if (SI1 != SelectTruncOp[1].end())
10234 Ops[1] = DAG.getNode(ISD::TRUNCATE, dl, SI1->second, Ops[1]);
10235 }
10236
Hal Finkel940ab932014-02-28 00:27:01 +000010237 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +000010238 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops));
Hal Finkel940ab932014-02-28 00:27:01 +000010239 }
10240
10241 // Now we're left with the initial extension itself.
10242 if (!ReallyNeedsExt)
10243 return N->getOperand(0);
10244
Hal Finkel46043ed2014-03-01 21:36:57 +000010245 // To zero extend, just mask off everything except for the first bit (in the
10246 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +000010247 if (N->getOpcode() == ISD::ZERO_EXTEND)
10248 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +000010249 DAG.getConstant(APInt::getLowBitsSet(
10250 N->getValueSizeInBits(0), PromBits),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010251 dl, N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +000010252
10253 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
10254 "Invalid extension type");
Mehdi Amini9639d652015-07-09 02:09:20 +000010255 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0), DAG.getDataLayout());
Hal Finkel940ab932014-02-28 00:27:01 +000010256 SDValue ShiftCst =
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +000010257 DAG.getConstant(N->getValueSizeInBits(0) - PromBits, dl, ShiftAmountTy);
10258 return DAG.getNode(
10259 ISD::SRA, dl, N->getValueType(0),
10260 DAG.getNode(ISD::SHL, dl, N->getValueType(0), N->getOperand(0), ShiftCst),
10261 ShiftCst);
Hal Finkel940ab932014-02-28 00:27:01 +000010262}
10263
Hal Finkel5efb9182015-01-06 06:01:57 +000010264SDValue PPCTargetLowering::combineFPToIntToFP(SDNode *N,
10265 DAGCombinerInfo &DCI) const {
10266 assert((N->getOpcode() == ISD::SINT_TO_FP ||
10267 N->getOpcode() == ISD::UINT_TO_FP) &&
10268 "Need an int -> FP conversion node here");
10269
10270 if (!Subtarget.has64BitSupport())
10271 return SDValue();
10272
10273 SelectionDAG &DAG = DCI.DAG;
10274 SDLoc dl(N);
10275 SDValue Op(N, 0);
10276
10277 // Don't handle ppc_fp128 here or i1 conversions.
10278 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
10279 return SDValue();
10280 if (Op.getOperand(0).getValueType() == MVT::i1)
10281 return SDValue();
10282
10283 // For i32 intermediate values, unfortunately, the conversion functions
10284 // leave the upper 32 bits of the value are undefined. Within the set of
10285 // scalar instructions, we have no method for zero- or sign-extending the
10286 // value. Thus, we cannot handle i32 intermediate values here.
10287 if (Op.getOperand(0).getValueType() == MVT::i32)
10288 return SDValue();
10289
10290 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
10291 "UINT_TO_FP is supported only with FPCVT");
10292
10293 // If we have FCFIDS, then use it when converting to single-precision.
10294 // Otherwise, convert to double-precision and then round.
Eric Christophercccae792015-01-30 22:02:31 +000010295 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
10296 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS
10297 : PPCISD::FCFIDS)
10298 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU
10299 : PPCISD::FCFID);
10300 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
10301 ? MVT::f32
10302 : MVT::f64;
Hal Finkel5efb9182015-01-06 06:01:57 +000010303
10304 // If we're converting from a float, to an int, and back to a float again,
10305 // then we don't need the store/load pair at all.
10306 if ((Op.getOperand(0).getOpcode() == ISD::FP_TO_UINT &&
10307 Subtarget.hasFPCVT()) ||
10308 (Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT)) {
10309 SDValue Src = Op.getOperand(0).getOperand(0);
10310 if (Src.getValueType() == MVT::f32) {
10311 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
10312 DCI.AddToWorklist(Src.getNode());
Hal Finkelbe78c252015-08-20 01:18:20 +000010313 } else if (Src.getValueType() != MVT::f64) {
10314 // Make sure that we don't pick up a ppc_fp128 source value.
10315 return SDValue();
Hal Finkel5efb9182015-01-06 06:01:57 +000010316 }
10317
10318 unsigned FCTOp =
10319 Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
10320 PPCISD::FCTIDUZ;
10321
10322 SDValue Tmp = DAG.getNode(FCTOp, dl, MVT::f64, Src);
10323 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Tmp);
10324
10325 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) {
10326 FP = DAG.getNode(ISD::FP_ROUND, dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010327 MVT::f32, FP, DAG.getIntPtrConstant(0, dl));
Hal Finkel5efb9182015-01-06 06:01:57 +000010328 DCI.AddToWorklist(FP.getNode());
10329 }
10330
10331 return FP;
10332 }
10333
10334 return SDValue();
10335}
10336
Bill Schmidtfae5d712014-12-09 16:35:51 +000010337// expandVSXLoadForLE - Convert VSX loads (which may be intrinsics for
10338// builtins) into loads with swaps.
10339SDValue PPCTargetLowering::expandVSXLoadForLE(SDNode *N,
10340 DAGCombinerInfo &DCI) const {
10341 SelectionDAG &DAG = DCI.DAG;
10342 SDLoc dl(N);
10343 SDValue Chain;
10344 SDValue Base;
10345 MachineMemOperand *MMO;
10346
10347 switch (N->getOpcode()) {
10348 default:
10349 llvm_unreachable("Unexpected opcode for little endian VSX load");
10350 case ISD::LOAD: {
10351 LoadSDNode *LD = cast<LoadSDNode>(N);
10352 Chain = LD->getChain();
10353 Base = LD->getBasePtr();
10354 MMO = LD->getMemOperand();
10355 // If the MMO suggests this isn't a load of a full vector, leave
10356 // things alone. For a built-in, we have to make the change for
10357 // correctness, so if there is a size problem that will be a bug.
10358 if (MMO->getSize() < 16)
10359 return SDValue();
10360 break;
10361 }
10362 case ISD::INTRINSIC_W_CHAIN: {
10363 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
10364 Chain = Intrin->getChain();
Nemanja Ivanovic7df26c92015-06-30 20:01:16 +000010365 // Similarly to the store case below, Intrin->getBasePtr() doesn't get
Nemanja Ivanovic9c8d4cf2015-06-30 19:45:45 +000010366 // us what we want. Get operand 2 instead.
Nemanja Ivanovic9c8d4cf2015-06-30 19:45:45 +000010367 Base = Intrin->getOperand(2);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010368 MMO = Intrin->getMemOperand();
10369 break;
10370 }
10371 }
10372
10373 MVT VecTy = N->getValueType(0).getSimpleVT();
10374 SDValue LoadOps[] = { Chain, Base };
10375 SDValue Load = DAG.getMemIntrinsicNode(PPCISD::LXVD2X, dl,
Nirav Dave1f51c332016-04-15 15:01:38 +000010376 DAG.getVTList(MVT::v2f64, MVT::Other),
10377 LoadOps, MVT::v2f64, MMO);
10378
Bill Schmidtfae5d712014-12-09 16:35:51 +000010379 DCI.AddToWorklist(Load.getNode());
10380 Chain = Load.getValue(1);
Nirav Dave1f51c332016-04-15 15:01:38 +000010381 SDValue Swap = DAG.getNode(
10382 PPCISD::XXSWAPD, dl, DAG.getVTList(MVT::v2f64, MVT::Other), Chain, Load);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010383 DCI.AddToWorklist(Swap.getNode());
Nirav Dave1f51c332016-04-15 15:01:38 +000010384
10385 // Add a bitcast if the resulting load type doesn't match v2f64.
10386 if (VecTy != MVT::v2f64) {
10387 SDValue N = DAG.getNode(ISD::BITCAST, dl, VecTy, Swap);
10388 DCI.AddToWorklist(N.getNode());
10389 // Package {bitcast value, swap's chain} to match Load's shape.
10390 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(VecTy, MVT::Other),
10391 N, Swap.getValue(1));
10392 }
10393
Bill Schmidtfae5d712014-12-09 16:35:51 +000010394 return Swap;
10395}
10396
10397// expandVSXStoreForLE - Convert VSX stores (which may be intrinsics for
10398// builtins) into stores with swaps.
10399SDValue PPCTargetLowering::expandVSXStoreForLE(SDNode *N,
10400 DAGCombinerInfo &DCI) const {
10401 SelectionDAG &DAG = DCI.DAG;
10402 SDLoc dl(N);
10403 SDValue Chain;
10404 SDValue Base;
10405 unsigned SrcOpnd;
10406 MachineMemOperand *MMO;
10407
10408 switch (N->getOpcode()) {
10409 default:
10410 llvm_unreachable("Unexpected opcode for little endian VSX store");
10411 case ISD::STORE: {
10412 StoreSDNode *ST = cast<StoreSDNode>(N);
10413 Chain = ST->getChain();
10414 Base = ST->getBasePtr();
10415 MMO = ST->getMemOperand();
10416 SrcOpnd = 1;
10417 // If the MMO suggests this isn't a store of a full vector, leave
10418 // things alone. For a built-in, we have to make the change for
10419 // correctness, so if there is a size problem that will be a bug.
10420 if (MMO->getSize() < 16)
10421 return SDValue();
10422 break;
10423 }
10424 case ISD::INTRINSIC_VOID: {
10425 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
10426 Chain = Intrin->getChain();
10427 // Intrin->getBasePtr() oddly does not get what we want.
10428 Base = Intrin->getOperand(3);
10429 MMO = Intrin->getMemOperand();
10430 SrcOpnd = 2;
10431 break;
10432 }
10433 }
10434
10435 SDValue Src = N->getOperand(SrcOpnd);
10436 MVT VecTy = Src.getValueType().getSimpleVT();
Nirav Dave1f51c332016-04-15 15:01:38 +000010437
10438 // All stores are done as v2f64 and possible bit cast.
10439 if (VecTy != MVT::v2f64) {
10440 Src = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Src);
10441 DCI.AddToWorklist(Src.getNode());
10442 }
10443
Bill Schmidtfae5d712014-12-09 16:35:51 +000010444 SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl,
Nirav Dave1f51c332016-04-15 15:01:38 +000010445 DAG.getVTList(MVT::v2f64, MVT::Other), Chain, Src);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010446 DCI.AddToWorklist(Swap.getNode());
10447 Chain = Swap.getValue(1);
10448 SDValue StoreOps[] = { Chain, Swap, Base };
10449 SDValue Store = DAG.getMemIntrinsicNode(PPCISD::STXVD2X, dl,
10450 DAG.getVTList(MVT::Other),
10451 StoreOps, VecTy, MMO);
10452 DCI.AddToWorklist(Store.getNode());
10453 return Store;
10454}
10455
Duncan Sandsdc2dac12008-11-24 14:53:14 +000010456SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
10457 DAGCombinerInfo &DCI) const {
Chris Lattnerf4184352006-03-01 04:57:39 +000010458 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +000010459 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +000010460 switch (N->getOpcode()) {
10461 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +000010462 case PPCISD::SHL:
Artyom Skrobov314ee042015-11-25 19:41:11 +000010463 if (isNullConstant(N->getOperand(0))) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +000010464 return N->getOperand(0);
Chris Lattner3c48ea52006-09-19 05:22:59 +000010465 break;
10466 case PPCISD::SRL:
Artyom Skrobov314ee042015-11-25 19:41:11 +000010467 if (isNullConstant(N->getOperand(0))) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +000010468 return N->getOperand(0);
Chris Lattner3c48ea52006-09-19 05:22:59 +000010469 break;
10470 case PPCISD::SRA:
10471 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +000010472 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +000010473 C->isAllOnesValue()) // -1 >>s V -> -1.
10474 return N->getOperand(0);
10475 }
10476 break;
Hal Finkel940ab932014-02-28 00:27:01 +000010477 case ISD::SIGN_EXTEND:
10478 case ISD::ZERO_EXTEND:
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +000010479 case ISD::ANY_EXTEND:
Hal Finkel940ab932014-02-28 00:27:01 +000010480 return DAGCombineExtBoolTrunc(N, DCI);
10481 case ISD::TRUNCATE:
10482 case ISD::SETCC:
10483 case ISD::SELECT_CC:
10484 return DAGCombineTruncBoolExt(N, DCI);
Chris Lattnerf4184352006-03-01 04:57:39 +000010485 case ISD::SINT_TO_FP:
Hal Finkel5efb9182015-01-06 06:01:57 +000010486 case ISD::UINT_TO_FP:
10487 return combineFPToIntToFP(N, DCI);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010488 case ISD::STORE: {
Chris Lattner27f53452006-03-01 05:50:56 +000010489 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
Eric Christophercccae792015-01-30 22:02:31 +000010490 if (Subtarget.hasSTFIWX() && !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +000010491 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +000010492 N->getOperand(1).getValueType() == MVT::i32 &&
10493 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010494 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +000010495 if (Val.getValueType() == MVT::f32) {
10496 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010497 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +000010498 }
Owen Anderson9f944592009-08-11 20:47:22 +000010499 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010500 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +000010501
Hal Finkel60c75102013-04-01 15:37:53 +000010502 SDValue Ops[] = {
10503 N->getOperand(0), Val, N->getOperand(2),
10504 DAG.getValueType(N->getOperand(1).getValueType())
10505 };
10506
10507 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +000010508 DAG.getVTList(MVT::Other), Ops,
Hal Finkel60c75102013-04-01 15:37:53 +000010509 cast<StoreSDNode>(N)->getMemoryVT(),
10510 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +000010511 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +000010512 return Val;
10513 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010514
Chris Lattnera7976d32006-07-10 20:56:58 +000010515 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +000010516 if (cast<StoreSDNode>(N)->isUnindexed() &&
10517 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +000010518 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +000010519 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +000010520 N->getOperand(1).getValueType() == MVT::i16 ||
Eric Christophercccae792015-01-30 22:02:31 +000010521 (Subtarget.hasLDBRX() && Subtarget.isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +000010522 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010523 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +000010524 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +000010525 if (BSwapOp.getValueType() == MVT::i16)
10526 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +000010527
Dan Gohman48b185d2009-09-25 20:36:54 +000010528 SDValue Ops[] = {
10529 N->getOperand(0), BSwapOp, N->getOperand(2),
10530 DAG.getValueType(N->getOperand(1).getValueType())
10531 };
10532 return
10533 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +000010534 Ops, cast<StoreSDNode>(N)->getMemoryVT(),
Dan Gohman48b185d2009-09-25 20:36:54 +000010535 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +000010536 }
Bill Schmidtfae5d712014-12-09 16:35:51 +000010537
10538 // For little endian, VSX stores require generating xxswapd/lxvd2x.
10539 EVT VT = N->getOperand(1).getValueType();
10540 if (VT.isSimple()) {
10541 MVT StoreVT = VT.getSimpleVT();
Eric Christophercccae792015-01-30 22:02:31 +000010542 if (Subtarget.hasVSX() && Subtarget.isLittleEndian() &&
Bill Schmidtfae5d712014-12-09 16:35:51 +000010543 (StoreVT == MVT::v2f64 || StoreVT == MVT::v2i64 ||
10544 StoreVT == MVT::v4f32 || StoreVT == MVT::v4i32))
10545 return expandVSXStoreForLE(N, DCI);
10546 }
Chris Lattnera7976d32006-07-10 20:56:58 +000010547 break;
Bill Schmidtfae5d712014-12-09 16:35:51 +000010548 }
Hal Finkelcf2e9082013-05-24 23:00:14 +000010549 case ISD::LOAD: {
10550 LoadSDNode *LD = cast<LoadSDNode>(N);
10551 EVT VT = LD->getValueType(0);
Bill Schmidtfae5d712014-12-09 16:35:51 +000010552
10553 // For little endian, VSX loads require generating lxvd2x/xxswapd.
10554 if (VT.isSimple()) {
10555 MVT LoadVT = VT.getSimpleVT();
Eric Christophercccae792015-01-30 22:02:31 +000010556 if (Subtarget.hasVSX() && Subtarget.isLittleEndian() &&
Bill Schmidtfae5d712014-12-09 16:35:51 +000010557 (LoadVT == MVT::v2f64 || LoadVT == MVT::v2i64 ||
10558 LoadVT == MVT::v4f32 || LoadVT == MVT::v4i32))
10559 return expandVSXLoadForLE(N, DCI);
10560 }
10561
Hal Finkel851b33a2016-03-31 02:56:05 +000010562 // We sometimes end up with a 64-bit integer load, from which we extract
10563 // two single-precision floating-point numbers. This happens with
10564 // std::complex<float>, and other similar structures, because of the way we
10565 // canonicalize structure copies. However, if we lack direct moves,
10566 // then the final bitcasts from the extracted integer values to the
10567 // floating-point numbers turn into store/load pairs. Even with direct moves,
10568 // just loading the two floating-point numbers is likely better.
10569 auto ReplaceTwoFloatLoad = [&]() {
10570 if (VT != MVT::i64)
10571 return false;
10572
10573 if (LD->getExtensionType() != ISD::NON_EXTLOAD ||
10574 LD->isVolatile())
10575 return false;
10576
10577 // We're looking for a sequence like this:
10578 // t13: i64,ch = load<LD8[%ref.tmp]> t0, t6, undef:i64
10579 // t16: i64 = srl t13, Constant:i32<32>
10580 // t17: i32 = truncate t16
10581 // t18: f32 = bitcast t17
10582 // t19: i32 = truncate t13
10583 // t20: f32 = bitcast t19
10584
10585 if (!LD->hasNUsesOfValue(2, 0))
10586 return false;
10587
10588 auto UI = LD->use_begin();
10589 while (UI.getUse().getResNo() != 0) ++UI;
10590 SDNode *Trunc = *UI++;
10591 while (UI.getUse().getResNo() != 0) ++UI;
10592 SDNode *RightShift = *UI;
10593 if (Trunc->getOpcode() != ISD::TRUNCATE)
10594 std::swap(Trunc, RightShift);
10595
10596 if (Trunc->getOpcode() != ISD::TRUNCATE ||
10597 Trunc->getValueType(0) != MVT::i32 ||
10598 !Trunc->hasOneUse())
10599 return false;
10600 if (RightShift->getOpcode() != ISD::SRL ||
10601 !isa<ConstantSDNode>(RightShift->getOperand(1)) ||
10602 RightShift->getConstantOperandVal(1) != 32 ||
10603 !RightShift->hasOneUse())
10604 return false;
10605
10606 SDNode *Trunc2 = *RightShift->use_begin();
10607 if (Trunc2->getOpcode() != ISD::TRUNCATE ||
10608 Trunc2->getValueType(0) != MVT::i32 ||
10609 !Trunc2->hasOneUse())
10610 return false;
10611
10612 SDNode *Bitcast = *Trunc->use_begin();
10613 SDNode *Bitcast2 = *Trunc2->use_begin();
10614
10615 if (Bitcast->getOpcode() != ISD::BITCAST ||
10616 Bitcast->getValueType(0) != MVT::f32)
10617 return false;
10618 if (Bitcast2->getOpcode() != ISD::BITCAST ||
10619 Bitcast2->getValueType(0) != MVT::f32)
10620 return false;
10621
10622 if (Subtarget.isLittleEndian())
10623 std::swap(Bitcast, Bitcast2);
10624
10625 // Bitcast has the second float (in memory-layout order) and Bitcast2
10626 // has the first one.
10627
10628 SDValue BasePtr = LD->getBasePtr();
10629 if (LD->isIndexed()) {
10630 assert(LD->getAddressingMode() == ISD::PRE_INC &&
10631 "Non-pre-inc AM on PPC?");
10632 BasePtr =
10633 DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
10634 LD->getOffset());
10635 }
10636
10637 SDValue FloatLoad =
10638 DAG.getLoad(MVT::f32, dl, LD->getChain(), BasePtr,
10639 LD->getPointerInfo(), false, LD->isNonTemporal(),
10640 LD->isInvariant(), LD->getAlignment(), LD->getAAInfo());
10641 SDValue AddPtr =
10642 DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(),
10643 BasePtr, DAG.getIntPtrConstant(4, dl));
10644 SDValue FloatLoad2 =
10645 DAG.getLoad(MVT::f32, dl, SDValue(FloatLoad.getNode(), 1), AddPtr,
10646 LD->getPointerInfo().getWithOffset(4), false,
10647 LD->isNonTemporal(), LD->isInvariant(),
10648 MinAlign(LD->getAlignment(), 4), LD->getAAInfo());
10649
10650 if (LD->isIndexed()) {
10651 // Note that DAGCombine should re-form any pre-increment load(s) from
10652 // what is produced here if that makes sense.
10653 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), BasePtr);
10654 }
10655
10656 DCI.CombineTo(Bitcast2, FloatLoad);
10657 DCI.CombineTo(Bitcast, FloatLoad2);
10658
10659 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, LD->isIndexed() ? 2 : 1),
10660 SDValue(FloatLoad2.getNode(), 1));
10661 return true;
10662 };
10663
10664 if (ReplaceTwoFloatLoad())
10665 return SDValue(N, 0);
10666
Hal Finkelc93a9a22015-02-25 01:06:45 +000010667 EVT MemVT = LD->getMemoryVT();
10668 Type *Ty = MemVT.getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +000010669 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
Hal Finkelc93a9a22015-02-25 01:06:45 +000010670 Type *STy = MemVT.getScalarType().getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +000010671 unsigned ScalarABIAlignment = DAG.getDataLayout().getABITypeAlignment(STy);
Hal Finkelc93a9a22015-02-25 01:06:45 +000010672 if (LD->isUnindexed() && VT.isVector() &&
10673 ((Subtarget.hasAltivec() && ISD::isNON_EXTLoad(N) &&
10674 // P8 and later hardware should just use LOAD.
10675 !Subtarget.hasP8Vector() && (VT == MVT::v16i8 || VT == MVT::v8i16 ||
10676 VT == MVT::v4i32 || VT == MVT::v4f32)) ||
10677 (Subtarget.hasQPX() && (VT == MVT::v4f64 || VT == MVT::v4f32) &&
10678 LD->getAlignment() >= ScalarABIAlignment)) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +000010679 LD->getAlignment() < ABIAlignment) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000010680 // This is a type-legal unaligned Altivec or QPX load.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010681 SDValue Chain = LD->getChain();
10682 SDValue Ptr = LD->getBasePtr();
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010683 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelcf2e9082013-05-24 23:00:14 +000010684
10685 // This implements the loading of unaligned vectors as described in
10686 // the venerable Apple Velocity Engine overview. Specifically:
10687 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
10688 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
10689 //
10690 // The general idea is to expand a sequence of one or more unaligned
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010691 // loads into an alignment-based permutation-control instruction (lvsl
10692 // or lvsr), a series of regular vector loads (which always truncate
10693 // their input address to an aligned address), and a series of
10694 // permutations. The results of these permutations are the requested
10695 // loaded values. The trick is that the last "extra" load is not taken
10696 // from the address you might suspect (sizeof(vector) bytes after the
10697 // last requested load), but rather sizeof(vector) - 1 bytes after the
10698 // last requested vector. The point of this is to avoid a page fault if
10699 // the base address happened to be aligned. This works because if the
10700 // base address is aligned, then adding less than a full vector length
10701 // will cause the last vector in the sequence to be (re)loaded.
10702 // Otherwise, the next vector will be fetched as you might suspect was
10703 // necessary.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010704
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010705 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +000010706 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010707 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
10708 // optimization later.
Hal Finkelc93a9a22015-02-25 01:06:45 +000010709 Intrinsic::ID Intr, IntrLD, IntrPerm;
10710 MVT PermCntlTy, PermTy, LDTy;
10711 if (Subtarget.hasAltivec()) {
10712 Intr = isLittleEndian ? Intrinsic::ppc_altivec_lvsr :
10713 Intrinsic::ppc_altivec_lvsl;
10714 IntrLD = Intrinsic::ppc_altivec_lvx;
10715 IntrPerm = Intrinsic::ppc_altivec_vperm;
10716 PermCntlTy = MVT::v16i8;
10717 PermTy = MVT::v4i32;
10718 LDTy = MVT::v4i32;
10719 } else {
10720 Intr = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlpcld :
10721 Intrinsic::ppc_qpx_qvlpcls;
10722 IntrLD = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlfd :
10723 Intrinsic::ppc_qpx_qvlfs;
10724 IntrPerm = Intrinsic::ppc_qpx_qvfperm;
10725 PermCntlTy = MVT::v4f64;
10726 PermTy = MVT::v4f64;
10727 LDTy = MemVT.getSimpleVT();
10728 }
10729
10730 SDValue PermCntl = BuildIntrinsicOp(Intr, Ptr, DAG, dl, PermCntlTy);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010731
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010732 // Create the new MMO for the new base load. It is like the original MMO,
10733 // but represents an area in memory almost twice the vector size centered
10734 // on the original address. If the address is unaligned, we might start
10735 // reading up to (sizeof(vector)-1) bytes below the address of the
10736 // original unaligned load.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010737 MachineFunction &MF = DAG.getMachineFunction();
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010738 MachineMemOperand *BaseMMO =
Hal Finkel99d95322015-09-03 21:12:15 +000010739 MF.getMachineMemOperand(LD->getMemOperand(),
10740 -(long)MemVT.getStoreSize()+1,
Hal Finkelc93a9a22015-02-25 01:06:45 +000010741 2*MemVT.getStoreSize()-1);
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010742
10743 // Create the new base load.
Mehdi Amini44ede332015-07-09 02:09:04 +000010744 SDValue LDXIntID =
10745 DAG.getTargetConstant(IntrLD, dl, getPointerTy(MF.getDataLayout()));
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010746 SDValue BaseLoadOps[] = { Chain, LDXIntID, Ptr };
10747 SDValue BaseLoad =
10748 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
Hal Finkelc93a9a22015-02-25 01:06:45 +000010749 DAG.getVTList(PermTy, MVT::Other),
10750 BaseLoadOps, LDTy, BaseMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010751
10752 // Note that the value of IncOffset (which is provided to the next
10753 // load's pointer info offset value, and thus used to calculate the
10754 // alignment), and the value of IncValue (which is actually used to
10755 // increment the pointer value) are different! This is because we
10756 // require the next load to appear to be aligned, even though it
10757 // is actually offset from the base pointer by a lesser amount.
10758 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +000010759 int IncValue = IncOffset;
10760
10761 // Walk (both up and down) the chain looking for another load at the real
10762 // (aligned) offset (the alignment of the other load does not matter in
10763 // this case). If found, then do not use the offset reduction trick, as
10764 // that will prevent the loads from being later combined (as they would
10765 // otherwise be duplicates).
10766 if (!findConsecutiveLoad(LD, DAG))
10767 --IncValue;
10768
Mehdi Amini44ede332015-07-09 02:09:04 +000010769 SDValue Increment =
10770 DAG.getConstant(IncValue, dl, getPointerTy(MF.getDataLayout()));
Hal Finkelcf2e9082013-05-24 23:00:14 +000010771 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
10772
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010773 MachineMemOperand *ExtraMMO =
10774 MF.getMachineMemOperand(LD->getMemOperand(),
Hal Finkelc93a9a22015-02-25 01:06:45 +000010775 1, 2*MemVT.getStoreSize()-1);
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010776 SDValue ExtraLoadOps[] = { Chain, LDXIntID, Ptr };
Hal Finkelcf2e9082013-05-24 23:00:14 +000010777 SDValue ExtraLoad =
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010778 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
Hal Finkelc93a9a22015-02-25 01:06:45 +000010779 DAG.getVTList(PermTy, MVT::Other),
10780 ExtraLoadOps, LDTy, ExtraMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010781
10782 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
10783 BaseLoad.getValue(1), ExtraLoad.getValue(1));
10784
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010785 // Because vperm has a big-endian bias, we must reverse the order
10786 // of the input vectors and complement the permute control vector
10787 // when generating little endian code. We have already handled the
10788 // latter by using lvsr instead of lvsl, so just reverse BaseLoad
10789 // and ExtraLoad here.
10790 SDValue Perm;
10791 if (isLittleEndian)
Hal Finkelc93a9a22015-02-25 01:06:45 +000010792 Perm = BuildIntrinsicOp(IntrPerm,
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010793 ExtraLoad, BaseLoad, PermCntl, DAG, dl);
10794 else
Hal Finkelc93a9a22015-02-25 01:06:45 +000010795 Perm = BuildIntrinsicOp(IntrPerm,
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010796 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010797
Hal Finkelc93a9a22015-02-25 01:06:45 +000010798 if (VT != PermTy)
10799 Perm = Subtarget.hasAltivec() ?
10800 DAG.getNode(ISD::BITCAST, dl, VT, Perm) :
10801 DAG.getNode(ISD::FP_ROUND, dl, VT, Perm, // QPX
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010802 DAG.getTargetConstant(1, dl, MVT::i64));
Hal Finkelc93a9a22015-02-25 01:06:45 +000010803 // second argument is 1 because this rounding
10804 // is always exact.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010805
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010806 // The output of the permutation is our loaded result, the TokenFactor is
10807 // our new chain.
10808 DCI.CombineTo(N, Perm, TF);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010809 return SDValue(N, 0);
10810 }
10811 }
10812 break;
Eric Christophercccae792015-01-30 22:02:31 +000010813 case ISD::INTRINSIC_WO_CHAIN: {
10814 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelc93a9a22015-02-25 01:06:45 +000010815 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
Eric Christophercccae792015-01-30 22:02:31 +000010816 Intrinsic::ID Intr = (isLittleEndian ? Intrinsic::ppc_altivec_lvsr
10817 : Intrinsic::ppc_altivec_lvsl);
Hal Finkelc93a9a22015-02-25 01:06:45 +000010818 if ((IID == Intr ||
10819 IID == Intrinsic::ppc_qpx_qvlpcld ||
10820 IID == Intrinsic::ppc_qpx_qvlpcls) &&
10821 N->getOperand(1)->getOpcode() == ISD::ADD) {
Eric Christophercccae792015-01-30 22:02:31 +000010822 SDValue Add = N->getOperand(1);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010823
Hal Finkelc93a9a22015-02-25 01:06:45 +000010824 int Bits = IID == Intrinsic::ppc_qpx_qvlpcld ?
10825 5 /* 32 byte alignment */ : 4 /* 16 byte alignment */;
10826
Eric Christophercccae792015-01-30 22:02:31 +000010827 if (DAG.MaskedValueIsZero(
10828 Add->getOperand(1),
Hal Finkelc93a9a22015-02-25 01:06:45 +000010829 APInt::getAllOnesValue(Bits /* alignment */)
Eric Christophercccae792015-01-30 22:02:31 +000010830 .zext(
10831 Add.getValueType().getScalarType().getSizeInBits()))) {
10832 SDNode *BasePtr = Add->getOperand(0).getNode();
10833 for (SDNode::use_iterator UI = BasePtr->use_begin(),
10834 UE = BasePtr->use_end();
10835 UI != UE; ++UI) {
10836 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
Hal Finkelc93a9a22015-02-25 01:06:45 +000010837 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() == IID) {
Eric Christophercccae792015-01-30 22:02:31 +000010838 // We've found another LVSL/LVSR, and this address is an aligned
10839 // multiple of that one. The results will be the same, so use the
10840 // one we've just found instead.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010841
Eric Christophercccae792015-01-30 22:02:31 +000010842 return SDValue(*UI, 0);
10843 }
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010844 }
10845 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000010846
10847 if (isa<ConstantSDNode>(Add->getOperand(1))) {
10848 SDNode *BasePtr = Add->getOperand(0).getNode();
10849 for (SDNode::use_iterator UI = BasePtr->use_begin(),
10850 UE = BasePtr->use_end(); UI != UE; ++UI) {
10851 if (UI->getOpcode() == ISD::ADD &&
10852 isa<ConstantSDNode>(UI->getOperand(1)) &&
10853 (cast<ConstantSDNode>(Add->getOperand(1))->getZExtValue() -
10854 cast<ConstantSDNode>(UI->getOperand(1))->getZExtValue()) %
Aaron Ballman5561ed42015-02-25 13:05:24 +000010855 (1ULL << Bits) == 0) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000010856 SDNode *OtherAdd = *UI;
10857 for (SDNode::use_iterator VI = OtherAdd->use_begin(),
10858 VE = OtherAdd->use_end(); VI != VE; ++VI) {
10859 if (VI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
10860 cast<ConstantSDNode>(VI->getOperand(0))->getZExtValue() == IID) {
10861 return SDValue(*VI, 0);
10862 }
10863 }
10864 }
10865 }
10866 }
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010867 }
10868 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +000010869
10870 break;
Bill Schmidtfae5d712014-12-09 16:35:51 +000010871 case ISD::INTRINSIC_W_CHAIN: {
10872 // For little endian, VSX loads require generating lxvd2x/xxswapd.
Eric Christophercccae792015-01-30 22:02:31 +000010873 if (Subtarget.hasVSX() && Subtarget.isLittleEndian()) {
Bill Schmidtfae5d712014-12-09 16:35:51 +000010874 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10875 default:
10876 break;
10877 case Intrinsic::ppc_vsx_lxvw4x:
10878 case Intrinsic::ppc_vsx_lxvd2x:
10879 return expandVSXLoadForLE(N, DCI);
10880 }
10881 }
10882 break;
10883 }
10884 case ISD::INTRINSIC_VOID: {
10885 // For little endian, VSX stores require generating xxswapd/stxvd2x.
Eric Christophercccae792015-01-30 22:02:31 +000010886 if (Subtarget.hasVSX() && Subtarget.isLittleEndian()) {
Bill Schmidtfae5d712014-12-09 16:35:51 +000010887 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10888 default:
10889 break;
10890 case Intrinsic::ppc_vsx_stxvw4x:
10891 case Intrinsic::ppc_vsx_stxvd2x:
10892 return expandVSXStoreForLE(N, DCI);
10893 }
10894 }
10895 break;
10896 }
Chris Lattnera7976d32006-07-10 20:56:58 +000010897 case ISD::BSWAP:
10898 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +000010899 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +000010900 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +000010901 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
Eric Christophercccae792015-01-30 22:02:31 +000010902 (Subtarget.hasLDBRX() && Subtarget.isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +000010903 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010904 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +000010905 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +000010906 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010907 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +000010908 LD->getChain(), // Chain
10909 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +000010910 DAG.getValueType(N->getValueType(0)) // VT
10911 };
Dan Gohman48b185d2009-09-25 20:36:54 +000010912 SDValue BSLoad =
10913 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +000010914 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
10915 MVT::i64 : MVT::i32, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +000010916 Ops, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +000010917
Scott Michelcf0da6c2009-02-17 22:15:04 +000010918 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010919 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +000010920 if (N->getValueType(0) == MVT::i16)
10921 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010922
Chris Lattnera7976d32006-07-10 20:56:58 +000010923 // First, combine the bswap away. This makes the value produced by the
10924 // load dead.
10925 DCI.CombineTo(N, ResVal);
10926
10927 // Next, combine the load away, we give it a bogus result value but a real
10928 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +000010929 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +000010930
Chris Lattnera7976d32006-07-10 20:56:58 +000010931 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010932 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +000010933 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010934
Chris Lattner27f53452006-03-01 05:50:56 +000010935 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +000010936 case PPCISD::VCMP: {
10937 // If a VCMPo node already exists with exactly the same operands as this
10938 // node, use its result instead of this node (VCMPo computes both a CR6 and
10939 // a normal output).
10940 //
10941 if (!N->getOperand(0).hasOneUse() &&
10942 !N->getOperand(1).hasOneUse() &&
10943 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000010944
Chris Lattnerd4058a52006-03-31 06:02:07 +000010945 // Scan all of the users of the LHS, looking for VCMPo's that match.
Craig Topper062a2ba2014-04-25 05:30:21 +000010946 SDNode *VCMPoNode = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010947
Gabor Greiff304a7a2008-08-28 21:40:38 +000010948 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +000010949 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
10950 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +000010951 if (UI->getOpcode() == PPCISD::VCMPo &&
10952 UI->getOperand(1) == N->getOperand(1) &&
10953 UI->getOperand(2) == N->getOperand(2) &&
10954 UI->getOperand(0) == N->getOperand(0)) {
10955 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +000010956 break;
10957 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010958
Chris Lattner518834c2006-04-18 18:28:22 +000010959 // If there is no VCMPo node, or if the flag value has a single use, don't
10960 // transform this.
10961 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
10962 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010963
10964 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +000010965 // chain, this transformation is more complex. Note that multiple things
10966 // could use the value result, which we should ignore.
Craig Topper062a2ba2014-04-25 05:30:21 +000010967 SDNode *FlagUser = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010968 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Craig Topper062a2ba2014-04-25 05:30:21 +000010969 FlagUser == nullptr; ++UI) {
Chris Lattner518834c2006-04-18 18:28:22 +000010970 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +000010971 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +000010972 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010973 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +000010974 FlagUser = User;
10975 break;
10976 }
10977 }
10978 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010979
Ulrich Weigandd5ebc622013-07-03 17:05:42 +000010980 // If the user is a MFOCRF instruction, we know this is safe.
10981 // Otherwise we give up for right now.
10982 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010983 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +000010984 }
10985 break;
10986 }
Hal Finkel940ab932014-02-28 00:27:01 +000010987 case ISD::BRCOND: {
10988 SDValue Cond = N->getOperand(1);
10989 SDValue Target = N->getOperand(2);
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +000010990
Hal Finkel940ab932014-02-28 00:27:01 +000010991 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
10992 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
10993 Intrinsic::ppc_is_decremented_ctr_nonzero) {
10994
10995 // We now need to make the intrinsic dead (it cannot be instruction
10996 // selected).
10997 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
10998 assert(Cond.getNode()->hasOneUse() &&
10999 "Counter decrement has more than one use");
11000
11001 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
11002 N->getOperand(0), Target);
11003 }
11004 }
11005 break;
Chris Lattner9754d142006-04-18 17:59:36 +000011006 case ISD::BR_CC: {
11007 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +000011008 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +000011009 // lowering is done pre-legalize, because the legalizer lowers the predicate
11010 // compare down to code that is difficult to reassemble.
11011 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011012 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +000011013
11014 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
11015 // value. If so, pass-through the AND to get to the intrinsic.
11016 if (LHS.getOpcode() == ISD::AND &&
11017 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
11018 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
11019 Intrinsic::ppc_is_decremented_ctr_nonzero &&
11020 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Artyom Skrobov314ee042015-11-25 19:41:11 +000011021 !isNullConstant(LHS.getOperand(1)))
Hal Finkel25c19922013-05-15 21:37:41 +000011022 LHS = LHS.getOperand(0);
11023
11024 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
11025 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
11026 Intrinsic::ppc_is_decremented_ctr_nonzero &&
11027 isa<ConstantSDNode>(RHS)) {
11028 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
11029 "Counter decrement comparison is not EQ or NE");
11030
11031 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
11032 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
11033 (CC == ISD::SETNE && !Val);
11034
11035 // We now need to make the intrinsic dead (it cannot be instruction
11036 // selected).
11037 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
11038 assert(LHS.getNode()->hasOneUse() &&
11039 "Counter decrement has more than one use");
11040
11041 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
11042 N->getOperand(0), N->getOperand(4));
11043 }
11044
Chris Lattner9754d142006-04-18 17:59:36 +000011045 int CompareOpc;
11046 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011047
Chris Lattner9754d142006-04-18 17:59:36 +000011048 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
11049 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
Nemanja Ivanovic2c84b292015-09-29 17:41:53 +000011050 getVectorCompareInfo(LHS, CompareOpc, isDot, Subtarget)) {
Chris Lattner9754d142006-04-18 17:59:36 +000011051 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +000011052
Chris Lattner9754d142006-04-18 17:59:36 +000011053 // If this is a comparison against something other than 0/1, then we know
11054 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +000011055 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +000011056 if (Val != 0 && Val != 1) {
11057 if (CC == ISD::SETEQ) // Cond never true, remove branch.
11058 return N->getOperand(0);
11059 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +000011060 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +000011061 N->getOperand(0), N->getOperand(4));
11062 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011063
Chris Lattner9754d142006-04-18 17:59:36 +000011064 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011065
Chris Lattner9754d142006-04-18 17:59:36 +000011066 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011067 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +000011068 LHS.getOperand(2), // LHS of compare
11069 LHS.getOperand(3), // RHS of compare
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011070 DAG.getConstant(CompareOpc, dl, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +000011071 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +000011072 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +000011073 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011074
Chris Lattner9754d142006-04-18 17:59:36 +000011075 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011076 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +000011077 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +000011078 default: // Can't happen, don't crash on invalid number though.
11079 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011080 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +000011081 break;
11082 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011083 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +000011084 break;
11085 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011086 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +000011087 break;
11088 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000011089 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +000011090 break;
11091 }
11092
Owen Anderson9f944592009-08-11 20:47:22 +000011093 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011094 DAG.getConstant(CompOpc, dl, MVT::i32),
Owen Anderson9f944592009-08-11 20:47:22 +000011095 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +000011096 N->getOperand(4), CompNode.getValue(1));
11097 }
11098 break;
11099 }
Chris Lattnerf4184352006-03-01 04:57:39 +000011100 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011101
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011102 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +000011103}
11104
Hal Finkel13d104b2014-12-11 18:37:52 +000011105SDValue
11106PPCTargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
11107 SelectionDAG &DAG,
11108 std::vector<SDNode *> *Created) const {
11109 // fold (sdiv X, pow2)
11110 EVT VT = N->getValueType(0);
Hal Finkel04b16b52014-12-23 08:38:50 +000011111 if (VT == MVT::i64 && !Subtarget.isPPC64())
11112 return SDValue();
Hal Finkel13d104b2014-12-11 18:37:52 +000011113 if ((VT != MVT::i32 && VT != MVT::i64) ||
11114 !(Divisor.isPowerOf2() || (-Divisor).isPowerOf2()))
11115 return SDValue();
11116
11117 SDLoc DL(N);
11118 SDValue N0 = N->getOperand(0);
11119
11120 bool IsNegPow2 = (-Divisor).isPowerOf2();
11121 unsigned Lg2 = (IsNegPow2 ? -Divisor : Divisor).countTrailingZeros();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011122 SDValue ShiftAmt = DAG.getConstant(Lg2, DL, VT);
Hal Finkel13d104b2014-12-11 18:37:52 +000011123
11124 SDValue Op = DAG.getNode(PPCISD::SRA_ADDZE, DL, VT, N0, ShiftAmt);
11125 if (Created)
11126 Created->push_back(Op.getNode());
11127
11128 if (IsNegPow2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011129 Op = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Op);
Hal Finkel13d104b2014-12-11 18:37:52 +000011130 if (Created)
11131 Created->push_back(Op.getNode());
11132 }
11133
11134 return Op;
11135}
11136
Chris Lattner4211ca92006-04-14 06:01:58 +000011137//===----------------------------------------------------------------------===//
11138// Inline Assembly Support
11139//===----------------------------------------------------------------------===//
11140
Jay Foada0653a32014-05-14 21:14:37 +000011141void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
11142 APInt &KnownZero,
11143 APInt &KnownOne,
11144 const SelectionDAG &DAG,
11145 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +000011146 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +000011147 switch (Op.getOpcode()) {
11148 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +000011149 case PPCISD::LBRX: {
11150 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +000011151 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +000011152 KnownZero = 0xFFFF0000;
11153 break;
11154 }
Chris Lattnerc5287c02006-04-02 06:26:07 +000011155 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011156 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +000011157 default: break;
11158 case Intrinsic::ppc_altivec_vcmpbfp_p:
11159 case Intrinsic::ppc_altivec_vcmpeqfp_p:
11160 case Intrinsic::ppc_altivec_vcmpequb_p:
11161 case Intrinsic::ppc_altivec_vcmpequh_p:
11162 case Intrinsic::ppc_altivec_vcmpequw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000011163 case Intrinsic::ppc_altivec_vcmpequd_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000011164 case Intrinsic::ppc_altivec_vcmpgefp_p:
11165 case Intrinsic::ppc_altivec_vcmpgtfp_p:
11166 case Intrinsic::ppc_altivec_vcmpgtsb_p:
11167 case Intrinsic::ppc_altivec_vcmpgtsh_p:
11168 case Intrinsic::ppc_altivec_vcmpgtsw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000011169 case Intrinsic::ppc_altivec_vcmpgtsd_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000011170 case Intrinsic::ppc_altivec_vcmpgtub_p:
11171 case Intrinsic::ppc_altivec_vcmpgtuh_p:
11172 case Intrinsic::ppc_altivec_vcmpgtuw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000011173 case Intrinsic::ppc_altivec_vcmpgtud_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000011174 KnownZero = ~1U; // All bits but the low one are known to be zero.
11175 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011176 }
Chris Lattnerc5287c02006-04-02 06:26:07 +000011177 }
11178 }
11179}
11180
Hal Finkel57725662015-01-03 17:58:24 +000011181unsigned PPCTargetLowering::getPrefLoopAlignment(MachineLoop *ML) const {
11182 switch (Subtarget.getDarwinDirective()) {
11183 default: break;
11184 case PPC::DIR_970:
11185 case PPC::DIR_PWR4:
11186 case PPC::DIR_PWR5:
11187 case PPC::DIR_PWR5X:
11188 case PPC::DIR_PWR6:
11189 case PPC::DIR_PWR6X:
11190 case PPC::DIR_PWR7:
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +000011191 case PPC::DIR_PWR8:
11192 case PPC::DIR_PWR9: {
Hal Finkel57725662015-01-03 17:58:24 +000011193 if (!ML)
11194 break;
11195
Eric Christophercccae792015-01-30 22:02:31 +000011196 const PPCInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel57725662015-01-03 17:58:24 +000011197
11198 // For small loops (between 5 and 8 instructions), align to a 32-byte
11199 // boundary so that the entire loop fits in one instruction-cache line.
11200 uint64_t LoopSize = 0;
11201 for (auto I = ML->block_begin(), IE = ML->block_end(); I != IE; ++I)
Chad Rosierbc9d4f92015-12-14 14:44:06 +000011202 for (auto J = (*I)->begin(), JE = (*I)->end(); J != JE; ++J) {
Hal Finkel57725662015-01-03 17:58:24 +000011203 LoopSize += TII->GetInstSizeInBytes(J);
Chad Rosierbc9d4f92015-12-14 14:44:06 +000011204 if (LoopSize > 32)
11205 break;
11206 }
Hal Finkel57725662015-01-03 17:58:24 +000011207
11208 if (LoopSize > 16 && LoopSize <= 32)
11209 return 5;
11210
11211 break;
11212 }
11213 }
11214
11215 return TargetLowering::getPrefLoopAlignment(ML);
11216}
Chris Lattnerc5287c02006-04-02 06:26:07 +000011217
Chris Lattnerd6855142007-03-25 02:14:49 +000011218/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +000011219/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +000011220PPCTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011221PPCTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000011222 if (Constraint.size() == 1) {
11223 switch (Constraint[0]) {
11224 default: break;
11225 case 'b':
11226 case 'r':
11227 case 'f':
Eric Christopherb979d512016-03-24 21:04:52 +000011228 case 'd':
Chris Lattnerd6855142007-03-25 02:14:49 +000011229 case 'v':
11230 case 'y':
11231 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +000011232 case 'Z':
11233 // FIXME: While Z does indicate a memory constraint, it specifically
11234 // indicates an r+r address (used in conjunction with the 'y' modifier
11235 // in the replacement string). Currently, we're forcing the base
11236 // register to be r0 in the asm printer (which is interpreted as zero)
11237 // and forming the complete address in the second register. This is
11238 // suboptimal.
11239 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000011240 }
Hal Finkel6aca2372014-03-02 18:23:39 +000011241 } else if (Constraint == "wc") { // individual CR bits.
11242 return C_RegisterClass;
Hal Finkel27774d92014-03-13 07:58:58 +000011243 } else if (Constraint == "wa" || Constraint == "wd" ||
11244 Constraint == "wf" || Constraint == "ws") {
11245 return C_RegisterClass; // VSX registers.
Chris Lattnerd6855142007-03-25 02:14:49 +000011246 }
11247 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +000011248}
11249
John Thompsone8360b72010-10-29 17:29:13 +000011250/// Examine constraint type and operand type and determine a weight value.
11251/// This object must already have been set up with the operand type
11252/// and the current alternative constraint selected.
11253TargetLowering::ConstraintWeight
11254PPCTargetLowering::getSingleConstraintMatchWeight(
11255 AsmOperandInfo &info, const char *constraint) const {
11256 ConstraintWeight weight = CW_Invalid;
11257 Value *CallOperandVal = info.CallOperandVal;
11258 // If we don't have a value, we can't do a match,
11259 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000011260 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000011261 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000011262 Type *type = CallOperandVal->getType();
Hal Finkel6aca2372014-03-02 18:23:39 +000011263
John Thompsone8360b72010-10-29 17:29:13 +000011264 // Look at the constraint type.
Hal Finkel6aca2372014-03-02 18:23:39 +000011265 if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
11266 return CW_Register; // an individual CR bit.
Hal Finkel27774d92014-03-13 07:58:58 +000011267 else if ((StringRef(constraint) == "wa" ||
11268 StringRef(constraint) == "wd" ||
11269 StringRef(constraint) == "wf") &&
11270 type->isVectorTy())
11271 return CW_Register;
11272 else if (StringRef(constraint) == "ws" && type->isDoubleTy())
11273 return CW_Register;
Hal Finkel6aca2372014-03-02 18:23:39 +000011274
John Thompsone8360b72010-10-29 17:29:13 +000011275 switch (*constraint) {
11276 default:
11277 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11278 break;
11279 case 'b':
11280 if (type->isIntegerTy())
11281 weight = CW_Register;
11282 break;
11283 case 'f':
11284 if (type->isFloatTy())
11285 weight = CW_Register;
11286 break;
11287 case 'd':
11288 if (type->isDoubleTy())
11289 weight = CW_Register;
11290 break;
11291 case 'v':
11292 if (type->isVectorTy())
11293 weight = CW_Register;
11294 break;
11295 case 'y':
11296 weight = CW_Register;
11297 break;
Hal Finkel4f24c622012-11-05 18:18:42 +000011298 case 'Z':
11299 weight = CW_Memory;
11300 break;
John Thompsone8360b72010-10-29 17:29:13 +000011301 }
11302 return weight;
11303}
11304
Eric Christopher11e4df72015-02-26 22:38:43 +000011305std::pair<unsigned, const TargetRegisterClass *>
11306PPCTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011307 StringRef Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000011308 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +000011309 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +000011310 // GCC RS6000 Constraint Letters
11311 switch (Constraint[0]) {
11312 case 'b': // R1-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011313 if (VT == MVT::i64 && Subtarget.isPPC64())
Hal Finkel638a9fa2013-03-19 18:51:05 +000011314 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
11315 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000011316 case 'r': // R0-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011317 if (VT == MVT::i64 && Subtarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +000011318 return std::make_pair(0U, &PPC::G8RCRegClass);
11319 return std::make_pair(0U, &PPC::GPRCRegClass);
Eric Christopherb979d512016-03-24 21:04:52 +000011320 // 'd' and 'f' constraints are both defined to be "the floating point
11321 // registers", where one is for 32-bit and the other for 64-bit. We don't
11322 // really care overly much here so just give them all the same reg classes.
11323 case 'd':
Chris Lattner584a11a2006-11-02 01:44:04 +000011324 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +000011325 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +000011326 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +000011327 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +000011328 return std::make_pair(0U, &PPC::F8RCRegClass);
Hal Finkelc93a9a22015-02-25 01:06:45 +000011329 if (VT == MVT::v4f64 && Subtarget.hasQPX())
11330 return std::make_pair(0U, &PPC::QFRCRegClass);
11331 if (VT == MVT::v4f32 && Subtarget.hasQPX())
11332 return std::make_pair(0U, &PPC::QSRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000011333 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011334 case 'v':
Hal Finkelc93a9a22015-02-25 01:06:45 +000011335 if (VT == MVT::v4f64 && Subtarget.hasQPX())
11336 return std::make_pair(0U, &PPC::QFRCRegClass);
11337 if (VT == MVT::v4f32 && Subtarget.hasQPX())
11338 return std::make_pair(0U, &PPC::QSRCRegClass);
Hal Finkelbdd292a2015-10-28 23:03:45 +000011339 if (Subtarget.hasAltivec())
11340 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000011341 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +000011342 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +000011343 }
Hal Finkel34d41492015-10-28 22:25:52 +000011344 } else if (Constraint == "wc" && Subtarget.useCRBits()) {
11345 // An individual CR bit.
Hal Finkel6aca2372014-03-02 18:23:39 +000011346 return std::make_pair(0U, &PPC::CRBITRCRegClass);
Hal Finkelbdd292a2015-10-28 23:03:45 +000011347 } else if ((Constraint == "wa" || Constraint == "wd" ||
11348 Constraint == "wf") && Subtarget.hasVSX()) {
Hal Finkel27774d92014-03-13 07:58:58 +000011349 return std::make_pair(0U, &PPC::VSRCRegClass);
Hal Finkelbdd292a2015-10-28 23:03:45 +000011350 } else if (Constraint == "ws" && Subtarget.hasVSX()) {
11351 if (VT == MVT::f32 && Subtarget.hasP8Vector())
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +000011352 return std::make_pair(0U, &PPC::VSSRCRegClass);
11353 else
11354 return std::make_pair(0U, &PPC::VSFRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +000011355 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011356
Eric Christopher11e4df72015-02-26 22:38:43 +000011357 std::pair<unsigned, const TargetRegisterClass *> R =
11358 TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Hal Finkelb176acb2013-08-03 12:25:10 +000011359
11360 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
11361 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
11362 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
11363 // register.
11364 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
11365 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011366 if (R.first && VT == MVT::i64 && Subtarget.isPPC64() &&
Eric Christopher11e4df72015-02-26 22:38:43 +000011367 PPC::GPRCRegClass.contains(R.first))
Hal Finkelb176acb2013-08-03 12:25:10 +000011368 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +000011369 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +000011370 &PPC::G8RCRegClass);
Hal Finkelb176acb2013-08-03 12:25:10 +000011371
Hal Finkelaa10b3c2014-12-08 22:54:22 +000011372 // GCC accepts 'cc' as an alias for 'cr0', and we need to do the same.
11373 if (!R.second && StringRef("{cc}").equals_lower(Constraint)) {
11374 R.first = PPC::CR0;
11375 R.second = &PPC::CRRCRegClass;
11376 }
11377
Hal Finkelb176acb2013-08-03 12:25:10 +000011378 return R;
Chris Lattner01513612006-01-31 19:20:21 +000011379}
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011380
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000011381/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +000011382/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +000011383void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000011384 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011385 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +000011386 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000011387 SDValue Result;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011388
Eric Christopherde9399b2011-06-02 23:16:42 +000011389 // Only support length 1 constraints.
11390 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011391
Eric Christopherde9399b2011-06-02 23:16:42 +000011392 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011393 switch (Letter) {
11394 default: break;
11395 case 'I':
11396 case 'J':
11397 case 'K':
11398 case 'L':
11399 case 'M':
11400 case 'N':
11401 case 'O':
11402 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +000011403 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000011404 if (!CST) return; // Must be an immediate to match.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011405 SDLoc dl(Op);
Hal Finkelc91fc112014-12-03 09:37:50 +000011406 int64_t Value = CST->getSExtValue();
11407 EVT TCVT = MVT::i64; // All constants taken to be 64 bits so that negative
11408 // numbers are printed as such.
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011409 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000011410 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011411 case 'I': // "I" is a signed 16-bit constant.
Hal Finkelc91fc112014-12-03 09:37:50 +000011412 if (isInt<16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011413 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011414 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011415 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
Hal Finkelc91fc112014-12-03 09:37:50 +000011416 if (isShiftedUInt<16, 16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011417 Result = DAG.getTargetConstant(Value, dl, TCVT);
Hal Finkelc91fc112014-12-03 09:37:50 +000011418 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011419 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Hal Finkelc91fc112014-12-03 09:37:50 +000011420 if (isShiftedInt<16, 16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011421 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011422 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011423 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Hal Finkelc91fc112014-12-03 09:37:50 +000011424 if (isUInt<16>(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011425 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011426 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011427 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +000011428 if (Value > 31)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011429 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011430 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011431 case 'N': // "N" is a positive constant that is an exact power of two.
Hal Finkelc91fc112014-12-03 09:37:50 +000011432 if (Value > 0 && isPowerOf2_64(Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011433 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011434 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011435 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +000011436 if (Value == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011437 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011438 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011439 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Hal Finkelc91fc112014-12-03 09:37:50 +000011440 if (isInt<16>(-Value))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011441 Result = DAG.getTargetConstant(Value, dl, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000011442 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011443 }
11444 break;
11445 }
11446 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011447
Gabor Greiff304a7a2008-08-28 21:40:38 +000011448 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000011449 Ops.push_back(Result);
11450 return;
11451 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011452
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011453 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +000011454 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +000011455}
Evan Cheng2dd2c652006-03-13 23:20:37 +000011456
Chris Lattner1eb94d92007-03-30 23:15:24 +000011457// isLegalAddressingMode - Return true if the addressing mode represented
11458// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011459bool PPCTargetLowering::isLegalAddressingMode(const DataLayout &DL,
11460 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000011461 unsigned AS) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +000011462 // PPC does not allow r+i addressing modes for vectors!
11463 if (Ty->isVectorTy() && AM.BaseOffs != 0)
11464 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011465
Chris Lattner1eb94d92007-03-30 23:15:24 +000011466 // PPC allows a sign-extended 16-bit immediate field.
11467 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
11468 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011469
Chris Lattner1eb94d92007-03-30 23:15:24 +000011470 // No global is ever allowed as a base.
11471 if (AM.BaseGV)
11472 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011473
11474 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +000011475 switch (AM.Scale) {
11476 case 0: // "r+i" or just "i", depending on HasBaseReg.
11477 break;
11478 case 1:
11479 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
11480 return false;
11481 // Otherwise we have r+r or r+i.
11482 break;
11483 case 2:
11484 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
11485 return false;
11486 // Allow 2*r as r+r.
11487 break;
Chris Lattner19ccd622007-04-09 22:10:05 +000011488 default:
11489 // No other scales are supported.
11490 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +000011491 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011492
Chris Lattner1eb94d92007-03-30 23:15:24 +000011493 return true;
11494}
11495
Dan Gohman21cea8a2010-04-17 15:26:15 +000011496SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
11497 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +000011498 MachineFunction &MF = DAG.getMachineFunction();
11499 MachineFrameInfo *MFI = MF.getFrameInfo();
11500 MFI->setReturnAddressIsTaken(true);
11501
Bill Wendling908bf812014-01-06 00:43:20 +000011502 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +000011503 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +000011504
Andrew Trickef9de2a2013-05-25 02:42:55 +000011505 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +000011506 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +000011507
Dale Johannesen81bfca72010-05-03 22:59:34 +000011508 // Make sure the function does not optimize away the store of the RA to
11509 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +000011510 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +000011511 FuncInfo->setLRStoreRequired();
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011512 bool isPPC64 = Subtarget.isPPC64();
Mehdi Amini44ede332015-07-09 02:09:04 +000011513 auto PtrVT = getPointerTy(MF.getDataLayout());
Dale Johannesen81bfca72010-05-03 22:59:34 +000011514
11515 if (Depth > 0) {
11516 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
11517 SDValue Offset =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011518 DAG.getConstant(Subtarget.getFrameLowering()->getReturnSaveOffset(), dl,
Eric Christopherf71609b2015-02-13 00:39:27 +000011519 isPPC64 ? MVT::i64 : MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +000011520 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
11521 DAG.getNode(ISD::ADD, dl, PtrVT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +000011522 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +000011523 }
Chris Lattnerf6a81562007-12-08 06:59:59 +000011524
Chris Lattnerf6a81562007-12-08 06:59:59 +000011525 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011526 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Mehdi Amini44ede332015-07-09 02:09:04 +000011527 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), RetAddrFI,
11528 MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +000011529}
11530
Dan Gohman21cea8a2010-04-17 15:26:15 +000011531SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
11532 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +000011533 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +000011534 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +000011535
Nicolas Geoffray75ab9792007-03-01 13:11:38 +000011536 MachineFunction &MF = DAG.getMachineFunction();
11537 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +000011538 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +000011539
Mehdi Amini44ede332015-07-09 02:09:04 +000011540 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout());
11541 bool isPPC64 = PtrVT == MVT::i64;
11542
Hal Finkelaa03c032013-03-21 19:03:19 +000011543 // Naked functions never have a frame pointer, and so we use r1. For all
11544 // other functions, this decision must be delayed until during PEI.
11545 unsigned FrameReg;
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +000011546 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Hal Finkelaa03c032013-03-21 19:03:19 +000011547 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
11548 else
11549 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
11550
Dale Johannesen81bfca72010-05-03 22:59:34 +000011551 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
11552 PtrVT);
11553 while (Depth--)
11554 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +000011555 FrameAddr, MachinePointerInfo(), false, false,
11556 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +000011557 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +000011558}
Dan Gohmanc14e5222008-10-21 03:41:46 +000011559
Hal Finkel0d8db462014-05-11 19:29:11 +000011560// FIXME? Maybe this could be a TableGen attribute on some registers and
11561// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +000011562unsigned PPCTargetLowering::getRegisterByName(const char* RegName, EVT VT,
11563 SelectionDAG &DAG) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011564 bool isPPC64 = Subtarget.isPPC64();
11565 bool isDarwinABI = Subtarget.isDarwinABI();
Hal Finkel0d8db462014-05-11 19:29:11 +000011566
11567 if ((isPPC64 && VT != MVT::i64 && VT != MVT::i32) ||
11568 (!isPPC64 && VT != MVT::i32))
11569 report_fatal_error("Invalid register global variable type");
11570
11571 bool is64Bit = isPPC64 && VT == MVT::i64;
11572 unsigned Reg = StringSwitch<unsigned>(RegName)
11573 .Case("r1", is64Bit ? PPC::X1 : PPC::R1)
Hal Finkele6698d52015-02-01 15:03:28 +000011574 .Case("r2", (isDarwinABI || isPPC64) ? 0 : PPC::R2)
Hal Finkel0d8db462014-05-11 19:29:11 +000011575 .Case("r13", (!isPPC64 && isDarwinABI) ? 0 :
11576 (is64Bit ? PPC::X13 : PPC::R13))
11577 .Default(0);
11578
11579 if (Reg)
11580 return Reg;
11581 report_fatal_error("Invalid register name global variable");
11582}
11583
Dan Gohmanc14e5222008-10-21 03:41:46 +000011584bool
11585PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11586 // The PowerPC target isn't yet aware of offsets.
11587 return false;
11588}
Tilmann Schellerb93960d2009-07-03 06:45:56 +000011589
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011590bool PPCTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11591 const CallInst &I,
11592 unsigned Intrinsic) const {
11593
11594 switch (Intrinsic) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000011595 case Intrinsic::ppc_qpx_qvlfd:
11596 case Intrinsic::ppc_qpx_qvlfs:
11597 case Intrinsic::ppc_qpx_qvlfcd:
11598 case Intrinsic::ppc_qpx_qvlfcs:
11599 case Intrinsic::ppc_qpx_qvlfiwa:
11600 case Intrinsic::ppc_qpx_qvlfiwz:
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011601 case Intrinsic::ppc_altivec_lvx:
11602 case Intrinsic::ppc_altivec_lvxl:
11603 case Intrinsic::ppc_altivec_lvebx:
11604 case Intrinsic::ppc_altivec_lvehx:
Bill Schmidt72954782014-11-12 04:19:40 +000011605 case Intrinsic::ppc_altivec_lvewx:
11606 case Intrinsic::ppc_vsx_lxvd2x:
11607 case Intrinsic::ppc_vsx_lxvw4x: {
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011608 EVT VT;
11609 switch (Intrinsic) {
11610 case Intrinsic::ppc_altivec_lvebx:
11611 VT = MVT::i8;
11612 break;
11613 case Intrinsic::ppc_altivec_lvehx:
11614 VT = MVT::i16;
11615 break;
11616 case Intrinsic::ppc_altivec_lvewx:
11617 VT = MVT::i32;
11618 break;
Bill Schmidt72954782014-11-12 04:19:40 +000011619 case Intrinsic::ppc_vsx_lxvd2x:
11620 VT = MVT::v2f64;
11621 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +000011622 case Intrinsic::ppc_qpx_qvlfd:
11623 VT = MVT::v4f64;
11624 break;
11625 case Intrinsic::ppc_qpx_qvlfs:
11626 VT = MVT::v4f32;
11627 break;
11628 case Intrinsic::ppc_qpx_qvlfcd:
11629 VT = MVT::v2f64;
11630 break;
11631 case Intrinsic::ppc_qpx_qvlfcs:
11632 VT = MVT::v2f32;
11633 break;
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011634 default:
11635 VT = MVT::v4i32;
11636 break;
11637 }
11638
11639 Info.opc = ISD::INTRINSIC_W_CHAIN;
11640 Info.memVT = VT;
11641 Info.ptrVal = I.getArgOperand(0);
11642 Info.offset = -VT.getStoreSize()+1;
11643 Info.size = 2*VT.getStoreSize()-1;
11644 Info.align = 1;
11645 Info.vol = false;
11646 Info.readMem = true;
11647 Info.writeMem = false;
11648 return true;
11649 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000011650 case Intrinsic::ppc_qpx_qvlfda:
11651 case Intrinsic::ppc_qpx_qvlfsa:
11652 case Intrinsic::ppc_qpx_qvlfcda:
11653 case Intrinsic::ppc_qpx_qvlfcsa:
11654 case Intrinsic::ppc_qpx_qvlfiwaa:
11655 case Intrinsic::ppc_qpx_qvlfiwza: {
11656 EVT VT;
11657 switch (Intrinsic) {
11658 case Intrinsic::ppc_qpx_qvlfda:
11659 VT = MVT::v4f64;
11660 break;
11661 case Intrinsic::ppc_qpx_qvlfsa:
11662 VT = MVT::v4f32;
11663 break;
11664 case Intrinsic::ppc_qpx_qvlfcda:
11665 VT = MVT::v2f64;
11666 break;
11667 case Intrinsic::ppc_qpx_qvlfcsa:
11668 VT = MVT::v2f32;
11669 break;
11670 default:
11671 VT = MVT::v4i32;
11672 break;
11673 }
11674
11675 Info.opc = ISD::INTRINSIC_W_CHAIN;
11676 Info.memVT = VT;
11677 Info.ptrVal = I.getArgOperand(0);
11678 Info.offset = 0;
11679 Info.size = VT.getStoreSize();
11680 Info.align = 1;
11681 Info.vol = false;
11682 Info.readMem = true;
11683 Info.writeMem = false;
11684 return true;
11685 }
11686 case Intrinsic::ppc_qpx_qvstfd:
11687 case Intrinsic::ppc_qpx_qvstfs:
11688 case Intrinsic::ppc_qpx_qvstfcd:
11689 case Intrinsic::ppc_qpx_qvstfcs:
11690 case Intrinsic::ppc_qpx_qvstfiw:
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011691 case Intrinsic::ppc_altivec_stvx:
11692 case Intrinsic::ppc_altivec_stvxl:
11693 case Intrinsic::ppc_altivec_stvebx:
11694 case Intrinsic::ppc_altivec_stvehx:
Bill Schmidt72954782014-11-12 04:19:40 +000011695 case Intrinsic::ppc_altivec_stvewx:
11696 case Intrinsic::ppc_vsx_stxvd2x:
11697 case Intrinsic::ppc_vsx_stxvw4x: {
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011698 EVT VT;
11699 switch (Intrinsic) {
11700 case Intrinsic::ppc_altivec_stvebx:
11701 VT = MVT::i8;
11702 break;
11703 case Intrinsic::ppc_altivec_stvehx:
11704 VT = MVT::i16;
11705 break;
11706 case Intrinsic::ppc_altivec_stvewx:
11707 VT = MVT::i32;
11708 break;
Bill Schmidt72954782014-11-12 04:19:40 +000011709 case Intrinsic::ppc_vsx_stxvd2x:
11710 VT = MVT::v2f64;
11711 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +000011712 case Intrinsic::ppc_qpx_qvstfd:
11713 VT = MVT::v4f64;
11714 break;
11715 case Intrinsic::ppc_qpx_qvstfs:
11716 VT = MVT::v4f32;
11717 break;
11718 case Intrinsic::ppc_qpx_qvstfcd:
11719 VT = MVT::v2f64;
11720 break;
11721 case Intrinsic::ppc_qpx_qvstfcs:
11722 VT = MVT::v2f32;
11723 break;
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011724 default:
11725 VT = MVT::v4i32;
11726 break;
11727 }
11728
11729 Info.opc = ISD::INTRINSIC_VOID;
11730 Info.memVT = VT;
11731 Info.ptrVal = I.getArgOperand(1);
11732 Info.offset = -VT.getStoreSize()+1;
11733 Info.size = 2*VT.getStoreSize()-1;
11734 Info.align = 1;
11735 Info.vol = false;
11736 Info.readMem = false;
11737 Info.writeMem = true;
11738 return true;
11739 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000011740 case Intrinsic::ppc_qpx_qvstfda:
11741 case Intrinsic::ppc_qpx_qvstfsa:
11742 case Intrinsic::ppc_qpx_qvstfcda:
11743 case Intrinsic::ppc_qpx_qvstfcsa:
11744 case Intrinsic::ppc_qpx_qvstfiwa: {
11745 EVT VT;
11746 switch (Intrinsic) {
11747 case Intrinsic::ppc_qpx_qvstfda:
11748 VT = MVT::v4f64;
11749 break;
11750 case Intrinsic::ppc_qpx_qvstfsa:
11751 VT = MVT::v4f32;
11752 break;
11753 case Intrinsic::ppc_qpx_qvstfcda:
11754 VT = MVT::v2f64;
11755 break;
11756 case Intrinsic::ppc_qpx_qvstfcsa:
11757 VT = MVT::v2f32;
11758 break;
11759 default:
11760 VT = MVT::v4i32;
11761 break;
11762 }
11763
11764 Info.opc = ISD::INTRINSIC_VOID;
11765 Info.memVT = VT;
11766 Info.ptrVal = I.getArgOperand(1);
11767 Info.offset = 0;
11768 Info.size = VT.getStoreSize();
11769 Info.align = 1;
11770 Info.vol = false;
11771 Info.readMem = false;
11772 Info.writeMem = true;
11773 return true;
11774 }
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011775 default:
11776 break;
11777 }
11778
11779 return false;
11780}
11781
Evan Chengd9929f02010-04-01 20:10:42 +000011782/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +000011783/// and store operations as a result of memset, memcpy, and memmove
11784/// lowering. If DstAlign is zero that means it's safe to destination
11785/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
11786/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +000011787/// probably because the source does not need to be loaded. If 'IsMemset' is
11788/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
11789/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
11790/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +000011791/// It returns EVT::Other if the type should be determined using generic
11792/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +000011793EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
11794 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000011795 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +000011796 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +000011797 MachineFunction &MF) const {
Hal Finkel52368d42015-03-31 20:56:09 +000011798 if (getTargetMachine().getOptLevel() != CodeGenOpt::None) {
11799 const Function *F = MF.getFunction();
11800 // When expanding a memset, require at least two QPX instructions to cover
11801 // the cost of loading the value to be stored from the constant pool.
11802 if (Subtarget.hasQPX() && Size >= 32 && (!IsMemset || Size >= 64) &&
11803 (!SrcAlign || SrcAlign >= 32) && (!DstAlign || DstAlign >= 32) &&
11804 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
11805 return MVT::v4f64;
11806 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000011807
Hal Finkel52368d42015-03-31 20:56:09 +000011808 // We should use Altivec/VSX loads and stores when available. For unaligned
11809 // addresses, unaligned VSX loads are only fast starting with the P8.
11810 if (Subtarget.hasAltivec() && Size >= 16 &&
11811 (((!SrcAlign || SrcAlign >= 16) && (!DstAlign || DstAlign >= 16)) ||
11812 ((IsMemset && Subtarget.hasVSX()) || Subtarget.hasP8Vector())))
11813 return MVT::v4i32;
11814 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000011815
Eric Christopherd90a8742014-06-12 22:38:20 +000011816 if (Subtarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +000011817 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +000011818 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000011819
11820 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +000011821}
Hal Finkel88ed4e32012-04-01 19:23:08 +000011822
Hal Finkel34974ed2014-04-12 21:52:38 +000011823/// \brief Returns true if it is beneficial to convert a load of a constant
11824/// to just the constant itself.
11825bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11826 Type *Ty) const {
11827 assert(Ty->isIntegerTy());
11828
11829 unsigned BitSize = Ty->getPrimitiveSizeInBits();
Alexander Kornienko175a7cb2015-12-28 13:38:42 +000011830 return !(BitSize == 0 || BitSize > 64);
Hal Finkel34974ed2014-04-12 21:52:38 +000011831}
11832
11833bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
11834 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
11835 return false;
11836 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11837 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
11838 return NumBits1 == 64 && NumBits2 == 32;
11839}
11840
11841bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
11842 if (!VT1.isInteger() || !VT2.isInteger())
11843 return false;
11844 unsigned NumBits1 = VT1.getSizeInBits();
11845 unsigned NumBits2 = VT2.getSizeInBits();
11846 return NumBits1 == 64 && NumBits2 == 32;
11847}
11848
Hal Finkel5d5d1532015-01-10 08:21:59 +000011849bool PPCTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
11850 // Generally speaking, zexts are not free, but they are free when they can be
11851 // folded with other operations.
11852 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Val)) {
11853 EVT MemVT = LD->getMemoryVT();
11854 if ((MemVT == MVT::i1 || MemVT == MVT::i8 || MemVT == MVT::i16 ||
11855 (Subtarget.isPPC64() && MemVT == MVT::i32)) &&
11856 (LD->getExtensionType() == ISD::NON_EXTLOAD ||
11857 LD->getExtensionType() == ISD::ZEXTLOAD))
11858 return true;
11859 }
11860
11861 // FIXME: Add other cases...
11862 // - 32-bit shifts with a zext to i64
11863 // - zext after ctlz, bswap, etc.
11864 // - zext after and by a constant mask
11865
11866 return TargetLowering::isZExtFree(Val, VT2);
11867}
11868
Olivier Sallenave32509692015-01-13 15:06:36 +000011869bool PPCTargetLowering::isFPExtFree(EVT VT) const {
11870 assert(VT.isFloatingPoint());
11871 return true;
11872}
11873
Hal Finkel34974ed2014-04-12 21:52:38 +000011874bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11875 return isInt<16>(Imm) || isUInt<16>(Imm);
11876}
11877
11878bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const {
11879 return isInt<16>(Imm) || isUInt<16>(Imm);
11880}
11881
Matt Arsenault6f2a5262014-07-27 17:46:40 +000011882bool PPCTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
11883 unsigned,
11884 unsigned,
11885 bool *Fast) const {
Hal Finkel8d7fbc92013-03-15 15:27:13 +000011886 if (DisablePPCUnaligned)
11887 return false;
11888
11889 // PowerPC supports unaligned memory access for simple non-vector types.
11890 // Although accessing unaligned addresses is not as efficient as accessing
11891 // aligned addresses, it is generally more efficient than manual expansion,
11892 // and generally only traps for software emulation when crossing page
11893 // boundaries.
11894
11895 if (!VT.isSimple())
11896 return false;
11897
Hal Finkel6e28e6a2014-03-26 19:39:09 +000011898 if (VT.getSimpleVT().isVector()) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011899 if (Subtarget.hasVSX()) {
Bill Schmidt2d1128a2014-10-17 15:13:38 +000011900 if (VT != MVT::v2f64 && VT != MVT::v2i64 &&
11901 VT != MVT::v4f32 && VT != MVT::v4i32)
Hal Finkel6e28e6a2014-03-26 19:39:09 +000011902 return false;
11903 } else {
11904 return false;
11905 }
11906 }
Hal Finkel8d7fbc92013-03-15 15:27:13 +000011907
11908 if (VT == MVT::ppcf128)
11909 return false;
11910
11911 if (Fast)
11912 *Fast = true;
11913
11914 return true;
11915}
11916
Stephen Lin73de7bf2013-07-09 18:16:56 +000011917bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
11918 VT = VT.getScalarType();
11919
Hal Finkel0a479ae2012-06-22 00:49:52 +000011920 if (!VT.isSimple())
11921 return false;
11922
11923 switch (VT.getSimpleVT().SimpleTy) {
11924 case MVT::f32:
11925 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +000011926 return true;
11927 default:
11928 break;
11929 }
11930
11931 return false;
11932}
11933
Hal Finkel934361a2015-01-14 01:07:51 +000011934const MCPhysReg *
11935PPCTargetLowering::getScratchRegisters(CallingConv::ID) const {
11936 // LR is a callee-save register, but we must treat it as clobbered by any call
11937 // site. Hence we include LR in the scratch registers, which are in turn added
11938 // as implicit-defs for stackmaps and patchpoints. The same reasoning applies
11939 // to CTR, which is used by any indirect call.
11940 static const MCPhysReg ScratchRegs[] = {
Hal Finkelc19805a2015-01-17 03:57:34 +000011941 PPC::X12, PPC::LR8, PPC::CTR8, 0
Hal Finkel934361a2015-01-14 01:07:51 +000011942 };
11943
11944 return ScratchRegs;
11945}
11946
Joseph Tremouletf748c892015-11-07 01:11:31 +000011947unsigned PPCTargetLowering::getExceptionPointerRegister(
11948 const Constant *PersonalityFn) const {
11949 return Subtarget.isPPC64() ? PPC::X3 : PPC::R3;
11950}
11951
11952unsigned PPCTargetLowering::getExceptionSelectorRegister(
11953 const Constant *PersonalityFn) const {
11954 return Subtarget.isPPC64() ? PPC::X4 : PPC::R4;
11955}
11956
Hal Finkelb4240ca2014-03-31 17:48:16 +000011957bool
11958PPCTargetLowering::shouldExpandBuildVectorWithShuffles(
11959 EVT VT , unsigned DefinedValues) const {
11960 if (VT == MVT::v2i64)
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +000011961 return Subtarget.hasDirectMove(); // Don't need stack ops with direct moves
Hal Finkelb4240ca2014-03-31 17:48:16 +000011962
Guozhi Weifa3e0422016-04-29 17:00:54 +000011963 if (Subtarget.hasVSX() || Subtarget.hasQPX())
11964 return true;
Hal Finkelc93a9a22015-02-25 01:06:45 +000011965
Hal Finkelb4240ca2014-03-31 17:48:16 +000011966 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
11967}
11968
Hal Finkel88ed4e32012-04-01 19:23:08 +000011969Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011970 if (DisableILPPref || Subtarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +000011971 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +000011972
Hal Finkel4e9f1a82012-06-10 19:32:29 +000011973 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +000011974}
11975
Bill Schmidt0cf702f2013-07-30 00:50:39 +000011976// Create a fast isel object.
11977FastISel *
11978PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
11979 const TargetLibraryInfo *LibInfo) const {
11980 return PPC::createFastISel(FuncInfo, LibInfo);
11981}
Chuang-Yu Cheng98c18942016-04-08 12:04:32 +000011982
11983void PPCTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const {
11984 if (Subtarget.isDarwinABI()) return;
11985 if (!Subtarget.isPPC64()) return;
11986
11987 // Update IsSplitCSR in PPCFunctionInfo
11988 PPCFunctionInfo *PFI = Entry->getParent()->getInfo<PPCFunctionInfo>();
11989 PFI->setIsSplitCSR(true);
11990}
11991
11992void PPCTargetLowering::insertCopiesSplitCSR(
11993 MachineBasicBlock *Entry,
11994 const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
11995 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo();
11996 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
11997 if (!IStart)
11998 return;
11999
12000 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
12001 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
12002 MachineBasicBlock::iterator MBBI = Entry->begin();
12003 for (const MCPhysReg *I = IStart; *I; ++I) {
12004 const TargetRegisterClass *RC = nullptr;
12005 if (PPC::G8RCRegClass.contains(*I))
12006 RC = &PPC::G8RCRegClass;
12007 else if (PPC::F8RCRegClass.contains(*I))
12008 RC = &PPC::F8RCRegClass;
12009 else if (PPC::CRRCRegClass.contains(*I))
12010 RC = &PPC::CRRCRegClass;
12011 else if (PPC::VRRCRegClass.contains(*I))
12012 RC = &PPC::VRRCRegClass;
12013 else
12014 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
12015
12016 unsigned NewVR = MRI->createVirtualRegister(RC);
12017 // Create copy from CSR to a virtual register.
12018 // FIXME: this currently does not emit CFI pseudo-instructions, it works
12019 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be
12020 // nounwind. If we want to generalize this later, we may need to emit
12021 // CFI pseudo-instructions.
12022 assert(Entry->getParent()->getFunction()->hasFnAttribute(
12023 Attribute::NoUnwind) &&
12024 "Function should be nounwind in insertCopiesSplitCSR!");
12025 Entry->addLiveIn(*I);
12026 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
12027 .addReg(*I);
12028
12029 // Insert the copy-back instructions right before the terminator
12030 for (auto *Exit : Exits)
12031 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
12032 TII->get(TargetOpcode::COPY), *I)
12033 .addReg(NewVR);
12034 }
12035}
Tim Shena1d8bc52016-04-19 20:14:52 +000012036
12037// Override to enable LOAD_STACK_GUARD lowering on Linux.
12038bool PPCTargetLowering::useLoadStackGuardNode() const {
12039 if (!Subtarget.isTargetLinux())
12040 return TargetLowering::useLoadStackGuardNode();
12041 return true;
12042}
12043
12044// Override to disable global variable loading on Linux.
12045void PPCTargetLowering::insertSSPDeclarations(Module &M) const {
12046 if (!Subtarget.isTargetLinux())
12047 return TargetLowering::insertSSPDeclarations(M);
12048}