blob: c50811d52bef8289a22387168e17914c4ebef385 [file] [log] [blame]
Nate Begeman1d9d7422005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Lattner7c5a3d32005-08-16 17:14:42 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattner7c5a3d32005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner16e71f22005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Jim Laskey2f616bf2006-11-16 22:43:37 +000016#include "PPCMachineFunctionInfo.h"
Bill Wendling53351a12010-03-12 02:00:43 +000017#include "PPCPerfectShuffle.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000018#include "PPCTargetMachine.h"
Owen Anderson718cb662007-09-07 04:06:50 +000019#include "llvm/ADT/STLExtras.h"
Chris Lattnerb9a7bea2007-03-06 00:59:59 +000020#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000025#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000026#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000027#include "llvm/IR/CallingConv.h"
28#include "llvm/IR/Constants.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/Intrinsics.h"
Chris Lattner4eab7142006-11-10 02:08:47 +000032#include "llvm/Support/CommandLine.h"
Torok Edwindac237e2009-07-08 20:53:28 +000033#include "llvm/Support/ErrorHandling.h"
Craig Topper79aa3412012-03-17 18:46:09 +000034#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000035#include "llvm/Support/raw_ostream.h"
Craig Topper79aa3412012-03-17 18:46:09 +000036#include "llvm/Target/TargetOptions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000037using namespace llvm;
38
Bill Schmidt212af6a2013-02-06 17:33:58 +000039static bool CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
40 CCValAssign::LocInfo &LocInfo,
41 ISD::ArgFlagsTy &ArgFlags,
42 CCState &State);
43static bool CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +000044 MVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +000045 CCValAssign::LocInfo &LocInfo,
46 ISD::ArgFlagsTy &ArgFlags,
47 CCState &State);
Bill Schmidt212af6a2013-02-06 17:33:58 +000048static bool CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
49 MVT &LocVT,
50 CCValAssign::LocInfo &LocInfo,
51 ISD::ArgFlagsTy &ArgFlags,
52 CCState &State);
Tilmann Schellerffd02002009-07-03 06:45:56 +000053
Hal Finkel77838f92012-06-04 02:21:00 +000054static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
55cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattner4eab7142006-11-10 02:08:47 +000056
Hal Finkel71ffcfe2012-06-10 19:32:29 +000057static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
58cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
59
Hal Finkel2d37f7b2013-03-15 15:27:13 +000060static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
61cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
62
Chris Lattnerf0144122009-07-28 03:13:23 +000063static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) {
64 if (TM.getSubtargetImpl()->isDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +000065 return new TargetLoweringObjectFileMachO();
Bill Wendling53351a12010-03-12 02:00:43 +000066
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000067 return new TargetLoweringObjectFileELF();
Chris Lattnerf0144122009-07-28 03:13:23 +000068}
69
Chris Lattner331d1bc2006-11-02 01:44:04 +000070PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000071 : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) {
Evan Cheng769951f2012-07-02 22:39:56 +000072 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
Scott Michelfdc40a02009-02-17 22:15:04 +000073
Nate Begeman405e3ec2005-10-21 00:02:42 +000074 setPow2DivIsCheap();
Dale Johannesen72324642008-07-31 18:13:12 +000075
Chris Lattnerd145a612005-09-27 22:18:25 +000076 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000077 setUseUnderscoreSetJmp(true);
78 setUseUnderscoreLongJmp(true);
Scott Michelfdc40a02009-02-17 22:15:04 +000079
Chris Lattner749dc722010-10-10 18:34:00 +000080 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
81 // arguments are at least 4/8 bytes aligned.
Evan Cheng769951f2012-07-02 22:39:56 +000082 bool isPPC64 = Subtarget->isPPC64();
83 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000084
Chris Lattner7c5a3d32005-08-16 17:14:42 +000085 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +000086 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
87 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
88 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelfdc40a02009-02-17 22:15:04 +000089
Evan Chengc5484282006-10-04 00:56:09 +000090 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson825b72b2009-08-11 20:47:22 +000091 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
92 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sandsf9c98e62008-01-23 20:39:46 +000093
Owen Anderson825b72b2009-08-11 20:47:22 +000094 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +000095
Chris Lattner94e509c2006-11-10 23:58:45 +000096 // PowerPC has pre-inc load and store's.
Owen Anderson825b72b2009-08-11 20:47:22 +000097 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
98 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
99 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
100 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
101 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
102 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
103 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
104 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
105 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
106 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Chengcd633192006-11-09 19:11:50 +0000107
Dale Johannesen6eaeff22007-10-10 01:01:31 +0000108 // This is used in the ppcf128->int sequence. Note it has different semantics
109 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000110 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesen638ccd52007-10-06 01:24:11 +0000111
Roman Divacky0016f732012-08-16 18:19:29 +0000112 // We do not currently implement these libm ops for PowerPC.
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000113 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
114 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
115 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
116 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
117 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
118
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000119 // PowerPC has no SREM/UREM instructions
Owen Anderson825b72b2009-08-11 20:47:22 +0000120 setOperationAction(ISD::SREM, MVT::i32, Expand);
121 setOperationAction(ISD::UREM, MVT::i32, Expand);
122 setOperationAction(ISD::SREM, MVT::i64, Expand);
123 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman3ce990d2007-10-08 17:28:24 +0000124
125 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
127 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
128 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
129 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
130 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
131 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
132 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
133 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000134
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000135 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setOperationAction(ISD::FSIN , MVT::f64, Expand);
137 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000138 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::FREM , MVT::f64, Expand);
140 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel070b8db2012-06-22 00:49:52 +0000141 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000142 setOperationAction(ISD::FSIN , MVT::f32, Expand);
143 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000144 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setOperationAction(ISD::FREM , MVT::f32, Expand);
146 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel070b8db2012-06-22 00:49:52 +0000147 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c5eb802008-01-18 19:55:37 +0000148
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000150
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000151 // If we're enabling GP optimizations, use hardware square root
Evan Cheng769951f2012-07-02 22:39:56 +0000152 if (!Subtarget->hasFSQRT()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
154 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000155 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000156
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
158 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000159
Nate Begemand88fc032006-01-14 03:14:10 +0000160 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
162 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
163 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000164 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
165 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
167 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
168 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000169 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
170 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000171
Nate Begeman35ef9132006-01-11 21:21:00 +0000172 // PowerPC does not have ROTR
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
174 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000175
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000176 // PowerPC does not have Select
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::SELECT, MVT::i32, Expand);
178 setOperationAction(ISD::SELECT, MVT::i64, Expand);
179 setOperationAction(ISD::SELECT, MVT::f32, Expand);
180 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000181
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000182 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
184 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +0000185
Nate Begeman750ac1b2006-02-01 07:19:44 +0000186 // PowerPC wants to optimize integer setcc a bit
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000188
Nate Begeman81e80972006-03-17 01:40:33 +0000189 // PowerPC does not have BRCOND which requires SetCC
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Chengc35497f2006-10-30 08:02:39 +0000191
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000193
Chris Lattnerf7605322005-08-31 21:09:52 +0000194 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000196
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000197 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
199 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000200
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000201 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
202 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
203 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
204 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattner53e88452005-12-23 05:13:35 +0000205
Chris Lattner25b8b8c2006-04-28 21:56:10 +0000206 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000208
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
210 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
211 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
212 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000213
214
215 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman28a6b022005-12-10 02:36:00 +0000216 // appropriate instructions to materialize the address.
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
218 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000219 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
221 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
222 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
223 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000224 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
226 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000227
Nate Begeman1db3c922008-08-11 17:36:31 +0000228 // TRAP is legal.
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling77959322008-09-17 00:30:57 +0000230
231 // TRAMPOLINE is custom lowered.
Duncan Sands4a544a72011-09-06 13:37:06 +0000232 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
233 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling77959322008-09-17 00:30:57 +0000234
Nate Begemanacc398c2006-01-25 18:21:52 +0000235 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000237
Evan Cheng769951f2012-07-02 22:39:56 +0000238 if (Subtarget->isSVR4ABI()) {
239 if (isPPC64) {
Hal Finkel179a4dd2012-03-24 03:53:55 +0000240 // VAARG always uses double-word chunks, so promote anything smaller.
241 setOperationAction(ISD::VAARG, MVT::i1, Promote);
242 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
243 setOperationAction(ISD::VAARG, MVT::i8, Promote);
244 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
245 setOperationAction(ISD::VAARG, MVT::i16, Promote);
246 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
247 setOperationAction(ISD::VAARG, MVT::i32, Promote);
248 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
249 setOperationAction(ISD::VAARG, MVT::Other, Expand);
250 } else {
251 // VAARG is custom lowered with the 32-bit SVR4 ABI.
252 setOperationAction(ISD::VAARG, MVT::Other, Custom);
253 setOperationAction(ISD::VAARG, MVT::i64, Custom);
254 }
Roman Divackybdb226e2011-06-28 15:30:42 +0000255 } else
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000257
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000258 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
260 setOperationAction(ISD::VAEND , MVT::Other, Expand);
261 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
262 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
263 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
264 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattner56a752e2006-10-18 01:18:48 +0000265
Chris Lattner6d92cad2006-03-26 10:06:40 +0000266 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000268
Dale Johannesen53e4e442008-11-07 22:54:33 +0000269 // Comparisons that require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
271 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
272 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
273 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
274 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
275 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
276 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
277 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
278 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
279 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
280 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
281 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000282
Evan Cheng769951f2012-07-02 22:39:56 +0000283 if (Subtarget->has64BitSupport()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000284 // They also have instructions for converting between i64 and fp.
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
286 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
287 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
288 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen4c9369d2009-06-04 20:53:52 +0000289 // This is just the low 32 bits of a (signed) fp->i64 conversion.
290 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000292
Chris Lattner7fbcef72006-03-24 07:53:47 +0000293 // FIXME: disable this lowered code. This generates 64-bit register values,
294 // and we don't model the fact that the top part is clobbered by calls. We
295 // need to flag these together so that the value isn't live across a call.
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000297 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000298 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000300 }
301
Evan Cheng769951f2012-07-02 22:39:56 +0000302 if (Subtarget->use64BitRegs()) {
Chris Lattner26cb2862007-10-19 04:08:28 +0000303 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperc9099502012-04-20 06:31:50 +0000304 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000305 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman9ed06db2008-03-07 20:36:53 +0000307 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
309 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
310 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000311 } else {
Chris Lattner26cb2862007-10-19 04:08:28 +0000312 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
314 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
315 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000316 }
Evan Chengd30bf012006-03-01 01:11:20 +0000317
Evan Cheng769951f2012-07-02 22:39:56 +0000318 if (Subtarget->hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000319 // First set operation action for all vector types to expand. Then we
320 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
322 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
323 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000324
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000325 // add/sub are legal for all supported vector VT's.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000326 setOperationAction(ISD::ADD , VT, Legal);
327 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000328
Chris Lattner7ff7e672006-04-04 17:25:31 +0000329 // We promote all shuffles to v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000330 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000332
333 // We promote all non-typed operations to v4i32.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000334 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000336 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000338 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000340 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000342 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000344 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +0000346
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000347 // No other operations are legal.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000348 setOperationAction(ISD::MUL , VT, Expand);
349 setOperationAction(ISD::SDIV, VT, Expand);
350 setOperationAction(ISD::SREM, VT, Expand);
351 setOperationAction(ISD::UDIV, VT, Expand);
352 setOperationAction(ISD::UREM, VT, Expand);
353 setOperationAction(ISD::FDIV, VT, Expand);
354 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topper44e394c2012-11-15 08:02:19 +0000355 setOperationAction(ISD::FSQRT, VT, Expand);
356 setOperationAction(ISD::FLOG, VT, Expand);
357 setOperationAction(ISD::FLOG10, VT, Expand);
358 setOperationAction(ISD::FLOG2, VT, Expand);
359 setOperationAction(ISD::FEXP, VT, Expand);
360 setOperationAction(ISD::FEXP2, VT, Expand);
361 setOperationAction(ISD::FSIN, VT, Expand);
362 setOperationAction(ISD::FCOS, VT, Expand);
363 setOperationAction(ISD::FABS, VT, Expand);
364 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topper1ab489a2012-11-14 08:11:25 +0000365 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000366 setOperationAction(ISD::FCEIL, VT, Expand);
367 setOperationAction(ISD::FTRUNC, VT, Expand);
368 setOperationAction(ISD::FRINT, VT, Expand);
369 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000370 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
371 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
372 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
373 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
374 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
375 setOperationAction(ISD::UDIVREM, VT, Expand);
376 setOperationAction(ISD::SDIVREM, VT, Expand);
377 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
378 setOperationAction(ISD::FPOW, VT, Expand);
379 setOperationAction(ISD::CTPOP, VT, Expand);
380 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000381 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000382 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000383 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramer91223a42012-12-19 15:49:14 +0000384 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellacfe09ed2012-11-05 17:15:56 +0000385 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
386
387 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
388 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
389 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
390 setTruncStoreAction(VT, InnerVT, Expand);
391 }
392 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
393 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
394 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000395 }
396
Chris Lattner7ff7e672006-04-04 17:25:31 +0000397 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
398 // with merges, splats, etc.
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000400
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::AND , MVT::v4i32, Legal);
402 setOperationAction(ISD::OR , MVT::v4i32, Legal);
403 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
404 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
405 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
406 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella51aaadb2012-10-08 17:27:24 +0000407 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
408 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
409 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
410 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellae95ed2b2012-11-15 20:56:03 +0000411 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
412 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
413 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
414 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000415
Craig Topperc9099502012-04-20 06:31:50 +0000416 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
417 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
418 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
419 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelfdc40a02009-02-17 22:15:04 +0000420
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel070b8db2012-06-22 00:49:52 +0000422 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
424 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
425 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000426
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
428 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000429
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
431 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
432 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
433 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella5f41fd62012-10-30 13:50:19 +0000434
435 // Altivec does not contain unordered floating-point compare instructions
436 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
437 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
438 setCondCodeAction(ISD::SETUGT, MVT::v4f32, Expand);
439 setCondCodeAction(ISD::SETUGE, MVT::v4f32, Expand);
440 setCondCodeAction(ISD::SETULT, MVT::v4f32, Expand);
441 setCondCodeAction(ISD::SETULE, MVT::v4f32, Expand);
Nate Begeman425a9692005-11-29 08:17:20 +0000442 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000443
Hal Finkel8cc34742012-08-04 14:10:46 +0000444 if (Subtarget->has64BitSupport()) {
Hal Finkel19aa2b52012-04-01 20:08:17 +0000445 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel8cc34742012-08-04 14:10:46 +0000446 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
447 }
Hal Finkel19aa2b52012-04-01 20:08:17 +0000448
Eli Friedman4db5aca2011-08-29 18:23:02 +0000449 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
450 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkelcd9ea512012-12-25 17:22:53 +0000451 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
452 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000453
Duncan Sands03228082008-11-23 15:47:28 +0000454 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000455 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Scott Michelfdc40a02009-02-17 22:15:04 +0000456
Evan Cheng769951f2012-07-02 22:39:56 +0000457 if (isPPC64) {
Chris Lattner10da9572006-10-18 01:20:43 +0000458 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000459 setExceptionPointerRegister(PPC::X3);
460 setExceptionSelectorRegister(PPC::X4);
461 } else {
Chris Lattner10da9572006-10-18 01:20:43 +0000462 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000463 setExceptionPointerRegister(PPC::R3);
464 setExceptionSelectorRegister(PPC::R4);
465 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000466
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000467 // We have target-specific dag combine patterns for the following nodes:
468 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000469 setTargetDAGCombine(ISD::STORE);
Chris Lattner90564f22006-04-18 17:59:36 +0000470 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnerd9989382006-07-10 20:56:58 +0000471 setTargetDAGCombine(ISD::BSWAP);
Scott Michelfdc40a02009-02-17 22:15:04 +0000472
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000473 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng769951f2012-07-02 22:39:56 +0000474 if (Subtarget->isDarwin()) {
Duncan Sands007f9842008-01-10 10:28:30 +0000475 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000476 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
477 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands007f9842008-01-10 10:28:30 +0000478 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
479 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000480 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
481 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
482 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
483 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
484 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000485 }
486
Hal Finkelc6129162011-10-17 18:53:03 +0000487 setMinFunctionAlignment(2);
488 if (PPCSubTarget.isDarwin())
489 setPrefFunctionAlignment(4);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000490
Evan Cheng769951f2012-07-02 22:39:56 +0000491 if (isPPC64 && Subtarget->isJITCodeModel())
492 // Temporary workaround for the inability of PPC64 JIT to handle jump
493 // tables.
494 setSupportJumpTables(false);
495
Eli Friedman26689ac2011-08-03 21:06:02 +0000496 setInsertFencesForAtomic(true);
497
Hal Finkel768c65f2011-11-22 16:21:04 +0000498 setSchedulingPreference(Sched::Hybrid);
499
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000500 computeRegisterProperties();
Hal Finkel621b77a2012-08-28 16:12:39 +0000501
502 // The Freescale cores does better with aggressive inlining of memcpy and
503 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
504 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
505 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach3450f802013-02-20 21:13:59 +0000506 MaxStoresPerMemset = 32;
507 MaxStoresPerMemsetOptSize = 16;
508 MaxStoresPerMemcpy = 32;
509 MaxStoresPerMemcpyOptSize = 8;
510 MaxStoresPerMemmove = 32;
511 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel621b77a2012-08-28 16:12:39 +0000512
513 setPrefFunctionAlignment(4);
Jim Grosbach3450f802013-02-20 21:13:59 +0000514 BenefitFromCodePlacementOpt = true;
Hal Finkel621b77a2012-08-28 16:12:39 +0000515 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000516}
517
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000518/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
519/// function arguments in the caller parameter area.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000520unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dan Gohmanf0757b02010-04-21 01:34:56 +0000521 const TargetMachine &TM = getTargetMachine();
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000522 // Darwin passes everything on 4 byte boundary.
523 if (TM.getSubtarget<PPCSubtarget>().isDarwin())
524 return 4;
Roman Divacky466958c2012-04-02 15:49:30 +0000525
526 // 16byte and wider vectors are passed on 16byte boundary.
527 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
528 if (VTy->getBitWidth() >= 128)
529 return 16;
530
531 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
532 if (PPCSubTarget.isPPC64())
533 return 8;
534
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000535 return 4;
536}
537
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000538const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
539 switch (Opcode) {
540 default: return 0;
Evan Cheng53301922008-07-12 02:23:19 +0000541 case PPCISD::FSEL: return "PPCISD::FSEL";
542 case PPCISD::FCFID: return "PPCISD::FCFID";
543 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
544 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
545 case PPCISD::STFIWX: return "PPCISD::STFIWX";
546 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
547 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
548 case PPCISD::VPERM: return "PPCISD::VPERM";
549 case PPCISD::Hi: return "PPCISD::Hi";
550 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000551 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller3a84dae2009-12-18 13:00:15 +0000552 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
553 case PPCISD::LOAD: return "PPCISD::LOAD";
554 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng53301922008-07-12 02:23:19 +0000555 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
556 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
557 case PPCISD::SRL: return "PPCISD::SRL";
558 case PPCISD::SRA: return "PPCISD::SRA";
559 case PPCISD::SHL: return "PPCISD::SHL";
560 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
561 case PPCISD::STD_32: return "PPCISD::STD_32";
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000562 case PPCISD::CALL_SVR4: return "PPCISD::CALL_SVR4";
Hal Finkel5b00cea2012-03-31 14:45:15 +0000563 case PPCISD::CALL_NOP_SVR4: return "PPCISD::CALL_NOP_SVR4";
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000564 case PPCISD::CALL_Darwin: return "PPCISD::CALL_Darwin";
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000565 case PPCISD::NOP: return "PPCISD::NOP";
Evan Cheng53301922008-07-12 02:23:19 +0000566 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000567 case PPCISD::BCTRL_Darwin: return "PPCISD::BCTRL_Darwin";
568 case PPCISD::BCTRL_SVR4: return "PPCISD::BCTRL_SVR4";
Evan Cheng53301922008-07-12 02:23:19 +0000569 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
570 case PPCISD::MFCR: return "PPCISD::MFCR";
571 case PPCISD::VCMP: return "PPCISD::VCMP";
572 case PPCISD::VCMPo: return "PPCISD::VCMPo";
573 case PPCISD::LBRX: return "PPCISD::LBRX";
574 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng53301922008-07-12 02:23:19 +0000575 case PPCISD::LARX: return "PPCISD::LARX";
576 case PPCISD::STCX: return "PPCISD::STCX";
577 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
578 case PPCISD::MFFS: return "PPCISD::MFFS";
579 case PPCISD::MTFSB0: return "PPCISD::MTFSB0";
580 case PPCISD::MTFSB1: return "PPCISD::MTFSB1";
581 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
582 case PPCISD::MTFSF: return "PPCISD::MTFSF";
Evan Cheng53301922008-07-12 02:23:19 +0000583 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel82b38212012-08-28 02:10:27 +0000584 case PPCISD::CR6SET: return "PPCISD::CR6SET";
585 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000586 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
587 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
588 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Bill Schmidtb453e162012-12-14 17:02:38 +0000589 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
590 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtd7802bf2012-12-04 16:18:08 +0000591 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidt57ac1f42012-12-11 20:30:11 +0000592 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
593 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
594 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt349c2782012-12-12 19:29:35 +0000595 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
596 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
597 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
598 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
599 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidtb34c79e2013-02-20 15:50:31 +0000600 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000601 }
602}
603
Duncan Sands28b77e92011-09-06 19:07:46 +0000604EVT PPCTargetLowering::getSetCCResultType(EVT VT) const {
Adhemerval Zanella1c7d69b2012-10-08 18:59:53 +0000605 if (!VT.isVector())
606 return MVT::i32;
607 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000608}
609
Chris Lattner1a635d62006-04-14 06:01:58 +0000610//===----------------------------------------------------------------------===//
611// Node matching predicates, for use by the tblgen matching code.
612//===----------------------------------------------------------------------===//
613
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000614/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman475871a2008-07-27 21:46:04 +0000615static bool isFloatingPointZero(SDValue Op) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000616 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000617 return CFP->getValueAPF().isZero();
Gabor Greifba36cb52008-08-28 21:40:38 +0000618 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000619 // Maybe this has already been legalized into the constant pool?
620 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohman46510a72010-04-15 01:51:59 +0000621 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000622 return CFP->getValueAPF().isZero();
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000623 }
624 return false;
625}
626
Chris Lattnerddb739e2006-04-06 17:23:16 +0000627/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
628/// true if Op is undef or if it matches the specified value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000629static bool isConstantOrUndef(int Op, int Val) {
630 return Op < 0 || Op == Val;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000631}
632
633/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
634/// VPKUHUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000635bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000636 if (!isUnary) {
637 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000638 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000639 return false;
640 } else {
641 for (unsigned i = 0; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000642 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
643 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000644 return false;
645 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000646 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000647}
648
649/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
650/// VPKUWUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000651bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000652 if (!isUnary) {
653 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000654 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
655 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000656 return false;
657 } else {
658 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000659 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
660 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
661 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
662 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000663 return false;
664 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000665 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000666}
667
Chris Lattnercaad1632006-04-06 22:02:42 +0000668/// isVMerge - Common function, used to match vmrg* shuffles.
669///
Nate Begeman9008ca62009-04-27 18:41:29 +0000670static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnercaad1632006-04-06 22:02:42 +0000671 unsigned LHSStart, unsigned RHSStart) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman9008ca62009-04-27 18:41:29 +0000673 "PPC only supports shuffles by bytes!");
Chris Lattner116cc482006-04-06 21:11:54 +0000674 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
675 "Unsupported merge size!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000676
Chris Lattner116cc482006-04-06 21:11:54 +0000677 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
678 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman9008ca62009-04-27 18:41:29 +0000679 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000680 LHSStart+j+i*UnitSize) ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000681 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000682 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000683 return false;
684 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000685 return true;
Chris Lattnercaad1632006-04-06 22:02:42 +0000686}
687
688/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
689/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000690bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman9008ca62009-04-27 18:41:29 +0000691 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000692 if (!isUnary)
693 return isVMerge(N, UnitSize, 8, 24);
694 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000695}
696
697/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
698/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000699bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman9008ca62009-04-27 18:41:29 +0000700 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000701 if (!isUnary)
702 return isVMerge(N, UnitSize, 0, 16);
703 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000704}
705
706
Chris Lattnerd0608e12006-04-06 18:26:28 +0000707/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
708/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000709int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000710 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman9008ca62009-04-27 18:41:29 +0000711 "PPC only supports shuffles by bytes!");
712
713 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000714
Chris Lattnerd0608e12006-04-06 18:26:28 +0000715 // Find the first non-undef value in the shuffle mask.
716 unsigned i;
Nate Begeman9008ca62009-04-27 18:41:29 +0000717 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattnerd0608e12006-04-06 18:26:28 +0000718 /*search*/;
Scott Michelfdc40a02009-02-17 22:15:04 +0000719
Chris Lattnerd0608e12006-04-06 18:26:28 +0000720 if (i == 16) return -1; // all undef.
Scott Michelfdc40a02009-02-17 22:15:04 +0000721
Nate Begeman9008ca62009-04-27 18:41:29 +0000722 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattnerd0608e12006-04-06 18:26:28 +0000723 // numbered from this value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000724 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattnerd0608e12006-04-06 18:26:28 +0000725 if (ShiftAmt < i) return -1;
726 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000727
Chris Lattnerf24380e2006-04-06 22:28:36 +0000728 if (!isUnary) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000729 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000730 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000731 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000732 return -1;
733 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000734 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000735 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000736 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000737 return -1;
738 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000739 return ShiftAmt;
740}
Chris Lattneref819f82006-03-20 06:33:01 +0000741
742/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
743/// specifies a splat of a single element that is suitable for input to
744/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman9008ca62009-04-27 18:41:29 +0000745bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner7ff7e672006-04-04 17:25:31 +0000747 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelfdc40a02009-02-17 22:15:04 +0000748
Chris Lattner88a99ef2006-03-20 06:37:44 +0000749 // This is a splat operation if each element of the permute is the same, and
750 // if the value doesn't reference the second vector.
Nate Begeman9008ca62009-04-27 18:41:29 +0000751 unsigned ElementBase = N->getMaskElt(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000752
Nate Begeman9008ca62009-04-27 18:41:29 +0000753 // FIXME: Handle UNDEF elements too!
754 if (ElementBase >= 16)
Chris Lattner7ff7e672006-04-04 17:25:31 +0000755 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000756
Nate Begeman9008ca62009-04-27 18:41:29 +0000757 // Check that the indices are consecutive, in the case of a multi-byte element
758 // splatted with a v16i8 mask.
759 for (unsigned i = 1; i != EltSize; ++i)
760 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000761 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000762
Chris Lattner7ff7e672006-04-04 17:25:31 +0000763 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000764 if (N->getMaskElt(i) < 0) continue;
Chris Lattner7ff7e672006-04-04 17:25:31 +0000765 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman9008ca62009-04-27 18:41:29 +0000766 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000767 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000768 }
Chris Lattner7ff7e672006-04-04 17:25:31 +0000769 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000770}
771
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000772/// isAllNegativeZeroVector - Returns true if all elements of build_vector
773/// are -0.0.
774bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000775 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
776
777 APInt APVal, APUndef;
778 unsigned BitSize;
779 bool HasAnyUndefs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000780
Dale Johannesen1e608812009-11-13 01:45:18 +0000781 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman9008ca62009-04-27 18:41:29 +0000782 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000783 return CFP->getValueAPF().isNegZero();
Nate Begeman9008ca62009-04-27 18:41:29 +0000784
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000785 return false;
786}
787
Chris Lattneref819f82006-03-20 06:33:01 +0000788/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
789/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000790unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000791 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
792 assert(isSplatShuffleMask(SVOp, EltSize));
793 return SVOp->getMaskElt(0) / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000794}
795
Chris Lattnere87192a2006-04-12 17:37:20 +0000796/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000797/// by using a vspltis[bhw] instruction of the specified element size, return
798/// the constant being splatted. The ByteSize field indicates the number of
799/// bytes of each element [124] -> [bhw].
Dan Gohman475871a2008-07-27 21:46:04 +0000800SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
801 SDValue OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000802
803 // If ByteSize of the splat is bigger than the element size of the
804 // build_vector, then we have a case where we are checking for a splat where
805 // multiple elements of the buildvector are folded together into a single
806 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
807 unsigned EltSize = 16/N->getNumOperands();
808 if (EltSize < ByteSize) {
809 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman475871a2008-07-27 21:46:04 +0000810 SDValue UniquedVals[4];
Chris Lattner79d9a882006-04-08 07:14:26 +0000811 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelfdc40a02009-02-17 22:15:04 +0000812
Chris Lattner79d9a882006-04-08 07:14:26 +0000813 // See if all of the elements in the buildvector agree across.
814 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
815 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
816 // If the element isn't a constant, bail fully out.
Dan Gohman475871a2008-07-27 21:46:04 +0000817 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000818
Scott Michelfdc40a02009-02-17 22:15:04 +0000819
Gabor Greifba36cb52008-08-28 21:40:38 +0000820 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattner79d9a882006-04-08 07:14:26 +0000821 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
822 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000823 return SDValue(); // no match.
Chris Lattner79d9a882006-04-08 07:14:26 +0000824 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000825
Chris Lattner79d9a882006-04-08 07:14:26 +0000826 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
827 // either constant or undef values that are identical for each chunk. See
828 // if these chunks can form into a larger vspltis*.
Scott Michelfdc40a02009-02-17 22:15:04 +0000829
Chris Lattner79d9a882006-04-08 07:14:26 +0000830 // Check to see if all of the leading entries are either 0 or -1. If
831 // neither, then this won't fit into the immediate field.
832 bool LeadingZero = true;
833 bool LeadingOnes = true;
834 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000835 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelfdc40a02009-02-17 22:15:04 +0000836
Chris Lattner79d9a882006-04-08 07:14:26 +0000837 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
838 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
839 }
840 // Finally, check the least significant entry.
841 if (LeadingZero) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000842 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +0000843 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000844 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000845 if (Val < 16)
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattner79d9a882006-04-08 07:14:26 +0000847 }
848 if (LeadingOnes) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000849 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman7810bfe2008-09-26 21:54:37 +0000851 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000852 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson825b72b2009-08-11 20:47:22 +0000853 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattner79d9a882006-04-08 07:14:26 +0000854 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000855
Dan Gohman475871a2008-07-27 21:46:04 +0000856 return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000857 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000858
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000859 // Check to see if this buildvec has a single non-undef value in its elements.
860 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
861 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greifba36cb52008-08-28 21:40:38 +0000862 if (OpVal.getNode() == 0)
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000863 OpVal = N->getOperand(i);
864 else if (OpVal != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000865 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000866 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000867
Gabor Greifba36cb52008-08-28 21:40:38 +0000868 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelfdc40a02009-02-17 22:15:04 +0000869
Eli Friedman1a8229b2009-05-24 02:03:36 +0000870 unsigned ValSizeInBytes = EltSize;
Nate Begeman98e70cc2006-03-28 04:15:58 +0000871 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000872 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000873 Value = CN->getZExtValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000874 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johanneseneaf08942007-08-31 04:03:46 +0000876 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000877 }
878
879 // If the splat value is larger than the element value, then we can never do
880 // this splat. The only case that we could fit the replicated bits into our
881 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman475871a2008-07-27 21:46:04 +0000882 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +0000883
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000884 // If the element value is larger than the splat value, cut it in half and
885 // check to see if the two halves are equal. Continue doing this until we
886 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
887 while (ValSizeInBytes > ByteSize) {
888 ValSizeInBytes >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000889
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000890 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000891 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
892 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman475871a2008-07-27 21:46:04 +0000893 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000894 }
895
896 // Properly sign extend the value.
Richard Smith1144af32012-08-24 23:29:28 +0000897 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelfdc40a02009-02-17 22:15:04 +0000898
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000899 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman475871a2008-07-27 21:46:04 +0000900 if (MaskVal == 0) return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000901
Chris Lattner140a58f2006-04-08 06:46:53 +0000902 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith1144af32012-08-24 23:29:28 +0000903 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000905 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000906}
907
Chris Lattner1a635d62006-04-14 06:01:58 +0000908//===----------------------------------------------------------------------===//
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000909// Addressing Mode Selection
910//===----------------------------------------------------------------------===//
911
912/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
913/// or 64-bit immediate, and if the value can be accurately represented as a
914/// sign extension from a 16-bit value. If so, this returns true and the
915/// immediate.
916static bool isIntS16Immediate(SDNode *N, short &Imm) {
917 if (N->getOpcode() != ISD::Constant)
918 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000919
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000920 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 if (N->getValueType(0) == MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000922 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000923 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000924 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000925}
Dan Gohman475871a2008-07-27 21:46:04 +0000926static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000927 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000928}
929
930
931/// SelectAddressRegReg - Given the specified addressed, check to see if it
932/// can be represented as an indexed [r+r] operation. Returns false if it
933/// can be more efficiently represented with [r+imm].
Dan Gohman475871a2008-07-27 21:46:04 +0000934bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
935 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +0000936 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000937 short imm = 0;
938 if (N.getOpcode() == ISD::ADD) {
939 if (isIntS16Immediate(N.getOperand(1), imm))
940 return false; // r+i
941 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
942 return false; // r+i
Scott Michelfdc40a02009-02-17 22:15:04 +0000943
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000944 Base = N.getOperand(0);
945 Index = N.getOperand(1);
946 return true;
947 } else if (N.getOpcode() == ISD::OR) {
948 if (isIntS16Immediate(N.getOperand(1), imm))
949 return false; // r+i can fold it if we can.
Scott Michelfdc40a02009-02-17 22:15:04 +0000950
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000951 // If this is an or of disjoint bitfields, we can codegen this as an add
952 // (for better address arithmetic) if the LHS and RHS of the OR are provably
953 // disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000954 APInt LHSKnownZero, LHSKnownOne;
955 APInt RHSKnownZero, RHSKnownOne;
956 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000957 LHSKnownZero, LHSKnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +0000958
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000959 if (LHSKnownZero.getBoolValue()) {
960 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000961 RHSKnownZero, RHSKnownOne);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000962 // If all of the bits are known zero on the LHS or RHS, the add won't
963 // carry.
Dan Gohmanec59b952008-02-27 21:12:32 +0000964 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000965 Base = N.getOperand(0);
966 Index = N.getOperand(1);
967 return true;
968 }
969 }
970 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000971
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000972 return false;
973}
974
975/// Returns true if the address N can be represented by a base register plus
976/// a signed 16-bit displacement [r+imm], and if it is not better
977/// represented as reg+reg.
Dan Gohman475871a2008-07-27 21:46:04 +0000978bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman73e09142009-01-15 16:29:45 +0000979 SDValue &Base,
980 SelectionDAG &DAG) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000981 // FIXME dl should come from parent load or store, not from address
982 DebugLoc dl = N.getDebugLoc();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000983 // If this can be more profitably realized as r+r, fail.
984 if (SelectAddressRegReg(N, Disp, Base, DAG))
985 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000986
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000987 if (N.getOpcode() == ISD::ADD) {
988 short imm = 0;
989 if (isIntS16Immediate(N.getOperand(1), imm)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000990 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000991 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
992 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
993 } else {
994 Base = N.getOperand(0);
995 }
996 return true; // [r+i]
997 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
998 // Match LOAD (ADD (X, Lo(G))).
Gabor Greif413ca0d2012-04-20 11:41:38 +0000999 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001000 && "Cannot handle constant offsets yet!");
1001 Disp = N.getOperand(1).getOperand(0); // The global address.
1002 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackyfd42ed62012-06-04 17:36:38 +00001003 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001004 Disp.getOpcode() == ISD::TargetConstantPool ||
1005 Disp.getOpcode() == ISD::TargetJumpTable);
1006 Base = N.getOperand(0);
1007 return true; // [&g+r]
1008 }
1009 } else if (N.getOpcode() == ISD::OR) {
1010 short imm = 0;
1011 if (isIntS16Immediate(N.getOperand(1), imm)) {
1012 // If this is an or of disjoint bitfields, we can codegen this as an add
1013 // (for better address arithmetic) if the LHS and RHS of the OR are
1014 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001015 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001016 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling3e98c302008-03-24 23:16:37 +00001017
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001018 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001019 // If all of the bits are known zero on the LHS or RHS, the add won't
1020 // carry.
1021 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001022 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001023 return true;
1024 }
1025 }
1026 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1027 // Loading from a constant address.
Scott Michelfdc40a02009-02-17 22:15:04 +00001028
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001029 // If this address fits entirely in a 16-bit sext immediate field, codegen
1030 // this as "d, 0"
1031 short Imm;
1032 if (isIntS16Immediate(CN, Imm)) {
1033 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkela548afc2013-03-19 18:51:05 +00001034 Base = DAG.getRegister(PPC::ZERO, CN->getValueType(0));
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001035 return true;
1036 }
Chris Lattnerbc681d62007-02-17 06:44:03 +00001037
1038 // Handle 32-bit sext immediates with LIS + addr mode.
Owen Anderson825b72b2009-08-11 20:47:22 +00001039 if (CN->getValueType(0) == MVT::i32 ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001040 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
1041 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001042
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001043 // Otherwise, break this down into an LIS + disp.
Owen Anderson825b72b2009-08-11 20:47:22 +00001044 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00001045
Owen Anderson825b72b2009-08-11 20:47:22 +00001046 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1047 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman602b0c82009-09-25 18:54:59 +00001048 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001049 return true;
1050 }
1051 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001052
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001053 Disp = DAG.getTargetConstant(0, getPointerTy());
1054 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
1055 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1056 else
1057 Base = N;
1058 return true; // [r+0]
1059}
1060
1061/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1062/// represented as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +00001063bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1064 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +00001065 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001066 // Check to see if we can easily represent this as an [r+r] address. This
1067 // will fail if it thinks that the address is more profitably represented as
1068 // reg+imm, e.g. where imm = 0.
1069 if (SelectAddressRegReg(N, Base, Index, DAG))
1070 return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001071
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001072 // If the operand is an addition, always emit this as [r+r], since this is
1073 // better (for code size, and execution, as the memop does the add for free)
1074 // than emitting an explicit add.
1075 if (N.getOpcode() == ISD::ADD) {
1076 Base = N.getOperand(0);
1077 Index = N.getOperand(1);
1078 return true;
1079 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001080
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001081 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkela548afc2013-03-19 18:51:05 +00001082 Base = DAG.getRegister(PPC::ZERO, N.getValueType());
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001083 Index = N;
1084 return true;
1085}
1086
1087/// SelectAddressRegImmShift - Returns true if the address N can be
1088/// represented by a base register plus a signed 14-bit displacement
1089/// [r+imm*4]. Suitable for use by STD and friends.
Dan Gohman475871a2008-07-27 21:46:04 +00001090bool PPCTargetLowering::SelectAddressRegImmShift(SDValue N, SDValue &Disp,
1091 SDValue &Base,
Dan Gohman73e09142009-01-15 16:29:45 +00001092 SelectionDAG &DAG) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +00001093 // FIXME dl should come from the parent load or store, not the address
1094 DebugLoc dl = N.getDebugLoc();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001095 // If this can be more profitably realized as r+r, fail.
1096 if (SelectAddressRegReg(N, Disp, Base, DAG))
1097 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001098
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001099 if (N.getOpcode() == ISD::ADD) {
1100 short imm = 0;
1101 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
Gabor Greifc77d6782012-04-20 08:58:49 +00001102 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001103 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1104 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1105 } else {
1106 Base = N.getOperand(0);
1107 }
1108 return true; // [r+i]
1109 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1110 // Match LOAD (ADD (X, Lo(G))).
Gabor Greif413ca0d2012-04-20 11:41:38 +00001111 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001112 && "Cannot handle constant offsets yet!");
1113 Disp = N.getOperand(1).getOperand(0); // The global address.
1114 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
1115 Disp.getOpcode() == ISD::TargetConstantPool ||
1116 Disp.getOpcode() == ISD::TargetJumpTable);
1117 Base = N.getOperand(0);
1118 return true; // [&g+r]
1119 }
1120 } else if (N.getOpcode() == ISD::OR) {
1121 short imm = 0;
1122 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
1123 // If this is an or of disjoint bitfields, we can codegen this as an add
1124 // (for better address arithmetic) if the LHS and RHS of the OR are
1125 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001126 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001127 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001128 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001129 // If all of the bits are known zero on the LHS or RHS, the add won't
1130 // carry.
1131 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001132 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001133 return true;
1134 }
1135 }
1136 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001137 // Loading from a constant address. Verify low two bits are clear.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001138 if ((CN->getZExtValue() & 3) == 0) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001139 // If this address fits entirely in a 14-bit sext immediate field, codegen
1140 // this as "d, 0"
1141 short Imm;
1142 if (isIntS16Immediate(CN, Imm)) {
1143 Disp = DAG.getTargetConstant((unsigned short)Imm >> 2, getPointerTy());
Hal Finkela548afc2013-03-19 18:51:05 +00001144 Base = DAG.getRegister(PPC::ZERO, CN->getValueType(0));
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001145 return true;
1146 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001147
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001148 // Fold the low-part of 32-bit absolute addresses into addr mode.
Owen Anderson825b72b2009-08-11 20:47:22 +00001149 if (CN->getValueType(0) == MVT::i32 ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001150 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
1151 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001152
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001153 // Otherwise, break this down into an LIS + disp.
Owen Anderson825b72b2009-08-11 20:47:22 +00001154 Disp = DAG.getTargetConstant((short)Addr >> 2, MVT::i32);
1155 Base = DAG.getTargetConstant((Addr-(signed short)Addr) >> 16, MVT::i32);
1156 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman602b0c82009-09-25 18:54:59 +00001157 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base),0);
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001158 return true;
1159 }
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001160 }
1161 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001162
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001163 Disp = DAG.getTargetConstant(0, getPointerTy());
1164 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
1165 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1166 else
1167 Base = N;
1168 return true; // [r+0]
1169}
1170
1171
1172/// getPreIndexedAddressParts - returns true by value, base pointer and
1173/// offset pointer and addressing mode by reference if the node's address
1174/// can be legally represented as pre-indexed load / store address.
Dan Gohman475871a2008-07-27 21:46:04 +00001175bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1176 SDValue &Offset,
Evan Cheng144d8f02006-11-09 17:55:04 +00001177 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00001178 SelectionDAG &DAG) const {
Hal Finkel77838f92012-06-04 02:21:00 +00001179 if (DisablePPCPreinc) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001180
Dan Gohman475871a2008-07-27 21:46:04 +00001181 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00001182 EVT VT;
Hal Finkel08a215c2013-03-18 23:00:58 +00001183 unsigned Alignment;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001184 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1185 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001186 VT = LD->getMemoryVT();
Hal Finkel08a215c2013-03-18 23:00:58 +00001187 Alignment = LD->getAlignment();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001188 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001189 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001190 VT = ST->getMemoryVT();
Hal Finkel08a215c2013-03-18 23:00:58 +00001191 Alignment = ST->getAlignment();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001192 } else
1193 return false;
1194
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001195 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001196 if (VT.isVector())
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001197 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001198
Hal Finkelac81cc32012-06-19 02:34:32 +00001199 if (SelectAddressRegReg(Ptr, Offset, Base, DAG)) {
Hal Finkel0fcdd8b2012-06-20 15:43:03 +00001200 AM = ISD::PRE_INC;
1201 return true;
Hal Finkelac81cc32012-06-19 02:34:32 +00001202 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001203
Chris Lattner0851b4f2006-11-15 19:55:13 +00001204 // LDU/STU use reg+imm*4, others use reg+imm.
Owen Anderson825b72b2009-08-11 20:47:22 +00001205 if (VT != MVT::i64) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001206 // reg + imm
1207 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG))
1208 return false;
1209 } else {
Hal Finkel08a215c2013-03-18 23:00:58 +00001210 // LDU/STU need an address with at least 4-byte alignment.
1211 if (Alignment < 4)
1212 return false;
1213
Chris Lattner0851b4f2006-11-15 19:55:13 +00001214 // reg + imm * 4.
1215 if (!SelectAddressRegImmShift(Ptr, Offset, Base, DAG))
1216 return false;
1217 }
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001218
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001219 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001220 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1221 // sext i32 to i64 when addr mode is r+i.
Owen Anderson825b72b2009-08-11 20:47:22 +00001222 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001223 LD->getExtensionType() == ISD::SEXTLOAD &&
1224 isa<ConstantSDNode>(Offset))
1225 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001226 }
1227
Chris Lattner4eab7142006-11-10 02:08:47 +00001228 AM = ISD::PRE_INC;
1229 return true;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001230}
1231
1232//===----------------------------------------------------------------------===//
Chris Lattner1a635d62006-04-14 06:01:58 +00001233// LowerOperation implementation
1234//===----------------------------------------------------------------------===//
1235
Chris Lattner1e61e692010-11-15 02:46:57 +00001236/// GetLabelAccessInfo - Return true if we should reference labels using a
1237/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1238static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Chris Lattner6d2ff122010-11-15 03:13:19 +00001239 unsigned &LoOpFlags, const GlobalValue *GV = 0) {
1240 HiOpFlags = PPCII::MO_HA16;
1241 LoOpFlags = PPCII::MO_LO16;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001242
Chris Lattner1e61e692010-11-15 02:46:57 +00001243 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1244 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001245 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattner1e61e692010-11-15 02:46:57 +00001246 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattner6d2ff122010-11-15 03:13:19 +00001247 if (isPIC) {
1248 HiOpFlags |= PPCII::MO_PIC_FLAG;
1249 LoOpFlags |= PPCII::MO_PIC_FLAG;
1250 }
1251
1252 // If this is a reference to a global value that requires a non-lazy-ptr, make
1253 // sure that instruction lowering adds it.
1254 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1255 HiOpFlags |= PPCII::MO_NLP_FLAG;
1256 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001257
Chris Lattner6d2ff122010-11-15 03:13:19 +00001258 if (GV->hasHiddenVisibility()) {
1259 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1260 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1261 }
1262 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001263
Chris Lattner1e61e692010-11-15 02:46:57 +00001264 return isPIC;
1265}
1266
1267static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1268 SelectionDAG &DAG) {
1269 EVT PtrVT = HiPart.getValueType();
1270 SDValue Zero = DAG.getConstant(0, PtrVT);
1271 DebugLoc DL = HiPart.getDebugLoc();
1272
1273 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1274 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001275
Chris Lattner1e61e692010-11-15 02:46:57 +00001276 // With PIC, the first instruction is actually "GR+hi(&G)".
1277 if (isPIC)
1278 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1279 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001280
Chris Lattner1e61e692010-11-15 02:46:57 +00001281 // Generate non-pic code that has direct accesses to the constant pool.
1282 // The address of the global is just (hi(&g)+lo(&g)).
1283 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1284}
1285
Scott Michelfdc40a02009-02-17 22:15:04 +00001286SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001287 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001288 EVT PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001289 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001290 const Constant *C = CP->getConstVal();
Chris Lattner1a635d62006-04-14 06:01:58 +00001291
Roman Divacky9fb8b492012-08-24 16:26:02 +00001292 // 64-bit SVR4 ABI code is always position-independent.
1293 // The actual address of the GlobalValue is stored in the TOC.
1294 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1295 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
1296 return DAG.getNode(PPCISD::TOC_ENTRY, CP->getDebugLoc(), MVT::i64, GA,
1297 DAG.getRegister(PPC::X2, MVT::i64));
1298 }
1299
Chris Lattner1e61e692010-11-15 02:46:57 +00001300 unsigned MOHiFlag, MOLoFlag;
1301 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1302 SDValue CPIHi =
1303 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1304 SDValue CPILo =
1305 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1306 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00001307}
1308
Dan Gohmand858e902010-04-17 15:26:15 +00001309SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001310 EVT PtrVT = Op.getValueType();
Nate Begeman37efe672006-04-22 18:53:45 +00001311 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001312
Roman Divacky9fb8b492012-08-24 16:26:02 +00001313 // 64-bit SVR4 ABI code is always position-independent.
1314 // The actual address of the GlobalValue is stored in the TOC.
1315 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1316 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1317 return DAG.getNode(PPCISD::TOC_ENTRY, JT->getDebugLoc(), MVT::i64, GA,
1318 DAG.getRegister(PPC::X2, MVT::i64));
1319 }
1320
Chris Lattner1e61e692010-11-15 02:46:57 +00001321 unsigned MOHiFlag, MOLoFlag;
1322 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1323 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1324 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1325 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001326}
1327
Dan Gohmand858e902010-04-17 15:26:15 +00001328SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1329 SelectionDAG &DAG) const {
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001330 EVT PtrVT = Op.getValueType();
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001331
Dan Gohman46510a72010-04-15 01:51:59 +00001332 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001333
Chris Lattner1e61e692010-11-15 02:46:57 +00001334 unsigned MOHiFlag, MOLoFlag;
1335 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001336 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1337 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattner1e61e692010-11-15 02:46:57 +00001338 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1339}
1340
Roman Divackyfd42ed62012-06-04 17:36:38 +00001341SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1342 SelectionDAG &DAG) const {
1343
1344 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1345 DebugLoc dl = GA->getDebugLoc();
1346 const GlobalValue *GV = GA->getGlobal();
1347 EVT PtrVT = getPointerTy();
1348 bool is64bit = PPCSubTarget.isPPC64();
1349
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001350 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackyfd42ed62012-06-04 17:36:38 +00001351
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001352 if (Model == TLSModel::LocalExec) {
1353 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1354 PPCII::MO_TPREL16_HA);
1355 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1356 PPCII::MO_TPREL16_LO);
1357 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1358 is64bit ? MVT::i64 : MVT::i32);
1359 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1360 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1361 }
Roman Divackyfd42ed62012-06-04 17:36:38 +00001362
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001363 if (!is64bit)
1364 llvm_unreachable("only local-exec is currently supported for ppc32");
1365
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001366 if (Model == TLSModel::InitialExec) {
Bill Schmidtdfebc4c2012-12-13 18:45:54 +00001367 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1368 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
Bill Schmidtb453e162012-12-14 17:02:38 +00001369 SDValue TPOffsetHi = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1370 PtrVT, GOTReg, TGA);
1371 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
1372 PtrVT, TGA, TPOffsetHi);
Bill Schmidtdfebc4c2012-12-13 18:45:54 +00001373 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGA);
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001374 }
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001375
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001376 if (Model == TLSModel::GeneralDynamic) {
1377 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1378 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1379 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1380 GOTReg, TGA);
1381 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1382 GOTEntryHi, TGA);
1383
1384 // We need a chain node, and don't have one handy. The underlying
1385 // call has no side effects, so using the function entry node
1386 // suffices.
1387 SDValue Chain = DAG.getEntryNode();
1388 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1389 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1390 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1391 PtrVT, ParmReg, TGA);
Bill Schmidt349c2782012-12-12 19:29:35 +00001392 // The return value from GET_TLS_ADDR really is in X3 already, but
1393 // some hacks are needed here to tie everything together. The extra
1394 // copies dissolve during subsequent transforms.
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001395 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1396 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1397 }
1398
Bill Schmidt349c2782012-12-12 19:29:35 +00001399 if (Model == TLSModel::LocalDynamic) {
1400 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1401 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1402 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1403 GOTReg, TGA);
1404 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1405 GOTEntryHi, TGA);
1406
1407 // We need a chain node, and don't have one handy. The underlying
1408 // call has no side effects, so using the function entry node
1409 // suffices.
1410 SDValue Chain = DAG.getEntryNode();
1411 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1412 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1413 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1414 PtrVT, ParmReg, TGA);
1415 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1416 // some hacks are needed here to tie everything together. The extra
1417 // copies dissolve during subsequent transforms.
1418 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1419 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt1e18b862012-12-13 20:57:10 +00001420 Chain, ParmReg, TGA);
Bill Schmidt349c2782012-12-12 19:29:35 +00001421 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1422 }
1423
1424 llvm_unreachable("Unknown TLS model!");
Roman Divackyfd42ed62012-06-04 17:36:38 +00001425}
1426
Chris Lattner1e61e692010-11-15 02:46:57 +00001427SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1428 SelectionDAG &DAG) const {
1429 EVT PtrVT = Op.getValueType();
1430 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
1431 DebugLoc DL = GSDN->getDebugLoc();
1432 const GlobalValue *GV = GSDN->getGlobal();
1433
Chris Lattner1e61e692010-11-15 02:46:57 +00001434 // 64-bit SVR4 ABI code is always position-independent.
1435 // The actual address of the GlobalValue is stored in the TOC.
1436 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1437 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1438 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1439 DAG.getRegister(PPC::X2, MVT::i64));
1440 }
1441
Chris Lattner6d2ff122010-11-15 03:13:19 +00001442 unsigned MOHiFlag, MOLoFlag;
1443 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattner1e61e692010-11-15 02:46:57 +00001444
Chris Lattner6d2ff122010-11-15 03:13:19 +00001445 SDValue GAHi =
1446 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1447 SDValue GALo =
1448 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001449
Chris Lattner6d2ff122010-11-15 03:13:19 +00001450 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001451
Chris Lattner6d2ff122010-11-15 03:13:19 +00001452 // If the global reference is actually to a non-lazy-pointer, we have to do an
1453 // extra load to get the address of the global.
1454 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1455 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001456 false, false, false, 0);
Chris Lattner6d2ff122010-11-15 03:13:19 +00001457 return Ptr;
Chris Lattner1a635d62006-04-14 06:01:58 +00001458}
1459
Dan Gohmand858e902010-04-17 15:26:15 +00001460SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner1a635d62006-04-14 06:01:58 +00001461 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001462 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001463
Chris Lattner1a635d62006-04-14 06:01:58 +00001464 // If we're comparing for equality to zero, expose the fact that this is
1465 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1466 // fold the new nodes.
1467 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1468 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Andersone50ed302009-08-10 22:56:29 +00001469 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001470 SDValue Zext = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001471 if (VT.bitsLT(MVT::i32)) {
1472 VT = MVT::i32;
Dale Johannesenf5d97892009-02-04 01:48:28 +00001473 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001474 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00001475 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001476 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1477 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson825b72b2009-08-11 20:47:22 +00001478 DAG.getConstant(Log2b, MVT::i32));
1479 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner1a635d62006-04-14 06:01:58 +00001480 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001481 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner1a635d62006-04-14 06:01:58 +00001482 // optimized. FIXME: revisit this when we can custom lower all setcc
1483 // optimizations.
1484 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman475871a2008-07-27 21:46:04 +00001485 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001486 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001487
Chris Lattner1a635d62006-04-14 06:01:58 +00001488 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattnerac011bc2006-11-14 05:28:08 +00001489 // by xor'ing the rhs with the lhs, which is faster than setting a
1490 // condition register, reading it back out, and masking the correct bit. The
1491 // normal approach here uses sub to do this instead of xor. Using xor exposes
1492 // the result to other bit-twiddling opportunities.
Owen Andersone50ed302009-08-10 22:56:29 +00001493 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001494 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001495 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001496 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner1a635d62006-04-14 06:01:58 +00001497 Op.getOperand(1));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001498 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner1a635d62006-04-14 06:01:58 +00001499 }
Dan Gohman475871a2008-07-27 21:46:04 +00001500 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001501}
1502
Dan Gohman475871a2008-07-27 21:46:04 +00001503SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001504 const PPCSubtarget &Subtarget) const {
Roman Divackybdb226e2011-06-28 15:30:42 +00001505 SDNode *Node = Op.getNode();
1506 EVT VT = Node->getValueType(0);
1507 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1508 SDValue InChain = Node->getOperand(0);
1509 SDValue VAListPtr = Node->getOperand(1);
1510 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1511 DebugLoc dl = Node->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001512
Roman Divackybdb226e2011-06-28 15:30:42 +00001513 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1514
1515 // gpr_index
1516 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1517 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1518 false, false, 0);
1519 InChain = GprIndex.getValue(1);
1520
1521 if (VT == MVT::i64) {
1522 // Check if GprIndex is even
1523 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1524 DAG.getConstant(1, MVT::i32));
1525 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1526 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1527 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1528 DAG.getConstant(1, MVT::i32));
1529 // Align GprIndex to be even if it isn't
1530 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1531 GprIndex);
1532 }
1533
1534 // fpr index is 1 byte after gpr
1535 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1536 DAG.getConstant(1, MVT::i32));
1537
1538 // fpr
1539 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1540 FprPtr, MachinePointerInfo(SV), MVT::i8,
1541 false, false, 0);
1542 InChain = FprIndex.getValue(1);
1543
1544 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1545 DAG.getConstant(8, MVT::i32));
1546
1547 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1548 DAG.getConstant(4, MVT::i32));
1549
1550 // areas
1551 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001552 MachinePointerInfo(), false, false,
1553 false, 0);
Roman Divackybdb226e2011-06-28 15:30:42 +00001554 InChain = OverflowArea.getValue(1);
1555
1556 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001557 MachinePointerInfo(), false, false,
1558 false, 0);
Roman Divackybdb226e2011-06-28 15:30:42 +00001559 InChain = RegSaveArea.getValue(1);
1560
1561 // select overflow_area if index > 8
1562 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1563 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1564
Roman Divackybdb226e2011-06-28 15:30:42 +00001565 // adjustment constant gpr_index * 4/8
1566 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1567 VT.isInteger() ? GprIndex : FprIndex,
1568 DAG.getConstant(VT.isInteger() ? 4 : 8,
1569 MVT::i32));
1570
1571 // OurReg = RegSaveArea + RegConstant
1572 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1573 RegConstant);
1574
1575 // Floating types are 32 bytes into RegSaveArea
1576 if (VT.isFloatingPoint())
1577 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1578 DAG.getConstant(32, MVT::i32));
1579
1580 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1581 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1582 VT.isInteger() ? GprIndex : FprIndex,
1583 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1584 MVT::i32));
1585
1586 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1587 VT.isInteger() ? VAListPtr : FprPtr,
1588 MachinePointerInfo(SV),
1589 MVT::i8, false, false, 0);
1590
1591 // determine if we should load from reg_save_area or overflow_area
1592 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1593
1594 // increase overflow_area by 4/8 if gpr/fpr > 8
1595 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1596 DAG.getConstant(VT.isInteger() ? 4 : 8,
1597 MVT::i32));
1598
1599 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1600 OverflowAreaPlusN);
1601
1602 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1603 OverflowAreaPtr,
1604 MachinePointerInfo(),
1605 MVT::i32, false, false, 0);
1606
NAKAMURA Takumi25f6b5a2012-08-30 15:52:23 +00001607 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001608 false, false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001609}
1610
Duncan Sands4a544a72011-09-06 13:37:06 +00001611SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1612 SelectionDAG &DAG) const {
1613 return Op.getOperand(0);
1614}
1615
1616SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1617 SelectionDAG &DAG) const {
Bill Wendling77959322008-09-17 00:30:57 +00001618 SDValue Chain = Op.getOperand(0);
1619 SDValue Trmp = Op.getOperand(1); // trampoline
1620 SDValue FPtr = Op.getOperand(2); // nested function
1621 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001622 DebugLoc dl = Op.getDebugLoc();
Bill Wendling77959322008-09-17 00:30:57 +00001623
Owen Andersone50ed302009-08-10 22:56:29 +00001624 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00001625 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001626 Type *IntPtrTy =
Micah Villmow3574eca2012-10-08 16:38:25 +00001627 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruthece6c6b2012-11-01 08:07:29 +00001628 *DAG.getContext());
Bill Wendling77959322008-09-17 00:30:57 +00001629
Scott Michelfdc40a02009-02-17 22:15:04 +00001630 TargetLowering::ArgListTy Args;
Bill Wendling77959322008-09-17 00:30:57 +00001631 TargetLowering::ArgListEntry Entry;
1632
1633 Entry.Ty = IntPtrTy;
1634 Entry.Node = Trmp; Args.push_back(Entry);
1635
1636 // TrampSize == (isPPC64 ? 48 : 40);
1637 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson825b72b2009-08-11 20:47:22 +00001638 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling77959322008-09-17 00:30:57 +00001639 Args.push_back(Entry);
1640
1641 Entry.Node = FPtr; Args.push_back(Entry);
1642 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelfdc40a02009-02-17 22:15:04 +00001643
Bill Wendling77959322008-09-17 00:30:57 +00001644 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001645 TargetLowering::CallLoweringInfo CLI(Chain,
1646 Type::getVoidTy(*DAG.getContext()),
1647 false, false, false, false, 0,
1648 CallingConv::C,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001649 /*isTailCall=*/false,
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001650 /*doesNotRet=*/false,
1651 /*isReturnValueUsed=*/true,
Bill Wendling77959322008-09-17 00:30:57 +00001652 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Bill Wendling46ada192010-03-02 01:55:18 +00001653 Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001654 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bill Wendling77959322008-09-17 00:30:57 +00001655
Duncan Sands4a544a72011-09-06 13:37:06 +00001656 return CallResult.second;
Bill Wendling77959322008-09-17 00:30:57 +00001657}
1658
Dan Gohman475871a2008-07-27 21:46:04 +00001659SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001660 const PPCSubtarget &Subtarget) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001661 MachineFunction &MF = DAG.getMachineFunction();
1662 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1663
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001664 DebugLoc dl = Op.getDebugLoc();
Nicolas Geoffray01119992007-04-03 13:59:52 +00001665
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001666 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray01119992007-04-03 13:59:52 +00001667 // vastart just stores the address of the VarArgsFrameIndex slot into the
1668 // memory location argument.
Owen Andersone50ed302009-08-10 22:56:29 +00001669 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001670 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001671 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner6229d0a2010-09-21 18:41:36 +00001672 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1673 MachinePointerInfo(SV),
David Greene534502d12010-02-15 16:56:53 +00001674 false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001675 }
1676
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001677 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray01119992007-04-03 13:59:52 +00001678 // We suppose the given va_list is already allocated.
1679 //
1680 // typedef struct {
1681 // char gpr; /* index into the array of 8 GPRs
1682 // * stored in the register save area
1683 // * gpr=0 corresponds to r3,
1684 // * gpr=1 to r4, etc.
1685 // */
1686 // char fpr; /* index into the array of 8 FPRs
1687 // * stored in the register save area
1688 // * fpr=0 corresponds to f1,
1689 // * fpr=1 to f2, etc.
1690 // */
1691 // char *overflow_arg_area;
1692 // /* location on stack that holds
1693 // * the next overflow argument
1694 // */
1695 // char *reg_save_area;
1696 // /* where r3:r10 and f1:f8 (if saved)
1697 // * are stored
1698 // */
1699 // } va_list[1];
1700
1701
Dan Gohman1e93df62010-04-17 14:41:14 +00001702 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1703 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00001704
Nicolas Geoffray01119992007-04-03 13:59:52 +00001705
Owen Andersone50ed302009-08-10 22:56:29 +00001706 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfdc40a02009-02-17 22:15:04 +00001707
Dan Gohman1e93df62010-04-17 14:41:14 +00001708 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1709 PtrVT);
1710 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1711 PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001712
Duncan Sands83ec4b62008-06-06 12:08:01 +00001713 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman475871a2008-07-27 21:46:04 +00001714 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001715
Duncan Sands83ec4b62008-06-06 12:08:01 +00001716 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001717 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001718
1719 uint64_t FPROffset = 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001720 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001721
Dan Gohman69de1932008-02-06 22:27:42 +00001722 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001723
Nicolas Geoffray01119992007-04-03 13:59:52 +00001724 // Store first byte : number of int regs
Tilmann Schellerffd02002009-07-03 06:45:56 +00001725 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001726 Op.getOperand(1),
1727 MachinePointerInfo(SV),
1728 MVT::i8, false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001729 uint64_t nextOffset = FPROffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001730 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray01119992007-04-03 13:59:52 +00001731 ConstFPROffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001732
Nicolas Geoffray01119992007-04-03 13:59:52 +00001733 // Store second byte : number of float regs
Dan Gohman475871a2008-07-27 21:46:04 +00001734 SDValue secondStore =
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001735 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1736 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene534502d12010-02-15 16:56:53 +00001737 false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001738 nextOffset += StackOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001739 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001740
Nicolas Geoffray01119992007-04-03 13:59:52 +00001741 // Store second word : arguments given on stack
Dan Gohman475871a2008-07-27 21:46:04 +00001742 SDValue thirdStore =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001743 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1744 MachinePointerInfo(SV, nextOffset),
David Greene534502d12010-02-15 16:56:53 +00001745 false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001746 nextOffset += FrameOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001747 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001748
1749 // Store third word : arguments given in registers
Chris Lattner6229d0a2010-09-21 18:41:36 +00001750 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1751 MachinePointerInfo(SV, nextOffset),
David Greene534502d12010-02-15 16:56:53 +00001752 false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001753
Chris Lattner1a635d62006-04-14 06:01:58 +00001754}
1755
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00001756#include "PPCGenCallingConv.inc"
1757
Bill Schmidt212af6a2013-02-06 17:33:58 +00001758static bool CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1759 CCValAssign::LocInfo &LocInfo,
1760 ISD::ArgFlagsTy &ArgFlags,
1761 CCState &State) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001762 return true;
1763}
1764
Bill Schmidt212af6a2013-02-06 17:33:58 +00001765static bool CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
1766 MVT &LocVT,
1767 CCValAssign::LocInfo &LocInfo,
1768 ISD::ArgFlagsTy &ArgFlags,
1769 CCState &State) {
Craig Topperc5eaae42012-03-11 07:57:25 +00001770 static const uint16_t ArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001771 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1772 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1773 };
1774 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001775
Tilmann Schellerffd02002009-07-03 06:45:56 +00001776 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1777
1778 // Skip one register if the first unallocated register has an even register
1779 // number and there are still argument registers available which have not been
1780 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1781 // need to skip a register if RegNum is odd.
1782 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1783 State.AllocateReg(ArgRegs[RegNum]);
1784 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001785
Tilmann Schellerffd02002009-07-03 06:45:56 +00001786 // Always return false here, as this function only makes sure that the first
1787 // unallocated register has an odd register number and does not actually
1788 // allocate a register for the current argument.
1789 return false;
1790}
1791
Bill Schmidt212af6a2013-02-06 17:33:58 +00001792static bool CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
1793 MVT &LocVT,
1794 CCValAssign::LocInfo &LocInfo,
1795 ISD::ArgFlagsTy &ArgFlags,
1796 CCState &State) {
Craig Topperc5eaae42012-03-11 07:57:25 +00001797 static const uint16_t ArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001798 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1799 PPC::F8
1800 };
1801
1802 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001803
Tilmann Schellerffd02002009-07-03 06:45:56 +00001804 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1805
1806 // If there is only one Floating-point register left we need to put both f64
1807 // values of a split ppc_fp128 value on the stack.
1808 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1809 State.AllocateReg(ArgRegs[RegNum]);
1810 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001811
Tilmann Schellerffd02002009-07-03 06:45:56 +00001812 // Always return false here, as this function only makes sure that the two f64
1813 // values a ppc_fp128 value is split into are both passed in registers or both
1814 // passed on the stack and does not actually allocate a register for the
1815 // current argument.
1816 return false;
1817}
1818
Chris Lattner9f0bc652007-02-25 05:34:32 +00001819/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001820/// on Darwin.
Craig Topperb78ca422012-03-11 07:16:55 +00001821static const uint16_t *GetFPR() {
1822 static const uint16_t FPR[] = {
Chris Lattner9f0bc652007-02-25 05:34:32 +00001823 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001824 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner9f0bc652007-02-25 05:34:32 +00001825 };
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001826
Chris Lattner9f0bc652007-02-25 05:34:32 +00001827 return FPR;
1828}
1829
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001830/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1831/// the stack.
Owen Andersone50ed302009-08-10 22:56:29 +00001832static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001833 unsigned PtrByteSize) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001834 unsigned ArgSize = ArgVT.getSizeInBits()/8;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001835 if (Flags.isByVal())
1836 ArgSize = Flags.getByValSize();
1837 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1838
1839 return ArgSize;
1840}
1841
Dan Gohman475871a2008-07-27 21:46:04 +00001842SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001843PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001844 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001845 const SmallVectorImpl<ISD::InputArg>
1846 &Ins,
1847 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001848 SmallVectorImpl<SDValue> &InVals)
1849 const {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00001850 if (PPCSubTarget.isSVR4ABI()) {
1851 if (PPCSubTarget.isPPC64())
1852 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
1853 dl, DAG, InVals);
1854 else
1855 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
1856 dl, DAG, InVals);
Bill Schmidt419f3762012-09-19 15:42:13 +00001857 } else {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00001858 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
1859 dl, DAG, InVals);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001860 }
1861}
1862
1863SDValue
Bill Schmidt419f3762012-09-19 15:42:13 +00001864PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohman98ca4f22009-08-05 01:29:28 +00001865 SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001866 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001867 const SmallVectorImpl<ISD::InputArg>
1868 &Ins,
1869 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001870 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001871
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001872 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001873 // +-----------------------------------+
1874 // +--> | Back chain |
1875 // | +-----------------------------------+
1876 // | | Floating-point register save area |
1877 // | +-----------------------------------+
1878 // | | General register save area |
1879 // | +-----------------------------------+
1880 // | | CR save word |
1881 // | +-----------------------------------+
1882 // | | VRSAVE save word |
1883 // | +-----------------------------------+
1884 // | | Alignment padding |
1885 // | +-----------------------------------+
1886 // | | Vector register save area |
1887 // | +-----------------------------------+
1888 // | | Local variable space |
1889 // | +-----------------------------------+
1890 // | | Parameter list area |
1891 // | +-----------------------------------+
1892 // | | LR save word |
1893 // | +-----------------------------------+
1894 // SP--> +--- | Back chain |
1895 // +-----------------------------------+
1896 //
1897 // Specifications:
1898 // System V Application Binary Interface PowerPC Processor Supplement
1899 // AltiVec Technology Programming Interface Manual
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001900
Tilmann Schellerffd02002009-07-03 06:45:56 +00001901 MachineFunction &MF = DAG.getMachineFunction();
1902 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +00001903 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerffd02002009-07-03 06:45:56 +00001904
Owen Andersone50ed302009-08-10 22:56:29 +00001905 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerffd02002009-07-03 06:45:56 +00001906 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001907 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
1908 (CallConv == CallingConv::Fast));
Tilmann Schellerffd02002009-07-03 06:45:56 +00001909 unsigned PtrByteSize = 4;
1910
1911 // Assign locations to all of the incoming arguments.
1912 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001913 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00001914 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00001915
1916 // Reserve space for the linkage area on the stack.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001917 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001918
Bill Schmidt212af6a2013-02-06 17:33:58 +00001919 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001920
Tilmann Schellerffd02002009-07-03 06:45:56 +00001921 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1922 CCValAssign &VA = ArgLocs[i];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001923
Tilmann Schellerffd02002009-07-03 06:45:56 +00001924 // Arguments stored in registers.
1925 if (VA.isRegLoc()) {
Craig Topper44d23822012-02-22 05:59:10 +00001926 const TargetRegisterClass *RC;
Owen Andersone50ed302009-08-10 22:56:29 +00001927 EVT ValVT = VA.getValVT();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001928
Owen Anderson825b72b2009-08-11 20:47:22 +00001929 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001930 default:
Dan Gohman98ca4f22009-08-05 01:29:28 +00001931 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Owen Anderson825b72b2009-08-11 20:47:22 +00001932 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +00001933 RC = &PPC::GPRCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001934 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001935 case MVT::f32:
Craig Topperc9099502012-04-20 06:31:50 +00001936 RC = &PPC::F4RCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001937 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001938 case MVT::f64:
Craig Topperc9099502012-04-20 06:31:50 +00001939 RC = &PPC::F8RCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001940 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001941 case MVT::v16i8:
1942 case MVT::v8i16:
1943 case MVT::v4i32:
1944 case MVT::v4f32:
Craig Topperc9099502012-04-20 06:31:50 +00001945 RC = &PPC::VRRCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001946 break;
1947 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001948
Tilmann Schellerffd02002009-07-03 06:45:56 +00001949 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00001950 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001951 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, ValVT);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001952
Dan Gohman98ca4f22009-08-05 01:29:28 +00001953 InVals.push_back(ArgValue);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001954 } else {
1955 // Argument stored in memory.
1956 assert(VA.isMemLoc());
1957
1958 unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8;
1959 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Chenged2ae132010-07-03 00:40:23 +00001960 isImmutable);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001961
1962 // Create load nodes to retrieve arguments from the stack.
1963 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001964 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1965 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001966 false, false, false, 0));
Tilmann Schellerffd02002009-07-03 06:45:56 +00001967 }
1968 }
1969
1970 // Assign locations to all of the incoming aggregate by value arguments.
1971 // Aggregates passed by value are stored in the local variable space of the
1972 // caller's stack frame, right above the parameter list area.
1973 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001974 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00001975 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00001976
1977 // Reserve stack space for the allocations in CCInfo.
1978 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
1979
Bill Schmidt212af6a2013-02-06 17:33:58 +00001980 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001981
1982 // Area that is at least reserved in the caller of this function.
1983 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001984
Tilmann Schellerffd02002009-07-03 06:45:56 +00001985 // Set the size that is at least reserved in caller of this function. Tail
1986 // call optimized function's reserved stack space needs to be aligned so that
1987 // taking the difference between two stack areas will result in an aligned
1988 // stack.
1989 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1990
1991 MinReservedArea =
1992 std::max(MinReservedArea,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001993 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001994
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001995 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerffd02002009-07-03 06:45:56 +00001996 getStackAlignment();
1997 unsigned AlignMask = TargetAlign-1;
1998 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001999
Tilmann Schellerffd02002009-07-03 06:45:56 +00002000 FI->setMinReservedArea(MinReservedArea);
2001
2002 SmallVector<SDValue, 8> MemOps;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002003
Tilmann Schellerffd02002009-07-03 06:45:56 +00002004 // If the function takes variable number of arguments, make a frame index for
2005 // the start of the first vararg value... for expansion of llvm.va_start.
2006 if (isVarArg) {
Craig Topperc5eaae42012-03-11 07:57:25 +00002007 static const uint16_t GPArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002008 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2009 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2010 };
2011 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2012
Craig Topperc5eaae42012-03-11 07:57:25 +00002013 static const uint16_t FPArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002014 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2015 PPC::F8
2016 };
2017 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2018
Dan Gohman1e93df62010-04-17 14:41:14 +00002019 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2020 NumGPArgRegs));
2021 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2022 NumFPArgRegs));
Tilmann Schellerffd02002009-07-03 06:45:56 +00002023
2024 // Make room for NumGPArgRegs and NumFPArgRegs.
2025 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson825b72b2009-08-11 20:47:22 +00002026 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002027
Dan Gohman1e93df62010-04-17 14:41:14 +00002028 FuncInfo->setVarArgsStackOffset(
2029 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00002030 CCInfo.getNextStackOffset(), true));
Tilmann Schellerffd02002009-07-03 06:45:56 +00002031
Dan Gohman1e93df62010-04-17 14:41:14 +00002032 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2033 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002034
Jakob Stoklund Olesen4f9af2e2010-10-11 20:43:09 +00002035 // The fixed integer arguments of a variadic function are stored to the
2036 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2037 // the result of va_next.
2038 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2039 // Get an existing live-in vreg, or add a new one.
2040 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2041 if (!VReg)
Devang Patel68e6bee2011-02-21 23:21:26 +00002042 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002043
Dan Gohman98ca4f22009-08-05 01:29:28 +00002044 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002045 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2046 MachinePointerInfo(), false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002047 MemOps.push_back(Store);
2048 // Increment the address by four for the next argument to store
2049 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2050 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2051 }
2052
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002053 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2054 // is set.
Tilmann Schellerffd02002009-07-03 06:45:56 +00002055 // The double arguments are stored to the VarArgsFrameIndex
2056 // on the stack.
Jakob Stoklund Olesen4f9af2e2010-10-11 20:43:09 +00002057 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2058 // Get an existing live-in vreg, or add a new one.
2059 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2060 if (!VReg)
Devang Patel68e6bee2011-02-21 23:21:26 +00002061 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002062
Owen Anderson825b72b2009-08-11 20:47:22 +00002063 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002064 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2065 MachinePointerInfo(), false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002066 MemOps.push_back(Store);
2067 // Increment the address by eight for the next argument to store
Owen Anderson825b72b2009-08-11 20:47:22 +00002068 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerffd02002009-07-03 06:45:56 +00002069 PtrVT);
2070 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2071 }
2072 }
2073
2074 if (!MemOps.empty())
Dan Gohman98ca4f22009-08-05 01:29:28 +00002075 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002076 MVT::Other, &MemOps[0], MemOps.size());
Tilmann Schellerffd02002009-07-03 06:45:56 +00002077
Dan Gohman98ca4f22009-08-05 01:29:28 +00002078 return Chain;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002079}
2080
Bill Schmidt726c2372012-10-23 15:51:16 +00002081// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2082// value to MVT::i64 and then truncate to the correct register size.
2083SDValue
2084PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2085 SelectionDAG &DAG, SDValue ArgVal,
2086 DebugLoc dl) const {
2087 if (Flags.isSExt())
2088 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2089 DAG.getValueType(ObjectVT));
2090 else if (Flags.isZExt())
2091 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2092 DAG.getValueType(ObjectVT));
2093
2094 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
2095}
2096
2097// Set the size that is at least reserved in caller of this function. Tail
2098// call optimized functions' reserved stack space needs to be aligned so that
2099// taking the difference between two stack areas will result in an aligned
2100// stack.
2101void
2102PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2103 unsigned nAltivecParamsAtEnd,
2104 unsigned MinReservedArea,
2105 bool isPPC64) const {
2106 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2107 // Add the Altivec parameters at the end, if needed.
2108 if (nAltivecParamsAtEnd) {
2109 MinReservedArea = ((MinReservedArea+15)/16)*16;
2110 MinReservedArea += 16*nAltivecParamsAtEnd;
2111 }
2112 MinReservedArea =
2113 std::max(MinReservedArea,
2114 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2115 unsigned TargetAlign
2116 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2117 getStackAlignment();
2118 unsigned AlignMask = TargetAlign-1;
2119 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2120 FI->setMinReservedArea(MinReservedArea);
2121}
2122
Tilmann Schellerffd02002009-07-03 06:45:56 +00002123SDValue
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002124PPCTargetLowering::LowerFormalArguments_64SVR4(
2125 SDValue Chain,
2126 CallingConv::ID CallConv, bool isVarArg,
2127 const SmallVectorImpl<ISD::InputArg>
2128 &Ins,
2129 DebugLoc dl, SelectionDAG &DAG,
2130 SmallVectorImpl<SDValue> &InVals) const {
2131 // TODO: add description of PPC stack frame format, or at least some docs.
2132 //
2133 MachineFunction &MF = DAG.getMachineFunction();
2134 MachineFrameInfo *MFI = MF.getFrameInfo();
2135 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2136
2137 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2138 // Potential tail calls could cause overwriting of argument stack slots.
2139 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2140 (CallConv == CallingConv::Fast));
2141 unsigned PtrByteSize = 8;
2142
2143 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2144 // Area that is at least reserved in caller of this function.
2145 unsigned MinReservedArea = ArgOffset;
2146
2147 static const uint16_t GPR[] = {
2148 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2149 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2150 };
2151
2152 static const uint16_t *FPR = GetFPR();
2153
2154 static const uint16_t VR[] = {
2155 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2156 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2157 };
2158
2159 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2160 const unsigned Num_FPR_Regs = 13;
2161 const unsigned Num_VR_Regs = array_lengthof(VR);
2162
2163 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2164
2165 // Add DAG nodes to load the arguments or copy them out of registers. On
2166 // entry to a function on PPC, the arguments start after the linkage area,
2167 // although the first ones are often in registers.
2168
2169 SmallVector<SDValue, 8> MemOps;
2170 unsigned nAltivecParamsAtEnd = 0;
2171 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt49deebb2013-02-20 17:31:41 +00002172 unsigned CurArgIdx = 0;
2173 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002174 SDValue ArgVal;
2175 bool needsLoad = false;
2176 EVT ObjectVT = Ins[ArgNo].VT;
2177 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
2178 unsigned ArgSize = ObjSize;
2179 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt49deebb2013-02-20 17:31:41 +00002180 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2181 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002182
2183 unsigned CurArgOffset = ArgOffset;
2184
2185 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2186 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2187 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
2188 if (isVarArg) {
2189 MinReservedArea = ((MinReservedArea+15)/16)*16;
2190 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2191 Flags,
2192 PtrByteSize);
2193 } else
2194 nAltivecParamsAtEnd++;
2195 } else
2196 // Calculate min reserved area.
2197 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2198 Flags,
2199 PtrByteSize);
2200
2201 // FIXME the codegen can be much improved in some cases.
2202 // We do not have to keep everything in memory.
2203 if (Flags.isByVal()) {
2204 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2205 ObjSize = Flags.getByValSize();
2206 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt42d43352012-10-31 01:15:05 +00002207 // Empty aggregate parameters do not take up registers. Examples:
2208 // struct { } a;
2209 // union { } b;
2210 // int c[0];
2211 // etc. However, we have to provide a place-holder in InVals, so
2212 // pretend we have an 8-byte item at the current address for that
2213 // purpose.
2214 if (!ObjSize) {
2215 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2216 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2217 InVals.push_back(FIN);
2218 continue;
2219 }
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002220 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt7a6cb152012-10-16 13:30:53 +00002221 if (ObjSize < PtrByteSize)
2222 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002223 // The value of the object is its address.
2224 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2225 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2226 InVals.push_back(FIN);
Bill Schmidt37900c52012-10-25 13:38:09 +00002227
2228 if (ObjSize < 8) {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002229 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt37900c52012-10-25 13:38:09 +00002230 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002231 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt37900c52012-10-25 13:38:09 +00002232 SDValue Store;
2233
2234 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2235 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2236 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2237 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
2238 MachinePointerInfo(FuncArg, CurArgOffset),
2239 ObjType, false, false, 0);
2240 } else {
2241 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2242 // store the whole register as-is to the parameter save area
2243 // slot. The address of the parameter was already calculated
2244 // above (InVals.push_back(FIN)) to be the right-justified
2245 // offset within the slot. For this store, we need a new
2246 // frame index that points at the beginning of the slot.
2247 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2248 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2249 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2250 MachinePointerInfo(FuncArg, ArgOffset),
2251 false, false, 0);
2252 }
2253
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002254 MemOps.push_back(Store);
2255 ++GPR_idx;
2256 }
Bill Schmidt37900c52012-10-25 13:38:09 +00002257 // Whether we copied from a register or not, advance the offset
2258 // into the parameter save area by a full doubleword.
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002259 ArgOffset += PtrByteSize;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002260 continue;
2261 }
Bill Schmidt37900c52012-10-25 13:38:09 +00002262
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002263 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2264 // Store whatever pieces of the object are in registers
2265 // to memory. ArgOffset will be the address of the beginning
2266 // of the object.
2267 if (GPR_idx != Num_GPR_Regs) {
2268 unsigned VReg;
2269 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2270 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2271 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2272 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt37900c52012-10-25 13:38:09 +00002273 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002274 MachinePointerInfo(FuncArg, ArgOffset),
2275 false, false, 0);
2276 MemOps.push_back(Store);
2277 ++GPR_idx;
2278 ArgOffset += PtrByteSize;
2279 } else {
Bill Schmidt7a6cb152012-10-16 13:30:53 +00002280 ArgOffset += ArgSize - j;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002281 break;
2282 }
2283 }
2284 continue;
2285 }
2286
2287 switch (ObjectVT.getSimpleVT().SimpleTy) {
2288 default: llvm_unreachable("Unhandled argument type!");
2289 case MVT::i32:
2290 case MVT::i64:
2291 if (GPR_idx != Num_GPR_Regs) {
2292 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2293 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2294
Bill Schmidt726c2372012-10-23 15:51:16 +00002295 if (ObjectVT == MVT::i32)
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002296 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2297 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt726c2372012-10-23 15:51:16 +00002298 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002299
2300 ++GPR_idx;
2301 } else {
2302 needsLoad = true;
2303 ArgSize = PtrByteSize;
2304 }
2305 ArgOffset += 8;
2306 break;
2307
2308 case MVT::f32:
2309 case MVT::f64:
2310 // Every 8 bytes of argument space consumes one of the GPRs available for
2311 // argument passing.
2312 if (GPR_idx != Num_GPR_Regs) {
2313 ++GPR_idx;
2314 }
2315 if (FPR_idx != Num_FPR_Regs) {
2316 unsigned VReg;
2317
2318 if (ObjectVT == MVT::f32)
2319 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2320 else
2321 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
2322
2323 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2324 ++FPR_idx;
2325 } else {
2326 needsLoad = true;
Bill Schmidta867f372012-10-11 15:38:20 +00002327 ArgSize = PtrByteSize;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002328 }
2329
2330 ArgOffset += 8;
2331 break;
2332 case MVT::v4f32:
2333 case MVT::v4i32:
2334 case MVT::v8i16:
2335 case MVT::v16i8:
2336 // Note that vector arguments in registers don't reserve stack space,
2337 // except in varargs functions.
2338 if (VR_idx != Num_VR_Regs) {
2339 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
2340 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2341 if (isVarArg) {
2342 while ((ArgOffset % 16) != 0) {
2343 ArgOffset += PtrByteSize;
2344 if (GPR_idx != Num_GPR_Regs)
2345 GPR_idx++;
2346 }
2347 ArgOffset += 16;
2348 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2349 }
2350 ++VR_idx;
2351 } else {
2352 // Vectors are aligned.
2353 ArgOffset = ((ArgOffset+15)/16)*16;
2354 CurArgOffset = ArgOffset;
2355 ArgOffset += 16;
2356 needsLoad = true;
2357 }
2358 break;
2359 }
2360
2361 // We need to load the argument to a virtual register if we determined
2362 // above that we ran out of physical registers of the appropriate type.
2363 if (needsLoad) {
2364 int FI = MFI->CreateFixedObject(ObjSize,
2365 CurArgOffset + (ArgSize - ObjSize),
2366 isImmutable);
2367 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2368 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2369 false, false, false, 0);
2370 }
2371
2372 InVals.push_back(ArgVal);
2373 }
2374
2375 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt726c2372012-10-23 15:51:16 +00002376 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002377 // taking the difference between two stack areas will result in an aligned
2378 // stack.
Bill Schmidt726c2372012-10-23 15:51:16 +00002379 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002380
2381 // If the function takes variable number of arguments, make a frame index for
2382 // the start of the first vararg value... for expansion of llvm.va_start.
2383 if (isVarArg) {
2384 int Depth = ArgOffset;
2385
2386 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt726c2372012-10-23 15:51:16 +00002387 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002388 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2389
2390 // If this function is vararg, store any remaining integer argument regs
2391 // to their spots on the stack so that they may be loaded by deferencing the
2392 // result of va_next.
2393 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2394 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2395 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2396 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2397 MachinePointerInfo(), false, false, 0);
2398 MemOps.push_back(Store);
2399 // Increment the address by four for the next argument to store
Bill Schmidt726c2372012-10-23 15:51:16 +00002400 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002401 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2402 }
2403 }
2404
2405 if (!MemOps.empty())
2406 Chain = DAG.getNode(ISD::TokenFactor, dl,
2407 MVT::Other, &MemOps[0], MemOps.size());
2408
2409 return Chain;
2410}
2411
2412SDValue
2413PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002414 SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002415 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002416 const SmallVectorImpl<ISD::InputArg>
2417 &Ins,
2418 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002419 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002420 // TODO: add description of PPC stack frame format, or at least some docs.
2421 //
2422 MachineFunction &MF = DAG.getMachineFunction();
2423 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +00002424 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00002425
Owen Andersone50ed302009-08-10 22:56:29 +00002426 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00002427 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002428 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002429 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2430 (CallConv == CallingConv::Fast));
Jim Laskeye9bd7b22006-11-28 14:53:52 +00002431 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey2f616bf2006-11-16 22:43:37 +00002432
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002433 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002434 // Area that is at least reserved in caller of this function.
2435 unsigned MinReservedArea = ArgOffset;
2436
Craig Topperb78ca422012-03-11 07:16:55 +00002437 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002438 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2439 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2440 };
Craig Topperb78ca422012-03-11 07:16:55 +00002441 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerc91a4752006-06-26 22:48:35 +00002442 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2443 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2444 };
Scott Michelfdc40a02009-02-17 22:15:04 +00002445
Craig Topperb78ca422012-03-11 07:16:55 +00002446 static const uint16_t *FPR = GetFPR();
Scott Michelfdc40a02009-02-17 22:15:04 +00002447
Craig Topperb78ca422012-03-11 07:16:55 +00002448 static const uint16_t VR[] = {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002449 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2450 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2451 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00002452
Owen Anderson718cb662007-09-07 04:06:50 +00002453 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002454 const unsigned Num_FPR_Regs = 13;
Owen Anderson718cb662007-09-07 04:06:50 +00002455 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey2f616bf2006-11-16 22:43:37 +00002456
2457 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002458
Craig Topperb78ca422012-03-11 07:16:55 +00002459 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelfdc40a02009-02-17 22:15:04 +00002460
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002461 // In 32-bit non-varargs functions, the stack space for vectors is after the
2462 // stack space for non-vectors. We do not use this space unless we have
2463 // too many vectors to fit in registers, something that only occurs in
Scott Michelfdc40a02009-02-17 22:15:04 +00002464 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002465 // that out...for the pathological case, compute VecArgOffset as the
2466 // start of the vector parameter area. Computing VecArgOffset is the
2467 // entire point of the following loop.
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002468 unsigned VecArgOffset = ArgOffset;
2469 if (!isVarArg && !isPPC64) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002470 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002471 ++ArgNo) {
Owen Andersone50ed302009-08-10 22:56:29 +00002472 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002473 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002474
Duncan Sands276dcbd2008-03-21 09:14:45 +00002475 if (Flags.isByVal()) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002476 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer263109d2012-01-20 14:42:32 +00002477 unsigned ObjSize = Flags.getByValSize();
Scott Michelfdc40a02009-02-17 22:15:04 +00002478 unsigned ArgSize =
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002479 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2480 VecArgOffset += ArgSize;
2481 continue;
2482 }
2483
Owen Anderson825b72b2009-08-11 20:47:22 +00002484 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002485 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002486 case MVT::i32:
2487 case MVT::f32:
Bill Schmidt419f3762012-09-19 15:42:13 +00002488 VecArgOffset += 4;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002489 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002490 case MVT::i64: // PPC64
2491 case MVT::f64:
Bill Schmidt419f3762012-09-19 15:42:13 +00002492 // FIXME: We are guaranteed to be !isPPC64 at this point.
2493 // Does MVT::i64 apply?
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002494 VecArgOffset += 8;
2495 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002496 case MVT::v4f32:
2497 case MVT::v4i32:
2498 case MVT::v8i16:
2499 case MVT::v16i8:
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002500 // Nothing to do, we're only looking at Nonvector args here.
2501 break;
2502 }
2503 }
2504 }
2505 // We've found where the vector parameter area in memory is. Skip the
2506 // first 12 parameters; these don't use that memory.
2507 VecArgOffset = ((VecArgOffset+15)/16)*16;
2508 VecArgOffset += 12*16;
2509
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002510 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey2f616bf2006-11-16 22:43:37 +00002511 // entry to a function on PPC, the arguments start after the linkage area,
2512 // although the first ones are often in registers.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002513
Dan Gohman475871a2008-07-27 21:46:04 +00002514 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002515 unsigned nAltivecParamsAtEnd = 0;
Bill Schmidt49deebb2013-02-20 17:31:41 +00002516 // FIXME: FuncArg and Ins[ArgNo] must reference the same argument.
2517 // When passing anonymous aggregates, this is currently not true.
2518 // See LowerFormalArguments_64SVR4 for a fix.
Roman Divacky5236ab32012-09-24 20:47:19 +00002519 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
2520 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo, ++FuncArg) {
Dan Gohman475871a2008-07-27 21:46:04 +00002521 SDValue ArgVal;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002522 bool needsLoad = false;
Owen Andersone50ed302009-08-10 22:56:29 +00002523 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002524 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey619965d2006-11-29 13:37:09 +00002525 unsigned ArgSize = ObjSize;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002526 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002527
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002528 unsigned CurArgOffset = ArgOffset;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002529
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002530 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson825b72b2009-08-11 20:47:22 +00002531 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2532 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002533 if (isVarArg || isPPC64) {
2534 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002535 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohman095cc292008-09-13 01:54:27 +00002536 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002537 PtrByteSize);
2538 } else nAltivecParamsAtEnd++;
2539 } else
2540 // Calculate min reserved area.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002541 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohman095cc292008-09-13 01:54:27 +00002542 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002543 PtrByteSize);
2544
Dale Johannesen8419dd62008-03-07 20:27:40 +00002545 // FIXME the codegen can be much improved in some cases.
2546 // We do not have to keep everything in memory.
Duncan Sands276dcbd2008-03-21 09:14:45 +00002547 if (Flags.isByVal()) {
Dale Johannesen8419dd62008-03-07 20:27:40 +00002548 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sands276dcbd2008-03-21 09:14:45 +00002549 ObjSize = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00002550 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002551 // Objects of size 1 and 2 are right justified, everything else is
2552 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen7f96f392008-03-08 01:41:42 +00002553 if (ObjSize==1 || ObjSize==2) {
2554 CurArgOffset = CurArgOffset + (4 - ObjSize);
2555 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002556 // The value of the object is its address.
Evan Chenged2ae132010-07-03 00:40:23 +00002557 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman475871a2008-07-27 21:46:04 +00002558 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002559 InVals.push_back(FIN);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002560 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen7f96f392008-03-08 01:41:42 +00002561 if (GPR_idx != Num_GPR_Regs) {
Roman Divacky951cd022011-06-17 15:21:10 +00002562 unsigned VReg;
2563 if (isPPC64)
2564 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2565 else
2566 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002567 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt726c2372012-10-23 15:51:16 +00002568 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelfdc40a02009-02-17 22:15:04 +00002569 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Roman Divacky5236ab32012-09-24 20:47:19 +00002570 MachinePointerInfo(FuncArg,
2571 CurArgOffset),
Bill Schmidt419f3762012-09-19 15:42:13 +00002572 ObjType, false, false, 0);
Dale Johannesen7f96f392008-03-08 01:41:42 +00002573 MemOps.push_back(Store);
2574 ++GPR_idx;
Dale Johannesen7f96f392008-03-08 01:41:42 +00002575 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002576
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002577 ArgOffset += PtrByteSize;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002578
Dale Johannesen7f96f392008-03-08 01:41:42 +00002579 continue;
2580 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002581 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2582 // Store whatever pieces of the object are in registers
Bill Schmidt419f3762012-09-19 15:42:13 +00002583 // to memory. ArgOffset will be the address of the beginning
2584 // of the object.
Dale Johannesen8419dd62008-03-07 20:27:40 +00002585 if (GPR_idx != Num_GPR_Regs) {
Roman Divacky951cd022011-06-17 15:21:10 +00002586 unsigned VReg;
2587 if (isPPC64)
2588 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2589 else
2590 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Chenged2ae132010-07-03 00:40:23 +00002591 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman475871a2008-07-27 21:46:04 +00002592 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002593 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002594 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Roman Divacky5236ab32012-09-24 20:47:19 +00002595 MachinePointerInfo(FuncArg, ArgOffset),
David Greene534502d12010-02-15 16:56:53 +00002596 false, false, 0);
Dale Johannesen8419dd62008-03-07 20:27:40 +00002597 MemOps.push_back(Store);
2598 ++GPR_idx;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002599 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002600 } else {
2601 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2602 break;
2603 }
2604 }
2605 continue;
2606 }
2607
Owen Anderson825b72b2009-08-11 20:47:22 +00002608 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002609 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002610 case MVT::i32:
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002611 if (!isPPC64) {
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002612 if (GPR_idx != Num_GPR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002613 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002614 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002615 ++GPR_idx;
2616 } else {
2617 needsLoad = true;
2618 ArgSize = PtrByteSize;
2619 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002620 // All int arguments reserve stack space in the Darwin ABI.
2621 ArgOffset += PtrByteSize;
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002622 break;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002623 }
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002624 // FALLTHROUGH
Owen Anderson825b72b2009-08-11 20:47:22 +00002625 case MVT::i64: // PPC64
Chris Lattnerc91a4752006-06-26 22:48:35 +00002626 if (GPR_idx != Num_GPR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002627 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002628 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002629
Bill Schmidt726c2372012-10-23 15:51:16 +00002630 if (ObjectVT == MVT::i32)
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002631 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson825b72b2009-08-11 20:47:22 +00002632 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt726c2372012-10-23 15:51:16 +00002633 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002634
Chris Lattnerc91a4752006-06-26 22:48:35 +00002635 ++GPR_idx;
2636 } else {
2637 needsLoad = true;
Evan Cheng982a0592008-07-24 08:17:07 +00002638 ArgSize = PtrByteSize;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002639 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002640 // All int arguments reserve stack space in the Darwin ABI.
2641 ArgOffset += 8;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002642 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002643
Owen Anderson825b72b2009-08-11 20:47:22 +00002644 case MVT::f32:
2645 case MVT::f64:
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002646 // Every 4 bytes of argument space consumes one of the GPRs available for
2647 // argument passing.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002648 if (GPR_idx != Num_GPR_Regs) {
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002649 ++GPR_idx;
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002650 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002651 ++GPR_idx;
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002652 }
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002653 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002654 unsigned VReg;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002655
Owen Anderson825b72b2009-08-11 20:47:22 +00002656 if (ObjectVT == MVT::f32)
Devang Patel68e6bee2011-02-21 23:21:26 +00002657 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002658 else
Devang Patel68e6bee2011-02-21 23:21:26 +00002659 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002660
Dan Gohman98ca4f22009-08-05 01:29:28 +00002661 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002662 ++FPR_idx;
2663 } else {
2664 needsLoad = true;
2665 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002666
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002667 // All FP arguments reserve stack space in the Darwin ABI.
2668 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002669 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002670 case MVT::v4f32:
2671 case MVT::v4i32:
2672 case MVT::v8i16:
2673 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00002674 // Note that vector arguments in registers don't reserve stack space,
2675 // except in varargs functions.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002676 if (VR_idx != Num_VR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002677 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002678 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesen75092de2008-03-12 00:22:17 +00002679 if (isVarArg) {
2680 while ((ArgOffset % 16) != 0) {
2681 ArgOffset += PtrByteSize;
2682 if (GPR_idx != Num_GPR_Regs)
2683 GPR_idx++;
2684 }
2685 ArgOffset += 16;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002686 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesen75092de2008-03-12 00:22:17 +00002687 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002688 ++VR_idx;
2689 } else {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002690 if (!isVarArg && !isPPC64) {
2691 // Vectors go after all the nonvectors.
2692 CurArgOffset = VecArgOffset;
2693 VecArgOffset += 16;
2694 } else {
2695 // Vectors are aligned.
2696 ArgOffset = ((ArgOffset+15)/16)*16;
2697 CurArgOffset = ArgOffset;
2698 ArgOffset += 16;
Dale Johannesen404d9902008-03-12 00:49:20 +00002699 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002700 needsLoad = true;
2701 }
2702 break;
2703 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002704
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002705 // We need to load the argument to a virtual register if we determined above
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002706 // that we ran out of physical registers of the appropriate type.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002707 if (needsLoad) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002708 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002709 CurArgOffset + (ArgSize - ObjSize),
Evan Chenged2ae132010-07-03 00:40:23 +00002710 isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00002711 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002712 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002713 false, false, false, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002714 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002715
Dan Gohman98ca4f22009-08-05 01:29:28 +00002716 InVals.push_back(ArgVal);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002717 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002718
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002719 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt726c2372012-10-23 15:51:16 +00002720 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002721 // taking the difference between two stack areas will result in an aligned
2722 // stack.
Bill Schmidt726c2372012-10-23 15:51:16 +00002723 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002724
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002725 // If the function takes variable number of arguments, make a frame index for
2726 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002727 if (isVarArg) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002728 int Depth = ArgOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00002729
Dan Gohman1e93df62010-04-17 14:41:14 +00002730 FuncInfo->setVarArgsFrameIndex(
2731 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00002732 Depth, true));
Dan Gohman1e93df62010-04-17 14:41:14 +00002733 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002734
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002735 // If this function is vararg, store any remaining integer argument regs
2736 // to their spots on the stack so that they may be loaded by deferencing the
2737 // result of va_next.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002738 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002739 unsigned VReg;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002740
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002741 if (isPPC64)
Devang Patel68e6bee2011-02-21 23:21:26 +00002742 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002743 else
Devang Patel68e6bee2011-02-21 23:21:26 +00002744 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002745
Dan Gohman98ca4f22009-08-05 01:29:28 +00002746 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002747 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2748 MachinePointerInfo(), false, false, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002749 MemOps.push_back(Store);
2750 // Increment the address by four for the next argument to store
Dan Gohman475871a2008-07-27 21:46:04 +00002751 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002752 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002753 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002754 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002755
Dale Johannesen8419dd62008-03-07 20:27:40 +00002756 if (!MemOps.empty())
Dan Gohman98ca4f22009-08-05 01:29:28 +00002757 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002758 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesen8419dd62008-03-07 20:27:40 +00002759
Dan Gohman98ca4f22009-08-05 01:29:28 +00002760 return Chain;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002761}
2762
Bill Schmidt419f3762012-09-19 15:42:13 +00002763/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
2764/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002765static unsigned
2766CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2767 bool isPPC64,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002768 bool isVarArg,
2769 unsigned CC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002770 const SmallVectorImpl<ISD::OutputArg>
2771 &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002772 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002773 unsigned &nAltivecParamsAtEnd) {
2774 // Count how many bytes are to be pushed on the stack, including the linkage
2775 // area, and parameter passing area. We start with 24/48 bytes, which is
2776 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002777 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002778 unsigned NumOps = Outs.size();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002779 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2780
2781 // Add up all the space actually used.
2782 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2783 // they all go in registers, but we must reserve stack space for them for
2784 // possible use by the caller. In varargs or 64-bit calls, parameters are
2785 // assigned stack space in order, with padding so Altivec parameters are
2786 // 16-byte aligned.
2787 nAltivecParamsAtEnd = 0;
2788 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002789 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanc9403652010-07-07 15:54:55 +00002790 EVT ArgVT = Outs[i].VT;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002791 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson825b72b2009-08-11 20:47:22 +00002792 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2793 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002794 if (!isVarArg && !isPPC64) {
2795 // Non-varargs Altivec parameters go after all the non-Altivec
2796 // parameters; handle those later so we know how much padding we need.
2797 nAltivecParamsAtEnd++;
2798 continue;
2799 }
2800 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2801 NumBytes = ((NumBytes+15)/16)*16;
2802 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002803 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002804 }
2805
2806 // Allow for Altivec parameters at the end, if needed.
2807 if (nAltivecParamsAtEnd) {
2808 NumBytes = ((NumBytes+15)/16)*16;
2809 NumBytes += 16*nAltivecParamsAtEnd;
2810 }
2811
2812 // The prolog code of the callee may store up to 8 GPR argument registers to
2813 // the stack, allowing va_start to index over them in memory if its varargs.
2814 // Because we cannot tell if this is needed on the caller side, we have to
2815 // conservatively assume that it is needed. As such, make sure we have at
2816 // least enough stack space for the caller to store the 8 GPRs.
2817 NumBytes = std::max(NumBytes,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002818 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002819
2820 // Tail call needs the stack to be aligned.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002821 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
2822 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
2823 getFrameLowering()->getStackAlignment();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002824 unsigned AlignMask = TargetAlign-1;
2825 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2826 }
2827
2828 return NumBytes;
2829}
2830
2831/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002832/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesenb60d5192009-11-24 01:09:07 +00002833static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002834 unsigned ParamSize) {
2835
Dale Johannesenb60d5192009-11-24 01:09:07 +00002836 if (!isTailCall) return 0;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002837
2838 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
2839 unsigned CallerMinReservedArea = FI->getMinReservedArea();
2840 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
2841 // Remember only if the new adjustement is bigger.
2842 if (SPDiff < FI->getTailCallSPDelta())
2843 FI->setTailCallSPDelta(SPDiff);
2844
2845 return SPDiff;
2846}
2847
Dan Gohman98ca4f22009-08-05 01:29:28 +00002848/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2849/// for tail call optimization. Targets which want to do tail call
2850/// optimization should implement this function.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002851bool
Dan Gohman98ca4f22009-08-05 01:29:28 +00002852PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002853 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002854 bool isVarArg,
2855 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002856 SelectionDAG& DAG) const {
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002857 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng6c2e8a92010-01-29 23:05:56 +00002858 return false;
2859
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002860 // Variable argument functions are not supported.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002861 if (isVarArg)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002862 return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002863
Dan Gohman98ca4f22009-08-05 01:29:28 +00002864 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002865 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002866 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
2867 // Functions containing by val parameters are not supported.
2868 for (unsigned i = 0; i != Ins.size(); i++) {
2869 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2870 if (Flags.isByVal()) return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002871 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002872
2873 // Non PIC/GOT tail calls are supported.
2874 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
2875 return true;
2876
2877 // At the moment we can only do local tail calls (in same module, hidden
2878 // or protected) if we are generating PIC.
2879 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2880 return G->getGlobal()->hasHiddenVisibility()
2881 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002882 }
2883
2884 return false;
2885}
2886
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002887/// isCallCompatibleAddress - Return the immediate to use if the specified
2888/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00002889static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002890 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2891 if (!C) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002892
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002893 int Addr = C->getZExtValue();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002894 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith1144af32012-08-24 23:29:28 +00002895 SignExtend32<26>(Addr) != Addr)
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002896 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002897
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002898 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greifba36cb52008-08-28 21:40:38 +00002899 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002900}
2901
Dan Gohman844731a2008-05-13 00:00:25 +00002902namespace {
2903
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002904struct TailCallArgumentInfo {
Dan Gohman475871a2008-07-27 21:46:04 +00002905 SDValue Arg;
2906 SDValue FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002907 int FrameIdx;
2908
2909 TailCallArgumentInfo() : FrameIdx(0) {}
2910};
2911
Dan Gohman844731a2008-05-13 00:00:25 +00002912}
2913
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002914/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
2915static void
2916StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Chengff89dcb2009-10-18 18:16:27 +00002917 SDValue Chain,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002918 const SmallVector<TailCallArgumentInfo, 8> &TailCallArgs,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002919 SmallVector<SDValue, 8> &MemOpChains,
2920 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002921 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002922 SDValue Arg = TailCallArgs[i].Arg;
2923 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002924 int FI = TailCallArgs[i].FrameIdx;
2925 // Store relative to framepointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002926 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002927 MachinePointerInfo::getFixedStack(FI),
2928 false, false, 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002929 }
2930}
2931
2932/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
2933/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman475871a2008-07-27 21:46:04 +00002934static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002935 MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002936 SDValue Chain,
2937 SDValue OldRetAddr,
2938 SDValue OldFP,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002939 int SPDiff,
2940 bool isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002941 bool isDarwinABI,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002942 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002943 if (SPDiff) {
2944 // Calculate the new stack slot for the return address.
2945 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002946 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002947 isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002948 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002949 NewRetAddrLoc, true);
Owen Anderson825b72b2009-08-11 20:47:22 +00002950 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002951 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002952 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002953 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene534502d12010-02-15 16:56:53 +00002954 false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002955
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002956 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
2957 // slot as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002958 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002959 int NewFPLoc =
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002960 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene3f2bf852009-11-12 20:49:22 +00002961 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Chenged2ae132010-07-03 00:40:23 +00002962 true);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002963 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
2964 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002965 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene534502d12010-02-15 16:56:53 +00002966 false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002967 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002968 }
2969 return Chain;
2970}
2971
2972/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
2973/// the position of the argument.
2974static void
2975CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman475871a2008-07-27 21:46:04 +00002976 SDValue Arg, int SPDiff, unsigned ArgOffset,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002977 SmallVector<TailCallArgumentInfo, 8>& TailCallArguments) {
2978 int Offset = ArgOffset + SPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002979 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002980 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson825b72b2009-08-11 20:47:22 +00002981 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002982 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002983 TailCallArgumentInfo Info;
2984 Info.Arg = Arg;
2985 Info.FrameIdxOp = FIN;
2986 Info.FrameIdx = FI;
2987 TailCallArguments.push_back(Info);
2988}
2989
2990/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
2991/// stack slot. Returns the chain as result and the loaded frame pointers in
2992/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman475871a2008-07-27 21:46:04 +00002993SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002994 int SPDiff,
2995 SDValue Chain,
2996 SDValue &LROpOut,
2997 SDValue &FPOpOut,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002998 bool isDarwinABI,
Dan Gohmand858e902010-04-17 15:26:15 +00002999 DebugLoc dl) const {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003000 if (SPDiff) {
3001 // Load the LR and FP stack slot for later adjusting.
Owen Anderson825b72b2009-08-11 20:47:22 +00003002 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003003 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003004 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003005 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003006 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003007
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003008 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3009 // slot as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003010 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00003011 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003012 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003013 false, false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003014 Chain = SDValue(FPOpOut.getNode(), 1);
3015 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003016 }
3017 return Chain;
3018}
3019
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003020/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelfdc40a02009-02-17 22:15:04 +00003021/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003022/// specified by the specific parameter attribute. The copy will be passed as
3023/// a byval function parameter.
3024/// Sometimes what we are copying is the end of a larger object, the part that
3025/// does not fit in registers.
Scott Michelfdc40a02009-02-17 22:15:04 +00003026static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003027CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00003028 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00003029 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003030 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen8ad9b432009-02-04 01:17:06 +00003031 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Chris Lattnere72f2022010-09-21 05:40:29 +00003032 false, false, MachinePointerInfo(0),
3033 MachinePointerInfo(0));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003034}
Chris Lattner9f0bc652007-02-25 05:34:32 +00003035
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003036/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3037/// tail calls.
3038static void
Dan Gohman475871a2008-07-27 21:46:04 +00003039LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3040 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003041 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Dan Gohman475871a2008-07-27 21:46:04 +00003042 bool isVector, SmallVector<SDValue, 8> &MemOpChains,
Chris Lattner6229d0a2010-09-21 18:41:36 +00003043 SmallVector<TailCallArgumentInfo, 8> &TailCallArguments,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003044 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00003045 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003046 if (!isTailCall) {
3047 if (isVector) {
Dan Gohman475871a2008-07-27 21:46:04 +00003048 SDValue StackPtr;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003049 if (isPPC64)
Owen Anderson825b72b2009-08-11 20:47:22 +00003050 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003051 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003052 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003053 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003054 DAG.getConstant(ArgOffset, PtrVT));
3055 }
Chris Lattner6229d0a2010-09-21 18:41:36 +00003056 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3057 MachinePointerInfo(), false, false, 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003058 // Calculate and remember argument location.
3059 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3060 TailCallArguments);
3061}
3062
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003063static
3064void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
3065 DebugLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
3066 SDValue LROp, SDValue FPOp, bool isDarwinABI,
3067 SmallVector<TailCallArgumentInfo, 8> &TailCallArguments) {
3068 MachineFunction &MF = DAG.getMachineFunction();
3069
3070 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3071 // might overwrite each other in case of tail call optimization.
3072 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00003073 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003074 InFlag = SDValue();
3075 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3076 MemOpChains2, dl);
3077 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00003078 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003079 &MemOpChains2[0], MemOpChains2.size());
3080
3081 // Store the return address to the appropriate stack slot.
3082 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3083 isPPC64, isDarwinABI, dl);
3084
3085 // Emit callseq_end just before tailcall node.
3086 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3087 DAG.getIntPtrConstant(0, true), InFlag);
3088 InFlag = Chain.getValue(1);
3089}
3090
3091static
3092unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
3093 SDValue &Chain, DebugLoc dl, int SPDiff, bool isTailCall,
3094 SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass,
Owen Andersone50ed302009-08-10 22:56:29 +00003095 SmallVector<SDValue, 8> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerb9082582010-11-14 23:42:06 +00003096 const PPCSubtarget &PPCSubTarget) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003097
Chris Lattnerb9082582010-11-14 23:42:06 +00003098 bool isPPC64 = PPCSubTarget.isPPC64();
3099 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3100
Owen Andersone50ed302009-08-10 22:56:29 +00003101 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00003102 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003103 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003104
3105 unsigned CallOpc = isSVR4ABI ? PPCISD::CALL_SVR4 : PPCISD::CALL_Darwin;
3106
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003107 bool needIndirectCall = true;
3108 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003109 // If this is an absolute destination address, use the munged value.
3110 Callee = SDValue(Dest, 0);
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003111 needIndirectCall = false;
3112 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003113
Chris Lattnerb9082582010-11-14 23:42:06 +00003114 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3115 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3116 // Use indirect calls for ALL functions calls in JIT mode, since the
3117 // far-call stubs may be outside relocation limits for a BL instruction.
3118 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3119 unsigned OpFlags = 0;
3120 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyd5601cc2011-07-24 08:22:56 +00003121 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00003122 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerb9082582010-11-14 23:42:06 +00003123 (G->getGlobal()->isDeclaration() ||
3124 G->getGlobal()->isWeakForLinker())) {
3125 // PC-relative references to external symbols should go through $stub,
3126 // unless we're building with the leopard linker or later, which
3127 // automatically synthesizes these stubs.
3128 OpFlags = PPCII::MO_DARWIN_STUB;
3129 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003130
Chris Lattnerb9082582010-11-14 23:42:06 +00003131 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3132 // every direct call is) turn it into a TargetGlobalAddress /
3133 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003134 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerb9082582010-11-14 23:42:06 +00003135 Callee.getValueType(),
3136 0, OpFlags);
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003137 needIndirectCall = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003138 }
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003139 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003140
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003141 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerb9082582010-11-14 23:42:06 +00003142 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003143
Chris Lattnerb9082582010-11-14 23:42:06 +00003144 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyd5601cc2011-07-24 08:22:56 +00003145 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00003146 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerb9082582010-11-14 23:42:06 +00003147 // PC-relative references to external symbols should go through $stub,
3148 // unless we're building with the leopard linker or later, which
3149 // automatically synthesizes these stubs.
3150 OpFlags = PPCII::MO_DARWIN_STUB;
3151 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003152
Chris Lattnerb9082582010-11-14 23:42:06 +00003153 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3154 OpFlags);
3155 needIndirectCall = false;
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003156 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003157
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003158 if (needIndirectCall) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003159 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3160 // to do the call, we can't use PPCISD::CALL.
3161 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003162
3163 if (isSVR4ABI && isPPC64) {
3164 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3165 // entry point, but to the function descriptor (the function entry point
3166 // address is part of the function descriptor though).
3167 // The function descriptor is a three doubleword structure with the
3168 // following fields: function entry point, TOC base address and
3169 // environment pointer.
3170 // Thus for a call through a function pointer, the following actions need
3171 // to be performed:
3172 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt726c2372012-10-23 15:51:16 +00003173 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003174 // 2. Load the address of the function entry point from the function
3175 // descriptor.
3176 // 3. Load the TOC of the callee from the function descriptor into r2.
3177 // 4. Load the environment pointer from the function descriptor into
3178 // r11.
3179 // 5. Branch to the function entry point address.
3180 // 6. On return of the callee, the TOC of the caller needs to be
3181 // restored (this is done in FinishCall()).
3182 //
3183 // All those operations are flagged together to ensure that no other
3184 // operations can be scheduled in between. E.g. without flagging the
3185 // operations together, a TOC access in the caller could be scheduled
3186 // between the load of the callee TOC and the branch to the callee, which
3187 // results in the TOC access going through the TOC of the callee instead
3188 // of going through the TOC of the caller, which leads to incorrect code.
3189
3190 // Load the address of the function entry point from the function
3191 // descriptor.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003192 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003193 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, MTCTROps,
3194 InFlag.getNode() ? 3 : 2);
3195 Chain = LoadFuncPtr.getValue(1);
3196 InFlag = LoadFuncPtr.getValue(2);
3197
3198 // Load environment pointer into r11.
3199 // Offset of the environment pointer within the function descriptor.
3200 SDValue PtrOff = DAG.getIntPtrConstant(16);
3201
3202 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3203 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3204 InFlag);
3205 Chain = LoadEnvPtr.getValue(1);
3206 InFlag = LoadEnvPtr.getValue(2);
3207
3208 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3209 InFlag);
3210 Chain = EnvVal.getValue(0);
3211 InFlag = EnvVal.getValue(1);
3212
3213 // Load TOC of the callee into r2. We are using a target-specific load
3214 // with r2 hard coded, because the result of a target-independent load
3215 // would never go directly into r2, since r2 is a reserved register (which
3216 // prevents the register allocator from allocating it), resulting in an
3217 // additional register being allocated and an unnecessary move instruction
3218 // being generated.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003219 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003220 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3221 Callee, InFlag);
3222 Chain = LoadTOCPtr.getValue(0);
3223 InFlag = LoadTOCPtr.getValue(1);
3224
3225 MTCTROps[0] = Chain;
3226 MTCTROps[1] = LoadFuncPtr;
3227 MTCTROps[2] = InFlag;
3228 }
3229
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003230 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
3231 2 + (InFlag.getNode() != 0));
3232 InFlag = Chain.getValue(1);
3233
3234 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00003235 NodeTys.push_back(MVT::Other);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003236 NodeTys.push_back(MVT::Glue);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003237 Ops.push_back(Chain);
3238 CallOpc = isSVR4ABI ? PPCISD::BCTRL_SVR4 : PPCISD::BCTRL_Darwin;
3239 Callee.setNode(0);
3240 // Add CTR register as callee so a bctr can be emitted later.
3241 if (isTailCall)
Roman Divacky0c9b5592011-06-03 15:47:49 +00003242 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003243 }
3244
3245 // If this is a direct call, pass the chain and the callee.
3246 if (Callee.getNode()) {
3247 Ops.push_back(Chain);
3248 Ops.push_back(Callee);
3249 }
3250 // If this is a tail call add stack pointer delta.
3251 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00003252 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003253
3254 // Add argument registers to the end of the list so that they are known live
3255 // into the call.
3256 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3257 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3258 RegsToPass[i].second.getValueType()));
3259
3260 return CallOpc;
3261}
3262
Roman Divackyeb8b7dc2012-09-18 16:47:58 +00003263static
3264bool isLocalCall(const SDValue &Callee)
3265{
3266 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky6fc3ea22012-09-18 18:27:49 +00003267 return !G->getGlobal()->isDeclaration() &&
3268 !G->getGlobal()->isWeakForLinker();
Roman Divackyeb8b7dc2012-09-18 16:47:58 +00003269 return false;
3270}
3271
Dan Gohman98ca4f22009-08-05 01:29:28 +00003272SDValue
3273PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003274 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003275 const SmallVectorImpl<ISD::InputArg> &Ins,
3276 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00003277 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003278
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003279 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003280 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00003281 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00003282 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003283
3284 // Copy all of the result registers out of their specified physreg.
3285 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3286 CCValAssign &VA = RVLocs[i];
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003287 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand86aef0a2012-11-05 19:39:45 +00003288
3289 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3290 VA.getLocReg(), VA.getLocVT(), InFlag);
3291 Chain = Val.getValue(1);
3292 InFlag = Val.getValue(2);
3293
3294 switch (VA.getLocInfo()) {
3295 default: llvm_unreachable("Unknown loc info!");
3296 case CCValAssign::Full: break;
3297 case CCValAssign::AExt:
3298 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3299 break;
3300 case CCValAssign::ZExt:
3301 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3302 DAG.getValueType(VA.getValVT()));
3303 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3304 break;
3305 case CCValAssign::SExt:
3306 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3307 DAG.getValueType(VA.getValVT()));
3308 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3309 break;
3310 }
3311
3312 InVals.push_back(Val);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003313 }
3314
Dan Gohman98ca4f22009-08-05 01:29:28 +00003315 return Chain;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003316}
3317
Dan Gohman98ca4f22009-08-05 01:29:28 +00003318SDValue
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003319PPCTargetLowering::FinishCall(CallingConv::ID CallConv, DebugLoc dl,
3320 bool isTailCall, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003321 SelectionDAG &DAG,
3322 SmallVector<std::pair<unsigned, SDValue>, 8>
3323 &RegsToPass,
3324 SDValue InFlag, SDValue Chain,
3325 SDValue &Callee,
3326 int SPDiff, unsigned NumBytes,
3327 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohmand858e902010-04-17 15:26:15 +00003328 SmallVectorImpl<SDValue> &InVals) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003329 std::vector<EVT> NodeTys;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003330 SmallVector<SDValue, 8> Ops;
3331 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3332 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerb9082582010-11-14 23:42:06 +00003333 PPCSubTarget);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003334
Hal Finkel82b38212012-08-28 02:10:27 +00003335 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3336 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3337 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3338
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003339 // When performing tail call optimization the callee pops its arguments off
3340 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky700ed802013-02-21 20:05:00 +00003341 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003342 int BytesCalleePops =
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003343 (CallConv == CallingConv::Fast &&
3344 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003345
Roman Divackye46137f2012-03-06 16:41:49 +00003346 // Add a register mask operand representing the call-preserved registers.
3347 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3348 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3349 assert(Mask && "Missing call preserved mask for calling convention");
3350 Ops.push_back(DAG.getRegisterMask(Mask));
3351
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003352 if (InFlag.getNode())
3353 Ops.push_back(InFlag);
3354
3355 // Emit tail call.
3356 if (isTailCall) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003357 assert(((Callee.getOpcode() == ISD::Register &&
3358 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3359 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3360 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3361 isa<ConstantSDNode>(Callee)) &&
3362 "Expecting an global address, external symbol, absolute value or register");
3363
Owen Anderson825b72b2009-08-11 20:47:22 +00003364 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003365 }
3366
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003367 // Add a NOP immediately after the branch instruction when using the 64-bit
3368 // SVR4 ABI. At link time, if caller and callee are in a different module and
3369 // thus have a different TOC, the call will be replaced with a call to a stub
3370 // function which saves the current TOC, loads the TOC of the callee and
3371 // branches to the callee. The NOP will be replaced with a load instruction
3372 // which restores the TOC of the caller from the TOC save slot of the current
3373 // stack frame. If caller and callee belong to the same module (and have the
3374 // same TOC), the NOP will remain unchanged.
Hal Finkel5b00cea2012-03-31 14:45:15 +00003375
3376 bool needsTOCRestore = false;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003377 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003378 if (CallOpc == PPCISD::BCTRL_SVR4) {
3379 // This is a call through a function pointer.
3380 // Restore the caller TOC from the save area into R2.
3381 // See PrepareCall() for more information about calls through function
3382 // pointers in the 64-bit SVR4 ABI.
3383 // We are using a target-specific load with r2 hard coded, because the
3384 // result of a target-independent load would never go directly into r2,
3385 // since r2 is a reserved register (which prevents the register allocator
3386 // from allocating it), resulting in an additional register being
3387 // allocated and an unnecessary move instruction being generated.
Hal Finkel5b00cea2012-03-31 14:45:15 +00003388 needsTOCRestore = true;
Roman Divackyeb8b7dc2012-09-18 16:47:58 +00003389 } else if ((CallOpc == PPCISD::CALL_SVR4) && !isLocalCall(Callee)) {
3390 // Otherwise insert NOP for non-local calls.
Hal Finkel5b00cea2012-03-31 14:45:15 +00003391 CallOpc = PPCISD::CALL_NOP_SVR4;
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003392 }
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003393 }
3394
Hal Finkel5b00cea2012-03-31 14:45:15 +00003395 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
3396 InFlag = Chain.getValue(1);
3397
3398 if (needsTOCRestore) {
3399 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3400 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3401 InFlag = Chain.getValue(1);
3402 }
3403
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003404 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3405 DAG.getIntPtrConstant(BytesCalleePops, true),
3406 InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00003407 if (!Ins.empty())
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003408 InFlag = Chain.getValue(1);
3409
Dan Gohman98ca4f22009-08-05 01:29:28 +00003410 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3411 Ins, dl, DAG, InVals);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003412}
3413
Dan Gohman98ca4f22009-08-05 01:29:28 +00003414SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003415PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00003416 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003417 SelectionDAG &DAG = CLI.DAG;
3418 DebugLoc &dl = CLI.DL;
3419 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
3420 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
3421 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
3422 SDValue Chain = CLI.Chain;
3423 SDValue Callee = CLI.Callee;
3424 bool &isTailCall = CLI.IsTailCall;
3425 CallingConv::ID CallConv = CLI.CallConv;
3426 bool isVarArg = CLI.IsVarArg;
3427
Evan Cheng0c439eb2010-01-27 00:07:07 +00003428 if (isTailCall)
3429 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3430 Ins, DAG);
3431
Bill Schmidt726c2372012-10-23 15:51:16 +00003432 if (PPCSubTarget.isSVR4ABI()) {
3433 if (PPCSubTarget.isPPC64())
3434 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3435 isTailCall, Outs, OutVals, Ins,
3436 dl, DAG, InVals);
3437 else
3438 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3439 isTailCall, Outs, OutVals, Ins,
3440 dl, DAG, InVals);
3441 }
Chris Lattnerb9082582010-11-14 23:42:06 +00003442
Bill Schmidt726c2372012-10-23 15:51:16 +00003443 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3444 isTailCall, Outs, OutVals, Ins,
3445 dl, DAG, InVals);
Dan Gohman98ca4f22009-08-05 01:29:28 +00003446}
3447
3448SDValue
Bill Schmidt419f3762012-09-19 15:42:13 +00003449PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3450 CallingConv::ID CallConv, bool isVarArg,
3451 bool isTailCall,
3452 const SmallVectorImpl<ISD::OutputArg> &Outs,
3453 const SmallVectorImpl<SDValue> &OutVals,
3454 const SmallVectorImpl<ISD::InputArg> &Ins,
3455 DebugLoc dl, SelectionDAG &DAG,
3456 SmallVectorImpl<SDValue> &InVals) const {
3457 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003458 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003459
Dan Gohman98ca4f22009-08-05 01:29:28 +00003460 assert((CallConv == CallingConv::C ||
3461 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerffd02002009-07-03 06:45:56 +00003462
Tilmann Schellerffd02002009-07-03 06:45:56 +00003463 unsigned PtrByteSize = 4;
3464
3465 MachineFunction &MF = DAG.getMachineFunction();
3466
3467 // Mark this function as potentially containing a function that contains a
3468 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3469 // and restoring the callers stack pointer in this functions epilog. This is
3470 // done because by tail calling the called function might overwrite the value
3471 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003472 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3473 CallConv == CallingConv::Fast)
Tilmann Schellerffd02002009-07-03 06:45:56 +00003474 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003475
Tilmann Schellerffd02002009-07-03 06:45:56 +00003476 // Count how many bytes are to be pushed on the stack, including the linkage
3477 // area, parameter list area and the part of the local variable space which
3478 // contains copies of aggregates which are passed by value.
3479
3480 // Assign locations to all of the outgoing arguments.
3481 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003482 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00003483 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00003484
3485 // Reserve space for the linkage area on the stack.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003486 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003487
3488 if (isVarArg) {
3489 // Handle fixed and variable vector arguments differently.
3490 // Fixed vector arguments go into registers as long as registers are
3491 // available. Variable vector arguments always go into memory.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003492 unsigned NumArgs = Outs.size();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003493
Tilmann Schellerffd02002009-07-03 06:45:56 +00003494 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00003495 MVT ArgVT = Outs[i].VT;
Dan Gohman98ca4f22009-08-05 01:29:28 +00003496 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerffd02002009-07-03 06:45:56 +00003497 bool Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003498
Dan Gohman98ca4f22009-08-05 01:29:28 +00003499 if (Outs[i].IsFixed) {
Bill Schmidt212af6a2013-02-06 17:33:58 +00003500 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3501 CCInfo);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003502 } else {
Bill Schmidt212af6a2013-02-06 17:33:58 +00003503 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3504 ArgFlags, CCInfo);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003505 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003506
Tilmann Schellerffd02002009-07-03 06:45:56 +00003507 if (Result) {
Torok Edwindac237e2009-07-08 20:53:28 +00003508#ifndef NDEBUG
Chris Lattner45cfe542009-08-23 06:03:38 +00003509 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sands1440e8b2010-11-03 11:35:31 +00003510 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +00003511#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00003512 llvm_unreachable(0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003513 }
3514 }
3515 } else {
3516 // All arguments are treated the same.
Bill Schmidt212af6a2013-02-06 17:33:58 +00003517 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003518 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003519
Tilmann Schellerffd02002009-07-03 06:45:56 +00003520 // Assign locations to all of the outgoing aggregate by value arguments.
3521 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003522 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00003523 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00003524
3525 // Reserve stack space for the allocations in CCInfo.
3526 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3527
Bill Schmidt212af6a2013-02-06 17:33:58 +00003528 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003529
3530 // Size of the linkage area, parameter list area and the part of the local
3531 // space variable where copies of aggregates which are passed by value are
3532 // stored.
3533 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003534
Tilmann Schellerffd02002009-07-03 06:45:56 +00003535 // Calculate by how many bytes the stack has to be adjusted in case of tail
3536 // call optimization.
3537 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3538
3539 // Adjust the stack pointer for the new arguments...
3540 // These operations are automatically eliminated by the prolog/epilog pass
3541 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
3542 SDValue CallSeqStart = Chain;
3543
3544 // Load the return address and frame pointer so it can be moved somewhere else
3545 // later.
3546 SDValue LROp, FPOp;
3547 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3548 dl);
3549
3550 // Set up a copy of the stack pointer for use loading and storing any
3551 // arguments that may not fit in the registers available for argument
3552 // passing.
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003554
Tilmann Schellerffd02002009-07-03 06:45:56 +00003555 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3556 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3557 SmallVector<SDValue, 8> MemOpChains;
3558
Roman Divacky0aaa9192011-08-30 17:04:16 +00003559 bool seenFloatArg = false;
Tilmann Schellerffd02002009-07-03 06:45:56 +00003560 // Walk the register/memloc assignments, inserting copies/loads.
3561 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3562 i != e;
3563 ++i) {
3564 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00003565 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00003566 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003567
Tilmann Schellerffd02002009-07-03 06:45:56 +00003568 if (Flags.isByVal()) {
3569 // Argument is an aggregate which is passed by value, thus we need to
3570 // create a copy of it in the local variable space of the current stack
3571 // frame (which is the stack frame of the caller) and pass the address of
3572 // this copy to the callee.
3573 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3574 CCValAssign &ByValVA = ByValArgLocs[j++];
3575 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003576
Tilmann Schellerffd02002009-07-03 06:45:56 +00003577 // Memory reserved in the local variable space of the callers stack frame.
3578 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003579
Tilmann Schellerffd02002009-07-03 06:45:56 +00003580 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3581 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003582
Tilmann Schellerffd02002009-07-03 06:45:56 +00003583 // Create a copy of the argument in the local area of the current
3584 // stack frame.
3585 SDValue MemcpyCall =
3586 CreateCopyOfByValArgument(Arg, PtrOff,
3587 CallSeqStart.getNode()->getOperand(0),
3588 Flags, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003589
Tilmann Schellerffd02002009-07-03 06:45:56 +00003590 // This must go outside the CALLSEQ_START..END.
3591 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
3592 CallSeqStart.getNode()->getOperand(1));
3593 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3594 NewCallSeqStart.getNode());
3595 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003596
Tilmann Schellerffd02002009-07-03 06:45:56 +00003597 // Pass the address of the aggregate copy on the stack either in a
3598 // physical register or in the parameter list area of the current stack
3599 // frame to the callee.
3600 Arg = PtrOff;
3601 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003602
Tilmann Schellerffd02002009-07-03 06:45:56 +00003603 if (VA.isRegLoc()) {
Roman Divacky0aaa9192011-08-30 17:04:16 +00003604 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerffd02002009-07-03 06:45:56 +00003605 // Put argument in a physical register.
3606 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3607 } else {
3608 // Put argument in the parameter list area of the current stack frame.
3609 assert(VA.isMemLoc());
3610 unsigned LocMemOffset = VA.getLocMemOffset();
3611
3612 if (!isTailCall) {
3613 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3614 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3615
3616 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner6229d0a2010-09-21 18:41:36 +00003617 MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00003618 false, false, 0));
Tilmann Schellerffd02002009-07-03 06:45:56 +00003619 } else {
3620 // Calculate and remember argument location.
3621 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3622 TailCallArguments);
3623 }
3624 }
3625 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003626
Tilmann Schellerffd02002009-07-03 06:45:56 +00003627 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00003628 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Schellerffd02002009-07-03 06:45:56 +00003629 &MemOpChains[0], MemOpChains.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003630
Tilmann Schellerffd02002009-07-03 06:45:56 +00003631 // Build a sequence of copy-to-reg nodes chained together with token chain
3632 // and flag operands which copy the outgoing args into the appropriate regs.
3633 SDValue InFlag;
3634 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3635 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3636 RegsToPass[i].second, InFlag);
3637 InFlag = Chain.getValue(1);
3638 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003639
Hal Finkel82b38212012-08-28 02:10:27 +00003640 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3641 // registers.
3642 if (isVarArg) {
NAKAMURA Takumid2a35f22012-08-30 15:52:29 +00003643 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3644 SDValue Ops[] = { Chain, InFlag };
3645
Hal Finkel82b38212012-08-28 02:10:27 +00003646 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
NAKAMURA Takumid2a35f22012-08-30 15:52:29 +00003647 dl, VTs, Ops, InFlag.getNode() ? 2 : 1);
3648
Hal Finkel82b38212012-08-28 02:10:27 +00003649 InFlag = Chain.getValue(1);
3650 }
3651
Chris Lattnerb9082582010-11-14 23:42:06 +00003652 if (isTailCall)
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003653 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3654 false, TailCallArguments);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003655
Dan Gohman98ca4f22009-08-05 01:29:28 +00003656 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3657 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3658 Ins, InVals);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003659}
3660
Bill Schmidt726c2372012-10-23 15:51:16 +00003661// Copy an argument into memory, being careful to do this outside the
3662// call sequence for the call to which the argument belongs.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003663SDValue
Bill Schmidt726c2372012-10-23 15:51:16 +00003664PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3665 SDValue CallSeqStart,
3666 ISD::ArgFlagsTy Flags,
3667 SelectionDAG &DAG,
3668 DebugLoc dl) const {
3669 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3670 CallSeqStart.getNode()->getOperand(0),
3671 Flags, DAG, dl);
3672 // The MEMCPY must go outside the CALLSEQ_START..END.
3673 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
3674 CallSeqStart.getNode()->getOperand(1));
3675 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3676 NewCallSeqStart.getNode());
3677 return NewCallSeqStart;
3678}
3679
3680SDValue
3681PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003682 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003683 bool isTailCall,
3684 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00003685 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003686 const SmallVectorImpl<ISD::InputArg> &Ins,
3687 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00003688 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003689
Bill Schmidt726c2372012-10-23 15:51:16 +00003690 unsigned NumOps = Outs.size();
Bill Schmidt419f3762012-09-19 15:42:13 +00003691
Bill Schmidt726c2372012-10-23 15:51:16 +00003692 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3693 unsigned PtrByteSize = 8;
3694
3695 MachineFunction &MF = DAG.getMachineFunction();
3696
3697 // Mark this function as potentially containing a function that contains a
3698 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3699 // and restoring the callers stack pointer in this functions epilog. This is
3700 // done because by tail calling the called function might overwrite the value
3701 // in this function's (MF) stack pointer stack slot 0(SP).
3702 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3703 CallConv == CallingConv::Fast)
3704 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3705
3706 unsigned nAltivecParamsAtEnd = 0;
3707
3708 // Count how many bytes are to be pushed on the stack, including the linkage
3709 // area, and parameter passing area. We start with at least 48 bytes, which
3710 // is reserved space for [SP][CR][LR][3 x unused].
3711 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
3712 // of this call.
3713 unsigned NumBytes =
3714 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
3715 Outs, OutVals, nAltivecParamsAtEnd);
3716
3717 // Calculate by how many bytes the stack has to be adjusted in case of tail
3718 // call optimization.
3719 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3720
3721 // To protect arguments on the stack from being clobbered in a tail call,
3722 // force all the loads to happen before doing any other lowering.
3723 if (isTailCall)
3724 Chain = DAG.getStackArgumentTokenFactor(Chain);
3725
3726 // Adjust the stack pointer for the new arguments...
3727 // These operations are automatically eliminated by the prolog/epilog pass
3728 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
3729 SDValue CallSeqStart = Chain;
3730
3731 // Load the return address and frame pointer so it can be move somewhere else
3732 // later.
3733 SDValue LROp, FPOp;
3734 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
3735 dl);
3736
3737 // Set up a copy of the stack pointer for use loading and storing any
3738 // arguments that may not fit in the registers available for argument
3739 // passing.
3740 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
3741
3742 // Figure out which arguments are going to go in registers, and which in
3743 // memory. Also, if this is a vararg function, floating point operations
3744 // must be stored to our stack, and loaded into integer regs as well, if
3745 // any integer regs are available for argument passing.
3746 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
3747 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
3748
3749 static const uint16_t GPR[] = {
3750 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3751 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3752 };
3753 static const uint16_t *FPR = GetFPR();
3754
3755 static const uint16_t VR[] = {
3756 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3757 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3758 };
3759 const unsigned NumGPRs = array_lengthof(GPR);
3760 const unsigned NumFPRs = 13;
3761 const unsigned NumVRs = array_lengthof(VR);
3762
3763 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3764 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3765
3766 SmallVector<SDValue, 8> MemOpChains;
3767 for (unsigned i = 0; i != NumOps; ++i) {
3768 SDValue Arg = OutVals[i];
3769 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3770
3771 // PtrOff will be used to store the current argument to the stack if a
3772 // register cannot be found for it.
3773 SDValue PtrOff;
3774
3775 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
3776
3777 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
3778
3779 // Promote integers to 64-bit values.
3780 if (Arg.getValueType() == MVT::i32) {
3781 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
3782 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3783 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
3784 }
3785
3786 // FIXME memcpy is used way more than necessary. Correctness first.
3787 // Note: "by value" is code for passing a structure by value, not
3788 // basic types.
3789 if (Flags.isByVal()) {
3790 // Note: Size includes alignment padding, so
3791 // struct x { short a; char b; }
3792 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
3793 // These are the proper values we need for right-justifying the
3794 // aggregate in a parameter register.
3795 unsigned Size = Flags.getByValSize();
Bill Schmidt42d43352012-10-31 01:15:05 +00003796
3797 // An empty aggregate parameter takes up no storage and no
3798 // registers.
3799 if (Size == 0)
3800 continue;
3801
Bill Schmidt726c2372012-10-23 15:51:16 +00003802 // All aggregates smaller than 8 bytes must be passed right-justified.
3803 if (Size==1 || Size==2 || Size==4) {
3804 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
3805 if (GPR_idx != NumGPRs) {
3806 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
3807 MachinePointerInfo(), VT,
3808 false, false, 0);
3809 MemOpChains.push_back(Load.getValue(1));
3810 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3811
3812 ArgOffset += PtrByteSize;
3813 continue;
3814 }
3815 }
3816
3817 if (GPR_idx == NumGPRs && Size < 8) {
3818 SDValue Const = DAG.getConstant(PtrByteSize - Size,
3819 PtrOff.getValueType());
3820 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3821 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3822 CallSeqStart,
3823 Flags, DAG, dl);
3824 ArgOffset += PtrByteSize;
3825 continue;
3826 }
3827 // Copy entire object into memory. There are cases where gcc-generated
3828 // code assumes it is there, even if it could be put entirely into
3829 // registers. (This is not what the doc says.)
3830
3831 // FIXME: The above statement is likely due to a misunderstanding of the
3832 // documents. All arguments must be copied into the parameter area BY
3833 // THE CALLEE in the event that the callee takes the address of any
3834 // formal argument. That has not yet been implemented. However, it is
3835 // reasonable to use the stack area as a staging area for the register
3836 // load.
3837
3838 // Skip this for small aggregates, as we will use the same slot for a
3839 // right-justified copy, below.
3840 if (Size >= 8)
3841 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
3842 CallSeqStart,
3843 Flags, DAG, dl);
3844
3845 // When a register is available, pass a small aggregate right-justified.
3846 if (Size < 8 && GPR_idx != NumGPRs) {
3847 // The easiest way to get this right-justified in a register
3848 // is to copy the structure into the rightmost portion of a
3849 // local variable slot, then load the whole slot into the
3850 // register.
3851 // FIXME: The memcpy seems to produce pretty awful code for
3852 // small aggregates, particularly for packed ones.
3853 // FIXME: It would be preferable to use the slot in the
3854 // parameter save area instead of a new local variable.
3855 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
3856 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3857 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3858 CallSeqStart,
3859 Flags, DAG, dl);
3860
3861 // Load the slot into the register.
3862 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
3863 MachinePointerInfo(),
3864 false, false, false, 0);
3865 MemOpChains.push_back(Load.getValue(1));
3866 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3867
3868 // Done with this argument.
3869 ArgOffset += PtrByteSize;
3870 continue;
3871 }
3872
3873 // For aggregates larger than PtrByteSize, copy the pieces of the
3874 // object that fit into registers from the parameter save area.
3875 for (unsigned j=0; j<Size; j+=PtrByteSize) {
3876 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
3877 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
3878 if (GPR_idx != NumGPRs) {
3879 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
3880 MachinePointerInfo(),
3881 false, false, false, 0);
3882 MemOpChains.push_back(Load.getValue(1));
3883 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3884 ArgOffset += PtrByteSize;
3885 } else {
3886 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
3887 break;
3888 }
3889 }
3890 continue;
3891 }
3892
3893 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
3894 default: llvm_unreachable("Unexpected ValueType for argument!");
3895 case MVT::i32:
3896 case MVT::i64:
3897 if (GPR_idx != NumGPRs) {
3898 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
3899 } else {
3900 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3901 true, isTailCall, false, MemOpChains,
3902 TailCallArguments, dl);
3903 }
3904 ArgOffset += PtrByteSize;
3905 break;
3906 case MVT::f32:
3907 case MVT::f64:
3908 if (FPR_idx != NumFPRs) {
3909 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
3910
3911 if (isVarArg) {
Bill Schmidte6c56432012-10-29 21:18:16 +00003912 // A single float or an aggregate containing only a single float
3913 // must be passed right-justified in the stack doubleword, and
3914 // in the GPR, if one is available.
3915 SDValue StoreOff;
3916 if (Arg.getValueType().getSimpleVT().SimpleTy == MVT::f32) {
3917 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
3918 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
3919 } else
3920 StoreOff = PtrOff;
3921
3922 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt726c2372012-10-23 15:51:16 +00003923 MachinePointerInfo(), false, false, 0);
3924 MemOpChains.push_back(Store);
3925
3926 // Float varargs are always shadowed in available integer registers
3927 if (GPR_idx != NumGPRs) {
3928 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
3929 MachinePointerInfo(), false, false,
3930 false, 0);
3931 MemOpChains.push_back(Load.getValue(1));
3932 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3933 }
3934 } else if (GPR_idx != NumGPRs)
3935 // If we have any FPRs remaining, we may also have GPRs remaining.
3936 ++GPR_idx;
3937 } else {
3938 // Single-precision floating-point values are mapped to the
3939 // second (rightmost) word of the stack doubleword.
3940 if (Arg.getValueType() == MVT::f32) {
3941 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
3942 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
3943 }
3944
3945 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3946 true, isTailCall, false, MemOpChains,
3947 TailCallArguments, dl);
3948 }
3949 ArgOffset += 8;
3950 break;
3951 case MVT::v4f32:
3952 case MVT::v4i32:
3953 case MVT::v8i16:
3954 case MVT::v16i8:
3955 if (isVarArg) {
3956 // These go aligned on the stack, or in the corresponding R registers
3957 // when within range. The Darwin PPC ABI doc claims they also go in
3958 // V registers; in fact gcc does this only for arguments that are
3959 // prototyped, not for those that match the ... We do it for all
3960 // arguments, seems to work.
3961 while (ArgOffset % 16 !=0) {
3962 ArgOffset += PtrByteSize;
3963 if (GPR_idx != NumGPRs)
3964 GPR_idx++;
3965 }
3966 // We could elide this store in the case where the object fits
3967 // entirely in R registers. Maybe later.
3968 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
3969 DAG.getConstant(ArgOffset, PtrVT));
3970 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
3971 MachinePointerInfo(), false, false, 0);
3972 MemOpChains.push_back(Store);
3973 if (VR_idx != NumVRs) {
3974 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
3975 MachinePointerInfo(),
3976 false, false, false, 0);
3977 MemOpChains.push_back(Load.getValue(1));
3978 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
3979 }
3980 ArgOffset += 16;
3981 for (unsigned i=0; i<16; i+=PtrByteSize) {
3982 if (GPR_idx == NumGPRs)
3983 break;
3984 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
3985 DAG.getConstant(i, PtrVT));
3986 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
3987 false, false, false, 0);
3988 MemOpChains.push_back(Load.getValue(1));
3989 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3990 }
3991 break;
3992 }
3993
3994 // Non-varargs Altivec params generally go in registers, but have
3995 // stack space allocated at the end.
3996 if (VR_idx != NumVRs) {
3997 // Doesn't have GPR space allocated.
3998 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
3999 } else {
4000 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4001 true, isTailCall, true, MemOpChains,
4002 TailCallArguments, dl);
4003 ArgOffset += 16;
4004 }
4005 break;
4006 }
4007 }
4008
4009 if (!MemOpChains.empty())
4010 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4011 &MemOpChains[0], MemOpChains.size());
4012
4013 // Check if this is an indirect call (MTCTR/BCTRL).
4014 // See PrepareCall() for more information about calls through function
4015 // pointers in the 64-bit SVR4 ABI.
4016 if (!isTailCall &&
4017 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4018 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4019 !isBLACompatibleAddress(Callee, DAG)) {
4020 // Load r2 into a virtual register and store it to the TOC save area.
4021 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4022 // TOC save area offset.
4023 SDValue PtrOff = DAG.getIntPtrConstant(40);
4024 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4025 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4026 false, false, 0);
4027 // R12 must contain the address of an indirect callee. This does not
4028 // mean the MTCTR instruction must use R12; it's easier to model this
4029 // as an extra parameter, so do that.
4030 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4031 }
4032
4033 // Build a sequence of copy-to-reg nodes chained together with token chain
4034 // and flag operands which copy the outgoing args into the appropriate regs.
4035 SDValue InFlag;
4036 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4037 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4038 RegsToPass[i].second, InFlag);
4039 InFlag = Chain.getValue(1);
4040 }
4041
4042 if (isTailCall)
4043 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4044 FPOp, true, TailCallArguments);
4045
4046 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4047 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4048 Ins, InVals);
4049}
4050
4051SDValue
4052PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4053 CallingConv::ID CallConv, bool isVarArg,
4054 bool isTailCall,
4055 const SmallVectorImpl<ISD::OutputArg> &Outs,
4056 const SmallVectorImpl<SDValue> &OutVals,
4057 const SmallVectorImpl<ISD::InputArg> &Ins,
4058 DebugLoc dl, SelectionDAG &DAG,
4059 SmallVectorImpl<SDValue> &InVals) const {
4060
4061 unsigned NumOps = Outs.size();
Scott Michelfdc40a02009-02-17 22:15:04 +00004062
Owen Andersone50ed302009-08-10 22:56:29 +00004063 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00004064 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerc91a4752006-06-26 22:48:35 +00004065 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004066
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004067 MachineFunction &MF = DAG.getMachineFunction();
4068
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004069 // Mark this function as potentially containing a function that contains a
4070 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4071 // and restoring the callers stack pointer in this functions epilog. This is
4072 // done because by tail calling the called function might overwrite the value
4073 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky8a8d4792011-12-02 22:16:29 +00004074 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4075 CallConv == CallingConv::Fast)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004076 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4077
4078 unsigned nAltivecParamsAtEnd = 0;
4079
Chris Lattnerabde4602006-05-16 22:56:08 +00004080 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerc91a4752006-06-26 22:48:35 +00004081 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004082 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004083 unsigned NumBytes =
Dan Gohman98ca4f22009-08-05 01:29:28 +00004084 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanc9403652010-07-07 15:54:55 +00004085 Outs, OutVals,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004086 nAltivecParamsAtEnd);
Dale Johannesen75092de2008-03-12 00:22:17 +00004087
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004088 // Calculate by how many bytes the stack has to be adjusted in case of tail
4089 // call optimization.
4090 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelfdc40a02009-02-17 22:15:04 +00004091
Dan Gohman98ca4f22009-08-05 01:29:28 +00004092 // To protect arguments on the stack from being clobbered in a tail call,
4093 // force all the loads to happen before doing any other lowering.
4094 if (isTailCall)
4095 Chain = DAG.getStackArgumentTokenFactor(Chain);
4096
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004097 // Adjust the stack pointer for the new arguments...
4098 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +00004099 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohman475871a2008-07-27 21:46:04 +00004100 SDValue CallSeqStart = Chain;
Scott Michelfdc40a02009-02-17 22:15:04 +00004101
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004102 // Load the return address and frame pointer so it can be move somewhere else
4103 // later.
Dan Gohman475871a2008-07-27 21:46:04 +00004104 SDValue LROp, FPOp;
Tilmann Schellerffd02002009-07-03 06:45:56 +00004105 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4106 dl);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004107
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004108 // Set up a copy of the stack pointer for use loading and storing any
4109 // arguments that may not fit in the registers available for argument
4110 // passing.
Dan Gohman475871a2008-07-27 21:46:04 +00004111 SDValue StackPtr;
Chris Lattnerc91a4752006-06-26 22:48:35 +00004112 if (isPPC64)
Owen Anderson825b72b2009-08-11 20:47:22 +00004113 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerc91a4752006-06-26 22:48:35 +00004114 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004115 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00004116
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004117 // Figure out which arguments are going to go in registers, and which in
4118 // memory. Also, if this is a vararg function, floating point operations
4119 // must be stored to our stack, and loaded into integer regs as well, if
4120 // any integer regs are available for argument passing.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00004121 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004122 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004123
Craig Topperb78ca422012-03-11 07:16:55 +00004124 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner9a2a4972006-05-17 06:01:33 +00004125 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4126 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4127 };
Craig Topperb78ca422012-03-11 07:16:55 +00004128 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerc91a4752006-06-26 22:48:35 +00004129 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4130 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4131 };
Craig Topperb78ca422012-03-11 07:16:55 +00004132 static const uint16_t *FPR = GetFPR();
Scott Michelfdc40a02009-02-17 22:15:04 +00004133
Craig Topperb78ca422012-03-11 07:16:55 +00004134 static const uint16_t VR[] = {
Chris Lattner9a2a4972006-05-17 06:01:33 +00004135 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4136 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4137 };
Owen Anderson718cb662007-09-07 04:06:50 +00004138 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004139 const unsigned NumFPRs = 13;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00004140 const unsigned NumVRs = array_lengthof(VR);
Scott Michelfdc40a02009-02-17 22:15:04 +00004141
Craig Topperb78ca422012-03-11 07:16:55 +00004142 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerc91a4752006-06-26 22:48:35 +00004143
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004144 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004145 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4146
Dan Gohman475871a2008-07-27 21:46:04 +00004147 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng4360bdc2006-05-25 00:57:32 +00004148 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00004149 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00004150 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00004151
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004152 // PtrOff will be used to store the current argument to the stack if a
4153 // register cannot be found for it.
Dan Gohman475871a2008-07-27 21:46:04 +00004154 SDValue PtrOff;
Scott Michelfdc40a02009-02-17 22:15:04 +00004155
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004156 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00004157
Dale Johannesen39355f92009-02-04 02:34:38 +00004158 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerc91a4752006-06-26 22:48:35 +00004159
4160 // On PPC64, promote integers to 64-bit values.
Owen Anderson825b72b2009-08-11 20:47:22 +00004161 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00004162 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4163 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson825b72b2009-08-11 20:47:22 +00004164 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00004165 }
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004166
Dale Johannesen8419dd62008-03-07 20:27:40 +00004167 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt419f3762012-09-19 15:42:13 +00004168 // Note: "by value" is code for passing a structure by value, not
4169 // basic types.
Duncan Sands276dcbd2008-03-21 09:14:45 +00004170 if (Flags.isByVal()) {
4171 unsigned Size = Flags.getByValSize();
Bill Schmidt726c2372012-10-23 15:51:16 +00004172 // Very small objects are passed right-justified. Everything else is
4173 // passed left-justified.
4174 if (Size==1 || Size==2) {
4175 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesen8419dd62008-03-07 20:27:40 +00004176 if (GPR_idx != NumGPRs) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004177 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004178 MachinePointerInfo(), VT,
4179 false, false, 0);
Dale Johannesen8419dd62008-03-07 20:27:40 +00004180 MemOpChains.push_back(Load.getValue(1));
4181 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004182
4183 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00004184 } else {
Bill Schmidt7a6cb152012-10-16 13:30:53 +00004185 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4186 PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00004187 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt726c2372012-10-23 15:51:16 +00004188 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4189 CallSeqStart,
4190 Flags, DAG, dl);
Dale Johannesen8419dd62008-03-07 20:27:40 +00004191 ArgOffset += PtrByteSize;
4192 }
4193 continue;
4194 }
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00004195 // Copy entire object into memory. There are cases where gcc-generated
4196 // code assumes it is there, even if it could be put entirely into
4197 // registers. (This is not what the doc says.)
Bill Schmidt726c2372012-10-23 15:51:16 +00004198 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4199 CallSeqStart,
4200 Flags, DAG, dl);
Bill Schmidt419f3762012-09-19 15:42:13 +00004201
4202 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4203 // copy the pieces of the object that fit into registers from the
4204 // parameter save area.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004205 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00004206 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00004207 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004208 if (GPR_idx != NumGPRs) {
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004209 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4210 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004211 false, false, false, 0);
Dale Johannesen1f797a32008-03-05 23:31:27 +00004212 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004213 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004214 ArgOffset += PtrByteSize;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004215 } else {
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00004216 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00004217 break;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004218 }
4219 }
4220 continue;
4221 }
4222
Owen Anderson825b72b2009-08-11 20:47:22 +00004223 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004224 default: llvm_unreachable("Unexpected ValueType for argument!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004225 case MVT::i32:
4226 case MVT::i64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00004227 if (GPR_idx != NumGPRs) {
4228 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004229 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004230 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4231 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004232 TailCallArguments, dl);
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004233 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004234 ArgOffset += PtrByteSize;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004235 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004236 case MVT::f32:
4237 case MVT::f64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00004238 if (FPR_idx != NumFPRs) {
4239 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4240
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004241 if (isVarArg) {
Chris Lattner6229d0a2010-09-21 18:41:36 +00004242 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4243 MachinePointerInfo(), false, false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004244 MemOpChains.push_back(Store);
4245
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004246 // Float varargs are always shadowed in available integer registers
Chris Lattner9a2a4972006-05-17 06:01:33 +00004247 if (GPR_idx != NumGPRs) {
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004248 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004249 MachinePointerInfo(), false, false,
4250 false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004251 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004252 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004253 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004254 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman475871a2008-07-27 21:46:04 +00004255 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00004256 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004257 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4258 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004259 false, false, false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004260 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004261 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerabde4602006-05-16 22:56:08 +00004262 }
4263 } else {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004264 // If we have any FPRs remaining, we may also have GPRs remaining.
4265 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4266 // GPRs.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004267 if (GPR_idx != NumGPRs)
4268 ++GPR_idx;
Owen Anderson825b72b2009-08-11 20:47:22 +00004269 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004270 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4271 ++GPR_idx;
Chris Lattnerabde4602006-05-16 22:56:08 +00004272 }
Bill Schmidt726c2372012-10-23 15:51:16 +00004273 } else
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004274 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4275 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004276 TailCallArguments, dl);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004277 if (isPPC64)
4278 ArgOffset += 8;
4279 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004280 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004281 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004282 case MVT::v4f32:
4283 case MVT::v4i32:
4284 case MVT::v8i16:
4285 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00004286 if (isVarArg) {
4287 // These go aligned on the stack, or in the corresponding R registers
Scott Michelfdc40a02009-02-17 22:15:04 +00004288 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesen75092de2008-03-12 00:22:17 +00004289 // V registers; in fact gcc does this only for arguments that are
4290 // prototyped, not for those that match the ... We do it for all
4291 // arguments, seems to work.
4292 while (ArgOffset % 16 !=0) {
4293 ArgOffset += PtrByteSize;
4294 if (GPR_idx != NumGPRs)
4295 GPR_idx++;
4296 }
4297 // We could elide this store in the case where the object fits
4298 // entirely in R registers. Maybe later.
Scott Michelfdc40a02009-02-17 22:15:04 +00004299 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesen75092de2008-03-12 00:22:17 +00004300 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner6229d0a2010-09-21 18:41:36 +00004301 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4302 MachinePointerInfo(), false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00004303 MemOpChains.push_back(Store);
4304 if (VR_idx != NumVRs) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004305 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004306 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004307 false, false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00004308 MemOpChains.push_back(Load.getValue(1));
4309 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4310 }
4311 ArgOffset += 16;
4312 for (unsigned i=0; i<16; i+=PtrByteSize) {
4313 if (GPR_idx == NumGPRs)
4314 break;
Dale Johannesen39355f92009-02-04 02:34:38 +00004315 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesen75092de2008-03-12 00:22:17 +00004316 DAG.getConstant(i, PtrVT));
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004317 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004318 false, false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00004319 MemOpChains.push_back(Load.getValue(1));
4320 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4321 }
4322 break;
4323 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004324
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004325 // Non-varargs Altivec params generally go in registers, but have
4326 // stack space allocated at the end.
4327 if (VR_idx != NumVRs) {
4328 // Doesn't have GPR space allocated.
4329 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4330 } else if (nAltivecParamsAtEnd==0) {
4331 // We are emitting Altivec params in order.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004332 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4333 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004334 TailCallArguments, dl);
Dale Johannesen75092de2008-03-12 00:22:17 +00004335 ArgOffset += 16;
Dale Johannesen75092de2008-03-12 00:22:17 +00004336 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004337 break;
Chris Lattnerabde4602006-05-16 22:56:08 +00004338 }
Chris Lattnerabde4602006-05-16 22:56:08 +00004339 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004340 // If all Altivec parameters fit in registers, as they usually do,
4341 // they get stack space following the non-Altivec parameters. We
4342 // don't track this here because nobody below needs it.
4343 // If there are more Altivec parameters than fit in registers emit
4344 // the stores here.
4345 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4346 unsigned j = 0;
4347 // Offset is aligned; skip 1st 12 params which go in V registers.
4348 ArgOffset = ((ArgOffset+15)/16)*16;
4349 ArgOffset += 12*16;
4350 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00004351 SDValue Arg = OutVals[i];
4352 EVT ArgType = Outs[i].VT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004353 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4354 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004355 if (++j > NumVRs) {
Dan Gohman475871a2008-07-27 21:46:04 +00004356 SDValue PtrOff;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004357 // We are emitting Altivec params in order.
4358 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4359 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004360 TailCallArguments, dl);
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004361 ArgOffset += 16;
4362 }
4363 }
4364 }
4365 }
4366
Chris Lattner9a2a4972006-05-17 06:01:33 +00004367 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00004368 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnere2199452006-08-11 17:38:39 +00004369 &MemOpChains[0], MemOpChains.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00004370
Dale Johannesenf7b73042010-03-09 20:15:42 +00004371 // On Darwin, R12 must contain the address of an indirect callee. This does
4372 // not mean the MTCTR instruction must use R12; it's easier to model this as
4373 // an extra parameter, so do that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004374 if (!isTailCall &&
Dale Johannesenf7b73042010-03-09 20:15:42 +00004375 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4376 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4377 !isBLACompatibleAddress(Callee, DAG))
4378 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4379 PPC::R12), Callee));
4380
Chris Lattner9a2a4972006-05-17 06:01:33 +00004381 // Build a sequence of copy-to-reg nodes chained together with token chain
4382 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00004383 SDValue InFlag;
Chris Lattner9a2a4972006-05-17 06:01:33 +00004384 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004385 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen39355f92009-02-04 02:34:38 +00004386 RegsToPass[i].second, InFlag);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004387 InFlag = Chain.getValue(1);
4388 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004389
Chris Lattnerb9082582010-11-14 23:42:06 +00004390 if (isTailCall)
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004391 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4392 FPOp, true, TailCallArguments);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004393
Dan Gohman98ca4f22009-08-05 01:29:28 +00004394 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4395 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4396 Ins, InVals);
Chris Lattnerabde4602006-05-16 22:56:08 +00004397}
4398
Hal Finkeld712f932011-10-14 19:51:36 +00004399bool
4400PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4401 MachineFunction &MF, bool isVarArg,
4402 const SmallVectorImpl<ISD::OutputArg> &Outs,
4403 LLVMContext &Context) const {
4404 SmallVector<CCValAssign, 16> RVLocs;
4405 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4406 RVLocs, Context);
4407 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4408}
4409
Dan Gohman98ca4f22009-08-05 01:29:28 +00004410SDValue
4411PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00004412 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004413 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00004414 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00004415 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00004416
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004417 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00004418 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00004419 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004420 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelfdc40a02009-02-17 22:15:04 +00004421
Dan Gohman475871a2008-07-27 21:46:04 +00004422 SDValue Flag;
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004423 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00004424
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004425 // Copy the result values into the output registers.
4426 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4427 CCValAssign &VA = RVLocs[i];
4428 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand86aef0a2012-11-05 19:39:45 +00004429
4430 SDValue Arg = OutVals[i];
4431
4432 switch (VA.getLocInfo()) {
4433 default: llvm_unreachable("Unknown loc info!");
4434 case CCValAssign::Full: break;
4435 case CCValAssign::AExt:
4436 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4437 break;
4438 case CCValAssign::ZExt:
4439 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4440 break;
4441 case CCValAssign::SExt:
4442 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4443 break;
4444 }
4445
4446 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004447 Flag = Chain.getValue(1);
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004448 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004449 }
4450
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004451 RetOps[0] = Chain; // Update chain.
4452
4453 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00004454 if (Flag.getNode())
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004455 RetOps.push_back(Flag);
4456
4457 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other,
4458 &RetOps[0], RetOps.size());
Chris Lattner1a635d62006-04-14 06:01:58 +00004459}
4460
Dan Gohman475871a2008-07-27 21:46:04 +00004461SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004462 const PPCSubtarget &Subtarget) const {
Jim Laskeyefc7e522006-12-04 22:04:42 +00004463 // When we pop the dynamic allocation we need to restore the SP link.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004464 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00004465
Jim Laskeyefc7e522006-12-04 22:04:42 +00004466 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00004467 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeyefc7e522006-12-04 22:04:42 +00004468
4469 // Construct the stack pointer operand.
Dale Johannesenb60d5192009-11-24 01:09:07 +00004470 bool isPPC64 = Subtarget.isPPC64();
4471 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman475871a2008-07-27 21:46:04 +00004472 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeyefc7e522006-12-04 22:04:42 +00004473
4474 // Get the operands for the STACKRESTORE.
Dan Gohman475871a2008-07-27 21:46:04 +00004475 SDValue Chain = Op.getOperand(0);
4476 SDValue SaveSP = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004477
Jim Laskeyefc7e522006-12-04 22:04:42 +00004478 // Load the old link SP.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004479 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4480 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004481 false, false, false, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004482
Jim Laskeyefc7e522006-12-04 22:04:42 +00004483 // Restore the stack pointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004484 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelfdc40a02009-02-17 22:15:04 +00004485
Jim Laskeyefc7e522006-12-04 22:04:42 +00004486 // Store the old link SP.
Chris Lattner6229d0a2010-09-21 18:41:36 +00004487 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00004488 false, false, 0);
Jim Laskeyefc7e522006-12-04 22:04:42 +00004489}
4490
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004491
4492
Dan Gohman475871a2008-07-27 21:46:04 +00004493SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004494PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00004495 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesenb60d5192009-11-24 01:09:07 +00004496 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004497 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00004498 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004499
4500 // Get current frame pointer save index. The users of this index will be
4501 // primarily DYNALLOC instructions.
4502 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4503 int RASI = FI->getReturnAddrSaveIndex();
4504
4505 // If the frame pointer save index hasn't been defined yet.
4506 if (!RASI) {
4507 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00004508 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004509 // Allocate the frame index for frame pointer save area.
Evan Chenged2ae132010-07-03 00:40:23 +00004510 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004511 // Save the result.
4512 FI->setReturnAddrSaveIndex(RASI);
4513 }
4514 return DAG.getFrameIndex(RASI, PtrVT);
4515}
4516
Dan Gohman475871a2008-07-27 21:46:04 +00004517SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004518PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4519 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesenb60d5192009-11-24 01:09:07 +00004520 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004521 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00004522 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00004523
4524 // Get current frame pointer save index. The users of this index will be
4525 // primarily DYNALLOC instructions.
4526 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4527 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004528
Jim Laskey2f616bf2006-11-16 22:43:37 +00004529 // If the frame pointer save index hasn't been defined yet.
4530 if (!FPSI) {
4531 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00004532 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004533 isDarwinABI);
Scott Michelfdc40a02009-02-17 22:15:04 +00004534
Jim Laskey2f616bf2006-11-16 22:43:37 +00004535 // Allocate the frame index for frame pointer save area.
Evan Chenged2ae132010-07-03 00:40:23 +00004536 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004537 // Save the result.
Scott Michelfdc40a02009-02-17 22:15:04 +00004538 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004539 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004540 return DAG.getFrameIndex(FPSI, PtrVT);
4541}
Jim Laskey2f616bf2006-11-16 22:43:37 +00004542
Dan Gohman475871a2008-07-27 21:46:04 +00004543SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004544 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004545 const PPCSubtarget &Subtarget) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00004546 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004547 SDValue Chain = Op.getOperand(0);
4548 SDValue Size = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004549 DebugLoc dl = Op.getDebugLoc();
4550
Jim Laskey2f616bf2006-11-16 22:43:37 +00004551 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00004552 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00004553 // Negate the size.
Dale Johannesende064702009-02-06 21:50:26 +00004554 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey2f616bf2006-11-16 22:43:37 +00004555 DAG.getConstant(0, PtrVT), Size);
4556 // Construct a node for the frame pointer save index.
Dan Gohman475871a2008-07-27 21:46:04 +00004557 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004558 // Build a DYNALLOC node.
Dan Gohman475871a2008-07-27 21:46:04 +00004559 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson825b72b2009-08-11 20:47:22 +00004560 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesende064702009-02-06 21:50:26 +00004561 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004562}
4563
Chris Lattner1a635d62006-04-14 06:01:58 +00004564/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4565/// possible.
Dan Gohmand858e902010-04-17 15:26:15 +00004566SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner1a635d62006-04-14 06:01:58 +00004567 // Not FP? Not a fsel.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004568 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4569 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedmanc06441e2009-05-28 04:31:08 +00004570 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004571
Chris Lattner1a635d62006-04-14 06:01:58 +00004572 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004573
Chris Lattner1a635d62006-04-14 06:01:58 +00004574 // Cannot handle SETEQ/SETNE.
Eli Friedmanc06441e2009-05-28 04:31:08 +00004575 if (CC == ISD::SETEQ || CC == ISD::SETNE) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004576
Owen Andersone50ed302009-08-10 22:56:29 +00004577 EVT ResVT = Op.getValueType();
4578 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004579 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4580 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00004581 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00004582
Chris Lattner1a635d62006-04-14 06:01:58 +00004583 // If the RHS of the comparison is a 0.0, we don't need to do the
4584 // subtraction at all.
4585 if (isFloatingPointZero(RHS))
4586 switch (CC) {
4587 default: break; // SETUO etc aren't handled by fsel.
4588 case ISD::SETULT:
4589 case ISD::SETLT:
4590 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00004591 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004592 case ISD::SETGE:
Owen Anderson825b72b2009-08-11 20:47:22 +00004593 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4594 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00004595 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004596 case ISD::SETUGT:
4597 case ISD::SETGT:
4598 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00004599 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004600 case ISD::SETLE:
Owen Anderson825b72b2009-08-11 20:47:22 +00004601 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4602 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00004603 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00004604 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004605 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004606
Dan Gohman475871a2008-07-27 21:46:04 +00004607 SDValue Cmp;
Chris Lattner1a635d62006-04-14 06:01:58 +00004608 switch (CC) {
4609 default: break; // SETUO etc aren't handled by fsel.
4610 case ISD::SETULT:
4611 case ISD::SETLT:
Dale Johannesende064702009-02-06 21:50:26 +00004612 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004613 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4614 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00004615 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00004616 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004617 case ISD::SETGE:
Dale Johannesende064702009-02-06 21:50:26 +00004618 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004619 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4620 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00004621 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004622 case ISD::SETUGT:
4623 case ISD::SETGT:
Dale Johannesende064702009-02-06 21:50:26 +00004624 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004625 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4626 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00004627 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00004628 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004629 case ISD::SETLE:
Dale Johannesende064702009-02-06 21:50:26 +00004630 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004631 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4632 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00004633 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004634 }
Eli Friedmanc06441e2009-05-28 04:31:08 +00004635 return Op;
Chris Lattner1a635d62006-04-14 06:01:58 +00004636}
4637
Chris Lattner1f873002007-11-28 18:44:47 +00004638// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004639SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004640 DebugLoc dl) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004641 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman475871a2008-07-27 21:46:04 +00004642 SDValue Src = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00004643 if (Src.getValueType() == MVT::f32)
4644 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sandsa7360f02008-07-19 16:26:02 +00004645
Dan Gohman475871a2008-07-27 21:46:04 +00004646 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00004647 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004648 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004649 case MVT::i32:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004650 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004651 PPCISD::FCTIDZ,
Owen Anderson825b72b2009-08-11 20:47:22 +00004652 dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00004653 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004654 case MVT::i64:
4655 Tmp = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00004656 break;
4657 }
Duncan Sandsa7360f02008-07-19 16:26:02 +00004658
Chris Lattner1a635d62006-04-14 06:01:58 +00004659 // Convert the FP value to an int value through memory.
Owen Anderson825b72b2009-08-11 20:47:22 +00004660 SDValue FIPtr = DAG.CreateStackTemporary(MVT::f64);
Duncan Sandsa7360f02008-07-19 16:26:02 +00004661
Chris Lattner1de7c1d2007-10-15 20:14:52 +00004662 // Emit a store to the stack slot.
Chris Lattner6229d0a2010-09-21 18:41:36 +00004663 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
4664 MachinePointerInfo(), false, false, 0);
Chris Lattner1de7c1d2007-10-15 20:14:52 +00004665
4666 // Result is a load from the stack slot. If loading 4 bytes, make sure to
4667 // add in a bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00004668 if (Op.getValueType() == MVT::i32)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004669 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner1de7c1d2007-10-15 20:14:52 +00004670 DAG.getConstant(4, FIPtr.getValueType()));
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004671 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004672 false, false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004673}
4674
Dan Gohmand858e902010-04-17 15:26:15 +00004675SDValue PPCTargetLowering::LowerSINT_TO_FP(SDValue Op,
4676 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004677 DebugLoc dl = Op.getDebugLoc();
Dan Gohman034f60e2008-03-11 01:59:03 +00004678 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson825b72b2009-08-11 20:47:22 +00004679 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman475871a2008-07-27 21:46:04 +00004680 return SDValue();
Dan Gohman034f60e2008-03-11 01:59:03 +00004681
Owen Anderson825b72b2009-08-11 20:47:22 +00004682 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigand6c28a7e2012-10-18 13:16:11 +00004683 SDValue SINT = Op.getOperand(0);
4684 // When converting to single-precision, we actually need to convert
4685 // to double-precision first and then round to single-precision.
4686 // To avoid double-rounding effects during that operation, we have
4687 // to prepare the input operand. Bits that might be truncated when
4688 // converting to double-precision are replaced by a bit that won't
4689 // be lost at this stage, but is below the single-precision rounding
4690 // position.
4691 //
4692 // However, if -enable-unsafe-fp-math is in effect, accept double
4693 // rounding to avoid the extra overhead.
4694 if (Op.getValueType() == MVT::f32 &&
4695 !DAG.getTarget().Options.UnsafeFPMath) {
4696
4697 // Twiddle input to make sure the low 11 bits are zero. (If this
4698 // is the case, we are guaranteed the value will fit into the 53 bit
4699 // mantissa of an IEEE double-precision value without rounding.)
4700 // If any of those low 11 bits were not zero originally, make sure
4701 // bit 12 (value 2048) is set instead, so that the final rounding
4702 // to single-precision gets the correct result.
4703 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4704 SINT, DAG.getConstant(2047, MVT::i64));
4705 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
4706 Round, DAG.getConstant(2047, MVT::i64));
4707 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
4708 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4709 Round, DAG.getConstant(-2048, MVT::i64));
4710
4711 // However, we cannot use that value unconditionally: if the magnitude
4712 // of the input value is small, the bit-twiddling we did above might
4713 // end up visibly changing the output. Fortunately, in that case, we
4714 // don't need to twiddle bits since the original input will convert
4715 // exactly to double-precision floating-point already. Therefore,
4716 // construct a conditional to use the original value if the top 11
4717 // bits are all sign-bit copies, and use the rounded value computed
4718 // above otherwise.
4719 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
4720 SINT, DAG.getConstant(53, MVT::i32));
4721 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
4722 Cond, DAG.getConstant(1, MVT::i64));
4723 Cond = DAG.getSetCC(dl, MVT::i32,
4724 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
4725
4726 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
4727 }
4728 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Owen Anderson825b72b2009-08-11 20:47:22 +00004729 SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Bits);
4730 if (Op.getValueType() == MVT::f32)
Scott Michelfdc40a02009-02-17 22:15:04 +00004731 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004732 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00004733 return FP;
4734 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004735
Owen Anderson825b72b2009-08-11 20:47:22 +00004736 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Chris Lattner1a635d62006-04-14 06:01:58 +00004737 "Unhandled SINT_TO_FP type in custom expander!");
4738 // Since we only generate this in 64-bit mode, we can take advantage of
4739 // 64-bit registers. In particular, sign extend the input value into the
4740 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
4741 // then lfd it and fcfid it.
Dan Gohmanc76909a2009-09-25 20:36:54 +00004742 MachineFunction &MF = DAG.getMachineFunction();
4743 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00004744 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
Owen Andersone50ed302009-08-10 22:56:29 +00004745 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00004746 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004747
Owen Anderson825b72b2009-08-11 20:47:22 +00004748 SDValue Ext64 = DAG.getNode(PPCISD::EXTSW_32, dl, MVT::i32,
Chris Lattner1a635d62006-04-14 06:01:58 +00004749 Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004750
Chris Lattner1a635d62006-04-14 06:01:58 +00004751 // STD the extended value into the stack slot.
Dan Gohmanc76909a2009-09-25 20:36:54 +00004752 MachineMemOperand *MMO =
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004753 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattner59db5492010-09-21 04:39:43 +00004754 MachineMemOperand::MOStore, 8, 8);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004755 SDValue Ops[] = { DAG.getEntryNode(), Ext64, FIdx };
4756 SDValue Store =
4757 DAG.getMemIntrinsicNode(PPCISD::STD_32, dl, DAG.getVTList(MVT::Other),
4758 Ops, 4, MVT::i64, MMO);
Chris Lattner1a635d62006-04-14 06:01:58 +00004759 // Load the value as a double.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004760 SDValue Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004761 false, false, false, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004762
Chris Lattner1a635d62006-04-14 06:01:58 +00004763 // FCFID it and return it.
Owen Anderson825b72b2009-08-11 20:47:22 +00004764 SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Ld);
4765 if (Op.getValueType() == MVT::f32)
4766 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00004767 return FP;
4768}
4769
Dan Gohmand858e902010-04-17 15:26:15 +00004770SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
4771 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004772 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004773 /*
4774 The rounding mode is in bits 30:31 of FPSR, and has the following
4775 settings:
4776 00 Round to nearest
4777 01 Round to 0
4778 10 Round to +inf
4779 11 Round to -inf
4780
4781 FLT_ROUNDS, on the other hand, expects the following:
4782 -1 Undefined
4783 0 Round to 0
4784 1 Round to nearest
4785 2 Round to +inf
4786 3 Round to -inf
4787
4788 To perform the conversion, we do:
4789 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
4790 */
4791
4792 MachineFunction &MF = DAG.getMachineFunction();
Owen Andersone50ed302009-08-10 22:56:29 +00004793 EVT VT = Op.getValueType();
4794 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00004795 SDValue MFFSreg, InFlag;
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004796
4797 // Save FP Control Word to register
Benjamin Kramer3853f742013-03-07 20:33:29 +00004798 EVT NodeTys[] = {
4799 MVT::f64, // return register
4800 MVT::Glue // unused in this context
4801 };
Dale Johannesen33c960f2009-02-04 20:06:27 +00004802 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004803
4804 // Save FP register to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00004805 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00004806 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00004807 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner6229d0a2010-09-21 18:41:36 +00004808 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004809
4810 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00004811 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00004812 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004813 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004814 false, false, false, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004815
4816 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00004817 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00004818 DAG.getNode(ISD::AND, dl, MVT::i32,
4819 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00004820 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00004821 DAG.getNode(ISD::SRL, dl, MVT::i32,
4822 DAG.getNode(ISD::AND, dl, MVT::i32,
4823 DAG.getNode(ISD::XOR, dl, MVT::i32,
4824 CWD, DAG.getConstant(3, MVT::i32)),
4825 DAG.getConstant(3, MVT::i32)),
4826 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004827
Dan Gohman475871a2008-07-27 21:46:04 +00004828 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00004829 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004830
Duncan Sands83ec4b62008-06-06 12:08:01 +00004831 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen33c960f2009-02-04 20:06:27 +00004832 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004833}
4834
Dan Gohmand858e902010-04-17 15:26:15 +00004835SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004836 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004837 unsigned BitWidth = VT.getSizeInBits();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004838 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9ed06db2008-03-07 20:36:53 +00004839 assert(Op.getNumOperands() == 3 &&
4840 VT == Op.getOperand(1).getValueType() &&
4841 "Unexpected SHL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004842
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00004843 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00004844 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00004845 SDValue Lo = Op.getOperand(0);
4846 SDValue Hi = Op.getOperand(1);
4847 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00004848 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00004849
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004850 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00004851 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004852 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
4853 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
4854 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
4855 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00004856 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004857 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
4858 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
4859 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00004860 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004861 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00004862}
4863
Dan Gohmand858e902010-04-17 15:26:15 +00004864SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004865 EVT VT = Op.getValueType();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004866 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004867 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00004868 assert(Op.getNumOperands() == 3 &&
4869 VT == Op.getOperand(1).getValueType() &&
4870 "Unexpected SRL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004871
Dan Gohman9ed06db2008-03-07 20:36:53 +00004872 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00004873 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00004874 SDValue Lo = Op.getOperand(0);
4875 SDValue Hi = Op.getOperand(1);
4876 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00004877 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00004878
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004879 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00004880 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004881 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
4882 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
4883 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
4884 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00004885 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004886 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
4887 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
4888 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00004889 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004890 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00004891}
4892
Dan Gohmand858e902010-04-17 15:26:15 +00004893SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004894 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004895 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004896 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00004897 assert(Op.getNumOperands() == 3 &&
4898 VT == Op.getOperand(1).getValueType() &&
4899 "Unexpected SRA!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004900
Dan Gohman9ed06db2008-03-07 20:36:53 +00004901 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman475871a2008-07-27 21:46:04 +00004902 SDValue Lo = Op.getOperand(0);
4903 SDValue Hi = Op.getOperand(1);
4904 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00004905 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00004906
Dale Johannesenf5d97892009-02-04 01:48:28 +00004907 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00004908 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf5d97892009-02-04 01:48:28 +00004909 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
4910 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
4911 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
4912 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00004913 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf5d97892009-02-04 01:48:28 +00004914 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
4915 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
4916 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands2fbfbd22008-10-30 19:28:32 +00004917 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman475871a2008-07-27 21:46:04 +00004918 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004919 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00004920}
4921
4922//===----------------------------------------------------------------------===//
4923// Vector related lowering.
4924//
4925
Chris Lattner4a998b92006-04-17 06:00:21 +00004926/// BuildSplatI - Build a canonical splati of Val with an element size of
4927/// SplatSize. Cast the result to VT.
Owen Andersone50ed302009-08-10 22:56:29 +00004928static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Dale Johannesened2eee62009-02-06 01:31:28 +00004929 SelectionDAG &DAG, DebugLoc dl) {
Chris Lattner4a998b92006-04-17 06:00:21 +00004930 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner70fa4932006-12-01 01:45:39 +00004931
Owen Andersone50ed302009-08-10 22:56:29 +00004932 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson825b72b2009-08-11 20:47:22 +00004933 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner4a998b92006-04-17 06:00:21 +00004934 };
Chris Lattner70fa4932006-12-01 01:45:39 +00004935
Owen Anderson825b72b2009-08-11 20:47:22 +00004936 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00004937
Chris Lattner70fa4932006-12-01 01:45:39 +00004938 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
4939 if (Val == -1)
4940 SplatSize = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00004941
Owen Andersone50ed302009-08-10 22:56:29 +00004942 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00004943
Chris Lattner4a998b92006-04-17 06:00:21 +00004944 // Build a canonical splat for this value.
Owen Anderson825b72b2009-08-11 20:47:22 +00004945 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00004946 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004947 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga87008d2009-02-25 22:49:59 +00004948 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
4949 &Ops[0], Ops.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004950 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00004951}
4952
Chris Lattnere7c768e2006-04-18 03:24:30 +00004953/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00004954/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00004955static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Dale Johannesened2eee62009-02-06 01:31:28 +00004956 SelectionDAG &DAG, DebugLoc dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004957 EVT DestVT = MVT::Other) {
4958 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00004959 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00004960 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner6876e662006-04-17 06:58:41 +00004961}
4962
Chris Lattnere7c768e2006-04-18 03:24:30 +00004963/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
4964/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00004965static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesened2eee62009-02-06 01:31:28 +00004966 SDValue Op2, SelectionDAG &DAG,
Owen Anderson825b72b2009-08-11 20:47:22 +00004967 DebugLoc dl, EVT DestVT = MVT::Other) {
4968 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00004969 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00004970 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnere7c768e2006-04-18 03:24:30 +00004971}
4972
4973
Chris Lattnerbdd558c2006-04-17 17:55:10 +00004974/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
4975/// amount. The result has the specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00004976static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Owen Andersone50ed302009-08-10 22:56:29 +00004977 EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Chris Lattnerbdd558c2006-04-17 17:55:10 +00004978 // Force LHS/RHS to be the right type.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004979 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
4980 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsd038e042008-07-21 10:20:31 +00004981
Nate Begeman9008ca62009-04-27 18:41:29 +00004982 int Ops[16];
Chris Lattnerbdd558c2006-04-17 17:55:10 +00004983 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004984 Ops[i] = i + Amt;
Owen Anderson825b72b2009-08-11 20:47:22 +00004985 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004986 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00004987}
4988
Chris Lattnerf1b47082006-04-14 05:19:18 +00004989// If this is a case we can't handle, return null and let the default
4990// expansion code take care of it. If we CAN select this case, and if it
4991// selects to a single instruction, return Op. Otherwise, if we can codegen
4992// this case more efficiently than a constant pool load, lower it to the
4993// sequence of ops that should be used.
Dan Gohmand858e902010-04-17 15:26:15 +00004994SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
4995 SelectionDAG &DAG) const {
Dale Johannesened2eee62009-02-06 01:31:28 +00004996 DebugLoc dl = Op.getDebugLoc();
Bob Wilsona27ea9e2009-03-01 01:13:55 +00004997 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
4998 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Micheldf380432009-02-25 03:12:50 +00004999
Bob Wilson24e338e2009-03-02 23:24:16 +00005000 // Check if this is a splat of a constant value.
5001 APInt APSplatBits, APSplatUndef;
5002 unsigned SplatBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005003 bool HasAnyUndefs;
Bob Wilsonf2950b02009-03-03 19:26:27 +00005004 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen1e608812009-11-13 01:45:18 +00005005 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilsonf2950b02009-03-03 19:26:27 +00005006 return SDValue();
Evan Chenga87008d2009-02-25 22:49:59 +00005007
Bob Wilsonf2950b02009-03-03 19:26:27 +00005008 unsigned SplatBits = APSplatBits.getZExtValue();
5009 unsigned SplatUndef = APSplatUndef.getZExtValue();
5010 unsigned SplatSize = SplatBitSize / 8;
Scott Michelfdc40a02009-02-17 22:15:04 +00005011
Bob Wilsonf2950b02009-03-03 19:26:27 +00005012 // First, handle single instruction cases.
5013
5014 // All zeros?
5015 if (SplatBits == 0) {
5016 // Canonicalize all zero vectors to be v4i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005017 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5018 SDValue Z = DAG.getConstant(0, MVT::i32);
5019 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005020 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattnerf1b47082006-04-14 05:19:18 +00005021 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00005022 return Op;
5023 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00005024
Bob Wilsonf2950b02009-03-03 19:26:27 +00005025 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5026 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5027 (32-SplatBitSize));
5028 if (SextVal >= -16 && SextVal <= 15)
5029 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005030
5031
Bob Wilsonf2950b02009-03-03 19:26:27 +00005032 // Two instruction sequences.
Scott Michelfdc40a02009-02-17 22:15:04 +00005033
Bob Wilsonf2950b02009-03-03 19:26:27 +00005034 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtabc40282013-02-20 20:41:42 +00005035 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5036 // If this value is in the range [17,31] and is odd, use:
5037 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5038 // If this value is in the range [-31,-17] and is odd, use:
5039 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5040 // Note the last two are three-instruction sequences.
5041 if (SextVal >= -32 && SextVal <= 31) {
5042 // To avoid having these optimizations undone by constant folding,
5043 // we convert to a pseudo that will be expanded later into one of
5044 // the above forms.
5045 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidtb34c79e2013-02-20 15:50:31 +00005046 EVT VT = Op.getValueType();
5047 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5048 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5049 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005050 }
5051
5052 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5053 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5054 // for fneg/fabs.
5055 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5056 // Make -1 and vspltisw -1:
Owen Anderson825b72b2009-08-11 20:47:22 +00005057 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005058
5059 // Make the VSLW intrinsic, computing 0x8000_0000.
5060 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5061 OnesV, DAG, dl);
5062
5063 // xor by OnesV to invert it.
Owen Anderson825b72b2009-08-11 20:47:22 +00005064 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005065 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005066 }
5067
5068 // Check to see if this is a wide variety of vsplti*, binop self cases.
5069 static const signed char SplatCsts[] = {
5070 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5071 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5072 };
5073
5074 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5075 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5076 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5077 int i = SplatCsts[idx];
5078
5079 // Figure out what shift amount will be used by altivec if shifted by i in
5080 // this splat size.
5081 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5082
5083 // vsplti + shl self.
Richard Smith1144af32012-08-24 23:29:28 +00005084 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005085 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005086 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5087 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5088 Intrinsic::ppc_altivec_vslw
5089 };
5090 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005091 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00005092 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005093
Bob Wilsonf2950b02009-03-03 19:26:27 +00005094 // vsplti + srl self.
5095 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005096 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005097 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5098 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5099 Intrinsic::ppc_altivec_vsrw
5100 };
5101 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005102 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00005103 }
5104
Bob Wilsonf2950b02009-03-03 19:26:27 +00005105 // vsplti + sra self.
5106 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005107 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005108 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5109 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5110 Intrinsic::ppc_altivec_vsraw
5111 };
5112 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005113 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00005114 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005115
Bob Wilsonf2950b02009-03-03 19:26:27 +00005116 // vsplti + rol self.
5117 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5118 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005119 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005120 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5121 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5122 Intrinsic::ppc_altivec_vrlw
5123 };
5124 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005125 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005126 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005127
Bob Wilsonf2950b02009-03-03 19:26:27 +00005128 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith1144af32012-08-24 23:29:28 +00005129 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005130 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005131 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnerdbce85d2006-04-17 18:09:22 +00005132 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00005133 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith1144af32012-08-24 23:29:28 +00005134 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005135 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005136 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattnerf1b47082006-04-14 05:19:18 +00005137 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00005138 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith1144af32012-08-24 23:29:28 +00005139 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005140 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005141 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5142 }
5143 }
5144
Dan Gohman475871a2008-07-27 21:46:04 +00005145 return SDValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00005146}
5147
Chris Lattner59138102006-04-17 05:28:54 +00005148/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5149/// the specified operations to build the shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00005150static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00005151 SDValue RHS, SelectionDAG &DAG,
Dale Johannesened2eee62009-02-06 01:31:28 +00005152 DebugLoc dl) {
Chris Lattner59138102006-04-17 05:28:54 +00005153 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling77959322008-09-17 00:30:57 +00005154 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner59138102006-04-17 05:28:54 +00005155 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005156
Chris Lattner59138102006-04-17 05:28:54 +00005157 enum {
Chris Lattner00402c72006-05-16 04:20:24 +00005158 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
Chris Lattner59138102006-04-17 05:28:54 +00005159 OP_VMRGHW,
5160 OP_VMRGLW,
5161 OP_VSPLTISW0,
5162 OP_VSPLTISW1,
5163 OP_VSPLTISW2,
5164 OP_VSPLTISW3,
5165 OP_VSLDOI4,
5166 OP_VSLDOI8,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00005167 OP_VSLDOI12
Chris Lattner59138102006-04-17 05:28:54 +00005168 };
Scott Michelfdc40a02009-02-17 22:15:04 +00005169
Chris Lattner59138102006-04-17 05:28:54 +00005170 if (OpNum == OP_COPY) {
5171 if (LHSID == (1*9+2)*9+3) return LHS;
5172 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5173 return RHS;
5174 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005175
Dan Gohman475871a2008-07-27 21:46:04 +00005176 SDValue OpLHS, OpRHS;
Dale Johannesened2eee62009-02-06 01:31:28 +00005177 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5178 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005179
Nate Begeman9008ca62009-04-27 18:41:29 +00005180 int ShufIdxs[16];
Chris Lattner59138102006-04-17 05:28:54 +00005181 switch (OpNum) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005182 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner59138102006-04-17 05:28:54 +00005183 case OP_VMRGHW:
5184 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5185 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5186 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5187 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5188 break;
5189 case OP_VMRGLW:
5190 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5191 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5192 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5193 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5194 break;
5195 case OP_VSPLTISW0:
5196 for (unsigned i = 0; i != 16; ++i)
5197 ShufIdxs[i] = (i&3)+0;
5198 break;
5199 case OP_VSPLTISW1:
5200 for (unsigned i = 0; i != 16; ++i)
5201 ShufIdxs[i] = (i&3)+4;
5202 break;
5203 case OP_VSPLTISW2:
5204 for (unsigned i = 0; i != 16; ++i)
5205 ShufIdxs[i] = (i&3)+8;
5206 break;
5207 case OP_VSPLTISW3:
5208 for (unsigned i = 0; i != 16; ++i)
5209 ShufIdxs[i] = (i&3)+12;
5210 break;
5211 case OP_VSLDOI4:
Dale Johannesened2eee62009-02-06 01:31:28 +00005212 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005213 case OP_VSLDOI8:
Dale Johannesened2eee62009-02-06 01:31:28 +00005214 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005215 case OP_VSLDOI12:
Dale Johannesened2eee62009-02-06 01:31:28 +00005216 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005217 }
Owen Andersone50ed302009-08-10 22:56:29 +00005218 EVT VT = OpLHS.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005219 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5220 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00005221 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005222 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner59138102006-04-17 05:28:54 +00005223}
5224
Chris Lattnerf1b47082006-04-14 05:19:18 +00005225/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5226/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5227/// return the code it can be lowered into. Worst case, it can always be
5228/// lowered into a vperm.
Scott Michelfdc40a02009-02-17 22:15:04 +00005229SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005230 SelectionDAG &DAG) const {
Dale Johannesened2eee62009-02-06 01:31:28 +00005231 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005232 SDValue V1 = Op.getOperand(0);
5233 SDValue V2 = Op.getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005234 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Andersone50ed302009-08-10 22:56:29 +00005235 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005236
Chris Lattnerf1b47082006-04-14 05:19:18 +00005237 // Cases that are handled by instructions that take permute immediates
5238 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5239 // selected by the instruction selector.
5240 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005241 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5242 PPC::isSplatShuffleMask(SVOp, 2) ||
5243 PPC::isSplatShuffleMask(SVOp, 4) ||
5244 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5245 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5246 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5247 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5248 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5249 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5250 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5251 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5252 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00005253 return Op;
5254 }
5255 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005256
Chris Lattnerf1b47082006-04-14 05:19:18 +00005257 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5258 // and produce a fixed permutation. If any of these match, do not lower to
5259 // VPERM.
Nate Begeman9008ca62009-04-27 18:41:29 +00005260 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5261 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5262 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5263 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5264 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5265 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5266 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5267 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5268 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattnerf1b47082006-04-14 05:19:18 +00005269 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005270
Chris Lattner59138102006-04-17 05:28:54 +00005271 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5272 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005273 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005274
Chris Lattner59138102006-04-17 05:28:54 +00005275 unsigned PFIndexes[4];
5276 bool isFourElementShuffle = true;
5277 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5278 unsigned EltNo = 8; // Start out undef.
5279 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman9008ca62009-04-27 18:41:29 +00005280 if (PermMask[i*4+j] < 0)
Chris Lattner59138102006-04-17 05:28:54 +00005281 continue; // Undef, ignore it.
Scott Michelfdc40a02009-02-17 22:15:04 +00005282
Nate Begeman9008ca62009-04-27 18:41:29 +00005283 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner59138102006-04-17 05:28:54 +00005284 if ((ByteSource & 3) != j) {
5285 isFourElementShuffle = false;
5286 break;
5287 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005288
Chris Lattner59138102006-04-17 05:28:54 +00005289 if (EltNo == 8) {
5290 EltNo = ByteSource/4;
5291 } else if (EltNo != ByteSource/4) {
5292 isFourElementShuffle = false;
5293 break;
5294 }
5295 }
5296 PFIndexes[i] = EltNo;
5297 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005298
5299 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner59138102006-04-17 05:28:54 +00005300 // perfect shuffle vector to determine if it is cost effective to do this as
5301 // discrete instructions, or whether we should use a vperm.
5302 if (isFourElementShuffle) {
5303 // Compute the index in the perfect shuffle table.
Scott Michelfdc40a02009-02-17 22:15:04 +00005304 unsigned PFTableIndex =
Chris Lattner59138102006-04-17 05:28:54 +00005305 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelfdc40a02009-02-17 22:15:04 +00005306
Chris Lattner59138102006-04-17 05:28:54 +00005307 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5308 unsigned Cost = (PFEntry >> 30);
Scott Michelfdc40a02009-02-17 22:15:04 +00005309
Chris Lattner59138102006-04-17 05:28:54 +00005310 // Determining when to avoid vperm is tricky. Many things affect the cost
5311 // of vperm, particularly how many times the perm mask needs to be computed.
5312 // For example, if the perm mask can be hoisted out of a loop or is already
5313 // used (perhaps because there are multiple permutes with the same shuffle
5314 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5315 // the loop requires an extra register.
5316 //
5317 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelfdc40a02009-02-17 22:15:04 +00005318 // generated in 3 or fewer operations. When we have loop information
Chris Lattner59138102006-04-17 05:28:54 +00005319 // available, if this block is within a loop, we should avoid using vperm
5320 // for 3-operation perms and use a constant pool load instead.
Scott Michelfdc40a02009-02-17 22:15:04 +00005321 if (Cost < 3)
Dale Johannesened2eee62009-02-06 01:31:28 +00005322 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005323 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005324
Chris Lattnerf1b47082006-04-14 05:19:18 +00005325 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5326 // vector that will get spilled to the constant pool.
5327 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelfdc40a02009-02-17 22:15:04 +00005328
Chris Lattnerf1b47082006-04-14 05:19:18 +00005329 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5330 // that it is in input element units, not in bytes. Convert now.
Owen Andersone50ed302009-08-10 22:56:29 +00005331 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005332 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelfdc40a02009-02-17 22:15:04 +00005333
Dan Gohman475871a2008-07-27 21:46:04 +00005334 SmallVector<SDValue, 16> ResultMask;
Nate Begeman9008ca62009-04-27 18:41:29 +00005335 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5336 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00005337
Chris Lattnerf1b47082006-04-14 05:19:18 +00005338 for (unsigned j = 0; j != BytesPerElement; ++j)
5339 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson825b72b2009-08-11 20:47:22 +00005340 MVT::i32));
Chris Lattnerf1b47082006-04-14 05:19:18 +00005341 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005342
Owen Anderson825b72b2009-08-11 20:47:22 +00005343 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga87008d2009-02-25 22:49:59 +00005344 &ResultMask[0], ResultMask.size());
Dale Johannesened2eee62009-02-06 01:31:28 +00005345 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattnerf1b47082006-04-14 05:19:18 +00005346}
5347
Chris Lattner90564f22006-04-18 17:59:36 +00005348/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5349/// altivec comparison. If it is, return true and fill in Opc/isDot with
5350/// information about the intrinsic.
Dan Gohman475871a2008-07-27 21:46:04 +00005351static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner90564f22006-04-18 17:59:36 +00005352 bool &isDot) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005353 unsigned IntrinsicID =
5354 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00005355 CompareOpc = -1;
5356 isDot = false;
5357 switch (IntrinsicID) {
5358 default: return false;
5359 // Comparison predicates.
Chris Lattner1a635d62006-04-14 06:01:58 +00005360 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5361 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5362 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5363 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5364 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5365 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5366 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5367 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5368 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5369 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5370 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5371 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5372 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005373
Chris Lattner1a635d62006-04-14 06:01:58 +00005374 // Normal Comparisons.
5375 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5376 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5377 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5378 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5379 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5380 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5381 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5382 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5383 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5384 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5385 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5386 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5387 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5388 }
Chris Lattner90564f22006-04-18 17:59:36 +00005389 return true;
5390}
5391
5392/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5393/// lower, do it, otherwise return null.
Scott Michelfdc40a02009-02-17 22:15:04 +00005394SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005395 SelectionDAG &DAG) const {
Chris Lattner90564f22006-04-18 17:59:36 +00005396 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5397 // opcode number of the comparison.
Dale Johannesen3484c092009-02-05 22:07:54 +00005398 DebugLoc dl = Op.getDebugLoc();
Chris Lattner90564f22006-04-18 17:59:36 +00005399 int CompareOpc;
5400 bool isDot;
5401 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman475871a2008-07-27 21:46:04 +00005402 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelfdc40a02009-02-17 22:15:04 +00005403
Chris Lattner90564f22006-04-18 17:59:36 +00005404 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner1a635d62006-04-14 06:01:58 +00005405 if (!isDot) {
Dale Johannesen3484c092009-02-05 22:07:54 +00005406 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner149add02010-03-14 22:44:11 +00005407 Op.getOperand(1), Op.getOperand(2),
5408 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005409 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner1a635d62006-04-14 06:01:58 +00005410 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005411
Chris Lattner1a635d62006-04-14 06:01:58 +00005412 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman475871a2008-07-27 21:46:04 +00005413 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00005414 Op.getOperand(2), // LHS
5415 Op.getOperand(3), // RHS
Owen Anderson825b72b2009-08-11 20:47:22 +00005416 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00005417 };
Benjamin Kramer3853f742013-03-07 20:33:29 +00005418 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesen3484c092009-02-05 22:07:54 +00005419 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00005420
Chris Lattner1a635d62006-04-14 06:01:58 +00005421 // Now that we have the comparison, emit a copy from the CR to a GPR.
5422 // This is flagged to the above dot comparison.
Owen Anderson825b72b2009-08-11 20:47:22 +00005423 SDValue Flags = DAG.getNode(PPCISD::MFCR, dl, MVT::i32,
5424 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelfdc40a02009-02-17 22:15:04 +00005425 CompNode.getValue(1));
5426
Chris Lattner1a635d62006-04-14 06:01:58 +00005427 // Unpack the result based on how the target uses it.
5428 unsigned BitNo; // Bit # of CR6.
5429 bool InvertBit; // Invert result?
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005430 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner1a635d62006-04-14 06:01:58 +00005431 default: // Can't happen, don't crash on invalid number though.
5432 case 0: // Return the value of the EQ bit of CR6.
5433 BitNo = 0; InvertBit = false;
5434 break;
5435 case 1: // Return the inverted value of the EQ bit of CR6.
5436 BitNo = 0; InvertBit = true;
5437 break;
5438 case 2: // Return the value of the LT bit of CR6.
5439 BitNo = 2; InvertBit = false;
5440 break;
5441 case 3: // Return the inverted value of the LT bit of CR6.
5442 BitNo = 2; InvertBit = true;
5443 break;
5444 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005445
Chris Lattner1a635d62006-04-14 06:01:58 +00005446 // Shift the bit into the low position.
Owen Anderson825b72b2009-08-11 20:47:22 +00005447 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5448 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00005449 // Isolate the bit.
Owen Anderson825b72b2009-08-11 20:47:22 +00005450 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5451 DAG.getConstant(1, MVT::i32));
Scott Michelfdc40a02009-02-17 22:15:04 +00005452
Chris Lattner1a635d62006-04-14 06:01:58 +00005453 // If we are supposed to, toggle the bit.
5454 if (InvertBit)
Owen Anderson825b72b2009-08-11 20:47:22 +00005455 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5456 DAG.getConstant(1, MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00005457 return Flags;
5458}
5459
Scott Michelfdc40a02009-02-17 22:15:04 +00005460SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005461 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005462 DebugLoc dl = Op.getDebugLoc();
Chris Lattner1a635d62006-04-14 06:01:58 +00005463 // Create a stack slot that is 16-byte aligned.
5464 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00005465 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen08673d22010-05-03 22:59:34 +00005466 EVT PtrVT = getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00005467 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005468
Chris Lattner1a635d62006-04-14 06:01:58 +00005469 // Store the input value into Value#0 of the stack slot.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005470 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00005471 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00005472 false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00005473 // Load it out.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00005474 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005475 false, false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00005476}
5477
Dan Gohmand858e902010-04-17 15:26:15 +00005478SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesened2eee62009-02-06 01:31:28 +00005479 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005480 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00005481 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005482
Owen Anderson825b72b2009-08-11 20:47:22 +00005483 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
5484 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelfdc40a02009-02-17 22:15:04 +00005485
Dan Gohman475871a2008-07-27 21:46:04 +00005486 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesened2eee62009-02-06 01:31:28 +00005487 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005488
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005489 // Shrinkify inputs to v8i16.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005490 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
5491 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
5492 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelfdc40a02009-02-17 22:15:04 +00005493
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005494 // Low parts multiplied together, generating 32-bit results (we ignore the
5495 // top parts).
Dan Gohman475871a2008-07-27 21:46:04 +00005496 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson825b72b2009-08-11 20:47:22 +00005497 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00005498
Dan Gohman475871a2008-07-27 21:46:04 +00005499 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson825b72b2009-08-11 20:47:22 +00005500 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005501 // Shift the high parts up 16 bits.
Scott Michelfdc40a02009-02-17 22:15:04 +00005502 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesened2eee62009-02-06 01:31:28 +00005503 Neg16, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00005504 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
5505 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005506 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005507
Owen Anderson825b72b2009-08-11 20:47:22 +00005508 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005509
Chris Lattnercea2aa72006-04-18 04:28:57 +00005510 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesened2eee62009-02-06 01:31:28 +00005511 LHS, RHS, Zero, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00005512 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005513 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005514
Chris Lattner19a81522006-04-18 03:57:35 +00005515 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00005516 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson825b72b2009-08-11 20:47:22 +00005517 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005518 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00005519
Chris Lattner19a81522006-04-18 03:57:35 +00005520 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00005521 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson825b72b2009-08-11 20:47:22 +00005522 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005523 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00005524
Chris Lattner19a81522006-04-18 03:57:35 +00005525 // Merge the results together.
Nate Begeman9008ca62009-04-27 18:41:29 +00005526 int Ops[16];
Chris Lattner19a81522006-04-18 03:57:35 +00005527 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005528 Ops[i*2 ] = 2*i+1;
5529 Ops[i*2+1] = 2*i+1+16;
Chris Lattner19a81522006-04-18 03:57:35 +00005530 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005531 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005532 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00005533 llvm_unreachable("Unknown mul to lower!");
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005534 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00005535}
5536
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00005537/// LowerOperation - Provide custom lowering hooks for some operations.
5538///
Dan Gohmand858e902010-04-17 15:26:15 +00005539SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00005540 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005541 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00005542 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson3d90dbe2009-11-04 21:31:18 +00005543 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00005544 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackyfd42ed62012-06-04 17:36:38 +00005545 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman37efe672006-04-22 18:53:45 +00005546 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00005547 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +00005548 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
5549 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005550 case ISD::VASTART:
Dan Gohman1e93df62010-04-17 14:41:14 +00005551 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelfdc40a02009-02-17 22:15:04 +00005552
5553 case ISD::VAARG:
Dan Gohman1e93df62010-04-17 14:41:14 +00005554 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray01119992007-04-03 13:59:52 +00005555
Jim Laskeyefc7e522006-12-04 22:04:42 +00005556 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner9f0bc652007-02-25 05:34:32 +00005557 case ISD::DYNAMIC_STACKALLOC:
5558 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng54fc97d2008-04-19 01:30:48 +00005559
Chris Lattner1a635d62006-04-14 06:01:58 +00005560 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen4c9369d2009-06-04 20:53:52 +00005561 case ISD::FP_TO_UINT:
5562 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Dale Johannesen3484c092009-02-05 22:07:54 +00005563 Op.getDebugLoc());
Chris Lattner1a635d62006-04-14 06:01:58 +00005564 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00005565 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005566
Chris Lattner1a635d62006-04-14 06:01:58 +00005567 // Lower 64-bit shifts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00005568 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
5569 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
5570 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005571
Chris Lattner1a635d62006-04-14 06:01:58 +00005572 // Vector-related lowering.
5573 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5574 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5575 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5576 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00005577 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005578
Chris Lattner3fc027d2007-12-08 06:59:59 +00005579 // Frame & Return address.
5580 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005581 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00005582 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00005583}
5584
Duncan Sands1607f052008-12-01 11:39:25 +00005585void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
5586 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005587 SelectionDAG &DAG) const {
Roman Divackybdb226e2011-06-28 15:30:42 +00005588 const TargetMachine &TM = getTargetMachine();
Dale Johannesen3484c092009-02-05 22:07:54 +00005589 DebugLoc dl = N->getDebugLoc();
Chris Lattner1f873002007-11-28 18:44:47 +00005590 switch (N->getOpcode()) {
Duncan Sands57760d92008-10-28 15:00:32 +00005591 default:
Craig Topperbc219812012-02-07 02:50:20 +00005592 llvm_unreachable("Do not know how to custom type legalize this operation!");
Roman Divackybdb226e2011-06-28 15:30:42 +00005593 case ISD::VAARG: {
5594 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
5595 || TM.getSubtarget<PPCSubtarget>().isPPC64())
5596 return;
5597
5598 EVT VT = N->getValueType(0);
5599
5600 if (VT == MVT::i64) {
5601 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
5602
5603 Results.push_back(NewNode);
5604 Results.push_back(NewNode.getValue(1));
5605 }
5606 return;
5607 }
Duncan Sands1607f052008-12-01 11:39:25 +00005608 case ISD::FP_ROUND_INREG: {
Owen Anderson825b72b2009-08-11 20:47:22 +00005609 assert(N->getValueType(0) == MVT::ppcf128);
5610 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelfdc40a02009-02-17 22:15:04 +00005611 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005612 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00005613 DAG.getIntPtrConstant(0));
Dale Johannesen3484c092009-02-05 22:07:54 +00005614 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005615 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00005616 DAG.getIntPtrConstant(1));
5617
5618 // This sequence changes FPSCR to do round-to-zero, adds the two halves
5619 // of the long double, and puts FPSCR back the way it was. We do not
5620 // actually model FPSCR.
Owen Andersone50ed302009-08-10 22:56:29 +00005621 std::vector<EVT> NodeTys;
Duncan Sands1607f052008-12-01 11:39:25 +00005622 SDValue Ops[4], Result, MFFSreg, InFlag, FPreg;
5623
Owen Anderson825b72b2009-08-11 20:47:22 +00005624 NodeTys.push_back(MVT::f64); // Return register
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005625 NodeTys.push_back(MVT::Glue); // Returns a flag for later insns
Dale Johannesen3484c092009-02-05 22:07:54 +00005626 Result = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Duncan Sands1607f052008-12-01 11:39:25 +00005627 MFFSreg = Result.getValue(0);
5628 InFlag = Result.getValue(1);
5629
5630 NodeTys.clear();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005631 NodeTys.push_back(MVT::Glue); // Returns a flag
Owen Anderson825b72b2009-08-11 20:47:22 +00005632 Ops[0] = DAG.getConstant(31, MVT::i32);
Duncan Sands1607f052008-12-01 11:39:25 +00005633 Ops[1] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00005634 Result = DAG.getNode(PPCISD::MTFSB1, dl, NodeTys, Ops, 2);
Duncan Sands1607f052008-12-01 11:39:25 +00005635 InFlag = Result.getValue(0);
5636
5637 NodeTys.clear();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005638 NodeTys.push_back(MVT::Glue); // Returns a flag
Owen Anderson825b72b2009-08-11 20:47:22 +00005639 Ops[0] = DAG.getConstant(30, MVT::i32);
Duncan Sands1607f052008-12-01 11:39:25 +00005640 Ops[1] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00005641 Result = DAG.getNode(PPCISD::MTFSB0, dl, NodeTys, Ops, 2);
Duncan Sands1607f052008-12-01 11:39:25 +00005642 InFlag = Result.getValue(0);
5643
5644 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005645 NodeTys.push_back(MVT::f64); // result of add
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005646 NodeTys.push_back(MVT::Glue); // Returns a flag
Duncan Sands1607f052008-12-01 11:39:25 +00005647 Ops[0] = Lo;
5648 Ops[1] = Hi;
5649 Ops[2] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00005650 Result = DAG.getNode(PPCISD::FADDRTZ, dl, NodeTys, Ops, 3);
Duncan Sands1607f052008-12-01 11:39:25 +00005651 FPreg = Result.getValue(0);
5652 InFlag = Result.getValue(1);
5653
5654 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005655 NodeTys.push_back(MVT::f64);
5656 Ops[0] = DAG.getConstant(1, MVT::i32);
Duncan Sands1607f052008-12-01 11:39:25 +00005657 Ops[1] = MFFSreg;
5658 Ops[2] = FPreg;
5659 Ops[3] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00005660 Result = DAG.getNode(PPCISD::MTFSF, dl, NodeTys, Ops, 4);
Duncan Sands1607f052008-12-01 11:39:25 +00005661 FPreg = Result.getValue(0);
5662
5663 // We know the low half is about to be thrown away, so just use something
5664 // convenient.
Owen Anderson825b72b2009-08-11 20:47:22 +00005665 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesen3484c092009-02-05 22:07:54 +00005666 FPreg, FPreg));
Duncan Sands1607f052008-12-01 11:39:25 +00005667 return;
Duncan Sandsa7360f02008-07-19 16:26:02 +00005668 }
Duncan Sands1607f052008-12-01 11:39:25 +00005669 case ISD::FP_TO_SINT:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00005670 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands1607f052008-12-01 11:39:25 +00005671 return;
Chris Lattner1f873002007-11-28 18:44:47 +00005672 }
5673}
5674
5675
Chris Lattner1a635d62006-04-14 06:01:58 +00005676//===----------------------------------------------------------------------===//
5677// Other Lowering Code
5678//===----------------------------------------------------------------------===//
5679
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00005680MachineBasicBlock *
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005681PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00005682 bool is64bit, unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00005683 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005684 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5685
5686 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5687 MachineFunction *F = BB->getParent();
5688 MachineFunction::iterator It = BB;
5689 ++It;
5690
5691 unsigned dest = MI->getOperand(0).getReg();
5692 unsigned ptrA = MI->getOperand(1).getReg();
5693 unsigned ptrB = MI->getOperand(2).getReg();
5694 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00005695 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005696
5697 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5698 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5699 F->insert(It, loopMBB);
5700 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005701 exitMBB->splice(exitMBB->begin(), BB,
5702 llvm::next(MachineBasicBlock::iterator(MI)),
5703 BB->end());
5704 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005705
5706 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesen0e55f062008-08-29 18:29:46 +00005707 unsigned TmpReg = (!BinOpcode) ? incr :
5708 RegInfo.createVirtualRegister(
Dale Johannesena619d012008-09-02 20:30:23 +00005709 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5710 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005711
5712 // thisMBB:
5713 // ...
5714 // fallthrough --> loopMBB
5715 BB->addSuccessor(loopMBB);
5716
5717 // loopMBB:
5718 // l[wd]arx dest, ptr
5719 // add r0, dest, incr
5720 // st[wd]cx. r0, ptr
5721 // bne- loopMBB
5722 // fallthrough --> exitMBB
5723 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005724 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005725 .addReg(ptrA).addReg(ptrB);
Dale Johannesen0e55f062008-08-29 18:29:46 +00005726 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00005727 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
5728 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005729 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005730 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00005731 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005732 BB->addSuccessor(loopMBB);
5733 BB->addSuccessor(exitMBB);
5734
5735 // exitMBB:
5736 // ...
5737 BB = exitMBB;
5738 return BB;
5739}
5740
5741MachineBasicBlock *
Scott Michelfdc40a02009-02-17 22:15:04 +00005742PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesen97efa362008-08-28 17:53:09 +00005743 MachineBasicBlock *BB,
5744 bool is8bit, // operation
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00005745 unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00005746 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesen97efa362008-08-28 17:53:09 +00005747 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5748 // In 64 bit mode we have to use 64 bits for addresses, even though the
5749 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
5750 // registers without caring whether they're 32 or 64, but here we're
5751 // doing actual arithmetic on the addresses.
5752 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkela548afc2013-03-19 18:51:05 +00005753 unsigned ZeroReg = PPC::ZERO;
Dale Johannesen97efa362008-08-28 17:53:09 +00005754
5755 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5756 MachineFunction *F = BB->getParent();
5757 MachineFunction::iterator It = BB;
5758 ++It;
5759
5760 unsigned dest = MI->getOperand(0).getReg();
5761 unsigned ptrA = MI->getOperand(1).getReg();
5762 unsigned ptrB = MI->getOperand(2).getReg();
5763 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00005764 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen97efa362008-08-28 17:53:09 +00005765
5766 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5767 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5768 F->insert(It, loopMBB);
5769 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005770 exitMBB->splice(exitMBB->begin(), BB,
5771 llvm::next(MachineBasicBlock::iterator(MI)),
5772 BB->end());
5773 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen97efa362008-08-28 17:53:09 +00005774
5775 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00005776 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00005777 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5778 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen97efa362008-08-28 17:53:09 +00005779 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
5780 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
5781 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
5782 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
5783 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
5784 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
5785 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
5786 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
5787 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
5788 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen0e55f062008-08-29 18:29:46 +00005789 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00005790 unsigned Ptr1Reg;
Dale Johannesen0e55f062008-08-29 18:29:46 +00005791 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00005792
5793 // thisMBB:
5794 // ...
5795 // fallthrough --> loopMBB
5796 BB->addSuccessor(loopMBB);
5797
5798 // The 4-byte load must be aligned, while a char or short may be
5799 // anywhere in the word. Hence all this nasty bookkeeping code.
5800 // add ptr1, ptrA, ptrB [copy if ptrA==0]
5801 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00005802 // xori shift, shift1, 24 [16]
Dale Johannesen97efa362008-08-28 17:53:09 +00005803 // rlwinm ptr, ptr1, 0, 0, 29
5804 // slw incr2, incr, shift
5805 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
5806 // slw mask, mask2, shift
5807 // loopMBB:
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005808 // lwarx tmpDest, ptr
Dale Johannesen0e55f062008-08-29 18:29:46 +00005809 // add tmp, tmpDest, incr2
5810 // andc tmp2, tmpDest, mask
Dale Johannesen97efa362008-08-28 17:53:09 +00005811 // and tmp3, tmp, mask
5812 // or tmp4, tmp3, tmp2
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005813 // stwcx. tmp4, ptr
Dale Johannesen97efa362008-08-28 17:53:09 +00005814 // bne- loopMBB
5815 // fallthrough --> exitMBB
Dale Johannesen0e55f062008-08-29 18:29:46 +00005816 // srw dest, tmpDest, shift
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00005817 if (ptrA != ZeroReg) {
Dale Johannesen97efa362008-08-28 17:53:09 +00005818 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005819 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005820 .addReg(ptrA).addReg(ptrB);
5821 } else {
5822 Ptr1Reg = ptrB;
5823 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00005824 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005825 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005826 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005827 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
5828 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00005829 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005830 .addReg(Ptr1Reg).addImm(0).addImm(61);
5831 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00005832 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005833 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005834 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005835 .addReg(incr).addReg(ShiftReg);
5836 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00005837 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen97efa362008-08-28 17:53:09 +00005838 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00005839 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
5840 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesen97efa362008-08-28 17:53:09 +00005841 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00005842 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005843 .addReg(Mask2Reg).addReg(ShiftReg);
5844
5845 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005846 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00005847 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen0e55f062008-08-29 18:29:46 +00005848 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00005849 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00005850 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005851 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00005852 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005853 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005854 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005855 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005856 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Roman Divacky951cd022011-06-17 15:21:10 +00005857 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00005858 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005859 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00005860 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesen97efa362008-08-28 17:53:09 +00005861 BB->addSuccessor(loopMBB);
5862 BB->addSuccessor(exitMBB);
5863
5864 // exitMBB:
5865 // ...
5866 BB = exitMBB;
Jakob Stoklund Olesen5fcb81d2011-04-04 17:57:29 +00005867 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
5868 .addReg(ShiftReg);
Dale Johannesen97efa362008-08-28 17:53:09 +00005869 return BB;
5870}
5871
5872MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005873PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005874 MachineBasicBlock *BB) const {
Evan Chengc0f64ff2006-11-27 23:37:22 +00005875 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng53301922008-07-12 02:23:19 +00005876
5877 // To "insert" these instructions we actually have to insert their
5878 // control-flow patterns.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00005879 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005880 MachineFunction::iterator It = BB;
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00005881 ++It;
Evan Cheng53301922008-07-12 02:23:19 +00005882
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005883 MachineFunction *F = BB->getParent();
Evan Cheng53301922008-07-12 02:23:19 +00005884
Hal Finkel009f7af2012-06-22 23:10:08 +00005885 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
5886 MI->getOpcode() == PPC::SELECT_CC_I8)) {
5887 unsigned OpCode = MI->getOpcode() == PPC::SELECT_CC_I8 ?
5888 PPC::ISEL8 : PPC::ISEL;
5889 unsigned SelectPred = MI->getOperand(4).getImm();
5890 DebugLoc dl = MI->getDebugLoc();
5891
5892 // The SelectPred is ((BI << 5) | BO) for a BCC
5893 unsigned BO = SelectPred & 0xF;
5894 assert((BO == 12 || BO == 4) && "invalid predicate BO field for isel");
5895
5896 unsigned TrueOpNo, FalseOpNo;
5897 if (BO == 12) {
5898 TrueOpNo = 2;
5899 FalseOpNo = 3;
5900 } else {
5901 TrueOpNo = 3;
5902 FalseOpNo = 2;
5903 SelectPred = PPC::InvertPredicate((PPC::Predicate)SelectPred);
5904 }
5905
5906 BuildMI(*BB, MI, dl, TII->get(OpCode), MI->getOperand(0).getReg())
5907 .addReg(MI->getOperand(TrueOpNo).getReg())
5908 .addReg(MI->getOperand(FalseOpNo).getReg())
5909 .addImm(SelectPred).addReg(MI->getOperand(1).getReg());
5910 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
5911 MI->getOpcode() == PPC::SELECT_CC_I8 ||
5912 MI->getOpcode() == PPC::SELECT_CC_F4 ||
5913 MI->getOpcode() == PPC::SELECT_CC_F8 ||
5914 MI->getOpcode() == PPC::SELECT_CC_VRRC) {
5915
Evan Cheng53301922008-07-12 02:23:19 +00005916
5917 // The incoming instruction knows the destination vreg to set, the
5918 // condition code register to branch on, the true/false values to
5919 // select between, and a branch opcode to use.
5920
5921 // thisMBB:
5922 // ...
5923 // TrueVal = ...
5924 // cmpTY ccX, r1, r2
5925 // bCC copy1MBB
5926 // fallthrough --> copy0MBB
5927 MachineBasicBlock *thisMBB = BB;
5928 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5929 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
5930 unsigned SelectPred = MI->getOperand(4).getImm();
Dale Johannesen536a2f12009-02-13 02:27:39 +00005931 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00005932 F->insert(It, copy0MBB);
5933 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005934
5935 // Transfer the remainder of BB and its successor edges to sinkMBB.
5936 sinkMBB->splice(sinkMBB->begin(), BB,
5937 llvm::next(MachineBasicBlock::iterator(MI)),
5938 BB->end());
5939 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5940
Evan Cheng53301922008-07-12 02:23:19 +00005941 // Next, add the true and fallthrough blocks as its successors.
5942 BB->addSuccessor(copy0MBB);
5943 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00005944
Dan Gohman14152b42010-07-06 20:24:04 +00005945 BuildMI(BB, dl, TII->get(PPC::BCC))
5946 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
5947
Evan Cheng53301922008-07-12 02:23:19 +00005948 // copy0MBB:
5949 // %FalseValue = ...
5950 // # fallthrough to sinkMBB
5951 BB = copy0MBB;
Scott Michelfdc40a02009-02-17 22:15:04 +00005952
Evan Cheng53301922008-07-12 02:23:19 +00005953 // Update machine-CFG edges
5954 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00005955
Evan Cheng53301922008-07-12 02:23:19 +00005956 // sinkMBB:
5957 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5958 // ...
5959 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005960 BuildMI(*BB, BB->begin(), dl,
5961 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng53301922008-07-12 02:23:19 +00005962 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
5963 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5964 }
Dale Johannesen97efa362008-08-28 17:53:09 +00005965 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
5966 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
5967 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
5968 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005969 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
5970 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
5971 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
5972 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesen97efa362008-08-28 17:53:09 +00005973
5974 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
5975 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
5976 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
5977 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005978 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
5979 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
5980 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
5981 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesen97efa362008-08-28 17:53:09 +00005982
5983 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
5984 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
5985 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
5986 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005987 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
5988 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
5989 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
5990 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00005991
5992 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
5993 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
5994 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
5995 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005996 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
5997 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
5998 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
5999 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006000
6001 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesen209a4092008-09-11 02:15:03 +00006002 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesen97efa362008-08-28 17:53:09 +00006003 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesen209a4092008-09-11 02:15:03 +00006004 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006005 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesen209a4092008-09-11 02:15:03 +00006006 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006007 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesen209a4092008-09-11 02:15:03 +00006008 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006009
6010 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6011 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6012 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6013 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006014 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6015 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6016 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6017 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006018
Dale Johannesen0e55f062008-08-29 18:29:46 +00006019 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6020 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6021 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6022 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6023 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6024 BB = EmitAtomicBinary(MI, BB, false, 0);
6025 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6026 BB = EmitAtomicBinary(MI, BB, true, 0);
6027
Evan Cheng53301922008-07-12 02:23:19 +00006028 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6029 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6030 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6031
6032 unsigned dest = MI->getOperand(0).getReg();
6033 unsigned ptrA = MI->getOperand(1).getReg();
6034 unsigned ptrB = MI->getOperand(2).getReg();
6035 unsigned oldval = MI->getOperand(3).getReg();
6036 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00006037 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00006038
Dale Johannesen65e39732008-08-25 18:53:26 +00006039 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6040 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6041 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng53301922008-07-12 02:23:19 +00006042 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen65e39732008-08-25 18:53:26 +00006043 F->insert(It, loop1MBB);
6044 F->insert(It, loop2MBB);
6045 F->insert(It, midMBB);
Evan Cheng53301922008-07-12 02:23:19 +00006046 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006047 exitMBB->splice(exitMBB->begin(), BB,
6048 llvm::next(MachineBasicBlock::iterator(MI)),
6049 BB->end());
6050 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng53301922008-07-12 02:23:19 +00006051
6052 // thisMBB:
6053 // ...
6054 // fallthrough --> loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00006055 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00006056
Dale Johannesen65e39732008-08-25 18:53:26 +00006057 // loop1MBB:
Evan Cheng53301922008-07-12 02:23:19 +00006058 // l[wd]arx dest, ptr
Dale Johannesen65e39732008-08-25 18:53:26 +00006059 // cmp[wd] dest, oldval
6060 // bne- midMBB
6061 // loop2MBB:
Evan Cheng53301922008-07-12 02:23:19 +00006062 // st[wd]cx. newval, ptr
6063 // bne- loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00006064 // b exitBB
6065 // midMBB:
6066 // st[wd]cx. dest, ptr
6067 // exitBB:
6068 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006069 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng53301922008-07-12 02:23:19 +00006070 .addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006071 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng53301922008-07-12 02:23:19 +00006072 .addReg(oldval).addReg(dest);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006073 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00006074 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6075 BB->addSuccessor(loop2MBB);
6076 BB->addSuccessor(midMBB);
6077
6078 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006079 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng53301922008-07-12 02:23:19 +00006080 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006081 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00006082 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006083 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen65e39732008-08-25 18:53:26 +00006084 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00006085 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006086
Dale Johannesen65e39732008-08-25 18:53:26 +00006087 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006088 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen65e39732008-08-25 18:53:26 +00006089 .addReg(dest).addReg(ptrA).addReg(ptrB);
6090 BB->addSuccessor(exitMBB);
6091
Evan Cheng53301922008-07-12 02:23:19 +00006092 // exitMBB:
6093 // ...
6094 BB = exitMBB;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006095 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6096 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6097 // We must use 64-bit registers for addresses when targeting 64-bit,
6098 // since we're actually doing arithmetic on them. Other registers
6099 // can be 32-bit.
6100 bool is64bit = PPCSubTarget.isPPC64();
6101 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6102
6103 unsigned dest = MI->getOperand(0).getReg();
6104 unsigned ptrA = MI->getOperand(1).getReg();
6105 unsigned ptrB = MI->getOperand(2).getReg();
6106 unsigned oldval = MI->getOperand(3).getReg();
6107 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00006108 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006109
6110 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6111 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6112 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6113 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6114 F->insert(It, loop1MBB);
6115 F->insert(It, loop2MBB);
6116 F->insert(It, midMBB);
6117 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006118 exitMBB->splice(exitMBB->begin(), BB,
6119 llvm::next(MachineBasicBlock::iterator(MI)),
6120 BB->end());
6121 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006122
6123 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00006124 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00006125 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6126 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006127 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6128 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6129 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6130 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6131 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6132 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6133 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6134 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6135 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6136 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6137 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6138 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6139 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6140 unsigned Ptr1Reg;
6141 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkela548afc2013-03-19 18:51:05 +00006142 unsigned ZeroReg = PPC::ZERO;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006143 // thisMBB:
6144 // ...
6145 // fallthrough --> loopMBB
6146 BB->addSuccessor(loop1MBB);
6147
6148 // The 4-byte load must be aligned, while a char or short may be
6149 // anywhere in the word. Hence all this nasty bookkeeping code.
6150 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6151 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00006152 // xori shift, shift1, 24 [16]
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006153 // rlwinm ptr, ptr1, 0, 0, 29
6154 // slw newval2, newval, shift
6155 // slw oldval2, oldval,shift
6156 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6157 // slw mask, mask2, shift
6158 // and newval3, newval2, mask
6159 // and oldval3, oldval2, mask
6160 // loop1MBB:
6161 // lwarx tmpDest, ptr
6162 // and tmp, tmpDest, mask
6163 // cmpw tmp, oldval3
6164 // bne- midMBB
6165 // loop2MBB:
6166 // andc tmp2, tmpDest, mask
6167 // or tmp4, tmp2, newval3
6168 // stwcx. tmp4, ptr
6169 // bne- loop1MBB
6170 // b exitBB
6171 // midMBB:
6172 // stwcx. tmpDest, ptr
6173 // exitBB:
6174 // srw dest, tmpDest, shift
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006175 if (ptrA != ZeroReg) {
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006176 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006177 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006178 .addReg(ptrA).addReg(ptrB);
6179 } else {
6180 Ptr1Reg = ptrB;
6181 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00006182 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006183 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006184 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006185 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6186 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00006187 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006188 .addReg(Ptr1Reg).addImm(0).addImm(61);
6189 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00006190 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006191 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006192 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006193 .addReg(newval).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006194 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006195 .addReg(oldval).addReg(ShiftReg);
6196 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00006197 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006198 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00006199 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6200 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6201 .addReg(Mask3Reg).addImm(65535);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006202 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00006203 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006204 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006205 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006206 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006207 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006208 .addReg(OldVal2Reg).addReg(MaskReg);
6209
6210 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006211 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006212 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006213 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6214 .addReg(TmpDestReg).addReg(MaskReg);
6215 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006216 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006217 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006218 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6219 BB->addSuccessor(loop2MBB);
6220 BB->addSuccessor(midMBB);
6221
6222 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006223 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6224 .addReg(TmpDestReg).addReg(MaskReg);
6225 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6226 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6227 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006228 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006229 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006230 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006231 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006232 BB->addSuccessor(loop1MBB);
6233 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006234
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006235 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006236 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006237 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006238 BB->addSuccessor(exitMBB);
6239
6240 // exitMBB:
6241 // ...
6242 BB = exitMBB;
Jakob Stoklund Olesen5fcb81d2011-04-04 17:57:29 +00006243 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
6244 .addReg(ShiftReg);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006245 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006246 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng53301922008-07-12 02:23:19 +00006247 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006248
Dan Gohman14152b42010-07-06 20:24:04 +00006249 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006250 return BB;
6251}
6252
Chris Lattner1a635d62006-04-14 06:01:58 +00006253//===----------------------------------------------------------------------===//
6254// Target Optimization Hooks
6255//===----------------------------------------------------------------------===//
6256
Duncan Sands25cf2272008-11-24 14:53:14 +00006257SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
6258 DAGCombinerInfo &DCI) const {
Dan Gohmanf0757b02010-04-21 01:34:56 +00006259 const TargetMachine &TM = getTargetMachine();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006260 SelectionDAG &DAG = DCI.DAG;
Dale Johannesen3484c092009-02-05 22:07:54 +00006261 DebugLoc dl = N->getDebugLoc();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006262 switch (N->getOpcode()) {
6263 default: break;
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006264 case PPCISD::SHL:
6265 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00006266 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006267 return N->getOperand(0);
6268 }
6269 break;
6270 case PPCISD::SRL:
6271 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00006272 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006273 return N->getOperand(0);
6274 }
6275 break;
6276 case PPCISD::SRA:
6277 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00006278 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006279 C->isAllOnesValue()) // -1 >>s V -> -1.
6280 return N->getOperand(0);
6281 }
6282 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006283
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006284 case ISD::SINT_TO_FP:
Chris Lattnera7a58542006-06-16 17:34:12 +00006285 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00006286 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
6287 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
6288 // We allow the src/dst to be either f32/f64, but the intermediate
6289 // type must be i64.
Owen Anderson825b72b2009-08-11 20:47:22 +00006290 if (N->getOperand(0).getValueType() == MVT::i64 &&
6291 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00006292 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006293 if (Val.getValueType() == MVT::f32) {
6294 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00006295 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00006296 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006297
Owen Anderson825b72b2009-08-11 20:47:22 +00006298 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00006299 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00006300 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00006301 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00006302 if (N->getValueType(0) == MVT::f32) {
6303 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner0bd48932008-01-17 07:00:52 +00006304 DAG.getIntPtrConstant(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00006305 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00006306 }
6307 return Val;
Owen Anderson825b72b2009-08-11 20:47:22 +00006308 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00006309 // If the intermediate type is i32, we can avoid the load/store here
6310 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006311 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006312 }
6313 }
6314 break;
Chris Lattner51269842006-03-01 05:50:56 +00006315 case ISD::STORE:
6316 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
6317 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnera7a02fb2008-01-18 16:54:56 +00006318 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner51269842006-03-01 05:50:56 +00006319 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006320 N->getOperand(1).getValueType() == MVT::i32 &&
6321 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00006322 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006323 if (Val.getValueType() == MVT::f32) {
6324 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00006325 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00006326 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006327 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00006328 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00006329
Owen Anderson825b72b2009-08-11 20:47:22 +00006330 Val = DAG.getNode(PPCISD::STFIWX, dl, MVT::Other, N->getOperand(0), Val,
Chris Lattner51269842006-03-01 05:50:56 +00006331 N->getOperand(2), N->getOperand(3));
Gabor Greifba36cb52008-08-28 21:40:38 +00006332 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00006333 return Val;
6334 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006335
Chris Lattnerd9989382006-07-10 20:56:58 +00006336 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman6acaaa82009-09-25 00:57:30 +00006337 if (cast<StoreSDNode>(N)->isUnindexed() &&
6338 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006339 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006340 (N->getOperand(1).getValueType() == MVT::i32 ||
6341 N->getOperand(1).getValueType() == MVT::i16)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006342 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnerd9989382006-07-10 20:56:58 +00006343 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson825b72b2009-08-11 20:47:22 +00006344 if (BSwapOp.getValueType() == MVT::i16)
6345 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnerd9989382006-07-10 20:56:58 +00006346
Dan Gohmanc76909a2009-09-25 20:36:54 +00006347 SDValue Ops[] = {
6348 N->getOperand(0), BSwapOp, N->getOperand(2),
6349 DAG.getValueType(N->getOperand(1).getValueType())
6350 };
6351 return
6352 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
6353 Ops, array_lengthof(Ops),
6354 cast<StoreSDNode>(N)->getMemoryVT(),
6355 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnerd9989382006-07-10 20:56:58 +00006356 }
6357 break;
6358 case ISD::BSWAP:
6359 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greifba36cb52008-08-28 21:40:38 +00006360 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnerd9989382006-07-10 20:56:58 +00006361 N->getOperand(0).hasOneUse() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006362 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006363 SDValue Load = N->getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +00006364 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnerd9989382006-07-10 20:56:58 +00006365 // Create the byte-swapping load.
Dan Gohman475871a2008-07-27 21:46:04 +00006366 SDValue Ops[] = {
Evan Cheng466685d2006-10-09 20:57:25 +00006367 LD->getChain(), // Chain
6368 LD->getBasePtr(), // Ptr
Chris Lattner79e490a2006-08-11 17:18:05 +00006369 DAG.getValueType(N->getValueType(0)) // VT
6370 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00006371 SDValue BSLoad =
6372 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
6373 DAG.getVTList(MVT::i32, MVT::Other), Ops, 3,
6374 LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnerd9989382006-07-10 20:56:58 +00006375
Scott Michelfdc40a02009-02-17 22:15:04 +00006376 // If this is an i16 load, insert the truncate.
Dan Gohman475871a2008-07-27 21:46:04 +00006377 SDValue ResVal = BSLoad;
Owen Anderson825b72b2009-08-11 20:47:22 +00006378 if (N->getValueType(0) == MVT::i16)
6379 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelfdc40a02009-02-17 22:15:04 +00006380
Chris Lattnerd9989382006-07-10 20:56:58 +00006381 // First, combine the bswap away. This makes the value produced by the
6382 // load dead.
6383 DCI.CombineTo(N, ResVal);
6384
6385 // Next, combine the load away, we give it a bogus result value but a real
6386 // chain result. The result value is dead because the bswap is dead.
Gabor Greifba36cb52008-08-28 21:40:38 +00006387 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006388
Chris Lattnerd9989382006-07-10 20:56:58 +00006389 // Return N so it doesn't get rechecked!
Dan Gohman475871a2008-07-27 21:46:04 +00006390 return SDValue(N, 0);
Chris Lattnerd9989382006-07-10 20:56:58 +00006391 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006392
Chris Lattner51269842006-03-01 05:50:56 +00006393 break;
Chris Lattner4468c222006-03-31 06:02:07 +00006394 case PPCISD::VCMP: {
6395 // If a VCMPo node already exists with exactly the same operands as this
6396 // node, use its result instead of this node (VCMPo computes both a CR6 and
6397 // a normal output).
6398 //
6399 if (!N->getOperand(0).hasOneUse() &&
6400 !N->getOperand(1).hasOneUse() &&
6401 !N->getOperand(2).hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006402
Chris Lattner4468c222006-03-31 06:02:07 +00006403 // Scan all of the users of the LHS, looking for VCMPo's that match.
6404 SDNode *VCMPoNode = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00006405
Gabor Greifba36cb52008-08-28 21:40:38 +00006406 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattner4468c222006-03-31 06:02:07 +00006407 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
6408 UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00006409 if (UI->getOpcode() == PPCISD::VCMPo &&
6410 UI->getOperand(1) == N->getOperand(1) &&
6411 UI->getOperand(2) == N->getOperand(2) &&
6412 UI->getOperand(0) == N->getOperand(0)) {
6413 VCMPoNode = *UI;
Chris Lattner4468c222006-03-31 06:02:07 +00006414 break;
6415 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006416
Chris Lattner00901202006-04-18 18:28:22 +00006417 // If there is no VCMPo node, or if the flag value has a single use, don't
6418 // transform this.
6419 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
6420 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006421
6422 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner00901202006-04-18 18:28:22 +00006423 // chain, this transformation is more complex. Note that multiple things
6424 // could use the value result, which we should ignore.
6425 SDNode *FlagUser = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00006426 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner00901202006-04-18 18:28:22 +00006427 FlagUser == 0; ++UI) {
6428 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman89684502008-07-27 20:43:25 +00006429 SDNode *User = *UI;
Chris Lattner00901202006-04-18 18:28:22 +00006430 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006431 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner00901202006-04-18 18:28:22 +00006432 FlagUser = User;
6433 break;
6434 }
6435 }
6436 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006437
Chris Lattner00901202006-04-18 18:28:22 +00006438 // If the user is a MFCR instruction, we know this is safe. Otherwise we
6439 // give up for right now.
6440 if (FlagUser->getOpcode() == PPCISD::MFCR)
Dan Gohman475871a2008-07-27 21:46:04 +00006441 return SDValue(VCMPoNode, 0);
Chris Lattner4468c222006-03-31 06:02:07 +00006442 }
6443 break;
6444 }
Chris Lattner90564f22006-04-18 17:59:36 +00006445 case ISD::BR_CC: {
6446 // If this is a branch on an altivec predicate comparison, lower this so
6447 // that we don't have to do a MFCR: instead, branch directly on CR6. This
6448 // lowering is done pre-legalize, because the legalizer lowers the predicate
6449 // compare down to code that is difficult to reassemble.
6450 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00006451 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Chris Lattner90564f22006-04-18 17:59:36 +00006452 int CompareOpc;
6453 bool isDot;
Scott Michelfdc40a02009-02-17 22:15:04 +00006454
Chris Lattner90564f22006-04-18 17:59:36 +00006455 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
6456 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
6457 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
6458 assert(isDot && "Can't compare against a vector result!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006459
Chris Lattner90564f22006-04-18 17:59:36 +00006460 // If this is a comparison against something other than 0/1, then we know
6461 // that the condition is never/always true.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006462 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00006463 if (Val != 0 && Val != 1) {
6464 if (CC == ISD::SETEQ) // Cond never true, remove branch.
6465 return N->getOperand(0);
6466 // Always !=, turn it into an unconditional branch.
Owen Anderson825b72b2009-08-11 20:47:22 +00006467 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner90564f22006-04-18 17:59:36 +00006468 N->getOperand(0), N->getOperand(4));
6469 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006470
Chris Lattner90564f22006-04-18 17:59:36 +00006471 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006472
Chris Lattner90564f22006-04-18 17:59:36 +00006473 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman475871a2008-07-27 21:46:04 +00006474 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00006475 LHS.getOperand(2), // LHS of compare
6476 LHS.getOperand(3), // RHS of compare
Owen Anderson825b72b2009-08-11 20:47:22 +00006477 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00006478 };
Benjamin Kramer3853f742013-03-07 20:33:29 +00006479 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesen3484c092009-02-05 22:07:54 +00006480 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006481
Chris Lattner90564f22006-04-18 17:59:36 +00006482 // Unpack the result based on how the target uses it.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00006483 PPC::Predicate CompOpc;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006484 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner90564f22006-04-18 17:59:36 +00006485 default: // Can't happen, don't crash on invalid number though.
6486 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00006487 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner90564f22006-04-18 17:59:36 +00006488 break;
6489 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00006490 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner90564f22006-04-18 17:59:36 +00006491 break;
6492 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00006493 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner90564f22006-04-18 17:59:36 +00006494 break;
6495 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00006496 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner90564f22006-04-18 17:59:36 +00006497 break;
6498 }
6499
Owen Anderson825b72b2009-08-11 20:47:22 +00006500 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
6501 DAG.getConstant(CompOpc, MVT::i32),
6502 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner90564f22006-04-18 17:59:36 +00006503 N->getOperand(4), CompNode.getValue(1));
6504 }
6505 break;
6506 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006507 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006508
Dan Gohman475871a2008-07-27 21:46:04 +00006509 return SDValue();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006510}
6511
Chris Lattner1a635d62006-04-14 06:01:58 +00006512//===----------------------------------------------------------------------===//
6513// Inline Assembly Support
6514//===----------------------------------------------------------------------===//
6515
Dan Gohman475871a2008-07-27 21:46:04 +00006516void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Scott Michelfdc40a02009-02-17 22:15:04 +00006517 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006518 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006519 const SelectionDAG &DAG,
Chris Lattnerbbe77de2006-04-02 06:26:07 +00006520 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00006521 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerbbe77de2006-04-02 06:26:07 +00006522 switch (Op.getOpcode()) {
6523 default: break;
Chris Lattnerd9989382006-07-10 20:56:58 +00006524 case PPCISD::LBRX: {
6525 // lhbrx is known to have the top bits cleared out.
Dan Gohmanae03af22009-09-27 23:17:47 +00006526 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnerd9989382006-07-10 20:56:58 +00006527 KnownZero = 0xFFFF0000;
6528 break;
6529 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00006530 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006531 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerbbe77de2006-04-02 06:26:07 +00006532 default: break;
6533 case Intrinsic::ppc_altivec_vcmpbfp_p:
6534 case Intrinsic::ppc_altivec_vcmpeqfp_p:
6535 case Intrinsic::ppc_altivec_vcmpequb_p:
6536 case Intrinsic::ppc_altivec_vcmpequh_p:
6537 case Intrinsic::ppc_altivec_vcmpequw_p:
6538 case Intrinsic::ppc_altivec_vcmpgefp_p:
6539 case Intrinsic::ppc_altivec_vcmpgtfp_p:
6540 case Intrinsic::ppc_altivec_vcmpgtsb_p:
6541 case Intrinsic::ppc_altivec_vcmpgtsh_p:
6542 case Intrinsic::ppc_altivec_vcmpgtsw_p:
6543 case Intrinsic::ppc_altivec_vcmpgtub_p:
6544 case Intrinsic::ppc_altivec_vcmpgtuh_p:
6545 case Intrinsic::ppc_altivec_vcmpgtuw_p:
6546 KnownZero = ~1U; // All bits but the low one are known to be zero.
6547 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006548 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00006549 }
6550 }
6551}
6552
6553
Chris Lattner4234f572007-03-25 02:14:49 +00006554/// getConstraintType - Given a constraint, return the type of
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00006555/// constraint it is for this target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006556PPCTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006557PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
6558 if (Constraint.size() == 1) {
6559 switch (Constraint[0]) {
6560 default: break;
6561 case 'b':
6562 case 'r':
6563 case 'f':
6564 case 'v':
6565 case 'y':
6566 return C_RegisterClass;
Hal Finkel827b7a02012-11-05 18:18:42 +00006567 case 'Z':
6568 // FIXME: While Z does indicate a memory constraint, it specifically
6569 // indicates an r+r address (used in conjunction with the 'y' modifier
6570 // in the replacement string). Currently, we're forcing the base
6571 // register to be r0 in the asm printer (which is interpreted as zero)
6572 // and forming the complete address in the second register. This is
6573 // suboptimal.
6574 return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00006575 }
6576 }
6577 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00006578}
6579
John Thompson44ab89e2010-10-29 17:29:13 +00006580/// Examine constraint type and operand type and determine a weight value.
6581/// This object must already have been set up with the operand type
6582/// and the current alternative constraint selected.
6583TargetLowering::ConstraintWeight
6584PPCTargetLowering::getSingleConstraintMatchWeight(
6585 AsmOperandInfo &info, const char *constraint) const {
6586 ConstraintWeight weight = CW_Invalid;
6587 Value *CallOperandVal = info.CallOperandVal;
6588 // If we don't have a value, we can't do a match,
6589 // but allow it at the lowest weight.
6590 if (CallOperandVal == NULL)
6591 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006592 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00006593 // Look at the constraint type.
6594 switch (*constraint) {
6595 default:
6596 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
6597 break;
6598 case 'b':
6599 if (type->isIntegerTy())
6600 weight = CW_Register;
6601 break;
6602 case 'f':
6603 if (type->isFloatTy())
6604 weight = CW_Register;
6605 break;
6606 case 'd':
6607 if (type->isDoubleTy())
6608 weight = CW_Register;
6609 break;
6610 case 'v':
6611 if (type->isVectorTy())
6612 weight = CW_Register;
6613 break;
6614 case 'y':
6615 weight = CW_Register;
6616 break;
Hal Finkel827b7a02012-11-05 18:18:42 +00006617 case 'Z':
6618 weight = CW_Memory;
6619 break;
John Thompson44ab89e2010-10-29 17:29:13 +00006620 }
6621 return weight;
6622}
6623
Scott Michelfdc40a02009-02-17 22:15:04 +00006624std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner331d1bc2006-11-02 01:44:04 +00006625PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006626 EVT VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00006627 if (Constraint.size() == 1) {
Chris Lattner331d1bc2006-11-02 01:44:04 +00006628 // GCC RS6000 Constraint Letters
6629 switch (Constraint[0]) {
6630 case 'b': // R1-R31
Hal Finkela548afc2013-03-19 18:51:05 +00006631 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
6632 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
6633 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00006634 case 'r': // R0-R31
Owen Anderson825b72b2009-08-11 20:47:22 +00006635 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperc9099502012-04-20 06:31:50 +00006636 return std::make_pair(0U, &PPC::G8RCRegClass);
6637 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00006638 case 'f':
Ulrich Weigand78dab642012-10-29 17:49:34 +00006639 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00006640 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand78dab642012-10-29 17:49:34 +00006641 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00006642 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00006643 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006644 case 'v':
Craig Topperc9099502012-04-20 06:31:50 +00006645 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00006646 case 'y': // crrc
Craig Topperc9099502012-04-20 06:31:50 +00006647 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattnerddc787d2006-01-31 19:20:21 +00006648 }
6649 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006650
Chris Lattner331d1bc2006-11-02 01:44:04 +00006651 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerddc787d2006-01-31 19:20:21 +00006652}
Chris Lattner763317d2006-02-07 00:47:13 +00006653
Chris Lattner331d1bc2006-11-02 01:44:04 +00006654
Chris Lattner48884cd2007-08-25 00:47:38 +00006655/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesen1784d162010-06-25 21:55:36 +00006656/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher471e4222011-06-08 23:55:35 +00006657void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00006658 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +00006659 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00006660 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00006661 SDValue Result(0,0);
Eric Christopher471e4222011-06-08 23:55:35 +00006662
Eric Christopher100c8332011-06-02 23:16:42 +00006663 // Only support length 1 constraints.
6664 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00006665
Eric Christopher100c8332011-06-02 23:16:42 +00006666 char Letter = Constraint[0];
Chris Lattner763317d2006-02-07 00:47:13 +00006667 switch (Letter) {
6668 default: break;
6669 case 'I':
6670 case 'J':
6671 case 'K':
6672 case 'L':
6673 case 'M':
6674 case 'N':
6675 case 'O':
6676 case 'P': {
Chris Lattner9f5d5782007-05-15 01:31:05 +00006677 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattner48884cd2007-08-25 00:47:38 +00006678 if (!CST) return; // Must be an immediate to match.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006679 unsigned Value = CST->getZExtValue();
Chris Lattner763317d2006-02-07 00:47:13 +00006680 switch (Letter) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006681 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner763317d2006-02-07 00:47:13 +00006682 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00006683 if ((short)Value == (int)Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00006684 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00006685 break;
Chris Lattner763317d2006-02-07 00:47:13 +00006686 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
6687 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner9f5d5782007-05-15 01:31:05 +00006688 if ((short)Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00006689 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00006690 break;
Chris Lattner763317d2006-02-07 00:47:13 +00006691 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00006692 if ((Value >> 16) == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00006693 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00006694 break;
Chris Lattner763317d2006-02-07 00:47:13 +00006695 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner9f5d5782007-05-15 01:31:05 +00006696 if (Value > 31)
Chris Lattner48884cd2007-08-25 00:47:38 +00006697 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00006698 break;
Chris Lattner763317d2006-02-07 00:47:13 +00006699 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner9f5d5782007-05-15 01:31:05 +00006700 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattner48884cd2007-08-25 00:47:38 +00006701 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00006702 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006703 case 'O': // "O" is the constant zero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00006704 if (Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00006705 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00006706 break;
Chris Lattner763317d2006-02-07 00:47:13 +00006707 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00006708 if ((short)-Value == (int)-Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00006709 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00006710 break;
Chris Lattner763317d2006-02-07 00:47:13 +00006711 }
6712 break;
6713 }
6714 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006715
Gabor Greifba36cb52008-08-28 21:40:38 +00006716 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00006717 Ops.push_back(Result);
6718 return;
6719 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006720
Chris Lattner763317d2006-02-07 00:47:13 +00006721 // Handle standard constraint letters.
Eric Christopher100c8332011-06-02 23:16:42 +00006722 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner763317d2006-02-07 00:47:13 +00006723}
Evan Chengc4c62572006-03-13 23:20:37 +00006724
Chris Lattnerc9addb72007-03-30 23:15:24 +00006725// isLegalAddressingMode - Return true if the addressing mode represented
6726// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00006727bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006728 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +00006729 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelfdc40a02009-02-17 22:15:04 +00006730
Chris Lattnerc9addb72007-03-30 23:15:24 +00006731 // PPC allows a sign-extended 16-bit immediate field.
6732 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
6733 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006734
Chris Lattnerc9addb72007-03-30 23:15:24 +00006735 // No global is ever allowed as a base.
6736 if (AM.BaseGV)
6737 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006738
6739 // PPC only support r+r,
Chris Lattnerc9addb72007-03-30 23:15:24 +00006740 switch (AM.Scale) {
6741 case 0: // "r+i" or just "i", depending on HasBaseReg.
6742 break;
6743 case 1:
6744 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
6745 return false;
6746 // Otherwise we have r+r or r+i.
6747 break;
6748 case 2:
6749 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
6750 return false;
6751 // Allow 2*r as r+r.
6752 break;
Chris Lattner7c7ba9d2007-04-09 22:10:05 +00006753 default:
6754 // No other scales are supported.
6755 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00006756 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006757
Chris Lattnerc9addb72007-03-30 23:15:24 +00006758 return true;
6759}
6760
Evan Chengc4c62572006-03-13 23:20:37 +00006761/// isLegalAddressImmediate - Return true if the integer value can be used
Evan Cheng86193912007-03-12 23:29:01 +00006762/// as the offset of the target addressing mode for load / store of the
6763/// given type.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006764bool PPCTargetLowering::isLegalAddressImmediate(int64_t V,Type *Ty) const{
Evan Chengc4c62572006-03-13 23:20:37 +00006765 // PPC allows a sign-extended 16-bit immediate field.
6766 return (V > -(1 << 16) && V < (1 << 16)-1);
6767}
Reid Spencer3a9ec242006-08-28 01:02:49 +00006768
Craig Topperc89c7442012-03-27 07:21:54 +00006769bool PPCTargetLowering::isLegalAddressImmediate(GlobalValue* GV) const {
Scott Michelfdc40a02009-02-17 22:15:04 +00006770 return false;
Reid Spencer3a9ec242006-08-28 01:02:49 +00006771}
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00006772
Dan Gohmand858e902010-04-17 15:26:15 +00006773SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
6774 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00006775 MachineFunction &MF = DAG.getMachineFunction();
6776 MachineFrameInfo *MFI = MF.getFrameInfo();
6777 MFI->setReturnAddressIsTaken(true);
6778
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006779 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen08673d22010-05-03 22:59:34 +00006780 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattner3fc027d2007-12-08 06:59:59 +00006781
Dale Johannesen08673d22010-05-03 22:59:34 +00006782 // Make sure the function does not optimize away the store of the RA to
6783 // the stack.
Chris Lattner3fc027d2007-12-08 06:59:59 +00006784 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen08673d22010-05-03 22:59:34 +00006785 FuncInfo->setLRStoreRequired();
6786 bool isPPC64 = PPCSubTarget.isPPC64();
6787 bool isDarwinABI = PPCSubTarget.isDarwinABI();
6788
6789 if (Depth > 0) {
6790 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6791 SDValue Offset =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006792
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00006793 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen08673d22010-05-03 22:59:34 +00006794 isPPC64? MVT::i64 : MVT::i32);
6795 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
6796 DAG.getNode(ISD::ADD, dl, getPointerTy(),
6797 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006798 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen08673d22010-05-03 22:59:34 +00006799 }
Chris Lattner3fc027d2007-12-08 06:59:59 +00006800
Chris Lattner3fc027d2007-12-08 06:59:59 +00006801 // Just load the return address off the stack.
Dan Gohman475871a2008-07-27 21:46:04 +00006802 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen08673d22010-05-03 22:59:34 +00006803 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006804 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattner3fc027d2007-12-08 06:59:59 +00006805}
6806
Dan Gohmand858e902010-04-17 15:26:15 +00006807SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
6808 SelectionDAG &DAG) const {
Dale Johannesena05dca42009-02-04 23:02:30 +00006809 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen08673d22010-05-03 22:59:34 +00006810 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006811
Owen Andersone50ed302009-08-10 22:56:29 +00006812 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006813 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelfdc40a02009-02-17 22:15:04 +00006814
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00006815 MachineFunction &MF = DAG.getMachineFunction();
6816 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen08673d22010-05-03 22:59:34 +00006817 MFI->setFrameAddressIsTaken(true);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006818 bool is31 = (getTargetMachine().Options.DisableFramePointerElim(MF) ||
6819 MFI->hasVarSizedObjects()) &&
Dale Johannesen08673d22010-05-03 22:59:34 +00006820 MFI->getStackSize() &&
Bill Wendling831737d2012-12-30 10:32:01 +00006821 !MF.getFunction()->getAttributes().
6822 hasAttribute(AttributeSet::FunctionIndex, Attribute::Naked);
Dale Johannesen08673d22010-05-03 22:59:34 +00006823 unsigned FrameReg = isPPC64 ? (is31 ? PPC::X31 : PPC::X1) :
6824 (is31 ? PPC::R31 : PPC::R1);
6825 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
6826 PtrVT);
6827 while (Depth--)
6828 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006829 FrameAddr, MachinePointerInfo(), false, false,
6830 false, 0);
Dale Johannesen08673d22010-05-03 22:59:34 +00006831 return FrameAddr;
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00006832}
Dan Gohman54aeea32008-10-21 03:41:46 +00006833
6834bool
6835PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
6836 // The PowerPC target isn't yet aware of offsets.
6837 return false;
6838}
Tilmann Schellerffd02002009-07-03 06:45:56 +00006839
Evan Cheng42642d02010-04-01 20:10:42 +00006840/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengf28f8bc2010-04-02 19:36:14 +00006841/// and store operations as a result of memset, memcpy, and memmove
6842/// lowering. If DstAlign is zero that means it's safe to destination
6843/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
6844/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00006845/// probably because the source does not need to be loaded. If 'IsMemset' is
6846/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
6847/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
6848/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00006849/// It returns EVT::Other if the type should be determined using generic
6850/// target-independent logic.
Evan Cheng255f20f2010-04-01 06:04:33 +00006851EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
6852 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00006853 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00006854 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00006855 MachineFunction &MF) const {
Tilmann Schellerffd02002009-07-03 06:45:56 +00006856 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006857 return MVT::i64;
Tilmann Schellerffd02002009-07-03 06:45:56 +00006858 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006859 return MVT::i32;
Tilmann Schellerffd02002009-07-03 06:45:56 +00006860 }
6861}
Hal Finkel3f31d492012-04-01 19:23:08 +00006862
Hal Finkel2d37f7b2013-03-15 15:27:13 +00006863bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
6864 bool *Fast) const {
6865 if (DisablePPCUnaligned)
6866 return false;
6867
6868 // PowerPC supports unaligned memory access for simple non-vector types.
6869 // Although accessing unaligned addresses is not as efficient as accessing
6870 // aligned addresses, it is generally more efficient than manual expansion,
6871 // and generally only traps for software emulation when crossing page
6872 // boundaries.
6873
6874 if (!VT.isSimple())
6875 return false;
6876
6877 if (VT.getSimpleVT().isVector())
6878 return false;
6879
6880 if (VT == MVT::ppcf128)
6881 return false;
6882
6883 if (Fast)
6884 *Fast = true;
6885
6886 return true;
6887}
6888
Hal Finkel070b8db2012-06-22 00:49:52 +00006889/// isFMAFasterThanMulAndAdd - Return true if an FMA operation is faster than
6890/// a pair of mul and add instructions. fmuladd intrinsics will be expanded to
6891/// FMAs when this method returns true (and FMAs are legal), otherwise fmuladd
6892/// is expanded to mul + add.
6893bool PPCTargetLowering::isFMAFasterThanMulAndAdd(EVT VT) const {
6894 if (!VT.isSimple())
6895 return false;
6896
6897 switch (VT.getSimpleVT().SimpleTy) {
6898 case MVT::f32:
6899 case MVT::f64:
6900 case MVT::v4f32:
6901 return true;
6902 default:
6903 break;
6904 }
6905
6906 return false;
6907}
6908
Hal Finkel3f31d492012-04-01 19:23:08 +00006909Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel71ffcfe2012-06-10 19:32:29 +00006910 if (DisableILPPref)
6911 return TargetLowering::getSchedulingPreference(N);
Hal Finkel3f31d492012-04-01 19:23:08 +00006912
Hal Finkel71ffcfe2012-06-10 19:32:29 +00006913 return Sched::ILP;
Hal Finkel3f31d492012-04-01 19:23:08 +00006914}
6915