blob: 4c0b6a6e871abd59d4a42899cb9916d8513c4d44 [file] [log] [blame]
Nate Begeman0b71e002005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattnerf22556d2005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf22556d2005-08-16 17:14:42 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattnerf22556d2005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6f3b9542005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Hal Finkel934361a2015-01-14 01:07:51 +000016#include "PPCCallingConv.h"
Jim Laskey48850c12006-11-16 22:43:37 +000017#include "PPCMachineFunctionInfo.h"
Bill Wendlingdd3fe942010-03-12 02:00:43 +000018#include "PPCPerfectShuffle.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000019#include "PPCTargetMachine.h"
Bill Schmidt22d40dc2013-05-13 19:34:37 +000020#include "PPCTargetObjectFile.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000021#include "llvm/ADT/STLExtras.h"
Hal Finkel0d8db462014-05-11 19:29:11 +000022#include "llvm/ADT/StringSwitch.h"
Eric Christopher89958332014-05-31 00:07:32 +000023#include "llvm/ADT/Triple.h"
Chris Lattner4f2e4e02007-03-06 00:59:59 +000024#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner9b577f12005-08-26 21:23:58 +000027#include "llvm/CodeGen/MachineInstrBuilder.h"
Hal Finkel57725662015-01-03 17:58:24 +000028#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000030#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000031#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/CallingConv.h"
33#include "llvm/IR/Constants.h"
34#include "llvm/IR/DerivedTypes.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/Intrinsics.h"
Chris Lattnerce645542006-11-10 02:08:47 +000037#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000038#include "llvm/Support/ErrorHandling.h"
Craig Topperb25fda92012-03-17 18:46:09 +000039#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000040#include "llvm/Support/raw_ostream.h"
Craig Topperb25fda92012-03-17 18:46:09 +000041#include "llvm/Target/TargetOptions.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000042using namespace llvm;
43
Joerg Sonnenbergereb8655a2014-08-08 16:46:10 +000044// FIXME: Remove this once soft-float is supported.
45static cl::opt<bool> DisablePPCFloatInVariadic("disable-ppc-float-in-variadic",
46cl::desc("disable saving float registers for va_start on PPC"), cl::Hidden);
47
Hal Finkel595817e2012-06-04 02:21:00 +000048static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
49cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000050
Hal Finkel4e9f1a82012-06-10 19:32:29 +000051static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
52cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
53
Hal Finkel8d7fbc92013-03-15 15:27:13 +000054static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
55cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
56
Hal Finkel940ab932014-02-28 00:27:01 +000057// FIXME: Remove this once the bug has been fixed!
58extern cl::opt<bool> ANDIGlueBug;
59
Eric Christophercccae792015-01-30 22:02:31 +000060PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM,
61 const PPCSubtarget &STI)
62 : TargetLowering(TM), Subtarget(STI) {
Chris Lattnera028e7a2005-09-27 22:18:25 +000063 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000064 setUseUnderscoreSetJmp(true);
65 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000066
Chris Lattnerd10babf2010-10-10 18:34:00 +000067 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
68 // arguments are at least 4/8 bytes aligned.
Eric Christopherb1aaebe2014-06-12 22:38:18 +000069 bool isPPC64 = Subtarget.isPPC64();
Evan Cheng39e90022012-07-02 22:39:56 +000070 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000071
Chris Lattnerf22556d2005-08-16 17:14:42 +000072 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000073 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
74 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
75 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +000076
Evan Cheng5d9fd972006-10-04 00:56:09 +000077 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000078 for (MVT VT : MVT::integer_valuetypes()) {
79 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
80 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand);
81 }
Duncan Sands95d46ef2008-01-23 20:39:46 +000082
Owen Anderson9f944592009-08-11 20:47:22 +000083 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000084
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000085 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000086 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
87 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
88 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
89 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
90 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
Hal Finkel65d1cbf2015-02-05 18:42:53 +000091 setIndexedLoadAction(ISD::PRE_INC, MVT::f32, Legal);
92 setIndexedLoadAction(ISD::PRE_INC, MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +000093 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
94 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
95 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
96 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
97 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Hal Finkel65d1cbf2015-02-05 18:42:53 +000098 setIndexedStoreAction(ISD::PRE_INC, MVT::f32, Legal);
99 setIndexedStoreAction(ISD::PRE_INC, MVT::f64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +0000100
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000101 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000102 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
103
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000104 if (isPPC64 || Subtarget.hasFPCVT()) {
Hal Finkel6a56b212014-03-05 22:14:00 +0000105 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
106 AddPromotedToType (ISD::SINT_TO_FP, MVT::i1,
107 isPPC64 ? MVT::i64 : MVT::i32);
108 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
109 AddPromotedToType (ISD::UINT_TO_FP, MVT::i1,
110 isPPC64 ? MVT::i64 : MVT::i32);
111 } else {
112 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom);
113 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom);
114 }
Hal Finkel940ab932014-02-28 00:27:01 +0000115
116 // PowerPC does not support direct load / store of condition registers
117 setOperationAction(ISD::LOAD, MVT::i1, Custom);
118 setOperationAction(ISD::STORE, MVT::i1, Custom);
119
120 // FIXME: Remove this once the ANDI glue bug is fixed:
121 if (ANDIGlueBug)
122 setOperationAction(ISD::TRUNCATE, MVT::i1, Custom);
123
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000124 for (MVT VT : MVT::integer_valuetypes()) {
125 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
126 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
127 setTruncStoreAction(VT, MVT::i1, Expand);
128 }
Hal Finkel940ab932014-02-28 00:27:01 +0000129
130 addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass);
131 }
132
Dale Johannesen666323e2007-10-10 01:01:31 +0000133 // This is used in the ppcf128->int sequence. Note it has different semantics
134 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +0000135 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000136
Roman Divacky1faf5b02012-08-16 18:19:29 +0000137 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000138 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
139 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
140 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
141 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
142 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000143 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000144
Chris Lattnerf22556d2005-08-16 17:14:42 +0000145 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000146 setOperationAction(ISD::SREM, MVT::i32, Expand);
147 setOperationAction(ISD::UREM, MVT::i32, Expand);
148 setOperationAction(ISD::SREM, MVT::i64, Expand);
149 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000150
151 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000152 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
153 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
154 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
155 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
156 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
157 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
158 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
159 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000160
Dan Gohman482732a2007-10-11 23:21:31 +0000161 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000162 setOperationAction(ISD::FSIN , MVT::f64, Expand);
163 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000164 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000165 setOperationAction(ISD::FREM , MVT::f64, Expand);
166 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000167 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000168 setOperationAction(ISD::FSIN , MVT::f32, Expand);
169 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000170 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000171 setOperationAction(ISD::FREM , MVT::f32, Expand);
172 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000173 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000174
Owen Anderson9f944592009-08-11 20:47:22 +0000175 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000176
Chris Lattnerf22556d2005-08-16 17:14:42 +0000177 // If we're enabling GP optimizations, use hardware square root
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000178 if (!Subtarget.hasFSQRT() &&
Eric Christophercccae792015-01-30 22:02:31 +0000179 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTE() &&
180 Subtarget.hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000181 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000182
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000183 if (!Subtarget.hasFSQRT() &&
Eric Christophercccae792015-01-30 22:02:31 +0000184 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTES() &&
185 Subtarget.hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000186 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000187
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000188 if (Subtarget.hasFCPSGN()) {
Hal Finkeldbc78e12013-08-19 05:01:02 +0000189 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
190 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
191 } else {
192 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
193 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
194 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000195
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000196 if (Subtarget.hasFPRND()) {
Hal Finkelc20a08d2013-03-29 08:57:48 +0000197 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
198 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
199 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000200 setOperationAction(ISD::FROUND, MVT::f64, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000201
202 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
203 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
204 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000205 setOperationAction(ISD::FROUND, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000206 }
207
Nate Begeman2fba8a32006-01-14 03:14:10 +0000208 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000209 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000210 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000211 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
212 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000213 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000214 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000215 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
216 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000217
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000218 if (Subtarget.hasPOPCNTD()) {
Hal Finkel290376d2013-04-01 15:58:15 +0000219 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000220 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
221 } else {
222 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
223 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
224 }
225
Nate Begeman1b8121b2006-01-11 21:21:00 +0000226 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000227 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
228 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000229
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000230 if (!Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000231 // PowerPC does not have Select
232 setOperationAction(ISD::SELECT, MVT::i32, Expand);
233 setOperationAction(ISD::SELECT, MVT::i64, Expand);
234 setOperationAction(ISD::SELECT, MVT::f32, Expand);
235 setOperationAction(ISD::SELECT, MVT::f64, Expand);
236 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000237
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000238 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000239 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
240 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000241
Nate Begeman7e7f4392006-02-01 07:19:44 +0000242 // PowerPC wants to optimize integer setcc a bit
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000243 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000244 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000245
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000246 // PowerPC does not have BRCOND which requires SetCC
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000247 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000248 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000249
Owen Anderson9f944592009-08-11 20:47:22 +0000250 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000251
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000252 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000253 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000254
Jim Laskey6267b2c2005-08-17 00:40:22 +0000255 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000256 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
257 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000258
Wesley Peck527da1b2010-11-23 03:31:01 +0000259 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
260 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
261 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
262 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattnerc46fc242005-12-23 05:13:35 +0000263
Chris Lattner84b49d52006-04-28 21:56:10 +0000264 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000265 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000266
Hal Finkel1996f3d2013-03-27 19:10:42 +0000267 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000268 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
269 // support continuation, user-level threading, and etc.. As a result, no
270 // other SjLj exception interfaces are implemented and please don't build
271 // your own exception handling based on them.
272 // LLVM/Clang supports zero-cost DWARF exception handling.
273 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
274 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000275
276 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000277 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000278 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
279 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000280 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000281 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
282 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
283 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
284 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000285 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000286 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
287 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000288
Nate Begemanf69d13b2008-08-11 17:36:31 +0000289 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000290 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000291
292 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000293 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
294 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000295
Nate Begemane74795c2006-01-25 18:21:52 +0000296 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000297 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000298
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000299 if (Subtarget.isSVR4ABI()) {
Evan Cheng39e90022012-07-02 22:39:56 +0000300 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000301 // VAARG always uses double-word chunks, so promote anything smaller.
302 setOperationAction(ISD::VAARG, MVT::i1, Promote);
303 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
304 setOperationAction(ISD::VAARG, MVT::i8, Promote);
305 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
306 setOperationAction(ISD::VAARG, MVT::i16, Promote);
307 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
308 setOperationAction(ISD::VAARG, MVT::i32, Promote);
309 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
310 setOperationAction(ISD::VAARG, MVT::Other, Expand);
311 } else {
312 // VAARG is custom lowered with the 32-bit SVR4 ABI.
313 setOperationAction(ISD::VAARG, MVT::Other, Custom);
314 setOperationAction(ISD::VAARG, MVT::i64, Custom);
315 }
Roman Divacky4394e682011-06-28 15:30:42 +0000316 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000317 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000318
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000319 if (Subtarget.isSVR4ABI() && !isPPC64)
Roman Divackyc3825df2013-07-25 21:36:47 +0000320 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
321 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
322 else
323 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
324
Chris Lattner5bd514d2006-01-15 09:02:48 +0000325 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000326 setOperationAction(ISD::VAEND , MVT::Other, Expand);
327 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
328 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
329 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
330 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000331
Chris Lattner6961fc72006-03-26 10:06:40 +0000332 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000333 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000334
Hal Finkel25c19922013-05-15 21:37:41 +0000335 // To handle counter-based loop conditions.
336 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
337
Dale Johannesen160be0f2008-11-07 22:54:33 +0000338 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000339 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
340 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
341 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
342 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
343 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
344 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
345 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
346 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
347 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
348 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
349 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
350 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000351
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000352 if (Subtarget.has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000353 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000354 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
355 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
356 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
357 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000358 // This is just the low 32 bits of a (signed) fp->i64 conversion.
359 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000360 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000361
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000362 if (Subtarget.hasLFIWAX() || Subtarget.isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000363 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000364 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000365 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000366 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000367 }
368
Hal Finkelf6d45f22013-04-01 17:52:07 +0000369 // With the instructions enabled under FPCVT, we can do everything.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000370 if (Subtarget.hasFPCVT()) {
371 if (Subtarget.has64BitSupport()) {
Hal Finkelf6d45f22013-04-01 17:52:07 +0000372 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
373 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
374 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
375 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
376 }
377
378 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
379 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
380 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
381 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
382 }
383
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000384 if (Subtarget.use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000385 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000386 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000387 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000388 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000389 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000390 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
391 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
392 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000393 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000394 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000395 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
396 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
397 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000398 }
Evan Cheng19264272006-03-01 01:11:20 +0000399
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000400 if (Subtarget.hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000401 // First set operation action for all vector types to expand. Then we
402 // will selectively turn on ones that can be effectively codegen'd.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000403 for (MVT VT : MVT::vector_valuetypes()) {
Chris Lattner06a21ba2006-04-16 01:37:57 +0000404 // add/sub are legal for all supported vector VT's.
Duncan Sands13237ac2008-06-06 12:08:01 +0000405 setOperationAction(ISD::ADD , VT, Legal);
406 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000407
Bill Schmidt433b1c32015-02-05 15:24:47 +0000408 // Vector instructions introduced in P8
409 if (Subtarget.hasP8Altivec()) {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000410 setOperationAction(ISD::CTPOP, VT, Legal);
Bill Schmidt433b1c32015-02-05 15:24:47 +0000411 setOperationAction(ISD::CTLZ, VT, Legal);
412 }
413 else {
Bill Schmidtfe88b182015-02-03 21:58:23 +0000414 setOperationAction(ISD::CTPOP, VT, Expand);
Bill Schmidt433b1c32015-02-05 15:24:47 +0000415 setOperationAction(ISD::CTLZ, VT, Expand);
416 }
Bill Schmidtfe88b182015-02-03 21:58:23 +0000417
Chris Lattner95c7adc2006-04-04 17:25:31 +0000418 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000419 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000420 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000421
422 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000423 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000424 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000425 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000426 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000427 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000428 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000429 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000430 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000431 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000432 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000433 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000434 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000435
Chris Lattner06a21ba2006-04-16 01:37:57 +0000436 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000437 setOperationAction(ISD::MUL , VT, Expand);
438 setOperationAction(ISD::SDIV, VT, Expand);
439 setOperationAction(ISD::SREM, VT, Expand);
440 setOperationAction(ISD::UDIV, VT, Expand);
441 setOperationAction(ISD::UREM, VT, Expand);
442 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000443 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000444 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000445 setOperationAction(ISD::FSQRT, VT, Expand);
446 setOperationAction(ISD::FLOG, VT, Expand);
447 setOperationAction(ISD::FLOG10, VT, Expand);
448 setOperationAction(ISD::FLOG2, VT, Expand);
449 setOperationAction(ISD::FEXP, VT, Expand);
450 setOperationAction(ISD::FEXP2, VT, Expand);
451 setOperationAction(ISD::FSIN, VT, Expand);
452 setOperationAction(ISD::FCOS, VT, Expand);
453 setOperationAction(ISD::FABS, VT, Expand);
454 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000455 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000456 setOperationAction(ISD::FCEIL, VT, Expand);
457 setOperationAction(ISD::FTRUNC, VT, Expand);
458 setOperationAction(ISD::FRINT, VT, Expand);
459 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000460 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
461 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
462 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
Ulrich Weigand51eccec2014-08-04 13:27:12 +0000463 setOperationAction(ISD::MULHU, VT, Expand);
464 setOperationAction(ISD::MULHS, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000465 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
466 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
467 setOperationAction(ISD::UDIVREM, VT, Expand);
468 setOperationAction(ISD::SDIVREM, VT, Expand);
469 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
470 setOperationAction(ISD::FPOW, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000471 setOperationAction(ISD::BSWAP, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000472 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000473 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000474 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000475 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000476 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
477
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000478 for (MVT InnerVT : MVT::vector_valuetypes()) {
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000479 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000480 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
481 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
482 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
483 }
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000484 }
485
Chris Lattner95c7adc2006-04-04 17:25:31 +0000486 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
487 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000488 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000489
Owen Anderson9f944592009-08-11 20:47:22 +0000490 setOperationAction(ISD::AND , MVT::v4i32, Legal);
491 setOperationAction(ISD::OR , MVT::v4i32, Legal);
492 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
493 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Hal Finkel940ab932014-02-28 00:27:01 +0000494 setOperationAction(ISD::SELECT, MVT::v4i32,
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000495 Subtarget.useCRBits() ? Legal : Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000496 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000497 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
498 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
499 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
500 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000501 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
502 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
503 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
504 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000505
Craig Topperabadc662012-04-20 06:31:50 +0000506 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
507 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
508 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
509 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000510
Owen Anderson9f944592009-08-11 20:47:22 +0000511 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000512 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000513
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000514 if (TM.Options.UnsafeFPMath || Subtarget.hasVSX()) {
Hal Finkel2e103312013-04-03 04:01:11 +0000515 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
516 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
517 }
518
Kit Barton20d39812015-03-10 19:49:38 +0000519
520 if (Subtarget.hasP8Altivec())
521 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
522 else
523 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
524
Owen Anderson9f944592009-08-11 20:47:22 +0000525 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
526 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000527
Owen Anderson9f944592009-08-11 20:47:22 +0000528 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
529 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000530
Owen Anderson9f944592009-08-11 20:47:22 +0000531 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
532 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
533 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
534 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000535
536 // Altivec does not contain unordered floating-point compare instructions
537 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
538 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000539 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
540 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000541
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000542 if (Subtarget.hasVSX()) {
Hal Finkel27774d92014-03-13 07:58:58 +0000543 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
Hal Finkel82569b62014-03-27 22:22:48 +0000544 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
Hal Finkel27774d92014-03-13 07:58:58 +0000545
546 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
547 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
548 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
549 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
550 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
551
552 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
553
554 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
555 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
556
557 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
558 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
559
Hal Finkel732f0f72014-03-26 12:49:28 +0000560 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
561 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal);
562 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
563 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
564 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
565
Hal Finkel27774d92014-03-13 07:58:58 +0000566 // Share the Altivec comparison restrictions.
567 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand);
568 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000569 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand);
570 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand);
571
Hal Finkel9281c9a2014-03-26 18:26:30 +0000572 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
573 setOperationAction(ISD::STORE, MVT::v2f64, Legal);
574
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000575 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal);
576
Hal Finkel19be5062014-03-29 05:29:01 +0000577 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000578
579 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass);
580 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass);
Hal Finkela6c8b512014-03-26 16:12:58 +0000581
Kit Barton0cfa7b72015-03-03 19:55:45 +0000582 if (Subtarget.hasP8Altivec()) {
Kit Bartone48b1e12015-03-05 16:24:38 +0000583 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
584 setOperationAction(ISD::SRA, MVT::v2i64, Legal);
585 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
586
Kit Barton0cfa7b72015-03-03 19:55:45 +0000587 setOperationAction(ISD::SETCC, MVT::v2i64, Legal);
588 }
589 else {
Kit Bartone48b1e12015-03-05 16:24:38 +0000590 setOperationAction(ISD::SHL, MVT::v2i64, Expand);
591 setOperationAction(ISD::SRA, MVT::v2i64, Expand);
592 setOperationAction(ISD::SRL, MVT::v2i64, Expand);
593
Kit Barton0cfa7b72015-03-03 19:55:45 +0000594 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
595
596 // VSX v2i64 only supports non-arithmetic operations.
597 setOperationAction(ISD::ADD, MVT::v2i64, Expand);
598 setOperationAction(ISD::SUB, MVT::v2i64, Expand);
599 }
Hal Finkel777c9dd2014-03-29 16:04:40 +0000600
Hal Finkel9281c9a2014-03-26 18:26:30 +0000601 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
602 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64);
603 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
604 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64);
605
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000606 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal);
607
Hal Finkel7279f4b2014-03-26 19:13:54 +0000608 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
609 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
610 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
611 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
612
Hal Finkel5c0d1452014-03-30 13:22:59 +0000613 // Vector operation legalization checks the result type of
614 // SIGN_EXTEND_INREG, overall legalization checks the inner type.
615 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal);
616 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal);
617 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
618 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
619
Hal Finkela6c8b512014-03-26 16:12:58 +0000620 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000621 }
Bill Schmidtfe88b182015-02-03 21:58:23 +0000622
623 if (Subtarget.hasP8Altivec())
624 addRegisterClass(MVT::v2i64, &PPC::VRRCRegClass);
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000625 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000626
Hal Finkelc93a9a22015-02-25 01:06:45 +0000627 if (Subtarget.hasQPX()) {
628 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
629 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
630 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
631 setOperationAction(ISD::FREM, MVT::v4f64, Expand);
632
633 setOperationAction(ISD::FCOPYSIGN, MVT::v4f64, Legal);
634 setOperationAction(ISD::FGETSIGN, MVT::v4f64, Expand);
635
636 setOperationAction(ISD::LOAD , MVT::v4f64, Custom);
637 setOperationAction(ISD::STORE , MVT::v4f64, Custom);
638
639 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Custom);
640 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Custom);
641
642 if (!Subtarget.useCRBits())
643 setOperationAction(ISD::SELECT, MVT::v4f64, Expand);
644 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
645
646 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f64, Legal);
647 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f64, Expand);
648 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f64, Expand);
649 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f64, Expand);
650 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f64, Custom);
651 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f64, Legal);
652 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
653
654 setOperationAction(ISD::FP_TO_SINT , MVT::v4f64, Legal);
655 setOperationAction(ISD::FP_TO_UINT , MVT::v4f64, Expand);
656
657 setOperationAction(ISD::FP_ROUND , MVT::v4f32, Legal);
658 setOperationAction(ISD::FP_ROUND_INREG , MVT::v4f32, Expand);
659 setOperationAction(ISD::FP_EXTEND, MVT::v4f64, Legal);
660
661 setOperationAction(ISD::FNEG , MVT::v4f64, Legal);
662 setOperationAction(ISD::FABS , MVT::v4f64, Legal);
663 setOperationAction(ISD::FSIN , MVT::v4f64, Expand);
664 setOperationAction(ISD::FCOS , MVT::v4f64, Expand);
665 setOperationAction(ISD::FPOWI , MVT::v4f64, Expand);
666 setOperationAction(ISD::FPOW , MVT::v4f64, Expand);
667 setOperationAction(ISD::FLOG , MVT::v4f64, Expand);
668 setOperationAction(ISD::FLOG2 , MVT::v4f64, Expand);
669 setOperationAction(ISD::FLOG10 , MVT::v4f64, Expand);
670 setOperationAction(ISD::FEXP , MVT::v4f64, Expand);
671 setOperationAction(ISD::FEXP2 , MVT::v4f64, Expand);
672
673 setOperationAction(ISD::FMINNUM, MVT::v4f64, Legal);
674 setOperationAction(ISD::FMAXNUM, MVT::v4f64, Legal);
675
676 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f64, Legal);
677 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f64, Legal);
678
679 addRegisterClass(MVT::v4f64, &PPC::QFRCRegClass);
680
681 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
682 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
683 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
684 setOperationAction(ISD::FREM, MVT::v4f32, Expand);
685
686 setOperationAction(ISD::FCOPYSIGN, MVT::v4f32, Legal);
687 setOperationAction(ISD::FGETSIGN, MVT::v4f32, Expand);
688
689 setOperationAction(ISD::LOAD , MVT::v4f32, Custom);
690 setOperationAction(ISD::STORE , MVT::v4f32, Custom);
691
692 if (!Subtarget.useCRBits())
693 setOperationAction(ISD::SELECT, MVT::v4f32, Expand);
694 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
695
696 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f32, Legal);
697 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f32, Expand);
698 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f32, Expand);
699 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f32, Expand);
700 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f32, Custom);
701 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
702 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
703
704 setOperationAction(ISD::FP_TO_SINT , MVT::v4f32, Legal);
705 setOperationAction(ISD::FP_TO_UINT , MVT::v4f32, Expand);
706
707 setOperationAction(ISD::FNEG , MVT::v4f32, Legal);
708 setOperationAction(ISD::FABS , MVT::v4f32, Legal);
709 setOperationAction(ISD::FSIN , MVT::v4f32, Expand);
710 setOperationAction(ISD::FCOS , MVT::v4f32, Expand);
711 setOperationAction(ISD::FPOWI , MVT::v4f32, Expand);
712 setOperationAction(ISD::FPOW , MVT::v4f32, Expand);
713 setOperationAction(ISD::FLOG , MVT::v4f32, Expand);
714 setOperationAction(ISD::FLOG2 , MVT::v4f32, Expand);
715 setOperationAction(ISD::FLOG10 , MVT::v4f32, Expand);
716 setOperationAction(ISD::FEXP , MVT::v4f32, Expand);
717 setOperationAction(ISD::FEXP2 , MVT::v4f32, Expand);
718
719 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
720 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
721
722 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f32, Legal);
723 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f32, Legal);
724
725 addRegisterClass(MVT::v4f32, &PPC::QSRCRegClass);
726
727 setOperationAction(ISD::AND , MVT::v4i1, Legal);
728 setOperationAction(ISD::OR , MVT::v4i1, Legal);
729 setOperationAction(ISD::XOR , MVT::v4i1, Legal);
730
731 if (!Subtarget.useCRBits())
732 setOperationAction(ISD::SELECT, MVT::v4i1, Expand);
733 setOperationAction(ISD::VSELECT, MVT::v4i1, Legal);
734
735 setOperationAction(ISD::LOAD , MVT::v4i1, Custom);
736 setOperationAction(ISD::STORE , MVT::v4i1, Custom);
737
738 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4i1, Custom);
739 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4i1, Expand);
740 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4i1, Expand);
741 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4i1, Expand);
742 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4i1, Custom);
743 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i1, Expand);
744 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i1, Custom);
745
746 setOperationAction(ISD::SINT_TO_FP, MVT::v4i1, Custom);
747 setOperationAction(ISD::UINT_TO_FP, MVT::v4i1, Custom);
748
749 addRegisterClass(MVT::v4i1, &PPC::QBRCRegClass);
750
751 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
752 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
753 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
754 setOperationAction(ISD::FROUND, MVT::v4f64, Legal);
755
756 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
757 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
758 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
759 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
760
761 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Expand);
762 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
763
764 // These need to set FE_INEXACT, and so cannot be vectorized here.
765 setOperationAction(ISD::FRINT, MVT::v4f64, Expand);
766 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
767
768 if (TM.Options.UnsafeFPMath) {
769 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
770 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
771
772 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
773 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
774 } else {
775 setOperationAction(ISD::FDIV, MVT::v4f64, Expand);
776 setOperationAction(ISD::FSQRT, MVT::v4f64, Expand);
777
778 setOperationAction(ISD::FDIV, MVT::v4f32, Expand);
779 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
780 }
781 }
782
Hal Finkel01fa7702014-12-03 00:19:17 +0000783 if (Subtarget.has64BitSupport())
Hal Finkel322e41a2012-04-01 20:08:17 +0000784 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel01fa7702014-12-03 00:19:17 +0000785
786 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, isPPC64 ? Legal : Custom);
Hal Finkel322e41a2012-04-01 20:08:17 +0000787
Robin Morissete1ca44b2014-10-02 22:27:07 +0000788 if (!isPPC64) {
789 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
790 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
791 }
Eli Friedman7dfa7912011-08-29 18:23:02 +0000792
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000793 setBooleanContents(ZeroOrOneBooleanContent);
Hal Finkelc93a9a22015-02-25 01:06:45 +0000794
795 if (Subtarget.hasAltivec()) {
796 // Altivec instructions set fields to all zeros or all ones.
797 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
798 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000799
Joerg Sonnenbergerb5459e62014-07-24 22:20:10 +0000800 if (!isPPC64) {
801 // These libcalls are not available in 32-bit.
802 setLibcallName(RTLIB::SHL_I128, nullptr);
803 setLibcallName(RTLIB::SRL_I128, nullptr);
804 setLibcallName(RTLIB::SRA_I128, nullptr);
805 }
806
Evan Cheng39e90022012-07-02 22:39:56 +0000807 if (isPPC64) {
Chris Lattner454436d2006-10-18 01:20:43 +0000808 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000809 setExceptionPointerRegister(PPC::X3);
810 setExceptionSelectorRegister(PPC::X4);
811 } else {
Chris Lattner454436d2006-10-18 01:20:43 +0000812 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000813 setExceptionPointerRegister(PPC::R3);
814 setExceptionSelectorRegister(PPC::R4);
815 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000816
Chris Lattnerf4184352006-03-01 04:57:39 +0000817 // We have target-specific dag combine patterns for the following nodes:
818 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkel5efb9182015-01-06 06:01:57 +0000819 if (Subtarget.hasFPCVT())
820 setTargetDAGCombine(ISD::UINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000821 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000822 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000823 setTargetDAGCombine(ISD::BR_CC);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000824 if (Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000825 setTargetDAGCombine(ISD::BRCOND);
Chris Lattnera7976d32006-07-10 20:56:58 +0000826 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000827 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Bill Schmidtfae5d712014-12-09 16:35:51 +0000828 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
829 setTargetDAGCombine(ISD::INTRINSIC_VOID);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000830
Hal Finkel46043ed2014-03-01 21:36:57 +0000831 setTargetDAGCombine(ISD::SIGN_EXTEND);
832 setTargetDAGCombine(ISD::ZERO_EXTEND);
833 setTargetDAGCombine(ISD::ANY_EXTEND);
834
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000835 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000836 setTargetDAGCombine(ISD::TRUNCATE);
837 setTargetDAGCombine(ISD::SETCC);
838 setTargetDAGCombine(ISD::SELECT_CC);
839 }
840
Hal Finkel2e103312013-04-03 04:01:11 +0000841 // Use reciprocal estimates.
842 if (TM.Options.UnsafeFPMath) {
843 setTargetDAGCombine(ISD::FDIV);
844 setTargetDAGCombine(ISD::FSQRT);
845 }
846
Dale Johannesen10432e52007-10-19 00:59:18 +0000847 // Darwin long double math library functions have $LDBL128 appended.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000848 if (Subtarget.isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000849 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000850 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
851 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000852 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
853 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000854 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
855 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
856 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
857 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
858 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000859 }
860
Hal Finkel940ab932014-02-28 00:27:01 +0000861 // With 32 condition bits, we don't need to sink (and duplicate) compares
862 // aggressively in CodeGenPrep.
Hal Finkel7a0516e2015-02-12 01:02:52 +0000863 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000864 setHasMultipleConditionRegisters();
Hal Finkel7a0516e2015-02-12 01:02:52 +0000865 setJumpIsExpensive();
866 }
Hal Finkel940ab932014-02-28 00:27:01 +0000867
Hal Finkel65298572011-10-17 18:53:03 +0000868 setMinFunctionAlignment(2);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000869 if (Subtarget.isDarwin())
Hal Finkel65298572011-10-17 18:53:03 +0000870 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000871
Hal Finkeld73bfba2015-01-03 14:58:25 +0000872 switch (Subtarget.getDarwinDirective()) {
873 default: break;
874 case PPC::DIR_970:
875 case PPC::DIR_A2:
876 case PPC::DIR_E500mc:
877 case PPC::DIR_E5500:
878 case PPC::DIR_PWR4:
879 case PPC::DIR_PWR5:
880 case PPC::DIR_PWR5X:
881 case PPC::DIR_PWR6:
882 case PPC::DIR_PWR6X:
883 case PPC::DIR_PWR7:
884 case PPC::DIR_PWR8:
885 setPrefFunctionAlignment(4);
886 setPrefLoopAlignment(4);
887 break;
888 }
889
Eli Friedman30a49e92011-08-03 21:06:02 +0000890 setInsertFencesForAtomic(true);
891
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000892 if (Subtarget.enableMachineScheduler())
Hal Finkel21442b22013-09-11 23:05:25 +0000893 setSchedulingPreference(Sched::Source);
894 else
895 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000896
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000897 computeRegisterProperties(STI.getRegisterInfo());
Hal Finkel742b5352012-08-28 16:12:39 +0000898
Hal Finkeld73bfba2015-01-03 14:58:25 +0000899 // The Freescale cores do better with aggressive inlining of memcpy and
900 // friends. GCC uses same threshold of 128 bytes (= 32 word stores).
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000901 if (Subtarget.getDarwinDirective() == PPC::DIR_E500mc ||
902 Subtarget.getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000903 MaxStoresPerMemset = 32;
904 MaxStoresPerMemsetOptSize = 16;
905 MaxStoresPerMemcpy = 32;
906 MaxStoresPerMemcpyOptSize = 8;
907 MaxStoresPerMemmove = 32;
908 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel5c3cacf2015-02-27 19:58:28 +0000909 } else if (Subtarget.getDarwinDirective() == PPC::DIR_A2) {
910 // The A2 also benefits from (very) aggressive inlining of memcpy and
911 // friends. The overhead of a the function call, even when warm, can be
912 // over one hundred cycles.
913 MaxStoresPerMemset = 128;
914 MaxStoresPerMemcpy = 128;
915 MaxStoresPerMemmove = 128;
Hal Finkel742b5352012-08-28 16:12:39 +0000916 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000917}
918
Hal Finkel262a2242013-09-12 23:20:06 +0000919/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
920/// the desired ByVal argument alignment.
921static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
922 unsigned MaxMaxAlign) {
923 if (MaxAlign == MaxMaxAlign)
924 return;
925 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
926 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
927 MaxAlign = 32;
928 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
929 MaxAlign = 16;
930 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
931 unsigned EltAlign = 0;
932 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
933 if (EltAlign > MaxAlign)
934 MaxAlign = EltAlign;
935 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
936 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
937 unsigned EltAlign = 0;
938 getMaxByValAlign(STy->getElementType(i), EltAlign, MaxMaxAlign);
939 if (EltAlign > MaxAlign)
940 MaxAlign = EltAlign;
941 if (MaxAlign == MaxMaxAlign)
942 break;
943 }
944 }
945}
946
Dale Johannesencbde4c22008-02-28 22:31:51 +0000947/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
948/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000949unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000950 // Darwin passes everything on 4 byte boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000951 if (Subtarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000952 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000953
954 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000955 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000956 unsigned Align = Subtarget.isPPC64() ? 8 : 4;
957 if (Subtarget.hasAltivec() || Subtarget.hasQPX())
958 getMaxByValAlign(Ty, Align, Subtarget.hasQPX() ? 32 : 16);
Hal Finkel262a2242013-09-12 23:20:06 +0000959 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000960}
961
Chris Lattner347ed8a2006-01-09 23:52:17 +0000962const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
963 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000964 default: return nullptr;
Evan Cheng32e376f2008-07-12 02:23:19 +0000965 case PPCISD::FSEL: return "PPCISD::FSEL";
966 case PPCISD::FCFID: return "PPCISD::FCFID";
Hal Finkel3fe09ea2015-01-06 07:02:15 +0000967 case PPCISD::FCFIDU: return "PPCISD::FCFIDU";
968 case PPCISD::FCFIDS: return "PPCISD::FCFIDS";
969 case PPCISD::FCFIDUS: return "PPCISD::FCFIDUS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000970 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
971 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel3fe09ea2015-01-06 07:02:15 +0000972 case PPCISD::FCTIDUZ: return "PPCISD::FCTIDUZ";
973 case PPCISD::FCTIWUZ: return "PPCISD::FCTIWUZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000974 case PPCISD::FRE: return "PPCISD::FRE";
975 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000976 case PPCISD::STFIWX: return "PPCISD::STFIWX";
977 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
978 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
979 case PPCISD::VPERM: return "PPCISD::VPERM";
Hal Finkel4edc66b2015-01-03 01:16:37 +0000980 case PPCISD::CMPB: return "PPCISD::CMPB";
Evan Cheng32e376f2008-07-12 02:23:19 +0000981 case PPCISD::Hi: return "PPCISD::Hi";
982 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000983 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Evan Cheng32e376f2008-07-12 02:23:19 +0000984 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
985 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
986 case PPCISD::SRL: return "PPCISD::SRL";
987 case PPCISD::SRA: return "PPCISD::SRA";
988 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000989 case PPCISD::CALL: return "PPCISD::CALL";
990 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000991 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000992 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Hal Finkelfc096c92014-12-23 22:29:40 +0000993 case PPCISD::BCTRL_LOAD_TOC: return "PPCISD::BCTRL_LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000994 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkelbbdee932014-12-02 22:01:00 +0000995 case PPCISD::READ_TIME_BASE: return "PPCISD::READ_TIME_BASE";
Hal Finkel756810f2013-03-21 21:37:52 +0000996 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
997 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000998 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +0000999 case PPCISD::MFVSR: return "PPCISD::MFVSR";
1000 case PPCISD::MTVSRA: return "PPCISD::MTVSRA";
1001 case PPCISD::MTVSRZ: return "PPCISD::MTVSRZ";
Evan Cheng32e376f2008-07-12 02:23:19 +00001002 case PPCISD::VCMP: return "PPCISD::VCMP";
1003 case PPCISD::VCMPo: return "PPCISD::VCMPo";
1004 case PPCISD::LBRX: return "PPCISD::LBRX";
1005 case PPCISD::STBRX: return "PPCISD::STBRX";
Hal Finkel3fe09ea2015-01-06 07:02:15 +00001006 case PPCISD::LFIWAX: return "PPCISD::LFIWAX";
1007 case PPCISD::LFIWZX: return "PPCISD::LFIWZX";
Evan Cheng32e376f2008-07-12 02:23:19 +00001008 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +00001009 case PPCISD::BDNZ: return "PPCISD::BDNZ";
1010 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +00001011 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +00001012 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +00001013 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +00001014 case PPCISD::CR6SET: return "PPCISD::CR6SET";
1015 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Roman Divacky32143e22013-12-20 18:08:54 +00001016 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001017 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
1018 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001019 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001020 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
1021 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
Bill Schmidt82f1c772015-02-10 19:09:05 +00001022 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
1023 case PPCISD::ADDI_TLSGD_L_ADDR: return "PPCISD::ADDI_TLSGD_L_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001024 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
1025 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
Bill Schmidt82f1c772015-02-10 19:09:05 +00001026 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
1027 case PPCISD::ADDI_TLSLD_L_ADDR: return "PPCISD::ADDI_TLSLD_L_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001028 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
1029 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +00001030 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +00001031 case PPCISD::SC: return "PPCISD::SC";
Hal Finkelc93a9a22015-02-25 01:06:45 +00001032 case PPCISD::QVFPERM: return "PPCISD::QVFPERM";
1033 case PPCISD::QVGPCI: return "PPCISD::QVGPCI";
1034 case PPCISD::QVALIGNI: return "PPCISD::QVALIGNI";
1035 case PPCISD::QVESPLATI: return "PPCISD::QVESPLATI";
1036 case PPCISD::QBFLT: return "PPCISD::QBFLT";
1037 case PPCISD::QVLFSb: return "PPCISD::QVLFSb";
Chris Lattner347ed8a2006-01-09 23:52:17 +00001038 }
1039}
1040
Hal Finkelc93a9a22015-02-25 01:06:45 +00001041EVT PPCTargetLowering::getSetCCResultType(LLVMContext &C, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00001042 if (!VT.isVector())
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001043 return Subtarget.useCRBits() ? MVT::i1 : MVT::i32;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001044
1045 if (Subtarget.hasQPX())
1046 return EVT::getVectorVT(C, MVT::i1, VT.getVectorNumElements());
1047
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00001048 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +00001049}
1050
Hal Finkel62ac7362014-09-19 11:42:56 +00001051bool PPCTargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1052 assert(VT.isFloatingPoint() && "Non-floating-point FMA?");
1053 return true;
1054}
1055
Chris Lattner4211ca92006-04-14 06:01:58 +00001056//===----------------------------------------------------------------------===//
1057// Node matching predicates, for use by the tblgen matching code.
1058//===----------------------------------------------------------------------===//
1059
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001060/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001061static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001062 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001063 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001064 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001065 // Maybe this has already been legalized into the constant pool?
1066 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001067 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001068 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +00001069 }
1070 return false;
1071}
1072
Chris Lattnere8b83b42006-04-06 17:23:16 +00001073/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
1074/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001075static bool isConstantOrUndef(int Op, int Val) {
1076 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001077}
1078
1079/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
1080/// VPKUHUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001081/// The ShuffleKind distinguishes between big-endian operations with
1082/// two different inputs (0), either-endian operations with two identical
1083/// inputs (1), and little-endian operantion with two different inputs (2).
1084/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1085bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +00001086 SelectionDAG &DAG) {
Eric Christopher8b770652015-01-26 19:03:15 +00001087 bool IsLE = DAG.getTarget().getDataLayout()->isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001088 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +00001089 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001090 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001091 for (unsigned i = 0; i != 16; ++i)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001092 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001093 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001094 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +00001095 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001096 return false;
1097 for (unsigned i = 0; i != 16; ++i)
1098 if (!isConstantOrUndef(N->getMaskElt(i), i*2))
1099 return false;
1100 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +00001101 unsigned j = IsLE ? 0 : 1;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001102 for (unsigned i = 0; i != 8; ++i)
Bill Schmidtf910a062014-06-10 14:35:01 +00001103 if (!isConstantOrUndef(N->getMaskElt(i), i*2+j) ||
1104 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001105 return false;
1106 }
Chris Lattner1d338192006-04-06 18:26:28 +00001107 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001108}
1109
1110/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
1111/// VPKUWUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001112/// The ShuffleKind distinguishes between big-endian operations with
1113/// two different inputs (0), either-endian operations with two identical
1114/// inputs (1), and little-endian operantion with two different inputs (2).
1115/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1116bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +00001117 SelectionDAG &DAG) {
Eric Christopher8b770652015-01-26 19:03:15 +00001118 bool IsLE = DAG.getTarget().getDataLayout()->isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001119 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +00001120 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001121 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001122 for (unsigned i = 0; i != 16; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001123 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
1124 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001125 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001126 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +00001127 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001128 return false;
1129 for (unsigned i = 0; i != 16; i += 2)
1130 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) ||
1131 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1))
1132 return false;
1133 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +00001134 unsigned j = IsLE ? 0 : 2;
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001135 for (unsigned i = 0; i != 8; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00001136 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) ||
1137 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) ||
1138 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) ||
1139 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001140 return false;
1141 }
Chris Lattner1d338192006-04-06 18:26:28 +00001142 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001143}
1144
Chris Lattnerf38e0332006-04-06 22:02:42 +00001145/// isVMerge - Common function, used to match vmrg* shuffles.
1146///
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001147static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +00001148 unsigned LHSStart, unsigned RHSStart) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001149 if (N->getValueType(0) != MVT::v16i8)
1150 return false;
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001151 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
1152 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001153
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001154 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
1155 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001156 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +00001157 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001158 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +00001159 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001160 return false;
1161 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001162 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +00001163}
1164
1165/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +00001166/// a VMRGL* instruction with the specified unit size (1,2 or 4 bytes).
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001167/// The ShuffleKind distinguishes between big-endian merges with two
1168/// different inputs (0), either-endian merges with two identical inputs (1),
1169/// and little-endian merges with two different inputs (2). For the latter,
1170/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +00001171bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001172 unsigned ShuffleKind, SelectionDAG &DAG) {
Eric Christopher8b770652015-01-26 19:03:15 +00001173 if (DAG.getTarget().getDataLayout()->isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001174 if (ShuffleKind == 1) // unary
1175 return isVMerge(N, UnitSize, 0, 0);
1176 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +00001177 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001178 else
1179 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001180 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001181 if (ShuffleKind == 1) // unary
1182 return isVMerge(N, UnitSize, 8, 8);
1183 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +00001184 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001185 else
1186 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001187 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001188}
1189
1190/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +00001191/// a VMRGH* instruction with the specified unit size (1,2 or 4 bytes).
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001192/// The ShuffleKind distinguishes between big-endian merges with two
1193/// different inputs (0), either-endian merges with two identical inputs (1),
1194/// and little-endian merges with two different inputs (2). For the latter,
1195/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +00001196bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001197 unsigned ShuffleKind, SelectionDAG &DAG) {
Eric Christopher8b770652015-01-26 19:03:15 +00001198 if (DAG.getTarget().getDataLayout()->isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001199 if (ShuffleKind == 1) // unary
1200 return isVMerge(N, UnitSize, 8, 8);
1201 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +00001202 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001203 else
1204 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001205 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001206 if (ShuffleKind == 1) // unary
1207 return isVMerge(N, UnitSize, 0, 0);
1208 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +00001209 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00001210 else
1211 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +00001212 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +00001213}
1214
1215
Chris Lattner1d338192006-04-06 18:26:28 +00001216/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
1217/// amount, otherwise return -1.
Bill Schmidt42a69362014-08-05 20:47:25 +00001218/// The ShuffleKind distinguishes between big-endian operations with two
1219/// different inputs (0), either-endian operations with two identical inputs
1220/// (1), and little-endian operations with two different inputs (2). For the
1221/// latter, the input operands are swapped (see PPCInstrAltivec.td).
1222int PPC::isVSLDOIShuffleMask(SDNode *N, unsigned ShuffleKind,
1223 SelectionDAG &DAG) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001224 if (N->getValueType(0) != MVT::v16i8)
Hal Finkela775e512014-04-08 19:00:27 +00001225 return -1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001226
1227 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +00001228
Chris Lattner1d338192006-04-06 18:26:28 +00001229 // Find the first non-undef value in the shuffle mask.
1230 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001231 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +00001232 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001233
Chris Lattner1d338192006-04-06 18:26:28 +00001234 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001235
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001236 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +00001237 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001238 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +00001239 if (ShiftAmt < i) return -1;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001240
Bill Schmidtf04e9982014-08-04 23:21:01 +00001241 ShiftAmt -= i;
Eric Christopher8b770652015-01-26 19:03:15 +00001242 bool isLE = DAG.getTarget().getDataLayout()->isLittleEndian();
Bill Schmidtf910a062014-06-10 14:35:01 +00001243
Bill Schmidt42a69362014-08-05 20:47:25 +00001244 if ((ShuffleKind == 0 && !isLE) || (ShuffleKind == 2 && isLE)) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001245 // Check the rest of the elements to see if they are consecutive.
1246 for (++i; i != 16; ++i)
1247 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
1248 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001249 } else if (ShuffleKind == 1) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001250 // Check the rest of the elements to see if they are consecutive.
1251 for (++i; i != 16; ++i)
1252 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
1253 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001254 } else
1255 return -1;
1256
1257 if (ShuffleKind == 2 && isLE)
1258 ShiftAmt = 16 - ShiftAmt;
Bill Schmidtf04e9982014-08-04 23:21:01 +00001259
Chris Lattner1d338192006-04-06 18:26:28 +00001260 return ShiftAmt;
1261}
Chris Lattnerffc47562006-03-20 06:33:01 +00001262
1263/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
1264/// specifies a splat of a single element that is suitable for input to
1265/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001266bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +00001267 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +00001268 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001269
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001270 // This is a splat operation if each element of the permute is the same, and
1271 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001272 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001273
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001274 // FIXME: Handle UNDEF elements too!
1275 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +00001276 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001277
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001278 // Check that the indices are consecutive, in the case of a multi-byte element
1279 // splatted with a v16i8 mask.
1280 for (unsigned i = 1; i != EltSize; ++i)
1281 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001282 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001283
Chris Lattner95c7adc2006-04-04 17:25:31 +00001284 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001285 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +00001286 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001287 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001288 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001289 }
Chris Lattner95c7adc2006-04-04 17:25:31 +00001290 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +00001291}
1292
1293/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
1294/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Bill Schmidtf910a062014-06-10 14:35:01 +00001295unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize,
1296 SelectionDAG &DAG) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001297 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1298 assert(isSplatShuffleMask(SVOp, EltSize));
Eric Christopher8b770652015-01-26 19:03:15 +00001299 if (DAG.getTarget().getDataLayout()->isLittleEndian())
Bill Schmidtf910a062014-06-10 14:35:01 +00001300 return (16 / EltSize) - 1 - (SVOp->getMaskElt(0) / EltSize);
1301 else
1302 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +00001303}
1304
Chris Lattner74cf9ff2006-04-12 17:37:20 +00001305/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001306/// by using a vspltis[bhw] instruction of the specified element size, return
1307/// the constant being splatted. The ByteSize field indicates the number of
1308/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001309SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001310 SDValue OpVal(nullptr, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001311
1312 // If ByteSize of the splat is bigger than the element size of the
1313 // build_vector, then we have a case where we are checking for a splat where
1314 // multiple elements of the buildvector are folded together into a single
1315 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
1316 unsigned EltSize = 16/N->getNumOperands();
1317 if (EltSize < ByteSize) {
1318 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001319 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001320 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001321
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001322 // See if all of the elements in the buildvector agree across.
1323 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1324 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1325 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001326 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001327
Scott Michelcf0da6c2009-02-17 22:15:04 +00001328
Craig Topper062a2ba2014-04-25 05:30:21 +00001329 if (!UniquedVals[i&(Multiple-1)].getNode())
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001330 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
1331 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001332 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001333 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001334
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001335 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
1336 // either constant or undef values that are identical for each chunk. See
1337 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001338
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001339 // Check to see if all of the leading entries are either 0 or -1. If
1340 // neither, then this won't fit into the immediate field.
1341 bool LeadingZero = true;
1342 bool LeadingOnes = true;
1343 for (unsigned i = 0; i != Multiple-1; ++i) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001344 if (!UniquedVals[i].getNode()) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001345
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001346 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
1347 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
1348 }
1349 // Finally, check the least significant entry.
1350 if (LeadingZero) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001351 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001352 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00001353 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001354 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +00001355 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001356 }
1357 if (LeadingOnes) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001358 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001359 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +00001360 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001361 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +00001362 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001363 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001364
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001365 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001366 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001367
Chris Lattner2771e2c2006-03-25 06:12:06 +00001368 // Check to see if this buildvec has a single non-undef value in its elements.
1369 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1370 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Craig Topper062a2ba2014-04-25 05:30:21 +00001371 if (!OpVal.getNode())
Chris Lattner2771e2c2006-03-25 06:12:06 +00001372 OpVal = N->getOperand(i);
1373 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001374 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001375 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001376
Craig Topper062a2ba2014-04-25 05:30:21 +00001377 if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001378
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001379 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001380 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001381 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001382 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001383 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001384 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001385 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001386 }
1387
1388 // If the splat value is larger than the element value, then we can never do
1389 // this splat. The only case that we could fit the replicated bits into our
1390 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001391 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001392
Benjamin Kramerb4b51502015-03-25 16:49:59 +00001393 // If the element value is larger than the splat value, check if it consists
1394 // of a repeated bit pattern of size ByteSize.
1395 if (!APInt(ValSizeInBytes * 8, Value).isSplat(ByteSize * 8))
1396 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001397
1398 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001399 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001400
Evan Chengb1ddc982006-03-26 09:52:32 +00001401 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001402 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001403
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001404 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001405 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +00001406 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001407 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001408}
1409
Hal Finkelc93a9a22015-02-25 01:06:45 +00001410/// isQVALIGNIShuffleMask - If this is a qvaligni shuffle mask, return the shift
1411/// amount, otherwise return -1.
1412int PPC::isQVALIGNIShuffleMask(SDNode *N) {
1413 EVT VT = N->getValueType(0);
1414 if (VT != MVT::v4f64 && VT != MVT::v4f32 && VT != MVT::v4i1)
1415 return -1;
1416
1417 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1418
1419 // Find the first non-undef value in the shuffle mask.
1420 unsigned i;
1421 for (i = 0; i != 4 && SVOp->getMaskElt(i) < 0; ++i)
1422 /*search*/;
1423
1424 if (i == 4) return -1; // all undef.
1425
1426 // Otherwise, check to see if the rest of the elements are consecutively
1427 // numbered from this value.
1428 unsigned ShiftAmt = SVOp->getMaskElt(i);
1429 if (ShiftAmt < i) return -1;
1430 ShiftAmt -= i;
1431
1432 // Check the rest of the elements to see if they are consecutive.
1433 for (++i; i != 4; ++i)
1434 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
1435 return -1;
1436
1437 return ShiftAmt;
1438}
1439
Chris Lattner4211ca92006-04-14 06:01:58 +00001440//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001441// Addressing Mode Selection
1442//===----------------------------------------------------------------------===//
1443
1444/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1445/// or 64-bit immediate, and if the value can be accurately represented as a
1446/// sign extension from a 16-bit value. If so, this returns true and the
1447/// immediate.
1448static bool isIntS16Immediate(SDNode *N, short &Imm) {
Adam Nemet571eb5f2014-05-20 17:20:34 +00001449 if (!isa<ConstantSDNode>(N))
Chris Lattnera801fced2006-11-08 02:15:41 +00001450 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001451
Dan Gohmaneffb8942008-09-12 16:56:44 +00001452 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001453 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001454 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001455 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001456 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001457}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001458static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001459 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001460}
1461
1462
1463/// SelectAddressRegReg - Given the specified addressed, check to see if it
1464/// can be represented as an indexed [r+r] operation. Returns false if it
1465/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001466bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1467 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001468 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001469 short imm = 0;
1470 if (N.getOpcode() == ISD::ADD) {
1471 if (isIntS16Immediate(N.getOperand(1), imm))
1472 return false; // r+i
1473 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1474 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001475
Chris Lattnera801fced2006-11-08 02:15:41 +00001476 Base = N.getOperand(0);
1477 Index = N.getOperand(1);
1478 return true;
1479 } else if (N.getOpcode() == ISD::OR) {
1480 if (isIntS16Immediate(N.getOperand(1), imm))
1481 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001482
Chris Lattnera801fced2006-11-08 02:15:41 +00001483 // If this is an or of disjoint bitfields, we can codegen this as an add
1484 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1485 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001486 APInt LHSKnownZero, LHSKnownOne;
1487 APInt RHSKnownZero, RHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001488 DAG.computeKnownBits(N.getOperand(0),
1489 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001490
Dan Gohmanf19609a2008-02-27 01:23:58 +00001491 if (LHSKnownZero.getBoolValue()) {
Jay Foada0653a32014-05-14 21:14:37 +00001492 DAG.computeKnownBits(N.getOperand(1),
1493 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001494 // If all of the bits are known zero on the LHS or RHS, the add won't
1495 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001496 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001497 Base = N.getOperand(0);
1498 Index = N.getOperand(1);
1499 return true;
1500 }
1501 }
1502 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001503
Chris Lattnera801fced2006-11-08 02:15:41 +00001504 return false;
1505}
1506
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001507// If we happen to be doing an i64 load or store into a stack slot that has
1508// less than a 4-byte alignment, then the frame-index elimination may need to
1509// use an indexed load or store instruction (because the offset may not be a
1510// multiple of 4). The extra register needed to hold the offset comes from the
1511// register scavenger, and it is possible that the scavenger will need to use
1512// an emergency spill slot. As a result, we need to make sure that a spill slot
1513// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1514// stack slot.
1515static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1516 // FIXME: This does not handle the LWA case.
1517 if (VT != MVT::i64)
1518 return;
1519
Hal Finkel7ab3db52013-07-10 15:29:01 +00001520 // NOTE: We'll exclude negative FIs here, which come from argument
1521 // lowering, because there are no known test cases triggering this problem
1522 // using packed structures (or similar). We can remove this exclusion if
1523 // we find such a test case. The reason why this is so test-case driven is
1524 // because this entire 'fixup' is only to prevent crashes (from the
1525 // register scavenger) on not-really-valid inputs. For example, if we have:
1526 // %a = alloca i1
1527 // %b = bitcast i1* %a to i64*
1528 // store i64* a, i64 b
1529 // then the store should really be marked as 'align 1', but is not. If it
1530 // were marked as 'align 1' then the indexed form would have been
1531 // instruction-selected initially, and the problem this 'fixup' is preventing
1532 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001533 if (FrameIdx < 0)
1534 return;
1535
1536 MachineFunction &MF = DAG.getMachineFunction();
1537 MachineFrameInfo *MFI = MF.getFrameInfo();
1538
1539 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1540 if (Align >= 4)
1541 return;
1542
1543 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1544 FuncInfo->setHasNonRISpills();
1545}
1546
Chris Lattnera801fced2006-11-08 02:15:41 +00001547/// Returns true if the address N can be represented by a base register plus
1548/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001549/// represented as reg+reg. If Aligned is true, only accept displacements
1550/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001551bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001552 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001553 SelectionDAG &DAG,
1554 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001555 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001556 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001557 // If this can be more profitably realized as r+r, fail.
1558 if (SelectAddressRegReg(N, Disp, Base, DAG))
1559 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001560
Chris Lattnera801fced2006-11-08 02:15:41 +00001561 if (N.getOpcode() == ISD::ADD) {
1562 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001563 if (isIntS16Immediate(N.getOperand(1), imm) &&
1564 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001565 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001566 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1567 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001568 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001569 } else {
1570 Base = N.getOperand(0);
1571 }
1572 return true; // [r+i]
1573 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1574 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001575 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001576 && "Cannot handle constant offsets yet!");
1577 Disp = N.getOperand(1).getOperand(0); // The global address.
1578 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001579 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001580 Disp.getOpcode() == ISD::TargetConstantPool ||
1581 Disp.getOpcode() == ISD::TargetJumpTable);
1582 Base = N.getOperand(0);
1583 return true; // [&g+r]
1584 }
1585 } else if (N.getOpcode() == ISD::OR) {
1586 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001587 if (isIntS16Immediate(N.getOperand(1), imm) &&
1588 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001589 // If this is an or of disjoint bitfields, we can codegen this as an add
1590 // (for better address arithmetic) if the LHS and RHS of the OR are
1591 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001592 APInt LHSKnownZero, LHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001593 DAG.computeKnownBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001594
Dan Gohmanf19609a2008-02-27 01:23:58 +00001595 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001596 // If all of the bits are known zero on the LHS or RHS, the add won't
1597 // carry.
Ulrich Weigand55a96652014-07-20 22:26:40 +00001598 if (FrameIndexSDNode *FI =
1599 dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1600 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1601 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1602 } else {
1603 Base = N.getOperand(0);
1604 }
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001605 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001606 return true;
1607 }
1608 }
1609 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1610 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001611
Chris Lattnera801fced2006-11-08 02:15:41 +00001612 // If this address fits entirely in a 16-bit sext immediate field, codegen
1613 // this as "d, 0"
1614 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001615 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001616 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001617 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001618 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001619 return true;
1620 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001621
1622 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001623 if ((CN->getValueType(0) == MVT::i32 ||
1624 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1625 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001626 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001627
Chris Lattnera801fced2006-11-08 02:15:41 +00001628 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001629 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001630
Owen Anderson9f944592009-08-11 20:47:22 +00001631 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1632 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001633 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001634 return true;
1635 }
1636 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001637
Chris Lattnera801fced2006-11-08 02:15:41 +00001638 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001639 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001640 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001641 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1642 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001643 Base = N;
1644 return true; // [r+0]
1645}
1646
1647/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1648/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001649bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1650 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001651 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001652 // Check to see if we can easily represent this as an [r+r] address. This
1653 // will fail if it thinks that the address is more profitably represented as
1654 // reg+imm, e.g. where imm = 0.
1655 if (SelectAddressRegReg(N, Base, Index, DAG))
1656 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001657
Chris Lattnera801fced2006-11-08 02:15:41 +00001658 // If the operand is an addition, always emit this as [r+r], since this is
1659 // better (for code size, and execution, as the memop does the add for free)
1660 // than emitting an explicit add.
1661 if (N.getOpcode() == ISD::ADD) {
1662 Base = N.getOperand(0);
1663 Index = N.getOperand(1);
1664 return true;
1665 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001666
Chris Lattnera801fced2006-11-08 02:15:41 +00001667 // Otherwise, do it the hard way, using R0 as the base register.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001668 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001669 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001670 Index = N;
1671 return true;
1672}
1673
Chris Lattnera801fced2006-11-08 02:15:41 +00001674/// getPreIndexedAddressParts - returns true by value, base pointer and
1675/// offset pointer and addressing mode by reference if the node's address
1676/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001677bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1678 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001679 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001680 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001681 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001682
Ulrich Weigande90b0222013-03-22 14:58:48 +00001683 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001684 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001685 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001686 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001687 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1688 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001689 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001690 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001691 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001692 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001693 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001694 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001695 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001696 } else
1697 return false;
1698
Hal Finkelc93a9a22015-02-25 01:06:45 +00001699 // PowerPC doesn't have preinc load/store instructions for vectors (except
1700 // for QPX, which does have preinc r+r forms).
1701 if (VT.isVector()) {
1702 if (!Subtarget.hasQPX() || (VT != MVT::v4f64 && VT != MVT::v4f32)) {
1703 return false;
1704 } else if (SelectAddressRegRegOnly(Ptr, Offset, Base, DAG)) {
1705 AM = ISD::PRE_INC;
1706 return true;
1707 }
1708 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001709
Ulrich Weigande90b0222013-03-22 14:58:48 +00001710 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1711
1712 // Common code will reject creating a pre-inc form if the base pointer
1713 // is a frame index, or if N is a store and the base pointer is either
1714 // the same as or a predecessor of the value being stored. Check for
1715 // those situations here, and try with swapped Base/Offset instead.
1716 bool Swap = false;
1717
1718 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1719 Swap = true;
1720 else if (!isLoad) {
1721 SDValue Val = cast<StoreSDNode>(N)->getValue();
1722 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1723 Swap = true;
1724 }
1725
1726 if (Swap)
1727 std::swap(Base, Offset);
1728
Hal Finkelca542be2012-06-20 15:43:03 +00001729 AM = ISD::PRE_INC;
1730 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001731 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001732
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001733 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001734 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001735 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001736 return false;
1737 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001738 // LDU/STU need an address with at least 4-byte alignment.
1739 if (Alignment < 4)
1740 return false;
1741
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001742 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001743 return false;
1744 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001745
Chris Lattnerb314b152006-11-11 00:08:42 +00001746 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001747 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1748 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001749 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001750 LD->getExtensionType() == ISD::SEXTLOAD &&
1751 isa<ConstantSDNode>(Offset))
1752 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001753 }
1754
Chris Lattnerce645542006-11-10 02:08:47 +00001755 AM = ISD::PRE_INC;
1756 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001757}
1758
1759//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001760// LowerOperation implementation
1761//===----------------------------------------------------------------------===//
1762
Chris Lattneredb9d842010-11-15 02:46:57 +00001763/// GetLabelAccessInfo - Return true if we should reference labels using a
1764/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
Eric Christophercccae792015-01-30 22:02:31 +00001765static bool GetLabelAccessInfo(const TargetMachine &TM,
1766 const PPCSubtarget &Subtarget,
1767 unsigned &HiOpFlags, unsigned &LoOpFlags,
Craig Topper062a2ba2014-04-25 05:30:21 +00001768 const GlobalValue *GV = nullptr) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001769 HiOpFlags = PPCII::MO_HA;
1770 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001771
Hal Finkel3ee2af72014-07-18 23:29:49 +00001772 // Don't use the pic base if not in PIC relocation model.
1773 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
1774
Chris Lattnerdd6df842010-11-15 03:13:19 +00001775 if (isPIC) {
1776 HiOpFlags |= PPCII::MO_PIC_FLAG;
1777 LoOpFlags |= PPCII::MO_PIC_FLAG;
1778 }
1779
1780 // If this is a reference to a global value that requires a non-lazy-ptr, make
1781 // sure that instruction lowering adds it.
Eric Christophere8dbfe12015-02-13 22:23:04 +00001782 if (GV && Subtarget.hasLazyResolverStub(GV)) {
Chris Lattnerdd6df842010-11-15 03:13:19 +00001783 HiOpFlags |= PPCII::MO_NLP_FLAG;
1784 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001785
Chris Lattnerdd6df842010-11-15 03:13:19 +00001786 if (GV->hasHiddenVisibility()) {
1787 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1788 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1789 }
1790 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001791
Chris Lattneredb9d842010-11-15 02:46:57 +00001792 return isPIC;
1793}
1794
1795static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1796 SelectionDAG &DAG) {
1797 EVT PtrVT = HiPart.getValueType();
1798 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001799 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001800
1801 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1802 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001803
Chris Lattneredb9d842010-11-15 02:46:57 +00001804 // With PIC, the first instruction is actually "GR+hi(&G)".
1805 if (isPIC)
1806 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1807 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001808
Chris Lattneredb9d842010-11-15 02:46:57 +00001809 // Generate non-pic code that has direct accesses to the constant pool.
1810 // The address of the global is just (hi(&g)+lo(&g)).
1811 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1812}
1813
Hal Finkele6698d52015-02-01 15:03:28 +00001814static void setUsesTOCBasePtr(MachineFunction &MF) {
1815 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1816 FuncInfo->setUsesTOCBasePtr();
1817}
1818
1819static void setUsesTOCBasePtr(SelectionDAG &DAG) {
1820 setUsesTOCBasePtr(DAG.getMachineFunction());
1821}
1822
Hal Finkelcf599212015-02-25 21:36:59 +00001823static SDValue getTOCEntry(SelectionDAG &DAG, SDLoc dl, bool Is64Bit,
1824 SDValue GA) {
1825 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
1826 SDValue Reg = Is64Bit ? DAG.getRegister(PPC::X2, VT) :
1827 DAG.getNode(PPCISD::GlobalBaseReg, dl, VT);
1828
1829 SDValue Ops[] = { GA, Reg };
1830 return DAG.getMemIntrinsicNode(PPCISD::TOC_ENTRY, dl,
1831 DAG.getVTList(VT, MVT::Other), Ops, VT,
1832 MachinePointerInfo::getGOT(), 0, false, true,
1833 false, 0);
1834}
1835
Scott Michelcf0da6c2009-02-17 22:15:04 +00001836SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001837 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001838 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001839 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001840 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001841
Roman Divackyace47072012-08-24 16:26:02 +00001842 // 64-bit SVR4 ABI code is always position-independent.
1843 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001844 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00001845 setUsesTOCBasePtr(DAG);
Roman Divackyace47072012-08-24 16:26:02 +00001846 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Hal Finkelcf599212015-02-25 21:36:59 +00001847 return getTOCEntry(DAG, SDLoc(CP), true, GA);
Roman Divackyace47072012-08-24 16:26:02 +00001848 }
1849
Chris Lattneredb9d842010-11-15 02:46:57 +00001850 unsigned MOHiFlag, MOLoFlag;
Eric Christophercccae792015-01-30 22:02:31 +00001851 bool isPIC =
1852 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001853
1854 if (isPIC && Subtarget.isSVR4ABI()) {
1855 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(),
1856 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00001857 return getTOCEntry(DAG, SDLoc(CP), false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001858 }
1859
Chris Lattneredb9d842010-11-15 02:46:57 +00001860 SDValue CPIHi =
1861 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1862 SDValue CPILo =
1863 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1864 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001865}
1866
Dan Gohman21cea8a2010-04-17 15:26:15 +00001867SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001868 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001869 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001870
Roman Divackyace47072012-08-24 16:26:02 +00001871 // 64-bit SVR4 ABI code is always position-independent.
1872 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001873 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00001874 setUsesTOCBasePtr(DAG);
Roman Divackyace47072012-08-24 16:26:02 +00001875 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Hal Finkelcf599212015-02-25 21:36:59 +00001876 return getTOCEntry(DAG, SDLoc(JT), true, GA);
Roman Divackyace47072012-08-24 16:26:02 +00001877 }
1878
Chris Lattneredb9d842010-11-15 02:46:57 +00001879 unsigned MOHiFlag, MOLoFlag;
Eric Christophercccae792015-01-30 22:02:31 +00001880 bool isPIC =
1881 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001882
1883 if (isPIC && Subtarget.isSVR4ABI()) {
1884 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1885 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00001886 return getTOCEntry(DAG, SDLoc(GA), false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001887 }
1888
Chris Lattneredb9d842010-11-15 02:46:57 +00001889 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1890 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1891 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001892}
1893
Dan Gohman21cea8a2010-04-17 15:26:15 +00001894SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1895 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001896 EVT PtrVT = Op.getValueType();
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001897 BlockAddressSDNode *BASDN = cast<BlockAddressSDNode>(Op);
1898 const BlockAddress *BA = BASDN->getBlockAddress();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001899
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001900 // 64-bit SVR4 ABI code is always position-independent.
1901 // The actual BlockAddress is stored in the TOC.
1902 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00001903 setUsesTOCBasePtr(DAG);
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001904 SDValue GA = DAG.getTargetBlockAddress(BA, PtrVT, BASDN->getOffset());
Hal Finkelcf599212015-02-25 21:36:59 +00001905 return getTOCEntry(DAG, SDLoc(BASDN), true, GA);
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001906 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001907
Chris Lattneredb9d842010-11-15 02:46:57 +00001908 unsigned MOHiFlag, MOLoFlag;
Eric Christophercccae792015-01-30 22:02:31 +00001909 bool isPIC =
1910 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001911 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1912 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001913 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1914}
1915
Roman Divackye3f15c982012-06-04 17:36:38 +00001916SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1917 SelectionDAG &DAG) const {
1918
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001919 // FIXME: TLS addresses currently use medium model code sequences,
1920 // which is the most useful form. Eventually support for small and
1921 // large models could be added if users need it, at the cost of
1922 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001923 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001924 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001925 const GlobalValue *GV = GA->getGlobal();
1926 EVT PtrVT = getPointerTy();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001927 bool is64bit = Subtarget.isPPC64();
Justin Hibbitsa88b6052014-11-12 15:16:30 +00001928 const Module *M = DAG.getMachineFunction().getFunction()->getParent();
1929 PICLevel::Level picLevel = M->getPICLevel();
Roman Divackye3f15c982012-06-04 17:36:38 +00001930
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001931 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001932
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001933 if (Model == TLSModel::LocalExec) {
1934 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001935 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001936 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001937 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001938 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1939 is64bit ? MVT::i64 : MVT::i32);
1940 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1941 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1942 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001943
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001944 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001945 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001946 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1947 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00001948 SDValue GOTPtr;
1949 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00001950 setUsesTOCBasePtr(DAG);
Roman Divacky32143e22013-12-20 18:08:54 +00001951 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1952 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1953 PtrVT, GOTReg, TGA);
1954 } else
1955 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001956 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00001957 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001958 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001959 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001960
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001961 if (Model == TLSModel::GeneralDynamic) {
Bill Schmidt82f1c772015-02-10 19:09:05 +00001962 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001963 SDValue GOTPtr;
1964 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00001965 setUsesTOCBasePtr(DAG);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001966 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1967 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1968 GOTReg, TGA);
1969 } else {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00001970 if (picLevel == PICLevel::Small)
1971 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
1972 else
1973 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001974 }
Bill Schmidt82f1c772015-02-10 19:09:05 +00001975 return DAG.getNode(PPCISD::ADDI_TLSGD_L_ADDR, dl, PtrVT,
1976 GOTPtr, TGA, TGA);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001977 }
1978
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001979 if (Model == TLSModel::LocalDynamic) {
Bill Schmidt82f1c772015-02-10 19:09:05 +00001980 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001981 SDValue GOTPtr;
1982 if (is64bit) {
Hal Finkele6698d52015-02-01 15:03:28 +00001983 setUsesTOCBasePtr(DAG);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001984 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1985 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1986 GOTReg, TGA);
1987 } else {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00001988 if (picLevel == PICLevel::Small)
1989 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
1990 else
1991 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001992 }
Bill Schmidt82f1c772015-02-10 19:09:05 +00001993 SDValue TLSAddr = DAG.getNode(PPCISD::ADDI_TLSLD_L_ADDR, dl,
1994 PtrVT, GOTPtr, TGA, TGA);
1995 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl,
1996 PtrVT, TLSAddr, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001997 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1998 }
1999
2000 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00002001}
2002
Chris Lattneredb9d842010-11-15 02:46:57 +00002003SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
2004 SelectionDAG &DAG) const {
2005 EVT PtrVT = Op.getValueType();
2006 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002007 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00002008 const GlobalValue *GV = GSDN->getGlobal();
2009
Chris Lattneredb9d842010-11-15 02:46:57 +00002010 // 64-bit SVR4 ABI code is always position-independent.
2011 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002012 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Hal Finkele6698d52015-02-01 15:03:28 +00002013 setUsesTOCBasePtr(DAG);
Chris Lattneredb9d842010-11-15 02:46:57 +00002014 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
Hal Finkelcf599212015-02-25 21:36:59 +00002015 return getTOCEntry(DAG, DL, true, GA);
Chris Lattneredb9d842010-11-15 02:46:57 +00002016 }
2017
Chris Lattnerdd6df842010-11-15 03:13:19 +00002018 unsigned MOHiFlag, MOLoFlag;
Eric Christophercccae792015-01-30 22:02:31 +00002019 bool isPIC =
2020 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00002021
Hal Finkel3ee2af72014-07-18 23:29:49 +00002022 if (isPIC && Subtarget.isSVR4ABI()) {
2023 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT,
2024 GSDN->getOffset(),
2025 PPCII::MO_PIC_FLAG);
Hal Finkelcf599212015-02-25 21:36:59 +00002026 return getTOCEntry(DAG, DL, false, GA);
Hal Finkel3ee2af72014-07-18 23:29:49 +00002027 }
2028
Chris Lattnerdd6df842010-11-15 03:13:19 +00002029 SDValue GAHi =
2030 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
2031 SDValue GALo =
2032 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00002033
Chris Lattnerdd6df842010-11-15 03:13:19 +00002034 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00002035
Chris Lattnerdd6df842010-11-15 03:13:19 +00002036 // If the global reference is actually to a non-lazy-pointer, we have to do an
2037 // extra load to get the address of the global.
2038 if (MOHiFlag & PPCII::MO_NLP_FLAG)
2039 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002040 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00002041 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00002042}
2043
Dan Gohman21cea8a2010-04-17 15:26:15 +00002044SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00002045 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002046 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002047
Hal Finkel777c9dd2014-03-29 16:04:40 +00002048 if (Op.getValueType() == MVT::v2i64) {
2049 // When the operands themselves are v2i64 values, we need to do something
2050 // special because VSX has no underlying comparison operations for these.
2051 if (Op.getOperand(0).getValueType() == MVT::v2i64) {
2052 // Equality can be handled by casting to the legal type for Altivec
2053 // comparisons, everything else needs to be expanded.
2054 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
2055 return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
2056 DAG.getSetCC(dl, MVT::v4i32,
2057 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)),
2058 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)),
2059 CC));
2060 }
2061
2062 return SDValue();
2063 }
2064
2065 // We handle most of these in the usual way.
2066 return Op;
2067 }
2068
Chris Lattner4211ca92006-04-14 06:01:58 +00002069 // If we're comparing for equality to zero, expose the fact that this is
2070 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
2071 // fold the new nodes.
2072 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2073 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002074 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002075 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00002076 if (VT.bitsLT(MVT::i32)) {
2077 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002078 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002079 }
Duncan Sands13237ac2008-06-06 12:08:01 +00002080 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002081 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
2082 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00002083 DAG.getConstant(Log2b, MVT::i32));
2084 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00002085 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002086 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00002087 // optimized. FIXME: revisit this when we can custom lower all setcc
2088 // optimizations.
2089 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002090 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00002091 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002092
Chris Lattner4211ca92006-04-14 06:01:58 +00002093 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00002094 // by xor'ing the rhs with the lhs, which is faster than setting a
2095 // condition register, reading it back out, and masking the correct bit. The
2096 // normal approach here uses sub to do this instead of xor. Using xor exposes
2097 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002098 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002099 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002100 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002101 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00002102 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002103 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00002104 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002105 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00002106}
2107
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002108SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002109 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00002110 SDNode *Node = Op.getNode();
2111 EVT VT = Node->getValueType(0);
2112 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2113 SDValue InChain = Node->getOperand(0);
2114 SDValue VAListPtr = Node->getOperand(1);
2115 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002116 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002117
Roman Divacky4394e682011-06-28 15:30:42 +00002118 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
2119
2120 // gpr_index
2121 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
2122 VAListPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00002123 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002124 InChain = GprIndex.getValue(1);
2125
2126 if (VT == MVT::i64) {
2127 // Check if GprIndex is even
2128 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
2129 DAG.getConstant(1, MVT::i32));
2130 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
2131 DAG.getConstant(0, MVT::i32), ISD::SETNE);
2132 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
2133 DAG.getConstant(1, MVT::i32));
2134 // Align GprIndex to be even if it isn't
2135 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
2136 GprIndex);
2137 }
2138
2139 // fpr index is 1 byte after gpr
2140 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
2141 DAG.getConstant(1, MVT::i32));
2142
2143 // fpr
2144 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
2145 FprPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00002146 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002147 InChain = FprIndex.getValue(1);
2148
2149 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
2150 DAG.getConstant(8, MVT::i32));
2151
2152 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
2153 DAG.getConstant(4, MVT::i32));
2154
2155 // areas
2156 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002157 MachinePointerInfo(), false, false,
2158 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002159 InChain = OverflowArea.getValue(1);
2160
2161 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002162 MachinePointerInfo(), false, false,
2163 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00002164 InChain = RegSaveArea.getValue(1);
2165
2166 // select overflow_area if index > 8
2167 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
2168 DAG.getConstant(8, MVT::i32), ISD::SETLT);
2169
Roman Divacky4394e682011-06-28 15:30:42 +00002170 // adjustment constant gpr_index * 4/8
2171 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
2172 VT.isInteger() ? GprIndex : FprIndex,
2173 DAG.getConstant(VT.isInteger() ? 4 : 8,
2174 MVT::i32));
2175
2176 // OurReg = RegSaveArea + RegConstant
2177 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
2178 RegConstant);
2179
2180 // Floating types are 32 bytes into RegSaveArea
2181 if (VT.isFloatingPoint())
2182 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
2183 DAG.getConstant(32, MVT::i32));
2184
2185 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
2186 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
2187 VT.isInteger() ? GprIndex : FprIndex,
2188 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
2189 MVT::i32));
2190
2191 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
2192 VT.isInteger() ? VAListPtr : FprPtr,
2193 MachinePointerInfo(SV),
2194 MVT::i8, false, false, 0);
2195
2196 // determine if we should load from reg_save_area or overflow_area
2197 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
2198
2199 // increase overflow_area by 4/8 if gpr/fpr > 8
2200 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
2201 DAG.getConstant(VT.isInteger() ? 4 : 8,
2202 MVT::i32));
2203
2204 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
2205 OverflowAreaPlusN);
2206
2207 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
2208 OverflowAreaPtr,
2209 MachinePointerInfo(),
2210 MVT::i32, false, false, 0);
2211
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00002212 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002213 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002214}
2215
Roman Divackyc3825df2013-07-25 21:36:47 +00002216SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
2217 const PPCSubtarget &Subtarget) const {
2218 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
2219
2220 // We have to copy the entire va_list struct:
2221 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
2222 return DAG.getMemcpy(Op.getOperand(0), Op,
2223 Op.getOperand(1), Op.getOperand(2),
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00002224 DAG.getConstant(12, MVT::i32), 8, false, true, false,
Roman Divackyc3825df2013-07-25 21:36:47 +00002225 MachinePointerInfo(), MachinePointerInfo());
2226}
2227
Duncan Sandsa0984362011-09-06 13:37:06 +00002228SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
2229 SelectionDAG &DAG) const {
2230 return Op.getOperand(0);
2231}
2232
2233SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
2234 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00002235 SDValue Chain = Op.getOperand(0);
2236 SDValue Trmp = Op.getOperand(1); // trampoline
2237 SDValue FPtr = Op.getOperand(2); // nested function
2238 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00002239 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00002240
Owen Anderson53aa7a92009-08-10 22:56:29 +00002241 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002242 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00002243 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00002244 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00002245 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00002246
Scott Michelcf0da6c2009-02-17 22:15:04 +00002247 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00002248 TargetLowering::ArgListEntry Entry;
2249
2250 Entry.Ty = IntPtrTy;
2251 Entry.Node = Trmp; Args.push_back(Entry);
2252
2253 // TrampSize == (isPPC64 ? 48 : 40);
2254 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00002255 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00002256 Args.push_back(Entry);
2257
2258 Entry.Node = FPtr; Args.push_back(Entry);
2259 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002260
Bill Wendling95e1af22008-09-17 00:30:57 +00002261 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002262 TargetLowering::CallLoweringInfo CLI(DAG);
2263 CLI.setDebugLoc(dl).setChain(Chain)
2264 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002265 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
2266 std::move(Args), 0);
Bill Wendling95e1af22008-09-17 00:30:57 +00002267
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002268 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Duncan Sandsa0984362011-09-06 13:37:06 +00002269 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00002270}
2271
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002272SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002273 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00002274 MachineFunction &MF = DAG.getMachineFunction();
2275 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2276
Andrew Trickef9de2a2013-05-25 02:42:55 +00002277 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002278
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002279 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002280 // vastart just stores the address of the VarArgsFrameIndex slot into the
2281 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002282 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002283 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002284 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00002285 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2286 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00002287 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002288 }
2289
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002290 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002291 // We suppose the given va_list is already allocated.
2292 //
2293 // typedef struct {
2294 // char gpr; /* index into the array of 8 GPRs
2295 // * stored in the register save area
2296 // * gpr=0 corresponds to r3,
2297 // * gpr=1 to r4, etc.
2298 // */
2299 // char fpr; /* index into the array of 8 FPRs
2300 // * stored in the register save area
2301 // * fpr=0 corresponds to f1,
2302 // * fpr=1 to f2, etc.
2303 // */
2304 // char *overflow_arg_area;
2305 // /* location on stack that holds
2306 // * the next overflow argument
2307 // */
2308 // char *reg_save_area;
2309 // /* where r3:r10 and f1:f8 (if saved)
2310 // * are stored
2311 // */
2312 // } va_list[1];
2313
2314
Dan Gohman31ae5862010-04-17 14:41:14 +00002315 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
2316 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002317
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002318
Owen Anderson53aa7a92009-08-10 22:56:29 +00002319 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002320
Dan Gohman31ae5862010-04-17 14:41:14 +00002321 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
2322 PtrVT);
2323 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2324 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002325
Duncan Sands13237ac2008-06-06 12:08:01 +00002326 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002327 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002328
Duncan Sands13237ac2008-06-06 12:08:01 +00002329 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002330 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002331
2332 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002333 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002334
Dan Gohman2d489b52008-02-06 22:27:42 +00002335 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002336
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002337 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002338 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00002339 Op.getOperand(1),
2340 MachinePointerInfo(SV),
2341 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002342 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002343 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002344 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002345
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002346 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002347 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00002348 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
2349 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00002350 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002351 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002352 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002353
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002354 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002355 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00002356 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
2357 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002358 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002359 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002360 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002361
2362 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00002363 return DAG.getStore(thirdStore, dl, FR, nextPtr,
2364 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002365 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002366
Chris Lattner4211ca92006-04-14 06:01:58 +00002367}
2368
Chris Lattner4f2e4e02007-03-06 00:59:59 +00002369#include "PPCGenCallingConv.inc"
2370
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002371// Function whose sole purpose is to kill compiler warnings
2372// stemming from unused functions included from PPCGenCallingConv.inc.
2373CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00002374 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002375}
2376
Bill Schmidt230b4512013-06-12 16:39:22 +00002377bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
2378 CCValAssign::LocInfo &LocInfo,
2379 ISD::ArgFlagsTy &ArgFlags,
2380 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002381 return true;
2382}
2383
Bill Schmidt230b4512013-06-12 16:39:22 +00002384bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
2385 MVT &LocVT,
2386 CCValAssign::LocInfo &LocInfo,
2387 ISD::ArgFlagsTy &ArgFlags,
2388 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002389 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002390 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2391 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2392 };
2393 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002394
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002395 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002396
2397 // Skip one register if the first unallocated register has an even register
2398 // number and there are still argument registers available which have not been
2399 // allocated yet. RegNum is actually an index into ArgRegs, which means we
2400 // need to skip a register if RegNum is odd.
2401 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
2402 State.AllocateReg(ArgRegs[RegNum]);
2403 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002404
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002405 // Always return false here, as this function only makes sure that the first
2406 // unallocated register has an odd register number and does not actually
2407 // allocate a register for the current argument.
2408 return false;
2409}
2410
Bill Schmidt230b4512013-06-12 16:39:22 +00002411bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
2412 MVT &LocVT,
2413 CCValAssign::LocInfo &LocInfo,
2414 ISD::ArgFlagsTy &ArgFlags,
2415 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002416 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002417 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2418 PPC::F8
2419 };
2420
2421 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002422
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002423 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002424
2425 // If there is only one Floating-point register left we need to put both f64
2426 // values of a split ppc_fp128 value on the stack.
2427 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
2428 State.AllocateReg(ArgRegs[RegNum]);
2429 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002430
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002431 // Always return false here, as this function only makes sure that the two f64
2432 // values a ppc_fp128 value is split into are both passed in registers or both
2433 // passed on the stack and does not actually allocate a register for the
2434 // current argument.
2435 return false;
2436}
2437
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002438/// FPR - The set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002439/// on Darwin.
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002440static const MCPhysReg FPR[] = {PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5,
2441 PPC::F6, PPC::F7, PPC::F8, PPC::F9, PPC::F10,
2442 PPC::F11, PPC::F12, PPC::F13};
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002443
Benjamin Kramer7149aab2015-03-01 18:09:56 +00002444/// QFPR - The set of QPX registers that should be allocated for arguments.
2445static const MCPhysReg QFPR[] = {
2446 PPC::QF1, PPC::QF2, PPC::QF3, PPC::QF4, PPC::QF5, PPC::QF6, PPC::QF7,
2447 PPC::QF8, PPC::QF9, PPC::QF10, PPC::QF11, PPC::QF12, PPC::QF13};
Hal Finkelc93a9a22015-02-25 01:06:45 +00002448
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002449/// CalculateStackSlotSize - Calculates the size reserved for this argument on
2450/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002451static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002452 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00002453 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002454 if (Flags.isByVal())
2455 ArgSize = Flags.getByValSize();
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002456
2457 // Round up to multiples of the pointer size, except for array members,
2458 // which are always packed.
2459 if (!Flags.isInConsecutiveRegs())
2460 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002461
2462 return ArgSize;
2463}
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002464
2465/// CalculateStackSlotAlignment - Calculates the alignment of this argument
2466/// on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002467static unsigned CalculateStackSlotAlignment(EVT ArgVT, EVT OrigVT,
2468 ISD::ArgFlagsTy Flags,
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002469 unsigned PtrByteSize) {
2470 unsigned Align = PtrByteSize;
2471
2472 // Altivec parameters are padded to a 16 byte boundary.
2473 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2474 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
2475 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64)
2476 Align = 16;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002477 // QPX vector types stored in double-precision are padded to a 32 byte
2478 // boundary.
2479 else if (ArgVT == MVT::v4f64 || ArgVT == MVT::v4i1)
2480 Align = 32;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002481
2482 // ByVal parameters are aligned as requested.
2483 if (Flags.isByVal()) {
2484 unsigned BVAlign = Flags.getByValAlign();
2485 if (BVAlign > PtrByteSize) {
2486 if (BVAlign % PtrByteSize != 0)
2487 llvm_unreachable(
2488 "ByVal alignment is not a multiple of the pointer size");
2489
2490 Align = BVAlign;
2491 }
2492 }
2493
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002494 // Array members are always packed to their original alignment.
2495 if (Flags.isInConsecutiveRegs()) {
2496 // If the array member was split into multiple registers, the first
2497 // needs to be aligned to the size of the full type. (Except for
2498 // ppcf128, which is only aligned as its f64 components.)
2499 if (Flags.isSplit() && OrigVT != MVT::ppcf128)
2500 Align = OrigVT.getStoreSize();
2501 else
2502 Align = ArgVT.getStoreSize();
2503 }
2504
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002505 return Align;
2506}
2507
Ulrich Weigand8658f172014-07-20 23:43:15 +00002508/// CalculateStackSlotUsed - Return whether this argument will use its
2509/// stack slot (instead of being passed in registers). ArgOffset,
2510/// AvailableFPRs, and AvailableVRs must hold the current argument
2511/// position, and will be updated to account for this argument.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002512static bool CalculateStackSlotUsed(EVT ArgVT, EVT OrigVT,
2513 ISD::ArgFlagsTy Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00002514 unsigned PtrByteSize,
2515 unsigned LinkageSize,
2516 unsigned ParamAreaSize,
2517 unsigned &ArgOffset,
2518 unsigned &AvailableFPRs,
Hal Finkelc93a9a22015-02-25 01:06:45 +00002519 unsigned &AvailableVRs, bool HasQPX) {
Ulrich Weigand8658f172014-07-20 23:43:15 +00002520 bool UseMemory = false;
2521
2522 // Respect alignment of argument on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002523 unsigned Align =
2524 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigand8658f172014-07-20 23:43:15 +00002525 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
2526 // If there's no space left in the argument save area, we must
2527 // use memory (this check also catches zero-sized arguments).
2528 if (ArgOffset >= LinkageSize + ParamAreaSize)
2529 UseMemory = true;
2530
2531 // Allocate argument on the stack.
2532 ArgOffset += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002533 if (Flags.isInConsecutiveRegsLast())
2534 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand8658f172014-07-20 23:43:15 +00002535 // If we overran the argument save area, we must use memory
2536 // (this check catches arguments passed partially in memory)
2537 if (ArgOffset > LinkageSize + ParamAreaSize)
2538 UseMemory = true;
2539
2540 // However, if the argument is actually passed in an FPR or a VR,
2541 // we don't use memory after all.
2542 if (!Flags.isByVal()) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00002543 if (ArgVT == MVT::f32 || ArgVT == MVT::f64 ||
2544 // QPX registers overlap with the scalar FP registers.
2545 (HasQPX && (ArgVT == MVT::v4f32 ||
2546 ArgVT == MVT::v4f64 ||
2547 ArgVT == MVT::v4i1)))
Ulrich Weigand8658f172014-07-20 23:43:15 +00002548 if (AvailableFPRs > 0) {
2549 --AvailableFPRs;
2550 return false;
2551 }
2552 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2553 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
2554 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64)
2555 if (AvailableVRs > 0) {
2556 --AvailableVRs;
2557 return false;
2558 }
2559 }
2560
2561 return UseMemory;
2562}
2563
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002564/// EnsureStackAlignment - Round stack frame size up from NumBytes to
2565/// ensure minimum alignment required for target.
Eric Christophercccae792015-01-30 22:02:31 +00002566static unsigned EnsureStackAlignment(const PPCFrameLowering *Lowering,
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002567 unsigned NumBytes) {
Eric Christophercccae792015-01-30 22:02:31 +00002568 unsigned TargetAlign = Lowering->getStackAlignment();
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002569 unsigned AlignMask = TargetAlign - 1;
2570 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2571 return NumBytes;
2572}
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002573
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002574SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002575PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002576 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002577 const SmallVectorImpl<ISD::InputArg>
2578 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002579 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002580 SmallVectorImpl<SDValue> &InVals)
2581 const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002582 if (Subtarget.isSVR4ABI()) {
2583 if (Subtarget.isPPC64())
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002584 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
2585 dl, DAG, InVals);
2586 else
2587 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2588 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002589 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002590 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2591 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002592 }
2593}
2594
2595SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002596PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002597 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002598 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002599 const SmallVectorImpl<ISD::InputArg>
2600 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002601 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002602 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002603
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002604 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002605 // +-----------------------------------+
2606 // +--> | Back chain |
2607 // | +-----------------------------------+
2608 // | | Floating-point register save area |
2609 // | +-----------------------------------+
2610 // | | General register save area |
2611 // | +-----------------------------------+
2612 // | | CR save word |
2613 // | +-----------------------------------+
2614 // | | VRSAVE save word |
2615 // | +-----------------------------------+
2616 // | | Alignment padding |
2617 // | +-----------------------------------+
2618 // | | Vector register save area |
2619 // | +-----------------------------------+
2620 // | | Local variable space |
2621 // | +-----------------------------------+
2622 // | | Parameter list area |
2623 // | +-----------------------------------+
2624 // | | LR save word |
2625 // | +-----------------------------------+
2626 // SP--> +--- | Back chain |
2627 // +-----------------------------------+
2628 //
2629 // Specifications:
2630 // System V Application Binary Interface PowerPC Processor Supplement
2631 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002632
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002633 MachineFunction &MF = DAG.getMachineFunction();
2634 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002635 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002636
Owen Anderson53aa7a92009-08-10 22:56:29 +00002637 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002638 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002639 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2640 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002641 unsigned PtrByteSize = 4;
2642
2643 // Assign locations to all of the incoming arguments.
2644 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002645 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
2646 *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002647
2648 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00002649 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002650 CCInfo.AllocateStack(LinkageSize, PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002651
Bill Schmidtef17c142013-02-06 17:33:58 +00002652 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002653
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002654 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2655 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002656
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002657 // Arguments stored in registers.
2658 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002659 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002660 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002661
Owen Anderson9f944592009-08-11 20:47:22 +00002662 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002663 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002664 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002665 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002666 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002667 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002668 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002669 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00002670 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002671 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002672 case MVT::f64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002673 if (Subtarget.hasVSX())
Hal Finkel19be5062014-03-29 05:29:01 +00002674 RC = &PPC::VSFRCRegClass;
2675 else
2676 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002677 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002678 case MVT::v16i8:
2679 case MVT::v8i16:
2680 case MVT::v4i32:
Hal Finkel7811c612014-03-28 19:58:11 +00002681 RC = &PPC::VRRCRegClass;
2682 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002683 case MVT::v4f32:
2684 RC = Subtarget.hasQPX() ? &PPC::QSRCRegClass : &PPC::VRRCRegClass;
2685 break;
Hal Finkel27774d92014-03-13 07:58:58 +00002686 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002687 case MVT::v2i64:
Hal Finkel7811c612014-03-28 19:58:11 +00002688 RC = &PPC::VSHRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002689 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002690 case MVT::v4f64:
2691 RC = &PPC::QFRCRegClass;
2692 break;
2693 case MVT::v4i1:
2694 RC = &PPC::QBRCRegClass;
2695 break;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002696 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002697
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002698 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002699 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002700 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2701 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2702
2703 if (ValVT == MVT::i1)
2704 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002705
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002706 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002707 } else {
2708 // Argument stored in memory.
2709 assert(VA.isMemLoc());
2710
Hal Finkel940ab932014-02-28 00:27:01 +00002711 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002712 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002713 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002714
2715 // Create load nodes to retrieve arguments from the stack.
2716 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002717 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2718 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002719 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002720 }
2721 }
2722
2723 // Assign locations to all of the incoming aggregate by value arguments.
2724 // Aggregates passed by value are stored in the local variable space of the
2725 // caller's stack frame, right above the parameter list area.
2726 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002727 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Eric Christopherb5217502014-08-06 18:45:26 +00002728 ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002729
2730 // Reserve stack space for the allocations in CCInfo.
2731 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2732
Bill Schmidtef17c142013-02-06 17:33:58 +00002733 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002734
2735 // Area that is at least reserved in the caller of this function.
2736 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002737 MinReservedArea = std::max(MinReservedArea, LinkageSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00002738
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002739 // Set the size that is at least reserved in caller of this function. Tail
2740 // call optimized function's reserved stack space needs to be aligned so that
2741 // taking the difference between two stack areas will result in an aligned
2742 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00002743 MinReservedArea =
2744 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002745 FuncInfo->setMinReservedArea(MinReservedArea);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002746
2747 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002748
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002749 // If the function takes variable number of arguments, make a frame index for
2750 // the start of the first vararg value... for expansion of llvm.va_start.
2751 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +00002752 static const MCPhysReg GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002753 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2754 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2755 };
2756 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2757
Craig Topper840beec2014-04-04 05:16:06 +00002758 static const MCPhysReg FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002759 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2760 PPC::F8
2761 };
Joerg Sonnenbergereb8655a2014-08-08 16:46:10 +00002762 unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2763 if (DisablePPCFloatInVariadic)
2764 NumFPArgRegs = 0;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002765
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002766 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs));
2767 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002768
2769 // Make room for NumGPArgRegs and NumFPArgRegs.
2770 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Craig Topper7ff15922014-09-10 04:51:36 +00002771 NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002772
Dan Gohman31ae5862010-04-17 14:41:14 +00002773 FuncInfo->setVarArgsStackOffset(
2774 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002775 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002776
Dan Gohman31ae5862010-04-17 14:41:14 +00002777 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2778 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002779
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002780 // The fixed integer arguments of a variadic function are stored to the
2781 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2782 // the result of va_next.
2783 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2784 // Get an existing live-in vreg, or add a new one.
2785 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2786 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002787 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002788
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002789 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002790 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2791 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002792 MemOps.push_back(Store);
2793 // Increment the address by four for the next argument to store
2794 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2795 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2796 }
2797
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002798 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2799 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002800 // The double arguments are stored to the VarArgsFrameIndex
2801 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002802 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2803 // Get an existing live-in vreg, or add a new one.
2804 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2805 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002806 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002807
Owen Anderson9f944592009-08-11 20:47:22 +00002808 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002809 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2810 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002811 MemOps.push_back(Store);
2812 // Increment the address by eight for the next argument to store
Craig Topper7ff15922014-09-10 04:51:36 +00002813 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002814 PtrVT);
2815 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2816 }
2817 }
2818
2819 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002820 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002821
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002822 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002823}
2824
Bill Schmidt57d6de52012-10-23 15:51:16 +00002825// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2826// value to MVT::i64 and then truncate to the correct register size.
2827SDValue
2828PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2829 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002830 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002831 if (Flags.isSExt())
2832 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2833 DAG.getValueType(ObjectVT));
2834 else if (Flags.isZExt())
2835 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2836 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002837
Hal Finkel940ab932014-02-28 00:27:01 +00002838 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002839}
2840
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002841SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002842PPCTargetLowering::LowerFormalArguments_64SVR4(
2843 SDValue Chain,
2844 CallingConv::ID CallConv, bool isVarArg,
2845 const SmallVectorImpl<ISD::InputArg>
2846 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002847 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002848 SmallVectorImpl<SDValue> &InVals) const {
2849 // TODO: add description of PPC stack frame format, or at least some docs.
2850 //
Ulrich Weigand8658f172014-07-20 23:43:15 +00002851 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00002852 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002853 MachineFunction &MF = DAG.getMachineFunction();
2854 MachineFrameInfo *MFI = MF.getFrameInfo();
2855 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2856
Hal Finkelf81b6dd2015-01-18 12:08:47 +00002857 assert(!(CallConv == CallingConv::Fast && isVarArg) &&
2858 "fastcc not supported on varargs functions");
2859
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002860 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2861 // Potential tail calls could cause overwriting of argument stack slots.
2862 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2863 (CallConv == CallingConv::Fast));
2864 unsigned PtrByteSize = 8;
Eric Christophera4ae2132015-02-13 22:22:57 +00002865 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002866
Craig Topper840beec2014-04-04 05:16:06 +00002867 static const MCPhysReg GPR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002868 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2869 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2870 };
Craig Topper840beec2014-04-04 05:16:06 +00002871 static const MCPhysReg VR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002872 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2873 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2874 };
Craig Topper840beec2014-04-04 05:16:06 +00002875 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00002876 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
2877 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
2878 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002879
2880 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2881 const unsigned Num_FPR_Regs = 13;
2882 const unsigned Num_VR_Regs = array_lengthof(VR);
Hal Finkelc93a9a22015-02-25 01:06:45 +00002883 const unsigned Num_QFPR_Regs = Num_FPR_Regs;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002884
Ulrich Weigand8658f172014-07-20 23:43:15 +00002885 // Do a first pass over the arguments to determine whether the ABI
2886 // guarantees that our caller has allocated the parameter save area
2887 // on its stack frame. In the ELFv1 ABI, this is always the case;
2888 // in the ELFv2 ABI, it is true if this is a vararg function or if
2889 // any parameter is located in a stack slot.
2890
2891 bool HasParameterArea = !isELFv2ABI || isVarArg;
2892 unsigned ParamAreaSize = Num_GPR_Regs * PtrByteSize;
2893 unsigned NumBytes = LinkageSize;
2894 unsigned AvailableFPRs = Num_FPR_Regs;
2895 unsigned AvailableVRs = Num_VR_Regs;
2896 for (unsigned i = 0, e = Ins.size(); i != e; ++i)
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002897 if (CalculateStackSlotUsed(Ins[i].VT, Ins[i].ArgVT, Ins[i].Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00002898 PtrByteSize, LinkageSize, ParamAreaSize,
Hal Finkelc93a9a22015-02-25 01:06:45 +00002899 NumBytes, AvailableFPRs, AvailableVRs,
2900 Subtarget.hasQPX()))
Ulrich Weigand8658f172014-07-20 23:43:15 +00002901 HasParameterArea = true;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002902
2903 // Add DAG nodes to load the arguments or copy them out of registers. On
2904 // entry to a function on PPC, the arguments start after the linkage area,
2905 // although the first ones are often in registers.
2906
Ulrich Weigand8658f172014-07-20 23:43:15 +00002907 unsigned ArgOffset = LinkageSize;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00002908 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002909 unsigned &QFPR_idx = FPR_idx;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002910 SmallVector<SDValue, 8> MemOps;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002911 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002912 unsigned CurArgIdx = 0;
2913 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002914 SDValue ArgVal;
2915 bool needsLoad = false;
2916 EVT ObjectVT = Ins[ArgNo].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002917 EVT OrigVT = Ins[ArgNo].ArgVT;
Hal Finkel940ab932014-02-28 00:27:01 +00002918 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002919 unsigned ArgSize = ObjSize;
2920 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Andrew Trick05938a52015-02-16 18:10:47 +00002921 if (Ins[ArgNo].isOrigArg()) {
2922 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx);
2923 CurArgIdx = Ins[ArgNo].getOrigArgIndex();
2924 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00002925 // We re-align the argument offset for each argument, except when using the
2926 // fast calling convention, when we need to make sure we do that only when
2927 // we'll actually use a stack slot.
2928 unsigned CurArgOffset, Align;
2929 auto ComputeArgOffset = [&]() {
2930 /* Respect alignment of argument on the stack. */
2931 Align = CalculateStackSlotAlignment(ObjectVT, OrigVT, Flags, PtrByteSize);
2932 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
2933 CurArgOffset = ArgOffset;
2934 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002935
Hal Finkelf81b6dd2015-01-18 12:08:47 +00002936 if (CallConv != CallingConv::Fast) {
2937 ComputeArgOffset();
2938
2939 /* Compute GPR index associated with argument offset. */
2940 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
2941 GPR_idx = std::min(GPR_idx, Num_GPR_Regs);
2942 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002943
2944 // FIXME the codegen can be much improved in some cases.
2945 // We do not have to keep everything in memory.
2946 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00002947 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit");
2948
Hal Finkelf81b6dd2015-01-18 12:08:47 +00002949 if (CallConv == CallingConv::Fast)
2950 ComputeArgOffset();
2951
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002952 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2953 ObjSize = Flags.getByValSize();
2954 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002955 // Empty aggregate parameters do not take up registers. Examples:
2956 // struct { } a;
2957 // union { } b;
2958 // int c[0];
2959 // etc. However, we have to provide a place-holder in InVals, so
2960 // pretend we have an 8-byte item at the current address for that
2961 // purpose.
2962 if (!ObjSize) {
2963 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2964 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2965 InVals.push_back(FIN);
2966 continue;
2967 }
Hal Finkel262a2242013-09-12 23:20:06 +00002968
Ulrich Weigand24195972014-07-20 22:36:52 +00002969 // Create a stack object covering all stack doublewords occupied
Ulrich Weigand8658f172014-07-20 23:43:15 +00002970 // by the argument. If the argument is (fully or partially) on
2971 // the stack, or if the argument is fully in registers but the
2972 // caller has allocated the parameter save anyway, we can refer
2973 // directly to the caller's stack frame. Otherwise, create a
2974 // local copy in our own frame.
2975 int FI;
2976 if (HasParameterArea ||
2977 ArgSize + ArgOffset > LinkageSize + Num_GPR_Regs * PtrByteSize)
Hal Finkel41a55ad2014-08-16 00:17:05 +00002978 FI = MFI->CreateFixedObject(ArgSize, ArgOffset, false, true);
Ulrich Weigand8658f172014-07-20 23:43:15 +00002979 else
2980 FI = MFI->CreateStackObject(ArgSize, Align, false);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002981 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002982
Ulrich Weigand24195972014-07-20 22:36:52 +00002983 // Handle aggregates smaller than 8 bytes.
2984 if (ObjSize < PtrByteSize) {
2985 // The value of the object is its address, which differs from the
2986 // address of the enclosing doubleword on big-endian systems.
2987 SDValue Arg = FIN;
2988 if (!isLittleEndian) {
2989 SDValue ArgOff = DAG.getConstant(PtrByteSize - ObjSize, PtrVT);
2990 Arg = DAG.getNode(ISD::ADD, dl, ArgOff.getValueType(), Arg, ArgOff);
2991 }
2992 InVals.push_back(Arg);
2993
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002994 if (GPR_idx != Num_GPR_Regs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00002995 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002996 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002997 SDValue Store;
2998
2999 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
3000 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
3001 (ObjSize == 2 ? MVT::i16 : MVT::i32));
Ulrich Weigand24195972014-07-20 22:36:52 +00003002 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, Arg,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003003 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003004 ObjType, false, false, 0);
3005 } else {
3006 // For sizes that don't fit a truncating store (3, 5, 6, 7),
3007 // store the whole register as-is to the parameter save area
Ulrich Weigand24195972014-07-20 22:36:52 +00003008 // slot.
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003009 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003010 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003011 false, false, 0);
3012 }
3013
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003014 MemOps.push_back(Store);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003015 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003016 // Whether we copied from a register or not, advance the offset
3017 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003018 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003019 continue;
3020 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00003021
Ulrich Weigand24195972014-07-20 22:36:52 +00003022 // The value of the object is its address, which is the address of
3023 // its first stack doubleword.
3024 InVals.push_back(FIN);
3025
3026 // Store whatever pieces of the object are in registers to memory.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003027 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
Ulrich Weigand24195972014-07-20 22:36:52 +00003028 if (GPR_idx == Num_GPR_Regs)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003029 break;
Ulrich Weigand24195972014-07-20 22:36:52 +00003030
3031 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3032 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
3033 SDValue Addr = FIN;
3034 if (j) {
3035 SDValue Off = DAG.getConstant(j, PtrVT);
3036 Addr = DAG.getNode(ISD::ADD, dl, Off.getValueType(), Addr, Off);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003037 }
Ulrich Weigand24195972014-07-20 22:36:52 +00003038 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, Addr,
3039 MachinePointerInfo(FuncArg, j),
3040 false, false, 0);
3041 MemOps.push_back(Store);
3042 ++GPR_idx;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003043 }
Ulrich Weigand24195972014-07-20 22:36:52 +00003044 ArgOffset += ArgSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003045 continue;
3046 }
3047
3048 switch (ObjectVT.getSimpleVT().SimpleTy) {
3049 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00003050 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003051 case MVT::i32:
3052 case MVT::i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003053 // These can be scalar arguments or elements of an integer array type
3054 // passed directly. Clang may use those instead of "byval" aggregate
3055 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003056 if (GPR_idx != Num_GPR_Regs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003057 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003058 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
3059
Hal Finkel940ab932014-02-28 00:27:01 +00003060 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003061 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
3062 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003063 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003064 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003065 if (CallConv == CallingConv::Fast)
3066 ComputeArgOffset();
3067
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003068 needsLoad = true;
3069 ArgSize = PtrByteSize;
3070 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003071 if (CallConv != CallingConv::Fast || needsLoad)
3072 ArgOffset += 8;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003073 break;
3074
3075 case MVT::f32:
3076 case MVT::f64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003077 // These can be scalar arguments or elements of a float array type
3078 // passed directly. The latter are used to implement ELFv2 homogenous
3079 // float aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003080 if (FPR_idx != Num_FPR_Regs) {
3081 unsigned VReg;
3082
3083 if (ObjectVT == MVT::f32)
3084 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
3085 else
Eric Christophercccae792015-01-30 22:02:31 +00003086 VReg = MF.addLiveIn(FPR[FPR_idx], Subtarget.hasVSX()
3087 ? &PPC::VSFRCRegClass
3088 : &PPC::F8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003089
3090 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
3091 ++FPR_idx;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003092 } else if (GPR_idx != Num_GPR_Regs && CallConv != CallingConv::Fast) {
Hal Finkel8ea446b2015-01-18 14:31:10 +00003093 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8
3094 // once we support fp <-> gpr moves.
3095
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003096 // This can only ever happen in the presence of f32 array types,
3097 // since otherwise we never run out of FPRs before running out
3098 // of GPRs.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003099 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003100 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
3101
3102 if (ObjectVT == MVT::f32) {
3103 if ((ArgOffset % PtrByteSize) == (isLittleEndian ? 4 : 0))
3104 ArgVal = DAG.getNode(ISD::SRL, dl, MVT::i64, ArgVal,
3105 DAG.getConstant(32, MVT::i32));
3106 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
3107 }
3108
3109 ArgVal = DAG.getNode(ISD::BITCAST, dl, ObjectVT, ArgVal);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003110 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003111 if (CallConv == CallingConv::Fast)
3112 ComputeArgOffset();
3113
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003114 needsLoad = true;
3115 }
3116
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003117 // When passing an array of floats, the array occupies consecutive
3118 // space in the argument area; only round up to the next doubleword
3119 // at the end of the array. Otherwise, each float takes 8 bytes.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003120 if (CallConv != CallingConv::Fast || needsLoad) {
3121 ArgSize = Flags.isInConsecutiveRegs() ? ObjSize : PtrByteSize;
3122 ArgOffset += ArgSize;
3123 if (Flags.isInConsecutiveRegsLast())
3124 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
3125 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003126 break;
3127 case MVT::v4f32:
3128 case MVT::v4i32:
3129 case MVT::v8i16:
3130 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00003131 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00003132 case MVT::v2i64:
Hal Finkelc93a9a22015-02-25 01:06:45 +00003133 if (!Subtarget.hasQPX()) {
Ulrich Weigand85d5df22014-07-21 00:13:26 +00003134 // These can be scalar arguments or elements of a vector array type
3135 // passed directly. The latter are used to implement ELFv2 homogenous
3136 // vector aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003137 if (VR_idx != Num_VR_Regs) {
Hal Finkel7811c612014-03-28 19:58:11 +00003138 unsigned VReg = (ObjectVT == MVT::v2f64 || ObjectVT == MVT::v2i64) ?
3139 MF.addLiveIn(VSRH[VR_idx], &PPC::VSHRCRegClass) :
3140 MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003141 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003142 ++VR_idx;
3143 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003144 if (CallConv == CallingConv::Fast)
3145 ComputeArgOffset();
3146
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003147 needsLoad = true;
3148 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00003149 if (CallConv != CallingConv::Fast || needsLoad)
3150 ArgOffset += 16;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003151 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00003152 } // not QPX
3153
3154 assert(ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 &&
3155 "Invalid QPX parameter type");
3156 /* fall through */
3157
3158 case MVT::v4f64:
3159 case MVT::v4i1:
3160 // QPX vectors are treated like their scalar floating-point subregisters
3161 // (except that they're larger).
3162 unsigned Sz = ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 ? 16 : 32;
3163 if (QFPR_idx != Num_QFPR_Regs) {
3164 const TargetRegisterClass *RC;
3165 switch (ObjectVT.getSimpleVT().SimpleTy) {
3166 case MVT::v4f64: RC = &PPC::QFRCRegClass; break;
3167 case MVT::v4f32: RC = &PPC::QSRCRegClass; break;
3168 default: RC = &PPC::QBRCRegClass; break;
3169 }
3170
3171 unsigned VReg = MF.addLiveIn(QFPR[QFPR_idx], RC);
3172 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
3173 ++QFPR_idx;
3174 } else {
3175 if (CallConv == CallingConv::Fast)
3176 ComputeArgOffset();
3177 needsLoad = true;
3178 }
3179 if (CallConv != CallingConv::Fast || needsLoad)
3180 ArgOffset += Sz;
3181 break;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003182 }
3183
3184 // We need to load the argument to a virtual register if we determined
3185 // above that we ran out of physical registers of the appropriate type.
3186 if (needsLoad) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00003187 if (ObjSize < ArgSize && !isLittleEndian)
3188 CurArgOffset += ArgSize - ObjSize;
3189 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, isImmutable);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003190 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
3191 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
3192 false, false, false, 0);
3193 }
3194
3195 InVals.push_back(ArgVal);
3196 }
3197
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003198 // Area that is at least reserved in the caller of this function.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00003199 unsigned MinReservedArea;
Ulrich Weigand8658f172014-07-20 23:43:15 +00003200 if (HasParameterArea)
3201 MinReservedArea = std::max(ArgOffset, LinkageSize + 8 * PtrByteSize);
3202 else
3203 MinReservedArea = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003204
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003205 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003206 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003207 // taking the difference between two stack areas will result in an aligned
3208 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00003209 MinReservedArea =
3210 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003211 FuncInfo->setMinReservedArea(MinReservedArea);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003212
3213 // If the function takes variable number of arguments, make a frame index for
3214 // the start of the first vararg value... for expansion of llvm.va_start.
3215 if (isVarArg) {
3216 int Depth = ArgOffset;
3217
3218 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00003219 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003220 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
3221
3222 // If this function is vararg, store any remaining integer argument regs
3223 // to their spots on the stack so that they may be loaded by deferencing the
3224 // result of va_next.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00003225 for (GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
3226 GPR_idx < Num_GPR_Regs; ++GPR_idx) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003227 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3228 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
3229 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3230 MachinePointerInfo(), false, false, 0);
3231 MemOps.push_back(Store);
3232 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00003233 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003234 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
3235 }
3236 }
3237
3238 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003239 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003240
3241 return Chain;
3242}
3243
3244SDValue
3245PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003246 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003247 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003248 const SmallVectorImpl<ISD::InputArg>
3249 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003250 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003251 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003252 // TODO: add description of PPC stack frame format, or at least some docs.
3253 //
3254 MachineFunction &MF = DAG.getMachineFunction();
3255 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00003256 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003257
Owen Anderson53aa7a92009-08-10 22:56:29 +00003258 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003259 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003260 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003261 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
3262 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00003263 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Eric Christophera4ae2132015-02-13 22:22:57 +00003264 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003265 unsigned ArgOffset = LinkageSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003266 // Area that is at least reserved in caller of this function.
3267 unsigned MinReservedArea = ArgOffset;
3268
Craig Topper840beec2014-04-04 05:16:06 +00003269 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003270 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
3271 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
3272 };
Craig Topper840beec2014-04-04 05:16:06 +00003273 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00003274 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3275 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3276 };
Craig Topper840beec2014-04-04 05:16:06 +00003277 static const MCPhysReg VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003278 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3279 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3280 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00003281
Owen Andersone2f23a32007-09-07 04:06:50 +00003282 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003283 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00003284 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00003285
3286 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003287
Craig Topper840beec2014-04-04 05:16:06 +00003288 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003289
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003290 // In 32-bit non-varargs functions, the stack space for vectors is after the
3291 // stack space for non-vectors. We do not use this space unless we have
3292 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00003293 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003294 // that out...for the pathological case, compute VecArgOffset as the
3295 // start of the vector parameter area. Computing VecArgOffset is the
3296 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003297 unsigned VecArgOffset = ArgOffset;
3298 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003299 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003300 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003301 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003302 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003303
Duncan Sandsd97eea32008-03-21 09:14:45 +00003304 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003305 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00003306 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003307 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003308 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
3309 VecArgOffset += ArgSize;
3310 continue;
3311 }
3312
Owen Anderson9f944592009-08-11 20:47:22 +00003313 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003314 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003315 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003316 case MVT::i32:
3317 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003318 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003319 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003320 case MVT::i64: // PPC64
3321 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003322 // FIXME: We are guaranteed to be !isPPC64 at this point.
3323 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003324 VecArgOffset += 8;
3325 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003326 case MVT::v4f32:
3327 case MVT::v4i32:
3328 case MVT::v8i16:
3329 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003330 // Nothing to do, we're only looking at Nonvector args here.
3331 break;
3332 }
3333 }
3334 }
3335 // We've found where the vector parameter area in memory is. Skip the
3336 // first 12 parameters; these don't use that memory.
3337 VecArgOffset = ((VecArgOffset+15)/16)*16;
3338 VecArgOffset += 12*16;
3339
Chris Lattner4302e8f2006-05-16 18:18:50 +00003340 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00003341 // entry to a function on PPC, the arguments start after the linkage area,
3342 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00003343
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003344 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003345 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00003346 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00003347 unsigned CurArgIdx = 0;
3348 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003349 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003350 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00003351 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00003352 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00003353 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003354 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Andrew Trick05938a52015-02-16 18:10:47 +00003355 if (Ins[ArgNo].isOrigArg()) {
3356 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx);
3357 CurArgIdx = Ins[ArgNo].getOrigArgIndex();
3358 }
Chris Lattner318f0d22006-05-16 18:51:52 +00003359 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003360
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003361 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00003362 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
3363 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003364 if (isVarArg || isPPC64) {
3365 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003366 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003367 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003368 PtrByteSize);
3369 } else nAltivecParamsAtEnd++;
3370 } else
3371 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003372 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003373 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003374 PtrByteSize);
3375
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003376 // FIXME the codegen can be much improved in some cases.
3377 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003378 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003379 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit");
3380
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003381 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003382 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003383 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003384 // Objects of size 1 and 2 are right justified, everything else is
3385 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00003386 if (ObjSize==1 || ObjSize==2) {
3387 CurArgOffset = CurArgOffset + (4 - ObjSize);
3388 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003389 // The value of the object is its address.
Hal Finkel41a55ad2014-08-16 00:17:05 +00003390 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, false, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003391 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003392 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003393 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00003394 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003395 unsigned VReg;
3396 if (isPPC64)
3397 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3398 else
3399 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003400 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003401 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003402 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003403 MachinePointerInfo(FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003404 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00003405 MemOps.push_back(Store);
3406 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00003407 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003408
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003409 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00003410
Dale Johannesen21a8f142008-03-08 01:41:42 +00003411 continue;
3412 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003413 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
3414 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003415 // to memory. ArgOffset will be the address of the beginning
3416 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003417 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003418 unsigned VReg;
3419 if (isPPC64)
3420 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3421 else
3422 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00003423 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003424 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003425 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003426 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003427 MachinePointerInfo(FuncArg, j),
David Greene87a5abe2010-02-15 16:56:53 +00003428 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003429 MemOps.push_back(Store);
3430 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003431 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003432 } else {
3433 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
3434 break;
3435 }
3436 }
3437 continue;
3438 }
3439
Owen Anderson9f944592009-08-11 20:47:22 +00003440 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003441 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003442 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003443 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00003444 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00003445 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003446 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003447 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Hal Finkel7f908e82014-03-06 00:45:19 +00003448
3449 if (ObjectVT == MVT::i1)
3450 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal);
3451
Bill Wendling968f32c2008-03-07 20:49:02 +00003452 ++GPR_idx;
3453 } else {
3454 needsLoad = true;
3455 ArgSize = PtrByteSize;
3456 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003457 // All int arguments reserve stack space in the Darwin ABI.
3458 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00003459 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003460 }
Bill Wendling968f32c2008-03-07 20:49:02 +00003461 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00003462 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00003463 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003464 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003465 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00003466
Hal Finkel940ab932014-02-28 00:27:01 +00003467 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00003468 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00003469 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003470 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00003471
Chris Lattnerec78cad2006-06-26 22:48:35 +00003472 ++GPR_idx;
3473 } else {
3474 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00003475 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003476 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003477 // All int arguments reserve stack space in the Darwin ABI.
3478 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003479 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003480
Owen Anderson9f944592009-08-11 20:47:22 +00003481 case MVT::f32:
3482 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00003483 // Every 4 bytes of argument space consumes one of the GPRs available for
3484 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003485 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003486 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00003487 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003488 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00003489 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003490 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003491 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003492
Owen Anderson9f944592009-08-11 20:47:22 +00003493 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00003494 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003495 else
Devang Patelf3292b22011-02-21 23:21:26 +00003496 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003497
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003498 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003499 ++FPR_idx;
3500 } else {
3501 needsLoad = true;
3502 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003503
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003504 // All FP arguments reserve stack space in the Darwin ABI.
3505 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003506 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003507 case MVT::v4f32:
3508 case MVT::v4i32:
3509 case MVT::v8i16:
3510 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00003511 // Note that vector arguments in registers don't reserve stack space,
3512 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003513 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003514 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003515 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00003516 if (isVarArg) {
3517 while ((ArgOffset % 16) != 0) {
3518 ArgOffset += PtrByteSize;
3519 if (GPR_idx != Num_GPR_Regs)
3520 GPR_idx++;
3521 }
3522 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003523 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00003524 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003525 ++VR_idx;
3526 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003527 if (!isVarArg && !isPPC64) {
3528 // Vectors go after all the nonvectors.
3529 CurArgOffset = VecArgOffset;
3530 VecArgOffset += 16;
3531 } else {
3532 // Vectors are aligned.
3533 ArgOffset = ((ArgOffset+15)/16)*16;
3534 CurArgOffset = ArgOffset;
3535 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00003536 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003537 needsLoad = true;
3538 }
3539 break;
3540 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003541
Chris Lattner4302e8f2006-05-16 18:18:50 +00003542 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003543 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003544 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003545 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003546 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00003547 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003548 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00003549 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003550 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003551 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003552
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003553 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003554 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003555
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003556 // Allow for Altivec parameters at the end, if needed.
3557 if (nAltivecParamsAtEnd) {
3558 MinReservedArea = ((MinReservedArea+15)/16)*16;
3559 MinReservedArea += 16*nAltivecParamsAtEnd;
3560 }
3561
3562 // Area that is at least reserved in the caller of this function.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003563 MinReservedArea = std::max(MinReservedArea, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003564
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003565 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003566 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003567 // taking the difference between two stack areas will result in an aligned
3568 // stack.
Eric Christophercccae792015-01-30 22:02:31 +00003569 MinReservedArea =
3570 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003571 FuncInfo->setMinReservedArea(MinReservedArea);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003572
Chris Lattner4302e8f2006-05-16 18:18:50 +00003573 // If the function takes variable number of arguments, make a frame index for
3574 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003575 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003576 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003577
Dan Gohman31ae5862010-04-17 14:41:14 +00003578 FuncInfo->setVarArgsFrameIndex(
3579 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00003580 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00003581 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003582
Chris Lattner4302e8f2006-05-16 18:18:50 +00003583 // If this function is vararg, store any remaining integer argument regs
3584 // to their spots on the stack so that they may be loaded by deferencing the
3585 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003586 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00003587 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00003588
Chris Lattner2cca3852006-11-18 01:57:19 +00003589 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00003590 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003591 else
Devang Patelf3292b22011-02-21 23:21:26 +00003592 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003593
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003594 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00003595 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3596 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003597 MemOps.push_back(Store);
3598 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003599 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00003600 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003601 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003602 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003603
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003604 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003605 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003606
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003607 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003608}
3609
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003610/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003611/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00003612static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003613 unsigned ParamSize) {
3614
Dale Johannesen86dcae12009-11-24 01:09:07 +00003615 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003616
3617 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
3618 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3619 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3620 // Remember only if the new adjustement is bigger.
3621 if (SPDiff < FI->getTailCallSPDelta())
3622 FI->setTailCallSPDelta(SPDiff);
3623
3624 return SPDiff;
3625}
3626
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003627/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3628/// for tail call optimization. Targets which want to do tail call
3629/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003630bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003631PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003632 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003633 bool isVarArg,
3634 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003635 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003636 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00003637 return false;
3638
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003639 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003640 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003641 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003642
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003643 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00003644 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003645 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
3646 // Functions containing by val parameters are not supported.
3647 for (unsigned i = 0; i != Ins.size(); i++) {
3648 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3649 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003650 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003651
Alp Tokerf907b892013-12-05 05:44:44 +00003652 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003653 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
3654 return true;
3655
3656 // At the moment we can only do local tail calls (in same module, hidden
3657 // or protected) if we are generating PIC.
3658 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
3659 return G->getGlobal()->hasHiddenVisibility()
3660 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003661 }
3662
3663 return false;
3664}
3665
Chris Lattnereb755fc2006-05-17 19:00:46 +00003666/// isCallCompatibleAddress - Return the immediate to use if the specified
3667/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003668static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00003669 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003670 if (!C) return nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003671
Dan Gohmaneffb8942008-09-12 16:56:44 +00003672 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003673 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00003674 SignExtend32<26>(Addr) != Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +00003675 return nullptr; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003676
Dan Gohmaneffb8942008-09-12 16:56:44 +00003677 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00003678 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003679}
3680
Dan Gohmand78c4002008-05-13 00:00:25 +00003681namespace {
3682
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003683struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003684 SDValue Arg;
3685 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003686 int FrameIdx;
3687
3688 TailCallArgumentInfo() : FrameIdx(0) {}
3689};
3690
Dan Gohmand78c4002008-05-13 00:00:25 +00003691}
3692
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003693/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3694static void
3695StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003696 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003697 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3698 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003699 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003700 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003701 SDValue Arg = TailCallArgs[i].Arg;
3702 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003703 int FI = TailCallArgs[i].FrameIdx;
3704 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003705 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003706 MachinePointerInfo::getFixedStack(FI),
3707 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003708 }
3709}
3710
3711/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3712/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003713static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003714 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003715 SDValue Chain,
3716 SDValue OldRetAddr,
3717 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003718 int SPDiff,
3719 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003720 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003721 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003722 if (SPDiff) {
3723 // Calculate the new stack slot for the return address.
3724 int SlotSize = isPPC64 ? 8 : 4;
Eric Christopherdc3a8a42015-02-13 00:39:38 +00003725 const PPCFrameLowering *FL =
3726 MF.getSubtarget<PPCSubtarget>().getFrameLowering();
3727 int NewRetAddrLoc = SPDiff + FL->getReturnSaveOffset();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003728 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003729 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003730 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003731 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003732 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003733 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003734 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003735
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003736 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3737 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003738 if (isDarwinABI) {
Eric Christopherdc3a8a42015-02-13 00:39:38 +00003739 int NewFPLoc = SPDiff + FL->getFramePointerSaveOffset();
David Greene1fbe0542009-11-12 20:49:22 +00003740 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003741 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003742 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3743 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003744 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003745 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003746 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003747 }
3748 return Chain;
3749}
3750
3751/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3752/// the position of the argument.
3753static void
3754CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003755 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003756 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003757 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003758 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003759 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003760 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003761 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003762 TailCallArgumentInfo Info;
3763 Info.Arg = Arg;
3764 Info.FrameIdxOp = FIN;
3765 Info.FrameIdx = FI;
3766 TailCallArguments.push_back(Info);
3767}
3768
3769/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3770/// stack slot. Returns the chain as result and the loaded frame pointers in
3771/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003772SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003773 int SPDiff,
3774 SDValue Chain,
3775 SDValue &LROpOut,
3776 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003777 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003778 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003779 if (SPDiff) {
3780 // Load the LR and FP stack slot for later adjusting.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003781 EVT VT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003782 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003783 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003784 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003785 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003786
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003787 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3788 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003789 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003790 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003791 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003792 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003793 Chain = SDValue(FPOpOut.getNode(), 1);
3794 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003795 }
3796 return Chain;
3797}
3798
Dale Johannesen85d41a12008-03-04 23:17:14 +00003799/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003800/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003801/// specified by the specific parameter attribute. The copy will be passed as
3802/// a byval function parameter.
3803/// Sometimes what we are copying is the end of a larger object, the part that
3804/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003805static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003806CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003807 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003808 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003809 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003810 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003811 false, false, false, MachinePointerInfo(),
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003812 MachinePointerInfo());
Dale Johannesen85d41a12008-03-04 23:17:14 +00003813}
Chris Lattner43df5b32007-02-25 05:34:32 +00003814
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003815/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3816/// tail calls.
3817static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003818LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3819 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003820 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003821 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3822 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003823 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003824 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003825 if (!isTailCall) {
3826 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003827 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003828 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003829 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003830 else
Owen Anderson9f944592009-08-11 20:47:22 +00003831 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003832 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003833 DAG.getConstant(ArgOffset, PtrVT));
3834 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003835 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3836 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003837 // Calculate and remember argument location.
3838 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3839 TailCallArguments);
3840}
3841
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003842static
3843void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003844 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003845 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003846 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003847 MachineFunction &MF = DAG.getMachineFunction();
3848
3849 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3850 // might overwrite each other in case of tail call optimization.
3851 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003852 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003853 InFlag = SDValue();
3854 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3855 MemOpChains2, dl);
3856 if (!MemOpChains2.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003857 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003858
3859 // Store the return address to the appropriate stack slot.
3860 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3861 isPPC64, isDarwinABI, dl);
3862
3863 // Emit callseq_end just before tailcall node.
3864 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003865 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003866 InFlag = Chain.getValue(1);
3867}
3868
Hal Finkel87deb0b2015-01-12 04:34:47 +00003869// Is this global address that of a function that can be called by name? (as
3870// opposed to something that must hold a descriptor for an indirect call).
3871static bool isFunctionGlobalAddress(SDValue Callee) {
3872 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3873 if (Callee.getOpcode() == ISD::GlobalTLSAddress ||
3874 Callee.getOpcode() == ISD::TargetGlobalTLSAddress)
3875 return false;
3876
3877 return G->getGlobal()->getType()->getElementType()->isFunctionTy();
3878 }
3879
3880 return false;
3881}
3882
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003883static
3884unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Hal Finkele2ab0f12015-01-15 21:17:34 +00003885 SDValue &Chain, SDValue CallSeqStart, SDLoc dl, int SPDiff,
3886 bool isTailCall, bool IsPatchPoint,
Craig Topperb94011f2013-07-14 04:42:23 +00003887 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3888 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Hal Finkele2ab0f12015-01-15 21:17:34 +00003889 ImmutableCallSite *CS, const PPCSubtarget &Subtarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003890
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003891 bool isPPC64 = Subtarget.isPPC64();
3892 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003893 bool isELFv2ABI = Subtarget.isELFv2ABI();
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003894
Owen Anderson53aa7a92009-08-10 22:56:29 +00003895 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003896 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003897 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003898
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003899 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003900
Torok Edwin31e90d22010-08-04 20:47:44 +00003901 bool needIndirectCall = true;
Ulrich Weigand9aa09ef2014-06-18 16:14:04 +00003902 if (!isSVR4ABI || !isPPC64)
3903 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
3904 // If this is an absolute destination address, use the munged value.
3905 Callee = SDValue(Dest, 0);
3906 needIndirectCall = false;
3907 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003908
Hal Finkel87deb0b2015-01-12 04:34:47 +00003909 if (isFunctionGlobalAddress(Callee)) {
3910 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Callee);
3911 // A call to a TLS address is actually an indirect call to a
3912 // thread-specific pointer.
Eric Christopher79cc1e32014-09-02 22:28:02 +00003913 unsigned OpFlags = 0;
3914 if ((DAG.getTarget().getRelocationModel() != Reloc::Static &&
3915 (Subtarget.getTargetTriple().isMacOSX() &&
3916 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
3917 (G->getGlobal()->isDeclaration() ||
3918 G->getGlobal()->isWeakForLinker())) ||
3919 (Subtarget.isTargetELF() && !isPPC64 &&
3920 !G->getGlobal()->hasLocalLinkage() &&
3921 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
3922 // PC-relative references to external symbols should go through $stub,
3923 // unless we're building with the leopard linker or later, which
3924 // automatically synthesizes these stubs.
3925 OpFlags = PPCII::MO_PLT_OR_STUB;
Eric Christopherb9fd9ed2014-08-07 22:02:54 +00003926 }
Eric Christopher79cc1e32014-09-02 22:28:02 +00003927
3928 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3929 // every direct call is) turn it into a TargetGlobalAddress /
3930 // TargetExternalSymbol node so that legalize doesn't hack it.
3931 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
3932 Callee.getValueType(), 0, OpFlags);
3933 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003934 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003935
Torok Edwin31e90d22010-08-04 20:47:44 +00003936 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003937 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003938
Hal Finkel3ee2af72014-07-18 23:29:49 +00003939 if ((DAG.getTarget().getRelocationModel() != Reloc::Static &&
3940 (Subtarget.getTargetTriple().isMacOSX() &&
3941 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5))) ||
3942 (Subtarget.isTargetELF() && !isPPC64 &&
Justin Hibbits17744c12015-01-10 07:50:31 +00003943 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003944 // PC-relative references to external symbols should go through $stub,
3945 // unless we're building with the leopard linker or later, which
3946 // automatically synthesizes these stubs.
Hal Finkel3ee2af72014-07-18 23:29:49 +00003947 OpFlags = PPCII::MO_PLT_OR_STUB;
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003948 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003949
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003950 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3951 OpFlags);
3952 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003953 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003954
Hal Finkel934361a2015-01-14 01:07:51 +00003955 if (IsPatchPoint) {
3956 // We'll form an invalid direct call when lowering a patchpoint; the full
3957 // sequence for an indirect call is complicated, and many of the
3958 // instructions introduced might have side effects (and, thus, can't be
3959 // removed later). The call itself will be removed as soon as the
3960 // argument/return lowering is complete, so the fact that it has the wrong
3961 // kind of operands should not really matter.
3962 needIndirectCall = false;
3963 }
3964
Torok Edwin31e90d22010-08-04 20:47:44 +00003965 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003966 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3967 // to do the call, we can't use PPCISD::CALL.
3968 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003969
Hal Finkel63fb9282015-01-13 18:25:05 +00003970 if (isSVR4ABI && isPPC64 && !isELFv2ABI) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003971 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3972 // entry point, but to the function descriptor (the function entry point
3973 // address is part of the function descriptor though).
3974 // The function descriptor is a three doubleword structure with the
3975 // following fields: function entry point, TOC base address and
3976 // environment pointer.
3977 // Thus for a call through a function pointer, the following actions need
3978 // to be performed:
3979 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003980 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003981 // 2. Load the address of the function entry point from the function
3982 // descriptor.
3983 // 3. Load the TOC of the callee from the function descriptor into r2.
3984 // 4. Load the environment pointer from the function descriptor into
3985 // r11.
3986 // 5. Branch to the function entry point address.
3987 // 6. On return of the callee, the TOC of the caller needs to be
3988 // restored (this is done in FinishCall()).
3989 //
Hal Finkele2ab0f12015-01-15 21:17:34 +00003990 // The loads are scheduled at the beginning of the call sequence, and the
3991 // register copies are flagged together to ensure that no other
Tilmann Scheller79fef932009-12-18 13:00:15 +00003992 // operations can be scheduled in between. E.g. without flagging the
Hal Finkele2ab0f12015-01-15 21:17:34 +00003993 // copies together, a TOC access in the caller could be scheduled between
3994 // the assignment of the callee TOC and the branch to the callee, which
Tilmann Scheller79fef932009-12-18 13:00:15 +00003995 // results in the TOC access going through the TOC of the callee instead
3996 // of going through the TOC of the caller, which leads to incorrect code.
3997
3998 // Load the address of the function entry point from the function
3999 // descriptor.
Hal Finkele2ab0f12015-01-15 21:17:34 +00004000 SDValue LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-1);
4001 if (LDChain.getValueType() == MVT::Glue)
4002 LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-2);
4003
4004 bool LoadsInv = Subtarget.hasInvariantFunctionDescriptors();
4005
4006 MachinePointerInfo MPI(CS ? CS->getCalledValue() : nullptr);
4007 SDValue LoadFuncPtr = DAG.getLoad(MVT::i64, dl, LDChain, Callee, MPI,
4008 false, false, LoadsInv, 8);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004009
4010 // Load environment pointer into r11.
Tilmann Scheller79fef932009-12-18 13:00:15 +00004011 SDValue PtrOff = DAG.getIntPtrConstant(16);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004012 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004013 SDValue LoadEnvPtr = DAG.getLoad(MVT::i64, dl, LDChain, AddPtr,
4014 MPI.getWithOffset(16), false, false,
4015 LoadsInv, 8);
4016
4017 SDValue TOCOff = DAG.getIntPtrConstant(8);
4018 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, TOCOff);
4019 SDValue TOCPtr = DAG.getLoad(MVT::i64, dl, LDChain, AddTOC,
4020 MPI.getWithOffset(8), false, false,
4021 LoadsInv, 8);
4022
Hal Finkele6698d52015-02-01 15:03:28 +00004023 setUsesTOCBasePtr(DAG);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004024 SDValue TOCVal = DAG.getCopyToReg(Chain, dl, PPC::X2, TOCPtr,
4025 InFlag);
4026 Chain = TOCVal.getValue(0);
4027 InFlag = TOCVal.getValue(1);
Tilmann Scheller79fef932009-12-18 13:00:15 +00004028
4029 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
4030 InFlag);
Hal Finkele2ab0f12015-01-15 21:17:34 +00004031
Tilmann Scheller79fef932009-12-18 13:00:15 +00004032 Chain = EnvVal.getValue(0);
4033 InFlag = EnvVal.getValue(1);
4034
Tilmann Scheller79fef932009-12-18 13:00:15 +00004035 MTCTROps[0] = Chain;
4036 MTCTROps[1] = LoadFuncPtr;
4037 MTCTROps[2] = InFlag;
4038 }
4039
Hal Finkel63fb9282015-01-13 18:25:05 +00004040 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys,
4041 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
4042 InFlag = Chain.getValue(1);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004043
4044 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00004045 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004046 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004047 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004048 CallOpc = PPCISD::BCTRL;
Craig Topper062a2ba2014-04-25 05:30:21 +00004049 Callee.setNode(nullptr);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004050 // Add use of X11 (holding environment pointer)
Hal Finkel63fb9282015-01-13 18:25:05 +00004051 if (isSVR4ABI && isPPC64 && !isELFv2ABI)
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004052 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004053 // Add CTR register as callee so a bctr can be emitted later.
4054 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00004055 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004056 }
4057
4058 // If this is a direct call, pass the chain and the callee.
4059 if (Callee.getNode()) {
4060 Ops.push_back(Chain);
4061 Ops.push_back(Callee);
4062 }
4063 // If this is a tail call add stack pointer delta.
4064 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00004065 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004066
4067 // Add argument registers to the end of the list so that they are known live
4068 // into the call.
4069 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
4070 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
4071 RegsToPass[i].second.getValueType()));
4072
Hal Finkelaf519932015-01-19 07:20:27 +00004073 // All calls, in both the ELF V1 and V2 ABIs, need the TOC register live
4074 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00004075 if (isSVR4ABI && isPPC64 && !IsPatchPoint) {
4076 setUsesTOCBasePtr(DAG);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004077 Ops.push_back(DAG.getRegister(PPC::X2, PtrVT));
Hal Finkele6698d52015-02-01 15:03:28 +00004078 }
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004079
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004080 return CallOpc;
4081}
4082
Roman Divacky76293062012-09-18 16:47:58 +00004083static
4084bool isLocalCall(const SDValue &Callee)
4085{
4086 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00004087 return !G->getGlobal()->isDeclaration() &&
4088 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00004089 return false;
4090}
4091
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004092SDValue
4093PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004094 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004095 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004096 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004097 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004098
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004099 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00004100 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
4101 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004102 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004103
4104 // Copy all of the result registers out of their specified physreg.
4105 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
4106 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004107 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004108
4109 SDValue Val = DAG.getCopyFromReg(Chain, dl,
4110 VA.getLocReg(), VA.getLocVT(), InFlag);
4111 Chain = Val.getValue(1);
4112 InFlag = Val.getValue(2);
4113
4114 switch (VA.getLocInfo()) {
4115 default: llvm_unreachable("Unknown loc info!");
4116 case CCValAssign::Full: break;
4117 case CCValAssign::AExt:
4118 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4119 break;
4120 case CCValAssign::ZExt:
4121 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
4122 DAG.getValueType(VA.getValVT()));
4123 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4124 break;
4125 case CCValAssign::SExt:
4126 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
4127 DAG.getValueType(VA.getValVT()));
4128 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
4129 break;
4130 }
4131
4132 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004133 }
4134
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004135 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004136}
4137
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004138SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004139PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Hal Finkel934361a2015-01-14 01:07:51 +00004140 bool isTailCall, bool isVarArg, bool IsPatchPoint,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004141 SelectionDAG &DAG,
4142 SmallVector<std::pair<unsigned, SDValue>, 8>
4143 &RegsToPass,
4144 SDValue InFlag, SDValue Chain,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004145 SDValue CallSeqStart, SDValue &Callee,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004146 int SPDiff, unsigned NumBytes,
4147 const SmallVectorImpl<ISD::InputArg> &Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004148 SmallVectorImpl<SDValue> &InVals,
4149 ImmutableCallSite *CS) const {
Ulrich Weigand8658f172014-07-20 23:43:15 +00004150
Owen Anderson53aa7a92009-08-10 22:56:29 +00004151 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004152 SmallVector<SDValue, 8> Ops;
Hal Finkele2ab0f12015-01-15 21:17:34 +00004153 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, CallSeqStart, dl,
4154 SPDiff, isTailCall, IsPatchPoint, RegsToPass,
4155 Ops, NodeTys, CS, Subtarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004156
Hal Finkel5ab37802012-08-28 02:10:27 +00004157 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004158 if (isVarArg && Subtarget.isSVR4ABI() && !Subtarget.isPPC64())
Hal Finkel5ab37802012-08-28 02:10:27 +00004159 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
4160
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004161 // When performing tail call optimization the callee pops its arguments off
4162 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00004163 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004164 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004165 (CallConv == CallingConv::Fast &&
4166 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004167
Roman Divackyef21be22012-03-06 16:41:49 +00004168 // Add a register mask operand representing the call-preserved registers.
Eric Christophercccae792015-01-30 22:02:31 +00004169 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00004170 const uint32_t *Mask =
4171 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv);
Roman Divackyef21be22012-03-06 16:41:49 +00004172 assert(Mask && "Missing call preserved mask for calling convention");
4173 Ops.push_back(DAG.getRegisterMask(Mask));
4174
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004175 if (InFlag.getNode())
4176 Ops.push_back(InFlag);
4177
4178 // Emit tail call.
4179 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004180 assert(((Callee.getOpcode() == ISD::Register &&
4181 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
4182 Callee.getOpcode() == ISD::TargetExternalSymbol ||
4183 Callee.getOpcode() == ISD::TargetGlobalAddress ||
4184 isa<ConstantSDNode>(Callee)) &&
4185 "Expecting an global address, external symbol, absolute value or register");
4186
Craig Topper48d114b2014-04-26 18:35:24 +00004187 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004188 }
4189
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004190 // Add a NOP immediately after the branch instruction when using the 64-bit
4191 // SVR4 ABI. At link time, if caller and callee are in a different module and
4192 // thus have a different TOC, the call will be replaced with a call to a stub
4193 // function which saves the current TOC, loads the TOC of the callee and
4194 // branches to the callee. The NOP will be replaced with a load instruction
4195 // which restores the TOC of the caller from the TOC save slot of the current
4196 // stack frame. If caller and callee belong to the same module (and have the
4197 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00004198
Hal Finkel934361a2015-01-14 01:07:51 +00004199 if (!isTailCall && Subtarget.isSVR4ABI()&& Subtarget.isPPC64() &&
4200 !IsPatchPoint) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004201 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00004202 // This is a call through a function pointer.
4203 // Restore the caller TOC from the save area into R2.
4204 // See PrepareCall() for more information about calls through function
4205 // pointers in the 64-bit SVR4 ABI.
4206 // We are using a target-specific load with r2 hard coded, because the
4207 // result of a target-independent load would never go directly into r2,
4208 // since r2 is a reserved register (which prevents the register allocator
4209 // from allocating it), resulting in an additional register being
4210 // allocated and an unnecessary move instruction being generated.
Hal Finkelfc096c92014-12-23 22:29:40 +00004211 CallOpc = PPCISD::BCTRL_LOAD_TOC;
4212
4213 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
4214 SDValue StackPtr = DAG.getRegister(PPC::X1, PtrVT);
Eric Christopher736d39e2015-02-13 00:39:36 +00004215 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
Hal Finkelfc096c92014-12-23 22:29:40 +00004216 SDValue TOCOff = DAG.getIntPtrConstant(TOCSaveOffset);
4217 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, StackPtr, TOCOff);
4218
4219 // The address needs to go after the chain input but before the flag (or
4220 // any other variadic arguments).
4221 Ops.insert(std::next(Ops.begin()), AddTOC);
Bill Schmidtcea15962013-09-26 17:09:28 +00004222 } else if ((CallOpc == PPCISD::CALL) &&
4223 (!isLocalCall(Callee) ||
Bill Schmidt82f1c772015-02-10 19:09:05 +00004224 DAG.getTarget().getRelocationModel() == Reloc::PIC_))
Roman Divacky76293062012-09-18 16:47:58 +00004225 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00004226 CallOpc = PPCISD::CALL_NOP;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004227 }
4228
Craig Topper48d114b2014-04-26 18:35:24 +00004229 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Hal Finkel51861b42012-03-31 14:45:15 +00004230 InFlag = Chain.getValue(1);
4231
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004232 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
4233 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00004234 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004235 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004236 InFlag = Chain.getValue(1);
4237
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004238 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
4239 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004240}
4241
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004242SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00004243PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004244 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00004245 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00004246 SDLoc &dl = CLI.DL;
4247 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
4248 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
4249 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00004250 SDValue Chain = CLI.Chain;
4251 SDValue Callee = CLI.Callee;
4252 bool &isTailCall = CLI.IsTailCall;
4253 CallingConv::ID CallConv = CLI.CallConv;
4254 bool isVarArg = CLI.IsVarArg;
Hal Finkel934361a2015-01-14 01:07:51 +00004255 bool IsPatchPoint = CLI.IsPatchPoint;
Hal Finkele2ab0f12015-01-15 21:17:34 +00004256 ImmutableCallSite *CS = CLI.CS;
Justin Holewinskiaa583972012-05-25 16:35:28 +00004257
Evan Cheng67a69dd2010-01-27 00:07:07 +00004258 if (isTailCall)
4259 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
4260 Ins, DAG);
4261
Hal Finkele2ab0f12015-01-15 21:17:34 +00004262 if (!isTailCall && CS && CS->isMustTailCall())
Reid Kleckner5772b772014-04-24 20:14:34 +00004263 report_fatal_error("failed to perform tail call elimination on a call "
4264 "site marked musttail");
4265
Eric Christopherb1aaebe2014-06-12 22:38:18 +00004266 if (Subtarget.isSVR4ABI()) {
4267 if (Subtarget.isPPC64())
Bill Schmidt57d6de52012-10-23 15:51:16 +00004268 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004269 isTailCall, IsPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004270 dl, DAG, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004271 else
4272 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004273 isTailCall, IsPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004274 dl, DAG, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004275 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004276
Bill Schmidt57d6de52012-10-23 15:51:16 +00004277 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004278 isTailCall, IsPatchPoint, Outs, OutVals, Ins,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004279 dl, DAG, InVals, CS);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004280}
4281
4282SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004283PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
4284 CallingConv::ID CallConv, bool isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004285 bool isTailCall, bool IsPatchPoint,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004286 const SmallVectorImpl<ISD::OutputArg> &Outs,
4287 const SmallVectorImpl<SDValue> &OutVals,
4288 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004289 SDLoc dl, SelectionDAG &DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004290 SmallVectorImpl<SDValue> &InVals,
4291 ImmutableCallSite *CS) const {
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004292 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00004293 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004294
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004295 assert((CallConv == CallingConv::C ||
4296 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004297
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004298 unsigned PtrByteSize = 4;
4299
4300 MachineFunction &MF = DAG.getMachineFunction();
4301
4302 // Mark this function as potentially containing a function that contains a
4303 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4304 // and restoring the callers stack pointer in this functions epilog. This is
4305 // done because by tail calling the called function might overwrite the value
4306 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004307 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4308 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004309 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00004310
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004311 // Count how many bytes are to be pushed on the stack, including the linkage
4312 // area, parameter list area and the part of the local variable space which
4313 // contains copies of aggregates which are passed by value.
4314
4315 // Assign locations to all of the outgoing arguments.
4316 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00004317 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
4318 *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004319
4320 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00004321 CCInfo.AllocateStack(Subtarget.getFrameLowering()->getLinkageSize(),
Ulrich Weigand8658f172014-07-20 23:43:15 +00004322 PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004323
4324 if (isVarArg) {
4325 // Handle fixed and variable vector arguments differently.
4326 // Fixed vector arguments go into registers as long as registers are
4327 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004328 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00004329
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004330 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00004331 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004332 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004333 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00004334
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004335 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00004336 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
4337 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004338 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00004339 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
4340 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004341 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004342
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004343 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00004344#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00004345 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00004346 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00004347#endif
Craig Toppere73658d2014-04-28 04:05:08 +00004348 llvm_unreachable(nullptr);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004349 }
4350 }
4351 } else {
4352 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00004353 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004354 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004355
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004356 // Assign locations to all of the outgoing aggregate by value arguments.
4357 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004358 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Eric Christopherb5217502014-08-06 18:45:26 +00004359 ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004360
4361 // Reserve stack space for the allocations in CCInfo.
4362 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
4363
Bill Schmidtef17c142013-02-06 17:33:58 +00004364 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004365
4366 // Size of the linkage area, parameter list area and the part of the local
4367 // space variable where copies of aggregates which are passed by value are
4368 // stored.
4369 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004370
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004371 // Calculate by how many bytes the stack has to be adjusted in case of tail
4372 // call optimization.
4373 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4374
4375 // Adjust the stack pointer for the new arguments...
4376 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004377 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4378 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004379 SDValue CallSeqStart = Chain;
4380
4381 // Load the return address and frame pointer so it can be moved somewhere else
4382 // later.
4383 SDValue LROp, FPOp;
4384 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
4385 dl);
4386
4387 // Set up a copy of the stack pointer for use loading and storing any
4388 // arguments that may not fit in the registers available for argument
4389 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00004390 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00004391
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004392 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4393 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4394 SmallVector<SDValue, 8> MemOpChains;
4395
Roman Divacky71038e72011-08-30 17:04:16 +00004396 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004397 // Walk the register/memloc assignments, inserting copies/loads.
4398 for (unsigned i = 0, j = 0, e = ArgLocs.size();
4399 i != e;
4400 ++i) {
4401 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004402 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004403 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00004404
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004405 if (Flags.isByVal()) {
4406 // Argument is an aggregate which is passed by value, thus we need to
4407 // create a copy of it in the local variable space of the current stack
4408 // frame (which is the stack frame of the caller) and pass the address of
4409 // this copy to the callee.
4410 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
4411 CCValAssign &ByValVA = ByValArgLocs[j++];
4412 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00004413
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004414 // Memory reserved in the local variable space of the callers stack frame.
4415 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004416
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004417 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
4418 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00004419
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004420 // Create a copy of the argument in the local area of the current
4421 // stack frame.
4422 SDValue MemcpyCall =
4423 CreateCopyOfByValArgument(Arg, PtrOff,
4424 CallSeqStart.getNode()->getOperand(0),
4425 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00004426
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004427 // This must go outside the CALLSEQ_START..END.
4428 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004429 CallSeqStart.getNode()->getOperand(1),
4430 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004431 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4432 NewCallSeqStart.getNode());
4433 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00004434
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004435 // Pass the address of the aggregate copy on the stack either in a
4436 // physical register or in the parameter list area of the current stack
4437 // frame to the callee.
4438 Arg = PtrOff;
4439 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004440
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004441 if (VA.isRegLoc()) {
Hal Finkel2a9d3182014-03-06 00:23:33 +00004442 if (Arg.getValueType() == MVT::i1)
4443 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg);
4444
Roman Divacky71038e72011-08-30 17:04:16 +00004445 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004446 // Put argument in a physical register.
4447 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
4448 } else {
4449 // Put argument in the parameter list area of the current stack frame.
4450 assert(VA.isMemLoc());
4451 unsigned LocMemOffset = VA.getLocMemOffset();
4452
4453 if (!isTailCall) {
4454 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
4455 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
4456
4457 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00004458 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004459 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004460 } else {
4461 // Calculate and remember argument location.
4462 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
4463 TailCallArguments);
4464 }
4465 }
4466 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004467
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004468 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004469 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Wesley Peck527da1b2010-11-23 03:31:01 +00004470
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004471 // Build a sequence of copy-to-reg nodes chained together with token chain
4472 // and flag operands which copy the outgoing args into the appropriate regs.
4473 SDValue InFlag;
4474 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4475 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4476 RegsToPass[i].second, InFlag);
4477 InFlag = Chain.getValue(1);
4478 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004479
Hal Finkel5ab37802012-08-28 02:10:27 +00004480 // Set CR bit 6 to true if this is a vararg call with floating args passed in
4481 // registers.
4482 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004483 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
4484 SDValue Ops[] = { Chain, InFlag };
4485
Hal Finkel5ab37802012-08-28 02:10:27 +00004486 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00004487 dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1));
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004488
Hal Finkel5ab37802012-08-28 02:10:27 +00004489 InFlag = Chain.getValue(1);
4490 }
4491
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004492 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004493 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
4494 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004495
Hal Finkel934361a2015-01-14 01:07:51 +00004496 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint, DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004497 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff,
4498 NumBytes, Ins, InVals, CS);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004499}
4500
Bill Schmidt57d6de52012-10-23 15:51:16 +00004501// Copy an argument into memory, being careful to do this outside the
4502// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004503SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00004504PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
4505 SDValue CallSeqStart,
4506 ISD::ArgFlagsTy Flags,
4507 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004508 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004509 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
4510 CallSeqStart.getNode()->getOperand(0),
4511 Flags, DAG, dl);
4512 // The MEMCPY must go outside the CALLSEQ_START..END.
4513 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004514 CallSeqStart.getNode()->getOperand(1),
4515 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004516 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4517 NewCallSeqStart.getNode());
4518 return NewCallSeqStart;
4519}
4520
4521SDValue
4522PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004523 CallingConv::ID CallConv, bool isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00004524 bool isTailCall, bool IsPatchPoint,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004525 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004526 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004527 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004528 SDLoc dl, SelectionDAG &DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00004529 SmallVectorImpl<SDValue> &InVals,
4530 ImmutableCallSite *CS) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004531
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004532 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004533 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004534 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004535
Bill Schmidt57d6de52012-10-23 15:51:16 +00004536 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
4537 unsigned PtrByteSize = 8;
4538
4539 MachineFunction &MF = DAG.getMachineFunction();
4540
4541 // Mark this function as potentially containing a function that contains a
4542 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4543 // and restoring the callers stack pointer in this functions epilog. This is
4544 // done because by tail calling the called function might overwrite the value
4545 // in this function's (MF) stack pointer stack slot 0(SP).
4546 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4547 CallConv == CallingConv::Fast)
4548 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4549
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004550 assert(!(CallConv == CallingConv::Fast && isVarArg) &&
4551 "fastcc not supported on varargs functions");
4552
Bill Schmidt57d6de52012-10-23 15:51:16 +00004553 // Count how many bytes are to be pushed on the stack, including the linkage
Ulrich Weigand8658f172014-07-20 23:43:15 +00004554 // area, and parameter passing area. On ELFv1, the linkage area is 48 bytes
4555 // reserved space for [SP][CR][LR][2 x unused][TOC]; on ELFv2, the linkage
4556 // area is 32 bytes reserved space for [SP][CR][LR][TOC].
Eric Christophera4ae2132015-02-13 22:22:57 +00004557 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004558 unsigned NumBytes = LinkageSize;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004559 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Hal Finkelc93a9a22015-02-25 01:06:45 +00004560 unsigned &QFPR_idx = FPR_idx;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004561
4562 static const MCPhysReg GPR[] = {
4563 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4564 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4565 };
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004566 static const MCPhysReg VR[] = {
4567 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4568 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4569 };
4570 static const MCPhysReg VSRH[] = {
4571 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
4572 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
4573 };
4574
4575 const unsigned NumGPRs = array_lengthof(GPR);
4576 const unsigned NumFPRs = 13;
4577 const unsigned NumVRs = array_lengthof(VR);
Hal Finkelc93a9a22015-02-25 01:06:45 +00004578 const unsigned NumQFPRs = NumFPRs;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004579
4580 // When using the fast calling convention, we don't provide backing for
4581 // arguments that will be in registers.
4582 unsigned NumGPRsUsed = 0, NumFPRsUsed = 0, NumVRsUsed = 0;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004583
4584 // Add up all the space actually used.
4585 for (unsigned i = 0; i != NumOps; ++i) {
4586 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4587 EVT ArgVT = Outs[i].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004588 EVT OrigVT = Outs[i].ArgVT;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004589
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004590 if (CallConv == CallingConv::Fast) {
4591 if (Flags.isByVal())
4592 NumGPRsUsed += (Flags.getByValSize()+7)/8;
4593 else
4594 switch (ArgVT.getSimpleVT().SimpleTy) {
4595 default: llvm_unreachable("Unexpected ValueType for argument!");
4596 case MVT::i1:
4597 case MVT::i32:
4598 case MVT::i64:
4599 if (++NumGPRsUsed <= NumGPRs)
4600 continue;
4601 break;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004602 case MVT::v4i32:
4603 case MVT::v8i16:
4604 case MVT::v16i8:
4605 case MVT::v2f64:
4606 case MVT::v2i64:
4607 if (++NumVRsUsed <= NumVRs)
4608 continue;
4609 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00004610 case MVT::v4f32:
4611 // When using QPX, this is handled like a FP register, otherwise, it
4612 // is an Altivec register.
4613 if (Subtarget.hasQPX()) {
4614 if (++NumFPRsUsed <= NumFPRs)
4615 continue;
4616 } else {
4617 if (++NumVRsUsed <= NumVRs)
4618 continue;
4619 }
4620 break;
4621 case MVT::f32:
4622 case MVT::f64:
4623 case MVT::v4f64: // QPX
4624 case MVT::v4i1: // QPX
4625 if (++NumFPRsUsed <= NumFPRs)
4626 continue;
4627 break;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004628 }
4629 }
4630
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004631 /* Respect alignment of argument on the stack. */
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004632 unsigned Align =
4633 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004634 NumBytes = ((NumBytes + Align - 1) / Align) * Align;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004635
4636 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004637 if (Flags.isInConsecutiveRegsLast())
4638 NumBytes = ((NumBytes + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004639 }
4640
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004641 unsigned NumBytesActuallyUsed = NumBytes;
4642
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004643 // The prolog code of the callee may store up to 8 GPR argument registers to
4644 // the stack, allowing va_start to index over them in memory if its varargs.
4645 // Because we cannot tell if this is needed on the caller side, we have to
4646 // conservatively assume that it is needed. As such, make sure we have at
4647 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00004648 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004649 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004650
4651 // Tail call needs the stack to be aligned.
4652 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4653 CallConv == CallingConv::Fast)
Eric Christophercccae792015-01-30 22:02:31 +00004654 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004655
4656 // Calculate by how many bytes the stack has to be adjusted in case of tail
4657 // call optimization.
4658 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4659
4660 // To protect arguments on the stack from being clobbered in a tail call,
4661 // force all the loads to happen before doing any other lowering.
4662 if (isTailCall)
4663 Chain = DAG.getStackArgumentTokenFactor(Chain);
4664
4665 // Adjust the stack pointer for the new arguments...
4666 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004667 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4668 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004669 SDValue CallSeqStart = Chain;
4670
4671 // Load the return address and frame pointer so it can be move somewhere else
4672 // later.
4673 SDValue LROp, FPOp;
4674 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4675 dl);
4676
4677 // Set up a copy of the stack pointer for use loading and storing any
4678 // arguments that may not fit in the registers available for argument
4679 // passing.
4680 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
4681
4682 // Figure out which arguments are going to go in registers, and which in
4683 // memory. Also, if this is a vararg function, floating point operations
4684 // must be stored to our stack, and loaded into integer regs as well, if
4685 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004686 unsigned ArgOffset = LinkageSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004687
4688 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4689 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4690
4691 SmallVector<SDValue, 8> MemOpChains;
4692 for (unsigned i = 0; i != NumOps; ++i) {
4693 SDValue Arg = OutVals[i];
4694 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004695 EVT ArgVT = Outs[i].VT;
4696 EVT OrigVT = Outs[i].ArgVT;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004697
4698 // PtrOff will be used to store the current argument to the stack if a
4699 // register cannot be found for it.
4700 SDValue PtrOff;
4701
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004702 // We re-align the argument offset for each argument, except when using the
4703 // fast calling convention, when we need to make sure we do that only when
4704 // we'll actually use a stack slot.
4705 auto ComputePtrOff = [&]() {
4706 /* Respect alignment of argument on the stack. */
4707 unsigned Align =
4708 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
4709 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004710
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004711 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
4712
4713 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4714 };
4715
4716 if (CallConv != CallingConv::Fast) {
4717 ComputePtrOff();
4718
4719 /* Compute GPR index associated with argument offset. */
4720 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
4721 GPR_idx = std::min(GPR_idx, NumGPRs);
4722 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004723
4724 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00004725 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004726 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4727 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4728 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
4729 }
4730
4731 // FIXME memcpy is used way more than necessary. Correctness first.
4732 // Note: "by value" is code for passing a structure by value, not
4733 // basic types.
4734 if (Flags.isByVal()) {
4735 // Note: Size includes alignment padding, so
4736 // struct x { short a; char b; }
4737 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
4738 // These are the proper values we need for right-justifying the
4739 // aggregate in a parameter register.
4740 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00004741
4742 // An empty aggregate parameter takes up no storage and no
4743 // registers.
4744 if (Size == 0)
4745 continue;
4746
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004747 if (CallConv == CallingConv::Fast)
4748 ComputePtrOff();
4749
Bill Schmidt57d6de52012-10-23 15:51:16 +00004750 // All aggregates smaller than 8 bytes must be passed right-justified.
4751 if (Size==1 || Size==2 || Size==4) {
4752 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
4753 if (GPR_idx != NumGPRs) {
4754 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
4755 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004756 false, false, false, 0);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004757 MemOpChains.push_back(Load.getValue(1));
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004758 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004759
4760 ArgOffset += PtrByteSize;
4761 continue;
4762 }
4763 }
4764
4765 if (GPR_idx == NumGPRs && Size < 8) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004766 SDValue AddPtr = PtrOff;
4767 if (!isLittleEndian) {
4768 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4769 PtrOff.getValueType());
4770 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4771 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004772 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4773 CallSeqStart,
4774 Flags, DAG, dl);
4775 ArgOffset += PtrByteSize;
4776 continue;
4777 }
4778 // Copy entire object into memory. There are cases where gcc-generated
4779 // code assumes it is there, even if it could be put entirely into
4780 // registers. (This is not what the doc says.)
4781
4782 // FIXME: The above statement is likely due to a misunderstanding of the
4783 // documents. All arguments must be copied into the parameter area BY
4784 // THE CALLEE in the event that the callee takes the address of any
4785 // formal argument. That has not yet been implemented. However, it is
4786 // reasonable to use the stack area as a staging area for the register
4787 // load.
4788
4789 // Skip this for small aggregates, as we will use the same slot for a
4790 // right-justified copy, below.
4791 if (Size >= 8)
4792 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4793 CallSeqStart,
4794 Flags, DAG, dl);
4795
4796 // When a register is available, pass a small aggregate right-justified.
4797 if (Size < 8 && GPR_idx != NumGPRs) {
4798 // The easiest way to get this right-justified in a register
4799 // is to copy the structure into the rightmost portion of a
4800 // local variable slot, then load the whole slot into the
4801 // register.
4802 // FIXME: The memcpy seems to produce pretty awful code for
4803 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00004804 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00004805 // parameter save area instead of a new local variable.
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004806 SDValue AddPtr = PtrOff;
4807 if (!isLittleEndian) {
4808 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
4809 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4810 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004811 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4812 CallSeqStart,
4813 Flags, DAG, dl);
4814
4815 // Load the slot into the register.
4816 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
4817 MachinePointerInfo(),
4818 false, false, false, 0);
4819 MemOpChains.push_back(Load.getValue(1));
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004820 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004821
4822 // Done with this argument.
4823 ArgOffset += PtrByteSize;
4824 continue;
4825 }
4826
4827 // For aggregates larger than PtrByteSize, copy the pieces of the
4828 // object that fit into registers from the parameter save area.
4829 for (unsigned j=0; j<Size; j+=PtrByteSize) {
4830 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
4831 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
4832 if (GPR_idx != NumGPRs) {
4833 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4834 MachinePointerInfo(),
4835 false, false, false, 0);
4836 MemOpChains.push_back(Load.getValue(1));
4837 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4838 ArgOffset += PtrByteSize;
4839 } else {
4840 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4841 break;
4842 }
4843 }
4844 continue;
4845 }
4846
Craig Topper56710102013-08-15 02:33:50 +00004847 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004848 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00004849 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004850 case MVT::i32:
4851 case MVT::i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004852 // These can be scalar arguments or elements of an integer array type
4853 // passed directly. Clang may use those instead of "byval" aggregate
4854 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidt57d6de52012-10-23 15:51:16 +00004855 if (GPR_idx != NumGPRs) {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004856 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004857 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004858 if (CallConv == CallingConv::Fast)
4859 ComputePtrOff();
4860
Bill Schmidt57d6de52012-10-23 15:51:16 +00004861 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4862 true, isTailCall, false, MemOpChains,
4863 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004864 if (CallConv == CallingConv::Fast)
4865 ArgOffset += PtrByteSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004866 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004867 if (CallConv != CallingConv::Fast)
4868 ArgOffset += PtrByteSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004869 break;
4870 case MVT::f32:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004871 case MVT::f64: {
4872 // These can be scalar arguments or elements of a float array type
4873 // passed directly. The latter are used to implement ELFv2 homogenous
4874 // float aggregates.
4875
4876 // Named arguments go into FPRs first, and once they overflow, the
4877 // remaining arguments go into GPRs and then the parameter save area.
4878 // Unnamed arguments for vararg functions always go to GPRs and
4879 // then the parameter save area. For now, put all arguments to vararg
4880 // routines always in both locations (FPR *and* GPR or stack slot).
4881 bool NeedGPROrStack = isVarArg || FPR_idx == NumFPRs;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004882 bool NeededLoad = false;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004883
4884 // First load the argument into the next available FPR.
4885 if (FPR_idx != NumFPRs)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004886 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4887
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004888 // Next, load the argument into GPR or stack slot if needed.
4889 if (!NeedGPROrStack)
4890 ;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004891 else if (GPR_idx != NumGPRs && CallConv != CallingConv::Fast) {
Hal Finkel8ea446b2015-01-18 14:31:10 +00004892 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8
4893 // once we support fp <-> gpr moves.
4894
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004895 // In the non-vararg case, this can only ever happen in the
4896 // presence of f32 array types, since otherwise we never run
4897 // out of FPRs before running out of GPRs.
4898 SDValue ArgVal;
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004899
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004900 // Double values are always passed in a single GPR.
4901 if (Arg.getValueType() != MVT::f32) {
4902 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004903
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004904 // Non-array float values are extended and passed in a GPR.
4905 } else if (!Flags.isInConsecutiveRegs()) {
4906 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4907 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
4908
4909 // If we have an array of floats, we collect every odd element
4910 // together with its predecessor into one GPR.
4911 } else if (ArgOffset % PtrByteSize != 0) {
4912 SDValue Lo, Hi;
4913 Lo = DAG.getNode(ISD::BITCAST, dl, MVT::i32, OutVals[i - 1]);
4914 Hi = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4915 if (!isLittleEndian)
4916 std::swap(Lo, Hi);
4917 ArgVal = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
4918
4919 // The final element, if even, goes into the first half of a GPR.
4920 } else if (Flags.isInConsecutiveRegsLast()) {
4921 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4922 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
4923 if (!isLittleEndian)
4924 ArgVal = DAG.getNode(ISD::SHL, dl, MVT::i64, ArgVal,
4925 DAG.getConstant(32, MVT::i32));
4926
4927 // Non-final even elements are skipped; they will be handled
4928 // together the with subsequent argument on the next go-around.
4929 } else
4930 ArgVal = SDValue();
4931
4932 if (ArgVal.getNode())
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004933 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], ArgVal));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004934 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004935 if (CallConv == CallingConv::Fast)
4936 ComputePtrOff();
4937
Bill Schmidt57d6de52012-10-23 15:51:16 +00004938 // Single-precision floating-point values are mapped to the
4939 // second (rightmost) word of the stack doubleword.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004940 if (Arg.getValueType() == MVT::f32 &&
4941 !isLittleEndian && !Flags.isInConsecutiveRegs()) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004942 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4943 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4944 }
4945
4946 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4947 true, isTailCall, false, MemOpChains,
4948 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004949
4950 NeededLoad = true;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004951 }
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004952 // When passing an array of floats, the array occupies consecutive
4953 // space in the argument area; only round up to the next doubleword
4954 // at the end of the array. Otherwise, each float takes 8 bytes.
Hal Finkelf81b6dd2015-01-18 12:08:47 +00004955 if (CallConv != CallingConv::Fast || NeededLoad) {
4956 ArgOffset += (Arg.getValueType() == MVT::f32 &&
4957 Flags.isInConsecutiveRegs()) ? 4 : 8;
4958 if (Flags.isInConsecutiveRegsLast())
4959 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
4960 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004961 break;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004962 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004963 case MVT::v4f32:
4964 case MVT::v4i32:
4965 case MVT::v8i16:
4966 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00004967 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00004968 case MVT::v2i64:
Hal Finkelc93a9a22015-02-25 01:06:45 +00004969 if (!Subtarget.hasQPX()) {
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004970 // These can be scalar arguments or elements of a vector array type
4971 // passed directly. The latter are used to implement ELFv2 homogenous
4972 // vector aggregates.
4973
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00004974 // For a varargs call, named arguments go into VRs or on the stack as
4975 // usual; unnamed arguments always go to the stack or the corresponding
4976 // GPRs when within range. For now, we always put the value in both
4977 // locations (or even all three).
Bill Schmidt57d6de52012-10-23 15:51:16 +00004978 if (isVarArg) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004979 // We could elide this store in the case where the object fits
4980 // entirely in R registers. Maybe later.
Bill Schmidt57d6de52012-10-23 15:51:16 +00004981 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4982 MachinePointerInfo(), false, false, 0);
4983 MemOpChains.push_back(Store);
4984 if (VR_idx != NumVRs) {
4985 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4986 MachinePointerInfo(),
4987 false, false, false, 0);
4988 MemOpChains.push_back(Load.getValue(1));
Hal Finkel7811c612014-03-28 19:58:11 +00004989
4990 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4991 Arg.getSimpleValueType() == MVT::v2i64) ?
4992 VSRH[VR_idx] : VR[VR_idx];
4993 ++VR_idx;
4994
4995 RegsToPass.push_back(std::make_pair(VReg, Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004996 }
4997 ArgOffset += 16;
4998 for (unsigned i=0; i<16; i+=PtrByteSize) {
4999 if (GPR_idx == NumGPRs)
5000 break;
5001 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
5002 DAG.getConstant(i, PtrVT));
5003 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
5004 false, false, false, 0);
5005 MemOpChains.push_back(Load.getValue(1));
5006 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5007 }
5008 break;
5009 }
5010
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00005011 // Non-varargs Altivec params go into VRs or on the stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00005012 if (VR_idx != NumVRs) {
Hal Finkel7811c612014-03-28 19:58:11 +00005013 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
5014 Arg.getSimpleValueType() == MVT::v2i64) ?
5015 VSRH[VR_idx] : VR[VR_idx];
5016 ++VR_idx;
5017
5018 RegsToPass.push_back(std::make_pair(VReg, Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005019 } else {
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005020 if (CallConv == CallingConv::Fast)
5021 ComputePtrOff();
5022
Bill Schmidt57d6de52012-10-23 15:51:16 +00005023 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5024 true, isTailCall, true, MemOpChains,
5025 TailCallArguments, dl);
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005026 if (CallConv == CallingConv::Fast)
5027 ArgOffset += 16;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005028 }
Hal Finkelf81b6dd2015-01-18 12:08:47 +00005029
5030 if (CallConv != CallingConv::Fast)
5031 ArgOffset += 16;
Bill Schmidt57d6de52012-10-23 15:51:16 +00005032 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +00005033 } // not QPX
5034
5035 assert(Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32 &&
5036 "Invalid QPX parameter type");
5037
5038 /* fall through */
5039 case MVT::v4f64:
5040 case MVT::v4i1: {
5041 bool IsF32 = Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32;
5042 if (isVarArg) {
5043 // We could elide this store in the case where the object fits
5044 // entirely in R registers. Maybe later.
5045 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5046 MachinePointerInfo(), false, false, 0);
5047 MemOpChains.push_back(Store);
5048 if (QFPR_idx != NumQFPRs) {
5049 SDValue Load = DAG.getLoad(IsF32 ? MVT::v4f32 : MVT::v4f64, dl,
5050 Store, PtrOff, MachinePointerInfo(),
5051 false, false, false, 0);
5052 MemOpChains.push_back(Load.getValue(1));
5053 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Load));
5054 }
5055 ArgOffset += (IsF32 ? 16 : 32);
Aaron Ballman70c27de2015-02-25 13:02:23 +00005056 for (unsigned i = 0; i < (IsF32 ? 16U : 32U); i += PtrByteSize) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00005057 if (GPR_idx == NumGPRs)
5058 break;
5059 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
5060 DAG.getConstant(i, PtrVT));
5061 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
5062 false, false, false, 0);
5063 MemOpChains.push_back(Load.getValue(1));
5064 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5065 }
5066 break;
5067 }
5068
5069 // Non-varargs QPX params go into registers or on the stack.
5070 if (QFPR_idx != NumQFPRs) {
5071 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Arg));
5072 } else {
5073 if (CallConv == CallingConv::Fast)
5074 ComputePtrOff();
5075
5076 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5077 true, isTailCall, true, MemOpChains,
5078 TailCallArguments, dl);
5079 if (CallConv == CallingConv::Fast)
5080 ArgOffset += (IsF32 ? 16 : 32);
5081 }
5082
5083 if (CallConv != CallingConv::Fast)
5084 ArgOffset += (IsF32 ? 16 : 32);
5085 break;
5086 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005087 }
5088 }
5089
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005090 assert(NumBytesActuallyUsed == ArgOffset);
Ulrich Weigandde8641b2014-07-07 19:39:44 +00005091 (void)NumBytesActuallyUsed;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00005092
Bill Schmidt57d6de52012-10-23 15:51:16 +00005093 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00005094 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005095
5096 // Check if this is an indirect call (MTCTR/BCTRL).
5097 // See PrepareCall() for more information about calls through function
5098 // pointers in the 64-bit SVR4 ABI.
Hal Finkel934361a2015-01-14 01:07:51 +00005099 if (!isTailCall && !IsPatchPoint &&
Hal Finkel87deb0b2015-01-12 04:34:47 +00005100 !isFunctionGlobalAddress(Callee) &&
5101 !isa<ExternalSymbolSDNode>(Callee)) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005102 // Load r2 into a virtual register and store it to the TOC save area.
Hal Finkele6698d52015-02-01 15:03:28 +00005103 setUsesTOCBasePtr(DAG);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005104 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
5105 // TOC save area offset.
Eric Christopher736d39e2015-02-13 00:39:36 +00005106 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
Ulrich Weigandad0cb912014-06-18 17:52:49 +00005107 SDValue PtrOff = DAG.getIntPtrConstant(TOCSaveOffset);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005108 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Hal Finkele2ab0f12015-01-15 21:17:34 +00005109 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr,
5110 MachinePointerInfo::getStack(TOCSaveOffset),
Bill Schmidt57d6de52012-10-23 15:51:16 +00005111 false, false, 0);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00005112 // In the ELFv2 ABI, R12 must contain the address of an indirect callee.
5113 // This does not mean the MTCTR instruction must use R12; it's easier
5114 // to model this as an extra parameter, so do that.
Hal Finkel934361a2015-01-14 01:07:51 +00005115 if (isELFv2ABI && !IsPatchPoint)
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00005116 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
Bill Schmidt57d6de52012-10-23 15:51:16 +00005117 }
5118
5119 // Build a sequence of copy-to-reg nodes chained together with token chain
5120 // and flag operands which copy the outgoing args into the appropriate regs.
5121 SDValue InFlag;
5122 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
5123 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
5124 RegsToPass[i].second, InFlag);
5125 InFlag = Chain.getValue(1);
5126 }
5127
5128 if (isTailCall)
5129 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
5130 FPOp, true, TailCallArguments);
5131
Hal Finkel934361a2015-01-14 01:07:51 +00005132 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint, DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00005133 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff,
5134 NumBytes, Ins, InVals, CS);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005135}
5136
5137SDValue
5138PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
5139 CallingConv::ID CallConv, bool isVarArg,
Hal Finkel934361a2015-01-14 01:07:51 +00005140 bool isTailCall, bool IsPatchPoint,
Bill Schmidt57d6de52012-10-23 15:51:16 +00005141 const SmallVectorImpl<ISD::OutputArg> &Outs,
5142 const SmallVectorImpl<SDValue> &OutVals,
5143 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005144 SDLoc dl, SelectionDAG &DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00005145 SmallVectorImpl<SDValue> &InVals,
5146 ImmutableCallSite *CS) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00005147
5148 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005149
Owen Anderson53aa7a92009-08-10 22:56:29 +00005150 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00005151 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005152 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005153
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005154 MachineFunction &MF = DAG.getMachineFunction();
5155
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005156 // Mark this function as potentially containing a function that contains a
5157 // tail call. As a consequence the frame pointer will be used for dynamicalloc
5158 // and restoring the callers stack pointer in this functions epilog. This is
5159 // done because by tail calling the called function might overwrite the value
5160 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00005161 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5162 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005163 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
5164
Chris Lattneraa40ec12006-05-16 22:56:08 +00005165 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00005166 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00005167 // prereserved space for [SP][CR][LR][3 x unused].
Eric Christophera4ae2132015-02-13 22:22:57 +00005168 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005169 unsigned NumBytes = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005170
5171 // Add up all the space actually used.
5172 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
5173 // they all go in registers, but we must reserve stack space for them for
5174 // possible use by the caller. In varargs or 64-bit calls, parameters are
5175 // assigned stack space in order, with padding so Altivec parameters are
5176 // 16-byte aligned.
5177 unsigned nAltivecParamsAtEnd = 0;
5178 for (unsigned i = 0; i != NumOps; ++i) {
5179 ISD::ArgFlagsTy Flags = Outs[i].Flags;
5180 EVT ArgVT = Outs[i].VT;
5181 // Varargs Altivec parameters are padded to a 16 byte boundary.
5182 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
5183 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
5184 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64) {
5185 if (!isVarArg && !isPPC64) {
5186 // Non-varargs Altivec parameters go after all the non-Altivec
5187 // parameters; handle those later so we know how much padding we need.
5188 nAltivecParamsAtEnd++;
5189 continue;
5190 }
5191 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
5192 NumBytes = ((NumBytes+15)/16)*16;
5193 }
5194 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
5195 }
5196
5197 // Allow for Altivec parameters at the end, if needed.
5198 if (nAltivecParamsAtEnd) {
5199 NumBytes = ((NumBytes+15)/16)*16;
5200 NumBytes += 16*nAltivecParamsAtEnd;
5201 }
5202
5203 // The prolog code of the callee may store up to 8 GPR argument registers to
5204 // the stack, allowing va_start to index over them in memory if its varargs.
5205 // Because we cannot tell if this is needed on the caller side, we have to
5206 // conservatively assume that it is needed. As such, make sure we have at
5207 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005208 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00005209
5210 // Tail call needs the stack to be aligned.
5211 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5212 CallConv == CallingConv::Fast)
Eric Christophercccae792015-01-30 22:02:31 +00005213 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005214
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005215 // Calculate by how many bytes the stack has to be adjusted in case of tail
5216 // call optimization.
5217 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005218
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005219 // To protect arguments on the stack from being clobbered in a tail call,
5220 // force all the loads to happen before doing any other lowering.
5221 if (isTailCall)
5222 Chain = DAG.getStackArgumentTokenFactor(Chain);
5223
Chris Lattnerb7552a82006-05-17 00:15:40 +00005224 // Adjust the stack pointer for the new arguments...
5225 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00005226 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
5227 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005228 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005229
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005230 // Load the return address and frame pointer so it can be move somewhere else
5231 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005232 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00005233 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
5234 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005235
Chris Lattnerb7552a82006-05-17 00:15:40 +00005236 // Set up a copy of the stack pointer for use loading and storing any
5237 // arguments that may not fit in the registers available for argument
5238 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005239 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005240 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00005241 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005242 else
Owen Anderson9f944592009-08-11 20:47:22 +00005243 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005244
Chris Lattnerb7552a82006-05-17 00:15:40 +00005245 // Figure out which arguments are going to go in registers, and which in
5246 // memory. Also, if this is a vararg function, floating point operations
5247 // must be stored to our stack, and loaded into integer regs as well, if
5248 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00005249 unsigned ArgOffset = LinkageSize;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005250 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005251
Craig Topper840beec2014-04-04 05:16:06 +00005252 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005253 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
5254 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
5255 };
Craig Topper840beec2014-04-04 05:16:06 +00005256 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00005257 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
5258 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
5259 };
Craig Topper840beec2014-04-04 05:16:06 +00005260 static const MCPhysReg VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005261 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
5262 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
5263 };
Owen Andersone2f23a32007-09-07 04:06:50 +00005264 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005265 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00005266 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005267
Craig Topper840beec2014-04-04 05:16:06 +00005268 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00005269
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005270 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005271 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
5272
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005273 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00005274 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005275 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005276 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00005277
Chris Lattnerb7552a82006-05-17 00:15:40 +00005278 // PtrOff will be used to store the current argument to the stack if a
5279 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005280 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005281
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005282 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00005283
Dale Johannesen679073b2009-02-04 02:34:38 +00005284 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005285
5286 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00005287 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00005288 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
5289 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00005290 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00005291 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00005292
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005293 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00005294 // Note: "by value" is code for passing a structure by value, not
5295 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00005296 if (Flags.isByVal()) {
5297 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00005298 // Very small objects are passed right-justified. Everything else is
5299 // passed left-justified.
5300 if (Size==1 || Size==2) {
5301 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005302 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00005303 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00005304 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00005305 false, false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005306 MemOpChains.push_back(Load.getValue(1));
5307 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005308
5309 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005310 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00005311 SDValue Const = DAG.getConstant(PtrByteSize - Size,
5312 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005313 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00005314 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
5315 CallSeqStart,
5316 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005317 ArgOffset += PtrByteSize;
5318 }
5319 continue;
5320 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00005321 // Copy entire object into memory. There are cases where gcc-generated
5322 // code assumes it is there, even if it could be put entirely into
5323 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00005324 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
5325 CallSeqStart,
5326 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00005327
5328 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
5329 // copy the pieces of the object that fit into registers from the
5330 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00005331 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005332 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005333 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00005334 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00005335 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
5336 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005337 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00005338 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00005339 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005340 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00005341 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00005342 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00005343 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00005344 }
5345 }
5346 continue;
5347 }
5348
Craig Topper56710102013-08-15 02:33:50 +00005349 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005350 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00005351 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00005352 case MVT::i32:
5353 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005354 if (GPR_idx != NumGPRs) {
Hal Finkel7f908e82014-03-06 00:45:19 +00005355 if (Arg.getValueType() == MVT::i1)
5356 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg);
5357
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005358 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00005359 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005360 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5361 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005362 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00005363 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005364 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00005365 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005366 case MVT::f32:
5367 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005368 if (FPR_idx != NumFPRs) {
5369 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
5370
Chris Lattnerb7552a82006-05-17 00:15:40 +00005371 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00005372 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5373 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005374 MemOpChains.push_back(Store);
5375
Chris Lattnerb7552a82006-05-17 00:15:40 +00005376 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005377 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00005378 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00005379 MachinePointerInfo(), false, false,
5380 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005381 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005382 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00005383 }
Owen Anderson9f944592009-08-11 20:47:22 +00005384 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005385 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00005386 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00005387 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
5388 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005389 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005390 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005391 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00005392 }
5393 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00005394 // If we have any FPRs remaining, we may also have GPRs remaining.
5395 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
5396 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005397 if (GPR_idx != NumGPRs)
5398 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00005399 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005400 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
5401 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00005402 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00005403 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005404 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5405 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005406 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005407 if (isPPC64)
5408 ArgOffset += 8;
5409 else
Owen Anderson9f944592009-08-11 20:47:22 +00005410 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00005411 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005412 case MVT::v4f32:
5413 case MVT::v4i32:
5414 case MVT::v8i16:
5415 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00005416 if (isVarArg) {
5417 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00005418 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00005419 // V registers; in fact gcc does this only for arguments that are
5420 // prototyped, not for those that match the ... We do it for all
5421 // arguments, seems to work.
5422 while (ArgOffset % 16 !=0) {
5423 ArgOffset += PtrByteSize;
5424 if (GPR_idx != NumGPRs)
5425 GPR_idx++;
5426 }
5427 // We could elide this store in the case where the object fits
5428 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005429 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00005430 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00005431 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
5432 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005433 MemOpChains.push_back(Store);
5434 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00005435 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00005436 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005437 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005438 MemOpChains.push_back(Load.getValue(1));
5439 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
5440 }
5441 ArgOffset += 16;
5442 for (unsigned i=0; i<16; i+=PtrByteSize) {
5443 if (GPR_idx == NumGPRs)
5444 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00005445 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00005446 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00005447 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005448 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005449 MemOpChains.push_back(Load.getValue(1));
5450 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
5451 }
5452 break;
5453 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005454
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005455 // Non-varargs Altivec params generally go in registers, but have
5456 // stack space allocated at the end.
5457 if (VR_idx != NumVRs) {
5458 // Doesn't have GPR space allocated.
5459 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
5460 } else if (nAltivecParamsAtEnd==0) {
5461 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005462 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5463 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005464 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00005465 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00005466 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00005467 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00005468 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00005469 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005470 // If all Altivec parameters fit in registers, as they usually do,
5471 // they get stack space following the non-Altivec parameters. We
5472 // don't track this here because nobody below needs it.
5473 // If there are more Altivec parameters than fit in registers emit
5474 // the stores here.
5475 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
5476 unsigned j = 0;
5477 // Offset is aligned; skip 1st 12 params which go in V registers.
5478 ArgOffset = ((ArgOffset+15)/16)*16;
5479 ArgOffset += 12*16;
5480 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005481 SDValue Arg = OutVals[i];
5482 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00005483 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
5484 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005485 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005486 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005487 // We are emitting Altivec params in order.
5488 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5489 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005490 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005491 ArgOffset += 16;
5492 }
5493 }
5494 }
5495 }
5496
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005497 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00005498 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005499
Dale Johannesen90eab672010-03-09 20:15:42 +00005500 // On Darwin, R12 must contain the address of an indirect callee. This does
5501 // not mean the MTCTR instruction must use R12; it's easier to model this as
5502 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00005503 if (!isTailCall &&
Hal Finkel87deb0b2015-01-12 04:34:47 +00005504 !isFunctionGlobalAddress(Callee) &&
5505 !isa<ExternalSymbolSDNode>(Callee) &&
Dale Johannesen90eab672010-03-09 20:15:42 +00005506 !isBLACompatibleAddress(Callee, DAG))
5507 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
5508 PPC::R12), Callee));
5509
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005510 // Build a sequence of copy-to-reg nodes chained together with token chain
5511 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005512 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005513 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00005514 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00005515 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005516 InFlag = Chain.getValue(1);
5517 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005518
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00005519 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005520 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
5521 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005522
Hal Finkel934361a2015-01-14 01:07:51 +00005523 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint, DAG,
Hal Finkele2ab0f12015-01-15 21:17:34 +00005524 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff,
5525 NumBytes, Ins, InVals, CS);
Chris Lattneraa40ec12006-05-16 22:56:08 +00005526}
5527
Hal Finkel450128a2011-10-14 19:51:36 +00005528bool
5529PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
5530 MachineFunction &MF, bool isVarArg,
5531 const SmallVectorImpl<ISD::OutputArg> &Outs,
5532 LLVMContext &Context) const {
5533 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00005534 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Hal Finkel450128a2011-10-14 19:51:36 +00005535 return CCInfo.CheckReturn(Outs, RetCC_PPC);
5536}
5537
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005538SDValue
5539PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00005540 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005541 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005542 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005543 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005544
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005545 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00005546 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
5547 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005548 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005549
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005550 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005551 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005552
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005553 // Copy the result values into the output registers.
5554 for (unsigned i = 0; i != RVLocs.size(); ++i) {
5555 CCValAssign &VA = RVLocs[i];
5556 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00005557
5558 SDValue Arg = OutVals[i];
5559
5560 switch (VA.getLocInfo()) {
5561 default: llvm_unreachable("Unknown loc info!");
5562 case CCValAssign::Full: break;
5563 case CCValAssign::AExt:
5564 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
5565 break;
5566 case CCValAssign::ZExt:
5567 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
5568 break;
5569 case CCValAssign::SExt:
5570 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
5571 break;
5572 }
5573
5574 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005575 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005576 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005577 }
5578
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005579 RetOps[0] = Chain; // Update chain.
5580
5581 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005582 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005583 RetOps.push_back(Flag);
5584
Craig Topper48d114b2014-04-26 18:35:24 +00005585 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps);
Chris Lattner4211ca92006-04-14 06:01:58 +00005586}
5587
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005588SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005589 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00005590 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005591 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005592
Jim Laskeye4f4d042006-12-04 22:04:42 +00005593 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005594 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00005595
5596 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00005597 bool isPPC64 = Subtarget.isPPC64();
5598 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005599 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00005600
5601 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005602 SDValue Chain = Op.getOperand(0);
5603 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005604
Jim Laskeye4f4d042006-12-04 22:04:42 +00005605 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00005606 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
5607 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005608 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005609
Jim Laskeye4f4d042006-12-04 22:04:42 +00005610 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00005611 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005612
Jim Laskeye4f4d042006-12-04 22:04:42 +00005613 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00005614 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005615 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00005616}
5617
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005618
5619
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005620SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005621PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00005622 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005623 bool isPPC64 = Subtarget.isPPC64();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005624 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005625
5626 // Get current frame pointer save index. The users of this index will be
5627 // primarily DYNALLOC instructions.
5628 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
5629 int RASI = FI->getReturnAddrSaveIndex();
5630
5631 // If the frame pointer save index hasn't been defined yet.
5632 if (!RASI) {
5633 // Find out what the fix offset of the frame pointer save area.
Eric Christopherf71609b2015-02-13 00:39:27 +00005634 int LROffset = Subtarget.getFrameLowering()->getReturnSaveOffset();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005635 // Allocate the frame index for frame pointer save area.
Hal Finkel6e27c6d2014-12-23 09:45:06 +00005636 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, false);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005637 // Save the result.
5638 FI->setReturnAddrSaveIndex(RASI);
5639 }
5640 return DAG.getFrameIndex(RASI, PtrVT);
5641}
5642
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005643SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005644PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
5645 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005646 bool isPPC64 = Subtarget.isPPC64();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005647 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00005648
5649 // Get current frame pointer save index. The users of this index will be
5650 // primarily DYNALLOC instructions.
5651 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
5652 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005653
Jim Laskey48850c12006-11-16 22:43:37 +00005654 // If the frame pointer save index hasn't been defined yet.
5655 if (!FPSI) {
5656 // Find out what the fix offset of the frame pointer save area.
Eric Christopherdc3a8a42015-02-13 00:39:38 +00005657 int FPOffset = Subtarget.getFrameLowering()->getFramePointerSaveOffset();
Jim Laskey48850c12006-11-16 22:43:37 +00005658 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00005659 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00005660 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005661 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00005662 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005663 return DAG.getFrameIndex(FPSI, PtrVT);
5664}
Jim Laskey48850c12006-11-16 22:43:37 +00005665
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005666SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005667 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005668 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00005669 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005670 SDValue Chain = Op.getOperand(0);
5671 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005672 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005673
Jim Laskey48850c12006-11-16 22:43:37 +00005674 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005675 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00005676 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005677 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00005678 DAG.getConstant(0, PtrVT), Size);
5679 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005680 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00005681 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005682 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00005683 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Craig Topper48d114b2014-04-26 18:35:24 +00005684 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops);
Jim Laskey48850c12006-11-16 22:43:37 +00005685}
5686
Hal Finkel756810f2013-03-21 21:37:52 +00005687SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
5688 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005689 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00005690 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
5691 DAG.getVTList(MVT::i32, MVT::Other),
5692 Op.getOperand(0), Op.getOperand(1));
5693}
5694
5695SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
5696 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005697 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00005698 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
5699 Op.getOperand(0), Op.getOperand(1));
5700}
5701
Hal Finkel940ab932014-02-28 00:27:01 +00005702SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +00005703 if (Op.getValueType().isVector())
5704 return LowerVectorLoad(Op, DAG);
5705
Hal Finkel940ab932014-02-28 00:27:01 +00005706 assert(Op.getValueType() == MVT::i1 &&
5707 "Custom lowering only for i1 loads");
5708
5709 // First, load 8 bits into 32 bits, then truncate to 1 bit.
5710
5711 SDLoc dl(Op);
5712 LoadSDNode *LD = cast<LoadSDNode>(Op);
5713
5714 SDValue Chain = LD->getChain();
5715 SDValue BasePtr = LD->getBasePtr();
5716 MachineMemOperand *MMO = LD->getMemOperand();
5717
5718 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(), Chain,
5719 BasePtr, MVT::i8, MMO);
5720 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
5721
5722 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00005723 return DAG.getMergeValues(Ops, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00005724}
5725
5726SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +00005727 if (Op.getOperand(1).getValueType().isVector())
5728 return LowerVectorStore(Op, DAG);
5729
Hal Finkel940ab932014-02-28 00:27:01 +00005730 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
5731 "Custom lowering only for i1 stores");
5732
5733 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
5734
5735 SDLoc dl(Op);
5736 StoreSDNode *ST = cast<StoreSDNode>(Op);
5737
5738 SDValue Chain = ST->getChain();
5739 SDValue BasePtr = ST->getBasePtr();
5740 SDValue Value = ST->getValue();
5741 MachineMemOperand *MMO = ST->getMemOperand();
5742
5743 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(), Value);
5744 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
5745}
5746
5747// FIXME: Remove this once the ANDI glue bug is fixed:
5748SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
5749 assert(Op.getValueType() == MVT::i1 &&
5750 "Custom lowering only for i1 results");
5751
5752 SDLoc DL(Op);
5753 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
5754 Op.getOperand(0));
5755}
5756
Chris Lattner4211ca92006-04-14 06:01:58 +00005757/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
5758/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005759SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00005760 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00005761 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
5762 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00005763 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005764
Hal Finkel81f87992013-04-07 22:11:09 +00005765 // We might be able to do better than this under some circumstances, but in
5766 // general, fsel-based lowering of select is a finite-math-only optimization.
5767 // For more information, see section F.3 of the 2.06 ISA specification.
5768 if (!DAG.getTarget().Options.NoInfsFPMath ||
5769 !DAG.getTarget().Options.NoNaNsFPMath)
5770 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005771
Hal Finkel81f87992013-04-07 22:11:09 +00005772 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005773
Owen Anderson53aa7a92009-08-10 22:56:29 +00005774 EVT ResVT = Op.getValueType();
5775 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005776 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
5777 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005778 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005779
Chris Lattner4211ca92006-04-14 06:01:58 +00005780 // If the RHS of the comparison is a 0.0, we don't need to do the
5781 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00005782 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00005783 if (isFloatingPointZero(RHS))
5784 switch (CC) {
5785 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00005786 case ISD::SETNE:
5787 std::swap(TV, FV);
5788 case ISD::SETEQ:
5789 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5790 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
5791 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
5792 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
5793 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
5794 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5795 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005796 case ISD::SETULT:
5797 case ISD::SETLT:
5798 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005799 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005800 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00005801 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5802 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005803 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005804 case ISD::SETUGT:
5805 case ISD::SETGT:
5806 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005807 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005808 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00005809 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5810 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005811 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005812 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005813 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005814
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005815 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00005816 switch (CC) {
5817 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00005818 case ISD::SETNE:
5819 std::swap(TV, FV);
5820 case ISD::SETEQ:
5821 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
5822 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5823 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
5824 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
5825 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
5826 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
5827 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5828 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005829 case ISD::SETULT:
5830 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005831 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005832 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5833 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005834 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005835 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005836 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005837 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005838 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5839 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005840 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005841 case ISD::SETUGT:
5842 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005843 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005844 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5845 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005846 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005847 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005848 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005849 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005850 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5851 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005852 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005853 }
Eli Friedman5806e182009-05-28 04:31:08 +00005854 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00005855}
5856
Hal Finkeled844c42015-01-06 22:31:02 +00005857void PPCTargetLowering::LowerFP_TO_INTForReuse(SDValue Op, ReuseLoadInfo &RLI,
5858 SelectionDAG &DAG,
5859 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00005860 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005861 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00005862 if (Src.getValueType() == MVT::f32)
5863 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00005864
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005865 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00005866 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005867 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00005868 case MVT::i32:
Eric Christophercccae792015-01-30 22:02:31 +00005869 Tmp = DAG.getNode(
5870 Op.getOpcode() == ISD::FP_TO_SINT
5871 ? PPCISD::FCTIWZ
5872 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ),
5873 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005874 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005875 case MVT::i64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005876 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) &&
Hal Finkel3f88d082013-04-01 18:42:58 +00005877 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00005878 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
5879 PPCISD::FCTIDUZ,
5880 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005881 break;
5882 }
Duncan Sands2a287912008-07-19 16:26:02 +00005883
Chris Lattner4211ca92006-04-14 06:01:58 +00005884 // Convert the FP value to an int value through memory.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005885 bool i32Stack = Op.getValueType() == MVT::i32 && Subtarget.hasSTFIWX() &&
5886 (Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT());
Hal Finkelf6d45f22013-04-01 17:52:07 +00005887 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
5888 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
5889 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00005890
Chris Lattner06a49542007-10-15 20:14:52 +00005891 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005892 SDValue Chain;
5893 if (i32Stack) {
5894 MachineFunction &MF = DAG.getMachineFunction();
5895 MachineMemOperand *MMO =
5896 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
5897 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
5898 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00005899 DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005900 } else
5901 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
5902 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00005903
5904 // Result is a load from the stack slot. If loading 4 bytes, make sure to
5905 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005906 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00005907 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00005908 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkeled844c42015-01-06 22:31:02 +00005909 MPI = MPI.getWithOffset(4);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005910 }
5911
Hal Finkeled844c42015-01-06 22:31:02 +00005912 RLI.Chain = Chain;
5913 RLI.Ptr = FIPtr;
5914 RLI.MPI = MPI;
5915}
5916
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00005917/// \brief Custom lowers floating point to integer conversions to use
5918/// the direct move instructions available in ISA 2.07 to avoid the
5919/// need for load/store combinations.
5920SDValue PPCTargetLowering::LowerFP_TO_INTDirectMove(SDValue Op,
5921 SelectionDAG &DAG,
5922 SDLoc dl) const {
5923 assert(Op.getOperand(0).getValueType().isFloatingPoint());
5924 SDValue Src = Op.getOperand(0);
5925
5926 if (Src.getValueType() == MVT::f32)
5927 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
5928
5929 SDValue Tmp;
5930 switch (Op.getSimpleValueType().SimpleTy) {
5931 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
5932 case MVT::i32:
5933 Tmp = DAG.getNode(
5934 Op.getOpcode() == ISD::FP_TO_SINT
5935 ? PPCISD::FCTIWZ
5936 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ),
5937 dl, MVT::f64, Src);
5938 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i32, Tmp);
5939 break;
5940 case MVT::i64:
5941 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) &&
5942 "i64 FP_TO_UINT is supported only with FPCVT");
5943 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
5944 PPCISD::FCTIDUZ,
5945 dl, MVT::f64, Src);
5946 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i64, Tmp);
5947 break;
5948 }
5949 return Tmp;
5950}
5951
Hal Finkeled844c42015-01-06 22:31:02 +00005952SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
5953 SDLoc dl) const {
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00005954 if (Subtarget.hasDirectMove() && Subtarget.isPPC64())
5955 return LowerFP_TO_INTDirectMove(Op, DAG, dl);
5956
Hal Finkeled844c42015-01-06 22:31:02 +00005957 ReuseLoadInfo RLI;
5958 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl);
5959
5960 return DAG.getLoad(Op.getValueType(), dl, RLI.Chain, RLI.Ptr, RLI.MPI, false,
5961 false, RLI.IsInvariant, RLI.Alignment, RLI.AAInfo,
5962 RLI.Ranges);
5963}
5964
5965// We're trying to insert a regular store, S, and then a load, L. If the
5966// incoming value, O, is a load, we might just be able to have our load use the
5967// address used by O. However, we don't know if anything else will store to
5968// that address before we can load from it. To prevent this situation, we need
5969// to insert our load, L, into the chain as a peer of O. To do this, we give L
5970// the same chain operand as O, we create a token factor from the chain results
5971// of O and L, and we replace all uses of O's chain result with that token
5972// factor (see spliceIntoChain below for this last part).
5973bool PPCTargetLowering::canReuseLoadAddress(SDValue Op, EVT MemVT,
5974 ReuseLoadInfo &RLI,
Hal Finkel6c392692015-01-09 01:34:30 +00005975 SelectionDAG &DAG,
5976 ISD::LoadExtType ET) const {
Hal Finkeled844c42015-01-06 22:31:02 +00005977 SDLoc dl(Op);
Hal Finkel6c392692015-01-09 01:34:30 +00005978 if (ET == ISD::NON_EXTLOAD &&
5979 (Op.getOpcode() == ISD::FP_TO_UINT ||
Hal Finkeled844c42015-01-06 22:31:02 +00005980 Op.getOpcode() == ISD::FP_TO_SINT) &&
5981 isOperationLegalOrCustom(Op.getOpcode(),
5982 Op.getOperand(0).getValueType())) {
5983
5984 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl);
5985 return true;
5986 }
5987
5988 LoadSDNode *LD = dyn_cast<LoadSDNode>(Op);
Hal Finkel6c392692015-01-09 01:34:30 +00005989 if (!LD || LD->getExtensionType() != ET || LD->isVolatile() ||
5990 LD->isNonTemporal())
Hal Finkeled844c42015-01-06 22:31:02 +00005991 return false;
5992 if (LD->getMemoryVT() != MemVT)
5993 return false;
5994
5995 RLI.Ptr = LD->getBasePtr();
5996 if (LD->isIndexed() && LD->getOffset().getOpcode() != ISD::UNDEF) {
5997 assert(LD->getAddressingMode() == ISD::PRE_INC &&
5998 "Non-pre-inc AM on PPC?");
5999 RLI.Ptr = DAG.getNode(ISD::ADD, dl, RLI.Ptr.getValueType(), RLI.Ptr,
6000 LD->getOffset());
6001 }
6002
6003 RLI.Chain = LD->getChain();
6004 RLI.MPI = LD->getPointerInfo();
6005 RLI.IsInvariant = LD->isInvariant();
6006 RLI.Alignment = LD->getAlignment();
6007 RLI.AAInfo = LD->getAAInfo();
6008 RLI.Ranges = LD->getRanges();
6009
6010 RLI.ResChain = SDValue(LD, LD->isIndexed() ? 2 : 1);
6011 return true;
6012}
6013
6014// Given the head of the old chain, ResChain, insert a token factor containing
6015// it and NewResChain, and make users of ResChain now be users of that token
6016// factor.
6017void PPCTargetLowering::spliceIntoChain(SDValue ResChain,
6018 SDValue NewResChain,
6019 SelectionDAG &DAG) const {
6020 if (!ResChain)
6021 return;
6022
6023 SDLoc dl(NewResChain);
6024
6025 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
6026 NewResChain, DAG.getUNDEF(MVT::Other));
6027 assert(TF.getNode() != NewResChain.getNode() &&
6028 "A new TF really is required here");
6029
6030 DAG.ReplaceAllUsesOfValueWith(ResChain, TF);
6031 DAG.UpdateNodeOperands(TF.getNode(), ResChain, NewResChain);
Chris Lattner4211ca92006-04-14 06:01:58 +00006032}
6033
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006034/// \brief Custom lowers integer to floating point conversions to use
6035/// the direct move instructions available in ISA 2.07 to avoid the
6036/// need for load/store combinations.
6037SDValue PPCTargetLowering::LowerINT_TO_FPDirectMove(SDValue Op,
6038 SelectionDAG &DAG,
6039 SDLoc dl) const {
6040 assert((Op.getValueType() == MVT::f32 ||
6041 Op.getValueType() == MVT::f64) &&
6042 "Invalid floating point type as target of conversion");
6043 assert(Subtarget.hasFPCVT() &&
6044 "Int to FP conversions with direct moves require FPCVT");
6045 SDValue FP;
6046 SDValue Src = Op.getOperand(0);
6047 bool SinglePrec = Op.getValueType() == MVT::f32;
6048 bool WordInt = Src.getSimpleValueType().SimpleTy == MVT::i32;
6049 bool Signed = Op.getOpcode() == ISD::SINT_TO_FP;
6050 unsigned ConvOp = Signed ? (SinglePrec ? PPCISD::FCFIDS : PPCISD::FCFID) :
6051 (SinglePrec ? PPCISD::FCFIDUS : PPCISD::FCFIDU);
6052
6053 if (WordInt) {
6054 FP = DAG.getNode(Signed ? PPCISD::MTVSRA : PPCISD::MTVSRZ,
6055 dl, MVT::f64, Src);
6056 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP);
6057 }
6058 else {
6059 FP = DAG.getNode(PPCISD::MTVSRA, dl, MVT::f64, Src);
6060 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP);
6061 }
6062
6063 return FP;
6064}
6065
Hal Finkelf6d45f22013-04-01 17:52:07 +00006066SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Hal Finkeled844c42015-01-06 22:31:02 +00006067 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006068 SDLoc dl(Op);
Hal Finkelc93a9a22015-02-25 01:06:45 +00006069
6070 if (Subtarget.hasQPX() && Op.getOperand(0).getValueType() == MVT::v4i1) {
6071 if (Op.getValueType() != MVT::v4f32 && Op.getValueType() != MVT::v4f64)
6072 return SDValue();
6073
6074 SDValue Value = Op.getOperand(0);
6075 // The values are now known to be -1 (false) or 1 (true). To convert this
6076 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
6077 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
6078 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
6079
6080 SDValue FPHalfs = DAG.getConstantFP(0.5, MVT::f64);
6081 FPHalfs = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f64,
6082 FPHalfs, FPHalfs, FPHalfs, FPHalfs);
6083
6084 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
6085
6086 if (Op.getValueType() != MVT::v4f64)
6087 Value = DAG.getNode(ISD::FP_ROUND, dl,
6088 Op.getValueType(), Value, DAG.getIntPtrConstant(1));
6089 return Value;
6090 }
6091
Dan Gohmand6819da2008-03-11 01:59:03 +00006092 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00006093 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006094 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00006095
Hal Finkel6a56b212014-03-05 22:14:00 +00006096 if (Op.getOperand(0).getValueType() == MVT::i1)
6097 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0),
6098 DAG.getConstantFP(1.0, Op.getValueType()),
6099 DAG.getConstantFP(0.0, Op.getValueType()));
6100
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00006101 // If we have direct moves, we can do all the conversion, skip the store/load
6102 // however, without FPCVT we can't do most conversions.
6103 if (Subtarget.hasDirectMove() && Subtarget.isPPC64() && Subtarget.hasFPCVT())
6104 return LowerINT_TO_FPDirectMove(Op, DAG, dl);
6105
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006106 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006107 "UINT_TO_FP is supported only with FPCVT");
6108
6109 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00006110 // Otherwise, convert to double-precision and then round.
Eric Christophercccae792015-01-30 22:02:31 +00006111 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
6112 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS
6113 : PPCISD::FCFIDS)
6114 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU
6115 : PPCISD::FCFID);
6116 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
6117 ? MVT::f32
6118 : MVT::f64;
Hal Finkelf6d45f22013-04-01 17:52:07 +00006119
Owen Anderson9f944592009-08-11 20:47:22 +00006120 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006121 SDValue SINT = Op.getOperand(0);
6122 // When converting to single-precision, we actually need to convert
6123 // to double-precision first and then round to single-precision.
6124 // To avoid double-rounding effects during that operation, we have
6125 // to prepare the input operand. Bits that might be truncated when
6126 // converting to double-precision are replaced by a bit that won't
6127 // be lost at this stage, but is below the single-precision rounding
6128 // position.
6129 //
6130 // However, if -enable-unsafe-fp-math is in effect, accept double
6131 // rounding to avoid the extra overhead.
6132 if (Op.getValueType() == MVT::f32 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006133 !Subtarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00006134 !DAG.getTarget().Options.UnsafeFPMath) {
6135
6136 // Twiddle input to make sure the low 11 bits are zero. (If this
6137 // is the case, we are guaranteed the value will fit into the 53 bit
6138 // mantissa of an IEEE double-precision value without rounding.)
6139 // If any of those low 11 bits were not zero originally, make sure
6140 // bit 12 (value 2048) is set instead, so that the final rounding
6141 // to single-precision gets the correct result.
6142 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
6143 SINT, DAG.getConstant(2047, MVT::i64));
6144 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
6145 Round, DAG.getConstant(2047, MVT::i64));
6146 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
6147 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
6148 Round, DAG.getConstant(-2048, MVT::i64));
6149
6150 // However, we cannot use that value unconditionally: if the magnitude
6151 // of the input value is small, the bit-twiddling we did above might
6152 // end up visibly changing the output. Fortunately, in that case, we
6153 // don't need to twiddle bits since the original input will convert
6154 // exactly to double-precision floating-point already. Therefore,
6155 // construct a conditional to use the original value if the top 11
6156 // bits are all sign-bit copies, and use the rounded value computed
6157 // above otherwise.
6158 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
6159 SINT, DAG.getConstant(53, MVT::i32));
6160 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
6161 Cond, DAG.getConstant(1, MVT::i64));
6162 Cond = DAG.getSetCC(dl, MVT::i32,
6163 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
6164
6165 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
6166 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00006167
Hal Finkeled844c42015-01-06 22:31:02 +00006168 ReuseLoadInfo RLI;
6169 SDValue Bits;
6170
Hal Finkel6c392692015-01-09 01:34:30 +00006171 MachineFunction &MF = DAG.getMachineFunction();
Hal Finkeled844c42015-01-06 22:31:02 +00006172 if (canReuseLoadAddress(SINT, MVT::i64, RLI, DAG)) {
6173 Bits = DAG.getLoad(MVT::f64, dl, RLI.Chain, RLI.Ptr, RLI.MPI, false,
6174 false, RLI.IsInvariant, RLI.Alignment, RLI.AAInfo,
6175 RLI.Ranges);
6176 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
Hal Finkel6c392692015-01-09 01:34:30 +00006177 } else if (Subtarget.hasLFIWAX() &&
6178 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::SEXTLOAD)) {
6179 MachineMemOperand *MMO =
6180 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6181 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6182 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6183 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWAX, dl,
6184 DAG.getVTList(MVT::f64, MVT::Other),
6185 Ops, MVT::i32, MMO);
6186 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
6187 } else if (Subtarget.hasFPCVT() &&
6188 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::ZEXTLOAD)) {
6189 MachineMemOperand *MMO =
6190 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6191 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6192 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6193 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWZX, dl,
6194 DAG.getVTList(MVT::f64, MVT::Other),
6195 Ops, MVT::i32, MMO);
6196 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
6197 } else if (((Subtarget.hasLFIWAX() &&
6198 SINT.getOpcode() == ISD::SIGN_EXTEND) ||
6199 (Subtarget.hasFPCVT() &&
6200 SINT.getOpcode() == ISD::ZERO_EXTEND)) &&
6201 SINT.getOperand(0).getValueType() == MVT::i32) {
6202 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
6203 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
6204
6205 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
6206 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
6207
6208 SDValue Store =
6209 DAG.getStore(DAG.getEntryNode(), dl, SINT.getOperand(0), FIdx,
6210 MachinePointerInfo::getFixedStack(FrameIdx),
6211 false, false, 0);
6212
6213 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
6214 "Expected an i32 store");
6215
6216 RLI.Ptr = FIdx;
6217 RLI.Chain = Store;
6218 RLI.MPI = MachinePointerInfo::getFixedStack(FrameIdx);
6219 RLI.Alignment = 4;
6220
6221 MachineMemOperand *MMO =
6222 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6223 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6224 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
6225 Bits = DAG.getMemIntrinsicNode(SINT.getOpcode() == ISD::ZERO_EXTEND ?
6226 PPCISD::LFIWZX : PPCISD::LFIWAX,
6227 dl, DAG.getVTList(MVT::f64, MVT::Other),
6228 Ops, MVT::i32, MMO);
Hal Finkeled844c42015-01-06 22:31:02 +00006229 } else
6230 Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
6231
Hal Finkelf6d45f22013-04-01 17:52:07 +00006232 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
6233
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006234 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00006235 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006236 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00006237 return FP;
6238 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006239
Owen Anderson9f944592009-08-11 20:47:22 +00006240 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006241 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00006242 // Since we only generate this in 64-bit mode, we can take advantage of
6243 // 64-bit registers. In particular, sign extend the input value into the
6244 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
6245 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00006246 MachineFunction &MF = DAG.getMachineFunction();
6247 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00006248 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006249
Hal Finkelbeb296b2013-03-31 10:12:51 +00006250 SDValue Ld;
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006251 if (Subtarget.hasLFIWAX() || Subtarget.hasFPCVT()) {
Hal Finkeled844c42015-01-06 22:31:02 +00006252 ReuseLoadInfo RLI;
6253 bool ReusingLoad;
6254 if (!(ReusingLoad = canReuseLoadAddress(Op.getOperand(0), MVT::i32, RLI,
6255 DAG))) {
6256 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
6257 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006258
Hal Finkeled844c42015-01-06 22:31:02 +00006259 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
6260 MachinePointerInfo::getFixedStack(FrameIdx),
6261 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00006262
Hal Finkeled844c42015-01-06 22:31:02 +00006263 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
6264 "Expected an i32 store");
6265
6266 RLI.Ptr = FIdx;
6267 RLI.Chain = Store;
6268 RLI.MPI = MachinePointerInfo::getFixedStack(FrameIdx);
6269 RLI.Alignment = 4;
6270 }
6271
Hal Finkelbeb296b2013-03-31 10:12:51 +00006272 MachineMemOperand *MMO =
Hal Finkeled844c42015-01-06 22:31:02 +00006273 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
6274 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
6275 SDValue Ops[] = { RLI.Chain, RLI.Ptr };
Hal Finkelf6d45f22013-04-01 17:52:07 +00006276 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
6277 PPCISD::LFIWZX : PPCISD::LFIWAX,
6278 dl, DAG.getVTList(MVT::f64, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00006279 Ops, MVT::i32, MMO);
Hal Finkeled844c42015-01-06 22:31:02 +00006280 if (ReusingLoad)
6281 spliceIntoChain(RLI.ResChain, Ld.getValue(1), DAG);
Hal Finkelbeb296b2013-03-31 10:12:51 +00006282 } else {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006283 assert(Subtarget.isPPC64() &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00006284 "i32->FP without LFIWAX supported only on PPC64");
6285
Hal Finkelbeb296b2013-03-31 10:12:51 +00006286 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
6287 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
6288
6289 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
6290 Op.getOperand(0));
6291
6292 // STD the extended value into the stack slot.
6293 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
6294 MachinePointerInfo::getFixedStack(FrameIdx),
6295 false, false, 0);
6296
6297 // Load the value as a double.
6298 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
6299 MachinePointerInfo::getFixedStack(FrameIdx),
6300 false, false, false, 0);
6301 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006302
Chris Lattner4211ca92006-04-14 06:01:58 +00006303 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00006304 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006305 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00006306 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00006307 return FP;
6308}
6309
Dan Gohman21cea8a2010-04-17 15:26:15 +00006310SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
6311 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006312 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006313 /*
6314 The rounding mode is in bits 30:31 of FPSR, and has the following
6315 settings:
6316 00 Round to nearest
6317 01 Round to 0
6318 10 Round to +inf
6319 11 Round to -inf
6320
6321 FLT_ROUNDS, on the other hand, expects the following:
6322 -1 Undefined
6323 0 Round to 0
6324 1 Round to nearest
6325 2 Round to +inf
6326 3 Round to -inf
6327
6328 To perform the conversion, we do:
6329 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
6330 */
6331
6332 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00006333 EVT VT = Op.getValueType();
6334 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006335
6336 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00006337 EVT NodeTys[] = {
6338 MVT::f64, // return register
6339 MVT::Glue // unused in this context
6340 };
Craig Topper2d2aa0c2014-04-30 07:17:30 +00006341 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, None);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006342
6343 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00006344 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006345 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00006346 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00006347 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006348
6349 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006350 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00006351 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00006352 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006353 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006354
6355 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006356 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00006357 DAG.getNode(ISD::AND, dl, MVT::i32,
6358 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006359 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00006360 DAG.getNode(ISD::SRL, dl, MVT::i32,
6361 DAG.getNode(ISD::AND, dl, MVT::i32,
6362 DAG.getNode(ISD::XOR, dl, MVT::i32,
6363 CWD, DAG.getConstant(3, MVT::i32)),
6364 DAG.getConstant(3, MVT::i32)),
6365 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006366
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006367 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00006368 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006369
Duncan Sands13237ac2008-06-06 12:08:01 +00006370 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00006371 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00006372}
6373
Dan Gohman21cea8a2010-04-17 15:26:15 +00006374SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006375 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006376 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006377 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00006378 assert(Op.getNumOperands() == 3 &&
6379 VT == Op.getOperand(1).getValueType() &&
6380 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006381
Chris Lattner601b8652006-09-20 03:47:40 +00006382 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00006383 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006384 SDValue Lo = Op.getOperand(0);
6385 SDValue Hi = Op.getOperand(1);
6386 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006387 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006388
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006389 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00006390 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006391 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
6392 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
6393 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
6394 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00006395 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006396 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
6397 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
6398 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006399 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006400 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006401}
6402
Dan Gohman21cea8a2010-04-17 15:26:15 +00006403SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006404 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006405 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00006406 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00006407 assert(Op.getNumOperands() == 3 &&
6408 VT == Op.getOperand(1).getValueType() &&
6409 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006410
Dan Gohman8d2ead22008-03-07 20:36:53 +00006411 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00006412 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006413 SDValue Lo = Op.getOperand(0);
6414 SDValue Hi = Op.getOperand(1);
6415 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006416 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006417
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006418 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00006419 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006420 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
6421 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
6422 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
6423 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00006424 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00006425 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
6426 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
6427 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006428 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006429 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006430}
6431
Dan Gohman21cea8a2010-04-17 15:26:15 +00006432SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006433 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006434 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006435 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00006436 assert(Op.getNumOperands() == 3 &&
6437 VT == Op.getOperand(1).getValueType() &&
6438 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006439
Dan Gohman8d2ead22008-03-07 20:36:53 +00006440 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006441 SDValue Lo = Op.getOperand(0);
6442 SDValue Hi = Op.getOperand(1);
6443 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006444 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006445
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006446 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00006447 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006448 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
6449 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
6450 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
6451 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00006452 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00006453 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
6454 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
6455 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00006456 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006457 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00006458 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00006459}
6460
6461//===----------------------------------------------------------------------===//
6462// Vector related lowering.
6463//
6464
Chris Lattner2a099c02006-04-17 06:00:21 +00006465/// BuildSplatI - Build a canonical splati of Val with an element size of
6466/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00006467static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006468 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00006469 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00006470
Benjamin Kramer7149aab2015-03-01 18:09:56 +00006471 static const MVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00006472 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00006473 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00006474
Owen Anderson9f944592009-08-11 20:47:22 +00006475 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00006476
Chris Lattner09ed0ff2006-12-01 01:45:39 +00006477 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
6478 if (Val == -1)
6479 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006480
Owen Anderson53aa7a92009-08-10 22:56:29 +00006481 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00006482
Chris Lattner2a099c02006-04-17 06:00:21 +00006483 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00006484 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006485 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00006486 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Craig Topper48d114b2014-04-26 18:35:24 +00006487 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006488 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00006489}
6490
Hal Finkelcf2e9082013-05-24 23:00:14 +00006491/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
6492/// specified intrinsic ID.
6493static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006494 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00006495 EVT DestVT = MVT::Other) {
6496 if (DestVT == MVT::Other) DestVT = Op.getValueType();
6497 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
6498 DAG.getConstant(IID, MVT::i32), Op);
6499}
6500
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006501/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00006502/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006503static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006504 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006505 EVT DestVT = MVT::Other) {
6506 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006507 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00006508 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00006509}
6510
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006511/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
6512/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006513static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006514 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006515 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00006516 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006517 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00006518 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006519}
6520
6521
Chris Lattner264c9082006-04-17 17:55:10 +00006522/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
6523/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006524static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006525 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00006526 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00006527 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
6528 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00006529
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006530 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00006531 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006532 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00006533 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006534 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00006535}
6536
Chris Lattner19e90552006-04-14 05:19:18 +00006537// If this is a case we can't handle, return null and let the default
6538// expansion code take care of it. If we CAN select this case, and if it
6539// selects to a single instruction, return Op. Otherwise, if we can codegen
6540// this case more efficiently than a constant pool load, lower it to the
6541// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00006542SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
6543 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006544 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006545 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
Craig Toppere73658d2014-04-28 04:05:08 +00006546 assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00006547
Hal Finkelc93a9a22015-02-25 01:06:45 +00006548 if (Subtarget.hasQPX() && Op.getValueType() == MVT::v4i1) {
6549 // We first build an i32 vector, load it into a QPX register,
6550 // then convert it to a floating-point vector and compare it
6551 // to a zero vector to get the boolean result.
6552 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6553 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
6554 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FrameIdx);
6555 EVT PtrVT = getPointerTy();
6556 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
6557
6558 assert(BVN->getNumOperands() == 4 &&
6559 "BUILD_VECTOR for v4i1 does not have 4 operands");
6560
6561 bool IsConst = true;
6562 for (unsigned i = 0; i < 4; ++i) {
6563 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF) continue;
6564 if (!isa<ConstantSDNode>(BVN->getOperand(i))) {
6565 IsConst = false;
6566 break;
6567 }
6568 }
6569
6570 if (IsConst) {
6571 Constant *One =
6572 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), 1.0);
6573 Constant *NegOne =
6574 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), -1.0);
6575
6576 SmallVector<Constant*, 4> CV(4, NegOne);
6577 for (unsigned i = 0; i < 4; ++i) {
6578 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF)
6579 CV[i] = UndefValue::get(Type::getFloatTy(*DAG.getContext()));
6580 else if (cast<ConstantSDNode>(BVN->getOperand(i))->
6581 getConstantIntValue()->isZero())
6582 continue;
6583 else
6584 CV[i] = One;
6585 }
6586
6587 Constant *CP = ConstantVector::get(CV);
6588 SDValue CPIdx = DAG.getConstantPool(CP, getPointerTy(),
6589 16 /* alignment */);
6590
6591 SmallVector<SDValue, 2> Ops;
6592 Ops.push_back(DAG.getEntryNode());
6593 Ops.push_back(CPIdx);
6594
6595 SmallVector<EVT, 2> ValueVTs;
6596 ValueVTs.push_back(MVT::v4i1);
6597 ValueVTs.push_back(MVT::Other); // chain
6598 SDVTList VTs = DAG.getVTList(ValueVTs);
6599
6600 return DAG.getMemIntrinsicNode(PPCISD::QVLFSb,
6601 dl, VTs, Ops, MVT::v4f32,
6602 MachinePointerInfo::getConstantPool());
6603 }
6604
6605 SmallVector<SDValue, 4> Stores;
6606 for (unsigned i = 0; i < 4; ++i) {
6607 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF) continue;
6608
6609 unsigned Offset = 4*i;
6610 SDValue Idx = DAG.getConstant(Offset, FIdx.getValueType());
6611 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
6612
6613 unsigned StoreSize = BVN->getOperand(i).getValueType().getStoreSize();
6614 if (StoreSize > 4) {
6615 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
6616 BVN->getOperand(i), Idx,
6617 PtrInfo.getWithOffset(Offset),
6618 MVT::i32, false, false, 0));
6619 } else {
6620 SDValue StoreValue = BVN->getOperand(i);
6621 if (StoreSize < 4)
6622 StoreValue = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, StoreValue);
6623
6624 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
6625 StoreValue, Idx,
6626 PtrInfo.getWithOffset(Offset),
6627 false, false, 0));
6628 }
6629 }
6630
6631 SDValue StoreChain;
6632 if (!Stores.empty())
6633 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
6634 else
6635 StoreChain = DAG.getEntryNode();
6636
6637 // Now load from v4i32 into the QPX register; this will extend it to
6638 // v4i64 but not yet convert it to a floating point. Nevertheless, this
6639 // is typed as v4f64 because the QPX register integer states are not
6640 // explicitly represented.
6641
6642 SmallVector<SDValue, 2> Ops;
6643 Ops.push_back(StoreChain);
6644 Ops.push_back(DAG.getConstant(Intrinsic::ppc_qpx_qvlfiwz, MVT::i32));
6645 Ops.push_back(FIdx);
6646
6647 SmallVector<EVT, 2> ValueVTs;
6648 ValueVTs.push_back(MVT::v4f64);
6649 ValueVTs.push_back(MVT::Other); // chain
6650 SDVTList VTs = DAG.getVTList(ValueVTs);
6651
6652 SDValue LoadedVect = DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN,
6653 dl, VTs, Ops, MVT::v4i32, PtrInfo);
6654 LoadedVect = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
6655 DAG.getConstant(Intrinsic::ppc_qpx_qvfcfidu, MVT::i32),
6656 LoadedVect);
6657
6658 SDValue FPZeros = DAG.getConstantFP(0.0, MVT::f64);
6659 FPZeros = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f64,
6660 FPZeros, FPZeros, FPZeros, FPZeros);
6661
6662 return DAG.getSetCC(dl, MVT::v4i1, LoadedVect, FPZeros, ISD::SETEQ);
6663 }
6664
6665 // All other QPX vectors are handled by generic code.
6666 if (Subtarget.hasQPX())
6667 return SDValue();
6668
Bob Wilson85cefe82009-03-02 23:24:16 +00006669 // Check if this is a splat of a constant value.
6670 APInt APSplatBits, APSplatUndef;
6671 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006672 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00006673 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Bill Schmidt91dd7652015-04-03 13:48:24 +00006674 HasAnyUndefs, 0, !Subtarget.isLittleEndian()) ||
6675 SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00006676 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00006677
Bob Wilson530e0382009-03-03 19:26:27 +00006678 unsigned SplatBits = APSplatBits.getZExtValue();
6679 unsigned SplatUndef = APSplatUndef.getZExtValue();
6680 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006681
Bob Wilson530e0382009-03-03 19:26:27 +00006682 // First, handle single instruction cases.
6683
6684 // All zeros?
6685 if (SplatBits == 0) {
6686 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00006687 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
6688 SDValue Z = DAG.getConstant(0, MVT::i32);
6689 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00006690 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00006691 }
Bob Wilson530e0382009-03-03 19:26:27 +00006692 return Op;
6693 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00006694
Bob Wilson530e0382009-03-03 19:26:27 +00006695 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
6696 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
6697 (32-SplatBitSize));
6698 if (SextVal >= -16 && SextVal <= 15)
6699 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006700
6701
Bob Wilson530e0382009-03-03 19:26:27 +00006702 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006703
Bob Wilson530e0382009-03-03 19:26:27 +00006704 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00006705 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
6706 // If this value is in the range [17,31] and is odd, use:
6707 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
6708 // If this value is in the range [-31,-17] and is odd, use:
6709 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
6710 // Note the last two are three-instruction sequences.
6711 if (SextVal >= -32 && SextVal <= 31) {
6712 // To avoid having these optimizations undone by constant folding,
6713 // we convert to a pseudo that will be expanded later into one of
6714 // the above forms.
6715 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt71dddd52014-05-27 15:57:51 +00006716 EVT VT = (SplatSize == 1 ? MVT::v16i8 :
6717 (SplatSize == 2 ? MVT::v8i16 : MVT::v4i32));
6718 SDValue EltSize = DAG.getConstant(SplatSize, MVT::i32);
6719 SDValue RetVal = DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
6720 if (VT == Op.getValueType())
6721 return RetVal;
6722 else
6723 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), RetVal);
Bob Wilson530e0382009-03-03 19:26:27 +00006724 }
6725
6726 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
6727 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
6728 // for fneg/fabs.
6729 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
6730 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00006731 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006732
6733 // Make the VSLW intrinsic, computing 0x8000_0000.
6734 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
6735 OnesV, DAG, dl);
6736
6737 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00006738 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00006739 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00006740 }
6741
6742 // Check to see if this is a wide variety of vsplti*, binop self cases.
6743 static const signed char SplatCsts[] = {
6744 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
6745 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
6746 };
6747
6748 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
6749 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
6750 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
6751 int i = SplatCsts[idx];
6752
6753 // Figure out what shift amount will be used by altivec if shifted by i in
6754 // this splat size.
6755 unsigned TypeShiftAmt = i & (SplatBitSize-1);
6756
6757 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00006758 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00006759 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006760 static const unsigned IIDs[] = { // Intrinsic to use for each size.
6761 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
6762 Intrinsic::ppc_altivec_vslw
6763 };
6764 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00006765 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00006766 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006767
Bob Wilson530e0382009-03-03 19:26:27 +00006768 // vsplti + srl self.
6769 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00006770 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006771 static const unsigned IIDs[] = { // Intrinsic to use for each size.
6772 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
6773 Intrinsic::ppc_altivec_vsrw
6774 };
6775 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00006776 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00006777 }
6778
Bob Wilson530e0382009-03-03 19:26:27 +00006779 // vsplti + sra self.
6780 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00006781 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006782 static const unsigned IIDs[] = { // Intrinsic to use for each size.
6783 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
6784 Intrinsic::ppc_altivec_vsraw
6785 };
6786 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00006787 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00006788 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006789
Bob Wilson530e0382009-03-03 19:26:27 +00006790 // vsplti + rol self.
6791 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
6792 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00006793 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006794 static const unsigned IIDs[] = { // Intrinsic to use for each size.
6795 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
6796 Intrinsic::ppc_altivec_vrlw
6797 };
6798 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00006799 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00006800 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006801
Bob Wilson530e0382009-03-03 19:26:27 +00006802 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00006803 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00006804 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006805 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00006806 }
Bob Wilson530e0382009-03-03 19:26:27 +00006807 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00006808 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00006809 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006810 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00006811 }
Bob Wilson530e0382009-03-03 19:26:27 +00006812 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00006813 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00006814 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00006815 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
6816 }
6817 }
6818
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006819 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00006820}
6821
Chris Lattner071ad012006-04-17 05:28:54 +00006822/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
6823/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006824static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00006825 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006826 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00006827 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00006828 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00006829 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006830
Chris Lattner071ad012006-04-17 05:28:54 +00006831 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00006832 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 +00006833 OP_VMRGHW,
6834 OP_VMRGLW,
6835 OP_VSPLTISW0,
6836 OP_VSPLTISW1,
6837 OP_VSPLTISW2,
6838 OP_VSPLTISW3,
6839 OP_VSLDOI4,
6840 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00006841 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00006842 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00006843
Chris Lattner071ad012006-04-17 05:28:54 +00006844 if (OpNum == OP_COPY) {
6845 if (LHSID == (1*9+2)*9+3) return LHS;
6846 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
6847 return RHS;
6848 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006849
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006850 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006851 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
6852 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006853
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006854 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00006855 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006856 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00006857 case OP_VMRGHW:
6858 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
6859 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
6860 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
6861 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
6862 break;
6863 case OP_VMRGLW:
6864 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
6865 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
6866 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
6867 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
6868 break;
6869 case OP_VSPLTISW0:
6870 for (unsigned i = 0; i != 16; ++i)
6871 ShufIdxs[i] = (i&3)+0;
6872 break;
6873 case OP_VSPLTISW1:
6874 for (unsigned i = 0; i != 16; ++i)
6875 ShufIdxs[i] = (i&3)+4;
6876 break;
6877 case OP_VSPLTISW2:
6878 for (unsigned i = 0; i != 16; ++i)
6879 ShufIdxs[i] = (i&3)+8;
6880 break;
6881 case OP_VSPLTISW3:
6882 for (unsigned i = 0; i != 16; ++i)
6883 ShufIdxs[i] = (i&3)+12;
6884 break;
6885 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006886 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006887 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006888 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006889 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006890 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006891 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00006892 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00006893 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
6894 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00006895 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00006896 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00006897}
6898
Chris Lattner19e90552006-04-14 05:19:18 +00006899/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
6900/// is a shuffle we can handle in a single instruction, return it. Otherwise,
6901/// return the code it can be lowered into. Worst case, it can always be
6902/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006903SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006904 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006905 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006906 SDValue V1 = Op.getOperand(0);
6907 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006908 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006909 EVT VT = Op.getValueType();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006910 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006911
Hal Finkelc93a9a22015-02-25 01:06:45 +00006912 if (Subtarget.hasQPX()) {
6913 if (VT.getVectorNumElements() != 4)
6914 return SDValue();
6915
6916 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
6917
6918 int AlignIdx = PPC::isQVALIGNIShuffleMask(SVOp);
6919 if (AlignIdx != -1) {
6920 return DAG.getNode(PPCISD::QVALIGNI, dl, VT, V1, V2,
6921 DAG.getConstant(AlignIdx, MVT::i32));
6922 } else if (SVOp->isSplat()) {
6923 int SplatIdx = SVOp->getSplatIndex();
6924 if (SplatIdx >= 4) {
6925 std::swap(V1, V2);
6926 SplatIdx -= 4;
6927 }
6928
6929 // FIXME: If SplatIdx == 0 and the input came from a load, then there is
6930 // nothing to do.
6931
6932 return DAG.getNode(PPCISD::QVESPLATI, dl, VT, V1,
6933 DAG.getConstant(SplatIdx, MVT::i32));
6934 }
6935
6936 // Lower this into a qvgpci/qvfperm pair.
6937
6938 // Compute the qvgpci literal
6939 unsigned idx = 0;
6940 for (unsigned i = 0; i < 4; ++i) {
6941 int m = SVOp->getMaskElt(i);
6942 unsigned mm = m >= 0 ? (unsigned) m : i;
6943 idx |= mm << (3-i)*3;
6944 }
6945
6946 SDValue V3 = DAG.getNode(PPCISD::QVGPCI, dl, MVT::v4f64,
6947 DAG.getConstant(idx, MVT::i32));
6948 return DAG.getNode(PPCISD::QVFPERM, dl, VT, V1, V2, V3);
6949 }
6950
Chris Lattner19e90552006-04-14 05:19:18 +00006951 // Cases that are handled by instructions that take permute immediates
6952 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
6953 // selected by the instruction selector.
6954 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006955 if (PPC::isSplatShuffleMask(SVOp, 1) ||
6956 PPC::isSplatShuffleMask(SVOp, 2) ||
6957 PPC::isSplatShuffleMask(SVOp, 4) ||
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00006958 PPC::isVPKUWUMShuffleMask(SVOp, 1, DAG) ||
6959 PPC::isVPKUHUMShuffleMask(SVOp, 1, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00006960 PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00006961 PPC::isVMRGLShuffleMask(SVOp, 1, 1, DAG) ||
6962 PPC::isVMRGLShuffleMask(SVOp, 2, 1, DAG) ||
6963 PPC::isVMRGLShuffleMask(SVOp, 4, 1, DAG) ||
6964 PPC::isVMRGHShuffleMask(SVOp, 1, 1, DAG) ||
6965 PPC::isVMRGHShuffleMask(SVOp, 2, 1, DAG) ||
6966 PPC::isVMRGHShuffleMask(SVOp, 4, 1, DAG)) {
Chris Lattner19e90552006-04-14 05:19:18 +00006967 return Op;
6968 }
6969 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006970
Chris Lattner19e90552006-04-14 05:19:18 +00006971 // Altivec has a variety of "shuffle immediates" that take two vector inputs
6972 // and produce a fixed permutation. If any of these match, do not lower to
6973 // VPERM.
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00006974 unsigned int ShuffleKind = isLittleEndian ? 2 : 0;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00006975 if (PPC::isVPKUWUMShuffleMask(SVOp, ShuffleKind, DAG) ||
6976 PPC::isVPKUHUMShuffleMask(SVOp, ShuffleKind, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00006977 PPC::isVSLDOIShuffleMask(SVOp, ShuffleKind, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00006978 PPC::isVMRGLShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
6979 PPC::isVMRGLShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
6980 PPC::isVMRGLShuffleMask(SVOp, 4, ShuffleKind, DAG) ||
6981 PPC::isVMRGHShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
6982 PPC::isVMRGHShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
6983 PPC::isVMRGHShuffleMask(SVOp, 4, ShuffleKind, DAG))
Chris Lattner19e90552006-04-14 05:19:18 +00006984 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006985
Chris Lattner071ad012006-04-17 05:28:54 +00006986 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
6987 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00006988 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00006989
Chris Lattner071ad012006-04-17 05:28:54 +00006990 unsigned PFIndexes[4];
6991 bool isFourElementShuffle = true;
6992 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
6993 unsigned EltNo = 8; // Start out undef.
6994 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006995 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00006996 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006997
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006998 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00006999 if ((ByteSource & 3) != j) {
7000 isFourElementShuffle = false;
7001 break;
7002 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007003
Chris Lattner071ad012006-04-17 05:28:54 +00007004 if (EltNo == 8) {
7005 EltNo = ByteSource/4;
7006 } else if (EltNo != ByteSource/4) {
7007 isFourElementShuffle = false;
7008 break;
7009 }
7010 }
7011 PFIndexes[i] = EltNo;
7012 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007013
7014 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00007015 // perfect shuffle vector to determine if it is cost effective to do this as
7016 // discrete instructions, or whether we should use a vperm.
Bill Schmidtf910a062014-06-10 14:35:01 +00007017 // For now, we skip this for little endian until such time as we have a
7018 // little-endian perfect shuffle table.
7019 if (isFourElementShuffle && !isLittleEndian) {
Chris Lattner071ad012006-04-17 05:28:54 +00007020 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007021 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00007022 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007023
Chris Lattner071ad012006-04-17 05:28:54 +00007024 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
7025 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007026
Chris Lattner071ad012006-04-17 05:28:54 +00007027 // Determining when to avoid vperm is tricky. Many things affect the cost
7028 // of vperm, particularly how many times the perm mask needs to be computed.
7029 // For example, if the perm mask can be hoisted out of a loop or is already
7030 // used (perhaps because there are multiple permutes with the same shuffle
7031 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
7032 // the loop requires an extra register.
7033 //
7034 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00007035 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00007036 // available, if this block is within a loop, we should avoid using vperm
7037 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007038 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007039 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00007040 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007041
Chris Lattner19e90552006-04-14 05:19:18 +00007042 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
7043 // vector that will get spilled to the constant pool.
7044 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007045
Chris Lattner19e90552006-04-14 05:19:18 +00007046 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
7047 // that it is in input element units, not in bytes. Convert now.
Bill Schmidt4aedff82014-06-06 14:06:26 +00007048
7049 // For little endian, the order of the input vectors is reversed, and
7050 // the permutation mask is complemented with respect to 31. This is
7051 // necessary to produce proper semantics with the big-endian-biased vperm
7052 // instruction.
Owen Anderson53aa7a92009-08-10 22:56:29 +00007053 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00007054 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007055
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007056 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007057 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
7058 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00007059
Chris Lattner19e90552006-04-14 05:19:18 +00007060 for (unsigned j = 0; j != BytesPerElement; ++j)
Bill Schmidt4aedff82014-06-06 14:06:26 +00007061 if (isLittleEndian)
7062 ResultMask.push_back(DAG.getConstant(31 - (SrcElt*BytesPerElement+j),
7063 MVT::i32));
7064 else
7065 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
7066 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00007067 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007068
Owen Anderson9f944592009-08-11 20:47:22 +00007069 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Craig Topper48d114b2014-04-26 18:35:24 +00007070 ResultMask);
Bill Schmidt4aedff82014-06-06 14:06:26 +00007071 if (isLittleEndian)
7072 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
7073 V2, V1, VPermMask);
7074 else
7075 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
7076 V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00007077}
7078
Chris Lattner9754d142006-04-18 17:59:36 +00007079/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
7080/// altivec comparison. If it is, return true and fill in Opc/isDot with
7081/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007082static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Kit Barton0cfa7b72015-03-03 19:55:45 +00007083 bool &isDot, const PPCSubtarget &Subtarget) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00007084 unsigned IntrinsicID =
7085 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00007086 CompareOpc = -1;
7087 isDot = false;
7088 switch (IntrinsicID) {
7089 default: return false;
7090 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00007091 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
7092 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
7093 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
7094 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
7095 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007096 case Intrinsic::ppc_altivec_vcmpequd_p:
7097 if (Subtarget.hasP8Altivec()) {
7098 CompareOpc = 199;
7099 isDot = 1;
7100 }
7101 else
7102 return false;
7103
7104 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007105 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
7106 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
7107 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
7108 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
7109 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007110 case Intrinsic::ppc_altivec_vcmpgtsd_p:
7111 if (Subtarget.hasP8Altivec()) {
7112 CompareOpc = 967;
7113 isDot = 1;
7114 }
7115 else
7116 return false;
7117
7118 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007119 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
7120 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
7121 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007122 case Intrinsic::ppc_altivec_vcmpgtud_p:
7123 if (Subtarget.hasP8Altivec()) {
7124 CompareOpc = 711;
7125 isDot = 1;
7126 }
7127 else
7128 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007129
Kit Barton0cfa7b72015-03-03 19:55:45 +00007130 break;
7131
Chris Lattner4211ca92006-04-14 06:01:58 +00007132 // Normal Comparisons.
7133 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
7134 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
7135 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
7136 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
7137 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007138 case Intrinsic::ppc_altivec_vcmpequd:
7139 if (Subtarget.hasP8Altivec()) {
7140 CompareOpc = 199;
7141 isDot = 0;
7142 }
7143 else
7144 return false;
7145
7146 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007147 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
7148 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
7149 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
7150 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
7151 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007152 case Intrinsic::ppc_altivec_vcmpgtsd:
7153 if (Subtarget.hasP8Altivec()) {
7154 CompareOpc = 967;
7155 isDot = 0;
7156 }
7157 else
7158 return false;
7159
7160 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007161 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
7162 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
7163 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007164 case Intrinsic::ppc_altivec_vcmpgtud:
7165 if (Subtarget.hasP8Altivec()) {
7166 CompareOpc = 711;
7167 isDot = 0;
7168 }
7169 else
7170 return false;
7171
7172 break;
Chris Lattner4211ca92006-04-14 06:01:58 +00007173 }
Chris Lattner9754d142006-04-18 17:59:36 +00007174 return true;
7175}
7176
7177/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
7178/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007179SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007180 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00007181 // If this is a lowered altivec predicate compare, CompareOpc is set to the
7182 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00007183 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00007184 int CompareOpc;
7185 bool isDot;
Kit Barton0cfa7b72015-03-03 19:55:45 +00007186 if (!getAltivecCompareInfo(Op, CompareOpc, isDot, Subtarget))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007187 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007188
Chris Lattner9754d142006-04-18 17:59:36 +00007189 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00007190 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00007191 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00007192 Op.getOperand(1), Op.getOperand(2),
7193 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00007194 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00007195 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007196
Chris Lattner4211ca92006-04-14 06:01:58 +00007197 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007198 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007199 Op.getOperand(2), // LHS
7200 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00007201 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007202 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00007203 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00007204 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007205
Chris Lattner4211ca92006-04-14 06:01:58 +00007206 // Now that we have the comparison, emit a copy from the CR to a GPR.
7207 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00007208 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00007209 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00007210 CompNode.getValue(1));
7211
Chris Lattner4211ca92006-04-14 06:01:58 +00007212 // Unpack the result based on how the target uses it.
7213 unsigned BitNo; // Bit # of CR6.
7214 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00007215 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00007216 default: // Can't happen, don't crash on invalid number though.
7217 case 0: // Return the value of the EQ bit of CR6.
7218 BitNo = 0; InvertBit = false;
7219 break;
7220 case 1: // Return the inverted value of the EQ bit of CR6.
7221 BitNo = 0; InvertBit = true;
7222 break;
7223 case 2: // Return the value of the LT bit of CR6.
7224 BitNo = 2; InvertBit = false;
7225 break;
7226 case 3: // Return the inverted value of the LT bit of CR6.
7227 BitNo = 2; InvertBit = true;
7228 break;
7229 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007230
Chris Lattner4211ca92006-04-14 06:01:58 +00007231 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00007232 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
7233 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00007234 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00007235 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
7236 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00007237
Chris Lattner4211ca92006-04-14 06:01:58 +00007238 // If we are supposed to, toggle the bit.
7239 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00007240 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
7241 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00007242 return Flags;
7243}
7244
Hal Finkel5c0d1452014-03-30 13:22:59 +00007245SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
7246 SelectionDAG &DAG) const {
7247 SDLoc dl(Op);
7248 // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int
7249 // instructions), but for smaller types, we need to first extend up to v2i32
7250 // before doing going farther.
7251 if (Op.getValueType() == MVT::v2i64) {
7252 EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
7253 if (ExtVT != MVT::v2i32) {
7254 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0));
7255 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op,
7256 DAG.getValueType(EVT::getVectorVT(*DAG.getContext(),
7257 ExtVT.getVectorElementType(), 4)));
7258 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op);
7259 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op,
7260 DAG.getValueType(MVT::v2i32));
7261 }
7262
7263 return Op;
7264 }
7265
7266 return SDValue();
7267}
7268
Scott Michelcf0da6c2009-02-17 22:15:04 +00007269SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007270 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007271 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00007272 // Create a stack slot that is 16-byte aligned.
7273 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00007274 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007275 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007276 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007277
Chris Lattner4211ca92006-04-14 06:01:58 +00007278 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00007279 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00007280 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00007281 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00007282 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00007283 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00007284 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00007285}
7286
Hal Finkelc93a9a22015-02-25 01:06:45 +00007287SDValue PPCTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7288 SelectionDAG &DAG) const {
7289 SDLoc dl(Op);
7290 SDNode *N = Op.getNode();
7291
7292 assert(N->getOperand(0).getValueType() == MVT::v4i1 &&
7293 "Unknown extract_vector_elt type");
7294
7295 SDValue Value = N->getOperand(0);
7296
7297 // The first part of this is like the store lowering except that we don't
7298 // need to track the chain.
7299
7300 // The values are now known to be -1 (false) or 1 (true). To convert this
7301 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
7302 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
7303 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
7304
7305 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to
7306 // understand how to form the extending load.
7307 SDValue FPHalfs = DAG.getConstantFP(0.5, MVT::f64);
7308 FPHalfs = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f64,
7309 FPHalfs, FPHalfs, FPHalfs, FPHalfs);
7310
7311 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
7312
7313 // Now convert to an integer and store.
7314 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
7315 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, MVT::i32),
7316 Value);
7317
7318 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
7319 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
7320 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FrameIdx);
7321 EVT PtrVT = getPointerTy();
7322 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
7323
7324 SDValue StoreChain = DAG.getEntryNode();
7325 SmallVector<SDValue, 2> Ops;
7326 Ops.push_back(StoreChain);
7327 Ops.push_back(DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, MVT::i32));
7328 Ops.push_back(Value);
7329 Ops.push_back(FIdx);
7330
7331 SmallVector<EVT, 2> ValueVTs;
7332 ValueVTs.push_back(MVT::Other); // chain
7333 SDVTList VTs = DAG.getVTList(ValueVTs);
7334
7335 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID,
7336 dl, VTs, Ops, MVT::v4i32, PtrInfo);
7337
7338 // Extract the value requested.
7339 unsigned Offset = 4*cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7340 SDValue Idx = DAG.getConstant(Offset, FIdx.getValueType());
7341 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
7342
7343 SDValue IntVal = DAG.getLoad(MVT::i32, dl, StoreChain, Idx,
7344 PtrInfo.getWithOffset(Offset),
7345 false, false, false, 0);
7346
7347 if (!Subtarget.useCRBits())
7348 return IntVal;
7349
7350 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, IntVal);
7351}
7352
7353/// Lowering for QPX v4i1 loads
7354SDValue PPCTargetLowering::LowerVectorLoad(SDValue Op,
7355 SelectionDAG &DAG) const {
7356 SDLoc dl(Op);
7357 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
7358 SDValue LoadChain = LN->getChain();
7359 SDValue BasePtr = LN->getBasePtr();
7360
7361 if (Op.getValueType() == MVT::v4f64 ||
7362 Op.getValueType() == MVT::v4f32) {
7363 EVT MemVT = LN->getMemoryVT();
7364 unsigned Alignment = LN->getAlignment();
7365
7366 // If this load is properly aligned, then it is legal.
7367 if (Alignment >= MemVT.getStoreSize())
7368 return Op;
7369
7370 EVT ScalarVT = Op.getValueType().getScalarType(),
7371 ScalarMemVT = MemVT.getScalarType();
7372 unsigned Stride = ScalarMemVT.getStoreSize();
7373
7374 SmallVector<SDValue, 8> Vals, LoadChains;
7375 for (unsigned Idx = 0; Idx < 4; ++Idx) {
7376 SDValue Load;
7377 if (ScalarVT != ScalarMemVT)
7378 Load =
7379 DAG.getExtLoad(LN->getExtensionType(), dl, ScalarVT, LoadChain,
7380 BasePtr,
7381 LN->getPointerInfo().getWithOffset(Idx*Stride),
7382 ScalarMemVT, LN->isVolatile(), LN->isNonTemporal(),
7383 LN->isInvariant(), MinAlign(Alignment, Idx*Stride),
7384 LN->getAAInfo());
7385 else
7386 Load =
7387 DAG.getLoad(ScalarVT, dl, LoadChain, BasePtr,
7388 LN->getPointerInfo().getWithOffset(Idx*Stride),
7389 LN->isVolatile(), LN->isNonTemporal(),
7390 LN->isInvariant(), MinAlign(Alignment, Idx*Stride),
7391 LN->getAAInfo());
7392
7393 if (Idx == 0 && LN->isIndexed()) {
7394 assert(LN->getAddressingMode() == ISD::PRE_INC &&
7395 "Unknown addressing mode on vector load");
7396 Load = DAG.getIndexedLoad(Load, dl, BasePtr, LN->getOffset(),
7397 LN->getAddressingMode());
7398 }
7399
7400 Vals.push_back(Load);
7401 LoadChains.push_back(Load.getValue(1));
7402
7403 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
7404 DAG.getConstant(Stride, BasePtr.getValueType()));
7405 }
7406
7407 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
7408 SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl,
7409 Op.getValueType(), Vals);
7410
7411 if (LN->isIndexed()) {
7412 SDValue RetOps[] = { Value, Vals[0].getValue(1), TF };
7413 return DAG.getMergeValues(RetOps, dl);
7414 }
7415
7416 SDValue RetOps[] = { Value, TF };
7417 return DAG.getMergeValues(RetOps, dl);
7418 }
7419
7420 assert(Op.getValueType() == MVT::v4i1 && "Unknown load to lower");
7421 assert(LN->isUnindexed() && "Indexed v4i1 loads are not supported");
7422
7423 // To lower v4i1 from a byte array, we load the byte elements of the
7424 // vector and then reuse the BUILD_VECTOR logic.
7425
7426 SmallVector<SDValue, 4> VectElmts, VectElmtChains;
7427 for (unsigned i = 0; i < 4; ++i) {
7428 SDValue Idx = DAG.getConstant(i, BasePtr.getValueType());
7429 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx);
7430
7431 VectElmts.push_back(DAG.getExtLoad(ISD::EXTLOAD,
7432 dl, MVT::i32, LoadChain, Idx,
7433 LN->getPointerInfo().getWithOffset(i),
7434 MVT::i8 /* memory type */,
7435 LN->isVolatile(), LN->isNonTemporal(),
7436 LN->isInvariant(),
7437 1 /* alignment */, LN->getAAInfo()));
7438 VectElmtChains.push_back(VectElmts[i].getValue(1));
7439 }
7440
7441 LoadChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, VectElmtChains);
7442 SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i1, VectElmts);
7443
7444 SDValue RVals[] = { Value, LoadChain };
7445 return DAG.getMergeValues(RVals, dl);
7446}
7447
7448/// Lowering for QPX v4i1 stores
7449SDValue PPCTargetLowering::LowerVectorStore(SDValue Op,
7450 SelectionDAG &DAG) const {
7451 SDLoc dl(Op);
7452 StoreSDNode *SN = cast<StoreSDNode>(Op.getNode());
7453 SDValue StoreChain = SN->getChain();
7454 SDValue BasePtr = SN->getBasePtr();
7455 SDValue Value = SN->getValue();
7456
7457 if (Value.getValueType() == MVT::v4f64 ||
7458 Value.getValueType() == MVT::v4f32) {
7459 EVT MemVT = SN->getMemoryVT();
7460 unsigned Alignment = SN->getAlignment();
7461
7462 // If this store is properly aligned, then it is legal.
7463 if (Alignment >= MemVT.getStoreSize())
7464 return Op;
7465
7466 EVT ScalarVT = Value.getValueType().getScalarType(),
7467 ScalarMemVT = MemVT.getScalarType();
7468 unsigned Stride = ScalarMemVT.getStoreSize();
7469
7470 SmallVector<SDValue, 8> Stores;
7471 for (unsigned Idx = 0; Idx < 4; ++Idx) {
7472 SDValue Ex =
7473 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ScalarVT, Value,
7474 DAG.getConstant(Idx, getVectorIdxTy()));
7475 SDValue Store;
7476 if (ScalarVT != ScalarMemVT)
7477 Store =
7478 DAG.getTruncStore(StoreChain, dl, Ex, BasePtr,
7479 SN->getPointerInfo().getWithOffset(Idx*Stride),
7480 ScalarMemVT, SN->isVolatile(), SN->isNonTemporal(),
7481 MinAlign(Alignment, Idx*Stride), SN->getAAInfo());
7482 else
7483 Store =
7484 DAG.getStore(StoreChain, dl, Ex, BasePtr,
7485 SN->getPointerInfo().getWithOffset(Idx*Stride),
7486 SN->isVolatile(), SN->isNonTemporal(),
7487 MinAlign(Alignment, Idx*Stride), SN->getAAInfo());
7488
7489 if (Idx == 0 && SN->isIndexed()) {
7490 assert(SN->getAddressingMode() == ISD::PRE_INC &&
7491 "Unknown addressing mode on vector store");
7492 Store = DAG.getIndexedStore(Store, dl, BasePtr, SN->getOffset(),
7493 SN->getAddressingMode());
7494 }
7495
7496 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
7497 DAG.getConstant(Stride, BasePtr.getValueType()));
7498 Stores.push_back(Store);
7499 }
7500
7501 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
7502
7503 if (SN->isIndexed()) {
7504 SDValue RetOps[] = { TF, Stores[0].getValue(1) };
7505 return DAG.getMergeValues(RetOps, dl);
7506 }
7507
7508 return TF;
7509 }
7510
7511 assert(SN->isUnindexed() && "Indexed v4i1 stores are not supported");
7512 assert(Value.getValueType() == MVT::v4i1 && "Unknown store to lower");
7513
7514 // The values are now known to be -1 (false) or 1 (true). To convert this
7515 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5).
7516 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5
7517 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value);
7518
7519 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to
7520 // understand how to form the extending load.
7521 SDValue FPHalfs = DAG.getConstantFP(0.5, MVT::f64);
7522 FPHalfs = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f64,
7523 FPHalfs, FPHalfs, FPHalfs, FPHalfs);
7524
7525 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs);
7526
7527 // Now convert to an integer and store.
7528 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64,
7529 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, MVT::i32),
7530 Value);
7531
7532 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
7533 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
7534 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FrameIdx);
7535 EVT PtrVT = getPointerTy();
7536 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
7537
7538 SmallVector<SDValue, 2> Ops;
7539 Ops.push_back(StoreChain);
7540 Ops.push_back(DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, MVT::i32));
7541 Ops.push_back(Value);
7542 Ops.push_back(FIdx);
7543
7544 SmallVector<EVT, 2> ValueVTs;
7545 ValueVTs.push_back(MVT::Other); // chain
7546 SDVTList VTs = DAG.getVTList(ValueVTs);
7547
7548 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID,
7549 dl, VTs, Ops, MVT::v4i32, PtrInfo);
7550
7551 // Move data into the byte array.
7552 SmallVector<SDValue, 4> Loads, LoadChains;
7553 for (unsigned i = 0; i < 4; ++i) {
7554 unsigned Offset = 4*i;
7555 SDValue Idx = DAG.getConstant(Offset, FIdx.getValueType());
7556 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx);
7557
7558 Loads.push_back(DAG.getLoad(MVT::i32, dl, StoreChain, Idx,
7559 PtrInfo.getWithOffset(Offset),
7560 false, false, false, 0));
7561 LoadChains.push_back(Loads[i].getValue(1));
7562 }
7563
7564 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
7565
7566 SmallVector<SDValue, 4> Stores;
7567 for (unsigned i = 0; i < 4; ++i) {
7568 SDValue Idx = DAG.getConstant(i, BasePtr.getValueType());
7569 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx);
7570
7571 Stores.push_back(DAG.getTruncStore(StoreChain, dl, Loads[i], Idx,
7572 SN->getPointerInfo().getWithOffset(i),
7573 MVT::i8 /* memory type */,
7574 SN->isNonTemporal(), SN->isVolatile(),
7575 1 /* alignment */, SN->getAAInfo()));
7576 }
7577
7578 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
7579
7580 return StoreChain;
7581}
7582
Dan Gohman21cea8a2010-04-17 15:26:15 +00007583SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007584 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00007585 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007586 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007587
Owen Anderson9f944592009-08-11 20:47:22 +00007588 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
7589 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007590
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007591 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007592 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007593
Chris Lattner7e4398742006-04-18 03:43:48 +00007594 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00007595 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
7596 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
7597 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007598
Chris Lattner7e4398742006-04-18 03:43:48 +00007599 // Low parts multiplied together, generating 32-bit results (we ignore the
7600 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007601 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00007602 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007603
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007604 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00007605 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00007606 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007607 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007608 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00007609 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
7610 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007611 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007612
Owen Anderson9f944592009-08-11 20:47:22 +00007613 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00007614
Chris Lattner96d50482006-04-18 04:28:57 +00007615 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00007616 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00007617 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007618 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007619 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00007620
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00007621 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007622 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00007623 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00007624 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007625
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00007626 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007627 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00007628 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00007629 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007630
Bill Schmidt42995e82014-06-09 16:06:29 +00007631 // Merge the results together. Because vmuleub and vmuloub are
7632 // instructions with a big-endian bias, we must reverse the
7633 // element numbering and reverse the meaning of "odd" and "even"
7634 // when generating little endian code.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00007635 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00007636 for (unsigned i = 0; i != 8; ++i) {
Bill Schmidt42995e82014-06-09 16:06:29 +00007637 if (isLittleEndian) {
7638 Ops[i*2 ] = 2*i;
7639 Ops[i*2+1] = 2*i+16;
7640 } else {
7641 Ops[i*2 ] = 2*i+1;
7642 Ops[i*2+1] = 2*i+1+16;
7643 }
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00007644 }
Bill Schmidt42995e82014-06-09 16:06:29 +00007645 if (isLittleEndian)
7646 return DAG.getVectorShuffle(MVT::v16i8, dl, OddParts, EvenParts, Ops);
7647 else
7648 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00007649 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007650 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00007651 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00007652}
7653
Chris Lattnerf3d06c62005-08-26 00:52:45 +00007654/// LowerOperation - Provide custom lowering hooks for some operations.
7655///
Dan Gohman21cea8a2010-04-17 15:26:15 +00007656SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00007657 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007658 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00007659 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00007660 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00007661 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00007662 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00007663 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00007664 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00007665 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
7666 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007667 case ISD::VASTART:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007668 return LowerVASTART(Op, DAG, Subtarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007669
7670 case ISD::VAARG:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007671 return LowerVAARG(Op, DAG, Subtarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00007672
Roman Divackyc3825df2013-07-25 21:36:47 +00007673 case ISD::VACOPY:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007674 return LowerVACOPY(Op, DAG, Subtarget);
Roman Divackyc3825df2013-07-25 21:36:47 +00007675
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007676 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, Subtarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00007677 case ISD::DYNAMIC_STACKALLOC:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007678 return LowerDYNAMIC_STACKALLOC(Op, DAG, Subtarget);
Evan Cheng51096af2008-04-19 01:30:48 +00007679
Hal Finkel756810f2013-03-21 21:37:52 +00007680 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
7681 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
7682
Hal Finkel940ab932014-02-28 00:27:01 +00007683 case ISD::LOAD: return LowerLOAD(Op, DAG);
7684 case ISD::STORE: return LowerSTORE(Op, DAG);
7685 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00007686 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00007687 case ISD::FP_TO_UINT:
7688 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Hal Finkeled844c42015-01-06 22:31:02 +00007689 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00007690 case ISD::UINT_TO_FP:
7691 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00007692 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00007693
Chris Lattner4211ca92006-04-14 06:01:58 +00007694 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00007695 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
7696 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
7697 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00007698
Chris Lattner4211ca92006-04-14 06:01:58 +00007699 // Vector-related lowering.
7700 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
7701 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7702 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
7703 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Hal Finkel5c0d1452014-03-30 13:22:59 +00007704 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Hal Finkelc93a9a22015-02-25 01:06:45 +00007705 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00007706 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007707
Hal Finkel25c19922013-05-15 21:37:41 +00007708 // For counter-based loop handling.
7709 case ISD::INTRINSIC_W_CHAIN: return SDValue();
7710
Chris Lattnerf6a81562007-12-08 06:59:59 +00007711 // Frame & Return address.
7712 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00007713 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00007714 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00007715}
7716
Duncan Sands6ed40142008-12-01 11:39:25 +00007717void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
7718 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007719 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007720 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00007721 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00007722 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007723 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkelbbdee932014-12-02 22:01:00 +00007724 case ISD::READCYCLECOUNTER: {
7725 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
7726 SDValue RTB = DAG.getNode(PPCISD::READ_TIME_BASE, dl, VTs, N->getOperand(0));
7727
7728 Results.push_back(RTB);
7729 Results.push_back(RTB.getValue(1));
7730 Results.push_back(RTB.getValue(2));
7731 break;
7732 }
Hal Finkel25c19922013-05-15 21:37:41 +00007733 case ISD::INTRINSIC_W_CHAIN: {
7734 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
7735 Intrinsic::ppc_is_decremented_ctr_nonzero)
7736 break;
7737
7738 assert(N->getValueType(0) == MVT::i1 &&
7739 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00007740 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00007741 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
7742 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
7743 N->getOperand(1));
7744
7745 Results.push_back(NewInt);
7746 Results.push_back(NewInt.getValue(1));
7747 break;
7748 }
Roman Divacky4394e682011-06-28 15:30:42 +00007749 case ISD::VAARG: {
Eric Christophercccae792015-01-30 22:02:31 +00007750 if (!Subtarget.isSVR4ABI() || Subtarget.isPPC64())
Roman Divacky4394e682011-06-28 15:30:42 +00007751 return;
7752
7753 EVT VT = N->getValueType(0);
7754
7755 if (VT == MVT::i64) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007756 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, Subtarget);
Roman Divacky4394e682011-06-28 15:30:42 +00007757
7758 Results.push_back(NewNode);
7759 Results.push_back(NewNode.getValue(1));
7760 }
7761 return;
7762 }
Duncan Sands6ed40142008-12-01 11:39:25 +00007763 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00007764 assert(N->getValueType(0) == MVT::ppcf128);
7765 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007766 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00007767 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00007768 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00007769 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00007770 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00007771 DAG.getIntPtrConstant(1));
7772
Ulrich Weigand874fc622013-03-26 10:56:22 +00007773 // Add the two halves of the long double in round-to-zero mode.
7774 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00007775
7776 // We know the low half is about to be thrown away, so just use something
7777 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00007778 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00007779 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00007780 return;
Duncan Sands2a287912008-07-19 16:26:02 +00007781 }
Duncan Sands6ed40142008-12-01 11:39:25 +00007782 case ISD::FP_TO_SINT:
Hal Finkel93138502015-04-10 03:39:00 +00007783 case ISD::FP_TO_UINT:
Bill Schmidt41221692013-07-09 18:50:20 +00007784 // LowerFP_TO_INT() can only handle f32 and f64.
7785 if (N->getOperand(0).getValueType() == MVT::ppcf128)
7786 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00007787 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00007788 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00007789 }
7790}
7791
7792
Chris Lattner4211ca92006-04-14 06:01:58 +00007793//===----------------------------------------------------------------------===//
7794// Other Lowering Code
7795//===----------------------------------------------------------------------===//
7796
Robin Morisset22129962014-09-23 20:46:49 +00007797static Instruction* callIntrinsic(IRBuilder<> &Builder, Intrinsic::ID Id) {
7798 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
7799 Function *Func = Intrinsic::getDeclaration(M, Id);
7800 return Builder.CreateCall(Func);
7801}
7802
7803// The mappings for emitLeading/TrailingFence is taken from
7804// http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
7805Instruction* PPCTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
7806 AtomicOrdering Ord, bool IsStore,
7807 bool IsLoad) const {
7808 if (Ord == SequentiallyConsistent)
7809 return callIntrinsic(Builder, Intrinsic::ppc_sync);
7810 else if (isAtLeastRelease(Ord))
7811 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
7812 else
7813 return nullptr;
7814}
7815
7816Instruction* PPCTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
7817 AtomicOrdering Ord, bool IsStore,
7818 bool IsLoad) const {
7819 if (IsLoad && isAtLeastAcquire(Ord))
7820 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
7821 // FIXME: this is too conservative, a dependent branch + isync is enough.
7822 // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html and
7823 // http://www.rdrop.com/users/paulmck/scalability/paper/N2745r.2011.03.04a.html
7824 // and http://www.cl.cam.ac.uk/~pes20/cppppc/ for justification.
7825 else
7826 return nullptr;
7827}
7828
Chris Lattner9b577f12005-08-26 21:23:58 +00007829MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00007830PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00007831 unsigned AtomicSize,
7832 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007833 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christophercccae792015-01-30 22:02:31 +00007834 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Dale Johannesend4eb0522008-08-25 22:34:37 +00007835
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00007836 auto LoadMnemonic = PPC::LDARX;
7837 auto StoreMnemonic = PPC::STDCX;
7838 switch (AtomicSize) {
7839 default:
7840 llvm_unreachable("Unexpected size of atomic entity");
7841 case 1:
7842 LoadMnemonic = PPC::LBARX;
7843 StoreMnemonic = PPC::STBCX;
7844 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4");
7845 break;
7846 case 2:
7847 LoadMnemonic = PPC::LHARX;
7848 StoreMnemonic = PPC::STHCX;
7849 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4");
7850 break;
7851 case 4:
7852 LoadMnemonic = PPC::LWARX;
7853 StoreMnemonic = PPC::STWCX;
7854 break;
7855 case 8:
7856 LoadMnemonic = PPC::LDARX;
7857 StoreMnemonic = PPC::STDCX;
7858 break;
7859 }
7860
Dale Johannesend4eb0522008-08-25 22:34:37 +00007861 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7862 MachineFunction *F = BB->getParent();
7863 MachineFunction::iterator It = BB;
7864 ++It;
7865
7866 unsigned dest = MI->getOperand(0).getReg();
7867 unsigned ptrA = MI->getOperand(1).getReg();
7868 unsigned ptrB = MI->getOperand(2).getReg();
7869 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00007870 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00007871
7872 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
7873 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
7874 F->insert(It, loopMBB);
7875 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007876 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007877 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007878 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007879
7880 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007881 unsigned TmpReg = (!BinOpcode) ? incr :
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00007882 RegInfo.createVirtualRegister( AtomicSize == 8 ? &PPC::G8RCRegClass
Craig Topper61e88f42014-11-21 05:58:21 +00007883 : &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007884
7885 // thisMBB:
7886 // ...
7887 // fallthrough --> loopMBB
7888 BB->addSuccessor(loopMBB);
7889
7890 // loopMBB:
7891 // l[wd]arx dest, ptr
7892 // add r0, dest, incr
7893 // st[wd]cx. r0, ptr
7894 // bne- loopMBB
7895 // fallthrough --> exitMBB
7896 BB = loopMBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00007897 BuildMI(BB, dl, TII->get(LoadMnemonic), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00007898 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007899 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00007900 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00007901 BuildMI(BB, dl, TII->get(StoreMnemonic))
Dale Johannesend4eb0522008-08-25 22:34:37 +00007902 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007903 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00007904 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007905 BB->addSuccessor(loopMBB);
7906 BB->addSuccessor(exitMBB);
7907
7908 // exitMBB:
7909 // ...
7910 BB = exitMBB;
7911 return BB;
7912}
7913
7914MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00007915PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00007916 MachineBasicBlock *BB,
7917 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00007918 unsigned BinOpcode) const {
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00007919 // If we support part-word atomic mnemonics, just use them
7920 if (Subtarget.hasPartwordAtomics())
7921 return EmitAtomicBinary(MI, BB, is8bit ? 1 : 2, BinOpcode);
7922
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007923 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christophercccae792015-01-30 22:02:31 +00007924 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Dale Johannesena32affb2008-08-28 17:53:09 +00007925 // In 64 bit mode we have to use 64 bits for addresses, even though the
7926 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
7927 // registers without caring whether they're 32 or 64, but here we're
7928 // doing actual arithmetic on the addresses.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007929 bool is64bit = Subtarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00007930 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00007931
7932 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7933 MachineFunction *F = BB->getParent();
7934 MachineFunction::iterator It = BB;
7935 ++It;
7936
7937 unsigned dest = MI->getOperand(0).getReg();
7938 unsigned ptrA = MI->getOperand(1).getReg();
7939 unsigned ptrB = MI->getOperand(2).getReg();
7940 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00007941 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00007942
7943 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
7944 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
7945 F->insert(It, loopMBB);
7946 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007947 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007948 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007949 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00007950
7951 MachineRegisterInfo &RegInfo = F->getRegInfo();
Craig Topper61e88f42014-11-21 05:58:21 +00007952 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass
7953 : &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00007954 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
7955 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
7956 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
7957 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
7958 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
7959 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
7960 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
7961 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
7962 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
7963 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007964 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00007965 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007966 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00007967
7968 // thisMBB:
7969 // ...
7970 // fallthrough --> loopMBB
7971 BB->addSuccessor(loopMBB);
7972
7973 // The 4-byte load must be aligned, while a char or short may be
7974 // anywhere in the word. Hence all this nasty bookkeeping code.
7975 // add ptr1, ptrA, ptrB [copy if ptrA==0]
7976 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00007977 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00007978 // rlwinm ptr, ptr1, 0, 0, 29
7979 // slw incr2, incr, shift
7980 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
7981 // slw mask, mask2, shift
7982 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00007983 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007984 // add tmp, tmpDest, incr2
7985 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00007986 // and tmp3, tmp, mask
7987 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00007988 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00007989 // bne- loopMBB
7990 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007991 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007992 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00007993 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007994 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00007995 .addReg(ptrA).addReg(ptrB);
7996 } else {
7997 Ptr1Reg = ptrB;
7998 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00007999 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008000 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008001 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008002 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
8003 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008004 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008005 .addReg(Ptr1Reg).addImm(0).addImm(61);
8006 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00008007 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008008 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008009 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008010 .addReg(incr).addReg(ShiftReg);
8011 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008012 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00008013 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00008014 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
8015 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00008016 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008017 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008018 .addReg(Mask2Reg).addReg(ShiftReg);
8019
8020 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00008021 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008022 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008023 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008024 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008025 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008026 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008027 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008028 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008029 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008030 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00008031 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00008032 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008033 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008034 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00008035 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00008036 BB->addSuccessor(loopMBB);
8037 BB->addSuccessor(exitMBB);
8038
8039 // exitMBB:
8040 // ...
8041 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00008042 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
8043 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00008044 return BB;
8045}
8046
Hal Finkel756810f2013-03-21 21:37:52 +00008047llvm::MachineBasicBlock*
8048PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
8049 MachineBasicBlock *MBB) const {
8050 DebugLoc DL = MI->getDebugLoc();
Eric Christophercccae792015-01-30 22:02:31 +00008051 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00008052
8053 MachineFunction *MF = MBB->getParent();
8054 MachineRegisterInfo &MRI = MF->getRegInfo();
8055
8056 const BasicBlock *BB = MBB->getBasicBlock();
8057 MachineFunction::iterator I = MBB;
8058 ++I;
8059
8060 // Memory Reference
8061 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
8062 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
8063
8064 unsigned DstReg = MI->getOperand(0).getReg();
8065 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
8066 assert(RC->hasType(MVT::i32) && "Invalid destination!");
8067 unsigned mainDstReg = MRI.createVirtualRegister(RC);
8068 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
8069
8070 MVT PVT = getPointerTy();
8071 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
8072 "Invalid Pointer Size!");
8073 // For v = setjmp(buf), we generate
8074 //
8075 // thisMBB:
8076 // SjLjSetup mainMBB
8077 // bl mainMBB
8078 // v_restore = 1
8079 // b sinkMBB
8080 //
8081 // mainMBB:
8082 // buf[LabelOffset] = LR
8083 // v_main = 0
8084 //
8085 // sinkMBB:
8086 // v = phi(main, restore)
8087 //
8088
8089 MachineBasicBlock *thisMBB = MBB;
8090 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
8091 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
8092 MF->insert(I, mainMBB);
8093 MF->insert(I, sinkMBB);
8094
8095 MachineInstrBuilder MIB;
8096
8097 // Transfer the remainder of BB and its successor edges to sinkMBB.
8098 sinkMBB->splice(sinkMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008099 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Hal Finkel756810f2013-03-21 21:37:52 +00008100 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
8101
8102 // Note that the structure of the jmp_buf used here is not compatible
8103 // with that used by libc, and is not designed to be. Specifically, it
8104 // stores only those 'reserved' registers that LLVM does not otherwise
8105 // understand how to spill. Also, by convention, by the time this
8106 // intrinsic is called, Clang has already stored the frame address in the
8107 // first slot of the buffer and stack address in the third. Following the
8108 // X86 target code, we'll store the jump address in the second slot. We also
8109 // need to save the TOC pointer (R2) to handle jumps between shared
8110 // libraries, and that will be stored in the fourth slot. The thread
8111 // identifier (R13) is not affected.
8112
8113 // thisMBB:
8114 const int64_t LabelOffset = 1 * PVT.getStoreSize();
8115 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00008116 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00008117
8118 // Prepare IP either in reg.
8119 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
8120 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
8121 unsigned BufReg = MI->getOperand(1).getReg();
8122
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008123 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI()) {
Hal Finkele6698d52015-02-01 15:03:28 +00008124 setUsesTOCBasePtr(*MBB->getParent());
Hal Finkel756810f2013-03-21 21:37:52 +00008125 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
8126 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008127 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008128 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00008129 MIB.setMemRefs(MMOBegin, MMOEnd);
8130 }
8131
Hal Finkelf05d6c72013-07-17 23:50:51 +00008132 // Naked functions never have a base pointer, and so we use r1. For all
8133 // other functions, this decision must be delayed until during PEI.
8134 unsigned BaseReg;
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +00008135 if (MF->getFunction()->hasFnAttribute(Attribute::Naked))
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008136 BaseReg = Subtarget.isPPC64() ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00008137 else
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008138 BaseReg = Subtarget.isPPC64() ? PPC::BP8 : PPC::BP;
Hal Finkelf05d6c72013-07-17 23:50:51 +00008139
8140 MIB = BuildMI(*thisMBB, MI, DL,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008141 TII->get(Subtarget.isPPC64() ? PPC::STD : PPC::STW))
Eric Christophercccae792015-01-30 22:02:31 +00008142 .addReg(BaseReg)
8143 .addImm(BPOffset)
8144 .addReg(BufReg);
Hal Finkelf05d6c72013-07-17 23:50:51 +00008145 MIB.setMemRefs(MMOBegin, MMOEnd);
8146
Hal Finkel756810f2013-03-21 21:37:52 +00008147 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00008148 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Eric Christophercccae792015-01-30 22:02:31 +00008149 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo();
Bill Wendling5e7656b2013-06-07 07:55:53 +00008150 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00008151
8152 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
8153
8154 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
8155 .addMBB(mainMBB);
8156 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
8157
8158 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
8159 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
8160
8161 // mainMBB:
8162 // mainDstReg = 0
Eric Christophercccae792015-01-30 22:02:31 +00008163 MIB =
8164 BuildMI(mainMBB, DL,
8165 TII->get(Subtarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
Hal Finkel756810f2013-03-21 21:37:52 +00008166
8167 // Store IP
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008168 if (Subtarget.isPPC64()) {
Hal Finkel756810f2013-03-21 21:37:52 +00008169 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
8170 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008171 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008172 .addReg(BufReg);
8173 } else {
8174 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
8175 .addReg(LabelReg)
8176 .addImm(LabelOffset)
8177 .addReg(BufReg);
8178 }
8179
8180 MIB.setMemRefs(MMOBegin, MMOEnd);
8181
8182 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
8183 mainMBB->addSuccessor(sinkMBB);
8184
8185 // sinkMBB:
8186 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
8187 TII->get(PPC::PHI), DstReg)
8188 .addReg(mainDstReg).addMBB(mainMBB)
8189 .addReg(restoreDstReg).addMBB(thisMBB);
8190
8191 MI->eraseFromParent();
8192 return sinkMBB;
8193}
8194
8195MachineBasicBlock *
8196PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
8197 MachineBasicBlock *MBB) const {
8198 DebugLoc DL = MI->getDebugLoc();
Eric Christophercccae792015-01-30 22:02:31 +00008199 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00008200
8201 MachineFunction *MF = MBB->getParent();
8202 MachineRegisterInfo &MRI = MF->getRegInfo();
8203
8204 // Memory Reference
8205 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
8206 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
8207
8208 MVT PVT = getPointerTy();
8209 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
8210 "Invalid Pointer Size!");
8211
8212 const TargetRegisterClass *RC =
8213 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
8214 unsigned Tmp = MRI.createVirtualRegister(RC);
8215 // Since FP is only updated here but NOT referenced, it's treated as GPR.
8216 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
8217 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Eric Christophercccae792015-01-30 22:02:31 +00008218 unsigned BP =
8219 (PVT == MVT::i64)
8220 ? PPC::X30
8221 : (Subtarget.isSVR4ABI() &&
8222 MF->getTarget().getRelocationModel() == Reloc::PIC_
8223 ? PPC::R29
8224 : PPC::R30);
Hal Finkel756810f2013-03-21 21:37:52 +00008225
8226 MachineInstrBuilder MIB;
8227
8228 const int64_t LabelOffset = 1 * PVT.getStoreSize();
8229 const int64_t SPOffset = 2 * PVT.getStoreSize();
8230 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00008231 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00008232
8233 unsigned BufReg = MI->getOperand(0).getReg();
8234
8235 // Reload FP (the jumped-to function may not have had a
8236 // frame pointer, and if so, then its r31 will be restored
8237 // as necessary).
8238 if (PVT == MVT::i64) {
8239 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
8240 .addImm(0)
8241 .addReg(BufReg);
8242 } else {
8243 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
8244 .addImm(0)
8245 .addReg(BufReg);
8246 }
8247 MIB.setMemRefs(MMOBegin, MMOEnd);
8248
8249 // Reload IP
8250 if (PVT == MVT::i64) {
8251 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008252 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008253 .addReg(BufReg);
8254 } else {
8255 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
8256 .addImm(LabelOffset)
8257 .addReg(BufReg);
8258 }
8259 MIB.setMemRefs(MMOBegin, MMOEnd);
8260
8261 // Reload SP
8262 if (PVT == MVT::i64) {
8263 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008264 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008265 .addReg(BufReg);
8266 } else {
8267 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
8268 .addImm(SPOffset)
8269 .addReg(BufReg);
8270 }
8271 MIB.setMemRefs(MMOBegin, MMOEnd);
8272
Hal Finkelf05d6c72013-07-17 23:50:51 +00008273 // Reload BP
8274 if (PVT == MVT::i64) {
8275 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
8276 .addImm(BPOffset)
8277 .addReg(BufReg);
8278 } else {
8279 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
8280 .addImm(BPOffset)
8281 .addReg(BufReg);
8282 }
8283 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00008284
8285 // Reload TOC
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008286 if (PVT == MVT::i64 && Subtarget.isSVR4ABI()) {
Hal Finkele6698d52015-02-01 15:03:28 +00008287 setUsesTOCBasePtr(*MBB->getParent());
Hal Finkel756810f2013-03-21 21:37:52 +00008288 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00008289 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00008290 .addReg(BufReg);
8291
8292 MIB.setMemRefs(MMOBegin, MMOEnd);
8293 }
8294
8295 // Jump
8296 BuildMI(*MBB, MI, DL,
8297 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
8298 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
8299
8300 MI->eraseFromParent();
8301 return MBB;
8302}
8303
Dale Johannesena32affb2008-08-28 17:53:09 +00008304MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00008305PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00008306 MachineBasicBlock *BB) const {
Hal Finkel934361a2015-01-14 01:07:51 +00008307 if (MI->getOpcode() == TargetOpcode::STACKMAP ||
Hal Finkelaf519932015-01-19 07:20:27 +00008308 MI->getOpcode() == TargetOpcode::PATCHPOINT) {
8309 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI() &&
8310 MI->getOpcode() == TargetOpcode::PATCHPOINT) {
8311 // Call lowering should have added an r2 operand to indicate a dependence
8312 // on the TOC base pointer value. It can't however, because there is no
8313 // way to mark the dependence as implicit there, and so the stackmap code
8314 // will confuse it with a regular operand. Instead, add the dependence
8315 // here.
Hal Finkele6698d52015-02-01 15:03:28 +00008316 setUsesTOCBasePtr(*BB->getParent());
Hal Finkelaf519932015-01-19 07:20:27 +00008317 MI->addOperand(MachineOperand::CreateReg(PPC::X2, false, true));
8318 }
8319
Hal Finkel934361a2015-01-14 01:07:51 +00008320 return emitPatchPoint(MI, BB);
Hal Finkelaf519932015-01-19 07:20:27 +00008321 }
Hal Finkel934361a2015-01-14 01:07:51 +00008322
Hal Finkel756810f2013-03-21 21:37:52 +00008323 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
8324 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
8325 return emitEHSjLjSetJmp(MI, BB);
8326 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
8327 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
8328 return emitEHSjLjLongJmp(MI, BB);
8329 }
8330
Eric Christophercccae792015-01-30 22:02:31 +00008331 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00008332
8333 // To "insert" these instructions we actually have to insert their
8334 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00008335 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00008336 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00008337 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00008338
Dan Gohman3b460302008-07-07 23:14:23 +00008339 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00008340
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008341 if (Subtarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Eric Christophercccae792015-01-30 22:02:31 +00008342 MI->getOpcode() == PPC::SELECT_CC_I8 ||
8343 MI->getOpcode() == PPC::SELECT_I4 ||
8344 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00008345 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00008346 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
8347 MI->getOpcode() == PPC::SELECT_CC_I8)
8348 Cond.push_back(MI->getOperand(4));
8349 else
8350 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00008351 Cond.push_back(MI->getOperand(1));
8352
Hal Finkel460e94d2012-06-22 23:10:08 +00008353 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00008354 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
8355 Cond, MI->getOperand(2).getReg(),
8356 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00008357 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
8358 MI->getOpcode() == PPC::SELECT_CC_I8 ||
8359 MI->getOpcode() == PPC::SELECT_CC_F4 ||
8360 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008361 MI->getOpcode() == PPC::SELECT_CC_QFRC ||
8362 MI->getOpcode() == PPC::SELECT_CC_QSRC ||
8363 MI->getOpcode() == PPC::SELECT_CC_QBRC ||
Hal Finkel940ab932014-02-28 00:27:01 +00008364 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00008365 MI->getOpcode() == PPC::SELECT_CC_VSFRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008366 MI->getOpcode() == PPC::SELECT_CC_VSRC ||
Hal Finkel940ab932014-02-28 00:27:01 +00008367 MI->getOpcode() == PPC::SELECT_I4 ||
8368 MI->getOpcode() == PPC::SELECT_I8 ||
8369 MI->getOpcode() == PPC::SELECT_F4 ||
8370 MI->getOpcode() == PPC::SELECT_F8 ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008371 MI->getOpcode() == PPC::SELECT_QFRC ||
8372 MI->getOpcode() == PPC::SELECT_QSRC ||
8373 MI->getOpcode() == PPC::SELECT_QBRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008374 MI->getOpcode() == PPC::SELECT_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00008375 MI->getOpcode() == PPC::SELECT_VSFRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008376 MI->getOpcode() == PPC::SELECT_VSRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00008377 // The incoming instruction knows the destination vreg to set, the
8378 // condition code register to branch on, the true/false values to
8379 // select between, and a branch opcode to use.
8380
8381 // thisMBB:
8382 // ...
8383 // TrueVal = ...
8384 // cmpTY ccX, r1, r2
8385 // bCC copy1MBB
8386 // fallthrough --> copy0MBB
8387 MachineBasicBlock *thisMBB = BB;
8388 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8389 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008390 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00008391 F->insert(It, copy0MBB);
8392 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008393
8394 // Transfer the remainder of BB and its successor edges to sinkMBB.
8395 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008396 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008397 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8398
Evan Cheng32e376f2008-07-12 02:23:19 +00008399 // Next, add the true and fallthrough blocks as its successors.
8400 BB->addSuccessor(copy0MBB);
8401 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008402
Hal Finkel940ab932014-02-28 00:27:01 +00008403 if (MI->getOpcode() == PPC::SELECT_I4 ||
8404 MI->getOpcode() == PPC::SELECT_I8 ||
8405 MI->getOpcode() == PPC::SELECT_F4 ||
8406 MI->getOpcode() == PPC::SELECT_F8 ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008407 MI->getOpcode() == PPC::SELECT_QFRC ||
8408 MI->getOpcode() == PPC::SELECT_QSRC ||
8409 MI->getOpcode() == PPC::SELECT_QBRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008410 MI->getOpcode() == PPC::SELECT_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00008411 MI->getOpcode() == PPC::SELECT_VSFRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00008412 MI->getOpcode() == PPC::SELECT_VSRC) {
Hal Finkel940ab932014-02-28 00:27:01 +00008413 BuildMI(BB, dl, TII->get(PPC::BC))
8414 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
8415 } else {
8416 unsigned SelectPred = MI->getOperand(4).getImm();
8417 BuildMI(BB, dl, TII->get(PPC::BCC))
8418 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
8419 }
Dan Gohman34396292010-07-06 20:24:04 +00008420
Evan Cheng32e376f2008-07-12 02:23:19 +00008421 // copy0MBB:
8422 // %FalseValue = ...
8423 // # fallthrough to sinkMBB
8424 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008425
Evan Cheng32e376f2008-07-12 02:23:19 +00008426 // Update machine-CFG edges
8427 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008428
Evan Cheng32e376f2008-07-12 02:23:19 +00008429 // sinkMBB:
8430 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8431 // ...
8432 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00008433 BuildMI(*BB, BB->begin(), dl,
8434 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00008435 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
8436 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Hal Finkelbbdee932014-12-02 22:01:00 +00008437 } else if (MI->getOpcode() == PPC::ReadTB) {
8438 // To read the 64-bit time-base register on a 32-bit target, we read the
8439 // two halves. Should the counter have wrapped while it was being read, we
8440 // need to try again.
8441 // ...
8442 // readLoop:
8443 // mfspr Rx,TBU # load from TBU
8444 // mfspr Ry,TB # load from TB
8445 // mfspr Rz,TBU # load from TBU
8446 // cmpw crX,Rx,Rz # check if ‘old’=’new’
8447 // bne readLoop # branch if they're not equal
8448 // ...
8449
8450 MachineBasicBlock *readMBB = F->CreateMachineBasicBlock(LLVM_BB);
8451 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8452 DebugLoc dl = MI->getDebugLoc();
8453 F->insert(It, readMBB);
8454 F->insert(It, sinkMBB);
8455
8456 // Transfer the remainder of BB and its successor edges to sinkMBB.
8457 sinkMBB->splice(sinkMBB->begin(), BB,
8458 std::next(MachineBasicBlock::iterator(MI)), BB->end());
8459 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8460
8461 BB->addSuccessor(readMBB);
8462 BB = readMBB;
8463
8464 MachineRegisterInfo &RegInfo = F->getRegInfo();
8465 unsigned ReadAgainReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
8466 unsigned LoReg = MI->getOperand(0).getReg();
8467 unsigned HiReg = MI->getOperand(1).getReg();
8468
8469 BuildMI(BB, dl, TII->get(PPC::MFSPR), HiReg).addImm(269);
8470 BuildMI(BB, dl, TII->get(PPC::MFSPR), LoReg).addImm(268);
8471 BuildMI(BB, dl, TII->get(PPC::MFSPR), ReadAgainReg).addImm(269);
8472
8473 unsigned CmpReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
8474
8475 BuildMI(BB, dl, TII->get(PPC::CMPW), CmpReg)
8476 .addReg(HiReg).addReg(ReadAgainReg);
8477 BuildMI(BB, dl, TII->get(PPC::BCC))
8478 .addImm(PPC::PRED_NE).addReg(CmpReg).addMBB(readMBB);
8479
8480 BB->addSuccessor(readMBB);
8481 BB->addSuccessor(sinkMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00008482 }
Dale Johannesena32affb2008-08-28 17:53:09 +00008483 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
8484 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
8485 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
8486 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008487 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008488 BB = EmitAtomicBinary(MI, BB, 4, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008489 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008490 BB = EmitAtomicBinary(MI, BB, 8, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008491
8492 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
8493 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
8494 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
8495 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008496 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008497 BB = EmitAtomicBinary(MI, BB, 4, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008498 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008499 BB = EmitAtomicBinary(MI, BB, 8, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008500
8501 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
8502 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
8503 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
8504 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008505 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008506 BB = EmitAtomicBinary(MI, BB, 4, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008507 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008508 BB = EmitAtomicBinary(MI, BB, 8, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008509
8510 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
8511 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
8512 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
8513 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008514 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008515 BB = EmitAtomicBinary(MI, BB, 4, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008516 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008517 BB = EmitAtomicBinary(MI, BB, 8, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008518
8519 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00008520 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::NAND);
Dale Johannesena32affb2008-08-28 17:53:09 +00008521 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00008522 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008523 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008524 BB = EmitAtomicBinary(MI, BB, 4, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008525 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008526 BB = EmitAtomicBinary(MI, BB, 8, PPC::NAND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008527
8528 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
8529 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
8530 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
8531 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008532 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008533 BB = EmitAtomicBinary(MI, BB, 4, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00008534 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008535 BB = EmitAtomicBinary(MI, BB, 8, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00008536
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008537 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
8538 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
8539 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
8540 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
8541 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008542 BB = EmitAtomicBinary(MI, BB, 4, 0);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008543 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008544 BB = EmitAtomicBinary(MI, BB, 8, 0);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00008545
Evan Cheng32e376f2008-07-12 02:23:19 +00008546 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008547 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64 ||
8548 (Subtarget.hasPartwordAtomics() &&
8549 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8) ||
8550 (Subtarget.hasPartwordAtomics() &&
8551 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16)) {
Evan Cheng32e376f2008-07-12 02:23:19 +00008552 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
8553
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008554 auto LoadMnemonic = PPC::LDARX;
8555 auto StoreMnemonic = PPC::STDCX;
8556 switch(MI->getOpcode()) {
8557 default:
8558 llvm_unreachable("Compare and swap of unknown size");
8559 case PPC::ATOMIC_CMP_SWAP_I8:
8560 LoadMnemonic = PPC::LBARX;
8561 StoreMnemonic = PPC::STBCX;
8562 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics.");
8563 break;
8564 case PPC::ATOMIC_CMP_SWAP_I16:
8565 LoadMnemonic = PPC::LHARX;
8566 StoreMnemonic = PPC::STHCX;
8567 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics.");
8568 break;
8569 case PPC::ATOMIC_CMP_SWAP_I32:
8570 LoadMnemonic = PPC::LWARX;
8571 StoreMnemonic = PPC::STWCX;
8572 break;
8573 case PPC::ATOMIC_CMP_SWAP_I64:
8574 LoadMnemonic = PPC::LDARX;
8575 StoreMnemonic = PPC::STDCX;
8576 break;
8577 }
Evan Cheng32e376f2008-07-12 02:23:19 +00008578 unsigned dest = MI->getOperand(0).getReg();
8579 unsigned ptrA = MI->getOperand(1).getReg();
8580 unsigned ptrB = MI->getOperand(2).getReg();
8581 unsigned oldval = MI->getOperand(3).getReg();
8582 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00008583 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00008584
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008585 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
8586 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
8587 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00008588 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008589 F->insert(It, loop1MBB);
8590 F->insert(It, loop2MBB);
8591 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00008592 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008593 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008594 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008595 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00008596
8597 // thisMBB:
8598 // ...
8599 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008600 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00008601
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008602 // loop1MBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008603 // l[bhwd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008604 // cmp[wd] dest, oldval
8605 // bne- midMBB
8606 // loop2MBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008607 // st[bhwd]cx. newval, ptr
Evan Cheng32e376f2008-07-12 02:23:19 +00008608 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008609 // b exitBB
8610 // midMBB:
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008611 // st[bhwd]cx. dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008612 // exitBB:
8613 BB = loop1MBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008614 BuildMI(BB, dl, TII->get(LoadMnemonic), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00008615 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008616 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00008617 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008618 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008619 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
8620 BB->addSuccessor(loop2MBB);
8621 BB->addSuccessor(midMBB);
8622
8623 BB = loop2MBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008624 BuildMI(BB, dl, TII->get(StoreMnemonic))
Evan Cheng32e376f2008-07-12 02:23:19 +00008625 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008626 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008627 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008628 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008629 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00008630 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008631
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008632 BB = midMBB;
Nemanja Ivanovic0adf26b2015-03-10 20:51:07 +00008633 BuildMI(BB, dl, TII->get(StoreMnemonic))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00008634 .addReg(dest).addReg(ptrA).addReg(ptrB);
8635 BB->addSuccessor(exitMBB);
8636
Evan Cheng32e376f2008-07-12 02:23:19 +00008637 // exitMBB:
8638 // ...
8639 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00008640 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
8641 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
8642 // We must use 64-bit registers for addresses when targeting 64-bit,
8643 // since we're actually doing arithmetic on them. Other registers
8644 // can be 32-bit.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008645 bool is64bit = Subtarget.isPPC64();
Dale Johannesen340d2642008-08-30 00:08:53 +00008646 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
8647
8648 unsigned dest = MI->getOperand(0).getReg();
8649 unsigned ptrA = MI->getOperand(1).getReg();
8650 unsigned ptrB = MI->getOperand(2).getReg();
8651 unsigned oldval = MI->getOperand(3).getReg();
8652 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00008653 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00008654
8655 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
8656 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
8657 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
8658 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
8659 F->insert(It, loop1MBB);
8660 F->insert(It, loop2MBB);
8661 F->insert(It, midMBB);
8662 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008663 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008664 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008665 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00008666
8667 MachineRegisterInfo &RegInfo = F->getRegInfo();
Craig Topper61e88f42014-11-21 05:58:21 +00008668 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass
8669 : &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00008670 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
8671 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
8672 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
8673 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
8674 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
8675 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
8676 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
8677 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
8678 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
8679 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
8680 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
8681 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
8682 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
8683 unsigned Ptr1Reg;
8684 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00008685 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00008686 // thisMBB:
8687 // ...
8688 // fallthrough --> loopMBB
8689 BB->addSuccessor(loop1MBB);
8690
8691 // The 4-byte load must be aligned, while a char or short may be
8692 // anywhere in the word. Hence all this nasty bookkeeping code.
8693 // add ptr1, ptrA, ptrB [copy if ptrA==0]
8694 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00008695 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00008696 // rlwinm ptr, ptr1, 0, 0, 29
8697 // slw newval2, newval, shift
8698 // slw oldval2, oldval,shift
8699 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
8700 // slw mask, mask2, shift
8701 // and newval3, newval2, mask
8702 // and oldval3, oldval2, mask
8703 // loop1MBB:
8704 // lwarx tmpDest, ptr
8705 // and tmp, tmpDest, mask
8706 // cmpw tmp, oldval3
8707 // bne- midMBB
8708 // loop2MBB:
8709 // andc tmp2, tmpDest, mask
8710 // or tmp4, tmp2, newval3
8711 // stwcx. tmp4, ptr
8712 // bne- loop1MBB
8713 // b exitBB
8714 // midMBB:
8715 // stwcx. tmpDest, ptr
8716 // exitBB:
8717 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008718 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00008719 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008720 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008721 .addReg(ptrA).addReg(ptrB);
8722 } else {
8723 Ptr1Reg = ptrB;
8724 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008725 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008726 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008727 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008728 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
8729 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008730 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008731 .addReg(Ptr1Reg).addImm(0).addImm(61);
8732 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00008733 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008734 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008735 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008736 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008737 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008738 .addReg(oldval).addReg(ShiftReg);
8739 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00008740 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00008741 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00008742 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
8743 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
8744 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00008745 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00008746 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008747 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008748 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008749 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008750 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00008751 .addReg(OldVal2Reg).addReg(MaskReg);
8752
8753 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00008754 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008755 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008756 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
8757 .addReg(TmpDestReg).addReg(MaskReg);
8758 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00008759 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008760 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00008761 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
8762 BB->addSuccessor(loop2MBB);
8763 BB->addSuccessor(midMBB);
8764
8765 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00008766 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
8767 .addReg(TmpDestReg).addReg(MaskReg);
8768 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
8769 .addReg(Tmp2Reg).addReg(NewVal3Reg);
8770 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008771 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008772 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00008773 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00008774 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00008775 BB->addSuccessor(loop1MBB);
8776 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008777
Dale Johannesen340d2642008-08-30 00:08:53 +00008778 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00008779 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00008780 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00008781 BB->addSuccessor(exitMBB);
8782
8783 // exitMBB:
8784 // ...
8785 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00008786 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
8787 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00008788 } else if (MI->getOpcode() == PPC::FADDrtz) {
8789 // This pseudo performs an FADD with rounding mode temporarily forced
8790 // to round-to-zero. We emit this via custom inserter since the FPSCR
8791 // is not modeled at the SelectionDAG level.
8792 unsigned Dest = MI->getOperand(0).getReg();
8793 unsigned Src1 = MI->getOperand(1).getReg();
8794 unsigned Src2 = MI->getOperand(2).getReg();
8795 DebugLoc dl = MI->getDebugLoc();
8796
8797 MachineRegisterInfo &RegInfo = F->getRegInfo();
8798 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
8799
8800 // Save FPSCR value.
8801 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
8802
8803 // Set rounding mode to round-to-zero.
8804 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
8805 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
8806
8807 // Perform addition.
8808 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
8809
8810 // Restore FPSCR value.
Hal Finkel64202162015-01-15 01:00:53 +00008811 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSFb)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00008812 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
8813 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
8814 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
8815 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
8816 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
8817 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
8818 PPC::ANDIo8 : PPC::ANDIo;
8819 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
8820 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
8821
8822 MachineRegisterInfo &RegInfo = F->getRegInfo();
8823 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
8824 &PPC::GPRCRegClass :
8825 &PPC::G8RCRegClass);
8826
8827 DebugLoc dl = MI->getDebugLoc();
8828 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
8829 .addReg(MI->getOperand(1).getReg()).addImm(1);
8830 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
8831 MI->getOperand(0).getReg())
8832 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Kit Barton535e69d2015-03-25 19:36:23 +00008833 } else if (MI->getOpcode() == PPC::TCHECK_RET) {
8834 DebugLoc Dl = MI->getDebugLoc();
8835 MachineRegisterInfo &RegInfo = F->getRegInfo();
8836 unsigned CRReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
8837 BuildMI(*BB, MI, Dl, TII->get(PPC::TCHECK), CRReg);
8838 return BB;
Dale Johannesen340d2642008-08-30 00:08:53 +00008839 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008840 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00008841 }
Chris Lattner9b577f12005-08-26 21:23:58 +00008842
Dan Gohman34396292010-07-06 20:24:04 +00008843 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00008844 return BB;
8845}
8846
Chris Lattner4211ca92006-04-14 06:01:58 +00008847//===----------------------------------------------------------------------===//
8848// Target Optimization Hooks
8849//===----------------------------------------------------------------------===//
8850
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008851SDValue PPCTargetLowering::getRsqrtEstimate(SDValue Operand,
8852 DAGCombinerInfo &DCI,
Sanjay Patel957efc232014-10-24 17:02:16 +00008853 unsigned &RefinementSteps,
8854 bool &UseOneConstNR) const {
Sanjay Patelbdf1e382014-09-26 23:01:47 +00008855 EVT VT = Operand.getValueType();
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008856 if ((VT == MVT::f32 && Subtarget.hasFRSQRTES()) ||
Eric Christophercccae792015-01-30 22:02:31 +00008857 (VT == MVT::f64 && Subtarget.hasFRSQRTE()) ||
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008858 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008859 (VT == MVT::v2f64 && Subtarget.hasVSX()) ||
8860 (VT == MVT::v4f32 && Subtarget.hasQPX()) ||
8861 (VT == MVT::v4f64 && Subtarget.hasQPX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00008862 // Convergence is quadratic, so we essentially double the number of digits
Sanjay Patelbdf1e382014-09-26 23:01:47 +00008863 // correct after every iteration. For both FRE and FRSQRTE, the minimum
8864 // architected relative accuracy is 2^-5. When hasRecipPrec(), this is
8865 // 2^-14. IEEE float has 23 digits and double has 52 digits.
8866 RefinementSteps = Subtarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00008867 if (VT.getScalarType() == MVT::f64)
Sanjay Patelbdf1e382014-09-26 23:01:47 +00008868 ++RefinementSteps;
Sanjay Patel957efc232014-10-24 17:02:16 +00008869 UseOneConstNR = true;
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008870 return DCI.DAG.getNode(PPCISD::FRSQRTE, SDLoc(Operand), VT, Operand);
Hal Finkel2e103312013-04-03 04:01:11 +00008871 }
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008872 return SDValue();
8873}
8874
8875SDValue PPCTargetLowering::getRecipEstimate(SDValue Operand,
8876 DAGCombinerInfo &DCI,
8877 unsigned &RefinementSteps) const {
8878 EVT VT = Operand.getValueType();
8879 if ((VT == MVT::f32 && Subtarget.hasFRES()) ||
Eric Christophercccae792015-01-30 22:02:31 +00008880 (VT == MVT::f64 && Subtarget.hasFRE()) ||
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008881 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
Hal Finkelc93a9a22015-02-25 01:06:45 +00008882 (VT == MVT::v2f64 && Subtarget.hasVSX()) ||
8883 (VT == MVT::v4f32 && Subtarget.hasQPX()) ||
8884 (VT == MVT::v4f64 && Subtarget.hasQPX())) {
Sanjay Patel8fde95c2014-09-30 20:28:48 +00008885 // Convergence is quadratic, so we essentially double the number of digits
8886 // correct after every iteration. For both FRE and FRSQRTE, the minimum
8887 // architected relative accuracy is 2^-5. When hasRecipPrec(), this is
8888 // 2^-14. IEEE float has 23 digits and double has 52 digits.
8889 RefinementSteps = Subtarget.hasRecipPrec() ? 1 : 3;
8890 if (VT.getScalarType() == MVT::f64)
8891 ++RefinementSteps;
8892 return DCI.DAG.getNode(PPCISD::FRE, SDLoc(Operand), VT, Operand);
8893 }
8894 return SDValue();
Hal Finkel2e103312013-04-03 04:01:11 +00008895}
8896
Hal Finkel360f2132014-11-24 23:45:21 +00008897bool PPCTargetLowering::combineRepeatedFPDivisors(unsigned NumUsers) const {
8898 // Note: This functionality is used only when unsafe-fp-math is enabled, and
8899 // on cores with reciprocal estimates (which are used when unsafe-fp-math is
8900 // enabled for division), this functionality is redundant with the default
8901 // combiner logic (once the division -> reciprocal/multiply transformation
8902 // has taken place). As a result, this matters more for older cores than for
8903 // newer ones.
8904
8905 // Combine multiple FDIVs with the same divisor into multiple FMULs by the
8906 // reciprocal if there are two or more FDIVs (for embedded cores with only
8907 // one FP pipeline) for three or more FDIVs (for generic OOO cores).
8908 switch (Subtarget.getDarwinDirective()) {
8909 default:
8910 return NumUsers > 2;
8911 case PPC::DIR_440:
8912 case PPC::DIR_A2:
8913 case PPC::DIR_E500mc:
8914 case PPC::DIR_E5500:
8915 return NumUsers > 1;
8916 }
8917}
8918
Hal Finkel3604bf72014-08-01 01:02:01 +00008919static bool isConsecutiveLSLoc(SDValue Loc, EVT VT, LSBaseSDNode *Base,
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00008920 unsigned Bytes, int Dist,
8921 SelectionDAG &DAG) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00008922 if (VT.getSizeInBits() / 8 != Bytes)
8923 return false;
8924
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00008925 SDValue BaseLoc = Base->getBasePtr();
8926 if (Loc.getOpcode() == ISD::FrameIndex) {
8927 if (BaseLoc.getOpcode() != ISD::FrameIndex)
8928 return false;
8929 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8930 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
8931 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
8932 int FS = MFI->getObjectSize(FI);
8933 int BFS = MFI->getObjectSize(BFI);
8934 if (FS != BFS || FS != (int)Bytes) return false;
8935 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
8936 }
8937
8938 // Handle X+C
8939 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
8940 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
8941 return true;
8942
8943 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Craig Topper062a2ba2014-04-25 05:30:21 +00008944 const GlobalValue *GV1 = nullptr;
8945 const GlobalValue *GV2 = nullptr;
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00008946 int64_t Offset1 = 0;
8947 int64_t Offset2 = 0;
8948 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
8949 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
8950 if (isGA1 && isGA2 && GV1 == GV2)
8951 return Offset1 == (Offset2 + Dist*Bytes);
8952 return false;
8953}
8954
Hal Finkel3604bf72014-08-01 01:02:01 +00008955// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
8956// not enforce equality of the chain operands.
8957static bool isConsecutiveLS(SDNode *N, LSBaseSDNode *Base,
8958 unsigned Bytes, int Dist,
8959 SelectionDAG &DAG) {
8960 if (LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(N)) {
8961 EVT VT = LS->getMemoryVT();
8962 SDValue Loc = LS->getBasePtr();
8963 return isConsecutiveLSLoc(Loc, VT, Base, Bytes, Dist, DAG);
8964 }
8965
8966 if (N->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
8967 EVT VT;
8968 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8969 default: return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00008970 case Intrinsic::ppc_qpx_qvlfd:
8971 case Intrinsic::ppc_qpx_qvlfda:
8972 VT = MVT::v4f64;
8973 break;
8974 case Intrinsic::ppc_qpx_qvlfs:
8975 case Intrinsic::ppc_qpx_qvlfsa:
8976 VT = MVT::v4f32;
8977 break;
8978 case Intrinsic::ppc_qpx_qvlfcd:
8979 case Intrinsic::ppc_qpx_qvlfcda:
8980 VT = MVT::v2f64;
8981 break;
8982 case Intrinsic::ppc_qpx_qvlfcs:
8983 case Intrinsic::ppc_qpx_qvlfcsa:
8984 VT = MVT::v2f32;
8985 break;
8986 case Intrinsic::ppc_qpx_qvlfiwa:
8987 case Intrinsic::ppc_qpx_qvlfiwz:
Hal Finkel3604bf72014-08-01 01:02:01 +00008988 case Intrinsic::ppc_altivec_lvx:
8989 case Intrinsic::ppc_altivec_lvxl:
Bill Schmidt72954782014-11-12 04:19:40 +00008990 case Intrinsic::ppc_vsx_lxvw4x:
Hal Finkel3604bf72014-08-01 01:02:01 +00008991 VT = MVT::v4i32;
8992 break;
Bill Schmidt72954782014-11-12 04:19:40 +00008993 case Intrinsic::ppc_vsx_lxvd2x:
8994 VT = MVT::v2f64;
8995 break;
Hal Finkel3604bf72014-08-01 01:02:01 +00008996 case Intrinsic::ppc_altivec_lvebx:
8997 VT = MVT::i8;
8998 break;
8999 case Intrinsic::ppc_altivec_lvehx:
9000 VT = MVT::i16;
9001 break;
9002 case Intrinsic::ppc_altivec_lvewx:
9003 VT = MVT::i32;
9004 break;
9005 }
9006
9007 return isConsecutiveLSLoc(N->getOperand(2), VT, Base, Bytes, Dist, DAG);
9008 }
9009
9010 if (N->getOpcode() == ISD::INTRINSIC_VOID) {
9011 EVT VT;
9012 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9013 default: return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00009014 case Intrinsic::ppc_qpx_qvstfd:
9015 case Intrinsic::ppc_qpx_qvstfda:
9016 VT = MVT::v4f64;
9017 break;
9018 case Intrinsic::ppc_qpx_qvstfs:
9019 case Intrinsic::ppc_qpx_qvstfsa:
9020 VT = MVT::v4f32;
9021 break;
9022 case Intrinsic::ppc_qpx_qvstfcd:
9023 case Intrinsic::ppc_qpx_qvstfcda:
9024 VT = MVT::v2f64;
9025 break;
9026 case Intrinsic::ppc_qpx_qvstfcs:
9027 case Intrinsic::ppc_qpx_qvstfcsa:
9028 VT = MVT::v2f32;
9029 break;
9030 case Intrinsic::ppc_qpx_qvstfiw:
9031 case Intrinsic::ppc_qpx_qvstfiwa:
Hal Finkel3604bf72014-08-01 01:02:01 +00009032 case Intrinsic::ppc_altivec_stvx:
9033 case Intrinsic::ppc_altivec_stvxl:
Bill Schmidt72954782014-11-12 04:19:40 +00009034 case Intrinsic::ppc_vsx_stxvw4x:
Hal Finkel3604bf72014-08-01 01:02:01 +00009035 VT = MVT::v4i32;
9036 break;
Bill Schmidt72954782014-11-12 04:19:40 +00009037 case Intrinsic::ppc_vsx_stxvd2x:
9038 VT = MVT::v2f64;
9039 break;
Hal Finkel3604bf72014-08-01 01:02:01 +00009040 case Intrinsic::ppc_altivec_stvebx:
9041 VT = MVT::i8;
9042 break;
9043 case Intrinsic::ppc_altivec_stvehx:
9044 VT = MVT::i16;
9045 break;
9046 case Intrinsic::ppc_altivec_stvewx:
9047 VT = MVT::i32;
9048 break;
9049 }
9050
9051 return isConsecutiveLSLoc(N->getOperand(3), VT, Base, Bytes, Dist, DAG);
9052 }
9053
9054 return false;
9055}
9056
Hal Finkel7d8a6912013-05-26 18:08:30 +00009057// Return true is there is a nearyby consecutive load to the one provided
9058// (regardless of alignment). We search up and down the chain, looking though
Matt Arsenault57e74d22014-07-29 00:02:40 +00009059// token factors and other loads (but nothing else). As a result, a true result
9060// indicates that it is safe to create a new consecutive load adjacent to the
9061// load provided.
Hal Finkel7d8a6912013-05-26 18:08:30 +00009062static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
9063 SDValue Chain = LD->getChain();
9064 EVT VT = LD->getMemoryVT();
9065
9066 SmallSet<SDNode *, 16> LoadRoots;
9067 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
9068 SmallSet<SDNode *, 16> Visited;
9069
9070 // First, search up the chain, branching to follow all token-factor operands.
9071 // If we find a consecutive load, then we're done, otherwise, record all
9072 // nodes just above the top-level loads and token factors.
9073 while (!Queue.empty()) {
9074 SDNode *ChainNext = Queue.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00009075 if (!Visited.insert(ChainNext).second)
Hal Finkel7d8a6912013-05-26 18:08:30 +00009076 continue;
9077
Hal Finkel3604bf72014-08-01 01:02:01 +00009078 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009079 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00009080 return true;
9081
9082 if (!Visited.count(ChainLD->getChain().getNode()))
9083 Queue.push_back(ChainLD->getChain().getNode());
9084 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
Craig Topper66e588b2014-06-29 00:40:57 +00009085 for (const SDUse &O : ChainNext->ops())
9086 if (!Visited.count(O.getNode()))
9087 Queue.push_back(O.getNode());
Hal Finkel7d8a6912013-05-26 18:08:30 +00009088 } else
9089 LoadRoots.insert(ChainNext);
9090 }
9091
9092 // Second, search down the chain, starting from the top-level nodes recorded
9093 // in the first phase. These top-level nodes are the nodes just above all
9094 // loads and token factors. Starting with their uses, recursively look though
9095 // all loads (just the chain uses) and token factors to find a consecutive
9096 // load.
9097 Visited.clear();
9098 Queue.clear();
9099
9100 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
9101 IE = LoadRoots.end(); I != IE; ++I) {
9102 Queue.push_back(*I);
9103
9104 while (!Queue.empty()) {
9105 SDNode *LoadRoot = Queue.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00009106 if (!Visited.insert(LoadRoot).second)
Hal Finkel7d8a6912013-05-26 18:08:30 +00009107 continue;
9108
Hal Finkel3604bf72014-08-01 01:02:01 +00009109 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00009110 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00009111 return true;
9112
9113 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
9114 UE = LoadRoot->use_end(); UI != UE; ++UI)
Hal Finkel3604bf72014-08-01 01:02:01 +00009115 if (((isa<MemSDNode>(*UI) &&
9116 cast<MemSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
Hal Finkel7d8a6912013-05-26 18:08:30 +00009117 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
9118 Queue.push_back(*UI);
9119 }
9120 }
9121
9122 return false;
9123}
9124
Hal Finkel940ab932014-02-28 00:27:01 +00009125SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
9126 DAGCombinerInfo &DCI) const {
9127 SelectionDAG &DAG = DCI.DAG;
9128 SDLoc dl(N);
9129
Eric Christophercccae792015-01-30 22:02:31 +00009130 assert(Subtarget.useCRBits() && "Expecting to be tracking CR bits");
Hal Finkel940ab932014-02-28 00:27:01 +00009131 // If we're tracking CR bits, we need to be careful that we don't have:
9132 // trunc(binary-ops(zext(x), zext(y)))
9133 // or
9134 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
9135 // such that we're unnecessarily moving things into GPRs when it would be
9136 // better to keep them in CR bits.
9137
9138 // Note that trunc here can be an actual i1 trunc, or can be the effective
9139 // truncation that comes from a setcc or select_cc.
9140 if (N->getOpcode() == ISD::TRUNCATE &&
9141 N->getValueType(0) != MVT::i1)
9142 return SDValue();
9143
9144 if (N->getOperand(0).getValueType() != MVT::i32 &&
9145 N->getOperand(0).getValueType() != MVT::i64)
9146 return SDValue();
9147
9148 if (N->getOpcode() == ISD::SETCC ||
9149 N->getOpcode() == ISD::SELECT_CC) {
9150 // If we're looking at a comparison, then we need to make sure that the
9151 // high bits (all except for the first) don't matter the result.
9152 ISD::CondCode CC =
9153 cast<CondCodeSDNode>(N->getOperand(
9154 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
9155 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
9156
9157 if (ISD::isSignedIntSetCC(CC)) {
9158 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
9159 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
9160 return SDValue();
9161 } else if (ISD::isUnsignedIntSetCC(CC)) {
9162 if (!DAG.MaskedValueIsZero(N->getOperand(0),
9163 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
9164 !DAG.MaskedValueIsZero(N->getOperand(1),
9165 APInt::getHighBitsSet(OpBits, OpBits-1)))
9166 return SDValue();
9167 } else {
9168 // This is neither a signed nor an unsigned comparison, just make sure
9169 // that the high bits are equal.
9170 APInt Op1Zero, Op1One;
9171 APInt Op2Zero, Op2One;
Jay Foada0653a32014-05-14 21:14:37 +00009172 DAG.computeKnownBits(N->getOperand(0), Op1Zero, Op1One);
9173 DAG.computeKnownBits(N->getOperand(1), Op2Zero, Op2One);
Hal Finkel940ab932014-02-28 00:27:01 +00009174
9175 // We don't really care about what is known about the first bit (if
9176 // anything), so clear it in all masks prior to comparing them.
9177 Op1Zero.clearBit(0); Op1One.clearBit(0);
9178 Op2Zero.clearBit(0); Op2One.clearBit(0);
9179
9180 if (Op1Zero != Op2Zero || Op1One != Op2One)
9181 return SDValue();
9182 }
9183 }
9184
9185 // We now know that the higher-order bits are irrelevant, we just need to
9186 // make sure that all of the intermediate operations are bit operations, and
9187 // all inputs are extensions.
9188 if (N->getOperand(0).getOpcode() != ISD::AND &&
9189 N->getOperand(0).getOpcode() != ISD::OR &&
9190 N->getOperand(0).getOpcode() != ISD::XOR &&
9191 N->getOperand(0).getOpcode() != ISD::SELECT &&
9192 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
9193 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
9194 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
9195 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
9196 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
9197 return SDValue();
9198
9199 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
9200 N->getOperand(1).getOpcode() != ISD::AND &&
9201 N->getOperand(1).getOpcode() != ISD::OR &&
9202 N->getOperand(1).getOpcode() != ISD::XOR &&
9203 N->getOperand(1).getOpcode() != ISD::SELECT &&
9204 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
9205 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
9206 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
9207 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
9208 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
9209 return SDValue();
9210
9211 SmallVector<SDValue, 4> Inputs;
9212 SmallVector<SDValue, 8> BinOps, PromOps;
9213 SmallPtrSet<SDNode *, 16> Visited;
9214
9215 for (unsigned i = 0; i < 2; ++i) {
9216 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9217 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9218 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
9219 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
9220 isa<ConstantSDNode>(N->getOperand(i)))
9221 Inputs.push_back(N->getOperand(i));
9222 else
9223 BinOps.push_back(N->getOperand(i));
9224
9225 if (N->getOpcode() == ISD::TRUNCATE)
9226 break;
9227 }
9228
9229 // Visit all inputs, collect all binary operations (and, or, xor and
9230 // select) that are all fed by extensions.
9231 while (!BinOps.empty()) {
9232 SDValue BinOp = BinOps.back();
9233 BinOps.pop_back();
9234
David Blaikie70573dc2014-11-19 07:49:26 +00009235 if (!Visited.insert(BinOp.getNode()).second)
Hal Finkel940ab932014-02-28 00:27:01 +00009236 continue;
9237
9238 PromOps.push_back(BinOp);
9239
9240 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
9241 // The condition of the select is not promoted.
9242 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
9243 continue;
9244 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
9245 continue;
9246
9247 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9248 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9249 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
9250 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
9251 isa<ConstantSDNode>(BinOp.getOperand(i))) {
9252 Inputs.push_back(BinOp.getOperand(i));
9253 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
9254 BinOp.getOperand(i).getOpcode() == ISD::OR ||
9255 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
9256 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
9257 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
9258 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
9259 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
9260 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
9261 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
9262 BinOps.push_back(BinOp.getOperand(i));
9263 } else {
9264 // We have an input that is not an extension or another binary
9265 // operation; we'll abort this transformation.
9266 return SDValue();
9267 }
9268 }
9269 }
9270
9271 // Make sure that this is a self-contained cluster of operations (which
9272 // is not quite the same thing as saying that everything has only one
9273 // use).
9274 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9275 if (isa<ConstantSDNode>(Inputs[i]))
9276 continue;
9277
9278 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
9279 UE = Inputs[i].getNode()->use_end();
9280 UI != UE; ++UI) {
9281 SDNode *User = *UI;
9282 if (User != N && !Visited.count(User))
9283 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00009284
9285 // Make sure that we're not going to promote the non-output-value
9286 // operand(s) or SELECT or SELECT_CC.
9287 // FIXME: Although we could sometimes handle this, and it does occur in
9288 // practice that one of the condition inputs to the select is also one of
9289 // the outputs, we currently can't deal with this.
9290 if (User->getOpcode() == ISD::SELECT) {
9291 if (User->getOperand(0) == Inputs[i])
9292 return SDValue();
9293 } else if (User->getOpcode() == ISD::SELECT_CC) {
9294 if (User->getOperand(0) == Inputs[i] ||
9295 User->getOperand(1) == Inputs[i])
9296 return SDValue();
9297 }
Hal Finkel940ab932014-02-28 00:27:01 +00009298 }
9299 }
9300
9301 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
9302 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
9303 UE = PromOps[i].getNode()->use_end();
9304 UI != UE; ++UI) {
9305 SDNode *User = *UI;
9306 if (User != N && !Visited.count(User))
9307 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00009308
9309 // Make sure that we're not going to promote the non-output-value
9310 // operand(s) or SELECT or SELECT_CC.
9311 // FIXME: Although we could sometimes handle this, and it does occur in
9312 // practice that one of the condition inputs to the select is also one of
9313 // the outputs, we currently can't deal with this.
9314 if (User->getOpcode() == ISD::SELECT) {
9315 if (User->getOperand(0) == PromOps[i])
9316 return SDValue();
9317 } else if (User->getOpcode() == ISD::SELECT_CC) {
9318 if (User->getOperand(0) == PromOps[i] ||
9319 User->getOperand(1) == PromOps[i])
9320 return SDValue();
9321 }
Hal Finkel940ab932014-02-28 00:27:01 +00009322 }
9323 }
9324
9325 // Replace all inputs with the extension operand.
9326 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9327 // Constants may have users outside the cluster of to-be-promoted nodes,
9328 // and so we need to replace those as we do the promotions.
9329 if (isa<ConstantSDNode>(Inputs[i]))
9330 continue;
9331 else
9332 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
9333 }
9334
9335 // Replace all operations (these are all the same, but have a different
9336 // (i1) return type). DAG.getNode will validate that the types of
9337 // a binary operator match, so go through the list in reverse so that
9338 // we've likely promoted both operands first. Any intermediate truncations or
9339 // extensions disappear.
9340 while (!PromOps.empty()) {
9341 SDValue PromOp = PromOps.back();
9342 PromOps.pop_back();
9343
9344 if (PromOp.getOpcode() == ISD::TRUNCATE ||
9345 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
9346 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
9347 PromOp.getOpcode() == ISD::ANY_EXTEND) {
9348 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
9349 PromOp.getOperand(0).getValueType() != MVT::i1) {
9350 // The operand is not yet ready (see comment below).
9351 PromOps.insert(PromOps.begin(), PromOp);
9352 continue;
9353 }
9354
9355 SDValue RepValue = PromOp.getOperand(0);
9356 if (isa<ConstantSDNode>(RepValue))
9357 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
9358
9359 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
9360 continue;
9361 }
9362
9363 unsigned C;
9364 switch (PromOp.getOpcode()) {
9365 default: C = 0; break;
9366 case ISD::SELECT: C = 1; break;
9367 case ISD::SELECT_CC: C = 2; break;
9368 }
9369
9370 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
9371 PromOp.getOperand(C).getValueType() != MVT::i1) ||
9372 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
9373 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
9374 // The to-be-promoted operands of this node have not yet been
9375 // promoted (this should be rare because we're going through the
9376 // list backward, but if one of the operands has several users in
9377 // this cluster of to-be-promoted nodes, it is possible).
9378 PromOps.insert(PromOps.begin(), PromOp);
9379 continue;
9380 }
9381
9382 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
9383 PromOp.getNode()->op_end());
9384
9385 // If there are any constant inputs, make sure they're replaced now.
9386 for (unsigned i = 0; i < 2; ++i)
9387 if (isa<ConstantSDNode>(Ops[C+i]))
9388 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
9389
9390 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00009391 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00009392 }
9393
9394 // Now we're left with the initial truncation itself.
9395 if (N->getOpcode() == ISD::TRUNCATE)
9396 return N->getOperand(0);
9397
9398 // Otherwise, this is a comparison. The operands to be compared have just
9399 // changed type (to i1), but everything else is the same.
9400 return SDValue(N, 0);
9401}
9402
9403SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
9404 DAGCombinerInfo &DCI) const {
9405 SelectionDAG &DAG = DCI.DAG;
9406 SDLoc dl(N);
9407
Hal Finkel940ab932014-02-28 00:27:01 +00009408 // If we're tracking CR bits, we need to be careful that we don't have:
9409 // zext(binary-ops(trunc(x), trunc(y)))
9410 // or
9411 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
9412 // such that we're unnecessarily moving things into CR bits that can more
9413 // efficiently stay in GPRs. Note that if we're not certain that the high
9414 // bits are set as required by the final extension, we still may need to do
9415 // some masking to get the proper behavior.
9416
Hal Finkel46043ed2014-03-01 21:36:57 +00009417 // This same functionality is important on PPC64 when dealing with
9418 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
9419 // the return values of functions. Because it is so similar, it is handled
9420 // here as well.
9421
Hal Finkel940ab932014-02-28 00:27:01 +00009422 if (N->getValueType(0) != MVT::i32 &&
9423 N->getValueType(0) != MVT::i64)
9424 return SDValue();
9425
Eric Christophercccae792015-01-30 22:02:31 +00009426 if (!((N->getOperand(0).getValueType() == MVT::i1 && Subtarget.useCRBits()) ||
9427 (N->getOperand(0).getValueType() == MVT::i32 && Subtarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +00009428 return SDValue();
9429
9430 if (N->getOperand(0).getOpcode() != ISD::AND &&
9431 N->getOperand(0).getOpcode() != ISD::OR &&
9432 N->getOperand(0).getOpcode() != ISD::XOR &&
9433 N->getOperand(0).getOpcode() != ISD::SELECT &&
9434 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
9435 return SDValue();
9436
9437 SmallVector<SDValue, 4> Inputs;
9438 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
9439 SmallPtrSet<SDNode *, 16> Visited;
9440
9441 // Visit all inputs, collect all binary operations (and, or, xor and
9442 // select) that are all fed by truncations.
9443 while (!BinOps.empty()) {
9444 SDValue BinOp = BinOps.back();
9445 BinOps.pop_back();
9446
David Blaikie70573dc2014-11-19 07:49:26 +00009447 if (!Visited.insert(BinOp.getNode()).second)
Hal Finkel940ab932014-02-28 00:27:01 +00009448 continue;
9449
9450 PromOps.push_back(BinOp);
9451
9452 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
9453 // The condition of the select is not promoted.
9454 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
9455 continue;
9456 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
9457 continue;
9458
9459 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
9460 isa<ConstantSDNode>(BinOp.getOperand(i))) {
9461 Inputs.push_back(BinOp.getOperand(i));
9462 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
9463 BinOp.getOperand(i).getOpcode() == ISD::OR ||
9464 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
9465 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
9466 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
9467 BinOps.push_back(BinOp.getOperand(i));
9468 } else {
9469 // We have an input that is not a truncation or another binary
9470 // operation; we'll abort this transformation.
9471 return SDValue();
9472 }
9473 }
9474 }
9475
Hal Finkel4104a1a2014-12-14 05:53:19 +00009476 // The operands of a select that must be truncated when the select is
9477 // promoted because the operand is actually part of the to-be-promoted set.
9478 DenseMap<SDNode *, EVT> SelectTruncOp[2];
9479
Hal Finkel940ab932014-02-28 00:27:01 +00009480 // Make sure that this is a self-contained cluster of operations (which
9481 // is not quite the same thing as saying that everything has only one
9482 // use).
9483 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9484 if (isa<ConstantSDNode>(Inputs[i]))
9485 continue;
9486
9487 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
9488 UE = Inputs[i].getNode()->use_end();
9489 UI != UE; ++UI) {
9490 SDNode *User = *UI;
9491 if (User != N && !Visited.count(User))
9492 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00009493
Hal Finkel4104a1a2014-12-14 05:53:19 +00009494 // If we're going to promote the non-output-value operand(s) or SELECT or
9495 // SELECT_CC, record them for truncation.
Hal Finkel46043ed2014-03-01 21:36:57 +00009496 if (User->getOpcode() == ISD::SELECT) {
9497 if (User->getOperand(0) == Inputs[i])
Hal Finkel4104a1a2014-12-14 05:53:19 +00009498 SelectTruncOp[0].insert(std::make_pair(User,
9499 User->getOperand(0).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +00009500 } else if (User->getOpcode() == ISD::SELECT_CC) {
Hal Finkel4104a1a2014-12-14 05:53:19 +00009501 if (User->getOperand(0) == Inputs[i])
9502 SelectTruncOp[0].insert(std::make_pair(User,
9503 User->getOperand(0).getValueType()));
9504 if (User->getOperand(1) == Inputs[i])
9505 SelectTruncOp[1].insert(std::make_pair(User,
9506 User->getOperand(1).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +00009507 }
Hal Finkel940ab932014-02-28 00:27:01 +00009508 }
9509 }
9510
9511 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
9512 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
9513 UE = PromOps[i].getNode()->use_end();
9514 UI != UE; ++UI) {
9515 SDNode *User = *UI;
9516 if (User != N && !Visited.count(User))
9517 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00009518
Hal Finkel4104a1a2014-12-14 05:53:19 +00009519 // If we're going to promote the non-output-value operand(s) or SELECT or
9520 // SELECT_CC, record them for truncation.
Hal Finkel46043ed2014-03-01 21:36:57 +00009521 if (User->getOpcode() == ISD::SELECT) {
9522 if (User->getOperand(0) == PromOps[i])
Hal Finkel4104a1a2014-12-14 05:53:19 +00009523 SelectTruncOp[0].insert(std::make_pair(User,
9524 User->getOperand(0).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +00009525 } else if (User->getOpcode() == ISD::SELECT_CC) {
Hal Finkel4104a1a2014-12-14 05:53:19 +00009526 if (User->getOperand(0) == PromOps[i])
9527 SelectTruncOp[0].insert(std::make_pair(User,
9528 User->getOperand(0).getValueType()));
9529 if (User->getOperand(1) == PromOps[i])
9530 SelectTruncOp[1].insert(std::make_pair(User,
9531 User->getOperand(1).getValueType()));
Hal Finkel46043ed2014-03-01 21:36:57 +00009532 }
Hal Finkel940ab932014-02-28 00:27:01 +00009533 }
9534 }
9535
Hal Finkel46043ed2014-03-01 21:36:57 +00009536 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +00009537 bool ReallyNeedsExt = false;
9538 if (N->getOpcode() != ISD::ANY_EXTEND) {
9539 // If all of the inputs are not already sign/zero extended, then
9540 // we'll still need to do that at the end.
9541 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9542 if (isa<ConstantSDNode>(Inputs[i]))
9543 continue;
9544
9545 unsigned OpBits =
9546 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +00009547 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
9548
Hal Finkel940ab932014-02-28 00:27:01 +00009549 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
9550 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00009551 APInt::getHighBitsSet(OpBits,
9552 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +00009553 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +00009554 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
9555 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +00009556 ReallyNeedsExt = true;
9557 break;
9558 }
9559 }
9560 }
9561
9562 // Replace all inputs, either with the truncation operand, or a
9563 // truncation or extension to the final output type.
9564 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
9565 // Constant inputs need to be replaced with the to-be-promoted nodes that
9566 // use them because they might have users outside of the cluster of
9567 // promoted nodes.
9568 if (isa<ConstantSDNode>(Inputs[i]))
9569 continue;
9570
9571 SDValue InSrc = Inputs[i].getOperand(0);
9572 if (Inputs[i].getValueType() == N->getValueType(0))
9573 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
9574 else if (N->getOpcode() == ISD::SIGN_EXTEND)
9575 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
9576 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
9577 else if (N->getOpcode() == ISD::ZERO_EXTEND)
9578 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
9579 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
9580 else
9581 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
9582 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
9583 }
9584
9585 // Replace all operations (these are all the same, but have a different
9586 // (promoted) return type). DAG.getNode will validate that the types of
9587 // a binary operator match, so go through the list in reverse so that
9588 // we've likely promoted both operands first.
9589 while (!PromOps.empty()) {
9590 SDValue PromOp = PromOps.back();
9591 PromOps.pop_back();
9592
9593 unsigned C;
9594 switch (PromOp.getOpcode()) {
9595 default: C = 0; break;
9596 case ISD::SELECT: C = 1; break;
9597 case ISD::SELECT_CC: C = 2; break;
9598 }
9599
9600 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
9601 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
9602 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
9603 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
9604 // The to-be-promoted operands of this node have not yet been
9605 // promoted (this should be rare because we're going through the
9606 // list backward, but if one of the operands has several users in
9607 // this cluster of to-be-promoted nodes, it is possible).
9608 PromOps.insert(PromOps.begin(), PromOp);
9609 continue;
9610 }
9611
Hal Finkel4104a1a2014-12-14 05:53:19 +00009612 // For SELECT and SELECT_CC nodes, we do a similar check for any
9613 // to-be-promoted comparison inputs.
9614 if (PromOp.getOpcode() == ISD::SELECT ||
9615 PromOp.getOpcode() == ISD::SELECT_CC) {
9616 if ((SelectTruncOp[0].count(PromOp.getNode()) &&
9617 PromOp.getOperand(0).getValueType() != N->getValueType(0)) ||
9618 (SelectTruncOp[1].count(PromOp.getNode()) &&
9619 PromOp.getOperand(1).getValueType() != N->getValueType(0))) {
9620 PromOps.insert(PromOps.begin(), PromOp);
9621 continue;
9622 }
9623 }
9624
Hal Finkel940ab932014-02-28 00:27:01 +00009625 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
9626 PromOp.getNode()->op_end());
9627
9628 // If this node has constant inputs, then they'll need to be promoted here.
9629 for (unsigned i = 0; i < 2; ++i) {
9630 if (!isa<ConstantSDNode>(Ops[C+i]))
9631 continue;
9632 if (Ops[C+i].getValueType() == N->getValueType(0))
9633 continue;
9634
9635 if (N->getOpcode() == ISD::SIGN_EXTEND)
9636 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
9637 else if (N->getOpcode() == ISD::ZERO_EXTEND)
9638 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
9639 else
9640 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
9641 }
9642
Hal Finkel4104a1a2014-12-14 05:53:19 +00009643 // If we've promoted the comparison inputs of a SELECT or SELECT_CC,
9644 // truncate them again to the original value type.
9645 if (PromOp.getOpcode() == ISD::SELECT ||
9646 PromOp.getOpcode() == ISD::SELECT_CC) {
9647 auto SI0 = SelectTruncOp[0].find(PromOp.getNode());
9648 if (SI0 != SelectTruncOp[0].end())
9649 Ops[0] = DAG.getNode(ISD::TRUNCATE, dl, SI0->second, Ops[0]);
9650 auto SI1 = SelectTruncOp[1].find(PromOp.getNode());
9651 if (SI1 != SelectTruncOp[1].end())
9652 Ops[1] = DAG.getNode(ISD::TRUNCATE, dl, SI1->second, Ops[1]);
9653 }
9654
Hal Finkel940ab932014-02-28 00:27:01 +00009655 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00009656 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00009657 }
9658
9659 // Now we're left with the initial extension itself.
9660 if (!ReallyNeedsExt)
9661 return N->getOperand(0);
9662
Hal Finkel46043ed2014-03-01 21:36:57 +00009663 // To zero extend, just mask off everything except for the first bit (in the
9664 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +00009665 if (N->getOpcode() == ISD::ZERO_EXTEND)
9666 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00009667 DAG.getConstant(APInt::getLowBitsSet(
9668 N->getValueSizeInBits(0), PromBits),
9669 N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +00009670
9671 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
9672 "Invalid extension type");
9673 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0));
9674 SDValue ShiftCst =
Hal Finkel46043ed2014-03-01 21:36:57 +00009675 DAG.getConstant(N->getValueSizeInBits(0)-PromBits, ShiftAmountTy);
Hal Finkel940ab932014-02-28 00:27:01 +00009676 return DAG.getNode(ISD::SRA, dl, N->getValueType(0),
9677 DAG.getNode(ISD::SHL, dl, N->getValueType(0),
9678 N->getOperand(0), ShiftCst), ShiftCst);
9679}
9680
Hal Finkel5efb9182015-01-06 06:01:57 +00009681SDValue PPCTargetLowering::combineFPToIntToFP(SDNode *N,
9682 DAGCombinerInfo &DCI) const {
9683 assert((N->getOpcode() == ISD::SINT_TO_FP ||
9684 N->getOpcode() == ISD::UINT_TO_FP) &&
9685 "Need an int -> FP conversion node here");
9686
9687 if (!Subtarget.has64BitSupport())
9688 return SDValue();
9689
9690 SelectionDAG &DAG = DCI.DAG;
9691 SDLoc dl(N);
9692 SDValue Op(N, 0);
9693
9694 // Don't handle ppc_fp128 here or i1 conversions.
9695 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
9696 return SDValue();
9697 if (Op.getOperand(0).getValueType() == MVT::i1)
9698 return SDValue();
9699
9700 // For i32 intermediate values, unfortunately, the conversion functions
9701 // leave the upper 32 bits of the value are undefined. Within the set of
9702 // scalar instructions, we have no method for zero- or sign-extending the
9703 // value. Thus, we cannot handle i32 intermediate values here.
9704 if (Op.getOperand(0).getValueType() == MVT::i32)
9705 return SDValue();
9706
9707 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
9708 "UINT_TO_FP is supported only with FPCVT");
9709
9710 // If we have FCFIDS, then use it when converting to single-precision.
9711 // Otherwise, convert to double-precision and then round.
Eric Christophercccae792015-01-30 22:02:31 +00009712 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
9713 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS
9714 : PPCISD::FCFIDS)
9715 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU
9716 : PPCISD::FCFID);
9717 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
9718 ? MVT::f32
9719 : MVT::f64;
Hal Finkel5efb9182015-01-06 06:01:57 +00009720
9721 // If we're converting from a float, to an int, and back to a float again,
9722 // then we don't need the store/load pair at all.
9723 if ((Op.getOperand(0).getOpcode() == ISD::FP_TO_UINT &&
9724 Subtarget.hasFPCVT()) ||
9725 (Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT)) {
9726 SDValue Src = Op.getOperand(0).getOperand(0);
9727 if (Src.getValueType() == MVT::f32) {
9728 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
9729 DCI.AddToWorklist(Src.getNode());
9730 }
9731
9732 unsigned FCTOp =
9733 Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
9734 PPCISD::FCTIDUZ;
9735
9736 SDValue Tmp = DAG.getNode(FCTOp, dl, MVT::f64, Src);
9737 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Tmp);
9738
9739 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) {
9740 FP = DAG.getNode(ISD::FP_ROUND, dl,
9741 MVT::f32, FP, DAG.getIntPtrConstant(0));
9742 DCI.AddToWorklist(FP.getNode());
9743 }
9744
9745 return FP;
9746 }
9747
9748 return SDValue();
9749}
9750
Bill Schmidtfae5d712014-12-09 16:35:51 +00009751// expandVSXLoadForLE - Convert VSX loads (which may be intrinsics for
9752// builtins) into loads with swaps.
9753SDValue PPCTargetLowering::expandVSXLoadForLE(SDNode *N,
9754 DAGCombinerInfo &DCI) const {
9755 SelectionDAG &DAG = DCI.DAG;
9756 SDLoc dl(N);
9757 SDValue Chain;
9758 SDValue Base;
9759 MachineMemOperand *MMO;
9760
9761 switch (N->getOpcode()) {
9762 default:
9763 llvm_unreachable("Unexpected opcode for little endian VSX load");
9764 case ISD::LOAD: {
9765 LoadSDNode *LD = cast<LoadSDNode>(N);
9766 Chain = LD->getChain();
9767 Base = LD->getBasePtr();
9768 MMO = LD->getMemOperand();
9769 // If the MMO suggests this isn't a load of a full vector, leave
9770 // things alone. For a built-in, we have to make the change for
9771 // correctness, so if there is a size problem that will be a bug.
9772 if (MMO->getSize() < 16)
9773 return SDValue();
9774 break;
9775 }
9776 case ISD::INTRINSIC_W_CHAIN: {
9777 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
9778 Chain = Intrin->getChain();
9779 Base = Intrin->getBasePtr();
9780 MMO = Intrin->getMemOperand();
9781 break;
9782 }
9783 }
9784
9785 MVT VecTy = N->getValueType(0).getSimpleVT();
9786 SDValue LoadOps[] = { Chain, Base };
9787 SDValue Load = DAG.getMemIntrinsicNode(PPCISD::LXVD2X, dl,
9788 DAG.getVTList(VecTy, MVT::Other),
9789 LoadOps, VecTy, MMO);
9790 DCI.AddToWorklist(Load.getNode());
9791 Chain = Load.getValue(1);
9792 SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl,
9793 DAG.getVTList(VecTy, MVT::Other), Chain, Load);
9794 DCI.AddToWorklist(Swap.getNode());
9795 return Swap;
9796}
9797
9798// expandVSXStoreForLE - Convert VSX stores (which may be intrinsics for
9799// builtins) into stores with swaps.
9800SDValue PPCTargetLowering::expandVSXStoreForLE(SDNode *N,
9801 DAGCombinerInfo &DCI) const {
9802 SelectionDAG &DAG = DCI.DAG;
9803 SDLoc dl(N);
9804 SDValue Chain;
9805 SDValue Base;
9806 unsigned SrcOpnd;
9807 MachineMemOperand *MMO;
9808
9809 switch (N->getOpcode()) {
9810 default:
9811 llvm_unreachable("Unexpected opcode for little endian VSX store");
9812 case ISD::STORE: {
9813 StoreSDNode *ST = cast<StoreSDNode>(N);
9814 Chain = ST->getChain();
9815 Base = ST->getBasePtr();
9816 MMO = ST->getMemOperand();
9817 SrcOpnd = 1;
9818 // If the MMO suggests this isn't a store of a full vector, leave
9819 // things alone. For a built-in, we have to make the change for
9820 // correctness, so if there is a size problem that will be a bug.
9821 if (MMO->getSize() < 16)
9822 return SDValue();
9823 break;
9824 }
9825 case ISD::INTRINSIC_VOID: {
9826 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
9827 Chain = Intrin->getChain();
9828 // Intrin->getBasePtr() oddly does not get what we want.
9829 Base = Intrin->getOperand(3);
9830 MMO = Intrin->getMemOperand();
9831 SrcOpnd = 2;
9832 break;
9833 }
9834 }
9835
9836 SDValue Src = N->getOperand(SrcOpnd);
9837 MVT VecTy = Src.getValueType().getSimpleVT();
9838 SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl,
9839 DAG.getVTList(VecTy, MVT::Other), Chain, Src);
9840 DCI.AddToWorklist(Swap.getNode());
9841 Chain = Swap.getValue(1);
9842 SDValue StoreOps[] = { Chain, Swap, Base };
9843 SDValue Store = DAG.getMemIntrinsicNode(PPCISD::STXVD2X, dl,
9844 DAG.getVTList(MVT::Other),
9845 StoreOps, VecTy, MMO);
9846 DCI.AddToWorklist(Store.getNode());
9847 return Store;
9848}
9849
Duncan Sandsdc2dac12008-11-24 14:53:14 +00009850SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
9851 DAGCombinerInfo &DCI) const {
Chris Lattnerf4184352006-03-01 04:57:39 +00009852 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009853 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00009854 switch (N->getOpcode()) {
9855 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00009856 case PPCISD::SHL:
9857 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00009858 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00009859 return N->getOperand(0);
9860 }
9861 break;
9862 case PPCISD::SRL:
9863 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00009864 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00009865 return N->getOperand(0);
9866 }
9867 break;
9868 case PPCISD::SRA:
9869 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00009870 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00009871 C->isAllOnesValue()) // -1 >>s V -> -1.
9872 return N->getOperand(0);
9873 }
9874 break;
Hal Finkel940ab932014-02-28 00:27:01 +00009875 case ISD::SIGN_EXTEND:
9876 case ISD::ZERO_EXTEND:
9877 case ISD::ANY_EXTEND:
9878 return DAGCombineExtBoolTrunc(N, DCI);
9879 case ISD::TRUNCATE:
9880 case ISD::SETCC:
9881 case ISD::SELECT_CC:
9882 return DAGCombineTruncBoolExt(N, DCI);
Chris Lattnerf4184352006-03-01 04:57:39 +00009883 case ISD::SINT_TO_FP:
Hal Finkel5efb9182015-01-06 06:01:57 +00009884 case ISD::UINT_TO_FP:
9885 return combineFPToIntToFP(N, DCI);
Bill Schmidtfae5d712014-12-09 16:35:51 +00009886 case ISD::STORE: {
Chris Lattner27f53452006-03-01 05:50:56 +00009887 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
Eric Christophercccae792015-01-30 22:02:31 +00009888 if (Subtarget.hasSTFIWX() && !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00009889 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00009890 N->getOperand(1).getValueType() == MVT::i32 &&
9891 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009892 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00009893 if (Val.getValueType() == MVT::f32) {
9894 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00009895 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00009896 }
Owen Anderson9f944592009-08-11 20:47:22 +00009897 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00009898 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00009899
Hal Finkel60c75102013-04-01 15:37:53 +00009900 SDValue Ops[] = {
9901 N->getOperand(0), Val, N->getOperand(2),
9902 DAG.getValueType(N->getOperand(1).getValueType())
9903 };
9904
9905 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00009906 DAG.getVTList(MVT::Other), Ops,
Hal Finkel60c75102013-04-01 15:37:53 +00009907 cast<StoreSDNode>(N)->getMemoryVT(),
9908 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00009909 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00009910 return Val;
9911 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009912
Chris Lattnera7976d32006-07-10 20:56:58 +00009913 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00009914 if (cast<StoreSDNode>(N)->isUnindexed() &&
9915 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00009916 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00009917 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00009918 N->getOperand(1).getValueType() == MVT::i16 ||
Eric Christophercccae792015-01-30 22:02:31 +00009919 (Subtarget.hasLDBRX() && Subtarget.isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00009920 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009921 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00009922 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00009923 if (BSwapOp.getValueType() == MVT::i16)
9924 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00009925
Dan Gohman48b185d2009-09-25 20:36:54 +00009926 SDValue Ops[] = {
9927 N->getOperand(0), BSwapOp, N->getOperand(2),
9928 DAG.getValueType(N->getOperand(1).getValueType())
9929 };
9930 return
9931 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00009932 Ops, cast<StoreSDNode>(N)->getMemoryVT(),
Dan Gohman48b185d2009-09-25 20:36:54 +00009933 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00009934 }
Bill Schmidtfae5d712014-12-09 16:35:51 +00009935
9936 // For little endian, VSX stores require generating xxswapd/lxvd2x.
9937 EVT VT = N->getOperand(1).getValueType();
9938 if (VT.isSimple()) {
9939 MVT StoreVT = VT.getSimpleVT();
Eric Christophercccae792015-01-30 22:02:31 +00009940 if (Subtarget.hasVSX() && Subtarget.isLittleEndian() &&
Bill Schmidtfae5d712014-12-09 16:35:51 +00009941 (StoreVT == MVT::v2f64 || StoreVT == MVT::v2i64 ||
9942 StoreVT == MVT::v4f32 || StoreVT == MVT::v4i32))
9943 return expandVSXStoreForLE(N, DCI);
9944 }
Chris Lattnera7976d32006-07-10 20:56:58 +00009945 break;
Bill Schmidtfae5d712014-12-09 16:35:51 +00009946 }
Hal Finkelcf2e9082013-05-24 23:00:14 +00009947 case ISD::LOAD: {
9948 LoadSDNode *LD = cast<LoadSDNode>(N);
9949 EVT VT = LD->getValueType(0);
Bill Schmidtfae5d712014-12-09 16:35:51 +00009950
9951 // For little endian, VSX loads require generating lxvd2x/xxswapd.
9952 if (VT.isSimple()) {
9953 MVT LoadVT = VT.getSimpleVT();
Eric Christophercccae792015-01-30 22:02:31 +00009954 if (Subtarget.hasVSX() && Subtarget.isLittleEndian() &&
Bill Schmidtfae5d712014-12-09 16:35:51 +00009955 (LoadVT == MVT::v2f64 || LoadVT == MVT::v2i64 ||
9956 LoadVT == MVT::v4f32 || LoadVT == MVT::v4i32))
9957 return expandVSXLoadForLE(N, DCI);
9958 }
9959
Hal Finkelc93a9a22015-02-25 01:06:45 +00009960 EVT MemVT = LD->getMemoryVT();
9961 Type *Ty = MemVT.getTypeForEVT(*DAG.getContext());
Hal Finkelcf2e9082013-05-24 23:00:14 +00009962 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
Hal Finkelc93a9a22015-02-25 01:06:45 +00009963 Type *STy = MemVT.getScalarType().getTypeForEVT(*DAG.getContext());
9964 unsigned ScalarABIAlignment = getDataLayout()->getABITypeAlignment(STy);
9965 if (LD->isUnindexed() && VT.isVector() &&
9966 ((Subtarget.hasAltivec() && ISD::isNON_EXTLoad(N) &&
9967 // P8 and later hardware should just use LOAD.
9968 !Subtarget.hasP8Vector() && (VT == MVT::v16i8 || VT == MVT::v8i16 ||
9969 VT == MVT::v4i32 || VT == MVT::v4f32)) ||
9970 (Subtarget.hasQPX() && (VT == MVT::v4f64 || VT == MVT::v4f32) &&
9971 LD->getAlignment() >= ScalarABIAlignment)) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +00009972 LD->getAlignment() < ABIAlignment) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00009973 // This is a type-legal unaligned Altivec or QPX load.
Hal Finkelcf2e9082013-05-24 23:00:14 +00009974 SDValue Chain = LD->getChain();
9975 SDValue Ptr = LD->getBasePtr();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009976 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelcf2e9082013-05-24 23:00:14 +00009977
9978 // This implements the loading of unaligned vectors as described in
9979 // the venerable Apple Velocity Engine overview. Specifically:
9980 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
9981 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
9982 //
9983 // The general idea is to expand a sequence of one or more unaligned
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00009984 // loads into an alignment-based permutation-control instruction (lvsl
9985 // or lvsr), a series of regular vector loads (which always truncate
9986 // their input address to an aligned address), and a series of
9987 // permutations. The results of these permutations are the requested
9988 // loaded values. The trick is that the last "extra" load is not taken
9989 // from the address you might suspect (sizeof(vector) bytes after the
9990 // last requested load), but rather sizeof(vector) - 1 bytes after the
9991 // last requested vector. The point of this is to avoid a page fault if
9992 // the base address happened to be aligned. This works because if the
9993 // base address is aligned, then adding less than a full vector length
9994 // will cause the last vector in the sequence to be (re)loaded.
9995 // Otherwise, the next vector will be fetched as you might suspect was
9996 // necessary.
Hal Finkelcf2e9082013-05-24 23:00:14 +00009997
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00009998 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00009999 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010000 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
10001 // optimization later.
Hal Finkelc93a9a22015-02-25 01:06:45 +000010002 Intrinsic::ID Intr, IntrLD, IntrPerm;
10003 MVT PermCntlTy, PermTy, LDTy;
10004 if (Subtarget.hasAltivec()) {
10005 Intr = isLittleEndian ? Intrinsic::ppc_altivec_lvsr :
10006 Intrinsic::ppc_altivec_lvsl;
10007 IntrLD = Intrinsic::ppc_altivec_lvx;
10008 IntrPerm = Intrinsic::ppc_altivec_vperm;
10009 PermCntlTy = MVT::v16i8;
10010 PermTy = MVT::v4i32;
10011 LDTy = MVT::v4i32;
10012 } else {
10013 Intr = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlpcld :
10014 Intrinsic::ppc_qpx_qvlpcls;
10015 IntrLD = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlfd :
10016 Intrinsic::ppc_qpx_qvlfs;
10017 IntrPerm = Intrinsic::ppc_qpx_qvfperm;
10018 PermCntlTy = MVT::v4f64;
10019 PermTy = MVT::v4f64;
10020 LDTy = MemVT.getSimpleVT();
10021 }
10022
10023 SDValue PermCntl = BuildIntrinsicOp(Intr, Ptr, DAG, dl, PermCntlTy);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010024
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010025 // Create the new MMO for the new base load. It is like the original MMO,
10026 // but represents an area in memory almost twice the vector size centered
10027 // on the original address. If the address is unaligned, we might start
10028 // reading up to (sizeof(vector)-1) bytes below the address of the
10029 // original unaligned load.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010030 MachineFunction &MF = DAG.getMachineFunction();
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010031 MachineMemOperand *BaseMMO =
Hal Finkelc93a9a22015-02-25 01:06:45 +000010032 MF.getMachineMemOperand(LD->getMemOperand(), -MemVT.getStoreSize()+1,
10033 2*MemVT.getStoreSize()-1);
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010034
10035 // Create the new base load.
Hal Finkelc93a9a22015-02-25 01:06:45 +000010036 SDValue LDXIntID = DAG.getTargetConstant(IntrLD, getPointerTy());
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010037 SDValue BaseLoadOps[] = { Chain, LDXIntID, Ptr };
10038 SDValue BaseLoad =
10039 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
Hal Finkelc93a9a22015-02-25 01:06:45 +000010040 DAG.getVTList(PermTy, MVT::Other),
10041 BaseLoadOps, LDTy, BaseMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010042
10043 // Note that the value of IncOffset (which is provided to the next
10044 // load's pointer info offset value, and thus used to calculate the
10045 // alignment), and the value of IncValue (which is actually used to
10046 // increment the pointer value) are different! This is because we
10047 // require the next load to appear to be aligned, even though it
10048 // is actually offset from the base pointer by a lesser amount.
10049 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +000010050 int IncValue = IncOffset;
10051
10052 // Walk (both up and down) the chain looking for another load at the real
10053 // (aligned) offset (the alignment of the other load does not matter in
10054 // this case). If found, then do not use the offset reduction trick, as
10055 // that will prevent the loads from being later combined (as they would
10056 // otherwise be duplicates).
10057 if (!findConsecutiveLoad(LD, DAG))
10058 --IncValue;
10059
Hal Finkelcf2e9082013-05-24 23:00:14 +000010060 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
10061 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
10062
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010063 MachineMemOperand *ExtraMMO =
10064 MF.getMachineMemOperand(LD->getMemOperand(),
Hal Finkelc93a9a22015-02-25 01:06:45 +000010065 1, 2*MemVT.getStoreSize()-1);
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010066 SDValue ExtraLoadOps[] = { Chain, LDXIntID, Ptr };
Hal Finkelcf2e9082013-05-24 23:00:14 +000010067 SDValue ExtraLoad =
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010068 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
Hal Finkelc93a9a22015-02-25 01:06:45 +000010069 DAG.getVTList(PermTy, MVT::Other),
10070 ExtraLoadOps, LDTy, ExtraMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010071
10072 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
10073 BaseLoad.getValue(1), ExtraLoad.getValue(1));
10074
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010075 // Because vperm has a big-endian bias, we must reverse the order
10076 // of the input vectors and complement the permute control vector
10077 // when generating little endian code. We have already handled the
10078 // latter by using lvsr instead of lvsl, so just reverse BaseLoad
10079 // and ExtraLoad here.
10080 SDValue Perm;
10081 if (isLittleEndian)
Hal Finkelc93a9a22015-02-25 01:06:45 +000010082 Perm = BuildIntrinsicOp(IntrPerm,
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010083 ExtraLoad, BaseLoad, PermCntl, DAG, dl);
10084 else
Hal Finkelc93a9a22015-02-25 01:06:45 +000010085 Perm = BuildIntrinsicOp(IntrPerm,
Bill Schmidt6b5a7df2014-06-09 22:00:52 +000010086 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010087
Hal Finkelc93a9a22015-02-25 01:06:45 +000010088 if (VT != PermTy)
10089 Perm = Subtarget.hasAltivec() ?
10090 DAG.getNode(ISD::BITCAST, dl, VT, Perm) :
10091 DAG.getNode(ISD::FP_ROUND, dl, VT, Perm, // QPX
10092 DAG.getTargetConstant(1, MVT::i64));
10093 // second argument is 1 because this rounding
10094 // is always exact.
Hal Finkelcf2e9082013-05-24 23:00:14 +000010095
Hal Finkelb6d0d6b2014-08-01 05:20:41 +000010096 // The output of the permutation is our loaded result, the TokenFactor is
10097 // our new chain.
10098 DCI.CombineTo(N, Perm, TF);
Hal Finkelcf2e9082013-05-24 23:00:14 +000010099 return SDValue(N, 0);
10100 }
10101 }
10102 break;
Eric Christophercccae792015-01-30 22:02:31 +000010103 case ISD::INTRINSIC_WO_CHAIN: {
10104 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelc93a9a22015-02-25 01:06:45 +000010105 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
Eric Christophercccae792015-01-30 22:02:31 +000010106 Intrinsic::ID Intr = (isLittleEndian ? Intrinsic::ppc_altivec_lvsr
10107 : Intrinsic::ppc_altivec_lvsl);
Hal Finkelc93a9a22015-02-25 01:06:45 +000010108 if ((IID == Intr ||
10109 IID == Intrinsic::ppc_qpx_qvlpcld ||
10110 IID == Intrinsic::ppc_qpx_qvlpcls) &&
10111 N->getOperand(1)->getOpcode() == ISD::ADD) {
Eric Christophercccae792015-01-30 22:02:31 +000010112 SDValue Add = N->getOperand(1);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010113
Hal Finkelc93a9a22015-02-25 01:06:45 +000010114 int Bits = IID == Intrinsic::ppc_qpx_qvlpcld ?
10115 5 /* 32 byte alignment */ : 4 /* 16 byte alignment */;
10116
Eric Christophercccae792015-01-30 22:02:31 +000010117 if (DAG.MaskedValueIsZero(
10118 Add->getOperand(1),
Hal Finkelc93a9a22015-02-25 01:06:45 +000010119 APInt::getAllOnesValue(Bits /* alignment */)
Eric Christophercccae792015-01-30 22:02:31 +000010120 .zext(
10121 Add.getValueType().getScalarType().getSizeInBits()))) {
10122 SDNode *BasePtr = Add->getOperand(0).getNode();
10123 for (SDNode::use_iterator UI = BasePtr->use_begin(),
10124 UE = BasePtr->use_end();
10125 UI != UE; ++UI) {
10126 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
Hal Finkelc93a9a22015-02-25 01:06:45 +000010127 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() == IID) {
Eric Christophercccae792015-01-30 22:02:31 +000010128 // We've found another LVSL/LVSR, and this address is an aligned
10129 // multiple of that one. The results will be the same, so use the
10130 // one we've just found instead.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010131
Eric Christophercccae792015-01-30 22:02:31 +000010132 return SDValue(*UI, 0);
10133 }
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010134 }
10135 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000010136
10137 if (isa<ConstantSDNode>(Add->getOperand(1))) {
10138 SDNode *BasePtr = Add->getOperand(0).getNode();
10139 for (SDNode::use_iterator UI = BasePtr->use_begin(),
10140 UE = BasePtr->use_end(); UI != UE; ++UI) {
10141 if (UI->getOpcode() == ISD::ADD &&
10142 isa<ConstantSDNode>(UI->getOperand(1)) &&
10143 (cast<ConstantSDNode>(Add->getOperand(1))->getZExtValue() -
10144 cast<ConstantSDNode>(UI->getOperand(1))->getZExtValue()) %
Aaron Ballman5561ed42015-02-25 13:05:24 +000010145 (1ULL << Bits) == 0) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000010146 SDNode *OtherAdd = *UI;
10147 for (SDNode::use_iterator VI = OtherAdd->use_begin(),
10148 VE = OtherAdd->use_end(); VI != VE; ++VI) {
10149 if (VI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
10150 cast<ConstantSDNode>(VI->getOperand(0))->getZExtValue() == IID) {
10151 return SDValue(*VI, 0);
10152 }
10153 }
10154 }
10155 }
10156 }
Hal Finkelbc2ee4c2013-05-25 04:05:05 +000010157 }
10158 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +000010159
10160 break;
Bill Schmidtfae5d712014-12-09 16:35:51 +000010161 case ISD::INTRINSIC_W_CHAIN: {
10162 // For little endian, VSX loads require generating lxvd2x/xxswapd.
Eric Christophercccae792015-01-30 22:02:31 +000010163 if (Subtarget.hasVSX() && Subtarget.isLittleEndian()) {
Bill Schmidtfae5d712014-12-09 16:35:51 +000010164 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10165 default:
10166 break;
10167 case Intrinsic::ppc_vsx_lxvw4x:
10168 case Intrinsic::ppc_vsx_lxvd2x:
10169 return expandVSXLoadForLE(N, DCI);
10170 }
10171 }
10172 break;
10173 }
10174 case ISD::INTRINSIC_VOID: {
10175 // For little endian, VSX stores require generating xxswapd/stxvd2x.
Eric Christophercccae792015-01-30 22:02:31 +000010176 if (Subtarget.hasVSX() && Subtarget.isLittleEndian()) {
Bill Schmidtfae5d712014-12-09 16:35:51 +000010177 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10178 default:
10179 break;
10180 case Intrinsic::ppc_vsx_stxvw4x:
10181 case Intrinsic::ppc_vsx_stxvd2x:
10182 return expandVSXStoreForLE(N, DCI);
10183 }
10184 }
10185 break;
10186 }
Chris Lattnera7976d32006-07-10 20:56:58 +000010187 case ISD::BSWAP:
10188 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +000010189 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +000010190 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +000010191 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
Eric Christophercccae792015-01-30 22:02:31 +000010192 (Subtarget.hasLDBRX() && Subtarget.isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +000010193 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010194 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +000010195 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +000010196 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010197 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +000010198 LD->getChain(), // Chain
10199 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +000010200 DAG.getValueType(N->getValueType(0)) // VT
10201 };
Dan Gohman48b185d2009-09-25 20:36:54 +000010202 SDValue BSLoad =
10203 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +000010204 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
10205 MVT::i64 : MVT::i32, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +000010206 Ops, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +000010207
Scott Michelcf0da6c2009-02-17 22:15:04 +000010208 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010209 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +000010210 if (N->getValueType(0) == MVT::i16)
10211 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010212
Chris Lattnera7976d32006-07-10 20:56:58 +000010213 // First, combine the bswap away. This makes the value produced by the
10214 // load dead.
10215 DCI.CombineTo(N, ResVal);
10216
10217 // Next, combine the load away, we give it a bogus result value but a real
10218 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +000010219 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +000010220
Chris Lattnera7976d32006-07-10 20:56:58 +000010221 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010222 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +000010223 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010224
Chris Lattner27f53452006-03-01 05:50:56 +000010225 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +000010226 case PPCISD::VCMP: {
10227 // If a VCMPo node already exists with exactly the same operands as this
10228 // node, use its result instead of this node (VCMPo computes both a CR6 and
10229 // a normal output).
10230 //
10231 if (!N->getOperand(0).hasOneUse() &&
10232 !N->getOperand(1).hasOneUse() &&
10233 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000010234
Chris Lattnerd4058a52006-03-31 06:02:07 +000010235 // Scan all of the users of the LHS, looking for VCMPo's that match.
Craig Topper062a2ba2014-04-25 05:30:21 +000010236 SDNode *VCMPoNode = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010237
Gabor Greiff304a7a2008-08-28 21:40:38 +000010238 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +000010239 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
10240 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +000010241 if (UI->getOpcode() == PPCISD::VCMPo &&
10242 UI->getOperand(1) == N->getOperand(1) &&
10243 UI->getOperand(2) == N->getOperand(2) &&
10244 UI->getOperand(0) == N->getOperand(0)) {
10245 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +000010246 break;
10247 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010248
Chris Lattner518834c2006-04-18 18:28:22 +000010249 // If there is no VCMPo node, or if the flag value has a single use, don't
10250 // transform this.
10251 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
10252 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010253
10254 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +000010255 // chain, this transformation is more complex. Note that multiple things
10256 // could use the value result, which we should ignore.
Craig Topper062a2ba2014-04-25 05:30:21 +000010257 SDNode *FlagUser = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010258 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Craig Topper062a2ba2014-04-25 05:30:21 +000010259 FlagUser == nullptr; ++UI) {
Chris Lattner518834c2006-04-18 18:28:22 +000010260 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +000010261 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +000010262 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010263 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +000010264 FlagUser = User;
10265 break;
10266 }
10267 }
10268 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010269
Ulrich Weigandd5ebc622013-07-03 17:05:42 +000010270 // If the user is a MFOCRF instruction, we know this is safe.
10271 // Otherwise we give up for right now.
10272 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010273 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +000010274 }
10275 break;
10276 }
Hal Finkel940ab932014-02-28 00:27:01 +000010277 case ISD::BRCOND: {
10278 SDValue Cond = N->getOperand(1);
10279 SDValue Target = N->getOperand(2);
10280
10281 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
10282 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
10283 Intrinsic::ppc_is_decremented_ctr_nonzero) {
10284
10285 // We now need to make the intrinsic dead (it cannot be instruction
10286 // selected).
10287 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
10288 assert(Cond.getNode()->hasOneUse() &&
10289 "Counter decrement has more than one use");
10290
10291 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
10292 N->getOperand(0), Target);
10293 }
10294 }
10295 break;
Chris Lattner9754d142006-04-18 17:59:36 +000010296 case ISD::BR_CC: {
10297 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +000010298 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +000010299 // lowering is done pre-legalize, because the legalizer lowers the predicate
10300 // compare down to code that is difficult to reassemble.
10301 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010302 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +000010303
10304 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
10305 // value. If so, pass-through the AND to get to the intrinsic.
10306 if (LHS.getOpcode() == ISD::AND &&
10307 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
10308 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
10309 Intrinsic::ppc_is_decremented_ctr_nonzero &&
10310 isa<ConstantSDNode>(LHS.getOperand(1)) &&
10311 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
10312 isZero())
10313 LHS = LHS.getOperand(0);
10314
10315 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
10316 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
10317 Intrinsic::ppc_is_decremented_ctr_nonzero &&
10318 isa<ConstantSDNode>(RHS)) {
10319 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
10320 "Counter decrement comparison is not EQ or NE");
10321
10322 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
10323 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
10324 (CC == ISD::SETNE && !Val);
10325
10326 // We now need to make the intrinsic dead (it cannot be instruction
10327 // selected).
10328 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
10329 assert(LHS.getNode()->hasOneUse() &&
10330 "Counter decrement has more than one use");
10331
10332 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
10333 N->getOperand(0), N->getOperand(4));
10334 }
10335
Chris Lattner9754d142006-04-18 17:59:36 +000010336 int CompareOpc;
10337 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010338
Chris Lattner9754d142006-04-18 17:59:36 +000010339 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
10340 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
Kit Barton0cfa7b72015-03-03 19:55:45 +000010341 getAltivecCompareInfo(LHS, CompareOpc, isDot, Subtarget)) {
Chris Lattner9754d142006-04-18 17:59:36 +000010342 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +000010343
Chris Lattner9754d142006-04-18 17:59:36 +000010344 // If this is a comparison against something other than 0/1, then we know
10345 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +000010346 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +000010347 if (Val != 0 && Val != 1) {
10348 if (CC == ISD::SETEQ) // Cond never true, remove branch.
10349 return N->getOperand(0);
10350 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +000010351 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +000010352 N->getOperand(0), N->getOperand(4));
10353 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010354
Chris Lattner9754d142006-04-18 17:59:36 +000010355 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010356
Chris Lattner9754d142006-04-18 17:59:36 +000010357 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010358 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +000010359 LHS.getOperand(2), // LHS of compare
10360 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +000010361 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +000010362 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +000010363 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +000010364 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010365
Chris Lattner9754d142006-04-18 17:59:36 +000010366 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000010367 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +000010368 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +000010369 default: // Can't happen, don't crash on invalid number though.
10370 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000010371 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +000010372 break;
10373 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000010374 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +000010375 break;
10376 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000010377 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +000010378 break;
10379 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +000010380 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +000010381 break;
10382 }
10383
Owen Anderson9f944592009-08-11 20:47:22 +000010384 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
10385 DAG.getConstant(CompOpc, MVT::i32),
10386 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +000010387 N->getOperand(4), CompNode.getValue(1));
10388 }
10389 break;
10390 }
Chris Lattnerf4184352006-03-01 04:57:39 +000010391 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010392
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010393 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +000010394}
10395
Hal Finkel13d104b2014-12-11 18:37:52 +000010396SDValue
10397PPCTargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
10398 SelectionDAG &DAG,
10399 std::vector<SDNode *> *Created) const {
10400 // fold (sdiv X, pow2)
10401 EVT VT = N->getValueType(0);
Hal Finkel04b16b52014-12-23 08:38:50 +000010402 if (VT == MVT::i64 && !Subtarget.isPPC64())
10403 return SDValue();
Hal Finkel13d104b2014-12-11 18:37:52 +000010404 if ((VT != MVT::i32 && VT != MVT::i64) ||
10405 !(Divisor.isPowerOf2() || (-Divisor).isPowerOf2()))
10406 return SDValue();
10407
10408 SDLoc DL(N);
10409 SDValue N0 = N->getOperand(0);
10410
10411 bool IsNegPow2 = (-Divisor).isPowerOf2();
10412 unsigned Lg2 = (IsNegPow2 ? -Divisor : Divisor).countTrailingZeros();
10413 SDValue ShiftAmt = DAG.getConstant(Lg2, VT);
10414
10415 SDValue Op = DAG.getNode(PPCISD::SRA_ADDZE, DL, VT, N0, ShiftAmt);
10416 if (Created)
10417 Created->push_back(Op.getNode());
10418
10419 if (IsNegPow2) {
10420 Op = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT), Op);
10421 if (Created)
10422 Created->push_back(Op.getNode());
10423 }
10424
10425 return Op;
10426}
10427
Chris Lattner4211ca92006-04-14 06:01:58 +000010428//===----------------------------------------------------------------------===//
10429// Inline Assembly Support
10430//===----------------------------------------------------------------------===//
10431
Jay Foada0653a32014-05-14 21:14:37 +000010432void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10433 APInt &KnownZero,
10434 APInt &KnownOne,
10435 const SelectionDAG &DAG,
10436 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +000010437 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +000010438 switch (Op.getOpcode()) {
10439 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +000010440 case PPCISD::LBRX: {
10441 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +000010442 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +000010443 KnownZero = 0xFFFF0000;
10444 break;
10445 }
Chris Lattnerc5287c02006-04-02 06:26:07 +000010446 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010447 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +000010448 default: break;
10449 case Intrinsic::ppc_altivec_vcmpbfp_p:
10450 case Intrinsic::ppc_altivec_vcmpeqfp_p:
10451 case Intrinsic::ppc_altivec_vcmpequb_p:
10452 case Intrinsic::ppc_altivec_vcmpequh_p:
10453 case Intrinsic::ppc_altivec_vcmpequw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000010454 case Intrinsic::ppc_altivec_vcmpequd_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000010455 case Intrinsic::ppc_altivec_vcmpgefp_p:
10456 case Intrinsic::ppc_altivec_vcmpgtfp_p:
10457 case Intrinsic::ppc_altivec_vcmpgtsb_p:
10458 case Intrinsic::ppc_altivec_vcmpgtsh_p:
10459 case Intrinsic::ppc_altivec_vcmpgtsw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000010460 case Intrinsic::ppc_altivec_vcmpgtsd_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000010461 case Intrinsic::ppc_altivec_vcmpgtub_p:
10462 case Intrinsic::ppc_altivec_vcmpgtuh_p:
10463 case Intrinsic::ppc_altivec_vcmpgtuw_p:
Kit Barton0cfa7b72015-03-03 19:55:45 +000010464 case Intrinsic::ppc_altivec_vcmpgtud_p:
Chris Lattnerc5287c02006-04-02 06:26:07 +000010465 KnownZero = ~1U; // All bits but the low one are known to be zero.
10466 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010467 }
Chris Lattnerc5287c02006-04-02 06:26:07 +000010468 }
10469 }
10470}
10471
Hal Finkel57725662015-01-03 17:58:24 +000010472unsigned PPCTargetLowering::getPrefLoopAlignment(MachineLoop *ML) const {
10473 switch (Subtarget.getDarwinDirective()) {
10474 default: break;
10475 case PPC::DIR_970:
10476 case PPC::DIR_PWR4:
10477 case PPC::DIR_PWR5:
10478 case PPC::DIR_PWR5X:
10479 case PPC::DIR_PWR6:
10480 case PPC::DIR_PWR6X:
10481 case PPC::DIR_PWR7:
10482 case PPC::DIR_PWR8: {
10483 if (!ML)
10484 break;
10485
Eric Christophercccae792015-01-30 22:02:31 +000010486 const PPCInstrInfo *TII = Subtarget.getInstrInfo();
Hal Finkel57725662015-01-03 17:58:24 +000010487
10488 // For small loops (between 5 and 8 instructions), align to a 32-byte
10489 // boundary so that the entire loop fits in one instruction-cache line.
10490 uint64_t LoopSize = 0;
10491 for (auto I = ML->block_begin(), IE = ML->block_end(); I != IE; ++I)
10492 for (auto J = (*I)->begin(), JE = (*I)->end(); J != JE; ++J)
10493 LoopSize += TII->GetInstSizeInBytes(J);
10494
10495 if (LoopSize > 16 && LoopSize <= 32)
10496 return 5;
10497
10498 break;
10499 }
10500 }
10501
10502 return TargetLowering::getPrefLoopAlignment(ML);
10503}
Chris Lattnerc5287c02006-04-02 06:26:07 +000010504
Chris Lattnerd6855142007-03-25 02:14:49 +000010505/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +000010506/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +000010507PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010508PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
10509 if (Constraint.size() == 1) {
10510 switch (Constraint[0]) {
10511 default: break;
10512 case 'b':
10513 case 'r':
10514 case 'f':
10515 case 'v':
10516 case 'y':
10517 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +000010518 case 'Z':
10519 // FIXME: While Z does indicate a memory constraint, it specifically
10520 // indicates an r+r address (used in conjunction with the 'y' modifier
10521 // in the replacement string). Currently, we're forcing the base
10522 // register to be r0 in the asm printer (which is interpreted as zero)
10523 // and forming the complete address in the second register. This is
10524 // suboptimal.
10525 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010526 }
Hal Finkel6aca2372014-03-02 18:23:39 +000010527 } else if (Constraint == "wc") { // individual CR bits.
10528 return C_RegisterClass;
Hal Finkel27774d92014-03-13 07:58:58 +000010529 } else if (Constraint == "wa" || Constraint == "wd" ||
10530 Constraint == "wf" || Constraint == "ws") {
10531 return C_RegisterClass; // VSX registers.
Chris Lattnerd6855142007-03-25 02:14:49 +000010532 }
10533 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +000010534}
10535
John Thompsone8360b72010-10-29 17:29:13 +000010536/// Examine constraint type and operand type and determine a weight value.
10537/// This object must already have been set up with the operand type
10538/// and the current alternative constraint selected.
10539TargetLowering::ConstraintWeight
10540PPCTargetLowering::getSingleConstraintMatchWeight(
10541 AsmOperandInfo &info, const char *constraint) const {
10542 ConstraintWeight weight = CW_Invalid;
10543 Value *CallOperandVal = info.CallOperandVal;
10544 // If we don't have a value, we can't do a match,
10545 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010546 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010547 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010548 Type *type = CallOperandVal->getType();
Hal Finkel6aca2372014-03-02 18:23:39 +000010549
John Thompsone8360b72010-10-29 17:29:13 +000010550 // Look at the constraint type.
Hal Finkel6aca2372014-03-02 18:23:39 +000010551 if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
10552 return CW_Register; // an individual CR bit.
Hal Finkel27774d92014-03-13 07:58:58 +000010553 else if ((StringRef(constraint) == "wa" ||
10554 StringRef(constraint) == "wd" ||
10555 StringRef(constraint) == "wf") &&
10556 type->isVectorTy())
10557 return CW_Register;
10558 else if (StringRef(constraint) == "ws" && type->isDoubleTy())
10559 return CW_Register;
Hal Finkel6aca2372014-03-02 18:23:39 +000010560
John Thompsone8360b72010-10-29 17:29:13 +000010561 switch (*constraint) {
10562 default:
10563 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10564 break;
10565 case 'b':
10566 if (type->isIntegerTy())
10567 weight = CW_Register;
10568 break;
10569 case 'f':
10570 if (type->isFloatTy())
10571 weight = CW_Register;
10572 break;
10573 case 'd':
10574 if (type->isDoubleTy())
10575 weight = CW_Register;
10576 break;
10577 case 'v':
10578 if (type->isVectorTy())
10579 weight = CW_Register;
10580 break;
10581 case 'y':
10582 weight = CW_Register;
10583 break;
Hal Finkel4f24c622012-11-05 18:18:42 +000010584 case 'Z':
10585 weight = CW_Memory;
10586 break;
John Thompsone8360b72010-10-29 17:29:13 +000010587 }
10588 return weight;
10589}
10590
Eric Christopher11e4df72015-02-26 22:38:43 +000010591std::pair<unsigned, const TargetRegisterClass *>
10592PPCTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
10593 const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010594 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +000010595 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +000010596 // GCC RS6000 Constraint Letters
10597 switch (Constraint[0]) {
10598 case 'b': // R1-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010599 if (VT == MVT::i64 && Subtarget.isPPC64())
Hal Finkel638a9fa2013-03-19 18:51:05 +000010600 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
10601 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000010602 case 'r': // R0-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010603 if (VT == MVT::i64 && Subtarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +000010604 return std::make_pair(0U, &PPC::G8RCRegClass);
10605 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000010606 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +000010607 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +000010608 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +000010609 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +000010610 return std::make_pair(0U, &PPC::F8RCRegClass);
Hal Finkelc93a9a22015-02-25 01:06:45 +000010611 if (VT == MVT::v4f64 && Subtarget.hasQPX())
10612 return std::make_pair(0U, &PPC::QFRCRegClass);
10613 if (VT == MVT::v4f32 && Subtarget.hasQPX())
10614 return std::make_pair(0U, &PPC::QSRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000010615 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010616 case 'v':
Hal Finkelc93a9a22015-02-25 01:06:45 +000010617 if (VT == MVT::v4f64 && Subtarget.hasQPX())
10618 return std::make_pair(0U, &PPC::QFRCRegClass);
10619 if (VT == MVT::v4f32 && Subtarget.hasQPX())
10620 return std::make_pair(0U, &PPC::QSRCRegClass);
Craig Topperabadc662012-04-20 06:31:50 +000010621 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +000010622 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +000010623 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +000010624 }
Hal Finkel6aca2372014-03-02 18:23:39 +000010625 } else if (Constraint == "wc") { // an individual CR bit.
10626 return std::make_pair(0U, &PPC::CRBITRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +000010627 } else if (Constraint == "wa" || Constraint == "wd" ||
Hal Finkel19be5062014-03-29 05:29:01 +000010628 Constraint == "wf") {
Hal Finkel27774d92014-03-13 07:58:58 +000010629 return std::make_pair(0U, &PPC::VSRCRegClass);
Hal Finkel19be5062014-03-29 05:29:01 +000010630 } else if (Constraint == "ws") {
10631 return std::make_pair(0U, &PPC::VSFRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +000010632 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010633
Eric Christopher11e4df72015-02-26 22:38:43 +000010634 std::pair<unsigned, const TargetRegisterClass *> R =
10635 TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Hal Finkelb176acb2013-08-03 12:25:10 +000010636
10637 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
10638 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
10639 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
10640 // register.
10641 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
10642 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010643 if (R.first && VT == MVT::i64 && Subtarget.isPPC64() &&
Eric Christopher11e4df72015-02-26 22:38:43 +000010644 PPC::GPRCRegClass.contains(R.first))
Hal Finkelb176acb2013-08-03 12:25:10 +000010645 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +000010646 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +000010647 &PPC::G8RCRegClass);
Hal Finkelb176acb2013-08-03 12:25:10 +000010648
Hal Finkelaa10b3c2014-12-08 22:54:22 +000010649 // GCC accepts 'cc' as an alias for 'cr0', and we need to do the same.
10650 if (!R.second && StringRef("{cc}").equals_lower(Constraint)) {
10651 R.first = PPC::CR0;
10652 R.second = &PPC::CRRCRegClass;
10653 }
10654
Hal Finkelb176acb2013-08-03 12:25:10 +000010655 return R;
Chris Lattner01513612006-01-31 19:20:21 +000010656}
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010657
Chris Lattner584a11a2006-11-02 01:44:04 +000010658
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000010659/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +000010660/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +000010661void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010662 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010663 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +000010664 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010665 SDValue Result;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010666
Eric Christopherde9399b2011-06-02 23:16:42 +000010667 // Only support length 1 constraints.
10668 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010669
Eric Christopherde9399b2011-06-02 23:16:42 +000010670 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010671 switch (Letter) {
10672 default: break;
10673 case 'I':
10674 case 'J':
10675 case 'K':
10676 case 'L':
10677 case 'M':
10678 case 'N':
10679 case 'O':
10680 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +000010681 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000010682 if (!CST) return; // Must be an immediate to match.
Hal Finkelc91fc112014-12-03 09:37:50 +000010683 int64_t Value = CST->getSExtValue();
10684 EVT TCVT = MVT::i64; // All constants taken to be 64 bits so that negative
10685 // numbers are printed as such.
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010686 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010687 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010688 case 'I': // "I" is a signed 16-bit constant.
Hal Finkelc91fc112014-12-03 09:37:50 +000010689 if (isInt<16>(Value))
10690 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010691 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010692 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
Hal Finkelc91fc112014-12-03 09:37:50 +000010693 if (isShiftedUInt<16, 16>(Value))
10694 Result = DAG.getTargetConstant(Value, TCVT);
10695 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010696 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Hal Finkelc91fc112014-12-03 09:37:50 +000010697 if (isShiftedInt<16, 16>(Value))
10698 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010699 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010700 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Hal Finkelc91fc112014-12-03 09:37:50 +000010701 if (isUInt<16>(Value))
10702 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010703 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010704 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +000010705 if (Value > 31)
Hal Finkelc91fc112014-12-03 09:37:50 +000010706 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010707 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010708 case 'N': // "N" is a positive constant that is an exact power of two.
Hal Finkelc91fc112014-12-03 09:37:50 +000010709 if (Value > 0 && isPowerOf2_64(Value))
10710 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010711 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010712 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +000010713 if (Value == 0)
Hal Finkelc91fc112014-12-03 09:37:50 +000010714 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010715 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010716 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Hal Finkelc91fc112014-12-03 09:37:50 +000010717 if (isInt<16>(-Value))
10718 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +000010719 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010720 }
10721 break;
10722 }
10723 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010724
Gabor Greiff304a7a2008-08-28 21:40:38 +000010725 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +000010726 Ops.push_back(Result);
10727 return;
10728 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010729
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010730 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +000010731 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +000010732}
Evan Cheng2dd2c652006-03-13 23:20:37 +000010733
Chris Lattner1eb94d92007-03-30 23:15:24 +000010734// isLegalAddressingMode - Return true if the addressing mode represented
10735// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +000010736bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +000010737 Type *Ty) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +000010738 // PPC does not allow r+i addressing modes for vectors!
10739 if (Ty->isVectorTy() && AM.BaseOffs != 0)
10740 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010741
Chris Lattner1eb94d92007-03-30 23:15:24 +000010742 // PPC allows a sign-extended 16-bit immediate field.
10743 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
10744 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010745
Chris Lattner1eb94d92007-03-30 23:15:24 +000010746 // No global is ever allowed as a base.
10747 if (AM.BaseGV)
10748 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010749
10750 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +000010751 switch (AM.Scale) {
10752 case 0: // "r+i" or just "i", depending on HasBaseReg.
10753 break;
10754 case 1:
10755 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
10756 return false;
10757 // Otherwise we have r+r or r+i.
10758 break;
10759 case 2:
10760 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
10761 return false;
10762 // Allow 2*r as r+r.
10763 break;
Chris Lattner19ccd622007-04-09 22:10:05 +000010764 default:
10765 // No other scales are supported.
10766 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +000010767 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010768
Chris Lattner1eb94d92007-03-30 23:15:24 +000010769 return true;
10770}
10771
Dan Gohman21cea8a2010-04-17 15:26:15 +000010772SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
10773 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +000010774 MachineFunction &MF = DAG.getMachineFunction();
10775 MachineFrameInfo *MFI = MF.getFrameInfo();
10776 MFI->setReturnAddressIsTaken(true);
10777
Bill Wendling908bf812014-01-06 00:43:20 +000010778 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +000010779 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +000010780
Andrew Trickef9de2a2013-05-25 02:42:55 +000010781 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +000010782 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +000010783
Dale Johannesen81bfca72010-05-03 22:59:34 +000010784 // Make sure the function does not optimize away the store of the RA to
10785 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +000010786 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +000010787 FuncInfo->setLRStoreRequired();
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010788 bool isPPC64 = Subtarget.isPPC64();
Dale Johannesen81bfca72010-05-03 22:59:34 +000010789
10790 if (Depth > 0) {
10791 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10792 SDValue Offset =
Eric Christopherf71609b2015-02-13 00:39:27 +000010793 DAG.getConstant(Subtarget.getFrameLowering()->getReturnSaveOffset(),
10794 isPPC64 ? MVT::i64 : MVT::i32);
Dale Johannesen81bfca72010-05-03 22:59:34 +000010795 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
10796 DAG.getNode(ISD::ADD, dl, getPointerTy(),
10797 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +000010798 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +000010799 }
Chris Lattnerf6a81562007-12-08 06:59:59 +000010800
Chris Lattnerf6a81562007-12-08 06:59:59 +000010801 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010802 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +000010803 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +000010804 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +000010805}
10806
Dan Gohman21cea8a2010-04-17 15:26:15 +000010807SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
10808 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010809 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +000010810 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +000010811
Owen Anderson53aa7a92009-08-10 22:56:29 +000010812 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +000010813 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010814
Nicolas Geoffray75ab9792007-03-01 13:11:38 +000010815 MachineFunction &MF = DAG.getMachineFunction();
10816 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +000010817 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +000010818
10819 // Naked functions never have a frame pointer, and so we use r1. For all
10820 // other functions, this decision must be delayed until during PEI.
10821 unsigned FrameReg;
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +000010822 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Hal Finkelaa03c032013-03-21 19:03:19 +000010823 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
10824 else
10825 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
10826
Dale Johannesen81bfca72010-05-03 22:59:34 +000010827 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
10828 PtrVT);
10829 while (Depth--)
10830 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +000010831 FrameAddr, MachinePointerInfo(), false, false,
10832 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +000010833 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +000010834}
Dan Gohmanc14e5222008-10-21 03:41:46 +000010835
Hal Finkel0d8db462014-05-11 19:29:11 +000010836// FIXME? Maybe this could be a TableGen attribute on some registers and
10837// this table could be generated automatically from RegInfo.
10838unsigned PPCTargetLowering::getRegisterByName(const char* RegName,
10839 EVT VT) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000010840 bool isPPC64 = Subtarget.isPPC64();
10841 bool isDarwinABI = Subtarget.isDarwinABI();
Hal Finkel0d8db462014-05-11 19:29:11 +000010842
10843 if ((isPPC64 && VT != MVT::i64 && VT != MVT::i32) ||
10844 (!isPPC64 && VT != MVT::i32))
10845 report_fatal_error("Invalid register global variable type");
10846
10847 bool is64Bit = isPPC64 && VT == MVT::i64;
10848 unsigned Reg = StringSwitch<unsigned>(RegName)
10849 .Case("r1", is64Bit ? PPC::X1 : PPC::R1)
Hal Finkele6698d52015-02-01 15:03:28 +000010850 .Case("r2", (isDarwinABI || isPPC64) ? 0 : PPC::R2)
Hal Finkel0d8db462014-05-11 19:29:11 +000010851 .Case("r13", (!isPPC64 && isDarwinABI) ? 0 :
10852 (is64Bit ? PPC::X13 : PPC::R13))
10853 .Default(0);
10854
10855 if (Reg)
10856 return Reg;
10857 report_fatal_error("Invalid register name global variable");
10858}
10859
Dan Gohmanc14e5222008-10-21 03:41:46 +000010860bool
10861PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10862 // The PowerPC target isn't yet aware of offsets.
10863 return false;
10864}
Tilmann Schellerb93960d2009-07-03 06:45:56 +000010865
Hal Finkel46ef7ce2014-08-13 01:15:40 +000010866bool PPCTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10867 const CallInst &I,
10868 unsigned Intrinsic) const {
10869
10870 switch (Intrinsic) {
Hal Finkelc93a9a22015-02-25 01:06:45 +000010871 case Intrinsic::ppc_qpx_qvlfd:
10872 case Intrinsic::ppc_qpx_qvlfs:
10873 case Intrinsic::ppc_qpx_qvlfcd:
10874 case Intrinsic::ppc_qpx_qvlfcs:
10875 case Intrinsic::ppc_qpx_qvlfiwa:
10876 case Intrinsic::ppc_qpx_qvlfiwz:
Hal Finkel46ef7ce2014-08-13 01:15:40 +000010877 case Intrinsic::ppc_altivec_lvx:
10878 case Intrinsic::ppc_altivec_lvxl:
10879 case Intrinsic::ppc_altivec_lvebx:
10880 case Intrinsic::ppc_altivec_lvehx:
Bill Schmidt72954782014-11-12 04:19:40 +000010881 case Intrinsic::ppc_altivec_lvewx:
10882 case Intrinsic::ppc_vsx_lxvd2x:
10883 case Intrinsic::ppc_vsx_lxvw4x: {
Hal Finkel46ef7ce2014-08-13 01:15:40 +000010884 EVT VT;
10885 switch (Intrinsic) {
10886 case Intrinsic::ppc_altivec_lvebx:
10887 VT = MVT::i8;
10888 break;
10889 case Intrinsic::ppc_altivec_lvehx:
10890 VT = MVT::i16;
10891 break;
10892 case Intrinsic::ppc_altivec_lvewx:
10893 VT = MVT::i32;
10894 break;
Bill Schmidt72954782014-11-12 04:19:40 +000010895 case Intrinsic::ppc_vsx_lxvd2x:
10896 VT = MVT::v2f64;
10897 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +000010898 case Intrinsic::ppc_qpx_qvlfd:
10899 VT = MVT::v4f64;
10900 break;
10901 case Intrinsic::ppc_qpx_qvlfs:
10902 VT = MVT::v4f32;
10903 break;
10904 case Intrinsic::ppc_qpx_qvlfcd:
10905 VT = MVT::v2f64;
10906 break;
10907 case Intrinsic::ppc_qpx_qvlfcs:
10908 VT = MVT::v2f32;
10909 break;
Hal Finkel46ef7ce2014-08-13 01:15:40 +000010910 default:
10911 VT = MVT::v4i32;
10912 break;
10913 }
10914
10915 Info.opc = ISD::INTRINSIC_W_CHAIN;
10916 Info.memVT = VT;
10917 Info.ptrVal = I.getArgOperand(0);
10918 Info.offset = -VT.getStoreSize()+1;
10919 Info.size = 2*VT.getStoreSize()-1;
10920 Info.align = 1;
10921 Info.vol = false;
10922 Info.readMem = true;
10923 Info.writeMem = false;
10924 return true;
10925 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000010926 case Intrinsic::ppc_qpx_qvlfda:
10927 case Intrinsic::ppc_qpx_qvlfsa:
10928 case Intrinsic::ppc_qpx_qvlfcda:
10929 case Intrinsic::ppc_qpx_qvlfcsa:
10930 case Intrinsic::ppc_qpx_qvlfiwaa:
10931 case Intrinsic::ppc_qpx_qvlfiwza: {
10932 EVT VT;
10933 switch (Intrinsic) {
10934 case Intrinsic::ppc_qpx_qvlfda:
10935 VT = MVT::v4f64;
10936 break;
10937 case Intrinsic::ppc_qpx_qvlfsa:
10938 VT = MVT::v4f32;
10939 break;
10940 case Intrinsic::ppc_qpx_qvlfcda:
10941 VT = MVT::v2f64;
10942 break;
10943 case Intrinsic::ppc_qpx_qvlfcsa:
10944 VT = MVT::v2f32;
10945 break;
10946 default:
10947 VT = MVT::v4i32;
10948 break;
10949 }
10950
10951 Info.opc = ISD::INTRINSIC_W_CHAIN;
10952 Info.memVT = VT;
10953 Info.ptrVal = I.getArgOperand(0);
10954 Info.offset = 0;
10955 Info.size = VT.getStoreSize();
10956 Info.align = 1;
10957 Info.vol = false;
10958 Info.readMem = true;
10959 Info.writeMem = false;
10960 return true;
10961 }
10962 case Intrinsic::ppc_qpx_qvstfd:
10963 case Intrinsic::ppc_qpx_qvstfs:
10964 case Intrinsic::ppc_qpx_qvstfcd:
10965 case Intrinsic::ppc_qpx_qvstfcs:
10966 case Intrinsic::ppc_qpx_qvstfiw:
Hal Finkel46ef7ce2014-08-13 01:15:40 +000010967 case Intrinsic::ppc_altivec_stvx:
10968 case Intrinsic::ppc_altivec_stvxl:
10969 case Intrinsic::ppc_altivec_stvebx:
10970 case Intrinsic::ppc_altivec_stvehx:
Bill Schmidt72954782014-11-12 04:19:40 +000010971 case Intrinsic::ppc_altivec_stvewx:
10972 case Intrinsic::ppc_vsx_stxvd2x:
10973 case Intrinsic::ppc_vsx_stxvw4x: {
Hal Finkel46ef7ce2014-08-13 01:15:40 +000010974 EVT VT;
10975 switch (Intrinsic) {
10976 case Intrinsic::ppc_altivec_stvebx:
10977 VT = MVT::i8;
10978 break;
10979 case Intrinsic::ppc_altivec_stvehx:
10980 VT = MVT::i16;
10981 break;
10982 case Intrinsic::ppc_altivec_stvewx:
10983 VT = MVT::i32;
10984 break;
Bill Schmidt72954782014-11-12 04:19:40 +000010985 case Intrinsic::ppc_vsx_stxvd2x:
10986 VT = MVT::v2f64;
10987 break;
Hal Finkelc93a9a22015-02-25 01:06:45 +000010988 case Intrinsic::ppc_qpx_qvstfd:
10989 VT = MVT::v4f64;
10990 break;
10991 case Intrinsic::ppc_qpx_qvstfs:
10992 VT = MVT::v4f32;
10993 break;
10994 case Intrinsic::ppc_qpx_qvstfcd:
10995 VT = MVT::v2f64;
10996 break;
10997 case Intrinsic::ppc_qpx_qvstfcs:
10998 VT = MVT::v2f32;
10999 break;
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011000 default:
11001 VT = MVT::v4i32;
11002 break;
11003 }
11004
11005 Info.opc = ISD::INTRINSIC_VOID;
11006 Info.memVT = VT;
11007 Info.ptrVal = I.getArgOperand(1);
11008 Info.offset = -VT.getStoreSize()+1;
11009 Info.size = 2*VT.getStoreSize()-1;
11010 Info.align = 1;
11011 Info.vol = false;
11012 Info.readMem = false;
11013 Info.writeMem = true;
11014 return true;
11015 }
Hal Finkelc93a9a22015-02-25 01:06:45 +000011016 case Intrinsic::ppc_qpx_qvstfda:
11017 case Intrinsic::ppc_qpx_qvstfsa:
11018 case Intrinsic::ppc_qpx_qvstfcda:
11019 case Intrinsic::ppc_qpx_qvstfcsa:
11020 case Intrinsic::ppc_qpx_qvstfiwa: {
11021 EVT VT;
11022 switch (Intrinsic) {
11023 case Intrinsic::ppc_qpx_qvstfda:
11024 VT = MVT::v4f64;
11025 break;
11026 case Intrinsic::ppc_qpx_qvstfsa:
11027 VT = MVT::v4f32;
11028 break;
11029 case Intrinsic::ppc_qpx_qvstfcda:
11030 VT = MVT::v2f64;
11031 break;
11032 case Intrinsic::ppc_qpx_qvstfcsa:
11033 VT = MVT::v2f32;
11034 break;
11035 default:
11036 VT = MVT::v4i32;
11037 break;
11038 }
11039
11040 Info.opc = ISD::INTRINSIC_VOID;
11041 Info.memVT = VT;
11042 Info.ptrVal = I.getArgOperand(1);
11043 Info.offset = 0;
11044 Info.size = VT.getStoreSize();
11045 Info.align = 1;
11046 Info.vol = false;
11047 Info.readMem = false;
11048 Info.writeMem = true;
11049 return true;
11050 }
Hal Finkel46ef7ce2014-08-13 01:15:40 +000011051 default:
11052 break;
11053 }
11054
11055 return false;
11056}
11057
Evan Chengd9929f02010-04-01 20:10:42 +000011058/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +000011059/// and store operations as a result of memset, memcpy, and memmove
11060/// lowering. If DstAlign is zero that means it's safe to destination
11061/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
11062/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +000011063/// probably because the source does not need to be loaded. If 'IsMemset' is
11064/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
11065/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
11066/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +000011067/// It returns EVT::Other if the type should be determined using generic
11068/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +000011069EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
11070 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000011071 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +000011072 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +000011073 MachineFunction &MF) const {
Hal Finkel52368d42015-03-31 20:56:09 +000011074 if (getTargetMachine().getOptLevel() != CodeGenOpt::None) {
11075 const Function *F = MF.getFunction();
11076 // When expanding a memset, require at least two QPX instructions to cover
11077 // the cost of loading the value to be stored from the constant pool.
11078 if (Subtarget.hasQPX() && Size >= 32 && (!IsMemset || Size >= 64) &&
11079 (!SrcAlign || SrcAlign >= 32) && (!DstAlign || DstAlign >= 32) &&
11080 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
11081 return MVT::v4f64;
11082 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000011083
Hal Finkel52368d42015-03-31 20:56:09 +000011084 // We should use Altivec/VSX loads and stores when available. For unaligned
11085 // addresses, unaligned VSX loads are only fast starting with the P8.
11086 if (Subtarget.hasAltivec() && Size >= 16 &&
11087 (((!SrcAlign || SrcAlign >= 16) && (!DstAlign || DstAlign >= 16)) ||
11088 ((IsMemset && Subtarget.hasVSX()) || Subtarget.hasP8Vector())))
11089 return MVT::v4i32;
11090 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000011091
Eric Christopherd90a8742014-06-12 22:38:20 +000011092 if (Subtarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +000011093 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +000011094 }
Hal Finkel5c3cacf2015-02-27 19:58:28 +000011095
11096 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +000011097}
Hal Finkel88ed4e32012-04-01 19:23:08 +000011098
Hal Finkel34974ed2014-04-12 21:52:38 +000011099/// \brief Returns true if it is beneficial to convert a load of a constant
11100/// to just the constant itself.
11101bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11102 Type *Ty) const {
11103 assert(Ty->isIntegerTy());
11104
11105 unsigned BitSize = Ty->getPrimitiveSizeInBits();
11106 if (BitSize == 0 || BitSize > 64)
11107 return false;
11108 return true;
11109}
11110
11111bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
11112 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
11113 return false;
11114 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11115 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
11116 return NumBits1 == 64 && NumBits2 == 32;
11117}
11118
11119bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
11120 if (!VT1.isInteger() || !VT2.isInteger())
11121 return false;
11122 unsigned NumBits1 = VT1.getSizeInBits();
11123 unsigned NumBits2 = VT2.getSizeInBits();
11124 return NumBits1 == 64 && NumBits2 == 32;
11125}
11126
Hal Finkel5d5d1532015-01-10 08:21:59 +000011127bool PPCTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
11128 // Generally speaking, zexts are not free, but they are free when they can be
11129 // folded with other operations.
11130 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Val)) {
11131 EVT MemVT = LD->getMemoryVT();
11132 if ((MemVT == MVT::i1 || MemVT == MVT::i8 || MemVT == MVT::i16 ||
11133 (Subtarget.isPPC64() && MemVT == MVT::i32)) &&
11134 (LD->getExtensionType() == ISD::NON_EXTLOAD ||
11135 LD->getExtensionType() == ISD::ZEXTLOAD))
11136 return true;
11137 }
11138
11139 // FIXME: Add other cases...
11140 // - 32-bit shifts with a zext to i64
11141 // - zext after ctlz, bswap, etc.
11142 // - zext after and by a constant mask
11143
11144 return TargetLowering::isZExtFree(Val, VT2);
11145}
11146
Olivier Sallenave32509692015-01-13 15:06:36 +000011147bool PPCTargetLowering::isFPExtFree(EVT VT) const {
11148 assert(VT.isFloatingPoint());
11149 return true;
11150}
11151
Hal Finkel34974ed2014-04-12 21:52:38 +000011152bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11153 return isInt<16>(Imm) || isUInt<16>(Imm);
11154}
11155
11156bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const {
11157 return isInt<16>(Imm) || isUInt<16>(Imm);
11158}
11159
Matt Arsenault6f2a5262014-07-27 17:46:40 +000011160bool PPCTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
11161 unsigned,
11162 unsigned,
11163 bool *Fast) const {
Hal Finkel8d7fbc92013-03-15 15:27:13 +000011164 if (DisablePPCUnaligned)
11165 return false;
11166
11167 // PowerPC supports unaligned memory access for simple non-vector types.
11168 // Although accessing unaligned addresses is not as efficient as accessing
11169 // aligned addresses, it is generally more efficient than manual expansion,
11170 // and generally only traps for software emulation when crossing page
11171 // boundaries.
11172
11173 if (!VT.isSimple())
11174 return false;
11175
Hal Finkel6e28e6a2014-03-26 19:39:09 +000011176 if (VT.getSimpleVT().isVector()) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011177 if (Subtarget.hasVSX()) {
Bill Schmidt2d1128a2014-10-17 15:13:38 +000011178 if (VT != MVT::v2f64 && VT != MVT::v2i64 &&
11179 VT != MVT::v4f32 && VT != MVT::v4i32)
Hal Finkel6e28e6a2014-03-26 19:39:09 +000011180 return false;
11181 } else {
11182 return false;
11183 }
11184 }
Hal Finkel8d7fbc92013-03-15 15:27:13 +000011185
11186 if (VT == MVT::ppcf128)
11187 return false;
11188
11189 if (Fast)
11190 *Fast = true;
11191
11192 return true;
11193}
11194
Stephen Lin73de7bf2013-07-09 18:16:56 +000011195bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
11196 VT = VT.getScalarType();
11197
Hal Finkel0a479ae2012-06-22 00:49:52 +000011198 if (!VT.isSimple())
11199 return false;
11200
11201 switch (VT.getSimpleVT().SimpleTy) {
11202 case MVT::f32:
11203 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +000011204 return true;
11205 default:
11206 break;
11207 }
11208
11209 return false;
11210}
11211
Hal Finkel934361a2015-01-14 01:07:51 +000011212const MCPhysReg *
11213PPCTargetLowering::getScratchRegisters(CallingConv::ID) const {
11214 // LR is a callee-save register, but we must treat it as clobbered by any call
11215 // site. Hence we include LR in the scratch registers, which are in turn added
11216 // as implicit-defs for stackmaps and patchpoints. The same reasoning applies
11217 // to CTR, which is used by any indirect call.
11218 static const MCPhysReg ScratchRegs[] = {
Hal Finkelc19805a2015-01-17 03:57:34 +000011219 PPC::X12, PPC::LR8, PPC::CTR8, 0
Hal Finkel934361a2015-01-14 01:07:51 +000011220 };
11221
11222 return ScratchRegs;
11223}
11224
Hal Finkelb4240ca2014-03-31 17:48:16 +000011225bool
11226PPCTargetLowering::shouldExpandBuildVectorWithShuffles(
11227 EVT VT , unsigned DefinedValues) const {
11228 if (VT == MVT::v2i64)
11229 return false;
11230
Hal Finkelc93a9a22015-02-25 01:06:45 +000011231 if (Subtarget.hasQPX()) {
11232 if (VT == MVT::v4f32 || VT == MVT::v4f64 || VT == MVT::v4i1)
11233 return true;
11234 }
11235
Hal Finkelb4240ca2014-03-31 17:48:16 +000011236 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
11237}
11238
Hal Finkel88ed4e32012-04-01 19:23:08 +000011239Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +000011240 if (DisableILPPref || Subtarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +000011241 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +000011242
Hal Finkel4e9f1a82012-06-10 19:32:29 +000011243 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +000011244}
11245
Bill Schmidt0cf702f2013-07-30 00:50:39 +000011246// Create a fast isel object.
11247FastISel *
11248PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
11249 const TargetLibraryInfo *LibInfo) const {
11250 return PPC::createFastISel(FuncInfo, LibInfo);
11251}