blob: fbcc34ee6557c598fa6e981547b355ca7cb1bbc3 [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 Christopherf6ed33e2014-10-01 21:36:28 +000058PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM)
Aditya Nandakumar30531552014-11-13 21:29:21 +000059 : TargetLowering(TM),
Eric Christopherb1aaebe2014-06-12 22:38:18 +000060 Subtarget(*TM.getSubtargetImpl()) {
Sanjay Patel2cdea4c2014-08-21 22:31:48 +000061 setPow2SDivIsCheap();
Dale Johannesenc31eb202008-07-31 18:13:12 +000062
Chris Lattnera028e7a2005-09-27 22:18:25 +000063 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000064 setUseUnderscoreSetJmp(true);
65 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000066
Chris Lattnerd10babf2010-10-10 18:34:00 +000067 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
68 // arguments are at least 4/8 bytes aligned.
Eric Christopherb1aaebe2014-06-12 22:38:18 +000069 bool isPPC64 = Subtarget.isPPC64();
Evan Cheng39e90022012-07-02 22:39:56 +000070 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000071
Chris Lattnerf22556d2005-08-16 17:14:42 +000072 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000073 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
74 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
75 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +000076
Evan Cheng5d9fd972006-10-04 00:56:09 +000077 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson9f944592009-08-11 20:47:22 +000078 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
79 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sands95d46ef2008-01-23 20:39:46 +000080
Owen Anderson9f944592009-08-11 20:47:22 +000081 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000082
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000083 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000084 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
85 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
86 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
87 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
88 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
89 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
90 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
91 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
92 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
93 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +000094
Eric Christopherb1aaebe2014-06-12 22:38:18 +000095 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +000096 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
97
Eric Christopherb1aaebe2014-06-12 22:38:18 +000098 if (isPPC64 || Subtarget.hasFPCVT()) {
Hal Finkel6a56b212014-03-05 22:14:00 +000099 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
100 AddPromotedToType (ISD::SINT_TO_FP, MVT::i1,
101 isPPC64 ? MVT::i64 : MVT::i32);
102 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
103 AddPromotedToType (ISD::UINT_TO_FP, MVT::i1,
104 isPPC64 ? MVT::i64 : MVT::i32);
105 } else {
106 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom);
107 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom);
108 }
Hal Finkel940ab932014-02-28 00:27:01 +0000109
110 // PowerPC does not support direct load / store of condition registers
111 setOperationAction(ISD::LOAD, MVT::i1, Custom);
112 setOperationAction(ISD::STORE, MVT::i1, Custom);
113
114 // FIXME: Remove this once the ANDI glue bug is fixed:
115 if (ANDIGlueBug)
116 setOperationAction(ISD::TRUNCATE, MVT::i1, Custom);
117
118 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
119 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
120 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
121 setTruncStoreAction(MVT::i32, MVT::i1, Expand);
122 setTruncStoreAction(MVT::i16, MVT::i1, Expand);
123 setTruncStoreAction(MVT::i8, MVT::i1, Expand);
124
125 addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass);
126 }
127
Dale Johannesen666323e2007-10-10 01:01:31 +0000128 // This is used in the ppcf128->int sequence. Note it has different semantics
129 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +0000130 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000131
Roman Divacky1faf5b02012-08-16 18:19:29 +0000132 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000133 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
134 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
135 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
136 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
137 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000138 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000139
Chris Lattnerf22556d2005-08-16 17:14:42 +0000140 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000141 setOperationAction(ISD::SREM, MVT::i32, Expand);
142 setOperationAction(ISD::UREM, MVT::i32, Expand);
143 setOperationAction(ISD::SREM, MVT::i64, Expand);
144 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000145
146 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000147 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
148 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
149 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
150 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
151 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
152 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
153 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
154 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000155
Dan Gohman482732a2007-10-11 23:21:31 +0000156 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000157 setOperationAction(ISD::FSIN , MVT::f64, Expand);
158 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000159 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000160 setOperationAction(ISD::FREM , MVT::f64, Expand);
161 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000162 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000163 setOperationAction(ISD::FSIN , MVT::f32, Expand);
164 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000165 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000166 setOperationAction(ISD::FREM , MVT::f32, Expand);
167 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000168 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000169
Owen Anderson9f944592009-08-11 20:47:22 +0000170 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000171
Chris Lattnerf22556d2005-08-16 17:14:42 +0000172 // If we're enabling GP optimizations, use hardware square root
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000173 if (!Subtarget.hasFSQRT() &&
Hal Finkel2e103312013-04-03 04:01:11 +0000174 !(TM.Options.UnsafeFPMath &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000175 Subtarget.hasFRSQRTE() && Subtarget.hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000176 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000177
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000178 if (!Subtarget.hasFSQRT() &&
Hal Finkel2e103312013-04-03 04:01:11 +0000179 !(TM.Options.UnsafeFPMath &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000180 Subtarget.hasFRSQRTES() && Subtarget.hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000181 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000182
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000183 if (Subtarget.hasFCPSGN()) {
Hal Finkeldbc78e12013-08-19 05:01:02 +0000184 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
185 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
186 } else {
187 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
188 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
189 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000190
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000191 if (Subtarget.hasFPRND()) {
Hal Finkelc20a08d2013-03-29 08:57:48 +0000192 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
193 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
194 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000195 setOperationAction(ISD::FROUND, MVT::f64, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000196
197 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
198 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
199 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000200 setOperationAction(ISD::FROUND, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000201 }
202
Nate Begeman2fba8a32006-01-14 03:14:10 +0000203 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000204 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000205 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000206 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
207 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000208 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000209 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000210 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
211 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000212
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000213 if (Subtarget.hasPOPCNTD()) {
Hal Finkel290376d2013-04-01 15:58:15 +0000214 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000215 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
216 } else {
217 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
218 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
219 }
220
Nate Begeman1b8121b2006-01-11 21:21:00 +0000221 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000222 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
223 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000224
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000225 if (!Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000226 // PowerPC does not have Select
227 setOperationAction(ISD::SELECT, MVT::i32, Expand);
228 setOperationAction(ISD::SELECT, MVT::i64, Expand);
229 setOperationAction(ISD::SELECT, MVT::f32, Expand);
230 setOperationAction(ISD::SELECT, MVT::f64, Expand);
231 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000232
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000233 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000234 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
235 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000236
Nate Begeman7e7f4392006-02-01 07:19:44 +0000237 // PowerPC wants to optimize integer setcc a bit
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000238 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000239 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000240
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000241 // PowerPC does not have BRCOND which requires SetCC
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000242 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000243 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000244
Owen Anderson9f944592009-08-11 20:47:22 +0000245 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000246
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000247 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000248 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000249
Jim Laskey6267b2c2005-08-17 00:40:22 +0000250 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000251 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
252 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000253
Wesley Peck527da1b2010-11-23 03:31:01 +0000254 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
255 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
256 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
257 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattnerc46fc242005-12-23 05:13:35 +0000258
Chris Lattner84b49d52006-04-28 21:56:10 +0000259 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000260 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000261
Hal Finkel1996f3d2013-03-27 19:10:42 +0000262 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000263 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
264 // support continuation, user-level threading, and etc.. As a result, no
265 // other SjLj exception interfaces are implemented and please don't build
266 // your own exception handling based on them.
267 // LLVM/Clang supports zero-cost DWARF exception handling.
268 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
269 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000270
271 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000272 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000273 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
274 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000275 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000276 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
277 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
278 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
279 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000280 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000281 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
282 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000283
Nate Begemanf69d13b2008-08-11 17:36:31 +0000284 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000285 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000286
287 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000288 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
289 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000290
Nate Begemane74795c2006-01-25 18:21:52 +0000291 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000292 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000293
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000294 if (Subtarget.isSVR4ABI()) {
Evan Cheng39e90022012-07-02 22:39:56 +0000295 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000296 // VAARG always uses double-word chunks, so promote anything smaller.
297 setOperationAction(ISD::VAARG, MVT::i1, Promote);
298 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
299 setOperationAction(ISD::VAARG, MVT::i8, Promote);
300 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
301 setOperationAction(ISD::VAARG, MVT::i16, Promote);
302 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
303 setOperationAction(ISD::VAARG, MVT::i32, Promote);
304 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
305 setOperationAction(ISD::VAARG, MVT::Other, Expand);
306 } else {
307 // VAARG is custom lowered with the 32-bit SVR4 ABI.
308 setOperationAction(ISD::VAARG, MVT::Other, Custom);
309 setOperationAction(ISD::VAARG, MVT::i64, Custom);
310 }
Roman Divacky4394e682011-06-28 15:30:42 +0000311 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000312 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000313
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000314 if (Subtarget.isSVR4ABI() && !isPPC64)
Roman Divackyc3825df2013-07-25 21:36:47 +0000315 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
316 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
317 else
318 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
319
Chris Lattner5bd514d2006-01-15 09:02:48 +0000320 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000321 setOperationAction(ISD::VAEND , MVT::Other, Expand);
322 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
323 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
324 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
325 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000326
Chris Lattner6961fc72006-03-26 10:06:40 +0000327 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000328 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000329
Hal Finkel25c19922013-05-15 21:37:41 +0000330 // To handle counter-based loop conditions.
331 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
332
Dale Johannesen160be0f2008-11-07 22:54:33 +0000333 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000334 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
335 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
336 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
337 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
338 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
339 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
340 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
341 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
342 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
343 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
344 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
345 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000346
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000347 if (Subtarget.has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000348 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000349 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
350 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
351 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
352 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000353 // This is just the low 32 bits of a (signed) fp->i64 conversion.
354 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000355 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000356
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000357 if (Subtarget.hasLFIWAX() || Subtarget.isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000358 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000359 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000360 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000361 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000362 }
363
Hal Finkelf6d45f22013-04-01 17:52:07 +0000364 // With the instructions enabled under FPCVT, we can do everything.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000365 if (Subtarget.hasFPCVT()) {
366 if (Subtarget.has64BitSupport()) {
Hal Finkelf6d45f22013-04-01 17:52:07 +0000367 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
368 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
369 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
370 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
371 }
372
373 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
374 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
375 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
376 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
377 }
378
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000379 if (Subtarget.use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000380 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000381 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000382 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000383 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000384 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000385 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
386 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
387 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000388 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000389 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000390 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
391 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
392 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000393 }
Evan Cheng19264272006-03-01 01:11:20 +0000394
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000395 if (Subtarget.hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000396 // First set operation action for all vector types to expand. Then we
397 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson9f944592009-08-11 20:47:22 +0000398 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
399 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
400 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands13237ac2008-06-06 12:08:01 +0000401
Chris Lattner06a21ba2006-04-16 01:37:57 +0000402 // add/sub are legal for all supported vector VT's.
Duncan Sands13237ac2008-06-06 12:08:01 +0000403 setOperationAction(ISD::ADD , VT, Legal);
404 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000405
Chris Lattner95c7adc2006-04-04 17:25:31 +0000406 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000407 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000408 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000409
410 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000411 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000412 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000413 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000414 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000415 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000416 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000417 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000418 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000419 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000420 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000421 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000422 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000423
Chris Lattner06a21ba2006-04-16 01:37:57 +0000424 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000425 setOperationAction(ISD::MUL , VT, Expand);
426 setOperationAction(ISD::SDIV, VT, Expand);
427 setOperationAction(ISD::SREM, VT, Expand);
428 setOperationAction(ISD::UDIV, VT, Expand);
429 setOperationAction(ISD::UREM, VT, Expand);
430 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000431 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000432 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000433 setOperationAction(ISD::FSQRT, VT, Expand);
434 setOperationAction(ISD::FLOG, VT, Expand);
435 setOperationAction(ISD::FLOG10, VT, Expand);
436 setOperationAction(ISD::FLOG2, VT, Expand);
437 setOperationAction(ISD::FEXP, VT, Expand);
438 setOperationAction(ISD::FEXP2, VT, Expand);
439 setOperationAction(ISD::FSIN, VT, Expand);
440 setOperationAction(ISD::FCOS, VT, Expand);
441 setOperationAction(ISD::FABS, VT, Expand);
442 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000443 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000444 setOperationAction(ISD::FCEIL, VT, Expand);
445 setOperationAction(ISD::FTRUNC, VT, Expand);
446 setOperationAction(ISD::FRINT, VT, Expand);
447 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000448 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
449 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
450 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
Ulrich Weigand51eccec2014-08-04 13:27:12 +0000451 setOperationAction(ISD::MULHU, VT, Expand);
452 setOperationAction(ISD::MULHS, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000453 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
454 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
455 setOperationAction(ISD::UDIVREM, VT, Expand);
456 setOperationAction(ISD::SDIVREM, VT, Expand);
457 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
458 setOperationAction(ISD::FPOW, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000459 setOperationAction(ISD::BSWAP, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000460 setOperationAction(ISD::CTPOP, VT, Expand);
461 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000462 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000463 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000464 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000465 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000466 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
467
468 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
469 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
470 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
471 setTruncStoreAction(VT, InnerVT, Expand);
472 }
473 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
474 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
475 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000476 }
477
Chris Lattner95c7adc2006-04-04 17:25:31 +0000478 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
479 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000480 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000481
Owen Anderson9f944592009-08-11 20:47:22 +0000482 setOperationAction(ISD::AND , MVT::v4i32, Legal);
483 setOperationAction(ISD::OR , MVT::v4i32, Legal);
484 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
485 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Hal Finkel940ab932014-02-28 00:27:01 +0000486 setOperationAction(ISD::SELECT, MVT::v4i32,
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000487 Subtarget.useCRBits() ? Legal : Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000488 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000489 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
490 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
491 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
492 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000493 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
494 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
495 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
496 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000497
Craig Topperabadc662012-04-20 06:31:50 +0000498 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
499 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
500 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
501 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000502
Owen Anderson9f944592009-08-11 20:47:22 +0000503 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000504 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000505
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000506 if (TM.Options.UnsafeFPMath || Subtarget.hasVSX()) {
Hal Finkel2e103312013-04-03 04:01:11 +0000507 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
508 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
509 }
510
Owen Anderson9f944592009-08-11 20:47:22 +0000511 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
512 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
513 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000514
Owen Anderson9f944592009-08-11 20:47:22 +0000515 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
516 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000517
Owen Anderson9f944592009-08-11 20:47:22 +0000518 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
519 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
520 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
521 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000522
523 // Altivec does not contain unordered floating-point compare instructions
524 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
525 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000526 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
527 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000528
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000529 if (Subtarget.hasVSX()) {
Hal Finkel27774d92014-03-13 07:58:58 +0000530 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
Hal Finkel82569b62014-03-27 22:22:48 +0000531 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
Hal Finkel27774d92014-03-13 07:58:58 +0000532
533 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
534 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
535 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
536 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
537 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
538
539 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
540
541 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
542 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
543
544 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
545 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
546
Hal Finkel732f0f72014-03-26 12:49:28 +0000547 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
548 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal);
549 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
550 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
551 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
552
Hal Finkel27774d92014-03-13 07:58:58 +0000553 // Share the Altivec comparison restrictions.
554 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand);
555 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000556 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand);
557 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand);
558
Hal Finkel9281c9a2014-03-26 18:26:30 +0000559 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
560 setOperationAction(ISD::STORE, MVT::v2f64, Legal);
561
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000562 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal);
563
Hal Finkel19be5062014-03-29 05:29:01 +0000564 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000565
566 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass);
567 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass);
Hal Finkela6c8b512014-03-26 16:12:58 +0000568
569 // VSX v2i64 only supports non-arithmetic operations.
570 setOperationAction(ISD::ADD, MVT::v2i64, Expand);
571 setOperationAction(ISD::SUB, MVT::v2i64, Expand);
572
Hal Finkelad801b72014-03-27 21:26:33 +0000573 setOperationAction(ISD::SHL, MVT::v2i64, Expand);
574 setOperationAction(ISD::SRA, MVT::v2i64, Expand);
575 setOperationAction(ISD::SRL, MVT::v2i64, Expand);
576
Hal Finkel777c9dd2014-03-29 16:04:40 +0000577 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
578
Hal Finkel9281c9a2014-03-26 18:26:30 +0000579 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
580 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64);
581 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
582 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64);
583
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000584 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal);
585
Hal Finkel7279f4b2014-03-26 19:13:54 +0000586 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
587 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
588 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
589 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
590
Hal Finkel5c0d1452014-03-30 13:22:59 +0000591 // Vector operation legalization checks the result type of
592 // SIGN_EXTEND_INREG, overall legalization checks the inner type.
593 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal);
594 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal);
595 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
596 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
597
Hal Finkela6c8b512014-03-26 16:12:58 +0000598 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000599 }
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000600 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000601
Hal Finkel01fa7702014-12-03 00:19:17 +0000602 if (Subtarget.has64BitSupport())
Hal Finkel322e41a2012-04-01 20:08:17 +0000603 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel01fa7702014-12-03 00:19:17 +0000604
605 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, isPPC64 ? Legal : Custom);
Hal Finkel322e41a2012-04-01 20:08:17 +0000606
Robin Morissete1ca44b2014-10-02 22:27:07 +0000607 if (!isPPC64) {
608 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
609 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
610 }
Eli Friedman7dfa7912011-08-29 18:23:02 +0000611
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000612 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidta76bf5a2013-04-23 18:49:44 +0000613 // Altivec instructions set fields to all zeros or all ones.
614 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000615
Joerg Sonnenbergerb5459e62014-07-24 22:20:10 +0000616 if (!isPPC64) {
617 // These libcalls are not available in 32-bit.
618 setLibcallName(RTLIB::SHL_I128, nullptr);
619 setLibcallName(RTLIB::SRL_I128, nullptr);
620 setLibcallName(RTLIB::SRA_I128, nullptr);
621 }
622
Evan Cheng39e90022012-07-02 22:39:56 +0000623 if (isPPC64) {
Chris Lattner454436d2006-10-18 01:20:43 +0000624 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000625 setExceptionPointerRegister(PPC::X3);
626 setExceptionSelectorRegister(PPC::X4);
627 } else {
Chris Lattner454436d2006-10-18 01:20:43 +0000628 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000629 setExceptionPointerRegister(PPC::R3);
630 setExceptionSelectorRegister(PPC::R4);
631 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000632
Chris Lattnerf4184352006-03-01 04:57:39 +0000633 // We have target-specific dag combine patterns for the following nodes:
634 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000635 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000636 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000637 setTargetDAGCombine(ISD::BR_CC);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000638 if (Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000639 setTargetDAGCombine(ISD::BRCOND);
Chris Lattnera7976d32006-07-10 20:56:58 +0000640 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000641 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Bill Schmidtfae5d712014-12-09 16:35:51 +0000642 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
643 setTargetDAGCombine(ISD::INTRINSIC_VOID);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000644
Hal Finkel46043ed2014-03-01 21:36:57 +0000645 setTargetDAGCombine(ISD::SIGN_EXTEND);
646 setTargetDAGCombine(ISD::ZERO_EXTEND);
647 setTargetDAGCombine(ISD::ANY_EXTEND);
648
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000649 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000650 setTargetDAGCombine(ISD::TRUNCATE);
651 setTargetDAGCombine(ISD::SETCC);
652 setTargetDAGCombine(ISD::SELECT_CC);
653 }
654
Hal Finkel2e103312013-04-03 04:01:11 +0000655 // Use reciprocal estimates.
656 if (TM.Options.UnsafeFPMath) {
657 setTargetDAGCombine(ISD::FDIV);
658 setTargetDAGCombine(ISD::FSQRT);
659 }
660
Dale Johannesen10432e52007-10-19 00:59:18 +0000661 // Darwin long double math library functions have $LDBL128 appended.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000662 if (Subtarget.isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000663 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000664 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
665 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000666 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
667 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000668 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
669 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
670 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
671 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
672 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000673 }
674
Hal Finkel940ab932014-02-28 00:27:01 +0000675 // With 32 condition bits, we don't need to sink (and duplicate) compares
676 // aggressively in CodeGenPrep.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000677 if (Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000678 setHasMultipleConditionRegisters();
679
Hal Finkel65298572011-10-17 18:53:03 +0000680 setMinFunctionAlignment(2);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000681 if (Subtarget.isDarwin())
Hal Finkel65298572011-10-17 18:53:03 +0000682 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000683
Eli Friedman30a49e92011-08-03 21:06:02 +0000684 setInsertFencesForAtomic(true);
685
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000686 if (Subtarget.enableMachineScheduler())
Hal Finkel21442b22013-09-11 23:05:25 +0000687 setSchedulingPreference(Sched::Source);
688 else
689 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000690
Chris Lattnerf22556d2005-08-16 17:14:42 +0000691 computeRegisterProperties();
Hal Finkel742b5352012-08-28 16:12:39 +0000692
693 // The Freescale cores does better with aggressive inlining of memcpy and
694 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000695 if (Subtarget.getDarwinDirective() == PPC::DIR_E500mc ||
696 Subtarget.getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000697 MaxStoresPerMemset = 32;
698 MaxStoresPerMemsetOptSize = 16;
699 MaxStoresPerMemcpy = 32;
700 MaxStoresPerMemcpyOptSize = 8;
701 MaxStoresPerMemmove = 32;
702 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel742b5352012-08-28 16:12:39 +0000703
704 setPrefFunctionAlignment(4);
Hal Finkel742b5352012-08-28 16:12:39 +0000705 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000706}
707
Hal Finkel262a2242013-09-12 23:20:06 +0000708/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
709/// the desired ByVal argument alignment.
710static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
711 unsigned MaxMaxAlign) {
712 if (MaxAlign == MaxMaxAlign)
713 return;
714 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
715 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
716 MaxAlign = 32;
717 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
718 MaxAlign = 16;
719 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
720 unsigned EltAlign = 0;
721 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
722 if (EltAlign > MaxAlign)
723 MaxAlign = EltAlign;
724 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
725 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
726 unsigned EltAlign = 0;
727 getMaxByValAlign(STy->getElementType(i), EltAlign, MaxMaxAlign);
728 if (EltAlign > MaxAlign)
729 MaxAlign = EltAlign;
730 if (MaxAlign == MaxMaxAlign)
731 break;
732 }
733 }
734}
735
Dale Johannesencbde4c22008-02-28 22:31:51 +0000736/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
737/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000738unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000739 // Darwin passes everything on 4 byte boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000740 if (Subtarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000741 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000742
743 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000744 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000745 unsigned Align = Subtarget.isPPC64() ? 8 : 4;
746 if (Subtarget.hasAltivec() || Subtarget.hasQPX())
747 getMaxByValAlign(Ty, Align, Subtarget.hasQPX() ? 32 : 16);
Hal Finkel262a2242013-09-12 23:20:06 +0000748 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000749}
750
Chris Lattner347ed8a2006-01-09 23:52:17 +0000751const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
752 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000753 default: return nullptr;
Evan Cheng32e376f2008-07-12 02:23:19 +0000754 case PPCISD::FSEL: return "PPCISD::FSEL";
755 case PPCISD::FCFID: return "PPCISD::FCFID";
756 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
757 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000758 case PPCISD::FRE: return "PPCISD::FRE";
759 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000760 case PPCISD::STFIWX: return "PPCISD::STFIWX";
761 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
762 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
763 case PPCISD::VPERM: return "PPCISD::VPERM";
764 case PPCISD::Hi: return "PPCISD::Hi";
765 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000766 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller79fef932009-12-18 13:00:15 +0000767 case PPCISD::LOAD: return "PPCISD::LOAD";
768 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000769 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
770 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
771 case PPCISD::SRL: return "PPCISD::SRL";
772 case PPCISD::SRA: return "PPCISD::SRA";
773 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000774 case PPCISD::CALL: return "PPCISD::CALL";
775 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Bill Schmidt3d9674c2014-11-11 20:44:09 +0000776 case PPCISD::CALL_TLS: return "PPCISD::CALL_TLS";
777 case PPCISD::CALL_NOP_TLS: return "PPCISD::CALL_NOP_TLS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000778 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000779 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng32e376f2008-07-12 02:23:19 +0000780 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkelbbdee932014-12-02 22:01:00 +0000781 case PPCISD::READ_TIME_BASE: return "PPCISD::READ_TIME_BASE";
Hal Finkel756810f2013-03-21 21:37:52 +0000782 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
783 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000784 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng32e376f2008-07-12 02:23:19 +0000785 case PPCISD::VCMP: return "PPCISD::VCMP";
786 case PPCISD::VCMPo: return "PPCISD::VCMPo";
787 case PPCISD::LBRX: return "PPCISD::LBRX";
788 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng32e376f2008-07-12 02:23:19 +0000789 case PPCISD::LARX: return "PPCISD::LARX";
790 case PPCISD::STCX: return "PPCISD::STCX";
791 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +0000792 case PPCISD::BDNZ: return "PPCISD::BDNZ";
793 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000794 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000795 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000796 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +0000797 case PPCISD::CR6SET: return "PPCISD::CR6SET";
798 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34627e32012-11-27 17:35:46 +0000799 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
800 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
801 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Roman Divacky32143e22013-12-20 18:08:54 +0000802 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000803 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
804 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000805 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000806 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
807 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000808 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
809 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000810 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
811 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +0000812 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +0000813 case PPCISD::SC: return "PPCISD::SC";
Chris Lattner347ed8a2006-01-09 23:52:17 +0000814 }
815}
816
Matt Arsenault758659232013-05-18 00:21:46 +0000817EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000818 if (!VT.isVector())
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000819 return Subtarget.useCRBits() ? MVT::i1 : MVT::i32;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000820 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000821}
822
Hal Finkel62ac7362014-09-19 11:42:56 +0000823bool PPCTargetLowering::enableAggressiveFMAFusion(EVT VT) const {
824 assert(VT.isFloatingPoint() && "Non-floating-point FMA?");
825 return true;
826}
827
Chris Lattner4211ca92006-04-14 06:01:58 +0000828//===----------------------------------------------------------------------===//
829// Node matching predicates, for use by the tblgen matching code.
830//===----------------------------------------------------------------------===//
831
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000832/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000833static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000834 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000835 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000836 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000837 // Maybe this has already been legalized into the constant pool?
838 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000839 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000840 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000841 }
842 return false;
843}
844
Chris Lattnere8b83b42006-04-06 17:23:16 +0000845/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
846/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000847static bool isConstantOrUndef(int Op, int Val) {
848 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000849}
850
851/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
852/// VPKUHUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000853/// The ShuffleKind distinguishes between big-endian operations with
854/// two different inputs (0), either-endian operations with two identical
855/// inputs (1), and little-endian operantion with two different inputs (2).
856/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
857bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +0000858 SelectionDAG &DAG) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000859 bool IsLE = DAG.getSubtarget().getDataLayout()->isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000860 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +0000861 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000862 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000863 for (unsigned i = 0; i != 16; ++i)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000864 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000865 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000866 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +0000867 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000868 return false;
869 for (unsigned i = 0; i != 16; ++i)
870 if (!isConstantOrUndef(N->getMaskElt(i), i*2))
871 return false;
872 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +0000873 unsigned j = IsLE ? 0 : 1;
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000874 for (unsigned i = 0; i != 8; ++i)
Bill Schmidtf910a062014-06-10 14:35:01 +0000875 if (!isConstantOrUndef(N->getMaskElt(i), i*2+j) ||
876 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000877 return false;
878 }
Chris Lattner1d338192006-04-06 18:26:28 +0000879 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000880}
881
882/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
883/// VPKUWUM instruction.
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000884/// The ShuffleKind distinguishes between big-endian operations with
885/// two different inputs (0), either-endian operations with two identical
886/// inputs (1), and little-endian operantion with two different inputs (2).
887/// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
888bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
Bill Schmidtf910a062014-06-10 14:35:01 +0000889 SelectionDAG &DAG) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000890 bool IsLE = DAG.getSubtarget().getDataLayout()->isLittleEndian();
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000891 if (ShuffleKind == 0) {
Eric Christopherd9134482014-08-04 21:25:23 +0000892 if (IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000893 return false;
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000894 for (unsigned i = 0; i != 16; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000895 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
896 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000897 return false;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000898 } else if (ShuffleKind == 2) {
Eric Christopherd9134482014-08-04 21:25:23 +0000899 if (!IsLE)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000900 return false;
901 for (unsigned i = 0; i != 16; i += 2)
902 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) ||
903 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1))
904 return false;
905 } else if (ShuffleKind == 1) {
Eric Christopherd9134482014-08-04 21:25:23 +0000906 unsigned j = IsLE ? 0 : 2;
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000907 for (unsigned i = 0; i != 8; i += 2)
Ulrich Weigandcc9909b2014-08-04 13:53:40 +0000908 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) ||
909 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) ||
910 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) ||
911 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000912 return false;
913 }
Chris Lattner1d338192006-04-06 18:26:28 +0000914 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000915}
916
Chris Lattnerf38e0332006-04-06 22:02:42 +0000917/// isVMerge - Common function, used to match vmrg* shuffles.
918///
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000919static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +0000920 unsigned LHSStart, unsigned RHSStart) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000921 if (N->getValueType(0) != MVT::v16i8)
922 return false;
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000923 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
924 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000925
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000926 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
927 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000928 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000929 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000930 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000931 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000932 return false;
933 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000934 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +0000935}
936
937/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +0000938/// a VMRGL* instruction with the specified unit size (1,2 or 4 bytes).
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000939/// The ShuffleKind distinguishes between big-endian merges with two
940/// different inputs (0), either-endian merges with two identical inputs (1),
941/// and little-endian merges with two different inputs (2). For the latter,
942/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +0000943bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000944 unsigned ShuffleKind, SelectionDAG &DAG) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000945 if (DAG.getSubtarget().getDataLayout()->isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000946 if (ShuffleKind == 1) // unary
947 return isVMerge(N, UnitSize, 0, 0);
948 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +0000949 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000950 else
951 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +0000952 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000953 if (ShuffleKind == 1) // unary
954 return isVMerge(N, UnitSize, 8, 8);
955 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +0000956 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000957 else
958 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +0000959 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000960}
961
962/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +0000963/// a VMRGH* instruction with the specified unit size (1,2 or 4 bytes).
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000964/// The ShuffleKind distinguishes between big-endian merges with two
965/// different inputs (0), either-endian merges with two identical inputs (1),
966/// and little-endian merges with two different inputs (2). For the latter,
967/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +0000968bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000969 unsigned ShuffleKind, SelectionDAG &DAG) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000970 if (DAG.getSubtarget().getDataLayout()->isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000971 if (ShuffleKind == 1) // unary
972 return isVMerge(N, UnitSize, 8, 8);
973 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +0000974 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000975 else
976 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +0000977 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000978 if (ShuffleKind == 1) // unary
979 return isVMerge(N, UnitSize, 0, 0);
980 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +0000981 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000982 else
983 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +0000984 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000985}
986
987
Chris Lattner1d338192006-04-06 18:26:28 +0000988/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
989/// amount, otherwise return -1.
Bill Schmidt42a69362014-08-05 20:47:25 +0000990/// The ShuffleKind distinguishes between big-endian operations with two
991/// different inputs (0), either-endian operations with two identical inputs
992/// (1), and little-endian operations with two different inputs (2). For the
993/// latter, the input operands are swapped (see PPCInstrAltivec.td).
994int PPC::isVSLDOIShuffleMask(SDNode *N, unsigned ShuffleKind,
995 SelectionDAG &DAG) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000996 if (N->getValueType(0) != MVT::v16i8)
Hal Finkela775e512014-04-08 19:00:27 +0000997 return -1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000998
999 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +00001000
Chris Lattner1d338192006-04-06 18:26:28 +00001001 // Find the first non-undef value in the shuffle mask.
1002 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001003 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +00001004 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001005
Chris Lattner1d338192006-04-06 18:26:28 +00001006 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001007
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001008 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +00001009 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001010 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +00001011 if (ShiftAmt < i) return -1;
Chris Lattnere8b83b42006-04-06 17:23:16 +00001012
Bill Schmidtf04e9982014-08-04 23:21:01 +00001013 ShiftAmt -= i;
Bill Schmidt42a69362014-08-05 20:47:25 +00001014 bool isLE = DAG.getTarget().getSubtargetImpl()->getDataLayout()->
1015 isLittleEndian();
Bill Schmidtf910a062014-06-10 14:35:01 +00001016
Bill Schmidt42a69362014-08-05 20:47:25 +00001017 if ((ShuffleKind == 0 && !isLE) || (ShuffleKind == 2 && isLE)) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001018 // Check the rest of the elements to see if they are consecutive.
1019 for (++i; i != 16; ++i)
1020 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
1021 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001022 } else if (ShuffleKind == 1) {
Bill Schmidtf04e9982014-08-04 23:21:01 +00001023 // Check the rest of the elements to see if they are consecutive.
1024 for (++i; i != 16; ++i)
1025 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
1026 return -1;
Bill Schmidt42a69362014-08-05 20:47:25 +00001027 } else
1028 return -1;
1029
1030 if (ShuffleKind == 2 && isLE)
1031 ShiftAmt = 16 - ShiftAmt;
Bill Schmidtf04e9982014-08-04 23:21:01 +00001032
Chris Lattner1d338192006-04-06 18:26:28 +00001033 return ShiftAmt;
1034}
Chris Lattnerffc47562006-03-20 06:33:01 +00001035
1036/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
1037/// specifies a splat of a single element that is suitable for input to
1038/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001039bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +00001040 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +00001041 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001042
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001043 // This is a splat operation if each element of the permute is the same, and
1044 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001045 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001046
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001047 // FIXME: Handle UNDEF elements too!
1048 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +00001049 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001050
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001051 // Check that the indices are consecutive, in the case of a multi-byte element
1052 // splatted with a v16i8 mask.
1053 for (unsigned i = 1; i != EltSize; ++i)
1054 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001055 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001056
Chris Lattner95c7adc2006-04-04 17:25:31 +00001057 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001058 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +00001059 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001060 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001061 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001062 }
Chris Lattner95c7adc2006-04-04 17:25:31 +00001063 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +00001064}
1065
Evan Cheng581d2792007-07-30 07:51:22 +00001066/// isAllNegativeZeroVector - Returns true if all elements of build_vector
1067/// are -0.0.
1068bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001069 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
1070
1071 APInt APVal, APUndef;
1072 unsigned BitSize;
1073 bool HasAnyUndefs;
Wesley Peck527da1b2010-11-23 03:31:01 +00001074
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00001075 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001076 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001077 return CFP->getValueAPF().isNegZero();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001078
Evan Cheng581d2792007-07-30 07:51:22 +00001079 return false;
1080}
1081
Chris Lattnerffc47562006-03-20 06:33:01 +00001082/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
1083/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Bill Schmidtf910a062014-06-10 14:35:01 +00001084unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize,
1085 SelectionDAG &DAG) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001086 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1087 assert(isSplatShuffleMask(SVOp, EltSize));
Eric Christopherfc6de422014-08-05 02:39:49 +00001088 if (DAG.getSubtarget().getDataLayout()->isLittleEndian())
Bill Schmidtf910a062014-06-10 14:35:01 +00001089 return (16 / EltSize) - 1 - (SVOp->getMaskElt(0) / EltSize);
1090 else
1091 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +00001092}
1093
Chris Lattner74cf9ff2006-04-12 17:37:20 +00001094/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001095/// by using a vspltis[bhw] instruction of the specified element size, return
1096/// the constant being splatted. The ByteSize field indicates the number of
1097/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001098SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001099 SDValue OpVal(nullptr, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001100
1101 // If ByteSize of the splat is bigger than the element size of the
1102 // build_vector, then we have a case where we are checking for a splat where
1103 // multiple elements of the buildvector are folded together into a single
1104 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
1105 unsigned EltSize = 16/N->getNumOperands();
1106 if (EltSize < ByteSize) {
1107 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001108 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001109 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001110
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001111 // See if all of the elements in the buildvector agree across.
1112 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1113 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1114 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001115 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001116
Scott Michelcf0da6c2009-02-17 22:15:04 +00001117
Craig Topper062a2ba2014-04-25 05:30:21 +00001118 if (!UniquedVals[i&(Multiple-1)].getNode())
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001119 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
1120 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001121 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001122 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001123
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001124 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
1125 // either constant or undef values that are identical for each chunk. See
1126 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001127
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001128 // Check to see if all of the leading entries are either 0 or -1. If
1129 // neither, then this won't fit into the immediate field.
1130 bool LeadingZero = true;
1131 bool LeadingOnes = true;
1132 for (unsigned i = 0; i != Multiple-1; ++i) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001133 if (!UniquedVals[i].getNode()) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001134
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001135 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
1136 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
1137 }
1138 // Finally, check the least significant entry.
1139 if (LeadingZero) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001140 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001141 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00001142 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001143 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +00001144 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001145 }
1146 if (LeadingOnes) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001147 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001148 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +00001149 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001150 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +00001151 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001152 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001153
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001154 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001155 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001156
Chris Lattner2771e2c2006-03-25 06:12:06 +00001157 // Check to see if this buildvec has a single non-undef value in its elements.
1158 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1159 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Craig Topper062a2ba2014-04-25 05:30:21 +00001160 if (!OpVal.getNode())
Chris Lattner2771e2c2006-03-25 06:12:06 +00001161 OpVal = N->getOperand(i);
1162 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001163 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001164 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001165
Craig Topper062a2ba2014-04-25 05:30:21 +00001166 if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001167
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001168 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001169 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001170 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001171 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001172 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001173 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001174 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001175 }
1176
1177 // If the splat value is larger than the element value, then we can never do
1178 // this splat. The only case that we could fit the replicated bits into our
1179 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001180 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001181
Chris Lattner2771e2c2006-03-25 06:12:06 +00001182 // If the element value is larger than the splat value, cut it in half and
1183 // check to see if the two halves are equal. Continue doing this until we
1184 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
1185 while (ValSizeInBytes > ByteSize) {
1186 ValSizeInBytes >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001187
Chris Lattner2771e2c2006-03-25 06:12:06 +00001188 // If the top half equals the bottom half, we're still ok.
Chris Lattner39cc7172006-04-05 17:39:25 +00001189 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
1190 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001191 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001192 }
1193
1194 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001195 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001196
Evan Chengb1ddc982006-03-26 09:52:32 +00001197 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001198 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001199
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001200 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001201 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +00001202 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001203 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001204}
1205
Chris Lattner4211ca92006-04-14 06:01:58 +00001206//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001207// Addressing Mode Selection
1208//===----------------------------------------------------------------------===//
1209
1210/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1211/// or 64-bit immediate, and if the value can be accurately represented as a
1212/// sign extension from a 16-bit value. If so, this returns true and the
1213/// immediate.
1214static bool isIntS16Immediate(SDNode *N, short &Imm) {
Adam Nemet571eb5f2014-05-20 17:20:34 +00001215 if (!isa<ConstantSDNode>(N))
Chris Lattnera801fced2006-11-08 02:15:41 +00001216 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001217
Dan Gohmaneffb8942008-09-12 16:56:44 +00001218 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001219 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001220 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001221 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001222 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001223}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001224static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001225 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001226}
1227
1228
1229/// SelectAddressRegReg - Given the specified addressed, check to see if it
1230/// can be represented as an indexed [r+r] operation. Returns false if it
1231/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001232bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1233 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001234 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001235 short imm = 0;
1236 if (N.getOpcode() == ISD::ADD) {
1237 if (isIntS16Immediate(N.getOperand(1), imm))
1238 return false; // r+i
1239 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1240 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001241
Chris Lattnera801fced2006-11-08 02:15:41 +00001242 Base = N.getOperand(0);
1243 Index = N.getOperand(1);
1244 return true;
1245 } else if (N.getOpcode() == ISD::OR) {
1246 if (isIntS16Immediate(N.getOperand(1), imm))
1247 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001248
Chris Lattnera801fced2006-11-08 02:15:41 +00001249 // If this is an or of disjoint bitfields, we can codegen this as an add
1250 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1251 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001252 APInt LHSKnownZero, LHSKnownOne;
1253 APInt RHSKnownZero, RHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001254 DAG.computeKnownBits(N.getOperand(0),
1255 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001256
Dan Gohmanf19609a2008-02-27 01:23:58 +00001257 if (LHSKnownZero.getBoolValue()) {
Jay Foada0653a32014-05-14 21:14:37 +00001258 DAG.computeKnownBits(N.getOperand(1),
1259 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001260 // If all of the bits are known zero on the LHS or RHS, the add won't
1261 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001262 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001263 Base = N.getOperand(0);
1264 Index = N.getOperand(1);
1265 return true;
1266 }
1267 }
1268 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001269
Chris Lattnera801fced2006-11-08 02:15:41 +00001270 return false;
1271}
1272
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001273// If we happen to be doing an i64 load or store into a stack slot that has
1274// less than a 4-byte alignment, then the frame-index elimination may need to
1275// use an indexed load or store instruction (because the offset may not be a
1276// multiple of 4). The extra register needed to hold the offset comes from the
1277// register scavenger, and it is possible that the scavenger will need to use
1278// an emergency spill slot. As a result, we need to make sure that a spill slot
1279// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1280// stack slot.
1281static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1282 // FIXME: This does not handle the LWA case.
1283 if (VT != MVT::i64)
1284 return;
1285
Hal Finkel7ab3db52013-07-10 15:29:01 +00001286 // NOTE: We'll exclude negative FIs here, which come from argument
1287 // lowering, because there are no known test cases triggering this problem
1288 // using packed structures (or similar). We can remove this exclusion if
1289 // we find such a test case. The reason why this is so test-case driven is
1290 // because this entire 'fixup' is only to prevent crashes (from the
1291 // register scavenger) on not-really-valid inputs. For example, if we have:
1292 // %a = alloca i1
1293 // %b = bitcast i1* %a to i64*
1294 // store i64* a, i64 b
1295 // then the store should really be marked as 'align 1', but is not. If it
1296 // were marked as 'align 1' then the indexed form would have been
1297 // instruction-selected initially, and the problem this 'fixup' is preventing
1298 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001299 if (FrameIdx < 0)
1300 return;
1301
1302 MachineFunction &MF = DAG.getMachineFunction();
1303 MachineFrameInfo *MFI = MF.getFrameInfo();
1304
1305 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1306 if (Align >= 4)
1307 return;
1308
1309 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1310 FuncInfo->setHasNonRISpills();
1311}
1312
Chris Lattnera801fced2006-11-08 02:15:41 +00001313/// Returns true if the address N can be represented by a base register plus
1314/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001315/// represented as reg+reg. If Aligned is true, only accept displacements
1316/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001317bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001318 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001319 SelectionDAG &DAG,
1320 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001321 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001322 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001323 // If this can be more profitably realized as r+r, fail.
1324 if (SelectAddressRegReg(N, Disp, Base, DAG))
1325 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001326
Chris Lattnera801fced2006-11-08 02:15:41 +00001327 if (N.getOpcode() == ISD::ADD) {
1328 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001329 if (isIntS16Immediate(N.getOperand(1), imm) &&
1330 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001331 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001332 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1333 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001334 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001335 } else {
1336 Base = N.getOperand(0);
1337 }
1338 return true; // [r+i]
1339 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1340 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001341 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001342 && "Cannot handle constant offsets yet!");
1343 Disp = N.getOperand(1).getOperand(0); // The global address.
1344 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001345 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001346 Disp.getOpcode() == ISD::TargetConstantPool ||
1347 Disp.getOpcode() == ISD::TargetJumpTable);
1348 Base = N.getOperand(0);
1349 return true; // [&g+r]
1350 }
1351 } else if (N.getOpcode() == ISD::OR) {
1352 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001353 if (isIntS16Immediate(N.getOperand(1), imm) &&
1354 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001355 // If this is an or of disjoint bitfields, we can codegen this as an add
1356 // (for better address arithmetic) if the LHS and RHS of the OR are
1357 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001358 APInt LHSKnownZero, LHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001359 DAG.computeKnownBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001360
Dan Gohmanf19609a2008-02-27 01:23:58 +00001361 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001362 // If all of the bits are known zero on the LHS or RHS, the add won't
1363 // carry.
Ulrich Weigand55a96652014-07-20 22:26:40 +00001364 if (FrameIndexSDNode *FI =
1365 dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1366 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1367 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1368 } else {
1369 Base = N.getOperand(0);
1370 }
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001371 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001372 return true;
1373 }
1374 }
1375 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1376 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001377
Chris Lattnera801fced2006-11-08 02:15:41 +00001378 // If this address fits entirely in a 16-bit sext immediate field, codegen
1379 // this as "d, 0"
1380 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001381 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001382 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001383 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001384 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001385 return true;
1386 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001387
1388 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001389 if ((CN->getValueType(0) == MVT::i32 ||
1390 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1391 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001392 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001393
Chris Lattnera801fced2006-11-08 02:15:41 +00001394 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001395 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001396
Owen Anderson9f944592009-08-11 20:47:22 +00001397 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1398 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001399 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001400 return true;
1401 }
1402 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001403
Chris Lattnera801fced2006-11-08 02:15:41 +00001404 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001405 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001406 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001407 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1408 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001409 Base = N;
1410 return true; // [r+0]
1411}
1412
1413/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1414/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001415bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1416 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001417 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001418 // Check to see if we can easily represent this as an [r+r] address. This
1419 // will fail if it thinks that the address is more profitably represented as
1420 // reg+imm, e.g. where imm = 0.
1421 if (SelectAddressRegReg(N, Base, Index, DAG))
1422 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001423
Chris Lattnera801fced2006-11-08 02:15:41 +00001424 // If the operand is an addition, always emit this as [r+r], since this is
1425 // better (for code size, and execution, as the memop does the add for free)
1426 // than emitting an explicit add.
1427 if (N.getOpcode() == ISD::ADD) {
1428 Base = N.getOperand(0);
1429 Index = N.getOperand(1);
1430 return true;
1431 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001432
Chris Lattnera801fced2006-11-08 02:15:41 +00001433 // Otherwise, do it the hard way, using R0 as the base register.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001434 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001435 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001436 Index = N;
1437 return true;
1438}
1439
Chris Lattnera801fced2006-11-08 02:15:41 +00001440/// getPreIndexedAddressParts - returns true by value, base pointer and
1441/// offset pointer and addressing mode by reference if the node's address
1442/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001443bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1444 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001445 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001446 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001447 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001448
Ulrich Weigande90b0222013-03-22 14:58:48 +00001449 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001450 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001451 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001452 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001453 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1454 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001455 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001456 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001457 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001458 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001459 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001460 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001461 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001462 } else
1463 return false;
1464
Chris Lattner68371252006-11-14 01:38:31 +00001465 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands13237ac2008-06-06 12:08:01 +00001466 if (VT.isVector())
Chris Lattner68371252006-11-14 01:38:31 +00001467 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001468
Ulrich Weigande90b0222013-03-22 14:58:48 +00001469 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1470
1471 // Common code will reject creating a pre-inc form if the base pointer
1472 // is a frame index, or if N is a store and the base pointer is either
1473 // the same as or a predecessor of the value being stored. Check for
1474 // those situations here, and try with swapped Base/Offset instead.
1475 bool Swap = false;
1476
1477 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1478 Swap = true;
1479 else if (!isLoad) {
1480 SDValue Val = cast<StoreSDNode>(N)->getValue();
1481 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1482 Swap = true;
1483 }
1484
1485 if (Swap)
1486 std::swap(Base, Offset);
1487
Hal Finkelca542be2012-06-20 15:43:03 +00001488 AM = ISD::PRE_INC;
1489 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001490 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001491
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001492 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001493 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001494 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001495 return false;
1496 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001497 // LDU/STU need an address with at least 4-byte alignment.
1498 if (Alignment < 4)
1499 return false;
1500
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001501 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001502 return false;
1503 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001504
Chris Lattnerb314b152006-11-11 00:08:42 +00001505 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001506 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1507 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001508 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001509 LD->getExtensionType() == ISD::SEXTLOAD &&
1510 isa<ConstantSDNode>(Offset))
1511 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001512 }
1513
Chris Lattnerce645542006-11-10 02:08:47 +00001514 AM = ISD::PRE_INC;
1515 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001516}
1517
1518//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001519// LowerOperation implementation
1520//===----------------------------------------------------------------------===//
1521
Chris Lattneredb9d842010-11-15 02:46:57 +00001522/// GetLabelAccessInfo - Return true if we should reference labels using a
1523/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1524static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Craig Topper062a2ba2014-04-25 05:30:21 +00001525 unsigned &LoOpFlags,
1526 const GlobalValue *GV = nullptr) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001527 HiOpFlags = PPCII::MO_HA;
1528 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001529
Hal Finkel3ee2af72014-07-18 23:29:49 +00001530 // Don't use the pic base if not in PIC relocation model.
1531 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
1532
Chris Lattnerdd6df842010-11-15 03:13:19 +00001533 if (isPIC) {
1534 HiOpFlags |= PPCII::MO_PIC_FLAG;
1535 LoOpFlags |= PPCII::MO_PIC_FLAG;
1536 }
1537
1538 // If this is a reference to a global value that requires a non-lazy-ptr, make
1539 // sure that instruction lowering adds it.
1540 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1541 HiOpFlags |= PPCII::MO_NLP_FLAG;
1542 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001543
Chris Lattnerdd6df842010-11-15 03:13:19 +00001544 if (GV->hasHiddenVisibility()) {
1545 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1546 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1547 }
1548 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001549
Chris Lattneredb9d842010-11-15 02:46:57 +00001550 return isPIC;
1551}
1552
1553static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1554 SelectionDAG &DAG) {
1555 EVT PtrVT = HiPart.getValueType();
1556 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001557 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001558
1559 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1560 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001561
Chris Lattneredb9d842010-11-15 02:46:57 +00001562 // With PIC, the first instruction is actually "GR+hi(&G)".
1563 if (isPIC)
1564 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1565 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001566
Chris Lattneredb9d842010-11-15 02:46:57 +00001567 // Generate non-pic code that has direct accesses to the constant pool.
1568 // The address of the global is just (hi(&g)+lo(&g)).
1569 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1570}
1571
Scott Michelcf0da6c2009-02-17 22:15:04 +00001572SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001573 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001574 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001575 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001576 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001577
Roman Divackyace47072012-08-24 16:26:02 +00001578 // 64-bit SVR4 ABI code is always position-independent.
1579 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001580 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Roman Divackyace47072012-08-24 16:26:02 +00001581 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001582 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001583 DAG.getRegister(PPC::X2, MVT::i64));
1584 }
1585
Chris Lattneredb9d842010-11-15 02:46:57 +00001586 unsigned MOHiFlag, MOLoFlag;
1587 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001588
1589 if (isPIC && Subtarget.isSVR4ABI()) {
1590 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(),
1591 PPCII::MO_PIC_FLAG);
1592 SDLoc DL(CP);
1593 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i32, GA,
1594 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT));
1595 }
1596
Chris Lattneredb9d842010-11-15 02:46:57 +00001597 SDValue CPIHi =
1598 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1599 SDValue CPILo =
1600 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1601 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001602}
1603
Dan Gohman21cea8a2010-04-17 15:26:15 +00001604SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001605 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001606 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001607
Roman Divackyace47072012-08-24 16:26:02 +00001608 // 64-bit SVR4 ABI code is always position-independent.
1609 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001610 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Roman Divackyace47072012-08-24 16:26:02 +00001611 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001612 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001613 DAG.getRegister(PPC::X2, MVT::i64));
1614 }
1615
Chris Lattneredb9d842010-11-15 02:46:57 +00001616 unsigned MOHiFlag, MOLoFlag;
1617 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001618
1619 if (isPIC && Subtarget.isSVR4ABI()) {
1620 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1621 PPCII::MO_PIC_FLAG);
1622 SDLoc DL(GA);
1623 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), PtrVT, GA,
1624 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT));
1625 }
1626
Chris Lattneredb9d842010-11-15 02:46:57 +00001627 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1628 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1629 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001630}
1631
Dan Gohman21cea8a2010-04-17 15:26:15 +00001632SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1633 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001634 EVT PtrVT = Op.getValueType();
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001635 BlockAddressSDNode *BASDN = cast<BlockAddressSDNode>(Op);
1636 const BlockAddress *BA = BASDN->getBlockAddress();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001637
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001638 // 64-bit SVR4 ABI code is always position-independent.
1639 // The actual BlockAddress is stored in the TOC.
1640 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
1641 SDValue GA = DAG.getTargetBlockAddress(BA, PtrVT, BASDN->getOffset());
1642 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(BASDN), MVT::i64, GA,
1643 DAG.getRegister(PPC::X2, MVT::i64));
1644 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001645
Chris Lattneredb9d842010-11-15 02:46:57 +00001646 unsigned MOHiFlag, MOLoFlag;
1647 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001648 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1649 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001650 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1651}
1652
Bill Schmidt3d9674c2014-11-11 20:44:09 +00001653// Generate a call to __tls_get_addr for the given GOT entry Op.
1654std::pair<SDValue,SDValue>
1655PPCTargetLowering::lowerTLSCall(SDValue Op, SDLoc dl,
1656 SelectionDAG &DAG) const {
1657
1658 Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext());
1659 TargetLowering::ArgListTy Args;
1660 TargetLowering::ArgListEntry Entry;
1661 Entry.Node = Op;
1662 Entry.Ty = IntPtrTy;
1663 Args.push_back(Entry);
1664
1665 TargetLowering::CallLoweringInfo CLI(DAG);
1666 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
1667 .setCallee(CallingConv::C, IntPtrTy,
1668 DAG.getTargetExternalSymbol("__tls_get_addr", getPointerTy()),
1669 std::move(Args), 0);
1670
1671 return LowerCallTo(CLI);
1672}
1673
Roman Divackye3f15c982012-06-04 17:36:38 +00001674SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1675 SelectionDAG &DAG) const {
1676
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001677 // FIXME: TLS addresses currently use medium model code sequences,
1678 // which is the most useful form. Eventually support for small and
1679 // large models could be added if users need it, at the cost of
1680 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001681 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001682 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001683 const GlobalValue *GV = GA->getGlobal();
1684 EVT PtrVT = getPointerTy();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001685 bool is64bit = Subtarget.isPPC64();
Justin Hibbitsa88b6052014-11-12 15:16:30 +00001686 const Module *M = DAG.getMachineFunction().getFunction()->getParent();
1687 PICLevel::Level picLevel = M->getPICLevel();
Roman Divackye3f15c982012-06-04 17:36:38 +00001688
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001689 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001690
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001691 if (Model == TLSModel::LocalExec) {
1692 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001693 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001694 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001695 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001696 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1697 is64bit ? MVT::i64 : MVT::i32);
1698 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1699 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1700 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001701
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001702 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001703 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001704 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1705 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00001706 SDValue GOTPtr;
1707 if (is64bit) {
1708 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1709 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1710 PtrVT, GOTReg, TGA);
1711 } else
1712 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001713 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00001714 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001715 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001716 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001717
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001718 if (Model == TLSModel::GeneralDynamic) {
Bill Schmidt3d9674c2014-11-11 20:44:09 +00001719 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1720 PPCII::MO_TLSGD);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001721 SDValue GOTPtr;
1722 if (is64bit) {
1723 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1724 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1725 GOTReg, TGA);
1726 } else {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00001727 if (picLevel == PICLevel::Small)
1728 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
1729 else
1730 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001731 }
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001732 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
Hal Finkel7c8ae532014-07-25 17:47:22 +00001733 GOTPtr, TGA);
Bill Schmidt3d9674c2014-11-11 20:44:09 +00001734 std::pair<SDValue, SDValue> CallResult = lowerTLSCall(GOTEntry, dl, DAG);
1735 return CallResult.first;
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001736 }
1737
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001738 if (Model == TLSModel::LocalDynamic) {
Bill Schmidt3d9674c2014-11-11 20:44:09 +00001739 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1740 PPCII::MO_TLSLD);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001741 SDValue GOTPtr;
1742 if (is64bit) {
1743 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1744 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1745 GOTReg, TGA);
1746 } else {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00001747 if (picLevel == PICLevel::Small)
1748 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
1749 else
1750 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001751 }
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001752 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
Hal Finkel7c8ae532014-07-25 17:47:22 +00001753 GOTPtr, TGA);
Bill Schmidt3d9674c2014-11-11 20:44:09 +00001754 std::pair<SDValue, SDValue> CallResult = lowerTLSCall(GOTEntry, dl, DAG);
1755 SDValue TLSAddr = CallResult.first;
1756 SDValue Chain = CallResult.second;
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001757 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt3d9674c2014-11-11 20:44:09 +00001758 Chain, TLSAddr, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001759 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1760 }
1761
1762 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00001763}
1764
Chris Lattneredb9d842010-11-15 02:46:57 +00001765SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1766 SelectionDAG &DAG) const {
1767 EVT PtrVT = Op.getValueType();
1768 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001769 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00001770 const GlobalValue *GV = GSDN->getGlobal();
1771
Chris Lattneredb9d842010-11-15 02:46:57 +00001772 // 64-bit SVR4 ABI code is always position-independent.
1773 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001774 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Chris Lattneredb9d842010-11-15 02:46:57 +00001775 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1776 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1777 DAG.getRegister(PPC::X2, MVT::i64));
1778 }
1779
Chris Lattnerdd6df842010-11-15 03:13:19 +00001780 unsigned MOHiFlag, MOLoFlag;
1781 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00001782
Hal Finkel3ee2af72014-07-18 23:29:49 +00001783 if (isPIC && Subtarget.isSVR4ABI()) {
1784 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT,
1785 GSDN->getOffset(),
1786 PPCII::MO_PIC_FLAG);
1787 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i32, GA,
1788 DAG.getNode(PPCISD::GlobalBaseReg, DL, MVT::i32));
1789 }
1790
Chris Lattnerdd6df842010-11-15 03:13:19 +00001791 SDValue GAHi =
1792 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1793 SDValue GALo =
1794 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00001795
Chris Lattnerdd6df842010-11-15 03:13:19 +00001796 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00001797
Chris Lattnerdd6df842010-11-15 03:13:19 +00001798 // If the global reference is actually to a non-lazy-pointer, we have to do an
1799 // extra load to get the address of the global.
1800 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1801 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001802 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00001803 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00001804}
1805
Dan Gohman21cea8a2010-04-17 15:26:15 +00001806SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00001807 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001808 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001809
Hal Finkel777c9dd2014-03-29 16:04:40 +00001810 if (Op.getValueType() == MVT::v2i64) {
1811 // When the operands themselves are v2i64 values, we need to do something
1812 // special because VSX has no underlying comparison operations for these.
1813 if (Op.getOperand(0).getValueType() == MVT::v2i64) {
1814 // Equality can be handled by casting to the legal type for Altivec
1815 // comparisons, everything else needs to be expanded.
1816 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
1817 return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
1818 DAG.getSetCC(dl, MVT::v4i32,
1819 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)),
1820 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)),
1821 CC));
1822 }
1823
1824 return SDValue();
1825 }
1826
1827 // We handle most of these in the usual way.
1828 return Op;
1829 }
1830
Chris Lattner4211ca92006-04-14 06:01:58 +00001831 // If we're comparing for equality to zero, expose the fact that this is
1832 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1833 // fold the new nodes.
1834 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1835 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001836 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001837 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001838 if (VT.bitsLT(MVT::i32)) {
1839 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001840 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001841 }
Duncan Sands13237ac2008-06-06 12:08:01 +00001842 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001843 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1844 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00001845 DAG.getConstant(Log2b, MVT::i32));
1846 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00001847 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001848 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00001849 // optimized. FIXME: revisit this when we can custom lower all setcc
1850 // optimizations.
1851 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001852 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001853 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001854
Chris Lattner4211ca92006-04-14 06:01:58 +00001855 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00001856 // by xor'ing the rhs with the lhs, which is faster than setting a
1857 // condition register, reading it back out, and masking the correct bit. The
1858 // normal approach here uses sub to do this instead of xor. Using xor exposes
1859 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001860 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00001861 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001862 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001863 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00001864 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001865 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00001866 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001867 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001868}
1869
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001870SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001871 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00001872 SDNode *Node = Op.getNode();
1873 EVT VT = Node->getValueType(0);
1874 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1875 SDValue InChain = Node->getOperand(0);
1876 SDValue VAListPtr = Node->getOperand(1);
1877 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001878 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001879
Roman Divacky4394e682011-06-28 15:30:42 +00001880 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1881
1882 // gpr_index
1883 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1884 VAListPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001885 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001886 InChain = GprIndex.getValue(1);
1887
1888 if (VT == MVT::i64) {
1889 // Check if GprIndex is even
1890 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1891 DAG.getConstant(1, MVT::i32));
1892 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1893 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1894 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1895 DAG.getConstant(1, MVT::i32));
1896 // Align GprIndex to be even if it isn't
1897 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1898 GprIndex);
1899 }
1900
1901 // fpr index is 1 byte after gpr
1902 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1903 DAG.getConstant(1, MVT::i32));
1904
1905 // fpr
1906 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1907 FprPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001908 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001909 InChain = FprIndex.getValue(1);
1910
1911 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1912 DAG.getConstant(8, MVT::i32));
1913
1914 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1915 DAG.getConstant(4, MVT::i32));
1916
1917 // areas
1918 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001919 MachinePointerInfo(), false, false,
1920 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001921 InChain = OverflowArea.getValue(1);
1922
1923 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001924 MachinePointerInfo(), false, false,
1925 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001926 InChain = RegSaveArea.getValue(1);
1927
1928 // select overflow_area if index > 8
1929 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1930 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1931
Roman Divacky4394e682011-06-28 15:30:42 +00001932 // adjustment constant gpr_index * 4/8
1933 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1934 VT.isInteger() ? GprIndex : FprIndex,
1935 DAG.getConstant(VT.isInteger() ? 4 : 8,
1936 MVT::i32));
1937
1938 // OurReg = RegSaveArea + RegConstant
1939 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1940 RegConstant);
1941
1942 // Floating types are 32 bytes into RegSaveArea
1943 if (VT.isFloatingPoint())
1944 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1945 DAG.getConstant(32, MVT::i32));
1946
1947 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1948 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1949 VT.isInteger() ? GprIndex : FprIndex,
1950 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1951 MVT::i32));
1952
1953 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1954 VT.isInteger() ? VAListPtr : FprPtr,
1955 MachinePointerInfo(SV),
1956 MVT::i8, false, false, 0);
1957
1958 // determine if we should load from reg_save_area or overflow_area
1959 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1960
1961 // increase overflow_area by 4/8 if gpr/fpr > 8
1962 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1963 DAG.getConstant(VT.isInteger() ? 4 : 8,
1964 MVT::i32));
1965
1966 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1967 OverflowAreaPlusN);
1968
1969 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1970 OverflowAreaPtr,
1971 MachinePointerInfo(),
1972 MVT::i32, false, false, 0);
1973
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00001974 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001975 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001976}
1977
Roman Divackyc3825df2013-07-25 21:36:47 +00001978SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
1979 const PPCSubtarget &Subtarget) const {
1980 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
1981
1982 // We have to copy the entire va_list struct:
1983 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
1984 return DAG.getMemcpy(Op.getOperand(0), Op,
1985 Op.getOperand(1), Op.getOperand(2),
1986 DAG.getConstant(12, MVT::i32), 8, false, true,
1987 MachinePointerInfo(), MachinePointerInfo());
1988}
1989
Duncan Sandsa0984362011-09-06 13:37:06 +00001990SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1991 SelectionDAG &DAG) const {
1992 return Op.getOperand(0);
1993}
1994
1995SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1996 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00001997 SDValue Chain = Op.getOperand(0);
1998 SDValue Trmp = Op.getOperand(1); // trampoline
1999 SDValue FPtr = Op.getOperand(2); // nested function
2000 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00002001 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00002002
Owen Anderson53aa7a92009-08-10 22:56:29 +00002003 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002004 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00002005 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00002006 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00002007 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00002008
Scott Michelcf0da6c2009-02-17 22:15:04 +00002009 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00002010 TargetLowering::ArgListEntry Entry;
2011
2012 Entry.Ty = IntPtrTy;
2013 Entry.Node = Trmp; Args.push_back(Entry);
2014
2015 // TrampSize == (isPPC64 ? 48 : 40);
2016 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00002017 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00002018 Args.push_back(Entry);
2019
2020 Entry.Node = FPtr; Args.push_back(Entry);
2021 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002022
Bill Wendling95e1af22008-09-17 00:30:57 +00002023 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002024 TargetLowering::CallLoweringInfo CLI(DAG);
2025 CLI.setDebugLoc(dl).setChain(Chain)
2026 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002027 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
2028 std::move(Args), 0);
Bill Wendling95e1af22008-09-17 00:30:57 +00002029
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002030 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Duncan Sandsa0984362011-09-06 13:37:06 +00002031 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00002032}
2033
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002034SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002035 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00002036 MachineFunction &MF = DAG.getMachineFunction();
2037 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2038
Andrew Trickef9de2a2013-05-25 02:42:55 +00002039 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002040
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002041 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002042 // vastart just stores the address of the VarArgsFrameIndex slot into the
2043 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002044 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002045 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002046 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00002047 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2048 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00002049 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002050 }
2051
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002052 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002053 // We suppose the given va_list is already allocated.
2054 //
2055 // typedef struct {
2056 // char gpr; /* index into the array of 8 GPRs
2057 // * stored in the register save area
2058 // * gpr=0 corresponds to r3,
2059 // * gpr=1 to r4, etc.
2060 // */
2061 // char fpr; /* index into the array of 8 FPRs
2062 // * stored in the register save area
2063 // * fpr=0 corresponds to f1,
2064 // * fpr=1 to f2, etc.
2065 // */
2066 // char *overflow_arg_area;
2067 // /* location on stack that holds
2068 // * the next overflow argument
2069 // */
2070 // char *reg_save_area;
2071 // /* where r3:r10 and f1:f8 (if saved)
2072 // * are stored
2073 // */
2074 // } va_list[1];
2075
2076
Dan Gohman31ae5862010-04-17 14:41:14 +00002077 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
2078 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002079
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002080
Owen Anderson53aa7a92009-08-10 22:56:29 +00002081 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002082
Dan Gohman31ae5862010-04-17 14:41:14 +00002083 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
2084 PtrVT);
2085 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2086 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002087
Duncan Sands13237ac2008-06-06 12:08:01 +00002088 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002089 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002090
Duncan Sands13237ac2008-06-06 12:08:01 +00002091 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002092 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002093
2094 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002095 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002096
Dan Gohman2d489b52008-02-06 22:27:42 +00002097 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002098
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002099 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002100 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00002101 Op.getOperand(1),
2102 MachinePointerInfo(SV),
2103 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002104 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002105 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002106 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002107
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002108 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002109 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00002110 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
2111 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00002112 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002113 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002114 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002115
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002116 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002117 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00002118 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
2119 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002120 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002121 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002122 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002123
2124 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00002125 return DAG.getStore(thirdStore, dl, FR, nextPtr,
2126 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002127 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002128
Chris Lattner4211ca92006-04-14 06:01:58 +00002129}
2130
Chris Lattner4f2e4e02007-03-06 00:59:59 +00002131#include "PPCGenCallingConv.inc"
2132
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002133// Function whose sole purpose is to kill compiler warnings
2134// stemming from unused functions included from PPCGenCallingConv.inc.
2135CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00002136 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002137}
2138
Bill Schmidt230b4512013-06-12 16:39:22 +00002139bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
2140 CCValAssign::LocInfo &LocInfo,
2141 ISD::ArgFlagsTy &ArgFlags,
2142 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002143 return true;
2144}
2145
Bill Schmidt230b4512013-06-12 16:39:22 +00002146bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
2147 MVT &LocVT,
2148 CCValAssign::LocInfo &LocInfo,
2149 ISD::ArgFlagsTy &ArgFlags,
2150 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002151 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002152 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2153 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2154 };
2155 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002156
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002157 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
2158
2159 // Skip one register if the first unallocated register has an even register
2160 // number and there are still argument registers available which have not been
2161 // allocated yet. RegNum is actually an index into ArgRegs, which means we
2162 // need to skip a register if RegNum is odd.
2163 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
2164 State.AllocateReg(ArgRegs[RegNum]);
2165 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002166
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002167 // Always return false here, as this function only makes sure that the first
2168 // unallocated register has an odd register number and does not actually
2169 // allocate a register for the current argument.
2170 return false;
2171}
2172
Bill Schmidt230b4512013-06-12 16:39:22 +00002173bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
2174 MVT &LocVT,
2175 CCValAssign::LocInfo &LocInfo,
2176 ISD::ArgFlagsTy &ArgFlags,
2177 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002178 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002179 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2180 PPC::F8
2181 };
2182
2183 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002184
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002185 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
2186
2187 // If there is only one Floating-point register left we need to put both f64
2188 // values of a split ppc_fp128 value on the stack.
2189 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
2190 State.AllocateReg(ArgRegs[RegNum]);
2191 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002192
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002193 // Always return false here, as this function only makes sure that the two f64
2194 // values a ppc_fp128 value is split into are both passed in registers or both
2195 // passed on the stack and does not actually allocate a register for the
2196 // current argument.
2197 return false;
2198}
2199
Chris Lattner43df5b32007-02-25 05:34:32 +00002200/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002201/// on Darwin.
Craig Topper840beec2014-04-04 05:16:06 +00002202static const MCPhysReg *GetFPR() {
2203 static const MCPhysReg FPR[] = {
Chris Lattner43df5b32007-02-25 05:34:32 +00002204 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002205 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner43df5b32007-02-25 05:34:32 +00002206 };
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002207
Chris Lattner43df5b32007-02-25 05:34:32 +00002208 return FPR;
2209}
2210
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002211/// CalculateStackSlotSize - Calculates the size reserved for this argument on
2212/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002213static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002214 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00002215 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002216 if (Flags.isByVal())
2217 ArgSize = Flags.getByValSize();
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002218
2219 // Round up to multiples of the pointer size, except for array members,
2220 // which are always packed.
2221 if (!Flags.isInConsecutiveRegs())
2222 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002223
2224 return ArgSize;
2225}
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002226
2227/// CalculateStackSlotAlignment - Calculates the alignment of this argument
2228/// on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002229static unsigned CalculateStackSlotAlignment(EVT ArgVT, EVT OrigVT,
2230 ISD::ArgFlagsTy Flags,
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002231 unsigned PtrByteSize) {
2232 unsigned Align = PtrByteSize;
2233
2234 // Altivec parameters are padded to a 16 byte boundary.
2235 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2236 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
2237 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64)
2238 Align = 16;
2239
2240 // ByVal parameters are aligned as requested.
2241 if (Flags.isByVal()) {
2242 unsigned BVAlign = Flags.getByValAlign();
2243 if (BVAlign > PtrByteSize) {
2244 if (BVAlign % PtrByteSize != 0)
2245 llvm_unreachable(
2246 "ByVal alignment is not a multiple of the pointer size");
2247
2248 Align = BVAlign;
2249 }
2250 }
2251
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002252 // Array members are always packed to their original alignment.
2253 if (Flags.isInConsecutiveRegs()) {
2254 // If the array member was split into multiple registers, the first
2255 // needs to be aligned to the size of the full type. (Except for
2256 // ppcf128, which is only aligned as its f64 components.)
2257 if (Flags.isSplit() && OrigVT != MVT::ppcf128)
2258 Align = OrigVT.getStoreSize();
2259 else
2260 Align = ArgVT.getStoreSize();
2261 }
2262
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002263 return Align;
2264}
2265
Ulrich Weigand8658f172014-07-20 23:43:15 +00002266/// CalculateStackSlotUsed - Return whether this argument will use its
2267/// stack slot (instead of being passed in registers). ArgOffset,
2268/// AvailableFPRs, and AvailableVRs must hold the current argument
2269/// position, and will be updated to account for this argument.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002270static bool CalculateStackSlotUsed(EVT ArgVT, EVT OrigVT,
2271 ISD::ArgFlagsTy Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00002272 unsigned PtrByteSize,
2273 unsigned LinkageSize,
2274 unsigned ParamAreaSize,
2275 unsigned &ArgOffset,
2276 unsigned &AvailableFPRs,
2277 unsigned &AvailableVRs) {
2278 bool UseMemory = false;
2279
2280 // Respect alignment of argument on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002281 unsigned Align =
2282 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigand8658f172014-07-20 23:43:15 +00002283 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
2284 // If there's no space left in the argument save area, we must
2285 // use memory (this check also catches zero-sized arguments).
2286 if (ArgOffset >= LinkageSize + ParamAreaSize)
2287 UseMemory = true;
2288
2289 // Allocate argument on the stack.
2290 ArgOffset += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002291 if (Flags.isInConsecutiveRegsLast())
2292 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand8658f172014-07-20 23:43:15 +00002293 // If we overran the argument save area, we must use memory
2294 // (this check catches arguments passed partially in memory)
2295 if (ArgOffset > LinkageSize + ParamAreaSize)
2296 UseMemory = true;
2297
2298 // However, if the argument is actually passed in an FPR or a VR,
2299 // we don't use memory after all.
2300 if (!Flags.isByVal()) {
2301 if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
2302 if (AvailableFPRs > 0) {
2303 --AvailableFPRs;
2304 return false;
2305 }
2306 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2307 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
2308 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64)
2309 if (AvailableVRs > 0) {
2310 --AvailableVRs;
2311 return false;
2312 }
2313 }
2314
2315 return UseMemory;
2316}
2317
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002318/// EnsureStackAlignment - Round stack frame size up from NumBytes to
2319/// ensure minimum alignment required for target.
2320static unsigned EnsureStackAlignment(const TargetMachine &Target,
2321 unsigned NumBytes) {
Eric Christopherd9134482014-08-04 21:25:23 +00002322 unsigned TargetAlign =
2323 Target.getSubtargetImpl()->getFrameLowering()->getStackAlignment();
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002324 unsigned AlignMask = TargetAlign - 1;
2325 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2326 return NumBytes;
2327}
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002328
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002329SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002330PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002331 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002332 const SmallVectorImpl<ISD::InputArg>
2333 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002334 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002335 SmallVectorImpl<SDValue> &InVals)
2336 const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002337 if (Subtarget.isSVR4ABI()) {
2338 if (Subtarget.isPPC64())
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002339 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
2340 dl, DAG, InVals);
2341 else
2342 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2343 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002344 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002345 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2346 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002347 }
2348}
2349
2350SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002351PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002352 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002353 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002354 const SmallVectorImpl<ISD::InputArg>
2355 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002356 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002357 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002358
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002359 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002360 // +-----------------------------------+
2361 // +--> | Back chain |
2362 // | +-----------------------------------+
2363 // | | Floating-point register save area |
2364 // | +-----------------------------------+
2365 // | | General register save area |
2366 // | +-----------------------------------+
2367 // | | CR save word |
2368 // | +-----------------------------------+
2369 // | | VRSAVE save word |
2370 // | +-----------------------------------+
2371 // | | Alignment padding |
2372 // | +-----------------------------------+
2373 // | | Vector register save area |
2374 // | +-----------------------------------+
2375 // | | Local variable space |
2376 // | +-----------------------------------+
2377 // | | Parameter list area |
2378 // | +-----------------------------------+
2379 // | | LR save word |
2380 // | +-----------------------------------+
2381 // SP--> +--- | Back chain |
2382 // +-----------------------------------+
2383 //
2384 // Specifications:
2385 // System V Application Binary Interface PowerPC Processor Supplement
2386 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002387
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002388 MachineFunction &MF = DAG.getMachineFunction();
2389 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002390 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002391
Owen Anderson53aa7a92009-08-10 22:56:29 +00002392 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002393 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002394 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2395 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002396 unsigned PtrByteSize = 4;
2397
2398 // Assign locations to all of the incoming arguments.
2399 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002400 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
2401 *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002402
2403 // Reserve space for the linkage area on the stack.
Ulrich Weigand8658f172014-07-20 23:43:15 +00002404 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(false, false, false);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002405 CCInfo.AllocateStack(LinkageSize, PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002406
Bill Schmidtef17c142013-02-06 17:33:58 +00002407 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002408
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002409 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2410 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002411
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002412 // Arguments stored in registers.
2413 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002414 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002415 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002416
Owen Anderson9f944592009-08-11 20:47:22 +00002417 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002418 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002419 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002420 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002421 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002422 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002423 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002424 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00002425 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002426 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002427 case MVT::f64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002428 if (Subtarget.hasVSX())
Hal Finkel19be5062014-03-29 05:29:01 +00002429 RC = &PPC::VSFRCRegClass;
2430 else
2431 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002432 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002433 case MVT::v16i8:
2434 case MVT::v8i16:
2435 case MVT::v4i32:
2436 case MVT::v4f32:
Hal Finkel7811c612014-03-28 19:58:11 +00002437 RC = &PPC::VRRCRegClass;
2438 break;
Hal Finkel27774d92014-03-13 07:58:58 +00002439 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002440 case MVT::v2i64:
Hal Finkel7811c612014-03-28 19:58:11 +00002441 RC = &PPC::VSHRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002442 break;
2443 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002444
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002445 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002446 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002447 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2448 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2449
2450 if (ValVT == MVT::i1)
2451 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002452
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002453 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002454 } else {
2455 // Argument stored in memory.
2456 assert(VA.isMemLoc());
2457
Hal Finkel940ab932014-02-28 00:27:01 +00002458 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002459 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002460 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002461
2462 // Create load nodes to retrieve arguments from the stack.
2463 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002464 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2465 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002466 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002467 }
2468 }
2469
2470 // Assign locations to all of the incoming aggregate by value arguments.
2471 // Aggregates passed by value are stored in the local variable space of the
2472 // caller's stack frame, right above the parameter list area.
2473 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002474 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Eric Christopherb5217502014-08-06 18:45:26 +00002475 ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002476
2477 // Reserve stack space for the allocations in CCInfo.
2478 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2479
Bill Schmidtef17c142013-02-06 17:33:58 +00002480 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002481
2482 // Area that is at least reserved in the caller of this function.
2483 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002484 MinReservedArea = std::max(MinReservedArea, LinkageSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00002485
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002486 // Set the size that is at least reserved in caller of this function. Tail
2487 // call optimized function's reserved stack space needs to be aligned so that
2488 // taking the difference between two stack areas will result in an aligned
2489 // stack.
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002490 MinReservedArea = EnsureStackAlignment(MF.getTarget(), MinReservedArea);
2491 FuncInfo->setMinReservedArea(MinReservedArea);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002492
2493 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002494
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002495 // If the function takes variable number of arguments, make a frame index for
2496 // the start of the first vararg value... for expansion of llvm.va_start.
2497 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +00002498 static const MCPhysReg GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002499 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2500 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2501 };
2502 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2503
Craig Topper840beec2014-04-04 05:16:06 +00002504 static const MCPhysReg FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002505 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2506 PPC::F8
2507 };
Joerg Sonnenbergereb8655a2014-08-08 16:46:10 +00002508 unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2509 if (DisablePPCFloatInVariadic)
2510 NumFPArgRegs = 0;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002511
Dan Gohman31ae5862010-04-17 14:41:14 +00002512 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2513 NumGPArgRegs));
2514 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2515 NumFPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002516
2517 // Make room for NumGPArgRegs and NumFPArgRegs.
2518 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Craig Topper7ff15922014-09-10 04:51:36 +00002519 NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002520
Dan Gohman31ae5862010-04-17 14:41:14 +00002521 FuncInfo->setVarArgsStackOffset(
2522 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002523 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002524
Dan Gohman31ae5862010-04-17 14:41:14 +00002525 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2526 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002527
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002528 // The fixed integer arguments of a variadic function are stored to the
2529 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2530 // the result of va_next.
2531 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2532 // Get an existing live-in vreg, or add a new one.
2533 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2534 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002535 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002536
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002537 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002538 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2539 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002540 MemOps.push_back(Store);
2541 // Increment the address by four for the next argument to store
2542 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2543 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2544 }
2545
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002546 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2547 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002548 // The double arguments are stored to the VarArgsFrameIndex
2549 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002550 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2551 // Get an existing live-in vreg, or add a new one.
2552 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2553 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002554 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002555
Owen Anderson9f944592009-08-11 20:47:22 +00002556 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002557 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2558 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002559 MemOps.push_back(Store);
2560 // Increment the address by eight for the next argument to store
Craig Topper7ff15922014-09-10 04:51:36 +00002561 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002562 PtrVT);
2563 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2564 }
2565 }
2566
2567 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002568 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002569
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002570 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002571}
2572
Bill Schmidt57d6de52012-10-23 15:51:16 +00002573// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2574// value to MVT::i64 and then truncate to the correct register size.
2575SDValue
2576PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2577 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002578 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002579 if (Flags.isSExt())
2580 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2581 DAG.getValueType(ObjectVT));
2582 else if (Flags.isZExt())
2583 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2584 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002585
Hal Finkel940ab932014-02-28 00:27:01 +00002586 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002587}
2588
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002589SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002590PPCTargetLowering::LowerFormalArguments_64SVR4(
2591 SDValue Chain,
2592 CallingConv::ID CallConv, bool isVarArg,
2593 const SmallVectorImpl<ISD::InputArg>
2594 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002595 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002596 SmallVectorImpl<SDValue> &InVals) const {
2597 // TODO: add description of PPC stack frame format, or at least some docs.
2598 //
Ulrich Weigand8658f172014-07-20 23:43:15 +00002599 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00002600 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002601 MachineFunction &MF = DAG.getMachineFunction();
2602 MachineFrameInfo *MFI = MF.getFrameInfo();
2603 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2604
2605 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2606 // Potential tail calls could cause overwriting of argument stack slots.
2607 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2608 (CallConv == CallingConv::Fast));
2609 unsigned PtrByteSize = 8;
2610
Ulrich Weigand8658f172014-07-20 23:43:15 +00002611 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(true, false,
2612 isELFv2ABI);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002613
Craig Topper840beec2014-04-04 05:16:06 +00002614 static const MCPhysReg GPR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002615 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2616 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2617 };
2618
Craig Topper840beec2014-04-04 05:16:06 +00002619 static const MCPhysReg *FPR = GetFPR();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002620
Craig Topper840beec2014-04-04 05:16:06 +00002621 static const MCPhysReg VR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002622 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2623 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2624 };
Craig Topper840beec2014-04-04 05:16:06 +00002625 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00002626 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
2627 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
2628 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002629
2630 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2631 const unsigned Num_FPR_Regs = 13;
2632 const unsigned Num_VR_Regs = array_lengthof(VR);
2633
Ulrich Weigand8658f172014-07-20 23:43:15 +00002634 // Do a first pass over the arguments to determine whether the ABI
2635 // guarantees that our caller has allocated the parameter save area
2636 // on its stack frame. In the ELFv1 ABI, this is always the case;
2637 // in the ELFv2 ABI, it is true if this is a vararg function or if
2638 // any parameter is located in a stack slot.
2639
2640 bool HasParameterArea = !isELFv2ABI || isVarArg;
2641 unsigned ParamAreaSize = Num_GPR_Regs * PtrByteSize;
2642 unsigned NumBytes = LinkageSize;
2643 unsigned AvailableFPRs = Num_FPR_Regs;
2644 unsigned AvailableVRs = Num_VR_Regs;
2645 for (unsigned i = 0, e = Ins.size(); i != e; ++i)
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002646 if (CalculateStackSlotUsed(Ins[i].VT, Ins[i].ArgVT, Ins[i].Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00002647 PtrByteSize, LinkageSize, ParamAreaSize,
2648 NumBytes, AvailableFPRs, AvailableVRs))
2649 HasParameterArea = true;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002650
2651 // Add DAG nodes to load the arguments or copy them out of registers. On
2652 // entry to a function on PPC, the arguments start after the linkage area,
2653 // although the first ones are often in registers.
2654
Ulrich Weigand8658f172014-07-20 23:43:15 +00002655 unsigned ArgOffset = LinkageSize;
2656 unsigned GPR_idx, FPR_idx = 0, VR_idx = 0;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002657 SmallVector<SDValue, 8> MemOps;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002658 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002659 unsigned CurArgIdx = 0;
2660 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002661 SDValue ArgVal;
2662 bool needsLoad = false;
2663 EVT ObjectVT = Ins[ArgNo].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002664 EVT OrigVT = Ins[ArgNo].ArgVT;
Hal Finkel940ab932014-02-28 00:27:01 +00002665 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002666 unsigned ArgSize = ObjSize;
2667 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt6631e942013-02-20 17:31:41 +00002668 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2669 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002670
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002671 /* Respect alignment of argument on the stack. */
2672 unsigned Align =
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002673 CalculateStackSlotAlignment(ObjectVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002674 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002675 unsigned CurArgOffset = ArgOffset;
2676
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002677 /* Compute GPR index associated with argument offset. */
2678 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
2679 GPR_idx = std::min(GPR_idx, Num_GPR_Regs);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002680
2681 // FIXME the codegen can be much improved in some cases.
2682 // We do not have to keep everything in memory.
2683 if (Flags.isByVal()) {
2684 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2685 ObjSize = Flags.getByValSize();
2686 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002687 // Empty aggregate parameters do not take up registers. Examples:
2688 // struct { } a;
2689 // union { } b;
2690 // int c[0];
2691 // etc. However, we have to provide a place-holder in InVals, so
2692 // pretend we have an 8-byte item at the current address for that
2693 // purpose.
2694 if (!ObjSize) {
2695 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2696 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2697 InVals.push_back(FIN);
2698 continue;
2699 }
Hal Finkel262a2242013-09-12 23:20:06 +00002700
Ulrich Weigand24195972014-07-20 22:36:52 +00002701 // Create a stack object covering all stack doublewords occupied
Ulrich Weigand8658f172014-07-20 23:43:15 +00002702 // by the argument. If the argument is (fully or partially) on
2703 // the stack, or if the argument is fully in registers but the
2704 // caller has allocated the parameter save anyway, we can refer
2705 // directly to the caller's stack frame. Otherwise, create a
2706 // local copy in our own frame.
2707 int FI;
2708 if (HasParameterArea ||
2709 ArgSize + ArgOffset > LinkageSize + Num_GPR_Regs * PtrByteSize)
Hal Finkel41a55ad2014-08-16 00:17:05 +00002710 FI = MFI->CreateFixedObject(ArgSize, ArgOffset, false, true);
Ulrich Weigand8658f172014-07-20 23:43:15 +00002711 else
2712 FI = MFI->CreateStackObject(ArgSize, Align, false);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002713 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002714
Ulrich Weigand24195972014-07-20 22:36:52 +00002715 // Handle aggregates smaller than 8 bytes.
2716 if (ObjSize < PtrByteSize) {
2717 // The value of the object is its address, which differs from the
2718 // address of the enclosing doubleword on big-endian systems.
2719 SDValue Arg = FIN;
2720 if (!isLittleEndian) {
2721 SDValue ArgOff = DAG.getConstant(PtrByteSize - ObjSize, PtrVT);
2722 Arg = DAG.getNode(ISD::ADD, dl, ArgOff.getValueType(), Arg, ArgOff);
2723 }
2724 InVals.push_back(Arg);
2725
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002726 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002727 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002728 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002729 SDValue Store;
2730
2731 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2732 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2733 (ObjSize == 2 ? MVT::i16 : MVT::i32));
Ulrich Weigand24195972014-07-20 22:36:52 +00002734 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, Arg,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002735 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002736 ObjType, false, false, 0);
2737 } else {
2738 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2739 // store the whole register as-is to the parameter save area
Ulrich Weigand24195972014-07-20 22:36:52 +00002740 // slot.
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002741 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002742 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002743 false, false, 0);
2744 }
2745
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002746 MemOps.push_back(Store);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002747 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002748 // Whether we copied from a register or not, advance the offset
2749 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002750 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002751 continue;
2752 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002753
Ulrich Weigand24195972014-07-20 22:36:52 +00002754 // The value of the object is its address, which is the address of
2755 // its first stack doubleword.
2756 InVals.push_back(FIN);
2757
2758 // Store whatever pieces of the object are in registers to memory.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002759 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
Ulrich Weigand24195972014-07-20 22:36:52 +00002760 if (GPR_idx == Num_GPR_Regs)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002761 break;
Ulrich Weigand24195972014-07-20 22:36:52 +00002762
2763 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2764 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2765 SDValue Addr = FIN;
2766 if (j) {
2767 SDValue Off = DAG.getConstant(j, PtrVT);
2768 Addr = DAG.getNode(ISD::ADD, dl, Off.getValueType(), Addr, Off);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002769 }
Ulrich Weigand24195972014-07-20 22:36:52 +00002770 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, Addr,
2771 MachinePointerInfo(FuncArg, j),
2772 false, false, 0);
2773 MemOps.push_back(Store);
2774 ++GPR_idx;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002775 }
Ulrich Weigand24195972014-07-20 22:36:52 +00002776 ArgOffset += ArgSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002777 continue;
2778 }
2779
2780 switch (ObjectVT.getSimpleVT().SimpleTy) {
2781 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00002782 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002783 case MVT::i32:
2784 case MVT::i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002785 // These can be scalar arguments or elements of an integer array type
2786 // passed directly. Clang may use those instead of "byval" aggregate
2787 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002788 if (GPR_idx != Num_GPR_Regs) {
2789 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2790 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2791
Hal Finkel940ab932014-02-28 00:27:01 +00002792 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002793 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2794 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002795 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002796 } else {
2797 needsLoad = true;
2798 ArgSize = PtrByteSize;
2799 }
2800 ArgOffset += 8;
2801 break;
2802
2803 case MVT::f32:
2804 case MVT::f64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002805 // These can be scalar arguments or elements of a float array type
2806 // passed directly. The latter are used to implement ELFv2 homogenous
2807 // float aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002808 if (FPR_idx != Num_FPR_Regs) {
2809 unsigned VReg;
2810
2811 if (ObjectVT == MVT::f32)
2812 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2813 else
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002814 VReg = MF.addLiveIn(FPR[FPR_idx], Subtarget.hasVSX() ?
Hal Finkel19be5062014-03-29 05:29:01 +00002815 &PPC::VSFRCRegClass :
2816 &PPC::F8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002817
2818 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2819 ++FPR_idx;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002820 } else if (GPR_idx != Num_GPR_Regs) {
2821 // This can only ever happen in the presence of f32 array types,
2822 // since otherwise we never run out of FPRs before running out
2823 // of GPRs.
2824 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2825 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2826
2827 if (ObjectVT == MVT::f32) {
2828 if ((ArgOffset % PtrByteSize) == (isLittleEndian ? 4 : 0))
2829 ArgVal = DAG.getNode(ISD::SRL, dl, MVT::i64, ArgVal,
2830 DAG.getConstant(32, MVT::i32));
2831 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
2832 }
2833
2834 ArgVal = DAG.getNode(ISD::BITCAST, dl, ObjectVT, ArgVal);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002835 } else {
2836 needsLoad = true;
2837 }
2838
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002839 // When passing an array of floats, the array occupies consecutive
2840 // space in the argument area; only round up to the next doubleword
2841 // at the end of the array. Otherwise, each float takes 8 bytes.
2842 ArgSize = Flags.isInConsecutiveRegs() ? ObjSize : PtrByteSize;
2843 ArgOffset += ArgSize;
2844 if (Flags.isInConsecutiveRegsLast())
2845 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002846 break;
2847 case MVT::v4f32:
2848 case MVT::v4i32:
2849 case MVT::v8i16:
2850 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00002851 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002852 case MVT::v2i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002853 // These can be scalar arguments or elements of a vector array type
2854 // passed directly. The latter are used to implement ELFv2 homogenous
2855 // vector aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002856 if (VR_idx != Num_VR_Regs) {
Hal Finkel7811c612014-03-28 19:58:11 +00002857 unsigned VReg = (ObjectVT == MVT::v2f64 || ObjectVT == MVT::v2i64) ?
2858 MF.addLiveIn(VSRH[VR_idx], &PPC::VSHRCRegClass) :
2859 MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002860 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002861 ++VR_idx;
2862 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002863 needsLoad = true;
2864 }
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00002865 ArgOffset += 16;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002866 break;
2867 }
2868
2869 // We need to load the argument to a virtual register if we determined
2870 // above that we ran out of physical registers of the appropriate type.
2871 if (needsLoad) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00002872 if (ObjSize < ArgSize && !isLittleEndian)
2873 CurArgOffset += ArgSize - ObjSize;
2874 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, isImmutable);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002875 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2876 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2877 false, false, false, 0);
2878 }
2879
2880 InVals.push_back(ArgVal);
2881 }
2882
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002883 // Area that is at least reserved in the caller of this function.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002884 unsigned MinReservedArea;
Ulrich Weigand8658f172014-07-20 23:43:15 +00002885 if (HasParameterArea)
2886 MinReservedArea = std::max(ArgOffset, LinkageSize + 8 * PtrByteSize);
2887 else
2888 MinReservedArea = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002889
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002890 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002891 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002892 // taking the difference between two stack areas will result in an aligned
2893 // stack.
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002894 MinReservedArea = EnsureStackAlignment(MF.getTarget(), MinReservedArea);
2895 FuncInfo->setMinReservedArea(MinReservedArea);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002896
2897 // If the function takes variable number of arguments, make a frame index for
2898 // the start of the first vararg value... for expansion of llvm.va_start.
2899 if (isVarArg) {
2900 int Depth = ArgOffset;
2901
2902 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00002903 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002904 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2905
2906 // If this function is vararg, store any remaining integer argument regs
2907 // to their spots on the stack so that they may be loaded by deferencing the
2908 // result of va_next.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002909 for (GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
2910 GPR_idx < Num_GPR_Regs; ++GPR_idx) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002911 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2912 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2913 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2914 MachinePointerInfo(), false, false, 0);
2915 MemOps.push_back(Store);
2916 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00002917 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002918 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2919 }
2920 }
2921
2922 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002923 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002924
2925 return Chain;
2926}
2927
2928SDValue
2929PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002930 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002931 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002932 const SmallVectorImpl<ISD::InputArg>
2933 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002934 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002935 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002936 // TODO: add description of PPC stack frame format, or at least some docs.
2937 //
2938 MachineFunction &MF = DAG.getMachineFunction();
2939 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002940 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002941
Owen Anderson53aa7a92009-08-10 22:56:29 +00002942 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002943 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002944 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002945 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2946 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00002947 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey48850c12006-11-16 22:43:37 +00002948
Ulrich Weigand8658f172014-07-20 23:43:15 +00002949 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(isPPC64, true,
2950 false);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002951 unsigned ArgOffset = LinkageSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002952 // Area that is at least reserved in caller of this function.
2953 unsigned MinReservedArea = ArgOffset;
2954
Craig Topper840beec2014-04-04 05:16:06 +00002955 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002956 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2957 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2958 };
Craig Topper840beec2014-04-04 05:16:06 +00002959 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00002960 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2961 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2962 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00002963
Craig Topper840beec2014-04-04 05:16:06 +00002964 static const MCPhysReg *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002965
Craig Topper840beec2014-04-04 05:16:06 +00002966 static const MCPhysReg VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002967 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2968 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2969 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00002970
Owen Andersone2f23a32007-09-07 04:06:50 +00002971 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002972 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00002973 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00002974
2975 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002976
Craig Topper840beec2014-04-04 05:16:06 +00002977 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002978
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002979 // In 32-bit non-varargs functions, the stack space for vectors is after the
2980 // stack space for non-vectors. We do not use this space unless we have
2981 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00002982 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002983 // that out...for the pathological case, compute VecArgOffset as the
2984 // start of the vector parameter area. Computing VecArgOffset is the
2985 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002986 unsigned VecArgOffset = ArgOffset;
2987 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002988 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002989 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002990 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002991 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002992
Duncan Sandsd97eea32008-03-21 09:14:45 +00002993 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002994 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00002995 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002996 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002997 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2998 VecArgOffset += ArgSize;
2999 continue;
3000 }
3001
Owen Anderson9f944592009-08-11 20:47:22 +00003002 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003003 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003004 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003005 case MVT::i32:
3006 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003007 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003008 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003009 case MVT::i64: // PPC64
3010 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003011 // FIXME: We are guaranteed to be !isPPC64 at this point.
3012 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003013 VecArgOffset += 8;
3014 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003015 case MVT::v4f32:
3016 case MVT::v4i32:
3017 case MVT::v8i16:
3018 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003019 // Nothing to do, we're only looking at Nonvector args here.
3020 break;
3021 }
3022 }
3023 }
3024 // We've found where the vector parameter area in memory is. Skip the
3025 // first 12 parameters; these don't use that memory.
3026 VecArgOffset = ((VecArgOffset+15)/16)*16;
3027 VecArgOffset += 12*16;
3028
Chris Lattner4302e8f2006-05-16 18:18:50 +00003029 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00003030 // entry to a function on PPC, the arguments start after the linkage area,
3031 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00003032
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003033 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003034 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00003035 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00003036 unsigned CurArgIdx = 0;
3037 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003038 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003039 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00003040 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00003041 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00003042 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003043 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt38b6cb52013-05-08 17:22:33 +00003044 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
3045 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003046
Chris Lattner318f0d22006-05-16 18:51:52 +00003047 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003048
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003049 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00003050 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
3051 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003052 if (isVarArg || isPPC64) {
3053 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003054 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003055 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003056 PtrByteSize);
3057 } else nAltivecParamsAtEnd++;
3058 } else
3059 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003060 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003061 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003062 PtrByteSize);
3063
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003064 // FIXME the codegen can be much improved in some cases.
3065 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003066 if (Flags.isByVal()) {
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003067 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003068 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003069 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003070 // Objects of size 1 and 2 are right justified, everything else is
3071 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00003072 if (ObjSize==1 || ObjSize==2) {
3073 CurArgOffset = CurArgOffset + (4 - ObjSize);
3074 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003075 // The value of the object is its address.
Hal Finkel41a55ad2014-08-16 00:17:05 +00003076 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, false, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003077 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003078 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003079 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00003080 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003081 unsigned VReg;
3082 if (isPPC64)
3083 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3084 else
3085 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003086 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003087 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003088 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003089 MachinePointerInfo(FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003090 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00003091 MemOps.push_back(Store);
3092 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00003093 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003094
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003095 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00003096
Dale Johannesen21a8f142008-03-08 01:41:42 +00003097 continue;
3098 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003099 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
3100 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003101 // to memory. ArgOffset will be the address of the beginning
3102 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003103 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003104 unsigned VReg;
3105 if (isPPC64)
3106 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3107 else
3108 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00003109 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003110 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003111 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003112 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003113 MachinePointerInfo(FuncArg, j),
David Greene87a5abe2010-02-15 16:56:53 +00003114 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003115 MemOps.push_back(Store);
3116 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003117 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003118 } else {
3119 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
3120 break;
3121 }
3122 }
3123 continue;
3124 }
3125
Owen Anderson9f944592009-08-11 20:47:22 +00003126 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003127 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003128 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003129 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00003130 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00003131 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003132 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003133 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Hal Finkel7f908e82014-03-06 00:45:19 +00003134
3135 if (ObjectVT == MVT::i1)
3136 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal);
3137
Bill Wendling968f32c2008-03-07 20:49:02 +00003138 ++GPR_idx;
3139 } else {
3140 needsLoad = true;
3141 ArgSize = PtrByteSize;
3142 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003143 // All int arguments reserve stack space in the Darwin ABI.
3144 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00003145 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003146 }
Bill Wendling968f32c2008-03-07 20:49:02 +00003147 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00003148 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00003149 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003150 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003151 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00003152
Hal Finkel940ab932014-02-28 00:27:01 +00003153 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00003154 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00003155 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003156 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00003157
Chris Lattnerec78cad2006-06-26 22:48:35 +00003158 ++GPR_idx;
3159 } else {
3160 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00003161 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003162 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003163 // All int arguments reserve stack space in the Darwin ABI.
3164 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003165 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003166
Owen Anderson9f944592009-08-11 20:47:22 +00003167 case MVT::f32:
3168 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00003169 // Every 4 bytes of argument space consumes one of the GPRs available for
3170 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003171 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003172 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00003173 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003174 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00003175 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003176 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003177 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003178
Owen Anderson9f944592009-08-11 20:47:22 +00003179 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00003180 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003181 else
Devang Patelf3292b22011-02-21 23:21:26 +00003182 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003183
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003184 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003185 ++FPR_idx;
3186 } else {
3187 needsLoad = true;
3188 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003189
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003190 // All FP arguments reserve stack space in the Darwin ABI.
3191 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003192 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003193 case MVT::v4f32:
3194 case MVT::v4i32:
3195 case MVT::v8i16:
3196 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00003197 // Note that vector arguments in registers don't reserve stack space,
3198 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003199 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003200 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003201 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00003202 if (isVarArg) {
3203 while ((ArgOffset % 16) != 0) {
3204 ArgOffset += PtrByteSize;
3205 if (GPR_idx != Num_GPR_Regs)
3206 GPR_idx++;
3207 }
3208 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003209 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00003210 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003211 ++VR_idx;
3212 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003213 if (!isVarArg && !isPPC64) {
3214 // Vectors go after all the nonvectors.
3215 CurArgOffset = VecArgOffset;
3216 VecArgOffset += 16;
3217 } else {
3218 // Vectors are aligned.
3219 ArgOffset = ((ArgOffset+15)/16)*16;
3220 CurArgOffset = ArgOffset;
3221 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00003222 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003223 needsLoad = true;
3224 }
3225 break;
3226 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003227
Chris Lattner4302e8f2006-05-16 18:18:50 +00003228 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003229 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003230 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003231 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003232 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00003233 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003234 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00003235 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003236 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003237 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003238
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003239 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003240 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003241
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003242 // Allow for Altivec parameters at the end, if needed.
3243 if (nAltivecParamsAtEnd) {
3244 MinReservedArea = ((MinReservedArea+15)/16)*16;
3245 MinReservedArea += 16*nAltivecParamsAtEnd;
3246 }
3247
3248 // Area that is at least reserved in the caller of this function.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003249 MinReservedArea = std::max(MinReservedArea, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003250
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003251 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003252 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003253 // taking the difference between two stack areas will result in an aligned
3254 // stack.
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003255 MinReservedArea = EnsureStackAlignment(MF.getTarget(), MinReservedArea);
3256 FuncInfo->setMinReservedArea(MinReservedArea);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003257
Chris Lattner4302e8f2006-05-16 18:18:50 +00003258 // If the function takes variable number of arguments, make a frame index for
3259 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003260 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003261 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003262
Dan Gohman31ae5862010-04-17 14:41:14 +00003263 FuncInfo->setVarArgsFrameIndex(
3264 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00003265 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00003266 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003267
Chris Lattner4302e8f2006-05-16 18:18:50 +00003268 // If this function is vararg, store any remaining integer argument regs
3269 // to their spots on the stack so that they may be loaded by deferencing the
3270 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003271 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00003272 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00003273
Chris Lattner2cca3852006-11-18 01:57:19 +00003274 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00003275 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003276 else
Devang Patelf3292b22011-02-21 23:21:26 +00003277 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003278
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003279 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00003280 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3281 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003282 MemOps.push_back(Store);
3283 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003284 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00003285 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003286 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003287 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003288
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003289 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003290 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003291
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003292 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003293}
3294
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003295/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003296/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00003297static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003298 unsigned ParamSize) {
3299
Dale Johannesen86dcae12009-11-24 01:09:07 +00003300 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003301
3302 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
3303 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3304 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3305 // Remember only if the new adjustement is bigger.
3306 if (SPDiff < FI->getTailCallSPDelta())
3307 FI->setTailCallSPDelta(SPDiff);
3308
3309 return SPDiff;
3310}
3311
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003312/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3313/// for tail call optimization. Targets which want to do tail call
3314/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003315bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003316PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003317 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003318 bool isVarArg,
3319 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003320 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003321 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00003322 return false;
3323
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003324 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003325 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003326 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003327
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003328 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00003329 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003330 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
3331 // Functions containing by val parameters are not supported.
3332 for (unsigned i = 0; i != Ins.size(); i++) {
3333 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3334 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003335 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003336
Alp Tokerf907b892013-12-05 05:44:44 +00003337 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003338 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
3339 return true;
3340
3341 // At the moment we can only do local tail calls (in same module, hidden
3342 // or protected) if we are generating PIC.
3343 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
3344 return G->getGlobal()->hasHiddenVisibility()
3345 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003346 }
3347
3348 return false;
3349}
3350
Chris Lattnereb755fc2006-05-17 19:00:46 +00003351/// isCallCompatibleAddress - Return the immediate to use if the specified
3352/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003353static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00003354 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003355 if (!C) return nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003356
Dan Gohmaneffb8942008-09-12 16:56:44 +00003357 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003358 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00003359 SignExtend32<26>(Addr) != Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +00003360 return nullptr; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003361
Dan Gohmaneffb8942008-09-12 16:56:44 +00003362 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00003363 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003364}
3365
Dan Gohmand78c4002008-05-13 00:00:25 +00003366namespace {
3367
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003368struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003369 SDValue Arg;
3370 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003371 int FrameIdx;
3372
3373 TailCallArgumentInfo() : FrameIdx(0) {}
3374};
3375
Dan Gohmand78c4002008-05-13 00:00:25 +00003376}
3377
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003378/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3379static void
3380StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003381 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003382 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3383 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003384 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003385 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003386 SDValue Arg = TailCallArgs[i].Arg;
3387 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003388 int FI = TailCallArgs[i].FrameIdx;
3389 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003390 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003391 MachinePointerInfo::getFixedStack(FI),
3392 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003393 }
3394}
3395
3396/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3397/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003398static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003399 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003400 SDValue Chain,
3401 SDValue OldRetAddr,
3402 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003403 int SPDiff,
3404 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003405 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003406 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003407 if (SPDiff) {
3408 // Calculate the new stack slot for the return address.
3409 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003410 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003411 isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003412 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003413 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003414 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003415 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003416 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003417 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003418 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003419
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003420 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3421 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003422 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003423 int NewFPLoc =
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003424 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene1fbe0542009-11-12 20:49:22 +00003425 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003426 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003427 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3428 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003429 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003430 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003431 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003432 }
3433 return Chain;
3434}
3435
3436/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3437/// the position of the argument.
3438static void
3439CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003440 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003441 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003442 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003443 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003444 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003445 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003446 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003447 TailCallArgumentInfo Info;
3448 Info.Arg = Arg;
3449 Info.FrameIdxOp = FIN;
3450 Info.FrameIdx = FI;
3451 TailCallArguments.push_back(Info);
3452}
3453
3454/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3455/// stack slot. Returns the chain as result and the loaded frame pointers in
3456/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003457SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003458 int SPDiff,
3459 SDValue Chain,
3460 SDValue &LROpOut,
3461 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003462 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003463 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003464 if (SPDiff) {
3465 // Load the LR and FP stack slot for later adjusting.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003466 EVT VT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003467 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003468 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003469 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003470 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003471
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003472 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3473 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003474 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003475 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003476 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003477 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003478 Chain = SDValue(FPOpOut.getNode(), 1);
3479 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003480 }
3481 return Chain;
3482}
3483
Dale Johannesen85d41a12008-03-04 23:17:14 +00003484/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003485/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003486/// specified by the specific parameter attribute. The copy will be passed as
3487/// a byval function parameter.
3488/// Sometimes what we are copying is the end of a larger object, the part that
3489/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003490static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003491CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003492 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003493 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003494 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003495 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003496 false, false, MachinePointerInfo(),
3497 MachinePointerInfo());
Dale Johannesen85d41a12008-03-04 23:17:14 +00003498}
Chris Lattner43df5b32007-02-25 05:34:32 +00003499
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003500/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3501/// tail calls.
3502static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003503LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3504 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003505 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003506 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3507 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003508 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003509 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003510 if (!isTailCall) {
3511 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003512 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003513 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003514 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003515 else
Owen Anderson9f944592009-08-11 20:47:22 +00003516 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003517 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003518 DAG.getConstant(ArgOffset, PtrVT));
3519 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003520 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3521 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003522 // Calculate and remember argument location.
3523 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3524 TailCallArguments);
3525}
3526
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003527static
3528void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003529 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003530 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003531 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003532 MachineFunction &MF = DAG.getMachineFunction();
3533
3534 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3535 // might overwrite each other in case of tail call optimization.
3536 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003537 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003538 InFlag = SDValue();
3539 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3540 MemOpChains2, dl);
3541 if (!MemOpChains2.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003542 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003543
3544 // Store the return address to the appropriate stack slot.
3545 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3546 isPPC64, isDarwinABI, dl);
3547
3548 // Emit callseq_end just before tailcall node.
3549 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003550 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003551 InFlag = Chain.getValue(1);
3552}
3553
3554static
3555unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003556 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003557 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3558 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003559 const PPCSubtarget &Subtarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003560
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003561 bool isPPC64 = Subtarget.isPPC64();
3562 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003563 bool isELFv2ABI = Subtarget.isELFv2ABI();
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003564
Owen Anderson53aa7a92009-08-10 22:56:29 +00003565 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003566 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003567 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003568
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003569 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003570
Torok Edwin31e90d22010-08-04 20:47:44 +00003571 bool needIndirectCall = true;
Ulrich Weigand9aa09ef2014-06-18 16:14:04 +00003572 if (!isSVR4ABI || !isPPC64)
3573 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
3574 // If this is an absolute destination address, use the munged value.
3575 Callee = SDValue(Dest, 0);
3576 needIndirectCall = false;
3577 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003578
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003579 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Eric Christopher79cc1e32014-09-02 22:28:02 +00003580 unsigned OpFlags = 0;
3581 if ((DAG.getTarget().getRelocationModel() != Reloc::Static &&
3582 (Subtarget.getTargetTriple().isMacOSX() &&
3583 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
3584 (G->getGlobal()->isDeclaration() ||
3585 G->getGlobal()->isWeakForLinker())) ||
3586 (Subtarget.isTargetELF() && !isPPC64 &&
3587 !G->getGlobal()->hasLocalLinkage() &&
3588 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
3589 // PC-relative references to external symbols should go through $stub,
3590 // unless we're building with the leopard linker or later, which
3591 // automatically synthesizes these stubs.
3592 OpFlags = PPCII::MO_PLT_OR_STUB;
Eric Christopherb9fd9ed2014-08-07 22:02:54 +00003593 }
Eric Christopher79cc1e32014-09-02 22:28:02 +00003594
3595 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3596 // every direct call is) turn it into a TargetGlobalAddress /
3597 // TargetExternalSymbol node so that legalize doesn't hack it.
3598 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
3599 Callee.getValueType(), 0, OpFlags);
3600 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003601 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003602
Torok Edwin31e90d22010-08-04 20:47:44 +00003603 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003604 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003605
Hal Finkel3ee2af72014-07-18 23:29:49 +00003606 if ((DAG.getTarget().getRelocationModel() != Reloc::Static &&
3607 (Subtarget.getTargetTriple().isMacOSX() &&
3608 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5))) ||
3609 (Subtarget.isTargetELF() && !isPPC64 &&
3610 DAG.getTarget().getRelocationModel() == Reloc::PIC_) ) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003611 // PC-relative references to external symbols should go through $stub,
3612 // unless we're building with the leopard linker or later, which
3613 // automatically synthesizes these stubs.
Hal Finkel3ee2af72014-07-18 23:29:49 +00003614 OpFlags = PPCII::MO_PLT_OR_STUB;
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003615 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003616
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003617 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3618 OpFlags);
3619 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003620 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003621
Torok Edwin31e90d22010-08-04 20:47:44 +00003622 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003623 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3624 // to do the call, we can't use PPCISD::CALL.
3625 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003626
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003627 if (isSVR4ABI && isPPC64 && !isELFv2ABI) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003628 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3629 // entry point, but to the function descriptor (the function entry point
3630 // address is part of the function descriptor though).
3631 // The function descriptor is a three doubleword structure with the
3632 // following fields: function entry point, TOC base address and
3633 // environment pointer.
3634 // Thus for a call through a function pointer, the following actions need
3635 // to be performed:
3636 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003637 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003638 // 2. Load the address of the function entry point from the function
3639 // descriptor.
3640 // 3. Load the TOC of the callee from the function descriptor into r2.
3641 // 4. Load the environment pointer from the function descriptor into
3642 // r11.
3643 // 5. Branch to the function entry point address.
3644 // 6. On return of the callee, the TOC of the caller needs to be
3645 // restored (this is done in FinishCall()).
3646 //
3647 // All those operations are flagged together to ensure that no other
3648 // operations can be scheduled in between. E.g. without flagging the
3649 // operations together, a TOC access in the caller could be scheduled
3650 // between the load of the callee TOC and the branch to the callee, which
3651 // results in the TOC access going through the TOC of the callee instead
3652 // of going through the TOC of the caller, which leads to incorrect code.
3653
3654 // Load the address of the function entry point from the function
3655 // descriptor.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003656 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00003657 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00003658 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
Tilmann Scheller79fef932009-12-18 13:00:15 +00003659 Chain = LoadFuncPtr.getValue(1);
3660 InFlag = LoadFuncPtr.getValue(2);
3661
3662 // Load environment pointer into r11.
3663 // Offset of the environment pointer within the function descriptor.
3664 SDValue PtrOff = DAG.getIntPtrConstant(16);
3665
3666 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3667 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3668 InFlag);
3669 Chain = LoadEnvPtr.getValue(1);
3670 InFlag = LoadEnvPtr.getValue(2);
3671
3672 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3673 InFlag);
3674 Chain = EnvVal.getValue(0);
3675 InFlag = EnvVal.getValue(1);
3676
3677 // Load TOC of the callee into r2. We are using a target-specific load
3678 // with r2 hard coded, because the result of a target-independent load
3679 // would never go directly into r2, since r2 is a reserved register (which
3680 // prevents the register allocator from allocating it), resulting in an
3681 // additional register being allocated and an unnecessary move instruction
3682 // being generated.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003683 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Ulrich Weigandad0cb912014-06-18 17:52:49 +00003684 SDValue TOCOff = DAG.getIntPtrConstant(8);
3685 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, TOCOff);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003686 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
Ulrich Weigandad0cb912014-06-18 17:52:49 +00003687 AddTOC, InFlag);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003688 Chain = LoadTOCPtr.getValue(0);
3689 InFlag = LoadTOCPtr.getValue(1);
3690
3691 MTCTROps[0] = Chain;
3692 MTCTROps[1] = LoadFuncPtr;
3693 MTCTROps[2] = InFlag;
3694 }
3695
Craig Topper48d114b2014-04-26 18:35:24 +00003696 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00003697 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003698 InFlag = Chain.getValue(1);
3699
3700 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00003701 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003702 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003703 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003704 CallOpc = PPCISD::BCTRL;
Craig Topper062a2ba2014-04-25 05:30:21 +00003705 Callee.setNode(nullptr);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003706 // Add use of X11 (holding environment pointer)
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003707 if (isSVR4ABI && isPPC64 && !isELFv2ABI)
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003708 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003709 // Add CTR register as callee so a bctr can be emitted later.
3710 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00003711 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003712 }
3713
3714 // If this is a direct call, pass the chain and the callee.
3715 if (Callee.getNode()) {
3716 Ops.push_back(Chain);
3717 Ops.push_back(Callee);
Bill Schmidt3d9674c2014-11-11 20:44:09 +00003718
3719 // If this is a call to __tls_get_addr, find the symbol whose address
3720 // is to be taken and add it to the list. This will be used to
3721 // generate __tls_get_addr(<sym>@tlsgd) or __tls_get_addr(<sym>@tlsld).
3722 // We find the symbol by walking the chain to the CopyFromReg, walking
3723 // back from the CopyFromReg to the ADDI_TLSGD_L or ADDI_TLSLD_L, and
3724 // pulling the symbol from that node.
3725 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
3726 if (!strcmp(S->getSymbol(), "__tls_get_addr")) {
3727 assert(!needIndirectCall && "Indirect call to __tls_get_addr???");
3728 SDNode *AddI = Chain.getNode()->getOperand(2).getNode();
3729 SDValue TGTAddr = AddI->getOperand(1);
3730 assert(TGTAddr.getNode()->getOpcode() == ISD::TargetGlobalTLSAddress &&
3731 "Didn't find target global TLS address where we expected one");
3732 Ops.push_back(TGTAddr);
3733 CallOpc = PPCISD::CALL_TLS;
3734 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003735 }
3736 // If this is a tail call add stack pointer delta.
3737 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00003738 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003739
3740 // Add argument registers to the end of the list so that they are known live
3741 // into the call.
3742 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3743 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3744 RegsToPass[i].second.getValueType()));
3745
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003746 // Direct calls in the ELFv2 ABI need the TOC register live into the call.
3747 if (Callee.getNode() && isELFv2ABI)
3748 Ops.push_back(DAG.getRegister(PPC::X2, PtrVT));
3749
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003750 return CallOpc;
3751}
3752
Roman Divacky76293062012-09-18 16:47:58 +00003753static
3754bool isLocalCall(const SDValue &Callee)
3755{
3756 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00003757 return !G->getGlobal()->isDeclaration() &&
3758 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00003759 return false;
3760}
3761
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003762SDValue
3763PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003764 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003765 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003766 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003767 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003768
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003769 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003770 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
3771 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003772 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003773
3774 // Copy all of the result registers out of their specified physreg.
3775 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3776 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003777 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00003778
3779 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3780 VA.getLocReg(), VA.getLocVT(), InFlag);
3781 Chain = Val.getValue(1);
3782 InFlag = Val.getValue(2);
3783
3784 switch (VA.getLocInfo()) {
3785 default: llvm_unreachable("Unknown loc info!");
3786 case CCValAssign::Full: break;
3787 case CCValAssign::AExt:
3788 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3789 break;
3790 case CCValAssign::ZExt:
3791 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3792 DAG.getValueType(VA.getValVT()));
3793 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3794 break;
3795 case CCValAssign::SExt:
3796 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3797 DAG.getValueType(VA.getValVT()));
3798 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3799 break;
3800 }
3801
3802 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003803 }
3804
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003805 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003806}
3807
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003808SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00003809PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003810 bool isTailCall, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003811 SelectionDAG &DAG,
3812 SmallVector<std::pair<unsigned, SDValue>, 8>
3813 &RegsToPass,
3814 SDValue InFlag, SDValue Chain,
3815 SDValue &Callee,
3816 int SPDiff, unsigned NumBytes,
3817 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003818 SmallVectorImpl<SDValue> &InVals) const {
Ulrich Weigand8658f172014-07-20 23:43:15 +00003819
3820 bool isELFv2ABI = Subtarget.isELFv2ABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003821 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003822 SmallVector<SDValue, 8> Ops;
3823 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3824 isTailCall, RegsToPass, Ops, NodeTys,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003825 Subtarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003826
Hal Finkel5ab37802012-08-28 02:10:27 +00003827 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003828 if (isVarArg && Subtarget.isSVR4ABI() && !Subtarget.isPPC64())
Hal Finkel5ab37802012-08-28 02:10:27 +00003829 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3830
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003831 // When performing tail call optimization the callee pops its arguments off
3832 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00003833 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003834 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003835 (CallConv == CallingConv::Fast &&
3836 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003837
Roman Divackyef21be22012-03-06 16:41:49 +00003838 // Add a register mask operand representing the call-preserved registers.
Eric Christopherd9134482014-08-04 21:25:23 +00003839 const TargetRegisterInfo *TRI =
3840 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Roman Divackyef21be22012-03-06 16:41:49 +00003841 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3842 assert(Mask && "Missing call preserved mask for calling convention");
3843 Ops.push_back(DAG.getRegisterMask(Mask));
3844
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003845 if (InFlag.getNode())
3846 Ops.push_back(InFlag);
3847
3848 // Emit tail call.
3849 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003850 assert(((Callee.getOpcode() == ISD::Register &&
3851 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3852 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3853 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3854 isa<ConstantSDNode>(Callee)) &&
3855 "Expecting an global address, external symbol, absolute value or register");
3856
Craig Topper48d114b2014-04-26 18:35:24 +00003857 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003858 }
3859
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003860 // Add a NOP immediately after the branch instruction when using the 64-bit
3861 // SVR4 ABI. At link time, if caller and callee are in a different module and
3862 // thus have a different TOC, the call will be replaced with a call to a stub
3863 // function which saves the current TOC, loads the TOC of the callee and
3864 // branches to the callee. The NOP will be replaced with a load instruction
3865 // which restores the TOC of the caller from the TOC save slot of the current
3866 // stack frame. If caller and callee belong to the same module (and have the
3867 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00003868
3869 bool needsTOCRestore = false;
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003870 if (!isTailCall && Subtarget.isSVR4ABI()&& Subtarget.isPPC64()) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003871 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003872 // This is a call through a function pointer.
3873 // Restore the caller TOC from the save area into R2.
3874 // See PrepareCall() for more information about calls through function
3875 // pointers in the 64-bit SVR4 ABI.
3876 // We are using a target-specific load with r2 hard coded, because the
3877 // result of a target-independent load would never go directly into r2,
3878 // since r2 is a reserved register (which prevents the register allocator
3879 // from allocating it), resulting in an additional register being
3880 // allocated and an unnecessary move instruction being generated.
Hal Finkel51861b42012-03-31 14:45:15 +00003881 needsTOCRestore = true;
Bill Schmidtcea15962013-09-26 17:09:28 +00003882 } else if ((CallOpc == PPCISD::CALL) &&
3883 (!isLocalCall(Callee) ||
3884 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Roman Divacky76293062012-09-18 16:47:58 +00003885 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003886 CallOpc = PPCISD::CALL_NOP;
Bill Schmidt3d9674c2014-11-11 20:44:09 +00003887 } else if (CallOpc == PPCISD::CALL_TLS)
3888 // For 64-bit SVR4, TLS calls are always non-local.
3889 CallOpc = PPCISD::CALL_NOP_TLS;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003890 }
3891
Craig Topper48d114b2014-04-26 18:35:24 +00003892 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Hal Finkel51861b42012-03-31 14:45:15 +00003893 InFlag = Chain.getValue(1);
3894
3895 if (needsTOCRestore) {
3896 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Ulrich Weigandad0cb912014-06-18 17:52:49 +00003897 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3898 SDValue StackPtr = DAG.getRegister(PPC::X1, PtrVT);
Ulrich Weigand8658f172014-07-20 23:43:15 +00003899 unsigned TOCSaveOffset = PPCFrameLowering::getTOCSaveOffset(isELFv2ABI);
Ulrich Weigandad0cb912014-06-18 17:52:49 +00003900 SDValue TOCOff = DAG.getIntPtrConstant(TOCSaveOffset);
3901 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, StackPtr, TOCOff);
3902 Chain = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain, AddTOC, InFlag);
Hal Finkel51861b42012-03-31 14:45:15 +00003903 InFlag = Chain.getValue(1);
3904 }
3905
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003906 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3907 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003908 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003909 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003910 InFlag = Chain.getValue(1);
3911
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003912 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3913 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003914}
3915
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003916SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00003917PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003918 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00003919 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00003920 SDLoc &dl = CLI.DL;
3921 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3922 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3923 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00003924 SDValue Chain = CLI.Chain;
3925 SDValue Callee = CLI.Callee;
3926 bool &isTailCall = CLI.IsTailCall;
3927 CallingConv::ID CallConv = CLI.CallConv;
3928 bool isVarArg = CLI.IsVarArg;
3929
Evan Cheng67a69dd2010-01-27 00:07:07 +00003930 if (isTailCall)
3931 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3932 Ins, DAG);
3933
Reid Kleckner5772b772014-04-24 20:14:34 +00003934 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
3935 report_fatal_error("failed to perform tail call elimination on a call "
3936 "site marked musttail");
3937
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003938 if (Subtarget.isSVR4ABI()) {
3939 if (Subtarget.isPPC64())
Bill Schmidt57d6de52012-10-23 15:51:16 +00003940 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3941 isTailCall, Outs, OutVals, Ins,
3942 dl, DAG, InVals);
3943 else
3944 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3945 isTailCall, Outs, OutVals, Ins,
3946 dl, DAG, InVals);
3947 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003948
Bill Schmidt57d6de52012-10-23 15:51:16 +00003949 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3950 isTailCall, Outs, OutVals, Ins,
3951 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003952}
3953
3954SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003955PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3956 CallingConv::ID CallConv, bool isVarArg,
3957 bool isTailCall,
3958 const SmallVectorImpl<ISD::OutputArg> &Outs,
3959 const SmallVectorImpl<SDValue> &OutVals,
3960 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003961 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003962 SmallVectorImpl<SDValue> &InVals) const {
3963 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003964 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003965
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003966 assert((CallConv == CallingConv::C ||
3967 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003968
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003969 unsigned PtrByteSize = 4;
3970
3971 MachineFunction &MF = DAG.getMachineFunction();
3972
3973 // Mark this function as potentially containing a function that contains a
3974 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3975 // and restoring the callers stack pointer in this functions epilog. This is
3976 // done because by tail calling the called function might overwrite the value
3977 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003978 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3979 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003980 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00003981
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003982 // Count how many bytes are to be pushed on the stack, including the linkage
3983 // area, parameter list area and the part of the local variable space which
3984 // contains copies of aggregates which are passed by value.
3985
3986 // Assign locations to all of the outgoing arguments.
3987 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003988 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3989 *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003990
3991 // Reserve space for the linkage area on the stack.
Ulrich Weigand8658f172014-07-20 23:43:15 +00003992 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false, false),
3993 PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003994
3995 if (isVarArg) {
3996 // Handle fixed and variable vector arguments differently.
3997 // Fixed vector arguments go into registers as long as registers are
3998 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003999 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00004000
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004001 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00004002 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004003 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004004 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00004005
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004006 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00004007 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
4008 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004009 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00004010 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
4011 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004012 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004013
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004014 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00004015#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00004016 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00004017 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00004018#endif
Craig Toppere73658d2014-04-28 04:05:08 +00004019 llvm_unreachable(nullptr);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004020 }
4021 }
4022 } else {
4023 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00004024 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004025 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004026
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004027 // Assign locations to all of the outgoing aggregate by value arguments.
4028 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004029 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Eric Christopherb5217502014-08-06 18:45:26 +00004030 ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004031
4032 // Reserve stack space for the allocations in CCInfo.
4033 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
4034
Bill Schmidtef17c142013-02-06 17:33:58 +00004035 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004036
4037 // Size of the linkage area, parameter list area and the part of the local
4038 // space variable where copies of aggregates which are passed by value are
4039 // stored.
4040 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004041
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004042 // Calculate by how many bytes the stack has to be adjusted in case of tail
4043 // call optimization.
4044 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4045
4046 // Adjust the stack pointer for the new arguments...
4047 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004048 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4049 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004050 SDValue CallSeqStart = Chain;
4051
4052 // Load the return address and frame pointer so it can be moved somewhere else
4053 // later.
4054 SDValue LROp, FPOp;
4055 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
4056 dl);
4057
4058 // Set up a copy of the stack pointer for use loading and storing any
4059 // arguments that may not fit in the registers available for argument
4060 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00004061 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00004062
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004063 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4064 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4065 SmallVector<SDValue, 8> MemOpChains;
4066
Roman Divacky71038e72011-08-30 17:04:16 +00004067 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004068 // Walk the register/memloc assignments, inserting copies/loads.
4069 for (unsigned i = 0, j = 0, e = ArgLocs.size();
4070 i != e;
4071 ++i) {
4072 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004073 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004074 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00004075
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004076 if (Flags.isByVal()) {
4077 // Argument is an aggregate which is passed by value, thus we need to
4078 // create a copy of it in the local variable space of the current stack
4079 // frame (which is the stack frame of the caller) and pass the address of
4080 // this copy to the callee.
4081 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
4082 CCValAssign &ByValVA = ByValArgLocs[j++];
4083 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00004084
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004085 // Memory reserved in the local variable space of the callers stack frame.
4086 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004087
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004088 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
4089 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00004090
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004091 // Create a copy of the argument in the local area of the current
4092 // stack frame.
4093 SDValue MemcpyCall =
4094 CreateCopyOfByValArgument(Arg, PtrOff,
4095 CallSeqStart.getNode()->getOperand(0),
4096 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00004097
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004098 // This must go outside the CALLSEQ_START..END.
4099 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004100 CallSeqStart.getNode()->getOperand(1),
4101 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004102 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4103 NewCallSeqStart.getNode());
4104 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00004105
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004106 // Pass the address of the aggregate copy on the stack either in a
4107 // physical register or in the parameter list area of the current stack
4108 // frame to the callee.
4109 Arg = PtrOff;
4110 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004111
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004112 if (VA.isRegLoc()) {
Hal Finkel2a9d3182014-03-06 00:23:33 +00004113 if (Arg.getValueType() == MVT::i1)
4114 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg);
4115
Roman Divacky71038e72011-08-30 17:04:16 +00004116 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004117 // Put argument in a physical register.
4118 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
4119 } else {
4120 // Put argument in the parameter list area of the current stack frame.
4121 assert(VA.isMemLoc());
4122 unsigned LocMemOffset = VA.getLocMemOffset();
4123
4124 if (!isTailCall) {
4125 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
4126 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
4127
4128 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00004129 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004130 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004131 } else {
4132 // Calculate and remember argument location.
4133 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
4134 TailCallArguments);
4135 }
4136 }
4137 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004138
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004139 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004140 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Wesley Peck527da1b2010-11-23 03:31:01 +00004141
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004142 // Build a sequence of copy-to-reg nodes chained together with token chain
4143 // and flag operands which copy the outgoing args into the appropriate regs.
4144 SDValue InFlag;
4145 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4146 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4147 RegsToPass[i].second, InFlag);
4148 InFlag = Chain.getValue(1);
4149 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004150
Hal Finkel5ab37802012-08-28 02:10:27 +00004151 // Set CR bit 6 to true if this is a vararg call with floating args passed in
4152 // registers.
4153 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004154 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
4155 SDValue Ops[] = { Chain, InFlag };
4156
Hal Finkel5ab37802012-08-28 02:10:27 +00004157 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00004158 dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1));
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004159
Hal Finkel5ab37802012-08-28 02:10:27 +00004160 InFlag = Chain.getValue(1);
4161 }
4162
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004163 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004164 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
4165 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004166
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004167 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4168 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4169 Ins, InVals);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004170}
4171
Bill Schmidt57d6de52012-10-23 15:51:16 +00004172// Copy an argument into memory, being careful to do this outside the
4173// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004174SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00004175PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
4176 SDValue CallSeqStart,
4177 ISD::ArgFlagsTy Flags,
4178 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004179 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004180 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
4181 CallSeqStart.getNode()->getOperand(0),
4182 Flags, DAG, dl);
4183 // The MEMCPY must go outside the CALLSEQ_START..END.
4184 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004185 CallSeqStart.getNode()->getOperand(1),
4186 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004187 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4188 NewCallSeqStart.getNode());
4189 return NewCallSeqStart;
4190}
4191
4192SDValue
4193PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004194 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004195 bool isTailCall,
4196 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004197 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004198 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004199 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004200 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004201
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004202 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004203 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004204 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004205
Bill Schmidt57d6de52012-10-23 15:51:16 +00004206 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
4207 unsigned PtrByteSize = 8;
4208
4209 MachineFunction &MF = DAG.getMachineFunction();
4210
4211 // Mark this function as potentially containing a function that contains a
4212 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4213 // and restoring the callers stack pointer in this functions epilog. This is
4214 // done because by tail calling the called function might overwrite the value
4215 // in this function's (MF) stack pointer stack slot 0(SP).
4216 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4217 CallConv == CallingConv::Fast)
4218 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4219
Bill Schmidt57d6de52012-10-23 15:51:16 +00004220 // Count how many bytes are to be pushed on the stack, including the linkage
Ulrich Weigand8658f172014-07-20 23:43:15 +00004221 // area, and parameter passing area. On ELFv1, the linkage area is 48 bytes
4222 // reserved space for [SP][CR][LR][2 x unused][TOC]; on ELFv2, the linkage
4223 // area is 32 bytes reserved space for [SP][CR][LR][TOC].
4224 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(true, false,
4225 isELFv2ABI);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004226 unsigned NumBytes = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004227
4228 // Add up all the space actually used.
4229 for (unsigned i = 0; i != NumOps; ++i) {
4230 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4231 EVT ArgVT = Outs[i].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004232 EVT OrigVT = Outs[i].ArgVT;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004233
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004234 /* Respect alignment of argument on the stack. */
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004235 unsigned Align =
4236 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004237 NumBytes = ((NumBytes + Align - 1) / Align) * Align;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004238
4239 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004240 if (Flags.isInConsecutiveRegsLast())
4241 NumBytes = ((NumBytes + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004242 }
4243
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004244 unsigned NumBytesActuallyUsed = NumBytes;
4245
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004246 // The prolog code of the callee may store up to 8 GPR argument registers to
4247 // the stack, allowing va_start to index over them in memory if its varargs.
4248 // Because we cannot tell if this is needed on the caller side, we have to
4249 // conservatively assume that it is needed. As such, make sure we have at
4250 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00004251 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004252 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004253
4254 // Tail call needs the stack to be aligned.
4255 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4256 CallConv == CallingConv::Fast)
4257 NumBytes = EnsureStackAlignment(MF.getTarget(), NumBytes);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004258
4259 // Calculate by how many bytes the stack has to be adjusted in case of tail
4260 // call optimization.
4261 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4262
4263 // To protect arguments on the stack from being clobbered in a tail call,
4264 // force all the loads to happen before doing any other lowering.
4265 if (isTailCall)
4266 Chain = DAG.getStackArgumentTokenFactor(Chain);
4267
4268 // Adjust the stack pointer for the new arguments...
4269 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004270 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4271 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004272 SDValue CallSeqStart = Chain;
4273
4274 // Load the return address and frame pointer so it can be move somewhere else
4275 // later.
4276 SDValue LROp, FPOp;
4277 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4278 dl);
4279
4280 // Set up a copy of the stack pointer for use loading and storing any
4281 // arguments that may not fit in the registers available for argument
4282 // passing.
4283 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
4284
4285 // Figure out which arguments are going to go in registers, and which in
4286 // memory. Also, if this is a vararg function, floating point operations
4287 // must be stored to our stack, and loaded into integer regs as well, if
4288 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004289 unsigned ArgOffset = LinkageSize;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004290 unsigned GPR_idx, FPR_idx = 0, VR_idx = 0;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004291
Craig Topper840beec2014-04-04 05:16:06 +00004292 static const MCPhysReg GPR[] = {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004293 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4294 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4295 };
Craig Topper840beec2014-04-04 05:16:06 +00004296 static const MCPhysReg *FPR = GetFPR();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004297
Craig Topper840beec2014-04-04 05:16:06 +00004298 static const MCPhysReg VR[] = {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004299 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4300 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4301 };
Craig Topper840beec2014-04-04 05:16:06 +00004302 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00004303 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
4304 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
4305 };
4306
Bill Schmidt57d6de52012-10-23 15:51:16 +00004307 const unsigned NumGPRs = array_lengthof(GPR);
4308 const unsigned NumFPRs = 13;
4309 const unsigned NumVRs = array_lengthof(VR);
4310
4311 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4312 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4313
4314 SmallVector<SDValue, 8> MemOpChains;
4315 for (unsigned i = 0; i != NumOps; ++i) {
4316 SDValue Arg = OutVals[i];
4317 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004318 EVT ArgVT = Outs[i].VT;
4319 EVT OrigVT = Outs[i].ArgVT;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004320
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004321 /* Respect alignment of argument on the stack. */
4322 unsigned Align =
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004323 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004324 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
4325
4326 /* Compute GPR index associated with argument offset. */
4327 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
4328 GPR_idx = std::min(GPR_idx, NumGPRs);
4329
Bill Schmidt57d6de52012-10-23 15:51:16 +00004330 // PtrOff will be used to store the current argument to the stack if a
4331 // register cannot be found for it.
4332 SDValue PtrOff;
4333
4334 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
4335
4336 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4337
4338 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00004339 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004340 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4341 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4342 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
4343 }
4344
4345 // FIXME memcpy is used way more than necessary. Correctness first.
4346 // Note: "by value" is code for passing a structure by value, not
4347 // basic types.
4348 if (Flags.isByVal()) {
4349 // Note: Size includes alignment padding, so
4350 // struct x { short a; char b; }
4351 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
4352 // These are the proper values we need for right-justifying the
4353 // aggregate in a parameter register.
4354 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00004355
4356 // An empty aggregate parameter takes up no storage and no
4357 // registers.
4358 if (Size == 0)
4359 continue;
4360
Bill Schmidt57d6de52012-10-23 15:51:16 +00004361 // All aggregates smaller than 8 bytes must be passed right-justified.
4362 if (Size==1 || Size==2 || Size==4) {
4363 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
4364 if (GPR_idx != NumGPRs) {
4365 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
4366 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004367 false, false, false, 0);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004368 MemOpChains.push_back(Load.getValue(1));
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004369 RegsToPass.push_back(std::make_pair(GPR[GPR_idx], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004370
4371 ArgOffset += PtrByteSize;
4372 continue;
4373 }
4374 }
4375
4376 if (GPR_idx == NumGPRs && Size < 8) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004377 SDValue AddPtr = PtrOff;
4378 if (!isLittleEndian) {
4379 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4380 PtrOff.getValueType());
4381 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4382 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004383 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4384 CallSeqStart,
4385 Flags, DAG, dl);
4386 ArgOffset += PtrByteSize;
4387 continue;
4388 }
4389 // Copy entire object into memory. There are cases where gcc-generated
4390 // code assumes it is there, even if it could be put entirely into
4391 // registers. (This is not what the doc says.)
4392
4393 // FIXME: The above statement is likely due to a misunderstanding of the
4394 // documents. All arguments must be copied into the parameter area BY
4395 // THE CALLEE in the event that the callee takes the address of any
4396 // formal argument. That has not yet been implemented. However, it is
4397 // reasonable to use the stack area as a staging area for the register
4398 // load.
4399
4400 // Skip this for small aggregates, as we will use the same slot for a
4401 // right-justified copy, below.
4402 if (Size >= 8)
4403 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4404 CallSeqStart,
4405 Flags, DAG, dl);
4406
4407 // When a register is available, pass a small aggregate right-justified.
4408 if (Size < 8 && GPR_idx != NumGPRs) {
4409 // The easiest way to get this right-justified in a register
4410 // is to copy the structure into the rightmost portion of a
4411 // local variable slot, then load the whole slot into the
4412 // register.
4413 // FIXME: The memcpy seems to produce pretty awful code for
4414 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00004415 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00004416 // parameter save area instead of a new local variable.
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004417 SDValue AddPtr = PtrOff;
4418 if (!isLittleEndian) {
4419 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
4420 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4421 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004422 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4423 CallSeqStart,
4424 Flags, DAG, dl);
4425
4426 // Load the slot into the register.
4427 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
4428 MachinePointerInfo(),
4429 false, false, false, 0);
4430 MemOpChains.push_back(Load.getValue(1));
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004431 RegsToPass.push_back(std::make_pair(GPR[GPR_idx], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004432
4433 // Done with this argument.
4434 ArgOffset += PtrByteSize;
4435 continue;
4436 }
4437
4438 // For aggregates larger than PtrByteSize, copy the pieces of the
4439 // object that fit into registers from the parameter save area.
4440 for (unsigned j=0; j<Size; j+=PtrByteSize) {
4441 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
4442 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
4443 if (GPR_idx != NumGPRs) {
4444 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4445 MachinePointerInfo(),
4446 false, false, false, 0);
4447 MemOpChains.push_back(Load.getValue(1));
4448 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4449 ArgOffset += PtrByteSize;
4450 } else {
4451 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4452 break;
4453 }
4454 }
4455 continue;
4456 }
4457
Craig Topper56710102013-08-15 02:33:50 +00004458 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004459 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00004460 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004461 case MVT::i32:
4462 case MVT::i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004463 // These can be scalar arguments or elements of an integer array type
4464 // passed directly. Clang may use those instead of "byval" aggregate
4465 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidt57d6de52012-10-23 15:51:16 +00004466 if (GPR_idx != NumGPRs) {
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004467 RegsToPass.push_back(std::make_pair(GPR[GPR_idx], Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004468 } else {
4469 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4470 true, isTailCall, false, MemOpChains,
4471 TailCallArguments, dl);
4472 }
4473 ArgOffset += PtrByteSize;
4474 break;
4475 case MVT::f32:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004476 case MVT::f64: {
4477 // These can be scalar arguments or elements of a float array type
4478 // passed directly. The latter are used to implement ELFv2 homogenous
4479 // float aggregates.
4480
4481 // Named arguments go into FPRs first, and once they overflow, the
4482 // remaining arguments go into GPRs and then the parameter save area.
4483 // Unnamed arguments for vararg functions always go to GPRs and
4484 // then the parameter save area. For now, put all arguments to vararg
4485 // routines always in both locations (FPR *and* GPR or stack slot).
4486 bool NeedGPROrStack = isVarArg || FPR_idx == NumFPRs;
4487
4488 // First load the argument into the next available FPR.
4489 if (FPR_idx != NumFPRs)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004490 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4491
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004492 // Next, load the argument into GPR or stack slot if needed.
4493 if (!NeedGPROrStack)
4494 ;
4495 else if (GPR_idx != NumGPRs) {
4496 // In the non-vararg case, this can only ever happen in the
4497 // presence of f32 array types, since otherwise we never run
4498 // out of FPRs before running out of GPRs.
4499 SDValue ArgVal;
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004500
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004501 // Double values are always passed in a single GPR.
4502 if (Arg.getValueType() != MVT::f32) {
4503 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004504
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004505 // Non-array float values are extended and passed in a GPR.
4506 } else if (!Flags.isInConsecutiveRegs()) {
4507 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4508 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
4509
4510 // If we have an array of floats, we collect every odd element
4511 // together with its predecessor into one GPR.
4512 } else if (ArgOffset % PtrByteSize != 0) {
4513 SDValue Lo, Hi;
4514 Lo = DAG.getNode(ISD::BITCAST, dl, MVT::i32, OutVals[i - 1]);
4515 Hi = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4516 if (!isLittleEndian)
4517 std::swap(Lo, Hi);
4518 ArgVal = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
4519
4520 // The final element, if even, goes into the first half of a GPR.
4521 } else if (Flags.isInConsecutiveRegsLast()) {
4522 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4523 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
4524 if (!isLittleEndian)
4525 ArgVal = DAG.getNode(ISD::SHL, dl, MVT::i64, ArgVal,
4526 DAG.getConstant(32, MVT::i32));
4527
4528 // Non-final even elements are skipped; they will be handled
4529 // together the with subsequent argument on the next go-around.
4530 } else
4531 ArgVal = SDValue();
4532
4533 if (ArgVal.getNode())
4534 RegsToPass.push_back(std::make_pair(GPR[GPR_idx], ArgVal));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004535 } else {
4536 // Single-precision floating-point values are mapped to the
4537 // second (rightmost) word of the stack doubleword.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004538 if (Arg.getValueType() == MVT::f32 &&
4539 !isLittleEndian && !Flags.isInConsecutiveRegs()) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004540 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4541 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4542 }
4543
4544 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4545 true, isTailCall, false, MemOpChains,
4546 TailCallArguments, dl);
4547 }
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004548 // When passing an array of floats, the array occupies consecutive
4549 // space in the argument area; only round up to the next doubleword
4550 // at the end of the array. Otherwise, each float takes 8 bytes.
4551 ArgOffset += (Arg.getValueType() == MVT::f32 &&
4552 Flags.isInConsecutiveRegs()) ? 4 : 8;
4553 if (Flags.isInConsecutiveRegsLast())
4554 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004555 break;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004556 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004557 case MVT::v4f32:
4558 case MVT::v4i32:
4559 case MVT::v8i16:
4560 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00004561 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00004562 case MVT::v2i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004563 // These can be scalar arguments or elements of a vector array type
4564 // passed directly. The latter are used to implement ELFv2 homogenous
4565 // vector aggregates.
4566
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00004567 // For a varargs call, named arguments go into VRs or on the stack as
4568 // usual; unnamed arguments always go to the stack or the corresponding
4569 // GPRs when within range. For now, we always put the value in both
4570 // locations (or even all three).
Bill Schmidt57d6de52012-10-23 15:51:16 +00004571 if (isVarArg) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004572 // We could elide this store in the case where the object fits
4573 // entirely in R registers. Maybe later.
Bill Schmidt57d6de52012-10-23 15:51:16 +00004574 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4575 MachinePointerInfo(), false, false, 0);
4576 MemOpChains.push_back(Store);
4577 if (VR_idx != NumVRs) {
4578 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4579 MachinePointerInfo(),
4580 false, false, false, 0);
4581 MemOpChains.push_back(Load.getValue(1));
Hal Finkel7811c612014-03-28 19:58:11 +00004582
4583 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4584 Arg.getSimpleValueType() == MVT::v2i64) ?
4585 VSRH[VR_idx] : VR[VR_idx];
4586 ++VR_idx;
4587
4588 RegsToPass.push_back(std::make_pair(VReg, Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004589 }
4590 ArgOffset += 16;
4591 for (unsigned i=0; i<16; i+=PtrByteSize) {
4592 if (GPR_idx == NumGPRs)
4593 break;
4594 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4595 DAG.getConstant(i, PtrVT));
4596 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4597 false, false, false, 0);
4598 MemOpChains.push_back(Load.getValue(1));
4599 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4600 }
4601 break;
4602 }
4603
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00004604 // Non-varargs Altivec params go into VRs or on the stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00004605 if (VR_idx != NumVRs) {
Hal Finkel7811c612014-03-28 19:58:11 +00004606 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4607 Arg.getSimpleValueType() == MVT::v2i64) ?
4608 VSRH[VR_idx] : VR[VR_idx];
4609 ++VR_idx;
4610
4611 RegsToPass.push_back(std::make_pair(VReg, Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004612 } else {
4613 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4614 true, isTailCall, true, MemOpChains,
4615 TailCallArguments, dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004616 }
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00004617 ArgOffset += 16;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004618 break;
4619 }
4620 }
4621
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004622 assert(NumBytesActuallyUsed == ArgOffset);
Ulrich Weigandde8641b2014-07-07 19:39:44 +00004623 (void)NumBytesActuallyUsed;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004624
Bill Schmidt57d6de52012-10-23 15:51:16 +00004625 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004626 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004627
4628 // Check if this is an indirect call (MTCTR/BCTRL).
4629 // See PrepareCall() for more information about calls through function
4630 // pointers in the 64-bit SVR4 ABI.
4631 if (!isTailCall &&
4632 !dyn_cast<GlobalAddressSDNode>(Callee) &&
Ulrich Weigand9aa09ef2014-06-18 16:14:04 +00004633 !dyn_cast<ExternalSymbolSDNode>(Callee)) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004634 // Load r2 into a virtual register and store it to the TOC save area.
4635 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4636 // TOC save area offset.
Ulrich Weigand8658f172014-07-20 23:43:15 +00004637 unsigned TOCSaveOffset = PPCFrameLowering::getTOCSaveOffset(isELFv2ABI);
Ulrich Weigandad0cb912014-06-18 17:52:49 +00004638 SDValue PtrOff = DAG.getIntPtrConstant(TOCSaveOffset);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004639 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4640 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4641 false, false, 0);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004642 // In the ELFv2 ABI, R12 must contain the address of an indirect callee.
4643 // This does not mean the MTCTR instruction must use R12; it's easier
4644 // to model this as an extra parameter, so do that.
4645 if (isELFv2ABI)
4646 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004647 }
4648
4649 // Build a sequence of copy-to-reg nodes chained together with token chain
4650 // and flag operands which copy the outgoing args into the appropriate regs.
4651 SDValue InFlag;
4652 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4653 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4654 RegsToPass[i].second, InFlag);
4655 InFlag = Chain.getValue(1);
4656 }
4657
4658 if (isTailCall)
4659 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4660 FPOp, true, TailCallArguments);
4661
4662 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4663 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4664 Ins, InVals);
4665}
4666
4667SDValue
4668PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4669 CallingConv::ID CallConv, bool isVarArg,
4670 bool isTailCall,
4671 const SmallVectorImpl<ISD::OutputArg> &Outs,
4672 const SmallVectorImpl<SDValue> &OutVals,
4673 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004674 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004675 SmallVectorImpl<SDValue> &InVals) const {
4676
4677 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004678
Owen Anderson53aa7a92009-08-10 22:56:29 +00004679 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00004680 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004681 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004682
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004683 MachineFunction &MF = DAG.getMachineFunction();
4684
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004685 // Mark this function as potentially containing a function that contains a
4686 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4687 // and restoring the callers stack pointer in this functions epilog. This is
4688 // done because by tail calling the called function might overwrite the value
4689 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004690 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4691 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004692 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4693
Chris Lattneraa40ec12006-05-16 22:56:08 +00004694 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00004695 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00004696 // prereserved space for [SP][CR][LR][3 x unused].
Ulrich Weigand8658f172014-07-20 23:43:15 +00004697 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(isPPC64, true,
4698 false);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004699 unsigned NumBytes = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004700
4701 // Add up all the space actually used.
4702 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
4703 // they all go in registers, but we must reserve stack space for them for
4704 // possible use by the caller. In varargs or 64-bit calls, parameters are
4705 // assigned stack space in order, with padding so Altivec parameters are
4706 // 16-byte aligned.
4707 unsigned nAltivecParamsAtEnd = 0;
4708 for (unsigned i = 0; i != NumOps; ++i) {
4709 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4710 EVT ArgVT = Outs[i].VT;
4711 // Varargs Altivec parameters are padded to a 16 byte boundary.
4712 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
4713 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
4714 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64) {
4715 if (!isVarArg && !isPPC64) {
4716 // Non-varargs Altivec parameters go after all the non-Altivec
4717 // parameters; handle those later so we know how much padding we need.
4718 nAltivecParamsAtEnd++;
4719 continue;
4720 }
4721 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
4722 NumBytes = ((NumBytes+15)/16)*16;
4723 }
4724 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
4725 }
4726
4727 // Allow for Altivec parameters at the end, if needed.
4728 if (nAltivecParamsAtEnd) {
4729 NumBytes = ((NumBytes+15)/16)*16;
4730 NumBytes += 16*nAltivecParamsAtEnd;
4731 }
4732
4733 // The prolog code of the callee may store up to 8 GPR argument registers to
4734 // the stack, allowing va_start to index over them in memory if its varargs.
4735 // Because we cannot tell if this is needed on the caller side, we have to
4736 // conservatively assume that it is needed. As such, make sure we have at
4737 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004738 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004739
4740 // Tail call needs the stack to be aligned.
4741 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4742 CallConv == CallingConv::Fast)
4743 NumBytes = EnsureStackAlignment(MF.getTarget(), NumBytes);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004744
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004745 // Calculate by how many bytes the stack has to be adjusted in case of tail
4746 // call optimization.
4747 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004748
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004749 // To protect arguments on the stack from being clobbered in a tail call,
4750 // force all the loads to happen before doing any other lowering.
4751 if (isTailCall)
4752 Chain = DAG.getStackArgumentTokenFactor(Chain);
4753
Chris Lattnerb7552a82006-05-17 00:15:40 +00004754 // Adjust the stack pointer for the new arguments...
4755 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004756 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4757 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004758 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004759
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004760 // Load the return address and frame pointer so it can be move somewhere else
4761 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004762 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004763 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4764 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004765
Chris Lattnerb7552a82006-05-17 00:15:40 +00004766 // Set up a copy of the stack pointer for use loading and storing any
4767 // arguments that may not fit in the registers available for argument
4768 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004769 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004770 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004771 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004772 else
Owen Anderson9f944592009-08-11 20:47:22 +00004773 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004774
Chris Lattnerb7552a82006-05-17 00:15:40 +00004775 // Figure out which arguments are going to go in registers, and which in
4776 // memory. Also, if this is a vararg function, floating point operations
4777 // must be stored to our stack, and loaded into integer regs as well, if
4778 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004779 unsigned ArgOffset = LinkageSize;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004780 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004781
Craig Topper840beec2014-04-04 05:16:06 +00004782 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004783 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4784 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4785 };
Craig Topper840beec2014-04-04 05:16:06 +00004786 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00004787 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4788 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4789 };
Craig Topper840beec2014-04-04 05:16:06 +00004790 static const MCPhysReg *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004791
Craig Topper840beec2014-04-04 05:16:06 +00004792 static const MCPhysReg VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004793 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4794 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4795 };
Owen Andersone2f23a32007-09-07 04:06:50 +00004796 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004797 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00004798 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004799
Craig Topper840beec2014-04-04 05:16:06 +00004800 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004801
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004802 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004803 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4804
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004805 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00004806 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004807 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004808 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004809
Chris Lattnerb7552a82006-05-17 00:15:40 +00004810 // PtrOff will be used to store the current argument to the stack if a
4811 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004812 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004813
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004814 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004815
Dale Johannesen679073b2009-02-04 02:34:38 +00004816 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004817
4818 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00004819 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00004820 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4821 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00004822 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004823 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00004824
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004825 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004826 // Note: "by value" is code for passing a structure by value, not
4827 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00004828 if (Flags.isByVal()) {
4829 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004830 // Very small objects are passed right-justified. Everything else is
4831 // passed left-justified.
4832 if (Size==1 || Size==2) {
4833 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004834 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00004835 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00004836 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004837 false, false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004838 MemOpChains.push_back(Load.getValue(1));
4839 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004840
4841 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004842 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00004843 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4844 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004845 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004846 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4847 CallSeqStart,
4848 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004849 ArgOffset += PtrByteSize;
4850 }
4851 continue;
4852 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004853 // Copy entire object into memory. There are cases where gcc-generated
4854 // code assumes it is there, even if it could be put entirely into
4855 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004856 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4857 CallSeqStart,
4858 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004859
4860 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4861 // copy the pieces of the object that fit into registers from the
4862 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00004863 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004864 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004865 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00004866 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004867 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4868 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004869 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00004870 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00004871 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004872 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004873 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004874 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004875 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004876 }
4877 }
4878 continue;
4879 }
4880
Craig Topper56710102013-08-15 02:33:50 +00004881 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004882 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00004883 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00004884 case MVT::i32:
4885 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004886 if (GPR_idx != NumGPRs) {
Hal Finkel7f908e82014-03-06 00:45:19 +00004887 if (Arg.getValueType() == MVT::i1)
4888 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg);
4889
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004890 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004891 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004892 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4893 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004894 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00004895 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004896 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004897 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004898 case MVT::f32:
4899 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004900 if (FPR_idx != NumFPRs) {
4901 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4902
Chris Lattnerb7552a82006-05-17 00:15:40 +00004903 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00004904 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4905 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004906 MemOpChains.push_back(Store);
4907
Chris Lattnerb7552a82006-05-17 00:15:40 +00004908 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004909 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004910 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004911 MachinePointerInfo(), false, false,
4912 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004913 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004914 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004915 }
Owen Anderson9f944592009-08-11 20:47:22 +00004916 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004917 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004918 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00004919 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4920 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004921 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004922 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004923 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00004924 }
4925 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00004926 // If we have any FPRs remaining, we may also have GPRs remaining.
4927 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4928 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004929 if (GPR_idx != NumGPRs)
4930 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00004931 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004932 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4933 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004934 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004935 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004936 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4937 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004938 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004939 if (isPPC64)
4940 ArgOffset += 8;
4941 else
Owen Anderson9f944592009-08-11 20:47:22 +00004942 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004943 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004944 case MVT::v4f32:
4945 case MVT::v4i32:
4946 case MVT::v8i16:
4947 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00004948 if (isVarArg) {
4949 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00004950 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00004951 // V registers; in fact gcc does this only for arguments that are
4952 // prototyped, not for those that match the ... We do it for all
4953 // arguments, seems to work.
4954 while (ArgOffset % 16 !=0) {
4955 ArgOffset += PtrByteSize;
4956 if (GPR_idx != NumGPRs)
4957 GPR_idx++;
4958 }
4959 // We could elide this store in the case where the object fits
4960 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004961 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004962 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00004963 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4964 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004965 MemOpChains.push_back(Store);
4966 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004967 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00004968 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004969 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004970 MemOpChains.push_back(Load.getValue(1));
4971 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4972 }
4973 ArgOffset += 16;
4974 for (unsigned i=0; i<16; i+=PtrByteSize) {
4975 if (GPR_idx == NumGPRs)
4976 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00004977 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004978 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00004979 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004980 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004981 MemOpChains.push_back(Load.getValue(1));
4982 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4983 }
4984 break;
4985 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004986
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004987 // Non-varargs Altivec params generally go in registers, but have
4988 // stack space allocated at the end.
4989 if (VR_idx != NumVRs) {
4990 // Doesn't have GPR space allocated.
4991 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4992 } else if (nAltivecParamsAtEnd==0) {
4993 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004994 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4995 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004996 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004997 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00004998 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00004999 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00005000 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00005001 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005002 // If all Altivec parameters fit in registers, as they usually do,
5003 // they get stack space following the non-Altivec parameters. We
5004 // don't track this here because nobody below needs it.
5005 // If there are more Altivec parameters than fit in registers emit
5006 // the stores here.
5007 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
5008 unsigned j = 0;
5009 // Offset is aligned; skip 1st 12 params which go in V registers.
5010 ArgOffset = ((ArgOffset+15)/16)*16;
5011 ArgOffset += 12*16;
5012 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005013 SDValue Arg = OutVals[i];
5014 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00005015 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
5016 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005017 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005018 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005019 // We are emitting Altivec params in order.
5020 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
5021 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00005022 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00005023 ArgOffset += 16;
5024 }
5025 }
5026 }
5027 }
5028
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005029 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00005030 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005031
Dale Johannesen90eab672010-03-09 20:15:42 +00005032 // On Darwin, R12 must contain the address of an indirect callee. This does
5033 // not mean the MTCTR instruction must use R12; it's easier to model this as
5034 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00005035 if (!isTailCall &&
Dale Johannesen90eab672010-03-09 20:15:42 +00005036 !dyn_cast<GlobalAddressSDNode>(Callee) &&
5037 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
5038 !isBLACompatibleAddress(Callee, DAG))
5039 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
5040 PPC::R12), Callee));
5041
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005042 // Build a sequence of copy-to-reg nodes chained together with token chain
5043 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005044 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005045 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00005046 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00005047 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005048 InFlag = Chain.getValue(1);
5049 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005050
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00005051 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005052 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
5053 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005054
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005055 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
5056 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
5057 Ins, InVals);
Chris Lattneraa40ec12006-05-16 22:56:08 +00005058}
5059
Hal Finkel450128a2011-10-14 19:51:36 +00005060bool
5061PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
5062 MachineFunction &MF, bool isVarArg,
5063 const SmallVectorImpl<ISD::OutputArg> &Outs,
5064 LLVMContext &Context) const {
5065 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00005066 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Hal Finkel450128a2011-10-14 19:51:36 +00005067 return CCInfo.CheckReturn(Outs, RetCC_PPC);
5068}
5069
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005070SDValue
5071PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00005072 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005073 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005074 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005075 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005076
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005077 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00005078 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
5079 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005080 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005081
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005082 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005083 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005084
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005085 // Copy the result values into the output registers.
5086 for (unsigned i = 0; i != RVLocs.size(); ++i) {
5087 CCValAssign &VA = RVLocs[i];
5088 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00005089
5090 SDValue Arg = OutVals[i];
5091
5092 switch (VA.getLocInfo()) {
5093 default: llvm_unreachable("Unknown loc info!");
5094 case CCValAssign::Full: break;
5095 case CCValAssign::AExt:
5096 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
5097 break;
5098 case CCValAssign::ZExt:
5099 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
5100 break;
5101 case CCValAssign::SExt:
5102 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
5103 break;
5104 }
5105
5106 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005107 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005108 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005109 }
5110
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005111 RetOps[0] = Chain; // Update chain.
5112
5113 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005114 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005115 RetOps.push_back(Flag);
5116
Craig Topper48d114b2014-04-26 18:35:24 +00005117 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps);
Chris Lattner4211ca92006-04-14 06:01:58 +00005118}
5119
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005120SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005121 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00005122 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005123 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005124
Jim Laskeye4f4d042006-12-04 22:04:42 +00005125 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005126 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00005127
5128 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00005129 bool isPPC64 = Subtarget.isPPC64();
5130 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005131 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00005132
5133 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005134 SDValue Chain = Op.getOperand(0);
5135 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005136
Jim Laskeye4f4d042006-12-04 22:04:42 +00005137 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00005138 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
5139 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005140 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005141
Jim Laskeye4f4d042006-12-04 22:04:42 +00005142 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00005143 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005144
Jim Laskeye4f4d042006-12-04 22:04:42 +00005145 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00005146 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005147 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00005148}
5149
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005150
5151
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005152SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005153PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00005154 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005155 bool isPPC64 = Subtarget.isPPC64();
5156 bool isDarwinABI = Subtarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005157 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005158
5159 // Get current frame pointer save index. The users of this index will be
5160 // primarily DYNALLOC instructions.
5161 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
5162 int RASI = FI->getReturnAddrSaveIndex();
5163
5164 // If the frame pointer save index hasn't been defined yet.
5165 if (!RASI) {
5166 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00005167 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005168 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00005169 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005170 // Save the result.
5171 FI->setReturnAddrSaveIndex(RASI);
5172 }
5173 return DAG.getFrameIndex(RASI, PtrVT);
5174}
5175
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005176SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005177PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
5178 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005179 bool isPPC64 = Subtarget.isPPC64();
5180 bool isDarwinABI = Subtarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005181 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00005182
5183 // Get current frame pointer save index. The users of this index will be
5184 // primarily DYNALLOC instructions.
5185 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
5186 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005187
Jim Laskey48850c12006-11-16 22:43:37 +00005188 // If the frame pointer save index hasn't been defined yet.
5189 if (!FPSI) {
5190 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00005191 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005192 isDarwinABI);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005193
Jim Laskey48850c12006-11-16 22:43:37 +00005194 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00005195 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00005196 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005197 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00005198 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005199 return DAG.getFrameIndex(FPSI, PtrVT);
5200}
Jim Laskey48850c12006-11-16 22:43:37 +00005201
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005202SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005203 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005204 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00005205 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005206 SDValue Chain = Op.getOperand(0);
5207 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005208 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005209
Jim Laskey48850c12006-11-16 22:43:37 +00005210 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005211 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00005212 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005213 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00005214 DAG.getConstant(0, PtrVT), Size);
5215 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005216 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00005217 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005218 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00005219 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Craig Topper48d114b2014-04-26 18:35:24 +00005220 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops);
Jim Laskey48850c12006-11-16 22:43:37 +00005221}
5222
Hal Finkel756810f2013-03-21 21:37:52 +00005223SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
5224 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005225 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00005226 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
5227 DAG.getVTList(MVT::i32, MVT::Other),
5228 Op.getOperand(0), Op.getOperand(1));
5229}
5230
5231SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
5232 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005233 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00005234 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
5235 Op.getOperand(0), Op.getOperand(1));
5236}
5237
Hal Finkel940ab932014-02-28 00:27:01 +00005238SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
5239 assert(Op.getValueType() == MVT::i1 &&
5240 "Custom lowering only for i1 loads");
5241
5242 // First, load 8 bits into 32 bits, then truncate to 1 bit.
5243
5244 SDLoc dl(Op);
5245 LoadSDNode *LD = cast<LoadSDNode>(Op);
5246
5247 SDValue Chain = LD->getChain();
5248 SDValue BasePtr = LD->getBasePtr();
5249 MachineMemOperand *MMO = LD->getMemOperand();
5250
5251 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(), Chain,
5252 BasePtr, MVT::i8, MMO);
5253 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
5254
5255 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00005256 return DAG.getMergeValues(Ops, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00005257}
5258
5259SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
5260 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
5261 "Custom lowering only for i1 stores");
5262
5263 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
5264
5265 SDLoc dl(Op);
5266 StoreSDNode *ST = cast<StoreSDNode>(Op);
5267
5268 SDValue Chain = ST->getChain();
5269 SDValue BasePtr = ST->getBasePtr();
5270 SDValue Value = ST->getValue();
5271 MachineMemOperand *MMO = ST->getMemOperand();
5272
5273 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(), Value);
5274 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
5275}
5276
5277// FIXME: Remove this once the ANDI glue bug is fixed:
5278SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
5279 assert(Op.getValueType() == MVT::i1 &&
5280 "Custom lowering only for i1 results");
5281
5282 SDLoc DL(Op);
5283 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
5284 Op.getOperand(0));
5285}
5286
Chris Lattner4211ca92006-04-14 06:01:58 +00005287/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
5288/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005289SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00005290 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00005291 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
5292 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00005293 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005294
Hal Finkel81f87992013-04-07 22:11:09 +00005295 // We might be able to do better than this under some circumstances, but in
5296 // general, fsel-based lowering of select is a finite-math-only optimization.
5297 // For more information, see section F.3 of the 2.06 ISA specification.
5298 if (!DAG.getTarget().Options.NoInfsFPMath ||
5299 !DAG.getTarget().Options.NoNaNsFPMath)
5300 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005301
Hal Finkel81f87992013-04-07 22:11:09 +00005302 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005303
Owen Anderson53aa7a92009-08-10 22:56:29 +00005304 EVT ResVT = Op.getValueType();
5305 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005306 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
5307 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005308 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005309
Chris Lattner4211ca92006-04-14 06:01:58 +00005310 // If the RHS of the comparison is a 0.0, we don't need to do the
5311 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00005312 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00005313 if (isFloatingPointZero(RHS))
5314 switch (CC) {
5315 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00005316 case ISD::SETNE:
5317 std::swap(TV, FV);
5318 case ISD::SETEQ:
5319 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5320 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
5321 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
5322 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
5323 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
5324 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5325 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005326 case ISD::SETULT:
5327 case ISD::SETLT:
5328 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005329 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005330 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00005331 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5332 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005333 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005334 case ISD::SETUGT:
5335 case ISD::SETGT:
5336 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005337 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005338 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00005339 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5340 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005341 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005342 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005343 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005344
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005345 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00005346 switch (CC) {
5347 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00005348 case ISD::SETNE:
5349 std::swap(TV, FV);
5350 case ISD::SETEQ:
5351 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
5352 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5353 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
5354 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
5355 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
5356 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
5357 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5358 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005359 case ISD::SETULT:
5360 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005361 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
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, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005365 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005366 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005367 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005368 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5369 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005370 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005371 case ISD::SETUGT:
5372 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005373 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005374 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5375 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005376 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005377 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005378 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005379 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005380 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5381 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005382 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005383 }
Eli Friedman5806e182009-05-28 04:31:08 +00005384 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00005385}
5386
Chris Lattner57ee7c62007-11-28 18:44:47 +00005387// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005388SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005389 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00005390 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005391 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00005392 if (Src.getValueType() == MVT::f32)
5393 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00005394
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005395 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00005396 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005397 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00005398 case MVT::i32:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005399 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005400 (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ :
Hal Finkelf6d45f22013-04-01 17:52:07 +00005401 PPCISD::FCTIDZ),
Owen Anderson9f944592009-08-11 20:47:22 +00005402 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005403 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005404 case MVT::i64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005405 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) &&
Hal Finkel3f88d082013-04-01 18:42:58 +00005406 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00005407 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
5408 PPCISD::FCTIDUZ,
5409 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005410 break;
5411 }
Duncan Sands2a287912008-07-19 16:26:02 +00005412
Chris Lattner4211ca92006-04-14 06:01:58 +00005413 // Convert the FP value to an int value through memory.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005414 bool i32Stack = Op.getValueType() == MVT::i32 && Subtarget.hasSTFIWX() &&
5415 (Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT());
Hal Finkelf6d45f22013-04-01 17:52:07 +00005416 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
5417 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
5418 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00005419
Chris Lattner06a49542007-10-15 20:14:52 +00005420 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005421 SDValue Chain;
5422 if (i32Stack) {
5423 MachineFunction &MF = DAG.getMachineFunction();
5424 MachineMemOperand *MMO =
5425 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
5426 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
5427 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00005428 DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005429 } else
5430 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
5431 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00005432
5433 // Result is a load from the stack slot. If loading 4 bytes, make sure to
5434 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005435 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00005436 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00005437 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkelf6d45f22013-04-01 17:52:07 +00005438 MPI = MachinePointerInfo();
5439 }
5440
5441 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooper82cd9e82011-11-08 18:42:53 +00005442 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005443}
5444
Hal Finkelf6d45f22013-04-01 17:52:07 +00005445SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005446 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005447 SDLoc dl(Op);
Dan Gohmand6819da2008-03-11 01:59:03 +00005448 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00005449 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005450 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00005451
Hal Finkel6a56b212014-03-05 22:14:00 +00005452 if (Op.getOperand(0).getValueType() == MVT::i1)
5453 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0),
5454 DAG.getConstantFP(1.0, Op.getValueType()),
5455 DAG.getConstantFP(0.0, Op.getValueType()));
5456
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005457 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005458 "UINT_TO_FP is supported only with FPCVT");
5459
5460 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00005461 // Otherwise, convert to double-precision and then round.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005462 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
Hal Finkelf6d45f22013-04-01 17:52:07 +00005463 (Op.getOpcode() == ISD::UINT_TO_FP ?
5464 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
5465 (Op.getOpcode() == ISD::UINT_TO_FP ?
5466 PPCISD::FCFIDU : PPCISD::FCFID);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005467 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
Hal Finkelf6d45f22013-04-01 17:52:07 +00005468 MVT::f32 : MVT::f64;
5469
Owen Anderson9f944592009-08-11 20:47:22 +00005470 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005471 SDValue SINT = Op.getOperand(0);
5472 // When converting to single-precision, we actually need to convert
5473 // to double-precision first and then round to single-precision.
5474 // To avoid double-rounding effects during that operation, we have
5475 // to prepare the input operand. Bits that might be truncated when
5476 // converting to double-precision are replaced by a bit that won't
5477 // be lost at this stage, but is below the single-precision rounding
5478 // position.
5479 //
5480 // However, if -enable-unsafe-fp-math is in effect, accept double
5481 // rounding to avoid the extra overhead.
5482 if (Op.getValueType() == MVT::f32 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005483 !Subtarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005484 !DAG.getTarget().Options.UnsafeFPMath) {
5485
5486 // Twiddle input to make sure the low 11 bits are zero. (If this
5487 // is the case, we are guaranteed the value will fit into the 53 bit
5488 // mantissa of an IEEE double-precision value without rounding.)
5489 // If any of those low 11 bits were not zero originally, make sure
5490 // bit 12 (value 2048) is set instead, so that the final rounding
5491 // to single-precision gets the correct result.
5492 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5493 SINT, DAG.getConstant(2047, MVT::i64));
5494 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
5495 Round, DAG.getConstant(2047, MVT::i64));
5496 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
5497 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5498 Round, DAG.getConstant(-2048, MVT::i64));
5499
5500 // However, we cannot use that value unconditionally: if the magnitude
5501 // of the input value is small, the bit-twiddling we did above might
5502 // end up visibly changing the output. Fortunately, in that case, we
5503 // don't need to twiddle bits since the original input will convert
5504 // exactly to double-precision floating-point already. Therefore,
5505 // construct a conditional to use the original value if the top 11
5506 // bits are all sign-bit copies, and use the rounded value computed
5507 // above otherwise.
5508 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
5509 SINT, DAG.getConstant(53, MVT::i32));
5510 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
5511 Cond, DAG.getConstant(1, MVT::i64));
5512 Cond = DAG.getSetCC(dl, MVT::i32,
5513 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
5514
5515 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
5516 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00005517
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005518 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005519 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
5520
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005521 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00005522 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005523 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005524 return FP;
5525 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005526
Owen Anderson9f944592009-08-11 20:47:22 +00005527 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005528 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005529 // Since we only generate this in 64-bit mode, we can take advantage of
5530 // 64-bit registers. In particular, sign extend the input value into the
5531 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
5532 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00005533 MachineFunction &MF = DAG.getMachineFunction();
5534 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005535 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005536
Hal Finkelbeb296b2013-03-31 10:12:51 +00005537 SDValue Ld;
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005538 if (Subtarget.hasLFIWAX() || Subtarget.hasFPCVT()) {
Hal Finkelbeb296b2013-03-31 10:12:51 +00005539 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
5540 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005541
Hal Finkelbeb296b2013-03-31 10:12:51 +00005542 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
5543 MachinePointerInfo::getFixedStack(FrameIdx),
5544 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00005545
Hal Finkelbeb296b2013-03-31 10:12:51 +00005546 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
5547 "Expected an i32 store");
5548 MachineMemOperand *MMO =
5549 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
5550 MachineMemOperand::MOLoad, 4, 4);
5551 SDValue Ops[] = { Store, FIdx };
Hal Finkelf6d45f22013-04-01 17:52:07 +00005552 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
5553 PPCISD::LFIWZX : PPCISD::LFIWAX,
5554 dl, DAG.getVTList(MVT::f64, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00005555 Ops, MVT::i32, MMO);
Hal Finkelbeb296b2013-03-31 10:12:51 +00005556 } else {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005557 assert(Subtarget.isPPC64() &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005558 "i32->FP without LFIWAX supported only on PPC64");
5559
Hal Finkelbeb296b2013-03-31 10:12:51 +00005560 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
5561 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
5562
5563 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
5564 Op.getOperand(0));
5565
5566 // STD the extended value into the stack slot.
5567 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
5568 MachinePointerInfo::getFixedStack(FrameIdx),
5569 false, false, 0);
5570
5571 // Load the value as a double.
5572 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
5573 MachinePointerInfo::getFixedStack(FrameIdx),
5574 false, false, false, 0);
5575 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005576
Chris Lattner4211ca92006-04-14 06:01:58 +00005577 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005578 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005579 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00005580 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005581 return FP;
5582}
5583
Dan Gohman21cea8a2010-04-17 15:26:15 +00005584SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
5585 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005586 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005587 /*
5588 The rounding mode is in bits 30:31 of FPSR, and has the following
5589 settings:
5590 00 Round to nearest
5591 01 Round to 0
5592 10 Round to +inf
5593 11 Round to -inf
5594
5595 FLT_ROUNDS, on the other hand, expects the following:
5596 -1 Undefined
5597 0 Round to 0
5598 1 Round to nearest
5599 2 Round to +inf
5600 3 Round to -inf
5601
5602 To perform the conversion, we do:
5603 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
5604 */
5605
5606 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005607 EVT VT = Op.getValueType();
5608 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005609
5610 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005611 EVT NodeTys[] = {
5612 MVT::f64, // return register
5613 MVT::Glue // unused in this context
5614 };
Craig Topper2d2aa0c2014-04-30 07:17:30 +00005615 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, None);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005616
5617 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00005618 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005619 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005620 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00005621 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005622
5623 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005624 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005625 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00005626 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005627 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005628
5629 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005630 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00005631 DAG.getNode(ISD::AND, dl, MVT::i32,
5632 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005633 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00005634 DAG.getNode(ISD::SRL, dl, MVT::i32,
5635 DAG.getNode(ISD::AND, dl, MVT::i32,
5636 DAG.getNode(ISD::XOR, dl, MVT::i32,
5637 CWD, DAG.getConstant(3, MVT::i32)),
5638 DAG.getConstant(3, MVT::i32)),
5639 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005640
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005641 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00005642 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005643
Duncan Sands13237ac2008-06-06 12:08:01 +00005644 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00005645 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005646}
5647
Dan Gohman21cea8a2010-04-17 15:26:15 +00005648SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005649 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005650 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005651 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00005652 assert(Op.getNumOperands() == 3 &&
5653 VT == Op.getOperand(1).getValueType() &&
5654 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005655
Chris Lattner601b8652006-09-20 03:47:40 +00005656 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005657 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005658 SDValue Lo = Op.getOperand(0);
5659 SDValue Hi = Op.getOperand(1);
5660 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005661 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005662
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005663 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005664 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005665 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5666 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5667 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5668 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005669 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005670 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5671 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5672 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005673 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005674 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005675}
5676
Dan Gohman21cea8a2010-04-17 15:26:15 +00005677SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005678 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005679 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00005680 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005681 assert(Op.getNumOperands() == 3 &&
5682 VT == Op.getOperand(1).getValueType() &&
5683 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005684
Dan Gohman8d2ead22008-03-07 20:36:53 +00005685 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005686 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005687 SDValue Lo = Op.getOperand(0);
5688 SDValue Hi = Op.getOperand(1);
5689 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005690 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005691
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005692 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005693 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005694 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5695 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5696 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5697 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005698 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005699 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5700 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5701 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005702 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005703 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005704}
5705
Dan Gohman21cea8a2010-04-17 15:26:15 +00005706SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005707 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005708 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005709 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005710 assert(Op.getNumOperands() == 3 &&
5711 VT == Op.getOperand(1).getValueType() &&
5712 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005713
Dan Gohman8d2ead22008-03-07 20:36:53 +00005714 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005715 SDValue Lo = Op.getOperand(0);
5716 SDValue Hi = Op.getOperand(1);
5717 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005718 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005719
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005720 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005721 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005722 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5723 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5724 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5725 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005726 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005727 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5728 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5729 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00005730 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005731 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005732 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005733}
5734
5735//===----------------------------------------------------------------------===//
5736// Vector related lowering.
5737//
5738
Chris Lattner2a099c02006-04-17 06:00:21 +00005739/// BuildSplatI - Build a canonical splati of Val with an element size of
5740/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005741static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005742 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00005743 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005744
Owen Anderson53aa7a92009-08-10 22:56:29 +00005745 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00005746 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00005747 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005748
Owen Anderson9f944592009-08-11 20:47:22 +00005749 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005750
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005751 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5752 if (Val == -1)
5753 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005754
Owen Anderson53aa7a92009-08-10 22:56:29 +00005755 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005756
Chris Lattner2a099c02006-04-17 06:00:21 +00005757 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00005758 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005759 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00005760 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Craig Topper48d114b2014-04-26 18:35:24 +00005761 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005762 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005763}
5764
Hal Finkelcf2e9082013-05-24 23:00:14 +00005765/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5766/// specified intrinsic ID.
5767static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005768 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00005769 EVT DestVT = MVT::Other) {
5770 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5771 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5772 DAG.getConstant(IID, MVT::i32), Op);
5773}
5774
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005775/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00005776/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005777static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005778 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005779 EVT DestVT = MVT::Other) {
5780 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005781 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005782 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005783}
5784
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005785/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5786/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005787static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005788 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005789 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00005790 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005791 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005792 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005793}
5794
5795
Chris Lattner264c9082006-04-17 17:55:10 +00005796/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5797/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005798static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005799 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00005800 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00005801 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5802 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005803
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005804 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00005805 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005806 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00005807 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005808 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00005809}
5810
Chris Lattner19e90552006-04-14 05:19:18 +00005811// If this is a case we can't handle, return null and let the default
5812// expansion code take care of it. If we CAN select this case, and if it
5813// selects to a single instruction, return Op. Otherwise, if we can codegen
5814// this case more efficiently than a constant pool load, lower it to the
5815// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005816SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5817 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005818 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005819 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
Craig Toppere73658d2014-04-28 04:05:08 +00005820 assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00005821
Bob Wilson85cefe82009-03-02 23:24:16 +00005822 // Check if this is a splat of a constant value.
5823 APInt APSplatBits, APSplatUndef;
5824 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005825 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00005826 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00005827 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00005828 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00005829
Bob Wilson530e0382009-03-03 19:26:27 +00005830 unsigned SplatBits = APSplatBits.getZExtValue();
5831 unsigned SplatUndef = APSplatUndef.getZExtValue();
5832 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005833
Bob Wilson530e0382009-03-03 19:26:27 +00005834 // First, handle single instruction cases.
5835
5836 // All zeros?
5837 if (SplatBits == 0) {
5838 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00005839 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5840 SDValue Z = DAG.getConstant(0, MVT::i32);
5841 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00005842 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00005843 }
Bob Wilson530e0382009-03-03 19:26:27 +00005844 return Op;
5845 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00005846
Bob Wilson530e0382009-03-03 19:26:27 +00005847 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5848 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5849 (32-SplatBitSize));
5850 if (SextVal >= -16 && SextVal <= 15)
5851 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005852
5853
Bob Wilson530e0382009-03-03 19:26:27 +00005854 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005855
Bob Wilson530e0382009-03-03 19:26:27 +00005856 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00005857 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5858 // If this value is in the range [17,31] and is odd, use:
5859 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5860 // If this value is in the range [-31,-17] and is odd, use:
5861 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5862 // Note the last two are three-instruction sequences.
5863 if (SextVal >= -32 && SextVal <= 31) {
5864 // To avoid having these optimizations undone by constant folding,
5865 // we convert to a pseudo that will be expanded later into one of
5866 // the above forms.
5867 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt71dddd52014-05-27 15:57:51 +00005868 EVT VT = (SplatSize == 1 ? MVT::v16i8 :
5869 (SplatSize == 2 ? MVT::v8i16 : MVT::v4i32));
5870 SDValue EltSize = DAG.getConstant(SplatSize, MVT::i32);
5871 SDValue RetVal = DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
5872 if (VT == Op.getValueType())
5873 return RetVal;
5874 else
5875 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), RetVal);
Bob Wilson530e0382009-03-03 19:26:27 +00005876 }
5877
5878 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5879 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5880 // for fneg/fabs.
5881 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5882 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00005883 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005884
5885 // Make the VSLW intrinsic, computing 0x8000_0000.
5886 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5887 OnesV, DAG, dl);
5888
5889 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00005890 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00005891 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005892 }
5893
Bill Schmidt4aedff82014-06-06 14:06:26 +00005894 // The remaining cases assume either big endian element order or
5895 // a splat-size that equates to the element size of the vector
5896 // to be built. An example that doesn't work for little endian is
5897 // {0, -1, 0, -1, 0, -1, 0, -1} which has a splat size of 32 bits
5898 // and a vector element size of 16 bits. The code below will
5899 // produce the vector in big endian element order, which for little
5900 // endian is {-1, 0, -1, 0, -1, 0, -1, 0}.
5901
5902 // For now, just avoid these optimizations in that case.
5903 // FIXME: Develop correct optimizations for LE with mismatched
5904 // splat and element sizes.
5905
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005906 if (Subtarget.isLittleEndian() &&
Bill Schmidt4aedff82014-06-06 14:06:26 +00005907 SplatSize != Op.getValueType().getVectorElementType().getSizeInBits())
5908 return SDValue();
5909
Bob Wilson530e0382009-03-03 19:26:27 +00005910 // Check to see if this is a wide variety of vsplti*, binop self cases.
5911 static const signed char SplatCsts[] = {
5912 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5913 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5914 };
5915
5916 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5917 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5918 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5919 int i = SplatCsts[idx];
5920
5921 // Figure out what shift amount will be used by altivec if shifted by i in
5922 // this splat size.
5923 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5924
5925 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00005926 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005927 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005928 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5929 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5930 Intrinsic::ppc_altivec_vslw
5931 };
5932 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005933 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005934 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005935
Bob Wilson530e0382009-03-03 19:26:27 +00005936 // vsplti + srl self.
5937 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005938 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005939 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5940 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5941 Intrinsic::ppc_altivec_vsrw
5942 };
5943 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005944 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005945 }
5946
Bob Wilson530e0382009-03-03 19:26:27 +00005947 // vsplti + sra self.
5948 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005949 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005950 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5951 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5952 Intrinsic::ppc_altivec_vsraw
5953 };
5954 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005955 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005956 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005957
Bob Wilson530e0382009-03-03 19:26:27 +00005958 // vsplti + rol self.
5959 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5960 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005961 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005962 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5963 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5964 Intrinsic::ppc_altivec_vrlw
5965 };
5966 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005967 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005968 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005969
Bob Wilson530e0382009-03-03 19:26:27 +00005970 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00005971 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005972 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005973 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00005974 }
Bob Wilson530e0382009-03-03 19:26:27 +00005975 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00005976 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005977 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005978 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00005979 }
Bob Wilson530e0382009-03-03 19:26:27 +00005980 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00005981 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005982 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005983 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5984 }
5985 }
5986
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005987 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00005988}
5989
Chris Lattner071ad012006-04-17 05:28:54 +00005990/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5991/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005992static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005993 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005994 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00005995 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00005996 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00005997 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005998
Chris Lattner071ad012006-04-17 05:28:54 +00005999 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00006000 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 +00006001 OP_VMRGHW,
6002 OP_VMRGLW,
6003 OP_VSPLTISW0,
6004 OP_VSPLTISW1,
6005 OP_VSPLTISW2,
6006 OP_VSPLTISW3,
6007 OP_VSLDOI4,
6008 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00006009 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00006010 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00006011
Chris Lattner071ad012006-04-17 05:28:54 +00006012 if (OpNum == OP_COPY) {
6013 if (LHSID == (1*9+2)*9+3) return LHS;
6014 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
6015 return RHS;
6016 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006017
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006018 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006019 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
6020 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006021
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006022 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00006023 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006024 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00006025 case OP_VMRGHW:
6026 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
6027 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
6028 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
6029 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
6030 break;
6031 case OP_VMRGLW:
6032 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
6033 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
6034 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
6035 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
6036 break;
6037 case OP_VSPLTISW0:
6038 for (unsigned i = 0; i != 16; ++i)
6039 ShufIdxs[i] = (i&3)+0;
6040 break;
6041 case OP_VSPLTISW1:
6042 for (unsigned i = 0; i != 16; ++i)
6043 ShufIdxs[i] = (i&3)+4;
6044 break;
6045 case OP_VSPLTISW2:
6046 for (unsigned i = 0; i != 16; ++i)
6047 ShufIdxs[i] = (i&3)+8;
6048 break;
6049 case OP_VSPLTISW3:
6050 for (unsigned i = 0; i != 16; ++i)
6051 ShufIdxs[i] = (i&3)+12;
6052 break;
6053 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006054 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006055 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006056 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006057 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006058 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006059 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00006060 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00006061 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
6062 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00006063 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00006064 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00006065}
6066
Chris Lattner19e90552006-04-14 05:19:18 +00006067/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
6068/// is a shuffle we can handle in a single instruction, return it. Otherwise,
6069/// return the code it can be lowered into. Worst case, it can always be
6070/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006071SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006072 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006073 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006074 SDValue V1 = Op.getOperand(0);
6075 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006076 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006077 EVT VT = Op.getValueType();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006078 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006079
Chris Lattner19e90552006-04-14 05:19:18 +00006080 // Cases that are handled by instructions that take permute immediates
6081 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
6082 // selected by the instruction selector.
6083 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006084 if (PPC::isSplatShuffleMask(SVOp, 1) ||
6085 PPC::isSplatShuffleMask(SVOp, 2) ||
6086 PPC::isSplatShuffleMask(SVOp, 4) ||
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00006087 PPC::isVPKUWUMShuffleMask(SVOp, 1, DAG) ||
6088 PPC::isVPKUHUMShuffleMask(SVOp, 1, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00006089 PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00006090 PPC::isVMRGLShuffleMask(SVOp, 1, 1, DAG) ||
6091 PPC::isVMRGLShuffleMask(SVOp, 2, 1, DAG) ||
6092 PPC::isVMRGLShuffleMask(SVOp, 4, 1, DAG) ||
6093 PPC::isVMRGHShuffleMask(SVOp, 1, 1, DAG) ||
6094 PPC::isVMRGHShuffleMask(SVOp, 2, 1, DAG) ||
6095 PPC::isVMRGHShuffleMask(SVOp, 4, 1, DAG)) {
Chris Lattner19e90552006-04-14 05:19:18 +00006096 return Op;
6097 }
6098 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006099
Chris Lattner19e90552006-04-14 05:19:18 +00006100 // Altivec has a variety of "shuffle immediates" that take two vector inputs
6101 // and produce a fixed permutation. If any of these match, do not lower to
6102 // VPERM.
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00006103 unsigned int ShuffleKind = isLittleEndian ? 2 : 0;
Ulrich Weigandcc9909b2014-08-04 13:53:40 +00006104 if (PPC::isVPKUWUMShuffleMask(SVOp, ShuffleKind, DAG) ||
6105 PPC::isVPKUHUMShuffleMask(SVOp, ShuffleKind, DAG) ||
Bill Schmidt42a69362014-08-05 20:47:25 +00006106 PPC::isVSLDOIShuffleMask(SVOp, ShuffleKind, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00006107 PPC::isVMRGLShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
6108 PPC::isVMRGLShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
6109 PPC::isVMRGLShuffleMask(SVOp, 4, ShuffleKind, DAG) ||
6110 PPC::isVMRGHShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
6111 PPC::isVMRGHShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
6112 PPC::isVMRGHShuffleMask(SVOp, 4, ShuffleKind, DAG))
Chris Lattner19e90552006-04-14 05:19:18 +00006113 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006114
Chris Lattner071ad012006-04-17 05:28:54 +00006115 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
6116 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00006117 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00006118
Chris Lattner071ad012006-04-17 05:28:54 +00006119 unsigned PFIndexes[4];
6120 bool isFourElementShuffle = true;
6121 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
6122 unsigned EltNo = 8; // Start out undef.
6123 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006124 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00006125 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006126
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006127 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00006128 if ((ByteSource & 3) != j) {
6129 isFourElementShuffle = false;
6130 break;
6131 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006132
Chris Lattner071ad012006-04-17 05:28:54 +00006133 if (EltNo == 8) {
6134 EltNo = ByteSource/4;
6135 } else if (EltNo != ByteSource/4) {
6136 isFourElementShuffle = false;
6137 break;
6138 }
6139 }
6140 PFIndexes[i] = EltNo;
6141 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006142
6143 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00006144 // perfect shuffle vector to determine if it is cost effective to do this as
6145 // discrete instructions, or whether we should use a vperm.
Bill Schmidtf910a062014-06-10 14:35:01 +00006146 // For now, we skip this for little endian until such time as we have a
6147 // little-endian perfect shuffle table.
6148 if (isFourElementShuffle && !isLittleEndian) {
Chris Lattner071ad012006-04-17 05:28:54 +00006149 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006150 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00006151 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00006152
Chris Lattner071ad012006-04-17 05:28:54 +00006153 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6154 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006155
Chris Lattner071ad012006-04-17 05:28:54 +00006156 // Determining when to avoid vperm is tricky. Many things affect the cost
6157 // of vperm, particularly how many times the perm mask needs to be computed.
6158 // For example, if the perm mask can be hoisted out of a loop or is already
6159 // used (perhaps because there are multiple permutes with the same shuffle
6160 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
6161 // the loop requires an extra register.
6162 //
6163 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00006164 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00006165 // available, if this block is within a loop, we should avoid using vperm
6166 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006167 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006168 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006169 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006170
Chris Lattner19e90552006-04-14 05:19:18 +00006171 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
6172 // vector that will get spilled to the constant pool.
6173 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006174
Chris Lattner19e90552006-04-14 05:19:18 +00006175 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
6176 // that it is in input element units, not in bytes. Convert now.
Bill Schmidt4aedff82014-06-06 14:06:26 +00006177
6178 // For little endian, the order of the input vectors is reversed, and
6179 // the permutation mask is complemented with respect to 31. This is
6180 // necessary to produce proper semantics with the big-endian-biased vperm
6181 // instruction.
Owen Anderson53aa7a92009-08-10 22:56:29 +00006182 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006183 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006184
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006185 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006186 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
6187 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00006188
Chris Lattner19e90552006-04-14 05:19:18 +00006189 for (unsigned j = 0; j != BytesPerElement; ++j)
Bill Schmidt4aedff82014-06-06 14:06:26 +00006190 if (isLittleEndian)
6191 ResultMask.push_back(DAG.getConstant(31 - (SrcElt*BytesPerElement+j),
6192 MVT::i32));
6193 else
6194 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
6195 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00006196 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006197
Owen Anderson9f944592009-08-11 20:47:22 +00006198 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Craig Topper48d114b2014-04-26 18:35:24 +00006199 ResultMask);
Bill Schmidt4aedff82014-06-06 14:06:26 +00006200 if (isLittleEndian)
6201 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
6202 V2, V1, VPermMask);
6203 else
6204 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
6205 V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00006206}
6207
Chris Lattner9754d142006-04-18 17:59:36 +00006208/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
6209/// altivec comparison. If it is, return true and fill in Opc/isDot with
6210/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006211static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner9754d142006-04-18 17:59:36 +00006212 bool &isDot) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00006213 unsigned IntrinsicID =
6214 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00006215 CompareOpc = -1;
6216 isDot = false;
6217 switch (IntrinsicID) {
6218 default: return false;
6219 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00006220 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
6221 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
6222 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
6223 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
6224 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
6225 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
6226 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
6227 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
6228 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
6229 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
6230 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
6231 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
6232 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006233
Chris Lattner4211ca92006-04-14 06:01:58 +00006234 // Normal Comparisons.
6235 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
6236 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
6237 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
6238 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
6239 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
6240 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
6241 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
6242 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
6243 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
6244 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
6245 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
6246 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
6247 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
6248 }
Chris Lattner9754d142006-04-18 17:59:36 +00006249 return true;
6250}
6251
6252/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
6253/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006254SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006255 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00006256 // If this is a lowered altivec predicate compare, CompareOpc is set to the
6257 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006258 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00006259 int CompareOpc;
6260 bool isDot;
6261 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006262 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006263
Chris Lattner9754d142006-04-18 17:59:36 +00006264 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00006265 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00006266 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00006267 Op.getOperand(1), Op.getOperand(2),
6268 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00006269 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00006270 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006271
Chris Lattner4211ca92006-04-14 06:01:58 +00006272 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006273 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00006274 Op.getOperand(2), // LHS
6275 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00006276 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00006277 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00006278 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00006279 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006280
Chris Lattner4211ca92006-04-14 06:01:58 +00006281 // Now that we have the comparison, emit a copy from the CR to a GPR.
6282 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00006283 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00006284 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00006285 CompNode.getValue(1));
6286
Chris Lattner4211ca92006-04-14 06:01:58 +00006287 // Unpack the result based on how the target uses it.
6288 unsigned BitNo; // Bit # of CR6.
6289 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00006290 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00006291 default: // Can't happen, don't crash on invalid number though.
6292 case 0: // Return the value of the EQ bit of CR6.
6293 BitNo = 0; InvertBit = false;
6294 break;
6295 case 1: // Return the inverted value of the EQ bit of CR6.
6296 BitNo = 0; InvertBit = true;
6297 break;
6298 case 2: // Return the value of the LT bit of CR6.
6299 BitNo = 2; InvertBit = false;
6300 break;
6301 case 3: // Return the inverted value of the LT bit of CR6.
6302 BitNo = 2; InvertBit = true;
6303 break;
6304 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006305
Chris Lattner4211ca92006-04-14 06:01:58 +00006306 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00006307 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
6308 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00006309 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00006310 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
6311 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006312
Chris Lattner4211ca92006-04-14 06:01:58 +00006313 // If we are supposed to, toggle the bit.
6314 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00006315 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
6316 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00006317 return Flags;
6318}
6319
Hal Finkel5c0d1452014-03-30 13:22:59 +00006320SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
6321 SelectionDAG &DAG) const {
6322 SDLoc dl(Op);
6323 // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int
6324 // instructions), but for smaller types, we need to first extend up to v2i32
6325 // before doing going farther.
6326 if (Op.getValueType() == MVT::v2i64) {
6327 EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
6328 if (ExtVT != MVT::v2i32) {
6329 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0));
6330 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op,
6331 DAG.getValueType(EVT::getVectorVT(*DAG.getContext(),
6332 ExtVT.getVectorElementType(), 4)));
6333 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op);
6334 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op,
6335 DAG.getValueType(MVT::v2i32));
6336 }
6337
6338 return Op;
6339 }
6340
6341 return SDValue();
6342}
6343
Scott Michelcf0da6c2009-02-17 22:15:04 +00006344SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006345 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006346 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00006347 // Create a stack slot that is 16-byte aligned.
6348 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00006349 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00006350 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006351 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006352
Chris Lattner4211ca92006-04-14 06:01:58 +00006353 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00006354 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00006355 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00006356 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00006357 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00006358 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006359 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00006360}
6361
Dan Gohman21cea8a2010-04-17 15:26:15 +00006362SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006363 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006364 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006365 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006366
Owen Anderson9f944592009-08-11 20:47:22 +00006367 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
6368 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006369
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006370 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006371 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006372
Chris Lattner7e4398742006-04-18 03:43:48 +00006373 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00006374 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
6375 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
6376 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006377
Chris Lattner7e4398742006-04-18 03:43:48 +00006378 // Low parts multiplied together, generating 32-bit results (we ignore the
6379 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006380 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00006381 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006382
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006383 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00006384 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00006385 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006386 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006387 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00006388 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
6389 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006390 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006391
Owen Anderson9f944592009-08-11 20:47:22 +00006392 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00006393
Chris Lattner96d50482006-04-18 04:28:57 +00006394 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006395 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00006396 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006397 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006398 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006399
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006400 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006401 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00006402 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00006403 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006404
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006405 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006406 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00006407 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00006408 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006409
Bill Schmidt42995e82014-06-09 16:06:29 +00006410 // Merge the results together. Because vmuleub and vmuloub are
6411 // instructions with a big-endian bias, we must reverse the
6412 // element numbering and reverse the meaning of "odd" and "even"
6413 // when generating little endian code.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006414 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006415 for (unsigned i = 0; i != 8; ++i) {
Bill Schmidt42995e82014-06-09 16:06:29 +00006416 if (isLittleEndian) {
6417 Ops[i*2 ] = 2*i;
6418 Ops[i*2+1] = 2*i+16;
6419 } else {
6420 Ops[i*2 ] = 2*i+1;
6421 Ops[i*2+1] = 2*i+1+16;
6422 }
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006423 }
Bill Schmidt42995e82014-06-09 16:06:29 +00006424 if (isLittleEndian)
6425 return DAG.getVectorShuffle(MVT::v16i8, dl, OddParts, EvenParts, Ops);
6426 else
6427 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00006428 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006429 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00006430 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006431}
6432
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006433/// LowerOperation - Provide custom lowering hooks for some operations.
6434///
Dan Gohman21cea8a2010-04-17 15:26:15 +00006435SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006436 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006437 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00006438 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00006439 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006440 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00006441 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00006442 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006443 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00006444 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
6445 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006446 case ISD::VASTART:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006447 return LowerVASTART(Op, DAG, Subtarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006448
6449 case ISD::VAARG:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006450 return LowerVAARG(Op, DAG, Subtarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00006451
Roman Divackyc3825df2013-07-25 21:36:47 +00006452 case ISD::VACOPY:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006453 return LowerVACOPY(Op, DAG, Subtarget);
Roman Divackyc3825df2013-07-25 21:36:47 +00006454
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006455 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, Subtarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00006456 case ISD::DYNAMIC_STACKALLOC:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006457 return LowerDYNAMIC_STACKALLOC(Op, DAG, Subtarget);
Evan Cheng51096af2008-04-19 01:30:48 +00006458
Hal Finkel756810f2013-03-21 21:37:52 +00006459 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
6460 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
6461
Hal Finkel940ab932014-02-28 00:27:01 +00006462 case ISD::LOAD: return LowerLOAD(Op, DAG);
6463 case ISD::STORE: return LowerSTORE(Op, DAG);
6464 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006465 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006466 case ISD::FP_TO_UINT:
6467 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006468 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00006469 case ISD::UINT_TO_FP:
6470 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00006471 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006472
Chris Lattner4211ca92006-04-14 06:01:58 +00006473 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00006474 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
6475 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
6476 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006477
Chris Lattner4211ca92006-04-14 06:01:58 +00006478 // Vector-related lowering.
6479 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6480 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6481 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6482 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Hal Finkel5c0d1452014-03-30 13:22:59 +00006483 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006484 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006485
Hal Finkel25c19922013-05-15 21:37:41 +00006486 // For counter-based loop handling.
6487 case ISD::INTRINSIC_W_CHAIN: return SDValue();
6488
Chris Lattnerf6a81562007-12-08 06:59:59 +00006489 // Frame & Return address.
6490 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00006491 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00006492 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006493}
6494
Duncan Sands6ed40142008-12-01 11:39:25 +00006495void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
6496 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006497 SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00006498 const TargetMachine &TM = getTargetMachine();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006499 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00006500 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00006501 default:
Craig Toppere55c5562012-02-07 02:50:20 +00006502 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkelbbdee932014-12-02 22:01:00 +00006503 case ISD::READCYCLECOUNTER: {
6504 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
6505 SDValue RTB = DAG.getNode(PPCISD::READ_TIME_BASE, dl, VTs, N->getOperand(0));
6506
6507 Results.push_back(RTB);
6508 Results.push_back(RTB.getValue(1));
6509 Results.push_back(RTB.getValue(2));
6510 break;
6511 }
Hal Finkel25c19922013-05-15 21:37:41 +00006512 case ISD::INTRINSIC_W_CHAIN: {
6513 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
6514 Intrinsic::ppc_is_decremented_ctr_nonzero)
6515 break;
6516
6517 assert(N->getValueType(0) == MVT::i1 &&
6518 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00006519 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00006520 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
6521 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
6522 N->getOperand(1));
6523
6524 Results.push_back(NewInt);
6525 Results.push_back(NewInt.getValue(1));
6526 break;
6527 }
Roman Divacky4394e682011-06-28 15:30:42 +00006528 case ISD::VAARG: {
6529 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
6530 || TM.getSubtarget<PPCSubtarget>().isPPC64())
6531 return;
6532
6533 EVT VT = N->getValueType(0);
6534
6535 if (VT == MVT::i64) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006536 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, Subtarget);
Roman Divacky4394e682011-06-28 15:30:42 +00006537
6538 Results.push_back(NewNode);
6539 Results.push_back(NewNode.getValue(1));
6540 }
6541 return;
6542 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006543 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00006544 assert(N->getValueType(0) == MVT::ppcf128);
6545 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006546 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006547 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006548 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00006549 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006550 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006551 DAG.getIntPtrConstant(1));
6552
Ulrich Weigand874fc622013-03-26 10:56:22 +00006553 // Add the two halves of the long double in round-to-zero mode.
6554 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00006555
6556 // We know the low half is about to be thrown away, so just use something
6557 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00006558 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00006559 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00006560 return;
Duncan Sands2a287912008-07-19 16:26:02 +00006561 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006562 case ISD::FP_TO_SINT:
Bill Schmidt41221692013-07-09 18:50:20 +00006563 // LowerFP_TO_INT() can only handle f32 and f64.
6564 if (N->getOperand(0).getValueType() == MVT::ppcf128)
6565 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006566 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00006567 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00006568 }
6569}
6570
6571
Chris Lattner4211ca92006-04-14 06:01:58 +00006572//===----------------------------------------------------------------------===//
6573// Other Lowering Code
6574//===----------------------------------------------------------------------===//
6575
Robin Morisset22129962014-09-23 20:46:49 +00006576static Instruction* callIntrinsic(IRBuilder<> &Builder, Intrinsic::ID Id) {
6577 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
6578 Function *Func = Intrinsic::getDeclaration(M, Id);
6579 return Builder.CreateCall(Func);
6580}
6581
6582// The mappings for emitLeading/TrailingFence is taken from
6583// http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
6584Instruction* PPCTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
6585 AtomicOrdering Ord, bool IsStore,
6586 bool IsLoad) const {
6587 if (Ord == SequentiallyConsistent)
6588 return callIntrinsic(Builder, Intrinsic::ppc_sync);
6589 else if (isAtLeastRelease(Ord))
6590 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
6591 else
6592 return nullptr;
6593}
6594
6595Instruction* PPCTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
6596 AtomicOrdering Ord, bool IsStore,
6597 bool IsLoad) const {
6598 if (IsLoad && isAtLeastAcquire(Ord))
6599 return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
6600 // FIXME: this is too conservative, a dependent branch + isync is enough.
6601 // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html and
6602 // http://www.rdrop.com/users/paulmck/scalability/paper/N2745r.2011.03.04a.html
6603 // and http://www.cl.cam.ac.uk/~pes20/cppppc/ for justification.
6604 else
6605 return nullptr;
6606}
6607
Chris Lattner9b577f12005-08-26 21:23:58 +00006608MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00006609PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman747e55b2009-02-07 16:15:20 +00006610 bool is64bit, unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006611 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christopherd9134482014-08-04 21:25:23 +00006612 const TargetInstrInfo *TII =
6613 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Dale Johannesend4eb0522008-08-25 22:34:37 +00006614
6615 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6616 MachineFunction *F = BB->getParent();
6617 MachineFunction::iterator It = BB;
6618 ++It;
6619
6620 unsigned dest = MI->getOperand(0).getReg();
6621 unsigned ptrA = MI->getOperand(1).getReg();
6622 unsigned ptrB = MI->getOperand(2).getReg();
6623 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006624 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00006625
6626 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6627 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6628 F->insert(It, loopMBB);
6629 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006630 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006631 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006632 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006633
6634 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006635 unsigned TmpReg = (!BinOpcode) ? incr :
Craig Topper61e88f42014-11-21 05:58:21 +00006636 RegInfo.createVirtualRegister( is64bit ? &PPC::G8RCRegClass
6637 : &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006638
6639 // thisMBB:
6640 // ...
6641 // fallthrough --> loopMBB
6642 BB->addSuccessor(loopMBB);
6643
6644 // loopMBB:
6645 // l[wd]arx dest, ptr
6646 // add r0, dest, incr
6647 // st[wd]cx. r0, ptr
6648 // bne- loopMBB
6649 // fallthrough --> exitMBB
6650 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006651 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00006652 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006653 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006654 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
6655 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesend4eb0522008-08-25 22:34:37 +00006656 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006657 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006658 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006659 BB->addSuccessor(loopMBB);
6660 BB->addSuccessor(exitMBB);
6661
6662 // exitMBB:
6663 // ...
6664 BB = exitMBB;
6665 return BB;
6666}
6667
6668MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00006669PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00006670 MachineBasicBlock *BB,
6671 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00006672 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006673 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Eric Christopherd9134482014-08-04 21:25:23 +00006674 const TargetInstrInfo *TII =
6675 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Dale Johannesena32affb2008-08-28 17:53:09 +00006676 // In 64 bit mode we have to use 64 bits for addresses, even though the
6677 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
6678 // registers without caring whether they're 32 or 64, but here we're
6679 // doing actual arithmetic on the addresses.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006680 bool is64bit = Subtarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00006681 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00006682
6683 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6684 MachineFunction *F = BB->getParent();
6685 MachineFunction::iterator It = BB;
6686 ++It;
6687
6688 unsigned dest = MI->getOperand(0).getReg();
6689 unsigned ptrA = MI->getOperand(1).getReg();
6690 unsigned ptrB = MI->getOperand(2).getReg();
6691 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006692 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00006693
6694 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6695 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6696 F->insert(It, loopMBB);
6697 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006698 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006699 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006700 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006701
6702 MachineRegisterInfo &RegInfo = F->getRegInfo();
Craig Topper61e88f42014-11-21 05:58:21 +00006703 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass
6704 : &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00006705 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6706 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6707 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6708 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
6709 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6710 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6711 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6712 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6713 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
6714 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006715 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006716 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006717 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006718
6719 // thisMBB:
6720 // ...
6721 // fallthrough --> loopMBB
6722 BB->addSuccessor(loopMBB);
6723
6724 // The 4-byte load must be aligned, while a char or short may be
6725 // anywhere in the word. Hence all this nasty bookkeeping code.
6726 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6727 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006728 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00006729 // rlwinm ptr, ptr1, 0, 0, 29
6730 // slw incr2, incr, shift
6731 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6732 // slw mask, mask2, shift
6733 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00006734 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006735 // add tmp, tmpDest, incr2
6736 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00006737 // and tmp3, tmp, mask
6738 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00006739 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00006740 // bne- loopMBB
6741 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006742 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006743 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00006744 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006745 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006746 .addReg(ptrA).addReg(ptrB);
6747 } else {
6748 Ptr1Reg = ptrB;
6749 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006750 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006751 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006752 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006753 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6754 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006755 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006756 .addReg(Ptr1Reg).addImm(0).addImm(61);
6757 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006758 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006759 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006760 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006761 .addReg(incr).addReg(ShiftReg);
6762 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006763 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00006764 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006765 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6766 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00006767 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006768 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006769 .addReg(Mask2Reg).addReg(ShiftReg);
6770
6771 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006772 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006773 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006774 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006775 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006776 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006777 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006778 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006779 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006780 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006781 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006782 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00006783 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006784 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006785 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006786 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006787 BB->addSuccessor(loopMBB);
6788 BB->addSuccessor(exitMBB);
6789
6790 // exitMBB:
6791 // ...
6792 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006793 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6794 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00006795 return BB;
6796}
6797
Hal Finkel756810f2013-03-21 21:37:52 +00006798llvm::MachineBasicBlock*
6799PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6800 MachineBasicBlock *MBB) const {
6801 DebugLoc DL = MI->getDebugLoc();
Eric Christopherd9134482014-08-04 21:25:23 +00006802 const TargetInstrInfo *TII =
6803 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00006804
6805 MachineFunction *MF = MBB->getParent();
6806 MachineRegisterInfo &MRI = MF->getRegInfo();
6807
6808 const BasicBlock *BB = MBB->getBasicBlock();
6809 MachineFunction::iterator I = MBB;
6810 ++I;
6811
6812 // Memory Reference
6813 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6814 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6815
6816 unsigned DstReg = MI->getOperand(0).getReg();
6817 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6818 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6819 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6820 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6821
6822 MVT PVT = getPointerTy();
6823 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6824 "Invalid Pointer Size!");
6825 // For v = setjmp(buf), we generate
6826 //
6827 // thisMBB:
6828 // SjLjSetup mainMBB
6829 // bl mainMBB
6830 // v_restore = 1
6831 // b sinkMBB
6832 //
6833 // mainMBB:
6834 // buf[LabelOffset] = LR
6835 // v_main = 0
6836 //
6837 // sinkMBB:
6838 // v = phi(main, restore)
6839 //
6840
6841 MachineBasicBlock *thisMBB = MBB;
6842 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6843 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6844 MF->insert(I, mainMBB);
6845 MF->insert(I, sinkMBB);
6846
6847 MachineInstrBuilder MIB;
6848
6849 // Transfer the remainder of BB and its successor edges to sinkMBB.
6850 sinkMBB->splice(sinkMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006851 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Hal Finkel756810f2013-03-21 21:37:52 +00006852 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6853
6854 // Note that the structure of the jmp_buf used here is not compatible
6855 // with that used by libc, and is not designed to be. Specifically, it
6856 // stores only those 'reserved' registers that LLVM does not otherwise
6857 // understand how to spill. Also, by convention, by the time this
6858 // intrinsic is called, Clang has already stored the frame address in the
6859 // first slot of the buffer and stack address in the third. Following the
6860 // X86 target code, we'll store the jump address in the second slot. We also
6861 // need to save the TOC pointer (R2) to handle jumps between shared
6862 // libraries, and that will be stored in the fourth slot. The thread
6863 // identifier (R13) is not affected.
6864
6865 // thisMBB:
6866 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6867 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006868 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006869
6870 // Prepare IP either in reg.
6871 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6872 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6873 unsigned BufReg = MI->getOperand(1).getReg();
6874
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006875 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI()) {
Hal Finkel756810f2013-03-21 21:37:52 +00006876 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6877 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006878 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006879 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006880 MIB.setMemRefs(MMOBegin, MMOEnd);
6881 }
6882
Hal Finkelf05d6c72013-07-17 23:50:51 +00006883 // Naked functions never have a base pointer, and so we use r1. For all
6884 // other functions, this decision must be delayed until during PEI.
6885 unsigned BaseReg;
6886 if (MF->getFunction()->getAttributes().hasAttribute(
6887 AttributeSet::FunctionIndex, Attribute::Naked))
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006888 BaseReg = Subtarget.isPPC64() ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006889 else
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006890 BaseReg = Subtarget.isPPC64() ? PPC::BP8 : PPC::BP;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006891
6892 MIB = BuildMI(*thisMBB, MI, DL,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006893 TII->get(Subtarget.isPPC64() ? PPC::STD : PPC::STW))
Hal Finkelf05d6c72013-07-17 23:50:51 +00006894 .addReg(BaseReg)
6895 .addImm(BPOffset)
6896 .addReg(BufReg);
6897 MIB.setMemRefs(MMOBegin, MMOEnd);
6898
Hal Finkel756810f2013-03-21 21:37:52 +00006899 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00006900 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling5e7656b2013-06-07 07:55:53 +00006901 const PPCRegisterInfo *TRI =
Eric Christopherd9134482014-08-04 21:25:23 +00006902 getTargetMachine().getSubtarget<PPCSubtarget>().getRegisterInfo();
Bill Wendling5e7656b2013-06-07 07:55:53 +00006903 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00006904
6905 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6906
6907 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6908 .addMBB(mainMBB);
6909 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6910
6911 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6912 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6913
6914 // mainMBB:
6915 // mainDstReg = 0
6916 MIB = BuildMI(mainMBB, DL,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006917 TII->get(Subtarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006918
6919 // Store IP
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006920 if (Subtarget.isPPC64()) {
Hal Finkel756810f2013-03-21 21:37:52 +00006921 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6922 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006923 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006924 .addReg(BufReg);
6925 } else {
6926 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6927 .addReg(LabelReg)
6928 .addImm(LabelOffset)
6929 .addReg(BufReg);
6930 }
6931
6932 MIB.setMemRefs(MMOBegin, MMOEnd);
6933
6934 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6935 mainMBB->addSuccessor(sinkMBB);
6936
6937 // sinkMBB:
6938 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6939 TII->get(PPC::PHI), DstReg)
6940 .addReg(mainDstReg).addMBB(mainMBB)
6941 .addReg(restoreDstReg).addMBB(thisMBB);
6942
6943 MI->eraseFromParent();
6944 return sinkMBB;
6945}
6946
6947MachineBasicBlock *
6948PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6949 MachineBasicBlock *MBB) const {
6950 DebugLoc DL = MI->getDebugLoc();
Eric Christopherd9134482014-08-04 21:25:23 +00006951 const TargetInstrInfo *TII =
6952 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Hal Finkel756810f2013-03-21 21:37:52 +00006953
6954 MachineFunction *MF = MBB->getParent();
6955 MachineRegisterInfo &MRI = MF->getRegInfo();
6956
6957 // Memory Reference
6958 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6959 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6960
6961 MVT PVT = getPointerTy();
6962 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6963 "Invalid Pointer Size!");
6964
6965 const TargetRegisterClass *RC =
6966 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6967 unsigned Tmp = MRI.createVirtualRegister(RC);
6968 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6969 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6970 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +00006971 unsigned BP = (PVT == MVT::i64) ? PPC::X30 :
6972 (Subtarget.isSVR4ABI() &&
6973 MF->getTarget().getRelocationModel() == Reloc::PIC_ ?
6974 PPC::R29 : PPC::R30);
Hal Finkel756810f2013-03-21 21:37:52 +00006975
6976 MachineInstrBuilder MIB;
6977
6978 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6979 const int64_t SPOffset = 2 * PVT.getStoreSize();
6980 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006981 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006982
6983 unsigned BufReg = MI->getOperand(0).getReg();
6984
6985 // Reload FP (the jumped-to function may not have had a
6986 // frame pointer, and if so, then its r31 will be restored
6987 // as necessary).
6988 if (PVT == MVT::i64) {
6989 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6990 .addImm(0)
6991 .addReg(BufReg);
6992 } else {
6993 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6994 .addImm(0)
6995 .addReg(BufReg);
6996 }
6997 MIB.setMemRefs(MMOBegin, MMOEnd);
6998
6999 // Reload IP
7000 if (PVT == MVT::i64) {
7001 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00007002 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00007003 .addReg(BufReg);
7004 } else {
7005 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
7006 .addImm(LabelOffset)
7007 .addReg(BufReg);
7008 }
7009 MIB.setMemRefs(MMOBegin, MMOEnd);
7010
7011 // Reload SP
7012 if (PVT == MVT::i64) {
7013 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00007014 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00007015 .addReg(BufReg);
7016 } else {
7017 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
7018 .addImm(SPOffset)
7019 .addReg(BufReg);
7020 }
7021 MIB.setMemRefs(MMOBegin, MMOEnd);
7022
Hal Finkelf05d6c72013-07-17 23:50:51 +00007023 // Reload BP
7024 if (PVT == MVT::i64) {
7025 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
7026 .addImm(BPOffset)
7027 .addReg(BufReg);
7028 } else {
7029 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
7030 .addImm(BPOffset)
7031 .addReg(BufReg);
7032 }
7033 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00007034
7035 // Reload TOC
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007036 if (PVT == MVT::i64 && Subtarget.isSVR4ABI()) {
Hal Finkel756810f2013-03-21 21:37:52 +00007037 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00007038 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00007039 .addReg(BufReg);
7040
7041 MIB.setMemRefs(MMOBegin, MMOEnd);
7042 }
7043
7044 // Jump
7045 BuildMI(*MBB, MI, DL,
7046 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
7047 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
7048
7049 MI->eraseFromParent();
7050 return MBB;
7051}
7052
Dale Johannesena32affb2008-08-28 17:53:09 +00007053MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007054PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007055 MachineBasicBlock *BB) const {
Hal Finkel756810f2013-03-21 21:37:52 +00007056 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
7057 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
7058 return emitEHSjLjSetJmp(MI, BB);
7059 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
7060 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
7061 return emitEHSjLjLongJmp(MI, BB);
7062 }
7063
Eric Christopherd9134482014-08-04 21:25:23 +00007064 const TargetInstrInfo *TII =
7065 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00007066
7067 // To "insert" these instructions we actually have to insert their
7068 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00007069 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007070 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00007071 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00007072
Dan Gohman3b460302008-07-07 23:14:23 +00007073 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00007074
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007075 if (Subtarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Hal Finkel940ab932014-02-28 00:27:01 +00007076 MI->getOpcode() == PPC::SELECT_CC_I8 ||
7077 MI->getOpcode() == PPC::SELECT_I4 ||
7078 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00007079 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00007080 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
7081 MI->getOpcode() == PPC::SELECT_CC_I8)
7082 Cond.push_back(MI->getOperand(4));
7083 else
7084 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00007085 Cond.push_back(MI->getOperand(1));
7086
Hal Finkel460e94d2012-06-22 23:10:08 +00007087 DebugLoc dl = MI->getDebugLoc();
Eric Christopherd9134482014-08-04 21:25:23 +00007088 const TargetInstrInfo *TII =
7089 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Bill Wendling5e7656b2013-06-07 07:55:53 +00007090 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
7091 Cond, MI->getOperand(2).getReg(),
7092 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00007093 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
7094 MI->getOpcode() == PPC::SELECT_CC_I8 ||
7095 MI->getOpcode() == PPC::SELECT_CC_F4 ||
7096 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkel940ab932014-02-28 00:27:01 +00007097 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00007098 MI->getOpcode() == PPC::SELECT_CC_VSFRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00007099 MI->getOpcode() == PPC::SELECT_CC_VSRC ||
Hal Finkel940ab932014-02-28 00:27:01 +00007100 MI->getOpcode() == PPC::SELECT_I4 ||
7101 MI->getOpcode() == PPC::SELECT_I8 ||
7102 MI->getOpcode() == PPC::SELECT_F4 ||
7103 MI->getOpcode() == PPC::SELECT_F8 ||
Bill Schmidt61e65232014-10-22 13:13:40 +00007104 MI->getOpcode() == PPC::SELECT_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00007105 MI->getOpcode() == PPC::SELECT_VSFRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00007106 MI->getOpcode() == PPC::SELECT_VSRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00007107 // The incoming instruction knows the destination vreg to set, the
7108 // condition code register to branch on, the true/false values to
7109 // select between, and a branch opcode to use.
7110
7111 // thisMBB:
7112 // ...
7113 // TrueVal = ...
7114 // cmpTY ccX, r1, r2
7115 // bCC copy1MBB
7116 // fallthrough --> copy0MBB
7117 MachineBasicBlock *thisMBB = BB;
7118 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7119 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007120 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00007121 F->insert(It, copy0MBB);
7122 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007123
7124 // Transfer the remainder of BB and its successor edges to sinkMBB.
7125 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007126 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007127 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7128
Evan Cheng32e376f2008-07-12 02:23:19 +00007129 // Next, add the true and fallthrough blocks as its successors.
7130 BB->addSuccessor(copy0MBB);
7131 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007132
Hal Finkel940ab932014-02-28 00:27:01 +00007133 if (MI->getOpcode() == PPC::SELECT_I4 ||
7134 MI->getOpcode() == PPC::SELECT_I8 ||
7135 MI->getOpcode() == PPC::SELECT_F4 ||
7136 MI->getOpcode() == PPC::SELECT_F8 ||
Bill Schmidt61e65232014-10-22 13:13:40 +00007137 MI->getOpcode() == PPC::SELECT_VRRC ||
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00007138 MI->getOpcode() == PPC::SELECT_VSFRC ||
Bill Schmidt61e65232014-10-22 13:13:40 +00007139 MI->getOpcode() == PPC::SELECT_VSRC) {
Hal Finkel940ab932014-02-28 00:27:01 +00007140 BuildMI(BB, dl, TII->get(PPC::BC))
7141 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
7142 } else {
7143 unsigned SelectPred = MI->getOperand(4).getImm();
7144 BuildMI(BB, dl, TII->get(PPC::BCC))
7145 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
7146 }
Dan Gohman34396292010-07-06 20:24:04 +00007147
Evan Cheng32e376f2008-07-12 02:23:19 +00007148 // copy0MBB:
7149 // %FalseValue = ...
7150 // # fallthrough to sinkMBB
7151 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007152
Evan Cheng32e376f2008-07-12 02:23:19 +00007153 // Update machine-CFG edges
7154 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007155
Evan Cheng32e376f2008-07-12 02:23:19 +00007156 // sinkMBB:
7157 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7158 // ...
7159 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007160 BuildMI(*BB, BB->begin(), dl,
7161 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00007162 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
7163 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Hal Finkelbbdee932014-12-02 22:01:00 +00007164 } else if (MI->getOpcode() == PPC::ReadTB) {
7165 // To read the 64-bit time-base register on a 32-bit target, we read the
7166 // two halves. Should the counter have wrapped while it was being read, we
7167 // need to try again.
7168 // ...
7169 // readLoop:
7170 // mfspr Rx,TBU # load from TBU
7171 // mfspr Ry,TB # load from TB
7172 // mfspr Rz,TBU # load from TBU
7173 // cmpw crX,Rx,Rz # check if ‘old’=’new’
7174 // bne readLoop # branch if they're not equal
7175 // ...
7176
7177 MachineBasicBlock *readMBB = F->CreateMachineBasicBlock(LLVM_BB);
7178 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
7179 DebugLoc dl = MI->getDebugLoc();
7180 F->insert(It, readMBB);
7181 F->insert(It, sinkMBB);
7182
7183 // Transfer the remainder of BB and its successor edges to sinkMBB.
7184 sinkMBB->splice(sinkMBB->begin(), BB,
7185 std::next(MachineBasicBlock::iterator(MI)), BB->end());
7186 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7187
7188 BB->addSuccessor(readMBB);
7189 BB = readMBB;
7190
7191 MachineRegisterInfo &RegInfo = F->getRegInfo();
7192 unsigned ReadAgainReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
7193 unsigned LoReg = MI->getOperand(0).getReg();
7194 unsigned HiReg = MI->getOperand(1).getReg();
7195
7196 BuildMI(BB, dl, TII->get(PPC::MFSPR), HiReg).addImm(269);
7197 BuildMI(BB, dl, TII->get(PPC::MFSPR), LoReg).addImm(268);
7198 BuildMI(BB, dl, TII->get(PPC::MFSPR), ReadAgainReg).addImm(269);
7199
7200 unsigned CmpReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
7201
7202 BuildMI(BB, dl, TII->get(PPC::CMPW), CmpReg)
7203 .addReg(HiReg).addReg(ReadAgainReg);
7204 BuildMI(BB, dl, TII->get(PPC::BCC))
7205 .addImm(PPC::PRED_NE).addReg(CmpReg).addMBB(readMBB);
7206
7207 BB->addSuccessor(readMBB);
7208 BB->addSuccessor(sinkMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007209 }
Dale Johannesena32affb2008-08-28 17:53:09 +00007210 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
7211 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
7212 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
7213 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007214 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
7215 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
7216 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
7217 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007218
7219 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
7220 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
7221 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
7222 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007223 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
7224 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
7225 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
7226 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007227
7228 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
7229 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
7230 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
7231 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007232 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
7233 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
7234 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
7235 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007236
7237 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
7238 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
7239 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
7240 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007241 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
7242 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
7243 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
7244 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007245
7246 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00007247 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::NAND);
Dale Johannesena32affb2008-08-28 17:53:09 +00007248 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00007249 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007250 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00007251 BB = EmitAtomicBinary(MI, BB, false, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007252 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00007253 BB = EmitAtomicBinary(MI, BB, true, PPC::NAND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007254
7255 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
7256 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
7257 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
7258 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007259 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
7260 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
7261 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
7262 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007263
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007264 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
7265 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
7266 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
7267 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
7268 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
7269 BB = EmitAtomicBinary(MI, BB, false, 0);
7270 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
7271 BB = EmitAtomicBinary(MI, BB, true, 0);
7272
Evan Cheng32e376f2008-07-12 02:23:19 +00007273 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
7274 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
7275 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
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();
Evan Cheng32e376f2008-07-12 02:23:19 +00007283
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007284 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
7285 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
7286 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007287 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007288 F->insert(It, loop1MBB);
7289 F->insert(It, loop2MBB);
7290 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007291 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);
Evan Cheng32e376f2008-07-12 02:23:19 +00007295
7296 // thisMBB:
7297 // ...
7298 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007299 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007300
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007301 // loop1MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00007302 // l[wd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007303 // cmp[wd] dest, oldval
7304 // bne- midMBB
7305 // loop2MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00007306 // st[wd]cx. newval, ptr
7307 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007308 // b exitBB
7309 // midMBB:
7310 // st[wd]cx. dest, ptr
7311 // exitBB:
7312 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007313 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00007314 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007315 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00007316 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007317 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007318 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
7319 BB->addSuccessor(loop2MBB);
7320 BB->addSuccessor(midMBB);
7321
7322 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007323 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng32e376f2008-07-12 02:23:19 +00007324 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007325 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007326 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007327 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007328 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007329 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007330
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007331 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007332 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007333 .addReg(dest).addReg(ptrA).addReg(ptrB);
7334 BB->addSuccessor(exitMBB);
7335
Evan Cheng32e376f2008-07-12 02:23:19 +00007336 // exitMBB:
7337 // ...
7338 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00007339 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
7340 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
7341 // We must use 64-bit registers for addresses when targeting 64-bit,
7342 // since we're actually doing arithmetic on them. Other registers
7343 // can be 32-bit.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007344 bool is64bit = Subtarget.isPPC64();
Dale Johannesen340d2642008-08-30 00:08:53 +00007345 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
7346
7347 unsigned dest = MI->getOperand(0).getReg();
7348 unsigned ptrA = MI->getOperand(1).getReg();
7349 unsigned ptrB = MI->getOperand(2).getReg();
7350 unsigned oldval = MI->getOperand(3).getReg();
7351 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00007352 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00007353
7354 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
7355 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
7356 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
7357 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
7358 F->insert(It, loop1MBB);
7359 F->insert(It, loop2MBB);
7360 F->insert(It, midMBB);
7361 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007362 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007363 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007364 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00007365
7366 MachineRegisterInfo &RegInfo = F->getRegInfo();
Craig Topper61e88f42014-11-21 05:58:21 +00007367 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass
7368 : &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00007369 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
7370 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
7371 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
7372 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
7373 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
7374 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
7375 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
7376 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
7377 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
7378 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
7379 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
7380 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
7381 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
7382 unsigned Ptr1Reg;
7383 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00007384 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00007385 // thisMBB:
7386 // ...
7387 // fallthrough --> loopMBB
7388 BB->addSuccessor(loop1MBB);
7389
7390 // The 4-byte load must be aligned, while a char or short may be
7391 // anywhere in the word. Hence all this nasty bookkeeping code.
7392 // add ptr1, ptrA, ptrB [copy if ptrA==0]
7393 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00007394 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00007395 // rlwinm ptr, ptr1, 0, 0, 29
7396 // slw newval2, newval, shift
7397 // slw oldval2, oldval,shift
7398 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
7399 // slw mask, mask2, shift
7400 // and newval3, newval2, mask
7401 // and oldval3, oldval2, mask
7402 // loop1MBB:
7403 // lwarx tmpDest, ptr
7404 // and tmp, tmpDest, mask
7405 // cmpw tmp, oldval3
7406 // bne- midMBB
7407 // loop2MBB:
7408 // andc tmp2, tmpDest, mask
7409 // or tmp4, tmp2, newval3
7410 // stwcx. tmp4, ptr
7411 // bne- loop1MBB
7412 // b exitBB
7413 // midMBB:
7414 // stwcx. tmpDest, ptr
7415 // exitBB:
7416 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007417 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00007418 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007419 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007420 .addReg(ptrA).addReg(ptrB);
7421 } else {
7422 Ptr1Reg = ptrB;
7423 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00007424 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007425 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007426 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007427 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
7428 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00007429 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007430 .addReg(Ptr1Reg).addImm(0).addImm(61);
7431 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00007432 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007433 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007434 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007435 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007436 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007437 .addReg(oldval).addReg(ShiftReg);
7438 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00007439 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00007440 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00007441 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
7442 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
7443 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00007444 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00007445 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007446 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007447 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007448 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007449 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007450 .addReg(OldVal2Reg).addReg(MaskReg);
7451
7452 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007453 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007454 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007455 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
7456 .addReg(TmpDestReg).addReg(MaskReg);
7457 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00007458 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007459 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00007460 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
7461 BB->addSuccessor(loop2MBB);
7462 BB->addSuccessor(midMBB);
7463
7464 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007465 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
7466 .addReg(TmpDestReg).addReg(MaskReg);
7467 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
7468 .addReg(Tmp2Reg).addReg(NewVal3Reg);
7469 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007470 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007471 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00007472 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007473 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00007474 BB->addSuccessor(loop1MBB);
7475 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007476
Dale Johannesen340d2642008-08-30 00:08:53 +00007477 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007478 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007479 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00007480 BB->addSuccessor(exitMBB);
7481
7482 // exitMBB:
7483 // ...
7484 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00007485 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
7486 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00007487 } else if (MI->getOpcode() == PPC::FADDrtz) {
7488 // This pseudo performs an FADD with rounding mode temporarily forced
7489 // to round-to-zero. We emit this via custom inserter since the FPSCR
7490 // is not modeled at the SelectionDAG level.
7491 unsigned Dest = MI->getOperand(0).getReg();
7492 unsigned Src1 = MI->getOperand(1).getReg();
7493 unsigned Src2 = MI->getOperand(2).getReg();
7494 DebugLoc dl = MI->getDebugLoc();
7495
7496 MachineRegisterInfo &RegInfo = F->getRegInfo();
7497 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
7498
7499 // Save FPSCR value.
7500 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
7501
7502 // Set rounding mode to round-to-zero.
7503 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
7504 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
7505
7506 // Perform addition.
7507 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
7508
7509 // Restore FPSCR value.
7510 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00007511 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
7512 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
7513 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
7514 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
7515 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
7516 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
7517 PPC::ANDIo8 : PPC::ANDIo;
7518 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
7519 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
7520
7521 MachineRegisterInfo &RegInfo = F->getRegInfo();
7522 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
7523 &PPC::GPRCRegClass :
7524 &PPC::G8RCRegClass);
7525
7526 DebugLoc dl = MI->getDebugLoc();
7527 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
7528 .addReg(MI->getOperand(1).getReg()).addImm(1);
7529 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
7530 MI->getOperand(0).getReg())
7531 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Dale Johannesen340d2642008-08-30 00:08:53 +00007532 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007533 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00007534 }
Chris Lattner9b577f12005-08-26 21:23:58 +00007535
Dan Gohman34396292010-07-06 20:24:04 +00007536 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00007537 return BB;
7538}
7539
Chris Lattner4211ca92006-04-14 06:01:58 +00007540//===----------------------------------------------------------------------===//
7541// Target Optimization Hooks
7542//===----------------------------------------------------------------------===//
7543
Sanjay Patel8fde95c2014-09-30 20:28:48 +00007544SDValue PPCTargetLowering::getRsqrtEstimate(SDValue Operand,
7545 DAGCombinerInfo &DCI,
Sanjay Patel957efc232014-10-24 17:02:16 +00007546 unsigned &RefinementSteps,
7547 bool &UseOneConstNR) const {
Sanjay Patelbdf1e382014-09-26 23:01:47 +00007548 EVT VT = Operand.getValueType();
Sanjay Patel8fde95c2014-09-30 20:28:48 +00007549 if ((VT == MVT::f32 && Subtarget.hasFRSQRTES()) ||
7550 (VT == MVT::f64 && Subtarget.hasFRSQRTE()) ||
7551 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
7552 (VT == MVT::v2f64 && Subtarget.hasVSX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00007553 // Convergence is quadratic, so we essentially double the number of digits
Sanjay Patelbdf1e382014-09-26 23:01:47 +00007554 // correct after every iteration. For both FRE and FRSQRTE, the minimum
7555 // architected relative accuracy is 2^-5. When hasRecipPrec(), this is
7556 // 2^-14. IEEE float has 23 digits and double has 52 digits.
7557 RefinementSteps = Subtarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00007558 if (VT.getScalarType() == MVT::f64)
Sanjay Patelbdf1e382014-09-26 23:01:47 +00007559 ++RefinementSteps;
Sanjay Patel957efc232014-10-24 17:02:16 +00007560 UseOneConstNR = true;
Sanjay Patel8fde95c2014-09-30 20:28:48 +00007561 return DCI.DAG.getNode(PPCISD::FRSQRTE, SDLoc(Operand), VT, Operand);
Hal Finkel2e103312013-04-03 04:01:11 +00007562 }
Sanjay Patel8fde95c2014-09-30 20:28:48 +00007563 return SDValue();
7564}
7565
7566SDValue PPCTargetLowering::getRecipEstimate(SDValue Operand,
7567 DAGCombinerInfo &DCI,
7568 unsigned &RefinementSteps) const {
7569 EVT VT = Operand.getValueType();
7570 if ((VT == MVT::f32 && Subtarget.hasFRES()) ||
7571 (VT == MVT::f64 && Subtarget.hasFRE()) ||
7572 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
7573 (VT == MVT::v2f64 && Subtarget.hasVSX())) {
7574 // Convergence is quadratic, so we essentially double the number of digits
7575 // correct after every iteration. For both FRE and FRSQRTE, the minimum
7576 // architected relative accuracy is 2^-5. When hasRecipPrec(), this is
7577 // 2^-14. IEEE float has 23 digits and double has 52 digits.
7578 RefinementSteps = Subtarget.hasRecipPrec() ? 1 : 3;
7579 if (VT.getScalarType() == MVT::f64)
7580 ++RefinementSteps;
7581 return DCI.DAG.getNode(PPCISD::FRE, SDLoc(Operand), VT, Operand);
7582 }
7583 return SDValue();
Hal Finkel2e103312013-04-03 04:01:11 +00007584}
7585
Hal Finkel360f2132014-11-24 23:45:21 +00007586bool PPCTargetLowering::combineRepeatedFPDivisors(unsigned NumUsers) const {
7587 // Note: This functionality is used only when unsafe-fp-math is enabled, and
7588 // on cores with reciprocal estimates (which are used when unsafe-fp-math is
7589 // enabled for division), this functionality is redundant with the default
7590 // combiner logic (once the division -> reciprocal/multiply transformation
7591 // has taken place). As a result, this matters more for older cores than for
7592 // newer ones.
7593
7594 // Combine multiple FDIVs with the same divisor into multiple FMULs by the
7595 // reciprocal if there are two or more FDIVs (for embedded cores with only
7596 // one FP pipeline) for three or more FDIVs (for generic OOO cores).
7597 switch (Subtarget.getDarwinDirective()) {
7598 default:
7599 return NumUsers > 2;
7600 case PPC::DIR_440:
7601 case PPC::DIR_A2:
7602 case PPC::DIR_E500mc:
7603 case PPC::DIR_E5500:
7604 return NumUsers > 1;
7605 }
7606}
7607
Hal Finkel3604bf72014-08-01 01:02:01 +00007608static bool isConsecutiveLSLoc(SDValue Loc, EVT VT, LSBaseSDNode *Base,
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007609 unsigned Bytes, int Dist,
7610 SelectionDAG &DAG) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007611 if (VT.getSizeInBits() / 8 != Bytes)
7612 return false;
7613
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007614 SDValue BaseLoc = Base->getBasePtr();
7615 if (Loc.getOpcode() == ISD::FrameIndex) {
7616 if (BaseLoc.getOpcode() != ISD::FrameIndex)
7617 return false;
7618 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7619 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
7620 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
7621 int FS = MFI->getObjectSize(FI);
7622 int BFS = MFI->getObjectSize(BFI);
7623 if (FS != BFS || FS != (int)Bytes) return false;
7624 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
7625 }
7626
7627 // Handle X+C
7628 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
7629 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
7630 return true;
7631
7632 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Craig Topper062a2ba2014-04-25 05:30:21 +00007633 const GlobalValue *GV1 = nullptr;
7634 const GlobalValue *GV2 = nullptr;
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007635 int64_t Offset1 = 0;
7636 int64_t Offset2 = 0;
7637 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
7638 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
7639 if (isGA1 && isGA2 && GV1 == GV2)
7640 return Offset1 == (Offset2 + Dist*Bytes);
7641 return false;
7642}
7643
Hal Finkel3604bf72014-08-01 01:02:01 +00007644// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
7645// not enforce equality of the chain operands.
7646static bool isConsecutiveLS(SDNode *N, LSBaseSDNode *Base,
7647 unsigned Bytes, int Dist,
7648 SelectionDAG &DAG) {
7649 if (LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(N)) {
7650 EVT VT = LS->getMemoryVT();
7651 SDValue Loc = LS->getBasePtr();
7652 return isConsecutiveLSLoc(Loc, VT, Base, Bytes, Dist, DAG);
7653 }
7654
7655 if (N->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
7656 EVT VT;
7657 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
7658 default: return false;
7659 case Intrinsic::ppc_altivec_lvx:
7660 case Intrinsic::ppc_altivec_lvxl:
Bill Schmidt72954782014-11-12 04:19:40 +00007661 case Intrinsic::ppc_vsx_lxvw4x:
Hal Finkel3604bf72014-08-01 01:02:01 +00007662 VT = MVT::v4i32;
7663 break;
Bill Schmidt72954782014-11-12 04:19:40 +00007664 case Intrinsic::ppc_vsx_lxvd2x:
7665 VT = MVT::v2f64;
7666 break;
Hal Finkel3604bf72014-08-01 01:02:01 +00007667 case Intrinsic::ppc_altivec_lvebx:
7668 VT = MVT::i8;
7669 break;
7670 case Intrinsic::ppc_altivec_lvehx:
7671 VT = MVT::i16;
7672 break;
7673 case Intrinsic::ppc_altivec_lvewx:
7674 VT = MVT::i32;
7675 break;
7676 }
7677
7678 return isConsecutiveLSLoc(N->getOperand(2), VT, Base, Bytes, Dist, DAG);
7679 }
7680
7681 if (N->getOpcode() == ISD::INTRINSIC_VOID) {
7682 EVT VT;
7683 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
7684 default: return false;
7685 case Intrinsic::ppc_altivec_stvx:
7686 case Intrinsic::ppc_altivec_stvxl:
Bill Schmidt72954782014-11-12 04:19:40 +00007687 case Intrinsic::ppc_vsx_stxvw4x:
Hal Finkel3604bf72014-08-01 01:02:01 +00007688 VT = MVT::v4i32;
7689 break;
Bill Schmidt72954782014-11-12 04:19:40 +00007690 case Intrinsic::ppc_vsx_stxvd2x:
7691 VT = MVT::v2f64;
7692 break;
Hal Finkel3604bf72014-08-01 01:02:01 +00007693 case Intrinsic::ppc_altivec_stvebx:
7694 VT = MVT::i8;
7695 break;
7696 case Intrinsic::ppc_altivec_stvehx:
7697 VT = MVT::i16;
7698 break;
7699 case Intrinsic::ppc_altivec_stvewx:
7700 VT = MVT::i32;
7701 break;
7702 }
7703
7704 return isConsecutiveLSLoc(N->getOperand(3), VT, Base, Bytes, Dist, DAG);
7705 }
7706
7707 return false;
7708}
7709
Hal Finkel7d8a6912013-05-26 18:08:30 +00007710// Return true is there is a nearyby consecutive load to the one provided
7711// (regardless of alignment). We search up and down the chain, looking though
Matt Arsenault57e74d22014-07-29 00:02:40 +00007712// token factors and other loads (but nothing else). As a result, a true result
7713// indicates that it is safe to create a new consecutive load adjacent to the
7714// load provided.
Hal Finkel7d8a6912013-05-26 18:08:30 +00007715static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
7716 SDValue Chain = LD->getChain();
7717 EVT VT = LD->getMemoryVT();
7718
7719 SmallSet<SDNode *, 16> LoadRoots;
7720 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
7721 SmallSet<SDNode *, 16> Visited;
7722
7723 // First, search up the chain, branching to follow all token-factor operands.
7724 // If we find a consecutive load, then we're done, otherwise, record all
7725 // nodes just above the top-level loads and token factors.
7726 while (!Queue.empty()) {
7727 SDNode *ChainNext = Queue.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00007728 if (!Visited.insert(ChainNext).second)
Hal Finkel7d8a6912013-05-26 18:08:30 +00007729 continue;
7730
Hal Finkel3604bf72014-08-01 01:02:01 +00007731 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007732 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007733 return true;
7734
7735 if (!Visited.count(ChainLD->getChain().getNode()))
7736 Queue.push_back(ChainLD->getChain().getNode());
7737 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
Craig Topper66e588b2014-06-29 00:40:57 +00007738 for (const SDUse &O : ChainNext->ops())
7739 if (!Visited.count(O.getNode()))
7740 Queue.push_back(O.getNode());
Hal Finkel7d8a6912013-05-26 18:08:30 +00007741 } else
7742 LoadRoots.insert(ChainNext);
7743 }
7744
7745 // Second, search down the chain, starting from the top-level nodes recorded
7746 // in the first phase. These top-level nodes are the nodes just above all
7747 // loads and token factors. Starting with their uses, recursively look though
7748 // all loads (just the chain uses) and token factors to find a consecutive
7749 // load.
7750 Visited.clear();
7751 Queue.clear();
7752
7753 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
7754 IE = LoadRoots.end(); I != IE; ++I) {
7755 Queue.push_back(*I);
7756
7757 while (!Queue.empty()) {
7758 SDNode *LoadRoot = Queue.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00007759 if (!Visited.insert(LoadRoot).second)
Hal Finkel7d8a6912013-05-26 18:08:30 +00007760 continue;
7761
Hal Finkel3604bf72014-08-01 01:02:01 +00007762 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007763 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007764 return true;
7765
7766 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
7767 UE = LoadRoot->use_end(); UI != UE; ++UI)
Hal Finkel3604bf72014-08-01 01:02:01 +00007768 if (((isa<MemSDNode>(*UI) &&
7769 cast<MemSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
Hal Finkel7d8a6912013-05-26 18:08:30 +00007770 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
7771 Queue.push_back(*UI);
7772 }
7773 }
7774
7775 return false;
7776}
7777
Hal Finkel940ab932014-02-28 00:27:01 +00007778SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
7779 DAGCombinerInfo &DCI) const {
7780 SelectionDAG &DAG = DCI.DAG;
7781 SDLoc dl(N);
7782
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007783 assert(Subtarget.useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00007784 "Expecting to be tracking CR bits");
7785 // If we're tracking CR bits, we need to be careful that we don't have:
7786 // trunc(binary-ops(zext(x), zext(y)))
7787 // or
7788 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
7789 // such that we're unnecessarily moving things into GPRs when it would be
7790 // better to keep them in CR bits.
7791
7792 // Note that trunc here can be an actual i1 trunc, or can be the effective
7793 // truncation that comes from a setcc or select_cc.
7794 if (N->getOpcode() == ISD::TRUNCATE &&
7795 N->getValueType(0) != MVT::i1)
7796 return SDValue();
7797
7798 if (N->getOperand(0).getValueType() != MVT::i32 &&
7799 N->getOperand(0).getValueType() != MVT::i64)
7800 return SDValue();
7801
7802 if (N->getOpcode() == ISD::SETCC ||
7803 N->getOpcode() == ISD::SELECT_CC) {
7804 // If we're looking at a comparison, then we need to make sure that the
7805 // high bits (all except for the first) don't matter the result.
7806 ISD::CondCode CC =
7807 cast<CondCodeSDNode>(N->getOperand(
7808 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
7809 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
7810
7811 if (ISD::isSignedIntSetCC(CC)) {
7812 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
7813 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
7814 return SDValue();
7815 } else if (ISD::isUnsignedIntSetCC(CC)) {
7816 if (!DAG.MaskedValueIsZero(N->getOperand(0),
7817 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
7818 !DAG.MaskedValueIsZero(N->getOperand(1),
7819 APInt::getHighBitsSet(OpBits, OpBits-1)))
7820 return SDValue();
7821 } else {
7822 // This is neither a signed nor an unsigned comparison, just make sure
7823 // that the high bits are equal.
7824 APInt Op1Zero, Op1One;
7825 APInt Op2Zero, Op2One;
Jay Foada0653a32014-05-14 21:14:37 +00007826 DAG.computeKnownBits(N->getOperand(0), Op1Zero, Op1One);
7827 DAG.computeKnownBits(N->getOperand(1), Op2Zero, Op2One);
Hal Finkel940ab932014-02-28 00:27:01 +00007828
7829 // We don't really care about what is known about the first bit (if
7830 // anything), so clear it in all masks prior to comparing them.
7831 Op1Zero.clearBit(0); Op1One.clearBit(0);
7832 Op2Zero.clearBit(0); Op2One.clearBit(0);
7833
7834 if (Op1Zero != Op2Zero || Op1One != Op2One)
7835 return SDValue();
7836 }
7837 }
7838
7839 // We now know that the higher-order bits are irrelevant, we just need to
7840 // make sure that all of the intermediate operations are bit operations, and
7841 // all inputs are extensions.
7842 if (N->getOperand(0).getOpcode() != ISD::AND &&
7843 N->getOperand(0).getOpcode() != ISD::OR &&
7844 N->getOperand(0).getOpcode() != ISD::XOR &&
7845 N->getOperand(0).getOpcode() != ISD::SELECT &&
7846 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
7847 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
7848 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
7849 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
7850 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
7851 return SDValue();
7852
7853 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
7854 N->getOperand(1).getOpcode() != ISD::AND &&
7855 N->getOperand(1).getOpcode() != ISD::OR &&
7856 N->getOperand(1).getOpcode() != ISD::XOR &&
7857 N->getOperand(1).getOpcode() != ISD::SELECT &&
7858 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
7859 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
7860 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
7861 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
7862 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
7863 return SDValue();
7864
7865 SmallVector<SDValue, 4> Inputs;
7866 SmallVector<SDValue, 8> BinOps, PromOps;
7867 SmallPtrSet<SDNode *, 16> Visited;
7868
7869 for (unsigned i = 0; i < 2; ++i) {
7870 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7871 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7872 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7873 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7874 isa<ConstantSDNode>(N->getOperand(i)))
7875 Inputs.push_back(N->getOperand(i));
7876 else
7877 BinOps.push_back(N->getOperand(i));
7878
7879 if (N->getOpcode() == ISD::TRUNCATE)
7880 break;
7881 }
7882
7883 // Visit all inputs, collect all binary operations (and, or, xor and
7884 // select) that are all fed by extensions.
7885 while (!BinOps.empty()) {
7886 SDValue BinOp = BinOps.back();
7887 BinOps.pop_back();
7888
David Blaikie70573dc2014-11-19 07:49:26 +00007889 if (!Visited.insert(BinOp.getNode()).second)
Hal Finkel940ab932014-02-28 00:27:01 +00007890 continue;
7891
7892 PromOps.push_back(BinOp);
7893
7894 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7895 // The condition of the select is not promoted.
7896 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7897 continue;
7898 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7899 continue;
7900
7901 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7902 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7903 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7904 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7905 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7906 Inputs.push_back(BinOp.getOperand(i));
7907 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7908 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7909 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7910 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7911 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
7912 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7913 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7914 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7915 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
7916 BinOps.push_back(BinOp.getOperand(i));
7917 } else {
7918 // We have an input that is not an extension or another binary
7919 // operation; we'll abort this transformation.
7920 return SDValue();
7921 }
7922 }
7923 }
7924
7925 // Make sure that this is a self-contained cluster of operations (which
7926 // is not quite the same thing as saying that everything has only one
7927 // use).
7928 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7929 if (isa<ConstantSDNode>(Inputs[i]))
7930 continue;
7931
7932 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7933 UE = Inputs[i].getNode()->use_end();
7934 UI != UE; ++UI) {
7935 SDNode *User = *UI;
7936 if (User != N && !Visited.count(User))
7937 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007938
7939 // Make sure that we're not going to promote the non-output-value
7940 // operand(s) or SELECT or SELECT_CC.
7941 // FIXME: Although we could sometimes handle this, and it does occur in
7942 // practice that one of the condition inputs to the select is also one of
7943 // the outputs, we currently can't deal with this.
7944 if (User->getOpcode() == ISD::SELECT) {
7945 if (User->getOperand(0) == Inputs[i])
7946 return SDValue();
7947 } else if (User->getOpcode() == ISD::SELECT_CC) {
7948 if (User->getOperand(0) == Inputs[i] ||
7949 User->getOperand(1) == Inputs[i])
7950 return SDValue();
7951 }
Hal Finkel940ab932014-02-28 00:27:01 +00007952 }
7953 }
7954
7955 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7956 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7957 UE = PromOps[i].getNode()->use_end();
7958 UI != UE; ++UI) {
7959 SDNode *User = *UI;
7960 if (User != N && !Visited.count(User))
7961 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007962
7963 // Make sure that we're not going to promote the non-output-value
7964 // operand(s) or SELECT or SELECT_CC.
7965 // FIXME: Although we could sometimes handle this, and it does occur in
7966 // practice that one of the condition inputs to the select is also one of
7967 // the outputs, we currently can't deal with this.
7968 if (User->getOpcode() == ISD::SELECT) {
7969 if (User->getOperand(0) == PromOps[i])
7970 return SDValue();
7971 } else if (User->getOpcode() == ISD::SELECT_CC) {
7972 if (User->getOperand(0) == PromOps[i] ||
7973 User->getOperand(1) == PromOps[i])
7974 return SDValue();
7975 }
Hal Finkel940ab932014-02-28 00:27:01 +00007976 }
7977 }
7978
7979 // Replace all inputs with the extension operand.
7980 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7981 // Constants may have users outside the cluster of to-be-promoted nodes,
7982 // and so we need to replace those as we do the promotions.
7983 if (isa<ConstantSDNode>(Inputs[i]))
7984 continue;
7985 else
7986 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
7987 }
7988
7989 // Replace all operations (these are all the same, but have a different
7990 // (i1) return type). DAG.getNode will validate that the types of
7991 // a binary operator match, so go through the list in reverse so that
7992 // we've likely promoted both operands first. Any intermediate truncations or
7993 // extensions disappear.
7994 while (!PromOps.empty()) {
7995 SDValue PromOp = PromOps.back();
7996 PromOps.pop_back();
7997
7998 if (PromOp.getOpcode() == ISD::TRUNCATE ||
7999 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
8000 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
8001 PromOp.getOpcode() == ISD::ANY_EXTEND) {
8002 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
8003 PromOp.getOperand(0).getValueType() != MVT::i1) {
8004 // The operand is not yet ready (see comment below).
8005 PromOps.insert(PromOps.begin(), PromOp);
8006 continue;
8007 }
8008
8009 SDValue RepValue = PromOp.getOperand(0);
8010 if (isa<ConstantSDNode>(RepValue))
8011 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
8012
8013 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
8014 continue;
8015 }
8016
8017 unsigned C;
8018 switch (PromOp.getOpcode()) {
8019 default: C = 0; break;
8020 case ISD::SELECT: C = 1; break;
8021 case ISD::SELECT_CC: C = 2; break;
8022 }
8023
8024 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
8025 PromOp.getOperand(C).getValueType() != MVT::i1) ||
8026 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
8027 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
8028 // The to-be-promoted operands of this node have not yet been
8029 // promoted (this should be rare because we're going through the
8030 // list backward, but if one of the operands has several users in
8031 // this cluster of to-be-promoted nodes, it is possible).
8032 PromOps.insert(PromOps.begin(), PromOp);
8033 continue;
8034 }
8035
8036 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
8037 PromOp.getNode()->op_end());
8038
8039 // If there are any constant inputs, make sure they're replaced now.
8040 for (unsigned i = 0; i < 2; ++i)
8041 if (isa<ConstantSDNode>(Ops[C+i]))
8042 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
8043
8044 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00008045 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00008046 }
8047
8048 // Now we're left with the initial truncation itself.
8049 if (N->getOpcode() == ISD::TRUNCATE)
8050 return N->getOperand(0);
8051
8052 // Otherwise, this is a comparison. The operands to be compared have just
8053 // changed type (to i1), but everything else is the same.
8054 return SDValue(N, 0);
8055}
8056
8057SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
8058 DAGCombinerInfo &DCI) const {
8059 SelectionDAG &DAG = DCI.DAG;
8060 SDLoc dl(N);
8061
Hal Finkel940ab932014-02-28 00:27:01 +00008062 // If we're tracking CR bits, we need to be careful that we don't have:
8063 // zext(binary-ops(trunc(x), trunc(y)))
8064 // or
8065 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
8066 // such that we're unnecessarily moving things into CR bits that can more
8067 // efficiently stay in GPRs. Note that if we're not certain that the high
8068 // bits are set as required by the final extension, we still may need to do
8069 // some masking to get the proper behavior.
8070
Hal Finkel46043ed2014-03-01 21:36:57 +00008071 // This same functionality is important on PPC64 when dealing with
8072 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
8073 // the return values of functions. Because it is so similar, it is handled
8074 // here as well.
8075
Hal Finkel940ab932014-02-28 00:27:01 +00008076 if (N->getValueType(0) != MVT::i32 &&
8077 N->getValueType(0) != MVT::i64)
8078 return SDValue();
8079
Hal Finkel46043ed2014-03-01 21:36:57 +00008080 if (!((N->getOperand(0).getValueType() == MVT::i1 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008081 Subtarget.useCRBits()) ||
Hal Finkel46043ed2014-03-01 21:36:57 +00008082 (N->getOperand(0).getValueType() == MVT::i32 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008083 Subtarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +00008084 return SDValue();
8085
8086 if (N->getOperand(0).getOpcode() != ISD::AND &&
8087 N->getOperand(0).getOpcode() != ISD::OR &&
8088 N->getOperand(0).getOpcode() != ISD::XOR &&
8089 N->getOperand(0).getOpcode() != ISD::SELECT &&
8090 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
8091 return SDValue();
8092
8093 SmallVector<SDValue, 4> Inputs;
8094 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
8095 SmallPtrSet<SDNode *, 16> Visited;
8096
8097 // Visit all inputs, collect all binary operations (and, or, xor and
8098 // select) that are all fed by truncations.
8099 while (!BinOps.empty()) {
8100 SDValue BinOp = BinOps.back();
8101 BinOps.pop_back();
8102
David Blaikie70573dc2014-11-19 07:49:26 +00008103 if (!Visited.insert(BinOp.getNode()).second)
Hal Finkel940ab932014-02-28 00:27:01 +00008104 continue;
8105
8106 PromOps.push_back(BinOp);
8107
8108 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
8109 // The condition of the select is not promoted.
8110 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
8111 continue;
8112 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
8113 continue;
8114
8115 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
8116 isa<ConstantSDNode>(BinOp.getOperand(i))) {
8117 Inputs.push_back(BinOp.getOperand(i));
8118 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
8119 BinOp.getOperand(i).getOpcode() == ISD::OR ||
8120 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
8121 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
8122 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
8123 BinOps.push_back(BinOp.getOperand(i));
8124 } else {
8125 // We have an input that is not a truncation or another binary
8126 // operation; we'll abort this transformation.
8127 return SDValue();
8128 }
8129 }
8130 }
8131
8132 // Make sure that this is a self-contained cluster of operations (which
8133 // is not quite the same thing as saying that everything has only one
8134 // use).
8135 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
8136 if (isa<ConstantSDNode>(Inputs[i]))
8137 continue;
8138
8139 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
8140 UE = Inputs[i].getNode()->use_end();
8141 UI != UE; ++UI) {
8142 SDNode *User = *UI;
8143 if (User != N && !Visited.count(User))
8144 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00008145
8146 // Make sure that we're not going to promote the non-output-value
8147 // operand(s) or SELECT or SELECT_CC.
8148 // FIXME: Although we could sometimes handle this, and it does occur in
8149 // practice that one of the condition inputs to the select is also one of
8150 // the outputs, we currently can't deal with this.
8151 if (User->getOpcode() == ISD::SELECT) {
8152 if (User->getOperand(0) == Inputs[i])
8153 return SDValue();
8154 } else if (User->getOpcode() == ISD::SELECT_CC) {
8155 if (User->getOperand(0) == Inputs[i] ||
8156 User->getOperand(1) == Inputs[i])
8157 return SDValue();
8158 }
Hal Finkel940ab932014-02-28 00:27:01 +00008159 }
8160 }
8161
8162 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
8163 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
8164 UE = PromOps[i].getNode()->use_end();
8165 UI != UE; ++UI) {
8166 SDNode *User = *UI;
8167 if (User != N && !Visited.count(User))
8168 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00008169
8170 // Make sure that we're not going to promote the non-output-value
8171 // operand(s) or SELECT or SELECT_CC.
8172 // FIXME: Although we could sometimes handle this, and it does occur in
8173 // practice that one of the condition inputs to the select is also one of
8174 // the outputs, we currently can't deal with this.
8175 if (User->getOpcode() == ISD::SELECT) {
8176 if (User->getOperand(0) == PromOps[i])
8177 return SDValue();
8178 } else if (User->getOpcode() == ISD::SELECT_CC) {
8179 if (User->getOperand(0) == PromOps[i] ||
8180 User->getOperand(1) == PromOps[i])
8181 return SDValue();
8182 }
Hal Finkel940ab932014-02-28 00:27:01 +00008183 }
8184 }
8185
Hal Finkel46043ed2014-03-01 21:36:57 +00008186 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +00008187 bool ReallyNeedsExt = false;
8188 if (N->getOpcode() != ISD::ANY_EXTEND) {
8189 // If all of the inputs are not already sign/zero extended, then
8190 // we'll still need to do that at the end.
8191 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
8192 if (isa<ConstantSDNode>(Inputs[i]))
8193 continue;
8194
8195 unsigned OpBits =
8196 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +00008197 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
8198
Hal Finkel940ab932014-02-28 00:27:01 +00008199 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
8200 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00008201 APInt::getHighBitsSet(OpBits,
8202 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +00008203 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +00008204 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
8205 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +00008206 ReallyNeedsExt = true;
8207 break;
8208 }
8209 }
8210 }
8211
8212 // Replace all inputs, either with the truncation operand, or a
8213 // truncation or extension to the final output type.
8214 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
8215 // Constant inputs need to be replaced with the to-be-promoted nodes that
8216 // use them because they might have users outside of the cluster of
8217 // promoted nodes.
8218 if (isa<ConstantSDNode>(Inputs[i]))
8219 continue;
8220
8221 SDValue InSrc = Inputs[i].getOperand(0);
8222 if (Inputs[i].getValueType() == N->getValueType(0))
8223 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
8224 else if (N->getOpcode() == ISD::SIGN_EXTEND)
8225 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
8226 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
8227 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8228 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
8229 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
8230 else
8231 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
8232 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
8233 }
8234
8235 // Replace all operations (these are all the same, but have a different
8236 // (promoted) return type). DAG.getNode will validate that the types of
8237 // a binary operator match, so go through the list in reverse so that
8238 // we've likely promoted both operands first.
8239 while (!PromOps.empty()) {
8240 SDValue PromOp = PromOps.back();
8241 PromOps.pop_back();
8242
8243 unsigned C;
8244 switch (PromOp.getOpcode()) {
8245 default: C = 0; break;
8246 case ISD::SELECT: C = 1; break;
8247 case ISD::SELECT_CC: C = 2; break;
8248 }
8249
8250 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
8251 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
8252 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
8253 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
8254 // The to-be-promoted operands of this node have not yet been
8255 // promoted (this should be rare because we're going through the
8256 // list backward, but if one of the operands has several users in
8257 // this cluster of to-be-promoted nodes, it is possible).
8258 PromOps.insert(PromOps.begin(), PromOp);
8259 continue;
8260 }
8261
8262 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
8263 PromOp.getNode()->op_end());
8264
8265 // If this node has constant inputs, then they'll need to be promoted here.
8266 for (unsigned i = 0; i < 2; ++i) {
8267 if (!isa<ConstantSDNode>(Ops[C+i]))
8268 continue;
8269 if (Ops[C+i].getValueType() == N->getValueType(0))
8270 continue;
8271
8272 if (N->getOpcode() == ISD::SIGN_EXTEND)
8273 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
8274 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8275 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
8276 else
8277 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
8278 }
8279
8280 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00008281 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00008282 }
8283
8284 // Now we're left with the initial extension itself.
8285 if (!ReallyNeedsExt)
8286 return N->getOperand(0);
8287
Hal Finkel46043ed2014-03-01 21:36:57 +00008288 // To zero extend, just mask off everything except for the first bit (in the
8289 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +00008290 if (N->getOpcode() == ISD::ZERO_EXTEND)
8291 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00008292 DAG.getConstant(APInt::getLowBitsSet(
8293 N->getValueSizeInBits(0), PromBits),
8294 N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +00008295
8296 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
8297 "Invalid extension type");
8298 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0));
8299 SDValue ShiftCst =
Hal Finkel46043ed2014-03-01 21:36:57 +00008300 DAG.getConstant(N->getValueSizeInBits(0)-PromBits, ShiftAmountTy);
Hal Finkel940ab932014-02-28 00:27:01 +00008301 return DAG.getNode(ISD::SRA, dl, N->getValueType(0),
8302 DAG.getNode(ISD::SHL, dl, N->getValueType(0),
8303 N->getOperand(0), ShiftCst), ShiftCst);
8304}
8305
Bill Schmidtfae5d712014-12-09 16:35:51 +00008306// expandVSXLoadForLE - Convert VSX loads (which may be intrinsics for
8307// builtins) into loads with swaps.
8308SDValue PPCTargetLowering::expandVSXLoadForLE(SDNode *N,
8309 DAGCombinerInfo &DCI) const {
8310 SelectionDAG &DAG = DCI.DAG;
8311 SDLoc dl(N);
8312 SDValue Chain;
8313 SDValue Base;
8314 MachineMemOperand *MMO;
8315
8316 switch (N->getOpcode()) {
8317 default:
8318 llvm_unreachable("Unexpected opcode for little endian VSX load");
8319 case ISD::LOAD: {
8320 LoadSDNode *LD = cast<LoadSDNode>(N);
8321 Chain = LD->getChain();
8322 Base = LD->getBasePtr();
8323 MMO = LD->getMemOperand();
8324 // If the MMO suggests this isn't a load of a full vector, leave
8325 // things alone. For a built-in, we have to make the change for
8326 // correctness, so if there is a size problem that will be a bug.
8327 if (MMO->getSize() < 16)
8328 return SDValue();
8329 break;
8330 }
8331 case ISD::INTRINSIC_W_CHAIN: {
8332 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
8333 Chain = Intrin->getChain();
8334 Base = Intrin->getBasePtr();
8335 MMO = Intrin->getMemOperand();
8336 break;
8337 }
8338 }
8339
8340 MVT VecTy = N->getValueType(0).getSimpleVT();
8341 SDValue LoadOps[] = { Chain, Base };
8342 SDValue Load = DAG.getMemIntrinsicNode(PPCISD::LXVD2X, dl,
8343 DAG.getVTList(VecTy, MVT::Other),
8344 LoadOps, VecTy, MMO);
8345 DCI.AddToWorklist(Load.getNode());
8346 Chain = Load.getValue(1);
8347 SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl,
8348 DAG.getVTList(VecTy, MVT::Other), Chain, Load);
8349 DCI.AddToWorklist(Swap.getNode());
8350 return Swap;
8351}
8352
8353// expandVSXStoreForLE - Convert VSX stores (which may be intrinsics for
8354// builtins) into stores with swaps.
8355SDValue PPCTargetLowering::expandVSXStoreForLE(SDNode *N,
8356 DAGCombinerInfo &DCI) const {
8357 SelectionDAG &DAG = DCI.DAG;
8358 SDLoc dl(N);
8359 SDValue Chain;
8360 SDValue Base;
8361 unsigned SrcOpnd;
8362 MachineMemOperand *MMO;
8363
8364 switch (N->getOpcode()) {
8365 default:
8366 llvm_unreachable("Unexpected opcode for little endian VSX store");
8367 case ISD::STORE: {
8368 StoreSDNode *ST = cast<StoreSDNode>(N);
8369 Chain = ST->getChain();
8370 Base = ST->getBasePtr();
8371 MMO = ST->getMemOperand();
8372 SrcOpnd = 1;
8373 // If the MMO suggests this isn't a store of a full vector, leave
8374 // things alone. For a built-in, we have to make the change for
8375 // correctness, so if there is a size problem that will be a bug.
8376 if (MMO->getSize() < 16)
8377 return SDValue();
8378 break;
8379 }
8380 case ISD::INTRINSIC_VOID: {
8381 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
8382 Chain = Intrin->getChain();
8383 // Intrin->getBasePtr() oddly does not get what we want.
8384 Base = Intrin->getOperand(3);
8385 MMO = Intrin->getMemOperand();
8386 SrcOpnd = 2;
8387 break;
8388 }
8389 }
8390
8391 SDValue Src = N->getOperand(SrcOpnd);
8392 MVT VecTy = Src.getValueType().getSimpleVT();
8393 SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl,
8394 DAG.getVTList(VecTy, MVT::Other), Chain, Src);
8395 DCI.AddToWorklist(Swap.getNode());
8396 Chain = Swap.getValue(1);
8397 SDValue StoreOps[] = { Chain, Swap, Base };
8398 SDValue Store = DAG.getMemIntrinsicNode(PPCISD::STXVD2X, dl,
8399 DAG.getVTList(MVT::Other),
8400 StoreOps, VecTy, MMO);
8401 DCI.AddToWorklist(Store.getNode());
8402 return Store;
8403}
8404
Duncan Sandsdc2dac12008-11-24 14:53:14 +00008405SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
8406 DAGCombinerInfo &DCI) const {
Dan Gohman57c732b2010-04-21 01:34:56 +00008407 const TargetMachine &TM = getTargetMachine();
Chris Lattnerf4184352006-03-01 04:57:39 +00008408 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008409 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00008410 switch (N->getOpcode()) {
8411 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00008412 case PPCISD::SHL:
8413 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00008414 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00008415 return N->getOperand(0);
8416 }
8417 break;
8418 case PPCISD::SRL:
8419 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00008420 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00008421 return N->getOperand(0);
8422 }
8423 break;
8424 case PPCISD::SRA:
8425 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00008426 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00008427 C->isAllOnesValue()) // -1 >>s V -> -1.
8428 return N->getOperand(0);
8429 }
8430 break;
Hal Finkel940ab932014-02-28 00:27:01 +00008431 case ISD::SIGN_EXTEND:
8432 case ISD::ZERO_EXTEND:
8433 case ISD::ANY_EXTEND:
8434 return DAGCombineExtBoolTrunc(N, DCI);
8435 case ISD::TRUNCATE:
8436 case ISD::SETCC:
8437 case ISD::SELECT_CC:
8438 return DAGCombineTruncBoolExt(N, DCI);
Chris Lattnerf4184352006-03-01 04:57:39 +00008439 case ISD::SINT_TO_FP:
Chris Lattnera35f3062006-06-16 17:34:12 +00008440 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattner4a66d692006-03-22 05:30:33 +00008441 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
8442 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
8443 // We allow the src/dst to be either f32/f64, but the intermediate
8444 // type must be i64.
Owen Anderson9f944592009-08-11 20:47:22 +00008445 if (N->getOperand(0).getValueType() == MVT::i64 &&
8446 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008447 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00008448 if (Val.getValueType() == MVT::f32) {
8449 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008450 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00008451 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008452
Owen Anderson9f944592009-08-11 20:47:22 +00008453 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008454 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00008455 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008456 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00008457 if (N->getValueType(0) == MVT::f32) {
8458 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner72733e52008-01-17 07:00:52 +00008459 DAG.getIntPtrConstant(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00008460 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00008461 }
8462 return Val;
Owen Anderson9f944592009-08-11 20:47:22 +00008463 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattner4a66d692006-03-22 05:30:33 +00008464 // If the intermediate type is i32, we can avoid the load/store here
8465 // too.
Chris Lattnerf4184352006-03-01 04:57:39 +00008466 }
Chris Lattnerf4184352006-03-01 04:57:39 +00008467 }
8468 }
8469 break;
Bill Schmidtfae5d712014-12-09 16:35:51 +00008470 case ISD::STORE: {
Chris Lattner27f53452006-03-01 05:50:56 +00008471 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
8472 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnerf5b46f72008-01-18 16:54:56 +00008473 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00008474 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00008475 N->getOperand(1).getValueType() == MVT::i32 &&
8476 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008477 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00008478 if (Val.getValueType() == MVT::f32) {
8479 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008480 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008481 }
Owen Anderson9f944592009-08-11 20:47:22 +00008482 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008483 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008484
Hal Finkel60c75102013-04-01 15:37:53 +00008485 SDValue Ops[] = {
8486 N->getOperand(0), Val, N->getOperand(2),
8487 DAG.getValueType(N->getOperand(1).getValueType())
8488 };
8489
8490 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00008491 DAG.getVTList(MVT::Other), Ops,
Hal Finkel60c75102013-04-01 15:37:53 +00008492 cast<StoreSDNode>(N)->getMemoryVT(),
8493 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00008494 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008495 return Val;
8496 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008497
Chris Lattnera7976d32006-07-10 20:56:58 +00008498 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00008499 if (cast<StoreSDNode>(N)->isUnindexed() &&
8500 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00008501 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00008502 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00008503 N->getOperand(1).getValueType() == MVT::i16 ||
8504 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008505 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008506 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008507 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008508 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00008509 if (BSwapOp.getValueType() == MVT::i16)
8510 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00008511
Dan Gohman48b185d2009-09-25 20:36:54 +00008512 SDValue Ops[] = {
8513 N->getOperand(0), BSwapOp, N->getOperand(2),
8514 DAG.getValueType(N->getOperand(1).getValueType())
8515 };
8516 return
8517 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00008518 Ops, cast<StoreSDNode>(N)->getMemoryVT(),
Dan Gohman48b185d2009-09-25 20:36:54 +00008519 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008520 }
Bill Schmidtfae5d712014-12-09 16:35:51 +00008521
8522 // For little endian, VSX stores require generating xxswapd/lxvd2x.
8523 EVT VT = N->getOperand(1).getValueType();
8524 if (VT.isSimple()) {
8525 MVT StoreVT = VT.getSimpleVT();
8526 if (TM.getSubtarget<PPCSubtarget>().hasVSX() &&
8527 TM.getSubtarget<PPCSubtarget>().isLittleEndian() &&
8528 (StoreVT == MVT::v2f64 || StoreVT == MVT::v2i64 ||
8529 StoreVT == MVT::v4f32 || StoreVT == MVT::v4i32))
8530 return expandVSXStoreForLE(N, DCI);
8531 }
Chris Lattnera7976d32006-07-10 20:56:58 +00008532 break;
Bill Schmidtfae5d712014-12-09 16:35:51 +00008533 }
Hal Finkelcf2e9082013-05-24 23:00:14 +00008534 case ISD::LOAD: {
8535 LoadSDNode *LD = cast<LoadSDNode>(N);
8536 EVT VT = LD->getValueType(0);
Bill Schmidtfae5d712014-12-09 16:35:51 +00008537
8538 // For little endian, VSX loads require generating lxvd2x/xxswapd.
8539 if (VT.isSimple()) {
8540 MVT LoadVT = VT.getSimpleVT();
8541 if (TM.getSubtarget<PPCSubtarget>().hasVSX() &&
8542 TM.getSubtarget<PPCSubtarget>().isLittleEndian() &&
8543 (LoadVT == MVT::v2f64 || LoadVT == MVT::v2i64 ||
8544 LoadVT == MVT::v4f32 || LoadVT == MVT::v4i32))
8545 return expandVSXLoadForLE(N, DCI);
8546 }
8547
Hal Finkelcf2e9082013-05-24 23:00:14 +00008548 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
8549 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
8550 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
8551 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
Bill Schmidt2d1128a2014-10-17 15:13:38 +00008552 // P8 and later hardware should just use LOAD.
8553 !TM.getSubtarget<PPCSubtarget>().hasP8Vector() &&
Hal Finkel40c34782013-09-15 22:09:58 +00008554 (VT == MVT::v16i8 || VT == MVT::v8i16 ||
8555 VT == MVT::v4i32 || VT == MVT::v4f32) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +00008556 LD->getAlignment() < ABIAlignment) {
8557 // This is a type-legal unaligned Altivec load.
8558 SDValue Chain = LD->getChain();
8559 SDValue Ptr = LD->getBasePtr();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008560 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelcf2e9082013-05-24 23:00:14 +00008561
8562 // This implements the loading of unaligned vectors as described in
8563 // the venerable Apple Velocity Engine overview. Specifically:
8564 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
8565 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
8566 //
8567 // The general idea is to expand a sequence of one or more unaligned
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008568 // loads into an alignment-based permutation-control instruction (lvsl
8569 // or lvsr), a series of regular vector loads (which always truncate
8570 // their input address to an aligned address), and a series of
8571 // permutations. The results of these permutations are the requested
8572 // loaded values. The trick is that the last "extra" load is not taken
8573 // from the address you might suspect (sizeof(vector) bytes after the
8574 // last requested load), but rather sizeof(vector) - 1 bytes after the
8575 // last requested vector. The point of this is to avoid a page fault if
8576 // the base address happened to be aligned. This works because if the
8577 // base address is aligned, then adding less than a full vector length
8578 // will cause the last vector in the sequence to be (re)loaded.
8579 // Otherwise, the next vector will be fetched as you might suspect was
8580 // necessary.
Hal Finkelcf2e9082013-05-24 23:00:14 +00008581
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008582 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00008583 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008584 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
8585 // optimization later.
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008586 Intrinsic::ID Intr = (isLittleEndian ?
8587 Intrinsic::ppc_altivec_lvsr :
8588 Intrinsic::ppc_altivec_lvsl);
8589 SDValue PermCntl = BuildIntrinsicOp(Intr, Ptr, DAG, dl, MVT::v16i8);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008590
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008591 // Create the new MMO for the new base load. It is like the original MMO,
8592 // but represents an area in memory almost twice the vector size centered
8593 // on the original address. If the address is unaligned, we might start
8594 // reading up to (sizeof(vector)-1) bytes below the address of the
8595 // original unaligned load.
Hal Finkelcf2e9082013-05-24 23:00:14 +00008596 MachineFunction &MF = DAG.getMachineFunction();
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008597 MachineMemOperand *BaseMMO =
8598 MF.getMachineMemOperand(LD->getMemOperand(),
8599 -LD->getMemoryVT().getStoreSize()+1,
8600 2*LD->getMemoryVT().getStoreSize()-1);
8601
8602 // Create the new base load.
8603 SDValue LDXIntID = DAG.getTargetConstant(Intrinsic::ppc_altivec_lvx,
8604 getPointerTy());
8605 SDValue BaseLoadOps[] = { Chain, LDXIntID, Ptr };
8606 SDValue BaseLoad =
8607 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
8608 DAG.getVTList(MVT::v4i32, MVT::Other),
8609 BaseLoadOps, MVT::v4i32, BaseMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008610
8611 // Note that the value of IncOffset (which is provided to the next
8612 // load's pointer info offset value, and thus used to calculate the
8613 // alignment), and the value of IncValue (which is actually used to
8614 // increment the pointer value) are different! This is because we
8615 // require the next load to appear to be aligned, even though it
8616 // is actually offset from the base pointer by a lesser amount.
8617 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +00008618 int IncValue = IncOffset;
8619
8620 // Walk (both up and down) the chain looking for another load at the real
8621 // (aligned) offset (the alignment of the other load does not matter in
8622 // this case). If found, then do not use the offset reduction trick, as
8623 // that will prevent the loads from being later combined (as they would
8624 // otherwise be duplicates).
8625 if (!findConsecutiveLoad(LD, DAG))
8626 --IncValue;
8627
Hal Finkelcf2e9082013-05-24 23:00:14 +00008628 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
8629 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
8630
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008631 MachineMemOperand *ExtraMMO =
8632 MF.getMachineMemOperand(LD->getMemOperand(),
8633 1, 2*LD->getMemoryVT().getStoreSize()-1);
8634 SDValue ExtraLoadOps[] = { Chain, LDXIntID, Ptr };
Hal Finkelcf2e9082013-05-24 23:00:14 +00008635 SDValue ExtraLoad =
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008636 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
8637 DAG.getVTList(MVT::v4i32, MVT::Other),
8638 ExtraLoadOps, MVT::v4i32, ExtraMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008639
8640 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8641 BaseLoad.getValue(1), ExtraLoad.getValue(1));
8642
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008643 // Because vperm has a big-endian bias, we must reverse the order
8644 // of the input vectors and complement the permute control vector
8645 // when generating little endian code. We have already handled the
8646 // latter by using lvsr instead of lvsl, so just reverse BaseLoad
8647 // and ExtraLoad here.
8648 SDValue Perm;
8649 if (isLittleEndian)
8650 Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
8651 ExtraLoad, BaseLoad, PermCntl, DAG, dl);
8652 else
8653 Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
8654 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008655
8656 if (VT != MVT::v4i32)
8657 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
8658
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008659 // The output of the permutation is our loaded result, the TokenFactor is
8660 // our new chain.
8661 DCI.CombineTo(N, Perm, TF);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008662 return SDValue(N, 0);
8663 }
8664 }
8665 break;
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008666 case ISD::INTRINSIC_WO_CHAIN: {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008667 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008668 Intrinsic::ID Intr = (isLittleEndian ?
8669 Intrinsic::ppc_altivec_lvsr :
8670 Intrinsic::ppc_altivec_lvsl);
8671 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() == Intr &&
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008672 N->getOperand(1)->getOpcode() == ISD::ADD) {
8673 SDValue Add = N->getOperand(1);
8674
8675 if (DAG.MaskedValueIsZero(Add->getOperand(1),
8676 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
8677 Add.getValueType().getScalarType().getSizeInBits()))) {
8678 SDNode *BasePtr = Add->getOperand(0).getNode();
8679 for (SDNode::use_iterator UI = BasePtr->use_begin(),
8680 UE = BasePtr->use_end(); UI != UE; ++UI) {
8681 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8682 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008683 Intr) {
8684 // We've found another LVSL/LVSR, and this address is an aligned
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008685 // multiple of that one. The results will be the same, so use the
8686 // one we've just found instead.
8687
8688 return SDValue(*UI, 0);
8689 }
8690 }
8691 }
8692 }
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008693 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +00008694
8695 break;
Bill Schmidtfae5d712014-12-09 16:35:51 +00008696 case ISD::INTRINSIC_W_CHAIN: {
8697 // For little endian, VSX loads require generating lxvd2x/xxswapd.
8698 if (TM.getSubtarget<PPCSubtarget>().hasVSX() &&
8699 TM.getSubtarget<PPCSubtarget>().isLittleEndian()) {
8700 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8701 default:
8702 break;
8703 case Intrinsic::ppc_vsx_lxvw4x:
8704 case Intrinsic::ppc_vsx_lxvd2x:
8705 return expandVSXLoadForLE(N, DCI);
8706 }
8707 }
8708 break;
8709 }
8710 case ISD::INTRINSIC_VOID: {
8711 // For little endian, VSX stores require generating xxswapd/stxvd2x.
8712 if (TM.getSubtarget<PPCSubtarget>().hasVSX() &&
8713 TM.getSubtarget<PPCSubtarget>().isLittleEndian()) {
8714 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8715 default:
8716 break;
8717 case Intrinsic::ppc_vsx_stxvw4x:
8718 case Intrinsic::ppc_vsx_stxvd2x:
8719 return expandVSXStoreForLE(N, DCI);
8720 }
8721 }
8722 break;
8723 }
Chris Lattnera7976d32006-07-10 20:56:58 +00008724 case ISD::BSWAP:
8725 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008726 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +00008727 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008728 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
8729 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008730 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008731 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008732 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00008733 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +00008734 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008735 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +00008736 LD->getChain(), // Chain
8737 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008738 DAG.getValueType(N->getValueType(0)) // VT
8739 };
Dan Gohman48b185d2009-09-25 20:36:54 +00008740 SDValue BSLoad =
8741 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +00008742 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
8743 MVT::i64 : MVT::i32, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00008744 Ops, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008745
Scott Michelcf0da6c2009-02-17 22:15:04 +00008746 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008747 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +00008748 if (N->getValueType(0) == MVT::i16)
8749 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008750
Chris Lattnera7976d32006-07-10 20:56:58 +00008751 // First, combine the bswap away. This makes the value produced by the
8752 // load dead.
8753 DCI.CombineTo(N, ResVal);
8754
8755 // Next, combine the load away, we give it a bogus result value but a real
8756 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008757 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00008758
Chris Lattnera7976d32006-07-10 20:56:58 +00008759 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008760 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008761 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008762
Chris Lattner27f53452006-03-01 05:50:56 +00008763 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008764 case PPCISD::VCMP: {
8765 // If a VCMPo node already exists with exactly the same operands as this
8766 // node, use its result instead of this node (VCMPo computes both a CR6 and
8767 // a normal output).
8768 //
8769 if (!N->getOperand(0).hasOneUse() &&
8770 !N->getOperand(1).hasOneUse() &&
8771 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00008772
Chris Lattnerd4058a52006-03-31 06:02:07 +00008773 // Scan all of the users of the LHS, looking for VCMPo's that match.
Craig Topper062a2ba2014-04-25 05:30:21 +00008774 SDNode *VCMPoNode = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008775
Gabor Greiff304a7a2008-08-28 21:40:38 +00008776 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +00008777 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
8778 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008779 if (UI->getOpcode() == PPCISD::VCMPo &&
8780 UI->getOperand(1) == N->getOperand(1) &&
8781 UI->getOperand(2) == N->getOperand(2) &&
8782 UI->getOperand(0) == N->getOperand(0)) {
8783 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008784 break;
8785 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008786
Chris Lattner518834c2006-04-18 18:28:22 +00008787 // If there is no VCMPo node, or if the flag value has a single use, don't
8788 // transform this.
8789 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
8790 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008791
8792 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +00008793 // chain, this transformation is more complex. Note that multiple things
8794 // could use the value result, which we should ignore.
Craig Topper062a2ba2014-04-25 05:30:21 +00008795 SDNode *FlagUser = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008796 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Craig Topper062a2ba2014-04-25 05:30:21 +00008797 FlagUser == nullptr; ++UI) {
Chris Lattner518834c2006-04-18 18:28:22 +00008798 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008799 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +00008800 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008801 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +00008802 FlagUser = User;
8803 break;
8804 }
8805 }
8806 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008807
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008808 // If the user is a MFOCRF instruction, we know this is safe.
8809 // Otherwise we give up for right now.
8810 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008811 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +00008812 }
8813 break;
8814 }
Hal Finkel940ab932014-02-28 00:27:01 +00008815 case ISD::BRCOND: {
8816 SDValue Cond = N->getOperand(1);
8817 SDValue Target = N->getOperand(2);
8818
8819 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8820 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
8821 Intrinsic::ppc_is_decremented_ctr_nonzero) {
8822
8823 // We now need to make the intrinsic dead (it cannot be instruction
8824 // selected).
8825 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
8826 assert(Cond.getNode()->hasOneUse() &&
8827 "Counter decrement has more than one use");
8828
8829 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
8830 N->getOperand(0), Target);
8831 }
8832 }
8833 break;
Chris Lattner9754d142006-04-18 17:59:36 +00008834 case ISD::BR_CC: {
8835 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008836 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +00008837 // lowering is done pre-legalize, because the legalizer lowers the predicate
8838 // compare down to code that is difficult to reassemble.
8839 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008840 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +00008841
8842 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
8843 // value. If so, pass-through the AND to get to the intrinsic.
8844 if (LHS.getOpcode() == ISD::AND &&
8845 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8846 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
8847 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8848 isa<ConstantSDNode>(LHS.getOperand(1)) &&
8849 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
8850 isZero())
8851 LHS = LHS.getOperand(0);
8852
8853 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8854 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
8855 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8856 isa<ConstantSDNode>(RHS)) {
8857 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
8858 "Counter decrement comparison is not EQ or NE");
8859
8860 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
8861 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
8862 (CC == ISD::SETNE && !Val);
8863
8864 // We now need to make the intrinsic dead (it cannot be instruction
8865 // selected).
8866 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
8867 assert(LHS.getNode()->hasOneUse() &&
8868 "Counter decrement has more than one use");
8869
8870 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
8871 N->getOperand(0), N->getOperand(4));
8872 }
8873
Chris Lattner9754d142006-04-18 17:59:36 +00008874 int CompareOpc;
8875 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008876
Chris Lattner9754d142006-04-18 17:59:36 +00008877 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8878 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
8879 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
8880 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00008881
Chris Lattner9754d142006-04-18 17:59:36 +00008882 // If this is a comparison against something other than 0/1, then we know
8883 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008884 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00008885 if (Val != 0 && Val != 1) {
8886 if (CC == ISD::SETEQ) // Cond never true, remove branch.
8887 return N->getOperand(0);
8888 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +00008889 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +00008890 N->getOperand(0), N->getOperand(4));
8891 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008892
Chris Lattner9754d142006-04-18 17:59:36 +00008893 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008894
Chris Lattner9754d142006-04-18 17:59:36 +00008895 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008896 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008897 LHS.getOperand(2), // LHS of compare
8898 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +00008899 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008900 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00008901 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00008902 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008903
Chris Lattner9754d142006-04-18 17:59:36 +00008904 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008905 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +00008906 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +00008907 default: // Can't happen, don't crash on invalid number though.
8908 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008909 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +00008910 break;
8911 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008912 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +00008913 break;
8914 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008915 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +00008916 break;
8917 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008918 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +00008919 break;
8920 }
8921
Owen Anderson9f944592009-08-11 20:47:22 +00008922 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
8923 DAG.getConstant(CompOpc, MVT::i32),
8924 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +00008925 N->getOperand(4), CompNode.getValue(1));
8926 }
8927 break;
8928 }
Chris Lattnerf4184352006-03-01 04:57:39 +00008929 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008930
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008931 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +00008932}
8933
Chris Lattner4211ca92006-04-14 06:01:58 +00008934//===----------------------------------------------------------------------===//
8935// Inline Assembly Support
8936//===----------------------------------------------------------------------===//
8937
Jay Foada0653a32014-05-14 21:14:37 +00008938void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
8939 APInt &KnownZero,
8940 APInt &KnownOne,
8941 const SelectionDAG &DAG,
8942 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00008943 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +00008944 switch (Op.getOpcode()) {
8945 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008946 case PPCISD::LBRX: {
8947 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +00008948 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +00008949 KnownZero = 0xFFFF0000;
8950 break;
8951 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008952 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00008953 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +00008954 default: break;
8955 case Intrinsic::ppc_altivec_vcmpbfp_p:
8956 case Intrinsic::ppc_altivec_vcmpeqfp_p:
8957 case Intrinsic::ppc_altivec_vcmpequb_p:
8958 case Intrinsic::ppc_altivec_vcmpequh_p:
8959 case Intrinsic::ppc_altivec_vcmpequw_p:
8960 case Intrinsic::ppc_altivec_vcmpgefp_p:
8961 case Intrinsic::ppc_altivec_vcmpgtfp_p:
8962 case Intrinsic::ppc_altivec_vcmpgtsb_p:
8963 case Intrinsic::ppc_altivec_vcmpgtsh_p:
8964 case Intrinsic::ppc_altivec_vcmpgtsw_p:
8965 case Intrinsic::ppc_altivec_vcmpgtub_p:
8966 case Intrinsic::ppc_altivec_vcmpgtuh_p:
8967 case Intrinsic::ppc_altivec_vcmpgtuw_p:
8968 KnownZero = ~1U; // All bits but the low one are known to be zero.
8969 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008970 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008971 }
8972 }
8973}
8974
8975
Chris Lattnerd6855142007-03-25 02:14:49 +00008976/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +00008977/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008978PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00008979PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
8980 if (Constraint.size() == 1) {
8981 switch (Constraint[0]) {
8982 default: break;
8983 case 'b':
8984 case 'r':
8985 case 'f':
8986 case 'v':
8987 case 'y':
8988 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +00008989 case 'Z':
8990 // FIXME: While Z does indicate a memory constraint, it specifically
8991 // indicates an r+r address (used in conjunction with the 'y' modifier
8992 // in the replacement string). Currently, we're forcing the base
8993 // register to be r0 in the asm printer (which is interpreted as zero)
8994 // and forming the complete address in the second register. This is
8995 // suboptimal.
8996 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +00008997 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008998 } else if (Constraint == "wc") { // individual CR bits.
8999 return C_RegisterClass;
Hal Finkel27774d92014-03-13 07:58:58 +00009000 } else if (Constraint == "wa" || Constraint == "wd" ||
9001 Constraint == "wf" || Constraint == "ws") {
9002 return C_RegisterClass; // VSX registers.
Chris Lattnerd6855142007-03-25 02:14:49 +00009003 }
9004 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +00009005}
9006
John Thompsone8360b72010-10-29 17:29:13 +00009007/// Examine constraint type and operand type and determine a weight value.
9008/// This object must already have been set up with the operand type
9009/// and the current alternative constraint selected.
9010TargetLowering::ConstraintWeight
9011PPCTargetLowering::getSingleConstraintMatchWeight(
9012 AsmOperandInfo &info, const char *constraint) const {
9013 ConstraintWeight weight = CW_Invalid;
9014 Value *CallOperandVal = info.CallOperandVal;
9015 // If we don't have a value, we can't do a match,
9016 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00009017 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00009018 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00009019 Type *type = CallOperandVal->getType();
Hal Finkel6aca2372014-03-02 18:23:39 +00009020
John Thompsone8360b72010-10-29 17:29:13 +00009021 // Look at the constraint type.
Hal Finkel6aca2372014-03-02 18:23:39 +00009022 if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
9023 return CW_Register; // an individual CR bit.
Hal Finkel27774d92014-03-13 07:58:58 +00009024 else if ((StringRef(constraint) == "wa" ||
9025 StringRef(constraint) == "wd" ||
9026 StringRef(constraint) == "wf") &&
9027 type->isVectorTy())
9028 return CW_Register;
9029 else if (StringRef(constraint) == "ws" && type->isDoubleTy())
9030 return CW_Register;
Hal Finkel6aca2372014-03-02 18:23:39 +00009031
John Thompsone8360b72010-10-29 17:29:13 +00009032 switch (*constraint) {
9033 default:
9034 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9035 break;
9036 case 'b':
9037 if (type->isIntegerTy())
9038 weight = CW_Register;
9039 break;
9040 case 'f':
9041 if (type->isFloatTy())
9042 weight = CW_Register;
9043 break;
9044 case 'd':
9045 if (type->isDoubleTy())
9046 weight = CW_Register;
9047 break;
9048 case 'v':
9049 if (type->isVectorTy())
9050 weight = CW_Register;
9051 break;
9052 case 'y':
9053 weight = CW_Register;
9054 break;
Hal Finkel4f24c622012-11-05 18:18:42 +00009055 case 'Z':
9056 weight = CW_Memory;
9057 break;
John Thompsone8360b72010-10-29 17:29:13 +00009058 }
9059 return weight;
9060}
9061
Scott Michelcf0da6c2009-02-17 22:15:04 +00009062std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner584a11a2006-11-02 01:44:04 +00009063PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00009064 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +00009065 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +00009066 // GCC RS6000 Constraint Letters
9067 switch (Constraint[0]) {
9068 case 'b': // R1-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009069 if (VT == MVT::i64 && Subtarget.isPPC64())
Hal Finkel638a9fa2013-03-19 18:51:05 +00009070 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
9071 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00009072 case 'r': // R0-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009073 if (VT == MVT::i64 && Subtarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +00009074 return std::make_pair(0U, &PPC::G8RCRegClass);
9075 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00009076 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00009077 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +00009078 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00009079 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +00009080 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00009081 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009082 case 'v':
Craig Topperabadc662012-04-20 06:31:50 +00009083 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00009084 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +00009085 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00009086 }
Hal Finkel6aca2372014-03-02 18:23:39 +00009087 } else if (Constraint == "wc") { // an individual CR bit.
9088 return std::make_pair(0U, &PPC::CRBITRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +00009089 } else if (Constraint == "wa" || Constraint == "wd" ||
Hal Finkel19be5062014-03-29 05:29:01 +00009090 Constraint == "wf") {
Hal Finkel27774d92014-03-13 07:58:58 +00009091 return std::make_pair(0U, &PPC::VSRCRegClass);
Hal Finkel19be5062014-03-29 05:29:01 +00009092 } else if (Constraint == "ws") {
9093 return std::make_pair(0U, &PPC::VSFRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00009094 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009095
Hal Finkelb176acb2013-08-03 12:25:10 +00009096 std::pair<unsigned, const TargetRegisterClass*> R =
9097 TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9098
9099 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
9100 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
9101 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
9102 // register.
9103 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
9104 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009105 if (R.first && VT == MVT::i64 && Subtarget.isPPC64() &&
Hal Finkelb176acb2013-08-03 12:25:10 +00009106 PPC::GPRCRegClass.contains(R.first)) {
Eric Christopherd9134482014-08-04 21:25:23 +00009107 const TargetRegisterInfo *TRI =
9108 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Hal Finkelb176acb2013-08-03 12:25:10 +00009109 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +00009110 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +00009111 &PPC::G8RCRegClass);
9112 }
9113
Hal Finkelaa10b3c2014-12-08 22:54:22 +00009114 // GCC accepts 'cc' as an alias for 'cr0', and we need to do the same.
9115 if (!R.second && StringRef("{cc}").equals_lower(Constraint)) {
9116 R.first = PPC::CR0;
9117 R.second = &PPC::CRRCRegClass;
9118 }
9119
Hal Finkelb176acb2013-08-03 12:25:10 +00009120 return R;
Chris Lattner01513612006-01-31 19:20:21 +00009121}
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009122
Chris Lattner584a11a2006-11-02 01:44:04 +00009123
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00009124/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +00009125/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +00009126void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00009127 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009128 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00009129 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00009130 SDValue Result;
Eric Christopher0713a9d2011-06-08 23:55:35 +00009131
Eric Christopherde9399b2011-06-02 23:16:42 +00009132 // Only support length 1 constraints.
9133 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +00009134
Eric Christopherde9399b2011-06-02 23:16:42 +00009135 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009136 switch (Letter) {
9137 default: break;
9138 case 'I':
9139 case 'J':
9140 case 'K':
9141 case 'L':
9142 case 'M':
9143 case 'N':
9144 case 'O':
9145 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +00009146 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00009147 if (!CST) return; // Must be an immediate to match.
Hal Finkelc91fc112014-12-03 09:37:50 +00009148 int64_t Value = CST->getSExtValue();
9149 EVT TCVT = MVT::i64; // All constants taken to be 64 bits so that negative
9150 // numbers are printed as such.
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009151 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009152 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009153 case 'I': // "I" is a signed 16-bit constant.
Hal Finkelc91fc112014-12-03 09:37:50 +00009154 if (isInt<16>(Value))
9155 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +00009156 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009157 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
Hal Finkelc91fc112014-12-03 09:37:50 +00009158 if (isShiftedUInt<16, 16>(Value))
9159 Result = DAG.getTargetConstant(Value, TCVT);
9160 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009161 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Hal Finkelc91fc112014-12-03 09:37:50 +00009162 if (isShiftedInt<16, 16>(Value))
9163 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +00009164 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009165 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Hal Finkelc91fc112014-12-03 09:37:50 +00009166 if (isUInt<16>(Value))
9167 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +00009168 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009169 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +00009170 if (Value > 31)
Hal Finkelc91fc112014-12-03 09:37:50 +00009171 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +00009172 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009173 case 'N': // "N" is a positive constant that is an exact power of two.
Hal Finkelc91fc112014-12-03 09:37:50 +00009174 if (Value > 0 && isPowerOf2_64(Value))
9175 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +00009176 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009177 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00009178 if (Value == 0)
Hal Finkelc91fc112014-12-03 09:37:50 +00009179 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +00009180 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009181 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Hal Finkelc91fc112014-12-03 09:37:50 +00009182 if (isInt<16>(-Value))
9183 Result = DAG.getTargetConstant(Value, TCVT);
Chris Lattner8c6949e2006-10-31 19:40:43 +00009184 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009185 }
9186 break;
9187 }
9188 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009189
Gabor Greiff304a7a2008-08-28 21:40:38 +00009190 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00009191 Ops.push_back(Result);
9192 return;
9193 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009194
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009195 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +00009196 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009197}
Evan Cheng2dd2c652006-03-13 23:20:37 +00009198
Chris Lattner1eb94d92007-03-30 23:15:24 +00009199// isLegalAddressingMode - Return true if the addressing mode represented
9200// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +00009201bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00009202 Type *Ty) const {
Chris Lattner1eb94d92007-03-30 23:15:24 +00009203 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelcf0da6c2009-02-17 22:15:04 +00009204
Chris Lattner1eb94d92007-03-30 23:15:24 +00009205 // PPC allows a sign-extended 16-bit immediate field.
9206 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
9207 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009208
Chris Lattner1eb94d92007-03-30 23:15:24 +00009209 // No global is ever allowed as a base.
9210 if (AM.BaseGV)
9211 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009212
9213 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +00009214 switch (AM.Scale) {
9215 case 0: // "r+i" or just "i", depending on HasBaseReg.
9216 break;
9217 case 1:
9218 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
9219 return false;
9220 // Otherwise we have r+r or r+i.
9221 break;
9222 case 2:
9223 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
9224 return false;
9225 // Allow 2*r as r+r.
9226 break;
Chris Lattner19ccd622007-04-09 22:10:05 +00009227 default:
9228 // No other scales are supported.
9229 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +00009230 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009231
Chris Lattner1eb94d92007-03-30 23:15:24 +00009232 return true;
9233}
9234
Dan Gohman21cea8a2010-04-17 15:26:15 +00009235SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
9236 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00009237 MachineFunction &MF = DAG.getMachineFunction();
9238 MachineFrameInfo *MFI = MF.getFrameInfo();
9239 MFI->setReturnAddressIsTaken(true);
9240
Bill Wendling908bf812014-01-06 00:43:20 +00009241 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00009242 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00009243
Andrew Trickef9de2a2013-05-25 02:42:55 +00009244 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009245 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +00009246
Dale Johannesen81bfca72010-05-03 22:59:34 +00009247 // Make sure the function does not optimize away the store of the RA to
9248 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +00009249 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +00009250 FuncInfo->setLRStoreRequired();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009251 bool isPPC64 = Subtarget.isPPC64();
9252 bool isDarwinABI = Subtarget.isDarwinABI();
Dale Johannesen81bfca72010-05-03 22:59:34 +00009253
9254 if (Depth > 0) {
9255 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9256 SDValue Offset =
Wesley Peck527da1b2010-11-23 03:31:01 +00009257
Anton Korobeynikov2f931282011-01-10 12:39:04 +00009258 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen81bfca72010-05-03 22:59:34 +00009259 isPPC64? MVT::i64 : MVT::i32);
9260 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
9261 DAG.getNode(ISD::ADD, dl, getPointerTy(),
9262 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009263 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009264 }
Chris Lattnerf6a81562007-12-08 06:59:59 +00009265
Chris Lattnerf6a81562007-12-08 06:59:59 +00009266 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009267 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009268 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009269 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +00009270}
9271
Dan Gohman21cea8a2010-04-17 15:26:15 +00009272SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
9273 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009274 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009275 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00009276
Owen Anderson53aa7a92009-08-10 22:56:29 +00009277 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00009278 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009279
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00009280 MachineFunction &MF = DAG.getMachineFunction();
9281 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +00009282 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +00009283
9284 // Naked functions never have a frame pointer, and so we use r1. For all
9285 // other functions, this decision must be delayed until during PEI.
9286 unsigned FrameReg;
9287 if (MF.getFunction()->getAttributes().hasAttribute(
9288 AttributeSet::FunctionIndex, Attribute::Naked))
9289 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
9290 else
9291 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
9292
Dale Johannesen81bfca72010-05-03 22:59:34 +00009293 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
9294 PtrVT);
9295 while (Depth--)
9296 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009297 FrameAddr, MachinePointerInfo(), false, false,
9298 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009299 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00009300}
Dan Gohmanc14e5222008-10-21 03:41:46 +00009301
Hal Finkel0d8db462014-05-11 19:29:11 +00009302// FIXME? Maybe this could be a TableGen attribute on some registers and
9303// this table could be generated automatically from RegInfo.
9304unsigned PPCTargetLowering::getRegisterByName(const char* RegName,
9305 EVT VT) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009306 bool isPPC64 = Subtarget.isPPC64();
9307 bool isDarwinABI = Subtarget.isDarwinABI();
Hal Finkel0d8db462014-05-11 19:29:11 +00009308
9309 if ((isPPC64 && VT != MVT::i64 && VT != MVT::i32) ||
9310 (!isPPC64 && VT != MVT::i32))
9311 report_fatal_error("Invalid register global variable type");
9312
9313 bool is64Bit = isPPC64 && VT == MVT::i64;
9314 unsigned Reg = StringSwitch<unsigned>(RegName)
9315 .Case("r1", is64Bit ? PPC::X1 : PPC::R1)
9316 .Case("r2", isDarwinABI ? 0 : (is64Bit ? PPC::X2 : PPC::R2))
9317 .Case("r13", (!isPPC64 && isDarwinABI) ? 0 :
9318 (is64Bit ? PPC::X13 : PPC::R13))
9319 .Default(0);
9320
9321 if (Reg)
9322 return Reg;
9323 report_fatal_error("Invalid register name global variable");
9324}
9325
Dan Gohmanc14e5222008-10-21 03:41:46 +00009326bool
9327PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9328 // The PowerPC target isn't yet aware of offsets.
9329 return false;
9330}
Tilmann Schellerb93960d2009-07-03 06:45:56 +00009331
Hal Finkel46ef7ce2014-08-13 01:15:40 +00009332bool PPCTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9333 const CallInst &I,
9334 unsigned Intrinsic) const {
9335
9336 switch (Intrinsic) {
9337 case Intrinsic::ppc_altivec_lvx:
9338 case Intrinsic::ppc_altivec_lvxl:
9339 case Intrinsic::ppc_altivec_lvebx:
9340 case Intrinsic::ppc_altivec_lvehx:
Bill Schmidt72954782014-11-12 04:19:40 +00009341 case Intrinsic::ppc_altivec_lvewx:
9342 case Intrinsic::ppc_vsx_lxvd2x:
9343 case Intrinsic::ppc_vsx_lxvw4x: {
Hal Finkel46ef7ce2014-08-13 01:15:40 +00009344 EVT VT;
9345 switch (Intrinsic) {
9346 case Intrinsic::ppc_altivec_lvebx:
9347 VT = MVT::i8;
9348 break;
9349 case Intrinsic::ppc_altivec_lvehx:
9350 VT = MVT::i16;
9351 break;
9352 case Intrinsic::ppc_altivec_lvewx:
9353 VT = MVT::i32;
9354 break;
Bill Schmidt72954782014-11-12 04:19:40 +00009355 case Intrinsic::ppc_vsx_lxvd2x:
9356 VT = MVT::v2f64;
9357 break;
Hal Finkel46ef7ce2014-08-13 01:15:40 +00009358 default:
9359 VT = MVT::v4i32;
9360 break;
9361 }
9362
9363 Info.opc = ISD::INTRINSIC_W_CHAIN;
9364 Info.memVT = VT;
9365 Info.ptrVal = I.getArgOperand(0);
9366 Info.offset = -VT.getStoreSize()+1;
9367 Info.size = 2*VT.getStoreSize()-1;
9368 Info.align = 1;
9369 Info.vol = false;
9370 Info.readMem = true;
9371 Info.writeMem = false;
9372 return true;
9373 }
9374 case Intrinsic::ppc_altivec_stvx:
9375 case Intrinsic::ppc_altivec_stvxl:
9376 case Intrinsic::ppc_altivec_stvebx:
9377 case Intrinsic::ppc_altivec_stvehx:
Bill Schmidt72954782014-11-12 04:19:40 +00009378 case Intrinsic::ppc_altivec_stvewx:
9379 case Intrinsic::ppc_vsx_stxvd2x:
9380 case Intrinsic::ppc_vsx_stxvw4x: {
Hal Finkel46ef7ce2014-08-13 01:15:40 +00009381 EVT VT;
9382 switch (Intrinsic) {
9383 case Intrinsic::ppc_altivec_stvebx:
9384 VT = MVT::i8;
9385 break;
9386 case Intrinsic::ppc_altivec_stvehx:
9387 VT = MVT::i16;
9388 break;
9389 case Intrinsic::ppc_altivec_stvewx:
9390 VT = MVT::i32;
9391 break;
Bill Schmidt72954782014-11-12 04:19:40 +00009392 case Intrinsic::ppc_vsx_stxvd2x:
9393 VT = MVT::v2f64;
9394 break;
Hal Finkel46ef7ce2014-08-13 01:15:40 +00009395 default:
9396 VT = MVT::v4i32;
9397 break;
9398 }
9399
9400 Info.opc = ISD::INTRINSIC_VOID;
9401 Info.memVT = VT;
9402 Info.ptrVal = I.getArgOperand(1);
9403 Info.offset = -VT.getStoreSize()+1;
9404 Info.size = 2*VT.getStoreSize()-1;
9405 Info.align = 1;
9406 Info.vol = false;
9407 Info.readMem = false;
9408 Info.writeMem = true;
9409 return true;
9410 }
9411 default:
9412 break;
9413 }
9414
9415 return false;
9416}
9417
Evan Chengd9929f02010-04-01 20:10:42 +00009418/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +00009419/// and store operations as a result of memset, memcpy, and memmove
9420/// lowering. If DstAlign is zero that means it's safe to destination
9421/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
9422/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +00009423/// probably because the source does not need to be loaded. If 'IsMemset' is
9424/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
9425/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
9426/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +00009427/// It returns EVT::Other if the type should be determined using generic
9428/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +00009429EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
9430 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009431 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00009432 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +00009433 MachineFunction &MF) const {
Eric Christopherd90a8742014-06-12 22:38:20 +00009434 if (Subtarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +00009435 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00009436 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00009437 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00009438 }
9439}
Hal Finkel88ed4e32012-04-01 19:23:08 +00009440
Hal Finkel34974ed2014-04-12 21:52:38 +00009441/// \brief Returns true if it is beneficial to convert a load of a constant
9442/// to just the constant itself.
9443bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
9444 Type *Ty) const {
9445 assert(Ty->isIntegerTy());
9446
9447 unsigned BitSize = Ty->getPrimitiveSizeInBits();
9448 if (BitSize == 0 || BitSize > 64)
9449 return false;
9450 return true;
9451}
9452
9453bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
9454 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9455 return false;
9456 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9457 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
9458 return NumBits1 == 64 && NumBits2 == 32;
9459}
9460
9461bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
9462 if (!VT1.isInteger() || !VT2.isInteger())
9463 return false;
9464 unsigned NumBits1 = VT1.getSizeInBits();
9465 unsigned NumBits2 = VT2.getSizeInBits();
9466 return NumBits1 == 64 && NumBits2 == 32;
9467}
9468
9469bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
9470 return isInt<16>(Imm) || isUInt<16>(Imm);
9471}
9472
9473bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const {
9474 return isInt<16>(Imm) || isUInt<16>(Imm);
9475}
9476
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009477bool PPCTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
9478 unsigned,
9479 unsigned,
9480 bool *Fast) const {
Hal Finkel8d7fbc92013-03-15 15:27:13 +00009481 if (DisablePPCUnaligned)
9482 return false;
9483
9484 // PowerPC supports unaligned memory access for simple non-vector types.
9485 // Although accessing unaligned addresses is not as efficient as accessing
9486 // aligned addresses, it is generally more efficient than manual expansion,
9487 // and generally only traps for software emulation when crossing page
9488 // boundaries.
9489
9490 if (!VT.isSimple())
9491 return false;
9492
Hal Finkel6e28e6a2014-03-26 19:39:09 +00009493 if (VT.getSimpleVT().isVector()) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009494 if (Subtarget.hasVSX()) {
Bill Schmidt2d1128a2014-10-17 15:13:38 +00009495 if (VT != MVT::v2f64 && VT != MVT::v2i64 &&
9496 VT != MVT::v4f32 && VT != MVT::v4i32)
Hal Finkel6e28e6a2014-03-26 19:39:09 +00009497 return false;
9498 } else {
9499 return false;
9500 }
9501 }
Hal Finkel8d7fbc92013-03-15 15:27:13 +00009502
9503 if (VT == MVT::ppcf128)
9504 return false;
9505
9506 if (Fast)
9507 *Fast = true;
9508
9509 return true;
9510}
9511
Stephen Lin73de7bf2013-07-09 18:16:56 +00009512bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
9513 VT = VT.getScalarType();
9514
Hal Finkel0a479ae2012-06-22 00:49:52 +00009515 if (!VT.isSimple())
9516 return false;
9517
9518 switch (VT.getSimpleVT().SimpleTy) {
9519 case MVT::f32:
9520 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +00009521 return true;
9522 default:
9523 break;
9524 }
9525
9526 return false;
9527}
9528
Hal Finkelb4240ca2014-03-31 17:48:16 +00009529bool
9530PPCTargetLowering::shouldExpandBuildVectorWithShuffles(
9531 EVT VT , unsigned DefinedValues) const {
9532 if (VT == MVT::v2i64)
9533 return false;
9534
9535 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
9536}
9537
Hal Finkel88ed4e32012-04-01 19:23:08 +00009538Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009539 if (DisableILPPref || Subtarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +00009540 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +00009541
Hal Finkel4e9f1a82012-06-10 19:32:29 +00009542 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +00009543}
9544
Bill Schmidt0cf702f2013-07-30 00:50:39 +00009545// Create a fast isel object.
9546FastISel *
9547PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
9548 const TargetLibraryInfo *LibInfo) const {
9549 return PPC::createFastISel(FuncInfo, LibInfo);
9550}