blob: 5e12664e728ca56ee5309e8611c08ebaa4c01810 [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"
Chris Lattner4f2e4e02007-03-06 00:59:59 +000021#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner9b577f12005-08-26 21:23:58 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000026#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000027#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/Constants.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/Intrinsics.h"
Chris Lattnerce645542006-11-10 02:08:47 +000033#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
Craig Topperb25fda92012-03-17 18:46:09 +000035#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000036#include "llvm/Support/raw_ostream.h"
Craig Topperb25fda92012-03-17 18:46:09 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000038using namespace llvm;
39
Hal Finkel595817e2012-06-04 02:21:00 +000040static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
41cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000042
Hal Finkel4e9f1a82012-06-10 19:32:29 +000043static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
44cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
45
Hal Finkel8d7fbc92013-03-15 15:27:13 +000046static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
47cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
48
Chris Lattner5e693ed2009-07-28 03:13:23 +000049static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) {
50 if (TM.getSubtargetImpl()->isDarwin())
Bill Wendlingbbcaa402010-03-15 21:09:38 +000051 return new TargetLoweringObjectFileMachO();
Bill Wendlingdd3fe942010-03-12 02:00:43 +000052
Bill Schmidt22d40dc2013-05-13 19:34:37 +000053 if (TM.getSubtargetImpl()->isSVR4ABI())
54 return new PPC64LinuxTargetObjectFile();
55
Bruno Cardoso Lopes62e6a8b2009-08-13 23:30:21 +000056 return new TargetLoweringObjectFileELF();
Chris Lattner5e693ed2009-07-28 03:13:23 +000057}
58
Chris Lattner584a11a2006-11-02 01:44:04 +000059PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Chris Lattner5e693ed2009-07-28 03:13:23 +000060 : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) {
Evan Cheng39e90022012-07-02 22:39:56 +000061 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
Scott Michelcf0da6c2009-02-17 22:15:04 +000062
Nate Begeman4dd38312005-10-21 00:02:42 +000063 setPow2DivIsCheap();
Dale Johannesenc31eb202008-07-31 18:13:12 +000064
Chris Lattnera028e7a2005-09-27 22:18:25 +000065 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000066 setUseUnderscoreSetJmp(true);
67 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000068
Chris Lattnerd10babf2010-10-10 18:34:00 +000069 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
70 // arguments are at least 4/8 bytes aligned.
Evan Cheng39e90022012-07-02 22:39:56 +000071 bool isPPC64 = Subtarget->isPPC64();
72 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000073
Chris Lattnerf22556d2005-08-16 17:14:42 +000074 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000075 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
76 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
77 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +000078
Evan Cheng5d9fd972006-10-04 00:56:09 +000079 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson9f944592009-08-11 20:47:22 +000080 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
81 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sands95d46ef2008-01-23 20:39:46 +000082
Owen Anderson9f944592009-08-11 20:47:22 +000083 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000084
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000085 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000086 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
87 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
88 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
89 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
90 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
91 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
92 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
93 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
94 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
95 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +000096
Dale Johannesen666323e2007-10-10 01:01:31 +000097 // This is used in the ppcf128->int sequence. Note it has different semantics
98 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +000099 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000100
Roman Divacky1faf5b02012-08-16 18:19:29 +0000101 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000102 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
103 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
104 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
105 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
106 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000107 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000108
Chris Lattnerf22556d2005-08-16 17:14:42 +0000109 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000110 setOperationAction(ISD::SREM, MVT::i32, Expand);
111 setOperationAction(ISD::UREM, MVT::i32, Expand);
112 setOperationAction(ISD::SREM, MVT::i64, Expand);
113 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000114
115 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000116 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
117 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
118 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
119 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
120 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
121 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
122 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
123 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000124
Dan Gohman482732a2007-10-11 23:21:31 +0000125 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000126 setOperationAction(ISD::FSIN , MVT::f64, Expand);
127 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000128 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000129 setOperationAction(ISD::FREM , MVT::f64, Expand);
130 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000131 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000132 setOperationAction(ISD::FSIN , MVT::f32, Expand);
133 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000134 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000135 setOperationAction(ISD::FREM , MVT::f32, Expand);
136 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000137 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000138
Owen Anderson9f944592009-08-11 20:47:22 +0000139 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000140
Chris Lattnerf22556d2005-08-16 17:14:42 +0000141 // If we're enabling GP optimizations, use hardware square root
Hal Finkel2e103312013-04-03 04:01:11 +0000142 if (!Subtarget->hasFSQRT() &&
143 !(TM.Options.UnsafeFPMath &&
144 Subtarget->hasFRSQRTE() && Subtarget->hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000145 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000146
147 if (!Subtarget->hasFSQRT() &&
148 !(TM.Options.UnsafeFPMath &&
149 Subtarget->hasFRSQRTES() && Subtarget->hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000150 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000151
Hal Finkeldbc78e12013-08-19 05:01:02 +0000152 if (Subtarget->hasFCPSGN()) {
153 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
154 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
155 } else {
156 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
157 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
158 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000159
Hal Finkelc20a08d2013-03-29 08:57:48 +0000160 if (Subtarget->hasFPRND()) {
161 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
162 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
163 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000164 setOperationAction(ISD::FROUND, MVT::f64, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000165
166 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
167 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
168 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000169 setOperationAction(ISD::FROUND, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000170 }
171
Nate Begeman2fba8a32006-01-14 03:14:10 +0000172 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000173 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000174 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000175 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
176 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000177 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000178 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000179 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
180 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000181
Hal Finkela4d07482013-03-28 13:29:47 +0000182 if (Subtarget->hasPOPCNTD()) {
Hal Finkel290376d2013-04-01 15:58:15 +0000183 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000184 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
185 } else {
186 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
187 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
188 }
189
Nate Begeman1b8121b2006-01-11 21:21:00 +0000190 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000191 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
192 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000193
Chris Lattnerf22556d2005-08-16 17:14:42 +0000194 // PowerPC does not have Select
Owen Anderson9f944592009-08-11 20:47:22 +0000195 setOperationAction(ISD::SELECT, MVT::i32, Expand);
196 setOperationAction(ISD::SELECT, MVT::i64, Expand);
197 setOperationAction(ISD::SELECT, MVT::f32, Expand);
198 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000199
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000200 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000201 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
202 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000203
Nate Begeman7e7f4392006-02-01 07:19:44 +0000204 // PowerPC wants to optimize integer setcc a bit
Owen Anderson9f944592009-08-11 20:47:22 +0000205 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000206
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000207 // PowerPC does not have BRCOND which requires SetCC
Owen Anderson9f944592009-08-11 20:47:22 +0000208 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000209
Owen Anderson9f944592009-08-11 20:47:22 +0000210 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000211
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000212 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000213 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000214
Jim Laskey6267b2c2005-08-17 00:40:22 +0000215 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000216 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
217 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000218
Wesley Peck527da1b2010-11-23 03:31:01 +0000219 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
220 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
221 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
222 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattnerc46fc242005-12-23 05:13:35 +0000223
Chris Lattner84b49d52006-04-28 21:56:10 +0000224 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000225 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000226
Hal Finkel1996f3d2013-03-27 19:10:42 +0000227 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000228 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
229 // support continuation, user-level threading, and etc.. As a result, no
230 // other SjLj exception interfaces are implemented and please don't build
231 // your own exception handling based on them.
232 // LLVM/Clang supports zero-cost DWARF exception handling.
233 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
234 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000235
236 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000237 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000238 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
239 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000240 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000241 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
242 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
243 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
244 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000245 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000246 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
247 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000248
Nate Begemanf69d13b2008-08-11 17:36:31 +0000249 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000250 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000251
252 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000253 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
254 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000255
Nate Begemane74795c2006-01-25 18:21:52 +0000256 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000257 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000258
Evan Cheng39e90022012-07-02 22:39:56 +0000259 if (Subtarget->isSVR4ABI()) {
260 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000261 // VAARG always uses double-word chunks, so promote anything smaller.
262 setOperationAction(ISD::VAARG, MVT::i1, Promote);
263 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
264 setOperationAction(ISD::VAARG, MVT::i8, Promote);
265 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
266 setOperationAction(ISD::VAARG, MVT::i16, Promote);
267 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
268 setOperationAction(ISD::VAARG, MVT::i32, Promote);
269 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
270 setOperationAction(ISD::VAARG, MVT::Other, Expand);
271 } else {
272 // VAARG is custom lowered with the 32-bit SVR4 ABI.
273 setOperationAction(ISD::VAARG, MVT::Other, Custom);
274 setOperationAction(ISD::VAARG, MVT::i64, Custom);
275 }
Roman Divacky4394e682011-06-28 15:30:42 +0000276 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000277 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000278
Roman Divackyc3825df2013-07-25 21:36:47 +0000279 if (Subtarget->isSVR4ABI() && !isPPC64)
280 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
281 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
282 else
283 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
284
Chris Lattner5bd514d2006-01-15 09:02:48 +0000285 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000286 setOperationAction(ISD::VAEND , MVT::Other, Expand);
287 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
288 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
289 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
290 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000291
Chris Lattner6961fc72006-03-26 10:06:40 +0000292 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000293 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000294
Hal Finkel25c19922013-05-15 21:37:41 +0000295 // To handle counter-based loop conditions.
296 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
297
Dale Johannesen160be0f2008-11-07 22:54:33 +0000298 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000299 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
300 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
301 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
302 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
303 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
304 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
305 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
306 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
307 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
308 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
309 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
310 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000311
Evan Cheng39e90022012-07-02 22:39:56 +0000312 if (Subtarget->has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000313 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000314 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
315 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
316 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
317 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000318 // This is just the low 32 bits of a (signed) fp->i64 conversion.
319 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000320 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000321
Hal Finkelf6d45f22013-04-01 17:52:07 +0000322 if (PPCSubTarget.hasLFIWAX() || Subtarget->isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000323 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000324 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000325 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000327 }
328
Hal Finkelf6d45f22013-04-01 17:52:07 +0000329 // With the instructions enabled under FPCVT, we can do everything.
330 if (PPCSubTarget.hasFPCVT()) {
331 if (Subtarget->has64BitSupport()) {
332 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
333 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
334 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
335 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
336 }
337
338 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
339 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
340 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
341 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
342 }
343
Evan Cheng39e90022012-07-02 22:39:56 +0000344 if (Subtarget->use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000345 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000346 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000347 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000348 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000349 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000350 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
351 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
352 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000353 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000354 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000355 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
356 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
357 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000358 }
Evan Cheng19264272006-03-01 01:11:20 +0000359
Evan Cheng39e90022012-07-02 22:39:56 +0000360 if (Subtarget->hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000361 // First set operation action for all vector types to expand. Then we
362 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson9f944592009-08-11 20:47:22 +0000363 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
364 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
365 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands13237ac2008-06-06 12:08:01 +0000366
Chris Lattner06a21ba2006-04-16 01:37:57 +0000367 // add/sub are legal for all supported vector VT's.
Duncan Sands13237ac2008-06-06 12:08:01 +0000368 setOperationAction(ISD::ADD , VT, Legal);
369 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000370
Chris Lattner95c7adc2006-04-04 17:25:31 +0000371 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000372 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000373 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000374
375 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000376 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000377 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000378 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000379 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000380 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000381 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000382 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000383 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000384 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000385 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000386 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000387 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000388
Chris Lattner06a21ba2006-04-16 01:37:57 +0000389 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000390 setOperationAction(ISD::MUL , VT, Expand);
391 setOperationAction(ISD::SDIV, VT, Expand);
392 setOperationAction(ISD::SREM, VT, Expand);
393 setOperationAction(ISD::UDIV, VT, Expand);
394 setOperationAction(ISD::UREM, VT, Expand);
395 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000396 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000397 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000398 setOperationAction(ISD::FSQRT, VT, Expand);
399 setOperationAction(ISD::FLOG, VT, Expand);
400 setOperationAction(ISD::FLOG10, VT, Expand);
401 setOperationAction(ISD::FLOG2, VT, Expand);
402 setOperationAction(ISD::FEXP, VT, Expand);
403 setOperationAction(ISD::FEXP2, VT, Expand);
404 setOperationAction(ISD::FSIN, VT, Expand);
405 setOperationAction(ISD::FCOS, VT, Expand);
406 setOperationAction(ISD::FABS, VT, Expand);
407 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000408 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000409 setOperationAction(ISD::FCEIL, VT, Expand);
410 setOperationAction(ISD::FTRUNC, VT, Expand);
411 setOperationAction(ISD::FRINT, VT, Expand);
412 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000413 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
414 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
415 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
416 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
417 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
418 setOperationAction(ISD::UDIVREM, VT, Expand);
419 setOperationAction(ISD::SDIVREM, VT, Expand);
420 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
421 setOperationAction(ISD::FPOW, VT, Expand);
422 setOperationAction(ISD::CTPOP, VT, Expand);
423 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000424 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000425 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000426 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000427 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000428 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
429
430 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
431 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
432 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
433 setTruncStoreAction(VT, InnerVT, Expand);
434 }
435 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
436 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
437 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000438 }
439
Chris Lattner95c7adc2006-04-04 17:25:31 +0000440 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
441 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000442 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000443
Owen Anderson9f944592009-08-11 20:47:22 +0000444 setOperationAction(ISD::AND , MVT::v4i32, Legal);
445 setOperationAction(ISD::OR , MVT::v4i32, Legal);
446 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
447 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
448 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
449 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000450 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
451 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
452 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
453 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000454 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
455 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
456 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
457 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000458
Craig Topperabadc662012-04-20 06:31:50 +0000459 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
460 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
461 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
462 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000463
Owen Anderson9f944592009-08-11 20:47:22 +0000464 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000465 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000466
467 if (TM.Options.UnsafeFPMath) {
468 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
469 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
470 }
471
Owen Anderson9f944592009-08-11 20:47:22 +0000472 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
473 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
474 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000475
Owen Anderson9f944592009-08-11 20:47:22 +0000476 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
477 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000478
Owen Anderson9f944592009-08-11 20:47:22 +0000479 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
480 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
481 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
482 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000483
484 // Altivec does not contain unordered floating-point compare instructions
485 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
486 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
487 setCondCodeAction(ISD::SETUGT, MVT::v4f32, Expand);
488 setCondCodeAction(ISD::SETUGE, MVT::v4f32, Expand);
489 setCondCodeAction(ISD::SETULT, MVT::v4f32, Expand);
490 setCondCodeAction(ISD::SETULE, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000491
492 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
493 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000494 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000495
Hal Finkel70381a72012-08-04 14:10:46 +0000496 if (Subtarget->has64BitSupport()) {
Hal Finkel322e41a2012-04-01 20:08:17 +0000497 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel70381a72012-08-04 14:10:46 +0000498 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
499 }
Hal Finkel322e41a2012-04-01 20:08:17 +0000500
Eli Friedman7dfa7912011-08-29 18:23:02 +0000501 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
502 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkel1b5ff082012-12-25 17:22:53 +0000503 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
504 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000505
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000506 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidta76bf5a2013-04-23 18:49:44 +0000507 // Altivec instructions set fields to all zeros or all ones.
508 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000509
Evan Cheng39e90022012-07-02 22:39:56 +0000510 if (isPPC64) {
Chris Lattner454436d2006-10-18 01:20:43 +0000511 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000512 setExceptionPointerRegister(PPC::X3);
513 setExceptionSelectorRegister(PPC::X4);
514 } else {
Chris Lattner454436d2006-10-18 01:20:43 +0000515 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000516 setExceptionPointerRegister(PPC::R3);
517 setExceptionSelectorRegister(PPC::R4);
518 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000519
Chris Lattnerf4184352006-03-01 04:57:39 +0000520 // We have target-specific dag combine patterns for the following nodes:
521 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000522 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000523 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000524 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnera7976d32006-07-10 20:56:58 +0000525 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000526 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000527
Hal Finkel2e103312013-04-03 04:01:11 +0000528 // Use reciprocal estimates.
529 if (TM.Options.UnsafeFPMath) {
530 setTargetDAGCombine(ISD::FDIV);
531 setTargetDAGCombine(ISD::FSQRT);
532 }
533
Dale Johannesen10432e52007-10-19 00:59:18 +0000534 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng39e90022012-07-02 22:39:56 +0000535 if (Subtarget->isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000536 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000537 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
538 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000539 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
540 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000541 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
542 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
543 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
544 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
545 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000546 }
547
Hal Finkel65298572011-10-17 18:53:03 +0000548 setMinFunctionAlignment(2);
549 if (PPCSubTarget.isDarwin())
550 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000551
Evan Cheng39e90022012-07-02 22:39:56 +0000552 if (isPPC64 && Subtarget->isJITCodeModel())
553 // Temporary workaround for the inability of PPC64 JIT to handle jump
554 // tables.
555 setSupportJumpTables(false);
556
Eli Friedman30a49e92011-08-03 21:06:02 +0000557 setInsertFencesForAtomic(true);
558
Hal Finkel21442b22013-09-11 23:05:25 +0000559 if (Subtarget->enableMachineScheduler())
560 setSchedulingPreference(Sched::Source);
561 else
562 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000563
Chris Lattnerf22556d2005-08-16 17:14:42 +0000564 computeRegisterProperties();
Hal Finkel742b5352012-08-28 16:12:39 +0000565
566 // The Freescale cores does better with aggressive inlining of memcpy and
567 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
568 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
569 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000570 MaxStoresPerMemset = 32;
571 MaxStoresPerMemsetOptSize = 16;
572 MaxStoresPerMemcpy = 32;
573 MaxStoresPerMemcpyOptSize = 8;
574 MaxStoresPerMemmove = 32;
575 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel742b5352012-08-28 16:12:39 +0000576
577 setPrefFunctionAlignment(4);
Hal Finkel742b5352012-08-28 16:12:39 +0000578 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000579}
580
Hal Finkel262a2242013-09-12 23:20:06 +0000581/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
582/// the desired ByVal argument alignment.
583static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
584 unsigned MaxMaxAlign) {
585 if (MaxAlign == MaxMaxAlign)
586 return;
587 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
588 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
589 MaxAlign = 32;
590 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
591 MaxAlign = 16;
592 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
593 unsigned EltAlign = 0;
594 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
595 if (EltAlign > MaxAlign)
596 MaxAlign = EltAlign;
597 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
598 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
599 unsigned EltAlign = 0;
600 getMaxByValAlign(STy->getElementType(i), EltAlign, MaxMaxAlign);
601 if (EltAlign > MaxAlign)
602 MaxAlign = EltAlign;
603 if (MaxAlign == MaxMaxAlign)
604 break;
605 }
606 }
607}
608
Dale Johannesencbde4c22008-02-28 22:31:51 +0000609/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
610/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000611unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000612 // Darwin passes everything on 4 byte boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000613 if (PPCSubTarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000614 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000615
616 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000617 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000618 unsigned Align = PPCSubTarget.isPPC64() ? 8 : 4;
619 if (PPCSubTarget.hasAltivec() || PPCSubTarget.hasQPX())
620 getMaxByValAlign(Ty, Align, PPCSubTarget.hasQPX() ? 32 : 16);
621 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000622}
623
Chris Lattner347ed8a2006-01-09 23:52:17 +0000624const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
625 switch (Opcode) {
626 default: return 0;
Evan Cheng32e376f2008-07-12 02:23:19 +0000627 case PPCISD::FSEL: return "PPCISD::FSEL";
628 case PPCISD::FCFID: return "PPCISD::FCFID";
629 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
630 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000631 case PPCISD::FRE: return "PPCISD::FRE";
632 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000633 case PPCISD::STFIWX: return "PPCISD::STFIWX";
634 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
635 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
636 case PPCISD::VPERM: return "PPCISD::VPERM";
637 case PPCISD::Hi: return "PPCISD::Hi";
638 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000639 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller79fef932009-12-18 13:00:15 +0000640 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
641 case PPCISD::LOAD: return "PPCISD::LOAD";
642 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000643 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
644 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
645 case PPCISD::SRL: return "PPCISD::SRL";
646 case PPCISD::SRA: return "PPCISD::SRA";
647 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000648 case PPCISD::CALL: return "PPCISD::CALL";
649 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000650 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000651 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng32e376f2008-07-12 02:23:19 +0000652 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel756810f2013-03-21 21:37:52 +0000653 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
654 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000655 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng32e376f2008-07-12 02:23:19 +0000656 case PPCISD::VCMP: return "PPCISD::VCMP";
657 case PPCISD::VCMPo: return "PPCISD::VCMPo";
658 case PPCISD::LBRX: return "PPCISD::LBRX";
659 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng32e376f2008-07-12 02:23:19 +0000660 case PPCISD::LARX: return "PPCISD::LARX";
661 case PPCISD::STCX: return "PPCISD::STCX";
662 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +0000663 case PPCISD::BDNZ: return "PPCISD::BDNZ";
664 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000665 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000666 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000667 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +0000668 case PPCISD::CR6SET: return "PPCISD::CR6SET";
669 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34627e32012-11-27 17:35:46 +0000670 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
671 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
672 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000673 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
674 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000675 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000676 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
677 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
678 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000679 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
680 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
681 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
682 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
683 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +0000684 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +0000685 case PPCISD::SC: return "PPCISD::SC";
Chris Lattner347ed8a2006-01-09 23:52:17 +0000686 }
687}
688
Matt Arsenault758659232013-05-18 00:21:46 +0000689EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000690 if (!VT.isVector())
691 return MVT::i32;
692 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000693}
694
Chris Lattner4211ca92006-04-14 06:01:58 +0000695//===----------------------------------------------------------------------===//
696// Node matching predicates, for use by the tblgen matching code.
697//===----------------------------------------------------------------------===//
698
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000699/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000700static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000701 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000702 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000703 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000704 // Maybe this has already been legalized into the constant pool?
705 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000706 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000707 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000708 }
709 return false;
710}
711
Chris Lattnere8b83b42006-04-06 17:23:16 +0000712/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
713/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000714static bool isConstantOrUndef(int Op, int Val) {
715 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000716}
717
718/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
719/// VPKUHUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000720bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000721 if (!isUnary) {
722 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000723 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000724 return false;
725 } else {
726 for (unsigned i = 0; i != 8; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000727 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
728 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000729 return false;
730 }
Chris Lattner1d338192006-04-06 18:26:28 +0000731 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000732}
733
734/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
735/// VPKUWUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000736bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000737 if (!isUnary) {
738 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000739 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
740 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000741 return false;
742 } else {
743 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000744 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
745 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
746 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
747 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000748 return false;
749 }
Chris Lattner1d338192006-04-06 18:26:28 +0000750 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000751}
752
Chris Lattnerf38e0332006-04-06 22:02:42 +0000753/// isVMerge - Common function, used to match vmrg* shuffles.
754///
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000755static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +0000756 unsigned LHSStart, unsigned RHSStart) {
Owen Anderson9f944592009-08-11 20:47:22 +0000757 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000758 "PPC only supports shuffles by bytes!");
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000759 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
760 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000761
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000762 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
763 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000764 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000765 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000766 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000767 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000768 return false;
769 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000770 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +0000771}
772
773/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
774/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000775bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000776 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000777 if (!isUnary)
778 return isVMerge(N, UnitSize, 8, 24);
779 return isVMerge(N, UnitSize, 8, 8);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000780}
781
782/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
783/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000784bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000785 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000786 if (!isUnary)
787 return isVMerge(N, UnitSize, 0, 16);
788 return isVMerge(N, UnitSize, 0, 0);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000789}
790
791
Chris Lattner1d338192006-04-06 18:26:28 +0000792/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
793/// amount, otherwise return -1.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000794int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Owen Anderson9f944592009-08-11 20:47:22 +0000795 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000796 "PPC only supports shuffles by bytes!");
797
798 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +0000799
Chris Lattner1d338192006-04-06 18:26:28 +0000800 // Find the first non-undef value in the shuffle mask.
801 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000802 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +0000803 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000804
Chris Lattner1d338192006-04-06 18:26:28 +0000805 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000806
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000807 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +0000808 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000809 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +0000810 if (ShiftAmt < i) return -1;
811 ShiftAmt -= i;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000812
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000813 if (!isUnary) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000814 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000815 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000816 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000817 return -1;
818 } else {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000819 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000820 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000821 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000822 return -1;
823 }
Chris Lattner1d338192006-04-06 18:26:28 +0000824 return ShiftAmt;
825}
Chris Lattnerffc47562006-03-20 06:33:01 +0000826
827/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
828/// specifies a splat of a single element that is suitable for input to
829/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000830bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +0000831 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +0000832 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000833
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000834 // This is a splat operation if each element of the permute is the same, and
835 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000836 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +0000837
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000838 // FIXME: Handle UNDEF elements too!
839 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +0000840 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000841
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000842 // Check that the indices are consecutive, in the case of a multi-byte element
843 // splatted with a v16i8 mask.
844 for (unsigned i = 1; i != EltSize; ++i)
845 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000846 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000847
Chris Lattner95c7adc2006-04-04 17:25:31 +0000848 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000849 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +0000850 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000851 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000852 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000853 }
Chris Lattner95c7adc2006-04-04 17:25:31 +0000854 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +0000855}
856
Evan Cheng581d2792007-07-30 07:51:22 +0000857/// isAllNegativeZeroVector - Returns true if all elements of build_vector
858/// are -0.0.
859bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000860 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
861
862 APInt APVal, APUndef;
863 unsigned BitSize;
864 bool HasAnyUndefs;
Wesley Peck527da1b2010-11-23 03:31:01 +0000865
Dale Johannesen5f4eecf2009-11-13 01:45:18 +0000866 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000867 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000868 return CFP->getValueAPF().isNegZero();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000869
Evan Cheng581d2792007-07-30 07:51:22 +0000870 return false;
871}
872
Chris Lattnerffc47562006-03-20 06:33:01 +0000873/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
874/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner95c7adc2006-04-04 17:25:31 +0000875unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000876 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
877 assert(isSplatShuffleMask(SVOp, EltSize));
878 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +0000879}
880
Chris Lattner74cf9ff2006-04-12 17:37:20 +0000881/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +0000882/// by using a vspltis[bhw] instruction of the specified element size, return
883/// the constant being splatted. The ByteSize field indicates the number of
884/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000885SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
886 SDValue OpVal(0, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000887
888 // If ByteSize of the splat is bigger than the element size of the
889 // build_vector, then we have a case where we are checking for a splat where
890 // multiple elements of the buildvector are folded together into a single
891 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
892 unsigned EltSize = 16/N->getNumOperands();
893 if (EltSize < ByteSize) {
894 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000895 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000896 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000897
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000898 // See if all of the elements in the buildvector agree across.
899 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
900 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
901 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000902 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000903
Scott Michelcf0da6c2009-02-17 22:15:04 +0000904
Gabor Greiff304a7a2008-08-28 21:40:38 +0000905 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000906 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
907 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000908 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000909 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000910
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000911 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
912 // either constant or undef values that are identical for each chunk. See
913 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000914
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000915 // Check to see if all of the leading entries are either 0 or -1. If
916 // neither, then this won't fit into the immediate field.
917 bool LeadingZero = true;
918 bool LeadingOnes = true;
919 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000920 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000921
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000922 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
923 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
924 }
925 // Finally, check the least significant entry.
926 if (LeadingZero) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000927 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +0000928 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +0000929 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000930 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +0000931 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000932 }
933 if (LeadingOnes) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000934 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +0000935 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +0000936 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000937 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +0000938 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000939 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000940
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000941 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000942 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000943
Chris Lattner2771e2c2006-03-25 06:12:06 +0000944 // Check to see if this buildvec has a single non-undef value in its elements.
945 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
946 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greiff304a7a2008-08-28 21:40:38 +0000947 if (OpVal.getNode() == 0)
Chris Lattner2771e2c2006-03-25 06:12:06 +0000948 OpVal = N->getOperand(i);
949 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000950 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000951 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000952
Gabor Greiff304a7a2008-08-28 21:40:38 +0000953 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000954
Eli Friedman9c6ab1a2009-05-24 02:03:36 +0000955 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +0000956 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +0000957 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000958 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000959 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +0000960 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000961 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +0000962 }
963
964 // If the splat value is larger than the element value, then we can never do
965 // this splat. The only case that we could fit the replicated bits into our
966 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000967 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000968
Chris Lattner2771e2c2006-03-25 06:12:06 +0000969 // If the element value is larger than the splat value, cut it in half and
970 // check to see if the two halves are equal. Continue doing this until we
971 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
972 while (ValSizeInBytes > ByteSize) {
973 ValSizeInBytes >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000974
Chris Lattner2771e2c2006-03-25 06:12:06 +0000975 // If the top half equals the bottom half, we're still ok.
Chris Lattner39cc7172006-04-05 17:39:25 +0000976 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
977 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000978 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000979 }
980
981 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +0000982 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000983
Evan Chengb1ddc982006-03-26 09:52:32 +0000984 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000985 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000986
Chris Lattnerd71a1f92006-04-08 06:46:53 +0000987 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +0000988 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +0000989 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000990 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000991}
992
Chris Lattner4211ca92006-04-14 06:01:58 +0000993//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +0000994// Addressing Mode Selection
995//===----------------------------------------------------------------------===//
996
997/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
998/// or 64-bit immediate, and if the value can be accurately represented as a
999/// sign extension from a 16-bit value. If so, this returns true and the
1000/// immediate.
1001static bool isIntS16Immediate(SDNode *N, short &Imm) {
1002 if (N->getOpcode() != ISD::Constant)
1003 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001004
Dan Gohmaneffb8942008-09-12 16:56:44 +00001005 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001006 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001007 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001008 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001009 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001010}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001011static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001012 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001013}
1014
1015
1016/// SelectAddressRegReg - Given the specified addressed, check to see if it
1017/// can be represented as an indexed [r+r] operation. Returns false if it
1018/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001019bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1020 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001021 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001022 short imm = 0;
1023 if (N.getOpcode() == ISD::ADD) {
1024 if (isIntS16Immediate(N.getOperand(1), imm))
1025 return false; // r+i
1026 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1027 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001028
Chris Lattnera801fced2006-11-08 02:15:41 +00001029 Base = N.getOperand(0);
1030 Index = N.getOperand(1);
1031 return true;
1032 } else if (N.getOpcode() == ISD::OR) {
1033 if (isIntS16Immediate(N.getOperand(1), imm))
1034 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001035
Chris Lattnera801fced2006-11-08 02:15:41 +00001036 // If this is an or of disjoint bitfields, we can codegen this as an add
1037 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1038 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001039 APInt LHSKnownZero, LHSKnownOne;
1040 APInt RHSKnownZero, RHSKnownOne;
1041 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001042 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001043
Dan Gohmanf19609a2008-02-27 01:23:58 +00001044 if (LHSKnownZero.getBoolValue()) {
1045 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001046 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001047 // If all of the bits are known zero on the LHS or RHS, the add won't
1048 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001049 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001050 Base = N.getOperand(0);
1051 Index = N.getOperand(1);
1052 return true;
1053 }
1054 }
1055 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001056
Chris Lattnera801fced2006-11-08 02:15:41 +00001057 return false;
1058}
1059
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001060// If we happen to be doing an i64 load or store into a stack slot that has
1061// less than a 4-byte alignment, then the frame-index elimination may need to
1062// use an indexed load or store instruction (because the offset may not be a
1063// multiple of 4). The extra register needed to hold the offset comes from the
1064// register scavenger, and it is possible that the scavenger will need to use
1065// an emergency spill slot. As a result, we need to make sure that a spill slot
1066// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1067// stack slot.
1068static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1069 // FIXME: This does not handle the LWA case.
1070 if (VT != MVT::i64)
1071 return;
1072
Hal Finkel7ab3db52013-07-10 15:29:01 +00001073 // NOTE: We'll exclude negative FIs here, which come from argument
1074 // lowering, because there are no known test cases triggering this problem
1075 // using packed structures (or similar). We can remove this exclusion if
1076 // we find such a test case. The reason why this is so test-case driven is
1077 // because this entire 'fixup' is only to prevent crashes (from the
1078 // register scavenger) on not-really-valid inputs. For example, if we have:
1079 // %a = alloca i1
1080 // %b = bitcast i1* %a to i64*
1081 // store i64* a, i64 b
1082 // then the store should really be marked as 'align 1', but is not. If it
1083 // were marked as 'align 1' then the indexed form would have been
1084 // instruction-selected initially, and the problem this 'fixup' is preventing
1085 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001086 if (FrameIdx < 0)
1087 return;
1088
1089 MachineFunction &MF = DAG.getMachineFunction();
1090 MachineFrameInfo *MFI = MF.getFrameInfo();
1091
1092 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1093 if (Align >= 4)
1094 return;
1095
1096 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1097 FuncInfo->setHasNonRISpills();
1098}
1099
Chris Lattnera801fced2006-11-08 02:15:41 +00001100/// Returns true if the address N can be represented by a base register plus
1101/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001102/// represented as reg+reg. If Aligned is true, only accept displacements
1103/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001104bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001105 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001106 SelectionDAG &DAG,
1107 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001108 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001109 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001110 // If this can be more profitably realized as r+r, fail.
1111 if (SelectAddressRegReg(N, Disp, Base, DAG))
1112 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001113
Chris Lattnera801fced2006-11-08 02:15:41 +00001114 if (N.getOpcode() == ISD::ADD) {
1115 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001116 if (isIntS16Immediate(N.getOperand(1), imm) &&
1117 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001118 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001119 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1120 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001121 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001122 } else {
1123 Base = N.getOperand(0);
1124 }
1125 return true; // [r+i]
1126 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1127 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001128 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001129 && "Cannot handle constant offsets yet!");
1130 Disp = N.getOperand(1).getOperand(0); // The global address.
1131 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001132 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001133 Disp.getOpcode() == ISD::TargetConstantPool ||
1134 Disp.getOpcode() == ISD::TargetJumpTable);
1135 Base = N.getOperand(0);
1136 return true; // [&g+r]
1137 }
1138 } else if (N.getOpcode() == ISD::OR) {
1139 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001140 if (isIntS16Immediate(N.getOperand(1), imm) &&
1141 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001142 // If this is an or of disjoint bitfields, we can codegen this as an add
1143 // (for better address arithmetic) if the LHS and RHS of the OR are
1144 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001145 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001146 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001147
Dan Gohmanf19609a2008-02-27 01:23:58 +00001148 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001149 // If all of the bits are known zero on the LHS or RHS, the add won't
1150 // carry.
1151 Base = N.getOperand(0);
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001152 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001153 return true;
1154 }
1155 }
1156 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1157 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001158
Chris Lattnera801fced2006-11-08 02:15:41 +00001159 // If this address fits entirely in a 16-bit sext immediate field, codegen
1160 // this as "d, 0"
1161 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001162 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001163 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkelf70c41e2013-03-21 23:45:03 +00001164 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1165 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001166 return true;
1167 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001168
1169 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001170 if ((CN->getValueType(0) == MVT::i32 ||
1171 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1172 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001173 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001174
Chris Lattnera801fced2006-11-08 02:15:41 +00001175 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001176 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001177
Owen Anderson9f944592009-08-11 20:47:22 +00001178 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1179 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001180 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001181 return true;
1182 }
1183 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001184
Chris Lattnera801fced2006-11-08 02:15:41 +00001185 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001186 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001187 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001188 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1189 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001190 Base = N;
1191 return true; // [r+0]
1192}
1193
1194/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1195/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001196bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1197 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001198 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001199 // Check to see if we can easily represent this as an [r+r] address. This
1200 // will fail if it thinks that the address is more profitably represented as
1201 // reg+imm, e.g. where imm = 0.
1202 if (SelectAddressRegReg(N, Base, Index, DAG))
1203 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001204
Chris Lattnera801fced2006-11-08 02:15:41 +00001205 // If the operand is an addition, always emit this as [r+r], since this is
1206 // better (for code size, and execution, as the memop does the add for free)
1207 // than emitting an explicit add.
1208 if (N.getOpcode() == ISD::ADD) {
1209 Base = N.getOperand(0);
1210 Index = N.getOperand(1);
1211 return true;
1212 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001213
Chris Lattnera801fced2006-11-08 02:15:41 +00001214 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkelf70c41e2013-03-21 23:45:03 +00001215 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1216 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001217 Index = N;
1218 return true;
1219}
1220
Chris Lattnera801fced2006-11-08 02:15:41 +00001221/// getPreIndexedAddressParts - returns true by value, base pointer and
1222/// offset pointer and addressing mode by reference if the node's address
1223/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001224bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1225 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001226 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001227 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001228 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001229
Ulrich Weigande90b0222013-03-22 14:58:48 +00001230 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001231 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001232 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001233 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001234 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1235 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001236 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001237 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001238 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001239 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001240 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001241 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001242 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001243 } else
1244 return false;
1245
Chris Lattner68371252006-11-14 01:38:31 +00001246 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands13237ac2008-06-06 12:08:01 +00001247 if (VT.isVector())
Chris Lattner68371252006-11-14 01:38:31 +00001248 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001249
Ulrich Weigande90b0222013-03-22 14:58:48 +00001250 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1251
1252 // Common code will reject creating a pre-inc form if the base pointer
1253 // is a frame index, or if N is a store and the base pointer is either
1254 // the same as or a predecessor of the value being stored. Check for
1255 // those situations here, and try with swapped Base/Offset instead.
1256 bool Swap = false;
1257
1258 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1259 Swap = true;
1260 else if (!isLoad) {
1261 SDValue Val = cast<StoreSDNode>(N)->getValue();
1262 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1263 Swap = true;
1264 }
1265
1266 if (Swap)
1267 std::swap(Base, Offset);
1268
Hal Finkelca542be2012-06-20 15:43:03 +00001269 AM = ISD::PRE_INC;
1270 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001271 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001272
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001273 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001274 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001275 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001276 return false;
1277 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001278 // LDU/STU need an address with at least 4-byte alignment.
1279 if (Alignment < 4)
1280 return false;
1281
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001282 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001283 return false;
1284 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001285
Chris Lattnerb314b152006-11-11 00:08:42 +00001286 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001287 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1288 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001289 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001290 LD->getExtensionType() == ISD::SEXTLOAD &&
1291 isa<ConstantSDNode>(Offset))
1292 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001293 }
1294
Chris Lattnerce645542006-11-10 02:08:47 +00001295 AM = ISD::PRE_INC;
1296 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001297}
1298
1299//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001300// LowerOperation implementation
1301//===----------------------------------------------------------------------===//
1302
Chris Lattneredb9d842010-11-15 02:46:57 +00001303/// GetLabelAccessInfo - Return true if we should reference labels using a
1304/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1305static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Chris Lattnerdd6df842010-11-15 03:13:19 +00001306 unsigned &LoOpFlags, const GlobalValue *GV = 0) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001307 HiOpFlags = PPCII::MO_HA;
1308 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001309
Chris Lattneredb9d842010-11-15 02:46:57 +00001310 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1311 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peck527da1b2010-11-23 03:31:01 +00001312 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattneredb9d842010-11-15 02:46:57 +00001313 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattnerdd6df842010-11-15 03:13:19 +00001314 if (isPIC) {
1315 HiOpFlags |= PPCII::MO_PIC_FLAG;
1316 LoOpFlags |= PPCII::MO_PIC_FLAG;
1317 }
1318
1319 // If this is a reference to a global value that requires a non-lazy-ptr, make
1320 // sure that instruction lowering adds it.
1321 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1322 HiOpFlags |= PPCII::MO_NLP_FLAG;
1323 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001324
Chris Lattnerdd6df842010-11-15 03:13:19 +00001325 if (GV->hasHiddenVisibility()) {
1326 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1327 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1328 }
1329 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001330
Chris Lattneredb9d842010-11-15 02:46:57 +00001331 return isPIC;
1332}
1333
1334static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1335 SelectionDAG &DAG) {
1336 EVT PtrVT = HiPart.getValueType();
1337 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001338 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001339
1340 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1341 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001342
Chris Lattneredb9d842010-11-15 02:46:57 +00001343 // With PIC, the first instruction is actually "GR+hi(&G)".
1344 if (isPIC)
1345 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1346 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001347
Chris Lattneredb9d842010-11-15 02:46:57 +00001348 // Generate non-pic code that has direct accesses to the constant pool.
1349 // The address of the global is just (hi(&g)+lo(&g)).
1350 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1351}
1352
Scott Michelcf0da6c2009-02-17 22:15:04 +00001353SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001354 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001355 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001356 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001357 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001358
Roman Divackyace47072012-08-24 16:26:02 +00001359 // 64-bit SVR4 ABI code is always position-independent.
1360 // The actual address of the GlobalValue is stored in the TOC.
1361 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1362 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001363 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001364 DAG.getRegister(PPC::X2, MVT::i64));
1365 }
1366
Chris Lattneredb9d842010-11-15 02:46:57 +00001367 unsigned MOHiFlag, MOLoFlag;
1368 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1369 SDValue CPIHi =
1370 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1371 SDValue CPILo =
1372 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1373 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001374}
1375
Dan Gohman21cea8a2010-04-17 15:26:15 +00001376SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001377 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001378 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001379
Roman Divackyace47072012-08-24 16:26:02 +00001380 // 64-bit SVR4 ABI code is always position-independent.
1381 // The actual address of the GlobalValue is stored in the TOC.
1382 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1383 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001384 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001385 DAG.getRegister(PPC::X2, MVT::i64));
1386 }
1387
Chris Lattneredb9d842010-11-15 02:46:57 +00001388 unsigned MOHiFlag, MOLoFlag;
1389 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1390 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1391 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1392 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001393}
1394
Dan Gohman21cea8a2010-04-17 15:26:15 +00001395SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1396 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001397 EVT PtrVT = Op.getValueType();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001398
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001399 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peck527da1b2010-11-23 03:31:01 +00001400
Chris Lattneredb9d842010-11-15 02:46:57 +00001401 unsigned MOHiFlag, MOLoFlag;
1402 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001403 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1404 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001405 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1406}
1407
Roman Divackye3f15c982012-06-04 17:36:38 +00001408SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1409 SelectionDAG &DAG) const {
1410
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001411 // FIXME: TLS addresses currently use medium model code sequences,
1412 // which is the most useful form. Eventually support for small and
1413 // large models could be added if users need it, at the cost of
1414 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001415 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001416 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001417 const GlobalValue *GV = GA->getGlobal();
1418 EVT PtrVT = getPointerTy();
1419 bool is64bit = PPCSubTarget.isPPC64();
1420
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001421 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001422
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001423 if (Model == TLSModel::LocalExec) {
1424 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001425 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001426 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001427 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001428 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1429 is64bit ? MVT::i64 : MVT::i32);
1430 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1431 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1432 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001433
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001434 if (!is64bit)
1435 llvm_unreachable("only local-exec is currently supported for ppc32");
1436
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001437 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001438 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001439 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1440 PPCII::MO_TLS);
Bill Schmidt732eb912012-12-13 18:45:54 +00001441 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001442 SDValue TPOffsetHi = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1443 PtrVT, GOTReg, TGA);
1444 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
1445 PtrVT, TGA, TPOffsetHi);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001446 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001447 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001448
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001449 if (Model == TLSModel::GeneralDynamic) {
1450 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1451 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1452 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1453 GOTReg, TGA);
1454 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1455 GOTEntryHi, TGA);
1456
1457 // We need a chain node, and don't have one handy. The underlying
1458 // call has no side effects, so using the function entry node
1459 // suffices.
1460 SDValue Chain = DAG.getEntryNode();
1461 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1462 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1463 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1464 PtrVT, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001465 // The return value from GET_TLS_ADDR really is in X3 already, but
1466 // some hacks are needed here to tie everything together. The extra
1467 // copies dissolve during subsequent transforms.
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001468 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1469 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1470 }
1471
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001472 if (Model == TLSModel::LocalDynamic) {
1473 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1474 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1475 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1476 GOTReg, TGA);
1477 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1478 GOTEntryHi, TGA);
1479
1480 // We need a chain node, and don't have one handy. The underlying
1481 // call has no side effects, so using the function entry node
1482 // suffices.
1483 SDValue Chain = DAG.getEntryNode();
1484 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1485 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1486 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1487 PtrVT, ParmReg, TGA);
1488 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1489 // some hacks are needed here to tie everything together. The extra
1490 // copies dissolve during subsequent transforms.
1491 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1492 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt9ed4dbc2012-12-13 20:57:10 +00001493 Chain, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001494 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1495 }
1496
1497 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00001498}
1499
Chris Lattneredb9d842010-11-15 02:46:57 +00001500SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1501 SelectionDAG &DAG) const {
1502 EVT PtrVT = Op.getValueType();
1503 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001504 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00001505 const GlobalValue *GV = GSDN->getGlobal();
1506
Chris Lattneredb9d842010-11-15 02:46:57 +00001507 // 64-bit SVR4 ABI code is always position-independent.
1508 // The actual address of the GlobalValue is stored in the TOC.
1509 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1510 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1511 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1512 DAG.getRegister(PPC::X2, MVT::i64));
1513 }
1514
Chris Lattnerdd6df842010-11-15 03:13:19 +00001515 unsigned MOHiFlag, MOLoFlag;
1516 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00001517
Chris Lattnerdd6df842010-11-15 03:13:19 +00001518 SDValue GAHi =
1519 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1520 SDValue GALo =
1521 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00001522
Chris Lattnerdd6df842010-11-15 03:13:19 +00001523 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00001524
Chris Lattnerdd6df842010-11-15 03:13:19 +00001525 // If the global reference is actually to a non-lazy-pointer, we have to do an
1526 // extra load to get the address of the global.
1527 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1528 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001529 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00001530 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00001531}
1532
Dan Gohman21cea8a2010-04-17 15:26:15 +00001533SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00001534 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001535 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001536
Chris Lattner4211ca92006-04-14 06:01:58 +00001537 // If we're comparing for equality to zero, expose the fact that this is
1538 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1539 // fold the new nodes.
1540 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1541 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001542 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001543 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001544 if (VT.bitsLT(MVT::i32)) {
1545 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001546 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001547 }
Duncan Sands13237ac2008-06-06 12:08:01 +00001548 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001549 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1550 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00001551 DAG.getConstant(Log2b, MVT::i32));
1552 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00001553 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001554 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00001555 // optimized. FIXME: revisit this when we can custom lower all setcc
1556 // optimizations.
1557 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001558 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001559 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001560
Chris Lattner4211ca92006-04-14 06:01:58 +00001561 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00001562 // by xor'ing the rhs with the lhs, which is faster than setting a
1563 // condition register, reading it back out, and masking the correct bit. The
1564 // normal approach here uses sub to do this instead of xor. Using xor exposes
1565 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001566 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00001567 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001568 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001569 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00001570 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001571 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00001572 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001573 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001574}
1575
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001576SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001577 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00001578 SDNode *Node = Op.getNode();
1579 EVT VT = Node->getValueType(0);
1580 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1581 SDValue InChain = Node->getOperand(0);
1582 SDValue VAListPtr = Node->getOperand(1);
1583 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001584 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001585
Roman Divacky4394e682011-06-28 15:30:42 +00001586 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1587
1588 // gpr_index
1589 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1590 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1591 false, false, 0);
1592 InChain = GprIndex.getValue(1);
1593
1594 if (VT == MVT::i64) {
1595 // Check if GprIndex is even
1596 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1597 DAG.getConstant(1, MVT::i32));
1598 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1599 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1600 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1601 DAG.getConstant(1, MVT::i32));
1602 // Align GprIndex to be even if it isn't
1603 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1604 GprIndex);
1605 }
1606
1607 // fpr index is 1 byte after gpr
1608 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1609 DAG.getConstant(1, MVT::i32));
1610
1611 // fpr
1612 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1613 FprPtr, MachinePointerInfo(SV), MVT::i8,
1614 false, false, 0);
1615 InChain = FprIndex.getValue(1);
1616
1617 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1618 DAG.getConstant(8, MVT::i32));
1619
1620 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1621 DAG.getConstant(4, MVT::i32));
1622
1623 // areas
1624 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001625 MachinePointerInfo(), false, false,
1626 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001627 InChain = OverflowArea.getValue(1);
1628
1629 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001630 MachinePointerInfo(), false, false,
1631 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001632 InChain = RegSaveArea.getValue(1);
1633
1634 // select overflow_area if index > 8
1635 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1636 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1637
Roman Divacky4394e682011-06-28 15:30:42 +00001638 // adjustment constant gpr_index * 4/8
1639 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1640 VT.isInteger() ? GprIndex : FprIndex,
1641 DAG.getConstant(VT.isInteger() ? 4 : 8,
1642 MVT::i32));
1643
1644 // OurReg = RegSaveArea + RegConstant
1645 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1646 RegConstant);
1647
1648 // Floating types are 32 bytes into RegSaveArea
1649 if (VT.isFloatingPoint())
1650 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1651 DAG.getConstant(32, MVT::i32));
1652
1653 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1654 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1655 VT.isInteger() ? GprIndex : FprIndex,
1656 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1657 MVT::i32));
1658
1659 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1660 VT.isInteger() ? VAListPtr : FprPtr,
1661 MachinePointerInfo(SV),
1662 MVT::i8, false, false, 0);
1663
1664 // determine if we should load from reg_save_area or overflow_area
1665 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1666
1667 // increase overflow_area by 4/8 if gpr/fpr > 8
1668 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1669 DAG.getConstant(VT.isInteger() ? 4 : 8,
1670 MVT::i32));
1671
1672 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1673 OverflowAreaPlusN);
1674
1675 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1676 OverflowAreaPtr,
1677 MachinePointerInfo(),
1678 MVT::i32, false, false, 0);
1679
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00001680 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001681 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001682}
1683
Roman Divackyc3825df2013-07-25 21:36:47 +00001684SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
1685 const PPCSubtarget &Subtarget) const {
1686 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
1687
1688 // We have to copy the entire va_list struct:
1689 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
1690 return DAG.getMemcpy(Op.getOperand(0), Op,
1691 Op.getOperand(1), Op.getOperand(2),
1692 DAG.getConstant(12, MVT::i32), 8, false, true,
1693 MachinePointerInfo(), MachinePointerInfo());
1694}
1695
Duncan Sandsa0984362011-09-06 13:37:06 +00001696SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1697 SelectionDAG &DAG) const {
1698 return Op.getOperand(0);
1699}
1700
1701SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1702 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00001703 SDValue Chain = Op.getOperand(0);
1704 SDValue Trmp = Op.getOperand(1); // trampoline
1705 SDValue FPtr = Op.getOperand(2); // nested function
1706 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00001707 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00001708
Owen Anderson53aa7a92009-08-10 22:56:29 +00001709 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00001710 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00001711 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001712 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00001713 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00001714
Scott Michelcf0da6c2009-02-17 22:15:04 +00001715 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00001716 TargetLowering::ArgListEntry Entry;
1717
1718 Entry.Ty = IntPtrTy;
1719 Entry.Node = Trmp; Args.push_back(Entry);
1720
1721 // TrampSize == (isPPC64 ? 48 : 40);
1722 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00001723 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00001724 Args.push_back(Entry);
1725
1726 Entry.Node = FPtr; Args.push_back(Entry);
1727 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001728
Bill Wendling95e1af22008-09-17 00:30:57 +00001729 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Justin Holewinskiaa583972012-05-25 16:35:28 +00001730 TargetLowering::CallLoweringInfo CLI(Chain,
1731 Type::getVoidTy(*DAG.getContext()),
1732 false, false, false, false, 0,
1733 CallingConv::C,
Evan Cheng65f9d192012-02-28 18:51:51 +00001734 /*isTailCall=*/false,
Justin Holewinskiaa583972012-05-25 16:35:28 +00001735 /*doesNotRet=*/false,
1736 /*isReturnValueUsed=*/true,
Bill Wendling95e1af22008-09-17 00:30:57 +00001737 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00001738 Args, DAG, dl);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001739 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bill Wendling95e1af22008-09-17 00:30:57 +00001740
Duncan Sandsa0984362011-09-06 13:37:06 +00001741 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00001742}
1743
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001744SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001745 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001746 MachineFunction &MF = DAG.getMachineFunction();
1747 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1748
Andrew Trickef9de2a2013-05-25 02:42:55 +00001749 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001750
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001751 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001752 // vastart just stores the address of the VarArgsFrameIndex slot into the
1753 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001754 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00001755 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001756 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00001757 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1758 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00001759 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001760 }
1761
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001762 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001763 // We suppose the given va_list is already allocated.
1764 //
1765 // typedef struct {
1766 // char gpr; /* index into the array of 8 GPRs
1767 // * stored in the register save area
1768 // * gpr=0 corresponds to r3,
1769 // * gpr=1 to r4, etc.
1770 // */
1771 // char fpr; /* index into the array of 8 FPRs
1772 // * stored in the register save area
1773 // * fpr=0 corresponds to f1,
1774 // * fpr=1 to f2, etc.
1775 // */
1776 // char *overflow_arg_area;
1777 // /* location on stack that holds
1778 // * the next overflow argument
1779 // */
1780 // char *reg_save_area;
1781 // /* where r3:r10 and f1:f8 (if saved)
1782 // * are stored
1783 // */
1784 // } va_list[1];
1785
1786
Dan Gohman31ae5862010-04-17 14:41:14 +00001787 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1788 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001789
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001790
Owen Anderson53aa7a92009-08-10 22:56:29 +00001791 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001792
Dan Gohman31ae5862010-04-17 14:41:14 +00001793 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1794 PtrVT);
1795 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1796 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001797
Duncan Sands13237ac2008-06-06 12:08:01 +00001798 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001799 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001800
Duncan Sands13237ac2008-06-06 12:08:01 +00001801 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001802 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001803
1804 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001805 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001806
Dan Gohman2d489b52008-02-06 22:27:42 +00001807 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001808
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001809 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001810 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00001811 Op.getOperand(1),
1812 MachinePointerInfo(SV),
1813 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001814 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001815 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001816 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001817
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001818 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001819 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00001820 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1821 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00001822 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001823 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001824 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001825
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001826 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001827 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00001828 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1829 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001830 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001831 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001832 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001833
1834 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00001835 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1836 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001837 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001838
Chris Lattner4211ca92006-04-14 06:01:58 +00001839}
1840
Chris Lattner4f2e4e02007-03-06 00:59:59 +00001841#include "PPCGenCallingConv.inc"
1842
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001843// Function whose sole purpose is to kill compiler warnings
1844// stemming from unused functions included from PPCGenCallingConv.inc.
1845CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001846 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001847}
1848
Bill Schmidt230b4512013-06-12 16:39:22 +00001849bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1850 CCValAssign::LocInfo &LocInfo,
1851 ISD::ArgFlagsTy &ArgFlags,
1852 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001853 return true;
1854}
1855
Bill Schmidt230b4512013-06-12 16:39:22 +00001856bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
1857 MVT &LocVT,
1858 CCValAssign::LocInfo &LocInfo,
1859 ISD::ArgFlagsTy &ArgFlags,
1860 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00001861 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001862 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1863 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1864 };
1865 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00001866
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001867 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1868
1869 // Skip one register if the first unallocated register has an even register
1870 // number and there are still argument registers available which have not been
1871 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1872 // need to skip a register if RegNum is odd.
1873 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1874 State.AllocateReg(ArgRegs[RegNum]);
1875 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001876
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001877 // Always return false here, as this function only makes sure that the first
1878 // unallocated register has an odd register number and does not actually
1879 // allocate a register for the current argument.
1880 return false;
1881}
1882
Bill Schmidt230b4512013-06-12 16:39:22 +00001883bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
1884 MVT &LocVT,
1885 CCValAssign::LocInfo &LocInfo,
1886 ISD::ArgFlagsTy &ArgFlags,
1887 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00001888 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001889 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1890 PPC::F8
1891 };
1892
1893 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00001894
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001895 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1896
1897 // If there is only one Floating-point register left we need to put both f64
1898 // values of a split ppc_fp128 value on the stack.
1899 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1900 State.AllocateReg(ArgRegs[RegNum]);
1901 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001902
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001903 // Always return false here, as this function only makes sure that the two f64
1904 // values a ppc_fp128 value is split into are both passed in registers or both
1905 // passed on the stack and does not actually allocate a register for the
1906 // current argument.
1907 return false;
1908}
1909
Chris Lattner43df5b32007-02-25 05:34:32 +00001910/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001911/// on Darwin.
Craig Topperca658c22012-03-11 07:16:55 +00001912static const uint16_t *GetFPR() {
1913 static const uint16_t FPR[] = {
Chris Lattner43df5b32007-02-25 05:34:32 +00001914 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001915 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner43df5b32007-02-25 05:34:32 +00001916 };
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001917
Chris Lattner43df5b32007-02-25 05:34:32 +00001918 return FPR;
1919}
1920
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00001921/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1922/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001923static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00001924 unsigned PtrByteSize) {
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00001925 unsigned ArgSize = ArgVT.getSizeInBits()/8;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00001926 if (Flags.isByVal())
1927 ArgSize = Flags.getByValSize();
1928 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1929
1930 return ArgSize;
1931}
1932
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001933SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001934PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001935 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001936 const SmallVectorImpl<ISD::InputArg>
1937 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001938 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001939 SmallVectorImpl<SDValue> &InVals)
1940 const {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00001941 if (PPCSubTarget.isSVR4ABI()) {
1942 if (PPCSubTarget.isPPC64())
1943 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
1944 dl, DAG, InVals);
1945 else
1946 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
1947 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00001948 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00001949 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
1950 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001951 }
1952}
1953
1954SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00001955PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001956 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001957 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001958 const SmallVectorImpl<ISD::InputArg>
1959 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001960 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001961 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001962
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001963 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001964 // +-----------------------------------+
1965 // +--> | Back chain |
1966 // | +-----------------------------------+
1967 // | | Floating-point register save area |
1968 // | +-----------------------------------+
1969 // | | General register save area |
1970 // | +-----------------------------------+
1971 // | | CR save word |
1972 // | +-----------------------------------+
1973 // | | VRSAVE save word |
1974 // | +-----------------------------------+
1975 // | | Alignment padding |
1976 // | +-----------------------------------+
1977 // | | Vector register save area |
1978 // | +-----------------------------------+
1979 // | | Local variable space |
1980 // | +-----------------------------------+
1981 // | | Parameter list area |
1982 // | +-----------------------------------+
1983 // | | LR save word |
1984 // | +-----------------------------------+
1985 // SP--> +--- | Back chain |
1986 // +-----------------------------------+
1987 //
1988 // Specifications:
1989 // System V Application Binary Interface PowerPC Processor Supplement
1990 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00001991
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001992 MachineFunction &MF = DAG.getMachineFunction();
1993 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00001994 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001995
Owen Anderson53aa7a92009-08-10 22:56:29 +00001996 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001997 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001998 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
1999 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002000 unsigned PtrByteSize = 4;
2001
2002 // Assign locations to all of the incoming arguments.
2003 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002004 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002005 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002006
2007 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002008 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002009
Bill Schmidtef17c142013-02-06 17:33:58 +00002010 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002011
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002012 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2013 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002014
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002015 // Arguments stored in registers.
2016 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002017 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002018 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002019
Owen Anderson9f944592009-08-11 20:47:22 +00002020 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002021 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002022 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Owen Anderson9f944592009-08-11 20:47:22 +00002023 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002024 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002025 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002026 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00002027 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002028 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002029 case MVT::f64:
Craig Topperabadc662012-04-20 06:31:50 +00002030 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002031 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002032 case MVT::v16i8:
2033 case MVT::v8i16:
2034 case MVT::v4i32:
2035 case MVT::v4f32:
Craig Topperabadc662012-04-20 06:31:50 +00002036 RC = &PPC::VRRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002037 break;
2038 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002039
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002040 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002041 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002042 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, ValVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002043
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002044 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002045 } else {
2046 // Argument stored in memory.
2047 assert(VA.isMemLoc());
2048
2049 unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8;
2050 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002051 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002052
2053 // Create load nodes to retrieve arguments from the stack.
2054 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002055 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2056 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002057 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002058 }
2059 }
2060
2061 // Assign locations to all of the incoming aggregate by value arguments.
2062 // Aggregates passed by value are stored in the local variable space of the
2063 // caller's stack frame, right above the parameter list area.
2064 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002065 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002066 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002067
2068 // Reserve stack space for the allocations in CCInfo.
2069 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2070
Bill Schmidtef17c142013-02-06 17:33:58 +00002071 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002072
2073 // Area that is at least reserved in the caller of this function.
2074 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00002075
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002076 // Set the size that is at least reserved in caller of this function. Tail
2077 // call optimized function's reserved stack space needs to be aligned so that
2078 // taking the difference between two stack areas will result in an aligned
2079 // stack.
2080 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2081
2082 MinReservedArea =
2083 std::max(MinReservedArea,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002084 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peck527da1b2010-11-23 03:31:01 +00002085
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002086 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002087 getStackAlignment();
2088 unsigned AlignMask = TargetAlign-1;
2089 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peck527da1b2010-11-23 03:31:01 +00002090
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002091 FI->setMinReservedArea(MinReservedArea);
2092
2093 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002094
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002095 // If the function takes variable number of arguments, make a frame index for
2096 // the start of the first vararg value... for expansion of llvm.va_start.
2097 if (isVarArg) {
Craig Topperbef78fc2012-03-11 07:57:25 +00002098 static const uint16_t GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002099 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2100 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2101 };
2102 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2103
Craig Topperbef78fc2012-03-11 07:57:25 +00002104 static const uint16_t FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002105 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2106 PPC::F8
2107 };
2108 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2109
Dan Gohman31ae5862010-04-17 14:41:14 +00002110 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2111 NumGPArgRegs));
2112 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2113 NumFPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002114
2115 // Make room for NumGPArgRegs and NumFPArgRegs.
2116 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson9f944592009-08-11 20:47:22 +00002117 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002118
Dan Gohman31ae5862010-04-17 14:41:14 +00002119 FuncInfo->setVarArgsStackOffset(
2120 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002121 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002122
Dan Gohman31ae5862010-04-17 14:41:14 +00002123 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2124 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002125
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002126 // The fixed integer arguments of a variadic function are stored to the
2127 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2128 // the result of va_next.
2129 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2130 // Get an existing live-in vreg, or add a new one.
2131 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2132 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002133 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002134
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002135 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002136 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2137 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002138 MemOps.push_back(Store);
2139 // Increment the address by four for the next argument to store
2140 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2141 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2142 }
2143
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002144 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2145 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002146 // The double arguments are stored to the VarArgsFrameIndex
2147 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002148 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2149 // Get an existing live-in vreg, or add a new one.
2150 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2151 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002152 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002153
Owen Anderson9f944592009-08-11 20:47:22 +00002154 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002155 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2156 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002157 MemOps.push_back(Store);
2158 // Increment the address by eight for the next argument to store
Owen Anderson9f944592009-08-11 20:47:22 +00002159 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002160 PtrVT);
2161 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2162 }
2163 }
2164
2165 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002166 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002167 MVT::Other, &MemOps[0], MemOps.size());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002168
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002169 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002170}
2171
Bill Schmidt57d6de52012-10-23 15:51:16 +00002172// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2173// value to MVT::i64 and then truncate to the correct register size.
2174SDValue
2175PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2176 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002177 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002178 if (Flags.isSExt())
2179 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2180 DAG.getValueType(ObjectVT));
2181 else if (Flags.isZExt())
2182 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2183 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002184
Bill Schmidt57d6de52012-10-23 15:51:16 +00002185 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
2186}
2187
2188// Set the size that is at least reserved in caller of this function. Tail
2189// call optimized functions' reserved stack space needs to be aligned so that
2190// taking the difference between two stack areas will result in an aligned
2191// stack.
2192void
2193PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2194 unsigned nAltivecParamsAtEnd,
2195 unsigned MinReservedArea,
2196 bool isPPC64) const {
2197 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2198 // Add the Altivec parameters at the end, if needed.
2199 if (nAltivecParamsAtEnd) {
2200 MinReservedArea = ((MinReservedArea+15)/16)*16;
2201 MinReservedArea += 16*nAltivecParamsAtEnd;
2202 }
2203 MinReservedArea =
2204 std::max(MinReservedArea,
2205 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2206 unsigned TargetAlign
2207 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2208 getStackAlignment();
2209 unsigned AlignMask = TargetAlign-1;
2210 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2211 FI->setMinReservedArea(MinReservedArea);
2212}
2213
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002214SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002215PPCTargetLowering::LowerFormalArguments_64SVR4(
2216 SDValue Chain,
2217 CallingConv::ID CallConv, bool isVarArg,
2218 const SmallVectorImpl<ISD::InputArg>
2219 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002220 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002221 SmallVectorImpl<SDValue> &InVals) const {
2222 // TODO: add description of PPC stack frame format, or at least some docs.
2223 //
2224 MachineFunction &MF = DAG.getMachineFunction();
2225 MachineFrameInfo *MFI = MF.getFrameInfo();
2226 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2227
2228 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2229 // Potential tail calls could cause overwriting of argument stack slots.
2230 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2231 (CallConv == CallingConv::Fast));
2232 unsigned PtrByteSize = 8;
2233
2234 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2235 // Area that is at least reserved in caller of this function.
2236 unsigned MinReservedArea = ArgOffset;
2237
2238 static const uint16_t GPR[] = {
2239 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2240 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2241 };
2242
2243 static const uint16_t *FPR = GetFPR();
2244
2245 static const uint16_t VR[] = {
2246 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2247 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2248 };
2249
2250 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2251 const unsigned Num_FPR_Regs = 13;
2252 const unsigned Num_VR_Regs = array_lengthof(VR);
2253
2254 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2255
2256 // Add DAG nodes to load the arguments or copy them out of registers. On
2257 // entry to a function on PPC, the arguments start after the linkage area,
2258 // although the first ones are often in registers.
2259
2260 SmallVector<SDValue, 8> MemOps;
2261 unsigned nAltivecParamsAtEnd = 0;
2262 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002263 unsigned CurArgIdx = 0;
2264 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002265 SDValue ArgVal;
2266 bool needsLoad = false;
2267 EVT ObjectVT = Ins[ArgNo].VT;
2268 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
2269 unsigned ArgSize = ObjSize;
2270 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt6631e942013-02-20 17:31:41 +00002271 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2272 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002273
2274 unsigned CurArgOffset = ArgOffset;
2275
2276 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2277 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2278 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
2279 if (isVarArg) {
2280 MinReservedArea = ((MinReservedArea+15)/16)*16;
2281 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2282 Flags,
2283 PtrByteSize);
2284 } else
2285 nAltivecParamsAtEnd++;
2286 } else
2287 // Calculate min reserved area.
2288 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2289 Flags,
2290 PtrByteSize);
2291
2292 // FIXME the codegen can be much improved in some cases.
2293 // We do not have to keep everything in memory.
2294 if (Flags.isByVal()) {
2295 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2296 ObjSize = Flags.getByValSize();
2297 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002298 // Empty aggregate parameters do not take up registers. Examples:
2299 // struct { } a;
2300 // union { } b;
2301 // int c[0];
2302 // etc. However, we have to provide a place-holder in InVals, so
2303 // pretend we have an 8-byte item at the current address for that
2304 // purpose.
2305 if (!ObjSize) {
2306 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2307 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2308 InVals.push_back(FIN);
2309 continue;
2310 }
Hal Finkel262a2242013-09-12 23:20:06 +00002311
2312 unsigned BVAlign = Flags.getByValAlign();
2313 if (BVAlign > 8) {
2314 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
2315 CurArgOffset = ArgOffset;
2316 }
2317
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002318 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt48081ca2012-10-16 13:30:53 +00002319 if (ObjSize < PtrByteSize)
2320 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002321 // The value of the object is its address.
2322 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2323 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2324 InVals.push_back(FIN);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002325
2326 if (ObjSize < 8) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002327 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002328 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002329 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002330 SDValue Store;
2331
2332 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2333 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2334 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2335 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
2336 MachinePointerInfo(FuncArg, CurArgOffset),
2337 ObjType, false, false, 0);
2338 } else {
2339 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2340 // store the whole register as-is to the parameter save area
2341 // slot. The address of the parameter was already calculated
2342 // above (InVals.push_back(FIN)) to be the right-justified
2343 // offset within the slot. For this store, we need a new
2344 // frame index that points at the beginning of the slot.
2345 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2346 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2347 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2348 MachinePointerInfo(FuncArg, ArgOffset),
2349 false, false, 0);
2350 }
2351
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002352 MemOps.push_back(Store);
2353 ++GPR_idx;
2354 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002355 // Whether we copied from a register or not, advance the offset
2356 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002357 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002358 continue;
2359 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002360
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002361 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2362 // Store whatever pieces of the object are in registers
2363 // to memory. ArgOffset will be the address of the beginning
2364 // of the object.
2365 if (GPR_idx != Num_GPR_Regs) {
2366 unsigned VReg;
2367 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2368 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2369 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2370 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002371 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002372 MachinePointerInfo(FuncArg, ArgOffset),
2373 false, false, 0);
2374 MemOps.push_back(Store);
2375 ++GPR_idx;
2376 ArgOffset += PtrByteSize;
2377 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00002378 ArgOffset += ArgSize - j;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002379 break;
2380 }
2381 }
2382 continue;
2383 }
2384
2385 switch (ObjectVT.getSimpleVT().SimpleTy) {
2386 default: llvm_unreachable("Unhandled argument type!");
2387 case MVT::i32:
2388 case MVT::i64:
2389 if (GPR_idx != Num_GPR_Regs) {
2390 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2391 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2392
Bill Schmidt57d6de52012-10-23 15:51:16 +00002393 if (ObjectVT == MVT::i32)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002394 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2395 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002396 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002397
2398 ++GPR_idx;
2399 } else {
2400 needsLoad = true;
2401 ArgSize = PtrByteSize;
2402 }
2403 ArgOffset += 8;
2404 break;
2405
2406 case MVT::f32:
2407 case MVT::f64:
2408 // Every 8 bytes of argument space consumes one of the GPRs available for
2409 // argument passing.
2410 if (GPR_idx != Num_GPR_Regs) {
2411 ++GPR_idx;
2412 }
2413 if (FPR_idx != Num_FPR_Regs) {
2414 unsigned VReg;
2415
2416 if (ObjectVT == MVT::f32)
2417 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2418 else
2419 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
2420
2421 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2422 ++FPR_idx;
2423 } else {
2424 needsLoad = true;
Bill Schmidt22162472012-10-11 15:38:20 +00002425 ArgSize = PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002426 }
2427
2428 ArgOffset += 8;
2429 break;
2430 case MVT::v4f32:
2431 case MVT::v4i32:
2432 case MVT::v8i16:
2433 case MVT::v16i8:
2434 // Note that vector arguments in registers don't reserve stack space,
2435 // except in varargs functions.
2436 if (VR_idx != Num_VR_Regs) {
2437 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
2438 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2439 if (isVarArg) {
2440 while ((ArgOffset % 16) != 0) {
2441 ArgOffset += PtrByteSize;
2442 if (GPR_idx != Num_GPR_Regs)
2443 GPR_idx++;
2444 }
2445 ArgOffset += 16;
2446 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2447 }
2448 ++VR_idx;
2449 } else {
2450 // Vectors are aligned.
2451 ArgOffset = ((ArgOffset+15)/16)*16;
2452 CurArgOffset = ArgOffset;
2453 ArgOffset += 16;
2454 needsLoad = true;
2455 }
2456 break;
2457 }
2458
2459 // We need to load the argument to a virtual register if we determined
2460 // above that we ran out of physical registers of the appropriate type.
2461 if (needsLoad) {
2462 int FI = MFI->CreateFixedObject(ObjSize,
2463 CurArgOffset + (ArgSize - ObjSize),
2464 isImmutable);
2465 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2466 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2467 false, false, false, 0);
2468 }
2469
2470 InVals.push_back(ArgVal);
2471 }
2472
2473 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002474 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002475 // taking the difference between two stack areas will result in an aligned
2476 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002477 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002478
2479 // If the function takes variable number of arguments, make a frame index for
2480 // the start of the first vararg value... for expansion of llvm.va_start.
2481 if (isVarArg) {
2482 int Depth = ArgOffset;
2483
2484 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00002485 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002486 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2487
2488 // If this function is vararg, store any remaining integer argument regs
2489 // to their spots on the stack so that they may be loaded by deferencing the
2490 // result of va_next.
2491 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2492 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2493 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2494 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2495 MachinePointerInfo(), false, false, 0);
2496 MemOps.push_back(Store);
2497 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00002498 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002499 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2500 }
2501 }
2502
2503 if (!MemOps.empty())
2504 Chain = DAG.getNode(ISD::TokenFactor, dl,
2505 MVT::Other, &MemOps[0], MemOps.size());
2506
2507 return Chain;
2508}
2509
2510SDValue
2511PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002512 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002513 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002514 const SmallVectorImpl<ISD::InputArg>
2515 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002516 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002517 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002518 // TODO: add description of PPC stack frame format, or at least some docs.
2519 //
2520 MachineFunction &MF = DAG.getMachineFunction();
2521 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002522 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002523
Owen Anderson53aa7a92009-08-10 22:56:29 +00002524 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002525 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002526 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002527 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2528 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00002529 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey48850c12006-11-16 22:43:37 +00002530
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002531 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002532 // Area that is at least reserved in caller of this function.
2533 unsigned MinReservedArea = ArgOffset;
2534
Craig Topperca658c22012-03-11 07:16:55 +00002535 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002536 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2537 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2538 };
Craig Topperca658c22012-03-11 07:16:55 +00002539 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00002540 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2541 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2542 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00002543
Craig Topperca658c22012-03-11 07:16:55 +00002544 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002545
Craig Topperca658c22012-03-11 07:16:55 +00002546 static const uint16_t VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002547 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2548 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2549 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00002550
Owen Andersone2f23a32007-09-07 04:06:50 +00002551 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002552 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00002553 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00002554
2555 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002556
Craig Topperca658c22012-03-11 07:16:55 +00002557 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002558
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002559 // In 32-bit non-varargs functions, the stack space for vectors is after the
2560 // stack space for non-vectors. We do not use this space unless we have
2561 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00002562 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002563 // that out...for the pathological case, compute VecArgOffset as the
2564 // start of the vector parameter area. Computing VecArgOffset is the
2565 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002566 unsigned VecArgOffset = ArgOffset;
2567 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002568 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002569 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002570 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002571 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002572
Duncan Sandsd97eea32008-03-21 09:14:45 +00002573 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002574 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00002575 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002576 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002577 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2578 VecArgOffset += ArgSize;
2579 continue;
2580 }
2581
Owen Anderson9f944592009-08-11 20:47:22 +00002582 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002583 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002584 case MVT::i32:
2585 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002586 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002587 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002588 case MVT::i64: // PPC64
2589 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002590 // FIXME: We are guaranteed to be !isPPC64 at this point.
2591 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002592 VecArgOffset += 8;
2593 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002594 case MVT::v4f32:
2595 case MVT::v4i32:
2596 case MVT::v8i16:
2597 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002598 // Nothing to do, we're only looking at Nonvector args here.
2599 break;
2600 }
2601 }
2602 }
2603 // We've found where the vector parameter area in memory is. Skip the
2604 // first 12 parameters; these don't use that memory.
2605 VecArgOffset = ((VecArgOffset+15)/16)*16;
2606 VecArgOffset += 12*16;
2607
Chris Lattner4302e8f2006-05-16 18:18:50 +00002608 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00002609 // entry to a function on PPC, the arguments start after the linkage area,
2610 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00002611
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002612 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002613 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00002614 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002615 unsigned CurArgIdx = 0;
2616 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002617 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002618 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002619 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00002620 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00002621 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002622 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002623 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2624 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002625
Chris Lattner318f0d22006-05-16 18:51:52 +00002626 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002627
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002628 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002629 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2630 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002631 if (isVarArg || isPPC64) {
2632 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002633 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002634 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002635 PtrByteSize);
2636 } else nAltivecParamsAtEnd++;
2637 } else
2638 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002639 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002640 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002641 PtrByteSize);
2642
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002643 // FIXME the codegen can be much improved in some cases.
2644 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002645 if (Flags.isByVal()) {
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002646 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002647 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002648 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002649 // Objects of size 1 and 2 are right justified, everything else is
2650 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00002651 if (ObjSize==1 || ObjSize==2) {
2652 CurArgOffset = CurArgOffset + (4 - ObjSize);
2653 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002654 // The value of the object is its address.
Evan Cheng0664a672010-07-03 00:40:23 +00002655 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002656 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002657 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002658 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00002659 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002660 unsigned VReg;
2661 if (isPPC64)
2662 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2663 else
2664 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002665 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002666 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002667 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Roman Divackyca103892012-09-24 20:47:19 +00002668 MachinePointerInfo(FuncArg,
2669 CurArgOffset),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002670 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00002671 MemOps.push_back(Store);
2672 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00002673 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002674
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002675 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00002676
Dale Johannesen21a8f142008-03-08 01:41:42 +00002677 continue;
2678 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002679 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2680 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002681 // to memory. ArgOffset will be the address of the beginning
2682 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002683 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002684 unsigned VReg;
2685 if (isPPC64)
2686 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2687 else
2688 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00002689 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002690 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002691 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002692 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Roman Divackyca103892012-09-24 20:47:19 +00002693 MachinePointerInfo(FuncArg, ArgOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002694 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002695 MemOps.push_back(Store);
2696 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002697 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002698 } else {
2699 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2700 break;
2701 }
2702 }
2703 continue;
2704 }
2705
Owen Anderson9f944592009-08-11 20:47:22 +00002706 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002707 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002708 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00002709 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00002710 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002711 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002712 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Bill Wendling968f32c2008-03-07 20:49:02 +00002713 ++GPR_idx;
2714 } else {
2715 needsLoad = true;
2716 ArgSize = PtrByteSize;
2717 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002718 // All int arguments reserve stack space in the Darwin ABI.
2719 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00002720 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002721 }
Bill Wendling968f32c2008-03-07 20:49:02 +00002722 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00002723 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00002724 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002725 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002726 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00002727
Bill Schmidt57d6de52012-10-23 15:51:16 +00002728 if (ObjectVT == MVT::i32)
Bill Wendling968f32c2008-03-07 20:49:02 +00002729 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00002730 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002731 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00002732
Chris Lattnerec78cad2006-06-26 22:48:35 +00002733 ++GPR_idx;
2734 } else {
2735 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00002736 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002737 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002738 // All int arguments reserve stack space in the Darwin ABI.
2739 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002740 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002741
Owen Anderson9f944592009-08-11 20:47:22 +00002742 case MVT::f32:
2743 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00002744 // Every 4 bytes of argument space consumes one of the GPRs available for
2745 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002746 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002747 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00002748 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002749 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00002750 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002751 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002752 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002753
Owen Anderson9f944592009-08-11 20:47:22 +00002754 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00002755 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002756 else
Devang Patelf3292b22011-02-21 23:21:26 +00002757 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002758
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002759 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002760 ++FPR_idx;
2761 } else {
2762 needsLoad = true;
2763 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002764
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002765 // All FP arguments reserve stack space in the Darwin ABI.
2766 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002767 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002768 case MVT::v4f32:
2769 case MVT::v4i32:
2770 case MVT::v8i16:
2771 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00002772 // Note that vector arguments in registers don't reserve stack space,
2773 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002774 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002775 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002776 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00002777 if (isVarArg) {
2778 while ((ArgOffset % 16) != 0) {
2779 ArgOffset += PtrByteSize;
2780 if (GPR_idx != Num_GPR_Regs)
2781 GPR_idx++;
2782 }
2783 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002784 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00002785 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002786 ++VR_idx;
2787 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002788 if (!isVarArg && !isPPC64) {
2789 // Vectors go after all the nonvectors.
2790 CurArgOffset = VecArgOffset;
2791 VecArgOffset += 16;
2792 } else {
2793 // Vectors are aligned.
2794 ArgOffset = ((ArgOffset+15)/16)*16;
2795 CurArgOffset = ArgOffset;
2796 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00002797 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002798 needsLoad = true;
2799 }
2800 break;
2801 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002802
Chris Lattner4302e8f2006-05-16 18:18:50 +00002803 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002804 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002805 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002806 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002807 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00002808 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002809 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002810 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002811 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002812 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002813
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002814 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002815 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002816
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002817 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002818 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002819 // taking the difference between two stack areas will result in an aligned
2820 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002821 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002822
Chris Lattner4302e8f2006-05-16 18:18:50 +00002823 // If the function takes variable number of arguments, make a frame index for
2824 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002825 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002826 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002827
Dan Gohman31ae5862010-04-17 14:41:14 +00002828 FuncInfo->setVarArgsFrameIndex(
2829 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002830 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00002831 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002832
Chris Lattner4302e8f2006-05-16 18:18:50 +00002833 // If this function is vararg, store any remaining integer argument regs
2834 // to their spots on the stack so that they may be loaded by deferencing the
2835 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002836 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00002837 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00002838
Chris Lattner2cca3852006-11-18 01:57:19 +00002839 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00002840 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00002841 else
Devang Patelf3292b22011-02-21 23:21:26 +00002842 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00002843
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002844 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002845 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2846 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002847 MemOps.push_back(Store);
2848 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002849 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00002850 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002851 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002852 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002853
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002854 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002855 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002856 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002857
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002858 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002859}
2860
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002861/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
2862/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002863static unsigned
2864CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2865 bool isPPC64,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002866 bool isVarArg,
2867 unsigned CC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002868 const SmallVectorImpl<ISD::OutputArg>
2869 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002870 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002871 unsigned &nAltivecParamsAtEnd) {
2872 // Count how many bytes are to be pushed on the stack, including the linkage
2873 // area, and parameter passing area. We start with 24/48 bytes, which is
2874 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002875 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002876 unsigned NumOps = Outs.size();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002877 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2878
2879 // Add up all the space actually used.
2880 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2881 // they all go in registers, but we must reserve stack space for them for
2882 // possible use by the caller. In varargs or 64-bit calls, parameters are
2883 // assigned stack space in order, with padding so Altivec parameters are
2884 // 16-byte aligned.
2885 nAltivecParamsAtEnd = 0;
2886 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002887 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002888 EVT ArgVT = Outs[i].VT;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002889 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002890 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2891 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002892 if (!isVarArg && !isPPC64) {
2893 // Non-varargs Altivec parameters go after all the non-Altivec
2894 // parameters; handle those later so we know how much padding we need.
2895 nAltivecParamsAtEnd++;
2896 continue;
2897 }
2898 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2899 NumBytes = ((NumBytes+15)/16)*16;
2900 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002901 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002902 }
2903
2904 // Allow for Altivec parameters at the end, if needed.
2905 if (nAltivecParamsAtEnd) {
2906 NumBytes = ((NumBytes+15)/16)*16;
2907 NumBytes += 16*nAltivecParamsAtEnd;
2908 }
2909
2910 // The prolog code of the callee may store up to 8 GPR argument registers to
2911 // the stack, allowing va_start to index over them in memory if its varargs.
2912 // Because we cannot tell if this is needed on the caller side, we have to
2913 // conservatively assume that it is needed. As such, make sure we have at
2914 // least enough stack space for the caller to store the 8 GPRs.
2915 NumBytes = std::max(NumBytes,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002916 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002917
2918 // Tail call needs the stack to be aligned.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002919 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
2920 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
2921 getFrameLowering()->getStackAlignment();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002922 unsigned AlignMask = TargetAlign-1;
2923 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2924 }
2925
2926 return NumBytes;
2927}
2928
2929/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00002930/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00002931static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002932 unsigned ParamSize) {
2933
Dale Johannesen86dcae12009-11-24 01:09:07 +00002934 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002935
2936 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
2937 unsigned CallerMinReservedArea = FI->getMinReservedArea();
2938 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
2939 // Remember only if the new adjustement is bigger.
2940 if (SPDiff < FI->getTailCallSPDelta())
2941 FI->setTailCallSPDelta(SPDiff);
2942
2943 return SPDiff;
2944}
2945
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002946/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2947/// for tail call optimization. Targets which want to do tail call
2948/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002949bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002950PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002951 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002952 bool isVarArg,
2953 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002954 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002955 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00002956 return false;
2957
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002958 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002959 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00002960 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002961
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002962 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00002963 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002964 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
2965 // Functions containing by val parameters are not supported.
2966 for (unsigned i = 0; i != Ins.size(); i++) {
2967 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2968 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002969 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002970
2971 // Non PIC/GOT tail calls are supported.
2972 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
2973 return true;
2974
2975 // At the moment we can only do local tail calls (in same module, hidden
2976 // or protected) if we are generating PIC.
2977 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2978 return G->getGlobal()->hasHiddenVisibility()
2979 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002980 }
2981
2982 return false;
2983}
2984
Chris Lattnereb755fc2006-05-17 19:00:46 +00002985/// isCallCompatibleAddress - Return the immediate to use if the specified
2986/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002987static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00002988 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2989 if (!C) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002990
Dan Gohmaneffb8942008-09-12 16:56:44 +00002991 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00002992 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00002993 SignExtend32<26>(Addr) != Addr)
Chris Lattnereb755fc2006-05-17 19:00:46 +00002994 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002995
Dan Gohmaneffb8942008-09-12 16:56:44 +00002996 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00002997 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00002998}
2999
Dan Gohmand78c4002008-05-13 00:00:25 +00003000namespace {
3001
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003002struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003003 SDValue Arg;
3004 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003005 int FrameIdx;
3006
3007 TailCallArgumentInfo() : FrameIdx(0) {}
3008};
3009
Dan Gohmand78c4002008-05-13 00:00:25 +00003010}
3011
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003012/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3013static void
3014StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003015 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003016 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3017 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003018 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003019 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003020 SDValue Arg = TailCallArgs[i].Arg;
3021 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003022 int FI = TailCallArgs[i].FrameIdx;
3023 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003024 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003025 MachinePointerInfo::getFixedStack(FI),
3026 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003027 }
3028}
3029
3030/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3031/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003032static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003033 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003034 SDValue Chain,
3035 SDValue OldRetAddr,
3036 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003037 int SPDiff,
3038 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003039 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003040 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003041 if (SPDiff) {
3042 // Calculate the new stack slot for the return address.
3043 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003044 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003045 isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003046 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003047 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003048 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003049 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003050 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003051 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003052 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003053
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003054 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3055 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003056 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003057 int NewFPLoc =
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003058 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene1fbe0542009-11-12 20:49:22 +00003059 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003060 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003061 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3062 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003063 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003064 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003065 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003066 }
3067 return Chain;
3068}
3069
3070/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3071/// the position of the argument.
3072static void
3073CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003074 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003075 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003076 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003077 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003078 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003079 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003080 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003081 TailCallArgumentInfo Info;
3082 Info.Arg = Arg;
3083 Info.FrameIdxOp = FIN;
3084 Info.FrameIdx = FI;
3085 TailCallArguments.push_back(Info);
3086}
3087
3088/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3089/// stack slot. Returns the chain as result and the loaded frame pointers in
3090/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003091SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003092 int SPDiff,
3093 SDValue Chain,
3094 SDValue &LROpOut,
3095 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003096 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003097 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003098 if (SPDiff) {
3099 // Load the LR and FP stack slot for later adjusting.
Owen Anderson9f944592009-08-11 20:47:22 +00003100 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003101 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003102 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003103 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003104 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003105
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003106 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3107 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003108 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003109 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003110 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003111 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003112 Chain = SDValue(FPOpOut.getNode(), 1);
3113 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003114 }
3115 return Chain;
3116}
3117
Dale Johannesen85d41a12008-03-04 23:17:14 +00003118/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003119/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003120/// specified by the specific parameter attribute. The copy will be passed as
3121/// a byval function parameter.
3122/// Sometimes what we are copying is the end of a larger object, the part that
3123/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003124static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003125CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003126 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003127 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003128 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003129 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Chris Lattner2510de22010-09-21 05:40:29 +00003130 false, false, MachinePointerInfo(0),
3131 MachinePointerInfo(0));
Dale Johannesen85d41a12008-03-04 23:17:14 +00003132}
Chris Lattner43df5b32007-02-25 05:34:32 +00003133
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003134/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3135/// tail calls.
3136static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003137LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3138 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003139 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003140 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3141 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003142 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003143 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003144 if (!isTailCall) {
3145 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003146 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003147 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003148 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003149 else
Owen Anderson9f944592009-08-11 20:47:22 +00003150 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003151 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003152 DAG.getConstant(ArgOffset, PtrVT));
3153 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003154 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3155 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003156 // Calculate and remember argument location.
3157 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3158 TailCallArguments);
3159}
3160
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003161static
3162void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003163 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003164 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003165 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003166 MachineFunction &MF = DAG.getMachineFunction();
3167
3168 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3169 // might overwrite each other in case of tail call optimization.
3170 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003171 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003172 InFlag = SDValue();
3173 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3174 MemOpChains2, dl);
3175 if (!MemOpChains2.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003176 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003177 &MemOpChains2[0], MemOpChains2.size());
3178
3179 // Store the return address to the appropriate stack slot.
3180 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3181 isPPC64, isDarwinABI, dl);
3182
3183 // Emit callseq_end just before tailcall node.
3184 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003185 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003186 InFlag = Chain.getValue(1);
3187}
3188
3189static
3190unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003191 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003192 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3193 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003194 const PPCSubtarget &PPCSubTarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003195
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003196 bool isPPC64 = PPCSubTarget.isPPC64();
3197 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3198
Owen Anderson53aa7a92009-08-10 22:56:29 +00003199 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003200 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003201 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003202
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003203 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003204
Torok Edwin31e90d22010-08-04 20:47:44 +00003205 bool needIndirectCall = true;
3206 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003207 // If this is an absolute destination address, use the munged value.
3208 Callee = SDValue(Dest, 0);
Torok Edwin31e90d22010-08-04 20:47:44 +00003209 needIndirectCall = false;
3210 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003211
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003212 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3213 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3214 // Use indirect calls for ALL functions calls in JIT mode, since the
3215 // far-call stubs may be outside relocation limits for a BL instruction.
3216 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3217 unsigned OpFlags = 0;
3218 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003219 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003220 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003221 (G->getGlobal()->isDeclaration() ||
3222 G->getGlobal()->isWeakForLinker())) {
3223 // PC-relative references to external symbols should go through $stub,
3224 // unless we're building with the leopard linker or later, which
3225 // automatically synthesizes these stubs.
3226 OpFlags = PPCII::MO_DARWIN_STUB;
3227 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003228
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003229 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3230 // every direct call is) turn it into a TargetGlobalAddress /
3231 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin31e90d22010-08-04 20:47:44 +00003232 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003233 Callee.getValueType(),
3234 0, OpFlags);
Torok Edwin31e90d22010-08-04 20:47:44 +00003235 needIndirectCall = false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003236 }
Torok Edwin31e90d22010-08-04 20:47:44 +00003237 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003238
Torok Edwin31e90d22010-08-04 20:47:44 +00003239 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003240 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003241
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003242 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003243 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003244 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003245 // PC-relative references to external symbols should go through $stub,
3246 // unless we're building with the leopard linker or later, which
3247 // automatically synthesizes these stubs.
3248 OpFlags = PPCII::MO_DARWIN_STUB;
3249 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003250
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003251 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3252 OpFlags);
3253 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003254 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003255
Torok Edwin31e90d22010-08-04 20:47:44 +00003256 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003257 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3258 // to do the call, we can't use PPCISD::CALL.
3259 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003260
3261 if (isSVR4ABI && isPPC64) {
3262 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3263 // entry point, but to the function descriptor (the function entry point
3264 // address is part of the function descriptor though).
3265 // The function descriptor is a three doubleword structure with the
3266 // following fields: function entry point, TOC base address and
3267 // environment pointer.
3268 // Thus for a call through a function pointer, the following actions need
3269 // to be performed:
3270 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003271 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003272 // 2. Load the address of the function entry point from the function
3273 // descriptor.
3274 // 3. Load the TOC of the callee from the function descriptor into r2.
3275 // 4. Load the environment pointer from the function descriptor into
3276 // r11.
3277 // 5. Branch to the function entry point address.
3278 // 6. On return of the callee, the TOC of the caller needs to be
3279 // restored (this is done in FinishCall()).
3280 //
3281 // All those operations are flagged together to ensure that no other
3282 // operations can be scheduled in between. E.g. without flagging the
3283 // operations together, a TOC access in the caller could be scheduled
3284 // between the load of the callee TOC and the branch to the callee, which
3285 // results in the TOC access going through the TOC of the callee instead
3286 // of going through the TOC of the caller, which leads to incorrect code.
3287
3288 // Load the address of the function entry point from the function
3289 // descriptor.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003290 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003291 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, MTCTROps,
3292 InFlag.getNode() ? 3 : 2);
3293 Chain = LoadFuncPtr.getValue(1);
3294 InFlag = LoadFuncPtr.getValue(2);
3295
3296 // Load environment pointer into r11.
3297 // Offset of the environment pointer within the function descriptor.
3298 SDValue PtrOff = DAG.getIntPtrConstant(16);
3299
3300 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3301 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3302 InFlag);
3303 Chain = LoadEnvPtr.getValue(1);
3304 InFlag = LoadEnvPtr.getValue(2);
3305
3306 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3307 InFlag);
3308 Chain = EnvVal.getValue(0);
3309 InFlag = EnvVal.getValue(1);
3310
3311 // Load TOC of the callee into r2. We are using a target-specific load
3312 // with r2 hard coded, because the result of a target-independent load
3313 // would never go directly into r2, since r2 is a reserved register (which
3314 // prevents the register allocator from allocating it), resulting in an
3315 // additional register being allocated and an unnecessary move instruction
3316 // being generated.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003317 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003318 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3319 Callee, InFlag);
3320 Chain = LoadTOCPtr.getValue(0);
3321 InFlag = LoadTOCPtr.getValue(1);
3322
3323 MTCTROps[0] = Chain;
3324 MTCTROps[1] = LoadFuncPtr;
3325 MTCTROps[2] = InFlag;
3326 }
3327
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003328 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
3329 2 + (InFlag.getNode() != 0));
3330 InFlag = Chain.getValue(1);
3331
3332 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00003333 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003334 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003335 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003336 CallOpc = PPCISD::BCTRL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003337 Callee.setNode(0);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003338 // Add use of X11 (holding environment pointer)
3339 if (isSVR4ABI && isPPC64)
3340 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003341 // Add CTR register as callee so a bctr can be emitted later.
3342 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00003343 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003344 }
3345
3346 // If this is a direct call, pass the chain and the callee.
3347 if (Callee.getNode()) {
3348 Ops.push_back(Chain);
3349 Ops.push_back(Callee);
3350 }
3351 // If this is a tail call add stack pointer delta.
3352 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00003353 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003354
3355 // Add argument registers to the end of the list so that they are known live
3356 // into the call.
3357 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3358 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3359 RegsToPass[i].second.getValueType()));
3360
3361 return CallOpc;
3362}
3363
Roman Divacky76293062012-09-18 16:47:58 +00003364static
3365bool isLocalCall(const SDValue &Callee)
3366{
3367 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00003368 return !G->getGlobal()->isDeclaration() &&
3369 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00003370 return false;
3371}
3372
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003373SDValue
3374PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003375 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003376 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003377 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003378 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003379
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003380 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003381 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003382 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003383 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003384
3385 // Copy all of the result registers out of their specified physreg.
3386 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3387 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003388 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00003389
3390 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3391 VA.getLocReg(), VA.getLocVT(), InFlag);
3392 Chain = Val.getValue(1);
3393 InFlag = Val.getValue(2);
3394
3395 switch (VA.getLocInfo()) {
3396 default: llvm_unreachable("Unknown loc info!");
3397 case CCValAssign::Full: break;
3398 case CCValAssign::AExt:
3399 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3400 break;
3401 case CCValAssign::ZExt:
3402 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3403 DAG.getValueType(VA.getValVT()));
3404 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3405 break;
3406 case CCValAssign::SExt:
3407 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3408 DAG.getValueType(VA.getValVT()));
3409 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3410 break;
3411 }
3412
3413 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003414 }
3415
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003416 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003417}
3418
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003419SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00003420PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003421 bool isTailCall, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003422 SelectionDAG &DAG,
3423 SmallVector<std::pair<unsigned, SDValue>, 8>
3424 &RegsToPass,
3425 SDValue InFlag, SDValue Chain,
3426 SDValue &Callee,
3427 int SPDiff, unsigned NumBytes,
3428 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003429 SmallVectorImpl<SDValue> &InVals) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003430 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003431 SmallVector<SDValue, 8> Ops;
3432 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3433 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003434 PPCSubTarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003435
Hal Finkel5ab37802012-08-28 02:10:27 +00003436 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3437 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3438 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3439
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003440 // When performing tail call optimization the callee pops its arguments off
3441 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00003442 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003443 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003444 (CallConv == CallingConv::Fast &&
3445 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003446
Roman Divackyef21be22012-03-06 16:41:49 +00003447 // Add a register mask operand representing the call-preserved registers.
3448 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3449 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3450 assert(Mask && "Missing call preserved mask for calling convention");
3451 Ops.push_back(DAG.getRegisterMask(Mask));
3452
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003453 if (InFlag.getNode())
3454 Ops.push_back(InFlag);
3455
3456 // Emit tail call.
3457 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003458 assert(((Callee.getOpcode() == ISD::Register &&
3459 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3460 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3461 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3462 isa<ConstantSDNode>(Callee)) &&
3463 "Expecting an global address, external symbol, absolute value or register");
3464
Owen Anderson9f944592009-08-11 20:47:22 +00003465 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003466 }
3467
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003468 // Add a NOP immediately after the branch instruction when using the 64-bit
3469 // SVR4 ABI. At link time, if caller and callee are in a different module and
3470 // thus have a different TOC, the call will be replaced with a call to a stub
3471 // function which saves the current TOC, loads the TOC of the callee and
3472 // branches to the callee. The NOP will be replaced with a load instruction
3473 // which restores the TOC of the caller from the TOC save slot of the current
3474 // stack frame. If caller and callee belong to the same module (and have the
3475 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00003476
3477 bool needsTOCRestore = false;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003478 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003479 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003480 // This is a call through a function pointer.
3481 // Restore the caller TOC from the save area into R2.
3482 // See PrepareCall() for more information about calls through function
3483 // pointers in the 64-bit SVR4 ABI.
3484 // We are using a target-specific load with r2 hard coded, because the
3485 // result of a target-independent load would never go directly into r2,
3486 // since r2 is a reserved register (which prevents the register allocator
3487 // from allocating it), resulting in an additional register being
3488 // allocated and an unnecessary move instruction being generated.
Hal Finkel51861b42012-03-31 14:45:15 +00003489 needsTOCRestore = true;
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003490 } else if ((CallOpc == PPCISD::CALL) && !isLocalCall(Callee)) {
Roman Divacky76293062012-09-18 16:47:58 +00003491 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003492 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller79fef932009-12-18 13:00:15 +00003493 }
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003494 }
3495
Hal Finkel51861b42012-03-31 14:45:15 +00003496 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
3497 InFlag = Chain.getValue(1);
3498
3499 if (needsTOCRestore) {
3500 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3501 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3502 InFlag = Chain.getValue(1);
3503 }
3504
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003505 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3506 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003507 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003508 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003509 InFlag = Chain.getValue(1);
3510
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003511 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3512 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003513}
3514
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003515SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00003516PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003517 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00003518 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00003519 SDLoc &dl = CLI.DL;
3520 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3521 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3522 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00003523 SDValue Chain = CLI.Chain;
3524 SDValue Callee = CLI.Callee;
3525 bool &isTailCall = CLI.IsTailCall;
3526 CallingConv::ID CallConv = CLI.CallConv;
3527 bool isVarArg = CLI.IsVarArg;
3528
Evan Cheng67a69dd2010-01-27 00:07:07 +00003529 if (isTailCall)
3530 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3531 Ins, DAG);
3532
Bill Schmidt57d6de52012-10-23 15:51:16 +00003533 if (PPCSubTarget.isSVR4ABI()) {
3534 if (PPCSubTarget.isPPC64())
3535 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3536 isTailCall, Outs, OutVals, Ins,
3537 dl, DAG, InVals);
3538 else
3539 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3540 isTailCall, Outs, OutVals, Ins,
3541 dl, DAG, InVals);
3542 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003543
Bill Schmidt57d6de52012-10-23 15:51:16 +00003544 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3545 isTailCall, Outs, OutVals, Ins,
3546 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003547}
3548
3549SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003550PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3551 CallingConv::ID CallConv, bool isVarArg,
3552 bool isTailCall,
3553 const SmallVectorImpl<ISD::OutputArg> &Outs,
3554 const SmallVectorImpl<SDValue> &OutVals,
3555 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003556 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003557 SmallVectorImpl<SDValue> &InVals) const {
3558 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003559 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003560
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003561 assert((CallConv == CallingConv::C ||
3562 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003563
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003564 unsigned PtrByteSize = 4;
3565
3566 MachineFunction &MF = DAG.getMachineFunction();
3567
3568 // Mark this function as potentially containing a function that contains a
3569 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3570 // and restoring the callers stack pointer in this functions epilog. This is
3571 // done because by tail calling the called function might overwrite the value
3572 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003573 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3574 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003575 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00003576
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003577 // Count how many bytes are to be pushed on the stack, including the linkage
3578 // area, parameter list area and the part of the local variable space which
3579 // contains copies of aggregates which are passed by value.
3580
3581 // Assign locations to all of the outgoing arguments.
3582 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003583 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003584 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003585
3586 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003587 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003588
3589 if (isVarArg) {
3590 // Handle fixed and variable vector arguments differently.
3591 // Fixed vector arguments go into registers as long as registers are
3592 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003593 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00003594
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003595 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003596 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003597 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003598 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00003599
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003600 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00003601 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3602 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003603 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00003604 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3605 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003606 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003607
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003608 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003609#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00003610 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00003611 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003612#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00003613 llvm_unreachable(0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003614 }
3615 }
3616 } else {
3617 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00003618 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003619 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003620
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003621 // Assign locations to all of the outgoing aggregate by value arguments.
3622 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003623 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003624 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003625
3626 // Reserve stack space for the allocations in CCInfo.
3627 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3628
Bill Schmidtef17c142013-02-06 17:33:58 +00003629 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003630
3631 // Size of the linkage area, parameter list area and the part of the local
3632 // space variable where copies of aggregates which are passed by value are
3633 // stored.
3634 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003635
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003636 // Calculate by how many bytes the stack has to be adjusted in case of tail
3637 // call optimization.
3638 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3639
3640 // Adjust the stack pointer for the new arguments...
3641 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003642 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3643 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003644 SDValue CallSeqStart = Chain;
3645
3646 // Load the return address and frame pointer so it can be moved somewhere else
3647 // later.
3648 SDValue LROp, FPOp;
3649 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3650 dl);
3651
3652 // Set up a copy of the stack pointer for use loading and storing any
3653 // arguments that may not fit in the registers available for argument
3654 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00003655 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00003656
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003657 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3658 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3659 SmallVector<SDValue, 8> MemOpChains;
3660
Roman Divacky71038e72011-08-30 17:04:16 +00003661 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003662 // Walk the register/memloc assignments, inserting copies/loads.
3663 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3664 i != e;
3665 ++i) {
3666 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003667 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003668 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00003669
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003670 if (Flags.isByVal()) {
3671 // Argument is an aggregate which is passed by value, thus we need to
3672 // create a copy of it in the local variable space of the current stack
3673 // frame (which is the stack frame of the caller) and pass the address of
3674 // this copy to the callee.
3675 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3676 CCValAssign &ByValVA = ByValArgLocs[j++];
3677 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00003678
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003679 // Memory reserved in the local variable space of the callers stack frame.
3680 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003681
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003682 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3683 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00003684
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003685 // Create a copy of the argument in the local area of the current
3686 // stack frame.
3687 SDValue MemcpyCall =
3688 CreateCopyOfByValArgument(Arg, PtrOff,
3689 CallSeqStart.getNode()->getOperand(0),
3690 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00003691
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003692 // This must go outside the CALLSEQ_START..END.
3693 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003694 CallSeqStart.getNode()->getOperand(1),
3695 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003696 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3697 NewCallSeqStart.getNode());
3698 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00003699
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003700 // Pass the address of the aggregate copy on the stack either in a
3701 // physical register or in the parameter list area of the current stack
3702 // frame to the callee.
3703 Arg = PtrOff;
3704 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003705
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003706 if (VA.isRegLoc()) {
Roman Divacky71038e72011-08-30 17:04:16 +00003707 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003708 // Put argument in a physical register.
3709 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3710 } else {
3711 // Put argument in the parameter list area of the current stack frame.
3712 assert(VA.isMemLoc());
3713 unsigned LocMemOffset = VA.getLocMemOffset();
3714
3715 if (!isTailCall) {
3716 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3717 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3718
3719 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00003720 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00003721 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003722 } else {
3723 // Calculate and remember argument location.
3724 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3725 TailCallArguments);
3726 }
3727 }
3728 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003729
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003730 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003731 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003732 &MemOpChains[0], MemOpChains.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00003733
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003734 // Build a sequence of copy-to-reg nodes chained together with token chain
3735 // and flag operands which copy the outgoing args into the appropriate regs.
3736 SDValue InFlag;
3737 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3738 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3739 RegsToPass[i].second, InFlag);
3740 InFlag = Chain.getValue(1);
3741 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003742
Hal Finkel5ab37802012-08-28 02:10:27 +00003743 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3744 // registers.
3745 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003746 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3747 SDValue Ops[] = { Chain, InFlag };
3748
Hal Finkel5ab37802012-08-28 02:10:27 +00003749 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003750 dl, VTs, Ops, InFlag.getNode() ? 2 : 1);
3751
Hal Finkel5ab37802012-08-28 02:10:27 +00003752 InFlag = Chain.getValue(1);
3753 }
3754
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003755 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003756 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3757 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003758
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003759 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3760 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3761 Ins, InVals);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003762}
3763
Bill Schmidt57d6de52012-10-23 15:51:16 +00003764// Copy an argument into memory, being careful to do this outside the
3765// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003766SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00003767PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3768 SDValue CallSeqStart,
3769 ISD::ArgFlagsTy Flags,
3770 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003771 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003772 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3773 CallSeqStart.getNode()->getOperand(0),
3774 Flags, DAG, dl);
3775 // The MEMCPY must go outside the CALLSEQ_START..END.
3776 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003777 CallSeqStart.getNode()->getOperand(1),
3778 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00003779 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3780 NewCallSeqStart.getNode());
3781 return NewCallSeqStart;
3782}
3783
3784SDValue
3785PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003786 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003787 bool isTailCall,
3788 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003789 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003790 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003791 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003792 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003793
Bill Schmidt57d6de52012-10-23 15:51:16 +00003794 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003795
Bill Schmidt57d6de52012-10-23 15:51:16 +00003796 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3797 unsigned PtrByteSize = 8;
3798
3799 MachineFunction &MF = DAG.getMachineFunction();
3800
3801 // Mark this function as potentially containing a function that contains a
3802 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3803 // and restoring the callers stack pointer in this functions epilog. This is
3804 // done because by tail calling the called function might overwrite the value
3805 // in this function's (MF) stack pointer stack slot 0(SP).
3806 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3807 CallConv == CallingConv::Fast)
3808 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3809
3810 unsigned nAltivecParamsAtEnd = 0;
3811
3812 // Count how many bytes are to be pushed on the stack, including the linkage
3813 // area, and parameter passing area. We start with at least 48 bytes, which
3814 // is reserved space for [SP][CR][LR][3 x unused].
3815 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
3816 // of this call.
3817 unsigned NumBytes =
3818 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
3819 Outs, OutVals, nAltivecParamsAtEnd);
3820
3821 // Calculate by how many bytes the stack has to be adjusted in case of tail
3822 // call optimization.
3823 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3824
3825 // To protect arguments on the stack from being clobbered in a tail call,
3826 // force all the loads to happen before doing any other lowering.
3827 if (isTailCall)
3828 Chain = DAG.getStackArgumentTokenFactor(Chain);
3829
3830 // Adjust the stack pointer for the new arguments...
3831 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003832 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3833 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003834 SDValue CallSeqStart = Chain;
3835
3836 // Load the return address and frame pointer so it can be move somewhere else
3837 // later.
3838 SDValue LROp, FPOp;
3839 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
3840 dl);
3841
3842 // Set up a copy of the stack pointer for use loading and storing any
3843 // arguments that may not fit in the registers available for argument
3844 // passing.
3845 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
3846
3847 // Figure out which arguments are going to go in registers, and which in
3848 // memory. Also, if this is a vararg function, floating point operations
3849 // must be stored to our stack, and loaded into integer regs as well, if
3850 // any integer regs are available for argument passing.
3851 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
3852 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
3853
3854 static const uint16_t GPR[] = {
3855 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3856 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3857 };
3858 static const uint16_t *FPR = GetFPR();
3859
3860 static const uint16_t VR[] = {
3861 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3862 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3863 };
3864 const unsigned NumGPRs = array_lengthof(GPR);
3865 const unsigned NumFPRs = 13;
3866 const unsigned NumVRs = array_lengthof(VR);
3867
3868 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3869 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3870
3871 SmallVector<SDValue, 8> MemOpChains;
3872 for (unsigned i = 0; i != NumOps; ++i) {
3873 SDValue Arg = OutVals[i];
3874 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3875
3876 // PtrOff will be used to store the current argument to the stack if a
3877 // register cannot be found for it.
3878 SDValue PtrOff;
3879
3880 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
3881
3882 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
3883
3884 // Promote integers to 64-bit values.
3885 if (Arg.getValueType() == MVT::i32) {
3886 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
3887 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3888 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
3889 }
3890
3891 // FIXME memcpy is used way more than necessary. Correctness first.
3892 // Note: "by value" is code for passing a structure by value, not
3893 // basic types.
3894 if (Flags.isByVal()) {
3895 // Note: Size includes alignment padding, so
3896 // struct x { short a; char b; }
3897 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
3898 // These are the proper values we need for right-justifying the
3899 // aggregate in a parameter register.
3900 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00003901
3902 // An empty aggregate parameter takes up no storage and no
3903 // registers.
3904 if (Size == 0)
3905 continue;
3906
Hal Finkel262a2242013-09-12 23:20:06 +00003907 unsigned BVAlign = Flags.getByValAlign();
3908 if (BVAlign > 8) {
3909 if (BVAlign % PtrByteSize != 0)
3910 llvm_unreachable(
3911 "ByVal alignment is not a multiple of the pointer size");
3912
3913 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
3914 }
3915
Bill Schmidt57d6de52012-10-23 15:51:16 +00003916 // All aggregates smaller than 8 bytes must be passed right-justified.
3917 if (Size==1 || Size==2 || Size==4) {
3918 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
3919 if (GPR_idx != NumGPRs) {
3920 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
3921 MachinePointerInfo(), VT,
3922 false, false, 0);
3923 MemOpChains.push_back(Load.getValue(1));
3924 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3925
3926 ArgOffset += PtrByteSize;
3927 continue;
3928 }
3929 }
3930
3931 if (GPR_idx == NumGPRs && Size < 8) {
3932 SDValue Const = DAG.getConstant(PtrByteSize - Size,
3933 PtrOff.getValueType());
3934 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3935 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3936 CallSeqStart,
3937 Flags, DAG, dl);
3938 ArgOffset += PtrByteSize;
3939 continue;
3940 }
3941 // Copy entire object into memory. There are cases where gcc-generated
3942 // code assumes it is there, even if it could be put entirely into
3943 // registers. (This is not what the doc says.)
3944
3945 // FIXME: The above statement is likely due to a misunderstanding of the
3946 // documents. All arguments must be copied into the parameter area BY
3947 // THE CALLEE in the event that the callee takes the address of any
3948 // formal argument. That has not yet been implemented. However, it is
3949 // reasonable to use the stack area as a staging area for the register
3950 // load.
3951
3952 // Skip this for small aggregates, as we will use the same slot for a
3953 // right-justified copy, below.
3954 if (Size >= 8)
3955 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
3956 CallSeqStart,
3957 Flags, DAG, dl);
3958
3959 // When a register is available, pass a small aggregate right-justified.
3960 if (Size < 8 && GPR_idx != NumGPRs) {
3961 // The easiest way to get this right-justified in a register
3962 // is to copy the structure into the rightmost portion of a
3963 // local variable slot, then load the whole slot into the
3964 // register.
3965 // FIXME: The memcpy seems to produce pretty awful code for
3966 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00003967 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00003968 // parameter save area instead of a new local variable.
3969 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
3970 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3971 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3972 CallSeqStart,
3973 Flags, DAG, dl);
3974
3975 // Load the slot into the register.
3976 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
3977 MachinePointerInfo(),
3978 false, false, false, 0);
3979 MemOpChains.push_back(Load.getValue(1));
3980 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3981
3982 // Done with this argument.
3983 ArgOffset += PtrByteSize;
3984 continue;
3985 }
3986
3987 // For aggregates larger than PtrByteSize, copy the pieces of the
3988 // object that fit into registers from the parameter save area.
3989 for (unsigned j=0; j<Size; j+=PtrByteSize) {
3990 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
3991 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
3992 if (GPR_idx != NumGPRs) {
3993 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
3994 MachinePointerInfo(),
3995 false, false, false, 0);
3996 MemOpChains.push_back(Load.getValue(1));
3997 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3998 ArgOffset += PtrByteSize;
3999 } else {
4000 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4001 break;
4002 }
4003 }
4004 continue;
4005 }
4006
Craig Topper56710102013-08-15 02:33:50 +00004007 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004008 default: llvm_unreachable("Unexpected ValueType for argument!");
4009 case MVT::i32:
4010 case MVT::i64:
4011 if (GPR_idx != NumGPRs) {
4012 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
4013 } else {
4014 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4015 true, isTailCall, false, MemOpChains,
4016 TailCallArguments, dl);
4017 }
4018 ArgOffset += PtrByteSize;
4019 break;
4020 case MVT::f32:
4021 case MVT::f64:
4022 if (FPR_idx != NumFPRs) {
4023 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4024
4025 if (isVarArg) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004026 // A single float or an aggregate containing only a single float
4027 // must be passed right-justified in the stack doubleword, and
4028 // in the GPR, if one is available.
4029 SDValue StoreOff;
Craig Topper56710102013-08-15 02:33:50 +00004030 if (Arg.getSimpleValueType().SimpleTy == MVT::f32) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004031 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4032 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4033 } else
4034 StoreOff = PtrOff;
4035
4036 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004037 MachinePointerInfo(), false, false, 0);
4038 MemOpChains.push_back(Store);
4039
4040 // Float varargs are always shadowed in available integer registers
4041 if (GPR_idx != NumGPRs) {
4042 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4043 MachinePointerInfo(), false, false,
4044 false, 0);
4045 MemOpChains.push_back(Load.getValue(1));
4046 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4047 }
4048 } else if (GPR_idx != NumGPRs)
4049 // If we have any FPRs remaining, we may also have GPRs remaining.
4050 ++GPR_idx;
4051 } else {
4052 // Single-precision floating-point values are mapped to the
4053 // second (rightmost) word of the stack doubleword.
4054 if (Arg.getValueType() == MVT::f32) {
4055 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4056 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4057 }
4058
4059 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4060 true, isTailCall, false, MemOpChains,
4061 TailCallArguments, dl);
4062 }
4063 ArgOffset += 8;
4064 break;
4065 case MVT::v4f32:
4066 case MVT::v4i32:
4067 case MVT::v8i16:
4068 case MVT::v16i8:
4069 if (isVarArg) {
4070 // These go aligned on the stack, or in the corresponding R registers
4071 // when within range. The Darwin PPC ABI doc claims they also go in
4072 // V registers; in fact gcc does this only for arguments that are
4073 // prototyped, not for those that match the ... We do it for all
4074 // arguments, seems to work.
4075 while (ArgOffset % 16 !=0) {
4076 ArgOffset += PtrByteSize;
4077 if (GPR_idx != NumGPRs)
4078 GPR_idx++;
4079 }
4080 // We could elide this store in the case where the object fits
4081 // entirely in R registers. Maybe later.
4082 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
4083 DAG.getConstant(ArgOffset, PtrVT));
4084 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4085 MachinePointerInfo(), false, false, 0);
4086 MemOpChains.push_back(Store);
4087 if (VR_idx != NumVRs) {
4088 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4089 MachinePointerInfo(),
4090 false, false, false, 0);
4091 MemOpChains.push_back(Load.getValue(1));
4092 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4093 }
4094 ArgOffset += 16;
4095 for (unsigned i=0; i<16; i+=PtrByteSize) {
4096 if (GPR_idx == NumGPRs)
4097 break;
4098 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4099 DAG.getConstant(i, PtrVT));
4100 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4101 false, false, false, 0);
4102 MemOpChains.push_back(Load.getValue(1));
4103 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4104 }
4105 break;
4106 }
4107
4108 // Non-varargs Altivec params generally go in registers, but have
4109 // stack space allocated at the end.
4110 if (VR_idx != NumVRs) {
4111 // Doesn't have GPR space allocated.
4112 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4113 } else {
4114 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4115 true, isTailCall, true, MemOpChains,
4116 TailCallArguments, dl);
4117 ArgOffset += 16;
4118 }
4119 break;
4120 }
4121 }
4122
4123 if (!MemOpChains.empty())
4124 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4125 &MemOpChains[0], MemOpChains.size());
4126
4127 // Check if this is an indirect call (MTCTR/BCTRL).
4128 // See PrepareCall() for more information about calls through function
4129 // pointers in the 64-bit SVR4 ABI.
4130 if (!isTailCall &&
4131 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4132 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4133 !isBLACompatibleAddress(Callee, DAG)) {
4134 // Load r2 into a virtual register and store it to the TOC save area.
4135 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4136 // TOC save area offset.
4137 SDValue PtrOff = DAG.getIntPtrConstant(40);
4138 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4139 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4140 false, false, 0);
4141 // R12 must contain the address of an indirect callee. This does not
4142 // mean the MTCTR instruction must use R12; it's easier to model this
4143 // as an extra parameter, so do that.
4144 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4145 }
4146
4147 // Build a sequence of copy-to-reg nodes chained together with token chain
4148 // and flag operands which copy the outgoing args into the appropriate regs.
4149 SDValue InFlag;
4150 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4151 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4152 RegsToPass[i].second, InFlag);
4153 InFlag = Chain.getValue(1);
4154 }
4155
4156 if (isTailCall)
4157 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4158 FPOp, true, TailCallArguments);
4159
4160 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4161 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4162 Ins, InVals);
4163}
4164
4165SDValue
4166PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4167 CallingConv::ID CallConv, bool isVarArg,
4168 bool isTailCall,
4169 const SmallVectorImpl<ISD::OutputArg> &Outs,
4170 const SmallVectorImpl<SDValue> &OutVals,
4171 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004172 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004173 SmallVectorImpl<SDValue> &InVals) const {
4174
4175 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004176
Owen Anderson53aa7a92009-08-10 22:56:29 +00004177 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00004178 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004179 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004180
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004181 MachineFunction &MF = DAG.getMachineFunction();
4182
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004183 // Mark this function as potentially containing a function that contains a
4184 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4185 // and restoring the callers stack pointer in this functions epilog. This is
4186 // done because by tail calling the called function might overwrite the value
4187 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004188 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4189 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004190 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4191
4192 unsigned nAltivecParamsAtEnd = 0;
4193
Chris Lattneraa40ec12006-05-16 22:56:08 +00004194 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00004195 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00004196 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004197 unsigned NumBytes =
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004198 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004199 Outs, OutVals,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004200 nAltivecParamsAtEnd);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004201
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004202 // Calculate by how many bytes the stack has to be adjusted in case of tail
4203 // call optimization.
4204 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004205
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004206 // To protect arguments on the stack from being clobbered in a tail call,
4207 // force all the loads to happen before doing any other lowering.
4208 if (isTailCall)
4209 Chain = DAG.getStackArgumentTokenFactor(Chain);
4210
Chris Lattnerb7552a82006-05-17 00:15:40 +00004211 // Adjust the stack pointer for the new arguments...
4212 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004213 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4214 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004215 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004216
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004217 // Load the return address and frame pointer so it can be move somewhere else
4218 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004219 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004220 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4221 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004222
Chris Lattnerb7552a82006-05-17 00:15:40 +00004223 // Set up a copy of the stack pointer for use loading and storing any
4224 // arguments that may not fit in the registers available for argument
4225 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004226 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004227 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004228 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004229 else
Owen Anderson9f944592009-08-11 20:47:22 +00004230 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004231
Chris Lattnerb7552a82006-05-17 00:15:40 +00004232 // Figure out which arguments are going to go in registers, and which in
4233 // memory. Also, if this is a vararg function, floating point operations
4234 // must be stored to our stack, and loaded into integer regs as well, if
4235 // any integer regs are available for argument passing.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004236 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004237 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004238
Craig Topperca658c22012-03-11 07:16:55 +00004239 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004240 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4241 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4242 };
Craig Topperca658c22012-03-11 07:16:55 +00004243 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00004244 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4245 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4246 };
Craig Topperca658c22012-03-11 07:16:55 +00004247 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004248
Craig Topperca658c22012-03-11 07:16:55 +00004249 static const uint16_t VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004250 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4251 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4252 };
Owen Andersone2f23a32007-09-07 04:06:50 +00004253 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004254 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00004255 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004256
Craig Topperca658c22012-03-11 07:16:55 +00004257 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004258
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004259 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004260 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4261
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004262 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00004263 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004264 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004265 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004266
Chris Lattnerb7552a82006-05-17 00:15:40 +00004267 // PtrOff will be used to store the current argument to the stack if a
4268 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004269 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004270
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004271 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004272
Dale Johannesen679073b2009-02-04 02:34:38 +00004273 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004274
4275 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00004276 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00004277 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4278 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00004279 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004280 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00004281
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004282 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004283 // Note: "by value" is code for passing a structure by value, not
4284 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00004285 if (Flags.isByVal()) {
4286 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004287 // Very small objects are passed right-justified. Everything else is
4288 // passed left-justified.
4289 if (Size==1 || Size==2) {
4290 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004291 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00004292 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00004293 MachinePointerInfo(), VT,
4294 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004295 MemOpChains.push_back(Load.getValue(1));
4296 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004297
4298 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004299 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00004300 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4301 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004302 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004303 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4304 CallSeqStart,
4305 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004306 ArgOffset += PtrByteSize;
4307 }
4308 continue;
4309 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004310 // Copy entire object into memory. There are cases where gcc-generated
4311 // code assumes it is there, even if it could be put entirely into
4312 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004313 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4314 CallSeqStart,
4315 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004316
4317 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4318 // copy the pieces of the object that fit into registers from the
4319 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00004320 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004321 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004322 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00004323 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004324 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4325 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004326 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00004327 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00004328 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004329 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004330 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004331 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004332 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004333 }
4334 }
4335 continue;
4336 }
4337
Craig Topper56710102013-08-15 02:33:50 +00004338 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004339 default: llvm_unreachable("Unexpected ValueType for argument!");
Owen Anderson9f944592009-08-11 20:47:22 +00004340 case MVT::i32:
4341 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004342 if (GPR_idx != NumGPRs) {
4343 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004344 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004345 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4346 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004347 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00004348 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004349 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004350 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004351 case MVT::f32:
4352 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004353 if (FPR_idx != NumFPRs) {
4354 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4355
Chris Lattnerb7552a82006-05-17 00:15:40 +00004356 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00004357 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4358 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004359 MemOpChains.push_back(Store);
4360
Chris Lattnerb7552a82006-05-17 00:15:40 +00004361 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004362 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004363 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004364 MachinePointerInfo(), false, false,
4365 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004366 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004367 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004368 }
Owen Anderson9f944592009-08-11 20:47:22 +00004369 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004370 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004371 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00004372 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4373 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004374 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004375 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004376 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00004377 }
4378 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00004379 // If we have any FPRs remaining, we may also have GPRs remaining.
4380 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4381 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004382 if (GPR_idx != NumGPRs)
4383 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00004384 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004385 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4386 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004387 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004388 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004389 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4390 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004391 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004392 if (isPPC64)
4393 ArgOffset += 8;
4394 else
Owen Anderson9f944592009-08-11 20:47:22 +00004395 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004396 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004397 case MVT::v4f32:
4398 case MVT::v4i32:
4399 case MVT::v8i16:
4400 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00004401 if (isVarArg) {
4402 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00004403 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00004404 // V registers; in fact gcc does this only for arguments that are
4405 // prototyped, not for those that match the ... We do it for all
4406 // arguments, seems to work.
4407 while (ArgOffset % 16 !=0) {
4408 ArgOffset += PtrByteSize;
4409 if (GPR_idx != NumGPRs)
4410 GPR_idx++;
4411 }
4412 // We could elide this store in the case where the object fits
4413 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004414 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004415 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00004416 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4417 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004418 MemOpChains.push_back(Store);
4419 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004420 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00004421 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004422 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004423 MemOpChains.push_back(Load.getValue(1));
4424 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4425 }
4426 ArgOffset += 16;
4427 for (unsigned i=0; i<16; i+=PtrByteSize) {
4428 if (GPR_idx == NumGPRs)
4429 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00004430 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004431 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00004432 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004433 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004434 MemOpChains.push_back(Load.getValue(1));
4435 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4436 }
4437 break;
4438 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004439
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004440 // Non-varargs Altivec params generally go in registers, but have
4441 // stack space allocated at the end.
4442 if (VR_idx != NumVRs) {
4443 // Doesn't have GPR space allocated.
4444 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4445 } else if (nAltivecParamsAtEnd==0) {
4446 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004447 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4448 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004449 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004450 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00004451 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00004452 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004453 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00004454 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004455 // If all Altivec parameters fit in registers, as they usually do,
4456 // they get stack space following the non-Altivec parameters. We
4457 // don't track this here because nobody below needs it.
4458 // If there are more Altivec parameters than fit in registers emit
4459 // the stores here.
4460 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4461 unsigned j = 0;
4462 // Offset is aligned; skip 1st 12 params which go in V registers.
4463 ArgOffset = ((ArgOffset+15)/16)*16;
4464 ArgOffset += 12*16;
4465 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004466 SDValue Arg = OutVals[i];
4467 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00004468 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4469 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004470 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004471 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004472 // We are emitting Altivec params in order.
4473 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4474 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004475 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004476 ArgOffset += 16;
4477 }
4478 }
4479 }
4480 }
4481
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004482 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00004483 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnered728e82006-08-11 17:38:39 +00004484 &MemOpChains[0], MemOpChains.size());
Scott Michelcf0da6c2009-02-17 22:15:04 +00004485
Dale Johannesen90eab672010-03-09 20:15:42 +00004486 // On Darwin, R12 must contain the address of an indirect callee. This does
4487 // not mean the MTCTR instruction must use R12; it's easier to model this as
4488 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004489 if (!isTailCall &&
Dale Johannesen90eab672010-03-09 20:15:42 +00004490 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4491 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4492 !isBLACompatibleAddress(Callee, DAG))
4493 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4494 PPC::R12), Callee));
4495
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004496 // Build a sequence of copy-to-reg nodes chained together with token chain
4497 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004498 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004499 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00004500 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00004501 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004502 InFlag = Chain.getValue(1);
4503 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004504
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004505 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004506 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4507 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004508
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004509 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4510 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4511 Ins, InVals);
Chris Lattneraa40ec12006-05-16 22:56:08 +00004512}
4513
Hal Finkel450128a2011-10-14 19:51:36 +00004514bool
4515PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4516 MachineFunction &MF, bool isVarArg,
4517 const SmallVectorImpl<ISD::OutputArg> &Outs,
4518 LLVMContext &Context) const {
4519 SmallVector<CCValAssign, 16> RVLocs;
4520 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4521 RVLocs, Context);
4522 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4523}
4524
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004525SDValue
4526PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004527 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004528 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004529 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004530 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004531
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004532 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004533 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00004534 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004535 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004536
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004537 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004538 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004539
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004540 // Copy the result values into the output registers.
4541 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4542 CCValAssign &VA = RVLocs[i];
4543 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004544
4545 SDValue Arg = OutVals[i];
4546
4547 switch (VA.getLocInfo()) {
4548 default: llvm_unreachable("Unknown loc info!");
4549 case CCValAssign::Full: break;
4550 case CCValAssign::AExt:
4551 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4552 break;
4553 case CCValAssign::ZExt:
4554 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4555 break;
4556 case CCValAssign::SExt:
4557 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4558 break;
4559 }
4560
4561 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004562 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004563 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004564 }
4565
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004566 RetOps[0] = Chain; // Update chain.
4567
4568 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00004569 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004570 RetOps.push_back(Flag);
4571
4572 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other,
4573 &RetOps[0], RetOps.size());
Chris Lattner4211ca92006-04-14 06:01:58 +00004574}
4575
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004576SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004577 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00004578 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004579 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004580
Jim Laskeye4f4d042006-12-04 22:04:42 +00004581 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004582 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00004583
4584 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00004585 bool isPPC64 = Subtarget.isPPC64();
4586 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004587 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004588
4589 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004590 SDValue Chain = Op.getOperand(0);
4591 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004592
Jim Laskeye4f4d042006-12-04 22:04:42 +00004593 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00004594 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4595 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004596 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004597
Jim Laskeye4f4d042006-12-04 22:04:42 +00004598 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00004599 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004600
Jim Laskeye4f4d042006-12-04 22:04:42 +00004601 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00004602 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004603 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004604}
4605
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004606
4607
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004608SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004609PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004610 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004611 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004612 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004613 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004614
4615 // Get current frame pointer save index. The users of this index will be
4616 // primarily DYNALLOC instructions.
4617 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4618 int RASI = FI->getReturnAddrSaveIndex();
4619
4620 // If the frame pointer save index hasn't been defined yet.
4621 if (!RASI) {
4622 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004623 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004624 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004625 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004626 // Save the result.
4627 FI->setReturnAddrSaveIndex(RASI);
4628 }
4629 return DAG.getFrameIndex(RASI, PtrVT);
4630}
4631
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004632SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004633PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4634 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004635 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004636 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004637 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004638
4639 // Get current frame pointer save index. The users of this index will be
4640 // primarily DYNALLOC instructions.
4641 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4642 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004643
Jim Laskey48850c12006-11-16 22:43:37 +00004644 // If the frame pointer save index hasn't been defined yet.
4645 if (!FPSI) {
4646 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004647 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004648 isDarwinABI);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004649
Jim Laskey48850c12006-11-16 22:43:37 +00004650 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004651 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00004652 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004653 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00004654 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004655 return DAG.getFrameIndex(FPSI, PtrVT);
4656}
Jim Laskey48850c12006-11-16 22:43:37 +00004657
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004658SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004659 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004660 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004661 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004662 SDValue Chain = Op.getOperand(0);
4663 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004664 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004665
Jim Laskey48850c12006-11-16 22:43:37 +00004666 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004667 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004668 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004669 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00004670 DAG.getConstant(0, PtrVT), Size);
4671 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004672 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00004673 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004674 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00004675 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004676 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey48850c12006-11-16 22:43:37 +00004677}
4678
Hal Finkel756810f2013-03-21 21:37:52 +00004679SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
4680 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004681 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004682 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
4683 DAG.getVTList(MVT::i32, MVT::Other),
4684 Op.getOperand(0), Op.getOperand(1));
4685}
4686
4687SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
4688 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004689 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004690 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
4691 Op.getOperand(0), Op.getOperand(1));
4692}
4693
Chris Lattner4211ca92006-04-14 06:01:58 +00004694/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4695/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004696SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00004697 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00004698 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4699 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00004700 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004701
Hal Finkel81f87992013-04-07 22:11:09 +00004702 // We might be able to do better than this under some circumstances, but in
4703 // general, fsel-based lowering of select is a finite-math-only optimization.
4704 // For more information, see section F.3 of the 2.06 ISA specification.
4705 if (!DAG.getTarget().Options.NoInfsFPMath ||
4706 !DAG.getTarget().Options.NoNaNsFPMath)
4707 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004708
Hal Finkel81f87992013-04-07 22:11:09 +00004709 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004710
Owen Anderson53aa7a92009-08-10 22:56:29 +00004711 EVT ResVT = Op.getValueType();
4712 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004713 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4714 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004715 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004716
Chris Lattner4211ca92006-04-14 06:01:58 +00004717 // If the RHS of the comparison is a 0.0, we don't need to do the
4718 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00004719 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00004720 if (isFloatingPointZero(RHS))
4721 switch (CC) {
4722 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004723 case ISD::SETNE:
4724 std::swap(TV, FV);
4725 case ISD::SETEQ:
4726 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4727 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
4728 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
4729 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4730 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4731 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4732 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004733 case ISD::SETULT:
4734 case ISD::SETLT:
4735 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004736 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004737 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00004738 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4739 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004740 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004741 case ISD::SETUGT:
4742 case ISD::SETGT:
4743 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004744 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004745 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00004746 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4747 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004748 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00004749 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004750 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004751
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004752 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00004753 switch (CC) {
4754 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004755 case ISD::SETNE:
4756 std::swap(TV, FV);
4757 case ISD::SETEQ:
4758 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
4759 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4760 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
4761 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
4762 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4763 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4764 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4765 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004766 case ISD::SETULT:
4767 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004768 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004769 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4770 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004771 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004772 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004773 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004774 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004775 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4776 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004777 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004778 case ISD::SETUGT:
4779 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004780 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004781 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4782 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004783 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004784 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004785 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004786 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004787 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4788 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004789 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004790 }
Eli Friedman5806e182009-05-28 04:31:08 +00004791 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00004792}
4793
Chris Lattner57ee7c62007-11-28 18:44:47 +00004794// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen37bc85f2009-06-04 20:53:52 +00004795SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004796 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00004797 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004798 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00004799 if (Src.getValueType() == MVT::f32)
4800 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00004801
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004802 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00004803 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004804 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00004805 case MVT::i32:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00004806 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Hal Finkelf6d45f22013-04-01 17:52:07 +00004807 (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ :
4808 PPCISD::FCTIDZ),
Owen Anderson9f944592009-08-11 20:47:22 +00004809 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00004810 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004811 case MVT::i64:
Hal Finkel3f88d082013-04-01 18:42:58 +00004812 assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) &&
4813 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00004814 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
4815 PPCISD::FCTIDUZ,
4816 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00004817 break;
4818 }
Duncan Sands2a287912008-07-19 16:26:02 +00004819
Chris Lattner4211ca92006-04-14 06:01:58 +00004820 // Convert the FP value to an int value through memory.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004821 bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() &&
4822 (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT());
4823 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
4824 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
4825 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00004826
Chris Lattner06a49542007-10-15 20:14:52 +00004827 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004828 SDValue Chain;
4829 if (i32Stack) {
4830 MachineFunction &MF = DAG.getMachineFunction();
4831 MachineMemOperand *MMO =
4832 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
4833 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
4834 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
4835 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
4836 MVT::i32, MMO);
4837 } else
4838 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
4839 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00004840
4841 // Result is a load from the stack slot. If loading 4 bytes, make sure to
4842 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004843 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00004844 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00004845 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkelf6d45f22013-04-01 17:52:07 +00004846 MPI = MachinePointerInfo();
4847 }
4848
4849 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004850 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00004851}
4852
Hal Finkelf6d45f22013-04-01 17:52:07 +00004853SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004854 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004855 SDLoc dl(Op);
Dan Gohmand6819da2008-03-11 01:59:03 +00004856 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00004857 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004858 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00004859
Hal Finkelf6d45f22013-04-01 17:52:07 +00004860 assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) &&
4861 "UINT_TO_FP is supported only with FPCVT");
4862
4863 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00004864 // Otherwise, convert to double-precision and then round.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004865 unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4866 (Op.getOpcode() == ISD::UINT_TO_FP ?
4867 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
4868 (Op.getOpcode() == ISD::UINT_TO_FP ?
4869 PPCISD::FCFIDU : PPCISD::FCFID);
4870 MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4871 MVT::f32 : MVT::f64;
4872
Owen Anderson9f944592009-08-11 20:47:22 +00004873 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00004874 SDValue SINT = Op.getOperand(0);
4875 // When converting to single-precision, we actually need to convert
4876 // to double-precision first and then round to single-precision.
4877 // To avoid double-rounding effects during that operation, we have
4878 // to prepare the input operand. Bits that might be truncated when
4879 // converting to double-precision are replaced by a bit that won't
4880 // be lost at this stage, but is below the single-precision rounding
4881 // position.
4882 //
4883 // However, if -enable-unsafe-fp-math is in effect, accept double
4884 // rounding to avoid the extra overhead.
4885 if (Op.getValueType() == MVT::f32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00004886 !PPCSubTarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00004887 !DAG.getTarget().Options.UnsafeFPMath) {
4888
4889 // Twiddle input to make sure the low 11 bits are zero. (If this
4890 // is the case, we are guaranteed the value will fit into the 53 bit
4891 // mantissa of an IEEE double-precision value without rounding.)
4892 // If any of those low 11 bits were not zero originally, make sure
4893 // bit 12 (value 2048) is set instead, so that the final rounding
4894 // to single-precision gets the correct result.
4895 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4896 SINT, DAG.getConstant(2047, MVT::i64));
4897 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
4898 Round, DAG.getConstant(2047, MVT::i64));
4899 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
4900 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4901 Round, DAG.getConstant(-2048, MVT::i64));
4902
4903 // However, we cannot use that value unconditionally: if the magnitude
4904 // of the input value is small, the bit-twiddling we did above might
4905 // end up visibly changing the output. Fortunately, in that case, we
4906 // don't need to twiddle bits since the original input will convert
4907 // exactly to double-precision floating-point already. Therefore,
4908 // construct a conditional to use the original value if the top 11
4909 // bits are all sign-bit copies, and use the rounded value computed
4910 // above otherwise.
4911 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
4912 SINT, DAG.getConstant(53, MVT::i32));
4913 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
4914 Cond, DAG.getConstant(1, MVT::i64));
4915 Cond = DAG.getSetCC(dl, MVT::i32,
4916 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
4917
4918 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
4919 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00004920
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00004921 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkelf6d45f22013-04-01 17:52:07 +00004922 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
4923
4924 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00004925 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00004926 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00004927 return FP;
4928 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004929
Owen Anderson9f944592009-08-11 20:47:22 +00004930 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00004931 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00004932 // Since we only generate this in 64-bit mode, we can take advantage of
4933 // 64-bit registers. In particular, sign extend the input value into the
4934 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
4935 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00004936 MachineFunction &MF = DAG.getMachineFunction();
4937 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004938 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004939
Hal Finkelbeb296b2013-03-31 10:12:51 +00004940 SDValue Ld;
Hal Finkelf6d45f22013-04-01 17:52:07 +00004941 if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) {
Hal Finkelbeb296b2013-03-31 10:12:51 +00004942 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
4943 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004944
Hal Finkelbeb296b2013-03-31 10:12:51 +00004945 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
4946 MachinePointerInfo::getFixedStack(FrameIdx),
4947 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00004948
Hal Finkelbeb296b2013-03-31 10:12:51 +00004949 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
4950 "Expected an i32 store");
4951 MachineMemOperand *MMO =
4952 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
4953 MachineMemOperand::MOLoad, 4, 4);
4954 SDValue Ops[] = { Store, FIdx };
Hal Finkelf6d45f22013-04-01 17:52:07 +00004955 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
4956 PPCISD::LFIWZX : PPCISD::LFIWAX,
4957 dl, DAG.getVTList(MVT::f64, MVT::Other),
4958 Ops, 2, MVT::i32, MMO);
Hal Finkelbeb296b2013-03-31 10:12:51 +00004959 } else {
Hal Finkelf6d45f22013-04-01 17:52:07 +00004960 assert(PPCSubTarget.isPPC64() &&
4961 "i32->FP without LFIWAX supported only on PPC64");
4962
Hal Finkelbeb296b2013-03-31 10:12:51 +00004963 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
4964 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
4965
4966 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
4967 Op.getOperand(0));
4968
4969 // STD the extended value into the stack slot.
4970 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
4971 MachinePointerInfo::getFixedStack(FrameIdx),
4972 false, false, 0);
4973
4974 // Load the value as a double.
4975 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
4976 MachinePointerInfo::getFixedStack(FrameIdx),
4977 false, false, false, 0);
4978 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004979
Chris Lattner4211ca92006-04-14 06:01:58 +00004980 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004981 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
4982 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00004983 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00004984 return FP;
4985}
4986
Dan Gohman21cea8a2010-04-17 15:26:15 +00004987SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
4988 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004989 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00004990 /*
4991 The rounding mode is in bits 30:31 of FPSR, and has the following
4992 settings:
4993 00 Round to nearest
4994 01 Round to 0
4995 10 Round to +inf
4996 11 Round to -inf
4997
4998 FLT_ROUNDS, on the other hand, expects the following:
4999 -1 Undefined
5000 0 Round to 0
5001 1 Round to nearest
5002 2 Round to +inf
5003 3 Round to -inf
5004
5005 To perform the conversion, we do:
5006 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
5007 */
5008
5009 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005010 EVT VT = Op.getValueType();
5011 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005012 SDValue MFFSreg, InFlag;
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005013
5014 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005015 EVT NodeTys[] = {
5016 MVT::f64, // return register
5017 MVT::Glue // unused in this context
5018 };
Dale Johannesen021052a2009-02-04 20:06:27 +00005019 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005020
5021 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00005022 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005023 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005024 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00005025 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005026
5027 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005028 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005029 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00005030 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005031 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005032
5033 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005034 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00005035 DAG.getNode(ISD::AND, dl, MVT::i32,
5036 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005037 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00005038 DAG.getNode(ISD::SRL, dl, MVT::i32,
5039 DAG.getNode(ISD::AND, dl, MVT::i32,
5040 DAG.getNode(ISD::XOR, dl, MVT::i32,
5041 CWD, DAG.getConstant(3, MVT::i32)),
5042 DAG.getConstant(3, MVT::i32)),
5043 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005044
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005045 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00005046 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005047
Duncan Sands13237ac2008-06-06 12:08:01 +00005048 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00005049 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005050}
5051
Dan Gohman21cea8a2010-04-17 15:26:15 +00005052SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005053 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005054 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005055 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00005056 assert(Op.getNumOperands() == 3 &&
5057 VT == Op.getOperand(1).getValueType() &&
5058 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005059
Chris Lattner601b8652006-09-20 03:47:40 +00005060 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005061 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005062 SDValue Lo = Op.getOperand(0);
5063 SDValue Hi = Op.getOperand(1);
5064 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005065 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005066
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005067 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005068 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005069 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5070 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5071 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5072 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005073 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005074 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5075 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5076 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005077 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005078 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005079}
5080
Dan Gohman21cea8a2010-04-17 15:26:15 +00005081SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005082 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005083 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00005084 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005085 assert(Op.getNumOperands() == 3 &&
5086 VT == Op.getOperand(1).getValueType() &&
5087 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005088
Dan Gohman8d2ead22008-03-07 20:36:53 +00005089 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005090 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005091 SDValue Lo = Op.getOperand(0);
5092 SDValue Hi = Op.getOperand(1);
5093 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005094 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005095
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005096 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005097 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005098 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5099 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5100 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5101 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005102 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005103 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5104 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5105 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005106 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005107 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005108}
5109
Dan Gohman21cea8a2010-04-17 15:26:15 +00005110SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005111 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005112 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005113 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005114 assert(Op.getNumOperands() == 3 &&
5115 VT == Op.getOperand(1).getValueType() &&
5116 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005117
Dan Gohman8d2ead22008-03-07 20:36:53 +00005118 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005119 SDValue Lo = Op.getOperand(0);
5120 SDValue Hi = Op.getOperand(1);
5121 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005122 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005123
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005124 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005125 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005126 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5127 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5128 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5129 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005130 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005131 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5132 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5133 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00005134 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005135 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005136 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005137}
5138
5139//===----------------------------------------------------------------------===//
5140// Vector related lowering.
5141//
5142
Chris Lattner2a099c02006-04-17 06:00:21 +00005143/// BuildSplatI - Build a canonical splati of Val with an element size of
5144/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005145static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005146 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00005147 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005148
Owen Anderson53aa7a92009-08-10 22:56:29 +00005149 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00005150 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00005151 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005152
Owen Anderson9f944592009-08-11 20:47:22 +00005153 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005154
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005155 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5156 if (Val == -1)
5157 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005158
Owen Anderson53aa7a92009-08-10 22:56:29 +00005159 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005160
Chris Lattner2a099c02006-04-17 06:00:21 +00005161 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00005162 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005163 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00005164 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga49de9d2009-02-25 22:49:59 +00005165 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
5166 &Ops[0], Ops.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00005167 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005168}
5169
Hal Finkelcf2e9082013-05-24 23:00:14 +00005170/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5171/// specified intrinsic ID.
5172static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005173 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00005174 EVT DestVT = MVT::Other) {
5175 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5176 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5177 DAG.getConstant(IID, MVT::i32), Op);
5178}
5179
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005180/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00005181/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005182static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005183 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005184 EVT DestVT = MVT::Other) {
5185 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005186 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005187 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005188}
5189
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005190/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5191/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005192static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005193 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005194 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00005195 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005196 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005197 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005198}
5199
5200
Chris Lattner264c9082006-04-17 17:55:10 +00005201/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5202/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005203static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005204 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00005205 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00005206 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5207 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005208
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005209 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00005210 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005211 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00005212 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005213 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00005214}
5215
Chris Lattner19e90552006-04-14 05:19:18 +00005216// If this is a case we can't handle, return null and let the default
5217// expansion code take care of it. If we CAN select this case, and if it
5218// selects to a single instruction, return Op. Otherwise, if we can codegen
5219// this case more efficiently than a constant pool load, lower it to the
5220// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005221SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5222 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005223 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005224 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5225 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00005226
Bob Wilson85cefe82009-03-02 23:24:16 +00005227 // Check if this is a splat of a constant value.
5228 APInt APSplatBits, APSplatUndef;
5229 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005230 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00005231 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00005232 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00005233 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00005234
Bob Wilson530e0382009-03-03 19:26:27 +00005235 unsigned SplatBits = APSplatBits.getZExtValue();
5236 unsigned SplatUndef = APSplatUndef.getZExtValue();
5237 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005238
Bob Wilson530e0382009-03-03 19:26:27 +00005239 // First, handle single instruction cases.
5240
5241 // All zeros?
5242 if (SplatBits == 0) {
5243 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00005244 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5245 SDValue Z = DAG.getConstant(0, MVT::i32);
5246 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00005247 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00005248 }
Bob Wilson530e0382009-03-03 19:26:27 +00005249 return Op;
5250 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00005251
Bob Wilson530e0382009-03-03 19:26:27 +00005252 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5253 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5254 (32-SplatBitSize));
5255 if (SextVal >= -16 && SextVal <= 15)
5256 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005257
5258
Bob Wilson530e0382009-03-03 19:26:27 +00005259 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005260
Bob Wilson530e0382009-03-03 19:26:27 +00005261 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00005262 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5263 // If this value is in the range [17,31] and is odd, use:
5264 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5265 // If this value is in the range [-31,-17] and is odd, use:
5266 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5267 // Note the last two are three-instruction sequences.
5268 if (SextVal >= -32 && SextVal <= 31) {
5269 // To avoid having these optimizations undone by constant folding,
5270 // we convert to a pseudo that will be expanded later into one of
5271 // the above forms.
5272 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt51e79512013-02-20 15:50:31 +00005273 EVT VT = Op.getValueType();
5274 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5275 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5276 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilson530e0382009-03-03 19:26:27 +00005277 }
5278
5279 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5280 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5281 // for fneg/fabs.
5282 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5283 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00005284 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005285
5286 // Make the VSLW intrinsic, computing 0x8000_0000.
5287 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5288 OnesV, DAG, dl);
5289
5290 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00005291 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00005292 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005293 }
5294
5295 // Check to see if this is a wide variety of vsplti*, binop self cases.
5296 static const signed char SplatCsts[] = {
5297 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5298 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5299 };
5300
5301 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5302 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5303 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5304 int i = SplatCsts[idx];
5305
5306 // Figure out what shift amount will be used by altivec if shifted by i in
5307 // this splat size.
5308 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5309
5310 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00005311 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005312 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005313 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5314 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5315 Intrinsic::ppc_altivec_vslw
5316 };
5317 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005318 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005319 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005320
Bob Wilson530e0382009-03-03 19:26:27 +00005321 // vsplti + srl self.
5322 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005323 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005324 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5325 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5326 Intrinsic::ppc_altivec_vsrw
5327 };
5328 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005329 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005330 }
5331
Bob Wilson530e0382009-03-03 19:26:27 +00005332 // vsplti + sra self.
5333 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005334 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005335 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5336 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5337 Intrinsic::ppc_altivec_vsraw
5338 };
5339 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005340 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005341 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005342
Bob Wilson530e0382009-03-03 19:26:27 +00005343 // vsplti + rol self.
5344 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5345 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005346 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005347 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5348 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5349 Intrinsic::ppc_altivec_vrlw
5350 };
5351 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005352 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005353 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005354
Bob Wilson530e0382009-03-03 19:26:27 +00005355 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00005356 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005357 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005358 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00005359 }
Bob Wilson530e0382009-03-03 19:26:27 +00005360 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00005361 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005362 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005363 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00005364 }
Bob Wilson530e0382009-03-03 19:26:27 +00005365 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00005366 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005367 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005368 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5369 }
5370 }
5371
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005372 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00005373}
5374
Chris Lattner071ad012006-04-17 05:28:54 +00005375/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5376/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005377static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005378 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005379 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00005380 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00005381 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00005382 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005383
Chris Lattner071ad012006-04-17 05:28:54 +00005384 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00005385 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 +00005386 OP_VMRGHW,
5387 OP_VMRGLW,
5388 OP_VSPLTISW0,
5389 OP_VSPLTISW1,
5390 OP_VSPLTISW2,
5391 OP_VSPLTISW3,
5392 OP_VSLDOI4,
5393 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00005394 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00005395 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00005396
Chris Lattner071ad012006-04-17 05:28:54 +00005397 if (OpNum == OP_COPY) {
5398 if (LHSID == (1*9+2)*9+3) return LHS;
5399 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5400 return RHS;
5401 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005402
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005403 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005404 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5405 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005406
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005407 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00005408 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005409 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00005410 case OP_VMRGHW:
5411 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5412 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5413 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5414 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5415 break;
5416 case OP_VMRGLW:
5417 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5418 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5419 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5420 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5421 break;
5422 case OP_VSPLTISW0:
5423 for (unsigned i = 0; i != 16; ++i)
5424 ShufIdxs[i] = (i&3)+0;
5425 break;
5426 case OP_VSPLTISW1:
5427 for (unsigned i = 0; i != 16; ++i)
5428 ShufIdxs[i] = (i&3)+4;
5429 break;
5430 case OP_VSPLTISW2:
5431 for (unsigned i = 0; i != 16; ++i)
5432 ShufIdxs[i] = (i&3)+8;
5433 break;
5434 case OP_VSPLTISW3:
5435 for (unsigned i = 0; i != 16; ++i)
5436 ShufIdxs[i] = (i&3)+12;
5437 break;
5438 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005439 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005440 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005441 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005442 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005443 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005444 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00005445 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00005446 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5447 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005448 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00005449 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00005450}
5451
Chris Lattner19e90552006-04-14 05:19:18 +00005452/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5453/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5454/// return the code it can be lowered into. Worst case, it can always be
5455/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005456SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005457 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005458 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005459 SDValue V1 = Op.getOperand(0);
5460 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005461 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005462 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005463
Chris Lattner19e90552006-04-14 05:19:18 +00005464 // Cases that are handled by instructions that take permute immediates
5465 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5466 // selected by the instruction selector.
5467 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005468 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5469 PPC::isSplatShuffleMask(SVOp, 2) ||
5470 PPC::isSplatShuffleMask(SVOp, 4) ||
5471 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5472 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5473 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5474 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5475 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5476 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5477 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5478 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5479 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattner19e90552006-04-14 05:19:18 +00005480 return Op;
5481 }
5482 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005483
Chris Lattner19e90552006-04-14 05:19:18 +00005484 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5485 // and produce a fixed permutation. If any of these match, do not lower to
5486 // VPERM.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005487 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5488 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5489 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5490 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5491 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5492 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5493 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5494 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5495 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattner19e90552006-04-14 05:19:18 +00005496 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005497
Chris Lattner071ad012006-04-17 05:28:54 +00005498 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5499 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005500 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00005501
Chris Lattner071ad012006-04-17 05:28:54 +00005502 unsigned PFIndexes[4];
5503 bool isFourElementShuffle = true;
5504 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5505 unsigned EltNo = 8; // Start out undef.
5506 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005507 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00005508 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005509
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005510 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00005511 if ((ByteSource & 3) != j) {
5512 isFourElementShuffle = false;
5513 break;
5514 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005515
Chris Lattner071ad012006-04-17 05:28:54 +00005516 if (EltNo == 8) {
5517 EltNo = ByteSource/4;
5518 } else if (EltNo != ByteSource/4) {
5519 isFourElementShuffle = false;
5520 break;
5521 }
5522 }
5523 PFIndexes[i] = EltNo;
5524 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005525
5526 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00005527 // perfect shuffle vector to determine if it is cost effective to do this as
5528 // discrete instructions, or whether we should use a vperm.
5529 if (isFourElementShuffle) {
5530 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005531 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00005532 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005533
Chris Lattner071ad012006-04-17 05:28:54 +00005534 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5535 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005536
Chris Lattner071ad012006-04-17 05:28:54 +00005537 // Determining when to avoid vperm is tricky. Many things affect the cost
5538 // of vperm, particularly how many times the perm mask needs to be computed.
5539 // For example, if the perm mask can be hoisted out of a loop or is already
5540 // used (perhaps because there are multiple permutes with the same shuffle
5541 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5542 // the loop requires an extra register.
5543 //
5544 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00005545 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00005546 // available, if this block is within a loop, we should avoid using vperm
5547 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005548 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005549 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005550 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005551
Chris Lattner19e90552006-04-14 05:19:18 +00005552 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5553 // vector that will get spilled to the constant pool.
5554 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005555
Chris Lattner19e90552006-04-14 05:19:18 +00005556 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5557 // that it is in input element units, not in bytes. Convert now.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005558 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005559 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005560
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005561 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005562 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5563 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005564
Chris Lattner19e90552006-04-14 05:19:18 +00005565 for (unsigned j = 0; j != BytesPerElement; ++j)
5566 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson9f944592009-08-11 20:47:22 +00005567 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00005568 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005569
Owen Anderson9f944592009-08-11 20:47:22 +00005570 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga49de9d2009-02-25 22:49:59 +00005571 &ResultMask[0], ResultMask.size());
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005572 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00005573}
5574
Chris Lattner9754d142006-04-18 17:59:36 +00005575/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5576/// altivec comparison. If it is, return true and fill in Opc/isDot with
5577/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005578static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner9754d142006-04-18 17:59:36 +00005579 bool &isDot) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005580 unsigned IntrinsicID =
5581 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00005582 CompareOpc = -1;
5583 isDot = false;
5584 switch (IntrinsicID) {
5585 default: return false;
5586 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00005587 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5588 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5589 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5590 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5591 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5592 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5593 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5594 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5595 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5596 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5597 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5598 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5599 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005600
Chris Lattner4211ca92006-04-14 06:01:58 +00005601 // Normal Comparisons.
5602 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5603 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5604 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5605 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5606 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5607 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5608 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5609 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5610 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5611 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5612 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5613 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5614 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5615 }
Chris Lattner9754d142006-04-18 17:59:36 +00005616 return true;
5617}
5618
5619/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5620/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005621SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005622 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00005623 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5624 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005625 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00005626 int CompareOpc;
5627 bool isDot;
5628 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005629 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005630
Chris Lattner9754d142006-04-18 17:59:36 +00005631 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00005632 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00005633 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00005634 Op.getOperand(1), Op.getOperand(2),
5635 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00005636 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00005637 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005638
Chris Lattner4211ca92006-04-14 06:01:58 +00005639 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005640 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005641 Op.getOperand(2), // LHS
5642 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00005643 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005644 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005645 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00005646 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005647
Chris Lattner4211ca92006-04-14 06:01:58 +00005648 // Now that we have the comparison, emit a copy from the CR to a GPR.
5649 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00005650 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00005651 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00005652 CompNode.getValue(1));
5653
Chris Lattner4211ca92006-04-14 06:01:58 +00005654 // Unpack the result based on how the target uses it.
5655 unsigned BitNo; // Bit # of CR6.
5656 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00005657 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00005658 default: // Can't happen, don't crash on invalid number though.
5659 case 0: // Return the value of the EQ bit of CR6.
5660 BitNo = 0; InvertBit = false;
5661 break;
5662 case 1: // Return the inverted value of the EQ bit of CR6.
5663 BitNo = 0; InvertBit = true;
5664 break;
5665 case 2: // Return the value of the LT bit of CR6.
5666 BitNo = 2; InvertBit = false;
5667 break;
5668 case 3: // Return the inverted value of the LT bit of CR6.
5669 BitNo = 2; InvertBit = true;
5670 break;
5671 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005672
Chris Lattner4211ca92006-04-14 06:01:58 +00005673 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00005674 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5675 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005676 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00005677 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5678 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00005679
Chris Lattner4211ca92006-04-14 06:01:58 +00005680 // If we are supposed to, toggle the bit.
5681 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00005682 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5683 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005684 return Flags;
5685}
5686
Scott Michelcf0da6c2009-02-17 22:15:04 +00005687SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005688 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005689 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00005690 // Create a stack slot that is 16-byte aligned.
5691 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00005692 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00005693 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005694 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005695
Chris Lattner4211ca92006-04-14 06:01:58 +00005696 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00005697 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00005698 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005699 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005700 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00005701 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005702 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005703}
5704
Dan Gohman21cea8a2010-04-17 15:26:15 +00005705SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005706 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005707 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005708 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005709
Owen Anderson9f944592009-08-11 20:47:22 +00005710 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
5711 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005712
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005713 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005714 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005715
Chris Lattner7e4398742006-04-18 03:43:48 +00005716 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00005717 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
5718 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
5719 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005720
Chris Lattner7e4398742006-04-18 03:43:48 +00005721 // Low parts multiplied together, generating 32-bit results (we ignore the
5722 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005723 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00005724 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005725
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005726 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00005727 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00005728 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005729 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005730 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005731 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
5732 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005733 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005734
Owen Anderson9f944592009-08-11 20:47:22 +00005735 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00005736
Chris Lattner96d50482006-04-18 04:28:57 +00005737 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005738 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005739 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005740 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005741
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005742 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005743 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00005744 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005745 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005746
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005747 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005748 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00005749 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005750 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005751
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005752 // Merge the results together.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005753 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005754 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005755 Ops[i*2 ] = 2*i+1;
5756 Ops[i*2+1] = 2*i+1+16;
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005757 }
Owen Anderson9f944592009-08-11 20:47:22 +00005758 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00005759 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005760 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00005761 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005762}
5763
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005764/// LowerOperation - Provide custom lowering hooks for some operations.
5765///
Dan Gohman21cea8a2010-04-17 15:26:15 +00005766SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005767 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005768 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005769 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00005770 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005771 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00005772 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00005773 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005774 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00005775 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
5776 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005777 case ISD::VASTART:
Dan Gohman31ae5862010-04-17 14:41:14 +00005778 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005779
5780 case ISD::VAARG:
Dan Gohman31ae5862010-04-17 14:41:14 +00005781 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00005782
Roman Divackyc3825df2013-07-25 21:36:47 +00005783 case ISD::VACOPY:
5784 return LowerVACOPY(Op, DAG, PPCSubTarget);
5785
Jim Laskeye4f4d042006-12-04 22:04:42 +00005786 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00005787 case ISD::DYNAMIC_STACKALLOC:
5788 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng51096af2008-04-19 01:30:48 +00005789
Hal Finkel756810f2013-03-21 21:37:52 +00005790 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
5791 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
5792
Chris Lattner4211ca92006-04-14 06:01:58 +00005793 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005794 case ISD::FP_TO_UINT:
5795 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005796 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00005797 case ISD::UINT_TO_FP:
5798 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00005799 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00005800
Chris Lattner4211ca92006-04-14 06:01:58 +00005801 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00005802 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
5803 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
5804 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00005805
Chris Lattner4211ca92006-04-14 06:01:58 +00005806 // Vector-related lowering.
5807 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5808 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5809 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5810 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005811 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005812
Hal Finkel25c19922013-05-15 21:37:41 +00005813 // For counter-based loop handling.
5814 case ISD::INTRINSIC_W_CHAIN: return SDValue();
5815
Chris Lattnerf6a81562007-12-08 06:59:59 +00005816 // Frame & Return address.
5817 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00005818 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00005819 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005820}
5821
Duncan Sands6ed40142008-12-01 11:39:25 +00005822void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
5823 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005824 SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00005825 const TargetMachine &TM = getTargetMachine();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005826 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00005827 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00005828 default:
Craig Toppere55c5562012-02-07 02:50:20 +00005829 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkel25c19922013-05-15 21:37:41 +00005830 case ISD::INTRINSIC_W_CHAIN: {
5831 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
5832 Intrinsic::ppc_is_decremented_ctr_nonzero)
5833 break;
5834
5835 assert(N->getValueType(0) == MVT::i1 &&
5836 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00005837 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00005838 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
5839 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
5840 N->getOperand(1));
5841
5842 Results.push_back(NewInt);
5843 Results.push_back(NewInt.getValue(1));
5844 break;
5845 }
Roman Divacky4394e682011-06-28 15:30:42 +00005846 case ISD::VAARG: {
5847 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
5848 || TM.getSubtarget<PPCSubtarget>().isPPC64())
5849 return;
5850
5851 EVT VT = N->getValueType(0);
5852
5853 if (VT == MVT::i64) {
5854 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
5855
5856 Results.push_back(NewNode);
5857 Results.push_back(NewNode.getValue(1));
5858 }
5859 return;
5860 }
Duncan Sands6ed40142008-12-01 11:39:25 +00005861 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00005862 assert(N->getValueType(0) == MVT::ppcf128);
5863 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005864 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005865 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00005866 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00005867 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005868 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00005869 DAG.getIntPtrConstant(1));
5870
Ulrich Weigand874fc622013-03-26 10:56:22 +00005871 // Add the two halves of the long double in round-to-zero mode.
5872 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00005873
5874 // We know the low half is about to be thrown away, so just use something
5875 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00005876 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00005877 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00005878 return;
Duncan Sands2a287912008-07-19 16:26:02 +00005879 }
Duncan Sands6ed40142008-12-01 11:39:25 +00005880 case ISD::FP_TO_SINT:
Bill Schmidt41221692013-07-09 18:50:20 +00005881 // LowerFP_TO_INT() can only handle f32 and f64.
5882 if (N->getOperand(0).getValueType() == MVT::ppcf128)
5883 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005884 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00005885 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00005886 }
5887}
5888
5889
Chris Lattner4211ca92006-04-14 06:01:58 +00005890//===----------------------------------------------------------------------===//
5891// Other Lowering Code
5892//===----------------------------------------------------------------------===//
5893
Chris Lattner9b577f12005-08-26 21:23:58 +00005894MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00005895PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman747e55b2009-02-07 16:15:20 +00005896 bool is64bit, unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005897 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesend4eb0522008-08-25 22:34:37 +00005898 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5899
5900 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5901 MachineFunction *F = BB->getParent();
5902 MachineFunction::iterator It = BB;
5903 ++It;
5904
5905 unsigned dest = MI->getOperand(0).getReg();
5906 unsigned ptrA = MI->getOperand(1).getReg();
5907 unsigned ptrB = MI->getOperand(2).getReg();
5908 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00005909 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00005910
5911 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5912 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5913 F->insert(It, loopMBB);
5914 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00005915 exitMBB->splice(exitMBB->begin(), BB,
5916 llvm::next(MachineBasicBlock::iterator(MI)),
5917 BB->end());
5918 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00005919
5920 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005921 unsigned TmpReg = (!BinOpcode) ? incr :
5922 RegInfo.createVirtualRegister(
Dale Johannesenbc698292008-09-02 20:30:23 +00005923 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5924 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00005925
5926 // thisMBB:
5927 // ...
5928 // fallthrough --> loopMBB
5929 BB->addSuccessor(loopMBB);
5930
5931 // loopMBB:
5932 // l[wd]arx dest, ptr
5933 // add r0, dest, incr
5934 // st[wd]cx. r0, ptr
5935 // bne- loopMBB
5936 // fallthrough --> exitMBB
5937 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00005938 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00005939 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005940 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00005941 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
5942 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesend4eb0522008-08-25 22:34:37 +00005943 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00005944 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00005945 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00005946 BB->addSuccessor(loopMBB);
5947 BB->addSuccessor(exitMBB);
5948
5949 // exitMBB:
5950 // ...
5951 BB = exitMBB;
5952 return BB;
5953}
5954
5955MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00005956PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00005957 MachineBasicBlock *BB,
5958 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00005959 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005960 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesena32affb2008-08-28 17:53:09 +00005961 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5962 // In 64 bit mode we have to use 64 bits for addresses, even though the
5963 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
5964 // registers without caring whether they're 32 or 64, but here we're
5965 // doing actual arithmetic on the addresses.
5966 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00005967 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00005968
5969 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5970 MachineFunction *F = BB->getParent();
5971 MachineFunction::iterator It = BB;
5972 ++It;
5973
5974 unsigned dest = MI->getOperand(0).getReg();
5975 unsigned ptrA = MI->getOperand(1).getReg();
5976 unsigned ptrB = MI->getOperand(2).getReg();
5977 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00005978 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00005979
5980 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5981 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5982 F->insert(It, loopMBB);
5983 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00005984 exitMBB->splice(exitMBB->begin(), BB,
5985 llvm::next(MachineBasicBlock::iterator(MI)),
5986 BB->end());
5987 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00005988
5989 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005990 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00005991 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5992 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00005993 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
5994 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
5995 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
5996 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
5997 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
5998 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
5999 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6000 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6001 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
6002 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006003 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006004 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006005 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006006
6007 // thisMBB:
6008 // ...
6009 // fallthrough --> loopMBB
6010 BB->addSuccessor(loopMBB);
6011
6012 // The 4-byte load must be aligned, while a char or short may be
6013 // anywhere in the word. Hence all this nasty bookkeeping code.
6014 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6015 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006016 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00006017 // rlwinm ptr, ptr1, 0, 0, 29
6018 // slw incr2, incr, shift
6019 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6020 // slw mask, mask2, shift
6021 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00006022 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006023 // add tmp, tmpDest, incr2
6024 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00006025 // and tmp3, tmp, mask
6026 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00006027 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00006028 // bne- loopMBB
6029 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006030 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006031 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00006032 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006033 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006034 .addReg(ptrA).addReg(ptrB);
6035 } else {
6036 Ptr1Reg = ptrB;
6037 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006038 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006039 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006040 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006041 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6042 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006043 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006044 .addReg(Ptr1Reg).addImm(0).addImm(61);
6045 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006046 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006047 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006048 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006049 .addReg(incr).addReg(ShiftReg);
6050 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006051 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00006052 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006053 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6054 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00006055 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006056 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006057 .addReg(Mask2Reg).addReg(ShiftReg);
6058
6059 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006060 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006061 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006062 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006063 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006064 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006065 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006066 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006067 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006068 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006069 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006070 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00006071 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006072 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006073 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006074 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006075 BB->addSuccessor(loopMBB);
6076 BB->addSuccessor(exitMBB);
6077
6078 // exitMBB:
6079 // ...
6080 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006081 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6082 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00006083 return BB;
6084}
6085
Hal Finkel756810f2013-03-21 21:37:52 +00006086llvm::MachineBasicBlock*
6087PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6088 MachineBasicBlock *MBB) const {
6089 DebugLoc DL = MI->getDebugLoc();
6090 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6091
6092 MachineFunction *MF = MBB->getParent();
6093 MachineRegisterInfo &MRI = MF->getRegInfo();
6094
6095 const BasicBlock *BB = MBB->getBasicBlock();
6096 MachineFunction::iterator I = MBB;
6097 ++I;
6098
6099 // Memory Reference
6100 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6101 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6102
6103 unsigned DstReg = MI->getOperand(0).getReg();
6104 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6105 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6106 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6107 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6108
6109 MVT PVT = getPointerTy();
6110 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6111 "Invalid Pointer Size!");
6112 // For v = setjmp(buf), we generate
6113 //
6114 // thisMBB:
6115 // SjLjSetup mainMBB
6116 // bl mainMBB
6117 // v_restore = 1
6118 // b sinkMBB
6119 //
6120 // mainMBB:
6121 // buf[LabelOffset] = LR
6122 // v_main = 0
6123 //
6124 // sinkMBB:
6125 // v = phi(main, restore)
6126 //
6127
6128 MachineBasicBlock *thisMBB = MBB;
6129 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6130 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6131 MF->insert(I, mainMBB);
6132 MF->insert(I, sinkMBB);
6133
6134 MachineInstrBuilder MIB;
6135
6136 // Transfer the remainder of BB and its successor edges to sinkMBB.
6137 sinkMBB->splice(sinkMBB->begin(), MBB,
6138 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
6139 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6140
6141 // Note that the structure of the jmp_buf used here is not compatible
6142 // with that used by libc, and is not designed to be. Specifically, it
6143 // stores only those 'reserved' registers that LLVM does not otherwise
6144 // understand how to spill. Also, by convention, by the time this
6145 // intrinsic is called, Clang has already stored the frame address in the
6146 // first slot of the buffer and stack address in the third. Following the
6147 // X86 target code, we'll store the jump address in the second slot. We also
6148 // need to save the TOC pointer (R2) to handle jumps between shared
6149 // libraries, and that will be stored in the fourth slot. The thread
6150 // identifier (R13) is not affected.
6151
6152 // thisMBB:
6153 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6154 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006155 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006156
6157 // Prepare IP either in reg.
6158 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6159 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6160 unsigned BufReg = MI->getOperand(1).getReg();
6161
6162 if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) {
6163 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6164 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006165 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006166 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006167 MIB.setMemRefs(MMOBegin, MMOEnd);
6168 }
6169
Hal Finkelf05d6c72013-07-17 23:50:51 +00006170 // Naked functions never have a base pointer, and so we use r1. For all
6171 // other functions, this decision must be delayed until during PEI.
6172 unsigned BaseReg;
6173 if (MF->getFunction()->getAttributes().hasAttribute(
6174 AttributeSet::FunctionIndex, Attribute::Naked))
6175 BaseReg = PPCSubTarget.isPPC64() ? PPC::X1 : PPC::R1;
6176 else
6177 BaseReg = PPCSubTarget.isPPC64() ? PPC::BP8 : PPC::BP;
6178
6179 MIB = BuildMI(*thisMBB, MI, DL,
6180 TII->get(PPCSubTarget.isPPC64() ? PPC::STD : PPC::STW))
6181 .addReg(BaseReg)
6182 .addImm(BPOffset)
6183 .addReg(BufReg);
6184 MIB.setMemRefs(MMOBegin, MMOEnd);
6185
Hal Finkel756810f2013-03-21 21:37:52 +00006186 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00006187 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling5e7656b2013-06-07 07:55:53 +00006188 const PPCRegisterInfo *TRI =
6189 static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo());
6190 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00006191
6192 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6193
6194 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6195 .addMBB(mainMBB);
6196 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6197
6198 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6199 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6200
6201 // mainMBB:
6202 // mainDstReg = 0
6203 MIB = BuildMI(mainMBB, DL,
6204 TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
6205
6206 // Store IP
6207 if (PPCSubTarget.isPPC64()) {
6208 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6209 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006210 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006211 .addReg(BufReg);
6212 } else {
6213 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6214 .addReg(LabelReg)
6215 .addImm(LabelOffset)
6216 .addReg(BufReg);
6217 }
6218
6219 MIB.setMemRefs(MMOBegin, MMOEnd);
6220
6221 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6222 mainMBB->addSuccessor(sinkMBB);
6223
6224 // sinkMBB:
6225 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6226 TII->get(PPC::PHI), DstReg)
6227 .addReg(mainDstReg).addMBB(mainMBB)
6228 .addReg(restoreDstReg).addMBB(thisMBB);
6229
6230 MI->eraseFromParent();
6231 return sinkMBB;
6232}
6233
6234MachineBasicBlock *
6235PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6236 MachineBasicBlock *MBB) const {
6237 DebugLoc DL = MI->getDebugLoc();
6238 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6239
6240 MachineFunction *MF = MBB->getParent();
6241 MachineRegisterInfo &MRI = MF->getRegInfo();
6242
6243 // Memory Reference
6244 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6245 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6246
6247 MVT PVT = getPointerTy();
6248 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6249 "Invalid Pointer Size!");
6250
6251 const TargetRegisterClass *RC =
6252 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6253 unsigned Tmp = MRI.createVirtualRegister(RC);
6254 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6255 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6256 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006257 unsigned BP = (PVT == MVT::i64) ? PPC::X30 : PPC::R30;
Hal Finkel756810f2013-03-21 21:37:52 +00006258
6259 MachineInstrBuilder MIB;
6260
6261 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6262 const int64_t SPOffset = 2 * PVT.getStoreSize();
6263 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006264 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006265
6266 unsigned BufReg = MI->getOperand(0).getReg();
6267
6268 // Reload FP (the jumped-to function may not have had a
6269 // frame pointer, and if so, then its r31 will be restored
6270 // as necessary).
6271 if (PVT == MVT::i64) {
6272 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6273 .addImm(0)
6274 .addReg(BufReg);
6275 } else {
6276 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6277 .addImm(0)
6278 .addReg(BufReg);
6279 }
6280 MIB.setMemRefs(MMOBegin, MMOEnd);
6281
6282 // Reload IP
6283 if (PVT == MVT::i64) {
6284 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006285 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006286 .addReg(BufReg);
6287 } else {
6288 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6289 .addImm(LabelOffset)
6290 .addReg(BufReg);
6291 }
6292 MIB.setMemRefs(MMOBegin, MMOEnd);
6293
6294 // Reload SP
6295 if (PVT == MVT::i64) {
6296 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006297 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006298 .addReg(BufReg);
6299 } else {
6300 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6301 .addImm(SPOffset)
6302 .addReg(BufReg);
6303 }
6304 MIB.setMemRefs(MMOBegin, MMOEnd);
6305
Hal Finkelf05d6c72013-07-17 23:50:51 +00006306 // Reload BP
6307 if (PVT == MVT::i64) {
6308 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
6309 .addImm(BPOffset)
6310 .addReg(BufReg);
6311 } else {
6312 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
6313 .addImm(BPOffset)
6314 .addReg(BufReg);
6315 }
6316 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00006317
6318 // Reload TOC
6319 if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) {
6320 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006321 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006322 .addReg(BufReg);
6323
6324 MIB.setMemRefs(MMOBegin, MMOEnd);
6325 }
6326
6327 // Jump
6328 BuildMI(*MBB, MI, DL,
6329 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6330 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6331
6332 MI->eraseFromParent();
6333 return MBB;
6334}
6335
Dale Johannesena32affb2008-08-28 17:53:09 +00006336MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00006337PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00006338 MachineBasicBlock *BB) const {
Hal Finkel756810f2013-03-21 21:37:52 +00006339 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6340 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6341 return emitEHSjLjSetJmp(MI, BB);
6342 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6343 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6344 return emitEHSjLjLongJmp(MI, BB);
6345 }
6346
Evan Cheng20350c42006-11-27 23:37:22 +00006347 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00006348
6349 // To "insert" these instructions we actually have to insert their
6350 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00006351 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00006352 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00006353 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00006354
Dan Gohman3b460302008-07-07 23:14:23 +00006355 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00006356
Hal Finkel460e94d2012-06-22 23:10:08 +00006357 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6358 MI->getOpcode() == PPC::SELECT_CC_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00006359 SmallVector<MachineOperand, 2> Cond;
6360 Cond.push_back(MI->getOperand(4));
6361 Cond.push_back(MI->getOperand(1));
6362
Hal Finkel460e94d2012-06-22 23:10:08 +00006363 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00006364 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6365 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
6366 Cond, MI->getOperand(2).getReg(),
6367 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00006368 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6369 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6370 MI->getOpcode() == PPC::SELECT_CC_F4 ||
6371 MI->getOpcode() == PPC::SELECT_CC_F8 ||
6372 MI->getOpcode() == PPC::SELECT_CC_VRRC) {
6373
Evan Cheng32e376f2008-07-12 02:23:19 +00006374
6375 // The incoming instruction knows the destination vreg to set, the
6376 // condition code register to branch on, the true/false values to
6377 // select between, and a branch opcode to use.
6378
6379 // thisMBB:
6380 // ...
6381 // TrueVal = ...
6382 // cmpTY ccX, r1, r2
6383 // bCC copy1MBB
6384 // fallthrough --> copy0MBB
6385 MachineBasicBlock *thisMBB = BB;
6386 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6387 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
6388 unsigned SelectPred = MI->getOperand(4).getImm();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006389 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006390 F->insert(It, copy0MBB);
6391 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006392
6393 // Transfer the remainder of BB and its successor edges to sinkMBB.
6394 sinkMBB->splice(sinkMBB->begin(), BB,
6395 llvm::next(MachineBasicBlock::iterator(MI)),
6396 BB->end());
6397 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6398
Evan Cheng32e376f2008-07-12 02:23:19 +00006399 // Next, add the true and fallthrough blocks as its successors.
6400 BB->addSuccessor(copy0MBB);
6401 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006402
Dan Gohman34396292010-07-06 20:24:04 +00006403 BuildMI(BB, dl, TII->get(PPC::BCC))
6404 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6405
Evan Cheng32e376f2008-07-12 02:23:19 +00006406 // copy0MBB:
6407 // %FalseValue = ...
6408 // # fallthrough to sinkMBB
6409 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006410
Evan Cheng32e376f2008-07-12 02:23:19 +00006411 // Update machine-CFG edges
6412 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006413
Evan Cheng32e376f2008-07-12 02:23:19 +00006414 // sinkMBB:
6415 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6416 // ...
6417 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00006418 BuildMI(*BB, BB->begin(), dl,
6419 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00006420 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
6421 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6422 }
Dale Johannesena32affb2008-08-28 17:53:09 +00006423 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
6424 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
6425 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
6426 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006427 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
6428 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
6429 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
6430 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006431
6432 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
6433 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
6434 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
6435 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006436 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
6437 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
6438 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
6439 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006440
6441 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
6442 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
6443 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
6444 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006445 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
6446 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
6447 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
6448 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006449
6450 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
6451 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
6452 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
6453 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006454 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
6455 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
6456 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
6457 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006458
6459 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006460 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006461 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006462 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006463 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006464 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006465 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006466 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006467
6468 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6469 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6470 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6471 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006472 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6473 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6474 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6475 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006476
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006477 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6478 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6479 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6480 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6481 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6482 BB = EmitAtomicBinary(MI, BB, false, 0);
6483 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6484 BB = EmitAtomicBinary(MI, BB, true, 0);
6485
Evan Cheng32e376f2008-07-12 02:23:19 +00006486 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6487 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6488 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6489
6490 unsigned dest = MI->getOperand(0).getReg();
6491 unsigned ptrA = MI->getOperand(1).getReg();
6492 unsigned ptrB = MI->getOperand(2).getReg();
6493 unsigned oldval = MI->getOperand(3).getReg();
6494 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006495 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006496
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006497 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6498 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6499 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006500 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006501 F->insert(It, loop1MBB);
6502 F->insert(It, loop2MBB);
6503 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006504 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006505 exitMBB->splice(exitMBB->begin(), BB,
6506 llvm::next(MachineBasicBlock::iterator(MI)),
6507 BB->end());
6508 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006509
6510 // thisMBB:
6511 // ...
6512 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006513 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006514
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006515 // loop1MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006516 // l[wd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006517 // cmp[wd] dest, oldval
6518 // bne- midMBB
6519 // loop2MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006520 // st[wd]cx. newval, ptr
6521 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006522 // b exitBB
6523 // midMBB:
6524 // st[wd]cx. dest, ptr
6525 // exitBB:
6526 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006527 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00006528 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006529 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00006530 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006531 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006532 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6533 BB->addSuccessor(loop2MBB);
6534 BB->addSuccessor(midMBB);
6535
6536 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006537 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng32e376f2008-07-12 02:23:19 +00006538 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006539 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006540 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006541 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006542 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006543 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006544
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006545 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006546 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006547 .addReg(dest).addReg(ptrA).addReg(ptrB);
6548 BB->addSuccessor(exitMBB);
6549
Evan Cheng32e376f2008-07-12 02:23:19 +00006550 // exitMBB:
6551 // ...
6552 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00006553 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6554 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6555 // We must use 64-bit registers for addresses when targeting 64-bit,
6556 // since we're actually doing arithmetic on them. Other registers
6557 // can be 32-bit.
6558 bool is64bit = PPCSubTarget.isPPC64();
6559 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6560
6561 unsigned dest = MI->getOperand(0).getReg();
6562 unsigned ptrA = MI->getOperand(1).getReg();
6563 unsigned ptrB = MI->getOperand(2).getReg();
6564 unsigned oldval = MI->getOperand(3).getReg();
6565 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006566 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00006567
6568 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6569 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6570 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6571 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6572 F->insert(It, loop1MBB);
6573 F->insert(It, loop2MBB);
6574 F->insert(It, midMBB);
6575 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006576 exitMBB->splice(exitMBB->begin(), BB,
6577 llvm::next(MachineBasicBlock::iterator(MI)),
6578 BB->end());
6579 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006580
6581 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006582 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006583 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6584 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00006585 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6586 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6587 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6588 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6589 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6590 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6591 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6592 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6593 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6594 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6595 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6596 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6597 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6598 unsigned Ptr1Reg;
6599 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00006600 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00006601 // thisMBB:
6602 // ...
6603 // fallthrough --> loopMBB
6604 BB->addSuccessor(loop1MBB);
6605
6606 // The 4-byte load must be aligned, while a char or short may be
6607 // anywhere in the word. Hence all this nasty bookkeeping code.
6608 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6609 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006610 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00006611 // rlwinm ptr, ptr1, 0, 0, 29
6612 // slw newval2, newval, shift
6613 // slw oldval2, oldval,shift
6614 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6615 // slw mask, mask2, shift
6616 // and newval3, newval2, mask
6617 // and oldval3, oldval2, mask
6618 // loop1MBB:
6619 // lwarx tmpDest, ptr
6620 // and tmp, tmpDest, mask
6621 // cmpw tmp, oldval3
6622 // bne- midMBB
6623 // loop2MBB:
6624 // andc tmp2, tmpDest, mask
6625 // or tmp4, tmp2, newval3
6626 // stwcx. tmp4, ptr
6627 // bne- loop1MBB
6628 // b exitBB
6629 // midMBB:
6630 // stwcx. tmpDest, ptr
6631 // exitBB:
6632 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006633 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00006634 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006635 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006636 .addReg(ptrA).addReg(ptrB);
6637 } else {
6638 Ptr1Reg = ptrB;
6639 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006640 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006641 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006642 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006643 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6644 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006645 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006646 .addReg(Ptr1Reg).addImm(0).addImm(61);
6647 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006648 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006649 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006650 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006651 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006652 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006653 .addReg(oldval).addReg(ShiftReg);
6654 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006655 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00006656 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006657 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6658 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6659 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00006660 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006661 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006662 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006663 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006664 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006665 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006666 .addReg(OldVal2Reg).addReg(MaskReg);
6667
6668 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006669 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006670 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006671 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6672 .addReg(TmpDestReg).addReg(MaskReg);
6673 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00006674 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006675 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006676 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6677 BB->addSuccessor(loop2MBB);
6678 BB->addSuccessor(midMBB);
6679
6680 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006681 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6682 .addReg(TmpDestReg).addReg(MaskReg);
6683 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6684 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6685 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006686 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006687 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006688 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006689 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006690 BB->addSuccessor(loop1MBB);
6691 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006692
Dale Johannesen340d2642008-08-30 00:08:53 +00006693 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006694 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006695 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00006696 BB->addSuccessor(exitMBB);
6697
6698 // exitMBB:
6699 // ...
6700 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006701 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
6702 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00006703 } else if (MI->getOpcode() == PPC::FADDrtz) {
6704 // This pseudo performs an FADD with rounding mode temporarily forced
6705 // to round-to-zero. We emit this via custom inserter since the FPSCR
6706 // is not modeled at the SelectionDAG level.
6707 unsigned Dest = MI->getOperand(0).getReg();
6708 unsigned Src1 = MI->getOperand(1).getReg();
6709 unsigned Src2 = MI->getOperand(2).getReg();
6710 DebugLoc dl = MI->getDebugLoc();
6711
6712 MachineRegisterInfo &RegInfo = F->getRegInfo();
6713 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
6714
6715 // Save FPSCR value.
6716 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
6717
6718 // Set rounding mode to round-to-zero.
6719 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
6720 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
6721
6722 // Perform addition.
6723 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
6724
6725 // Restore FPSCR value.
6726 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00006727 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006728 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00006729 }
Chris Lattner9b577f12005-08-26 21:23:58 +00006730
Dan Gohman34396292010-07-06 20:24:04 +00006731 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00006732 return BB;
6733}
6734
Chris Lattner4211ca92006-04-14 06:01:58 +00006735//===----------------------------------------------------------------------===//
6736// Target Optimization Hooks
6737//===----------------------------------------------------------------------===//
6738
Hal Finkelb0c810f2013-04-03 17:44:56 +00006739SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
6740 DAGCombinerInfo &DCI) const {
Hal Finkel2e103312013-04-03 04:01:11 +00006741 if (DCI.isAfterLegalizeVectorOps())
6742 return SDValue();
6743
Hal Finkelb0c810f2013-04-03 17:44:56 +00006744 EVT VT = Op.getValueType();
6745
6746 if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) ||
6747 (VT == MVT::f64 && PPCSubTarget.hasFRE()) ||
6748 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel2e103312013-04-03 04:01:11 +00006749
6750 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6751 // For the reciprocal, we need to find the zero of the function:
6752 // F(X) = A X - 1 [which has a zero at X = 1/A]
6753 // =>
6754 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
6755 // does not require additional intermediate precision]
6756
6757 // Convergence is quadratic, so we essentially double the number of digits
6758 // correct after every iteration. The minimum architected relative
6759 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6760 // 23 digits and double has 52 digits.
6761 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00006762 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00006763 ++Iterations;
6764
6765 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006766 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006767
6768 SDValue FPOne =
Hal Finkelb0c810f2013-04-03 17:44:56 +00006769 DAG.getConstantFP(1.0, VT.getScalarType());
6770 if (VT.isVector()) {
6771 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00006772 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00006773 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel2e103312013-04-03 04:01:11 +00006774 FPOne, FPOne, FPOne, FPOne);
6775 }
6776
Hal Finkelb0c810f2013-04-03 17:44:56 +00006777 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006778 DCI.AddToWorklist(Est.getNode());
6779
6780 // Newton iterations: Est = Est + Est (1 - Arg * Est)
6781 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00006782 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00006783 DCI.AddToWorklist(NewEst.getNode());
6784
Hal Finkelb0c810f2013-04-03 17:44:56 +00006785 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006786 DCI.AddToWorklist(NewEst.getNode());
6787
Hal Finkelb0c810f2013-04-03 17:44:56 +00006788 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006789 DCI.AddToWorklist(NewEst.getNode());
6790
Hal Finkelb0c810f2013-04-03 17:44:56 +00006791 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006792 DCI.AddToWorklist(Est.getNode());
6793 }
6794
6795 return Est;
6796 }
6797
6798 return SDValue();
6799}
6800
Hal Finkelb0c810f2013-04-03 17:44:56 +00006801SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel2e103312013-04-03 04:01:11 +00006802 DAGCombinerInfo &DCI) const {
6803 if (DCI.isAfterLegalizeVectorOps())
6804 return SDValue();
6805
Hal Finkelb0c810f2013-04-03 17:44:56 +00006806 EVT VT = Op.getValueType();
6807
6808 if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) ||
6809 (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) ||
6810 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel2e103312013-04-03 04:01:11 +00006811
6812 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6813 // For the reciprocal sqrt, we need to find the zero of the function:
6814 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
6815 // =>
6816 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
6817 // As a result, we precompute A/2 prior to the iteration loop.
6818
6819 // Convergence is quadratic, so we essentially double the number of digits
6820 // correct after every iteration. The minimum architected relative
6821 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6822 // 23 digits and double has 52 digits.
6823 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00006824 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00006825 ++Iterations;
6826
6827 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006828 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006829
Hal Finkelb0c810f2013-04-03 17:44:56 +00006830 SDValue FPThreeHalves =
6831 DAG.getConstantFP(1.5, VT.getScalarType());
6832 if (VT.isVector()) {
6833 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00006834 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00006835 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
6836 FPThreeHalves, FPThreeHalves,
6837 FPThreeHalves, FPThreeHalves);
Hal Finkel2e103312013-04-03 04:01:11 +00006838 }
6839
Hal Finkelb0c810f2013-04-03 17:44:56 +00006840 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006841 DCI.AddToWorklist(Est.getNode());
6842
6843 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
6844 // this entire sequence requires only one FP constant.
Hal Finkelb0c810f2013-04-03 17:44:56 +00006845 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006846 DCI.AddToWorklist(HalfArg.getNode());
6847
Hal Finkelb0c810f2013-04-03 17:44:56 +00006848 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006849 DCI.AddToWorklist(HalfArg.getNode());
6850
6851 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
6852 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00006853 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00006854 DCI.AddToWorklist(NewEst.getNode());
6855
Hal Finkelb0c810f2013-04-03 17:44:56 +00006856 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006857 DCI.AddToWorklist(NewEst.getNode());
6858
Hal Finkelb0c810f2013-04-03 17:44:56 +00006859 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006860 DCI.AddToWorklist(NewEst.getNode());
6861
Hal Finkelb0c810f2013-04-03 17:44:56 +00006862 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006863 DCI.AddToWorklist(Est.getNode());
6864 }
6865
6866 return Est;
6867 }
6868
6869 return SDValue();
6870}
6871
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00006872// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
6873// not enforce equality of the chain operands.
6874static bool isConsecutiveLS(LSBaseSDNode *LS, LSBaseSDNode *Base,
6875 unsigned Bytes, int Dist,
6876 SelectionDAG &DAG) {
6877 EVT VT = LS->getMemoryVT();
6878 if (VT.getSizeInBits() / 8 != Bytes)
6879 return false;
6880
6881 SDValue Loc = LS->getBasePtr();
6882 SDValue BaseLoc = Base->getBasePtr();
6883 if (Loc.getOpcode() == ISD::FrameIndex) {
6884 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6885 return false;
6886 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6887 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6888 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6889 int FS = MFI->getObjectSize(FI);
6890 int BFS = MFI->getObjectSize(BFI);
6891 if (FS != BFS || FS != (int)Bytes) return false;
6892 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6893 }
6894
6895 // Handle X+C
6896 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6897 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6898 return true;
6899
6900 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6901 const GlobalValue *GV1 = NULL;
6902 const GlobalValue *GV2 = NULL;
6903 int64_t Offset1 = 0;
6904 int64_t Offset2 = 0;
6905 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6906 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6907 if (isGA1 && isGA2 && GV1 == GV2)
6908 return Offset1 == (Offset2 + Dist*Bytes);
6909 return false;
6910}
6911
Hal Finkel7d8a6912013-05-26 18:08:30 +00006912// Return true is there is a nearyby consecutive load to the one provided
6913// (regardless of alignment). We search up and down the chain, looking though
6914// token factors and other loads (but nothing else). As a result, a true
6915// results indicates that it is safe to create a new consecutive load adjacent
6916// to the load provided.
6917static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
6918 SDValue Chain = LD->getChain();
6919 EVT VT = LD->getMemoryVT();
6920
6921 SmallSet<SDNode *, 16> LoadRoots;
6922 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
6923 SmallSet<SDNode *, 16> Visited;
6924
6925 // First, search up the chain, branching to follow all token-factor operands.
6926 // If we find a consecutive load, then we're done, otherwise, record all
6927 // nodes just above the top-level loads and token factors.
6928 while (!Queue.empty()) {
6929 SDNode *ChainNext = Queue.pop_back_val();
6930 if (!Visited.insert(ChainNext))
6931 continue;
6932
6933 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00006934 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00006935 return true;
6936
6937 if (!Visited.count(ChainLD->getChain().getNode()))
6938 Queue.push_back(ChainLD->getChain().getNode());
6939 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
6940 for (SDNode::op_iterator O = ChainNext->op_begin(),
6941 OE = ChainNext->op_end(); O != OE; ++O)
6942 if (!Visited.count(O->getNode()))
6943 Queue.push_back(O->getNode());
6944 } else
6945 LoadRoots.insert(ChainNext);
6946 }
6947
6948 // Second, search down the chain, starting from the top-level nodes recorded
6949 // in the first phase. These top-level nodes are the nodes just above all
6950 // loads and token factors. Starting with their uses, recursively look though
6951 // all loads (just the chain uses) and token factors to find a consecutive
6952 // load.
6953 Visited.clear();
6954 Queue.clear();
6955
6956 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
6957 IE = LoadRoots.end(); I != IE; ++I) {
6958 Queue.push_back(*I);
6959
6960 while (!Queue.empty()) {
6961 SDNode *LoadRoot = Queue.pop_back_val();
6962 if (!Visited.insert(LoadRoot))
6963 continue;
6964
6965 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00006966 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00006967 return true;
6968
6969 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
6970 UE = LoadRoot->use_end(); UI != UE; ++UI)
6971 if (((isa<LoadSDNode>(*UI) &&
6972 cast<LoadSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
6973 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
6974 Queue.push_back(*UI);
6975 }
6976 }
6977
6978 return false;
6979}
6980
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006981SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
6982 DAGCombinerInfo &DCI) const {
Dan Gohman57c732b2010-04-21 01:34:56 +00006983 const TargetMachine &TM = getTargetMachine();
Chris Lattnerf4184352006-03-01 04:57:39 +00006984 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006985 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00006986 switch (N->getOpcode()) {
6987 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00006988 case PPCISD::SHL:
6989 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00006990 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00006991 return N->getOperand(0);
6992 }
6993 break;
6994 case PPCISD::SRL:
6995 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00006996 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00006997 return N->getOperand(0);
6998 }
6999 break;
7000 case PPCISD::SRA:
7001 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007002 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007003 C->isAllOnesValue()) // -1 >>s V -> -1.
7004 return N->getOperand(0);
7005 }
7006 break;
Hal Finkel2e103312013-04-03 04:01:11 +00007007 case ISD::FDIV: {
7008 assert(TM.Options.UnsafeFPMath &&
7009 "Reciprocal estimates require UnsafeFPMath");
Scott Michelcf0da6c2009-02-17 22:15:04 +00007010
Hal Finkel2e103312013-04-03 04:01:11 +00007011 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007012 SDValue RV =
7013 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007014 if (RV.getNode() != 0) {
7015 DCI.AddToWorklist(RV.getNode());
7016 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7017 N->getOperand(0), RV);
7018 }
Hal Finkelf96c18e2013-04-04 22:44:12 +00007019 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
7020 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7021 SDValue RV =
7022 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7023 DCI);
7024 if (RV.getNode() != 0) {
7025 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007026 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007027 N->getValueType(0), RV);
7028 DCI.AddToWorklist(RV.getNode());
7029 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7030 N->getOperand(0), RV);
7031 }
7032 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
7033 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7034 SDValue RV =
7035 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7036 DCI);
7037 if (RV.getNode() != 0) {
7038 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007039 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007040 N->getValueType(0), RV,
7041 N->getOperand(1).getOperand(1));
7042 DCI.AddToWorklist(RV.getNode());
7043 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7044 N->getOperand(0), RV);
7045 }
Hal Finkel2e103312013-04-03 04:01:11 +00007046 }
7047
Hal Finkelb0c810f2013-04-03 17:44:56 +00007048 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007049 if (RV.getNode() != 0) {
7050 DCI.AddToWorklist(RV.getNode());
7051 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7052 N->getOperand(0), RV);
7053 }
7054
7055 }
7056 break;
7057 case ISD::FSQRT: {
7058 assert(TM.Options.UnsafeFPMath &&
7059 "Reciprocal estimates require UnsafeFPMath");
7060
7061 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
7062 // reciprocal sqrt.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007063 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007064 if (RV.getNode() != 0) {
7065 DCI.AddToWorklist(RV.getNode());
Hal Finkelb0c810f2013-04-03 17:44:56 +00007066 RV = DAGCombineFastRecip(RV, DCI);
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007067 if (RV.getNode() != 0) {
7068 // Unfortunately, RV is now NaN if the input was exactly 0. Select out
7069 // this case and force the answer to 0.
7070
7071 EVT VT = RV.getValueType();
7072
7073 SDValue Zero = DAG.getConstantFP(0.0, VT.getScalarType());
7074 if (VT.isVector()) {
7075 assert(VT.getVectorNumElements() == 4 && "Unknown vector type");
7076 Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Zero, Zero, Zero, Zero);
7077 }
7078
7079 SDValue ZeroCmp =
7080 DAG.getSetCC(dl, getSetCCResultType(*DAG.getContext(), VT),
7081 N->getOperand(0), Zero, ISD::SETEQ);
7082 DCI.AddToWorklist(ZeroCmp.getNode());
7083 DCI.AddToWorklist(RV.getNode());
7084
7085 RV = DAG.getNode(VT.isVector() ? ISD::VSELECT : ISD::SELECT, dl, VT,
7086 ZeroCmp, Zero, RV);
Hal Finkel2e103312013-04-03 04:01:11 +00007087 return RV;
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007088 }
Hal Finkel2e103312013-04-03 04:01:11 +00007089 }
7090
7091 }
7092 break;
Chris Lattnerf4184352006-03-01 04:57:39 +00007093 case ISD::SINT_TO_FP:
Chris Lattnera35f3062006-06-16 17:34:12 +00007094 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007095 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
7096 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
7097 // We allow the src/dst to be either f32/f64, but the intermediate
7098 // type must be i64.
Owen Anderson9f944592009-08-11 20:47:22 +00007099 if (N->getOperand(0).getValueType() == MVT::i64 &&
7100 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007101 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007102 if (Val.getValueType() == MVT::f32) {
7103 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007104 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007105 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007106
Owen Anderson9f944592009-08-11 20:47:22 +00007107 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007108 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007109 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007110 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007111 if (N->getValueType(0) == MVT::f32) {
7112 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner72733e52008-01-17 07:00:52 +00007113 DAG.getIntPtrConstant(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007114 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007115 }
7116 return Val;
Owen Anderson9f944592009-08-11 20:47:22 +00007117 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007118 // If the intermediate type is i32, we can avoid the load/store here
7119 // too.
Chris Lattnerf4184352006-03-01 04:57:39 +00007120 }
Chris Lattnerf4184352006-03-01 04:57:39 +00007121 }
7122 }
7123 break;
Chris Lattner27f53452006-03-01 05:50:56 +00007124 case ISD::STORE:
7125 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
7126 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnerf5b46f72008-01-18 16:54:56 +00007127 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00007128 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00007129 N->getOperand(1).getValueType() == MVT::i32 &&
7130 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007131 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007132 if (Val.getValueType() == MVT::f32) {
7133 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007134 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007135 }
Owen Anderson9f944592009-08-11 20:47:22 +00007136 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007137 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007138
Hal Finkel60c75102013-04-01 15:37:53 +00007139 SDValue Ops[] = {
7140 N->getOperand(0), Val, N->getOperand(2),
7141 DAG.getValueType(N->getOperand(1).getValueType())
7142 };
7143
7144 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
7145 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
7146 cast<StoreSDNode>(N)->getMemoryVT(),
7147 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00007148 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007149 return Val;
7150 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007151
Chris Lattnera7976d32006-07-10 20:56:58 +00007152 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00007153 if (cast<StoreSDNode>(N)->isUnindexed() &&
7154 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00007155 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00007156 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00007157 N->getOperand(1).getValueType() == MVT::i16 ||
7158 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00007159 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007160 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007161 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00007162 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00007163 if (BSwapOp.getValueType() == MVT::i16)
7164 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00007165
Dan Gohman48b185d2009-09-25 20:36:54 +00007166 SDValue Ops[] = {
7167 N->getOperand(0), BSwapOp, N->getOperand(2),
7168 DAG.getValueType(N->getOperand(1).getValueType())
7169 };
7170 return
7171 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
7172 Ops, array_lengthof(Ops),
7173 cast<StoreSDNode>(N)->getMemoryVT(),
7174 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00007175 }
7176 break;
Hal Finkelcf2e9082013-05-24 23:00:14 +00007177 case ISD::LOAD: {
7178 LoadSDNode *LD = cast<LoadSDNode>(N);
7179 EVT VT = LD->getValueType(0);
7180 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
7181 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
7182 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
7183 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
Hal Finkel40c34782013-09-15 22:09:58 +00007184 (VT == MVT::v16i8 || VT == MVT::v8i16 ||
7185 VT == MVT::v4i32 || VT == MVT::v4f32) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +00007186 LD->getAlignment() < ABIAlignment) {
7187 // This is a type-legal unaligned Altivec load.
7188 SDValue Chain = LD->getChain();
7189 SDValue Ptr = LD->getBasePtr();
7190
7191 // This implements the loading of unaligned vectors as described in
7192 // the venerable Apple Velocity Engine overview. Specifically:
7193 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
7194 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
7195 //
7196 // The general idea is to expand a sequence of one or more unaligned
7197 // loads into a alignment-based permutation-control instruction (lvsl),
7198 // a series of regular vector loads (which always truncate their
7199 // input address to an aligned address), and a series of permutations.
7200 // The results of these permutations are the requested loaded values.
7201 // The trick is that the last "extra" load is not taken from the address
7202 // you might suspect (sizeof(vector) bytes after the last requested
7203 // load), but rather sizeof(vector) - 1 bytes after the last
7204 // requested vector. The point of this is to avoid a page fault if the
7205 // base address happend to be aligned. This works because if the base
7206 // address is aligned, then adding less than a full vector length will
7207 // cause the last vector in the sequence to be (re)loaded. Otherwise,
7208 // the next vector will be fetched as you might suspect was necessary.
7209
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007210 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00007211 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007212 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
7213 // optimization later.
Hal Finkelcf2e9082013-05-24 23:00:14 +00007214 SDValue PermCntl = BuildIntrinsicOp(Intrinsic::ppc_altivec_lvsl, Ptr,
7215 DAG, dl, MVT::v16i8);
7216
7217 // Refine the alignment of the original load (a "new" load created here
7218 // which was identical to the first except for the alignment would be
7219 // merged with the existing node regardless).
7220 MachineFunction &MF = DAG.getMachineFunction();
7221 MachineMemOperand *MMO =
7222 MF.getMachineMemOperand(LD->getPointerInfo(),
7223 LD->getMemOperand()->getFlags(),
7224 LD->getMemoryVT().getStoreSize(),
7225 ABIAlignment);
7226 LD->refineAlignment(MMO);
7227 SDValue BaseLoad = SDValue(LD, 0);
7228
7229 // Note that the value of IncOffset (which is provided to the next
7230 // load's pointer info offset value, and thus used to calculate the
7231 // alignment), and the value of IncValue (which is actually used to
7232 // increment the pointer value) are different! This is because we
7233 // require the next load to appear to be aligned, even though it
7234 // is actually offset from the base pointer by a lesser amount.
7235 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +00007236 int IncValue = IncOffset;
7237
7238 // Walk (both up and down) the chain looking for another load at the real
7239 // (aligned) offset (the alignment of the other load does not matter in
7240 // this case). If found, then do not use the offset reduction trick, as
7241 // that will prevent the loads from being later combined (as they would
7242 // otherwise be duplicates).
7243 if (!findConsecutiveLoad(LD, DAG))
7244 --IncValue;
7245
Hal Finkelcf2e9082013-05-24 23:00:14 +00007246 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
7247 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
7248
Hal Finkelcf2e9082013-05-24 23:00:14 +00007249 SDValue ExtraLoad =
7250 DAG.getLoad(VT, dl, Chain, Ptr,
7251 LD->getPointerInfo().getWithOffset(IncOffset),
7252 LD->isVolatile(), LD->isNonTemporal(),
7253 LD->isInvariant(), ABIAlignment);
7254
7255 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7256 BaseLoad.getValue(1), ExtraLoad.getValue(1));
7257
7258 if (BaseLoad.getValueType() != MVT::v4i32)
7259 BaseLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, BaseLoad);
7260
7261 if (ExtraLoad.getValueType() != MVT::v4i32)
7262 ExtraLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ExtraLoad);
7263
7264 SDValue Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
7265 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
7266
7267 if (VT != MVT::v4i32)
7268 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
7269
7270 // Now we need to be really careful about how we update the users of the
7271 // original load. We cannot just call DCI.CombineTo (or
7272 // DAG.ReplaceAllUsesWith for that matter), because the load still has
7273 // uses created here (the permutation for example) that need to stay.
7274 SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
7275 while (UI != UE) {
7276 SDUse &Use = UI.getUse();
7277 SDNode *User = *UI;
7278 // Note: BaseLoad is checked here because it might not be N, but a
7279 // bitcast of N.
7280 if (User == Perm.getNode() || User == BaseLoad.getNode() ||
7281 User == TF.getNode() || Use.getResNo() > 1) {
7282 ++UI;
7283 continue;
7284 }
7285
7286 SDValue To = Use.getResNo() ? TF : Perm;
7287 ++UI;
7288
7289 SmallVector<SDValue, 8> Ops;
7290 for (SDNode::op_iterator O = User->op_begin(),
7291 OE = User->op_end(); O != OE; ++O) {
7292 if (*O == Use)
7293 Ops.push_back(To);
7294 else
7295 Ops.push_back(*O);
7296 }
7297
7298 DAG.UpdateNodeOperands(User, Ops.data(), Ops.size());
7299 }
7300
7301 return SDValue(N, 0);
7302 }
7303 }
7304 break;
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007305 case ISD::INTRINSIC_WO_CHAIN:
7306 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() ==
7307 Intrinsic::ppc_altivec_lvsl &&
7308 N->getOperand(1)->getOpcode() == ISD::ADD) {
7309 SDValue Add = N->getOperand(1);
7310
7311 if (DAG.MaskedValueIsZero(Add->getOperand(1),
7312 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
7313 Add.getValueType().getScalarType().getSizeInBits()))) {
7314 SDNode *BasePtr = Add->getOperand(0).getNode();
7315 for (SDNode::use_iterator UI = BasePtr->use_begin(),
7316 UE = BasePtr->use_end(); UI != UE; ++UI) {
7317 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
7318 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
7319 Intrinsic::ppc_altivec_lvsl) {
7320 // We've found another LVSL, and this address if an aligned
7321 // multiple of that one. The results will be the same, so use the
7322 // one we've just found instead.
7323
7324 return SDValue(*UI, 0);
7325 }
7326 }
7327 }
7328 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +00007329
7330 break;
Chris Lattnera7976d32006-07-10 20:56:58 +00007331 case ISD::BSWAP:
7332 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +00007333 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +00007334 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007335 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
7336 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00007337 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007338 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007339 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00007340 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +00007341 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007342 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +00007343 LD->getChain(), // Chain
7344 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007345 DAG.getValueType(N->getValueType(0)) // VT
7346 };
Dan Gohman48b185d2009-09-25 20:36:54 +00007347 SDValue BSLoad =
7348 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +00007349 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
7350 MVT::i64 : MVT::i32, MVT::Other),
Hal Finkel93492fa2013-03-28 19:43:12 +00007351 Ops, 3, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00007352
Scott Michelcf0da6c2009-02-17 22:15:04 +00007353 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007354 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +00007355 if (N->getValueType(0) == MVT::i16)
7356 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007357
Chris Lattnera7976d32006-07-10 20:56:58 +00007358 // First, combine the bswap away. This makes the value produced by the
7359 // load dead.
7360 DCI.CombineTo(N, ResVal);
7361
7362 // Next, combine the load away, we give it a bogus result value but a real
7363 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +00007364 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00007365
Chris Lattnera7976d32006-07-10 20:56:58 +00007366 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007367 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +00007368 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007369
Chris Lattner27f53452006-03-01 05:50:56 +00007370 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +00007371 case PPCISD::VCMP: {
7372 // If a VCMPo node already exists with exactly the same operands as this
7373 // node, use its result instead of this node (VCMPo computes both a CR6 and
7374 // a normal output).
7375 //
7376 if (!N->getOperand(0).hasOneUse() &&
7377 !N->getOperand(1).hasOneUse() &&
7378 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00007379
Chris Lattnerd4058a52006-03-31 06:02:07 +00007380 // Scan all of the users of the LHS, looking for VCMPo's that match.
7381 SDNode *VCMPoNode = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007382
Gabor Greiff304a7a2008-08-28 21:40:38 +00007383 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +00007384 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
7385 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007386 if (UI->getOpcode() == PPCISD::VCMPo &&
7387 UI->getOperand(1) == N->getOperand(1) &&
7388 UI->getOperand(2) == N->getOperand(2) &&
7389 UI->getOperand(0) == N->getOperand(0)) {
7390 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +00007391 break;
7392 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007393
Chris Lattner518834c2006-04-18 18:28:22 +00007394 // If there is no VCMPo node, or if the flag value has a single use, don't
7395 // transform this.
7396 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
7397 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007398
7399 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +00007400 // chain, this transformation is more complex. Note that multiple things
7401 // could use the value result, which we should ignore.
7402 SDNode *FlagUser = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007403 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner518834c2006-04-18 18:28:22 +00007404 FlagUser == 0; ++UI) {
7405 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007406 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +00007407 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007408 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +00007409 FlagUser = User;
7410 break;
7411 }
7412 }
7413 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007414
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00007415 // If the user is a MFOCRF instruction, we know this is safe.
7416 // Otherwise we give up for right now.
7417 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007418 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +00007419 }
7420 break;
7421 }
Chris Lattner9754d142006-04-18 17:59:36 +00007422 case ISD::BR_CC: {
7423 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00007424 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +00007425 // lowering is done pre-legalize, because the legalizer lowers the predicate
7426 // compare down to code that is difficult to reassemble.
7427 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007428 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +00007429
7430 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
7431 // value. If so, pass-through the AND to get to the intrinsic.
7432 if (LHS.getOpcode() == ISD::AND &&
7433 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
7434 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
7435 Intrinsic::ppc_is_decremented_ctr_nonzero &&
7436 isa<ConstantSDNode>(LHS.getOperand(1)) &&
7437 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
7438 isZero())
7439 LHS = LHS.getOperand(0);
7440
7441 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
7442 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
7443 Intrinsic::ppc_is_decremented_ctr_nonzero &&
7444 isa<ConstantSDNode>(RHS)) {
7445 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
7446 "Counter decrement comparison is not EQ or NE");
7447
7448 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
7449 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
7450 (CC == ISD::SETNE && !Val);
7451
7452 // We now need to make the intrinsic dead (it cannot be instruction
7453 // selected).
7454 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
7455 assert(LHS.getNode()->hasOneUse() &&
7456 "Counter decrement has more than one use");
7457
7458 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
7459 N->getOperand(0), N->getOperand(4));
7460 }
7461
Chris Lattner9754d142006-04-18 17:59:36 +00007462 int CompareOpc;
7463 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007464
Chris Lattner9754d142006-04-18 17:59:36 +00007465 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
7466 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
7467 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
7468 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00007469
Chris Lattner9754d142006-04-18 17:59:36 +00007470 // If this is a comparison against something other than 0/1, then we know
7471 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +00007472 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00007473 if (Val != 0 && Val != 1) {
7474 if (CC == ISD::SETEQ) // Cond never true, remove branch.
7475 return N->getOperand(0);
7476 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +00007477 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +00007478 N->getOperand(0), N->getOperand(4));
7479 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007480
Chris Lattner9754d142006-04-18 17:59:36 +00007481 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007482
Chris Lattner9754d142006-04-18 17:59:36 +00007483 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007484 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007485 LHS.getOperand(2), // LHS of compare
7486 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +00007487 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007488 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00007489 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00007490 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007491
Chris Lattner9754d142006-04-18 17:59:36 +00007492 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007493 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +00007494 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +00007495 default: // Can't happen, don't crash on invalid number though.
7496 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007497 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +00007498 break;
7499 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007500 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +00007501 break;
7502 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007503 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +00007504 break;
7505 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007506 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +00007507 break;
7508 }
7509
Owen Anderson9f944592009-08-11 20:47:22 +00007510 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
7511 DAG.getConstant(CompOpc, MVT::i32),
7512 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +00007513 N->getOperand(4), CompNode.getValue(1));
7514 }
7515 break;
7516 }
Chris Lattnerf4184352006-03-01 04:57:39 +00007517 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007518
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007519 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +00007520}
7521
Chris Lattner4211ca92006-04-14 06:01:58 +00007522//===----------------------------------------------------------------------===//
7523// Inline Assembly Support
7524//===----------------------------------------------------------------------===//
7525
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007526void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Scott Michelcf0da6c2009-02-17 22:15:04 +00007527 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +00007528 APInt &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +00007529 const SelectionDAG &DAG,
Chris Lattnerc5287c02006-04-02 06:26:07 +00007530 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00007531 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +00007532 switch (Op.getOpcode()) {
7533 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +00007534 case PPCISD::LBRX: {
7535 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +00007536 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +00007537 KnownZero = 0xFFFF0000;
7538 break;
7539 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00007540 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00007541 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +00007542 default: break;
7543 case Intrinsic::ppc_altivec_vcmpbfp_p:
7544 case Intrinsic::ppc_altivec_vcmpeqfp_p:
7545 case Intrinsic::ppc_altivec_vcmpequb_p:
7546 case Intrinsic::ppc_altivec_vcmpequh_p:
7547 case Intrinsic::ppc_altivec_vcmpequw_p:
7548 case Intrinsic::ppc_altivec_vcmpgefp_p:
7549 case Intrinsic::ppc_altivec_vcmpgtfp_p:
7550 case Intrinsic::ppc_altivec_vcmpgtsb_p:
7551 case Intrinsic::ppc_altivec_vcmpgtsh_p:
7552 case Intrinsic::ppc_altivec_vcmpgtsw_p:
7553 case Intrinsic::ppc_altivec_vcmpgtub_p:
7554 case Intrinsic::ppc_altivec_vcmpgtuh_p:
7555 case Intrinsic::ppc_altivec_vcmpgtuw_p:
7556 KnownZero = ~1U; // All bits but the low one are known to be zero.
7557 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007558 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00007559 }
7560 }
7561}
7562
7563
Chris Lattnerd6855142007-03-25 02:14:49 +00007564/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +00007565/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007566PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00007567PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
7568 if (Constraint.size() == 1) {
7569 switch (Constraint[0]) {
7570 default: break;
7571 case 'b':
7572 case 'r':
7573 case 'f':
7574 case 'v':
7575 case 'y':
7576 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +00007577 case 'Z':
7578 // FIXME: While Z does indicate a memory constraint, it specifically
7579 // indicates an r+r address (used in conjunction with the 'y' modifier
7580 // in the replacement string). Currently, we're forcing the base
7581 // register to be r0 in the asm printer (which is interpreted as zero)
7582 // and forming the complete address in the second register. This is
7583 // suboptimal.
7584 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +00007585 }
7586 }
7587 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +00007588}
7589
John Thompsone8360b72010-10-29 17:29:13 +00007590/// Examine constraint type and operand type and determine a weight value.
7591/// This object must already have been set up with the operand type
7592/// and the current alternative constraint selected.
7593TargetLowering::ConstraintWeight
7594PPCTargetLowering::getSingleConstraintMatchWeight(
7595 AsmOperandInfo &info, const char *constraint) const {
7596 ConstraintWeight weight = CW_Invalid;
7597 Value *CallOperandVal = info.CallOperandVal;
7598 // If we don't have a value, we can't do a match,
7599 // but allow it at the lowest weight.
7600 if (CallOperandVal == NULL)
7601 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00007602 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00007603 // Look at the constraint type.
7604 switch (*constraint) {
7605 default:
7606 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7607 break;
7608 case 'b':
7609 if (type->isIntegerTy())
7610 weight = CW_Register;
7611 break;
7612 case 'f':
7613 if (type->isFloatTy())
7614 weight = CW_Register;
7615 break;
7616 case 'd':
7617 if (type->isDoubleTy())
7618 weight = CW_Register;
7619 break;
7620 case 'v':
7621 if (type->isVectorTy())
7622 weight = CW_Register;
7623 break;
7624 case 'y':
7625 weight = CW_Register;
7626 break;
Hal Finkel4f24c622012-11-05 18:18:42 +00007627 case 'Z':
7628 weight = CW_Memory;
7629 break;
John Thompsone8360b72010-10-29 17:29:13 +00007630 }
7631 return weight;
7632}
7633
Scott Michelcf0da6c2009-02-17 22:15:04 +00007634std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner584a11a2006-11-02 01:44:04 +00007635PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00007636 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +00007637 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +00007638 // GCC RS6000 Constraint Letters
7639 switch (Constraint[0]) {
7640 case 'b': // R1-R31
Hal Finkel638a9fa2013-03-19 18:51:05 +00007641 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
7642 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
7643 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00007644 case 'r': // R0-R31
Owen Anderson9f944592009-08-11 20:47:22 +00007645 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +00007646 return std::make_pair(0U, &PPC::G8RCRegClass);
7647 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00007648 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00007649 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +00007650 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00007651 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +00007652 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00007653 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007654 case 'v':
Craig Topperabadc662012-04-20 06:31:50 +00007655 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00007656 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +00007657 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00007658 }
7659 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007660
Hal Finkelb176acb2013-08-03 12:25:10 +00007661 std::pair<unsigned, const TargetRegisterClass*> R =
7662 TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7663
7664 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
7665 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
7666 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
7667 // register.
7668 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
7669 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
7670 if (R.first && VT == MVT::i64 && PPCSubTarget.isPPC64() &&
7671 PPC::GPRCRegClass.contains(R.first)) {
7672 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
7673 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +00007674 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +00007675 &PPC::G8RCRegClass);
7676 }
7677
7678 return R;
Chris Lattner01513612006-01-31 19:20:21 +00007679}
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007680
Chris Lattner584a11a2006-11-02 01:44:04 +00007681
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007682/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +00007683/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +00007684void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00007685 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007686 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00007687 SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007688 SDValue Result(0,0);
Eric Christopher0713a9d2011-06-08 23:55:35 +00007689
Eric Christopherde9399b2011-06-02 23:16:42 +00007690 // Only support length 1 constraints.
7691 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +00007692
Eric Christopherde9399b2011-06-02 23:16:42 +00007693 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007694 switch (Letter) {
7695 default: break;
7696 case 'I':
7697 case 'J':
7698 case 'K':
7699 case 'L':
7700 case 'M':
7701 case 'N':
7702 case 'O':
7703 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +00007704 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007705 if (!CST) return; // Must be an immediate to match.
Dan Gohmaneffb8942008-09-12 16:56:44 +00007706 unsigned Value = CST->getZExtValue();
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007707 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007708 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007709 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007710 if ((short)Value == (int)Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007711 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007712 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007713 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
7714 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007715 if ((short)Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007716 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007717 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007718 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007719 if ((Value >> 16) == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007720 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007721 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007722 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007723 if (Value > 31)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007724 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007725 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007726 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007727 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007728 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007729 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007730 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007731 if (Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007732 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007733 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007734 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007735 if ((short)-Value == (int)-Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007736 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007737 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007738 }
7739 break;
7740 }
7741 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007742
Gabor Greiff304a7a2008-08-28 21:40:38 +00007743 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007744 Ops.push_back(Result);
7745 return;
7746 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007747
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007748 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +00007749 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007750}
Evan Cheng2dd2c652006-03-13 23:20:37 +00007751
Chris Lattner1eb94d92007-03-30 23:15:24 +00007752// isLegalAddressingMode - Return true if the addressing mode represented
7753// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007754bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00007755 Type *Ty) const {
Chris Lattner1eb94d92007-03-30 23:15:24 +00007756 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelcf0da6c2009-02-17 22:15:04 +00007757
Chris Lattner1eb94d92007-03-30 23:15:24 +00007758 // PPC allows a sign-extended 16-bit immediate field.
7759 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
7760 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007761
Chris Lattner1eb94d92007-03-30 23:15:24 +00007762 // No global is ever allowed as a base.
7763 if (AM.BaseGV)
7764 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007765
7766 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +00007767 switch (AM.Scale) {
7768 case 0: // "r+i" or just "i", depending on HasBaseReg.
7769 break;
7770 case 1:
7771 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
7772 return false;
7773 // Otherwise we have r+r or r+i.
7774 break;
7775 case 2:
7776 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
7777 return false;
7778 // Allow 2*r as r+r.
7779 break;
Chris Lattner19ccd622007-04-09 22:10:05 +00007780 default:
7781 // No other scales are supported.
7782 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +00007783 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007784
Chris Lattner1eb94d92007-03-30 23:15:24 +00007785 return true;
7786}
7787
Dan Gohman21cea8a2010-04-17 15:26:15 +00007788SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
7789 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00007790 MachineFunction &MF = DAG.getMachineFunction();
7791 MachineFrameInfo *MFI = MF.getFrameInfo();
7792 MFI->setReturnAddressIsTaken(true);
7793
Andrew Trickef9de2a2013-05-25 02:42:55 +00007794 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007795 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +00007796
Dale Johannesen81bfca72010-05-03 22:59:34 +00007797 // Make sure the function does not optimize away the store of the RA to
7798 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +00007799 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +00007800 FuncInfo->setLRStoreRequired();
7801 bool isPPC64 = PPCSubTarget.isPPC64();
7802 bool isDarwinABI = PPCSubTarget.isDarwinABI();
7803
7804 if (Depth > 0) {
7805 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7806 SDValue Offset =
Wesley Peck527da1b2010-11-23 03:31:01 +00007807
Anton Korobeynikov2f931282011-01-10 12:39:04 +00007808 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen81bfca72010-05-03 22:59:34 +00007809 isPPC64? MVT::i64 : MVT::i32);
7810 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
7811 DAG.getNode(ISD::ADD, dl, getPointerTy(),
7812 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00007813 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007814 }
Chris Lattnerf6a81562007-12-08 06:59:59 +00007815
Chris Lattnerf6a81562007-12-08 06:59:59 +00007816 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007817 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007818 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00007819 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +00007820}
7821
Dan Gohman21cea8a2010-04-17 15:26:15 +00007822SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
7823 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007824 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007825 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00007826
Owen Anderson53aa7a92009-08-10 22:56:29 +00007827 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00007828 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007829
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00007830 MachineFunction &MF = DAG.getMachineFunction();
7831 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +00007832 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +00007833
7834 // Naked functions never have a frame pointer, and so we use r1. For all
7835 // other functions, this decision must be delayed until during PEI.
7836 unsigned FrameReg;
7837 if (MF.getFunction()->getAttributes().hasAttribute(
7838 AttributeSet::FunctionIndex, Attribute::Naked))
7839 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
7840 else
7841 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
7842
Dale Johannesen81bfca72010-05-03 22:59:34 +00007843 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
7844 PtrVT);
7845 while (Depth--)
7846 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00007847 FrameAddr, MachinePointerInfo(), false, false,
7848 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007849 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00007850}
Dan Gohmanc14e5222008-10-21 03:41:46 +00007851
7852bool
7853PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7854 // The PowerPC target isn't yet aware of offsets.
7855 return false;
7856}
Tilmann Schellerb93960d2009-07-03 06:45:56 +00007857
Evan Chengd9929f02010-04-01 20:10:42 +00007858/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +00007859/// and store operations as a result of memset, memcpy, and memmove
7860/// lowering. If DstAlign is zero that means it's safe to destination
7861/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
7862/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +00007863/// probably because the source does not need to be loaded. If 'IsMemset' is
7864/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
7865/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
7866/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +00007867/// It returns EVT::Other if the type should be determined using generic
7868/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +00007869EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
7870 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00007871 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00007872 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +00007873 MachineFunction &MF) const {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00007874 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +00007875 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00007876 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00007877 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00007878 }
7879}
Hal Finkel88ed4e32012-04-01 19:23:08 +00007880
Hal Finkel8d7fbc92013-03-15 15:27:13 +00007881bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
7882 bool *Fast) const {
7883 if (DisablePPCUnaligned)
7884 return false;
7885
7886 // PowerPC supports unaligned memory access for simple non-vector types.
7887 // Although accessing unaligned addresses is not as efficient as accessing
7888 // aligned addresses, it is generally more efficient than manual expansion,
7889 // and generally only traps for software emulation when crossing page
7890 // boundaries.
7891
7892 if (!VT.isSimple())
7893 return false;
7894
7895 if (VT.getSimpleVT().isVector())
7896 return false;
7897
7898 if (VT == MVT::ppcf128)
7899 return false;
7900
7901 if (Fast)
7902 *Fast = true;
7903
7904 return true;
7905}
7906
Stephen Lin73de7bf2013-07-09 18:16:56 +00007907bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
7908 VT = VT.getScalarType();
7909
Hal Finkel0a479ae2012-06-22 00:49:52 +00007910 if (!VT.isSimple())
7911 return false;
7912
7913 switch (VT.getSimpleVT().SimpleTy) {
7914 case MVT::f32:
7915 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +00007916 return true;
7917 default:
7918 break;
7919 }
7920
7921 return false;
7922}
7923
Hal Finkel88ed4e32012-04-01 19:23:08 +00007924Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel21442b22013-09-11 23:05:25 +00007925 if (DisableILPPref || PPCSubTarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +00007926 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +00007927
Hal Finkel4e9f1a82012-06-10 19:32:29 +00007928 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +00007929}
7930
Bill Schmidt0cf702f2013-07-30 00:50:39 +00007931// Create a fast isel object.
7932FastISel *
7933PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
7934 const TargetLibraryInfo *LibInfo) const {
7935 return PPC::createFastISel(FuncInfo, LibInfo);
7936}