blob: c9e2802f7ad1c6e6e39c998da05632db8dae2151 [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"
Jim Laskey48850c12006-11-16 22:43:37 +000016#include "PPCMachineFunctionInfo.h"
Bill Wendlingdd3fe942010-03-12 02:00:43 +000017#include "PPCPerfectShuffle.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000018#include "PPCTargetMachine.h"
Bill Schmidt22d40dc2013-05-13 19:34:37 +000019#include "PPCTargetObjectFile.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000020#include "llvm/ADT/STLExtras.h"
Hal Finkel0d8db462014-05-11 19:29:11 +000021#include "llvm/ADT/StringSwitch.h"
Eric Christopher89958332014-05-31 00:07:32 +000022#include "llvm/ADT/Triple.h"
Chris Lattner4f2e4e02007-03-06 00:59:59 +000023#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner9b577f12005-08-26 21:23:58 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000028#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000029#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/Constants.h"
32#include "llvm/IR/DerivedTypes.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/Intrinsics.h"
Chris Lattnerce645542006-11-10 02:08:47 +000035#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000036#include "llvm/Support/ErrorHandling.h"
Craig Topperb25fda92012-03-17 18:46:09 +000037#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000038#include "llvm/Support/raw_ostream.h"
Craig Topperb25fda92012-03-17 18:46:09 +000039#include "llvm/Target/TargetOptions.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000040using namespace llvm;
41
Joerg Sonnenbergereb8655a2014-08-08 16:46:10 +000042// FIXME: Remove this once soft-float is supported.
43static cl::opt<bool> DisablePPCFloatInVariadic("disable-ppc-float-in-variadic",
44cl::desc("disable saving float registers for va_start on PPC"), cl::Hidden);
45
Hal Finkel595817e2012-06-04 02:21:00 +000046static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
47cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000048
Hal Finkel4e9f1a82012-06-10 19:32:29 +000049static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
50cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
51
Hal Finkel8d7fbc92013-03-15 15:27:13 +000052static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
53cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
54
Hal Finkel940ab932014-02-28 00:27:01 +000055// FIXME: Remove this once the bug has been fixed!
56extern cl::opt<bool> ANDIGlueBug;
57
Eric Christopher89958332014-05-31 00:07:32 +000058static TargetLoweringObjectFile *createTLOF(const Triple &TT) {
Eric Christophera84189a2014-06-02 17:29:07 +000059 // If it isn't a Mach-O file then it's going to be a linux ELF
60 // object file.
Eric Christopher89958332014-05-31 00:07:32 +000061 if (TT.isOSDarwin())
Bill Wendlingbbcaa402010-03-15 21:09:38 +000062 return new TargetLoweringObjectFileMachO();
Eric Christophera84189a2014-06-02 17:29:07 +000063
64 return new PPC64LinuxTargetObjectFile();
Chris Lattner5e693ed2009-07-28 03:13:23 +000065}
66
Eric Christopherf6ed33e2014-10-01 21:36:28 +000067PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM)
Eric Christopher89958332014-05-31 00:07:32 +000068 : TargetLowering(TM, createTLOF(Triple(TM.getTargetTriple()))),
Eric Christopherb1aaebe2014-06-12 22:38:18 +000069 Subtarget(*TM.getSubtargetImpl()) {
Sanjay Patel2cdea4c2014-08-21 22:31:48 +000070 setPow2SDivIsCheap();
Dale Johannesenc31eb202008-07-31 18:13:12 +000071
Chris Lattnera028e7a2005-09-27 22:18:25 +000072 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000073 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000075
Chris Lattnerd10babf2010-10-10 18:34:00 +000076 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
77 // arguments are at least 4/8 bytes aligned.
Eric Christopherb1aaebe2014-06-12 22:38:18 +000078 bool isPPC64 = Subtarget.isPPC64();
Evan Cheng39e90022012-07-02 22:39:56 +000079 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000080
Chris Lattnerf22556d2005-08-16 17:14:42 +000081 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000082 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
83 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
84 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +000085
Evan Cheng5d9fd972006-10-04 00:56:09 +000086 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson9f944592009-08-11 20:47:22 +000087 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
88 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sands95d46ef2008-01-23 20:39:46 +000089
Owen Anderson9f944592009-08-11 20:47:22 +000090 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000091
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000092 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000093 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
94 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
95 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
96 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
97 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
98 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
99 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
100 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
101 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
102 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +0000103
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000104 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000105 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
106
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000107 if (isPPC64 || Subtarget.hasFPCVT()) {
Hal Finkel6a56b212014-03-05 22:14:00 +0000108 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
109 AddPromotedToType (ISD::SINT_TO_FP, MVT::i1,
110 isPPC64 ? MVT::i64 : MVT::i32);
111 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
112 AddPromotedToType (ISD::UINT_TO_FP, MVT::i1,
113 isPPC64 ? MVT::i64 : MVT::i32);
114 } else {
115 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom);
116 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom);
117 }
Hal Finkel940ab932014-02-28 00:27:01 +0000118
119 // PowerPC does not support direct load / store of condition registers
120 setOperationAction(ISD::LOAD, MVT::i1, Custom);
121 setOperationAction(ISD::STORE, MVT::i1, Custom);
122
123 // FIXME: Remove this once the ANDI glue bug is fixed:
124 if (ANDIGlueBug)
125 setOperationAction(ISD::TRUNCATE, MVT::i1, Custom);
126
127 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
128 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
129 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
130 setTruncStoreAction(MVT::i32, MVT::i1, Expand);
131 setTruncStoreAction(MVT::i16, MVT::i1, Expand);
132 setTruncStoreAction(MVT::i8, MVT::i1, Expand);
133
134 addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass);
135 }
136
Dale Johannesen666323e2007-10-10 01:01:31 +0000137 // This is used in the ppcf128->int sequence. Note it has different semantics
138 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +0000139 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000140
Roman Divacky1faf5b02012-08-16 18:19:29 +0000141 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000142 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
143 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
144 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
145 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
146 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000147 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000148
Chris Lattnerf22556d2005-08-16 17:14:42 +0000149 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000150 setOperationAction(ISD::SREM, MVT::i32, Expand);
151 setOperationAction(ISD::UREM, MVT::i32, Expand);
152 setOperationAction(ISD::SREM, MVT::i64, Expand);
153 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000154
155 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000156 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
157 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
158 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
159 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
160 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
161 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
162 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
163 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000164
Dan Gohman482732a2007-10-11 23:21:31 +0000165 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000166 setOperationAction(ISD::FSIN , MVT::f64, Expand);
167 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000168 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000169 setOperationAction(ISD::FREM , MVT::f64, Expand);
170 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000171 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000172 setOperationAction(ISD::FSIN , MVT::f32, Expand);
173 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000174 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000175 setOperationAction(ISD::FREM , MVT::f32, Expand);
176 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000177 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000178
Owen Anderson9f944592009-08-11 20:47:22 +0000179 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000180
Chris Lattnerf22556d2005-08-16 17:14:42 +0000181 // If we're enabling GP optimizations, use hardware square root
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000182 if (!Subtarget.hasFSQRT() &&
Hal Finkel2e103312013-04-03 04:01:11 +0000183 !(TM.Options.UnsafeFPMath &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000184 Subtarget.hasFRSQRTE() && Subtarget.hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000185 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000186
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000187 if (!Subtarget.hasFSQRT() &&
Hal Finkel2e103312013-04-03 04:01:11 +0000188 !(TM.Options.UnsafeFPMath &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000189 Subtarget.hasFRSQRTES() && Subtarget.hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000190 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000191
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000192 if (Subtarget.hasFCPSGN()) {
Hal Finkeldbc78e12013-08-19 05:01:02 +0000193 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
194 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
195 } else {
196 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
197 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
198 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000199
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000200 if (Subtarget.hasFPRND()) {
Hal Finkelc20a08d2013-03-29 08:57:48 +0000201 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
202 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
203 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000204 setOperationAction(ISD::FROUND, MVT::f64, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000205
206 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
207 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
208 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000209 setOperationAction(ISD::FROUND, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000210 }
211
Nate Begeman2fba8a32006-01-14 03:14:10 +0000212 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000213 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000214 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000215 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
216 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000217 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000218 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000219 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
220 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000221
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000222 if (Subtarget.hasPOPCNTD()) {
Hal Finkel290376d2013-04-01 15:58:15 +0000223 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000224 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
225 } else {
226 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
227 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
228 }
229
Nate Begeman1b8121b2006-01-11 21:21:00 +0000230 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000231 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
232 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000233
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000234 if (!Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000235 // PowerPC does not have Select
236 setOperationAction(ISD::SELECT, MVT::i32, Expand);
237 setOperationAction(ISD::SELECT, MVT::i64, Expand);
238 setOperationAction(ISD::SELECT, MVT::f32, Expand);
239 setOperationAction(ISD::SELECT, MVT::f64, Expand);
240 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000241
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000242 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000243 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
244 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000245
Nate Begeman7e7f4392006-02-01 07:19:44 +0000246 // PowerPC wants to optimize integer setcc a bit
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000247 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000248 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000249
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000250 // PowerPC does not have BRCOND which requires SetCC
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000251 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000252 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000253
Owen Anderson9f944592009-08-11 20:47:22 +0000254 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000255
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000256 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000257 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000258
Jim Laskey6267b2c2005-08-17 00:40:22 +0000259 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000260 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
261 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000262
Wesley Peck527da1b2010-11-23 03:31:01 +0000263 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
264 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
265 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
266 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattnerc46fc242005-12-23 05:13:35 +0000267
Chris Lattner84b49d52006-04-28 21:56:10 +0000268 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000269 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000270
Hal Finkel1996f3d2013-03-27 19:10:42 +0000271 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000272 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
273 // support continuation, user-level threading, and etc.. As a result, no
274 // other SjLj exception interfaces are implemented and please don't build
275 // your own exception handling based on them.
276 // LLVM/Clang supports zero-cost DWARF exception handling.
277 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
278 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000279
280 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000281 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000282 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
283 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000284 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000285 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
286 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
287 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
288 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000289 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000290 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
291 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000292
Nate Begemanf69d13b2008-08-11 17:36:31 +0000293 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000294 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000295
296 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000297 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
298 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000299
Nate Begemane74795c2006-01-25 18:21:52 +0000300 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000301 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000302
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000303 if (Subtarget.isSVR4ABI()) {
Evan Cheng39e90022012-07-02 22:39:56 +0000304 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000305 // VAARG always uses double-word chunks, so promote anything smaller.
306 setOperationAction(ISD::VAARG, MVT::i1, Promote);
307 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
308 setOperationAction(ISD::VAARG, MVT::i8, Promote);
309 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
310 setOperationAction(ISD::VAARG, MVT::i16, Promote);
311 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
312 setOperationAction(ISD::VAARG, MVT::i32, Promote);
313 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
314 setOperationAction(ISD::VAARG, MVT::Other, Expand);
315 } else {
316 // VAARG is custom lowered with the 32-bit SVR4 ABI.
317 setOperationAction(ISD::VAARG, MVT::Other, Custom);
318 setOperationAction(ISD::VAARG, MVT::i64, Custom);
319 }
Roman Divacky4394e682011-06-28 15:30:42 +0000320 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000321 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000322
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000323 if (Subtarget.isSVR4ABI() && !isPPC64)
Roman Divackyc3825df2013-07-25 21:36:47 +0000324 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
325 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
326 else
327 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
328
Chris Lattner5bd514d2006-01-15 09:02:48 +0000329 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000330 setOperationAction(ISD::VAEND , MVT::Other, Expand);
331 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
332 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
333 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
334 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000335
Chris Lattner6961fc72006-03-26 10:06:40 +0000336 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000337 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000338
Hal Finkel25c19922013-05-15 21:37:41 +0000339 // To handle counter-based loop conditions.
340 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
341
Dale Johannesen160be0f2008-11-07 22:54:33 +0000342 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000343 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
344 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
345 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
346 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
347 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
348 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
349 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
350 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
351 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
352 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
353 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
354 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000355
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000356 if (Subtarget.has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000357 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000358 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
359 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
360 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
361 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000362 // This is just the low 32 bits of a (signed) fp->i64 conversion.
363 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000364 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000365
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000366 if (Subtarget.hasLFIWAX() || Subtarget.isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000367 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000368 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000369 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000370 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000371 }
372
Hal Finkelf6d45f22013-04-01 17:52:07 +0000373 // With the instructions enabled under FPCVT, we can do everything.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000374 if (Subtarget.hasFPCVT()) {
375 if (Subtarget.has64BitSupport()) {
Hal Finkelf6d45f22013-04-01 17:52:07 +0000376 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
377 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
378 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
379 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
380 }
381
382 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
383 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
384 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
385 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
386 }
387
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000388 if (Subtarget.use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000389 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000390 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000391 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000392 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000393 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000394 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
395 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
396 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000397 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000398 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000399 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
400 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
401 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000402 }
Evan Cheng19264272006-03-01 01:11:20 +0000403
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000404 if (Subtarget.hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000405 // First set operation action for all vector types to expand. Then we
406 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson9f944592009-08-11 20:47:22 +0000407 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
408 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
409 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands13237ac2008-06-06 12:08:01 +0000410
Chris Lattner06a21ba2006-04-16 01:37:57 +0000411 // add/sub are legal for all supported vector VT's.
Duncan Sands13237ac2008-06-06 12:08:01 +0000412 setOperationAction(ISD::ADD , VT, Legal);
413 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000414
Chris Lattner95c7adc2006-04-04 17:25:31 +0000415 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000416 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000417 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000418
419 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000420 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000421 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000422 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000423 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000424 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000425 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000426 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000427 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000428 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000429 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000430 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000431 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000432
Chris Lattner06a21ba2006-04-16 01:37:57 +0000433 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000434 setOperationAction(ISD::MUL , VT, Expand);
435 setOperationAction(ISD::SDIV, VT, Expand);
436 setOperationAction(ISD::SREM, VT, Expand);
437 setOperationAction(ISD::UDIV, VT, Expand);
438 setOperationAction(ISD::UREM, VT, Expand);
439 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000440 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000441 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000442 setOperationAction(ISD::FSQRT, VT, Expand);
443 setOperationAction(ISD::FLOG, VT, Expand);
444 setOperationAction(ISD::FLOG10, VT, Expand);
445 setOperationAction(ISD::FLOG2, VT, Expand);
446 setOperationAction(ISD::FEXP, VT, Expand);
447 setOperationAction(ISD::FEXP2, VT, Expand);
448 setOperationAction(ISD::FSIN, VT, Expand);
449 setOperationAction(ISD::FCOS, VT, Expand);
450 setOperationAction(ISD::FABS, VT, Expand);
451 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000452 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000453 setOperationAction(ISD::FCEIL, VT, Expand);
454 setOperationAction(ISD::FTRUNC, VT, Expand);
455 setOperationAction(ISD::FRINT, VT, Expand);
456 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000457 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
458 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
459 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
Ulrich Weigand51eccec2014-08-04 13:27:12 +0000460 setOperationAction(ISD::MULHU, VT, Expand);
461 setOperationAction(ISD::MULHS, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000462 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
463 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
464 setOperationAction(ISD::UDIVREM, VT, Expand);
465 setOperationAction(ISD::SDIVREM, VT, Expand);
466 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
467 setOperationAction(ISD::FPOW, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000468 setOperationAction(ISD::BSWAP, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000469 setOperationAction(ISD::CTPOP, VT, Expand);
470 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000471 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000472 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000473 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000474 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000475 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
476
477 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
478 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
479 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
480 setTruncStoreAction(VT, InnerVT, Expand);
481 }
482 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
483 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
484 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000485 }
486
Chris Lattner95c7adc2006-04-04 17:25:31 +0000487 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
488 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000489 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000490
Owen Anderson9f944592009-08-11 20:47:22 +0000491 setOperationAction(ISD::AND , MVT::v4i32, Legal);
492 setOperationAction(ISD::OR , MVT::v4i32, Legal);
493 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
494 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Hal Finkel940ab932014-02-28 00:27:01 +0000495 setOperationAction(ISD::SELECT, MVT::v4i32,
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000496 Subtarget.useCRBits() ? Legal : Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000497 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000498 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
499 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
500 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
501 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000502 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
503 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
504 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
505 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000506
Craig Topperabadc662012-04-20 06:31:50 +0000507 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
508 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
509 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
510 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000511
Owen Anderson9f944592009-08-11 20:47:22 +0000512 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000513 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000514
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000515 if (TM.Options.UnsafeFPMath || Subtarget.hasVSX()) {
Hal Finkel2e103312013-04-03 04:01:11 +0000516 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
517 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
518 }
519
Owen Anderson9f944592009-08-11 20:47:22 +0000520 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
521 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
522 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000523
Owen Anderson9f944592009-08-11 20:47:22 +0000524 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
525 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000526
Owen Anderson9f944592009-08-11 20:47:22 +0000527 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
528 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
529 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
530 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000531
532 // Altivec does not contain unordered floating-point compare instructions
533 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
534 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000535 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
536 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000537
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000538 if (Subtarget.hasVSX()) {
Hal Finkel27774d92014-03-13 07:58:58 +0000539 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
Hal Finkel82569b62014-03-27 22:22:48 +0000540 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
Hal Finkel27774d92014-03-13 07:58:58 +0000541
542 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
543 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
544 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
545 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
546 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
547
548 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
549
550 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
551 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
552
553 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
554 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
555
Hal Finkel732f0f72014-03-26 12:49:28 +0000556 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
557 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal);
558 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
559 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
560 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
561
Hal Finkel27774d92014-03-13 07:58:58 +0000562 // Share the Altivec comparison restrictions.
563 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand);
564 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000565 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand);
566 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand);
567
Hal Finkel9281c9a2014-03-26 18:26:30 +0000568 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
569 setOperationAction(ISD::STORE, MVT::v2f64, Legal);
570
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000571 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal);
572
Hal Finkel19be5062014-03-29 05:29:01 +0000573 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000574
575 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass);
576 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass);
Hal Finkela6c8b512014-03-26 16:12:58 +0000577
578 // VSX v2i64 only supports non-arithmetic operations.
579 setOperationAction(ISD::ADD, MVT::v2i64, Expand);
580 setOperationAction(ISD::SUB, MVT::v2i64, Expand);
581
Hal Finkelad801b72014-03-27 21:26:33 +0000582 setOperationAction(ISD::SHL, MVT::v2i64, Expand);
583 setOperationAction(ISD::SRA, MVT::v2i64, Expand);
584 setOperationAction(ISD::SRL, MVT::v2i64, Expand);
585
Hal Finkel777c9dd2014-03-29 16:04:40 +0000586 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
587
Hal Finkel9281c9a2014-03-26 18:26:30 +0000588 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
589 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64);
590 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
591 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64);
592
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000593 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal);
594
Hal Finkel7279f4b2014-03-26 19:13:54 +0000595 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
596 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
597 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
598 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
599
Hal Finkel5c0d1452014-03-30 13:22:59 +0000600 // Vector operation legalization checks the result type of
601 // SIGN_EXTEND_INREG, overall legalization checks the inner type.
602 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal);
603 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal);
604 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
605 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
606
Hal Finkela6c8b512014-03-26 16:12:58 +0000607 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000608 }
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000609 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000610
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000611 if (Subtarget.has64BitSupport()) {
Hal Finkel322e41a2012-04-01 20:08:17 +0000612 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel70381a72012-08-04 14:10:46 +0000613 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
614 }
Hal Finkel322e41a2012-04-01 20:08:17 +0000615
Robin Morissete1ca44b2014-10-02 22:27:07 +0000616 if (!isPPC64) {
617 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
618 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
619 }
Eli Friedman7dfa7912011-08-29 18:23:02 +0000620
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000621 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidta76bf5a2013-04-23 18:49:44 +0000622 // Altivec instructions set fields to all zeros or all ones.
623 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000624
Joerg Sonnenbergerb5459e62014-07-24 22:20:10 +0000625 if (!isPPC64) {
626 // These libcalls are not available in 32-bit.
627 setLibcallName(RTLIB::SHL_I128, nullptr);
628 setLibcallName(RTLIB::SRL_I128, nullptr);
629 setLibcallName(RTLIB::SRA_I128, nullptr);
630 }
631
Evan Cheng39e90022012-07-02 22:39:56 +0000632 if (isPPC64) {
Chris Lattner454436d2006-10-18 01:20:43 +0000633 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000634 setExceptionPointerRegister(PPC::X3);
635 setExceptionSelectorRegister(PPC::X4);
636 } else {
Chris Lattner454436d2006-10-18 01:20:43 +0000637 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000638 setExceptionPointerRegister(PPC::R3);
639 setExceptionSelectorRegister(PPC::R4);
640 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000641
Chris Lattnerf4184352006-03-01 04:57:39 +0000642 // We have target-specific dag combine patterns for the following nodes:
643 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000644 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000645 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000646 setTargetDAGCombine(ISD::BR_CC);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000647 if (Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000648 setTargetDAGCombine(ISD::BRCOND);
Chris Lattnera7976d32006-07-10 20:56:58 +0000649 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000650 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000651
Hal Finkel46043ed2014-03-01 21:36:57 +0000652 setTargetDAGCombine(ISD::SIGN_EXTEND);
653 setTargetDAGCombine(ISD::ZERO_EXTEND);
654 setTargetDAGCombine(ISD::ANY_EXTEND);
655
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000656 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000657 setTargetDAGCombine(ISD::TRUNCATE);
658 setTargetDAGCombine(ISD::SETCC);
659 setTargetDAGCombine(ISD::SELECT_CC);
660 }
661
Hal Finkel2e103312013-04-03 04:01:11 +0000662 // Use reciprocal estimates.
663 if (TM.Options.UnsafeFPMath) {
664 setTargetDAGCombine(ISD::FDIV);
665 setTargetDAGCombine(ISD::FSQRT);
666 }
667
Dale Johannesen10432e52007-10-19 00:59:18 +0000668 // Darwin long double math library functions have $LDBL128 appended.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000669 if (Subtarget.isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000670 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000671 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
672 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000673 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
674 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000675 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
676 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
677 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
678 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
679 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000680 }
681
Hal Finkel940ab932014-02-28 00:27:01 +0000682 // With 32 condition bits, we don't need to sink (and duplicate) compares
683 // aggressively in CodeGenPrep.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000684 if (Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000685 setHasMultipleConditionRegisters();
686
Hal Finkel65298572011-10-17 18:53:03 +0000687 setMinFunctionAlignment(2);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000688 if (Subtarget.isDarwin())
Hal Finkel65298572011-10-17 18:53:03 +0000689 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000690
Eli Friedman30a49e92011-08-03 21:06:02 +0000691 setInsertFencesForAtomic(true);
692
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000693 if (Subtarget.enableMachineScheduler())
Hal Finkel21442b22013-09-11 23:05:25 +0000694 setSchedulingPreference(Sched::Source);
695 else
696 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000697
Chris Lattnerf22556d2005-08-16 17:14:42 +0000698 computeRegisterProperties();
Hal Finkel742b5352012-08-28 16:12:39 +0000699
700 // The Freescale cores does better with aggressive inlining of memcpy and
701 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000702 if (Subtarget.getDarwinDirective() == PPC::DIR_E500mc ||
703 Subtarget.getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000704 MaxStoresPerMemset = 32;
705 MaxStoresPerMemsetOptSize = 16;
706 MaxStoresPerMemcpy = 32;
707 MaxStoresPerMemcpyOptSize = 8;
708 MaxStoresPerMemmove = 32;
709 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel742b5352012-08-28 16:12:39 +0000710
711 setPrefFunctionAlignment(4);
Hal Finkel742b5352012-08-28 16:12:39 +0000712 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000713}
714
Hal Finkel262a2242013-09-12 23:20:06 +0000715/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
716/// the desired ByVal argument alignment.
717static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
718 unsigned MaxMaxAlign) {
719 if (MaxAlign == MaxMaxAlign)
720 return;
721 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
722 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
723 MaxAlign = 32;
724 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
725 MaxAlign = 16;
726 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
727 unsigned EltAlign = 0;
728 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
729 if (EltAlign > MaxAlign)
730 MaxAlign = EltAlign;
731 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
732 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
733 unsigned EltAlign = 0;
734 getMaxByValAlign(STy->getElementType(i), EltAlign, MaxMaxAlign);
735 if (EltAlign > MaxAlign)
736 MaxAlign = EltAlign;
737 if (MaxAlign == MaxMaxAlign)
738 break;
739 }
740 }
741}
742
Dale Johannesencbde4c22008-02-28 22:31:51 +0000743/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
744/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000745unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000746 // Darwin passes everything on 4 byte boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000747 if (Subtarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000748 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000749
750 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000751 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000752 unsigned Align = Subtarget.isPPC64() ? 8 : 4;
753 if (Subtarget.hasAltivec() || Subtarget.hasQPX())
754 getMaxByValAlign(Ty, Align, Subtarget.hasQPX() ? 32 : 16);
Hal Finkel262a2242013-09-12 23:20:06 +0000755 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000756}
757
Chris Lattner347ed8a2006-01-09 23:52:17 +0000758const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
759 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000760 default: return nullptr;
Evan Cheng32e376f2008-07-12 02:23:19 +0000761 case PPCISD::FSEL: return "PPCISD::FSEL";
762 case PPCISD::FCFID: return "PPCISD::FCFID";
763 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
764 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000765 case PPCISD::FRE: return "PPCISD::FRE";
766 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000767 case PPCISD::STFIWX: return "PPCISD::STFIWX";
768 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
769 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
770 case PPCISD::VPERM: return "PPCISD::VPERM";
771 case PPCISD::Hi: return "PPCISD::Hi";
772 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000773 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller79fef932009-12-18 13:00:15 +0000774 case PPCISD::LOAD: return "PPCISD::LOAD";
775 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000776 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
777 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
778 case PPCISD::SRL: return "PPCISD::SRL";
779 case PPCISD::SRA: return "PPCISD::SRA";
780 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000781 case PPCISD::CALL: return "PPCISD::CALL";
782 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000783 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000784 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng32e376f2008-07-12 02:23:19 +0000785 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel756810f2013-03-21 21:37:52 +0000786 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
787 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000788 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng32e376f2008-07-12 02:23:19 +0000789 case PPCISD::VCMP: return "PPCISD::VCMP";
790 case PPCISD::VCMPo: return "PPCISD::VCMPo";
791 case PPCISD::LBRX: return "PPCISD::LBRX";
792 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng32e376f2008-07-12 02:23:19 +0000793 case PPCISD::LARX: return "PPCISD::LARX";
794 case PPCISD::STCX: return "PPCISD::STCX";
795 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +0000796 case PPCISD::BDNZ: return "PPCISD::BDNZ";
797 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000798 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000799 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000800 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +0000801 case PPCISD::CR6SET: return "PPCISD::CR6SET";
802 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34627e32012-11-27 17:35:46 +0000803 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
804 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
805 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Roman Divacky32143e22013-12-20 18:08:54 +0000806 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000807 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
808 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000809 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000810 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
811 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
812 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000813 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
814 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
815 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
816 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
817 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +0000818 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +0000819 case PPCISD::SC: return "PPCISD::SC";
Chris Lattner347ed8a2006-01-09 23:52:17 +0000820 }
821}
822
Matt Arsenault758659232013-05-18 00:21:46 +0000823EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000824 if (!VT.isVector())
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000825 return Subtarget.useCRBits() ? MVT::i1 : MVT::i32;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000826 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000827}
828
Hal Finkel62ac7362014-09-19 11:42:56 +0000829bool PPCTargetLowering::enableAggressiveFMAFusion(EVT VT) const {
830 assert(VT.isFloatingPoint() && "Non-floating-point FMA?");
831 return true;
832}
833
Chris Lattner4211ca92006-04-14 06:01:58 +0000834//===----------------------------------------------------------------------===//
835// Node matching predicates, for use by the tblgen matching code.
836//===----------------------------------------------------------------------===//
837
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000838/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000839static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000840 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000841 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000842 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000843 // Maybe this has already been legalized into the constant pool?
844 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000845 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000846 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000847 }
848 return false;
849}
850
Chris Lattnere8b83b42006-04-06 17:23:16 +0000851/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
852/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000853static bool isConstantOrUndef(int Op, int Val) {
854 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000855}
856
857/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
858/// VPKUHUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000859/// The ShuffleKind distinguishes between big-endian operations with
860/// two different inputs (0), either-endian operations with two identical
861/// inputs (1), and little-endian operantion with two different inputs (2).
862/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
863bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +0000864 SelectionDAG &DAG) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000865 bool IsLE = DAG.getSubtarget().getDataLayout()->isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000866 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +0000867 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000868 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000869 for (unsigned i = 0; i != 16; ++i)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000870 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000871 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000872 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +0000873 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000874 return false;
875 for (unsigned i = 0; i != 16; ++i)
876 if (!isConstantOrUndef(N->getMaskElt(i), i*2))
877 return false;
878 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +0000879 unsigned j = IsLE ? 0 : 1;
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000880 for (unsigned i = 0; i != 8; ++i)
Bill Schmidtf910a062014-06-10 14:35:01 +0000881 if (!isConstantOrUndef(N->getMaskElt(i), i*2+j) ||
882 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000883 return false;
884 }
Chris Lattner1d338192006-04-06 18:26:28 +0000885 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000886}
887
888/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
889/// VPKUWUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000890/// The ShuffleKind distinguishes between big-endian operations with
891/// two different inputs (0), either-endian operations with two identical
892/// inputs (1), and little-endian operantion with two different inputs (2).
893/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
894bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +0000895 SelectionDAG &DAG) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000896 bool IsLE = DAG.getSubtarget().getDataLayout()->isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000897 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +0000898 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000899 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000900 for (unsigned i = 0; i != 16; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000901 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
902 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000903 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000904 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +0000905 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000906 return false;
907 for (unsigned i = 0; i != 16; i += 2)
908 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) ||
909 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1))
910 return false;
911 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +0000912 unsigned j = IsLE ? 0 : 2;
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000913 for (unsigned i = 0; i != 8; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000914 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) ||
915 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) ||
916 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) ||
917 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000918 return false;
919 }
Chris Lattner1d338192006-04-06 18:26:28 +0000920 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000921}
922
Chris Lattnerf38e0332006-04-06 22:02:42 +0000923/// isVMerge - Common function, used to match vmrg* shuffles.
924///
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000925static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +0000926 unsigned LHSStart, unsigned RHSStart) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000927 if (N->getValueType(0) != MVT::v16i8)
928 return false;
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000929 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
930 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000931
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000932 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
933 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000934 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000935 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000936 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000937 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000938 return false;
939 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000940 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +0000941}
942
943/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +0000944/// a VMRGL* instruction with the specified unit size (1,2 or 4 bytes).
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000945/// The ShuffleKind distinguishes between big-endian merges with two
946/// different inputs (0), either-endian merges with two identical inputs (1),
947/// and little-endian merges with two different inputs (2). For the latter,
948/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +0000949bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000950 unsigned ShuffleKind, SelectionDAG &DAG) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000951 if (DAG.getSubtarget().getDataLayout()->isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000952 if (ShuffleKind == 1) // unary
953 return isVMerge(N, UnitSize, 0, 0);
954 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +0000955 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000956 else
957 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +0000958 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000959 if (ShuffleKind == 1) // unary
960 return isVMerge(N, UnitSize, 8, 8);
961 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +0000962 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000963 else
964 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +0000965 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000966}
967
968/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +0000969/// a VMRGH* instruction with the specified unit size (1,2 or 4 bytes).
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000970/// The ShuffleKind distinguishes between big-endian merges with two
971/// different inputs (0), either-endian merges with two identical inputs (1),
972/// and little-endian merges with two different inputs (2). For the latter,
973/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +0000974bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000975 unsigned ShuffleKind, SelectionDAG &DAG) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000976 if (DAG.getSubtarget().getDataLayout()->isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000977 if (ShuffleKind == 1) // unary
978 return isVMerge(N, UnitSize, 8, 8);
979 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +0000980 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000981 else
982 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +0000983 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000984 if (ShuffleKind == 1) // unary
985 return isVMerge(N, UnitSize, 0, 0);
986 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +0000987 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000988 else
989 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +0000990 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000991}
992
993
Chris Lattner1d338192006-04-06 18:26:28 +0000994/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
995/// amount, otherwise return -1.
Bill Schmidt42a69362014-08-05 20:47:25 +0000996/// The ShuffleKind distinguishes between big-endian operations with two
997/// different inputs (0), either-endian operations with two identical inputs
998/// (1), and little-endian operations with two different inputs (2). For the
999/// latter, the input operands are swapped (see PPCInstrAltivec.td).
1000int PPC::isVSLDOIShuffleMask(SDNode *N, unsigned ShuffleKind,
1001 SelectionDAG &DAG) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001002 if (N->getValueType(0) != MVT::v16i8)
Hal Finkela775e512014-04-08 19:00:27 +00001003 return -1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001004
1005 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +00001006
Chris Lattner1d338192006-04-06 18:26:28 +00001007 // Find the first non-undef value in the shuffle mask.
1008 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001009 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +00001010 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001011
Chris Lattner1d338192006-04-06 18:26:28 +00001012 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001013
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001014 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +00001015 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001016 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +00001017 if (ShiftAmt < i) return -1;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001018
Bill Schmidtf04e9982014-08-04 23:21:01 +00001019 ShiftAmt -= i;
Bill Schmidt42a69362014-08-05 20:47:25 +00001020 bool isLE = DAG.getTarget().getSubtargetImpl()->getDataLayout()->
1021 isLittleEndian();
Bill Schmidtf910a062014-06-10 14:35:01 +00001022
Bill Schmidt42a69362014-08-05 20:47:25 +00001023 if ((ShuffleKind == 0 && !isLE) || (ShuffleKind == 2 && isLE)) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001024 // Check the rest of the elements to see if they are consecutive.
1025 for (++i; i != 16; ++i)
1026 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
1027 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001028 } else if (ShuffleKind == 1) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001029 // Check the rest of the elements to see if they are consecutive.
1030 for (++i; i != 16; ++i)
1031 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
1032 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001033 } else
1034 return -1;
1035
1036 if (ShuffleKind == 2 && isLE)
1037 ShiftAmt = 16 - ShiftAmt;
Bill Schmidtf04e9982014-08-04 23:21:01 +00001038
Chris Lattner1d338192006-04-06 18:26:28 +00001039 return ShiftAmt;
1040}
Chris Lattnerffc47562006-03-20 06:33:01 +00001041
1042/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
1043/// specifies a splat of a single element that is suitable for input to
1044/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001045bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +00001046 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +00001047 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001048
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001049 // This is a splat operation if each element of the permute is the same, and
1050 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001051 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001052
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001053 // FIXME: Handle UNDEF elements too!
1054 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +00001055 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001056
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001057 // Check that the indices are consecutive, in the case of a multi-byte element
1058 // splatted with a v16i8 mask.
1059 for (unsigned i = 1; i != EltSize; ++i)
1060 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001061 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001062
Chris Lattner95c7adc2006-04-04 17:25:31 +00001063 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001064 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +00001065 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001066 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001067 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001068 }
Chris Lattner95c7adc2006-04-04 17:25:31 +00001069 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +00001070}
1071
Evan Cheng581d2792007-07-30 07:51:22 +00001072/// isAllNegativeZeroVector - Returns true if all elements of build_vector
1073/// are -0.0.
1074bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001075 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
1076
1077 APInt APVal, APUndef;
1078 unsigned BitSize;
1079 bool HasAnyUndefs;
Wesley Peck527da1b2010-11-23 03:31:01 +00001080
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00001081 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001082 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001083 return CFP->getValueAPF().isNegZero();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001084
Evan Cheng581d2792007-07-30 07:51:22 +00001085 return false;
1086}
1087
Chris Lattnerffc47562006-03-20 06:33:01 +00001088/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
1089/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Bill Schmidtf910a062014-06-10 14:35:01 +00001090unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize,
1091 SelectionDAG &DAG) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001092 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1093 assert(isSplatShuffleMask(SVOp, EltSize));
Eric Christopherfc6de422014-08-05 02:39:49 +00001094 if (DAG.getSubtarget().getDataLayout()->isLittleEndian())
Bill Schmidtf910a062014-06-10 14:35:01 +00001095 return (16 / EltSize) - 1 - (SVOp->getMaskElt(0) / EltSize);
1096 else
1097 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +00001098}
1099
Chris Lattner74cf9ff2006-04-12 17:37:20 +00001100/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001101/// by using a vspltis[bhw] instruction of the specified element size, return
1102/// the constant being splatted. The ByteSize field indicates the number of
1103/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001104SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001105 SDValue OpVal(nullptr, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001106
1107 // If ByteSize of the splat is bigger than the element size of the
1108 // build_vector, then we have a case where we are checking for a splat where
1109 // multiple elements of the buildvector are folded together into a single
1110 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
1111 unsigned EltSize = 16/N->getNumOperands();
1112 if (EltSize < ByteSize) {
1113 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001114 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001115 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001116
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001117 // See if all of the elements in the buildvector agree across.
1118 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1119 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1120 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001121 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001122
Scott Michelcf0da6c2009-02-17 22:15:04 +00001123
Craig Topper062a2ba2014-04-25 05:30:21 +00001124 if (!UniquedVals[i&(Multiple-1)].getNode())
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001125 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
1126 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001127 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001128 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001129
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001130 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
1131 // either constant or undef values that are identical for each chunk. See
1132 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001133
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001134 // Check to see if all of the leading entries are either 0 or -1. If
1135 // neither, then this won't fit into the immediate field.
1136 bool LeadingZero = true;
1137 bool LeadingOnes = true;
1138 for (unsigned i = 0; i != Multiple-1; ++i) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001139 if (!UniquedVals[i].getNode()) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001140
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001141 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
1142 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
1143 }
1144 // Finally, check the least significant entry.
1145 if (LeadingZero) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001146 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001147 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00001148 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001149 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +00001150 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001151 }
1152 if (LeadingOnes) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001153 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001154 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +00001155 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001156 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +00001157 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001158 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001159
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001160 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001161 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001162
Chris Lattner2771e2c2006-03-25 06:12:06 +00001163 // Check to see if this buildvec has a single non-undef value in its elements.
1164 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1165 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Craig Topper062a2ba2014-04-25 05:30:21 +00001166 if (!OpVal.getNode())
Chris Lattner2771e2c2006-03-25 06:12:06 +00001167 OpVal = N->getOperand(i);
1168 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001169 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001170 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001171
Craig Topper062a2ba2014-04-25 05:30:21 +00001172 if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001173
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001174 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001175 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001176 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001177 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001178 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001179 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001180 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001181 }
1182
1183 // If the splat value is larger than the element value, then we can never do
1184 // this splat. The only case that we could fit the replicated bits into our
1185 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001186 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001187
Chris Lattner2771e2c2006-03-25 06:12:06 +00001188 // If the element value is larger than the splat value, cut it in half and
1189 // check to see if the two halves are equal. Continue doing this until we
1190 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
1191 while (ValSizeInBytes > ByteSize) {
1192 ValSizeInBytes >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001193
Chris Lattner2771e2c2006-03-25 06:12:06 +00001194 // If the top half equals the bottom half, we're still ok.
Chris Lattner39cc7172006-04-05 17:39:25 +00001195 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
1196 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001197 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001198 }
1199
1200 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001201 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001202
Evan Chengb1ddc982006-03-26 09:52:32 +00001203 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001204 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001205
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001206 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001207 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +00001208 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001209 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001210}
1211
Chris Lattner4211ca92006-04-14 06:01:58 +00001212//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001213// Addressing Mode Selection
1214//===----------------------------------------------------------------------===//
1215
1216/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1217/// or 64-bit immediate, and if the value can be accurately represented as a
1218/// sign extension from a 16-bit value. If so, this returns true and the
1219/// immediate.
1220static bool isIntS16Immediate(SDNode *N, short &Imm) {
Adam Nemet571eb5f2014-05-20 17:20:34 +00001221 if (!isa<ConstantSDNode>(N))
Chris Lattnera801fced2006-11-08 02:15:41 +00001222 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001223
Dan Gohmaneffb8942008-09-12 16:56:44 +00001224 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001225 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001226 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001227 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001228 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001229}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001230static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001231 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001232}
1233
1234
1235/// SelectAddressRegReg - Given the specified addressed, check to see if it
1236/// can be represented as an indexed [r+r] operation. Returns false if it
1237/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001238bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1239 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001240 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001241 short imm = 0;
1242 if (N.getOpcode() == ISD::ADD) {
1243 if (isIntS16Immediate(N.getOperand(1), imm))
1244 return false; // r+i
1245 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1246 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001247
Chris Lattnera801fced2006-11-08 02:15:41 +00001248 Base = N.getOperand(0);
1249 Index = N.getOperand(1);
1250 return true;
1251 } else if (N.getOpcode() == ISD::OR) {
1252 if (isIntS16Immediate(N.getOperand(1), imm))
1253 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001254
Chris Lattnera801fced2006-11-08 02:15:41 +00001255 // If this is an or of disjoint bitfields, we can codegen this as an add
1256 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1257 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001258 APInt LHSKnownZero, LHSKnownOne;
1259 APInt RHSKnownZero, RHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001260 DAG.computeKnownBits(N.getOperand(0),
1261 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001262
Dan Gohmanf19609a2008-02-27 01:23:58 +00001263 if (LHSKnownZero.getBoolValue()) {
Jay Foada0653a32014-05-14 21:14:37 +00001264 DAG.computeKnownBits(N.getOperand(1),
1265 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001266 // If all of the bits are known zero on the LHS or RHS, the add won't
1267 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001268 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001269 Base = N.getOperand(0);
1270 Index = N.getOperand(1);
1271 return true;
1272 }
1273 }
1274 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001275
Chris Lattnera801fced2006-11-08 02:15:41 +00001276 return false;
1277}
1278
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001279// If we happen to be doing an i64 load or store into a stack slot that has
1280// less than a 4-byte alignment, then the frame-index elimination may need to
1281// use an indexed load or store instruction (because the offset may not be a
1282// multiple of 4). The extra register needed to hold the offset comes from the
1283// register scavenger, and it is possible that the scavenger will need to use
1284// an emergency spill slot. As a result, we need to make sure that a spill slot
1285// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1286// stack slot.
1287static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1288 // FIXME: This does not handle the LWA case.
1289 if (VT != MVT::i64)
1290 return;
1291
Hal Finkel7ab3db52013-07-10 15:29:01 +00001292 // NOTE: We'll exclude negative FIs here, which come from argument
1293 // lowering, because there are no known test cases triggering this problem
1294 // using packed structures (or similar). We can remove this exclusion if
1295 // we find such a test case. The reason why this is so test-case driven is
1296 // because this entire 'fixup' is only to prevent crashes (from the
1297 // register scavenger) on not-really-valid inputs. For example, if we have:
1298 // %a = alloca i1
1299 // %b = bitcast i1* %a to i64*
1300 // store i64* a, i64 b
1301 // then the store should really be marked as 'align 1', but is not. If it
1302 // were marked as 'align 1' then the indexed form would have been
1303 // instruction-selected initially, and the problem this 'fixup' is preventing
1304 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001305 if (FrameIdx < 0)
1306 return;
1307
1308 MachineFunction &MF = DAG.getMachineFunction();
1309 MachineFrameInfo *MFI = MF.getFrameInfo();
1310
1311 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1312 if (Align >= 4)
1313 return;
1314
1315 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1316 FuncInfo->setHasNonRISpills();
1317}
1318
Chris Lattnera801fced2006-11-08 02:15:41 +00001319/// Returns true if the address N can be represented by a base register plus
1320/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001321/// represented as reg+reg. If Aligned is true, only accept displacements
1322/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001323bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001324 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001325 SelectionDAG &DAG,
1326 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001327 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001328 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001329 // If this can be more profitably realized as r+r, fail.
1330 if (SelectAddressRegReg(N, Disp, Base, DAG))
1331 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001332
Chris Lattnera801fced2006-11-08 02:15:41 +00001333 if (N.getOpcode() == ISD::ADD) {
1334 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001335 if (isIntS16Immediate(N.getOperand(1), imm) &&
1336 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001337 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001338 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1339 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001340 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001341 } else {
1342 Base = N.getOperand(0);
1343 }
1344 return true; // [r+i]
1345 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1346 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001347 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001348 && "Cannot handle constant offsets yet!");
1349 Disp = N.getOperand(1).getOperand(0); // The global address.
1350 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001351 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001352 Disp.getOpcode() == ISD::TargetConstantPool ||
1353 Disp.getOpcode() == ISD::TargetJumpTable);
1354 Base = N.getOperand(0);
1355 return true; // [&g+r]
1356 }
1357 } else if (N.getOpcode() == ISD::OR) {
1358 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001359 if (isIntS16Immediate(N.getOperand(1), imm) &&
1360 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001361 // If this is an or of disjoint bitfields, we can codegen this as an add
1362 // (for better address arithmetic) if the LHS and RHS of the OR are
1363 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001364 APInt LHSKnownZero, LHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001365 DAG.computeKnownBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001366
Dan Gohmanf19609a2008-02-27 01:23:58 +00001367 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001368 // If all of the bits are known zero on the LHS or RHS, the add won't
1369 // carry.
Ulrich Weigand55a96652014-07-20 22:26:40 +00001370 if (FrameIndexSDNode *FI =
1371 dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1372 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1373 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1374 } else {
1375 Base = N.getOperand(0);
1376 }
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001377 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001378 return true;
1379 }
1380 }
1381 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1382 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001383
Chris Lattnera801fced2006-11-08 02:15:41 +00001384 // If this address fits entirely in a 16-bit sext immediate field, codegen
1385 // this as "d, 0"
1386 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001387 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001388 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001389 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001390 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001391 return true;
1392 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001393
1394 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001395 if ((CN->getValueType(0) == MVT::i32 ||
1396 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1397 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001398 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001399
Chris Lattnera801fced2006-11-08 02:15:41 +00001400 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001401 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001402
Owen Anderson9f944592009-08-11 20:47:22 +00001403 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1404 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001405 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001406 return true;
1407 }
1408 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001409
Chris Lattnera801fced2006-11-08 02:15:41 +00001410 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001411 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001412 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001413 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1414 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001415 Base = N;
1416 return true; // [r+0]
1417}
1418
1419/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1420/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001421bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1422 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001423 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001424 // Check to see if we can easily represent this as an [r+r] address. This
1425 // will fail if it thinks that the address is more profitably represented as
1426 // reg+imm, e.g. where imm = 0.
1427 if (SelectAddressRegReg(N, Base, Index, DAG))
1428 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001429
Chris Lattnera801fced2006-11-08 02:15:41 +00001430 // If the operand is an addition, always emit this as [r+r], since this is
1431 // better (for code size, and execution, as the memop does the add for free)
1432 // than emitting an explicit add.
1433 if (N.getOpcode() == ISD::ADD) {
1434 Base = N.getOperand(0);
1435 Index = N.getOperand(1);
1436 return true;
1437 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001438
Chris Lattnera801fced2006-11-08 02:15:41 +00001439 // Otherwise, do it the hard way, using R0 as the base register.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001440 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001441 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001442 Index = N;
1443 return true;
1444}
1445
Chris Lattnera801fced2006-11-08 02:15:41 +00001446/// getPreIndexedAddressParts - returns true by value, base pointer and
1447/// offset pointer and addressing mode by reference if the node's address
1448/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001449bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1450 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001451 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001452 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001453 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001454
Ulrich Weigande90b0222013-03-22 14:58:48 +00001455 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001456 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001457 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001458 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001459 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1460 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001461 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001462 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001463 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001464 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001465 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001466 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001467 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001468 } else
1469 return false;
1470
Chris Lattner68371252006-11-14 01:38:31 +00001471 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands13237ac2008-06-06 12:08:01 +00001472 if (VT.isVector())
Chris Lattner68371252006-11-14 01:38:31 +00001473 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001474
Ulrich Weigande90b0222013-03-22 14:58:48 +00001475 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1476
1477 // Common code will reject creating a pre-inc form if the base pointer
1478 // is a frame index, or if N is a store and the base pointer is either
1479 // the same as or a predecessor of the value being stored. Check for
1480 // those situations here, and try with swapped Base/Offset instead.
1481 bool Swap = false;
1482
1483 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1484 Swap = true;
1485 else if (!isLoad) {
1486 SDValue Val = cast<StoreSDNode>(N)->getValue();
1487 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1488 Swap = true;
1489 }
1490
1491 if (Swap)
1492 std::swap(Base, Offset);
1493
Hal Finkelca542be2012-06-20 15:43:03 +00001494 AM = ISD::PRE_INC;
1495 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001496 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001497
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001498 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001499 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001500 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001501 return false;
1502 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001503 // LDU/STU need an address with at least 4-byte alignment.
1504 if (Alignment < 4)
1505 return false;
1506
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001507 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001508 return false;
1509 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001510
Chris Lattnerb314b152006-11-11 00:08:42 +00001511 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001512 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1513 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001514 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001515 LD->getExtensionType() == ISD::SEXTLOAD &&
1516 isa<ConstantSDNode>(Offset))
1517 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001518 }
1519
Chris Lattnerce645542006-11-10 02:08:47 +00001520 AM = ISD::PRE_INC;
1521 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001522}
1523
1524//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001525// LowerOperation implementation
1526//===----------------------------------------------------------------------===//
1527
Chris Lattneredb9d842010-11-15 02:46:57 +00001528/// GetLabelAccessInfo - Return true if we should reference labels using a
1529/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1530static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Craig Topper062a2ba2014-04-25 05:30:21 +00001531 unsigned &LoOpFlags,
1532 const GlobalValue *GV = nullptr) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001533 HiOpFlags = PPCII::MO_HA;
1534 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001535
Hal Finkel3ee2af72014-07-18 23:29:49 +00001536 // Don't use the pic base if not in PIC relocation model.
1537 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
1538
Chris Lattnerdd6df842010-11-15 03:13:19 +00001539 if (isPIC) {
1540 HiOpFlags |= PPCII::MO_PIC_FLAG;
1541 LoOpFlags |= PPCII::MO_PIC_FLAG;
1542 }
1543
1544 // If this is a reference to a global value that requires a non-lazy-ptr, make
1545 // sure that instruction lowering adds it.
1546 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1547 HiOpFlags |= PPCII::MO_NLP_FLAG;
1548 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001549
Chris Lattnerdd6df842010-11-15 03:13:19 +00001550 if (GV->hasHiddenVisibility()) {
1551 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1552 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1553 }
1554 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001555
Chris Lattneredb9d842010-11-15 02:46:57 +00001556 return isPIC;
1557}
1558
1559static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1560 SelectionDAG &DAG) {
1561 EVT PtrVT = HiPart.getValueType();
1562 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001563 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001564
1565 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1566 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001567
Chris Lattneredb9d842010-11-15 02:46:57 +00001568 // With PIC, the first instruction is actually "GR+hi(&G)".
1569 if (isPIC)
1570 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1571 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001572
Chris Lattneredb9d842010-11-15 02:46:57 +00001573 // Generate non-pic code that has direct accesses to the constant pool.
1574 // The address of the global is just (hi(&g)+lo(&g)).
1575 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1576}
1577
Scott Michelcf0da6c2009-02-17 22:15:04 +00001578SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001579 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001580 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001581 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001582 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001583
Roman Divackyace47072012-08-24 16:26:02 +00001584 // 64-bit SVR4 ABI code is always position-independent.
1585 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001586 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Roman Divackyace47072012-08-24 16:26:02 +00001587 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001588 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001589 DAG.getRegister(PPC::X2, MVT::i64));
1590 }
1591
Chris Lattneredb9d842010-11-15 02:46:57 +00001592 unsigned MOHiFlag, MOLoFlag;
1593 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001594
1595 if (isPIC && Subtarget.isSVR4ABI()) {
1596 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(),
1597 PPCII::MO_PIC_FLAG);
1598 SDLoc DL(CP);
1599 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i32, GA,
1600 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT));
1601 }
1602
Chris Lattneredb9d842010-11-15 02:46:57 +00001603 SDValue CPIHi =
1604 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1605 SDValue CPILo =
1606 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1607 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001608}
1609
Dan Gohman21cea8a2010-04-17 15:26:15 +00001610SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001611 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001612 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001613
Roman Divackyace47072012-08-24 16:26:02 +00001614 // 64-bit SVR4 ABI code is always position-independent.
1615 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001616 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Roman Divackyace47072012-08-24 16:26:02 +00001617 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001618 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001619 DAG.getRegister(PPC::X2, MVT::i64));
1620 }
1621
Chris Lattneredb9d842010-11-15 02:46:57 +00001622 unsigned MOHiFlag, MOLoFlag;
1623 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001624
1625 if (isPIC && Subtarget.isSVR4ABI()) {
1626 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1627 PPCII::MO_PIC_FLAG);
1628 SDLoc DL(GA);
1629 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), PtrVT, GA,
1630 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT));
1631 }
1632
Chris Lattneredb9d842010-11-15 02:46:57 +00001633 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1634 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1635 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001636}
1637
Dan Gohman21cea8a2010-04-17 15:26:15 +00001638SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1639 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001640 EVT PtrVT = Op.getValueType();
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001641 BlockAddressSDNode *BASDN = cast<BlockAddressSDNode>(Op);
1642 const BlockAddress *BA = BASDN->getBlockAddress();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001643
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001644 // 64-bit SVR4 ABI code is always position-independent.
1645 // The actual BlockAddress is stored in the TOC.
1646 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
1647 SDValue GA = DAG.getTargetBlockAddress(BA, PtrVT, BASDN->getOffset());
1648 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(BASDN), MVT::i64, GA,
1649 DAG.getRegister(PPC::X2, MVT::i64));
1650 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001651
Chris Lattneredb9d842010-11-15 02:46:57 +00001652 unsigned MOHiFlag, MOLoFlag;
1653 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001654 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1655 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001656 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1657}
1658
Roman Divackye3f15c982012-06-04 17:36:38 +00001659SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1660 SelectionDAG &DAG) const {
1661
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001662 // FIXME: TLS addresses currently use medium model code sequences,
1663 // which is the most useful form. Eventually support for small and
1664 // large models could be added if users need it, at the cost of
1665 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001666 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001667 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001668 const GlobalValue *GV = GA->getGlobal();
1669 EVT PtrVT = getPointerTy();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001670 bool is64bit = Subtarget.isPPC64();
Roman Divackye3f15c982012-06-04 17:36:38 +00001671
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001672 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001673
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001674 if (Model == TLSModel::LocalExec) {
1675 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001676 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001677 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001678 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001679 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1680 is64bit ? MVT::i64 : MVT::i32);
1681 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1682 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1683 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001684
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001685 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001686 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001687 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1688 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00001689 SDValue GOTPtr;
1690 if (is64bit) {
1691 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1692 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1693 PtrVT, GOTReg, TGA);
1694 } else
1695 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001696 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00001697 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001698 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001699 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001700
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001701 if (Model == TLSModel::GeneralDynamic) {
1702 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001703 SDValue GOTPtr;
1704 if (is64bit) {
1705 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1706 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1707 GOTReg, TGA);
1708 } else {
1709 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
1710 }
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001711 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
Hal Finkel7c8ae532014-07-25 17:47:22 +00001712 GOTPtr, TGA);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001713
1714 // We need a chain node, and don't have one handy. The underlying
1715 // call has no side effects, so using the function entry node
1716 // suffices.
1717 SDValue Chain = DAG.getEntryNode();
Hal Finkel7c8ae532014-07-25 17:47:22 +00001718 Chain = DAG.getCopyToReg(Chain, dl,
1719 is64bit ? PPC::X3 : PPC::R3, GOTEntry);
1720 SDValue ParmReg = DAG.getRegister(is64bit ? PPC::X3 : PPC::R3,
1721 is64bit ? MVT::i64 : MVT::i32);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001722 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1723 PtrVT, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001724 // The return value from GET_TLS_ADDR really is in X3 already, but
1725 // some hacks are needed here to tie everything together. The extra
1726 // copies dissolve during subsequent transforms.
Hal Finkel7c8ae532014-07-25 17:47:22 +00001727 Chain = DAG.getCopyToReg(Chain, dl, is64bit ? PPC::X3 : PPC::R3, TLSAddr);
1728 return DAG.getCopyFromReg(Chain, dl, is64bit ? PPC::X3 : PPC::R3, PtrVT);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001729 }
1730
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001731 if (Model == TLSModel::LocalDynamic) {
1732 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001733 SDValue GOTPtr;
1734 if (is64bit) {
1735 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1736 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1737 GOTReg, TGA);
1738 } else {
1739 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
1740 }
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001741 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
Hal Finkel7c8ae532014-07-25 17:47:22 +00001742 GOTPtr, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001743
1744 // We need a chain node, and don't have one handy. The underlying
1745 // call has no side effects, so using the function entry node
1746 // suffices.
1747 SDValue Chain = DAG.getEntryNode();
Hal Finkel7c8ae532014-07-25 17:47:22 +00001748 Chain = DAG.getCopyToReg(Chain, dl,
1749 is64bit ? PPC::X3 : PPC::R3, GOTEntry);
1750 SDValue ParmReg = DAG.getRegister(is64bit ? PPC::X3 : PPC::R3,
1751 is64bit ? MVT::i64 : MVT::i32);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001752 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1753 PtrVT, ParmReg, TGA);
1754 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1755 // some hacks are needed here to tie everything together. The extra
1756 // copies dissolve during subsequent transforms.
Hal Finkel7c8ae532014-07-25 17:47:22 +00001757 Chain = DAG.getCopyToReg(Chain, dl, is64bit ? PPC::X3 : PPC::R3, TLSAddr);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001758 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt9ed4dbc2012-12-13 20:57:10 +00001759 Chain, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001760 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1761 }
1762
1763 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00001764}
1765
Chris Lattneredb9d842010-11-15 02:46:57 +00001766SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1767 SelectionDAG &DAG) const {
1768 EVT PtrVT = Op.getValueType();
1769 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001770 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00001771 const GlobalValue *GV = GSDN->getGlobal();
1772
Chris Lattneredb9d842010-11-15 02:46:57 +00001773 // 64-bit SVR4 ABI code is always position-independent.
1774 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001775 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Chris Lattneredb9d842010-11-15 02:46:57 +00001776 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1777 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1778 DAG.getRegister(PPC::X2, MVT::i64));
1779 }
1780
Chris Lattnerdd6df842010-11-15 03:13:19 +00001781 unsigned MOHiFlag, MOLoFlag;
1782 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00001783
Hal Finkel3ee2af72014-07-18 23:29:49 +00001784 if (isPIC && Subtarget.isSVR4ABI()) {
1785 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT,
1786 GSDN->getOffset(),
1787 PPCII::MO_PIC_FLAG);
1788 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i32, GA,
1789 DAG.getNode(PPCISD::GlobalBaseReg, DL, MVT::i32));
1790 }
1791
Chris Lattnerdd6df842010-11-15 03:13:19 +00001792 SDValue GAHi =
1793 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1794 SDValue GALo =
1795 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00001796
Chris Lattnerdd6df842010-11-15 03:13:19 +00001797 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00001798
Chris Lattnerdd6df842010-11-15 03:13:19 +00001799 // If the global reference is actually to a non-lazy-pointer, we have to do an
1800 // extra load to get the address of the global.
1801 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1802 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001803 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00001804 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00001805}
1806
Dan Gohman21cea8a2010-04-17 15:26:15 +00001807SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00001808 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001809 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001810
Hal Finkel777c9dd2014-03-29 16:04:40 +00001811 if (Op.getValueType() == MVT::v2i64) {
1812 // When the operands themselves are v2i64 values, we need to do something
1813 // special because VSX has no underlying comparison operations for these.
1814 if (Op.getOperand(0).getValueType() == MVT::v2i64) {
1815 // Equality can be handled by casting to the legal type for Altivec
1816 // comparisons, everything else needs to be expanded.
1817 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
1818 return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
1819 DAG.getSetCC(dl, MVT::v4i32,
1820 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)),
1821 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)),
1822 CC));
1823 }
1824
1825 return SDValue();
1826 }
1827
1828 // We handle most of these in the usual way.
1829 return Op;
1830 }
1831
Chris Lattner4211ca92006-04-14 06:01:58 +00001832 // If we're comparing for equality to zero, expose the fact that this is
1833 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1834 // fold the new nodes.
1835 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1836 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001837 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001838 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001839 if (VT.bitsLT(MVT::i32)) {
1840 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001841 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001842 }
Duncan Sands13237ac2008-06-06 12:08:01 +00001843 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001844 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1845 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00001846 DAG.getConstant(Log2b, MVT::i32));
1847 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00001848 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001849 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00001850 // optimized. FIXME: revisit this when we can custom lower all setcc
1851 // optimizations.
1852 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001853 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001854 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001855
Chris Lattner4211ca92006-04-14 06:01:58 +00001856 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00001857 // by xor'ing the rhs with the lhs, which is faster than setting a
1858 // condition register, reading it back out, and masking the correct bit. The
1859 // normal approach here uses sub to do this instead of xor. Using xor exposes
1860 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001861 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00001862 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001863 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001864 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00001865 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001866 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00001867 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001868 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001869}
1870
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001871SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001872 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00001873 SDNode *Node = Op.getNode();
1874 EVT VT = Node->getValueType(0);
1875 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1876 SDValue InChain = Node->getOperand(0);
1877 SDValue VAListPtr = Node->getOperand(1);
1878 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001879 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001880
Roman Divacky4394e682011-06-28 15:30:42 +00001881 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1882
1883 // gpr_index
1884 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1885 VAListPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001886 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001887 InChain = GprIndex.getValue(1);
1888
1889 if (VT == MVT::i64) {
1890 // Check if GprIndex is even
1891 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1892 DAG.getConstant(1, MVT::i32));
1893 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1894 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1895 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1896 DAG.getConstant(1, MVT::i32));
1897 // Align GprIndex to be even if it isn't
1898 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1899 GprIndex);
1900 }
1901
1902 // fpr index is 1 byte after gpr
1903 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1904 DAG.getConstant(1, MVT::i32));
1905
1906 // fpr
1907 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1908 FprPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001909 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001910 InChain = FprIndex.getValue(1);
1911
1912 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1913 DAG.getConstant(8, MVT::i32));
1914
1915 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1916 DAG.getConstant(4, MVT::i32));
1917
1918 // areas
1919 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001920 MachinePointerInfo(), false, false,
1921 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001922 InChain = OverflowArea.getValue(1);
1923
1924 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001925 MachinePointerInfo(), false, false,
1926 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001927 InChain = RegSaveArea.getValue(1);
1928
1929 // select overflow_area if index > 8
1930 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1931 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1932
Roman Divacky4394e682011-06-28 15:30:42 +00001933 // adjustment constant gpr_index * 4/8
1934 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1935 VT.isInteger() ? GprIndex : FprIndex,
1936 DAG.getConstant(VT.isInteger() ? 4 : 8,
1937 MVT::i32));
1938
1939 // OurReg = RegSaveArea + RegConstant
1940 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1941 RegConstant);
1942
1943 // Floating types are 32 bytes into RegSaveArea
1944 if (VT.isFloatingPoint())
1945 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1946 DAG.getConstant(32, MVT::i32));
1947
1948 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1949 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1950 VT.isInteger() ? GprIndex : FprIndex,
1951 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1952 MVT::i32));
1953
1954 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1955 VT.isInteger() ? VAListPtr : FprPtr,
1956 MachinePointerInfo(SV),
1957 MVT::i8, false, false, 0);
1958
1959 // determine if we should load from reg_save_area or overflow_area
1960 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1961
1962 // increase overflow_area by 4/8 if gpr/fpr > 8
1963 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1964 DAG.getConstant(VT.isInteger() ? 4 : 8,
1965 MVT::i32));
1966
1967 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1968 OverflowAreaPlusN);
1969
1970 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1971 OverflowAreaPtr,
1972 MachinePointerInfo(),
1973 MVT::i32, false, false, 0);
1974
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00001975 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001976 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001977}
1978
Roman Divackyc3825df2013-07-25 21:36:47 +00001979SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
1980 const PPCSubtarget &Subtarget) const {
1981 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
1982
1983 // We have to copy the entire va_list struct:
1984 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
1985 return DAG.getMemcpy(Op.getOperand(0), Op,
1986 Op.getOperand(1), Op.getOperand(2),
1987 DAG.getConstant(12, MVT::i32), 8, false, true,
1988 MachinePointerInfo(), MachinePointerInfo());
1989}
1990
Duncan Sandsa0984362011-09-06 13:37:06 +00001991SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1992 SelectionDAG &DAG) const {
1993 return Op.getOperand(0);
1994}
1995
1996SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1997 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00001998 SDValue Chain = Op.getOperand(0);
1999 SDValue Trmp = Op.getOperand(1); // trampoline
2000 SDValue FPtr = Op.getOperand(2); // nested function
2001 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00002002 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00002003
Owen Anderson53aa7a92009-08-10 22:56:29 +00002004 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002005 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00002006 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00002007 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00002008 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00002009
Scott Michelcf0da6c2009-02-17 22:15:04 +00002010 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00002011 TargetLowering::ArgListEntry Entry;
2012
2013 Entry.Ty = IntPtrTy;
2014 Entry.Node = Trmp; Args.push_back(Entry);
2015
2016 // TrampSize == (isPPC64 ? 48 : 40);
2017 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00002018 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00002019 Args.push_back(Entry);
2020
2021 Entry.Node = FPtr; Args.push_back(Entry);
2022 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002023
Bill Wendling95e1af22008-09-17 00:30:57 +00002024 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002025 TargetLowering::CallLoweringInfo CLI(DAG);
2026 CLI.setDebugLoc(dl).setChain(Chain)
2027 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002028 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
2029 std::move(Args), 0);
Bill Wendling95e1af22008-09-17 00:30:57 +00002030
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002031 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Duncan Sandsa0984362011-09-06 13:37:06 +00002032 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00002033}
2034
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002035SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002036 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00002037 MachineFunction &MF = DAG.getMachineFunction();
2038 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2039
Andrew Trickef9de2a2013-05-25 02:42:55 +00002040 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002041
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002042 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002043 // vastart just stores the address of the VarArgsFrameIndex slot into the
2044 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002045 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002046 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002047 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00002048 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2049 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00002050 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002051 }
2052
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002053 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002054 // We suppose the given va_list is already allocated.
2055 //
2056 // typedef struct {
2057 // char gpr; /* index into the array of 8 GPRs
2058 // * stored in the register save area
2059 // * gpr=0 corresponds to r3,
2060 // * gpr=1 to r4, etc.
2061 // */
2062 // char fpr; /* index into the array of 8 FPRs
2063 // * stored in the register save area
2064 // * fpr=0 corresponds to f1,
2065 // * fpr=1 to f2, etc.
2066 // */
2067 // char *overflow_arg_area;
2068 // /* location on stack that holds
2069 // * the next overflow argument
2070 // */
2071 // char *reg_save_area;
2072 // /* where r3:r10 and f1:f8 (if saved)
2073 // * are stored
2074 // */
2075 // } va_list[1];
2076
2077
Dan Gohman31ae5862010-04-17 14:41:14 +00002078 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
2079 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002080
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002081
Owen Anderson53aa7a92009-08-10 22:56:29 +00002082 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002083
Dan Gohman31ae5862010-04-17 14:41:14 +00002084 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
2085 PtrVT);
2086 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2087 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002088
Duncan Sands13237ac2008-06-06 12:08:01 +00002089 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002090 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002091
Duncan Sands13237ac2008-06-06 12:08:01 +00002092 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002093 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002094
2095 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002096 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002097
Dan Gohman2d489b52008-02-06 22:27:42 +00002098 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002099
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002100 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002101 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00002102 Op.getOperand(1),
2103 MachinePointerInfo(SV),
2104 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002105 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002106 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002107 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002108
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002109 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002110 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00002111 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
2112 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00002113 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002114 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002115 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002116
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002117 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002118 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00002119 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
2120 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002121 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002122 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002123 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002124
2125 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00002126 return DAG.getStore(thirdStore, dl, FR, nextPtr,
2127 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002128 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002129
Chris Lattner4211ca92006-04-14 06:01:58 +00002130}
2131
Chris Lattner4f2e4e02007-03-06 00:59:59 +00002132#include "PPCGenCallingConv.inc"
2133
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002134// Function whose sole purpose is to kill compiler warnings
2135// stemming from unused functions included from PPCGenCallingConv.inc.
2136CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00002137 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002138}
2139
Bill Schmidt230b4512013-06-12 16:39:22 +00002140bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
2141 CCValAssign::LocInfo &LocInfo,
2142 ISD::ArgFlagsTy &ArgFlags,
2143 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002144 return true;
2145}
2146
Bill Schmidt230b4512013-06-12 16:39:22 +00002147bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
2148 MVT &LocVT,
2149 CCValAssign::LocInfo &LocInfo,
2150 ISD::ArgFlagsTy &ArgFlags,
2151 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002152 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002153 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2154 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2155 };
2156 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002157
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002158 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
2159
2160 // Skip one register if the first unallocated register has an even register
2161 // number and there are still argument registers available which have not been
2162 // allocated yet. RegNum is actually an index into ArgRegs, which means we
2163 // need to skip a register if RegNum is odd.
2164 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
2165 State.AllocateReg(ArgRegs[RegNum]);
2166 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002167
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002168 // Always return false here, as this function only makes sure that the first
2169 // unallocated register has an odd register number and does not actually
2170 // allocate a register for the current argument.
2171 return false;
2172}
2173
Bill Schmidt230b4512013-06-12 16:39:22 +00002174bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
2175 MVT &LocVT,
2176 CCValAssign::LocInfo &LocInfo,
2177 ISD::ArgFlagsTy &ArgFlags,
2178 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002179 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002180 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2181 PPC::F8
2182 };
2183
2184 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002185
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002186 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
2187
2188 // If there is only one Floating-point register left we need to put both f64
2189 // values of a split ppc_fp128 value on the stack.
2190 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
2191 State.AllocateReg(ArgRegs[RegNum]);
2192 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002193
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002194 // Always return false here, as this function only makes sure that the two f64
2195 // values a ppc_fp128 value is split into are both passed in registers or both
2196 // passed on the stack and does not actually allocate a register for the
2197 // current argument.
2198 return false;
2199}
2200
Chris Lattner43df5b32007-02-25 05:34:32 +00002201/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002202/// on Darwin.
Craig Topper840beec2014-04-04 05:16:06 +00002203static const MCPhysReg *GetFPR() {
2204 static const MCPhysReg FPR[] = {
Chris Lattner43df5b32007-02-25 05:34:32 +00002205 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002206 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner43df5b32007-02-25 05:34:32 +00002207 };
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002208
Chris Lattner43df5b32007-02-25 05:34:32 +00002209 return FPR;
2210}
2211
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002212/// CalculateStackSlotSize - Calculates the size reserved for this argument on
2213/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002214static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002215 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00002216 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002217 if (Flags.isByVal())
2218 ArgSize = Flags.getByValSize();
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002219
2220 // Round up to multiples of the pointer size, except for array members,
2221 // which are always packed.
2222 if (!Flags.isInConsecutiveRegs())
2223 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002224
2225 return ArgSize;
2226}
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002227
2228/// CalculateStackSlotAlignment - Calculates the alignment of this argument
2229/// on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002230static unsigned CalculateStackSlotAlignment(EVT ArgVT, EVT OrigVT,
2231 ISD::ArgFlagsTy Flags,
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002232 unsigned PtrByteSize) {
2233 unsigned Align = PtrByteSize;
2234
2235 // Altivec parameters are padded to a 16 byte boundary.
2236 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2237 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
2238 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64)
2239 Align = 16;
2240
2241 // ByVal parameters are aligned as requested.
2242 if (Flags.isByVal()) {
2243 unsigned BVAlign = Flags.getByValAlign();
2244 if (BVAlign > PtrByteSize) {
2245 if (BVAlign % PtrByteSize != 0)
2246 llvm_unreachable(
2247 "ByVal alignment is not a multiple of the pointer size");
2248
2249 Align = BVAlign;
2250 }
2251 }
2252
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002253 // Array members are always packed to their original alignment.
2254 if (Flags.isInConsecutiveRegs()) {
2255 // If the array member was split into multiple registers, the first
2256 // needs to be aligned to the size of the full type. (Except for
2257 // ppcf128, which is only aligned as its f64 components.)
2258 if (Flags.isSplit() && OrigVT != MVT::ppcf128)
2259 Align = OrigVT.getStoreSize();
2260 else
2261 Align = ArgVT.getStoreSize();
2262 }
2263
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002264 return Align;
2265}
2266
Ulrich Weigand8658f172014-07-20 23:43:15 +00002267/// CalculateStackSlotUsed - Return whether this argument will use its
2268/// stack slot (instead of being passed in registers). ArgOffset,
2269/// AvailableFPRs, and AvailableVRs must hold the current argument
2270/// position, and will be updated to account for this argument.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002271static bool CalculateStackSlotUsed(EVT ArgVT, EVT OrigVT,
2272 ISD::ArgFlagsTy Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00002273 unsigned PtrByteSize,
2274 unsigned LinkageSize,
2275 unsigned ParamAreaSize,
2276 unsigned &ArgOffset,
2277 unsigned &AvailableFPRs,
2278 unsigned &AvailableVRs) {
2279 bool UseMemory = false;
2280
2281 // Respect alignment of argument on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002282 unsigned Align =
2283 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigand8658f172014-07-20 23:43:15 +00002284 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
2285 // If there's no space left in the argument save area, we must
2286 // use memory (this check also catches zero-sized arguments).
2287 if (ArgOffset >= LinkageSize + ParamAreaSize)
2288 UseMemory = true;
2289
2290 // Allocate argument on the stack.
2291 ArgOffset += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002292 if (Flags.isInConsecutiveRegsLast())
2293 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand8658f172014-07-20 23:43:15 +00002294 // If we overran the argument save area, we must use memory
2295 // (this check catches arguments passed partially in memory)
2296 if (ArgOffset > LinkageSize + ParamAreaSize)
2297 UseMemory = true;
2298
2299 // However, if the argument is actually passed in an FPR or a VR,
2300 // we don't use memory after all.
2301 if (!Flags.isByVal()) {
2302 if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
2303 if (AvailableFPRs > 0) {
2304 --AvailableFPRs;
2305 return false;
2306 }
2307 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2308 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
2309 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64)
2310 if (AvailableVRs > 0) {
2311 --AvailableVRs;
2312 return false;
2313 }
2314 }
2315
2316 return UseMemory;
2317}
2318
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002319/// EnsureStackAlignment - Round stack frame size up from NumBytes to
2320/// ensure minimum alignment required for target.
2321static unsigned EnsureStackAlignment(const TargetMachine &Target,
2322 unsigned NumBytes) {
Eric Christopherd9134482014-08-04 21:25:23 +00002323 unsigned TargetAlign =
2324 Target.getSubtargetImpl()->getFrameLowering()->getStackAlignment();
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002325 unsigned AlignMask = TargetAlign - 1;
2326 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2327 return NumBytes;
2328}
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002329
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002330SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002331PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002332 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002333 const SmallVectorImpl<ISD::InputArg>
2334 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002335 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002336 SmallVectorImpl<SDValue> &InVals)
2337 const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002338 if (Subtarget.isSVR4ABI()) {
2339 if (Subtarget.isPPC64())
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002340 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
2341 dl, DAG, InVals);
2342 else
2343 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2344 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002345 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002346 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2347 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002348 }
2349}
2350
2351SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002352PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002353 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002354 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002355 const SmallVectorImpl<ISD::InputArg>
2356 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002357 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002358 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002359
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002360 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002361 // +-----------------------------------+
2362 // +--> | Back chain |
2363 // | +-----------------------------------+
2364 // | | Floating-point register save area |
2365 // | +-----------------------------------+
2366 // | | General register save area |
2367 // | +-----------------------------------+
2368 // | | CR save word |
2369 // | +-----------------------------------+
2370 // | | VRSAVE save word |
2371 // | +-----------------------------------+
2372 // | | Alignment padding |
2373 // | +-----------------------------------+
2374 // | | Vector register save area |
2375 // | +-----------------------------------+
2376 // | | Local variable space |
2377 // | +-----------------------------------+
2378 // | | Parameter list area |
2379 // | +-----------------------------------+
2380 // | | LR save word |
2381 // | +-----------------------------------+
2382 // SP--> +--- | Back chain |
2383 // +-----------------------------------+
2384 //
2385 // Specifications:
2386 // System V Application Binary Interface PowerPC Processor Supplement
2387 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002388
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002389 MachineFunction &MF = DAG.getMachineFunction();
2390 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002391 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002392
Owen Anderson53aa7a92009-08-10 22:56:29 +00002393 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002394 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002395 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2396 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002397 unsigned PtrByteSize = 4;
2398
2399 // Assign locations to all of the incoming arguments.
2400 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002401 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
2402 *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002403
2404 // Reserve space for the linkage area on the stack.
Ulrich Weigand8658f172014-07-20 23:43:15 +00002405 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(false, false, false);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002406 CCInfo.AllocateStack(LinkageSize, PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002407
Bill Schmidtef17c142013-02-06 17:33:58 +00002408 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002409
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002410 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2411 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002412
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002413 // Arguments stored in registers.
2414 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002415 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002416 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002417
Owen Anderson9f944592009-08-11 20:47:22 +00002418 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002419 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002420 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002421 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002422 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002423 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002424 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002425 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00002426 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002427 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002428 case MVT::f64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002429 if (Subtarget.hasVSX())
Hal Finkel19be5062014-03-29 05:29:01 +00002430 RC = &PPC::VSFRCRegClass;
2431 else
2432 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002433 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002434 case MVT::v16i8:
2435 case MVT::v8i16:
2436 case MVT::v4i32:
2437 case MVT::v4f32:
Hal Finkel7811c612014-03-28 19:58:11 +00002438 RC = &PPC::VRRCRegClass;
2439 break;
Hal Finkel27774d92014-03-13 07:58:58 +00002440 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002441 case MVT::v2i64:
Hal Finkel7811c612014-03-28 19:58:11 +00002442 RC = &PPC::VSHRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002443 break;
2444 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002445
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002446 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002447 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002448 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2449 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2450
2451 if (ValVT == MVT::i1)
2452 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002453
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002454 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002455 } else {
2456 // Argument stored in memory.
2457 assert(VA.isMemLoc());
2458
Hal Finkel940ab932014-02-28 00:27:01 +00002459 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002460 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002461 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002462
2463 // Create load nodes to retrieve arguments from the stack.
2464 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002465 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2466 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002467 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002468 }
2469 }
2470
2471 // Assign locations to all of the incoming aggregate by value arguments.
2472 // Aggregates passed by value are stored in the local variable space of the
2473 // caller's stack frame, right above the parameter list area.
2474 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002475 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Eric Christopherb5217502014-08-06 18:45:26 +00002476 ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002477
2478 // Reserve stack space for the allocations in CCInfo.
2479 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2480
Bill Schmidtef17c142013-02-06 17:33:58 +00002481 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002482
2483 // Area that is at least reserved in the caller of this function.
2484 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002485 MinReservedArea = std::max(MinReservedArea, LinkageSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00002486
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002487 // Set the size that is at least reserved in caller of this function. Tail
2488 // call optimized function's reserved stack space needs to be aligned so that
2489 // taking the difference between two stack areas will result in an aligned
2490 // stack.
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002491 MinReservedArea = EnsureStackAlignment(MF.getTarget(), MinReservedArea);
2492 FuncInfo->setMinReservedArea(MinReservedArea);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002493
2494 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002495
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002496 // If the function takes variable number of arguments, make a frame index for
2497 // the start of the first vararg value... for expansion of llvm.va_start.
2498 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +00002499 static const MCPhysReg GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002500 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2501 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2502 };
2503 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2504
Craig Topper840beec2014-04-04 05:16:06 +00002505 static const MCPhysReg FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002506 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2507 PPC::F8
2508 };
Joerg Sonnenbergereb8655a2014-08-08 16:46:10 +00002509 unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2510 if (DisablePPCFloatInVariadic)
2511 NumFPArgRegs = 0;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002512
Dan Gohman31ae5862010-04-17 14:41:14 +00002513 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2514 NumGPArgRegs));
2515 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2516 NumFPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002517
2518 // Make room for NumGPArgRegs and NumFPArgRegs.
2519 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Craig Topper7ff15922014-09-10 04:51:36 +00002520 NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002521
Dan Gohman31ae5862010-04-17 14:41:14 +00002522 FuncInfo->setVarArgsStackOffset(
2523 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002524 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002525
Dan Gohman31ae5862010-04-17 14:41:14 +00002526 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2527 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002528
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002529 // The fixed integer arguments of a variadic function are stored to the
2530 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2531 // the result of va_next.
2532 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2533 // Get an existing live-in vreg, or add a new one.
2534 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2535 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002536 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002537
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002538 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002539 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2540 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002541 MemOps.push_back(Store);
2542 // Increment the address by four for the next argument to store
2543 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2544 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2545 }
2546
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002547 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2548 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002549 // The double arguments are stored to the VarArgsFrameIndex
2550 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002551 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2552 // Get an existing live-in vreg, or add a new one.
2553 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2554 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002555 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002556
Owen Anderson9f944592009-08-11 20:47:22 +00002557 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002558 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2559 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002560 MemOps.push_back(Store);
2561 // Increment the address by eight for the next argument to store
Craig Topper7ff15922014-09-10 04:51:36 +00002562 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002563 PtrVT);
2564 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2565 }
2566 }
2567
2568 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002569 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002570
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002571 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002572}
2573
Bill Schmidt57d6de52012-10-23 15:51:16 +00002574// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2575// value to MVT::i64 and then truncate to the correct register size.
2576SDValue
2577PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2578 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002579 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002580 if (Flags.isSExt())
2581 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2582 DAG.getValueType(ObjectVT));
2583 else if (Flags.isZExt())
2584 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2585 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002586
Hal Finkel940ab932014-02-28 00:27:01 +00002587 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002588}
2589
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002590SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002591PPCTargetLowering::LowerFormalArguments_64SVR4(
2592 SDValue Chain,
2593 CallingConv::ID CallConv, bool isVarArg,
2594 const SmallVectorImpl<ISD::InputArg>
2595 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002596 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002597 SmallVectorImpl<SDValue> &InVals) const {
2598 // TODO: add description of PPC stack frame format, or at least some docs.
2599 //
Ulrich Weigand8658f172014-07-20 23:43:15 +00002600 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00002601 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002602 MachineFunction &MF = DAG.getMachineFunction();
2603 MachineFrameInfo *MFI = MF.getFrameInfo();
2604 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2605
2606 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2607 // Potential tail calls could cause overwriting of argument stack slots.
2608 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2609 (CallConv == CallingConv::Fast));
2610 unsigned PtrByteSize = 8;
2611
Ulrich Weigand8658f172014-07-20 23:43:15 +00002612 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(true, false,
2613 isELFv2ABI);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002614
Craig Topper840beec2014-04-04 05:16:06 +00002615 static const MCPhysReg GPR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002616 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2617 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2618 };
2619
Craig Topper840beec2014-04-04 05:16:06 +00002620 static const MCPhysReg *FPR = GetFPR();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002621
Craig Topper840beec2014-04-04 05:16:06 +00002622 static const MCPhysReg VR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002623 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2624 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2625 };
Craig Topper840beec2014-04-04 05:16:06 +00002626 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00002627 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
2628 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
2629 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002630
2631 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2632 const unsigned Num_FPR_Regs = 13;
2633 const unsigned Num_VR_Regs = array_lengthof(VR);
2634
Ulrich Weigand8658f172014-07-20 23:43:15 +00002635 // Do a first pass over the arguments to determine whether the ABI
2636 // guarantees that our caller has allocated the parameter save area
2637 // on its stack frame. In the ELFv1 ABI, this is always the case;
2638 // in the ELFv2 ABI, it is true if this is a vararg function or if
2639 // any parameter is located in a stack slot.
2640
2641 bool HasParameterArea = !isELFv2ABI || isVarArg;
2642 unsigned ParamAreaSize = Num_GPR_Regs * PtrByteSize;
2643 unsigned NumBytes = LinkageSize;
2644 unsigned AvailableFPRs = Num_FPR_Regs;
2645 unsigned AvailableVRs = Num_VR_Regs;
2646 for (unsigned i = 0, e = Ins.size(); i != e; ++i)
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002647 if (CalculateStackSlotUsed(Ins[i].VT, Ins[i].ArgVT, Ins[i].Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00002648 PtrByteSize, LinkageSize, ParamAreaSize,
2649 NumBytes, AvailableFPRs, AvailableVRs))
2650 HasParameterArea = true;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002651
2652 // Add DAG nodes to load the arguments or copy them out of registers. On
2653 // entry to a function on PPC, the arguments start after the linkage area,
2654 // although the first ones are often in registers.
2655
Ulrich Weigand8658f172014-07-20 23:43:15 +00002656 unsigned ArgOffset = LinkageSize;
2657 unsigned GPR_idx, FPR_idx = 0, VR_idx = 0;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002658 SmallVector<SDValue, 8> MemOps;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002659 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002660 unsigned CurArgIdx = 0;
2661 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002662 SDValue ArgVal;
2663 bool needsLoad = false;
2664 EVT ObjectVT = Ins[ArgNo].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002665 EVT OrigVT = Ins[ArgNo].ArgVT;
Hal Finkel940ab932014-02-28 00:27:01 +00002666 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002667 unsigned ArgSize = ObjSize;
2668 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt6631e942013-02-20 17:31:41 +00002669 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2670 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002671
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002672 /* Respect alignment of argument on the stack. */
2673 unsigned Align =
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002674 CalculateStackSlotAlignment(ObjectVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002675 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002676 unsigned CurArgOffset = ArgOffset;
2677
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002678 /* Compute GPR index associated with argument offset. */
2679 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
2680 GPR_idx = std::min(GPR_idx, Num_GPR_Regs);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002681
2682 // FIXME the codegen can be much improved in some cases.
2683 // We do not have to keep everything in memory.
2684 if (Flags.isByVal()) {
2685 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2686 ObjSize = Flags.getByValSize();
2687 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002688 // Empty aggregate parameters do not take up registers. Examples:
2689 // struct { } a;
2690 // union { } b;
2691 // int c[0];
2692 // etc. However, we have to provide a place-holder in InVals, so
2693 // pretend we have an 8-byte item at the current address for that
2694 // purpose.
2695 if (!ObjSize) {
2696 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2697 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2698 InVals.push_back(FIN);
2699 continue;
2700 }
Hal Finkel262a2242013-09-12 23:20:06 +00002701
Ulrich Weigand24195972014-07-20 22:36:52 +00002702 // Create a stack object covering all stack doublewords occupied
Ulrich Weigand8658f172014-07-20 23:43:15 +00002703 // by the argument. If the argument is (fully or partially) on
2704 // the stack, or if the argument is fully in registers but the
2705 // caller has allocated the parameter save anyway, we can refer
2706 // directly to the caller's stack frame. Otherwise, create a
2707 // local copy in our own frame.
2708 int FI;
2709 if (HasParameterArea ||
2710 ArgSize + ArgOffset > LinkageSize + Num_GPR_Regs * PtrByteSize)
Hal Finkel41a55ad2014-08-16 00:17:05 +00002711 FI = MFI->CreateFixedObject(ArgSize, ArgOffset, false, true);
Ulrich Weigand8658f172014-07-20 23:43:15 +00002712 else
2713 FI = MFI->CreateStackObject(ArgSize, Align, false);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002714 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002715
Ulrich Weigand24195972014-07-20 22:36:52 +00002716 // Handle aggregates smaller than 8 bytes.
2717 if (ObjSize < PtrByteSize) {
2718 // The value of the object is its address, which differs from the
2719 // address of the enclosing doubleword on big-endian systems.
2720 SDValue Arg = FIN;
2721 if (!isLittleEndian) {
2722 SDValue ArgOff = DAG.getConstant(PtrByteSize - ObjSize, PtrVT);
2723 Arg = DAG.getNode(ISD::ADD, dl, ArgOff.getValueType(), Arg, ArgOff);
2724 }
2725 InVals.push_back(Arg);
2726
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002727 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002728 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002729 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002730 SDValue Store;
2731
2732 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2733 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2734 (ObjSize == 2 ? MVT::i16 : MVT::i32));
Ulrich Weigand24195972014-07-20 22:36:52 +00002735 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, Arg,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002736 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002737 ObjType, false, false, 0);
2738 } else {
2739 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2740 // store the whole register as-is to the parameter save area
Ulrich Weigand24195972014-07-20 22:36:52 +00002741 // slot.
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002742 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002743 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002744 false, false, 0);
2745 }
2746
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002747 MemOps.push_back(Store);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002748 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002749 // Whether we copied from a register or not, advance the offset
2750 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002751 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002752 continue;
2753 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002754
Ulrich Weigand24195972014-07-20 22:36:52 +00002755 // The value of the object is its address, which is the address of
2756 // its first stack doubleword.
2757 InVals.push_back(FIN);
2758
2759 // Store whatever pieces of the object are in registers to memory.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002760 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
Ulrich Weigand24195972014-07-20 22:36:52 +00002761 if (GPR_idx == Num_GPR_Regs)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002762 break;
Ulrich Weigand24195972014-07-20 22:36:52 +00002763
2764 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2765 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2766 SDValue Addr = FIN;
2767 if (j) {
2768 SDValue Off = DAG.getConstant(j, PtrVT);
2769 Addr = DAG.getNode(ISD::ADD, dl, Off.getValueType(), Addr, Off);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002770 }
Ulrich Weigand24195972014-07-20 22:36:52 +00002771 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, Addr,
2772 MachinePointerInfo(FuncArg, j),
2773 false, false, 0);
2774 MemOps.push_back(Store);
2775 ++GPR_idx;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002776 }
Ulrich Weigand24195972014-07-20 22:36:52 +00002777 ArgOffset += ArgSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002778 continue;
2779 }
2780
2781 switch (ObjectVT.getSimpleVT().SimpleTy) {
2782 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00002783 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002784 case MVT::i32:
2785 case MVT::i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002786 // These can be scalar arguments or elements of an integer array type
2787 // passed directly. Clang may use those instead of "byval" aggregate
2788 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002789 if (GPR_idx != Num_GPR_Regs) {
2790 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2791 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2792
Hal Finkel940ab932014-02-28 00:27:01 +00002793 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002794 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2795 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002796 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002797 } else {
2798 needsLoad = true;
2799 ArgSize = PtrByteSize;
2800 }
2801 ArgOffset += 8;
2802 break;
2803
2804 case MVT::f32:
2805 case MVT::f64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002806 // These can be scalar arguments or elements of a float array type
2807 // passed directly. The latter are used to implement ELFv2 homogenous
2808 // float aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002809 if (FPR_idx != Num_FPR_Regs) {
2810 unsigned VReg;
2811
2812 if (ObjectVT == MVT::f32)
2813 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2814 else
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002815 VReg = MF.addLiveIn(FPR[FPR_idx], Subtarget.hasVSX() ?
Hal Finkel19be5062014-03-29 05:29:01 +00002816 &PPC::VSFRCRegClass :
2817 &PPC::F8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002818
2819 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2820 ++FPR_idx;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002821 } else if (GPR_idx != Num_GPR_Regs) {
2822 // This can only ever happen in the presence of f32 array types,
2823 // since otherwise we never run out of FPRs before running out
2824 // of GPRs.
2825 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2826 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2827
2828 if (ObjectVT == MVT::f32) {
2829 if ((ArgOffset % PtrByteSize) == (isLittleEndian ? 4 : 0))
2830 ArgVal = DAG.getNode(ISD::SRL, dl, MVT::i64, ArgVal,
2831 DAG.getConstant(32, MVT::i32));
2832 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
2833 }
2834
2835 ArgVal = DAG.getNode(ISD::BITCAST, dl, ObjectVT, ArgVal);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002836 } else {
2837 needsLoad = true;
2838 }
2839
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002840 // When passing an array of floats, the array occupies consecutive
2841 // space in the argument area; only round up to the next doubleword
2842 // at the end of the array. Otherwise, each float takes 8 bytes.
2843 ArgSize = Flags.isInConsecutiveRegs() ? ObjSize : PtrByteSize;
2844 ArgOffset += ArgSize;
2845 if (Flags.isInConsecutiveRegsLast())
2846 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002847 break;
2848 case MVT::v4f32:
2849 case MVT::v4i32:
2850 case MVT::v8i16:
2851 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00002852 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002853 case MVT::v2i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002854 // These can be scalar arguments or elements of a vector array type
2855 // passed directly. The latter are used to implement ELFv2 homogenous
2856 // vector aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002857 if (VR_idx != Num_VR_Regs) {
Hal Finkel7811c612014-03-28 19:58:11 +00002858 unsigned VReg = (ObjectVT == MVT::v2f64 || ObjectVT == MVT::v2i64) ?
2859 MF.addLiveIn(VSRH[VR_idx], &PPC::VSHRCRegClass) :
2860 MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002861 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002862 ++VR_idx;
2863 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002864 needsLoad = true;
2865 }
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00002866 ArgOffset += 16;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002867 break;
2868 }
2869
2870 // We need to load the argument to a virtual register if we determined
2871 // above that we ran out of physical registers of the appropriate type.
2872 if (needsLoad) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00002873 if (ObjSize < ArgSize && !isLittleEndian)
2874 CurArgOffset += ArgSize - ObjSize;
2875 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, isImmutable);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002876 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2877 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2878 false, false, false, 0);
2879 }
2880
2881 InVals.push_back(ArgVal);
2882 }
2883
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002884 // Area that is at least reserved in the caller of this function.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002885 unsigned MinReservedArea;
Ulrich Weigand8658f172014-07-20 23:43:15 +00002886 if (HasParameterArea)
2887 MinReservedArea = std::max(ArgOffset, LinkageSize + 8 * PtrByteSize);
2888 else
2889 MinReservedArea = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002890
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002891 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002892 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002893 // taking the difference between two stack areas will result in an aligned
2894 // stack.
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002895 MinReservedArea = EnsureStackAlignment(MF.getTarget(), MinReservedArea);
2896 FuncInfo->setMinReservedArea(MinReservedArea);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002897
2898 // If the function takes variable number of arguments, make a frame index for
2899 // the start of the first vararg value... for expansion of llvm.va_start.
2900 if (isVarArg) {
2901 int Depth = ArgOffset;
2902
2903 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00002904 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002905 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2906
2907 // If this function is vararg, store any remaining integer argument regs
2908 // to their spots on the stack so that they may be loaded by deferencing the
2909 // result of va_next.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002910 for (GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
2911 GPR_idx < Num_GPR_Regs; ++GPR_idx) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002912 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2913 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2914 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2915 MachinePointerInfo(), false, false, 0);
2916 MemOps.push_back(Store);
2917 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00002918 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002919 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2920 }
2921 }
2922
2923 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002924 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002925
2926 return Chain;
2927}
2928
2929SDValue
2930PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002931 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002932 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002933 const SmallVectorImpl<ISD::InputArg>
2934 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002935 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002936 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002937 // TODO: add description of PPC stack frame format, or at least some docs.
2938 //
2939 MachineFunction &MF = DAG.getMachineFunction();
2940 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002941 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002942
Owen Anderson53aa7a92009-08-10 22:56:29 +00002943 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002944 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002945 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002946 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2947 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00002948 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey48850c12006-11-16 22:43:37 +00002949
Ulrich Weigand8658f172014-07-20 23:43:15 +00002950 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(isPPC64, true,
2951 false);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002952 unsigned ArgOffset = LinkageSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002953 // Area that is at least reserved in caller of this function.
2954 unsigned MinReservedArea = ArgOffset;
2955
Craig Topper840beec2014-04-04 05:16:06 +00002956 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002957 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2958 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2959 };
Craig Topper840beec2014-04-04 05:16:06 +00002960 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00002961 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2962 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2963 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00002964
Craig Topper840beec2014-04-04 05:16:06 +00002965 static const MCPhysReg *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002966
Craig Topper840beec2014-04-04 05:16:06 +00002967 static const MCPhysReg VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002968 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2969 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2970 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00002971
Owen Andersone2f23a32007-09-07 04:06:50 +00002972 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002973 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00002974 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00002975
2976 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002977
Craig Topper840beec2014-04-04 05:16:06 +00002978 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002979
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002980 // In 32-bit non-varargs functions, the stack space for vectors is after the
2981 // stack space for non-vectors. We do not use this space unless we have
2982 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00002983 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002984 // that out...for the pathological case, compute VecArgOffset as the
2985 // start of the vector parameter area. Computing VecArgOffset is the
2986 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002987 unsigned VecArgOffset = ArgOffset;
2988 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002989 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002990 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002991 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002992 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002993
Duncan Sandsd97eea32008-03-21 09:14:45 +00002994 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002995 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00002996 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002997 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002998 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2999 VecArgOffset += ArgSize;
3000 continue;
3001 }
3002
Owen Anderson9f944592009-08-11 20:47:22 +00003003 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003004 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003005 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003006 case MVT::i32:
3007 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003008 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003009 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003010 case MVT::i64: // PPC64
3011 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003012 // FIXME: We are guaranteed to be !isPPC64 at this point.
3013 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003014 VecArgOffset += 8;
3015 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003016 case MVT::v4f32:
3017 case MVT::v4i32:
3018 case MVT::v8i16:
3019 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003020 // Nothing to do, we're only looking at Nonvector args here.
3021 break;
3022 }
3023 }
3024 }
3025 // We've found where the vector parameter area in memory is. Skip the
3026 // first 12 parameters; these don't use that memory.
3027 VecArgOffset = ((VecArgOffset+15)/16)*16;
3028 VecArgOffset += 12*16;
3029
Chris Lattner4302e8f2006-05-16 18:18:50 +00003030 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00003031 // entry to a function on PPC, the arguments start after the linkage area,
3032 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00003033
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003034 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003035 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00003036 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00003037 unsigned CurArgIdx = 0;
3038 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003039 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003040 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00003041 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00003042 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00003043 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003044 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt38b6cb52013-05-08 17:22:33 +00003045 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
3046 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003047
Chris Lattner318f0d22006-05-16 18:51:52 +00003048 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003049
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003050 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00003051 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
3052 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003053 if (isVarArg || isPPC64) {
3054 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003055 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003056 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003057 PtrByteSize);
3058 } else nAltivecParamsAtEnd++;
3059 } else
3060 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003061 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003062 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003063 PtrByteSize);
3064
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003065 // FIXME the codegen can be much improved in some cases.
3066 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003067 if (Flags.isByVal()) {
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003068 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003069 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003070 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003071 // Objects of size 1 and 2 are right justified, everything else is
3072 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00003073 if (ObjSize==1 || ObjSize==2) {
3074 CurArgOffset = CurArgOffset + (4 - ObjSize);
3075 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003076 // The value of the object is its address.
Hal Finkel41a55ad2014-08-16 00:17:05 +00003077 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, false, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003078 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003079 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003080 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00003081 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003082 unsigned VReg;
3083 if (isPPC64)
3084 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3085 else
3086 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003087 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003088 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003089 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003090 MachinePointerInfo(FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003091 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00003092 MemOps.push_back(Store);
3093 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00003094 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003095
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003096 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00003097
Dale Johannesen21a8f142008-03-08 01:41:42 +00003098 continue;
3099 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003100 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
3101 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003102 // to memory. ArgOffset will be the address of the beginning
3103 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003104 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003105 unsigned VReg;
3106 if (isPPC64)
3107 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3108 else
3109 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00003110 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003111 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003112 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003113 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003114 MachinePointerInfo(FuncArg, j),
David Greene87a5abe2010-02-15 16:56:53 +00003115 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003116 MemOps.push_back(Store);
3117 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003118 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003119 } else {
3120 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
3121 break;
3122 }
3123 }
3124 continue;
3125 }
3126
Owen Anderson9f944592009-08-11 20:47:22 +00003127 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003128 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003129 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003130 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00003131 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00003132 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003133 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003134 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Hal Finkel7f908e82014-03-06 00:45:19 +00003135
3136 if (ObjectVT == MVT::i1)
3137 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal);
3138
Bill Wendling968f32c2008-03-07 20:49:02 +00003139 ++GPR_idx;
3140 } else {
3141 needsLoad = true;
3142 ArgSize = PtrByteSize;
3143 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003144 // All int arguments reserve stack space in the Darwin ABI.
3145 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00003146 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003147 }
Bill Wendling968f32c2008-03-07 20:49:02 +00003148 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00003149 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00003150 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003151 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003152 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00003153
Hal Finkel940ab932014-02-28 00:27:01 +00003154 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00003155 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00003156 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003157 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00003158
Chris Lattnerec78cad2006-06-26 22:48:35 +00003159 ++GPR_idx;
3160 } else {
3161 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00003162 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003163 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003164 // All int arguments reserve stack space in the Darwin ABI.
3165 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003166 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003167
Owen Anderson9f944592009-08-11 20:47:22 +00003168 case MVT::f32:
3169 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00003170 // Every 4 bytes of argument space consumes one of the GPRs available for
3171 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003172 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003173 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00003174 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003175 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00003176 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003177 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003178 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003179
Owen Anderson9f944592009-08-11 20:47:22 +00003180 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00003181 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003182 else
Devang Patelf3292b22011-02-21 23:21:26 +00003183 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003184
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003185 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003186 ++FPR_idx;
3187 } else {
3188 needsLoad = true;
3189 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003190
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003191 // All FP arguments reserve stack space in the Darwin ABI.
3192 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003193 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003194 case MVT::v4f32:
3195 case MVT::v4i32:
3196 case MVT::v8i16:
3197 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00003198 // Note that vector arguments in registers don't reserve stack space,
3199 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003200 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003201 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003202 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00003203 if (isVarArg) {
3204 while ((ArgOffset % 16) != 0) {
3205 ArgOffset += PtrByteSize;
3206 if (GPR_idx != Num_GPR_Regs)
3207 GPR_idx++;
3208 }
3209 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003210 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00003211 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003212 ++VR_idx;
3213 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003214 if (!isVarArg && !isPPC64) {
3215 // Vectors go after all the nonvectors.
3216 CurArgOffset = VecArgOffset;
3217 VecArgOffset += 16;
3218 } else {
3219 // Vectors are aligned.
3220 ArgOffset = ((ArgOffset+15)/16)*16;
3221 CurArgOffset = ArgOffset;
3222 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00003223 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003224 needsLoad = true;
3225 }
3226 break;
3227 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003228
Chris Lattner4302e8f2006-05-16 18:18:50 +00003229 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003230 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003231 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003232 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003233 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00003234 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003235 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00003236 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003237 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003238 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003239
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003240 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003241 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003242
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003243 // Allow for Altivec parameters at the end, if needed.
3244 if (nAltivecParamsAtEnd) {
3245 MinReservedArea = ((MinReservedArea+15)/16)*16;
3246 MinReservedArea += 16*nAltivecParamsAtEnd;
3247 }
3248
3249 // Area that is at least reserved in the caller of this function.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003250 MinReservedArea = std::max(MinReservedArea, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003251
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003252 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003253 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003254 // taking the difference between two stack areas will result in an aligned
3255 // stack.
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003256 MinReservedArea = EnsureStackAlignment(MF.getTarget(), MinReservedArea);
3257 FuncInfo->setMinReservedArea(MinReservedArea);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003258
Chris Lattner4302e8f2006-05-16 18:18:50 +00003259 // If the function takes variable number of arguments, make a frame index for
3260 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003261 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003262 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003263
Dan Gohman31ae5862010-04-17 14:41:14 +00003264 FuncInfo->setVarArgsFrameIndex(
3265 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00003266 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00003267 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003268
Chris Lattner4302e8f2006-05-16 18:18:50 +00003269 // If this function is vararg, store any remaining integer argument regs
3270 // to their spots on the stack so that they may be loaded by deferencing the
3271 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003272 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00003273 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00003274
Chris Lattner2cca3852006-11-18 01:57:19 +00003275 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00003276 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003277 else
Devang Patelf3292b22011-02-21 23:21:26 +00003278 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003279
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003280 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00003281 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3282 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003283 MemOps.push_back(Store);
3284 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003285 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00003286 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003287 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003288 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003289
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003290 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003291 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003292
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003293 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003294}
3295
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003296/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003297/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00003298static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003299 unsigned ParamSize) {
3300
Dale Johannesen86dcae12009-11-24 01:09:07 +00003301 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003302
3303 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
3304 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3305 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3306 // Remember only if the new adjustement is bigger.
3307 if (SPDiff < FI->getTailCallSPDelta())
3308 FI->setTailCallSPDelta(SPDiff);
3309
3310 return SPDiff;
3311}
3312
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003313/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3314/// for tail call optimization. Targets which want to do tail call
3315/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003316bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003317PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003318 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003319 bool isVarArg,
3320 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003321 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003322 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00003323 return false;
3324
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003325 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003326 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003327 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003328
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003329 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00003330 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003331 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
3332 // Functions containing by val parameters are not supported.
3333 for (unsigned i = 0; i != Ins.size(); i++) {
3334 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3335 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003336 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003337
Alp Tokerf907b892013-12-05 05:44:44 +00003338 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003339 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
3340 return true;
3341
3342 // At the moment we can only do local tail calls (in same module, hidden
3343 // or protected) if we are generating PIC.
3344 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
3345 return G->getGlobal()->hasHiddenVisibility()
3346 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003347 }
3348
3349 return false;
3350}
3351
Chris Lattnereb755fc2006-05-17 19:00:46 +00003352/// isCallCompatibleAddress - Return the immediate to use if the specified
3353/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003354static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00003355 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003356 if (!C) return nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003357
Dan Gohmaneffb8942008-09-12 16:56:44 +00003358 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003359 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00003360 SignExtend32<26>(Addr) != Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +00003361 return nullptr; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003362
Dan Gohmaneffb8942008-09-12 16:56:44 +00003363 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00003364 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003365}
3366
Dan Gohmand78c4002008-05-13 00:00:25 +00003367namespace {
3368
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003369struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003370 SDValue Arg;
3371 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003372 int FrameIdx;
3373
3374 TailCallArgumentInfo() : FrameIdx(0) {}
3375};
3376
Dan Gohmand78c4002008-05-13 00:00:25 +00003377}
3378
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003379/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3380static void
3381StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003382 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003383 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3384 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003385 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003386 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003387 SDValue Arg = TailCallArgs[i].Arg;
3388 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003389 int FI = TailCallArgs[i].FrameIdx;
3390 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003391 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003392 MachinePointerInfo::getFixedStack(FI),
3393 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003394 }
3395}
3396
3397/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3398/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003399static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003400 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003401 SDValue Chain,
3402 SDValue OldRetAddr,
3403 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003404 int SPDiff,
3405 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003406 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003407 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003408 if (SPDiff) {
3409 // Calculate the new stack slot for the return address.
3410 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003411 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003412 isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003413 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003414 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003415 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003416 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003417 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003418 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003419 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003420
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003421 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3422 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003423 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003424 int NewFPLoc =
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003425 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene1fbe0542009-11-12 20:49:22 +00003426 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003427 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003428 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3429 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003430 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003431 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003432 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003433 }
3434 return Chain;
3435}
3436
3437/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3438/// the position of the argument.
3439static void
3440CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003441 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003442 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003443 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003444 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003445 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003446 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003447 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003448 TailCallArgumentInfo Info;
3449 Info.Arg = Arg;
3450 Info.FrameIdxOp = FIN;
3451 Info.FrameIdx = FI;
3452 TailCallArguments.push_back(Info);
3453}
3454
3455/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3456/// stack slot. Returns the chain as result and the loaded frame pointers in
3457/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003458SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003459 int SPDiff,
3460 SDValue Chain,
3461 SDValue &LROpOut,
3462 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003463 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003464 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003465 if (SPDiff) {
3466 // Load the LR and FP stack slot for later adjusting.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003467 EVT VT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003468 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003469 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003470 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003471 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003472
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003473 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3474 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003475 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003476 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003477 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003478 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003479 Chain = SDValue(FPOpOut.getNode(), 1);
3480 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003481 }
3482 return Chain;
3483}
3484
Dale Johannesen85d41a12008-03-04 23:17:14 +00003485/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003486/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003487/// specified by the specific parameter attribute. The copy will be passed as
3488/// a byval function parameter.
3489/// Sometimes what we are copying is the end of a larger object, the part that
3490/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003491static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003492CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003493 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003494 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003495 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003496 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003497 false, false, MachinePointerInfo(),
3498 MachinePointerInfo());
Dale Johannesen85d41a12008-03-04 23:17:14 +00003499}
Chris Lattner43df5b32007-02-25 05:34:32 +00003500
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003501/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3502/// tail calls.
3503static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003504LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3505 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003506 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003507 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3508 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003509 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003510 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003511 if (!isTailCall) {
3512 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003513 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003514 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003515 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003516 else
Owen Anderson9f944592009-08-11 20:47:22 +00003517 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003518 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003519 DAG.getConstant(ArgOffset, PtrVT));
3520 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003521 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3522 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003523 // Calculate and remember argument location.
3524 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3525 TailCallArguments);
3526}
3527
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003528static
3529void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003530 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003531 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003532 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003533 MachineFunction &MF = DAG.getMachineFunction();
3534
3535 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3536 // might overwrite each other in case of tail call optimization.
3537 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003538 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003539 InFlag = SDValue();
3540 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3541 MemOpChains2, dl);
3542 if (!MemOpChains2.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003543 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003544
3545 // Store the return address to the appropriate stack slot.
3546 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3547 isPPC64, isDarwinABI, dl);
3548
3549 // Emit callseq_end just before tailcall node.
3550 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003551 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003552 InFlag = Chain.getValue(1);
3553}
3554
3555static
3556unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003557 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003558 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3559 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003560 const PPCSubtarget &Subtarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003561
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003562 bool isPPC64 = Subtarget.isPPC64();
3563 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003564 bool isELFv2ABI = Subtarget.isELFv2ABI();
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003565
Owen Anderson53aa7a92009-08-10 22:56:29 +00003566 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003567 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003568 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003569
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003570 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003571
Torok Edwin31e90d22010-08-04 20:47:44 +00003572 bool needIndirectCall = true;
Ulrich Weigand9aa09ef2014-06-18 16:14:04 +00003573 if (!isSVR4ABI || !isPPC64)
3574 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
3575 // If this is an absolute destination address, use the munged value.
3576 Callee = SDValue(Dest, 0);
3577 needIndirectCall = false;
3578 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003579
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003580 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Eric Christopher79cc1e32014-09-02 22:28:02 +00003581 unsigned OpFlags = 0;
3582 if ((DAG.getTarget().getRelocationModel() != Reloc::Static &&
3583 (Subtarget.getTargetTriple().isMacOSX() &&
3584 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
3585 (G->getGlobal()->isDeclaration() ||
3586 G->getGlobal()->isWeakForLinker())) ||
3587 (Subtarget.isTargetELF() && !isPPC64 &&
3588 !G->getGlobal()->hasLocalLinkage() &&
3589 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
3590 // PC-relative references to external symbols should go through $stub,
3591 // unless we're building with the leopard linker or later, which
3592 // automatically synthesizes these stubs.
3593 OpFlags = PPCII::MO_PLT_OR_STUB;
Eric Christopherb9fd9ed2014-08-07 22:02:54 +00003594 }
Eric Christopher79cc1e32014-09-02 22:28:02 +00003595
3596 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3597 // every direct call is) turn it into a TargetGlobalAddress /
3598 // TargetExternalSymbol node so that legalize doesn't hack it.
3599 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
3600 Callee.getValueType(), 0, OpFlags);
3601 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003602 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003603
Torok Edwin31e90d22010-08-04 20:47:44 +00003604 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003605 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003606
Hal Finkel3ee2af72014-07-18 23:29:49 +00003607 if ((DAG.getTarget().getRelocationModel() != Reloc::Static &&
3608 (Subtarget.getTargetTriple().isMacOSX() &&
3609 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5))) ||
3610 (Subtarget.isTargetELF() && !isPPC64 &&
3611 DAG.getTarget().getRelocationModel() == Reloc::PIC_) ) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003612 // PC-relative references to external symbols should go through $stub,
3613 // unless we're building with the leopard linker or later, which
3614 // automatically synthesizes these stubs.
Hal Finkel3ee2af72014-07-18 23:29:49 +00003615 OpFlags = PPCII::MO_PLT_OR_STUB;
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003616 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003617
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003618 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3619 OpFlags);
3620 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003621 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003622
Torok Edwin31e90d22010-08-04 20:47:44 +00003623 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003624 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3625 // to do the call, we can't use PPCISD::CALL.
3626 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003627
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003628 if (isSVR4ABI && isPPC64 && !isELFv2ABI) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003629 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3630 // entry point, but to the function descriptor (the function entry point
3631 // address is part of the function descriptor though).
3632 // The function descriptor is a three doubleword structure with the
3633 // following fields: function entry point, TOC base address and
3634 // environment pointer.
3635 // Thus for a call through a function pointer, the following actions need
3636 // to be performed:
3637 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003638 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003639 // 2. Load the address of the function entry point from the function
3640 // descriptor.
3641 // 3. Load the TOC of the callee from the function descriptor into r2.
3642 // 4. Load the environment pointer from the function descriptor into
3643 // r11.
3644 // 5. Branch to the function entry point address.
3645 // 6. On return of the callee, the TOC of the caller needs to be
3646 // restored (this is done in FinishCall()).
3647 //
3648 // All those operations are flagged together to ensure that no other
3649 // operations can be scheduled in between. E.g. without flagging the
3650 // operations together, a TOC access in the caller could be scheduled
3651 // between the load of the callee TOC and the branch to the callee, which
3652 // results in the TOC access going through the TOC of the callee instead
3653 // of going through the TOC of the caller, which leads to incorrect code.
3654
3655 // Load the address of the function entry point from the function
3656 // descriptor.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003657 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00003658 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00003659 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
Tilmann Scheller79fef932009-12-18 13:00:15 +00003660 Chain = LoadFuncPtr.getValue(1);
3661 InFlag = LoadFuncPtr.getValue(2);
3662
3663 // Load environment pointer into r11.
3664 // Offset of the environment pointer within the function descriptor.
3665 SDValue PtrOff = DAG.getIntPtrConstant(16);
3666
3667 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3668 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3669 InFlag);
3670 Chain = LoadEnvPtr.getValue(1);
3671 InFlag = LoadEnvPtr.getValue(2);
3672
3673 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3674 InFlag);
3675 Chain = EnvVal.getValue(0);
3676 InFlag = EnvVal.getValue(1);
3677
3678 // Load TOC of the callee into r2. We are using a target-specific load
3679 // with r2 hard coded, because the result of a target-independent load
3680 // would never go directly into r2, since r2 is a reserved register (which
3681 // prevents the register allocator from allocating it), resulting in an
3682 // additional register being allocated and an unnecessary move instruction
3683 // being generated.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003684 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Ulrich Weigandad0cb912014-06-18 17:52:49 +00003685 SDValue TOCOff = DAG.getIntPtrConstant(8);
3686 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, TOCOff);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003687 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
Ulrich Weigandad0cb912014-06-18 17:52:49 +00003688 AddTOC, InFlag);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003689 Chain = LoadTOCPtr.getValue(0);
3690 InFlag = LoadTOCPtr.getValue(1);
3691
3692 MTCTROps[0] = Chain;
3693 MTCTROps[1] = LoadFuncPtr;
3694 MTCTROps[2] = InFlag;
3695 }
3696
Craig Topper48d114b2014-04-26 18:35:24 +00003697 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00003698 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003699 InFlag = Chain.getValue(1);
3700
3701 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00003702 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003703 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003704 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003705 CallOpc = PPCISD::BCTRL;
Craig Topper062a2ba2014-04-25 05:30:21 +00003706 Callee.setNode(nullptr);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003707 // Add use of X11 (holding environment pointer)
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003708 if (isSVR4ABI && isPPC64 && !isELFv2ABI)
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003709 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003710 // Add CTR register as callee so a bctr can be emitted later.
3711 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00003712 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003713 }
3714
3715 // If this is a direct call, pass the chain and the callee.
3716 if (Callee.getNode()) {
3717 Ops.push_back(Chain);
3718 Ops.push_back(Callee);
3719 }
3720 // If this is a tail call add stack pointer delta.
3721 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00003722 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003723
3724 // Add argument registers to the end of the list so that they are known live
3725 // into the call.
3726 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3727 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3728 RegsToPass[i].second.getValueType()));
3729
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003730 // Direct calls in the ELFv2 ABI need the TOC register live into the call.
3731 if (Callee.getNode() && isELFv2ABI)
3732 Ops.push_back(DAG.getRegister(PPC::X2, PtrVT));
3733
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003734 return CallOpc;
3735}
3736
Roman Divacky76293062012-09-18 16:47:58 +00003737static
3738bool isLocalCall(const SDValue &Callee)
3739{
3740 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00003741 return !G->getGlobal()->isDeclaration() &&
3742 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00003743 return false;
3744}
3745
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003746SDValue
3747PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003748 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003749 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003750 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003751 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003752
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003753 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003754 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
3755 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003756 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003757
3758 // Copy all of the result registers out of their specified physreg.
3759 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3760 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003761 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00003762
3763 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3764 VA.getLocReg(), VA.getLocVT(), InFlag);
3765 Chain = Val.getValue(1);
3766 InFlag = Val.getValue(2);
3767
3768 switch (VA.getLocInfo()) {
3769 default: llvm_unreachable("Unknown loc info!");
3770 case CCValAssign::Full: break;
3771 case CCValAssign::AExt:
3772 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3773 break;
3774 case CCValAssign::ZExt:
3775 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3776 DAG.getValueType(VA.getValVT()));
3777 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3778 break;
3779 case CCValAssign::SExt:
3780 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3781 DAG.getValueType(VA.getValVT()));
3782 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3783 break;
3784 }
3785
3786 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003787 }
3788
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003789 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003790}
3791
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003792SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00003793PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003794 bool isTailCall, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003795 SelectionDAG &DAG,
3796 SmallVector<std::pair<unsigned, SDValue>, 8>
3797 &RegsToPass,
3798 SDValue InFlag, SDValue Chain,
3799 SDValue &Callee,
3800 int SPDiff, unsigned NumBytes,
3801 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003802 SmallVectorImpl<SDValue> &InVals) const {
Ulrich Weigand8658f172014-07-20 23:43:15 +00003803
3804 bool isELFv2ABI = Subtarget.isELFv2ABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003805 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003806 SmallVector<SDValue, 8> Ops;
3807 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3808 isTailCall, RegsToPass, Ops, NodeTys,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003809 Subtarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003810
Hal Finkel5ab37802012-08-28 02:10:27 +00003811 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003812 if (isVarArg && Subtarget.isSVR4ABI() && !Subtarget.isPPC64())
Hal Finkel5ab37802012-08-28 02:10:27 +00003813 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3814
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003815 // When performing tail call optimization the callee pops its arguments off
3816 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00003817 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003818 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003819 (CallConv == CallingConv::Fast &&
3820 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003821
Roman Divackyef21be22012-03-06 16:41:49 +00003822 // Add a register mask operand representing the call-preserved registers.
Eric Christopherd9134482014-08-04 21:25:23 +00003823 const TargetRegisterInfo *TRI =
3824 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Roman Divackyef21be22012-03-06 16:41:49 +00003825 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3826 assert(Mask && "Missing call preserved mask for calling convention");
3827 Ops.push_back(DAG.getRegisterMask(Mask));
3828
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003829 if (InFlag.getNode())
3830 Ops.push_back(InFlag);
3831
3832 // Emit tail call.
3833 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003834 assert(((Callee.getOpcode() == ISD::Register &&
3835 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3836 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3837 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3838 isa<ConstantSDNode>(Callee)) &&
3839 "Expecting an global address, external symbol, absolute value or register");
3840
Craig Topper48d114b2014-04-26 18:35:24 +00003841 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003842 }
3843
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003844 // Add a NOP immediately after the branch instruction when using the 64-bit
3845 // SVR4 ABI. At link time, if caller and callee are in a different module and
3846 // thus have a different TOC, the call will be replaced with a call to a stub
3847 // function which saves the current TOC, loads the TOC of the callee and
3848 // branches to the callee. The NOP will be replaced with a load instruction
3849 // which restores the TOC of the caller from the TOC save slot of the current
3850 // stack frame. If caller and callee belong to the same module (and have the
3851 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00003852
3853 bool needsTOCRestore = false;
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003854 if (!isTailCall && Subtarget.isSVR4ABI()&& Subtarget.isPPC64()) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003855 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003856 // This is a call through a function pointer.
3857 // Restore the caller TOC from the save area into R2.
3858 // See PrepareCall() for more information about calls through function
3859 // pointers in the 64-bit SVR4 ABI.
3860 // We are using a target-specific load with r2 hard coded, because the
3861 // result of a target-independent load would never go directly into r2,
3862 // since r2 is a reserved register (which prevents the register allocator
3863 // from allocating it), resulting in an additional register being
3864 // allocated and an unnecessary move instruction being generated.
Hal Finkel51861b42012-03-31 14:45:15 +00003865 needsTOCRestore = true;
Bill Schmidtcea15962013-09-26 17:09:28 +00003866 } else if ((CallOpc == PPCISD::CALL) &&
3867 (!isLocalCall(Callee) ||
3868 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Roman Divacky76293062012-09-18 16:47:58 +00003869 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003870 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller79fef932009-12-18 13:00:15 +00003871 }
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003872 }
3873
Craig Topper48d114b2014-04-26 18:35:24 +00003874 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Hal Finkel51861b42012-03-31 14:45:15 +00003875 InFlag = Chain.getValue(1);
3876
3877 if (needsTOCRestore) {
3878 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Ulrich Weigandad0cb912014-06-18 17:52:49 +00003879 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3880 SDValue StackPtr = DAG.getRegister(PPC::X1, PtrVT);
Ulrich Weigand8658f172014-07-20 23:43:15 +00003881 unsigned TOCSaveOffset = PPCFrameLowering::getTOCSaveOffset(isELFv2ABI);
Ulrich Weigandad0cb912014-06-18 17:52:49 +00003882 SDValue TOCOff = DAG.getIntPtrConstant(TOCSaveOffset);
3883 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, StackPtr, TOCOff);
3884 Chain = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain, AddTOC, InFlag);
Hal Finkel51861b42012-03-31 14:45:15 +00003885 InFlag = Chain.getValue(1);
3886 }
3887
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003888 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3889 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003890 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003891 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003892 InFlag = Chain.getValue(1);
3893
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003894 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3895 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003896}
3897
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003898SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00003899PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003900 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00003901 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00003902 SDLoc &dl = CLI.DL;
3903 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3904 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3905 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00003906 SDValue Chain = CLI.Chain;
3907 SDValue Callee = CLI.Callee;
3908 bool &isTailCall = CLI.IsTailCall;
3909 CallingConv::ID CallConv = CLI.CallConv;
3910 bool isVarArg = CLI.IsVarArg;
3911
Evan Cheng67a69dd2010-01-27 00:07:07 +00003912 if (isTailCall)
3913 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3914 Ins, DAG);
3915
Reid Kleckner5772b772014-04-24 20:14:34 +00003916 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
3917 report_fatal_error("failed to perform tail call elimination on a call "
3918 "site marked musttail");
3919
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003920 if (Subtarget.isSVR4ABI()) {
3921 if (Subtarget.isPPC64())
Bill Schmidt57d6de52012-10-23 15:51:16 +00003922 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3923 isTailCall, Outs, OutVals, Ins,
3924 dl, DAG, InVals);
3925 else
3926 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3927 isTailCall, Outs, OutVals, Ins,
3928 dl, DAG, InVals);
3929 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003930
Bill Schmidt57d6de52012-10-23 15:51:16 +00003931 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3932 isTailCall, Outs, OutVals, Ins,
3933 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003934}
3935
3936SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003937PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3938 CallingConv::ID CallConv, bool isVarArg,
3939 bool isTailCall,
3940 const SmallVectorImpl<ISD::OutputArg> &Outs,
3941 const SmallVectorImpl<SDValue> &OutVals,
3942 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003943 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003944 SmallVectorImpl<SDValue> &InVals) const {
3945 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003946 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003947
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003948 assert((CallConv == CallingConv::C ||
3949 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003950
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003951 unsigned PtrByteSize = 4;
3952
3953 MachineFunction &MF = DAG.getMachineFunction();
3954
3955 // Mark this function as potentially containing a function that contains a
3956 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3957 // and restoring the callers stack pointer in this functions epilog. This is
3958 // done because by tail calling the called function might overwrite the value
3959 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003960 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3961 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003962 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00003963
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003964 // Count how many bytes are to be pushed on the stack, including the linkage
3965 // area, parameter list area and the part of the local variable space which
3966 // contains copies of aggregates which are passed by value.
3967
3968 // Assign locations to all of the outgoing arguments.
3969 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003970 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3971 *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003972
3973 // Reserve space for the linkage area on the stack.
Ulrich Weigand8658f172014-07-20 23:43:15 +00003974 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false, false),
3975 PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003976
3977 if (isVarArg) {
3978 // Handle fixed and variable vector arguments differently.
3979 // Fixed vector arguments go into registers as long as registers are
3980 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003981 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00003982
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003983 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003984 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003985 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003986 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00003987
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003988 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00003989 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3990 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003991 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00003992 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3993 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003994 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003995
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003996 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003997#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00003998 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00003999 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00004000#endif
Craig Toppere73658d2014-04-28 04:05:08 +00004001 llvm_unreachable(nullptr);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004002 }
4003 }
4004 } else {
4005 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00004006 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004007 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004008
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004009 // Assign locations to all of the outgoing aggregate by value arguments.
4010 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004011 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Eric Christopherb5217502014-08-06 18:45:26 +00004012 ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004013
4014 // Reserve stack space for the allocations in CCInfo.
4015 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
4016
Bill Schmidtef17c142013-02-06 17:33:58 +00004017 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004018
4019 // Size of the linkage area, parameter list area and the part of the local
4020 // space variable where copies of aggregates which are passed by value are
4021 // stored.
4022 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004023
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004024 // Calculate by how many bytes the stack has to be adjusted in case of tail
4025 // call optimization.
4026 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4027
4028 // Adjust the stack pointer for the new arguments...
4029 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004030 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4031 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004032 SDValue CallSeqStart = Chain;
4033
4034 // Load the return address and frame pointer so it can be moved somewhere else
4035 // later.
4036 SDValue LROp, FPOp;
4037 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
4038 dl);
4039
4040 // Set up a copy of the stack pointer for use loading and storing any
4041 // arguments that may not fit in the registers available for argument
4042 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00004043 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00004044
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004045 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4046 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4047 SmallVector<SDValue, 8> MemOpChains;
4048
Roman Divacky71038e72011-08-30 17:04:16 +00004049 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004050 // Walk the register/memloc assignments, inserting copies/loads.
4051 for (unsigned i = 0, j = 0, e = ArgLocs.size();
4052 i != e;
4053 ++i) {
4054 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004055 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004056 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00004057
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004058 if (Flags.isByVal()) {
4059 // Argument is an aggregate which is passed by value, thus we need to
4060 // create a copy of it in the local variable space of the current stack
4061 // frame (which is the stack frame of the caller) and pass the address of
4062 // this copy to the callee.
4063 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
4064 CCValAssign &ByValVA = ByValArgLocs[j++];
4065 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00004066
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004067 // Memory reserved in the local variable space of the callers stack frame.
4068 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004069
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004070 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
4071 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00004072
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004073 // Create a copy of the argument in the local area of the current
4074 // stack frame.
4075 SDValue MemcpyCall =
4076 CreateCopyOfByValArgument(Arg, PtrOff,
4077 CallSeqStart.getNode()->getOperand(0),
4078 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00004079
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004080 // This must go outside the CALLSEQ_START..END.
4081 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004082 CallSeqStart.getNode()->getOperand(1),
4083 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004084 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4085 NewCallSeqStart.getNode());
4086 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00004087
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004088 // Pass the address of the aggregate copy on the stack either in a
4089 // physical register or in the parameter list area of the current stack
4090 // frame to the callee.
4091 Arg = PtrOff;
4092 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004093
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004094 if (VA.isRegLoc()) {
Hal Finkel2a9d3182014-03-06 00:23:33 +00004095 if (Arg.getValueType() == MVT::i1)
4096 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg);
4097
Roman Divacky71038e72011-08-30 17:04:16 +00004098 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004099 // Put argument in a physical register.
4100 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
4101 } else {
4102 // Put argument in the parameter list area of the current stack frame.
4103 assert(VA.isMemLoc());
4104 unsigned LocMemOffset = VA.getLocMemOffset();
4105
4106 if (!isTailCall) {
4107 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
4108 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
4109
4110 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00004111 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004112 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004113 } else {
4114 // Calculate and remember argument location.
4115 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
4116 TailCallArguments);
4117 }
4118 }
4119 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004120
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004121 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004122 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Wesley Peck527da1b2010-11-23 03:31:01 +00004123
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004124 // Build a sequence of copy-to-reg nodes chained together with token chain
4125 // and flag operands which copy the outgoing args into the appropriate regs.
4126 SDValue InFlag;
4127 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4128 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4129 RegsToPass[i].second, InFlag);
4130 InFlag = Chain.getValue(1);
4131 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004132
Hal Finkel5ab37802012-08-28 02:10:27 +00004133 // Set CR bit 6 to true if this is a vararg call with floating args passed in
4134 // registers.
4135 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004136 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
4137 SDValue Ops[] = { Chain, InFlag };
4138
Hal Finkel5ab37802012-08-28 02:10:27 +00004139 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00004140 dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1));
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004141
Hal Finkel5ab37802012-08-28 02:10:27 +00004142 InFlag = Chain.getValue(1);
4143 }
4144
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004145 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004146 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
4147 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004148
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004149 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4150 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4151 Ins, InVals);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004152}
4153
Bill Schmidt57d6de52012-10-23 15:51:16 +00004154// Copy an argument into memory, being careful to do this outside the
4155// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004156SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00004157PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
4158 SDValue CallSeqStart,
4159 ISD::ArgFlagsTy Flags,
4160 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004161 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004162 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
4163 CallSeqStart.getNode()->getOperand(0),
4164 Flags, DAG, dl);
4165 // The MEMCPY must go outside the CALLSEQ_START..END.
4166 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004167 CallSeqStart.getNode()->getOperand(1),
4168 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004169 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4170 NewCallSeqStart.getNode());
4171 return NewCallSeqStart;
4172}
4173
4174SDValue
4175PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004176 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004177 bool isTailCall,
4178 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004179 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004180 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004181 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004182 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004183
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004184 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004185 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004186 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004187
Bill Schmidt57d6de52012-10-23 15:51:16 +00004188 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
4189 unsigned PtrByteSize = 8;
4190
4191 MachineFunction &MF = DAG.getMachineFunction();
4192
4193 // Mark this function as potentially containing a function that contains a
4194 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4195 // and restoring the callers stack pointer in this functions epilog. This is
4196 // done because by tail calling the called function might overwrite the value
4197 // in this function's (MF) stack pointer stack slot 0(SP).
4198 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4199 CallConv == CallingConv::Fast)
4200 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4201
Bill Schmidt57d6de52012-10-23 15:51:16 +00004202 // Count how many bytes are to be pushed on the stack, including the linkage
Ulrich Weigand8658f172014-07-20 23:43:15 +00004203 // area, and parameter passing area. On ELFv1, the linkage area is 48 bytes
4204 // reserved space for [SP][CR][LR][2 x unused][TOC]; on ELFv2, the linkage
4205 // area is 32 bytes reserved space for [SP][CR][LR][TOC].
4206 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(true, false,
4207 isELFv2ABI);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004208 unsigned NumBytes = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004209
4210 // Add up all the space actually used.
4211 for (unsigned i = 0; i != NumOps; ++i) {
4212 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4213 EVT ArgVT = Outs[i].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004214 EVT OrigVT = Outs[i].ArgVT;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004215
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004216 /* Respect alignment of argument on the stack. */
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004217 unsigned Align =
4218 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004219 NumBytes = ((NumBytes + Align - 1) / Align) * Align;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004220
4221 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004222 if (Flags.isInConsecutiveRegsLast())
4223 NumBytes = ((NumBytes + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004224 }
4225
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004226 unsigned NumBytesActuallyUsed = NumBytes;
4227
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004228 // The prolog code of the callee may store up to 8 GPR argument registers to
4229 // the stack, allowing va_start to index over them in memory if its varargs.
4230 // Because we cannot tell if this is needed on the caller side, we have to
4231 // conservatively assume that it is needed. As such, make sure we have at
4232 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00004233 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004234 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004235
4236 // Tail call needs the stack to be aligned.
4237 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4238 CallConv == CallingConv::Fast)
4239 NumBytes = EnsureStackAlignment(MF.getTarget(), NumBytes);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004240
4241 // Calculate by how many bytes the stack has to be adjusted in case of tail
4242 // call optimization.
4243 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4244
4245 // To protect arguments on the stack from being clobbered in a tail call,
4246 // force all the loads to happen before doing any other lowering.
4247 if (isTailCall)
4248 Chain = DAG.getStackArgumentTokenFactor(Chain);
4249
4250 // Adjust the stack pointer for the new arguments...
4251 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004252 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4253 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004254 SDValue CallSeqStart = Chain;
4255
4256 // Load the return address and frame pointer so it can be move somewhere else
4257 // later.
4258 SDValue LROp, FPOp;
4259 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4260 dl);
4261
4262 // Set up a copy of the stack pointer for use loading and storing any
4263 // arguments that may not fit in the registers available for argument
4264 // passing.
4265 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
4266
4267 // Figure out which arguments are going to go in registers, and which in
4268 // memory. Also, if this is a vararg function, floating point operations
4269 // must be stored to our stack, and loaded into integer regs as well, if
4270 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004271 unsigned ArgOffset = LinkageSize;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004272 unsigned GPR_idx, FPR_idx = 0, VR_idx = 0;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004273
Craig Topper840beec2014-04-04 05:16:06 +00004274 static const MCPhysReg GPR[] = {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004275 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4276 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4277 };
Craig Topper840beec2014-04-04 05:16:06 +00004278 static const MCPhysReg *FPR = GetFPR();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004279
Craig Topper840beec2014-04-04 05:16:06 +00004280 static const MCPhysReg VR[] = {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004281 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4282 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4283 };
Craig Topper840beec2014-04-04 05:16:06 +00004284 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00004285 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
4286 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
4287 };
4288
Bill Schmidt57d6de52012-10-23 15:51:16 +00004289 const unsigned NumGPRs = array_lengthof(GPR);
4290 const unsigned NumFPRs = 13;
4291 const unsigned NumVRs = array_lengthof(VR);
4292
4293 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4294 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4295
4296 SmallVector<SDValue, 8> MemOpChains;
4297 for (unsigned i = 0; i != NumOps; ++i) {
4298 SDValue Arg = OutVals[i];
4299 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004300 EVT ArgVT = Outs[i].VT;
4301 EVT OrigVT = Outs[i].ArgVT;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004302
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004303 /* Respect alignment of argument on the stack. */
4304 unsigned Align =
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004305 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004306 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
4307
4308 /* Compute GPR index associated with argument offset. */
4309 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
4310 GPR_idx = std::min(GPR_idx, NumGPRs);
4311
Bill Schmidt57d6de52012-10-23 15:51:16 +00004312 // PtrOff will be used to store the current argument to the stack if a
4313 // register cannot be found for it.
4314 SDValue PtrOff;
4315
4316 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
4317
4318 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4319
4320 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00004321 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004322 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4323 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4324 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
4325 }
4326
4327 // FIXME memcpy is used way more than necessary. Correctness first.
4328 // Note: "by value" is code for passing a structure by value, not
4329 // basic types.
4330 if (Flags.isByVal()) {
4331 // Note: Size includes alignment padding, so
4332 // struct x { short a; char b; }
4333 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
4334 // These are the proper values we need for right-justifying the
4335 // aggregate in a parameter register.
4336 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00004337
4338 // An empty aggregate parameter takes up no storage and no
4339 // registers.
4340 if (Size == 0)
4341 continue;
4342
Bill Schmidt57d6de52012-10-23 15:51:16 +00004343 // All aggregates smaller than 8 bytes must be passed right-justified.
4344 if (Size==1 || Size==2 || Size==4) {
4345 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
4346 if (GPR_idx != NumGPRs) {
4347 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
4348 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004349 false, false, false, 0);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004350 MemOpChains.push_back(Load.getValue(1));
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004351 RegsToPass.push_back(std::make_pair(GPR[GPR_idx], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004352
4353 ArgOffset += PtrByteSize;
4354 continue;
4355 }
4356 }
4357
4358 if (GPR_idx == NumGPRs && Size < 8) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004359 SDValue AddPtr = PtrOff;
4360 if (!isLittleEndian) {
4361 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4362 PtrOff.getValueType());
4363 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4364 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004365 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4366 CallSeqStart,
4367 Flags, DAG, dl);
4368 ArgOffset += PtrByteSize;
4369 continue;
4370 }
4371 // Copy entire object into memory. There are cases where gcc-generated
4372 // code assumes it is there, even if it could be put entirely into
4373 // registers. (This is not what the doc says.)
4374
4375 // FIXME: The above statement is likely due to a misunderstanding of the
4376 // documents. All arguments must be copied into the parameter area BY
4377 // THE CALLEE in the event that the callee takes the address of any
4378 // formal argument. That has not yet been implemented. However, it is
4379 // reasonable to use the stack area as a staging area for the register
4380 // load.
4381
4382 // Skip this for small aggregates, as we will use the same slot for a
4383 // right-justified copy, below.
4384 if (Size >= 8)
4385 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4386 CallSeqStart,
4387 Flags, DAG, dl);
4388
4389 // When a register is available, pass a small aggregate right-justified.
4390 if (Size < 8 && GPR_idx != NumGPRs) {
4391 // The easiest way to get this right-justified in a register
4392 // is to copy the structure into the rightmost portion of a
4393 // local variable slot, then load the whole slot into the
4394 // register.
4395 // FIXME: The memcpy seems to produce pretty awful code for
4396 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00004397 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00004398 // parameter save area instead of a new local variable.
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004399 SDValue AddPtr = PtrOff;
4400 if (!isLittleEndian) {
4401 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
4402 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4403 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004404 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4405 CallSeqStart,
4406 Flags, DAG, dl);
4407
4408 // Load the slot into the register.
4409 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
4410 MachinePointerInfo(),
4411 false, false, false, 0);
4412 MemOpChains.push_back(Load.getValue(1));
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004413 RegsToPass.push_back(std::make_pair(GPR[GPR_idx], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004414
4415 // Done with this argument.
4416 ArgOffset += PtrByteSize;
4417 continue;
4418 }
4419
4420 // For aggregates larger than PtrByteSize, copy the pieces of the
4421 // object that fit into registers from the parameter save area.
4422 for (unsigned j=0; j<Size; j+=PtrByteSize) {
4423 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
4424 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
4425 if (GPR_idx != NumGPRs) {
4426 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4427 MachinePointerInfo(),
4428 false, false, false, 0);
4429 MemOpChains.push_back(Load.getValue(1));
4430 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4431 ArgOffset += PtrByteSize;
4432 } else {
4433 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4434 break;
4435 }
4436 }
4437 continue;
4438 }
4439
Craig Topper56710102013-08-15 02:33:50 +00004440 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004441 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00004442 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004443 case MVT::i32:
4444 case MVT::i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004445 // These can be scalar arguments or elements of an integer array type
4446 // passed directly. Clang may use those instead of "byval" aggregate
4447 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidt57d6de52012-10-23 15:51:16 +00004448 if (GPR_idx != NumGPRs) {
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004449 RegsToPass.push_back(std::make_pair(GPR[GPR_idx], Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004450 } else {
4451 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4452 true, isTailCall, false, MemOpChains,
4453 TailCallArguments, dl);
4454 }
4455 ArgOffset += PtrByteSize;
4456 break;
4457 case MVT::f32:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004458 case MVT::f64: {
4459 // These can be scalar arguments or elements of a float array type
4460 // passed directly. The latter are used to implement ELFv2 homogenous
4461 // float aggregates.
4462
4463 // Named arguments go into FPRs first, and once they overflow, the
4464 // remaining arguments go into GPRs and then the parameter save area.
4465 // Unnamed arguments for vararg functions always go to GPRs and
4466 // then the parameter save area. For now, put all arguments to vararg
4467 // routines always in both locations (FPR *and* GPR or stack slot).
4468 bool NeedGPROrStack = isVarArg || FPR_idx == NumFPRs;
4469
4470 // First load the argument into the next available FPR.
4471 if (FPR_idx != NumFPRs)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004472 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4473
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004474 // Next, load the argument into GPR or stack slot if needed.
4475 if (!NeedGPROrStack)
4476 ;
4477 else if (GPR_idx != NumGPRs) {
4478 // In the non-vararg case, this can only ever happen in the
4479 // presence of f32 array types, since otherwise we never run
4480 // out of FPRs before running out of GPRs.
4481 SDValue ArgVal;
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004482
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004483 // Double values are always passed in a single GPR.
4484 if (Arg.getValueType() != MVT::f32) {
4485 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004486
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004487 // Non-array float values are extended and passed in a GPR.
4488 } else if (!Flags.isInConsecutiveRegs()) {
4489 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4490 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
4491
4492 // If we have an array of floats, we collect every odd element
4493 // together with its predecessor into one GPR.
4494 } else if (ArgOffset % PtrByteSize != 0) {
4495 SDValue Lo, Hi;
4496 Lo = DAG.getNode(ISD::BITCAST, dl, MVT::i32, OutVals[i - 1]);
4497 Hi = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4498 if (!isLittleEndian)
4499 std::swap(Lo, Hi);
4500 ArgVal = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
4501
4502 // The final element, if even, goes into the first half of a GPR.
4503 } else if (Flags.isInConsecutiveRegsLast()) {
4504 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4505 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
4506 if (!isLittleEndian)
4507 ArgVal = DAG.getNode(ISD::SHL, dl, MVT::i64, ArgVal,
4508 DAG.getConstant(32, MVT::i32));
4509
4510 // Non-final even elements are skipped; they will be handled
4511 // together the with subsequent argument on the next go-around.
4512 } else
4513 ArgVal = SDValue();
4514
4515 if (ArgVal.getNode())
4516 RegsToPass.push_back(std::make_pair(GPR[GPR_idx], ArgVal));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004517 } else {
4518 // Single-precision floating-point values are mapped to the
4519 // second (rightmost) word of the stack doubleword.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004520 if (Arg.getValueType() == MVT::f32 &&
4521 !isLittleEndian && !Flags.isInConsecutiveRegs()) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004522 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4523 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4524 }
4525
4526 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4527 true, isTailCall, false, MemOpChains,
4528 TailCallArguments, dl);
4529 }
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004530 // When passing an array of floats, the array occupies consecutive
4531 // space in the argument area; only round up to the next doubleword
4532 // at the end of the array. Otherwise, each float takes 8 bytes.
4533 ArgOffset += (Arg.getValueType() == MVT::f32 &&
4534 Flags.isInConsecutiveRegs()) ? 4 : 8;
4535 if (Flags.isInConsecutiveRegsLast())
4536 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004537 break;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004538 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004539 case MVT::v4f32:
4540 case MVT::v4i32:
4541 case MVT::v8i16:
4542 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00004543 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00004544 case MVT::v2i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004545 // These can be scalar arguments or elements of a vector array type
4546 // passed directly. The latter are used to implement ELFv2 homogenous
4547 // vector aggregates.
4548
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00004549 // For a varargs call, named arguments go into VRs or on the stack as
4550 // usual; unnamed arguments always go to the stack or the corresponding
4551 // GPRs when within range. For now, we always put the value in both
4552 // locations (or even all three).
Bill Schmidt57d6de52012-10-23 15:51:16 +00004553 if (isVarArg) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004554 // We could elide this store in the case where the object fits
4555 // entirely in R registers. Maybe later.
Bill Schmidt57d6de52012-10-23 15:51:16 +00004556 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4557 MachinePointerInfo(), false, false, 0);
4558 MemOpChains.push_back(Store);
4559 if (VR_idx != NumVRs) {
4560 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4561 MachinePointerInfo(),
4562 false, false, false, 0);
4563 MemOpChains.push_back(Load.getValue(1));
Hal Finkel7811c612014-03-28 19:58:11 +00004564
4565 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4566 Arg.getSimpleValueType() == MVT::v2i64) ?
4567 VSRH[VR_idx] : VR[VR_idx];
4568 ++VR_idx;
4569
4570 RegsToPass.push_back(std::make_pair(VReg, Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004571 }
4572 ArgOffset += 16;
4573 for (unsigned i=0; i<16; i+=PtrByteSize) {
4574 if (GPR_idx == NumGPRs)
4575 break;
4576 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4577 DAG.getConstant(i, PtrVT));
4578 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4579 false, false, false, 0);
4580 MemOpChains.push_back(Load.getValue(1));
4581 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4582 }
4583 break;
4584 }
4585
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00004586 // Non-varargs Altivec params go into VRs or on the stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00004587 if (VR_idx != NumVRs) {
Hal Finkel7811c612014-03-28 19:58:11 +00004588 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4589 Arg.getSimpleValueType() == MVT::v2i64) ?
4590 VSRH[VR_idx] : VR[VR_idx];
4591 ++VR_idx;
4592
4593 RegsToPass.push_back(std::make_pair(VReg, Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004594 } else {
4595 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4596 true, isTailCall, true, MemOpChains,
4597 TailCallArguments, dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004598 }
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00004599 ArgOffset += 16;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004600 break;
4601 }
4602 }
4603
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004604 assert(NumBytesActuallyUsed == ArgOffset);
Ulrich Weigandde8641b2014-07-07 19:39:44 +00004605 (void)NumBytesActuallyUsed;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004606
Bill Schmidt57d6de52012-10-23 15:51:16 +00004607 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004608 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004609
4610 // Check if this is an indirect call (MTCTR/BCTRL).
4611 // See PrepareCall() for more information about calls through function
4612 // pointers in the 64-bit SVR4 ABI.
4613 if (!isTailCall &&
4614 !dyn_cast<GlobalAddressSDNode>(Callee) &&
Ulrich Weigand9aa09ef2014-06-18 16:14:04 +00004615 !dyn_cast<ExternalSymbolSDNode>(Callee)) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004616 // Load r2 into a virtual register and store it to the TOC save area.
4617 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4618 // TOC save area offset.
Ulrich Weigand8658f172014-07-20 23:43:15 +00004619 unsigned TOCSaveOffset = PPCFrameLowering::getTOCSaveOffset(isELFv2ABI);
Ulrich Weigandad0cb912014-06-18 17:52:49 +00004620 SDValue PtrOff = DAG.getIntPtrConstant(TOCSaveOffset);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004621 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4622 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4623 false, false, 0);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004624 // In the ELFv2 ABI, R12 must contain the address of an indirect callee.
4625 // This does not mean the MTCTR instruction must use R12; it's easier
4626 // to model this as an extra parameter, so do that.
4627 if (isELFv2ABI)
4628 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004629 }
4630
4631 // Build a sequence of copy-to-reg nodes chained together with token chain
4632 // and flag operands which copy the outgoing args into the appropriate regs.
4633 SDValue InFlag;
4634 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4635 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4636 RegsToPass[i].second, InFlag);
4637 InFlag = Chain.getValue(1);
4638 }
4639
4640 if (isTailCall)
4641 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4642 FPOp, true, TailCallArguments);
4643
4644 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4645 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4646 Ins, InVals);
4647}
4648
4649SDValue
4650PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4651 CallingConv::ID CallConv, bool isVarArg,
4652 bool isTailCall,
4653 const SmallVectorImpl<ISD::OutputArg> &Outs,
4654 const SmallVectorImpl<SDValue> &OutVals,
4655 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004656 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004657 SmallVectorImpl<SDValue> &InVals) const {
4658
4659 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004660
Owen Anderson53aa7a92009-08-10 22:56:29 +00004661 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00004662 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004663 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004664
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004665 MachineFunction &MF = DAG.getMachineFunction();
4666
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004667 // Mark this function as potentially containing a function that contains a
4668 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4669 // and restoring the callers stack pointer in this functions epilog. This is
4670 // done because by tail calling the called function might overwrite the value
4671 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004672 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4673 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004674 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4675
Chris Lattneraa40ec12006-05-16 22:56:08 +00004676 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00004677 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00004678 // prereserved space for [SP][CR][LR][3 x unused].
Ulrich Weigand8658f172014-07-20 23:43:15 +00004679 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(isPPC64, true,
4680 false);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004681 unsigned NumBytes = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004682
4683 // Add up all the space actually used.
4684 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
4685 // they all go in registers, but we must reserve stack space for them for
4686 // possible use by the caller. In varargs or 64-bit calls, parameters are
4687 // assigned stack space in order, with padding so Altivec parameters are
4688 // 16-byte aligned.
4689 unsigned nAltivecParamsAtEnd = 0;
4690 for (unsigned i = 0; i != NumOps; ++i) {
4691 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4692 EVT ArgVT = Outs[i].VT;
4693 // Varargs Altivec parameters are padded to a 16 byte boundary.
4694 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
4695 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
4696 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64) {
4697 if (!isVarArg && !isPPC64) {
4698 // Non-varargs Altivec parameters go after all the non-Altivec
4699 // parameters; handle those later so we know how much padding we need.
4700 nAltivecParamsAtEnd++;
4701 continue;
4702 }
4703 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
4704 NumBytes = ((NumBytes+15)/16)*16;
4705 }
4706 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
4707 }
4708
4709 // Allow for Altivec parameters at the end, if needed.
4710 if (nAltivecParamsAtEnd) {
4711 NumBytes = ((NumBytes+15)/16)*16;
4712 NumBytes += 16*nAltivecParamsAtEnd;
4713 }
4714
4715 // The prolog code of the callee may store up to 8 GPR argument registers to
4716 // the stack, allowing va_start to index over them in memory if its varargs.
4717 // Because we cannot tell if this is needed on the caller side, we have to
4718 // conservatively assume that it is needed. As such, make sure we have at
4719 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004720 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004721
4722 // Tail call needs the stack to be aligned.
4723 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4724 CallConv == CallingConv::Fast)
4725 NumBytes = EnsureStackAlignment(MF.getTarget(), NumBytes);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004726
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004727 // Calculate by how many bytes the stack has to be adjusted in case of tail
4728 // call optimization.
4729 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004730
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004731 // To protect arguments on the stack from being clobbered in a tail call,
4732 // force all the loads to happen before doing any other lowering.
4733 if (isTailCall)
4734 Chain = DAG.getStackArgumentTokenFactor(Chain);
4735
Chris Lattnerb7552a82006-05-17 00:15:40 +00004736 // Adjust the stack pointer for the new arguments...
4737 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004738 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4739 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004740 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004741
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004742 // Load the return address and frame pointer so it can be move somewhere else
4743 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004744 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004745 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4746 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004747
Chris Lattnerb7552a82006-05-17 00:15:40 +00004748 // Set up a copy of the stack pointer for use loading and storing any
4749 // arguments that may not fit in the registers available for argument
4750 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004751 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004752 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004753 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004754 else
Owen Anderson9f944592009-08-11 20:47:22 +00004755 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004756
Chris Lattnerb7552a82006-05-17 00:15:40 +00004757 // Figure out which arguments are going to go in registers, and which in
4758 // memory. Also, if this is a vararg function, floating point operations
4759 // must be stored to our stack, and loaded into integer regs as well, if
4760 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004761 unsigned ArgOffset = LinkageSize;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004762 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004763
Craig Topper840beec2014-04-04 05:16:06 +00004764 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004765 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4766 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4767 };
Craig Topper840beec2014-04-04 05:16:06 +00004768 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00004769 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4770 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4771 };
Craig Topper840beec2014-04-04 05:16:06 +00004772 static const MCPhysReg *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004773
Craig Topper840beec2014-04-04 05:16:06 +00004774 static const MCPhysReg VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004775 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4776 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4777 };
Owen Andersone2f23a32007-09-07 04:06:50 +00004778 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004779 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00004780 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004781
Craig Topper840beec2014-04-04 05:16:06 +00004782 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004783
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004784 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004785 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4786
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004787 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00004788 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004789 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004790 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004791
Chris Lattnerb7552a82006-05-17 00:15:40 +00004792 // PtrOff will be used to store the current argument to the stack if a
4793 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004794 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004795
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004796 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004797
Dale Johannesen679073b2009-02-04 02:34:38 +00004798 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004799
4800 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00004801 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00004802 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4803 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00004804 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004805 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00004806
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004807 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004808 // Note: "by value" is code for passing a structure by value, not
4809 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00004810 if (Flags.isByVal()) {
4811 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004812 // Very small objects are passed right-justified. Everything else is
4813 // passed left-justified.
4814 if (Size==1 || Size==2) {
4815 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004816 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00004817 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00004818 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004819 false, false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004820 MemOpChains.push_back(Load.getValue(1));
4821 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004822
4823 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004824 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00004825 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4826 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004827 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004828 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4829 CallSeqStart,
4830 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004831 ArgOffset += PtrByteSize;
4832 }
4833 continue;
4834 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004835 // Copy entire object into memory. There are cases where gcc-generated
4836 // code assumes it is there, even if it could be put entirely into
4837 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004838 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4839 CallSeqStart,
4840 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004841
4842 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4843 // copy the pieces of the object that fit into registers from the
4844 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00004845 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004846 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004847 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00004848 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004849 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4850 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004851 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00004852 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00004853 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004854 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004855 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004856 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004857 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004858 }
4859 }
4860 continue;
4861 }
4862
Craig Topper56710102013-08-15 02:33:50 +00004863 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004864 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00004865 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00004866 case MVT::i32:
4867 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004868 if (GPR_idx != NumGPRs) {
Hal Finkel7f908e82014-03-06 00:45:19 +00004869 if (Arg.getValueType() == MVT::i1)
4870 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg);
4871
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004872 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004873 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004874 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4875 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004876 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00004877 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004878 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004879 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004880 case MVT::f32:
4881 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004882 if (FPR_idx != NumFPRs) {
4883 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4884
Chris Lattnerb7552a82006-05-17 00:15:40 +00004885 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00004886 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4887 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004888 MemOpChains.push_back(Store);
4889
Chris Lattnerb7552a82006-05-17 00:15:40 +00004890 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004891 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004892 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004893 MachinePointerInfo(), false, false,
4894 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004895 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004896 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004897 }
Owen Anderson9f944592009-08-11 20:47:22 +00004898 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004899 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004900 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00004901 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4902 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004903 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004904 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004905 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00004906 }
4907 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00004908 // If we have any FPRs remaining, we may also have GPRs remaining.
4909 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4910 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004911 if (GPR_idx != NumGPRs)
4912 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00004913 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004914 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4915 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004916 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004917 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004918 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4919 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004920 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004921 if (isPPC64)
4922 ArgOffset += 8;
4923 else
Owen Anderson9f944592009-08-11 20:47:22 +00004924 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004925 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004926 case MVT::v4f32:
4927 case MVT::v4i32:
4928 case MVT::v8i16:
4929 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00004930 if (isVarArg) {
4931 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00004932 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00004933 // V registers; in fact gcc does this only for arguments that are
4934 // prototyped, not for those that match the ... We do it for all
4935 // arguments, seems to work.
4936 while (ArgOffset % 16 !=0) {
4937 ArgOffset += PtrByteSize;
4938 if (GPR_idx != NumGPRs)
4939 GPR_idx++;
4940 }
4941 // We could elide this store in the case where the object fits
4942 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004943 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004944 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00004945 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4946 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004947 MemOpChains.push_back(Store);
4948 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004949 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00004950 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004951 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004952 MemOpChains.push_back(Load.getValue(1));
4953 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4954 }
4955 ArgOffset += 16;
4956 for (unsigned i=0; i<16; i+=PtrByteSize) {
4957 if (GPR_idx == NumGPRs)
4958 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00004959 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004960 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00004961 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004962 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004963 MemOpChains.push_back(Load.getValue(1));
4964 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4965 }
4966 break;
4967 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004968
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004969 // Non-varargs Altivec params generally go in registers, but have
4970 // stack space allocated at the end.
4971 if (VR_idx != NumVRs) {
4972 // Doesn't have GPR space allocated.
4973 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4974 } else if (nAltivecParamsAtEnd==0) {
4975 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004976 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4977 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004978 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004979 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00004980 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00004981 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004982 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00004983 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004984 // If all Altivec parameters fit in registers, as they usually do,
4985 // they get stack space following the non-Altivec parameters. We
4986 // don't track this here because nobody below needs it.
4987 // If there are more Altivec parameters than fit in registers emit
4988 // the stores here.
4989 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4990 unsigned j = 0;
4991 // Offset is aligned; skip 1st 12 params which go in V registers.
4992 ArgOffset = ((ArgOffset+15)/16)*16;
4993 ArgOffset += 12*16;
4994 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004995 SDValue Arg = OutVals[i];
4996 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00004997 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4998 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004999 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005000 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005001 // We are emitting Altivec params in order.
5002 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5003 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005004 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005005 ArgOffset += 16;
5006 }
5007 }
5008 }
5009 }
5010
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005011 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00005012 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005013
Dale Johannesen90eab672010-03-09 20:15:42 +00005014 // On Darwin, R12 must contain the address of an indirect callee. This does
5015 // not mean the MTCTR instruction must use R12; it's easier to model this as
5016 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00005017 if (!isTailCall &&
Dale Johannesen90eab672010-03-09 20:15:42 +00005018 !dyn_cast<GlobalAddressSDNode>(Callee) &&
5019 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
5020 !isBLACompatibleAddress(Callee, DAG))
5021 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
5022 PPC::R12), Callee));
5023
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005024 // Build a sequence of copy-to-reg nodes chained together with token chain
5025 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005026 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005027 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00005028 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00005029 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005030 InFlag = Chain.getValue(1);
5031 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005032
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00005033 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005034 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
5035 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005036
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005037 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
5038 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
5039 Ins, InVals);
Chris Lattneraa40ec12006-05-16 22:56:08 +00005040}
5041
Hal Finkel450128a2011-10-14 19:51:36 +00005042bool
5043PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
5044 MachineFunction &MF, bool isVarArg,
5045 const SmallVectorImpl<ISD::OutputArg> &Outs,
5046 LLVMContext &Context) const {
5047 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00005048 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Hal Finkel450128a2011-10-14 19:51:36 +00005049 return CCInfo.CheckReturn(Outs, RetCC_PPC);
5050}
5051
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005052SDValue
5053PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00005054 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005055 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005056 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005057 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005058
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005059 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00005060 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
5061 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005062 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005063
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005064 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005065 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005066
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005067 // Copy the result values into the output registers.
5068 for (unsigned i = 0; i != RVLocs.size(); ++i) {
5069 CCValAssign &VA = RVLocs[i];
5070 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00005071
5072 SDValue Arg = OutVals[i];
5073
5074 switch (VA.getLocInfo()) {
5075 default: llvm_unreachable("Unknown loc info!");
5076 case CCValAssign::Full: break;
5077 case CCValAssign::AExt:
5078 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
5079 break;
5080 case CCValAssign::ZExt:
5081 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
5082 break;
5083 case CCValAssign::SExt:
5084 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
5085 break;
5086 }
5087
5088 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005089 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005090 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005091 }
5092
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005093 RetOps[0] = Chain; // Update chain.
5094
5095 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005096 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005097 RetOps.push_back(Flag);
5098
Craig Topper48d114b2014-04-26 18:35:24 +00005099 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps);
Chris Lattner4211ca92006-04-14 06:01:58 +00005100}
5101
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005102SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005103 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00005104 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005105 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005106
Jim Laskeye4f4d042006-12-04 22:04:42 +00005107 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005108 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00005109
5110 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00005111 bool isPPC64 = Subtarget.isPPC64();
5112 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005113 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00005114
5115 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005116 SDValue Chain = Op.getOperand(0);
5117 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005118
Jim Laskeye4f4d042006-12-04 22:04:42 +00005119 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00005120 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
5121 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005122 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005123
Jim Laskeye4f4d042006-12-04 22:04:42 +00005124 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00005125 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005126
Jim Laskeye4f4d042006-12-04 22:04:42 +00005127 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00005128 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005129 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00005130}
5131
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005132
5133
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005134SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005135PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00005136 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005137 bool isPPC64 = Subtarget.isPPC64();
5138 bool isDarwinABI = Subtarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005139 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005140
5141 // Get current frame pointer save index. The users of this index will be
5142 // primarily DYNALLOC instructions.
5143 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
5144 int RASI = FI->getReturnAddrSaveIndex();
5145
5146 // If the frame pointer save index hasn't been defined yet.
5147 if (!RASI) {
5148 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00005149 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005150 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00005151 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005152 // Save the result.
5153 FI->setReturnAddrSaveIndex(RASI);
5154 }
5155 return DAG.getFrameIndex(RASI, PtrVT);
5156}
5157
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005158SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005159PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
5160 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005161 bool isPPC64 = Subtarget.isPPC64();
5162 bool isDarwinABI = Subtarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005163 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00005164
5165 // Get current frame pointer save index. The users of this index will be
5166 // primarily DYNALLOC instructions.
5167 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
5168 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005169
Jim Laskey48850c12006-11-16 22:43:37 +00005170 // If the frame pointer save index hasn't been defined yet.
5171 if (!FPSI) {
5172 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00005173 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005174 isDarwinABI);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005175
Jim Laskey48850c12006-11-16 22:43:37 +00005176 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00005177 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00005178 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005179 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00005180 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005181 return DAG.getFrameIndex(FPSI, PtrVT);
5182}
Jim Laskey48850c12006-11-16 22:43:37 +00005183
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005184SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005185 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005186 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00005187 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005188 SDValue Chain = Op.getOperand(0);
5189 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005190 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005191
Jim Laskey48850c12006-11-16 22:43:37 +00005192 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005193 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00005194 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005195 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00005196 DAG.getConstant(0, PtrVT), Size);
5197 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005198 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00005199 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005200 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00005201 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Craig Topper48d114b2014-04-26 18:35:24 +00005202 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops);
Jim Laskey48850c12006-11-16 22:43:37 +00005203}
5204
Hal Finkel756810f2013-03-21 21:37:52 +00005205SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
5206 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005207 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00005208 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
5209 DAG.getVTList(MVT::i32, MVT::Other),
5210 Op.getOperand(0), Op.getOperand(1));
5211}
5212
5213SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
5214 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005215 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00005216 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
5217 Op.getOperand(0), Op.getOperand(1));
5218}
5219
Hal Finkel940ab932014-02-28 00:27:01 +00005220SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
5221 assert(Op.getValueType() == MVT::i1 &&
5222 "Custom lowering only for i1 loads");
5223
5224 // First, load 8 bits into 32 bits, then truncate to 1 bit.
5225
5226 SDLoc dl(Op);
5227 LoadSDNode *LD = cast<LoadSDNode>(Op);
5228
5229 SDValue Chain = LD->getChain();
5230 SDValue BasePtr = LD->getBasePtr();
5231 MachineMemOperand *MMO = LD->getMemOperand();
5232
5233 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(), Chain,
5234 BasePtr, MVT::i8, MMO);
5235 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
5236
5237 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00005238 return DAG.getMergeValues(Ops, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00005239}
5240
5241SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
5242 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
5243 "Custom lowering only for i1 stores");
5244
5245 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
5246
5247 SDLoc dl(Op);
5248 StoreSDNode *ST = cast<StoreSDNode>(Op);
5249
5250 SDValue Chain = ST->getChain();
5251 SDValue BasePtr = ST->getBasePtr();
5252 SDValue Value = ST->getValue();
5253 MachineMemOperand *MMO = ST->getMemOperand();
5254
5255 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(), Value);
5256 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
5257}
5258
5259// FIXME: Remove this once the ANDI glue bug is fixed:
5260SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
5261 assert(Op.getValueType() == MVT::i1 &&
5262 "Custom lowering only for i1 results");
5263
5264 SDLoc DL(Op);
5265 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
5266 Op.getOperand(0));
5267}
5268
Chris Lattner4211ca92006-04-14 06:01:58 +00005269/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
5270/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005271SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00005272 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00005273 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
5274 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00005275 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005276
Hal Finkel81f87992013-04-07 22:11:09 +00005277 // We might be able to do better than this under some circumstances, but in
5278 // general, fsel-based lowering of select is a finite-math-only optimization.
5279 // For more information, see section F.3 of the 2.06 ISA specification.
5280 if (!DAG.getTarget().Options.NoInfsFPMath ||
5281 !DAG.getTarget().Options.NoNaNsFPMath)
5282 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005283
Hal Finkel81f87992013-04-07 22:11:09 +00005284 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005285
Owen Anderson53aa7a92009-08-10 22:56:29 +00005286 EVT ResVT = Op.getValueType();
5287 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005288 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
5289 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005290 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005291
Chris Lattner4211ca92006-04-14 06:01:58 +00005292 // If the RHS of the comparison is a 0.0, we don't need to do the
5293 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00005294 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00005295 if (isFloatingPointZero(RHS))
5296 switch (CC) {
5297 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00005298 case ISD::SETNE:
5299 std::swap(TV, FV);
5300 case ISD::SETEQ:
5301 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5302 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
5303 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
5304 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
5305 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
5306 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5307 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005308 case ISD::SETULT:
5309 case ISD::SETLT:
5310 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005311 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005312 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00005313 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5314 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005315 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005316 case ISD::SETUGT:
5317 case ISD::SETGT:
5318 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005319 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005320 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00005321 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5322 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005323 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005324 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005325 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005326
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005327 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00005328 switch (CC) {
5329 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00005330 case ISD::SETNE:
5331 std::swap(TV, FV);
5332 case ISD::SETEQ:
5333 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
5334 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5335 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
5336 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
5337 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
5338 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
5339 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5340 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005341 case ISD::SETULT:
5342 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005343 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005344 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5345 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005346 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005347 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005348 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005349 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005350 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5351 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005352 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005353 case ISD::SETUGT:
5354 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005355 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005356 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5357 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005358 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005359 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005360 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005361 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005362 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5363 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005364 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005365 }
Eli Friedman5806e182009-05-28 04:31:08 +00005366 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00005367}
5368
Chris Lattner57ee7c62007-11-28 18:44:47 +00005369// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005370SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005371 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00005372 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005373 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00005374 if (Src.getValueType() == MVT::f32)
5375 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00005376
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005377 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00005378 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005379 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00005380 case MVT::i32:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005381 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005382 (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ :
Hal Finkelf6d45f22013-04-01 17:52:07 +00005383 PPCISD::FCTIDZ),
Owen Anderson9f944592009-08-11 20:47:22 +00005384 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005385 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005386 case MVT::i64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005387 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) &&
Hal Finkel3f88d082013-04-01 18:42:58 +00005388 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00005389 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
5390 PPCISD::FCTIDUZ,
5391 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005392 break;
5393 }
Duncan Sands2a287912008-07-19 16:26:02 +00005394
Chris Lattner4211ca92006-04-14 06:01:58 +00005395 // Convert the FP value to an int value through memory.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005396 bool i32Stack = Op.getValueType() == MVT::i32 && Subtarget.hasSTFIWX() &&
5397 (Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT());
Hal Finkelf6d45f22013-04-01 17:52:07 +00005398 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
5399 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
5400 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00005401
Chris Lattner06a49542007-10-15 20:14:52 +00005402 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005403 SDValue Chain;
5404 if (i32Stack) {
5405 MachineFunction &MF = DAG.getMachineFunction();
5406 MachineMemOperand *MMO =
5407 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
5408 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
5409 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00005410 DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005411 } else
5412 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
5413 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00005414
5415 // Result is a load from the stack slot. If loading 4 bytes, make sure to
5416 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005417 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00005418 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00005419 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkelf6d45f22013-04-01 17:52:07 +00005420 MPI = MachinePointerInfo();
5421 }
5422
5423 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooper82cd9e82011-11-08 18:42:53 +00005424 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005425}
5426
Hal Finkelf6d45f22013-04-01 17:52:07 +00005427SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005428 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005429 SDLoc dl(Op);
Dan Gohmand6819da2008-03-11 01:59:03 +00005430 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00005431 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005432 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00005433
Hal Finkel6a56b212014-03-05 22:14:00 +00005434 if (Op.getOperand(0).getValueType() == MVT::i1)
5435 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0),
5436 DAG.getConstantFP(1.0, Op.getValueType()),
5437 DAG.getConstantFP(0.0, Op.getValueType()));
5438
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005439 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005440 "UINT_TO_FP is supported only with FPCVT");
5441
5442 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00005443 // Otherwise, convert to double-precision and then round.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005444 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
Hal Finkelf6d45f22013-04-01 17:52:07 +00005445 (Op.getOpcode() == ISD::UINT_TO_FP ?
5446 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
5447 (Op.getOpcode() == ISD::UINT_TO_FP ?
5448 PPCISD::FCFIDU : PPCISD::FCFID);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005449 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
Hal Finkelf6d45f22013-04-01 17:52:07 +00005450 MVT::f32 : MVT::f64;
5451
Owen Anderson9f944592009-08-11 20:47:22 +00005452 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005453 SDValue SINT = Op.getOperand(0);
5454 // When converting to single-precision, we actually need to convert
5455 // to double-precision first and then round to single-precision.
5456 // To avoid double-rounding effects during that operation, we have
5457 // to prepare the input operand. Bits that might be truncated when
5458 // converting to double-precision are replaced by a bit that won't
5459 // be lost at this stage, but is below the single-precision rounding
5460 // position.
5461 //
5462 // However, if -enable-unsafe-fp-math is in effect, accept double
5463 // rounding to avoid the extra overhead.
5464 if (Op.getValueType() == MVT::f32 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005465 !Subtarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005466 !DAG.getTarget().Options.UnsafeFPMath) {
5467
5468 // Twiddle input to make sure the low 11 bits are zero. (If this
5469 // is the case, we are guaranteed the value will fit into the 53 bit
5470 // mantissa of an IEEE double-precision value without rounding.)
5471 // If any of those low 11 bits were not zero originally, make sure
5472 // bit 12 (value 2048) is set instead, so that the final rounding
5473 // to single-precision gets the correct result.
5474 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5475 SINT, DAG.getConstant(2047, MVT::i64));
5476 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
5477 Round, DAG.getConstant(2047, MVT::i64));
5478 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
5479 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5480 Round, DAG.getConstant(-2048, MVT::i64));
5481
5482 // However, we cannot use that value unconditionally: if the magnitude
5483 // of the input value is small, the bit-twiddling we did above might
5484 // end up visibly changing the output. Fortunately, in that case, we
5485 // don't need to twiddle bits since the original input will convert
5486 // exactly to double-precision floating-point already. Therefore,
5487 // construct a conditional to use the original value if the top 11
5488 // bits are all sign-bit copies, and use the rounded value computed
5489 // above otherwise.
5490 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
5491 SINT, DAG.getConstant(53, MVT::i32));
5492 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
5493 Cond, DAG.getConstant(1, MVT::i64));
5494 Cond = DAG.getSetCC(dl, MVT::i32,
5495 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
5496
5497 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
5498 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00005499
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005500 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005501 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
5502
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005503 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00005504 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005505 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005506 return FP;
5507 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005508
Owen Anderson9f944592009-08-11 20:47:22 +00005509 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005510 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005511 // Since we only generate this in 64-bit mode, we can take advantage of
5512 // 64-bit registers. In particular, sign extend the input value into the
5513 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
5514 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00005515 MachineFunction &MF = DAG.getMachineFunction();
5516 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005517 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005518
Hal Finkelbeb296b2013-03-31 10:12:51 +00005519 SDValue Ld;
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005520 if (Subtarget.hasLFIWAX() || Subtarget.hasFPCVT()) {
Hal Finkelbeb296b2013-03-31 10:12:51 +00005521 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
5522 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005523
Hal Finkelbeb296b2013-03-31 10:12:51 +00005524 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
5525 MachinePointerInfo::getFixedStack(FrameIdx),
5526 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00005527
Hal Finkelbeb296b2013-03-31 10:12:51 +00005528 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
5529 "Expected an i32 store");
5530 MachineMemOperand *MMO =
5531 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
5532 MachineMemOperand::MOLoad, 4, 4);
5533 SDValue Ops[] = { Store, FIdx };
Hal Finkelf6d45f22013-04-01 17:52:07 +00005534 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
5535 PPCISD::LFIWZX : PPCISD::LFIWAX,
5536 dl, DAG.getVTList(MVT::f64, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00005537 Ops, MVT::i32, MMO);
Hal Finkelbeb296b2013-03-31 10:12:51 +00005538 } else {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005539 assert(Subtarget.isPPC64() &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005540 "i32->FP without LFIWAX supported only on PPC64");
5541
Hal Finkelbeb296b2013-03-31 10:12:51 +00005542 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
5543 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
5544
5545 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
5546 Op.getOperand(0));
5547
5548 // STD the extended value into the stack slot.
5549 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
5550 MachinePointerInfo::getFixedStack(FrameIdx),
5551 false, false, 0);
5552
5553 // Load the value as a double.
5554 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
5555 MachinePointerInfo::getFixedStack(FrameIdx),
5556 false, false, false, 0);
5557 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005558
Chris Lattner4211ca92006-04-14 06:01:58 +00005559 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005560 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005561 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00005562 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005563 return FP;
5564}
5565
Dan Gohman21cea8a2010-04-17 15:26:15 +00005566SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
5567 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005568 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005569 /*
5570 The rounding mode is in bits 30:31 of FPSR, and has the following
5571 settings:
5572 00 Round to nearest
5573 01 Round to 0
5574 10 Round to +inf
5575 11 Round to -inf
5576
5577 FLT_ROUNDS, on the other hand, expects the following:
5578 -1 Undefined
5579 0 Round to 0
5580 1 Round to nearest
5581 2 Round to +inf
5582 3 Round to -inf
5583
5584 To perform the conversion, we do:
5585 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
5586 */
5587
5588 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005589 EVT VT = Op.getValueType();
5590 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005591
5592 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005593 EVT NodeTys[] = {
5594 MVT::f64, // return register
5595 MVT::Glue // unused in this context
5596 };
Craig Topper2d2aa0c2014-04-30 07:17:30 +00005597 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, None);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005598
5599 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00005600 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005601 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005602 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00005603 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005604
5605 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005606 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005607 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00005608 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005609 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005610
5611 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005612 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00005613 DAG.getNode(ISD::AND, dl, MVT::i32,
5614 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005615 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00005616 DAG.getNode(ISD::SRL, dl, MVT::i32,
5617 DAG.getNode(ISD::AND, dl, MVT::i32,
5618 DAG.getNode(ISD::XOR, dl, MVT::i32,
5619 CWD, DAG.getConstant(3, MVT::i32)),
5620 DAG.getConstant(3, MVT::i32)),
5621 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005622
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005623 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00005624 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005625
Duncan Sands13237ac2008-06-06 12:08:01 +00005626 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00005627 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005628}
5629
Dan Gohman21cea8a2010-04-17 15:26:15 +00005630SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005631 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005632 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005633 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00005634 assert(Op.getNumOperands() == 3 &&
5635 VT == Op.getOperand(1).getValueType() &&
5636 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005637
Chris Lattner601b8652006-09-20 03:47:40 +00005638 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005639 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005640 SDValue Lo = Op.getOperand(0);
5641 SDValue Hi = Op.getOperand(1);
5642 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005643 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005644
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005645 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005646 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005647 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5648 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5649 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5650 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005651 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005652 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5653 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5654 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005655 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005656 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005657}
5658
Dan Gohman21cea8a2010-04-17 15:26:15 +00005659SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005660 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005661 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00005662 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005663 assert(Op.getNumOperands() == 3 &&
5664 VT == Op.getOperand(1).getValueType() &&
5665 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005666
Dan Gohman8d2ead22008-03-07 20:36:53 +00005667 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005668 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005669 SDValue Lo = Op.getOperand(0);
5670 SDValue Hi = Op.getOperand(1);
5671 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005672 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005673
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005674 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005675 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005676 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5677 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5678 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5679 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005680 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005681 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5682 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5683 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005684 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005685 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005686}
5687
Dan Gohman21cea8a2010-04-17 15:26:15 +00005688SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005689 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005690 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005691 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005692 assert(Op.getNumOperands() == 3 &&
5693 VT == Op.getOperand(1).getValueType() &&
5694 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005695
Dan Gohman8d2ead22008-03-07 20:36:53 +00005696 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005697 SDValue Lo = Op.getOperand(0);
5698 SDValue Hi = Op.getOperand(1);
5699 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005700 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005701
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005702 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005703 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005704 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5705 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5706 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5707 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005708 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005709 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5710 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5711 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00005712 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005713 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005714 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005715}
5716
5717//===----------------------------------------------------------------------===//
5718// Vector related lowering.
5719//
5720
Chris Lattner2a099c02006-04-17 06:00:21 +00005721/// BuildSplatI - Build a canonical splati of Val with an element size of
5722/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005723static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005724 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00005725 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005726
Owen Anderson53aa7a92009-08-10 22:56:29 +00005727 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00005728 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00005729 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005730
Owen Anderson9f944592009-08-11 20:47:22 +00005731 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005732
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005733 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5734 if (Val == -1)
5735 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005736
Owen Anderson53aa7a92009-08-10 22:56:29 +00005737 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005738
Chris Lattner2a099c02006-04-17 06:00:21 +00005739 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00005740 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005741 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00005742 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Craig Topper48d114b2014-04-26 18:35:24 +00005743 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005744 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005745}
5746
Hal Finkelcf2e9082013-05-24 23:00:14 +00005747/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5748/// specified intrinsic ID.
5749static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005750 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00005751 EVT DestVT = MVT::Other) {
5752 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5753 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5754 DAG.getConstant(IID, MVT::i32), Op);
5755}
5756
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005757/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00005758/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005759static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005760 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005761 EVT DestVT = MVT::Other) {
5762 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005763 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005764 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005765}
5766
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005767/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5768/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005769static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005770 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005771 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00005772 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005773 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005774 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005775}
5776
5777
Chris Lattner264c9082006-04-17 17:55:10 +00005778/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5779/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005780static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005781 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00005782 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00005783 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5784 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005785
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005786 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00005787 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005788 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00005789 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005790 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00005791}
5792
Chris Lattner19e90552006-04-14 05:19:18 +00005793// If this is a case we can't handle, return null and let the default
5794// expansion code take care of it. If we CAN select this case, and if it
5795// selects to a single instruction, return Op. Otherwise, if we can codegen
5796// this case more efficiently than a constant pool load, lower it to the
5797// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005798SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5799 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005800 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005801 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
Craig Toppere73658d2014-04-28 04:05:08 +00005802 assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00005803
Bob Wilson85cefe82009-03-02 23:24:16 +00005804 // Check if this is a splat of a constant value.
5805 APInt APSplatBits, APSplatUndef;
5806 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005807 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00005808 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00005809 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00005810 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00005811
Bob Wilson530e0382009-03-03 19:26:27 +00005812 unsigned SplatBits = APSplatBits.getZExtValue();
5813 unsigned SplatUndef = APSplatUndef.getZExtValue();
5814 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005815
Bob Wilson530e0382009-03-03 19:26:27 +00005816 // First, handle single instruction cases.
5817
5818 // All zeros?
5819 if (SplatBits == 0) {
5820 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00005821 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5822 SDValue Z = DAG.getConstant(0, MVT::i32);
5823 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00005824 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00005825 }
Bob Wilson530e0382009-03-03 19:26:27 +00005826 return Op;
5827 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00005828
Bob Wilson530e0382009-03-03 19:26:27 +00005829 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5830 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5831 (32-SplatBitSize));
5832 if (SextVal >= -16 && SextVal <= 15)
5833 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005834
5835
Bob Wilson530e0382009-03-03 19:26:27 +00005836 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005837
Bob Wilson530e0382009-03-03 19:26:27 +00005838 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00005839 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5840 // If this value is in the range [17,31] and is odd, use:
5841 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5842 // If this value is in the range [-31,-17] and is odd, use:
5843 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5844 // Note the last two are three-instruction sequences.
5845 if (SextVal >= -32 && SextVal <= 31) {
5846 // To avoid having these optimizations undone by constant folding,
5847 // we convert to a pseudo that will be expanded later into one of
5848 // the above forms.
5849 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt71dddd52014-05-27 15:57:51 +00005850 EVT VT = (SplatSize == 1 ? MVT::v16i8 :
5851 (SplatSize == 2 ? MVT::v8i16 : MVT::v4i32));
5852 SDValue EltSize = DAG.getConstant(SplatSize, MVT::i32);
5853 SDValue RetVal = DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
5854 if (VT == Op.getValueType())
5855 return RetVal;
5856 else
5857 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), RetVal);
Bob Wilson530e0382009-03-03 19:26:27 +00005858 }
5859
5860 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5861 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5862 // for fneg/fabs.
5863 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5864 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00005865 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005866
5867 // Make the VSLW intrinsic, computing 0x8000_0000.
5868 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5869 OnesV, DAG, dl);
5870
5871 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00005872 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00005873 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005874 }
5875
Bill Schmidt4aedff82014-06-06 14:06:26 +00005876 // The remaining cases assume either big endian element order or
5877 // a splat-size that equates to the element size of the vector
5878 // to be built. An example that doesn't work for little endian is
5879 // {0, -1, 0, -1, 0, -1, 0, -1} which has a splat size of 32 bits
5880 // and a vector element size of 16 bits. The code below will
5881 // produce the vector in big endian element order, which for little
5882 // endian is {-1, 0, -1, 0, -1, 0, -1, 0}.
5883
5884 // For now, just avoid these optimizations in that case.
5885 // FIXME: Develop correct optimizations for LE with mismatched
5886 // splat and element sizes.
5887
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005888 if (Subtarget.isLittleEndian() &&
Bill Schmidt4aedff82014-06-06 14:06:26 +00005889 SplatSize != Op.getValueType().getVectorElementType().getSizeInBits())
5890 return SDValue();
5891
Bob Wilson530e0382009-03-03 19:26:27 +00005892 // Check to see if this is a wide variety of vsplti*, binop self cases.
5893 static const signed char SplatCsts[] = {
5894 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5895 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5896 };
5897
5898 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5899 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5900 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5901 int i = SplatCsts[idx];
5902
5903 // Figure out what shift amount will be used by altivec if shifted by i in
5904 // this splat size.
5905 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5906
5907 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00005908 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005909 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005910 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5911 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5912 Intrinsic::ppc_altivec_vslw
5913 };
5914 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005915 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005916 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005917
Bob Wilson530e0382009-03-03 19:26:27 +00005918 // vsplti + srl self.
5919 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005920 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005921 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5922 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5923 Intrinsic::ppc_altivec_vsrw
5924 };
5925 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005926 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005927 }
5928
Bob Wilson530e0382009-03-03 19:26:27 +00005929 // vsplti + sra self.
5930 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005931 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005932 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5933 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5934 Intrinsic::ppc_altivec_vsraw
5935 };
5936 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005937 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005938 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005939
Bob Wilson530e0382009-03-03 19:26:27 +00005940 // vsplti + rol self.
5941 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5942 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005943 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005944 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5945 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5946 Intrinsic::ppc_altivec_vrlw
5947 };
5948 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005949 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005950 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005951
Bob Wilson530e0382009-03-03 19:26:27 +00005952 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00005953 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005954 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005955 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00005956 }
Bob Wilson530e0382009-03-03 19:26:27 +00005957 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00005958 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005959 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005960 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00005961 }
Bob Wilson530e0382009-03-03 19:26:27 +00005962 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00005963 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005964 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005965 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5966 }
5967 }
5968
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005969 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00005970}
5971
Chris Lattner071ad012006-04-17 05:28:54 +00005972/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5973/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005974static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005975 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005976 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00005977 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00005978 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00005979 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005980
Chris Lattner071ad012006-04-17 05:28:54 +00005981 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00005982 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 +00005983 OP_VMRGHW,
5984 OP_VMRGLW,
5985 OP_VSPLTISW0,
5986 OP_VSPLTISW1,
5987 OP_VSPLTISW2,
5988 OP_VSPLTISW3,
5989 OP_VSLDOI4,
5990 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00005991 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00005992 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00005993
Chris Lattner071ad012006-04-17 05:28:54 +00005994 if (OpNum == OP_COPY) {
5995 if (LHSID == (1*9+2)*9+3) return LHS;
5996 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5997 return RHS;
5998 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005999
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006000 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006001 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
6002 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006003
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006004 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00006005 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006006 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00006007 case OP_VMRGHW:
6008 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
6009 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
6010 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
6011 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
6012 break;
6013 case OP_VMRGLW:
6014 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
6015 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
6016 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
6017 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
6018 break;
6019 case OP_VSPLTISW0:
6020 for (unsigned i = 0; i != 16; ++i)
6021 ShufIdxs[i] = (i&3)+0;
6022 break;
6023 case OP_VSPLTISW1:
6024 for (unsigned i = 0; i != 16; ++i)
6025 ShufIdxs[i] = (i&3)+4;
6026 break;
6027 case OP_VSPLTISW2:
6028 for (unsigned i = 0; i != 16; ++i)
6029 ShufIdxs[i] = (i&3)+8;
6030 break;
6031 case OP_VSPLTISW3:
6032 for (unsigned i = 0; i != 16; ++i)
6033 ShufIdxs[i] = (i&3)+12;
6034 break;
6035 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006036 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006037 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006038 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006039 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006040 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006041 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00006042 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00006043 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
6044 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00006045 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00006046 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00006047}
6048
Chris Lattner19e90552006-04-14 05:19:18 +00006049/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
6050/// is a shuffle we can handle in a single instruction, return it. Otherwise,
6051/// return the code it can be lowered into. Worst case, it can always be
6052/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006053SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006054 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006055 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006056 SDValue V1 = Op.getOperand(0);
6057 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006058 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006059 EVT VT = Op.getValueType();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006060 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006061
Chris Lattner19e90552006-04-14 05:19:18 +00006062 // Cases that are handled by instructions that take permute immediates
6063 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
6064 // selected by the instruction selector.
6065 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006066 if (PPC::isSplatShuffleMask(SVOp, 1) ||
6067 PPC::isSplatShuffleMask(SVOp, 2) ||
6068 PPC::isSplatShuffleMask(SVOp, 4) ||
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00006069 PPC::isVPKUWUMShuffleMask(SVOp, 1, DAG) ||
6070 PPC::isVPKUHUMShuffleMask(SVOp, 1, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00006071 PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00006072 PPC::isVMRGLShuffleMask(SVOp, 1, 1, DAG) ||
6073 PPC::isVMRGLShuffleMask(SVOp, 2, 1, DAG) ||
6074 PPC::isVMRGLShuffleMask(SVOp, 4, 1, DAG) ||
6075 PPC::isVMRGHShuffleMask(SVOp, 1, 1, DAG) ||
6076 PPC::isVMRGHShuffleMask(SVOp, 2, 1, DAG) ||
6077 PPC::isVMRGHShuffleMask(SVOp, 4, 1, DAG)) {
Chris Lattner19e90552006-04-14 05:19:18 +00006078 return Op;
6079 }
6080 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006081
Chris Lattner19e90552006-04-14 05:19:18 +00006082 // Altivec has a variety of "shuffle immediates" that take two vector inputs
6083 // and produce a fixed permutation. If any of these match, do not lower to
6084 // VPERM.
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00006085 unsigned int ShuffleKind = isLittleEndian ? 2 : 0;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00006086 if (PPC::isVPKUWUMShuffleMask(SVOp, ShuffleKind, DAG) ||
6087 PPC::isVPKUHUMShuffleMask(SVOp, ShuffleKind, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00006088 PPC::isVSLDOIShuffleMask(SVOp, ShuffleKind, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00006089 PPC::isVMRGLShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
6090 PPC::isVMRGLShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
6091 PPC::isVMRGLShuffleMask(SVOp, 4, ShuffleKind, DAG) ||
6092 PPC::isVMRGHShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
6093 PPC::isVMRGHShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
6094 PPC::isVMRGHShuffleMask(SVOp, 4, ShuffleKind, DAG))
Chris Lattner19e90552006-04-14 05:19:18 +00006095 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006096
Chris Lattner071ad012006-04-17 05:28:54 +00006097 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
6098 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00006099 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00006100
Chris Lattner071ad012006-04-17 05:28:54 +00006101 unsigned PFIndexes[4];
6102 bool isFourElementShuffle = true;
6103 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
6104 unsigned EltNo = 8; // Start out undef.
6105 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006106 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00006107 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006108
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006109 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00006110 if ((ByteSource & 3) != j) {
6111 isFourElementShuffle = false;
6112 break;
6113 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006114
Chris Lattner071ad012006-04-17 05:28:54 +00006115 if (EltNo == 8) {
6116 EltNo = ByteSource/4;
6117 } else if (EltNo != ByteSource/4) {
6118 isFourElementShuffle = false;
6119 break;
6120 }
6121 }
6122 PFIndexes[i] = EltNo;
6123 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006124
6125 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00006126 // perfect shuffle vector to determine if it is cost effective to do this as
6127 // discrete instructions, or whether we should use a vperm.
Bill Schmidtf910a062014-06-10 14:35:01 +00006128 // For now, we skip this for little endian until such time as we have a
6129 // little-endian perfect shuffle table.
6130 if (isFourElementShuffle && !isLittleEndian) {
Chris Lattner071ad012006-04-17 05:28:54 +00006131 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006132 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00006133 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00006134
Chris Lattner071ad012006-04-17 05:28:54 +00006135 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6136 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006137
Chris Lattner071ad012006-04-17 05:28:54 +00006138 // Determining when to avoid vperm is tricky. Many things affect the cost
6139 // of vperm, particularly how many times the perm mask needs to be computed.
6140 // For example, if the perm mask can be hoisted out of a loop or is already
6141 // used (perhaps because there are multiple permutes with the same shuffle
6142 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
6143 // the loop requires an extra register.
6144 //
6145 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00006146 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00006147 // available, if this block is within a loop, we should avoid using vperm
6148 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006149 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006150 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006151 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006152
Chris Lattner19e90552006-04-14 05:19:18 +00006153 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
6154 // vector that will get spilled to the constant pool.
6155 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006156
Chris Lattner19e90552006-04-14 05:19:18 +00006157 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
6158 // that it is in input element units, not in bytes. Convert now.
Bill Schmidt4aedff82014-06-06 14:06:26 +00006159
6160 // For little endian, the order of the input vectors is reversed, and
6161 // the permutation mask is complemented with respect to 31. This is
6162 // necessary to produce proper semantics with the big-endian-biased vperm
6163 // instruction.
Owen Anderson53aa7a92009-08-10 22:56:29 +00006164 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006165 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006166
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006167 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006168 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
6169 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00006170
Chris Lattner19e90552006-04-14 05:19:18 +00006171 for (unsigned j = 0; j != BytesPerElement; ++j)
Bill Schmidt4aedff82014-06-06 14:06:26 +00006172 if (isLittleEndian)
6173 ResultMask.push_back(DAG.getConstant(31 - (SrcElt*BytesPerElement+j),
6174 MVT::i32));
6175 else
6176 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
6177 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00006178 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006179
Owen Anderson9f944592009-08-11 20:47:22 +00006180 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Craig Topper48d114b2014-04-26 18:35:24 +00006181 ResultMask);
Bill Schmidt4aedff82014-06-06 14:06:26 +00006182 if (isLittleEndian)
6183 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
6184 V2, V1, VPermMask);
6185 else
6186 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
6187 V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00006188}
6189
Chris Lattner9754d142006-04-18 17:59:36 +00006190/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
6191/// altivec comparison. If it is, return true and fill in Opc/isDot with
6192/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006193static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner9754d142006-04-18 17:59:36 +00006194 bool &isDot) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00006195 unsigned IntrinsicID =
6196 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00006197 CompareOpc = -1;
6198 isDot = false;
6199 switch (IntrinsicID) {
6200 default: return false;
6201 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00006202 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
6203 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
6204 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
6205 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
6206 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
6207 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
6208 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
6209 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
6210 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
6211 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
6212 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
6213 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
6214 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006215
Chris Lattner4211ca92006-04-14 06:01:58 +00006216 // Normal Comparisons.
6217 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
6218 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
6219 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
6220 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
6221 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
6222 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
6223 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
6224 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
6225 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
6226 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
6227 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
6228 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
6229 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
6230 }
Chris Lattner9754d142006-04-18 17:59:36 +00006231 return true;
6232}
6233
6234/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
6235/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006236SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006237 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00006238 // If this is a lowered altivec predicate compare, CompareOpc is set to the
6239 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006240 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00006241 int CompareOpc;
6242 bool isDot;
6243 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006244 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006245
Chris Lattner9754d142006-04-18 17:59:36 +00006246 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00006247 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00006248 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00006249 Op.getOperand(1), Op.getOperand(2),
6250 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00006251 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00006252 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006253
Chris Lattner4211ca92006-04-14 06:01:58 +00006254 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006255 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00006256 Op.getOperand(2), // LHS
6257 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00006258 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00006259 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00006260 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00006261 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006262
Chris Lattner4211ca92006-04-14 06:01:58 +00006263 // Now that we have the comparison, emit a copy from the CR to a GPR.
6264 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00006265 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00006266 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00006267 CompNode.getValue(1));
6268
Chris Lattner4211ca92006-04-14 06:01:58 +00006269 // Unpack the result based on how the target uses it.
6270 unsigned BitNo; // Bit # of CR6.
6271 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00006272 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00006273 default: // Can't happen, don't crash on invalid number though.
6274 case 0: // Return the value of the EQ bit of CR6.
6275 BitNo = 0; InvertBit = false;
6276 break;
6277 case 1: // Return the inverted value of the EQ bit of CR6.
6278 BitNo = 0; InvertBit = true;
6279 break;
6280 case 2: // Return the value of the LT bit of CR6.
6281 BitNo = 2; InvertBit = false;
6282 break;
6283 case 3: // Return the inverted value of the LT bit of CR6.
6284 BitNo = 2; InvertBit = true;
6285 break;
6286 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006287
Chris Lattner4211ca92006-04-14 06:01:58 +00006288 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00006289 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
6290 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00006291 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00006292 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
6293 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006294
Chris Lattner4211ca92006-04-14 06:01:58 +00006295 // If we are supposed to, toggle the bit.
6296 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00006297 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
6298 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00006299 return Flags;
6300}
6301
Hal Finkel5c0d1452014-03-30 13:22:59 +00006302SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
6303 SelectionDAG &DAG) const {
6304 SDLoc dl(Op);
6305 // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int
6306 // instructions), but for smaller types, we need to first extend up to v2i32
6307 // before doing going farther.
6308 if (Op.getValueType() == MVT::v2i64) {
6309 EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
6310 if (ExtVT != MVT::v2i32) {
6311 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0));
6312 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op,
6313 DAG.getValueType(EVT::getVectorVT(*DAG.getContext(),
6314 ExtVT.getVectorElementType(), 4)));
6315 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op);
6316 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op,
6317 DAG.getValueType(MVT::v2i32));
6318 }
6319
6320 return Op;
6321 }
6322
6323 return SDValue();
6324}
6325
Scott Michelcf0da6c2009-02-17 22:15:04 +00006326SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006327 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006328 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00006329 // Create a stack slot that is 16-byte aligned.
6330 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00006331 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00006332 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006333 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006334
Chris Lattner4211ca92006-04-14 06:01:58 +00006335 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00006336 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00006337 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00006338 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00006339 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00006340 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006341 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00006342}
6343
Dan Gohman21cea8a2010-04-17 15:26:15 +00006344SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006345 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006346 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006347 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006348
Owen Anderson9f944592009-08-11 20:47:22 +00006349 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
6350 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006351
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006352 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006353 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006354
Chris Lattner7e4398742006-04-18 03:43:48 +00006355 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00006356 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
6357 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
6358 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006359
Chris Lattner7e4398742006-04-18 03:43:48 +00006360 // Low parts multiplied together, generating 32-bit results (we ignore the
6361 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006362 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00006363 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006364
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006365 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00006366 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00006367 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006368 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006369 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00006370 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
6371 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006372 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006373
Owen Anderson9f944592009-08-11 20:47:22 +00006374 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00006375
Chris Lattner96d50482006-04-18 04:28:57 +00006376 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006377 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00006378 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006379 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006380 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006381
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006382 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006383 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00006384 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00006385 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006386
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006387 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006388 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00006389 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00006390 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006391
Bill Schmidt42995e82014-06-09 16:06:29 +00006392 // Merge the results together. Because vmuleub and vmuloub are
6393 // instructions with a big-endian bias, we must reverse the
6394 // element numbering and reverse the meaning of "odd" and "even"
6395 // when generating little endian code.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006396 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006397 for (unsigned i = 0; i != 8; ++i) {
Bill Schmidt42995e82014-06-09 16:06:29 +00006398 if (isLittleEndian) {
6399 Ops[i*2 ] = 2*i;
6400 Ops[i*2+1] = 2*i+16;
6401 } else {
6402 Ops[i*2 ] = 2*i+1;
6403 Ops[i*2+1] = 2*i+1+16;
6404 }
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006405 }
Bill Schmidt42995e82014-06-09 16:06:29 +00006406 if (isLittleEndian)
6407 return DAG.getVectorShuffle(MVT::v16i8, dl, OddParts, EvenParts, Ops);
6408 else
6409 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00006410 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006411 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00006412 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006413}
6414
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006415/// LowerOperation - Provide custom lowering hooks for some operations.
6416///
Dan Gohman21cea8a2010-04-17 15:26:15 +00006417SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006418 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006419 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00006420 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00006421 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006422 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00006423 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00006424 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006425 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00006426 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
6427 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006428 case ISD::VASTART:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006429 return LowerVASTART(Op, DAG, Subtarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006430
6431 case ISD::VAARG:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006432 return LowerVAARG(Op, DAG, Subtarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00006433
Roman Divackyc3825df2013-07-25 21:36:47 +00006434 case ISD::VACOPY:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006435 return LowerVACOPY(Op, DAG, Subtarget);
Roman Divackyc3825df2013-07-25 21:36:47 +00006436
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006437 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, Subtarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00006438 case ISD::DYNAMIC_STACKALLOC:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006439 return LowerDYNAMIC_STACKALLOC(Op, DAG, Subtarget);
Evan Cheng51096af2008-04-19 01:30:48 +00006440
Hal Finkel756810f2013-03-21 21:37:52 +00006441 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
6442 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
6443
Hal Finkel940ab932014-02-28 00:27:01 +00006444 case ISD::LOAD: return LowerLOAD(Op, DAG);
6445 case ISD::STORE: return LowerSTORE(Op, DAG);
6446 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006447 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006448 case ISD::FP_TO_UINT:
6449 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006450 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00006451 case ISD::UINT_TO_FP:
6452 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00006453 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006454
Chris Lattner4211ca92006-04-14 06:01:58 +00006455 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00006456 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
6457 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
6458 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006459
Chris Lattner4211ca92006-04-14 06:01:58 +00006460 // Vector-related lowering.
6461 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6462 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6463 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6464 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Hal Finkel5c0d1452014-03-30 13:22:59 +00006465 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006466 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006467
Hal Finkel25c19922013-05-15 21:37:41 +00006468 // For counter-based loop handling.
6469 case ISD::INTRINSIC_W_CHAIN: return SDValue();
6470
Chris Lattnerf6a81562007-12-08 06:59:59 +00006471 // Frame & Return address.
6472 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00006473 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00006474 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006475}
6476
Duncan Sands6ed40142008-12-01 11:39:25 +00006477void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
6478 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006479 SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00006480 const TargetMachine &TM = getTargetMachine();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006481 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00006482 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00006483 default:
Craig Toppere55c5562012-02-07 02:50:20 +00006484 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkel25c19922013-05-15 21:37:41 +00006485 case ISD::INTRINSIC_W_CHAIN: {
6486 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
6487 Intrinsic::ppc_is_decremented_ctr_nonzero)
6488 break;
6489
6490 assert(N->getValueType(0) == MVT::i1 &&
6491 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00006492 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00006493 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
6494 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
6495 N->getOperand(1));
6496
6497 Results.push_back(NewInt);
6498 Results.push_back(NewInt.getValue(1));
6499 break;
6500 }
Roman Divacky4394e682011-06-28 15:30:42 +00006501 case ISD::VAARG: {
6502 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
6503 || TM.getSubtarget<PPCSubtarget>().isPPC64())
6504 return;
6505
6506 EVT VT = N->getValueType(0);
6507
6508 if (VT == MVT::i64) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006509 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, Subtarget);
Roman Divacky4394e682011-06-28 15:30:42 +00006510
6511 Results.push_back(NewNode);
6512 Results.push_back(NewNode.getValue(1));
6513 }
6514 return;
6515 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006516 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00006517 assert(N->getValueType(0) == MVT::ppcf128);
6518 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006519 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006520 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006521 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00006522 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006523 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006524 DAG.getIntPtrConstant(1));
6525
Ulrich Weigand874fc622013-03-26 10:56:22 +00006526 // Add the two halves of the long double in round-to-zero mode.
6527 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00006528
6529 // We know the low half is about to be thrown away, so just use something
6530 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00006531 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00006532 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00006533 return;
Duncan Sands2a287912008-07-19 16:26:02 +00006534 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006535 case ISD::FP_TO_SINT:
Bill Schmidt41221692013-07-09 18:50:20 +00006536 // LowerFP_TO_INT() can only handle f32 and f64.
6537 if (N->getOperand(0).getValueType() == MVT::ppcf128)
6538 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006539 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00006540 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00006541 }
6542}
6543
6544
Chris Lattner4211ca92006-04-14 06:01:58 +00006545//===----------------------------------------------------------------------===//
6546// Other Lowering Code
6547//===----------------------------------------------------------------------===//
6548
Robin Morisset22129962014-09-23 20:46:49 +00006549static Instruction* callIntrinsic(IRBuilder<> &Builder, Intrinsic::ID Id) {
6550 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
6551 Function *Func = Intrinsic::getDeclaration(M, Id);
6552 return Builder.CreateCall(Func);
6553}
6554
6555// The mappings for emitLeading/TrailingFence is taken from
6556// http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
6557Instruction* PPCTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
6558 AtomicOrdering Ord, bool IsStore,
6559 bool IsLoad) const {
6560 if (Ord == SequentiallyConsistent)
6561 return callIntrinsic(Builder, Intrinsic::ppc_sync);
6562 else if (isAtLeastRelease(Ord))
6563 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
6564 else
6565 return nullptr;
6566}
6567
6568Instruction* PPCTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
6569 AtomicOrdering Ord, bool IsStore,
6570 bool IsLoad) const {
6571 if (IsLoad && isAtLeastAcquire(Ord))
6572 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
6573 // FIXME: this is too conservative, a dependent branch + isync is enough.
6574 // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html and
6575 // http://www.rdrop.com/users/paulmck/scalability/paper/N2745r.2011.03.04a.html
6576 // and http://www.cl.cam.ac.uk/~pes20/cppppc/ for justification.
6577 else
6578 return nullptr;
6579}
6580
Chris Lattner9b577f12005-08-26 21:23:58 +00006581MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00006582PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman747e55b2009-02-07 16:15:20 +00006583 bool is64bit, unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006584 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christopherd9134482014-08-04 21:25:23 +00006585 const TargetInstrInfo *TII =
6586 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Dale Johannesend4eb0522008-08-25 22:34:37 +00006587
6588 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6589 MachineFunction *F = BB->getParent();
6590 MachineFunction::iterator It = BB;
6591 ++It;
6592
6593 unsigned dest = MI->getOperand(0).getReg();
6594 unsigned ptrA = MI->getOperand(1).getReg();
6595 unsigned ptrB = MI->getOperand(2).getReg();
6596 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006597 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00006598
6599 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6600 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6601 F->insert(It, loopMBB);
6602 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006603 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006604 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006605 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006606
6607 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006608 unsigned TmpReg = (!BinOpcode) ? incr :
6609 RegInfo.createVirtualRegister(
Dale Johannesenbc698292008-09-02 20:30:23 +00006610 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6611 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006612
6613 // thisMBB:
6614 // ...
6615 // fallthrough --> loopMBB
6616 BB->addSuccessor(loopMBB);
6617
6618 // loopMBB:
6619 // l[wd]arx dest, ptr
6620 // add r0, dest, incr
6621 // st[wd]cx. r0, ptr
6622 // bne- loopMBB
6623 // fallthrough --> exitMBB
6624 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006625 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00006626 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006627 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006628 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
6629 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesend4eb0522008-08-25 22:34:37 +00006630 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006631 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006632 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006633 BB->addSuccessor(loopMBB);
6634 BB->addSuccessor(exitMBB);
6635
6636 // exitMBB:
6637 // ...
6638 BB = exitMBB;
6639 return BB;
6640}
6641
6642MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00006643PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00006644 MachineBasicBlock *BB,
6645 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00006646 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006647 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christopherd9134482014-08-04 21:25:23 +00006648 const TargetInstrInfo *TII =
6649 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Dale Johannesena32affb2008-08-28 17:53:09 +00006650 // In 64 bit mode we have to use 64 bits for addresses, even though the
6651 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
6652 // registers without caring whether they're 32 or 64, but here we're
6653 // doing actual arithmetic on the addresses.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006654 bool is64bit = Subtarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00006655 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00006656
6657 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6658 MachineFunction *F = BB->getParent();
6659 MachineFunction::iterator It = BB;
6660 ++It;
6661
6662 unsigned dest = MI->getOperand(0).getReg();
6663 unsigned ptrA = MI->getOperand(1).getReg();
6664 unsigned ptrB = MI->getOperand(2).getReg();
6665 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006666 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00006667
6668 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6669 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6670 F->insert(It, loopMBB);
6671 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006672 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006673 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006674 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006675
6676 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006677 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006678 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6679 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00006680 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6681 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6682 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6683 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
6684 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6685 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6686 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6687 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6688 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
6689 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006690 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006691 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006692 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006693
6694 // thisMBB:
6695 // ...
6696 // fallthrough --> loopMBB
6697 BB->addSuccessor(loopMBB);
6698
6699 // The 4-byte load must be aligned, while a char or short may be
6700 // anywhere in the word. Hence all this nasty bookkeeping code.
6701 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6702 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006703 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00006704 // rlwinm ptr, ptr1, 0, 0, 29
6705 // slw incr2, incr, shift
6706 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6707 // slw mask, mask2, shift
6708 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00006709 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006710 // add tmp, tmpDest, incr2
6711 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00006712 // and tmp3, tmp, mask
6713 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00006714 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00006715 // bne- loopMBB
6716 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006717 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006718 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00006719 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006720 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006721 .addReg(ptrA).addReg(ptrB);
6722 } else {
6723 Ptr1Reg = ptrB;
6724 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006725 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006726 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006727 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006728 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6729 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006730 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006731 .addReg(Ptr1Reg).addImm(0).addImm(61);
6732 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006733 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006734 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006735 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006736 .addReg(incr).addReg(ShiftReg);
6737 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006738 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00006739 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006740 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6741 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00006742 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006743 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006744 .addReg(Mask2Reg).addReg(ShiftReg);
6745
6746 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006747 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006748 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006749 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006750 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006751 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006752 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006753 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006754 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006755 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006756 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006757 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00006758 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006759 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006760 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006761 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006762 BB->addSuccessor(loopMBB);
6763 BB->addSuccessor(exitMBB);
6764
6765 // exitMBB:
6766 // ...
6767 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006768 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6769 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00006770 return BB;
6771}
6772
Hal Finkel756810f2013-03-21 21:37:52 +00006773llvm::MachineBasicBlock*
6774PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6775 MachineBasicBlock *MBB) const {
6776 DebugLoc DL = MI->getDebugLoc();
Eric Christopherd9134482014-08-04 21:25:23 +00006777 const TargetInstrInfo *TII =
6778 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00006779
6780 MachineFunction *MF = MBB->getParent();
6781 MachineRegisterInfo &MRI = MF->getRegInfo();
6782
6783 const BasicBlock *BB = MBB->getBasicBlock();
6784 MachineFunction::iterator I = MBB;
6785 ++I;
6786
6787 // Memory Reference
6788 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6789 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6790
6791 unsigned DstReg = MI->getOperand(0).getReg();
6792 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6793 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6794 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6795 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6796
6797 MVT PVT = getPointerTy();
6798 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6799 "Invalid Pointer Size!");
6800 // For v = setjmp(buf), we generate
6801 //
6802 // thisMBB:
6803 // SjLjSetup mainMBB
6804 // bl mainMBB
6805 // v_restore = 1
6806 // b sinkMBB
6807 //
6808 // mainMBB:
6809 // buf[LabelOffset] = LR
6810 // v_main = 0
6811 //
6812 // sinkMBB:
6813 // v = phi(main, restore)
6814 //
6815
6816 MachineBasicBlock *thisMBB = MBB;
6817 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6818 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6819 MF->insert(I, mainMBB);
6820 MF->insert(I, sinkMBB);
6821
6822 MachineInstrBuilder MIB;
6823
6824 // Transfer the remainder of BB and its successor edges to sinkMBB.
6825 sinkMBB->splice(sinkMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006826 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Hal Finkel756810f2013-03-21 21:37:52 +00006827 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6828
6829 // Note that the structure of the jmp_buf used here is not compatible
6830 // with that used by libc, and is not designed to be. Specifically, it
6831 // stores only those 'reserved' registers that LLVM does not otherwise
6832 // understand how to spill. Also, by convention, by the time this
6833 // intrinsic is called, Clang has already stored the frame address in the
6834 // first slot of the buffer and stack address in the third. Following the
6835 // X86 target code, we'll store the jump address in the second slot. We also
6836 // need to save the TOC pointer (R2) to handle jumps between shared
6837 // libraries, and that will be stored in the fourth slot. The thread
6838 // identifier (R13) is not affected.
6839
6840 // thisMBB:
6841 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6842 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006843 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006844
6845 // Prepare IP either in reg.
6846 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6847 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6848 unsigned BufReg = MI->getOperand(1).getReg();
6849
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006850 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI()) {
Hal Finkel756810f2013-03-21 21:37:52 +00006851 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6852 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006853 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006854 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006855 MIB.setMemRefs(MMOBegin, MMOEnd);
6856 }
6857
Hal Finkelf05d6c72013-07-17 23:50:51 +00006858 // Naked functions never have a base pointer, and so we use r1. For all
6859 // other functions, this decision must be delayed until during PEI.
6860 unsigned BaseReg;
6861 if (MF->getFunction()->getAttributes().hasAttribute(
6862 AttributeSet::FunctionIndex, Attribute::Naked))
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006863 BaseReg = Subtarget.isPPC64() ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006864 else
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006865 BaseReg = Subtarget.isPPC64() ? PPC::BP8 : PPC::BP;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006866
6867 MIB = BuildMI(*thisMBB, MI, DL,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006868 TII->get(Subtarget.isPPC64() ? PPC::STD : PPC::STW))
Hal Finkelf05d6c72013-07-17 23:50:51 +00006869 .addReg(BaseReg)
6870 .addImm(BPOffset)
6871 .addReg(BufReg);
6872 MIB.setMemRefs(MMOBegin, MMOEnd);
6873
Hal Finkel756810f2013-03-21 21:37:52 +00006874 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00006875 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling5e7656b2013-06-07 07:55:53 +00006876 const PPCRegisterInfo *TRI =
Eric Christopherd9134482014-08-04 21:25:23 +00006877 getTargetMachine().getSubtarget<PPCSubtarget>().getRegisterInfo();
Bill Wendling5e7656b2013-06-07 07:55:53 +00006878 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00006879
6880 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6881
6882 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6883 .addMBB(mainMBB);
6884 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6885
6886 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6887 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6888
6889 // mainMBB:
6890 // mainDstReg = 0
6891 MIB = BuildMI(mainMBB, DL,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006892 TII->get(Subtarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006893
6894 // Store IP
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006895 if (Subtarget.isPPC64()) {
Hal Finkel756810f2013-03-21 21:37:52 +00006896 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6897 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006898 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006899 .addReg(BufReg);
6900 } else {
6901 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6902 .addReg(LabelReg)
6903 .addImm(LabelOffset)
6904 .addReg(BufReg);
6905 }
6906
6907 MIB.setMemRefs(MMOBegin, MMOEnd);
6908
6909 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6910 mainMBB->addSuccessor(sinkMBB);
6911
6912 // sinkMBB:
6913 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6914 TII->get(PPC::PHI), DstReg)
6915 .addReg(mainDstReg).addMBB(mainMBB)
6916 .addReg(restoreDstReg).addMBB(thisMBB);
6917
6918 MI->eraseFromParent();
6919 return sinkMBB;
6920}
6921
6922MachineBasicBlock *
6923PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6924 MachineBasicBlock *MBB) const {
6925 DebugLoc DL = MI->getDebugLoc();
Eric Christopherd9134482014-08-04 21:25:23 +00006926 const TargetInstrInfo *TII =
6927 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00006928
6929 MachineFunction *MF = MBB->getParent();
6930 MachineRegisterInfo &MRI = MF->getRegInfo();
6931
6932 // Memory Reference
6933 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6934 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6935
6936 MVT PVT = getPointerTy();
6937 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6938 "Invalid Pointer Size!");
6939
6940 const TargetRegisterClass *RC =
6941 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6942 unsigned Tmp = MRI.createVirtualRegister(RC);
6943 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6944 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6945 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +00006946 unsigned BP = (PVT == MVT::i64) ? PPC::X30 :
6947 (Subtarget.isSVR4ABI() &&
6948 MF->getTarget().getRelocationModel() == Reloc::PIC_ ?
6949 PPC::R29 : PPC::R30);
Hal Finkel756810f2013-03-21 21:37:52 +00006950
6951 MachineInstrBuilder MIB;
6952
6953 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6954 const int64_t SPOffset = 2 * PVT.getStoreSize();
6955 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006956 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006957
6958 unsigned BufReg = MI->getOperand(0).getReg();
6959
6960 // Reload FP (the jumped-to function may not have had a
6961 // frame pointer, and if so, then its r31 will be restored
6962 // as necessary).
6963 if (PVT == MVT::i64) {
6964 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6965 .addImm(0)
6966 .addReg(BufReg);
6967 } else {
6968 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6969 .addImm(0)
6970 .addReg(BufReg);
6971 }
6972 MIB.setMemRefs(MMOBegin, MMOEnd);
6973
6974 // Reload IP
6975 if (PVT == MVT::i64) {
6976 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006977 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006978 .addReg(BufReg);
6979 } else {
6980 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6981 .addImm(LabelOffset)
6982 .addReg(BufReg);
6983 }
6984 MIB.setMemRefs(MMOBegin, MMOEnd);
6985
6986 // Reload SP
6987 if (PVT == MVT::i64) {
6988 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006989 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006990 .addReg(BufReg);
6991 } else {
6992 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6993 .addImm(SPOffset)
6994 .addReg(BufReg);
6995 }
6996 MIB.setMemRefs(MMOBegin, MMOEnd);
6997
Hal Finkelf05d6c72013-07-17 23:50:51 +00006998 // Reload BP
6999 if (PVT == MVT::i64) {
7000 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
7001 .addImm(BPOffset)
7002 .addReg(BufReg);
7003 } else {
7004 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
7005 .addImm(BPOffset)
7006 .addReg(BufReg);
7007 }
7008 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00007009
7010 // Reload TOC
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007011 if (PVT == MVT::i64 && Subtarget.isSVR4ABI()) {
Hal Finkel756810f2013-03-21 21:37:52 +00007012 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00007013 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00007014 .addReg(BufReg);
7015
7016 MIB.setMemRefs(MMOBegin, MMOEnd);
7017 }
7018
7019 // Jump
7020 BuildMI(*MBB, MI, DL,
7021 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
7022 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
7023
7024 MI->eraseFromParent();
7025 return MBB;
7026}
7027
Dale Johannesena32affb2008-08-28 17:53:09 +00007028MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007029PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007030 MachineBasicBlock *BB) const {
Hal Finkel756810f2013-03-21 21:37:52 +00007031 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
7032 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
7033 return emitEHSjLjSetJmp(MI, BB);
7034 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
7035 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
7036 return emitEHSjLjLongJmp(MI, BB);
7037 }
7038
Eric Christopherd9134482014-08-04 21:25:23 +00007039 const TargetInstrInfo *TII =
7040 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00007041
7042 // To "insert" these instructions we actually have to insert their
7043 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00007044 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007045 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00007046 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00007047
Dan Gohman3b460302008-07-07 23:14:23 +00007048 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00007049
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007050 if (Subtarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Hal Finkel940ab932014-02-28 00:27:01 +00007051 MI->getOpcode() == PPC::SELECT_CC_I8 ||
7052 MI->getOpcode() == PPC::SELECT_I4 ||
7053 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00007054 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00007055 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
7056 MI->getOpcode() == PPC::SELECT_CC_I8)
7057 Cond.push_back(MI->getOperand(4));
7058 else
7059 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00007060 Cond.push_back(MI->getOperand(1));
7061
Hal Finkel460e94d2012-06-22 23:10:08 +00007062 DebugLoc dl = MI->getDebugLoc();
Eric Christopherd9134482014-08-04 21:25:23 +00007063 const TargetInstrInfo *TII =
7064 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Bill Wendling5e7656b2013-06-07 07:55:53 +00007065 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
7066 Cond, MI->getOperand(2).getReg(),
7067 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00007068 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
7069 MI->getOpcode() == PPC::SELECT_CC_I8 ||
7070 MI->getOpcode() == PPC::SELECT_CC_F4 ||
7071 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkel940ab932014-02-28 00:27:01 +00007072 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00007073 MI->getOpcode() == PPC::SELECT_CC_VSFRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00007074 MI->getOpcode() == PPC::SELECT_CC_VSRC ||
Hal Finkel940ab932014-02-28 00:27:01 +00007075 MI->getOpcode() == PPC::SELECT_I4 ||
7076 MI->getOpcode() == PPC::SELECT_I8 ||
7077 MI->getOpcode() == PPC::SELECT_F4 ||
7078 MI->getOpcode() == PPC::SELECT_F8 ||
Bill Schmidt61e65232014-10-22 13:13:40 +00007079 MI->getOpcode() == PPC::SELECT_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00007080 MI->getOpcode() == PPC::SELECT_VSFRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00007081 MI->getOpcode() == PPC::SELECT_VSRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00007082 // The incoming instruction knows the destination vreg to set, the
7083 // condition code register to branch on, the true/false values to
7084 // select between, and a branch opcode to use.
7085
7086 // thisMBB:
7087 // ...
7088 // TrueVal = ...
7089 // cmpTY ccX, r1, r2
7090 // bCC copy1MBB
7091 // fallthrough --> copy0MBB
7092 MachineBasicBlock *thisMBB = BB;
7093 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7094 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007095 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00007096 F->insert(It, copy0MBB);
7097 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007098
7099 // Transfer the remainder of BB and its successor edges to sinkMBB.
7100 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007101 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007102 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7103
Evan Cheng32e376f2008-07-12 02:23:19 +00007104 // Next, add the true and fallthrough blocks as its successors.
7105 BB->addSuccessor(copy0MBB);
7106 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007107
Hal Finkel940ab932014-02-28 00:27:01 +00007108 if (MI->getOpcode() == PPC::SELECT_I4 ||
7109 MI->getOpcode() == PPC::SELECT_I8 ||
7110 MI->getOpcode() == PPC::SELECT_F4 ||
7111 MI->getOpcode() == PPC::SELECT_F8 ||
Bill Schmidt61e65232014-10-22 13:13:40 +00007112 MI->getOpcode() == PPC::SELECT_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00007113 MI->getOpcode() == PPC::SELECT_VSFRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00007114 MI->getOpcode() == PPC::SELECT_VSRC) {
Hal Finkel940ab932014-02-28 00:27:01 +00007115 BuildMI(BB, dl, TII->get(PPC::BC))
7116 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
7117 } else {
7118 unsigned SelectPred = MI->getOperand(4).getImm();
7119 BuildMI(BB, dl, TII->get(PPC::BCC))
7120 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
7121 }
Dan Gohman34396292010-07-06 20:24:04 +00007122
Evan Cheng32e376f2008-07-12 02:23:19 +00007123 // copy0MBB:
7124 // %FalseValue = ...
7125 // # fallthrough to sinkMBB
7126 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007127
Evan Cheng32e376f2008-07-12 02:23:19 +00007128 // Update machine-CFG edges
7129 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007130
Evan Cheng32e376f2008-07-12 02:23:19 +00007131 // sinkMBB:
7132 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7133 // ...
7134 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007135 BuildMI(*BB, BB->begin(), dl,
7136 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00007137 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
7138 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7139 }
Dale Johannesena32affb2008-08-28 17:53:09 +00007140 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
7141 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
7142 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
7143 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007144 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
7145 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
7146 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
7147 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007148
7149 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
7150 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
7151 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
7152 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007153 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
7154 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
7155 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
7156 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007157
7158 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
7159 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
7160 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
7161 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007162 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
7163 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
7164 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
7165 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007166
7167 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
7168 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
7169 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
7170 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007171 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
7172 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
7173 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
7174 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007175
7176 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00007177 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::NAND);
Dale Johannesena32affb2008-08-28 17:53:09 +00007178 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00007179 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007180 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00007181 BB = EmitAtomicBinary(MI, BB, false, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007182 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00007183 BB = EmitAtomicBinary(MI, BB, true, PPC::NAND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007184
7185 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
7186 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
7187 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
7188 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007189 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
7190 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
7191 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
7192 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007193
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007194 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
7195 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
7196 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
7197 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
7198 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
7199 BB = EmitAtomicBinary(MI, BB, false, 0);
7200 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
7201 BB = EmitAtomicBinary(MI, BB, true, 0);
7202
Evan Cheng32e376f2008-07-12 02:23:19 +00007203 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
7204 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
7205 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
7206
7207 unsigned dest = MI->getOperand(0).getReg();
7208 unsigned ptrA = MI->getOperand(1).getReg();
7209 unsigned ptrB = MI->getOperand(2).getReg();
7210 unsigned oldval = MI->getOperand(3).getReg();
7211 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00007212 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00007213
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007214 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
7215 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
7216 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007217 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007218 F->insert(It, loop1MBB);
7219 F->insert(It, loop2MBB);
7220 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007221 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007222 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007223 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007224 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007225
7226 // thisMBB:
7227 // ...
7228 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007229 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007230
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007231 // loop1MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00007232 // l[wd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007233 // cmp[wd] dest, oldval
7234 // bne- midMBB
7235 // loop2MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00007236 // st[wd]cx. newval, ptr
7237 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007238 // b exitBB
7239 // midMBB:
7240 // st[wd]cx. dest, ptr
7241 // exitBB:
7242 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007243 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00007244 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007245 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00007246 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007247 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007248 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
7249 BB->addSuccessor(loop2MBB);
7250 BB->addSuccessor(midMBB);
7251
7252 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007253 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng32e376f2008-07-12 02:23:19 +00007254 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007255 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007256 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007257 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007258 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007259 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007260
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007261 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007262 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007263 .addReg(dest).addReg(ptrA).addReg(ptrB);
7264 BB->addSuccessor(exitMBB);
7265
Evan Cheng32e376f2008-07-12 02:23:19 +00007266 // exitMBB:
7267 // ...
7268 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00007269 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
7270 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
7271 // We must use 64-bit registers for addresses when targeting 64-bit,
7272 // since we're actually doing arithmetic on them. Other registers
7273 // can be 32-bit.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007274 bool is64bit = Subtarget.isPPC64();
Dale Johannesen340d2642008-08-30 00:08:53 +00007275 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
7276
7277 unsigned dest = MI->getOperand(0).getReg();
7278 unsigned ptrA = MI->getOperand(1).getReg();
7279 unsigned ptrB = MI->getOperand(2).getReg();
7280 unsigned oldval = MI->getOperand(3).getReg();
7281 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00007282 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00007283
7284 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
7285 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
7286 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
7287 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
7288 F->insert(It, loop1MBB);
7289 F->insert(It, loop2MBB);
7290 F->insert(It, midMBB);
7291 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007292 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007293 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007294 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00007295
7296 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00007297 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00007298 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
7299 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00007300 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
7301 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
7302 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
7303 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
7304 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
7305 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
7306 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
7307 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
7308 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
7309 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
7310 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
7311 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
7312 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
7313 unsigned Ptr1Reg;
7314 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00007315 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00007316 // thisMBB:
7317 // ...
7318 // fallthrough --> loopMBB
7319 BB->addSuccessor(loop1MBB);
7320
7321 // The 4-byte load must be aligned, while a char or short may be
7322 // anywhere in the word. Hence all this nasty bookkeeping code.
7323 // add ptr1, ptrA, ptrB [copy if ptrA==0]
7324 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00007325 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00007326 // rlwinm ptr, ptr1, 0, 0, 29
7327 // slw newval2, newval, shift
7328 // slw oldval2, oldval,shift
7329 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
7330 // slw mask, mask2, shift
7331 // and newval3, newval2, mask
7332 // and oldval3, oldval2, mask
7333 // loop1MBB:
7334 // lwarx tmpDest, ptr
7335 // and tmp, tmpDest, mask
7336 // cmpw tmp, oldval3
7337 // bne- midMBB
7338 // loop2MBB:
7339 // andc tmp2, tmpDest, mask
7340 // or tmp4, tmp2, newval3
7341 // stwcx. tmp4, ptr
7342 // bne- loop1MBB
7343 // b exitBB
7344 // midMBB:
7345 // stwcx. tmpDest, ptr
7346 // exitBB:
7347 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007348 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00007349 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007350 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007351 .addReg(ptrA).addReg(ptrB);
7352 } else {
7353 Ptr1Reg = ptrB;
7354 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00007355 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007356 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007357 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007358 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
7359 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00007360 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007361 .addReg(Ptr1Reg).addImm(0).addImm(61);
7362 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00007363 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007364 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007365 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007366 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007367 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007368 .addReg(oldval).addReg(ShiftReg);
7369 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00007370 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00007371 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00007372 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
7373 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
7374 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00007375 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00007376 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007377 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007378 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007379 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007380 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007381 .addReg(OldVal2Reg).addReg(MaskReg);
7382
7383 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007384 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007385 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007386 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
7387 .addReg(TmpDestReg).addReg(MaskReg);
7388 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00007389 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007390 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00007391 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
7392 BB->addSuccessor(loop2MBB);
7393 BB->addSuccessor(midMBB);
7394
7395 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007396 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
7397 .addReg(TmpDestReg).addReg(MaskReg);
7398 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
7399 .addReg(Tmp2Reg).addReg(NewVal3Reg);
7400 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007401 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007402 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00007403 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007404 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00007405 BB->addSuccessor(loop1MBB);
7406 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007407
Dale Johannesen340d2642008-08-30 00:08:53 +00007408 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007409 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007410 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00007411 BB->addSuccessor(exitMBB);
7412
7413 // exitMBB:
7414 // ...
7415 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00007416 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
7417 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00007418 } else if (MI->getOpcode() == PPC::FADDrtz) {
7419 // This pseudo performs an FADD with rounding mode temporarily forced
7420 // to round-to-zero. We emit this via custom inserter since the FPSCR
7421 // is not modeled at the SelectionDAG level.
7422 unsigned Dest = MI->getOperand(0).getReg();
7423 unsigned Src1 = MI->getOperand(1).getReg();
7424 unsigned Src2 = MI->getOperand(2).getReg();
7425 DebugLoc dl = MI->getDebugLoc();
7426
7427 MachineRegisterInfo &RegInfo = F->getRegInfo();
7428 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
7429
7430 // Save FPSCR value.
7431 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
7432
7433 // Set rounding mode to round-to-zero.
7434 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
7435 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
7436
7437 // Perform addition.
7438 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
7439
7440 // Restore FPSCR value.
7441 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00007442 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
7443 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
7444 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
7445 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
7446 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
7447 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
7448 PPC::ANDIo8 : PPC::ANDIo;
7449 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
7450 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
7451
7452 MachineRegisterInfo &RegInfo = F->getRegInfo();
7453 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
7454 &PPC::GPRCRegClass :
7455 &PPC::G8RCRegClass);
7456
7457 DebugLoc dl = MI->getDebugLoc();
7458 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
7459 .addReg(MI->getOperand(1).getReg()).addImm(1);
7460 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
7461 MI->getOperand(0).getReg())
7462 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Dale Johannesen340d2642008-08-30 00:08:53 +00007463 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007464 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00007465 }
Chris Lattner9b577f12005-08-26 21:23:58 +00007466
Dan Gohman34396292010-07-06 20:24:04 +00007467 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00007468 return BB;
7469}
7470
Chris Lattner4211ca92006-04-14 06:01:58 +00007471//===----------------------------------------------------------------------===//
7472// Target Optimization Hooks
7473//===----------------------------------------------------------------------===//
7474
Sanjay Patel8fde95c2014-09-30 20:28:48 +00007475SDValue PPCTargetLowering::getRsqrtEstimate(SDValue Operand,
7476 DAGCombinerInfo &DCI,
Sanjay Patel957efc232014-10-24 17:02:16 +00007477 unsigned &RefinementSteps,
7478 bool &UseOneConstNR) const {
Sanjay Patelbdf1e382014-09-26 23:01:47 +00007479 EVT VT = Operand.getValueType();
Sanjay Patel8fde95c2014-09-30 20:28:48 +00007480 if ((VT == MVT::f32 && Subtarget.hasFRSQRTES()) ||
7481 (VT == MVT::f64 && Subtarget.hasFRSQRTE()) ||
7482 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
7483 (VT == MVT::v2f64 && Subtarget.hasVSX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00007484 // Convergence is quadratic, so we essentially double the number of digits
Sanjay Patelbdf1e382014-09-26 23:01:47 +00007485 // correct after every iteration. For both FRE and FRSQRTE, the minimum
7486 // architected relative accuracy is 2^-5. When hasRecipPrec(), this is
7487 // 2^-14. IEEE float has 23 digits and double has 52 digits.
7488 RefinementSteps = Subtarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00007489 if (VT.getScalarType() == MVT::f64)
Sanjay Patelbdf1e382014-09-26 23:01:47 +00007490 ++RefinementSteps;
Sanjay Patel957efc232014-10-24 17:02:16 +00007491 UseOneConstNR = true;
Sanjay Patel8fde95c2014-09-30 20:28:48 +00007492 return DCI.DAG.getNode(PPCISD::FRSQRTE, SDLoc(Operand), VT, Operand);
Hal Finkel2e103312013-04-03 04:01:11 +00007493 }
Sanjay Patel8fde95c2014-09-30 20:28:48 +00007494 return SDValue();
7495}
7496
7497SDValue PPCTargetLowering::getRecipEstimate(SDValue Operand,
7498 DAGCombinerInfo &DCI,
7499 unsigned &RefinementSteps) const {
7500 EVT VT = Operand.getValueType();
7501 if ((VT == MVT::f32 && Subtarget.hasFRES()) ||
7502 (VT == MVT::f64 && Subtarget.hasFRE()) ||
7503 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
7504 (VT == MVT::v2f64 && Subtarget.hasVSX())) {
7505 // Convergence is quadratic, so we essentially double the number of digits
7506 // correct after every iteration. For both FRE and FRSQRTE, the minimum
7507 // architected relative accuracy is 2^-5. When hasRecipPrec(), this is
7508 // 2^-14. IEEE float has 23 digits and double has 52 digits.
7509 RefinementSteps = Subtarget.hasRecipPrec() ? 1 : 3;
7510 if (VT.getScalarType() == MVT::f64)
7511 ++RefinementSteps;
7512 return DCI.DAG.getNode(PPCISD::FRE, SDLoc(Operand), VT, Operand);
7513 }
7514 return SDValue();
Hal Finkel2e103312013-04-03 04:01:11 +00007515}
7516
Hal Finkel3604bf72014-08-01 01:02:01 +00007517static bool isConsecutiveLSLoc(SDValue Loc, EVT VT, LSBaseSDNode *Base,
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007518 unsigned Bytes, int Dist,
7519 SelectionDAG &DAG) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007520 if (VT.getSizeInBits() / 8 != Bytes)
7521 return false;
7522
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007523 SDValue BaseLoc = Base->getBasePtr();
7524 if (Loc.getOpcode() == ISD::FrameIndex) {
7525 if (BaseLoc.getOpcode() != ISD::FrameIndex)
7526 return false;
7527 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7528 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
7529 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
7530 int FS = MFI->getObjectSize(FI);
7531 int BFS = MFI->getObjectSize(BFI);
7532 if (FS != BFS || FS != (int)Bytes) return false;
7533 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
7534 }
7535
7536 // Handle X+C
7537 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
7538 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
7539 return true;
7540
7541 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Craig Topper062a2ba2014-04-25 05:30:21 +00007542 const GlobalValue *GV1 = nullptr;
7543 const GlobalValue *GV2 = nullptr;
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007544 int64_t Offset1 = 0;
7545 int64_t Offset2 = 0;
7546 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
7547 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
7548 if (isGA1 && isGA2 && GV1 == GV2)
7549 return Offset1 == (Offset2 + Dist*Bytes);
7550 return false;
7551}
7552
Hal Finkel3604bf72014-08-01 01:02:01 +00007553// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
7554// not enforce equality of the chain operands.
7555static bool isConsecutiveLS(SDNode *N, LSBaseSDNode *Base,
7556 unsigned Bytes, int Dist,
7557 SelectionDAG &DAG) {
7558 if (LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(N)) {
7559 EVT VT = LS->getMemoryVT();
7560 SDValue Loc = LS->getBasePtr();
7561 return isConsecutiveLSLoc(Loc, VT, Base, Bytes, Dist, DAG);
7562 }
7563
7564 if (N->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
7565 EVT VT;
7566 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
7567 default: return false;
7568 case Intrinsic::ppc_altivec_lvx:
7569 case Intrinsic::ppc_altivec_lvxl:
7570 VT = MVT::v4i32;
7571 break;
7572 case Intrinsic::ppc_altivec_lvebx:
7573 VT = MVT::i8;
7574 break;
7575 case Intrinsic::ppc_altivec_lvehx:
7576 VT = MVT::i16;
7577 break;
7578 case Intrinsic::ppc_altivec_lvewx:
7579 VT = MVT::i32;
7580 break;
7581 }
7582
7583 return isConsecutiveLSLoc(N->getOperand(2), VT, Base, Bytes, Dist, DAG);
7584 }
7585
7586 if (N->getOpcode() == ISD::INTRINSIC_VOID) {
7587 EVT VT;
7588 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
7589 default: return false;
7590 case Intrinsic::ppc_altivec_stvx:
7591 case Intrinsic::ppc_altivec_stvxl:
7592 VT = MVT::v4i32;
7593 break;
7594 case Intrinsic::ppc_altivec_stvebx:
7595 VT = MVT::i8;
7596 break;
7597 case Intrinsic::ppc_altivec_stvehx:
7598 VT = MVT::i16;
7599 break;
7600 case Intrinsic::ppc_altivec_stvewx:
7601 VT = MVT::i32;
7602 break;
7603 }
7604
7605 return isConsecutiveLSLoc(N->getOperand(3), VT, Base, Bytes, Dist, DAG);
7606 }
7607
7608 return false;
7609}
7610
Hal Finkel7d8a6912013-05-26 18:08:30 +00007611// Return true is there is a nearyby consecutive load to the one provided
7612// (regardless of alignment). We search up and down the chain, looking though
Matt Arsenault57e74d22014-07-29 00:02:40 +00007613// token factors and other loads (but nothing else). As a result, a true result
7614// indicates that it is safe to create a new consecutive load adjacent to the
7615// load provided.
Hal Finkel7d8a6912013-05-26 18:08:30 +00007616static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
7617 SDValue Chain = LD->getChain();
7618 EVT VT = LD->getMemoryVT();
7619
7620 SmallSet<SDNode *, 16> LoadRoots;
7621 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
7622 SmallSet<SDNode *, 16> Visited;
7623
7624 // First, search up the chain, branching to follow all token-factor operands.
7625 // If we find a consecutive load, then we're done, otherwise, record all
7626 // nodes just above the top-level loads and token factors.
7627 while (!Queue.empty()) {
7628 SDNode *ChainNext = Queue.pop_back_val();
7629 if (!Visited.insert(ChainNext))
7630 continue;
7631
Hal Finkel3604bf72014-08-01 01:02:01 +00007632 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007633 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007634 return true;
7635
7636 if (!Visited.count(ChainLD->getChain().getNode()))
7637 Queue.push_back(ChainLD->getChain().getNode());
7638 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
Craig Topper66e588b2014-06-29 00:40:57 +00007639 for (const SDUse &O : ChainNext->ops())
7640 if (!Visited.count(O.getNode()))
7641 Queue.push_back(O.getNode());
Hal Finkel7d8a6912013-05-26 18:08:30 +00007642 } else
7643 LoadRoots.insert(ChainNext);
7644 }
7645
7646 // Second, search down the chain, starting from the top-level nodes recorded
7647 // in the first phase. These top-level nodes are the nodes just above all
7648 // loads and token factors. Starting with their uses, recursively look though
7649 // all loads (just the chain uses) and token factors to find a consecutive
7650 // load.
7651 Visited.clear();
7652 Queue.clear();
7653
7654 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
7655 IE = LoadRoots.end(); I != IE; ++I) {
7656 Queue.push_back(*I);
7657
7658 while (!Queue.empty()) {
7659 SDNode *LoadRoot = Queue.pop_back_val();
7660 if (!Visited.insert(LoadRoot))
7661 continue;
7662
Hal Finkel3604bf72014-08-01 01:02:01 +00007663 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007664 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007665 return true;
7666
7667 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
7668 UE = LoadRoot->use_end(); UI != UE; ++UI)
Hal Finkel3604bf72014-08-01 01:02:01 +00007669 if (((isa<MemSDNode>(*UI) &&
7670 cast<MemSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
Hal Finkel7d8a6912013-05-26 18:08:30 +00007671 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
7672 Queue.push_back(*UI);
7673 }
7674 }
7675
7676 return false;
7677}
7678
Hal Finkel940ab932014-02-28 00:27:01 +00007679SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
7680 DAGCombinerInfo &DCI) const {
7681 SelectionDAG &DAG = DCI.DAG;
7682 SDLoc dl(N);
7683
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007684 assert(Subtarget.useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00007685 "Expecting to be tracking CR bits");
7686 // If we're tracking CR bits, we need to be careful that we don't have:
7687 // trunc(binary-ops(zext(x), zext(y)))
7688 // or
7689 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
7690 // such that we're unnecessarily moving things into GPRs when it would be
7691 // better to keep them in CR bits.
7692
7693 // Note that trunc here can be an actual i1 trunc, or can be the effective
7694 // truncation that comes from a setcc or select_cc.
7695 if (N->getOpcode() == ISD::TRUNCATE &&
7696 N->getValueType(0) != MVT::i1)
7697 return SDValue();
7698
7699 if (N->getOperand(0).getValueType() != MVT::i32 &&
7700 N->getOperand(0).getValueType() != MVT::i64)
7701 return SDValue();
7702
7703 if (N->getOpcode() == ISD::SETCC ||
7704 N->getOpcode() == ISD::SELECT_CC) {
7705 // If we're looking at a comparison, then we need to make sure that the
7706 // high bits (all except for the first) don't matter the result.
7707 ISD::CondCode CC =
7708 cast<CondCodeSDNode>(N->getOperand(
7709 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
7710 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
7711
7712 if (ISD::isSignedIntSetCC(CC)) {
7713 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
7714 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
7715 return SDValue();
7716 } else if (ISD::isUnsignedIntSetCC(CC)) {
7717 if (!DAG.MaskedValueIsZero(N->getOperand(0),
7718 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
7719 !DAG.MaskedValueIsZero(N->getOperand(1),
7720 APInt::getHighBitsSet(OpBits, OpBits-1)))
7721 return SDValue();
7722 } else {
7723 // This is neither a signed nor an unsigned comparison, just make sure
7724 // that the high bits are equal.
7725 APInt Op1Zero, Op1One;
7726 APInt Op2Zero, Op2One;
Jay Foada0653a32014-05-14 21:14:37 +00007727 DAG.computeKnownBits(N->getOperand(0), Op1Zero, Op1One);
7728 DAG.computeKnownBits(N->getOperand(1), Op2Zero, Op2One);
Hal Finkel940ab932014-02-28 00:27:01 +00007729
7730 // We don't really care about what is known about the first bit (if
7731 // anything), so clear it in all masks prior to comparing them.
7732 Op1Zero.clearBit(0); Op1One.clearBit(0);
7733 Op2Zero.clearBit(0); Op2One.clearBit(0);
7734
7735 if (Op1Zero != Op2Zero || Op1One != Op2One)
7736 return SDValue();
7737 }
7738 }
7739
7740 // We now know that the higher-order bits are irrelevant, we just need to
7741 // make sure that all of the intermediate operations are bit operations, and
7742 // all inputs are extensions.
7743 if (N->getOperand(0).getOpcode() != ISD::AND &&
7744 N->getOperand(0).getOpcode() != ISD::OR &&
7745 N->getOperand(0).getOpcode() != ISD::XOR &&
7746 N->getOperand(0).getOpcode() != ISD::SELECT &&
7747 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
7748 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
7749 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
7750 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
7751 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
7752 return SDValue();
7753
7754 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
7755 N->getOperand(1).getOpcode() != ISD::AND &&
7756 N->getOperand(1).getOpcode() != ISD::OR &&
7757 N->getOperand(1).getOpcode() != ISD::XOR &&
7758 N->getOperand(1).getOpcode() != ISD::SELECT &&
7759 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
7760 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
7761 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
7762 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
7763 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
7764 return SDValue();
7765
7766 SmallVector<SDValue, 4> Inputs;
7767 SmallVector<SDValue, 8> BinOps, PromOps;
7768 SmallPtrSet<SDNode *, 16> Visited;
7769
7770 for (unsigned i = 0; i < 2; ++i) {
7771 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7772 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7773 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7774 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7775 isa<ConstantSDNode>(N->getOperand(i)))
7776 Inputs.push_back(N->getOperand(i));
7777 else
7778 BinOps.push_back(N->getOperand(i));
7779
7780 if (N->getOpcode() == ISD::TRUNCATE)
7781 break;
7782 }
7783
7784 // Visit all inputs, collect all binary operations (and, or, xor and
7785 // select) that are all fed by extensions.
7786 while (!BinOps.empty()) {
7787 SDValue BinOp = BinOps.back();
7788 BinOps.pop_back();
7789
7790 if (!Visited.insert(BinOp.getNode()))
7791 continue;
7792
7793 PromOps.push_back(BinOp);
7794
7795 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7796 // The condition of the select is not promoted.
7797 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7798 continue;
7799 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7800 continue;
7801
7802 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7803 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7804 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7805 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7806 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7807 Inputs.push_back(BinOp.getOperand(i));
7808 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7809 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7810 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7811 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7812 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
7813 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7814 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7815 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7816 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
7817 BinOps.push_back(BinOp.getOperand(i));
7818 } else {
7819 // We have an input that is not an extension or another binary
7820 // operation; we'll abort this transformation.
7821 return SDValue();
7822 }
7823 }
7824 }
7825
7826 // Make sure that this is a self-contained cluster of operations (which
7827 // is not quite the same thing as saying that everything has only one
7828 // use).
7829 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7830 if (isa<ConstantSDNode>(Inputs[i]))
7831 continue;
7832
7833 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7834 UE = Inputs[i].getNode()->use_end();
7835 UI != UE; ++UI) {
7836 SDNode *User = *UI;
7837 if (User != N && !Visited.count(User))
7838 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007839
7840 // Make sure that we're not going to promote the non-output-value
7841 // operand(s) or SELECT or SELECT_CC.
7842 // FIXME: Although we could sometimes handle this, and it does occur in
7843 // practice that one of the condition inputs to the select is also one of
7844 // the outputs, we currently can't deal with this.
7845 if (User->getOpcode() == ISD::SELECT) {
7846 if (User->getOperand(0) == Inputs[i])
7847 return SDValue();
7848 } else if (User->getOpcode() == ISD::SELECT_CC) {
7849 if (User->getOperand(0) == Inputs[i] ||
7850 User->getOperand(1) == Inputs[i])
7851 return SDValue();
7852 }
Hal Finkel940ab932014-02-28 00:27:01 +00007853 }
7854 }
7855
7856 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7857 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7858 UE = PromOps[i].getNode()->use_end();
7859 UI != UE; ++UI) {
7860 SDNode *User = *UI;
7861 if (User != N && !Visited.count(User))
7862 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007863
7864 // Make sure that we're not going to promote the non-output-value
7865 // operand(s) or SELECT or SELECT_CC.
7866 // FIXME: Although we could sometimes handle this, and it does occur in
7867 // practice that one of the condition inputs to the select is also one of
7868 // the outputs, we currently can't deal with this.
7869 if (User->getOpcode() == ISD::SELECT) {
7870 if (User->getOperand(0) == PromOps[i])
7871 return SDValue();
7872 } else if (User->getOpcode() == ISD::SELECT_CC) {
7873 if (User->getOperand(0) == PromOps[i] ||
7874 User->getOperand(1) == PromOps[i])
7875 return SDValue();
7876 }
Hal Finkel940ab932014-02-28 00:27:01 +00007877 }
7878 }
7879
7880 // Replace all inputs with the extension operand.
7881 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7882 // Constants may have users outside the cluster of to-be-promoted nodes,
7883 // and so we need to replace those as we do the promotions.
7884 if (isa<ConstantSDNode>(Inputs[i]))
7885 continue;
7886 else
7887 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
7888 }
7889
7890 // Replace all operations (these are all the same, but have a different
7891 // (i1) return type). DAG.getNode will validate that the types of
7892 // a binary operator match, so go through the list in reverse so that
7893 // we've likely promoted both operands first. Any intermediate truncations or
7894 // extensions disappear.
7895 while (!PromOps.empty()) {
7896 SDValue PromOp = PromOps.back();
7897 PromOps.pop_back();
7898
7899 if (PromOp.getOpcode() == ISD::TRUNCATE ||
7900 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
7901 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
7902 PromOp.getOpcode() == ISD::ANY_EXTEND) {
7903 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
7904 PromOp.getOperand(0).getValueType() != MVT::i1) {
7905 // The operand is not yet ready (see comment below).
7906 PromOps.insert(PromOps.begin(), PromOp);
7907 continue;
7908 }
7909
7910 SDValue RepValue = PromOp.getOperand(0);
7911 if (isa<ConstantSDNode>(RepValue))
7912 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
7913
7914 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
7915 continue;
7916 }
7917
7918 unsigned C;
7919 switch (PromOp.getOpcode()) {
7920 default: C = 0; break;
7921 case ISD::SELECT: C = 1; break;
7922 case ISD::SELECT_CC: C = 2; break;
7923 }
7924
7925 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7926 PromOp.getOperand(C).getValueType() != MVT::i1) ||
7927 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7928 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
7929 // The to-be-promoted operands of this node have not yet been
7930 // promoted (this should be rare because we're going through the
7931 // list backward, but if one of the operands has several users in
7932 // this cluster of to-be-promoted nodes, it is possible).
7933 PromOps.insert(PromOps.begin(), PromOp);
7934 continue;
7935 }
7936
7937 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7938 PromOp.getNode()->op_end());
7939
7940 // If there are any constant inputs, make sure they're replaced now.
7941 for (unsigned i = 0; i < 2; ++i)
7942 if (isa<ConstantSDNode>(Ops[C+i]))
7943 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
7944
7945 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00007946 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00007947 }
7948
7949 // Now we're left with the initial truncation itself.
7950 if (N->getOpcode() == ISD::TRUNCATE)
7951 return N->getOperand(0);
7952
7953 // Otherwise, this is a comparison. The operands to be compared have just
7954 // changed type (to i1), but everything else is the same.
7955 return SDValue(N, 0);
7956}
7957
7958SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
7959 DAGCombinerInfo &DCI) const {
7960 SelectionDAG &DAG = DCI.DAG;
7961 SDLoc dl(N);
7962
Hal Finkel940ab932014-02-28 00:27:01 +00007963 // If we're tracking CR bits, we need to be careful that we don't have:
7964 // zext(binary-ops(trunc(x), trunc(y)))
7965 // or
7966 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
7967 // such that we're unnecessarily moving things into CR bits that can more
7968 // efficiently stay in GPRs. Note that if we're not certain that the high
7969 // bits are set as required by the final extension, we still may need to do
7970 // some masking to get the proper behavior.
7971
Hal Finkel46043ed2014-03-01 21:36:57 +00007972 // This same functionality is important on PPC64 when dealing with
7973 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
7974 // the return values of functions. Because it is so similar, it is handled
7975 // here as well.
7976
Hal Finkel940ab932014-02-28 00:27:01 +00007977 if (N->getValueType(0) != MVT::i32 &&
7978 N->getValueType(0) != MVT::i64)
7979 return SDValue();
7980
Hal Finkel46043ed2014-03-01 21:36:57 +00007981 if (!((N->getOperand(0).getValueType() == MVT::i1 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007982 Subtarget.useCRBits()) ||
Hal Finkel46043ed2014-03-01 21:36:57 +00007983 (N->getOperand(0).getValueType() == MVT::i32 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007984 Subtarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +00007985 return SDValue();
7986
7987 if (N->getOperand(0).getOpcode() != ISD::AND &&
7988 N->getOperand(0).getOpcode() != ISD::OR &&
7989 N->getOperand(0).getOpcode() != ISD::XOR &&
7990 N->getOperand(0).getOpcode() != ISD::SELECT &&
7991 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
7992 return SDValue();
7993
7994 SmallVector<SDValue, 4> Inputs;
7995 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
7996 SmallPtrSet<SDNode *, 16> Visited;
7997
7998 // Visit all inputs, collect all binary operations (and, or, xor and
7999 // select) that are all fed by truncations.
8000 while (!BinOps.empty()) {
8001 SDValue BinOp = BinOps.back();
8002 BinOps.pop_back();
8003
8004 if (!Visited.insert(BinOp.getNode()))
8005 continue;
8006
8007 PromOps.push_back(BinOp);
8008
8009 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
8010 // The condition of the select is not promoted.
8011 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
8012 continue;
8013 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
8014 continue;
8015
8016 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
8017 isa<ConstantSDNode>(BinOp.getOperand(i))) {
8018 Inputs.push_back(BinOp.getOperand(i));
8019 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
8020 BinOp.getOperand(i).getOpcode() == ISD::OR ||
8021 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
8022 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
8023 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
8024 BinOps.push_back(BinOp.getOperand(i));
8025 } else {
8026 // We have an input that is not a truncation or another binary
8027 // operation; we'll abort this transformation.
8028 return SDValue();
8029 }
8030 }
8031 }
8032
8033 // Make sure that this is a self-contained cluster of operations (which
8034 // is not quite the same thing as saying that everything has only one
8035 // use).
8036 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
8037 if (isa<ConstantSDNode>(Inputs[i]))
8038 continue;
8039
8040 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
8041 UE = Inputs[i].getNode()->use_end();
8042 UI != UE; ++UI) {
8043 SDNode *User = *UI;
8044 if (User != N && !Visited.count(User))
8045 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00008046
8047 // Make sure that we're not going to promote the non-output-value
8048 // operand(s) or SELECT or SELECT_CC.
8049 // FIXME: Although we could sometimes handle this, and it does occur in
8050 // practice that one of the condition inputs to the select is also one of
8051 // the outputs, we currently can't deal with this.
8052 if (User->getOpcode() == ISD::SELECT) {
8053 if (User->getOperand(0) == Inputs[i])
8054 return SDValue();
8055 } else if (User->getOpcode() == ISD::SELECT_CC) {
8056 if (User->getOperand(0) == Inputs[i] ||
8057 User->getOperand(1) == Inputs[i])
8058 return SDValue();
8059 }
Hal Finkel940ab932014-02-28 00:27:01 +00008060 }
8061 }
8062
8063 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
8064 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
8065 UE = PromOps[i].getNode()->use_end();
8066 UI != UE; ++UI) {
8067 SDNode *User = *UI;
8068 if (User != N && !Visited.count(User))
8069 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00008070
8071 // Make sure that we're not going to promote the non-output-value
8072 // operand(s) or SELECT or SELECT_CC.
8073 // FIXME: Although we could sometimes handle this, and it does occur in
8074 // practice that one of the condition inputs to the select is also one of
8075 // the outputs, we currently can't deal with this.
8076 if (User->getOpcode() == ISD::SELECT) {
8077 if (User->getOperand(0) == PromOps[i])
8078 return SDValue();
8079 } else if (User->getOpcode() == ISD::SELECT_CC) {
8080 if (User->getOperand(0) == PromOps[i] ||
8081 User->getOperand(1) == PromOps[i])
8082 return SDValue();
8083 }
Hal Finkel940ab932014-02-28 00:27:01 +00008084 }
8085 }
8086
Hal Finkel46043ed2014-03-01 21:36:57 +00008087 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +00008088 bool ReallyNeedsExt = false;
8089 if (N->getOpcode() != ISD::ANY_EXTEND) {
8090 // If all of the inputs are not already sign/zero extended, then
8091 // we'll still need to do that at the end.
8092 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
8093 if (isa<ConstantSDNode>(Inputs[i]))
8094 continue;
8095
8096 unsigned OpBits =
8097 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +00008098 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
8099
Hal Finkel940ab932014-02-28 00:27:01 +00008100 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
8101 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00008102 APInt::getHighBitsSet(OpBits,
8103 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +00008104 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +00008105 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
8106 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +00008107 ReallyNeedsExt = true;
8108 break;
8109 }
8110 }
8111 }
8112
8113 // Replace all inputs, either with the truncation operand, or a
8114 // truncation or extension to the final output type.
8115 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
8116 // Constant inputs need to be replaced with the to-be-promoted nodes that
8117 // use them because they might have users outside of the cluster of
8118 // promoted nodes.
8119 if (isa<ConstantSDNode>(Inputs[i]))
8120 continue;
8121
8122 SDValue InSrc = Inputs[i].getOperand(0);
8123 if (Inputs[i].getValueType() == N->getValueType(0))
8124 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
8125 else if (N->getOpcode() == ISD::SIGN_EXTEND)
8126 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
8127 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
8128 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8129 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
8130 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
8131 else
8132 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
8133 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
8134 }
8135
8136 // Replace all operations (these are all the same, but have a different
8137 // (promoted) return type). DAG.getNode will validate that the types of
8138 // a binary operator match, so go through the list in reverse so that
8139 // we've likely promoted both operands first.
8140 while (!PromOps.empty()) {
8141 SDValue PromOp = PromOps.back();
8142 PromOps.pop_back();
8143
8144 unsigned C;
8145 switch (PromOp.getOpcode()) {
8146 default: C = 0; break;
8147 case ISD::SELECT: C = 1; break;
8148 case ISD::SELECT_CC: C = 2; break;
8149 }
8150
8151 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
8152 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
8153 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
8154 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
8155 // The to-be-promoted operands of this node have not yet been
8156 // promoted (this should be rare because we're going through the
8157 // list backward, but if one of the operands has several users in
8158 // this cluster of to-be-promoted nodes, it is possible).
8159 PromOps.insert(PromOps.begin(), PromOp);
8160 continue;
8161 }
8162
8163 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
8164 PromOp.getNode()->op_end());
8165
8166 // If this node has constant inputs, then they'll need to be promoted here.
8167 for (unsigned i = 0; i < 2; ++i) {
8168 if (!isa<ConstantSDNode>(Ops[C+i]))
8169 continue;
8170 if (Ops[C+i].getValueType() == N->getValueType(0))
8171 continue;
8172
8173 if (N->getOpcode() == ISD::SIGN_EXTEND)
8174 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
8175 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8176 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
8177 else
8178 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
8179 }
8180
8181 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00008182 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00008183 }
8184
8185 // Now we're left with the initial extension itself.
8186 if (!ReallyNeedsExt)
8187 return N->getOperand(0);
8188
Hal Finkel46043ed2014-03-01 21:36:57 +00008189 // To zero extend, just mask off everything except for the first bit (in the
8190 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +00008191 if (N->getOpcode() == ISD::ZERO_EXTEND)
8192 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00008193 DAG.getConstant(APInt::getLowBitsSet(
8194 N->getValueSizeInBits(0), PromBits),
8195 N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +00008196
8197 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
8198 "Invalid extension type");
8199 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0));
8200 SDValue ShiftCst =
Hal Finkel46043ed2014-03-01 21:36:57 +00008201 DAG.getConstant(N->getValueSizeInBits(0)-PromBits, ShiftAmountTy);
Hal Finkel940ab932014-02-28 00:27:01 +00008202 return DAG.getNode(ISD::SRA, dl, N->getValueType(0),
8203 DAG.getNode(ISD::SHL, dl, N->getValueType(0),
8204 N->getOperand(0), ShiftCst), ShiftCst);
8205}
8206
Duncan Sandsdc2dac12008-11-24 14:53:14 +00008207SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
8208 DAGCombinerInfo &DCI) const {
Dan Gohman57c732b2010-04-21 01:34:56 +00008209 const TargetMachine &TM = getTargetMachine();
Chris Lattnerf4184352006-03-01 04:57:39 +00008210 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008211 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00008212 switch (N->getOpcode()) {
8213 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00008214 case PPCISD::SHL:
8215 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00008216 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00008217 return N->getOperand(0);
8218 }
8219 break;
8220 case PPCISD::SRL:
8221 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00008222 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00008223 return N->getOperand(0);
8224 }
8225 break;
8226 case PPCISD::SRA:
8227 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00008228 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00008229 C->isAllOnesValue()) // -1 >>s V -> -1.
8230 return N->getOperand(0);
8231 }
8232 break;
Hal Finkel940ab932014-02-28 00:27:01 +00008233 case ISD::SIGN_EXTEND:
8234 case ISD::ZERO_EXTEND:
8235 case ISD::ANY_EXTEND:
8236 return DAGCombineExtBoolTrunc(N, DCI);
8237 case ISD::TRUNCATE:
8238 case ISD::SETCC:
8239 case ISD::SELECT_CC:
8240 return DAGCombineTruncBoolExt(N, DCI);
Chris Lattnerf4184352006-03-01 04:57:39 +00008241 case ISD::SINT_TO_FP:
Chris Lattnera35f3062006-06-16 17:34:12 +00008242 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattner4a66d692006-03-22 05:30:33 +00008243 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
8244 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
8245 // We allow the src/dst to be either f32/f64, but the intermediate
8246 // type must be i64.
Owen Anderson9f944592009-08-11 20:47:22 +00008247 if (N->getOperand(0).getValueType() == MVT::i64 &&
8248 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008249 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00008250 if (Val.getValueType() == MVT::f32) {
8251 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008252 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00008253 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008254
Owen Anderson9f944592009-08-11 20:47:22 +00008255 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008256 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00008257 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008258 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00008259 if (N->getValueType(0) == MVT::f32) {
8260 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner72733e52008-01-17 07:00:52 +00008261 DAG.getIntPtrConstant(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00008262 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00008263 }
8264 return Val;
Owen Anderson9f944592009-08-11 20:47:22 +00008265 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattner4a66d692006-03-22 05:30:33 +00008266 // If the intermediate type is i32, we can avoid the load/store here
8267 // too.
Chris Lattnerf4184352006-03-01 04:57:39 +00008268 }
Chris Lattnerf4184352006-03-01 04:57:39 +00008269 }
8270 }
8271 break;
Chris Lattner27f53452006-03-01 05:50:56 +00008272 case ISD::STORE:
8273 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
8274 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnerf5b46f72008-01-18 16:54:56 +00008275 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00008276 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00008277 N->getOperand(1).getValueType() == MVT::i32 &&
8278 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008279 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00008280 if (Val.getValueType() == MVT::f32) {
8281 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008282 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008283 }
Owen Anderson9f944592009-08-11 20:47:22 +00008284 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008285 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008286
Hal Finkel60c75102013-04-01 15:37:53 +00008287 SDValue Ops[] = {
8288 N->getOperand(0), Val, N->getOperand(2),
8289 DAG.getValueType(N->getOperand(1).getValueType())
8290 };
8291
8292 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00008293 DAG.getVTList(MVT::Other), Ops,
Hal Finkel60c75102013-04-01 15:37:53 +00008294 cast<StoreSDNode>(N)->getMemoryVT(),
8295 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00008296 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008297 return Val;
8298 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008299
Chris Lattnera7976d32006-07-10 20:56:58 +00008300 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00008301 if (cast<StoreSDNode>(N)->isUnindexed() &&
8302 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00008303 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00008304 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00008305 N->getOperand(1).getValueType() == MVT::i16 ||
8306 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008307 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008308 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008309 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008310 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00008311 if (BSwapOp.getValueType() == MVT::i16)
8312 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00008313
Dan Gohman48b185d2009-09-25 20:36:54 +00008314 SDValue Ops[] = {
8315 N->getOperand(0), BSwapOp, N->getOperand(2),
8316 DAG.getValueType(N->getOperand(1).getValueType())
8317 };
8318 return
8319 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00008320 Ops, cast<StoreSDNode>(N)->getMemoryVT(),
Dan Gohman48b185d2009-09-25 20:36:54 +00008321 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008322 }
8323 break;
Hal Finkelcf2e9082013-05-24 23:00:14 +00008324 case ISD::LOAD: {
8325 LoadSDNode *LD = cast<LoadSDNode>(N);
8326 EVT VT = LD->getValueType(0);
8327 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
8328 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
8329 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
8330 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
Bill Schmidt2d1128a2014-10-17 15:13:38 +00008331 // P8 and later hardware should just use LOAD.
8332 !TM.getSubtarget<PPCSubtarget>().hasP8Vector() &&
Hal Finkel40c34782013-09-15 22:09:58 +00008333 (VT == MVT::v16i8 || VT == MVT::v8i16 ||
8334 VT == MVT::v4i32 || VT == MVT::v4f32) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +00008335 LD->getAlignment() < ABIAlignment) {
8336 // This is a type-legal unaligned Altivec load.
8337 SDValue Chain = LD->getChain();
8338 SDValue Ptr = LD->getBasePtr();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008339 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelcf2e9082013-05-24 23:00:14 +00008340
8341 // This implements the loading of unaligned vectors as described in
8342 // the venerable Apple Velocity Engine overview. Specifically:
8343 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
8344 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
8345 //
8346 // The general idea is to expand a sequence of one or more unaligned
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008347 // loads into an alignment-based permutation-control instruction (lvsl
8348 // or lvsr), a series of regular vector loads (which always truncate
8349 // their input address to an aligned address), and a series of
8350 // permutations. The results of these permutations are the requested
8351 // loaded values. The trick is that the last "extra" load is not taken
8352 // from the address you might suspect (sizeof(vector) bytes after the
8353 // last requested load), but rather sizeof(vector) - 1 bytes after the
8354 // last requested vector. The point of this is to avoid a page fault if
8355 // the base address happened to be aligned. This works because if the
8356 // base address is aligned, then adding less than a full vector length
8357 // will cause the last vector in the sequence to be (re)loaded.
8358 // Otherwise, the next vector will be fetched as you might suspect was
8359 // necessary.
Hal Finkelcf2e9082013-05-24 23:00:14 +00008360
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008361 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00008362 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008363 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
8364 // optimization later.
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008365 Intrinsic::ID Intr = (isLittleEndian ?
8366 Intrinsic::ppc_altivec_lvsr :
8367 Intrinsic::ppc_altivec_lvsl);
8368 SDValue PermCntl = BuildIntrinsicOp(Intr, Ptr, DAG, dl, MVT::v16i8);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008369
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008370 // Create the new MMO for the new base load. It is like the original MMO,
8371 // but represents an area in memory almost twice the vector size centered
8372 // on the original address. If the address is unaligned, we might start
8373 // reading up to (sizeof(vector)-1) bytes below the address of the
8374 // original unaligned load.
Hal Finkelcf2e9082013-05-24 23:00:14 +00008375 MachineFunction &MF = DAG.getMachineFunction();
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008376 MachineMemOperand *BaseMMO =
8377 MF.getMachineMemOperand(LD->getMemOperand(),
8378 -LD->getMemoryVT().getStoreSize()+1,
8379 2*LD->getMemoryVT().getStoreSize()-1);
8380
8381 // Create the new base load.
8382 SDValue LDXIntID = DAG.getTargetConstant(Intrinsic::ppc_altivec_lvx,
8383 getPointerTy());
8384 SDValue BaseLoadOps[] = { Chain, LDXIntID, Ptr };
8385 SDValue BaseLoad =
8386 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
8387 DAG.getVTList(MVT::v4i32, MVT::Other),
8388 BaseLoadOps, MVT::v4i32, BaseMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008389
8390 // Note that the value of IncOffset (which is provided to the next
8391 // load's pointer info offset value, and thus used to calculate the
8392 // alignment), and the value of IncValue (which is actually used to
8393 // increment the pointer value) are different! This is because we
8394 // require the next load to appear to be aligned, even though it
8395 // is actually offset from the base pointer by a lesser amount.
8396 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +00008397 int IncValue = IncOffset;
8398
8399 // Walk (both up and down) the chain looking for another load at the real
8400 // (aligned) offset (the alignment of the other load does not matter in
8401 // this case). If found, then do not use the offset reduction trick, as
8402 // that will prevent the loads from being later combined (as they would
8403 // otherwise be duplicates).
8404 if (!findConsecutiveLoad(LD, DAG))
8405 --IncValue;
8406
Hal Finkelcf2e9082013-05-24 23:00:14 +00008407 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
8408 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
8409
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008410 MachineMemOperand *ExtraMMO =
8411 MF.getMachineMemOperand(LD->getMemOperand(),
8412 1, 2*LD->getMemoryVT().getStoreSize()-1);
8413 SDValue ExtraLoadOps[] = { Chain, LDXIntID, Ptr };
Hal Finkelcf2e9082013-05-24 23:00:14 +00008414 SDValue ExtraLoad =
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008415 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
8416 DAG.getVTList(MVT::v4i32, MVT::Other),
8417 ExtraLoadOps, MVT::v4i32, ExtraMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008418
8419 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8420 BaseLoad.getValue(1), ExtraLoad.getValue(1));
8421
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008422 // Because vperm has a big-endian bias, we must reverse the order
8423 // of the input vectors and complement the permute control vector
8424 // when generating little endian code. We have already handled the
8425 // latter by using lvsr instead of lvsl, so just reverse BaseLoad
8426 // and ExtraLoad here.
8427 SDValue Perm;
8428 if (isLittleEndian)
8429 Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
8430 ExtraLoad, BaseLoad, PermCntl, DAG, dl);
8431 else
8432 Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
8433 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008434
8435 if (VT != MVT::v4i32)
8436 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
8437
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008438 // The output of the permutation is our loaded result, the TokenFactor is
8439 // our new chain.
8440 DCI.CombineTo(N, Perm, TF);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008441 return SDValue(N, 0);
8442 }
8443 }
8444 break;
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008445 case ISD::INTRINSIC_WO_CHAIN: {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008446 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008447 Intrinsic::ID Intr = (isLittleEndian ?
8448 Intrinsic::ppc_altivec_lvsr :
8449 Intrinsic::ppc_altivec_lvsl);
8450 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() == Intr &&
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008451 N->getOperand(1)->getOpcode() == ISD::ADD) {
8452 SDValue Add = N->getOperand(1);
8453
8454 if (DAG.MaskedValueIsZero(Add->getOperand(1),
8455 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
8456 Add.getValueType().getScalarType().getSizeInBits()))) {
8457 SDNode *BasePtr = Add->getOperand(0).getNode();
8458 for (SDNode::use_iterator UI = BasePtr->use_begin(),
8459 UE = BasePtr->use_end(); UI != UE; ++UI) {
8460 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8461 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008462 Intr) {
8463 // We've found another LVSL/LVSR, and this address is an aligned
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008464 // multiple of that one. The results will be the same, so use the
8465 // one we've just found instead.
8466
8467 return SDValue(*UI, 0);
8468 }
8469 }
8470 }
8471 }
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008472 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +00008473
8474 break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008475 case ISD::BSWAP:
8476 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008477 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +00008478 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008479 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
8480 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008481 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008482 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008483 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00008484 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +00008485 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008486 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +00008487 LD->getChain(), // Chain
8488 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008489 DAG.getValueType(N->getValueType(0)) // VT
8490 };
Dan Gohman48b185d2009-09-25 20:36:54 +00008491 SDValue BSLoad =
8492 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +00008493 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
8494 MVT::i64 : MVT::i32, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00008495 Ops, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008496
Scott Michelcf0da6c2009-02-17 22:15:04 +00008497 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008498 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +00008499 if (N->getValueType(0) == MVT::i16)
8500 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008501
Chris Lattnera7976d32006-07-10 20:56:58 +00008502 // First, combine the bswap away. This makes the value produced by the
8503 // load dead.
8504 DCI.CombineTo(N, ResVal);
8505
8506 // Next, combine the load away, we give it a bogus result value but a real
8507 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008508 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00008509
Chris Lattnera7976d32006-07-10 20:56:58 +00008510 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008511 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008512 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008513
Chris Lattner27f53452006-03-01 05:50:56 +00008514 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008515 case PPCISD::VCMP: {
8516 // If a VCMPo node already exists with exactly the same operands as this
8517 // node, use its result instead of this node (VCMPo computes both a CR6 and
8518 // a normal output).
8519 //
8520 if (!N->getOperand(0).hasOneUse() &&
8521 !N->getOperand(1).hasOneUse() &&
8522 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00008523
Chris Lattnerd4058a52006-03-31 06:02:07 +00008524 // Scan all of the users of the LHS, looking for VCMPo's that match.
Craig Topper062a2ba2014-04-25 05:30:21 +00008525 SDNode *VCMPoNode = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008526
Gabor Greiff304a7a2008-08-28 21:40:38 +00008527 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +00008528 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
8529 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008530 if (UI->getOpcode() == PPCISD::VCMPo &&
8531 UI->getOperand(1) == N->getOperand(1) &&
8532 UI->getOperand(2) == N->getOperand(2) &&
8533 UI->getOperand(0) == N->getOperand(0)) {
8534 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008535 break;
8536 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008537
Chris Lattner518834c2006-04-18 18:28:22 +00008538 // If there is no VCMPo node, or if the flag value has a single use, don't
8539 // transform this.
8540 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
8541 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008542
8543 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +00008544 // chain, this transformation is more complex. Note that multiple things
8545 // could use the value result, which we should ignore.
Craig Topper062a2ba2014-04-25 05:30:21 +00008546 SDNode *FlagUser = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008547 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Craig Topper062a2ba2014-04-25 05:30:21 +00008548 FlagUser == nullptr; ++UI) {
Chris Lattner518834c2006-04-18 18:28:22 +00008549 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008550 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +00008551 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008552 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +00008553 FlagUser = User;
8554 break;
8555 }
8556 }
8557 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008558
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008559 // If the user is a MFOCRF instruction, we know this is safe.
8560 // Otherwise we give up for right now.
8561 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008562 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +00008563 }
8564 break;
8565 }
Hal Finkel940ab932014-02-28 00:27:01 +00008566 case ISD::BRCOND: {
8567 SDValue Cond = N->getOperand(1);
8568 SDValue Target = N->getOperand(2);
8569
8570 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8571 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
8572 Intrinsic::ppc_is_decremented_ctr_nonzero) {
8573
8574 // We now need to make the intrinsic dead (it cannot be instruction
8575 // selected).
8576 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
8577 assert(Cond.getNode()->hasOneUse() &&
8578 "Counter decrement has more than one use");
8579
8580 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
8581 N->getOperand(0), Target);
8582 }
8583 }
8584 break;
Chris Lattner9754d142006-04-18 17:59:36 +00008585 case ISD::BR_CC: {
8586 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008587 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +00008588 // lowering is done pre-legalize, because the legalizer lowers the predicate
8589 // compare down to code that is difficult to reassemble.
8590 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008591 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +00008592
8593 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
8594 // value. If so, pass-through the AND to get to the intrinsic.
8595 if (LHS.getOpcode() == ISD::AND &&
8596 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8597 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
8598 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8599 isa<ConstantSDNode>(LHS.getOperand(1)) &&
8600 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
8601 isZero())
8602 LHS = LHS.getOperand(0);
8603
8604 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8605 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
8606 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8607 isa<ConstantSDNode>(RHS)) {
8608 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
8609 "Counter decrement comparison is not EQ or NE");
8610
8611 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
8612 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
8613 (CC == ISD::SETNE && !Val);
8614
8615 // We now need to make the intrinsic dead (it cannot be instruction
8616 // selected).
8617 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
8618 assert(LHS.getNode()->hasOneUse() &&
8619 "Counter decrement has more than one use");
8620
8621 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
8622 N->getOperand(0), N->getOperand(4));
8623 }
8624
Chris Lattner9754d142006-04-18 17:59:36 +00008625 int CompareOpc;
8626 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008627
Chris Lattner9754d142006-04-18 17:59:36 +00008628 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8629 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
8630 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
8631 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00008632
Chris Lattner9754d142006-04-18 17:59:36 +00008633 // If this is a comparison against something other than 0/1, then we know
8634 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008635 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00008636 if (Val != 0 && Val != 1) {
8637 if (CC == ISD::SETEQ) // Cond never true, remove branch.
8638 return N->getOperand(0);
8639 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +00008640 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +00008641 N->getOperand(0), N->getOperand(4));
8642 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008643
Chris Lattner9754d142006-04-18 17:59:36 +00008644 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008645
Chris Lattner9754d142006-04-18 17:59:36 +00008646 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008647 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008648 LHS.getOperand(2), // LHS of compare
8649 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +00008650 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008651 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00008652 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00008653 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008654
Chris Lattner9754d142006-04-18 17:59:36 +00008655 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008656 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +00008657 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +00008658 default: // Can't happen, don't crash on invalid number though.
8659 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008660 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +00008661 break;
8662 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008663 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +00008664 break;
8665 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008666 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +00008667 break;
8668 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008669 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +00008670 break;
8671 }
8672
Owen Anderson9f944592009-08-11 20:47:22 +00008673 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
8674 DAG.getConstant(CompOpc, MVT::i32),
8675 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +00008676 N->getOperand(4), CompNode.getValue(1));
8677 }
8678 break;
8679 }
Chris Lattnerf4184352006-03-01 04:57:39 +00008680 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008681
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008682 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +00008683}
8684
Chris Lattner4211ca92006-04-14 06:01:58 +00008685//===----------------------------------------------------------------------===//
8686// Inline Assembly Support
8687//===----------------------------------------------------------------------===//
8688
Jay Foada0653a32014-05-14 21:14:37 +00008689void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
8690 APInt &KnownZero,
8691 APInt &KnownOne,
8692 const SelectionDAG &DAG,
8693 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00008694 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +00008695 switch (Op.getOpcode()) {
8696 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008697 case PPCISD::LBRX: {
8698 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +00008699 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +00008700 KnownZero = 0xFFFF0000;
8701 break;
8702 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008703 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00008704 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +00008705 default: break;
8706 case Intrinsic::ppc_altivec_vcmpbfp_p:
8707 case Intrinsic::ppc_altivec_vcmpeqfp_p:
8708 case Intrinsic::ppc_altivec_vcmpequb_p:
8709 case Intrinsic::ppc_altivec_vcmpequh_p:
8710 case Intrinsic::ppc_altivec_vcmpequw_p:
8711 case Intrinsic::ppc_altivec_vcmpgefp_p:
8712 case Intrinsic::ppc_altivec_vcmpgtfp_p:
8713 case Intrinsic::ppc_altivec_vcmpgtsb_p:
8714 case Intrinsic::ppc_altivec_vcmpgtsh_p:
8715 case Intrinsic::ppc_altivec_vcmpgtsw_p:
8716 case Intrinsic::ppc_altivec_vcmpgtub_p:
8717 case Intrinsic::ppc_altivec_vcmpgtuh_p:
8718 case Intrinsic::ppc_altivec_vcmpgtuw_p:
8719 KnownZero = ~1U; // All bits but the low one are known to be zero.
8720 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008721 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008722 }
8723 }
8724}
8725
8726
Chris Lattnerd6855142007-03-25 02:14:49 +00008727/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +00008728/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008729PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00008730PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
8731 if (Constraint.size() == 1) {
8732 switch (Constraint[0]) {
8733 default: break;
8734 case 'b':
8735 case 'r':
8736 case 'f':
8737 case 'v':
8738 case 'y':
8739 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +00008740 case 'Z':
8741 // FIXME: While Z does indicate a memory constraint, it specifically
8742 // indicates an r+r address (used in conjunction with the 'y' modifier
8743 // in the replacement string). Currently, we're forcing the base
8744 // register to be r0 in the asm printer (which is interpreted as zero)
8745 // and forming the complete address in the second register. This is
8746 // suboptimal.
8747 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +00008748 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008749 } else if (Constraint == "wc") { // individual CR bits.
8750 return C_RegisterClass;
Hal Finkel27774d92014-03-13 07:58:58 +00008751 } else if (Constraint == "wa" || Constraint == "wd" ||
8752 Constraint == "wf" || Constraint == "ws") {
8753 return C_RegisterClass; // VSX registers.
Chris Lattnerd6855142007-03-25 02:14:49 +00008754 }
8755 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +00008756}
8757
John Thompsone8360b72010-10-29 17:29:13 +00008758/// Examine constraint type and operand type and determine a weight value.
8759/// This object must already have been set up with the operand type
8760/// and the current alternative constraint selected.
8761TargetLowering::ConstraintWeight
8762PPCTargetLowering::getSingleConstraintMatchWeight(
8763 AsmOperandInfo &info, const char *constraint) const {
8764 ConstraintWeight weight = CW_Invalid;
8765 Value *CallOperandVal = info.CallOperandVal;
8766 // If we don't have a value, we can't do a match,
8767 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00008768 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00008769 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00008770 Type *type = CallOperandVal->getType();
Hal Finkel6aca2372014-03-02 18:23:39 +00008771
John Thompsone8360b72010-10-29 17:29:13 +00008772 // Look at the constraint type.
Hal Finkel6aca2372014-03-02 18:23:39 +00008773 if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
8774 return CW_Register; // an individual CR bit.
Hal Finkel27774d92014-03-13 07:58:58 +00008775 else if ((StringRef(constraint) == "wa" ||
8776 StringRef(constraint) == "wd" ||
8777 StringRef(constraint) == "wf") &&
8778 type->isVectorTy())
8779 return CW_Register;
8780 else if (StringRef(constraint) == "ws" && type->isDoubleTy())
8781 return CW_Register;
Hal Finkel6aca2372014-03-02 18:23:39 +00008782
John Thompsone8360b72010-10-29 17:29:13 +00008783 switch (*constraint) {
8784 default:
8785 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8786 break;
8787 case 'b':
8788 if (type->isIntegerTy())
8789 weight = CW_Register;
8790 break;
8791 case 'f':
8792 if (type->isFloatTy())
8793 weight = CW_Register;
8794 break;
8795 case 'd':
8796 if (type->isDoubleTy())
8797 weight = CW_Register;
8798 break;
8799 case 'v':
8800 if (type->isVectorTy())
8801 weight = CW_Register;
8802 break;
8803 case 'y':
8804 weight = CW_Register;
8805 break;
Hal Finkel4f24c622012-11-05 18:18:42 +00008806 case 'Z':
8807 weight = CW_Memory;
8808 break;
John Thompsone8360b72010-10-29 17:29:13 +00008809 }
8810 return weight;
8811}
8812
Scott Michelcf0da6c2009-02-17 22:15:04 +00008813std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner584a11a2006-11-02 01:44:04 +00008814PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00008815 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +00008816 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +00008817 // GCC RS6000 Constraint Letters
8818 switch (Constraint[0]) {
8819 case 'b': // R1-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008820 if (VT == MVT::i64 && Subtarget.isPPC64())
Hal Finkel638a9fa2013-03-19 18:51:05 +00008821 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
8822 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008823 case 'r': // R0-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008824 if (VT == MVT::i64 && Subtarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +00008825 return std::make_pair(0U, &PPC::G8RCRegClass);
8826 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008827 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008828 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +00008829 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008830 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +00008831 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008832 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008833 case 'v':
Craig Topperabadc662012-04-20 06:31:50 +00008834 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008835 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +00008836 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008837 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008838 } else if (Constraint == "wc") { // an individual CR bit.
8839 return std::make_pair(0U, &PPC::CRBITRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +00008840 } else if (Constraint == "wa" || Constraint == "wd" ||
Hal Finkel19be5062014-03-29 05:29:01 +00008841 Constraint == "wf") {
Hal Finkel27774d92014-03-13 07:58:58 +00008842 return std::make_pair(0U, &PPC::VSRCRegClass);
Hal Finkel19be5062014-03-29 05:29:01 +00008843 } else if (Constraint == "ws") {
8844 return std::make_pair(0U, &PPC::VSFRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008845 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008846
Hal Finkelb176acb2013-08-03 12:25:10 +00008847 std::pair<unsigned, const TargetRegisterClass*> R =
8848 TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8849
8850 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
8851 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
8852 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
8853 // register.
8854 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
8855 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008856 if (R.first && VT == MVT::i64 && Subtarget.isPPC64() &&
Hal Finkelb176acb2013-08-03 12:25:10 +00008857 PPC::GPRCRegClass.contains(R.first)) {
Eric Christopherd9134482014-08-04 21:25:23 +00008858 const TargetRegisterInfo *TRI =
8859 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Hal Finkelb176acb2013-08-03 12:25:10 +00008860 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +00008861 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +00008862 &PPC::G8RCRegClass);
8863 }
8864
8865 return R;
Chris Lattner01513612006-01-31 19:20:21 +00008866}
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008867
Chris Lattner584a11a2006-11-02 01:44:04 +00008868
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008869/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +00008870/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +00008871void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00008872 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008873 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00008874 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00008875 SDValue Result;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008876
Eric Christopherde9399b2011-06-02 23:16:42 +00008877 // Only support length 1 constraints.
8878 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008879
Eric Christopherde9399b2011-06-02 23:16:42 +00008880 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008881 switch (Letter) {
8882 default: break;
8883 case 'I':
8884 case 'J':
8885 case 'K':
8886 case 'L':
8887 case 'M':
8888 case 'N':
8889 case 'O':
8890 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +00008891 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008892 if (!CST) return; // Must be an immediate to match.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008893 unsigned Value = CST->getZExtValue();
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008894 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008895 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008896 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008897 if ((short)Value == (int)Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008898 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008899 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008900 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
8901 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008902 if ((short)Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008903 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008904 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008905 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008906 if ((Value >> 16) == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008907 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008908 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008909 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008910 if (Value > 31)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008911 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008912 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008913 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008914 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008915 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008916 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008917 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008918 if (Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008919 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008920 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008921 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008922 if ((short)-Value == (int)-Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008923 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008924 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008925 }
8926 break;
8927 }
8928 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008929
Gabor Greiff304a7a2008-08-28 21:40:38 +00008930 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008931 Ops.push_back(Result);
8932 return;
8933 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008934
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008935 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +00008936 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008937}
Evan Cheng2dd2c652006-03-13 23:20:37 +00008938
Chris Lattner1eb94d92007-03-30 23:15:24 +00008939// isLegalAddressingMode - Return true if the addressing mode represented
8940// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008941bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00008942 Type *Ty) const {
Chris Lattner1eb94d92007-03-30 23:15:24 +00008943 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelcf0da6c2009-02-17 22:15:04 +00008944
Chris Lattner1eb94d92007-03-30 23:15:24 +00008945 // PPC allows a sign-extended 16-bit immediate field.
8946 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
8947 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008948
Chris Lattner1eb94d92007-03-30 23:15:24 +00008949 // No global is ever allowed as a base.
8950 if (AM.BaseGV)
8951 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008952
8953 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +00008954 switch (AM.Scale) {
8955 case 0: // "r+i" or just "i", depending on HasBaseReg.
8956 break;
8957 case 1:
8958 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
8959 return false;
8960 // Otherwise we have r+r or r+i.
8961 break;
8962 case 2:
8963 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
8964 return false;
8965 // Allow 2*r as r+r.
8966 break;
Chris Lattner19ccd622007-04-09 22:10:05 +00008967 default:
8968 // No other scales are supported.
8969 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +00008970 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008971
Chris Lattner1eb94d92007-03-30 23:15:24 +00008972 return true;
8973}
8974
Dan Gohman21cea8a2010-04-17 15:26:15 +00008975SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
8976 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00008977 MachineFunction &MF = DAG.getMachineFunction();
8978 MachineFrameInfo *MFI = MF.getFrameInfo();
8979 MFI->setReturnAddressIsTaken(true);
8980
Bill Wendling908bf812014-01-06 00:43:20 +00008981 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008982 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008983
Andrew Trickef9de2a2013-05-25 02:42:55 +00008984 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008985 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +00008986
Dale Johannesen81bfca72010-05-03 22:59:34 +00008987 // Make sure the function does not optimize away the store of the RA to
8988 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +00008989 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008990 FuncInfo->setLRStoreRequired();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008991 bool isPPC64 = Subtarget.isPPC64();
8992 bool isDarwinABI = Subtarget.isDarwinABI();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008993
8994 if (Depth > 0) {
8995 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8996 SDValue Offset =
Wesley Peck527da1b2010-11-23 03:31:01 +00008997
Anton Korobeynikov2f931282011-01-10 12:39:04 +00008998 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen81bfca72010-05-03 22:59:34 +00008999 isPPC64? MVT::i64 : MVT::i32);
9000 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
9001 DAG.getNode(ISD::ADD, dl, getPointerTy(),
9002 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009003 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009004 }
Chris Lattnerf6a81562007-12-08 06:59:59 +00009005
Chris Lattnerf6a81562007-12-08 06:59:59 +00009006 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009007 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009008 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009009 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +00009010}
9011
Dan Gohman21cea8a2010-04-17 15:26:15 +00009012SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
9013 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009014 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009015 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00009016
Owen Anderson53aa7a92009-08-10 22:56:29 +00009017 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00009018 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009019
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00009020 MachineFunction &MF = DAG.getMachineFunction();
9021 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +00009022 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +00009023
9024 // Naked functions never have a frame pointer, and so we use r1. For all
9025 // other functions, this decision must be delayed until during PEI.
9026 unsigned FrameReg;
9027 if (MF.getFunction()->getAttributes().hasAttribute(
9028 AttributeSet::FunctionIndex, Attribute::Naked))
9029 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
9030 else
9031 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
9032
Dale Johannesen81bfca72010-05-03 22:59:34 +00009033 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
9034 PtrVT);
9035 while (Depth--)
9036 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009037 FrameAddr, MachinePointerInfo(), false, false,
9038 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009039 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00009040}
Dan Gohmanc14e5222008-10-21 03:41:46 +00009041
Hal Finkel0d8db462014-05-11 19:29:11 +00009042// FIXME? Maybe this could be a TableGen attribute on some registers and
9043// this table could be generated automatically from RegInfo.
9044unsigned PPCTargetLowering::getRegisterByName(const char* RegName,
9045 EVT VT) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009046 bool isPPC64 = Subtarget.isPPC64();
9047 bool isDarwinABI = Subtarget.isDarwinABI();
Hal Finkel0d8db462014-05-11 19:29:11 +00009048
9049 if ((isPPC64 && VT != MVT::i64 && VT != MVT::i32) ||
9050 (!isPPC64 && VT != MVT::i32))
9051 report_fatal_error("Invalid register global variable type");
9052
9053 bool is64Bit = isPPC64 && VT == MVT::i64;
9054 unsigned Reg = StringSwitch<unsigned>(RegName)
9055 .Case("r1", is64Bit ? PPC::X1 : PPC::R1)
9056 .Case("r2", isDarwinABI ? 0 : (is64Bit ? PPC::X2 : PPC::R2))
9057 .Case("r13", (!isPPC64 && isDarwinABI) ? 0 :
9058 (is64Bit ? PPC::X13 : PPC::R13))
9059 .Default(0);
9060
9061 if (Reg)
9062 return Reg;
9063 report_fatal_error("Invalid register name global variable");
9064}
9065
Dan Gohmanc14e5222008-10-21 03:41:46 +00009066bool
9067PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9068 // The PowerPC target isn't yet aware of offsets.
9069 return false;
9070}
Tilmann Schellerb93960d2009-07-03 06:45:56 +00009071
Hal Finkel46ef7ce2014-08-13 01:15:40 +00009072bool PPCTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9073 const CallInst &I,
9074 unsigned Intrinsic) const {
9075
9076 switch (Intrinsic) {
9077 case Intrinsic::ppc_altivec_lvx:
9078 case Intrinsic::ppc_altivec_lvxl:
9079 case Intrinsic::ppc_altivec_lvebx:
9080 case Intrinsic::ppc_altivec_lvehx:
9081 case Intrinsic::ppc_altivec_lvewx: {
9082 EVT VT;
9083 switch (Intrinsic) {
9084 case Intrinsic::ppc_altivec_lvebx:
9085 VT = MVT::i8;
9086 break;
9087 case Intrinsic::ppc_altivec_lvehx:
9088 VT = MVT::i16;
9089 break;
9090 case Intrinsic::ppc_altivec_lvewx:
9091 VT = MVT::i32;
9092 break;
9093 default:
9094 VT = MVT::v4i32;
9095 break;
9096 }
9097
9098 Info.opc = ISD::INTRINSIC_W_CHAIN;
9099 Info.memVT = VT;
9100 Info.ptrVal = I.getArgOperand(0);
9101 Info.offset = -VT.getStoreSize()+1;
9102 Info.size = 2*VT.getStoreSize()-1;
9103 Info.align = 1;
9104 Info.vol = false;
9105 Info.readMem = true;
9106 Info.writeMem = false;
9107 return true;
9108 }
9109 case Intrinsic::ppc_altivec_stvx:
9110 case Intrinsic::ppc_altivec_stvxl:
9111 case Intrinsic::ppc_altivec_stvebx:
9112 case Intrinsic::ppc_altivec_stvehx:
9113 case Intrinsic::ppc_altivec_stvewx: {
9114 EVT VT;
9115 switch (Intrinsic) {
9116 case Intrinsic::ppc_altivec_stvebx:
9117 VT = MVT::i8;
9118 break;
9119 case Intrinsic::ppc_altivec_stvehx:
9120 VT = MVT::i16;
9121 break;
9122 case Intrinsic::ppc_altivec_stvewx:
9123 VT = MVT::i32;
9124 break;
9125 default:
9126 VT = MVT::v4i32;
9127 break;
9128 }
9129
9130 Info.opc = ISD::INTRINSIC_VOID;
9131 Info.memVT = VT;
9132 Info.ptrVal = I.getArgOperand(1);
9133 Info.offset = -VT.getStoreSize()+1;
9134 Info.size = 2*VT.getStoreSize()-1;
9135 Info.align = 1;
9136 Info.vol = false;
9137 Info.readMem = false;
9138 Info.writeMem = true;
9139 return true;
9140 }
9141 default:
9142 break;
9143 }
9144
9145 return false;
9146}
9147
Evan Chengd9929f02010-04-01 20:10:42 +00009148/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +00009149/// and store operations as a result of memset, memcpy, and memmove
9150/// lowering. If DstAlign is zero that means it's safe to destination
9151/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
9152/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +00009153/// probably because the source does not need to be loaded. If 'IsMemset' is
9154/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
9155/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
9156/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +00009157/// It returns EVT::Other if the type should be determined using generic
9158/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +00009159EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
9160 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009161 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00009162 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +00009163 MachineFunction &MF) const {
Eric Christopherd90a8742014-06-12 22:38:20 +00009164 if (Subtarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +00009165 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00009166 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00009167 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00009168 }
9169}
Hal Finkel88ed4e32012-04-01 19:23:08 +00009170
Hal Finkel34974ed2014-04-12 21:52:38 +00009171/// \brief Returns true if it is beneficial to convert a load of a constant
9172/// to just the constant itself.
9173bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
9174 Type *Ty) const {
9175 assert(Ty->isIntegerTy());
9176
9177 unsigned BitSize = Ty->getPrimitiveSizeInBits();
9178 if (BitSize == 0 || BitSize > 64)
9179 return false;
9180 return true;
9181}
9182
9183bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
9184 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9185 return false;
9186 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9187 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
9188 return NumBits1 == 64 && NumBits2 == 32;
9189}
9190
9191bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
9192 if (!VT1.isInteger() || !VT2.isInteger())
9193 return false;
9194 unsigned NumBits1 = VT1.getSizeInBits();
9195 unsigned NumBits2 = VT2.getSizeInBits();
9196 return NumBits1 == 64 && NumBits2 == 32;
9197}
9198
9199bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
9200 return isInt<16>(Imm) || isUInt<16>(Imm);
9201}
9202
9203bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const {
9204 return isInt<16>(Imm) || isUInt<16>(Imm);
9205}
9206
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009207bool PPCTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
9208 unsigned,
9209 unsigned,
9210 bool *Fast) const {
Hal Finkel8d7fbc92013-03-15 15:27:13 +00009211 if (DisablePPCUnaligned)
9212 return false;
9213
9214 // PowerPC supports unaligned memory access for simple non-vector types.
9215 // Although accessing unaligned addresses is not as efficient as accessing
9216 // aligned addresses, it is generally more efficient than manual expansion,
9217 // and generally only traps for software emulation when crossing page
9218 // boundaries.
9219
9220 if (!VT.isSimple())
9221 return false;
9222
Hal Finkel6e28e6a2014-03-26 19:39:09 +00009223 if (VT.getSimpleVT().isVector()) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009224 if (Subtarget.hasVSX()) {
Bill Schmidt2d1128a2014-10-17 15:13:38 +00009225 if (VT != MVT::v2f64 && VT != MVT::v2i64 &&
9226 VT != MVT::v4f32 && VT != MVT::v4i32)
Hal Finkel6e28e6a2014-03-26 19:39:09 +00009227 return false;
9228 } else {
9229 return false;
9230 }
9231 }
Hal Finkel8d7fbc92013-03-15 15:27:13 +00009232
9233 if (VT == MVT::ppcf128)
9234 return false;
9235
9236 if (Fast)
9237 *Fast = true;
9238
9239 return true;
9240}
9241
Stephen Lin73de7bf2013-07-09 18:16:56 +00009242bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
9243 VT = VT.getScalarType();
9244
Hal Finkel0a479ae2012-06-22 00:49:52 +00009245 if (!VT.isSimple())
9246 return false;
9247
9248 switch (VT.getSimpleVT().SimpleTy) {
9249 case MVT::f32:
9250 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +00009251 return true;
9252 default:
9253 break;
9254 }
9255
9256 return false;
9257}
9258
Hal Finkelb4240ca2014-03-31 17:48:16 +00009259bool
9260PPCTargetLowering::shouldExpandBuildVectorWithShuffles(
9261 EVT VT , unsigned DefinedValues) const {
9262 if (VT == MVT::v2i64)
9263 return false;
9264
9265 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
9266}
9267
Hal Finkel88ed4e32012-04-01 19:23:08 +00009268Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009269 if (DisableILPPref || Subtarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +00009270 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +00009271
Hal Finkel4e9f1a82012-06-10 19:32:29 +00009272 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +00009273}
9274
Bill Schmidt0cf702f2013-07-30 00:50:39 +00009275// Create a fast isel object.
9276FastISel *
9277PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
9278 const TargetLibraryInfo *LibInfo) const {
9279 return PPC::createFastISel(FuncInfo, LibInfo);
9280}