blob: 664dd12573a67298498ad5538a7557badb67902f [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"
Bill Schmidt240b9b62013-05-13 19:34:37 +000019#include "PPCTargetObjectFile.h"
Owen Anderson718cb662007-09-07 04:06:50 +000020#include "llvm/ADT/STLExtras.h"
Chris Lattnerb9a7bea2007-03-06 00:59:59 +000021#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000026#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000027#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000028#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/Constants.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/Intrinsics.h"
Chris Lattner4eab7142006-11-10 02:08:47 +000033#include "llvm/Support/CommandLine.h"
Torok Edwindac237e2009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
Craig Topper79aa3412012-03-17 18:46:09 +000035#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000036#include "llvm/Support/raw_ostream.h"
Craig Topper79aa3412012-03-17 18:46:09 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000038using namespace llvm;
39
Hal Finkel77838f92012-06-04 02:21:00 +000040static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
41cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattner4eab7142006-11-10 02:08:47 +000042
Hal Finkel71ffcfe2012-06-10 19:32:29 +000043static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
44cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
45
Hal Finkel2d37f7b2013-03-15 15:27:13 +000046static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
47cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
48
Chris Lattnerf0144122009-07-28 03:13:23 +000049static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) {
50 if (TM.getSubtargetImpl()->isDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +000051 return new TargetLoweringObjectFileMachO();
Bill Wendling53351a12010-03-12 02:00:43 +000052
Bill Schmidt240b9b62013-05-13 19:34:37 +000053 if (TM.getSubtargetImpl()->isSVR4ABI())
54 return new PPC64LinuxTargetObjectFile();
55
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000056 return new TargetLoweringObjectFileELF();
Chris Lattnerf0144122009-07-28 03:13:23 +000057}
58
Chris Lattner331d1bc2006-11-02 01:44:04 +000059PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000060 : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) {
Evan Cheng769951f2012-07-02 22:39:56 +000061 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
Scott Michelfdc40a02009-02-17 22:15:04 +000062
Nate Begeman405e3ec2005-10-21 00:02:42 +000063 setPow2DivIsCheap();
Dale Johannesen72324642008-07-31 18:13:12 +000064
Chris Lattnerd145a612005-09-27 22:18:25 +000065 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000066 setUseUnderscoreSetJmp(true);
67 setUseUnderscoreLongJmp(true);
Scott Michelfdc40a02009-02-17 22:15:04 +000068
Chris Lattner749dc722010-10-10 18:34:00 +000069 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
70 // arguments are at least 4/8 bytes aligned.
Evan Cheng769951f2012-07-02 22:39:56 +000071 bool isPPC64 = Subtarget->isPPC64();
72 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000073
Chris Lattner7c5a3d32005-08-16 17:14:42 +000074 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +000075 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
76 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
77 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelfdc40a02009-02-17 22:15:04 +000078
Evan Chengc5484282006-10-04 00:56:09 +000079 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson825b72b2009-08-11 20:47:22 +000080 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
81 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sandsf9c98e62008-01-23 20:39:46 +000082
Owen Anderson825b72b2009-08-11 20:47:22 +000083 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +000084
Chris Lattner94e509c2006-11-10 23:58:45 +000085 // PowerPC has pre-inc load and store's.
Owen Anderson825b72b2009-08-11 20:47:22 +000086 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
87 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
88 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
89 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
90 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
91 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
92 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
93 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
94 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
95 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Chengcd633192006-11-09 19:11:50 +000096
Dale Johannesen6eaeff22007-10-10 01:01:31 +000097 // This is used in the ppcf128->int sequence. Note it has different semantics
98 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson825b72b2009-08-11 20:47:22 +000099 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesen638ccd52007-10-06 01:24:11 +0000100
Roman Divacky0016f732012-08-16 18:19:29 +0000101 // We do not currently implement these libm ops for PowerPC.
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000102 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
103 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
104 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
105 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
106 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidtcd7a1552013-04-03 13:05:44 +0000107 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000108
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000109 // PowerPC has no SREM/UREM instructions
Owen Anderson825b72b2009-08-11 20:47:22 +0000110 setOperationAction(ISD::SREM, MVT::i32, Expand);
111 setOperationAction(ISD::UREM, MVT::i32, Expand);
112 setOperationAction(ISD::SREM, MVT::i64, Expand);
113 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman3ce990d2007-10-08 17:28:24 +0000114
115 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
117 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
118 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
119 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
120 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
121 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
122 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
123 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000124
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000125 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 setOperationAction(ISD::FSIN , MVT::f64, Expand);
127 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000128 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 setOperationAction(ISD::FREM , MVT::f64, Expand);
130 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel070b8db2012-06-22 00:49:52 +0000131 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setOperationAction(ISD::FSIN , MVT::f32, Expand);
133 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000134 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setOperationAction(ISD::FREM , MVT::f32, Expand);
136 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel070b8db2012-06-22 00:49:52 +0000137 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c5eb802008-01-18 19:55:37 +0000138
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000140
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000141 // If we're enabling GP optimizations, use hardware square root
Hal Finkel827307b2013-04-03 04:01:11 +0000142 if (!Subtarget->hasFSQRT() &&
143 !(TM.Options.UnsafeFPMath &&
144 Subtarget->hasFRSQRTE() && Subtarget->hasFRE()))
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel827307b2013-04-03 04:01:11 +0000146
147 if (!Subtarget->hasFSQRT() &&
148 !(TM.Options.UnsafeFPMath &&
149 Subtarget->hasFRSQRTES() && Subtarget->hasFRES()))
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000151
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
153 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000154
Hal Finkelf5d5c432013-03-29 08:57:48 +0000155 if (Subtarget->hasFPRND()) {
156 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
157 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
158 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
159
160 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
161 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
162 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
163
164 // frin does not implement "ties to even." Thus, this is safe only in
165 // fast-math mode.
166 if (TM.Options.UnsafeFPMath) {
167 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
168 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
Hal Finkel0882fd62013-03-29 19:41:55 +0000169
170 // These need to set FE_INEXACT, and use a custom inserter.
171 setOperationAction(ISD::FRINT, MVT::f64, Legal);
172 setOperationAction(ISD::FRINT, MVT::f32, Legal);
Hal Finkelf5d5c432013-03-29 08:57:48 +0000173 }
174 }
175
Nate Begemand88fc032006-01-14 03:14:10 +0000176 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000179 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
180 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000183 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
184 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000185
Hal Finkelc53ab4d2013-03-28 13:29:47 +0000186 if (Subtarget->hasPOPCNTD()) {
Hal Finkel1fce8832013-04-01 15:58:15 +0000187 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkelc53ab4d2013-03-28 13:29:47 +0000188 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
189 } else {
190 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
191 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
192 }
193
Nate Begeman35ef9132006-01-11 21:21:00 +0000194 // PowerPC does not have ROTR
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
196 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000197
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000198 // PowerPC does not have Select
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 setOperationAction(ISD::SELECT, MVT::i32, Expand);
200 setOperationAction(ISD::SELECT, MVT::i64, Expand);
201 setOperationAction(ISD::SELECT, MVT::f32, Expand);
202 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000203
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000204 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson825b72b2009-08-11 20:47:22 +0000205 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
206 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +0000207
Nate Begeman750ac1b2006-02-01 07:19:44 +0000208 // PowerPC wants to optimize integer setcc a bit
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000210
Nate Begeman81e80972006-03-17 01:40:33 +0000211 // PowerPC does not have BRCOND which requires SetCC
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Chengc35497f2006-10-30 08:02:39 +0000213
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000215
Chris Lattnerf7605322005-08-31 21:09:52 +0000216 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000218
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000219 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
221 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000222
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000223 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
224 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
225 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
226 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattner53e88452005-12-23 05:13:35 +0000227
Chris Lattner25b8b8c2006-04-28 21:56:10 +0000228 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000230
Hal Finkele9150472013-03-27 19:10:42 +0000231 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel7ee74a62013-03-21 21:37:52 +0000232 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
233 // support continuation, user-level threading, and etc.. As a result, no
234 // other SjLj exception interfaces are implemented and please don't build
235 // your own exception handling based on them.
236 // LLVM/Clang supports zero-cost DWARF exception handling.
237 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
238 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000239
240 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman28a6b022005-12-10 02:36:00 +0000241 // appropriate instructions to materialize the address.
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
243 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000244 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
246 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
247 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
248 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000249 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
251 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000252
Nate Begeman1db3c922008-08-11 17:36:31 +0000253 // TRAP is legal.
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling77959322008-09-17 00:30:57 +0000255
256 // TRAMPOLINE is custom lowered.
Duncan Sands4a544a72011-09-06 13:37:06 +0000257 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
258 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling77959322008-09-17 00:30:57 +0000259
Nate Begemanacc398c2006-01-25 18:21:52 +0000260 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000262
Evan Cheng769951f2012-07-02 22:39:56 +0000263 if (Subtarget->isSVR4ABI()) {
264 if (isPPC64) {
Hal Finkel179a4dd2012-03-24 03:53:55 +0000265 // VAARG always uses double-word chunks, so promote anything smaller.
266 setOperationAction(ISD::VAARG, MVT::i1, Promote);
267 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
268 setOperationAction(ISD::VAARG, MVT::i8, Promote);
269 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
270 setOperationAction(ISD::VAARG, MVT::i16, Promote);
271 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
272 setOperationAction(ISD::VAARG, MVT::i32, Promote);
273 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
274 setOperationAction(ISD::VAARG, MVT::Other, Expand);
275 } else {
276 // VAARG is custom lowered with the 32-bit SVR4 ABI.
277 setOperationAction(ISD::VAARG, MVT::Other, Custom);
278 setOperationAction(ISD::VAARG, MVT::i64, Custom);
279 }
Roman Divackybdb226e2011-06-28 15:30:42 +0000280 } else
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000282
Roman Divacky6ebf55d2013-07-25 21:36:47 +0000283 if (Subtarget->isSVR4ABI() && !isPPC64)
284 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
285 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
286 else
287 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
288
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000289 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::VAEND , MVT::Other, Expand);
291 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
292 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
293 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
294 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattner56a752e2006-10-18 01:18:48 +0000295
Chris Lattner6d92cad2006-03-26 10:06:40 +0000296 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000298
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000299 // To handle counter-based loop conditions.
300 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
301
Dale Johannesen53e4e442008-11-07 22:54:33 +0000302 // Comparisons that require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
304 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
305 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
306 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
307 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
308 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
309 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
310 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
311 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
312 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
313 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
314 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000315
Evan Cheng769951f2012-07-02 22:39:56 +0000316 if (Subtarget->has64BitSupport()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000317 // They also have instructions for converting between i64 and fp.
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
319 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
320 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
321 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen4c9369d2009-06-04 20:53:52 +0000322 // This is just the low 32 bits of a (signed) fp->i64 conversion.
323 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000325
Hal Finkel46479192013-04-01 17:52:07 +0000326 if (PPCSubTarget.hasLFIWAX() || Subtarget->isPPC64())
Hal Finkel9ad0f492013-03-31 01:58:02 +0000327 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000328 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000329 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000331 }
332
Hal Finkel46479192013-04-01 17:52:07 +0000333 // With the instructions enabled under FPCVT, we can do everything.
334 if (PPCSubTarget.hasFPCVT()) {
335 if (Subtarget->has64BitSupport()) {
336 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
337 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
338 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
339 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
340 }
341
342 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
343 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
344 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
345 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
346 }
347
Evan Cheng769951f2012-07-02 22:39:56 +0000348 if (Subtarget->use64BitRegs()) {
Chris Lattner26cb2862007-10-19 04:08:28 +0000349 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperc9099502012-04-20 06:31:50 +0000350 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000351 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson825b72b2009-08-11 20:47:22 +0000352 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman9ed06db2008-03-07 20:36:53 +0000353 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
355 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
356 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000357 } else {
Chris Lattner26cb2862007-10-19 04:08:28 +0000358 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
360 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
361 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000362 }
Evan Chengd30bf012006-03-01 01:11:20 +0000363
Evan Cheng769951f2012-07-02 22:39:56 +0000364 if (Subtarget->hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000365 // First set operation action for all vector types to expand. Then we
366 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
368 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
369 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000370
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000371 // add/sub are legal for all supported vector VT's.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000372 setOperationAction(ISD::ADD , VT, Legal);
373 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000374
Chris Lattner7ff7e672006-04-04 17:25:31 +0000375 // We promote all shuffles to v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000376 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000378
379 // We promote all non-typed operations to v4i32.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000380 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000382 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000384 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000386 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000388 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000390 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000391 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +0000392
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000393 // No other operations are legal.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000394 setOperationAction(ISD::MUL , VT, Expand);
395 setOperationAction(ISD::SDIV, VT, Expand);
396 setOperationAction(ISD::SREM, VT, Expand);
397 setOperationAction(ISD::UDIV, VT, Expand);
398 setOperationAction(ISD::UREM, VT, Expand);
399 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkelad3b34d2013-07-08 17:30:25 +0000400 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000401 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topper44e394c2012-11-15 08:02:19 +0000402 setOperationAction(ISD::FSQRT, VT, Expand);
403 setOperationAction(ISD::FLOG, VT, Expand);
404 setOperationAction(ISD::FLOG10, VT, Expand);
405 setOperationAction(ISD::FLOG2, VT, Expand);
406 setOperationAction(ISD::FEXP, VT, Expand);
407 setOperationAction(ISD::FEXP2, VT, Expand);
408 setOperationAction(ISD::FSIN, VT, Expand);
409 setOperationAction(ISD::FCOS, VT, Expand);
410 setOperationAction(ISD::FABS, VT, Expand);
411 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topper1ab489a2012-11-14 08:11:25 +0000412 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000413 setOperationAction(ISD::FCEIL, VT, Expand);
414 setOperationAction(ISD::FTRUNC, VT, Expand);
415 setOperationAction(ISD::FRINT, VT, Expand);
416 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000417 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
418 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
419 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
420 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
421 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
422 setOperationAction(ISD::UDIVREM, VT, Expand);
423 setOperationAction(ISD::SDIVREM, VT, Expand);
424 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
425 setOperationAction(ISD::FPOW, VT, Expand);
426 setOperationAction(ISD::CTPOP, VT, Expand);
427 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000428 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000429 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000430 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramer91223a42012-12-19 15:49:14 +0000431 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellacfe09ed2012-11-05 17:15:56 +0000432 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
433
434 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
435 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
436 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
437 setTruncStoreAction(VT, InnerVT, Expand);
438 }
439 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
440 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
441 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000442 }
443
Chris Lattner7ff7e672006-04-04 17:25:31 +0000444 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
445 // with merges, splats, etc.
Owen Anderson825b72b2009-08-11 20:47:22 +0000446 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000447
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::AND , MVT::v4i32, Legal);
449 setOperationAction(ISD::OR , MVT::v4i32, Legal);
450 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
451 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
452 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
453 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella51aaadb2012-10-08 17:27:24 +0000454 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
455 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
456 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
457 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellae95ed2b2012-11-15 20:56:03 +0000458 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
459 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
460 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
461 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000462
Craig Topperc9099502012-04-20 06:31:50 +0000463 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
464 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
465 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
466 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelfdc40a02009-02-17 22:15:04 +0000467
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel070b8db2012-06-22 00:49:52 +0000469 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel827307b2013-04-03 04:01:11 +0000470
471 if (TM.Options.UnsafeFPMath) {
472 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
473 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
474 }
475
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
477 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
478 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000479
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
481 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000482
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
484 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
485 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
486 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella5f41fd62012-10-30 13:50:19 +0000487
488 // Altivec does not contain unordered floating-point compare instructions
489 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
490 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
491 setCondCodeAction(ISD::SETUGT, MVT::v4f32, Expand);
492 setCondCodeAction(ISD::SETUGE, MVT::v4f32, Expand);
493 setCondCodeAction(ISD::SETULT, MVT::v4f32, Expand);
494 setCondCodeAction(ISD::SETULE, MVT::v4f32, Expand);
Hal Finkel947d4472013-07-08 20:00:03 +0000495
496 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
497 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Nate Begeman425a9692005-11-29 08:17:20 +0000498 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000499
Hal Finkel8cc34742012-08-04 14:10:46 +0000500 if (Subtarget->has64BitSupport()) {
Hal Finkel19aa2b52012-04-01 20:08:17 +0000501 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel8cc34742012-08-04 14:10:46 +0000502 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
503 }
Hal Finkel19aa2b52012-04-01 20:08:17 +0000504
Eli Friedman4db5aca2011-08-29 18:23:02 +0000505 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
506 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkelcd9ea512012-12-25 17:22:53 +0000507 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
508 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000509
Duncan Sands03228082008-11-23 15:47:28 +0000510 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidtfa799112013-04-23 18:49:44 +0000511 // Altivec instructions set fields to all zeros or all ones.
512 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelfdc40a02009-02-17 22:15:04 +0000513
Evan Cheng769951f2012-07-02 22:39:56 +0000514 if (isPPC64) {
Chris Lattner10da9572006-10-18 01:20:43 +0000515 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000516 setExceptionPointerRegister(PPC::X3);
517 setExceptionSelectorRegister(PPC::X4);
518 } else {
Chris Lattner10da9572006-10-18 01:20:43 +0000519 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000520 setExceptionPointerRegister(PPC::R3);
521 setExceptionSelectorRegister(PPC::R4);
522 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000523
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000524 // We have target-specific dag combine patterns for the following nodes:
525 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkel80d10de2013-05-24 23:00:14 +0000526 setTargetDAGCombine(ISD::LOAD);
Chris Lattner51269842006-03-01 05:50:56 +0000527 setTargetDAGCombine(ISD::STORE);
Chris Lattner90564f22006-04-18 17:59:36 +0000528 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnerd9989382006-07-10 20:56:58 +0000529 setTargetDAGCombine(ISD::BSWAP);
Hal Finkel5a0e6042013-05-25 04:05:05 +0000530 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Scott Michelfdc40a02009-02-17 22:15:04 +0000531
Hal Finkel827307b2013-04-03 04:01:11 +0000532 // Use reciprocal estimates.
533 if (TM.Options.UnsafeFPMath) {
534 setTargetDAGCombine(ISD::FDIV);
535 setTargetDAGCombine(ISD::FSQRT);
536 }
537
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000538 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng769951f2012-07-02 22:39:56 +0000539 if (Subtarget->isDarwin()) {
Duncan Sands007f9842008-01-10 10:28:30 +0000540 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000541 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
542 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands007f9842008-01-10 10:28:30 +0000543 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
544 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000545 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
546 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
547 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
548 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
549 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000550 }
551
Hal Finkelc6129162011-10-17 18:53:03 +0000552 setMinFunctionAlignment(2);
553 if (PPCSubTarget.isDarwin())
554 setPrefFunctionAlignment(4);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000555
Evan Cheng769951f2012-07-02 22:39:56 +0000556 if (isPPC64 && Subtarget->isJITCodeModel())
557 // Temporary workaround for the inability of PPC64 JIT to handle jump
558 // tables.
559 setSupportJumpTables(false);
560
Eli Friedman26689ac2011-08-03 21:06:02 +0000561 setInsertFencesForAtomic(true);
562
Hal Finkel768c65f2011-11-22 16:21:04 +0000563 setSchedulingPreference(Sched::Hybrid);
564
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000565 computeRegisterProperties();
Hal Finkel621b77a2012-08-28 16:12:39 +0000566
567 // The Freescale cores does better with aggressive inlining of memcpy and
568 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
569 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
570 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach3450f802013-02-20 21:13:59 +0000571 MaxStoresPerMemset = 32;
572 MaxStoresPerMemsetOptSize = 16;
573 MaxStoresPerMemcpy = 32;
574 MaxStoresPerMemcpyOptSize = 8;
575 MaxStoresPerMemmove = 32;
576 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel621b77a2012-08-28 16:12:39 +0000577
578 setPrefFunctionAlignment(4);
Hal Finkel621b77a2012-08-28 16:12:39 +0000579 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000580}
581
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000582/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
583/// function arguments in the caller parameter area.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000584unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dan Gohmanf0757b02010-04-21 01:34:56 +0000585 const TargetMachine &TM = getTargetMachine();
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000586 // Darwin passes everything on 4 byte boundary.
587 if (TM.getSubtarget<PPCSubtarget>().isDarwin())
588 return 4;
Roman Divacky466958c2012-04-02 15:49:30 +0000589
590 // 16byte and wider vectors are passed on 16byte boundary.
591 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
592 if (VTy->getBitWidth() >= 128)
593 return 16;
594
595 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
596 if (PPCSubTarget.isPPC64())
597 return 8;
598
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000599 return 4;
600}
601
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000602const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
603 switch (Opcode) {
604 default: return 0;
Evan Cheng53301922008-07-12 02:23:19 +0000605 case PPCISD::FSEL: return "PPCISD::FSEL";
606 case PPCISD::FCFID: return "PPCISD::FCFID";
607 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
608 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel827307b2013-04-03 04:01:11 +0000609 case PPCISD::FRE: return "PPCISD::FRE";
610 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng53301922008-07-12 02:23:19 +0000611 case PPCISD::STFIWX: return "PPCISD::STFIWX";
612 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
613 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
614 case PPCISD::VPERM: return "PPCISD::VPERM";
615 case PPCISD::Hi: return "PPCISD::Hi";
616 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000617 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller3a84dae2009-12-18 13:00:15 +0000618 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
619 case PPCISD::LOAD: return "PPCISD::LOAD";
620 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng53301922008-07-12 02:23:19 +0000621 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
622 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
623 case PPCISD::SRL: return "PPCISD::SRL";
624 case PPCISD::SRA: return "PPCISD::SRA";
625 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigand86765fb2013-03-22 15:24:13 +0000626 case PPCISD::CALL: return "PPCISD::CALL";
627 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng53301922008-07-12 02:23:19 +0000628 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigand86765fb2013-03-22 15:24:13 +0000629 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng53301922008-07-12 02:23:19 +0000630 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel7ee74a62013-03-21 21:37:52 +0000631 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
632 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigand965b20e2013-07-03 17:05:42 +0000633 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng53301922008-07-12 02:23:19 +0000634 case PPCISD::VCMP: return "PPCISD::VCMP";
635 case PPCISD::VCMPo: return "PPCISD::VCMPo";
636 case PPCISD::LBRX: return "PPCISD::LBRX";
637 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng53301922008-07-12 02:23:19 +0000638 case PPCISD::LARX: return "PPCISD::LARX";
639 case PPCISD::STCX: return "PPCISD::STCX";
640 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000641 case PPCISD::BDNZ: return "PPCISD::BDNZ";
642 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng53301922008-07-12 02:23:19 +0000643 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng53301922008-07-12 02:23:19 +0000644 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng53301922008-07-12 02:23:19 +0000645 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel82b38212012-08-28 02:10:27 +0000646 case PPCISD::CR6SET: return "PPCISD::CR6SET";
647 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000648 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
649 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
650 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Bill Schmidtb453e162012-12-14 17:02:38 +0000651 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
652 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtd7802bf2012-12-04 16:18:08 +0000653 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidt57ac1f42012-12-11 20:30:11 +0000654 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
655 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
656 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt349c2782012-12-12 19:29:35 +0000657 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
658 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
659 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
660 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
661 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidtb34c79e2013-02-20 15:50:31 +0000662 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidt5bbdb192013-05-14 19:35:45 +0000663 case PPCISD::SC: return "PPCISD::SC";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000664 }
665}
666
Matt Arsenault225ed702013-05-18 00:21:46 +0000667EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanella1c7d69b2012-10-08 18:59:53 +0000668 if (!VT.isVector())
669 return MVT::i32;
670 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000671}
672
Chris Lattner1a635d62006-04-14 06:01:58 +0000673//===----------------------------------------------------------------------===//
674// Node matching predicates, for use by the tblgen matching code.
675//===----------------------------------------------------------------------===//
676
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000677/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman475871a2008-07-27 21:46:04 +0000678static bool isFloatingPointZero(SDValue Op) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000679 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000680 return CFP->getValueAPF().isZero();
Gabor Greifba36cb52008-08-28 21:40:38 +0000681 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000682 // Maybe this has already been legalized into the constant pool?
683 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohman46510a72010-04-15 01:51:59 +0000684 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000685 return CFP->getValueAPF().isZero();
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000686 }
687 return false;
688}
689
Chris Lattnerddb739e2006-04-06 17:23:16 +0000690/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
691/// true if Op is undef or if it matches the specified value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000692static bool isConstantOrUndef(int Op, int Val) {
693 return Op < 0 || Op == Val;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000694}
695
696/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
697/// VPKUHUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000698bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000699 if (!isUnary) {
700 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000701 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000702 return false;
703 } else {
704 for (unsigned i = 0; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000705 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
706 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000707 return false;
708 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000709 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000710}
711
712/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
713/// VPKUWUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000714bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000715 if (!isUnary) {
716 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000717 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
718 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000719 return false;
720 } else {
721 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000722 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
723 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
724 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
725 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000726 return false;
727 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000728 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000729}
730
Chris Lattnercaad1632006-04-06 22:02:42 +0000731/// isVMerge - Common function, used to match vmrg* shuffles.
732///
Nate Begeman9008ca62009-04-27 18:41:29 +0000733static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnercaad1632006-04-06 22:02:42 +0000734 unsigned LHSStart, unsigned RHSStart) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000735 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman9008ca62009-04-27 18:41:29 +0000736 "PPC only supports shuffles by bytes!");
Chris Lattner116cc482006-04-06 21:11:54 +0000737 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
738 "Unsupported merge size!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000739
Chris Lattner116cc482006-04-06 21:11:54 +0000740 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
741 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman9008ca62009-04-27 18:41:29 +0000742 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000743 LHSStart+j+i*UnitSize) ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000744 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000745 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000746 return false;
747 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000748 return true;
Chris Lattnercaad1632006-04-06 22:02:42 +0000749}
750
751/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
752/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000753bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman9008ca62009-04-27 18:41:29 +0000754 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000755 if (!isUnary)
756 return isVMerge(N, UnitSize, 8, 24);
757 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000758}
759
760/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
761/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000762bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman9008ca62009-04-27 18:41:29 +0000763 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000764 if (!isUnary)
765 return isVMerge(N, UnitSize, 0, 16);
766 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000767}
768
769
Chris Lattnerd0608e12006-04-06 18:26:28 +0000770/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
771/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000772int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000773 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman9008ca62009-04-27 18:41:29 +0000774 "PPC only supports shuffles by bytes!");
775
776 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000777
Chris Lattnerd0608e12006-04-06 18:26:28 +0000778 // Find the first non-undef value in the shuffle mask.
779 unsigned i;
Nate Begeman9008ca62009-04-27 18:41:29 +0000780 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattnerd0608e12006-04-06 18:26:28 +0000781 /*search*/;
Scott Michelfdc40a02009-02-17 22:15:04 +0000782
Chris Lattnerd0608e12006-04-06 18:26:28 +0000783 if (i == 16) return -1; // all undef.
Scott Michelfdc40a02009-02-17 22:15:04 +0000784
Nate Begeman9008ca62009-04-27 18:41:29 +0000785 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattnerd0608e12006-04-06 18:26:28 +0000786 // numbered from this value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000787 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattnerd0608e12006-04-06 18:26:28 +0000788 if (ShiftAmt < i) return -1;
789 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000790
Chris Lattnerf24380e2006-04-06 22:28:36 +0000791 if (!isUnary) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000792 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000793 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000794 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000795 return -1;
796 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000797 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000798 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000799 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000800 return -1;
801 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000802 return ShiftAmt;
803}
Chris Lattneref819f82006-03-20 06:33:01 +0000804
805/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
806/// specifies a splat of a single element that is suitable for input to
807/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman9008ca62009-04-27 18:41:29 +0000808bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner7ff7e672006-04-04 17:25:31 +0000810 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelfdc40a02009-02-17 22:15:04 +0000811
Chris Lattner88a99ef2006-03-20 06:37:44 +0000812 // This is a splat operation if each element of the permute is the same, and
813 // if the value doesn't reference the second vector.
Nate Begeman9008ca62009-04-27 18:41:29 +0000814 unsigned ElementBase = N->getMaskElt(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000815
Nate Begeman9008ca62009-04-27 18:41:29 +0000816 // FIXME: Handle UNDEF elements too!
817 if (ElementBase >= 16)
Chris Lattner7ff7e672006-04-04 17:25:31 +0000818 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000819
Nate Begeman9008ca62009-04-27 18:41:29 +0000820 // Check that the indices are consecutive, in the case of a multi-byte element
821 // splatted with a v16i8 mask.
822 for (unsigned i = 1; i != EltSize; ++i)
823 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000824 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000825
Chris Lattner7ff7e672006-04-04 17:25:31 +0000826 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000827 if (N->getMaskElt(i) < 0) continue;
Chris Lattner7ff7e672006-04-04 17:25:31 +0000828 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman9008ca62009-04-27 18:41:29 +0000829 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000830 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000831 }
Chris Lattner7ff7e672006-04-04 17:25:31 +0000832 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000833}
834
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000835/// isAllNegativeZeroVector - Returns true if all elements of build_vector
836/// are -0.0.
837bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000838 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
839
840 APInt APVal, APUndef;
841 unsigned BitSize;
842 bool HasAnyUndefs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000843
Dale Johannesen1e608812009-11-13 01:45:18 +0000844 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman9008ca62009-04-27 18:41:29 +0000845 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000846 return CFP->getValueAPF().isNegZero();
Nate Begeman9008ca62009-04-27 18:41:29 +0000847
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000848 return false;
849}
850
Chris Lattneref819f82006-03-20 06:33:01 +0000851/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
852/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000853unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000854 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
855 assert(isSplatShuffleMask(SVOp, EltSize));
856 return SVOp->getMaskElt(0) / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000857}
858
Chris Lattnere87192a2006-04-12 17:37:20 +0000859/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000860/// by using a vspltis[bhw] instruction of the specified element size, return
861/// the constant being splatted. The ByteSize field indicates the number of
862/// bytes of each element [124] -> [bhw].
Dan Gohman475871a2008-07-27 21:46:04 +0000863SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
864 SDValue OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000865
866 // If ByteSize of the splat is bigger than the element size of the
867 // build_vector, then we have a case where we are checking for a splat where
868 // multiple elements of the buildvector are folded together into a single
869 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
870 unsigned EltSize = 16/N->getNumOperands();
871 if (EltSize < ByteSize) {
872 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman475871a2008-07-27 21:46:04 +0000873 SDValue UniquedVals[4];
Chris Lattner79d9a882006-04-08 07:14:26 +0000874 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelfdc40a02009-02-17 22:15:04 +0000875
Chris Lattner79d9a882006-04-08 07:14:26 +0000876 // See if all of the elements in the buildvector agree across.
877 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
878 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
879 // If the element isn't a constant, bail fully out.
Dan Gohman475871a2008-07-27 21:46:04 +0000880 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000881
Scott Michelfdc40a02009-02-17 22:15:04 +0000882
Gabor Greifba36cb52008-08-28 21:40:38 +0000883 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattner79d9a882006-04-08 07:14:26 +0000884 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
885 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000886 return SDValue(); // no match.
Chris Lattner79d9a882006-04-08 07:14:26 +0000887 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000888
Chris Lattner79d9a882006-04-08 07:14:26 +0000889 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
890 // either constant or undef values that are identical for each chunk. See
891 // if these chunks can form into a larger vspltis*.
Scott Michelfdc40a02009-02-17 22:15:04 +0000892
Chris Lattner79d9a882006-04-08 07:14:26 +0000893 // Check to see if all of the leading entries are either 0 or -1. If
894 // neither, then this won't fit into the immediate field.
895 bool LeadingZero = true;
896 bool LeadingOnes = true;
897 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000898 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelfdc40a02009-02-17 22:15:04 +0000899
Chris Lattner79d9a882006-04-08 07:14:26 +0000900 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
901 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
902 }
903 // Finally, check the least significant entry.
904 if (LeadingZero) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000905 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +0000906 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000907 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000908 if (Val < 16)
Owen Anderson825b72b2009-08-11 20:47:22 +0000909 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattner79d9a882006-04-08 07:14:26 +0000910 }
911 if (LeadingOnes) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000912 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman7810bfe2008-09-26 21:54:37 +0000914 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000915 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattner79d9a882006-04-08 07:14:26 +0000917 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000918
Dan Gohman475871a2008-07-27 21:46:04 +0000919 return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000920 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000921
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000922 // Check to see if this buildvec has a single non-undef value in its elements.
923 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
924 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greifba36cb52008-08-28 21:40:38 +0000925 if (OpVal.getNode() == 0)
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000926 OpVal = N->getOperand(i);
927 else if (OpVal != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000928 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000929 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000930
Gabor Greifba36cb52008-08-28 21:40:38 +0000931 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelfdc40a02009-02-17 22:15:04 +0000932
Eli Friedman1a8229b2009-05-24 02:03:36 +0000933 unsigned ValSizeInBytes = EltSize;
Nate Begeman98e70cc2006-03-28 04:15:58 +0000934 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000935 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000936 Value = CN->getZExtValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000937 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johanneseneaf08942007-08-31 04:03:46 +0000939 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000940 }
941
942 // If the splat value is larger than the element value, then we can never do
943 // this splat. The only case that we could fit the replicated bits into our
944 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman475871a2008-07-27 21:46:04 +0000945 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +0000946
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000947 // If the element value is larger than the splat value, cut it in half and
948 // check to see if the two halves are equal. Continue doing this until we
949 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
950 while (ValSizeInBytes > ByteSize) {
951 ValSizeInBytes >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000952
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000953 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000954 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
955 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman475871a2008-07-27 21:46:04 +0000956 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000957 }
958
959 // Properly sign extend the value.
Richard Smith1144af32012-08-24 23:29:28 +0000960 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelfdc40a02009-02-17 22:15:04 +0000961
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000962 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman475871a2008-07-27 21:46:04 +0000963 if (MaskVal == 0) return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000964
Chris Lattner140a58f2006-04-08 06:46:53 +0000965 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith1144af32012-08-24 23:29:28 +0000966 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson825b72b2009-08-11 20:47:22 +0000967 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000968 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000969}
970
Chris Lattner1a635d62006-04-14 06:01:58 +0000971//===----------------------------------------------------------------------===//
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000972// Addressing Mode Selection
973//===----------------------------------------------------------------------===//
974
975/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
976/// or 64-bit immediate, and if the value can be accurately represented as a
977/// sign extension from a 16-bit value. If so, this returns true and the
978/// immediate.
979static bool isIntS16Immediate(SDNode *N, short &Imm) {
980 if (N->getOpcode() != ISD::Constant)
981 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000982
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000983 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 if (N->getValueType(0) == MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000985 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000986 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000987 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000988}
Dan Gohman475871a2008-07-27 21:46:04 +0000989static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000990 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000991}
992
993
994/// SelectAddressRegReg - Given the specified addressed, check to see if it
995/// can be represented as an indexed [r+r] operation. Returns false if it
996/// can be more efficiently represented with [r+imm].
Dan Gohman475871a2008-07-27 21:46:04 +0000997bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
998 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +0000999 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001000 short imm = 0;
1001 if (N.getOpcode() == ISD::ADD) {
1002 if (isIntS16Immediate(N.getOperand(1), imm))
1003 return false; // r+i
1004 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1005 return false; // r+i
Scott Michelfdc40a02009-02-17 22:15:04 +00001006
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001007 Base = N.getOperand(0);
1008 Index = N.getOperand(1);
1009 return true;
1010 } else if (N.getOpcode() == ISD::OR) {
1011 if (isIntS16Immediate(N.getOperand(1), imm))
1012 return false; // r+i can fold it if we can.
Scott Michelfdc40a02009-02-17 22:15:04 +00001013
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001014 // If this is an or of disjoint bitfields, we can codegen this as an add
1015 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1016 // disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001017 APInt LHSKnownZero, LHSKnownOne;
1018 APInt RHSKnownZero, RHSKnownOne;
1019 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001020 LHSKnownZero, LHSKnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001021
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001022 if (LHSKnownZero.getBoolValue()) {
1023 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001024 RHSKnownZero, RHSKnownOne);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001025 // If all of the bits are known zero on the LHS or RHS, the add won't
1026 // carry.
Dan Gohmanec59b952008-02-27 21:12:32 +00001027 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001028 Base = N.getOperand(0);
1029 Index = N.getOperand(1);
1030 return true;
1031 }
1032 }
1033 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001034
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001035 return false;
1036}
1037
Hal Finkelfa559692013-07-09 06:34:51 +00001038// If we happen to be doing an i64 load or store into a stack slot that has
1039// less than a 4-byte alignment, then the frame-index elimination may need to
1040// use an indexed load or store instruction (because the offset may not be a
1041// multiple of 4). The extra register needed to hold the offset comes from the
1042// register scavenger, and it is possible that the scavenger will need to use
1043// an emergency spill slot. As a result, we need to make sure that a spill slot
1044// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1045// stack slot.
1046static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1047 // FIXME: This does not handle the LWA case.
1048 if (VT != MVT::i64)
1049 return;
1050
Hal Finkele355d852013-07-10 15:29:01 +00001051 // NOTE: We'll exclude negative FIs here, which come from argument
1052 // lowering, because there are no known test cases triggering this problem
1053 // using packed structures (or similar). We can remove this exclusion if
1054 // we find such a test case. The reason why this is so test-case driven is
1055 // because this entire 'fixup' is only to prevent crashes (from the
1056 // register scavenger) on not-really-valid inputs. For example, if we have:
1057 // %a = alloca i1
1058 // %b = bitcast i1* %a to i64*
1059 // store i64* a, i64 b
1060 // then the store should really be marked as 'align 1', but is not. If it
1061 // were marked as 'align 1' then the indexed form would have been
1062 // instruction-selected initially, and the problem this 'fixup' is preventing
1063 // won't happen regardless.
Hal Finkelfa559692013-07-09 06:34:51 +00001064 if (FrameIdx < 0)
1065 return;
1066
1067 MachineFunction &MF = DAG.getMachineFunction();
1068 MachineFrameInfo *MFI = MF.getFrameInfo();
1069
1070 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1071 if (Align >= 4)
1072 return;
1073
1074 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1075 FuncInfo->setHasNonRISpills();
1076}
1077
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001078/// Returns true if the address N can be represented by a base register plus
1079/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand347a5072013-05-16 17:58:02 +00001080/// represented as reg+reg. If Aligned is true, only accept displacements
1081/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman475871a2008-07-27 21:46:04 +00001082bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman73e09142009-01-15 16:29:45 +00001083 SDValue &Base,
Ulrich Weigand347a5072013-05-16 17:58:02 +00001084 SelectionDAG &DAG,
1085 bool Aligned) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +00001086 // FIXME dl should come from parent load or store, not from address
Andrew Trickac6d9be2013-05-25 02:42:55 +00001087 SDLoc dl(N);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001088 // If this can be more profitably realized as r+r, fail.
1089 if (SelectAddressRegReg(N, Disp, Base, DAG))
1090 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001091
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001092 if (N.getOpcode() == ISD::ADD) {
1093 short imm = 0;
Ulrich Weigand347a5072013-05-16 17:58:02 +00001094 if (isIntS16Immediate(N.getOperand(1), imm) &&
1095 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigandf0ef8822013-05-16 14:53:05 +00001096 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001097 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1098 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkelfa559692013-07-09 06:34:51 +00001099 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001100 } else {
1101 Base = N.getOperand(0);
1102 }
1103 return true; // [r+i]
1104 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1105 // Match LOAD (ADD (X, Lo(G))).
Gabor Greif413ca0d2012-04-20 11:41:38 +00001106 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001107 && "Cannot handle constant offsets yet!");
1108 Disp = N.getOperand(1).getOperand(0); // The global address.
1109 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackyfd42ed62012-06-04 17:36:38 +00001110 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001111 Disp.getOpcode() == ISD::TargetConstantPool ||
1112 Disp.getOpcode() == ISD::TargetJumpTable);
1113 Base = N.getOperand(0);
1114 return true; // [&g+r]
1115 }
1116 } else if (N.getOpcode() == ISD::OR) {
1117 short imm = 0;
Ulrich Weigand347a5072013-05-16 17:58:02 +00001118 if (isIntS16Immediate(N.getOperand(1), imm) &&
1119 (!Aligned || (imm & 3) == 0)) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001120 // If this is an or of disjoint bitfields, we can codegen this as an add
1121 // (for better address arithmetic) if the LHS and RHS of the OR are
1122 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001123 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001124 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling3e98c302008-03-24 23:16:37 +00001125
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001126 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001127 // If all of the bits are known zero on the LHS or RHS, the add won't
1128 // carry.
1129 Base = N.getOperand(0);
Ulrich Weigandf0ef8822013-05-16 14:53:05 +00001130 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001131 return true;
1132 }
1133 }
1134 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1135 // Loading from a constant address.
Scott Michelfdc40a02009-02-17 22:15:04 +00001136
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001137 // If this address fits entirely in a 16-bit sext immediate field, codegen
1138 // this as "d, 0"
1139 short Imm;
Ulrich Weigand347a5072013-05-16 17:58:02 +00001140 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001141 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkel76973702013-03-21 23:45:03 +00001142 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1143 CN->getValueType(0));
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001144 return true;
1145 }
Chris Lattnerbc681d62007-02-17 06:44:03 +00001146
1147 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand347a5072013-05-16 17:58:02 +00001148 if ((CN->getValueType(0) == MVT::i32 ||
1149 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1150 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001151 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001152
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001153 // Otherwise, break this down into an LIS + disp.
Owen Anderson825b72b2009-08-11 20:47:22 +00001154 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00001155
Owen Anderson825b72b2009-08-11 20:47:22 +00001156 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1157 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman602b0c82009-09-25 18:54:59 +00001158 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001159 return true;
1160 }
1161 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001162
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001163 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkelfa559692013-07-09 06:34:51 +00001164 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001165 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkelfa559692013-07-09 06:34:51 +00001166 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1167 } else
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001168 Base = N;
1169 return true; // [r+0]
1170}
1171
1172/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1173/// represented as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +00001174bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1175 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +00001176 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001177 // Check to see if we can easily represent this as an [r+r] address. This
1178 // will fail if it thinks that the address is more profitably represented as
1179 // reg+imm, e.g. where imm = 0.
1180 if (SelectAddressRegReg(N, Base, Index, DAG))
1181 return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001182
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001183 // If the operand is an addition, always emit this as [r+r], since this is
1184 // better (for code size, and execution, as the memop does the add for free)
1185 // than emitting an explicit add.
1186 if (N.getOpcode() == ISD::ADD) {
1187 Base = N.getOperand(0);
1188 Index = N.getOperand(1);
1189 return true;
1190 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001191
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001192 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkel76973702013-03-21 23:45:03 +00001193 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1194 N.getValueType());
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001195 Index = N;
1196 return true;
1197}
1198
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001199/// getPreIndexedAddressParts - returns true by value, base pointer and
1200/// offset pointer and addressing mode by reference if the node's address
1201/// can be legally represented as pre-indexed load / store address.
Dan Gohman475871a2008-07-27 21:46:04 +00001202bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1203 SDValue &Offset,
Evan Cheng144d8f02006-11-09 17:55:04 +00001204 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00001205 SelectionDAG &DAG) const {
Hal Finkel77838f92012-06-04 02:21:00 +00001206 if (DisablePPCPreinc) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001207
Ulrich Weigand881a7152013-03-22 14:58:48 +00001208 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00001209 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00001210 EVT VT;
Hal Finkel08a215c2013-03-18 23:00:58 +00001211 unsigned Alignment;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001212 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1213 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001214 VT = LD->getMemoryVT();
Hal Finkel08a215c2013-03-18 23:00:58 +00001215 Alignment = LD->getAlignment();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001216 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001217 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001218 VT = ST->getMemoryVT();
Hal Finkel08a215c2013-03-18 23:00:58 +00001219 Alignment = ST->getAlignment();
Ulrich Weigand881a7152013-03-22 14:58:48 +00001220 isLoad = false;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001221 } else
1222 return false;
1223
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001224 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001225 if (VT.isVector())
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001226 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001227
Ulrich Weigand881a7152013-03-22 14:58:48 +00001228 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1229
1230 // Common code will reject creating a pre-inc form if the base pointer
1231 // is a frame index, or if N is a store and the base pointer is either
1232 // the same as or a predecessor of the value being stored. Check for
1233 // those situations here, and try with swapped Base/Offset instead.
1234 bool Swap = false;
1235
1236 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1237 Swap = true;
1238 else if (!isLoad) {
1239 SDValue Val = cast<StoreSDNode>(N)->getValue();
1240 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1241 Swap = true;
1242 }
1243
1244 if (Swap)
1245 std::swap(Base, Offset);
1246
Hal Finkel0fcdd8b2012-06-20 15:43:03 +00001247 AM = ISD::PRE_INC;
1248 return true;
Hal Finkelac81cc32012-06-19 02:34:32 +00001249 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001250
Ulrich Weigand347a5072013-05-16 17:58:02 +00001251 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson825b72b2009-08-11 20:47:22 +00001252 if (VT != MVT::i64) {
Ulrich Weigand347a5072013-05-16 17:58:02 +00001253 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner0851b4f2006-11-15 19:55:13 +00001254 return false;
1255 } else {
Hal Finkel08a215c2013-03-18 23:00:58 +00001256 // LDU/STU need an address with at least 4-byte alignment.
1257 if (Alignment < 4)
1258 return false;
1259
Ulrich Weigand347a5072013-05-16 17:58:02 +00001260 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner0851b4f2006-11-15 19:55:13 +00001261 return false;
1262 }
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001263
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001264 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001265 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1266 // sext i32 to i64 when addr mode is r+i.
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001268 LD->getExtensionType() == ISD::SEXTLOAD &&
1269 isa<ConstantSDNode>(Offset))
1270 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001271 }
1272
Chris Lattner4eab7142006-11-10 02:08:47 +00001273 AM = ISD::PRE_INC;
1274 return true;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001275}
1276
1277//===----------------------------------------------------------------------===//
Chris Lattner1a635d62006-04-14 06:01:58 +00001278// LowerOperation implementation
1279//===----------------------------------------------------------------------===//
1280
Chris Lattner1e61e692010-11-15 02:46:57 +00001281/// GetLabelAccessInfo - Return true if we should reference labels using a
1282/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1283static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Chris Lattner6d2ff122010-11-15 03:13:19 +00001284 unsigned &LoOpFlags, const GlobalValue *GV = 0) {
Ulrich Weigand92cfa612013-06-21 14:42:20 +00001285 HiOpFlags = PPCII::MO_HA;
1286 LoOpFlags = PPCII::MO_LO;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001287
Chris Lattner1e61e692010-11-15 02:46:57 +00001288 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1289 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001290 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattner1e61e692010-11-15 02:46:57 +00001291 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattner6d2ff122010-11-15 03:13:19 +00001292 if (isPIC) {
1293 HiOpFlags |= PPCII::MO_PIC_FLAG;
1294 LoOpFlags |= PPCII::MO_PIC_FLAG;
1295 }
1296
1297 // If this is a reference to a global value that requires a non-lazy-ptr, make
1298 // sure that instruction lowering adds it.
1299 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1300 HiOpFlags |= PPCII::MO_NLP_FLAG;
1301 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001302
Chris Lattner6d2ff122010-11-15 03:13:19 +00001303 if (GV->hasHiddenVisibility()) {
1304 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1305 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1306 }
1307 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001308
Chris Lattner1e61e692010-11-15 02:46:57 +00001309 return isPIC;
1310}
1311
1312static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1313 SelectionDAG &DAG) {
1314 EVT PtrVT = HiPart.getValueType();
1315 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001316 SDLoc DL(HiPart);
Chris Lattner1e61e692010-11-15 02:46:57 +00001317
1318 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1319 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001320
Chris Lattner1e61e692010-11-15 02:46:57 +00001321 // With PIC, the first instruction is actually "GR+hi(&G)".
1322 if (isPIC)
1323 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1324 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001325
Chris Lattner1e61e692010-11-15 02:46:57 +00001326 // Generate non-pic code that has direct accesses to the constant pool.
1327 // The address of the global is just (hi(&g)+lo(&g)).
1328 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1329}
1330
Scott Michelfdc40a02009-02-17 22:15:04 +00001331SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001332 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001333 EVT PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001334 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001335 const Constant *C = CP->getConstVal();
Chris Lattner1a635d62006-04-14 06:01:58 +00001336
Roman Divacky9fb8b492012-08-24 16:26:02 +00001337 // 64-bit SVR4 ABI code is always position-independent.
1338 // The actual address of the GlobalValue is stored in the TOC.
1339 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1340 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001341 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divacky9fb8b492012-08-24 16:26:02 +00001342 DAG.getRegister(PPC::X2, MVT::i64));
1343 }
1344
Chris Lattner1e61e692010-11-15 02:46:57 +00001345 unsigned MOHiFlag, MOLoFlag;
1346 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1347 SDValue CPIHi =
1348 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1349 SDValue CPILo =
1350 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1351 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00001352}
1353
Dan Gohmand858e902010-04-17 15:26:15 +00001354SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001355 EVT PtrVT = Op.getValueType();
Nate Begeman37efe672006-04-22 18:53:45 +00001356 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001357
Roman Divacky9fb8b492012-08-24 16:26:02 +00001358 // 64-bit SVR4 ABI code is always position-independent.
1359 // The actual address of the GlobalValue is stored in the TOC.
1360 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1361 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001362 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divacky9fb8b492012-08-24 16:26:02 +00001363 DAG.getRegister(PPC::X2, MVT::i64));
1364 }
1365
Chris Lattner1e61e692010-11-15 02:46:57 +00001366 unsigned MOHiFlag, MOLoFlag;
1367 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1368 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1369 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1370 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001371}
1372
Dan Gohmand858e902010-04-17 15:26:15 +00001373SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1374 SelectionDAG &DAG) const {
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001375 EVT PtrVT = Op.getValueType();
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001376
Dan Gohman46510a72010-04-15 01:51:59 +00001377 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001378
Chris Lattner1e61e692010-11-15 02:46:57 +00001379 unsigned MOHiFlag, MOLoFlag;
1380 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001381 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1382 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattner1e61e692010-11-15 02:46:57 +00001383 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1384}
1385
Roman Divackyfd42ed62012-06-04 17:36:38 +00001386SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1387 SelectionDAG &DAG) const {
1388
1389 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001390 SDLoc dl(GA);
Roman Divackyfd42ed62012-06-04 17:36:38 +00001391 const GlobalValue *GV = GA->getGlobal();
1392 EVT PtrVT = getPointerTy();
1393 bool is64bit = PPCSubTarget.isPPC64();
1394
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001395 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackyfd42ed62012-06-04 17:36:38 +00001396
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001397 if (Model == TLSModel::LocalExec) {
1398 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigand92cfa612013-06-21 14:42:20 +00001399 PPCII::MO_TPREL_HA);
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001400 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigand92cfa612013-06-21 14:42:20 +00001401 PPCII::MO_TPREL_LO);
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001402 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1403 is64bit ? MVT::i64 : MVT::i32);
1404 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1405 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1406 }
Roman Divackyfd42ed62012-06-04 17:36:38 +00001407
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001408 if (!is64bit)
1409 llvm_unreachable("only local-exec is currently supported for ppc32");
1410
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001411 if (Model == TLSModel::InitialExec) {
Bill Schmidtdfebc4c2012-12-13 18:45:54 +00001412 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand23a72c82013-07-05 12:22:36 +00001413 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1414 PPCII::MO_TLS);
Bill Schmidtdfebc4c2012-12-13 18:45:54 +00001415 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
Bill Schmidtb453e162012-12-14 17:02:38 +00001416 SDValue TPOffsetHi = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1417 PtrVT, GOTReg, TGA);
1418 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
1419 PtrVT, TGA, TPOffsetHi);
Ulrich Weigand23a72c82013-07-05 12:22:36 +00001420 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001421 }
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001422
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001423 if (Model == TLSModel::GeneralDynamic) {
1424 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1425 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1426 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1427 GOTReg, TGA);
1428 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1429 GOTEntryHi, TGA);
1430
1431 // We need a chain node, and don't have one handy. The underlying
1432 // call has no side effects, so using the function entry node
1433 // suffices.
1434 SDValue Chain = DAG.getEntryNode();
1435 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1436 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1437 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1438 PtrVT, ParmReg, TGA);
Bill Schmidt349c2782012-12-12 19:29:35 +00001439 // The return value from GET_TLS_ADDR really is in X3 already, but
1440 // some hacks are needed here to tie everything together. The extra
1441 // copies dissolve during subsequent transforms.
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001442 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1443 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1444 }
1445
Bill Schmidt349c2782012-12-12 19:29:35 +00001446 if (Model == TLSModel::LocalDynamic) {
1447 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1448 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1449 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1450 GOTReg, TGA);
1451 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1452 GOTEntryHi, TGA);
1453
1454 // We need a chain node, and don't have one handy. The underlying
1455 // call has no side effects, so using the function entry node
1456 // suffices.
1457 SDValue Chain = DAG.getEntryNode();
1458 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1459 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1460 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1461 PtrVT, ParmReg, TGA);
1462 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1463 // some hacks are needed here to tie everything together. The extra
1464 // copies dissolve during subsequent transforms.
1465 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1466 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt1e18b862012-12-13 20:57:10 +00001467 Chain, ParmReg, TGA);
Bill Schmidt349c2782012-12-12 19:29:35 +00001468 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1469 }
1470
1471 llvm_unreachable("Unknown TLS model!");
Roman Divackyfd42ed62012-06-04 17:36:38 +00001472}
1473
Chris Lattner1e61e692010-11-15 02:46:57 +00001474SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1475 SelectionDAG &DAG) const {
1476 EVT PtrVT = Op.getValueType();
1477 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001478 SDLoc DL(GSDN);
Chris Lattner1e61e692010-11-15 02:46:57 +00001479 const GlobalValue *GV = GSDN->getGlobal();
1480
Chris Lattner1e61e692010-11-15 02:46:57 +00001481 // 64-bit SVR4 ABI code is always position-independent.
1482 // The actual address of the GlobalValue is stored in the TOC.
1483 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1484 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1485 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1486 DAG.getRegister(PPC::X2, MVT::i64));
1487 }
1488
Chris Lattner6d2ff122010-11-15 03:13:19 +00001489 unsigned MOHiFlag, MOLoFlag;
1490 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattner1e61e692010-11-15 02:46:57 +00001491
Chris Lattner6d2ff122010-11-15 03:13:19 +00001492 SDValue GAHi =
1493 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1494 SDValue GALo =
1495 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001496
Chris Lattner6d2ff122010-11-15 03:13:19 +00001497 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001498
Chris Lattner6d2ff122010-11-15 03:13:19 +00001499 // If the global reference is actually to a non-lazy-pointer, we have to do an
1500 // extra load to get the address of the global.
1501 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1502 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001503 false, false, false, 0);
Chris Lattner6d2ff122010-11-15 03:13:19 +00001504 return Ptr;
Chris Lattner1a635d62006-04-14 06:01:58 +00001505}
1506
Dan Gohmand858e902010-04-17 15:26:15 +00001507SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner1a635d62006-04-14 06:01:58 +00001508 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001509 SDLoc dl(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00001510
Chris Lattner1a635d62006-04-14 06:01:58 +00001511 // If we're comparing for equality to zero, expose the fact that this is
1512 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1513 // fold the new nodes.
1514 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1515 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Andersone50ed302009-08-10 22:56:29 +00001516 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001517 SDValue Zext = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001518 if (VT.bitsLT(MVT::i32)) {
1519 VT = MVT::i32;
Dale Johannesenf5d97892009-02-04 01:48:28 +00001520 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001521 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00001522 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001523 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1524 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson825b72b2009-08-11 20:47:22 +00001525 DAG.getConstant(Log2b, MVT::i32));
1526 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner1a635d62006-04-14 06:01:58 +00001527 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001528 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner1a635d62006-04-14 06:01:58 +00001529 // optimized. FIXME: revisit this when we can custom lower all setcc
1530 // optimizations.
1531 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman475871a2008-07-27 21:46:04 +00001532 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001533 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001534
Chris Lattner1a635d62006-04-14 06:01:58 +00001535 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattnerac011bc2006-11-14 05:28:08 +00001536 // by xor'ing the rhs with the lhs, which is faster than setting a
1537 // condition register, reading it back out, and masking the correct bit. The
1538 // normal approach here uses sub to do this instead of xor. Using xor exposes
1539 // the result to other bit-twiddling opportunities.
Owen Andersone50ed302009-08-10 22:56:29 +00001540 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001541 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001542 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001543 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner1a635d62006-04-14 06:01:58 +00001544 Op.getOperand(1));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001545 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner1a635d62006-04-14 06:01:58 +00001546 }
Dan Gohman475871a2008-07-27 21:46:04 +00001547 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001548}
1549
Dan Gohman475871a2008-07-27 21:46:04 +00001550SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001551 const PPCSubtarget &Subtarget) const {
Roman Divackybdb226e2011-06-28 15:30:42 +00001552 SDNode *Node = Op.getNode();
1553 EVT VT = Node->getValueType(0);
1554 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1555 SDValue InChain = Node->getOperand(0);
1556 SDValue VAListPtr = Node->getOperand(1);
1557 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001558 SDLoc dl(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001559
Roman Divackybdb226e2011-06-28 15:30:42 +00001560 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1561
1562 // gpr_index
1563 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1564 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1565 false, false, 0);
1566 InChain = GprIndex.getValue(1);
1567
1568 if (VT == MVT::i64) {
1569 // Check if GprIndex is even
1570 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1571 DAG.getConstant(1, MVT::i32));
1572 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1573 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1574 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1575 DAG.getConstant(1, MVT::i32));
1576 // Align GprIndex to be even if it isn't
1577 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1578 GprIndex);
1579 }
1580
1581 // fpr index is 1 byte after gpr
1582 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1583 DAG.getConstant(1, MVT::i32));
1584
1585 // fpr
1586 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1587 FprPtr, MachinePointerInfo(SV), MVT::i8,
1588 false, false, 0);
1589 InChain = FprIndex.getValue(1);
1590
1591 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1592 DAG.getConstant(8, MVT::i32));
1593
1594 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1595 DAG.getConstant(4, MVT::i32));
1596
1597 // areas
1598 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001599 MachinePointerInfo(), false, false,
1600 false, 0);
Roman Divackybdb226e2011-06-28 15:30:42 +00001601 InChain = OverflowArea.getValue(1);
1602
1603 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001604 MachinePointerInfo(), false, false,
1605 false, 0);
Roman Divackybdb226e2011-06-28 15:30:42 +00001606 InChain = RegSaveArea.getValue(1);
1607
1608 // select overflow_area if index > 8
1609 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1610 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1611
Roman Divackybdb226e2011-06-28 15:30:42 +00001612 // adjustment constant gpr_index * 4/8
1613 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1614 VT.isInteger() ? GprIndex : FprIndex,
1615 DAG.getConstant(VT.isInteger() ? 4 : 8,
1616 MVT::i32));
1617
1618 // OurReg = RegSaveArea + RegConstant
1619 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1620 RegConstant);
1621
1622 // Floating types are 32 bytes into RegSaveArea
1623 if (VT.isFloatingPoint())
1624 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1625 DAG.getConstant(32, MVT::i32));
1626
1627 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1628 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1629 VT.isInteger() ? GprIndex : FprIndex,
1630 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1631 MVT::i32));
1632
1633 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1634 VT.isInteger() ? VAListPtr : FprPtr,
1635 MachinePointerInfo(SV),
1636 MVT::i8, false, false, 0);
1637
1638 // determine if we should load from reg_save_area or overflow_area
1639 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1640
1641 // increase overflow_area by 4/8 if gpr/fpr > 8
1642 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1643 DAG.getConstant(VT.isInteger() ? 4 : 8,
1644 MVT::i32));
1645
1646 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1647 OverflowAreaPlusN);
1648
1649 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1650 OverflowAreaPtr,
1651 MachinePointerInfo(),
1652 MVT::i32, false, false, 0);
1653
NAKAMURA Takumi25f6b5a2012-08-30 15:52:23 +00001654 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001655 false, false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001656}
1657
Roman Divacky6ebf55d2013-07-25 21:36:47 +00001658SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
1659 const PPCSubtarget &Subtarget) const {
1660 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
1661
1662 // We have to copy the entire va_list struct:
1663 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
1664 return DAG.getMemcpy(Op.getOperand(0), Op,
1665 Op.getOperand(1), Op.getOperand(2),
1666 DAG.getConstant(12, MVT::i32), 8, false, true,
1667 MachinePointerInfo(), MachinePointerInfo());
1668}
1669
Duncan Sands4a544a72011-09-06 13:37:06 +00001670SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1671 SelectionDAG &DAG) const {
1672 return Op.getOperand(0);
1673}
1674
1675SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1676 SelectionDAG &DAG) const {
Bill Wendling77959322008-09-17 00:30:57 +00001677 SDValue Chain = Op.getOperand(0);
1678 SDValue Trmp = Op.getOperand(1); // trampoline
1679 SDValue FPtr = Op.getOperand(2); // nested function
1680 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickac6d9be2013-05-25 02:42:55 +00001681 SDLoc dl(Op);
Bill Wendling77959322008-09-17 00:30:57 +00001682
Owen Andersone50ed302009-08-10 22:56:29 +00001683 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00001684 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001685 Type *IntPtrTy =
Micah Villmow3574eca2012-10-08 16:38:25 +00001686 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruthece6c6b2012-11-01 08:07:29 +00001687 *DAG.getContext());
Bill Wendling77959322008-09-17 00:30:57 +00001688
Scott Michelfdc40a02009-02-17 22:15:04 +00001689 TargetLowering::ArgListTy Args;
Bill Wendling77959322008-09-17 00:30:57 +00001690 TargetLowering::ArgListEntry Entry;
1691
1692 Entry.Ty = IntPtrTy;
1693 Entry.Node = Trmp; Args.push_back(Entry);
1694
1695 // TrampSize == (isPPC64 ? 48 : 40);
1696 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling77959322008-09-17 00:30:57 +00001698 Args.push_back(Entry);
1699
1700 Entry.Node = FPtr; Args.push_back(Entry);
1701 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelfdc40a02009-02-17 22:15:04 +00001702
Bill Wendling77959322008-09-17 00:30:57 +00001703 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001704 TargetLowering::CallLoweringInfo CLI(Chain,
1705 Type::getVoidTy(*DAG.getContext()),
1706 false, false, false, false, 0,
1707 CallingConv::C,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001708 /*isTailCall=*/false,
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001709 /*doesNotRet=*/false,
1710 /*isReturnValueUsed=*/true,
Bill Wendling77959322008-09-17 00:30:57 +00001711 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Bill Wendling46ada192010-03-02 01:55:18 +00001712 Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001713 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bill Wendling77959322008-09-17 00:30:57 +00001714
Duncan Sands4a544a72011-09-06 13:37:06 +00001715 return CallResult.second;
Bill Wendling77959322008-09-17 00:30:57 +00001716}
1717
Dan Gohman475871a2008-07-27 21:46:04 +00001718SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001719 const PPCSubtarget &Subtarget) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001720 MachineFunction &MF = DAG.getMachineFunction();
1721 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1722
Andrew Trickac6d9be2013-05-25 02:42:55 +00001723 SDLoc dl(Op);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001724
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001725 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray01119992007-04-03 13:59:52 +00001726 // vastart just stores the address of the VarArgsFrameIndex slot into the
1727 // memory location argument.
Owen Andersone50ed302009-08-10 22:56:29 +00001728 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001729 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001730 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner6229d0a2010-09-21 18:41:36 +00001731 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1732 MachinePointerInfo(SV),
David Greene534502d12010-02-15 16:56:53 +00001733 false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001734 }
1735
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001736 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray01119992007-04-03 13:59:52 +00001737 // We suppose the given va_list is already allocated.
1738 //
1739 // typedef struct {
1740 // char gpr; /* index into the array of 8 GPRs
1741 // * stored in the register save area
1742 // * gpr=0 corresponds to r3,
1743 // * gpr=1 to r4, etc.
1744 // */
1745 // char fpr; /* index into the array of 8 FPRs
1746 // * stored in the register save area
1747 // * fpr=0 corresponds to f1,
1748 // * fpr=1 to f2, etc.
1749 // */
1750 // char *overflow_arg_area;
1751 // /* location on stack that holds
1752 // * the next overflow argument
1753 // */
1754 // char *reg_save_area;
1755 // /* where r3:r10 and f1:f8 (if saved)
1756 // * are stored
1757 // */
1758 // } va_list[1];
1759
1760
Dan Gohman1e93df62010-04-17 14:41:14 +00001761 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1762 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00001763
Nicolas Geoffray01119992007-04-03 13:59:52 +00001764
Owen Andersone50ed302009-08-10 22:56:29 +00001765 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfdc40a02009-02-17 22:15:04 +00001766
Dan Gohman1e93df62010-04-17 14:41:14 +00001767 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1768 PtrVT);
1769 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1770 PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001771
Duncan Sands83ec4b62008-06-06 12:08:01 +00001772 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman475871a2008-07-27 21:46:04 +00001773 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001774
Duncan Sands83ec4b62008-06-06 12:08:01 +00001775 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001776 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001777
1778 uint64_t FPROffset = 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001779 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001780
Dan Gohman69de1932008-02-06 22:27:42 +00001781 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001782
Nicolas Geoffray01119992007-04-03 13:59:52 +00001783 // Store first byte : number of int regs
Tilmann Schellerffd02002009-07-03 06:45:56 +00001784 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001785 Op.getOperand(1),
1786 MachinePointerInfo(SV),
1787 MVT::i8, false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001788 uint64_t nextOffset = FPROffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001789 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray01119992007-04-03 13:59:52 +00001790 ConstFPROffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001791
Nicolas Geoffray01119992007-04-03 13:59:52 +00001792 // Store second byte : number of float regs
Dan Gohman475871a2008-07-27 21:46:04 +00001793 SDValue secondStore =
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001794 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1795 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene534502d12010-02-15 16:56:53 +00001796 false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001797 nextOffset += StackOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001798 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001799
Nicolas Geoffray01119992007-04-03 13:59:52 +00001800 // Store second word : arguments given on stack
Dan Gohman475871a2008-07-27 21:46:04 +00001801 SDValue thirdStore =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001802 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1803 MachinePointerInfo(SV, nextOffset),
David Greene534502d12010-02-15 16:56:53 +00001804 false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001805 nextOffset += FrameOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001806 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001807
1808 // Store third word : arguments given in registers
Chris Lattner6229d0a2010-09-21 18:41:36 +00001809 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1810 MachinePointerInfo(SV, nextOffset),
David Greene534502d12010-02-15 16:56:53 +00001811 false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001812
Chris Lattner1a635d62006-04-14 06:01:58 +00001813}
1814
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00001815#include "PPCGenCallingConv.inc"
1816
Bill Schmidtd3f77662013-06-12 16:39:22 +00001817bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1818 CCValAssign::LocInfo &LocInfo,
1819 ISD::ArgFlagsTy &ArgFlags,
1820 CCState &State) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001821 return true;
1822}
1823
Bill Schmidtd3f77662013-06-12 16:39:22 +00001824bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
1825 MVT &LocVT,
1826 CCValAssign::LocInfo &LocInfo,
1827 ISD::ArgFlagsTy &ArgFlags,
1828 CCState &State) {
Craig Topperc5eaae42012-03-11 07:57:25 +00001829 static const uint16_t ArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001830 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1831 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1832 };
1833 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001834
Tilmann Schellerffd02002009-07-03 06:45:56 +00001835 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1836
1837 // Skip one register if the first unallocated register has an even register
1838 // number and there are still argument registers available which have not been
1839 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1840 // need to skip a register if RegNum is odd.
1841 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1842 State.AllocateReg(ArgRegs[RegNum]);
1843 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001844
Tilmann Schellerffd02002009-07-03 06:45:56 +00001845 // Always return false here, as this function only makes sure that the first
1846 // unallocated register has an odd register number and does not actually
1847 // allocate a register for the current argument.
1848 return false;
1849}
1850
Bill Schmidtd3f77662013-06-12 16:39:22 +00001851bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
1852 MVT &LocVT,
1853 CCValAssign::LocInfo &LocInfo,
1854 ISD::ArgFlagsTy &ArgFlags,
1855 CCState &State) {
Craig Topperc5eaae42012-03-11 07:57:25 +00001856 static const uint16_t ArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001857 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1858 PPC::F8
1859 };
1860
1861 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001862
Tilmann Schellerffd02002009-07-03 06:45:56 +00001863 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1864
1865 // If there is only one Floating-point register left we need to put both f64
1866 // values of a split ppc_fp128 value on the stack.
1867 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1868 State.AllocateReg(ArgRegs[RegNum]);
1869 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001870
Tilmann Schellerffd02002009-07-03 06:45:56 +00001871 // Always return false here, as this function only makes sure that the two f64
1872 // values a ppc_fp128 value is split into are both passed in registers or both
1873 // passed on the stack and does not actually allocate a register for the
1874 // current argument.
1875 return false;
1876}
1877
Chris Lattner9f0bc652007-02-25 05:34:32 +00001878/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001879/// on Darwin.
Craig Topperb78ca422012-03-11 07:16:55 +00001880static const uint16_t *GetFPR() {
1881 static const uint16_t FPR[] = {
Chris Lattner9f0bc652007-02-25 05:34:32 +00001882 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001883 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner9f0bc652007-02-25 05:34:32 +00001884 };
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001885
Chris Lattner9f0bc652007-02-25 05:34:32 +00001886 return FPR;
1887}
1888
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001889/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1890/// the stack.
Owen Andersone50ed302009-08-10 22:56:29 +00001891static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001892 unsigned PtrByteSize) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001893 unsigned ArgSize = ArgVT.getSizeInBits()/8;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001894 if (Flags.isByVal())
1895 ArgSize = Flags.getByValSize();
1896 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1897
1898 return ArgSize;
1899}
1900
Dan Gohman475871a2008-07-27 21:46:04 +00001901SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001902PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001903 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001904 const SmallVectorImpl<ISD::InputArg>
1905 &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001906 SDLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001907 SmallVectorImpl<SDValue> &InVals)
1908 const {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00001909 if (PPCSubTarget.isSVR4ABI()) {
1910 if (PPCSubTarget.isPPC64())
1911 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
1912 dl, DAG, InVals);
1913 else
1914 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
1915 dl, DAG, InVals);
Bill Schmidt419f3762012-09-19 15:42:13 +00001916 } else {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00001917 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
1918 dl, DAG, InVals);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001919 }
1920}
1921
1922SDValue
Bill Schmidt419f3762012-09-19 15:42:13 +00001923PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohman98ca4f22009-08-05 01:29:28 +00001924 SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001925 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001926 const SmallVectorImpl<ISD::InputArg>
1927 &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001928 SDLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001929 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001930
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001931 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001932 // +-----------------------------------+
1933 // +--> | Back chain |
1934 // | +-----------------------------------+
1935 // | | Floating-point register save area |
1936 // | +-----------------------------------+
1937 // | | General register save area |
1938 // | +-----------------------------------+
1939 // | | CR save word |
1940 // | +-----------------------------------+
1941 // | | VRSAVE save word |
1942 // | +-----------------------------------+
1943 // | | Alignment padding |
1944 // | +-----------------------------------+
1945 // | | Vector register save area |
1946 // | +-----------------------------------+
1947 // | | Local variable space |
1948 // | +-----------------------------------+
1949 // | | Parameter list area |
1950 // | +-----------------------------------+
1951 // | | LR save word |
1952 // | +-----------------------------------+
1953 // SP--> +--- | Back chain |
1954 // +-----------------------------------+
1955 //
1956 // Specifications:
1957 // System V Application Binary Interface PowerPC Processor Supplement
1958 // AltiVec Technology Programming Interface Manual
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001959
Tilmann Schellerffd02002009-07-03 06:45:56 +00001960 MachineFunction &MF = DAG.getMachineFunction();
1961 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +00001962 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerffd02002009-07-03 06:45:56 +00001963
Owen Andersone50ed302009-08-10 22:56:29 +00001964 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerffd02002009-07-03 06:45:56 +00001965 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001966 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
1967 (CallConv == CallingConv::Fast));
Tilmann Schellerffd02002009-07-03 06:45:56 +00001968 unsigned PtrByteSize = 4;
1969
1970 // Assign locations to all of the incoming arguments.
1971 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001972 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00001973 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00001974
1975 // Reserve space for the linkage area on the stack.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001976 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001977
Bill Schmidt212af6a2013-02-06 17:33:58 +00001978 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001979
Tilmann Schellerffd02002009-07-03 06:45:56 +00001980 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1981 CCValAssign &VA = ArgLocs[i];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001982
Tilmann Schellerffd02002009-07-03 06:45:56 +00001983 // Arguments stored in registers.
1984 if (VA.isRegLoc()) {
Craig Topper44d23822012-02-22 05:59:10 +00001985 const TargetRegisterClass *RC;
Owen Andersone50ed302009-08-10 22:56:29 +00001986 EVT ValVT = VA.getValVT();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001987
Owen Anderson825b72b2009-08-11 20:47:22 +00001988 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001989 default:
Dan Gohman98ca4f22009-08-05 01:29:28 +00001990 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +00001992 RC = &PPC::GPRCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001993 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001994 case MVT::f32:
Craig Topperc9099502012-04-20 06:31:50 +00001995 RC = &PPC::F4RCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001996 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001997 case MVT::f64:
Craig Topperc9099502012-04-20 06:31:50 +00001998 RC = &PPC::F8RCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001999 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002000 case MVT::v16i8:
2001 case MVT::v8i16:
2002 case MVT::v4i32:
2003 case MVT::v4f32:
Craig Topperc9099502012-04-20 06:31:50 +00002004 RC = &PPC::VRRCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002005 break;
2006 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002007
Tilmann Schellerffd02002009-07-03 06:45:56 +00002008 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002009 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002010 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, ValVT);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002011
Dan Gohman98ca4f22009-08-05 01:29:28 +00002012 InVals.push_back(ArgValue);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002013 } else {
2014 // Argument stored in memory.
2015 assert(VA.isMemLoc());
2016
2017 unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8;
2018 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Chenged2ae132010-07-03 00:40:23 +00002019 isImmutable);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002020
2021 // Create load nodes to retrieve arguments from the stack.
2022 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002023 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2024 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002025 false, false, false, 0));
Tilmann Schellerffd02002009-07-03 06:45:56 +00002026 }
2027 }
2028
2029 // Assign locations to all of the incoming aggregate by value arguments.
2030 // Aggregates passed by value are stored in the local variable space of the
2031 // caller's stack frame, right above the parameter list area.
2032 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002033 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00002034 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00002035
2036 // Reserve stack space for the allocations in CCInfo.
2037 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2038
Bill Schmidt212af6a2013-02-06 17:33:58 +00002039 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002040
2041 // Area that is at least reserved in the caller of this function.
2042 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002043
Tilmann Schellerffd02002009-07-03 06:45:56 +00002044 // Set the size that is at least reserved in caller of this function. Tail
2045 // call optimized function's reserved stack space needs to be aligned so that
2046 // taking the difference between two stack areas will result in an aligned
2047 // stack.
2048 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2049
2050 MinReservedArea =
2051 std::max(MinReservedArea,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002052 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002053
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002054 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerffd02002009-07-03 06:45:56 +00002055 getStackAlignment();
2056 unsigned AlignMask = TargetAlign-1;
2057 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002058
Tilmann Schellerffd02002009-07-03 06:45:56 +00002059 FI->setMinReservedArea(MinReservedArea);
2060
2061 SmallVector<SDValue, 8> MemOps;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002062
Tilmann Schellerffd02002009-07-03 06:45:56 +00002063 // If the function takes variable number of arguments, make a frame index for
2064 // the start of the first vararg value... for expansion of llvm.va_start.
2065 if (isVarArg) {
Craig Topperc5eaae42012-03-11 07:57:25 +00002066 static const uint16_t GPArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002067 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2068 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2069 };
2070 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2071
Craig Topperc5eaae42012-03-11 07:57:25 +00002072 static const uint16_t FPArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002073 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2074 PPC::F8
2075 };
2076 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2077
Dan Gohman1e93df62010-04-17 14:41:14 +00002078 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2079 NumGPArgRegs));
2080 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2081 NumFPArgRegs));
Tilmann Schellerffd02002009-07-03 06:45:56 +00002082
2083 // Make room for NumGPArgRegs and NumFPArgRegs.
2084 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson825b72b2009-08-11 20:47:22 +00002085 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002086
Dan Gohman1e93df62010-04-17 14:41:14 +00002087 FuncInfo->setVarArgsStackOffset(
2088 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00002089 CCInfo.getNextStackOffset(), true));
Tilmann Schellerffd02002009-07-03 06:45:56 +00002090
Dan Gohman1e93df62010-04-17 14:41:14 +00002091 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2092 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002093
Jakob Stoklund Olesen4f9af2e2010-10-11 20:43:09 +00002094 // The fixed integer arguments of a variadic function are stored to the
2095 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2096 // the result of va_next.
2097 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2098 // Get an existing live-in vreg, or add a new one.
2099 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2100 if (!VReg)
Devang Patel68e6bee2011-02-21 23:21:26 +00002101 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002102
Dan Gohman98ca4f22009-08-05 01:29:28 +00002103 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002104 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2105 MachinePointerInfo(), false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002106 MemOps.push_back(Store);
2107 // Increment the address by four for the next argument to store
2108 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2109 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2110 }
2111
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002112 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2113 // is set.
Tilmann Schellerffd02002009-07-03 06:45:56 +00002114 // The double arguments are stored to the VarArgsFrameIndex
2115 // on the stack.
Jakob Stoklund Olesen4f9af2e2010-10-11 20:43:09 +00002116 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2117 // Get an existing live-in vreg, or add a new one.
2118 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2119 if (!VReg)
Devang Patel68e6bee2011-02-21 23:21:26 +00002120 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002121
Owen Anderson825b72b2009-08-11 20:47:22 +00002122 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002123 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2124 MachinePointerInfo(), false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002125 MemOps.push_back(Store);
2126 // Increment the address by eight for the next argument to store
Owen Anderson825b72b2009-08-11 20:47:22 +00002127 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerffd02002009-07-03 06:45:56 +00002128 PtrVT);
2129 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2130 }
2131 }
2132
2133 if (!MemOps.empty())
Dan Gohman98ca4f22009-08-05 01:29:28 +00002134 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002135 MVT::Other, &MemOps[0], MemOps.size());
Tilmann Schellerffd02002009-07-03 06:45:56 +00002136
Dan Gohman98ca4f22009-08-05 01:29:28 +00002137 return Chain;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002138}
2139
Bill Schmidt726c2372012-10-23 15:51:16 +00002140// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2141// value to MVT::i64 and then truncate to the correct register size.
2142SDValue
2143PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2144 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002145 SDLoc dl) const {
Bill Schmidt726c2372012-10-23 15:51:16 +00002146 if (Flags.isSExt())
2147 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2148 DAG.getValueType(ObjectVT));
2149 else if (Flags.isZExt())
2150 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2151 DAG.getValueType(ObjectVT));
Matt Arsenault225ed702013-05-18 00:21:46 +00002152
Bill Schmidt726c2372012-10-23 15:51:16 +00002153 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
2154}
2155
2156// Set the size that is at least reserved in caller of this function. Tail
2157// call optimized functions' reserved stack space needs to be aligned so that
2158// taking the difference between two stack areas will result in an aligned
2159// stack.
2160void
2161PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2162 unsigned nAltivecParamsAtEnd,
2163 unsigned MinReservedArea,
2164 bool isPPC64) const {
2165 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2166 // Add the Altivec parameters at the end, if needed.
2167 if (nAltivecParamsAtEnd) {
2168 MinReservedArea = ((MinReservedArea+15)/16)*16;
2169 MinReservedArea += 16*nAltivecParamsAtEnd;
2170 }
2171 MinReservedArea =
2172 std::max(MinReservedArea,
2173 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2174 unsigned TargetAlign
2175 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2176 getStackAlignment();
2177 unsigned AlignMask = TargetAlign-1;
2178 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2179 FI->setMinReservedArea(MinReservedArea);
2180}
2181
Tilmann Schellerffd02002009-07-03 06:45:56 +00002182SDValue
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002183PPCTargetLowering::LowerFormalArguments_64SVR4(
2184 SDValue Chain,
2185 CallingConv::ID CallConv, bool isVarArg,
2186 const SmallVectorImpl<ISD::InputArg>
2187 &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002188 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002189 SmallVectorImpl<SDValue> &InVals) const {
2190 // TODO: add description of PPC stack frame format, or at least some docs.
2191 //
2192 MachineFunction &MF = DAG.getMachineFunction();
2193 MachineFrameInfo *MFI = MF.getFrameInfo();
2194 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2195
2196 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2197 // Potential tail calls could cause overwriting of argument stack slots.
2198 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2199 (CallConv == CallingConv::Fast));
2200 unsigned PtrByteSize = 8;
2201
2202 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2203 // Area that is at least reserved in caller of this function.
2204 unsigned MinReservedArea = ArgOffset;
2205
2206 static const uint16_t GPR[] = {
2207 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2208 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2209 };
2210
2211 static const uint16_t *FPR = GetFPR();
2212
2213 static const uint16_t VR[] = {
2214 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2215 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2216 };
2217
2218 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2219 const unsigned Num_FPR_Regs = 13;
2220 const unsigned Num_VR_Regs = array_lengthof(VR);
2221
2222 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2223
2224 // Add DAG nodes to load the arguments or copy them out of registers. On
2225 // entry to a function on PPC, the arguments start after the linkage area,
2226 // although the first ones are often in registers.
2227
2228 SmallVector<SDValue, 8> MemOps;
2229 unsigned nAltivecParamsAtEnd = 0;
2230 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt49deebb2013-02-20 17:31:41 +00002231 unsigned CurArgIdx = 0;
2232 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002233 SDValue ArgVal;
2234 bool needsLoad = false;
2235 EVT ObjectVT = Ins[ArgNo].VT;
2236 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
2237 unsigned ArgSize = ObjSize;
2238 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt49deebb2013-02-20 17:31:41 +00002239 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2240 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002241
2242 unsigned CurArgOffset = ArgOffset;
2243
2244 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2245 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2246 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
2247 if (isVarArg) {
2248 MinReservedArea = ((MinReservedArea+15)/16)*16;
2249 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2250 Flags,
2251 PtrByteSize);
2252 } else
2253 nAltivecParamsAtEnd++;
2254 } else
2255 // Calculate min reserved area.
2256 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2257 Flags,
2258 PtrByteSize);
2259
2260 // FIXME the codegen can be much improved in some cases.
2261 // We do not have to keep everything in memory.
2262 if (Flags.isByVal()) {
2263 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2264 ObjSize = Flags.getByValSize();
2265 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt42d43352012-10-31 01:15:05 +00002266 // Empty aggregate parameters do not take up registers. Examples:
2267 // struct { } a;
2268 // union { } b;
2269 // int c[0];
2270 // etc. However, we have to provide a place-holder in InVals, so
2271 // pretend we have an 8-byte item at the current address for that
2272 // purpose.
2273 if (!ObjSize) {
2274 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2275 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2276 InVals.push_back(FIN);
2277 continue;
2278 }
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002279 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt7a6cb152012-10-16 13:30:53 +00002280 if (ObjSize < PtrByteSize)
2281 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002282 // The value of the object is its address.
2283 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2284 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2285 InVals.push_back(FIN);
Bill Schmidt37900c52012-10-25 13:38:09 +00002286
2287 if (ObjSize < 8) {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002288 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt37900c52012-10-25 13:38:09 +00002289 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002290 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt37900c52012-10-25 13:38:09 +00002291 SDValue Store;
2292
2293 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2294 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2295 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2296 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
2297 MachinePointerInfo(FuncArg, CurArgOffset),
2298 ObjType, false, false, 0);
2299 } else {
2300 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2301 // store the whole register as-is to the parameter save area
2302 // slot. The address of the parameter was already calculated
2303 // above (InVals.push_back(FIN)) to be the right-justified
2304 // offset within the slot. For this store, we need a new
2305 // frame index that points at the beginning of the slot.
2306 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2307 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2308 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2309 MachinePointerInfo(FuncArg, ArgOffset),
2310 false, false, 0);
2311 }
2312
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002313 MemOps.push_back(Store);
2314 ++GPR_idx;
2315 }
Bill Schmidt37900c52012-10-25 13:38:09 +00002316 // Whether we copied from a register or not, advance the offset
2317 // into the parameter save area by a full doubleword.
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002318 ArgOffset += PtrByteSize;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002319 continue;
2320 }
Bill Schmidt37900c52012-10-25 13:38:09 +00002321
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002322 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2323 // Store whatever pieces of the object are in registers
2324 // to memory. ArgOffset will be the address of the beginning
2325 // of the object.
2326 if (GPR_idx != Num_GPR_Regs) {
2327 unsigned VReg;
2328 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2329 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2330 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2331 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt37900c52012-10-25 13:38:09 +00002332 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002333 MachinePointerInfo(FuncArg, ArgOffset),
2334 false, false, 0);
2335 MemOps.push_back(Store);
2336 ++GPR_idx;
2337 ArgOffset += PtrByteSize;
2338 } else {
Bill Schmidt7a6cb152012-10-16 13:30:53 +00002339 ArgOffset += ArgSize - j;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002340 break;
2341 }
2342 }
2343 continue;
2344 }
2345
2346 switch (ObjectVT.getSimpleVT().SimpleTy) {
2347 default: llvm_unreachable("Unhandled argument type!");
2348 case MVT::i32:
2349 case MVT::i64:
2350 if (GPR_idx != Num_GPR_Regs) {
2351 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2352 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2353
Bill Schmidt726c2372012-10-23 15:51:16 +00002354 if (ObjectVT == MVT::i32)
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002355 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2356 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt726c2372012-10-23 15:51:16 +00002357 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002358
2359 ++GPR_idx;
2360 } else {
2361 needsLoad = true;
2362 ArgSize = PtrByteSize;
2363 }
2364 ArgOffset += 8;
2365 break;
2366
2367 case MVT::f32:
2368 case MVT::f64:
2369 // Every 8 bytes of argument space consumes one of the GPRs available for
2370 // argument passing.
2371 if (GPR_idx != Num_GPR_Regs) {
2372 ++GPR_idx;
2373 }
2374 if (FPR_idx != Num_FPR_Regs) {
2375 unsigned VReg;
2376
2377 if (ObjectVT == MVT::f32)
2378 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2379 else
2380 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
2381
2382 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2383 ++FPR_idx;
2384 } else {
2385 needsLoad = true;
Bill Schmidta867f372012-10-11 15:38:20 +00002386 ArgSize = PtrByteSize;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002387 }
2388
2389 ArgOffset += 8;
2390 break;
2391 case MVT::v4f32:
2392 case MVT::v4i32:
2393 case MVT::v8i16:
2394 case MVT::v16i8:
2395 // Note that vector arguments in registers don't reserve stack space,
2396 // except in varargs functions.
2397 if (VR_idx != Num_VR_Regs) {
2398 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
2399 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2400 if (isVarArg) {
2401 while ((ArgOffset % 16) != 0) {
2402 ArgOffset += PtrByteSize;
2403 if (GPR_idx != Num_GPR_Regs)
2404 GPR_idx++;
2405 }
2406 ArgOffset += 16;
2407 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2408 }
2409 ++VR_idx;
2410 } else {
2411 // Vectors are aligned.
2412 ArgOffset = ((ArgOffset+15)/16)*16;
2413 CurArgOffset = ArgOffset;
2414 ArgOffset += 16;
2415 needsLoad = true;
2416 }
2417 break;
2418 }
2419
2420 // We need to load the argument to a virtual register if we determined
2421 // above that we ran out of physical registers of the appropriate type.
2422 if (needsLoad) {
2423 int FI = MFI->CreateFixedObject(ObjSize,
2424 CurArgOffset + (ArgSize - ObjSize),
2425 isImmutable);
2426 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2427 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2428 false, false, false, 0);
2429 }
2430
2431 InVals.push_back(ArgVal);
2432 }
2433
2434 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt726c2372012-10-23 15:51:16 +00002435 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002436 // taking the difference between two stack areas will result in an aligned
2437 // stack.
Bill Schmidt726c2372012-10-23 15:51:16 +00002438 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002439
2440 // If the function takes variable number of arguments, make a frame index for
2441 // the start of the first vararg value... for expansion of llvm.va_start.
2442 if (isVarArg) {
2443 int Depth = ArgOffset;
2444
2445 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt726c2372012-10-23 15:51:16 +00002446 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002447 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2448
2449 // If this function is vararg, store any remaining integer argument regs
2450 // to their spots on the stack so that they may be loaded by deferencing the
2451 // result of va_next.
2452 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2453 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2454 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2455 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2456 MachinePointerInfo(), false, false, 0);
2457 MemOps.push_back(Store);
2458 // Increment the address by four for the next argument to store
Bill Schmidt726c2372012-10-23 15:51:16 +00002459 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002460 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2461 }
2462 }
2463
2464 if (!MemOps.empty())
2465 Chain = DAG.getNode(ISD::TokenFactor, dl,
2466 MVT::Other, &MemOps[0], MemOps.size());
2467
2468 return Chain;
2469}
2470
2471SDValue
2472PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002473 SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002474 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002475 const SmallVectorImpl<ISD::InputArg>
2476 &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002477 SDLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002478 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002479 // TODO: add description of PPC stack frame format, or at least some docs.
2480 //
2481 MachineFunction &MF = DAG.getMachineFunction();
2482 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +00002483 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00002484
Owen Andersone50ed302009-08-10 22:56:29 +00002485 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00002486 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002487 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002488 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2489 (CallConv == CallingConv::Fast));
Jim Laskeye9bd7b22006-11-28 14:53:52 +00002490 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey2f616bf2006-11-16 22:43:37 +00002491
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002492 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002493 // Area that is at least reserved in caller of this function.
2494 unsigned MinReservedArea = ArgOffset;
2495
Craig Topperb78ca422012-03-11 07:16:55 +00002496 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002497 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2498 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2499 };
Craig Topperb78ca422012-03-11 07:16:55 +00002500 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerc91a4752006-06-26 22:48:35 +00002501 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2502 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2503 };
Scott Michelfdc40a02009-02-17 22:15:04 +00002504
Craig Topperb78ca422012-03-11 07:16:55 +00002505 static const uint16_t *FPR = GetFPR();
Scott Michelfdc40a02009-02-17 22:15:04 +00002506
Craig Topperb78ca422012-03-11 07:16:55 +00002507 static const uint16_t VR[] = {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002508 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2509 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2510 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00002511
Owen Anderson718cb662007-09-07 04:06:50 +00002512 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002513 const unsigned Num_FPR_Regs = 13;
Owen Anderson718cb662007-09-07 04:06:50 +00002514 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey2f616bf2006-11-16 22:43:37 +00002515
2516 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002517
Craig Topperb78ca422012-03-11 07:16:55 +00002518 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelfdc40a02009-02-17 22:15:04 +00002519
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002520 // In 32-bit non-varargs functions, the stack space for vectors is after the
2521 // stack space for non-vectors. We do not use this space unless we have
2522 // too many vectors to fit in registers, something that only occurs in
Scott Michelfdc40a02009-02-17 22:15:04 +00002523 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002524 // that out...for the pathological case, compute VecArgOffset as the
2525 // start of the vector parameter area. Computing VecArgOffset is the
2526 // entire point of the following loop.
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002527 unsigned VecArgOffset = ArgOffset;
2528 if (!isVarArg && !isPPC64) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002529 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002530 ++ArgNo) {
Owen Andersone50ed302009-08-10 22:56:29 +00002531 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002532 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002533
Duncan Sands276dcbd2008-03-21 09:14:45 +00002534 if (Flags.isByVal()) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002535 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer263109d2012-01-20 14:42:32 +00002536 unsigned ObjSize = Flags.getByValSize();
Scott Michelfdc40a02009-02-17 22:15:04 +00002537 unsigned ArgSize =
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002538 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2539 VecArgOffset += ArgSize;
2540 continue;
2541 }
2542
Owen Anderson825b72b2009-08-11 20:47:22 +00002543 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002544 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002545 case MVT::i32:
2546 case MVT::f32:
Bill Schmidt419f3762012-09-19 15:42:13 +00002547 VecArgOffset += 4;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002548 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002549 case MVT::i64: // PPC64
2550 case MVT::f64:
Bill Schmidt419f3762012-09-19 15:42:13 +00002551 // FIXME: We are guaranteed to be !isPPC64 at this point.
2552 // Does MVT::i64 apply?
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002553 VecArgOffset += 8;
2554 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002555 case MVT::v4f32:
2556 case MVT::v4i32:
2557 case MVT::v8i16:
2558 case MVT::v16i8:
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002559 // Nothing to do, we're only looking at Nonvector args here.
2560 break;
2561 }
2562 }
2563 }
2564 // We've found where the vector parameter area in memory is. Skip the
2565 // first 12 parameters; these don't use that memory.
2566 VecArgOffset = ((VecArgOffset+15)/16)*16;
2567 VecArgOffset += 12*16;
2568
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002569 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey2f616bf2006-11-16 22:43:37 +00002570 // entry to a function on PPC, the arguments start after the linkage area,
2571 // although the first ones are often in registers.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002572
Dan Gohman475871a2008-07-27 21:46:04 +00002573 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002574 unsigned nAltivecParamsAtEnd = 0;
Roman Divacky5236ab32012-09-24 20:47:19 +00002575 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidta7f2ce82013-05-08 17:22:33 +00002576 unsigned CurArgIdx = 0;
2577 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002578 SDValue ArgVal;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002579 bool needsLoad = false;
Owen Andersone50ed302009-08-10 22:56:29 +00002580 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002581 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey619965d2006-11-29 13:37:09 +00002582 unsigned ArgSize = ObjSize;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002583 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidta7f2ce82013-05-08 17:22:33 +00002584 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2585 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002586
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002587 unsigned CurArgOffset = ArgOffset;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002588
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002589 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson825b72b2009-08-11 20:47:22 +00002590 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2591 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002592 if (isVarArg || isPPC64) {
2593 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002594 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohman095cc292008-09-13 01:54:27 +00002595 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002596 PtrByteSize);
2597 } else nAltivecParamsAtEnd++;
2598 } else
2599 // Calculate min reserved area.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002600 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohman095cc292008-09-13 01:54:27 +00002601 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002602 PtrByteSize);
2603
Dale Johannesen8419dd62008-03-07 20:27:40 +00002604 // FIXME the codegen can be much improved in some cases.
2605 // We do not have to keep everything in memory.
Duncan Sands276dcbd2008-03-21 09:14:45 +00002606 if (Flags.isByVal()) {
Dale Johannesen8419dd62008-03-07 20:27:40 +00002607 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sands276dcbd2008-03-21 09:14:45 +00002608 ObjSize = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00002609 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002610 // Objects of size 1 and 2 are right justified, everything else is
2611 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen7f96f392008-03-08 01:41:42 +00002612 if (ObjSize==1 || ObjSize==2) {
2613 CurArgOffset = CurArgOffset + (4 - ObjSize);
2614 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002615 // The value of the object is its address.
Evan Chenged2ae132010-07-03 00:40:23 +00002616 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman475871a2008-07-27 21:46:04 +00002617 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002618 InVals.push_back(FIN);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002619 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen7f96f392008-03-08 01:41:42 +00002620 if (GPR_idx != Num_GPR_Regs) {
Roman Divacky951cd022011-06-17 15:21:10 +00002621 unsigned VReg;
2622 if (isPPC64)
2623 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2624 else
2625 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002626 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt726c2372012-10-23 15:51:16 +00002627 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelfdc40a02009-02-17 22:15:04 +00002628 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Roman Divacky5236ab32012-09-24 20:47:19 +00002629 MachinePointerInfo(FuncArg,
2630 CurArgOffset),
Bill Schmidt419f3762012-09-19 15:42:13 +00002631 ObjType, false, false, 0);
Dale Johannesen7f96f392008-03-08 01:41:42 +00002632 MemOps.push_back(Store);
2633 ++GPR_idx;
Dale Johannesen7f96f392008-03-08 01:41:42 +00002634 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002635
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002636 ArgOffset += PtrByteSize;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002637
Dale Johannesen7f96f392008-03-08 01:41:42 +00002638 continue;
2639 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002640 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2641 // Store whatever pieces of the object are in registers
Bill Schmidt419f3762012-09-19 15:42:13 +00002642 // to memory. ArgOffset will be the address of the beginning
2643 // of the object.
Dale Johannesen8419dd62008-03-07 20:27:40 +00002644 if (GPR_idx != Num_GPR_Regs) {
Roman Divacky951cd022011-06-17 15:21:10 +00002645 unsigned VReg;
2646 if (isPPC64)
2647 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2648 else
2649 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Chenged2ae132010-07-03 00:40:23 +00002650 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman475871a2008-07-27 21:46:04 +00002651 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002652 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002653 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Roman Divacky5236ab32012-09-24 20:47:19 +00002654 MachinePointerInfo(FuncArg, ArgOffset),
David Greene534502d12010-02-15 16:56:53 +00002655 false, false, 0);
Dale Johannesen8419dd62008-03-07 20:27:40 +00002656 MemOps.push_back(Store);
2657 ++GPR_idx;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002658 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002659 } else {
2660 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2661 break;
2662 }
2663 }
2664 continue;
2665 }
2666
Owen Anderson825b72b2009-08-11 20:47:22 +00002667 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002668 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002669 case MVT::i32:
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002670 if (!isPPC64) {
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002671 if (GPR_idx != Num_GPR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002672 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002673 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002674 ++GPR_idx;
2675 } else {
2676 needsLoad = true;
2677 ArgSize = PtrByteSize;
2678 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002679 // All int arguments reserve stack space in the Darwin ABI.
2680 ArgOffset += PtrByteSize;
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002681 break;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002682 }
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002683 // FALLTHROUGH
Owen Anderson825b72b2009-08-11 20:47:22 +00002684 case MVT::i64: // PPC64
Chris Lattnerc91a4752006-06-26 22:48:35 +00002685 if (GPR_idx != Num_GPR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002686 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002687 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002688
Bill Schmidt726c2372012-10-23 15:51:16 +00002689 if (ObjectVT == MVT::i32)
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002690 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson825b72b2009-08-11 20:47:22 +00002691 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt726c2372012-10-23 15:51:16 +00002692 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002693
Chris Lattnerc91a4752006-06-26 22:48:35 +00002694 ++GPR_idx;
2695 } else {
2696 needsLoad = true;
Evan Cheng982a0592008-07-24 08:17:07 +00002697 ArgSize = PtrByteSize;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002698 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002699 // All int arguments reserve stack space in the Darwin ABI.
2700 ArgOffset += 8;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002701 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002702
Owen Anderson825b72b2009-08-11 20:47:22 +00002703 case MVT::f32:
2704 case MVT::f64:
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002705 // Every 4 bytes of argument space consumes one of the GPRs available for
2706 // argument passing.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002707 if (GPR_idx != Num_GPR_Regs) {
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002708 ++GPR_idx;
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002709 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002710 ++GPR_idx;
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002711 }
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002712 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002713 unsigned VReg;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002714
Owen Anderson825b72b2009-08-11 20:47:22 +00002715 if (ObjectVT == MVT::f32)
Devang Patel68e6bee2011-02-21 23:21:26 +00002716 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002717 else
Devang Patel68e6bee2011-02-21 23:21:26 +00002718 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002719
Dan Gohman98ca4f22009-08-05 01:29:28 +00002720 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002721 ++FPR_idx;
2722 } else {
2723 needsLoad = true;
2724 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002725
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002726 // All FP arguments reserve stack space in the Darwin ABI.
2727 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002728 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002729 case MVT::v4f32:
2730 case MVT::v4i32:
2731 case MVT::v8i16:
2732 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00002733 // Note that vector arguments in registers don't reserve stack space,
2734 // except in varargs functions.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002735 if (VR_idx != Num_VR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002736 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002737 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesen75092de2008-03-12 00:22:17 +00002738 if (isVarArg) {
2739 while ((ArgOffset % 16) != 0) {
2740 ArgOffset += PtrByteSize;
2741 if (GPR_idx != Num_GPR_Regs)
2742 GPR_idx++;
2743 }
2744 ArgOffset += 16;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002745 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesen75092de2008-03-12 00:22:17 +00002746 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002747 ++VR_idx;
2748 } else {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002749 if (!isVarArg && !isPPC64) {
2750 // Vectors go after all the nonvectors.
2751 CurArgOffset = VecArgOffset;
2752 VecArgOffset += 16;
2753 } else {
2754 // Vectors are aligned.
2755 ArgOffset = ((ArgOffset+15)/16)*16;
2756 CurArgOffset = ArgOffset;
2757 ArgOffset += 16;
Dale Johannesen404d9902008-03-12 00:49:20 +00002758 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002759 needsLoad = true;
2760 }
2761 break;
2762 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002763
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002764 // We need to load the argument to a virtual register if we determined above
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002765 // that we ran out of physical registers of the appropriate type.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002766 if (needsLoad) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002767 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002768 CurArgOffset + (ArgSize - ObjSize),
Evan Chenged2ae132010-07-03 00:40:23 +00002769 isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00002770 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002771 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002772 false, false, false, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002773 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002774
Dan Gohman98ca4f22009-08-05 01:29:28 +00002775 InVals.push_back(ArgVal);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002776 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002777
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002778 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt726c2372012-10-23 15:51:16 +00002779 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002780 // taking the difference between two stack areas will result in an aligned
2781 // stack.
Bill Schmidt726c2372012-10-23 15:51:16 +00002782 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002783
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002784 // If the function takes variable number of arguments, make a frame index for
2785 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002786 if (isVarArg) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002787 int Depth = ArgOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00002788
Dan Gohman1e93df62010-04-17 14:41:14 +00002789 FuncInfo->setVarArgsFrameIndex(
2790 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00002791 Depth, true));
Dan Gohman1e93df62010-04-17 14:41:14 +00002792 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002793
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002794 // If this function is vararg, store any remaining integer argument regs
2795 // to their spots on the stack so that they may be loaded by deferencing the
2796 // result of va_next.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002797 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002798 unsigned VReg;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002799
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002800 if (isPPC64)
Devang Patel68e6bee2011-02-21 23:21:26 +00002801 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002802 else
Devang Patel68e6bee2011-02-21 23:21:26 +00002803 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002804
Dan Gohman98ca4f22009-08-05 01:29:28 +00002805 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002806 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2807 MachinePointerInfo(), false, false, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002808 MemOps.push_back(Store);
2809 // Increment the address by four for the next argument to store
Dan Gohman475871a2008-07-27 21:46:04 +00002810 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002811 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002812 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002813 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002814
Dale Johannesen8419dd62008-03-07 20:27:40 +00002815 if (!MemOps.empty())
Dan Gohman98ca4f22009-08-05 01:29:28 +00002816 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002817 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesen8419dd62008-03-07 20:27:40 +00002818
Dan Gohman98ca4f22009-08-05 01:29:28 +00002819 return Chain;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002820}
2821
Bill Schmidt419f3762012-09-19 15:42:13 +00002822/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
2823/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002824static unsigned
2825CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2826 bool isPPC64,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002827 bool isVarArg,
2828 unsigned CC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002829 const SmallVectorImpl<ISD::OutputArg>
2830 &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002831 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002832 unsigned &nAltivecParamsAtEnd) {
2833 // Count how many bytes are to be pushed on the stack, including the linkage
2834 // area, and parameter passing area. We start with 24/48 bytes, which is
2835 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002836 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002837 unsigned NumOps = Outs.size();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002838 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2839
2840 // Add up all the space actually used.
2841 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2842 // they all go in registers, but we must reserve stack space for them for
2843 // possible use by the caller. In varargs or 64-bit calls, parameters are
2844 // assigned stack space in order, with padding so Altivec parameters are
2845 // 16-byte aligned.
2846 nAltivecParamsAtEnd = 0;
2847 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002848 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanc9403652010-07-07 15:54:55 +00002849 EVT ArgVT = Outs[i].VT;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002850 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson825b72b2009-08-11 20:47:22 +00002851 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2852 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002853 if (!isVarArg && !isPPC64) {
2854 // Non-varargs Altivec parameters go after all the non-Altivec
2855 // parameters; handle those later so we know how much padding we need.
2856 nAltivecParamsAtEnd++;
2857 continue;
2858 }
2859 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2860 NumBytes = ((NumBytes+15)/16)*16;
2861 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002862 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002863 }
2864
2865 // Allow for Altivec parameters at the end, if needed.
2866 if (nAltivecParamsAtEnd) {
2867 NumBytes = ((NumBytes+15)/16)*16;
2868 NumBytes += 16*nAltivecParamsAtEnd;
2869 }
2870
2871 // The prolog code of the callee may store up to 8 GPR argument registers to
2872 // the stack, allowing va_start to index over them in memory if its varargs.
2873 // Because we cannot tell if this is needed on the caller side, we have to
2874 // conservatively assume that it is needed. As such, make sure we have at
2875 // least enough stack space for the caller to store the 8 GPRs.
2876 NumBytes = std::max(NumBytes,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002877 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002878
2879 // Tail call needs the stack to be aligned.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002880 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
2881 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
2882 getFrameLowering()->getStackAlignment();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002883 unsigned AlignMask = TargetAlign-1;
2884 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2885 }
2886
2887 return NumBytes;
2888}
2889
2890/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002891/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesenb60d5192009-11-24 01:09:07 +00002892static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002893 unsigned ParamSize) {
2894
Dale Johannesenb60d5192009-11-24 01:09:07 +00002895 if (!isTailCall) return 0;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002896
2897 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
2898 unsigned CallerMinReservedArea = FI->getMinReservedArea();
2899 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
2900 // Remember only if the new adjustement is bigger.
2901 if (SPDiff < FI->getTailCallSPDelta())
2902 FI->setTailCallSPDelta(SPDiff);
2903
2904 return SPDiff;
2905}
2906
Dan Gohman98ca4f22009-08-05 01:29:28 +00002907/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2908/// for tail call optimization. Targets which want to do tail call
2909/// optimization should implement this function.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002910bool
Dan Gohman98ca4f22009-08-05 01:29:28 +00002911PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002912 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002913 bool isVarArg,
2914 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002915 SelectionDAG& DAG) const {
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002916 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng6c2e8a92010-01-29 23:05:56 +00002917 return false;
2918
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002919 // Variable argument functions are not supported.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002920 if (isVarArg)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002921 return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002922
Dan Gohman98ca4f22009-08-05 01:29:28 +00002923 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002924 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002925 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
2926 // Functions containing by val parameters are not supported.
2927 for (unsigned i = 0; i != Ins.size(); i++) {
2928 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2929 if (Flags.isByVal()) return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002930 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002931
2932 // Non PIC/GOT tail calls are supported.
2933 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
2934 return true;
2935
2936 // At the moment we can only do local tail calls (in same module, hidden
2937 // or protected) if we are generating PIC.
2938 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2939 return G->getGlobal()->hasHiddenVisibility()
2940 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002941 }
2942
2943 return false;
2944}
2945
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002946/// isCallCompatibleAddress - Return the immediate to use if the specified
2947/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00002948static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002949 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2950 if (!C) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002951
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002952 int Addr = C->getZExtValue();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002953 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith1144af32012-08-24 23:29:28 +00002954 SignExtend32<26>(Addr) != Addr)
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002955 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002956
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002957 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greifba36cb52008-08-28 21:40:38 +00002958 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002959}
2960
Dan Gohman844731a2008-05-13 00:00:25 +00002961namespace {
2962
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002963struct TailCallArgumentInfo {
Dan Gohman475871a2008-07-27 21:46:04 +00002964 SDValue Arg;
2965 SDValue FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002966 int FrameIdx;
2967
2968 TailCallArgumentInfo() : FrameIdx(0) {}
2969};
2970
Dan Gohman844731a2008-05-13 00:00:25 +00002971}
2972
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002973/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
2974static void
2975StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Chengff89dcb2009-10-18 18:16:27 +00002976 SDValue Chain,
Craig Toppera0ec3f92013-07-14 04:42:23 +00002977 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
2978 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002979 SDLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002980 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002981 SDValue Arg = TailCallArgs[i].Arg;
2982 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002983 int FI = TailCallArgs[i].FrameIdx;
2984 // Store relative to framepointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002985 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002986 MachinePointerInfo::getFixedStack(FI),
2987 false, false, 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002988 }
2989}
2990
2991/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
2992/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman475871a2008-07-27 21:46:04 +00002993static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002994 MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002995 SDValue Chain,
2996 SDValue OldRetAddr,
2997 SDValue OldFP,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002998 int SPDiff,
2999 bool isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003000 bool isDarwinABI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003001 SDLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003002 if (SPDiff) {
3003 // Calculate the new stack slot for the return address.
3004 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003005 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003006 isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003007 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003008 NewRetAddrLoc, true);
Owen Anderson825b72b2009-08-11 20:47:22 +00003009 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00003010 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003011 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003012 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene534502d12010-02-15 16:56:53 +00003013 false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003014
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003015 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3016 // slot as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003017 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00003018 int NewFPLoc =
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003019 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene3f2bf852009-11-12 20:49:22 +00003020 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Chenged2ae132010-07-03 00:40:23 +00003021 true);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003022 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3023 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003024 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene534502d12010-02-15 16:56:53 +00003025 false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003026 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003027 }
3028 return Chain;
3029}
3030
3031/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3032/// the position of the argument.
3033static void
3034CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman475871a2008-07-27 21:46:04 +00003035 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003036 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003037 int Offset = ArgOffset + SPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003038 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00003039 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson825b72b2009-08-11 20:47:22 +00003040 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00003041 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003042 TailCallArgumentInfo Info;
3043 Info.Arg = Arg;
3044 Info.FrameIdxOp = FIN;
3045 Info.FrameIdx = FI;
3046 TailCallArguments.push_back(Info);
3047}
3048
3049/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3050/// stack slot. Returns the chain as result and the loaded frame pointers in
3051/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman475871a2008-07-27 21:46:04 +00003052SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003053 int SPDiff,
3054 SDValue Chain,
3055 SDValue &LROpOut,
3056 SDValue &FPOpOut,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003057 bool isDarwinABI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003058 SDLoc dl) const {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003059 if (SPDiff) {
3060 // Load the LR and FP stack slot for later adjusting.
Owen Anderson825b72b2009-08-11 20:47:22 +00003061 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003062 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003063 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003064 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003065 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003066
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003067 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3068 // slot as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003069 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00003070 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003071 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003072 false, false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003073 Chain = SDValue(FPOpOut.getNode(), 1);
3074 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003075 }
3076 return Chain;
3077}
3078
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003079/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelfdc40a02009-02-17 22:15:04 +00003080/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003081/// specified by the specific parameter attribute. The copy will be passed as
3082/// a byval function parameter.
3083/// Sometimes what we are copying is the end of a larger object, the part that
3084/// does not fit in registers.
Scott Michelfdc40a02009-02-17 22:15:04 +00003085static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003086CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00003087 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003088 SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003089 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen8ad9b432009-02-04 01:17:06 +00003090 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Chris Lattnere72f2022010-09-21 05:40:29 +00003091 false, false, MachinePointerInfo(0),
3092 MachinePointerInfo(0));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003093}
Chris Lattner9f0bc652007-02-25 05:34:32 +00003094
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003095/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3096/// tail calls.
3097static void
Dan Gohman475871a2008-07-27 21:46:04 +00003098LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3099 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003100 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003101 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3102 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003103 SDLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00003104 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003105 if (!isTailCall) {
3106 if (isVector) {
Dan Gohman475871a2008-07-27 21:46:04 +00003107 SDValue StackPtr;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003108 if (isPPC64)
Owen Anderson825b72b2009-08-11 20:47:22 +00003109 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003110 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003111 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003112 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003113 DAG.getConstant(ArgOffset, PtrVT));
3114 }
Chris Lattner6229d0a2010-09-21 18:41:36 +00003115 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3116 MachinePointerInfo(), false, false, 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003117 // Calculate and remember argument location.
3118 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3119 TailCallArguments);
3120}
3121
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003122static
3123void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003124 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003125 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003126 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003127 MachineFunction &MF = DAG.getMachineFunction();
3128
3129 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3130 // might overwrite each other in case of tail call optimization.
3131 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00003132 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003133 InFlag = SDValue();
3134 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3135 MemOpChains2, dl);
3136 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00003137 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003138 &MemOpChains2[0], MemOpChains2.size());
3139
3140 // Store the return address to the appropriate stack slot.
3141 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3142 isPPC64, isDarwinABI, dl);
3143
3144 // Emit callseq_end just before tailcall node.
3145 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trick6e0b2a02013-05-29 22:03:55 +00003146 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003147 InFlag = Chain.getValue(1);
3148}
3149
3150static
3151unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003152 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003153 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3154 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerb9082582010-11-14 23:42:06 +00003155 const PPCSubtarget &PPCSubTarget) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003156
Chris Lattnerb9082582010-11-14 23:42:06 +00003157 bool isPPC64 = PPCSubTarget.isPPC64();
3158 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3159
Owen Andersone50ed302009-08-10 22:56:29 +00003160 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00003161 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003162 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003163
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003164 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003165
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003166 bool needIndirectCall = true;
3167 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003168 // If this is an absolute destination address, use the munged value.
3169 Callee = SDValue(Dest, 0);
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003170 needIndirectCall = false;
3171 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003172
Chris Lattnerb9082582010-11-14 23:42:06 +00003173 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3174 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3175 // Use indirect calls for ALL functions calls in JIT mode, since the
3176 // far-call stubs may be outside relocation limits for a BL instruction.
3177 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3178 unsigned OpFlags = 0;
3179 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyd5601cc2011-07-24 08:22:56 +00003180 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00003181 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerb9082582010-11-14 23:42:06 +00003182 (G->getGlobal()->isDeclaration() ||
3183 G->getGlobal()->isWeakForLinker())) {
3184 // PC-relative references to external symbols should go through $stub,
3185 // unless we're building with the leopard linker or later, which
3186 // automatically synthesizes these stubs.
3187 OpFlags = PPCII::MO_DARWIN_STUB;
3188 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003189
Chris Lattnerb9082582010-11-14 23:42:06 +00003190 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3191 // every direct call is) turn it into a TargetGlobalAddress /
3192 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003193 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerb9082582010-11-14 23:42:06 +00003194 Callee.getValueType(),
3195 0, OpFlags);
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003196 needIndirectCall = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003197 }
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003198 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003199
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003200 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerb9082582010-11-14 23:42:06 +00003201 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003202
Chris Lattnerb9082582010-11-14 23:42:06 +00003203 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyd5601cc2011-07-24 08:22:56 +00003204 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00003205 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerb9082582010-11-14 23:42:06 +00003206 // PC-relative references to external symbols should go through $stub,
3207 // unless we're building with the leopard linker or later, which
3208 // automatically synthesizes these stubs.
3209 OpFlags = PPCII::MO_DARWIN_STUB;
3210 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003211
Chris Lattnerb9082582010-11-14 23:42:06 +00003212 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3213 OpFlags);
3214 needIndirectCall = false;
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003215 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003216
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003217 if (needIndirectCall) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003218 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3219 // to do the call, we can't use PPCISD::CALL.
3220 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003221
3222 if (isSVR4ABI && isPPC64) {
3223 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3224 // entry point, but to the function descriptor (the function entry point
3225 // address is part of the function descriptor though).
3226 // The function descriptor is a three doubleword structure with the
3227 // following fields: function entry point, TOC base address and
3228 // environment pointer.
3229 // Thus for a call through a function pointer, the following actions need
3230 // to be performed:
3231 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt726c2372012-10-23 15:51:16 +00003232 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003233 // 2. Load the address of the function entry point from the function
3234 // descriptor.
3235 // 3. Load the TOC of the callee from the function descriptor into r2.
3236 // 4. Load the environment pointer from the function descriptor into
3237 // r11.
3238 // 5. Branch to the function entry point address.
3239 // 6. On return of the callee, the TOC of the caller needs to be
3240 // restored (this is done in FinishCall()).
3241 //
3242 // All those operations are flagged together to ensure that no other
3243 // operations can be scheduled in between. E.g. without flagging the
3244 // operations together, a TOC access in the caller could be scheduled
3245 // between the load of the callee TOC and the branch to the callee, which
3246 // results in the TOC access going through the TOC of the callee instead
3247 // of going through the TOC of the caller, which leads to incorrect code.
3248
3249 // Load the address of the function entry point from the function
3250 // descriptor.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003251 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003252 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, MTCTROps,
3253 InFlag.getNode() ? 3 : 2);
3254 Chain = LoadFuncPtr.getValue(1);
3255 InFlag = LoadFuncPtr.getValue(2);
3256
3257 // Load environment pointer into r11.
3258 // Offset of the environment pointer within the function descriptor.
3259 SDValue PtrOff = DAG.getIntPtrConstant(16);
3260
3261 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3262 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3263 InFlag);
3264 Chain = LoadEnvPtr.getValue(1);
3265 InFlag = LoadEnvPtr.getValue(2);
3266
3267 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3268 InFlag);
3269 Chain = EnvVal.getValue(0);
3270 InFlag = EnvVal.getValue(1);
3271
3272 // Load TOC of the callee into r2. We are using a target-specific load
3273 // with r2 hard coded, because the result of a target-independent load
3274 // would never go directly into r2, since r2 is a reserved register (which
3275 // prevents the register allocator from allocating it), resulting in an
3276 // additional register being allocated and an unnecessary move instruction
3277 // being generated.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003278 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003279 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3280 Callee, InFlag);
3281 Chain = LoadTOCPtr.getValue(0);
3282 InFlag = LoadTOCPtr.getValue(1);
3283
3284 MTCTROps[0] = Chain;
3285 MTCTROps[1] = LoadFuncPtr;
3286 MTCTROps[2] = InFlag;
3287 }
3288
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003289 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
3290 2 + (InFlag.getNode() != 0));
3291 InFlag = Chain.getValue(1);
3292
3293 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00003294 NodeTys.push_back(MVT::Other);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003295 NodeTys.push_back(MVT::Glue);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003296 Ops.push_back(Chain);
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003297 CallOpc = PPCISD::BCTRL;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003298 Callee.setNode(0);
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003299 // Add use of X11 (holding environment pointer)
3300 if (isSVR4ABI && isPPC64)
3301 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003302 // Add CTR register as callee so a bctr can be emitted later.
3303 if (isTailCall)
Roman Divacky0c9b5592011-06-03 15:47:49 +00003304 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003305 }
3306
3307 // If this is a direct call, pass the chain and the callee.
3308 if (Callee.getNode()) {
3309 Ops.push_back(Chain);
3310 Ops.push_back(Callee);
3311 }
3312 // If this is a tail call add stack pointer delta.
3313 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00003314 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003315
3316 // Add argument registers to the end of the list so that they are known live
3317 // into the call.
3318 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3319 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3320 RegsToPass[i].second.getValueType()));
3321
3322 return CallOpc;
3323}
3324
Roman Divackyeb8b7dc2012-09-18 16:47:58 +00003325static
3326bool isLocalCall(const SDValue &Callee)
3327{
3328 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky6fc3ea22012-09-18 18:27:49 +00003329 return !G->getGlobal()->isDeclaration() &&
3330 !G->getGlobal()->isWeakForLinker();
Roman Divackyeb8b7dc2012-09-18 16:47:58 +00003331 return false;
3332}
3333
Dan Gohman98ca4f22009-08-05 01:29:28 +00003334SDValue
3335PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003336 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003337 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003338 SDLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00003339 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003340
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003341 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003342 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00003343 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00003344 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003345
3346 // Copy all of the result registers out of their specified physreg.
3347 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3348 CCValAssign &VA = RVLocs[i];
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003349 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand86aef0a2012-11-05 19:39:45 +00003350
3351 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3352 VA.getLocReg(), VA.getLocVT(), InFlag);
3353 Chain = Val.getValue(1);
3354 InFlag = Val.getValue(2);
3355
3356 switch (VA.getLocInfo()) {
3357 default: llvm_unreachable("Unknown loc info!");
3358 case CCValAssign::Full: break;
3359 case CCValAssign::AExt:
3360 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3361 break;
3362 case CCValAssign::ZExt:
3363 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3364 DAG.getValueType(VA.getValVT()));
3365 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3366 break;
3367 case CCValAssign::SExt:
3368 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3369 DAG.getValueType(VA.getValVT()));
3370 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3371 break;
3372 }
3373
3374 InVals.push_back(Val);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003375 }
3376
Dan Gohman98ca4f22009-08-05 01:29:28 +00003377 return Chain;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003378}
3379
Dan Gohman98ca4f22009-08-05 01:29:28 +00003380SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00003381PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003382 bool isTailCall, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003383 SelectionDAG &DAG,
3384 SmallVector<std::pair<unsigned, SDValue>, 8>
3385 &RegsToPass,
3386 SDValue InFlag, SDValue Chain,
3387 SDValue &Callee,
3388 int SPDiff, unsigned NumBytes,
3389 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohmand858e902010-04-17 15:26:15 +00003390 SmallVectorImpl<SDValue> &InVals) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003391 std::vector<EVT> NodeTys;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003392 SmallVector<SDValue, 8> Ops;
3393 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3394 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerb9082582010-11-14 23:42:06 +00003395 PPCSubTarget);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003396
Hal Finkel82b38212012-08-28 02:10:27 +00003397 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3398 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3399 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3400
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003401 // When performing tail call optimization the callee pops its arguments off
3402 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky700ed802013-02-21 20:05:00 +00003403 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003404 int BytesCalleePops =
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003405 (CallConv == CallingConv::Fast &&
3406 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003407
Roman Divackye46137f2012-03-06 16:41:49 +00003408 // Add a register mask operand representing the call-preserved registers.
3409 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3410 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3411 assert(Mask && "Missing call preserved mask for calling convention");
3412 Ops.push_back(DAG.getRegisterMask(Mask));
3413
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003414 if (InFlag.getNode())
3415 Ops.push_back(InFlag);
3416
3417 // Emit tail call.
3418 if (isTailCall) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003419 assert(((Callee.getOpcode() == ISD::Register &&
3420 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3421 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3422 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3423 isa<ConstantSDNode>(Callee)) &&
3424 "Expecting an global address, external symbol, absolute value or register");
3425
Owen Anderson825b72b2009-08-11 20:47:22 +00003426 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003427 }
3428
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003429 // Add a NOP immediately after the branch instruction when using the 64-bit
3430 // SVR4 ABI. At link time, if caller and callee are in a different module and
3431 // thus have a different TOC, the call will be replaced with a call to a stub
3432 // function which saves the current TOC, loads the TOC of the callee and
3433 // branches to the callee. The NOP will be replaced with a load instruction
3434 // which restores the TOC of the caller from the TOC save slot of the current
3435 // stack frame. If caller and callee belong to the same module (and have the
3436 // same TOC), the NOP will remain unchanged.
Hal Finkel5b00cea2012-03-31 14:45:15 +00003437
3438 bool needsTOCRestore = false;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003439 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003440 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003441 // This is a call through a function pointer.
3442 // Restore the caller TOC from the save area into R2.
3443 // See PrepareCall() for more information about calls through function
3444 // pointers in the 64-bit SVR4 ABI.
3445 // We are using a target-specific load with r2 hard coded, because the
3446 // result of a target-independent load would never go directly into r2,
3447 // since r2 is a reserved register (which prevents the register allocator
3448 // from allocating it), resulting in an additional register being
3449 // allocated and an unnecessary move instruction being generated.
Hal Finkel5b00cea2012-03-31 14:45:15 +00003450 needsTOCRestore = true;
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003451 } else if ((CallOpc == PPCISD::CALL) && !isLocalCall(Callee)) {
Roman Divackyeb8b7dc2012-09-18 16:47:58 +00003452 // Otherwise insert NOP for non-local calls.
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003453 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003454 }
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003455 }
3456
Hal Finkel5b00cea2012-03-31 14:45:15 +00003457 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
3458 InFlag = Chain.getValue(1);
3459
3460 if (needsTOCRestore) {
3461 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3462 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3463 InFlag = Chain.getValue(1);
3464 }
3465
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003466 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3467 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trick6e0b2a02013-05-29 22:03:55 +00003468 InFlag, dl);
Dan Gohman98ca4f22009-08-05 01:29:28 +00003469 if (!Ins.empty())
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003470 InFlag = Chain.getValue(1);
3471
Dan Gohman98ca4f22009-08-05 01:29:28 +00003472 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3473 Ins, dl, DAG, InVals);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003474}
3475
Dan Gohman98ca4f22009-08-05 01:29:28 +00003476SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003477PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00003478 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003479 SelectionDAG &DAG = CLI.DAG;
Craig Toppera0ec3f92013-07-14 04:42:23 +00003480 SDLoc &dl = CLI.DL;
3481 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3482 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3483 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003484 SDValue Chain = CLI.Chain;
3485 SDValue Callee = CLI.Callee;
3486 bool &isTailCall = CLI.IsTailCall;
3487 CallingConv::ID CallConv = CLI.CallConv;
3488 bool isVarArg = CLI.IsVarArg;
3489
Evan Cheng0c439eb2010-01-27 00:07:07 +00003490 if (isTailCall)
3491 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3492 Ins, DAG);
3493
Bill Schmidt726c2372012-10-23 15:51:16 +00003494 if (PPCSubTarget.isSVR4ABI()) {
3495 if (PPCSubTarget.isPPC64())
3496 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3497 isTailCall, Outs, OutVals, Ins,
3498 dl, DAG, InVals);
3499 else
3500 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3501 isTailCall, Outs, OutVals, Ins,
3502 dl, DAG, InVals);
3503 }
Chris Lattnerb9082582010-11-14 23:42:06 +00003504
Bill Schmidt726c2372012-10-23 15:51:16 +00003505 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3506 isTailCall, Outs, OutVals, Ins,
3507 dl, DAG, InVals);
Dan Gohman98ca4f22009-08-05 01:29:28 +00003508}
3509
3510SDValue
Bill Schmidt419f3762012-09-19 15:42:13 +00003511PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3512 CallingConv::ID CallConv, bool isVarArg,
3513 bool isTailCall,
3514 const SmallVectorImpl<ISD::OutputArg> &Outs,
3515 const SmallVectorImpl<SDValue> &OutVals,
3516 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003517 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt419f3762012-09-19 15:42:13 +00003518 SmallVectorImpl<SDValue> &InVals) const {
3519 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003520 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003521
Dan Gohman98ca4f22009-08-05 01:29:28 +00003522 assert((CallConv == CallingConv::C ||
3523 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerffd02002009-07-03 06:45:56 +00003524
Tilmann Schellerffd02002009-07-03 06:45:56 +00003525 unsigned PtrByteSize = 4;
3526
3527 MachineFunction &MF = DAG.getMachineFunction();
3528
3529 // Mark this function as potentially containing a function that contains a
3530 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3531 // and restoring the callers stack pointer in this functions epilog. This is
3532 // done because by tail calling the called function might overwrite the value
3533 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003534 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3535 CallConv == CallingConv::Fast)
Tilmann Schellerffd02002009-07-03 06:45:56 +00003536 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003537
Tilmann Schellerffd02002009-07-03 06:45:56 +00003538 // Count how many bytes are to be pushed on the stack, including the linkage
3539 // area, parameter list area and the part of the local variable space which
3540 // contains copies of aggregates which are passed by value.
3541
3542 // Assign locations to all of the outgoing arguments.
3543 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003544 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00003545 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00003546
3547 // Reserve space for the linkage area on the stack.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003548 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003549
3550 if (isVarArg) {
3551 // Handle fixed and variable vector arguments differently.
3552 // Fixed vector arguments go into registers as long as registers are
3553 // available. Variable vector arguments always go into memory.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003554 unsigned NumArgs = Outs.size();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003555
Tilmann Schellerffd02002009-07-03 06:45:56 +00003556 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00003557 MVT ArgVT = Outs[i].VT;
Dan Gohman98ca4f22009-08-05 01:29:28 +00003558 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerffd02002009-07-03 06:45:56 +00003559 bool Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003560
Dan Gohman98ca4f22009-08-05 01:29:28 +00003561 if (Outs[i].IsFixed) {
Bill Schmidt212af6a2013-02-06 17:33:58 +00003562 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3563 CCInfo);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003564 } else {
Bill Schmidt212af6a2013-02-06 17:33:58 +00003565 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3566 ArgFlags, CCInfo);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003567 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003568
Tilmann Schellerffd02002009-07-03 06:45:56 +00003569 if (Result) {
Torok Edwindac237e2009-07-08 20:53:28 +00003570#ifndef NDEBUG
Chris Lattner45cfe542009-08-23 06:03:38 +00003571 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sands1440e8b2010-11-03 11:35:31 +00003572 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +00003573#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00003574 llvm_unreachable(0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003575 }
3576 }
3577 } else {
3578 // All arguments are treated the same.
Bill Schmidt212af6a2013-02-06 17:33:58 +00003579 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003580 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003581
Tilmann Schellerffd02002009-07-03 06:45:56 +00003582 // Assign locations to all of the outgoing aggregate by value arguments.
3583 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003584 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00003585 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00003586
3587 // Reserve stack space for the allocations in CCInfo.
3588 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3589
Bill Schmidt212af6a2013-02-06 17:33:58 +00003590 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003591
3592 // Size of the linkage area, parameter list area and the part of the local
3593 // space variable where copies of aggregates which are passed by value are
3594 // stored.
3595 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003596
Tilmann Schellerffd02002009-07-03 06:45:56 +00003597 // Calculate by how many bytes the stack has to be adjusted in case of tail
3598 // call optimization.
3599 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3600
3601 // Adjust the stack pointer for the new arguments...
3602 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trick6e0b2a02013-05-29 22:03:55 +00003603 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3604 dl);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003605 SDValue CallSeqStart = Chain;
3606
3607 // Load the return address and frame pointer so it can be moved somewhere else
3608 // later.
3609 SDValue LROp, FPOp;
3610 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3611 dl);
3612
3613 // Set up a copy of the stack pointer for use loading and storing any
3614 // arguments that may not fit in the registers available for argument
3615 // passing.
Owen Anderson825b72b2009-08-11 20:47:22 +00003616 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003617
Tilmann Schellerffd02002009-07-03 06:45:56 +00003618 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3619 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3620 SmallVector<SDValue, 8> MemOpChains;
3621
Roman Divacky0aaa9192011-08-30 17:04:16 +00003622 bool seenFloatArg = false;
Tilmann Schellerffd02002009-07-03 06:45:56 +00003623 // Walk the register/memloc assignments, inserting copies/loads.
3624 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3625 i != e;
3626 ++i) {
3627 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00003628 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00003629 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003630
Tilmann Schellerffd02002009-07-03 06:45:56 +00003631 if (Flags.isByVal()) {
3632 // Argument is an aggregate which is passed by value, thus we need to
3633 // create a copy of it in the local variable space of the current stack
3634 // frame (which is the stack frame of the caller) and pass the address of
3635 // this copy to the callee.
3636 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3637 CCValAssign &ByValVA = ByValArgLocs[j++];
3638 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003639
Tilmann Schellerffd02002009-07-03 06:45:56 +00003640 // Memory reserved in the local variable space of the callers stack frame.
3641 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003642
Tilmann Schellerffd02002009-07-03 06:45:56 +00003643 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3644 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003645
Tilmann Schellerffd02002009-07-03 06:45:56 +00003646 // Create a copy of the argument in the local area of the current
3647 // stack frame.
3648 SDValue MemcpyCall =
3649 CreateCopyOfByValArgument(Arg, PtrOff,
3650 CallSeqStart.getNode()->getOperand(0),
3651 Flags, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003652
Tilmann Schellerffd02002009-07-03 06:45:56 +00003653 // This must go outside the CALLSEQ_START..END.
3654 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00003655 CallSeqStart.getNode()->getOperand(1),
3656 SDLoc(MemcpyCall));
Tilmann Schellerffd02002009-07-03 06:45:56 +00003657 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3658 NewCallSeqStart.getNode());
3659 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003660
Tilmann Schellerffd02002009-07-03 06:45:56 +00003661 // Pass the address of the aggregate copy on the stack either in a
3662 // physical register or in the parameter list area of the current stack
3663 // frame to the callee.
3664 Arg = PtrOff;
3665 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003666
Tilmann Schellerffd02002009-07-03 06:45:56 +00003667 if (VA.isRegLoc()) {
Roman Divacky0aaa9192011-08-30 17:04:16 +00003668 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerffd02002009-07-03 06:45:56 +00003669 // Put argument in a physical register.
3670 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3671 } else {
3672 // Put argument in the parameter list area of the current stack frame.
3673 assert(VA.isMemLoc());
3674 unsigned LocMemOffset = VA.getLocMemOffset();
3675
3676 if (!isTailCall) {
3677 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3678 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3679
3680 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner6229d0a2010-09-21 18:41:36 +00003681 MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00003682 false, false, 0));
Tilmann Schellerffd02002009-07-03 06:45:56 +00003683 } else {
3684 // Calculate and remember argument location.
3685 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3686 TailCallArguments);
3687 }
3688 }
3689 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003690
Tilmann Schellerffd02002009-07-03 06:45:56 +00003691 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00003692 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Schellerffd02002009-07-03 06:45:56 +00003693 &MemOpChains[0], MemOpChains.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003694
Tilmann Schellerffd02002009-07-03 06:45:56 +00003695 // Build a sequence of copy-to-reg nodes chained together with token chain
3696 // and flag operands which copy the outgoing args into the appropriate regs.
3697 SDValue InFlag;
3698 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3699 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3700 RegsToPass[i].second, InFlag);
3701 InFlag = Chain.getValue(1);
3702 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003703
Hal Finkel82b38212012-08-28 02:10:27 +00003704 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3705 // registers.
3706 if (isVarArg) {
NAKAMURA Takumid2a35f22012-08-30 15:52:29 +00003707 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3708 SDValue Ops[] = { Chain, InFlag };
3709
Hal Finkel82b38212012-08-28 02:10:27 +00003710 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
NAKAMURA Takumid2a35f22012-08-30 15:52:29 +00003711 dl, VTs, Ops, InFlag.getNode() ? 2 : 1);
3712
Hal Finkel82b38212012-08-28 02:10:27 +00003713 InFlag = Chain.getValue(1);
3714 }
3715
Chris Lattnerb9082582010-11-14 23:42:06 +00003716 if (isTailCall)
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003717 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3718 false, TailCallArguments);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003719
Dan Gohman98ca4f22009-08-05 01:29:28 +00003720 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3721 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3722 Ins, InVals);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003723}
3724
Bill Schmidt726c2372012-10-23 15:51:16 +00003725// Copy an argument into memory, being careful to do this outside the
3726// call sequence for the call to which the argument belongs.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003727SDValue
Bill Schmidt726c2372012-10-23 15:51:16 +00003728PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3729 SDValue CallSeqStart,
3730 ISD::ArgFlagsTy Flags,
3731 SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003732 SDLoc dl) const {
Bill Schmidt726c2372012-10-23 15:51:16 +00003733 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3734 CallSeqStart.getNode()->getOperand(0),
3735 Flags, DAG, dl);
3736 // The MEMCPY must go outside the CALLSEQ_START..END.
3737 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00003738 CallSeqStart.getNode()->getOperand(1),
3739 SDLoc(MemcpyCall));
Bill Schmidt726c2372012-10-23 15:51:16 +00003740 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3741 NewCallSeqStart.getNode());
3742 return NewCallSeqStart;
3743}
3744
3745SDValue
3746PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003747 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003748 bool isTailCall,
3749 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00003750 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003751 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003752 SDLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00003753 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003754
Bill Schmidt726c2372012-10-23 15:51:16 +00003755 unsigned NumOps = Outs.size();
Bill Schmidt419f3762012-09-19 15:42:13 +00003756
Bill Schmidt726c2372012-10-23 15:51:16 +00003757 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3758 unsigned PtrByteSize = 8;
3759
3760 MachineFunction &MF = DAG.getMachineFunction();
3761
3762 // Mark this function as potentially containing a function that contains a
3763 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3764 // and restoring the callers stack pointer in this functions epilog. This is
3765 // done because by tail calling the called function might overwrite the value
3766 // in this function's (MF) stack pointer stack slot 0(SP).
3767 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3768 CallConv == CallingConv::Fast)
3769 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3770
3771 unsigned nAltivecParamsAtEnd = 0;
3772
3773 // Count how many bytes are to be pushed on the stack, including the linkage
3774 // area, and parameter passing area. We start with at least 48 bytes, which
3775 // is reserved space for [SP][CR][LR][3 x unused].
3776 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
3777 // of this call.
3778 unsigned NumBytes =
3779 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
3780 Outs, OutVals, nAltivecParamsAtEnd);
3781
3782 // Calculate by how many bytes the stack has to be adjusted in case of tail
3783 // call optimization.
3784 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3785
3786 // To protect arguments on the stack from being clobbered in a tail call,
3787 // force all the loads to happen before doing any other lowering.
3788 if (isTailCall)
3789 Chain = DAG.getStackArgumentTokenFactor(Chain);
3790
3791 // Adjust the stack pointer for the new arguments...
3792 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trick6e0b2a02013-05-29 22:03:55 +00003793 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3794 dl);
Bill Schmidt726c2372012-10-23 15:51:16 +00003795 SDValue CallSeqStart = Chain;
3796
3797 // Load the return address and frame pointer so it can be move somewhere else
3798 // later.
3799 SDValue LROp, FPOp;
3800 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
3801 dl);
3802
3803 // Set up a copy of the stack pointer for use loading and storing any
3804 // arguments that may not fit in the registers available for argument
3805 // passing.
3806 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
3807
3808 // Figure out which arguments are going to go in registers, and which in
3809 // memory. Also, if this is a vararg function, floating point operations
3810 // must be stored to our stack, and loaded into integer regs as well, if
3811 // any integer regs are available for argument passing.
3812 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
3813 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
3814
3815 static const uint16_t GPR[] = {
3816 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3817 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3818 };
3819 static const uint16_t *FPR = GetFPR();
3820
3821 static const uint16_t VR[] = {
3822 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3823 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3824 };
3825 const unsigned NumGPRs = array_lengthof(GPR);
3826 const unsigned NumFPRs = 13;
3827 const unsigned NumVRs = array_lengthof(VR);
3828
3829 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3830 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3831
3832 SmallVector<SDValue, 8> MemOpChains;
3833 for (unsigned i = 0; i != NumOps; ++i) {
3834 SDValue Arg = OutVals[i];
3835 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3836
3837 // PtrOff will be used to store the current argument to the stack if a
3838 // register cannot be found for it.
3839 SDValue PtrOff;
3840
3841 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
3842
3843 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
3844
3845 // Promote integers to 64-bit values.
3846 if (Arg.getValueType() == MVT::i32) {
3847 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
3848 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3849 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
3850 }
3851
3852 // FIXME memcpy is used way more than necessary. Correctness first.
3853 // Note: "by value" is code for passing a structure by value, not
3854 // basic types.
3855 if (Flags.isByVal()) {
3856 // Note: Size includes alignment padding, so
3857 // struct x { short a; char b; }
3858 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
3859 // These are the proper values we need for right-justifying the
3860 // aggregate in a parameter register.
3861 unsigned Size = Flags.getByValSize();
Bill Schmidt42d43352012-10-31 01:15:05 +00003862
3863 // An empty aggregate parameter takes up no storage and no
3864 // registers.
3865 if (Size == 0)
3866 continue;
3867
Bill Schmidt726c2372012-10-23 15:51:16 +00003868 // All aggregates smaller than 8 bytes must be passed right-justified.
3869 if (Size==1 || Size==2 || Size==4) {
3870 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
3871 if (GPR_idx != NumGPRs) {
3872 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
3873 MachinePointerInfo(), VT,
3874 false, false, 0);
3875 MemOpChains.push_back(Load.getValue(1));
3876 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3877
3878 ArgOffset += PtrByteSize;
3879 continue;
3880 }
3881 }
3882
3883 if (GPR_idx == NumGPRs && Size < 8) {
3884 SDValue Const = DAG.getConstant(PtrByteSize - Size,
3885 PtrOff.getValueType());
3886 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3887 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3888 CallSeqStart,
3889 Flags, DAG, dl);
3890 ArgOffset += PtrByteSize;
3891 continue;
3892 }
3893 // Copy entire object into memory. There are cases where gcc-generated
3894 // code assumes it is there, even if it could be put entirely into
3895 // registers. (This is not what the doc says.)
3896
3897 // FIXME: The above statement is likely due to a misunderstanding of the
3898 // documents. All arguments must be copied into the parameter area BY
3899 // THE CALLEE in the event that the callee takes the address of any
3900 // formal argument. That has not yet been implemented. However, it is
3901 // reasonable to use the stack area as a staging area for the register
3902 // load.
3903
3904 // Skip this for small aggregates, as we will use the same slot for a
3905 // right-justified copy, below.
3906 if (Size >= 8)
3907 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
3908 CallSeqStart,
3909 Flags, DAG, dl);
3910
3911 // When a register is available, pass a small aggregate right-justified.
3912 if (Size < 8 && GPR_idx != NumGPRs) {
3913 // The easiest way to get this right-justified in a register
3914 // is to copy the structure into the rightmost portion of a
3915 // local variable slot, then load the whole slot into the
3916 // register.
3917 // FIXME: The memcpy seems to produce pretty awful code for
3918 // small aggregates, particularly for packed ones.
Matt Arsenault225ed702013-05-18 00:21:46 +00003919 // FIXME: It would be preferable to use the slot in the
Bill Schmidt726c2372012-10-23 15:51:16 +00003920 // parameter save area instead of a new local variable.
3921 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
3922 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3923 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3924 CallSeqStart,
3925 Flags, DAG, dl);
3926
3927 // Load the slot into the register.
3928 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
3929 MachinePointerInfo(),
3930 false, false, false, 0);
3931 MemOpChains.push_back(Load.getValue(1));
3932 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3933
3934 // Done with this argument.
3935 ArgOffset += PtrByteSize;
3936 continue;
3937 }
3938
3939 // For aggregates larger than PtrByteSize, copy the pieces of the
3940 // object that fit into registers from the parameter save area.
3941 for (unsigned j=0; j<Size; j+=PtrByteSize) {
3942 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
3943 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
3944 if (GPR_idx != NumGPRs) {
3945 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
3946 MachinePointerInfo(),
3947 false, false, false, 0);
3948 MemOpChains.push_back(Load.getValue(1));
3949 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3950 ArgOffset += PtrByteSize;
3951 } else {
3952 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
3953 break;
3954 }
3955 }
3956 continue;
3957 }
3958
3959 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
3960 default: llvm_unreachable("Unexpected ValueType for argument!");
3961 case MVT::i32:
3962 case MVT::i64:
3963 if (GPR_idx != NumGPRs) {
3964 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
3965 } else {
3966 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3967 true, isTailCall, false, MemOpChains,
3968 TailCallArguments, dl);
3969 }
3970 ArgOffset += PtrByteSize;
3971 break;
3972 case MVT::f32:
3973 case MVT::f64:
3974 if (FPR_idx != NumFPRs) {
3975 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
3976
3977 if (isVarArg) {
Bill Schmidte6c56432012-10-29 21:18:16 +00003978 // A single float or an aggregate containing only a single float
3979 // must be passed right-justified in the stack doubleword, and
3980 // in the GPR, if one is available.
3981 SDValue StoreOff;
3982 if (Arg.getValueType().getSimpleVT().SimpleTy == MVT::f32) {
3983 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
3984 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
3985 } else
3986 StoreOff = PtrOff;
3987
3988 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt726c2372012-10-23 15:51:16 +00003989 MachinePointerInfo(), false, false, 0);
3990 MemOpChains.push_back(Store);
3991
3992 // Float varargs are always shadowed in available integer registers
3993 if (GPR_idx != NumGPRs) {
3994 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
3995 MachinePointerInfo(), false, false,
3996 false, 0);
3997 MemOpChains.push_back(Load.getValue(1));
3998 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3999 }
4000 } else if (GPR_idx != NumGPRs)
4001 // If we have any FPRs remaining, we may also have GPRs remaining.
4002 ++GPR_idx;
4003 } else {
4004 // Single-precision floating-point values are mapped to the
4005 // second (rightmost) word of the stack doubleword.
4006 if (Arg.getValueType() == MVT::f32) {
4007 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4008 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4009 }
4010
4011 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4012 true, isTailCall, false, MemOpChains,
4013 TailCallArguments, dl);
4014 }
4015 ArgOffset += 8;
4016 break;
4017 case MVT::v4f32:
4018 case MVT::v4i32:
4019 case MVT::v8i16:
4020 case MVT::v16i8:
4021 if (isVarArg) {
4022 // These go aligned on the stack, or in the corresponding R registers
4023 // when within range. The Darwin PPC ABI doc claims they also go in
4024 // V registers; in fact gcc does this only for arguments that are
4025 // prototyped, not for those that match the ... We do it for all
4026 // arguments, seems to work.
4027 while (ArgOffset % 16 !=0) {
4028 ArgOffset += PtrByteSize;
4029 if (GPR_idx != NumGPRs)
4030 GPR_idx++;
4031 }
4032 // We could elide this store in the case where the object fits
4033 // entirely in R registers. Maybe later.
4034 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
4035 DAG.getConstant(ArgOffset, PtrVT));
4036 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4037 MachinePointerInfo(), false, false, 0);
4038 MemOpChains.push_back(Store);
4039 if (VR_idx != NumVRs) {
4040 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4041 MachinePointerInfo(),
4042 false, false, false, 0);
4043 MemOpChains.push_back(Load.getValue(1));
4044 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4045 }
4046 ArgOffset += 16;
4047 for (unsigned i=0; i<16; i+=PtrByteSize) {
4048 if (GPR_idx == NumGPRs)
4049 break;
4050 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4051 DAG.getConstant(i, PtrVT));
4052 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4053 false, false, false, 0);
4054 MemOpChains.push_back(Load.getValue(1));
4055 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4056 }
4057 break;
4058 }
4059
4060 // Non-varargs Altivec params generally go in registers, but have
4061 // stack space allocated at the end.
4062 if (VR_idx != NumVRs) {
4063 // Doesn't have GPR space allocated.
4064 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4065 } else {
4066 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4067 true, isTailCall, true, MemOpChains,
4068 TailCallArguments, dl);
4069 ArgOffset += 16;
4070 }
4071 break;
4072 }
4073 }
4074
4075 if (!MemOpChains.empty())
4076 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4077 &MemOpChains[0], MemOpChains.size());
4078
4079 // Check if this is an indirect call (MTCTR/BCTRL).
4080 // See PrepareCall() for more information about calls through function
4081 // pointers in the 64-bit SVR4 ABI.
4082 if (!isTailCall &&
4083 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4084 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4085 !isBLACompatibleAddress(Callee, DAG)) {
4086 // Load r2 into a virtual register and store it to the TOC save area.
4087 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4088 // TOC save area offset.
4089 SDValue PtrOff = DAG.getIntPtrConstant(40);
4090 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4091 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4092 false, false, 0);
4093 // R12 must contain the address of an indirect callee. This does not
4094 // mean the MTCTR instruction must use R12; it's easier to model this
4095 // as an extra parameter, so do that.
4096 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4097 }
4098
4099 // Build a sequence of copy-to-reg nodes chained together with token chain
4100 // and flag operands which copy the outgoing args into the appropriate regs.
4101 SDValue InFlag;
4102 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4103 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4104 RegsToPass[i].second, InFlag);
4105 InFlag = Chain.getValue(1);
4106 }
4107
4108 if (isTailCall)
4109 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4110 FPOp, true, TailCallArguments);
4111
4112 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4113 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4114 Ins, InVals);
4115}
4116
4117SDValue
4118PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4119 CallingConv::ID CallConv, bool isVarArg,
4120 bool isTailCall,
4121 const SmallVectorImpl<ISD::OutputArg> &Outs,
4122 const SmallVectorImpl<SDValue> &OutVals,
4123 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004124 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt726c2372012-10-23 15:51:16 +00004125 SmallVectorImpl<SDValue> &InVals) const {
4126
4127 unsigned NumOps = Outs.size();
Scott Michelfdc40a02009-02-17 22:15:04 +00004128
Owen Andersone50ed302009-08-10 22:56:29 +00004129 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00004130 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerc91a4752006-06-26 22:48:35 +00004131 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004132
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004133 MachineFunction &MF = DAG.getMachineFunction();
4134
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004135 // Mark this function as potentially containing a function that contains a
4136 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4137 // and restoring the callers stack pointer in this functions epilog. This is
4138 // done because by tail calling the called function might overwrite the value
4139 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky8a8d4792011-12-02 22:16:29 +00004140 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4141 CallConv == CallingConv::Fast)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004142 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4143
4144 unsigned nAltivecParamsAtEnd = 0;
4145
Chris Lattnerabde4602006-05-16 22:56:08 +00004146 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerc91a4752006-06-26 22:48:35 +00004147 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004148 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004149 unsigned NumBytes =
Dan Gohman98ca4f22009-08-05 01:29:28 +00004150 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanc9403652010-07-07 15:54:55 +00004151 Outs, OutVals,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004152 nAltivecParamsAtEnd);
Dale Johannesen75092de2008-03-12 00:22:17 +00004153
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004154 // Calculate by how many bytes the stack has to be adjusted in case of tail
4155 // call optimization.
4156 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelfdc40a02009-02-17 22:15:04 +00004157
Dan Gohman98ca4f22009-08-05 01:29:28 +00004158 // To protect arguments on the stack from being clobbered in a tail call,
4159 // force all the loads to happen before doing any other lowering.
4160 if (isTailCall)
4161 Chain = DAG.getStackArgumentTokenFactor(Chain);
4162
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004163 // Adjust the stack pointer for the new arguments...
4164 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trick6e0b2a02013-05-29 22:03:55 +00004165 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4166 dl);
Dan Gohman475871a2008-07-27 21:46:04 +00004167 SDValue CallSeqStart = Chain;
Scott Michelfdc40a02009-02-17 22:15:04 +00004168
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004169 // Load the return address and frame pointer so it can be move somewhere else
4170 // later.
Dan Gohman475871a2008-07-27 21:46:04 +00004171 SDValue LROp, FPOp;
Tilmann Schellerffd02002009-07-03 06:45:56 +00004172 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4173 dl);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004174
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004175 // Set up a copy of the stack pointer for use loading and storing any
4176 // arguments that may not fit in the registers available for argument
4177 // passing.
Dan Gohman475871a2008-07-27 21:46:04 +00004178 SDValue StackPtr;
Chris Lattnerc91a4752006-06-26 22:48:35 +00004179 if (isPPC64)
Owen Anderson825b72b2009-08-11 20:47:22 +00004180 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerc91a4752006-06-26 22:48:35 +00004181 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00004183
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004184 // Figure out which arguments are going to go in registers, and which in
4185 // memory. Also, if this is a vararg function, floating point operations
4186 // must be stored to our stack, and loaded into integer regs as well, if
4187 // any integer regs are available for argument passing.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00004188 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004189 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004190
Craig Topperb78ca422012-03-11 07:16:55 +00004191 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner9a2a4972006-05-17 06:01:33 +00004192 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4193 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4194 };
Craig Topperb78ca422012-03-11 07:16:55 +00004195 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerc91a4752006-06-26 22:48:35 +00004196 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4197 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4198 };
Craig Topperb78ca422012-03-11 07:16:55 +00004199 static const uint16_t *FPR = GetFPR();
Scott Michelfdc40a02009-02-17 22:15:04 +00004200
Craig Topperb78ca422012-03-11 07:16:55 +00004201 static const uint16_t VR[] = {
Chris Lattner9a2a4972006-05-17 06:01:33 +00004202 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4203 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4204 };
Owen Anderson718cb662007-09-07 04:06:50 +00004205 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004206 const unsigned NumFPRs = 13;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00004207 const unsigned NumVRs = array_lengthof(VR);
Scott Michelfdc40a02009-02-17 22:15:04 +00004208
Craig Topperb78ca422012-03-11 07:16:55 +00004209 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerc91a4752006-06-26 22:48:35 +00004210
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004211 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004212 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4213
Dan Gohman475871a2008-07-27 21:46:04 +00004214 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng4360bdc2006-05-25 00:57:32 +00004215 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00004216 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00004217 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00004218
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004219 // PtrOff will be used to store the current argument to the stack if a
4220 // register cannot be found for it.
Dan Gohman475871a2008-07-27 21:46:04 +00004221 SDValue PtrOff;
Scott Michelfdc40a02009-02-17 22:15:04 +00004222
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004223 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00004224
Dale Johannesen39355f92009-02-04 02:34:38 +00004225 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerc91a4752006-06-26 22:48:35 +00004226
4227 // On PPC64, promote integers to 64-bit values.
Owen Anderson825b72b2009-08-11 20:47:22 +00004228 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00004229 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4230 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson825b72b2009-08-11 20:47:22 +00004231 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00004232 }
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004233
Dale Johannesen8419dd62008-03-07 20:27:40 +00004234 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt419f3762012-09-19 15:42:13 +00004235 // Note: "by value" is code for passing a structure by value, not
4236 // basic types.
Duncan Sands276dcbd2008-03-21 09:14:45 +00004237 if (Flags.isByVal()) {
4238 unsigned Size = Flags.getByValSize();
Bill Schmidt726c2372012-10-23 15:51:16 +00004239 // Very small objects are passed right-justified. Everything else is
4240 // passed left-justified.
4241 if (Size==1 || Size==2) {
4242 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesen8419dd62008-03-07 20:27:40 +00004243 if (GPR_idx != NumGPRs) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004244 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004245 MachinePointerInfo(), VT,
4246 false, false, 0);
Dale Johannesen8419dd62008-03-07 20:27:40 +00004247 MemOpChains.push_back(Load.getValue(1));
4248 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004249
4250 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00004251 } else {
Bill Schmidt7a6cb152012-10-16 13:30:53 +00004252 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4253 PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00004254 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt726c2372012-10-23 15:51:16 +00004255 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4256 CallSeqStart,
4257 Flags, DAG, dl);
Dale Johannesen8419dd62008-03-07 20:27:40 +00004258 ArgOffset += PtrByteSize;
4259 }
4260 continue;
4261 }
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00004262 // Copy entire object into memory. There are cases where gcc-generated
4263 // code assumes it is there, even if it could be put entirely into
4264 // registers. (This is not what the doc says.)
Bill Schmidt726c2372012-10-23 15:51:16 +00004265 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4266 CallSeqStart,
4267 Flags, DAG, dl);
Bill Schmidt419f3762012-09-19 15:42:13 +00004268
4269 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4270 // copy the pieces of the object that fit into registers from the
4271 // parameter save area.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004272 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00004273 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00004274 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004275 if (GPR_idx != NumGPRs) {
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004276 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4277 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004278 false, false, false, 0);
Dale Johannesen1f797a32008-03-05 23:31:27 +00004279 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004280 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004281 ArgOffset += PtrByteSize;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004282 } else {
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00004283 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00004284 break;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004285 }
4286 }
4287 continue;
4288 }
4289
Owen Anderson825b72b2009-08-11 20:47:22 +00004290 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004291 default: llvm_unreachable("Unexpected ValueType for argument!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004292 case MVT::i32:
4293 case MVT::i64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00004294 if (GPR_idx != NumGPRs) {
4295 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004296 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004297 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4298 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004299 TailCallArguments, dl);
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004300 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004301 ArgOffset += PtrByteSize;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004302 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004303 case MVT::f32:
4304 case MVT::f64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00004305 if (FPR_idx != NumFPRs) {
4306 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4307
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004308 if (isVarArg) {
Chris Lattner6229d0a2010-09-21 18:41:36 +00004309 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4310 MachinePointerInfo(), false, false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004311 MemOpChains.push_back(Store);
4312
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004313 // Float varargs are always shadowed in available integer registers
Chris Lattner9a2a4972006-05-17 06:01:33 +00004314 if (GPR_idx != NumGPRs) {
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004315 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004316 MachinePointerInfo(), false, false,
4317 false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004318 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004319 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004320 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004321 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman475871a2008-07-27 21:46:04 +00004322 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00004323 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004324 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4325 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004326 false, false, false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004327 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004328 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerabde4602006-05-16 22:56:08 +00004329 }
4330 } else {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004331 // If we have any FPRs remaining, we may also have GPRs remaining.
4332 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4333 // GPRs.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004334 if (GPR_idx != NumGPRs)
4335 ++GPR_idx;
Owen Anderson825b72b2009-08-11 20:47:22 +00004336 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004337 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4338 ++GPR_idx;
Chris Lattnerabde4602006-05-16 22:56:08 +00004339 }
Bill Schmidt726c2372012-10-23 15:51:16 +00004340 } else
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004341 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4342 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004343 TailCallArguments, dl);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004344 if (isPPC64)
4345 ArgOffset += 8;
4346 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004347 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004348 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004349 case MVT::v4f32:
4350 case MVT::v4i32:
4351 case MVT::v8i16:
4352 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00004353 if (isVarArg) {
4354 // These go aligned on the stack, or in the corresponding R registers
Scott Michelfdc40a02009-02-17 22:15:04 +00004355 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesen75092de2008-03-12 00:22:17 +00004356 // V registers; in fact gcc does this only for arguments that are
4357 // prototyped, not for those that match the ... We do it for all
4358 // arguments, seems to work.
4359 while (ArgOffset % 16 !=0) {
4360 ArgOffset += PtrByteSize;
4361 if (GPR_idx != NumGPRs)
4362 GPR_idx++;
4363 }
4364 // We could elide this store in the case where the object fits
4365 // entirely in R registers. Maybe later.
Scott Michelfdc40a02009-02-17 22:15:04 +00004366 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesen75092de2008-03-12 00:22:17 +00004367 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner6229d0a2010-09-21 18:41:36 +00004368 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4369 MachinePointerInfo(), false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00004370 MemOpChains.push_back(Store);
4371 if (VR_idx != NumVRs) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004372 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004373 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004374 false, false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00004375 MemOpChains.push_back(Load.getValue(1));
4376 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4377 }
4378 ArgOffset += 16;
4379 for (unsigned i=0; i<16; i+=PtrByteSize) {
4380 if (GPR_idx == NumGPRs)
4381 break;
Dale Johannesen39355f92009-02-04 02:34:38 +00004382 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesen75092de2008-03-12 00:22:17 +00004383 DAG.getConstant(i, PtrVT));
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004384 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004385 false, false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00004386 MemOpChains.push_back(Load.getValue(1));
4387 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4388 }
4389 break;
4390 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004391
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004392 // Non-varargs Altivec params generally go in registers, but have
4393 // stack space allocated at the end.
4394 if (VR_idx != NumVRs) {
4395 // Doesn't have GPR space allocated.
4396 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4397 } else if (nAltivecParamsAtEnd==0) {
4398 // We are emitting Altivec params in order.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004399 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4400 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004401 TailCallArguments, dl);
Dale Johannesen75092de2008-03-12 00:22:17 +00004402 ArgOffset += 16;
Dale Johannesen75092de2008-03-12 00:22:17 +00004403 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004404 break;
Chris Lattnerabde4602006-05-16 22:56:08 +00004405 }
Chris Lattnerabde4602006-05-16 22:56:08 +00004406 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004407 // If all Altivec parameters fit in registers, as they usually do,
4408 // they get stack space following the non-Altivec parameters. We
4409 // don't track this here because nobody below needs it.
4410 // If there are more Altivec parameters than fit in registers emit
4411 // the stores here.
4412 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4413 unsigned j = 0;
4414 // Offset is aligned; skip 1st 12 params which go in V registers.
4415 ArgOffset = ((ArgOffset+15)/16)*16;
4416 ArgOffset += 12*16;
4417 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00004418 SDValue Arg = OutVals[i];
4419 EVT ArgType = Outs[i].VT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004420 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4421 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004422 if (++j > NumVRs) {
Dan Gohman475871a2008-07-27 21:46:04 +00004423 SDValue PtrOff;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004424 // We are emitting Altivec params in order.
4425 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4426 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004427 TailCallArguments, dl);
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004428 ArgOffset += 16;
4429 }
4430 }
4431 }
4432 }
4433
Chris Lattner9a2a4972006-05-17 06:01:33 +00004434 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00004435 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnere2199452006-08-11 17:38:39 +00004436 &MemOpChains[0], MemOpChains.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00004437
Dale Johannesenf7b73042010-03-09 20:15:42 +00004438 // On Darwin, R12 must contain the address of an indirect callee. This does
4439 // not mean the MTCTR instruction must use R12; it's easier to model this as
4440 // an extra parameter, so do that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004441 if (!isTailCall &&
Dale Johannesenf7b73042010-03-09 20:15:42 +00004442 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4443 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4444 !isBLACompatibleAddress(Callee, DAG))
4445 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4446 PPC::R12), Callee));
4447
Chris Lattner9a2a4972006-05-17 06:01:33 +00004448 // Build a sequence of copy-to-reg nodes chained together with token chain
4449 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00004450 SDValue InFlag;
Chris Lattner9a2a4972006-05-17 06:01:33 +00004451 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004452 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen39355f92009-02-04 02:34:38 +00004453 RegsToPass[i].second, InFlag);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004454 InFlag = Chain.getValue(1);
4455 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004456
Chris Lattnerb9082582010-11-14 23:42:06 +00004457 if (isTailCall)
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004458 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4459 FPOp, true, TailCallArguments);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004460
Dan Gohman98ca4f22009-08-05 01:29:28 +00004461 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4462 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4463 Ins, InVals);
Chris Lattnerabde4602006-05-16 22:56:08 +00004464}
4465
Hal Finkeld712f932011-10-14 19:51:36 +00004466bool
4467PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4468 MachineFunction &MF, bool isVarArg,
4469 const SmallVectorImpl<ISD::OutputArg> &Outs,
4470 LLVMContext &Context) const {
4471 SmallVector<CCValAssign, 16> RVLocs;
4472 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4473 RVLocs, Context);
4474 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4475}
4476
Dan Gohman98ca4f22009-08-05 01:29:28 +00004477SDValue
4478PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00004479 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004480 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00004481 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004482 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00004483
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004484 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00004485 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00004486 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004487 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelfdc40a02009-02-17 22:15:04 +00004488
Dan Gohman475871a2008-07-27 21:46:04 +00004489 SDValue Flag;
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004490 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00004491
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004492 // Copy the result values into the output registers.
4493 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4494 CCValAssign &VA = RVLocs[i];
4495 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand86aef0a2012-11-05 19:39:45 +00004496
4497 SDValue Arg = OutVals[i];
4498
4499 switch (VA.getLocInfo()) {
4500 default: llvm_unreachable("Unknown loc info!");
4501 case CCValAssign::Full: break;
4502 case CCValAssign::AExt:
4503 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4504 break;
4505 case CCValAssign::ZExt:
4506 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4507 break;
4508 case CCValAssign::SExt:
4509 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4510 break;
4511 }
4512
4513 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004514 Flag = Chain.getValue(1);
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004515 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004516 }
4517
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004518 RetOps[0] = Chain; // Update chain.
4519
4520 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00004521 if (Flag.getNode())
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004522 RetOps.push_back(Flag);
4523
4524 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other,
4525 &RetOps[0], RetOps.size());
Chris Lattner1a635d62006-04-14 06:01:58 +00004526}
4527
Dan Gohman475871a2008-07-27 21:46:04 +00004528SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004529 const PPCSubtarget &Subtarget) const {
Jim Laskeyefc7e522006-12-04 22:04:42 +00004530 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004531 SDLoc dl(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004532
Jim Laskeyefc7e522006-12-04 22:04:42 +00004533 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00004534 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeyefc7e522006-12-04 22:04:42 +00004535
4536 // Construct the stack pointer operand.
Dale Johannesenb60d5192009-11-24 01:09:07 +00004537 bool isPPC64 = Subtarget.isPPC64();
4538 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman475871a2008-07-27 21:46:04 +00004539 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeyefc7e522006-12-04 22:04:42 +00004540
4541 // Get the operands for the STACKRESTORE.
Dan Gohman475871a2008-07-27 21:46:04 +00004542 SDValue Chain = Op.getOperand(0);
4543 SDValue SaveSP = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004544
Jim Laskeyefc7e522006-12-04 22:04:42 +00004545 // Load the old link SP.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004546 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4547 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004548 false, false, false, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004549
Jim Laskeyefc7e522006-12-04 22:04:42 +00004550 // Restore the stack pointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004551 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelfdc40a02009-02-17 22:15:04 +00004552
Jim Laskeyefc7e522006-12-04 22:04:42 +00004553 // Store the old link SP.
Chris Lattner6229d0a2010-09-21 18:41:36 +00004554 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00004555 false, false, 0);
Jim Laskeyefc7e522006-12-04 22:04:42 +00004556}
4557
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004558
4559
Dan Gohman475871a2008-07-27 21:46:04 +00004560SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004561PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00004562 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesenb60d5192009-11-24 01:09:07 +00004563 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004564 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00004565 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004566
4567 // Get current frame pointer save index. The users of this index will be
4568 // primarily DYNALLOC instructions.
4569 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4570 int RASI = FI->getReturnAddrSaveIndex();
4571
4572 // If the frame pointer save index hasn't been defined yet.
4573 if (!RASI) {
4574 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00004575 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004576 // Allocate the frame index for frame pointer save area.
Evan Chenged2ae132010-07-03 00:40:23 +00004577 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004578 // Save the result.
4579 FI->setReturnAddrSaveIndex(RASI);
4580 }
4581 return DAG.getFrameIndex(RASI, PtrVT);
4582}
4583
Dan Gohman475871a2008-07-27 21:46:04 +00004584SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004585PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4586 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesenb60d5192009-11-24 01:09:07 +00004587 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004588 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00004589 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00004590
4591 // Get current frame pointer save index. The users of this index will be
4592 // primarily DYNALLOC instructions.
4593 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4594 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004595
Jim Laskey2f616bf2006-11-16 22:43:37 +00004596 // If the frame pointer save index hasn't been defined yet.
4597 if (!FPSI) {
4598 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00004599 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004600 isDarwinABI);
Scott Michelfdc40a02009-02-17 22:15:04 +00004601
Jim Laskey2f616bf2006-11-16 22:43:37 +00004602 // Allocate the frame index for frame pointer save area.
Evan Chenged2ae132010-07-03 00:40:23 +00004603 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004604 // Save the result.
Scott Michelfdc40a02009-02-17 22:15:04 +00004605 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004606 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004607 return DAG.getFrameIndex(FPSI, PtrVT);
4608}
Jim Laskey2f616bf2006-11-16 22:43:37 +00004609
Dan Gohman475871a2008-07-27 21:46:04 +00004610SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004611 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004612 const PPCSubtarget &Subtarget) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00004613 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004614 SDValue Chain = Op.getOperand(0);
4615 SDValue Size = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004616 SDLoc dl(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004617
Jim Laskey2f616bf2006-11-16 22:43:37 +00004618 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00004619 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00004620 // Negate the size.
Dale Johannesende064702009-02-06 21:50:26 +00004621 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey2f616bf2006-11-16 22:43:37 +00004622 DAG.getConstant(0, PtrVT), Size);
4623 // Construct a node for the frame pointer save index.
Dan Gohman475871a2008-07-27 21:46:04 +00004624 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004625 // Build a DYNALLOC node.
Dan Gohman475871a2008-07-27 21:46:04 +00004626 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson825b72b2009-08-11 20:47:22 +00004627 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesende064702009-02-06 21:50:26 +00004628 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004629}
4630
Hal Finkel7ee74a62013-03-21 21:37:52 +00004631SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
4632 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004633 SDLoc DL(Op);
Hal Finkel7ee74a62013-03-21 21:37:52 +00004634 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
4635 DAG.getVTList(MVT::i32, MVT::Other),
4636 Op.getOperand(0), Op.getOperand(1));
4637}
4638
4639SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
4640 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004641 SDLoc DL(Op);
Hal Finkel7ee74a62013-03-21 21:37:52 +00004642 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
4643 Op.getOperand(0), Op.getOperand(1));
4644}
4645
Chris Lattner1a635d62006-04-14 06:01:58 +00004646/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4647/// possible.
Dan Gohmand858e902010-04-17 15:26:15 +00004648SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner1a635d62006-04-14 06:01:58 +00004649 // Not FP? Not a fsel.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004650 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4651 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedmanc06441e2009-05-28 04:31:08 +00004652 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004653
Hal Finkel59889f72013-04-07 22:11:09 +00004654 // We might be able to do better than this under some circumstances, but in
4655 // general, fsel-based lowering of select is a finite-math-only optimization.
4656 // For more information, see section F.3 of the 2.06 ISA specification.
4657 if (!DAG.getTarget().Options.NoInfsFPMath ||
4658 !DAG.getTarget().Options.NoNaNsFPMath)
4659 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004660
Hal Finkel59889f72013-04-07 22:11:09 +00004661 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004662
Owen Andersone50ed302009-08-10 22:56:29 +00004663 EVT ResVT = Op.getValueType();
4664 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004665 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4666 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004667 SDLoc dl(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004668
Chris Lattner1a635d62006-04-14 06:01:58 +00004669 // If the RHS of the comparison is a 0.0, we don't need to do the
4670 // subtraction at all.
Hal Finkel59889f72013-04-07 22:11:09 +00004671 SDValue Sel1;
Chris Lattner1a635d62006-04-14 06:01:58 +00004672 if (isFloatingPointZero(RHS))
4673 switch (CC) {
4674 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel59889f72013-04-07 22:11:09 +00004675 case ISD::SETNE:
4676 std::swap(TV, FV);
4677 case ISD::SETEQ:
4678 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4679 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
4680 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
4681 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4682 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4683 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4684 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004685 case ISD::SETULT:
4686 case ISD::SETLT:
4687 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00004688 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004689 case ISD::SETGE:
Owen Anderson825b72b2009-08-11 20:47:22 +00004690 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4691 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00004692 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004693 case ISD::SETUGT:
4694 case ISD::SETGT:
4695 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00004696 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004697 case ISD::SETLE:
Owen Anderson825b72b2009-08-11 20:47:22 +00004698 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4699 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00004700 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00004701 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004702 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004703
Dan Gohman475871a2008-07-27 21:46:04 +00004704 SDValue Cmp;
Chris Lattner1a635d62006-04-14 06:01:58 +00004705 switch (CC) {
4706 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel59889f72013-04-07 22:11:09 +00004707 case ISD::SETNE:
4708 std::swap(TV, FV);
4709 case ISD::SETEQ:
4710 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
4711 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4712 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
4713 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
4714 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4715 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4716 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4717 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004718 case ISD::SETULT:
4719 case ISD::SETLT:
Dale Johannesende064702009-02-06 21:50:26 +00004720 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004721 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4722 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel59889f72013-04-07 22:11:09 +00004723 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00004724 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004725 case ISD::SETGE:
Dale Johannesende064702009-02-06 21:50:26 +00004726 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004727 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4728 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel59889f72013-04-07 22:11:09 +00004729 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004730 case ISD::SETUGT:
4731 case ISD::SETGT:
Dale Johannesende064702009-02-06 21:50:26 +00004732 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004733 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4734 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel59889f72013-04-07 22:11:09 +00004735 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00004736 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004737 case ISD::SETLE:
Dale Johannesende064702009-02-06 21:50:26 +00004738 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004739 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4740 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel59889f72013-04-07 22:11:09 +00004741 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004742 }
Eli Friedmanc06441e2009-05-28 04:31:08 +00004743 return Op;
Chris Lattner1a635d62006-04-14 06:01:58 +00004744}
4745
Chris Lattner1f873002007-11-28 18:44:47 +00004746// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004747SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004748 SDLoc dl) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004749 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman475871a2008-07-27 21:46:04 +00004750 SDValue Src = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00004751 if (Src.getValueType() == MVT::f32)
4752 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sandsa7360f02008-07-19 16:26:02 +00004753
Dan Gohman475871a2008-07-27 21:46:04 +00004754 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00004755 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004756 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004757 case MVT::i32:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004758 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Hal Finkel46479192013-04-01 17:52:07 +00004759 (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ :
4760 PPCISD::FCTIDZ),
Owen Anderson825b72b2009-08-11 20:47:22 +00004761 dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00004762 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004763 case MVT::i64:
Hal Finkela1646ce2013-04-01 18:42:58 +00004764 assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) &&
4765 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkel46479192013-04-01 17:52:07 +00004766 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
4767 PPCISD::FCTIDUZ,
4768 dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00004769 break;
4770 }
Duncan Sandsa7360f02008-07-19 16:26:02 +00004771
Chris Lattner1a635d62006-04-14 06:01:58 +00004772 // Convert the FP value to an int value through memory.
Hal Finkel46479192013-04-01 17:52:07 +00004773 bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() &&
4774 (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT());
4775 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
4776 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
4777 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sandsa7360f02008-07-19 16:26:02 +00004778
Chris Lattner1de7c1d2007-10-15 20:14:52 +00004779 // Emit a store to the stack slot.
Hal Finkel46479192013-04-01 17:52:07 +00004780 SDValue Chain;
4781 if (i32Stack) {
4782 MachineFunction &MF = DAG.getMachineFunction();
4783 MachineMemOperand *MMO =
4784 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
4785 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
4786 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
4787 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
4788 MVT::i32, MMO);
4789 } else
4790 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
4791 MPI, false, false, 0);
Chris Lattner1de7c1d2007-10-15 20:14:52 +00004792
4793 // Result is a load from the stack slot. If loading 4 bytes, make sure to
4794 // add in a bias.
Hal Finkel46479192013-04-01 17:52:07 +00004795 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004796 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner1de7c1d2007-10-15 20:14:52 +00004797 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkel46479192013-04-01 17:52:07 +00004798 MPI = MachinePointerInfo();
4799 }
4800
4801 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004802 false, false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004803}
4804
Hal Finkel46479192013-04-01 17:52:07 +00004805SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004806 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004807 SDLoc dl(Op);
Dan Gohman034f60e2008-03-11 01:59:03 +00004808 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson825b72b2009-08-11 20:47:22 +00004809 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman475871a2008-07-27 21:46:04 +00004810 return SDValue();
Dan Gohman034f60e2008-03-11 01:59:03 +00004811
Hal Finkel46479192013-04-01 17:52:07 +00004812 assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) &&
4813 "UINT_TO_FP is supported only with FPCVT");
4814
4815 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel2a401952013-04-02 03:29:51 +00004816 // Otherwise, convert to double-precision and then round.
Hal Finkel46479192013-04-01 17:52:07 +00004817 unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4818 (Op.getOpcode() == ISD::UINT_TO_FP ?
4819 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
4820 (Op.getOpcode() == ISD::UINT_TO_FP ?
4821 PPCISD::FCFIDU : PPCISD::FCFID);
4822 MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4823 MVT::f32 : MVT::f64;
4824
Owen Anderson825b72b2009-08-11 20:47:22 +00004825 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigand6c28a7e2012-10-18 13:16:11 +00004826 SDValue SINT = Op.getOperand(0);
4827 // When converting to single-precision, we actually need to convert
4828 // to double-precision first and then round to single-precision.
4829 // To avoid double-rounding effects during that operation, we have
4830 // to prepare the input operand. Bits that might be truncated when
4831 // converting to double-precision are replaced by a bit that won't
4832 // be lost at this stage, but is below the single-precision rounding
4833 // position.
4834 //
4835 // However, if -enable-unsafe-fp-math is in effect, accept double
4836 // rounding to avoid the extra overhead.
4837 if (Op.getValueType() == MVT::f32 &&
Hal Finkel46479192013-04-01 17:52:07 +00004838 !PPCSubTarget.hasFPCVT() &&
Ulrich Weigand6c28a7e2012-10-18 13:16:11 +00004839 !DAG.getTarget().Options.UnsafeFPMath) {
4840
4841 // Twiddle input to make sure the low 11 bits are zero. (If this
4842 // is the case, we are guaranteed the value will fit into the 53 bit
4843 // mantissa of an IEEE double-precision value without rounding.)
4844 // If any of those low 11 bits were not zero originally, make sure
4845 // bit 12 (value 2048) is set instead, so that the final rounding
4846 // to single-precision gets the correct result.
4847 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4848 SINT, DAG.getConstant(2047, MVT::i64));
4849 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
4850 Round, DAG.getConstant(2047, MVT::i64));
4851 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
4852 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4853 Round, DAG.getConstant(-2048, MVT::i64));
4854
4855 // However, we cannot use that value unconditionally: if the magnitude
4856 // of the input value is small, the bit-twiddling we did above might
4857 // end up visibly changing the output. Fortunately, in that case, we
4858 // don't need to twiddle bits since the original input will convert
4859 // exactly to double-precision floating-point already. Therefore,
4860 // construct a conditional to use the original value if the top 11
4861 // bits are all sign-bit copies, and use the rounded value computed
4862 // above otherwise.
4863 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
4864 SINT, DAG.getConstant(53, MVT::i32));
4865 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
4866 Cond, DAG.getConstant(1, MVT::i64));
4867 Cond = DAG.getSetCC(dl, MVT::i32,
4868 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
4869
4870 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
4871 }
Hal Finkel46479192013-04-01 17:52:07 +00004872
Ulrich Weigand6c28a7e2012-10-18 13:16:11 +00004873 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkel46479192013-04-01 17:52:07 +00004874 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
4875
4876 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Scott Michelfdc40a02009-02-17 22:15:04 +00004877 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004878 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00004879 return FP;
4880 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004881
Owen Anderson825b72b2009-08-11 20:47:22 +00004882 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkel46479192013-04-01 17:52:07 +00004883 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner1a635d62006-04-14 06:01:58 +00004884 // Since we only generate this in 64-bit mode, we can take advantage of
4885 // 64-bit registers. In particular, sign extend the input value into the
4886 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
4887 // then lfd it and fcfid it.
Dan Gohmanc76909a2009-09-25 20:36:54 +00004888 MachineFunction &MF = DAG.getMachineFunction();
4889 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004890 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfdc40a02009-02-17 22:15:04 +00004891
Hal Finkel8049ab12013-03-31 10:12:51 +00004892 SDValue Ld;
Hal Finkel46479192013-04-01 17:52:07 +00004893 if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) {
Hal Finkel8049ab12013-03-31 10:12:51 +00004894 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
4895 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004896
Hal Finkel8049ab12013-03-31 10:12:51 +00004897 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
4898 MachinePointerInfo::getFixedStack(FrameIdx),
4899 false, false, 0);
Hal Finkel9ad0f492013-03-31 01:58:02 +00004900
Hal Finkel8049ab12013-03-31 10:12:51 +00004901 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
4902 "Expected an i32 store");
4903 MachineMemOperand *MMO =
4904 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
4905 MachineMemOperand::MOLoad, 4, 4);
4906 SDValue Ops[] = { Store, FIdx };
Hal Finkel46479192013-04-01 17:52:07 +00004907 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
4908 PPCISD::LFIWZX : PPCISD::LFIWAX,
4909 dl, DAG.getVTList(MVT::f64, MVT::Other),
4910 Ops, 2, MVT::i32, MMO);
Hal Finkel8049ab12013-03-31 10:12:51 +00004911 } else {
Hal Finkel46479192013-04-01 17:52:07 +00004912 assert(PPCSubTarget.isPPC64() &&
4913 "i32->FP without LFIWAX supported only on PPC64");
4914
Hal Finkel8049ab12013-03-31 10:12:51 +00004915 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
4916 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
4917
4918 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
4919 Op.getOperand(0));
4920
4921 // STD the extended value into the stack slot.
4922 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
4923 MachinePointerInfo::getFixedStack(FrameIdx),
4924 false, false, 0);
4925
4926 // Load the value as a double.
4927 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
4928 MachinePointerInfo::getFixedStack(FrameIdx),
4929 false, false, false, 0);
4930 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004931
Chris Lattner1a635d62006-04-14 06:01:58 +00004932 // FCFID it and return it.
Hal Finkel46479192013-04-01 17:52:07 +00004933 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
4934 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Owen Anderson825b72b2009-08-11 20:47:22 +00004935 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00004936 return FP;
4937}
4938
Dan Gohmand858e902010-04-17 15:26:15 +00004939SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
4940 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004941 SDLoc dl(Op);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004942 /*
4943 The rounding mode is in bits 30:31 of FPSR, and has the following
4944 settings:
4945 00 Round to nearest
4946 01 Round to 0
4947 10 Round to +inf
4948 11 Round to -inf
4949
4950 FLT_ROUNDS, on the other hand, expects the following:
4951 -1 Undefined
4952 0 Round to 0
4953 1 Round to nearest
4954 2 Round to +inf
4955 3 Round to -inf
4956
4957 To perform the conversion, we do:
4958 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
4959 */
4960
4961 MachineFunction &MF = DAG.getMachineFunction();
Owen Andersone50ed302009-08-10 22:56:29 +00004962 EVT VT = Op.getValueType();
4963 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00004964 SDValue MFFSreg, InFlag;
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004965
4966 // Save FP Control Word to register
Benjamin Kramer3853f742013-03-07 20:33:29 +00004967 EVT NodeTys[] = {
4968 MVT::f64, // return register
4969 MVT::Glue // unused in this context
4970 };
Dale Johannesen33c960f2009-02-04 20:06:27 +00004971 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004972
4973 // Save FP register to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00004974 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00004975 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00004976 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner6229d0a2010-09-21 18:41:36 +00004977 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004978
4979 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00004980 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00004981 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004982 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004983 false, false, false, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004984
4985 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00004986 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00004987 DAG.getNode(ISD::AND, dl, MVT::i32,
4988 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00004989 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00004990 DAG.getNode(ISD::SRL, dl, MVT::i32,
4991 DAG.getNode(ISD::AND, dl, MVT::i32,
4992 DAG.getNode(ISD::XOR, dl, MVT::i32,
4993 CWD, DAG.getConstant(3, MVT::i32)),
4994 DAG.getConstant(3, MVT::i32)),
4995 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004996
Dan Gohman475871a2008-07-27 21:46:04 +00004997 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00004998 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004999
Duncan Sands83ec4b62008-06-06 12:08:01 +00005000 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen33c960f2009-02-04 20:06:27 +00005001 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00005002}
5003
Dan Gohmand858e902010-04-17 15:26:15 +00005004SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005005 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005006 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005007 SDLoc dl(Op);
Dan Gohman9ed06db2008-03-07 20:36:53 +00005008 assert(Op.getNumOperands() == 3 &&
5009 VT == Op.getOperand(1).getValueType() &&
5010 "Unexpected SHL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005011
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00005012 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00005013 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00005014 SDValue Lo = Op.getOperand(0);
5015 SDValue Hi = Op.getOperand(1);
5016 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005017 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005018
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005019 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005020 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005021 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5022 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5023 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5024 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005025 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005026 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5027 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5028 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00005029 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005030 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00005031}
5032
Dan Gohmand858e902010-04-17 15:26:15 +00005033SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005034 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005035 SDLoc dl(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005036 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00005037 assert(Op.getNumOperands() == 3 &&
5038 VT == Op.getOperand(1).getValueType() &&
5039 "Unexpected SRL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005040
Dan Gohman9ed06db2008-03-07 20:36:53 +00005041 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00005042 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00005043 SDValue Lo = Op.getOperand(0);
5044 SDValue Hi = Op.getOperand(1);
5045 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005046 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005047
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005048 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005049 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005050 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5051 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5052 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5053 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005054 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005055 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5056 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5057 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00005058 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005059 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00005060}
5061
Dan Gohmand858e902010-04-17 15:26:15 +00005062SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005063 SDLoc dl(Op);
Owen Andersone50ed302009-08-10 22:56:29 +00005064 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005065 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00005066 assert(Op.getNumOperands() == 3 &&
5067 VT == Op.getOperand(1).getValueType() &&
5068 "Unexpected SRA!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005069
Dan Gohman9ed06db2008-03-07 20:36:53 +00005070 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman475871a2008-07-27 21:46:04 +00005071 SDValue Lo = Op.getOperand(0);
5072 SDValue Hi = Op.getOperand(1);
5073 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005074 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005075
Dale Johannesenf5d97892009-02-04 01:48:28 +00005076 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005077 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf5d97892009-02-04 01:48:28 +00005078 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5079 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5080 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5081 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005082 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf5d97892009-02-04 01:48:28 +00005083 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5084 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5085 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005086 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman475871a2008-07-27 21:46:04 +00005087 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005088 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00005089}
5090
5091//===----------------------------------------------------------------------===//
5092// Vector related lowering.
5093//
5094
Chris Lattner4a998b92006-04-17 06:00:21 +00005095/// BuildSplatI - Build a canonical splati of Val with an element size of
5096/// SplatSize. Cast the result to VT.
Owen Andersone50ed302009-08-10 22:56:29 +00005097static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005098 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner4a998b92006-04-17 06:00:21 +00005099 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner70fa4932006-12-01 01:45:39 +00005100
Owen Andersone50ed302009-08-10 22:56:29 +00005101 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson825b72b2009-08-11 20:47:22 +00005102 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner4a998b92006-04-17 06:00:21 +00005103 };
Chris Lattner70fa4932006-12-01 01:45:39 +00005104
Owen Anderson825b72b2009-08-11 20:47:22 +00005105 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00005106
Chris Lattner70fa4932006-12-01 01:45:39 +00005107 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5108 if (Val == -1)
5109 SplatSize = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00005110
Owen Andersone50ed302009-08-10 22:56:29 +00005111 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00005112
Chris Lattner4a998b92006-04-17 06:00:21 +00005113 // Build a canonical splat for this value.
Owen Anderson825b72b2009-08-11 20:47:22 +00005114 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00005115 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005116 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga87008d2009-02-25 22:49:59 +00005117 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
5118 &Ops[0], Ops.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005119 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00005120}
5121
Hal Finkel80d10de2013-05-24 23:00:14 +00005122/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5123/// specified intrinsic ID.
5124static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005125 SelectionDAG &DAG, SDLoc dl,
Hal Finkel80d10de2013-05-24 23:00:14 +00005126 EVT DestVT = MVT::Other) {
5127 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5128 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5129 DAG.getConstant(IID, MVT::i32), Op);
5130}
5131
Chris Lattnere7c768e2006-04-18 03:24:30 +00005132/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00005133/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00005134static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005135 SelectionDAG &DAG, SDLoc dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005136 EVT DestVT = MVT::Other) {
5137 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00005138 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005139 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner6876e662006-04-17 06:58:41 +00005140}
5141
Chris Lattnere7c768e2006-04-18 03:24:30 +00005142/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5143/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00005144static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesened2eee62009-02-06 01:31:28 +00005145 SDValue Op2, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005146 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005147 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00005148 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005149 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnere7c768e2006-04-18 03:24:30 +00005150}
5151
5152
Chris Lattnerbdd558c2006-04-17 17:55:10 +00005153/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5154/// amount. The result has the specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005155static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005156 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattnerbdd558c2006-04-17 17:55:10 +00005157 // Force LHS/RHS to be the right type.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005158 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5159 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsd038e042008-07-21 10:20:31 +00005160
Nate Begeman9008ca62009-04-27 18:41:29 +00005161 int Ops[16];
Chris Lattnerbdd558c2006-04-17 17:55:10 +00005162 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005163 Ops[i] = i + Amt;
Owen Anderson825b72b2009-08-11 20:47:22 +00005164 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005165 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00005166}
5167
Chris Lattnerf1b47082006-04-14 05:19:18 +00005168// If this is a case we can't handle, return null and let the default
5169// expansion code take care of it. If we CAN select this case, and if it
5170// selects to a single instruction, return Op. Otherwise, if we can codegen
5171// this case more efficiently than a constant pool load, lower it to the
5172// sequence of ops that should be used.
Dan Gohmand858e902010-04-17 15:26:15 +00005173SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5174 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005175 SDLoc dl(Op);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005176 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5177 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Micheldf380432009-02-25 03:12:50 +00005178
Bob Wilson24e338e2009-03-02 23:24:16 +00005179 // Check if this is a splat of a constant value.
5180 APInt APSplatBits, APSplatUndef;
5181 unsigned SplatBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005182 bool HasAnyUndefs;
Bob Wilsonf2950b02009-03-03 19:26:27 +00005183 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen1e608812009-11-13 01:45:18 +00005184 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilsonf2950b02009-03-03 19:26:27 +00005185 return SDValue();
Evan Chenga87008d2009-02-25 22:49:59 +00005186
Bob Wilsonf2950b02009-03-03 19:26:27 +00005187 unsigned SplatBits = APSplatBits.getZExtValue();
5188 unsigned SplatUndef = APSplatUndef.getZExtValue();
5189 unsigned SplatSize = SplatBitSize / 8;
Scott Michelfdc40a02009-02-17 22:15:04 +00005190
Bob Wilsonf2950b02009-03-03 19:26:27 +00005191 // First, handle single instruction cases.
5192
5193 // All zeros?
5194 if (SplatBits == 0) {
5195 // Canonicalize all zero vectors to be v4i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005196 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5197 SDValue Z = DAG.getConstant(0, MVT::i32);
5198 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005199 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattnerf1b47082006-04-14 05:19:18 +00005200 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00005201 return Op;
5202 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00005203
Bob Wilsonf2950b02009-03-03 19:26:27 +00005204 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5205 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5206 (32-SplatBitSize));
5207 if (SextVal >= -16 && SextVal <= 15)
5208 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005209
5210
Bob Wilsonf2950b02009-03-03 19:26:27 +00005211 // Two instruction sequences.
Scott Michelfdc40a02009-02-17 22:15:04 +00005212
Bob Wilsonf2950b02009-03-03 19:26:27 +00005213 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtabc40282013-02-20 20:41:42 +00005214 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5215 // If this value is in the range [17,31] and is odd, use:
5216 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5217 // If this value is in the range [-31,-17] and is odd, use:
5218 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5219 // Note the last two are three-instruction sequences.
5220 if (SextVal >= -32 && SextVal <= 31) {
5221 // To avoid having these optimizations undone by constant folding,
5222 // we convert to a pseudo that will be expanded later into one of
5223 // the above forms.
5224 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidtb34c79e2013-02-20 15:50:31 +00005225 EVT VT = Op.getValueType();
5226 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5227 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5228 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005229 }
5230
5231 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5232 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5233 // for fneg/fabs.
5234 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5235 // Make -1 and vspltisw -1:
Owen Anderson825b72b2009-08-11 20:47:22 +00005236 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005237
5238 // Make the VSLW intrinsic, computing 0x8000_0000.
5239 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5240 OnesV, DAG, dl);
5241
5242 // xor by OnesV to invert it.
Owen Anderson825b72b2009-08-11 20:47:22 +00005243 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005244 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005245 }
5246
5247 // Check to see if this is a wide variety of vsplti*, binop self cases.
5248 static const signed char SplatCsts[] = {
5249 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5250 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5251 };
5252
5253 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5254 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5255 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5256 int i = SplatCsts[idx];
5257
5258 // Figure out what shift amount will be used by altivec if shifted by i in
5259 // this splat size.
5260 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5261
5262 // vsplti + shl self.
Richard Smith1144af32012-08-24 23:29:28 +00005263 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005264 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005265 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5266 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5267 Intrinsic::ppc_altivec_vslw
5268 };
5269 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005270 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00005271 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005272
Bob Wilsonf2950b02009-03-03 19:26:27 +00005273 // vsplti + srl self.
5274 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005275 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005276 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5277 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5278 Intrinsic::ppc_altivec_vsrw
5279 };
5280 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005281 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00005282 }
5283
Bob Wilsonf2950b02009-03-03 19:26:27 +00005284 // vsplti + sra self.
5285 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005286 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005287 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5288 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5289 Intrinsic::ppc_altivec_vsraw
5290 };
5291 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005292 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00005293 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005294
Bob Wilsonf2950b02009-03-03 19:26:27 +00005295 // vsplti + rol self.
5296 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5297 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005298 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005299 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5300 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5301 Intrinsic::ppc_altivec_vrlw
5302 };
5303 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005304 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005305 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005306
Bob Wilsonf2950b02009-03-03 19:26:27 +00005307 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith1144af32012-08-24 23:29:28 +00005308 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005309 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005310 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnerdbce85d2006-04-17 18:09:22 +00005311 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00005312 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith1144af32012-08-24 23:29:28 +00005313 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005314 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005315 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattnerf1b47082006-04-14 05:19:18 +00005316 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00005317 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith1144af32012-08-24 23:29:28 +00005318 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005319 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005320 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5321 }
5322 }
5323
Dan Gohman475871a2008-07-27 21:46:04 +00005324 return SDValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00005325}
5326
Chris Lattner59138102006-04-17 05:28:54 +00005327/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5328/// the specified operations to build the shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00005329static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00005330 SDValue RHS, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005331 SDLoc dl) {
Chris Lattner59138102006-04-17 05:28:54 +00005332 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling77959322008-09-17 00:30:57 +00005333 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner59138102006-04-17 05:28:54 +00005334 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005335
Chris Lattner59138102006-04-17 05:28:54 +00005336 enum {
Chris Lattner00402c72006-05-16 04:20:24 +00005337 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 +00005338 OP_VMRGHW,
5339 OP_VMRGLW,
5340 OP_VSPLTISW0,
5341 OP_VSPLTISW1,
5342 OP_VSPLTISW2,
5343 OP_VSPLTISW3,
5344 OP_VSLDOI4,
5345 OP_VSLDOI8,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00005346 OP_VSLDOI12
Chris Lattner59138102006-04-17 05:28:54 +00005347 };
Scott Michelfdc40a02009-02-17 22:15:04 +00005348
Chris Lattner59138102006-04-17 05:28:54 +00005349 if (OpNum == OP_COPY) {
5350 if (LHSID == (1*9+2)*9+3) return LHS;
5351 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5352 return RHS;
5353 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005354
Dan Gohman475871a2008-07-27 21:46:04 +00005355 SDValue OpLHS, OpRHS;
Dale Johannesened2eee62009-02-06 01:31:28 +00005356 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5357 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005358
Nate Begeman9008ca62009-04-27 18:41:29 +00005359 int ShufIdxs[16];
Chris Lattner59138102006-04-17 05:28:54 +00005360 switch (OpNum) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005361 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner59138102006-04-17 05:28:54 +00005362 case OP_VMRGHW:
5363 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5364 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5365 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5366 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5367 break;
5368 case OP_VMRGLW:
5369 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5370 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5371 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5372 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5373 break;
5374 case OP_VSPLTISW0:
5375 for (unsigned i = 0; i != 16; ++i)
5376 ShufIdxs[i] = (i&3)+0;
5377 break;
5378 case OP_VSPLTISW1:
5379 for (unsigned i = 0; i != 16; ++i)
5380 ShufIdxs[i] = (i&3)+4;
5381 break;
5382 case OP_VSPLTISW2:
5383 for (unsigned i = 0; i != 16; ++i)
5384 ShufIdxs[i] = (i&3)+8;
5385 break;
5386 case OP_VSPLTISW3:
5387 for (unsigned i = 0; i != 16; ++i)
5388 ShufIdxs[i] = (i&3)+12;
5389 break;
5390 case OP_VSLDOI4:
Dale Johannesened2eee62009-02-06 01:31:28 +00005391 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005392 case OP_VSLDOI8:
Dale Johannesened2eee62009-02-06 01:31:28 +00005393 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005394 case OP_VSLDOI12:
Dale Johannesened2eee62009-02-06 01:31:28 +00005395 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005396 }
Owen Andersone50ed302009-08-10 22:56:29 +00005397 EVT VT = OpLHS.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005398 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5399 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00005400 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005401 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner59138102006-04-17 05:28:54 +00005402}
5403
Chris Lattnerf1b47082006-04-14 05:19:18 +00005404/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5405/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5406/// return the code it can be lowered into. Worst case, it can always be
5407/// lowered into a vperm.
Scott Michelfdc40a02009-02-17 22:15:04 +00005408SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005409 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005410 SDLoc dl(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005411 SDValue V1 = Op.getOperand(0);
5412 SDValue V2 = Op.getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005413 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Andersone50ed302009-08-10 22:56:29 +00005414 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005415
Chris Lattnerf1b47082006-04-14 05:19:18 +00005416 // Cases that are handled by instructions that take permute immediates
5417 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5418 // selected by the instruction selector.
5419 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005420 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5421 PPC::isSplatShuffleMask(SVOp, 2) ||
5422 PPC::isSplatShuffleMask(SVOp, 4) ||
5423 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5424 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5425 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5426 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5427 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5428 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5429 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5430 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5431 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00005432 return Op;
5433 }
5434 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005435
Chris Lattnerf1b47082006-04-14 05:19:18 +00005436 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5437 // and produce a fixed permutation. If any of these match, do not lower to
5438 // VPERM.
Nate Begeman9008ca62009-04-27 18:41:29 +00005439 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5440 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5441 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5442 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5443 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5444 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5445 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5446 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5447 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattnerf1b47082006-04-14 05:19:18 +00005448 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005449
Chris Lattner59138102006-04-17 05:28:54 +00005450 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5451 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005452 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005453
Chris Lattner59138102006-04-17 05:28:54 +00005454 unsigned PFIndexes[4];
5455 bool isFourElementShuffle = true;
5456 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5457 unsigned EltNo = 8; // Start out undef.
5458 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman9008ca62009-04-27 18:41:29 +00005459 if (PermMask[i*4+j] < 0)
Chris Lattner59138102006-04-17 05:28:54 +00005460 continue; // Undef, ignore it.
Scott Michelfdc40a02009-02-17 22:15:04 +00005461
Nate Begeman9008ca62009-04-27 18:41:29 +00005462 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner59138102006-04-17 05:28:54 +00005463 if ((ByteSource & 3) != j) {
5464 isFourElementShuffle = false;
5465 break;
5466 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005467
Chris Lattner59138102006-04-17 05:28:54 +00005468 if (EltNo == 8) {
5469 EltNo = ByteSource/4;
5470 } else if (EltNo != ByteSource/4) {
5471 isFourElementShuffle = false;
5472 break;
5473 }
5474 }
5475 PFIndexes[i] = EltNo;
5476 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005477
5478 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner59138102006-04-17 05:28:54 +00005479 // perfect shuffle vector to determine if it is cost effective to do this as
5480 // discrete instructions, or whether we should use a vperm.
5481 if (isFourElementShuffle) {
5482 // Compute the index in the perfect shuffle table.
Scott Michelfdc40a02009-02-17 22:15:04 +00005483 unsigned PFTableIndex =
Chris Lattner59138102006-04-17 05:28:54 +00005484 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelfdc40a02009-02-17 22:15:04 +00005485
Chris Lattner59138102006-04-17 05:28:54 +00005486 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5487 unsigned Cost = (PFEntry >> 30);
Scott Michelfdc40a02009-02-17 22:15:04 +00005488
Chris Lattner59138102006-04-17 05:28:54 +00005489 // Determining when to avoid vperm is tricky. Many things affect the cost
5490 // of vperm, particularly how many times the perm mask needs to be computed.
5491 // For example, if the perm mask can be hoisted out of a loop or is already
5492 // used (perhaps because there are multiple permutes with the same shuffle
5493 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5494 // the loop requires an extra register.
5495 //
5496 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelfdc40a02009-02-17 22:15:04 +00005497 // generated in 3 or fewer operations. When we have loop information
Chris Lattner59138102006-04-17 05:28:54 +00005498 // available, if this block is within a loop, we should avoid using vperm
5499 // for 3-operation perms and use a constant pool load instead.
Scott Michelfdc40a02009-02-17 22:15:04 +00005500 if (Cost < 3)
Dale Johannesened2eee62009-02-06 01:31:28 +00005501 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005502 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005503
Chris Lattnerf1b47082006-04-14 05:19:18 +00005504 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5505 // vector that will get spilled to the constant pool.
5506 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelfdc40a02009-02-17 22:15:04 +00005507
Chris Lattnerf1b47082006-04-14 05:19:18 +00005508 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5509 // that it is in input element units, not in bytes. Convert now.
Owen Andersone50ed302009-08-10 22:56:29 +00005510 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005511 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelfdc40a02009-02-17 22:15:04 +00005512
Dan Gohman475871a2008-07-27 21:46:04 +00005513 SmallVector<SDValue, 16> ResultMask;
Nate Begeman9008ca62009-04-27 18:41:29 +00005514 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5515 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00005516
Chris Lattnerf1b47082006-04-14 05:19:18 +00005517 for (unsigned j = 0; j != BytesPerElement; ++j)
5518 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson825b72b2009-08-11 20:47:22 +00005519 MVT::i32));
Chris Lattnerf1b47082006-04-14 05:19:18 +00005520 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005521
Owen Anderson825b72b2009-08-11 20:47:22 +00005522 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga87008d2009-02-25 22:49:59 +00005523 &ResultMask[0], ResultMask.size());
Dale Johannesened2eee62009-02-06 01:31:28 +00005524 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattnerf1b47082006-04-14 05:19:18 +00005525}
5526
Chris Lattner90564f22006-04-18 17:59:36 +00005527/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5528/// altivec comparison. If it is, return true and fill in Opc/isDot with
5529/// information about the intrinsic.
Dan Gohman475871a2008-07-27 21:46:04 +00005530static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner90564f22006-04-18 17:59:36 +00005531 bool &isDot) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005532 unsigned IntrinsicID =
5533 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00005534 CompareOpc = -1;
5535 isDot = false;
5536 switch (IntrinsicID) {
5537 default: return false;
5538 // Comparison predicates.
Chris Lattner1a635d62006-04-14 06:01:58 +00005539 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5540 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5541 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5542 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5543 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5544 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5545 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5546 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5547 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5548 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5549 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5550 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5551 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005552
Chris Lattner1a635d62006-04-14 06:01:58 +00005553 // Normal Comparisons.
5554 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5555 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5556 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5557 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5558 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5559 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5560 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5561 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5562 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5563 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5564 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5565 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5566 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5567 }
Chris Lattner90564f22006-04-18 17:59:36 +00005568 return true;
5569}
5570
5571/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5572/// lower, do it, otherwise return null.
Scott Michelfdc40a02009-02-17 22:15:04 +00005573SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005574 SelectionDAG &DAG) const {
Chris Lattner90564f22006-04-18 17:59:36 +00005575 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5576 // opcode number of the comparison.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005577 SDLoc dl(Op);
Chris Lattner90564f22006-04-18 17:59:36 +00005578 int CompareOpc;
5579 bool isDot;
5580 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman475871a2008-07-27 21:46:04 +00005581 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelfdc40a02009-02-17 22:15:04 +00005582
Chris Lattner90564f22006-04-18 17:59:36 +00005583 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner1a635d62006-04-14 06:01:58 +00005584 if (!isDot) {
Dale Johannesen3484c092009-02-05 22:07:54 +00005585 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner149add02010-03-14 22:44:11 +00005586 Op.getOperand(1), Op.getOperand(2),
5587 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005588 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner1a635d62006-04-14 06:01:58 +00005589 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005590
Chris Lattner1a635d62006-04-14 06:01:58 +00005591 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman475871a2008-07-27 21:46:04 +00005592 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00005593 Op.getOperand(2), // LHS
5594 Op.getOperand(3), // RHS
Owen Anderson825b72b2009-08-11 20:47:22 +00005595 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00005596 };
Benjamin Kramer3853f742013-03-07 20:33:29 +00005597 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesen3484c092009-02-05 22:07:54 +00005598 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00005599
Chris Lattner1a635d62006-04-14 06:01:58 +00005600 // Now that we have the comparison, emit a copy from the CR to a GPR.
5601 // This is flagged to the above dot comparison.
Ulrich Weigand965b20e2013-07-03 17:05:42 +00005602 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005603 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelfdc40a02009-02-17 22:15:04 +00005604 CompNode.getValue(1));
5605
Chris Lattner1a635d62006-04-14 06:01:58 +00005606 // Unpack the result based on how the target uses it.
5607 unsigned BitNo; // Bit # of CR6.
5608 bool InvertBit; // Invert result?
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005609 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner1a635d62006-04-14 06:01:58 +00005610 default: // Can't happen, don't crash on invalid number though.
5611 case 0: // Return the value of the EQ bit of CR6.
5612 BitNo = 0; InvertBit = false;
5613 break;
5614 case 1: // Return the inverted value of the EQ bit of CR6.
5615 BitNo = 0; InvertBit = true;
5616 break;
5617 case 2: // Return the value of the LT bit of CR6.
5618 BitNo = 2; InvertBit = false;
5619 break;
5620 case 3: // Return the inverted value of the LT bit of CR6.
5621 BitNo = 2; InvertBit = true;
5622 break;
5623 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005624
Chris Lattner1a635d62006-04-14 06:01:58 +00005625 // Shift the bit into the low position.
Owen Anderson825b72b2009-08-11 20:47:22 +00005626 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5627 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00005628 // Isolate the bit.
Owen Anderson825b72b2009-08-11 20:47:22 +00005629 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5630 DAG.getConstant(1, MVT::i32));
Scott Michelfdc40a02009-02-17 22:15:04 +00005631
Chris Lattner1a635d62006-04-14 06:01:58 +00005632 // If we are supposed to, toggle the bit.
5633 if (InvertBit)
Owen Anderson825b72b2009-08-11 20:47:22 +00005634 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5635 DAG.getConstant(1, MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00005636 return Flags;
5637}
5638
Scott Michelfdc40a02009-02-17 22:15:04 +00005639SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005640 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005641 SDLoc dl(Op);
Chris Lattner1a635d62006-04-14 06:01:58 +00005642 // Create a stack slot that is 16-byte aligned.
5643 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00005644 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen08673d22010-05-03 22:59:34 +00005645 EVT PtrVT = getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00005646 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005647
Chris Lattner1a635d62006-04-14 06:01:58 +00005648 // Store the input value into Value#0 of the stack slot.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005649 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00005650 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00005651 false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00005652 // Load it out.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00005653 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005654 false, false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00005655}
5656
Dan Gohmand858e902010-04-17 15:26:15 +00005657SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005658 SDLoc dl(Op);
Owen Anderson825b72b2009-08-11 20:47:22 +00005659 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00005660 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005661
Owen Anderson825b72b2009-08-11 20:47:22 +00005662 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
5663 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelfdc40a02009-02-17 22:15:04 +00005664
Dan Gohman475871a2008-07-27 21:46:04 +00005665 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesened2eee62009-02-06 01:31:28 +00005666 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005667
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005668 // Shrinkify inputs to v8i16.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005669 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
5670 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
5671 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelfdc40a02009-02-17 22:15:04 +00005672
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005673 // Low parts multiplied together, generating 32-bit results (we ignore the
5674 // top parts).
Dan Gohman475871a2008-07-27 21:46:04 +00005675 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson825b72b2009-08-11 20:47:22 +00005676 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00005677
Dan Gohman475871a2008-07-27 21:46:04 +00005678 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson825b72b2009-08-11 20:47:22 +00005679 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005680 // Shift the high parts up 16 bits.
Scott Michelfdc40a02009-02-17 22:15:04 +00005681 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesened2eee62009-02-06 01:31:28 +00005682 Neg16, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00005683 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
5684 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005685 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005686
Owen Anderson825b72b2009-08-11 20:47:22 +00005687 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005688
Chris Lattnercea2aa72006-04-18 04:28:57 +00005689 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesened2eee62009-02-06 01:31:28 +00005690 LHS, RHS, Zero, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00005691 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005692 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005693
Chris Lattner19a81522006-04-18 03:57:35 +00005694 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00005695 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson825b72b2009-08-11 20:47:22 +00005696 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005697 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00005698
Chris Lattner19a81522006-04-18 03:57:35 +00005699 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00005700 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson825b72b2009-08-11 20:47:22 +00005701 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005702 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00005703
Chris Lattner19a81522006-04-18 03:57:35 +00005704 // Merge the results together.
Nate Begeman9008ca62009-04-27 18:41:29 +00005705 int Ops[16];
Chris Lattner19a81522006-04-18 03:57:35 +00005706 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005707 Ops[i*2 ] = 2*i+1;
5708 Ops[i*2+1] = 2*i+1+16;
Chris Lattner19a81522006-04-18 03:57:35 +00005709 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005710 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005711 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00005712 llvm_unreachable("Unknown mul to lower!");
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005713 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00005714}
5715
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00005716/// LowerOperation - Provide custom lowering hooks for some operations.
5717///
Dan Gohmand858e902010-04-17 15:26:15 +00005718SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00005719 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005720 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00005721 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson3d90dbe2009-11-04 21:31:18 +00005722 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00005723 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackyfd42ed62012-06-04 17:36:38 +00005724 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman37efe672006-04-22 18:53:45 +00005725 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00005726 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +00005727 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
5728 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005729 case ISD::VASTART:
Dan Gohman1e93df62010-04-17 14:41:14 +00005730 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelfdc40a02009-02-17 22:15:04 +00005731
5732 case ISD::VAARG:
Dan Gohman1e93df62010-04-17 14:41:14 +00005733 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray01119992007-04-03 13:59:52 +00005734
Roman Divacky6ebf55d2013-07-25 21:36:47 +00005735 case ISD::VACOPY:
5736 return LowerVACOPY(Op, DAG, PPCSubTarget);
5737
Jim Laskeyefc7e522006-12-04 22:04:42 +00005738 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner9f0bc652007-02-25 05:34:32 +00005739 case ISD::DYNAMIC_STACKALLOC:
5740 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng54fc97d2008-04-19 01:30:48 +00005741
Hal Finkel7ee74a62013-03-21 21:37:52 +00005742 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
5743 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
5744
Chris Lattner1a635d62006-04-14 06:01:58 +00005745 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen4c9369d2009-06-04 20:53:52 +00005746 case ISD::FP_TO_UINT:
5747 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005748 SDLoc(Op));
Hal Finkel46479192013-04-01 17:52:07 +00005749 case ISD::UINT_TO_FP:
5750 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00005751 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005752
Chris Lattner1a635d62006-04-14 06:01:58 +00005753 // Lower 64-bit shifts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00005754 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
5755 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
5756 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005757
Chris Lattner1a635d62006-04-14 06:01:58 +00005758 // Vector-related lowering.
5759 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5760 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5761 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5762 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00005763 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005764
Hal Finkelb1fd3cd2013-05-15 21:37:41 +00005765 // For counter-based loop handling.
5766 case ISD::INTRINSIC_W_CHAIN: return SDValue();
5767
Chris Lattner3fc027d2007-12-08 06:59:59 +00005768 // Frame & Return address.
5769 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005770 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00005771 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00005772}
5773
Duncan Sands1607f052008-12-01 11:39:25 +00005774void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
5775 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005776 SelectionDAG &DAG) const {
Roman Divackybdb226e2011-06-28 15:30:42 +00005777 const TargetMachine &TM = getTargetMachine();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005778 SDLoc dl(N);
Chris Lattner1f873002007-11-28 18:44:47 +00005779 switch (N->getOpcode()) {
Duncan Sands57760d92008-10-28 15:00:32 +00005780 default:
Craig Topperbc219812012-02-07 02:50:20 +00005781 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkelb1fd3cd2013-05-15 21:37:41 +00005782 case ISD::INTRINSIC_W_CHAIN: {
5783 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
5784 Intrinsic::ppc_is_decremented_ctr_nonzero)
5785 break;
5786
5787 assert(N->getValueType(0) == MVT::i1 &&
5788 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault225ed702013-05-18 00:21:46 +00005789 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkelb1fd3cd2013-05-15 21:37:41 +00005790 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
5791 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
5792 N->getOperand(1));
5793
5794 Results.push_back(NewInt);
5795 Results.push_back(NewInt.getValue(1));
5796 break;
5797 }
Roman Divackybdb226e2011-06-28 15:30:42 +00005798 case ISD::VAARG: {
5799 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
5800 || TM.getSubtarget<PPCSubtarget>().isPPC64())
5801 return;
5802
5803 EVT VT = N->getValueType(0);
5804
5805 if (VT == MVT::i64) {
5806 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
5807
5808 Results.push_back(NewNode);
5809 Results.push_back(NewNode.getValue(1));
5810 }
5811 return;
5812 }
Duncan Sands1607f052008-12-01 11:39:25 +00005813 case ISD::FP_ROUND_INREG: {
Owen Anderson825b72b2009-08-11 20:47:22 +00005814 assert(N->getValueType(0) == MVT::ppcf128);
5815 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelfdc40a02009-02-17 22:15:04 +00005816 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005817 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00005818 DAG.getIntPtrConstant(0));
Dale Johannesen3484c092009-02-05 22:07:54 +00005819 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005820 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00005821 DAG.getIntPtrConstant(1));
5822
Ulrich Weigand7d35d3f2013-03-26 10:56:22 +00005823 // Add the two halves of the long double in round-to-zero mode.
5824 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands1607f052008-12-01 11:39:25 +00005825
5826 // We know the low half is about to be thrown away, so just use something
5827 // convenient.
Owen Anderson825b72b2009-08-11 20:47:22 +00005828 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesen3484c092009-02-05 22:07:54 +00005829 FPreg, FPreg));
Duncan Sands1607f052008-12-01 11:39:25 +00005830 return;
Duncan Sandsa7360f02008-07-19 16:26:02 +00005831 }
Duncan Sands1607f052008-12-01 11:39:25 +00005832 case ISD::FP_TO_SINT:
Bill Schmidt7c2d8f72013-07-09 18:50:20 +00005833 // LowerFP_TO_INT() can only handle f32 and f64.
5834 if (N->getOperand(0).getValueType() == MVT::ppcf128)
5835 return;
Dale Johannesen4c9369d2009-06-04 20:53:52 +00005836 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands1607f052008-12-01 11:39:25 +00005837 return;
Chris Lattner1f873002007-11-28 18:44:47 +00005838 }
5839}
5840
5841
Chris Lattner1a635d62006-04-14 06:01:58 +00005842//===----------------------------------------------------------------------===//
5843// Other Lowering Code
5844//===----------------------------------------------------------------------===//
5845
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00005846MachineBasicBlock *
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005847PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00005848 bool is64bit, unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00005849 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005850 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5851
5852 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5853 MachineFunction *F = BB->getParent();
5854 MachineFunction::iterator It = BB;
5855 ++It;
5856
5857 unsigned dest = MI->getOperand(0).getReg();
5858 unsigned ptrA = MI->getOperand(1).getReg();
5859 unsigned ptrB = MI->getOperand(2).getReg();
5860 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00005861 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005862
5863 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5864 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5865 F->insert(It, loopMBB);
5866 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005867 exitMBB->splice(exitMBB->begin(), BB,
5868 llvm::next(MachineBasicBlock::iterator(MI)),
5869 BB->end());
5870 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005871
5872 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesen0e55f062008-08-29 18:29:46 +00005873 unsigned TmpReg = (!BinOpcode) ? incr :
5874 RegInfo.createVirtualRegister(
Dale Johannesena619d012008-09-02 20:30:23 +00005875 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5876 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005877
5878 // thisMBB:
5879 // ...
5880 // fallthrough --> loopMBB
5881 BB->addSuccessor(loopMBB);
5882
5883 // loopMBB:
5884 // l[wd]arx dest, ptr
5885 // add r0, dest, incr
5886 // st[wd]cx. r0, ptr
5887 // bne- loopMBB
5888 // fallthrough --> exitMBB
5889 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005890 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005891 .addReg(ptrA).addReg(ptrB);
Dale Johannesen0e55f062008-08-29 18:29:46 +00005892 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00005893 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
5894 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005895 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005896 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00005897 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005898 BB->addSuccessor(loopMBB);
5899 BB->addSuccessor(exitMBB);
5900
5901 // exitMBB:
5902 // ...
5903 BB = exitMBB;
5904 return BB;
5905}
5906
5907MachineBasicBlock *
Scott Michelfdc40a02009-02-17 22:15:04 +00005908PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesen97efa362008-08-28 17:53:09 +00005909 MachineBasicBlock *BB,
5910 bool is8bit, // operation
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00005911 unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00005912 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesen97efa362008-08-28 17:53:09 +00005913 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5914 // In 64 bit mode we have to use 64 bits for addresses, even though the
5915 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
5916 // registers without caring whether they're 32 or 64, but here we're
5917 // doing actual arithmetic on the addresses.
5918 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkel76973702013-03-21 23:45:03 +00005919 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen97efa362008-08-28 17:53:09 +00005920
5921 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5922 MachineFunction *F = BB->getParent();
5923 MachineFunction::iterator It = BB;
5924 ++It;
5925
5926 unsigned dest = MI->getOperand(0).getReg();
5927 unsigned ptrA = MI->getOperand(1).getReg();
5928 unsigned ptrB = MI->getOperand(2).getReg();
5929 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00005930 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen97efa362008-08-28 17:53:09 +00005931
5932 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5933 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5934 F->insert(It, loopMBB);
5935 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005936 exitMBB->splice(exitMBB->begin(), BB,
5937 llvm::next(MachineBasicBlock::iterator(MI)),
5938 BB->end());
5939 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen97efa362008-08-28 17:53:09 +00005940
5941 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00005942 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00005943 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5944 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen97efa362008-08-28 17:53:09 +00005945 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
5946 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
5947 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
5948 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
5949 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
5950 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
5951 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
5952 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
5953 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
5954 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen0e55f062008-08-29 18:29:46 +00005955 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00005956 unsigned Ptr1Reg;
Dale Johannesen0e55f062008-08-29 18:29:46 +00005957 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00005958
5959 // thisMBB:
5960 // ...
5961 // fallthrough --> loopMBB
5962 BB->addSuccessor(loopMBB);
5963
5964 // The 4-byte load must be aligned, while a char or short may be
5965 // anywhere in the word. Hence all this nasty bookkeeping code.
5966 // add ptr1, ptrA, ptrB [copy if ptrA==0]
5967 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00005968 // xori shift, shift1, 24 [16]
Dale Johannesen97efa362008-08-28 17:53:09 +00005969 // rlwinm ptr, ptr1, 0, 0, 29
5970 // slw incr2, incr, shift
5971 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
5972 // slw mask, mask2, shift
5973 // loopMBB:
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005974 // lwarx tmpDest, ptr
Dale Johannesen0e55f062008-08-29 18:29:46 +00005975 // add tmp, tmpDest, incr2
5976 // andc tmp2, tmpDest, mask
Dale Johannesen97efa362008-08-28 17:53:09 +00005977 // and tmp3, tmp, mask
5978 // or tmp4, tmp3, tmp2
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005979 // stwcx. tmp4, ptr
Dale Johannesen97efa362008-08-28 17:53:09 +00005980 // bne- loopMBB
5981 // fallthrough --> exitMBB
Dale Johannesen0e55f062008-08-29 18:29:46 +00005982 // srw dest, tmpDest, shift
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00005983 if (ptrA != ZeroReg) {
Dale Johannesen97efa362008-08-28 17:53:09 +00005984 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005985 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005986 .addReg(ptrA).addReg(ptrB);
5987 } else {
5988 Ptr1Reg = ptrB;
5989 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00005990 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005991 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005992 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005993 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
5994 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00005995 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005996 .addReg(Ptr1Reg).addImm(0).addImm(61);
5997 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00005998 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005999 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006000 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00006001 .addReg(incr).addReg(ShiftReg);
6002 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00006003 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen97efa362008-08-28 17:53:09 +00006004 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00006005 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6006 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesen97efa362008-08-28 17:53:09 +00006007 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00006008 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00006009 .addReg(Mask2Reg).addReg(ShiftReg);
6010
6011 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006012 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006013 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen0e55f062008-08-29 18:29:46 +00006014 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00006015 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00006016 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006017 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00006018 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006019 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00006020 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006021 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00006022 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidtdebf7d32013-04-02 18:37:08 +00006023 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006024 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006025 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00006026 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesen97efa362008-08-28 17:53:09 +00006027 BB->addSuccessor(loopMBB);
6028 BB->addSuccessor(exitMBB);
6029
6030 // exitMBB:
6031 // ...
6032 BB = exitMBB;
Jakob Stoklund Olesen5fcb81d2011-04-04 17:57:29 +00006033 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6034 .addReg(ShiftReg);
Dale Johannesen97efa362008-08-28 17:53:09 +00006035 return BB;
6036}
6037
Hal Finkel7ee74a62013-03-21 21:37:52 +00006038llvm::MachineBasicBlock*
6039PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6040 MachineBasicBlock *MBB) const {
6041 DebugLoc DL = MI->getDebugLoc();
6042 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6043
6044 MachineFunction *MF = MBB->getParent();
6045 MachineRegisterInfo &MRI = MF->getRegInfo();
6046
6047 const BasicBlock *BB = MBB->getBasicBlock();
6048 MachineFunction::iterator I = MBB;
6049 ++I;
6050
6051 // Memory Reference
6052 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6053 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6054
6055 unsigned DstReg = MI->getOperand(0).getReg();
6056 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6057 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6058 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6059 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6060
6061 MVT PVT = getPointerTy();
6062 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6063 "Invalid Pointer Size!");
6064 // For v = setjmp(buf), we generate
6065 //
6066 // thisMBB:
6067 // SjLjSetup mainMBB
6068 // bl mainMBB
6069 // v_restore = 1
6070 // b sinkMBB
6071 //
6072 // mainMBB:
6073 // buf[LabelOffset] = LR
6074 // v_main = 0
6075 //
6076 // sinkMBB:
6077 // v = phi(main, restore)
6078 //
6079
6080 MachineBasicBlock *thisMBB = MBB;
6081 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6082 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6083 MF->insert(I, mainMBB);
6084 MF->insert(I, sinkMBB);
6085
6086 MachineInstrBuilder MIB;
6087
6088 // Transfer the remainder of BB and its successor edges to sinkMBB.
6089 sinkMBB->splice(sinkMBB->begin(), MBB,
6090 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
6091 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6092
6093 // Note that the structure of the jmp_buf used here is not compatible
6094 // with that used by libc, and is not designed to be. Specifically, it
6095 // stores only those 'reserved' registers that LLVM does not otherwise
6096 // understand how to spill. Also, by convention, by the time this
6097 // intrinsic is called, Clang has already stored the frame address in the
6098 // first slot of the buffer and stack address in the third. Following the
6099 // X86 target code, we'll store the jump address in the second slot. We also
6100 // need to save the TOC pointer (R2) to handle jumps between shared
6101 // libraries, and that will be stored in the fourth slot. The thread
6102 // identifier (R13) is not affected.
6103
6104 // thisMBB:
6105 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6106 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkel05417222013-07-17 23:50:51 +00006107 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel7ee74a62013-03-21 21:37:52 +00006108
6109 // Prepare IP either in reg.
6110 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6111 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6112 unsigned BufReg = MI->getOperand(1).getReg();
6113
6114 if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) {
6115 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6116 .addReg(PPC::X2)
Ulrich Weigand347a5072013-05-16 17:58:02 +00006117 .addImm(TOCOffset)
Hal Finkel7ee74a62013-03-21 21:37:52 +00006118 .addReg(BufReg);
Hal Finkel7ee74a62013-03-21 21:37:52 +00006119 MIB.setMemRefs(MMOBegin, MMOEnd);
6120 }
6121
Hal Finkel05417222013-07-17 23:50:51 +00006122 // Naked functions never have a base pointer, and so we use r1. For all
6123 // other functions, this decision must be delayed until during PEI.
6124 unsigned BaseReg;
6125 if (MF->getFunction()->getAttributes().hasAttribute(
6126 AttributeSet::FunctionIndex, Attribute::Naked))
6127 BaseReg = PPCSubTarget.isPPC64() ? PPC::X1 : PPC::R1;
6128 else
6129 BaseReg = PPCSubTarget.isPPC64() ? PPC::BP8 : PPC::BP;
6130
6131 MIB = BuildMI(*thisMBB, MI, DL,
6132 TII->get(PPCSubTarget.isPPC64() ? PPC::STD : PPC::STW))
6133 .addReg(BaseReg)
6134 .addImm(BPOffset)
6135 .addReg(BufReg);
6136 MIB.setMemRefs(MMOBegin, MMOEnd);
6137
Hal Finkel7ee74a62013-03-21 21:37:52 +00006138 // Setup
Hal Finkelcaeeb182013-04-04 22:55:54 +00006139 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling80ada582013-06-07 07:55:53 +00006140 const PPCRegisterInfo *TRI =
6141 static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo());
6142 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel7ee74a62013-03-21 21:37:52 +00006143
6144 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6145
6146 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6147 .addMBB(mainMBB);
6148 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6149
6150 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6151 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6152
6153 // mainMBB:
6154 // mainDstReg = 0
6155 MIB = BuildMI(mainMBB, DL,
6156 TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
6157
6158 // Store IP
6159 if (PPCSubTarget.isPPC64()) {
6160 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6161 .addReg(LabelReg)
Ulrich Weigand347a5072013-05-16 17:58:02 +00006162 .addImm(LabelOffset)
Hal Finkel7ee74a62013-03-21 21:37:52 +00006163 .addReg(BufReg);
6164 } else {
6165 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6166 .addReg(LabelReg)
6167 .addImm(LabelOffset)
6168 .addReg(BufReg);
6169 }
6170
6171 MIB.setMemRefs(MMOBegin, MMOEnd);
6172
6173 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6174 mainMBB->addSuccessor(sinkMBB);
6175
6176 // sinkMBB:
6177 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6178 TII->get(PPC::PHI), DstReg)
6179 .addReg(mainDstReg).addMBB(mainMBB)
6180 .addReg(restoreDstReg).addMBB(thisMBB);
6181
6182 MI->eraseFromParent();
6183 return sinkMBB;
6184}
6185
6186MachineBasicBlock *
6187PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6188 MachineBasicBlock *MBB) const {
6189 DebugLoc DL = MI->getDebugLoc();
6190 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6191
6192 MachineFunction *MF = MBB->getParent();
6193 MachineRegisterInfo &MRI = MF->getRegInfo();
6194
6195 // Memory Reference
6196 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6197 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6198
6199 MVT PVT = getPointerTy();
6200 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6201 "Invalid Pointer Size!");
6202
6203 const TargetRegisterClass *RC =
6204 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6205 unsigned Tmp = MRI.createVirtualRegister(RC);
6206 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6207 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6208 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Hal Finkel05417222013-07-17 23:50:51 +00006209 unsigned BP = (PVT == MVT::i64) ? PPC::X30 : PPC::R30;
Hal Finkel7ee74a62013-03-21 21:37:52 +00006210
6211 MachineInstrBuilder MIB;
6212
6213 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6214 const int64_t SPOffset = 2 * PVT.getStoreSize();
6215 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkel05417222013-07-17 23:50:51 +00006216 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel7ee74a62013-03-21 21:37:52 +00006217
6218 unsigned BufReg = MI->getOperand(0).getReg();
6219
6220 // Reload FP (the jumped-to function may not have had a
6221 // frame pointer, and if so, then its r31 will be restored
6222 // as necessary).
6223 if (PVT == MVT::i64) {
6224 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6225 .addImm(0)
6226 .addReg(BufReg);
6227 } else {
6228 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6229 .addImm(0)
6230 .addReg(BufReg);
6231 }
6232 MIB.setMemRefs(MMOBegin, MMOEnd);
6233
6234 // Reload IP
6235 if (PVT == MVT::i64) {
6236 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand347a5072013-05-16 17:58:02 +00006237 .addImm(LabelOffset)
Hal Finkel7ee74a62013-03-21 21:37:52 +00006238 .addReg(BufReg);
6239 } else {
6240 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6241 .addImm(LabelOffset)
6242 .addReg(BufReg);
6243 }
6244 MIB.setMemRefs(MMOBegin, MMOEnd);
6245
6246 // Reload SP
6247 if (PVT == MVT::i64) {
6248 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand347a5072013-05-16 17:58:02 +00006249 .addImm(SPOffset)
Hal Finkel7ee74a62013-03-21 21:37:52 +00006250 .addReg(BufReg);
6251 } else {
6252 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6253 .addImm(SPOffset)
6254 .addReg(BufReg);
6255 }
6256 MIB.setMemRefs(MMOBegin, MMOEnd);
6257
Hal Finkel05417222013-07-17 23:50:51 +00006258 // Reload BP
6259 if (PVT == MVT::i64) {
6260 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
6261 .addImm(BPOffset)
6262 .addReg(BufReg);
6263 } else {
6264 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
6265 .addImm(BPOffset)
6266 .addReg(BufReg);
6267 }
6268 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel7ee74a62013-03-21 21:37:52 +00006269
6270 // Reload TOC
6271 if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) {
6272 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand347a5072013-05-16 17:58:02 +00006273 .addImm(TOCOffset)
Hal Finkel7ee74a62013-03-21 21:37:52 +00006274 .addReg(BufReg);
6275
6276 MIB.setMemRefs(MMOBegin, MMOEnd);
6277 }
6278
6279 // Jump
6280 BuildMI(*MBB, MI, DL,
6281 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6282 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6283
6284 MI->eraseFromParent();
6285 return MBB;
6286}
6287
Dale Johannesen97efa362008-08-28 17:53:09 +00006288MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006289PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006290 MachineBasicBlock *BB) const {
Hal Finkel7ee74a62013-03-21 21:37:52 +00006291 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6292 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6293 return emitEHSjLjSetJmp(MI, BB);
6294 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6295 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6296 return emitEHSjLjLongJmp(MI, BB);
6297 }
6298
Evan Chengc0f64ff2006-11-27 23:37:22 +00006299 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng53301922008-07-12 02:23:19 +00006300
6301 // To "insert" these instructions we actually have to insert their
6302 // control-flow patterns.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006303 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006304 MachineFunction::iterator It = BB;
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006305 ++It;
Evan Cheng53301922008-07-12 02:23:19 +00006306
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006307 MachineFunction *F = BB->getParent();
Evan Cheng53301922008-07-12 02:23:19 +00006308
Hal Finkel009f7af2012-06-22 23:10:08 +00006309 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6310 MI->getOpcode() == PPC::SELECT_CC_I8)) {
Hal Finkelff56d1a2013-04-05 23:29:01 +00006311 SmallVector<MachineOperand, 2> Cond;
6312 Cond.push_back(MI->getOperand(4));
6313 Cond.push_back(MI->getOperand(1));
6314
Hal Finkel009f7af2012-06-22 23:10:08 +00006315 DebugLoc dl = MI->getDebugLoc();
Bill Wendling80ada582013-06-07 07:55:53 +00006316 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6317 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
6318 Cond, MI->getOperand(2).getReg(),
6319 MI->getOperand(3).getReg());
Hal Finkel009f7af2012-06-22 23:10:08 +00006320 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6321 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6322 MI->getOpcode() == PPC::SELECT_CC_F4 ||
6323 MI->getOpcode() == PPC::SELECT_CC_F8 ||
6324 MI->getOpcode() == PPC::SELECT_CC_VRRC) {
6325
Evan Cheng53301922008-07-12 02:23:19 +00006326
6327 // The incoming instruction knows the destination vreg to set, the
6328 // condition code register to branch on, the true/false values to
6329 // select between, and a branch opcode to use.
6330
6331 // thisMBB:
6332 // ...
6333 // TrueVal = ...
6334 // cmpTY ccX, r1, r2
6335 // bCC copy1MBB
6336 // fallthrough --> copy0MBB
6337 MachineBasicBlock *thisMBB = BB;
6338 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6339 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
6340 unsigned SelectPred = MI->getOperand(4).getImm();
Dale Johannesen536a2f12009-02-13 02:27:39 +00006341 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00006342 F->insert(It, copy0MBB);
6343 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006344
6345 // Transfer the remainder of BB and its successor edges to sinkMBB.
6346 sinkMBB->splice(sinkMBB->begin(), BB,
6347 llvm::next(MachineBasicBlock::iterator(MI)),
6348 BB->end());
6349 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6350
Evan Cheng53301922008-07-12 02:23:19 +00006351 // Next, add the true and fallthrough blocks as its successors.
6352 BB->addSuccessor(copy0MBB);
6353 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006354
Dan Gohman14152b42010-07-06 20:24:04 +00006355 BuildMI(BB, dl, TII->get(PPC::BCC))
6356 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6357
Evan Cheng53301922008-07-12 02:23:19 +00006358 // copy0MBB:
6359 // %FalseValue = ...
6360 // # fallthrough to sinkMBB
6361 BB = copy0MBB;
Scott Michelfdc40a02009-02-17 22:15:04 +00006362
Evan Cheng53301922008-07-12 02:23:19 +00006363 // Update machine-CFG edges
6364 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006365
Evan Cheng53301922008-07-12 02:23:19 +00006366 // sinkMBB:
6367 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6368 // ...
6369 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006370 BuildMI(*BB, BB->begin(), dl,
6371 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng53301922008-07-12 02:23:19 +00006372 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
6373 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6374 }
Dale Johannesen97efa362008-08-28 17:53:09 +00006375 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
6376 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
6377 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
6378 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006379 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
6380 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
6381 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
6382 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006383
6384 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
6385 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
6386 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
6387 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006388 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
6389 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
6390 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
6391 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006392
6393 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
6394 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
6395 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
6396 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006397 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
6398 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
6399 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
6400 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006401
6402 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
6403 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
6404 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
6405 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006406 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
6407 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
6408 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
6409 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006410
6411 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesen209a4092008-09-11 02:15:03 +00006412 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesen97efa362008-08-28 17:53:09 +00006413 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesen209a4092008-09-11 02:15:03 +00006414 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006415 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesen209a4092008-09-11 02:15:03 +00006416 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006417 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesen209a4092008-09-11 02:15:03 +00006418 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006419
6420 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6421 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6422 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6423 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006424 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6425 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6426 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6427 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006428
Dale Johannesen0e55f062008-08-29 18:29:46 +00006429 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6430 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6431 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6432 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6433 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6434 BB = EmitAtomicBinary(MI, BB, false, 0);
6435 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6436 BB = EmitAtomicBinary(MI, BB, true, 0);
6437
Evan Cheng53301922008-07-12 02:23:19 +00006438 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6439 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6440 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6441
6442 unsigned dest = MI->getOperand(0).getReg();
6443 unsigned ptrA = MI->getOperand(1).getReg();
6444 unsigned ptrB = MI->getOperand(2).getReg();
6445 unsigned oldval = MI->getOperand(3).getReg();
6446 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00006447 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00006448
Dale Johannesen65e39732008-08-25 18:53:26 +00006449 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6450 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6451 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng53301922008-07-12 02:23:19 +00006452 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen65e39732008-08-25 18:53:26 +00006453 F->insert(It, loop1MBB);
6454 F->insert(It, loop2MBB);
6455 F->insert(It, midMBB);
Evan Cheng53301922008-07-12 02:23:19 +00006456 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006457 exitMBB->splice(exitMBB->begin(), BB,
6458 llvm::next(MachineBasicBlock::iterator(MI)),
6459 BB->end());
6460 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng53301922008-07-12 02:23:19 +00006461
6462 // thisMBB:
6463 // ...
6464 // fallthrough --> loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00006465 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00006466
Dale Johannesen65e39732008-08-25 18:53:26 +00006467 // loop1MBB:
Evan Cheng53301922008-07-12 02:23:19 +00006468 // l[wd]arx dest, ptr
Dale Johannesen65e39732008-08-25 18:53:26 +00006469 // cmp[wd] dest, oldval
6470 // bne- midMBB
6471 // loop2MBB:
Evan Cheng53301922008-07-12 02:23:19 +00006472 // st[wd]cx. newval, ptr
6473 // bne- loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00006474 // b exitBB
6475 // midMBB:
6476 // st[wd]cx. dest, ptr
6477 // exitBB:
6478 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006479 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng53301922008-07-12 02:23:19 +00006480 .addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006481 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng53301922008-07-12 02:23:19 +00006482 .addReg(oldval).addReg(dest);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006483 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00006484 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6485 BB->addSuccessor(loop2MBB);
6486 BB->addSuccessor(midMBB);
6487
6488 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006489 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng53301922008-07-12 02:23:19 +00006490 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006491 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00006492 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006493 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen65e39732008-08-25 18:53:26 +00006494 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00006495 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006496
Dale Johannesen65e39732008-08-25 18:53:26 +00006497 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006498 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen65e39732008-08-25 18:53:26 +00006499 .addReg(dest).addReg(ptrA).addReg(ptrB);
6500 BB->addSuccessor(exitMBB);
6501
Evan Cheng53301922008-07-12 02:23:19 +00006502 // exitMBB:
6503 // ...
6504 BB = exitMBB;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006505 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6506 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6507 // We must use 64-bit registers for addresses when targeting 64-bit,
6508 // since we're actually doing arithmetic on them. Other registers
6509 // can be 32-bit.
6510 bool is64bit = PPCSubTarget.isPPC64();
6511 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6512
6513 unsigned dest = MI->getOperand(0).getReg();
6514 unsigned ptrA = MI->getOperand(1).getReg();
6515 unsigned ptrB = MI->getOperand(2).getReg();
6516 unsigned oldval = MI->getOperand(3).getReg();
6517 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00006518 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006519
6520 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6521 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6522 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6523 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6524 F->insert(It, loop1MBB);
6525 F->insert(It, loop2MBB);
6526 F->insert(It, midMBB);
6527 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006528 exitMBB->splice(exitMBB->begin(), BB,
6529 llvm::next(MachineBasicBlock::iterator(MI)),
6530 BB->end());
6531 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006532
6533 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00006534 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00006535 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6536 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006537 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6538 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6539 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6540 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6541 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6542 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6543 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6544 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6545 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6546 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6547 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6548 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6549 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6550 unsigned Ptr1Reg;
6551 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkel76973702013-03-21 23:45:03 +00006552 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006553 // thisMBB:
6554 // ...
6555 // fallthrough --> loopMBB
6556 BB->addSuccessor(loop1MBB);
6557
6558 // The 4-byte load must be aligned, while a char or short may be
6559 // anywhere in the word. Hence all this nasty bookkeeping code.
6560 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6561 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00006562 // xori shift, shift1, 24 [16]
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006563 // rlwinm ptr, ptr1, 0, 0, 29
6564 // slw newval2, newval, shift
6565 // slw oldval2, oldval,shift
6566 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6567 // slw mask, mask2, shift
6568 // and newval3, newval2, mask
6569 // and oldval3, oldval2, mask
6570 // loop1MBB:
6571 // lwarx tmpDest, ptr
6572 // and tmp, tmpDest, mask
6573 // cmpw tmp, oldval3
6574 // bne- midMBB
6575 // loop2MBB:
6576 // andc tmp2, tmpDest, mask
6577 // or tmp4, tmp2, newval3
6578 // stwcx. tmp4, ptr
6579 // bne- loop1MBB
6580 // b exitBB
6581 // midMBB:
6582 // stwcx. tmpDest, ptr
6583 // exitBB:
6584 // srw dest, tmpDest, shift
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006585 if (ptrA != ZeroReg) {
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006586 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006587 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006588 .addReg(ptrA).addReg(ptrB);
6589 } else {
6590 Ptr1Reg = ptrB;
6591 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00006592 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006593 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006594 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006595 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6596 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00006597 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006598 .addReg(Ptr1Reg).addImm(0).addImm(61);
6599 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00006600 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006601 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006602 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006603 .addReg(newval).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006604 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006605 .addReg(oldval).addReg(ShiftReg);
6606 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00006607 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006608 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00006609 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6610 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6611 .addReg(Mask3Reg).addImm(65535);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006612 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00006613 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006614 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006615 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006616 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006617 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006618 .addReg(OldVal2Reg).addReg(MaskReg);
6619
6620 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006621 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006622 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006623 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6624 .addReg(TmpDestReg).addReg(MaskReg);
6625 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006626 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006627 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006628 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6629 BB->addSuccessor(loop2MBB);
6630 BB->addSuccessor(midMBB);
6631
6632 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006633 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6634 .addReg(TmpDestReg).addReg(MaskReg);
6635 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6636 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6637 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006638 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006639 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006640 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006641 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006642 BB->addSuccessor(loop1MBB);
6643 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006644
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006645 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006646 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006647 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006648 BB->addSuccessor(exitMBB);
6649
6650 // exitMBB:
6651 // ...
6652 BB = exitMBB;
Jakob Stoklund Olesen5fcb81d2011-04-04 17:57:29 +00006653 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
6654 .addReg(ShiftReg);
Ulrich Weigand7d35d3f2013-03-26 10:56:22 +00006655 } else if (MI->getOpcode() == PPC::FADDrtz) {
6656 // This pseudo performs an FADD with rounding mode temporarily forced
6657 // to round-to-zero. We emit this via custom inserter since the FPSCR
6658 // is not modeled at the SelectionDAG level.
6659 unsigned Dest = MI->getOperand(0).getReg();
6660 unsigned Src1 = MI->getOperand(1).getReg();
6661 unsigned Src2 = MI->getOperand(2).getReg();
6662 DebugLoc dl = MI->getDebugLoc();
6663
6664 MachineRegisterInfo &RegInfo = F->getRegInfo();
6665 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
6666
6667 // Save FPSCR value.
6668 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
6669
6670 // Set rounding mode to round-to-zero.
6671 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
6672 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
6673
6674 // Perform addition.
6675 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
6676
6677 // Restore FPSCR value.
6678 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkel0882fd62013-03-29 19:41:55 +00006679 } else if (MI->getOpcode() == PPC::FRINDrint ||
6680 MI->getOpcode() == PPC::FRINSrint) {
6681 bool isf32 = MI->getOpcode() == PPC::FRINSrint;
6682 unsigned Dest = MI->getOperand(0).getReg();
6683 unsigned Src = MI->getOperand(1).getReg();
6684 DebugLoc dl = MI->getDebugLoc();
6685
6686 MachineRegisterInfo &RegInfo = F->getRegInfo();
6687 unsigned CRReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
6688
6689 // Perform the rounding.
6690 BuildMI(*BB, MI, dl, TII->get(isf32 ? PPC::FRINS : PPC::FRIND), Dest)
6691 .addReg(Src);
6692
6693 // Compare the results.
6694 BuildMI(*BB, MI, dl, TII->get(isf32 ? PPC::FCMPUS : PPC::FCMPUD), CRReg)
6695 .addReg(Dest).addReg(Src);
6696
6697 // If the results were not equal, then set the FPSCR XX bit.
6698 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6699 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6700 F->insert(It, midMBB);
6701 F->insert(It, exitMBB);
6702 exitMBB->splice(exitMBB->begin(), BB,
6703 llvm::next(MachineBasicBlock::iterator(MI)),
6704 BB->end());
6705 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6706
6707 BuildMI(*BB, MI, dl, TII->get(PPC::BCC))
6708 .addImm(PPC::PRED_EQ).addReg(CRReg).addMBB(exitMBB);
6709
6710 BB->addSuccessor(midMBB);
6711 BB->addSuccessor(exitMBB);
6712
6713 BB = midMBB;
6714
6715 // Set the FPSCR XX bit (FE_INEXACT). Note that we cannot just set
6716 // the FI bit here because that will not automatically set XX also,
6717 // and XX is what libm interprets as the FE_INEXACT flag.
6718 BuildMI(BB, dl, TII->get(PPC::MTFSB1)).addImm(/* 38 - 32 = */ 6);
6719 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
6720
6721 BB->addSuccessor(exitMBB);
6722
6723 BB = exitMBB;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006724 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006725 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng53301922008-07-12 02:23:19 +00006726 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006727
Dan Gohman14152b42010-07-06 20:24:04 +00006728 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006729 return BB;
6730}
6731
Chris Lattner1a635d62006-04-14 06:01:58 +00006732//===----------------------------------------------------------------------===//
6733// Target Optimization Hooks
6734//===----------------------------------------------------------------------===//
6735
Hal Finkel63c32a72013-04-03 17:44:56 +00006736SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
6737 DAGCombinerInfo &DCI) const {
Hal Finkel827307b2013-04-03 04:01:11 +00006738 if (DCI.isAfterLegalizeVectorOps())
6739 return SDValue();
6740
Hal Finkel63c32a72013-04-03 17:44:56 +00006741 EVT VT = Op.getValueType();
6742
6743 if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) ||
6744 (VT == MVT::f64 && PPCSubTarget.hasFRE()) ||
6745 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel827307b2013-04-03 04:01:11 +00006746
6747 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6748 // For the reciprocal, we need to find the zero of the function:
6749 // F(X) = A X - 1 [which has a zero at X = 1/A]
6750 // =>
6751 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
6752 // does not require additional intermediate precision]
6753
6754 // Convergence is quadratic, so we essentially double the number of digits
6755 // correct after every iteration. The minimum architected relative
6756 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6757 // 23 digits and double has 52 digits.
6758 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkel63c32a72013-04-03 17:44:56 +00006759 if (VT.getScalarType() == MVT::f64)
Hal Finkel827307b2013-04-03 04:01:11 +00006760 ++Iterations;
6761
6762 SelectionDAG &DAG = DCI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006763 SDLoc dl(Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006764
6765 SDValue FPOne =
Hal Finkel63c32a72013-04-03 17:44:56 +00006766 DAG.getConstantFP(1.0, VT.getScalarType());
6767 if (VT.isVector()) {
6768 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel827307b2013-04-03 04:01:11 +00006769 "Unknown vector type");
Hal Finkel63c32a72013-04-03 17:44:56 +00006770 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel827307b2013-04-03 04:01:11 +00006771 FPOne, FPOne, FPOne, FPOne);
6772 }
6773
Hal Finkel63c32a72013-04-03 17:44:56 +00006774 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006775 DCI.AddToWorklist(Est.getNode());
6776
6777 // Newton iterations: Est = Est + Est (1 - Arg * Est)
6778 for (int i = 0; i < Iterations; ++i) {
Hal Finkel63c32a72013-04-03 17:44:56 +00006779 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel827307b2013-04-03 04:01:11 +00006780 DCI.AddToWorklist(NewEst.getNode());
6781
Hal Finkel63c32a72013-04-03 17:44:56 +00006782 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006783 DCI.AddToWorklist(NewEst.getNode());
6784
Hal Finkel63c32a72013-04-03 17:44:56 +00006785 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006786 DCI.AddToWorklist(NewEst.getNode());
6787
Hal Finkel63c32a72013-04-03 17:44:56 +00006788 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006789 DCI.AddToWorklist(Est.getNode());
6790 }
6791
6792 return Est;
6793 }
6794
6795 return SDValue();
6796}
6797
Hal Finkel63c32a72013-04-03 17:44:56 +00006798SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel827307b2013-04-03 04:01:11 +00006799 DAGCombinerInfo &DCI) const {
6800 if (DCI.isAfterLegalizeVectorOps())
6801 return SDValue();
6802
Hal Finkel63c32a72013-04-03 17:44:56 +00006803 EVT VT = Op.getValueType();
6804
6805 if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) ||
6806 (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) ||
6807 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel827307b2013-04-03 04:01:11 +00006808
6809 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6810 // For the reciprocal sqrt, we need to find the zero of the function:
6811 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
6812 // =>
6813 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
6814 // As a result, we precompute A/2 prior to the iteration loop.
6815
6816 // Convergence is quadratic, so we essentially double the number of digits
6817 // correct after every iteration. The minimum architected relative
6818 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6819 // 23 digits and double has 52 digits.
6820 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkel63c32a72013-04-03 17:44:56 +00006821 if (VT.getScalarType() == MVT::f64)
Hal Finkel827307b2013-04-03 04:01:11 +00006822 ++Iterations;
6823
6824 SelectionDAG &DAG = DCI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006825 SDLoc dl(Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006826
Hal Finkel63c32a72013-04-03 17:44:56 +00006827 SDValue FPThreeHalves =
6828 DAG.getConstantFP(1.5, VT.getScalarType());
6829 if (VT.isVector()) {
6830 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel827307b2013-04-03 04:01:11 +00006831 "Unknown vector type");
Hal Finkel63c32a72013-04-03 17:44:56 +00006832 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
6833 FPThreeHalves, FPThreeHalves,
6834 FPThreeHalves, FPThreeHalves);
Hal Finkel827307b2013-04-03 04:01:11 +00006835 }
6836
Hal Finkel63c32a72013-04-03 17:44:56 +00006837 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006838 DCI.AddToWorklist(Est.getNode());
6839
6840 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
6841 // this entire sequence requires only one FP constant.
Hal Finkel63c32a72013-04-03 17:44:56 +00006842 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006843 DCI.AddToWorklist(HalfArg.getNode());
6844
Hal Finkel63c32a72013-04-03 17:44:56 +00006845 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006846 DCI.AddToWorklist(HalfArg.getNode());
6847
6848 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
6849 for (int i = 0; i < Iterations; ++i) {
Hal Finkel63c32a72013-04-03 17:44:56 +00006850 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel827307b2013-04-03 04:01:11 +00006851 DCI.AddToWorklist(NewEst.getNode());
6852
Hal Finkel63c32a72013-04-03 17:44:56 +00006853 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006854 DCI.AddToWorklist(NewEst.getNode());
6855
Hal Finkel63c32a72013-04-03 17:44:56 +00006856 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006857 DCI.AddToWorklist(NewEst.getNode());
6858
Hal Finkel63c32a72013-04-03 17:44:56 +00006859 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006860 DCI.AddToWorklist(Est.getNode());
6861 }
6862
6863 return Est;
6864 }
6865
6866 return SDValue();
6867}
6868
Hal Finkel119da2e2013-05-27 02:06:39 +00006869// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
6870// not enforce equality of the chain operands.
6871static bool isConsecutiveLS(LSBaseSDNode *LS, LSBaseSDNode *Base,
6872 unsigned Bytes, int Dist,
6873 SelectionDAG &DAG) {
6874 EVT VT = LS->getMemoryVT();
6875 if (VT.getSizeInBits() / 8 != Bytes)
6876 return false;
6877
6878 SDValue Loc = LS->getBasePtr();
6879 SDValue BaseLoc = Base->getBasePtr();
6880 if (Loc.getOpcode() == ISD::FrameIndex) {
6881 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6882 return false;
6883 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6884 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6885 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6886 int FS = MFI->getObjectSize(FI);
6887 int BFS = MFI->getObjectSize(BFI);
6888 if (FS != BFS || FS != (int)Bytes) return false;
6889 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6890 }
6891
6892 // Handle X+C
6893 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6894 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6895 return true;
6896
6897 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6898 const GlobalValue *GV1 = NULL;
6899 const GlobalValue *GV2 = NULL;
6900 int64_t Offset1 = 0;
6901 int64_t Offset2 = 0;
6902 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6903 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6904 if (isGA1 && isGA2 && GV1 == GV2)
6905 return Offset1 == (Offset2 + Dist*Bytes);
6906 return false;
6907}
6908
Hal Finkel1907cad2013-05-26 18:08:30 +00006909// Return true is there is a nearyby consecutive load to the one provided
6910// (regardless of alignment). We search up and down the chain, looking though
6911// token factors and other loads (but nothing else). As a result, a true
6912// results indicates that it is safe to create a new consecutive load adjacent
6913// to the load provided.
6914static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
6915 SDValue Chain = LD->getChain();
6916 EVT VT = LD->getMemoryVT();
6917
6918 SmallSet<SDNode *, 16> LoadRoots;
6919 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
6920 SmallSet<SDNode *, 16> Visited;
6921
6922 // First, search up the chain, branching to follow all token-factor operands.
6923 // If we find a consecutive load, then we're done, otherwise, record all
6924 // nodes just above the top-level loads and token factors.
6925 while (!Queue.empty()) {
6926 SDNode *ChainNext = Queue.pop_back_val();
6927 if (!Visited.insert(ChainNext))
6928 continue;
6929
6930 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(ChainNext)) {
Hal Finkel119da2e2013-05-27 02:06:39 +00006931 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel1907cad2013-05-26 18:08:30 +00006932 return true;
6933
6934 if (!Visited.count(ChainLD->getChain().getNode()))
6935 Queue.push_back(ChainLD->getChain().getNode());
6936 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
6937 for (SDNode::op_iterator O = ChainNext->op_begin(),
6938 OE = ChainNext->op_end(); O != OE; ++O)
6939 if (!Visited.count(O->getNode()))
6940 Queue.push_back(O->getNode());
6941 } else
6942 LoadRoots.insert(ChainNext);
6943 }
6944
6945 // Second, search down the chain, starting from the top-level nodes recorded
6946 // in the first phase. These top-level nodes are the nodes just above all
6947 // loads and token factors. Starting with their uses, recursively look though
6948 // all loads (just the chain uses) and token factors to find a consecutive
6949 // load.
6950 Visited.clear();
6951 Queue.clear();
6952
6953 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
6954 IE = LoadRoots.end(); I != IE; ++I) {
6955 Queue.push_back(*I);
6956
6957 while (!Queue.empty()) {
6958 SDNode *LoadRoot = Queue.pop_back_val();
6959 if (!Visited.insert(LoadRoot))
6960 continue;
6961
6962 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(LoadRoot))
Hal Finkel119da2e2013-05-27 02:06:39 +00006963 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel1907cad2013-05-26 18:08:30 +00006964 return true;
6965
6966 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
6967 UE = LoadRoot->use_end(); UI != UE; ++UI)
6968 if (((isa<LoadSDNode>(*UI) &&
6969 cast<LoadSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
6970 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
6971 Queue.push_back(*UI);
6972 }
6973 }
6974
6975 return false;
6976}
6977
Duncan Sands25cf2272008-11-24 14:53:14 +00006978SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
6979 DAGCombinerInfo &DCI) const {
Dan Gohmanf0757b02010-04-21 01:34:56 +00006980 const TargetMachine &TM = getTargetMachine();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006981 SelectionDAG &DAG = DCI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006982 SDLoc dl(N);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006983 switch (N->getOpcode()) {
6984 default: break;
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006985 case PPCISD::SHL:
6986 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00006987 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006988 return N->getOperand(0);
6989 }
6990 break;
6991 case PPCISD::SRL:
6992 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00006993 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006994 return N->getOperand(0);
6995 }
6996 break;
6997 case PPCISD::SRA:
6998 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00006999 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00007000 C->isAllOnesValue()) // -1 >>s V -> -1.
7001 return N->getOperand(0);
7002 }
7003 break;
Hal Finkel827307b2013-04-03 04:01:11 +00007004 case ISD::FDIV: {
7005 assert(TM.Options.UnsafeFPMath &&
7006 "Reciprocal estimates require UnsafeFPMath");
Scott Michelfdc40a02009-02-17 22:15:04 +00007007
Hal Finkel827307b2013-04-03 04:01:11 +00007008 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkel63c32a72013-04-03 17:44:56 +00007009 SDValue RV =
7010 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Hal Finkel827307b2013-04-03 04:01:11 +00007011 if (RV.getNode() != 0) {
7012 DCI.AddToWorklist(RV.getNode());
7013 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7014 N->getOperand(0), RV);
7015 }
Hal Finkel7530a9f2013-04-04 22:44:12 +00007016 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
7017 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7018 SDValue RV =
7019 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7020 DCI);
7021 if (RV.getNode() != 0) {
7022 DCI.AddToWorklist(RV.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00007023 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)),
Hal Finkel7530a9f2013-04-04 22:44:12 +00007024 N->getValueType(0), RV);
7025 DCI.AddToWorklist(RV.getNode());
7026 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7027 N->getOperand(0), RV);
7028 }
7029 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
7030 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7031 SDValue RV =
7032 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7033 DCI);
7034 if (RV.getNode() != 0) {
7035 DCI.AddToWorklist(RV.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00007036 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)),
Hal Finkel7530a9f2013-04-04 22:44:12 +00007037 N->getValueType(0), RV,
7038 N->getOperand(1).getOperand(1));
7039 DCI.AddToWorklist(RV.getNode());
7040 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7041 N->getOperand(0), RV);
7042 }
Hal Finkel827307b2013-04-03 04:01:11 +00007043 }
7044
Hal Finkel63c32a72013-04-03 17:44:56 +00007045 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Hal Finkel827307b2013-04-03 04:01:11 +00007046 if (RV.getNode() != 0) {
7047 DCI.AddToWorklist(RV.getNode());
7048 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7049 N->getOperand(0), RV);
7050 }
7051
7052 }
7053 break;
7054 case ISD::FSQRT: {
7055 assert(TM.Options.UnsafeFPMath &&
7056 "Reciprocal estimates require UnsafeFPMath");
7057
7058 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
7059 // reciprocal sqrt.
Hal Finkel63c32a72013-04-03 17:44:56 +00007060 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Hal Finkel827307b2013-04-03 04:01:11 +00007061 if (RV.getNode() != 0) {
7062 DCI.AddToWorklist(RV.getNode());
Hal Finkel63c32a72013-04-03 17:44:56 +00007063 RV = DAGCombineFastRecip(RV, DCI);
Hal Finkel827307b2013-04-03 04:01:11 +00007064 if (RV.getNode() != 0)
7065 return RV;
7066 }
7067
7068 }
7069 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00007070 case ISD::SINT_TO_FP:
Chris Lattnera7a58542006-06-16 17:34:12 +00007071 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00007072 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
7073 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
7074 // We allow the src/dst to be either f32/f64, but the intermediate
7075 // type must be i64.
Owen Anderson825b72b2009-08-11 20:47:22 +00007076 if (N->getOperand(0).getValueType() == MVT::i64 &&
7077 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00007078 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 if (Val.getValueType() == MVT::f32) {
7080 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00007081 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00007082 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007083
Owen Anderson825b72b2009-08-11 20:47:22 +00007084 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00007085 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00007086 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00007087 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00007088 if (N->getValueType(0) == MVT::f32) {
7089 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner0bd48932008-01-17 07:00:52 +00007090 DAG.getIntPtrConstant(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00007091 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00007092 }
7093 return Val;
Owen Anderson825b72b2009-08-11 20:47:22 +00007094 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00007095 // If the intermediate type is i32, we can avoid the load/store here
7096 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00007097 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00007098 }
7099 }
7100 break;
Chris Lattner51269842006-03-01 05:50:56 +00007101 case ISD::STORE:
7102 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
7103 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnera7a02fb2008-01-18 16:54:56 +00007104 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner51269842006-03-01 05:50:56 +00007105 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007106 N->getOperand(1).getValueType() == MVT::i32 &&
7107 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00007108 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007109 if (Val.getValueType() == MVT::f32) {
7110 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00007111 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00007112 }
Owen Anderson825b72b2009-08-11 20:47:22 +00007113 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00007114 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00007115
Hal Finkelf170cc92013-04-01 15:37:53 +00007116 SDValue Ops[] = {
7117 N->getOperand(0), Val, N->getOperand(2),
7118 DAG.getValueType(N->getOperand(1).getValueType())
7119 };
7120
7121 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
7122 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
7123 cast<StoreSDNode>(N)->getMemoryVT(),
7124 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greifba36cb52008-08-28 21:40:38 +00007125 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00007126 return Val;
7127 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007128
Chris Lattnerd9989382006-07-10 20:56:58 +00007129 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman6acaaa82009-09-25 00:57:30 +00007130 if (cast<StoreSDNode>(N)->isUnindexed() &&
7131 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00007132 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007133 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkelefdd4672013-03-28 19:25:55 +00007134 N->getOperand(1).getValueType() == MVT::i16 ||
7135 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel2544f222013-03-28 20:23:46 +00007136 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkelefdd4672013-03-28 19:25:55 +00007137 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman475871a2008-07-27 21:46:04 +00007138 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnerd9989382006-07-10 20:56:58 +00007139 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson825b72b2009-08-11 20:47:22 +00007140 if (BSwapOp.getValueType() == MVT::i16)
7141 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnerd9989382006-07-10 20:56:58 +00007142
Dan Gohmanc76909a2009-09-25 20:36:54 +00007143 SDValue Ops[] = {
7144 N->getOperand(0), BSwapOp, N->getOperand(2),
7145 DAG.getValueType(N->getOperand(1).getValueType())
7146 };
7147 return
7148 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
7149 Ops, array_lengthof(Ops),
7150 cast<StoreSDNode>(N)->getMemoryVT(),
7151 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnerd9989382006-07-10 20:56:58 +00007152 }
7153 break;
Hal Finkel80d10de2013-05-24 23:00:14 +00007154 case ISD::LOAD: {
7155 LoadSDNode *LD = cast<LoadSDNode>(N);
7156 EVT VT = LD->getValueType(0);
7157 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
7158 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
7159 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
7160 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
7161 DCI.getDAGCombineLevel() == AfterLegalizeTypes &&
7162 LD->getAlignment() < ABIAlignment) {
7163 // This is a type-legal unaligned Altivec load.
7164 SDValue Chain = LD->getChain();
7165 SDValue Ptr = LD->getBasePtr();
7166
7167 // This implements the loading of unaligned vectors as described in
7168 // the venerable Apple Velocity Engine overview. Specifically:
7169 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
7170 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
7171 //
7172 // The general idea is to expand a sequence of one or more unaligned
7173 // loads into a alignment-based permutation-control instruction (lvsl),
7174 // a series of regular vector loads (which always truncate their
7175 // input address to an aligned address), and a series of permutations.
7176 // The results of these permutations are the requested loaded values.
7177 // The trick is that the last "extra" load is not taken from the address
7178 // you might suspect (sizeof(vector) bytes after the last requested
7179 // load), but rather sizeof(vector) - 1 bytes after the last
7180 // requested vector. The point of this is to avoid a page fault if the
7181 // base address happend to be aligned. This works because if the base
7182 // address is aligned, then adding less than a full vector length will
7183 // cause the last vector in the sequence to be (re)loaded. Otherwise,
7184 // the next vector will be fetched as you might suspect was necessary.
7185
Hal Finkel5a0e6042013-05-25 04:05:05 +00007186 // We might be able to reuse the permutation generation from
Hal Finkel80d10de2013-05-24 23:00:14 +00007187 // a different base address offset from this one by an aligned amount.
Hal Finkel5a0e6042013-05-25 04:05:05 +00007188 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
7189 // optimization later.
Hal Finkel80d10de2013-05-24 23:00:14 +00007190 SDValue PermCntl = BuildIntrinsicOp(Intrinsic::ppc_altivec_lvsl, Ptr,
7191 DAG, dl, MVT::v16i8);
7192
7193 // Refine the alignment of the original load (a "new" load created here
7194 // which was identical to the first except for the alignment would be
7195 // merged with the existing node regardless).
7196 MachineFunction &MF = DAG.getMachineFunction();
7197 MachineMemOperand *MMO =
7198 MF.getMachineMemOperand(LD->getPointerInfo(),
7199 LD->getMemOperand()->getFlags(),
7200 LD->getMemoryVT().getStoreSize(),
7201 ABIAlignment);
7202 LD->refineAlignment(MMO);
7203 SDValue BaseLoad = SDValue(LD, 0);
7204
7205 // Note that the value of IncOffset (which is provided to the next
7206 // load's pointer info offset value, and thus used to calculate the
7207 // alignment), and the value of IncValue (which is actually used to
7208 // increment the pointer value) are different! This is because we
7209 // require the next load to appear to be aligned, even though it
7210 // is actually offset from the base pointer by a lesser amount.
7211 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel1907cad2013-05-26 18:08:30 +00007212 int IncValue = IncOffset;
7213
7214 // Walk (both up and down) the chain looking for another load at the real
7215 // (aligned) offset (the alignment of the other load does not matter in
7216 // this case). If found, then do not use the offset reduction trick, as
7217 // that will prevent the loads from being later combined (as they would
7218 // otherwise be duplicates).
7219 if (!findConsecutiveLoad(LD, DAG))
7220 --IncValue;
7221
Hal Finkel80d10de2013-05-24 23:00:14 +00007222 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
7223 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
7224
Hal Finkel80d10de2013-05-24 23:00:14 +00007225 SDValue ExtraLoad =
7226 DAG.getLoad(VT, dl, Chain, Ptr,
7227 LD->getPointerInfo().getWithOffset(IncOffset),
7228 LD->isVolatile(), LD->isNonTemporal(),
7229 LD->isInvariant(), ABIAlignment);
7230
7231 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7232 BaseLoad.getValue(1), ExtraLoad.getValue(1));
7233
7234 if (BaseLoad.getValueType() != MVT::v4i32)
7235 BaseLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, BaseLoad);
7236
7237 if (ExtraLoad.getValueType() != MVT::v4i32)
7238 ExtraLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ExtraLoad);
7239
7240 SDValue Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
7241 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
7242
7243 if (VT != MVT::v4i32)
7244 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
7245
7246 // Now we need to be really careful about how we update the users of the
7247 // original load. We cannot just call DCI.CombineTo (or
7248 // DAG.ReplaceAllUsesWith for that matter), because the load still has
7249 // uses created here (the permutation for example) that need to stay.
7250 SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
7251 while (UI != UE) {
7252 SDUse &Use = UI.getUse();
7253 SDNode *User = *UI;
7254 // Note: BaseLoad is checked here because it might not be N, but a
7255 // bitcast of N.
7256 if (User == Perm.getNode() || User == BaseLoad.getNode() ||
7257 User == TF.getNode() || Use.getResNo() > 1) {
7258 ++UI;
7259 continue;
7260 }
7261
7262 SDValue To = Use.getResNo() ? TF : Perm;
7263 ++UI;
7264
7265 SmallVector<SDValue, 8> Ops;
7266 for (SDNode::op_iterator O = User->op_begin(),
7267 OE = User->op_end(); O != OE; ++O) {
7268 if (*O == Use)
7269 Ops.push_back(To);
7270 else
7271 Ops.push_back(*O);
7272 }
7273
7274 DAG.UpdateNodeOperands(User, Ops.data(), Ops.size());
7275 }
7276
7277 return SDValue(N, 0);
7278 }
7279 }
7280 break;
Hal Finkel5a0e6042013-05-25 04:05:05 +00007281 case ISD::INTRINSIC_WO_CHAIN:
7282 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() ==
7283 Intrinsic::ppc_altivec_lvsl &&
7284 N->getOperand(1)->getOpcode() == ISD::ADD) {
7285 SDValue Add = N->getOperand(1);
7286
7287 if (DAG.MaskedValueIsZero(Add->getOperand(1),
7288 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
7289 Add.getValueType().getScalarType().getSizeInBits()))) {
7290 SDNode *BasePtr = Add->getOperand(0).getNode();
7291 for (SDNode::use_iterator UI = BasePtr->use_begin(),
7292 UE = BasePtr->use_end(); UI != UE; ++UI) {
7293 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
7294 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
7295 Intrinsic::ppc_altivec_lvsl) {
7296 // We've found another LVSL, and this address if an aligned
7297 // multiple of that one. The results will be the same, so use the
7298 // one we've just found instead.
7299
7300 return SDValue(*UI, 0);
7301 }
7302 }
7303 }
7304 }
Chris Lattnerd9989382006-07-10 20:56:58 +00007305 case ISD::BSWAP:
7306 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greifba36cb52008-08-28 21:40:38 +00007307 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnerd9989382006-07-10 20:56:58 +00007308 N->getOperand(0).hasOneUse() &&
Hal Finkelefdd4672013-03-28 19:25:55 +00007309 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
7310 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel2544f222013-03-28 20:23:46 +00007311 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkelefdd4672013-03-28 19:25:55 +00007312 N->getValueType(0) == MVT::i64))) {
Dan Gohman475871a2008-07-27 21:46:04 +00007313 SDValue Load = N->getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +00007314 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnerd9989382006-07-10 20:56:58 +00007315 // Create the byte-swapping load.
Dan Gohman475871a2008-07-27 21:46:04 +00007316 SDValue Ops[] = {
Evan Cheng466685d2006-10-09 20:57:25 +00007317 LD->getChain(), // Chain
7318 LD->getBasePtr(), // Ptr
Chris Lattner79e490a2006-08-11 17:18:05 +00007319 DAG.getValueType(N->getValueType(0)) // VT
7320 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00007321 SDValue BSLoad =
7322 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkelefdd4672013-03-28 19:25:55 +00007323 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
7324 MVT::i64 : MVT::i32, MVT::Other),
Hal Finkelb52980b2013-03-28 19:43:12 +00007325 Ops, 3, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnerd9989382006-07-10 20:56:58 +00007326
Scott Michelfdc40a02009-02-17 22:15:04 +00007327 // If this is an i16 load, insert the truncate.
Dan Gohman475871a2008-07-27 21:46:04 +00007328 SDValue ResVal = BSLoad;
Owen Anderson825b72b2009-08-11 20:47:22 +00007329 if (N->getValueType(0) == MVT::i16)
7330 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelfdc40a02009-02-17 22:15:04 +00007331
Chris Lattnerd9989382006-07-10 20:56:58 +00007332 // First, combine the bswap away. This makes the value produced by the
7333 // load dead.
7334 DCI.CombineTo(N, ResVal);
7335
7336 // Next, combine the load away, we give it a bogus result value but a real
7337 // chain result. The result value is dead because the bswap is dead.
Gabor Greifba36cb52008-08-28 21:40:38 +00007338 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00007339
Chris Lattnerd9989382006-07-10 20:56:58 +00007340 // Return N so it doesn't get rechecked!
Dan Gohman475871a2008-07-27 21:46:04 +00007341 return SDValue(N, 0);
Chris Lattnerd9989382006-07-10 20:56:58 +00007342 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007343
Chris Lattner51269842006-03-01 05:50:56 +00007344 break;
Chris Lattner4468c222006-03-31 06:02:07 +00007345 case PPCISD::VCMP: {
7346 // If a VCMPo node already exists with exactly the same operands as this
7347 // node, use its result instead of this node (VCMPo computes both a CR6 and
7348 // a normal output).
7349 //
7350 if (!N->getOperand(0).hasOneUse() &&
7351 !N->getOperand(1).hasOneUse() &&
7352 !N->getOperand(2).hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00007353
Chris Lattner4468c222006-03-31 06:02:07 +00007354 // Scan all of the users of the LHS, looking for VCMPo's that match.
7355 SDNode *VCMPoNode = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00007356
Gabor Greifba36cb52008-08-28 21:40:38 +00007357 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattner4468c222006-03-31 06:02:07 +00007358 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
7359 UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00007360 if (UI->getOpcode() == PPCISD::VCMPo &&
7361 UI->getOperand(1) == N->getOperand(1) &&
7362 UI->getOperand(2) == N->getOperand(2) &&
7363 UI->getOperand(0) == N->getOperand(0)) {
7364 VCMPoNode = *UI;
Chris Lattner4468c222006-03-31 06:02:07 +00007365 break;
7366 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007367
Chris Lattner00901202006-04-18 18:28:22 +00007368 // If there is no VCMPo node, or if the flag value has a single use, don't
7369 // transform this.
7370 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
7371 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007372
7373 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner00901202006-04-18 18:28:22 +00007374 // chain, this transformation is more complex. Note that multiple things
7375 // could use the value result, which we should ignore.
7376 SDNode *FlagUser = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00007377 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner00901202006-04-18 18:28:22 +00007378 FlagUser == 0; ++UI) {
7379 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman89684502008-07-27 20:43:25 +00007380 SDNode *User = *UI;
Chris Lattner00901202006-04-18 18:28:22 +00007381 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007382 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner00901202006-04-18 18:28:22 +00007383 FlagUser = User;
7384 break;
7385 }
7386 }
7387 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007388
Ulrich Weigand965b20e2013-07-03 17:05:42 +00007389 // If the user is a MFOCRF instruction, we know this is safe.
7390 // Otherwise we give up for right now.
7391 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman475871a2008-07-27 21:46:04 +00007392 return SDValue(VCMPoNode, 0);
Chris Lattner4468c222006-03-31 06:02:07 +00007393 }
7394 break;
7395 }
Chris Lattner90564f22006-04-18 17:59:36 +00007396 case ISD::BR_CC: {
7397 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigand965b20e2013-07-03 17:05:42 +00007398 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner90564f22006-04-18 17:59:36 +00007399 // lowering is done pre-legalize, because the legalizer lowers the predicate
7400 // compare down to code that is difficult to reassemble.
7401 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00007402 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkelb1fd3cd2013-05-15 21:37:41 +00007403
7404 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
7405 // value. If so, pass-through the AND to get to the intrinsic.
7406 if (LHS.getOpcode() == ISD::AND &&
7407 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
7408 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
7409 Intrinsic::ppc_is_decremented_ctr_nonzero &&
7410 isa<ConstantSDNode>(LHS.getOperand(1)) &&
7411 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
7412 isZero())
7413 LHS = LHS.getOperand(0);
7414
7415 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
7416 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
7417 Intrinsic::ppc_is_decremented_ctr_nonzero &&
7418 isa<ConstantSDNode>(RHS)) {
7419 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
7420 "Counter decrement comparison is not EQ or NE");
7421
7422 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
7423 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
7424 (CC == ISD::SETNE && !Val);
7425
7426 // We now need to make the intrinsic dead (it cannot be instruction
7427 // selected).
7428 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
7429 assert(LHS.getNode()->hasOneUse() &&
7430 "Counter decrement has more than one use");
7431
7432 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
7433 N->getOperand(0), N->getOperand(4));
7434 }
7435
Chris Lattner90564f22006-04-18 17:59:36 +00007436 int CompareOpc;
7437 bool isDot;
Scott Michelfdc40a02009-02-17 22:15:04 +00007438
Chris Lattner90564f22006-04-18 17:59:36 +00007439 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
7440 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
7441 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
7442 assert(isDot && "Can't compare against a vector result!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007443
Chris Lattner90564f22006-04-18 17:59:36 +00007444 // If this is a comparison against something other than 0/1, then we know
7445 // that the condition is never/always true.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007446 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00007447 if (Val != 0 && Val != 1) {
7448 if (CC == ISD::SETEQ) // Cond never true, remove branch.
7449 return N->getOperand(0);
7450 // Always !=, turn it into an unconditional branch.
Owen Anderson825b72b2009-08-11 20:47:22 +00007451 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner90564f22006-04-18 17:59:36 +00007452 N->getOperand(0), N->getOperand(4));
7453 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007454
Chris Lattner90564f22006-04-18 17:59:36 +00007455 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00007456
Chris Lattner90564f22006-04-18 17:59:36 +00007457 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman475871a2008-07-27 21:46:04 +00007458 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00007459 LHS.getOperand(2), // LHS of compare
7460 LHS.getOperand(3), // RHS of compare
Owen Anderson825b72b2009-08-11 20:47:22 +00007461 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00007462 };
Benjamin Kramer3853f742013-03-07 20:33:29 +00007463 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesen3484c092009-02-05 22:07:54 +00007464 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00007465
Chris Lattner90564f22006-04-18 17:59:36 +00007466 // Unpack the result based on how the target uses it.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007467 PPC::Predicate CompOpc;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007468 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner90564f22006-04-18 17:59:36 +00007469 default: // Can't happen, don't crash on invalid number though.
7470 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007471 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner90564f22006-04-18 17:59:36 +00007472 break;
7473 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007474 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner90564f22006-04-18 17:59:36 +00007475 break;
7476 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007477 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner90564f22006-04-18 17:59:36 +00007478 break;
7479 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007480 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner90564f22006-04-18 17:59:36 +00007481 break;
7482 }
7483
Owen Anderson825b72b2009-08-11 20:47:22 +00007484 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
7485 DAG.getConstant(CompOpc, MVT::i32),
7486 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner90564f22006-04-18 17:59:36 +00007487 N->getOperand(4), CompNode.getValue(1));
7488 }
7489 break;
7490 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00007491 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007492
Dan Gohman475871a2008-07-27 21:46:04 +00007493 return SDValue();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00007494}
7495
Chris Lattner1a635d62006-04-14 06:01:58 +00007496//===----------------------------------------------------------------------===//
7497// Inline Assembly Support
7498//===----------------------------------------------------------------------===//
7499
Dan Gohman475871a2008-07-27 21:46:04 +00007500void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Scott Michelfdc40a02009-02-17 22:15:04 +00007501 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007502 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007503 const SelectionDAG &DAG,
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007504 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00007505 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007506 switch (Op.getOpcode()) {
7507 default: break;
Chris Lattnerd9989382006-07-10 20:56:58 +00007508 case PPCISD::LBRX: {
7509 // lhbrx is known to have the top bits cleared out.
Dan Gohmanae03af22009-09-27 23:17:47 +00007510 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnerd9989382006-07-10 20:56:58 +00007511 KnownZero = 0xFFFF0000;
7512 break;
7513 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007514 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007515 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007516 default: break;
7517 case Intrinsic::ppc_altivec_vcmpbfp_p:
7518 case Intrinsic::ppc_altivec_vcmpeqfp_p:
7519 case Intrinsic::ppc_altivec_vcmpequb_p:
7520 case Intrinsic::ppc_altivec_vcmpequh_p:
7521 case Intrinsic::ppc_altivec_vcmpequw_p:
7522 case Intrinsic::ppc_altivec_vcmpgefp_p:
7523 case Intrinsic::ppc_altivec_vcmpgtfp_p:
7524 case Intrinsic::ppc_altivec_vcmpgtsb_p:
7525 case Intrinsic::ppc_altivec_vcmpgtsh_p:
7526 case Intrinsic::ppc_altivec_vcmpgtsw_p:
7527 case Intrinsic::ppc_altivec_vcmpgtub_p:
7528 case Intrinsic::ppc_altivec_vcmpgtuh_p:
7529 case Intrinsic::ppc_altivec_vcmpgtuw_p:
7530 KnownZero = ~1U; // All bits but the low one are known to be zero.
7531 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007532 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007533 }
7534 }
7535}
7536
7537
Chris Lattner4234f572007-03-25 02:14:49 +00007538/// getConstraintType - Given a constraint, return the type of
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00007539/// constraint it is for this target.
Scott Michelfdc40a02009-02-17 22:15:04 +00007540PPCTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007541PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
7542 if (Constraint.size() == 1) {
7543 switch (Constraint[0]) {
7544 default: break;
7545 case 'b':
7546 case 'r':
7547 case 'f':
7548 case 'v':
7549 case 'y':
7550 return C_RegisterClass;
Hal Finkel827b7a02012-11-05 18:18:42 +00007551 case 'Z':
7552 // FIXME: While Z does indicate a memory constraint, it specifically
7553 // indicates an r+r address (used in conjunction with the 'y' modifier
7554 // in the replacement string). Currently, we're forcing the base
7555 // register to be r0 in the asm printer (which is interpreted as zero)
7556 // and forming the complete address in the second register. This is
7557 // suboptimal.
7558 return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00007559 }
7560 }
7561 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00007562}
7563
John Thompson44ab89e2010-10-29 17:29:13 +00007564/// Examine constraint type and operand type and determine a weight value.
7565/// This object must already have been set up with the operand type
7566/// and the current alternative constraint selected.
7567TargetLowering::ConstraintWeight
7568PPCTargetLowering::getSingleConstraintMatchWeight(
7569 AsmOperandInfo &info, const char *constraint) const {
7570 ConstraintWeight weight = CW_Invalid;
7571 Value *CallOperandVal = info.CallOperandVal;
7572 // If we don't have a value, we can't do a match,
7573 // but allow it at the lowest weight.
7574 if (CallOperandVal == NULL)
7575 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007576 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00007577 // Look at the constraint type.
7578 switch (*constraint) {
7579 default:
7580 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7581 break;
7582 case 'b':
7583 if (type->isIntegerTy())
7584 weight = CW_Register;
7585 break;
7586 case 'f':
7587 if (type->isFloatTy())
7588 weight = CW_Register;
7589 break;
7590 case 'd':
7591 if (type->isDoubleTy())
7592 weight = CW_Register;
7593 break;
7594 case 'v':
7595 if (type->isVectorTy())
7596 weight = CW_Register;
7597 break;
7598 case 'y':
7599 weight = CW_Register;
7600 break;
Hal Finkel827b7a02012-11-05 18:18:42 +00007601 case 'Z':
7602 weight = CW_Memory;
7603 break;
John Thompson44ab89e2010-10-29 17:29:13 +00007604 }
7605 return weight;
7606}
7607
Scott Michelfdc40a02009-02-17 22:15:04 +00007608std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner331d1bc2006-11-02 01:44:04 +00007609PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier5b3fca52013-06-22 18:37:38 +00007610 MVT VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00007611 if (Constraint.size() == 1) {
Chris Lattner331d1bc2006-11-02 01:44:04 +00007612 // GCC RS6000 Constraint Letters
7613 switch (Constraint[0]) {
7614 case 'b': // R1-R31
Hal Finkela548afc2013-03-19 18:51:05 +00007615 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
7616 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
7617 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00007618 case 'r': // R0-R31
Owen Anderson825b72b2009-08-11 20:47:22 +00007619 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperc9099502012-04-20 06:31:50 +00007620 return std::make_pair(0U, &PPC::G8RCRegClass);
7621 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00007622 case 'f':
Ulrich Weigand78dab642012-10-29 17:49:34 +00007623 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00007624 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand78dab642012-10-29 17:49:34 +00007625 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00007626 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00007627 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007628 case 'v':
Craig Topperc9099502012-04-20 06:31:50 +00007629 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00007630 case 'y': // crrc
Craig Topperc9099502012-04-20 06:31:50 +00007631 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattnerddc787d2006-01-31 19:20:21 +00007632 }
7633 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007634
Hal Finkel5cad12d2013-08-03 12:25:10 +00007635 std::pair<unsigned, const TargetRegisterClass*> R =
7636 TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7637
7638 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
7639 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
7640 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
7641 // register.
7642 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
7643 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
7644 if (R.first && VT == MVT::i64 && PPCSubTarget.isPPC64() &&
7645 PPC::GPRCRegClass.contains(R.first)) {
7646 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
7647 return std::make_pair(TRI->getMatchingSuperReg(R.first,
7648 PPC::sub_32, &PPC::GPRCRegClass),
7649 &PPC::G8RCRegClass);
7650 }
7651
7652 return R;
Chris Lattnerddc787d2006-01-31 19:20:21 +00007653}
Chris Lattner763317d2006-02-07 00:47:13 +00007654
Chris Lattner331d1bc2006-11-02 01:44:04 +00007655
Chris Lattner48884cd2007-08-25 00:47:38 +00007656/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesen1784d162010-06-25 21:55:36 +00007657/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher471e4222011-06-08 23:55:35 +00007658void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007659 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +00007660 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00007661 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007662 SDValue Result(0,0);
Eric Christopher471e4222011-06-08 23:55:35 +00007663
Eric Christopher100c8332011-06-02 23:16:42 +00007664 // Only support length 1 constraints.
7665 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007666
Eric Christopher100c8332011-06-02 23:16:42 +00007667 char Letter = Constraint[0];
Chris Lattner763317d2006-02-07 00:47:13 +00007668 switch (Letter) {
7669 default: break;
7670 case 'I':
7671 case 'J':
7672 case 'K':
7673 case 'L':
7674 case 'M':
7675 case 'N':
7676 case 'O':
7677 case 'P': {
Chris Lattner9f5d5782007-05-15 01:31:05 +00007678 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattner48884cd2007-08-25 00:47:38 +00007679 if (!CST) return; // Must be an immediate to match.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007680 unsigned Value = CST->getZExtValue();
Chris Lattner763317d2006-02-07 00:47:13 +00007681 switch (Letter) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007682 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner763317d2006-02-07 00:47:13 +00007683 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007684 if ((short)Value == (int)Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00007685 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007686 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007687 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
7688 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007689 if ((short)Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00007690 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007691 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007692 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007693 if ((Value >> 16) == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00007694 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007695 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007696 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007697 if (Value > 31)
Chris Lattner48884cd2007-08-25 00:47:38 +00007698 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007699 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007700 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007701 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattner48884cd2007-08-25 00:47:38 +00007702 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007703 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007704 case 'O': // "O" is the constant zero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007705 if (Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00007706 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007707 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007708 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007709 if ((short)-Value == (int)-Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00007710 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007711 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007712 }
7713 break;
7714 }
7715 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007716
Gabor Greifba36cb52008-08-28 21:40:38 +00007717 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00007718 Ops.push_back(Result);
7719 return;
7720 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007721
Chris Lattner763317d2006-02-07 00:47:13 +00007722 // Handle standard constraint letters.
Eric Christopher100c8332011-06-02 23:16:42 +00007723 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner763317d2006-02-07 00:47:13 +00007724}
Evan Chengc4c62572006-03-13 23:20:37 +00007725
Chris Lattnerc9addb72007-03-30 23:15:24 +00007726// isLegalAddressingMode - Return true if the addressing mode represented
7727// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007728bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007729 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +00007730 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelfdc40a02009-02-17 22:15:04 +00007731
Chris Lattnerc9addb72007-03-30 23:15:24 +00007732 // PPC allows a sign-extended 16-bit immediate field.
7733 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
7734 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007735
Chris Lattnerc9addb72007-03-30 23:15:24 +00007736 // No global is ever allowed as a base.
7737 if (AM.BaseGV)
7738 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007739
7740 // PPC only support r+r,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007741 switch (AM.Scale) {
7742 case 0: // "r+i" or just "i", depending on HasBaseReg.
7743 break;
7744 case 1:
7745 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
7746 return false;
7747 // Otherwise we have r+r or r+i.
7748 break;
7749 case 2:
7750 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
7751 return false;
7752 // Allow 2*r as r+r.
7753 break;
Chris Lattner7c7ba9d2007-04-09 22:10:05 +00007754 default:
7755 // No other scales are supported.
7756 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007757 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007758
Chris Lattnerc9addb72007-03-30 23:15:24 +00007759 return true;
7760}
7761
Dan Gohmand858e902010-04-17 15:26:15 +00007762SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
7763 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007764 MachineFunction &MF = DAG.getMachineFunction();
7765 MachineFrameInfo *MFI = MF.getFrameInfo();
7766 MFI->setReturnAddressIsTaken(true);
7767
Andrew Trickac6d9be2013-05-25 02:42:55 +00007768 SDLoc dl(Op);
Dale Johannesen08673d22010-05-03 22:59:34 +00007769 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattner3fc027d2007-12-08 06:59:59 +00007770
Dale Johannesen08673d22010-05-03 22:59:34 +00007771 // Make sure the function does not optimize away the store of the RA to
7772 // the stack.
Chris Lattner3fc027d2007-12-08 06:59:59 +00007773 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen08673d22010-05-03 22:59:34 +00007774 FuncInfo->setLRStoreRequired();
7775 bool isPPC64 = PPCSubTarget.isPPC64();
7776 bool isDarwinABI = PPCSubTarget.isDarwinABI();
7777
7778 if (Depth > 0) {
7779 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7780 SDValue Offset =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007781
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00007782 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen08673d22010-05-03 22:59:34 +00007783 isPPC64? MVT::i64 : MVT::i32);
7784 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
7785 DAG.getNode(ISD::ADD, dl, getPointerTy(),
7786 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007787 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen08673d22010-05-03 22:59:34 +00007788 }
Chris Lattner3fc027d2007-12-08 06:59:59 +00007789
Chris Lattner3fc027d2007-12-08 06:59:59 +00007790 // Just load the return address off the stack.
Dan Gohman475871a2008-07-27 21:46:04 +00007791 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen08673d22010-05-03 22:59:34 +00007792 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007793 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattner3fc027d2007-12-08 06:59:59 +00007794}
7795
Dan Gohmand858e902010-04-17 15:26:15 +00007796SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
7797 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007798 SDLoc dl(Op);
Dale Johannesen08673d22010-05-03 22:59:34 +00007799 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007800
Owen Andersone50ed302009-08-10 22:56:29 +00007801 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00007802 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelfdc40a02009-02-17 22:15:04 +00007803
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00007804 MachineFunction &MF = DAG.getMachineFunction();
7805 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen08673d22010-05-03 22:59:34 +00007806 MFI->setFrameAddressIsTaken(true);
Hal Finkele9cc0a02013-03-21 19:03:19 +00007807
7808 // Naked functions never have a frame pointer, and so we use r1. For all
7809 // other functions, this decision must be delayed until during PEI.
7810 unsigned FrameReg;
7811 if (MF.getFunction()->getAttributes().hasAttribute(
7812 AttributeSet::FunctionIndex, Attribute::Naked))
7813 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
7814 else
7815 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
7816
Dale Johannesen08673d22010-05-03 22:59:34 +00007817 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
7818 PtrVT);
7819 while (Depth--)
7820 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007821 FrameAddr, MachinePointerInfo(), false, false,
7822 false, 0);
Dale Johannesen08673d22010-05-03 22:59:34 +00007823 return FrameAddr;
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00007824}
Dan Gohman54aeea32008-10-21 03:41:46 +00007825
7826bool
7827PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7828 // The PowerPC target isn't yet aware of offsets.
7829 return false;
7830}
Tilmann Schellerffd02002009-07-03 06:45:56 +00007831
Evan Cheng42642d02010-04-01 20:10:42 +00007832/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengf28f8bc2010-04-02 19:36:14 +00007833/// and store operations as a result of memset, memcpy, and memmove
7834/// lowering. If DstAlign is zero that means it's safe to destination
7835/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
7836/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00007837/// probably because the source does not need to be loaded. If 'IsMemset' is
7838/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
7839/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
7840/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00007841/// It returns EVT::Other if the type should be determined using generic
7842/// target-independent logic.
Evan Cheng255f20f2010-04-01 06:04:33 +00007843EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
7844 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00007845 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00007846 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00007847 MachineFunction &MF) const {
Tilmann Schellerffd02002009-07-03 06:45:56 +00007848 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007849 return MVT::i64;
Tilmann Schellerffd02002009-07-03 06:45:56 +00007850 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00007851 return MVT::i32;
Tilmann Schellerffd02002009-07-03 06:45:56 +00007852 }
7853}
Hal Finkel3f31d492012-04-01 19:23:08 +00007854
Hal Finkel2d37f7b2013-03-15 15:27:13 +00007855bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
7856 bool *Fast) const {
7857 if (DisablePPCUnaligned)
7858 return false;
7859
7860 // PowerPC supports unaligned memory access for simple non-vector types.
7861 // Although accessing unaligned addresses is not as efficient as accessing
7862 // aligned addresses, it is generally more efficient than manual expansion,
7863 // and generally only traps for software emulation when crossing page
7864 // boundaries.
7865
7866 if (!VT.isSimple())
7867 return false;
7868
7869 if (VT.getSimpleVT().isVector())
7870 return false;
7871
7872 if (VT == MVT::ppcf128)
7873 return false;
7874
7875 if (Fast)
7876 *Fast = true;
7877
7878 return true;
7879}
7880
Stephen Line54885a2013-07-09 18:16:56 +00007881bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
7882 VT = VT.getScalarType();
7883
Hal Finkel070b8db2012-06-22 00:49:52 +00007884 if (!VT.isSimple())
7885 return false;
7886
7887 switch (VT.getSimpleVT().SimpleTy) {
7888 case MVT::f32:
7889 case MVT::f64:
Hal Finkel070b8db2012-06-22 00:49:52 +00007890 return true;
7891 default:
7892 break;
7893 }
7894
7895 return false;
7896}
7897
Hal Finkel3f31d492012-04-01 19:23:08 +00007898Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel71ffcfe2012-06-10 19:32:29 +00007899 if (DisableILPPref)
7900 return TargetLowering::getSchedulingPreference(N);
Hal Finkel3f31d492012-04-01 19:23:08 +00007901
Hal Finkel71ffcfe2012-06-10 19:32:29 +00007902 return Sched::ILP;
Hal Finkel3f31d492012-04-01 19:23:08 +00007903}
7904
Bill Schmidt646cd792013-07-30 00:50:39 +00007905// Create a fast isel object.
7906FastISel *
7907PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
7908 const TargetLibraryInfo *LibInfo) const {
7909 return PPC::createFastISel(FuncInfo, LibInfo);
7910}